summaryrefslogtreecommitdiff
path: root/mkdiskimage.in
blob: a250af297e51deb428552d41b96365fefc7363db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/perl
#
# Creates a blank MS-DOS formatted hard disk image
#

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 =~ /^\-/ ) {
	$opt{$'} = 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 [-o] file c h s (max: 1024 256 63)\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 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;
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__