2 ## -----------------------------------------------------------------------
4 ## Copyright 2002-2008 H. Peter Anvin - All Rights Reserved
5 ## Copyright 2009 Intel Corporation; author: H. Peter Anvin
7 ## This program is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation, Inc., 53 Temple Place Ste 330,
10 ## Boston MA 02111-1307, USA; either version 2 of the License, or
11 ## (at your option) any later version; incorporated herein by reference.
13 ## -----------------------------------------------------------------------
16 # Post-process an ISO 9660 image generated with mkisofs/genisoimage
17 # to allow "hybrid booting" as a CD-ROM or as a hard disk.
23 # User-specifyable options
25 # Fake geometry (zipdrive-style...)
33 'type' => 0x17, # "Windows hidden IFS"
44 'id' => [0, 0xffffffff],
48 # Boolean options just set other options
50 'nohd0' => ['hd0', 0],
51 'forcehd0' => ['hd0', 1],
52 'ctrlhd0' => ['hd0', 2],
56 print STDERR "Usage: $0 [options] filename.iso\n",
58 " -h Number of default geometry heads\n",
59 " -s Number of default geometry sectors\n",
60 " -entry Specify partition entry number (1-4)\n",
61 " -offset Specify partition offset (default 0)\n",
62 " -type Specify partition type (default 0x17)\n",
63 " -id Specify MBR ID (default random)\n",
64 " -forcehd0 Always assume we are loaded as disk ID 0\n",
65 " -ctrlhd0 Assume disk ID 0 if the Ctrl key is pressed\n",
69 # Parse a C-style integer (decimal/octal/hex)
72 return ($n =~ /^0/) ? oct $n : $n+0;
76 # Get a 32-bit random number
80 if (open($rfd, "< /dev/urandom\0") && read($rfd, $rnd, 4) == 4) {
81 $rid = unpack("V", $rnd);
84 close($rfd) if (defined($rfd));
85 return $rid if (defined($rid));
87 # This sucks but is better than nothing...
88 return ($$+time()) & 0xffffffff;
94 while ( $line = <DATA> ) {
96 last if ($line eq '*');
97 foreach $byte ( split(/\s+/, $line) ) {
98 $mbr .= chr(hex($byte));
104 while ($ARGV[0] =~ /^\-(.*)$/) {
107 if (defined($bool_opt{$o})) {
108 ($o, $v) = @{$bool_opt{$o}};
110 } elsif (exists($opt{$o})) {
111 $opt{$o} = doh(shift @ARGV);
112 if (defined($valid_range{$o})) {
113 ($l, $h) = @{$valid_range{$o}};
114 if ($opt{$o} < $l || $opt{$o} > $h) {
115 die "$0: valid values for the -$o parameter are $l to $h\n";
125 if (!defined($file)) {
129 open(FILE, "+< $file\0") or die "$0: cannot open $file: $!\n";
133 # First, actually figure out where mkisofs hid isolinux.bin
135 seek(FILE, 17*2048, SEEK_SET) or die "$0: $file: $!\n";
136 read(FILE, $boot_record, 2048) == 2048 or die "$0: $file: read error\n";
137 ($br_sign, $br_cat_offset) = unpack("a71V", $boot_record);
138 if ($br_sign ne ("\0CD001\1EL TORITO SPECIFICATION" . ("\0" x 41))) {
139 die "$0: $file: no boot record found\n";
141 seek(FILE, $br_cat_offset*2048, SEEK_SET) or die "$0: $file: $!\n";
142 read(FILE, $boot_cat, 2048) == 2048 or die "$0: $file: read error\n";
144 # We must have a Validation Entry followed by a Default Entry...
145 # no fanciness allowed for the Hybrid mode [XXX: might relax this later]
146 @ve = unpack("v16", $boot_cat);
148 for ($i = 0; $i < 16; $i++) {
151 if ($ve[0] != 0x0001 || $ve[15] != 0xaa55 || $cs & 0xffff) {
152 die "$0: $file: invalid boot catalog\n";
154 ($de_boot, $de_media, $de_seg, $de_sys, $de_mbz1, $de_count,
155 $de_lba, $de_mbz2) = unpack("CCvCCvVv", substr($boot_cat, 32, 32));
156 if ($de_boot != 0x88 || $de_media != 0 ||
157 ($de_segment != 0 && $de_segment != 0x7c0) || $de_count != 4) {
158 die "$0: $file: unexpected boot catalog parameters\n";
161 # Now $de_lba should contain the CD sector number for isolinux.bin
162 seek(FILE, $de_lba*2048+0x40, SEEK_SET) or die "$0: $file: $!\n";
163 read(FILE, $ibsig, 4);
164 if ($ibsig ne "\xfb\xc0\x78\x70") {
165 die "$0: $file: bootloader is missing isolinux.bin hybrid signature\n".
166 "Note: isolinux-debug.bin does not support hybrid booting\n";
169 # Get the total size of the image
170 (@imgstat = stat(FILE)) or die "$0: $file: $!\n";
171 $imgsize = $imgstat[7];
173 die "$0: $file: cannot determine length of file\n";
175 # Target image size: round up to a multiple of $h*$s*512
178 $cylsize = $h*$s*512;
179 $frac = $imgsize % $cylsize;
180 $padding = ($frac > 0) ? $cylsize - $frac : 0;
181 $imgsize += $padding;
182 $c = int($imgsize/$cylsize);
184 print STDERR "Warning: more than 1024 cylinders ($c).\n";
185 print STDERR "Not all BIOSes will be able to boot this device.\n";
191 # Preserve id when run again
192 if (defined($opt{'id'})) {
195 seek(FILE, 440, SEEK_SET) or die "$0: $file: $!\n";
197 if ($id eq "\x00\x00\x00\x00") {
198 $id = pack("V", get_random());
202 # Print the MBR and partition table
203 seek(FILE, 0, SEEK_SET) or die "$0: $file: $!\n";
205 for ($i = 0; $i <= $opt{'hd0'}; $i++) {
206 $mbr = get_hex_data();
208 if ( length($mbr) > 432 ) {
209 die "$0: Bad MBR code\n";
212 $mbr .= "\0" x (432 - length($mbr));
214 $mbr .= pack("VV", $de_lba*4, 0); # Offset 432: LBA of isolinux.bin
215 $mbr .= $id; # Offset 440: MBR ID
216 $mbr .= "\0\0"; # Offset 446: actual partition table
218 # Print partition table
219 $offset = $opt{'offset'};
221 $bhead = int($offset/$s) % $h;
222 $bsect = ($offset % $s) + 1;
223 $bcyl = int($offset/($h*$s));
224 $bsect += ($bcyl & 0x300) >> 2;
227 $esect = $s + ((($cc-1) & 0x300) >> 2);
228 $ecyl = ($cc-1) & 0xff;
229 $fstype = $opt{'type'}; # Partition type
230 $pentry = $opt{'entry'}; # Partition slot
232 for ( $i = 1 ; $i <= 4 ; $i++ ) {
233 if ( $i == $pentry ) {
234 $mbr .= pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
235 $ehead, $esect, $ecyl, 0, $psize);
244 # Pad the image to a fake cylinder boundary
245 seek(FILE, $imgstat[7], SEEK_SET) or die "$0: $file: $!\n";
247 print FILE "\0" x $padding;