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