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