isohybrid: verify we have a hybrid-compatible isolinux.bin
[profile/ivi/syslinux.git] / utils / isohybrid.in
1 #!/usr/bin/perl
2 ## -----------------------------------------------------------------------
3 ##
4 ##   Copyright 2002-2008 H. Peter Anvin - All Rights Reserved
5 ##
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.
11 ##
12 ## -----------------------------------------------------------------------
13
14 #
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.
17 #
18
19 use bytes;
20 use integer;
21 use Fcntl;
22 use Errno;
23 use Cwd;
24 use IO::Handle;                 # For flush()
25
26 # Use this fake geometry (zipdrive-style...)
27 $h = 64; $s = 32;
28
29 sub get_random() {
30     # Get a 32-bit random number
31     my $rfd, $rnd;
32     my $rid;
33
34     if (open($rfd, "< /dev/urandom\0") && read($rfd, $rnd, 4) == 4) {
35         $rid = unpack("V", $rnd);
36     }
37
38     close($rfd) if (defined($rfd));
39     return $rid if (defined($rid));
40
41     # This sucks but is better than nothing...
42     return ($$+time()) & 0xffffffff;
43 }
44
45
46 ($file) = @ARGV;
47 open(FILE, "+< $file\0") or die "$0: cannot open $file: $!\n";
48 binmode FILE;
49
50 #
51 # First, actually figure out where mkisofs hid isolinux.bin
52 #
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";
58 }
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";
61
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);
65 $cs = 0;
66 for ($i = 0; $i < 16; $i++) {
67     $cs += $ve[$i];
68 }
69 if ($ve[0] != 0x0001 || $ve[15] != 0xaa55 || $cs & 0xffff) {
70     die "$0: $file: invalid boot catalog\n";
71 }
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";
77 }
78
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";
85 }
86
87 # Get the total size of the image
88 (@imgstat = stat(FILE)) or die "$0: $file: $!\n";
89 $imgsize = $imgstat[7];
90 if (!$imgsize) {
91     die "$0: $file: cannot determine length of file\n";
92 }
93 # Target image size: round up to a multiple of $h*$s*512
94 $cylsize = $h*$s*512;
95 $frac = $imgsize % $cylsize;
96 $padding = ($frac > 0) ? $cylsize - $frac : 0;
97 $imgsize += $padding;
98 $c = $imgsize/$cylsize;
99 if ($c > 1024) {
100     print STDERR "Warning: more than 1024 cylinders ($c).\n";
101     print STDERR "Not all BIOSes will be able to boot this device.\n";
102     $cc = 1024;
103 } else {
104     $cc = $c;
105 }
106
107 # Print the MBR and partition table
108 seek(FILE, 0, SEEK_SET) or die "$0: $file: $!\n";
109
110 $mbr = '';
111 while ( $line = <DATA> ) {
112     chomp $line;
113     foreach $byte ( split(/\s+/, $line) ) {
114         $mbr .= chr(hex($byte));
115     }
116 }
117 if ( length($mbr) > 432 ) {
118     die "$0: Bad MBR code\n";
119 }
120
121 $mbr .= "\0" x (432 - length($mbr));
122
123 $mbr .= pack("VV", $de_lba*4, 0); # Offset 432: LBA of isolinux.bin
124 if (defined($id)) {
125     $id = to_int($id);
126 } else {
127     $id = get_random();
128 }
129 $mbr .= pack("V", $id);         # Offset 440: MBR ID
130 $mbr .= "\0\0";                 # Offset 446: actual partition table
131
132 # Print partition table
133 $psize   = $c*$h*$s;
134 $bhead   = 0;
135 $bsect   = 1;
136 $bcyl    = 0;
137 $ehead   = $h-1;
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
142
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);
147     } else {
148         $mbr .= "\0" x 16;
149     }
150 }
151 $mbr .= "\x55\xaa";
152
153 print FILE $mbr;
154
155 # Pad the image to a fake cylinder boundary
156 seek(FILE, $imgstat[7], SEEK_SET) or die "$0: $file: $!\n";
157 if ($padding) {
158     print FILE "\0" x $padding;
159 }
160
161 # Done...
162 close(FILE);
163
164 exit 0;
165 __END__