• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/perl
2## -----------------------------------------------------------------------
3##
4##   Copyright 2002-2008 H. Peter Anvin - All Rights Reserved
5##   Copyright 2009-2010 Intel Corporation; author: H. Peter Anvin
6##
7##   This program is free software; you can redistribute it and/or modify
8##   it under the terms of the GNU General Public License as published by
9##   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
10##   Boston MA 02111-1307, USA; either version 2 of the License, or
11##   (at your option) any later version; incorporated herein by reference.
12##
13## -----------------------------------------------------------------------
14
15#
16# Creates a blank MS-DOS formatted hard disk image
17#
18
19use bytes;
20use integer;
21use Fcntl;
22use Errno;
23use Cwd;
24use IO::Handle;			# For flush()
25
26sub absolute_path($) {
27    my($f) = @_;
28    my($c);
29
30    return $f if ( $f =~ /^\// );
31    $c = cwd();
32    $c = '' if ( $c eq '/' );
33    return $c.'/'.$f;
34}
35
36sub is_linux() {
37    return !!eval '{ '.
38	'use POSIX; '.
39	'($sysname, $nodename, $release, $version, $machine) = POSIX::uname(); '.
40	"return \$sysname eq \'Linux\'; }";
41}
42
43sub get_random() {
44    # Get a 32-bit random number
45    my $rfd, $rnd;
46    my $rid;
47
48    if (open($rfd, "< /dev/urandom\0") && read($rfd, $rnd, 4) == 4) {
49	$rid = unpack("V", $rnd);
50    }
51
52    close($rfd) if (defined($rfd));
53    return $rid if (defined($rid));
54
55    # This sucks but is better than nothing...
56    return ($$+time()) & 0xffffffff;
57}
58
59sub get_hex_data() {
60    my $mbr = '';
61    my $line, $byte;
62    while ( $line = <DATA> ) {
63	chomp $line;
64	last if ($line eq '*');
65	foreach $byte ( split(/\s+/, $line) ) {
66	    $mbr .= chr(hex($byte));
67	}
68    }
69    return $mbr;
70}
71
72$is_linux = is_linux();
73if ( $is_linux ) {
74    # IOCTL numbers
75    $BLKRRPART    = 0x125f;
76    $BLKGETSIZE   = 0x1260;
77}
78
79%opt = ();
80@args = ();
81
82while (defined($a = shift(@ARGV))) {
83    if ( $a =~ /^\-/ ) {
84	foreach $o ( split(//, substr($a,1)) ) {
85	    $opt{$o} = 1;
86	    if ($o eq 'i') {
87		$id = shift(@ARGV);
88	    }
89	}
90    } else {
91	push(@args, $a);
92    }
93}
94
95($file,$c,$h,$s) = @args;
96$c += 0;  $h += 0;  $s += 0;
97
98$pentry = 1;
99$pentry = 2 if ( $opt{'2'} );
100$pentry = 3 if ( $opt{'3'} );
101$pentry = 4 if ( $opt{'4'} );
102
103if ( $opt{'z'} ) {
104    $h = $h || 64;
105    $s = $s || 32;
106}
107
108if ( $opt{'M'} && $h && $s ) {
109    # Specify size in megabytes, not in cylinders
110    $c = ($c*1024*2)/($h*$s);
111}
112
113$is_open = 0;
114
115if ( $c == 0 && $file ne '' ) {
116    $len = 0;
117    if ( sysopen(OUTPUT, $file, O_RDWR, 0666) ) {
118	$is_open = 1;
119
120	if ( (@filestat = stat(OUTPUT)) && S_ISREG($filestat[2]) ) {
121	    $len = $filestat[7] >> 9;
122	} elsif ( $is_linux && S_ISBLK($filestat[2]) ) {
123	    $blksize = pack("L!", 0);
124	    if ( ioctl(OUTPUT, $BLKGETSIZE, $blksize) == 0 ) {
125		$len = unpack("L!", $blksize); # In 512-byte sectors!
126	    }
127	}
128    }
129
130    if ( !$len ) {
131	print STDERR "$0: $file: don't know how to determine the size of this device\n";
132	exit 1;
133    }
134
135    $c = $len/($h*$s);
136}
137
138if ( $file eq '' || $c < 1 || $h < 1 || $h > 256 || $s < 1 || $s > 63 ) {
139    print STDERR "Usage: $0 [-doFMz4][-i id] file c h s (max: 1024 256 63)\n";
140    print STDERR "    -d    add DOSEMU header\n";
141    print STDERR "    -o    print filesystem offset to stdout\n";
142    print STDERR "    -F    format partition as FAT32\n";
143    print STDERR "    -M    \"c\" argument is megabytes, calculate cylinders\n";
144    print STDERR "    -z    use zipdisk geometry (h=64 s=32)\n";
145    print STDERR "    -4    use partition entry 4 (standard for zipdisks)\n";
146    print STDERR "    -i    specify the MBR ID\n";
147    print STDERR "    -s    output a sparse file (don't allocate all blocks)\n";
148    exit 1;
149}
150
151if ($c > 1024) {
152    print STDERR "Warning: more than 1024 cylinders ($c).\n";
153    print STDERR "Not all BIOSes will be able to boot this device.\n";
154    $cc = 1024;
155} else {
156    $cc = $c;
157}
158
159$cylsize = $h*$s*512;
160
161if ( !$is_open ) {
162    sysopen(OUTPUT, $file, O_CREAT|O_RDWR|O_TRUNC, 0666)
163	or die "$0: Cannot open: $file\n";
164}
165binmode OUTPUT;
166
167# Print out DOSEMU header, if requested
168if ( $opt{'d'} ) {
169    $emuhdr = "DOSEMU\0" . pack("VVVV", $h, $s, $c, 128);
170    $emuhdr .= "\0" x (128 - length($emuhdr));
171    print OUTPUT $emuhdr;
172}
173
174# Print the MBR and partition table
175$mbr = get_hex_data();
176if ( length($mbr) > 440 ) {
177    die "$0: Bad MBR code\n";
178}
179
180$mbr .= "\0" x (440 - length($mbr));
181if (defined($id)) {
182    $id = to_int($id);
183} else {
184    $id = get_random();
185}
186$mbr .= pack("V", $id);		# Offset 440: MBR ID
187$mbr .= "\0\0";			# Offset 446: actual partition table
188
189print OUTPUT $mbr;
190
191# Print partition table
192$psize = $c*$h*$s-$s;
193$bhead   = ($h > 1) ? 1 : 0;
194$bsect   = 1;
195$bcyl    = ($h > 1) ? 0 : 1;
196$ehead   = $h-1;
197$esect   = $s + ((($cc-1) & 0x300) >> 2);
198$ecyl    = ($cc-1) & 0xff;
199if ( $c > 1024 ) {
200    $fstype = 0x0e;
201} elsif ( $psize > 65536 ) {
202    $fstype = 0x06;
203} else {
204    $fstype = 0x04;
205}
206for ( $i = 1 ; $i <= 4 ; $i++ ) {
207    if ( $i == $pentry ) {
208	print OUTPUT pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
209			  $ehead, $esect, $ecyl, $s, $psize);
210    } else {
211	print OUTPUT "\0" x 16;
212    }
213}
214print OUTPUT "\x55\xaa";
215
216# Output blank file
217$totalsize = $c*$h*$s;
218$tracks    = $c*$h;
219
220# If -s is given, try to simply use truncate...
221unless ($opt{'s'} && truncate(OUTPUT, $totalsize)) {
222    $track = "\0" x (512*$s);
223
224    # Print fractional track
225    print OUTPUT "\0" x (512 * ($s-1));
226
227    for ( $i = 1 ; $i < $tracks ; $i++ ) {
228	print OUTPUT $track;
229    }
230}
231
232# Print mtools temp file
233$n = 0;
234while ( !defined($tmpdir) ) {
235    $tmpdir = "/tmp/mkdiskimage.$$.".($n++);
236    if ( !mkdir($tmpdir, 0700) ) {
237	die "$0: Failed to make temp directory: $tmpdir\n"
238	    if ( $! != EEXIST );
239	undef $tmpdir;
240    }
241}
242
243$cfgfile = $tmpdir.'/mtools.conf';
244$imglink = $tmpdir.'/disk.img';
245die "$0: Failed to create symlink $imglink\n"
246    if ( !symlink(absolute_path($file), $imglink) );
247
248$header_size = ($opt{'d'} ? 128 : 0);
249
250# Start of filesystem
251$offset = $s*512 + $header_size;
252
253# Start of partition table entry
254$pstart = $header_size + 446 + 16*($pentry-1);
255
256open(MCONFIG, "> ${cfgfile}") or die "$0: Cannot make mtools config\n";
257print MCONFIG "drive z:\n";
258print MCONFIG "file=\"${imglink}\"\n";
259print MCONFIG "cylinders=${c}\n";
260print MCONFIG "heads=${h}\n";
261print MCONFIG "sectors=${s}\n";
262print MCONFIG "offset=${offset}\n";
263print MCONFIG "mformat_only\n";
264close(MCONFIG);
265
266# Output the filesystem offset to stdout if appropriate
267if ( $opt{'o'} ) {
268    print $offset, "\n";
269}
270
271$ENV{'MTOOLSRC'} = $cfgfile;
272if ( $opt{'F'} ) {
273    system('mformat', '-F', 'z:');
274} else {
275    system('mformat', 'z:');
276}
277
278# Clean up in /tmp
279unlink($cfgfile);
280unlink($imglink);
281rmdir($tmpdir);
282
283# MTOOLS doesn't write the bsHiddenSecs field correctly
284seek(OUTPUT, $offset + 0x1c, 0);
285print OUTPUT pack("V", ($offset-$header_size)>>9);
286
287# Set the partition type
288if ( $opt{'F'} ) {
289    if ( $c > 1024 ) {
290	$fstype = 0x0c;		# FAT32 LBA
291    } else {
292	$fstype = 0x0b;
293    }
294} else {
295    if ( $c > 1024 ) {
296	$fstype = 0x0e;		# FAT16 LBA
297    } elsif ( $psize > 65536 ) {
298	$fstype = 0x06;		# FAT16 > 32MB
299    } else {
300	$fstype = 0x04;		# FAT16 <= 32MB
301    }
302    seek(OUTPUT, $offset + 0x36, 0);
303    read(OUTPUT, $fsname, 8);
304
305    # FAT12: adjust partition type
306    if ( $fsname eq 'FAT12   ' ) {
307	$fstype = 0x01;		# FAT12
308    }
309}
310seek(OUTPUT, $pstart+4, 0);
311print OUTPUT pack("C", $fstype);
312
313flush OUTPUT;
314
315# Just in case this is a block device, try to flush the partition table
316if ( $is_linux ) {
317    ioctl(OUTPUT, $BLKRRPART, 0);
318};
319
320exit 0;
321__END__
32233 c0 fa 8e d8 8e d0 bc 0 7c 89 e6 6 57 8e c0 fb fc bf 0 6 b9 0 1 f3 a5 ea
3231f 6 0 0 52 52 b4 41 bb aa 55 31 c9 30 f6 f9 cd 13 72 13 81 fb 55 aa 75 d
324d1 e9 73 9 66 c7 6 8d 6 b4 42 eb 15 5a b4 8 cd 13 83 e1 3f 51 f b6 c6 40 f7
325e1 52 50 66 31 c0 66 99 e8 66 0 e8 35 1 4d 69 73 73 69 6e 67 20 6f 70 65 72
32661 74 69 6e 67 20 73 79 73 74 65 6d 2e d a 66 60 66 31 d2 bb 0 7c 66 52 66
32750 6 53 6a 1 6a 10 89 e6 66 f7 36 f4 7b c0 e4 6 88 e1 88 c5 92 f6 36 f8 7b
32888 c6 8 e1 41 b8 1 2 8a 16 fa 7b cd 13 8d 64 10 66 61 c3 e8 c4 ff be be 7d
329bf 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
3303 40 89 de 83 c3 10 e2 f3 48 74 5b 79 39 59 5b 8a 47 4 3c f 74 6 24 7f 3c
3315 75 22 66 8b 47 8 66 8b 56 14 66 1 d0 66 21 d2 75 3 66 89 c2 e8 ac ff 72
3323 e8 b6 ff 66 8b 46 1c e8 a0 ff 83 c3 10 e2 cc 66 61 c3 e8 76 0 4d 75 6c 74
33369 70 6c 65 20 61 63 74 69 76 65 20 70 61 72 74 69 74 69 6f 6e 73 2e d a 66
3348b 44 8 66 3 46 1c 66 89 44 8 e8 30 ff 72 27 66 81 3e 0 7c 58 46 53 42 75
3359 66 83 c0 4 e8 1c ff 72 13 81 3e fe 7d 55 aa f 85 f2 fe bc fa 7b 5a 5f 7
336fa ff e4 e8 1e 0 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 6c 6f
33761 64 20 65 72 72 6f 72 2e d a 5e ac b4 e 8a 3e 62 4 b3 7 cd 10 3c a 75 f1
338cd 18 f4 eb fd 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
339*
340