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);
263 fa
31 c0
8e d8
8e c0
8e d0 bc
0 7c fb fc
89 e6 bf
0 6 b9
0 1 f3 a5 ea
1d
6 0 0 88 16 0 8 b4
8 cd
13
264 31 c0
88 f0
40 a3 f0
6 80 e1
3f
88 e f2
6 be be
7 31 c0 b9
4 0 f6
4 80 74 3 40 89 f7
83 c6
10 e2 f3
83
265 f8
1 75 73 8a
16 0 8 b8
0 41 bb aa
55 31 c9
30 f6 f9 cd
13 72 23 81 fb
55 aa
75 1d f6 c1
1 74 18 57 be e0
266 6 8b
5d
8 89 5c
8 8b
5d a
89 5c a
8a
16 0 8 b4
42 eb
2a
57 8b
45 8 8b
55 a f7
36 f2
6 42 89 d1
31 d2
267 f7
36 f0
6 88 c5 d1 e8 d1 e8
24 c0
8 c1
88 d6
8a
16 0 8 bb
0 7c b8
1 2 cd
13 72 16 5e
81 3e fe
7d
55 aa
268 75 8 fa ea
0 7c
0 0 77 5 be f4
6 eb
3 be f
7 ac
20 c0
74 c b4 e
8a
3e
62 4 b3
7 cd
10 eb ef eb fe
269 0 0 10 0 1 0 0 7c
0 0 0 0 0 0 0 0 0 0 0 0 0 0 4d
69 73 73 69 6e
67 20 6f
70 65 72 61 74 69
270 6e
67 20 73 79 73 74 65 6d d a
0 4f
70 65 72 61 74 69 6e
67 20 73 79 73 74 65 6d
20 6c
6f
61 64 69 6e
67 20