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