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