More fixes to the extlinux installer; change back to writable types
[profile/ivi/syslinux.git] / mkdiskimage.in
1 #!/usr/bin/perl
2 ## -----------------------------------------------------------------------
3 ##  $Id$
4 ##   
5 ##   Copyright 2002-2003 H. Peter Anvin - All Rights Reserved
6 ##
7 ##   This program is free software; you can redistribute it and/or modify
8 ##   it under the terms of the GNU General Public License as published by
9 ##   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
10 ##   Boston MA 02111-1307, USA; either version 2 of the License, or
11 ##   (at your option) any later version; incorporated herein by reference.
12 ##
13 ## -----------------------------------------------------------------------
14
15 #
16 # Creates a blank MS-DOS formatted hard disk image
17 #
18
19 eval { use bytes; };
20 use Fcntl;
21 use Errno;
22 use Cwd;
23
24 sub absolute_path($) {
25     my($f) = @_;
26     my($c);
27
28     return $f if ( $f =~ /^\// );
29     $c = cwd();
30     $c = '' if ( $c eq '/' );
31     return $c.'/'.$f;
32 }
33
34 %opt = ();
35 @args = ();
36
37 for $a ( @ARGV ) {
38     if ( $a =~ /^\-/ ) {
39         foreach $o ( split(//, substr($a,1)) ) {
40             $opt{$o} = 1;
41         }
42     } else {
43         push(@args, $a);
44     }
45 }
46
47 ($file,$c,$h,$s) = @args;
48 $c += 0;  $h += 0;  $s += 0;
49
50 if ( !$file || $c < 1 || $c > 1024 ||
51      $h < 1 || $h > 256 || $s < 1 || $s > 63 ) {
52     print STDERR "Usage: $0 [-do] file c h s (max: 1024 256 63)\n";
53     print STDERR "    -d    add DOSEMU header\n";
54     print STDERR "    -o    print filesystem offset to stdout\n";
55     exit 1;
56 }
57
58 $cylsize = $h*$s*512;
59
60 sysopen(OUTPUT, $file, O_CREAT|O_RDWR|O_TRUNC, 0666)
61     or die "$0: Cannot open: $file\n";
62 eval { binmode OUTPUT; };
63
64 # Print out DOSEMU header, if requested
65 if ( $opt{'d'} ) {
66     $emuhdr = "DOSEMU\0" . pack("VVVV", $h, $s, $c, 128);
67     $emuhdr .= "\0" x (128 - length($emuhdr));
68     print OUTPUT $emuhdr;
69 }
70
71 # Print the MBR and partition table
72 $mbr = '';
73 while ( $line = <DATA> ) {
74     chomp $line;
75     foreach $byte ( split(/\s+/, $line) ) {
76         $mbr .= chr(hex($byte));
77     }
78 }
79 if ( length($mbr) > 446 ) {
80     die "$0: Bad MBR code\n";
81 }
82
83 $mbr .= "\0" x (446 - length($mbr));
84
85 print OUTPUT $mbr;
86
87 # Print partition table
88 $psize = $c*$h*$s-$s;
89 $bhead   = ($h > 1) ? 1 : 0;
90 $bsect   = 1;
91 $bcyl    = ($h > 1) ? 0 : 1;
92 $ehead   = $h-1;
93 $esect   = $s + ((($c-1) & 0x300) >> 2);
94 $ecyl    = ($c-1) & 0xff;
95 if ( $psize > 65536 ) {
96     $fstype = 0x06;
97 } else {
98     $fstype = 0x04;
99 }
100 print OUTPUT pack("CCCCCCCCVV", 0x80, $bhead, $bsect, $bcyl, $fstype,
101                   $ehead, $esect, $ecyl, $s, $psize);
102 print OUTPUT "\0" x 48;
103 print OUTPUT "\x55\xaa";
104
105 # Output blank file
106 $totalsize = $c*$h*$s;
107 $tracks    = $c*$h;
108
109 $track = "\0" x (512*$s);
110
111 # Print fractional track
112 print OUTPUT "\0" x (512 * ($s-1));
113
114 for ( $i = 1 ; $i < $tracks ; $i++ ) {
115     print OUTPUT $track;
116 }
117
118 # Print mtools temp file
119 $n = 0;
120 while ( !defined($tmpdir) ) {
121     $tmpdir = "/tmp/mkdiskimage.$$.".($n++);
122     if ( !mkdir($tmpdir, 0700) ) {
123         die "$0: Failed to make temp directory: $tmpdir\n"
124             if ( $! != EEXIST );
125         undef $tmpdir;
126     }
127 }
128
129 $cfgfile = $tmpdir.'/mtools.conf';
130 $imglink = $tmpdir.'/disk.img';
131 die "$0: Failed to create symlink $imglink\n"
132     if ( !symlink(absolute_path($file), $imglink) );
133
134 $offset = $s*512 + ($opt{'d'} ? 128 : 0);
135 open(MCONFIG, "> ${cfgfile}") or die "$0: Cannot make mtools config\n";
136 print MCONFIG "drive z:\n";
137 print MCONFIG "file=\"${imglink}\"\n";
138 print MCONFIG "cylinders=${c}\n";
139 print MCONFIG "heads=${h}\n";
140 print MCONFIG "sectors=${s}\n";
141 print MCONFIG "offset=${offset}\n";
142 print MCONFIG "mformat_only\n";
143 close(MCONFIG);
144
145 # Output the filesystem offset to stdout if appropriate
146 if ( $opt{'o'} ) {
147     print $offset, "\n";
148 }
149
150 $ENV{'MTOOLSRC'} = $cfgfile;
151 system('mformat', 'z:');
152
153 # Clean up in /tmp
154 unlink($cfgfile);
155 unlink($imglink);
156 rmdir($tmpdir);
157
158 seek(OUTPUT, $s*512+0x36, 0);
159 read(OUTPUT, $fsname, 8);
160
161 # FAT12: adjust partition type
162 if ( $fsname eq 'FAT12   ' ) {
163     $fstype = 0x01;
164 }
165 seek(OUTPUT, 446+4, 0);
166 print OUTPUT pack("C", $fstype);
167
168 exit 0;
169 __END__