176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#!/usr/bin/perl
276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman## -----------------------------------------------------------------------
376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman##
476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman##   Copyright 2002-2008 H. Peter Anvin - All Rights Reserved
576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman##   Copyright 2009-2010 Intel Corporation; author: H. Peter Anvin
676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman##
776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman##   This program is free software; you can redistribute it and/or modify
876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman##   it under the terms of the GNU General Public License as published by
976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman##   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
1076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman##   Boston MA 02111-1307, USA; either version 2 of the License, or
1176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman##   (at your option) any later version; incorporated herein by reference.
1276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman##
1376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman## -----------------------------------------------------------------------
1476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
1576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#
1676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman# Creates a blank MS-DOS formatted hard disk image
1776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman#
1876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
1976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanuse bytes;
2076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanuse integer;
2176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanuse Fcntl;
2276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanuse Errno;
2376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanuse Cwd;
2476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanuse IO::Handle;			# For flush()
2576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
2676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmansub absolute_path($) {
2776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    my($f) = @_;
2876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    my($c);
2976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
3076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    return $f if ( $f =~ /^\// );
3176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $c = cwd();
3276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $c = '' if ( $c eq '/' );
3376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    return $c.'/'.$f;
3476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
3576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
3676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmansub is_linux() {
3776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    return !!eval '{ '.
3876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	'use POSIX; '.
3976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	'($sysname, $nodename, $release, $version, $machine) = POSIX::uname(); '.
4076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	"return \$sysname eq \'Linux\'; }";
4176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
4276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
4376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmansub get_random() {
4476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    # Get a 32-bit random number
4576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    my $rfd, $rnd;
4676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    my $rid;
4776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
4876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    if (open($rfd, "< /dev/urandom\0") && read($rfd, $rnd, 4) == 4) {
4976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	$rid = unpack("V", $rnd);
5076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    }
5176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
5276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    close($rfd) if (defined($rfd));
5376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    return $rid if (defined($rid));
5476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
5576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    # This sucks but is better than nothing...
5676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    return ($$+time()) & 0xffffffff;
5776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
5876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
5976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmansub get_hex_data() {
6076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    my $mbr = '';
6176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    my $line, $byte;
6276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    while ( $line = <DATA> ) {
6376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	chomp $line;
6476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	last if ($line eq '*');
6576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	foreach $byte ( split(/\s+/, $line) ) {
6676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	    $mbr .= chr(hex($byte));
6776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	}
6876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    }
6976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    return $mbr;
7076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
7176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
7276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$is_linux = is_linux();
7376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanif ( $is_linux ) {
7476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    # IOCTL numbers
7576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $BLKRRPART    = 0x125f;
7676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $BLKGETSIZE   = 0x1260;
7776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
7876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
7976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman%opt = ();
8076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman@args = ();
8176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
8276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanwhile (defined($a = shift(@ARGV))) {
8376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    if ( $a =~ /^\-/ ) {
8476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	foreach $o ( split(//, substr($a,1)) ) {
8576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	    $opt{$o} = 1;
8676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	    if ($o eq 'i') {
8776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman		$id = shift(@ARGV);
8876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	    }
8976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	}
9076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    } else {
9176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	push(@args, $a);
9276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    }
9376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
9476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
9576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman($file,$c,$h,$s) = @args;
9676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$c += 0;  $h += 0;  $s += 0;
9776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
9876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$pentry = 1;
9976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$pentry = 2 if ( $opt{'2'} );
10076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$pentry = 3 if ( $opt{'3'} );
10176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$pentry = 4 if ( $opt{'4'} );
10276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
10376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanif ( $opt{'z'} ) {
10476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $h = $h || 64;
10576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $s = $s || 32;
10676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
10776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
10876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanif ( $opt{'M'} && $h && $s ) {
10976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    # Specify size in megabytes, not in cylinders
11076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $c = ($c*1024*2)/($h*$s);
11176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
11276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
11376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$is_open = 0;
11476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
11576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanif ( $c == 0 && $file ne '' ) {
11676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $len = 0;
11776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    if ( sysopen(OUTPUT, $file, O_RDWR, 0666) ) {
11876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	$is_open = 1;
11976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
12076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	if ( (@filestat = stat(OUTPUT)) && S_ISREG($filestat[2]) ) {
12176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	    $len = $filestat[7] >> 9;
12276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	} elsif ( $is_linux && S_ISBLK($filestat[2]) ) {
12376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	    $blksize = pack("L!", 0);
12476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	    if ( ioctl(OUTPUT, $BLKGETSIZE, $blksize) == 0 ) {
12576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman		$len = unpack("L!", $blksize); # In 512-byte sectors!
12676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	    }
12776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	}
12876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    }
12976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
13076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    if ( !$len ) {
13176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	print STDERR "$0: $file: don't know how to determine the size of this device\n";
13276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	exit 1;
13376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    }
13476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
13576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $c = $len/($h*$s);
13676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
13776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
13876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanif ( $file eq '' || $c < 1 || $h < 1 || $h > 256 || $s < 1 || $s > 63 ) {
13976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    print STDERR "Usage: $0 [-doFMz4][-i id] file c h s (max: 1024 256 63)\n";
14076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    print STDERR "    -d    add DOSEMU header\n";
14176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    print STDERR "    -o    print filesystem offset to stdout\n";
14276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    print STDERR "    -F    format partition as FAT32\n";
14376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    print STDERR "    -M    \"c\" argument is megabytes, calculate cylinders\n";
14476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    print STDERR "    -z    use zipdisk geometry (h=64 s=32)\n";
14576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    print STDERR "    -4    use partition entry 4 (standard for zipdisks)\n";
14676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    print STDERR "    -i    specify the MBR ID\n";
14776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    print STDERR "    -s    output a sparse file (don't allocate all blocks)\n";
14876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    exit 1;
14976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
15076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
15176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanif ($c > 1024) {
15276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    print STDERR "Warning: more than 1024 cylinders ($c).\n";
15376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    print STDERR "Not all BIOSes will be able to boot this device.\n";
15476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $cc = 1024;
15576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} else {
15676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $cc = $c;
15776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
15876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
15976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$cylsize = $h*$s*512;
16076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
16176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanif ( !$is_open ) {
16276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    sysopen(OUTPUT, $file, O_CREAT|O_RDWR|O_TRUNC, 0666)
16376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	or die "$0: Cannot open: $file\n";
16476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
16576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanbinmode OUTPUT;
16676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
16776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman# Print out DOSEMU header, if requested
16876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanif ( $opt{'d'} ) {
16976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $emuhdr = "DOSEMU\0" . pack("VVVV", $h, $s, $c, 128);
17076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $emuhdr .= "\0" x (128 - length($emuhdr));
17176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    print OUTPUT $emuhdr;
17276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
17376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
17476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman# Print the MBR and partition table
17576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$mbr = get_hex_data();
17676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanif ( length($mbr) > 440 ) {
17776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    die "$0: Bad MBR code\n";
17876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
17976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
18076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$mbr .= "\0" x (440 - length($mbr));
18176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanif (defined($id)) {
18276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $id = to_int($id);
18376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} else {
18476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $id = get_random();
18576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
18676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$mbr .= pack("V", $id);		# Offset 440: MBR ID
18776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$mbr .= "\0\0";			# Offset 446: actual partition table
18876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
18976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanprint OUTPUT $mbr;
19076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
19176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman# Print partition table
19276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$psize = $c*$h*$s-$s;
19376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$bhead   = ($h > 1) ? 1 : 0;
19476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$bsect   = 1;
19576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$bcyl    = ($h > 1) ? 0 : 1;
19676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$ehead   = $h-1;
19776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$esect   = $s + ((($cc-1) & 0x300) >> 2);
19876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$ecyl    = ($cc-1) & 0xff;
19976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanif ( $c > 1024 ) {
20076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $fstype = 0x0e;
20176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} elsif ( $psize > 65536 ) {
20276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $fstype = 0x06;
20376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} else {
20476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $fstype = 0x04;
20576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
20676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanfor ( $i = 1 ; $i <= 4 ; $i++ ) {
20776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    if ( $i == $pentry ) {
20876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	print OUTPUT pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
20976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman			  $ehead, $esect, $ecyl, $s, $psize);
21076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    } else {
21176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	print OUTPUT "\0" x 16;
21276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    }
21376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
21476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanprint OUTPUT "\x55\xaa";
21576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
21676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman# Output blank file
21776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$totalsize = $c*$h*$s;
21876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$tracks    = $c*$h;
21976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
22076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman# If -s is given, try to simply use truncate...
22176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanunless ($opt{'s'} && truncate(OUTPUT, $totalsize)) {
22276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $track = "\0" x (512*$s);
22376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
22476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    # Print fractional track
22576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    print OUTPUT "\0" x (512 * ($s-1));
22676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
22776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    for ( $i = 1 ; $i < $tracks ; $i++ ) {
22876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	print OUTPUT $track;
22976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    }
23076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
23176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
23276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman# Print mtools temp file
23376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$n = 0;
23476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanwhile ( !defined($tmpdir) ) {
23576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    $tmpdir = "/tmp/mkdiskimage.$$.".($n++);
23676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    if ( !mkdir($tmpdir, 0700) ) {
23776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	die "$0: Failed to make temp directory: $tmpdir\n"
23876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	    if ( $! != EEXIST );
23976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	undef $tmpdir;
24076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    }
24176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
24276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
24376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$cfgfile = $tmpdir.'/mtools.conf';
24476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$imglink = $tmpdir.'/disk.img';
24576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmandie "$0: Failed to create symlink $imglink\n"
24676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    if ( !symlink(absolute_path($file), $imglink) );
24776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
24876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$header_size = ($opt{'d'} ? 128 : 0);
24976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
25076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman# Start of filesystem
25176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$offset = $s*512 + $header_size;
25276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
25376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman# Start of partition table entry
25476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$pstart = $header_size + 446 + 16*($pentry-1);
25576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
25676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanopen(MCONFIG, "> ${cfgfile}") or die "$0: Cannot make mtools config\n";
25776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanprint MCONFIG "drive z:\n";
25876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanprint MCONFIG "file=\"${imglink}\"\n";
25976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanprint MCONFIG "cylinders=${c}\n";
26076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanprint MCONFIG "heads=${h}\n";
26176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanprint MCONFIG "sectors=${s}\n";
26276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanprint MCONFIG "offset=${offset}\n";
26376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanprint MCONFIG "mformat_only\n";
26476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanclose(MCONFIG);
26576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
26676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman# Output the filesystem offset to stdout if appropriate
26776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanif ( $opt{'o'} ) {
26876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    print $offset, "\n";
26976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
27076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
27176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman$ENV{'MTOOLSRC'} = $cfgfile;
27276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanif ( $opt{'F'} ) {
27376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    system('mformat', '-F', 'z:');
27476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} else {
27576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    system('mformat', 'z:');
27676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
27776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
27876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman# Clean up in /tmp
27976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanunlink($cfgfile);
28076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanunlink($imglink);
28176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanrmdir($tmpdir);
28276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
28376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman# MTOOLS doesn't write the bsHiddenSecs field correctly
28476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanseek(OUTPUT, $offset + 0x1c, 0);
28576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanprint OUTPUT pack("V", ($offset-$header_size)>>9);
28676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
28776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman# Set the partition type
28876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanif ( $opt{'F'} ) {
28976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    if ( $c > 1024 ) {
29076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	$fstype = 0x0c;		# FAT32 LBA
29176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    } else {
29276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	$fstype = 0x0b;
29376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    }
29476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman} else {
29576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    if ( $c > 1024 ) {
29676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	$fstype = 0x0e;		# FAT16 LBA
29776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    } elsif ( $psize > 65536 ) {
29876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	$fstype = 0x06;		# FAT16 > 32MB
29976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    } else {
30076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	$fstype = 0x04;		# FAT16 <= 32MB
30176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    }
30276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    seek(OUTPUT, $offset + 0x36, 0);
30376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    read(OUTPUT, $fsname, 8);
30476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
30576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    # FAT12: adjust partition type
30676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    if ( $fsname eq 'FAT12   ' ) {
30776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman	$fstype = 0x01;		# FAT12
30876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    }
30976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman}
31076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanseek(OUTPUT, $pstart+4, 0);
31176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanprint OUTPUT pack("C", $fstype);
31276d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
31376d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanflush OUTPUT;
31476d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
31576d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman# Just in case this is a block device, try to flush the partition table
31676d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanif ( $is_linux ) {
31776d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman    ioctl(OUTPUT, $BLKRRPART, 0);
31876d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman};
31976d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman
32076d05dc695b06c4e987bb8078f78032441e1430cGreg Hartmanexit 0;
32176d05dc695b06c4e987bb8078f78032441e1430cGreg Hartman__END__
322