1*25d017aaSSevan Janiyan /* $NetBSD: mkfs_msdos.c,v 1.13 2017/04/14 15:39:29 christos Exp $ */
29f988b79SJean-Baptiste Boric
39f988b79SJean-Baptiste Boric /*
49f988b79SJean-Baptiste Boric * Copyright (c) 1998 Robert Nordier
59f988b79SJean-Baptiste Boric * All rights reserved.
69f988b79SJean-Baptiste Boric *
79f988b79SJean-Baptiste Boric * Redistribution and use in source and binary forms, with or without
89f988b79SJean-Baptiste Boric * modification, are permitted provided that the following conditions
99f988b79SJean-Baptiste Boric * are met:
109f988b79SJean-Baptiste Boric * 1. Redistributions of source code must retain the above copyright
119f988b79SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer.
129f988b79SJean-Baptiste Boric * 2. Redistributions in binary form must reproduce the above copyright
139f988b79SJean-Baptiste Boric * notice, this list of conditions and the following disclaimer in
149f988b79SJean-Baptiste Boric * the documentation and/or other materials provided with the
159f988b79SJean-Baptiste Boric * distribution.
169f988b79SJean-Baptiste Boric *
179f988b79SJean-Baptiste Boric * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
189f988b79SJean-Baptiste Boric * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
199f988b79SJean-Baptiste Boric * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
209f988b79SJean-Baptiste Boric * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
219f988b79SJean-Baptiste Boric * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
229f988b79SJean-Baptiste Boric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
239f988b79SJean-Baptiste Boric * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
249f988b79SJean-Baptiste Boric * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
259f988b79SJean-Baptiste Boric * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
269f988b79SJean-Baptiste Boric * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
279f988b79SJean-Baptiste Boric * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
289f988b79SJean-Baptiste Boric */
299f988b79SJean-Baptiste Boric
309f988b79SJean-Baptiste Boric #if HAVE_NBTOOL_CONFIG_H
319f988b79SJean-Baptiste Boric #include "nbtool_config.h"
329f988b79SJean-Baptiste Boric #endif
339f988b79SJean-Baptiste Boric
349f988b79SJean-Baptiste Boric #include <sys/cdefs.h>
359f988b79SJean-Baptiste Boric #ifndef lint
369f988b79SJean-Baptiste Boric #if 0
379f988b79SJean-Baptiste Boric static const char rcsid[] =
389f988b79SJean-Baptiste Boric "$FreeBSD: src/sbin/newfs_msdos/newfs_msdos.c,v 1.15 2000/10/10 01:49:37 wollman Exp $";
399f988b79SJean-Baptiste Boric #else
40*25d017aaSSevan Janiyan __RCSID("$NetBSD: mkfs_msdos.c,v 1.13 2017/04/14 15:39:29 christos Exp $");
419f988b79SJean-Baptiste Boric #endif
429f988b79SJean-Baptiste Boric #endif /* not lint */
439f988b79SJean-Baptiste Boric
449f988b79SJean-Baptiste Boric #include <sys/types.h>
459f988b79SJean-Baptiste Boric #include <sys/param.h>
469f988b79SJean-Baptiste Boric #include <sys/stat.h>
479f988b79SJean-Baptiste Boric #include <sys/time.h>
489f988b79SJean-Baptiste Boric #ifndef MAKEFS
499f988b79SJean-Baptiste Boric #include <sys/mount.h>
509f988b79SJean-Baptiste Boric #include <sys/disk.h>
519f988b79SJean-Baptiste Boric #endif
529f988b79SJean-Baptiste Boric
539f988b79SJean-Baptiste Boric #include <ctype.h>
549f988b79SJean-Baptiste Boric #include <err.h>
559f988b79SJean-Baptiste Boric #include <errno.h>
569f988b79SJean-Baptiste Boric #include <fcntl.h>
579f988b79SJean-Baptiste Boric #include <paths.h>
589f988b79SJean-Baptiste Boric #include <stdio.h>
599f988b79SJean-Baptiste Boric #include <stdlib.h>
609f988b79SJean-Baptiste Boric #include <string.h>
619f988b79SJean-Baptiste Boric #include <time.h>
629f988b79SJean-Baptiste Boric #include <unistd.h>
639f988b79SJean-Baptiste Boric #include <signal.h>
649f988b79SJean-Baptiste Boric
659f988b79SJean-Baptiste Boric #include <util.h>
669f988b79SJean-Baptiste Boric #include <disktab.h>
679f988b79SJean-Baptiste Boric
689f988b79SJean-Baptiste Boric #ifndef MAKEFS
699f988b79SJean-Baptiste Boric #include "partutil.h"
709f988b79SJean-Baptiste Boric #endif
719f988b79SJean-Baptiste Boric #include "mkfs_msdos.h"
729f988b79SJean-Baptiste Boric
739f988b79SJean-Baptiste Boric #define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
749f988b79SJean-Baptiste Boric #define BPN 4 /* bits per nibble */
759f988b79SJean-Baptiste Boric #define NPB 2 /* nibbles per byte */
769f988b79SJean-Baptiste Boric
779f988b79SJean-Baptiste Boric #define DOSMAGIC 0xaa55 /* DOS magic number */
789f988b79SJean-Baptiste Boric #define MINBPS 512 /* minimum bytes per sector */
799f988b79SJean-Baptiste Boric #define MAXSPC 128 /* maximum sectors per cluster */
809f988b79SJean-Baptiste Boric #define MAXNFT 16 /* maximum number of FATs */
819f988b79SJean-Baptiste Boric #define DEFBLK 4096 /* default block size */
829f988b79SJean-Baptiste Boric #define DEFBLK16 2048 /* default block size FAT16 */
839f988b79SJean-Baptiste Boric #define DEFRDE 512 /* default root directory entries */
849f988b79SJean-Baptiste Boric #define RESFTE 2 /* reserved FAT entries */
859f988b79SJean-Baptiste Boric #define MINCLS12 1 /* minimum FAT12 clusters */
869f988b79SJean-Baptiste Boric #define MINCLS16 0xff5 /* minimum FAT16 clusters */
879f988b79SJean-Baptiste Boric #define MINCLS32 0xfff5 /* minimum FAT32 clusters */
889f988b79SJean-Baptiste Boric #define MAXCLS12 0xff4 /* maximum FAT12 clusters */
899f988b79SJean-Baptiste Boric #define MAXCLS16 0xfff4 /* maximum FAT16 clusters */
909f988b79SJean-Baptiste Boric #define MAXCLS32 0xffffff4 /* maximum FAT32 clusters */
919f988b79SJean-Baptiste Boric
929f988b79SJean-Baptiste Boric #define mincls(fat_type) ((fat_type) == 12 ? MINCLS12 : \
939f988b79SJean-Baptiste Boric (fat_type) == 16 ? MINCLS16 : \
949f988b79SJean-Baptiste Boric MINCLS32)
959f988b79SJean-Baptiste Boric
969f988b79SJean-Baptiste Boric #define maxcls(fat_type) ((fat_type) == 12 ? MAXCLS12 : \
979f988b79SJean-Baptiste Boric (fat_type) == 16 ? MAXCLS16 : \
989f988b79SJean-Baptiste Boric MAXCLS32)
999f988b79SJean-Baptiste Boric
1009f988b79SJean-Baptiste Boric #define mk1(p, x) \
1019f988b79SJean-Baptiste Boric (p) = (u_int8_t)(x)
1029f988b79SJean-Baptiste Boric
1039f988b79SJean-Baptiste Boric #define mk2(p, x) \
1049f988b79SJean-Baptiste Boric (p)[0] = (u_int8_t)(x), \
1059f988b79SJean-Baptiste Boric (p)[1] = (u_int8_t)((x) >> 010)
1069f988b79SJean-Baptiste Boric
1079f988b79SJean-Baptiste Boric #define mk4(p, x) \
1089f988b79SJean-Baptiste Boric (p)[0] = (u_int8_t)(x), \
1099f988b79SJean-Baptiste Boric (p)[1] = (u_int8_t)((x) >> 010), \
1109f988b79SJean-Baptiste Boric (p)[2] = (u_int8_t)((x) >> 020), \
1119f988b79SJean-Baptiste Boric (p)[3] = (u_int8_t)((x) >> 030)
1129f988b79SJean-Baptiste Boric
1139f988b79SJean-Baptiste Boric struct bs {
1149f988b79SJean-Baptiste Boric u_int8_t jmp[3]; /* bootstrap entry point */
1159f988b79SJean-Baptiste Boric u_int8_t oem[8]; /* OEM name and version */
1169f988b79SJean-Baptiste Boric };
1179f988b79SJean-Baptiste Boric
1189f988b79SJean-Baptiste Boric struct bsbpb {
1199f988b79SJean-Baptiste Boric u_int8_t bps[2]; /* bytes per sector */
1209f988b79SJean-Baptiste Boric u_int8_t spc; /* sectors per cluster */
1219f988b79SJean-Baptiste Boric u_int8_t res[2]; /* reserved sectors */
1229f988b79SJean-Baptiste Boric u_int8_t nft; /* number of FATs */
1239f988b79SJean-Baptiste Boric u_int8_t rde[2]; /* root directory entries */
1249f988b79SJean-Baptiste Boric u_int8_t sec[2]; /* total sectors */
1259f988b79SJean-Baptiste Boric u_int8_t mid; /* media descriptor */
1269f988b79SJean-Baptiste Boric u_int8_t spf[2]; /* sectors per FAT */
1279f988b79SJean-Baptiste Boric u_int8_t spt[2]; /* sectors per track */
1289f988b79SJean-Baptiste Boric u_int8_t hds[2]; /* drive heads */
1299f988b79SJean-Baptiste Boric u_int8_t hid[4]; /* hidden sectors */
1309f988b79SJean-Baptiste Boric u_int8_t bsec[4]; /* big total sectors */
1319f988b79SJean-Baptiste Boric };
1329f988b79SJean-Baptiste Boric
1339f988b79SJean-Baptiste Boric struct bsxbpb {
1349f988b79SJean-Baptiste Boric u_int8_t bspf[4]; /* big sectors per FAT */
1359f988b79SJean-Baptiste Boric u_int8_t xflg[2]; /* FAT control flags */
1369f988b79SJean-Baptiste Boric u_int8_t vers[2]; /* file system version */
1379f988b79SJean-Baptiste Boric u_int8_t rdcl[4]; /* root directory start cluster */
1389f988b79SJean-Baptiste Boric u_int8_t infs[2]; /* file system info sector */
1399f988b79SJean-Baptiste Boric u_int8_t bkbs[2]; /* backup boot sector */
1409f988b79SJean-Baptiste Boric u_int8_t rsvd[12]; /* reserved */
1419f988b79SJean-Baptiste Boric };
1429f988b79SJean-Baptiste Boric
1439f988b79SJean-Baptiste Boric struct bsx {
1449f988b79SJean-Baptiste Boric u_int8_t drv; /* drive number */
1459f988b79SJean-Baptiste Boric u_int8_t rsvd; /* reserved */
1469f988b79SJean-Baptiste Boric u_int8_t sig; /* extended boot signature */
1479f988b79SJean-Baptiste Boric u_int8_t volid[4]; /* volume ID number */
1489f988b79SJean-Baptiste Boric u_int8_t label[11]; /* volume label */
1499f988b79SJean-Baptiste Boric u_int8_t type[8]; /* file system type */
1509f988b79SJean-Baptiste Boric };
1519f988b79SJean-Baptiste Boric
1529f988b79SJean-Baptiste Boric struct de {
1539f988b79SJean-Baptiste Boric u_int8_t namext[11]; /* name and extension */
1549f988b79SJean-Baptiste Boric u_int8_t attr; /* attributes */
1559f988b79SJean-Baptiste Boric u_int8_t rsvd[10]; /* reserved */
1569f988b79SJean-Baptiste Boric u_int8_t time[2]; /* creation time */
1579f988b79SJean-Baptiste Boric u_int8_t date[2]; /* creation date */
1589f988b79SJean-Baptiste Boric u_int8_t clus[2]; /* starting cluster */
1599f988b79SJean-Baptiste Boric u_int8_t size[4]; /* size */
1609f988b79SJean-Baptiste Boric };
1619f988b79SJean-Baptiste Boric
1629f988b79SJean-Baptiste Boric struct bpb {
1639f988b79SJean-Baptiste Boric u_int bps; /* bytes per sector */
1649f988b79SJean-Baptiste Boric u_int spc; /* sectors per cluster */
1659f988b79SJean-Baptiste Boric u_int res; /* reserved sectors */
1669f988b79SJean-Baptiste Boric u_int nft; /* number of FATs */
1679f988b79SJean-Baptiste Boric u_int rde; /* root directory entries */
1689f988b79SJean-Baptiste Boric u_int sec; /* total sectors */
1699f988b79SJean-Baptiste Boric u_int mid; /* media descriptor */
1709f988b79SJean-Baptiste Boric u_int spf; /* sectors per FAT */
1719f988b79SJean-Baptiste Boric u_int spt; /* sectors per track */
1729f988b79SJean-Baptiste Boric u_int hds; /* drive heads */
1739f988b79SJean-Baptiste Boric u_int hid; /* hidden sectors */
1749f988b79SJean-Baptiste Boric u_int bsec; /* big total sectors */
1759f988b79SJean-Baptiste Boric u_int bspf; /* big sectors per FAT */
1769f988b79SJean-Baptiste Boric u_int rdcl; /* root directory start cluster */
1779f988b79SJean-Baptiste Boric u_int infs; /* file system info sector */
1789f988b79SJean-Baptiste Boric u_int bkbs; /* backup boot sector */
1799f988b79SJean-Baptiste Boric };
1809f988b79SJean-Baptiste Boric
1819f988b79SJean-Baptiste Boric #define INIT(a, b, c, d, e, f, g, h, i, j) \
1829f988b79SJean-Baptiste Boric { .bps = a, .spc = b, .res = c, .nft = d, .rde = e, \
1839f988b79SJean-Baptiste Boric .sec = f, .mid = g, .spf = h, .spt = i, .hds = j, }
1849f988b79SJean-Baptiste Boric static struct {
1859f988b79SJean-Baptiste Boric const char *name;
1869f988b79SJean-Baptiste Boric struct bpb bpb;
1879f988b79SJean-Baptiste Boric } stdfmt[] = {
1889f988b79SJean-Baptiste Boric {"160", INIT(512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1)},
1899f988b79SJean-Baptiste Boric {"180", INIT(512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1)},
1909f988b79SJean-Baptiste Boric {"320", INIT(512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2)},
1919f988b79SJean-Baptiste Boric {"360", INIT(512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2)},
1929f988b79SJean-Baptiste Boric {"640", INIT(512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2)},
1939f988b79SJean-Baptiste Boric {"720", INIT(512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2)},
1949f988b79SJean-Baptiste Boric {"1200", INIT(512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2)},
1959f988b79SJean-Baptiste Boric {"1232", INIT(1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2)},
1969f988b79SJean-Baptiste Boric {"1440", INIT(512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2)},
1979f988b79SJean-Baptiste Boric {"2880", INIT(512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2)}
1989f988b79SJean-Baptiste Boric };
1999f988b79SJean-Baptiste Boric
2009f988b79SJean-Baptiste Boric static u_int8_t bootcode[] = {
2019f988b79SJean-Baptiste Boric 0xfa, /* cli */
2029f988b79SJean-Baptiste Boric 0x31, 0xc0, /* xor ax,ax */
2039f988b79SJean-Baptiste Boric 0x8e, 0xd0, /* mov ss,ax */
2049f988b79SJean-Baptiste Boric 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
2059f988b79SJean-Baptiste Boric 0xfb, /* sti */
2069f988b79SJean-Baptiste Boric 0x8e, 0xd8, /* mov ds,ax */
2079f988b79SJean-Baptiste Boric 0xe8, 0x00, 0x00, /* call $ + 3 */
2089f988b79SJean-Baptiste Boric 0x5e, /* pop si */
2099f988b79SJean-Baptiste Boric 0x83, 0xc6, 0x19, /* add si,+19h */
2109f988b79SJean-Baptiste Boric 0xbb, 0x07, 0x00, /* mov bx,0007h */
2119f988b79SJean-Baptiste Boric 0xfc, /* cld */
2129f988b79SJean-Baptiste Boric 0xac, /* lodsb */
2139f988b79SJean-Baptiste Boric 0x84, 0xc0, /* test al,al */
2149f988b79SJean-Baptiste Boric 0x74, 0x06, /* jz $ + 8 */
2159f988b79SJean-Baptiste Boric 0xb4, 0x0e, /* mov ah,0eh */
2169f988b79SJean-Baptiste Boric 0xcd, 0x10, /* int 10h */
2179f988b79SJean-Baptiste Boric 0xeb, 0xf5, /* jmp $ - 9 */
2189f988b79SJean-Baptiste Boric 0x30, 0xe4, /* xor ah,ah */
2199f988b79SJean-Baptiste Boric 0xcd, 0x16, /* int 16h */
2209f988b79SJean-Baptiste Boric 0xcd, 0x19, /* int 19h */
2219f988b79SJean-Baptiste Boric 0x0d, 0x0a,
2229f988b79SJean-Baptiste Boric 'N', 'o', 'n', '-', 's', 'y', 's', 't',
2239f988b79SJean-Baptiste Boric 'e', 'm', ' ', 'd', 'i', 's', 'k',
2249f988b79SJean-Baptiste Boric 0x0d, 0x0a,
2259f988b79SJean-Baptiste Boric 'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
2269f988b79SJean-Baptiste Boric 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
2279f988b79SJean-Baptiste Boric ' ', 'r', 'e', 'b', 'o', 'o', 't',
2289f988b79SJean-Baptiste Boric 0x0d, 0x0a,
2299f988b79SJean-Baptiste Boric 0
2309f988b79SJean-Baptiste Boric };
2319f988b79SJean-Baptiste Boric
2329f988b79SJean-Baptiste Boric static int got_siginfo = 0; /* received a SIGINFO */
2339f988b79SJean-Baptiste Boric
2349f988b79SJean-Baptiste Boric #ifndef MAKEFS
2359f988b79SJean-Baptiste Boric static int check_mounted(const char *, mode_t);
2369f988b79SJean-Baptiste Boric #endif
2379f988b79SJean-Baptiste Boric static int getstdfmt(const char *, struct bpb *);
238*25d017aaSSevan Janiyan static int getbpbinfo(int, const char *, const char *, int, struct bpb *, off_t);
2399f988b79SJean-Baptiste Boric static void print_bpb(struct bpb *);
2409f988b79SJean-Baptiste Boric static int ckgeom(const char *, u_int, const char *);
2419f988b79SJean-Baptiste Boric static int oklabel(const char *);
2429f988b79SJean-Baptiste Boric static void mklabel(u_int8_t *, const char *);
2439f988b79SJean-Baptiste Boric static void setstr(u_int8_t *, const char *, size_t);
2449f988b79SJean-Baptiste Boric static void infohandler(int sig);
2459f988b79SJean-Baptiste Boric
2469f988b79SJean-Baptiste Boric int
mkfs_msdos(const char * fname,const char * dtype,const struct msdos_options * op)2479f988b79SJean-Baptiste Boric mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op)
2489f988b79SJean-Baptiste Boric {
2499f988b79SJean-Baptiste Boric char buf[MAXPATHLEN];
2509f988b79SJean-Baptiste Boric struct stat sb;
2519f988b79SJean-Baptiste Boric struct timeval tv;
2529f988b79SJean-Baptiste Boric struct bpb bpb;
2539f988b79SJean-Baptiste Boric struct tm *tm;
2549f988b79SJean-Baptiste Boric struct bs *bs;
2559f988b79SJean-Baptiste Boric struct bsbpb *bsbpb;
2569f988b79SJean-Baptiste Boric struct bsxbpb *bsxbpb;
2579f988b79SJean-Baptiste Boric struct bsx *bsx;
2589f988b79SJean-Baptiste Boric struct de *de;
2599f988b79SJean-Baptiste Boric u_int8_t *img;
2609f988b79SJean-Baptiste Boric const char *bname;
2619f988b79SJean-Baptiste Boric ssize_t n;
2629f988b79SJean-Baptiste Boric time_t now;
2639f988b79SJean-Baptiste Boric u_int bss, rds, cls, dir, lsn, x, x1, x2;
2649f988b79SJean-Baptiste Boric int ch, fd, fd1;
2659f988b79SJean-Baptiste Boric struct msdos_options o = *op;
2669f988b79SJean-Baptiste Boric int oflags = O_RDWR | O_CREAT;
2679f988b79SJean-Baptiste Boric
2689f988b79SJean-Baptiste Boric if (o.block_size && o.sectors_per_cluster) {
2699f988b79SJean-Baptiste Boric warnx("Cannot specify both block size and sectors per cluster");
2709f988b79SJean-Baptiste Boric return -1;
2719f988b79SJean-Baptiste Boric }
2729f988b79SJean-Baptiste Boric if (o.OEM_string && strlen(o.OEM_string) > 8) {
2739f988b79SJean-Baptiste Boric warnx("%s: bad OEM string", o.OEM_string);
2749f988b79SJean-Baptiste Boric return -1;
2759f988b79SJean-Baptiste Boric }
2769f988b79SJean-Baptiste Boric if (o.create_size) {
2779f988b79SJean-Baptiste Boric if (o.no_create) {
2789f988b79SJean-Baptiste Boric warnx("create (-C) is incompatible with -N");
2799f988b79SJean-Baptiste Boric return -1;
2809f988b79SJean-Baptiste Boric }
2819f988b79SJean-Baptiste Boric if (o.offset == 0)
2829f988b79SJean-Baptiste Boric oflags |= O_TRUNC;
2839f988b79SJean-Baptiste Boric fd = open(fname, oflags, 0644);
2849f988b79SJean-Baptiste Boric if (fd == -1) {
2859f988b79SJean-Baptiste Boric warnx("failed to create %s", fname);
2869f988b79SJean-Baptiste Boric return -1;
2879f988b79SJean-Baptiste Boric }
2889f988b79SJean-Baptiste Boric (void)lseek(fd, o.create_size - 1, SEEK_SET);
2899f988b79SJean-Baptiste Boric if (write(fd, "\0", 1) != 1) {
2909f988b79SJean-Baptiste Boric warn("failed to set file size");
2919f988b79SJean-Baptiste Boric return -1;
2929f988b79SJean-Baptiste Boric }
2939f988b79SJean-Baptiste Boric (void)lseek(fd, 0, SEEK_SET);
2949f988b79SJean-Baptiste Boric } else if ((fd = open(fname, o.no_create ? O_RDONLY : O_RDWR)) == -1 ||
2959f988b79SJean-Baptiste Boric fstat(fd, &sb)) {
2969f988b79SJean-Baptiste Boric warn("%s", fname);
2979f988b79SJean-Baptiste Boric return -1;
2989f988b79SJean-Baptiste Boric }
2999f988b79SJean-Baptiste Boric #ifndef MAKEFS
3009f988b79SJean-Baptiste Boric if (!o.no_create)
3019f988b79SJean-Baptiste Boric if (check_mounted(fname, sb.st_mode) == -1)
3029f988b79SJean-Baptiste Boric return -1;
3039f988b79SJean-Baptiste Boric #endif
3049f988b79SJean-Baptiste Boric if (!S_ISCHR(sb.st_mode) && !o.create_size) {
3059f988b79SJean-Baptiste Boric warnx("warning, %s is not a character device", fname);
3069f988b79SJean-Baptiste Boric return -1;
3079f988b79SJean-Baptiste Boric }
3089f988b79SJean-Baptiste Boric if (o.offset && o.offset != lseek(fd, o.offset, SEEK_SET)) {
3099f988b79SJean-Baptiste Boric warnx("cannot seek to %jd", (intmax_t)o.offset);
3109f988b79SJean-Baptiste Boric return -1;
3119f988b79SJean-Baptiste Boric }
3129f988b79SJean-Baptiste Boric memset(&bpb, 0, sizeof(bpb));
3139f988b79SJean-Baptiste Boric if (o.floppy) {
3149f988b79SJean-Baptiste Boric if (getstdfmt(o.floppy, &bpb) == -1)
3159f988b79SJean-Baptiste Boric return -1;
3169f988b79SJean-Baptiste Boric bpb.bsec = bpb.sec;
3179f988b79SJean-Baptiste Boric bpb.sec = 0;
3189f988b79SJean-Baptiste Boric bpb.bspf = bpb.spf;
3199f988b79SJean-Baptiste Boric bpb.spf = 0;
3209f988b79SJean-Baptiste Boric }
3219f988b79SJean-Baptiste Boric if (o.drive_heads)
3229f988b79SJean-Baptiste Boric bpb.hds = o.drive_heads;
3239f988b79SJean-Baptiste Boric if (o.sectors_per_track)
3249f988b79SJean-Baptiste Boric bpb.spt = o.sectors_per_track;
3259f988b79SJean-Baptiste Boric if (o.bytes_per_sector)
3269f988b79SJean-Baptiste Boric bpb.bps = o.bytes_per_sector;
3279f988b79SJean-Baptiste Boric if (o.size)
3289f988b79SJean-Baptiste Boric bpb.bsec = o.size;
3299f988b79SJean-Baptiste Boric if (o.hidden_sectors_set)
3309f988b79SJean-Baptiste Boric bpb.hid = o.hidden_sectors;
3319f988b79SJean-Baptiste Boric if (!(o.floppy || (o.drive_heads && o.sectors_per_track &&
3329f988b79SJean-Baptiste Boric o.bytes_per_sector && o.size && o.hidden_sectors_set))) {
3339f988b79SJean-Baptiste Boric if (getbpbinfo(fd, fname, dtype, o.hidden_sectors_set, &bpb,
334*25d017aaSSevan Janiyan o.create_size) == -1)
3359f988b79SJean-Baptiste Boric return -1;
3369f988b79SJean-Baptiste Boric bpb.bsec -= (o.offset / bpb.bps);
3379f988b79SJean-Baptiste Boric if (bpb.spc == 0) { /* set defaults */
338*25d017aaSSevan Janiyan /* minimum cluster size */
339*25d017aaSSevan Janiyan switch (o.fat_type) {
340*25d017aaSSevan Janiyan case 12:
341*25d017aaSSevan Janiyan bpb.spc = 1; /* use 512 bytes */
342*25d017aaSSevan Janiyan x = 2; /* up to 2MB */
343*25d017aaSSevan Janiyan break;
344*25d017aaSSevan Janiyan case 16:
345*25d017aaSSevan Janiyan bpb.spc = 1; /* use 512 bytes */
346*25d017aaSSevan Janiyan x = 32; /* up to 32MB */
347*25d017aaSSevan Janiyan break;
348*25d017aaSSevan Janiyan default:
349*25d017aaSSevan Janiyan bpb.spc = 8; /* use 4k */
350*25d017aaSSevan Janiyan x = 8192; /* up to 8GB */
351*25d017aaSSevan Janiyan break;
352*25d017aaSSevan Janiyan }
353*25d017aaSSevan Janiyan x1 = howmany(bpb.bsec, (1048576 / 512)); /* -> MB */
354*25d017aaSSevan Janiyan while (bpb.spc < 128 && x < x1) {
355*25d017aaSSevan Janiyan x *= 2;
356*25d017aaSSevan Janiyan bpb.spc *= 2;
357*25d017aaSSevan Janiyan }
3589f988b79SJean-Baptiste Boric }
3599f988b79SJean-Baptiste Boric }
3609f988b79SJean-Baptiste Boric
3619f988b79SJean-Baptiste Boric if (o.volume_label && !oklabel(o.volume_label)) {
3629f988b79SJean-Baptiste Boric warnx("%s: bad volume label", o.volume_label);
3639f988b79SJean-Baptiste Boric return -1;
3649f988b79SJean-Baptiste Boric }
3659f988b79SJean-Baptiste Boric
3669f988b79SJean-Baptiste Boric switch (o.fat_type) {
3679f988b79SJean-Baptiste Boric case 0:
3689f988b79SJean-Baptiste Boric if (o.floppy)
3699f988b79SJean-Baptiste Boric o.fat_type = 12;
3709f988b79SJean-Baptiste Boric else if (!o.directory_entries && (o.info_sector || o.backup_sector))
3719f988b79SJean-Baptiste Boric o.fat_type = 32;
3729f988b79SJean-Baptiste Boric break;
3739f988b79SJean-Baptiste Boric case 12:
3749f988b79SJean-Baptiste Boric case 16:
3759f988b79SJean-Baptiste Boric if (o.info_sector) {
3769f988b79SJean-Baptiste Boric warnx("Cannot specify info sector with FAT%u", o.fat_type);
3779f988b79SJean-Baptiste Boric return -1;
3789f988b79SJean-Baptiste Boric }
3799f988b79SJean-Baptiste Boric if (o.backup_sector) {
3809f988b79SJean-Baptiste Boric warnx("Cannot specify backup sector with FAT%u", o.fat_type);
3819f988b79SJean-Baptiste Boric return -1;
3829f988b79SJean-Baptiste Boric }
3839f988b79SJean-Baptiste Boric break;
3849f988b79SJean-Baptiste Boric case 32:
3859f988b79SJean-Baptiste Boric if (o.directory_entries) {
3869f988b79SJean-Baptiste Boric warnx("Cannot specify directory entries with FAT32");
3879f988b79SJean-Baptiste Boric return -1;
3889f988b79SJean-Baptiste Boric }
3899f988b79SJean-Baptiste Boric break;
3909f988b79SJean-Baptiste Boric default:
3919f988b79SJean-Baptiste Boric warnx("%d: bad FAT type", o.fat_type);
3929f988b79SJean-Baptiste Boric return -1;
3939f988b79SJean-Baptiste Boric }
3949f988b79SJean-Baptiste Boric if (!powerof2(bpb.bps)) {
3959f988b79SJean-Baptiste Boric warnx("bytes/sector (%u) is not a power of 2", bpb.bps);
3969f988b79SJean-Baptiste Boric return -1;
3979f988b79SJean-Baptiste Boric }
3989f988b79SJean-Baptiste Boric if (bpb.bps < MINBPS) {
3999f988b79SJean-Baptiste Boric warnx("bytes/sector (%u) is too small; minimum is %u",
4009f988b79SJean-Baptiste Boric bpb.bps, MINBPS);
4019f988b79SJean-Baptiste Boric return -1;
4029f988b79SJean-Baptiste Boric }
4039f988b79SJean-Baptiste Boric
4049f988b79SJean-Baptiste Boric if (o.floppy && o.fat_type == 32)
4059f988b79SJean-Baptiste Boric bpb.rde = 0;
4069f988b79SJean-Baptiste Boric if (o.block_size) {
4079f988b79SJean-Baptiste Boric if (!powerof2(o.block_size)) {
4089f988b79SJean-Baptiste Boric warnx("block size (%u) is not a power of 2", o.block_size);
4099f988b79SJean-Baptiste Boric return -1;
4109f988b79SJean-Baptiste Boric }
4119f988b79SJean-Baptiste Boric if (o.block_size < bpb.bps) {
4129f988b79SJean-Baptiste Boric warnx("block size (%u) is too small; minimum is %u",
4139f988b79SJean-Baptiste Boric o.block_size, bpb.bps);
4149f988b79SJean-Baptiste Boric return -1;
4159f988b79SJean-Baptiste Boric }
4169f988b79SJean-Baptiste Boric if (o.block_size > bpb.bps * MAXSPC) {
4179f988b79SJean-Baptiste Boric warnx("block size (%u) is too large; maximum is %u",
4189f988b79SJean-Baptiste Boric o.block_size, bpb.bps * MAXSPC);
4199f988b79SJean-Baptiste Boric return -1;
4209f988b79SJean-Baptiste Boric }
4219f988b79SJean-Baptiste Boric bpb.spc = o.block_size / bpb.bps;
4229f988b79SJean-Baptiste Boric }
4239f988b79SJean-Baptiste Boric if (o.sectors_per_cluster) {
4249f988b79SJean-Baptiste Boric if (!powerof2(o.sectors_per_cluster)) {
4259f988b79SJean-Baptiste Boric warnx("sectors/cluster (%u) is not a power of 2",
4269f988b79SJean-Baptiste Boric o.sectors_per_cluster);
4279f988b79SJean-Baptiste Boric return -1;
4289f988b79SJean-Baptiste Boric }
4299f988b79SJean-Baptiste Boric bpb.spc = o.sectors_per_cluster;
4309f988b79SJean-Baptiste Boric }
4319f988b79SJean-Baptiste Boric if (o.reserved_sectors)
4329f988b79SJean-Baptiste Boric bpb.res = o.reserved_sectors;
4339f988b79SJean-Baptiste Boric if (o.num_FAT) {
4349f988b79SJean-Baptiste Boric if (o.num_FAT > MAXNFT) {
4359f988b79SJean-Baptiste Boric warnx("number of FATs (%u) is too large; maximum is %u",
4369f988b79SJean-Baptiste Boric o.num_FAT, MAXNFT);
4379f988b79SJean-Baptiste Boric return -1;
4389f988b79SJean-Baptiste Boric }
4399f988b79SJean-Baptiste Boric bpb.nft = o.num_FAT;
4409f988b79SJean-Baptiste Boric }
4419f988b79SJean-Baptiste Boric if (o.directory_entries)
4429f988b79SJean-Baptiste Boric bpb.rde = o.directory_entries;
4439f988b79SJean-Baptiste Boric if (o.media_descriptor_set) {
4449f988b79SJean-Baptiste Boric if (o.media_descriptor < 0xf0) {
4459f988b79SJean-Baptiste Boric warnx("illegal media descriptor (%#x)", o.media_descriptor);
4469f988b79SJean-Baptiste Boric return -1;
4479f988b79SJean-Baptiste Boric }
4489f988b79SJean-Baptiste Boric bpb.mid = o.media_descriptor;
4499f988b79SJean-Baptiste Boric }
4509f988b79SJean-Baptiste Boric if (o.sectors_per_fat)
4519f988b79SJean-Baptiste Boric bpb.bspf = o.sectors_per_fat;
4529f988b79SJean-Baptiste Boric if (o.info_sector)
4539f988b79SJean-Baptiste Boric bpb.infs = o.info_sector;
4549f988b79SJean-Baptiste Boric if (o.backup_sector)
4559f988b79SJean-Baptiste Boric bpb.bkbs = o.backup_sector;
4569f988b79SJean-Baptiste Boric bss = 1;
4579f988b79SJean-Baptiste Boric bname = NULL;
4589f988b79SJean-Baptiste Boric fd1 = -1;
4599f988b79SJean-Baptiste Boric if (o.bootstrap) {
4609f988b79SJean-Baptiste Boric bname = o.bootstrap;
4619f988b79SJean-Baptiste Boric if (!strchr(bname, '/')) {
4629f988b79SJean-Baptiste Boric snprintf(buf, sizeof(buf), "/boot/%s", bname);
4639f988b79SJean-Baptiste Boric if (!(bname = strdup(buf))) {
4649f988b79SJean-Baptiste Boric warn(NULL);
4659f988b79SJean-Baptiste Boric return -1;
4669f988b79SJean-Baptiste Boric }
4679f988b79SJean-Baptiste Boric }
4689f988b79SJean-Baptiste Boric if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb)) {
4699f988b79SJean-Baptiste Boric warn("%s", bname);
4709f988b79SJean-Baptiste Boric return -1;
4719f988b79SJean-Baptiste Boric }
4729f988b79SJean-Baptiste Boric if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||
4739f988b79SJean-Baptiste Boric sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16) {
4749f988b79SJean-Baptiste Boric warnx("%s: inappropriate file type or format", bname);
4759f988b79SJean-Baptiste Boric return -1;
4769f988b79SJean-Baptiste Boric }
4779f988b79SJean-Baptiste Boric bss = sb.st_size / bpb.bps;
4789f988b79SJean-Baptiste Boric }
4799f988b79SJean-Baptiste Boric if (!bpb.nft)
4809f988b79SJean-Baptiste Boric bpb.nft = 2;
4819f988b79SJean-Baptiste Boric if (!o.fat_type) {
4829f988b79SJean-Baptiste Boric if (bpb.bsec < (bpb.res ? bpb.res : bss) +
4839f988b79SJean-Baptiste Boric howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *
4849f988b79SJean-Baptiste Boric ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *
4859f988b79SJean-Baptiste Boric bpb.nft +
4869f988b79SJean-Baptiste Boric howmany(bpb.rde ? bpb.rde : DEFRDE,
4879f988b79SJean-Baptiste Boric bpb.bps / sizeof(struct de)) +
4889f988b79SJean-Baptiste Boric (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *
4899f988b79SJean-Baptiste Boric (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))
4909f988b79SJean-Baptiste Boric o.fat_type = 12;
4919f988b79SJean-Baptiste Boric else if (bpb.rde || bpb.bsec <
4929f988b79SJean-Baptiste Boric (bpb.res ? bpb.res : bss) +
4939f988b79SJean-Baptiste Boric howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +
4949f988b79SJean-Baptiste Boric howmany(DEFRDE, bpb.bps / sizeof(struct de)) +
4959f988b79SJean-Baptiste Boric (MAXCLS16 + 1) *
4969f988b79SJean-Baptiste Boric (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))
4979f988b79SJean-Baptiste Boric o.fat_type = 16;
4989f988b79SJean-Baptiste Boric else
4999f988b79SJean-Baptiste Boric o.fat_type = 32;
5009f988b79SJean-Baptiste Boric }
5019f988b79SJean-Baptiste Boric x = bss;
5029f988b79SJean-Baptiste Boric if (o.fat_type == 32) {
5039f988b79SJean-Baptiste Boric if (!bpb.infs) {
5049f988b79SJean-Baptiste Boric if (x == MAXU16 || x == bpb.bkbs) {
5059f988b79SJean-Baptiste Boric warnx("no room for info sector");
5069f988b79SJean-Baptiste Boric return -1;
5079f988b79SJean-Baptiste Boric }
5089f988b79SJean-Baptiste Boric bpb.infs = x;
5099f988b79SJean-Baptiste Boric }
5109f988b79SJean-Baptiste Boric if (bpb.infs != MAXU16 && x <= bpb.infs)
5119f988b79SJean-Baptiste Boric x = bpb.infs + 1;
5129f988b79SJean-Baptiste Boric if (!bpb.bkbs) {
5139f988b79SJean-Baptiste Boric if (x == MAXU16) {
5149f988b79SJean-Baptiste Boric warnx("no room for backup sector");
5159f988b79SJean-Baptiste Boric return -1;
5169f988b79SJean-Baptiste Boric }
5179f988b79SJean-Baptiste Boric bpb.bkbs = x;
5189f988b79SJean-Baptiste Boric } else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs) {
5199f988b79SJean-Baptiste Boric warnx("backup sector would overwrite info sector");
5209f988b79SJean-Baptiste Boric return -1;
5219f988b79SJean-Baptiste Boric }
5229f988b79SJean-Baptiste Boric if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)
5239f988b79SJean-Baptiste Boric x = bpb.bkbs + 1;
5249f988b79SJean-Baptiste Boric }
5259f988b79SJean-Baptiste Boric if (!bpb.res)
5269f988b79SJean-Baptiste Boric bpb.res = o.fat_type == 32 ? MAX(x, MAX(16384 / bpb.bps, 4)) : x;
5279f988b79SJean-Baptiste Boric else if (bpb.res < x) {
5289f988b79SJean-Baptiste Boric warnx("too few reserved sectors (need %d have %d)", x, bpb.res);
5299f988b79SJean-Baptiste Boric return -1;
5309f988b79SJean-Baptiste Boric }
5319f988b79SJean-Baptiste Boric if (o.fat_type != 32 && !bpb.rde)
5329f988b79SJean-Baptiste Boric bpb.rde = DEFRDE;
5339f988b79SJean-Baptiste Boric rds = howmany(bpb.rde, bpb.bps / sizeof(struct de));
5349f988b79SJean-Baptiste Boric if (!bpb.spc)
5359f988b79SJean-Baptiste Boric for (bpb.spc = howmany(o.fat_type == 16 ? DEFBLK16 : DEFBLK, bpb.bps);
5369f988b79SJean-Baptiste Boric bpb.spc < MAXSPC &&
5379f988b79SJean-Baptiste Boric bpb.res +
5389f988b79SJean-Baptiste Boric howmany((RESFTE + maxcls(o.fat_type)) * (o.fat_type / BPN),
5399f988b79SJean-Baptiste Boric bpb.bps * NPB) * bpb.nft +
5409f988b79SJean-Baptiste Boric rds +
5419f988b79SJean-Baptiste Boric (u_int64_t)(maxcls(o.fat_type) + 1) * bpb.spc <= bpb.bsec;
5429f988b79SJean-Baptiste Boric bpb.spc <<= 1);
5439f988b79SJean-Baptiste Boric if (o.fat_type != 32 && bpb.bspf > MAXU16) {
5449f988b79SJean-Baptiste Boric warnx("too many sectors/FAT for FAT12/16");
5459f988b79SJean-Baptiste Boric return -1;
5469f988b79SJean-Baptiste Boric }
5479f988b79SJean-Baptiste Boric x1 = bpb.res + rds;
5489f988b79SJean-Baptiste Boric x = bpb.bspf ? bpb.bspf : 1;
5499f988b79SJean-Baptiste Boric if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec) {
5509f988b79SJean-Baptiste Boric warnx("meta data exceeds file system size");
5519f988b79SJean-Baptiste Boric return -1;
5529f988b79SJean-Baptiste Boric }
5539f988b79SJean-Baptiste Boric x1 += x * bpb.nft;
5549f988b79SJean-Baptiste Boric x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /
5559f988b79SJean-Baptiste Boric (bpb.spc * bpb.bps * NPB + o.fat_type / BPN * bpb.nft);
5569f988b79SJean-Baptiste Boric x2 = howmany((RESFTE + MIN(x, maxcls(o.fat_type))) * (o.fat_type / BPN),
5579f988b79SJean-Baptiste Boric bpb.bps * NPB);
5589f988b79SJean-Baptiste Boric if (!bpb.bspf) {
5599f988b79SJean-Baptiste Boric bpb.bspf = x2;
5609f988b79SJean-Baptiste Boric x1 += (bpb.bspf - 1) * bpb.nft;
5619f988b79SJean-Baptiste Boric }
5629f988b79SJean-Baptiste Boric cls = (bpb.bsec - x1) / bpb.spc;
5639f988b79SJean-Baptiste Boric x = (u_int64_t)bpb.bspf * bpb.bps * NPB / (o.fat_type / BPN) - RESFTE;
5649f988b79SJean-Baptiste Boric if (cls > x)
5659f988b79SJean-Baptiste Boric cls = x;
5669f988b79SJean-Baptiste Boric if (bpb.bspf < x2) {
5679f988b79SJean-Baptiste Boric warnx("warning: sectors/FAT limits file system to %u clusters",
5689f988b79SJean-Baptiste Boric cls);
5699f988b79SJean-Baptiste Boric return -1;
5709f988b79SJean-Baptiste Boric }
5719f988b79SJean-Baptiste Boric if (cls < mincls(o.fat_type)) {
5729f988b79SJean-Baptiste Boric warnx("%u clusters too few clusters for FAT%u, need %u", cls,
5739f988b79SJean-Baptiste Boric o.fat_type, mincls(o.fat_type));
5749f988b79SJean-Baptiste Boric return -1;
5759f988b79SJean-Baptiste Boric }
5769f988b79SJean-Baptiste Boric if (cls > maxcls(o.fat_type)) {
5779f988b79SJean-Baptiste Boric cls = maxcls(o.fat_type);
5789f988b79SJean-Baptiste Boric bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;
5799f988b79SJean-Baptiste Boric warnx("warning: FAT type limits file system to %u sectors",
5809f988b79SJean-Baptiste Boric bpb.bsec);
5819f988b79SJean-Baptiste Boric return -1;
5829f988b79SJean-Baptiste Boric }
5839f988b79SJean-Baptiste Boric printf("%s: %u sector%s in %u FAT%u cluster%s "
5849f988b79SJean-Baptiste Boric "(%u bytes/cluster)\n", fname, cls * bpb.spc,
5859f988b79SJean-Baptiste Boric cls * bpb.spc == 1 ? "" : "s", cls, o.fat_type,
5869f988b79SJean-Baptiste Boric cls == 1 ? "" : "s", bpb.bps * bpb.spc);
5879f988b79SJean-Baptiste Boric if (!bpb.mid)
5889f988b79SJean-Baptiste Boric bpb.mid = !bpb.hid ? 0xf0 : 0xf8;
5899f988b79SJean-Baptiste Boric if (o.fat_type == 32)
5909f988b79SJean-Baptiste Boric bpb.rdcl = RESFTE;
5919f988b79SJean-Baptiste Boric if (bpb.hid + bpb.bsec <= MAXU16) {
5929f988b79SJean-Baptiste Boric bpb.sec = bpb.bsec;
5939f988b79SJean-Baptiste Boric bpb.bsec = 0;
5949f988b79SJean-Baptiste Boric }
5959f988b79SJean-Baptiste Boric if (o.fat_type != 32) {
5969f988b79SJean-Baptiste Boric bpb.spf = bpb.bspf;
5979f988b79SJean-Baptiste Boric bpb.bspf = 0;
5989f988b79SJean-Baptiste Boric }
5999f988b79SJean-Baptiste Boric ch = 0;
6009f988b79SJean-Baptiste Boric if (o.fat_type == 12)
6019f988b79SJean-Baptiste Boric ch = 1; /* 001 Primary DOS with 12 bit FAT */
6029f988b79SJean-Baptiste Boric else if (o.fat_type == 16) {
6039f988b79SJean-Baptiste Boric if (bpb.bsec == 0)
6049f988b79SJean-Baptiste Boric ch = 4; /* 004 Primary DOS with 16 bit FAT <32M */
6059f988b79SJean-Baptiste Boric else
6069f988b79SJean-Baptiste Boric ch = 6; /* 006 Primary 'big' DOS, 16-bit FAT (> 32MB) */
6079f988b79SJean-Baptiste Boric /*
6089f988b79SJean-Baptiste Boric * XXX: what about:
6099f988b79SJean-Baptiste Boric * 014 DOS (16-bit FAT) - LBA
6109f988b79SJean-Baptiste Boric * ?
6119f988b79SJean-Baptiste Boric */
6129f988b79SJean-Baptiste Boric } else if (o.fat_type == 32) {
6139f988b79SJean-Baptiste Boric ch = 11; /* 011 Primary DOS with 32 bit FAT */
6149f988b79SJean-Baptiste Boric /*
6159f988b79SJean-Baptiste Boric * XXX: what about:
6169f988b79SJean-Baptiste Boric * 012 Primary DOS with 32 bit FAT - LBA
6179f988b79SJean-Baptiste Boric * ?
6189f988b79SJean-Baptiste Boric */
6199f988b79SJean-Baptiste Boric }
6209f988b79SJean-Baptiste Boric if (ch != 0)
6219f988b79SJean-Baptiste Boric printf("MBR type: %d\n", ch);
6229f988b79SJean-Baptiste Boric print_bpb(&bpb);
6239f988b79SJean-Baptiste Boric if (!o.no_create) {
624*25d017aaSSevan Janiyan if (o.timestamp_set) {
625*25d017aaSSevan Janiyan tv.tv_sec = now = o.timestamp;
626*25d017aaSSevan Janiyan tv.tv_usec = 0;
627*25d017aaSSevan Janiyan tm = gmtime(&now);
628*25d017aaSSevan Janiyan } else {
6299f988b79SJean-Baptiste Boric gettimeofday(&tv, NULL);
6309f988b79SJean-Baptiste Boric now = tv.tv_sec;
6319f988b79SJean-Baptiste Boric tm = localtime(&now);
632*25d017aaSSevan Janiyan }
6339f988b79SJean-Baptiste Boric if (!(img = malloc(bpb.bps)))
6349f988b79SJean-Baptiste Boric err(1, NULL);
6359f988b79SJean-Baptiste Boric dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
6369f988b79SJean-Baptiste Boric #ifdef SIGINFO
6379f988b79SJean-Baptiste Boric signal(SIGINFO, infohandler);
6389f988b79SJean-Baptiste Boric #endif
6399f988b79SJean-Baptiste Boric for (lsn = 0; lsn < dir + (o.fat_type == 32 ? bpb.spc : rds); lsn++) {
6409f988b79SJean-Baptiste Boric if (got_siginfo) {
6419f988b79SJean-Baptiste Boric fprintf(stderr,"%s: writing sector %u of %u (%u%%)\n",
6429f988b79SJean-Baptiste Boric fname,lsn,(dir + (o.fat_type == 32 ? bpb.spc : rds)),
6439f988b79SJean-Baptiste Boric (lsn*100)/(dir + (o.fat_type == 32 ? bpb.spc : rds)));
6449f988b79SJean-Baptiste Boric got_siginfo = 0;
6459f988b79SJean-Baptiste Boric }
6469f988b79SJean-Baptiste Boric x = lsn;
6479f988b79SJean-Baptiste Boric if (o.bootstrap &&
6489f988b79SJean-Baptiste Boric o.fat_type == 32 && bpb.bkbs != MAXU16 &&
6499f988b79SJean-Baptiste Boric bss <= bpb.bkbs && x >= bpb.bkbs) {
6509f988b79SJean-Baptiste Boric x -= bpb.bkbs;
6519f988b79SJean-Baptiste Boric if (!x && lseek(fd1, o.offset, SEEK_SET)) {
6529f988b79SJean-Baptiste Boric warn("%s", bname);
6539f988b79SJean-Baptiste Boric return -1;
6549f988b79SJean-Baptiste Boric }
6559f988b79SJean-Baptiste Boric }
6569f988b79SJean-Baptiste Boric if (o.bootstrap && x < bss) {
6579f988b79SJean-Baptiste Boric if ((n = read(fd1, img, bpb.bps)) == -1) {
6589f988b79SJean-Baptiste Boric warn("%s", bname);
6599f988b79SJean-Baptiste Boric return -1;
6609f988b79SJean-Baptiste Boric }
6619f988b79SJean-Baptiste Boric if ((size_t)n != bpb.bps) {
6629f988b79SJean-Baptiste Boric warnx("%s: can't read sector %u", bname, x);
6639f988b79SJean-Baptiste Boric return -1;
6649f988b79SJean-Baptiste Boric }
6659f988b79SJean-Baptiste Boric } else
6669f988b79SJean-Baptiste Boric memset(img, 0, bpb.bps);
6679f988b79SJean-Baptiste Boric if (!lsn ||
6689f988b79SJean-Baptiste Boric (o.fat_type == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {
6699f988b79SJean-Baptiste Boric x1 = sizeof(struct bs);
6709f988b79SJean-Baptiste Boric bsbpb = (struct bsbpb *)(img + x1);
6719f988b79SJean-Baptiste Boric mk2(bsbpb->bps, bpb.bps);
6729f988b79SJean-Baptiste Boric mk1(bsbpb->spc, bpb.spc);
6739f988b79SJean-Baptiste Boric mk2(bsbpb->res, bpb.res);
6749f988b79SJean-Baptiste Boric mk1(bsbpb->nft, bpb.nft);
6759f988b79SJean-Baptiste Boric mk2(bsbpb->rde, bpb.rde);
6769f988b79SJean-Baptiste Boric mk2(bsbpb->sec, bpb.sec);
6779f988b79SJean-Baptiste Boric mk1(bsbpb->mid, bpb.mid);
6789f988b79SJean-Baptiste Boric mk2(bsbpb->spf, bpb.spf);
6799f988b79SJean-Baptiste Boric mk2(bsbpb->spt, bpb.spt);
6809f988b79SJean-Baptiste Boric mk2(bsbpb->hds, bpb.hds);
6819f988b79SJean-Baptiste Boric mk4(bsbpb->hid, bpb.hid);
6829f988b79SJean-Baptiste Boric mk4(bsbpb->bsec, bpb.bsec);
6839f988b79SJean-Baptiste Boric x1 += sizeof(struct bsbpb);
6849f988b79SJean-Baptiste Boric if (o.fat_type == 32) {
6859f988b79SJean-Baptiste Boric bsxbpb = (struct bsxbpb *)(img + x1);
6869f988b79SJean-Baptiste Boric mk4(bsxbpb->bspf, bpb.bspf);
6879f988b79SJean-Baptiste Boric mk2(bsxbpb->xflg, 0);
6889f988b79SJean-Baptiste Boric mk2(bsxbpb->vers, 0);
6899f988b79SJean-Baptiste Boric mk4(bsxbpb->rdcl, bpb.rdcl);
6909f988b79SJean-Baptiste Boric mk2(bsxbpb->infs, bpb.infs);
6919f988b79SJean-Baptiste Boric mk2(bsxbpb->bkbs, bpb.bkbs);
6929f988b79SJean-Baptiste Boric x1 += sizeof(struct bsxbpb);
6939f988b79SJean-Baptiste Boric }
6949f988b79SJean-Baptiste Boric bsx = (struct bsx *)(img + x1);
6959f988b79SJean-Baptiste Boric mk1(bsx->sig, 0x29);
6969f988b79SJean-Baptiste Boric if (o.volume_id_set)
6979f988b79SJean-Baptiste Boric x = o.volume_id;
6989f988b79SJean-Baptiste Boric else
6999f988b79SJean-Baptiste Boric x = (((u_int)(1 + tm->tm_mon) << 8 |
7009f988b79SJean-Baptiste Boric (u_int)tm->tm_mday) +
7019f988b79SJean-Baptiste Boric ((u_int)tm->tm_sec << 8 |
7029f988b79SJean-Baptiste Boric (u_int)(tv.tv_usec / 10))) << 16 |
7039f988b79SJean-Baptiste Boric ((u_int)(1900 + tm->tm_year) +
7049f988b79SJean-Baptiste Boric ((u_int)tm->tm_hour << 8 |
7059f988b79SJean-Baptiste Boric (u_int)tm->tm_min));
7069f988b79SJean-Baptiste Boric mk4(bsx->volid, x);
7079f988b79SJean-Baptiste Boric mklabel(bsx->label, o.volume_label ? o.volume_label : "NO NAME");
7089f988b79SJean-Baptiste Boric snprintf(buf, sizeof(buf), "FAT%u", o.fat_type);
7099f988b79SJean-Baptiste Boric setstr(bsx->type, buf, sizeof(bsx->type));
7109f988b79SJean-Baptiste Boric if (!o.bootstrap) {
7119f988b79SJean-Baptiste Boric x1 += sizeof(struct bsx);
7129f988b79SJean-Baptiste Boric bs = (struct bs *)img;
7139f988b79SJean-Baptiste Boric mk1(bs->jmp[0], 0xeb);
7149f988b79SJean-Baptiste Boric mk1(bs->jmp[1], x1 - 2);
7159f988b79SJean-Baptiste Boric mk1(bs->jmp[2], 0x90);
7169f988b79SJean-Baptiste Boric setstr(bs->oem, o.OEM_string ? o.OEM_string : "NetBSD",
7179f988b79SJean-Baptiste Boric sizeof(bs->oem));
7189f988b79SJean-Baptiste Boric memcpy(img + x1, bootcode, sizeof(bootcode));
7199f988b79SJean-Baptiste Boric mk2(img + MINBPS - 2, DOSMAGIC);
7209f988b79SJean-Baptiste Boric }
7219f988b79SJean-Baptiste Boric } else if (o.fat_type == 32 && bpb.infs != MAXU16 &&
7229f988b79SJean-Baptiste Boric (lsn == bpb.infs ||
7239f988b79SJean-Baptiste Boric (bpb.bkbs != MAXU16 &&
7249f988b79SJean-Baptiste Boric lsn == bpb.bkbs + bpb.infs))) {
7259f988b79SJean-Baptiste Boric mk4(img, 0x41615252);
7269f988b79SJean-Baptiste Boric mk4(img + MINBPS - 28, 0x61417272);
7279f988b79SJean-Baptiste Boric mk4(img + MINBPS - 24, 0xffffffff);
7280a6a1f1dSLionel Sambuc mk4(img + MINBPS - 20, 0xffffffff);
7299f988b79SJean-Baptiste Boric mk2(img + MINBPS - 2, DOSMAGIC);
7309f988b79SJean-Baptiste Boric } else if (lsn >= bpb.res && lsn < dir &&
7319f988b79SJean-Baptiste Boric !((lsn - bpb.res) %
7329f988b79SJean-Baptiste Boric (bpb.spf ? bpb.spf : bpb.bspf))) {
7339f988b79SJean-Baptiste Boric mk1(img[0], bpb.mid);
7349f988b79SJean-Baptiste Boric for (x = 1; x < o.fat_type * (o.fat_type == 32 ? 3U : 2U) / 8U; x++)
7359f988b79SJean-Baptiste Boric mk1(img[x], o.fat_type == 32 && x % 4 == 3 ? 0x0f : 0xff);
7369f988b79SJean-Baptiste Boric } else if (lsn == dir && o.volume_label) {
7379f988b79SJean-Baptiste Boric de = (struct de *)img;
7389f988b79SJean-Baptiste Boric mklabel(de->namext, o.volume_label);
7399f988b79SJean-Baptiste Boric mk1(de->attr, 050);
7409f988b79SJean-Baptiste Boric x = (u_int)tm->tm_hour << 11 |
7419f988b79SJean-Baptiste Boric (u_int)tm->tm_min << 5 |
7429f988b79SJean-Baptiste Boric (u_int)tm->tm_sec >> 1;
7439f988b79SJean-Baptiste Boric mk2(de->time, x);
7449f988b79SJean-Baptiste Boric x = (u_int)(tm->tm_year - 80) << 9 |
7459f988b79SJean-Baptiste Boric (u_int)(tm->tm_mon + 1) << 5 |
7469f988b79SJean-Baptiste Boric (u_int)tm->tm_mday;
7479f988b79SJean-Baptiste Boric mk2(de->date, x);
7489f988b79SJean-Baptiste Boric }
7499f988b79SJean-Baptiste Boric if ((n = write(fd, img, bpb.bps)) == -1) {
7509f988b79SJean-Baptiste Boric warn("%s", fname);
7519f988b79SJean-Baptiste Boric return -1;
7529f988b79SJean-Baptiste Boric }
7539f988b79SJean-Baptiste Boric if ((size_t)n != bpb.bps) {
7549f988b79SJean-Baptiste Boric warnx("%s: can't write sector %u", fname, lsn);
7559f988b79SJean-Baptiste Boric return -1;
7569f988b79SJean-Baptiste Boric }
7579f988b79SJean-Baptiste Boric }
7589f988b79SJean-Baptiste Boric }
7599f988b79SJean-Baptiste Boric return 0;
7609f988b79SJean-Baptiste Boric }
7619f988b79SJean-Baptiste Boric
7629f988b79SJean-Baptiste Boric #ifndef MAKEFS
7639f988b79SJean-Baptiste Boric /*
7649f988b79SJean-Baptiste Boric * return -1 with error if file system is mounted.
7659f988b79SJean-Baptiste Boric */
7669f988b79SJean-Baptiste Boric static int
check_mounted(const char * fname,mode_t mode)7679f988b79SJean-Baptiste Boric check_mounted(const char *fname, mode_t mode)
7689f988b79SJean-Baptiste Boric {
7699f988b79SJean-Baptiste Boric struct statvfs *mp;
7709f988b79SJean-Baptiste Boric const char *s1, *s2;
7719f988b79SJean-Baptiste Boric size_t len;
7729f988b79SJean-Baptiste Boric int n, r;
7739f988b79SJean-Baptiste Boric
7749f988b79SJean-Baptiste Boric if (!(n = getmntinfo(&mp, MNT_NOWAIT))) {
7759f988b79SJean-Baptiste Boric warn("getmntinfo");
7769f988b79SJean-Baptiste Boric return -1;
7779f988b79SJean-Baptiste Boric }
7789f988b79SJean-Baptiste Boric len = strlen(_PATH_DEV);
7799f988b79SJean-Baptiste Boric s1 = fname;
7809f988b79SJean-Baptiste Boric if (!strncmp(s1, _PATH_DEV, len))
7819f988b79SJean-Baptiste Boric s1 += len;
7829f988b79SJean-Baptiste Boric r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
7839f988b79SJean-Baptiste Boric for (; n--; mp++) {
7849f988b79SJean-Baptiste Boric s2 = mp->f_mntfromname;
7859f988b79SJean-Baptiste Boric if (!strncmp(s2, _PATH_DEV, len))
7869f988b79SJean-Baptiste Boric s2 += len;
7879f988b79SJean-Baptiste Boric if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
7889f988b79SJean-Baptiste Boric !strcmp(s1, s2)) {
7899f988b79SJean-Baptiste Boric warnx("%s is mounted on %s", fname, mp->f_mntonname);
7909f988b79SJean-Baptiste Boric return -1;
7919f988b79SJean-Baptiste Boric }
7929f988b79SJean-Baptiste Boric }
7939f988b79SJean-Baptiste Boric return 0;
7949f988b79SJean-Baptiste Boric }
7959f988b79SJean-Baptiste Boric #endif
7969f988b79SJean-Baptiste Boric
7979f988b79SJean-Baptiste Boric /*
7989f988b79SJean-Baptiste Boric * Get a standard format.
7999f988b79SJean-Baptiste Boric */
8009f988b79SJean-Baptiste Boric static int
getstdfmt(const char * fmt,struct bpb * bpb)8019f988b79SJean-Baptiste Boric getstdfmt(const char *fmt, struct bpb *bpb)
8029f988b79SJean-Baptiste Boric {
8039f988b79SJean-Baptiste Boric u_int x, i;
8049f988b79SJean-Baptiste Boric
8059f988b79SJean-Baptiste Boric x = sizeof(stdfmt) / sizeof(stdfmt[0]);
8069f988b79SJean-Baptiste Boric for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
8079f988b79SJean-Baptiste Boric if (i == x) {
8089f988b79SJean-Baptiste Boric warnx("%s: unknown standard format", fmt);
8099f988b79SJean-Baptiste Boric return -1;
8109f988b79SJean-Baptiste Boric }
8119f988b79SJean-Baptiste Boric *bpb = stdfmt[i].bpb;
8129f988b79SJean-Baptiste Boric return 0;
8139f988b79SJean-Baptiste Boric }
8149f988b79SJean-Baptiste Boric
8159f988b79SJean-Baptiste Boric /*
8169f988b79SJean-Baptiste Boric * Get disk slice, partition, and geometry information.
8179f988b79SJean-Baptiste Boric */
8189f988b79SJean-Baptiste Boric static int
getbpbinfo(int fd,const char * fname,const char * dtype,int iflag,struct bpb * bpb,off_t create_size)8199f988b79SJean-Baptiste Boric getbpbinfo(int fd, const char *fname, const char *dtype, int iflag,
820*25d017aaSSevan Janiyan struct bpb *bpb, off_t create_size)
8219f988b79SJean-Baptiste Boric {
8229f988b79SJean-Baptiste Boric const char *s1, *s2;
8239f988b79SJean-Baptiste Boric int part;
8249f988b79SJean-Baptiste Boric
8259f988b79SJean-Baptiste Boric part = -1;
8269f988b79SJean-Baptiste Boric s1 = fname;
8279f988b79SJean-Baptiste Boric if ((s2 = strrchr(s1, '/')))
8289f988b79SJean-Baptiste Boric s1 = s2 + 1;
8299f988b79SJean-Baptiste Boric for (s2 = s1; *s2 && !isdigit((unsigned char)*s2); s2++);
8309f988b79SJean-Baptiste Boric if (!*s2 || s2 == s1)
8319f988b79SJean-Baptiste Boric s2 = NULL;
8329f988b79SJean-Baptiste Boric else
8339f988b79SJean-Baptiste Boric while (isdigit((unsigned char)*++s2));
8349f988b79SJean-Baptiste Boric s1 = s2;
8359f988b79SJean-Baptiste Boric
8369f988b79SJean-Baptiste Boric #ifndef MAKEFS
8379f988b79SJean-Baptiste Boric int maxpartitions = getmaxpartitions();
838*25d017aaSSevan Janiyan struct disk_geom geo;
839*25d017aaSSevan Janiyan struct dkwedge_info dkw;
8409f988b79SJean-Baptiste Boric
8419f988b79SJean-Baptiste Boric // XXX: Does not work with wedges
8429f988b79SJean-Baptiste Boric if (s2 && *s2 >= 'a' && *s2 <= 'a' + maxpartitions - 1) {
8439f988b79SJean-Baptiste Boric part = *s2++ - 'a';
8449f988b79SJean-Baptiste Boric }
8459f988b79SJean-Baptiste Boric #endif
8469f988b79SJean-Baptiste Boric
847*25d017aaSSevan Janiyan if (!(((part != -1) && ((!iflag && part != -1) || !bpb->bsec)) ||
848*25d017aaSSevan Janiyan !bpb->bps || !bpb->spt || !bpb->hds)) {
849*25d017aaSSevan Janiyan return 0;
850*25d017aaSSevan Janiyan }
851*25d017aaSSevan Janiyan
852*25d017aaSSevan Janiyan u_int sector_size = 512;
853*25d017aaSSevan Janiyan u_int nsectors = 63;
854*25d017aaSSevan Janiyan u_int ntracks = 255;
855*25d017aaSSevan Janiyan u_int size;
856*25d017aaSSevan Janiyan
857*25d017aaSSevan Janiyan if (create_size == 0) {
858*25d017aaSSevan Janiyan #ifndef MAKEFS
859*25d017aaSSevan Janiyan if (getdiskinfo(fname, fd, NULL, &geo, &dkw) != -1) {
860*25d017aaSSevan Janiyan sector_size = geo.dg_secsize;
861*25d017aaSSevan Janiyan nsectors = geo.dg_nsectors;
862*25d017aaSSevan Janiyan ntracks = geo.dg_ntracks;
8639f988b79SJean-Baptiste Boric size = dkw.dkw_size;
8649f988b79SJean-Baptiste Boric } else
8659f988b79SJean-Baptiste Boric #endif
8669f988b79SJean-Baptiste Boric {
8679f988b79SJean-Baptiste Boric struct stat st;
8689f988b79SJean-Baptiste Boric
8699f988b79SJean-Baptiste Boric if (fstat(fd, &st) == -1) {
8709f988b79SJean-Baptiste Boric warnx("Can't get disk size for `%s'", fname);
8719f988b79SJean-Baptiste Boric return -1;
8729f988b79SJean-Baptiste Boric }
8739f988b79SJean-Baptiste Boric size = st.st_size / sector_size;
8749f988b79SJean-Baptiste Boric }
875*25d017aaSSevan Janiyan } else {
876*25d017aaSSevan Janiyan size = create_size / sector_size;
877*25d017aaSSevan Janiyan }
878*25d017aaSSevan Janiyan
8799f988b79SJean-Baptiste Boric if (!bpb->bps) {
8809f988b79SJean-Baptiste Boric if (ckgeom(fname, sector_size, "bytes/sector") == -1)
8819f988b79SJean-Baptiste Boric return -1;
8829f988b79SJean-Baptiste Boric bpb->bps = sector_size;
8839f988b79SJean-Baptiste Boric }
8849f988b79SJean-Baptiste Boric
8859f988b79SJean-Baptiste Boric if (nsectors > 63) {
8869f988b79SJean-Baptiste Boric /*
8879f988b79SJean-Baptiste Boric * The kernel doesn't accept BPB with spt > 63.
8889f988b79SJean-Baptiste Boric * (see sys/fs/msdosfs/msdosfs_vfsops.c:msdosfs_mountfs())
8899f988b79SJean-Baptiste Boric * If values taken from disklabel don't match these
8909f988b79SJean-Baptiste Boric * restrictions, use popular BIOS default values instead.
8919f988b79SJean-Baptiste Boric */
8929f988b79SJean-Baptiste Boric nsectors = 63;
8939f988b79SJean-Baptiste Boric }
8949f988b79SJean-Baptiste Boric if (!bpb->spt) {
8959f988b79SJean-Baptiste Boric if (ckgeom(fname, nsectors, "sectors/track") == -1)
8969f988b79SJean-Baptiste Boric return -1;
8979f988b79SJean-Baptiste Boric bpb->spt = nsectors;
8989f988b79SJean-Baptiste Boric }
8999f988b79SJean-Baptiste Boric if (!bpb->hds)
9009f988b79SJean-Baptiste Boric if (ckgeom(fname, ntracks, "drive heads") == -1)
9019f988b79SJean-Baptiste Boric return -1;
9029f988b79SJean-Baptiste Boric bpb->hds = ntracks;
9039f988b79SJean-Baptiste Boric if (!bpb->bsec)
9049f988b79SJean-Baptiste Boric bpb->bsec = size;
905*25d017aaSSevan Janiyan
9069f988b79SJean-Baptiste Boric return 0;
9079f988b79SJean-Baptiste Boric }
9089f988b79SJean-Baptiste Boric
9099f988b79SJean-Baptiste Boric /*
9109f988b79SJean-Baptiste Boric * Print out BPB values.
9119f988b79SJean-Baptiste Boric */
9129f988b79SJean-Baptiste Boric static void
print_bpb(struct bpb * bpb)9139f988b79SJean-Baptiste Boric print_bpb(struct bpb *bpb)
9149f988b79SJean-Baptiste Boric {
9159f988b79SJean-Baptiste Boric printf("bps=%u spc=%u res=%u nft=%u", bpb->bps, bpb->spc, bpb->res,
9169f988b79SJean-Baptiste Boric bpb->nft);
9179f988b79SJean-Baptiste Boric if (bpb->rde)
9189f988b79SJean-Baptiste Boric printf(" rde=%u", bpb->rde);
9199f988b79SJean-Baptiste Boric if (bpb->sec)
9209f988b79SJean-Baptiste Boric printf(" sec=%u", bpb->sec);
9219f988b79SJean-Baptiste Boric printf(" mid=%#x", bpb->mid);
9229f988b79SJean-Baptiste Boric if (bpb->spf)
9239f988b79SJean-Baptiste Boric printf(" spf=%u", bpb->spf);
9249f988b79SJean-Baptiste Boric printf(" spt=%u hds=%u hid=%u", bpb->spt, bpb->hds, bpb->hid);
9259f988b79SJean-Baptiste Boric if (bpb->bsec)
9269f988b79SJean-Baptiste Boric printf(" bsec=%u", bpb->bsec);
9279f988b79SJean-Baptiste Boric if (!bpb->spf) {
9289f988b79SJean-Baptiste Boric printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);
9299f988b79SJean-Baptiste Boric printf(" infs=");
9309f988b79SJean-Baptiste Boric printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);
9319f988b79SJean-Baptiste Boric printf(" bkbs=");
9329f988b79SJean-Baptiste Boric printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);
9339f988b79SJean-Baptiste Boric }
9349f988b79SJean-Baptiste Boric printf("\n");
9359f988b79SJean-Baptiste Boric }
9369f988b79SJean-Baptiste Boric
9379f988b79SJean-Baptiste Boric /*
9389f988b79SJean-Baptiste Boric * Check a disk geometry value.
9399f988b79SJean-Baptiste Boric */
9409f988b79SJean-Baptiste Boric static int
ckgeom(const char * fname,u_int val,const char * msg)9419f988b79SJean-Baptiste Boric ckgeom(const char *fname, u_int val, const char *msg)
9429f988b79SJean-Baptiste Boric {
9439f988b79SJean-Baptiste Boric if (!val) {
9449f988b79SJean-Baptiste Boric warnx("%s: no default %s", fname, msg);
9459f988b79SJean-Baptiste Boric return -1;
9469f988b79SJean-Baptiste Boric }
9479f988b79SJean-Baptiste Boric if (val > MAXU16) {
9489f988b79SJean-Baptiste Boric warnx("%s: illegal %s", fname, msg);
9499f988b79SJean-Baptiste Boric return -1;
9509f988b79SJean-Baptiste Boric }
9519f988b79SJean-Baptiste Boric return 0;
9529f988b79SJean-Baptiste Boric }
9539f988b79SJean-Baptiste Boric /*
9549f988b79SJean-Baptiste Boric * Check a volume label.
9559f988b79SJean-Baptiste Boric */
9569f988b79SJean-Baptiste Boric static int
oklabel(const char * src)9579f988b79SJean-Baptiste Boric oklabel(const char *src)
9589f988b79SJean-Baptiste Boric {
9599f988b79SJean-Baptiste Boric int c, i;
9609f988b79SJean-Baptiste Boric
9619f988b79SJean-Baptiste Boric for (i = 0; i <= 11; i++) {
9629f988b79SJean-Baptiste Boric c = (u_char)*src++;
9639f988b79SJean-Baptiste Boric if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
9649f988b79SJean-Baptiste Boric break;
9659f988b79SJean-Baptiste Boric }
9669f988b79SJean-Baptiste Boric return i && !c;
9679f988b79SJean-Baptiste Boric }
9689f988b79SJean-Baptiste Boric
9699f988b79SJean-Baptiste Boric /*
9709f988b79SJean-Baptiste Boric * Make a volume label.
9719f988b79SJean-Baptiste Boric */
9729f988b79SJean-Baptiste Boric static void
mklabel(u_int8_t * dest,const char * src)9739f988b79SJean-Baptiste Boric mklabel(u_int8_t *dest, const char *src)
9749f988b79SJean-Baptiste Boric {
9759f988b79SJean-Baptiste Boric int c, i;
9769f988b79SJean-Baptiste Boric
9779f988b79SJean-Baptiste Boric for (i = 0; i < 11; i++) {
9789f988b79SJean-Baptiste Boric c = *src ? toupper((unsigned char)*src++) : ' ';
9799f988b79SJean-Baptiste Boric *dest++ = !i && c == '\xe5' ? 5 : c;
9809f988b79SJean-Baptiste Boric }
9819f988b79SJean-Baptiste Boric }
9829f988b79SJean-Baptiste Boric
9839f988b79SJean-Baptiste Boric /*
9849f988b79SJean-Baptiste Boric * Copy string, padding with spaces.
9859f988b79SJean-Baptiste Boric */
9869f988b79SJean-Baptiste Boric static void
setstr(u_int8_t * dest,const char * src,size_t len)9879f988b79SJean-Baptiste Boric setstr(u_int8_t *dest, const char *src, size_t len)
9889f988b79SJean-Baptiste Boric {
9899f988b79SJean-Baptiste Boric while (len--)
9909f988b79SJean-Baptiste Boric *dest++ = *src ? *src++ : ' ';
9919f988b79SJean-Baptiste Boric }
9929f988b79SJean-Baptiste Boric
9939f988b79SJean-Baptiste Boric static void
infohandler(int sig)9949f988b79SJean-Baptiste Boric infohandler(int sig)
9959f988b79SJean-Baptiste Boric {
9969f988b79SJean-Baptiste Boric got_siginfo = 1;
9979f988b79SJean-Baptiste Boric }
998