Adding upstream version 3.53.
[syslinux-debian/hramrach.git] / mkdiskimage
blob1af992a7f1fb0d7b4fd7d4fab1081ada212fd85c
1 #!/usr/bin/perl
2 ## -----------------------------------------------------------------------
3 ##
4 ## Copyright 2002-2007 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.
12 ## -----------------------------------------------------------------------
15 # Creates a blank MS-DOS formatted hard disk image
18 use bytes;
19 use integer;
20 use Fcntl;
21 use Errno;
22 use Cwd;
23 use IO::Handle; # For flush()
25 sub absolute_path($) {
26 my($f) = @_;
27 my($c);
29 return $f if ( $f =~ /^\// );
30 $c = cwd();
31 $c = '' if ( $c eq '/' );
32 return $c.'/'.$f;
35 sub is_linux() {
36 return !!eval '{ '.
37 'use POSIX; '.
38 '($sysname, $nodename, $release, $version, $machine) = POSIX::uname(); '.
39 "return \$sysname eq \'Linux\'; }";
42 sub get_random() {
43 # Get a 32-bit random number
44 my $rfd, $rnd;
45 my $rid;
47 if (sysopen($rfd, '/dev/urandom', O_RDONLY) &&
48 sysread($rfd, $rnd, 4) == 4) {
49 $rid = unpack("V", $rnd);
52 close($rfd) if (defined($rfd));
53 return $rid if (defined($rid));
55 # This sucks but is better than nothing...
56 return ($$+time()) & 0xffffffff;
59 $is_linux = is_linux();
60 if ( $is_linux ) {
61 # IOCTL numbers
62 $BLKRRPART = 0x125f;
63 $BLKGETSIZE = 0x1260;
66 %opt = ();
67 @args = ();
69 while (defined($a = shift(@ARGV))) {
70 if ( $a =~ /^\-/ ) {
71 foreach $o ( split(//, substr($a,1)) ) {
72 $opt{$o} = 1;
73 if ($o eq 'i') {
74 $id = shift(@ARGV);
77 } else {
78 push(@args, $a);
82 ($file,$c,$h,$s) = @args;
83 $c += 0; $h += 0; $s += 0;
85 $pentry = 1;
86 $pentry = 2 if ( $opt{'2'} );
87 $pentry = 3 if ( $opt{'3'} );
88 $pentry = 4 if ( $opt{'4'} );
90 if ( $opt{'z'} ) {
91 $h = $h || 64;
92 $s = $s || 32;
95 if ( $opt{'M'} && $h && $s ) {
96 # Specify size in megabytes, not in cylinders
97 $c = ($c*1024*2)/($h*$s);
100 $is_open = 0;
102 if ( $c == 0 && $file ne '' ) {
103 $len = 0;
104 if ( sysopen(OUTPUT, $file, O_RDWR, 0666) ) {
105 $is_open = 1;
107 if ( (@filestat = stat(OUTPUT)) && S_ISREG($filestat[2]) ) {
108 $len = $filestat[7] >> 9;
109 } elsif ( $is_linux && S_ISBLK($filestat[2]) ) {
110 $blksize = pack("L!", 0);
111 if ( ioctl(OUTPUT, $BLKGETSIZE, $blksize) == 0 ) {
112 $len = unpack("L!", $blksize); # In 512-byte sectors!
117 if ( !$len ) {
118 print STDERR "$0: $file: don't know how to determine the size of this device\n";
119 exit 1;
122 $c = $len/($h*$s);
125 if ( $file eq '' || $c < 1 || $h < 1 || $h > 256 || $s < 1 || $s > 63 ) {
126 print STDERR "Usage: $0 [-doFMz4][-i id] file c h s (max: 1024 256 63)\n";
127 print STDERR " -d add DOSEMU header\n";
128 print STDERR " -o print filesystem offset to stdout\n";
129 print STDERR " -F format partition as FAT32\n";
130 print STDERR " -M \"c\" argument is megabytes, calculate cylinders\n";
131 print STDERR " -z use zipdisk geometry (h=64 s=32)\n";
132 print STDERR " -4 use partition entry 4 (standard for zipdisks)\n";
133 print STDERR " -i specify the MBR ID\n";
134 exit 1;
137 if ($c > 1024) {
138 print STDERR "Warning: more than 1024 cylinders ($c).\n";
139 print STDERR "Not all BIOSes will be able to boot this device.\n";
140 $cc = 1024;
141 } else {
142 $cc = $c;
145 $cylsize = $h*$s*512;
147 if ( !$is_open ) {
148 sysopen(OUTPUT, $file, O_CREAT|O_RDWR|O_TRUNC, 0666)
149 or die "$0: Cannot open: $file\n";
151 binmode OUTPUT;
153 # Print out DOSEMU header, if requested
154 if ( $opt{'d'} ) {
155 $emuhdr = "DOSEMU\0" . pack("VVVV", $h, $s, $c, 128);
156 $emuhdr .= "\0" x (128 - length($emuhdr));
157 print OUTPUT $emuhdr;
160 # Print the MBR and partition table
161 $mbr = '';
162 while ( $line = <DATA> ) {
163 chomp $line;
164 foreach $byte ( split(/\s+/, $line) ) {
165 $mbr .= chr(hex($byte));
168 if ( length($mbr) > 440 ) {
169 die "$0: Bad MBR code\n";
172 $mbr .= "\0" x (440 - length($mbr));
173 if (defined($id)) {
174 $id = to_int($id);
175 } else {
176 $id = get_random();
178 $mbr .= pack("V", $id); # Offset 440: MBR ID
179 $mbr .= "\0\0"; # Offset 446: actual partition table
181 print OUTPUT $mbr;
183 # Print partition table
184 $psize = $c*$h*$s-$s;
185 $bhead = ($h > 1) ? 1 : 0;
186 $bsect = 1;
187 $bcyl = ($h > 1) ? 0 : 1;
188 $ehead = $h-1;
189 $esect = $s + ((($cc-1) & 0x300) >> 2);
190 $ecyl = ($cc-1) & 0xff;
191 if ( $c > 1024 ) {
192 $fstype = 0x0e;
193 } elsif ( $psize > 65536 ) {
194 $fstype = 0x06;
195 } else {
196 $fstype = 0x04;
198 for ( $i = 1 ; $i <= 4 ; $i++ ) {
199 if ( $i == $pentry ) {
200 print OUTPUT pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
201 $ehead, $esect, $ecyl, $s, $psize);
202 } else {
203 print OUTPUT "\0" x 16;
206 print OUTPUT "\x55\xaa";
208 # Output blank file
209 $totalsize = $c*$h*$s;
210 $tracks = $c*$h;
212 $track = "\0" x (512*$s);
214 # Print fractional track
215 print OUTPUT "\0" x (512 * ($s-1));
217 for ( $i = 1 ; $i < $tracks ; $i++ ) {
218 print OUTPUT $track;
221 # Print mtools temp file
222 $n = 0;
223 while ( !defined($tmpdir) ) {
224 $tmpdir = "/tmp/mkdiskimage.$$.".($n++);
225 if ( !mkdir($tmpdir, 0700) ) {
226 die "$0: Failed to make temp directory: $tmpdir\n"
227 if ( $! != EEXIST );
228 undef $tmpdir;
232 $cfgfile = $tmpdir.'/mtools.conf';
233 $imglink = $tmpdir.'/disk.img';
234 die "$0: Failed to create symlink $imglink\n"
235 if ( !symlink(absolute_path($file), $imglink) );
237 $header_size = ($opt{'d'} ? 128 : 0);
239 # Start of filesystem
240 $offset = $s*512 + $header_size;
242 # Start of partition table entry
243 $pstart = $header_size + 446 + 16*($pentry-1);
245 open(MCONFIG, "> ${cfgfile}") or die "$0: Cannot make mtools config\n";
246 print MCONFIG "drive z:\n";
247 print MCONFIG "file=\"${imglink}\"\n";
248 print MCONFIG "cylinders=${c}\n";
249 print MCONFIG "heads=${h}\n";
250 print MCONFIG "sectors=${s}\n";
251 print MCONFIG "offset=${offset}\n";
252 print MCONFIG "mformat_only\n";
253 close(MCONFIG);
255 # Output the filesystem offset to stdout if appropriate
256 if ( $opt{'o'} ) {
257 print $offset, "\n";
260 $ENV{'MTOOLSRC'} = $cfgfile;
261 if ( $opt{'F'} ) {
262 system('mformat', '-F', 'z:');
263 } else {
264 system('mformat', 'z:');
267 # Clean up in /tmp
268 unlink($cfgfile);
269 unlink($imglink);
270 rmdir($tmpdir);
272 # MTOOLS doesn't write the bsHiddenSecs field correctly
273 seek(OUTPUT, $offset + 0x1c, 0);
274 print OUTPUT pack("V", ($offset-$header_size)>>9);
276 # Set the partition type
277 if ( $opt{'F'} ) {
278 if ( $c > 1024 ) {
279 $fstype = 0x0c; # FAT32 LBA
280 } else {
281 $fstype = 0x0b;
283 } else {
284 if ( $c > 1024 ) {
285 $fstype = 0x0e; # FAT16 LBA
286 } elsif ( $psize > 65536 ) {
287 $fstype = 0x06; # FAT16 > 32MB
288 } else {
289 $fstype = 0x04; # FAT16 <= 32MB
291 seek(OUTPUT, $offset + 0x36, 0);
292 read(OUTPUT, $fsname, 8);
294 # FAT12: adjust partition type
295 if ( $fsname eq 'FAT12 ' ) {
296 $fstype = 0x01; # FAT12
299 seek(OUTPUT, $pstart+4, 0);
300 print OUTPUT pack("C", $fstype);
302 flush OUTPUT;
304 # Just in case this is a block device, try to flush the partition table
305 if ( $is_linux ) {
306 ioctl(OUTPUT, $BLKRRPART, 0);
309 exit 0;
310 __END__
311 fa 31 c0 8e d8 8e d0 bc 0 7c 89 e6 6 57 52 8e c0 fb fc bf 0 6 b9 0 1 f3 a5 ea 20 6 0 0 52 b8 0 41 bb
312 aa 55 31 c9 30 f6 f9 cd 13 72 13 81 fb 55 aa 75 d d1 e9 73 9 66 c7 6 8f 6 b4 42 eb 15 5a b4 8 cd 13 83 e1
313 3f 51 f b6 c6 40 f7 e1 52 50 66 31 c0 66 99 e8 67 0 e8 24 1 4d 69 73 73 69 6e 67 20 6f 70 65 72 61 74 69 6e
314 67 20 73 79 73 74 65 6d 2e d a 0 66 60 66 31 d2 bb 0 7c 66 52 66 50 6 53 6a 1 6a 10 89 e6 66 f7 36 f4 7b
315 c0 e4 6 88 e1 88 c5 92 f6 36 f8 7b 88 c6 8 e1 41 b8 1 2 8a 16 fa 7b cd 13 83 c4 10 66 61 c3 e8 c4 ff be be
316 7d bf be 7 b9 20 0 f3 a5 c3 66 60 89 e5 bb be 7 b9 4 0 31 c0 53 51 f6 7 80 74 3 40 89 de 83 c3 10 e2 f3
317 48 74 5c 79 39 59 5b 8a 47 4 3c f 74 6 24 7f 3c 5 75 22 66 8b 47 8 66 8b 56 14 66 1 d0 66 21 d2 75 3 66
318 89 c2 e8 ac ff 72 3 e8 b6 ff 66 8b 46 1c e8 a0 ff 83 c3 10 e2 cc 66 61 c3 e8 64 0 4d 75 6c 74 69 70 6c 65 20
319 61 63 74 69 76 65 20 70 61 72 74 69 74 69 6f 6e 73 2e d a 0 66 8b 44 8 66 3 46 1c 66 89 44 8 e8 2f ff 72
320 13 81 3e fe 7d 55 aa f 85 4 ff bc fa 7b 5a 5f 7 fa ff e4 e8 1f 0 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74
321 65 6d 20 6c 6f 61 64 20 65 72 72 6f 72 2e d a 0 5e ac 20 c0 74 c b4 e 8a 3e 62 4 b3 7 cd 10 eb ef cd 18
322 f4 eb fd