2 * Copyright (c) 1998 Robert Nordier
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * $FreeBSD: src/sbin/newfs_msdos/newfs_msdos.c,v 1.9.2.4 2001/08/01 08:43:04 obrien Exp $
28 * $DragonFly: src/sbin/newfs_msdos/newfs_msdos.c,v 1.6 2007/07/30 23:56:29 dillon Exp $
31 #include <sys/param.h>
32 #include <sys/diskslice.h>
33 #include <sys/diskmbr.h>
34 #include <sys/mount.h>
50 #define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
51 #define BPN 4 /* bits per nibble */
52 #define NPB 2 /* nibbles per byte */
54 #define MINBPS 128 /* minimum bytes per sector */
55 #define MAXSPC 128 /* maximum sectors per cluster */
56 #define MAXNFT 16 /* maximum number of FATs */
57 #define DEFBLK 4096 /* default block size */
58 #define DEFBLK16 2048 /* default block size FAT16 */
59 #define DEFRDE 512 /* default root directory entries */
60 #define RESFTE 2 /* reserved FAT entries */
61 #define MINCLS12 1 /* minimum FAT12 clusters */
62 #define MINCLS16 0x1000 /* minimum FAT16 clusters */
63 #define MINCLS32 2 /* minimum FAT32 clusters */
64 #define MAXCLS12 0xfed /* maximum FAT12 clusters */
65 #define MAXCLS16 0xfff5 /* maximum FAT16 clusters */
66 #define MAXCLS32 0xffffff5 /* maximum FAT32 clusters */
68 #define mincls(fat) ((fat) == 12 ? MINCLS12 : \
69 (fat) == 16 ? MINCLS16 : \
72 #define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \
73 (fat) == 16 ? MAXCLS16 : \
80 (p)[0] = (u_int8_t)(x), \
81 (p)[1] = (u_int8_t)((x) >> 010)
84 (p)[0] = (u_int8_t)(x), \
85 (p)[1] = (u_int8_t)((x) >> 010), \
86 (p)[2] = (u_int8_t)((x) >> 020), \
87 (p)[3] = (u_int8_t)((x) >> 030)
89 #define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
90 #define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg)
91 #define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg)
92 #define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
95 u_int8_t jmp
[3]; /* bootstrap entry point */
96 u_int8_t oem
[8]; /* OEM name and version */
100 u_int8_t bps
[2]; /* bytes per sector */
101 u_int8_t spc
; /* sectors per cluster */
102 u_int8_t res
[2]; /* reserved sectors */
103 u_int8_t nft
; /* number of FATs */
104 u_int8_t rde
[2]; /* root directory entries */
105 u_int8_t sec
[2]; /* total sectors */
106 u_int8_t mid
; /* media descriptor */
107 u_int8_t spf
[2]; /* sectors per FAT */
108 u_int8_t spt
[2]; /* sectors per track */
109 u_int8_t hds
[2]; /* drive heads */
110 u_int8_t hid
[4]; /* hidden sectors */
111 u_int8_t bsec
[4]; /* big total sectors */
115 u_int8_t bspf
[4]; /* big sectors per FAT */
116 u_int8_t xflg
[2]; /* FAT control flags */
117 u_int8_t vers
[2]; /* file system version */
118 u_int8_t rdcl
[4]; /* root directory start cluster */
119 u_int8_t infs
[2]; /* file system info sector */
120 u_int8_t bkbs
[2]; /* backup boot sector */
121 u_int8_t rsvd
[12]; /* reserved */
125 u_int8_t drv
; /* drive number */
126 u_int8_t rsvd
; /* reserved */
127 u_int8_t sig
; /* extended boot signature */
128 u_int8_t volid
[4]; /* volume ID number */
129 u_int8_t label
[11]; /* volume label */
130 u_int8_t type
[8]; /* file system type */
134 u_int8_t namext
[11]; /* name and extension */
135 u_int8_t attr
; /* attributes */
136 u_int8_t rsvd
[10]; /* reserved */
137 u_int8_t time
[2]; /* creation time */
138 u_int8_t date
[2]; /* creation date */
139 u_int8_t clus
[2]; /* starting cluster */
140 u_int8_t size
[4]; /* size */
144 u_int bps
; /* bytes per sector */
145 u_int spc
; /* sectors per cluster */
146 u_int res
; /* reserved sectors */
147 u_int nft
; /* number of FATs */
148 u_int rde
; /* root directory entries */
149 u_int sec
; /* total sectors */
150 u_int mid
; /* media descriptor */
151 u_int spf
; /* sectors per FAT */
152 u_int spt
; /* sectors per track */
153 u_int hds
; /* drive heads */
154 u_int hid
; /* hidden sectors */
155 u_int bsec
; /* big total sectors */
156 u_int bspf
; /* big sectors per FAT */
157 u_int rdcl
; /* root directory start cluster */
158 u_int infs
; /* file system info sector */
159 u_int bkbs
; /* backup boot sector */
166 {"160", {512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1}},
167 {"180", {512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1}},
168 {"320", {512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2}},
169 {"360", {512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2}},
170 {"640", {512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2}},
171 {"720", {512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2}},
172 {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2}},
173 {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2}},
174 {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2}},
175 {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2}}
178 static u_int8_t bootcode
[] = {
180 0x31, 0xc0, /* xor ax,ax */
181 0x8e, 0xd0, /* mov ss,ax */
182 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
184 0x8e, 0xd8, /* mov ds,ax */
185 0xe8, 0x00, 0x00, /* call $ + 3 */
187 0x83, 0xc6, 0x19, /* add si,+19h */
188 0xbb, 0x07, 0x00, /* mov bx,0007h */
191 0x84, 0xc0, /* test al,al */
192 0x74, 0x06, /* jz $ + 8 */
193 0xb4, 0x0e, /* mov ah,0eh */
194 0xcd, 0x10, /* int 10h */
195 0xeb, 0xf5, /* jmp $ - 9 */
196 0x30, 0xe4, /* xor ah,ah */
197 0xcd, 0x16, /* int 16h */
198 0xcd, 0x19, /* int 19h */
200 'N', 'o', 'n', '-', 's', 'y', 's', 't',
201 'e', 'm', ' ', 'd', 'i', 's', 'k',
203 'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
204 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
205 ' ', 'r', 'e', 'b', 'o', 'o', 't',
210 static void check_mounted(const char *, mode_t
);
211 static void getstdfmt(const char *, struct bpb
*);
212 static void getdiskinfo(int, const char *, const char *, int,
214 static void print_bpb(struct bpb
*);
215 static u_int
ckgeom(const char *, u_int
, const char *);
216 static u_int
argtou(const char *, u_int
, u_int
, const char *);
217 static int oklabel(const char *);
218 static void mklabel(u_int8_t
*, const char *);
219 static void setstr(u_int8_t
*, const char *, size_t);
220 static void usage(void);
223 * Construct a FAT12, FAT16, or FAT32 file system.
226 main(int argc
, char *argv
[])
228 static char opts
[] = "NB:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
229 static const char *opt_B
, *opt_L
, *opt_O
, *opt_f
;
230 static u_int opt_F
, opt_I
, opt_S
, opt_a
, opt_b
, opt_c
, opt_e
;
231 static u_int opt_h
, opt_i
, opt_k
, opt_m
, opt_n
, opt_o
, opt_r
;
232 static u_int opt_s
, opt_u
;
234 static int Iflag
, mflag
, oflag
;
235 char buf
[MAXPATHLEN
];
242 struct bsxbpb
*bsxbpb
;
246 const char *fname
, *dtype
, *bname
;
249 u_int fat
, bss
, rds
, cls
, dir
, lsn
, x
, x1
, x2
;
252 while ((ch
= getopt(argc
, argv
, opts
)) != -1)
261 if (strcmp(optarg
, "12") &&
262 strcmp(optarg
, "16") &&
263 strcmp(optarg
, "32"))
264 errx(1, "%s: bad FAT type", optarg
);
265 opt_F
= atoi(optarg
);
268 opt_I
= argto4(optarg
, 0, "volume ID");
272 if (!oklabel(optarg
))
273 errx(1, "%s: bad volume label", optarg
);
277 if (strlen(optarg
) > 8)
278 errx(1, "%s: bad OEM string", optarg
);
282 opt_S
= argto2(optarg
, 1, "bytes/sector");
285 opt_a
= argto4(optarg
, 1, "sectors/FAT");
288 opt_b
= argtox(optarg
, 1, "block size");
292 opt_c
= argto1(optarg
, 1, "sectors/cluster");
296 opt_e
= argto2(optarg
, 1, "directory entries");
302 opt_h
= argto2(optarg
, 1, "drive heads");
305 opt_i
= argto2(optarg
, 1, "info sector");
308 opt_k
= argto2(optarg
, 1, "backup sector");
311 opt_m
= argto1(optarg
, 0, "media descriptor");
315 opt_n
= argto1(optarg
, 1, "number of FATs");
318 opt_o
= argto4(optarg
, 0, "hidden sectors");
322 opt_r
= argto2(optarg
, 1, "reserved sectors");
325 opt_s
= argto4(optarg
, 1, "file system size");
328 opt_u
= argto2(optarg
, 1, "sectors/track");
335 if (argc
< 1 || argc
> 2)
338 if (!strchr(fname
, '/')) {
339 snprintf(buf
, sizeof(buf
), "%s%s", _PATH_DEV
, fname
);
340 if (!(fname
= strdup(buf
)))
344 if ((fd
= open(fname
, opt_N
? O_RDONLY
: O_RDWR
)) == -1 ||
348 check_mounted(fname
, sb
.st_mode
);
349 if (!S_ISCHR(sb
.st_mode
))
350 errx(1, "%s is not a character device", fname
);
351 memset(&bpb
, 0, sizeof(bpb
));
353 getstdfmt(opt_f
, &bpb
);
369 if (!(opt_f
|| (opt_h
&& opt_u
&& opt_S
&& opt_s
&& oflag
)))
370 getdiskinfo(fd
, fname
, dtype
, oflag
, &bpb
);
371 if (!powerof2(bpb
.bps
))
372 errx(1, "bytes/sector (%u) is not a power of 2", bpb
.bps
);
373 if (bpb
.bps
< MINBPS
)
374 errx(1, "bytes/sector (%u) is too small; minimum is %u",
376 if (!(fat
= opt_F
)) {
379 else if (!opt_e
&& (opt_i
|| opt_k
))
382 if ((fat
== 32 && opt_e
) || (fat
!= 32 && (opt_i
|| opt_k
)))
383 errx(1, "-%c is not a legal FAT%s option",
384 fat
== 32 ? 'e' : opt_i
? 'i' : 'k',
385 fat
== 32 ? "32" : "12/16");
386 if (opt_f
&& fat
== 32)
389 if (!powerof2(opt_b
))
390 errx(1, "block size (%u) is not a power of 2", opt_b
);
392 errx(1, "block size (%u) is too small; minimum is %u",
394 if (opt_b
> bpb
.bps
* MAXSPC
)
395 errx(1, "block size (%u) is too large; maximum is %u",
396 opt_b
, bpb
.bps
* MAXSPC
);
397 bpb
.spc
= opt_b
/ bpb
.bps
;
400 if (!powerof2(opt_c
))
401 errx(1, "sectors/cluster (%u) is not a power of 2", opt_c
);
408 errx(1, "number of FATs (%u) is too large; maximum is %u",
416 errx(1, "illegal media descriptor (%#x)", opt_m
);
430 if (!strchr(bname
, '/')) {
431 snprintf(buf
, sizeof(buf
), "/boot/%s", bname
);
432 if (!(bname
= strdup(buf
)))
435 if ((fd1
= open(bname
, O_RDONLY
)) == -1 || fstat(fd1
, &sb
))
437 if (!S_ISREG(sb
.st_mode
) || sb
.st_size
% bpb
.bps
||
438 sb
.st_size
< bpb
.bps
|| sb
.st_size
> bpb
.bps
* MAXU16
)
439 errx(1, "%s: inappropriate file type or format", bname
);
440 bss
= sb
.st_size
/ bpb
.bps
;
445 if (bpb
.bsec
< (bpb
.res
? bpb
.res
: bss
) +
446 howmany((RESFTE
+ (bpb
.spc
? MINCLS16
: MAXCLS12
+ 1)) *
447 ((bpb
.spc
? 16 : 12) / BPN
), bpb
.bps
* NPB
) *
449 howmany(bpb
.rde
? bpb
.rde
: DEFRDE
,
450 bpb
.bps
/ sizeof(struct de
)) +
451 (bpb
.spc
? MINCLS16
: MAXCLS12
+ 1) *
452 (bpb
.spc
? bpb
.spc
: howmany(DEFBLK
, bpb
.bps
)))
454 else if (bpb
.rde
|| bpb
.bsec
<
455 (bpb
.res
? bpb
.res
: bss
) +
456 howmany((RESFTE
+ MAXCLS16
) * 2, bpb
.bps
) * bpb
.nft
+
457 howmany(DEFRDE
, bpb
.bps
/ sizeof(struct de
)) +
459 (bpb
.spc
? bpb
.spc
: howmany(8192, bpb
.bps
)))
467 if (x
== MAXU16
|| x
== bpb
.bkbs
)
468 errx(1, "no room for info sector");
471 if (bpb
.infs
!= MAXU16
&& x
<= bpb
.infs
)
475 errx(1, "no room for backup sector");
477 } else if (bpb
.bkbs
!= MAXU16
&& bpb
.bkbs
== bpb
.infs
)
478 errx(1, "backup sector would overwrite info sector");
479 if (bpb
.bkbs
!= MAXU16
&& x
<= bpb
.bkbs
)
483 bpb
.res
= fat
== 32 ? MAX(x
, MAX(16384 / bpb
.bps
, 4)) : x
;
484 else if (bpb
.res
< x
)
485 errx(1, "too few reserved sectors");
486 if (fat
!= 32 && !bpb
.rde
)
488 rds
= howmany(bpb
.rde
, bpb
.bps
/ sizeof(struct de
));
490 for (bpb
.spc
= howmany(fat
== 16 ? DEFBLK16
: DEFBLK
, bpb
.bps
);
493 howmany((RESFTE
+ maxcls(fat
)) * (fat
/ BPN
),
494 bpb
.bps
* NPB
) * bpb
.nft
+
496 (u_int64_t
)(maxcls(fat
) + 1) * bpb
.spc
<= bpb
.bsec
;
498 if (fat
!= 32 && bpb
.bspf
> MAXU16
)
499 errx(1, "too many sectors/FAT for FAT12/16");
501 x
= bpb
.bspf
? bpb
.bspf
: 1;
502 if (x1
+ (u_int64_t
)x
* bpb
.nft
> bpb
.bsec
)
503 errx(1, "meta data exceeds file system size");
505 x
= (u_int64_t
)(bpb
.bsec
- x1
) * bpb
.bps
* NPB
/
506 (bpb
.spc
* bpb
.bps
* NPB
+ fat
/ BPN
* bpb
.nft
);
507 x2
= howmany((RESFTE
+ MIN(x
, maxcls(fat
))) * (fat
/ BPN
),
511 x1
+= (bpb
.bspf
- 1) * bpb
.nft
;
513 cls
= (bpb
.bsec
- x1
) / bpb
.spc
;
514 x
= (u_int64_t
)bpb
.bspf
* bpb
.bps
* NPB
/ (fat
/ BPN
) - RESFTE
;
518 warnx("warning: sectors/FAT limits file system to %u clusters",
520 if (cls
< mincls(fat
))
521 errx(1, "%u clusters too few clusters for FAT%u, need %u", cls
, fat
,
523 if (cls
> maxcls(fat
)) {
525 bpb
.bsec
= x1
+ (cls
+ 1) * bpb
.spc
- 1;
526 warnx("warning: FAT type limits file system to %u sectors",
529 printf("%s: %u sector%s in %u FAT%u cluster%s "
530 "(%u bytes/cluster)\n", fname
, cls
* bpb
.spc
,
531 cls
* bpb
.spc
== 1 ? "" : "s", cls
, fat
,
532 cls
== 1 ? "" : "s", bpb
.bps
* bpb
.spc
);
534 bpb
.mid
= !bpb
.hid
? 0xf0 : 0xf8;
537 if (bpb
.hid
+ bpb
.bsec
<= MAXU16
) {
547 gettimeofday(&tv
, NULL
);
549 tm
= localtime(&now
);
550 if (!(img
= malloc(bpb
.bps
)))
552 dir
= bpb
.res
+ (bpb
.spf
? bpb
.spf
: bpb
.bspf
) * bpb
.nft
;
553 for (lsn
= 0; lsn
< dir
+ (fat
== 32 ? bpb
.spc
: rds
); lsn
++) {
556 fat
== 32 && bpb
.bkbs
!= MAXU16
&&
557 bss
<= bpb
.bkbs
&& x
>= bpb
.bkbs
) {
559 if (!x
&& lseek(fd1
, 0, SEEK_SET
))
562 if (opt_B
&& x
< bss
) {
563 if ((n
= read(fd1
, img
, bpb
.bps
)) == -1)
566 errx(1, "%s: can't read sector %u", bname
, x
);
568 memset(img
, 0, bpb
.bps
);
570 (fat
== 32 && bpb
.bkbs
!= MAXU16
&& lsn
== bpb
.bkbs
)) {
571 x1
= sizeof(struct bs
);
572 bsbpb
= (struct bsbpb
*)(img
+ x1
);
573 mk2(bsbpb
->bps
, bpb
.bps
);
574 mk1(bsbpb
->spc
, bpb
.spc
);
575 mk2(bsbpb
->res
, bpb
.res
);
576 mk1(bsbpb
->nft
, bpb
.nft
);
577 mk2(bsbpb
->rde
, bpb
.rde
);
578 mk2(bsbpb
->sec
, bpb
.sec
);
579 mk1(bsbpb
->mid
, bpb
.mid
);
580 mk2(bsbpb
->spf
, bpb
.spf
);
581 mk2(bsbpb
->spt
, bpb
.spt
);
582 mk2(bsbpb
->hds
, bpb
.hds
);
583 mk4(bsbpb
->hid
, bpb
.hid
);
584 mk4(bsbpb
->bsec
, bpb
.bsec
);
585 x1
+= sizeof(struct bsbpb
);
587 bsxbpb
= (struct bsxbpb
*)(img
+ x1
);
588 mk4(bsxbpb
->bspf
, bpb
.bspf
);
589 mk2(bsxbpb
->xflg
, 0);
590 mk2(bsxbpb
->vers
, 0);
591 mk4(bsxbpb
->rdcl
, bpb
.rdcl
);
592 mk2(bsxbpb
->infs
, bpb
.infs
);
593 mk2(bsxbpb
->bkbs
, bpb
.bkbs
);
594 x1
+= sizeof(struct bsxbpb
);
596 bsx
= (struct bsx
*)(img
+ x1
);
601 x
= (((u_int
)(1 + tm
->tm_mon
) << 8 |
602 (u_int
)tm
->tm_mday
) +
603 ((u_int
)tm
->tm_sec
<< 8 |
604 (u_int
)(tv
.tv_usec
/ 10))) << 16 |
605 ((u_int
)(1900 + tm
->tm_year
) +
606 ((u_int
)tm
->tm_hour
<< 8 |
609 mklabel(bsx
->label
, opt_L
? opt_L
: "NO NAME");
610 sprintf(buf
, "FAT%u", fat
);
611 setstr(bsx
->type
, buf
, sizeof(bsx
->type
));
613 x1
+= sizeof(struct bsx
);
614 bs
= (struct bs
*)img
;
615 mk1(bs
->jmp
[0], 0xeb);
616 mk1(bs
->jmp
[1], x1
- 2);
617 mk1(bs
->jmp
[2], 0x90);
618 setstr(bs
->oem
, opt_O
? opt_O
: "BSD 4.4",
620 memcpy(img
+ x1
, bootcode
, sizeof(bootcode
));
621 mk2(img
+ bpb
.bps
- 2, DOSMAGIC
);
623 } else if (fat
== 32 && bpb
.infs
!= MAXU16
&&
625 (bpb
.bkbs
!= MAXU16
&&
626 lsn
== bpb
.bkbs
+ bpb
.infs
))) {
627 mk4(img
, 0x41615252);
628 mk4(img
+ bpb
.bps
- 28, 0x61417272);
629 mk4(img
+ bpb
.bps
- 24, 0xffffffff);
630 mk4(img
+ bpb
.bps
- 20, bpb
.rdcl
);
631 mk2(img
+ bpb
.bps
- 2, DOSMAGIC
);
632 } else if (lsn
>= bpb
.res
&& lsn
< dir
&&
634 (bpb
.spf
? bpb
.spf
: bpb
.bspf
))) {
635 mk1(img
[0], bpb
.mid
);
636 for (x
= 1; x
< fat
* (fat
== 32 ? 3 : 2) / 8; x
++)
637 mk1(img
[x
], fat
== 32 && x
% 4 == 3 ? 0x0f : 0xff);
638 } else if (lsn
== dir
&& opt_L
) {
639 de
= (struct de
*)img
;
640 mklabel(de
->namext
, opt_L
);
642 x
= (u_int
)tm
->tm_hour
<< 11 |
643 (u_int
)tm
->tm_min
<< 5 |
644 (u_int
)tm
->tm_sec
>> 1;
646 x
= (u_int
)(tm
->tm_year
- 80) << 9 |
647 (u_int
)(tm
->tm_mon
+ 1) << 5 |
651 if ((n
= write(fd
, img
, bpb
.bps
)) == -1)
654 errx(1, "%s: can't write sector %u", fname
, lsn
);
661 * Exit with error if file system is mounted.
664 check_mounted(const char *fname
, mode_t mode
)
671 if (!(n
= getmntinfo(&mp
, MNT_NOWAIT
)))
672 err(1, "getmntinfo");
673 len
= strlen(_PATH_DEV
);
675 if (!strncmp(s1
, _PATH_DEV
, len
))
677 r
= S_ISCHR(mode
) && s1
!= fname
&& *s1
== 'r';
679 s2
= mp
->f_mntfromname
;
680 if (!strncmp(s2
, _PATH_DEV
, len
))
682 if ((r
&& s2
!= mp
->f_mntfromname
&& !strcmp(s1
+ 1, s2
)) ||
684 errx(1, "%s is mounted on %s", fname
, mp
->f_mntonname
);
689 * Get a standard format.
692 getstdfmt(const char *fmt
, struct bpb
*bpb
)
696 x
= sizeof(stdfmt
) / sizeof(stdfmt
[0]);
697 for (i
= 0; i
< x
&& strcmp(fmt
, stdfmt
[i
].name
); i
++);
699 errx(1, "%s: unknown standard format", fmt
);
700 *bpb
= stdfmt
[i
].bpb
;
704 * Get disk slice, partition, and geometry information.
707 getdiskinfo(int fd
, const char *fname
, const char *dtype
, int oflag
,
710 struct diskslices ds
;
717 if ((s2
= strrchr(s1
, '/')))
719 for (s2
= s1
; *s2
&& !isdigit(*s2
); s2
++);
720 if (!*s2
|| s2
== s1
)
723 while (isdigit(*++s2
));
725 if (s2
&& *s2
== 's') {
726 slice
= strtol(s2
+ 1, &s
, 10);
727 if (slice
< 0 || slice
> MAX_SLICES
- BASE_SLICE
)
731 slice
= BASE_SLICE
+ slice
- 1;
735 if (s2
&& *s2
>= 'a' && *s2
<= 'a' + MAXPARTITIONS
- 1) {
737 slice
= COMPATIBILITY_SLICE
;
740 if (!s2
|| (*s2
&& *s2
!= '.'))
741 errx(1, "%s: can't figure out partition info", fname
);
742 if (slice
!= -1 && (!oflag
|| (!bpb
->bsec
&& part
== -1))) {
743 if (ioctl(fd
, DIOCGSLICEINFO
, &ds
) == -1) {
744 warn("ioctl (GSLICEINFO)");
745 errx(1, "%s: can't get slice info", fname
);
747 if (slice
>= ds
.dss_nslices
|| !ds
.dss_slices
[slice
].ds_size
)
748 errx(1, "%s: slice is unavailable", fname
);
750 bpb
->hid
= ds
.dss_slices
[slice
].ds_offset
;
751 if (!bpb
->bsec
&& part
== -1)
752 bpb
->bsec
= ds
.dss_slices
[slice
].ds_size
;
754 if (((slice
== -1 || part
!= -1) &&
755 ((!oflag
&& part
!= -1) || !bpb
->bsec
)) ||
756 !bpb
->bps
|| !bpb
->spt
|| !bpb
->hds
758 struct partinfo pinfo
;
759 struct disktab
*dtab
= NULL
;
761 i
= ioctl(fd
, DIOCGPART
, &pinfo
);
764 warn("ioctl (GPART)");
765 errx(1, "%s: can't read disk label; "
766 "disk type must be specified", fname
);
767 } else if (!(dtab
= getdisktabbyname(dtype
))) {
768 errx(1, "%s: unknown disk type", dtype
);
770 pinfo
.media_blocks
= dtab
->d_media_blocks
;
771 pinfo
.media_blksize
= dtab
->d_media_blksize
;
772 pinfo
.d_nheads
= dtab
->d_nheads
;
773 pinfo
.d_secpertrack
= dtab
->d_secpertrack
;
775 if (!oflag
&& part
!= -1)
776 bpb
->hid
+= pinfo
.media_offset
;
778 bpb
->bsec
= pinfo
.media_blocks
;
781 bpb
->bps
= ckgeom(fname
, pinfo
.media_blksize
, "bytes/sector");
783 bpb
->spt
= ckgeom(fname
, pinfo
.d_secpertrack
, "sectors/track");
785 bpb
->hds
= ckgeom(fname
, pinfo
.d_nheads
, "drive heads");
790 * Print out BPB values.
793 print_bpb(struct bpb
*bpb
)
795 printf("bps=%u spc=%u res=%u nft=%u", bpb
->bps
, bpb
->spc
, bpb
->res
,
798 printf(" rde=%u", bpb
->rde
);
800 printf(" sec=%u", bpb
->sec
);
801 printf(" mid=%#x", bpb
->mid
);
803 printf(" spf=%u", bpb
->spf
);
804 printf(" spt=%u hds=%u hid=%u", bpb
->spt
, bpb
->hds
, bpb
->hid
);
806 printf(" bsec=%u", bpb
->bsec
);
808 printf(" bspf=%u rdcl=%u", bpb
->bspf
, bpb
->rdcl
);
810 printf(bpb
->infs
== MAXU16
? "%#x" : "%u", bpb
->infs
);
812 printf(bpb
->bkbs
== MAXU16
? "%#x" : "%u", bpb
->bkbs
);
818 * Check a disk geometry value.
821 ckgeom(const char *fname
, u_int val
, const char *msg
)
824 errx(1, "%s: no default %s", fname
, msg
);
826 errx(1, "%s: illegal %s", fname
, msg
);
831 * Convert and check a numeric option argument.
834 argtou(const char *arg
, u_int lo
, u_int hi
, const char *msg
)
840 x
= strtoul(arg
, &s
, 0);
841 if (errno
|| !*arg
|| *s
|| x
< lo
|| x
> hi
)
842 errx(1, "%s: bad %s", arg
, msg
);
847 * Check a volume label.
850 oklabel(const char *src
)
854 for (i
= 0; i
<= 11; i
++) {
856 if (c
< ' ' + !i
|| strchr("\"*+,./:;<=>?[\\]|", c
))
863 * Make a volume label.
866 mklabel(u_int8_t
*dest
, const char *src
)
870 for (i
= 0; i
< 11; i
++) {
871 c
= *src
? toupper(*src
++) : ' ';
872 *dest
++ = !i
&& c
== '\xe5' ? 5 : c
;
877 * Copy string, padding with spaces.
880 setstr(u_int8_t
*dest
, const char *src
, size_t len
)
883 *dest
++ = *src
? *src
++ : ' ';
887 * Print usage message.
893 "usage: newfs_msdos [ -options ] special [disktype]\n");
894 fprintf(stderr
, "where the options are:\n");
895 fprintf(stderr
, "\t-N don't create file system: "
896 "just print out parameters\n");
897 fprintf(stderr
, "\t-B get bootstrap from file\n");
898 fprintf(stderr
, "\t-F FAT type (12, 16, or 32)\n");
899 fprintf(stderr
, "\t-I volume ID\n");
900 fprintf(stderr
, "\t-L volume label\n");
901 fprintf(stderr
, "\t-O OEM string\n");
902 fprintf(stderr
, "\t-S bytes/sector\n");
903 fprintf(stderr
, "\t-a sectors/FAT\n");
904 fprintf(stderr
, "\t-b block size\n");
905 fprintf(stderr
, "\t-c sectors/cluster\n");
906 fprintf(stderr
, "\t-e root directory entries\n");
907 fprintf(stderr
, "\t-f standard format\n");
908 fprintf(stderr
, "\t-h drive heads\n");
909 fprintf(stderr
, "\t-i file system info sector\n");
910 fprintf(stderr
, "\t-k backup boot sector\n");
911 fprintf(stderr
, "\t-m media descriptor\n");
912 fprintf(stderr
, "\t-n number of FATs\n");
913 fprintf(stderr
, "\t-o hidden sectors\n");
914 fprintf(stderr
, "\t-r reserved sectors\n");
915 fprintf(stderr
, "\t-s file system size (sectors)\n");
916 fprintf(stderr
, "\t-u sectors/track\n");