GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/makefs/msdos/mkfs_msdos.c Lines: 0 432 0.0 %
Date: 2017-11-13 Branches: 0 406 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: mkfs_msdos.c,v 1.5 2017/11/02 11:06:54 jca Exp $	*/
2
/*	$NetBSD: mkfs_msdos.c,v 1.10 2016/04/03 11:00:13 mlelstv Exp $	*/
3
4
/*
5
 * Copyright (c) 1998 Robert Nordier
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in
15
 *    the documentation and/or other materials provided with the
16
 *    distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
19
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
26
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
28
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 */
30
31
#include <sys/param.h>
32
#include <sys/stat.h>
33
#include <sys/time.h>
34
35
#include <ctype.h>
36
#include <err.h>
37
#include <errno.h>
38
#include <fcntl.h>
39
#include <paths.h>
40
#include <stdio.h>
41
#include <stdint.h>
42
#include <stdlib.h>
43
#include <string.h>
44
#include <time.h>
45
#include <unistd.h>
46
#include <signal.h>
47
48
#include <util.h>
49
#include <disktab.h>
50
51
#include "makefs.h"
52
#include "msdos/mkfs_msdos.h"
53
54
#define MAXU16	  0xffff	/* maximum unsigned 16-bit quantity */
55
#define BPN	  4		/* bits per nibble */
56
#define NPB	  2		/* nibbles per byte */
57
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  1		/* minimum FAT12 clusters */
67
#define MINCLS16  0xff5		/* minimum FAT16 clusters */
68
#define MINCLS32  0xfff5	/* minimum FAT32 clusters */
69
#define MAXCLS12  0xff4		/* maximum FAT12 clusters */
70
#define MAXCLS16  0xfff4	/* maximum FAT16 clusters */
71
#define MAXCLS32  0xffffff4	/* maximum FAT32 clusters */
72
73
#define mincls(fat_type)  ((fat_type) == 12 ? MINCLS12 :	\
74
		      (fat_type) == 16 ? MINCLS16 :	\
75
				    MINCLS32)
76
77
#define maxcls(fat_type)  ((fat_type) == 12 ? MAXCLS12 :	\
78
		      (fat_type) == 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
struct bs {
95
    u_int8_t jmp[3];		/* bootstrap entry point */
96
    u_int8_t oem[8];		/* OEM name and version */
97
};
98
99
struct bsbpb {
100
    u_int8_t bps[2];		/* bytes per sector */
101
    u_int8_t spc;		/* sectors per cluster */
102
    u_int8_t res[2];		/* reserved sectors */
103
    u_int8_t nft;		/* number of FATs */
104
    u_int8_t rde[2];		/* root directory entries */
105
    u_int8_t sec[2];		/* total sectors */
106
    u_int8_t mid;		/* media descriptor */
107
    u_int8_t spf[2];		/* sectors per FAT */
108
    u_int8_t spt[2];		/* sectors per track */
109
    u_int8_t hds[2];		/* drive heads */
110
    u_int8_t hid[4];		/* hidden sectors */
111
    u_int8_t bsec[4];		/* big total sectors */
112
};
113
114
struct bsxbpb {
115
    u_int8_t bspf[4];		/* big sectors per FAT */
116
    u_int8_t xflg[2];		/* FAT control flags */
117
    u_int8_t vers[2];		/* file system version */
118
    u_int8_t rdcl[4];		/* root directory start cluster */
119
    u_int8_t infs[2];		/* file system info sector */
120
    u_int8_t bkbs[2];		/* backup boot sector */
121
    u_int8_t rsvd[12];		/* reserved */
122
};
123
124
struct bsx {
125
    u_int8_t drv;		/* drive number */
126
    u_int8_t rsvd;		/* reserved */
127
    u_int8_t sig;		/* extended boot signature */
128
    u_int8_t volid[4];		/* volume ID number */
129
    u_int8_t label[11];		/* volume label */
130
    u_int8_t type[8];		/* file system type */
131
};
132
133
struct de {
134
    u_int8_t namext[11];	/* name and extension */
135
    u_int8_t attr;		/* attributes */
136
    u_int8_t rsvd[10];		/* reserved */
137
    u_int8_t time[2];		/* creation time */
138
    u_int8_t date[2];		/* creation date */
139
    u_int8_t clus[2];		/* starting cluster */
140
    u_int8_t size[4];		/* size */
141
};
142
143
struct bpb {
144
    u_int bps;			/* bytes per sector */
145
    u_int spc;			/* sectors per cluster */
146
    u_int res;			/* reserved sectors */
147
    u_int nft;			/* number of FATs */
148
    u_int rde;			/* root directory entries */
149
    u_int sec;			/* total sectors */
150
    u_int mid;			/* media descriptor */
151
    u_int spf;			/* sectors per FAT */
152
    u_int spt;			/* sectors per track */
153
    u_int hds;			/* drive heads */
154
    u_int hid;			/* hidden sectors */
155
    u_int bsec;			/* big total sectors */
156
    u_int bspf;			/* big sectors per FAT */
157
    u_int rdcl;			/* root directory start cluster */
158
    u_int infs;			/* file system info sector */
159
    u_int bkbs;			/* backup boot sector */
160
};
161
162
#define INIT(a, b, c, d, e, f, g, h, i, j) \
163
    { .bps = a, .spc = b, .res = c, .nft = d, .rde = e, \
164
      .sec = f, .mid = g, .spf = h, .spt = i, .hds = j, }
165
static struct {
166
    const char *name;
167
    struct bpb bpb;
168
} stdfmt[] = {
169
    {"160",  INIT(512, 1, 1, 2,  64,  320, 0xfe, 1,  8, 1)},
170
    {"180",  INIT(512, 1, 1, 2,  64,  360, 0xfc, 2,  9, 1)},
171
    {"320",  INIT(512, 2, 1, 2, 112,  640, 0xff, 1,  8, 2)},
172
    {"360",  INIT(512, 2, 1, 2, 112,  720, 0xfd, 2,  9, 2)},
173
    {"640",  INIT(512, 2, 1, 2, 112, 1280, 0xfb, 2,  8, 2)},
174
    {"720",  INIT(512, 2, 1, 2, 112, 1440, 0xf9, 3,  9, 2)},
175
    {"1200", INIT(512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2)},
176
    {"1232", INIT(1024,1, 1, 2, 192, 1232, 0xfe, 2,  8, 2)},
177
    {"1440", INIT(512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2)},
178
    {"2880", INIT(512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2)}
179
};
180
181
static u_int8_t bootcode[] = {
182
    0xfa,			/* cli		    */
183
    0x31, 0xc0,			/* xor	   ax,ax    */
184
    0x8e, 0xd0,			/* mov	   ss,ax    */
185
    0xbc, 0x00, 0x7c,		/* mov	   sp,7c00h */
186
    0xfb,			/* sti		    */
187
    0x8e, 0xd8,			/* mov	   ds,ax    */
188
    0xe8, 0x00, 0x00,		/* call    $ + 3    */
189
    0x5e,			/* pop	   si	    */
190
    0x83, 0xc6, 0x19,		/* add	   si,+19h  */
191
    0xbb, 0x07, 0x00,		/* mov	   bx,0007h */
192
    0xfc,			/* cld		    */
193
    0xac,			/* lodsb	    */
194
    0x84, 0xc0,			/* test    al,al    */
195
    0x74, 0x06,			/* jz	   $ + 8    */
196
    0xb4, 0x0e,			/* mov	   ah,0eh   */
197
    0xcd, 0x10,			/* int	   10h	    */
198
    0xeb, 0xf5,			/* jmp	   $ - 9    */
199
    0x30, 0xe4,			/* xor	   ah,ah    */
200
    0xcd, 0x16,			/* int	   16h	    */
201
    0xcd, 0x19,			/* int	   19h	    */
202
    0x0d, 0x0a,
203
    'N', 'o', 'n', '-', 's', 'y', 's', 't',
204
    'e', 'm', ' ', 'd', 'i', 's', 'k',
205
    0x0d, 0x0a,
206
    'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
207
    'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
208
    ' ', 'r', 'e', 'b', 'o', 'o', 't',
209
    0x0d, 0x0a,
210
    0
211
};
212
213
static int got_siginfo = 0; /* received a SIGINFO */
214
215
static int getstdfmt(const char *, struct bpb *);
216
static int getbpbinfo(int, const char *, const char *, int, struct bpb *, int);
217
static void print_bpb(struct bpb *);
218
static int ckgeom(const char *, u_int, const char *);
219
static int oklabel(const char *);
220
static void mklabel(u_int8_t *, const char *);
221
static void setstr(u_int8_t *, const char *, size_t);
222
static void infohandler(int sig);
223
224
int
225
mkfs_msdos(const char *fname, const char *dtype, const struct msdos_options *op)
226
{
227
    char buf[MAXPATHLEN];
228
    struct stat sb;
229
    struct timeval tv;
230
    struct bpb bpb;
231
    struct tm *tm;
232
    struct bs *bs;
233
    struct bsbpb *bsbpb;
234
    struct bsxbpb *bsxbpb;
235
    struct bsx *bsx;
236
    struct de *de;
237
    u_int8_t *img;
238
    const char *bname;
239
    ssize_t n;
240
    time_t now;
241
    u_int bss, rds, cls, dir, lsn, x, x1, x2;
242
    int ch, fd, fd1;
243
    struct msdos_options o = *op;
244
    int oflags = O_RDWR | O_CREAT;
245
246
    if (o.block_size && o.sectors_per_cluster) {
247
	warnx("Cannot specify both block size and sectors per cluster");
248
	return -1;
249
    }
250
    if (o.OEM_string && strlen(o.OEM_string) > 8) {
251
	warnx("%s: bad OEM string", o.OEM_string);
252
	return -1;
253
    }
254
    if (o.create_size) {
255
	if (o.offset == 0)
256
		oflags |= O_TRUNC;
257
	fd = open(fname, oflags, 0644);
258
	if (fd == -1) {
259
	    warnx("failed to create %s", fname);
260
	    return -1;
261
	}
262
	(void)lseek(fd, o.create_size - 1, SEEK_SET);
263
	if (write(fd, "\0", 1) != 1) {
264
	    warn("failed to set file size");
265
	    return -1;
266
	}
267
	(void)lseek(fd, 0, SEEK_SET);
268
    } else if ((fd = open(fname, O_RDWR)) == -1 ||
269
	fstat(fd, &sb)) {
270
	warn("%s", fname);
271
	return -1;
272
    }
273
    if (!S_ISCHR(sb.st_mode) && !o.create_size) {
274
	warnx("warning, %s is not a character device", fname);
275
	return -1;
276
    }
277
    if (o.offset && o.offset != lseek(fd, o.offset, SEEK_SET)) {
278
	warnx("cannot seek to %jd", (intmax_t)o.offset);
279
	return -1;
280
    }
281
    memset(&bpb, 0, sizeof(bpb));
282
    if (o.floppy) {
283
	if (getstdfmt(o.floppy, &bpb) == -1)
284
	    return -1;
285
	bpb.bsec = bpb.sec;
286
	bpb.sec = 0;
287
	bpb.bspf = bpb.spf;
288
	bpb.spf = 0;
289
    }
290
    if (o.drive_heads)
291
	bpb.hds = o.drive_heads;
292
    if (o.sectors_per_track)
293
	bpb.spt = o.sectors_per_track;
294
    if (o.bytes_per_sector)
295
	bpb.bps = o.bytes_per_sector;
296
    if (o.size)
297
	bpb.bsec = o.size;
298
    if (o.hidden_sectors_set)
299
	bpb.hid = o.hidden_sectors;
300
    if (!(o.floppy || (o.drive_heads && o.sectors_per_track &&
301
	o.bytes_per_sector && o.size && o.hidden_sectors_set))) {
302
	if (getbpbinfo(fd, fname, dtype, o.hidden_sectors_set, &bpb,
303
	    o.create_size != 0) == -1)
304
	    return -1;
305
	bpb.bsec -= (o.offset / bpb.bps);
306
	if (bpb.spc == 0) {     /* set defaults */
307
	    /* minimum cluster size */
308
	    switch (o.fat_type) {
309
	    case 12:
310
		bpb.spc = 1;            /* use 512 bytes */
311
		x = 2;                  /* up to 2MB */
312
		break;
313
	    case 16:
314
		bpb.spc = 1;            /* use 512 bytes */
315
		x = 32;                 /* up to 32MB */
316
		break;
317
	    default:
318
		bpb.spc = 8;            /* use 4k */
319
		x = 8192;               /* up to 8GB */
320
		break;
321
	    }
322
	    x1 = howmany(bpb.bsec, (1048576 / 512)); /* -> MB */
323
	    while (bpb.spc < 128 && x < x1) {
324
		x *= 2;
325
		bpb.spc *= 2;
326
	    }
327
	}
328
    }
329
330
    if (o.volume_label && !oklabel(o.volume_label)) {
331
	warnx("%s: bad volume label", o.volume_label);
332
	return -1;
333
    }
334
335
    switch (o.fat_type) {
336
    case 0:
337
	if (o.floppy)
338
	    o.fat_type = 12;
339
	else if (!o.directory_entries && (o.info_sector || o.backup_sector))
340
	    o.fat_type = 32;
341
	break;
342
    case 12:
343
    case 16:
344
	if (o.info_sector) {
345
	    warnx("Cannot specify info sector with FAT%u", o.fat_type);
346
	    return -1;
347
	}
348
	if (o.backup_sector) {
349
	    warnx("Cannot specify backup sector with FAT%u", o.fat_type);
350
	    return -1;
351
	}
352
	break;
353
    case 32:
354
	if (o.directory_entries) {
355
	    warnx("Cannot specify directory entries with FAT32");
356
	    return -1;
357
	}
358
	break;
359
    default:
360
	warnx("%d: bad FAT type", o.fat_type);
361
	return -1;
362
    }
363
    if (!powerof2(bpb.bps)) {
364
	warnx("bytes/sector (%u) is not a power of 2", bpb.bps);
365
	return -1;
366
    }
367
    if (bpb.bps < MINBPS) {
368
	warnx("bytes/sector (%u) is too small; minimum is %u",
369
	     bpb.bps, MINBPS);
370
	return -1;
371
    }
372
373
    if (o.floppy && o.fat_type == 32)
374
	bpb.rde = 0;
375
    if (o.block_size) {
376
	if (!powerof2(o.block_size)) {
377
	    warnx("block size (%u) is not a power of 2", o.block_size);
378
	    return -1;
379
	}
380
	if (o.block_size < bpb.bps) {
381
	    warnx("block size (%u) is too small; minimum is %u",
382
		o.block_size, bpb.bps);
383
	    return -1;
384
	}
385
	if (o.block_size > bpb.bps * MAXSPC) {
386
	    warnx("block size (%u) is too large; maximum is %u",
387
		o.block_size, bpb.bps * MAXSPC);
388
	    return -1;
389
	}
390
	bpb.spc = o.block_size / bpb.bps;
391
    }
392
    if (o.sectors_per_cluster) {
393
	if (!powerof2(o.sectors_per_cluster)) {
394
	    warnx("sectors/cluster (%u) is not a power of 2",
395
		o.sectors_per_cluster);
396
	    return -1;
397
	}
398
	bpb.spc = o.sectors_per_cluster;
399
    }
400
    if (o.reserved_sectors)
401
	bpb.res = o.reserved_sectors;
402
    if (o.num_FAT) {
403
	if (o.num_FAT > MAXNFT) {
404
	    warnx("number of FATs (%u) is too large; maximum is %u",
405
		 o.num_FAT, MAXNFT);
406
	    return -1;
407
	}
408
	bpb.nft = o.num_FAT;
409
    }
410
    if (o.directory_entries)
411
	bpb.rde = o.directory_entries;
412
    if (o.media_descriptor_set) {
413
	if (o.media_descriptor < 0xf0) {
414
	    warnx("illegal media descriptor (%#x)", o.media_descriptor);
415
	    return -1;
416
	}
417
	bpb.mid = o.media_descriptor;
418
    }
419
    if (o.sectors_per_fat)
420
	bpb.bspf = o.sectors_per_fat;
421
    if (o.info_sector)
422
	bpb.infs = o.info_sector;
423
    if (o.backup_sector)
424
	bpb.bkbs = o.backup_sector;
425
    bss = 1;
426
    bname = NULL;
427
    fd1 = -1;
428
    if (o.bootstrap) {
429
	bname = o.bootstrap;
430
	if (!strchr(bname, '/')) {
431
	    snprintf(buf, sizeof(buf), "/boot/%s", bname);
432
	    if (!(bname = strdup(buf))) {
433
		warn(NULL);
434
		return -1;
435
	    }
436
	}
437
	if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb)) {
438
	    warn("%s", bname);
439
	    return -1;
440
	}
441
	if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||
442
	    sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16) {
443
	    warnx("%s: inappropriate file type or format", bname);
444
	    return -1;
445
	}
446
	bss = sb.st_size / bpb.bps;
447
    }
448
    if (!bpb.nft)
449
	bpb.nft = 2;
450
    if (!o.fat_type) {
451
	if (bpb.bsec < (bpb.res ? bpb.res : bss) +
452
	    howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *
453
		    ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *
454
	    bpb.nft +
455
	    howmany(bpb.rde ? bpb.rde : DEFRDE,
456
		    bpb.bps / sizeof(struct de)) +
457
	    (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *
458
	    (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))
459
	    o.fat_type = 12;
460
	else if (bpb.rde || bpb.bsec <
461
		 (bpb.res ? bpb.res : bss) +
462
		 howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +
463
		 howmany(DEFRDE, bpb.bps / sizeof(struct de)) +
464
		 (MAXCLS16 + 1) *
465
		 (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))
466
	    o.fat_type = 16;
467
	else
468
	    o.fat_type = 32;
469
    }
470
    x = bss;
471
    if (o.fat_type == 32) {
472
	if (!bpb.infs) {
473
	    if (x == MAXU16 || x == bpb.bkbs) {
474
		warnx("no room for info sector");
475
		return -1;
476
	    }
477
	    bpb.infs = x;
478
	}
479
	if (bpb.infs != MAXU16 && x <= bpb.infs)
480
	    x = bpb.infs + 1;
481
	if (!bpb.bkbs) {
482
	    if (x == MAXU16) {
483
		warnx("no room for backup sector");
484
		return -1;
485
	    }
486
	    bpb.bkbs = x;
487
	} else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs) {
488
	    warnx("backup sector would overwrite info sector");
489
	    return -1;
490
	}
491
	if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)
492
	    x = bpb.bkbs + 1;
493
    }
494
    if (!bpb.res)
495
	bpb.res = o.fat_type == 32 ? MAX(x, MAX(16384 / bpb.bps, 4)) : x;
496
    else if (bpb.res < x) {
497
	warnx("too few reserved sectors (need %d have %d)", x, bpb.res);
498
	return -1;
499
    }
500
    if (o.fat_type != 32 && !bpb.rde)
501
	bpb.rde = DEFRDE;
502
    rds = howmany(bpb.rde, bpb.bps / sizeof(struct de));
503
    if (!bpb.spc)
504
	for (bpb.spc = howmany(o.fat_type == 16 ? DEFBLK16 : DEFBLK, bpb.bps);
505
	     bpb.spc < MAXSPC &&
506
	     bpb.res +
507
	     howmany((RESFTE + maxcls(o.fat_type)) * (o.fat_type / BPN),
508
		     bpb.bps * NPB) * bpb.nft +
509
	     rds +
510
	     (u_int64_t)(maxcls(o.fat_type) + 1) * bpb.spc <= bpb.bsec;
511
	     bpb.spc <<= 1);
512
    if (o.fat_type != 32 && bpb.bspf > MAXU16) {
513
	warnx("too many sectors/FAT for FAT12/16");
514
	return -1;
515
    }
516
    x1 = bpb.res + rds;
517
    x = bpb.bspf ? bpb.bspf : 1;
518
    if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec) {
519
	warnx("meta data exceeds file system size");
520
	return -1;
521
    }
522
    x1 += x * bpb.nft;
523
    x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /
524
	(bpb.spc * bpb.bps * NPB + o.fat_type / BPN * bpb.nft);
525
    x2 = howmany((RESFTE + MIN(x, maxcls(o.fat_type))) * (o.fat_type / BPN),
526
		 bpb.bps * NPB);
527
    if (!bpb.bspf) {
528
	bpb.bspf = x2;
529
	x1 += (bpb.bspf - 1) * bpb.nft;
530
    }
531
    cls = (bpb.bsec - x1) / bpb.spc;
532
    x = (u_int64_t)bpb.bspf * bpb.bps * NPB / (o.fat_type / BPN) - RESFTE;
533
    if (cls > x)
534
	cls = x;
535
    if (bpb.bspf < x2) {
536
	warnx("warning: sectors/FAT limits file system to %u clusters",
537
	      cls);
538
	return -1;
539
    }
540
    if (cls < mincls(o.fat_type)) {
541
	warnx("%u clusters too few clusters for FAT%u, need %u", cls,
542
	    o.fat_type, mincls(o.fat_type));
543
	return -1;
544
    }
545
    if (cls > maxcls(o.fat_type)) {
546
	cls = maxcls(o.fat_type);
547
	bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;
548
	warnx("warning: FAT type limits file system to %u sectors",
549
	      bpb.bsec);
550
	return -1;
551
    }
552
    printf("%s: %u sector%s in %u FAT%u cluster%s "
553
	   "(%u bytes/cluster)\n", fname, cls * bpb.spc,
554
	   cls * bpb.spc == 1 ? "" : "s", cls, o.fat_type,
555
	   cls == 1 ? "" : "s", bpb.bps * bpb.spc);
556
    if (!bpb.mid)
557
	bpb.mid = !bpb.hid ? 0xf0 : 0xf8;
558
    if (o.fat_type == 32)
559
	bpb.rdcl = RESFTE;
560
    if (bpb.hid + bpb.bsec <= MAXU16) {
561
	bpb.sec = bpb.bsec;
562
	bpb.bsec = 0;
563
    }
564
    if (o.fat_type != 32) {
565
	bpb.spf = bpb.bspf;
566
	bpb.bspf = 0;
567
    }
568
    ch = 0;
569
    if (o.fat_type == 12)
570
	ch = 1;			/* 001 Primary DOS with 12 bit FAT */
571
    else if (o.fat_type == 16) {
572
	if (bpb.bsec == 0)
573
	    ch = 4;		/* 004 Primary DOS with 16 bit FAT <32M */
574
	else
575
	    ch = 6;		/* 006 Primary 'big' DOS, 16-bit FAT (> 32MB) */
576
				/*
577
				 * XXX: what about:
578
				 * 014 DOS (16-bit FAT) - LBA
579
				 *  ?
580
				 */
581
    } else if (o.fat_type == 32) {
582
	ch = 11;		/* 011 Primary DOS with 32 bit FAT */
583
				/*
584
				 * XXX: what about:
585
				 * 012 Primary DOS with 32 bit FAT - LBA
586
				 *  ?
587
				 */
588
    }
589
    if (ch != 0)
590
	printf("MBR type: %d\n", ch);
591
    print_bpb(&bpb);
592
593
    gettimeofday(&tv, NULL);
594
    now = tv.tv_sec;
595
    tm = localtime(&now);
596
    img = emalloc(bpb.bps);
597
    dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
598
    signal(SIGINFO, infohandler);
599
    for (lsn = 0; lsn < dir + (o.fat_type == 32 ? bpb.spc : rds); lsn++) {
600
        if (got_siginfo) {
601
            fprintf(stderr,"%s: writing sector %u of %u (%u%%)\n",
602
                fname,lsn,(dir + (o.fat_type == 32 ? bpb.spc : rds)),
603
                (lsn*100)/(dir + (o.fat_type == 32 ? bpb.spc : rds)));
604
            got_siginfo = 0;
605
        }
606
        x = lsn;
607
        if (o.bootstrap &&
608
            o.fat_type == 32 && bpb.bkbs != MAXU16 &&
609
            bss <= bpb.bkbs && x >= bpb.bkbs) {
610
            x -= bpb.bkbs;
611
            if (!x && lseek(fd1, o.offset, SEEK_SET)) {
612
                warn("%s", bname);
613
                return -1;
614
            }
615
        }
616
        if (o.bootstrap && x < bss) {
617
            if ((n = read(fd1, img, bpb.bps)) == -1) {
618
                warn("%s", bname);
619
                return -1;
620
            }
621
            if ((size_t)n != bpb.bps) {
622
                warnx("%s: can't read sector %u", bname, x);
623
                return -1;
624
            }
625
        } else
626
            memset(img, 0, bpb.bps);
627
        if (!lsn ||
628
          (o.fat_type == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {
629
            x1 = sizeof(struct bs);
630
            bsbpb = (struct bsbpb *)(img + x1);
631
            mk2(bsbpb->bps, bpb.bps);
632
            mk1(bsbpb->spc, bpb.spc);
633
            mk2(bsbpb->res, bpb.res);
634
            mk1(bsbpb->nft, bpb.nft);
635
            mk2(bsbpb->rde, bpb.rde);
636
            mk2(bsbpb->sec, bpb.sec);
637
            mk1(bsbpb->mid, bpb.mid);
638
            mk2(bsbpb->spf, bpb.spf);
639
            mk2(bsbpb->spt, bpb.spt);
640
            mk2(bsbpb->hds, bpb.hds);
641
            mk4(bsbpb->hid, bpb.hid);
642
            mk4(bsbpb->bsec, bpb.bsec);
643
            x1 += sizeof(struct bsbpb);
644
            if (o.fat_type == 32) {
645
                bsxbpb = (struct bsxbpb *)(img + x1);
646
                mk4(bsxbpb->bspf, bpb.bspf);
647
                mk2(bsxbpb->xflg, 0);
648
                mk2(bsxbpb->vers, 0);
649
                mk4(bsxbpb->rdcl, bpb.rdcl);
650
                mk2(bsxbpb->infs, bpb.infs);
651
                mk2(bsxbpb->bkbs, bpb.bkbs);
652
                x1 += sizeof(struct bsxbpb);
653
            }
654
            bsx = (struct bsx *)(img + x1);
655
            mk1(bsx->sig, 0x29);
656
            if (o.volume_id_set)
657
                x = o.volume_id;
658
            else
659
                x = (((u_int)(1 + tm->tm_mon) << 8 |
660
                      (u_int)tm->tm_mday) +
661
                     ((u_int)tm->tm_sec << 8 |
662
                      (u_int)(tv.tv_usec / 10))) << 16 |
663
                    ((u_int)(1900 + tm->tm_year) +
664
                     ((u_int)tm->tm_hour << 8 |
665
                      (u_int)tm->tm_min));
666
            mk4(bsx->volid, x);
667
            mklabel(bsx->label, o.volume_label ? o.volume_label : "NO NAME");
668
            snprintf(buf, sizeof(buf), "FAT%u", o.fat_type);
669
            setstr(bsx->type, buf, sizeof(bsx->type));
670
            if (!o.bootstrap) {
671
                x1 += sizeof(struct bsx);
672
                bs = (struct bs *)img;
673
                mk1(bs->jmp[0], 0xeb);
674
                mk1(bs->jmp[1], x1 - 2);
675
                mk1(bs->jmp[2], 0x90);
676
                setstr(bs->oem, o.OEM_string ? o.OEM_string : "NetBSD",
677
                       sizeof(bs->oem));
678
                memcpy(img + x1, bootcode, sizeof(bootcode));
679
                mk2(img + MINBPS - 2, DOSMAGIC);
680
            }
681
        } else if (o.fat_type == 32 && bpb.infs != MAXU16 &&
682
                   (lsn == bpb.infs ||
683
                    (bpb.bkbs != MAXU16 &&
684
                     lsn == bpb.bkbs + bpb.infs))) {
685
            mk4(img, 0x41615252);
686
            mk4(img + MINBPS - 28, 0x61417272);
687
            mk4(img + MINBPS - 24, 0xffffffff);
688
            mk4(img + MINBPS - 20, 0xffffffff);
689
            mk2(img + MINBPS - 2, DOSMAGIC);
690
        } else if (lsn >= bpb.res && lsn < dir &&
691
                   !((lsn - bpb.res) %
692
                     (bpb.spf ? bpb.spf : bpb.bspf))) {
693
            mk1(img[0], bpb.mid);
694
            for (x = 1; x < o.fat_type * (o.fat_type == 32 ? 3U : 2U) / 8U; x++)
695
                mk1(img[x], o.fat_type == 32 && x % 4 == 3 ? 0x0f : 0xff);
696
        } else if (lsn == dir && o.volume_label) {
697
            de = (struct de *)img;
698
            mklabel(de->namext, o.volume_label);
699
            mk1(de->attr, 050);
700
            x = (u_int)tm->tm_hour << 11 |
701
                (u_int)tm->tm_min << 5 |
702
                (u_int)tm->tm_sec >> 1;
703
            mk2(de->time, x);
704
            x = (u_int)(tm->tm_year - 80) << 9 |
705
                (u_int)(tm->tm_mon + 1) << 5 |
706
                (u_int)tm->tm_mday;
707
            mk2(de->date, x);
708
        }
709
        if ((n = write(fd, img, bpb.bps)) == -1) {
710
            warn("%s", fname);
711
            return -1;
712
        }
713
        if ((size_t)n != bpb.bps) {
714
            warnx("%s: can't write sector %u", fname, lsn);
715
            return -1;
716
        }
717
    }
718
    return 0;
719
}
720
721
722
/*
723
 * Get a standard format.
724
 */
725
static int
726
getstdfmt(const char *fmt, struct bpb *bpb)
727
{
728
    u_int x, i;
729
730
    x = sizeof(stdfmt) / sizeof(stdfmt[0]);
731
    for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
732
    if (i == x) {
733
	warnx("%s: unknown standard format", fmt);
734
	return -1;
735
    }
736
    *bpb = stdfmt[i].bpb;
737
    return 0;
738
}
739
740
/*
741
 * Get disk slice, partition, and geometry information.
742
 */
743
static int
744
getbpbinfo(int fd, const char *fname, const char *dtype, int iflag,
745
    struct bpb *bpb, int create)
746
{
747
    const char *s1, *s2;
748
    int part;
749
750
    part = -1;
751
    s1 = fname;
752
    if ((s2 = strrchr(s1, '/')))
753
	s1 = s2 + 1;
754
    for (s2 = s1; *s2 && !isdigit((unsigned char)*s2); s2++);
755
    if (!*s2 || s2 == s1)
756
	s2 = NULL;
757
    else
758
	while (isdigit((unsigned char)*++s2));
759
    s1 = s2;
760
761
    if (((part != -1) && ((!iflag && part != -1) || !bpb->bsec)) ||
762
	!bpb->bps || !bpb->spt || !bpb->hds) {
763
	u_int sector_size;
764
	u_int nsectors;
765
	u_int ntracks;
766
	u_int size;
767
	{
768
	    struct stat st;
769
770
	    if (fstat(fd, &st) == -1) {
771
		warnx("Can't get disk size for `%s'", fname);
772
		return -1;
773
	    }
774
	    /* create a fake geometry for a file image */
775
	    sector_size = 512;
776
	    nsectors = 63;
777
	    ntracks = 255;
778
	    size = st.st_size / sector_size;
779
	}
780
	if (!bpb->bps) {
781
	    if (ckgeom(fname, sector_size, "bytes/sector") == -1)
782
		return -1;
783
	    bpb->bps = sector_size;
784
	}
785
786
	if (nsectors > 63) {
787
		/*
788
		 * The kernel doesn't accept BPB with spt > 63.
789
		 * (see sys/fs/msdosfs/msdosfs_vfsops.c:msdosfs_mountfs())
790
		 * If values taken from disklabel don't match these
791
		 * restrictions, use popular BIOS default values instead.
792
		 */
793
		nsectors = 63;
794
	}
795
	if (!bpb->spt) {
796
	    if (ckgeom(fname, nsectors, "sectors/track") == -1)
797
		return -1;
798
	    bpb->spt = nsectors;
799
	}
800
	if (!bpb->hds) {
801
	    if (ckgeom(fname, ntracks, "drive heads") == -1)
802
		return -1;
803
	    bpb->hds = ntracks;
804
	}
805
	if (!bpb->bsec)
806
	    bpb->bsec = size;
807
    }
808
    return 0;
809
}
810
811
/*
812
 * Print out BPB values.
813
 */
814
static void
815
print_bpb(struct bpb *bpb)
816
{
817
    printf("bps=%u spc=%u res=%u nft=%u", bpb->bps, bpb->spc, bpb->res,
818
	   bpb->nft);
819
    if (bpb->rde)
820
	printf(" rde=%u", bpb->rde);
821
    if (bpb->sec)
822
	printf(" sec=%u", bpb->sec);
823
    printf(" mid=%#x", bpb->mid);
824
    if (bpb->spf)
825
	printf(" spf=%u", bpb->spf);
826
    printf(" spt=%u hds=%u hid=%u", bpb->spt, bpb->hds, bpb->hid);
827
    if (bpb->bsec)
828
	printf(" bsec=%u", bpb->bsec);
829
    if (!bpb->spf) {
830
	printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);
831
	printf(" infs=");
832
	printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);
833
	printf(" bkbs=");
834
	printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);
835
    }
836
    printf("\n");
837
}
838
839
/*
840
 * Check a disk geometry value.
841
 */
842
static int
843
ckgeom(const char *fname, u_int val, const char *msg)
844
{
845
    if (!val) {
846
	warnx("%s: no default %s", fname, msg);
847
	return -1;
848
    }
849
    if (val > MAXU16) {
850
	warnx("%s: illegal %s", fname, msg);
851
	return -1;
852
    }
853
    return 0;
854
}
855
/*
856
 * Check a volume label.
857
 */
858
static int
859
oklabel(const char *src)
860
{
861
    int c, i;
862
863
    for (i = 0; i <= 11; i++) {
864
	c = (u_char)*src++;
865
	if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
866
	    break;
867
    }
868
    return i && !c;
869
}
870
871
/*
872
 * Make a volume label.
873
 */
874
static void
875
mklabel(u_int8_t *dest, const char *src)
876
{
877
    int c, i;
878
879
    for (i = 0; i < 11; i++) {
880
	c = *src ? toupper((unsigned char)*src++) : ' ';
881
	*dest++ = !i && c == '\xe5' ? 5 : c;
882
    }
883
}
884
885
/*
886
 * Copy string, padding with spaces.
887
 */
888
static void
889
setstr(u_int8_t *dest, const char *src, size_t len)
890
{
891
    while (len--)
892
	*dest++ = *src ? *src++ : ' ';
893
}
894
895
static void
896
infohandler(int sig)
897
{
898
    got_siginfo = 1;
899
}