1*b205d946Sflorian /* $OpenBSD: newfs_msdos.c,v 1.29 2024/04/28 16:43:42 florian Exp $ */
248f0b646Sjsg
32700319eSderaadt /*
45ca4677cSimp * Copyright (c) 1998 Robert Nordier
52700319eSderaadt * All rights reserved.
62700319eSderaadt *
72700319eSderaadt * Redistribution and use in source and binary forms, with or without
82700319eSderaadt * modification, are permitted provided that the following conditions
92700319eSderaadt * are met:
102700319eSderaadt * 1. Redistributions of source code must retain the above copyright
112700319eSderaadt * notice, this list of conditions and the following disclaimer.
122700319eSderaadt * 2. Redistributions in binary form must reproduce the above copyright
135ca4677cSimp * notice, this list of conditions and the following disclaimer in
145ca4677cSimp * the documentation and/or other materials provided with the
155ca4677cSimp * distribution.
162700319eSderaadt *
175ca4677cSimp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
185ca4677cSimp * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
195ca4677cSimp * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
205ca4677cSimp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
215ca4677cSimp * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
225ca4677cSimp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
235ca4677cSimp * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
245ca4677cSimp * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
255ca4677cSimp * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
265ca4677cSimp * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
275ca4677cSimp * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282700319eSderaadt */
292700319eSderaadt
30b9fc9a72Sderaadt #include <sys/param.h> /* powerof2 */
312700319eSderaadt #include <sys/stat.h>
32f0faf921Sderaadt #include <sys/disklabel.h>
33f0faf921Sderaadt #include <sys/ioctl.h>
3491f4f7d8Sdlg #include <sys/dkio.h>
355ca4677cSimp #include <sys/mount.h>
365ca4677cSimp
375ca4677cSimp #include <ctype.h>
382700319eSderaadt #include <err.h>
392700319eSderaadt #include <errno.h>
402700319eSderaadt #include <fcntl.h>
415ca4677cSimp #include <paths.h>
425ca4677cSimp #include <stdio.h>
435ca4677cSimp #include <stdlib.h>
445ca4677cSimp #include <string.h>
455ca4677cSimp #include <unistd.h>
46b9fc9a72Sderaadt #include <limits.h>
471a918ef3Skstailey #include <util.h>
482700319eSderaadt
49b9fc9a72Sderaadt #define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
50b9fc9a72Sderaadt #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
51b9fc9a72Sderaadt
525ca4677cSimp #define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
535ca4677cSimp #define BPN 4 /* bits per nibble */
545ca4677cSimp #define NPB 2 /* nibbles per byte */
552700319eSderaadt
565ca4677cSimp #define DOSMAGIC 0xaa55 /* DOS magic number */
579c69801eStobias #define MINBPS 512 /* minimum bytes per sector */
585ca4677cSimp #define MAXSPC 128 /* maximum sectors per cluster */
595ca4677cSimp #define MAXNFT 16 /* maximum number of FATs */
605ca4677cSimp #define DEFBLK 4096 /* default block size */
615ca4677cSimp #define DEFBLK16 2048 /* default block size FAT16 */
625ca4677cSimp #define DEFRDE 512 /* default root directory entries */
635ca4677cSimp #define RESFTE 2 /* reserved FAT entries */
645ca4677cSimp #define MINCLS12 1 /* minimum FAT12 clusters */
657693a36fSkettenis #define MINCLS16 0xff5 /* minimum FAT16 clusters */
667693a36fSkettenis #define MINCLS32 0xfff5 /* minimum FAT32 clusters */
677693a36fSkettenis #define MAXCLS12 0xff4 /* maximum FAT12 clusters */
687693a36fSkettenis #define MAXCLS16 0xfff4 /* maximum FAT16 clusters */
697693a36fSkettenis #define MAXCLS32 0xffffff4 /* maximum FAT32 clusters */
702700319eSderaadt
715ca4677cSimp #define mincls(fat) ((fat) == 12 ? MINCLS12 : \
725ca4677cSimp (fat) == 16 ? MINCLS16 : \
735ca4677cSimp MINCLS32)
742700319eSderaadt
755ca4677cSimp #define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \
765ca4677cSimp (fat) == 16 ? MAXCLS16 : \
775ca4677cSimp MAXCLS32)
785ca4677cSimp
795ca4677cSimp #define mk1(p, x) \
805ca4677cSimp (p) = (u_int8_t)(x)
815ca4677cSimp
825ca4677cSimp #define mk2(p, x) \
835ca4677cSimp (p)[0] = (u_int8_t)(x), \
845ca4677cSimp (p)[1] = (u_int8_t)((x) >> 010)
855ca4677cSimp
865ca4677cSimp #define mk4(p, x) \
875ca4677cSimp (p)[0] = (u_int8_t)(x), \
885ca4677cSimp (p)[1] = (u_int8_t)((x) >> 010), \
895ca4677cSimp (p)[2] = (u_int8_t)((x) >> 020), \
905ca4677cSimp (p)[3] = (u_int8_t)((x) >> 030)
915ca4677cSimp
925ca4677cSimp #define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
935ca4677cSimp #define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg)
945ca4677cSimp #define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg)
955ca4677cSimp #define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
965ca4677cSimp
975ca4677cSimp struct bs {
985ca4677cSimp u_int8_t jmp[3]; /* bootstrap entry point */
995ca4677cSimp u_int8_t oem[8]; /* OEM name and version */
1002700319eSderaadt };
1012700319eSderaadt
1025ca4677cSimp struct bsbpb {
1035ca4677cSimp u_int8_t bps[2]; /* bytes per sector */
1045ca4677cSimp u_int8_t spc; /* sectors per cluster */
1055ca4677cSimp u_int8_t res[2]; /* reserved sectors */
1065ca4677cSimp u_int8_t nft; /* number of FATs */
1075ca4677cSimp u_int8_t rde[2]; /* root directory entries */
1085ca4677cSimp u_int8_t sec[2]; /* total sectors */
1095ca4677cSimp u_int8_t mid; /* media descriptor */
1105ca4677cSimp u_int8_t spf[2]; /* sectors per FAT */
1115ca4677cSimp u_int8_t spt[2]; /* sectors per track */
1125ca4677cSimp u_int8_t hds[2]; /* drive heads */
1135ca4677cSimp u_int8_t hid[4]; /* hidden sectors */
1145ca4677cSimp u_int8_t bsec[4]; /* big total sectors */
1152700319eSderaadt };
1162700319eSderaadt
1175ca4677cSimp struct bsxbpb {
1185ca4677cSimp u_int8_t bspf[4]; /* big sectors per FAT */
1195ca4677cSimp u_int8_t xflg[2]; /* FAT control flags */
1205ca4677cSimp u_int8_t vers[2]; /* file system version */
1215ca4677cSimp u_int8_t rdcl[4]; /* root directory start cluster */
1225ca4677cSimp u_int8_t infs[2]; /* file system info sector */
1235ca4677cSimp u_int8_t bkbs[2]; /* backup boot sector */
1245ca4677cSimp u_int8_t rsvd[12]; /* reserved */
1255ca4677cSimp };
126f0faf921Sderaadt
1275ca4677cSimp struct bsx {
1285ca4677cSimp u_int8_t drv; /* drive number */
1295ca4677cSimp u_int8_t rsvd; /* reserved */
1305ca4677cSimp u_int8_t sig; /* extended boot signature */
1315ca4677cSimp u_int8_t volid[4]; /* volume ID number */
1325ca4677cSimp u_int8_t label[11]; /* volume label */
1335ca4677cSimp u_int8_t type[8]; /* file system type */
1345ca4677cSimp };
135f0faf921Sderaadt
1365ca4677cSimp struct de {
1375ca4677cSimp u_int8_t namext[11]; /* name and extension */
1385ca4677cSimp u_int8_t attr; /* attributes */
1395ca4677cSimp u_int8_t rsvd[10]; /* reserved */
1405ca4677cSimp u_int8_t time[2]; /* creation time */
1415ca4677cSimp u_int8_t date[2]; /* creation date */
1425ca4677cSimp u_int8_t clus[2]; /* starting cluster */
1435ca4677cSimp u_int8_t size[4]; /* size */
1445ca4677cSimp };
1452700319eSderaadt
1465ca4677cSimp struct bpb {
1475ca4677cSimp u_int bps; /* bytes per sector */
1485ca4677cSimp u_int spc; /* sectors per cluster */
1495ca4677cSimp u_int res; /* reserved sectors */
1505ca4677cSimp u_int nft; /* number of FATs */
1515ca4677cSimp u_int rde; /* root directory entries */
1525ca4677cSimp u_int sec; /* total sectors */
1535ca4677cSimp u_int mid; /* media descriptor */
1545ca4677cSimp u_int spf; /* sectors per FAT */
1555ca4677cSimp u_int spt; /* sectors per track */
1565ca4677cSimp u_int hds; /* drive heads */
1575ca4677cSimp u_int hid; /* hidden sectors */
1585ca4677cSimp u_int bsec; /* big total sectors */
1595ca4677cSimp u_int bspf; /* big sectors per FAT */
1605ca4677cSimp u_int rdcl; /* root directory start cluster */
1615ca4677cSimp u_int infs; /* file system info sector */
1625ca4677cSimp u_int bkbs; /* backup boot sector */
1635ca4677cSimp };
1645ca4677cSimp
1655ca4677cSimp static struct {
1665ca4677cSimp const char *name;
1675ca4677cSimp struct bpb bpb;
1685ca4677cSimp } stdfmt[] = {
1695ca4677cSimp {"160", {512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1}},
1705ca4677cSimp {"180", {512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1}},
1715ca4677cSimp {"320", {512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2}},
1725ca4677cSimp {"360", {512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2}},
1735ca4677cSimp {"720", {512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2}},
1745ca4677cSimp {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2}},
1755ca4677cSimp {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2}},
1765ca4677cSimp {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2}}
1775ca4677cSimp };
1785ca4677cSimp
1795ca4677cSimp static u_int8_t bootcode[] = {
1805ca4677cSimp 0xfa, /* cli */
1815ca4677cSimp 0x31, 0xc0, /* xor ax,ax */
1825ca4677cSimp 0x8e, 0xd0, /* mov ss,ax */
1835ca4677cSimp 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
1845ca4677cSimp 0xfb, /* sti */
1855ca4677cSimp 0x8e, 0xd8, /* mov ds,ax */
1865ca4677cSimp 0xe8, 0x00, 0x00, /* call $ + 3 */
1875ca4677cSimp 0x5e, /* pop si */
1885ca4677cSimp 0x83, 0xc6, 0x19, /* add si,+19h */
1895ca4677cSimp 0xbb, 0x07, 0x00, /* mov bx,0007h */
1905ca4677cSimp 0xfc, /* cld */
1915ca4677cSimp 0xac, /* lodsb */
1925ca4677cSimp 0x84, 0xc0, /* test al,al */
1935ca4677cSimp 0x74, 0x06, /* jz $ + 8 */
1945ca4677cSimp 0xb4, 0x0e, /* mov ah,0eh */
1955ca4677cSimp 0xcd, 0x10, /* int 10h */
1965ca4677cSimp 0xeb, 0xf5, /* jmp $ - 9 */
1975ca4677cSimp 0x30, 0xe4, /* xor ah,ah */
1985ca4677cSimp 0xcd, 0x16, /* int 16h */
1995ca4677cSimp 0xcd, 0x19, /* int 19h */
2005ca4677cSimp 0x0d, 0x0a,
2015ca4677cSimp 'N', 'o', 'n', '-', 's', 'y', 's', 't',
2025ca4677cSimp 'e', 'm', ' ', 'd', 'i', 's', 'k',
2035ca4677cSimp 0x0d, 0x0a,
2045ca4677cSimp 'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
2055ca4677cSimp 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
2065ca4677cSimp ' ', 'r', 'e', 'b', 'o', 'o', 't',
2075ca4677cSimp 0x0d, 0x0a,
2085ca4677cSimp 0
2095ca4677cSimp };
2105ca4677cSimp
2115ca4677cSimp static void check_mounted(const char *, mode_t);
2125ca4677cSimp static void getstdfmt(const char *, struct bpb *);
2135ca4677cSimp static void getdiskinfo(int, const char *, const char *, int,
2145ca4677cSimp struct bpb *);
2155ca4677cSimp static void print_bpb(struct bpb *);
2165ca4677cSimp static u_int ckgeom(const char *, u_int, const char *);
2175ca4677cSimp static u_int argtou(const char *, u_int, u_int, const char *);
2185ca4677cSimp static int oklabel(const char *);
2195ca4677cSimp static void mklabel(u_int8_t *, const char *);
2205ca4677cSimp static void setstr(u_int8_t *, const char *, size_t);
221b1d95c40Spyr static __dead void usage(void);
2222700319eSderaadt
2232700319eSderaadt /*
2245ca4677cSimp * Construct a FAT12, FAT16, or FAT32 file system.
2252700319eSderaadt */
2262700319eSderaadt int
main(int argc,char * argv[])2275ca4677cSimp main(int argc, char *argv[])
2282700319eSderaadt {
2295ca4677cSimp static char opts[] = "NB:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:qr:s:t:u:";
2305ca4677cSimp static const char *opt_B, *opt_L, *opt_O, *opt_f;
2315ca4677cSimp static u_int opt_F, opt_I, opt_S, opt_a, opt_b, opt_c, opt_e;
2325ca4677cSimp static u_int opt_h, opt_i, opt_k, opt_m, opt_n, opt_o, opt_r;
2335ca4677cSimp static u_int opt_s, opt_u;
2345ca4677cSimp static int opt_N;
2355ca4677cSimp static int Iflag, mflag, oflag;
236b9fc9a72Sderaadt char buf[PATH_MAX];
2375ca4677cSimp struct stat sb;
2385ca4677cSimp struct timeval tv;
2395ca4677cSimp struct bpb bpb;
2405ca4677cSimp struct tm *tm;
2415ca4677cSimp struct bs *bs;
2425ca4677cSimp struct bsbpb *bsbpb;
2435ca4677cSimp struct bsxbpb *bsxbpb;
2445ca4677cSimp struct bsx *bsx;
2455ca4677cSimp struct de *de;
2465ca4677cSimp u_int8_t *img;
2475ca4677cSimp const char *dtype, *bname;
2485ca4677cSimp char *sname, *fname;
2495ca4677cSimp ssize_t n;
2502700319eSderaadt time_t now;
2515ca4677cSimp u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
2525ca4677cSimp int ch, fd, fd1;
2532700319eSderaadt
254e384f6efSderaadt if (pledge("stdio rpath wpath disklabel", NULL) == -1)
255e384f6efSderaadt err(1, "pledge");
256e384f6efSderaadt
2575ca4677cSimp while ((ch = getopt(argc, argv, opts)) != -1)
2585ca4677cSimp switch (ch) {
2595ca4677cSimp case 'N':
2605ca4677cSimp opt_N = 1;
2615ca4677cSimp break;
2625ca4677cSimp case 'B':
2635ca4677cSimp opt_B = optarg;
2645ca4677cSimp break;
2655ca4677cSimp case 'F':
266a47b6461Sderaadt opt_F = strtonum(optarg, 1, INT_MAX, NULL);
267a47b6461Sderaadt if (!(opt_F == 12 || opt_F == 16 || opt_F == 32))
2685ca4677cSimp errx(1, "%s: bad FAT type", optarg);
2695ca4677cSimp break;
2705ca4677cSimp case 'I':
2715ca4677cSimp opt_I = argto4(optarg, 0, "volume ID");
2725ca4677cSimp Iflag = 1;
2732700319eSderaadt break;
2742700319eSderaadt case 'L':
2755ca4677cSimp if (!oklabel(optarg))
2765ca4677cSimp errx(1, "%s: bad volume label", optarg);
2775ca4677cSimp opt_L = optarg;
2782700319eSderaadt break;
2795ca4677cSimp case 'O':
2805ca4677cSimp if (strlen(optarg) > 8)
2815ca4677cSimp errx(1, "%s: bad OEM string", optarg);
2825ca4677cSimp opt_O = optarg;
283c78524a2Sderaadt break;
2845ca4677cSimp case 'S':
2855ca4677cSimp opt_S = argto2(optarg, 1, "bytes/sector");
2865ca4677cSimp break;
2875ca4677cSimp case 'a':
2885ca4677cSimp opt_a = argto4(optarg, 1, "sectors/FAT");
2895ca4677cSimp break;
2905ca4677cSimp case 'b':
2915ca4677cSimp opt_b = argtox(optarg, 1, "block size");
2925ca4677cSimp opt_c = 0;
2935ca4677cSimp break;
2945ca4677cSimp case 'c':
2955ca4677cSimp opt_c = argto1(optarg, 1, "sectors/cluster");
2965ca4677cSimp opt_b = 0;
2975ca4677cSimp break;
2985ca4677cSimp case 'e':
2995ca4677cSimp opt_e = argto2(optarg, 1, "directory entries");
3005ca4677cSimp break;
3015ca4677cSimp case 'f':
3025ca4677cSimp opt_f = optarg;
3035ca4677cSimp break;
3045ca4677cSimp case 'h':
3055ca4677cSimp opt_h = argto2(optarg, 1, "drive heads");
3065ca4677cSimp break;
3075ca4677cSimp case 'i':
3085ca4677cSimp opt_i = argto2(optarg, 1, "info sector");
3095ca4677cSimp break;
3105ca4677cSimp case 'k':
3115ca4677cSimp opt_k = argto2(optarg, 1, "backup sector");
3125ca4677cSimp break;
3135ca4677cSimp case 'm':
3145ca4677cSimp opt_m = argto1(optarg, 0, "media descriptor");
3155ca4677cSimp mflag = 1;
3165ca4677cSimp break;
3175ca4677cSimp case 'n':
3185ca4677cSimp opt_n = argto1(optarg, 1, "number of FATs");
3195ca4677cSimp break;
3205ca4677cSimp case 'o':
3215ca4677cSimp opt_o = argto4(optarg, 0, "hidden sectors");
3225ca4677cSimp oflag = 1;
3235ca4677cSimp break;
3245ca4677cSimp case 'q': /* Compat with newfs -q */
3255ca4677cSimp break;
3265ca4677cSimp case 'r':
3275ca4677cSimp opt_r = argto2(optarg, 1, "reserved sectors");
3285ca4677cSimp break;
3295ca4677cSimp case 's':
3305ca4677cSimp opt_s = argto4(optarg, 1, "file system size");
3315ca4677cSimp break;
3325ca4677cSimp case 't': /* Compat with newfs -t */
3335ca4677cSimp break;
3345ca4677cSimp case 'u':
3355ca4677cSimp opt_u = argto2(optarg, 1, "sectors/track");
3365ca4677cSimp break;
3372700319eSderaadt default:
3382700319eSderaadt usage();
3392700319eSderaadt }
3402700319eSderaadt argc -= optind;
3412700319eSderaadt argv += optind;
3425ca4677cSimp if (argc < 1 || argc > 2)
3432700319eSderaadt usage();
3445ca4677cSimp sname = *argv++;
3455ca4677cSimp dtype = *argv;
3465ca4677cSimp if ((fd = opendev(sname, opt_N ? O_RDONLY : O_RDWR, 0, &fname)) == -1 ||
3475ca4677cSimp fstat(fd, &sb))
3485ca4677cSimp err(1, "%s", fname);
3495ca4677cSimp if (!opt_N)
3505ca4677cSimp check_mounted(fname, sb.st_mode);
3515d88db58Shalex if (S_ISBLK(sb.st_mode))
3525d88db58Shalex errx(1, "%s: block device", fname);
3535ca4677cSimp if (!S_ISCHR(sb.st_mode))
3545ca4677cSimp warnx("warning: %s is not a character device", fname);
3555ca4677cSimp memset(&bpb, 0, sizeof(bpb));
3565ca4677cSimp if (opt_f) {
3575ca4677cSimp getstdfmt(opt_f, &bpb);
3585ca4677cSimp bpb.bsec = bpb.sec;
3595ca4677cSimp bpb.sec = 0;
3605ca4677cSimp bpb.bspf = bpb.spf;
3615ca4677cSimp bpb.spf = 0;
3625ca4677cSimp }
3635ca4677cSimp if (opt_h)
3645ca4677cSimp bpb.hds = opt_h;
3655ca4677cSimp if (opt_u)
3665ca4677cSimp bpb.spt = opt_u;
3675ca4677cSimp if (opt_S)
3685ca4677cSimp bpb.bps = opt_S;
3695ca4677cSimp if (opt_s)
3705ca4677cSimp bpb.bsec = opt_s;
3715ca4677cSimp if (oflag)
3725ca4677cSimp bpb.hid = opt_o;
3735ca4677cSimp if (!(opt_f || (opt_h && opt_u && opt_S && opt_s && oflag)))
3745ca4677cSimp getdiskinfo(fd, fname, dtype, oflag, &bpb);
3755ca4677cSimp if (!powerof2(bpb.bps))
3765ca4677cSimp errx(1, "bytes/sector (%u) is not a power of 2", bpb.bps);
3775ca4677cSimp if (bpb.bps < MINBPS)
3785ca4677cSimp errx(1, "bytes/sector (%u) is too small; minimum is %u",
3795ca4677cSimp bpb.bps, MINBPS);
3805ca4677cSimp if (!(fat = opt_F)) {
3815ca4677cSimp if (opt_f)
3825ca4677cSimp fat = 12;
3835ca4677cSimp else if (!opt_e && (opt_i || opt_k))
3845ca4677cSimp fat = 32;
3855ca4677cSimp }
3865ca4677cSimp if ((fat == 32 && opt_e) || (fat != 32 && (opt_i || opt_k)))
3875ca4677cSimp errx(1, "-%c is not a legal FAT%s option",
3885ca4677cSimp fat == 32 ? 'e' : opt_i ? 'i' : 'k',
3895ca4677cSimp fat == 32 ? "32" : "12/16");
3905ca4677cSimp if (opt_f && fat == 32)
3915ca4677cSimp bpb.rde = 0;
3925ca4677cSimp if (opt_b) {
3935ca4677cSimp if (!powerof2(opt_b))
3945ca4677cSimp errx(1, "block size (%u) is not a power of 2", opt_b);
3955ca4677cSimp if (opt_b < bpb.bps)
3965ca4677cSimp errx(1, "block size (%u) is too small; minimum is %u",
3975ca4677cSimp opt_b, bpb.bps);
3985ca4677cSimp if (opt_b > bpb.bps * MAXSPC)
3995ca4677cSimp errx(1, "block size (%u) is too large; maximum is %u",
4005ca4677cSimp opt_b, bpb.bps * MAXSPC);
4015ca4677cSimp bpb.spc = opt_b / bpb.bps;
4025ca4677cSimp }
4035ca4677cSimp if (opt_c) {
4045ca4677cSimp if (!powerof2(opt_c))
4055ca4677cSimp errx(1, "sectors/cluster (%u) is not a power of 2", opt_c);
4065ca4677cSimp bpb.spc = opt_c;
4075ca4677cSimp }
4085ca4677cSimp if (opt_r)
4095ca4677cSimp bpb.res = opt_r;
4105ca4677cSimp if (opt_n) {
4115ca4677cSimp if (opt_n > MAXNFT)
4125ca4677cSimp errx(1, "number of FATs (%u) is too large; maximum is %u",
4135ca4677cSimp opt_n, MAXNFT);
4145ca4677cSimp bpb.nft = opt_n;
4155ca4677cSimp }
4165ca4677cSimp if (opt_e)
4175ca4677cSimp bpb.rde = opt_e;
4185ca4677cSimp if (mflag) {
4195ca4677cSimp if (opt_m < 0xf0)
4205ca4677cSimp errx(1, "illegal media descriptor (%#x)", opt_m);
4215ca4677cSimp bpb.mid = opt_m;
4225ca4677cSimp }
4235ca4677cSimp if (opt_a)
4245ca4677cSimp bpb.bspf = opt_a;
4255ca4677cSimp if (opt_i)
4265ca4677cSimp bpb.infs = opt_i;
4275ca4677cSimp if (opt_k)
4285ca4677cSimp bpb.bkbs = opt_k;
4295ca4677cSimp bss = 1;
4305ca4677cSimp bname = NULL;
4315ca4677cSimp fd1 = -1;
4325ca4677cSimp if (opt_B) {
4335ca4677cSimp bname = opt_B;
4345ca4677cSimp if (!strchr(bname, '/')) {
4355ca4677cSimp snprintf(buf, sizeof(buf), "/boot/%s", bname);
4365ca4677cSimp if (!(bname = strdup(buf)))
4375ca4677cSimp err(1, NULL);
4385ca4677cSimp }
4395ca4677cSimp if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb))
4405ca4677cSimp err(1, "%s", bname);
4415ca4677cSimp if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||
4425ca4677cSimp sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16)
4435ca4677cSimp errx(1, "%s: inappropriate file type or format", bname);
4445ca4677cSimp bss = sb.st_size / bpb.bps;
4455ca4677cSimp }
4465ca4677cSimp if (!bpb.nft)
4475ca4677cSimp bpb.nft = 2;
4485ca4677cSimp if (!fat) {
4495ca4677cSimp if (bpb.bsec < (bpb.res ? bpb.res : bss) +
4505ca4677cSimp howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *
4515ca4677cSimp ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *
4525ca4677cSimp bpb.nft +
4535ca4677cSimp howmany(bpb.rde ? bpb.rde : DEFRDE,
4545ca4677cSimp bpb.bps / sizeof(struct de)) +
4555ca4677cSimp (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *
4565ca4677cSimp (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))
4575ca4677cSimp fat = 12;
4585ca4677cSimp else if (bpb.rde || bpb.bsec <
4595ca4677cSimp (bpb.res ? bpb.res : bss) +
4605ca4677cSimp howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +
4615ca4677cSimp howmany(DEFRDE, bpb.bps / sizeof(struct de)) +
4625ca4677cSimp (MAXCLS16 + 1) *
4635ca4677cSimp (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))
4645ca4677cSimp fat = 16;
4655ca4677cSimp else
4665ca4677cSimp fat = 32;
4675ca4677cSimp }
4685ca4677cSimp x = bss;
4695ca4677cSimp if (fat == 32) {
4705ca4677cSimp if (!bpb.infs) {
4715ca4677cSimp if (x == MAXU16 || x == bpb.bkbs)
4725ca4677cSimp errx(1, "no room for info sector");
4735ca4677cSimp bpb.infs = x;
4745ca4677cSimp }
4755ca4677cSimp if (bpb.infs != MAXU16 && x <= bpb.infs)
4765ca4677cSimp x = bpb.infs + 1;
4775ca4677cSimp if (!bpb.bkbs) {
4785ca4677cSimp if (x == MAXU16)
4795ca4677cSimp errx(1, "no room for backup sector");
4805ca4677cSimp bpb.bkbs = x;
4815ca4677cSimp } else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs)
4825ca4677cSimp errx(1, "backup sector would overwrite info sector");
4835ca4677cSimp if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)
4845ca4677cSimp x = bpb.bkbs + 1;
4855ca4677cSimp }
4865ca4677cSimp if (!bpb.res)
487b9fc9a72Sderaadt bpb.res = fat == 32 ? MAXIMUM(x, MAXIMUM(16384 / bpb.bps, 4)) : x;
4885ca4677cSimp else if (bpb.res < x)
4895ca4677cSimp errx(1, "too few reserved sectors");
4905ca4677cSimp if (fat != 32 && !bpb.rde)
4915ca4677cSimp bpb.rde = DEFRDE;
4925ca4677cSimp rds = howmany(bpb.rde, bpb.bps / sizeof(struct de));
4935ca4677cSimp if (!bpb.spc)
4945ca4677cSimp for (bpb.spc = howmany(fat == 16 ? DEFBLK16 : DEFBLK, bpb.bps);
4955ca4677cSimp bpb.spc < MAXSPC &&
4965ca4677cSimp bpb.res +
4975ca4677cSimp howmany((RESFTE + maxcls(fat)) * (fat / BPN),
4985ca4677cSimp bpb.bps * NPB) * bpb.nft +
4995ca4677cSimp rds +
5005ca4677cSimp (u_int64_t)(maxcls(fat) + 1) * bpb.spc <= bpb.bsec;
5015ca4677cSimp bpb.spc <<= 1);
5025ca4677cSimp if (fat != 32 && bpb.bspf > MAXU16)
5035ca4677cSimp errx(1, "too many sectors/FAT for FAT12/16");
5045ca4677cSimp x1 = bpb.res + rds;
5055ca4677cSimp x = bpb.bspf ? bpb.bspf : 1;
5065ca4677cSimp if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec)
5075ca4677cSimp errx(1, "meta data exceeds file system size");
5085ca4677cSimp x1 += x * bpb.nft;
5095ca4677cSimp x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /
5105ca4677cSimp (bpb.spc * bpb.bps * NPB + fat / BPN * bpb.nft);
511b9fc9a72Sderaadt x2 = howmany((RESFTE + MINIMUM(x, maxcls(fat))) * (fat / BPN),
5125ca4677cSimp bpb.bps * NPB);
5135ca4677cSimp if (!bpb.bspf) {
5145ca4677cSimp bpb.bspf = x2;
5155ca4677cSimp x1 += (bpb.bspf - 1) * bpb.nft;
5165ca4677cSimp }
5175ca4677cSimp cls = (bpb.bsec - x1) / bpb.spc;
5185ca4677cSimp x = (u_int64_t)bpb.bspf * bpb.bps * NPB / (fat / BPN) - RESFTE;
5195ca4677cSimp if (cls > x)
5205ca4677cSimp cls = x;
5215ca4677cSimp if (bpb.bspf < x2)
5225ca4677cSimp warnx("warning: sectors/FAT limits file system to %u clusters",
5235ca4677cSimp cls);
5245ca4677cSimp if (cls < mincls(fat))
5255ca4677cSimp errx(1, "%u clusters too few clusters for FAT%u, need %u", cls, fat,
5265ca4677cSimp mincls(fat));
5275ca4677cSimp if (cls > maxcls(fat)) {
5285ca4677cSimp cls = maxcls(fat);
5295ca4677cSimp bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;
5305ca4677cSimp warnx("warning: FAT type limits file system to %u sectors",
5315ca4677cSimp bpb.bsec);
5325ca4677cSimp }
5335ca4677cSimp printf("%s: %u sector%s in %u FAT%u cluster%s "
5345ca4677cSimp "(%u bytes/cluster)\n", fname, cls * bpb.spc,
5355ca4677cSimp cls * bpb.spc == 1 ? "" : "s", cls, fat,
5365ca4677cSimp cls == 1 ? "" : "s", bpb.bps * bpb.spc);
5375ca4677cSimp if (!bpb.mid)
5385ca4677cSimp bpb.mid = !bpb.hid ? 0xf0 : 0xf8;
5395ca4677cSimp if (fat == 32)
5405ca4677cSimp bpb.rdcl = RESFTE;
5415ca4677cSimp if (bpb.hid + bpb.bsec <= MAXU16) {
5425ca4677cSimp bpb.sec = bpb.bsec;
5435ca4677cSimp bpb.bsec = 0;
5445ca4677cSimp }
5455ca4677cSimp if (fat != 32) {
5465ca4677cSimp bpb.spf = bpb.bspf;
5475ca4677cSimp bpb.bspf = 0;
5485ca4677cSimp }
5495ca4677cSimp print_bpb(&bpb);
5505ca4677cSimp if (!opt_N) {
5515ca4677cSimp gettimeofday(&tv, NULL);
5525ca4677cSimp now = tv.tv_sec;
553*b205d946Sflorian if ((tm = localtime(&now)) == NULL)
554*b205d946Sflorian errx(1, "Invalid time");
555*b205d946Sflorian
5565ca4677cSimp if (!(img = malloc(bpb.bps)))
5575ca4677cSimp err(1, NULL);
5585ca4677cSimp dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
5595ca4677cSimp for (lsn = 0; lsn < dir + (fat == 32 ? bpb.spc : rds); lsn++) {
5605ca4677cSimp x = lsn;
5615ca4677cSimp if (opt_B &&
5625ca4677cSimp fat == 32 && bpb.bkbs != MAXU16 &&
5635ca4677cSimp bss <= bpb.bkbs && x >= bpb.bkbs) {
5645ca4677cSimp x -= bpb.bkbs;
5655ca4677cSimp if (!x && lseek(fd1, 0, SEEK_SET))
5665ca4677cSimp err(1, "%s", bname);
5675ca4677cSimp }
5685ca4677cSimp if (opt_B && x < bss) {
5695ca4677cSimp if ((n = read(fd1, img, bpb.bps)) == -1)
5705ca4677cSimp err(1, "%s", bname);
5715ca4677cSimp if (n != bpb.bps)
5725ca4677cSimp errx(1, "%s: can't read sector %u", bname, x);
5735ca4677cSimp } else
5745ca4677cSimp memset(img, 0, bpb.bps);
5755ca4677cSimp if (!lsn ||
5765ca4677cSimp (fat == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {
5775ca4677cSimp x1 = sizeof(struct bs);
5785ca4677cSimp bsbpb = (struct bsbpb *)(img + x1);
5795ca4677cSimp mk2(bsbpb->bps, bpb.bps);
5805ca4677cSimp mk1(bsbpb->spc, bpb.spc);
5815ca4677cSimp mk2(bsbpb->res, bpb.res);
5825ca4677cSimp mk1(bsbpb->nft, bpb.nft);
5835ca4677cSimp mk2(bsbpb->rde, bpb.rde);
5845ca4677cSimp mk2(bsbpb->sec, bpb.sec);
5855ca4677cSimp mk1(bsbpb->mid, bpb.mid);
5865ca4677cSimp mk2(bsbpb->spf, bpb.spf);
5875ca4677cSimp mk2(bsbpb->spt, bpb.spt);
5885ca4677cSimp mk2(bsbpb->hds, bpb.hds);
5895ca4677cSimp mk4(bsbpb->hid, bpb.hid);
5905ca4677cSimp mk4(bsbpb->bsec, bpb.bsec);
5915ca4677cSimp x1 += sizeof(struct bsbpb);
5925ca4677cSimp if (fat == 32) {
5935ca4677cSimp bsxbpb = (struct bsxbpb *)(img + x1);
5945ca4677cSimp mk4(bsxbpb->bspf, bpb.bspf);
5955ca4677cSimp mk2(bsxbpb->xflg, 0);
5965ca4677cSimp mk2(bsxbpb->vers, 0);
5975ca4677cSimp mk4(bsxbpb->rdcl, bpb.rdcl);
5985ca4677cSimp mk2(bsxbpb->infs, bpb.infs);
5995ca4677cSimp mk2(bsxbpb->bkbs, bpb.bkbs);
6005ca4677cSimp x1 += sizeof(struct bsxbpb);
6015ca4677cSimp }
6025ca4677cSimp bsx = (struct bsx *)(img + x1);
6035ca4677cSimp mk1(bsx->sig, 0x29);
6045ca4677cSimp if (Iflag)
6055ca4677cSimp x = opt_I;
6065ca4677cSimp else
6075ca4677cSimp x = (((u_int)(1 + tm->tm_mon) << 8 |
6085ca4677cSimp (u_int)tm->tm_mday) +
6095ca4677cSimp ((u_int)tm->tm_sec << 8 |
6105ca4677cSimp (u_int)(tv.tv_usec / 10))) << 16 |
6115ca4677cSimp ((u_int)(1900 + tm->tm_year) +
6125ca4677cSimp ((u_int)tm->tm_hour << 8 |
6135ca4677cSimp (u_int)tm->tm_min));
6145ca4677cSimp mk4(bsx->volid, x);
6155ca4677cSimp mklabel(bsx->label, opt_L ? opt_L : "NO NAME");
6169bad0b17Sderaadt snprintf(buf, sizeof buf, "FAT%u", fat);
6175ca4677cSimp setstr(bsx->type, buf, sizeof(bsx->type));
6185ca4677cSimp if (!opt_B) {
6195ca4677cSimp x1 += sizeof(struct bsx);
6205ca4677cSimp bs = (struct bs *)img;
6215ca4677cSimp mk1(bs->jmp[0], 0xeb);
6225ca4677cSimp mk1(bs->jmp[1], x1 - 2);
6235ca4677cSimp mk1(bs->jmp[2], 0x90);
6245ca4677cSimp setstr(bs->oem, opt_O ? opt_O : "BSD 4.4",
6255ca4677cSimp sizeof(bs->oem));
6265ca4677cSimp memcpy(img + x1, bootcode, sizeof(bootcode));
6279c69801eStobias mk2(img + MINBPS - 2, DOSMAGIC);
6285ca4677cSimp }
6295ca4677cSimp } else if (fat == 32 && bpb.infs != MAXU16 &&
6305ca4677cSimp (lsn == bpb.infs ||
6315ca4677cSimp (bpb.bkbs != MAXU16 &&
6325ca4677cSimp lsn == bpb.bkbs + bpb.infs))) {
6335ca4677cSimp mk4(img, 0x41615252);
6349c69801eStobias mk4(img + MINBPS - 28, 0x61417272);
6359c69801eStobias mk4(img + MINBPS - 24, 0xffffffff);
6360f42ffa9Stobias mk4(img + MINBPS - 20, 0xffffffff);
6379c69801eStobias mk2(img + MINBPS - 2, DOSMAGIC);
6385ca4677cSimp } else if (lsn >= bpb.res && lsn < dir &&
6395ca4677cSimp !((lsn - bpb.res) %
6405ca4677cSimp (bpb.spf ? bpb.spf : bpb.bspf))) {
6415ca4677cSimp mk1(img[0], bpb.mid);
6425ca4677cSimp for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
6435ca4677cSimp mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
6445ca4677cSimp } else if (lsn == dir && opt_L) {
6455ca4677cSimp de = (struct de *)img;
6465ca4677cSimp mklabel(de->namext, opt_L);
6475ca4677cSimp mk1(de->attr, 050);
6485ca4677cSimp x = (u_int)tm->tm_hour << 11 |
6495ca4677cSimp (u_int)tm->tm_min << 5 |
6505ca4677cSimp (u_int)tm->tm_sec >> 1;
6515ca4677cSimp mk2(de->time, x);
6525ca4677cSimp x = (u_int)(tm->tm_year - 80) << 9 |
6535ca4677cSimp (u_int)(tm->tm_mon + 1) << 5 |
6545ca4677cSimp (u_int)tm->tm_mday;
6555ca4677cSimp mk2(de->date, x);
6565ca4677cSimp }
6575ca4677cSimp if ((n = write(fd, img, bpb.bps)) == -1)
6585ca4677cSimp err(1, "%s", fname);
6595ca4677cSimp if (n != bpb.bps)
6605ca4677cSimp errx(1, "%s: can't write sector %u", fname, lsn);
6615ca4677cSimp }
6625ca4677cSimp }
6635ca4677cSimp return 0;
6645ca4677cSimp }
665f0faf921Sderaadt
6662700319eSderaadt /*
6675ca4677cSimp * Exit with error if file system is mounted.
6682700319eSderaadt */
6695ca4677cSimp static void
check_mounted(const char * fname,mode_t mode)6705ca4677cSimp check_mounted(const char *fname, mode_t mode)
6715ca4677cSimp {
6725ca4677cSimp struct statfs *mp;
6735ca4677cSimp const char *s1, *s2;
6745ca4677cSimp size_t len;
6755ca4677cSimp int n, r;
676f0faf921Sderaadt
6775ca4677cSimp if (!(n = getmntinfo(&mp, MNT_NOWAIT)))
6785ca4677cSimp err(1, "getmntinfo");
6793332ec12Sfgsch len = sizeof(_PATH_DEV) - 1;
6805ca4677cSimp s1 = fname;
6815ca4677cSimp if (!strncmp(s1, _PATH_DEV, len))
6825ca4677cSimp s1 += len;
6835ca4677cSimp r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
6845ca4677cSimp for (; n--; mp++) {
6855ca4677cSimp s2 = mp->f_mntfromname;
6865ca4677cSimp if (!strncmp(s2, _PATH_DEV, len))
6875ca4677cSimp s2 += len;
6885ca4677cSimp if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
6895ca4677cSimp !strcmp(s1, s2))
6905ca4677cSimp errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
6912700319eSderaadt }
6925ca4677cSimp }
6932700319eSderaadt
6945ca4677cSimp /*
6955ca4677cSimp * Get a standard format.
6965ca4677cSimp */
6975ca4677cSimp static void
getstdfmt(const char * fmt,struct bpb * bpb)6985ca4677cSimp getstdfmt(const char *fmt, struct bpb *bpb)
6995ca4677cSimp {
7005ca4677cSimp u_int x, i;
7012700319eSderaadt
7025ca4677cSimp x = sizeof(stdfmt) / sizeof(stdfmt[0]);
7035ca4677cSimp for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
7045ca4677cSimp if (i == x)
7055ca4677cSimp errx(1, "%s: unknown standard format", fmt);
7065ca4677cSimp *bpb = stdfmt[i].bpb;
7075ca4677cSimp }
7082700319eSderaadt
7095ca4677cSimp /*
7105ca4677cSimp * Get disk slice, partition, and geometry information.
7115ca4677cSimp */
7125ca4677cSimp static void
getdiskinfo(int fd,const char * fname,const char * dtype,int oflag,struct bpb * bpb)7135ca4677cSimp getdiskinfo(int fd, const char *fname, const char *dtype, int oflag,
7145ca4677cSimp struct bpb *bpb)
7155ca4677cSimp {
7165ca4677cSimp struct disklabel dl, *lp;
7175ca4677cSimp const char *s1, *s2;
718a4df0321Sderaadt int part, i;
7192700319eSderaadt
7205ca4677cSimp part = -1;
7215ca4677cSimp s1 = fname;
7225ca4677cSimp if ((s2 = strrchr(s1, '/')))
7235ca4677cSimp s1 = s2 + 1;
724025f5691Sderaadt for (s2 = s1; *s2 && !isdigit((unsigned char)*s2); s2++);
7255ca4677cSimp if (!*s2 || s2 == s1)
7265ca4677cSimp s2 = NULL;
7272700319eSderaadt else
728025f5691Sderaadt while (isdigit((unsigned char)*++s2));
7295ca4677cSimp s1 = s2;
7305ca4677cSimp if (s2 && *s2 >= 'a' && *s2 <= 'a' + MAXPARTITIONS - 1) {
7315ca4677cSimp part = *s2++ - 'a';
7325ca4677cSimp }
7335ca4677cSimp if (!s2 || (*s2 && *s2 != '.'))
7345ca4677cSimp errx(1, "%s: can't figure out partition info", fname);
7355ca4677cSimp if ((((!oflag && part != -1) || !bpb->bsec)) ||
7365ca4677cSimp !bpb->bps || !bpb->spt || !bpb->hds) {
7375ca4677cSimp lp = &dl;
7385ca4677cSimp i = ioctl(fd, DIOCGDINFO, lp);
7395ca4677cSimp if (i == -1) {
7405ca4677cSimp if (!dtype) {
7415ca4677cSimp warn("ioctl (GDINFO)");
7425ca4677cSimp errx(1, "%s: can't read disk label; "
7435ca4677cSimp "disk type must be specified", fname);
7445ca4677cSimp } else if (!(lp = getdiskbyname(dtype)))
7455ca4677cSimp errx(1, "%s: unknown disk type", dtype);
7465ca4677cSimp }
7475ca4677cSimp if (part == -1)
7485ca4677cSimp part = RAW_PART;
7495ca4677cSimp if (part >= lp->d_npartitions ||
750862ef960Skrw !DL_GETPSIZE(&lp->d_partitions[part]))
7515ca4677cSimp errx(1, "%s: partition is unavailable", fname);
7525ca4677cSimp if (!oflag && part != -1)
753862ef960Skrw bpb->hid += DL_GETPOFFSET(&lp->d_partitions[part]);
7545ca4677cSimp if (!bpb->bsec)
755862ef960Skrw bpb->bsec = DL_GETPSIZE(&lp->d_partitions[part]);
7565ca4677cSimp if (!bpb->bps)
7575ca4677cSimp bpb->bps = ckgeom(fname, lp->d_secsize, "bytes/sector");
7585ca4677cSimp if (!bpb->spt)
7595ca4677cSimp bpb->spt = ckgeom(fname, lp->d_nsectors, "sectors/track");
7605ca4677cSimp if (!bpb->hds)
7615ca4677cSimp bpb->hds = ckgeom(fname, lp->d_ntracks, "drive heads");
7625ca4677cSimp if (bpb->spt > 63) {
7635ca4677cSimp bpb->hds = bpb->hds * bpb->spt / 63;
7645ca4677cSimp bpb->spt = 63;
7655ca4677cSimp }
7665ca4677cSimp }
7675ca4677cSimp }
7682700319eSderaadt
7695ca4677cSimp /*
7705ca4677cSimp * Print out BPB values.
7715ca4677cSimp */
7725ca4677cSimp static void
print_bpb(struct bpb * bpb)7735ca4677cSimp print_bpb(struct bpb *bpb)
7745ca4677cSimp {
7755ca4677cSimp printf("bps=%u spc=%u res=%u nft=%u", bpb->bps, bpb->spc, bpb->res,
7765ca4677cSimp bpb->nft);
7775ca4677cSimp if (bpb->rde)
7785ca4677cSimp printf(" rde=%u", bpb->rde);
7795ca4677cSimp if (bpb->sec)
7805ca4677cSimp printf(" sec=%u", bpb->sec);
7815ca4677cSimp printf(" mid=%#x", bpb->mid);
7825ca4677cSimp if (bpb->spf)
7835ca4677cSimp printf(" spf=%u", bpb->spf);
7845ca4677cSimp printf(" spt=%u hds=%u hid=%u", bpb->spt, bpb->hds, bpb->hid);
7855ca4677cSimp if (bpb->bsec)
7865ca4677cSimp printf(" bsec=%u", bpb->bsec);
7875ca4677cSimp if (!bpb->spf) {
7885ca4677cSimp printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);
7895ca4677cSimp printf(" infs=");
7905ca4677cSimp printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);
7915ca4677cSimp printf(" bkbs=");
7925ca4677cSimp printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);
7935ca4677cSimp }
7945ca4677cSimp printf("\n");
7955ca4677cSimp }
7962700319eSderaadt
7975ca4677cSimp /*
7985ca4677cSimp * Check a disk geometry value.
7995ca4677cSimp */
8005ca4677cSimp static u_int
ckgeom(const char * fname,u_int val,const char * msg)8015ca4677cSimp ckgeom(const char *fname, u_int val, const char *msg)
8025ca4677cSimp {
8035ca4677cSimp if (!val)
8045ca4677cSimp errx(1, "%s: no default %s", fname, msg);
8055ca4677cSimp if (val > MAXU16)
8065ca4677cSimp errx(1, "%s: illegal %s", fname, msg);
8075ca4677cSimp return val;
8085ca4677cSimp }
8095ca4677cSimp
8105ca4677cSimp /*
8115ca4677cSimp * Convert and check a numeric option argument.
8125ca4677cSimp */
8135ca4677cSimp static u_int
argtou(const char * arg,u_int lo,u_int hi,const char * msg)8145ca4677cSimp argtou(const char *arg, u_int lo, u_int hi, const char *msg)
8155ca4677cSimp {
8165ca4677cSimp char *s;
8175ca4677cSimp u_long x;
8185ca4677cSimp
8195ca4677cSimp errno = 0;
8205ca4677cSimp x = strtoul(arg, &s, 0);
8215ca4677cSimp if (errno || !*arg || *s || x < lo || x > hi)
8225ca4677cSimp errx(1, "%s: bad %s", arg, msg);
8235ca4677cSimp return x;
8245ca4677cSimp }
8255ca4677cSimp
8265ca4677cSimp /*
8275ca4677cSimp * Check a volume label.
8285ca4677cSimp */
8295ca4677cSimp static int
oklabel(const char * src)8305ca4677cSimp oklabel(const char *src)
8315ca4677cSimp {
832a4df0321Sderaadt int c = 0, i;
8335ca4677cSimp
8345ca4677cSimp for (i = 0; i <= 11; i++) {
8355ca4677cSimp c = (u_char)*src++;
8365ca4677cSimp if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
8375ca4677cSimp break;
8385ca4677cSimp }
8395ca4677cSimp return i && !c;
8405ca4677cSimp }
8415ca4677cSimp
8425ca4677cSimp /*
8435ca4677cSimp * Make a volume label.
8445ca4677cSimp */
8455ca4677cSimp static void
mklabel(u_int8_t * dest,const char * src)8465ca4677cSimp mklabel(u_int8_t *dest, const char *src)
8475ca4677cSimp {
8485ca4677cSimp int c, i;
8495ca4677cSimp
8505ca4677cSimp for (i = 0; i < 11; i++) {
851025f5691Sderaadt c = *src ? toupper((unsigned char)*src++) : ' ';
8525ca4677cSimp *dest++ = !i && c == '\xe5' ? 5 : c;
8535ca4677cSimp }
8545ca4677cSimp }
8555ca4677cSimp
8565ca4677cSimp /*
8575ca4677cSimp * Copy string, padding with spaces.
8585ca4677cSimp */
8595ca4677cSimp static void
setstr(u_int8_t * dest,const char * src,size_t len)8605ca4677cSimp setstr(u_int8_t *dest, const char *src, size_t len)
8615ca4677cSimp {
8625ca4677cSimp while (len--)
8635ca4677cSimp *dest++ = *src ? *src++ : ' ';
8645ca4677cSimp }
8655ca4677cSimp
8665ca4677cSimp /*
8675ca4677cSimp * Print usage message.
8685ca4677cSimp */
869b1d95c40Spyr static __dead void
usage(void)8705ca4677cSimp usage(void)
8715ca4677cSimp {
872b1d95c40Spyr extern const char *__progname;
873b1d95c40Spyr
874b1d95c40Spyr fprintf(stderr, "usage: %s "
8757d27bc2dSjmc "[-N] [-a FAT-size] [-B boot] [-b block-size]\n"
8767d27bc2dSjmc "\t[-c cluster-size] [-e dirents] [-F FAT-type] [-f format]\n"
8777d27bc2dSjmc "\t[-h heads] [-I volid] [-i info] [-k backup] [-L label]\n"
8787d27bc2dSjmc "\t[-m media] [-n FATs] [-O OEM] [-o hidden] [-r reserved]\n"
8797d27bc2dSjmc "\t[-S sector-size] [-s total] [-u track-size] special\n"
8807d27bc2dSjmc "\t[disktype]\n",
881b1d95c40Spyr __progname);
8825ca4677cSimp exit(1);
8832700319eSderaadt }
884