Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 97 lines (78 sloc) 2.006 kb
1043c241 » hpa
2002-10-03 Add tool to create a DOS-formatted hard drive image; minor tidying of…
1 #!/usr/bin/perl
2 #
3 # Creates a blank MS-DOS formatted hard disk image
4 #
5
6 use Fcntl;
7
8 ($file,$c,$h,$s) = @ARGV;
9 $c += 0; $h += 0; $s += 0;
10
11 if ( !$file || $c < 1 || $c > 1024 ||
12 $h < 1 || $h > 256 || $s < 1 || $s > 63 ) {
13 print STDERR "Usage: $0 file c h s (max: 1024 256 63)\n";
14 exit 1;
15 }
16
17 $cylsize = $h*$s*512;
18
19 sysopen(OUTPUT, $file, O_CREAT|O_RDWR|O_TRUNC, 0666)
20 or die "$0: Cannot open: $file\n";
21
22 # Print the MBR and partition table
23 $mbr = '';
24 while ( $line = <DATA> ) {
25 chomp $line;
26 foreach $byte ( split(/\s+/, $line) ) {
27 $mbr .= chr(hex($byte));
28 }
29 }
30 if ( length($mbr) > 446 ) {
31 die "$0: Bad MBR code\n";
32 }
33
34 $mbr .= "\0" x (446 - length($mbr));
35
36 print OUTPUT $mbr;
37
38 # Print partition table
39 $psize = $c*$h*$s-$s;
40 $bhead = ($h > 1) ? 1 : 0;
41 $bsect = 1;
42 $bcyl = ($h > 1) ? 0 : 1;
43 $ehead = $h;
44 $esect = $s + (($c & 0x300) >> 2);
45 $ecyl = $c & 0xff;
46 if ( $psize > 65536 ) {
47 $fstype = 0x06;
48 } else {
49 $fstype = 0x04;
50 }
51 print OUTPUT pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
52 $ehead, $esect, $ecyl, $s, $psize);
53 print OUTPUT "\0" x 48;
54 print OUTPUT "\x55\xaa";
55
56 # Output blank file
57 $totalsize = $c*$h*$s;
58 $tracks = $c*$h;
59
60 $track = "\0" x (512*$s);
61
62 # Print fractional track
63 print OUTPUT "\0" x (512 * ($s-1));
64
65 for ( $i = 1 ; $i < $tracks ; $i++ ) {
66 print OUTPUT $track;
67 }
68
69 # Print mtools temp file
70 $tmpfile = "/tmp/mconfig.$$";
71 $offset = $s*512;
72 open(MCONFIG, "> ${tmpfile}") or die "$0: Cannot make mtools config\n";
73 print MCONFIG "drive z:\n";
74 print MCONFIG "file=\"\Q${file}\E\"\n";
75 print MCONFIG "cylinders=${c}\n";
76 print MCONFIG "heads=${h}\n";
77 print MCONFIG "sectors=${s}\n";
78 print MCONFIG "offset=${offset}\n";
79 close(MCONFIG);
80
81 $ENV{'MTOOLSRC'} = $tmpfile;
82 system('mformat', 'z:');
83
84 unlink($tmpfile);
85
86 seek(OUTPUT, $s*512+0x36, 0);
87 read(OUTPUT, $fsname, 8);
88
89 # FAT12: adjust partition type
90 if ( $fsname eq 'FAT12 ' ) {
91 $fstype = 0x01;
92 }
93 seek(OUTPUT, 446+4, 0);
94 print OUTPUT pack("C", $fstype);
95
96 exit 0;
97 __END__
Something went wrong with that request. Please try again.