xref: /aosp_15_r20/external/newfs_msdos/mkfs_msdos.c (revision d656534b87bd8f59341392a2dadc9aa101e4b018)
1*d656534bSElliott Hughes /*
2*d656534bSElliott Hughes  * Copyright (c) 1998 Robert Nordier
3*d656534bSElliott Hughes  * All rights reserved.
4*d656534bSElliott Hughes  *
5*d656534bSElliott Hughes  * Redistribution and use in source and binary forms, with or without
6*d656534bSElliott Hughes  * modification, are permitted provided that the following conditions
7*d656534bSElliott Hughes  * are met:
8*d656534bSElliott Hughes  * 1. Redistributions of source code must retain the above copyright
9*d656534bSElliott Hughes  *    notice, this list of conditions and the following disclaimer.
10*d656534bSElliott Hughes  * 2. Redistributions in binary form must reproduce the above copyright
11*d656534bSElliott Hughes  *    notice, this list of conditions and the following disclaimer in
12*d656534bSElliott Hughes  *    the documentation and/or other materials provided with the
13*d656534bSElliott Hughes  *    distribution.
14*d656534bSElliott Hughes  *
15*d656534bSElliott Hughes  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
16*d656534bSElliott Hughes  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17*d656534bSElliott Hughes  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*d656534bSElliott Hughes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
19*d656534bSElliott Hughes  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*d656534bSElliott Hughes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21*d656534bSElliott Hughes  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*d656534bSElliott Hughes  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23*d656534bSElliott Hughes  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24*d656534bSElliott Hughes  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25*d656534bSElliott Hughes  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*d656534bSElliott Hughes  */
27*d656534bSElliott Hughes 
28*d656534bSElliott Hughes #ifndef lint
29*d656534bSElliott Hughes static const char rcsid[] =
30*d656534bSElliott Hughes   "$FreeBSD$";
31*d656534bSElliott Hughes #endif /* not lint */
32*d656534bSElliott Hughes 
33*d656534bSElliott Hughes #include <sys/param.h>
34*d656534bSElliott Hughes #ifdef MAKEFS
35*d656534bSElliott Hughes /* In the makefs case we only want struct disklabel */
36*d656534bSElliott Hughes #include <sys/disk/bsd.h>
37*d656534bSElliott Hughes #elif defined(__linux__)
38*d656534bSElliott Hughes #include <linux/fs.h>
39*d656534bSElliott Hughes #include <linux/hdreg.h>
40*d656534bSElliott Hughes #include <sys/ioctl.h>
41*d656534bSElliott Hughes #elif defined(__APPLE__)
42*d656534bSElliott Hughes /* Nothing. */
43*d656534bSElliott Hughes #else
44*d656534bSElliott Hughes #include <sys/fdcio.h>
45*d656534bSElliott Hughes #include <sys/disk.h>
46*d656534bSElliott Hughes #include <sys/disklabel.h>
47*d656534bSElliott Hughes #include <sys/mount.h>
48*d656534bSElliott Hughes #endif
49*d656534bSElliott Hughes #include <sys/stat.h>
50*d656534bSElliott Hughes #if __has_include(<sys/sysctl.h>)
51*d656534bSElliott Hughes #include <sys/sysctl.h>
52*d656534bSElliott Hughes #endif
53*d656534bSElliott Hughes #include <sys/time.h>
54*d656534bSElliott Hughes 
55*d656534bSElliott Hughes #include <assert.h>
56*d656534bSElliott Hughes #include <ctype.h>
57*d656534bSElliott Hughes #include <err.h>
58*d656534bSElliott Hughes #include <errno.h>
59*d656534bSElliott Hughes #include <fcntl.h>
60*d656534bSElliott Hughes #include <inttypes.h>
61*d656534bSElliott Hughes #include <paths.h>
62*d656534bSElliott Hughes #include <signal.h>
63*d656534bSElliott Hughes #include <stdio.h>
64*d656534bSElliott Hughes #include <stdlib.h>
65*d656534bSElliott Hughes #include <string.h>
66*d656534bSElliott Hughes #include <time.h>
67*d656534bSElliott Hughes #include <unistd.h>
68*d656534bSElliott Hughes 
69*d656534bSElliott Hughes #include "mkfs_msdos.h"
70*d656534bSElliott Hughes 
71*d656534bSElliott Hughes #define	MAXU16	  0xffff	/* maximum unsigned 16-bit quantity */
72*d656534bSElliott Hughes #define	BPN	  4		/* bits per nibble */
73*d656534bSElliott Hughes #define	NPB	  2		/* nibbles per byte */
74*d656534bSElliott Hughes 
75*d656534bSElliott Hughes #define	DOSMAGIC  0xaa55	/* DOS magic number */
76*d656534bSElliott Hughes #define	MINBPS	  512		/* minimum bytes per sector */
77*d656534bSElliott Hughes #define	MAXBPS    4096		/* maximum bytes per sector */
78*d656534bSElliott Hughes #define	MAXSPC	  128		/* maximum sectors per cluster */
79*d656534bSElliott Hughes #define	MAXNFT	  16		/* maximum number of FATs */
80*d656534bSElliott Hughes #define	DEFBLK	  4096		/* default block size */
81*d656534bSElliott Hughes #define	DEFBLK16  2048		/* default block size FAT16 */
82*d656534bSElliott Hughes #define	DEFRDE	  512		/* default root directory entries */
83*d656534bSElliott Hughes #define	RESFTE	  2		/* reserved FAT entries */
84*d656534bSElliott Hughes #define	MINCLS12  1U		/* minimum FAT12 clusters */
85*d656534bSElliott Hughes #define	MINCLS16  0xff5U	/* minimum FAT16 clusters */
86*d656534bSElliott Hughes #define	MINCLS32  0xfff5U	/* minimum FAT32 clusters */
87*d656534bSElliott Hughes #define	MAXCLS12  0xff4U	/* maximum FAT12 clusters */
88*d656534bSElliott Hughes #define	MAXCLS16  0xfff4U	/* maximum FAT16 clusters */
89*d656534bSElliott Hughes #define	MAXCLS32  0xffffff4U	/* maximum FAT32 clusters */
90*d656534bSElliott Hughes 
91*d656534bSElliott Hughes #define	mincls(fat)  ((fat) == 12 ? MINCLS12 :	\
92*d656534bSElliott Hughes 		      (fat) == 16 ? MINCLS16 :	\
93*d656534bSElliott Hughes 				    MINCLS32)
94*d656534bSElliott Hughes 
95*d656534bSElliott Hughes #define	maxcls(fat)  ((fat) == 12 ? MAXCLS12 :	\
96*d656534bSElliott Hughes 		      (fat) == 16 ? MAXCLS16 :	\
97*d656534bSElliott Hughes 				    MAXCLS32)
98*d656534bSElliott Hughes 
99*d656534bSElliott Hughes #define	mk1(p, x)				\
100*d656534bSElliott Hughes     (p) = (u_int8_t)(x)
101*d656534bSElliott Hughes 
102*d656534bSElliott Hughes #define	mk2(p, x)				\
103*d656534bSElliott Hughes     (p)[0] = (u_int8_t)(x),			\
104*d656534bSElliott Hughes     (p)[1] = (u_int8_t)((x) >> 010)
105*d656534bSElliott Hughes 
106*d656534bSElliott Hughes #define	mk4(p, x)				\
107*d656534bSElliott Hughes     (p)[0] = (u_int8_t)(x),			\
108*d656534bSElliott Hughes     (p)[1] = (u_int8_t)((x) >> 010),		\
109*d656534bSElliott Hughes     (p)[2] = (u_int8_t)((x) >> 020),		\
110*d656534bSElliott Hughes     (p)[3] = (u_int8_t)((x) >> 030)
111*d656534bSElliott Hughes 
112*d656534bSElliott Hughes struct bs {
113*d656534bSElliott Hughes     u_int8_t bsJump[3];			/* bootstrap entry point */
114*d656534bSElliott Hughes     u_int8_t bsOemName[8];		/* OEM name and version */
115*d656534bSElliott Hughes } __packed;
116*d656534bSElliott Hughes 
117*d656534bSElliott Hughes struct bsbpb {
118*d656534bSElliott Hughes     u_int8_t bpbBytesPerSec[2];		/* bytes per sector */
119*d656534bSElliott Hughes     u_int8_t bpbSecPerClust;		/* sectors per cluster */
120*d656534bSElliott Hughes     u_int8_t bpbResSectors[2];		/* reserved sectors */
121*d656534bSElliott Hughes     u_int8_t bpbFATs;			/* number of FATs */
122*d656534bSElliott Hughes     u_int8_t bpbRootDirEnts[2];		/* root directory entries */
123*d656534bSElliott Hughes     u_int8_t bpbSectors[2];		/* total sectors */
124*d656534bSElliott Hughes     u_int8_t bpbMedia;			/* media descriptor */
125*d656534bSElliott Hughes     u_int8_t bpbFATsecs[2];		/* sectors per FAT */
126*d656534bSElliott Hughes     u_int8_t bpbSecPerTrack[2];		/* sectors per track */
127*d656534bSElliott Hughes     u_int8_t bpbHeads[2];		/* drive heads */
128*d656534bSElliott Hughes     u_int8_t bpbHiddenSecs[4];		/* hidden sectors */
129*d656534bSElliott Hughes     u_int8_t bpbHugeSectors[4];		/* big total sectors */
130*d656534bSElliott Hughes } __packed;
131*d656534bSElliott Hughes 
132*d656534bSElliott Hughes struct bsxbpb {
133*d656534bSElliott Hughes     u_int8_t bpbBigFATsecs[4];		/* big sectors per FAT */
134*d656534bSElliott Hughes     u_int8_t bpbExtFlags[2];		/* FAT control flags */
135*d656534bSElliott Hughes     u_int8_t bpbFSVers[2];		/* file system version */
136*d656534bSElliott Hughes     u_int8_t bpbRootClust[4];		/* root directory start cluster */
137*d656534bSElliott Hughes     u_int8_t bpbFSInfo[2];		/* file system info sector */
138*d656534bSElliott Hughes     u_int8_t bpbBackup[2];		/* backup boot sector */
139*d656534bSElliott Hughes     u_int8_t bpbReserved[12];		/* reserved */
140*d656534bSElliott Hughes } __packed;
141*d656534bSElliott Hughes 
142*d656534bSElliott Hughes struct bsx {
143*d656534bSElliott Hughes     u_int8_t exDriveNumber;		/* drive number */
144*d656534bSElliott Hughes     u_int8_t exReserved1;		/* reserved */
145*d656534bSElliott Hughes     u_int8_t exBootSignature;		/* extended boot signature */
146*d656534bSElliott Hughes     u_int8_t exVolumeID[4];		/* volume ID number */
147*d656534bSElliott Hughes     u_int8_t exVolumeLabel[11];		/* volume label */
148*d656534bSElliott Hughes     u_int8_t exFileSysType[8];		/* file system type */
149*d656534bSElliott Hughes } __packed;
150*d656534bSElliott Hughes 
151*d656534bSElliott Hughes struct de {
152*d656534bSElliott Hughes     u_int8_t deName[11];		/* name and extension */
153*d656534bSElliott Hughes     u_int8_t deAttributes;		/* attributes */
154*d656534bSElliott Hughes     u_int8_t rsvd[10];			/* reserved */
155*d656534bSElliott Hughes     u_int8_t deMTime[2];		/* last-modified time */
156*d656534bSElliott Hughes     u_int8_t deMDate[2];		/* last-modified date */
157*d656534bSElliott Hughes     u_int8_t deStartCluster[2];		/* starting cluster */
158*d656534bSElliott Hughes     u_int8_t deFileSize[4];		/* size */
159*d656534bSElliott Hughes } __packed;
160*d656534bSElliott Hughes 
161*d656534bSElliott Hughes struct bpb {
162*d656534bSElliott Hughes     u_int bpbBytesPerSec;		/* bytes per sector */
163*d656534bSElliott Hughes     u_int bpbSecPerClust;		/* sectors per cluster */
164*d656534bSElliott Hughes     u_int bpbResSectors;		/* reserved sectors */
165*d656534bSElliott Hughes     u_int bpbFATs;			/* number of FATs */
166*d656534bSElliott Hughes     u_int bpbRootDirEnts;		/* root directory entries */
167*d656534bSElliott Hughes     u_int bpbSectors;			/* total sectors */
168*d656534bSElliott Hughes     u_int bpbMedia;			/* media descriptor */
169*d656534bSElliott Hughes     u_int bpbFATsecs;			/* sectors per FAT */
170*d656534bSElliott Hughes     u_int bpbSecPerTrack;		/* sectors per track */
171*d656534bSElliott Hughes     u_int bpbHeads;			/* drive heads */
172*d656534bSElliott Hughes     u_int bpbHiddenSecs;		/* hidden sectors */
173*d656534bSElliott Hughes     u_int bpbHugeSectors; 		/* big total sectors */
174*d656534bSElliott Hughes     u_int bpbBigFATsecs; 		/* big sectors per FAT */
175*d656534bSElliott Hughes     u_int bpbRootClust; 		/* root directory start cluster */
176*d656534bSElliott Hughes     u_int bpbFSInfo; 			/* file system info sector */
177*d656534bSElliott Hughes     u_int bpbBackup; 			/* backup boot sector */
178*d656534bSElliott Hughes };
179*d656534bSElliott Hughes 
180*d656534bSElliott Hughes #define	BPBGAP 0, 0, 0, 0, 0, 0
181*d656534bSElliott Hughes 
182*d656534bSElliott Hughes static struct {
183*d656534bSElliott Hughes     const char *name;
184*d656534bSElliott Hughes     struct bpb bpb;
185*d656534bSElliott Hughes } const stdfmt[] = {
186*d656534bSElliott Hughes     {"160",  {512, 1, 1, 2,  64,  320, 0xfe, 1,  8, 1, BPBGAP}},
187*d656534bSElliott Hughes     {"180",  {512, 1, 1, 2,  64,  360, 0xfc, 2,  9, 1, BPBGAP}},
188*d656534bSElliott Hughes     {"320",  {512, 2, 1, 2, 112,  640, 0xff, 1,  8, 2, BPBGAP}},
189*d656534bSElliott Hughes     {"360",  {512, 2, 1, 2, 112,  720, 0xfd, 2,  9, 2, BPBGAP}},
190*d656534bSElliott Hughes     {"640",  {512, 2, 1, 2, 112, 1280, 0xfb, 2,  8, 2, BPBGAP}},
191*d656534bSElliott Hughes     {"720",  {512, 2, 1, 2, 112, 1440, 0xf9, 3,  9, 2, BPBGAP}},
192*d656534bSElliott Hughes     {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2, BPBGAP}},
193*d656534bSElliott Hughes     {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2,  8, 2, BPBGAP}},
194*d656534bSElliott Hughes     {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2, BPBGAP}},
195*d656534bSElliott Hughes     {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2, BPBGAP}}
196*d656534bSElliott Hughes };
197*d656534bSElliott Hughes 
198*d656534bSElliott Hughes static const u_int8_t bootcode[] = {
199*d656534bSElliott Hughes     0xfa,			/* cli		    */
200*d656534bSElliott Hughes     0x31, 0xc0, 		/* xor	   ax,ax    */
201*d656534bSElliott Hughes     0x8e, 0xd0, 		/* mov	   ss,ax    */
202*d656534bSElliott Hughes     0xbc, 0x00, 0x7c,		/* mov	   sp,7c00h */
203*d656534bSElliott Hughes     0xfb,			/* sti		    */
204*d656534bSElliott Hughes     0x8e, 0xd8, 		/* mov	   ds,ax    */
205*d656534bSElliott Hughes     0xe8, 0x00, 0x00,		/* call    $ + 3    */
206*d656534bSElliott Hughes     0x5e,			/* pop	   si	    */
207*d656534bSElliott Hughes     0x83, 0xc6, 0x19,		/* add	   si,+19h  */
208*d656534bSElliott Hughes     0xbb, 0x07, 0x00,		/* mov	   bx,0007h */
209*d656534bSElliott Hughes     0xfc,			/* cld		    */
210*d656534bSElliott Hughes     0xac,			/* lodsb	    */
211*d656534bSElliott Hughes     0x84, 0xc0, 		/* test    al,al    */
212*d656534bSElliott Hughes     0x74, 0x06, 		/* jz	   $ + 8    */
213*d656534bSElliott Hughes     0xb4, 0x0e, 		/* mov	   ah,0eh   */
214*d656534bSElliott Hughes     0xcd, 0x10, 		/* int	   10h	    */
215*d656534bSElliott Hughes     0xeb, 0xf5, 		/* jmp	   $ - 9    */
216*d656534bSElliott Hughes     0x30, 0xe4, 		/* xor	   ah,ah    */
217*d656534bSElliott Hughes     0xcd, 0x16, 		/* int	   16h	    */
218*d656534bSElliott Hughes     0xcd, 0x19, 		/* int	   19h	    */
219*d656534bSElliott Hughes     0x0d, 0x0a,
220*d656534bSElliott Hughes     'N', 'o', 'n', '-', 's', 'y', 's', 't',
221*d656534bSElliott Hughes     'e', 'm', ' ', 'd', 'i', 's', 'k',
222*d656534bSElliott Hughes     0x0d, 0x0a,
223*d656534bSElliott Hughes     'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
224*d656534bSElliott Hughes     'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
225*d656534bSElliott Hughes     ' ', 'r', 'e', 'b', 'o', 'o', 't',
226*d656534bSElliott Hughes     0x0d, 0x0a,
227*d656534bSElliott Hughes     0
228*d656534bSElliott Hughes };
229*d656534bSElliott Hughes 
230*d656534bSElliott Hughes static volatile sig_atomic_t got_siginfo;
231*d656534bSElliott Hughes static void infohandler(int);
232*d656534bSElliott Hughes 
233*d656534bSElliott Hughes static int check_mounted(const char *, mode_t);
234*d656534bSElliott Hughes static ssize_t getchunksize(void);
235*d656534bSElliott Hughes static int getstdfmt(const char *, struct bpb *);
236*d656534bSElliott Hughes static int getdiskinfo(int, const char *, const char *, int, struct bpb *);
237*d656534bSElliott Hughes static void print_bpb(struct bpb *);
238*d656534bSElliott Hughes static int ckgeom(const char *, u_int, const char *);
239*d656534bSElliott Hughes static void mklabel(u_int8_t *, const char *);
240*d656534bSElliott Hughes static int oklabel(const char *);
241*d656534bSElliott Hughes static void setstr(u_int8_t *, const char *, size_t);
242*d656534bSElliott Hughes 
243*d656534bSElliott Hughes int
mkfs_msdos(const char * fname,const char * dtype,const struct msdos_options * op)244*d656534bSElliott Hughes mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op)
245*d656534bSElliott Hughes {
246*d656534bSElliott Hughes     char buf[MAXPATHLEN];
247*d656534bSElliott Hughes     struct sigaction si_sa;
248*d656534bSElliott Hughes     struct stat sb;
249*d656534bSElliott Hughes     struct timeval tv;
250*d656534bSElliott Hughes     struct bpb bpb;
251*d656534bSElliott Hughes     struct tm *tm;
252*d656534bSElliott Hughes     struct bs *bs;
253*d656534bSElliott Hughes     struct bsbpb *bsbpb;
254*d656534bSElliott Hughes     struct bsxbpb *bsxbpb;
255*d656534bSElliott Hughes     struct bsx *bsx;
256*d656534bSElliott Hughes     struct de *de;
257*d656534bSElliott Hughes     u_int8_t *img;
258*d656534bSElliott Hughes     u_int8_t *physbuf, *physbuf_end;
259*d656534bSElliott Hughes     const char *bname;
260*d656534bSElliott Hughes     ssize_t n;
261*d656534bSElliott Hughes     time_t now;
262*d656534bSElliott Hughes     u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
263*d656534bSElliott Hughes     u_int extra_res, alignment, saved_x, attempts=0;
264*d656534bSElliott Hughes     bool set_res, set_spf, set_spc;
265*d656534bSElliott Hughes     int fd, fd1, rv;
266*d656534bSElliott Hughes     struct msdos_options o = *op;
267*d656534bSElliott Hughes     ssize_t chunksize;
268*d656534bSElliott Hughes 
269*d656534bSElliott Hughes     physbuf = NULL;
270*d656534bSElliott Hughes     rv = -1;
271*d656534bSElliott Hughes     fd = fd1 = -1;
272*d656534bSElliott Hughes 
273*d656534bSElliott Hughes     if (o.block_size && o.sectors_per_cluster) {
274*d656534bSElliott Hughes 	warnx("Cannot specify both block size and sectors per cluster");
275*d656534bSElliott Hughes 	goto done;
276*d656534bSElliott Hughes     }
277*d656534bSElliott Hughes     if (o.OEM_string && strlen(o.OEM_string) > 8) {
278*d656534bSElliott Hughes 	warnx("%s: bad OEM string", o.OEM_string);
279*d656534bSElliott Hughes 	goto done;
280*d656534bSElliott Hughes     }
281*d656534bSElliott Hughes     if (o.create_size) {
282*d656534bSElliott Hughes 	if (o.no_create) {
283*d656534bSElliott Hughes 	    warnx("create (-C) is incompatible with -N");
284*d656534bSElliott Hughes 	    goto done;
285*d656534bSElliott Hughes 	}
286*d656534bSElliott Hughes 	fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644);
287*d656534bSElliott Hughes 	if (fd == -1) {
288*d656534bSElliott Hughes 	    warnx("failed to create %s", fname);
289*d656534bSElliott Hughes 	    goto done;
290*d656534bSElliott Hughes 	}
291*d656534bSElliott Hughes 	if (ftruncate(fd, o.create_size)) {
292*d656534bSElliott Hughes 	    warnx("failed to initialize %jd bytes", (intmax_t)o.create_size);
293*d656534bSElliott Hughes 	    goto done;
294*d656534bSElliott Hughes 	}
295*d656534bSElliott Hughes     } else if ((fd = open(fname, o.no_create ? O_RDONLY : O_RDWR)) == -1) {
296*d656534bSElliott Hughes 	warn("%s", fname);
297*d656534bSElliott Hughes 	goto done;
298*d656534bSElliott Hughes     }
299*d656534bSElliott Hughes     if (fstat(fd, &sb)) {
300*d656534bSElliott Hughes 	warn("%s", fname);
301*d656534bSElliott Hughes 	goto done;
302*d656534bSElliott Hughes     }
303*d656534bSElliott Hughes     if (o.create_size) {
304*d656534bSElliott Hughes 	if (!S_ISREG(sb.st_mode))
305*d656534bSElliott Hughes 	    warnx("warning, %s is not a regular file", fname);
306*d656534bSElliott Hughes     } else {
307*d656534bSElliott Hughes #ifdef MAKEFS
308*d656534bSElliott Hughes 	errx(1, "o.create_size must be set!");
309*d656534bSElliott Hughes #else
310*d656534bSElliott Hughes 	if (!S_ISCHR(sb.st_mode))
311*d656534bSElliott Hughes 	    warnx("warning, %s is not a character device", fname);
312*d656534bSElliott Hughes #endif
313*d656534bSElliott Hughes     }
314*d656534bSElliott Hughes #ifndef MAKEFS
315*d656534bSElliott Hughes     if (!o.no_create)
316*d656534bSElliott Hughes 	if (check_mounted(fname, sb.st_mode) == -1)
317*d656534bSElliott Hughes 	    goto done;
318*d656534bSElliott Hughes #endif
319*d656534bSElliott Hughes     if (o.offset && o.offset != lseek(fd, o.offset, SEEK_SET)) {
320*d656534bSElliott Hughes 	warnx("cannot seek to %jd", (intmax_t)o.offset);
321*d656534bSElliott Hughes 	goto done;
322*d656534bSElliott Hughes     }
323*d656534bSElliott Hughes     memset(&bpb, 0, sizeof(bpb));
324*d656534bSElliott Hughes     if (o.floppy) {
325*d656534bSElliott Hughes 	if (getstdfmt(o.floppy, &bpb) == -1)
326*d656534bSElliott Hughes 	    goto done;
327*d656534bSElliott Hughes 	bpb.bpbHugeSectors = bpb.bpbSectors;
328*d656534bSElliott Hughes 	bpb.bpbSectors = 0;
329*d656534bSElliott Hughes 	bpb.bpbBigFATsecs = bpb.bpbFATsecs;
330*d656534bSElliott Hughes 	bpb.bpbFATsecs = 0;
331*d656534bSElliott Hughes     }
332*d656534bSElliott Hughes     if (o.drive_heads)
333*d656534bSElliott Hughes 	bpb.bpbHeads = o.drive_heads;
334*d656534bSElliott Hughes     if (o.sectors_per_track)
335*d656534bSElliott Hughes 	bpb.bpbSecPerTrack = o.sectors_per_track;
336*d656534bSElliott Hughes     if (o.bytes_per_sector)
337*d656534bSElliott Hughes 	bpb.bpbBytesPerSec = o.bytes_per_sector;
338*d656534bSElliott Hughes     if (o.size)
339*d656534bSElliott Hughes 	bpb.bpbHugeSectors = o.size;
340*d656534bSElliott Hughes     if (o.hidden_sectors_set)
341*d656534bSElliott Hughes 	bpb.bpbHiddenSecs = o.hidden_sectors;
342*d656534bSElliott Hughes     if (!(o.floppy || (o.drive_heads && o.sectors_per_track &&
343*d656534bSElliott Hughes 	o.bytes_per_sector && o.size && o.hidden_sectors_set))) {
344*d656534bSElliott Hughes 	if (getdiskinfo(fd, fname, dtype, o.hidden_sectors_set, &bpb) == -1)
345*d656534bSElliott Hughes 		goto done;
346*d656534bSElliott Hughes 	bpb.bpbHugeSectors -= (o.offset / bpb.bpbBytesPerSec);
347*d656534bSElliott Hughes 	if (bpb.bpbSecPerClust == 0) {	/* set defaults */
348*d656534bSElliott Hughes 	    if (bpb.bpbHugeSectors <= 6000)	/* about 3MB -> 512 bytes */
349*d656534bSElliott Hughes 		bpb.bpbSecPerClust = 1;
350*d656534bSElliott Hughes 	    else if (bpb.bpbHugeSectors <= (1<<17)) /* 64M -> 4k */
351*d656534bSElliott Hughes 		bpb.bpbSecPerClust = 8;
352*d656534bSElliott Hughes 	    else if (bpb.bpbHugeSectors <= (1<<19)) /* 256M -> 8k */
353*d656534bSElliott Hughes 		bpb.bpbSecPerClust = 16;
354*d656534bSElliott Hughes 	    else if (bpb.bpbHugeSectors <= (1<<21)) /* 1G -> 16k */
355*d656534bSElliott Hughes 		bpb.bpbSecPerClust = 32;
356*d656534bSElliott Hughes 	    else
357*d656534bSElliott Hughes 		bpb.bpbSecPerClust = 64;		/* otherwise 32k */
358*d656534bSElliott Hughes 	}
359*d656534bSElliott Hughes     }
360*d656534bSElliott Hughes     if (bpb.bpbBytesPerSec < MINBPS ||
361*d656534bSElliott Hughes         bpb.bpbBytesPerSec > MAXBPS ||
362*d656534bSElliott Hughes 	!powerof2(bpb.bpbBytesPerSec)) {
363*d656534bSElliott Hughes 	warnx("Invalid bytes/sector (%u): must be 512, 1024, 2048 or 4096",
364*d656534bSElliott Hughes 	    bpb.bpbBytesPerSec);
365*d656534bSElliott Hughes 	goto done;
366*d656534bSElliott Hughes     }
367*d656534bSElliott Hughes 
368*d656534bSElliott Hughes     if (o.volume_label && !oklabel(o.volume_label)) {
369*d656534bSElliott Hughes 	warnx("%s: bad volume label", o.volume_label);
370*d656534bSElliott Hughes 	goto done;
371*d656534bSElliott Hughes     }
372*d656534bSElliott Hughes     if (!(fat = o.fat_type)) {
373*d656534bSElliott Hughes 	if (o.floppy)
374*d656534bSElliott Hughes 	    fat = 12;
375*d656534bSElliott Hughes 	else if (!o.directory_entries && (o.info_sector || o.backup_sector))
376*d656534bSElliott Hughes 	    fat = 32;
377*d656534bSElliott Hughes     }
378*d656534bSElliott Hughes     if ((fat == 32 && o.directory_entries) || (fat != 32 && (o.info_sector || o.backup_sector))) {
379*d656534bSElliott Hughes 	warnx("-%c is not a legal FAT%s option",
380*d656534bSElliott Hughes 	     fat == 32 ? 'e' : o.info_sector ? 'i' : 'k',
381*d656534bSElliott Hughes 	     fat == 32 ? "32" : "12/16");
382*d656534bSElliott Hughes 	goto done;
383*d656534bSElliott Hughes     }
384*d656534bSElliott Hughes     if (o.floppy && fat == 32)
385*d656534bSElliott Hughes 	bpb.bpbRootDirEnts = 0;
386*d656534bSElliott Hughes     if (fat != 0 && fat != 12 && fat != 16 && fat != 32) {
387*d656534bSElliott Hughes 	warnx("%d: bad FAT type", fat);
388*d656534bSElliott Hughes 	goto done;
389*d656534bSElliott Hughes     }
390*d656534bSElliott Hughes 
391*d656534bSElliott Hughes     if (o.block_size) {
392*d656534bSElliott Hughes 	if (!powerof2(o.block_size)) {
393*d656534bSElliott Hughes 	    warnx("block size (%u) is not a power of 2", o.block_size);
394*d656534bSElliott Hughes 	    goto done;
395*d656534bSElliott Hughes 	}
396*d656534bSElliott Hughes 	if (o.block_size < bpb.bpbBytesPerSec) {
397*d656534bSElliott Hughes 	    warnx("block size (%u) is too small; minimum is %u",
398*d656534bSElliott Hughes 		 o.block_size, bpb.bpbBytesPerSec);
399*d656534bSElliott Hughes 	    goto done;
400*d656534bSElliott Hughes 	}
401*d656534bSElliott Hughes 	if (o.block_size > bpb.bpbBytesPerSec * MAXSPC) {
402*d656534bSElliott Hughes 	    warnx("block size (%u) is too large; maximum is %u",
403*d656534bSElliott Hughes 		 o.block_size, bpb.bpbBytesPerSec * MAXSPC);
404*d656534bSElliott Hughes 	    goto done;
405*d656534bSElliott Hughes 	}
406*d656534bSElliott Hughes 	bpb.bpbSecPerClust = o.block_size / bpb.bpbBytesPerSec;
407*d656534bSElliott Hughes     }
408*d656534bSElliott Hughes     if (o.sectors_per_cluster) {
409*d656534bSElliott Hughes 	if (!powerof2(o.sectors_per_cluster)) {
410*d656534bSElliott Hughes 	    warnx("sectors/cluster (%u) is not a power of 2",
411*d656534bSElliott Hughes 		o.sectors_per_cluster);
412*d656534bSElliott Hughes 	    goto done;
413*d656534bSElliott Hughes 	}
414*d656534bSElliott Hughes 	bpb.bpbSecPerClust = o.sectors_per_cluster;
415*d656534bSElliott Hughes     }
416*d656534bSElliott Hughes     if (o.reserved_sectors)
417*d656534bSElliott Hughes 	bpb.bpbResSectors = o.reserved_sectors;
418*d656534bSElliott Hughes     if (o.num_FAT) {
419*d656534bSElliott Hughes 	if (o.num_FAT > MAXNFT) {
420*d656534bSElliott Hughes 	    warnx("number of FATs (%u) is too large; maximum is %u",
421*d656534bSElliott Hughes 		 o.num_FAT, MAXNFT);
422*d656534bSElliott Hughes 	    goto done;
423*d656534bSElliott Hughes 	}
424*d656534bSElliott Hughes 	bpb.bpbFATs = o.num_FAT;
425*d656534bSElliott Hughes     }
426*d656534bSElliott Hughes     if (o.directory_entries)
427*d656534bSElliott Hughes 	bpb.bpbRootDirEnts = o.directory_entries;
428*d656534bSElliott Hughes     if (o.media_descriptor_set) {
429*d656534bSElliott Hughes 	if (o.media_descriptor < 0xf0) {
430*d656534bSElliott Hughes 	    warnx("illegal media descriptor (%#x)", o.media_descriptor);
431*d656534bSElliott Hughes 	    goto done;
432*d656534bSElliott Hughes 	}
433*d656534bSElliott Hughes 	bpb.bpbMedia = o.media_descriptor;
434*d656534bSElliott Hughes     }
435*d656534bSElliott Hughes     if (o.sectors_per_fat)
436*d656534bSElliott Hughes 	bpb.bpbBigFATsecs = o.sectors_per_fat;
437*d656534bSElliott Hughes     if (o.info_sector)
438*d656534bSElliott Hughes 	bpb.bpbFSInfo = o.info_sector;
439*d656534bSElliott Hughes     if (o.backup_sector)
440*d656534bSElliott Hughes 	bpb.bpbBackup = o.backup_sector;
441*d656534bSElliott Hughes     bss = 1;
442*d656534bSElliott Hughes     bname = NULL;
443*d656534bSElliott Hughes     fd1 = -1;
444*d656534bSElliott Hughes     if (o.bootstrap) {
445*d656534bSElliott Hughes 	bname = o.bootstrap;
446*d656534bSElliott Hughes 	if (!strchr(bname, '/')) {
447*d656534bSElliott Hughes 	    snprintf(buf, sizeof(buf), "/boot/%s", bname);
448*d656534bSElliott Hughes 	    bname = buf;
449*d656534bSElliott Hughes 	}
450*d656534bSElliott Hughes 	if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb)) {
451*d656534bSElliott Hughes 	    warn("%s", bname);
452*d656534bSElliott Hughes 	    goto done;
453*d656534bSElliott Hughes 	}
454*d656534bSElliott Hughes 	if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bpbBytesPerSec ||
455*d656534bSElliott Hughes 	    sb.st_size < bpb.bpbBytesPerSec ||
456*d656534bSElliott Hughes 	    sb.st_size > bpb.bpbBytesPerSec * MAXU16) {
457*d656534bSElliott Hughes 	    warnx("%s: inappropriate file type or format", bname);
458*d656534bSElliott Hughes 	    goto done;
459*d656534bSElliott Hughes 	}
460*d656534bSElliott Hughes 	bss = sb.st_size / bpb.bpbBytesPerSec;
461*d656534bSElliott Hughes     }
462*d656534bSElliott Hughes     if (!bpb.bpbFATs)
463*d656534bSElliott Hughes 	bpb.bpbFATs = 2;
464*d656534bSElliott Hughes     if (!fat) {
465*d656534bSElliott Hughes 	if (bpb.bpbHugeSectors < (bpb.bpbResSectors ? bpb.bpbResSectors : bss) +
466*d656534bSElliott Hughes 	    howmany((RESFTE + (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1)) *
467*d656534bSElliott Hughes 		(bpb.bpbSecPerClust ? 16 : 12) / BPN,
468*d656534bSElliott Hughes 		bpb.bpbBytesPerSec * NPB) *
469*d656534bSElliott Hughes 	    bpb.bpbFATs +
470*d656534bSElliott Hughes 	    howmany(bpb.bpbRootDirEnts ? bpb.bpbRootDirEnts : DEFRDE,
471*d656534bSElliott Hughes 		    bpb.bpbBytesPerSec / sizeof(struct de)) +
472*d656534bSElliott Hughes 	    (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1) *
473*d656534bSElliott Hughes 	    (bpb.bpbSecPerClust ? bpb.bpbSecPerClust :
474*d656534bSElliott Hughes 	     howmany(DEFBLK, bpb.bpbBytesPerSec)))
475*d656534bSElliott Hughes 	    fat = 12;
476*d656534bSElliott Hughes 	else if (bpb.bpbRootDirEnts || bpb.bpbHugeSectors <
477*d656534bSElliott Hughes 		 (bpb.bpbResSectors ? bpb.bpbResSectors : bss) +
478*d656534bSElliott Hughes 		 howmany((RESFTE + MAXCLS16) * 2, bpb.bpbBytesPerSec) *
479*d656534bSElliott Hughes 		 bpb.bpbFATs +
480*d656534bSElliott Hughes 		 howmany(DEFRDE, bpb.bpbBytesPerSec / sizeof(struct de)) +
481*d656534bSElliott Hughes 		 (MAXCLS16 + 1) *
482*d656534bSElliott Hughes 		 (bpb.bpbSecPerClust ? bpb.bpbSecPerClust :
483*d656534bSElliott Hughes 		  howmany(8192, bpb.bpbBytesPerSec)))
484*d656534bSElliott Hughes 	    fat = 16;
485*d656534bSElliott Hughes 	else
486*d656534bSElliott Hughes 	    fat = 32;
487*d656534bSElliott Hughes     }
488*d656534bSElliott Hughes     x = bss;
489*d656534bSElliott Hughes     if (fat == 32) {
490*d656534bSElliott Hughes 	if (!bpb.bpbFSInfo) {
491*d656534bSElliott Hughes 	    if (x == MAXU16 || x == bpb.bpbBackup) {
492*d656534bSElliott Hughes 		warnx("no room for info sector");
493*d656534bSElliott Hughes 		goto done;
494*d656534bSElliott Hughes 	    }
495*d656534bSElliott Hughes 	    bpb.bpbFSInfo = x;
496*d656534bSElliott Hughes 	}
497*d656534bSElliott Hughes 	if (bpb.bpbFSInfo != MAXU16 && x <= bpb.bpbFSInfo)
498*d656534bSElliott Hughes 	    x = bpb.bpbFSInfo + 1;
499*d656534bSElliott Hughes 	if (!bpb.bpbBackup) {
500*d656534bSElliott Hughes 	    if (x == MAXU16) {
501*d656534bSElliott Hughes 		warnx("no room for backup sector");
502*d656534bSElliott Hughes 		goto done;
503*d656534bSElliott Hughes 	    }
504*d656534bSElliott Hughes 	    bpb.bpbBackup = x;
505*d656534bSElliott Hughes 	} else if (bpb.bpbBackup != MAXU16 && bpb.bpbBackup == bpb.bpbFSInfo) {
506*d656534bSElliott Hughes 	    warnx("backup sector would overwrite info sector");
507*d656534bSElliott Hughes 	    goto done;
508*d656534bSElliott Hughes 	}
509*d656534bSElliott Hughes 	if (bpb.bpbBackup != MAXU16 && x <= bpb.bpbBackup)
510*d656534bSElliott Hughes 	    x = bpb.bpbBackup + 1;
511*d656534bSElliott Hughes     }
512*d656534bSElliott Hughes 
513*d656534bSElliott Hughes     extra_res = 0;
514*d656534bSElliott Hughes     alignment = 0;
515*d656534bSElliott Hughes     set_res = (bpb.bpbResSectors == 0);
516*d656534bSElliott Hughes     set_spf = (bpb.bpbBigFATsecs == 0);
517*d656534bSElliott Hughes     set_spc = (bpb.bpbSecPerClust == 0);
518*d656534bSElliott Hughes     saved_x = x;
519*d656534bSElliott Hughes 
520*d656534bSElliott Hughes     /*
521*d656534bSElliott Hughes      * Attempt to align the root directory to cluster if o.align is set.
522*d656534bSElliott Hughes      * This is done by padding with reserved blocks. Note that this can
523*d656534bSElliott Hughes      * cause other factors to change, which can in turn change the alignment.
524*d656534bSElliott Hughes      * This should take at most 2 iterations, as increasing the reserved
525*d656534bSElliott Hughes      * amount may cause the FAT size to decrease by 1, requiring another
526*d656534bSElliott Hughes      * bpbFATs reserved blocks. If bpbSecPerClust changes, it will
527*d656534bSElliott Hughes      * be half of its previous size, and thus will not throw off alignment.
528*d656534bSElliott Hughes      */
529*d656534bSElliott Hughes     do {
530*d656534bSElliott Hughes 	x = saved_x;
531*d656534bSElliott Hughes 	if (set_res)
532*d656534bSElliott Hughes 	    bpb.bpbResSectors = ((fat == 32) ?
533*d656534bSElliott Hughes 		MAX(x, MAX(16384 / bpb.bpbBytesPerSec, 4)) : x) + extra_res;
534*d656534bSElliott Hughes 	else if (bpb.bpbResSectors < x) {
535*d656534bSElliott Hughes 	    warnx("too few reserved sectors (need %d have %d)", x,
536*d656534bSElliott Hughes 		bpb.bpbResSectors);
537*d656534bSElliott Hughes 	    goto done;
538*d656534bSElliott Hughes 	}
539*d656534bSElliott Hughes 	if (fat != 32 && !bpb.bpbRootDirEnts)
540*d656534bSElliott Hughes 	    bpb.bpbRootDirEnts = DEFRDE;
541*d656534bSElliott Hughes 	rds = howmany(bpb.bpbRootDirEnts,
542*d656534bSElliott Hughes 	    bpb.bpbBytesPerSec / sizeof(struct de));
543*d656534bSElliott Hughes 	if (set_spc) {
544*d656534bSElliott Hughes 	    for (bpb.bpbSecPerClust = howmany(fat == 16 ? DEFBLK16 :
545*d656534bSElliott Hughes 		    DEFBLK, bpb.bpbBytesPerSec);
546*d656534bSElliott Hughes 		bpb.bpbSecPerClust < MAXSPC && (bpb.bpbResSectors +
547*d656534bSElliott Hughes 		    howmany((RESFTE + maxcls(fat)) * (fat / BPN),
548*d656534bSElliott Hughes 			bpb.bpbBytesPerSec * NPB) * bpb.bpbFATs +
549*d656534bSElliott Hughes 		    rds +
550*d656534bSElliott Hughes 		    (u_int64_t) (maxcls(fat) + 1) * bpb.bpbSecPerClust) <=
551*d656534bSElliott Hughes 		    bpb.bpbHugeSectors;
552*d656534bSElliott Hughes 		bpb.bpbSecPerClust <<= 1)
553*d656534bSElliott Hughes 		    continue;
554*d656534bSElliott Hughes 
555*d656534bSElliott Hughes 	}
556*d656534bSElliott Hughes 	if (fat != 32 && bpb.bpbBigFATsecs > MAXU16) {
557*d656534bSElliott Hughes 	    warnx("too many sectors/FAT for FAT12/16");
558*d656534bSElliott Hughes 	    goto done;
559*d656534bSElliott Hughes 	}
560*d656534bSElliott Hughes 	x1 = bpb.bpbResSectors + rds;
561*d656534bSElliott Hughes 	x = bpb.bpbBigFATsecs ? bpb.bpbBigFATsecs : 1;
562*d656534bSElliott Hughes 	if (x1 + (u_int64_t)x * bpb.bpbFATs > bpb.bpbHugeSectors) {
563*d656534bSElliott Hughes 	    warnx("meta data exceeds file system size");
564*d656534bSElliott Hughes 	    goto done;
565*d656534bSElliott Hughes 	}
566*d656534bSElliott Hughes 	x1 += x * bpb.bpbFATs;
567*d656534bSElliott Hughes 	x = (u_int64_t)(bpb.bpbHugeSectors - x1) * bpb.bpbBytesPerSec * NPB /
568*d656534bSElliott Hughes 	    (bpb.bpbSecPerClust * bpb.bpbBytesPerSec * NPB +
569*d656534bSElliott Hughes 	    fat / BPN * bpb.bpbFATs);
570*d656534bSElliott Hughes 	x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN),
571*d656534bSElliott Hughes 	    bpb.bpbBytesPerSec * NPB);
572*d656534bSElliott Hughes 	if (set_spf) {
573*d656534bSElliott Hughes 	    if (bpb.bpbBigFATsecs == 0)
574*d656534bSElliott Hughes 		bpb.bpbBigFATsecs = x2;
575*d656534bSElliott Hughes 	    x1 += (bpb.bpbBigFATsecs - 1) * bpb.bpbFATs;
576*d656534bSElliott Hughes 	}
577*d656534bSElliott Hughes 	if (set_res) {
578*d656534bSElliott Hughes 	    /* attempt to align root directory */
579*d656534bSElliott Hughes 	    alignment = (bpb.bpbResSectors + bpb.bpbBigFATsecs * bpb.bpbFATs) %
580*d656534bSElliott Hughes 		bpb.bpbSecPerClust;
581*d656534bSElliott Hughes 	    if (o.align)
582*d656534bSElliott Hughes 		extra_res += bpb.bpbSecPerClust - alignment;
583*d656534bSElliott Hughes 	}
584*d656534bSElliott Hughes 	attempts++;
585*d656534bSElliott Hughes     } while (o.align && alignment != 0 && attempts < 2);
586*d656534bSElliott Hughes     if (o.align && alignment != 0)
587*d656534bSElliott Hughes 	warnx("warning: Alignment failed.");
588*d656534bSElliott Hughes 
589*d656534bSElliott Hughes     cls = (bpb.bpbHugeSectors - x1) / bpb.bpbSecPerClust;
590*d656534bSElliott Hughes     x = (u_int64_t)bpb.bpbBigFATsecs * bpb.bpbBytesPerSec * NPB / (fat / BPN) -
591*d656534bSElliott Hughes 	RESFTE;
592*d656534bSElliott Hughes     if (cls > x)
593*d656534bSElliott Hughes 	cls = x;
594*d656534bSElliott Hughes     if (bpb.bpbBigFATsecs < x2)
595*d656534bSElliott Hughes 	warnx("warning: sectors/FAT limits file system to %u clusters",
596*d656534bSElliott Hughes 	      cls);
597*d656534bSElliott Hughes     if (cls < mincls(fat)) {
598*d656534bSElliott Hughes 	warnx("%u clusters too few clusters for FAT%u, need %u", cls, fat,
599*d656534bSElliott Hughes 	    mincls(fat));
600*d656534bSElliott Hughes 	goto done;
601*d656534bSElliott Hughes     }
602*d656534bSElliott Hughes     if (cls > maxcls(fat)) {
603*d656534bSElliott Hughes 	cls = maxcls(fat);
604*d656534bSElliott Hughes 	bpb.bpbHugeSectors = x1 + (cls + 1) * bpb.bpbSecPerClust - 1;
605*d656534bSElliott Hughes 	warnx("warning: FAT type limits file system to %u sectors",
606*d656534bSElliott Hughes 	      bpb.bpbHugeSectors);
607*d656534bSElliott Hughes     }
608*d656534bSElliott Hughes     printf("%s: %u sector%s in %u FAT%u cluster%s "
609*d656534bSElliott Hughes 	   "(%u bytes/cluster)\n", fname, cls * bpb.bpbSecPerClust,
610*d656534bSElliott Hughes 	   cls * bpb.bpbSecPerClust == 1 ? "" : "s", cls, fat,
611*d656534bSElliott Hughes 	   cls == 1 ? "" : "s", bpb.bpbBytesPerSec * bpb.bpbSecPerClust);
612*d656534bSElliott Hughes     if (!bpb.bpbMedia)
613*d656534bSElliott Hughes 	bpb.bpbMedia = !bpb.bpbHiddenSecs ? 0xf0 : 0xf8;
614*d656534bSElliott Hughes     if (fat == 32)
615*d656534bSElliott Hughes 	bpb.bpbRootClust = RESFTE;
616*d656534bSElliott Hughes     if (bpb.bpbHugeSectors <= MAXU16) {
617*d656534bSElliott Hughes 	bpb.bpbSectors = bpb.bpbHugeSectors;
618*d656534bSElliott Hughes 	bpb.bpbHugeSectors = 0;
619*d656534bSElliott Hughes     }
620*d656534bSElliott Hughes     if (fat != 32) {
621*d656534bSElliott Hughes 	bpb.bpbFATsecs = bpb.bpbBigFATsecs;
622*d656534bSElliott Hughes 	bpb.bpbBigFATsecs = 0;
623*d656534bSElliott Hughes     }
624*d656534bSElliott Hughes     print_bpb(&bpb);
625*d656534bSElliott Hughes     if (!o.no_create) {
626*d656534bSElliott Hughes 	if (o.timestamp_set) {
627*d656534bSElliott Hughes 	    tv.tv_sec = now = o.timestamp;
628*d656534bSElliott Hughes 	    tv.tv_usec = 0;
629*d656534bSElliott Hughes 	    tm = gmtime(&now);
630*d656534bSElliott Hughes 	} else {
631*d656534bSElliott Hughes 	    gettimeofday(&tv, NULL);
632*d656534bSElliott Hughes 	    now = tv.tv_sec;
633*d656534bSElliott Hughes 	    tm = localtime(&now);
634*d656534bSElliott Hughes 	}
635*d656534bSElliott Hughes 
636*d656534bSElliott Hughes 	chunksize = getchunksize();
637*d656534bSElliott Hughes 	physbuf = malloc(chunksize);
638*d656534bSElliott Hughes 	if (physbuf == NULL) {
639*d656534bSElliott Hughes 	    warn(NULL);
640*d656534bSElliott Hughes 	    goto done;
641*d656534bSElliott Hughes 	}
642*d656534bSElliott Hughes 	physbuf_end = physbuf + chunksize;
643*d656534bSElliott Hughes 	img = physbuf;
644*d656534bSElliott Hughes 
645*d656534bSElliott Hughes 	dir = bpb.bpbResSectors + (bpb.bpbFATsecs ? bpb.bpbFATsecs :
646*d656534bSElliott Hughes 				   bpb.bpbBigFATsecs) * bpb.bpbFATs;
647*d656534bSElliott Hughes 	memset(&si_sa, 0, sizeof(si_sa));
648*d656534bSElliott Hughes 	si_sa.sa_handler = infohandler;
649*d656534bSElliott Hughes #ifdef SIGINFO
650*d656534bSElliott Hughes 	if (sigaction(SIGINFO, &si_sa, NULL) == -1) {
651*d656534bSElliott Hughes 	    warn("sigaction SIGINFO");
652*d656534bSElliott Hughes 	    goto done;
653*d656534bSElliott Hughes 	}
654*d656534bSElliott Hughes #endif
655*d656534bSElliott Hughes 
656*d656534bSElliott Hughes 	for (lsn = 0; lsn < dir + (fat == 32 ? bpb.bpbSecPerClust : rds); lsn++) {
657*d656534bSElliott Hughes 	    if (got_siginfo) {
658*d656534bSElliott Hughes 		    fprintf(stderr,"%s: writing sector %u of %u (%u%%)\n",
659*d656534bSElliott Hughes 			fname, lsn,
660*d656534bSElliott Hughes 			(dir + (fat == 32 ? bpb.bpbSecPerClust: rds)),
661*d656534bSElliott Hughes 			(lsn * 100) / (dir +
662*d656534bSElliott Hughes 			    (fat == 32 ? bpb.bpbSecPerClust: rds)));
663*d656534bSElliott Hughes 		    got_siginfo = 0;
664*d656534bSElliott Hughes 	    }
665*d656534bSElliott Hughes 	    x = lsn;
666*d656534bSElliott Hughes 	    if (o.bootstrap &&
667*d656534bSElliott Hughes 		fat == 32 && bpb.bpbBackup != MAXU16 &&
668*d656534bSElliott Hughes 		bss <= bpb.bpbBackup && x >= bpb.bpbBackup) {
669*d656534bSElliott Hughes 		x -= bpb.bpbBackup;
670*d656534bSElliott Hughes 		if (!x && lseek(fd1, o.offset, SEEK_SET)) {
671*d656534bSElliott Hughes 		    warn("%s", bname);
672*d656534bSElliott Hughes 		    goto done;
673*d656534bSElliott Hughes 		}
674*d656534bSElliott Hughes 	    }
675*d656534bSElliott Hughes 	    if (o.bootstrap && x < bss) {
676*d656534bSElliott Hughes 		if ((n = read(fd1, img, bpb.bpbBytesPerSec)) == -1) {
677*d656534bSElliott Hughes 		    warn("%s", bname);
678*d656534bSElliott Hughes 		    goto done;
679*d656534bSElliott Hughes 		}
680*d656534bSElliott Hughes 		if ((unsigned)n != bpb.bpbBytesPerSec) {
681*d656534bSElliott Hughes 		    warnx("%s: can't read sector %u", bname, x);
682*d656534bSElliott Hughes 		    goto done;
683*d656534bSElliott Hughes 		}
684*d656534bSElliott Hughes 	    } else
685*d656534bSElliott Hughes 		memset(img, 0, bpb.bpbBytesPerSec);
686*d656534bSElliott Hughes 	    if (!lsn ||
687*d656534bSElliott Hughes 		(fat == 32 && bpb.bpbBackup != MAXU16 &&
688*d656534bSElliott Hughes 		 lsn == bpb.bpbBackup)) {
689*d656534bSElliott Hughes 		x1 = sizeof(struct bs);
690*d656534bSElliott Hughes 		bsbpb = (struct bsbpb *)(img + x1);
691*d656534bSElliott Hughes 		mk2(bsbpb->bpbBytesPerSec, bpb.bpbBytesPerSec);
692*d656534bSElliott Hughes 		mk1(bsbpb->bpbSecPerClust, bpb.bpbSecPerClust);
693*d656534bSElliott Hughes 		mk2(bsbpb->bpbResSectors, bpb.bpbResSectors);
694*d656534bSElliott Hughes 		mk1(bsbpb->bpbFATs, bpb.bpbFATs);
695*d656534bSElliott Hughes 		mk2(bsbpb->bpbRootDirEnts, bpb.bpbRootDirEnts);
696*d656534bSElliott Hughes 		mk2(bsbpb->bpbSectors, bpb.bpbSectors);
697*d656534bSElliott Hughes 		mk1(bsbpb->bpbMedia, bpb.bpbMedia);
698*d656534bSElliott Hughes 		mk2(bsbpb->bpbFATsecs, bpb.bpbFATsecs);
699*d656534bSElliott Hughes 		mk2(bsbpb->bpbSecPerTrack, bpb.bpbSecPerTrack);
700*d656534bSElliott Hughes 		mk2(bsbpb->bpbHeads, bpb.bpbHeads);
701*d656534bSElliott Hughes 		mk4(bsbpb->bpbHiddenSecs, bpb.bpbHiddenSecs);
702*d656534bSElliott Hughes 		mk4(bsbpb->bpbHugeSectors, bpb.bpbHugeSectors);
703*d656534bSElliott Hughes 		x1 += sizeof(struct bsbpb);
704*d656534bSElliott Hughes 		if (fat == 32) {
705*d656534bSElliott Hughes 		    bsxbpb = (struct bsxbpb *)(img + x1);
706*d656534bSElliott Hughes 		    mk4(bsxbpb->bpbBigFATsecs, bpb.bpbBigFATsecs);
707*d656534bSElliott Hughes 		    mk2(bsxbpb->bpbExtFlags, 0);
708*d656534bSElliott Hughes 		    mk2(bsxbpb->bpbFSVers, 0);
709*d656534bSElliott Hughes 		    mk4(bsxbpb->bpbRootClust, bpb.bpbRootClust);
710*d656534bSElliott Hughes 		    mk2(bsxbpb->bpbFSInfo, bpb.bpbFSInfo);
711*d656534bSElliott Hughes 		    mk2(bsxbpb->bpbBackup, bpb.bpbBackup);
712*d656534bSElliott Hughes 		    x1 += sizeof(struct bsxbpb);
713*d656534bSElliott Hughes 		}
714*d656534bSElliott Hughes 		bsx = (struct bsx *)(img + x1);
715*d656534bSElliott Hughes 		mk1(bsx->exBootSignature, 0x29);
716*d656534bSElliott Hughes 		if (o.volume_id_set)
717*d656534bSElliott Hughes 		    x = o.volume_id;
718*d656534bSElliott Hughes 		else
719*d656534bSElliott Hughes 		    x = (((u_int)(1 + tm->tm_mon) << 8 |
720*d656534bSElliott Hughes 			  (u_int)tm->tm_mday) +
721*d656534bSElliott Hughes 			 ((u_int)tm->tm_sec << 8 |
722*d656534bSElliott Hughes 			  (u_int)(tv.tv_usec / 10))) << 16 |
723*d656534bSElliott Hughes 			((u_int)(1900 + tm->tm_year) +
724*d656534bSElliott Hughes 			 ((u_int)tm->tm_hour << 8 |
725*d656534bSElliott Hughes 			  (u_int)tm->tm_min));
726*d656534bSElliott Hughes 		mk4(bsx->exVolumeID, x);
727*d656534bSElliott Hughes 		mklabel(bsx->exVolumeLabel, o.volume_label ? o.volume_label : "NO NAME");
728*d656534bSElliott Hughes 		snprintf(buf, sizeof(buf), "FAT%u", fat);
729*d656534bSElliott Hughes 		setstr(bsx->exFileSysType, buf, sizeof(bsx->exFileSysType));
730*d656534bSElliott Hughes 		if (!o.bootstrap) {
731*d656534bSElliott Hughes 		    x1 += sizeof(struct bsx);
732*d656534bSElliott Hughes 		    bs = (struct bs *)img;
733*d656534bSElliott Hughes 		    mk1(bs->bsJump[0], 0xeb);
734*d656534bSElliott Hughes 		    mk1(bs->bsJump[1], x1 - 2);
735*d656534bSElliott Hughes 		    mk1(bs->bsJump[2], 0x90);
736*d656534bSElliott Hughes 		    setstr(bs->bsOemName, o.OEM_string ? o.OEM_string : "BSD4.4  ",
737*d656534bSElliott Hughes 			   sizeof(bs->bsOemName));
738*d656534bSElliott Hughes 		    memcpy(img + x1, bootcode, sizeof(bootcode));
739*d656534bSElliott Hughes 		    mk2(img + MINBPS - 2, DOSMAGIC);
740*d656534bSElliott Hughes 		}
741*d656534bSElliott Hughes 	    } else if (fat == 32 && bpb.bpbFSInfo != MAXU16 &&
742*d656534bSElliott Hughes 		       (lsn == bpb.bpbFSInfo ||
743*d656534bSElliott Hughes 			(bpb.bpbBackup != MAXU16 &&
744*d656534bSElliott Hughes 			 lsn == bpb.bpbBackup + bpb.bpbFSInfo))) {
745*d656534bSElliott Hughes 		mk4(img, 0x41615252);
746*d656534bSElliott Hughes 		mk4(img + MINBPS - 28, 0x61417272);
747*d656534bSElliott Hughes 		mk4(img + MINBPS - 24, 0xffffffff);
748*d656534bSElliott Hughes 		mk4(img + MINBPS - 20, 0xffffffff);
749*d656534bSElliott Hughes 		mk2(img + MINBPS - 2, DOSMAGIC);
750*d656534bSElliott Hughes 	    } else if (lsn >= bpb.bpbResSectors && lsn < dir &&
751*d656534bSElliott Hughes 		       !((lsn - bpb.bpbResSectors) %
752*d656534bSElliott Hughes 			 (bpb.bpbFATsecs ? bpb.bpbFATsecs :
753*d656534bSElliott Hughes 			  bpb.bpbBigFATsecs))) {
754*d656534bSElliott Hughes 		mk1(img[0], bpb.bpbMedia);
755*d656534bSElliott Hughes 		for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
756*d656534bSElliott Hughes 		    mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
757*d656534bSElliott Hughes 	    } else if (lsn == dir && o.volume_label) {
758*d656534bSElliott Hughes 		de = (struct de *)img;
759*d656534bSElliott Hughes 		mklabel(de->deName, o.volume_label);
760*d656534bSElliott Hughes 		mk1(de->deAttributes, 050);
761*d656534bSElliott Hughes 		x = (u_int)tm->tm_hour << 11 |
762*d656534bSElliott Hughes 		    (u_int)tm->tm_min << 5 |
763*d656534bSElliott Hughes 		    (u_int)tm->tm_sec >> 1;
764*d656534bSElliott Hughes 		mk2(de->deMTime, x);
765*d656534bSElliott Hughes 		x = (u_int)(tm->tm_year - 80) << 9 |
766*d656534bSElliott Hughes 		    (u_int)(tm->tm_mon + 1) << 5 |
767*d656534bSElliott Hughes 		    (u_int)tm->tm_mday;
768*d656534bSElliott Hughes 		mk2(de->deMDate, x);
769*d656534bSElliott Hughes 	    }
770*d656534bSElliott Hughes 	    /*
771*d656534bSElliott Hughes 	     * Issue a write of chunksize once we have collected
772*d656534bSElliott Hughes 	     * enough sectors.
773*d656534bSElliott Hughes 	     */
774*d656534bSElliott Hughes 	    img += bpb.bpbBytesPerSec;
775*d656534bSElliott Hughes 	    if (img >= physbuf_end) {
776*d656534bSElliott Hughes 		n = write(fd, physbuf, chunksize);
777*d656534bSElliott Hughes 		if (n != chunksize) {
778*d656534bSElliott Hughes 		    warnx("%s: can't write sector %u", fname, lsn);
779*d656534bSElliott Hughes 		    goto done;
780*d656534bSElliott Hughes 		}
781*d656534bSElliott Hughes 		img = physbuf;
782*d656534bSElliott Hughes 	    }
783*d656534bSElliott Hughes 	}
784*d656534bSElliott Hughes 	/*
785*d656534bSElliott Hughes 	 * Write remaining sectors, if the last write didn't end
786*d656534bSElliott Hughes 	 * up filling a whole chunk.
787*d656534bSElliott Hughes 	 */
788*d656534bSElliott Hughes 	if (img != physbuf) {
789*d656534bSElliott Hughes 		ssize_t tailsize = img - physbuf;
790*d656534bSElliott Hughes 
791*d656534bSElliott Hughes 		n = write(fd, physbuf, tailsize);
792*d656534bSElliott Hughes 		if (n != tailsize) {
793*d656534bSElliott Hughes 		    warnx("%s: can't write sector %u", fname, lsn);
794*d656534bSElliott Hughes 		    goto done;
795*d656534bSElliott Hughes 		}
796*d656534bSElliott Hughes 	}
797*d656534bSElliott Hughes     }
798*d656534bSElliott Hughes     rv = 0;
799*d656534bSElliott Hughes done:
800*d656534bSElliott Hughes     free(physbuf);
801*d656534bSElliott Hughes     if (fd != -1)
802*d656534bSElliott Hughes 	    close(fd);
803*d656534bSElliott Hughes     if (fd1 != -1)
804*d656534bSElliott Hughes 	    close(fd1);
805*d656534bSElliott Hughes 
806*d656534bSElliott Hughes     return rv;
807*d656534bSElliott Hughes }
808*d656534bSElliott Hughes 
809*d656534bSElliott Hughes /*
810*d656534bSElliott Hughes  * return -1 with error if file system is mounted.
811*d656534bSElliott Hughes  */
812*d656534bSElliott Hughes static int
check_mounted(const char * fname,mode_t mode)813*d656534bSElliott Hughes check_mounted(const char *fname, mode_t mode)
814*d656534bSElliott Hughes {
815*d656534bSElliott Hughes /*
816*d656534bSElliott Hughes  * If getmntinfo() is not available (e.g. Linux) don't check. This should
817*d656534bSElliott Hughes  * not be a problem since we will only be using makefs to create images.
818*d656534bSElliott Hughes  */
819*d656534bSElliott Hughes #if 0 && !defined(MAKEFS)
820*d656534bSElliott Hughes     struct statfs *mp;
821*d656534bSElliott Hughes     const char *s1, *s2;
822*d656534bSElliott Hughes     size_t len;
823*d656534bSElliott Hughes     int n, r;
824*d656534bSElliott Hughes 
825*d656534bSElliott Hughes     if (!(n = getmntinfo(&mp, MNT_NOWAIT))) {
826*d656534bSElliott Hughes 	warn("getmntinfo");
827*d656534bSElliott Hughes 	return -1;
828*d656534bSElliott Hughes     }
829*d656534bSElliott Hughes     len = strlen(_PATH_DEV);
830*d656534bSElliott Hughes     s1 = fname;
831*d656534bSElliott Hughes     if (!strncmp(s1, _PATH_DEV, len))
832*d656534bSElliott Hughes 	s1 += len;
833*d656534bSElliott Hughes     r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
834*d656534bSElliott Hughes     for (; n--; mp++) {
835*d656534bSElliott Hughes 	s2 = mp->f_mntfromname;
836*d656534bSElliott Hughes 	if (!strncmp(s2, _PATH_DEV, len))
837*d656534bSElliott Hughes 	    s2 += len;
838*d656534bSElliott Hughes 	if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
839*d656534bSElliott Hughes 	    !strcmp(s1, s2)) {
840*d656534bSElliott Hughes 	    warnx("%s is mounted on %s", fname, mp->f_mntonname);
841*d656534bSElliott Hughes 	    return -1;
842*d656534bSElliott Hughes 	}
843*d656534bSElliott Hughes     }
844*d656534bSElliott Hughes #endif
845*d656534bSElliott Hughes     return 0;
846*d656534bSElliott Hughes }
847*d656534bSElliott Hughes 
848*d656534bSElliott Hughes /*
849*d656534bSElliott Hughes  * Get optimal I/O size
850*d656534bSElliott Hughes  */
851*d656534bSElliott Hughes static ssize_t
getchunksize(void)852*d656534bSElliott Hughes getchunksize(void)
853*d656534bSElliott Hughes {
854*d656534bSElliott Hughes 	static int chunksize;
855*d656534bSElliott Hughes 
856*d656534bSElliott Hughes 	if (chunksize != 0)
857*d656534bSElliott Hughes 		return ((ssize_t)chunksize);
858*d656534bSElliott Hughes 
859*d656534bSElliott Hughes #ifdef	KERN_MAXPHYS
860*d656534bSElliott Hughes 	int mib[2];
861*d656534bSElliott Hughes 	size_t len;
862*d656534bSElliott Hughes 
863*d656534bSElliott Hughes 	mib[0] = CTL_KERN;
864*d656534bSElliott Hughes 	mib[1] = KERN_MAXPHYS;
865*d656534bSElliott Hughes 	len = sizeof(chunksize);
866*d656534bSElliott Hughes 
867*d656534bSElliott Hughes 	if (sysctl(mib, 2, &chunksize, &len, NULL, 0) == -1) {
868*d656534bSElliott Hughes 		warn("sysctl: KERN_MAXPHYS, using %zu", (size_t)MAXPHYS);
869*d656534bSElliott Hughes 		chunksize = 0;
870*d656534bSElliott Hughes 	}
871*d656534bSElliott Hughes #endif
872*d656534bSElliott Hughes 	if (chunksize == 0)
873*d656534bSElliott Hughes 		chunksize = MAXPHYS;
874*d656534bSElliott Hughes 
875*d656534bSElliott Hughes 	/*
876*d656534bSElliott Hughes 	 * For better performance, we want to write larger chunks instead of
877*d656534bSElliott Hughes 	 * individual sectors (the size can only be 512, 1024, 2048 or 4096
878*d656534bSElliott Hughes 	 * bytes). Assert that chunksize can always hold an integer number of
879*d656534bSElliott Hughes 	 * sectors by asserting that both are power of two numbers and the
880*d656534bSElliott Hughes 	 * chunksize is greater than MAXBPS.
881*d656534bSElliott Hughes 	 */
882*d656534bSElliott Hughes 	static_assert(powerof2(MAXBPS), "MAXBPS is not power of 2");
883*d656534bSElliott Hughes 	assert(powerof2(chunksize));
884*d656534bSElliott Hughes 	assert(chunksize > MAXBPS);
885*d656534bSElliott Hughes 
886*d656534bSElliott Hughes 	return ((ssize_t)chunksize);
887*d656534bSElliott Hughes }
888*d656534bSElliott Hughes 
889*d656534bSElliott Hughes /*
890*d656534bSElliott Hughes  * Get a standard format.
891*d656534bSElliott Hughes  */
892*d656534bSElliott Hughes static int
getstdfmt(const char * fmt,struct bpb * bpb)893*d656534bSElliott Hughes getstdfmt(const char *fmt, struct bpb *bpb)
894*d656534bSElliott Hughes {
895*d656534bSElliott Hughes     u_int x, i;
896*d656534bSElliott Hughes 
897*d656534bSElliott Hughes     x = nitems(stdfmt);
898*d656534bSElliott Hughes     for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
899*d656534bSElliott Hughes     if (i == x) {
900*d656534bSElliott Hughes 	warnx("%s: unknown standard format", fmt);
901*d656534bSElliott Hughes 	return -1;
902*d656534bSElliott Hughes     }
903*d656534bSElliott Hughes     *bpb = stdfmt[i].bpb;
904*d656534bSElliott Hughes     return 0;
905*d656534bSElliott Hughes }
906*d656534bSElliott Hughes 
907*d656534bSElliott Hughes #if 0
908*d656534bSElliott Hughes static void
909*d656534bSElliott Hughes compute_geometry_from_file(int fd, const char *fname, struct disklabel *lp)
910*d656534bSElliott Hughes {
911*d656534bSElliott Hughes 	struct stat st;
912*d656534bSElliott Hughes 	off_t ms;
913*d656534bSElliott Hughes 
914*d656534bSElliott Hughes 	if (fstat(fd, &st))
915*d656534bSElliott Hughes 		err(1, "cannot get disk size");
916*d656534bSElliott Hughes 	if (!S_ISREG(st.st_mode))
917*d656534bSElliott Hughes 		errx(1, "%s is not a regular file", fname);
918*d656534bSElliott Hughes 	ms = st.st_size;
919*d656534bSElliott Hughes 	lp->d_secsize = 512;
920*d656534bSElliott Hughes 	lp->d_nsectors = 63;
921*d656534bSElliott Hughes 	lp->d_ntracks = 255;
922*d656534bSElliott Hughes 	lp->d_secperunit = ms / lp->d_secsize;
923*d656534bSElliott Hughes }
924*d656534bSElliott Hughes #endif
925*d656534bSElliott Hughes 
926*d656534bSElliott Hughes /*
927*d656534bSElliott Hughes  * Get disk slice, partition, and geometry information.
928*d656534bSElliott Hughes  */
929*d656534bSElliott Hughes #if defined(__linux__)
getdiskinfo(int fd,const char * fname,const char * dtype,__unused int oflag,struct bpb * bpb)930*d656534bSElliott Hughes static int getdiskinfo(int fd, const char *fname, const char *dtype,
931*d656534bSElliott Hughes                        __unused int oflag, struct bpb *bpb)
932*d656534bSElliott Hughes {
933*d656534bSElliott Hughes     if (ioctl(fd, BLKSSZGET, &bpb->bpbBytesPerSec)) {
934*d656534bSElliott Hughes         err(1, "ioctl(BLKSSZGET) for bytes/sector failed");
935*d656534bSElliott Hughes     }
936*d656534bSElliott Hughes 
937*d656534bSElliott Hughes     if (ckgeom(fname, bpb->bpbBytesPerSec, "bytes/sector") == -1) return -1;
938*d656534bSElliott Hughes 
939*d656534bSElliott Hughes     u_int64_t device_size;
940*d656534bSElliott Hughes     if (ioctl(fd, BLKGETSIZE64, &device_size)) {
941*d656534bSElliott Hughes         err(1, "ioctl(BLKGETSIZE64) failed");
942*d656534bSElliott Hughes     }
943*d656534bSElliott Hughes 
944*d656534bSElliott Hughes     u_int64_t sectors = device_size/bpb->bpbBytesPerSec;
945*d656534bSElliott Hughes     if (sectors > UINT_MAX) {
946*d656534bSElliott Hughes         errx(1, "too many sectors: %"PRIu64" (%"PRIu64" byte device, %u bytes/sector)",
947*d656534bSElliott Hughes              sectors, device_size, bpb->bpbBytesPerSec);
948*d656534bSElliott Hughes     }
949*d656534bSElliott Hughes     bpb->bpbHugeSectors = sectors;
950*d656534bSElliott Hughes 
951*d656534bSElliott Hughes     bpb->bpbSecPerTrack = 63;
952*d656534bSElliott Hughes     if (ckgeom(fname, bpb->bpbSecPerTrack, "sectors/track") == -1) return -1;
953*d656534bSElliott Hughes 
954*d656534bSElliott Hughes     bpb->bpbHeads = 64;
955*d656534bSElliott Hughes     if (ckgeom(fname, bpb->bpbHeads, "drive heads") == -1) return -1;
956*d656534bSElliott Hughes 
957*d656534bSElliott Hughes     return 0;
958*d656534bSElliott Hughes }
959*d656534bSElliott Hughes #elif defined(__APPLE__)
960*d656534bSElliott Hughes static int
getdiskinfo(int fd,const char * fname,const char * dtype,__unused int oflag,struct bpb * bpb)961*d656534bSElliott Hughes getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
962*d656534bSElliott Hughes 	    struct bpb *bpb) {
963*d656534bSElliott Hughes     return 0;
964*d656534bSElliott Hughes }
965*d656534bSElliott Hughes #else
966*d656534bSElliott Hughes static int
getdiskinfo(int fd,const char * fname,const char * dtype,__unused int oflag,struct bpb * bpb)967*d656534bSElliott Hughes getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
968*d656534bSElliott Hughes 	    struct bpb *bpb)
969*d656534bSElliott Hughes {
970*d656534bSElliott Hughes     struct disklabel *lp, dlp;
971*d656534bSElliott Hughes     off_t hs = 0;
972*d656534bSElliott Hughes #ifndef MAKEFS
973*d656534bSElliott Hughes     off_t ms;
974*d656534bSElliott Hughes     struct fd_type type;
975*d656534bSElliott Hughes 
976*d656534bSElliott Hughes     lp = NULL;
977*d656534bSElliott Hughes 
978*d656534bSElliott Hughes     /* If the user specified a disk type, try to use that */
979*d656534bSElliott Hughes     if (dtype != NULL) {
980*d656534bSElliott Hughes 	lp = getdiskbyname(dtype);
981*d656534bSElliott Hughes     }
982*d656534bSElliott Hughes 
983*d656534bSElliott Hughes     /* Maybe it's a floppy drive */
984*d656534bSElliott Hughes     if (lp == NULL) {
985*d656534bSElliott Hughes 	if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1) {
986*d656534bSElliott Hughes 	    /* create a fake geometry for a file image */
987*d656534bSElliott Hughes 	    compute_geometry_from_file(fd, fname, &dlp);
988*d656534bSElliott Hughes 	    lp = &dlp;
989*d656534bSElliott Hughes 	} else if (ioctl(fd, FD_GTYPE, &type) != -1) {
990*d656534bSElliott Hughes 	    dlp.d_secsize = 128 << type.secsize;
991*d656534bSElliott Hughes 	    dlp.d_nsectors = type.sectrac;
992*d656534bSElliott Hughes 	    dlp.d_ntracks = type.heads;
993*d656534bSElliott Hughes 	    dlp.d_secperunit = ms / dlp.d_secsize;
994*d656534bSElliott Hughes 	    lp = &dlp;
995*d656534bSElliott Hughes 	}
996*d656534bSElliott Hughes     }
997*d656534bSElliott Hughes 
998*d656534bSElliott Hughes     /* Maybe it's a fixed drive */
999*d656534bSElliott Hughes     if (lp == NULL) {
1000*d656534bSElliott Hughes 	if (bpb->bpbBytesPerSec)
1001*d656534bSElliott Hughes 	    dlp.d_secsize = bpb->bpbBytesPerSec;
1002*d656534bSElliott Hughes 	if (bpb->bpbBytesPerSec == 0 && ioctl(fd, DIOCGSECTORSIZE,
1003*d656534bSElliott Hughes 					      &dlp.d_secsize) == -1)
1004*d656534bSElliott Hughes 	    err(1, "cannot get sector size");
1005*d656534bSElliott Hughes 
1006*d656534bSElliott Hughes 	dlp.d_secperunit = ms / dlp.d_secsize;
1007*d656534bSElliott Hughes 
1008*d656534bSElliott Hughes 	if (bpb->bpbSecPerTrack == 0 && ioctl(fd, DIOCGFWSECTORS,
1009*d656534bSElliott Hughes 					      &dlp.d_nsectors) == -1) {
1010*d656534bSElliott Hughes 	    warn("cannot get number of sectors per track");
1011*d656534bSElliott Hughes 	    dlp.d_nsectors = 63;
1012*d656534bSElliott Hughes 	}
1013*d656534bSElliott Hughes 	if (bpb->bpbHeads == 0 &&
1014*d656534bSElliott Hughes 	    ioctl(fd, DIOCGFWHEADS, &dlp.d_ntracks) == -1) {
1015*d656534bSElliott Hughes 	    warn("cannot get number of heads");
1016*d656534bSElliott Hughes 	    if (dlp.d_secperunit <= 63*1*1024)
1017*d656534bSElliott Hughes 		dlp.d_ntracks = 1;
1018*d656534bSElliott Hughes 	    else if (dlp.d_secperunit <= 63*16*1024)
1019*d656534bSElliott Hughes 		dlp.d_ntracks = 16;
1020*d656534bSElliott Hughes 	    else
1021*d656534bSElliott Hughes 		dlp.d_ntracks = 255;
1022*d656534bSElliott Hughes 	}
1023*d656534bSElliott Hughes 
1024*d656534bSElliott Hughes 	hs = (ms / dlp.d_secsize) - dlp.d_secperunit;
1025*d656534bSElliott Hughes 	lp = &dlp;
1026*d656534bSElliott Hughes     }
1027*d656534bSElliott Hughes #else
1028*d656534bSElliott Hughes     /* In the makefs case we only support image files: */
1029*d656534bSElliott Hughes     compute_geometry_from_file(fd, fname, &dlp);
1030*d656534bSElliott Hughes     lp = &dlp;
1031*d656534bSElliott Hughes #endif
1032*d656534bSElliott Hughes 
1033*d656534bSElliott Hughes     if (bpb->bpbBytesPerSec == 0) {
1034*d656534bSElliott Hughes 	if (ckgeom(fname, lp->d_secsize, "bytes/sector") == -1)
1035*d656534bSElliott Hughes 	    return -1;
1036*d656534bSElliott Hughes 	bpb->bpbBytesPerSec = lp->d_secsize;
1037*d656534bSElliott Hughes     }
1038*d656534bSElliott Hughes     if (bpb->bpbSecPerTrack == 0) {
1039*d656534bSElliott Hughes 	if (ckgeom(fname, lp->d_nsectors, "sectors/track") == -1)
1040*d656534bSElliott Hughes 	    return -1;
1041*d656534bSElliott Hughes 	bpb->bpbSecPerTrack = lp->d_nsectors;
1042*d656534bSElliott Hughes     }
1043*d656534bSElliott Hughes     if (bpb->bpbHeads == 0) {
1044*d656534bSElliott Hughes 	if (ckgeom(fname, lp->d_ntracks, "drive heads") == -1)
1045*d656534bSElliott Hughes 	    return -1;
1046*d656534bSElliott Hughes 	bpb->bpbHeads = lp->d_ntracks;
1047*d656534bSElliott Hughes     }
1048*d656534bSElliott Hughes     if (bpb->bpbHugeSectors == 0)
1049*d656534bSElliott Hughes 	bpb->bpbHugeSectors = lp->d_secperunit;
1050*d656534bSElliott Hughes     if (bpb->bpbHiddenSecs == 0)
1051*d656534bSElliott Hughes 	bpb->bpbHiddenSecs = hs;
1052*d656534bSElliott Hughes     return 0;
1053*d656534bSElliott Hughes }
1054*d656534bSElliott Hughes #endif
1055*d656534bSElliott Hughes 
1056*d656534bSElliott Hughes /*
1057*d656534bSElliott Hughes  * Print out BPB values.
1058*d656534bSElliott Hughes  */
1059*d656534bSElliott Hughes static void
print_bpb(struct bpb * bpb)1060*d656534bSElliott Hughes print_bpb(struct bpb *bpb)
1061*d656534bSElliott Hughes {
1062*d656534bSElliott Hughes     printf("BytesPerSec=%u SecPerClust=%u ResSectors=%u FATs=%u",
1063*d656534bSElliott Hughes 	   bpb->bpbBytesPerSec, bpb->bpbSecPerClust, bpb->bpbResSectors,
1064*d656534bSElliott Hughes 	   bpb->bpbFATs);
1065*d656534bSElliott Hughes     if (bpb->bpbRootDirEnts)
1066*d656534bSElliott Hughes 	printf(" RootDirEnts=%u", bpb->bpbRootDirEnts);
1067*d656534bSElliott Hughes     if (bpb->bpbSectors)
1068*d656534bSElliott Hughes 	printf(" Sectors=%u", bpb->bpbSectors);
1069*d656534bSElliott Hughes     printf(" Media=%#x", bpb->bpbMedia);
1070*d656534bSElliott Hughes     if (bpb->bpbFATsecs)
1071*d656534bSElliott Hughes 	printf(" FATsecs=%u", bpb->bpbFATsecs);
1072*d656534bSElliott Hughes     printf(" SecPerTrack=%u Heads=%u HiddenSecs=%u", bpb->bpbSecPerTrack,
1073*d656534bSElliott Hughes 	   bpb->bpbHeads, bpb->bpbHiddenSecs);
1074*d656534bSElliott Hughes     if (bpb->bpbHugeSectors)
1075*d656534bSElliott Hughes 	printf(" HugeSectors=%u", bpb->bpbHugeSectors);
1076*d656534bSElliott Hughes     if (!bpb->bpbFATsecs) {
1077*d656534bSElliott Hughes 	printf(" FATsecs=%u RootCluster=%u", bpb->bpbBigFATsecs,
1078*d656534bSElliott Hughes 	       bpb->bpbRootClust);
1079*d656534bSElliott Hughes 	printf(" FSInfo=");
1080*d656534bSElliott Hughes 	printf(bpb->bpbFSInfo == MAXU16 ? "%#x" : "%u", bpb->bpbFSInfo);
1081*d656534bSElliott Hughes 	printf(" Backup=");
1082*d656534bSElliott Hughes 	printf(bpb->bpbBackup == MAXU16 ? "%#x" : "%u", bpb->bpbBackup);
1083*d656534bSElliott Hughes     }
1084*d656534bSElliott Hughes     printf("\n");
1085*d656534bSElliott Hughes }
1086*d656534bSElliott Hughes 
1087*d656534bSElliott Hughes /*
1088*d656534bSElliott Hughes  * Check a disk geometry value.
1089*d656534bSElliott Hughes  */
1090*d656534bSElliott Hughes static int
ckgeom(const char * fname,u_int val,const char * msg)1091*d656534bSElliott Hughes ckgeom(const char *fname, u_int val, const char *msg)
1092*d656534bSElliott Hughes {
1093*d656534bSElliott Hughes     if (!val) {
1094*d656534bSElliott Hughes 	warnx("%s: no default %s", fname, msg);
1095*d656534bSElliott Hughes 	return -1;
1096*d656534bSElliott Hughes     }
1097*d656534bSElliott Hughes     if (val > MAXU16) {
1098*d656534bSElliott Hughes 	warnx("%s: illegal %s %d", fname, msg, val);
1099*d656534bSElliott Hughes 	return -1;
1100*d656534bSElliott Hughes     }
1101*d656534bSElliott Hughes     return 0;
1102*d656534bSElliott Hughes }
1103*d656534bSElliott Hughes 
1104*d656534bSElliott Hughes /*
1105*d656534bSElliott Hughes  * Check a volume label.
1106*d656534bSElliott Hughes  */
1107*d656534bSElliott Hughes static int
oklabel(const char * src)1108*d656534bSElliott Hughes oklabel(const char *src)
1109*d656534bSElliott Hughes {
1110*d656534bSElliott Hughes     int c, i;
1111*d656534bSElliott Hughes 
1112*d656534bSElliott Hughes     for (i = 0; i <= 11; i++) {
1113*d656534bSElliott Hughes 	c = (u_char)*src++;
1114*d656534bSElliott Hughes 	if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
1115*d656534bSElliott Hughes 	    break;
1116*d656534bSElliott Hughes     }
1117*d656534bSElliott Hughes     return i && !c;
1118*d656534bSElliott Hughes }
1119*d656534bSElliott Hughes 
1120*d656534bSElliott Hughes /*
1121*d656534bSElliott Hughes  * Make a volume label.
1122*d656534bSElliott Hughes  */
1123*d656534bSElliott Hughes static void
mklabel(u_int8_t * dest,const char * src)1124*d656534bSElliott Hughes mklabel(u_int8_t *dest, const char *src)
1125*d656534bSElliott Hughes {
1126*d656534bSElliott Hughes     int c, i;
1127*d656534bSElliott Hughes 
1128*d656534bSElliott Hughes     for (i = 0; i < 11; i++) {
1129*d656534bSElliott Hughes 	c = *src ? toupper(*src++) : ' ';
1130*d656534bSElliott Hughes 	*dest++ = !i && c == '\xe5' ? 5 : c;
1131*d656534bSElliott Hughes     }
1132*d656534bSElliott Hughes }
1133*d656534bSElliott Hughes 
1134*d656534bSElliott Hughes /*
1135*d656534bSElliott Hughes  * Copy string, padding with spaces.
1136*d656534bSElliott Hughes  */
1137*d656534bSElliott Hughes static void
setstr(u_int8_t * dest,const char * src,size_t len)1138*d656534bSElliott Hughes setstr(u_int8_t *dest, const char *src, size_t len)
1139*d656534bSElliott Hughes {
1140*d656534bSElliott Hughes     while (len--)
1141*d656534bSElliott Hughes 	*dest++ = *src ? *src++ : ' ';
1142*d656534bSElliott Hughes }
1143*d656534bSElliott Hughes 
1144*d656534bSElliott Hughes static void
infohandler(int sig __unused)1145*d656534bSElliott Hughes infohandler(int sig __unused)
1146*d656534bSElliott Hughes {
1147*d656534bSElliott Hughes 
1148*d656534bSElliott Hughes 	got_siginfo = 1;
1149*d656534bSElliott Hughes }
1150