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