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.
29 static const char rcsid[] =
33 #include <sys/param.h>
35 /* In the makefs case we only want struct disklabel */
36 #include <sys/disk/bsd.h>
38 #include <sys/fdcio.h>
40 #include <sys/disklabel.h>
41 #include <sys/mount.h>
44 #include <sys/sysctl.h>
61 #include "mkfs_msdos.h"
63 #define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
64 #define BPN 4 /* bits per nibble */
65 #define NPB 2 /* nibbles per byte */
67 #define DOSMAGIC 0xaa55 /* DOS magic number */
68 #define MINBPS 512 /* minimum bytes per sector */
69 #define MAXBPS 4096 /* maximum 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 1U /* minimum FAT12 clusters */
77 #define MINCLS16 0xff5U /* minimum FAT16 clusters */
78 #define MINCLS32 0xfff5U /* minimum FAT32 clusters */
79 #define MAXCLS12 0xff4U /* maximum FAT12 clusters */
80 #define MAXCLS16 0xfff4U /* maximum FAT16 clusters */
81 #define MAXCLS32 0xffffff4U /* 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)
105 u_int8_t bsJump[3]; /* bootstrap entry point */
106 u_int8_t bsOemName[8]; /* OEM name and version */
110 u_int8_t bpbBytesPerSec[2]; /* bytes per sector */
111 u_int8_t bpbSecPerClust; /* sectors per cluster */
112 u_int8_t bpbResSectors[2]; /* reserved sectors */
113 u_int8_t bpbFATs; /* number of FATs */
114 u_int8_t bpbRootDirEnts[2]; /* root directory entries */
115 u_int8_t bpbSectors[2]; /* total sectors */
116 u_int8_t bpbMedia; /* media descriptor */
117 u_int8_t bpbFATsecs[2]; /* sectors per FAT */
118 u_int8_t bpbSecPerTrack[2]; /* sectors per track */
119 u_int8_t bpbHeads[2]; /* drive heads */
120 u_int8_t bpbHiddenSecs[4]; /* hidden sectors */
121 u_int8_t bpbHugeSectors[4]; /* big total sectors */
125 u_int8_t bpbBigFATsecs[4]; /* big sectors per FAT */
126 u_int8_t bpbExtFlags[2]; /* FAT control flags */
127 u_int8_t bpbFSVers[2]; /* file system version */
128 u_int8_t bpbRootClust[4]; /* root directory start cluster */
129 u_int8_t bpbFSInfo[2]; /* file system info sector */
130 u_int8_t bpbBackup[2]; /* backup boot sector */
131 u_int8_t bpbReserved[12]; /* reserved */
135 u_int8_t exDriveNumber; /* drive number */
136 u_int8_t exReserved1; /* reserved */
137 u_int8_t exBootSignature; /* extended boot signature */
138 u_int8_t exVolumeID[4]; /* volume ID number */
139 u_int8_t exVolumeLabel[11]; /* volume label */
140 u_int8_t exFileSysType[8]; /* file system type */
144 u_int8_t deName[11]; /* name and extension */
145 u_int8_t deAttributes; /* attributes */
146 u_int8_t rsvd[10]; /* reserved */
147 u_int8_t deMTime[2]; /* last-modified time */
148 u_int8_t deMDate[2]; /* last-modified date */
149 u_int8_t deStartCluster[2]; /* starting cluster */
150 u_int8_t deFileSize[4]; /* size */
154 u_int bpbBytesPerSec; /* bytes per sector */
155 u_int bpbSecPerClust; /* sectors per cluster */
156 u_int bpbResSectors; /* reserved sectors */
157 u_int bpbFATs; /* number of FATs */
158 u_int bpbRootDirEnts; /* root directory entries */
159 u_int bpbSectors; /* total sectors */
160 u_int bpbMedia; /* media descriptor */
161 u_int bpbFATsecs; /* sectors per FAT */
162 u_int bpbSecPerTrack; /* sectors per track */
163 u_int bpbHeads; /* drive heads */
164 u_int bpbHiddenSecs; /* hidden sectors */
165 u_int bpbHugeSectors; /* big total sectors */
166 u_int bpbBigFATsecs; /* big sectors per FAT */
167 u_int bpbRootClust; /* root directory start cluster */
168 u_int bpbFSInfo; /* file system info sector */
169 u_int bpbBackup; /* backup boot sector */
172 #define BPBGAP 0, 0, 0, 0, 0, 0
178 {"160", {512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1, BPBGAP}},
179 {"180", {512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1, BPBGAP}},
180 {"320", {512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2, BPBGAP}},
181 {"360", {512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2, BPBGAP}},
182 {"640", {512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2, BPBGAP}},
183 {"720", {512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2, BPBGAP}},
184 {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2, BPBGAP}},
185 {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2, BPBGAP}},
186 {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2, BPBGAP}},
187 {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2, BPBGAP}}
190 static const u_int8_t bootcode[] = {
192 0x31, 0xc0, /* xor ax,ax */
193 0x8e, 0xd0, /* mov ss,ax */
194 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
196 0x8e, 0xd8, /* mov ds,ax */
197 0xe8, 0x00, 0x00, /* call $ + 3 */
199 0x83, 0xc6, 0x19, /* add si,+19h */
200 0xbb, 0x07, 0x00, /* mov bx,0007h */
203 0x84, 0xc0, /* test al,al */
204 0x74, 0x06, /* jz $ + 8 */
205 0xb4, 0x0e, /* mov ah,0eh */
206 0xcd, 0x10, /* int 10h */
207 0xeb, 0xf5, /* jmp $ - 9 */
208 0x30, 0xe4, /* xor ah,ah */
209 0xcd, 0x16, /* int 16h */
210 0xcd, 0x19, /* int 19h */
212 'N', 'o', 'n', '-', 's', 'y', 's', 't',
213 'e', 'm', ' ', 'd', 'i', 's', 'k',
215 'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
216 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
217 ' ', 'r', 'e', 'b', 'o', 'o', 't',
222 static volatile sig_atomic_t got_siginfo;
223 static void infohandler(int);
226 static int check_mounted(const char *, mode_t);
228 static ssize_t getchunksize(void);
229 static int getstdfmt(const char *, struct bpb *);
230 static int getdiskinfo(int, const char *, const char *, int, struct bpb *);
231 static void print_bpb(struct bpb *);
232 static int ckgeom(const char *, u_int, const char *);
233 static void mklabel(u_int8_t *, const char *);
234 static int oklabel(const char *);
235 static void setstr(u_int8_t *, const char *, size_t);
238 mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op)
240 char buf[MAXPATHLEN];
241 struct sigaction si_sa;
248 struct bsxbpb *bsxbpb;
252 u_int8_t *physbuf, *physbuf_end;
256 u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
257 u_int extra_res, alignment, saved_x, attempts=0;
258 bool set_res, set_spf, set_spc;
260 struct msdos_options o = *op;
267 if (o.block_size && o.sectors_per_cluster) {
268 warnx("Cannot specify both block size and sectors per cluster");
271 if (o.OEM_string && strlen(o.OEM_string) > 8) {
272 warnx("%s: bad OEM string", o.OEM_string);
277 warnx("create (-C) is incompatible with -N");
280 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644);
282 warnx("failed to create %s", fname);
285 if (ftruncate(fd, o.create_size)) {
286 warnx("failed to initialize %jd bytes", (intmax_t)o.create_size);
289 } else if ((fd = open(fname, o.no_create ? O_RDONLY : O_RDWR)) == -1) {
293 if (fstat(fd, &sb)) {
298 if (!S_ISREG(sb.st_mode))
299 warnx("warning, %s is not a regular file", fname);
302 errx(1, "o.create_size must be set!");
304 if (!S_ISCHR(sb.st_mode))
305 warnx("warning, %s is not a character device", fname);
310 if (check_mounted(fname, sb.st_mode) == -1)
313 if (o.offset && o.offset != lseek(fd, o.offset, SEEK_SET)) {
314 warnx("cannot seek to %jd", (intmax_t)o.offset);
317 memset(&bpb, 0, sizeof(bpb));
319 if (getstdfmt(o.floppy, &bpb) == -1)
321 bpb.bpbHugeSectors = bpb.bpbSectors;
323 bpb.bpbBigFATsecs = bpb.bpbFATsecs;
327 bpb.bpbHeads = o.drive_heads;
328 if (o.sectors_per_track)
329 bpb.bpbSecPerTrack = o.sectors_per_track;
330 if (o.bytes_per_sector)
331 bpb.bpbBytesPerSec = o.bytes_per_sector;
333 bpb.bpbHugeSectors = o.size;
334 if (o.hidden_sectors_set)
335 bpb.bpbHiddenSecs = o.hidden_sectors;
336 if (!(o.floppy || (o.drive_heads && o.sectors_per_track &&
337 o.bytes_per_sector && o.size && o.hidden_sectors_set))) {
338 if (getdiskinfo(fd, fname, dtype, o.hidden_sectors_set, &bpb) == -1)
340 bpb.bpbHugeSectors -= (o.offset / bpb.bpbBytesPerSec);
341 if (bpb.bpbSecPerClust == 0) { /* set defaults */
342 if (bpb.bpbHugeSectors <= 6000) /* about 3MB -> 512 bytes */
343 bpb.bpbSecPerClust = 1;
344 else if (bpb.bpbHugeSectors <= (1<<17)) /* 64M -> 4k */
345 bpb.bpbSecPerClust = 8;
346 else if (bpb.bpbHugeSectors <= (1<<19)) /* 256M -> 8k */
347 bpb.bpbSecPerClust = 16;
348 else if (bpb.bpbHugeSectors <= (1<<21)) /* 1G -> 16k */
349 bpb.bpbSecPerClust = 32;
351 bpb.bpbSecPerClust = 64; /* otherwise 32k */
354 if (bpb.bpbBytesPerSec < MINBPS ||
355 bpb.bpbBytesPerSec > MAXBPS ||
356 !powerof2(bpb.bpbBytesPerSec)) {
357 warnx("Invalid bytes/sector (%u): must be 512, 1024, 2048 or 4096",
362 if (o.volume_label && !oklabel(o.volume_label)) {
363 warnx("%s: bad volume label", o.volume_label);
366 if (!(fat = o.fat_type)) {
369 else if (!o.directory_entries && (o.info_sector || o.backup_sector))
372 if ((fat == 32 && o.directory_entries) || (fat != 32 && (o.info_sector || o.backup_sector))) {
373 warnx("-%c is not a legal FAT%s option",
374 fat == 32 ? 'e' : o.info_sector ? 'i' : 'k',
375 fat == 32 ? "32" : "12/16");
378 if (o.floppy && fat == 32)
379 bpb.bpbRootDirEnts = 0;
380 if (fat != 0 && fat != 12 && fat != 16 && fat != 32) {
381 warnx("%d: bad FAT type", fat);
386 if (!powerof2(o.block_size)) {
387 warnx("block size (%u) is not a power of 2", o.block_size);
390 if (o.block_size < bpb.bpbBytesPerSec) {
391 warnx("block size (%u) is too small; minimum is %u",
392 o.block_size, bpb.bpbBytesPerSec);
395 if (o.block_size > bpb.bpbBytesPerSec * MAXSPC) {
396 warnx("block size (%u) is too large; maximum is %u",
397 o.block_size, bpb.bpbBytesPerSec * MAXSPC);
400 bpb.bpbSecPerClust = o.block_size / bpb.bpbBytesPerSec;
402 if (o.sectors_per_cluster) {
403 if (!powerof2(o.sectors_per_cluster)) {
404 warnx("sectors/cluster (%u) is not a power of 2",
405 o.sectors_per_cluster);
408 bpb.bpbSecPerClust = o.sectors_per_cluster;
410 if (o.reserved_sectors)
411 bpb.bpbResSectors = o.reserved_sectors;
413 if (o.num_FAT > MAXNFT) {
414 warnx("number of FATs (%u) is too large; maximum is %u",
418 bpb.bpbFATs = o.num_FAT;
420 if (o.directory_entries)
421 bpb.bpbRootDirEnts = o.directory_entries;
422 if (o.media_descriptor_set) {
423 if (o.media_descriptor < 0xf0) {
424 warnx("illegal media descriptor (%#x)", o.media_descriptor);
427 bpb.bpbMedia = o.media_descriptor;
429 if (o.sectors_per_fat)
430 bpb.bpbBigFATsecs = o.sectors_per_fat;
432 bpb.bpbFSInfo = o.info_sector;
434 bpb.bpbBackup = o.backup_sector;
440 if (!strchr(bname, '/')) {
441 snprintf(buf, sizeof(buf), "/boot/%s", bname);
444 if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb)) {
448 if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bpbBytesPerSec ||
449 sb.st_size < bpb.bpbBytesPerSec ||
450 sb.st_size > bpb.bpbBytesPerSec * MAXU16) {
451 warnx("%s: inappropriate file type or format", bname);
454 bss = sb.st_size / bpb.bpbBytesPerSec;
459 if (bpb.bpbHugeSectors < (bpb.bpbResSectors ? bpb.bpbResSectors : bss) +
460 howmany((RESFTE + (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1)) *
461 (bpb.bpbSecPerClust ? 16 : 12) / BPN,
462 bpb.bpbBytesPerSec * NPB) *
464 howmany(bpb.bpbRootDirEnts ? bpb.bpbRootDirEnts : DEFRDE,
465 bpb.bpbBytesPerSec / sizeof(struct de)) +
466 (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1) *
467 (bpb.bpbSecPerClust ? bpb.bpbSecPerClust :
468 howmany(DEFBLK, bpb.bpbBytesPerSec)))
470 else if (bpb.bpbRootDirEnts || bpb.bpbHugeSectors <
471 (bpb.bpbResSectors ? bpb.bpbResSectors : bss) +
472 howmany((RESFTE + MAXCLS16) * 2, bpb.bpbBytesPerSec) *
474 howmany(DEFRDE, bpb.bpbBytesPerSec / sizeof(struct de)) +
476 (bpb.bpbSecPerClust ? bpb.bpbSecPerClust :
477 howmany(8192, bpb.bpbBytesPerSec)))
484 if (!bpb.bpbFSInfo) {
485 if (x == MAXU16 || x == bpb.bpbBackup) {
486 warnx("no room for info sector");
491 if (bpb.bpbFSInfo != MAXU16 && x <= bpb.bpbFSInfo)
492 x = bpb.bpbFSInfo + 1;
493 if (!bpb.bpbBackup) {
495 warnx("no room for backup sector");
499 } else if (bpb.bpbBackup != MAXU16 && bpb.bpbBackup == bpb.bpbFSInfo) {
500 warnx("backup sector would overwrite info sector");
503 if (bpb.bpbBackup != MAXU16 && x <= bpb.bpbBackup)
504 x = bpb.bpbBackup + 1;
509 set_res = (bpb.bpbResSectors == 0);
510 set_spf = (bpb.bpbBigFATsecs == 0);
511 set_spc = (bpb.bpbSecPerClust == 0);
515 * Attempt to align the root directory to cluster if o.align is set.
516 * This is done by padding with reserved blocks. Note that this can
517 * cause other factors to change, which can in turn change the alignment.
518 * This should take at most 2 iterations, as increasing the reserved
519 * amount may cause the FAT size to decrease by 1, requiring another
520 * bpbFATs reserved blocks. If bpbSecPerClust changes, it will
521 * be half of its previous size, and thus will not throw off alignment.
526 bpb.bpbResSectors = ((fat == 32) ?
527 MAX(x, MAX(16384 / bpb.bpbBytesPerSec, 4)) : x) + extra_res;
528 else if (bpb.bpbResSectors < x) {
529 warnx("too few reserved sectors (need %d have %d)", x,
533 if (fat != 32 && !bpb.bpbRootDirEnts)
534 bpb.bpbRootDirEnts = DEFRDE;
535 rds = howmany(bpb.bpbRootDirEnts,
536 bpb.bpbBytesPerSec / sizeof(struct de));
538 for (bpb.bpbSecPerClust = howmany(fat == 16 ? DEFBLK16 :
539 DEFBLK, bpb.bpbBytesPerSec);
540 bpb.bpbSecPerClust < MAXSPC && (bpb.bpbResSectors +
541 howmany((RESFTE + maxcls(fat)) * (fat / BPN),
542 bpb.bpbBytesPerSec * NPB) * bpb.bpbFATs +
544 (u_int64_t) (maxcls(fat) + 1) * bpb.bpbSecPerClust) <=
546 bpb.bpbSecPerClust <<= 1)
550 if (fat != 32 && bpb.bpbBigFATsecs > MAXU16) {
551 warnx("too many sectors/FAT for FAT12/16");
554 x1 = bpb.bpbResSectors + rds;
555 x = bpb.bpbBigFATsecs ? bpb.bpbBigFATsecs : 1;
556 if (x1 + (u_int64_t)x * bpb.bpbFATs > bpb.bpbHugeSectors) {
557 warnx("meta data exceeds file system size");
560 x1 += x * bpb.bpbFATs;
561 x = (u_int64_t)(bpb.bpbHugeSectors - x1) * bpb.bpbBytesPerSec * NPB /
562 (bpb.bpbSecPerClust * bpb.bpbBytesPerSec * NPB +
563 fat / BPN * bpb.bpbFATs);
564 x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN),
565 bpb.bpbBytesPerSec * NPB);
567 if (bpb.bpbBigFATsecs == 0)
568 bpb.bpbBigFATsecs = x2;
569 x1 += (bpb.bpbBigFATsecs - 1) * bpb.bpbFATs;
572 /* attempt to align root directory */
573 alignment = (bpb.bpbResSectors + bpb.bpbBigFATsecs * bpb.bpbFATs) %
576 extra_res += bpb.bpbSecPerClust - alignment;
579 } while (o.align && alignment != 0 && attempts < 2);
580 if (o.align && alignment != 0)
581 warnx("warning: Alignment failed.");
583 cls = (bpb.bpbHugeSectors - x1) / bpb.bpbSecPerClust;
584 x = (u_int64_t)bpb.bpbBigFATsecs * bpb.bpbBytesPerSec * NPB / (fat / BPN) -
588 if (bpb.bpbBigFATsecs < x2)
589 warnx("warning: sectors/FAT limits file system to %u clusters",
591 if (cls < mincls(fat)) {
592 warnx("%u clusters too few clusters for FAT%u, need %u", cls, fat,
596 if (cls > maxcls(fat)) {
598 bpb.bpbHugeSectors = x1 + (cls + 1) * bpb.bpbSecPerClust - 1;
599 warnx("warning: FAT type limits file system to %u sectors",
602 printf("%s: %u sector%s in %u FAT%u cluster%s "
603 "(%u bytes/cluster)\n", fname, cls * bpb.bpbSecPerClust,
604 cls * bpb.bpbSecPerClust == 1 ? "" : "s", cls, fat,
605 cls == 1 ? "" : "s", bpb.bpbBytesPerSec * bpb.bpbSecPerClust);
607 bpb.bpbMedia = !bpb.bpbHiddenSecs ? 0xf0 : 0xf8;
609 bpb.bpbRootClust = RESFTE;
610 if (bpb.bpbHugeSectors <= MAXU16) {
611 bpb.bpbSectors = bpb.bpbHugeSectors;
612 bpb.bpbHugeSectors = 0;
615 bpb.bpbFATsecs = bpb.bpbBigFATsecs;
616 bpb.bpbBigFATsecs = 0;
620 if (o.timestamp_set) {
621 tv.tv_sec = now = o.timestamp;
625 gettimeofday(&tv, NULL);
627 tm = localtime(&now);
630 chunksize = getchunksize();
631 physbuf = malloc(chunksize);
632 if (physbuf == NULL) {
636 physbuf_end = physbuf + chunksize;
639 dir = bpb.bpbResSectors + (bpb.bpbFATsecs ? bpb.bpbFATsecs :
640 bpb.bpbBigFATsecs) * bpb.bpbFATs;
641 memset(&si_sa, 0, sizeof(si_sa));
642 si_sa.sa_handler = infohandler;
644 if (sigaction(SIGINFO, &si_sa, NULL) == -1) {
645 warn("sigaction SIGINFO");
649 for (lsn = 0; lsn < dir + (fat == 32 ? bpb.bpbSecPerClust : rds); lsn++) {
651 fprintf(stderr,"%s: writing sector %u of %u (%u%%)\n",
653 (dir + (fat == 32 ? bpb.bpbSecPerClust: rds)),
655 (fat == 32 ? bpb.bpbSecPerClust: rds)));
660 fat == 32 && bpb.bpbBackup != MAXU16 &&
661 bss <= bpb.bpbBackup && x >= bpb.bpbBackup) {
663 if (!x && lseek(fd1, o.offset, SEEK_SET)) {
668 if (o.bootstrap && x < bss) {
669 if ((n = read(fd1, img, bpb.bpbBytesPerSec)) == -1) {
673 if ((unsigned)n != bpb.bpbBytesPerSec) {
674 warnx("%s: can't read sector %u", bname, x);
678 memset(img, 0, bpb.bpbBytesPerSec);
680 (fat == 32 && bpb.bpbBackup != MAXU16 &&
681 lsn == bpb.bpbBackup)) {
682 x1 = sizeof(struct bs);
683 bsbpb = (struct bsbpb *)(img + x1);
684 mk2(bsbpb->bpbBytesPerSec, bpb.bpbBytesPerSec);
685 mk1(bsbpb->bpbSecPerClust, bpb.bpbSecPerClust);
686 mk2(bsbpb->bpbResSectors, bpb.bpbResSectors);
687 mk1(bsbpb->bpbFATs, bpb.bpbFATs);
688 mk2(bsbpb->bpbRootDirEnts, bpb.bpbRootDirEnts);
689 mk2(bsbpb->bpbSectors, bpb.bpbSectors);
690 mk1(bsbpb->bpbMedia, bpb.bpbMedia);
691 mk2(bsbpb->bpbFATsecs, bpb.bpbFATsecs);
692 mk2(bsbpb->bpbSecPerTrack, bpb.bpbSecPerTrack);
693 mk2(bsbpb->bpbHeads, bpb.bpbHeads);
694 mk4(bsbpb->bpbHiddenSecs, bpb.bpbHiddenSecs);
695 mk4(bsbpb->bpbHugeSectors, bpb.bpbHugeSectors);
696 x1 += sizeof(struct bsbpb);
698 bsxbpb = (struct bsxbpb *)(img + x1);
699 mk4(bsxbpb->bpbBigFATsecs, bpb.bpbBigFATsecs);
700 mk2(bsxbpb->bpbExtFlags, 0);
701 mk2(bsxbpb->bpbFSVers, 0);
702 mk4(bsxbpb->bpbRootClust, bpb.bpbRootClust);
703 mk2(bsxbpb->bpbFSInfo, bpb.bpbFSInfo);
704 mk2(bsxbpb->bpbBackup, bpb.bpbBackup);
705 x1 += sizeof(struct bsxbpb);
707 bsx = (struct bsx *)(img + x1);
708 mk1(bsx->exBootSignature, 0x29);
712 x = (((u_int)(1 + tm->tm_mon) << 8 |
713 (u_int)tm->tm_mday) +
714 ((u_int)tm->tm_sec << 8 |
715 (u_int)(tv.tv_usec / 10))) << 16 |
716 ((u_int)(1900 + tm->tm_year) +
717 ((u_int)tm->tm_hour << 8 |
719 mk4(bsx->exVolumeID, x);
720 mklabel(bsx->exVolumeLabel, o.volume_label ? o.volume_label : "NO NAME");
721 snprintf(buf, sizeof(buf), "FAT%u", fat);
722 setstr(bsx->exFileSysType, buf, sizeof(bsx->exFileSysType));
724 x1 += sizeof(struct bsx);
725 bs = (struct bs *)img;
726 mk1(bs->bsJump[0], 0xeb);
727 mk1(bs->bsJump[1], x1 - 2);
728 mk1(bs->bsJump[2], 0x90);
729 setstr(bs->bsOemName, o.OEM_string ? o.OEM_string : "BSD4.4 ",
730 sizeof(bs->bsOemName));
731 memcpy(img + x1, bootcode, sizeof(bootcode));
732 mk2(img + MINBPS - 2, DOSMAGIC);
734 } else if (fat == 32 && bpb.bpbFSInfo != MAXU16 &&
735 (lsn == bpb.bpbFSInfo ||
736 (bpb.bpbBackup != MAXU16 &&
737 lsn == bpb.bpbBackup + bpb.bpbFSInfo))) {
738 mk4(img, 0x41615252);
739 mk4(img + MINBPS - 28, 0x61417272);
740 mk4(img + MINBPS - 24, 0xffffffff);
741 mk4(img + MINBPS - 20, 0xffffffff);
742 mk2(img + MINBPS - 2, DOSMAGIC);
743 } else if (lsn >= bpb.bpbResSectors && lsn < dir &&
744 !((lsn - bpb.bpbResSectors) %
745 (bpb.bpbFATsecs ? bpb.bpbFATsecs :
746 bpb.bpbBigFATsecs))) {
747 mk1(img[0], bpb.bpbMedia);
748 for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
749 mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
750 } else if (lsn == dir && o.volume_label) {
751 de = (struct de *)img;
752 mklabel(de->deName, o.volume_label);
753 mk1(de->deAttributes, 050);
754 x = (u_int)tm->tm_hour << 11 |
755 (u_int)tm->tm_min << 5 |
756 (u_int)tm->tm_sec >> 1;
758 x = (u_int)(tm->tm_year - 80) << 9 |
759 (u_int)(tm->tm_mon + 1) << 5 |
764 * Issue a write of chunksize once we have collected
767 img += bpb.bpbBytesPerSec;
768 if (img >= physbuf_end) {
769 n = write(fd, physbuf, chunksize);
770 if (n != chunksize) {
771 warnx("%s: can't write sector %u", fname, lsn);
778 * Write remaining sectors, if the last write didn't end
779 * up filling a whole chunk.
781 if (img != physbuf) {
782 ssize_t tailsize = img - physbuf;
784 n = write(fd, physbuf, tailsize);
786 warnx("%s: can't write sector %u", fname, lsn);
803 * return -1 with error if file system is mounted.
807 check_mounted(const char *fname, mode_t mode)
810 * If getmntinfo() is not available (e.g. Linux) don't check. This should
811 * not be a problem since we will only be using makefs to create images.
818 if (!(n = getmntinfo(&mp, MNT_NOWAIT))) {
822 len = strlen(_PATH_DEV);
824 if (!strncmp(s1, _PATH_DEV, len))
826 r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
828 s2 = mp->f_mntfromname;
829 if (!strncmp(s2, _PATH_DEV, len))
831 if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
833 warnx("%s is mounted on %s", fname, mp->f_mntonname);
842 * Get optimal I/O size
847 static ssize_t chunksize;
857 mib[1] = KERN_MAXPHYS;
858 len = sizeof(chunksize);
860 if (sysctl(mib, 2, &chunksize, &len, NULL, 0) == -1) {
861 warn("sysctl: KERN_MAXPHYS, using %zu", (size_t)MAXPHYS);
869 * For better performance, we want to write larger chunks instead of
870 * individual sectors (the size can only be 512, 1024, 2048 or 4096
871 * bytes). Assert that chunksize can always hold an integer number of
872 * sectors by asserting that both are power of two numbers and the
873 * chunksize is greater than MAXBPS.
875 static_assert(powerof2(MAXBPS), "MAXBPS is not power of 2");
876 assert(powerof2(chunksize));
877 assert(chunksize > MAXBPS);
883 * Get a standard format.
886 getstdfmt(const char *fmt, struct bpb *bpb)
891 for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
893 warnx("%s: unknown standard format", fmt);
896 *bpb = stdfmt[i].bpb;
901 compute_geometry_from_file(int fd, const char *fname, struct disklabel *lp)
907 err(1, "cannot get disk size");
908 if (!S_ISREG(st.st_mode))
909 errx(1, "%s is not a regular file", fname);
914 lp->d_secperunit = ms / lp->d_secsize;
918 * Get disk slice, partition, and geometry information.
921 getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
924 struct disklabel *lp, dlp;
932 /* If the user specified a disk type, try to use that */
934 lp = getdiskbyname(dtype);
937 /* Maybe it's a floppy drive */
939 if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1) {
940 /* create a fake geometry for a file image */
941 compute_geometry_from_file(fd, fname, &dlp);
943 } else if (ioctl(fd, FD_GTYPE, &type) != -1) {
944 dlp.d_secsize = 128 << type.secsize;
945 dlp.d_nsectors = type.sectrac;
946 dlp.d_ntracks = type.heads;
947 dlp.d_secperunit = ms / dlp.d_secsize;
952 /* Maybe it's a fixed drive */
954 if (bpb->bpbBytesPerSec)
955 dlp.d_secsize = bpb->bpbBytesPerSec;
956 if (bpb->bpbBytesPerSec == 0 && ioctl(fd, DIOCGSECTORSIZE,
957 &dlp.d_secsize) == -1)
958 err(1, "cannot get sector size");
960 dlp.d_secperunit = ms / dlp.d_secsize;
962 if (bpb->bpbSecPerTrack == 0 && ioctl(fd, DIOCGFWSECTORS,
963 &dlp.d_nsectors) == -1) {
964 warn("cannot get number of sectors per track");
967 if (bpb->bpbHeads == 0 &&
968 ioctl(fd, DIOCGFWHEADS, &dlp.d_ntracks) == -1) {
969 warn("cannot get number of heads");
970 if (dlp.d_secperunit <= 63*1*1024)
972 else if (dlp.d_secperunit <= 63*16*1024)
978 hs = (ms / dlp.d_secsize) - dlp.d_secperunit;
983 /* In the makefs case we only support image files: */
984 compute_geometry_from_file(fd, fname, &dlp);
988 if (bpb->bpbBytesPerSec == 0) {
989 if (ckgeom(fname, lp->d_secsize, "bytes/sector") == -1)
991 bpb->bpbBytesPerSec = lp->d_secsize;
993 if (bpb->bpbSecPerTrack == 0) {
994 if (ckgeom(fname, lp->d_nsectors, "sectors/track") == -1)
996 bpb->bpbSecPerTrack = lp->d_nsectors;
998 if (bpb->bpbHeads == 0) {
999 if (ckgeom(fname, lp->d_ntracks, "drive heads") == -1)
1001 bpb->bpbHeads = lp->d_ntracks;
1003 if (bpb->bpbHugeSectors == 0)
1004 bpb->bpbHugeSectors = lp->d_secperunit;
1005 if (bpb->bpbHiddenSecs == 0)
1006 bpb->bpbHiddenSecs = hs;
1011 * Print out BPB values.
1014 print_bpb(struct bpb *bpb)
1016 printf("BytesPerSec=%u SecPerClust=%u ResSectors=%u FATs=%u",
1017 bpb->bpbBytesPerSec, bpb->bpbSecPerClust, bpb->bpbResSectors,
1019 if (bpb->bpbRootDirEnts)
1020 printf(" RootDirEnts=%u", bpb->bpbRootDirEnts);
1021 if (bpb->bpbSectors)
1022 printf(" Sectors=%u", bpb->bpbSectors);
1023 printf(" Media=%#x", bpb->bpbMedia);
1024 if (bpb->bpbFATsecs)
1025 printf(" FATsecs=%u", bpb->bpbFATsecs);
1026 printf(" SecPerTrack=%u Heads=%u HiddenSecs=%u", bpb->bpbSecPerTrack,
1027 bpb->bpbHeads, bpb->bpbHiddenSecs);
1028 if (bpb->bpbHugeSectors)
1029 printf(" HugeSectors=%u", bpb->bpbHugeSectors);
1030 if (!bpb->bpbFATsecs) {
1031 printf(" FATsecs=%u RootCluster=%u", bpb->bpbBigFATsecs,
1034 printf(bpb->bpbFSInfo == MAXU16 ? "%#x" : "%u", bpb->bpbFSInfo);
1036 printf(bpb->bpbBackup == MAXU16 ? "%#x" : "%u", bpb->bpbBackup);
1042 * Check a disk geometry value.
1045 ckgeom(const char *fname, u_int val, const char *msg)
1048 warnx("%s: no default %s", fname, msg);
1052 warnx("%s: illegal %s %d", fname, msg, val);
1059 * Check a volume label.
1062 oklabel(const char *src)
1066 for (i = 0; i <= 11; i++) {
1068 if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
1075 * Make a volume label.
1078 mklabel(u_int8_t *dest, const char *src)
1082 for (i = 0; i < 11; i++) {
1083 c = *src ? toupper(*src++) : ' ';
1084 *dest++ = !i && c == '\xe5' ? 5 : c;
1089 * Copy string, padding with spaces.
1092 setstr(u_int8_t *dest, const char *src, size_t len)
1095 *dest++ = *src ? *src++ : ' ';
1099 infohandler(int sig __unused)