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 $is_linux = is_linux
();
55 foreach $o ( split(//, substr($a,1)) ) {
63 ($file,$c,$h,$s) = @args;
64 $c += 0; $h += 0; $s += 0;
67 $pentry = 2 if ( $opt{'2'} );
68 $pentry = 3 if ( $opt{'3'} );
69 $pentry = 4 if ( $opt{'4'} );
76 if ( $opt{'M'} && $h && $s ) {
77 # Specify size in megabytes, not in cylinders
78 $c = ($c*1024*2)/($h*$s);
83 if ( $c == 0 && $file ne '' ) {
85 if ( sysopen(OUTPUT
, $file, O_RDWR
, 0666) ) {
88 if ( (@filestat = stat(OUTPUT
)) && S_ISREG
($filestat[2]) ) {
89 $len = $filestat[7] >> 9;
90 } elsif ( $is_linux && S_ISBLK
($filestat[2]) ) {
91 $blksize = pack("L!", 0);
92 if ( ioctl(OUTPUT
, $BLKGETSIZE, $blksize) == 0 ) {
93 $len = unpack("L!", $blksize); # In 512-byte sectors!
99 print STDERR
"$0: $file: don't know how to determine the size of this device\n";
106 if ( $file eq '' || $c < 1 || $c > 1024 ||
107 $h < 1 || $h > 256 || $s < 1 || $s > 63 ) {
108 print STDERR
"Usage: $0 [-doFMz4] file c h s (max: 1024 256 63)\n";
109 print STDERR
" -d add DOSEMU header\n";
110 print STDERR
" -o print filesystem offset to stdout\n";
111 print STDERR
" -F format partition as FAT32\n";
112 print STDERR
" -M \"c\" argument is megabytes, calculate cylinders\n";
113 print STDERR
" -z use zipdisk geometry (h=64 s=32)\n";
114 print STDERR
" -4 use partition entry 4 (standard for zipdisks)\n";
118 $cylsize = $h*$s*512;
121 sysopen(OUTPUT
, $file, O_CREAT
|O_RDWR
|O_TRUNC
, 0666)
122 or die "$0: Cannot open: $file\n";
126 # Print out DOSEMU header, if requested
128 $emuhdr = "DOSEMU\0" . pack("VVVV", $h, $s, $c, 128);
129 $emuhdr .= "\0" x
(128 - length($emuhdr));
130 print OUTPUT
$emuhdr;
133 # Print the MBR and partition table
135 while ( $line = <DATA
> ) {
137 foreach $byte ( split(/\s+/, $line) ) {
138 $mbr .= chr(hex($byte));
141 if ( length($mbr) > 446 ) {
142 die "$0: Bad MBR code\n";
145 $mbr .= "\0" x
(446 - length($mbr));
149 # Print partition table
150 $psize = $c*$h*$s-$s;
151 $bhead = ($h > 1) ?
1 : 0;
153 $bcyl = ($h > 1) ?
0 : 1;
155 $esect = $s + ((($c-1) & 0x300) >> 2);
156 $ecyl = ($c-1) & 0xff;
157 if ( $psize > 65536 ) {
162 for ( $i = 1 ; $i <= 4 ; $i++ ) {
163 if ( $i == $pentry ) {
164 print OUTPUT
pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
165 $ehead, $esect, $ecyl, $s, $psize);
167 print OUTPUT
"\0" x
16;
170 print OUTPUT
"\x55\xaa";
173 $totalsize = $c*$h*$s;
176 $track = "\0" x
(512*$s);
178 # Print fractional track
179 print OUTPUT
"\0" x
(512 * ($s-1));
181 for ( $i = 1 ; $i < $tracks ; $i++ ) {
185 # Print mtools temp file
187 while ( !defined($tmpdir) ) {
188 $tmpdir = "/tmp/mkdiskimage.$$.".($n++);
189 if ( !mkdir($tmpdir, 0700) ) {
190 die "$0: Failed to make temp directory: $tmpdir\n"
196 $cfgfile = $tmpdir.'/mtools.conf';
197 $imglink = $tmpdir.'/disk.img';
198 die "$0: Failed to create symlink $imglink\n"
199 if ( !symlink(absolute_path
($file), $imglink) );
201 $header_size = ($opt{'d'} ?
128 : 0);
203 # Start of filesystem
204 $offset = $s*512 + $header_size;
206 # Start of partition table entry
207 $pstart = $header_size + 446 + 16*($pentry-1);
209 open(MCONFIG
, "> ${cfgfile}") or die "$0: Cannot make mtools config\n";
210 print MCONFIG
"drive z:\n";
211 print MCONFIG
"file=\"${imglink}\"\n";
212 print MCONFIG
"cylinders=${c}\n";
213 print MCONFIG
"heads=${h}\n";
214 print MCONFIG
"sectors=${s}\n";
215 print MCONFIG
"offset=${offset}\n";
216 print MCONFIG
"mformat_only\n";
219 # Output the filesystem offset to stdout if appropriate
224 $ENV{'MTOOLSRC'} = $cfgfile;
226 system('mformat', '-F', 'z:');
228 system('mformat', 'z:');
236 # MTOOLS doesn't write the bsHiddenSecs field correctly
237 seek(OUTPUT
, $offset + 0x1c, 0);
238 print OUTPUT
pack("V", ($offset-$header_size)>>9);
240 # Set the partition type
242 $fstype = 0x0b; # FAT32
244 if ( $psize > 65536 ) {
245 $fstype = 0x06; # FAT16 > 32MB
247 $fstype = 0x04; # FAT16 <= 32MB
249 seek(OUTPUT
, $offset + 0x36, 0);
250 read(OUTPUT
, $fsname, 8);
252 # FAT12: adjust partition type
253 if ( $fsname eq 'FAT12 ' ) {
254 $fstype = 0x01; # FAT12
257 seek(OUTPUT
, $pstart+4, 0);
258 print OUTPUT
pack("C", $fstype);
262 # Just in case this is a block device, try to flush the partition table
264 ioctl(OUTPUT
, $BLKRRPART, 0);