#!/usr/bin/perl
## -----------------------------------------------------------------------
##  $Id: mkdiskimage.in,v 1.6 2003/04/10 06:25:59 hpa Exp $
##   
##   Copyright 2002-2003 H. Peter Anvin - All Rights Reserved
##
##   This program is free software; you can redistribute it and/or modify
##   it under the terms of the GNU General Public License as published by
##   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
##   Bostom MA 02111-1307, USA; either version 2 of the License, or
##   (at your option) any later version; incorporated herein by reference.
##
## -----------------------------------------------------------------------

#
# Creates a blank MS-DOS formatted hard disk image
#

eval { use bytes; };
use Fcntl;
use Errno;
use Cwd;

sub absolute_path($) {
    my($f) = @_;
    my($c);

    return $f if ( $f =~ /^\// );
    $c = cwd();
    $c = '' if ( $c eq '/' );
    return $c.'/'.$f;
}

%opt = ();
@args = ();

for $a ( @ARGV ) {
    if ( $a =~ /^\-/ ) {
	foreach $o ( split(//, substr($a,1)) ) {
	    $opt{$o} = 1;
	}
    } else {
	push(@args, $a);
    }
}

($file,$c,$h,$s) = @args;
$c += 0;  $h += 0;  $s += 0;

if ( !$file || $c < 1 || $c > 1024 ||
     $h < 1 || $h > 256 || $s < 1 || $s > 63 ) {
    print STDERR "Usage: $0 [-do] file c h s (max: 1024 256 63)\n";
    print STDERR "    -d    add DOSEMU header\n";
    print STDERR "    -o    print filesystem offset to stdout\n";
    exit 1;
}

$cylsize = $h*$s*512;

sysopen(OUTPUT, $file, O_CREAT|O_RDWR|O_TRUNC, 0666)
    or die "$0: Cannot open: $file\n";

# Print out DOSEMU header, if requested
if ( $opt{'d'} ) {
    $emuhdr = "DOSEMU\0" . pack("VVVV", $h, $s, $c, 128);
    $emuhdr .= "\0" x (128 - length($emuhdr));
    print OUTPUT $emuhdr;
}

# Print the MBR and partition table
$mbr = '';
while ( $line = <DATA> ) {
    chomp $line;
    foreach $byte ( split(/\s+/, $line) ) {
	$mbr .= chr(hex($byte));
    }
}
if ( length($mbr) > 446 ) {
    die "$0: Bad MBR code\n";
}

$mbr .= "\0" x (446 - length($mbr));

print OUTPUT $mbr;

# Print partition table
$psize = $c*$h*$s-$s;
$bhead   = ($h > 1) ? 1 : 0;
$bsect   = 1;
$bcyl    = ($h > 1) ? 0 : 1;
$ehead   = $h;
$esect   = $s + (($c & 0x300) >> 2);
$ecyl    = $c & 0xff;
if ( $psize > 65536 ) {
    $fstype = 0x06;
} else {
    $fstype = 0x04;
}
print OUTPUT pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
		  $ehead, $esect, $ecyl, $s, $psize);
print OUTPUT "\0" x 48;
print OUTPUT "\x55\xaa";

# Output blank file
$totalsize = $c*$h*$s;
$tracks    = $c*$h;

$track = "\0" x (512*$s);

# Print fractional track
print OUTPUT "\0" x (512 * ($s-1));

for ( $i = 1 ; $i < $tracks ; $i++ ) {
    print OUTPUT $track;
}

# Print mtools temp file
$n = 0;
while ( !defined($tmpdir) ) {
    $tmpdir = "/tmp/mkdiskimage.$$.".($n++);
    if ( !mkdir($tmpdir, 0700) ) {
	die "$0: Failed to make temp directory: $tmpdir\n"
	    if ( $! != EEXIST );
	undef $tmpdir;
    }
}

$cfgfile = $tmpdir.'/mtools.conf';
$imglink = $tmpdir.'/disk.img';
die "$0: Failed to create symlink $imglink\n"
    if ( !symlink(absolute_path($file), $imglink) );

$offset = $s*512 + ($opt{'d'} ? 128 : 0);
open(MCONFIG, "> ${cfgfile}") or die "$0: Cannot make mtools config\n";
print MCONFIG "drive z:\n";
print MCONFIG "file=\"${imglink}\"\n";
print MCONFIG "cylinders=${c}\n";
print MCONFIG "heads=${h}\n";
print MCONFIG "sectors=${s}\n";
print MCONFIG "offset=${offset}\n";
print MCONFIG "mformat_only\n";
close(MCONFIG);

# Output the filesystem offset to stdout if appropriate
if ( $opt{'o'} ) {
    print $offset, "\n";
}

$ENV{'MTOOLSRC'} = $cfgfile;
system('mformat', 'z:');

# Clean up in /tmp
unlink($cfgfile);
unlink($imglink);
rmdir($tmpdir);

seek(OUTPUT, $s*512+0x36, 0);
read(OUTPUT, $fsname, 8);

# FAT12: adjust partition type
if ( $fsname eq 'FAT12   ' ) {
    $fstype = 0x01;
}
seek(OUTPUT, 446+4, 0);
print OUTPUT pack("C", $fstype);

exit 0;
__END__
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
31 c0 88 f0 40 a3 ec 6 80 e1 3f 88 e ee 6 be be 7 31 c0 b9 4 0 f6 4 80 74 3 40 89 f7 83 c6 10 e2 f3 83
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 dc
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 ee 6 42 89 d1 31 d2
f7 36 ec 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
75 8 fa ea 0 7c 0 0 77 5 be f0 6 eb 3 be b 7 ac 30 c0 74 9 b4 e bb 7 0 cd 10 eb f2 eb fe 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 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 65 72 72 6f
72 d a 0 
