1 /* $NetBSD: newfs_msdos.c,v 1.33 2009/03/28 21:34:33 he Exp $ */
4 * Copyright (c) 1998 Robert Nordier
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
30 #include <sys/cdefs.h>
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 $";
36 __RCSID("$NetBSD: newfs_msdos.c,v 1.33 2009/03/28 21:34:33 he Exp $");
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/mount.h>
64 #define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
65 #define BPN 4 /* bits per nibble */
66 #define NPB 2 /* nibbles per byte */
68 #define DOSMAGIC 0xaa55 /* DOS magic number */
69 #define MINBPS 512 /* minimum bytes per sector */
70 #define MAXSPC 128 /* maximum sectors per cluster */
71 #define MAXNFT 16 /* maximum number of FATs */
72 #define DEFBLK 4096 /* default block size */
73 #define DEFBLK16 2048 /* default block size FAT16 */
74 #define DEFRDE 512 /* default root directory entries */
75 #define RESFTE 2 /* reserved FAT entries */
76 #define MINCLS12 1 /* minimum FAT12 clusters */
77 #define MINCLS16 0x1000 /* minimum FAT16 clusters */
78 #define MINCLS32 2 /* minimum FAT32 clusters */
79 #define MAXCLS12 0xfed /* maximum FAT12 clusters */
80 #define MAXCLS16 0xfff5 /* maximum FAT16 clusters */
81 #define MAXCLS32 0xffffff5 /* maximum FAT32 clusters */
83 #define mincls(fat) ((fat) == 12 ? MINCLS12 : \
84 (fat) == 16 ? MINCLS16 : \
87 #define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \
88 (fat) == 16 ? MAXCLS16 : \
95 (p)[0] = (u_int8_t)(x), \
96 (p)[1] = (u_int8_t)((x) >> 010)
99 (p)[0] = (u_int8_t)(x), \
100 (p)[1] = (u_int8_t)((x) >> 010), \
101 (p)[2] = (u_int8_t)((x) >> 020), \
102 (p)[3] = (u_int8_t)((x) >> 030)
104 #define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
105 #define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg)
106 #define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg)
107 #define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
110 u_int8_t jmp
[3]; /* bootstrap entry point */
111 u_int8_t oem
[8]; /* OEM name and version */
115 u_int8_t bps
[2]; /* bytes per sector */
116 u_int8_t spc
; /* sectors per cluster */
117 u_int8_t res
[2]; /* reserved sectors */
118 u_int8_t nft
; /* number of FATs */
119 u_int8_t rde
[2]; /* root directory entries */
120 u_int8_t sec
[2]; /* total sectors */
121 u_int8_t mid
; /* media descriptor */
122 u_int8_t spf
[2]; /* sectors per FAT */
123 u_int8_t spt
[2]; /* sectors per track */
124 u_int8_t hds
[2]; /* drive heads */
125 u_int8_t hid
[4]; /* hidden sectors */
126 u_int8_t bsec
[4]; /* big total sectors */
130 u_int8_t bspf
[4]; /* big sectors per FAT */
131 u_int8_t xflg
[2]; /* FAT control flags */
132 u_int8_t vers
[2]; /* file system version */
133 u_int8_t rdcl
[4]; /* root directory start cluster */
134 u_int8_t infs
[2]; /* file system info sector */
135 u_int8_t bkbs
[2]; /* backup boot sector */
136 u_int8_t rsvd
[12]; /* reserved */
140 u_int8_t drv
; /* drive number */
141 u_int8_t rsvd
; /* reserved */
142 u_int8_t sig
; /* extended boot signature */
143 u_int8_t volid
[4]; /* volume ID number */
144 u_int8_t label
[11]; /* volume label */
145 u_int8_t type
[8]; /* file system type */
149 u_int8_t namext
[11]; /* name and extension */
150 u_int8_t attr
; /* attributes */
151 u_int8_t rsvd
[10]; /* reserved */
152 u_int8_t time
[2]; /* creation time */
153 u_int8_t date
[2]; /* creation date */
154 u_int8_t clus
[2]; /* starting cluster */
155 u_int8_t size
[4]; /* size */
159 u_int bps
; /* bytes per sector */
160 u_int spc
; /* sectors per cluster */
161 u_int res
; /* reserved sectors */
162 u_int nft
; /* number of FATs */
163 u_int rde
; /* root directory entries */
164 u_int sec
; /* total sectors */
165 u_int mid
; /* media descriptor */
166 u_int spf
; /* sectors per FAT */
167 u_int spt
; /* sectors per track */
168 u_int hds
; /* drive heads */
169 u_int hid
; /* hidden sectors */
170 u_int bsec
; /* big total sectors */
171 u_int bspf
; /* big sectors per FAT */
172 u_int rdcl
; /* root directory start cluster */
173 u_int infs
; /* file system info sector */
174 u_int bkbs
; /* backup boot sector */
177 #define INIT(a, b, c, d, e, f, g, h, i, j) \
178 { .bps = a, .spc = b, .res = c, .nft = d, .rde = e, \
179 .sec = f, .mid = g, .spf = h, .spt = i, .hds = j, }
184 {"160", INIT(512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1)},
185 {"180", INIT(512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1)},
186 {"320", INIT(512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2)},
187 {"360", INIT(512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2)},
188 {"640", INIT(512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2)},
189 {"720", INIT(512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2)},
190 {"1200", INIT(512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2)},
191 {"1232", INIT(1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2)},
192 {"1440", INIT(512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2)},
193 {"2880", INIT(512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2)}
196 static u_int8_t bootcode
[] = {
198 0x31, 0xc0, /* xor ax,ax */
199 0x8e, 0xd0, /* mov ss,ax */
200 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
202 0x8e, 0xd8, /* mov ds,ax */
203 0xe8, 0x00, 0x00, /* call $ + 3 */
205 0x83, 0xc6, 0x19, /* add si,+19h */
206 0xbb, 0x07, 0x00, /* mov bx,0007h */
209 0x84, 0xc0, /* test al,al */
210 0x74, 0x06, /* jz $ + 8 */
211 0xb4, 0x0e, /* mov ah,0eh */
212 0xcd, 0x10, /* int 10h */
213 0xeb, 0xf5, /* jmp $ - 9 */
214 0x30, 0xe4, /* xor ah,ah */
215 0xcd, 0x16, /* int 16h */
216 0xcd, 0x19, /* int 19h */
218 'N', 'o', 'n', '-', 's', 'y', 's', 't',
219 'e', 'm', ' ', 'd', 'i', 's', 'k',
221 'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
222 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
223 ' ', 'r', 'e', 'b', 'o', 'o', 't',
228 static int got_siginfo
= 0; /* received a SIGINFO */
230 static void check_mounted(const char *, mode_t
);
231 static void getstdfmt(const char *, struct bpb
*);
232 static void getbpbinfo(int, const char *, const char *, int, struct bpb
*, int);
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 off_t
argtooff(const char *, const char *);
237 static int oklabel(const char *);
238 static void mklabel(u_int8_t
*, const char *);
239 static void setstr(u_int8_t
*, const char *, size_t);
240 static void usage(void);
241 static void infohandler(int sig
);
244 * Construct a FAT12, FAT16, or FAT32 file system.
247 main(int argc
, char *argv
[])
249 static char opts
[] = "@:NB:C:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
250 static const char *opt_B
, *opt_L
, *opt_O
, *opt_f
;
251 static u_int opt_F
, opt_I
, opt_S
, opt_a
, opt_b
, opt_c
, opt_e
;
252 static u_int opt_h
, opt_i
, opt_k
, opt_m
, opt_n
, opt_o
, opt_r
;
253 static u_int opt_s
, opt_u
;
255 static int Iflag
, mflag
, oflag
;
256 char buf
[MAXPATHLEN
];
263 struct bsxbpb
*bsxbpb
;
267 const char *fname
, *dtype
, *bname
;
270 u_int fat
, bss
, rds
, cls
, dir
, lsn
, x
, x1
, x2
;
272 static off_t opt_create
=0, opt_ofs
=0;
274 while ((ch
= getopt(argc
, argv
, opts
)) != -1)
277 opt_ofs
= argtooff(optarg
, "offset");
286 opt_create
= argtooff(optarg
, "create size");
289 if (strcmp(optarg
, "12") &&
290 strcmp(optarg
, "16") &&
291 strcmp(optarg
, "32"))
292 errx(1, "%s: bad FAT type", optarg
);
293 opt_F
= atoi(optarg
);
296 opt_I
= argto4(optarg
, 0, "volume ID");
300 if (!oklabel(optarg
))
301 errx(1, "%s: bad volume label", optarg
);
305 if (strlen(optarg
) > 8)
306 errx(1, "%s: bad OEM string", optarg
);
310 opt_S
= argto2(optarg
, 1, "bytes/sector");
313 opt_a
= argto4(optarg
, 1, "sectors/FAT");
316 opt_b
= argtox(optarg
, 1, "block size");
320 opt_c
= argto1(optarg
, 1, "sectors/cluster");
324 opt_e
= argto2(optarg
, 1, "directory entries");
330 opt_h
= argto2(optarg
, 1, "drive heads");
333 opt_i
= argto2(optarg
, 1, "info sector");
336 opt_k
= argto2(optarg
, 1, "backup sector");
339 opt_m
= argto1(optarg
, 0, "media descriptor");
343 opt_n
= argto1(optarg
, 1, "number of FATs");
346 opt_o
= argto4(optarg
, 0, "hidden sectors");
350 opt_r
= argto2(optarg
, 1, "reserved sectors");
353 opt_s
= argto4(optarg
, 1, "file system size");
356 opt_u
= argto2(optarg
, 1, "sectors/track");
363 if (argc
< 1 || argc
> 2)
366 if (!strchr(fname
, '/') && !opt_create
) {
367 snprintf(buf
, sizeof(buf
), "%sr%s", _PATH_DEV
, fname
);
368 if (!(fname
= strdup(buf
)))
376 errx(1, "create (-C) is incompatible with -N");
377 fd
= open(fname
, O_RDWR
| O_CREAT
| O_TRUNC
, 0644);
379 errx(1, "failed to create %s", fname
);
380 pos
= lseek(fd
, opt_create
- 1, SEEK_SET
);
381 if (write(fd
, "\0", 1) != 1)
382 err(1, "failed to set file size");
383 pos
= lseek(fd
, 0, SEEK_SET
);
384 } else if ((fd
= open(fname
, opt_N
? O_RDONLY
: O_RDWR
)) == -1 ||
388 check_mounted(fname
, sb
.st_mode
);
389 if (!S_ISCHR(sb
.st_mode
) && !opt_create
)
390 warnx("warning, %s is not a character device", fname
);
391 if (opt_ofs
&& opt_ofs
!= lseek(fd
, opt_ofs
, SEEK_SET
))
392 errx(1, "cannot seek to %jd", (intmax_t)opt_ofs
);
393 memset(&bpb
, 0, sizeof(bpb
));
395 getstdfmt(opt_f
, &bpb
);
411 if (!(opt_f
|| (opt_h
&& opt_u
&& opt_S
&& opt_s
&& oflag
))) {
413 getbpbinfo(fd
, fname
, dtype
, oflag
, &bpb
, opt_create
!= 0);
414 bpb
.bsec
-= (opt_ofs
/ bpb
.bps
);
415 delta
= bpb
.bsec
% bpb
.spt
;
417 warnx("trim %d sectors to adjust to a multiple of %d",
418 (int)delta
, bpb
.spt
);
421 if (bpb
.spc
== 0) { /* set defaults */
422 if (bpb
.bsec
<= 6000) /* about 3MB -> 512 bytes */
424 else if (bpb
.bsec
<= (1<<17)) /* 64M -> 4k */
426 else if (bpb
.bsec
<= (1<<19)) /* 256M -> 8k */
428 else if (bpb
.bsec
<= (1<<21)) /* 1G -> 16k */
431 bpb
.spc
= 64; /* otherwise 32k */
434 if (!powerof2(bpb
.bps
))
435 errx(1, "bytes/sector (%u) is not a power of 2", bpb
.bps
);
436 if (bpb
.bps
< MINBPS
)
437 errx(1, "bytes/sector (%u) is too small; minimum is %u",
439 if (!(fat
= opt_F
)) {
442 else if (!opt_e
&& (opt_i
|| opt_k
))
445 if ((fat
== 32 && opt_e
) || (fat
!= 32 && (opt_i
|| opt_k
)))
446 errx(1, "-%c is not a legal FAT%s option",
447 fat
== 32 ? 'e' : opt_i
? 'i' : 'k',
448 fat
== 32 ? "32" : "12/16");
449 if (opt_f
&& fat
== 32)
452 if (!powerof2(opt_b
))
453 errx(1, "block size (%u) is not a power of 2", opt_b
);
455 errx(1, "block size (%u) is too small; minimum is %u",
457 if (opt_b
> bpb
.bps
* MAXSPC
)
458 errx(1, "block size (%u) is too large; maximum is %u",
459 opt_b
, bpb
.bps
* MAXSPC
);
460 bpb
.spc
= opt_b
/ bpb
.bps
;
463 if (!powerof2(opt_c
))
464 errx(1, "sectors/cluster (%u) is not a power of 2", opt_c
);
471 errx(1, "number of FATs (%u) is too large; maximum is %u",
479 errx(1, "illegal media descriptor (%#x)", opt_m
);
493 if (!strchr(bname
, '/')) {
494 snprintf(buf
, sizeof(buf
), "/boot/%s", bname
);
495 if (!(bname
= strdup(buf
)))
498 if ((fd1
= open(bname
, O_RDONLY
)) == -1 || fstat(fd1
, &sb
))
500 if (!S_ISREG(sb
.st_mode
) || sb
.st_size
% bpb
.bps
||
501 sb
.st_size
< bpb
.bps
|| sb
.st_size
> bpb
.bps
* MAXU16
)
502 errx(1, "%s: inappropriate file type or format", bname
);
503 bss
= sb
.st_size
/ bpb
.bps
;
508 if (bpb
.bsec
< (bpb
.res
? bpb
.res
: bss
) +
509 howmany((RESFTE
+ (bpb
.spc
? MINCLS16
: MAXCLS12
+ 1)) *
510 ((bpb
.spc
? 16 : 12) / BPN
), bpb
.bps
* NPB
) *
512 howmany(bpb
.rde
? bpb
.rde
: DEFRDE
,
513 bpb
.bps
/ sizeof(struct de
)) +
514 (bpb
.spc
? MINCLS16
: MAXCLS12
+ 1) *
515 (bpb
.spc
? bpb
.spc
: howmany(DEFBLK
, bpb
.bps
)))
517 else if (bpb
.rde
|| bpb
.bsec
<
518 (bpb
.res
? bpb
.res
: bss
) +
519 howmany((RESFTE
+ MAXCLS16
) * 2, bpb
.bps
) * bpb
.nft
+
520 howmany(DEFRDE
, bpb
.bps
/ sizeof(struct de
)) +
522 (bpb
.spc
? bpb
.spc
: howmany(8192, bpb
.bps
)))
530 if (x
== MAXU16
|| x
== bpb
.bkbs
)
531 errx(1, "no room for info sector");
534 if (bpb
.infs
!= MAXU16
&& x
<= bpb
.infs
)
538 errx(1, "no room for backup sector");
540 } else if (bpb
.bkbs
!= MAXU16
&& bpb
.bkbs
== bpb
.infs
)
541 errx(1, "backup sector would overwrite info sector");
542 if (bpb
.bkbs
!= MAXU16
&& x
<= bpb
.bkbs
)
546 bpb
.res
= fat
== 32 ? MAX(x
, MAX(16384 / bpb
.bps
, 4)) : x
;
547 else if (bpb
.res
< x
)
548 errx(1, "too few reserved sectors (need %d have %d)", x
, bpb
.res
);
549 if (fat
!= 32 && !bpb
.rde
)
551 rds
= howmany(bpb
.rde
, bpb
.bps
/ sizeof(struct de
));
553 for (bpb
.spc
= howmany(fat
== 16 ? DEFBLK16
: DEFBLK
, bpb
.bps
);
556 howmany((RESFTE
+ maxcls(fat
)) * (fat
/ BPN
),
557 bpb
.bps
* NPB
) * bpb
.nft
+
559 (u_int64_t
)(maxcls(fat
) + 1) * bpb
.spc
<= bpb
.bsec
;
561 if (fat
!= 32 && bpb
.bspf
> MAXU16
)
562 errx(1, "too many sectors/FAT for FAT12/16");
564 x
= bpb
.bspf
? bpb
.bspf
: 1;
565 if (x1
+ (u_int64_t
)x
* bpb
.nft
> bpb
.bsec
)
566 errx(1, "meta data exceeds file system size");
568 x
= (u_int64_t
)(bpb
.bsec
- x1
) * bpb
.bps
* NPB
/
569 (bpb
.spc
* bpb
.bps
* NPB
+ fat
/ BPN
* bpb
.nft
);
570 x2
= howmany((RESFTE
+ MIN(x
, maxcls(fat
))) * (fat
/ BPN
),
574 x1
+= (bpb
.bspf
- 1) * bpb
.nft
;
576 cls
= (bpb
.bsec
- x1
) / bpb
.spc
;
577 x
= (u_int64_t
)bpb
.bspf
* bpb
.bps
* NPB
/ (fat
/ BPN
) - RESFTE
;
581 warnx("warning: sectors/FAT limits file system to %u clusters",
583 if (cls
< mincls(fat
))
584 errx(1, "%u clusters too few clusters for FAT%u, need %u", cls
, fat
,
586 if (cls
> maxcls(fat
)) {
588 bpb
.bsec
= x1
+ (cls
+ 1) * bpb
.spc
- 1;
589 warnx("warning: FAT type limits file system to %u sectors",
592 printf("%s: %u sector%s in %u FAT%u cluster%s "
593 "(%u bytes/cluster)\n", fname
, cls
* bpb
.spc
,
594 cls
* bpb
.spc
== 1 ? "" : "s", cls
, fat
,
595 cls
== 1 ? "" : "s", bpb
.bps
* bpb
.spc
);
597 bpb
.mid
= !bpb
.hid
? 0xf0 : 0xf8;
600 if (bpb
.hid
+ bpb
.bsec
<= MAXU16
) {
610 ch
= 1; /* 001 Primary DOS with 12 bit FAT */
611 else if (fat
== 16) {
613 ch
= 4; /* 004 Primary DOS with 16 bit FAT <32M */
615 ch
= 6; /* 006 Primary 'big' DOS, 16-bit FAT (> 32MB) */
618 * 014 DOS (16-bit FAT) - LBA
621 } else if (fat
== 32) {
622 ch
= 11; /* 011 Primary DOS with 32 bit FAT */
625 * 012 Primary DOS with 32 bit FAT - LBA
630 printf("MBR type: %d\n", ch
);
633 gettimeofday(&tv
, NULL
);
635 tm
= localtime(&now
);
636 if (!(img
= malloc(bpb
.bps
)))
638 dir
= bpb
.res
+ (bpb
.spf
? bpb
.spf
: bpb
.bspf
) * bpb
.nft
;
639 signal(SIGINFO
, infohandler
);
640 for (lsn
= 0; lsn
< dir
+ (fat
== 32 ? bpb
.spc
: rds
); lsn
++) {
642 fprintf(stderr
,"%s: writing sector %u of %u (%u%%)\n",
643 fname
,lsn
,(dir
+ (fat
== 32 ? bpb
.spc
: rds
)),
644 (lsn
*100)/(dir
+ (fat
== 32 ? bpb
.spc
: rds
)));
649 fat
== 32 && bpb
.bkbs
!= MAXU16
&&
650 bss
<= bpb
.bkbs
&& x
>= bpb
.bkbs
) {
652 if (!x
&& lseek(fd1
, opt_ofs
, SEEK_SET
))
655 if (opt_B
&& x
< bss
) {
656 if ((n
= read(fd1
, img
, bpb
.bps
)) == -1)
658 if ((size_t)n
!= bpb
.bps
)
659 errx(1, "%s: can't read sector %u", bname
, x
);
661 memset(img
, 0, bpb
.bps
);
663 (fat
== 32 && bpb
.bkbs
!= MAXU16
&& lsn
== bpb
.bkbs
)) {
664 x1
= sizeof(struct bs
);
665 bsbpb
= (struct bsbpb
*)(img
+ x1
);
666 mk2(bsbpb
->bps
, bpb
.bps
);
667 mk1(bsbpb
->spc
, bpb
.spc
);
668 mk2(bsbpb
->res
, bpb
.res
);
669 mk1(bsbpb
->nft
, bpb
.nft
);
670 mk2(bsbpb
->rde
, bpb
.rde
);
671 mk2(bsbpb
->sec
, bpb
.sec
);
672 mk1(bsbpb
->mid
, bpb
.mid
);
673 mk2(bsbpb
->spf
, bpb
.spf
);
674 mk2(bsbpb
->spt
, bpb
.spt
);
675 mk2(bsbpb
->hds
, bpb
.hds
);
676 mk4(bsbpb
->hid
, bpb
.hid
);
677 mk4(bsbpb
->bsec
, bpb
.bsec
);
678 x1
+= sizeof(struct bsbpb
);
680 bsxbpb
= (struct bsxbpb
*)(img
+ x1
);
681 mk4(bsxbpb
->bspf
, bpb
.bspf
);
682 mk2(bsxbpb
->xflg
, 0);
683 mk2(bsxbpb
->vers
, 0);
684 mk4(bsxbpb
->rdcl
, bpb
.rdcl
);
685 mk2(bsxbpb
->infs
, bpb
.infs
);
686 mk2(bsxbpb
->bkbs
, bpb
.bkbs
);
687 x1
+= sizeof(struct bsxbpb
);
689 bsx
= (struct bsx
*)(img
+ x1
);
694 x
= (((u_int
)(1 + tm
->tm_mon
) << 8 |
695 (u_int
)tm
->tm_mday
) +
696 ((u_int
)tm
->tm_sec
<< 8 |
697 (u_int
)(tv
.tv_usec
/ 10))) << 16 |
698 ((u_int
)(1900 + tm
->tm_year
) +
699 ((u_int
)tm
->tm_hour
<< 8 |
702 mklabel(bsx
->label
, opt_L
? opt_L
: "NO_NAME");
703 snprintf(buf
, sizeof(buf
), "FAT%u", fat
);
704 setstr(bsx
->type
, buf
, sizeof(bsx
->type
));
706 x1
+= sizeof(struct bsx
);
707 bs
= (struct bs
*)img
;
708 mk1(bs
->jmp
[0], 0xeb);
709 mk1(bs
->jmp
[1], x1
- 2);
710 mk1(bs
->jmp
[2], 0x90);
711 setstr(bs
->oem
, opt_O
? opt_O
: "NetBSD",
713 memcpy(img
+ x1
, bootcode
, sizeof(bootcode
));
714 mk2(img
+ MINBPS
- 2, DOSMAGIC
);
716 } else if (fat
== 32 && bpb
.infs
!= MAXU16
&&
718 (bpb
.bkbs
!= MAXU16
&&
719 lsn
== bpb
.bkbs
+ bpb
.infs
))) {
720 mk4(img
, 0x41615252);
721 mk4(img
+ MINBPS
- 28, 0x61417272);
722 mk4(img
+ MINBPS
- 24, 0xffffffff);
723 mk4(img
+ MINBPS
- 20, bpb
.rdcl
);
724 mk2(img
+ MINBPS
- 2, DOSMAGIC
);
725 } else if (lsn
>= bpb
.res
&& lsn
< dir
&&
727 (bpb
.spf
? bpb
.spf
: bpb
.bspf
))) {
728 mk1(img
[0], bpb
.mid
);
729 for (x
= 1; x
< fat
* (fat
== 32 ? 3 : 2) / 8; x
++)
730 mk1(img
[x
], fat
== 32 && x
% 4 == 3 ? 0x0f : 0xff);
731 } else if (lsn
== dir
&& opt_L
) {
732 de
= (struct de
*)img
;
733 mklabel(de
->namext
, opt_L
);
735 x
= (u_int
)tm
->tm_hour
<< 11 |
736 (u_int
)tm
->tm_min
<< 5 |
737 (u_int
)tm
->tm_sec
>> 1;
739 x
= (u_int
)(tm
->tm_year
- 80) << 9 |
740 (u_int
)(tm
->tm_mon
+ 1) << 5 |
744 if ((n
= write(fd
, img
, bpb
.bps
)) == -1)
746 if ((size_t)n
!= bpb
.bps
)
747 errx(1, "%s: can't write sector %u", fname
, lsn
);
754 * Exit with error if file system is mounted.
757 check_mounted(const char *fname
, mode_t mode
)
764 if (!(n
= getmntinfo(&mp
, MNT_NOWAIT
)))
765 err(1, "getmntinfo");
766 len
= strlen(_PATH_DEV
);
768 if (!strncmp(s1
, _PATH_DEV
, len
))
770 r
= S_ISCHR(mode
) && s1
!= fname
&& *s1
== 'r';
772 s2
= mp
->f_mntfromname
;
773 if (!strncmp(s2
, _PATH_DEV
, len
))
775 if ((r
&& s2
!= mp
->f_mntfromname
&& !strcmp(s1
+ 1, s2
)) ||
777 errx(1, "%s is mounted on %s", fname
, mp
->f_mntonname
);
782 * Get a standard format.
785 getstdfmt(const char *fmt
, struct bpb
*bpb
)
789 x
= sizeof(stdfmt
) / sizeof(stdfmt
[0]);
790 for (i
= 0; i
< x
&& strcmp(fmt
, stdfmt
[i
].name
); i
++);
792 errx(1, "%s: unknown standard format", fmt
);
793 *bpb
= stdfmt
[i
].bpb
;
797 * Get disk slice, partition, and geometry information.
800 getbpbinfo(int fd
, const char *fname
, const char *dtype
, int oflag
,
801 struct bpb
*bpb
, int create
)
803 struct disk_geom geo
;
804 struct dkwedge_info dkw
;
811 if ((s2
= strrchr(s1
, '/')))
813 for (s2
= s1
; *s2
&& !isdigit((unsigned char)*s2
); s2
++);
814 if (!*s2
|| s2
== s1
)
817 while (isdigit((unsigned char)*++s2
));
820 maxpartitions
= getmaxpartitions();
822 if (s2
&& *s2
>= 'a' && *s2
<= 'a' + maxpartitions
- 1) {
825 if (((part
!= -1) && ((!oflag
&& part
!= -1) || !bpb
->bsec
)) ||
826 !bpb
->bps
|| !bpb
->spt
|| !bpb
->hds
) {
827 if (create
|| getdiskinfo(fname
, fd
, NULL
, &geo
, &dkw
) == -1) {
830 if (fstat(fd
, &st
) == -1)
831 errx(1, "Can't get disk size for `%s'", fname
);
832 /* create a fake geometry for a file image */
833 geo
.dg_secsize
= 512;
834 geo
.dg_nsectors
= 63;
835 geo
.dg_ntracks
= 255;
836 dkw
.dkw_size
= st
.st_size
/ geo
.dg_secsize
;
839 bpb
->bps
= ckgeom(fname
, geo
.dg_secsize
, "bytes/sector");
841 if (geo
.dg_nsectors
> 63) {
843 * The kernel doesn't accept BPB with spt > 63.
844 * (see sys/fs/msdosfs/msdosfs_vfsops.c:msdosfs_mountfs())
845 * If values taken from disklabel don't match these
846 * restrictions, use popular BIOS default values instead.
848 geo
.dg_nsectors
= 63;
851 bpb
->spt
= ckgeom(fname
, geo
.dg_nsectors
, "sectors/track");
853 bpb
->hds
= ckgeom(fname
, geo
.dg_ntracks
, "drive heads");
855 bpb
->bsec
= dkw
.dkw_size
;
860 * Print out BPB values.
863 print_bpb(struct bpb
*bpb
)
865 printf("bps=%u spc=%u res=%u nft=%u", bpb
->bps
, bpb
->spc
, bpb
->res
,
868 printf(" rde=%u", bpb
->rde
);
870 printf(" sec=%u", bpb
->sec
);
871 printf(" mid=%#x", bpb
->mid
);
873 printf(" spf=%u", bpb
->spf
);
874 printf(" spt=%u hds=%u hid=%u", bpb
->spt
, bpb
->hds
, bpb
->hid
);
876 printf(" bsec=%u", bpb
->bsec
);
878 printf(" bspf=%u rdcl=%u", bpb
->bspf
, bpb
->rdcl
);
880 printf(bpb
->infs
== MAXU16
? "%#x" : "%u", bpb
->infs
);
882 printf(bpb
->bkbs
== MAXU16
? "%#x" : "%u", bpb
->bkbs
);
888 * Check a disk geometry value.
891 ckgeom(const char *fname
, u_int val
, const char *msg
)
894 errx(1, "%s: no default %s", fname
, msg
);
896 errx(1, "%s: illegal %s", fname
, msg
);
901 * Convert and check a numeric option argument.
904 argtou(const char *arg
, u_int lo
, u_int hi
, const char *msg
)
910 x
= strtoul(arg
, &s
, 0);
911 if (errno
|| !*arg
|| *s
|| x
< lo
|| x
> hi
)
912 errx(1, "%s: bad %s", arg
, msg
);
917 * Same for off_t, with optional skmgpP suffix
920 argtooff(const char *arg
, const char *msg
)
925 x
= strtoll(arg
, &s
, 0);
926 /* allow at most one extra char */
927 if (errno
|| x
< 0 || (s
[0] && s
[1]) )
928 errx(1, "%s: bad %s", arg
, msg
);
929 if (*s
) { /* the extra char is the multiplier */
932 errx(1, "%s: bad %s", arg
, msg
);
935 case 's': /* sector */
937 x
<<= 9; /* times 512 */
940 case 'k': /* kilobyte */
942 x
<<= 10; /* times 1024 */
945 case 'm': /* megabyte */
947 x
<<= 20; /* times 1024*1024 */
950 case 'g': /* gigabyte */
952 x
<<= 30; /* times 1024*1024*1024 */
955 case 'p': /* partition start */
956 case 'P': /* partition start */
957 case 'l': /* partition length */
958 case 'L': /* partition length */
959 errx(1, "%s: not supported yet %s", arg
, msg
);
967 * Check a volume label.
970 oklabel(const char *src
)
974 for (i
= 0; i
<= 11; i
++) {
976 if (c
< ' ' + !i
|| strchr("\"*+,./:;<=>?[\\]|", c
))
983 * Make a volume label.
986 mklabel(u_int8_t
*dest
, const char *src
)
990 for (i
= 0; i
< 11; i
++) {
991 c
= *src
? toupper((unsigned char)*src
++) : ' ';
992 *dest
++ = !i
&& c
== '\xe5' ? 5 : c
;
997 * Copy string, padding with spaces.
1000 setstr(u_int8_t
*dest
, const char *src
, size_t len
)
1003 *dest
++ = *src
? *src
++ : ' ';
1007 * Print usage message.
1013 "usage: %s [ -options ] special [disktype]\n", getprogname());
1014 fprintf(stderr
, "where the options are:\n");
1015 fprintf(stderr
, "\t-N don't create file system: "
1016 "just print out parameters\n");
1017 fprintf(stderr
, "\t-B get bootstrap from file\n");
1018 fprintf(stderr
, "\t-F FAT type (12, 16, or 32)\n");
1019 fprintf(stderr
, "\t-I volume ID\n");
1020 fprintf(stderr
, "\t-L volume label\n");
1021 fprintf(stderr
, "\t-O OEM string\n");
1022 fprintf(stderr
, "\t-S bytes/sector\n");
1023 fprintf(stderr
, "\t-a sectors/FAT\n");
1024 fprintf(stderr
, "\t-b block size\n");
1025 fprintf(stderr
, "\t-c sectors/cluster\n");
1026 fprintf(stderr
, "\t-e root directory entries\n");
1027 fprintf(stderr
, "\t-f standard format\n");
1028 fprintf(stderr
, "\t-h drive heads\n");
1029 fprintf(stderr
, "\t-i file system info sector\n");
1030 fprintf(stderr
, "\t-k backup boot sector\n");
1031 fprintf(stderr
, "\t-m media descriptor\n");
1032 fprintf(stderr
, "\t-n number of FATs\n");
1033 fprintf(stderr
, "\t-o hidden sectors\n");
1034 fprintf(stderr
, "\t-r reserved sectors\n");
1035 fprintf(stderr
, "\t-s file system size (sectors)\n");
1036 fprintf(stderr
, "\t-u sectors/track\n");
1041 infohandler(int sig
)