Deleted Added
full compact
newfs_msdos.c (289369) newfs_msdos.c (289376)
1/*
2 * Copyright (c) 1998 Robert Nordier
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 13 unchanged lines hidden (view full) ---

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.
26 */
27
28#ifndef lint
29static const char rcsid[] =
1/*
2 * Copyright (c) 1998 Robert Nordier
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 13 unchanged lines hidden (view full) ---

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.
26 */
27
28#ifndef lint
29static const char rcsid[] =
30 "$FreeBSD: head/sbin/newfs_msdos/newfs_msdos.c 289369 2015-10-15 16:19:00Z emaste $";
30 "$FreeBSD: head/sbin/newfs_msdos/newfs_msdos.c 289376 2015-10-15 19:00:33Z emaste $";
31#endif /* not lint */
32
33#include <sys/param.h>
31#endif /* not lint */
32
33#include <sys/param.h>
34#include <sys/fdcio.h>
35#include <sys/disk.h>
36#include <sys/disklabel.h>
37#include <sys/mount.h>
38#include <sys/stat.h>
39#include <sys/time.h>
40
34
41#include <ctype.h>
42#include <err.h>
43#include <errno.h>
35#include <err.h>
36#include <errno.h>
44#include <fcntl.h>
45#include <inttypes.h>
46#include <paths.h>
37#include <paths.h>
47#include <signal.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
51#include <time.h>
52#include <unistd.h>
53
41#include <unistd.h>
42
54#define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
55#define BPN 4 /* bits per nibble */
56#define NPB 2 /* nibbles per byte */
43#include "mkfs_msdos.h"
57
44
58#define DOSMAGIC 0xaa55 /* DOS magic number */
59#define MINBPS 512 /* minimum bytes per sector */
60#define MAXSPC 128 /* maximum sectors per cluster */
61#define MAXNFT 16 /* maximum number of FATs */
62#define DEFBLK 4096 /* default block size */
63#define DEFBLK16 2048 /* default block size FAT16 */
64#define DEFRDE 512 /* default root directory entries */
65#define RESFTE 2 /* reserved FAT entries */
66#define MINCLS12 1U /* minimum FAT12 clusters */
67#define MINCLS16 0xff5U /* minimum FAT16 clusters */
68#define MINCLS32 0xfff5U /* minimum FAT32 clusters */
69#define MAXCLS12 0xff4U /* maximum FAT12 clusters */
70#define MAXCLS16 0xfff4U /* maximum FAT16 clusters */
71#define MAXCLS32 0xffffff4U /* maximum FAT32 clusters */
72
73#define mincls(fat) ((fat) == 12 ? MINCLS12 : \
74 (fat) == 16 ? MINCLS16 : \
75 MINCLS32)
76
77#define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \
78 (fat) == 16 ? MAXCLS16 : \
79 MAXCLS32)
80
81#define mk1(p, x) \
82 (p) = (u_int8_t)(x)
83
84#define mk2(p, x) \
85 (p)[0] = (u_int8_t)(x), \
86 (p)[1] = (u_int8_t)((x) >> 010)
87
88#define mk4(p, x) \
89 (p)[0] = (u_int8_t)(x), \
90 (p)[1] = (u_int8_t)((x) >> 010), \
91 (p)[2] = (u_int8_t)((x) >> 020), \
92 (p)[3] = (u_int8_t)((x) >> 030)
93
94#define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
95#define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg)
96#define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg)
97#define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
98
45#define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
46#define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg)
47#define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg)
48#define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
49
99struct bs {
100 u_int8_t bsJump[3]; /* bootstrap entry point */
101 u_int8_t bsOemName[8]; /* OEM name and version */
102} __packed;
103
104struct bsbpb {
105 u_int8_t bpbBytesPerSec[2]; /* bytes per sector */
106 u_int8_t bpbSecPerClust; /* sectors per cluster */
107 u_int8_t bpbResSectors[2]; /* reserved sectors */
108 u_int8_t bpbFATs; /* number of FATs */
109 u_int8_t bpbRootDirEnts[2]; /* root directory entries */
110 u_int8_t bpbSectors[2]; /* total sectors */
111 u_int8_t bpbMedia; /* media descriptor */
112 u_int8_t bpbFATsecs[2]; /* sectors per FAT */
113 u_int8_t bpbSecPerTrack[2]; /* sectors per track */
114 u_int8_t bpbHeads[2]; /* drive heads */
115 u_int8_t bpbHiddenSecs[4]; /* hidden sectors */
116 u_int8_t bpbHugeSectors[4]; /* big total sectors */
117} __packed;
118
119struct bsxbpb {
120 u_int8_t bpbBigFATsecs[4]; /* big sectors per FAT */
121 u_int8_t bpbExtFlags[2]; /* FAT control flags */
122 u_int8_t bpbFSVers[2]; /* file system version */
123 u_int8_t bpbRootClust[4]; /* root directory start cluster */
124 u_int8_t bpbFSInfo[2]; /* file system info sector */
125 u_int8_t bpbBackup[2]; /* backup boot sector */
126 u_int8_t bpbReserved[12]; /* reserved */
127} __packed;
128
129struct bsx {
130 u_int8_t exDriveNumber; /* drive number */
131 u_int8_t exReserved1; /* reserved */
132 u_int8_t exBootSignature; /* extended boot signature */
133 u_int8_t exVolumeID[4]; /* volume ID number */
134 u_int8_t exVolumeLabel[11]; /* volume label */
135 u_int8_t exFileSysType[8]; /* file system type */
136} __packed;
137
138struct de {
139 u_int8_t deName[11]; /* name and extension */
140 u_int8_t deAttributes; /* attributes */
141 u_int8_t rsvd[10]; /* reserved */
142 u_int8_t deMTime[2]; /* last-modified time */
143 u_int8_t deMDate[2]; /* last-modified date */
144 u_int8_t deStartCluster[2]; /* starting cluster */
145 u_int8_t deFileSize[4]; /* size */
146} __packed;
147
148struct bpb {
149 u_int bpbBytesPerSec; /* bytes per sector */
150 u_int bpbSecPerClust; /* sectors per cluster */
151 u_int bpbResSectors; /* reserved sectors */
152 u_int bpbFATs; /* number of FATs */
153 u_int bpbRootDirEnts; /* root directory entries */
154 u_int bpbSectors; /* total sectors */
155 u_int bpbMedia; /* media descriptor */
156 u_int bpbFATsecs; /* sectors per FAT */
157 u_int bpbSecPerTrack; /* sectors per track */
158 u_int bpbHeads; /* drive heads */
159 u_int bpbHiddenSecs; /* hidden sectors */
160 u_int bpbHugeSectors; /* big total sectors */
161 u_int bpbBigFATsecs; /* big sectors per FAT */
162 u_int bpbRootClust; /* root directory start cluster */
163 u_int bpbFSInfo; /* file system info sector */
164 u_int bpbBackup; /* backup boot sector */
165};
166
167#define BPBGAP 0, 0, 0, 0, 0, 0
168
169static struct {
170 const char *name;
171 struct bpb bpb;
172} const stdfmt[] = {
173 {"160", {512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1, BPBGAP}},
174 {"180", {512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1, BPBGAP}},
175 {"320", {512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2, BPBGAP}},
176 {"360", {512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2, BPBGAP}},
177 {"640", {512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2, BPBGAP}},
178 {"720", {512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2, BPBGAP}},
179 {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2, BPBGAP}},
180 {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2, BPBGAP}},
181 {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2, BPBGAP}},
182 {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2, BPBGAP}}
183};
184
185static const u_int8_t bootcode[] = {
186 0xfa, /* cli */
187 0x31, 0xc0, /* xor ax,ax */
188 0x8e, 0xd0, /* mov ss,ax */
189 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
190 0xfb, /* sti */
191 0x8e, 0xd8, /* mov ds,ax */
192 0xe8, 0x00, 0x00, /* call $ + 3 */
193 0x5e, /* pop si */
194 0x83, 0xc6, 0x19, /* add si,+19h */
195 0xbb, 0x07, 0x00, /* mov bx,0007h */
196 0xfc, /* cld */
197 0xac, /* lodsb */
198 0x84, 0xc0, /* test al,al */
199 0x74, 0x06, /* jz $ + 8 */
200 0xb4, 0x0e, /* mov ah,0eh */
201 0xcd, 0x10, /* int 10h */
202 0xeb, 0xf5, /* jmp $ - 9 */
203 0x30, 0xe4, /* xor ah,ah */
204 0xcd, 0x16, /* int 16h */
205 0xcd, 0x19, /* int 19h */
206 0x0d, 0x0a,
207 'N', 'o', 'n', '-', 's', 'y', 's', 't',
208 'e', 'm', ' ', 'd', 'i', 's', 'k',
209 0x0d, 0x0a,
210 'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
211 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
212 ' ', 'r', 'e', 'b', 'o', 'o', 't',
213 0x0d, 0x0a,
214 0
215};
216
217struct msdos_options {
218 const char *bootstrap;
219 const char *volume_label;
220 const char *OEM_string;
221 const char *floppy;
222 u_int fat_type;
223 u_int volume_id;
224 u_int bytes_per_sector;
225 u_int sectors_per_fat;
226 u_int block_size;
227 u_int sectors_per_cluster;
228 u_int directory_entries;
229 u_int drive_heads;
230 u_int info_sector;
231 u_int backup_sector;
232 u_int media_descriptor;
233 u_int num_FAT;
234 u_int hidden_sectors;
235 u_int reserved_sectors;
236 u_int size;
237 u_int sectors_per_track;
238 int no_create;
239 off_t create_size;
240 off_t offset;
241 int volume_id_set;
242 int media_descriptor_set;
243 int hidden_sectors_set;
244};
245
246static volatile sig_atomic_t got_siginfo;
247static void infohandler(int);
248
249static void check_mounted(const char *, mode_t);
250static void getstdfmt(const char *, struct bpb *);
251static void getdiskinfo(int, const char *, const char *, int,
252 struct bpb *);
253static void print_bpb(struct bpb *);
254static u_int ckgeom(const char *, u_int, const char *);
255static u_int argtou(const char *, u_int, u_int, const char *);
256static off_t argtooff(const char *, const char *);
50static u_int argtou(const char *, u_int, u_int, const char *);
51static off_t argtooff(const char *, const char *);
257static int oklabel(const char *);
258static int mkfs_msdos(const char *, const char *, const struct msdos_options *);
259static void mklabel(u_int8_t *, const char *);
260static void setstr(u_int8_t *, const char *, size_t);
261static void usage(void);
262
263/*
264 * Construct a FAT12, FAT16, or FAT32 file system.
265 */
266int
267main(int argc, char *argv[])
268{

--- 26 unchanged lines hidden (view full) ---

295 errx(1, "%s: bad FAT type", optarg);
296 o.fat_type = atoi(optarg);
297 break;
298 case 'I':
299 o.volume_id = argto4(optarg, 0, "volume ID");
300 o.volume_id_set = 1;
301 break;
302 case 'L':
52static void usage(void);
53
54/*
55 * Construct a FAT12, FAT16, or FAT32 file system.
56 */
57int
58main(int argc, char *argv[])
59{

--- 26 unchanged lines hidden (view full) ---

86 errx(1, "%s: bad FAT type", optarg);
87 o.fat_type = atoi(optarg);
88 break;
89 case 'I':
90 o.volume_id = argto4(optarg, 0, "volume ID");
91 o.volume_id_set = 1;
92 break;
93 case 'L':
303 if (!oklabel(optarg))
304 errx(1, "%s: bad volume label", optarg);
305 o.volume_label = optarg;
306 break;
307 case 'O':
94 o.volume_label = optarg;
95 break;
96 case 'O':
308 if (strlen(optarg) > 8)
309 errx(1, "%s: bad OEM string", optarg);
310 o.OEM_string = optarg;
311 break;
312 case 'S':
313 o.bytes_per_sector = argto2(optarg, 1, "bytes/sector");
314 break;
315 case 'a':
316 o.sectors_per_fat = argto4(optarg, 1, "sectors/FAT");
317 break;

--- 52 unchanged lines hidden (view full) ---

370 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
371 if (!(fname = strdup(buf)))
372 err(1, NULL);
373 }
374 dtype = *argv;
375 return mkfs_msdos(fname, dtype, &o);
376}
377
97 o.OEM_string = optarg;
98 break;
99 case 'S':
100 o.bytes_per_sector = argto2(optarg, 1, "bytes/sector");
101 break;
102 case 'a':
103 o.sectors_per_fat = argto4(optarg, 1, "sectors/FAT");
104 break;

--- 52 unchanged lines hidden (view full) ---

157 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
158 if (!(fname = strdup(buf)))
159 err(1, NULL);
160 }
161 dtype = *argv;
162 return mkfs_msdos(fname, dtype, &o);
163}
164
378int mkfs_msdos(const char *fname, const char *dtype,
379 const struct msdos_options *op)
380{
381 char buf[MAXPATHLEN];
382 struct sigaction si_sa;
383 struct stat sb;
384 struct timeval tv;
385 struct bpb bpb;
386 struct tm *tm;
387 struct bs *bs;
388 struct bsbpb *bsbpb;
389 struct bsxbpb *bsxbpb;
390 struct bsx *bsx;
391 struct de *de;
392 u_int8_t *img;
393 const char *bname;
394 ssize_t n;
395 time_t now;
396 u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
397 int fd, fd1;
398 struct msdos_options o = *op;
399
400 if (o.create_size) {
401 if (o.no_create)
402 errx(1, "create (-C) is incompatible with -N");
403 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644);
404 if (fd == -1)
405 errx(1, "failed to create %s", fname);
406 if (ftruncate(fd, o.create_size))
407 errx(1, "failed to initialize %jd bytes", (intmax_t)o.create_size);
408 } else if ((fd = open(fname, o.no_create ? O_RDONLY : O_RDWR)) == -1)
409 err(1, "%s", fname);
410 if (fstat(fd, &sb))
411 err(1, "%s", fname);
412 if (o.create_size) {
413 if (!S_ISREG(sb.st_mode))
414 warnx("warning, %s is not a regular file", fname);
415 } else {
416 if (!S_ISCHR(sb.st_mode))
417 warnx("warning, %s is not a character device", fname);
418 }
419 if (!o.no_create)
420 check_mounted(fname, sb.st_mode);
421 if (o.offset && o.offset != lseek(fd, o.offset, SEEK_SET))
422 errx(1, "cannot seek to %jd", (intmax_t)o.offset);
423 memset(&bpb, 0, sizeof(bpb));
424 if (o.floppy) {
425 getstdfmt(o.floppy, &bpb);
426 bpb.bpbHugeSectors = bpb.bpbSectors;
427 bpb.bpbSectors = 0;
428 bpb.bpbBigFATsecs = bpb.bpbFATsecs;
429 bpb.bpbFATsecs = 0;
430 }
431 if (o.drive_heads)
432 bpb.bpbHeads = o.drive_heads;
433 if (o.sectors_per_track)
434 bpb.bpbSecPerTrack = o.sectors_per_track;
435 if (o.bytes_per_sector)
436 bpb.bpbBytesPerSec = o.bytes_per_sector;
437 if (o.size)
438 bpb.bpbHugeSectors = o.size;
439 if (o.hidden_sectors_set)
440 bpb.bpbHiddenSecs = o.hidden_sectors;
441 if (!(o.floppy || (o.drive_heads && o.sectors_per_track && o.bytes_per_sector && o.size && o.hidden_sectors_set))) {
442 off_t delta;
443 getdiskinfo(fd, fname, dtype, o.hidden_sectors_set, &bpb);
444 bpb.bpbHugeSectors -= (o.offset / bpb.bpbBytesPerSec);
445 delta = bpb.bpbHugeSectors % bpb.bpbSecPerTrack;
446 if (delta != 0) {
447 warnx("trim %d sectors to adjust to a multiple of %d",
448 (int)delta, bpb.bpbSecPerTrack);
449 bpb.bpbHugeSectors -= delta;
450 }
451 if (bpb.bpbSecPerClust == 0) { /* set defaults */
452 if (bpb.bpbHugeSectors <= 6000) /* about 3MB -> 512 bytes */
453 bpb.bpbSecPerClust = 1;
454 else if (bpb.bpbHugeSectors <= (1<<17)) /* 64M -> 4k */
455 bpb.bpbSecPerClust = 8;
456 else if (bpb.bpbHugeSectors <= (1<<19)) /* 256M -> 8k */
457 bpb.bpbSecPerClust = 16;
458 else if (bpb.bpbHugeSectors <= (1<<21)) /* 1G -> 16k */
459 bpb.bpbSecPerClust = 32;
460 else
461 bpb.bpbSecPerClust = 64; /* otherwise 32k */
462 }
463 }
464 if (!powerof2(bpb.bpbBytesPerSec))
465 errx(1, "bytes/sector (%u) is not a power of 2", bpb.bpbBytesPerSec);
466 if (bpb.bpbBytesPerSec < MINBPS)
467 errx(1, "bytes/sector (%u) is too small; minimum is %u",
468 bpb.bpbBytesPerSec, MINBPS);
469 if (!(fat = o.fat_type)) {
470 if (o.floppy)
471 fat = 12;
472 else if (!o.directory_entries && (o.info_sector || o.backup_sector))
473 fat = 32;
474 }
475 if ((fat == 32 && o.directory_entries) || (fat != 32 && (o.info_sector || o.backup_sector)))
476 errx(1, "-%c is not a legal FAT%s option",
477 fat == 32 ? 'e' : o.info_sector ? 'i' : 'k',
478 fat == 32 ? "32" : "12/16");
479 if (o.floppy && fat == 32)
480 bpb.bpbRootDirEnts = 0;
481 if (o.block_size) {
482 if (!powerof2(o.block_size))
483 errx(1, "block size (%u) is not a power of 2", o.block_size);
484 if (o.block_size < bpb.bpbBytesPerSec)
485 errx(1, "block size (%u) is too small; minimum is %u",
486 o.block_size, bpb.bpbBytesPerSec);
487 if (o.block_size > bpb.bpbBytesPerSec * MAXSPC)
488 errx(1, "block size (%u) is too large; maximum is %u",
489 o.block_size, bpb.bpbBytesPerSec * MAXSPC);
490 bpb.bpbSecPerClust = o.block_size / bpb.bpbBytesPerSec;
491 }
492 if (o.sectors_per_cluster) {
493 if (!powerof2(o.sectors_per_cluster))
494 errx(1, "sectors/cluster (%u) is not a power of 2", o.sectors_per_cluster);
495 bpb.bpbSecPerClust = o.sectors_per_cluster;
496 }
497 if (o.reserved_sectors)
498 bpb.bpbResSectors = o.reserved_sectors;
499 if (o.num_FAT) {
500 if (o.num_FAT > MAXNFT)
501 errx(1, "number of FATs (%u) is too large; maximum is %u",
502 o.num_FAT, MAXNFT);
503 bpb.bpbFATs = o.num_FAT;
504 }
505 if (o.directory_entries)
506 bpb.bpbRootDirEnts = o.directory_entries;
507 if (o.media_descriptor_set) {
508 if (o.media_descriptor < 0xf0)
509 errx(1, "illegal media descriptor (%#x)", o.media_descriptor);
510 bpb.bpbMedia = o.media_descriptor;
511 }
512 if (o.sectors_per_fat)
513 bpb.bpbBigFATsecs = o.sectors_per_fat;
514 if (o.info_sector)
515 bpb.bpbFSInfo = o.info_sector;
516 if (o.backup_sector)
517 bpb.bpbBackup = o.backup_sector;
518 bss = 1;
519 bname = NULL;
520 fd1 = -1;
521 if (o.bootstrap) {
522 bname = o.bootstrap;
523 if (!strchr(bname, '/')) {
524 snprintf(buf, sizeof(buf), "/boot/%s", bname);
525 if (!(bname = strdup(buf)))
526 err(1, NULL);
527 }
528 if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb))
529 err(1, "%s", bname);
530 if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bpbBytesPerSec ||
531 sb.st_size < bpb.bpbBytesPerSec ||
532 sb.st_size > bpb.bpbBytesPerSec * MAXU16)
533 errx(1, "%s: inappropriate file type or format", bname);
534 bss = sb.st_size / bpb.bpbBytesPerSec;
535 }
536 if (!bpb.bpbFATs)
537 bpb.bpbFATs = 2;
538 if (!fat) {
539 if (bpb.bpbHugeSectors < (bpb.bpbResSectors ? bpb.bpbResSectors : bss) +
540 howmany((RESFTE + (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1)) *
541 (bpb.bpbSecPerClust ? 16 : 12) / BPN,
542 bpb.bpbBytesPerSec * NPB) *
543 bpb.bpbFATs +
544 howmany(bpb.bpbRootDirEnts ? bpb.bpbRootDirEnts : DEFRDE,
545 bpb.bpbBytesPerSec / sizeof(struct de)) +
546 (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1) *
547 (bpb.bpbSecPerClust ? bpb.bpbSecPerClust :
548 howmany(DEFBLK, bpb.bpbBytesPerSec)))
549 fat = 12;
550 else if (bpb.bpbRootDirEnts || bpb.bpbHugeSectors <
551 (bpb.bpbResSectors ? bpb.bpbResSectors : bss) +
552 howmany((RESFTE + MAXCLS16) * 2, bpb.bpbBytesPerSec) *
553 bpb.bpbFATs +
554 howmany(DEFRDE, bpb.bpbBytesPerSec / sizeof(struct de)) +
555 (MAXCLS16 + 1) *
556 (bpb.bpbSecPerClust ? bpb.bpbSecPerClust :
557 howmany(8192, bpb.bpbBytesPerSec)))
558 fat = 16;
559 else
560 fat = 32;
561 }
562 x = bss;
563 if (fat == 32) {
564 if (!bpb.bpbFSInfo) {
565 if (x == MAXU16 || x == bpb.bpbBackup)
566 errx(1, "no room for info sector");
567 bpb.bpbFSInfo = x;
568 }
569 if (bpb.bpbFSInfo != MAXU16 && x <= bpb.bpbFSInfo)
570 x = bpb.bpbFSInfo + 1;
571 if (!bpb.bpbBackup) {
572 if (x == MAXU16)
573 errx(1, "no room for backup sector");
574 bpb.bpbBackup = x;
575 } else if (bpb.bpbBackup != MAXU16 && bpb.bpbBackup == bpb.bpbFSInfo)
576 errx(1, "backup sector would overwrite info sector");
577 if (bpb.bpbBackup != MAXU16 && x <= bpb.bpbBackup)
578 x = bpb.bpbBackup + 1;
579 }
580 if (!bpb.bpbResSectors)
581 bpb.bpbResSectors = fat == 32 ?
582 MAX(x, MAX(16384 / bpb.bpbBytesPerSec, 4)) : x;
583 else if (bpb.bpbResSectors < x)
584 errx(1, "too few reserved sectors (need %d have %d)", x,
585 bpb.bpbResSectors);
586 if (fat != 32 && !bpb.bpbRootDirEnts)
587 bpb.bpbRootDirEnts = DEFRDE;
588 rds = howmany(bpb.bpbRootDirEnts, bpb.bpbBytesPerSec / sizeof(struct de));
589 if (!bpb.bpbSecPerClust)
590 for (bpb.bpbSecPerClust = howmany(fat == 16 ? DEFBLK16 :
591 DEFBLK, bpb.bpbBytesPerSec);
592 bpb.bpbSecPerClust < MAXSPC &&
593 bpb.bpbResSectors +
594 howmany((RESFTE + maxcls(fat)) * (fat / BPN),
595 bpb.bpbBytesPerSec * NPB) *
596 bpb.bpbFATs +
597 rds +
598 (u_int64_t) (maxcls(fat) + 1) *
599 bpb.bpbSecPerClust <= bpb.bpbHugeSectors;
600 bpb.bpbSecPerClust <<= 1)
601 continue;
602 if (fat != 32 && bpb.bpbBigFATsecs > MAXU16)
603 errx(1, "too many sectors/FAT for FAT12/16");
604 x1 = bpb.bpbResSectors + rds;
605 x = bpb.bpbBigFATsecs ? bpb.bpbBigFATsecs : 1;
606 if (x1 + (u_int64_t)x * bpb.bpbFATs > bpb.bpbHugeSectors)
607 errx(1, "meta data exceeds file system size");
608 x1 += x * bpb.bpbFATs;
609 x = (u_int64_t)(bpb.bpbHugeSectors - x1) * bpb.bpbBytesPerSec * NPB /
610 (bpb.bpbSecPerClust * bpb.bpbBytesPerSec * NPB + fat /
611 BPN * bpb.bpbFATs);
612 x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN),
613 bpb.bpbBytesPerSec * NPB);
614 if (!bpb.bpbBigFATsecs) {
615 bpb.bpbBigFATsecs = x2;
616 x1 += (bpb.bpbBigFATsecs - 1) * bpb.bpbFATs;
617 }
618 cls = (bpb.bpbHugeSectors - x1) / bpb.bpbSecPerClust;
619 x = (u_int64_t)bpb.bpbBigFATsecs * bpb.bpbBytesPerSec * NPB / (fat / BPN) -
620 RESFTE;
621 if (cls > x)
622 cls = x;
623 if (bpb.bpbBigFATsecs < x2)
624 warnx("warning: sectors/FAT limits file system to %u clusters",
625 cls);
626 if (cls < mincls(fat))
627 errx(1, "%u clusters too few clusters for FAT%u, need %u", cls, fat,
628 mincls(fat));
629 if (cls > maxcls(fat)) {
630 cls = maxcls(fat);
631 bpb.bpbHugeSectors = x1 + (cls + 1) * bpb.bpbSecPerClust - 1;
632 warnx("warning: FAT type limits file system to %u sectors",
633 bpb.bpbHugeSectors);
634 }
635 printf("%s: %u sector%s in %u FAT%u cluster%s "
636 "(%u bytes/cluster)\n", fname, cls * bpb.bpbSecPerClust,
637 cls * bpb.bpbSecPerClust == 1 ? "" : "s", cls, fat,
638 cls == 1 ? "" : "s", bpb.bpbBytesPerSec * bpb.bpbSecPerClust);
639 if (!bpb.bpbMedia)
640 bpb.bpbMedia = !bpb.bpbHiddenSecs ? 0xf0 : 0xf8;
641 if (fat == 32)
642 bpb.bpbRootClust = RESFTE;
643 if (bpb.bpbHiddenSecs + bpb.bpbHugeSectors <= MAXU16) {
644 bpb.bpbSectors = bpb.bpbHugeSectors;
645 bpb.bpbHugeSectors = 0;
646 }
647 if (fat != 32) {
648 bpb.bpbFATsecs = bpb.bpbBigFATsecs;
649 bpb.bpbBigFATsecs = 0;
650 }
651 print_bpb(&bpb);
652 if (!o.no_create) {
653 gettimeofday(&tv, NULL);
654 now = tv.tv_sec;
655 tm = localtime(&now);
656 if (!(img = malloc(bpb.bpbBytesPerSec)))
657 err(1, NULL);
658 dir = bpb.bpbResSectors + (bpb.bpbFATsecs ? bpb.bpbFATsecs :
659 bpb.bpbBigFATsecs) * bpb.bpbFATs;
660 memset(&si_sa, 0, sizeof(si_sa));
661 si_sa.sa_handler = infohandler;
662 if (sigaction(SIGINFO, &si_sa, NULL) == -1)
663 err(1, "sigaction SIGINFO");
664 for (lsn = 0; lsn < dir + (fat == 32 ? bpb.bpbSecPerClust : rds); lsn++) {
665 if (got_siginfo) {
666 fprintf(stderr,"%s: writing sector %u of %u (%u%%)\n",
667 fname, lsn,
668 (dir + (fat == 32 ? bpb.bpbSecPerClust: rds)),
669 (lsn * 100) / (dir +
670 (fat == 32 ? bpb.bpbSecPerClust: rds)));
671 got_siginfo = 0;
672 }
673 x = lsn;
674 if (o.bootstrap &&
675 fat == 32 && bpb.bpbBackup != MAXU16 &&
676 bss <= bpb.bpbBackup && x >= bpb.bpbBackup) {
677 x -= bpb.bpbBackup;
678 if (!x && lseek(fd1, o.offset, SEEK_SET))
679 err(1, "%s", bname);
680 }
681 if (o.bootstrap && x < bss) {
682 if ((n = read(fd1, img, bpb.bpbBytesPerSec)) == -1)
683 err(1, "%s", bname);
684 if ((unsigned)n != bpb.bpbBytesPerSec)
685 errx(1, "%s: can't read sector %u", bname, x);
686 } else
687 memset(img, 0, bpb.bpbBytesPerSec);
688 if (!lsn ||
689 (fat == 32 && bpb.bpbBackup != MAXU16 &&
690 lsn == bpb.bpbBackup)) {
691 x1 = sizeof(struct bs);
692 bsbpb = (struct bsbpb *)(img + x1);
693 mk2(bsbpb->bpbBytesPerSec, bpb.bpbBytesPerSec);
694 mk1(bsbpb->bpbSecPerClust, bpb.bpbSecPerClust);
695 mk2(bsbpb->bpbResSectors, bpb.bpbResSectors);
696 mk1(bsbpb->bpbFATs, bpb.bpbFATs);
697 mk2(bsbpb->bpbRootDirEnts, bpb.bpbRootDirEnts);
698 mk2(bsbpb->bpbSectors, bpb.bpbSectors);
699 mk1(bsbpb->bpbMedia, bpb.bpbMedia);
700 mk2(bsbpb->bpbFATsecs, bpb.bpbFATsecs);
701 mk2(bsbpb->bpbSecPerTrack, bpb.bpbSecPerTrack);
702 mk2(bsbpb->bpbHeads, bpb.bpbHeads);
703 mk4(bsbpb->bpbHiddenSecs, bpb.bpbHiddenSecs);
704 mk4(bsbpb->bpbHugeSectors, bpb.bpbHugeSectors);
705 x1 += sizeof(struct bsbpb);
706 if (fat == 32) {
707 bsxbpb = (struct bsxbpb *)(img + x1);
708 mk4(bsxbpb->bpbBigFATsecs, bpb.bpbBigFATsecs);
709 mk2(bsxbpb->bpbExtFlags, 0);
710 mk2(bsxbpb->bpbFSVers, 0);
711 mk4(bsxbpb->bpbRootClust, bpb.bpbRootClust);
712 mk2(bsxbpb->bpbFSInfo, bpb.bpbFSInfo);
713 mk2(bsxbpb->bpbBackup, bpb.bpbBackup);
714 x1 += sizeof(struct bsxbpb);
715 }
716 bsx = (struct bsx *)(img + x1);
717 mk1(bsx->exBootSignature, 0x29);
718 if (o.volume_id_set)
719 x = o.volume_id;
720 else
721 x = (((u_int)(1 + tm->tm_mon) << 8 |
722 (u_int)tm->tm_mday) +
723 ((u_int)tm->tm_sec << 8 |
724 (u_int)(tv.tv_usec / 10))) << 16 |
725 ((u_int)(1900 + tm->tm_year) +
726 ((u_int)tm->tm_hour << 8 |
727 (u_int)tm->tm_min));
728 mk4(bsx->exVolumeID, x);
729 mklabel(bsx->exVolumeLabel, o.volume_label ? o.volume_label : "NO NAME");
730 sprintf(buf, "FAT%u", fat);
731 setstr(bsx->exFileSysType, buf, sizeof(bsx->exFileSysType));
732 if (!o.bootstrap) {
733 x1 += sizeof(struct bsx);
734 bs = (struct bs *)img;
735 mk1(bs->bsJump[0], 0xeb);
736 mk1(bs->bsJump[1], x1 - 2);
737 mk1(bs->bsJump[2], 0x90);
738 setstr(bs->bsOemName, o.OEM_string ? o.OEM_string : "BSD4.4 ",
739 sizeof(bs->bsOemName));
740 memcpy(img + x1, bootcode, sizeof(bootcode));
741 mk2(img + MINBPS - 2, DOSMAGIC);
742 }
743 } else if (fat == 32 && bpb.bpbFSInfo != MAXU16 &&
744 (lsn == bpb.bpbFSInfo ||
745 (bpb.bpbBackup != MAXU16 &&
746 lsn == bpb.bpbBackup + bpb.bpbFSInfo))) {
747 mk4(img, 0x41615252);
748 mk4(img + MINBPS - 28, 0x61417272);
749 mk4(img + MINBPS - 24, 0xffffffff);
750 mk4(img + MINBPS - 20, bpb.bpbRootClust);
751 mk2(img + MINBPS - 2, DOSMAGIC);
752 } else if (lsn >= bpb.bpbResSectors && lsn < dir &&
753 !((lsn - bpb.bpbResSectors) %
754 (bpb.bpbFATsecs ? bpb.bpbFATsecs :
755 bpb.bpbBigFATsecs))) {
756 mk1(img[0], bpb.bpbMedia);
757 for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
758 mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
759 } else if (lsn == dir && o.volume_label) {
760 de = (struct de *)img;
761 mklabel(de->deName, o.volume_label);
762 mk1(de->deAttributes, 050);
763 x = (u_int)tm->tm_hour << 11 |
764 (u_int)tm->tm_min << 5 |
765 (u_int)tm->tm_sec >> 1;
766 mk2(de->deMTime, x);
767 x = (u_int)(tm->tm_year - 80) << 9 |
768 (u_int)(tm->tm_mon + 1) << 5 |
769 (u_int)tm->tm_mday;
770 mk2(de->deMDate, x);
771 }
772 if ((n = write(fd, img, bpb.bpbBytesPerSec)) == -1)
773 err(1, "%s", fname);
774 if ((unsigned)n != bpb.bpbBytesPerSec)
775 errx(1, "%s: can't write sector %u", fname, lsn);
776 }
777 }
778 return 0;
779}
780
781/*
165/*
782 * Exit with error if file system is mounted.
783 */
784static void
785check_mounted(const char *fname, mode_t mode)
786{
787 struct statfs *mp;
788 const char *s1, *s2;
789 size_t len;
790 int n, r;
791
792 if (!(n = getmntinfo(&mp, MNT_NOWAIT)))
793 err(1, "getmntinfo");
794 len = strlen(_PATH_DEV);
795 s1 = fname;
796 if (!strncmp(s1, _PATH_DEV, len))
797 s1 += len;
798 r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
799 for (; n--; mp++) {
800 s2 = mp->f_mntfromname;
801 if (!strncmp(s2, _PATH_DEV, len))
802 s2 += len;
803 if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
804 !strcmp(s1, s2))
805 errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
806 }
807}
808
809/*
810 * Get a standard format.
811 */
812static void
813getstdfmt(const char *fmt, struct bpb *bpb)
814{
815 u_int x, i;
816
817 x = sizeof(stdfmt) / sizeof(stdfmt[0]);
818 for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
819 if (i == x)
820 errx(1, "%s: unknown standard format", fmt);
821 *bpb = stdfmt[i].bpb;
822}
823
824/*
825 * Get disk slice, partition, and geometry information.
826 */
827static void
828getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
829 struct bpb *bpb)
830{
831 struct disklabel *lp, dlp;
832 struct fd_type type;
833 off_t ms, hs = 0;
834
835 lp = NULL;
836
837 /* If the user specified a disk type, try to use that */
838 if (dtype != NULL) {
839 lp = getdiskbyname(dtype);
840 }
841
842 /* Maybe it's a floppy drive */
843 if (lp == NULL) {
844 if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1) {
845 struct stat st;
846
847 if (fstat(fd, &st))
848 err(1, "cannot get disk size");
849 /* create a fake geometry for a file image */
850 ms = st.st_size;
851 dlp.d_secsize = 512;
852 dlp.d_nsectors = 63;
853 dlp.d_ntracks = 255;
854 dlp.d_secperunit = ms / dlp.d_secsize;
855 lp = &dlp;
856 } else if (ioctl(fd, FD_GTYPE, &type) != -1) {
857 dlp.d_secsize = 128 << type.secsize;
858 dlp.d_nsectors = type.sectrac;
859 dlp.d_ntracks = type.heads;
860 dlp.d_secperunit = ms / dlp.d_secsize;
861 lp = &dlp;
862 }
863 }
864
865 /* Maybe it's a fixed drive */
866 if (lp == NULL) {
867 if (bpb->bpbBytesPerSec)
868 dlp.d_secsize = bpb->bpbBytesPerSec;
869 if (bpb->bpbBytesPerSec == 0 && ioctl(fd, DIOCGSECTORSIZE,
870 &dlp.d_secsize) == -1)
871 err(1, "cannot get sector size");
872
873 dlp.d_secperunit = ms / dlp.d_secsize;
874
875 if (bpb->bpbSecPerTrack == 0 && ioctl(fd, DIOCGFWSECTORS,
876 &dlp.d_nsectors) == -1) {
877 warn("cannot get number of sectors per track");
878 dlp.d_nsectors = 63;
879 }
880 if (bpb->bpbHeads == 0 &&
881 ioctl(fd, DIOCGFWHEADS, &dlp.d_ntracks) == -1) {
882 warn("cannot get number of heads");
883 if (dlp.d_secperunit <= 63*1*1024)
884 dlp.d_ntracks = 1;
885 else if (dlp.d_secperunit <= 63*16*1024)
886 dlp.d_ntracks = 16;
887 else
888 dlp.d_ntracks = 255;
889 }
890
891 hs = (ms / dlp.d_secsize) - dlp.d_secperunit;
892 lp = &dlp;
893 }
894
895 if (bpb->bpbBytesPerSec == 0)
896 bpb->bpbBytesPerSec = ckgeom(fname, lp->d_secsize, "bytes/sector");
897 if (bpb->bpbSecPerTrack == 0)
898 bpb->bpbSecPerTrack = ckgeom(fname, lp->d_nsectors, "sectors/track");
899 if (bpb->bpbHeads == 0)
900 bpb->bpbHeads = ckgeom(fname, lp->d_ntracks, "drive heads");
901 if (bpb->bpbHugeSectors == 0)
902 bpb->bpbHugeSectors = lp->d_secperunit;
903 if (bpb->bpbHiddenSecs == 0)
904 bpb->bpbHiddenSecs = hs;
905}
906
907/*
908 * Print out BPB values.
909 */
910static void
911print_bpb(struct bpb *bpb)
912{
913 printf("BytesPerSec=%u SecPerClust=%u ResSectors=%u FATs=%u",
914 bpb->bpbBytesPerSec, bpb->bpbSecPerClust, bpb->bpbResSectors,
915 bpb->bpbFATs);
916 if (bpb->bpbRootDirEnts)
917 printf(" RootDirEnts=%u", bpb->bpbRootDirEnts);
918 if (bpb->bpbSectors)
919 printf(" Sectors=%u", bpb->bpbSectors);
920 printf(" Media=%#x", bpb->bpbMedia);
921 if (bpb->bpbFATsecs)
922 printf(" FATsecs=%u", bpb->bpbFATsecs);
923 printf(" SecPerTrack=%u Heads=%u HiddenSecs=%u", bpb->bpbSecPerTrack,
924 bpb->bpbHeads, bpb->bpbHiddenSecs);
925 if (bpb->bpbHugeSectors)
926 printf(" HugeSectors=%u", bpb->bpbHugeSectors);
927 if (!bpb->bpbFATsecs) {
928 printf(" FATsecs=%u RootCluster=%u", bpb->bpbBigFATsecs,
929 bpb->bpbRootClust);
930 printf(" FSInfo=");
931 printf(bpb->bpbFSInfo == MAXU16 ? "%#x" : "%u", bpb->bpbFSInfo);
932 printf(" Backup=");
933 printf(bpb->bpbBackup == MAXU16 ? "%#x" : "%u", bpb->bpbBackup);
934 }
935 printf("\n");
936}
937
938/*
939 * Check a disk geometry value.
940 */
941static u_int
942ckgeom(const char *fname, u_int val, const char *msg)
943{
944 if (!val)
945 errx(1, "%s: no default %s", fname, msg);
946 if (val > MAXU16)
947 errx(1, "%s: illegal %s %d", fname, msg, val);
948 return val;
949}
950
951/*
952 * Convert and check a numeric option argument.
953 */
954static u_int
955argtou(const char *arg, u_int lo, u_int hi, const char *msg)
956{
957 char *s;
958 u_long x;
959

--- 18 unchanged lines hidden (view full) ---

978 /* allow at most one extra char */
979 if (errno || x < 0 || (s[0] && s[1]) )
980 errx(1, "%s: bad %s", arg, msg);
981 if (*s) { /* the extra char is the multiplier */
982 switch (*s) {
983 default:
984 errx(1, "%s: bad %s", arg, msg);
985 /* notreached */
166 * Convert and check a numeric option argument.
167 */
168static u_int
169argtou(const char *arg, u_int lo, u_int hi, const char *msg)
170{
171 char *s;
172 u_long x;
173

--- 18 unchanged lines hidden (view full) ---

192 /* allow at most one extra char */
193 if (errno || x < 0 || (s[0] && s[1]) )
194 errx(1, "%s: bad %s", arg, msg);
195 if (*s) { /* the extra char is the multiplier */
196 switch (*s) {
197 default:
198 errx(1, "%s: bad %s", arg, msg);
199 /* notreached */
986
200
987 case 's': /* sector */
988 case 'S':
989 x <<= 9; /* times 512 */
990 break;
991
992 case 'k': /* kilobyte */
993 case 'K':
994 x <<= 10; /* times 1024 */

--- 16 unchanged lines hidden (view full) ---

1011 errx(1, "%s: not supported yet %s", arg, msg);
1012 /* notreached */
1013 }
1014 }
1015 return x;
1016}
1017
1018/*
201 case 's': /* sector */
202 case 'S':
203 x <<= 9; /* times 512 */
204 break;
205
206 case 'k': /* kilobyte */
207 case 'K':
208 x <<= 10; /* times 1024 */

--- 16 unchanged lines hidden (view full) ---

225 errx(1, "%s: not supported yet %s", arg, msg);
226 /* notreached */
227 }
228 }
229 return x;
230}
231
232/*
1019 * Check a volume label.
1020 */
1021static int
1022oklabel(const char *src)
1023{
1024 int c, i;
1025
1026 for (i = 0; i <= 11; i++) {
1027 c = (u_char)*src++;
1028 if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
1029 break;
1030 }
1031 return i && !c;
1032}
1033
1034/*
1035 * Make a volume label.
1036 */
1037static void
1038mklabel(u_int8_t *dest, const char *src)
1039{
1040 int c, i;
1041
1042 for (i = 0; i < 11; i++) {
1043 c = *src ? toupper(*src++) : ' ';
1044 *dest++ = !i && c == '\xe5' ? 5 : c;
1045 }
1046}
1047
1048/*
1049 * Copy string, padding with spaces.
1050 */
1051static void
1052setstr(u_int8_t *dest, const char *src, size_t len)
1053{
1054 while (len--)
1055 *dest++ = *src ? *src++ : ' ';
1056}
1057
1058/*
1059 * Print usage message.
1060 */
1061static void
1062usage(void)
1063{
1064 fprintf(stderr,
1065 "usage: newfs_msdos [ -options ] special [disktype]\n"
1066 "where the options are:\n"

--- 17 unchanged lines hidden (view full) ---

1084 "\t-m media descriptor\n"
1085 "\t-n number of FATs\n"
1086 "\t-o hidden sectors\n"
1087 "\t-r reserved sectors\n"
1088 "\t-s file system size (sectors)\n"
1089 "\t-u sectors/track\n");
1090 exit(1);
1091}
233 * Print usage message.
234 */
235static void
236usage(void)
237{
238 fprintf(stderr,
239 "usage: newfs_msdos [ -options ] special [disktype]\n"
240 "where the options are:\n"

--- 17 unchanged lines hidden (view full) ---

258 "\t-m media descriptor\n"
259 "\t-n number of FATs\n"
260 "\t-o hidden sectors\n"
261 "\t-r reserved sectors\n"
262 "\t-s file system size (sectors)\n"
263 "\t-u sectors/track\n");
264 exit(1);
265}
1092
1093static void
1094infohandler(int sig __unused)
1095{
1096
1097 got_siginfo = 1;
1098}