summaryrefslogtreecommitdiff
path: root/mkdiskimage.in
diff options
context:
space:
mode:
authorhpa <hpa>2002-10-03 10:02:50 +0000
committerhpa <hpa>2002-10-03 10:02:50 +0000
commit1043c2415c84ec6c48d44890027a6fbd5140f577 (patch)
tree9d08a9211e5671e6e91abdaf4279331e343c3a4a /mkdiskimage.in
parent1769d57c94d7a965168b72b6fd8d48251710b452 (diff)
downloadsyslinux-1043c2415c84ec6c48d44890027a6fbd5140f577.tar.gz
Add tool to create a DOS-formatted hard drive image; minor tidying of the
MBR code.
Diffstat (limited to 'mkdiskimage.in')
-rwxr-xr-xmkdiskimage.in97
1 files changed, 97 insertions, 0 deletions
diff --git a/mkdiskimage.in b/mkdiskimage.in
new file mode 100755
index 00000000..d0d8703c
--- /dev/null
+++ b/mkdiskimage.in
@@ -0,0 +1,97 @@
+#!/usr/bin/perl
+#
+# Creates a blank MS-DOS formatted hard disk image
+#
+
+use Fcntl;
+
+($file,$c,$h,$s) = @ARGV;
+$c += 0; $h += 0; $s += 0;
+
+if ( !$file || $c < 1 || $c > 1024 ||
+ $h < 1 || $h > 256 || $s < 1 || $s > 63 ) {
+ print STDERR "Usage: $0 file c h s (max: 1024 256 63)\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
+$tmpfile = "/tmp/mconfig.$$";
+$offset = $s*512;
+open(MCONFIG, "> ${tmpfile}") or die "$0: Cannot make mtools config\n";
+print MCONFIG "drive z:\n";
+print MCONFIG "file=\"\Q${file}\E\"\n";
+print MCONFIG "cylinders=${c}\n";
+print MCONFIG "heads=${h}\n";
+print MCONFIG "sectors=${s}\n";
+print MCONFIG "offset=${offset}\n";
+close(MCONFIG);
+
+$ENV{'MTOOLSRC'} = $tmpfile;
+system('mformat', 'z:');
+
+unlink($tmpfile);
+
+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__