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