-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdimage
executable file
·452 lines (361 loc) · 10.7 KB
/
hdimage
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#! /usr/bin/perl
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# package HDImage version 1.5
#
# Create disk image with partition table and a single partition.
#
# Copyright (c) 2008 Steffen Winterfeldt
#
# License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
# This is free software: you are free to change and redistribute it.
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
{
package HDImage;
use strict 'vars';
use bigint;
sub new
{
my $self = {};
bless $self;
return $self;
}
sub verbose
{
my $self = shift;
$self->{verbose} = shift;
}
sub mbr
{
my $self = shift;
if(@_) {
my $file = shift;
open F1, $file;
sysread F1, $self->{mbr}, 440;
close F1;
if(length($self->{mbr}) != 440) {
print STDERR "warning: $file: no valid MBR\n";
}
}
else {
undef $self->{mbr};
}
}
sub boot_fat12
{
my $self = shift;
if(@_) {
my $file = shift;
open F1, $file;
sysread F1, $self->{boot_fat12}, 512;
close F1;
if(length($self->{boot_fat12}) != 512 || substr($self->{boot_fat12}, 0x1fe, 2) ne "\x55\xaa") {
print STDERR "warning: $file: no valid boot block\n";
}
}
else {
undef $self->{boot_fat12};
}
}
sub boot_fat16
{
my $self = shift;
if(@_) {
my $file = shift;
open F1, $file;
sysread F1, $self->{boot_fat16}, 512;
close F1;
if(length($self->{boot_fat16}) != 512 || substr($self->{boot_fat16}, 0x1fe, 2) ne "\x55\xaa") {
print STDERR "warning: $file: no valid boot block\n";
}
}
else {
undef $self->{boot_fat16};
}
}
sub chs
{
my $self = shift;
my $c = shift;
my $h = shift;
my $s = shift;
$h = 255 if $h < 1 || $h > 255;
$s = 63 if $s < 1 || $s > 63;
$self->{h} = $h;
$self->{s} = $s;
if($c == 0 && $self->{size}) {
$c = ($self->{size} + $h * $s - 1) / $h / $s;
}
if($c > 0) {
$self->{c} = $c;
$self->{size} = $c * $h * $s;
}
return $self->{size};
}
sub size
{
my $self = shift;
my $size = $self->parse_size(shift);
$self->{size} = $size;
if($self->{h} && $self->{s}) {
$self->{c} = ($self->{size} + $self->{h} * $self->{s} - 1) / $self->{h} / $self->{s};
$self->{size} = $self->{c} * $self->{h} * $self->{s};
}
return $self->{size};
}
sub extra_size
{
my $self = shift;
$self->{extra_size} = $self->parse_size(shift);
}
sub type
{
my $self = shift;
$self->{type} = shift;
}
sub label
{
my $self = shift;
$self->{label} = shift;
}
sub fs
{
my $self = shift;
$self->{fs} = shift;
}
sub add_files
{
my $self = shift;
local $_;
for (@_) {
if(-f || -d) {
push @{$self->{files}}, $_;
}
else {
print STDERR "$_: no such file or directory\n";
}
}
}
sub tmp_file
{
my $self = shift;
chomp (my $t = `mktemp /tmp/HDImage.XXXXXXXXXX`);
die "error: mktemp failed\n" if $?;
eval 'END { unlink $t }';
my $s_t = $SIG{TERM};
$SIG{TERM} = sub { unlink $t; &$s_t if $s_t };
my $s_i = $SIG{INT};
$SIG{INT} = sub { unlink $t; &$s_i if $s_i };
return $t;
}
sub partition_ofs
{
my $self = shift;
$self->{part_ofs} = $self->parse_size(shift) if @_;
return defined($self->{part_ofs}) ? $self->{part_ofs} : $self->{s};
}
sub write
{
my $self = shift;
local $_;
return undef unless @_;
my $file = shift;
$self->{image_name} = $file;
$self->chs(0, 255, 63) unless $self->{s};
my $p_size = $self->{size} - $self->partition_ofs - $self->{extra_size};
return undef if $p_size < 0;
my $c = $self->{c};
my $h = $self->{h};
my $s = $self->{s};
my $type = $self->{type};
my $pt_size = $self->partition_ofs;
my $p_end = $p_size + $pt_size - 1;
$type = 0x83 unless defined $type;
print "$file: chs = $c/$h/$s, size = $self->{size} blocks\n" if $self->{verbose};
print "- writing mbr\n" if $self->{verbose} && $self->{mbr};
$c = 1023 if $c > 1023;
my $s_0 = $pt_size % $s + 1;
my $h_0 = ($pt_size / $s) % $h;
my $c_0 = $pt_size / ($s * $h);
$c_0 = 1023 if $c_0 > 1023;
my $s_1 = $p_end % $s + 1;
my $h_1 = ($p_end / $s) % $h;
my $c_1 = $p_end / ($s * $h);
$c_1 = 1023 if $c_1 > 1023;
my $p_0 = $pt_size;
$p_0 = 0xffffffff if $p_0 > 0xffffffff;
my $p_1 = $p_size;
$p_1 = 0xffffffff if $p_1 > 0xffffffff;
open W1, ">$file";
if($pt_size) {
my $mbr = pack (
"Z446CCCCCCCCVVZ48v",
$self->{mbr}, # boot code, if any
0x80, # bootflag
$h_0, # head start
(($c_0 >> 8) << 6) + $s_0, # cyl/sector start, low
$c_0 & 0xff, # cyl/sector start, hi
$type, # partition type
$h_1, # head last
(($c_1 >> 8) << 6) + $s_1, # cyl/sector last, low
$c_1 & 0xff, # cyl/sector last, hi
$p_0, # partition offset
$p_1, # partition size
"", 0xaa55
);
syswrite W1, $mbr;
if($pt_size > 1) {
sysseek W1, $pt_size * 512 - 1, 0;
syswrite W1, "\x00", 1;
}
}
close W1;
if($p_size) {
if($self->{fs}) {
my $f = $pt_size ? tmp_file() : $file;
open W1, ">$f";
sysseek W1, $p_size * 512 - 1, 0;
syswrite W1, "\x00", 1;
close W1;
if($self->{fs} eq 'fat') {
my $x = " -n '$self->{label}'" if $self->{label} ne "";
system "mkfs.vfat -h $pt_size$x $f >/dev/null";
my ($fat, $boot);
# mkfs.vfat is a bit stupid; fix FAT superblock
open W1, "+<$f";
sysseek W1, 0x18, 0;
syswrite W1, pack("vv", $s, $h);
sysseek W1, 0x24, 0;
syswrite W1, "\xff";
sysseek W1, 0x36, 0;
sysread W1, $fat, 5;
# FAT32: at ofs 0x52
close W1;
$boot = $self->{boot_fat12} if $fat eq "FAT12";
$boot = $self->{boot_fat16} if $fat eq "FAT16";
# write boot block ex bpb
if($boot) {
print "- writing \L$fat\E boot block\n" if $self->{verbose};
open W1, "+<$f";
syswrite W1, $boot, 11;
sysseek W1, 0x3e, 0;
syswrite W1, substr($boot, 0x3e);
close W1;
}
if($self->{files}) {
print "- copying:\n " . join("\n ", @{$self->{files}}) . "\n" if $self->{verbose};
system "mcopy -D o -s -i $f " . join(" ", @{$self->{files}}) . " ::";
}
}
elsif($self->{fs} eq 'ext2' || $self->{fs} eq 'ext3') {
my $x = " -L '$self->{label}'" if $self->{label} ne "";
system "mkfs.$self->{fs} -q -m 0 -F$x $f";
system "tune2fs -c 0 -i 0 $f >/dev/null 2>&1";
}
elsif($self->{fs} eq 'reiserfs') {
my $x = " -l '$self->{label}'" if $self->{label} ne "";
system "mkfs.reiserfs -q -ff$x $f";
}
elsif($self->{fs} eq 'xfs') {
my $x = " -L '$self->{label}'" if $self->{label} ne "";
system "mkfs.xfs -q$x $f";
}
else {
print STDERR "warning: $self->{fs}: unsupported file system\n";
}
if($pt_size) {
system "cat $f >>$file";
unlink $f;
}
}
else {
open W1, "+<$file";
sysseek W1, ($self->{size} - $self->{extra_size}) * 512 - 1, 0;
syswrite W1, "\x00", 1;
close W1;
}
}
if($self->{extra_size}) {
open W1, "+<$file";
sysseek W1, $self->{extra_size} * 512 - 1, 2;
syswrite W1, "\x00", 1;
close W1;
}
}
sub parse_size
{
my $self = shift;
my $s = shift;
my $bs = 0;
if($s =~ s/(b|k|M|G|T|P|E)$//) {
$bs = 0 if $1 eq 'b';
$bs = 1 if $1 eq 'k';
$bs = 11 if $1 eq 'M';
$bs = 21 if $1 eq 'G';
$bs = 31 if $1 eq 'T';
$bs = 41 if $1 eq 'P';
$bs = 51 if $1 eq 'E';
}
# note: 'bigint' works a bit differently when converting strings to numbers
$s = $s << $bs;
return $s + 1 == $s ? undef : $s;
}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
use Getopt::Long;
sub usage;
sub parse_size;
usage 0 if !@ARGV;
GetOptions(
'help' => sub { usage 0 },
'verbose+' => \$opt_verbose,
'mbr=s' => \$opt_mbr,
'part-ofs=s' => \$opt_partition_ofs,
'type=o' => \$opt_type,
'size=s' => \$opt_size,
'extra-size=s' => \$opt_extra_size,
'chs=o{3}' => \@opt_chs,
'mkfs=s' => \$opt_mkfs,
'label=s' => \$opt_label,
'boot-fat12=s' => \$opt_boot_fat12,
'boot-fat16=s' => \$opt_boot_fat16,
'add-files=s{1,}' => \@opt_files,
) || usage 1;
usage 1 if @ARGV != 1;
$file = shift;
$hdimage = HDImage::new;
$hdimage->verbose($opt_verbose);
$hdimage->extra_size($opt_extra_size) if $opt_extra_size;
$size = $hdimage->chs(@opt_chs) if (@opt_chs);
$size = $hdimage->size($opt_size) if defined $opt_size;
die "sorry, no disk size\n" unless defined $size;
$hdimage->type($opt_type);
$hdimage->partition_ofs($opt_partition_ofs) if defined $opt_partition_ofs;
$hdimage->label($opt_label);
$hdimage->fs($opt_mkfs);
$hdimage->mbr($opt_mbr) if $opt_mbr;
$hdimage->boot_fat12($opt_boot_fat12) if $opt_boot_fat12;
$hdimage->boot_fat16($opt_boot_fat16) if $opt_boot_fat16;
$hdimage->add_files(@opt_files) if @opt_files;
$hdimage->write($file);
sub usage
{
print <<" usage";
Usage: hdimage [options] image_file
Create disk image with partition table and a single bootable partition.
Options:
--size SIZE Disk size. Will be rounded up to full cylinders.
--part-ofs SIZE Start partition at SIZE (0 = no partition table).
--extra-size SIZE Leave that much space after partition.
--type PARTITIONTYPE Set partition type.
--chs CYLINDERS HEADS SECTORS Disk geometry.
--mbr FILE Add bootloader from FILE to MBR.
--mkfs FS Create file system FS (FS: ext2, ext3, fat, reiserfs, xfs).
--boot-fat12 FILE For FAT12 filesystem: add bootloader from FILE to FAT boot block.
--boot-fat16 FILE For FAT16 filesystem: add bootloader from FILE to FAT boot block.
--add-files FILE1 FILE2 ... Copy files to FAT partition.
--verbose Be more verbose.
SIZE may include a unit (b, k, M, G, T, P, E). Default is b (512 bytes).
usage
exit shift;
}