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