xref: /freebsd/sbin/newfs_msdos/mkfs_msdos.c (revision 17e85f59)
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
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
13  *    distribution.
14  *
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.
26  */
27 
28 #include <sys/param.h>
29 #ifdef MAKEFS
30 /* In the makefs case we only want struct disklabel */
31 #include <sys/disk/bsd.h>
32 #else
33 #include <sys/fdcio.h>
34 #include <sys/disk.h>
35 #include <sys/disklabel.h>
36 #include <sys/mount.h>
37 #endif
38 #include <sys/stat.h>
39 #include <sys/sysctl.h>
40 #include <sys/time.h>
41 
42 #include <assert.h>
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <inttypes.h>
48 #include <paths.h>
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <time.h>
54 #include <unistd.h>
55 
56 #include "mkfs_msdos.h"
57 
58 #define	MAXU16	  0xffff	/* maximum unsigned 16-bit quantity */
59 #define	BPN	  4		/* bits per nibble */
60 #define	NPB	  2		/* nibbles per byte */
61 
62 #define	DOSMAGIC  0xaa55	/* DOS magic number */
63 #define	MINBPS	  512		/* minimum bytes per sector */
64 #define	MAXBPS    4096		/* maximum bytes per sector */
65 #define	MAXSPC	  128		/* maximum sectors per cluster */
66 #define	MAXNFT	  16		/* maximum number of FATs */
67 #define	DEFBLK	  4096		/* default block size */
68 #define	DEFBLK16  2048		/* default block size FAT16 */
69 #define	DEFRDE	  512		/* default root directory entries */
70 #define	RESFTE	  2		/* reserved FAT entries */
71 #define	MINCLS12  1U		/* minimum FAT12 clusters */
72 #define	MINCLS16  0xff5U	/* minimum FAT16 clusters */
73 #define	MINCLS32  0xfff5U	/* minimum FAT32 clusters */
74 #define	MAXCLS12  0xff4U	/* maximum FAT12 clusters */
75 #define	MAXCLS16  0xfff4U	/* maximum FAT16 clusters */
76 #define	MAXCLS32  0xffffff4U	/* maximum FAT32 clusters */
77 
78 #define	mincls(fat)  ((fat) == 12 ? MINCLS12 :	\
79 		      (fat) == 16 ? MINCLS16 :	\
80 				    MINCLS32)
81 
82 #define	maxcls(fat)  ((fat) == 12 ? MAXCLS12 :	\
83 		      (fat) == 16 ? MAXCLS16 :	\
84 				    MAXCLS32)
85 
86 #define	mk1(p, x)				\
87     (p) = (u_int8_t)(x)
88 
89 #define	mk2(p, x)				\
90     (p)[0] = (u_int8_t)(x),			\
91     (p)[1] = (u_int8_t)((x) >> 010)
92 
93 #define	mk4(p, x)				\
94     (p)[0] = (u_int8_t)(x),			\
95     (p)[1] = (u_int8_t)((x) >> 010),		\
96     (p)[2] = (u_int8_t)((x) >> 020),		\
97     (p)[3] = (u_int8_t)((x) >> 030)
98 
99 struct bs {
100     u_int8_t bsJump[3];			/* bootstrap entry point */
101     u_int8_t bsOemName[8];		/* OEM name and version */
102 } __packed;
103 
104 struct 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 
119 struct 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 
129 struct 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 
138 struct 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 
148 struct 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 
169 static 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 
185 static 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 
217 static volatile sig_atomic_t got_siginfo;
218 static void infohandler(int);
219 
220 #ifndef MAKEFS
221 static int check_mounted(const char *, mode_t);
222 #endif
223 static ssize_t getchunksize(void);
224 static int getstdfmt(const char *, struct bpb *);
225 static int getdiskinfo(int, const char *, const char *, int, struct bpb *);
226 static void print_bpb(struct bpb *);
227 static int ckgeom(const char *, u_int, const char *);
228 static void mklabel(u_int8_t *, const char *);
229 static int oklabel(const char *);
230 static void setstr(u_int8_t *, const char *, size_t);
231 
232 int
mkfs_msdos(const char * fname,const char * dtype,const struct msdos_options * op)233 mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op)
234 {
235     char buf[MAXPATHLEN];
236     struct sigaction si_sa;
237     struct stat sb;
238     struct timeval tv;
239     struct bpb bpb;
240     struct tm *tm;
241     struct bs *bs;
242     struct bsbpb *bsbpb;
243     struct bsxbpb *bsxbpb;
244     struct bsx *bsx;
245     struct de *de;
246     u_int8_t *img;
247     u_int8_t *physbuf, *physbuf_end;
248     const char *bname;
249     ssize_t n;
250     time_t now;
251     u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
252     u_int extra_res, alignment, alignto, saved_x, attempts=0;
253     bool set_res, set_spf, set_spc;
254     int fd, fd1, rv;
255     struct msdos_options o = *op;
256     ssize_t chunksize;
257 
258     physbuf = NULL;
259     rv = -1;
260     fd = fd1 = -1;
261 
262     if (o.block_size && o.sectors_per_cluster) {
263 	warnx("Cannot specify both block size and sectors per cluster");
264 	goto done;
265     }
266     if (o.OEM_string && strlen(o.OEM_string) > 8) {
267 	warnx("%s: bad OEM string", o.OEM_string);
268 	goto done;
269     }
270     if (o.create_size) {
271 	if (o.no_create) {
272 	    warnx("create (-C) is incompatible with -N");
273 	    goto done;
274 	}
275 	fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644);
276 	if (fd == -1) {
277 	    warnx("failed to create %s", fname);
278 	    goto done;
279 	}
280 	if (ftruncate(fd, o.create_size)) {
281 	    warnx("failed to initialize %jd bytes", (intmax_t)o.create_size);
282 	    goto done;
283 	}
284     } else if ((fd = open(fname, o.no_create ? O_RDONLY : O_RDWR)) == -1) {
285 	warn("%s", fname);
286 	goto done;
287     }
288     if (fstat(fd, &sb)) {
289 	warn("%s", fname);
290 	goto done;
291     }
292     if (o.create_size) {
293 	if (!S_ISREG(sb.st_mode))
294 	    warnx("warning, %s is not a regular file", fname);
295     } else {
296 #ifdef MAKEFS
297 	errx(1, "o.create_size must be set!");
298 #else
299 	if (!S_ISCHR(sb.st_mode))
300 	    warnx("warning, %s is not a character device", fname);
301 #endif
302     }
303 #ifndef MAKEFS
304     if (!o.no_create)
305 	if (check_mounted(fname, sb.st_mode) == -1)
306 	    goto done;
307 #endif
308     if (o.offset && o.offset != lseek(fd, o.offset, SEEK_SET)) {
309 	warnx("cannot seek to %jd", (intmax_t)o.offset);
310 	goto done;
311     }
312     memset(&bpb, 0, sizeof(bpb));
313     if (o.floppy) {
314 	if (getstdfmt(o.floppy, &bpb) == -1)
315 	    goto done;
316 	bpb.bpbHugeSectors = bpb.bpbSectors;
317 	bpb.bpbSectors = 0;
318 	bpb.bpbBigFATsecs = bpb.bpbFATsecs;
319 	bpb.bpbFATsecs = 0;
320     }
321     if (o.drive_heads)
322 	bpb.bpbHeads = o.drive_heads;
323     if (o.sectors_per_track)
324 	bpb.bpbSecPerTrack = o.sectors_per_track;
325     if (o.bytes_per_sector)
326 	bpb.bpbBytesPerSec = o.bytes_per_sector;
327     if (o.size)
328 	bpb.bpbHugeSectors = o.size;
329     if (o.hidden_sectors_set)
330 	bpb.bpbHiddenSecs = 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 (getdiskinfo(fd, fname, dtype, o.hidden_sectors_set, &bpb) == -1)
334 		goto done;
335 	bpb.bpbHugeSectors -= (o.offset / bpb.bpbBytesPerSec);
336 	if (bpb.bpbSecPerClust == 0) {	/* set defaults */
337 	    if (bpb.bpbHugeSectors <= 6000)	/* about 3MB -> 512 bytes */
338 		bpb.bpbSecPerClust = 1;
339 	    else if (bpb.bpbHugeSectors <= (1<<17)) /* 64M -> 4k */
340 		bpb.bpbSecPerClust = 8;
341 	    else if (bpb.bpbHugeSectors <= (1<<19)) /* 256M -> 8k */
342 		bpb.bpbSecPerClust = 16;
343 	    else if (bpb.bpbHugeSectors <= (1<<21)) /* 1G -> 16k */
344 		bpb.bpbSecPerClust = 32;
345 	    else
346 		bpb.bpbSecPerClust = 64;		/* otherwise 32k */
347 	}
348     }
349     if (bpb.bpbBytesPerSec < MINBPS ||
350         bpb.bpbBytesPerSec > MAXBPS ||
351 	!powerof2(bpb.bpbBytesPerSec)) {
352 	warnx("Invalid bytes/sector (%u): must be 512, 1024, 2048 or 4096",
353 	    bpb.bpbBytesPerSec);
354 	goto done;
355     }
356 
357     if (o.volume_label && !oklabel(o.volume_label)) {
358 	warnx("%s: bad volume label", o.volume_label);
359 	goto done;
360     }
361     if (!(fat = o.fat_type)) {
362 	if (o.floppy)
363 	    fat = 12;
364 	else if (!o.directory_entries && (o.info_sector || o.backup_sector))
365 	    fat = 32;
366     }
367     if ((fat == 32 && o.directory_entries) || (fat != 32 && (o.info_sector || o.backup_sector))) {
368 	warnx("-%c is not a legal FAT%s option",
369 	     fat == 32 ? 'e' : o.info_sector ? 'i' : 'k',
370 	     fat == 32 ? "32" : "12/16");
371 	goto done;
372     }
373     if (o.floppy && fat == 32)
374 	bpb.bpbRootDirEnts = 0;
375     if (fat != 0 && fat != 12 && fat != 16 && fat != 32) {
376 	warnx("%d: bad FAT type", fat);
377 	goto done;
378     }
379 
380     if (o.block_size) {
381 	if (!powerof2(o.block_size)) {
382 	    warnx("block size (%u) is not a power of 2", o.block_size);
383 	    goto done;
384 	}
385 	if (o.block_size < bpb.bpbBytesPerSec) {
386 	    warnx("block size (%u) is too small; minimum is %u",
387 		 o.block_size, bpb.bpbBytesPerSec);
388 	    goto done;
389 	}
390 	if (o.block_size > bpb.bpbBytesPerSec * MAXSPC) {
391 	    warnx("block size (%u) is too large; maximum is %u",
392 		 o.block_size, bpb.bpbBytesPerSec * MAXSPC);
393 	    goto done;
394 	}
395 	bpb.bpbSecPerClust = o.block_size / bpb.bpbBytesPerSec;
396     }
397     if (o.sectors_per_cluster) {
398 	if (!powerof2(o.sectors_per_cluster)) {
399 	    warnx("sectors/cluster (%u) is not a power of 2",
400 		o.sectors_per_cluster);
401 	    goto done;
402 	}
403 	bpb.bpbSecPerClust = o.sectors_per_cluster;
404     }
405     if (o.reserved_sectors)
406 	bpb.bpbResSectors = o.reserved_sectors;
407     if (o.num_FAT) {
408 	if (o.num_FAT > MAXNFT) {
409 	    warnx("number of FATs (%u) is too large; maximum is %u",
410 		 o.num_FAT, MAXNFT);
411 	    goto done;
412 	}
413 	bpb.bpbFATs = o.num_FAT;
414     }
415     if (o.directory_entries) {
416 	bpb.bpbRootDirEnts = roundup(o.directory_entries,
417 	    bpb.bpbBytesPerSec / sizeof(struct de));
418 	if (bpb.bpbBytesPerSec == 0 || o.directory_entries >= MAXU16)
419 	    bpb.bpbRootDirEnts = MAXU16;
420     }
421     if (o.media_descriptor_set) {
422 	if (o.media_descriptor < 0xf0) {
423 	    warnx("illegal media descriptor (%#x)", o.media_descriptor);
424 	    goto done;
425 	}
426 	bpb.bpbMedia = o.media_descriptor;
427     }
428     if (o.sectors_per_fat)
429 	bpb.bpbBigFATsecs = o.sectors_per_fat;
430     if (o.info_sector)
431 	bpb.bpbFSInfo = o.info_sector;
432     if (o.backup_sector)
433 	bpb.bpbBackup = o.backup_sector;
434     bss = 1;
435     bname = NULL;
436     fd1 = -1;
437     if (o.bootstrap) {
438 	bname = o.bootstrap;
439 	if (!strchr(bname, '/')) {
440 	    snprintf(buf, sizeof(buf), "/boot/%s", bname);
441 	    bname = buf;
442 	}
443 	if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb)) {
444 	    warn("%s", bname);
445 	    goto done;
446 	}
447 	if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bpbBytesPerSec ||
448 	    sb.st_size < bpb.bpbBytesPerSec ||
449 	    sb.st_size > bpb.bpbBytesPerSec * MAXU16) {
450 	    warnx("%s: inappropriate file type or format", bname);
451 	    goto done;
452 	}
453 	bss = sb.st_size / bpb.bpbBytesPerSec;
454     }
455     if (!bpb.bpbFATs)
456 	bpb.bpbFATs = 2;
457     if (!fat) {
458 	if (bpb.bpbHugeSectors < (bpb.bpbResSectors ? bpb.bpbResSectors : bss) +
459 	    howmany((RESFTE + (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1)) *
460 		(bpb.bpbSecPerClust ? 16 : 12) / BPN,
461 		bpb.bpbBytesPerSec * NPB) *
462 	    bpb.bpbFATs +
463 	    howmany(bpb.bpbRootDirEnts ? bpb.bpbRootDirEnts : DEFRDE,
464 		    bpb.bpbBytesPerSec / sizeof(struct de)) +
465 	    (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1) *
466 	    (bpb.bpbSecPerClust ? bpb.bpbSecPerClust :
467 	     howmany(DEFBLK, bpb.bpbBytesPerSec)))
468 	    fat = 12;
469 	else if (bpb.bpbRootDirEnts || bpb.bpbHugeSectors <
470 		 (bpb.bpbResSectors ? bpb.bpbResSectors : bss) +
471 		 howmany((RESFTE + MAXCLS16) * 2, bpb.bpbBytesPerSec) *
472 		 bpb.bpbFATs +
473 		 howmany(DEFRDE, bpb.bpbBytesPerSec / sizeof(struct de)) +
474 		 (MAXCLS16 + 1) *
475 		 (bpb.bpbSecPerClust ? bpb.bpbSecPerClust :
476 		  howmany(8192, bpb.bpbBytesPerSec)))
477 	    fat = 16;
478 	else
479 	    fat = 32;
480     }
481     x = bss;
482     if (fat == 32) {
483 	if (!bpb.bpbFSInfo) {
484 	    if (x == MAXU16 || x == bpb.bpbBackup) {
485 		warnx("no room for info sector");
486 		goto done;
487 	    }
488 	    bpb.bpbFSInfo = x;
489 	}
490 	if (bpb.bpbFSInfo != MAXU16 && x <= bpb.bpbFSInfo)
491 	    x = bpb.bpbFSInfo + 1;
492 	if (!bpb.bpbBackup) {
493 	    if (x == MAXU16) {
494 		warnx("no room for backup sector");
495 		goto done;
496 	    }
497 	    bpb.bpbBackup = x;
498 	} else if (bpb.bpbBackup != MAXU16 && bpb.bpbBackup == bpb.bpbFSInfo) {
499 	    warnx("backup sector would overwrite info sector");
500 	    goto done;
501 	}
502 	if (bpb.bpbBackup != MAXU16 && x <= bpb.bpbBackup)
503 	    x = bpb.bpbBackup + 1;
504     }
505 
506     extra_res = 0;
507     alignment = 0;
508     set_res = (bpb.bpbResSectors == 0);
509     set_spf = (bpb.bpbBigFATsecs == 0);
510     set_spc = (bpb.bpbSecPerClust == 0);
511     saved_x = x;
512 
513     /*
514      * Attempt to align the root directory to cluster if o.align is set.
515      * This is done by padding with reserved blocks. Note that this can
516      * cause other factors to change, which can in turn change the alignment.
517      * This should take at most 2 iterations, as increasing the reserved
518      * amount may cause the FAT size to decrease by 1, requiring another
519      * bpbFATs reserved blocks. If bpbSecPerClust changes, it will
520      * be half of its previous size, and thus will not throw off alignment.
521      */
522     do {
523 	x = saved_x;
524 	if (set_res)
525 	    bpb.bpbResSectors = ((fat == 32) ?
526 		MAX(x, MAX(16384 / bpb.bpbBytesPerSec, 4)) : x) + extra_res;
527 	else if (bpb.bpbResSectors < x) {
528 	    warnx("too few reserved sectors (need %d have %d)", x,
529 		bpb.bpbResSectors);
530 	    goto done;
531 	}
532 	if (fat != 32 && !bpb.bpbRootDirEnts)
533 	    bpb.bpbRootDirEnts = DEFRDE;
534 	rds = howmany(bpb.bpbRootDirEnts,
535 	    bpb.bpbBytesPerSec / sizeof(struct de));
536 	if (set_spc) {
537 	    for (bpb.bpbSecPerClust = howmany(fat == 16 ? DEFBLK16 :
538 		    DEFBLK, bpb.bpbBytesPerSec);
539 		bpb.bpbSecPerClust < MAXSPC && (bpb.bpbResSectors +
540 		    howmany((RESFTE + maxcls(fat)) * (fat / BPN),
541 			bpb.bpbBytesPerSec * NPB) * bpb.bpbFATs +
542 		    rds +
543 		    (u_int64_t) (maxcls(fat) + 1) * bpb.bpbSecPerClust) <=
544 		    bpb.bpbHugeSectors;
545 		bpb.bpbSecPerClust <<= 1)
546 		    continue;
547 
548 	}
549 	if (fat != 32 && bpb.bpbBigFATsecs > MAXU16) {
550 	    warnx("too many sectors/FAT for FAT12/16");
551 	    goto done;
552 	}
553 	x1 = bpb.bpbResSectors + rds;
554 	x = bpb.bpbBigFATsecs ? bpb.bpbBigFATsecs : 1;
555 	if (x1 + (u_int64_t)x * bpb.bpbFATs > bpb.bpbHugeSectors) {
556 	    warnx("meta data exceeds file system size");
557 	    goto done;
558 	}
559 	x1 += x * bpb.bpbFATs;
560 	x = (u_int64_t)(bpb.bpbHugeSectors - x1) * bpb.bpbBytesPerSec * NPB /
561 	    (bpb.bpbSecPerClust * bpb.bpbBytesPerSec * NPB +
562 	    fat / BPN * bpb.bpbFATs);
563 	x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN),
564 	    bpb.bpbBytesPerSec * NPB);
565 	if (set_spf) {
566 	    if (bpb.bpbBigFATsecs == 0)
567 		bpb.bpbBigFATsecs = x2;
568 	    x1 += (bpb.bpbBigFATsecs - 1) * bpb.bpbFATs;
569 	}
570 	if (set_res) {
571 	    alignto = bpb.bpbSecPerClust;
572 	    if (alignto > 1) {
573 		/* align data clusters */
574 		alignment = (bpb.bpbResSectors + bpb.bpbBigFATsecs * bpb.bpbFATs + rds) %
575 		    alignto;
576 		if (alignment != 0)
577 		    extra_res += alignto - alignment;
578 	    }
579 	}
580 	attempts++;
581     } while (alignment != 0 && attempts < 2);
582     if (o.align && alignment != 0)
583 	warnx("warning: Alignment failed.");
584 
585     cls = (bpb.bpbHugeSectors - x1) / bpb.bpbSecPerClust;
586     x = (u_int64_t)bpb.bpbBigFATsecs * bpb.bpbBytesPerSec * NPB / (fat / BPN) -
587 	RESFTE;
588     if (cls > x)
589 	cls = x;
590     if (bpb.bpbBigFATsecs < x2)
591 	warnx("warning: sectors/FAT limits file system to %u clusters",
592 	      cls);
593     if (cls < mincls(fat)) {
594 	warnx("%u clusters too few clusters for FAT%u, need %u", cls, fat,
595 	    mincls(fat));
596 	goto done;
597     }
598     if (cls > maxcls(fat)) {
599 	cls = maxcls(fat);
600 	bpb.bpbHugeSectors = x1 + (cls + 1) * bpb.bpbSecPerClust - 1;
601 	warnx("warning: FAT type limits file system to %u sectors",
602 	      bpb.bpbHugeSectors);
603     }
604     printf("%s: %u sector%s in %u FAT%u cluster%s "
605 	   "(%u bytes/cluster)\n", fname, cls * bpb.bpbSecPerClust,
606 	   cls * bpb.bpbSecPerClust == 1 ? "" : "s", cls, fat,
607 	   cls == 1 ? "" : "s", bpb.bpbBytesPerSec * bpb.bpbSecPerClust);
608     if (!bpb.bpbMedia)
609 	bpb.bpbMedia = !bpb.bpbHiddenSecs ? 0xf0 : 0xf8;
610     if (fat == 32)
611 	bpb.bpbRootClust = RESFTE;
612     if (bpb.bpbHugeSectors <= MAXU16) {
613 	bpb.bpbSectors = bpb.bpbHugeSectors;
614 	bpb.bpbHugeSectors = 0;
615     }
616     if (fat != 32) {
617 	bpb.bpbFATsecs = bpb.bpbBigFATsecs;
618 	bpb.bpbBigFATsecs = 0;
619     }
620     print_bpb(&bpb);
621     if (!o.no_create) {
622 	if (o.timestamp_set) {
623 	    tv.tv_sec = now = o.timestamp;
624 	    tv.tv_usec = 0;
625 	    tm = gmtime(&now);
626 	} else {
627 	    gettimeofday(&tv, NULL);
628 	    now = tv.tv_sec;
629 	    tm = localtime(&now);
630 	}
631 
632 	chunksize = getchunksize();
633 	physbuf = malloc(chunksize);
634 	if (physbuf == NULL) {
635 	    warn(NULL);
636 	    goto done;
637 	}
638 	physbuf_end = physbuf + chunksize;
639 	img = physbuf;
640 
641 	dir = bpb.bpbResSectors + (bpb.bpbFATsecs ? bpb.bpbFATsecs :
642 				   bpb.bpbBigFATsecs) * bpb.bpbFATs;
643 	memset(&si_sa, 0, sizeof(si_sa));
644 	si_sa.sa_handler = infohandler;
645 #ifdef SIGINFO
646 	if (sigaction(SIGINFO, &si_sa, NULL) == -1) {
647 	    warn("sigaction SIGINFO");
648 	    goto done;
649 	}
650 #endif
651 	for (lsn = 0; lsn < dir + (fat == 32 ? bpb.bpbSecPerClust : rds); lsn++) {
652 	    if (got_siginfo) {
653 		    fprintf(stderr,"%s: writing sector %u of %u (%u%%)\n",
654 			fname, lsn,
655 			(dir + (fat == 32 ? bpb.bpbSecPerClust: rds)),
656 			(lsn * 100) / (dir +
657 			    (fat == 32 ? bpb.bpbSecPerClust: rds)));
658 		    got_siginfo = 0;
659 	    }
660 	    x = lsn;
661 	    if (o.bootstrap &&
662 		fat == 32 && bpb.bpbBackup != MAXU16 &&
663 		bss <= bpb.bpbBackup && x >= bpb.bpbBackup) {
664 		x -= bpb.bpbBackup;
665 		if (!x && lseek(fd1, o.offset, SEEK_SET)) {
666 		    warn("%s", bname);
667 		    goto done;
668 		}
669 	    }
670 	    if (o.bootstrap && x < bss) {
671 		if ((n = read(fd1, img, bpb.bpbBytesPerSec)) == -1) {
672 		    warn("%s", bname);
673 		    goto done;
674 		}
675 		if ((unsigned)n != bpb.bpbBytesPerSec) {
676 		    warnx("%s: can't read sector %u", bname, x);
677 		    goto done;
678 		}
679 	    } else
680 		memset(img, 0, bpb.bpbBytesPerSec);
681 	    if (!lsn ||
682 		(fat == 32 && bpb.bpbBackup != MAXU16 &&
683 		 lsn == bpb.bpbBackup)) {
684 		x1 = sizeof(struct bs);
685 		bsbpb = (struct bsbpb *)(img + x1);
686 		mk2(bsbpb->bpbBytesPerSec, bpb.bpbBytesPerSec);
687 		mk1(bsbpb->bpbSecPerClust, bpb.bpbSecPerClust);
688 		mk2(bsbpb->bpbResSectors, bpb.bpbResSectors);
689 		mk1(bsbpb->bpbFATs, bpb.bpbFATs);
690 		mk2(bsbpb->bpbRootDirEnts, bpb.bpbRootDirEnts);
691 		mk2(bsbpb->bpbSectors, bpb.bpbSectors);
692 		mk1(bsbpb->bpbMedia, bpb.bpbMedia);
693 		mk2(bsbpb->bpbFATsecs, bpb.bpbFATsecs);
694 		mk2(bsbpb->bpbSecPerTrack, bpb.bpbSecPerTrack);
695 		mk2(bsbpb->bpbHeads, bpb.bpbHeads);
696 		mk4(bsbpb->bpbHiddenSecs, bpb.bpbHiddenSecs);
697 		mk4(bsbpb->bpbHugeSectors, bpb.bpbHugeSectors);
698 		x1 += sizeof(struct bsbpb);
699 		if (fat == 32) {
700 		    bsxbpb = (struct bsxbpb *)(img + x1);
701 		    mk4(bsxbpb->bpbBigFATsecs, bpb.bpbBigFATsecs);
702 		    mk2(bsxbpb->bpbExtFlags, 0);
703 		    mk2(bsxbpb->bpbFSVers, 0);
704 		    mk4(bsxbpb->bpbRootClust, bpb.bpbRootClust);
705 		    mk2(bsxbpb->bpbFSInfo, bpb.bpbFSInfo);
706 		    mk2(bsxbpb->bpbBackup, bpb.bpbBackup);
707 		    x1 += sizeof(struct bsxbpb);
708 		}
709 		bsx = (struct bsx *)(img + x1);
710 		mk1(bsx->exBootSignature, 0x29);
711 		if (o.volume_id_set)
712 		    x = o.volume_id;
713 		else
714 		    x = (((u_int)(1 + tm->tm_mon) << 8 |
715 			  (u_int)tm->tm_mday) +
716 			 ((u_int)tm->tm_sec << 8 |
717 			  (u_int)(tv.tv_usec / 10))) << 16 |
718 			((u_int)(1900 + tm->tm_year) +
719 			 ((u_int)tm->tm_hour << 8 |
720 			  (u_int)tm->tm_min));
721 		mk4(bsx->exVolumeID, x);
722 		mklabel(bsx->exVolumeLabel, o.volume_label ? o.volume_label : "NO NAME");
723 		snprintf(buf, sizeof(buf), "FAT%u", fat);
724 		setstr(bsx->exFileSysType, buf, sizeof(bsx->exFileSysType));
725 		if (!o.bootstrap) {
726 		    x1 += sizeof(struct bsx);
727 		    bs = (struct bs *)img;
728 		    mk1(bs->bsJump[0], 0xeb);
729 		    mk1(bs->bsJump[1], x1 - 2);
730 		    mk1(bs->bsJump[2], 0x90);
731 		    setstr(bs->bsOemName, o.OEM_string ? o.OEM_string : "BSD4.4  ",
732 			   sizeof(bs->bsOemName));
733 		    memcpy(img + x1, bootcode, sizeof(bootcode));
734 		    mk2(img + MINBPS - 2, DOSMAGIC);
735 		}
736 	    } else if (fat == 32 && bpb.bpbFSInfo != MAXU16 &&
737 		       (lsn == bpb.bpbFSInfo ||
738 			(bpb.bpbBackup != MAXU16 &&
739 			 lsn == bpb.bpbBackup + bpb.bpbFSInfo))) {
740 		mk4(img, 0x41615252);
741 		mk4(img + MINBPS - 28, 0x61417272);
742 		mk4(img + MINBPS - 24, 0xffffffff);
743 		mk4(img + MINBPS - 20, 0xffffffff);
744 		mk2(img + MINBPS - 2, DOSMAGIC);
745 	    } else if (lsn >= bpb.bpbResSectors && lsn < dir &&
746 		       !((lsn - bpb.bpbResSectors) %
747 			 (bpb.bpbFATsecs ? bpb.bpbFATsecs :
748 			  bpb.bpbBigFATsecs))) {
749 		mk1(img[0], bpb.bpbMedia);
750 		for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
751 		    mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
752 	    } else if (lsn == dir && o.volume_label) {
753 		de = (struct de *)img;
754 		mklabel(de->deName, o.volume_label);
755 		mk1(de->deAttributes, 050);
756 		x = (u_int)tm->tm_hour << 11 |
757 		    (u_int)tm->tm_min << 5 |
758 		    (u_int)tm->tm_sec >> 1;
759 		mk2(de->deMTime, x);
760 		x = (u_int)(tm->tm_year - 80) << 9 |
761 		    (u_int)(tm->tm_mon + 1) << 5 |
762 		    (u_int)tm->tm_mday;
763 		mk2(de->deMDate, x);
764 	    }
765 	    /*
766 	     * Issue a write of chunksize once we have collected
767 	     * enough sectors.
768 	     */
769 	    img += bpb.bpbBytesPerSec;
770 	    if (img >= physbuf_end) {
771 		n = write(fd, physbuf, chunksize);
772 		if (n != chunksize) {
773 		    warnx("%s: can't write sector %u", fname, lsn);
774 		    goto done;
775 		}
776 		img = physbuf;
777 	    }
778 	}
779 	/*
780 	 * Write remaining sectors, if the last write didn't end
781 	 * up filling a whole chunk.
782 	 */
783 	if (img != physbuf) {
784 		ssize_t tailsize = img - physbuf;
785 
786 		n = write(fd, physbuf, tailsize);
787 		if (n != tailsize) {
788 		    warnx("%s: can't write sector %u", fname, lsn);
789 		    goto done;
790 		}
791 	}
792     }
793     rv = 0;
794 done:
795     free(physbuf);
796     if (fd != -1)
797 	    close(fd);
798     if (fd1 != -1)
799 	    close(fd1);
800 
801     return rv;
802 }
803 
804 /*
805  * return -1 with error if file system is mounted.
806  */
807 #ifndef MAKEFS
808 static int
check_mounted(const char * fname,mode_t mode)809 check_mounted(const char *fname, mode_t mode)
810 {
811 /*
812  * If getmntinfo() is not available (e.g. Linux) don't check. This should
813  * not be a problem since we will only be using makefs to create images.
814  */
815     struct statfs *mp;
816     const char *s1, *s2;
817     size_t len;
818     int n, r;
819 
820     if (!(n = getmntinfo(&mp, MNT_NOWAIT))) {
821 	warn("getmntinfo");
822 	return -1;
823     }
824     len = strlen(_PATH_DEV);
825     s1 = fname;
826     if (!strncmp(s1, _PATH_DEV, len))
827 	s1 += len;
828     r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
829     for (; n--; mp++) {
830 	s2 = mp->f_mntfromname;
831 	if (!strncmp(s2, _PATH_DEV, len))
832 	    s2 += len;
833 	if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
834 	    !strcmp(s1, s2)) {
835 	    warnx("%s is mounted on %s", fname, mp->f_mntonname);
836 	    return -1;
837 	}
838     }
839     return 0;
840 }
841 #endif
842 
843 /*
844  * Get optimal I/O size
845  */
846 static ssize_t
getchunksize(void)847 getchunksize(void)
848 {
849 	static ssize_t chunksize;
850 
851 	if (chunksize != 0)
852 		return (chunksize);
853 
854 #ifdef	KERN_MAXPHYS
855 	int mib[2];
856 	size_t len;
857 
858 	mib[0] = CTL_KERN;
859 	mib[1] = KERN_MAXPHYS;
860 	len = sizeof(chunksize);
861 
862 	if (sysctl(mib, 2, &chunksize, &len, NULL, 0) == -1) {
863 		warn("sysctl: KERN_MAXPHYS, using %zu", (size_t)MAXPHYS);
864 		chunksize = 0;
865 	}
866 #endif
867 	if (chunksize == 0)
868 		chunksize = MAXPHYS;
869 
870 	/*
871 	 * For better performance, we want to write larger chunks instead of
872 	 * individual sectors (the size can only be 512, 1024, 2048 or 4096
873 	 * bytes). Assert that chunksize can always hold an integer number of
874 	 * sectors by asserting that both are power of two numbers and the
875 	 * chunksize is greater than MAXBPS.
876 	 */
877 	static_assert(powerof2(MAXBPS), "MAXBPS is not power of 2");
878 	assert(powerof2(chunksize));
879 	assert(chunksize > MAXBPS);
880 
881 	return (chunksize);
882 }
883 
884 /*
885  * Get a standard format.
886  */
887 static int
getstdfmt(const char * fmt,struct bpb * bpb)888 getstdfmt(const char *fmt, struct bpb *bpb)
889 {
890     u_int x, i;
891 
892     x = nitems(stdfmt);
893     for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
894     if (i == x) {
895 	warnx("%s: unknown standard format", fmt);
896 	return -1;
897     }
898     *bpb = stdfmt[i].bpb;
899     return 0;
900 }
901 
902 static void
compute_geometry_from_file(int fd,const char * fname,struct disklabel * lp)903 compute_geometry_from_file(int fd, const char *fname, struct disklabel *lp)
904 {
905 	struct stat st;
906 	off_t ms;
907 
908 	if (fstat(fd, &st))
909 		err(1, "cannot get disk size");
910 	if (!S_ISREG(st.st_mode))
911 		errx(1, "%s is not a regular file", fname);
912 	ms = st.st_size;
913 	lp->d_secsize = 512;
914 	lp->d_nsectors = 63;
915 	lp->d_ntracks = 255;
916 	lp->d_secperunit = ms / lp->d_secsize;
917 }
918 
919 /*
920  * Get disk slice, partition, and geometry information.
921  */
922 static int
getdiskinfo(int fd,const char * fname,const char * dtype,__unused int oflag,struct bpb * bpb)923 getdiskinfo(int fd, const char *fname, const char *dtype, __unused int oflag,
924 	    struct bpb *bpb)
925 {
926     struct disklabel *lp, dlp;
927     off_t hs = 0;
928 #ifndef MAKEFS
929     off_t ms;
930     struct fd_type type;
931 
932     lp = NULL;
933 
934     /* If the user specified a disk type, try to use that */
935     if (dtype != NULL) {
936 	lp = getdiskbyname(dtype);
937     }
938 
939     /* Maybe it's a floppy drive */
940     if (lp == NULL) {
941 	if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1) {
942 	    /* create a fake geometry for a file image */
943 	    compute_geometry_from_file(fd, fname, &dlp);
944 	    lp = &dlp;
945 	} else if (ioctl(fd, FD_GTYPE, &type) != -1) {
946 	    dlp.d_secsize = 128 << type.secsize;
947 	    dlp.d_nsectors = type.sectrac;
948 	    dlp.d_ntracks = type.heads;
949 	    dlp.d_secperunit = ms / dlp.d_secsize;
950 	    lp = &dlp;
951 	}
952     }
953 
954     /* Maybe it's a fixed drive */
955     if (lp == NULL) {
956 	if (bpb->bpbBytesPerSec)
957 	    dlp.d_secsize = bpb->bpbBytesPerSec;
958 	if (bpb->bpbBytesPerSec == 0 && ioctl(fd, DIOCGSECTORSIZE,
959 					      &dlp.d_secsize) == -1)
960 	    err(1, "cannot get sector size");
961 
962 	dlp.d_secperunit = ms / dlp.d_secsize;
963 
964 	if (bpb->bpbSecPerTrack == 0 && ioctl(fd, DIOCGFWSECTORS,
965 					      &dlp.d_nsectors) == -1) {
966 	    warn("cannot get number of sectors per track");
967 	    dlp.d_nsectors = 63;
968 	}
969 	if (bpb->bpbHeads == 0 &&
970 	    ioctl(fd, DIOCGFWHEADS, &dlp.d_ntracks) == -1) {
971 	    warn("cannot get number of heads");
972 	    if (dlp.d_secperunit <= 63*1*1024)
973 		dlp.d_ntracks = 1;
974 	    else if (dlp.d_secperunit <= 63*16*1024)
975 		dlp.d_ntracks = 16;
976 	    else
977 		dlp.d_ntracks = 255;
978 	}
979 
980 	hs = (ms / dlp.d_secsize) - dlp.d_secperunit;
981 	lp = &dlp;
982     }
983 #else
984     (void)dtype;
985     /* In the makefs case we only support image files: */
986     compute_geometry_from_file(fd, fname, &dlp);
987     lp = &dlp;
988 #endif
989 
990     if (bpb->bpbBytesPerSec == 0) {
991 	if (ckgeom(fname, lp->d_secsize, "bytes/sector") == -1)
992 	    return -1;
993 	bpb->bpbBytesPerSec = lp->d_secsize;
994     }
995     if (bpb->bpbSecPerTrack == 0) {
996 	if (ckgeom(fname, lp->d_nsectors, "sectors/track") == -1)
997 	    return -1;
998 	bpb->bpbSecPerTrack = lp->d_nsectors;
999     }
1000     if (bpb->bpbHeads == 0) {
1001 	if (ckgeom(fname, lp->d_ntracks, "drive heads") == -1)
1002 	    return -1;
1003 	bpb->bpbHeads = lp->d_ntracks;
1004     }
1005     if (bpb->bpbHugeSectors == 0)
1006 	bpb->bpbHugeSectors = lp->d_secperunit;
1007     if (bpb->bpbHiddenSecs == 0)
1008 	bpb->bpbHiddenSecs = hs;
1009     return 0;
1010 }
1011 
1012 /*
1013  * Print out BPB values.
1014  */
1015 static void
print_bpb(struct bpb * bpb)1016 print_bpb(struct bpb *bpb)
1017 {
1018     printf("BytesPerSec=%u SecPerClust=%u ResSectors=%u FATs=%u",
1019 	   bpb->bpbBytesPerSec, bpb->bpbSecPerClust, bpb->bpbResSectors,
1020 	   bpb->bpbFATs);
1021     if (bpb->bpbRootDirEnts)
1022 	printf(" RootDirEnts=%u", bpb->bpbRootDirEnts);
1023     if (bpb->bpbSectors)
1024 	printf(" Sectors=%u", bpb->bpbSectors);
1025     printf(" Media=%#x", bpb->bpbMedia);
1026     if (bpb->bpbFATsecs)
1027 	printf(" FATsecs=%u", bpb->bpbFATsecs);
1028     printf(" SecPerTrack=%u Heads=%u HiddenSecs=%u", bpb->bpbSecPerTrack,
1029 	   bpb->bpbHeads, bpb->bpbHiddenSecs);
1030     if (bpb->bpbHugeSectors)
1031 	printf(" HugeSectors=%u", bpb->bpbHugeSectors);
1032     if (!bpb->bpbFATsecs) {
1033 	printf(" FATsecs=%u RootCluster=%u", bpb->bpbBigFATsecs,
1034 	       bpb->bpbRootClust);
1035 	printf(" FSInfo=");
1036 	printf(bpb->bpbFSInfo == MAXU16 ? "%#x" : "%u", bpb->bpbFSInfo);
1037 	printf(" Backup=");
1038 	printf(bpb->bpbBackup == MAXU16 ? "%#x" : "%u", bpb->bpbBackup);
1039     }
1040     printf("\n");
1041 }
1042 
1043 /*
1044  * Check a disk geometry value.
1045  */
1046 static int
ckgeom(const char * fname,u_int val,const char * msg)1047 ckgeom(const char *fname, u_int val, const char *msg)
1048 {
1049     if (!val) {
1050 	warnx("%s: no default %s", fname, msg);
1051 	return -1;
1052     }
1053     if (val > MAXU16) {
1054 	warnx("%s: illegal %s %d", fname, msg, val);
1055 	return -1;
1056     }
1057     return 0;
1058 }
1059 
1060 /*
1061  * Check a volume label.
1062  */
1063 static int
oklabel(const char * src)1064 oklabel(const char *src)
1065 {
1066     int c, i;
1067 
1068     for (i = 0; i <= 11; i++) {
1069 	c = (u_char)*src++;
1070 	if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
1071 	    break;
1072     }
1073     return i && !c;
1074 }
1075 
1076 /*
1077  * Make a volume label.
1078  */
1079 static void
mklabel(u_int8_t * dest,const char * src)1080 mklabel(u_int8_t *dest, const char *src)
1081 {
1082     int c, i;
1083 
1084     for (i = 0; i < 11; i++) {
1085 	c = *src ? toupper(*src++) : ' ';
1086 	*dest++ = !i && c == '\xe5' ? 5 : c;
1087     }
1088 }
1089 
1090 /*
1091  * Copy string, padding with spaces.
1092  */
1093 static void
setstr(u_int8_t * dest,const char * src,size_t len)1094 setstr(u_int8_t *dest, const char *src, size_t len)
1095 {
1096     while (len--)
1097 	*dest++ = *src ? *src++ : ' ';
1098 }
1099 
1100 static void
infohandler(int sig __unused)1101 infohandler(int sig __unused)
1102 {
1103 
1104 	got_siginfo = 1;
1105 }
1106