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