Adding upstream version 3.50~pre5.
[syslinux-debian/hramrach.git] / mkdiskimage.in
blob856ad3c5cba5c2e7f0dc3dcdcae91a9594bcc1bc
1 #!/usr/bin/perl
2 ## -----------------------------------------------------------------------
3 ##
4 ## Copyright 2002-2007 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{'z'} ) {
72 $h = $h || 64;
73 $s = $s || 32;
76 if ( $opt{'M'} && $h && $s ) {
77 # Specify size in megabytes, not in cylinders
78 $c = ($c*1024*2)/($h*$s);
81 $is_open = 0;
83 if ( $c == 0 && $file ne '' ) {
84 $len = 0;
85 if ( sysopen(OUTPUT, $file, O_RDWR, 0666) ) {
86 $is_open = 1;
88 if ( (@filestat = stat(OUTPUT)) && S_ISREG($filestat[2]) ) {
89 $len = $filestat[7] >> 9;
90 } elsif ( $is_linux && S_ISBLK($filestat[2]) ) {
91 $blksize = pack("L!", 0);
92 if ( ioctl(OUTPUT, $BLKGETSIZE, $blksize) == 0 ) {
93 $len = unpack("L!", $blksize); # In 512-byte sectors!
98 if ( !$len ) {
99 print STDERR "$0: $file: don't know how to determine the size of this device\n";
100 exit 1;
103 $c = $len/($h*$s);
106 if ( $file eq '' || $c < 1 || $c > 1024 ||
107 $h < 1 || $h > 256 || $s < 1 || $s > 63 ) {
108 print STDERR "Usage: $0 [-doFMz4] file c h s (max: 1024 256 63)\n";
109 print STDERR " -d add DOSEMU header\n";
110 print STDERR " -o print filesystem offset to stdout\n";
111 print STDERR " -F format partition as FAT32\n";
112 print STDERR " -M \"c\" argument is megabytes, calculate cylinders\n";
113 print STDERR " -z use zipdisk geometry (h=64 s=32)\n";
114 print STDERR " -4 use partition entry 4 (standard for zipdisks)\n";
115 exit 1;
118 $cylsize = $h*$s*512;
120 if ( !$is_open ) {
121 sysopen(OUTPUT, $file, O_CREAT|O_RDWR|O_TRUNC, 0666)
122 or die "$0: Cannot open: $file\n";
124 binmode OUTPUT;
126 # Print out DOSEMU header, if requested
127 if ( $opt{'d'} ) {
128 $emuhdr = "DOSEMU\0" . pack("VVVV", $h, $s, $c, 128);
129 $emuhdr .= "\0" x (128 - length($emuhdr));
130 print OUTPUT $emuhdr;
133 # Print the MBR and partition table
134 $mbr = '';
135 while ( $line = <DATA> ) {
136 chomp $line;
137 foreach $byte ( split(/\s+/, $line) ) {
138 $mbr .= chr(hex($byte));
141 if ( length($mbr) > 446 ) {
142 die "$0: Bad MBR code\n";
145 $mbr .= "\0" x (446 - length($mbr));
147 print OUTPUT $mbr;
149 # Print partition table
150 $psize = $c*$h*$s-$s;
151 $bhead = ($h > 1) ? 1 : 0;
152 $bsect = 1;
153 $bcyl = ($h > 1) ? 0 : 1;
154 $ehead = $h-1;
155 $esect = $s + ((($c-1) & 0x300) >> 2);
156 $ecyl = ($c-1) & 0xff;
157 if ( $psize > 65536 ) {
158 $fstype = 0x06;
159 } else {
160 $fstype = 0x04;
162 for ( $i = 1 ; $i <= 4 ; $i++ ) {
163 if ( $i == $pentry ) {
164 print OUTPUT pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
165 $ehead, $esect, $ecyl, $s, $psize);
166 } else {
167 print OUTPUT "\0" x 16;
170 print OUTPUT "\x55\xaa";
172 # Output blank file
173 $totalsize = $c*$h*$s;
174 $tracks = $c*$h;
176 $track = "\0" x (512*$s);
178 # Print fractional track
179 print OUTPUT "\0" x (512 * ($s-1));
181 for ( $i = 1 ; $i < $tracks ; $i++ ) {
182 print OUTPUT $track;
185 # Print mtools temp file
186 $n = 0;
187 while ( !defined($tmpdir) ) {
188 $tmpdir = "/tmp/mkdiskimage.$$.".($n++);
189 if ( !mkdir($tmpdir, 0700) ) {
190 die "$0: Failed to make temp directory: $tmpdir\n"
191 if ( $! != EEXIST );
192 undef $tmpdir;
196 $cfgfile = $tmpdir.'/mtools.conf';
197 $imglink = $tmpdir.'/disk.img';
198 die "$0: Failed to create symlink $imglink\n"
199 if ( !symlink(absolute_path($file), $imglink) );
201 $header_size = ($opt{'d'} ? 128 : 0);
203 # Start of filesystem
204 $offset = $s*512 + $header_size;
206 # Start of partition table entry
207 $pstart = $header_size + 446 + 16*($pentry-1);
209 open(MCONFIG, "> ${cfgfile}") or die "$0: Cannot make mtools config\n";
210 print MCONFIG "drive z:\n";
211 print MCONFIG "file=\"${imglink}\"\n";
212 print MCONFIG "cylinders=${c}\n";
213 print MCONFIG "heads=${h}\n";
214 print MCONFIG "sectors=${s}\n";
215 print MCONFIG "offset=${offset}\n";
216 print MCONFIG "mformat_only\n";
217 close(MCONFIG);
219 # Output the filesystem offset to stdout if appropriate
220 if ( $opt{'o'} ) {
221 print $offset, "\n";
224 $ENV{'MTOOLSRC'} = $cfgfile;
225 if ( $opt{'F'} ) {
226 system('mformat', '-F', 'z:');
227 } else {
228 system('mformat', 'z:');
231 # Clean up in /tmp
232 unlink($cfgfile);
233 unlink($imglink);
234 rmdir($tmpdir);
236 # MTOOLS doesn't write the bsHiddenSecs field correctly
237 seek(OUTPUT, $offset + 0x1c, 0);
238 print OUTPUT pack("V", ($offset-$header_size)>>9);
240 # Set the partition type
241 if ( $opt{'F'} ) {
242 $fstype = 0x0b; # FAT32
243 } else {
244 if ( $psize > 65536 ) {
245 $fstype = 0x06; # FAT16 > 32MB
246 } else {
247 $fstype = 0x04; # FAT16 <= 32MB
249 seek(OUTPUT, $offset + 0x36, 0);
250 read(OUTPUT, $fsname, 8);
252 # FAT12: adjust partition type
253 if ( $fsname eq 'FAT12 ' ) {
254 $fstype = 0x01; # FAT12
257 seek(OUTPUT, $pstart+4, 0);
258 print OUTPUT pack("C", $fstype);
260 flush OUTPUT;
262 # Just in case this is a block device, try to flush the partition table
263 if ( $is_linux ) {
264 ioctl(OUTPUT, $BLKRRPART, 0);
267 exit 0;
268 __END__