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