2 ## -----------------------------------------------------------------------
5 ## Copyright 2002-2003 H. Peter Anvin - All Rights Reserved
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.
13 ## -----------------------------------------------------------------------
16 # Creates a blank MS-DOS formatted hard disk image
24 sub absolute_path($) {
28 return $f if ( $f =~ /^\// );
30 $c = '' if ( $c eq '/' );
39 foreach $o ( split(//, substr($a,1)) ) {
47 ($file,$c,$h,$s) = @args;
48 $c += 0; $h += 0; $s += 0;
50 if ( !$file || $c < 1 || $c > 1024 ||
51 $h < 1 || $h > 256 || $s < 1 || $s > 63 ) {
52 print STDERR "Usage: $0 [-do] file c h s (max: 1024 256 63)\n";
53 print STDERR " -d add DOSEMU header\n";
54 print STDERR " -o print filesystem offset to stdout\n";
60 sysopen(OUTPUT, $file, O_CREAT|O_RDWR|O_TRUNC, 0666)
61 or die "$0: Cannot open: $file\n";
62 eval { binmode OUTPUT; };
64 # Print out DOSEMU header, if requested
66 $emuhdr = "DOSEMU\0" . pack("VVVV", $h, $s, $c, 128);
67 $emuhdr .= "\0" x (128 - length($emuhdr));
71 # Print the MBR and partition table
73 while ( $line = <DATA> ) {
75 foreach $byte ( split(/\s+/, $line) ) {
76 $mbr .= chr(hex($byte));
79 if ( length($mbr) > 446 ) {
80 die "$0: Bad MBR code\n";
83 $mbr .= "\0" x (446 - length($mbr));
87 # Print partition table
89 $bhead = ($h > 1) ? 1 : 0;
91 $bcyl = ($h > 1) ? 0 : 1;
93 $esect = $s + ((($c-1) & 0x300) >> 2);
94 $ecyl = ($c-1) & 0xff;
95 if ( $psize > 65536 ) {
100 print OUTPUT pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
101 $ehead, $esect, $ecyl, $s, $psize);
102 print OUTPUT "\0" x 48;
103 print OUTPUT "\x55\xaa";
106 $totalsize = $c*$h*$s;
109 $track = "\0" x (512*$s);
111 # Print fractional track
112 print OUTPUT "\0" x (512 * ($s-1));
114 for ( $i = 1 ; $i < $tracks ; $i++ ) {
118 # Print mtools temp file
120 while ( !defined($tmpdir) ) {
121 $tmpdir = "/tmp/mkdiskimage.$$.".($n++);
122 if ( !mkdir($tmpdir, 0700) ) {
123 die "$0: Failed to make temp directory: $tmpdir\n"
129 $cfgfile = $tmpdir.'/mtools.conf';
130 $imglink = $tmpdir.'/disk.img';
131 die "$0: Failed to create symlink $imglink\n"
132 if ( !symlink(absolute_path($file), $imglink) );
134 $offset = $s*512 + ($opt{'d'} ? 128 : 0);
135 open(MCONFIG, "> ${cfgfile}") or die "$0: Cannot make mtools config\n";
136 print MCONFIG "drive z:\n";
137 print MCONFIG "file=\"${imglink}\"\n";
138 print MCONFIG "cylinders=${c}\n";
139 print MCONFIG "heads=${h}\n";
140 print MCONFIG "sectors=${s}\n";
141 print MCONFIG "offset=${offset}\n";
142 print MCONFIG "mformat_only\n";
145 # Output the filesystem offset to stdout if appropriate
150 $ENV{'MTOOLSRC'} = $cfgfile;
151 system('mformat', 'z:');
158 seek(OUTPUT, $s*512+0x36, 0);
159 read(OUTPUT, $fsname, 8);
161 # FAT12: adjust partition type
162 if ( $fsname eq 'FAT12 ' ) {
165 seek(OUTPUT, 446+4, 0);
166 print OUTPUT pack("C", $fstype);