Merge commit 'stefanha/gdbstub'
[profile/ivi/syslinux.git] / utils / mkdiskimage.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 # Creates a blank MS-DOS formatted hard disk image
16 #
17
18 use bytes;
19 use integer;
20 use Fcntl;
21 use Errno;
22 use Cwd;
23 use IO::Handle;                 # For flush()
24
25 sub absolute_path($) {
26     my($f) = @_;
27     my($c);
28
29     return $f if ( $f =~ /^\// );
30     $c = cwd();
31     $c = '' if ( $c eq '/' );
32     return $c.'/'.$f;
33 }
34
35 sub is_linux() {
36     return !!eval '{ '.
37         'use POSIX; '.
38         '($sysname, $nodename, $release, $version, $machine) = POSIX::uname(); '.
39         "return \$sysname eq \'Linux\'; }";
40 }
41
42 sub get_random() {
43     # Get a 32-bit random number
44     my $rfd, $rnd;
45     my $rid;
46
47     if (open($rfd, "< /dev/urandom\0") && read($rfd, $rnd, 4) == 4) {
48         $rid = unpack("V", $rnd);
49     }
50
51     close($rfd) if (defined($rfd));
52     return $rid if (defined($rid));
53
54     # This sucks but is better than nothing...
55     return ($$+time()) & 0xffffffff;
56 }
57
58 $is_linux = is_linux();
59 if ( $is_linux ) {
60     # IOCTL numbers
61     $BLKRRPART    = 0x125f;
62     $BLKGETSIZE   = 0x1260;
63 }
64
65 %opt = ();
66 @args = ();
67
68 while (defined($a = shift(@ARGV))) {
69     if ( $a =~ /^\-/ ) {
70         foreach $o ( split(//, substr($a,1)) ) {
71             $opt{$o} = 1;
72             if ($o eq 'i') {
73                 $id = shift(@ARGV);
74             }
75         }
76     } else {
77         push(@args, $a);
78     }
79 }
80
81 ($file,$c,$h,$s) = @args;
82 $c += 0;  $h += 0;  $s += 0;
83
84 $pentry = 1;
85 $pentry = 2 if ( $opt{'2'} );
86 $pentry = 3 if ( $opt{'3'} );
87 $pentry = 4 if ( $opt{'4'} );
88
89 if ( $opt{'z'} ) {
90     $h = $h || 64;
91     $s = $s || 32;
92 }
93
94 if ( $opt{'M'} && $h && $s ) {
95     # Specify size in megabytes, not in cylinders
96     $c = ($c*1024*2)/($h*$s);
97 }
98
99 $is_open = 0;
100
101 if ( $c == 0 && $file ne '' ) {
102     $len = 0;
103     if ( sysopen(OUTPUT, $file, O_RDWR, 0666) ) {
104         $is_open = 1;
105
106         if ( (@filestat = stat(OUTPUT)) && S_ISREG($filestat[2]) ) {
107             $len = $filestat[7] >> 9;
108         } elsif ( $is_linux && S_ISBLK($filestat[2]) ) {
109             $blksize = pack("L!", 0);
110             if ( ioctl(OUTPUT, $BLKGETSIZE, $blksize) == 0 ) {
111                 $len = unpack("L!", $blksize); # In 512-byte sectors!
112             }
113         }
114     }
115
116     if ( !$len ) {
117         print STDERR "$0: $file: don't know how to determine the size of this device\n";
118         exit 1;
119     }
120
121     $c = $len/($h*$s);
122 }
123
124 if ( $file eq '' || $c < 1 || $h < 1 || $h > 256 || $s < 1 || $s > 63 ) {
125     print STDERR "Usage: $0 [-doFMz4][-i id] file c h s (max: 1024 256 63)\n";
126     print STDERR "    -d    add DOSEMU header\n";
127     print STDERR "    -o    print filesystem offset to stdout\n";
128     print STDERR "    -F    format partition as FAT32\n";
129     print STDERR "    -M    \"c\" argument is megabytes, calculate cylinders\n";
130     print STDERR "    -z    use zipdisk geometry (h=64 s=32)\n";
131     print STDERR "    -4    use partition entry 4 (standard for zipdisks)\n";
132     print STDERR "    -i    specify the MBR ID\n";
133     exit 1;
134 }
135
136 if ($c > 1024) {
137     print STDERR "Warning: more than 1024 cylinders ($c).\n";
138     print STDERR "Not all BIOSes will be able to boot this device.\n";
139     $cc = 1024;
140 } else {
141     $cc = $c;
142 }
143
144 $cylsize = $h*$s*512;
145
146 if ( !$is_open ) {
147     sysopen(OUTPUT, $file, O_CREAT|O_RDWR|O_TRUNC, 0666)
148         or die "$0: Cannot open: $file\n";
149 }
150 binmode OUTPUT;
151
152 # Print out DOSEMU header, if requested
153 if ( $opt{'d'} ) {
154     $emuhdr = "DOSEMU\0" . pack("VVVV", $h, $s, $c, 128);
155     $emuhdr .= "\0" x (128 - length($emuhdr));
156     print OUTPUT $emuhdr;
157 }
158
159 # Print the MBR and partition table
160 $mbr = '';
161 while ( $line = <DATA> ) {
162     chomp $line;
163     foreach $byte ( split(/\s+/, $line) ) {
164         $mbr .= chr(hex($byte));
165     }
166 }
167 if ( length($mbr) > 440 ) {
168     die "$0: Bad MBR code\n";
169 }
170
171 $mbr .= "\0" x (440 - length($mbr));
172 if (defined($id)) {
173     $id = to_int($id);
174 } else {
175     $id = get_random();
176 }
177 $mbr .= pack("V", $id);         # Offset 440: MBR ID
178 $mbr .= "\0\0";                 # Offset 446: actual partition table
179
180 print OUTPUT $mbr;
181
182 # Print partition table
183 $psize = $c*$h*$s-$s;
184 $bhead   = ($h > 1) ? 1 : 0;
185 $bsect   = 1;
186 $bcyl    = ($h > 1) ? 0 : 1;
187 $ehead   = $h-1;
188 $esect   = $s + ((($cc-1) & 0x300) >> 2);
189 $ecyl    = ($cc-1) & 0xff;
190 if ( $c > 1024 ) {
191     $fstype = 0x0e;
192 } elsif ( $psize > 65536 ) {
193     $fstype = 0x06;
194 } else {
195     $fstype = 0x04;
196 }
197 for ( $i = 1 ; $i <= 4 ; $i++ ) {
198     if ( $i == $pentry ) {
199         print OUTPUT pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
200                           $ehead, $esect, $ecyl, $s, $psize);
201     } else {
202         print OUTPUT "\0" x 16;
203     }
204 }
205 print OUTPUT "\x55\xaa";
206
207 # Output blank file
208 $totalsize = $c*$h*$s;
209 $tracks    = $c*$h;
210
211 $track = "\0" x (512*$s);
212
213 # Print fractional track
214 print OUTPUT "\0" x (512 * ($s-1));
215
216 for ( $i = 1 ; $i < $tracks ; $i++ ) {
217     print OUTPUT $track;
218 }
219
220 # Print mtools temp file
221 $n = 0;
222 while ( !defined($tmpdir) ) {
223     $tmpdir = "/tmp/mkdiskimage.$$.".($n++);
224     if ( !mkdir($tmpdir, 0700) ) {
225         die "$0: Failed to make temp directory: $tmpdir\n"
226             if ( $! != EEXIST );
227         undef $tmpdir;
228     }
229 }
230
231 $cfgfile = $tmpdir.'/mtools.conf';
232 $imglink = $tmpdir.'/disk.img';
233 die "$0: Failed to create symlink $imglink\n"
234     if ( !symlink(absolute_path($file), $imglink) );
235
236 $header_size = ($opt{'d'} ? 128 : 0);
237
238 # Start of filesystem
239 $offset = $s*512 + $header_size;
240
241 # Start of partition table entry
242 $pstart = $header_size + 446 + 16*($pentry-1);
243
244 open(MCONFIG, "> ${cfgfile}") or die "$0: Cannot make mtools config\n";
245 print MCONFIG "drive z:\n";
246 print MCONFIG "file=\"${imglink}\"\n";
247 print MCONFIG "cylinders=${c}\n";
248 print MCONFIG "heads=${h}\n";
249 print MCONFIG "sectors=${s}\n";
250 print MCONFIG "offset=${offset}\n";
251 print MCONFIG "mformat_only\n";
252 close(MCONFIG);
253
254 # Output the filesystem offset to stdout if appropriate
255 if ( $opt{'o'} ) {
256     print $offset, "\n";
257 }
258
259 $ENV{'MTOOLSRC'} = $cfgfile;
260 if ( $opt{'F'} ) {
261     system('mformat', '-F', 'z:');
262 } else {
263     system('mformat', 'z:');
264 }
265
266 # Clean up in /tmp
267 unlink($cfgfile);
268 unlink($imglink);
269 rmdir($tmpdir);
270
271 # MTOOLS doesn't write the bsHiddenSecs field correctly
272 seek(OUTPUT, $offset + 0x1c, 0);
273 print OUTPUT pack("V", ($offset-$header_size)>>9);
274
275 # Set the partition type
276 if ( $opt{'F'} ) {
277     if ( $c > 1024 ) {
278         $fstype = 0x0c;         # FAT32 LBA
279     } else {
280         $fstype = 0x0b;
281     }
282 } else {
283     if ( $c > 1024 ) {
284         $fstype = 0x0e;         # FAT16 LBA
285     } elsif ( $psize > 65536 ) {
286         $fstype = 0x06;         # FAT16 > 32MB
287     } else {
288         $fstype = 0x04;         # FAT16 <= 32MB
289     }
290     seek(OUTPUT, $offset + 0x36, 0);
291     read(OUTPUT, $fsname, 8);
292
293     # FAT12: adjust partition type
294     if ( $fsname eq 'FAT12   ' ) {
295         $fstype = 0x01;         # FAT12
296     }
297 }
298 seek(OUTPUT, $pstart+4, 0);
299 print OUTPUT pack("C", $fstype);
300
301 flush OUTPUT;
302
303 # Just in case this is a block device, try to flush the partition table
304 if ( $is_linux ) {
305     ioctl(OUTPUT, $BLKRRPART, 0);
306 };
307
308 exit 0;
309 __END__