Adding debian version 3.40~pre9-1.
[syslinux-debian/hramrach.git] / mkdiskimage.in
blob3db9bbe053495242b72d3eb360ac9ee94502c292
1 #!/usr/bin/perl
2 ## -----------------------------------------------------------------------
3 ##
4 ## Copyright 2002-2004 H. Peter Anvin - All Rights Reserved
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 ## Boston MA 02111-1307, USA; either version 2 of the License, or
10 ## (at your option) any later version; incorporated herein by reference.
12 ## -----------------------------------------------------------------------
15 # Creates a blank MS-DOS formatted hard disk image
18 use bytes;
19 use integer;
20 use Fcntl;
21 use Errno;
22 use Cwd;
23 use IO::Handle; # For flush()
25 sub absolute_path($) {
26 my($f) = @_;
27 my($c);
29 return $f if ( $f =~ /^\// );
30 $c = cwd();
31 $c = '' if ( $c eq '/' );
32 return $c.'/'.$f;
35 sub is_linux() {
36 return !!eval '{ '.
37 'use POSIX; '.
38 '($sysname, $nodename, $release, $version, $machine) = POSIX::uname(); '.
39 "return \$sysname eq \'Linux\'; }";
43 $is_linux = is_linux();
44 if ( $is_linux ) {
45 # IOCTL numbers
46 $BLKRRPART = 0x125f;
47 $BLKGETSIZE = 0x1260;
50 %opt = ();
51 @args = ();
53 for $a ( @ARGV ) {
54 if ( $a =~ /^\-/ ) {
55 foreach $o ( split(//, substr($a,1)) ) {
56 $opt{$o} = 1;
58 } else {
59 push(@args, $a);
63 ($file,$c,$h,$s) = @args;
64 $c += 0; $h += 0; $s += 0;
66 $pentry = 1;
67 $pentry = 2 if ( $opt{'2'} );
68 $pentry = 3 if ( $opt{'3'} );
69 $pentry = 4 if ( $opt{'4'} );
71 if ( $opt{'M'} ) {
72 # Specify size in megabytes, not in cylinders
73 $c = ($c*1024*2)/($h*$s);
76 $is_open = 0;
78 if ( $c == 0 && $file ne '' ) {
79 $len = 0;
80 if ( sysopen(OUTPUT, $file, O_RDWR, 0666) ) {
81 $is_open = 1;
83 if ( (@filestat = stat(OUTPUT)) && S_ISREG($filestat[2]) ) {
84 $len = $filestat[7] >> 9;
85 } elsif ( $is_linux && S_ISBLK($filestat[2]) ) {
86 $blksize = pack("L!", 0);
87 if ( ioctl(OUTPUT, $BLKGETSIZE, $blksize) == 0 ) {
88 $len = unpack("L!", $blksize); # In 512-byte sectors!
93 if ( !$len ) {
94 print STDERR "$0: $file: don't know how to determine the size of this device\n";
95 exit 1;
98 $c = $len/($h*$s);
101 if ( $file eq '' || $c < 1 || $c > 1024 ||
102 $h < 1 || $h > 256 || $s < 1 || $s > 63 ) {
103 print STDERR "Usage: $0 [-doF4] file [c h s] (max: 1024 256 63)\n";
104 print STDERR " -d add DOSEMU header\n";
105 print STDERR " -o print filesystem offset to stdout\n";
106 print STDERR " -F format partition as FAT32\n";
107 print STDERR " -M \"c\" argument is megabytes, calculate cylinders\n";
108 print STDERR " -4 use partition entry 4 (standard for zipdisks)\n";
109 exit 1;
112 $cylsize = $h*$s*512;
114 if ( !$is_open ) {
115 sysopen(OUTPUT, $file, O_CREAT|O_RDWR|O_TRUNC, 0666)
116 or die "$0: Cannot open: $file\n";
118 binmode OUTPUT;
120 # Print out DOSEMU header, if requested
121 if ( $opt{'d'} ) {
122 $emuhdr = "DOSEMU\0" . pack("VVVV", $h, $s, $c, 128);
123 $emuhdr .= "\0" x (128 - length($emuhdr));
124 print OUTPUT $emuhdr;
127 # Print the MBR and partition table
128 $mbr = '';
129 while ( $line = <DATA> ) {
130 chomp $line;
131 foreach $byte ( split(/\s+/, $line) ) {
132 $mbr .= chr(hex($byte));
135 if ( length($mbr) > 446 ) {
136 die "$0: Bad MBR code\n";
139 $mbr .= "\0" x (446 - length($mbr));
141 print OUTPUT $mbr;
143 # Print partition table
144 $psize = $c*$h*$s-$s;
145 $bhead = ($h > 1) ? 1 : 0;
146 $bsect = 1;
147 $bcyl = ($h > 1) ? 0 : 1;
148 $ehead = $h-1;
149 $esect = $s + ((($c-1) & 0x300) >> 2);
150 $ecyl = ($c-1) & 0xff;
151 if ( $psize > 65536 ) {
152 $fstype = 0x06;
153 } else {
154 $fstype = 0x04;
156 for ( $i = 1 ; $i <= 4 ; $i++ ) {
157 if ( $i == $pentry ) {
158 print OUTPUT pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
159 $ehead, $esect, $ecyl, $s, $psize);
160 } else {
161 print OUTPUT "\0" x 16;
164 print OUTPUT "\x55\xaa";
166 # Output blank file
167 $totalsize = $c*$h*$s;
168 $tracks = $c*$h;
170 $track = "\0" x (512*$s);
172 # Print fractional track
173 print OUTPUT "\0" x (512 * ($s-1));
175 for ( $i = 1 ; $i < $tracks ; $i++ ) {
176 print OUTPUT $track;
179 # Print mtools temp file
180 $n = 0;
181 while ( !defined($tmpdir) ) {
182 $tmpdir = "/tmp/mkdiskimage.$$.".($n++);
183 if ( !mkdir($tmpdir, 0700) ) {
184 die "$0: Failed to make temp directory: $tmpdir\n"
185 if ( $! != EEXIST );
186 undef $tmpdir;
190 $cfgfile = $tmpdir.'/mtools.conf';
191 $imglink = $tmpdir.'/disk.img';
192 die "$0: Failed to create symlink $imglink\n"
193 if ( !symlink(absolute_path($file), $imglink) );
195 $header_size = ($opt{'d'} ? 128 : 0);
197 # Start of filesystem
198 $offset = $s*512 + $header_size;
200 # Start of partition table entry
201 $pstart = $header_size + 446 + 16*($pentry-1);
203 open(MCONFIG, "> ${cfgfile}") or die "$0: Cannot make mtools config\n";
204 print MCONFIG "drive z:\n";
205 print MCONFIG "file=\"${imglink}\"\n";
206 print MCONFIG "cylinders=${c}\n";
207 print MCONFIG "heads=${h}\n";
208 print MCONFIG "sectors=${s}\n";
209 print MCONFIG "offset=${offset}\n";
210 print MCONFIG "mformat_only\n";
211 close(MCONFIG);
213 # Output the filesystem offset to stdout if appropriate
214 if ( $opt{'o'} ) {
215 print $offset, "\n";
218 $ENV{'MTOOLSRC'} = $cfgfile;
219 if ( $opt{'F'} ) {
220 system('mformat', '-F', 'z:');
221 } else {
222 system('mformat', 'z:');
225 # Clean up in /tmp
226 unlink($cfgfile);
227 unlink($imglink);
228 rmdir($tmpdir);
230 # MTOOLS doesn't write the bsHiddenSecs field correctly
231 seek(OUTPUT, $offset + 0x1c, 0);
232 print OUTPUT pack("V", ($offset-$header_size)>>9);
234 # Set the partition type
235 if ( $opt{'F'} ) {
236 $fstype = 0x0b; # FAT32
237 } else {
238 if ( $psize > 65536 ) {
239 $fstype = 0x06; # FAT16 > 32MB
240 } else {
241 $fstype = 0x04; # FAT16 <= 32MB
243 seek(OUTPUT, $offset + 0x36, 0);
244 read(OUTPUT, $fsname, 8);
246 # FAT12: adjust partition type
247 if ( $fsname eq 'FAT12 ' ) {
248 $fstype = 0x01; # FAT12
251 seek(OUTPUT, $pstart+4, 0);
252 print OUTPUT pack("C", $fstype);
254 flush OUTPUT;
256 # Just in case this is a block device, try to flush the partition table
257 if ( $is_linux ) {
258 ioctl(OUTPUT, $BLKRRPART, 0);
261 exit 0;
262 __END__