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