1*c2c66affSColin Finck /*
2*c2c66affSColin Finck    Filename:     mkdosfs.c
3*c2c66affSColin Finck    Version:      0.3b (Yggdrasil)
4*c2c66affSColin Finck    Author:       Dave Hudson
5*c2c66affSColin Finck    Started:      24th August 1994
6*c2c66affSColin Finck    Last Updated: 7th May 1998
7*c2c66affSColin Finck    Updated by:   Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
8*c2c66affSColin Finck    Target O/S:   Linux (2.x)
9*c2c66affSColin Finck 
10*c2c66affSColin Finck    Description: Utility to allow an MS-DOS filesystem to be created
11*c2c66affSColin Finck    under Linux.  A lot of the basic structure of this program has been
12*c2c66affSColin Finck    borrowed from Remy Card's "mke2fs" code.
13*c2c66affSColin Finck 
14*c2c66affSColin Finck    As far as possible the aim here is to make the "mkdosfs" command
15*c2c66affSColin Finck    look almost identical to the other Linux filesystem make utilties,
16*c2c66affSColin Finck    eg bad blocks are still specified as blocks, not sectors, but when
17*c2c66affSColin Finck    it comes down to it, DOS is tied to the idea of a sector (512 bytes
18*c2c66affSColin Finck    as a rule), and not the block.  For example the boot block does not
19*c2c66affSColin Finck    occupy a full cluster.
20*c2c66affSColin Finck 
21*c2c66affSColin Finck    Fixes/additions May 1998 by Roman Hodek
22*c2c66affSColin Finck    <Roman.Hodek@informatik.uni-erlangen.de>:
23*c2c66affSColin Finck    - Atari format support
24*c2c66affSColin Finck    - New options -A, -S, -C
25*c2c66affSColin Finck    - Support for filesystems > 2GB
26*c2c66affSColin Finck    - FAT32 support
27*c2c66affSColin Finck 
28*c2c66affSColin Finck    Port to work under Windows NT/2K/XP Dec 2002 by
29*c2c66affSColin Finck    Jens-Uwe Mager <jum@anubis.han.de>
30*c2c66affSColin Finck 
31*c2c66affSColin Finck    Copying:     Copyright 1993, 1994 David Hudson (dave@humbug.demon.co.uk)
32*c2c66affSColin Finck 
33*c2c66affSColin Finck    Portions copyright 1992, 1993 Remy Card (card@masi.ibp.fr)
34*c2c66affSColin Finck    and 1991 Linus Torvalds (torvalds@klaava.helsinki.fi)
35*c2c66affSColin Finck 
36*c2c66affSColin Finck    This program is free software; you can redistribute it and/or modify
37*c2c66affSColin Finck    it under the terms of the GNU General Public License as published by
38*c2c66affSColin Finck    the Free Software Foundation; either version 2, or (at your option)
39*c2c66affSColin Finck    any later version.
40*c2c66affSColin Finck 
41*c2c66affSColin Finck    This program is distributed in the hope that it will be useful,
42*c2c66affSColin Finck    but WITHOUT ANY WARRANTY; without even the implied warranty of
43*c2c66affSColin Finck    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44*c2c66affSColin Finck    GNU General Public License for more details.
45*c2c66affSColin Finck 
46*c2c66affSColin Finck    You should have received a copy of the GNU General Public License
47*c2c66affSColin Finck    along with this program; if not, write to the Free Software
48*c2c66affSColin Finck    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
49*c2c66affSColin Finck 
50*c2c66affSColin Finck 
51*c2c66affSColin Finck /* Include the header files */
52*c2c66affSColin Finck 
53*c2c66affSColin Finck #include "../version.h"
54*c2c66affSColin Finck 
55*c2c66affSColin Finck #ifdef _WIN32
56*c2c66affSColin Finck #define _WIN32_WINNT	0x0400
57*c2c66affSColin Finck #include <windows.h>
58*c2c66affSColin Finck #include <winioctl.h>
59*c2c66affSColin Finck #define __LITTLE_ENDIAN	1234
60*c2c66affSColin Finck #define __BIG_ENDIAN	4321
61*c2c66affSColin Finck #define __BYTE_ORDER	__LITTLE_ENDIAN
62*c2c66affSColin Finck #define inline
63*c2c66affSColin Finck #define __attribute__(x)
64*c2c66affSColin Finck #define BLOCK_SIZE		512
65*c2c66affSColin Finck #else
66*c2c66affSColin Finck #include <linux/hdreg.h>
67*c2c66affSColin Finck #include <linux/fs.h>
68*c2c66affSColin Finck #include <linux/fd.h>
69*c2c66affSColin Finck #include <endian.h>
70*c2c66affSColin Finck #include <mntent.h>
71*c2c66affSColin Finck #include <signal.h>
72*c2c66affSColin Finck #include <sys/ioctl.h>
73*c2c66affSColin Finck #include <unistd.h>
74*c2c66affSColin Finck #endif
75*c2c66affSColin Finck #include <fcntl.h>
76*c2c66affSColin Finck #include <string.h>
77*c2c66affSColin Finck #include <stdio.h>
78*c2c66affSColin Finck #include <stdlib.h>
79*c2c66affSColin Finck #include <sys/stat.h>
80*c2c66affSColin Finck #include <sys/types.h>
81*c2c66affSColin Finck #include <time.h>
82*c2c66affSColin Finck 
83*c2c66affSColin Finck #if __BYTE_ORDER == __BIG_ENDIAN
84*c2c66affSColin Finck 
85*c2c66affSColin Finck #include <asm/byteorder.h>
86*c2c66affSColin Finck #ifdef __le16_to_cpu
87*c2c66affSColin Finck /* ++roman: 2.1 kernel headers define these function, they're probably more
88*c2c66affSColin Finck  * efficient then coding the swaps machine-independently. */
89*c2c66affSColin Finck #define CF_LE_W	__le16_to_cpu
90*c2c66affSColin Finck #define CF_LE_L	__le32_to_cpu
91*c2c66affSColin Finck #define CT_LE_W	__cpu_to_le16
92*c2c66affSColin Finck #define CT_LE_L	__cpu_to_le32
93*c2c66affSColin Finck #else
94*c2c66affSColin Finck #define CF_LE_W(v) ((((v) & 0xff) << 8) | (((v) >> 8) & 0xff))
95*c2c66affSColin Finck #define CF_LE_L(v) (((unsigned)(v)>>24) | (((unsigned)(v)>>8)&0xff00) | \
96*c2c66affSColin Finck                (((unsigned)(v)<<8)&0xff0000) | ((unsigned)(v)<<24))
97*c2c66affSColin Finck #define CT_LE_W(v) CF_LE_W(v)
98*c2c66affSColin Finck #define CT_LE_L(v) CF_LE_L(v)
99*c2c66affSColin Finck #endif /* defined(__le16_to_cpu) */
100*c2c66affSColin Finck 
101*c2c66affSColin Finck #else
102*c2c66affSColin Finck 
103*c2c66affSColin Finck #define CF_LE_W(v) (v)
104*c2c66affSColin Finck #define CF_LE_L(v) (v)
105*c2c66affSColin Finck #define CT_LE_W(v) (v)
106*c2c66affSColin Finck #define CT_LE_L(v) (v)
107*c2c66affSColin Finck 
108*c2c66affSColin Finck #endif /* __BIG_ENDIAN */
109*c2c66affSColin Finck 
110*c2c66affSColin Finck #ifdef _WIN32
111*c2c66affSColin Finck 
112*c2c66affSColin Finck typedef unsigned char __u8;
113*c2c66affSColin Finck typedef unsigned short __u16;
114*c2c66affSColin Finck typedef unsigned int __u32;
115*c2c66affSColin Finck typedef unsigned __int64 __u64;
116*c2c66affSColin Finck typedef __int64 loff_t;
117*c2c66affSColin Finck typedef __int64 ll_t;
118*c2c66affSColin Finck 
119*c2c66affSColin Finck extern char *optarg;
120*c2c66affSColin Finck extern int optind;
121*c2c66affSColin Finck extern int opterr;
122*c2c66affSColin Finck extern int optopt;
123*c2c66affSColin Finck int getopt(int argc, char *const argv[], const char * optstring);
124*c2c66affSColin Finck 
125*c2c66affSColin Finck static int is_device = 0;
126*c2c66affSColin Finck 
127*c2c66affSColin Finck #define open	WIN32open
128*c2c66affSColin Finck #define close	WIN32close
129*c2c66affSColin Finck #define read	WIN32read
130*c2c66affSColin Finck #define write	WIN32write
131*c2c66affSColin Finck #define llseek	WIN32llseek
132*c2c66affSColin Finck 
133*c2c66affSColin Finck #define O_SHORT_LIVED   _O_SHORT_LIVED
134*c2c66affSColin Finck #define O_ACCMODE       3
135*c2c66affSColin Finck #define O_NONE          3
136*c2c66affSColin Finck #define O_BACKUP        0x10000
137*c2c66affSColin Finck #define O_SHARED        0x20000
138*c2c66affSColin Finck 
WIN32open(const char * path,int oflag,...)139*c2c66affSColin Finck static int WIN32open(const char *path, int oflag, ...)
140*c2c66affSColin Finck {
141*c2c66affSColin Finck 	HANDLE fh;
142*c2c66affSColin Finck 	DWORD desiredAccess;
143*c2c66affSColin Finck 	DWORD shareMode;
144*c2c66affSColin Finck 	DWORD creationDisposition;
145*c2c66affSColin Finck 	DWORD flagsAttributes = FILE_ATTRIBUTE_NORMAL;
146*c2c66affSColin Finck 	SECURITY_ATTRIBUTES securityAttributes;
147*c2c66affSColin Finck 	va_list ap;
148*c2c66affSColin Finck 	int pmode;
149*c2c66affSColin Finck 	int trunc = FALSE;
150*c2c66affSColin Finck 
151*c2c66affSColin Finck 	securityAttributes.nLength = sizeof(securityAttributes);
152*c2c66affSColin Finck 	securityAttributes.lpSecurityDescriptor = NULL;
153*c2c66affSColin Finck 	securityAttributes.bInheritHandle = oflag & O_NOINHERIT ? FALSE : TRUE;
154*c2c66affSColin Finck 	switch (oflag & O_ACCMODE) {
155*c2c66affSColin Finck 	case O_RDONLY:
156*c2c66affSColin Finck 		desiredAccess = GENERIC_READ;
157*c2c66affSColin Finck 		shareMode = FILE_SHARE_READ;
158*c2c66affSColin Finck 		break;
159*c2c66affSColin Finck 	case O_WRONLY:
160*c2c66affSColin Finck 		desiredAccess = GENERIC_WRITE;
161*c2c66affSColin Finck 		shareMode = 0;
162*c2c66affSColin Finck 		break;
163*c2c66affSColin Finck 	case O_RDWR:
164*c2c66affSColin Finck 		desiredAccess = GENERIC_READ|GENERIC_WRITE;
165*c2c66affSColin Finck 		shareMode = 0;
166*c2c66affSColin Finck 		break;
167*c2c66affSColin Finck 	case O_NONE:
168*c2c66affSColin Finck 		desiredAccess = 0;
169*c2c66affSColin Finck 		shareMode = FILE_SHARE_READ|FILE_SHARE_WRITE;
170*c2c66affSColin Finck 	}
171*c2c66affSColin Finck 	if (oflag & O_APPEND) {
172*c2c66affSColin Finck 		desiredAccess |= FILE_APPEND_DATA|SYNCHRONIZE;
173*c2c66affSColin Finck 		shareMode = FILE_SHARE_READ|FILE_SHARE_WRITE;
174*c2c66affSColin Finck 	}
175*c2c66affSColin Finck 	if (oflag & O_SHARED)
176*c2c66affSColin Finck 		shareMode |= FILE_SHARE_READ|FILE_SHARE_WRITE;
177*c2c66affSColin Finck         switch (oflag & (O_CREAT|O_EXCL|O_TRUNC)) {
178*c2c66affSColin Finck 	case 0:
179*c2c66affSColin Finck 	case O_EXCL:
180*c2c66affSColin Finck 		creationDisposition = OPEN_EXISTING;
181*c2c66affSColin Finck 		break;
182*c2c66affSColin Finck 	case O_CREAT:
183*c2c66affSColin Finck 		creationDisposition = OPEN_ALWAYS;
184*c2c66affSColin Finck 		break;
185*c2c66affSColin Finck 	case O_CREAT|O_EXCL:
186*c2c66affSColin Finck 	case O_CREAT|O_TRUNC|O_EXCL:
187*c2c66affSColin Finck 		creationDisposition = CREATE_NEW;
188*c2c66affSColin Finck 		break;
189*c2c66affSColin Finck 	case O_TRUNC:
190*c2c66affSColin Finck 	case O_TRUNC|O_EXCL:
191*c2c66affSColin Finck 		creationDisposition = TRUNCATE_EXISTING;
192*c2c66affSColin Finck 		break;
193*c2c66affSColin Finck 	case O_CREAT|O_TRUNC:
194*c2c66affSColin Finck 		creationDisposition = OPEN_ALWAYS;
195*c2c66affSColin Finck 		trunc = TRUE;
196*c2c66affSColin Finck 		break;
197*c2c66affSColin Finck         }
198*c2c66affSColin Finck 	if (oflag & O_CREAT) {
199*c2c66affSColin Finck 		va_start(ap, oflag);
200*c2c66affSColin Finck 		pmode = va_arg(ap, int);
201*c2c66affSColin Finck 		va_end(ap);
202*c2c66affSColin Finck 		if ((pmode & 0222) == 0)
203*c2c66affSColin Finck 			flagsAttributes |= FILE_ATTRIBUTE_READONLY;
204*c2c66affSColin Finck 	}
205*c2c66affSColin Finck 	if (oflag & O_TEMPORARY) {
206*c2c66affSColin Finck 		flagsAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
207*c2c66affSColin Finck 		desiredAccess |= DELETE;
208*c2c66affSColin Finck 	}
209*c2c66affSColin Finck 	if (oflag & O_SHORT_LIVED)
210*c2c66affSColin Finck 		flagsAttributes |= FILE_ATTRIBUTE_TEMPORARY;
211*c2c66affSColin Finck 	if (oflag & O_SEQUENTIAL)
212*c2c66affSColin Finck 		flagsAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
213*c2c66affSColin Finck 	else if (oflag & O_RANDOM)
214*c2c66affSColin Finck 		flagsAttributes |= FILE_FLAG_RANDOM_ACCESS;
215*c2c66affSColin Finck 	if (oflag & O_BACKUP)
216*c2c66affSColin Finck 		flagsAttributes |= FILE_FLAG_BACKUP_SEMANTICS;
217*c2c66affSColin Finck 	if ((fh = CreateFile(path, desiredAccess, shareMode, &securityAttributes,
218*c2c66affSColin Finck 				creationDisposition, flagsAttributes, NULL)) == INVALID_HANDLE_VALUE) {
219*c2c66affSColin Finck 		errno = GetLastError();
220*c2c66affSColin Finck 		return -1;
221*c2c66affSColin Finck 	}
222*c2c66affSColin Finck 	if (trunc) {
223*c2c66affSColin Finck 		if (!SetEndOfFile(fh)) {
224*c2c66affSColin Finck 			errno = GetLastError();
225*c2c66affSColin Finck 			CloseHandle(fh);
226*c2c66affSColin Finck 			DeleteFile(path);
227*c2c66affSColin Finck 			return -1;
228*c2c66affSColin Finck 		}
229*c2c66affSColin Finck 	}
230*c2c66affSColin Finck 	return (int)fh;
231*c2c66affSColin Finck }
232*c2c66affSColin Finck 
WIN32close(int fd)233*c2c66affSColin Finck static int WIN32close(int fd)
234*c2c66affSColin Finck {
235*c2c66affSColin Finck 	if (!CloseHandle((HANDLE)fd)) {
236*c2c66affSColin Finck 		errno = GetLastError();
237*c2c66affSColin Finck 		return -1;
238*c2c66affSColin Finck 	}
239*c2c66affSColin Finck 	return 0;
240*c2c66affSColin Finck }
241*c2c66affSColin Finck 
WIN32read(int fd,void * buf,unsigned int len)242*c2c66affSColin Finck static int WIN32read(int fd, void *buf, unsigned int len)
243*c2c66affSColin Finck {
244*c2c66affSColin Finck 	DWORD actualLen;
245*c2c66affSColin Finck 
246*c2c66affSColin Finck 	if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &actualLen, NULL)) {
247*c2c66affSColin Finck 		errno = GetLastError();
248*c2c66affSColin Finck 		if (errno == ERROR_BROKEN_PIPE)
249*c2c66affSColin Finck 			return 0;
250*c2c66affSColin Finck 		else
251*c2c66affSColin Finck 			return -1;
252*c2c66affSColin Finck 	}
253*c2c66affSColin Finck 	return (int)actualLen;
254*c2c66affSColin Finck }
255*c2c66affSColin Finck 
WIN32write(int fd,void * buf,unsigned int len)256*c2c66affSColin Finck static int WIN32write(int fd, void *buf, unsigned int len)
257*c2c66affSColin Finck {
258*c2c66affSColin Finck 	DWORD actualLen;
259*c2c66affSColin Finck 
260*c2c66affSColin Finck 	if (!WriteFile((HANDLE)fd, buf, (DWORD)len, &actualLen, NULL)) {
261*c2c66affSColin Finck 		errno = GetLastError();
262*c2c66affSColin Finck 		return -1;
263*c2c66affSColin Finck 	}
264*c2c66affSColin Finck 	return (int)actualLen;
265*c2c66affSColin Finck }
266*c2c66affSColin Finck 
WIN32llseek(int fd,loff_t offset,int whence)267*c2c66affSColin Finck static loff_t WIN32llseek(int fd, loff_t offset, int whence)
268*c2c66affSColin Finck {
269*c2c66affSColin Finck 	long lo, hi;
270*c2c66affSColin Finck 	DWORD err;
271*c2c66affSColin Finck 
272*c2c66affSColin Finck 	lo = offset & 0xffffffff;
273*c2c66affSColin Finck 	hi = offset >> 32;
274*c2c66affSColin Finck 	lo = SetFilePointer((HANDLE)fd, lo, &hi, whence);
275*c2c66affSColin Finck 	if (lo == 0xFFFFFFFF && (err = GetLastError()) != NO_ERROR) {
276*c2c66affSColin Finck 		errno = err;
277*c2c66affSColin Finck 		return -1;
278*c2c66affSColin Finck 	}
279*c2c66affSColin Finck 	return ((loff_t)hi << 32) | (off_t)lo;
280*c2c66affSColin Finck }
281*c2c66affSColin Finck 
fsctl(int fd,int code)282*c2c66affSColin Finck int fsctl(int fd, int code)
283*c2c66affSColin Finck {
284*c2c66affSColin Finck 	DWORD ret;
285*c2c66affSColin Finck 	if (!DeviceIoControl((HANDLE)fd, code, NULL, 0, NULL, 0, &ret, NULL)) {
286*c2c66affSColin Finck 		errno = GetLastError();
287*c2c66affSColin Finck 		return -1;
288*c2c66affSColin Finck 	}
289*c2c66affSColin Finck 	return 0;
290*c2c66affSColin Finck }
291*c2c66affSColin Finck 
292*c2c66affSColin Finck #else
293*c2c66affSColin Finck 
294*c2c66affSColin Finck #define O_NOINHERIT    0
295*c2c66affSColin Finck #define O_TEMPORARY    0
296*c2c66affSColin Finck #define O_SHORT_LIVED  0
297*c2c66affSColin Finck #define O_SEQUENTIAL   0
298*c2c66affSColin Finck #define O_RANDOM       0
299*c2c66affSColin Finck #define O_BACKUP       0
300*c2c66affSColin Finck #define O_SHARED       0
301*c2c66affSColin Finck #ifndef O_NONE
302*c2c66affSColin Finck # define O_NONE        0
303*c2c66affSColin Finck #endif
304*c2c66affSColin Finck 
305*c2c66affSColin Finck typedef long long ll_t;
306*c2c66affSColin Finck /* Use the _llseek system call directly, because there (once?) was a bug in
307*c2c66affSColin Finck  * the glibc implementation of it. */
308*c2c66affSColin Finck #include <linux/unistd.h>
309*c2c66affSColin Finck #if defined(__alpha) || defined(__ia64__)
310*c2c66affSColin Finck /* On alpha, the syscall is simply lseek, because it's a 64 bit system. */
llseek(int fd,loff_t offset,int whence)311*c2c66affSColin Finck static loff_t llseek( int fd, loff_t offset, int whence )
312*c2c66affSColin Finck {
313*c2c66affSColin Finck     return lseek(fd, offset, whence);
314*c2c66affSColin Finck }
315*c2c66affSColin Finck #else
316*c2c66affSColin Finck # ifndef __NR__llseek
317*c2c66affSColin Finck # error _llseek system call not present
318*c2c66affSColin Finck # endif
319*c2c66affSColin Finck static _syscall5( int, _llseek, uint, fd, ulong, hi, ulong, lo,
320*c2c66affSColin Finck 		  loff_t *, res, uint, wh );
llseek(int fd,loff_t offset,int whence)321*c2c66affSColin Finck static loff_t llseek( int fd, loff_t offset, int whence )
322*c2c66affSColin Finck {
323*c2c66affSColin Finck     loff_t actual;
324*c2c66affSColin Finck 
325*c2c66affSColin Finck     if (_llseek(fd, offset>>32, offset&0xffffffff, &actual, whence) != 0)
326*c2c66affSColin Finck 	return (loff_t)-1;
327*c2c66affSColin Finck     return actual;
328*c2c66affSColin Finck }
329*c2c66affSColin Finck #endif
330*c2c66affSColin Finck 
331*c2c66affSColin Finck #endif
332*c2c66affSColin Finck 
333*c2c66affSColin Finck /* Constant definitions */
334*c2c66affSColin Finck 
335*c2c66affSColin Finck #define TRUE 1			/* Boolean constants */
336*c2c66affSColin Finck #define FALSE 0
337*c2c66affSColin Finck 
338*c2c66affSColin Finck #define TEST_BUFFER_BLOCKS 16
339*c2c66affSColin Finck #define HARD_SECTOR_SIZE   512
340*c2c66affSColin Finck #define SECTORS_PER_BLOCK ( BLOCK_SIZE / HARD_SECTOR_SIZE )
341*c2c66affSColin Finck 
342*c2c66affSColin Finck 
343*c2c66affSColin Finck /* Macro definitions */
344*c2c66affSColin Finck 
345*c2c66affSColin Finck /* Report a failure message and return a failure error code */
346*c2c66affSColin Finck 
347*c2c66affSColin Finck #define die( str ) fatal_error( "%s: " str "\n" )
348*c2c66affSColin Finck 
349*c2c66affSColin Finck 
350*c2c66affSColin Finck /* Mark a cluster in the FAT as bad */
351*c2c66affSColin Finck 
352*c2c66affSColin Finck #define mark_sector_bad( sector ) mark_FAT_sector( sector, FAT_BAD )
353*c2c66affSColin Finck 
354*c2c66affSColin Finck /* Compute ceil(a/b) */
355*c2c66affSColin Finck 
356*c2c66affSColin Finck inline int
cdiv(int a,int b)357*c2c66affSColin Finck cdiv (int a, int b)
358*c2c66affSColin Finck {
359*c2c66affSColin Finck   return (a + b - 1) / b;
360*c2c66affSColin Finck }
361*c2c66affSColin Finck 
362*c2c66affSColin Finck /* MS-DOS filesystem structures -- I included them here instead of
363*c2c66affSColin Finck    including linux/msdos_fs.h since that doesn't include some fields we
364*c2c66affSColin Finck    need */
365*c2c66affSColin Finck 
366*c2c66affSColin Finck #define ATTR_RO      1		/* read-only */
367*c2c66affSColin Finck #define ATTR_HIDDEN  2		/* hidden */
368*c2c66affSColin Finck #define ATTR_SYS     4		/* system */
369*c2c66affSColin Finck #define ATTR_VOLUME  8		/* volume label */
370*c2c66affSColin Finck #define ATTR_DIR     16		/* directory */
371*c2c66affSColin Finck #define ATTR_ARCH    32		/* archived */
372*c2c66affSColin Finck 
373*c2c66affSColin Finck #define ATTR_NONE    0		/* no attribute bits */
374*c2c66affSColin Finck #define ATTR_UNUSED  (ATTR_VOLUME | ATTR_ARCH | ATTR_SYS | ATTR_HIDDEN)
375*c2c66affSColin Finck 	/* attribute bits that are copied "as is" */
376*c2c66affSColin Finck 
377*c2c66affSColin Finck /* FAT values */
378*c2c66affSColin Finck #define FAT_EOF      (atari_format ? 0x0fffffff : 0x0ffffff8)
379*c2c66affSColin Finck #define FAT_BAD      0x0ffffff7
380*c2c66affSColin Finck 
381*c2c66affSColin Finck #define MSDOS_EXT_SIGN 0x29	/* extended boot sector signature */
382*c2c66affSColin Finck #define MSDOS_FAT12_SIGN "FAT12   "	/* FAT12 filesystem signature */
383*c2c66affSColin Finck #define MSDOS_FAT16_SIGN "FAT16   "	/* FAT16 filesystem signature */
384*c2c66affSColin Finck #define MSDOS_FAT32_SIGN "FAT32   "	/* FAT32 filesystem signature */
385*c2c66affSColin Finck 
386*c2c66affSColin Finck #define BOOT_SIGN 0xAA55	/* Boot sector magic number */
387*c2c66affSColin Finck 
388*c2c66affSColin Finck #define MAX_CLUST_12	((1 << 12) - 16)
389*c2c66affSColin Finck #define MAX_CLUST_16	((1 << 16) - 16)
390*c2c66affSColin Finck /* M$ says the high 4 bits of a FAT32 FAT entry are reserved and don't belong
391*c2c66affSColin Finck  * to the cluster number. So the max. cluster# is based on 2^28 */
392*c2c66affSColin Finck #define MAX_CLUST_32	((1 << 28) - 16)
393*c2c66affSColin Finck 
394*c2c66affSColin Finck #define FAT12_THRESHOLD	4078
395*c2c66affSColin Finck 
396*c2c66affSColin Finck #define OLDGEMDOS_MAX_SECTORS	32765
397*c2c66affSColin Finck #define GEMDOS_MAX_SECTORS	65531
398*c2c66affSColin Finck #define GEMDOS_MAX_SECTOR_SIZE	(16*1024)
399*c2c66affSColin Finck 
400*c2c66affSColin Finck #define BOOTCODE_SIZE		448
401*c2c66affSColin Finck #define BOOTCODE_FAT32_SIZE	420
402*c2c66affSColin Finck 
403*c2c66affSColin Finck /* __attribute__ ((packed)) is used on all structures to make gcc ignore any
404*c2c66affSColin Finck  * alignments */
405*c2c66affSColin Finck 
406*c2c66affSColin Finck #ifdef _WIN32
407*c2c66affSColin Finck #pragma pack(push, 1)
408*c2c66affSColin Finck #endif
409*c2c66affSColin Finck struct msdos_volume_info {
410*c2c66affSColin Finck   __u8		drive_number;	/* BIOS drive number */
411*c2c66affSColin Finck   __u8		RESERVED;	/* Unused */
412*c2c66affSColin Finck   __u8		ext_boot_sign;	/* 0x29 if fields below exist (DOS 3.3+) */
413*c2c66affSColin Finck   __u8		volume_id[4];	/* Volume ID number */
414*c2c66affSColin Finck   __u8		volume_label[11];/* Volume label */
415*c2c66affSColin Finck   __u8		fs_type[8];	/* Typically FAT12 or FAT16 */
416*c2c66affSColin Finck } __attribute__ ((packed));
417*c2c66affSColin Finck 
418*c2c66affSColin Finck struct msdos_boot_sector
419*c2c66affSColin Finck {
420*c2c66affSColin Finck   __u8	        boot_jump[3];	/* Boot strap short or near jump */
421*c2c66affSColin Finck   __u8          system_id[8];	/* Name - can be used to special case
422*c2c66affSColin Finck 				   partition manager volumes */
423*c2c66affSColin Finck   __u8          sector_size[2];	/* bytes per logical sector */
424*c2c66affSColin Finck   __u8          cluster_size;	/* sectors/cluster */
425*c2c66affSColin Finck   __u16         reserved;	/* reserved sectors */
426*c2c66affSColin Finck   __u8          fats;		/* number of FATs */
427*c2c66affSColin Finck   __u8          dir_entries[2];	/* root directory entries */
428*c2c66affSColin Finck   __u8          sectors[2];	/* number of sectors */
429*c2c66affSColin Finck   __u8          media;		/* media code (unused) */
430*c2c66affSColin Finck   __u16         fat_length;	/* sectors/FAT */
431*c2c66affSColin Finck   __u16         secs_track;	/* sectors per track */
432*c2c66affSColin Finck   __u16         heads;		/* number of heads */
433*c2c66affSColin Finck   __u32         hidden;		/* hidden sectors (unused) */
434*c2c66affSColin Finck   __u32         total_sect;	/* number of sectors (if sectors == 0) */
435*c2c66affSColin Finck   union {
436*c2c66affSColin Finck     struct {
437*c2c66affSColin Finck       struct msdos_volume_info vi;
438*c2c66affSColin Finck       __u8	boot_code[BOOTCODE_SIZE];
439*c2c66affSColin Finck     } __attribute__ ((packed)) _oldfat;
440*c2c66affSColin Finck     struct {
441*c2c66affSColin Finck       __u32	fat32_length;	/* sectors/FAT */
442*c2c66affSColin Finck       __u16	flags;		/* bit 8: fat mirroring, low 4: active fat */
443*c2c66affSColin Finck       __u8	version[2];	/* major, minor filesystem version */
444*c2c66affSColin Finck       __u32	root_cluster;	/* first cluster in root directory */
445*c2c66affSColin Finck       __u16	info_sector;	/* filesystem info sector */
446*c2c66affSColin Finck       __u16	backup_boot;	/* backup boot sector */
447*c2c66affSColin Finck       __u16	reserved2[6];	/* Unused */
448*c2c66affSColin Finck       struct msdos_volume_info vi;
449*c2c66affSColin Finck       __u8	boot_code[BOOTCODE_FAT32_SIZE];
450*c2c66affSColin Finck     } __attribute__ ((packed)) _fat32;
451*c2c66affSColin Finck   } __attribute__ ((packed)) fstype;
452*c2c66affSColin Finck   __u16		boot_sign;
453*c2c66affSColin Finck } __attribute__ ((packed));
454*c2c66affSColin Finck #define fat32	fstype._fat32
455*c2c66affSColin Finck #define oldfat	fstype._oldfat
456*c2c66affSColin Finck 
457*c2c66affSColin Finck struct fat32_fsinfo {
458*c2c66affSColin Finck   __u32		reserved1;	/* Nothing as far as I can tell */
459*c2c66affSColin Finck   __u32		signature;	/* 0x61417272L */
460*c2c66affSColin Finck   __u32		free_clusters;	/* Free cluster count.  -1 if unknown */
461*c2c66affSColin Finck   __u32		next_cluster;	/* Most recently allocated cluster.
462*c2c66affSColin Finck 				 * Unused under Linux. */
463*c2c66affSColin Finck   __u32		reserved2[4];
464*c2c66affSColin Finck };
465*c2c66affSColin Finck 
466*c2c66affSColin Finck struct msdos_dir_entry
467*c2c66affSColin Finck   {
468*c2c66affSColin Finck     char	name[8], ext[3];	/* name and extension */
469*c2c66affSColin Finck     __u8        attr;			/* attribute bits */
470*c2c66affSColin Finck     __u8	lcase;			/* Case for base and extension */
471*c2c66affSColin Finck     __u8	ctime_ms;		/* Creation time, milliseconds */
472*c2c66affSColin Finck     __u16	ctime;			/* Creation time */
473*c2c66affSColin Finck     __u16	cdate;			/* Creation date */
474*c2c66affSColin Finck     __u16	adate;			/* Last access date */
475*c2c66affSColin Finck     __u16	starthi;		/* high 16 bits of first cl. (FAT32) */
476*c2c66affSColin Finck     __u16	time, date, start;	/* time, date and first cluster */
477*c2c66affSColin Finck     __u32	size;			/* file size (in bytes) */
478*c2c66affSColin Finck   } __attribute__ ((packed));
479*c2c66affSColin Finck 
480*c2c66affSColin Finck #ifdef _WIN32
481*c2c66affSColin Finck #pragma pack(pop)
482*c2c66affSColin Finck #endif
483*c2c66affSColin Finck 
484*c2c66affSColin Finck /* The "boot code" we put into the filesystem... it writes a message and
485*c2c66affSColin Finck    tells the user to try again */
486*c2c66affSColin Finck 
487*c2c66affSColin Finck char dummy_boot_jump[3] = { 0xeb, 0x3c, 0x90 };
488*c2c66affSColin Finck 
489*c2c66affSColin Finck char dummy_boot_jump_m68k[2] = { 0x60, 0x1c };
490*c2c66affSColin Finck 
491*c2c66affSColin Finck char dummy_boot_code[BOOTCODE_SIZE] =
492*c2c66affSColin Finck   "\x0e"			/* push cs */
493*c2c66affSColin Finck   "\x1f"			/* pop ds */
494*c2c66affSColin Finck   "\xbe\x5b\x7c"		/* mov si, offset message_txt */
495*c2c66affSColin Finck 				/* write_msg: */
496*c2c66affSColin Finck   "\xac"			/* lodsb */
497*c2c66affSColin Finck   "\x22\xc0"			/* and al, al */
498*c2c66affSColin Finck   "\x74\x0b"			/* jz key_press */
499*c2c66affSColin Finck   "\x56"			/* push si */
500*c2c66affSColin Finck   "\xb4\x0e"			/* mov ah, 0eh */
501*c2c66affSColin Finck   "\xbb\x07\x00"		/* mov bx, 0007h */
502*c2c66affSColin Finck   "\xcd\x10"			/* int 10h */
503*c2c66affSColin Finck   "\x5e"			/* pop si */
504*c2c66affSColin Finck   "\xeb\xf0"			/* jmp write_msg */
505*c2c66affSColin Finck 				/* key_press: */
506*c2c66affSColin Finck   "\x32\xe4"			/* xor ah, ah */
507*c2c66affSColin Finck   "\xcd\x16"			/* int 16h */
508*c2c66affSColin Finck   "\xcd\x19"			/* int 19h */
509*c2c66affSColin Finck   "\xeb\xfe"			/* foo: jmp foo */
510*c2c66affSColin Finck 				/* message_txt: */
511*c2c66affSColin Finck 
512*c2c66affSColin Finck   "This is not a bootable disk.  Please insert a bootable floppy and\r\n"
513*c2c66affSColin Finck   "press any key to try again ... \r\n";
514*c2c66affSColin Finck 
515*c2c66affSColin Finck #define MESSAGE_OFFSET 29	/* Offset of message in above code */
516*c2c66affSColin Finck 
517*c2c66affSColin Finck /* Global variables - the root of all evil :-) - see these and weep! */
518*c2c66affSColin Finck 
519*c2c66affSColin Finck static char *program_name = "mkdosfs";	/* Name of the program */
520*c2c66affSColin Finck static char *device_name = NULL;	/* Name of the device on which to create the filesystem */
521*c2c66affSColin Finck static int atari_format = 0;	/* Use Atari variation of MS-DOS FS format */
522*c2c66affSColin Finck static int check = FALSE;	/* Default to no readablity checking */
523*c2c66affSColin Finck static int verbose = 0;		/* Default to verbose mode off */
524*c2c66affSColin Finck static long volume_id;		/* Volume ID number */
525*c2c66affSColin Finck static time_t create_time;	/* Creation time */
526*c2c66affSColin Finck static char volume_name[] = "           "; /* Volume name */
527*c2c66affSColin Finck static int blocks;		/* Number of blocks in filesystem */
528*c2c66affSColin Finck static int sector_size = 512;	/* Size of a logical sector */
529*c2c66affSColin Finck static int sector_size_set = 0; /* User selected sector size */
530*c2c66affSColin Finck static int backup_boot = 0;	/* Sector# of backup boot sector */
531*c2c66affSColin Finck static int reserved_sectors = 0;/* Number of reserved sectors */
532*c2c66affSColin Finck static int badblocks = 0;	/* Number of bad blocks in the filesystem */
533*c2c66affSColin Finck static int nr_fats = 2;		/* Default number of FATs to produce */
534*c2c66affSColin Finck static int size_fat = 0;	/* Size in bits of FAT entries */
535*c2c66affSColin Finck static int size_fat_by_user = 0; /* 1 if FAT size user selected */
536*c2c66affSColin Finck static int dev = -1;		/* FS block device file handle */
537*c2c66affSColin Finck static int  ignore_full_disk = 0; /* Ignore warning about 'full' disk devices */
538*c2c66affSColin Finck static unsigned int currently_testing = 0;	/* Block currently being tested (if autodetect bad blocks) */
539*c2c66affSColin Finck static struct msdos_boot_sector bs;	/* Boot sector data */
540*c2c66affSColin Finck static int start_data_sector;	/* Sector number for the start of the data area */
541*c2c66affSColin Finck static int start_data_block;	/* Block number for the start of the data area */
542*c2c66affSColin Finck static unsigned char *fat;	/* File allocation table */
543*c2c66affSColin Finck static unsigned char *info_sector;	/* FAT32 info sector */
544*c2c66affSColin Finck static struct msdos_dir_entry *root_dir;	/* Root directory */
545*c2c66affSColin Finck static int size_root_dir;	/* Size of the root directory in bytes */
546*c2c66affSColin Finck static int sectors_per_cluster = 0;	/* Number of sectors per disk cluster */
547*c2c66affSColin Finck static int root_dir_entries = 0;	/* Number of root directory entries */
548*c2c66affSColin Finck static char *blank_sector;		/* Blank sector - all zeros */
549*c2c66affSColin Finck 
550*c2c66affSColin Finck 
551*c2c66affSColin Finck /* Function prototype definitions */
552*c2c66affSColin Finck 
553*c2c66affSColin Finck static void fatal_error (const char *fmt_string) __attribute__((noreturn));
554*c2c66affSColin Finck static void mark_FAT_cluster (int cluster, unsigned int value);
555*c2c66affSColin Finck static void mark_FAT_sector (int sector, unsigned int value);
556*c2c66affSColin Finck static long do_check (char *buffer, int try, unsigned int current_block);
557*c2c66affSColin Finck static void alarm_intr (int alnum);
558*c2c66affSColin Finck static void check_blocks (void);
559*c2c66affSColin Finck static void get_list_blocks (char *filename);
560*c2c66affSColin Finck static int valid_offset (int fd, loff_t offset);
561*c2c66affSColin Finck static int count_blocks (char *filename);
562*c2c66affSColin Finck static void check_mount (char *device_name);
563*c2c66affSColin Finck #ifdef _WIN32
564*c2c66affSColin Finck static void establish_params (void);
565*c2c66affSColin Finck #else
566*c2c66affSColin Finck static void establish_params (int device_num, int size);
567*c2c66affSColin Finck #endif
568*c2c66affSColin Finck static void setup_tables (void);
569*c2c66affSColin Finck static void write_tables (void);
570*c2c66affSColin Finck 
571*c2c66affSColin Finck 
572*c2c66affSColin Finck /* The function implementations */
573*c2c66affSColin Finck 
574*c2c66affSColin Finck /* Handle the reporting of fatal errors.  Volatile to let gcc know that this doesn't return */
575*c2c66affSColin Finck 
576*c2c66affSColin Finck static void
fatal_error(const char * fmt_string)577*c2c66affSColin Finck fatal_error (const char *fmt_string)
578*c2c66affSColin Finck {
579*c2c66affSColin Finck   fprintf (stderr, fmt_string, program_name, device_name);
580*c2c66affSColin Finck   exit (1);			/* The error exit code is 1! */
581*c2c66affSColin Finck }
582*c2c66affSColin Finck 
583*c2c66affSColin Finck 
584*c2c66affSColin Finck /* Mark the specified cluster as having a particular value */
585*c2c66affSColin Finck 
586*c2c66affSColin Finck static void
mark_FAT_cluster(int cluster,unsigned int value)587*c2c66affSColin Finck mark_FAT_cluster (int cluster, unsigned int value)
588*c2c66affSColin Finck {
589*c2c66affSColin Finck   switch( size_fat ) {
590*c2c66affSColin Finck     case 12:
591*c2c66affSColin Finck       value &= 0x0fff;
592*c2c66affSColin Finck       if (((cluster * 3) & 0x1) == 0)
593*c2c66affSColin Finck 	{
594*c2c66affSColin Finck 	  fat[3 * cluster / 2] = (unsigned char) (value & 0x00ff);
595*c2c66affSColin Finck 	  fat[(3 * cluster / 2) + 1] = (unsigned char) ((fat[(3 * cluster / 2) + 1] & 0x00f0)
596*c2c66affSColin Finck 						 | ((value & 0x0f00) >> 8));
597*c2c66affSColin Finck 	}
598*c2c66affSColin Finck       else
599*c2c66affSColin Finck 	{
600*c2c66affSColin Finck 	  fat[3 * cluster / 2] = (unsigned char) ((fat[3 * cluster / 2] & 0x000f) | ((value & 0x000f) << 4));
601*c2c66affSColin Finck 	  fat[(3 * cluster / 2) + 1] = (unsigned char) ((value & 0x0ff0) >> 4);
602*c2c66affSColin Finck 	}
603*c2c66affSColin Finck       break;
604*c2c66affSColin Finck 
605*c2c66affSColin Finck     case 16:
606*c2c66affSColin Finck       value &= 0xffff;
607*c2c66affSColin Finck       fat[2 * cluster] = (unsigned char) (value & 0x00ff);
608*c2c66affSColin Finck       fat[(2 * cluster) + 1] = (unsigned char) (value >> 8);
609*c2c66affSColin Finck       break;
610*c2c66affSColin Finck 
611*c2c66affSColin Finck     case 32:
612*c2c66affSColin Finck       value &= 0xfffffff;
613*c2c66affSColin Finck       fat[4 * cluster] =       (unsigned char)  (value & 0x000000ff);
614*c2c66affSColin Finck       fat[(4 * cluster) + 1] = (unsigned char) ((value & 0x0000ff00) >> 8);
615*c2c66affSColin Finck       fat[(4 * cluster) + 2] = (unsigned char) ((value & 0x00ff0000) >> 16);
616*c2c66affSColin Finck       fat[(4 * cluster) + 3] = (unsigned char) ((value & 0xff000000) >> 24);
617*c2c66affSColin Finck       break;
618*c2c66affSColin Finck 
619*c2c66affSColin Finck     default:
620*c2c66affSColin Finck       die("Bad FAT size (not 12, 16, or 32)");
621*c2c66affSColin Finck   }
622*c2c66affSColin Finck }
623*c2c66affSColin Finck 
624*c2c66affSColin Finck 
625*c2c66affSColin Finck /* Mark a specified sector as having a particular value in it's FAT entry */
626*c2c66affSColin Finck 
627*c2c66affSColin Finck static void
mark_FAT_sector(int sector,unsigned int value)628*c2c66affSColin Finck mark_FAT_sector (int sector, unsigned int value)
629*c2c66affSColin Finck {
630*c2c66affSColin Finck   int cluster;
631*c2c66affSColin Finck 
632*c2c66affSColin Finck   cluster = (sector - start_data_sector) / (int) (bs.cluster_size) /
633*c2c66affSColin Finck 	    (sector_size/HARD_SECTOR_SIZE);
634*c2c66affSColin Finck   if (cluster < 0)
635*c2c66affSColin Finck     die ("Invalid cluster number in mark_FAT_sector: probably bug!");
636*c2c66affSColin Finck 
637*c2c66affSColin Finck   mark_FAT_cluster (cluster, value);
638*c2c66affSColin Finck }
639*c2c66affSColin Finck 
640*c2c66affSColin Finck 
641*c2c66affSColin Finck /* Perform a test on a block.  Return the number of blocks that could be read successfully */
642*c2c66affSColin Finck 
643*c2c66affSColin Finck static long
do_check(char * buffer,int try,unsigned int current_block)644*c2c66affSColin Finck do_check (char *buffer, int try, unsigned int current_block)
645*c2c66affSColin Finck {
646*c2c66affSColin Finck   long got;
647*c2c66affSColin Finck 
648*c2c66affSColin Finck   if (llseek (dev, (loff_t)current_block * BLOCK_SIZE, SEEK_SET) /* Seek to the correct location */
649*c2c66affSColin Finck       != (loff_t)current_block * BLOCK_SIZE)
650*c2c66affSColin Finck     die ("seek failed during testing for blocks");
651*c2c66affSColin Finck 
652*c2c66affSColin Finck   got = read (dev, buffer, try * BLOCK_SIZE);	/* Try reading! */
653*c2c66affSColin Finck   if (got < 0)
654*c2c66affSColin Finck     got = 0;
655*c2c66affSColin Finck 
656*c2c66affSColin Finck   if (got & (BLOCK_SIZE - 1))
657*c2c66affSColin Finck     printf ("Unexpected values in do_check: probably bugs\n");
658*c2c66affSColin Finck   got /= BLOCK_SIZE;
659*c2c66affSColin Finck 
660*c2c66affSColin Finck   return got;
661*c2c66affSColin Finck }
662*c2c66affSColin Finck 
663*c2c66affSColin Finck #ifndef _WIN32
664*c2c66affSColin Finck /* Alarm clock handler - display the status of the quest for bad blocks!  Then retrigger the alarm for five senconds
665*c2c66affSColin Finck    later (so we can come here again) */
666*c2c66affSColin Finck 
667*c2c66affSColin Finck static void
alarm_intr(int alnum)668*c2c66affSColin Finck alarm_intr (int alnum)
669*c2c66affSColin Finck {
670*c2c66affSColin Finck   if (currently_testing >= blocks)
671*c2c66affSColin Finck     return;
672*c2c66affSColin Finck 
673*c2c66affSColin Finck   signal (SIGALRM, alarm_intr);
674*c2c66affSColin Finck   alarm (5);
675*c2c66affSColin Finck   if (!currently_testing)
676*c2c66affSColin Finck     return;
677*c2c66affSColin Finck 
678*c2c66affSColin Finck   printf ("%d... ", currently_testing);
679*c2c66affSColin Finck   fflush (stdout);
680*c2c66affSColin Finck }
681*c2c66affSColin Finck #endif
682*c2c66affSColin Finck 
683*c2c66affSColin Finck static void
check_blocks(void)684*c2c66affSColin Finck check_blocks (void)
685*c2c66affSColin Finck {
686*c2c66affSColin Finck   int try, got;
687*c2c66affSColin Finck   int i;
688*c2c66affSColin Finck   static char blkbuf[BLOCK_SIZE * TEST_BUFFER_BLOCKS];
689*c2c66affSColin Finck 
690*c2c66affSColin Finck   if (verbose)
691*c2c66affSColin Finck     {
692*c2c66affSColin Finck       printf ("Searching for bad blocks ");
693*c2c66affSColin Finck       fflush (stdout);
694*c2c66affSColin Finck     }
695*c2c66affSColin Finck   currently_testing = 0;
696*c2c66affSColin Finck #ifndef _WIN32
697*c2c66affSColin Finck   if (verbose)
698*c2c66affSColin Finck     {
699*c2c66affSColin Finck       signal (SIGALRM, alarm_intr);
700*c2c66affSColin Finck       alarm (5);
701*c2c66affSColin Finck     }
702*c2c66affSColin Finck #endif
703*c2c66affSColin Finck   try = TEST_BUFFER_BLOCKS;
704*c2c66affSColin Finck   while (currently_testing < blocks)
705*c2c66affSColin Finck     {
706*c2c66affSColin Finck       if (currently_testing + try > blocks)
707*c2c66affSColin Finck 	try = blocks - currently_testing;
708*c2c66affSColin Finck       got = do_check (blkbuf, try, currently_testing);
709*c2c66affSColin Finck       currently_testing += got;
710*c2c66affSColin Finck       if (got == try)
711*c2c66affSColin Finck 	{
712*c2c66affSColin Finck 	  try = TEST_BUFFER_BLOCKS;
713*c2c66affSColin Finck 	  continue;
714*c2c66affSColin Finck 	}
715*c2c66affSColin Finck       else
716*c2c66affSColin Finck 	try = 1;
717*c2c66affSColin Finck       if (currently_testing < start_data_block)
718*c2c66affSColin Finck 	die ("bad blocks before data-area: cannot make fs");
719*c2c66affSColin Finck 
720*c2c66affSColin Finck       for (i = 0; i < SECTORS_PER_BLOCK; i++)	/* Mark all of the sectors in the block as bad */
721*c2c66affSColin Finck 	mark_sector_bad (currently_testing * SECTORS_PER_BLOCK + i);
722*c2c66affSColin Finck       badblocks++;
723*c2c66affSColin Finck       currently_testing++;
724*c2c66affSColin Finck     }
725*c2c66affSColin Finck 
726*c2c66affSColin Finck   if (verbose)
727*c2c66affSColin Finck     printf ("\n");
728*c2c66affSColin Finck 
729*c2c66affSColin Finck   if (badblocks)
730*c2c66affSColin Finck     printf ("%d bad block%s\n", badblocks,
731*c2c66affSColin Finck 	    (badblocks > 1) ? "s" : "");
732*c2c66affSColin Finck }
733*c2c66affSColin Finck 
734*c2c66affSColin Finck 
735*c2c66affSColin Finck static void
get_list_blocks(char * filename)736*c2c66affSColin Finck get_list_blocks (char *filename)
737*c2c66affSColin Finck {
738*c2c66affSColin Finck   int i;
739*c2c66affSColin Finck   FILE *listfile;
740*c2c66affSColin Finck   unsigned long blockno;
741*c2c66affSColin Finck 
742*c2c66affSColin Finck   listfile = fopen (filename, "r");
743*c2c66affSColin Finck   if (listfile == (FILE *) NULL)
744*c2c66affSColin Finck     die ("Can't open file of bad blocks");
745*c2c66affSColin Finck 
746*c2c66affSColin Finck   while (!feof (listfile))
747*c2c66affSColin Finck     {
748*c2c66affSColin Finck       fscanf (listfile, "%ld\n", &blockno);
749*c2c66affSColin Finck       for (i = 0; i < SECTORS_PER_BLOCK; i++)	/* Mark all of the sectors in the block as bad */
750*c2c66affSColin Finck 	mark_sector_bad (blockno * SECTORS_PER_BLOCK + i);
751*c2c66affSColin Finck       badblocks++;
752*c2c66affSColin Finck     }
753*c2c66affSColin Finck   fclose (listfile);
754*c2c66affSColin Finck 
755*c2c66affSColin Finck   if (badblocks)
756*c2c66affSColin Finck     printf ("%d bad block%s\n", badblocks,
757*c2c66affSColin Finck 	    (badblocks > 1) ? "s" : "");
758*c2c66affSColin Finck }
759*c2c66affSColin Finck 
760*c2c66affSColin Finck 
761*c2c66affSColin Finck #ifndef _WIN32
762*c2c66affSColin Finck /* Given a file descriptor and an offset, check whether the offset is a valid offset for the file - return FALSE if it
763*c2c66affSColin Finck    isn't valid or TRUE if it is */
764*c2c66affSColin Finck 
765*c2c66affSColin Finck static int
valid_offset(int fd,loff_t offset)766*c2c66affSColin Finck valid_offset (int fd, loff_t offset)
767*c2c66affSColin Finck {
768*c2c66affSColin Finck   char ch;
769*c2c66affSColin Finck 
770*c2c66affSColin Finck   if (llseek (fd, offset, SEEK_SET) < 0)
771*c2c66affSColin Finck     return FALSE;
772*c2c66affSColin Finck   if (read (fd, &ch, 1) < 1)
773*c2c66affSColin Finck     return FALSE;
774*c2c66affSColin Finck   return TRUE;
775*c2c66affSColin Finck }
776*c2c66affSColin Finck #endif
777*c2c66affSColin Finck 
778*c2c66affSColin Finck 
779*c2c66affSColin Finck /* Given a filename, look to see how many blocks of BLOCK_SIZE are present, returning the answer */
780*c2c66affSColin Finck 
781*c2c66affSColin Finck static int
count_blocks(char * filename)782*c2c66affSColin Finck count_blocks (char *filename)
783*c2c66affSColin Finck {
784*c2c66affSColin Finck #ifdef _WIN32
785*c2c66affSColin Finck 	int fd;
786*c2c66affSColin Finck 	DISK_GEOMETRY geom;
787*c2c66affSColin Finck 	BY_HANDLE_FILE_INFORMATION hinfo;
788*c2c66affSColin Finck 	DWORD ret;
789*c2c66affSColin Finck 	loff_t len = 0;
790*c2c66affSColin Finck 
791*c2c66affSColin Finck 	if ((fd = open(filename, O_RDONLY)) < 0) {
792*c2c66affSColin Finck 		perror(filename);
793*c2c66affSColin Finck 		exit(1);
794*c2c66affSColin Finck 	}
795*c2c66affSColin Finck 	/*
796*c2c66affSColin Finck 	 * This should probably use IOCTL_DISK_GET_LENGTH_INFO here, but
797*c2c66affSColin Finck 	 * this ioctl is only available in XP and up.
798*c2c66affSColin Finck 	 */
799*c2c66affSColin Finck 	if (is_device) {
800*c2c66affSColin Finck 		if (!DeviceIoControl((HANDLE)fd, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geom, sizeof(geom), &ret, NULL)) {
801*c2c66affSColin Finck 			errno = GetLastError();
802*c2c66affSColin Finck 			die("unable to get length for '%s'");
803*c2c66affSColin Finck 		}
804*c2c66affSColin Finck 		len = geom.Cylinders.QuadPart*geom.TracksPerCylinder*geom.SectorsPerTrack*BLOCK_SIZE;
805*c2c66affSColin Finck 	} else {
806*c2c66affSColin Finck 		if (!GetFileInformationByHandle((HANDLE)fd, &hinfo)) {
807*c2c66affSColin Finck 				errno = GetLastError();
808*c2c66affSColin Finck 				die("unable to get length for '%s'");
809*c2c66affSColin Finck 		}
810*c2c66affSColin Finck 		len = ((loff_t)hinfo.nFileSizeHigh << 32) | (loff_t)hinfo.nFileSizeLow;
811*c2c66affSColin Finck 	}
812*c2c66affSColin Finck 	close(fd);
813*c2c66affSColin Finck 	return len/BLOCK_SIZE;
814*c2c66affSColin Finck #else
815*c2c66affSColin Finck   loff_t high, low;
816*c2c66affSColin Finck   int fd;
817*c2c66affSColin Finck 
818*c2c66affSColin Finck   if ((fd = open (filename, O_RDONLY)) < 0)
819*c2c66affSColin Finck     {
820*c2c66affSColin Finck       perror (filename);
821*c2c66affSColin Finck       exit (1);
822*c2c66affSColin Finck     }
823*c2c66affSColin Finck   low = 0;
824*c2c66affSColin Finck 
825*c2c66affSColin Finck   for (high = 1; valid_offset (fd, high); high *= 2)
826*c2c66affSColin Finck     low = high;
827*c2c66affSColin Finck   while (low < high - 1)
828*c2c66affSColin Finck     {
829*c2c66affSColin Finck       const loff_t mid = (low + high) / 2;
830*c2c66affSColin Finck 
831*c2c66affSColin Finck       if (valid_offset (fd, mid))
832*c2c66affSColin Finck 	low = mid;
833*c2c66affSColin Finck       else
834*c2c66affSColin Finck 	high = mid;
835*c2c66affSColin Finck     }
836*c2c66affSColin Finck   valid_offset (fd, 0);
837*c2c66affSColin Finck   close (fd);
838*c2c66affSColin Finck 
839*c2c66affSColin Finck   return (low + 1) / BLOCK_SIZE;
840*c2c66affSColin Finck #endif
841*c2c66affSColin Finck }
842*c2c66affSColin Finck 
843*c2c66affSColin Finck 
844*c2c66affSColin Finck /* Check to see if the specified device is currently mounted - abort if it is */
845*c2c66affSColin Finck 
846*c2c66affSColin Finck static void
check_mount(char * device_name)847*c2c66affSColin Finck check_mount (char *device_name)
848*c2c66affSColin Finck {
849*c2c66affSColin Finck #ifndef _WIN32
850*c2c66affSColin Finck   FILE *f;
851*c2c66affSColin Finck   struct mntent *mnt;
852*c2c66affSColin Finck 
853*c2c66affSColin Finck   if ((f = setmntent (MOUNTED, "r")) == NULL)
854*c2c66affSColin Finck     return;
855*c2c66affSColin Finck   while ((mnt = getmntent (f)) != NULL)
856*c2c66affSColin Finck     if (strcmp (device_name, mnt->mnt_fsname) == 0)
857*c2c66affSColin Finck       die ("%s contains a mounted file system.");
858*c2c66affSColin Finck   endmntent (f);
859*c2c66affSColin Finck #endif
860*c2c66affSColin Finck }
861*c2c66affSColin Finck 
862*c2c66affSColin Finck 
863*c2c66affSColin Finck /* Establish the geometry and media parameters for the device */
864*c2c66affSColin Finck #ifdef _WIN32
865*c2c66affSColin Finck static void
establish_params(void)866*c2c66affSColin Finck establish_params (void)
867*c2c66affSColin Finck {
868*c2c66affSColin Finck 	DISK_GEOMETRY geometry;
869*c2c66affSColin Finck 	DWORD ret;
870*c2c66affSColin Finck 
871*c2c66affSColin Finck 	if (!is_device) {
872*c2c66affSColin Finck 		bs.media = (char) 0xf8; /* Set up the media descriptor for a hard drive */
873*c2c66affSColin Finck 		bs.dir_entries[0] = (char) 0;
874*c2c66affSColin Finck 		bs.dir_entries[1] = (char) 2;
875*c2c66affSColin Finck 		/* For FAT32, use 4k clusters on sufficiently large file systems,
876*c2c66affSColin Finck 		 * otherwise 1 sector per cluster. This is also what M$'s format
877*c2c66affSColin Finck 		 * command does for FAT32. */
878*c2c66affSColin Finck 		bs.cluster_size = (char)
879*c2c66affSColin Finck 		 (size_fat == 32 ?
880*c2c66affSColin Finck 	     ((ll_t)blocks*SECTORS_PER_BLOCK >= 512*1024 ? 8 : 1) :
881*c2c66affSColin Finck 	      4); /* FAT12 and FAT16: start at 4 sectors per cluster */
882*c2c66affSColin Finck 		return;
883*c2c66affSColin Finck 	}
884*c2c66affSColin Finck 	if (!DeviceIoControl((HANDLE)dev, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geometry, sizeof(geometry), &ret, NULL)) {
885*c2c66affSColin Finck 		errno = GetLastError();
886*c2c66affSColin Finck 		die ("unable to get geometry for '%s'");
887*c2c66affSColin Finck 	}
888*c2c66affSColin Finck     bs.secs_track = geometry.SectorsPerTrack;
889*c2c66affSColin Finck     bs.heads = geometry.TracksPerCylinder;
890*c2c66affSColin Finck 	switch (geometry.MediaType) {
891*c2c66affSColin Finck 	case F3_1Pt44_512:
892*c2c66affSColin Finck 		bs.media = (char) 0xf9;
893*c2c66affSColin Finck 		bs.cluster_size = (char) 2;
894*c2c66affSColin Finck 		bs.dir_entries[0] = (char) 112;
895*c2c66affSColin Finck 		bs.dir_entries[1] = (char) 0;
896*c2c66affSColin Finck 		break;
897*c2c66affSColin Finck 	case F3_2Pt88_512:
898*c2c66affSColin Finck 		bs.media = (char) 0xf0;
899*c2c66affSColin Finck 		bs.cluster_size = (char)(atari_format ? 2 : 1);
900*c2c66affSColin Finck 		bs.dir_entries[0] = (char) 224;
901*c2c66affSColin Finck 		bs.dir_entries[1] = (char) 0;
902*c2c66affSColin Finck 		break;
903*c2c66affSColin Finck 	case F3_720_512:
904*c2c66affSColin Finck 		bs.media = (char) 0xfd;
905*c2c66affSColin Finck 		bs.cluster_size = (char) 2;
906*c2c66affSColin Finck 		bs.dir_entries[0] = (char) 112;
907*c2c66affSColin Finck 		bs.dir_entries[1] = (char) 0;
908*c2c66affSColin Finck 		break;
909*c2c66affSColin Finck 	default:
910*c2c66affSColin Finck 		bs.media = (char) 0xf8; /* Set up the media descriptor for a hard drive */
911*c2c66affSColin Finck 		bs.dir_entries[0] = (char) 0;
912*c2c66affSColin Finck 		bs.dir_entries[1] = (char) 2;
913*c2c66affSColin Finck 		/* For FAT32, use 4k clusters on sufficiently large file systems,
914*c2c66affSColin Finck 		 * otherwise 1 sector per cluster. This is also what M$'s format
915*c2c66affSColin Finck 		 * command does for FAT32. */
916*c2c66affSColin Finck 		bs.cluster_size = (char)
917*c2c66affSColin Finck 		 (size_fat == 32 ?
918*c2c66affSColin Finck 	     ((ll_t)blocks*SECTORS_PER_BLOCK >= 512*1024 ? 8 : 1) :
919*c2c66affSColin Finck 	      4); /* FAT12 and FAT16: start at 4 sectors per cluster */
920*c2c66affSColin Finck 	}
921*c2c66affSColin Finck }
922*c2c66affSColin Finck #else
923*c2c66affSColin Finck static void
establish_params(int device_num,int size)924*c2c66affSColin Finck establish_params (int device_num,int size)
925*c2c66affSColin Finck {
926*c2c66affSColin Finck   long loop_size;
927*c2c66affSColin Finck   struct hd_geometry geometry;
928*c2c66affSColin Finck   struct floppy_struct param;
929*c2c66affSColin Finck 
930*c2c66affSColin Finck   if ((0 == device_num) || ((device_num & 0xff00) == 0x0200))
931*c2c66affSColin Finck     /* file image or floppy disk */
932*c2c66affSColin Finck     {
933*c2c66affSColin Finck       if (0 == device_num)
934*c2c66affSColin Finck 	{
935*c2c66affSColin Finck 	  param.size = size/512;
936*c2c66affSColin Finck 	  switch(param.size)
937*c2c66affSColin Finck 	    {
938*c2c66affSColin Finck 	    case 720:
939*c2c66affSColin Finck 	      param.sect = 9 ;
940*c2c66affSColin Finck 	      param.head = 2;
941*c2c66affSColin Finck 	      break;
942*c2c66affSColin Finck 	    case 1440:
943*c2c66affSColin Finck 	      param.sect = 9;
944*c2c66affSColin Finck 	      param.head = 2;
945*c2c66affSColin Finck 	      break;
946*c2c66affSColin Finck 	    case 2400:
947*c2c66affSColin Finck 	      param.sect = 15;
948*c2c66affSColin Finck 	      param.head = 2;
949*c2c66affSColin Finck 	      break;
950*c2c66affSColin Finck 	    case 2880:
951*c2c66affSColin Finck 	      param.sect = 18;
952*c2c66affSColin Finck 	      param.head = 2;
953*c2c66affSColin Finck 	      break;
954*c2c66affSColin Finck 	    case 5760:
955*c2c66affSColin Finck 	      param.sect = 36;
956*c2c66affSColin Finck 	      param.head = 2;
957*c2c66affSColin Finck 	      break;
958*c2c66affSColin Finck 	    default:
959*c2c66affSColin Finck 	      /* fake values */
960*c2c66affSColin Finck 	      param.sect = 32;
961*c2c66affSColin Finck 	      param.head = 64;
962*c2c66affSColin Finck 	      break;
963*c2c66affSColin Finck 	    }
964*c2c66affSColin Finck 
965*c2c66affSColin Finck 	}
966*c2c66affSColin Finck       else 	/* is a floppy diskette */
967*c2c66affSColin Finck 	{
968*c2c66affSColin Finck 	  if (ioctl (dev, FDGETPRM, &param))	/*  Can we get the diskette geometry? */
969*c2c66affSColin Finck 	    die ("unable to get diskette geometry for '%s'");
970*c2c66affSColin Finck 	}
971*c2c66affSColin Finck       bs.secs_track = CT_LE_W(param.sect);	/*  Set up the geometry information */
972*c2c66affSColin Finck       bs.heads = CT_LE_W(param.head);
973*c2c66affSColin Finck       switch (param.size)	/*  Set up the media descriptor byte */
974*c2c66affSColin Finck 	{
975*c2c66affSColin Finck 	case 720:		/* 5.25", 2, 9, 40 - 360K */
976*c2c66affSColin Finck 	  bs.media = (char) 0xfd;
977*c2c66affSColin Finck 	  bs.cluster_size = (char) 2;
978*c2c66affSColin Finck 	  bs.dir_entries[0] = (char) 112;
979*c2c66affSColin Finck 	  bs.dir_entries[1] = (char) 0;
980*c2c66affSColin Finck 	  break;
981*c2c66affSColin Finck 
982*c2c66affSColin Finck 	case 1440:		/* 3.5", 2, 9, 80 - 720K */
983*c2c66affSColin Finck 	  bs.media = (char) 0xf9;
984*c2c66affSColin Finck 	  bs.cluster_size = (char) 2;
985*c2c66affSColin Finck 	  bs.dir_entries[0] = (char) 112;
986*c2c66affSColin Finck 	  bs.dir_entries[1] = (char) 0;
987*c2c66affSColin Finck 	  break;
988*c2c66affSColin Finck 
989*c2c66affSColin Finck 	case 2400:		/* 5.25", 2, 15, 80 - 1200K */
990*c2c66affSColin Finck 	  bs.media = (char) 0xf9;
991*c2c66affSColin Finck 	  bs.cluster_size = (char)(atari_format ? 2 : 1);
992*c2c66affSColin Finck 	  bs.dir_entries[0] = (char) 224;
993*c2c66affSColin Finck 	  bs.dir_entries[1] = (char) 0;
994*c2c66affSColin Finck 	  break;
995*c2c66affSColin Finck 
996*c2c66affSColin Finck 	case 5760:		/* 3.5", 2, 36, 80 - 2880K */
997*c2c66affSColin Finck 	  bs.media = (char) 0xf0;
998*c2c66affSColin Finck 	  bs.cluster_size = (char) 2;
999*c2c66affSColin Finck 	  bs.dir_entries[0] = (char) 224;
1000*c2c66affSColin Finck 	  bs.dir_entries[1] = (char) 0;
1001*c2c66affSColin Finck 	  break;
1002*c2c66affSColin Finck 
1003*c2c66affSColin Finck 	case 2880:		/* 3.5", 2, 18, 80 - 1440K */
1004*c2c66affSColin Finck 	floppy_default:
1005*c2c66affSColin Finck 	  bs.media = (char) 0xf0;
1006*c2c66affSColin Finck 	  bs.cluster_size = (char)(atari_format ? 2 : 1);
1007*c2c66affSColin Finck 	  bs.dir_entries[0] = (char) 224;
1008*c2c66affSColin Finck 	  bs.dir_entries[1] = (char) 0;
1009*c2c66affSColin Finck 	  break;
1010*c2c66affSColin Finck 
1011*c2c66affSColin Finck 	default:		/* Anything else */
1012*c2c66affSColin Finck 	  if (0 == device_num)
1013*c2c66affSColin Finck 	      goto def_hd_params;
1014*c2c66affSColin Finck 	  else
1015*c2c66affSColin Finck 	      goto floppy_default;
1016*c2c66affSColin Finck 	}
1017*c2c66affSColin Finck     }
1018*c2c66affSColin Finck   else if ((device_num & 0xff00) == 0x0700) /* This is a loop device */
1019*c2c66affSColin Finck     {
1020*c2c66affSColin Finck       /* Can we get the loop geometry? This is in 512 byte blocks, always? */
1021*c2c66affSColin Finck       if (ioctl (dev, BLKGETSIZE, &loop_size))
1022*c2c66affSColin Finck 	die ("unable to get loop geometry for '%s'");
1023*c2c66affSColin Finck       loop_size = loop_size >> 1;
1024*c2c66affSColin Finck 
1025*c2c66affSColin Finck       switch (loop_size)  /* Assuming the loop device -> floppy later */
1026*c2c66affSColin Finck 	{
1027*c2c66affSColin Finck 	case 720:		/* 5.25", 2, 9, 40 - 360K */
1028*c2c66affSColin Finck 	  bs.secs_track = CF_LE_W(9);
1029*c2c66affSColin Finck 	  bs.heads = CF_LE_W(2);
1030*c2c66affSColin Finck 	  bs.media = (char) 0xfd;
1031*c2c66affSColin Finck 	  bs.cluster_size = (char) 2;
1032*c2c66affSColin Finck 	  bs.dir_entries[0] = (char) 112;
1033*c2c66affSColin Finck 	  bs.dir_entries[1] = (char) 0;
1034*c2c66affSColin Finck 	  break;
1035*c2c66affSColin Finck 
1036*c2c66affSColin Finck 	case 1440:		/* 3.5", 2, 9, 80 - 720K */
1037*c2c66affSColin Finck 	  bs.secs_track = CF_LE_W(9);
1038*c2c66affSColin Finck 	  bs.heads = CF_LE_W(2);
1039*c2c66affSColin Finck 	  bs.media = (char) 0xf9;
1040*c2c66affSColin Finck 	  bs.cluster_size = (char) 2;
1041*c2c66affSColin Finck 	  bs.dir_entries[0] = (char) 112;
1042*c2c66affSColin Finck 	  bs.dir_entries[1] = (char) 0;
1043*c2c66affSColin Finck 	  break;
1044*c2c66affSColin Finck 
1045*c2c66affSColin Finck 	case 2400:		/* 5.25", 2, 15, 80 - 1200K */
1046*c2c66affSColin Finck 	  bs.secs_track = CF_LE_W(15);
1047*c2c66affSColin Finck 	  bs.heads = CF_LE_W(2);
1048*c2c66affSColin Finck 	  bs.media = (char) 0xf9;
1049*c2c66affSColin Finck 	  bs.cluster_size = (char)(atari_format ? 2 : 1);
1050*c2c66affSColin Finck 	  bs.dir_entries[0] = (char) 224;
1051*c2c66affSColin Finck 	  bs.dir_entries[1] = (char) 0;
1052*c2c66affSColin Finck 	  break;
1053*c2c66affSColin Finck 
1054*c2c66affSColin Finck 	case 5760:		/* 3.5", 2, 36, 80 - 2880K */
1055*c2c66affSColin Finck 	  bs.secs_track = CF_LE_W(36);
1056*c2c66affSColin Finck 	  bs.heads = CF_LE_W(2);
1057*c2c66affSColin Finck 	  bs.media = (char) 0xf0;
1058*c2c66affSColin Finck 	  bs.cluster_size = (char) 2;
1059*c2c66affSColin Finck 	  bs.dir_entries[0] = (char) 224;
1060*c2c66affSColin Finck 	  bs.dir_entries[1] = (char) 0;
1061*c2c66affSColin Finck 	  break;
1062*c2c66affSColin Finck 
1063*c2c66affSColin Finck 	case 2880:		/* 3.5", 2, 18, 80 - 1440K */
1064*c2c66affSColin Finck 	  bs.secs_track = CF_LE_W(18);
1065*c2c66affSColin Finck 	  bs.heads = CF_LE_W(2);
1066*c2c66affSColin Finck 	  bs.media = (char) 0xf0;
1067*c2c66affSColin Finck 	  bs.cluster_size = (char)(atari_format ? 2 : 1);
1068*c2c66affSColin Finck 	  bs.dir_entries[0] = (char) 224;
1069*c2c66affSColin Finck 	  bs.dir_entries[1] = (char) 0;
1070*c2c66affSColin Finck 	  break;
1071*c2c66affSColin Finck 
1072*c2c66affSColin Finck 	default:		/* Anything else: default hd setup */
1073*c2c66affSColin Finck 	  printf("Loop device does not match a floppy size, using "
1074*c2c66affSColin Finck 		 "default hd params\n");
1075*c2c66affSColin Finck 	  bs.secs_track = CT_LE_W(32); /* these are fake values... */
1076*c2c66affSColin Finck 	  bs.heads = CT_LE_W(64);
1077*c2c66affSColin Finck 	  goto def_hd_params;
1078*c2c66affSColin Finck 	}
1079*c2c66affSColin Finck     }
1080*c2c66affSColin Finck   else
1081*c2c66affSColin Finck     /* Must be a hard disk then! */
1082*c2c66affSColin Finck     {
1083*c2c66affSColin Finck       /* Can we get the drive geometry? (Note I'm not too sure about */
1084*c2c66affSColin Finck       /* whether to use HDIO_GETGEO or HDIO_REQ) */
1085*c2c66affSColin Finck       if (ioctl (dev, HDIO_GETGEO, &geometry))
1086*c2c66affSColin Finck 	die ("unable to get drive geometry for '%s'");
1087*c2c66affSColin Finck       bs.secs_track = CT_LE_W(geometry.sectors);	/* Set up the geometry information */
1088*c2c66affSColin Finck       bs.heads = CT_LE_W(geometry.heads);
1089*c2c66affSColin Finck     def_hd_params:
1090*c2c66affSColin Finck       bs.media = (char) 0xf8; /* Set up the media descriptor for a hard drive */
1091*c2c66affSColin Finck       bs.dir_entries[0] = (char) 0;	/* Default to 512 entries */
1092*c2c66affSColin Finck       bs.dir_entries[1] = (char) 2;
1093*c2c66affSColin Finck       /* For FAT32, use 4k clusters on sufficiently large file systems,
1094*c2c66affSColin Finck        * otherwise 1 sector per cluster. This is also what M$'s format
1095*c2c66affSColin Finck        * command does for FAT32. */
1096*c2c66affSColin Finck       bs.cluster_size = (char)
1097*c2c66affSColin Finck 	    (size_fat == 32 ?
1098*c2c66affSColin Finck 	     ((ll_t)blocks*SECTORS_PER_BLOCK >= 512*1024 ? 8 : 1) :
1099*c2c66affSColin Finck 	     4); /* FAT12 and FAT16: start at 4 sectors per cluster */
1100*c2c66affSColin Finck     }
1101*c2c66affSColin Finck }
1102*c2c66affSColin Finck #endif
1103*c2c66affSColin Finck 
1104*c2c66affSColin Finck 
1105*c2c66affSColin Finck /* Create the filesystem data tables */
1106*c2c66affSColin Finck 
1107*c2c66affSColin Finck static void
setup_tables(void)1108*c2c66affSColin Finck setup_tables (void)
1109*c2c66affSColin Finck {
1110*c2c66affSColin Finck   unsigned num_sectors;
1111*c2c66affSColin Finck   unsigned cluster_count = 0, fat_length;
1112*c2c66affSColin Finck   unsigned fatdata;			/* Sectors for FATs + data area */
1113*c2c66affSColin Finck   struct tm *ctime;
1114*c2c66affSColin Finck   struct msdos_volume_info *vi = (size_fat == 32 ? &bs.fat32.vi : &bs.oldfat.vi);
1115*c2c66affSColin Finck 
1116*c2c66affSColin Finck   if (atari_format)
1117*c2c66affSColin Finck       /* On Atari, the first few bytes of the boot sector are assigned
1118*c2c66affSColin Finck        * differently: The jump code is only 2 bytes (and m68k machine code
1119*c2c66affSColin Finck        * :-), then 6 bytes filler (ignored), then 3 byte serial number. */
1120*c2c66affSColin Finck     strncpy( bs.system_id-1, "mkdosf", 6 );
1121*c2c66affSColin Finck   else
1122*c2c66affSColin Finck     strcpy (bs.system_id, "mkdosfs");
1123*c2c66affSColin Finck   if (sectors_per_cluster)
1124*c2c66affSColin Finck     bs.cluster_size = (char) sectors_per_cluster;
1125*c2c66affSColin Finck   if (size_fat == 32)
1126*c2c66affSColin Finck     {
1127*c2c66affSColin Finck       /* Under FAT32, the root dir is in a cluster chain, and this is
1128*c2c66affSColin Finck        * signalled by bs.dir_entries being 0. */
1129*c2c66affSColin Finck       bs.dir_entries[0] = bs.dir_entries[1] = (char) 0;
1130*c2c66affSColin Finck       root_dir_entries = 0;
1131*c2c66affSColin Finck     }
1132*c2c66affSColin Finck   else if (root_dir_entries)
1133*c2c66affSColin Finck     {
1134*c2c66affSColin Finck       /* Override default from establish_params() */
1135*c2c66affSColin Finck       bs.dir_entries[0] = (char) (root_dir_entries & 0x00ff);
1136*c2c66affSColin Finck       bs.dir_entries[1] = (char) ((root_dir_entries & 0xff00) >> 8);
1137*c2c66affSColin Finck     }
1138*c2c66affSColin Finck   else
1139*c2c66affSColin Finck     root_dir_entries = bs.dir_entries[0] + (bs.dir_entries[1] << 8);
1140*c2c66affSColin Finck 
1141*c2c66affSColin Finck   if (atari_format) {
1142*c2c66affSColin Finck     bs.system_id[5] = (unsigned char) (volume_id & 0x000000ff);
1143*c2c66affSColin Finck     bs.system_id[6] = (unsigned char) ((volume_id & 0x0000ff00) >> 8);
1144*c2c66affSColin Finck     bs.system_id[7] = (unsigned char) ((volume_id & 0x00ff0000) >> 16);
1145*c2c66affSColin Finck   }
1146*c2c66affSColin Finck   else {
1147*c2c66affSColin Finck     vi->volume_id[0] = (unsigned char) (volume_id & 0x000000ff);
1148*c2c66affSColin Finck     vi->volume_id[1] = (unsigned char) ((volume_id & 0x0000ff00) >> 8);
1149*c2c66affSColin Finck     vi->volume_id[2] = (unsigned char) ((volume_id & 0x00ff0000) >> 16);
1150*c2c66affSColin Finck     vi->volume_id[3] = (unsigned char) (volume_id >> 24);
1151*c2c66affSColin Finck   }
1152*c2c66affSColin Finck 
1153*c2c66affSColin Finck   if (!atari_format) {
1154*c2c66affSColin Finck     memcpy(vi->volume_label, volume_name, 11);
1155*c2c66affSColin Finck 
1156*c2c66affSColin Finck     memcpy(bs.boot_jump, dummy_boot_jump, 3);
1157*c2c66affSColin Finck     /* Patch in the correct offset to the boot code */
1158*c2c66affSColin Finck     bs.boot_jump[1] = ((size_fat == 32 ?
1159*c2c66affSColin Finck 			(char *)&bs.fat32.boot_code :
1160*c2c66affSColin Finck 			(char *)&bs.oldfat.boot_code) -
1161*c2c66affSColin Finck 		       (char *)&bs) - 2;
1162*c2c66affSColin Finck 
1163*c2c66affSColin Finck     if (size_fat == 32) {
1164*c2c66affSColin Finck 	if (dummy_boot_code[BOOTCODE_FAT32_SIZE-1])
1165*c2c66affSColin Finck 	  printf ("Warning: message too long; truncated\n");
1166*c2c66affSColin Finck 	dummy_boot_code[BOOTCODE_FAT32_SIZE-1] = 0;
1167*c2c66affSColin Finck 	memcpy(bs.fat32.boot_code, dummy_boot_code, BOOTCODE_FAT32_SIZE);
1168*c2c66affSColin Finck     }
1169*c2c66affSColin Finck     else {
1170*c2c66affSColin Finck 	memcpy(bs.oldfat.boot_code, dummy_boot_code, BOOTCODE_SIZE);
1171*c2c66affSColin Finck     }
1172*c2c66affSColin Finck     bs.boot_sign = CT_LE_W(BOOT_SIGN);
1173*c2c66affSColin Finck   }
1174*c2c66affSColin Finck   else {
1175*c2c66affSColin Finck     memcpy(bs.boot_jump, dummy_boot_jump_m68k, 2);
1176*c2c66affSColin Finck   }
1177*c2c66affSColin Finck   if (verbose >= 2)
1178*c2c66affSColin Finck     printf( "Boot jump code is %02x %02x\n",
1179*c2c66affSColin Finck 	    bs.boot_jump[0], bs.boot_jump[1] );
1180*c2c66affSColin Finck 
1181*c2c66affSColin Finck   if (!reserved_sectors)
1182*c2c66affSColin Finck       reserved_sectors = (size_fat == 32) ? 32 : 1;
1183*c2c66affSColin Finck   else {
1184*c2c66affSColin Finck       if (size_fat == 32 && reserved_sectors < 2)
1185*c2c66affSColin Finck 	  die("On FAT32 at least 2 reserved sectors are needed.");
1186*c2c66affSColin Finck   }
1187*c2c66affSColin Finck   bs.reserved = CT_LE_W(reserved_sectors);
1188*c2c66affSColin Finck   if (verbose >= 2)
1189*c2c66affSColin Finck     printf( "Using %d reserved sectors\n", reserved_sectors );
1190*c2c66affSColin Finck   bs.fats = (char) nr_fats;
1191*c2c66affSColin Finck   if (!atari_format || size_fat == 32)
1192*c2c66affSColin Finck     bs.hidden = CT_LE_L(0);
1193*c2c66affSColin Finck   else
1194*c2c66affSColin Finck     /* In Atari format, hidden is a 16 bit field */
1195*c2c66affSColin Finck     memset( &bs.hidden, 0, 2 );
1196*c2c66affSColin Finck 
1197*c2c66affSColin Finck   num_sectors = (ll_t)blocks*BLOCK_SIZE/sector_size;
1198*c2c66affSColin Finck   if (!atari_format) {
1199*c2c66affSColin Finck     unsigned fatlength12, fatlength16, fatlength32;
1200*c2c66affSColin Finck     unsigned maxclust12, maxclust16, maxclust32;
1201*c2c66affSColin Finck     unsigned clust12, clust16, clust32;
1202*c2c66affSColin Finck     int maxclustsize;
1203*c2c66affSColin Finck 
1204*c2c66affSColin Finck     fatdata = num_sectors - cdiv (root_dir_entries * 32, sector_size) -
1205*c2c66affSColin Finck 	      reserved_sectors;
1206*c2c66affSColin Finck 
1207*c2c66affSColin Finck     if (sectors_per_cluster)
1208*c2c66affSColin Finck       bs.cluster_size = maxclustsize = sectors_per_cluster;
1209*c2c66affSColin Finck     else
1210*c2c66affSColin Finck       /* An initial guess for bs.cluster_size should already be set */
1211*c2c66affSColin Finck       maxclustsize = 128;
1212*c2c66affSColin Finck 
1213*c2c66affSColin Finck     if (verbose >= 2)
1214*c2c66affSColin Finck       printf( "%d sectors for FAT+data, starting with %d sectors/cluster\n",
1215*c2c66affSColin Finck 	      fatdata, bs.cluster_size );
1216*c2c66affSColin Finck     do {
1217*c2c66affSColin Finck       if (verbose >= 2)
1218*c2c66affSColin Finck 	printf( "Trying with %d sectors/cluster:\n", bs.cluster_size );
1219*c2c66affSColin Finck 
1220*c2c66affSColin Finck       /* The factor 2 below avoids cut-off errors for nr_fats == 1.
1221*c2c66affSColin Finck        * The "nr_fats*3" is for the reserved first two FAT entries */
1222*c2c66affSColin Finck       clust12 = 2*((ll_t) fatdata *sector_size + nr_fats*3) /
1223*c2c66affSColin Finck 	(2*(int) bs.cluster_size * sector_size + nr_fats*3);
1224*c2c66affSColin Finck       fatlength12 = cdiv (((clust12+2) * 3 + 1) >> 1, sector_size);
1225*c2c66affSColin Finck       /* Need to recalculate number of clusters, since the unused parts of the
1226*c2c66affSColin Finck        * FATS and data area together could make up space for an additional,
1227*c2c66affSColin Finck        * not really present cluster. */
1228*c2c66affSColin Finck       clust12 = (fatdata - nr_fats*fatlength12)/bs.cluster_size;
1229*c2c66affSColin Finck       maxclust12 = (fatlength12 * 2 * sector_size) / 3;
1230*c2c66affSColin Finck       if (maxclust12 > MAX_CLUST_12)
1231*c2c66affSColin Finck 	maxclust12 = MAX_CLUST_12;
1232*c2c66affSColin Finck       if (verbose >= 2)
1233*c2c66affSColin Finck 	printf( "FAT12: #clu=%u, fatlen=%u, maxclu=%u, limit=%u\n",
1234*c2c66affSColin Finck 		clust12, fatlength12, maxclust12, MAX_CLUST_12 );
1235*c2c66affSColin Finck       if (clust12 > maxclust12-2) {
1236*c2c66affSColin Finck 	clust12 = 0;
1237*c2c66affSColin Finck 	if (verbose >= 2)
1238*c2c66affSColin Finck 	  printf( "FAT12: too much clusters\n" );
1239*c2c66affSColin Finck       }
1240*c2c66affSColin Finck 
1241*c2c66affSColin Finck       clust16 = ((ll_t) fatdata *sector_size + nr_fats*4) /
1242*c2c66affSColin Finck 	((int) bs.cluster_size * sector_size + nr_fats*2);
1243*c2c66affSColin Finck       fatlength16 = cdiv ((clust16+2) * 2, sector_size);
1244*c2c66affSColin Finck       /* Need to recalculate number of clusters, since the unused parts of the
1245*c2c66affSColin Finck        * FATS and data area together could make up space for an additional,
1246*c2c66affSColin Finck        * not really present cluster. */
1247*c2c66affSColin Finck       clust16 = (fatdata - nr_fats*fatlength16)/bs.cluster_size;
1248*c2c66affSColin Finck       maxclust16 = (fatlength16 * sector_size) / 2;
1249*c2c66affSColin Finck       if (maxclust16 > MAX_CLUST_16)
1250*c2c66affSColin Finck 	maxclust16 = MAX_CLUST_16;
1251*c2c66affSColin Finck       if (verbose >= 2)
1252*c2c66affSColin Finck 	printf( "FAT16: #clu=%u, fatlen=%u, maxclu=%u, limit=%u\n",
1253*c2c66affSColin Finck 		clust16, fatlength16, maxclust16, MAX_CLUST_16 );
1254*c2c66affSColin Finck       if (clust16 > maxclust16-2) {
1255*c2c66affSColin Finck 	if (verbose >= 2)
1256*c2c66affSColin Finck 	  printf( "FAT16: too much clusters\n" );
1257*c2c66affSColin Finck 	clust16 = 0;
1258*c2c66affSColin Finck       }
1259*c2c66affSColin Finck       /* The < 4078 avoids that the filesystem will be misdetected as having a
1260*c2c66affSColin Finck        * 12 bit FAT. */
1261*c2c66affSColin Finck       if (clust16 < FAT12_THRESHOLD && !(size_fat_by_user && size_fat == 16)) {
1262*c2c66affSColin Finck 	if (verbose >= 2)
1263*c2c66affSColin Finck 	  printf( clust16 < FAT12_THRESHOLD ?
1264*c2c66affSColin Finck 		  "FAT16: would be misdetected as FAT12\n" :
1265*c2c66affSColin Finck 		  "FAT16: too much clusters\n" );
1266*c2c66affSColin Finck 	clust16 = 0;
1267*c2c66affSColin Finck       }
1268*c2c66affSColin Finck 
1269*c2c66affSColin Finck       clust32 = ((ll_t) fatdata *sector_size + nr_fats*8) /
1270*c2c66affSColin Finck 	((int) bs.cluster_size * sector_size + nr_fats*4);
1271*c2c66affSColin Finck       fatlength32 = cdiv ((clust32+2) * 4, sector_size);
1272*c2c66affSColin Finck       /* Need to recalculate number of clusters, since the unused parts of the
1273*c2c66affSColin Finck        * FATS and data area together could make up space for an additional,
1274*c2c66affSColin Finck        * not really present cluster. */
1275*c2c66affSColin Finck       clust32 = (fatdata - nr_fats*fatlength32)/bs.cluster_size;
1276*c2c66affSColin Finck       maxclust32 = (fatlength32 * sector_size) / 4;
1277*c2c66affSColin Finck       if (maxclust32 > MAX_CLUST_32)
1278*c2c66affSColin Finck 	maxclust32 = MAX_CLUST_32;
1279*c2c66affSColin Finck       if (verbose >= 2)
1280*c2c66affSColin Finck 	printf( "FAT32: #clu=%u, fatlen=%u, maxclu=%u, limit=%u\n",
1281*c2c66affSColin Finck 		clust32, fatlength32, maxclust32, MAX_CLUST_32 );
1282*c2c66affSColin Finck       if (clust32 > maxclust32) {
1283*c2c66affSColin Finck 	clust32 = 0;
1284*c2c66affSColin Finck 	if (verbose >= 2)
1285*c2c66affSColin Finck 	  printf( "FAT32: too much clusters\n" );
1286*c2c66affSColin Finck       }
1287*c2c66affSColin Finck 
1288*c2c66affSColin Finck       if ((clust12 && (size_fat == 0 || size_fat == 12)) ||
1289*c2c66affSColin Finck 	  (clust16 && (size_fat == 0 || size_fat == 16)) ||
1290*c2c66affSColin Finck 	  (clust32 && size_fat == 32))
1291*c2c66affSColin Finck 	break;
1292*c2c66affSColin Finck 
1293*c2c66affSColin Finck       bs.cluster_size <<= 1;
1294*c2c66affSColin Finck     } while (bs.cluster_size && bs.cluster_size <= maxclustsize);
1295*c2c66affSColin Finck 
1296*c2c66affSColin Finck     /* Use the optimal FAT size if not specified;
1297*c2c66affSColin Finck      * FAT32 is (not yet) choosen automatically */
1298*c2c66affSColin Finck     if (!size_fat) {
1299*c2c66affSColin Finck 	size_fat = (clust16 > clust12) ? 16 : 12;
1300*c2c66affSColin Finck 	if (verbose >= 2)
1301*c2c66affSColin Finck 	  printf( "Choosing %d bits for FAT\n", size_fat );
1302*c2c66affSColin Finck     }
1303*c2c66affSColin Finck 
1304*c2c66affSColin Finck     switch (size_fat) {
1305*c2c66affSColin Finck       case 12:
1306*c2c66affSColin Finck 	cluster_count = clust12;
1307*c2c66affSColin Finck 	fat_length = fatlength12;
1308*c2c66affSColin Finck 	bs.fat_length = CT_LE_W(fatlength12);
1309*c2c66affSColin Finck 	memcpy(vi->fs_type, MSDOS_FAT12_SIGN, 8);
1310*c2c66affSColin Finck 	break;
1311*c2c66affSColin Finck 
1312*c2c66affSColin Finck       case 16:
1313*c2c66affSColin Finck 	if (clust16 < FAT12_THRESHOLD) {
1314*c2c66affSColin Finck 	    if (size_fat_by_user) {
1315*c2c66affSColin Finck 		fprintf( stderr, "WARNING: Not enough clusters for a "
1316*c2c66affSColin Finck 			 "16 bit FAT! The filesystem will be\n"
1317*c2c66affSColin Finck 			 "misinterpreted as having a 12 bit FAT without "
1318*c2c66affSColin Finck 			 "mount option \"fat=16\".\n" );
1319*c2c66affSColin Finck 	    }
1320*c2c66affSColin Finck 	    else {
1321*c2c66affSColin Finck 		fprintf( stderr, "This filesystem has an unfortunate size. "
1322*c2c66affSColin Finck 			 "A 12 bit FAT cannot provide\n"
1323*c2c66affSColin Finck 			 "enough clusters, but a 16 bit FAT takes up a little "
1324*c2c66affSColin Finck 			 "bit more space so that\n"
1325*c2c66affSColin Finck 			 "the total number of clusters becomes less than the "
1326*c2c66affSColin Finck 			 "threshold value for\n"
1327*c2c66affSColin Finck 			 "distinction between 12 and 16 bit FATs.\n" );
1328*c2c66affSColin Finck 		die( "Make the file system a bit smaller manually." );
1329*c2c66affSColin Finck 	    }
1330*c2c66affSColin Finck 	}
1331*c2c66affSColin Finck 	cluster_count = clust16;
1332*c2c66affSColin Finck 	fat_length = fatlength16;
1333*c2c66affSColin Finck 	bs.fat_length = CT_LE_W(fatlength16);
1334*c2c66affSColin Finck 	memcpy(vi->fs_type, MSDOS_FAT16_SIGN, 8);
1335*c2c66affSColin Finck 	break;
1336*c2c66affSColin Finck 
1337*c2c66affSColin Finck       case 32:
1338*c2c66affSColin Finck 	cluster_count = clust32;
1339*c2c66affSColin Finck 	fat_length = fatlength32;
1340*c2c66affSColin Finck 	bs.fat_length = CT_LE_W(0);
1341*c2c66affSColin Finck 	bs.fat32.fat32_length = CT_LE_L(fatlength32);
1342*c2c66affSColin Finck 	memcpy(vi->fs_type, MSDOS_FAT32_SIGN, 8);
1343*c2c66affSColin Finck 	break;
1344*c2c66affSColin Finck 
1345*c2c66affSColin Finck       default:
1346*c2c66affSColin Finck 	die("FAT not 12, 16 or 32 bits");
1347*c2c66affSColin Finck     }
1348*c2c66affSColin Finck   }
1349*c2c66affSColin Finck   else {
1350*c2c66affSColin Finck     unsigned clusters, maxclust;
1351*c2c66affSColin Finck 
1352*c2c66affSColin Finck     /* GEMDOS always uses a 12 bit FAT on floppies, and always a 16 bit FAT on
1353*c2c66affSColin Finck      * hard disks. So use 12 bit if the size of the file system suggests that
1354*c2c66affSColin Finck      * this fs is for a floppy disk, if the user hasn't explicitly requested a
1355*c2c66affSColin Finck      * size.
1356*c2c66affSColin Finck      */
1357*c2c66affSColin Finck     if (!size_fat)
1358*c2c66affSColin Finck       size_fat = (num_sectors == 1440 || num_sectors == 2400 ||
1359*c2c66affSColin Finck 		  num_sectors == 2880 || num_sectors == 5760) ? 12 : 16;
1360*c2c66affSColin Finck     if (verbose >= 2)
1361*c2c66affSColin Finck       printf( "Choosing %d bits for FAT\n", size_fat );
1362*c2c66affSColin Finck 
1363*c2c66affSColin Finck     /* Atari format: cluster size should be 2, except explicitly requested by
1364*c2c66affSColin Finck      * the user, since GEMDOS doesn't like other cluster sizes very much.
1365*c2c66affSColin Finck      * Instead, tune the sector size for the FS to fit.
1366*c2c66affSColin Finck      */
1367*c2c66affSColin Finck     bs.cluster_size = sectors_per_cluster ? sectors_per_cluster : 2;
1368*c2c66affSColin Finck     if (!sector_size_set) {
1369*c2c66affSColin Finck       while( num_sectors > GEMDOS_MAX_SECTORS ) {
1370*c2c66affSColin Finck 	num_sectors >>= 1;
1371*c2c66affSColin Finck 	sector_size <<= 1;
1372*c2c66affSColin Finck       }
1373*c2c66affSColin Finck     }
1374*c2c66affSColin Finck     if (verbose >= 2)
1375*c2c66affSColin Finck       printf( "Sector size must be %d to have less than %d log. sectors\n",
1376*c2c66affSColin Finck 	      sector_size, GEMDOS_MAX_SECTORS );
1377*c2c66affSColin Finck 
1378*c2c66affSColin Finck     /* Check if there are enough FAT indices for how much clusters we have */
1379*c2c66affSColin Finck     do {
1380*c2c66affSColin Finck       fatdata = num_sectors - cdiv (root_dir_entries * 32, sector_size) -
1381*c2c66affSColin Finck 		reserved_sectors;
1382*c2c66affSColin Finck       /* The factor 2 below avoids cut-off errors for nr_fats == 1 and
1383*c2c66affSColin Finck        * size_fat == 12
1384*c2c66affSColin Finck        * The "2*nr_fats*size_fat/8" is for the reserved first two FAT entries
1385*c2c66affSColin Finck        */
1386*c2c66affSColin Finck       clusters = (2*((ll_t)fatdata*sector_size - 2*nr_fats*size_fat/8)) /
1387*c2c66affSColin Finck 		 (2*((int)bs.cluster_size*sector_size + nr_fats*size_fat/8));
1388*c2c66affSColin Finck       fat_length = cdiv( (clusters+2)*size_fat/8, sector_size );
1389*c2c66affSColin Finck       /* Need to recalculate number of clusters, since the unused parts of the
1390*c2c66affSColin Finck        * FATS and data area together could make up space for an additional,
1391*c2c66affSColin Finck        * not really present cluster. */
1392*c2c66affSColin Finck       clusters = (fatdata - nr_fats*fat_length)/bs.cluster_size;
1393*c2c66affSColin Finck       maxclust = (fat_length*sector_size*8)/size_fat;
1394*c2c66affSColin Finck       if (verbose >= 2)
1395*c2c66affSColin Finck 	printf( "ss=%d: #clu=%d, fat_len=%d, maxclu=%d\n",
1396*c2c66affSColin Finck 		sector_size, clusters, fat_length, maxclust );
1397*c2c66affSColin Finck 
1398*c2c66affSColin Finck       /* last 10 cluster numbers are special (except FAT32: 4 high bits rsvd);
1399*c2c66affSColin Finck        * first two numbers are reserved */
1400*c2c66affSColin Finck       if (maxclust <= (size_fat == 32 ? MAX_CLUST_32 : (1<<size_fat)-0x10) &&
1401*c2c66affSColin Finck 	  clusters <= maxclust-2)
1402*c2c66affSColin Finck 	break;
1403*c2c66affSColin Finck       if (verbose >= 2)
1404*c2c66affSColin Finck 	printf( clusters > maxclust-2 ?
1405*c2c66affSColin Finck 		"Too many clusters\n" : "FAT too big\n" );
1406*c2c66affSColin Finck 
1407*c2c66affSColin Finck       /* need to increment sector_size once more to  */
1408*c2c66affSColin Finck       if (sector_size_set)
1409*c2c66affSColin Finck 	  die( "With this sector size, the maximum number of FAT entries "
1410*c2c66affSColin Finck 	       "would be exceeded." );
1411*c2c66affSColin Finck       num_sectors >>= 1;
1412*c2c66affSColin Finck       sector_size <<= 1;
1413*c2c66affSColin Finck     } while( sector_size <= GEMDOS_MAX_SECTOR_SIZE );
1414*c2c66affSColin Finck 
1415*c2c66affSColin Finck     if (sector_size > GEMDOS_MAX_SECTOR_SIZE)
1416*c2c66affSColin Finck       die( "Would need a sector size > 16k, which GEMDOS can't work with");
1417*c2c66affSColin Finck 
1418*c2c66affSColin Finck     cluster_count = clusters;
1419*c2c66affSColin Finck     if (size_fat != 32)
1420*c2c66affSColin Finck 	bs.fat_length = CT_LE_W(fat_length);
1421*c2c66affSColin Finck     else {
1422*c2c66affSColin Finck 	bs.fat_length = 0;
1423*c2c66affSColin Finck 	bs.fat32.fat32_length = CT_LE_L(fat_length);
1424*c2c66affSColin Finck     }
1425*c2c66affSColin Finck   }
1426*c2c66affSColin Finck 
1427*c2c66affSColin Finck   bs.sector_size[0] = (char) (sector_size & 0x00ff);
1428*c2c66affSColin Finck   bs.sector_size[1] = (char) ((sector_size & 0xff00) >> 8);
1429*c2c66affSColin Finck 
1430*c2c66affSColin Finck   if (size_fat == 32)
1431*c2c66affSColin Finck     {
1432*c2c66affSColin Finck       /* set up additional FAT32 fields */
1433*c2c66affSColin Finck       bs.fat32.flags = CT_LE_W(0);
1434*c2c66affSColin Finck       bs.fat32.version[0] = 0;
1435*c2c66affSColin Finck       bs.fat32.version[1] = 0;
1436*c2c66affSColin Finck       bs.fat32.root_cluster = CT_LE_L(2);
1437*c2c66affSColin Finck       bs.fat32.info_sector = CT_LE_W(1);
1438*c2c66affSColin Finck       if (!backup_boot)
1439*c2c66affSColin Finck 	backup_boot = (reserved_sectors >= 7) ? 6 :
1440*c2c66affSColin Finck 		      (reserved_sectors >= 2) ? reserved_sectors-1 : 0;
1441*c2c66affSColin Finck       else
1442*c2c66affSColin Finck 	{
1443*c2c66affSColin Finck 	  if (backup_boot == 1)
1444*c2c66affSColin Finck 	    die("Backup boot sector must be after sector 1");
1445*c2c66affSColin Finck 	  else if (backup_boot >= reserved_sectors)
1446*c2c66affSColin Finck 	    die("Backup boot sector must be a reserved sector");
1447*c2c66affSColin Finck 	}
1448*c2c66affSColin Finck       if (verbose >= 2)
1449*c2c66affSColin Finck 	printf( "Using sector %d as backup boot sector (0 = none)\n",
1450*c2c66affSColin Finck 		backup_boot );
1451*c2c66affSColin Finck       bs.fat32.backup_boot = CT_LE_W(backup_boot);
1452*c2c66affSColin Finck       memset( &bs.fat32.reserved2, 0, sizeof(bs.fat32.reserved2) );
1453*c2c66affSColin Finck     }
1454*c2c66affSColin Finck 
1455*c2c66affSColin Finck   if (atari_format) {
1456*c2c66affSColin Finck       /* Just some consistency checks */
1457*c2c66affSColin Finck       if (num_sectors >= GEMDOS_MAX_SECTORS)
1458*c2c66affSColin Finck 	  die( "GEMDOS can't handle more than 65531 sectors" );
1459*c2c66affSColin Finck       else if (num_sectors >= OLDGEMDOS_MAX_SECTORS)
1460*c2c66affSColin Finck 	  printf( "Warning: More than 32765 sector need TOS 1.04 "
1461*c2c66affSColin Finck 		  "or higher.\n" );
1462*c2c66affSColin Finck   }
1463*c2c66affSColin Finck   if (num_sectors >= 65536)
1464*c2c66affSColin Finck     {
1465*c2c66affSColin Finck       bs.sectors[0] = (char) 0;
1466*c2c66affSColin Finck       bs.sectors[1] = (char) 0;
1467*c2c66affSColin Finck       bs.total_sect = CT_LE_L(num_sectors);
1468*c2c66affSColin Finck     }
1469*c2c66affSColin Finck   else
1470*c2c66affSColin Finck     {
1471*c2c66affSColin Finck       bs.sectors[0] = (char) (num_sectors & 0x00ff);
1472*c2c66affSColin Finck       bs.sectors[1] = (char) ((num_sectors & 0xff00) >> 8);
1473*c2c66affSColin Finck       if (!atari_format)
1474*c2c66affSColin Finck 	  bs.total_sect = CT_LE_L(0);
1475*c2c66affSColin Finck     }
1476*c2c66affSColin Finck 
1477*c2c66affSColin Finck   if (!atari_format)
1478*c2c66affSColin Finck     vi->ext_boot_sign = MSDOS_EXT_SIGN;
1479*c2c66affSColin Finck 
1480*c2c66affSColin Finck   if (!cluster_count)
1481*c2c66affSColin Finck     {
1482*c2c66affSColin Finck       if (sectors_per_cluster)	/* If yes, die if we'd spec'd sectors per cluster */
1483*c2c66affSColin Finck 	die ("Too many clusters for file system - try more sectors per cluster");
1484*c2c66affSColin Finck       else
1485*c2c66affSColin Finck 	die ("Attempting to create a too large file system");
1486*c2c66affSColin Finck     }
1487*c2c66affSColin Finck 
1488*c2c66affSColin Finck 
1489*c2c66affSColin Finck   /* The two following vars are in hard sectors, i.e. 512 byte sectors! */
1490*c2c66affSColin Finck   start_data_sector = (reserved_sectors + nr_fats * fat_length) *
1491*c2c66affSColin Finck 		      (sector_size/HARD_SECTOR_SIZE);
1492*c2c66affSColin Finck   start_data_block = (start_data_sector + SECTORS_PER_BLOCK - 1) /
1493*c2c66affSColin Finck 		     SECTORS_PER_BLOCK;
1494*c2c66affSColin Finck 
1495*c2c66affSColin Finck   if (blocks < start_data_block + 32)	/* Arbitrary undersize file system! */
1496*c2c66affSColin Finck     die ("Too few blocks for viable file system");
1497*c2c66affSColin Finck 
1498*c2c66affSColin Finck   if (verbose)
1499*c2c66affSColin Finck     {
1500*c2c66affSColin Finck       printf("%s has %d head%s and %d sector%s per track,\n",
1501*c2c66affSColin Finck 	     device_name, CF_LE_W(bs.heads), (CF_LE_W(bs.heads) != 1) ? "s" : "",
1502*c2c66affSColin Finck 	     CF_LE_W(bs.secs_track), (CF_LE_W(bs.secs_track) != 1) ? "s" : "");
1503*c2c66affSColin Finck       printf("logical sector size is %d,\n",sector_size);
1504*c2c66affSColin Finck       printf("using 0x%02x media descriptor, with %d sectors;\n",
1505*c2c66affSColin Finck 	     (int) (bs.media), num_sectors);
1506*c2c66affSColin Finck       printf("file system has %d %d-bit FAT%s and %d sector%s per cluster.\n",
1507*c2c66affSColin Finck 	     (int) (bs.fats), size_fat, (bs.fats != 1) ? "s" : "",
1508*c2c66affSColin Finck 	     (int) (bs.cluster_size), (bs.cluster_size != 1) ? "s" : "");
1509*c2c66affSColin Finck       printf ("FAT size is %d sector%s, and provides %d cluster%s.\n",
1510*c2c66affSColin Finck 	      fat_length, (fat_length != 1) ? "s" : "",
1511*c2c66affSColin Finck 	      cluster_count, (cluster_count != 1) ? "s" : "");
1512*c2c66affSColin Finck       if (size_fat != 32)
1513*c2c66affSColin Finck 	printf ("Root directory contains %d slots.\n",
1514*c2c66affSColin Finck 		(int) (bs.dir_entries[0]) + (int) (bs.dir_entries[1]) * 256);
1515*c2c66affSColin Finck       printf ("Volume ID is %08lx, ", volume_id &
1516*c2c66affSColin Finck 	      (atari_format ? 0x00ffffff : 0xffffffff));
1517*c2c66affSColin Finck       if ( strcmp(volume_name, "           ") )
1518*c2c66affSColin Finck 	printf("volume label %s.\n", volume_name);
1519*c2c66affSColin Finck       else
1520*c2c66affSColin Finck 	printf("no volume label.\n");
1521*c2c66affSColin Finck     }
1522*c2c66affSColin Finck 
1523*c2c66affSColin Finck   /* Make the file allocation tables! */
1524*c2c66affSColin Finck 
1525*c2c66affSColin Finck   if ((fat = (unsigned char *) malloc (fat_length * sector_size)) == NULL)
1526*c2c66affSColin Finck     die ("unable to allocate space for FAT image in memory");
1527*c2c66affSColin Finck 
1528*c2c66affSColin Finck   memset( fat, 0, fat_length * sector_size );
1529*c2c66affSColin Finck 
1530*c2c66affSColin Finck   mark_FAT_cluster (0, 0xffffffff);	/* Initial fat entries */
1531*c2c66affSColin Finck   mark_FAT_cluster (1, 0xffffffff);
1532*c2c66affSColin Finck   fat[0] = (unsigned char) bs.media;	/* Put media type in first byte! */
1533*c2c66affSColin Finck   if (size_fat == 32) {
1534*c2c66affSColin Finck     /* Mark cluster 2 as EOF (used for root dir) */
1535*c2c66affSColin Finck     mark_FAT_cluster (2, FAT_EOF);
1536*c2c66affSColin Finck   }
1537*c2c66affSColin Finck 
1538*c2c66affSColin Finck   /* Make the root directory entries */
1539*c2c66affSColin Finck 
1540*c2c66affSColin Finck   size_root_dir = (size_fat == 32) ?
1541*c2c66affSColin Finck 		  bs.cluster_size*sector_size :
1542*c2c66affSColin Finck 		  (((int)bs.dir_entries[1]*256+(int)bs.dir_entries[0]) *
1543*c2c66affSColin Finck 		   sizeof (struct msdos_dir_entry));
1544*c2c66affSColin Finck   if ((root_dir = (struct msdos_dir_entry *) malloc (size_root_dir)) == NULL)
1545*c2c66affSColin Finck     {
1546*c2c66affSColin Finck       free (fat);		/* Tidy up before we die! */
1547*c2c66affSColin Finck       die ("unable to allocate space for root directory in memory");
1548*c2c66affSColin Finck     }
1549*c2c66affSColin Finck 
1550*c2c66affSColin Finck   memset(root_dir, 0, size_root_dir);
1551*c2c66affSColin Finck   if ( memcmp(volume_name, "           ", 11) )
1552*c2c66affSColin Finck     {
1553*c2c66affSColin Finck       struct msdos_dir_entry *de = &root_dir[0];
1554*c2c66affSColin Finck       memcpy(de->name, volume_name, 11);
1555*c2c66affSColin Finck       de->attr = ATTR_VOLUME;
1556*c2c66affSColin Finck       ctime = localtime(&create_time);
1557*c2c66affSColin Finck       de->time = CT_LE_W((unsigned short)((ctime->tm_sec >> 1) +
1558*c2c66affSColin Finck 			  (ctime->tm_min << 5) + (ctime->tm_hour << 11)));
1559*c2c66affSColin Finck       de->date = CT_LE_W((unsigned short)(ctime->tm_mday +
1560*c2c66affSColin Finck 					  ((ctime->tm_mon+1) << 5) +
1561*c2c66affSColin Finck 					  ((ctime->tm_year-80) << 9)));
1562*c2c66affSColin Finck       de->ctime_ms = 0;
1563*c2c66affSColin Finck       de->ctime = de->time;
1564*c2c66affSColin Finck       de->cdate = de->date;
1565*c2c66affSColin Finck       de->adate = de->date;
1566*c2c66affSColin Finck       de->starthi = CT_LE_W(0);
1567*c2c66affSColin Finck       de->start = CT_LE_W(0);
1568*c2c66affSColin Finck       de->size = CT_LE_L(0);
1569*c2c66affSColin Finck     }
1570*c2c66affSColin Finck 
1571*c2c66affSColin Finck   if (size_fat == 32) {
1572*c2c66affSColin Finck     /* For FAT32, create an info sector */
1573*c2c66affSColin Finck     struct fat32_fsinfo *info;
1574*c2c66affSColin Finck 
1575*c2c66affSColin Finck     if (!(info_sector = malloc( sector_size )))
1576*c2c66affSColin Finck       die("Out of memory");
1577*c2c66affSColin Finck     memset(info_sector, 0, sector_size);
1578*c2c66affSColin Finck     /* fsinfo structure is at offset 0x1e0 in info sector by observation */
1579*c2c66affSColin Finck     info = (struct fat32_fsinfo *)(info_sector + 0x1e0);
1580*c2c66affSColin Finck 
1581*c2c66affSColin Finck     /* Info sector magic */
1582*c2c66affSColin Finck     info_sector[0] = 'R';
1583*c2c66affSColin Finck     info_sector[1] = 'R';
1584*c2c66affSColin Finck     info_sector[2] = 'a';
1585*c2c66affSColin Finck     info_sector[3] = 'A';
1586*c2c66affSColin Finck 
1587*c2c66affSColin Finck     /* Magic for fsinfo structure */
1588*c2c66affSColin Finck     info->signature = CT_LE_L(0x61417272);
1589*c2c66affSColin Finck     /* We've allocated cluster 2 for the root dir. */
1590*c2c66affSColin Finck     info->free_clusters = CT_LE_L(cluster_count - 1);
1591*c2c66affSColin Finck     info->next_cluster = CT_LE_L(2);
1592*c2c66affSColin Finck 
1593*c2c66affSColin Finck     /* Info sector also must have boot sign */
1594*c2c66affSColin Finck     *(__u16 *)(info_sector + 0x1fe) = CT_LE_W(BOOT_SIGN);
1595*c2c66affSColin Finck   }
1596*c2c66affSColin Finck 
1597*c2c66affSColin Finck   if (!(blank_sector = malloc( sector_size )))
1598*c2c66affSColin Finck       die( "Out of memory" );
1599*c2c66affSColin Finck   memset(blank_sector, 0, sector_size);
1600*c2c66affSColin Finck }
1601*c2c66affSColin Finck 
1602*c2c66affSColin Finck 
1603*c2c66affSColin Finck /* Write the new filesystem's data tables to wherever they're going to end up! */
1604*c2c66affSColin Finck 
1605*c2c66affSColin Finck #define error(str)				\
1606*c2c66affSColin Finck   do {						\
1607*c2c66affSColin Finck     free (fat);					\
1608*c2c66affSColin Finck     if (info_sector) free (info_sector);	\
1609*c2c66affSColin Finck     free (root_dir);				\
1610*c2c66affSColin Finck     die (str);					\
1611*c2c66affSColin Finck   } while(0)
1612*c2c66affSColin Finck 
1613*c2c66affSColin Finck #define seekto(pos,errstr)						\
1614*c2c66affSColin Finck   do {									\
1615*c2c66affSColin Finck     loff_t __pos = (pos);						\
1616*c2c66affSColin Finck     if (llseek (dev, __pos, SEEK_SET) != __pos)				\
1617*c2c66affSColin Finck 	error ("seek to " errstr " failed whilst writing tables");	\
1618*c2c66affSColin Finck   } while(0)
1619*c2c66affSColin Finck 
1620*c2c66affSColin Finck #define writebuf(buf,size,errstr)			\
1621*c2c66affSColin Finck   do {							\
1622*c2c66affSColin Finck     int __size = (size);				\
1623*c2c66affSColin Finck     if (write (dev, buf, __size) != __size)		\
1624*c2c66affSColin Finck 	error ("failed whilst writing " errstr);	\
1625*c2c66affSColin Finck   } while(0)
1626*c2c66affSColin Finck 
1627*c2c66affSColin Finck 
1628*c2c66affSColin Finck static void
write_tables(void)1629*c2c66affSColin Finck write_tables (void)
1630*c2c66affSColin Finck {
1631*c2c66affSColin Finck   int x;
1632*c2c66affSColin Finck   int fat_length;
1633*c2c66affSColin Finck #ifdef _WIN32
1634*c2c66affSColin Finck   int blk;
1635*c2c66affSColin Finck #endif
1636*c2c66affSColin Finck 
1637*c2c66affSColin Finck   fat_length = (size_fat == 32) ?
1638*c2c66affSColin Finck 	       CF_LE_L(bs.fat32.fat32_length) : CF_LE_W(bs.fat_length);
1639*c2c66affSColin Finck 
1640*c2c66affSColin Finck   seekto( 0, "start of device" );
1641*c2c66affSColin Finck   /* clear all reserved sectors */
1642*c2c66affSColin Finck   for( x = 0; x < reserved_sectors; ++x )
1643*c2c66affSColin Finck     writebuf( blank_sector, sector_size, "reserved sector" );
1644*c2c66affSColin Finck   /* seek back to sector 0 and write the boot sector */
1645*c2c66affSColin Finck   seekto( 0, "boot sector" );
1646*c2c66affSColin Finck   writebuf( (char *) &bs, sizeof (struct msdos_boot_sector), "boot sector" );
1647*c2c66affSColin Finck   /* on FAT32, write the info sector and backup boot sector */
1648*c2c66affSColin Finck   if (size_fat == 32)
1649*c2c66affSColin Finck     {
1650*c2c66affSColin Finck       seekto( CF_LE_W(bs.fat32.info_sector)*sector_size, "info sector" );
1651*c2c66affSColin Finck       writebuf( info_sector, 512, "info sector" );
1652*c2c66affSColin Finck       if (backup_boot != 0)
1653*c2c66affSColin Finck 	{
1654*c2c66affSColin Finck 	  seekto( backup_boot*sector_size, "backup boot sector" );
1655*c2c66affSColin Finck 	  writebuf( (char *) &bs, sizeof (struct msdos_boot_sector),
1656*c2c66affSColin Finck 		    "backup boot sector" );
1657*c2c66affSColin Finck 	}
1658*c2c66affSColin Finck     }
1659*c2c66affSColin Finck   /* seek to start of FATS and write them all */
1660*c2c66affSColin Finck   seekto( reserved_sectors*sector_size, "first FAT" );
1661*c2c66affSColin Finck   for (x = 1; x <= nr_fats; x++)
1662*c2c66affSColin Finck #ifdef _WIN32
1663*c2c66affSColin Finck 	  /*
1664*c2c66affSColin Finck 	   * WIN32 appearently has problems writing very large chunks directly
1665*c2c66affSColin Finck 	   * to disk devices. To not produce errors because of resource shortages
1666*c2c66affSColin Finck 	   * split up the write in sector size chunks.
1667*c2c66affSColin Finck 	   */
1668*c2c66affSColin Finck 	  for (blk = 0; blk < fat_length; blk++)
1669*c2c66affSColin Finck 		  writebuf(fat+blk*sector_size, sector_size, "FAT");
1670*c2c66affSColin Finck #else
1671*c2c66affSColin Finck     writebuf( fat, fat_length * sector_size, "FAT" );
1672*c2c66affSColin Finck #endif
1673*c2c66affSColin Finck   /* Write the root directory directly after the last FAT. This is the root
1674*c2c66affSColin Finck    * dir area on FAT12/16, and the first cluster on FAT32. */
1675*c2c66affSColin Finck   writebuf( (char *) root_dir, size_root_dir, "root directory" );
1676*c2c66affSColin Finck 
1677*c2c66affSColin Finck   if (info_sector) free( info_sector );
1678*c2c66affSColin Finck   free (root_dir);   /* Free up the root directory space from setup_tables */
1679*c2c66affSColin Finck   free (fat);  /* Free up the fat table space reserved during setup_tables */
1680*c2c66affSColin Finck }
1681*c2c66affSColin Finck 
1682*c2c66affSColin Finck 
1683*c2c66affSColin Finck /* Report the command usage and return a failure error code */
1684*c2c66affSColin Finck 
1685*c2c66affSColin Finck void
usage(void)1686*c2c66affSColin Finck usage (void)
1687*c2c66affSColin Finck {
1688*c2c66affSColin Finck   fatal_error("\
1689*c2c66affSColin Finck Usage: mkdosfs [-A] [-c] [-C] [-v] [-I] [-l bad-block-file] [-b backup-boot-sector]\n\
1690*c2c66affSColin Finck        [-m boot-msg-file] [-n volume-name] [-i volume-id]\n\
1691*c2c66affSColin Finck        [-s sectors-per-cluster] [-S logical-sector-size] [-f number-of-FATs]\n\
1692*c2c66affSColin Finck        [-F fat-size] [-r root-dir-entries] [-R reserved-sectors]\n\
1693*c2c66affSColin Finck        /dev/name [blocks]\n");
1694*c2c66affSColin Finck }
1695*c2c66affSColin Finck 
1696*c2c66affSColin Finck /*
1697*c2c66affSColin Finck  * ++roman: On m68k, check if this is an Atari; if yes, turn on Atari variant
1698*c2c66affSColin Finck  * of MS-DOS filesystem by default.
1699*c2c66affSColin Finck  */
check_atari(void)1700*c2c66affSColin Finck static void check_atari( void )
1701*c2c66affSColin Finck {
1702*c2c66affSColin Finck #ifdef __mc68000__
1703*c2c66affSColin Finck     FILE *f;
1704*c2c66affSColin Finck     char line[128], *p;
1705*c2c66affSColin Finck 
1706*c2c66affSColin Finck     if (!(f = fopen( "/proc/hardware", "r" ))) {
1707*c2c66affSColin Finck 	perror( "/proc/hardware" );
1708*c2c66affSColin Finck 	return;
1709*c2c66affSColin Finck     }
1710*c2c66affSColin Finck 
1711*c2c66affSColin Finck     while( fgets( line, sizeof(line), f ) ) {
1712*c2c66affSColin Finck 	if (strncmp( line, "Model:", 6 ) == 0) {
1713*c2c66affSColin Finck 	    p = line + 6;
1714*c2c66affSColin Finck 	    p += strspn( p, " \t" );
1715*c2c66affSColin Finck 	    if (strncmp( p, "Atari ", 6 ) == 0)
1716*c2c66affSColin Finck 		atari_format = 1;
1717*c2c66affSColin Finck 	    break;
1718*c2c66affSColin Finck 	}
1719*c2c66affSColin Finck     }
1720*c2c66affSColin Finck     fclose( f );
1721*c2c66affSColin Finck #endif
1722*c2c66affSColin Finck }
1723*c2c66affSColin Finck 
1724*c2c66affSColin Finck /* The "main" entry point into the utility - we pick up the options and attempt to process them in some sort of sensible
1725*c2c66affSColin Finck    way.  In the event that some/all of the options are invalid we need to tell the user so that something can be done! */
1726*c2c66affSColin Finck 
1727*c2c66affSColin Finck int
main(int argc,char ** argv)1728*c2c66affSColin Finck main (int argc, char **argv)
1729*c2c66affSColin Finck {
1730*c2c66affSColin Finck   int c;
1731*c2c66affSColin Finck   char *tmp;
1732*c2c66affSColin Finck   char *listfile = NULL;
1733*c2c66affSColin Finck   FILE *msgfile;
1734*c2c66affSColin Finck #ifdef _WIN32
1735*c2c66affSColin Finck   static char dev_buf[] = "\\\\.\\X:";
1736*c2c66affSColin Finck #else
1737*c2c66affSColin Finck   struct stat statbuf;
1738*c2c66affSColin Finck #endif
1739*c2c66affSColin Finck   int i = 0, pos, ch;
1740*c2c66affSColin Finck   int create = 0;
1741*c2c66affSColin Finck 
1742*c2c66affSColin Finck   if (argc && *argv) {		/* What's the program name? */
1743*c2c66affSColin Finck     char *p;
1744*c2c66affSColin Finck     program_name = *argv;
1745*c2c66affSColin Finck #ifdef _WIN32
1746*c2c66affSColin Finck     if ((p = strrchr( program_name, '\\' )))
1747*c2c66affSColin Finck #else
1748*c2c66affSColin Finck     if ((p = strrchr( program_name, '/' )))
1749*c2c66affSColin Finck #endif
1750*c2c66affSColin Finck 	program_name = p+1;
1751*c2c66affSColin Finck   }
1752*c2c66affSColin Finck 
1753*c2c66affSColin Finck   time(&create_time);
1754*c2c66affSColin Finck   volume_id = (long)create_time;	/* Default volume ID = creation time */
1755*c2c66affSColin Finck   check_atari();
1756*c2c66affSColin Finck 
1757*c2c66affSColin Finck   printf ("%s " VERSION " (" VERSION_DATE ")\n"
1758*c2c66affSColin Finck #ifdef _WIN32
1759*c2c66affSColin Finck 	  "Win32 port by Jens-Uwe Mager <jum@anubis.han.de>\n"
1760*c2c66affSColin Finck #endif
1761*c2c66affSColin Finck 	   , program_name);
1762*c2c66affSColin Finck 
1763*c2c66affSColin Finck   while ((c = getopt (argc, argv, "AcCf:F:Ii:l:m:n:r:R:s:S:v")) != EOF)
1764*c2c66affSColin Finck     /* Scan the command line for options */
1765*c2c66affSColin Finck     switch (c)
1766*c2c66affSColin Finck       {
1767*c2c66affSColin Finck       case 'A':		/* toggle Atari format */
1768*c2c66affSColin Finck 	atari_format = !atari_format;
1769*c2c66affSColin Finck 	break;
1770*c2c66affSColin Finck 
1771*c2c66affSColin Finck       case 'b':		/* b : location of backup boot sector */
1772*c2c66affSColin Finck 	backup_boot = (int) strtol (optarg, &tmp, 0);
1773*c2c66affSColin Finck 	if (*tmp || backup_boot < 2 || backup_boot > 0xffff)
1774*c2c66affSColin Finck 	  {
1775*c2c66affSColin Finck 	    printf ("Bad location for backup boot sector : %s\n", optarg);
1776*c2c66affSColin Finck 	    usage ();
1777*c2c66affSColin Finck 	  }
1778*c2c66affSColin Finck 	break;
1779*c2c66affSColin Finck 
1780*c2c66affSColin Finck       case 'c':		/* c : Check FS as we build it */
1781*c2c66affSColin Finck 	check = TRUE;
1782*c2c66affSColin Finck 	break;
1783*c2c66affSColin Finck 
1784*c2c66affSColin Finck       case 'C':		/* C : Create a new file */
1785*c2c66affSColin Finck 	create = TRUE;
1786*c2c66affSColin Finck 	break;
1787*c2c66affSColin Finck 
1788*c2c66affSColin Finck       case 'f':		/* f : Choose number of FATs */
1789*c2c66affSColin Finck 	nr_fats = (int) strtol (optarg, &tmp, 0);
1790*c2c66affSColin Finck 	if (*tmp || nr_fats < 1 || nr_fats > 4)
1791*c2c66affSColin Finck 	  {
1792*c2c66affSColin Finck 	    printf ("Bad number of FATs : %s\n", optarg);
1793*c2c66affSColin Finck 	    usage ();
1794*c2c66affSColin Finck 	  }
1795*c2c66affSColin Finck 	break;
1796*c2c66affSColin Finck 
1797*c2c66affSColin Finck       case 'F':		/* F : Choose FAT size */
1798*c2c66affSColin Finck 	size_fat = (int) strtol (optarg, &tmp, 0);
1799*c2c66affSColin Finck 	if (*tmp || (size_fat != 12 && size_fat != 16 && size_fat != 32))
1800*c2c66affSColin Finck 	  {
1801*c2c66affSColin Finck 	    printf ("Bad FAT type : %s\n", optarg);
1802*c2c66affSColin Finck 	    usage ();
1803*c2c66affSColin Finck 	  }
1804*c2c66affSColin Finck 	size_fat_by_user = 1;
1805*c2c66affSColin Finck 	break;
1806*c2c66affSColin Finck 
1807*c2c66affSColin Finck       case 'I':
1808*c2c66affSColin Finck 	ignore_full_disk = 1;
1809*c2c66affSColin Finck 	break;
1810*c2c66affSColin Finck 
1811*c2c66affSColin Finck       case 'i':		/* i : specify volume ID */
1812*c2c66affSColin Finck 	volume_id = strtol(optarg, &tmp, 16);
1813*c2c66affSColin Finck 	if ( *tmp )
1814*c2c66affSColin Finck 	  {
1815*c2c66affSColin Finck 	    printf("Volume ID must be a hexadecimal number\n");
1816*c2c66affSColin Finck 	    usage();
1817*c2c66affSColin Finck 	  }
1818*c2c66affSColin Finck 	break;
1819*c2c66affSColin Finck 
1820*c2c66affSColin Finck       case 'l':		/* l : Bad block filename */
1821*c2c66affSColin Finck 	listfile = optarg;
1822*c2c66affSColin Finck 	break;
1823*c2c66affSColin Finck 
1824*c2c66affSColin Finck       case 'm':		/* m : Set boot message */
1825*c2c66affSColin Finck 	if ( strcmp(optarg, "-") )
1826*c2c66affSColin Finck 	  {
1827*c2c66affSColin Finck 	    msgfile = fopen(optarg, "r");
1828*c2c66affSColin Finck 	    if ( !msgfile )
1829*c2c66affSColin Finck 	      perror(optarg);
1830*c2c66affSColin Finck 	  }
1831*c2c66affSColin Finck 	else
1832*c2c66affSColin Finck 	  msgfile = stdin;
1833*c2c66affSColin Finck 
1834*c2c66affSColin Finck 	if ( msgfile )
1835*c2c66affSColin Finck 	  {
1836*c2c66affSColin Finck 	    /* The boot code ends at offset 448 and needs a null terminator */
1837*c2c66affSColin Finck 	    i = MESSAGE_OFFSET;
1838*c2c66affSColin Finck 	    pos = 0;		/* We are at beginning of line */
1839*c2c66affSColin Finck 	    do
1840*c2c66affSColin Finck 	      {
1841*c2c66affSColin Finck 		ch = getc(msgfile);
1842*c2c66affSColin Finck 		switch (ch)
1843*c2c66affSColin Finck 		  {
1844*c2c66affSColin Finck 		  case '\r':	/* Ignore CRs */
1845*c2c66affSColin Finck 		  case '\0':	/* and nulls */
1846*c2c66affSColin Finck 		    break;
1847*c2c66affSColin Finck 
1848*c2c66affSColin Finck 		  case '\n':	/* LF -> CR+LF if necessary */
1849*c2c66affSColin Finck 		    if ( pos )	/* If not at beginning of line */
1850*c2c66affSColin Finck 		      {
1851*c2c66affSColin Finck 			dummy_boot_code[i++] = '\r';
1852*c2c66affSColin Finck 			pos = 0;
1853*c2c66affSColin Finck 		      }
1854*c2c66affSColin Finck 		    dummy_boot_code[i++] = '\n';
1855*c2c66affSColin Finck 		    break;
1856*c2c66affSColin Finck 
1857*c2c66affSColin Finck 		  case '\t':	/* Expand tabs */
1858*c2c66affSColin Finck 		    do
1859*c2c66affSColin Finck 		      {
1860*c2c66affSColin Finck 			dummy_boot_code[i++] = ' ';
1861*c2c66affSColin Finck 			pos++;
1862*c2c66affSColin Finck 		      }
1863*c2c66affSColin Finck 		    while ( pos % 8 && i < BOOTCODE_SIZE-1 );
1864*c2c66affSColin Finck 		    break;
1865*c2c66affSColin Finck 
1866*c2c66affSColin Finck 		  case EOF:
1867*c2c66affSColin Finck 		    dummy_boot_code[i++] = '\0'; /* Null terminator */
1868*c2c66affSColin Finck 		    break;
1869*c2c66affSColin Finck 
1870*c2c66affSColin Finck 		  default:
1871*c2c66affSColin Finck 		    dummy_boot_code[i++] = ch; /* Store character */
1872*c2c66affSColin Finck 		    pos++;	/* Advance position */
1873*c2c66affSColin Finck 		    break;
1874*c2c66affSColin Finck 		  }
1875*c2c66affSColin Finck 	      }
1876*c2c66affSColin Finck 	    while ( ch != EOF && i < BOOTCODE_SIZE-1 );
1877*c2c66affSColin Finck 
1878*c2c66affSColin Finck 	    /* Fill up with zeros */
1879*c2c66affSColin Finck 	    while( i < BOOTCODE_SIZE-1 )
1880*c2c66affSColin Finck 		dummy_boot_code[i++] = '\0';
1881*c2c66affSColin Finck 	    dummy_boot_code[BOOTCODE_SIZE-1] = '\0'; /* Just in case */
1882*c2c66affSColin Finck 
1883*c2c66affSColin Finck 	    if ( ch != EOF )
1884*c2c66affSColin Finck 	      printf ("Warning: message too long; truncated\n");
1885*c2c66affSColin Finck 
1886*c2c66affSColin Finck 	    if ( msgfile != stdin )
1887*c2c66affSColin Finck 	      fclose(msgfile);
1888*c2c66affSColin Finck 	  }
1889*c2c66affSColin Finck 	break;
1890*c2c66affSColin Finck 
1891*c2c66affSColin Finck       case 'n':		/* n : Volume name */
1892*c2c66affSColin Finck 	sprintf(volume_name, "%-11.11s", optarg);
1893*c2c66affSColin Finck 	break;
1894*c2c66affSColin Finck 
1895*c2c66affSColin Finck       case 'r':		/* r : Root directory entries */
1896*c2c66affSColin Finck 	root_dir_entries = (int) strtol (optarg, &tmp, 0);
1897*c2c66affSColin Finck 	if (*tmp || root_dir_entries < 16 || root_dir_entries > 32768)
1898*c2c66affSColin Finck 	  {
1899*c2c66affSColin Finck 	    printf ("Bad number of root directory entries : %s\n", optarg);
1900*c2c66affSColin Finck 	    usage ();
1901*c2c66affSColin Finck 	  }
1902*c2c66affSColin Finck 	break;
1903*c2c66affSColin Finck 
1904*c2c66affSColin Finck       case 'R':		/* R : number of reserved sectors */
1905*c2c66affSColin Finck 	reserved_sectors = (int) strtol (optarg, &tmp, 0);
1906*c2c66affSColin Finck 	if (*tmp || reserved_sectors < 1 || reserved_sectors > 0xffff)
1907*c2c66affSColin Finck 	  {
1908*c2c66affSColin Finck 	    printf ("Bad number of reserved sectors : %s\n", optarg);
1909*c2c66affSColin Finck 	    usage ();
1910*c2c66affSColin Finck 	  }
1911*c2c66affSColin Finck 	break;
1912*c2c66affSColin Finck 
1913*c2c66affSColin Finck       case 's':		/* s : Sectors per cluster */
1914*c2c66affSColin Finck 	sectors_per_cluster = (int) strtol (optarg, &tmp, 0);
1915*c2c66affSColin Finck 	if (*tmp || (sectors_per_cluster != 1 && sectors_per_cluster != 2
1916*c2c66affSColin Finck 		     && sectors_per_cluster != 4 && sectors_per_cluster != 8
1917*c2c66affSColin Finck 		   && sectors_per_cluster != 16 && sectors_per_cluster != 32
1918*c2c66affSColin Finck 		&& sectors_per_cluster != 64 && sectors_per_cluster != 128))
1919*c2c66affSColin Finck 	  {
1920*c2c66affSColin Finck 	    printf ("Bad number of sectors per cluster : %s\n", optarg);
1921*c2c66affSColin Finck 	    usage ();
1922*c2c66affSColin Finck 	  }
1923*c2c66affSColin Finck 	break;
1924*c2c66affSColin Finck 
1925*c2c66affSColin Finck       case 'S':		/* S : Sector size */
1926*c2c66affSColin Finck 	sector_size = (int) strtol (optarg, &tmp, 0);
1927*c2c66affSColin Finck 	if (*tmp || (sector_size != 512 && sector_size != 1024 &&
1928*c2c66affSColin Finck 		     sector_size != 2048 && sector_size != 4096 &&
1929*c2c66affSColin Finck 		     sector_size != 8192 && sector_size != 16384 &&
1930*c2c66affSColin Finck 		     sector_size != 32768))
1931*c2c66affSColin Finck 	  {
1932*c2c66affSColin Finck 	    printf ("Bad logical sector size : %s\n", optarg);
1933*c2c66affSColin Finck 	    usage ();
1934*c2c66affSColin Finck 	  }
1935*c2c66affSColin Finck 	sector_size_set = 1;
1936*c2c66affSColin Finck 	break;
1937*c2c66affSColin Finck 
1938*c2c66affSColin Finck       case 'v':		/* v : Verbose execution */
1939*c2c66affSColin Finck 	++verbose;
1940*c2c66affSColin Finck 	break;
1941*c2c66affSColin Finck 
1942*c2c66affSColin Finck       default:
1943*c2c66affSColin Finck 	printf( "Unknown option: %c\n", c );
1944*c2c66affSColin Finck 	usage ();
1945*c2c66affSColin Finck       }
1946*c2c66affSColin Finck 
1947*c2c66affSColin Finck   if (optind >= argc)
1948*c2c66affSColin Finck 	  usage();
1949*c2c66affSColin Finck   device_name = argv[optind];	/* Determine the number of blocks in the FS */
1950*c2c66affSColin Finck #ifdef _WIN32
1951*c2c66affSColin Finck   if (device_name[1] == ':' && device_name[2] == '\0') {
1952*c2c66affSColin Finck 	  dev_buf[4] = device_name[0];
1953*c2c66affSColin Finck 	  device_name = dev_buf;
1954*c2c66affSColin Finck 	  is_device = 1;
1955*c2c66affSColin Finck   }
1956*c2c66affSColin Finck #endif
1957*c2c66affSColin Finck   if (!create)
1958*c2c66affSColin Finck     i = count_blocks (device_name); /*  Have a look and see! */
1959*c2c66affSColin Finck   if (optind == argc - 2)	/*  Either check the user specified number */
1960*c2c66affSColin Finck     {
1961*c2c66affSColin Finck       blocks = (int) strtol (argv[optind + 1], &tmp, 0);
1962*c2c66affSColin Finck       if (!create && blocks != i)
1963*c2c66affSColin Finck 	{
1964*c2c66affSColin Finck 	  fprintf (stderr, "Warning: block count mismatch: ");
1965*c2c66affSColin Finck 	  fprintf (stderr, "found %d but assuming %d.\n",i,blocks);
1966*c2c66affSColin Finck 	}
1967*c2c66affSColin Finck     }
1968*c2c66affSColin Finck   else if (optind == argc - 1)	/*  Or use value found */
1969*c2c66affSColin Finck     {
1970*c2c66affSColin Finck       if (create)
1971*c2c66affSColin Finck 	die( "Need intended size with -C." );
1972*c2c66affSColin Finck       blocks = i;
1973*c2c66affSColin Finck       tmp = "";
1974*c2c66affSColin Finck     }
1975*c2c66affSColin Finck   else
1976*c2c66affSColin Finck     usage ();
1977*c2c66affSColin Finck   if (*tmp)
1978*c2c66affSColin Finck     {
1979*c2c66affSColin Finck       printf ("Bad block count : %s\n", argv[optind + 1]);
1980*c2c66affSColin Finck       usage ();
1981*c2c66affSColin Finck     }
1982*c2c66affSColin Finck 
1983*c2c66affSColin Finck   if (check && listfile)	/* Auto and specified bad block handling are mutually */
1984*c2c66affSColin Finck     die ("-c and -l are incompatible");		/* exclusive of each other! */
1985*c2c66affSColin Finck 
1986*c2c66affSColin Finck   if (!create) {
1987*c2c66affSColin Finck     check_mount (device_name);	/* Is the device already mounted? */
1988*c2c66affSColin Finck     dev = open (device_name, O_RDWR|O_SHARED);	/* Is it a suitable device to build the FS on? */
1989*c2c66affSColin Finck     if (dev < 0)
1990*c2c66affSColin Finck       die ("unable to open %s");
1991*c2c66affSColin Finck #ifdef _WIN32
1992*c2c66affSColin Finck 	if (is_device) {
1993*c2c66affSColin Finck 		if (fsctl(dev, FSCTL_LOCK_VOLUME) == -1)
1994*c2c66affSColin Finck 			die("unable to lock %s");
1995*c2c66affSColin Finck 	}
1996*c2c66affSColin Finck #endif
1997*c2c66affSColin Finck   }
1998*c2c66affSColin Finck   else {
1999*c2c66affSColin Finck       loff_t offset = blocks*BLOCK_SIZE - 1;
2000*c2c66affSColin Finck       char null = 0;
2001*c2c66affSColin Finck       /* create the file */
2002*c2c66affSColin Finck       dev = open( device_name, O_RDWR|O_CREAT|O_TRUNC, 0775 );
2003*c2c66affSColin Finck       if (dev < 0)
2004*c2c66affSColin Finck 	die("unable to create %s");
2005*c2c66affSColin Finck       /* seek to the intended end-1, and write one byte. this creates a
2006*c2c66affSColin Finck        * sparse-as-possible file of appropriate size. */
2007*c2c66affSColin Finck       if (llseek( dev, offset, SEEK_SET ) != offset)
2008*c2c66affSColin Finck 	die( "seek failed" );
2009*c2c66affSColin Finck       if (write( dev, &null, 1 ) < 0)
2010*c2c66affSColin Finck 	die( "write failed" );
2011*c2c66affSColin Finck       if (llseek( dev, 0, SEEK_SET ) != 0)
2012*c2c66affSColin Finck 	die( "seek failed" );
2013*c2c66affSColin Finck   }
2014*c2c66affSColin Finck 
2015*c2c66affSColin Finck #ifdef _WIN32
2016*c2c66affSColin Finck   if (!is_device)
2017*c2c66affSColin Finck 	  check = 0;
2018*c2c66affSColin Finck   establish_params();
2019*c2c66affSColin Finck #else
2020*c2c66affSColin Finck   if (fstat (dev, &statbuf) < 0)
2021*c2c66affSColin Finck     die ("unable to stat %s");
2022*c2c66affSColin Finck   if (!S_ISBLK (statbuf.st_mode)) {
2023*c2c66affSColin Finck     statbuf.st_rdev = 0;
2024*c2c66affSColin Finck     check = 0;
2025*c2c66affSColin Finck   }
2026*c2c66affSColin Finck   else
2027*c2c66affSColin Finck     /*
2028*c2c66affSColin Finck      * Ignore any 'full' fixed disk devices, if -I is not given.
2029*c2c66affSColin Finck      * On a MO-disk one doesn't need partitions.  The filesytem can go
2030*c2c66affSColin Finck      * directly to the whole disk.  Under other OSes this is known as
2031*c2c66affSColin Finck      * the 'superfloppy' format.  As I don't know how to find out if
2032*c2c66affSColin Finck      * this is a MO disk I introduce a -I (ignore) switch.  -Joey
2033*c2c66affSColin Finck      */
2034*c2c66affSColin Finck     if (!ignore_full_disk && (
2035*c2c66affSColin Finck 	(statbuf.st_rdev & 0xff3f) == 0x0300 || /* hda, hdb */
2036*c2c66affSColin Finck 	(statbuf.st_rdev & 0xff0f) == 0x0800 || /* sd */
2037*c2c66affSColin Finck 	(statbuf.st_rdev & 0xff3f) == 0x0d00 || /* xd */
2038*c2c66affSColin Finck 	(statbuf.st_rdev & 0xff3f) == 0x1600 )  /* hdc, hdd */
2039*c2c66affSColin Finck 	)
2040*c2c66affSColin Finck       die ("Will not try to make filesystem on '%s'");
2041*c2c66affSColin Finck 
2042*c2c66affSColin Finck   establish_params (statbuf.st_rdev,statbuf.st_size);
2043*c2c66affSColin Finck                                 /* Establish the media parameters */
2044*c2c66affSColin Finck #endif
2045*c2c66affSColin Finck 
2046*c2c66affSColin Finck   setup_tables ();		/* Establish the file system tables */
2047*c2c66affSColin Finck 
2048*c2c66affSColin Finck   if (check)			/* Determine any bad block locations and mark them */
2049*c2c66affSColin Finck     check_blocks ();
2050*c2c66affSColin Finck   else if (listfile)
2051*c2c66affSColin Finck     get_list_blocks (listfile);
2052*c2c66affSColin Finck 
2053*c2c66affSColin Finck   write_tables ();		/* Write the file system tables away! */
2054*c2c66affSColin Finck 
2055*c2c66affSColin Finck #ifdef _WIN32
2056*c2c66affSColin Finck 	if (is_device) {
2057*c2c66affSColin Finck 		if (fsctl(dev, FSCTL_DISMOUNT_VOLUME) == -1)
2058*c2c66affSColin Finck 			die("unable to dismount %s");
2059*c2c66affSColin Finck 		if (fsctl(dev, FSCTL_UNLOCK_VOLUME) == -1)
2060*c2c66affSColin Finck 			die("unable to unlock %s");
2061*c2c66affSColin Finck 	}
2062*c2c66affSColin Finck #endif
2063*c2c66affSColin Finck   exit (0);			/* Terminate with no errors! */
2064*c2c66affSColin Finck }
2065*c2c66affSColin Finck 
2066*c2c66affSColin Finck 
2067*c2c66affSColin Finck /* That's All Folks */
2068*c2c66affSColin Finck /* Local Variables: */
2069*c2c66affSColin Finck /* tab-width: 8     */
2070*c2c66affSColin Finck /* End:             */
2071