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