package partition_table_bsd; # $Id$

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

@ISA = qw(partition_table_raw);

use common qw(:common :system :file :functional);
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";
    $info{magic2} == $magic or die "bad magic number";

    [ @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) };
}

1;
mtim/designWork</option>
<option value='user/colin/rescue-systemd'>user/colin/rescue-systemd</option>
<option value='user/ennael/mga6'>user/ennael/mga6</option>
<option value='user/erwan/bug-13680'>user/erwan/bug-13680</option>
<option value='user/jibz/aarch64'>user/jibz/aarch64</option>
<option value='user/martinw/mga6'>user/martinw/mga6</option>
<option value='user/pterjan/arm64'>user/pterjan/arm64</option>
</select> <input type='submit' value='switch'/></form></td></tr>
<tr><td class='sub'>Mageia Installer and base platform for many utilities</td><td class='sub right'>Thierry Vignaud [tv]</td></tr></table>
<table class='tabs'><tr><td>
<a href='/software/drakx/?h=17.81'>summary</a><a href='/software/drakx/refs/?h=17.81&amp;id=f88c47292b5d025700c9b52db0a3cb8128fb3e31'>refs</a><a href='/software/drakx/log/mdk-stage1?h=17.81'>log</a><a href='/software/drakx/tree/mdk-stage1?h=17.81&amp;id=f88c47292b5d025700c9b52db0a3cb8128fb3e31'>tree</a><a class='active' href='/software/drakx/commit/mdk-stage1?h=17.81&amp;id=f88c47292b5d025700c9b52db0a3cb8128fb3e31'>commit</a><a href='/software/drakx/diff/mdk-stage1?h=17.81&amp;id=f88c47292b5d025700c9b52db0a3cb8128fb3e31'>diff</a><a href='/software/drakx/stats/mdk-stage1?h=17.81'>stats</a></td><td class='form'><form class='right' method='get' action='/software/drakx/log/mdk-stage1'>
<input type='hidden' name='h' value='17.81'/><input type='hidden' name='id' value='f88c47292b5d025700c9b52db0a3cb8128fb3e31'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/software/drakx/commit/?h=17.81&amp;id=f88c47292b5d025700c9b52db0a3cb8128fb3e31'>root</a>/<a href='/software/drakx/commit/mdk-stage1?h=17.81&amp;id=f88c47292b5d025700c9b52db0a3cb8128fb3e31'>mdk-stage1</a></div><div class='content'><div class='cgit-panel'><b>diff options</b><form method='get'><input type='hidden' name='h' value='17.81'/><input type='hidden' name='id' value='f88c47292b5d025700c9b52db0a3cb8128fb3e31'/><table><tr><td colspan='2'/></tr><tr><td class='label'>context:</td><td class='ctrl'><select name='context' onchange='this.form.submit();'><option value='1'>1</option><option value='2'>2</option><option value='3' selected='selected'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option><option value='15'>15</option><option value='20'>20</option><option value='25'>25</option><option value='30'>30</option><option value='35'>35</option><option value='40'>40</option></select></td></tr><tr><td class='label'>space:</td><td class='ctrl'><select name='ignorews' onchange='this.form.submit();'><option value='0' selected='selected'>include</option><option value='1'>ignore</option></select></td></tr><tr><td class='label'>mode:</td><td class='ctrl'><select name='dt' onchange='this.form.submit();'><option value='0' selected='selected'>unified</option><option value='1'>ssdiff</option><option value='2'>stat only</option></select></td></tr><tr><td/><td class='ctrl'><noscript><input type='submit' value='reload'/></noscript></td></tr></table></form></div><table summary='commit info' class='commit-info'>
<tr><th>author</th><td>Thierry Vignaud &lt;tv@mageia.org&gt;</td><td class='right'>2012-09-17 17:59:07 +0000</td></tr>
<tr><th>committer</th><td>Thierry Vignaud &lt;tv@mageia.org&gt;</td><td class='right'>2012-09-17 17:59:07 +0000</td></tr>
<tr><th>commit</th><td colspan='2' class='sha1'><a href='/software/drakx/commit/mdk-stage1?h=17.81&amp;id=f88c47292b5d025700c9b52db0a3cb8128fb3e31'>f88c47292b5d025700c9b52db0a3cb8128fb3e31</a> (<a href='/software/drakx/patch/mdk-stage1?id=f88c47292b5d025700c9b52db0a3cb8128fb3e31'>patch</a>)</td></tr>
<tr><th>tree</th><td colspan='2' class='sha1'><a href='/software/drakx/tree/?h=17.81&amp;id=f88c47292b5d025700c9b52db0a3cb8128fb3e31'>c87f5925c838615485f09c570fa52f24a5dcb549</a> /<a href='/software/drakx/tree/mdk-stage1?h=17.81&amp;id=f88c47292b5d025700c9b52db0a3cb8128fb3e31'>mdk-stage1</a></td></tr>
<tr><th>parent</th><td colspan='2' class='sha1'><a href='/software/drakx/commit/mdk-stage1?h=17.81&amp;id=f605d42f44ed046ab7152a680624165c837bbcfc'>f605d42f44ed046ab7152a680624165c837bbcfc</a> (<a href='/software/drakx/diff/mdk-stage1?h=17.81&amp;id=f88c47292b5d025700c9b52db0a3cb8128fb3e31&amp;id2=f605d42f44ed046ab7152a680624165c837bbcfc'>diff</a>)</td></tr><tr><th>download</th><td colspan='2' class='sha1'><a href='/software/drakx/snapshot/drakx-f88c47292b5d025700c9b52db0a3cb8128fb3e31.tar'>drakx-f88c47292b5d025700c9b52db0a3cb8128fb3e31.tar</a><br/><a href='/software/drakx/snapshot/drakx-f88c47292b5d025700c9b52db0a3cb8128fb3e31.tar.gz'>drakx-f88c47292b5d025700c9b52db0a3cb8128fb3e31.tar.gz</a><br/><a href='/software/drakx/snapshot/drakx-f88c47292b5d025700c9b52db0a3cb8128fb3e31.tar.bz2'>drakx-f88c47292b5d025700c9b52db0a3cb8128fb3e31.tar.bz2</a><br/><a href='/software/drakx/snapshot/drakx-f88c47292b5d025700c9b52db0a3cb8128fb3e31.tar.xz'>drakx-f88c47292b5d025700c9b52db0a3cb8128fb3e31.tar.xz</a><br/><a href='/software/drakx/snapshot/drakx-f88c47292b5d025700c9b52db0a3cb8128fb3e31.zip'>drakx-f88c47292b5d025700c9b52db0a3cb8128fb3e31.zip</a><br/></td></tr></table>
<div class='commit-subject'>add firmware loader written in C (based on hotplug2) (<a href="https://bugs.mageia.org/6323">mga#6323</a>)</div><div class='commit-msg'>