use bytes;
use Fcntl;
-# Use this fake geometry (zipdrive-style...)
-$h = 64; $s = 32;
+# User-specifyable options
+%opt = (
+ # Fake geometry (zipdrive-style...)
+ 'h' => 64,
+ 's' => 32,
+ # Partition number
+ 'entry' => 1,
+ # Partition offset
+ 'offset' => 0,
+ # Partition type
+ 'type' => 0x17, # "Windows hidden IFS"
+ # MBR ID
+ 'id' => undef,
+);
+
+%valid_range = (
+ 'h' => [1, 256],
+ 's' => [1, 63],
+ 'entry' => [1, 4],
+ 'offset' => [0, 64],
+ 'type' => [0, 255],
+ 'id' => [0, 0xffffffff],
+);
+
+sub usage() {
+ print STDERR "Usage: $0 [options] filename\n",
+ "Options:\n",
+ " -h Number of default geometry heads\n",
+ " -s Number of default geometry sectors\n",
+ " -entry Specify partition entry number (1-4)\n",
+ " -offset Specify partition offset (default 0)\n",
+ " -type Specify partition type (default 0x17)\n",
+ " -id Specify MBR ID (default random)\n";
+ exit 1;
+}
+
+# Parse a C-style integer (decimal/octal/hex)
+sub doh($) {
+ my($n) = @_;
+ return ($n =~ /^0/) ? oct $n : $n+0;
+}
sub get_random() {
# Get a 32-bit random number
return ($$+time()) & 0xffffffff;
}
+while ($ARGV[0] =~ /^\-(.*)$/) {
+ $o = $1;
+ shift @ARGV;
+ if (exists($opt{$o})) {
+ $opt{$o} = doh(shift @ARGV);
+ if (defined($valid_range{$o})) {
+ ($l, $h) = @{$valid_range{$o}};
+ if ($opt{$o} < $l || $opt{$o} > $h) {
+ die "$0: valid values for the -$o parameter are $l to $h\n";
+ }
+ }
+ } else {
+ usage();
+ }
+}
($file) = @ARGV;
+
+if (!defined($file)) {
+ usage();
+}
+
open(FILE, "+< $file\0") or die "$0: cannot open $file: $!\n";
binmode FILE;
die "$0: $file: cannot determine length of file\n";
}
# Target image size: round up to a multiple of $h*$s*512
+$h = $opt{'h'};
+$s = $opt{'s'};
$cylsize = $h*$s*512;
$frac = $imgsize % $cylsize;
$padding = ($frac > 0) ? $cylsize - $frac : 0;
}
# Preserve id when run again
-seek(FILE, 440, SEEK_SET) or die "$0: $file: $!\n";
-read(FILE, $id, 4);
-if ($id eq "\x00\x00\x00\x00") {
- $id = pack("V", get_random());
+if (defined($opt{'id'})) {
+ $id = $opt{'id'};
+} else {
+ seek(FILE, 440, SEEK_SET) or die "$0: $file: $!\n";
+ read(FILE, $id, 4);
+ if ($id eq "\x00\x00\x00\x00") {
+ $id = pack("V", get_random());
+ }
}
# Print the MBR and partition table
$mbr .= "\0" x (432 - length($mbr));
-$mbr .= pack("VV", $de_lba*4, 0); # Offset 432: LBA of isolinux.bin
+$mbr .= pack("VV", $de_lba*4, 0); # Offset 432: LBA of isolinux.bin
$mbr .= $id; # Offset 440: MBR ID
-$mbr .= "\0\0"; # Offset 446: actual partition table
+$mbr .= "\0\0"; # Offset 446: actual partition table
# Print partition table
+$offset = $opt{'offset'};
$psize = $c*$h*$s;
-$bhead = 0;
-$bsect = 1;
-$bcyl = 0;
+$bhead = int($offset/$s) % $h;
+$bsect = ($offset % $s) + 1;
+$bcyl = int($offset/($h*$s));
+$bsect += ($bcyl & 0x300) >> 2;
+$bcyl &= 0xff;
$ehead = $h-1;
$esect = $s + ((($cc-1) & 0x300) >> 2);
$ecyl = ($cc-1) & 0xff;
-$fstype = 0x83; # Linux (any better ideas?)
-$pentry = 1; # First partition slot
+$fstype = $opt{'type'}; # Partition type
+$pentry = $opt{'entry'}; # Partition slot
for ( $i = 1 ; $i <= 4 ; $i++ ) {
if ( $i == $pentry ) {