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.
24 use IO::Handle; # For flush()
26 # Use this fake geometry (zipdrive-style...)
30 # Get a 32-bit random number
34 if (open($rfd, "< /dev/urandom\0") && read($rfd, $rnd, 4) == 4) {
35 $rid = unpack("V", $rnd);
38 close($rfd) if (defined($rfd));
39 return $rid if (defined($rid));
41 # This sucks but is better than nothing...
42 return ($$+time()) & 0xffffffff;
47 open(FILE, "+< $file\0") or die "$0: cannot open $file: $!\n";
51 # First, actually figure out where mkisofs hid isolinux.bin
53 seek(FILE, 17*2048, SEEK_SET) or die "$0: $file: $!\n";
54 read(FILE, $boot_record, 2048) == 2048 or die "$0: $file: read error\n";
55 ($br_sign, $br_cat_offset) = unpack("a71V", $boot_record);
56 if ($br_sign ne ("\0CD001\1EL TORITO SPECIFICATION" . ("\0" x 41))) {
57 die "$0: $file: no boot record found\n";
59 seek(FILE, $br_cat_offset*2048, SEEK_SET) or die "$0: $file: $!\n";
60 read(FILE, $boot_cat, 2048) == 2048 or die "$0: $file: read error\n";
62 # We must have a Validation Entry followed by a Default Entry...
63 # no fanciness allowed for the Hybrid mode [XXX: might relax this later]
64 @ve = unpack("v16", $boot_cat);
66 for ($i = 0; $i < 16; $i++) {
69 if ($ve[0] != 0x0001 || $ve[15] != 0xaa55 || $cs & 0xffff) {
70 die "$0: $file: invalid boot catalog\n";
72 ($de_boot, $de_media, $de_seg, $de_sys, $de_mbz1, $de_count,
73 $de_lba, $de_mbz2) = unpack("CCvCCvVv", substr($boot_cat, 32, 32));
74 if ($de_boot != 0x88 || $de_media != 0 ||
75 ($de_segment != 0 && $de_segment != 0x7c0) || $de_count != 4) {
76 die "$0: $file: unexpected boot catalog parameters\n";
79 # Now $de_lba should contain the CD sector number for isolinux.bin
80 seek(FILE, $de_lba*2048+0x40, SEEK_SET) or die "$0: $file: $!\n";
81 read(FILE, $ibsig, 4);
82 if ($ibsig ne "\xfb\xc0\x78\x70") {
83 die "$0: $file: bootloader is missing isolinux.bin hybrid signature\n".
84 "Note: isolinux-debug.bin does not support hybrid booting\n";
87 # Get the total size of the image
88 (@imgstat = stat(FILE)) or die "$0: $file: $!\n";
89 $imgsize = $imgstat[7];
91 die "$0: $file: cannot determine length of file\n";
93 # Target image size: round up to a multiple of $h*$s*512
95 $frac = $imgsize % $cylsize;
96 $padding = ($frac > 0) ? $cylsize - $frac : 0;
98 $c = $imgsize/$cylsize;
100 print STDERR "Warning: more than 1024 cylinders ($c).\n";
101 print STDERR "Not all BIOSes will be able to boot this device.\n";
107 # Print the MBR and partition table
108 seek(FILE, 0, SEEK_SET) or die "$0: $file: $!\n";
111 while ( $line = <DATA> ) {
113 foreach $byte ( split(/\s+/, $line) ) {
114 $mbr .= chr(hex($byte));
117 if ( length($mbr) > 432 ) {
118 die "$0: Bad MBR code\n";
121 $mbr .= "\0" x (432 - length($mbr));
123 $mbr .= pack("VV", $de_lba*4, 0); # Offset 432: LBA of isolinux.bin
129 $mbr .= pack("V", $id); # Offset 440: MBR ID
130 $mbr .= "\0\0"; # Offset 446: actual partition table
132 # Print partition table
138 $esect = $s + ((($cc-1) & 0x300) >> 2);
139 $ecyl = ($cc-1) & 0xff;
140 $fstype = 0x83; # Linux (any better ideas?)
141 $pentry = 1; # First partition slot
143 for ( $i = 1 ; $i <= 4 ; $i++ ) {
144 if ( $i == $pentry ) {
145 $mbr .= pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
146 $ehead, $esect, $ecyl, 0, $psize);
155 # Pad the image to a fake cylinder boundary
156 seek(FILE, $imgstat[7], SEEK_SET) or die "$0: $file: $!\n";
158 print FILE "\0" x $padding;