Deleted Added
full compact
newfs_msdos.c (64685) newfs_msdos.c (66907)
1/*
2 * Copyright (c) 1998 Robert Nordier
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef lint
29static const char rcsid[] =
1/*
2 * Copyright (c) 1998 Robert Nordier
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef lint
29static const char rcsid[] =
30 "$FreeBSD: head/sbin/newfs_msdos/newfs_msdos.c 64685 2000-08-15 18:04:16Z rnordier $";
30 "$FreeBSD: head/sbin/newfs_msdos/newfs_msdos.c 66907 2000-10-10 01:50:26Z wollman $";
31#endif /* not lint */
32
33#include <sys/param.h>
31#endif /* not lint */
32
33#include <sys/param.h>
34#include <sys/stat.h>
35#include <sys/diskslice.h>
36#include <sys/disklabel.h>
37#include <sys/mount.h>
34#include <sys/diskslice.h>
35#include <sys/disklabel.h>
36#include <sys/mount.h>
37#include <sys/stat.h>
38#include <sys/time.h>
38
39#include <ctype.h>
40#include <err.h>
41#include <errno.h>
42#include <fcntl.h>
43#include <paths.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
39
40#include <ctype.h>
41#include <err.h>
42#include <errno.h>
43#include <fcntl.h>
44#include <paths.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <time.h>
47#include <unistd.h>
48
49#define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
50#define BPN 4 /* bits per nibble */
51#define NPB 2 /* nibbles per byte */
52
53#define DOSMAGIC 0xaa55 /* DOS magic number */
54#define MINBPS 128 /* minimum bytes per sector */
55#define MAXSPC 128 /* maximum sectors per cluster */
56#define MAXNFT 16 /* maximum number of FATs */
57#define DEFBLK 4096 /* default block size */
58#define DEFBLK16 2048 /* default block size FAT16 */
59#define DEFRDE 512 /* default root directory entries */
60#define RESFTE 2 /* reserved FAT entries */
61#define MINCLS12 1 /* minimum FAT12 clusters */
62#define MINCLS16 0x1000 /* minimum FAT16 clusters */
63#define MINCLS32 2 /* minimum FAT32 clusters */
64#define MAXCLS12 0xfed /* maximum FAT12 clusters */
65#define MAXCLS16 0xfff5 /* maximum FAT16 clusters */
66#define MAXCLS32 0xffffff5 /* maximum FAT32 clusters */
67
68#define mincls(fat) ((fat) == 12 ? MINCLS12 : \
69 (fat) == 16 ? MINCLS16 : \
70 MINCLS32)
71
72#define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \
73 (fat) == 16 ? MAXCLS16 : \
74 MAXCLS32)
75
76#define mk1(p, x) \
77 (p) = (u_int8_t)(x)
78
79#define mk2(p, x) \
80 (p)[0] = (u_int8_t)(x), \
81 (p)[1] = (u_int8_t)((x) >> 010)
82
83#define mk4(p, x) \
84 (p)[0] = (u_int8_t)(x), \
85 (p)[1] = (u_int8_t)((x) >> 010), \
86 (p)[2] = (u_int8_t)((x) >> 020), \
87 (p)[3] = (u_int8_t)((x) >> 030)
88
89#define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
90#define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg)
91#define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg)
92#define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
93
94struct bs {
95 u_int8_t jmp[3]; /* bootstrap entry point */
96 u_int8_t oem[8]; /* OEM name and version */
97};
98
99struct bsbpb {
100 u_int8_t bps[2]; /* bytes per sector */
101 u_int8_t spc; /* sectors per cluster */
102 u_int8_t res[2]; /* reserved sectors */
103 u_int8_t nft; /* number of FATs */
104 u_int8_t rde[2]; /* root directory entries */
105 u_int8_t sec[2]; /* total sectors */
106 u_int8_t mid; /* media descriptor */
107 u_int8_t spf[2]; /* sectors per FAT */
108 u_int8_t spt[2]; /* sectors per track */
109 u_int8_t hds[2]; /* drive heads */
110 u_int8_t hid[4]; /* hidden sectors */
111 u_int8_t bsec[4]; /* big total sectors */
112};
113
114struct bsxbpb {
115 u_int8_t bspf[4]; /* big sectors per FAT */
116 u_int8_t xflg[2]; /* FAT control flags */
117 u_int8_t vers[2]; /* file system version */
118 u_int8_t rdcl[4]; /* root directory start cluster */
119 u_int8_t infs[2]; /* file system info sector */
120 u_int8_t bkbs[2]; /* backup boot sector */
121 u_int8_t rsvd[12]; /* reserved */
122};
123
124struct bsx {
125 u_int8_t drv; /* drive number */
126 u_int8_t rsvd; /* reserved */
127 u_int8_t sig; /* extended boot signature */
128 u_int8_t volid[4]; /* volume ID number */
129 u_int8_t label[11]; /* volume label */
130 u_int8_t type[8]; /* file system type */
131};
132
133struct de {
134 u_int8_t namext[11]; /* name and extension */
135 u_int8_t attr; /* attributes */
136 u_int8_t rsvd[10]; /* reserved */
137 u_int8_t time[2]; /* creation time */
138 u_int8_t date[2]; /* creation date */
139 u_int8_t clus[2]; /* starting cluster */
140 u_int8_t size[4]; /* size */
141};
142
143struct bpb {
144 u_int bps; /* bytes per sector */
145 u_int spc; /* sectors per cluster */
146 u_int res; /* reserved sectors */
147 u_int nft; /* number of FATs */
148 u_int rde; /* root directory entries */
149 u_int sec; /* total sectors */
150 u_int mid; /* media descriptor */
151 u_int spf; /* sectors per FAT */
152 u_int spt; /* sectors per track */
153 u_int hds; /* drive heads */
154 u_int hid; /* hidden sectors */
155 u_int bsec; /* big total sectors */
156 u_int bspf; /* big sectors per FAT */
157 u_int rdcl; /* root directory start cluster */
158 u_int infs; /* file system info sector */
159 u_int bkbs; /* backup boot sector */
160};
161
162static struct {
163 const char *name;
164 struct bpb bpb;
165} stdfmt[] = {
166 {"160", {512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1}},
167 {"180", {512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1}},
168 {"320", {512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2}},
169 {"360", {512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2}},
170 {"640", {512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2}},
171 {"720", {512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2}},
172 {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2}},
173 {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2}},
174 {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2}},
175 {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2}}
176};
177
178static u_int8_t bootcode[] = {
179 0xfa, /* cli */
180 0x31, 0xc0, /* xor ax,ax */
181 0x8e, 0xd0, /* mov ss,ax */
182 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
183 0xfb, /* sti */
184 0x8e, 0xd8, /* mov ds,ax */
185 0xe8, 0x00, 0x00, /* call $ + 3 */
186 0x5e, /* pop si */
187 0x83, 0xc6, 0x19, /* add si,+19h */
188 0xbb, 0x07, 0x00, /* mov bx,0007h */
189 0xfc, /* cld */
190 0xac, /* lodsb */
191 0x84, 0xc0, /* test al,al */
192 0x74, 0x06, /* jz $ + 8 */
193 0xb4, 0x0e, /* mov ah,0eh */
194 0xcd, 0x10, /* int 10h */
195 0xeb, 0xf5, /* jmp $ - 9 */
196 0x30, 0xe4, /* xor ah,ah */
197 0xcd, 0x16, /* int 16h */
198 0xcd, 0x19, /* int 19h */
199 0x0d, 0x0a,
200 'N', 'o', 'n', '-', 's', 'y', 's', 't',
201 'e', 'm', ' ', 'd', 'i', 's', 'k',
202 0x0d, 0x0a,
203 'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
204 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
205 ' ', 'r', 'e', 'b', 'o', 'o', 't',
206 0x0d, 0x0a,
207 0
208};
209
210static void check_mounted(const char *, mode_t);
211static void getstdfmt(const char *, struct bpb *);
212static void getdiskinfo(int, const char *, const char *, int,
213 struct bpb *);
214static void print_bpb(struct bpb *);
215static u_int ckgeom(const char *, u_int, const char *);
216static u_int argtou(const char *, u_int, u_int, const char *);
217static int oklabel(const char *);
218static void mklabel(u_int8_t *, const char *);
219static void setstr(u_int8_t *, const char *, size_t);
220static void usage(void);
221
222/*
223 * Construct a FAT12, FAT16, or FAT32 file system.
224 */
225int
226main(int argc, char *argv[])
227{
228 static char opts[] = "NB:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
229 static const char *opt_B, *opt_L, *opt_O, *opt_f;
230 static u_int opt_F, opt_I, opt_S, opt_a, opt_b, opt_c, opt_e;
231 static u_int opt_h, opt_i, opt_k, opt_m, opt_n, opt_o, opt_r;
232 static u_int opt_s, opt_u;
233 static int opt_N;
234 static int Iflag, mflag, oflag;
235 char buf[MAXPATHLEN];
236 struct stat sb;
237 struct timeval tv;
238 struct bpb bpb;
239 struct tm *tm;
240 struct bs *bs;
241 struct bsbpb *bsbpb;
242 struct bsxbpb *bsxbpb;
243 struct bsx *bsx;
244 struct de *de;
245 u_int8_t *img;
246 const char *fname, *dtype, *bname;
247 ssize_t n;
248 time_t now;
249 u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
250 int ch, fd, fd1;
251
252 while ((ch = getopt(argc, argv, opts)) != -1)
253 switch (ch) {
254 case 'N':
255 opt_N = 1;
256 break;
257 case 'B':
258 opt_B = optarg;
259 break;
260 case 'F':
261 if (strcmp(optarg, "12") &&
262 strcmp(optarg, "16") &&
263 strcmp(optarg, "32"))
264 errx(1, "%s: bad FAT type", optarg);
265 opt_F = atoi(optarg);
266 break;
267 case 'I':
268 opt_I = argto4(optarg, 0, "volume ID");
269 Iflag = 1;
270 break;
271 case 'L':
272 if (!oklabel(optarg))
273 errx(1, "%s: bad volume label", optarg);
274 opt_L = optarg;
275 break;
276 case 'O':
277 if (strlen(optarg) > 8)
278 errx(1, "%s: bad OEM string", optarg);
279 opt_O = optarg;
280 break;
281 case 'S':
282 opt_S = argto2(optarg, 1, "bytes/sector");
283 break;
284 case 'a':
285 opt_a = argto4(optarg, 1, "sectors/FAT");
286 break;
287 case 'b':
288 opt_b = argtox(optarg, 1, "block size");
289 opt_c = 0;
290 break;
291 case 'c':
292 opt_c = argto1(optarg, 1, "sectors/cluster");
293 opt_b = 0;
294 break;
295 case 'e':
296 opt_e = argto2(optarg, 1, "directory entries");
297 break;
298 case 'f':
299 opt_f = optarg;
300 break;
301 case 'h':
302 opt_h = argto2(optarg, 1, "drive heads");
303 break;
304 case 'i':
305 opt_i = argto2(optarg, 1, "info sector");
306 break;
307 case 'k':
308 opt_k = argto2(optarg, 1, "backup sector");
309 break;
310 case 'm':
311 opt_m = argto1(optarg, 0, "media descriptor");
312 mflag = 1;
313 break;
314 case 'n':
315 opt_n = argto1(optarg, 1, "number of FATs");
316 break;
317 case 'o':
318 opt_o = argto4(optarg, 0, "hidden sectors");
319 oflag = 1;
320 break;
321 case 'r':
322 opt_r = argto2(optarg, 1, "reserved sectors");
323 break;
324 case 's':
325 opt_s = argto4(optarg, 1, "file system size");
326 break;
327 case 'u':
328 opt_u = argto2(optarg, 1, "sectors/track");
329 break;
330 default:
331 usage();
332 }
333 argc -= optind;
334 argv += optind;
335 if (argc < 1 || argc > 2)
336 usage();
337 fname = *argv++;
338 if (!strchr(fname, '/')) {
339 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
340 if (!(fname = strdup(buf)))
341 err(1, NULL);
342 }
343 dtype = *argv;
344 if ((fd = open(fname, opt_N ? O_RDONLY : O_RDWR)) == -1 ||
345 fstat(fd, &sb))
346 err(1, "%s", fname);
347 if (!opt_N)
348 check_mounted(fname, sb.st_mode);
349 if (!S_ISCHR(sb.st_mode))
350 warnx("warning: %s is not a character device", fname);
351 memset(&bpb, 0, sizeof(bpb));
352 if (opt_f) {
353 getstdfmt(opt_f, &bpb);
354 bpb.bsec = bpb.sec;
355 bpb.sec = 0;
356 bpb.bspf = bpb.spf;
357 bpb.spf = 0;
358 }
359 if (opt_h)
360 bpb.hds = opt_h;
361 if (opt_u)
362 bpb.spt = opt_u;
363 if (opt_S)
364 bpb.bps = opt_S;
365 if (opt_s)
366 bpb.bsec = opt_s;
367 if (oflag)
368 bpb.hid = opt_o;
369 if (!(opt_f || (opt_h && opt_u && opt_S && opt_s && oflag)))
370 getdiskinfo(fd, fname, dtype, oflag, &bpb);
371 if (!powerof2(bpb.bps))
372 errx(1, "bytes/sector (%u) is not a power of 2", bpb.bps);
373 if (bpb.bps < MINBPS)
374 errx(1, "bytes/sector (%u) is too small; minimum is %u",
375 bpb.bps, MINBPS);
376 if (!(fat = opt_F)) {
377 if (opt_f)
378 fat = 12;
379 else if (!opt_e && (opt_i || opt_k))
380 fat = 32;
381 }
382 if ((fat == 32 && opt_e) || (fat != 32 && (opt_i || opt_k)))
383 errx(1, "-%c is not a legal FAT%s option",
384 fat == 32 ? 'e' : opt_i ? 'i' : 'k',
385 fat == 32 ? "32" : "12/16");
386 if (opt_f && fat == 32)
387 bpb.rde = 0;
388 if (opt_b) {
389 if (!powerof2(opt_b))
390 errx(1, "block size (%u) is not a power of 2", opt_b);
391 if (opt_b < bpb.bps)
392 errx(1, "block size (%u) is too small; minimum is %u",
393 opt_b, bpb.bps);
394 if (opt_b > bpb.bps * MAXSPC)
395 errx(1, "block size (%u) is too large; maximum is %u",
396 opt_b, bpb.bps * MAXSPC);
397 bpb.spc = opt_b / bpb.bps;
398 }
399 if (opt_c) {
400 if (!powerof2(opt_c))
401 errx(1, "sectors/cluster (%u) is not a power of 2", opt_c);
402 bpb.spc = opt_c;
403 }
404 if (opt_r)
405 bpb.res = opt_r;
406 if (opt_n) {
407 if (opt_n > MAXNFT)
408 errx(1, "number of FATs (%u) is too large; maximum is %u",
409 opt_n, MAXNFT);
410 bpb.nft = opt_n;
411 }
412 if (opt_e)
413 bpb.rde = opt_e;
414 if (mflag) {
415 if (opt_m < 0xf0)
416 errx(1, "illegal media descriptor (%#x)", opt_m);
417 bpb.mid = opt_m;
418 }
419 if (opt_a)
420 bpb.bspf = opt_a;
421 if (opt_i)
422 bpb.infs = opt_i;
423 if (opt_k)
424 bpb.bkbs = opt_k;
425 bss = 1;
426 bname = NULL;
427 fd1 = -1;
428 if (opt_B) {
429 bname = opt_B;
430 if (!strchr(bname, '/')) {
431 snprintf(buf, sizeof(buf), "/boot/%s", bname);
432 if (!(bname = strdup(buf)))
433 err(1, NULL);
434 }
435 if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb))
436 err(1, "%s", bname);
437 if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||
438 sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16)
439 errx(1, "%s: inappropriate file type or format", bname);
440 bss = sb.st_size / bpb.bps;
441 }
442 if (!bpb.nft)
443 bpb.nft = 2;
444 if (!fat) {
445 if (bpb.bsec < (bpb.res ? bpb.res : bss) +
446 howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *
447 ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *
448 bpb.nft +
449 howmany(bpb.rde ? bpb.rde : DEFRDE,
450 bpb.bps / sizeof(struct de)) +
451 (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *
452 (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))
453 fat = 12;
454 else if (bpb.rde || bpb.bsec <
455 (bpb.res ? bpb.res : bss) +
456 howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +
457 howmany(DEFRDE, bpb.bps / sizeof(struct de)) +
458 (MAXCLS16 + 1) *
459 (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))
460 fat = 16;
461 else
462 fat = 32;
463 }
464 x = bss;
465 if (fat == 32) {
466 if (!bpb.infs) {
467 if (x == MAXU16 || x == bpb.bkbs)
468 errx(1, "no room for info sector");
469 bpb.infs = x;
470 }
471 if (bpb.infs != MAXU16 && x <= bpb.infs)
472 x = bpb.infs + 1;
473 if (!bpb.bkbs) {
474 if (x == MAXU16)
475 errx(1, "no room for backup sector");
476 bpb.bkbs = x;
477 } else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs)
478 errx(1, "backup sector would overwrite info sector");
479 if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)
480 x = bpb.bkbs + 1;
481 }
482 if (!bpb.res)
483 bpb.res = fat == 32 ? MAX(x, MAX(16384 / bpb.bps, 4)) : x;
484 else if (bpb.res < x)
485 errx(1, "too few reserved sectors");
486 if (fat != 32 && !bpb.rde)
487 bpb.rde = DEFRDE;
488 rds = howmany(bpb.rde, bpb.bps / sizeof(struct de));
489 if (!bpb.spc)
490 for (bpb.spc = howmany(fat == 16 ? DEFBLK16 : DEFBLK, bpb.bps);
491 bpb.spc < MAXSPC &&
492 bpb.res +
493 howmany((RESFTE + maxcls(fat)) * (fat / BPN),
494 bpb.bps * NPB) * bpb.nft +
495 rds +
496 (u_int64_t)(maxcls(fat) + 1) * bpb.spc <= bpb.bsec;
497 bpb.spc <<= 1);
498 if (fat != 32 && bpb.bspf > MAXU16)
499 errx(1, "too many sectors/FAT for FAT12/16");
500 x1 = bpb.res + rds;
501 x = bpb.bspf ? bpb.bspf : 1;
502 if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec)
503 errx(1, "meta data exceeds file system size");
504 x1 += x * bpb.nft;
505 x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /
506 (bpb.spc * bpb.bps * NPB + fat / BPN * bpb.nft);
507 x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN),
508 bpb.bps * NPB);
509 if (!bpb.bspf) {
510 bpb.bspf = x2;
511 x1 += (bpb.bspf - 1) * bpb.nft;
512 }
513 cls = (bpb.bsec - x1) / bpb.spc;
514 x = (u_int64_t)bpb.bspf * bpb.bps * NPB / (fat / BPN) - RESFTE;
515 if (cls > x)
516 cls = x;
517 if (bpb.bspf < x2)
518 warnx("warning: sectors/FAT limits file system to %u clusters",
519 cls);
520 if (cls < mincls(fat))
521 errx(1, "%u clusters too few clusters for FAT%u, need %u", cls, fat,
522 mincls(fat));
523 if (cls > maxcls(fat)) {
524 cls = maxcls(fat);
525 bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;
526 warnx("warning: FAT type limits file system to %u sectors",
527 bpb.bsec);
528 }
529 printf("%s: %u sector%s in %u FAT%u cluster%s "
530 "(%u bytes/cluster)\n", fname, cls * bpb.spc,
531 cls * bpb.spc == 1 ? "" : "s", cls, fat,
532 cls == 1 ? "" : "s", bpb.bps * bpb.spc);
533 if (!bpb.mid)
534 bpb.mid = !bpb.hid ? 0xf0 : 0xf8;
535 if (fat == 32)
536 bpb.rdcl = RESFTE;
537 if (bpb.hid + bpb.bsec <= MAXU16) {
538 bpb.sec = bpb.bsec;
539 bpb.bsec = 0;
540 }
541 if (fat != 32) {
542 bpb.spf = bpb.bspf;
543 bpb.bspf = 0;
544 }
545 print_bpb(&bpb);
546 if (!opt_N) {
547 gettimeofday(&tv, NULL);
548 now = tv.tv_sec;
549 tm = localtime(&now);
550 if (!(img = malloc(bpb.bps)))
551 err(1, NULL);
552 dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
553 for (lsn = 0; lsn < dir + (fat == 32 ? bpb.spc : rds); lsn++) {
554 x = lsn;
555 if (opt_B &&
556 fat == 32 && bpb.bkbs != MAXU16 &&
557 bss <= bpb.bkbs && x >= bpb.bkbs) {
558 x -= bpb.bkbs;
559 if (!x && lseek(fd1, 0, SEEK_SET))
560 err(1, "%s", bname);
561 }
562 if (opt_B && x < bss) {
563 if ((n = read(fd1, img, bpb.bps)) == -1)
564 err(1, "%s", bname);
565 if (n != bpb.bps)
566 errx(1, "%s: can't read sector %u", bname, x);
567 } else
568 memset(img, 0, bpb.bps);
569 if (!lsn ||
570 (fat == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {
571 x1 = sizeof(struct bs);
572 bsbpb = (struct bsbpb *)(img + x1);
573 mk2(bsbpb->bps, bpb.bps);
574 mk1(bsbpb->spc, bpb.spc);
575 mk2(bsbpb->res, bpb.res);
576 mk1(bsbpb->nft, bpb.nft);
577 mk2(bsbpb->rde, bpb.rde);
578 mk2(bsbpb->sec, bpb.sec);
579 mk1(bsbpb->mid, bpb.mid);
580 mk2(bsbpb->spf, bpb.spf);
581 mk2(bsbpb->spt, bpb.spt);
582 mk2(bsbpb->hds, bpb.hds);
583 mk4(bsbpb->hid, bpb.hid);
584 mk4(bsbpb->bsec, bpb.bsec);
585 x1 += sizeof(struct bsbpb);
586 if (fat == 32) {
587 bsxbpb = (struct bsxbpb *)(img + x1);
588 mk4(bsxbpb->bspf, bpb.bspf);
589 mk2(bsxbpb->xflg, 0);
590 mk2(bsxbpb->vers, 0);
591 mk4(bsxbpb->rdcl, bpb.rdcl);
592 mk2(bsxbpb->infs, bpb.infs);
593 mk2(bsxbpb->bkbs, bpb.bkbs);
594 x1 += sizeof(struct bsxbpb);
595 }
596 bsx = (struct bsx *)(img + x1);
597 mk1(bsx->sig, 0x29);
598 if (Iflag)
599 x = opt_I;
600 else
601 x = (((u_int)(1 + tm->tm_mon) << 8 |
602 (u_int)tm->tm_mday) +
603 ((u_int)tm->tm_sec << 8 |
604 (u_int)(tv.tv_usec / 10))) << 16 |
605 ((u_int)(1900 + tm->tm_year) +
606 ((u_int)tm->tm_hour << 8 |
607 (u_int)tm->tm_min));
608 mk4(bsx->volid, x);
609 mklabel(bsx->label, opt_L ? opt_L : "NO NAME");
610 sprintf(buf, "FAT%u", fat);
611 setstr(bsx->type, buf, sizeof(bsx->type));
612 if (!opt_B) {
613 x1 += sizeof(struct bsx);
614 bs = (struct bs *)img;
615 mk1(bs->jmp[0], 0xeb);
616 mk1(bs->jmp[1], x1 - 2);
617 mk1(bs->jmp[2], 0x90);
618 setstr(bs->oem, opt_O ? opt_O : "BSD 4.4",
619 sizeof(bs->oem));
620 memcpy(img + x1, bootcode, sizeof(bootcode));
621 mk2(img + bpb.bps - 2, DOSMAGIC);
622 }
623 } else if (fat == 32 && bpb.infs != MAXU16 &&
624 (lsn == bpb.infs ||
625 (bpb.bkbs != MAXU16 &&
626 lsn == bpb.bkbs + bpb.infs))) {
627 mk4(img, 0x41615252);
628 mk4(img + bpb.bps - 28, 0x61417272);
629 mk4(img + bpb.bps - 24, 0xffffffff);
630 mk4(img + bpb.bps - 20, bpb.rdcl);
631 mk2(img + bpb.bps - 2, DOSMAGIC);
632 } else if (lsn >= bpb.res && lsn < dir &&
633 !((lsn - bpb.res) %
634 (bpb.spf ? bpb.spf : bpb.bspf))) {
635 mk1(img[0], bpb.mid);
636 for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
637 mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
638 } else if (lsn == dir && opt_L) {
639 de = (struct de *)img;
640 mklabel(de->namext, opt_L);
641 mk1(de->attr, 050);
642 x = (u_int)tm->tm_hour << 11 |
643 (u_int)tm->tm_min << 5 |
644 (u_int)tm->tm_sec >> 1;
645 mk2(de->time, x);
646 x = (u_int)(tm->tm_year - 80) << 9 |
647 (u_int)(tm->tm_mon + 1) << 5 |
648 (u_int)tm->tm_mday;
649 mk2(de->date, x);
650 }
651 if ((n = write(fd, img, bpb.bps)) == -1)
652 err(1, "%s", fname);
653 if (n != bpb.bps)
654 errx(1, "%s: can't write sector %u", fname, lsn);
655 }
656 }
657 return 0;
658}
659
660/*
661 * Exit with error if file system is mounted.
662 */
663static void
664check_mounted(const char *fname, mode_t mode)
665{
666 struct statfs *mp;
667 const char *s1, *s2;
668 size_t len;
669 int n, r;
670
671 if (!(n = getmntinfo(&mp, MNT_NOWAIT)))
672 err(1, "getmntinfo");
673 len = strlen(_PATH_DEV);
674 s1 = fname;
675 if (!strncmp(s1, _PATH_DEV, len))
676 s1 += len;
677 r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
678 for (; n--; mp++) {
679 s2 = mp->f_mntfromname;
680 if (!strncmp(s2, _PATH_DEV, len))
681 s2 += len;
682 if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
683 !strcmp(s1, s2))
684 errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
685 }
686}
687
688/*
689 * Get a standard format.
690 */
691static void
692getstdfmt(const char *fmt, struct bpb *bpb)
693{
694 u_int x, i;
695
696 x = sizeof(stdfmt) / sizeof(stdfmt[0]);
697 for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
698 if (i == x)
699 errx(1, "%s: unknown standard format", fmt);
700 *bpb = stdfmt[i].bpb;
701}
702
703/*
704 * Get disk slice, partition, and geometry information.
705 */
706static void
707getdiskinfo(int fd, const char *fname, const char *dtype, int oflag,
708 struct bpb *bpb)
709{
710 struct diskslices ds;
711 struct disklabel dl, *lp;
712 const char *s1, *s2;
713 char *s;
714 int slice, part, fd1, i, e;
715
716 slice = part = -1;
717 s1 = fname;
718 if ((s2 = strrchr(s1, '/')))
719 s1 = s2 + 1;
720 for (s2 = s1; *s2 && !isdigit(*s2); s2++);
721 if (!*s2 || s2 == s1)
722 s2 = NULL;
723 else
724 while (isdigit(*++s2));
725 s1 = s2;
726 if (s2 && *s2 == 's') {
727 slice = strtol(s2 + 1, &s, 10);
728 if (slice < 1 || slice > MAX_SLICES - BASE_SLICE)
729 s2 = NULL;
730 else {
731 slice = BASE_SLICE + slice - 1;
732 s2 = s;
733 }
734 }
735 if (s2 && *s2 >= 'a' && *s2 <= 'a' + MAXPARTITIONS - 1) {
736 if (slice == -1)
737 slice = COMPATIBILITY_SLICE;
738 part = *s2++ - 'a';
739 }
740 if (!s2 || (*s2 && *s2 != '.'))
741 errx(1, "%s: can't figure out partition info", fname);
742 if (slice != -1 && (!oflag || (!bpb->bsec && part == -1))) {
743 if (ioctl(fd, DIOCGSLICEINFO, &ds) == -1) {
744 warn("ioctl (GSLICEINFO)");
745 errx(1, "%s: can't get slice info", fname);
746 }
747 if (slice >= ds.dss_nslices || !ds.dss_slices[slice].ds_size)
748 errx(1, "%s: slice is unavailable", fname);
749 if (!oflag)
750 bpb->hid = ds.dss_slices[slice].ds_offset;
751 if (!bpb->bsec && part == -1)
752 bpb->bsec = ds.dss_slices[slice].ds_size;
753 }
754 if (((slice == -1 || part != -1) &&
755 ((!oflag && part != -1) || !bpb->bsec)) ||
756 !bpb->bps || !bpb->spt || !bpb->hds) {
757 lp = &dl;
758 i = ioctl(fd, DIOCGDINFO, lp);
759 if (i == -1 && slice != -1 && part == -1) {
760 e = errno;
761 if (!(s = strdup(fname)))
762 err(1, NULL);
763 s[s1 - fname] = 0;
764 if ((fd1 = open(s, O_RDONLY)) != -1) {
765 i = ioctl(fd1, DIOCGDINFO, lp);
766 close(fd1);
767 }
768 free(s);
769 errno = e;
770 }
771 if (i == -1) {
772 if (!dtype) {
773 warn("ioctl (GDINFO)");
774 errx(1, "%s: can't read disk label; "
775 "disk type must be specified", fname);
776 } else if (!(lp = getdiskbyname(dtype)))
777 errx(1, "%s: unknown disk type", dtype);
778 }
779 if (slice == -1 || part != -1) {
780 if (part == -1)
781 part = RAW_PART;
782 if (part >= lp->d_npartitions ||
783 !lp->d_partitions[part].p_size)
784 errx(1, "%s: partition is unavailable", fname);
785 if (!oflag && part != -1)
786 bpb->hid += lp->d_partitions[part].p_offset;
787 if (!bpb->bsec)
788 bpb->bsec = lp->d_partitions[part].p_size;
789 }
790 if (!bpb->bps)
791 bpb->bps = ckgeom(fname, lp->d_secsize, "bytes/sector");
792 if (!bpb->spt)
793 bpb->spt = ckgeom(fname, lp->d_nsectors, "sectors/track");
794 if (!bpb->hds)
795 bpb->hds = ckgeom(fname, lp->d_ntracks, "drive heads");
796 }
797}
798
799/*
800 * Print out BPB values.
801 */
802static void
803print_bpb(struct bpb *bpb)
804{
805 printf("bps=%u spc=%u res=%u nft=%u", bpb->bps, bpb->spc, bpb->res,
806 bpb->nft);
807 if (bpb->rde)
808 printf(" rde=%u", bpb->rde);
809 if (bpb->sec)
810 printf(" sec=%u", bpb->sec);
811 printf(" mid=%#x", bpb->mid);
812 if (bpb->spf)
813 printf(" spf=%u", bpb->spf);
814 printf(" spt=%u hds=%u hid=%u", bpb->spt, bpb->hds, bpb->hid);
815 if (bpb->bsec)
816 printf(" bsec=%u", bpb->bsec);
817 if (!bpb->spf) {
818 printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);
819 printf(" infs=");
820 printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);
821 printf(" bkbs=");
822 printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);
823 }
824 printf("\n");
825}
826
827/*
828 * Check a disk geometry value.
829 */
830static u_int
831ckgeom(const char *fname, u_int val, const char *msg)
832{
833 if (!val)
834 errx(1, "%s: no default %s", fname, msg);
835 if (val > MAXU16)
836 errx(1, "%s: illegal %s", fname, msg);
837 return val;
838}
839
840/*
841 * Convert and check a numeric option argument.
842 */
843static u_int
844argtou(const char *arg, u_int lo, u_int hi, const char *msg)
845{
846 char *s;
847 u_long x;
848
849 errno = 0;
850 x = strtoul(arg, &s, 0);
851 if (errno || !*arg || *s || x < lo || x > hi)
852 errx(1, "%s: bad %s", arg, msg);
853 return x;
854}
855
856/*
857 * Check a volume label.
858 */
859static int
860oklabel(const char *src)
861{
862 int c, i;
863
864 for (i = 0; i <= 11; i++) {
865 c = (u_char)*src++;
866 if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
867 break;
868 }
869 return i && !c;
870}
871
872/*
873 * Make a volume label.
874 */
875static void
876mklabel(u_int8_t *dest, const char *src)
877{
878 int c, i;
879
880 for (i = 0; i < 11; i++) {
881 c = *src ? toupper(*src++) : ' ';
882 *dest++ = !i && c == '\xe5' ? 5 : c;
883 }
884}
885
886/*
887 * Copy string, padding with spaces.
888 */
889static void
890setstr(u_int8_t *dest, const char *src, size_t len)
891{
892 while (len--)
893 *dest++ = *src ? *src++ : ' ';
894}
895
896/*
897 * Print usage message.
898 */
899static void
900usage(void)
901{
902 fprintf(stderr,
903 "usage: newfs_msdos [ -options ] special [disktype]\n");
904 fprintf(stderr, "where the options are:\n");
905 fprintf(stderr, "\t-N don't create file system: "
906 "just print out parameters\n");
907 fprintf(stderr, "\t-B get bootstrap from file\n");
908 fprintf(stderr, "\t-F FAT type (12, 16, or 32)\n");
909 fprintf(stderr, "\t-I volume ID\n");
910 fprintf(stderr, "\t-L volume label\n");
911 fprintf(stderr, "\t-O OEM string\n");
912 fprintf(stderr, "\t-S bytes/sector\n");
913 fprintf(stderr, "\t-a sectors/FAT\n");
914 fprintf(stderr, "\t-b block size\n");
915 fprintf(stderr, "\t-c sectors/cluster\n");
916 fprintf(stderr, "\t-e root directory entries\n");
917 fprintf(stderr, "\t-f standard format\n");
918 fprintf(stderr, "\t-h drive heads\n");
919 fprintf(stderr, "\t-i file system info sector\n");
920 fprintf(stderr, "\t-k backup boot sector\n");
921 fprintf(stderr, "\t-m media descriptor\n");
922 fprintf(stderr, "\t-n number of FATs\n");
923 fprintf(stderr, "\t-o hidden sectors\n");
924 fprintf(stderr, "\t-r reserved sectors\n");
925 fprintf(stderr, "\t-s file system size (sectors)\n");
926 fprintf(stderr, "\t-u sectors/track\n");
927 exit(1);
928}
49#include <unistd.h>
50
51#define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
52#define BPN 4 /* bits per nibble */
53#define NPB 2 /* nibbles per byte */
54
55#define DOSMAGIC 0xaa55 /* DOS magic number */
56#define MINBPS 128 /* minimum bytes per sector */
57#define MAXSPC 128 /* maximum sectors per cluster */
58#define MAXNFT 16 /* maximum number of FATs */
59#define DEFBLK 4096 /* default block size */
60#define DEFBLK16 2048 /* default block size FAT16 */
61#define DEFRDE 512 /* default root directory entries */
62#define RESFTE 2 /* reserved FAT entries */
63#define MINCLS12 1 /* minimum FAT12 clusters */
64#define MINCLS16 0x1000 /* minimum FAT16 clusters */
65#define MINCLS32 2 /* minimum FAT32 clusters */
66#define MAXCLS12 0xfed /* maximum FAT12 clusters */
67#define MAXCLS16 0xfff5 /* maximum FAT16 clusters */
68#define MAXCLS32 0xffffff5 /* maximum FAT32 clusters */
69
70#define mincls(fat) ((fat) == 12 ? MINCLS12 : \
71 (fat) == 16 ? MINCLS16 : \
72 MINCLS32)
73
74#define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \
75 (fat) == 16 ? MAXCLS16 : \
76 MAXCLS32)
77
78#define mk1(p, x) \
79 (p) = (u_int8_t)(x)
80
81#define mk2(p, x) \
82 (p)[0] = (u_int8_t)(x), \
83 (p)[1] = (u_int8_t)((x) >> 010)
84
85#define mk4(p, x) \
86 (p)[0] = (u_int8_t)(x), \
87 (p)[1] = (u_int8_t)((x) >> 010), \
88 (p)[2] = (u_int8_t)((x) >> 020), \
89 (p)[3] = (u_int8_t)((x) >> 030)
90
91#define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
92#define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg)
93#define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg)
94#define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
95
96struct bs {
97 u_int8_t jmp[3]; /* bootstrap entry point */
98 u_int8_t oem[8]; /* OEM name and version */
99};
100
101struct bsbpb {
102 u_int8_t bps[2]; /* bytes per sector */
103 u_int8_t spc; /* sectors per cluster */
104 u_int8_t res[2]; /* reserved sectors */
105 u_int8_t nft; /* number of FATs */
106 u_int8_t rde[2]; /* root directory entries */
107 u_int8_t sec[2]; /* total sectors */
108 u_int8_t mid; /* media descriptor */
109 u_int8_t spf[2]; /* sectors per FAT */
110 u_int8_t spt[2]; /* sectors per track */
111 u_int8_t hds[2]; /* drive heads */
112 u_int8_t hid[4]; /* hidden sectors */
113 u_int8_t bsec[4]; /* big total sectors */
114};
115
116struct bsxbpb {
117 u_int8_t bspf[4]; /* big sectors per FAT */
118 u_int8_t xflg[2]; /* FAT control flags */
119 u_int8_t vers[2]; /* file system version */
120 u_int8_t rdcl[4]; /* root directory start cluster */
121 u_int8_t infs[2]; /* file system info sector */
122 u_int8_t bkbs[2]; /* backup boot sector */
123 u_int8_t rsvd[12]; /* reserved */
124};
125
126struct bsx {
127 u_int8_t drv; /* drive number */
128 u_int8_t rsvd; /* reserved */
129 u_int8_t sig; /* extended boot signature */
130 u_int8_t volid[4]; /* volume ID number */
131 u_int8_t label[11]; /* volume label */
132 u_int8_t type[8]; /* file system type */
133};
134
135struct de {
136 u_int8_t namext[11]; /* name and extension */
137 u_int8_t attr; /* attributes */
138 u_int8_t rsvd[10]; /* reserved */
139 u_int8_t time[2]; /* creation time */
140 u_int8_t date[2]; /* creation date */
141 u_int8_t clus[2]; /* starting cluster */
142 u_int8_t size[4]; /* size */
143};
144
145struct bpb {
146 u_int bps; /* bytes per sector */
147 u_int spc; /* sectors per cluster */
148 u_int res; /* reserved sectors */
149 u_int nft; /* number of FATs */
150 u_int rde; /* root directory entries */
151 u_int sec; /* total sectors */
152 u_int mid; /* media descriptor */
153 u_int spf; /* sectors per FAT */
154 u_int spt; /* sectors per track */
155 u_int hds; /* drive heads */
156 u_int hid; /* hidden sectors */
157 u_int bsec; /* big total sectors */
158 u_int bspf; /* big sectors per FAT */
159 u_int rdcl; /* root directory start cluster */
160 u_int infs; /* file system info sector */
161 u_int bkbs; /* backup boot sector */
162};
163
164static struct {
165 const char *name;
166 struct bpb bpb;
167} stdfmt[] = {
168 {"160", {512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1}},
169 {"180", {512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1}},
170 {"320", {512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2}},
171 {"360", {512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2}},
172 {"640", {512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2}},
173 {"720", {512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2}},
174 {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2}},
175 {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2}},
176 {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2}},
177 {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2}}
178};
179
180static u_int8_t bootcode[] = {
181 0xfa, /* cli */
182 0x31, 0xc0, /* xor ax,ax */
183 0x8e, 0xd0, /* mov ss,ax */
184 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
185 0xfb, /* sti */
186 0x8e, 0xd8, /* mov ds,ax */
187 0xe8, 0x00, 0x00, /* call $ + 3 */
188 0x5e, /* pop si */
189 0x83, 0xc6, 0x19, /* add si,+19h */
190 0xbb, 0x07, 0x00, /* mov bx,0007h */
191 0xfc, /* cld */
192 0xac, /* lodsb */
193 0x84, 0xc0, /* test al,al */
194 0x74, 0x06, /* jz $ + 8 */
195 0xb4, 0x0e, /* mov ah,0eh */
196 0xcd, 0x10, /* int 10h */
197 0xeb, 0xf5, /* jmp $ - 9 */
198 0x30, 0xe4, /* xor ah,ah */
199 0xcd, 0x16, /* int 16h */
200 0xcd, 0x19, /* int 19h */
201 0x0d, 0x0a,
202 'N', 'o', 'n', '-', 's', 'y', 's', 't',
203 'e', 'm', ' ', 'd', 'i', 's', 'k',
204 0x0d, 0x0a,
205 'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
206 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
207 ' ', 'r', 'e', 'b', 'o', 'o', 't',
208 0x0d, 0x0a,
209 0
210};
211
212static void check_mounted(const char *, mode_t);
213static void getstdfmt(const char *, struct bpb *);
214static void getdiskinfo(int, const char *, const char *, int,
215 struct bpb *);
216static void print_bpb(struct bpb *);
217static u_int ckgeom(const char *, u_int, const char *);
218static u_int argtou(const char *, u_int, u_int, const char *);
219static int oklabel(const char *);
220static void mklabel(u_int8_t *, const char *);
221static void setstr(u_int8_t *, const char *, size_t);
222static void usage(void);
223
224/*
225 * Construct a FAT12, FAT16, or FAT32 file system.
226 */
227int
228main(int argc, char *argv[])
229{
230 static char opts[] = "NB:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
231 static const char *opt_B, *opt_L, *opt_O, *opt_f;
232 static u_int opt_F, opt_I, opt_S, opt_a, opt_b, opt_c, opt_e;
233 static u_int opt_h, opt_i, opt_k, opt_m, opt_n, opt_o, opt_r;
234 static u_int opt_s, opt_u;
235 static int opt_N;
236 static int Iflag, mflag, oflag;
237 char buf[MAXPATHLEN];
238 struct stat sb;
239 struct timeval tv;
240 struct bpb bpb;
241 struct tm *tm;
242 struct bs *bs;
243 struct bsbpb *bsbpb;
244 struct bsxbpb *bsxbpb;
245 struct bsx *bsx;
246 struct de *de;
247 u_int8_t *img;
248 const char *fname, *dtype, *bname;
249 ssize_t n;
250 time_t now;
251 u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
252 int ch, fd, fd1;
253
254 while ((ch = getopt(argc, argv, opts)) != -1)
255 switch (ch) {
256 case 'N':
257 opt_N = 1;
258 break;
259 case 'B':
260 opt_B = optarg;
261 break;
262 case 'F':
263 if (strcmp(optarg, "12") &&
264 strcmp(optarg, "16") &&
265 strcmp(optarg, "32"))
266 errx(1, "%s: bad FAT type", optarg);
267 opt_F = atoi(optarg);
268 break;
269 case 'I':
270 opt_I = argto4(optarg, 0, "volume ID");
271 Iflag = 1;
272 break;
273 case 'L':
274 if (!oklabel(optarg))
275 errx(1, "%s: bad volume label", optarg);
276 opt_L = optarg;
277 break;
278 case 'O':
279 if (strlen(optarg) > 8)
280 errx(1, "%s: bad OEM string", optarg);
281 opt_O = optarg;
282 break;
283 case 'S':
284 opt_S = argto2(optarg, 1, "bytes/sector");
285 break;
286 case 'a':
287 opt_a = argto4(optarg, 1, "sectors/FAT");
288 break;
289 case 'b':
290 opt_b = argtox(optarg, 1, "block size");
291 opt_c = 0;
292 break;
293 case 'c':
294 opt_c = argto1(optarg, 1, "sectors/cluster");
295 opt_b = 0;
296 break;
297 case 'e':
298 opt_e = argto2(optarg, 1, "directory entries");
299 break;
300 case 'f':
301 opt_f = optarg;
302 break;
303 case 'h':
304 opt_h = argto2(optarg, 1, "drive heads");
305 break;
306 case 'i':
307 opt_i = argto2(optarg, 1, "info sector");
308 break;
309 case 'k':
310 opt_k = argto2(optarg, 1, "backup sector");
311 break;
312 case 'm':
313 opt_m = argto1(optarg, 0, "media descriptor");
314 mflag = 1;
315 break;
316 case 'n':
317 opt_n = argto1(optarg, 1, "number of FATs");
318 break;
319 case 'o':
320 opt_o = argto4(optarg, 0, "hidden sectors");
321 oflag = 1;
322 break;
323 case 'r':
324 opt_r = argto2(optarg, 1, "reserved sectors");
325 break;
326 case 's':
327 opt_s = argto4(optarg, 1, "file system size");
328 break;
329 case 'u':
330 opt_u = argto2(optarg, 1, "sectors/track");
331 break;
332 default:
333 usage();
334 }
335 argc -= optind;
336 argv += optind;
337 if (argc < 1 || argc > 2)
338 usage();
339 fname = *argv++;
340 if (!strchr(fname, '/')) {
341 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
342 if (!(fname = strdup(buf)))
343 err(1, NULL);
344 }
345 dtype = *argv;
346 if ((fd = open(fname, opt_N ? O_RDONLY : O_RDWR)) == -1 ||
347 fstat(fd, &sb))
348 err(1, "%s", fname);
349 if (!opt_N)
350 check_mounted(fname, sb.st_mode);
351 if (!S_ISCHR(sb.st_mode))
352 warnx("warning: %s is not a character device", fname);
353 memset(&bpb, 0, sizeof(bpb));
354 if (opt_f) {
355 getstdfmt(opt_f, &bpb);
356 bpb.bsec = bpb.sec;
357 bpb.sec = 0;
358 bpb.bspf = bpb.spf;
359 bpb.spf = 0;
360 }
361 if (opt_h)
362 bpb.hds = opt_h;
363 if (opt_u)
364 bpb.spt = opt_u;
365 if (opt_S)
366 bpb.bps = opt_S;
367 if (opt_s)
368 bpb.bsec = opt_s;
369 if (oflag)
370 bpb.hid = opt_o;
371 if (!(opt_f || (opt_h && opt_u && opt_S && opt_s && oflag)))
372 getdiskinfo(fd, fname, dtype, oflag, &bpb);
373 if (!powerof2(bpb.bps))
374 errx(1, "bytes/sector (%u) is not a power of 2", bpb.bps);
375 if (bpb.bps < MINBPS)
376 errx(1, "bytes/sector (%u) is too small; minimum is %u",
377 bpb.bps, MINBPS);
378 if (!(fat = opt_F)) {
379 if (opt_f)
380 fat = 12;
381 else if (!opt_e && (opt_i || opt_k))
382 fat = 32;
383 }
384 if ((fat == 32 && opt_e) || (fat != 32 && (opt_i || opt_k)))
385 errx(1, "-%c is not a legal FAT%s option",
386 fat == 32 ? 'e' : opt_i ? 'i' : 'k',
387 fat == 32 ? "32" : "12/16");
388 if (opt_f && fat == 32)
389 bpb.rde = 0;
390 if (opt_b) {
391 if (!powerof2(opt_b))
392 errx(1, "block size (%u) is not a power of 2", opt_b);
393 if (opt_b < bpb.bps)
394 errx(1, "block size (%u) is too small; minimum is %u",
395 opt_b, bpb.bps);
396 if (opt_b > bpb.bps * MAXSPC)
397 errx(1, "block size (%u) is too large; maximum is %u",
398 opt_b, bpb.bps * MAXSPC);
399 bpb.spc = opt_b / bpb.bps;
400 }
401 if (opt_c) {
402 if (!powerof2(opt_c))
403 errx(1, "sectors/cluster (%u) is not a power of 2", opt_c);
404 bpb.spc = opt_c;
405 }
406 if (opt_r)
407 bpb.res = opt_r;
408 if (opt_n) {
409 if (opt_n > MAXNFT)
410 errx(1, "number of FATs (%u) is too large; maximum is %u",
411 opt_n, MAXNFT);
412 bpb.nft = opt_n;
413 }
414 if (opt_e)
415 bpb.rde = opt_e;
416 if (mflag) {
417 if (opt_m < 0xf0)
418 errx(1, "illegal media descriptor (%#x)", opt_m);
419 bpb.mid = opt_m;
420 }
421 if (opt_a)
422 bpb.bspf = opt_a;
423 if (opt_i)
424 bpb.infs = opt_i;
425 if (opt_k)
426 bpb.bkbs = opt_k;
427 bss = 1;
428 bname = NULL;
429 fd1 = -1;
430 if (opt_B) {
431 bname = opt_B;
432 if (!strchr(bname, '/')) {
433 snprintf(buf, sizeof(buf), "/boot/%s", bname);
434 if (!(bname = strdup(buf)))
435 err(1, NULL);
436 }
437 if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb))
438 err(1, "%s", bname);
439 if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||
440 sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16)
441 errx(1, "%s: inappropriate file type or format", bname);
442 bss = sb.st_size / bpb.bps;
443 }
444 if (!bpb.nft)
445 bpb.nft = 2;
446 if (!fat) {
447 if (bpb.bsec < (bpb.res ? bpb.res : bss) +
448 howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *
449 ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *
450 bpb.nft +
451 howmany(bpb.rde ? bpb.rde : DEFRDE,
452 bpb.bps / sizeof(struct de)) +
453 (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *
454 (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))
455 fat = 12;
456 else if (bpb.rde || bpb.bsec <
457 (bpb.res ? bpb.res : bss) +
458 howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +
459 howmany(DEFRDE, bpb.bps / sizeof(struct de)) +
460 (MAXCLS16 + 1) *
461 (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))
462 fat = 16;
463 else
464 fat = 32;
465 }
466 x = bss;
467 if (fat == 32) {
468 if (!bpb.infs) {
469 if (x == MAXU16 || x == bpb.bkbs)
470 errx(1, "no room for info sector");
471 bpb.infs = x;
472 }
473 if (bpb.infs != MAXU16 && x <= bpb.infs)
474 x = bpb.infs + 1;
475 if (!bpb.bkbs) {
476 if (x == MAXU16)
477 errx(1, "no room for backup sector");
478 bpb.bkbs = x;
479 } else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs)
480 errx(1, "backup sector would overwrite info sector");
481 if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)
482 x = bpb.bkbs + 1;
483 }
484 if (!bpb.res)
485 bpb.res = fat == 32 ? MAX(x, MAX(16384 / bpb.bps, 4)) : x;
486 else if (bpb.res < x)
487 errx(1, "too few reserved sectors");
488 if (fat != 32 && !bpb.rde)
489 bpb.rde = DEFRDE;
490 rds = howmany(bpb.rde, bpb.bps / sizeof(struct de));
491 if (!bpb.spc)
492 for (bpb.spc = howmany(fat == 16 ? DEFBLK16 : DEFBLK, bpb.bps);
493 bpb.spc < MAXSPC &&
494 bpb.res +
495 howmany((RESFTE + maxcls(fat)) * (fat / BPN),
496 bpb.bps * NPB) * bpb.nft +
497 rds +
498 (u_int64_t)(maxcls(fat) + 1) * bpb.spc <= bpb.bsec;
499 bpb.spc <<= 1);
500 if (fat != 32 && bpb.bspf > MAXU16)
501 errx(1, "too many sectors/FAT for FAT12/16");
502 x1 = bpb.res + rds;
503 x = bpb.bspf ? bpb.bspf : 1;
504 if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec)
505 errx(1, "meta data exceeds file system size");
506 x1 += x * bpb.nft;
507 x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /
508 (bpb.spc * bpb.bps * NPB + fat / BPN * bpb.nft);
509 x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN),
510 bpb.bps * NPB);
511 if (!bpb.bspf) {
512 bpb.bspf = x2;
513 x1 += (bpb.bspf - 1) * bpb.nft;
514 }
515 cls = (bpb.bsec - x1) / bpb.spc;
516 x = (u_int64_t)bpb.bspf * bpb.bps * NPB / (fat / BPN) - RESFTE;
517 if (cls > x)
518 cls = x;
519 if (bpb.bspf < x2)
520 warnx("warning: sectors/FAT limits file system to %u clusters",
521 cls);
522 if (cls < mincls(fat))
523 errx(1, "%u clusters too few clusters for FAT%u, need %u", cls, fat,
524 mincls(fat));
525 if (cls > maxcls(fat)) {
526 cls = maxcls(fat);
527 bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;
528 warnx("warning: FAT type limits file system to %u sectors",
529 bpb.bsec);
530 }
531 printf("%s: %u sector%s in %u FAT%u cluster%s "
532 "(%u bytes/cluster)\n", fname, cls * bpb.spc,
533 cls * bpb.spc == 1 ? "" : "s", cls, fat,
534 cls == 1 ? "" : "s", bpb.bps * bpb.spc);
535 if (!bpb.mid)
536 bpb.mid = !bpb.hid ? 0xf0 : 0xf8;
537 if (fat == 32)
538 bpb.rdcl = RESFTE;
539 if (bpb.hid + bpb.bsec <= MAXU16) {
540 bpb.sec = bpb.bsec;
541 bpb.bsec = 0;
542 }
543 if (fat != 32) {
544 bpb.spf = bpb.bspf;
545 bpb.bspf = 0;
546 }
547 print_bpb(&bpb);
548 if (!opt_N) {
549 gettimeofday(&tv, NULL);
550 now = tv.tv_sec;
551 tm = localtime(&now);
552 if (!(img = malloc(bpb.bps)))
553 err(1, NULL);
554 dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
555 for (lsn = 0; lsn < dir + (fat == 32 ? bpb.spc : rds); lsn++) {
556 x = lsn;
557 if (opt_B &&
558 fat == 32 && bpb.bkbs != MAXU16 &&
559 bss <= bpb.bkbs && x >= bpb.bkbs) {
560 x -= bpb.bkbs;
561 if (!x && lseek(fd1, 0, SEEK_SET))
562 err(1, "%s", bname);
563 }
564 if (opt_B && x < bss) {
565 if ((n = read(fd1, img, bpb.bps)) == -1)
566 err(1, "%s", bname);
567 if (n != bpb.bps)
568 errx(1, "%s: can't read sector %u", bname, x);
569 } else
570 memset(img, 0, bpb.bps);
571 if (!lsn ||
572 (fat == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {
573 x1 = sizeof(struct bs);
574 bsbpb = (struct bsbpb *)(img + x1);
575 mk2(bsbpb->bps, bpb.bps);
576 mk1(bsbpb->spc, bpb.spc);
577 mk2(bsbpb->res, bpb.res);
578 mk1(bsbpb->nft, bpb.nft);
579 mk2(bsbpb->rde, bpb.rde);
580 mk2(bsbpb->sec, bpb.sec);
581 mk1(bsbpb->mid, bpb.mid);
582 mk2(bsbpb->spf, bpb.spf);
583 mk2(bsbpb->spt, bpb.spt);
584 mk2(bsbpb->hds, bpb.hds);
585 mk4(bsbpb->hid, bpb.hid);
586 mk4(bsbpb->bsec, bpb.bsec);
587 x1 += sizeof(struct bsbpb);
588 if (fat == 32) {
589 bsxbpb = (struct bsxbpb *)(img + x1);
590 mk4(bsxbpb->bspf, bpb.bspf);
591 mk2(bsxbpb->xflg, 0);
592 mk2(bsxbpb->vers, 0);
593 mk4(bsxbpb->rdcl, bpb.rdcl);
594 mk2(bsxbpb->infs, bpb.infs);
595 mk2(bsxbpb->bkbs, bpb.bkbs);
596 x1 += sizeof(struct bsxbpb);
597 }
598 bsx = (struct bsx *)(img + x1);
599 mk1(bsx->sig, 0x29);
600 if (Iflag)
601 x = opt_I;
602 else
603 x = (((u_int)(1 + tm->tm_mon) << 8 |
604 (u_int)tm->tm_mday) +
605 ((u_int)tm->tm_sec << 8 |
606 (u_int)(tv.tv_usec / 10))) << 16 |
607 ((u_int)(1900 + tm->tm_year) +
608 ((u_int)tm->tm_hour << 8 |
609 (u_int)tm->tm_min));
610 mk4(bsx->volid, x);
611 mklabel(bsx->label, opt_L ? opt_L : "NO NAME");
612 sprintf(buf, "FAT%u", fat);
613 setstr(bsx->type, buf, sizeof(bsx->type));
614 if (!opt_B) {
615 x1 += sizeof(struct bsx);
616 bs = (struct bs *)img;
617 mk1(bs->jmp[0], 0xeb);
618 mk1(bs->jmp[1], x1 - 2);
619 mk1(bs->jmp[2], 0x90);
620 setstr(bs->oem, opt_O ? opt_O : "BSD 4.4",
621 sizeof(bs->oem));
622 memcpy(img + x1, bootcode, sizeof(bootcode));
623 mk2(img + bpb.bps - 2, DOSMAGIC);
624 }
625 } else if (fat == 32 && bpb.infs != MAXU16 &&
626 (lsn == bpb.infs ||
627 (bpb.bkbs != MAXU16 &&
628 lsn == bpb.bkbs + bpb.infs))) {
629 mk4(img, 0x41615252);
630 mk4(img + bpb.bps - 28, 0x61417272);
631 mk4(img + bpb.bps - 24, 0xffffffff);
632 mk4(img + bpb.bps - 20, bpb.rdcl);
633 mk2(img + bpb.bps - 2, DOSMAGIC);
634 } else if (lsn >= bpb.res && lsn < dir &&
635 !((lsn - bpb.res) %
636 (bpb.spf ? bpb.spf : bpb.bspf))) {
637 mk1(img[0], bpb.mid);
638 for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
639 mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
640 } else if (lsn == dir && opt_L) {
641 de = (struct de *)img;
642 mklabel(de->namext, opt_L);
643 mk1(de->attr, 050);
644 x = (u_int)tm->tm_hour << 11 |
645 (u_int)tm->tm_min << 5 |
646 (u_int)tm->tm_sec >> 1;
647 mk2(de->time, x);
648 x = (u_int)(tm->tm_year - 80) << 9 |
649 (u_int)(tm->tm_mon + 1) << 5 |
650 (u_int)tm->tm_mday;
651 mk2(de->date, x);
652 }
653 if ((n = write(fd, img, bpb.bps)) == -1)
654 err(1, "%s", fname);
655 if (n != bpb.bps)
656 errx(1, "%s: can't write sector %u", fname, lsn);
657 }
658 }
659 return 0;
660}
661
662/*
663 * Exit with error if file system is mounted.
664 */
665static void
666check_mounted(const char *fname, mode_t mode)
667{
668 struct statfs *mp;
669 const char *s1, *s2;
670 size_t len;
671 int n, r;
672
673 if (!(n = getmntinfo(&mp, MNT_NOWAIT)))
674 err(1, "getmntinfo");
675 len = strlen(_PATH_DEV);
676 s1 = fname;
677 if (!strncmp(s1, _PATH_DEV, len))
678 s1 += len;
679 r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
680 for (; n--; mp++) {
681 s2 = mp->f_mntfromname;
682 if (!strncmp(s2, _PATH_DEV, len))
683 s2 += len;
684 if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
685 !strcmp(s1, s2))
686 errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
687 }
688}
689
690/*
691 * Get a standard format.
692 */
693static void
694getstdfmt(const char *fmt, struct bpb *bpb)
695{
696 u_int x, i;
697
698 x = sizeof(stdfmt) / sizeof(stdfmt[0]);
699 for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
700 if (i == x)
701 errx(1, "%s: unknown standard format", fmt);
702 *bpb = stdfmt[i].bpb;
703}
704
705/*
706 * Get disk slice, partition, and geometry information.
707 */
708static void
709getdiskinfo(int fd, const char *fname, const char *dtype, int oflag,
710 struct bpb *bpb)
711{
712 struct diskslices ds;
713 struct disklabel dl, *lp;
714 const char *s1, *s2;
715 char *s;
716 int slice, part, fd1, i, e;
717
718 slice = part = -1;
719 s1 = fname;
720 if ((s2 = strrchr(s1, '/')))
721 s1 = s2 + 1;
722 for (s2 = s1; *s2 && !isdigit(*s2); s2++);
723 if (!*s2 || s2 == s1)
724 s2 = NULL;
725 else
726 while (isdigit(*++s2));
727 s1 = s2;
728 if (s2 && *s2 == 's') {
729 slice = strtol(s2 + 1, &s, 10);
730 if (slice < 1 || slice > MAX_SLICES - BASE_SLICE)
731 s2 = NULL;
732 else {
733 slice = BASE_SLICE + slice - 1;
734 s2 = s;
735 }
736 }
737 if (s2 && *s2 >= 'a' && *s2 <= 'a' + MAXPARTITIONS - 1) {
738 if (slice == -1)
739 slice = COMPATIBILITY_SLICE;
740 part = *s2++ - 'a';
741 }
742 if (!s2 || (*s2 && *s2 != '.'))
743 errx(1, "%s: can't figure out partition info", fname);
744 if (slice != -1 && (!oflag || (!bpb->bsec && part == -1))) {
745 if (ioctl(fd, DIOCGSLICEINFO, &ds) == -1) {
746 warn("ioctl (GSLICEINFO)");
747 errx(1, "%s: can't get slice info", fname);
748 }
749 if (slice >= ds.dss_nslices || !ds.dss_slices[slice].ds_size)
750 errx(1, "%s: slice is unavailable", fname);
751 if (!oflag)
752 bpb->hid = ds.dss_slices[slice].ds_offset;
753 if (!bpb->bsec && part == -1)
754 bpb->bsec = ds.dss_slices[slice].ds_size;
755 }
756 if (((slice == -1 || part != -1) &&
757 ((!oflag && part != -1) || !bpb->bsec)) ||
758 !bpb->bps || !bpb->spt || !bpb->hds) {
759 lp = &dl;
760 i = ioctl(fd, DIOCGDINFO, lp);
761 if (i == -1 && slice != -1 && part == -1) {
762 e = errno;
763 if (!(s = strdup(fname)))
764 err(1, NULL);
765 s[s1 - fname] = 0;
766 if ((fd1 = open(s, O_RDONLY)) != -1) {
767 i = ioctl(fd1, DIOCGDINFO, lp);
768 close(fd1);
769 }
770 free(s);
771 errno = e;
772 }
773 if (i == -1) {
774 if (!dtype) {
775 warn("ioctl (GDINFO)");
776 errx(1, "%s: can't read disk label; "
777 "disk type must be specified", fname);
778 } else if (!(lp = getdiskbyname(dtype)))
779 errx(1, "%s: unknown disk type", dtype);
780 }
781 if (slice == -1 || part != -1) {
782 if (part == -1)
783 part = RAW_PART;
784 if (part >= lp->d_npartitions ||
785 !lp->d_partitions[part].p_size)
786 errx(1, "%s: partition is unavailable", fname);
787 if (!oflag && part != -1)
788 bpb->hid += lp->d_partitions[part].p_offset;
789 if (!bpb->bsec)
790 bpb->bsec = lp->d_partitions[part].p_size;
791 }
792 if (!bpb->bps)
793 bpb->bps = ckgeom(fname, lp->d_secsize, "bytes/sector");
794 if (!bpb->spt)
795 bpb->spt = ckgeom(fname, lp->d_nsectors, "sectors/track");
796 if (!bpb->hds)
797 bpb->hds = ckgeom(fname, lp->d_ntracks, "drive heads");
798 }
799}
800
801/*
802 * Print out BPB values.
803 */
804static void
805print_bpb(struct bpb *bpb)
806{
807 printf("bps=%u spc=%u res=%u nft=%u", bpb->bps, bpb->spc, bpb->res,
808 bpb->nft);
809 if (bpb->rde)
810 printf(" rde=%u", bpb->rde);
811 if (bpb->sec)
812 printf(" sec=%u", bpb->sec);
813 printf(" mid=%#x", bpb->mid);
814 if (bpb->spf)
815 printf(" spf=%u", bpb->spf);
816 printf(" spt=%u hds=%u hid=%u", bpb->spt, bpb->hds, bpb->hid);
817 if (bpb->bsec)
818 printf(" bsec=%u", bpb->bsec);
819 if (!bpb->spf) {
820 printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);
821 printf(" infs=");
822 printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);
823 printf(" bkbs=");
824 printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);
825 }
826 printf("\n");
827}
828
829/*
830 * Check a disk geometry value.
831 */
832static u_int
833ckgeom(const char *fname, u_int val, const char *msg)
834{
835 if (!val)
836 errx(1, "%s: no default %s", fname, msg);
837 if (val > MAXU16)
838 errx(1, "%s: illegal %s", fname, msg);
839 return val;
840}
841
842/*
843 * Convert and check a numeric option argument.
844 */
845static u_int
846argtou(const char *arg, u_int lo, u_int hi, const char *msg)
847{
848 char *s;
849 u_long x;
850
851 errno = 0;
852 x = strtoul(arg, &s, 0);
853 if (errno || !*arg || *s || x < lo || x > hi)
854 errx(1, "%s: bad %s", arg, msg);
855 return x;
856}
857
858/*
859 * Check a volume label.
860 */
861static int
862oklabel(const char *src)
863{
864 int c, i;
865
866 for (i = 0; i <= 11; i++) {
867 c = (u_char)*src++;
868 if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
869 break;
870 }
871 return i && !c;
872}
873
874/*
875 * Make a volume label.
876 */
877static void
878mklabel(u_int8_t *dest, const char *src)
879{
880 int c, i;
881
882 for (i = 0; i < 11; i++) {
883 c = *src ? toupper(*src++) : ' ';
884 *dest++ = !i && c == '\xe5' ? 5 : c;
885 }
886}
887
888/*
889 * Copy string, padding with spaces.
890 */
891static void
892setstr(u_int8_t *dest, const char *src, size_t len)
893{
894 while (len--)
895 *dest++ = *src ? *src++ : ' ';
896}
897
898/*
899 * Print usage message.
900 */
901static void
902usage(void)
903{
904 fprintf(stderr,
905 "usage: newfs_msdos [ -options ] special [disktype]\n");
906 fprintf(stderr, "where the options are:\n");
907 fprintf(stderr, "\t-N don't create file system: "
908 "just print out parameters\n");
909 fprintf(stderr, "\t-B get bootstrap from file\n");
910 fprintf(stderr, "\t-F FAT type (12, 16, or 32)\n");
911 fprintf(stderr, "\t-I volume ID\n");
912 fprintf(stderr, "\t-L volume label\n");
913 fprintf(stderr, "\t-O OEM string\n");
914 fprintf(stderr, "\t-S bytes/sector\n");
915 fprintf(stderr, "\t-a sectors/FAT\n");
916 fprintf(stderr, "\t-b block size\n");
917 fprintf(stderr, "\t-c sectors/cluster\n");
918 fprintf(stderr, "\t-e root directory entries\n");
919 fprintf(stderr, "\t-f standard format\n");
920 fprintf(stderr, "\t-h drive heads\n");
921 fprintf(stderr, "\t-i file system info sector\n");
922 fprintf(stderr, "\t-k backup boot sector\n");
923 fprintf(stderr, "\t-m media descriptor\n");
924 fprintf(stderr, "\t-n number of FATs\n");
925 fprintf(stderr, "\t-o hidden sectors\n");
926 fprintf(stderr, "\t-r reserved sectors\n");
927 fprintf(stderr, "\t-s file system size (sectors)\n");
928 fprintf(stderr, "\t-u sectors/track\n");
929 exit(1);
930}