xref: /freebsd-src/sbin/newfs_msdos/mkfs_msdos.c (revision 17e85f59907aecc0c9e812ee094303f6e1fdd845)
1041b03daSEd Maste /*
2041b03daSEd Maste  * Copyright (c) 1998 Robert Nordier
3041b03daSEd Maste  * All rights reserved.
4041b03daSEd Maste  *
5041b03daSEd Maste  * Redistribution and use in source and binary forms, with or without
6041b03daSEd Maste  * modification, are permitted provided that the following conditions
7041b03daSEd Maste  * are met:
8041b03daSEd Maste  * 1. Redistributions of source code must retain the above copyright
9041b03daSEd Maste  *    notice, this list of conditions and the following disclaimer.
10041b03daSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
11041b03daSEd Maste  *    notice, this list of conditions and the following disclaimer in
12041b03daSEd Maste  *    the documentation and/or other materials provided with the
13041b03daSEd Maste  *    distribution.
14041b03daSEd Maste  *
15041b03daSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
16041b03daSEd Maste  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17041b03daSEd Maste  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18041b03daSEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
19041b03daSEd Maste  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20041b03daSEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21041b03daSEd Maste  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22041b03daSEd Maste  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23041b03daSEd Maste  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24041b03daSEd Maste  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25041b03daSEd Maste  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26041b03daSEd Maste  */
27041b03daSEd Maste 
28041b03daSEd Maste #include <sys/param.h>
29162ae9c8SAlex Richardson #ifdef MAKEFS
30162ae9c8SAlex Richardson /* In the makefs case we only want struct disklabel */
31162ae9c8SAlex Richardson #include <sys/disk/bsd.h>
32162ae9c8SAlex Richardson #else
33041b03daSEd Maste #include <sys/fdcio.h>
34041b03daSEd Maste #include <sys/disk.h>
35041b03daSEd Maste #include <sys/disklabel.h>
36041b03daSEd Maste #include <sys/mount.h>
37162ae9c8SAlex Richardson #endif
38041b03daSEd Maste #include <sys/stat.h>
391cbad9d7SXin LI #include <sys/sysctl.h>
40041b03daSEd Maste #include <sys/time.h>
41041b03daSEd Maste 
421cbad9d7SXin LI #include <assert.h>
43041b03daSEd Maste #include <ctype.h>
44041b03daSEd Maste #include <err.h>
45041b03daSEd Maste #include <errno.h>
46041b03daSEd Maste #include <fcntl.h>
47041b03daSEd Maste #include <inttypes.h>
48041b03daSEd Maste #include <paths.h>
49041b03daSEd Maste #include <signal.h>
50041b03daSEd Maste #include <stdio.h>
51041b03daSEd Maste #include <stdlib.h>
52041b03daSEd Maste #include <string.h>
53041b03daSEd Maste #include <time.h>
54041b03daSEd Maste #include <unistd.h>
55041b03daSEd Maste 
56041b03daSEd Maste #include "mkfs_msdos.h"
57041b03daSEd Maste 
58041b03daSEd Maste #define	MAXU16	  0xffff	/* maximum unsigned 16-bit quantity */
59041b03daSEd Maste #define	BPN	  4		/* bits per nibble */
60041b03daSEd Maste #define	NPB	  2		/* nibbles per byte */
61041b03daSEd Maste 
62041b03daSEd Maste #define	DOSMAGIC  0xaa55	/* DOS magic number */
63041b03daSEd Maste #define	MINBPS	  512		/* minimum bytes per sector */
6433c2d974SXin LI #define	MAXBPS    4096		/* maximum bytes per sector */
65041b03daSEd Maste #define	MAXSPC	  128		/* maximum sectors per cluster */
66041b03daSEd Maste #define	MAXNFT	  16		/* maximum number of FATs */
67041b03daSEd Maste #define	DEFBLK	  4096		/* default block size */
68041b03daSEd Maste #define	DEFBLK16  2048		/* default block size FAT16 */
69041b03daSEd Maste #define	DEFRDE	  512		/* default root directory entries */
70041b03daSEd Maste #define	RESFTE	  2		/* reserved FAT entries */
71041b03daSEd Maste #define	MINCLS12  1U		/* minimum FAT12 clusters */
72041b03daSEd Maste #define	MINCLS16  0xff5U	/* minimum FAT16 clusters */
73041b03daSEd Maste #define	MINCLS32  0xfff5U	/* minimum FAT32 clusters */
74041b03daSEd Maste #define	MAXCLS12  0xff4U	/* maximum FAT12 clusters */
75041b03daSEd Maste #define	MAXCLS16  0xfff4U	/* maximum FAT16 clusters */
76041b03daSEd Maste #define	MAXCLS32  0xffffff4U	/* maximum FAT32 clusters */
77041b03daSEd Maste 
78041b03daSEd Maste #define	mincls(fat)  ((fat) == 12 ? MINCLS12 :	\
79041b03daSEd Maste 		      (fat) == 16 ? MINCLS16 :	\
80041b03daSEd Maste 				    MINCLS32)
81041b03daSEd Maste 
82041b03daSEd Maste #define	maxcls(fat)  ((fat) == 12 ? MAXCLS12 :	\
83041b03daSEd Maste 		      (fat) == 16 ? MAXCLS16 :	\
84041b03daSEd Maste 				    MAXCLS32)
85041b03daSEd Maste 
86041b03daSEd Maste #define	mk1(p, x)				\
87041b03daSEd Maste     (p) = (u_int8_t)(x)
88041b03daSEd Maste 
89041b03daSEd Maste #define	mk2(p, x)				\
90041b03daSEd Maste     (p)[0] = (u_int8_t)(x),			\
91041b03daSEd Maste     (p)[1] = (u_int8_t)((x) >> 010)
92041b03daSEd Maste 
93041b03daSEd Maste #define	mk4(p, x)				\
94041b03daSEd Maste     (p)[0] = (u_int8_t)(x),			\
95041b03daSEd Maste     (p)[1] = (u_int8_t)((x) >> 010),		\
96041b03daSEd Maste     (p)[2] = (u_int8_t)((x) >> 020),		\
97041b03daSEd Maste     (p)[3] = (u_int8_t)((x) >> 030)
98041b03daSEd Maste 
99041b03daSEd Maste struct bs {
100041b03daSEd Maste     u_int8_t bsJump[3];			/* bootstrap entry point */
101041b03daSEd Maste     u_int8_t bsOemName[8];		/* OEM name and version */
102041b03daSEd Maste } __packed;
103041b03daSEd Maste 
104041b03daSEd Maste struct bsbpb {
105041b03daSEd Maste     u_int8_t bpbBytesPerSec[2];		/* bytes per sector */
106041b03daSEd Maste     u_int8_t bpbSecPerClust;		/* sectors per cluster */
107041b03daSEd Maste     u_int8_t bpbResSectors[2];		/* reserved sectors */
108041b03daSEd Maste     u_int8_t bpbFATs;			/* number of FATs */
109041b03daSEd Maste     u_int8_t bpbRootDirEnts[2];		/* root directory entries */
110041b03daSEd Maste     u_int8_t bpbSectors[2];		/* total sectors */
111041b03daSEd Maste     u_int8_t bpbMedia;			/* media descriptor */
112041b03daSEd Maste     u_int8_t bpbFATsecs[2];		/* sectors per FAT */
113041b03daSEd Maste     u_int8_t bpbSecPerTrack[2];		/* sectors per track */
114041b03daSEd Maste     u_int8_t bpbHeads[2];		/* drive heads */
115041b03daSEd Maste     u_int8_t bpbHiddenSecs[4];		/* hidden sectors */
116041b03daSEd Maste     u_int8_t bpbHugeSectors[4];		/* big total sectors */
117041b03daSEd Maste } __packed;
118041b03daSEd Maste 
119041b03daSEd Maste struct bsxbpb {
120041b03daSEd Maste     u_int8_t bpbBigFATsecs[4];		/* big sectors per FAT */
121041b03daSEd Maste     u_int8_t bpbExtFlags[2];		/* FAT control flags */
122041b03daSEd Maste     u_int8_t bpbFSVers[2];		/* file system version */
123041b03daSEd Maste     u_int8_t bpbRootClust[4];		/* root directory start cluster */
124041b03daSEd Maste     u_int8_t bpbFSInfo[2];		/* file system info sector */
125041b03daSEd Maste     u_int8_t bpbBackup[2];		/* backup boot sector */
126041b03daSEd Maste     u_int8_t bpbReserved[12];		/* reserved */
127041b03daSEd Maste } __packed;
128041b03daSEd Maste 
129041b03daSEd Maste struct bsx {
130041b03daSEd Maste     u_int8_t exDriveNumber;		/* drive number */
131041b03daSEd Maste     u_int8_t exReserved1;		/* reserved */
132041b03daSEd Maste     u_int8_t exBootSignature;		/* extended boot signature */
133041b03daSEd Maste     u_int8_t exVolumeID[4];		/* volume ID number */
134041b03daSEd Maste     u_int8_t exVolumeLabel[11];		/* volume label */
135041b03daSEd Maste     u_int8_t exFileSysType[8];		/* file system type */
136041b03daSEd Maste } __packed;
137041b03daSEd Maste 
138041b03daSEd Maste struct de {
139041b03daSEd Maste     u_int8_t deName[11];		/* name and extension */
140041b03daSEd Maste     u_int8_t deAttributes;		/* attributes */
141041b03daSEd Maste     u_int8_t rsvd[10];			/* reserved */
142041b03daSEd Maste     u_int8_t deMTime[2];		/* last-modified time */
143041b03daSEd Maste     u_int8_t deMDate[2];		/* last-modified date */
144041b03daSEd Maste     u_int8_t deStartCluster[2];		/* starting cluster */
145041b03daSEd Maste     u_int8_t deFileSize[4];		/* size */
146041b03daSEd Maste } __packed;
147041b03daSEd Maste 
148041b03daSEd Maste struct bpb {
149041b03daSEd Maste     u_int bpbBytesPerSec;		/* bytes per sector */
150041b03daSEd Maste     u_int bpbSecPerClust;		/* sectors per cluster */
151041b03daSEd Maste     u_int bpbResSectors;		/* reserved sectors */
152041b03daSEd Maste     u_int bpbFATs;			/* number of FATs */
153041b03daSEd Maste     u_int bpbRootDirEnts;		/* root directory entries */
154041b03daSEd Maste     u_int bpbSectors;			/* total sectors */
155041b03daSEd Maste     u_int bpbMedia;			/* media descriptor */
156041b03daSEd Maste     u_int bpbFATsecs;			/* sectors per FAT */
157041b03daSEd Maste     u_int bpbSecPerTrack;		/* sectors per track */
158041b03daSEd Maste     u_int bpbHeads;			/* drive heads */
159041b03daSEd Maste     u_int bpbHiddenSecs;		/* hidden sectors */
160041b03daSEd Maste     u_int bpbHugeSectors; 		/* big total sectors */
161041b03daSEd Maste     u_int bpbBigFATsecs; 		/* big sectors per FAT */
162041b03daSEd Maste     u_int bpbRootClust; 		/* root directory start cluster */
163041b03daSEd Maste     u_int bpbFSInfo; 			/* file system info sector */
164041b03daSEd Maste     u_int bpbBackup; 			/* backup boot sector */
165041b03daSEd Maste };
166041b03daSEd Maste 
167041b03daSEd Maste #define	BPBGAP 0, 0, 0, 0, 0, 0
168041b03daSEd Maste 
169041b03daSEd Maste static struct {
170041b03daSEd Maste     const char *name;
171041b03daSEd Maste     struct bpb bpb;
172041b03daSEd Maste } const stdfmt[] = {
173041b03daSEd Maste     {"160",  {512, 1, 1, 2,  64,  320, 0xfe, 1,  8, 1, BPBGAP}},
174041b03daSEd Maste     {"180",  {512, 1, 1, 2,  64,  360, 0xfc, 2,  9, 1, BPBGAP}},
175041b03daSEd Maste     {"320",  {512, 2, 1, 2, 112,  640, 0xff, 1,  8, 2, BPBGAP}},
176041b03daSEd Maste     {"360",  {512, 2, 1, 2, 112,  720, 0xfd, 2,  9, 2, BPBGAP}},
177041b03daSEd Maste     {"640",  {512, 2, 1, 2, 112, 1280, 0xfb, 2,  8, 2, BPBGAP}},
178041b03daSEd Maste     {"720",  {512, 2, 1, 2, 112, 1440, 0xf9, 3,  9, 2, BPBGAP}},
179041b03daSEd Maste     {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2, BPBGAP}},
180041b03daSEd Maste     {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2,  8, 2, BPBGAP}},
181041b03daSEd Maste     {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2, BPBGAP}},
182041b03daSEd Maste     {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2, BPBGAP}}
183041b03daSEd Maste };
184041b03daSEd Maste 
185041b03daSEd Maste static const u_int8_t bootcode[] = {
186041b03daSEd Maste     0xfa,			/* cli		    */
187041b03daSEd Maste     0x31, 0xc0, 		/* xor	   ax,ax    */
188041b03daSEd Maste     0x8e, 0xd0, 		/* mov	   ss,ax    */
189041b03daSEd Maste     0xbc, 0x00, 0x7c,		/* mov	   sp,7c00h */
190041b03daSEd Maste     0xfb,			/* sti		    */
191041b03daSEd Maste     0x8e, 0xd8, 		/* mov	   ds,ax    */
192041b03daSEd Maste     0xe8, 0x00, 0x00,		/* call    $ + 3    */
193041b03daSEd Maste     0x5e,			/* pop	   si	    */
194041b03daSEd Maste     0x83, 0xc6, 0x19,		/* add	   si,+19h  */
195041b03daSEd Maste     0xbb, 0x07, 0x00,		/* mov	   bx,0007h */
196041b03daSEd Maste     0xfc,			/* cld		    */
197041b03daSEd Maste     0xac,			/* lodsb	    */
198041b03daSEd Maste     0x84, 0xc0, 		/* test    al,al    */
199041b03daSEd Maste     0x74, 0x06, 		/* jz	   $ + 8    */
200041b03daSEd Maste     0xb4, 0x0e, 		/* mov	   ah,0eh   */
201041b03daSEd Maste     0xcd, 0x10, 		/* int	   10h	    */
202041b03daSEd Maste     0xeb, 0xf5, 		/* jmp	   $ - 9    */
203041b03daSEd Maste     0x30, 0xe4, 		/* xor	   ah,ah    */
204041b03daSEd Maste     0xcd, 0x16, 		/* int	   16h	    */
205041b03daSEd Maste     0xcd, 0x19, 		/* int	   19h	    */
206041b03daSEd Maste     0x0d, 0x0a,
207041b03daSEd Maste     'N', 'o', 'n', '-', 's', 'y', 's', 't',
208041b03daSEd Maste     'e', 'm', ' ', 'd', 'i', 's', 'k',
209041b03daSEd Maste     0x0d, 0x0a,
210041b03daSEd Maste     'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
211041b03daSEd Maste     'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
212041b03daSEd Maste     ' ', 'r', 'e', 'b', 'o', 'o', 't',
213041b03daSEd Maste     0x0d, 0x0a,
214041b03daSEd Maste     0
215041b03daSEd Maste };
216041b03daSEd Maste 
217041b03daSEd Maste static volatile sig_atomic_t got_siginfo;
218041b03daSEd Maste static void infohandler(int);
219041b03daSEd Maste 
220f775c417SMark Johnston #ifndef MAKEFS
22199f4158cSEd Maste static int check_mounted(const char *, mode_t);
222f775c417SMark Johnston #endif
2231cbad9d7SXin LI static ssize_t getchunksize(void);
22499f4158cSEd Maste static int getstdfmt(const char *, struct bpb *);
22599f4158cSEd Maste static int getdiskinfo(int, const char *, const char *, int, struct bpb *);
226041b03daSEd Maste static void print_bpb(struct bpb *);
22799f4158cSEd Maste static int ckgeom(const char *, u_int, const char *);
228041b03daSEd Maste static void mklabel(u_int8_t *, const char *);
229041b03daSEd Maste static int oklabel(const char *);
230041b03daSEd Maste static void setstr(u_int8_t *, const char *, size_t);
231041b03daSEd Maste 
232937744dfSEd Maste int
mkfs_msdos(const char * fname,const char * dtype,const struct msdos_options * op)233937744dfSEd Maste mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op)
234041b03daSEd Maste {
235041b03daSEd Maste     char buf[MAXPATHLEN];
236041b03daSEd Maste     struct sigaction si_sa;
237041b03daSEd Maste     struct stat sb;
238041b03daSEd Maste     struct timeval tv;
239041b03daSEd Maste     struct bpb bpb;
240041b03daSEd Maste     struct tm *tm;
241041b03daSEd Maste     struct bs *bs;
242041b03daSEd Maste     struct bsbpb *bsbpb;
243041b03daSEd Maste     struct bsxbpb *bsxbpb;
244041b03daSEd Maste     struct bsx *bsx;
245041b03daSEd Maste     struct de *de;
246041b03daSEd Maste     u_int8_t *img;
24733c2d974SXin LI     u_int8_t *physbuf, *physbuf_end;
248041b03daSEd Maste     const char *bname;
249041b03daSEd Maste     ssize_t n;
250041b03daSEd Maste     time_t now;
251041b03daSEd Maste     u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
252*077f757dSStefan Eßer     u_int extra_res, alignment, alignto, saved_x, attempts=0;
2530531ab72SXin LI     bool set_res, set_spf, set_spc;
254170b1dd5SEnji Cooper     int fd, fd1, rv;
255041b03daSEd Maste     struct msdos_options o = *op;
2561cbad9d7SXin LI     ssize_t chunksize;
257041b03daSEd Maste 
25833c2d974SXin LI     physbuf = NULL;
259170b1dd5SEnji Cooper     rv = -1;
260cd446151SWarner Losh     fd = fd1 = -1;
261170b1dd5SEnji Cooper 
262937744dfSEd Maste     if (o.block_size && o.sectors_per_cluster) {
263937744dfSEd Maste 	warnx("Cannot specify both block size and sectors per cluster");
264170b1dd5SEnji Cooper 	goto done;
265937744dfSEd Maste     }
266041b03daSEd Maste     if (o.OEM_string && strlen(o.OEM_string) > 8) {
26799f4158cSEd Maste 	warnx("%s: bad OEM string", o.OEM_string);
268170b1dd5SEnji Cooper 	goto done;
269041b03daSEd Maste     }
270041b03daSEd Maste     if (o.create_size) {
27199f4158cSEd Maste 	if (o.no_create) {
27299f4158cSEd Maste 	    warnx("create (-C) is incompatible with -N");
273170b1dd5SEnji Cooper 	    goto done;
27499f4158cSEd Maste 	}
275041b03daSEd Maste 	fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644);
27699f4158cSEd Maste 	if (fd == -1) {
27799f4158cSEd Maste 	    warnx("failed to create %s", fname);
278170b1dd5SEnji Cooper 	    goto done;
27999f4158cSEd Maste 	}
28099f4158cSEd Maste 	if (ftruncate(fd, o.create_size)) {
28199f4158cSEd Maste 	    warnx("failed to initialize %jd bytes", (intmax_t)o.create_size);
282170b1dd5SEnji Cooper 	    goto done;
28399f4158cSEd Maste 	}
28499f4158cSEd Maste     } else if ((fd = open(fname, o.no_create ? O_RDONLY : O_RDWR)) == -1) {
28599f4158cSEd Maste 	warn("%s", fname);
286170b1dd5SEnji Cooper 	goto done;
28799f4158cSEd Maste     }
28899f4158cSEd Maste     if (fstat(fd, &sb)) {
28999f4158cSEd Maste 	warn("%s", fname);
290170b1dd5SEnji Cooper 	goto done;
29199f4158cSEd Maste     }
292041b03daSEd Maste     if (o.create_size) {
293041b03daSEd Maste 	if (!S_ISREG(sb.st_mode))
294041b03daSEd Maste 	    warnx("warning, %s is not a regular file", fname);
295041b03daSEd Maste     } else {
296162ae9c8SAlex Richardson #ifdef MAKEFS
297162ae9c8SAlex Richardson 	errx(1, "o.create_size must be set!");
298162ae9c8SAlex Richardson #else
299041b03daSEd Maste 	if (!S_ISCHR(sb.st_mode))
300041b03daSEd Maste 	    warnx("warning, %s is not a character device", fname);
301d9aee13fSEd Maste #endif
302041b03daSEd Maste     }
303162ae9c8SAlex Richardson #ifndef MAKEFS
304041b03daSEd Maste     if (!o.no_create)
30599f4158cSEd Maste 	if (check_mounted(fname, sb.st_mode) == -1)
306170b1dd5SEnji Cooper 	    goto done;
307162ae9c8SAlex Richardson #endif
30899f4158cSEd Maste     if (o.offset && o.offset != lseek(fd, o.offset, SEEK_SET)) {
30999f4158cSEd Maste 	warnx("cannot seek to %jd", (intmax_t)o.offset);
310170b1dd5SEnji Cooper 	goto done;
31199f4158cSEd Maste     }
312041b03daSEd Maste     memset(&bpb, 0, sizeof(bpb));
313041b03daSEd Maste     if (o.floppy) {
31499f4158cSEd Maste 	if (getstdfmt(o.floppy, &bpb) == -1)
315170b1dd5SEnji Cooper 	    goto done;
316041b03daSEd Maste 	bpb.bpbHugeSectors = bpb.bpbSectors;
317041b03daSEd Maste 	bpb.bpbSectors = 0;
318041b03daSEd Maste 	bpb.bpbBigFATsecs = bpb.bpbFATsecs;
319041b03daSEd Maste 	bpb.bpbFATsecs = 0;
320041b03daSEd Maste     }
321041b03daSEd Maste     if (o.drive_heads)
322041b03daSEd Maste 	bpb.bpbHeads = o.drive_heads;
323041b03daSEd Maste     if (o.sectors_per_track)
324041b03daSEd Maste 	bpb.bpbSecPerTrack = o.sectors_per_track;
325041b03daSEd Maste     if (o.bytes_per_sector)
326041b03daSEd Maste 	bpb.bpbBytesPerSec = o.bytes_per_sector;
327041b03daSEd Maste     if (o.size)
328041b03daSEd Maste 	bpb.bpbHugeSectors = o.size;
329041b03daSEd Maste     if (o.hidden_sectors_set)
330041b03daSEd Maste 	bpb.bpbHiddenSecs = o.hidden_sectors;
331937744dfSEd Maste     if (!(o.floppy || (o.drive_heads && o.sectors_per_track &&
332937744dfSEd Maste 	o.bytes_per_sector && o.size && o.hidden_sectors_set))) {
3332780a26bSXin LI 	if (getdiskinfo(fd, fname, dtype, o.hidden_sectors_set, &bpb) == -1)
3342780a26bSXin LI 		goto done;
335041b03daSEd Maste 	bpb.bpbHugeSectors -= (o.offset / bpb.bpbBytesPerSec);
336041b03daSEd Maste 	if (bpb.bpbSecPerClust == 0) {	/* set defaults */
337041b03daSEd Maste 	    if (bpb.bpbHugeSectors <= 6000)	/* about 3MB -> 512 bytes */
338041b03daSEd Maste 		bpb.bpbSecPerClust = 1;
339041b03daSEd Maste 	    else if (bpb.bpbHugeSectors <= (1<<17)) /* 64M -> 4k */
340041b03daSEd Maste 		bpb.bpbSecPerClust = 8;
341041b03daSEd Maste 	    else if (bpb.bpbHugeSectors <= (1<<19)) /* 256M -> 8k */
342041b03daSEd Maste 		bpb.bpbSecPerClust = 16;
343041b03daSEd Maste 	    else if (bpb.bpbHugeSectors <= (1<<21)) /* 1G -> 16k */
344041b03daSEd Maste 		bpb.bpbSecPerClust = 32;
345041b03daSEd Maste 	    else
346041b03daSEd Maste 		bpb.bpbSecPerClust = 64;		/* otherwise 32k */
347041b03daSEd Maste 	}
348041b03daSEd Maste     }
34933c2d974SXin LI     if (bpb.bpbBytesPerSec < MINBPS ||
35033c2d974SXin LI         bpb.bpbBytesPerSec > MAXBPS ||
35133c2d974SXin LI 	!powerof2(bpb.bpbBytesPerSec)) {
35233c2d974SXin LI 	warnx("Invalid bytes/sector (%u): must be 512, 1024, 2048 or 4096",
35333c2d974SXin LI 	    bpb.bpbBytesPerSec);
354170b1dd5SEnji Cooper 	goto done;
35599f4158cSEd Maste     }
35699f4158cSEd Maste 
35799f4158cSEd Maste     if (o.volume_label && !oklabel(o.volume_label)) {
35899f4158cSEd Maste 	warnx("%s: bad volume label", o.volume_label);
359170b1dd5SEnji Cooper 	goto done;
36099f4158cSEd Maste     }
361041b03daSEd Maste     if (!(fat = o.fat_type)) {
362041b03daSEd Maste 	if (o.floppy)
363041b03daSEd Maste 	    fat = 12;
364041b03daSEd Maste 	else if (!o.directory_entries && (o.info_sector || o.backup_sector))
365041b03daSEd Maste 	    fat = 32;
366041b03daSEd Maste     }
36799f4158cSEd Maste     if ((fat == 32 && o.directory_entries) || (fat != 32 && (o.info_sector || o.backup_sector))) {
36899f4158cSEd Maste 	warnx("-%c is not a legal FAT%s option",
369041b03daSEd Maste 	     fat == 32 ? 'e' : o.info_sector ? 'i' : 'k',
370041b03daSEd Maste 	     fat == 32 ? "32" : "12/16");
371170b1dd5SEnji Cooper 	goto done;
37299f4158cSEd Maste     }
373041b03daSEd Maste     if (o.floppy && fat == 32)
374041b03daSEd Maste 	bpb.bpbRootDirEnts = 0;
375041b03daSEd Maste     if (fat != 0 && fat != 12 && fat != 16 && fat != 32) {
37699f4158cSEd Maste 	warnx("%d: bad FAT type", fat);
377170b1dd5SEnji Cooper 	goto done;
378041b03daSEd Maste     }
379041b03daSEd Maste 
380041b03daSEd Maste     if (o.block_size) {
38199f4158cSEd Maste 	if (!powerof2(o.block_size)) {
38299f4158cSEd Maste 	    warnx("block size (%u) is not a power of 2", o.block_size);
383170b1dd5SEnji Cooper 	    goto done;
38499f4158cSEd Maste 	}
38599f4158cSEd Maste 	if (o.block_size < bpb.bpbBytesPerSec) {
38699f4158cSEd Maste 	    warnx("block size (%u) is too small; minimum is %u",
387041b03daSEd Maste 		 o.block_size, bpb.bpbBytesPerSec);
388170b1dd5SEnji Cooper 	    goto done;
38999f4158cSEd Maste 	}
39099f4158cSEd Maste 	if (o.block_size > bpb.bpbBytesPerSec * MAXSPC) {
39199f4158cSEd Maste 	    warnx("block size (%u) is too large; maximum is %u",
392041b03daSEd Maste 		 o.block_size, bpb.bpbBytesPerSec * MAXSPC);
393170b1dd5SEnji Cooper 	    goto done;
39499f4158cSEd Maste 	}
395041b03daSEd Maste 	bpb.bpbSecPerClust = o.block_size / bpb.bpbBytesPerSec;
396041b03daSEd Maste     }
397041b03daSEd Maste     if (o.sectors_per_cluster) {
39899f4158cSEd Maste 	if (!powerof2(o.sectors_per_cluster)) {
39999f4158cSEd Maste 	    warnx("sectors/cluster (%u) is not a power of 2",
40099f4158cSEd Maste 		o.sectors_per_cluster);
401170b1dd5SEnji Cooper 	    goto done;
40299f4158cSEd Maste 	}
403041b03daSEd Maste 	bpb.bpbSecPerClust = o.sectors_per_cluster;
404041b03daSEd Maste     }
405041b03daSEd Maste     if (o.reserved_sectors)
406041b03daSEd Maste 	bpb.bpbResSectors = o.reserved_sectors;
407041b03daSEd Maste     if (o.num_FAT) {
40899f4158cSEd Maste 	if (o.num_FAT > MAXNFT) {
40999f4158cSEd Maste 	    warnx("number of FATs (%u) is too large; maximum is %u",
410041b03daSEd Maste 		 o.num_FAT, MAXNFT);
411170b1dd5SEnji Cooper 	    goto done;
41299f4158cSEd Maste 	}
413041b03daSEd Maste 	bpb.bpbFATs = o.num_FAT;
414041b03daSEd Maste     }
415*077f757dSStefan Eßer     if (o.directory_entries) {
416*077f757dSStefan Eßer 	bpb.bpbRootDirEnts = roundup(o.directory_entries,
417*077f757dSStefan Eßer 	    bpb.bpbBytesPerSec / sizeof(struct de));
418*077f757dSStefan Eßer 	if (bpb.bpbBytesPerSec == 0 || o.directory_entries >= MAXU16)
419*077f757dSStefan Eßer 	    bpb.bpbRootDirEnts = MAXU16;
420*077f757dSStefan Eßer     }
421041b03daSEd Maste     if (o.media_descriptor_set) {
42299f4158cSEd Maste 	if (o.media_descriptor < 0xf0) {
42399f4158cSEd Maste 	    warnx("illegal media descriptor (%#x)", o.media_descriptor);
424170b1dd5SEnji Cooper 	    goto done;
42599f4158cSEd Maste 	}
426041b03daSEd Maste 	bpb.bpbMedia = o.media_descriptor;
427041b03daSEd Maste     }
428041b03daSEd Maste     if (o.sectors_per_fat)
429041b03daSEd Maste 	bpb.bpbBigFATsecs = o.sectors_per_fat;
430041b03daSEd Maste     if (o.info_sector)
431041b03daSEd Maste 	bpb.bpbFSInfo = o.info_sector;
432041b03daSEd Maste     if (o.backup_sector)
433041b03daSEd Maste 	bpb.bpbBackup = o.backup_sector;
434041b03daSEd Maste     bss = 1;
435041b03daSEd Maste     bname = NULL;
436041b03daSEd Maste     fd1 = -1;
437041b03daSEd Maste     if (o.bootstrap) {
438041b03daSEd Maste 	bname = o.bootstrap;
439041b03daSEd Maste 	if (!strchr(bname, '/')) {
440041b03daSEd Maste 	    snprintf(buf, sizeof(buf), "/boot/%s", bname);
4412780a26bSXin LI 	    bname = buf;
44299f4158cSEd Maste 	}
44399f4158cSEd Maste 	if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb)) {
44499f4158cSEd Maste 	    warn("%s", bname);
445170b1dd5SEnji Cooper 	    goto done;
44699f4158cSEd Maste 	}
447041b03daSEd Maste 	if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bpbBytesPerSec ||
448041b03daSEd Maste 	    sb.st_size < bpb.bpbBytesPerSec ||
44999f4158cSEd Maste 	    sb.st_size > bpb.bpbBytesPerSec * MAXU16) {
45099f4158cSEd Maste 	    warnx("%s: inappropriate file type or format", bname);
451170b1dd5SEnji Cooper 	    goto done;
45299f4158cSEd Maste 	}
453041b03daSEd Maste 	bss = sb.st_size / bpb.bpbBytesPerSec;
454041b03daSEd Maste     }
455041b03daSEd Maste     if (!bpb.bpbFATs)
456041b03daSEd Maste 	bpb.bpbFATs = 2;
457041b03daSEd Maste     if (!fat) {
458041b03daSEd Maste 	if (bpb.bpbHugeSectors < (bpb.bpbResSectors ? bpb.bpbResSectors : bss) +
459041b03daSEd Maste 	    howmany((RESFTE + (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1)) *
460041b03daSEd Maste 		(bpb.bpbSecPerClust ? 16 : 12) / BPN,
461041b03daSEd Maste 		bpb.bpbBytesPerSec * NPB) *
462041b03daSEd Maste 	    bpb.bpbFATs +
463041b03daSEd Maste 	    howmany(bpb.bpbRootDirEnts ? bpb.bpbRootDirEnts : DEFRDE,
464041b03daSEd Maste 		    bpb.bpbBytesPerSec / sizeof(struct de)) +
465041b03daSEd Maste 	    (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1) *
466041b03daSEd Maste 	    (bpb.bpbSecPerClust ? bpb.bpbSecPerClust :
467041b03daSEd Maste 	     howmany(DEFBLK, bpb.bpbBytesPerSec)))
468041b03daSEd Maste 	    fat = 12;
469041b03daSEd Maste 	else if (bpb.bpbRootDirEnts || bpb.bpbHugeSectors <
470041b03daSEd Maste 		 (bpb.bpbResSectors ? bpb.bpbResSectors : bss) +
471041b03daSEd Maste 		 howmany((RESFTE + MAXCLS16) * 2, bpb.bpbBytesPerSec) *
472041b03daSEd Maste 		 bpb.bpbFATs +
473041b03daSEd Maste 		 howmany(DEFRDE, bpb.bpbBytesPerSec / sizeof(struct de)) +
474041b03daSEd Maste 		 (MAXCLS16 + 1) *
475041b03daSEd Maste 		 (bpb.bpbSecPerClust ? bpb.bpbSecPerClust :
476041b03daSEd Maste 		  howmany(8192, bpb.bpbBytesPerSec)))
477041b03daSEd Maste 	    fat = 16;
478041b03daSEd Maste 	else
479041b03daSEd Maste 	    fat = 32;
480041b03daSEd Maste     }
481041b03daSEd Maste     x = bss;
482041b03daSEd Maste     if (fat == 32) {
483041b03daSEd Maste 	if (!bpb.bpbFSInfo) {
48499f4158cSEd Maste 	    if (x == MAXU16 || x == bpb.bpbBackup) {
48599f4158cSEd Maste 		warnx("no room for info sector");
486170b1dd5SEnji Cooper 		goto done;
48799f4158cSEd Maste 	    }
488041b03daSEd Maste 	    bpb.bpbFSInfo = x;
489041b03daSEd Maste 	}
490041b03daSEd Maste 	if (bpb.bpbFSInfo != MAXU16 && x <= bpb.bpbFSInfo)
491041b03daSEd Maste 	    x = bpb.bpbFSInfo + 1;
492041b03daSEd Maste 	if (!bpb.bpbBackup) {
49399f4158cSEd Maste 	    if (x == MAXU16) {
49499f4158cSEd Maste 		warnx("no room for backup sector");
495170b1dd5SEnji Cooper 		goto done;
49699f4158cSEd Maste 	    }
497041b03daSEd Maste 	    bpb.bpbBackup = x;
49899f4158cSEd Maste 	} else if (bpb.bpbBackup != MAXU16 && bpb.bpbBackup == bpb.bpbFSInfo) {
49999f4158cSEd Maste 	    warnx("backup sector would overwrite info sector");
500170b1dd5SEnji Cooper 	    goto done;
50199f4158cSEd Maste 	}
502041b03daSEd Maste 	if (bpb.bpbBackup != MAXU16 && x <= bpb.bpbBackup)
503041b03daSEd Maste 	    x = bpb.bpbBackup + 1;
504041b03daSEd Maste     }
5050531ab72SXin LI 
5060531ab72SXin LI     extra_res = 0;
5070531ab72SXin LI     alignment = 0;
5080531ab72SXin LI     set_res = (bpb.bpbResSectors == 0);
5090531ab72SXin LI     set_spf = (bpb.bpbBigFATsecs == 0);
5100531ab72SXin LI     set_spc = (bpb.bpbSecPerClust == 0);
5110531ab72SXin LI     saved_x = x;
5120531ab72SXin LI 
5130531ab72SXin LI     /*
5140531ab72SXin LI      * Attempt to align the root directory to cluster if o.align is set.
5150531ab72SXin LI      * This is done by padding with reserved blocks. Note that this can
5160531ab72SXin LI      * cause other factors to change, which can in turn change the alignment.
5170531ab72SXin LI      * This should take at most 2 iterations, as increasing the reserved
5180531ab72SXin LI      * amount may cause the FAT size to decrease by 1, requiring another
5190531ab72SXin LI      * bpbFATs reserved blocks. If bpbSecPerClust changes, it will
5200531ab72SXin LI      * be half of its previous size, and thus will not throw off alignment.
5210531ab72SXin LI      */
5220531ab72SXin LI     do {
5230531ab72SXin LI 	x = saved_x;
5240531ab72SXin LI 	if (set_res)
5250531ab72SXin LI 	    bpb.bpbResSectors = ((fat == 32) ?
5260531ab72SXin LI 		MAX(x, MAX(16384 / bpb.bpbBytesPerSec, 4)) : x) + extra_res;
52799f4158cSEd Maste 	else if (bpb.bpbResSectors < x) {
52899f4158cSEd Maste 	    warnx("too few reserved sectors (need %d have %d)", x,
529041b03daSEd Maste 		bpb.bpbResSectors);
530170b1dd5SEnji Cooper 	    goto done;
53199f4158cSEd Maste 	}
532041b03daSEd Maste 	if (fat != 32 && !bpb.bpbRootDirEnts)
533041b03daSEd Maste 	    bpb.bpbRootDirEnts = DEFRDE;
5340531ab72SXin LI 	rds = howmany(bpb.bpbRootDirEnts,
5350531ab72SXin LI 	    bpb.bpbBytesPerSec / sizeof(struct de));
5360531ab72SXin LI 	if (set_spc) {
537041b03daSEd Maste 	    for (bpb.bpbSecPerClust = howmany(fat == 16 ? DEFBLK16 :
538041b03daSEd Maste 		    DEFBLK, bpb.bpbBytesPerSec);
5390531ab72SXin LI 		bpb.bpbSecPerClust < MAXSPC && (bpb.bpbResSectors +
540041b03daSEd Maste 		    howmany((RESFTE + maxcls(fat)) * (fat / BPN),
5410531ab72SXin LI 			bpb.bpbBytesPerSec * NPB) * bpb.bpbFATs +
542041b03daSEd Maste 		    rds +
5430531ab72SXin LI 		    (u_int64_t) (maxcls(fat) + 1) * bpb.bpbSecPerClust) <=
5440531ab72SXin LI 		    bpb.bpbHugeSectors;
545041b03daSEd Maste 		bpb.bpbSecPerClust <<= 1)
546041b03daSEd Maste 		    continue;
5470531ab72SXin LI 
5480531ab72SXin LI 	}
54999f4158cSEd Maste 	if (fat != 32 && bpb.bpbBigFATsecs > MAXU16) {
55099f4158cSEd Maste 	    warnx("too many sectors/FAT for FAT12/16");
551170b1dd5SEnji Cooper 	    goto done;
55299f4158cSEd Maste 	}
553041b03daSEd Maste 	x1 = bpb.bpbResSectors + rds;
554041b03daSEd Maste 	x = bpb.bpbBigFATsecs ? bpb.bpbBigFATsecs : 1;
55599f4158cSEd Maste 	if (x1 + (u_int64_t)x * bpb.bpbFATs > bpb.bpbHugeSectors) {
55699f4158cSEd Maste 	    warnx("meta data exceeds file system size");
557170b1dd5SEnji Cooper 	    goto done;
55899f4158cSEd Maste 	}
559041b03daSEd Maste 	x1 += x * bpb.bpbFATs;
560041b03daSEd Maste 	x = (u_int64_t)(bpb.bpbHugeSectors - x1) * bpb.bpbBytesPerSec * NPB /
5610531ab72SXin LI 	    (bpb.bpbSecPerClust * bpb.bpbBytesPerSec * NPB +
5620531ab72SXin LI 	    fat / BPN * bpb.bpbFATs);
563041b03daSEd Maste 	x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN),
564041b03daSEd Maste 	    bpb.bpbBytesPerSec * NPB);
5650531ab72SXin LI 	if (set_spf) {
5660531ab72SXin LI 	    if (bpb.bpbBigFATsecs == 0)
567041b03daSEd Maste 		bpb.bpbBigFATsecs = x2;
568041b03daSEd Maste 	    x1 += (bpb.bpbBigFATsecs - 1) * bpb.bpbFATs;
569041b03daSEd Maste 	}
5700531ab72SXin LI 	if (set_res) {
571*077f757dSStefan Eßer 	    alignto = bpb.bpbSecPerClust;
572*077f757dSStefan Eßer 	    if (alignto > 1) {
573*077f757dSStefan Eßer 		/* align data clusters */
574*077f757dSStefan Eßer 		alignment = (bpb.bpbResSectors + bpb.bpbBigFATsecs * bpb.bpbFATs + rds) %
575*077f757dSStefan Eßer 		    alignto;
576*077f757dSStefan Eßer 		if (alignment != 0)
577*077f757dSStefan Eßer 		    extra_res += alignto - alignment;
578*077f757dSStefan Eßer 	    }
5790531ab72SXin LI 	}
5800531ab72SXin LI 	attempts++;
581*077f757dSStefan Eßer     } while (alignment != 0 && attempts < 2);
5820531ab72SXin LI     if (o.align && alignment != 0)
5830531ab72SXin LI 	warnx("warning: Alignment failed.");
5840531ab72SXin LI 
585041b03daSEd Maste     cls = (bpb.bpbHugeSectors - x1) / bpb.bpbSecPerClust;
586041b03daSEd Maste     x = (u_int64_t)bpb.bpbBigFATsecs * bpb.bpbBytesPerSec * NPB / (fat / BPN) -
587041b03daSEd Maste 	RESFTE;
588041b03daSEd Maste     if (cls > x)
589041b03daSEd Maste 	cls = x;
590041b03daSEd Maste     if (bpb.bpbBigFATsecs < x2)
591041b03daSEd Maste 	warnx("warning: sectors/FAT limits file system to %u clusters",
592041b03daSEd Maste 	      cls);
59399f4158cSEd Maste     if (cls < mincls(fat)) {
59499f4158cSEd Maste 	warnx("%u clusters too few clusters for FAT%u, need %u", cls, fat,
595041b03daSEd Maste 	    mincls(fat));
596170b1dd5SEnji Cooper 	goto done;
59799f4158cSEd Maste     }
598041b03daSEd Maste     if (cls > maxcls(fat)) {
599041b03daSEd Maste 	cls = maxcls(fat);
600041b03daSEd Maste 	bpb.bpbHugeSectors = x1 + (cls + 1) * bpb.bpbSecPerClust - 1;
601041b03daSEd Maste 	warnx("warning: FAT type limits file system to %u sectors",
602041b03daSEd Maste 	      bpb.bpbHugeSectors);
603041b03daSEd Maste     }
604041b03daSEd Maste     printf("%s: %u sector%s in %u FAT%u cluster%s "
605041b03daSEd Maste 	   "(%u bytes/cluster)\n", fname, cls * bpb.bpbSecPerClust,
606041b03daSEd Maste 	   cls * bpb.bpbSecPerClust == 1 ? "" : "s", cls, fat,
607041b03daSEd Maste 	   cls == 1 ? "" : "s", bpb.bpbBytesPerSec * bpb.bpbSecPerClust);
608041b03daSEd Maste     if (!bpb.bpbMedia)
609041b03daSEd Maste 	bpb.bpbMedia = !bpb.bpbHiddenSecs ? 0xf0 : 0xf8;
610041b03daSEd Maste     if (fat == 32)
611041b03daSEd Maste 	bpb.bpbRootClust = RESFTE;
612bf0d940aSWarner Losh     if (bpb.bpbHugeSectors <= MAXU16) {
613041b03daSEd Maste 	bpb.bpbSectors = bpb.bpbHugeSectors;
614041b03daSEd Maste 	bpb.bpbHugeSectors = 0;
615041b03daSEd Maste     }
616041b03daSEd Maste     if (fat != 32) {
617041b03daSEd Maste 	bpb.bpbFATsecs = bpb.bpbBigFATsecs;
618041b03daSEd Maste 	bpb.bpbBigFATsecs = 0;
619041b03daSEd Maste     }
620041b03daSEd Maste     print_bpb(&bpb);
621041b03daSEd Maste     if (!o.no_create) {
62228ef05f7SEd Maste 	if (o.timestamp_set) {
62328ef05f7SEd Maste 	    tv.tv_sec = now = o.timestamp;
62428ef05f7SEd Maste 	    tv.tv_usec = 0;
62528ef05f7SEd Maste 	    tm = gmtime(&now);
62628ef05f7SEd Maste 	} else {
627041b03daSEd Maste 	    gettimeofday(&tv, NULL);
628041b03daSEd Maste 	    now = tv.tv_sec;
629041b03daSEd Maste 	    tm = localtime(&now);
63028ef05f7SEd Maste 	}
63128ef05f7SEd Maste 
6321cbad9d7SXin LI 	chunksize = getchunksize();
63333c2d974SXin LI 	physbuf = malloc(chunksize);
63433c2d974SXin LI 	if (physbuf == NULL) {
63599f4158cSEd Maste 	    warn(NULL);
636170b1dd5SEnji Cooper 	    goto done;
63799f4158cSEd Maste 	}
63833c2d974SXin LI 	physbuf_end = physbuf + chunksize;
63933c2d974SXin LI 	img = physbuf;
64033c2d974SXin LI 
641041b03daSEd Maste 	dir = bpb.bpbResSectors + (bpb.bpbFATsecs ? bpb.bpbFATsecs :
642041b03daSEd Maste 				   bpb.bpbBigFATsecs) * bpb.bpbFATs;
643041b03daSEd Maste 	memset(&si_sa, 0, sizeof(si_sa));
644041b03daSEd Maste 	si_sa.sa_handler = infohandler;
645162ae9c8SAlex Richardson #ifdef SIGINFO
64699f4158cSEd Maste 	if (sigaction(SIGINFO, &si_sa, NULL) == -1) {
64799f4158cSEd Maste 	    warn("sigaction SIGINFO");
648170b1dd5SEnji Cooper 	    goto done;
64999f4158cSEd Maste 	}
650162ae9c8SAlex Richardson #endif
651041b03daSEd Maste 	for (lsn = 0; lsn < dir + (fat == 32 ? bpb.bpbSecPerClust : rds); lsn++) {
652041b03daSEd Maste 	    if (got_siginfo) {
653041b03daSEd Maste 		    fprintf(stderr,"%s: writing sector %u of %u (%u%%)\n",
654041b03daSEd Maste 			fname, lsn,
655041b03daSEd Maste 			(dir + (fat == 32 ? bpb.bpbSecPerClust: rds)),
656041b03daSEd Maste 			(lsn * 100) / (dir +
657041b03daSEd Maste 			    (fat == 32 ? bpb.bpbSecPerClust: rds)));
658041b03daSEd Maste 		    got_siginfo = 0;
659041b03daSEd Maste 	    }
660041b03daSEd Maste 	    x = lsn;
661041b03daSEd Maste 	    if (o.bootstrap &&
662041b03daSEd Maste 		fat == 32 && bpb.bpbBackup != MAXU16 &&
663041b03daSEd Maste 		bss <= bpb.bpbBackup && x >= bpb.bpbBackup) {
664041b03daSEd Maste 		x -= bpb.bpbBackup;
66599f4158cSEd Maste 		if (!x && lseek(fd1, o.offset, SEEK_SET)) {
66699f4158cSEd Maste 		    warn("%s", bname);
667170b1dd5SEnji Cooper 		    goto done;
66899f4158cSEd Maste 		}
669041b03daSEd Maste 	    }
670041b03daSEd Maste 	    if (o.bootstrap && x < bss) {
67199f4158cSEd Maste 		if ((n = read(fd1, img, bpb.bpbBytesPerSec)) == -1) {
67299f4158cSEd Maste 		    warn("%s", bname);
673170b1dd5SEnji Cooper 		    goto done;
67499f4158cSEd Maste 		}
67599f4158cSEd Maste 		if ((unsigned)n != bpb.bpbBytesPerSec) {
67699f4158cSEd Maste 		    warnx("%s: can't read sector %u", bname, x);
677170b1dd5SEnji Cooper 		    goto done;
67899f4158cSEd Maste 		}
679041b03daSEd Maste 	    } else
680041b03daSEd Maste 		memset(img, 0, bpb.bpbBytesPerSec);
681041b03daSEd Maste 	    if (!lsn ||
682041b03daSEd Maste 		(fat == 32 && bpb.bpbBackup != MAXU16 &&
683041b03daSEd Maste 		 lsn == bpb.bpbBackup)) {
684041b03daSEd Maste 		x1 = sizeof(struct bs);
685041b03daSEd Maste 		bsbpb = (struct bsbpb *)(img + x1);
686041b03daSEd Maste 		mk2(bsbpb->bpbBytesPerSec, bpb.bpbBytesPerSec);
687041b03daSEd Maste 		mk1(bsbpb->bpbSecPerClust, bpb.bpbSecPerClust);
688041b03daSEd Maste 		mk2(bsbpb->bpbResSectors, bpb.bpbResSectors);
689041b03daSEd Maste 		mk1(bsbpb->bpbFATs, bpb.bpbFATs);
690041b03daSEd Maste 		mk2(bsbpb->bpbRootDirEnts, bpb.bpbRootDirEnts);
691041b03daSEd Maste 		mk2(bsbpb->bpbSectors, bpb.bpbSectors);
692041b03daSEd Maste 		mk1(bsbpb->bpbMedia, bpb.bpbMedia);
693041b03daSEd Maste 		mk2(bsbpb->bpbFATsecs, bpb.bpbFATsecs);
694041b03daSEd Maste 		mk2(bsbpb->bpbSecPerTrack, bpb.bpbSecPerTrack);
695041b03daSEd Maste 		mk2(bsbpb->bpbHeads, bpb.bpbHeads);
696041b03daSEd Maste 		mk4(bsbpb->bpbHiddenSecs, bpb.bpbHiddenSecs);
697041b03daSEd Maste 		mk4(bsbpb->bpbHugeSectors, bpb.bpbHugeSectors);
698041b03daSEd Maste 		x1 += sizeof(struct bsbpb);
699041b03daSEd Maste 		if (fat == 32) {
700041b03daSEd Maste 		    bsxbpb = (struct bsxbpb *)(img + x1);
701041b03daSEd Maste 		    mk4(bsxbpb->bpbBigFATsecs, bpb.bpbBigFATsecs);
702041b03daSEd Maste 		    mk2(bsxbpb->bpbExtFlags, 0);
703041b03daSEd Maste 		    mk2(bsxbpb->bpbFSVers, 0);
704041b03daSEd Maste 		    mk4(bsxbpb->bpbRootClust, bpb.bpbRootClust);
705041b03daSEd Maste 		    mk2(bsxbpb->bpbFSInfo, bpb.bpbFSInfo);
706041b03daSEd Maste 		    mk2(bsxbpb->bpbBackup, bpb.bpbBackup);
707041b03daSEd Maste 		    x1 += sizeof(struct bsxbpb);
708041b03daSEd Maste 		}
709041b03daSEd Maste 		bsx = (struct bsx *)(img + x1);
710041b03daSEd Maste 		mk1(bsx->exBootSignature, 0x29);
711041b03daSEd Maste 		if (o.volume_id_set)
712041b03daSEd Maste 		    x = o.volume_id;
713041b03daSEd Maste 		else
714041b03daSEd Maste 		    x = (((u_int)(1 + tm->tm_mon) << 8 |
715041b03daSEd Maste 			  (u_int)tm->tm_mday) +
716041b03daSEd Maste 			 ((u_int)tm->tm_sec << 8 |
717041b03daSEd Maste 			  (u_int)(tv.tv_usec / 10))) << 16 |
718041b03daSEd Maste 			((u_int)(1900 + tm->tm_year) +
719041b03daSEd Maste 			 ((u_int)tm->tm_hour << 8 |
720041b03daSEd Maste 			  (u_int)tm->tm_min));
721041b03daSEd Maste 		mk4(bsx->exVolumeID, x);
722041b03daSEd Maste 		mklabel(bsx->exVolumeLabel, o.volume_label ? o.volume_label : "NO NAME");
72359faa1e9SEd Maste 		snprintf(buf, sizeof(buf), "FAT%u", fat);
724041b03daSEd Maste 		setstr(bsx->exFileSysType, buf, sizeof(bsx->exFileSysType));
725041b03daSEd Maste 		if (!o.bootstrap) {
726041b03daSEd Maste 		    x1 += sizeof(struct bsx);
727041b03daSEd Maste 		    bs = (struct bs *)img;
728041b03daSEd Maste 		    mk1(bs->bsJump[0], 0xeb);
729041b03daSEd Maste 		    mk1(bs->bsJump[1], x1 - 2);
730041b03daSEd Maste 		    mk1(bs->bsJump[2], 0x90);
731041b03daSEd Maste 		    setstr(bs->bsOemName, o.OEM_string ? o.OEM_string : "BSD4.4  ",
732041b03daSEd Maste 			   sizeof(bs->bsOemName));
733041b03daSEd Maste 		    memcpy(img + x1, bootcode, sizeof(bootcode));
734041b03daSEd Maste 		    mk2(img + MINBPS - 2, DOSMAGIC);
735041b03daSEd Maste 		}
736041b03daSEd Maste 	    } else if (fat == 32 && bpb.bpbFSInfo != MAXU16 &&
737041b03daSEd Maste 		       (lsn == bpb.bpbFSInfo ||
738041b03daSEd Maste 			(bpb.bpbBackup != MAXU16 &&
739041b03daSEd Maste 			 lsn == bpb.bpbBackup + bpb.bpbFSInfo))) {
740041b03daSEd Maste 		mk4(img, 0x41615252);
741041b03daSEd Maste 		mk4(img + MINBPS - 28, 0x61417272);
742041b03daSEd Maste 		mk4(img + MINBPS - 24, 0xffffffff);
743aa9cb40eSXin LI 		mk4(img + MINBPS - 20, 0xffffffff);
744041b03daSEd Maste 		mk2(img + MINBPS - 2, DOSMAGIC);
745041b03daSEd Maste 	    } else if (lsn >= bpb.bpbResSectors && lsn < dir &&
746041b03daSEd Maste 		       !((lsn - bpb.bpbResSectors) %
747041b03daSEd Maste 			 (bpb.bpbFATsecs ? bpb.bpbFATsecs :
748041b03daSEd Maste 			  bpb.bpbBigFATsecs))) {
749041b03daSEd Maste 		mk1(img[0], bpb.bpbMedia);
750041b03daSEd Maste 		for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
751041b03daSEd Maste 		    mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
752041b03daSEd Maste 	    } else if (lsn == dir && o.volume_label) {
753041b03daSEd Maste 		de = (struct de *)img;
754041b03daSEd Maste 		mklabel(de->deName, o.volume_label);
755041b03daSEd Maste 		mk1(de->deAttributes, 050);
756041b03daSEd Maste 		x = (u_int)tm->tm_hour << 11 |
757041b03daSEd Maste 		    (u_int)tm->tm_min << 5 |
758041b03daSEd Maste 		    (u_int)tm->tm_sec >> 1;
759041b03daSEd Maste 		mk2(de->deMTime, x);
760041b03daSEd Maste 		x = (u_int)(tm->tm_year - 80) << 9 |
761041b03daSEd Maste 		    (u_int)(tm->tm_mon + 1) << 5 |
762041b03daSEd Maste 		    (u_int)tm->tm_mday;
763041b03daSEd Maste 		mk2(de->deMDate, x);
764041b03daSEd Maste 	    }
76533c2d974SXin LI 	    /*
76633c2d974SXin LI 	     * Issue a write of chunksize once we have collected
76733c2d974SXin LI 	     * enough sectors.
76833c2d974SXin LI 	     */
76933c2d974SXin LI 	    img += bpb.bpbBytesPerSec;
77033c2d974SXin LI 	    if (img >= physbuf_end) {
77133c2d974SXin LI 		n = write(fd, physbuf, chunksize);
77233c2d974SXin LI 		if (n != chunksize) {
77333c2d974SXin LI 		    warnx("%s: can't write sector %u", fname, lsn);
774170b1dd5SEnji Cooper 		    goto done;
77599f4158cSEd Maste 		}
77633c2d974SXin LI 		img = physbuf;
77733c2d974SXin LI 	    }
77833c2d974SXin LI 	}
77933c2d974SXin LI 	/*
78033c2d974SXin LI 	 * Write remaining sectors, if the last write didn't end
78133c2d974SXin LI 	 * up filling a whole chunk.
78233c2d974SXin LI 	 */
78333c2d974SXin LI 	if (img != physbuf) {
78433c2d974SXin LI 		ssize_t tailsize = img - physbuf;
78533c2d974SXin LI 
78633c2d974SXin LI 		n = write(fd, physbuf, tailsize);
78733c2d974SXin LI 		if (n != tailsize) {
78899f4158cSEd Maste 		    warnx("%s: can't write sector %u", fname, lsn);
789170b1dd5SEnji Cooper 		    goto done;
79099f4158cSEd Maste 		}
791041b03daSEd Maste 	}
792041b03daSEd Maste     }
793170b1dd5SEnji Cooper     rv = 0;
794170b1dd5SEnji Cooper done:
79533c2d974SXin LI     free(physbuf);
796783d8ed0SWarner Losh     if (fd != -1)
797cd446151SWarner Losh 	    close(fd);
798783d8ed0SWarner Losh     if (fd1 != -1)
799cd446151SWarner Losh 	    close(fd1);
800170b1dd5SEnji Cooper 
801170b1dd5SEnji Cooper     return rv;
802041b03daSEd Maste }
803041b03daSEd Maste 
804041b03daSEd Maste /*
80599f4158cSEd Maste  * return -1 with error if file system is mounted.
806041b03daSEd Maste  */
807f775c417SMark Johnston #ifndef MAKEFS
80899f4158cSEd Maste static int
check_mounted(const char * fname,mode_t mode)809041b03daSEd Maste check_mounted(const char *fname, mode_t mode)
810041b03daSEd Maste {
811162ae9c8SAlex Richardson /*
812162ae9c8SAlex Richardson  * If getmntinfo() is not available (e.g. Linux) don't check. This should
813162ae9c8SAlex Richardson  * not be a problem since we will only be using makefs to create images.
814162ae9c8SAlex Richardson  */
815041b03daSEd Maste     struct statfs *mp;
816041b03daSEd Maste     const char *s1, *s2;
817041b03daSEd Maste     size_t len;
818041b03daSEd Maste     int n, r;
819041b03daSEd Maste 
82099f4158cSEd Maste     if (!(n = getmntinfo(&mp, MNT_NOWAIT))) {
82199f4158cSEd Maste 	warn("getmntinfo");
82299f4158cSEd Maste 	return -1;
82399f4158cSEd Maste     }
824041b03daSEd Maste     len = strlen(_PATH_DEV);
825041b03daSEd Maste     s1 = fname;
826041b03daSEd Maste     if (!strncmp(s1, _PATH_DEV, len))
827041b03daSEd Maste 	s1 += len;
828041b03daSEd Maste     r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
829041b03daSEd Maste     for (; n--; mp++) {
830041b03daSEd Maste 	s2 = mp->f_mntfromname;
831041b03daSEd Maste 	if (!strncmp(s2, _PATH_DEV, len))
832041b03daSEd Maste 	    s2 += len;
833041b03daSEd Maste 	if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
83499f4158cSEd Maste 	    !strcmp(s1, s2)) {
83599f4158cSEd Maste 	    warnx("%s is mounted on %s", fname, mp->f_mntonname);
83699f4158cSEd Maste 	    return -1;
837041b03daSEd Maste 	}
838041b03daSEd Maste     }
83999f4158cSEd Maste     return 0;
84099f4158cSEd Maste }
841f775c417SMark Johnston #endif
842041b03daSEd Maste 
843041b03daSEd Maste /*
8441cbad9d7SXin LI  * Get optimal I/O size
8451cbad9d7SXin LI  */
8461cbad9d7SXin LI static ssize_t
getchunksize(void)8471cbad9d7SXin LI getchunksize(void)
8481cbad9d7SXin LI {
8499990450eSEric van Gyzen 	static ssize_t chunksize;
8501cbad9d7SXin LI 
8511cbad9d7SXin LI 	if (chunksize != 0)
8529990450eSEric van Gyzen 		return (chunksize);
8531cbad9d7SXin LI 
8541cbad9d7SXin LI #ifdef	KERN_MAXPHYS
8551cbad9d7SXin LI 	int mib[2];
8561cbad9d7SXin LI 	size_t len;
8571cbad9d7SXin LI 
8581cbad9d7SXin LI 	mib[0] = CTL_KERN;
8591cbad9d7SXin LI 	mib[1] = KERN_MAXPHYS;
8601cbad9d7SXin LI 	len = sizeof(chunksize);
8611cbad9d7SXin LI 
8621cbad9d7SXin LI 	if (sysctl(mib, 2, &chunksize, &len, NULL, 0) == -1) {
8631cbad9d7SXin LI 		warn("sysctl: KERN_MAXPHYS, using %zu", (size_t)MAXPHYS);
8641cbad9d7SXin LI 		chunksize = 0;
8651cbad9d7SXin LI 	}
8661cbad9d7SXin LI #endif
8671cbad9d7SXin LI 	if (chunksize == 0)
8681cbad9d7SXin LI 		chunksize = MAXPHYS;
8691cbad9d7SXin LI 
8701cbad9d7SXin LI 	/*
8711cbad9d7SXin LI 	 * For better performance, we want to write larger chunks instead of
8721cbad9d7SXin LI 	 * individual sectors (the size can only be 512, 1024, 2048 or 4096
8731cbad9d7SXin LI 	 * bytes). Assert that chunksize can always hold an integer number of
8741cbad9d7SXin LI 	 * sectors by asserting that both are power of two numbers and the
8751cbad9d7SXin LI 	 * chunksize is greater than MAXBPS.
8761cbad9d7SXin LI 	 */
8771cbad9d7SXin LI 	static_assert(powerof2(MAXBPS), "MAXBPS is not power of 2");
8781cbad9d7SXin LI 	assert(powerof2(chunksize));
8791cbad9d7SXin LI 	assert(chunksize > MAXBPS);
8801cbad9d7SXin LI 
8819990450eSEric van Gyzen 	return (chunksize);
8821cbad9d7SXin LI }
8831cbad9d7SXin LI 
8841cbad9d7SXin LI /*
885041b03daSEd Maste  * Get a standard format.
886041b03daSEd Maste  */
88799f4158cSEd Maste static int
getstdfmt(const char * fmt,struct bpb * bpb)888041b03daSEd Maste getstdfmt(const char *fmt, struct bpb *bpb)
889041b03daSEd Maste {
890041b03daSEd Maste     u_int x, i;
891041b03daSEd Maste 
892564500deSMarcelo Araujo     x = nitems(stdfmt);
893041b03daSEd Maste     for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
89499f4158cSEd Maste     if (i == x) {
89599f4158cSEd Maste 	warnx("%s: unknown standard format", fmt);
89699f4158cSEd Maste 	return -1;
89799f4158cSEd Maste     }
898041b03daSEd Maste     *bpb = stdfmt[i].bpb;
89999f4158cSEd Maste     return 0;
900041b03daSEd Maste }
901041b03daSEd Maste 
902162ae9c8SAlex Richardson static void
compute_geometry_from_file(int fd,const char * fname,struct disklabel * lp)903162ae9c8SAlex Richardson compute_geometry_from_file(int fd, const char *fname, struct disklabel *lp)
904162ae9c8SAlex Richardson {
905162ae9c8SAlex Richardson 	struct stat st;
906162ae9c8SAlex Richardson 	off_t ms;
907162ae9c8SAlex Richardson 
908162ae9c8SAlex Richardson 	if (fstat(fd, &st))
909162ae9c8SAlex Richardson 		err(1, "cannot get disk size");
910162ae9c8SAlex Richardson 	if (!S_ISREG(st.st_mode))
911162ae9c8SAlex Richardson 		errx(1, "%s is not a regular file", fname);
912162ae9c8SAlex Richardson 	ms = st.st_size;
913162ae9c8SAlex Richardson 	lp->d_secsize = 512;
914162ae9c8SAlex Richardson 	lp->d_nsectors = 63;
915162ae9c8SAlex Richardson 	lp->d_ntracks = 255;
916162ae9c8SAlex Richardson 	lp->d_secperunit = ms / lp->d_secsize;
917162ae9c8SAlex Richardson }
918162ae9c8SAlex Richardson 
919041b03daSEd Maste /*
920041b03daSEd Maste  * Get disk slice, partition, and geometry information.
921041b03daSEd Maste  */
92299f4158cSEd Maste static int
getdiskinfo(int fd,const char * fname,const char * dtype,__unused int oflag,struct bpb * bpb)923041b03daSEd Maste getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
924041b03daSEd Maste 	    struct bpb *bpb)
925041b03daSEd Maste {
926041b03daSEd Maste     struct disklabel *lp, dlp;
927162ae9c8SAlex Richardson     off_t hs = 0;
928162ae9c8SAlex Richardson #ifndef MAKEFS
929162ae9c8SAlex Richardson     off_t ms;
930041b03daSEd Maste     struct fd_type type;
931041b03daSEd Maste 
932041b03daSEd Maste     lp = NULL;
933041b03daSEd Maste 
934041b03daSEd Maste     /* If the user specified a disk type, try to use that */
935041b03daSEd Maste     if (dtype != NULL) {
936041b03daSEd Maste 	lp = getdiskbyname(dtype);
937041b03daSEd Maste     }
938041b03daSEd Maste 
939041b03daSEd Maste     /* Maybe it's a floppy drive */
940041b03daSEd Maste     if (lp == NULL) {
941041b03daSEd Maste 	if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1) {
942041b03daSEd Maste 	    /* create a fake geometry for a file image */
943162ae9c8SAlex Richardson 	    compute_geometry_from_file(fd, fname, &dlp);
944041b03daSEd Maste 	    lp = &dlp;
945041b03daSEd Maste 	} else if (ioctl(fd, FD_GTYPE, &type) != -1) {
946041b03daSEd Maste 	    dlp.d_secsize = 128 << type.secsize;
947041b03daSEd Maste 	    dlp.d_nsectors = type.sectrac;
948041b03daSEd Maste 	    dlp.d_ntracks = type.heads;
949041b03daSEd Maste 	    dlp.d_secperunit = ms / dlp.d_secsize;
950041b03daSEd Maste 	    lp = &dlp;
951041b03daSEd Maste 	}
952041b03daSEd Maste     }
953041b03daSEd Maste 
954041b03daSEd Maste     /* Maybe it's a fixed drive */
955041b03daSEd Maste     if (lp == NULL) {
956041b03daSEd Maste 	if (bpb->bpbBytesPerSec)
957041b03daSEd Maste 	    dlp.d_secsize = bpb->bpbBytesPerSec;
958041b03daSEd Maste 	if (bpb->bpbBytesPerSec == 0 && ioctl(fd, DIOCGSECTORSIZE,
959041b03daSEd Maste 					      &dlp.d_secsize) == -1)
960041b03daSEd Maste 	    err(1, "cannot get sector size");
961041b03daSEd Maste 
962041b03daSEd Maste 	dlp.d_secperunit = ms / dlp.d_secsize;
963041b03daSEd Maste 
964041b03daSEd Maste 	if (bpb->bpbSecPerTrack == 0 && ioctl(fd, DIOCGFWSECTORS,
965041b03daSEd Maste 					      &dlp.d_nsectors) == -1) {
966041b03daSEd Maste 	    warn("cannot get number of sectors per track");
967041b03daSEd Maste 	    dlp.d_nsectors = 63;
968041b03daSEd Maste 	}
969041b03daSEd Maste 	if (bpb->bpbHeads == 0 &&
970041b03daSEd Maste 	    ioctl(fd, DIOCGFWHEADS, &dlp.d_ntracks) == -1) {
971041b03daSEd Maste 	    warn("cannot get number of heads");
972041b03daSEd Maste 	    if (dlp.d_secperunit <= 63*1*1024)
973041b03daSEd Maste 		dlp.d_ntracks = 1;
974041b03daSEd Maste 	    else if (dlp.d_secperunit <= 63*16*1024)
975041b03daSEd Maste 		dlp.d_ntracks = 16;
976041b03daSEd Maste 	    else
977041b03daSEd Maste 		dlp.d_ntracks = 255;
978041b03daSEd Maste 	}
979041b03daSEd Maste 
980041b03daSEd Maste 	hs = (ms / dlp.d_secsize) - dlp.d_secperunit;
981041b03daSEd Maste 	lp = &dlp;
982041b03daSEd Maste     }
983162ae9c8SAlex Richardson #else
984f775c417SMark Johnston     (void)dtype;
985162ae9c8SAlex Richardson     /* In the makefs case we only support image files: */
986162ae9c8SAlex Richardson     compute_geometry_from_file(fd, fname, &dlp);
987162ae9c8SAlex Richardson     lp = &dlp;
988162ae9c8SAlex Richardson #endif
989041b03daSEd Maste 
99099f4158cSEd Maste     if (bpb->bpbBytesPerSec == 0) {
99199f4158cSEd Maste 	if (ckgeom(fname, lp->d_secsize, "bytes/sector") == -1)
99299f4158cSEd Maste 	    return -1;
99399f4158cSEd Maste 	bpb->bpbBytesPerSec = lp->d_secsize;
99499f4158cSEd Maste     }
99599f4158cSEd Maste     if (bpb->bpbSecPerTrack == 0) {
99699f4158cSEd Maste 	if (ckgeom(fname, lp->d_nsectors, "sectors/track") == -1)
99799f4158cSEd Maste 	    return -1;
99899f4158cSEd Maste 	bpb->bpbSecPerTrack = lp->d_nsectors;
99999f4158cSEd Maste     }
100099f4158cSEd Maste     if (bpb->bpbHeads == 0) {
100199f4158cSEd Maste 	if (ckgeom(fname, lp->d_ntracks, "drive heads") == -1)
100299f4158cSEd Maste 	    return -1;
100399f4158cSEd Maste 	bpb->bpbHeads = lp->d_ntracks;
100499f4158cSEd Maste     }
1005041b03daSEd Maste     if (bpb->bpbHugeSectors == 0)
1006041b03daSEd Maste 	bpb->bpbHugeSectors = lp->d_secperunit;
1007041b03daSEd Maste     if (bpb->bpbHiddenSecs == 0)
1008041b03daSEd Maste 	bpb->bpbHiddenSecs = hs;
100999f4158cSEd Maste     return 0;
1010041b03daSEd Maste }
1011041b03daSEd Maste 
1012041b03daSEd Maste /*
1013041b03daSEd Maste  * Print out BPB values.
1014041b03daSEd Maste  */
1015041b03daSEd Maste static void
print_bpb(struct bpb * bpb)1016041b03daSEd Maste print_bpb(struct bpb *bpb)
1017041b03daSEd Maste {
1018041b03daSEd Maste     printf("BytesPerSec=%u SecPerClust=%u ResSectors=%u FATs=%u",
1019041b03daSEd Maste 	   bpb->bpbBytesPerSec, bpb->bpbSecPerClust, bpb->bpbResSectors,
1020041b03daSEd Maste 	   bpb->bpbFATs);
1021041b03daSEd Maste     if (bpb->bpbRootDirEnts)
1022041b03daSEd Maste 	printf(" RootDirEnts=%u", bpb->bpbRootDirEnts);
1023041b03daSEd Maste     if (bpb->bpbSectors)
1024041b03daSEd Maste 	printf(" Sectors=%u", bpb->bpbSectors);
1025041b03daSEd Maste     printf(" Media=%#x", bpb->bpbMedia);
1026041b03daSEd Maste     if (bpb->bpbFATsecs)
1027041b03daSEd Maste 	printf(" FATsecs=%u", bpb->bpbFATsecs);
1028041b03daSEd Maste     printf(" SecPerTrack=%u Heads=%u HiddenSecs=%u", bpb->bpbSecPerTrack,
1029041b03daSEd Maste 	   bpb->bpbHeads, bpb->bpbHiddenSecs);
1030041b03daSEd Maste     if (bpb->bpbHugeSectors)
1031041b03daSEd Maste 	printf(" HugeSectors=%u", bpb->bpbHugeSectors);
1032041b03daSEd Maste     if (!bpb->bpbFATsecs) {
1033041b03daSEd Maste 	printf(" FATsecs=%u RootCluster=%u", bpb->bpbBigFATsecs,
1034041b03daSEd Maste 	       bpb->bpbRootClust);
1035041b03daSEd Maste 	printf(" FSInfo=");
1036041b03daSEd Maste 	printf(bpb->bpbFSInfo == MAXU16 ? "%#x" : "%u", bpb->bpbFSInfo);
1037041b03daSEd Maste 	printf(" Backup=");
1038041b03daSEd Maste 	printf(bpb->bpbBackup == MAXU16 ? "%#x" : "%u", bpb->bpbBackup);
1039041b03daSEd Maste     }
1040041b03daSEd Maste     printf("\n");
1041041b03daSEd Maste }
1042041b03daSEd Maste 
1043041b03daSEd Maste /*
1044041b03daSEd Maste  * Check a disk geometry value.
1045041b03daSEd Maste  */
104699f4158cSEd Maste static int
ckgeom(const char * fname,u_int val,const char * msg)1047041b03daSEd Maste ckgeom(const char *fname, u_int val, const char *msg)
1048041b03daSEd Maste {
104999f4158cSEd Maste     if (!val) {
105099f4158cSEd Maste 	warnx("%s: no default %s", fname, msg);
105199f4158cSEd Maste 	return -1;
105299f4158cSEd Maste     }
105399f4158cSEd Maste     if (val > MAXU16) {
105499f4158cSEd Maste 	warnx("%s: illegal %s %d", fname, msg, val);
105599f4158cSEd Maste 	return -1;
105699f4158cSEd Maste     }
105799f4158cSEd Maste     return 0;
1058041b03daSEd Maste }
1059041b03daSEd Maste 
1060041b03daSEd Maste /*
1061041b03daSEd Maste  * Check a volume label.
1062041b03daSEd Maste  */
1063041b03daSEd Maste static int
oklabel(const char * src)1064041b03daSEd Maste oklabel(const char *src)
1065041b03daSEd Maste {
1066041b03daSEd Maste     int c, i;
1067041b03daSEd Maste 
1068041b03daSEd Maste     for (i = 0; i <= 11; i++) {
1069041b03daSEd Maste 	c = (u_char)*src++;
1070041b03daSEd Maste 	if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
1071041b03daSEd Maste 	    break;
1072041b03daSEd Maste     }
1073041b03daSEd Maste     return i && !c;
1074041b03daSEd Maste }
1075041b03daSEd Maste 
1076041b03daSEd Maste /*
1077041b03daSEd Maste  * Make a volume label.
1078041b03daSEd Maste  */
1079041b03daSEd Maste static void
mklabel(u_int8_t * dest,const char * src)1080041b03daSEd Maste mklabel(u_int8_t *dest, const char *src)
1081041b03daSEd Maste {
1082041b03daSEd Maste     int c, i;
1083041b03daSEd Maste 
1084041b03daSEd Maste     for (i = 0; i < 11; i++) {
1085041b03daSEd Maste 	c = *src ? toupper(*src++) : ' ';
1086041b03daSEd Maste 	*dest++ = !i && c == '\xe5' ? 5 : c;
1087041b03daSEd Maste     }
1088041b03daSEd Maste }
1089041b03daSEd Maste 
1090041b03daSEd Maste /*
1091041b03daSEd Maste  * Copy string, padding with spaces.
1092041b03daSEd Maste  */
1093041b03daSEd Maste static void
setstr(u_int8_t * dest,const char * src,size_t len)1094041b03daSEd Maste setstr(u_int8_t *dest, const char *src, size_t len)
1095041b03daSEd Maste {
1096041b03daSEd Maste     while (len--)
1097041b03daSEd Maste 	*dest++ = *src ? *src++ : ' ';
1098041b03daSEd Maste }
1099041b03daSEd Maste 
1100041b03daSEd Maste static void
infohandler(int sig __unused)1101041b03daSEd Maste infohandler(int sig __unused)
1102041b03daSEd Maste {
1103041b03daSEd Maste 
1104041b03daSEd Maste 	got_siginfo = 1;
1105041b03daSEd Maste }
1106