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