2 ## -----------------------------------------------------------------------
4 ## Copyright 2002-2007 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 # Creates a blank MS-DOS formatted hard disk image
23 use IO
::Handle
; # For flush()
25 sub absolute_path
($) {
29 return $f if ( $f =~ /^\// );
31 $c = '' if ( $c eq '/' );
38 '($sysname, $nodename, $release, $version, $machine) = POSIX::uname(); '.
39 "return \$sysname eq \'Linux\'; }";
43 # Get a 32-bit random number
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
();
69 while (defined($a = shift(@ARGV))) {
71 foreach $o ( split(//, substr($a,1)) ) {
82 ($file,$c,$h,$s) = @args;
83 $c += 0; $h += 0; $s += 0;
86 $pentry = 2 if ( $opt{'2'} );
87 $pentry = 3 if ( $opt{'3'} );
88 $pentry = 4 if ( $opt{'4'} );
95 if ( $opt{'M'} && $h && $s ) {
96 # Specify size in megabytes, not in cylinders
97 $c = ($c*1024*2)/($h*$s);
102 if ( $c == 0 && $file ne '' ) {
104 if ( sysopen(OUTPUT
, $file, O_RDWR
, 0666) ) {
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!
118 print STDERR
"$0: $file: don't know how to determine the size of this device\n";
125 if ( $file eq '' || $c < 1 || $c > 1024 ||
126 $h < 1 || $h > 256 || $s < 1 || $s > 63 ) {
127 print STDERR
"Usage: $0 [-doFMz4][-i id] file c h s (max: 1024 256 63)\n";
128 print STDERR
" -d add DOSEMU header\n";
129 print STDERR
" -o print filesystem offset to stdout\n";
130 print STDERR
" -F format partition as FAT32\n";
131 print STDERR
" -M \"c\" argument is megabytes, calculate cylinders\n";
132 print STDERR
" -z use zipdisk geometry (h=64 s=32)\n";
133 print STDERR
" -4 use partition entry 4 (standard for zipdisks)\n";
134 print STDERR
" -i specify the MBR ID\n";
138 $cylsize = $h*$s*512;
141 sysopen(OUTPUT
, $file, O_CREAT
|O_RDWR
|O_TRUNC
, 0666)
142 or die "$0: Cannot open: $file\n";
146 # Print out DOSEMU header, if requested
148 $emuhdr = "DOSEMU\0" . pack("VVVV", $h, $s, $c, 128);
149 $emuhdr .= "\0" x
(128 - length($emuhdr));
150 print OUTPUT
$emuhdr;
153 # Print the MBR and partition table
155 while ( $line = <DATA
> ) {
157 foreach $byte ( split(/\s+/, $line) ) {
158 $mbr .= chr(hex($byte));
161 if ( length($mbr) > 440 ) {
162 die "$0: Bad MBR code\n";
165 $mbr .= "\0" x
(440 - length($mbr));
171 $mbr .= pack("V", $id); # Offset 440: MBR ID
172 $mbr .= "\0\0"; # Offset 446: actual partition table
176 # Print partition table
177 $psize = $c*$h*$s-$s;
178 $bhead = ($h > 1) ?
1 : 0;
180 $bcyl = ($h > 1) ?
0 : 1;
182 $esect = $s + ((($c-1) & 0x300) >> 2);
183 $ecyl = ($c-1) & 0xff;
184 if ( $psize > 65536 ) {
189 for ( $i = 1 ; $i <= 4 ; $i++ ) {
190 if ( $i == $pentry ) {
191 print OUTPUT
pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
192 $ehead, $esect, $ecyl, $s, $psize);
194 print OUTPUT
"\0" x
16;
197 print OUTPUT
"\x55\xaa";
200 $totalsize = $c*$h*$s;
203 $track = "\0" x
(512*$s);
205 # Print fractional track
206 print OUTPUT
"\0" x
(512 * ($s-1));
208 for ( $i = 1 ; $i < $tracks ; $i++ ) {
212 # Print mtools temp file
214 while ( !defined($tmpdir) ) {
215 $tmpdir = "/tmp/mkdiskimage.$$.".($n++);
216 if ( !mkdir($tmpdir, 0700) ) {
217 die "$0: Failed to make temp directory: $tmpdir\n"
223 $cfgfile = $tmpdir.'/mtools.conf';
224 $imglink = $tmpdir.'/disk.img';
225 die "$0: Failed to create symlink $imglink\n"
226 if ( !symlink(absolute_path
($file), $imglink) );
228 $header_size = ($opt{'d'} ?
128 : 0);
230 # Start of filesystem
231 $offset = $s*512 + $header_size;
233 # Start of partition table entry
234 $pstart = $header_size + 446 + 16*($pentry-1);
236 open(MCONFIG
, "> ${cfgfile}") or die "$0: Cannot make mtools config\n";
237 print MCONFIG
"drive z:\n";
238 print MCONFIG
"file=\"${imglink}\"\n";
239 print MCONFIG
"cylinders=${c}\n";
240 print MCONFIG
"heads=${h}\n";
241 print MCONFIG
"sectors=${s}\n";
242 print MCONFIG
"offset=${offset}\n";
243 print MCONFIG
"mformat_only\n";
246 # Output the filesystem offset to stdout if appropriate
251 $ENV{'MTOOLSRC'} = $cfgfile;
253 system('mformat', '-F', 'z:');
255 system('mformat', 'z:');
263 # MTOOLS doesn't write the bsHiddenSecs field correctly
264 seek(OUTPUT
, $offset + 0x1c, 0);
265 print OUTPUT
pack("V", ($offset-$header_size)>>9);
267 # Set the partition type
269 $fstype = 0x0b; # FAT32
271 if ( $psize > 65536 ) {
272 $fstype = 0x06; # FAT16 > 32MB
274 $fstype = 0x04; # FAT16 <= 32MB
276 seek(OUTPUT
, $offset + 0x36, 0);
277 read(OUTPUT
, $fsname, 8);
279 # FAT12: adjust partition type
280 if ( $fsname eq 'FAT12 ' ) {
281 $fstype = 0x01; # FAT12
284 seek(OUTPUT
, $pstart+4, 0);
285 print OUTPUT
pack("C", $fstype);
289 # Just in case this is a block device, try to flush the partition table
291 ioctl(OUTPUT
, $BLKRRPART, 0);