2 ## -----------------------------------------------------------------------
4 ## Copyright 2002-2008 H. Peter Anvin - All Rights Reserved
5 ## Copyright 2009-2010 Intel Corporation; author: H. Peter Anvin
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 use IO
::Handle
; # For flush()
26 sub absolute_path
($) {
30 return $f if ( $f =~ /^\// );
32 $c = '' if ( $c eq '/' );
39 '($sysname, $nodename, $release, $version, $machine) = POSIX::uname(); '.
40 "return \$sysname eq \'Linux\'; }";
44 # Get a 32-bit random number
48 if (open($rfd, "< /dev/urandom\0") && read($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;
62 while ( $line = <DATA
> ) {
64 last if ($line eq '*');
65 foreach $byte ( split(/\s+/, $line) ) {
66 $mbr .= chr(hex($byte));
72 $is_linux = is_linux
();
82 while (defined($a = shift(@ARGV))) {
84 foreach $o ( split(//, substr($a,1)) ) {
95 ($file,$c,$h,$s) = @args;
96 $c += 0; $h += 0; $s += 0;
99 $pentry = 2 if ( $opt{'2'} );
100 $pentry = 3 if ( $opt{'3'} );
101 $pentry = 4 if ( $opt{'4'} );
108 if ( $opt{'M'} && $h && $s ) {
109 # Specify size in megabytes, not in cylinders
110 $c = ($c*1024*2)/($h*$s);
115 if ( $c == 0 && $file ne '' ) {
117 if ( sysopen(OUTPUT
, $file, O_RDWR
, 0666) ) {
120 if ( (@filestat = stat(OUTPUT
)) && S_ISREG
($filestat[2]) ) {
121 $len = $filestat[7] >> 9;
122 } elsif ( $is_linux && S_ISBLK
($filestat[2]) ) {
123 $blksize = pack("L!", 0);
124 if ( ioctl(OUTPUT
, $BLKGETSIZE, $blksize) == 0 ) {
125 $len = unpack("L!", $blksize); # In 512-byte sectors!
131 print STDERR
"$0: $file: don't know how to determine the size of this device\n";
138 if ( $file eq '' || $c < 1 || $h < 1 || $h > 256 || $s < 1 || $s > 63 ) {
139 print STDERR
"Usage: $0 [-doFMz4][-i id] file c h s (max: 1024 256 63)\n";
140 print STDERR
" -d add DOSEMU header\n";
141 print STDERR
" -o print filesystem offset to stdout\n";
142 print STDERR
" -F format partition as FAT32\n";
143 print STDERR
" -M \"c\" argument is megabytes, calculate cylinders\n";
144 print STDERR
" -z use zipdisk geometry (h=64 s=32)\n";
145 print STDERR
" -4 use partition entry 4 (standard for zipdisks)\n";
146 print STDERR
" -i specify the MBR ID\n";
147 print STDERR
" -s output a sparse file (don't allocate all blocks)\n";
152 print STDERR
"Warning: more than 1024 cylinders ($c).\n";
153 print STDERR
"Not all BIOSes will be able to boot this device.\n";
159 $cylsize = $h*$s*512;
162 sysopen(OUTPUT
, $file, O_CREAT
|O_RDWR
|O_TRUNC
, 0666)
163 or die "$0: Cannot open: $file\n";
167 # Print out DOSEMU header, if requested
169 $emuhdr = "DOSEMU\0" . pack("VVVV", $h, $s, $c, 128);
170 $emuhdr .= "\0" x
(128 - length($emuhdr));
171 print OUTPUT
$emuhdr;
174 # Print the MBR and partition table
175 $mbr = get_hex_data
();
176 if ( length($mbr) > 440 ) {
177 die "$0: Bad MBR code\n";
180 $mbr .= "\0" x
(440 - length($mbr));
186 $mbr .= pack("V", $id); # Offset 440: MBR ID
187 $mbr .= "\0\0"; # Offset 446: actual partition table
191 # Print partition table
192 $psize = $c*$h*$s-$s;
193 $bhead = ($h > 1) ?
1 : 0;
195 $bcyl = ($h > 1) ?
0 : 1;
197 $esect = $s + ((($cc-1) & 0x300) >> 2);
198 $ecyl = ($cc-1) & 0xff;
201 } elsif ( $psize > 65536 ) {
206 for ( $i = 1 ; $i <= 4 ; $i++ ) {
207 if ( $i == $pentry ) {
208 print OUTPUT
pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
209 $ehead, $esect, $ecyl, $s, $psize);
211 print OUTPUT
"\0" x
16;
214 print OUTPUT
"\x55\xaa";
217 $totalsize = $c*$h*$s;
220 # If -s is given, try to simply use truncate...
221 unless ($opt{'s'} && truncate(OUTPUT
, $totalsize)) {
222 $track = "\0" x
(512*$s);
224 # Print fractional track
225 print OUTPUT
"\0" x
(512 * ($s-1));
227 for ( $i = 1 ; $i < $tracks ; $i++ ) {
232 # Print mtools temp file
234 while ( !defined($tmpdir) ) {
235 $tmpdir = "/tmp/mkdiskimage.$$.".($n++);
236 if ( !mkdir($tmpdir, 0700) ) {
237 die "$0: Failed to make temp directory: $tmpdir\n"
243 $cfgfile = $tmpdir.'/mtools.conf';
244 $imglink = $tmpdir.'/disk.img';
245 die "$0: Failed to create symlink $imglink\n"
246 if ( !symlink(absolute_path
($file), $imglink) );
248 $header_size = ($opt{'d'} ?
128 : 0);
250 # Start of filesystem
251 $offset = $s*512 + $header_size;
253 # Start of partition table entry
254 $pstart = $header_size + 446 + 16*($pentry-1);
256 open(MCONFIG
, "> ${cfgfile}") or die "$0: Cannot make mtools config\n";
257 print MCONFIG
"drive z:\n";
258 print MCONFIG
"file=\"${imglink}\"\n";
259 print MCONFIG
"cylinders=${c}\n";
260 print MCONFIG
"heads=${h}\n";
261 print MCONFIG
"sectors=${s}\n";
262 print MCONFIG
"offset=${offset}\n";
263 print MCONFIG
"mformat_only\n";
266 # Output the filesystem offset to stdout if appropriate
271 $ENV{'MTOOLSRC'} = $cfgfile;
273 system('mformat', '-F', 'z:');
275 system('mformat', 'z:');
283 # MTOOLS doesn't write the bsHiddenSecs field correctly
284 seek(OUTPUT
, $offset + 0x1c, 0);
285 print OUTPUT
pack("V", ($offset-$header_size)>>9);
287 # Set the partition type
290 $fstype = 0x0c; # FAT32 LBA
296 $fstype = 0x0e; # FAT16 LBA
297 } elsif ( $psize > 65536 ) {
298 $fstype = 0x06; # FAT16 > 32MB
300 $fstype = 0x04; # FAT16 <= 32MB
302 seek(OUTPUT
, $offset + 0x36, 0);
303 read(OUTPUT
, $fsname, 8);
305 # FAT12: adjust partition type
306 if ( $fsname eq 'FAT12 ' ) {
307 $fstype = 0x01; # FAT12
310 seek(OUTPUT
, $pstart+4, 0);
311 print OUTPUT
pack("C", $fstype);
315 # Just in case this is a block device, try to flush the partition table
317 ioctl(OUTPUT
, $BLKRRPART, 0);