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