blob: 5fbca8d71a4ac86e7a4c91292ccffafe1ba57b8d [file] [log] [blame]
San Mehat72eead42009-07-06 11:10:03 -07001/*
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#ifndef lint
29static const char rcsid[] =
Daniel Rosenberg33e7b132014-07-08 19:26:02 -070030 "$FreeBSD: src/sbin/newfs_msdos/newfs_msdos.c,v 1.33 2009/04/11 14:56:29 ed Exp $";
San Mehat72eead42009-07-06 11:10:03 -070031#endif /* not lint */
32
33#include <sys/param.h>
34
35#ifndef ANDROID
Daniel Rosenberg33e7b132014-07-08 19:26:02 -070036#include <sys/fdcio.h>
37#include <sys/disk.h>
38#include <sys/disklabel.h>
39#include <sys/mount.h>
San Mehat72eead42009-07-06 11:10:03 -070040#else
Daniel Rosenberg33e7b132014-07-08 19:26:02 -070041#include <stdarg.h>
42#include <linux/fs.h>
43#include <linux/hdreg.h>
San Mehat72eead42009-07-06 11:10:03 -070044#endif
45
46#include <sys/stat.h>
47#include <sys/time.h>
48
49#include <ctype.h>
50#include <err.h>
51#include <errno.h>
52#include <fcntl.h>
53#include <inttypes.h>
54#include <paths.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <time.h>
59#include <unistd.h>
60
Daniel Rosenberg33e7b132014-07-08 19:26:02 -070061#define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
62#define BPN 4 /* bits per nibble */
63#define NPB 2 /* nibbles per byte */
San Mehat72eead42009-07-06 11:10:03 -070064
Daniel Rosenberg33e7b132014-07-08 19:26:02 -070065#define DOSMAGIC 0xaa55 /* DOS magic number */
66#define MINBPS 512 /* minimum bytes per sector */
67#define MAXSPC 128 /* maximum sectors per cluster */
68#define MAXNFT 16 /* maximum number of FATs */
69#define DEFBLK 4096 /* default block size */
70#define DEFBLK16 2048 /* default block size FAT16 */
71#define DEFRDE 512 /* default root directory entries */
72#define RESFTE 2 /* reserved FAT entries */
73#define MINCLS12 1 /* minimum FAT12 clusters */
74#define MINCLS16 0x1000 /* minimum FAT16 clusters */
75#define MINCLS32 2 /* minimum FAT32 clusters */
76#define MAXCLS12 0xfed /* maximum FAT12 clusters */
77#define MAXCLS16 0xfff5 /* maximum FAT16 clusters */
78#define MAXCLS32 0xffffff5 /* maximum FAT32 clusters */
San Mehat72eead42009-07-06 11:10:03 -070079
Daniel Rosenberg33e7b132014-07-08 19:26:02 -070080#define mincls(fat) ((fat) == 12 ? MINCLS12 : \
81 (fat) == 16 ? MINCLS16 : \
82 MINCLS32)
San Mehat72eead42009-07-06 11:10:03 -070083
Daniel Rosenberg33e7b132014-07-08 19:26:02 -070084#define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \
85 (fat) == 16 ? MAXCLS16 : \
86 MAXCLS32)
San Mehat72eead42009-07-06 11:10:03 -070087
Daniel Rosenberg33e7b132014-07-08 19:26:02 -070088#define mk1(p, x) \
San Mehat72eead42009-07-06 11:10:03 -070089 (p) = (u_int8_t)(x)
90
Daniel Rosenberg33e7b132014-07-08 19:26:02 -070091#define mk2(p, x) \
92 (p)[0] = (u_int8_t)(x), \
San Mehat72eead42009-07-06 11:10:03 -070093 (p)[1] = (u_int8_t)((x) >> 010)
94
Daniel Rosenberg33e7b132014-07-08 19:26:02 -070095#define mk4(p, x) \
96 (p)[0] = (u_int8_t)(x), \
97 (p)[1] = (u_int8_t)((x) >> 010), \
98 (p)[2] = (u_int8_t)((x) >> 020), \
San Mehat72eead42009-07-06 11:10:03 -070099 (p)[3] = (u_int8_t)((x) >> 030)
100
101#define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
102#define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg)
103#define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg)
104#define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
105
106struct bs {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700107 u_int8_t jmp[3]; /* bootstrap entry point */
108 u_int8_t oem[8]; /* OEM name and version */
San Mehat72eead42009-07-06 11:10:03 -0700109};
110
111struct bsbpb {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700112 u_int8_t bps[2]; /* bytes per sector */
113 u_int8_t spc; /* sectors per cluster */
114 u_int8_t res[2]; /* reserved sectors */
115 u_int8_t nft; /* number of FATs */
116 u_int8_t rde[2]; /* root directory entries */
117 u_int8_t sec[2]; /* total sectors */
118 u_int8_t mid; /* media descriptor */
119 u_int8_t spf[2]; /* sectors per FAT */
120 u_int8_t spt[2]; /* sectors per track */
121 u_int8_t hds[2]; /* drive heads */
122 u_int8_t hid[4]; /* hidden sectors */
123 u_int8_t bsec[4]; /* big total sectors */
San Mehat72eead42009-07-06 11:10:03 -0700124};
125
126struct bsxbpb {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700127 u_int8_t bspf[4]; /* big sectors per FAT */
128 u_int8_t xflg[2]; /* FAT control flags */
129 u_int8_t vers[2]; /* file system version */
130 u_int8_t rdcl[4]; /* root directory start cluster */
131 u_int8_t infs[2]; /* file system info sector */
132 u_int8_t bkbs[2]; /* backup boot sector */
133 u_int8_t rsvd[12]; /* reserved */
San Mehat72eead42009-07-06 11:10:03 -0700134};
135
136struct bsx {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700137 u_int8_t drv; /* drive number */
138 u_int8_t rsvd; /* reserved */
139 u_int8_t sig; /* extended boot signature */
140 u_int8_t volid[4]; /* volume ID number */
141 u_int8_t label[11]; /* volume label */
142 u_int8_t type[8]; /* file system type */
San Mehat72eead42009-07-06 11:10:03 -0700143};
144
145struct de {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700146 u_int8_t namext[11]; /* name and extension */
147 u_int8_t attr; /* attributes */
148 u_int8_t rsvd[10]; /* reserved */
149 u_int8_t time[2]; /* creation time */
150 u_int8_t date[2]; /* creation date */
151 u_int8_t clus[2]; /* starting cluster */
152 u_int8_t size[4]; /* size */
San Mehat72eead42009-07-06 11:10:03 -0700153};
154
155struct bpb {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700156 u_int bps; /* bytes per sector */
157 u_int spc; /* sectors per cluster */
158 u_int res; /* reserved sectors */
159 u_int nft; /* number of FATs */
160 u_int rde; /* root directory entries */
161 u_int sec; /* total sectors */
162 u_int mid; /* media descriptor */
163 u_int spf; /* sectors per FAT */
164 u_int spt; /* sectors per track */
165 u_int hds; /* drive heads */
166 u_int hid; /* hidden sectors */
167 u_int bsec; /* big total sectors */
168 u_int bspf; /* big sectors per FAT */
169 u_int rdcl; /* root directory start cluster */
170 u_int infs; /* file system info sector */
171 u_int bkbs; /* backup boot sector */
San Mehat72eead42009-07-06 11:10:03 -0700172};
173
174#define BPBGAP 0, 0, 0, 0, 0, 0
175
176static struct {
177 const char *name;
178 struct bpb bpb;
179} const stdfmt[] = {
180 {"160", {512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1, BPBGAP}},
181 {"180", {512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1, BPBGAP}},
182 {"320", {512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2, BPBGAP}},
183 {"360", {512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2, BPBGAP}},
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700184 {"640", {512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2, BPBGAP}},
San Mehat72eead42009-07-06 11:10:03 -0700185 {"720", {512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2, BPBGAP}},
186 {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2, BPBGAP}},
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700187 {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2, BPBGAP}},
San Mehat72eead42009-07-06 11:10:03 -0700188 {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2, BPBGAP}},
189 {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2, BPBGAP}}
190};
191
192static const u_int8_t bootcode[] = {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700193 0xfa, /* cli */
194 0x31, 0xc0, /* xor ax,ax */
195 0x8e, 0xd0, /* mov ss,ax */
196 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
197 0xfb, /* sti */
198 0x8e, 0xd8, /* mov ds,ax */
199 0xe8, 0x00, 0x00, /* call $ + 3 */
200 0x5e, /* pop si */
201 0x83, 0xc6, 0x19, /* add si,+19h */
202 0xbb, 0x07, 0x00, /* mov bx,0007h */
203 0xfc, /* cld */
204 0xac, /* lodsb */
205 0x84, 0xc0, /* test al,al */
206 0x74, 0x06, /* jz $ + 8 */
207 0xb4, 0x0e, /* mov ah,0eh */
208 0xcd, 0x10, /* int 10h */
209 0xeb, 0xf5, /* jmp $ - 9 */
210 0x30, 0xe4, /* xor ah,ah */
211 0xcd, 0x16, /* int 16h */
212 0xcd, 0x19, /* int 19h */
San Mehat72eead42009-07-06 11:10:03 -0700213 0x0d, 0x0a,
214 'N', 'o', 'n', '-', 's', 'y', 's', 't',
215 'e', 'm', ' ', 'd', 'i', 's', 'k',
216 0x0d, 0x0a,
217 'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
218 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
219 ' ', 'r', 'e', 'b', 'o', 'o', 't',
220 0x0d, 0x0a,
221 0
222};
223
224static void check_mounted(const char *, mode_t);
225static void getstdfmt(const char *, struct bpb *);
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700226static void getdiskinfo(int, const char *, const char *, int, struct bpb *);
San Mehat72eead42009-07-06 11:10:03 -0700227static void print_bpb(struct bpb *);
228static u_int ckgeom(const char *, u_int, const char *);
229static u_int argtou(const char *, u_int, u_int, const char *);
230static off_t argtooff(const char *, const char *);
231static int oklabel(const char *);
232static void mklabel(u_int8_t *, const char *);
233static void setstr(u_int8_t *, const char *, size_t);
234static void usage(void);
235
San Mehat72eead42009-07-06 11:10:03 -0700236/*
237 * Construct a FAT12, FAT16, or FAT32 file system.
238 */
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700239int newfs_msdos_main(int argc, char *argv[])
San Mehat72eead42009-07-06 11:10:03 -0700240{
241 static const char opts[] = "@:NB:C:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
242 const char *opt_B = NULL, *opt_L = NULL, *opt_O = NULL, *opt_f = NULL;
243 u_int opt_F = 0, opt_I = 0, opt_S = 0, opt_a = 0, opt_b = 0, opt_c = 0;
244 u_int opt_e = 0, opt_h = 0, opt_i = 0, opt_k = 0, opt_m = 0, opt_n = 0;
245 u_int opt_o = 0, opt_r = 0, opt_s = 0, opt_u = 0;
246 int opt_N = 0;
247 int Iflag = 0, mflag = 0, oflag = 0;
248 char buf[MAXPATHLEN];
249 struct stat sb;
250 struct timeval tv;
251 struct bpb bpb;
252 struct tm *tm;
253 struct bs *bs;
254 struct bsbpb *bsbpb;
255 struct bsxbpb *bsxbpb;
256 struct bsx *bsx;
257 struct de *de;
258 u_int8_t *img;
259 const char *fname, *dtype, *bname;
260 ssize_t n;
261 time_t now;
262 u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
263 int ch, fd, fd1;
264 off_t opt_create = 0, opt_ofs = 0;
265
266 while ((ch = getopt(argc, argv, opts)) != -1)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700267 switch (ch) {
268 case '@':
269 opt_ofs = argtooff(optarg, "offset");
270 break;
271 case 'N':
272 opt_N = 1;
273 break;
274 case 'B':
275 opt_B = optarg;
276 break;
277 case 'C':
278 opt_create = argtooff(optarg, "create size");
279 break;
280 case 'F':
281 if (strcmp(optarg, "12") && strcmp(optarg, "16") && strcmp(optarg, "32"))
282 errx(1, "%s: bad FAT type", optarg);
283 opt_F = atoi(optarg);
284 break;
285 case 'I':
286 opt_I = argto4(optarg, 0, "volume ID");
287 Iflag = 1;
288 break;
289 case 'L':
290 if (!oklabel(optarg))
291 errx(1, "%s: bad volume label", optarg);
292 opt_L = optarg;
293 break;
294 case 'O':
295 if (strlen(optarg) > 8)
296 errx(1, "%s: bad OEM string", optarg);
297 opt_O = optarg;
298 break;
299 case 'S':
300 opt_S = argto2(optarg, 1, "bytes/sector");
301 break;
302 case 'a':
303 opt_a = argto4(optarg, 1, "sectors/FAT");
304 break;
305 case 'b':
306 opt_b = argtox(optarg, 1, "block size");
307 opt_c = 0;
308 break;
309 case 'c':
310 opt_c = argto1(optarg, 1, "sectors/cluster");
311 opt_b = 0;
312 break;
313 case 'e':
314 opt_e = argto2(optarg, 1, "directory entries");
315 break;
316 case 'f':
317 opt_f = optarg;
318 break;
319 case 'h':
320 opt_h = argto2(optarg, 1, "drive heads");
321 break;
322 case 'i':
323 opt_i = argto2(optarg, 1, "info sector");
324 break;
325 case 'k':
326 opt_k = argto2(optarg, 1, "backup sector");
327 break;
328 case 'm':
329 opt_m = argto1(optarg, 0, "media descriptor");
330 mflag = 1;
331 break;
332 case 'n':
333 opt_n = argto1(optarg, 1, "number of FATs");
334 break;
335 case 'o':
336 opt_o = argto4(optarg, 0, "hidden sectors");
337 oflag = 1;
338 break;
339 case 'r':
340 opt_r = argto2(optarg, 1, "reserved sectors");
341 break;
342 case 's':
343 opt_s = argto4(optarg, 1, "file system size");
344 break;
345 case 'u':
346 opt_u = argto2(optarg, 1, "sectors/track");
347 break;
348 default:
349 usage();
350 }
San Mehat72eead42009-07-06 11:10:03 -0700351 argc -= optind;
352 argv += optind;
353 if (argc < 1 || argc > 2)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700354 usage();
San Mehat72eead42009-07-06 11:10:03 -0700355 fname = *argv++;
356 if (!opt_create && !strchr(fname, '/')) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700357 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
358 if (!(fname = strdup(buf)))
359 err(1, "%s", buf);
San Mehat72eead42009-07-06 11:10:03 -0700360 }
361 dtype = *argv;
362 if (opt_create) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700363 if (opt_N)
364 errx(1, "create (-C) is incompatible with -N");
365 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644);
366 if (fd == -1)
367 errx(1, "failed to create %s", fname);
368 if (ftruncate(fd, opt_create))
369 errx(1, "failed to initialize %jd bytes", (intmax_t)opt_create);
San Mehat72eead42009-07-06 11:10:03 -0700370 } else if ((fd = open(fname, opt_N ? O_RDONLY : O_RDWR)) == -1)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700371 err(1, "%s", fname);
San Mehat72eead42009-07-06 11:10:03 -0700372 if (fstat(fd, &sb))
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700373 err(1, "%s", fname);
San Mehat72eead42009-07-06 11:10:03 -0700374 if (opt_create) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700375 if (!S_ISREG(sb.st_mode))
376 warnx("warning, %s is not a regular file", fname);
San Mehat72eead42009-07-06 11:10:03 -0700377 } else {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700378 if (!S_ISCHR(sb.st_mode))
379 warnx("warning, %s is not a character device", fname);
San Mehat72eead42009-07-06 11:10:03 -0700380 }
381 if (!opt_N)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700382 check_mounted(fname, sb.st_mode);
San Mehat72eead42009-07-06 11:10:03 -0700383 if (opt_ofs && opt_ofs != lseek(fd, opt_ofs, SEEK_SET))
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700384 errx(1, "cannot seek to %jd", (intmax_t)opt_ofs);
San Mehat72eead42009-07-06 11:10:03 -0700385 memset(&bpb, 0, sizeof(bpb));
386 if (opt_f) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700387 getstdfmt(opt_f, &bpb);
388 bpb.bsec = bpb.sec;
389 bpb.sec = 0;
390 bpb.bspf = bpb.spf;
391 bpb.spf = 0;
San Mehat72eead42009-07-06 11:10:03 -0700392 }
393 if (opt_h)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700394 bpb.hds = opt_h;
San Mehat72eead42009-07-06 11:10:03 -0700395 if (opt_u)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700396 bpb.spt = opt_u;
San Mehat72eead42009-07-06 11:10:03 -0700397 if (opt_S)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700398 bpb.bps = opt_S;
San Mehat72eead42009-07-06 11:10:03 -0700399 if (opt_s)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700400 bpb.bsec = opt_s;
San Mehat72eead42009-07-06 11:10:03 -0700401 if (oflag)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700402 bpb.hid = opt_o;
San Mehat72eead42009-07-06 11:10:03 -0700403 if (!(opt_f || (opt_h && opt_u && opt_S && opt_s && oflag))) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700404 off_t delta;
405 getdiskinfo(fd, fname, dtype, oflag, &bpb);
San Mehat661aff62010-03-03 12:40:21 -0800406 if (opt_s) {
407 bpb.bsec = opt_s;
408 }
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700409 bpb.bsec -= (opt_ofs / bpb.bps);
410 delta = bpb.bsec % bpb.spt;
411 if (delta != 0) {
412 warnx("trim %d sectors from %d to adjust to a multiple of %d",
413 (int)delta, bpb.bsec, bpb.spt);
414 bpb.bsec -= delta;
415 }
416 if (bpb.spc == 0) { /* set defaults */
417 if (bpb.bsec <= 6000) /* about 3MB -> 512 bytes */
418 bpb.spc = 1;
419 else if (bpb.bsec <= (1<<17)) /* 64M -> 4k */
420 bpb.spc = 8;
421 else if (bpb.bsec <= (1<<19)) /* 256M -> 8k */
422 bpb.spc = 16;
423 else if (bpb.bsec <= (1<<22)) /* 2G -> 16k, some versions of windows
424 require a minimum of 65527 clusters */
425 bpb.spc = 32;
426 else
427 bpb.spc = 64; /* otherwise 32k */
428 }
San Mehat72eead42009-07-06 11:10:03 -0700429 }
430 if (!powerof2(bpb.bps))
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700431 errx(1, "bytes/sector (%u) is not a power of 2", bpb.bps);
San Mehat72eead42009-07-06 11:10:03 -0700432 if (bpb.bps < MINBPS)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700433 errx(1, "bytes/sector (%u) is too small; minimum is %u",
434 bpb.bps, MINBPS);
San Mehat72eead42009-07-06 11:10:03 -0700435 if (!(fat = opt_F)) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700436 if (opt_f)
437 fat = 12;
438 else if (!opt_e && (opt_i || opt_k))
439 fat = 32;
San Mehat72eead42009-07-06 11:10:03 -0700440 }
441 if ((fat == 32 && opt_e) || (fat != 32 && (opt_i || opt_k)))
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700442 errx(1, "-%c is not a legal FAT%s option",
443 fat == 32 ? 'e' : opt_i ? 'i' : 'k',
444 fat == 32 ? "32" : "12/16");
San Mehat72eead42009-07-06 11:10:03 -0700445 if (opt_f && fat == 32)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700446 bpb.rde = 0;
San Mehat72eead42009-07-06 11:10:03 -0700447 if (opt_b) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700448 if (!powerof2(opt_b))
449 errx(1, "block size (%u) is not a power of 2", opt_b);
450 if (opt_b < bpb.bps)
451 errx(1, "block size (%u) is too small; minimum is %u",
452 opt_b, bpb.bps);
453 if (opt_b > bpb.bps * MAXSPC)
454 errx(1, "block size (%u) is too large; maximum is %u", opt_b, bpb.bps * MAXSPC);
455 bpb.spc = opt_b / bpb.bps;
San Mehat72eead42009-07-06 11:10:03 -0700456 }
457 if (opt_c) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700458 if (!powerof2(opt_c))
459 errx(1, "sectors/cluster (%u) is not a power of 2", opt_c);
460 bpb.spc = opt_c;
San Mehat72eead42009-07-06 11:10:03 -0700461 }
462 if (opt_r)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700463 bpb.res = opt_r;
San Mehat72eead42009-07-06 11:10:03 -0700464 if (opt_n) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700465 if (opt_n > MAXNFT)
466 errx(1, "number of FATs (%u) is too large; maximum is %u", opt_n, MAXNFT);
467 bpb.nft = opt_n;
San Mehat72eead42009-07-06 11:10:03 -0700468 }
469 if (opt_e)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700470 bpb.rde = opt_e;
San Mehat72eead42009-07-06 11:10:03 -0700471 if (mflag) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700472 if (opt_m < 0xf0)
473 errx(1, "illegal media descriptor (%#x)", opt_m);
474 bpb.mid = opt_m;
San Mehat72eead42009-07-06 11:10:03 -0700475 }
476 if (opt_a)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700477 bpb.bspf = opt_a;
San Mehat72eead42009-07-06 11:10:03 -0700478 if (opt_i)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700479 bpb.infs = opt_i;
San Mehat72eead42009-07-06 11:10:03 -0700480 if (opt_k)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700481 bpb.bkbs = opt_k;
San Mehat72eead42009-07-06 11:10:03 -0700482 bss = 1;
483 bname = NULL;
484 fd1 = -1;
485 if (opt_B) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700486 bname = opt_B;
487 if (!strchr(bname, '/')) {
488 snprintf(buf, sizeof(buf), "/boot/%s", bname);
489 if (!(bname = strdup(buf)))
490 err(1, "%s", buf);
491 }
492 if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb))
493 err(1, "%s", bname);
494 if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||
495 sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16)
496 errx(1, "%s: inappropriate file type or format", bname);
497 bss = sb.st_size / bpb.bps;
San Mehat72eead42009-07-06 11:10:03 -0700498 }
499 if (!bpb.nft)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700500 bpb.nft = 2;
San Mehat72eead42009-07-06 11:10:03 -0700501 if (!fat) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700502 if (bpb.bsec < (bpb.res ? bpb.res : bss) +
503 howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *
504 ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *
505 bpb.nft +
506 howmany(bpb.rde ? bpb.rde : DEFRDE,
507 bpb.bps / sizeof(struct de)) +
508 (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *
509 (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))
510 fat = 12;
511 else if (bpb.rde || bpb.bsec <
512 (bpb.res ? bpb.res : bss) +
513 howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +
514 howmany(DEFRDE, bpb.bps / sizeof(struct de)) +
515 (MAXCLS16 + 1) *
516 (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))
517 fat = 16;
518 else
519 fat = 32;
San Mehat72eead42009-07-06 11:10:03 -0700520 }
521 x = bss;
522 if (fat == 32) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700523 if (!bpb.infs) {
524 if (x == MAXU16 || x == bpb.bkbs)
525 errx(1, "no room for info sector");
526 bpb.infs = x;
527 }
528 if (bpb.infs != MAXU16 && x <= bpb.infs)
529 x = bpb.infs + 1;
530 if (!bpb.bkbs) {
531 if (x == MAXU16)
532 errx(1, "no room for backup sector");
533 bpb.bkbs = x;
534 } else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs)
535 errx(1, "backup sector would overwrite info sector");
536 if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)
537 x = bpb.bkbs + 1;
San Mehat72eead42009-07-06 11:10:03 -0700538 }
539 if (!bpb.res)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700540 bpb.res = fat == 32 ? MAX(x, MAX(16384 / bpb.bps, 4)) : x;
San Mehat72eead42009-07-06 11:10:03 -0700541 else if (bpb.res < x)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700542 errx(1, "too few reserved sectors");
San Mehat72eead42009-07-06 11:10:03 -0700543 if (fat != 32 && !bpb.rde)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700544 bpb.rde = DEFRDE;
San Mehat72eead42009-07-06 11:10:03 -0700545 rds = howmany(bpb.rde, bpb.bps / sizeof(struct de));
546 if (!bpb.spc)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700547 for (bpb.spc = howmany(fat == 16 ? DEFBLK16 : DEFBLK, bpb.bps);
548 bpb.spc < MAXSPC &&
549 bpb.res +
550 howmany((RESFTE + maxcls(fat)) * (fat / BPN),
551 bpb.bps * NPB) * bpb.nft +
552 rds +
553 (u_int64_t)(maxcls(fat) + 1) * bpb.spc <= bpb.bsec;
554 bpb.spc <<= 1);
San Mehat72eead42009-07-06 11:10:03 -0700555 if (fat != 32 && bpb.bspf > MAXU16)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700556 errx(1, "too many sectors/FAT for FAT12/16");
San Mehat72eead42009-07-06 11:10:03 -0700557 x1 = bpb.res + rds;
558 x = bpb.bspf ? bpb.bspf : 1;
559 if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700560 errx(1, "meta data exceeds file system size");
San Mehat72eead42009-07-06 11:10:03 -0700561 x1 += x * bpb.nft;
562 x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700563 (bpb.spc * bpb.bps * NPB + fat / BPN * bpb.nft);
564 x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN), bpb.bps * NPB);
San Mehat72eead42009-07-06 11:10:03 -0700565 if (!bpb.bspf) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700566 bpb.bspf = x2;
567 x1 += (bpb.bspf - 1) * bpb.nft;
San Mehat72eead42009-07-06 11:10:03 -0700568 }
569 cls = (bpb.bsec - x1) / bpb.spc;
570 x = (u_int64_t)bpb.bspf * bpb.bps * NPB / (fat / BPN) - RESFTE;
571 if (cls > x)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700572 cls = x;
San Mehat72eead42009-07-06 11:10:03 -0700573 if (bpb.bspf < x2)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700574 warnx("warning: sectors/FAT limits file system to %u clusters", cls);
San Mehat72eead42009-07-06 11:10:03 -0700575 if (cls < mincls(fat))
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700576 errx(1, "%u clusters too few clusters for FAT%u, need %u", cls, fat, mincls(fat));
San Mehat72eead42009-07-06 11:10:03 -0700577 if (cls > maxcls(fat)) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700578 cls = maxcls(fat);
579 bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;
580 warnx("warning: FAT type limits file system to %u sectors", bpb.bsec);
San Mehat72eead42009-07-06 11:10:03 -0700581 }
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700582 printf("%s: %u sector%s in %u FAT%u cluster%s (%u bytes/cluster)\n",
583 fname, cls * bpb.spc, cls * bpb.spc == 1 ? "" : "s", cls, fat,
584 cls == 1 ? "" : "s", bpb.bps * bpb.spc);
San Mehat72eead42009-07-06 11:10:03 -0700585 if (!bpb.mid)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700586 bpb.mid = !bpb.hid ? 0xf0 : 0xf8;
San Mehat72eead42009-07-06 11:10:03 -0700587 if (fat == 32)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700588 bpb.rdcl = RESFTE;
San Mehat72eead42009-07-06 11:10:03 -0700589 if (bpb.hid + bpb.bsec <= MAXU16) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700590 bpb.sec = bpb.bsec;
591 bpb.bsec = 0;
San Mehat72eead42009-07-06 11:10:03 -0700592 }
593 if (fat != 32) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700594 bpb.spf = bpb.bspf;
595 bpb.bspf = 0;
San Mehat72eead42009-07-06 11:10:03 -0700596 }
597 print_bpb(&bpb);
598 if (!opt_N) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700599 gettimeofday(&tv, NULL);
600 now = tv.tv_sec;
601 tm = localtime(&now);
602 if (!(img = malloc(bpb.bps)))
603 err(1, "%u", bpb.bps);
604 dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
605 for (lsn = 0; lsn < dir + (fat == 32 ? bpb.spc : rds); lsn++) {
606 x = lsn;
607 if (opt_B && fat == 32 && bpb.bkbs != MAXU16 && bss <= bpb.bkbs && x >= bpb.bkbs) {
608 x -= bpb.bkbs;
609 if (!x && lseek(fd1, opt_ofs, SEEK_SET))
610 err(1, "%s", bname);
611 }
612 if (opt_B && x < bss) {
613 if ((n = read(fd1, img, bpb.bps)) == -1)
614 err(1, "%s", bname);
615 if ((unsigned)n != bpb.bps)
616 errx(1, "%s: can't read sector %u", bname, x);
617 } else
618 memset(img, 0, bpb.bps);
619 if (!lsn || (fat == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {
620 x1 = sizeof(struct bs);
621 bsbpb = (struct bsbpb *)(img + x1);
622 mk2(bsbpb->bps, bpb.bps);
623 mk1(bsbpb->spc, bpb.spc);
624 mk2(bsbpb->res, bpb.res);
625 mk1(bsbpb->nft, bpb.nft);
626 mk2(bsbpb->rde, bpb.rde);
627 mk2(bsbpb->sec, bpb.sec);
628 mk1(bsbpb->mid, bpb.mid);
629 mk2(bsbpb->spf, bpb.spf);
630 mk2(bsbpb->spt, bpb.spt);
631 mk2(bsbpb->hds, bpb.hds);
632 mk4(bsbpb->hid, bpb.hid);
633 mk4(bsbpb->bsec, bpb.bsec);
634 x1 += sizeof(struct bsbpb);
635 if (fat == 32) {
636 bsxbpb = (struct bsxbpb *)(img + x1);
637 mk4(bsxbpb->bspf, bpb.bspf);
638 mk2(bsxbpb->xflg, 0);
639 mk2(bsxbpb->vers, 0);
640 mk4(bsxbpb->rdcl, bpb.rdcl);
641 mk2(bsxbpb->infs, bpb.infs);
642 mk2(bsxbpb->bkbs, bpb.bkbs);
643 x1 += sizeof(struct bsxbpb);
644 }
645 bsx = (struct bsx *)(img + x1);
646 mk1(bsx->sig, 0x29);
647 if (Iflag)
648 x = opt_I;
649 else
650 x = (((u_int)(1 + tm->tm_mon) << 8 |
651 (u_int)tm->tm_mday) +
652 ((u_int)tm->tm_sec << 8 |
653 (u_int)(tv.tv_usec / 10))) << 16 |
654 ((u_int)(1900 + tm->tm_year) +
655 ((u_int)tm->tm_hour << 8 |
656 (u_int)tm->tm_min));
657 mk4(bsx->volid, x);
658 mklabel(bsx->label, opt_L ? opt_L : "NO NAME");
659 sprintf(buf, "FAT%u", fat);
660 setstr(bsx->type, buf, sizeof(bsx->type));
661 if (!opt_B) {
662 x1 += sizeof(struct bsx);
663 bs = (struct bs *)img;
664 mk1(bs->jmp[0], 0xeb);
665 mk1(bs->jmp[1], x1 - 2);
666 mk1(bs->jmp[2], 0x90);
667 setstr(bs->oem, opt_O ? opt_O : "BSD 4.4",
668 sizeof(bs->oem));
669 memcpy(img + x1, bootcode, sizeof(bootcode));
670 mk2(img + MINBPS - 2, DOSMAGIC);
671 }
672 } else if (fat == 32 && bpb.infs != MAXU16 &&
673 (lsn == bpb.infs || (bpb.bkbs != MAXU16 &&
674 lsn == bpb.bkbs + bpb.infs))) {
675 mk4(img, 0x41615252);
676 mk4(img + MINBPS - 28, 0x61417272);
677 mk4(img + MINBPS - 24, 0xffffffff);
678 mk4(img + MINBPS - 20, bpb.rdcl);
679 mk2(img + MINBPS - 2, DOSMAGIC);
680 } else if (lsn >= bpb.res && lsn < dir &&
681 !((lsn - bpb.res) % (bpb.spf ? bpb.spf : bpb.bspf))) {
682 mk1(img[0], bpb.mid);
683 for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
684 mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
685 } else if (lsn == dir && opt_L) {
686 de = (struct de *)img;
687 mklabel(de->namext, opt_L);
688 mk1(de->attr, 050);
689 x = (u_int)tm->tm_hour << 11 |
690 (u_int)tm->tm_min << 5 |
691 (u_int)tm->tm_sec >> 1;
692 mk2(de->time, x);
693 x = (u_int)(tm->tm_year - 80) << 9 |
694 (u_int)(tm->tm_mon + 1) << 5 |
695 (u_int)tm->tm_mday;
696 mk2(de->date, x);
697 }
698 if ((n = write(fd, img, bpb.bps)) == -1)
699 err(1, "%s", fname);
700 if ((unsigned)n != bpb.bps) {
701 errx(1, "%s: can't write sector %u", fname, lsn);
San Mehateab453c2010-01-10 11:10:21 -0800702 exit(1);
703 }
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700704 }
San Mehat72eead42009-07-06 11:10:03 -0700705 }
706 return 0;
707}
708
709/*
710 * Exit with error if file system is mounted.
711 */
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700712static void check_mounted(const char *fname, mode_t mode)
San Mehat72eead42009-07-06 11:10:03 -0700713{
Mark Salyzynaa907762014-05-08 09:31:43 -0700714#ifdef ANDROID
715 warnx("Skipping mount checks");
716#else
San Mehat72eead42009-07-06 11:10:03 -0700717 struct statfs *mp;
718 const char *s1, *s2;
719 size_t len;
720 int n, r;
721
San Mehat72eead42009-07-06 11:10:03 -0700722 if (!(n = getmntinfo(&mp, MNT_NOWAIT)))
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700723 err(1, "getmntinfo");
San Mehat72eead42009-07-06 11:10:03 -0700724 len = strlen(_PATH_DEV);
725 s1 = fname;
726 if (!strncmp(s1, _PATH_DEV, len))
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700727 s1 += len;
San Mehat72eead42009-07-06 11:10:03 -0700728 r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
729 for (; n--; mp++) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700730 s2 = mp->f_mntfromname;
731 if (!strncmp(s2, _PATH_DEV, len))
732 s2 += len;
733 if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) || !strcmp(s1, s2))
734 errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
San Mehat72eead42009-07-06 11:10:03 -0700735 }
736#endif
737}
738
739/*
740 * Get a standard format.
741 */
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700742static void getstdfmt(const char *fmt, struct bpb *bpb)
San Mehat72eead42009-07-06 11:10:03 -0700743{
744 u_int x, i;
745
746 x = sizeof(stdfmt) / sizeof(stdfmt[0]);
747 for (i = 0; i < x && strcmp(fmt, stdfmt[i].name); i++);
748 if (i == x)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700749 errx(1, "%s: unknown standard format", fmt);
San Mehat72eead42009-07-06 11:10:03 -0700750 *bpb = stdfmt[i].bpb;
751}
752
753/*
754 * Get disk slice, partition, and geometry information.
755 */
756
757#ifdef ANDROID
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700758static void getdiskinfo(int fd, const char *fname, const char *dtype,
759 __unused int oflag,struct bpb *bpb)
San Mehat72eead42009-07-06 11:10:03 -0700760{
761 struct hd_geometry geom;
762
763 if (ioctl(fd, BLKSSZGET, &bpb->bps)) {
San Mehatff3bcd02010-01-05 13:49:11 -0800764 fprintf(stderr, "Error getting bytes / sector (%s)\n", strerror(errno));
San Mehat72eead42009-07-06 11:10:03 -0700765 exit(1);
766 }
767
768 ckgeom(fname, bpb->bps, "bytes/sector");
769
770 if (ioctl(fd, BLKGETSIZE, &bpb->bsec)) {
San Mehatff3bcd02010-01-05 13:49:11 -0800771 fprintf(stderr, "Error getting blocksize (%s)\n", strerror(errno));
San Mehat72eead42009-07-06 11:10:03 -0700772 exit(1);
773 }
774
775 if (ioctl(fd, HDIO_GETGEO, &geom)) {
San Mehatff3bcd02010-01-05 13:49:11 -0800776 fprintf(stderr, "Error getting gemoetry (%s) - trying sane values\n", strerror(errno));
777 geom.heads = 64;
778 geom.sectors = 63;
San Mehat72eead42009-07-06 11:10:03 -0700779 }
780
San Mehateab453c2010-01-10 11:10:21 -0800781 if (!geom.heads) {
782 printf("Bogus heads from kernel - setting sane value\n");
783 geom.heads = 64;
784 }
785
786 if (!geom.sectors) {
787 printf("Bogus sectors from kernel - setting sane value\n");
788 geom.sectors = 63;
789 }
790
San Mehat72eead42009-07-06 11:10:03 -0700791 bpb->spt = geom.sectors;
792 ckgeom(fname, bpb->spt, "sectors/track");
793
794 bpb->hds = geom.heads;
795 ckgeom(fname, bpb->hds, "drive heads");
796}
797
798#else
799
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700800static void getdiskinfo(int fd, const char *fname, const char *dtype,
801 __unused int oflag, struct bpb *bpb)
San Mehat72eead42009-07-06 11:10:03 -0700802{
803 struct disklabel *lp, dlp;
804 struct fd_type type;
805 off_t ms, hs = 0;
806
807 lp = NULL;
808
809 /* If the user specified a disk type, try to use that */
810 if (dtype != NULL) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700811 lp = getdiskbyname(dtype);
San Mehat72eead42009-07-06 11:10:03 -0700812 }
813
814 /* Maybe it's a floppy drive */
815 if (lp == NULL) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700816 if (ioctl(fd, DIOCGMEDIASIZE, &ms) == -1) {
817 struct stat st;
San Mehat72eead42009-07-06 11:10:03 -0700818
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700819 if (fstat(fd, &st))
820 err(1, "Cannot get disk size");
821 /* create a fake geometry for a file image */
822 ms = st.st_size;
823 dlp.d_secsize = 512;
824 dlp.d_nsectors = 63;
825 dlp.d_ntracks = 255;
826 dlp.d_secperunit = ms / dlp.d_secsize;
827 lp = &dlp;
828 } else if (ioctl(fd, FD_GTYPE, &type) != -1) {
829 dlp.d_secsize = 128 << type.secsize;
830 dlp.d_nsectors = type.sectrac;
831 dlp.d_ntracks = type.heads;
832 dlp.d_secperunit = ms / dlp.d_secsize;
833 lp = &dlp;
834 }
San Mehat72eead42009-07-06 11:10:03 -0700835 }
836
837 /* Maybe it's a fixed drive */
838 if (lp == NULL) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700839 if (ioctl(fd, DIOCGDINFO, &dlp) == -1) {
840 if (bpb->bps == 0 && ioctl(fd, DIOCGSECTORSIZE, &dlp.d_secsize) == -1)
841 errx(1, "Cannot get sector size, %s", strerror(errno));
San Mehat72eead42009-07-06 11:10:03 -0700842
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700843 /* XXX Should we use bpb->bps if it's set? */
844 dlp.d_secperunit = ms / dlp.d_secsize;
San Mehat72eead42009-07-06 11:10:03 -0700845
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700846 if (bpb->spt == 0 && ioctl(fd, DIOCGFWSECTORS, &dlp.d_nsectors) == -1) {
847 warnx("Cannot get number of sectors per track, %s", strerror(errno));
848 dlp.d_nsectors = 63;
849 }
850 if (bpb->hds == 0 && ioctl(fd, DIOCGFWHEADS, &dlp.d_ntracks) == -1) {
851 warnx("Cannot get number of heads, %s", strerror(errno));
852 if (dlp.d_secperunit <= 63*1*1024)
853 dlp.d_ntracks = 1;
854 else if (dlp.d_secperunit <= 63*16*1024)
855 dlp.d_ntracks = 16;
856 else
857 dlp.d_ntracks = 255;
858 }
859 }
San Mehat72eead42009-07-06 11:10:03 -0700860
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700861 hs = (ms / dlp.d_secsize) - dlp.d_secperunit;
862 lp = &dlp;
San Mehat72eead42009-07-06 11:10:03 -0700863 }
864
865 if (bpb->bps == 0)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700866 bpb->bps = ckgeom(fname, lp->d_secsize, "bytes/sector");
San Mehat72eead42009-07-06 11:10:03 -0700867 if (bpb->spt == 0)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700868 bpb->spt = ckgeom(fname, lp->d_nsectors, "sectors/track");
San Mehat72eead42009-07-06 11:10:03 -0700869 if (bpb->hds == 0)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700870 bpb->hds = ckgeom(fname, lp->d_ntracks, "drive heads");
San Mehat72eead42009-07-06 11:10:03 -0700871 if (bpb->bsec == 0)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700872 bpb->bsec = lp->d_secperunit;
San Mehat72eead42009-07-06 11:10:03 -0700873 if (bpb->hid == 0)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700874 bpb->hid = hs;
San Mehat72eead42009-07-06 11:10:03 -0700875}
876#endif
877
878/*
879 * Print out BPB values.
880 */
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700881static void print_bpb(struct bpb *bpb)
San Mehat72eead42009-07-06 11:10:03 -0700882{
883 printf("bps=%u spc=%u res=%u nft=%u", bpb->bps, bpb->spc, bpb->res,
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700884 bpb->nft);
San Mehat72eead42009-07-06 11:10:03 -0700885 if (bpb->rde)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700886 printf(" rde=%u", bpb->rde);
San Mehat72eead42009-07-06 11:10:03 -0700887 if (bpb->sec)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700888 printf(" sec=%u", bpb->sec);
San Mehat72eead42009-07-06 11:10:03 -0700889 printf(" mid=%#x", bpb->mid);
890 if (bpb->spf)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700891 printf(" spf=%u", bpb->spf);
San Mehat72eead42009-07-06 11:10:03 -0700892 printf(" spt=%u hds=%u hid=%u", bpb->spt, bpb->hds, bpb->hid);
893 if (bpb->bsec)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700894 printf(" bsec=%u", bpb->bsec);
San Mehat72eead42009-07-06 11:10:03 -0700895 if (!bpb->spf) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700896 printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);
897 printf(" infs=");
898 printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);
899 printf(" bkbs=");
900 printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);
San Mehat72eead42009-07-06 11:10:03 -0700901 }
902 printf("\n");
903}
904
905/*
906 * Check a disk geometry value.
907 */
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700908static u_int ckgeom(const char *fname, u_int val, const char *msg)
San Mehat72eead42009-07-06 11:10:03 -0700909{
910 if (!val)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700911 errx(1, "%s: no default %s", fname, msg);
San Mehat72eead42009-07-06 11:10:03 -0700912 if (val > MAXU16)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700913 errx(1, "%s: illegal %s %d", fname, msg, val);
San Mehat72eead42009-07-06 11:10:03 -0700914 return val;
915}
916
917/*
918 * Convert and check a numeric option argument.
919 */
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700920static u_int argtou(const char *arg, u_int lo, u_int hi, const char *msg)
San Mehat72eead42009-07-06 11:10:03 -0700921{
922 char *s;
923 u_long x;
924
925 errno = 0;
926 x = strtoul(arg, &s, 0);
927 if (errno || !*arg || *s || x < lo || x > hi)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700928 errx(1, "%s: bad %s", arg, msg);
San Mehat72eead42009-07-06 11:10:03 -0700929 return x;
930}
931
932/*
933 * Same for off_t, with optional skmgpP suffix
934 */
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700935static off_t argtooff(const char *arg, const char *msg)
San Mehat72eead42009-07-06 11:10:03 -0700936{
937 char *s;
938 off_t x;
939
940 x = strtoll(arg, &s, 0);
941 /* allow at most one extra char */
942 if (errno || x < 0 || (s[0] && s[1]) )
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700943 errx(1, "%s: bad %s", arg, msg);
944 if (*s) { /* the extra char is the multiplier */
945 switch (*s) {
946 default:
947 errx(1, "%s: bad %s", arg, msg);
948 /* notreached */
San Mehat72eead42009-07-06 11:10:03 -0700949
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700950 case 's': /* sector */
951 case 'S':
952 x <<= 9; /* times 512 */
953 break;
San Mehat72eead42009-07-06 11:10:03 -0700954
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700955 case 'k': /* kilobyte */
956 case 'K':
957 x <<= 10; /* times 1024 */
958 break;
San Mehat72eead42009-07-06 11:10:03 -0700959
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700960 case 'm': /* megabyte */
961 case 'M':
962 x <<= 20; /* times 1024*1024 */
963 break;
San Mehat72eead42009-07-06 11:10:03 -0700964
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700965 case 'g': /* gigabyte */
966 case 'G':
967 x <<= 30; /* times 1024*1024*1024 */
968 break;
969
970 case 'p': /* partition start */
971 case 'P': /* partition start */
972 case 'l': /* partition length */
973 case 'L': /* partition length */
974 errx(1, "%s: not supported yet %s", arg, msg);
975 /* notreached */
976 }
San Mehat72eead42009-07-06 11:10:03 -0700977 }
978 return x;
979}
980
981/*
982 * Check a volume label.
983 */
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700984static int oklabel(const char *src)
San Mehat72eead42009-07-06 11:10:03 -0700985{
986 int c, i;
987
988 for (i = 0; i <= 11; i++) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700989 c = (u_char)*src++;
990 if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
991 break;
San Mehat72eead42009-07-06 11:10:03 -0700992 }
993 return i && !c;
994}
995
996/*
997 * Make a volume label.
998 */
Daniel Rosenberg33e7b132014-07-08 19:26:02 -0700999static void mklabel(u_int8_t *dest, const char *src)
San Mehat72eead42009-07-06 11:10:03 -07001000{
1001 int c, i;
1002
1003 for (i = 0; i < 11; i++) {
Daniel Rosenberg33e7b132014-07-08 19:26:02 -07001004 c = *src ? toupper(*src++) : ' ';
1005 *dest++ = !i && c == '\xe5' ? 5 : c;
San Mehat72eead42009-07-06 11:10:03 -07001006 }
1007}
1008
1009/*
1010 * Copy string, padding with spaces.
1011 */
Daniel Rosenberg33e7b132014-07-08 19:26:02 -07001012static void setstr(u_int8_t *dest, const char *src, size_t len)
San Mehat72eead42009-07-06 11:10:03 -07001013{
1014 while (len--)
Daniel Rosenberg33e7b132014-07-08 19:26:02 -07001015 *dest++ = *src ? *src++ : ' ';
San Mehat72eead42009-07-06 11:10:03 -07001016}
1017
1018/*
1019 * Print usage message.
1020 */
Daniel Rosenberg33e7b132014-07-08 19:26:02 -07001021static void usage(void)
San Mehat72eead42009-07-06 11:10:03 -07001022{
Daniel Rosenberg33e7b132014-07-08 19:26:02 -07001023 fprintf(stderr,
1024 "usage: newfs_msdos [ -options ] special [disktype]\n"
1025 "where the options are:\n"
1026 "\t-@ create file system at specified offset\n"
1027 "\t-B get bootstrap from file\n"
1028 "\t-C create image file with specified size\n"
1029 "\t-F FAT type (12, 16, or 32)\n"
1030 "\t-I volume ID\n"
1031 "\t-L volume label\n"
1032 "\t-N don't create file system: just print out parameters\n"
1033 "\t-O OEM string\n"
1034 "\t-S bytes/sector\n"
1035 "\t-a sectors/FAT\n"
1036 "\t-b block size\n"
1037 "\t-c sectors/cluster\n"
1038 "\t-e root directory entries\n"
1039 "\t-f standard format\n"
1040 "\t-h drive heads\n"
1041 "\t-i file system info sector\n"
1042 "\t-k backup boot sector\n"
1043 "\t-m media descriptor\n"
1044 "\t-n number of FATs\n"
1045 "\t-o hidden sectors\n"
1046 "\t-r reserved sectors\n"
1047 "\t-s file system size (sectors)\n"
1048 "\t-u sectors/track\n");
1049 exit(1);
San Mehat72eead42009-07-06 11:10:03 -07001050}