summaryrefslogtreecommitdiffstats
path: root/perl-install/partition_table_bsd.pm
blob: cb6a8441ebb886ce24d50e245bb2eb3f4c0f34ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package partition_table_bsd; # $Id$

use diagnostics;
use strict;
use vars qw(@ISA);

@ISA = qw(partition_table_raw);

use common;
use partition_table_raw;
use partition_table;
use c;

#- very bad and rough handling :(
my %typeToDos = (
  8 => 0x83,
  1 => 0x82,
);
my %typeFromDos = reverse %typeToDos;

my ($main_format, $main_fields) = list2kv(
  I   => 'magic',
  S   => 'type',
  S   => 'subtype',
  a16 => 'typename',
  a16 => 'packname',
  I   => 'secsize',
  I   => 'nsectors',
  I   => 'ntracks',
  I   => 'ncylinders',
  I   => 'secpercyl',
  I   => 'secprtunit',
  S   => 'sparespertrack',
  S   => 'sparespercyl',
  I   => 'acylinders',
  S   => 'rpm',
  S   => 'interleave',
  S   => 'trackskew',
  S   => 'cylskew',
  I   => 'headswitch',
  I   => 'trkseek',
  I   => 'flags',
  a20 => 'drivedata',
  a20 => 'spare',
  I   => 'magic2',
  S   => 'checksum',
  S   => 'npartitions',
  I   => 'bbsize',
  I   => 'sbsize',
  a128=> 'partitions',
  a236=> 'blank',
);
$main_format = join '', @$main_format;

my @fields = qw(size start fsize type frag cpg);
my $format = "I I I C C S";
my $magic = 0x82564557;
my $nb_primary = 8;
my $offset = 0x40;

sub read($$) {
    my ($hd, $sector) = @_;
    my $tmp;

    local *F; partition_table_raw::openit($hd, *F) or die "failed to open device";
    c::lseek_sector(fileno(F), $sector, $offset) or die "reading of partition in sector $sector failed";

    sysread F, $tmp, psizeof($main_format) or die "error while reading partition table in sector $sector";
    my %info; @info{@$main_fields} = unpack $main_format, $tmp;

    #- TODO verify checksum

    my $size = psizeof($format);
    my @pt = map {
	my %h; @h{@fields} = unpack $format, $_;
	$h{type} = $typeToDos{$h{type}} || $h{type};
	\%h;
    } $info{partitions} =~ /(.{$size})/g;

    #- check magic number
    $info{magic}  == $magic or die "bad magic number";an>

my ($guid_format, $guid_fields) = list2kv(
  N     => 'time_low',
  n     => 'time_mid',
  n     => 'time_hi_and_version',
  n     => 'clock_seq',
  a6    => 'node',
);

$_ = join('', @$_) foreach $main_format, $partitionEntry_format, $guid_format;

my $magic = "EFI PART";

sub generate_guid {
    my $tmp;
    local *F;
    open F, devices::make("random") or die "Could not open /dev/random for GUID generation";
    read F, $tmp, psizeof($guid_format);
	
    my %guid; @guid{@$guid_fields} = unpack $guid_format, $tmp;
    $guid{clock_seq} = ($guid{clock_seq} & 0x3fff) | 0x8000;
    $guid{time_hi_and_version} = ($guid{time_hi_and_version} & 0x0fff) | 0x4000;
    pack($guid_format, @guid{@$guid_fields});
}

sub crc32 {
    my ($buffer) = @_;

    my $crc = 0xFFFFFFFF;
    foreach (unpack "C*", $buffer) {
	my $subcrc = ($crc ^ $_) & 0xFF;
        for (my $j = 8; $j > 0; $j--){
	    my $b = $subcrc & 1;
	    $subcrc = ($subcrc >> 1) & 0x7FFFFFFF;
	    $subcrc = $subcrc ^ 0xEDB88320 if $b;
        }
        $crc = ($crc >> 8) ^ $subcrc;
    }
    $crc ^ 0xFFFFFFFF;
}

sub compute_headerCRC32 {
    my ($info) = @_;
    local $info->{headerCRC32} = 0;
    crc32(pack($main_format, @$info{@$main_fields}));
}

sub read_header {
    my ($sector, $F) = @_;
    my $tmp;

    c::lseek_sector(fileno($F), $sector, 0) or die "reading of partition in sector $sector failed";

    sysread $F, $tmp, psizeof($main_format) or die "error while reading partition table in sector $sector";
    my %info; @info{@$main_fields} = unpack $main_format, $tmp;
    
    $info{magic} eq $magic or die "bad magic number";
    $info{myLBA} == $sector or die "myLBA is not the same";
    $info{headerSize} == psizeof($main_format) or die "bad partition table header size";
    $info{partitionEntrySize} == psizeof($partitionEntry_format) or die "bad partitionEntrySize";
    $info{revision} <= $current_revision or log::l("oops, this is a new GPT revision ($info{revision} > $current_revision)");

    $info{headerCRC32} == compute_headerCRC32(\%info) or die "bad partition table checksum";
    \%info
}

sub read_partitionEntries {
    my ($info, $F) = @_;
    my $tmp;

    c::lseek_sector(fileno($F), $info->{partitionEntriesLBA}, 0) or die "can't seek to sector partitionEntriesLBA";
    sysread $F, $tmp, psizeof($partitionEntry_format) * $info->{nbPartitions} or die "error while reading partition table in sector $info->{partitionEntriesLBA}";
    $info->{partitionEntriesCRC32} == crc32($tmp) or die "bad partition entries checksum";

    c::lseek_sector(fileno($F), $info->{partitionEntriesLBA}, 0) or die "can't seek to sector partitionEntriesLBA";
    my %gpt_types_rev = reverse %gpt_types;
    my @pt = 
      map {
	sysread $F, $tmp, psizeof($partitionEntry_format) or die "error while reading partition table in sector $info->{partitionEntriesLBA}";
	my %h; @h{@$partitionEntry_fields} = unpack $partitionEntry_format, $tmp;
	$h{size} = $h{ending} - $h{start} + 1;
	$h{type} = $gpt_types_rev{$h{gpt_type}};
	$h{type} = 0x100 if !defined $h{type};
	\%h;
    } (1 .. $info->{nbPartitions});
    \@pt;
}

sub read {
    my ($hd, $sector) = @_;
    my $tmp;

    my $l = partition_table_dos::read($hd, $sector);
    my @l = grep { $_->{size} && $_->{type} && !partition_table::isExtended($_) } @$l;
    @l == 1 or die "bad PMBR";
    $l[0]{type} == 0xee or die "bad PMBR";
    my $myLBA = $l[0]{start};

    local *F; partition_table_raw::openit($hd, *F) or die "failed to open device";
    my $info1 = eval { read_header($myLBA, *F) };
    my $info2 = eval { read_header($info1->{alternateLBA} || $l[0]{start} + $l[0]{size} - 1, *F) }; #- what about using $hd->{totalsectors} ???
    my $info = $info1 || { %$info2, myLBA => $info2->{alternateLBA}, alternateLBA => $info2->{myLBA}, partitionEntriesLBA => $info2->{alternateLBA} + 1 } or die;
    my $pt = $info1 && $info2 ? 
	eval { $info1 && read_partitionEntries($info1, *F) } || read_partitionEntries($info2, *F) :
	read_partitionEntries($info, *F);
    $hd->raw_removed($pt);

    $pt, $info;
}

# write the partition table (and extended ones)
# for each entry, it uses fields: start, size, type, active
sub write {
    my ($hd, $sector, $pt, $info) = @_;

    foreach (@$pt) {
	$_->{ending} = $_->{start} + $_->{size} - 1;
	$_->{guid} ||= generate_guid;
	$_->{gpt_type} = $gpt_types{$_->{type}} || $_->{gpt_type} || $gpt_types{0x83};
    }
    my $partitionEntries = join('', map {
	pack($partitionEntry_format, @$_{@$partitionEntry_fields})	
    } (@$pt, ({}) x ($info->{nbPartitions} - @$pt)));

    $info->{partitionEntriesCRC32} = crc32($partitionEntries);
    $info->{headerCRC32} = compute_headerCRC32($info);

    my $info2 = { %$info, 
		  myLBA => $info->{alternateLBA}, alternateLBA => $info->{myLBA}, 
		  partitionEntriesLBA => $info->{alternateLBA} - psizeof($partitionEntry_format) * $info->{nbPartitions} / 512,
		};
    $info2->{headerCRC32} = compute_headerCRC32($info2);

    {
	# write the PMBR
	my $pmbr = partition_table_dos::clear_raw();
	$pmbr->{raw}[0] = { type => 0xee, local_start => $info->{myLBA}, size => $info->{alternateLBA} - $info->{myLBA} + 1 };
	partition_table_dos::write($hd, $sector, $pmbr->{raw});
    }

    local *F;
    partition_table_raw::openit($hd, *F, 2) or die "error opening device $hd->{device} for writing";
    
    c::lseek_sector(fileno(F), $info->{myLBA}, 0) or return 0;
    #- pad with 0's
    syswrite F, pack($main_format, @$info{@$main_fields}) . "\0" x 512, 512 or return 0;

    c::lseek_sector(fileno(F), $info->{alternateLBA}, 0) or return 0;
    #- pad with 0's
    syswrite F, pack($main_format, @$info2{@$main_fields}) . "\0" x 512, 512 or return 0;

    c::lseek_sector(fileno(F), $info->{partitionEntriesLBA}, 0) or return 0;
    syswrite F, $partitionEntries or return 0;
    
    c::lseek_sector(fileno(F), $info2->{partitionEntriesLBA}, 0) or return 0;
    syswrite F, $partitionEntries or return 0;

    common::sync();
    1;
}

sub raw_removed {
    my ($hd, $raw) = @_;
    @$raw = grep { $_->{size} && $_->{type} } @$raw;
}
sub can_raw_add {
    my ($hd) = @_;
    @{$hd->{primary}{raw}} < $hd->{primary}{info}{nbPartitions};
}
sub raw_add {
    my ($hd, $raw, $part) = @_;
    $hd->can_raw_add or die "raw_add: partition table already full";
    push @$raw, $part;
}

sub adjustStart {}
sub adjustEnd {}

sub first_usable_sector {
    my ($hd) = @_;
    $hd->{primary}{info}{firstUsableLBA};
}
sub last_usable_sector { 
    my ($hd) = @_;
    $hd->{primary}{info}{lastUsableLBA} + 1;
}

sub info {
    my ($hd) = @_;
    my $nb_sect = 32;

    #- build a default suitable partition table,
    #- checksum will be built when writing on disk.
    {
	magic => $magic,
	revision => $current_revision,
	headerSize => psizeof($main_format),
	myLBA => 1,
	alternateLBA => $hd->{totalsectors} - 1,
	firstUsableLBA => $nb_sect + 2,
	lastUsableLBA => $hd->{totalsectors} - $nb_sect - 2,
	guid => generate_guid,
	partitionEntriesLBA => 2,
	nbPartitions => $nb_sect * 512 / psizeof($partitionEntry_format),
	partitionEntrySize => psizeof($partitionEntry_format),
    };
}

sub clear_raw {
    my ($hd) = @_;
    { raw => [], info => info($hd) };
}

1;