summaryrefslogtreecommitdiffstats
path: root/perl-install/partition_table
diff options
context:
space:
mode:
authorAntoine Ginies <aginies@mandriva.com>2011-01-19 10:44:49 +0000
committerAntoine Ginies <aginies@mandriva.com>2011-01-19 10:44:49 +0000
commit530a16ec071db0e24e6e949e265a96848864967c (patch)
treefe40cacd28d67b98186754c551b7fd339ebc7e17 /perl-install/partition_table
downloaddrakx-backup-do-not-use-530a16ec071db0e24e6e949e265a96848864967c.tar
drakx-backup-do-not-use-530a16ec071db0e24e6e949e265a96848864967c.tar.gz
drakx-backup-do-not-use-530a16ec071db0e24e6e949e265a96848864967c.tar.bz2
drakx-backup-do-not-use-530a16ec071db0e24e6e949e265a96848864967c.tar.xz
drakx-backup-do-not-use-530a16ec071db0e24e6e949e265a96848864967c.zip
add mes5-2.6.33 branch
Diffstat (limited to 'perl-install/partition_table')
-rw-r--r--perl-install/partition_table/bsd.pm152
-rw-r--r--perl-install/partition_table/dmcrypt.pm41
-rw-r--r--perl-install/partition_table/dos.pm262
-rw-r--r--perl-install/partition_table/empty.pm36
-rw-r--r--perl-install/partition_table/gpt.pm268
-rw-r--r--perl-install/partition_table/lvm.pm38
-rw-r--r--perl-install/partition_table/mac.pm396
-rw-r--r--perl-install/partition_table/raw.pm290
-rw-r--r--perl-install/partition_table/readonly.pm20
-rw-r--r--perl-install/partition_table/sun.pm205
10 files changed, 1708 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..f69cae97c
--- /dev/null
+++ b/perl-install/partition_table/bsd.pm
@@ -0,0 +1,152 @@
+package partition_table::bsd; # $Id: bsd.pm 228416 2007-09-18 10:32:56Z pixel $
+
+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 %pt_typeToDos = (
+ 8 => 0x83,
+ 1 => 0x82,
+);
+my %pt_typeFromDos = reverse %pt_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 pt_type frag cpg);
+my $format = "I I I C C S";
+my $magic = 0x82564557;
+my $nb_primary = 8;
+my $offset = 0x40;
+
+
+sub use_pt_type { 1 }
+
+sub read_one {
+ my ($hd, $sector) = @_;
+ my $tmp;
+
+ my $F = partition_table::raw::openit($hd) 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, $_;
+ fs::type::set_pt_type(\%h, $pt_typeToDos{$h{pt_type}} || $h{pt_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, pt_type, active
+sub write($$$;$) {
+ my ($hd, $sector, $pt, $info) = @_;
+
+ #- handle testing for writing partition table on file only!
+ my $F;
+ if ($::testing) {
+ my $file = "/tmp/partition_table_$hd->{device}";
+ open $F, ">$file" or die "error opening test file $file";
+ } else {
+ $F = partition_table::raw::openit($hd, 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 $_->{pt_type} = $pt_typeFromDos{$_->{pt_type}} || $_->{pt_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 initialize {
+ my ($class, $hd) = @_;
+ $hd->{primary} = { raw => [ ({}) x $nb_primary ], info => info($hd) };
+ bless $hd, $class;
+;
+}
+
+sub first_usable_sector { 2048 }
+
+1;
diff --git a/perl-install/partition_table/dmcrypt.pm b/perl-install/partition_table/dmcrypt.pm
new file mode 100644
index 000000000..3eae783c6
--- /dev/null
+++ b/perl-install/partition_table/dmcrypt.pm
@@ -0,0 +1,41 @@
+package partition_table::dmcrypt; # $Id: $
+
+# dmcrypt on full disk
+
+use diagnostics;
+use strict;
+
+our @ISA = qw(partition_table::readonly);
+
+use common;
+use partition_table::readonly;
+use fs::type;
+
+sub _parts {
+ my ($hd) = @_;
+
+ my $part = { size => $hd->{totalsectors}, device => $hd->{device} };
+ add2hash($part, fs::type::type_name2subpart('Encrypted'));
+
+ require fs;
+ fs::get_major_minor([$part]); # to allow is_same_hd() in fs::dmcrypt
+
+ [ $part ];
+}
+
+sub read_primary {
+ my ($hd) = @_;
+
+ my $type = fs::type::type_subpart_from_magic($hd);
+
+ $type && $type->{type_name} eq 'Encrypted' or return;
+
+ partition_table::dmcrypt->initialize($hd);
+ 1;
+}
+
+sub initialize {
+ my ($class, $hd) = @_;
+
+ partition_table::readonly::initialize($class, $hd, _parts($hd));
+}
diff --git a/perl-install/partition_table/dos.pm b/perl-install/partition_table/dos.pm
new file mode 100644
index 000000000..00b3c531e
--- /dev/null
+++ b/perl-install/partition_table/dos.pm
@@ -0,0 +1,262 @@
+package partition_table::dos; # $Id: dos.pm 228577 2007-09-19 15:34:17Z pixel $
+
+use diagnostics;
+use strict;
+use vars qw(@ISA);
+
+@ISA = qw(partition_table::raw);
+
+use common;
+use partition_table::raw;
+use partition_table;
+use fs::type;
+use c;
+
+my @fields = qw(active start_head start_sec start_cyl pt_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 use_pt_type { 1 }
+sub hasExtended { 1 }
+
+sub geometry_to_string {
+ my ($geom) = @_;
+ "$geom->{cylinders}/$geom->{heads}/$geom->{sectors}";
+}
+
+sub last_usable_sector {
+ my ($hd) = @_;
+ #- do not use totalsectors, see gi/docs/Partition-ends-after-end-of-disk.txt for more
+ $hd->{geom}{sectors} * $hd->{geom}{heads} * $hd->{geom}{cylinders};
+}
+
+my $two_TB = 2 * 1024 * 1024 * 2048;
+sub max_partition_start { $two_TB - 1 }
+sub max_partition_size { $two_TB - 1 }
+
+sub get_rawCHS {
+ my ($part) = @_;
+
+ exists $part->{start_cyl} or internal_error("get_rawCHS $part->{device}");
+
+ [ $part->{start_cyl}, $part->{start_head}, $part->{start_sec} ],
+ [ $part->{end_cyl}, $part->{end_head}, $part->{end_sec} ];
+}
+sub set_rawCHS {
+ my ($part, $raw_chs_start, $raw_chs_end) = @_;
+ ($part->{start_cyl}, $part->{start_head}, $part->{start_sec}) = @$raw_chs_start;
+ ($part->{end_cyl}, $part->{end_head}, $part->{end_sec}) = @$raw_chs_end;
+}
+
+sub compute_CHS {
+ my ($hd, $part) = @_;
+ my ($chs_start, $chs_end) = CHS_from_part_linear($hd->{geom}, $part);
+ set_rawCHS($part, $chs_start ? CHS2rawCHS($hd->{geom}, $chs_start) : [0,0,0],
+ $chs_end ? CHS2rawCHS($hd->{geom}, $chs_end) : [0,0,0]);
+}
+
+sub CHS_from_part_rawCHS {
+ my ($part) = @_;
+
+ $part->{start} || $part->{pt_type} or return;
+
+ my ($raw_chs_start, $raw_chs_end) = get_rawCHS($part);
+ rawCHS2CHS($raw_chs_start), rawCHS2CHS($raw_chs_end);
+}
+
+sub CHS_from_part_linear {
+ my ($geom, $part) = @_;
+
+ $part->{start} || $part->{pt_type} or return;
+
+ sector2CHS($geom, $part->{start}), sector2CHS($geom, $part->{start} + $part->{size} - 1);
+}
+
+sub rawCHS2CHS {
+ my ($chs) = @_;
+ my ($c, $h, $s) = @$chs;
+ [ $c | (($s & 0xc0) << 2), $h, ($s & 0x3f) - 1 ];
+}
+
+sub CHS2rawCHS {
+ my ($geom, $chs) = @_;
+ my ($c, $h, $s) = @$chs;
+ if ($c > 1023) {
+ #- no way to have a #cylinder >= 1024
+ $c = 1023;
+ $h = $geom->{heads} - 1;
+ $s = $geom->{sectors} - 1;
+ }
+ [ $c & 0xff, $h, ($s + 1) | (($c >> 2) & 0xc0) ];
+}
+
+# returns (cylinder, head, sector)
+sub sector2CHS {
+ my ($geom, $start) = @_;
+ my ($s, $h);
+ ($start, $s) = divide($start, $geom->{sectors});
+ ($start, $h) = divide($start, $geom->{heads});
+ [ $start, $h, $s ];
+}
+
+sub is_geometry_valid_for_the_partition_table {
+ my ($hd, $geom, $no_log) = @_;
+
+ every {
+ my ($chs_start_v1, $chs_end_v1) = map { join(',', @$_) } CHS_from_part_rawCHS($_) or next;
+ my ($chs_start_v2, $chs_end_v2) = map { join(',', @$_) } map { [ min($_->[0], 1023), $_->[1], $_->[2] ] } CHS_from_part_linear($geom, $_);
+ if (!$no_log) {
+ $chs_start_v1 eq $chs_start_v2 or log::l("is_geometry_valid_for_the_partition_table failed for ($_->{device}, $_->{start}): $chs_start_v1 vs $chs_start_v2 with geometry " . geometry_to_string($geom));
+ $chs_end_v1 eq $chs_end_v2 or log::l("is_geometry_valid_for_the_partition_table failed for ($_->{device}, " . ($_->{start} + $_->{size} - 1) . "): $chs_end_v1 vs $chs_end_v2 with geometry " . geometry_to_string($geom));
+ }
+ $chs_start_v1 eq $chs_start_v2 && $chs_end_v1 eq $chs_end_v2;
+ } @{$hd->{primary}{normal} || []};
+}
+
+#- from parted, thanks!
+my @valid_nb_sectors = (63, 61, 48, 32, 16);
+my @valid_nb_heads = (255, 240, 192, 128, 96, 64, 61, 32, 17, 16);
+
+sub guess_geometry_from_partition_table {
+ my ($hd) = @_;
+
+ my @chss = map { CHS_from_part_rawCHS($_) } @{$hd->{primary}{normal} || []} or return { empty => 1 };
+ my ($nb_heads, $nb_sectors) = (max(map { $_->[1] } @chss) + 1, max(map { $_->[2] } @chss) + 1);
+ my $geom = { sectors => $nb_sectors, heads => $nb_heads };
+ partition_table::raw::compute_nb_cylinders($geom, $hd->{totalsectors});
+ log::l("guess_geometry_from_partition_table $hd->{device}: " . geometry_to_string($geom));
+
+ member($geom->{sectors}, @valid_nb_sectors) or return { invalid => 1 };
+ $geom;
+}
+
+sub geometry_from_edd {
+ my ($hd, $edd_dir) = @_;
+
+ my $sectors = cat_("$edd_dir/legacy_sectors_per_track") or return;
+ my $heads = cat_("$edd_dir/legacy_max_head") or return;
+
+ my $geom = { sectors => 0 + $sectors, heads => 1 + $heads, from_edd => 1 };
+ is_geometry_valid_for_the_partition_table($hd, $geom, 0) or return;
+ partition_table::raw::compute_nb_cylinders($geom, $hd->{totalsectors});
+
+ log::l("geometry_from_edd $hd->{device} $hd->{volume_id}: " . geometry_to_string($geom));
+
+ $geom;
+}
+
+
+sub try_every_geometry {
+ my ($hd) = @_;
+
+ my $geom = {};
+ foreach (@valid_nb_sectors) {
+ $geom->{sectors} = $_;
+ foreach (@valid_nb_heads) {
+ $geom->{heads} = $_;
+ if (is_geometry_valid_for_the_partition_table($hd, $geom, 1)) {
+ partition_table::raw::compute_nb_cylinders($geom, $hd->{totalsectors});
+ log::l("try_every_geometry $hd->{device}: found " . geometry_to_string($geom));
+ return $geom;
+ }
+ }
+ }
+ log::l("$hd->{device}: argh! no geometry exists for this partition table");
+ undef;
+}
+
+sub set_best_geometry_for_the_partition_table {
+ my ($hd) = @_;
+
+ my $guessed_geom = guess_geometry_from_partition_table($hd);
+ if ($guessed_geom->{empty}) {
+ log::l("$hd->{device}: would need looking at BIOS info to find out geometry") if !$hd->{geom}{from_edd};
+ return;
+ }
+ my $default_ok = is_geometry_valid_for_the_partition_table($hd, $hd->{geom}, 0);
+ if ($guessed_geom->{invalid}) {
+ log::l("$hd->{device}: no valid geometry guessed from partition table");
+ $default_ok and return;
+ $guessed_geom = try_every_geometry($hd) or return;
+ }
+
+ if ($guessed_geom->{heads} == $hd->{geom}{heads} && $guessed_geom->{sectors} == $hd->{geom}{sectors}) {
+ # cool!
+ } else {
+ my $guessed_ok = is_geometry_valid_for_the_partition_table($hd, $guessed_geom, 0);
+ if ($default_ok && $guessed_ok) {
+ #- oh my!?
+ log::l("$hd->{device}: both guessed and default are valid??? " . geometry_to_string($hd->{geom}) . " vs " . geometry_to_string($guessed_geom));
+ } elsif ($default_ok) {
+ log::l("$hd->{device}: keeping default geometry " . geometry_to_string($hd->{geom}));
+ } elsif ($guessed_ok) {
+ log::l("$hd->{device}: using guessed geometry " . geometry_to_string($guessed_geom) . " instead of " . geometry_to_string($hd->{geom}));
+ put_in_hash($hd->{geom}, $guessed_geom);
+ } else {
+ log::l("$hd->{device}: argh! no valid geometry found");
+ }
+ }
+}
+
+sub read_one {
+ my ($hd, $sector) = @_;
+ my $tmp;
+
+ my $F = partition_table::raw::openit($hd) 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;
+ fs::type::set_pt_type(\%h, $h{pt_type});
+ \%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, pt_type, active
+sub write {
+ my ($hd, $sector, $pt) = @_;
+
+ log::l("partition::dos::write $hd->{device}");
+
+ #- handle testing for writing partition table on file only!
+ my $F;
+ if ($::testing) {
+ my $file = "/tmp/partition_table_$hd->{device}";
+ open $F, ">$file" or die "error opening test file $file";
+ } else {
+ $F = partition_table::raw::openit($hd, 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; $_->{pt_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 empty_raw { { raw => [ ({}) x $nb_primary ] } }
+
+sub initialize {
+ my ($class, $hd) = @_;
+ $hd->{primary} = empty_raw();
+ bless $hd, $class;
+}
+
+1;
diff --git a/perl-install/partition_table/empty.pm b/perl-install/partition_table/empty.pm
new file mode 100644
index 000000000..5d45143f8
--- /dev/null
+++ b/perl-install/partition_table/empty.pm
@@ -0,0 +1,36 @@
+package partition_table::empty; # $Id: empty.pm 228433 2007-09-18 12:30:17Z pixel $
+
+#- this is a mainly dummy partition table. If we find it's empty, we just call -
+#- ->clear 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_one {
+ my ($hd, $sector) = @_;
+ my $tmp;
+
+ my $F = partition_table::raw::openit($hd) 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, 1024 or die "error reading magic number on disk $hd->{device}";
+ $tmp eq substr($tmp, 0, 1) x 1024 or die "bad magic number on disk $hd->{device}";
+
+ partition_table::initialize($hd);
+
+ $hd->{primary}{raw}, $hd->{primary}{info};
+}
+
+1;
diff --git a/perl-install/partition_table/gpt.pm b/perl-install/partition_table/gpt.pm
new file mode 100644
index 000000000..e50624410
--- /dev/null
+++ b/perl-install/partition_table/gpt.pm
@@ -0,0 +1,268 @@
+package partition_table::gpt; # $Id: gpt.pm 240647 2008-03-25 17:19:28Z pixel $
+
+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 fs::type;
+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;
+ open(my $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 not 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 not 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;
+ my $pt_type = $gpt_types_rev{$h{gpt_type}};
+ fs::type::set_pt_type(\%h, defined $pt_type ? $pt_type : 0x100);
+ \%h;
+ } (1 .. $info->{nbPartitions});
+ \@pt;
+}
+
+sub read_one {
+ my ($hd, $sector) = @_;
+
+ my $l = partition_table::dos::read($hd, $sector);
+ my @l = grep { $_->{size} && $_->{pt_type} && !partition_table::isExtended($_) } @$l;
+ @l == 1 or die "bad PMBR";
+ $l[0]{pt_type} == 0xee or die "bad PMBR";
+ my $myLBA = $l[0]{start};
+
+ my $F = partition_table::raw::openit($hd) 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, pt_type, active
+sub write {
+ my ($hd, $sector, $pt, $info) = @_;
+
+ foreach (@$pt) {
+ $_->{ending} = $_->{start} + $_->{size} - 1;
+ $_->{guid} ||= generate_guid();
+ $_->{gpt_type} = $gpt_types{$_->{pt_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::empty_raw();
+ $pmbr->{raw}[0] = { pt_type => 0xee, local_start => $info->{myLBA}, size => $info->{alternateLBA} - $info->{myLBA} + 1 };
+ partition_table::dos::write($hd, $sector, $pmbr->{raw});
+ }
+
+ my $F = partition_table::raw::openit($hd, 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} && $_->{pt_type} } @$raw;
+}
+sub can_add { &can_raw_add }
+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 use_pt_type { 1 }
+
+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 initialize {
+ my ($class, $hd) = @_;
+ $hd->{primary} = { raw => [], info => info($hd) };
+ bless $hd, $class;
+}
+
+1;
diff --git a/perl-install/partition_table/lvm.pm b/perl-install/partition_table/lvm.pm
new file mode 100644
index 000000000..8a28a4c2a
--- /dev/null
+++ b/perl-install/partition_table/lvm.pm
@@ -0,0 +1,38 @@
+package partition_table::lvm; # $Id: $
+
+# LVM on full disk
+
+use diagnostics;
+use strict;
+
+our @ISA = qw(partition_table::readonly);
+
+use common;
+use partition_table::readonly;
+use fs::type;
+
+sub _parts {
+ my ($hd) = @_;
+
+ my $part = { size => $hd->{totalsectors}, device => $hd->{device} };
+ add2hash($part, fs::type::type_name2subpart('Linux Logical Volume Manager'));
+
+ [ $part ];
+}
+
+sub read_primary {
+ my ($hd) = @_;
+
+ my $type = fs::type::type_subpart_from_magic($hd);
+
+ $type && $type->{type_name} eq 'Linux Logical Volume Manager' or return;
+
+ partition_table::lvm->initialize($hd);
+ 1;
+}
+
+sub initialize {
+ my ($class, $hd) = @_;
+
+ partition_table::readonly::initialize($class, $hd, _parts($hd));
+}
diff --git a/perl-install/partition_table/mac.pm b/perl-install/partition_table/mac.pm
new file mode 100644
index 000000000..015feaee6
--- /dev/null
+++ b/perl-install/partition_table/mac.pm
@@ -0,0 +1,396 @@
+package partition_table::mac; # $Id: mac.pm 228416 2007-09-18 10:32:56Z pixel $
+
+use diagnostics;
+#use strict; - fixed other PPC code to comply, but program bails on empty partition table - sbenedict
+use vars qw(@ISA $freepart $bootstrap_part $macos_part $new_bootstrap);
+
+@ISA = qw(partition_table::raw);
+
+use common;
+use fs::type;
+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 ($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 use_pt_type { 1 }
+
+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_one {
+ my ($hd, $sector) = @_;
+ my $tmp;
+
+ my $F = partition_table::raw::openit($hd) 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;
+
+ foreach (1 .. $info{bzDrvrCnt}) {
+ 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 (my $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{fs_type} = $h{pName} =~ /swap/i ? 'swap' : 'ext2';
+ } elsif ($h{pType} =~ /^Apple_Free/i) {
+ #- need to locate a 1MB partition to setup a bootstrap on
+ if ($freepart && $freepart->{size} >= 1) {
+ #- already found a suitable partition
+ } else {
+ $freepart = { start => $h{start}, size => $h{size}/2048, hd => $hd, part => "/dev/$hd->{device}" . ($i+1) };
+ log::l("free apple partition found on drive /dev/$freepart->{hd}{device}, block $freepart->{start}, size $freepart->{size}");
+ }
+ $h{pt_type} = 0x0;
+ $h{pName} = 'Extra';
+ } elsif ($h{pType} =~ /^Apple_HFS/i) {
+ fs::type::set_pt_type(\%h, 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{pt_type} = 0x401;
+ $h{isMap} = 1;
+ } elsif ($h{pType} =~ /^Apple_Bootstrap/i) {
+ $h{pt_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{pt_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!
+ my $F;
+ if ($::testing) {
+ my $file = "/tmp/partition_table_$hd->{device}";
+ open $F, ">$file" or die "error opening test file $file";
+ } else {
+ $F = partition_table::raw::openit($hd, 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, {
+ pt_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, {
+ pt_type => 0x0,
+ start => $last->{start} + $last->{size},
+ size => $hd->{totalsectors} - ($last->{start} + $last->{size}),
+ };
+ }
+
+ # Since we did not 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;
+ foreach my $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 ($_->{pt_type} == 0x402) {
+ $_->{pType} = "Apple_HFS";
+ $_->{pName} = "MacOS";
+ $_->{pFlags} = 0x4000037F;
+ } elsif ($_->{pt_type} == 0x401 && $_->{start} == 1) {
+ $_->{pType} = "Apple_Partition_Map";
+ $_->{pName} = "Apple";
+ $_->{pFlags} = 0x33;
+ } elsif ($_->{pt_type} == 0x401) {
+ $_->{pType} = "Apple_Bootstrap";
+ $_->{pName} = "bootstrap";
+ $_->{pFlags} = 0x33;
+ $_->{isBoot} = 1;
+ log::l("writing a bootstrap at /dev/$_->{device}");
+ $new_bootstrap = 1 if !(defined $bootstrap_part);
+ $bootstrap_part = "/dev/" . $_->{device};
+ } elsif (isSwap($_)) {
+ $_->{pType} = "Apple_UNIX_SVR2";
+ $_->{pName} = "swap";
+ $_->{pFlags} = 0x33;
+ } elsif ($_->{fs_type} eq 'ext2') {
+ $_->{pType} = "Apple_UNIX_SVR2";
+ $_->{pName} = "Linux Native";
+ $_->{pFlags} = 0x33;
+ } elsif ($_->{fs_type} eq 'reiserfs') {
+ $_->{pType} = "Apple_UNIX_SVR2";
+ $_->{pName} = "Linux ReiserFS";
+ $_->{pFlags} = 0x33;
+ } elsif ($_->{fs_type} eq 'xfs') {
+ $_->{pType} = "Apple_UNIX_SVR2";
+ $_->{pName} = "Linux XFS";
+ $_->{pFlags} = 0x33;
+ } elsif ($_->{fs_type} eq 'jfs') {
+ $_->{pType} = "Apple_UNIX_SVR2";
+ $_->{pName} = "Linux JFS";
+ $_->{pFlags} = 0x33;
+ } elsif ($_->{fs_type} eq 'ext3') {
+ $_->{pType} = "Apple_UNIX_SVR2";
+ $_->{pName} = "Linux ext3";
+ $_->{pFlags} = 0x33;
+ } elsif ($_->{pt_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 initialize {
+ my ($class, $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] = {
+ pt_type => 0x401,
+ start => 1,
+ size => 63,
+ isMap => 1,
+ };
+# $pt->{raw}[1] = {
+# pt_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}};
+
+ $hd->{primary} = $pt;
+ bless $hd, $class;
+}
+
+1;
diff --git a/perl-install/partition_table/raw.pm b/perl-install/partition_table/raw.pm
new file mode 100644
index 000000000..a43cdc2ee
--- /dev/null
+++ b/perl-install/partition_table/raw.pm
@@ -0,0 +1,290 @@
+package partition_table::raw; # $Id: raw.pm 244309 2008-08-21 14:36:52Z pixel $
+
+use diagnostics;
+use strict;
+
+use common;
+use devices;
+use detect_devices;
+use fs::type;
+use log;
+use c;
+
+my @MBR_signatures = (
+if_(arch() =~ /ppc/,
+ (map { [ 'yaboot', 0, "PM", 0x200 * $_ + 0x10, "bootstrap\0" ] } 0 .. 61), #- "PM" is a Partition Map
+ [ 'yaboot', 0x400, "BD", 0x424, "\011bootstrap" ], #- "BD" is a HFS filesystem
+),
+ [ 'empty', 0, "\0\0\0\0" ],
+ [ 'grub', 0, "\xEBG", 0x17d, "stage1 \0" ],
+ [ 'grub', 0, "\xEBH", 0x17e, "stage1 \0" ],
+ [ 'grub', 0, "\xEBH", 0x18a, "stage1 \0" ],
+ sub { my ($F) = @_;
+ #- standard grub has no good magic (Mandriva's grub is patched to have "GRUB" at offset 6)
+ #- so scanning a range of possible places where grub can have its string
+ #- 0x176 found on Conectiva 10
+ my ($min, $max, $magic) = (0x176, 0x181, "GRUB \0");
+ my $tmp;
+ sysseek($F, 0, 0) && sysread($F, $tmp, $max + length($magic)) or return;
+ substr($tmp, 0, 2) eq "\xEBH" or return;
+ index($tmp, $magic, $min) >= 0 && "grub";
+ },
+ [ 'lilo', 0x2, "LILO" ],
+ [ 'lilo', 0x6, "LILO" ],
+ [ 'lilo', 0x6 + 0x40, "LILO" ], #- when relocated in lilo's bsect_update(), variable "space" on paragraph boundary gives 0x40
+ [ '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' ],
+ [ 'os2', 0, "\xFA\xB8\x30\x00", 0xfA, "OS/2" ],
+ [ '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, "\x0A" ],
+ [ 'Acronis', 0, "\xE8\x12\x01" ],
+);
+
+sub typeOfMBR($) { typeFromMagic(devices::make($_[0]), @MBR_signatures) }
+sub typeOfMBR_($) { typeFromMagic($_[0], @MBR_signatures) }
+
+sub use_pt_type { 0 }
+sub hasExtended { 0 }
+sub set_best_geometry_for_the_partition_table {}
+
+sub cylinder_size($) {
+ my ($hd) = @_;
+ $hd->{geom}{sectors} * $hd->{geom}{heads};
+}
+sub first_usable_sector { 1 }
+sub last_usable_sector {
+ my ($hd) = @_;
+ $hd->{totalsectors};
+}
+# no limit
+sub max_partition_start { 1e99 }
+sub max_partition_size { 1e99 }
+
+#- 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 internal_error("adjustEnd get a too small partition to handle correctly");
+}
+
+sub compute_nb_cylinders {
+ my ($geom, $totalsectors) = @_;
+ if ($geom->{heads} && $geom->{sectors}) {
+ $geom->{cylinders} = int $totalsectors / $geom->{heads} / $geom->{sectors};
+ }
+}
+
+sub keep_non_duplicates {
+ my %l;
+ $l{$_->[0]}++ foreach @_;
+ map { @$_ } grep { $l{$_->[0]} == 1 } @_;
+}
+
+sub get_geometries {
+ my (@hds) = @_;
+
+ @hds = grep {
+ if (my $h = get_geometry($_->{file})) {
+ add2hash_($_, $h);
+ 1;
+ } else {
+ log::l("An error occurred while getting the geometry of block device $_->{file}: $!");
+ 0;
+ }
+ } @hds;
+
+ my %id2hd = keep_non_duplicates(map {
+ my $F = openit($_) or log::l("failed to open device $_->{device}");
+ my $tmp;
+ if ($F && c::lseek_sector(fileno($F), 0, 0x1b8) && sysread($F, $tmp, 4)) {
+ [ sprintf('0x%08x', unpack('V', $tmp)), $_ ];
+ } else {
+ ();
+ }
+ } @hds);
+
+
+ my %id2edd = keep_non_duplicates(map { [ scalar(chomp_(cat_("$_/mbr_signature"))), $_ ] } glob("/sys/firmware/edd/int13_dev*"));
+
+ log::l("id2hd: " . join(' ', map_each { "$::a=>$::b->{device}" } %id2hd));
+ log::l("id2edd: " . join(' ', map_each { "$::a=>$::b" } %id2edd));
+
+ foreach my $id (keys %id2hd) {
+ my $hd = $id2hd{$id};
+ $hd->{volume_id} = $id;
+
+ if (my $edd_dir = $id2edd{$id}) {
+ $hd->{bios_from_edd} = $1 if $edd_dir =~ /int13_dev(.*)/;
+
+ require partition_table::dos;
+ my $geom = partition_table::dos::geometry_from_edd($hd, $edd_dir);
+ $hd->{geom} = $geom if $geom;
+ }
+ }
+
+ @hds;
+}
+
+sub get_geometry {
+ my ($dev) = @_;
+ sysopen(my $F, $dev, 0) or return;
+
+ my $total = c::total_sectors(fileno $F);
+
+ my $g = "";
+ my %geom;
+ if (ioctl($F, c::HDIO_GETGEO(), $g)) {
+ @geom{qw(heads sectors cylinders start)} = unpack "CCSL", $g;
+ log::l("HDIO_GETGEO on $dev succeeded: heads=$geom{heads} sectors=$geom{sectors} cylinders=$geom{cylinders} start=$geom{start}");
+ $geom{totalcylinders} = $geom{cylinders};
+
+ #- $geom{cylinders} is no good (only a ushort, that means less than 2^16 => at best 512MB)
+ if ($total) {
+ compute_nb_cylinders(\%geom, $total);
+ } else {
+ $total = $geom{heads} * $geom{sectors} * $geom{cylinders};
+ }
+ }
+
+ { totalsectors => $total, if_($geom{heads}, geom => \%geom) };
+}
+
+sub openit {
+ my ($hd, $o_mode) = @_;
+ my $F; sysopen($F, $hd->{file}, $o_mode || 0) && $F;
+}
+
+sub can_add {
+ my ($hd) = @_;
+ !$_->{size} && !$_->{pt_type} || isExtended($_) and return 1 foreach @{$hd->{primary}{raw}};
+ 0;
+}
+
+sub raw_removed {
+ my ($_hd, $_raw) = @_;
+}
+sub can_raw_add {
+ my ($hd) = @_;
+ $_->{size} || $_->{pt_type} or return 1 foreach @{$hd->{primary}{raw}};
+ 0;
+}
+sub raw_add {
+ my ($_hd, $raw, $part) = @_;
+
+ foreach (@$raw) {
+ $_->{size} || $_->{pt_type} and next;
+ $_ = $part;
+ return;
+ }
+ die "raw_add: partition table already full";
+}
+
+sub zero_MBR { &partition_table::initialize } #- deprecated
+
+sub clear_existing {
+ my ($hd) = @_;
+ my @parts = (partition_table::get_normal_parts($hd), if_($hd->{primary}{extended}, $hd->{primary}{extended}));
+ partition_table::will_tell_kernel($hd, del => $_) foreach @parts;
+}
+
+#- deprecated
+sub zero_MBR_and_dirty {
+ my ($hd) = @_;
+ fsedit::partition_table_clear_and_initialize([], $hd);
+}
+
+sub read_primary {
+ my ($hd) = @_;
+
+ my ($pt, $info) = eval { $hd->read_one(0) } or return;
+ my $primary = partition_table::raw::pt_info_to_primary($hd, $pt, $info);
+ $hd->{primary} = $primary;
+ undef $hd->{extended};
+ partition_table::verifyPrimary($primary);
+ 1;
+}
+
+sub pt_info_to_primary {
+ my ($hd, $pt, $info) = @_;
+
+ my @extended = $hd->hasExtended ? grep { isExtended($_) } @$pt : ();
+ my @normal = grep { $_->{size} && !isEmpty($_) && !isExtended($_) } @$pt;
+ my $nb_special_empty = int(grep { $_->{size} && isEmpty($_) } @$pt);
+
+ @extended > 1 and die "more than one extended partition";
+
+ put_in_hash($_, partition_table::hd2minimal_part($hd)) foreach @normal, @extended;
+ { raw => $pt, extended => $extended[0], normal => \@normal, info => $info, nb_special_empty => $nb_special_empty };
+}
+
+#- 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 not be written to (especially for Smartmedia)
+sub test_for_bad_drives {
+ my ($hd) = @_;
+
+ my $sector = $hd->{geom} ? $hd->{geom}{sectors} - 1 : 0;
+ log::l("test_for_bad_drives($hd->{file} on sector #$sector)");
+
+ sub error { die "$_[0] error: $_[1]" }
+
+ my $F = openit($hd, $::testing ? 0 : 2) or error(openit($hd) ? 'write' : 'read', "can not open device");
+
+ my $seek = sub {
+ c::lseek_sector(fileno($F), $sector, 0) or error('read', "seeking to sector $sector failed");
+ };
+ my $tmp;
+
+ &$seek; sysread $F, $tmp, $SECTORSIZE or error('read', "can not even read ($!)");
+ return if $hd->{readonly} || $::testing;
+ &$seek; syswrite $F, $tmp or error('write', "can not even write ($!)");
+
+ my $tmp2;
+ &$seek; sysread $F, $tmp2, $SECTORSIZE or die "test_for_bad_drives: can not even read again ($!)";
+ $tmp eq $tmp2 or die
+N("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, corrupted data.");
+}
+
+1;
diff --git a/perl-install/partition_table/readonly.pm b/perl-install/partition_table/readonly.pm
new file mode 100644
index 000000000..f36b60e4a
--- /dev/null
+++ b/perl-install/partition_table/readonly.pm
@@ -0,0 +1,20 @@
+package partition_table::readonly; # $Id: $
+
+use diagnostics;
+use strict;
+
+our @ISA = qw(partition_table::raw);
+
+use common;
+use partition_table::raw;
+use fs::type;
+
+sub initialize {
+ my ($class, $hd, $parts) = @_;
+
+ $hd->{readonly} = $hd->{getting_rid_of_readonly_allowed} = 1;
+ $hd->{primary} = { normal => $parts };
+ delete $hd->{extended};
+
+ bless $hd, $class;
+}
diff --git a/perl-install/partition_table/sun.pm b/perl-install/partition_table/sun.pm
new file mode 100644
index 000000000..47d702621
--- /dev/null
+++ b/perl-install/partition_table/sun.pm
@@ -0,0 +1,205 @@
+package partition_table::sun; # $Id: sun.pm 228416 2007-09-18 10:32:56Z pixel $
+
+use diagnostics;
+use strict;
+use vars qw(@ISA);
+
+@ISA = qw(partition_table::raw);
+
+use common;
+use partition_table::raw;
+use partition_table;
+use fs::type;
+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(pt_type flags) ], [ qw(start_cylinder size) ]);
+my ($format1, $format2) = ("xCxC", "N2");
+my $magic = 0xDABE;
+my $nb_primary = 8;
+my $offset = 0;
+
+sub use_pt_type { 1 }
+
+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;
+
+ $crc ^= $_ foreach @l2b;
+
+ $crc;
+}
+
+sub read_one {
+ my ($hd, $sector) = @_;
+ my $tmp;
+
+ my $F = partition_table::raw::openit($hd) 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};
+ foreach (0..$nb_primary-1) {
+ my $h = { flag => $infos_up[1 + 2 * $_],
+ start_cylinder => $partitions_up[2 * $_], size => $partitions_up[1 + 2 * $_] };
+ fs::type::set_pt_type($h, $infos_up[2 * $_]);
+ $h->{start} = $sector + $h->{start_cylinder} * $hd->cylinder_size;
+ $h->{pt_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{pt_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, pt_type, active
+sub write($$$;$) {
+ my ($hd, $sector, $pt, $info) = @_;
+# my ($csize, $wdsize) = (0, 0);
+
+ #- handle testing for writing partition table on file only!
+ my $F;
+ if ($::testing) {
+ my $file = "/tmp/partition_table_$hd->{device}";
+ open $F, ">$file" or die "error opening test file $file";
+ } else {
+ $F = partition_table::raw::openit($hd, 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 $_->{pt_type} != 5;
+# $wdsize += $_->{size} if $_->{pt_type} == 5;
+ $_->{flags} |= 0x10 if $_->{mntpoint} eq '/';
+ $_->{flags} |= 0x01 if !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 initialize {
+ my ($hd) = @_;
+ my $pt = { raw => [ ({}) x $nb_primary ], info => info($hd) };
+
+ #- handle special case for partition 2 which is whole disk.
+ $pt->{raw}[2] = {
+ pt_type => 5, #- the whole disk type.
+ flags => 0,
+ start_cylinder => 0,
+ size => $hd->{geom}{cylinders} * $hd->cylinder_size,
+ };
+
+ $hd->{primary} = $pt;
+ bless $hd, 'partition::sun';
+}
+
+1;