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