2 ## -----------------------------------------------------------------------
4 ## Copyright 2002-2004 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'} );
72 # Specify size in megabytes, not in cylinders
73 $c = ($c*1024*2)/($h*$s);
78 if ( $c == 0 && $file ne '' ) {
80 if ( sysopen(OUTPUT
, $file, O_RDWR
, 0666) ) {
83 if ( (@filestat = stat(OUTPUT
)) && S_ISREG
($filestat[2]) ) {
84 $len = $filestat[7] >> 9;
85 } elsif ( $is_linux && S_ISBLK
($filestat[2]) ) {
86 $blksize = pack("L!", 0);
87 if ( ioctl(OUTPUT
, $BLKGETSIZE, $blksize) == 0 ) {
88 $len = unpack("L!", $blksize); # In 512-byte sectors!
94 print STDERR
"$0: $file: don't know how to determine the size of this device\n";
101 if ( $file eq '' || $c < 1 || $c > 1024 ||
102 $h < 1 || $h > 256 || $s < 1 || $s > 63 ) {
103 print STDERR
"Usage: $0 [-doF4] file [c h s] (max: 1024 256 63)\n";
104 print STDERR
" -d add DOSEMU header\n";
105 print STDERR
" -o print filesystem offset to stdout\n";
106 print STDERR
" -F format partition as FAT32\n";
107 print STDERR
" -M \"c\" argument is megabytes, calculate cylinders\n";
108 print STDERR
" -4 use partition entry 4 (standard for zipdisks)\n";
112 $cylsize = $h*$s*512;
115 sysopen(OUTPUT
, $file, O_CREAT
|O_RDWR
|O_TRUNC
, 0666)
116 or die "$0: Cannot open: $file\n";
120 # Print out DOSEMU header, if requested
122 $emuhdr = "DOSEMU\0" . pack("VVVV", $h, $s, $c, 128);
123 $emuhdr .= "\0" x
(128 - length($emuhdr));
124 print OUTPUT
$emuhdr;
127 # Print the MBR and partition table
129 while ( $line = <DATA
> ) {
131 foreach $byte ( split(/\s+/, $line) ) {
132 $mbr .= chr(hex($byte));
135 if ( length($mbr) > 446 ) {
136 die "$0: Bad MBR code\n";
139 $mbr .= "\0" x
(446 - length($mbr));
143 # Print partition table
144 $psize = $c*$h*$s-$s;
145 $bhead = ($h > 1) ?
1 : 0;
147 $bcyl = ($h > 1) ?
0 : 1;
149 $esect = $s + ((($c-1) & 0x300) >> 2);
150 $ecyl = ($c-1) & 0xff;
151 if ( $psize > 65536 ) {
156 for ( $i = 1 ; $i <= 4 ; $i++ ) {
157 if ( $i == $pentry ) {
158 print OUTPUT
pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
159 $ehead, $esect, $ecyl, $s, $psize);
161 print OUTPUT
"\0" x
16;
164 print OUTPUT
"\x55\xaa";
167 $totalsize = $c*$h*$s;
170 $track = "\0" x
(512*$s);
172 # Print fractional track
173 print OUTPUT
"\0" x
(512 * ($s-1));
175 for ( $i = 1 ; $i < $tracks ; $i++ ) {
179 # Print mtools temp file
181 while ( !defined($tmpdir) ) {
182 $tmpdir = "/tmp/mkdiskimage.$$.".($n++);
183 if ( !mkdir($tmpdir, 0700) ) {
184 die "$0: Failed to make temp directory: $tmpdir\n"
190 $cfgfile = $tmpdir.'/mtools.conf';
191 $imglink = $tmpdir.'/disk.img';
192 die "$0: Failed to create symlink $imglink\n"
193 if ( !symlink(absolute_path
($file), $imglink) );
195 $header_size = ($opt{'d'} ?
128 : 0);
197 # Start of filesystem
198 $offset = $s*512 + $header_size;
200 # Start of partition table entry
201 $pstart = $header_size + 446 + 16*($pentry-1);
203 open(MCONFIG
, "> ${cfgfile}") or die "$0: Cannot make mtools config\n";
204 print MCONFIG
"drive z:\n";
205 print MCONFIG
"file=\"${imglink}\"\n";
206 print MCONFIG
"cylinders=${c}\n";
207 print MCONFIG
"heads=${h}\n";
208 print MCONFIG
"sectors=${s}\n";
209 print MCONFIG
"offset=${offset}\n";
210 print MCONFIG
"mformat_only\n";
213 # Output the filesystem offset to stdout if appropriate
218 $ENV{'MTOOLSRC'} = $cfgfile;
220 system('mformat', '-F', 'z:');
222 system('mformat', 'z:');
230 # MTOOLS doesn't write the bsHiddenSecs field correctly
231 seek(OUTPUT
, $offset + 0x1c, 0);
232 print OUTPUT
pack("V", ($offset-$header_size)>>9);
234 # Set the partition type
236 $fstype = 0x0b; # FAT32
238 if ( $psize > 65536 ) {
239 $fstype = 0x06; # FAT16 > 32MB
241 $fstype = 0x04; # FAT16 <= 32MB
243 seek(OUTPUT
, $offset + 0x36, 0);
244 read(OUTPUT
, $fsname, 8);
246 # FAT12: adjust partition type
247 if ( $fsname eq 'FAT12 ' ) {
248 $fstype = 0x01; # FAT12
251 seek(OUTPUT
, $pstart+4, 0);
252 print OUTPUT
pack("C", $fstype);
256 # Just in case this is a block device, try to flush the partition table
258 ioctl(OUTPUT
, $BLKRRPART, 0);