2 * Copyright (c) 1998 Robert Nordier
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
29 static const char rcsid[] =
33 #include <sys/param.h>
34 #include <sys/fdcio.h>
36 #include <sys/disklabel.h>
37 #include <sys/mount.h>
52 #define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
53 #define BPN 4 /* bits per nibble */
54 #define NPB 2 /* nibbles per byte */
56 #define DOSMAGIC 0xaa55 /* DOS magic number */
57 #define MINBPS 512 /* minimum bytes per sector */
58 #define MAXSPC 128 /* maximum sectors per cluster */
59 #define MAXNFT 16 /* maximum number of FATs */
60 #define DEFBLK 4096 /* default block size */
61 #define DEFBLK16 2048 /* default block size FAT16 */
62 #define DEFRDE 512 /* default root directory entries */
63 #define RESFTE 2 /* reserved FAT entries */
64 #define MINCLS12 1 /* minimum FAT12 clusters */
65 #define MINCLS16 0x1000 /* minimum FAT16 clusters */
66 #define MINCLS32 2 /* minimum FAT32 clusters */
67 #define MAXCLS12 0xfed /* maximum FAT12 clusters */
68 #define MAXCLS16 0xfff5 /* maximum FAT16 clusters */
69 #define MAXCLS32 0xffffff5 /* maximum FAT32 clusters */
71 #define mincls(fat) ((fat) == 12 ? MINCLS12 : \
72 (fat) == 16 ? MINCLS16 : \
75 #define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \
76 (fat) == 16 ? MAXCLS16 : \
83 (p)[0] = (u_int8_t)(x), \
84 (p)[1] = (u_int8_t)((x) >> 010)
87 (p)[0] = (u_int8_t)(x), \
88 (p)[1] = (u_int8_t)((x) >> 010), \
89 (p)[2] = (u_int8_t)((x) >> 020), \
90 (p)[3] = (u_int8_t)((x) >> 030)
92 #define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
93 #define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg)
94 #define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg)
95 #define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
98 u_int8_t jmp[3]; /* bootstrap entry point */
99 u_int8_t oem[8]; /* OEM name and version */
103 u_int8_t bps[2]; /* bytes per sector */
104 u_int8_t spc; /* sectors per cluster */
105 u_int8_t res[2]; /* reserved sectors */
106 u_int8_t nft; /* number of FATs */
107 u_int8_t rde[2]; /* root directory entries */
108 u_int8_t sec[2]; /* total sectors */
109 u_int8_t mid; /* media descriptor */
110 u_int8_t spf[2]; /* sectors per FAT */
111 u_int8_t spt[2]; /* sectors per track */
112 u_int8_t hds[2]; /* drive heads */
113 u_int8_t hid[4]; /* hidden sectors */
114 u_int8_t bsec[4]; /* big total sectors */
118 u_int8_t bspf[4]; /* big sectors per FAT */
119 u_int8_t xflg[2]; /* FAT control flags */
120 u_int8_t vers[2]; /* file system version */
121 u_int8_t rdcl[4]; /* root directory start cluster */
122 u_int8_t infs[2]; /* file system info sector */
123 u_int8_t bkbs[2]; /* backup boot sector */
124 u_int8_t rsvd[12]; /* reserved */
128 u_int8_t drv; /* drive number */
129 u_int8_t rsvd; /* reserved */
130 u_int8_t sig; /* extended boot signature */
131 u_int8_t volid[4]; /* volume ID number */
132 u_int8_t label[11]; /* volume label */
133 u_int8_t type[8]; /* file system type */
137 u_int8_t namext[11]; /* name and extension */
138 u_int8_t attr; /* attributes */
139 u_int8_t rsvd[10]; /* reserved */
140 u_int8_t time[2]; /* creation time */
141 u_int8_t date[2]; /* creation date */
142 u_int8_t clus[2]; /* starting cluster */
143 u_int8_t size[4]; /* size */
147 u_int bps; /* bytes per sector */
148 u_int spc; /* sectors per cluster */
149 u_int res; /* reserved sectors */
150 u_int nft; /* number of FATs */
151 u_int rde; /* root directory entries */
152 u_int sec; /* total sectors */
153 u_int mid; /* media descriptor */
154 u_int spf; /* sectors per FAT */
155 u_int spt; /* sectors per track */
156 u_int hds; /* drive heads */
157 u_int hid; /* hidden sectors */
158 u_int bsec; /* big total sectors */
159 u_int bspf; /* big sectors per FAT */
160 u_int rdcl; /* root directory start cluster */
161 u_int infs; /* file system info sector */
162 u_int bkbs; /* backup boot sector */
165 #define BPBGAP 0, 0, 0, 0, 0, 0
171 {"160", {512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1, BPBGAP}},
172 {"180", {512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1, BPBGAP}},
173 {"320", {512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2, BPBGAP}},
174 {"360", {512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2, BPBGAP}},
175 {"640", {512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2, BPBGAP}},
176 {"720", {512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2, BPBGAP}},
177 {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2, BPBGAP}},
178 {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2, BPBGAP}},
179 {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2, BPBGAP}},
180 {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2, BPBGAP}}
183 static u_int8_t bootcode[] = {
185 0x31, 0xc0, /* xor ax,ax */
186 0x8e, 0xd0, /* mov ss,ax */
187 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
189 0x8e, 0xd8, /* mov ds,ax */
190 0xe8, 0x00, 0x00, /* call $ + 3 */
192 0x83, 0xc6, 0x19, /* add si,+19h */
193 0xbb, 0x07, 0x00, /* mov bx,0007h */
196 0x84, 0xc0, /* test al,al */
197 0x74, 0x06, /* jz $ + 8 */
198 0xb4, 0x0e, /* mov ah,0eh */
199 0xcd, 0x10, /* int 10h */
200 0xeb, 0xf5, /* jmp $ - 9 */
201 0x30, 0xe4, /* xor ah,ah */
202 0xcd, 0x16, /* int 16h */
203 0xcd, 0x19, /* int 19h */
205 'N', 'o', 'n', '-', 's', 'y', 's', 't',
206 'e', 'm', ' ', 'd', 'i', 's', 'k',
208 'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
209 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
210 ' ', 'r', 'e', 'b', 'o', 'o', 't',
215 static void check_mounted(const char *, mode_t);
216 static void getstdfmt(const char *, struct bpb *);
217 static void getdiskinfo(int, const char *, const char *, int,
219 static void print_bpb(struct bpb *);
220 static u_int ckgeom(const char *, u_int, const char *);
221 static u_int argtou(const char *, u_int, u_int, const char *);
222 static int oklabel(const char *);
223 static void mklabel(u_int8_t *, const char *);
224 static void setstr(u_int8_t *, const char *, size_t);
225 static void usage(void);
228 * Construct a FAT12, FAT16, or FAT32 file system.
231 main(int argc, char *argv[])
233 static char opts[] = "NB:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
234 static const char *opt_B, *opt_L, *opt_O, *opt_f;
235 static u_int opt_F, opt_I, opt_S, opt_a, opt_b, opt_c, opt_e;
236 static u_int opt_h, opt_i, opt_k, opt_m, opt_n, opt_o, opt_r;
237 static u_int opt_s, opt_u;
239 static int Iflag, mflag, oflag;
240 char buf[MAXPATHLEN];
247 struct bsxbpb *bsxbpb;
251 const char *fname, *dtype, *bname;
254 u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
257 while ((ch = getopt(argc, argv, opts)) != -1)
266 if (strcmp(optarg, "12") &&
267 strcmp(optarg, "16") &&
268 strcmp(optarg, "32"))
269 errx(1, "%s: bad FAT type", optarg);
270 opt_F = atoi(optarg);
273 opt_I = argto4(optarg, 0, "volume ID");
277 if (!oklabel(optarg))
278 errx(1, "%s: bad volume label", optarg);
282 if (strlen(optarg) > 8)
283 errx(1, "%s: bad OEM string", optarg);
287 opt_S = argto2(optarg, 1, "bytes/sector");
290 opt_a = argto4(optarg, 1, "sectors/FAT");
293 opt_b = argtox(optarg, 1, "block size");
297 opt_c = argto1(optarg, 1, "sectors/cluster");
301 opt_e = argto2(optarg, 1, "directory entries");
307 opt_h = argto2(optarg, 1, "drive heads");
310 opt_i = argto2(optarg, 1, "info sector");
313 opt_k = argto2(optarg, 1, "backup sector");
316 opt_m = argto1(optarg, 0, "media descriptor");
320 opt_n = argto1(optarg, 1, "number of FATs");
323 opt_o = argto4(optarg, 0, "hidden sectors");
327 opt_r = argto2(optarg, 1, "reserved sectors");
330 opt_s = argto4(optarg, 1, "file system size");
333 opt_u = argto2(optarg, 1, "sectors/track");
340 if (argc < 1 || argc > 2)
343 if (!strchr(fname, '/')) {
344 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
345 if (!(fname = strdup(buf)))
349 if ((fd = open(fname, opt_N ? O_RDONLY : O_RDWR)) == -1 ||
353 check_mounted(fname, sb.st_mode);
354 if (!S_ISCHR(sb.st_mode))
355 warnx("warning: %s is not a character device", fname);
356 memset(&bpb, 0, sizeof(bpb));
358 getstdfmt(opt_f, &bpb);
374 if (!(opt_f || (opt_h && opt_u && opt_S && opt_s && oflag)))
375 getdiskinfo(fd, fname, dtype, oflag, &bpb);
376 if (!powerof2(bpb.bps))
377 errx(1, "bytes/sector (%u) is not a power of 2", bpb.bps);
378 if (bpb.bps < MINBPS)
379 errx(1, "bytes/sector (%u) is too small; minimum is %u",
381 if (!(fat = opt_F)) {
384 else if (!opt_e && (opt_i || opt_k))
387 if ((fat == 32 && opt_e) || (fat != 32 && (opt_i || opt_k)))
388 errx(1, "-%c is not a legal FAT%s option",
389 fat == 32 ? 'e' : opt_i ? 'i' : 'k',
390 fat == 32 ? "32" : "12/16");
391 if (opt_f && fat == 32)
394 if (!powerof2(opt_b))
395 errx(1, "block size (%u) is not a power of 2", opt_b);
397 errx(1, "block size (%u) is too small; minimum is %u",
399 if (opt_b > bpb.bps * MAXSPC)
400 errx(1, "block size (%u) is too large; maximum is %u",
401 opt_b, bpb.bps * MAXSPC);
402 bpb.spc = opt_b / bpb.bps;
405 if (!powerof2(opt_c))
406 errx(1, "sectors/cluster (%u) is not a power of 2", opt_c);
413 errx(1, "number of FATs (%u) is too large; maximum is %u",
421 errx(1, "illegal media descriptor (%#x)", opt_m);
435 if (!strchr(bname, '/')) {
436 snprintf(buf, sizeof(buf), "/boot/%s", bname);
437 if (!(bname = strdup(buf)))
440 if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb))
442 if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||
443 sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16)
444 errx(1, "%s: inappropriate file type or format", bname);
445 bss = sb.st_size / bpb.bps;
450 if (bpb.bsec < (bpb.res ? bpb.res : bss) +
451 howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *
452 ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *
454 howmany(bpb.rde ? bpb.rde : DEFRDE,
455 bpb.bps / sizeof(struct de)) +
456 (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *
457 (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))
459 else if (bpb.rde || bpb.bsec <
460 (bpb.res ? bpb.res : bss) +
461 howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +
462 howmany(DEFRDE, bpb.bps / sizeof(struct de)) +
464 (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))
472 if (x == MAXU16 || x == bpb.bkbs)
473 errx(1, "no room for info sector");
476 if (bpb.infs != MAXU16 && x <= bpb.infs)
480 errx(1, "no room for backup sector");
482 } else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs)
483 errx(1, "backup sector would overwrite info sector");
484 if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)
488 bpb.res = fat == 32 ? MAX(x, MAX(16384 / bpb.bps, 4)) : x;
489 else if (bpb.res < x)
490 errx(1, "too few reserved sectors");
491 if (fat != 32 && !bpb.rde)
493 rds = howmany(bpb.rde, bpb.bps / sizeof(struct de));
495 for (bpb.spc = howmany(fat == 16 ? DEFBLK16 : DEFBLK, bpb.bps);
498 howmany((RESFTE + maxcls(fat)) * (fat / BPN),
499 bpb.bps * NPB) * bpb.nft +
501 (u_int64_t)(maxcls(fat) + 1) * bpb.spc <= bpb.bsec;
503 if (fat != 32 && bpb.bspf > MAXU16)
504 errx(1, "too many sectors/FAT for FAT12/16");
506 x = bpb.bspf ? bpb.bspf : 1;
507 if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec)
508 errx(1, "meta data exceeds file system size");
510 x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /
511 (bpb.spc * bpb.bps * NPB + fat / BPN * bpb.nft);
512 x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN),
516 x1 += (bpb.bspf - 1) * bpb.nft;
518 cls = (bpb.bsec - x1) / bpb.spc;
519 x = (u_int64_t)bpb.bspf * bpb.bps * NPB / (fat / BPN) - RESFTE;
523 warnx("warning: sectors/FAT limits file system to %u clusters",
525 if (cls < mincls(fat))
526 errx(1, "%u clusters too few clusters for FAT%u, need %u", cls, fat,
528 if (cls > maxcls(fat)) {
530 bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;
531 warnx("warning: FAT type limits file system to %u sectors",
534 printf("%s: %u sector%s in %u FAT%u cluster%s "
535 "(%u bytes/cluster)\n", fname, cls * bpb.spc,
536 cls * bpb.spc == 1 ? "" : "s", cls, fat,
537 cls == 1 ? "" : "s", bpb.bps * bpb.spc);
539 bpb.mid = !bpb.hid ? 0xf0 : 0xf8;
542 if (bpb.hid + bpb.bsec <= MAXU16) {
552 gettimeofday(&tv, NULL);
554 tm = localtime(&now);
555 if (!(img = malloc(bpb.bps)))
557 dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
558 for (lsn = 0; lsn < dir + (fat == 32 ? bpb.spc : rds); lsn++) {
561 fat == 32 && bpb.bkbs != MAXU16 &&
562 bss <= bpb.bkbs && x >= bpb.bkbs) {
564 if (!x && lseek(fd1, 0, SEEK_SET))
567 if (opt_B && x < bss) {
568 if ((n = read(fd1, img, bpb.bps)) == -1)
570 if ((unsigned)n != bpb.bps)
571 errx(1, "%s: can't read sector %u", bname, x);
573 memset(img, 0, bpb.bps);
575 (fat == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {
576 x1 = sizeof(struct bs);
577 bsbpb = (struct bsbpb *)(img + x1);
578 mk2(bsbpb->bps, bpb.bps);
579 mk1(bsbpb->spc, bpb.spc);
580 mk2(bsbpb->res, bpb.res);
581 mk1(bsbpb->nft, bpb.nft);
582 mk2(bsbpb->rde, bpb.rde);
583 mk2(bsbpb->sec, bpb.sec);
584 mk1(bsbpb->mid, bpb.mid);
585 mk2(bsbpb->spf, bpb.spf);
586 mk2(bsbpb->spt, bpb.spt);
587 mk2(bsbpb->hds, bpb.hds);
588 mk4(bsbpb->hid, bpb.hid);
589 mk4(bsbpb->bsec, bpb.bsec);
590 x1 += sizeof(struct bsbpb);
592 bsxbpb = (struct bsxbpb *)(img + x1);
593 mk4(bsxbpb->bspf, bpb.bspf);
594 mk2(bsxbpb->xflg, 0);
595 mk2(bsxbpb->vers, 0);
596 mk4(bsxbpb->rdcl, bpb.rdcl);
597 mk2(bsxbpb->infs, bpb.infs);
598 mk2(bsxbpb->bkbs, bpb.bkbs);
599 x1 += sizeof(struct bsxbpb);
601 bsx = (struct bsx *)(img + x1);
606 x = (((u_int)(1 + tm->tm_mon) << 8 |
607 (u_int)tm->tm_mday) +
608 ((u_int)tm->tm_sec << 8 |
609 (u_int)(tv.tv_usec / 10))) << 16 |
610 ((u_int)(1900 + tm->tm_year) +
611 ((u_int)tm->tm_hour << 8 |
614 mklabel(bsx->label, opt_L ? opt_L : "NO NAME");
615 sprintf(buf, "FAT%u", fat);
616 setstr(bsx->type, buf, sizeof(bsx->type));
618 x1 += sizeof(struct bsx);
619 bs = (struct bs *)img;
620 mk1(bs->jmp[0], 0xeb);
621 mk1(bs->jmp[1], x1 - 2);
622 mk1(bs->jmp[2], 0x90);
623 setstr(bs->oem, opt_O ? opt_O : "BSD 4.4",
625 memcpy(img + x1, bootcode, sizeof(bootcode));
626 mk2(img + MINBPS - 2, DOSMAGIC);
628 } else if (fat == 32 && bpb.infs != MAXU16 &&
630 (bpb.bkbs != MAXU16 &&
631 lsn == bpb.bkbs + bpb.infs))) {
632 mk4(img, 0x41615252);
633 mk4(img + MINBPS - 28, 0x61417272);
634 mk4(img + MINBPS - 24, 0xffffffff);
635 mk4(img + MINBPS - 20, bpb.rdcl);
636 mk2(img + MINBPS - 2, DOSMAGIC);
637 } else if (lsn >= bpb.res && lsn < dir &&
639 (bpb.spf ? bpb.spf : bpb.bspf))) {
640 mk1(img[0], bpb.mid);
641 for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
642 mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
643 } else if (lsn == dir && opt_L) {
644 de = (struct de *)img;
645 mklabel(de->namext, opt_L);
647 x = (u_int)tm->tm_hour << 11 |
648 (u_int)tm->tm_min << 5 |
649 (u_int)tm->tm_sec >> 1;
651 x = (u_int)(tm->tm_year - 80) << 9 |
652 (u_int)(tm->tm_mon + 1) << 5 |
656 if ((n = write(fd, img, bpb.bps)) == -1)
658 if ((unsigned)n != bpb.bps)
659 errx(1, "%s: can't write sector %u", fname, lsn);
666 * Exit with error if file system is mounted.
669 check_mounted(const char *fname, mode_t mode)
676 if (!(n = getmntinfo(&mp, MNT_NOWAIT)))
677 err(1, "getmntinfo");
678 len = strlen(_PATH_DEV);
680 if (!strncmp(s1, _PATH_DEV, len))
682 r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
684 s2 = mp->f_mntfromname;
685 if (!strncmp(s2, _PATH_DEV, len))
687 if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
689 errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
694 * Get a standard format.
697 getstdfmt(const char *fmt, struct bpb *bpb)
701 x = sizeof(stdfmt) / sizeof(stdfmt[0]);
702 for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
704 errx(1, "%s: unknown standard format", fmt);
705 *bpb = stdfmt[i].bpb;
709 * Get disk slice, partition, and geometry information.
712 getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
715 struct disklabel *lp, dlp;
721 /* If the user specified a disk type, try to use that */
723 lp = getdiskbyname(dtype);
726 /* Maybe it's a floppy drive */
728 if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1)
729 errx(1, "Cannot get disk size, %s", strerror(errno));
730 if (ioctl(fd, FD_GTYPE, &type) != -1) {
731 dlp.d_secsize = 128 << type.secsize;
732 dlp.d_nsectors = type.sectrac;
733 dlp.d_ntracks = type.heads;
734 dlp.d_secperunit = ms / dlp.d_secsize;
739 /* Maybe it's a fixed drive */
741 if (ioctl(fd, DIOCGDINFO, &dlp) == -1) {
742 if (ioctl(fd, DIOCGSECTORSIZE, &dlp.d_secsize) == -1)
743 errx(1, "Cannot get sector size, %s", strerror(errno));
744 if (ioctl(fd, DIOCGFWSECTORS, &dlp.d_nsectors) == -1)
745 errx(1, "Cannot get number of sectors, %s", strerror(errno));
746 if (ioctl(fd, DIOCGFWHEADS, &dlp.d_ntracks)== -1)
747 errx(1, "Cannot get number of heads, %s", strerror(errno));
748 dlp.d_secperunit = ms / dlp.d_secsize;
751 hs = (ms / dlp.d_secsize) - dlp.d_secperunit;
756 bpb->bps = ckgeom(fname, lp->d_secsize, "bytes/sector");
758 bpb->spt = ckgeom(fname, lp->d_nsectors, "sectors/track");
760 bpb->hds = ckgeom(fname, lp->d_ntracks, "drive heads");
762 bpb->bsec = lp->d_secperunit;
768 * Print out BPB values.
771 print_bpb(struct bpb *bpb)
773 printf("bps=%u spc=%u res=%u nft=%u", bpb->bps, bpb->spc, bpb->res,
776 printf(" rde=%u", bpb->rde);
778 printf(" sec=%u", bpb->sec);
779 printf(" mid=%#x", bpb->mid);
781 printf(" spf=%u", bpb->spf);
782 printf(" spt=%u hds=%u hid=%u", bpb->spt, bpb->hds, bpb->hid);
784 printf(" bsec=%u", bpb->bsec);
786 printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);
788 printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);
790 printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);
796 * Check a disk geometry value.
799 ckgeom(const char *fname, u_int val, const char *msg)
802 errx(1, "%s: no default %s", fname, msg);
804 errx(1, "%s: illegal %s %d", fname, msg, val);
809 * Convert and check a numeric option argument.
812 argtou(const char *arg, u_int lo, u_int hi, const char *msg)
818 x = strtoul(arg, &s, 0);
819 if (errno || !*arg || *s || x < lo || x > hi)
820 errx(1, "%s: bad %s", arg, msg);
825 * Check a volume label.
828 oklabel(const char *src)
832 for (i = 0; i <= 11; i++) {
834 if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
841 * Make a volume label.
844 mklabel(u_int8_t *dest, const char *src)
848 for (i = 0; i < 11; i++) {
849 c = *src ? toupper(*src++) : ' ';
850 *dest++ = !i && c == '\xe5' ? 5 : c;
855 * Copy string, padding with spaces.
858 setstr(u_int8_t *dest, const char *src, size_t len)
861 *dest++ = *src ? *src++ : ' ';
865 * Print usage message.
871 "usage: newfs_msdos [ -options ] special [disktype]\n");
872 fprintf(stderr, "where the options are:\n");
873 fprintf(stderr, "\t-N don't create file system: "
874 "just print out parameters\n");
875 fprintf(stderr, "\t-B get bootstrap from file\n");
876 fprintf(stderr, "\t-F FAT type (12, 16, or 32)\n");
877 fprintf(stderr, "\t-I volume ID\n");
878 fprintf(stderr, "\t-L volume label\n");
879 fprintf(stderr, "\t-O OEM string\n");
880 fprintf(stderr, "\t-S bytes/sector\n");
881 fprintf(stderr, "\t-a sectors/FAT\n");
882 fprintf(stderr, "\t-b block size\n");
883 fprintf(stderr, "\t-c sectors/cluster\n");
884 fprintf(stderr, "\t-e root directory entries\n");
885 fprintf(stderr, "\t-f standard format\n");
886 fprintf(stderr, "\t-h drive heads\n");
887 fprintf(stderr, "\t-i file system info sector\n");
888 fprintf(stderr, "\t-k backup boot sector\n");
889 fprintf(stderr, "\t-m media descriptor\n");
890 fprintf(stderr, "\t-n number of FATs\n");
891 fprintf(stderr, "\t-o hidden sectors\n");
892 fprintf(stderr, "\t-r reserved sectors\n");
893 fprintf(stderr, "\t-s file system size (sectors)\n");
894 fprintf(stderr, "\t-u sectors/track\n");