newfs_msdos.c revision 1.8 1 1.8 pooka /* $NetBSD: newfs_msdos.c,v 1.8 2001/09/10 19:25:35 pooka Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.7 christos * Copyright (c) 1998 Robert Nordier
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.7 christos * notice, this list of conditions and the following disclaimer in
14 1.7 christos * the documentation and/or other materials provided with the
15 1.7 christos * distribution.
16 1.1 christos *
17 1.7 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
18 1.7 christos * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 1.7 christos * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.7 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
21 1.7 christos * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.7 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 1.7 christos * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.7 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 1.7 christos * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 1.7 christos * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 1.7 christos * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 christos */
29 1.2 lukem
30 1.2 lukem #include <sys/cdefs.h>
31 1.1 christos #ifndef lint
32 1.7 christos #if 0
33 1.7 christos static const char rcsid[] =
34 1.7 christos "$FreeBSD: src/sbin/newfs_msdos/newfs_msdos.c,v 1.15 2000/10/10 01:49:37 wollman Exp $";
35 1.7 christos #else
36 1.8 pooka __RCSID("$NetBSD: newfs_msdos.c,v 1.8 2001/09/10 19:25:35 pooka Exp $");
37 1.7 christos #endif
38 1.1 christos #endif /* not lint */
39 1.1 christos
40 1.1 christos #include <sys/types.h>
41 1.1 christos #include <sys/param.h>
42 1.7 christos #ifdef __FreeBSD__
43 1.7 christos #include <sys/diskslice.h>
44 1.7 christos #endif
45 1.7 christos #include <sys/disklabel.h>
46 1.7 christos #include <sys/mount.h>
47 1.1 christos #include <sys/stat.h>
48 1.7 christos #include <sys/time.h>
49 1.7 christos #include <sys/ioctl.h>
50 1.7 christos
51 1.7 christos #include <ctype.h>
52 1.7 christos #include <err.h>
53 1.7 christos #include <errno.h>
54 1.7 christos #include <fcntl.h>
55 1.7 christos #include <paths.h>
56 1.1 christos #include <stdio.h>
57 1.1 christos #include <stdlib.h>
58 1.7 christos #include <string.h>
59 1.1 christos #include <time.h>
60 1.1 christos #include <unistd.h>
61 1.7 christos #ifdef __NetBSD__
62 1.7 christos #include <disktab.h>
63 1.7 christos #include <util.h>
64 1.7 christos #endif
65 1.7 christos
66 1.7 christos #define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
67 1.7 christos #define BPN 4 /* bits per nibble */
68 1.7 christos #define NPB 2 /* nibbles per byte */
69 1.7 christos
70 1.7 christos #define DOSMAGIC 0xaa55 /* DOS magic number */
71 1.7 christos #define MINBPS 128 /* minimum bytes per sector */
72 1.7 christos #define MAXSPC 128 /* maximum sectors per cluster */
73 1.7 christos #define MAXNFT 16 /* maximum number of FATs */
74 1.7 christos #define DEFBLK 4096 /* default block size */
75 1.7 christos #define DEFBLK16 2048 /* default block size FAT16 */
76 1.7 christos #define DEFRDE 512 /* default root directory entries */
77 1.7 christos #define RESFTE 2 /* reserved FAT entries */
78 1.7 christos #define MINCLS12 1 /* minimum FAT12 clusters */
79 1.7 christos #define MINCLS16 0x1000 /* minimum FAT16 clusters */
80 1.7 christos #define MINCLS32 2 /* minimum FAT32 clusters */
81 1.7 christos #define MAXCLS12 0xfed /* maximum FAT12 clusters */
82 1.7 christos #define MAXCLS16 0xfff5 /* maximum FAT16 clusters */
83 1.7 christos #define MAXCLS32 0xffffff5 /* maximum FAT32 clusters */
84 1.7 christos
85 1.7 christos #define mincls(fat) ((fat) == 12 ? MINCLS12 : \
86 1.7 christos (fat) == 16 ? MINCLS16 : \
87 1.7 christos MINCLS32)
88 1.7 christos
89 1.7 christos #define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \
90 1.7 christos (fat) == 16 ? MAXCLS16 : \
91 1.7 christos MAXCLS32)
92 1.7 christos
93 1.7 christos #define mk1(p, x) \
94 1.7 christos (p) = (u_int8_t)(x)
95 1.7 christos
96 1.7 christos #define mk2(p, x) \
97 1.7 christos (p)[0] = (u_int8_t)(x), \
98 1.7 christos (p)[1] = (u_int8_t)((x) >> 010)
99 1.7 christos
100 1.7 christos #define mk4(p, x) \
101 1.7 christos (p)[0] = (u_int8_t)(x), \
102 1.7 christos (p)[1] = (u_int8_t)((x) >> 010), \
103 1.7 christos (p)[2] = (u_int8_t)((x) >> 020), \
104 1.7 christos (p)[3] = (u_int8_t)((x) >> 030)
105 1.7 christos
106 1.7 christos #define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
107 1.7 christos #define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg)
108 1.7 christos #define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg)
109 1.7 christos #define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
110 1.7 christos
111 1.7 christos struct bs {
112 1.7 christos u_int8_t jmp[3]; /* bootstrap entry point */
113 1.7 christos u_int8_t oem[8]; /* OEM name and version */
114 1.7 christos };
115 1.1 christos
116 1.7 christos struct bsbpb {
117 1.7 christos u_int8_t bps[2]; /* bytes per sector */
118 1.7 christos u_int8_t spc; /* sectors per cluster */
119 1.7 christos u_int8_t res[2]; /* reserved sectors */
120 1.7 christos u_int8_t nft; /* number of FATs */
121 1.7 christos u_int8_t rde[2]; /* root directory entries */
122 1.7 christos u_int8_t sec[2]; /* total sectors */
123 1.7 christos u_int8_t mid; /* media descriptor */
124 1.7 christos u_int8_t spf[2]; /* sectors per FAT */
125 1.7 christos u_int8_t spt[2]; /* sectors per track */
126 1.7 christos u_int8_t hds[2]; /* drive heads */
127 1.7 christos u_int8_t hid[4]; /* hidden sectors */
128 1.7 christos u_int8_t bsec[4]; /* big total sectors */
129 1.1 christos };
130 1.1 christos
131 1.7 christos struct bsxbpb {
132 1.7 christos u_int8_t bspf[4]; /* big sectors per FAT */
133 1.7 christos u_int8_t xflg[2]; /* FAT control flags */
134 1.7 christos u_int8_t vers[2]; /* file system version */
135 1.7 christos u_int8_t rdcl[4]; /* root directory start cluster */
136 1.7 christos u_int8_t infs[2]; /* file system info sector */
137 1.7 christos u_int8_t bkbs[2]; /* backup boot sector */
138 1.7 christos u_int8_t rsvd[12]; /* reserved */
139 1.1 christos };
140 1.1 christos
141 1.7 christos struct bsx {
142 1.7 christos u_int8_t drv; /* drive number */
143 1.7 christos u_int8_t rsvd; /* reserved */
144 1.7 christos u_int8_t sig; /* extended boot signature */
145 1.7 christos u_int8_t volid[4]; /* volume ID number */
146 1.7 christos u_int8_t label[11]; /* volume label */
147 1.7 christos u_int8_t type[8]; /* file system type */
148 1.7 christos };
149 1.1 christos
150 1.7 christos struct de {
151 1.7 christos u_int8_t namext[11]; /* name and extension */
152 1.7 christos u_int8_t attr; /* attributes */
153 1.7 christos u_int8_t rsvd[10]; /* reserved */
154 1.7 christos u_int8_t time[2]; /* creation time */
155 1.7 christos u_int8_t date[2]; /* creation date */
156 1.7 christos u_int8_t clus[2]; /* starting cluster */
157 1.7 christos u_int8_t size[4]; /* size */
158 1.7 christos };
159 1.1 christos
160 1.7 christos struct bpb {
161 1.7 christos u_int bps; /* bytes per sector */
162 1.7 christos u_int spc; /* sectors per cluster */
163 1.7 christos u_int res; /* reserved sectors */
164 1.7 christos u_int nft; /* number of FATs */
165 1.7 christos u_int rde; /* root directory entries */
166 1.7 christos u_int sec; /* total sectors */
167 1.7 christos u_int mid; /* media descriptor */
168 1.7 christos u_int spf; /* sectors per FAT */
169 1.7 christos u_int spt; /* sectors per track */
170 1.7 christos u_int hds; /* drive heads */
171 1.7 christos u_int hid; /* hidden sectors */
172 1.7 christos u_int bsec; /* big total sectors */
173 1.7 christos u_int bspf; /* big sectors per FAT */
174 1.7 christos u_int rdcl; /* root directory start cluster */
175 1.7 christos u_int infs; /* file system info sector */
176 1.7 christos u_int bkbs; /* backup boot sector */
177 1.7 christos };
178 1.1 christos
179 1.7 christos static struct {
180 1.7 christos const char *name;
181 1.7 christos struct bpb bpb;
182 1.7 christos } stdfmt[] = {
183 1.7 christos {"160", {512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1}},
184 1.7 christos {"180", {512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1}},
185 1.7 christos {"320", {512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2}},
186 1.7 christos {"360", {512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2}},
187 1.7 christos {"640", {512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2}},
188 1.7 christos {"720", {512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2}},
189 1.7 christos {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2}},
190 1.7 christos {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2}},
191 1.7 christos {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2}},
192 1.7 christos {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2}}
193 1.7 christos };
194 1.7 christos
195 1.7 christos static u_int8_t bootcode[] = {
196 1.7 christos 0xfa, /* cli */
197 1.7 christos 0x31, 0xc0, /* xor ax,ax */
198 1.7 christos 0x8e, 0xd0, /* mov ss,ax */
199 1.7 christos 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
200 1.7 christos 0xfb, /* sti */
201 1.7 christos 0x8e, 0xd8, /* mov ds,ax */
202 1.7 christos 0xe8, 0x00, 0x00, /* call $ + 3 */
203 1.7 christos 0x5e, /* pop si */
204 1.7 christos 0x83, 0xc6, 0x19, /* add si,+19h */
205 1.7 christos 0xbb, 0x07, 0x00, /* mov bx,0007h */
206 1.7 christos 0xfc, /* cld */
207 1.7 christos 0xac, /* lodsb */
208 1.7 christos 0x84, 0xc0, /* test al,al */
209 1.7 christos 0x74, 0x06, /* jz $ + 8 */
210 1.7 christos 0xb4, 0x0e, /* mov ah,0eh */
211 1.7 christos 0xcd, 0x10, /* int 10h */
212 1.7 christos 0xeb, 0xf5, /* jmp $ - 9 */
213 1.7 christos 0x30, 0xe4, /* xor ah,ah */
214 1.7 christos 0xcd, 0x16, /* int 16h */
215 1.7 christos 0xcd, 0x19, /* int 19h */
216 1.7 christos 0x0d, 0x0a,
217 1.7 christos 'N', 'o', 'n', '-', 's', 'y', 's', 't',
218 1.7 christos 'e', 'm', ' ', 'd', 'i', 's', 'k',
219 1.7 christos 0x0d, 0x0a,
220 1.7 christos 'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
221 1.7 christos 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
222 1.7 christos ' ', 'r', 'e', 'b', 'o', 'o', 't',
223 1.7 christos 0x0d, 0x0a,
224 1.7 christos 0
225 1.7 christos };
226 1.6 cgd
227 1.7 christos static void check_mounted(const char *, mode_t);
228 1.7 christos static void getstdfmt(const char *, struct bpb *);
229 1.7 christos static void getdiskinfo(int, const char *, const char *, int,
230 1.7 christos struct bpb *);
231 1.7 christos static void print_bpb(struct bpb *);
232 1.7 christos static u_int ckgeom(const char *, u_int, const char *);
233 1.7 christos static u_int argtou(const char *, u_int, u_int, const char *);
234 1.7 christos static int oklabel(const char *);
235 1.7 christos static void mklabel(u_int8_t *, const char *);
236 1.7 christos static void setstr(u_int8_t *, const char *, size_t);
237 1.7 christos static void usage(void);
238 1.1 christos
239 1.1 christos /*
240 1.7 christos * Construct a FAT12, FAT16, or FAT32 file system.
241 1.7 christos */
242 1.7 christos int
243 1.7 christos main(int argc, char *argv[])
244 1.7 christos {
245 1.7 christos static char opts[] = "NB:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
246 1.7 christos static const char *opt_B, *opt_L, *opt_O, *opt_f;
247 1.7 christos static u_int opt_F, opt_I, opt_S, opt_a, opt_b, opt_c, opt_e;
248 1.7 christos static u_int opt_h, opt_i, opt_k, opt_m, opt_n, opt_o, opt_r;
249 1.7 christos static u_int opt_s, opt_u;
250 1.7 christos static int opt_N;
251 1.7 christos static int Iflag, mflag, oflag;
252 1.7 christos char buf[MAXPATHLEN];
253 1.7 christos struct stat sb;
254 1.7 christos struct timeval tv;
255 1.7 christos struct bpb bpb;
256 1.7 christos struct tm *tm;
257 1.7 christos struct bs *bs;
258 1.7 christos struct bsbpb *bsbpb;
259 1.7 christos struct bsxbpb *bsxbpb;
260 1.7 christos struct bsx *bsx;
261 1.7 christos struct de *de;
262 1.7 christos u_int8_t *img;
263 1.7 christos const char *fname, *dtype, *bname;
264 1.7 christos ssize_t n;
265 1.7 christos time_t now;
266 1.7 christos u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
267 1.7 christos int ch, fd, fd1;
268 1.7 christos
269 1.7 christos while ((ch = getopt(argc, argv, opts)) != -1)
270 1.7 christos switch (ch) {
271 1.7 christos case 'N':
272 1.7 christos opt_N = 1;
273 1.7 christos break;
274 1.7 christos case 'B':
275 1.7 christos opt_B = optarg;
276 1.7 christos break;
277 1.7 christos case 'F':
278 1.7 christos if (strcmp(optarg, "12") &&
279 1.7 christos strcmp(optarg, "16") &&
280 1.7 christos strcmp(optarg, "32"))
281 1.7 christos errx(1, "%s: bad FAT type", optarg);
282 1.7 christos opt_F = atoi(optarg);
283 1.7 christos break;
284 1.7 christos case 'I':
285 1.7 christos opt_I = argto4(optarg, 0, "volume ID");
286 1.7 christos Iflag = 1;
287 1.7 christos break;
288 1.7 christos case 'L':
289 1.7 christos if (!oklabel(optarg))
290 1.7 christos errx(1, "%s: bad volume label", optarg);
291 1.7 christos opt_L = optarg;
292 1.7 christos break;
293 1.7 christos case 'O':
294 1.7 christos if (strlen(optarg) > 8)
295 1.7 christos errx(1, "%s: bad OEM string", optarg);
296 1.7 christos opt_O = optarg;
297 1.7 christos break;
298 1.7 christos case 'S':
299 1.7 christos opt_S = argto2(optarg, 1, "bytes/sector");
300 1.7 christos break;
301 1.7 christos case 'a':
302 1.7 christos opt_a = argto4(optarg, 1, "sectors/FAT");
303 1.7 christos break;
304 1.7 christos case 'b':
305 1.7 christos opt_b = argtox(optarg, 1, "block size");
306 1.7 christos opt_c = 0;
307 1.7 christos break;
308 1.7 christos case 'c':
309 1.7 christos opt_c = argto1(optarg, 1, "sectors/cluster");
310 1.7 christos opt_b = 0;
311 1.7 christos break;
312 1.7 christos case 'e':
313 1.7 christos opt_e = argto2(optarg, 1, "directory entries");
314 1.7 christos break;
315 1.7 christos case 'f':
316 1.7 christos opt_f = optarg;
317 1.7 christos break;
318 1.7 christos case 'h':
319 1.7 christos opt_h = argto2(optarg, 1, "drive heads");
320 1.7 christos break;
321 1.7 christos case 'i':
322 1.7 christos opt_i = argto2(optarg, 1, "info sector");
323 1.7 christos break;
324 1.7 christos case 'k':
325 1.7 christos opt_k = argto2(optarg, 1, "backup sector");
326 1.7 christos break;
327 1.7 christos case 'm':
328 1.7 christos opt_m = argto1(optarg, 0, "media descriptor");
329 1.7 christos mflag = 1;
330 1.7 christos break;
331 1.7 christos case 'n':
332 1.7 christos opt_n = argto1(optarg, 1, "number of FATs");
333 1.7 christos break;
334 1.7 christos case 'o':
335 1.7 christos opt_o = argto4(optarg, 0, "hidden sectors");
336 1.7 christos oflag = 1;
337 1.7 christos break;
338 1.7 christos case 'r':
339 1.7 christos opt_r = argto2(optarg, 1, "reserved sectors");
340 1.7 christos break;
341 1.7 christos case 's':
342 1.7 christos opt_s = argto4(optarg, 1, "file system size");
343 1.7 christos break;
344 1.7 christos case 'u':
345 1.7 christos opt_u = argto2(optarg, 1, "sectors/track");
346 1.7 christos break;
347 1.7 christos default:
348 1.7 christos usage();
349 1.7 christos }
350 1.7 christos argc -= optind;
351 1.7 christos argv += optind;
352 1.7 christos if (argc < 1 || argc > 2)
353 1.7 christos usage();
354 1.7 christos fname = *argv++;
355 1.7 christos if (!strchr(fname, '/')) {
356 1.8 pooka snprintf(buf, sizeof(buf), "%sr%s", _PATH_DEV, fname);
357 1.7 christos if (!(fname = strdup(buf)))
358 1.7 christos err(1, NULL);
359 1.7 christos }
360 1.7 christos dtype = *argv;
361 1.7 christos if ((fd = open(fname, opt_N ? O_RDONLY : O_RDWR)) == -1 ||
362 1.7 christos fstat(fd, &sb))
363 1.7 christos err(1, "%s", fname);
364 1.7 christos if (!opt_N)
365 1.7 christos check_mounted(fname, sb.st_mode);
366 1.7 christos if (!S_ISCHR(sb.st_mode))
367 1.7 christos warnx("warning: %s is not a character device", fname);
368 1.7 christos memset(&bpb, 0, sizeof(bpb));
369 1.7 christos if (opt_f) {
370 1.7 christos getstdfmt(opt_f, &bpb);
371 1.7 christos bpb.bsec = bpb.sec;
372 1.7 christos bpb.sec = 0;
373 1.7 christos bpb.bspf = bpb.spf;
374 1.7 christos bpb.spf = 0;
375 1.7 christos }
376 1.7 christos if (opt_h)
377 1.7 christos bpb.hds = opt_h;
378 1.7 christos if (opt_u)
379 1.7 christos bpb.spt = opt_u;
380 1.7 christos if (opt_S)
381 1.7 christos bpb.bps = opt_S;
382 1.7 christos if (opt_s)
383 1.7 christos bpb.bsec = opt_s;
384 1.7 christos if (oflag)
385 1.7 christos bpb.hid = opt_o;
386 1.7 christos if (!(opt_f || (opt_h && opt_u && opt_S && opt_s && oflag)))
387 1.7 christos getdiskinfo(fd, fname, dtype, oflag, &bpb);
388 1.7 christos if (!powerof2(bpb.bps))
389 1.7 christos errx(1, "bytes/sector (%u) is not a power of 2", bpb.bps);
390 1.7 christos if (bpb.bps < MINBPS)
391 1.7 christos errx(1, "bytes/sector (%u) is too small; minimum is %u",
392 1.7 christos bpb.bps, MINBPS);
393 1.7 christos if (!(fat = opt_F)) {
394 1.7 christos if (opt_f)
395 1.7 christos fat = 12;
396 1.7 christos else if (!opt_e && (opt_i || opt_k))
397 1.7 christos fat = 32;
398 1.7 christos }
399 1.7 christos if ((fat == 32 && opt_e) || (fat != 32 && (opt_i || opt_k)))
400 1.7 christos errx(1, "-%c is not a legal FAT%s option",
401 1.7 christos fat == 32 ? 'e' : opt_i ? 'i' : 'k',
402 1.7 christos fat == 32 ? "32" : "12/16");
403 1.7 christos if (opt_f && fat == 32)
404 1.7 christos bpb.rde = 0;
405 1.7 christos if (opt_b) {
406 1.7 christos if (!powerof2(opt_b))
407 1.7 christos errx(1, "block size (%u) is not a power of 2", opt_b);
408 1.7 christos if (opt_b < bpb.bps)
409 1.7 christos errx(1, "block size (%u) is too small; minimum is %u",
410 1.7 christos opt_b, bpb.bps);
411 1.7 christos if (opt_b > bpb.bps * MAXSPC)
412 1.7 christos errx(1, "block size (%u) is too large; maximum is %u",
413 1.7 christos opt_b, bpb.bps * MAXSPC);
414 1.7 christos bpb.spc = opt_b / bpb.bps;
415 1.7 christos }
416 1.7 christos if (opt_c) {
417 1.7 christos if (!powerof2(opt_c))
418 1.7 christos errx(1, "sectors/cluster (%u) is not a power of 2", opt_c);
419 1.7 christos bpb.spc = opt_c;
420 1.7 christos }
421 1.7 christos if (opt_r)
422 1.7 christos bpb.res = opt_r;
423 1.7 christos if (opt_n) {
424 1.7 christos if (opt_n > MAXNFT)
425 1.7 christos errx(1, "number of FATs (%u) is too large; maximum is %u",
426 1.7 christos opt_n, MAXNFT);
427 1.7 christos bpb.nft = opt_n;
428 1.7 christos }
429 1.7 christos if (opt_e)
430 1.7 christos bpb.rde = opt_e;
431 1.7 christos if (mflag) {
432 1.7 christos if (opt_m < 0xf0)
433 1.7 christos errx(1, "illegal media descriptor (%#x)", opt_m);
434 1.7 christos bpb.mid = opt_m;
435 1.7 christos }
436 1.7 christos if (opt_a)
437 1.7 christos bpb.bspf = opt_a;
438 1.7 christos if (opt_i)
439 1.7 christos bpb.infs = opt_i;
440 1.7 christos if (opt_k)
441 1.7 christos bpb.bkbs = opt_k;
442 1.7 christos bss = 1;
443 1.7 christos bname = NULL;
444 1.7 christos fd1 = -1;
445 1.7 christos if (opt_B) {
446 1.7 christos bname = opt_B;
447 1.7 christos if (!strchr(bname, '/')) {
448 1.7 christos snprintf(buf, sizeof(buf), "/boot/%s", bname);
449 1.7 christos if (!(bname = strdup(buf)))
450 1.7 christos err(1, NULL);
451 1.7 christos }
452 1.7 christos if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb))
453 1.7 christos err(1, "%s", bname);
454 1.7 christos if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||
455 1.7 christos sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16)
456 1.7 christos errx(1, "%s: inappropriate file type or format", bname);
457 1.7 christos bss = sb.st_size / bpb.bps;
458 1.7 christos }
459 1.7 christos if (!bpb.nft)
460 1.7 christos bpb.nft = 2;
461 1.7 christos if (!fat) {
462 1.7 christos if (bpb.bsec < (bpb.res ? bpb.res : bss) +
463 1.7 christos howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *
464 1.7 christos ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *
465 1.7 christos bpb.nft +
466 1.7 christos howmany(bpb.rde ? bpb.rde : DEFRDE,
467 1.7 christos bpb.bps / sizeof(struct de)) +
468 1.7 christos (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *
469 1.7 christos (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))
470 1.7 christos fat = 12;
471 1.7 christos else if (bpb.rde || bpb.bsec <
472 1.7 christos (bpb.res ? bpb.res : bss) +
473 1.7 christos howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +
474 1.7 christos howmany(DEFRDE, bpb.bps / sizeof(struct de)) +
475 1.7 christos (MAXCLS16 + 1) *
476 1.7 christos (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))
477 1.7 christos fat = 16;
478 1.7 christos else
479 1.7 christos fat = 32;
480 1.7 christos }
481 1.7 christos x = bss;
482 1.7 christos if (fat == 32) {
483 1.7 christos if (!bpb.infs) {
484 1.7 christos if (x == MAXU16 || x == bpb.bkbs)
485 1.7 christos errx(1, "no room for info sector");
486 1.7 christos bpb.infs = x;
487 1.7 christos }
488 1.7 christos if (bpb.infs != MAXU16 && x <= bpb.infs)
489 1.7 christos x = bpb.infs + 1;
490 1.7 christos if (!bpb.bkbs) {
491 1.7 christos if (x == MAXU16)
492 1.7 christos errx(1, "no room for backup sector");
493 1.7 christos bpb.bkbs = x;
494 1.7 christos } else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs)
495 1.7 christos errx(1, "backup sector would overwrite info sector");
496 1.7 christos if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)
497 1.7 christos x = bpb.bkbs + 1;
498 1.7 christos }
499 1.7 christos if (!bpb.res)
500 1.7 christos bpb.res = fat == 32 ? MAX(x, MAX(16384 / bpb.bps, 4)) : x;
501 1.7 christos else if (bpb.res < x)
502 1.7 christos errx(1, "too few reserved sectors");
503 1.7 christos if (fat != 32 && !bpb.rde)
504 1.7 christos bpb.rde = DEFRDE;
505 1.7 christos rds = howmany(bpb.rde, bpb.bps / sizeof(struct de));
506 1.7 christos if (!bpb.spc)
507 1.7 christos for (bpb.spc = howmany(fat == 16 ? DEFBLK16 : DEFBLK, bpb.bps);
508 1.7 christos bpb.spc < MAXSPC &&
509 1.7 christos bpb.res +
510 1.7 christos howmany((RESFTE + maxcls(fat)) * (fat / BPN),
511 1.7 christos bpb.bps * NPB) * bpb.nft +
512 1.7 christos rds +
513 1.7 christos (u_int64_t)(maxcls(fat) + 1) * bpb.spc <= bpb.bsec;
514 1.7 christos bpb.spc <<= 1);
515 1.7 christos if (fat != 32 && bpb.bspf > MAXU16)
516 1.7 christos errx(1, "too many sectors/FAT for FAT12/16");
517 1.7 christos x1 = bpb.res + rds;
518 1.7 christos x = bpb.bspf ? bpb.bspf : 1;
519 1.7 christos if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec)
520 1.7 christos errx(1, "meta data exceeds file system size");
521 1.7 christos x1 += x * bpb.nft;
522 1.7 christos x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /
523 1.7 christos (bpb.spc * bpb.bps * NPB + fat / BPN * bpb.nft);
524 1.7 christos x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN),
525 1.7 christos bpb.bps * NPB);
526 1.7 christos if (!bpb.bspf) {
527 1.7 christos bpb.bspf = x2;
528 1.7 christos x1 += (bpb.bspf - 1) * bpb.nft;
529 1.7 christos }
530 1.7 christos cls = (bpb.bsec - x1) / bpb.spc;
531 1.7 christos x = (u_int64_t)bpb.bspf * bpb.bps * NPB / (fat / BPN) - RESFTE;
532 1.7 christos if (cls > x)
533 1.7 christos cls = x;
534 1.7 christos if (bpb.bspf < x2)
535 1.7 christos warnx("warning: sectors/FAT limits file system to %u clusters",
536 1.7 christos cls);
537 1.7 christos if (cls < mincls(fat))
538 1.7 christos errx(1, "%u clusters too few clusters for FAT%u, need %u", cls, fat,
539 1.7 christos mincls(fat));
540 1.7 christos if (cls > maxcls(fat)) {
541 1.7 christos cls = maxcls(fat);
542 1.7 christos bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;
543 1.7 christos warnx("warning: FAT type limits file system to %u sectors",
544 1.7 christos bpb.bsec);
545 1.7 christos }
546 1.7 christos printf("%s: %u sector%s in %u FAT%u cluster%s "
547 1.7 christos "(%u bytes/cluster)\n", fname, cls * bpb.spc,
548 1.7 christos cls * bpb.spc == 1 ? "" : "s", cls, fat,
549 1.7 christos cls == 1 ? "" : "s", bpb.bps * bpb.spc);
550 1.7 christos if (!bpb.mid)
551 1.7 christos bpb.mid = !bpb.hid ? 0xf0 : 0xf8;
552 1.7 christos if (fat == 32)
553 1.7 christos bpb.rdcl = RESFTE;
554 1.7 christos if (bpb.hid + bpb.bsec <= MAXU16) {
555 1.7 christos bpb.sec = bpb.bsec;
556 1.7 christos bpb.bsec = 0;
557 1.7 christos }
558 1.7 christos if (fat != 32) {
559 1.7 christos bpb.spf = bpb.bspf;
560 1.7 christos bpb.bspf = 0;
561 1.7 christos }
562 1.7 christos print_bpb(&bpb);
563 1.7 christos if (!opt_N) {
564 1.7 christos gettimeofday(&tv, NULL);
565 1.7 christos now = tv.tv_sec;
566 1.7 christos tm = localtime(&now);
567 1.7 christos if (!(img = malloc(bpb.bps)))
568 1.7 christos err(1, NULL);
569 1.7 christos dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
570 1.7 christos for (lsn = 0; lsn < dir + (fat == 32 ? bpb.spc : rds); lsn++) {
571 1.7 christos x = lsn;
572 1.7 christos if (opt_B &&
573 1.7 christos fat == 32 && bpb.bkbs != MAXU16 &&
574 1.7 christos bss <= bpb.bkbs && x >= bpb.bkbs) {
575 1.7 christos x -= bpb.bkbs;
576 1.7 christos if (!x && lseek(fd1, 0, SEEK_SET))
577 1.7 christos err(1, "%s", bname);
578 1.7 christos }
579 1.7 christos if (opt_B && x < bss) {
580 1.7 christos if ((n = read(fd1, img, bpb.bps)) == -1)
581 1.7 christos err(1, "%s", bname);
582 1.7 christos if (n != bpb.bps)
583 1.7 christos errx(1, "%s: can't read sector %u", bname, x);
584 1.7 christos } else
585 1.7 christos memset(img, 0, bpb.bps);
586 1.7 christos if (!lsn ||
587 1.7 christos (fat == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {
588 1.7 christos x1 = sizeof(struct bs);
589 1.7 christos bsbpb = (struct bsbpb *)(img + x1);
590 1.7 christos mk2(bsbpb->bps, bpb.bps);
591 1.7 christos mk1(bsbpb->spc, bpb.spc);
592 1.7 christos mk2(bsbpb->res, bpb.res);
593 1.7 christos mk1(bsbpb->nft, bpb.nft);
594 1.7 christos mk2(bsbpb->rde, bpb.rde);
595 1.7 christos mk2(bsbpb->sec, bpb.sec);
596 1.7 christos mk1(bsbpb->mid, bpb.mid);
597 1.7 christos mk2(bsbpb->spf, bpb.spf);
598 1.7 christos mk2(bsbpb->spt, bpb.spt);
599 1.7 christos mk2(bsbpb->hds, bpb.hds);
600 1.7 christos mk4(bsbpb->hid, bpb.hid);
601 1.7 christos mk4(bsbpb->bsec, bpb.bsec);
602 1.7 christos x1 += sizeof(struct bsbpb);
603 1.7 christos if (fat == 32) {
604 1.7 christos bsxbpb = (struct bsxbpb *)(img + x1);
605 1.7 christos mk4(bsxbpb->bspf, bpb.bspf);
606 1.7 christos mk2(bsxbpb->xflg, 0);
607 1.7 christos mk2(bsxbpb->vers, 0);
608 1.7 christos mk4(bsxbpb->rdcl, bpb.rdcl);
609 1.7 christos mk2(bsxbpb->infs, bpb.infs);
610 1.7 christos mk2(bsxbpb->bkbs, bpb.bkbs);
611 1.7 christos x1 += sizeof(struct bsxbpb);
612 1.7 christos }
613 1.7 christos bsx = (struct bsx *)(img + x1);
614 1.7 christos mk1(bsx->sig, 0x29);
615 1.7 christos if (Iflag)
616 1.7 christos x = opt_I;
617 1.7 christos else
618 1.7 christos x = (((u_int)(1 + tm->tm_mon) << 8 |
619 1.7 christos (u_int)tm->tm_mday) +
620 1.7 christos ((u_int)tm->tm_sec << 8 |
621 1.7 christos (u_int)(tv.tv_usec / 10))) << 16 |
622 1.7 christos ((u_int)(1900 + tm->tm_year) +
623 1.7 christos ((u_int)tm->tm_hour << 8 |
624 1.7 christos (u_int)tm->tm_min));
625 1.7 christos mk4(bsx->volid, x);
626 1.7 christos mklabel(bsx->label, opt_L ? opt_L : "NO NAME");
627 1.7 christos sprintf(buf, "FAT%u", fat);
628 1.7 christos setstr(bsx->type, buf, sizeof(bsx->type));
629 1.7 christos if (!opt_B) {
630 1.7 christos x1 += sizeof(struct bsx);
631 1.7 christos bs = (struct bs *)img;
632 1.7 christos mk1(bs->jmp[0], 0xeb);
633 1.7 christos mk1(bs->jmp[1], x1 - 2);
634 1.7 christos mk1(bs->jmp[2], 0x90);
635 1.7 christos setstr(bs->oem, opt_O ? opt_O : "BSD 4.4",
636 1.7 christos sizeof(bs->oem));
637 1.7 christos memcpy(img + x1, bootcode, sizeof(bootcode));
638 1.7 christos mk2(img + bpb.bps - 2, DOSMAGIC);
639 1.1 christos }
640 1.7 christos } else if (fat == 32 && bpb.infs != MAXU16 &&
641 1.7 christos (lsn == bpb.infs ||
642 1.7 christos (bpb.bkbs != MAXU16 &&
643 1.7 christos lsn == bpb.bkbs + bpb.infs))) {
644 1.7 christos mk4(img, 0x41615252);
645 1.7 christos mk4(img + bpb.bps - 28, 0x61417272);
646 1.7 christos mk4(img + bpb.bps - 24, 0xffffffff);
647 1.7 christos mk4(img + bpb.bps - 20, bpb.rdcl);
648 1.7 christos mk2(img + bpb.bps - 2, DOSMAGIC);
649 1.7 christos } else if (lsn >= bpb.res && lsn < dir &&
650 1.7 christos !((lsn - bpb.res) %
651 1.7 christos (bpb.spf ? bpb.spf : bpb.bspf))) {
652 1.7 christos mk1(img[0], bpb.mid);
653 1.7 christos for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
654 1.7 christos mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
655 1.7 christos } else if (lsn == dir && opt_L) {
656 1.7 christos de = (struct de *)img;
657 1.7 christos mklabel(de->namext, opt_L);
658 1.7 christos mk1(de->attr, 050);
659 1.7 christos x = (u_int)tm->tm_hour << 11 |
660 1.7 christos (u_int)tm->tm_min << 5 |
661 1.7 christos (u_int)tm->tm_sec >> 1;
662 1.7 christos mk2(de->time, x);
663 1.7 christos x = (u_int)(tm->tm_year - 80) << 9 |
664 1.7 christos (u_int)(tm->tm_mon + 1) << 5 |
665 1.7 christos (u_int)tm->tm_mday;
666 1.7 christos mk2(de->date, x);
667 1.7 christos }
668 1.7 christos if ((n = write(fd, img, bpb.bps)) == -1)
669 1.7 christos err(1, "%s", fname);
670 1.7 christos if (n != bpb.bps)
671 1.7 christos errx(1, "%s: can't write sector %u", fname, lsn);
672 1.7 christos }
673 1.7 christos }
674 1.7 christos return 0;
675 1.1 christos }
676 1.1 christos
677 1.7 christos /*
678 1.7 christos * Exit with error if file system is mounted.
679 1.7 christos */
680 1.1 christos static void
681 1.7 christos check_mounted(const char *fname, mode_t mode)
682 1.7 christos {
683 1.7 christos struct statfs *mp;
684 1.7 christos const char *s1, *s2;
685 1.7 christos size_t len;
686 1.7 christos int n, r;
687 1.7 christos
688 1.7 christos if (!(n = getmntinfo(&mp, MNT_NOWAIT)))
689 1.7 christos err(1, "getmntinfo");
690 1.7 christos len = strlen(_PATH_DEV);
691 1.7 christos s1 = fname;
692 1.7 christos if (!strncmp(s1, _PATH_DEV, len))
693 1.7 christos s1 += len;
694 1.7 christos r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
695 1.7 christos for (; n--; mp++) {
696 1.7 christos s2 = mp->f_mntfromname;
697 1.7 christos if (!strncmp(s2, _PATH_DEV, len))
698 1.7 christos s2 += len;
699 1.7 christos if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
700 1.7 christos !strcmp(s1, s2))
701 1.7 christos errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
702 1.7 christos }
703 1.1 christos }
704 1.1 christos
705 1.7 christos /*
706 1.7 christos * Get a standard format.
707 1.7 christos */
708 1.7 christos static void
709 1.7 christos getstdfmt(const char *fmt, struct bpb *bpb)
710 1.7 christos {
711 1.7 christos u_int x, i;
712 1.1 christos
713 1.7 christos x = sizeof(stdfmt) / sizeof(stdfmt[0]);
714 1.7 christos for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
715 1.7 christos if (i == x)
716 1.7 christos errx(1, "%s: unknown standard format", fmt);
717 1.7 christos *bpb = stdfmt[i].bpb;
718 1.7 christos }
719 1.1 christos
720 1.7 christos /*
721 1.7 christos * Get disk slice, partition, and geometry information.
722 1.7 christos */
723 1.7 christos static void
724 1.7 christos getdiskinfo(int fd, const char *fname, const char *dtype, int oflag,
725 1.7 christos struct bpb *bpb)
726 1.7 christos {
727 1.7 christos #ifdef __FreeBSD__
728 1.7 christos struct diskslices ds;
729 1.7 christos #endif
730 1.7 christos struct disklabel dl, *lp;
731 1.7 christos const char *s1, *s2;
732 1.7 christos char *s;
733 1.7 christos int slice, part, fd1, i, e;
734 1.7 christos int maxpartitions;
735 1.7 christos
736 1.7 christos #ifdef __FreeBSD__
737 1.7 christos slice = part = -1;
738 1.7 christos s1 = fname;
739 1.7 christos if ((s2 = strrchr(s1, '/')))
740 1.7 christos s1 = s2 + 1;
741 1.7 christos for (s2 = s1; *s2 && !isdigit(*s2); s2++);
742 1.7 christos if (!*s2 || s2 == s1)
743 1.7 christos s2 = NULL;
744 1.7 christos else
745 1.7 christos while (isdigit(*++s2));
746 1.7 christos s1 = s2;
747 1.7 christos if (s2 && *s2 == 's') {
748 1.7 christos slice = strtol(s2 + 1, &s, 10);
749 1.7 christos if (slice < 1 || slice > MAX_SLICES - BASE_SLICE)
750 1.7 christos s2 = NULL;
751 1.7 christos else {
752 1.7 christos slice = BASE_SLICE + slice - 1;
753 1.7 christos s2 = s;
754 1.7 christos }
755 1.7 christos }
756 1.7 christos #else
757 1.7 christos slice = -1;
758 1.7 christos #endif
759 1.7 christos
760 1.7 christos #ifdef __NetBSD__
761 1.7 christos maxpartitions = getmaxpartitions();
762 1.7 christos #else
763 1.7 christos maxpartitions = MAXPARTITIONS;
764 1.7 christos #endif
765 1.7 christos
766 1.7 christos if (s2 && *s2 >= 'a' && *s2 <= 'a' + maxpartitions - 1) {
767 1.7 christos #ifdef __FreeBSD__
768 1.7 christos if (slice == -1)
769 1.7 christos slice = COMPATIBILITY_SLICE;
770 1.7 christos #endif
771 1.7 christos part = *s2++ - 'a';
772 1.7 christos }
773 1.8 pooka #ifdef __FreeBSD__
774 1.7 christos if (!s2 || (*s2 && *s2 != '.'))
775 1.7 christos errx(1, "%s: can't figure out partition info", fname);
776 1.7 christos if (slice != -1 && (!oflag || (!bpb->bsec && part == -1))) {
777 1.7 christos if (ioctl(fd, DIOCGSLICEINFO, &ds) == -1) {
778 1.7 christos warn("ioctl (GSLICEINFO)");
779 1.7 christos errx(1, "%s: can't get slice info", fname);
780 1.7 christos }
781 1.7 christos if (slice >= ds.dss_nslices || !ds.dss_slices[slice].ds_size)
782 1.7 christos errx(1, "%s: slice is unavailable", fname);
783 1.7 christos if (!oflag)
784 1.7 christos bpb->hid = ds.dss_slices[slice].ds_offset;
785 1.7 christos if (!bpb->bsec && part == -1)
786 1.7 christos bpb->bsec = ds.dss_slices[slice].ds_size;
787 1.7 christos }
788 1.7 christos #endif
789 1.7 christos if (((slice == -1 || part != -1) &&
790 1.7 christos ((!oflag && part != -1) || !bpb->bsec)) ||
791 1.7 christos !bpb->bps || !bpb->spt || !bpb->hds) {
792 1.7 christos lp = &dl;
793 1.7 christos i = ioctl(fd, DIOCGDINFO, lp);
794 1.7 christos if (i == -1 && slice != -1 && part == -1) {
795 1.7 christos e = errno;
796 1.7 christos if (!(s = strdup(fname)))
797 1.7 christos err(1, NULL);
798 1.7 christos s[s1 - fname] = 0;
799 1.7 christos if ((fd1 = open(s, O_RDONLY)) != -1) {
800 1.7 christos i = ioctl(fd1, DIOCGDINFO, lp);
801 1.7 christos close(fd1);
802 1.7 christos }
803 1.7 christos free(s);
804 1.7 christos errno = e;
805 1.7 christos }
806 1.7 christos if (i == -1) {
807 1.7 christos if (!dtype) {
808 1.7 christos warn("ioctl (DIOCGDINFO)");
809 1.7 christos errx(1, "%s: can't read disk label; "
810 1.7 christos "disk type must be specified", fname);
811 1.7 christos } else if (!(lp = getdiskbyname(dtype)))
812 1.7 christos errx(1, "%s: unknown disk type", dtype);
813 1.7 christos }
814 1.7 christos if (slice == -1 || part != -1) {
815 1.7 christos if (part == -1)
816 1.7 christos part = RAW_PART;
817 1.7 christos if (part >= lp->d_npartitions ||
818 1.7 christos !lp->d_partitions[part].p_size)
819 1.7 christos errx(1, "%s: partition is unavailable", fname);
820 1.7 christos if (!oflag && part != -1)
821 1.7 christos bpb->hid += lp->d_partitions[part].p_offset;
822 1.7 christos if (!bpb->bsec)
823 1.7 christos bpb->bsec = lp->d_partitions[part].p_size;
824 1.7 christos }
825 1.7 christos if (!bpb->bps)
826 1.7 christos bpb->bps = ckgeom(fname, lp->d_secsize, "bytes/sector");
827 1.7 christos if (!bpb->spt)
828 1.7 christos bpb->spt = ckgeom(fname, lp->d_nsectors, "sectors/track");
829 1.7 christos if (!bpb->hds)
830 1.7 christos bpb->hds = ckgeom(fname, lp->d_ntracks, "drive heads");
831 1.7 christos }
832 1.7 christos }
833 1.1 christos
834 1.7 christos /*
835 1.7 christos * Print out BPB values.
836 1.7 christos */
837 1.7 christos static void
838 1.7 christos print_bpb(struct bpb *bpb)
839 1.7 christos {
840 1.7 christos printf("bps=%u spc=%u res=%u nft=%u", bpb->bps, bpb->spc, bpb->res,
841 1.7 christos bpb->nft);
842 1.7 christos if (bpb->rde)
843 1.7 christos printf(" rde=%u", bpb->rde);
844 1.7 christos if (bpb->sec)
845 1.7 christos printf(" sec=%u", bpb->sec);
846 1.7 christos printf(" mid=%#x", bpb->mid);
847 1.7 christos if (bpb->spf)
848 1.7 christos printf(" spf=%u", bpb->spf);
849 1.7 christos printf(" spt=%u hds=%u hid=%u", bpb->spt, bpb->hds, bpb->hid);
850 1.7 christos if (bpb->bsec)
851 1.7 christos printf(" bsec=%u", bpb->bsec);
852 1.7 christos if (!bpb->spf) {
853 1.7 christos printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);
854 1.7 christos printf(" infs=");
855 1.7 christos printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);
856 1.7 christos printf(" bkbs=");
857 1.7 christos printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);
858 1.7 christos }
859 1.7 christos printf("\n");
860 1.7 christos }
861 1.1 christos
862 1.7 christos /*
863 1.7 christos * Check a disk geometry value.
864 1.7 christos */
865 1.7 christos static u_int
866 1.7 christos ckgeom(const char *fname, u_int val, const char *msg)
867 1.7 christos {
868 1.7 christos if (!val)
869 1.7 christos errx(1, "%s: no default %s", fname, msg);
870 1.7 christos if (val > MAXU16)
871 1.7 christos errx(1, "%s: illegal %s", fname, msg);
872 1.7 christos return val;
873 1.7 christos }
874 1.1 christos
875 1.7 christos /*
876 1.7 christos * Convert and check a numeric option argument.
877 1.7 christos */
878 1.7 christos static u_int
879 1.7 christos argtou(const char *arg, u_int lo, u_int hi, const char *msg)
880 1.7 christos {
881 1.7 christos char *s;
882 1.7 christos u_long x;
883 1.1 christos
884 1.7 christos errno = 0;
885 1.7 christos x = strtoul(arg, &s, 0);
886 1.7 christos if (errno || !*arg || *s || x < lo || x > hi)
887 1.7 christos errx(1, "%s: bad %s", arg, msg);
888 1.7 christos return x;
889 1.7 christos }
890 1.1 christos
891 1.7 christos /*
892 1.7 christos * Check a volume label.
893 1.7 christos */
894 1.7 christos static int
895 1.7 christos oklabel(const char *src)
896 1.7 christos {
897 1.7 christos int c, i;
898 1.1 christos
899 1.7 christos for (i = 0; i <= 11; i++) {
900 1.7 christos c = (u_char)*src++;
901 1.7 christos if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
902 1.7 christos break;
903 1.7 christos }
904 1.7 christos return i && !c;
905 1.7 christos }
906 1.1 christos
907 1.7 christos /*
908 1.7 christos * Make a volume label.
909 1.7 christos */
910 1.7 christos static void
911 1.7 christos mklabel(u_int8_t *dest, const char *src)
912 1.7 christos {
913 1.7 christos int c, i;
914 1.1 christos
915 1.7 christos for (i = 0; i < 11; i++) {
916 1.7 christos c = *src ? toupper(*src++) : ' ';
917 1.7 christos *dest++ = !i && c == '\xe5' ? 5 : c;
918 1.7 christos }
919 1.7 christos }
920 1.1 christos
921 1.7 christos /*
922 1.7 christos * Copy string, padding with spaces.
923 1.7 christos */
924 1.7 christos static void
925 1.7 christos setstr(u_int8_t *dest, const char *src, size_t len)
926 1.7 christos {
927 1.7 christos while (len--)
928 1.7 christos *dest++ = *src ? *src++ : ' ';
929 1.7 christos }
930 1.1 christos
931 1.7 christos /*
932 1.7 christos * Print usage message.
933 1.7 christos */
934 1.7 christos static void
935 1.7 christos usage(void)
936 1.7 christos {
937 1.7 christos fprintf(stderr,
938 1.7 christos "Usage: %s [ -options ] special [disktype]\n", getprogname());
939 1.7 christos fprintf(stderr, "where the options are:\n");
940 1.7 christos fprintf(stderr, "\t-N don't create file system: "
941 1.7 christos "just print out parameters\n");
942 1.7 christos fprintf(stderr, "\t-B get bootstrap from file\n");
943 1.7 christos fprintf(stderr, "\t-F FAT type (12, 16, or 32)\n");
944 1.7 christos fprintf(stderr, "\t-I volume ID\n");
945 1.7 christos fprintf(stderr, "\t-L volume label\n");
946 1.7 christos fprintf(stderr, "\t-O OEM string\n");
947 1.7 christos fprintf(stderr, "\t-S bytes/sector\n");
948 1.7 christos fprintf(stderr, "\t-a sectors/FAT\n");
949 1.7 christos fprintf(stderr, "\t-b block size\n");
950 1.7 christos fprintf(stderr, "\t-c sectors/cluster\n");
951 1.7 christos fprintf(stderr, "\t-e root directory entries\n");
952 1.7 christos fprintf(stderr, "\t-f standard format\n");
953 1.7 christos fprintf(stderr, "\t-h drive heads\n");
954 1.7 christos fprintf(stderr, "\t-i file system info sector\n");
955 1.7 christos fprintf(stderr, "\t-k backup boot sector\n");
956 1.7 christos fprintf(stderr, "\t-m media descriptor\n");
957 1.7 christos fprintf(stderr, "\t-n number of FATs\n");
958 1.7 christos fprintf(stderr, "\t-o hidden sectors\n");
959 1.7 christos fprintf(stderr, "\t-r reserved sectors\n");
960 1.7 christos fprintf(stderr, "\t-s file system size (sectors)\n");
961 1.7 christos fprintf(stderr, "\t-u sectors/track\n");
962 1.7 christos exit(1);
963 1.1 christos }
964