1 /* $NetBSD: mkfs_msdos.c,v 1.13 2017/04/14 15:39:29 christos 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 #if HAVE_NBTOOL_CONFIG_H
31 #include "nbtool_config.h"
34 #include <sys/cdefs.h>
37 static const char rcsid
[] =
38 "$FreeBSD: src/sbin/newfs_msdos/newfs_msdos.c,v 1.15 2000/10/10 01:49:37 wollman Exp $";
40 __RCSID("$NetBSD: mkfs_msdos.c,v 1.13 2017/04/14 15:39:29 christos Exp $");
44 #include <sys/types.h>
45 #include <sys/param.h>
49 #include <sys/mount.h>
71 #include "mkfs_msdos.h"
73 #define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
74 #define BPN 4 /* bits per nibble */
75 #define NPB 2 /* nibbles per byte */
77 #define DOSMAGIC 0xaa55 /* DOS magic number */
78 #define MINBPS 512 /* minimum bytes per sector */
79 #define MAXSPC 128 /* maximum sectors per cluster */
80 #define MAXNFT 16 /* maximum number of FATs */
81 #define DEFBLK 4096 /* default block size */
82 #define DEFBLK16 2048 /* default block size FAT16 */
83 #define DEFRDE 512 /* default root directory entries */
84 #define RESFTE 2 /* reserved FAT entries */
85 #define MINCLS12 1 /* minimum FAT12 clusters */
86 #define MINCLS16 0xff5 /* minimum FAT16 clusters */
87 #define MINCLS32 0xfff5 /* minimum FAT32 clusters */
88 #define MAXCLS12 0xff4 /* maximum FAT12 clusters */
89 #define MAXCLS16 0xfff4 /* maximum FAT16 clusters */
90 #define MAXCLS32 0xffffff4 /* maximum FAT32 clusters */
92 #define mincls(fat_type) ((fat_type) == 12 ? MINCLS12 : \
93 (fat_type) == 16 ? MINCLS16 : \
96 #define maxcls(fat_type) ((fat_type) == 12 ? MAXCLS12 : \
97 (fat_type) == 16 ? MAXCLS16 : \
104 (p)[0] = (u_int8_t)(x), \
105 (p)[1] = (u_int8_t)((x) >> 010)
108 (p)[0] = (u_int8_t)(x), \
109 (p)[1] = (u_int8_t)((x) >> 010), \
110 (p)[2] = (u_int8_t)((x) >> 020), \
111 (p)[3] = (u_int8_t)((x) >> 030)
114 u_int8_t jmp
[3]; /* bootstrap entry point */
115 u_int8_t oem
[8]; /* OEM name and version */
119 u_int8_t bps
[2]; /* bytes per sector */
120 u_int8_t spc
; /* sectors per cluster */
121 u_int8_t res
[2]; /* reserved sectors */
122 u_int8_t nft
; /* number of FATs */
123 u_int8_t rde
[2]; /* root directory entries */
124 u_int8_t sec
[2]; /* total sectors */
125 u_int8_t mid
; /* media descriptor */
126 u_int8_t spf
[2]; /* sectors per FAT */
127 u_int8_t spt
[2]; /* sectors per track */
128 u_int8_t hds
[2]; /* drive heads */
129 u_int8_t hid
[4]; /* hidden sectors */
130 u_int8_t bsec
[4]; /* big total sectors */
134 u_int8_t bspf
[4]; /* big sectors per FAT */
135 u_int8_t xflg
[2]; /* FAT control flags */
136 u_int8_t vers
[2]; /* file system version */
137 u_int8_t rdcl
[4]; /* root directory start cluster */
138 u_int8_t infs
[2]; /* file system info sector */
139 u_int8_t bkbs
[2]; /* backup boot sector */
140 u_int8_t rsvd
[12]; /* reserved */
144 u_int8_t drv
; /* drive number */
145 u_int8_t rsvd
; /* reserved */
146 u_int8_t sig
; /* extended boot signature */
147 u_int8_t volid
[4]; /* volume ID number */
148 u_int8_t label
[11]; /* volume label */
149 u_int8_t type
[8]; /* file system type */
153 u_int8_t namext
[11]; /* name and extension */
154 u_int8_t attr
; /* attributes */
155 u_int8_t rsvd
[10]; /* reserved */
156 u_int8_t time
[2]; /* creation time */
157 u_int8_t date
[2]; /* creation date */
158 u_int8_t clus
[2]; /* starting cluster */
159 u_int8_t size
[4]; /* size */
163 u_int bps
; /* bytes per sector */
164 u_int spc
; /* sectors per cluster */
165 u_int res
; /* reserved sectors */
166 u_int nft
; /* number of FATs */
167 u_int rde
; /* root directory entries */
168 u_int sec
; /* total sectors */
169 u_int mid
; /* media descriptor */
170 u_int spf
; /* sectors per FAT */
171 u_int spt
; /* sectors per track */
172 u_int hds
; /* drive heads */
173 u_int hid
; /* hidden sectors */
174 u_int bsec
; /* big total sectors */
175 u_int bspf
; /* big sectors per FAT */
176 u_int rdcl
; /* root directory start cluster */
177 u_int infs
; /* file system info sector */
178 u_int bkbs
; /* backup boot sector */
181 #define INIT(a, b, c, d, e, f, g, h, i, j) \
182 { .bps = a, .spc = b, .res = c, .nft = d, .rde = e, \
183 .sec = f, .mid = g, .spf = h, .spt = i, .hds = j, }
188 {"160", INIT(512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1)},
189 {"180", INIT(512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1)},
190 {"320", INIT(512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2)},
191 {"360", INIT(512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2)},
192 {"640", INIT(512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2)},
193 {"720", INIT(512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2)},
194 {"1200", INIT(512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2)},
195 {"1232", INIT(1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2)},
196 {"1440", INIT(512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2)},
197 {"2880", INIT(512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2)}
200 static u_int8_t bootcode
[] = {
202 0x31, 0xc0, /* xor ax,ax */
203 0x8e, 0xd0, /* mov ss,ax */
204 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
206 0x8e, 0xd8, /* mov ds,ax */
207 0xe8, 0x00, 0x00, /* call $ + 3 */
209 0x83, 0xc6, 0x19, /* add si,+19h */
210 0xbb, 0x07, 0x00, /* mov bx,0007h */
213 0x84, 0xc0, /* test al,al */
214 0x74, 0x06, /* jz $ + 8 */
215 0xb4, 0x0e, /* mov ah,0eh */
216 0xcd, 0x10, /* int 10h */
217 0xeb, 0xf5, /* jmp $ - 9 */
218 0x30, 0xe4, /* xor ah,ah */
219 0xcd, 0x16, /* int 16h */
220 0xcd, 0x19, /* int 19h */
222 'N', 'o', 'n', '-', 's', 'y', 's', 't',
223 'e', 'm', ' ', 'd', 'i', 's', 'k',
225 'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
226 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
227 ' ', 'r', 'e', 'b', 'o', 'o', 't',
232 static int got_siginfo
= 0; /* received a SIGINFO */
235 static int check_mounted(const char *, mode_t
);
237 static int getstdfmt(const char *, struct bpb
*);
238 static int getbpbinfo(int, const char *, const char *, int, struct bpb
*, off_t
);
239 static void print_bpb(struct bpb
*);
240 static int ckgeom(const char *, u_int
, const char *);
241 static int oklabel(const char *);
242 static void mklabel(u_int8_t
*, const char *);
243 static void setstr(u_int8_t
*, const char *, size_t);
244 static void infohandler(int sig
);
247 mkfs_msdos(const char *fname
, const char *dtype
, const struct msdos_options
*op
)
249 char buf
[MAXPATHLEN
];
256 struct bsxbpb
*bsxbpb
;
263 u_int bss
, rds
, cls
, dir
, lsn
, x
, x1
, x2
;
265 struct msdos_options o
= *op
;
266 int oflags
= O_RDWR
| O_CREAT
;
268 if (o
.block_size
&& o
.sectors_per_cluster
) {
269 warnx("Cannot specify both block size and sectors per cluster");
272 if (o
.OEM_string
&& strlen(o
.OEM_string
) > 8) {
273 warnx("%s: bad OEM string", o
.OEM_string
);
278 warnx("create (-C) is incompatible with -N");
283 fd
= open(fname
, oflags
, 0644);
285 warnx("failed to create %s", fname
);
288 (void)lseek(fd
, o
.create_size
- 1, SEEK_SET
);
289 if (write(fd
, "\0", 1) != 1) {
290 warn("failed to set file size");
293 (void)lseek(fd
, 0, SEEK_SET
);
294 } else if ((fd
= open(fname
, o
.no_create
? O_RDONLY
: O_RDWR
)) == -1 ||
301 if (check_mounted(fname
, sb
.st_mode
) == -1)
304 if (!S_ISCHR(sb
.st_mode
) && !o
.create_size
) {
305 warnx("warning, %s is not a character device", fname
);
308 if (o
.offset
&& o
.offset
!= lseek(fd
, o
.offset
, SEEK_SET
)) {
309 warnx("cannot seek to %jd", (intmax_t)o
.offset
);
312 memset(&bpb
, 0, sizeof(bpb
));
314 if (getstdfmt(o
.floppy
, &bpb
) == -1)
322 bpb
.hds
= o
.drive_heads
;
323 if (o
.sectors_per_track
)
324 bpb
.spt
= o
.sectors_per_track
;
325 if (o
.bytes_per_sector
)
326 bpb
.bps
= o
.bytes_per_sector
;
329 if (o
.hidden_sectors_set
)
330 bpb
.hid
= o
.hidden_sectors
;
331 if (!(o
.floppy
|| (o
.drive_heads
&& o
.sectors_per_track
&&
332 o
.bytes_per_sector
&& o
.size
&& o
.hidden_sectors_set
))) {
333 if (getbpbinfo(fd
, fname
, dtype
, o
.hidden_sectors_set
, &bpb
,
334 o
.create_size
) == -1)
336 bpb
.bsec
-= (o
.offset
/ bpb
.bps
);
337 if (bpb
.spc
== 0) { /* set defaults */
338 /* minimum cluster size */
339 switch (o
.fat_type
) {
341 bpb
.spc
= 1; /* use 512 bytes */
342 x
= 2; /* up to 2MB */
345 bpb
.spc
= 1; /* use 512 bytes */
346 x
= 32; /* up to 32MB */
349 bpb
.spc
= 8; /* use 4k */
350 x
= 8192; /* up to 8GB */
353 x1
= howmany(bpb
.bsec
, (1048576 / 512)); /* -> MB */
354 while (bpb
.spc
< 128 && x
< x1
) {
361 if (o
.volume_label
&& !oklabel(o
.volume_label
)) {
362 warnx("%s: bad volume label", o
.volume_label
);
366 switch (o
.fat_type
) {
370 else if (!o
.directory_entries
&& (o
.info_sector
|| o
.backup_sector
))
376 warnx("Cannot specify info sector with FAT%u", o
.fat_type
);
379 if (o
.backup_sector
) {
380 warnx("Cannot specify backup sector with FAT%u", o
.fat_type
);
385 if (o
.directory_entries
) {
386 warnx("Cannot specify directory entries with FAT32");
391 warnx("%d: bad FAT type", o
.fat_type
);
394 if (!powerof2(bpb
.bps
)) {
395 warnx("bytes/sector (%u) is not a power of 2", bpb
.bps
);
398 if (bpb
.bps
< MINBPS
) {
399 warnx("bytes/sector (%u) is too small; minimum is %u",
404 if (o
.floppy
&& o
.fat_type
== 32)
407 if (!powerof2(o
.block_size
)) {
408 warnx("block size (%u) is not a power of 2", o
.block_size
);
411 if (o
.block_size
< bpb
.bps
) {
412 warnx("block size (%u) is too small; minimum is %u",
413 o
.block_size
, bpb
.bps
);
416 if (o
.block_size
> bpb
.bps
* MAXSPC
) {
417 warnx("block size (%u) is too large; maximum is %u",
418 o
.block_size
, bpb
.bps
* MAXSPC
);
421 bpb
.spc
= o
.block_size
/ bpb
.bps
;
423 if (o
.sectors_per_cluster
) {
424 if (!powerof2(o
.sectors_per_cluster
)) {
425 warnx("sectors/cluster (%u) is not a power of 2",
426 o
.sectors_per_cluster
);
429 bpb
.spc
= o
.sectors_per_cluster
;
431 if (o
.reserved_sectors
)
432 bpb
.res
= o
.reserved_sectors
;
434 if (o
.num_FAT
> MAXNFT
) {
435 warnx("number of FATs (%u) is too large; maximum is %u",
441 if (o
.directory_entries
)
442 bpb
.rde
= o
.directory_entries
;
443 if (o
.media_descriptor_set
) {
444 if (o
.media_descriptor
< 0xf0) {
445 warnx("illegal media descriptor (%#x)", o
.media_descriptor
);
448 bpb
.mid
= o
.media_descriptor
;
450 if (o
.sectors_per_fat
)
451 bpb
.bspf
= o
.sectors_per_fat
;
453 bpb
.infs
= o
.info_sector
;
455 bpb
.bkbs
= o
.backup_sector
;
461 if (!strchr(bname
, '/')) {
462 snprintf(buf
, sizeof(buf
), "/boot/%s", bname
);
463 if (!(bname
= strdup(buf
))) {
468 if ((fd1
= open(bname
, O_RDONLY
)) == -1 || fstat(fd1
, &sb
)) {
472 if (!S_ISREG(sb
.st_mode
) || sb
.st_size
% bpb
.bps
||
473 sb
.st_size
< bpb
.bps
|| sb
.st_size
> bpb
.bps
* MAXU16
) {
474 warnx("%s: inappropriate file type or format", bname
);
477 bss
= sb
.st_size
/ bpb
.bps
;
482 if (bpb
.bsec
< (bpb
.res
? bpb
.res
: bss
) +
483 howmany((RESFTE
+ (bpb
.spc
? MINCLS16
: MAXCLS12
+ 1)) *
484 ((bpb
.spc
? 16 : 12) / BPN
), bpb
.bps
* NPB
) *
486 howmany(bpb
.rde
? bpb
.rde
: DEFRDE
,
487 bpb
.bps
/ sizeof(struct de
)) +
488 (bpb
.spc
? MINCLS16
: MAXCLS12
+ 1) *
489 (bpb
.spc
? bpb
.spc
: howmany(DEFBLK
, bpb
.bps
)))
491 else if (bpb
.rde
|| bpb
.bsec
<
492 (bpb
.res
? bpb
.res
: bss
) +
493 howmany((RESFTE
+ MAXCLS16
) * 2, bpb
.bps
) * bpb
.nft
+
494 howmany(DEFRDE
, bpb
.bps
/ sizeof(struct de
)) +
496 (bpb
.spc
? bpb
.spc
: howmany(8192, bpb
.bps
)))
502 if (o
.fat_type
== 32) {
504 if (x
== MAXU16
|| x
== bpb
.bkbs
) {
505 warnx("no room for info sector");
510 if (bpb
.infs
!= MAXU16
&& x
<= bpb
.infs
)
514 warnx("no room for backup sector");
518 } else if (bpb
.bkbs
!= MAXU16
&& bpb
.bkbs
== bpb
.infs
) {
519 warnx("backup sector would overwrite info sector");
522 if (bpb
.bkbs
!= MAXU16
&& x
<= bpb
.bkbs
)
526 bpb
.res
= o
.fat_type
== 32 ? MAX(x
, MAX(16384 / bpb
.bps
, 4)) : x
;
527 else if (bpb
.res
< x
) {
528 warnx("too few reserved sectors (need %d have %d)", x
, bpb
.res
);
531 if (o
.fat_type
!= 32 && !bpb
.rde
)
533 rds
= howmany(bpb
.rde
, bpb
.bps
/ sizeof(struct de
));
535 for (bpb
.spc
= howmany(o
.fat_type
== 16 ? DEFBLK16
: DEFBLK
, bpb
.bps
);
538 howmany((RESFTE
+ maxcls(o
.fat_type
)) * (o
.fat_type
/ BPN
),
539 bpb
.bps
* NPB
) * bpb
.nft
+
541 (u_int64_t
)(maxcls(o
.fat_type
) + 1) * bpb
.spc
<= bpb
.bsec
;
543 if (o
.fat_type
!= 32 && bpb
.bspf
> MAXU16
) {
544 warnx("too many sectors/FAT for FAT12/16");
548 x
= bpb
.bspf
? bpb
.bspf
: 1;
549 if (x1
+ (u_int64_t
)x
* bpb
.nft
> bpb
.bsec
) {
550 warnx("meta data exceeds file system size");
554 x
= (u_int64_t
)(bpb
.bsec
- x1
) * bpb
.bps
* NPB
/
555 (bpb
.spc
* bpb
.bps
* NPB
+ o
.fat_type
/ BPN
* bpb
.nft
);
556 x2
= howmany((RESFTE
+ MIN(x
, maxcls(o
.fat_type
))) * (o
.fat_type
/ BPN
),
560 x1
+= (bpb
.bspf
- 1) * bpb
.nft
;
562 cls
= (bpb
.bsec
- x1
) / bpb
.spc
;
563 x
= (u_int64_t
)bpb
.bspf
* bpb
.bps
* NPB
/ (o
.fat_type
/ BPN
) - RESFTE
;
567 warnx("warning: sectors/FAT limits file system to %u clusters",
571 if (cls
< mincls(o
.fat_type
)) {
572 warnx("%u clusters too few clusters for FAT%u, need %u", cls
,
573 o
.fat_type
, mincls(o
.fat_type
));
576 if (cls
> maxcls(o
.fat_type
)) {
577 cls
= maxcls(o
.fat_type
);
578 bpb
.bsec
= x1
+ (cls
+ 1) * bpb
.spc
- 1;
579 warnx("warning: FAT type limits file system to %u sectors",
583 printf("%s: %u sector%s in %u FAT%u cluster%s "
584 "(%u bytes/cluster)\n", fname
, cls
* bpb
.spc
,
585 cls
* bpb
.spc
== 1 ? "" : "s", cls
, o
.fat_type
,
586 cls
== 1 ? "" : "s", bpb
.bps
* bpb
.spc
);
588 bpb
.mid
= !bpb
.hid
? 0xf0 : 0xf8;
589 if (o
.fat_type
== 32)
591 if (bpb
.hid
+ bpb
.bsec
<= MAXU16
) {
595 if (o
.fat_type
!= 32) {
600 if (o
.fat_type
== 12)
601 ch
= 1; /* 001 Primary DOS with 12 bit FAT */
602 else if (o
.fat_type
== 16) {
604 ch
= 4; /* 004 Primary DOS with 16 bit FAT <32M */
606 ch
= 6; /* 006 Primary 'big' DOS, 16-bit FAT (> 32MB) */
609 * 014 DOS (16-bit FAT) - LBA
612 } else if (o
.fat_type
== 32) {
613 ch
= 11; /* 011 Primary DOS with 32 bit FAT */
616 * 012 Primary DOS with 32 bit FAT - LBA
621 printf("MBR type: %d\n", ch
);
624 if (o
.timestamp_set
) {
625 tv
.tv_sec
= now
= o
.timestamp
;
629 gettimeofday(&tv
, NULL
);
631 tm
= localtime(&now
);
633 if (!(img
= malloc(bpb
.bps
)))
635 dir
= bpb
.res
+ (bpb
.spf
? bpb
.spf
: bpb
.bspf
) * bpb
.nft
;
637 signal(SIGINFO
, infohandler
);
639 for (lsn
= 0; lsn
< dir
+ (o
.fat_type
== 32 ? bpb
.spc
: rds
); lsn
++) {
641 fprintf(stderr
,"%s: writing sector %u of %u (%u%%)\n",
642 fname
,lsn
,(dir
+ (o
.fat_type
== 32 ? bpb
.spc
: rds
)),
643 (lsn
*100)/(dir
+ (o
.fat_type
== 32 ? bpb
.spc
: rds
)));
648 o
.fat_type
== 32 && bpb
.bkbs
!= MAXU16
&&
649 bss
<= bpb
.bkbs
&& x
>= bpb
.bkbs
) {
651 if (!x
&& lseek(fd1
, o
.offset
, SEEK_SET
)) {
656 if (o
.bootstrap
&& x
< bss
) {
657 if ((n
= read(fd1
, img
, bpb
.bps
)) == -1) {
661 if ((size_t)n
!= bpb
.bps
) {
662 warnx("%s: can't read sector %u", bname
, x
);
666 memset(img
, 0, bpb
.bps
);
668 (o
.fat_type
== 32 && bpb
.bkbs
!= MAXU16
&& lsn
== bpb
.bkbs
)) {
669 x1
= sizeof(struct bs
);
670 bsbpb
= (struct bsbpb
*)(img
+ x1
);
671 mk2(bsbpb
->bps
, bpb
.bps
);
672 mk1(bsbpb
->spc
, bpb
.spc
);
673 mk2(bsbpb
->res
, bpb
.res
);
674 mk1(bsbpb
->nft
, bpb
.nft
);
675 mk2(bsbpb
->rde
, bpb
.rde
);
676 mk2(bsbpb
->sec
, bpb
.sec
);
677 mk1(bsbpb
->mid
, bpb
.mid
);
678 mk2(bsbpb
->spf
, bpb
.spf
);
679 mk2(bsbpb
->spt
, bpb
.spt
);
680 mk2(bsbpb
->hds
, bpb
.hds
);
681 mk4(bsbpb
->hid
, bpb
.hid
);
682 mk4(bsbpb
->bsec
, bpb
.bsec
);
683 x1
+= sizeof(struct bsbpb
);
684 if (o
.fat_type
== 32) {
685 bsxbpb
= (struct bsxbpb
*)(img
+ x1
);
686 mk4(bsxbpb
->bspf
, bpb
.bspf
);
687 mk2(bsxbpb
->xflg
, 0);
688 mk2(bsxbpb
->vers
, 0);
689 mk4(bsxbpb
->rdcl
, bpb
.rdcl
);
690 mk2(bsxbpb
->infs
, bpb
.infs
);
691 mk2(bsxbpb
->bkbs
, bpb
.bkbs
);
692 x1
+= sizeof(struct bsxbpb
);
694 bsx
= (struct bsx
*)(img
+ x1
);
699 x
= (((u_int
)(1 + tm
->tm_mon
) << 8 |
700 (u_int
)tm
->tm_mday
) +
701 ((u_int
)tm
->tm_sec
<< 8 |
702 (u_int
)(tv
.tv_usec
/ 10))) << 16 |
703 ((u_int
)(1900 + tm
->tm_year
) +
704 ((u_int
)tm
->tm_hour
<< 8 |
707 mklabel(bsx
->label
, o
.volume_label
? o
.volume_label
: "NO NAME");
708 snprintf(buf
, sizeof(buf
), "FAT%u", o
.fat_type
);
709 setstr(bsx
->type
, buf
, sizeof(bsx
->type
));
711 x1
+= sizeof(struct bsx
);
712 bs
= (struct bs
*)img
;
713 mk1(bs
->jmp
[0], 0xeb);
714 mk1(bs
->jmp
[1], x1
- 2);
715 mk1(bs
->jmp
[2], 0x90);
716 setstr(bs
->oem
, o
.OEM_string
? o
.OEM_string
: "NetBSD",
718 memcpy(img
+ x1
, bootcode
, sizeof(bootcode
));
719 mk2(img
+ MINBPS
- 2, DOSMAGIC
);
721 } else if (o
.fat_type
== 32 && bpb
.infs
!= MAXU16
&&
723 (bpb
.bkbs
!= MAXU16
&&
724 lsn
== bpb
.bkbs
+ bpb
.infs
))) {
725 mk4(img
, 0x41615252);
726 mk4(img
+ MINBPS
- 28, 0x61417272);
727 mk4(img
+ MINBPS
- 24, 0xffffffff);
728 mk4(img
+ MINBPS
- 20, 0xffffffff);
729 mk2(img
+ MINBPS
- 2, DOSMAGIC
);
730 } else if (lsn
>= bpb
.res
&& lsn
< dir
&&
732 (bpb
.spf
? bpb
.spf
: bpb
.bspf
))) {
733 mk1(img
[0], bpb
.mid
);
734 for (x
= 1; x
< o
.fat_type
* (o
.fat_type
== 32 ? 3U : 2U) / 8U; x
++)
735 mk1(img
[x
], o
.fat_type
== 32 && x
% 4 == 3 ? 0x0f : 0xff);
736 } else if (lsn
== dir
&& o
.volume_label
) {
737 de
= (struct de
*)img
;
738 mklabel(de
->namext
, o
.volume_label
);
740 x
= (u_int
)tm
->tm_hour
<< 11 |
741 (u_int
)tm
->tm_min
<< 5 |
742 (u_int
)tm
->tm_sec
>> 1;
744 x
= (u_int
)(tm
->tm_year
- 80) << 9 |
745 (u_int
)(tm
->tm_mon
+ 1) << 5 |
749 if ((n
= write(fd
, img
, bpb
.bps
)) == -1) {
753 if ((size_t)n
!= bpb
.bps
) {
754 warnx("%s: can't write sector %u", fname
, lsn
);
764 * return -1 with error if file system is mounted.
767 check_mounted(const char *fname
, mode_t mode
)
774 if (!(n
= getmntinfo(&mp
, MNT_NOWAIT
))) {
778 len
= strlen(_PATH_DEV
);
780 if (!strncmp(s1
, _PATH_DEV
, len
))
782 r
= S_ISCHR(mode
) && s1
!= fname
&& *s1
== 'r';
784 s2
= mp
->f_mntfromname
;
785 if (!strncmp(s2
, _PATH_DEV
, len
))
787 if ((r
&& s2
!= mp
->f_mntfromname
&& !strcmp(s1
+ 1, s2
)) ||
789 warnx("%s is mounted on %s", fname
, mp
->f_mntonname
);
798 * Get a standard format.
801 getstdfmt(const char *fmt
, struct bpb
*bpb
)
805 x
= sizeof(stdfmt
) / sizeof(stdfmt
[0]);
806 for (i
= 0; i
< x
&& strcmp(fmt
, stdfmt
[i
].name
); i
++);
808 warnx("%s: unknown standard format", fmt
);
811 *bpb
= stdfmt
[i
].bpb
;
816 * Get disk slice, partition, and geometry information.
819 getbpbinfo(int fd
, const char *fname
, const char *dtype
, int iflag
,
820 struct bpb
*bpb
, off_t create_size
)
827 if ((s2
= strrchr(s1
, '/')))
829 for (s2
= s1
; *s2
&& !isdigit((unsigned char)*s2
); s2
++);
830 if (!*s2
|| s2
== s1
)
833 while (isdigit((unsigned char)*++s2
));
837 int maxpartitions
= getmaxpartitions();
838 struct disk_geom geo
;
839 struct dkwedge_info dkw
;
841 // XXX: Does not work with wedges
842 if (s2
&& *s2
>= 'a' && *s2
<= 'a' + maxpartitions
- 1) {
847 if (!(((part
!= -1) && ((!iflag
&& part
!= -1) || !bpb
->bsec
)) ||
848 !bpb
->bps
|| !bpb
->spt
|| !bpb
->hds
)) {
852 u_int sector_size
= 512;
857 if (create_size
== 0) {
859 if (getdiskinfo(fname
, fd
, NULL
, &geo
, &dkw
) != -1) {
860 sector_size
= geo
.dg_secsize
;
861 nsectors
= geo
.dg_nsectors
;
862 ntracks
= geo
.dg_ntracks
;
869 if (fstat(fd
, &st
) == -1) {
870 warnx("Can't get disk size for `%s'", fname
);
873 size
= st
.st_size
/ sector_size
;
876 size
= create_size
/ sector_size
;
880 if (ckgeom(fname
, sector_size
, "bytes/sector") == -1)
882 bpb
->bps
= sector_size
;
887 * The kernel doesn't accept BPB with spt > 63.
888 * (see sys/fs/msdosfs/msdosfs_vfsops.c:msdosfs_mountfs())
889 * If values taken from disklabel don't match these
890 * restrictions, use popular BIOS default values instead.
895 if (ckgeom(fname
, nsectors
, "sectors/track") == -1)
900 if (ckgeom(fname
, ntracks
, "drive heads") == -1)
910 * Print out BPB values.
913 print_bpb(struct bpb
*bpb
)
915 printf("bps=%u spc=%u res=%u nft=%u", bpb
->bps
, bpb
->spc
, bpb
->res
,
918 printf(" rde=%u", bpb
->rde
);
920 printf(" sec=%u", bpb
->sec
);
921 printf(" mid=%#x", bpb
->mid
);
923 printf(" spf=%u", bpb
->spf
);
924 printf(" spt=%u hds=%u hid=%u", bpb
->spt
, bpb
->hds
, bpb
->hid
);
926 printf(" bsec=%u", bpb
->bsec
);
928 printf(" bspf=%u rdcl=%u", bpb
->bspf
, bpb
->rdcl
);
930 printf(bpb
->infs
== MAXU16
? "%#x" : "%u", bpb
->infs
);
932 printf(bpb
->bkbs
== MAXU16
? "%#x" : "%u", bpb
->bkbs
);
938 * Check a disk geometry value.
941 ckgeom(const char *fname
, u_int val
, const char *msg
)
944 warnx("%s: no default %s", fname
, msg
);
948 warnx("%s: illegal %s", fname
, msg
);
954 * Check a volume label.
957 oklabel(const char *src
)
961 for (i
= 0; i
<= 11; i
++) {
963 if (c
< ' ' + !i
|| strchr("\"*+,./:;<=>?[\\]|", c
))
970 * Make a volume label.
973 mklabel(u_int8_t
*dest
, const char *src
)
977 for (i
= 0; i
< 11; i
++) {
978 c
= *src
? toupper((unsigned char)*src
++) : ' ';
979 *dest
++ = !i
&& c
== '\xe5' ? 5 : c
;
984 * Copy string, padding with spaces.
987 setstr(u_int8_t
*dest
, const char *src
, size_t len
)
990 *dest
++ = *src
? *src
++ : ' ';