newfs_msdos.c revision 229550
1241675Suqs/* 2241675Suqs * Copyright (c) 1998 Robert Nordier 3241675Suqs * All rights reserved. 4241675Suqs * 5241675Suqs * Redistribution and use in source and binary forms, with or without 6241675Suqs * modification, are permitted provided that the following conditions 7241675Suqs * are met: 8241675Suqs * 1. Redistributions of source code must retain the above copyright 9241675Suqs * notice, this list of conditions and the following disclaimer. 10241675Suqs * 2. Redistributions in binary form must reproduce the above copyright 11241675Suqs * notice, this list of conditions and the following disclaimer in 12241675Suqs * the documentation and/or other materials provided with the 13241675Suqs * distribution. 14241675Suqs * 15241675Suqs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS 16241675Suqs * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17241675Suqs * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18241675Suqs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY 19241675Suqs * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20241675Suqs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 21241675Suqs * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22241675Suqs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 23241675Suqs * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24241675Suqs * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 25241675Suqs * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26241675Suqs */ 27241675Suqs 28241675Suqs#ifndef lint 29241675Suqsstatic const char rcsid[] = 30241675Suqs "$FreeBSD: stable/9/sbin/newfs_msdos/newfs_msdos.c 229550 2012-01-05 01:40:42Z pfg $"; 31241675Suqs#endif /* not lint */ 32241675Suqs 33241675Suqs#include <sys/param.h> 34241675Suqs#include <sys/fdcio.h> 35241675Suqs#include <sys/disk.h> 36241675Suqs#include <sys/disklabel.h> 37241675Suqs#include <sys/mount.h> 38241675Suqs#include <sys/stat.h> 39241675Suqs#include <sys/time.h> 40241675Suqs 41241675Suqs#include <ctype.h> 42241675Suqs#include <err.h> 43241675Suqs#include <errno.h> 44241675Suqs#include <fcntl.h> 45241675Suqs#include <inttypes.h> 46241675Suqs#include <paths.h> 47241675Suqs#include <signal.h> 48241675Suqs#include <stdio.h> 49241675Suqs#include <stdlib.h> 50241675Suqs#include <string.h> 51241675Suqs#include <time.h> 52241675Suqs#include <unistd.h> 53241675Suqs 54241675Suqs#define MAXU16 0xffff /* maximum unsigned 16-bit quantity */ 55241675Suqs#define BPN 4 /* bits per nibble */ 56241675Suqs#define NPB 2 /* nibbles per byte */ 57241675Suqs 58241675Suqs#define DOSMAGIC 0xaa55 /* DOS magic number */ 59241675Suqs#define MINBPS 512 /* minimum bytes per sector */ 60241675Suqs#define MAXSPC 128 /* maximum sectors per cluster */ 61241675Suqs#define MAXNFT 16 /* maximum number of FATs */ 62241675Suqs#define DEFBLK 4096 /* default block size */ 63241675Suqs#define DEFBLK16 2048 /* default block size FAT16 */ 64241675Suqs#define DEFRDE 512 /* default root directory entries */ 65241675Suqs#define RESFTE 2 /* reserved FAT entries */ 66241675Suqs#define MINCLS12 1U /* minimum FAT12 clusters */ 67241675Suqs#define MINCLS16 0x1000U /* minimum FAT16 clusters */ 68241675Suqs#define MINCLS32 2U /* minimum FAT32 clusters */ 69241675Suqs#define MAXCLS12 0xfedU /* maximum FAT12 clusters */ 70241675Suqs#define MAXCLS16 0xfff5U /* maximum FAT16 clusters */ 71241675Suqs#define MAXCLS32 0xffffff5U /* maximum FAT32 clusters */ 72241675Suqs 73241675Suqs#define mincls(fat) ((fat) == 12 ? MINCLS12 : \ 74241675Suqs (fat) == 16 ? MINCLS16 : \ 75241675Suqs MINCLS32) 76241675Suqs 77241675Suqs#define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \ 78241675Suqs (fat) == 16 ? MAXCLS16 : \ 79241675Suqs MAXCLS32) 80241675Suqs 81241675Suqs#define mk1(p, x) \ 82241675Suqs (p) = (u_int8_t)(x) 83241675Suqs 84241675Suqs#define mk2(p, x) \ 85241675Suqs (p)[0] = (u_int8_t)(x), \ 86241675Suqs (p)[1] = (u_int8_t)((x) >> 010) 87241675Suqs 88241675Suqs#define mk4(p, x) \ 89241675Suqs (p)[0] = (u_int8_t)(x), \ 90241675Suqs (p)[1] = (u_int8_t)((x) >> 010), \ 91241675Suqs (p)[2] = (u_int8_t)((x) >> 020), \ 92241675Suqs (p)[3] = (u_int8_t)((x) >> 030) 93241675Suqs 94241675Suqs#define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg) 95241675Suqs#define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg) 96241675Suqs#define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg) 97241675Suqs#define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg) 98241675Suqs 99241675Suqsstruct bs { 100241675Suqs u_int8_t bsJump[3]; /* bootstrap entry point */ 101241675Suqs u_int8_t bsOemName[8]; /* OEM name and version */ 102241675Suqs}; 103241675Suqs 104241675Suqsstruct bsbpb { 105241675Suqs u_int8_t bpbBytesPerSec[2]; /* bytes per sector */ 106241675Suqs u_int8_t bpbSecPerClust; /* sectors per cluster */ 107241675Suqs u_int8_t bpbResSectors[2]; /* reserved sectors */ 108241675Suqs u_int8_t bpbFATs; /* number of FATs */ 109241675Suqs u_int8_t bpbRootDirEnts[2]; /* root directory entries */ 110241675Suqs u_int8_t bpbSectors[2]; /* total sectors */ 111241675Suqs u_int8_t bpbMedia; /* media descriptor */ 112241675Suqs u_int8_t bpbFATsecs[2]; /* sectors per FAT */ 113241675Suqs u_int8_t bpbSecPerTrack[2]; /* sectors per track */ 114241675Suqs u_int8_t bpbHeads[2]; /* drive heads */ 115241675Suqs u_int8_t bpbHiddenSecs[4]; /* hidden sectors */ 116241675Suqs u_int8_t bpbHugeSectors[4]; /* big total sectors */ 117241675Suqs}; 118241675Suqs 119241675Suqsstruct bsxbpb { 120241675Suqs u_int8_t bpbBigFATsecs[4]; /* big sectors per FAT */ 121241675Suqs u_int8_t bpbExtFlags[2]; /* FAT control flags */ 122241675Suqs u_int8_t bpbFSVers[2]; /* file system version */ 123241675Suqs u_int8_t bpbRootClust[4]; /* root directory start cluster */ 124241675Suqs u_int8_t bpbFSInfo[2]; /* file system info sector */ 125241675Suqs u_int8_t bpbBackup[2]; /* backup boot sector */ 126241675Suqs u_int8_t bpbReserved[12]; /* reserved */ 127241675Suqs}; 128241675Suqs 129241675Suqsstruct bsx { 130241675Suqs u_int8_t exDriveNumber; /* drive number */ 131241675Suqs u_int8_t exReserved1; /* reserved */ 132241675Suqs u_int8_t exBootSignature; /* extended boot signature */ 133241675Suqs u_int8_t exVolumeID[4]; /* volume ID number */ 134241675Suqs u_int8_t exVolumeLabel[11]; /* volume label */ 135241675Suqs u_int8_t exFileSysType[8]; /* file system type */ 136241675Suqs}; 137241675Suqs 138241675Suqsstruct de { 139241675Suqs u_int8_t deName[11]; /* name and extension */ 140241675Suqs u_int8_t deAttributes; /* attributes */ 141241675Suqs u_int8_t rsvd[10]; /* reserved */ 142241675Suqs u_int8_t deMTime[2]; /* creation time */ 143241675Suqs u_int8_t deMDate[2]; /* creation date */ 144241675Suqs u_int8_t deStartCluster[2]; /* starting cluster */ 145241675Suqs u_int8_t deFileSize[4]; /* size */ 146241675Suqs}; 147241675Suqs 148241675Suqsstruct bpb { 149241675Suqs u_int bpbBytesPerSec; /* bytes per sector */ 150241675Suqs u_int bpbSecPerClust; /* sectors per cluster */ 151241675Suqs u_int bpbResSectors; /* reserved sectors */ 152241675Suqs u_int bpbFATs; /* number of FATs */ 153241675Suqs u_int bpbRootDirEnts; /* root directory entries */ 154241675Suqs u_int bpbSectors; /* total sectors */ 155241675Suqs u_int bpbMedia; /* media descriptor */ 156241675Suqs u_int bpbFATsecs; /* sectors per FAT */ 157241675Suqs u_int bpbSecPerTrack; /* sectors per track */ 158241675Suqs u_int bpbHeads; /* drive heads */ 159241675Suqs u_int bpbHiddenSecs; /* hidden sectors */ 160241675Suqs u_int bpbHugeSectors; /* big total sectors */ 161241675Suqs u_int bpbBigFATsecs; /* big sectors per FAT */ 162241675Suqs u_int bpbRootClust; /* root directory start cluster */ 163241675Suqs u_int bpbFSInfo; /* file system info sector */ 164241675Suqs u_int bpbBackup; /* backup boot sector */ 165241675Suqs}; 166241675Suqs 167241675Suqs#define BPBGAP 0, 0, 0, 0, 0, 0 168241675Suqs 169241675Suqsstatic struct { 170241675Suqs const char *name; 171241675Suqs struct bpb bpb; 172241675Suqs} const stdfmt[] = { 173241675Suqs {"160", {512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1, BPBGAP}}, 174241675Suqs {"180", {512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1, BPBGAP}}, 175241675Suqs {"320", {512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2, BPBGAP}}, 176241675Suqs {"360", {512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2, BPBGAP}}, 177241675Suqs {"640", {512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2, BPBGAP}}, 178241675Suqs {"720", {512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2, BPBGAP}}, 179241675Suqs {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2, BPBGAP}}, 180241675Suqs {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2, BPBGAP}}, 181241675Suqs {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2, BPBGAP}}, 182241675Suqs {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2, BPBGAP}} 183241675Suqs}; 184241675Suqs 185241675Suqsstatic const u_int8_t bootcode[] = { 186241675Suqs 0xfa, /* cli */ 187241675Suqs 0x31, 0xc0, /* xor ax,ax */ 188241675Suqs 0x8e, 0xd0, /* mov ss,ax */ 189241675Suqs 0xbc, 0x00, 0x7c, /* mov sp,7c00h */ 190241675Suqs 0xfb, /* sti */ 191241675Suqs 0x8e, 0xd8, /* mov ds,ax */ 192241675Suqs 0xe8, 0x00, 0x00, /* call $ + 3 */ 193241675Suqs 0x5e, /* pop si */ 194241675Suqs 0x83, 0xc6, 0x19, /* add si,+19h */ 195241675Suqs 0xbb, 0x07, 0x00, /* mov bx,0007h */ 196241675Suqs 0xfc, /* cld */ 197241675Suqs 0xac, /* lodsb */ 198241675Suqs 0x84, 0xc0, /* test al,al */ 199241675Suqs 0x74, 0x06, /* jz $ + 8 */ 200241675Suqs 0xb4, 0x0e, /* mov ah,0eh */ 201241675Suqs 0xcd, 0x10, /* int 10h */ 202241675Suqs 0xeb, 0xf5, /* jmp $ - 9 */ 203241675Suqs 0x30, 0xe4, /* xor ah,ah */ 204241675Suqs 0xcd, 0x16, /* int 16h */ 205241675Suqs 0xcd, 0x19, /* int 19h */ 206241675Suqs 0x0d, 0x0a, 207241675Suqs 'N', 'o', 'n', '-', 's', 'y', 's', 't', 208241675Suqs 'e', 'm', ' ', 'd', 'i', 's', 'k', 209241675Suqs 0x0d, 0x0a, 210241675Suqs 'P', 'r', 'e', 's', 's', ' ', 'a', 'n', 211241675Suqs 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o', 212241675Suqs ' ', 'r', 'e', 'b', 'o', 'o', 't', 213241675Suqs 0x0d, 0x0a, 214241675Suqs 0 215241675Suqs}; 216241675Suqs 217241675Suqsstatic volatile sig_atomic_t got_siginfo; 218241675Suqsstatic void infohandler(int); 219241675Suqs 220241675Suqsstatic void check_mounted(const char *, mode_t); 221241675Suqsstatic void getstdfmt(const char *, struct bpb *); 222241675Suqsstatic void getdiskinfo(int, const char *, const char *, int, 223241675Suqs struct bpb *); 224241675Suqsstatic void print_bpb(struct bpb *); 225241675Suqsstatic u_int ckgeom(const char *, u_int, const char *); 226241675Suqsstatic u_int argtou(const char *, u_int, u_int, const char *); 227241675Suqsstatic off_t argtooff(const char *, const char *); 228241675Suqsstatic int oklabel(const char *); 229241675Suqsstatic void mklabel(u_int8_t *, const char *); 230241675Suqsstatic void setstr(u_int8_t *, const char *, size_t); 231241675Suqsstatic void usage(void); 232241675Suqs 233241675Suqs/* 234241675Suqs * Construct a FAT12, FAT16, or FAT32 file system. 235241675Suqs */ 236241675Suqsint 237241675Suqsmain(int argc, char *argv[]) 238241675Suqs{ 239241675Suqs static const char opts[] = "@:NB:C:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:"; 240241675Suqs const char *opt_B = NULL, *opt_L = NULL, *opt_O = NULL, *opt_f = NULL; 241241675Suqs u_int opt_F = 0, opt_I = 0, opt_S = 0, opt_a = 0, opt_b = 0, opt_c = 0; 242241675Suqs u_int opt_e = 0, opt_h = 0, opt_i = 0, opt_k = 0, opt_m = 0, opt_n = 0; 243241675Suqs u_int opt_o = 0, opt_r = 0, opt_s = 0, opt_u = 0; 244241675Suqs int opt_N = 0; 245241675Suqs int Iflag = 0, mflag = 0, oflag = 0; 246241675Suqs char buf[MAXPATHLEN]; 247241675Suqs struct sigaction si_sa; 248241675Suqs struct stat sb; 249241675Suqs struct timeval tv; 250241675Suqs struct bpb bpb; 251241675Suqs struct tm *tm; 252241675Suqs struct bs *bs; 253241675Suqs struct bsbpb *bsbpb; 254241675Suqs struct bsxbpb *bsxbpb; 255241675Suqs struct bsx *bsx; 256241675Suqs struct de *de; 257241675Suqs u_int8_t *img; 258241675Suqs const char *fname, *dtype, *bname; 259241675Suqs ssize_t n; 260241675Suqs time_t now; 261241675Suqs u_int fat, bss, rds, cls, dir, lsn, x, x1, x2; 262241675Suqs int ch, fd, fd1; 263241675Suqs off_t opt_create = 0, opt_ofs = 0; 264241675Suqs 265241675Suqs while ((ch = getopt(argc, argv, opts)) != -1) 266241675Suqs switch (ch) { 267241675Suqs case '@': 268241675Suqs opt_ofs = argtooff(optarg, "offset"); 269241675Suqs break; 270241675Suqs case 'N': 271241675Suqs opt_N = 1; 272241675Suqs break; 273241675Suqs case 'B': 274241675Suqs opt_B = optarg; 275241675Suqs break; 276241675Suqs case 'C': 277241675Suqs opt_create = argtooff(optarg, "create size"); 278241675Suqs break; 279241675Suqs case 'F': 280241675Suqs if (strcmp(optarg, "12") && 281241675Suqs strcmp(optarg, "16") && 282241675Suqs strcmp(optarg, "32")) 283241675Suqs errx(1, "%s: bad FAT type", optarg); 284241675Suqs opt_F = atoi(optarg); 285241675Suqs break; 286241675Suqs case 'I': 287241675Suqs opt_I = argto4(optarg, 0, "volume ID"); 288241675Suqs Iflag = 1; 289241675Suqs break; 290241675Suqs case 'L': 291241675Suqs if (!oklabel(optarg)) 292241675Suqs errx(1, "%s: bad volume label", optarg); 293241675Suqs opt_L = optarg; 294241675Suqs break; 295241675Suqs case 'O': 296241675Suqs if (strlen(optarg) > 8) 297241675Suqs errx(1, "%s: bad OEM string", optarg); 298241675Suqs opt_O = optarg; 299241675Suqs break; 300241675Suqs case 'S': 301241675Suqs opt_S = argto2(optarg, 1, "bytes/sector"); 302241675Suqs break; 303241675Suqs case 'a': 304241675Suqs opt_a = argto4(optarg, 1, "sectors/FAT"); 305241675Suqs break; 306241675Suqs case 'b': 307241675Suqs opt_b = argtox(optarg, 1, "block size"); 308241675Suqs opt_c = 0; 309241675Suqs break; 310241675Suqs case 'c': 311241675Suqs opt_c = argto1(optarg, 1, "sectors/cluster"); 312241675Suqs opt_b = 0; 313241675Suqs break; 314241675Suqs case 'e': 315241675Suqs opt_e = argto2(optarg, 1, "directory entries"); 316241675Suqs break; 317241675Suqs case 'f': 318241675Suqs opt_f = optarg; 319241675Suqs break; 320241675Suqs case 'h': 321241675Suqs opt_h = argto2(optarg, 1, "drive heads"); 322241675Suqs break; 323241675Suqs case 'i': 324241675Suqs opt_i = argto2(optarg, 1, "info sector"); 325241675Suqs break; 326241675Suqs case 'k': 327241675Suqs opt_k = argto2(optarg, 1, "backup sector"); 328241675Suqs break; 329241675Suqs case 'm': 330241675Suqs opt_m = argto1(optarg, 0, "media descriptor"); 331241675Suqs mflag = 1; 332241675Suqs break; 333241675Suqs case 'n': 334241675Suqs opt_n = argto1(optarg, 1, "number of FATs"); 335241675Suqs break; 336241675Suqs case 'o': 337241675Suqs opt_o = argto4(optarg, 0, "hidden sectors"); 338241675Suqs oflag = 1; 339241675Suqs break; 340241675Suqs case 'r': 341241675Suqs opt_r = argto2(optarg, 1, "reserved sectors"); 342241675Suqs break; 343241675Suqs case 's': 344241675Suqs opt_s = argto4(optarg, 1, "file system size"); 345241675Suqs break; 346241675Suqs case 'u': 347241675Suqs opt_u = argto2(optarg, 1, "sectors/track"); 348241675Suqs break; 349241675Suqs default: 350241675Suqs usage(); 351241675Suqs } 352241675Suqs argc -= optind; 353241675Suqs argv += optind; 354241675Suqs if (argc < 1 || argc > 2) 355241675Suqs usage(); 356241675Suqs fname = *argv++; 357241675Suqs if (!opt_create && !strchr(fname, '/')) { 358241675Suqs snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname); 359241675Suqs if (!(fname = strdup(buf))) 360241675Suqs err(1, NULL); 361241675Suqs } 362241675Suqs dtype = *argv; 363241675Suqs if (opt_create) { 364241675Suqs if (opt_N) 365241675Suqs errx(1, "create (-C) is incompatible with -N"); 366241675Suqs fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644); 367241675Suqs if (fd == -1) 368241675Suqs errx(1, "failed to create %s", fname); 369241675Suqs if (ftruncate(fd, opt_create)) 370241675Suqs errx(1, "failed to initialize %jd bytes", (intmax_t)opt_create); 371241675Suqs } else if ((fd = open(fname, opt_N ? O_RDONLY : O_RDWR)) == -1) 372241675Suqs err(1, "%s", fname); 373241675Suqs if (fstat(fd, &sb)) 374241675Suqs err(1, "%s", fname); 375241675Suqs if (opt_create) { 376241675Suqs if (!S_ISREG(sb.st_mode)) 377241675Suqs warnx("warning, %s is not a regular file", fname); 378241675Suqs } else { 379241675Suqs if (!S_ISCHR(sb.st_mode)) 380241675Suqs warnx("warning, %s is not a character device", fname); 381241675Suqs } 382241675Suqs if (!opt_N) 383241675Suqs check_mounted(fname, sb.st_mode); 384241675Suqs if (opt_ofs && opt_ofs != lseek(fd, opt_ofs, SEEK_SET)) 385241675Suqs errx(1, "cannot seek to %jd", (intmax_t)opt_ofs); 386241675Suqs memset(&bpb, 0, sizeof(bpb)); 387241675Suqs if (opt_f) { 388241675Suqs getstdfmt(opt_f, &bpb); 389241675Suqs bpb.bpbHugeSectors = bpb.bpbSectors; 390241675Suqs bpb.bpbSectors = 0; 391241675Suqs bpb.bpbBigFATsecs = bpb.bpbFATsecs; 392241675Suqs bpb.bpbFATsecs = 0; 393241675Suqs } 394241675Suqs if (opt_h) 395241675Suqs bpb.bpbHeads = opt_h; 396241675Suqs if (opt_u) 397241675Suqs bpb.bpbSecPerTrack = opt_u; 398241675Suqs if (opt_S) 399241675Suqs bpb.bpbBytesPerSec = opt_S; 400241675Suqs if (opt_s) 401241675Suqs bpb.bpbHugeSectors = opt_s; 402241675Suqs if (oflag) 403241675Suqs bpb.bpbHiddenSecs = opt_o; 404241675Suqs if (!(opt_f || (opt_h && opt_u && opt_S && opt_s && oflag))) { 405241675Suqs off_t delta; 406241675Suqs getdiskinfo(fd, fname, dtype, oflag, &bpb); 407241675Suqs bpb.bpbHugeSectors -= (opt_ofs / bpb.bpbBytesPerSec); 408241675Suqs delta = bpb.bpbHugeSectors % bpb.bpbSecPerTrack; 409241675Suqs if (delta != 0) { 410241675Suqs warnx("trim %d sectors to adjust to a multiple of %d", 411241675Suqs (int)delta, bpb.bpbSecPerTrack); 412241675Suqs bpb.bpbHugeSectors -= delta; 413241675Suqs } 414241675Suqs if (bpb.bpbSecPerClust == 0) { /* set defaults */ 415241675Suqs if (bpb.bpbHugeSectors <= 6000) /* about 3MB -> 512 bytes */ 416241675Suqs bpb.bpbSecPerClust = 1; 417241675Suqs else if (bpb.bpbHugeSectors <= (1<<17)) /* 64M -> 4k */ 418241675Suqs bpb.bpbSecPerClust = 8; 419241675Suqs else if (bpb.bpbHugeSectors <= (1<<19)) /* 256M -> 8k */ 420241675Suqs bpb.bpbSecPerClust = 16; 421241675Suqs else if (bpb.bpbHugeSectors <= (1<<21)) /* 1G -> 16k */ 422241675Suqs bpb.bpbSecPerClust = 32; 423241675Suqs else 424241675Suqs bpb.bpbSecPerClust = 64; /* otherwise 32k */ 425241675Suqs } 426241675Suqs } 427241675Suqs if (!powerof2(bpb.bpbBytesPerSec)) 428241675Suqs errx(1, "bytes/sector (%u) is not a power of 2", bpb.bpbBytesPerSec); 429241675Suqs if (bpb.bpbBytesPerSec < MINBPS) 430241675Suqs errx(1, "bytes/sector (%u) is too small; minimum is %u", 431241675Suqs bpb.bpbBytesPerSec, MINBPS); 432241675Suqs if (!(fat = opt_F)) { 433241675Suqs if (opt_f) 434241675Suqs fat = 12; 435241675Suqs else if (!opt_e && (opt_i || opt_k)) 436241675Suqs fat = 32; 437241675Suqs } 438241675Suqs if ((fat == 32 && opt_e) || (fat != 32 && (opt_i || opt_k))) 439241675Suqs errx(1, "-%c is not a legal FAT%s option", 440241675Suqs fat == 32 ? 'e' : opt_i ? 'i' : 'k', 441241675Suqs fat == 32 ? "32" : "12/16"); 442241675Suqs if (opt_f && fat == 32) 443241675Suqs bpb.bpbRootDirEnts = 0; 444241675Suqs if (opt_b) { 445241675Suqs if (!powerof2(opt_b)) 446241675Suqs errx(1, "block size (%u) is not a power of 2", opt_b); 447241675Suqs if (opt_b < bpb.bpbBytesPerSec) 448241675Suqs errx(1, "block size (%u) is too small; minimum is %u", 449241675Suqs opt_b, bpb.bpbBytesPerSec); 450241675Suqs if (opt_b > bpb.bpbBytesPerSec * MAXSPC) 451241675Suqs errx(1, "block size (%u) is too large; maximum is %u", 452241675Suqs opt_b, bpb.bpbBytesPerSec * MAXSPC); 453241675Suqs bpb.bpbSecPerClust = opt_b / bpb.bpbBytesPerSec; 454241675Suqs } 455241675Suqs if (opt_c) { 456241675Suqs if (!powerof2(opt_c)) 457241675Suqs errx(1, "sectors/cluster (%u) is not a power of 2", opt_c); 458241675Suqs bpb.bpbSecPerClust = opt_c; 459241675Suqs } 460241675Suqs if (opt_r) 461241675Suqs bpb.bpbResSectors = opt_r; 462241675Suqs if (opt_n) { 463241675Suqs if (opt_n > MAXNFT) 464241675Suqs errx(1, "number of FATs (%u) is too large; maximum is %u", 465241675Suqs opt_n, MAXNFT); 466241675Suqs bpb.bpbFATs = opt_n; 467241675Suqs } 468241675Suqs if (opt_e) 469241675Suqs bpb.bpbRootDirEnts = opt_e; 470241675Suqs if (mflag) { 471241675Suqs if (opt_m < 0xf0) 472241675Suqs errx(1, "illegal media descriptor (%#x)", opt_m); 473241675Suqs bpb.bpbMedia = opt_m; 474241675Suqs } 475241675Suqs if (opt_a) 476241675Suqs bpb.bpbBigFATsecs = opt_a; 477241675Suqs if (opt_i) 478241675Suqs bpb.bpbFSInfo = opt_i; 479241675Suqs if (opt_k) 480241675Suqs bpb.bpbBackup = opt_k; 481241675Suqs bss = 1; 482241675Suqs bname = NULL; 483241675Suqs fd1 = -1; 484241675Suqs if (opt_B) { 485241675Suqs bname = opt_B; 486241675Suqs if (!strchr(bname, '/')) { 487241675Suqs snprintf(buf, sizeof(buf), "/boot/%s", bname); 488241675Suqs if (!(bname = strdup(buf))) 489241675Suqs err(1, NULL); 490241675Suqs } 491241675Suqs if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb)) 492241675Suqs err(1, "%s", bname); 493241675Suqs if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bpbBytesPerSec || 494241675Suqs sb.st_size < bpb.bpbBytesPerSec || 495241675Suqs sb.st_size > bpb.bpbBytesPerSec * MAXU16) 496241675Suqs errx(1, "%s: inappropriate file type or format", bname); 497241675Suqs bss = sb.st_size / bpb.bpbBytesPerSec; 498241675Suqs } 499241675Suqs if (!bpb.bpbFATs) 500241675Suqs bpb.bpbFATs = 2; 501241675Suqs if (!fat) { 502241675Suqs if (bpb.bpbHugeSectors < (bpb.bpbResSectors ? bpb.bpbResSectors : bss) + 503241675Suqs howmany((RESFTE + (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1)) * 504241675Suqs (bpb.bpbSecPerClust ? 16 : 12) / BPN, 505241675Suqs bpb.bpbBytesPerSec * NPB) * 506241675Suqs bpb.bpbFATs + 507241675Suqs howmany(bpb.bpbRootDirEnts ? bpb.bpbRootDirEnts : DEFRDE, 508241675Suqs bpb.bpbBytesPerSec / sizeof(struct de)) + 509241675Suqs (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1) * 510241675Suqs (bpb.bpbSecPerClust ? bpb.bpbSecPerClust : 511241675Suqs howmany(DEFBLK, bpb.bpbBytesPerSec))) 512241675Suqs fat = 12; 513241675Suqs else if (bpb.bpbRootDirEnts || bpb.bpbHugeSectors < 514241675Suqs (bpb.bpbResSectors ? bpb.bpbResSectors : bss) + 515241675Suqs howmany((RESFTE + MAXCLS16) * 2, bpb.bpbBytesPerSec) * 516241675Suqs bpb.bpbFATs + 517241675Suqs howmany(DEFRDE, bpb.bpbBytesPerSec / sizeof(struct de)) + 518241675Suqs (MAXCLS16 + 1) * 519241675Suqs (bpb.bpbSecPerClust ? bpb.bpbSecPerClust : 520241675Suqs howmany(8192, bpb.bpbBytesPerSec))) 521241675Suqs fat = 16; 522241675Suqs else 523241675Suqs fat = 32; 524241675Suqs } 525241675Suqs x = bss; 526241675Suqs if (fat == 32) { 527241675Suqs if (!bpb.bpbFSInfo) { 528241675Suqs if (x == MAXU16 || x == bpb.bpbBackup) 529241675Suqs errx(1, "no room for info sector"); 530241675Suqs bpb.bpbFSInfo = x; 531241675Suqs } 532241675Suqs if (bpb.bpbFSInfo != MAXU16 && x <= bpb.bpbFSInfo) 533241675Suqs x = bpb.bpbFSInfo + 1; 534241675Suqs if (!bpb.bpbBackup) { 535241675Suqs if (x == MAXU16) 536241675Suqs errx(1, "no room for backup sector"); 537241675Suqs bpb.bpbBackup = x; 538241675Suqs } else if (bpb.bpbBackup != MAXU16 && bpb.bpbBackup == bpb.bpbFSInfo) 539241675Suqs errx(1, "backup sector would overwrite info sector"); 540241675Suqs if (bpb.bpbBackup != MAXU16 && x <= bpb.bpbBackup) 541241675Suqs x = bpb.bpbBackup + 1; 542241675Suqs } 543241675Suqs if (!bpb.bpbResSectors) 544241675Suqs bpb.bpbResSectors = fat == 32 ? 545241675Suqs MAX(x, MAX(16384 / bpb.bpbBytesPerSec, 4)) : x; 546241675Suqs else if (bpb.bpbResSectors < x) 547241675Suqs errx(1, "too few reserved sectors (need %d have %d)", x, 548241675Suqs bpb.bpbResSectors); 549241675Suqs if (fat != 32 && !bpb.bpbRootDirEnts) 550241675Suqs bpb.bpbRootDirEnts = DEFRDE; 551 rds = howmany(bpb.bpbRootDirEnts, bpb.bpbBytesPerSec / sizeof(struct de)); 552 if (!bpb.bpbSecPerClust) 553 for (bpb.bpbSecPerClust = howmany(fat == 16 ? DEFBLK16 : 554 DEFBLK, bpb.bpbBytesPerSec); 555 bpb.bpbSecPerClust < MAXSPC && 556 bpb.bpbResSectors + 557 howmany((RESFTE + maxcls(fat)) * (fat / BPN), 558 bpb.bpbBytesPerSec * NPB) * 559 bpb.bpbFATs + 560 rds + 561 (u_int64_t) (maxcls(fat) + 1) * 562 bpb.bpbSecPerClust <= bpb.bpbHugeSectors; 563 bpb.bpbSecPerClust <<= 1) 564 continue; 565 if (fat != 32 && bpb.bpbBigFATsecs > MAXU16) 566 errx(1, "too many sectors/FAT for FAT12/16"); 567 x1 = bpb.bpbResSectors + rds; 568 x = bpb.bpbBigFATsecs ? bpb.bpbBigFATsecs : 1; 569 if (x1 + (u_int64_t)x * bpb.bpbFATs > bpb.bpbHugeSectors) 570 errx(1, "meta data exceeds file system size"); 571 x1 += x * bpb.bpbFATs; 572 x = (u_int64_t)(bpb.bpbHugeSectors - x1) * bpb.bpbBytesPerSec * NPB / 573 (bpb.bpbSecPerClust * bpb.bpbBytesPerSec * NPB + fat / 574 BPN * bpb.bpbFATs); 575 x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN), 576 bpb.bpbBytesPerSec * NPB); 577 if (!bpb.bpbBigFATsecs) { 578 bpb.bpbBigFATsecs = x2; 579 x1 += (bpb.bpbBigFATsecs - 1) * bpb.bpbFATs; 580 } 581 cls = (bpb.bpbHugeSectors - x1) / bpb.bpbSecPerClust; 582 x = (u_int64_t)bpb.bpbBigFATsecs * bpb.bpbBytesPerSec * NPB / (fat / BPN) - 583 RESFTE; 584 if (cls > x) 585 cls = x; 586 if (bpb.bpbBigFATsecs < x2) 587 warnx("warning: sectors/FAT limits file system to %u clusters", 588 cls); 589 if (cls < mincls(fat)) 590 errx(1, "%u clusters too few clusters for FAT%u, need %u", cls, fat, 591 mincls(fat)); 592 if (cls > maxcls(fat)) { 593 cls = maxcls(fat); 594 bpb.bpbHugeSectors = x1 + (cls + 1) * bpb.bpbSecPerClust - 1; 595 warnx("warning: FAT type limits file system to %u sectors", 596 bpb.bpbHugeSectors); 597 } 598 printf("%s: %u sector%s in %u FAT%u cluster%s " 599 "(%u bytes/cluster)\n", fname, cls * bpb.bpbSecPerClust, 600 cls * bpb.bpbSecPerClust == 1 ? "" : "s", cls, fat, 601 cls == 1 ? "" : "s", bpb.bpbBytesPerSec * bpb.bpbSecPerClust); 602 if (!bpb.bpbMedia) 603 bpb.bpbMedia = !bpb.bpbHiddenSecs ? 0xf0 : 0xf8; 604 if (fat == 32) 605 bpb.bpbRootClust = RESFTE; 606 if (bpb.bpbHiddenSecs + bpb.bpbHugeSectors <= MAXU16) { 607 bpb.bpbSectors = bpb.bpbHugeSectors; 608 bpb.bpbHugeSectors = 0; 609 } 610 if (fat != 32) { 611 bpb.bpbFATsecs = bpb.bpbBigFATsecs; 612 bpb.bpbBigFATsecs = 0; 613 } 614 print_bpb(&bpb); 615 if (!opt_N) { 616 gettimeofday(&tv, NULL); 617 now = tv.tv_sec; 618 tm = localtime(&now); 619 if (!(img = malloc(bpb.bpbBytesPerSec))) 620 err(1, NULL); 621 dir = bpb.bpbResSectors + (bpb.bpbFATsecs ? bpb.bpbFATsecs : 622 bpb.bpbBigFATsecs) * bpb.bpbFATs; 623 memset(&si_sa, 0, sizeof(si_sa)); 624 si_sa.sa_handler = infohandler; 625 if (sigaction(SIGINFO, &si_sa, NULL) == -1) 626 err(1, "sigaction SIGINFO"); 627 for (lsn = 0; lsn < dir + (fat == 32 ? bpb.bpbSecPerClust : rds); lsn++) { 628 if (got_siginfo) { 629 fprintf(stderr,"%s: writing sector %u of %u (%u%%)\n", 630 fname, lsn, 631 (dir + (fat == 32 ? bpb.bpbSecPerClust: rds)), 632 (lsn * 100) / (dir + 633 (fat == 32 ? bpb.bpbSecPerClust: rds))); 634 got_siginfo = 0; 635 } 636 x = lsn; 637 if (opt_B && 638 fat == 32 && bpb.bpbBackup != MAXU16 && 639 bss <= bpb.bpbBackup && x >= bpb.bpbBackup) { 640 x -= bpb.bpbBackup; 641 if (!x && lseek(fd1, opt_ofs, SEEK_SET)) 642 err(1, "%s", bname); 643 } 644 if (opt_B && x < bss) { 645 if ((n = read(fd1, img, bpb.bpbBytesPerSec)) == -1) 646 err(1, "%s", bname); 647 if ((unsigned)n != bpb.bpbBytesPerSec) 648 errx(1, "%s: can't read sector %u", bname, x); 649 } else 650 memset(img, 0, bpb.bpbBytesPerSec); 651 if (!lsn || 652 (fat == 32 && bpb.bpbBackup != MAXU16 && 653 lsn == bpb.bpbBackup)) { 654 x1 = sizeof(struct bs); 655 bsbpb = (struct bsbpb *)(img + x1); 656 mk2(bsbpb->bpbBytesPerSec, bpb.bpbBytesPerSec); 657 mk1(bsbpb->bpbSecPerClust, bpb.bpbSecPerClust); 658 mk2(bsbpb->bpbResSectors, bpb.bpbResSectors); 659 mk1(bsbpb->bpbFATs, bpb.bpbFATs); 660 mk2(bsbpb->bpbRootDirEnts, bpb.bpbRootDirEnts); 661 mk2(bsbpb->bpbSectors, bpb.bpbSectors); 662 mk1(bsbpb->bpbMedia, bpb.bpbMedia); 663 mk2(bsbpb->bpbFATsecs, bpb.bpbFATsecs); 664 mk2(bsbpb->bpbSecPerTrack, bpb.bpbSecPerTrack); 665 mk2(bsbpb->bpbHeads, bpb.bpbHeads); 666 mk4(bsbpb->bpbHiddenSecs, bpb.bpbHiddenSecs); 667 mk4(bsbpb->bpbHugeSectors, bpb.bpbHugeSectors); 668 x1 += sizeof(struct bsbpb); 669 if (fat == 32) { 670 bsxbpb = (struct bsxbpb *)(img + x1); 671 mk4(bsxbpb->bpbBigFATsecs, bpb.bpbBigFATsecs); 672 mk2(bsxbpb->bpbExtFlags, 0); 673 mk2(bsxbpb->bpbFSVers, 0); 674 mk4(bsxbpb->bpbRootClust, bpb.bpbRootClust); 675 mk2(bsxbpb->bpbFSInfo, bpb.bpbFSInfo); 676 mk2(bsxbpb->bpbBackup, bpb.bpbBackup); 677 x1 += sizeof(struct bsxbpb); 678 } 679 bsx = (struct bsx *)(img + x1); 680 mk1(bsx->exBootSignature, 0x29); 681 if (Iflag) 682 x = opt_I; 683 else 684 x = (((u_int)(1 + tm->tm_mon) << 8 | 685 (u_int)tm->tm_mday) + 686 ((u_int)tm->tm_sec << 8 | 687 (u_int)(tv.tv_usec / 10))) << 16 | 688 ((u_int)(1900 + tm->tm_year) + 689 ((u_int)tm->tm_hour << 8 | 690 (u_int)tm->tm_min)); 691 mk4(bsx->exVolumeID, x); 692 mklabel(bsx->exVolumeLabel, opt_L ? opt_L : "NO_NAME"); 693 sprintf(buf, "FAT%u", fat); 694 setstr(bsx->exFileSysType, buf, sizeof(bsx->exFileSysType)); 695 if (!opt_B) { 696 x1 += sizeof(struct bsx); 697 bs = (struct bs *)img; 698 mk1(bs->bsJump[0], 0xeb); 699 mk1(bs->bsJump[1], x1 - 2); 700 mk1(bs->bsJump[2], 0x90); 701 setstr(bs->bsOemName, opt_O ? opt_O : "BSD4.4 ", 702 sizeof(bs->bsOemName)); 703 memcpy(img + x1, bootcode, sizeof(bootcode)); 704 mk2(img + MINBPS - 2, DOSMAGIC); 705 } 706 } else if (fat == 32 && bpb.bpbFSInfo != MAXU16 && 707 (lsn == bpb.bpbFSInfo || 708 (bpb.bpbBackup != MAXU16 && 709 lsn == bpb.bpbBackup + bpb.bpbFSInfo))) { 710 mk4(img, 0x41615252); 711 mk4(img + MINBPS - 28, 0x61417272); 712 mk4(img + MINBPS - 24, 0xffffffff); 713 mk4(img + MINBPS - 20, bpb.bpbRootClust); 714 mk2(img + MINBPS - 2, DOSMAGIC); 715 } else if (lsn >= bpb.bpbResSectors && lsn < dir && 716 !((lsn - bpb.bpbResSectors) % 717 (bpb.bpbFATsecs ? bpb.bpbFATsecs : 718 bpb.bpbBigFATsecs))) { 719 mk1(img[0], bpb.bpbMedia); 720 for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++) 721 mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff); 722 } else if (lsn == dir && opt_L) { 723 de = (struct de *)img; 724 mklabel(de->deName, opt_L); 725 mk1(de->deAttributes, 050); 726 x = (u_int)tm->tm_hour << 11 | 727 (u_int)tm->tm_min << 5 | 728 (u_int)tm->tm_sec >> 1; 729 mk2(de->deMTime, x); 730 x = (u_int)(tm->tm_year - 80) << 9 | 731 (u_int)(tm->tm_mon + 1) << 5 | 732 (u_int)tm->tm_mday; 733 mk2(de->deMDate, x); 734 } 735 if ((n = write(fd, img, bpb.bpbBytesPerSec)) == -1) 736 err(1, "%s", fname); 737 if ((unsigned)n != bpb.bpbBytesPerSec) 738 errx(1, "%s: can't write sector %u", fname, lsn); 739 } 740 } 741 return 0; 742} 743 744/* 745 * Exit with error if file system is mounted. 746 */ 747static void 748check_mounted(const char *fname, mode_t mode) 749{ 750 struct statfs *mp; 751 const char *s1, *s2; 752 size_t len; 753 int n, r; 754 755 if (!(n = getmntinfo(&mp, MNT_NOWAIT))) 756 err(1, "getmntinfo"); 757 len = strlen(_PATH_DEV); 758 s1 = fname; 759 if (!strncmp(s1, _PATH_DEV, len)) 760 s1 += len; 761 r = S_ISCHR(mode) && s1 != fname && *s1 == 'r'; 762 for (; n--; mp++) { 763 s2 = mp->f_mntfromname; 764 if (!strncmp(s2, _PATH_DEV, len)) 765 s2 += len; 766 if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) || 767 !strcmp(s1, s2)) 768 errx(1, "%s is mounted on %s", fname, mp->f_mntonname); 769 } 770} 771 772/* 773 * Get a standard format. 774 */ 775static void 776getstdfmt(const char *fmt, struct bpb *bpb) 777{ 778 u_int x, i; 779 780 x = sizeof(stdfmt) / sizeof(stdfmt[0]); 781 for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++); 782 if (i == x) 783 errx(1, "%s: unknown standard format", fmt); 784 *bpb = stdfmt[i].bpb; 785} 786 787/* 788 * Get disk slice, partition, and geometry information. 789 */ 790static void 791getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag, 792 struct bpb *bpb) 793{ 794 struct disklabel *lp, dlp; 795 struct fd_type type; 796 off_t ms, hs = 0; 797 798 lp = NULL; 799 800 /* If the user specified a disk type, try to use that */ 801 if (dtype != NULL) { 802 lp = getdiskbyname(dtype); 803 } 804 805 /* Maybe it's a floppy drive */ 806 if (lp == NULL) { 807 if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1) { 808 struct stat st; 809 810 if (fstat(fd, &st)) 811 err(1, "Cannot get disk size"); 812 /* create a fake geometry for a file image */ 813 ms = st.st_size; 814 dlp.d_secsize = 512; 815 dlp.d_nsectors = 63; 816 dlp.d_ntracks = 255; 817 dlp.d_secperunit = ms / dlp.d_secsize; 818 lp = &dlp; 819 } else if (ioctl(fd, FD_GTYPE, &type) != -1) { 820 dlp.d_secsize = 128 << type.secsize; 821 dlp.d_nsectors = type.sectrac; 822 dlp.d_ntracks = type.heads; 823 dlp.d_secperunit = ms / dlp.d_secsize; 824 lp = &dlp; 825 } 826 } 827 828 /* Maybe it's a fixed drive */ 829 if (lp == NULL) { 830 if (bpb->bpbBytesPerSec) 831 dlp.d_secsize = bpb->bpbBytesPerSec; 832 if (ioctl(fd, DIOCGDINFO, &dlp) == -1) { 833 if (bpb->bpbBytesPerSec == 0 && ioctl(fd, DIOCGSECTORSIZE, 834 &dlp.d_secsize) == -1) 835 errx(1, "Cannot get sector size, %s", strerror(errno)); 836 837 dlp.d_secperunit = ms / dlp.d_secsize; 838 839 if (bpb->bpbSecPerTrack == 0 && ioctl(fd, DIOCGFWSECTORS, 840 &dlp.d_nsectors) == -1) { 841 warnx("Cannot get number of sectors per track, %s", strerror(errno)); 842 dlp.d_nsectors = 63; 843 } 844 if (bpb->bpbHeads == 0 && 845 ioctl(fd, DIOCGFWHEADS, &dlp.d_ntracks) == -1) { 846 warnx("Cannot get number of heads, %s", strerror(errno)); 847 if (dlp.d_secperunit <= 63*1*1024) 848 dlp.d_ntracks = 1; 849 else if (dlp.d_secperunit <= 63*16*1024) 850 dlp.d_ntracks = 16; 851 else 852 dlp.d_ntracks = 255; 853 } 854 } 855 856 hs = (ms / dlp.d_secsize) - dlp.d_secperunit; 857 lp = &dlp; 858 } 859 860 if (bpb->bpbBytesPerSec == 0) 861 bpb->bpbBytesPerSec = ckgeom(fname, lp->d_secsize, "bytes/sector"); 862 if (bpb->bpbSecPerTrack == 0) 863 bpb->bpbSecPerTrack = ckgeom(fname, lp->d_nsectors, "sectors/track"); 864 if (bpb->bpbHeads == 0) 865 bpb->bpbHeads = ckgeom(fname, lp->d_ntracks, "drive heads"); 866 if (bpb->bpbHugeSectors == 0) 867 bpb->bpbHugeSectors = lp->d_secperunit; 868 if (bpb->bpbHiddenSecs == 0) 869 bpb->bpbHiddenSecs = hs; 870} 871 872/* 873 * Print out BPB values. 874 */ 875static void 876print_bpb(struct bpb *bpb) 877{ 878 printf("BytesPerSec=%u SecPerClust=%u ResSectors=%u FATs=%u", 879 bpb->bpbBytesPerSec, bpb->bpbSecPerClust, bpb->bpbResSectors, 880 bpb->bpbFATs); 881 if (bpb->bpbRootDirEnts) 882 printf(" RootDirEnts=%u", bpb->bpbRootDirEnts); 883 if (bpb->bpbSectors) 884 printf(" Sectors=%u", bpb->bpbSectors); 885 printf(" Media=%#x", bpb->bpbMedia); 886 if (bpb->bpbFATsecs) 887 printf(" FATsecs=%u", bpb->bpbFATsecs); 888 printf(" SecPerTrack=%u Heads=%u HiddenSecs=%u", bpb->bpbSecPerTrack, 889 bpb->bpbHeads, bpb->bpbHiddenSecs); 890 if (bpb->bpbHugeSectors) 891 printf(" HugeSectors=%u", bpb->bpbHugeSectors); 892 if (!bpb->bpbFATsecs) { 893 printf(" FATsecs=%u RootCluster=%u", bpb->bpbBigFATsecs, 894 bpb->bpbRootClust); 895 printf(" FSInfo="); 896 printf(bpb->bpbFSInfo == MAXU16 ? "%#x" : "%u", bpb->bpbFSInfo); 897 printf(" Backup="); 898 printf(bpb->bpbBackup == MAXU16 ? "%#x" : "%u", bpb->bpbBackup); 899 } 900 printf("\n"); 901} 902 903/* 904 * Check a disk geometry value. 905 */ 906static u_int 907ckgeom(const char *fname, u_int val, const char *msg) 908{ 909 if (!val) 910 errx(1, "%s: no default %s", fname, msg); 911 if (val > MAXU16) 912 errx(1, "%s: illegal %s %d", fname, msg, val); 913 return val; 914} 915 916/* 917 * Convert and check a numeric option argument. 918 */ 919static u_int 920argtou(const char *arg, u_int lo, u_int hi, const char *msg) 921{ 922 char *s; 923 u_long x; 924 925 errno = 0; 926 x = strtoul(arg, &s, 0); 927 if (errno || !*arg || *s || x < lo || x > hi) 928 errx(1, "%s: bad %s", arg, msg); 929 return x; 930} 931 932/* 933 * Same for off_t, with optional skmgpP suffix 934 */ 935static off_t 936argtooff(const char *arg, const char *msg) 937{ 938 char *s; 939 off_t x; 940 941 errno = 0; 942 x = strtoll(arg, &s, 0); 943 /* allow at most one extra char */ 944 if (errno || x < 0 || (s[0] && s[1]) ) 945 errx(1, "%s: bad %s", arg, msg); 946 if (*s) { /* the extra char is the multiplier */ 947 switch (*s) { 948 default: 949 errx(1, "%s: bad %s", arg, msg); 950 /* notreached */ 951 952 case 's': /* sector */ 953 case 'S': 954 x <<= 9; /* times 512 */ 955 break; 956 957 case 'k': /* kilobyte */ 958 case 'K': 959 x <<= 10; /* times 1024 */ 960 break; 961 962 case 'm': /* megabyte */ 963 case 'M': 964 x <<= 20; /* times 1024*1024 */ 965 break; 966 967 case 'g': /* gigabyte */ 968 case 'G': 969 x <<= 30; /* times 1024*1024*1024 */ 970 break; 971 972 case 'p': /* partition start */ 973 case 'P': 974 case 'l': /* partition length */ 975 case 'L': 976 errx(1, "%s: not supported yet %s", arg, msg); 977 /* notreached */ 978 } 979 } 980 return x; 981} 982 983/* 984 * Check a volume label. 985 */ 986static int 987oklabel(const char *src) 988{ 989 int c, i; 990 991 for (i = 0; i <= 11; i++) { 992 c = (u_char)*src++; 993 if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c)) 994 break; 995 } 996 return i && !c; 997} 998 999/* 1000 * Make a volume label. 1001 */ 1002static void 1003mklabel(u_int8_t *dest, const char *src) 1004{ 1005 int c, i; 1006 1007 for (i = 0; i < 11; i++) { 1008 c = *src ? toupper(*src++) : ' '; 1009 *dest++ = !i && c == '\xe5' ? 5 : c; 1010 } 1011} 1012 1013/* 1014 * Copy string, padding with spaces. 1015 */ 1016static void 1017setstr(u_int8_t *dest, const char *src, size_t len) 1018{ 1019 while (len--) 1020 *dest++ = *src ? *src++ : ' '; 1021} 1022 1023/* 1024 * Print usage message. 1025 */ 1026static void 1027usage(void) 1028{ 1029 fprintf(stderr, 1030 "usage: newfs_msdos [ -options ] special [disktype]\n" 1031 "where the options are:\n" 1032 "\t-@ create file system at specified offset\n" 1033 "\t-B get bootstrap from file\n" 1034 "\t-C create image file with specified size\n" 1035 "\t-F FAT type (12, 16, or 32)\n" 1036 "\t-I volume ID\n" 1037 "\t-L volume label\n" 1038 "\t-N don't create file system: just print out parameters\n" 1039 "\t-O OEM string\n" 1040 "\t-S bytes/sector\n" 1041 "\t-a sectors/FAT\n" 1042 "\t-b block size\n" 1043 "\t-c sectors/cluster\n" 1044 "\t-e root directory entries\n" 1045 "\t-f standard format\n" 1046 "\t-h drive heads\n" 1047 "\t-i file system info sector\n" 1048 "\t-k backup boot sector\n" 1049 "\t-m media descriptor\n" 1050 "\t-n number of FATs\n" 1051 "\t-o hidden sectors\n" 1052 "\t-r reserved sectors\n" 1053 "\t-s file system size (sectors)\n" 1054 "\t-u sectors/track\n"); 1055 exit(1); 1056} 1057 1058static void 1059infohandler(int sig __unused) 1060{ 1061 1062 got_siginfo = 1; 1063} 1064