2 ## -----------------------------------------------------------------------
4 ## Copyright 2002-2008 H. Peter Anvin - All Rights Reserved
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 ## Boston MA 02111-1307, USA; either version 2 of the License, or
10 ## (at your option) any later version; incorporated herein by reference.
12 ## -----------------------------------------------------------------------
15 # Post-process an ISO 9660 image generated with mkisofs/genisoimage
16 # to allow "hybrid booting" as a CD-ROM or as a hard disk.
22 # Use this fake geometry (zipdrive-style...)
26 # Get a 32-bit random number
30 if (open($rfd, "< /dev/urandom\0") && read($rfd, $rnd, 4) == 4) {
31 $rid = unpack("V", $rnd);
34 close($rfd) if (defined($rfd));
35 return $rid if (defined($rid));
37 # This sucks but is better than nothing...
38 return ($$+time()) & 0xffffffff;
43 open(FILE, "+< $file\0") or die "$0: cannot open $file: $!\n";
47 # First, actually figure out where mkisofs hid isolinux.bin
49 seek(FILE, 17*2048, SEEK_SET) or die "$0: $file: $!\n";
50 read(FILE, $boot_record, 2048) == 2048 or die "$0: $file: read error\n";
51 ($br_sign, $br_cat_offset) = unpack("a71V", $boot_record);
52 if ($br_sign ne ("\0CD001\1EL TORITO SPECIFICATION" . ("\0" x 41))) {
53 die "$0: $file: no boot record found\n";
55 seek(FILE, $br_cat_offset*2048, SEEK_SET) or die "$0: $file: $!\n";
56 read(FILE, $boot_cat, 2048) == 2048 or die "$0: $file: read error\n";
58 # We must have a Validation Entry followed by a Default Entry...
59 # no fanciness allowed for the Hybrid mode [XXX: might relax this later]
60 @ve = unpack("v16", $boot_cat);
62 for ($i = 0; $i < 16; $i++) {
65 if ($ve[0] != 0x0001 || $ve[15] != 0xaa55 || $cs & 0xffff) {
66 die "$0: $file: invalid boot catalog\n";
68 ($de_boot, $de_media, $de_seg, $de_sys, $de_mbz1, $de_count,
69 $de_lba, $de_mbz2) = unpack("CCvCCvVv", substr($boot_cat, 32, 32));
70 if ($de_boot != 0x88 || $de_media != 0 ||
71 ($de_segment != 0 && $de_segment != 0x7c0) || $de_count != 4) {
72 die "$0: $file: unexpected boot catalog parameters\n";
75 # Now $de_lba should contain the CD sector number for isolinux.bin
76 seek(FILE, $de_lba*2048+0x40, SEEK_SET) or die "$0: $file: $!\n";
77 read(FILE, $ibsig, 4);
78 if ($ibsig ne "\xfb\xc0\x78\x70") {
79 die "$0: $file: bootloader is missing isolinux.bin hybrid signature\n".
80 "Note: isolinux-debug.bin does not support hybrid booting\n";
83 # Get the total size of the image
84 (@imgstat = stat(FILE)) or die "$0: $file: $!\n";
85 $imgsize = $imgstat[7];
87 die "$0: $file: cannot determine length of file\n";
89 # Target image size: round up to a multiple of $h*$s*512
91 $frac = $imgsize % $cylsize;
92 $padding = ($frac > 0) ? $cylsize - $frac : 0;
94 $c = int($imgsize/$cylsize);
96 print STDERR "Warning: more than 1024 cylinders ($c).\n";
97 print STDERR "Not all BIOSes will be able to boot this device.\n";
103 # Print the MBR and partition table
104 seek(FILE, 0, SEEK_SET) or die "$0: $file: $!\n";
107 while ( $line = <DATA> ) {
109 foreach $byte ( split(/\s+/, $line) ) {
110 $mbr .= chr(hex($byte));
113 if ( length($mbr) > 432 ) {
114 die "$0: Bad MBR code\n";
117 $mbr .= "\0" x (432 - length($mbr));
119 $mbr .= pack("VV", $de_lba*4, 0); # Offset 432: LBA of isolinux.bin
125 $mbr .= pack("V", $id); # Offset 440: MBR ID
126 $mbr .= "\0\0"; # Offset 446: actual partition table
128 # Print partition table
134 $esect = $s + ((($cc-1) & 0x300) >> 2);
135 $ecyl = ($cc-1) & 0xff;
136 $fstype = 0x83; # Linux (any better ideas?)
137 $pentry = 1; # First partition slot
139 for ( $i = 1 ; $i <= 4 ; $i++ ) {
140 if ( $i == $pentry ) {
141 $mbr .= pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
142 $ehead, $esect, $ecyl, 0, $psize);
151 # Pad the image to a fake cylinder boundary
152 seek(FILE, $imgstat[7], SEEK_SET) or die "$0: $file: $!\n";
154 print FILE "\0" x $padding;