From 082160604ca91a457405a94ccedf19143ff00857 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Tue, 6 Dec 2016 08:35:58 +0100
Subject: [PATCH 10/10] WIP Sync newfs_msdos(8) with FreeBSD.

---
 sbin/newfs_msdos/Makefile      |   5 +-
 sbin/newfs_msdos/mkfs_msdos.c  | 941 +++++++++++++++++++++++++++++++++++++++++
 sbin/newfs_msdos/mkfs_msdos.h  |  69 +++
 sbin/newfs_msdos/newfs_msdos.8 | 147 +++++--
 sbin/newfs_msdos/newfs_msdos.c | 867 +++++--------------------------------
 5 files changed, 1213 insertions(+), 816 deletions(-)
 create mode 100644 sbin/newfs_msdos/mkfs_msdos.c
 create mode 100644 sbin/newfs_msdos/mkfs_msdos.h

diff --git a/sbin/newfs_msdos/Makefile b/sbin/newfs_msdos/Makefile
index 76af677..75b2462 100644
--- a/sbin/newfs_msdos/Makefile
+++ b/sbin/newfs_msdos/Makefile
@@ -1,8 +1,7 @@
-# $FreeBSD: src/sbin/newfs_msdos/Makefile,v 1.2.2.2 2001/04/25 10:58:42 ru Exp $
-# $DragonFly: src/sbin/newfs_msdos/Makefile,v 1.4 2006/10/17 00:55:43 pavalos Exp $
+# $FreeBSD: head/sbin/newfs_msdos/Makefile 298107 2016-04-16 07:45:30Z gjb $
 
 PROG=	newfs_msdos
+SRCS=	newfs_msdos.c mkfs_msdos.c
 MAN=	newfs_msdos.8
-WARNS?=	2
 
 .include <bsd.prog.mk>
diff --git a/sbin/newfs_msdos/mkfs_msdos.c b/sbin/newfs_msdos/mkfs_msdos.c
new file mode 100644
index 0000000..a75583b
--- /dev/null
+++ b/sbin/newfs_msdos/mkfs_msdos.c
@@ -0,0 +1,941 @@
+/*
+ * Copyright (c) 1998 Robert Nordier
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $FreeBSD: head/sbin/newfs_msdos/mkfs_msdos.c 305075 2016-08-30 18:01:26Z imp $
+ */
+
+#include <sys/param.h>
+#include <sys/disklabel32.h>
+#include <sys/diskmbr.h>
+#include <sys/diskslice.h>
+#include <sys/mount.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+
+#include <machine/ioctl_fd.h>
+
+#include <ctype.h>
+#include <disktab.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <paths.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "mkfs_msdos.h"
+
+#define	MAXU16	  0xffff	/* maximum unsigned 16-bit quantity */
+#define	BPN	  4		/* bits per nibble */
+#define	NPB	  2		/* nibbles per byte */
+
+#define	MINBPS	  512		/* minimum bytes per sector */
+#define	MAXSPC	  128		/* maximum sectors per cluster */
+#define	MAXNFT	  16		/* maximum number of FATs */
+#define	DEFBLK	  4096		/* default block size */
+#define	DEFBLK16  2048		/* default block size FAT16 */
+#define	DEFRDE	  512		/* default root directory entries */
+#define	RESFTE	  2		/* reserved FAT entries */
+#define	MINCLS12  1U		/* minimum FAT12 clusters */
+#define	MINCLS16  0xff5U	/* minimum FAT16 clusters */
+#define	MINCLS32  0xfff5U	/* minimum FAT32 clusters */
+#define	MAXCLS12  0xff4U	/* maximum FAT12 clusters */
+#define	MAXCLS16  0xfff4U	/* maximum FAT16 clusters */
+#define	MAXCLS32  0xffffff4U	/* maximum FAT32 clusters */
+
+#define	mincls(fat)  ((fat) == 12 ? MINCLS12 :	\
+		      (fat) == 16 ? MINCLS16 :	\
+				    MINCLS32)
+
+#define	maxcls(fat)  ((fat) == 12 ? MAXCLS12 :	\
+		      (fat) == 16 ? MAXCLS16 :	\
+				    MAXCLS32)
+
+#define	mk1(p, x)				\
+    (p) = (u_int8_t)(x)
+
+#define	mk2(p, x)				\
+    (p)[0] = (u_int8_t)(x),			\
+    (p)[1] = (u_int8_t)((x) >> 010)
+
+#define	mk4(p, x)				\
+    (p)[0] = (u_int8_t)(x),			\
+    (p)[1] = (u_int8_t)((x) >> 010),		\
+    (p)[2] = (u_int8_t)((x) >> 020),		\
+    (p)[3] = (u_int8_t)((x) >> 030)
+
+struct bs {
+    u_int8_t bsJump[3];			/* bootstrap entry point */
+    u_int8_t bsOemName[8];		/* OEM name and version */
+} __packed;
+
+struct bsbpb {
+    u_int8_t bpbBytesPerSec[2];		/* bytes per sector */
+    u_int8_t bpbSecPerClust;		/* sectors per cluster */
+    u_int8_t bpbResSectors[2];		/* reserved sectors */
+    u_int8_t bpbFATs;			/* number of FATs */
+    u_int8_t bpbRootDirEnts[2];		/* root directory entries */
+    u_int8_t bpbSectors[2];		/* total sectors */
+    u_int8_t bpbMedia;			/* media descriptor */
+    u_int8_t bpbFATsecs[2];		/* sectors per FAT */
+    u_int8_t bpbSecPerTrack[2];		/* sectors per track */
+    u_int8_t bpbHeads[2];		/* drive heads */
+    u_int8_t bpbHiddenSecs[4];		/* hidden sectors */
+    u_int8_t bpbHugeSectors[4];		/* big total sectors */
+} __packed;
+
+struct bsxbpb {
+    u_int8_t bpbBigFATsecs[4];		/* big sectors per FAT */
+    u_int8_t bpbExtFlags[2];		/* FAT control flags */
+    u_int8_t bpbFSVers[2];		/* file system version */
+    u_int8_t bpbRootClust[4];		/* root directory start cluster */
+    u_int8_t bpbFSInfo[2];		/* file system info sector */
+    u_int8_t bpbBackup[2];		/* backup boot sector */
+    u_int8_t bpbReserved[12];		/* reserved */
+} __packed;
+
+struct bsx {
+    u_int8_t exDriveNumber;		/* drive number */
+    u_int8_t exReserved1;		/* reserved */
+    u_int8_t exBootSignature;		/* extended boot signature */
+    u_int8_t exVolumeID[4];		/* volume ID number */
+    u_int8_t exVolumeLabel[11];		/* volume label */
+    u_int8_t exFileSysType[8];		/* file system type */
+} __packed;
+
+struct de {
+    u_int8_t deName[11];		/* name and extension */
+    u_int8_t deAttributes;		/* attributes */
+    u_int8_t rsvd[10];			/* reserved */
+    u_int8_t deMTime[2];		/* last-modified time */
+    u_int8_t deMDate[2];		/* last-modified date */
+    u_int8_t deStartCluster[2];		/* starting cluster */
+    u_int8_t deFileSize[4];		/* size */
+} __packed;
+
+struct bpb {
+    u_int bpbBytesPerSec;		/* bytes per sector */
+    u_int bpbSecPerClust;		/* sectors per cluster */
+    u_int bpbResSectors;		/* reserved sectors */
+    u_int bpbFATs;			/* number of FATs */
+    u_int bpbRootDirEnts;		/* root directory entries */
+    u_int bpbSectors;			/* total sectors */
+    u_int bpbMedia;			/* media descriptor */
+    u_int bpbFATsecs;			/* sectors per FAT */
+    u_int bpbSecPerTrack;		/* sectors per track */
+    u_int bpbHeads;			/* drive heads */
+    u_int bpbHiddenSecs;		/* hidden sectors */
+    u_int bpbHugeSectors; 		/* big total sectors */
+    u_int bpbBigFATsecs; 		/* big sectors per FAT */
+    u_int bpbRootClust; 		/* root directory start cluster */
+    u_int bpbFSInfo; 			/* file system info sector */
+    u_int bpbBackup; 			/* backup boot sector */
+};
+
+#define	BPBGAP 0, 0, 0, 0, 0, 0
+
+static struct {
+    const char *name;
+    struct bpb bpb;
+} const stdfmt[] = {
+    {"160",  {512, 1, 1, 2,  64,  320, 0xfe, 1,  8, 1, BPBGAP}},
+    {"180",  {512, 1, 1, 2,  64,  360, 0xfc, 2,  9, 1, BPBGAP}},
+    {"320",  {512, 2, 1, 2, 112,  640, 0xff, 1,  8, 2, BPBGAP}},
+    {"360",  {512, 2, 1, 2, 112,  720, 0xfd, 2,  9, 2, BPBGAP}},
+    {"640",  {512, 2, 1, 2, 112, 1280, 0xfb, 2,  8, 2, BPBGAP}},
+    {"720",  {512, 2, 1, 2, 112, 1440, 0xf9, 3,  9, 2, BPBGAP}},
+    {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2, BPBGAP}},
+    {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2,  8, 2, BPBGAP}},
+    {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2, BPBGAP}},
+    {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2, BPBGAP}}
+};
+
+static const u_int8_t bootcode[] = {
+    0xfa,			/* cli		    */
+    0x31, 0xc0, 		/* xor	   ax,ax    */
+    0x8e, 0xd0, 		/* mov	   ss,ax    */
+    0xbc, 0x00, 0x7c,		/* mov	   sp,7c00h */
+    0xfb,			/* sti		    */
+    0x8e, 0xd8, 		/* mov	   ds,ax    */
+    0xe8, 0x00, 0x00,		/* call    $ + 3    */
+    0x5e,			/* pop	   si	    */
+    0x83, 0xc6, 0x19,		/* add	   si,+19h  */
+    0xbb, 0x07, 0x00,		/* mov	   bx,0007h */
+    0xfc,			/* cld		    */
+    0xac,			/* lodsb	    */
+    0x84, 0xc0, 		/* test    al,al    */
+    0x74, 0x06, 		/* jz	   $ + 8    */
+    0xb4, 0x0e, 		/* mov	   ah,0eh   */
+    0xcd, 0x10, 		/* int	   10h	    */
+    0xeb, 0xf5, 		/* jmp	   $ - 9    */
+    0x30, 0xe4, 		/* xor	   ah,ah    */
+    0xcd, 0x16, 		/* int	   16h	    */
+    0xcd, 0x19, 		/* int	   19h	    */
+    0x0d, 0x0a,
+    'N', 'o', 'n', '-', 's', 'y', 's', 't',
+    'e', 'm', ' ', 'd', 'i', 's', 'k',
+    0x0d, 0x0a,
+    'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
+    'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
+    ' ', 'r', 'e', 'b', 'o', 'o', 't',
+    0x0d, 0x0a,
+    0
+};
+
+static volatile sig_atomic_t got_siginfo;
+static void infohandler(int);
+
+static int check_mounted(const char *, mode_t);
+static int getstdfmt(const char *, struct bpb *);
+static int getdiskinfo(int, const char *, const char *, int, struct bpb *);
+static void print_bpb(struct bpb *);
+static int ckgeom(const char *, u_int, const char *);
+static void mklabel(u_int8_t *, const char *);
+static int oklabel(const char *);
+static void setstr(u_int8_t *, const char *, size_t);
+
+int
+mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op)
+{
+    char buf[MAXPATHLEN];
+    struct sigaction si_sa;
+    struct stat sb;
+    struct timeval tv;
+    struct bpb bpb;
+    struct tm *tm;
+    struct bs *bs;
+    struct bsbpb *bsbpb;
+    struct bsxbpb *bsxbpb;
+    struct bsx *bsx;
+    struct de *de;
+    u_int8_t *img;
+    const char *bname;
+    ssize_t n;
+    time_t now;
+    u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
+    int fd, fd1, rv;
+    struct msdos_options o = *op;
+
+    img = NULL;
+    rv = -1;
+
+    if (o.block_size && o.sectors_per_cluster) {
+	warnx("Cannot specify both block size and sectors per cluster");
+	goto done;
+    }
+    if (o.OEM_string && strlen(o.OEM_string) > 8) {
+	warnx("%s: bad OEM string", o.OEM_string);
+	goto done;
+    }
+    if (o.create_size) {
+	if (o.no_create) {
+	    warnx("create (-C) is incompatible with -N");
+	    goto done;
+	}
+	fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644);
+	if (fd == -1) {
+	    warnx("failed to create %s", fname);
+	    goto done;
+	}
+	if (ftruncate(fd, o.create_size)) {
+	    warnx("failed to initialize %jd bytes", (intmax_t)o.create_size);
+	    goto done;
+	}
+    } else if ((fd = open(fname, o.no_create ? O_RDONLY : O_RDWR)) == -1) {
+	warn("%s", fname);
+	goto done;
+    }
+    if (fstat(fd, &sb)) {
+	warn("%s", fname);
+	goto done;
+    }
+    if (o.create_size) {
+	if (!S_ISREG(sb.st_mode))
+	    warnx("warning, %s is not a regular file", fname);
+    } else {
+	if (!S_ISCHR(sb.st_mode))
+	    warnx("warning, %s is not a character device", fname);
+    }
+    if (!o.no_create)
+	if (check_mounted(fname, sb.st_mode) == -1)
+	    goto done;
+    if (o.offset && o.offset != lseek(fd, o.offset, SEEK_SET)) {
+	warnx("cannot seek to %jd", (intmax_t)o.offset);
+	goto done;
+    }
+    memset(&bpb, 0, sizeof(bpb));
+    if (o.floppy) {
+	if (getstdfmt(o.floppy, &bpb) == -1)
+	    goto done;
+	bpb.bpbHugeSectors = bpb.bpbSectors;
+	bpb.bpbSectors = 0;
+	bpb.bpbBigFATsecs = bpb.bpbFATsecs;
+	bpb.bpbFATsecs = 0;
+    }
+    if (o.drive_heads)
+	bpb.bpbHeads = o.drive_heads;
+    if (o.sectors_per_track)
+	bpb.bpbSecPerTrack = o.sectors_per_track;
+    if (o.bytes_per_sector)
+	bpb.bpbBytesPerSec = o.bytes_per_sector;
+    if (o.size)
+	bpb.bpbHugeSectors = o.size;
+    if (o.hidden_sectors_set)
+	bpb.bpbHiddenSecs = o.hidden_sectors;
+    if (!(o.floppy || (o.drive_heads && o.sectors_per_track &&
+	o.bytes_per_sector && o.size && o.hidden_sectors_set))) {
+	getdiskinfo(fd, fname, dtype, o.hidden_sectors_set, &bpb);
+	bpb.bpbHugeSectors -= (o.offset / bpb.bpbBytesPerSec);
+	if (bpb.bpbSecPerClust == 0) {	/* set defaults */
+	    if (bpb.bpbHugeSectors <= 6000)	/* about 3MB -> 512 bytes */
+		bpb.bpbSecPerClust = 1;
+	    else if (bpb.bpbHugeSectors <= (1<<17)) /* 64M -> 4k */
+		bpb.bpbSecPerClust = 8;
+	    else if (bpb.bpbHugeSectors <= (1<<19)) /* 256M -> 8k */
+		bpb.bpbSecPerClust = 16;
+	    else if (bpb.bpbHugeSectors <= (1<<21)) /* 1G -> 16k */
+		bpb.bpbSecPerClust = 32;
+	    else
+		bpb.bpbSecPerClust = 64;		/* otherwise 32k */
+	}
+    }
+    if (!powerof2(bpb.bpbBytesPerSec)) {
+	warnx("bytes/sector (%u) is not a power of 2", bpb.bpbBytesPerSec);
+	goto done;
+    }
+    if (bpb.bpbBytesPerSec < MINBPS) {
+	warnx("bytes/sector (%u) is too small; minimum is %u",
+	     bpb.bpbBytesPerSec, MINBPS);
+	goto done;
+    }
+
+    if (o.volume_label && !oklabel(o.volume_label)) {
+	warnx("%s: bad volume label", o.volume_label);
+	goto done;
+    }
+    if (!(fat = o.fat_type)) {
+	if (o.floppy)
+	    fat = 12;
+	else if (!o.directory_entries && (o.info_sector || o.backup_sector))
+	    fat = 32;
+    }
+    if ((fat == 32 && o.directory_entries) || (fat != 32 && (o.info_sector || o.backup_sector))) {
+	warnx("-%c is not a legal FAT%s option",
+	     fat == 32 ? 'e' : o.info_sector ? 'i' : 'k',
+	     fat == 32 ? "32" : "12/16");
+	goto done;
+    }
+    if (o.floppy && fat == 32)
+	bpb.bpbRootDirEnts = 0;
+    if (fat != 0 && fat != 12 && fat != 16 && fat != 32) {
+	warnx("%d: bad FAT type", fat);
+	goto done;
+    }
+
+    if (o.block_size) {
+	if (!powerof2(o.block_size)) {
+	    warnx("block size (%u) is not a power of 2", o.block_size);
+	    goto done;
+	}
+	if (o.block_size < bpb.bpbBytesPerSec) {
+	    warnx("block size (%u) is too small; minimum is %u",
+		 o.block_size, bpb.bpbBytesPerSec);
+	    goto done;
+	}
+	if (o.block_size > bpb.bpbBytesPerSec * MAXSPC) {
+	    warnx("block size (%u) is too large; maximum is %u",
+		 o.block_size, bpb.bpbBytesPerSec * MAXSPC);
+	    goto done;
+	}
+	bpb.bpbSecPerClust = o.block_size / bpb.bpbBytesPerSec;
+    }
+    if (o.sectors_per_cluster) {
+	if (!powerof2(o.sectors_per_cluster)) {
+	    warnx("sectors/cluster (%u) is not a power of 2",
+		o.sectors_per_cluster);
+	    goto done;
+	}
+	bpb.bpbSecPerClust = o.sectors_per_cluster;
+    }
+    if (o.reserved_sectors)
+	bpb.bpbResSectors = o.reserved_sectors;
+    if (o.num_FAT) {
+	if (o.num_FAT > MAXNFT) {
+	    warnx("number of FATs (%u) is too large; maximum is %u",
+		 o.num_FAT, MAXNFT);
+	    goto done;
+	}
+	bpb.bpbFATs = o.num_FAT;
+    }
+    if (o.directory_entries)
+	bpb.bpbRootDirEnts = o.directory_entries;
+    if (o.media_descriptor_set) {
+	if (o.media_descriptor < 0xf0) {
+	    warnx("illegal media descriptor (%#x)", o.media_descriptor);
+	    goto done;
+	}
+	bpb.bpbMedia = o.media_descriptor;
+    }
+    if (o.sectors_per_fat)
+	bpb.bpbBigFATsecs = o.sectors_per_fat;
+    if (o.info_sector)
+	bpb.bpbFSInfo = o.info_sector;
+    if (o.backup_sector)
+	bpb.bpbBackup = o.backup_sector;
+    bss = 1;
+    bname = NULL;
+    fd1 = -1;
+    if (o.bootstrap) {
+	bname = o.bootstrap;
+	if (!strchr(bname, '/')) {
+	    snprintf(buf, sizeof(buf), "/boot/%s", bname);
+	    if (!(bname = strdup(buf))) {
+		warn(NULL);
+		goto done;
+	    }
+	}
+	if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb)) {
+	    warn("%s", bname);
+	    goto done;
+	}
+	if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bpbBytesPerSec ||
+	    sb.st_size < bpb.bpbBytesPerSec ||
+	    sb.st_size > bpb.bpbBytesPerSec * MAXU16) {
+	    warnx("%s: inappropriate file type or format", bname);
+	    goto done;
+	}
+	bss = sb.st_size / bpb.bpbBytesPerSec;
+    }
+    if (!bpb.bpbFATs)
+	bpb.bpbFATs = 2;
+    if (!fat) {
+	if (bpb.bpbHugeSectors < (bpb.bpbResSectors ? bpb.bpbResSectors : bss) +
+	    howmany((RESFTE + (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1)) *
+		(bpb.bpbSecPerClust ? 16 : 12) / BPN,
+		bpb.bpbBytesPerSec * NPB) *
+	    bpb.bpbFATs +
+	    howmany(bpb.bpbRootDirEnts ? bpb.bpbRootDirEnts : DEFRDE,
+		    bpb.bpbBytesPerSec / sizeof(struct de)) +
+	    (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1) *
+	    (bpb.bpbSecPerClust ? bpb.bpbSecPerClust :
+	     howmany(DEFBLK, bpb.bpbBytesPerSec)))
+	    fat = 12;
+	else if (bpb.bpbRootDirEnts || bpb.bpbHugeSectors <
+		 (bpb.bpbResSectors ? bpb.bpbResSectors : bss) +
+		 howmany((RESFTE + MAXCLS16) * 2, bpb.bpbBytesPerSec) *
+		 bpb.bpbFATs +
+		 howmany(DEFRDE, bpb.bpbBytesPerSec / sizeof(struct de)) +
+		 (MAXCLS16 + 1) *
+		 (bpb.bpbSecPerClust ? bpb.bpbSecPerClust :
+		  howmany(8192, bpb.bpbBytesPerSec)))
+	    fat = 16;
+	else
+	    fat = 32;
+    }
+    x = bss;
+    if (fat == 32) {
+	if (!bpb.bpbFSInfo) {
+	    if (x == MAXU16 || x == bpb.bpbBackup) {
+		warnx("no room for info sector");
+		goto done;
+	    }
+	    bpb.bpbFSInfo = x;
+	}
+	if (bpb.bpbFSInfo != MAXU16 && x <= bpb.bpbFSInfo)
+	    x = bpb.bpbFSInfo + 1;
+	if (!bpb.bpbBackup) {
+	    if (x == MAXU16) {
+		warnx("no room for backup sector");
+		goto done;
+	    }
+	    bpb.bpbBackup = x;
+	} else if (bpb.bpbBackup != MAXU16 && bpb.bpbBackup == bpb.bpbFSInfo) {
+	    warnx("backup sector would overwrite info sector");
+	    goto done;
+	}
+	if (bpb.bpbBackup != MAXU16 && x <= bpb.bpbBackup)
+	    x = bpb.bpbBackup + 1;
+    }
+    if (!bpb.bpbResSectors)
+	bpb.bpbResSectors = fat == 32 ?
+	    MAX(x, MAX(16384 / bpb.bpbBytesPerSec, 4)) : x;
+    else if (bpb.bpbResSectors < x) {
+	warnx("too few reserved sectors (need %d have %d)", x,
+	     bpb.bpbResSectors);
+	goto done;
+    }
+    if (fat != 32 && !bpb.bpbRootDirEnts)
+	bpb.bpbRootDirEnts = DEFRDE;
+    rds = howmany(bpb.bpbRootDirEnts, bpb.bpbBytesPerSec / sizeof(struct de));
+    if (!bpb.bpbSecPerClust)
+	for (bpb.bpbSecPerClust = howmany(fat == 16 ? DEFBLK16 :
+					  DEFBLK, bpb.bpbBytesPerSec);
+	     bpb.bpbSecPerClust < MAXSPC &&
+	     bpb.bpbResSectors +
+	     howmany((RESFTE + maxcls(fat)) * (fat / BPN),
+		     bpb.bpbBytesPerSec * NPB) *
+	     bpb.bpbFATs +
+	     rds +
+	     (u_int64_t) (maxcls(fat) + 1) *
+	     bpb.bpbSecPerClust <= bpb.bpbHugeSectors;
+	     bpb.bpbSecPerClust <<= 1)
+	    continue;
+    if (fat != 32 && bpb.bpbBigFATsecs > MAXU16) {
+	warnx("too many sectors/FAT for FAT12/16");
+	goto done;
+    }
+    x1 = bpb.bpbResSectors + rds;
+    x = bpb.bpbBigFATsecs ? bpb.bpbBigFATsecs : 1;
+    if (x1 + (u_int64_t)x * bpb.bpbFATs > bpb.bpbHugeSectors) {
+	warnx("meta data exceeds file system size");
+	goto done;
+    }
+    x1 += x * bpb.bpbFATs;
+    x = (u_int64_t)(bpb.bpbHugeSectors - x1) * bpb.bpbBytesPerSec * NPB /
+	(bpb.bpbSecPerClust * bpb.bpbBytesPerSec * NPB + fat /
+	 BPN * bpb.bpbFATs);
+    x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN),
+		 bpb.bpbBytesPerSec * NPB);
+    if (!bpb.bpbBigFATsecs) {
+	bpb.bpbBigFATsecs = x2;
+	x1 += (bpb.bpbBigFATsecs - 1) * bpb.bpbFATs;
+    }
+    cls = (bpb.bpbHugeSectors - x1) / bpb.bpbSecPerClust;
+    x = (u_int64_t)bpb.bpbBigFATsecs * bpb.bpbBytesPerSec * NPB / (fat / BPN) -
+	RESFTE;
+    if (cls > x)
+	cls = x;
+    if (bpb.bpbBigFATsecs < x2)
+	warnx("warning: sectors/FAT limits file system to %u clusters",
+	      cls);
+    if (cls < mincls(fat)) {
+	warnx("%u clusters too few clusters for FAT%u, need %u", cls, fat,
+	    mincls(fat));
+	goto done;
+    }
+    if (cls > maxcls(fat)) {
+	cls = maxcls(fat);
+	bpb.bpbHugeSectors = x1 + (cls + 1) * bpb.bpbSecPerClust - 1;
+	warnx("warning: FAT type limits file system to %u sectors",
+	      bpb.bpbHugeSectors);
+    }
+    printf("%s: %u sector%s in %u FAT%u cluster%s "
+	   "(%u bytes/cluster)\n", fname, cls * bpb.bpbSecPerClust,
+	   cls * bpb.bpbSecPerClust == 1 ? "" : "s", cls, fat,
+	   cls == 1 ? "" : "s", bpb.bpbBytesPerSec * bpb.bpbSecPerClust);
+    if (!bpb.bpbMedia)
+	bpb.bpbMedia = !bpb.bpbHiddenSecs ? 0xf0 : 0xf8;
+    if (fat == 32)
+	bpb.bpbRootClust = RESFTE;
+    if (bpb.bpbHugeSectors <= MAXU16) {
+	bpb.bpbSectors = bpb.bpbHugeSectors;
+	bpb.bpbHugeSectors = 0;
+    }
+    if (fat != 32) {
+	bpb.bpbFATsecs = bpb.bpbBigFATsecs;
+	bpb.bpbBigFATsecs = 0;
+    }
+    print_bpb(&bpb);
+    if (!o.no_create) {
+	gettimeofday(&tv, NULL);
+	now = tv.tv_sec;
+	tm = localtime(&now);
+	if (!(img = malloc(bpb.bpbBytesPerSec))) {
+	    warn(NULL);
+	    goto done;
+	}
+	dir = bpb.bpbResSectors + (bpb.bpbFATsecs ? bpb.bpbFATsecs :
+				   bpb.bpbBigFATsecs) * bpb.bpbFATs;
+	memset(&si_sa, 0, sizeof(si_sa));
+	si_sa.sa_handler = infohandler;
+	if (sigaction(SIGINFO, &si_sa, NULL) == -1) {
+	    warn("sigaction SIGINFO");
+	    goto done;
+	}
+	for (lsn = 0; lsn < dir + (fat == 32 ? bpb.bpbSecPerClust : rds); lsn++) {
+	    if (got_siginfo) {
+		    fprintf(stderr,"%s: writing sector %u of %u (%u%%)\n",
+			fname, lsn,
+			(dir + (fat == 32 ? bpb.bpbSecPerClust: rds)),
+			(lsn * 100) / (dir +
+			    (fat == 32 ? bpb.bpbSecPerClust: rds)));
+		    got_siginfo = 0;
+	    }
+	    x = lsn;
+	    if (o.bootstrap &&
+		fat == 32 && bpb.bpbBackup != MAXU16 &&
+		bss <= bpb.bpbBackup && x >= bpb.bpbBackup) {
+		x -= bpb.bpbBackup;
+		if (!x && lseek(fd1, o.offset, SEEK_SET)) {
+		    warn("%s", bname);
+		    goto done;
+		}
+	    }
+	    if (o.bootstrap && x < bss) {
+		if ((n = read(fd1, img, bpb.bpbBytesPerSec)) == -1) {
+		    warn("%s", bname);
+		    goto done;
+		}
+		if ((unsigned)n != bpb.bpbBytesPerSec) {
+		    warnx("%s: can't read sector %u", bname, x);
+		    goto done;
+		}
+	    } else
+		memset(img, 0, bpb.bpbBytesPerSec);
+	    if (!lsn ||
+		(fat == 32 && bpb.bpbBackup != MAXU16 &&
+		 lsn == bpb.bpbBackup)) {
+		x1 = sizeof(struct bs);
+		bsbpb = (struct bsbpb *)(img + x1);
+		mk2(bsbpb->bpbBytesPerSec, bpb.bpbBytesPerSec);
+		mk1(bsbpb->bpbSecPerClust, bpb.bpbSecPerClust);
+		mk2(bsbpb->bpbResSectors, bpb.bpbResSectors);
+		mk1(bsbpb->bpbFATs, bpb.bpbFATs);
+		mk2(bsbpb->bpbRootDirEnts, bpb.bpbRootDirEnts);
+		mk2(bsbpb->bpbSectors, bpb.bpbSectors);
+		mk1(bsbpb->bpbMedia, bpb.bpbMedia);
+		mk2(bsbpb->bpbFATsecs, bpb.bpbFATsecs);
+		mk2(bsbpb->bpbSecPerTrack, bpb.bpbSecPerTrack);
+		mk2(bsbpb->bpbHeads, bpb.bpbHeads);
+		mk4(bsbpb->bpbHiddenSecs, bpb.bpbHiddenSecs);
+		mk4(bsbpb->bpbHugeSectors, bpb.bpbHugeSectors);
+		x1 += sizeof(struct bsbpb);
+		if (fat == 32) {
+		    bsxbpb = (struct bsxbpb *)(img + x1);
+		    mk4(bsxbpb->bpbBigFATsecs, bpb.bpbBigFATsecs);
+		    mk2(bsxbpb->bpbExtFlags, 0);
+		    mk2(bsxbpb->bpbFSVers, 0);
+		    mk4(bsxbpb->bpbRootClust, bpb.bpbRootClust);
+		    mk2(bsxbpb->bpbFSInfo, bpb.bpbFSInfo);
+		    mk2(bsxbpb->bpbBackup, bpb.bpbBackup);
+		    x1 += sizeof(struct bsxbpb);
+		}
+		bsx = (struct bsx *)(img + x1);
+		mk1(bsx->exBootSignature, 0x29);
+		if (o.volume_id_set)
+		    x = o.volume_id;
+		else
+		    x = (((u_int)(1 + tm->tm_mon) << 8 |
+			  (u_int)tm->tm_mday) +
+			 ((u_int)tm->tm_sec << 8 |
+			  (u_int)(tv.tv_usec / 10))) << 16 |
+			((u_int)(1900 + tm->tm_year) +
+			 ((u_int)tm->tm_hour << 8 |
+			  (u_int)tm->tm_min));
+		mk4(bsx->exVolumeID, x);
+		mklabel(bsx->exVolumeLabel, o.volume_label ? o.volume_label : "NO NAME");
+		snprintf(buf, sizeof(buf), "FAT%u", fat);
+		setstr(bsx->exFileSysType, buf, sizeof(bsx->exFileSysType));
+		if (!o.bootstrap) {
+		    x1 += sizeof(struct bsx);
+		    bs = (struct bs *)img;
+		    mk1(bs->bsJump[0], 0xeb);
+		    mk1(bs->bsJump[1], x1 - 2);
+		    mk1(bs->bsJump[2], 0x90);
+		    setstr(bs->bsOemName, o.OEM_string ? o.OEM_string : "BSD4.4  ",
+			   sizeof(bs->bsOemName));
+		    memcpy(img + x1, bootcode, sizeof(bootcode));
+		    mk2(img + MINBPS - 2, DOSMAGIC);
+		}
+	    } else if (fat == 32 && bpb.bpbFSInfo != MAXU16 &&
+		       (lsn == bpb.bpbFSInfo ||
+			(bpb.bpbBackup != MAXU16 &&
+			 lsn == bpb.bpbBackup + bpb.bpbFSInfo))) {
+		mk4(img, 0x41615252);
+		mk4(img + MINBPS - 28, 0x61417272);
+		mk4(img + MINBPS - 24, 0xffffffff);
+		mk4(img + MINBPS - 20, bpb.bpbRootClust);
+		mk2(img + MINBPS - 2, DOSMAGIC);
+	    } else if (lsn >= bpb.bpbResSectors && lsn < dir &&
+		       !((lsn - bpb.bpbResSectors) %
+			 (bpb.bpbFATsecs ? bpb.bpbFATsecs :
+			  bpb.bpbBigFATsecs))) {
+		mk1(img[0], bpb.bpbMedia);
+		for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
+		    mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
+	    } else if (lsn == dir && o.volume_label) {
+		de = (struct de *)img;
+		mklabel(de->deName, o.volume_label);
+		mk1(de->deAttributes, 050);
+		x = (u_int)tm->tm_hour << 11 |
+		    (u_int)tm->tm_min << 5 |
+		    (u_int)tm->tm_sec >> 1;
+		mk2(de->deMTime, x);
+		x = (u_int)(tm->tm_year - 80) << 9 |
+		    (u_int)(tm->tm_mon + 1) << 5 |
+		    (u_int)tm->tm_mday;
+		mk2(de->deMDate, x);
+	    }
+	    if ((n = write(fd, img, bpb.bpbBytesPerSec)) == -1) {
+		warn("%s", fname);
+		goto done;
+	    }
+	    if ((unsigned)n != bpb.bpbBytesPerSec) {
+		warnx("%s: can't write sector %u", fname, lsn);
+		goto done;
+	    }
+	}
+    }
+    rv = 0;
+done:
+    free(img);
+
+    return rv;
+}
+
+/*
+ * return -1 with error if file system is mounted.
+ */
+static int
+check_mounted(const char *fname, mode_t mode)
+{
+    struct statfs *mp;
+    const char *s1, *s2;
+    size_t len;
+    int n, r;
+
+    if (!(n = getmntinfo(&mp, MNT_NOWAIT))) {
+	warn("getmntinfo");
+	return -1;
+    }
+    len = strlen(_PATH_DEV);
+    s1 = fname;
+    if (!strncmp(s1, _PATH_DEV, len))
+	s1 += len;
+    r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
+    for (; n--; mp++) {
+	s2 = mp->f_mntfromname;
+	if (!strncmp(s2, _PATH_DEV, len))
+	    s2 += len;
+	if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
+	    !strcmp(s1, s2)) {
+	    warnx("%s is mounted on %s", fname, mp->f_mntonname);
+	    return -1;
+	}
+    }
+    return 0;
+}
+
+/*
+ * Get a standard format.
+ */
+static int
+getstdfmt(const char *fmt, struct bpb *bpb)
+{
+    u_int x, i;
+
+    x = nitems(stdfmt);
+    for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
+    if (i == x) {
+	warnx("%s: unknown standard format", fmt);
+	return -1;
+    }
+    *bpb = stdfmt[i].bpb;
+    return 0;
+}
+
+/*
+ * Get disk slice, partition, and geometry information.
+ */
+static int
+getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
+	    struct bpb *bpb)
+{
+    struct disklabel32 *lp, dlp;
+    struct fd_type type;
+    struct partinfo pi;
+    off_t ms = 0, hs = 0;
+
+    lp = NULL;
+
+    /* If the user specified a disk type, try to use that */
+    if (dtype != NULL) {
+	lp = getdiskbyname(dtype);
+    }
+
+    /* Maybe it's a floppy drive */
+    if (lp == NULL) {
+	if (ioctl(fd, DIOCGPART, &pi) == -1) {
+	    struct stat st;
+
+	    if (fstat(fd, &st))
+		err(1, "cannot get disk size");
+	    /* create a fake geometry for a file image */
+	    ms = st.st_size;
+	    dlp.d_secsize = 512;
+	    dlp.d_nsectors = 63;
+	    dlp.d_ntracks = 255;
+	    dlp.d_secperunit = ms / dlp.d_secsize;
+	    lp = &dlp;
+	} else if (ioctl(fd, FD_GTYPE, &type) != -1) {
+	    ms = pi.media_size;
+	    dlp.d_secsize = 128 << type.secsize;
+	    dlp.d_nsectors = type.sectrac;
+	    dlp.d_ntracks = type.heads;
+	    dlp.d_secperunit = ms / dlp.d_secsize;
+	    lp = &dlp;
+	} else {
+	    ms = pi.media_size;
+	}
+    }
+
+    /* Maybe it's a fixed drive */
+    if (lp == NULL) {
+	if (ioctl(fd, DIOCGPART, &pi) == - 1)
+	    err(1, "cannot get disk information");
+	dlp.d_secsize = pi.media_blksize;
+	dlp.d_nsectors = pi.d_secpertrack;
+	dlp.d_ntracks = pi.d_nheads;
+	if (bpb->bpbBytesPerSec)
+	    dlp.d_secsize = bpb->bpbBytesPerSec;
+
+	dlp.d_secperunit = ms / dlp.d_secsize;
+
+	hs = (ms / dlp.d_secsize) - dlp.d_secperunit;
+	lp = &dlp;
+    }
+
+    if (bpb->bpbBytesPerSec == 0) {
+	if (ckgeom(fname, lp->d_secsize, "bytes/sector") == -1)
+	    return -1;
+	bpb->bpbBytesPerSec = lp->d_secsize;
+    }
+    if (bpb->bpbSecPerTrack == 0) {
+	if (ckgeom(fname, lp->d_nsectors, "sectors/track") == -1)
+	    return -1;
+	bpb->bpbSecPerTrack = lp->d_nsectors;
+    }
+    if (bpb->bpbHeads == 0) {
+	if (ckgeom(fname, lp->d_ntracks, "drive heads") == -1)
+	    return -1;
+	bpb->bpbHeads = lp->d_ntracks;
+    }
+    if (bpb->bpbHugeSectors == 0)
+	bpb->bpbHugeSectors = lp->d_secperunit;
+    if (bpb->bpbHiddenSecs == 0)
+	bpb->bpbHiddenSecs = hs;
+    return 0;
+}
+
+/*
+ * Print out BPB values.
+ */
+static void
+print_bpb(struct bpb *bpb)
+{
+    printf("BytesPerSec=%u SecPerClust=%u ResSectors=%u FATs=%u",
+	   bpb->bpbBytesPerSec, bpb->bpbSecPerClust, bpb->bpbResSectors,
+	   bpb->bpbFATs);
+    if (bpb->bpbRootDirEnts)
+	printf(" RootDirEnts=%u", bpb->bpbRootDirEnts);
+    if (bpb->bpbSectors)
+	printf(" Sectors=%u", bpb->bpbSectors);
+    printf(" Media=%#x", bpb->bpbMedia);
+    if (bpb->bpbFATsecs)
+	printf(" FATsecs=%u", bpb->bpbFATsecs);
+    printf(" SecPerTrack=%u Heads=%u HiddenSecs=%u", bpb->bpbSecPerTrack,
+	   bpb->bpbHeads, bpb->bpbHiddenSecs);
+    if (bpb->bpbHugeSectors)
+	printf(" HugeSectors=%u", bpb->bpbHugeSectors);
+    if (!bpb->bpbFATsecs) {
+	printf(" FATsecs=%u RootCluster=%u", bpb->bpbBigFATsecs,
+	       bpb->bpbRootClust);
+	printf(" FSInfo=");
+	printf(bpb->bpbFSInfo == MAXU16 ? "%#x" : "%u", bpb->bpbFSInfo);
+	printf(" Backup=");
+	printf(bpb->bpbBackup == MAXU16 ? "%#x" : "%u", bpb->bpbBackup);
+    }
+    printf("\n");
+}
+
+/*
+ * Check a disk geometry value.
+ */
+static int
+ckgeom(const char *fname, u_int val, const char *msg)
+{
+    if (!val) {
+	warnx("%s: no default %s", fname, msg);
+	return -1;
+    }
+    if (val > MAXU16) {
+	warnx("%s: illegal %s %d", fname, msg, val);
+	return -1;
+    }
+    return 0;
+}
+
+/*
+ * Check a volume label.
+ */
+static int
+oklabel(const char *src)
+{
+    int c, i;
+
+    for (i = 0; i <= 11; i++) {
+	c = (u_char)*src++;
+	if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
+	    break;
+    }
+    return i && !c;
+}
+
+/*
+ * Make a volume label.
+ */
+static void
+mklabel(u_int8_t *dest, const char *src)
+{
+    int c, i;
+
+    for (i = 0; i < 11; i++) {
+	c = *src ? toupper(*src++) : ' ';
+	*dest++ = !i && c == '\xe5' ? 5 : c;
+    }
+}
+
+/*
+ * Copy string, padding with spaces.
+ */
+static void
+setstr(u_int8_t *dest, const char *src, size_t len)
+{
+    while (len--)
+	*dest++ = *src ? *src++ : ' ';
+}
+
+static void
+infohandler(int sig __unused)
+{
+
+	got_siginfo = 1;
+}
diff --git a/sbin/newfs_msdos/mkfs_msdos.h b/sbin/newfs_msdos/mkfs_msdos.h
new file mode 100644
index 0000000..0cff19d
--- /dev/null
+++ b/sbin/newfs_msdos/mkfs_msdos.h
@@ -0,0 +1,69 @@
+/*	$FreeBSD: head/sbin/newfs_msdos/mkfs_msdos.h 291218 2015-11-23 18:56:10Z emaste $	*/
+/*	$NetBSD: mkfs_msdos.h,v 1.3 2015/10/16 17:38:17 christos Exp $	*/
+
+/*-
+ * Copyright (c) 2013 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <stdbool.h>
+#define ALLOPTS \
+AOPT('@', off_t, offset, 0, "Offset in device") \
+AOPT('B', const char *, bootstrap, -1, "Bootstrap file") \
+AOPT('C', off_t, create_size, 0, "Create file") \
+AOPT('F', uint8_t,  fat_type, 12, "FAT type (12, 16, or 32)") \
+AOPT('I', uint32_t, volume_id, 0, "Volume ID") \
+AOPT('L', const char *, volume_label, -1, "Volume Label") \
+AOPT('N', bool, no_create, -2, "Don't create filesystem, print params only") \
+AOPT('O', const char *, OEM_string, -1, "OEM string") \
+AOPT('S', uint16_t, bytes_per_sector, 1, "Bytes per sector") \
+AOPT('a', uint32_t, sectors_per_fat, 1, "Sectors per FAT") \
+AOPT('b', uint32_t, block_size, 1, "Block size") \
+AOPT('c', uint8_t, sectors_per_cluster, 1, "Sectors per cluster") \
+AOPT('e', uint16_t, directory_entries, 1, "Directory entries") \
+AOPT('f', const char *, floppy, -1, "Standard format floppies (160,180,320,360,640,720,1200,1232,1440,2880)") \
+AOPT('h', uint16_t, drive_heads, 1, "Drive heads") \
+AOPT('i', uint16_t, info_sector, 1, "Info sector") \
+AOPT('k', uint16_t, backup_sector, 1, "Backup sector") \
+AOPT('m', uint8_t, media_descriptor, 0, "Media descriptor") \
+AOPT('n', uint8_t, num_FAT, 1, "Number of FATs") \
+AOPT('o', uint32_t, hidden_sectors, 0, "Hidden sectors") \
+AOPT('r', uint16_t, reserved_sectors, 1, "Reserved sectors") \
+AOPT('s', uint32_t, size, 1, "File System size") \
+AOPT('u', uint16_t, sectors_per_track, 1, "Sectors per track")
+
+struct msdos_options {
+#define AOPT(_opt, _type, _name, _min, _desc) _type _name;
+ALLOPTS
+#undef AOPT	
+	uint32_t volume_id_set:1;
+	uint32_t media_descriptor_set:1;
+	uint32_t hidden_sectors_set:1;
+};
+
+int mkfs_msdos(const char *, const char *, const struct msdos_options *);
diff --git a/sbin/newfs_msdos/newfs_msdos.8 b/sbin/newfs_msdos/newfs_msdos.8
index 571c2ca..6f7cafc 100644
--- a/sbin/newfs_msdos/newfs_msdos.8
+++ b/sbin/newfs_msdos/newfs_msdos.8
@@ -23,9 +23,9 @@
 .\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 .\"
-.\" $FreeBSD: src/sbin/newfs_msdos/newfs_msdos.8,v 1.7.2.7 2003/02/24 00:56:42 trhodes Exp $
+.\" $FreeBSD: head/sbin/newfs_msdos/newfs_msdos.8 287396 2015-09-02 14:08:43Z trasz $
 .\"
-.Dd July 6, 1998
+.Dd December 5, 2016
 .Dt NEWFS_MSDOS 8
 .Os
 .Sh NAME
@@ -34,16 +34,18 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl N
+.Op Fl @ Ar offset
 .Op Fl B Ar boot
+.Op Fl C Ar create-size
 .Op Fl F Ar FAT-type
-.Op Fl I Ar volid
+.Op Fl I Ar VolumeId
 .Op Fl L Ar label
 .Op Fl O Ar OEM
 .Op Fl S Ar sector-size
 .Op Fl a Ar FAT-size
 .Op Fl b Ar block-size
 .Op Fl c Ar cluster-size
-.Op Fl e Ar dirents
+.Op Fl e Ar DirEnts
 .Op Fl f Ar format
 .Op Fl h Ar heads
 .Op Fl i Ar info
@@ -59,7 +61,7 @@
 .Sh DESCRIPTION
 The
 .Nm
-utility creates a FAT12, FAT16, or FAT32 file system on device
+utility creates a FAT12, FAT16, or FAT32 file system on device or file named
 .Ar special ,
 using
 .Xr disktab 5
@@ -67,37 +69,72 @@ entry
 .Ar disktype
 to determine geometry, if required.
 .Pp
+If
+.Ar special
+does not contain a
+.Ar /
+and
+.Fl C
+is not used, it is assumed to be a device name and
+.Ar /dev/
+is prepended to the name to construct the actual device name.
+To work a file in the current directory use
+.Ar ./filename
+.Pp
 The options are as follows:
 .Bl -tag -width indent
 .It Fl N
-Don't create a file system: just print out parameters.
+Do not create a file system: just print out parameters.
+.It Fl @ Ar offset
+Build the filesystem at the specified offset in bytes in the device or file.
+A suffix s, k, m, g (lower or upper case)
+appended to the offset specifies that the
+number is in sectors, kilobytes, megabytes or gigabytes, respectively.
 .It Fl B Ar boot
 Get bootstrap from file.
+.It Fl C Ar create-size
+Create the image file with the specified size.
+A suffix character appended to the size is interpreted as for the
+.Fl @
+option.
+The file is created by truncating any existing file with the same name and
+resizing it to the requested size.
+If the file system supports sparse files, the space occupied on disk may be
+smaller than the size specified as parameter.
 .It Fl F Ar FAT-type
 FAT type (one of 12, 16, or 32).
-.It Fl I Ar volid
-Volume ID.
+.It Fl I Ar VolumeID
+Volume ID, a 32 bit number in decimal or hexadecimal (0x...) format.
 .It Fl L Ar label
-Volume label (up to 11 characters).  The label should consist of
+Volume label (up to 11 characters).
+The label should consist of
 only those characters permitted in regular DOS (8+3) filenames.
 .It Fl O Ar OEM
-OEM string (up to 8 characters).  The default is
-.Qq Li "BSD  4.4" .
+OEM string (up to 8 characters).
+The default is
+.Qq Li "BSD4.4  " .
 .It Fl S Ar sector-size
-Number of bytes per sector.  Acceptable values are powers of 2
-in the range 128 through 32768.
+Number of bytes per sector.
+Acceptable values are powers of 2
+in the range 512 through 32768, inclusive.
 .It Fl a Ar FAT-size
 Number of sectors per FAT.
 .It Fl b Ar block-size
-File system block size (bytes per cluster).  This should resolve to an
+File system block size (bytes per cluster).
+This should resolve to an
 acceptable number of sectors per cluster (see below).
 .It Fl c Ar cluster-size
-Sectors per cluster.  Acceptable values are powers of 2 in the range
+Sectors per cluster.
+Acceptable values are powers of 2 in the range
 1 through 128.
-.It Fl e Ar dirents
+If the block or cluster size are not specified, the code
+uses a cluster between 512 bytes and 32K depending on
+the filesystem size.
+.It Fl e Ar DirEnts
 Number of root directory entries (FAT12 and FAT16 only).
 .It Fl f Ar format
-Specify a standard (floppy disk) format.  The standard formats
+Specify a standard (floppy disk) format.
+The standard formats
 are (capacities in kilobytes): 160, 180, 320, 360, 640, 720, 1200,
 1232, 1440, 2880.
 .It Fl h Ar heads
@@ -106,12 +143,14 @@ Number of drive heads.
 Location of the file system info sector (FAT32 only).
 A value of 0xffff signifies no info sector.
 .It Fl k Ar backup
-Location of the backup boot sector (FAT32 only).  A value
+Location of the backup boot sector (FAT32 only).
+A value
 of 0xffff signifies no backup sector.
 .It Fl m Ar media
 Media descriptor (acceptable range 0xf0 to 0xff).
 .It Fl n Ar FATs
-Number of FATs.  Acceptable values are 1 to 16 inclusive.
+Number of FATs.
+Acceptable values are 1 to 16 inclusive.
 The default
 is 2.
 .It Fl o Ar hidden
@@ -124,52 +163,70 @@ File system size.
 Number of sectors per track.
 .El
 .Sh NOTES
+If some parameters (e.g. size, number of sectors, etc.) are not specified
+through options or disktype, the program tries to generate them automatically.
+In particular, the size is determined as the device or file size minus the
+offset specified with the
+.Fl @
+option.
+When the geometry is not available, it is assumed to be 63 sectors, 255 heads.
+The size is then rounded to become a multiple of the track size and avoid
+complaints by some filesystem code.
+.Pp
 FAT file system parameters occupy a "Boot Sector BPB (BIOS Parameter
 Block)" in the first of the "reserved" sectors which precede the actual
-file system.  For reference purposes, this structure is presented
+file system.
+For reference purposes, this structure is presented
 below.
 .Bd -literal
 struct bsbpb {
-    u_int16_t	bps;		/* [-S] bytes per sector */
-    u_int8_t	spc;		/* [-c] sectors per cluster */
-    u_int16_t	res;		/* [-r] reserved sectors */
-    u_int8_t	nft;		/* [-n] number of FATs */
-    u_int16_t	rde;		/* [-e] root directory entries */
-    u_int16_t	sec;		/* [-s] total sectors */
-    u_int8_t	mid;		/* [-m] media descriptor */
-    u_int16_t	spf;		/* [-a] sectors per FAT */
-    u_int16_t	spt;		/* [-u] sectors per track */
-    u_int16_t	hds;		/* [-h] drive heads */
-    u_int32_t	hid;		/* [-o] hidden sectors */
-    u_int32_t	bsec;		/* [-s] big total sectors */
+    uint16_t	bpbBytesPerSec;		/* [-S] bytes per sector */
+    uint8_t	bpbSecPerClust;		/* [-c] sectors per cluster */
+    uint16_t	bpbResSectors;		/* [-r] reserved sectors */
+    uint8_t	bpbFATs;		/* [-n] number of FATs */
+    uint16_t	bpbRootDirEnts;		/* [-e] root directory entries */
+    uint16_t	bpbSectors;		/* [-s] total sectors */
+    uint8_t	bpbMedia;		/* [-m] media descriptor */
+    uint16_t	bpbFATsecs;		/* [-a] sectors per FAT */
+    uint16_t	bpbSecPerTrack;		/* [-u] sectors per track */
+    uint16_t	bpbHeads;		/* [-h] drive heads */
+    uint32_t	bpbHiddenSecs;		/* [-o] hidden sectors */
+    uint32_t	bpbHugeSectors;		/* [-s] big total sectors */
 };
 /* FAT32 extensions */
 struct bsxbpb {
-    u_int32_t	bspf;		/* [-a] big sectors per FAT */
-    u_int16_t	xflg;		/* control flags */
-    u_int16_t	vers;		/* file system version */
-    u_int32_t	rdcl;		/* root directory start cluster */
-    u_int16_t	infs;		/* [-i] file system info sector */
-    u_int16_t	bkbs;		/* [-k] backup boot sector */
+    uint32_t	bpbBigFATsecs;		/* [-a] big sectors per FAT */
+    uint16_t	bpbExtFlags;		/* control flags */
+    uint16_t	bpbFSVers;		/* file system version */
+    uint32_t	bpbRootClust;		/* root directory start cluster */
+    uint16_t	bpbFSInfo;		/* [-i] file system info sector */
+    uint16_t	bpbBackup;		/* [-k] backup boot sector */
 };
 .Ed
+.Sh LIMITATION
+The maximum file size is 4GB, even if the file system itself is bigger.
 .Sh EXIT STATUS
 .Ex -std
 .Sh EXAMPLES
-.Bd -literal -offset indent
-newfs_msdos /dev/ad0s1
-.Ed
-.Pp
 Create a file system, using default parameters, on
-.Pa /dev/ad0s1 .
+.Pa /dev/da0s1 :
 .Bd -literal -offset indent
-newfs_msdos -f 1440 -L foo fd0
+newfs_msdos /dev/da0s1
 .Ed
 .Pp
 Create a standard 1.44M file system, with volume label
 .Ar foo ,
 on
-.Pa /dev/fd0 .
+.Pa /dev/fd0 :
+.Bd -literal -offset indent
+newfs_msdos -f 1440 -L foo fd0
+.Ed
+.Pp
+Create a 30MB image file, with the FAT partition starting
+63 sectors within the image file:
+.Bd -literal -offset indent
+newfs_msdos -C 30M -@63s ./somefile
+.Ed
 .Sh SEE ALSO
 .Xr disktab 5 ,
 .Xr disklabel 8 ,
diff --git a/sbin/newfs_msdos/newfs_msdos.c b/sbin/newfs_msdos/newfs_msdos.c
index 5540477..3f53cc3 100644
--- a/sbin/newfs_msdos/newfs_msdos.c
+++ b/sbin/newfs_msdos/newfs_msdos.c
@@ -24,199 +24,28 @@
  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
- * $FreeBSD: src/sbin/newfs_msdos/newfs_msdos.c,v 1.9.2.4 2001/08/01 08:43:04 obrien Exp $
- * $DragonFly: src/sbin/newfs_msdos/newfs_msdos.c,v 1.6 2007/07/30 23:56:29 dillon Exp $
+ * $FreeBSD: head/sbin/newfs_msdos/newfs_msdos.c 291385 2015-11-27 14:40:21Z emaste $
  */
 
 #include <sys/param.h>
-#include <sys/diskslice.h>
-#include <sys/diskmbr.h>
-#include <sys/mount.h>
-#include <sys/stat.h>
-#include <sys/time.h>
 
-#include <ctype.h>
 #include <err.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <paths.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <time.h>
 #include <unistd.h>
-#include <disktab.h>
 
-#define MAXU16	  0xffff	/* maximum unsigned 16-bit quantity */
-#define BPN	  4		/* bits per nibble */
-#define NPB	  2		/* nibbles per byte */
+#include "mkfs_msdos.h"
 
-#define MINBPS	  128		/* minimum bytes per sector */
-#define MAXSPC	  128		/* maximum sectors per cluster */
-#define MAXNFT	  16		/* maximum number of FATs */
-#define DEFBLK	  4096		/* default block size */
-#define DEFBLK16  2048		/* default block size FAT16 */
-#define DEFRDE	  512		/* default root directory entries */
-#define RESFTE	  2		/* reserved FAT entries */
-#define MINCLS12  1		/* minimum FAT12 clusters */
-#define MINCLS16  0x1000	/* minimum FAT16 clusters */
-#define MINCLS32  2		/* minimum FAT32 clusters */
-#define MAXCLS12  0xfed 	/* maximum FAT12 clusters */
-#define MAXCLS16  0xfff5	/* maximum FAT16 clusters */
-#define MAXCLS32  0xffffff5	/* maximum FAT32 clusters */
+#define	argto1(arg, lo, msg)  argtou(arg, lo, 0xff, msg)
+#define	argto2(arg, lo, msg)  argtou(arg, lo, 0xffff, msg)
+#define	argto4(arg, lo, msg)  argtou(arg, lo, 0xffffffff, msg)
+#define	argtox(arg, lo, msg)  argtou(arg, lo, UINT_MAX, msg)
 
-#define mincls(fat)  ((fat) == 12 ? MINCLS12 :	\
-		      (fat) == 16 ? MINCLS16 :	\
-				    MINCLS32)
-
-#define maxcls(fat)  ((fat) == 12 ? MAXCLS12 :	\
-		      (fat) == 16 ? MAXCLS16 :	\
-				    MAXCLS32)
-
-#define mk1(p, x)				\
-    (p) = (u_int8_t)(x)
-
-#define mk2(p, x)				\
-    (p)[0] = (u_int8_t)(x),			\
-    (p)[1] = (u_int8_t)((x) >> 010)
-
-#define mk4(p, x)				\
-    (p)[0] = (u_int8_t)(x),			\
-    (p)[1] = (u_int8_t)((x) >> 010),		\
-    (p)[2] = (u_int8_t)((x) >> 020),		\
-    (p)[3] = (u_int8_t)((x) >> 030)
-
-#define argto1(arg, lo, msg)  argtou(arg, lo, 0xff, msg)
-#define argto2(arg, lo, msg)  argtou(arg, lo, 0xffff, msg)
-#define argto4(arg, lo, msg)  argtou(arg, lo, 0xffffffff, msg)
-#define argtox(arg, lo, msg)  argtou(arg, lo, UINT_MAX, msg)
-
-struct bs {
-    u_int8_t jmp[3];		/* bootstrap entry point */
-    u_int8_t oem[8];		/* OEM name and version */
-};
-
-struct bsbpb {
-    u_int8_t bps[2];		/* bytes per sector */
-    u_int8_t spc;		/* sectors per cluster */
-    u_int8_t res[2];		/* reserved sectors */
-    u_int8_t nft;		/* number of FATs */
-    u_int8_t rde[2];		/* root directory entries */
-    u_int8_t sec[2];		/* total sectors */
-    u_int8_t mid;		/* media descriptor */
-    u_int8_t spf[2];		/* sectors per FAT */
-    u_int8_t spt[2];		/* sectors per track */
-    u_int8_t hds[2];		/* drive heads */
-    u_int8_t hid[4];		/* hidden sectors */
-    u_int8_t bsec[4];		/* big total sectors */
-};
-
-struct bsxbpb {
-    u_int8_t bspf[4];		/* big sectors per FAT */
-    u_int8_t xflg[2];		/* FAT control flags */
-    u_int8_t vers[2];		/* file system version */
-    u_int8_t rdcl[4];		/* root directory start cluster */
-    u_int8_t infs[2];		/* file system info sector */
-    u_int8_t bkbs[2];		/* backup boot sector */
-    u_int8_t rsvd[12];		/* reserved */
-};
-
-struct bsx {
-    u_int8_t drv;		/* drive number */
-    u_int8_t rsvd;		/* reserved */
-    u_int8_t sig;		/* extended boot signature */
-    u_int8_t volid[4];		/* volume ID number */
-    u_int8_t label[11]; 	/* volume label */
-    u_int8_t type[8];		/* file system type */
-};
-
-struct de {
-    u_int8_t namext[11];	/* name and extension */
-    u_int8_t attr;		/* attributes */
-    u_int8_t rsvd[10];		/* reserved */
-    u_int8_t time[2];		/* creation time */
-    u_int8_t date[2];		/* creation date */
-    u_int8_t clus[2];		/* starting cluster */
-    u_int8_t size[4];		/* size */
-};
-
-struct bpb {
-    u_int bps;			/* bytes per sector */
-    u_int spc;			/* sectors per cluster */
-    u_int res;			/* reserved sectors */
-    u_int nft;			/* number of FATs */
-    u_int rde;			/* root directory entries */
-    u_int sec;			/* total sectors */
-    u_int mid;			/* media descriptor */
-    u_int spf;			/* sectors per FAT */
-    u_int spt;			/* sectors per track */
-    u_int hds;			/* drive heads */
-    u_int hid;			/* hidden sectors */
-    u_int bsec; 		/* big total sectors */
-    u_int bspf; 		/* big sectors per FAT */
-    u_int rdcl; 		/* root directory start cluster */
-    u_int infs; 		/* file system info sector */
-    u_int bkbs; 		/* backup boot sector */
-};
-
-static struct {
-    const char *name;
-    struct bpb bpb;
-} stdfmt[] = {
-    {"160",  {512, 1, 1, 2,  64,  320, 0xfe, 1,  8, 1}},
-    {"180",  {512, 1, 1, 2,  64,  360, 0xfc, 2,  9, 1}},
-    {"320",  {512, 2, 1, 2, 112,  640, 0xff, 1,  8, 2}},
-    {"360",  {512, 2, 1, 2, 112,  720, 0xfd, 2,  9, 2}},
-    {"640",  {512, 2, 1, 2, 112, 1280, 0xfb, 2,  8, 2}},    
-    {"720",  {512, 2, 1, 2, 112, 1440, 0xf9, 3,  9, 2}},
-    {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2}},
-    {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2,  8, 2}},    
-    {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2}},
-    {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2}}
-};
-
-static u_int8_t bootcode[] = {
-    0xfa,			/* cli		    */
-    0x31, 0xc0, 		/* xor	   ax,ax    */
-    0x8e, 0xd0, 		/* mov	   ss,ax    */
-    0xbc, 0x00, 0x7c,		/* mov	   sp,7c00h */
-    0xfb,			/* sti		    */
-    0x8e, 0xd8, 		/* mov	   ds,ax    */
-    0xe8, 0x00, 0x00,		/* call    $ + 3    */
-    0x5e,			/* pop	   si	    */
-    0x83, 0xc6, 0x19,		/* add	   si,+19h  */
-    0xbb, 0x07, 0x00,		/* mov	   bx,0007h */
-    0xfc,			/* cld		    */
-    0xac,			/* lodsb	    */
-    0x84, 0xc0, 		/* test    al,al    */
-    0x74, 0x06, 		/* jz	   $ + 8    */
-    0xb4, 0x0e, 		/* mov	   ah,0eh   */
-    0xcd, 0x10, 		/* int	   10h	    */
-    0xeb, 0xf5, 		/* jmp	   $ - 9    */
-    0x30, 0xe4, 		/* xor	   ah,ah    */
-    0xcd, 0x16, 		/* int	   16h	    */
-    0xcd, 0x19, 		/* int	   19h	    */
-    0x0d, 0x0a,
-    'N', 'o', 'n', '-', 's', 'y', 's', 't',
-    'e', 'm', ' ', 'd', 'i', 's', 'k',
-    0x0d, 0x0a,
-    'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
-    'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
-    ' ', 'r', 'e', 'b', 'o', 'o', 't',
-    0x0d, 0x0a,
-    0
-};
-
-static void check_mounted(const char *, mode_t);
-static void getstdfmt(const char *, struct bpb *);
-static void getdiskinfo(int, const char *, const char *, int,
-			struct bpb *);
-static void print_bpb(struct bpb *);
-static u_int ckgeom(const char *, u_int, const char *);
 static u_int argtou(const char *, u_int, u_int, const char *);
-static int oklabel(const char *);
-static void mklabel(u_int8_t *, const char *);
-static void setstr(u_int8_t *, const char *, size_t);
+static off_t argtooff(const char *, const char *);
 static void usage(void);
 
 /*
@@ -225,107 +54,93 @@ static void usage(void);
 int
 main(int argc, char *argv[])
 {
-    static char opts[] = "NB:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
-    static const char *opt_B, *opt_L, *opt_O, *opt_f;
-    static u_int opt_F, opt_I, opt_S, opt_a, opt_b, opt_c, opt_e;
-    static u_int opt_h, opt_i, opt_k, opt_m, opt_n, opt_o, opt_r;
-    static u_int opt_s, opt_u;
-    static int opt_N;
-    static int Iflag, mflag, oflag;
+    static const char opts[] = "@:NB:C:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
+    struct msdos_options o;
+    const char *fname, *dtype;
     char buf[MAXPATHLEN];
-    struct stat sb;
-    struct timeval tv;
-    struct bpb bpb;
-    struct tm *tm;
-    struct bs *bs;
-    struct bsbpb *bsbpb;
-    struct bsxbpb *bsxbpb;
-    struct bsx *bsx;
-    struct de *de;
-    u_int8_t *img;
-    const char *fname, *dtype, *bname;
-    ssize_t n;
-    time_t now;
-    u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
-    int ch, fd, fd1;
+    int ch;
+
+    memset(&o, 0, sizeof(o));
 
     while ((ch = getopt(argc, argv, opts)) != -1)
 	switch (ch) {
+	case '@':
+	    o.offset = argtooff(optarg, "offset");
+	    break;
 	case 'N':
-	    opt_N = 1;
+	    o.no_create = 1;
 	    break;
 	case 'B':
-	    opt_B = optarg;
+	    o.bootstrap = optarg;
+	    break;
+	case 'C':
+	    o.create_size = argtooff(optarg, "create size");
 	    break;
 	case 'F':
 	    if (strcmp(optarg, "12") &&
 		strcmp(optarg, "16") &&
 		strcmp(optarg, "32"))
 		errx(1, "%s: bad FAT type", optarg);
-	    opt_F = atoi(optarg);
+	    o.fat_type = atoi(optarg);
 	    break;
 	case 'I':
-	    opt_I = argto4(optarg, 0, "volume ID");
-	    Iflag = 1;
+	    o.volume_id = argto4(optarg, 0, "volume ID");
+	    o.volume_id_set = 1;
 	    break;
 	case 'L':
-	    if (!oklabel(optarg))
-		errx(1, "%s: bad volume label", optarg);
-	    opt_L = optarg;
+	    o.volume_label = optarg;
 	    break;
 	case 'O':
-	    if (strlen(optarg) > 8)
-		errx(1, "%s: bad OEM string", optarg);
-	    opt_O = optarg;
+	    o.OEM_string = optarg;
 	    break;
 	case 'S':
-	    opt_S = argto2(optarg, 1, "bytes/sector");
+	    o.bytes_per_sector = argto2(optarg, 1, "bytes/sector");
 	    break;
 	case 'a':
-	    opt_a = argto4(optarg, 1, "sectors/FAT");
+	    o.sectors_per_fat = argto4(optarg, 1, "sectors/FAT");
 	    break;
 	case 'b':
-	    opt_b = argtox(optarg, 1, "block size");
-	    opt_c = 0;
+	    o.block_size = argtox(optarg, 1, "block size");
+	    o.sectors_per_cluster = 0;
 	    break;
 	case 'c':
-	    opt_c = argto1(optarg, 1, "sectors/cluster");
-	    opt_b = 0;
+	    o.sectors_per_cluster = argto1(optarg, 1, "sectors/cluster");
+	    o.block_size = 0;
 	    break;
 	case 'e':
-	    opt_e = argto2(optarg, 1, "directory entries");
+	    o.directory_entries = argto2(optarg, 1, "directory entries");
 	    break;
 	case 'f':
-	    opt_f = optarg;
+	    o.floppy = optarg;
 	    break;
 	case 'h':
-	    opt_h = argto2(optarg, 1, "drive heads");
+	    o.drive_heads = argto2(optarg, 1, "drive heads");
 	    break;
 	case 'i':
-	    opt_i = argto2(optarg, 1, "info sector");
+	    o.info_sector = argto2(optarg, 1, "info sector");
 	    break;
 	case 'k':
-	    opt_k = argto2(optarg, 1, "backup sector");
+	    o.backup_sector = argto2(optarg, 1, "backup sector");
 	    break;
 	case 'm':
-	    opt_m = argto1(optarg, 0, "media descriptor");
-	    mflag = 1;
+	    o.media_descriptor = argto1(optarg, 0, "media descriptor");
+	    o.media_descriptor_set = 1;
 	    break;
 	case 'n':
-	    opt_n = argto1(optarg, 1, "number of FATs");
+	    o.num_FAT = argto1(optarg, 1, "number of FATs");
 	    break;
 	case 'o':
-	    opt_o = argto4(optarg, 0, "hidden sectors");
-	    oflag = 1;
+	    o.hidden_sectors = argto4(optarg, 0, "hidden sectors");
+	    o.hidden_sectors_set = 1;
 	    break;
 	case 'r':
-	    opt_r = argto2(optarg, 1, "reserved sectors");
+	    o.reserved_sectors = argto2(optarg, 1, "reserved sectors");
 	    break;
 	case 's':
-	    opt_s = argto4(optarg, 1, "file system size");
+	    o.size = argto4(optarg, 1, "file system size");
 	    break;
 	case 'u':
-	    opt_u = argto2(optarg, 1, "sectors/track");
+	    o.sectors_per_track = argto2(optarg, 1, "sectors/track");
 	    break;
 	default:
 	    usage();
@@ -335,496 +150,13 @@ main(int argc, char *argv[])
     if (argc < 1 || argc > 2)
 	usage();
     fname = *argv++;
-    if (!strchr(fname, '/')) {
+    if (!o.create_size && !strchr(fname, '/')) {
 	snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
 	if (!(fname = strdup(buf)))
 	    err(1, NULL);
     }
     dtype = *argv;
-    if ((fd = open(fname, opt_N ? O_RDONLY : O_RDWR)) == -1 ||
-	fstat(fd, &sb))
-	err(1, "%s", fname);
-    if (!opt_N)
-	check_mounted(fname, sb.st_mode);
-    if (!S_ISCHR(sb.st_mode))
-	errx(1, "%s is not a character device", fname);
-    memset(&bpb, 0, sizeof(bpb));
-    if (opt_f) {
-	getstdfmt(opt_f, &bpb);
-	bpb.bsec = bpb.sec;
-	bpb.sec = 0;
-	bpb.bspf = bpb.spf;
-	bpb.spf = 0;
-    }
-    if (opt_h)
-	bpb.hds = opt_h;
-    if (opt_u)
-	bpb.spt = opt_u;
-    if (opt_S)
-	bpb.bps = opt_S;
-    if (opt_s)
-	bpb.bsec = opt_s;
-    if (oflag)
-	bpb.hid = opt_o;
-    if (!(opt_f || (opt_h && opt_u && opt_S && opt_s && oflag)))
-	getdiskinfo(fd, fname, dtype, oflag, &bpb);
-    if (!powerof2(bpb.bps))
-	errx(1, "bytes/sector (%u) is not a power of 2", bpb.bps);
-    if (bpb.bps < MINBPS)
-	errx(1, "bytes/sector (%u) is too small; minimum is %u",
-	     bpb.bps, MINBPS);
-    if (!(fat = opt_F)) {
-	if (opt_f)
-	    fat = 12;
-	else if (!opt_e && (opt_i || opt_k))
-	    fat = 32;
-    }
-    if ((fat == 32 && opt_e) || (fat != 32 && (opt_i || opt_k)))
-	errx(1, "-%c is not a legal FAT%s option",
-	     fat == 32 ? 'e' : opt_i ? 'i' : 'k',
-	     fat == 32 ? "32" : "12/16");
-    if (opt_f && fat == 32)
-	bpb.rde = 0;
-    if (opt_b) {
-	if (!powerof2(opt_b))
-	    errx(1, "block size (%u) is not a power of 2", opt_b);
-	if (opt_b < bpb.bps)
-	    errx(1, "block size (%u) is too small; minimum is %u",
-		 opt_b, bpb.bps);
-	if (opt_b > bpb.bps * MAXSPC)
-	    errx(1, "block size (%u) is too large; maximum is %u",
-		 opt_b, bpb.bps * MAXSPC);
-	bpb.spc = opt_b / bpb.bps;
-    }
-    if (opt_c) {
-	if (!powerof2(opt_c))
-	    errx(1, "sectors/cluster (%u) is not a power of 2", opt_c);
-	bpb.spc = opt_c;
-    }
-    if (opt_r)
-	bpb.res = opt_r;
-    if (opt_n) {
-	if (opt_n > MAXNFT)
-	    errx(1, "number of FATs (%u) is too large; maximum is %u",
-		 opt_n, MAXNFT);
-	bpb.nft = opt_n;
-    }
-    if (opt_e)
-	bpb.rde = opt_e;
-    if (mflag) {
-	if (opt_m < 0xf0)
-	    errx(1, "illegal media descriptor (%#x)", opt_m);
-	bpb.mid = opt_m;
-    }
-    if (opt_a)
-	bpb.bspf = opt_a;
-    if (opt_i)
-	bpb.infs = opt_i;
-    if (opt_k)
-	bpb.bkbs = opt_k;
-    bss = 1;
-    bname = NULL;
-    fd1 = -1;
-    if (opt_B) {
-	bname = opt_B;
-	if (!strchr(bname, '/')) {
-	    snprintf(buf, sizeof(buf), "/boot/%s", bname);
-	    if (!(bname = strdup(buf)))
-		err(1, NULL);
-	}
-	if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb))
-	    err(1, "%s", bname);
-	if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||
-	    sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16)
-	    errx(1, "%s: inappropriate file type or format", bname);
-	bss = sb.st_size / bpb.bps;
-    }
-    if (!bpb.nft)
-	bpb.nft = 2;
-    if (!fat) {
-	if (bpb.bsec < (bpb.res ? bpb.res : bss) +
-	    howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *
-		    ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *
-	    bpb.nft +
-	    howmany(bpb.rde ? bpb.rde : DEFRDE,
-		    bpb.bps / sizeof(struct de)) +
-	    (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *
-	    (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))
-	    fat = 12;
-	else if (bpb.rde || bpb.bsec <
-		 (bpb.res ? bpb.res : bss) +
-		 howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +
-		 howmany(DEFRDE, bpb.bps / sizeof(struct de)) +
-		 (MAXCLS16 + 1) *
-		 (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))
-	    fat = 16;
-	else
-	    fat = 32;
-    }
-    x = bss;
-    if (fat == 32) {
-	if (!bpb.infs) {
-	    if (x == MAXU16 || x == bpb.bkbs)
-		errx(1, "no room for info sector");
-	    bpb.infs = x;
-	}
-	if (bpb.infs != MAXU16 && x <= bpb.infs)
-	    x = bpb.infs + 1;
-	if (!bpb.bkbs) {
-	    if (x == MAXU16)
-		errx(1, "no room for backup sector");
-	    bpb.bkbs = x;
-	} else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs)
-	    errx(1, "backup sector would overwrite info sector");
-	if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)
-	    x = bpb.bkbs + 1;
-    }
-    if (!bpb.res)
-	bpb.res = fat == 32 ? MAX(x, MAX(16384 / bpb.bps, 4)) : x;
-    else if (bpb.res < x)
-	errx(1, "too few reserved sectors");
-    if (fat != 32 && !bpb.rde)
-	bpb.rde = DEFRDE;
-    rds = howmany(bpb.rde, bpb.bps / sizeof(struct de));
-    if (!bpb.spc)
-	for (bpb.spc = howmany(fat == 16 ? DEFBLK16 : DEFBLK, bpb.bps);
-	     bpb.spc < MAXSPC &&
-	     bpb.res +
-	     howmany((RESFTE + maxcls(fat)) * (fat / BPN),
-		     bpb.bps * NPB) * bpb.nft +
-	     rds +
-	     (u_int64_t)(maxcls(fat) + 1) * bpb.spc <= bpb.bsec;
-	     bpb.spc <<= 1);
-    if (fat != 32 && bpb.bspf > MAXU16)
-	errx(1, "too many sectors/FAT for FAT12/16");
-    x1 = bpb.res + rds;
-    x = bpb.bspf ? bpb.bspf : 1;
-    if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec)
-	errx(1, "meta data exceeds file system size");
-    x1 += x * bpb.nft;
-    x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /
-	(bpb.spc * bpb.bps * NPB + fat / BPN * bpb.nft);
-    x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN),
-		 bpb.bps * NPB);
-    if (!bpb.bspf) {
-	bpb.bspf = x2;
-	x1 += (bpb.bspf - 1) * bpb.nft;
-    }
-    cls = (bpb.bsec - x1) / bpb.spc;
-    x = (u_int64_t)bpb.bspf * bpb.bps * NPB / (fat / BPN) - RESFTE;
-    if (cls > x)
-	cls = x;
-    if (bpb.bspf < x2)
-	warnx("warning: sectors/FAT limits file system to %u clusters",
-	      cls);
-    if (cls < mincls(fat))
-	errx(1, "%u clusters too few clusters for FAT%u, need %u", cls, fat,
-	    mincls(fat));
-    if (cls > maxcls(fat)) {
-	cls = maxcls(fat);
-	bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;
-	warnx("warning: FAT type limits file system to %u sectors",
-	      bpb.bsec);
-    }
-    printf("%s: %u sector%s in %u FAT%u cluster%s "
-	   "(%u bytes/cluster)\n", fname, cls * bpb.spc,
-	   cls * bpb.spc == 1 ? "" : "s", cls, fat,
-	   cls == 1 ? "" : "s", bpb.bps * bpb.spc);
-    if (!bpb.mid)
-	bpb.mid = !bpb.hid ? 0xf0 : 0xf8;
-    if (fat == 32)
-	bpb.rdcl = RESFTE;
-    if (bpb.hid + bpb.bsec <= MAXU16) {
-	bpb.sec = bpb.bsec;
-	bpb.bsec = 0;
-    }
-    if (fat != 32) {
-	bpb.spf = bpb.bspf;
-	bpb.bspf = 0;
-    }
-    print_bpb(&bpb);
-    if (!opt_N) {
-	gettimeofday(&tv, NULL);
-	now = tv.tv_sec;
-	tm = localtime(&now);
-	if (!(img = malloc(bpb.bps)))
-	    err(1, NULL);
-	dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
-	for (lsn = 0; lsn < dir + (fat == 32 ? bpb.spc : rds); lsn++) {
-	    x = lsn;
-	    if (opt_B &&
-		fat == 32 && bpb.bkbs != MAXU16 &&
-		bss <= bpb.bkbs && x >= bpb.bkbs) {
-		x -= bpb.bkbs;
-		if (!x && lseek(fd1, 0, SEEK_SET))
-		    err(1, "%s", bname);
-	    }
-	    if (opt_B && x < bss) {
-		if ((n = read(fd1, img, bpb.bps)) == -1)
-		    err(1, "%s", bname);
-		if (n != bpb.bps)
-		    errx(1, "%s: can't read sector %u", bname, x);
-	    } else
-		memset(img, 0, bpb.bps);
-	    if (!lsn ||
-	      (fat == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {
-		x1 = sizeof(struct bs);
-		bsbpb = (struct bsbpb *)(img + x1);
-		mk2(bsbpb->bps, bpb.bps);
-		mk1(bsbpb->spc, bpb.spc);
-		mk2(bsbpb->res, bpb.res);
-		mk1(bsbpb->nft, bpb.nft);
-		mk2(bsbpb->rde, bpb.rde);
-		mk2(bsbpb->sec, bpb.sec);
-		mk1(bsbpb->mid, bpb.mid);
-		mk2(bsbpb->spf, bpb.spf);
-		mk2(bsbpb->spt, bpb.spt);
-		mk2(bsbpb->hds, bpb.hds);
-		mk4(bsbpb->hid, bpb.hid);
-		mk4(bsbpb->bsec, bpb.bsec);
-		x1 += sizeof(struct bsbpb);
-		if (fat == 32) {
-		    bsxbpb = (struct bsxbpb *)(img + x1);
-		    mk4(bsxbpb->bspf, bpb.bspf);
-		    mk2(bsxbpb->xflg, 0);
-		    mk2(bsxbpb->vers, 0);
-		    mk4(bsxbpb->rdcl, bpb.rdcl);
-		    mk2(bsxbpb->infs, bpb.infs);
-		    mk2(bsxbpb->bkbs, bpb.bkbs);
-		    x1 += sizeof(struct bsxbpb);
-		}
-		bsx = (struct bsx *)(img + x1);
-		mk1(bsx->sig, 0x29);
-		if (Iflag)
-		    x = opt_I;
-		else
-		    x = (((u_int)(1 + tm->tm_mon) << 8 |
-			  (u_int)tm->tm_mday) +
-			 ((u_int)tm->tm_sec << 8 |
-			  (u_int)(tv.tv_usec / 10))) << 16 |
-			((u_int)(1900 + tm->tm_year) +
-			 ((u_int)tm->tm_hour << 8 |
-			  (u_int)tm->tm_min));
-		mk4(bsx->volid, x);
-		mklabel(bsx->label, opt_L ? opt_L : "NO NAME");
-		sprintf(buf, "FAT%u", fat);
-		setstr(bsx->type, buf, sizeof(bsx->type));
-		if (!opt_B) {
-		    x1 += sizeof(struct bsx);
-		    bs = (struct bs *)img;
-		    mk1(bs->jmp[0], 0xeb);
-		    mk1(bs->jmp[1], x1 - 2);
-		    mk1(bs->jmp[2], 0x90);
-		    setstr(bs->oem, opt_O ? opt_O : "BSD  4.4",
-			   sizeof(bs->oem));
-		    memcpy(img + x1, bootcode, sizeof(bootcode));
-		    mk2(img + bpb.bps - 2, DOSMAGIC);
-		}
-	    } else if (fat == 32 && bpb.infs != MAXU16 &&
-		       (lsn == bpb.infs ||
-			(bpb.bkbs != MAXU16 &&
-			 lsn == bpb.bkbs + bpb.infs))) {
-		mk4(img, 0x41615252);
-		mk4(img + bpb.bps - 28, 0x61417272);
-		mk4(img + bpb.bps - 24, 0xffffffff);
-		mk4(img + bpb.bps - 20, bpb.rdcl);
-		mk2(img + bpb.bps - 2, DOSMAGIC);
-	    } else if (lsn >= bpb.res && lsn < dir &&
-		       !((lsn - bpb.res) %
-			 (bpb.spf ? bpb.spf : bpb.bspf))) {
-		mk1(img[0], bpb.mid);
-		for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
-		    mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
-	    } else if (lsn == dir && opt_L) {
-		de = (struct de *)img;
-		mklabel(de->namext, opt_L);
-		mk1(de->attr, 050);
-		x = (u_int)tm->tm_hour << 11 |
-		    (u_int)tm->tm_min << 5 |
-		    (u_int)tm->tm_sec >> 1;
-		mk2(de->time, x);
-		x = (u_int)(tm->tm_year - 80) << 9 |
-		    (u_int)(tm->tm_mon + 1) << 5 |
-		    (u_int)tm->tm_mday;
-		mk2(de->date, x);
-	    }
-	    if ((n = write(fd, img, bpb.bps)) == -1)
-		err(1, "%s", fname);
-	    if (n != bpb.bps)
-		errx(1, "%s: can't write sector %u", fname, lsn);
-	}
-    }
-    return 0;
-}
-
-/*
- * Exit with error if file system is mounted.
- */
-static void
-check_mounted(const char *fname, mode_t mode)
-{
-    struct statfs *mp;
-    const char *s1, *s2;
-    size_t len;
-    int n, r;
-
-    if (!(n = getmntinfo(&mp, MNT_NOWAIT)))
-	err(1, "getmntinfo");
-    len = strlen(_PATH_DEV);
-    s1 = fname;
-    if (!strncmp(s1, _PATH_DEV, len))
-	s1 += len;
-    r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
-    for (; n--; mp++) {
-	s2 = mp->f_mntfromname;
-	if (!strncmp(s2, _PATH_DEV, len))
-	    s2 += len;
-	if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
-	    !strcmp(s1, s2))
-	    errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
-    }
-}
-
-/*
- * Get a standard format.
- */
-static void
-getstdfmt(const char *fmt, struct bpb *bpb)
-{
-    u_int x, i;
-
-    x = sizeof(stdfmt) / sizeof(stdfmt[0]);
-    for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
-    if (i == x)
-	errx(1, "%s: unknown standard format", fmt);
-    *bpb = stdfmt[i].bpb;
-}
-
-/*
- * Get disk slice, partition, and geometry information.
- */
-static void
-getdiskinfo(int fd, const char *fname, const char *dtype, int oflag,
-	    struct bpb *bpb)
-{
-    struct diskslices ds;
-    const char *s1, *s2;
-    char *s;
-    int slice, part, i;
-
-    slice = part = -1;
-    s1 = fname;
-    if ((s2 = strrchr(s1, '/')))
-	s1 = s2 + 1;
-    for (s2 = s1; *s2 && !isdigit(*s2); s2++);
-    if (!*s2 || s2 == s1)
-	s2 = NULL;
-    else
-	while (isdigit(*++s2));
-    s1 = s2;
-    if (s2 && *s2 == 's') {
-	slice = strtol(s2 + 1, &s, 10);
-	if (slice < 0 || slice > MAX_SLICES - BASE_SLICE)
-	    s2 = NULL;
-	else {
-	    if (slice)
-		slice = BASE_SLICE + slice - 1;
-	    s2 = s;
-	}
-    }
-    if (s2 && *s2 >= 'a' && *s2 <= 'a' + MAXPARTITIONS - 1) {
-	if (slice == -1)
-	    slice = COMPATIBILITY_SLICE;
-	part = *s2++ - 'a';
-    }
-    if (!s2 || (*s2 && *s2 != '.'))
-	errx(1, "%s: can't figure out partition info", fname);
-    if (slice != -1 && (!oflag || (!bpb->bsec && part == -1))) {
-	if (ioctl(fd, DIOCGSLICEINFO, &ds) == -1) {
-	    warn("ioctl (GSLICEINFO)");
-	    errx(1, "%s: can't get slice info", fname);
-	}
-	if (slice >= ds.dss_nslices || !ds.dss_slices[slice].ds_size)
-	    errx(1, "%s: slice is unavailable", fname);
-	if (!oflag)
-	    bpb->hid = ds.dss_slices[slice].ds_offset;
-	if (!bpb->bsec && part == -1)
-	    bpb->bsec = ds.dss_slices[slice].ds_size;
-    }
-    if (((slice == -1 || part != -1) &&
-	 ((!oflag && part != -1) || !bpb->bsec)) ||
-	!bpb->bps || !bpb->spt || !bpb->hds
-    ) {
-	struct partinfo pinfo;
-	struct disktab *dtab = NULL;
-
-	i = ioctl(fd, DIOCGPART, &pinfo);
-	if (i == -1) {
-	    if (!dtype) {
-		warn("ioctl (GPART)");
-		errx(1, "%s: can't read disk label; "
-		     "disk type must be specified", fname);
-	    } else if (!(dtab = getdisktabbyname(dtype))) {
-		errx(1, "%s: unknown disk type", dtype);
-	    }
-	    pinfo.media_blocks = dtab->d_media_blocks;
-	    pinfo.media_blksize = dtab->d_media_blksize;
-	    pinfo.d_nheads = dtab->d_nheads;
-	    pinfo.d_secpertrack = dtab->d_secpertrack;
-	}
-	if (!oflag && part != -1)
-	    bpb->hid += pinfo.media_offset;
-	if (!bpb->bsec)
-	    bpb->bsec = pinfo.media_blocks;
-
-	if (!bpb->bps)
-	    bpb->bps = ckgeom(fname, pinfo.media_blksize, "bytes/sector");
-	if (!bpb->spt)
-	    bpb->spt = ckgeom(fname, pinfo.d_secpertrack, "sectors/track");
-	if (!bpb->hds)
-	    bpb->hds = ckgeom(fname, pinfo.d_nheads, "drive heads");
-    }
-}
-
-/*
- * Print out BPB values.
- */
-static void
-print_bpb(struct bpb *bpb)
-{
-    printf("bps=%u spc=%u res=%u nft=%u", bpb->bps, bpb->spc, bpb->res,
-	   bpb->nft);
-    if (bpb->rde)
-	printf(" rde=%u", bpb->rde);
-    if (bpb->sec)
-	printf(" sec=%u", bpb->sec);
-    printf(" mid=%#x", bpb->mid);
-    if (bpb->spf)
-	printf(" spf=%u", bpb->spf);
-    printf(" spt=%u hds=%u hid=%u", bpb->spt, bpb->hds, bpb->hid);
-    if (bpb->bsec)
-	printf(" bsec=%u", bpb->bsec);
-    if (!bpb->spf) {
-	printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);
-	printf(" infs=");
-	printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);
-	printf(" bkbs=");
-	printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);
-    }
-    printf("\n");
-}
-
-/*
- * Check a disk geometry value.
- */
-static u_int
-ckgeom(const char *fname, u_int val, const char *msg)
-{
-    if (!val)
-	errx(1, "%s: no default %s", fname, msg);
-    if (val > MAXU16)
-	errx(1, "%s: illegal %s", fname, msg);
-    return val;
+    return !!mkfs_msdos(fname, dtype, &o);
 }
 
 /*
@@ -844,43 +176,54 @@ argtou(const char *arg, u_int lo, u_int hi, const char *msg)
 }
 
 /*
- * Check a volume label.
+ * Same for off_t, with optional skmgpP suffix
  */
-static int
-oklabel(const char *src)
+static off_t
+argtooff(const char *arg, const char *msg)
 {
-    int c, i;
+    char *s;
+    off_t x;
 
-    for (i = 0; i <= 11; i++) {
-	c = (u_char)*src++;
-	if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
+    errno = 0;
+    x = strtoll(arg, &s, 0);
+    /* allow at most one extra char */
+    if (errno || x < 0 || (s[0] && s[1]) )
+	errx(1, "%s: bad %s", arg, msg);
+    if (*s) {	/* the extra char is the multiplier */
+	switch (*s) {
+	default:
+	    errx(1, "%s: bad %s", arg, msg);
+	    /* notreached */
+
+	case 's':		/* sector */
+	case 'S':
+	    x <<= 9;		/* times 512 */
 	    break;
-    }
-    return i && !c;
-}
 
-/*
- * Make a volume label.
- */
-static void
-mklabel(u_int8_t *dest, const char *src)
-{
-    int c, i;
+	case 'k':		/* kilobyte */
+	case 'K':
+	    x <<= 10;		/* times 1024 */
+	    break;
 
-    for (i = 0; i < 11; i++) {
-	c = *src ? toupper(*src++) : ' ';
-	*dest++ = !i && c == '\xe5' ? 5 : c;
-    }
-}
+	case 'm':		/* megabyte */
+	case 'M':
+	    x <<= 20;		/* times 1024*1024 */
+	    break;
 
-/*
- * Copy string, padding with spaces.
- */
-static void
-setstr(u_int8_t *dest, const char *src, size_t len)
-{
-    while (len--)
-	*dest++ = *src ? *src++ : ' ';
+	case 'g':		/* gigabyte */
+	case 'G':
+	    x <<= 30;		/* times 1024*1024*1024 */
+	    break;
+
+	case 'p':		/* partition start */
+	case 'P':
+	case 'l':		/* partition length */
+	case 'L':
+	    errx(1, "%s: not supported yet %s", arg, msg);
+	    /* notreached */
+	}
+    }
+    return x;
 }
 
 /*
@@ -890,29 +233,17 @@ static void
 usage(void)
 {
     fprintf(stderr,
-	    "usage: newfs_msdos [ -options ] special [disktype]\n");
+	    "usage: %s [ -options ] special [disktype]\n", getprogname());
     fprintf(stderr, "where the options are:\n");
-    fprintf(stderr, "\t-N don't create file system: "
-	    "just print out parameters\n");
-    fprintf(stderr, "\t-B get bootstrap from file\n");
-    fprintf(stderr, "\t-F FAT type (12, 16, or 32)\n");
-    fprintf(stderr, "\t-I volume ID\n");
-    fprintf(stderr, "\t-L volume label\n");
-    fprintf(stderr, "\t-O OEM string\n");
-    fprintf(stderr, "\t-S bytes/sector\n");
-    fprintf(stderr, "\t-a sectors/FAT\n");
-    fprintf(stderr, "\t-b block size\n");
-    fprintf(stderr, "\t-c sectors/cluster\n");
-    fprintf(stderr, "\t-e root directory entries\n");
-    fprintf(stderr, "\t-f standard format\n");
-    fprintf(stderr, "\t-h drive heads\n");
-    fprintf(stderr, "\t-i file system info sector\n");
-    fprintf(stderr, "\t-k backup boot sector\n");
-    fprintf(stderr, "\t-m media descriptor\n");
-    fprintf(stderr, "\t-n number of FATs\n");
-    fprintf(stderr, "\t-o hidden sectors\n");
-    fprintf(stderr, "\t-r reserved sectors\n");
-    fprintf(stderr, "\t-s file system size (sectors)\n");
-    fprintf(stderr, "\t-u sectors/track\n");
+static struct {
+    char o;
+    const char *h;
+} opts[] = {
+#define AOPT(_opt, _type, _name, _min, _desc) { _opt, _desc },
+ALLOPTS
+#undef AOPT
+    };
+    for (size_t i = 0; i < nitems(opts); i++)
+	fprintf(stderr, "\t-%c %s\n", opts[i].o, opts[i].h);
     exit(1);
 }
-- 
2.10.1

