blob: 744aad158531d096920d1fabc1e70b8c0c16f13b [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/* $NetBSD: newfs_msdos.c,v 1.18.2.1 2005/05/01 18:44:02 tron Exp $ */
2
3/*
4 * Copyright (c) 1998 Robert Nordier
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#define __USE_FILE_OFFSET64
31
32#include <sys/cdefs.h>
33
34#include <sys/types.h>
35#include <sys/param.h>
36#ifdef __FreeBSD__
37#include <sys/diskslice.h>
38#endif
39#include <sys/mount.h>
40#include <sys/stat.h>
41#include <sys/time.h>
42#include <sys/ioctl.h>
43
44#include <ctype.h>
45#include <err.h>
46#include <errno.h>
47#include <fcntl.h>
48#include <paths.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <time.h>
53#include <unistd.h>
54#ifdef __NetBSD__
55#include <disktab.h>
56#include <util.h>
57#endif
58
59#define MAXU16 0xffff /* maximum unsigned 16-bit quantity */
60#define BPN 4 /* bits per nibble */
61#define NPB 2 /* nibbles per byte */
62
63#define DOSMAGIC 0xaa55 /* DOS magic number */
64#define MINBPS 128 /* minimum bytes per sector */
65#define MAXSPC 128 /* maximum sectors per cluster */
66#define MAXNFT 16 /* maximum number of FATs */
67#define DEFBLK 4096 /* default block size */
68#define DEFBLK16 2048 /* default block size FAT16 */
69#define DEFRDE 512 /* default root directory entries */
70#define RESFTE 2 /* reserved FAT entries */
71#define MINCLS12 1 /* minimum FAT12 clusters */
72#define MINCLS16 0x1000 /* minimum FAT16 clusters */
73#define MINCLS32 2 /* minimum FAT32 clusters */
74#define MAXCLS12 0xfed /* maximum FAT12 clusters */
75#define MAXCLS16 0xfff5 /* maximum FAT16 clusters */
76#define MAXCLS32 0xffffff5 /* maximum FAT32 clusters */
77
78#define mincls(fat) ((fat) == 12 ? MINCLS12 : \
79 (fat) == 16 ? MINCLS16 : \
80 MINCLS32)
81
82#define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \
83 (fat) == 16 ? MAXCLS16 : \
84 MAXCLS32)
85
86#define mk1(p, x) \
87 (p) = (u_int8_t)(x)
88
89#define mk2(p, x) \
90 (p)[0] = (u_int8_t)(x), \
91 (p)[1] = (u_int8_t)((x) >> 010)
92
93#define mk4(p, x) \
94 (p)[0] = (u_int8_t)(x), \
95 (p)[1] = (u_int8_t)((x) >> 010), \
96 (p)[2] = (u_int8_t)((x) >> 020), \
97 (p)[3] = (u_int8_t)((x) >> 030)
98
99#define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg)
100#define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg)
101#define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg)
102#define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg)
103
104#ifndef MAX
105#define MAX(x, y) ((x) > (y) ? (x) : (y))
106#endif
107#ifndef MIN
108#define MIN(x, y) ((x) < (y) ? (x) : (y))
109#endif
110
111static int powerof2(int x) {
112 int i;
113 for (i = 0; i < 32; i++) {
114 if (x & 1) {
115 x >>= 1;
116 // if x is zero, then original x was a power of two
117 return (x == 0);
118 }
119 x >>= 1;
120 }
121
122 return 0;
123}
124
125#ifndef howmany
126#define howmany(x, y) (((x)+((y)-1))/(y))
127#endif
128
129#pragma pack(push, 1)
130struct bs {
131 u_int8_t jmp[3]; /* bootstrap entry point */
132 u_int8_t oem[8]; /* OEM name and version */
133};
134#define BS_SIZE 11
135
136struct bsbpb {
137 u_int8_t bps[2]; /* bytes per sector */
138 u_int8_t spc; /* sectors per cluster */
139 u_int8_t res[2]; /* reserved sectors */
140 u_int8_t nft; /* number of FATs */
141 u_int8_t rde[2]; /* root directory entries */
142 u_int8_t sec[2]; /* total sectors */
143 u_int8_t mid; /* media descriptor */
144 u_int8_t spf[2]; /* sectors per FAT */
145 u_int8_t spt[2]; /* sectors per track */
146 u_int8_t hds[2]; /* drive heads */
147 u_int8_t hid[4]; /* hidden sectors */
148 u_int8_t bsec[4]; /* big total sectors */
149};
150#define BSBPB_SIZE 25
151
152struct bsxbpb {
153 u_int8_t bspf[4]; /* big sectors per FAT */
154 u_int8_t xflg[2]; /* FAT control flags */
155 u_int8_t vers[2]; /* file system version */
156 u_int8_t rdcl[4]; /* root directory start cluster */
157 u_int8_t infs[2]; /* file system info sector */
158 u_int8_t bkbs[2]; /* backup boot sector */
159 u_int8_t rsvd[12]; /* reserved */
160};
161#define BSXBPB_SIZE 28
162
163struct bsx {
164 u_int8_t drv; /* drive number */
165 u_int8_t rsvd; /* reserved */
166 u_int8_t sig; /* extended boot signature */
167 u_int8_t volid[4]; /* volume ID number */
168 u_int8_t label[11]; /* volume label */
169 u_int8_t type[8]; /* file system type */
170};
171#define BSX_SIZE 26
172
173struct de {
174 u_int8_t namext[11]; /* name and extension */
175 u_int8_t attr; /* attributes */
176 u_int8_t rsvd[10]; /* reserved */
177 u_int8_t time[2]; /* creation time */
178 u_int8_t date[2]; /* creation date */
179 u_int8_t clus[2]; /* starting cluster */
180 u_int8_t size[4]; /* size */
181#define DE_SIZE 32
182};
183#pragma pack(pop)
184
185struct bpb {
186 u_int bps; /* bytes per sector */
187 u_int spc; /* sectors per cluster */
188 u_int res; /* reserved sectors */
189 u_int nft; /* number of FATs */
190 u_int rde; /* root directory entries */
191 u_int sec; /* total sectors */
192 u_int mid; /* media descriptor */
193 u_int spf; /* sectors per FAT */
194 u_int spt; /* sectors per track */
195 u_int hds; /* drive heads */
196 u_int hid; /* hidden sectors */
197 u_int bsec; /* big total sectors */
198 u_int bspf; /* big sectors per FAT */
199 u_int rdcl; /* root directory start cluster */
200 u_int infs; /* file system info sector */
201 u_int bkbs; /* backup boot sector */
202};
203
204static u_int8_t bootcode[] = {
205 0xfa, /* cli */
206 0x31, 0xc0, /* xor ax,ax */
207 0x8e, 0xd0, /* mov ss,ax */
208 0xbc, 0x00, 0x7c, /* mov sp,7c00h */
209 0xfb, /* sti */
210 0x8e, 0xd8, /* mov ds,ax */
211 0xe8, 0x00, 0x00, /* call $ + 3 */
212 0x5e, /* pop si */
213 0x83, 0xc6, 0x19, /* add si,+19h */
214 0xbb, 0x07, 0x00, /* mov bx,0007h */
215 0xfc, /* cld */
216 0xac, /* lodsb */
217 0x84, 0xc0, /* test al,al */
218 0x74, 0x06, /* jz $ + 8 */
219 0xb4, 0x0e, /* mov ah,0eh */
220 0xcd, 0x10, /* int 10h */
221 0xeb, 0xf5, /* jmp $ - 9 */
222 0x30, 0xe4, /* xor ah,ah */
223 0xcd, 0x16, /* int 16h */
224 0xcd, 0x19, /* int 19h */
225 0x0d, 0x0a,
226 'N', 'o', 'n', '-', 's', 'y', 's', 't',
227 'e', 'm', ' ', 'd', 'i', 's', 'k',
228 0x0d, 0x0a,
229 'P', 'r', 'e', 's', 's', ' ', 'a', 'n',
230 'y', ' ', 'k', 'e', 'y', ' ', 't', 'o',
231 ' ', 'r', 'e', 'b', 'o', 'o', 't',
232 0x0d, 0x0a,
233 0
234};
235
236static void print_bpb(struct bpb *);
237static u_int ckgeom(const char *, u_int, const char *);
238static u_int argtou(const char *, u_int, u_int, const char *);
239static int oklabel(const char *);
240static void mklabel(u_int8_t *, const char *);
241static void setstr(u_int8_t *, const char *, size_t);
242static void usage(char* progname);
243
244/*
245 * Construct a FAT12, FAT16, or FAT32 file system.
246 */
247int
248mkdosfs_main(int argc, char *argv[])
249{
250 static char opts[] = "NB:F:I:L:O:S:a:b:c:e:f:h:i:k:m:n:o:r:s:u:";
251 static const char *opt_B, *opt_L, *opt_O;
252 static u_int opt_F, opt_I, opt_S, opt_a, opt_b, opt_c, opt_e;
253 static u_int opt_h, opt_i, opt_k, opt_m, opt_n, opt_o, opt_r;
254 static u_int opt_s, opt_u;
255 static int opt_N;
256 static int Iflag, mflag, oflag;
257 char buf[MAXPATHLEN];
258 struct stat sb;
259 struct timeval tv;
260 struct bpb bpb;
261 struct tm *tm;
262 struct bs *bs;
263 struct bsbpb *bsbpb;
264 struct bsxbpb *bsxbpb;
265 struct bsx *bsx;
266 struct de *de;
267 u_int8_t *img;
268 const char *fname, *dtype, *bname;
269 ssize_t n;
270 time_t now;
271 u_int fat, bss, rds, cls, dir, lsn, x, x1, x2;
272 int ch, fd, fd1;
273 char* progname = argv[0];
274
275 while ((ch = getopt(argc, argv, opts)) != -1)
276 switch (ch) {
277 case 'N':
278 opt_N = 1;
279 break;
280 case 'B':
281 opt_B = optarg;
282 break;
283 case 'F':
284 if (strcmp(optarg, "12") &&
285 strcmp(optarg, "16") &&
286 strcmp(optarg, "32"))
287 fprintf(stderr, "%s: bad FAT type\n", optarg);
288 opt_F = atoi(optarg);
289 break;
290 case 'I':
291 opt_I = argto4(optarg, 0, "volume ID");
292 Iflag = 1;
293 break;
294 case 'L':
295 if (!oklabel(optarg))
296 fprintf(stderr, "%s: bad volume label\n", optarg);
297 opt_L = optarg;
298 break;
299 case 'O':
300 if (strlen(optarg) > 8)
301 fprintf(stderr, "%s: bad OEM string\n", optarg);
302 opt_O = optarg;
303 break;
304 case 'S':
305 opt_S = argto2(optarg, 1, "bytes/sector");
306 break;
307 case 'a':
308 opt_a = argto4(optarg, 1, "sectors/FAT");
309 break;
310 case 'b':
311 opt_b = argtox(optarg, 1, "block size");
312 opt_c = 0;
313 break;
314 case 'c':
315 opt_c = argto1(optarg, 1, "sectors/cluster");
316 opt_b = 0;
317 break;
318 case 'e':
319 opt_e = argto2(optarg, 1, "directory entries");
320 break;
321 case 'h':
322 opt_h = argto2(optarg, 1, "drive heads");
323 break;
324 case 'i':
325 opt_i = argto2(optarg, 1, "info sector");
326 break;
327 case 'k':
328 opt_k = argto2(optarg, 1, "backup sector");
329 break;
330 case 'm':
331 opt_m = argto1(optarg, 0, "media descriptor");
332 mflag = 1;
333 break;
334 case 'n':
335 opt_n = argto1(optarg, 1, "number of FATs");
336 break;
337 case 'o':
338 opt_o = argto4(optarg, 0, "hidden sectors");
339 oflag = 1;
340 break;
341 case 'r':
342 opt_r = argto2(optarg, 1, "reserved sectors");
343 break;
344 case 's':
345 opt_s = argto4(optarg, 1, "file system size");
346 break;
347 case 'u':
348 opt_u = argto2(optarg, 1, "sectors/track");
349 break;
350 default:
351 usage(progname);
352 }
353 argc -= optind;
354 argv += optind;
355 if (argc < 1 || argc > 2)
356 usage(progname);
357 fname = *argv++;
358 if (!strchr(fname, '/')) {
359 snprintf(buf, sizeof(buf), "%sr%s", _PATH_DEV, fname);
360 if (!(fname = strdup(buf)))
361 fprintf(stderr, NULL);
362 }
363 dtype = *argv;
364 if ((fd = open(fname, opt_N ? O_RDONLY : O_RDWR)) == -1 ||
365 fstat(fd, &sb))
366 fprintf(stderr, "%s\n", fname);
367 memset(&bpb, 0, sizeof(bpb));
368
369 if (opt_h)
370 bpb.hds = opt_h;
371 if (opt_u)
372 bpb.spt = opt_u;
373 if (opt_S)
374 bpb.bps = opt_S;
375 if (opt_s)
376 bpb.bsec = opt_s;
377 if (oflag)
378 bpb.hid = opt_o;
379
380 bpb.bps = 512; // 512 bytes/sector
381 bpb.spc = 8; // 4K clusters
382
383
384 fprintf(stderr, "opening %s\n", fname);
385 if ((fd1 = open(fname, O_RDONLY)) == -1) {
386 fprintf(stderr, "failed to open %s\n", fname);
387 exit(1);
388 }
389
The Android Open Source Projectc2d26a12009-03-02 22:54:47 -0800390 lseek64(fd1, 0, SEEK_SET);
391 loff_t length = lseek64(fd1, 0, SEEK_END);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700392 if (length > 0) {
393 bpb.bsec = length / bpb.bps;
394 bpb.spt = bpb.bsec;
395 // use FAT32 for 2 gig or greater
396 if (length >= 2 *1024 *1024 *1024) {
397 fat = 32;
398 } else {
399 fat = 16;
400 }
401 }
402 close(fd1);
403 fd1 = -1;
404
405 if (!powerof2(bpb.bps))
406 fprintf(stderr, "bytes/sector (%u) is not a power of 2\n", bpb.bps);
407 if (bpb.bps < MINBPS)
408 fprintf(stderr, "bytes/sector (%u) is too small; minimum is %u\n",
409 bpb.bps, MINBPS);
410
411 if (!(fat = opt_F)) {
412 if (!opt_e && (opt_i || opt_k))
413 fat = 32;
414 }
415
416 if ((fat == 32 && opt_e) || (fat != 32 && (opt_i || opt_k)))
417 fprintf(stderr, "-%c is not a legal FAT%s option\n",
418 fat == 32 ? 'e' : opt_i ? 'i' : 'k',
419 fat == 32 ? "32" : "12/16");
420 if (fat == 32)
421 bpb.rde = 0;
422 if (opt_b) {
423 if (!powerof2(opt_b))
424 fprintf(stderr, "block size (%u) is not a power of 2\n", opt_b);
425 if (opt_b < bpb.bps)
426 fprintf(stderr, "block size (%u) is too small; minimum is %u\n",
427 opt_b, bpb.bps);
428 if (opt_b > bpb.bps * MAXSPC)
429 fprintf(stderr, "block size (%u) is too large; maximum is %u\n",
430 opt_b, bpb.bps * MAXSPC);
431 bpb.spc = opt_b / bpb.bps;
432 }
433 if (opt_c) {
434 if (!powerof2(opt_c))
435 fprintf(stderr, "sectors/cluster (%u) is not a power of 2\n", opt_c);
436 bpb.spc = opt_c;
437 }
438 if (opt_r)
439 bpb.res = opt_r;
440 if (opt_n) {
441 if (opt_n > MAXNFT)
442 fprintf(stderr, "number of FATs (%u) is too large; maximum is %u\n",
443 opt_n, MAXNFT);
444 bpb.nft = opt_n;
445 }
446 if (opt_e)
447 bpb.rde = opt_e;
448 if (mflag) {
449 if (opt_m < 0xf0)
450 fprintf(stderr, "illegal media descriptor (%#x)\n", opt_m);
451 bpb.mid = opt_m;
452 }
453 if (opt_a)
454 bpb.bspf = opt_a;
455 if (opt_i)
456 bpb.infs = opt_i;
457 if (opt_k)
458 bpb.bkbs = opt_k;
459 bss = 1;
460 bname = NULL;
461 fd1 = -1;
462 if (opt_B) {
463 bname = opt_B;
464 if (!strchr(bname, '/')) {
465 snprintf(buf, sizeof(buf), "/boot/%s", bname);
466 if (!(bname = strdup(buf)))
467 fprintf(stderr, NULL);
468 }
469 if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb))
470 fprintf(stderr, "%s", bname);
471 if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||
472 sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16)
473 fprintf(stderr, "%s: inappropriate file type or format\n", bname);
474 bss = sb.st_size / bpb.bps;
475 }
476 if (!bpb.nft)
477 bpb.nft = 2;
478 if (!fat) {
479 if (bpb.bsec < (bpb.res ? bpb.res : bss) +
480 howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *
481 ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *
482 bpb.nft +
483 howmany(bpb.rde ? bpb.rde : DEFRDE,
484 bpb.bps / DE_SIZE) +
485 (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *
486 (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))
487 fat = 12;
488 else if (bpb.rde || bpb.bsec <
489 (bpb.res ? bpb.res : bss) +
490 howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +
491 howmany(DEFRDE, bpb.bps / DE_SIZE) +
492 (MAXCLS16 + 1) *
493 (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))
494 fat = 16;
495 else
496 fat = 32;
497 }
498 x = bss;
499 if (fat == 32) {
500 if (!bpb.infs) {
501 if (x == MAXU16 || x == bpb.bkbs)
502 fprintf(stderr, "no room for info sector\n");
503 bpb.infs = x;
504 }
505 if (bpb.infs != MAXU16 && x <= bpb.infs)
506 x = bpb.infs + 1;
507 if (!bpb.bkbs) {
508 if (x == MAXU16)
509 fprintf(stderr, "no room for backup sector\n");
510 bpb.bkbs = x;
511 } else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs)
512 fprintf(stderr, "backup sector would overwrite info sector\n");
513 if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)
514 x = bpb.bkbs + 1;
515 }
516 if (!bpb.res)
517 bpb.res = fat == 32 ? MAX(x, MAX(16384 / bpb.bps, 4)) : x;
518 else if (bpb.res < x)
519 fprintf(stderr, "too few reserved sectors (need %d have %d)\n", x, bpb.res);
520 if (fat != 32 && !bpb.rde)
521 bpb.rde = DEFRDE;
522 rds = howmany(bpb.rde, bpb.bps / DE_SIZE);
523 if (!bpb.spc)
524 for (bpb.spc = howmany(fat == 16 ? DEFBLK16 : DEFBLK, bpb.bps);
525 bpb.spc < MAXSPC &&
526 bpb.res +
527 howmany((RESFTE + maxcls(fat)) * (fat / BPN),
528 bpb.bps * NPB) * bpb.nft +
529 rds +
530 (u_int64_t)(maxcls(fat) + 1) * bpb.spc <= bpb.bsec;
531 bpb.spc <<= 1);
532 if (fat != 32 && bpb.bspf > MAXU16)
533 fprintf(stderr, "too many sectors/FAT for FAT12/16\n");
534 x1 = bpb.res + rds;
535 x = bpb.bspf ? bpb.bspf : 1;
536 if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec)
537 fprintf(stderr, "meta data exceeds file system size\n");
538 x1 += x * bpb.nft;
539 x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /
540 (bpb.spc * bpb.bps * NPB + fat / BPN * bpb.nft);
541 x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN),
542 bpb.bps * NPB);
543 if (!bpb.bspf) {
544 bpb.bspf = x2;
545 x1 += (bpb.bspf - 1) * bpb.nft;
546 }
547 cls = (bpb.bsec - x1) / bpb.spc;
548 x = (u_int64_t)bpb.bspf * bpb.bps * NPB / (fat / BPN) - RESFTE;
549 if (cls > x)
550 cls = x;
551 if (bpb.bspf < x2)
552 fprintf(stderr, "warning: sectors/FAT limits file system to %u clusters\n",
553 cls);
554 if (cls < mincls(fat))
555 fprintf(stderr, "%u clusters too few clusters for FAT%u, need %u\n", cls, fat,
556 mincls(fat));
557 if (cls > maxcls(fat)) {
558 cls = maxcls(fat);
559 bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;
560 fprintf(stderr, "warning: FAT type limits file system to %u sectors\n",
561 bpb.bsec);
562 }
563 printf("%s: %u sector%s in %u FAT%u cluster%s "
564 "(%u bytes/cluster)\n", fname, cls * bpb.spc,
565 cls * bpb.spc == 1 ? "" : "s", cls, fat,
566 cls == 1 ? "" : "s", bpb.bps * bpb.spc);
567 if (!bpb.mid)
568 bpb.mid = !bpb.hid ? 0xf0 : 0xf8;
569 if (fat == 32)
570 bpb.rdcl = RESFTE;
571 if (bpb.hid + bpb.bsec <= MAXU16) {
572 bpb.sec = bpb.bsec;
573 bpb.bsec = 0;
574 }
575 if (fat != 32) {
576 bpb.spf = bpb.bspf;
577 bpb.bspf = 0;
578 }
579 ch = 0;
580 if (fat == 12)
581 ch = 1; /* 001 Primary DOS with 12 bit FAT */
582 else if (fat == 16) {
583 if (bpb.bsec == 0)
584 ch = 4; /* 004 Primary DOS with 16 bit FAT <32M */
585 else
586 ch = 6; /* 006 Primary 'big' DOS, 16-bit FAT (> 32MB) */
587 /*
588 * XXX: what about:
589 * 014 DOS (16-bit FAT) - LBA
590 * ?
591 */
592 } else if (fat == 32) {
593 ch = 11; /* 011 Primary DOS with 32 bit FAT */
594 /*
595 * XXX: what about:
596 * 012 Primary DOS with 32 bit FAT - LBA
597 * ?
598 */
599 }
600 if (ch != 0)
601 printf("MBR type: %d\n", ch);
602 print_bpb(&bpb);
603 if (!opt_N) {
604 gettimeofday(&tv, NULL);
605 now = tv.tv_sec;
606 tm = localtime(&now);
607 if (!(img = malloc(bpb.bps)))
608 fprintf(stderr, NULL);
609 dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
610
611 for (lsn = 0; lsn < dir + (fat == 32 ? bpb.spc : rds); lsn++) {
612 x = lsn;
613 if (opt_B &&
614 fat == 32 && bpb.bkbs != MAXU16 &&
615 bss <= bpb.bkbs && x >= bpb.bkbs) {
616 x -= bpb.bkbs;
The Android Open Source Projectc2d26a12009-03-02 22:54:47 -0800617 if (!x && lseek64(fd1, 0, SEEK_SET))
618 fprintf(stderr, "lseek64 failed for %s\n", bname);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700619 }
620 if (opt_B && x < bss) {
621 if ((n = read(fd1, img, bpb.bps)) == -1)
622 fprintf(stderr, "%s\n", bname);
623 if (n != bpb.bps)
624 fprintf(stderr, "%s: can't read sector %u\n", bname, x);
625 } else
626 memset(img, 0, bpb.bps);
627 if (!lsn ||
628 (fat == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {
629 x1 = BS_SIZE;
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 += BSBPB_SIZE;
644 if (fat == 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 += BSXBPB_SIZE;
653 }
654 bsx = (struct bsx *)(img + x1);
655 mk1(bsx->sig, 0x29);
656 if (Iflag)
657 x = opt_I;
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, opt_L ? opt_L : "NO_NAME");
668 snprintf(buf, sizeof(buf), "FAT%u", fat);
669 setstr(bsx->type, buf, sizeof(bsx->type));
670 if (!opt_B) {
671 x1 += BSX_SIZE;
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, opt_O ? opt_O : "NetBSD",
677 sizeof(bs->oem));
678 memcpy(img + x1, bootcode, sizeof(bootcode));
679 mk2(img + bpb.bps - 2, DOSMAGIC);
680 }
681 } else if (fat == 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 + bpb.bps - 28, 0x61417272);
687 mk4(img + bpb.bps - 24, 0xffffffff);
688 mk4(img + bpb.bps - 20, bpb.rdcl);
689 mk2(img + bpb.bps - 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 < fat * (fat == 32 ? 3 : 2) / 8; x++)
695 mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
696 } else if (lsn == dir && opt_L) {
697 de = (struct de *)img;
698 mklabel(de->namext, opt_L);
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 fprintf(stderr, "%s\n", fname);
711 if (n != bpb.bps)
712 fprintf(stderr, "%s: can't write sector %u\n", fname, lsn);
713 }
714 }
715 return 0;
716}
717
718/*
719 * Print out BPB values.
720 */
721static void
722print_bpb(struct bpb *bpb)
723{
724 printf("bps=%u spc=%u res=%u nft=%u", bpb->bps, bpb->spc, bpb->res,
725 bpb->nft);
726 if (bpb->rde)
727 printf(" rde=%u", bpb->rde);
728 if (bpb->sec)
729 printf(" sec=%u", bpb->sec);
730 printf(" mid=%#x", bpb->mid);
731 if (bpb->spf)
732 printf(" spf=%u", bpb->spf);
733 printf(" spt=%u hds=%u hid=%u", bpb->spt, bpb->hds, bpb->hid);
734 if (bpb->bsec)
735 printf(" bsec=%u", bpb->bsec);
736 if (!bpb->spf) {
737 printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);
738 printf(" infs=");
739 printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);
740 printf(" bkbs=");
741 printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);
742 }
743 printf("\n");
744}
745
746/*
747 * Check a disk geometry value.
748 */
749static u_int
750ckgeom(const char *fname, u_int val, const char *msg)
751{
752 if (!val)
753 fprintf(stderr, "%s: no default %s\n", fname, msg);
754 if (val > MAXU16)
755 fprintf(stderr, "%s: illegal %s\n", fname, msg);
756 return val;
757}
758
759/*
760 * Convert and check a numeric option argument.
761 */
762static u_int
763argtou(const char *arg, u_int lo, u_int hi, const char *msg)
764{
765 char *s;
766 u_long x;
767
768 errno = 0;
769 x = strtoul(arg, &s, 0);
770 if (errno || !*arg || *s || x < lo || x > hi)
771 fprintf(stderr, "%s: bad %s\n", arg, msg);
772 return x;
773}
774
775/*
776 * Check a volume label.
777 */
778static int
779oklabel(const char *src)
780{
781 int c, i;
782
783 for (i = 0; i <= 11; i++) {
784 c = (u_char)*src++;
785 if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
786 break;
787 }
788 return i && !c;
789}
790
791/*
792 * Make a volume label.
793 */
794static void
795mklabel(u_int8_t *dest, const char *src)
796{
797 int c, i;
798
799 for (i = 0; i < 11; i++) {
800 c = *src ? toupper((unsigned char)*src++) : ' ';
801 *dest++ = !i && c == '\xe5' ? 5 : c;
802 }
803}
804
805/*
806 * Copy string, padding with spaces.
807 */
808static void
809setstr(u_int8_t *dest, const char *src, size_t len)
810{
811 while (len--)
812 *dest++ = *src ? *src++ : ' ';
813}
814
815/*
816 * Print usage message.
817 */
818static void
819usage(char* progname)
820{
821 fprintf(stderr,
822 "usage: %s [ -options ] special [disktype]\n", progname);
823 fprintf(stderr, "where the options are:\n");
824 fprintf(stderr, "\t-N don't create file system: "
825 "just print out parameters\n");
826 fprintf(stderr, "\t-B get bootstrap from file\n");
827 fprintf(stderr, "\t-F FAT type (12, 16, or 32)\n");
828 fprintf(stderr, "\t-I volume ID\n");
829 fprintf(stderr, "\t-L volume label\n");
830 fprintf(stderr, "\t-O OEM string\n");
831 fprintf(stderr, "\t-S bytes/sector\n");
832 fprintf(stderr, "\t-a sectors/FAT\n");
833 fprintf(stderr, "\t-b block size\n");
834 fprintf(stderr, "\t-c sectors/cluster\n");
835 fprintf(stderr, "\t-e root directory entries\n");
836 fprintf(stderr, "\t-h drive heads\n");
837 fprintf(stderr, "\t-i file system info sector\n");
838 fprintf(stderr, "\t-k backup boot sector\n");
839 fprintf(stderr, "\t-m media descriptor\n");
840 fprintf(stderr, "\t-n number of FATs\n");
841 fprintf(stderr, "\t-o hidden sectors\n");
842 fprintf(stderr, "\t-r reserved sectors\n");
843 fprintf(stderr, "\t-s file system size (sectors)\n");
844 fprintf(stderr, "\t-u sectors/track\n");
845 exit(1);
846}
847
848