diff options
Diffstat (limited to 'perl-install/partition_table')
-rw-r--r-- | perl-install/partition_table/bsd.pm | 147 | ||||
-rw-r--r-- | perl-install/partition_table/dos.pm | 99 | ||||
-rw-r--r-- | perl-install/partition_table/empty.pm | 36 | ||||
-rw-r--r-- | perl-install/partition_table/gpt.pm | 266 | ||||
-rw-r--r-- | perl-install/partition_table/lvm_PV.pm | 43 | ||||
-rw-r--r-- | perl-install/partition_table/mac.pm | 398 | ||||
-rw-r--r-- | perl-install/partition_table/raw.pm | 192 | ||||
-rw-r--r-- | perl-install/partition_table/sun.pm | 201 |
8 files changed, 1382 insertions, 0 deletions
diff --git a/perl-install/partition_table/bsd.pm b/perl-install/partition_table/bsd.pm new file mode 100644 index 000000000..431d17f1f --- /dev/null +++ b/perl-install/partition_table/bsd.pm @@ -0,0 +1,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 on disk $hd->{device}"; + $info{magic2} == $magic or die "bad magic number on disk $hd->{device}"; + + [ @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) = @_; + + #- handle testing for writing partition table on file only! + local *F; + if ($::testing) { + my $file = "/tmp/partition_table_$hd->{device}"; + open F, ">$file" or die "error opening test file $file"; + } else { + partition_table::raw::openit($hd, *F, 2) or die "error opening device $hd->{device} for writing"; + c::lseek_sector(fileno(F), $sector, $offset) or return 0; + } + + #- TODO compute checksum + + $info->{npartitions} = $nb_primary; #- is it ok? + + @$pt == $nb_primary or die "partition table does not have $nb_primary entries"; + $info->{partitions} = join '', map { + local $_->{type} = $typeFromDos{$_->{type}} || $_->{type}; + pack $format, @$_{@fields}; + } @$pt; + + syswrite F, pack($main_format, @$info{@$main_fields}), psizeof($main_format) or return 0; + 1; +} + +sub info { + my ($hd) = @_; + my $dtype_scsi = 4; #- taken from fdisk, removed unused one, + my $dtype_ST506 = 6; #- see fdisk for more + + { + magic => $magic, + magic2 => $magic, + dtype => $hd->{device} =~ /^sd/ ? $dtype_scsi : $dtype_ST506, + secsize => $common::SECTORSIZE, + ncylinders => $hd->{geom}{cylinders}, + secpercyl => $hd->cylinder_size(), + secprtunit => $hd->{geom}{totalsectors}, + rpm => 3600, + interleave => 1, + trackskew => 0, + cylskew => 0, + headswitch => 0, + trkseek => 0, + bbsize => 8192, #- size of boot area, with label + sbsize => 8192, #- max size of fs superblock + }; +} + +sub clear_raw { + my ($hd) = @_; + { raw => [ ({}) x $nb_primary ], info => info($hd) }; +} + +sub first_usable_sector { 2048 } + +1; diff --git a/perl-install/partition_table/dos.pm b/perl-install/partition_table/dos.pm new file mode 100644 index 000000000..a6ad4be76 --- /dev/null +++ b/perl-install/partition_table/dos.pm @@ -0,0 +1,99 @@ +package partition_table::dos; # $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; + +my @fields = qw(active start_head start_sec start_cyl type end_head end_sec end_cyl start size); +my $format = "C8 V2"; +my $magic = "\x55\xAA"; +my $nb_primary = 4; + +my $offset = $common::SECTORSIZE - length($magic) - $nb_primary * common::psizeof($format); + +sub hasExtended { 1 } + +sub compute_CHS($$) { + my ($hd, $e) = @_; + my @l = qw(cyl head sec); + @$e{map { "start_$_" } @l} = $e->{start} || $e->{type} ? CHS2rawCHS($hd, sector2CHS($hd, $e->{start})) : (0,0,0); + @$e{map { "end_$_" } @l} = $e->{start} || $e->{type} ? CHS2rawCHS($hd, sector2CHS($hd, $e->{start} + $e->{size} - 1)) : (0,0,0); + 1; +} + +sub CHS2rawCHS { + my ($hd, $c, $h, $s) = @_; + if ($c > 1023) { + #- no way to have a #cylinder >= 1024 + $c = 1023; + $h = $hd->{geom}{heads} - 1; + $s = $hd->{geom}{sectors}; + } + ($c & 0xff, $h, $s | ($c >> 2 & 0xc0)); +} + +# returns (cylinder, head, sector) +sub sector2CHS { + my ($hd, $start) = @_; + my ($s, $h); + ($start, $s) = divide($start, $hd->{geom}{sectors}); + ($start, $h) = divide($start, $hd->{geom}{heads}); + ($start, $h, $s + 1); +} + +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"; + + my @pt = map { + sysread F, $tmp, psizeof($format) or die "error while reading partition table in sector $sector"; + my %h; @h{@fields} = unpack $format, $tmp; + \%h; + } (1..$nb_primary); + + #- check magic number + sysread F, $tmp, length $magic or die "error reading magic number on disk $hd->{device}"; + $tmp eq $magic or die "bad magic number on disk $hd->{device}"; + + [ @pt ]; +} + +# write the partition table (and extended ones) +# for each entry, it uses fields: start, size, type, active +sub write($$$;$) { + my ($hd, $sector, $pt) = @_; + + #- handle testing for writing partition table on file only! + local *F; + if ($::testing) { + my $file = "/tmp/partition_table_$hd->{device}"; + open F, ">$file" or die "error opening test file $file"; + } else { + partition_table::raw::openit($hd, *F, 2) or die "error opening device $hd->{device} for writing"; + c::lseek_sector(fileno(F), $sector, $offset) or return 0; + } + + @$pt == $nb_primary or die "partition table does not have $nb_primary entries"; + foreach (@$pt) { + compute_CHS($hd, $_); + local $_->{start} = $_->{local_start} || 0; + $_->{active} ||= 0; $_->{type} ||= 0; $_->{size} ||= 0; #- for no warning + syswrite F, pack($format, @$_{@fields}), psizeof($format) or return 0; + } + syswrite F, $magic, length $magic or return 0; + 1; +} + +sub clear_raw { { raw => [ ({}) x $nb_primary ] } } + +1; diff --git a/perl-install/partition_table/empty.pm b/perl-install/partition_table/empty.pm new file mode 100644 index 000000000..e90991684 --- /dev/null +++ b/perl-install/partition_table/empty.pm @@ -0,0 +1,36 @@ +package partition_table::empty; # $Id$ + +#- this is a mainly dummy partition table. If we find it's empty, we just call - +#- zero_MBR which will take care of bless'ing us to the partition table type best +#- suited + + +use diagnostics; +use strict; +use vars qw(@ISA); + +@ISA = qw(partition_table::raw); + +use common; +use partition_table::raw; +use partition_table; +use c; + + +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, 0) or die "reading of partition in sector $sector failed"; + + #- check magic number + sysread F, $tmp, 512 or die "error reading magic number on disk $hd->{device}"; + $tmp eq substr($tmp, 0, 1) x 512 or die "bad magic number on disk $hd->{device}"; + + partition_table::raw::zero_MBR($hd); + + $hd->{primary}{raw}; +} + +1; diff --git a/perl-install/partition_table/gpt.pm b/perl-install/partition_table/gpt.pm new file mode 100644 index 000000000..01de8f8a7 --- /dev/null +++ b/perl-install/partition_table/gpt.pm @@ -0,0 +1,266 @@ +package partition_table::gpt; # $Id$ + +use diagnostics; +use strict; +use vars qw(@ISA); + +@ISA = qw(partition_table::raw); + +use common; +use partition_table::raw; +use partition_table::dos; +use partition_table; +use c; + +my %gpt_types = ( + 0x00 => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + 0x82 => "\x6d\xfd\x57\x06\xab\xa4\xc4\x43\x84\xe5\x09\x33\xc8\x4b\x4f\x4f", + 0x83 => "\xA2\xA0\xD0\xEB\xE5\xB9\x33\x44\x87\xC0\x68\xB6\xB7\x26\x99\xC7", + 0x8e => "\x79\xd3\xd6\xe6\x07\xf5\xc2\x44\xa2\x3c\x23\x8f\x2a\x3d\xf9\x28", + 0xfd => "\x0f\x88\x9d\xa1\xfc\x05\x3b\x4d\xa0\x06\x74\x3f\x0f\x84\x91\x1e", + 0xef => "\x28\x73\x2A\xC1\x1F\xF8\xd2\x11\xBA\x4B\x00\xA0\xC9\x3E\xC9\x3B", + # legacy_partition_table => "\x41\xEE\x4D\x02\xE7\x33\xd3\x11\x9D\x69\x00\x08\xC7\x81\xF3\x9F" + # PARTITION_MSFT_RESERVED_GUID "\x16\xE3\xC9\xE3\x5C\x0B\xB8\x4D\x81\x7D\xF9\x2D\xF0\x02\x15\xAE" + #PARTITION_RESERVED_GUID "\x39\x33\xa6\x8d\x07\x00\xc0\x60\xc4\x36\x08\x3a\xc8\x23\x09\x08" +); + +my $current_revision = 0x00010200; +my ($main_format, $main_fields) = list2kv( + a8 => 'magic', + V => 'revision', + V => 'headerSize', + V => 'headerCRC32', + a4 => 'blank1', + Q => 'myLBA', + Q => 'alternateLBA', + Q => 'firstUsableLBA', + Q => 'lastUsableLBA', + a16 => 'guid', + Q => 'partitionEntriesLBA', + V => 'nbPartitions', + V => 'partitionEntrySize', + V => 'partitionEntriesCRC32', +); + +my ($partitionEntry_format, $partitionEntry_fields) = list2kv( + a16 => 'gpt_type', + a16 => 'guid', + Q => 'start', + Q => 'ending', + a8 => 'efi_attributes', + a72 => 'name', +); + +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 on disk $hd->{device}"; + $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; diff --git a/perl-install/partition_table/lvm_PV.pm b/perl-install/partition_table/lvm_PV.pm new file mode 100644 index 000000000..4a55d3810 --- /dev/null +++ b/perl-install/partition_table/lvm_PV.pm @@ -0,0 +1,43 @@ +package partition_table::lvm; # $Id$ + +use diagnostics; +use strict; + +our @ISA = qw(partition_table::raw); + +use partition_table::raw; +use c; + +my $magic = "HM\1\0"; +my $offset = 0; + + +#- Allows people having PVs on unpartitioned disks to install +#- (but no way to create such beasts) +#- +#- another way to handle them would be to ignore those disks, +#- but this would make those hds unshown in diskdrake, +#- disallowing to zero_MBR, clearing this PV + + +sub read { + my ($hd, $sector) = @_; + + 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, my $tmp, length $magic or die "error reading magic number on disk $hd->{file}"; + $tmp eq $magic or die "bad magic number on disk $hd->{file}"; + + [ ]; +} + +sub write { + die "ERROR: should not be writing raw disk lvm PV!!"; +} + +sub clear_raw { + die "ERROR: should not be creating new raw disk lvm PV!!"; +} + +1; diff --git a/perl-install/partition_table/mac.pm b/perl-install/partition_table/mac.pm new file mode 100644 index 000000000..89fb9ceaf --- /dev/null +++ b/perl-install/partition_table/mac.pm @@ -0,0 +1,398 @@ +package partition_table::mac; # $Id$ + +use diagnostics; +#use strict; - fixed other PPC code to comply, but program bails on empty partition table - sbenedict +use vars qw(@ISA $freepart_device $bootstrap_part $freepart_start $freepart_size $freepart_part $macos_part); + +@ISA = qw(partition_table::raw); + +use common; +use partition_table::raw; +use partition_table; +use c; + +my %typeToDos = ( + "Apple_partition_map" => 0x401, + "Apple_Bootstrap" => 0x401, + "Apple_Driver43" => 0x401, + "Apple_Driver_IOKit" => 0x401, + "Apple_Patches" => 0x401, + "Apple_HFS" => 0x402, + "Apple_UNIX_SVR2" => 0x83, + "Apple_UNIX_SVR2" => 0x183, + "Apple_UNIX_SVR2" => 0x283, + "Apple_UNIX_SVR2" => 0x383, + "Apple_UNIX_SVR2" => 0x483, + "Apple_Free" => 0x0, +); +my %typeFromDos = reverse %typeToDos; + + +my ($bz_format, $bz_fields) = list2kv( + n => 'bzSig', + n => 'bzBlkSize', + N => 'bzBlkCnt', + n => 'bzDevType', + n => 'bzDevID', + N => 'bzReserved', + n => 'bzDrvrCnt', +); +$bz_format = join '', @$bz_format; + + +my ($dd_format, $dd_fields) = list2kv( + N => 'ddBlock', + n => 'ddSize', + n => 'ddType', +); +$dd_format = join '', @$dd_format; + + +my ($p_format, $p_fields) = list2kv( + n => 'pSig', + n => 'pSigPad', + N => 'pMapEntry', + N => 'pPBlockStart', + N => 'pPBlocks', + + a32 => 'pName', + a32 => 'pType', + + N => 'pLBlockStart', + N => 'pLBlocks', + N => 'pFlags', + N => 'pBootBlock', + N => 'pBootBytes', + + N => 'pAddrs1', + N => 'pAddrs2', + N => 'pAddrs3', + N => 'pAddrs4', + N => 'pChecksum', + + a16 => 'pProcID', + a128 => 'pBootArgs', + a248 => 'pReserved', +); +$p_format = join '', @$p_format; + +my $magic = 0x4552; +my $pmagic = 0x504D; + +sub first_usable_sector { 1 } + +sub adjustStart($$) { + my ($hd, $part) = @_; + my $end = $part->{start} + $part->{size}; + my $partmap_end = $hd->{primary}{raw}[0]{size}; + + if ($part->{start} <= $partmap_end) { + $part->{start} = $partmap_end + 1; + $part->{size} = $end - $part->{start}; + } +} + +sub adjustEnd($$) { + my ($hd, $part) = @_; +} + +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, 0) or die "reading of partition in sector $sector failed"; + + sysread F, $tmp, psizeof($bz_format) or die "error while reading bz \(Block Zero\) in sector $sector"; + my %info; @info{@$bz_fields} = unpack $bz_format, $tmp; + my $i; + + foreach $i (0 .. $info{bzDrvrCnt}-1) { + sysread F, $tmp, psizeof($dd_format) or die "error while reading driver data in sector $sector"; + my %dd; @dd{@$dd_fields} = unpack $dd_format, $tmp; + push @{$info{ddMap}}, \%dd; + } + + #- check magic number + $info{bzSig} == $magic or die "bad magic number on disk $hd->{device}"; + + my $numparts; + c::lseek_sector(fileno(F), $sector, 516) or die "reading of partition in sector $sector failed"; + sysread F, $tmp, 4 or die "error while reading partition info in sector $sector"; + $numparts = unpack "N", $tmp; + + my $partmapsize; + c::lseek_sector(fileno(F), $sector, 524) or die "reading of partition in sector $sector failed"; + sysread F, $tmp, 4 or die "error while reading partition info in sector $sector"; + $partmapsize = ((unpack "N", $tmp) * $info{bzBlkSize}) / psizeof($p_format); + + c::lseek_sector(fileno(F), $sector, 512) or die "reading of partition in sector $sector failed"; + + my @pt; + for ($i=0;$i<$partmapsize;$i++) { + my $part; + sysread F, $part, psizeof($p_format) or die "error while reading partition info in sector $sector"; + + push @pt, map { + my %h; @h{@$p_fields} = unpack $p_format, $part; + if ($i < $numparts && $h{pSig} eq $pmagic) { + + $h{size} = ($h{pPBlocks} * $info{bzBlkSize}) / 512; + $h{start} = ($h{pPBlockStart} * $info{bzBlkSize}) / 512; + + if ($h{pType} =~ /^Apple_UNIX_SVR2/i) { + $h{pName} =~ /swap/i ? ($h{type} = 0x82) : ($h{type} = 0x83); + } elsif ($h{pType} =~ /^Apple_Free/i) { + #- need to locate a 1MB partition to setup a bootstrap on + if (defined $freepart_start && $freepart_size >= 1) { + #- already found a suitable partition + } else { + $freepart_start = $h{start}; + $freepart_size = $h{size}/2048; + $freepart_device = $hd; + $freepart_part = "/dev/" . $hd->{device} . ($i+1); + log::l("free apple partition found on drive /dev/$freepart_device->{device}, block $freepart_start, size $freepart_size"); + } + $h{type} = 0x0; + $h{pName} = 'Extra'; + } elsif ($h{pType} =~ /^Apple_HFS/i) { + $h{type} = 0x402; + if (defined $macos_part) { + #- swag at identifying MacOS - 1st HFS partition + } else { + $macos_part = "/dev/" . $hd->{device} . ($i+1); + log::l("found MacOS at partition $macos_part"); + } + } elsif ($h{pType} =~ /^Apple_Partition_Map/i) { + $h{type} = 0x401; + $h{isMap} = 1; + } elsif ($h{pType} =~ /^Apple_Bootstrap/i) { + $h{type} = 0x401; + $h{isBoot} = 1; + if (defined $bootstrap_part) { + #found a bootstrap already - use it, but log the find + log::l("found another apple bootstrap at partition /dev/$hd->{device}" . ($i+1)); + } else { + $bootstrap_part = "/dev/" . $hd->{device} . ($i+1); + log::l("found apple bootstrap at partition $bootstrap_part"); + } + } else { + $h{type} = 0x401; + $h{isDriver} = 1; + }; + + # Let's see if this partition is a driver. + foreach (@{$info{ddMap}}) { + $_->{ddBlock} == $h{pPBlockStart} and $h{isDriver} = 1; + } + + } + \%h; + } [ $part ]; + }; + + [ @pt ], \%info; +} + +sub write($$$;$) { + my ($hd, $sector, $pt, $info) = @_; + + #- handle testing for writing partition table on file only! + local *F; + if ($::testing) { + my $file = "/tmp/partition_table_$hd->{device}"; + open F, ">$file" or die "error opening test file $file"; + } else { + partition_table::raw::openit($hd, *F, 2) or die "error opening device $hd->{device} for writing"; + c::lseek_sector(fileno(F), $sector, 0) or return 0; + } + + # Find the partition map. + my @partstowrite; + my $part = $pt->[0]; + (defined $part->{isMap}) or die "the first partition is not the partition map"; + push @partstowrite, $part; + + # Now go thru the partitions, sort and fill gaps. + my $last; + while ($part) { + $last = $part; + $part = &partition_table::next($hd, $part); + $part or last; + + if ($last->{start} + $last->{size} < $part->{start}) { + #There is a gap between partitions. Fill it and move on. + push @partstowrite, { + type => 0x0, + start => $last->{start} + $last->{size}, + size => $part->{start} - ($last->{start} + $last->{size}), + }; + } + push @partstowrite, $part; + }; + + # now, fill a gap at the end if there is one. + if ($last->{start} + $last->{size} < $hd->{totalsectors}) { + push @partstowrite, { + type => 0x0, + start => $last->{start} + $last->{size}, + size => $hd->{totalsectors} - ($last->{start} + $last->{size}), + }; + } + + # Since we didn't create any new drivers, let's try and match up our driver records with out partitons and see if any are missing. + $info->{bzDrvrCnt} = 0; + my @ddstowrite; + my $dd; + foreach $dd (@{$info->{ddMap}}) { + foreach (@partstowrite) { + if ($dd->{ddBlock} == $_->{pPBlockStart}) { + push @ddstowrite, $dd; + $info->{bzDrvrCnt}++; + last; + } + } + } + + # Now let's write our first block. + syswrite F, pack($bz_format, @$info{@$bz_fields}), psizeof($bz_format) or return 0; + + # ...and now the driver information. + foreach (@ddstowrite) { + syswrite F, pack($dd_format, @$_{@$dd_fields}), psizeof($dd_format) or return 0; + } + # zero the rest of the data in the first block. + foreach ( 1 .. (494 - ((@ddstowrite) * 8))) { + syswrite F, "\0", 1 or return 0; + } + #c::lseek_sector(fileno(F), $sector, 512) or return 0; + # Now, we iterate thru the partstowrite and write them. + foreach (@partstowrite) { + if (!defined $_->{pSig}) { + # The values we need to write to disk are not defined. Let's make them up. + $_->{pSig} = $pmagic; + $_->{pSigPad} = 0; + $_->{pPBlockStart} = ($_->{start} * 512) / $info->{bzBlkSize}; + $_->{pPBlocks} = ($_->{size} * 512) / $info->{bzBlkSize}; + $_->{pLBlockStart} = 0; + $_->{pLBlocks} = $_->{pPBlocks}; + $_->{pBootBlock} = 0; + $_->{pBootBytes} = 0; + $_->{pAddrs1} = 0; + $_->{pAddrs2} = 0; + $_->{pAddrs3} = 0; + $_->{pAddrs4} = 0; + $_->{pChecksum} = 0; + $_->{pProcID} = "\0"; + $_->{pBootArgs} = "\0"; + $_->{pReserved} = "\0"; + + if ($_->{type} == 0x402) { + $_->{pType} = "Apple_HFS"; + $_->{pName} = "MacOS"; + $_->{pFlags} = 0x4000037F; + } elsif ($_->{type} == 0x401 && $_->{start} == 1) { + $_->{pType} = "Apple_Partition_Map"; + $_->{pName} = "Apple"; + $_->{pFlags} = 0x33; + } elsif ($_->{type} == 0x401) { + $_->{pType} = "Apple_Bootstrap"; + $_->{pName} = "bootstrap"; + $_->{pFlags} = 0x33; + $_->{isBoot} = 1; + log::l("writing a bootstrap at /dev/$_->{device}"); + $install_steps_interactive::new_bootstrap = 1 if !(defined $partition_table::mac::bootstrap_part); + $bootstrap_part = "/dev/" . $_->{device}; + } elsif ($_->{type} == 0x82) { + $_->{pType} = "Apple_UNIX_SVR2"; + $_->{pName} = "swap"; + $_->{pFlags} = 0x33; + } elsif ($_->{type} == 0x83) { + $_->{pType} = "Apple_UNIX_SVR2"; + $_->{pName} = "Linux Native"; + $_->{pFlags} = 0x33; + } elsif ($_->{type} == 0x183) { + $_->{pType} = "Apple_UNIX_SVR2"; + $_->{pName} = "Linux ReiserFS"; + $_->{pFlags} = 0x33; + } elsif ($_->{type} == 0x283) { + $_->{pType} = "Apple_UNIX_SVR2"; + $_->{pName} = "Linux XFS"; + $_->{pFlags} = 0x33; + } elsif ($_->{type} == 0x383) { + $_->{pType} = "Apple_UNIX_SVR2"; + $_->{pName} = "Linux JFS"; + $_->{pFlags} = 0x33; + } elsif ($_->{type} == 0x483) { + $_->{pType} = "Apple_UNIX_SVR2"; + $_->{pName} = "Linux ext3"; + $_->{pFlags} = 0x33; + } elsif ($_->{type} == 0x0) { + $_->{pType} = "Apple_Free"; + $_->{pName} = "Extra"; + $_->{pFlags} = 0x31; + } + }; + $_->{pMapEntry} = @partstowrite; + syswrite F, pack($p_format, @$_{@$p_fields}), psizeof($p_format) or return 0; + } + + common::sync(); + + 1; +} + +sub info { + my ($hd) = @_; + + # - Build the first block of the drive. + + my $info = { + bzSig => $magic, + bzBlkSize => 512, + bzBlkCnt => $hd->{totalsectors}, + bzDevType => 0, + bzDevID => 0, + bzReserved => 0, + bzDrvrCnt => 0, + }; + + $info; +} + +sub clear_raw { + my ($hd) = @_; + my @oldraw = @{$hd->{primary}{raw}}; + my $pt = { raw => [ ({}) x 63 ], info => info($hd) }; + + #- handle special case for partition 1 which is the partition map. + $pt->{raw}[0] = { + type => 0x401, + start => 1, + size => 63, + isMap => 1, + }; +# $pt->{raw}[1] = { +# type => 0x0, +# start => 64, +# size => $hd->{totalsectors} - 64, +# isMap => 0, +# }; + push @{$pt->{normal}}, $pt->{raw}[0]; +# push @{$pt->{normal}}, $pt->{raw}[1]; + + #- Recover any Apple Drivers, if any. + my $i = 1; + foreach (@oldraw) { + if (defined $_->{isDriver}) { + $pt->{raw}[$i] = $_; + push @{$pt->{normal}}, $pt->{raw}[$i]; + $i++; + } + }; + @{$pt->{info}{ddMap}} = @{$hd->{primary}{info}{ddMap}}; + + $pt; +} + +1; diff --git a/perl-install/partition_table/raw.pm b/perl-install/partition_table/raw.pm new file mode 100644 index 000000000..3be68fe56 --- /dev/null +++ b/perl-install/partition_table/raw.pm @@ -0,0 +1,192 @@ +package partition_table::raw; # $Id$ + +use diagnostics; +use strict; + +use common; +use devices; +use detect_devices; +use log; +use c; + +my @MBR_signatures = ( +if_(arch() =~ /ppc/, + map { [ 'yaboot', 0, "PM", 0x200 * $_ + 0x10, "bootstrap\0" ] } 0 .. 61 +), + [ 'empty', 0, "\0\0\0\0" ], + [ 'grub', 0, "\xEBG", 0x17d, "stage1 \0" ], + [ 'grub', 0, "\xEBH", 0x17e, "stage1 \0" ], + [ 'grub', 0, "\xEBH", 0x18a, "stage1 \0" ], + [ 'grub', 0, "\xEBH", 0x181, "GRUB \0" ], + [ 'lilo', 0x2, "LILO" ], + [ 'lilo', 0x6, "LILO" ], + [ 'grub', 0x6, "GRUB" ], + [ 'osbs', 0x2, "OSBS" ], #- http://www.prz.tu-berlin.de/~wolf/os-bs.html + [ 'pqmagic', 0xef, "PQV" ], + [ 'BootStar', 0x130, "BootStar:" ], + [ 'DocsBoot', 0x148, 'DocsBoot' ], + [ 'system_commander', 0x1ad, "SYSCMNDRSYS" ], + [ 'Be Os', 0x24, 'Boot Manager' ], + [ 'TimO', 0, 'IBM Thinkpad hibernation partition' ], + [ 'dos', 0xa0, "\x25\x03\x4E\x02\xCD\x13" ], + [ 'dos', 0xa0, "\x00\xB4\x08\xCD\x13\x72" ], #- nt2k's + [ 'dos', 0x60, "\xBB\x00\x7C\xB8\x01\x02\x57\xCD\x13\x5F\x73\x0C\x33\xC0\xCD\x13" ], #- nt's + [ 'dos', 0x70, "\x0C\x33\xC0\xCD\x13\x4F\x75\xED\xBE\xA3" ], + [ 'freebsd', 0xC0, "\x00\x30\xE4\xCD\x16\xCD\x19\xBB\x07\x00\xB4" ], + [ 'freebsd', 0x160, "\x6A\x10\x89\xE6\x48\x80\xCC\x40\xCD\x13" ], + [ 'dummy', 0xAC, "\x0E\xB3\x07\x56\xCD\x10\x5E\xEB" ], #- caldera? + [ 'ranish', 0x100, "\x6A\x10\xB4\x42\x8B\xF4\xCD\x13\x8B\xE5\x73" ], + [ 'os2', 0x1c2, "\xA" ], +); + +sub typeOfMBR($) { typeFromMagic(devices::make($_[0]), @MBR_signatures) } +sub typeOfMBR_($) { typeFromMagic($_[0], @MBR_signatures) } + +sub hasExtended { 0 } + +sub cylinder_size($) { + my ($hd) = @_; + $hd->{geom}{sectors} * $hd->{geom}{heads}; +} +sub first_usable_sector { 1 } +sub last_usable_sector { + my ($hd) = @_; + $hd->{totalsectors}; +} + +#- default method for starting a partition, only head size or twice +#- is allowed for starting a partition after a cylinder boundarie. +sub adjustStart($$) { + my ($hd, $part) = @_; + my $end = $part->{start} + $part->{size}; + + $part->{start} = round_up($part->{start}, + $part->{start} % cylinder_size($hd) < 2 * $hd->{geom}{sectors} ? + $hd->{geom}{sectors} : cylinder_size($hd)); + $part->{size} = $end - $part->{start}; + $part->{size} > 0 or die "adjustStart get a too small partition to handle correctly"; +} +#- adjusting end to match a cylinder boundary, two methods are used and must +#- match at the end, else something is wrong and nothing will be done on +#- partition table. +#- $end2 is computed by removing 2 (or only 1 if only 2 heads on drive) groups +#- of sectors, this is necessary to handle extended partition where logical +#- partition start after 1 (or 2 accepted) groups of sectors (typically 63). +#- $end is floating (is not on cylinder boudary) so we have to choice a good +#- candidate, $end1 or $end2 should always be good except $end1 for small +#- partition size. +sub adjustEnd($$) { + my ($hd, $part) = @_; + my $end = $part->{start} + $part->{size}; + $end > $hd->{geom}{cylinders} * cylinder_size($hd) && $end <= $hd->{totalsectors} and return; + my $end1 = round_down($end, cylinder_size($hd)); + my $end2 = round_up($end - ($hd->{geom}{heads} > 2 ? 2 : 1) * $hd->{geom}{sectors}, cylinder_size($hd)); + $end2 <= $hd->{geom}{cylinders} * cylinder_size($hd) or die "adjustEnd go beyond end of device geometry ($end2 > $hd->{totalsectors})"; + $part->{size} = ($end1 - $part->{start} > cylinder_size($hd) ? $end1 : $end2) - $part->{start}; + $part->{size} > 0 or die "adjustEnd get a too small partition to handle correctly"; +} + +sub get_geometry($) { + my ($dev) = @_; + my $g = ""; + + local *F; sysopen F, $dev, 0 or return; + ioctl(F, c::HDIO_GETGEO(), $g) or return; + my %geom; @geom{qw(heads sectors cylinders start)} = unpack "CCSL", $g; + $geom{totalcylinders} = $geom{cylinders}; + + my $total; + #- $geom{cylinders} is no good (only a ushort, that means less than 2^16 => at best 512MB) + if ($total = c::total_sectors(fileno F)) { + $geom{cylinders} = int $total / $geom{heads} / $geom{sectors}; + } else { + $total = $geom{heads} * $geom{sectors} * $geom{cylinders} + } + + { geom => \%geom, totalsectors => $total }; +} + +sub openit($$;$) { sysopen $_[1], $_[0]{file}, $_[2] || 0; } + +# cause kernel to re-read partition table +sub kernel_read($) { + my ($hd) = @_; + common::sync(); + local *F; openit($hd, *F) or return 0; + common::sync(); sleep(1); + $hd->{rebootNeeded} = !ioctl(F, c::BLKRRPART(), 0); + common::sync(); + close F; + common::sync(); sleep(1); +} + +sub raw_removed { + my ($hd, $raw) = @_; +} +sub can_raw_add { + my ($hd) = @_; + $_->{size} || $_->{type} or return 1 foreach @{$hd->{primary}{raw}}; + 0; +} +sub raw_add { + my ($hd, $raw, $part) = @_; + + foreach (@$raw) { + $_->{size} || $_->{type} and next; + $_ = $part; + return; + } + die "raw_add: partition table already full"; +} + +sub zero_MBR { + my ($hd) = @_; + #- force the standard partition type for the architecture + my $type = arch() =~ /ia64/ ? 'gpt' : arch() eq "alpha" ? "bsd" : arch() =~ /^sparc/ ? "sun" : arch() eq "ppc" ? "mac" : "dos"; + #- override standard mac type on PPC for IBM machines to dos + $type = "dos" if (arch() =~ /ppc/ && detect_devices::get_mac_model() =~ /^IBM/); + require("partition_table/$type.pm"); + bless $hd, "partition_table::$type"; + $hd->{primary} = $hd->clear_raw(); + delete $hd->{extended}; +} + +sub zero_MBR_and_dirty { + my ($hd) = @_; + zero_MBR($hd); + $hd->{isDirty} = $hd->{needKernelReread} = 1; + +} + +#- ugly stuff needed mainly for Western Digital IDE drives +#- try writing what we've just read, yells if it fails +#- testing on last sector of head #0 (unused in 99% cases) +#- +#- return false if the device can't be written to (especially for Smartmedia) +sub test_for_bad_drives { + my ($hd) = @_; + + log::l("test_for_bad_drives($hd->{file})"); + my $sector = $hd->{geom}{sectors} - 1; + + + local *F; openit($hd, *F, 2) or die "can't open device"; + + my $seek = sub { + c::lseek_sector(fileno(F), $sector, 0) or die "seeking to sector $sector failed"; + }; + my $tmp; + + &$seek; sysread F, $tmp, $SECTORSIZE or die "test_for_bad_drives: can't even read ($!)"; + &$seek; syswrite F, $tmp or die "test_for_bad_drives: can't even write ($!)"; + + my $tmp2; + &$seek; sysread F, $tmp2, $SECTORSIZE or die "test_for_bad_drives: can't even read again ($!)"; + $tmp eq $tmp2 or die +_("Something bad is happening on your drive. +A test to check the integrity of data has failed. +It means writing anything on the disk will end up with random trash"); + 1; +} + +1; diff --git a/perl-install/partition_table/sun.pm b/perl-install/partition_table/sun.pm new file mode 100644 index 000000000..6944130dc --- /dev/null +++ b/perl-install/partition_table/sun.pm @@ -0,0 +1,201 @@ +package partition_table::sun; # $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; + +my ($main_format, $main_fields) = list2kv( + a128 => 'info', + a14 => 'spare0', + a32 => 'infos', + a246 => 'spare1', + n => 'rspeed', + n => 'pcylcount', + n => 'sparecyl', + a4 => 'spare2', + n => 'ilfact', + n => 'ncyl', + n => 'nacyl', + n => 'ntrks', + n => 'nsect', + a4 => 'spare3', + a64 => 'partitions', + n => 'magic', + n => 'csum', +); +$main_format = join '', @$main_format; + +my ($fields1, $fields2) = ([ qw(type flags) ], [ qw(start_cylinder size) ]); +my ($format1, $format2) = ("xCxC", "N2"); +my ($size1, $size2) = map { psizeof($_) } ($format1, $format2); +my $magic = 0xDABE; +my $nb_primary = 8; +my $offset = 0; + +sub adjustStart($$) { + my ($hd, $part) = @_; + my $end = $part->{start} + $part->{size}; + + #- since partition must always start on cylinders boundaries on sparc, + #- note that if start sector is on the first cylinder, it is adjusted + #- to 0 and it is valid, cylinder 0 bug is from bad define for sparc + #- compilation of mke2fs combined with a blind kernel... + $part->{start} = round_down($part->{start}, $hd->cylinder_size()); + $part->{size} = $end - $part->{start}; + $part->{size} = $hd->cylinder_size() if $part->{size} <= 0; +} +sub adjustEnd($$) { + my ($hd, $part) = @_; + my $end = $part->{start} + $part->{size}; + my $end2 = round_up($end, $hd->cylinder_size()); + $end2 = $hd->{geom}{cylinders} * $hd->cylinder_size() if $end2 > $hd->{geom}{cylinders} * $hd->cylinder_size(); + $part->{size} = $end2 - $part->{start}; +} + +#- compute crc checksum used for Sun Label partition, expect +#- $tmp to be the 512 bytes buffer to be read/written to MBR. +sub compute_crc($) { + my ($tmp) = @_; + my @l2b = unpack "n256", $tmp; + my $crc = 0; + + map { $crc ^= $_ } @l2b; + + $crc; +} + +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; + + #- check magic number + $info{magic} == $magic or die "bad magic number on disk $hd->{device}"; + + #- check crc, csum contains the crc so result should be 0. + compute_crc($tmp) == 0 or die "bad checksum"; + + @{$hd->{geom}}{qw(cylinders heads sectors)} = @info{qw(ncyl ntrks nsect)}; + + my @pt; + my @infos_up = unpack $format1 x $nb_primary, $info{infos}; + my @partitions_up = unpack $format2 x $nb_primary, $info{partitions}; + for (0..$nb_primary-1) { + my $h = { type => $infos_up[2 * $_], flag => $infos_up[1 + 2 * $_], + start_cylinder => $partitions_up[2 * $_], size => $partitions_up[1 + 2 * $_] }; + $h->{start} = $sector + $h->{start_cylinder} * $hd->cylinder_size(); + $h->{type} && $h->{size} or $h->{$_} = 0 foreach keys %$h; + push @pt, $h; + } + +#- this code is completely broken by null char inside strings, it gets completely crazy :-) +# my @pt = mapn { +# my %h; +# @h{@$fields1} = unpack $format1, $_[0]; +# @h{@$fields2} = unpack $format2, $_[1]; +# $h{start} = $sector + $h{start_cylinder} * $hd->cylinder_size(); +# $h{type} && $h{size} or $h{$_} = 0 foreach keys %h; +# \%h; +# } [ grep { $_ } split /(.{$size1})/o, $info{infos} ], [ grep { $_ } split /(.{$size2})/o, $info{partitions} ]; + + [ @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) = @_; +# my ($csize, $wdsize) = (0, 0); + + #- handle testing for writing partition table on file only! + local *F; + if ($::testing) { + my $file = "/tmp/partition_table_$hd->{device}"; + open F, ">$file" or die "error opening test file $file"; + } else { + partition_table::raw::openit($hd, *F, 2) or die "error opening device $hd->{device} for writing"; + c::lseek_sector(fileno(F), $sector, $offset) or return 0; + } + + ($info->{infos}, $info->{partitions}) = map { join '', @$_ } list2kv map { + $_->{start} % $hd->cylinder_size() == 0 or die "partition not at beginning of cylinder"; +# $csize += $_->{size} if $_->{type} != 5; +# $wdsize += $_->{size} if $_->{type} == 5; + $_->{flags} |= 0x10 if $_->{mntpoint} eq '/'; + $_->{flags} |= 0x01 if partition_table::isSwap($_); + local $_->{start_cylinder} = $_->{start} / $hd->cylinder_size() - $sector; + pack($format1, @$_{@$fields1}), pack($format2, @$_{@$fields2}); + } @$pt; +# $csize == $wdsize or die "partitions are not using whole disk space"; + + #- compute the checksum by building the buffer to write and call compute_crc. + #- set csum to 0 so compute_crc will give the right csum value. + $info->{csum} = 0; + $info->{csum} = compute_crc(pack($main_format, @$info{@$main_fields})); + + syswrite F, pack($main_format, @$info{@$main_fields}), psizeof($main_format) or return 0; + + common::sync(); + + 1; +} + +sub info { + my ($hd) = @_; + + #- take care of reduction of the number of cylinders, avoid loop of reduction! + unless ($hd->{geom}{totalcylinders} > $hd->{geom}{cylinders}) { + $hd->{geom}{totalcylinders} = $hd->{geom}{cylinders}; + $hd->{geom}{cylinders} -= 2; + + #- rebuild some constants according to number of cylinders. + $hd->{totalsectors} = $hd->{geom}{heads} * $hd->{geom}{sectors} * $hd->{geom}{cylinders}; + } + + #- build a default suitable partition table, + #- checksum will be built when writing on disk. + #- note third partition is ALWAYS of type Whole disk. + my $info = { + info => "DiskDrake partition table", + rspeed => 5400, + pcylcount => $hd->{geom}{totalcylinders}, + sparecyl => 0, + ilfact => 1, + ncyl => $hd->{geom}{cylinders}, + nacyl => $hd->{geom}{totalcylinders} - $hd->{geom}{cylinders}, + ntrks => $hd->{geom}{heads}, + nsect => $hd->{geom}{sectors}, + magic => $magic, + }; + + $info; +} + +sub clear_raw { + my ($hd) = @_; + my $pt = { raw => [ ({}) x $nb_primary ], info => info($hd) }; + + #- handle special case for partition 2 which is whole disk. + $pt->{raw}[2] = { + type => 5, #- the whole disk type. + flags => 0, + start_cylinder => 0, + size => $hd->{geom}{cylinders} * $hd->cylinder_size(), + }; + + $pt; +} + +1; |