blob: 9ba94090daf17440e3dd5e58b5062de89b845e89 [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 Projecta1e1c1b2009-03-03 14:04:41 -0800390 lseek(fd1, 0, SEEK_SET);
391 off_t length = lseek(fd1, 0, SEEK_END);
392 fprintf(stderr, "lseek returned %ld\n", length);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700393 if (length > 0) {
394 bpb.bsec = length / bpb.bps;
395 bpb.spt = bpb.bsec;
396 // use FAT32 for 2 gig or greater
397 if (length >= 2 *1024 *1024 *1024) {
398 fat = 32;
399 } else {
400 fat = 16;
401 }
402 }
403 close(fd1);
404 fd1 = -1;
405
406 if (!powerof2(bpb.bps))
407 fprintf(stderr, "bytes/sector (%u) is not a power of 2\n", bpb.bps);
408 if (bpb.bps < MINBPS)
409 fprintf(stderr, "bytes/sector (%u) is too small; minimum is %u\n",
410 bpb.bps, MINBPS);
411
412 if (!(fat = opt_F)) {
413 if (!opt_e && (opt_i || opt_k))
414 fat = 32;
415 }
416
417 if ((fat == 32 && opt_e) || (fat != 32 && (opt_i || opt_k)))
418 fprintf(stderr, "-%c is not a legal FAT%s option\n",
419 fat == 32 ? 'e' : opt_i ? 'i' : 'k',
420 fat == 32 ? "32" : "12/16");
421 if (fat == 32)
422 bpb.rde = 0;
423 if (opt_b) {
424 if (!powerof2(opt_b))
425 fprintf(stderr, "block size (%u) is not a power of 2\n", opt_b);
426 if (opt_b < bpb.bps)
427 fprintf(stderr, "block size (%u) is too small; minimum is %u\n",
428 opt_b, bpb.bps);
429 if (opt_b > bpb.bps * MAXSPC)
430 fprintf(stderr, "block size (%u) is too large; maximum is %u\n",
431 opt_b, bpb.bps * MAXSPC);
432 bpb.spc = opt_b / bpb.bps;
433 }
434 if (opt_c) {
435 if (!powerof2(opt_c))
436 fprintf(stderr, "sectors/cluster (%u) is not a power of 2\n", opt_c);
437 bpb.spc = opt_c;
438 }
439 if (opt_r)
440 bpb.res = opt_r;
441 if (opt_n) {
442 if (opt_n > MAXNFT)
443 fprintf(stderr, "number of FATs (%u) is too large; maximum is %u\n",
444 opt_n, MAXNFT);
445 bpb.nft = opt_n;
446 }
447 if (opt_e)
448 bpb.rde = opt_e;
449 if (mflag) {
450 if (opt_m < 0xf0)
451 fprintf(stderr, "illegal media descriptor (%#x)\n", opt_m);
452 bpb.mid = opt_m;
453 }
454 if (opt_a)
455 bpb.bspf = opt_a;
456 if (opt_i)
457 bpb.infs = opt_i;
458 if (opt_k)
459 bpb.bkbs = opt_k;
460 bss = 1;
461 bname = NULL;
462 fd1 = -1;
463 if (opt_B) {
464 bname = opt_B;
465 if (!strchr(bname, '/')) {
466 snprintf(buf, sizeof(buf), "/boot/%s", bname);
467 if (!(bname = strdup(buf)))
468 fprintf(stderr, NULL);
469 }
470 if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb))
471 fprintf(stderr, "%s", bname);
472 if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bps ||
473 sb.st_size < bpb.bps || sb.st_size > bpb.bps * MAXU16)
474 fprintf(stderr, "%s: inappropriate file type or format\n", bname);
475 bss = sb.st_size / bpb.bps;
476 }
477 if (!bpb.nft)
478 bpb.nft = 2;
479 if (!fat) {
480 if (bpb.bsec < (bpb.res ? bpb.res : bss) +
481 howmany((RESFTE + (bpb.spc ? MINCLS16 : MAXCLS12 + 1)) *
482 ((bpb.spc ? 16 : 12) / BPN), bpb.bps * NPB) *
483 bpb.nft +
484 howmany(bpb.rde ? bpb.rde : DEFRDE,
485 bpb.bps / DE_SIZE) +
486 (bpb.spc ? MINCLS16 : MAXCLS12 + 1) *
487 (bpb.spc ? bpb.spc : howmany(DEFBLK, bpb.bps)))
488 fat = 12;
489 else if (bpb.rde || bpb.bsec <
490 (bpb.res ? bpb.res : bss) +
491 howmany((RESFTE + MAXCLS16) * 2, bpb.bps) * bpb.nft +
492 howmany(DEFRDE, bpb.bps / DE_SIZE) +
493 (MAXCLS16 + 1) *
494 (bpb.spc ? bpb.spc : howmany(8192, bpb.bps)))
495 fat = 16;
496 else
497 fat = 32;
498 }
499 x = bss;
500 if (fat == 32) {
501 if (!bpb.infs) {
502 if (x == MAXU16 || x == bpb.bkbs)
503 fprintf(stderr, "no room for info sector\n");
504 bpb.infs = x;
505 }
506 if (bpb.infs != MAXU16 && x <= bpb.infs)
507 x = bpb.infs + 1;
508 if (!bpb.bkbs) {
509 if (x == MAXU16)
510 fprintf(stderr, "no room for backup sector\n");
511 bpb.bkbs = x;
512 } else if (bpb.bkbs != MAXU16 && bpb.bkbs == bpb.infs)
513 fprintf(stderr, "backup sector would overwrite info sector\n");
514 if (bpb.bkbs != MAXU16 && x <= bpb.bkbs)
515 x = bpb.bkbs + 1;
516 }
517 if (!bpb.res)
518 bpb.res = fat == 32 ? MAX(x, MAX(16384 / bpb.bps, 4)) : x;
519 else if (bpb.res < x)
520 fprintf(stderr, "too few reserved sectors (need %d have %d)\n", x, bpb.res);
521 if (fat != 32 && !bpb.rde)
522 bpb.rde = DEFRDE;
523 rds = howmany(bpb.rde, bpb.bps / DE_SIZE);
524 if (!bpb.spc)
525 for (bpb.spc = howmany(fat == 16 ? DEFBLK16 : DEFBLK, bpb.bps);
526 bpb.spc < MAXSPC &&
527 bpb.res +
528 howmany((RESFTE + maxcls(fat)) * (fat / BPN),
529 bpb.bps * NPB) * bpb.nft +
530 rds +
531 (u_int64_t)(maxcls(fat) + 1) * bpb.spc <= bpb.bsec;
532 bpb.spc <<= 1);
533 if (fat != 32 && bpb.bspf > MAXU16)
534 fprintf(stderr, "too many sectors/FAT for FAT12/16\n");
535 x1 = bpb.res + rds;
536 x = bpb.bspf ? bpb.bspf : 1;
537 if (x1 + (u_int64_t)x * bpb.nft > bpb.bsec)
538 fprintf(stderr, "meta data exceeds file system size\n");
539 x1 += x * bpb.nft;
540 x = (u_int64_t)(bpb.bsec - x1) * bpb.bps * NPB /
541 (bpb.spc * bpb.bps * NPB + fat / BPN * bpb.nft);
542 x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN),
543 bpb.bps * NPB);
544 if (!bpb.bspf) {
545 bpb.bspf = x2;
546 x1 += (bpb.bspf - 1) * bpb.nft;
547 }
548 cls = (bpb.bsec - x1) / bpb.spc;
549 x = (u_int64_t)bpb.bspf * bpb.bps * NPB / (fat / BPN) - RESFTE;
550 if (cls > x)
551 cls = x;
552 if (bpb.bspf < x2)
553 fprintf(stderr, "warning: sectors/FAT limits file system to %u clusters\n",
554 cls);
555 if (cls < mincls(fat))
556 fprintf(stderr, "%u clusters too few clusters for FAT%u, need %u\n", cls, fat,
557 mincls(fat));
558 if (cls > maxcls(fat)) {
559 cls = maxcls(fat);
560 bpb.bsec = x1 + (cls + 1) * bpb.spc - 1;
561 fprintf(stderr, "warning: FAT type limits file system to %u sectors\n",
562 bpb.bsec);
563 }
564 printf("%s: %u sector%s in %u FAT%u cluster%s "
565 "(%u bytes/cluster)\n", fname, cls * bpb.spc,
566 cls * bpb.spc == 1 ? "" : "s", cls, fat,
567 cls == 1 ? "" : "s", bpb.bps * bpb.spc);
568 if (!bpb.mid)
569 bpb.mid = !bpb.hid ? 0xf0 : 0xf8;
570 if (fat == 32)
571 bpb.rdcl = RESFTE;
572 if (bpb.hid + bpb.bsec <= MAXU16) {
573 bpb.sec = bpb.bsec;
574 bpb.bsec = 0;
575 }
576 if (fat != 32) {
577 bpb.spf = bpb.bspf;
578 bpb.bspf = 0;
579 }
580 ch = 0;
581 if (fat == 12)
582 ch = 1; /* 001 Primary DOS with 12 bit FAT */
583 else if (fat == 16) {
584 if (bpb.bsec == 0)
585 ch = 4; /* 004 Primary DOS with 16 bit FAT <32M */
586 else
587 ch = 6; /* 006 Primary 'big' DOS, 16-bit FAT (> 32MB) */
588 /*
589 * XXX: what about:
590 * 014 DOS (16-bit FAT) - LBA
591 * ?
592 */
593 } else if (fat == 32) {
594 ch = 11; /* 011 Primary DOS with 32 bit FAT */
595 /*
596 * XXX: what about:
597 * 012 Primary DOS with 32 bit FAT - LBA
598 * ?
599 */
600 }
601 if (ch != 0)
602 printf("MBR type: %d\n", ch);
603 print_bpb(&bpb);
604 if (!opt_N) {
605 gettimeofday(&tv, NULL);
606 now = tv.tv_sec;
607 tm = localtime(&now);
608 if (!(img = malloc(bpb.bps)))
609 fprintf(stderr, NULL);
610 dir = bpb.res + (bpb.spf ? bpb.spf : bpb.bspf) * bpb.nft;
611
612 for (lsn = 0; lsn < dir + (fat == 32 ? bpb.spc : rds); lsn++) {
613 x = lsn;
614 if (opt_B &&
615 fat == 32 && bpb.bkbs != MAXU16 &&
616 bss <= bpb.bkbs && x >= bpb.bkbs) {
617 x -= bpb.bkbs;
The Android Open Source Projecta1e1c1b2009-03-03 14:04:41 -0800618 if (!x && lseek(fd1, 0, SEEK_SET))
619 fprintf(stderr, "lseek failed for %s\n", bname);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700620 }
621 if (opt_B && x < bss) {
622 if ((n = read(fd1, img, bpb.bps)) == -1)
623 fprintf(stderr, "%s\n", bname);
624 if (n != bpb.bps)
625 fprintf(stderr, "%s: can't read sector %u\n", bname, x);
626 } else
627 memset(img, 0, bpb.bps);
628 if (!lsn ||
629 (fat == 32 && bpb.bkbs != MAXU16 && lsn == bpb.bkbs)) {
630 x1 = BS_SIZE;
631 bsbpb = (struct bsbpb *)(img + x1);
632 mk2(bsbpb->bps, bpb.bps);
633 mk1(bsbpb->spc, bpb.spc);
634 mk2(bsbpb->res, bpb.res);
635 mk1(bsbpb->nft, bpb.nft);
636 mk2(bsbpb->rde, bpb.rde);
637 mk2(bsbpb->sec, bpb.sec);
638 mk1(bsbpb->mid, bpb.mid);
639 mk2(bsbpb->spf, bpb.spf);
640 mk2(bsbpb->spt, bpb.spt);
641 mk2(bsbpb->hds, bpb.hds);
642 mk4(bsbpb->hid, bpb.hid);
643 mk4(bsbpb->bsec, bpb.bsec);
644 x1 += BSBPB_SIZE;
645 if (fat == 32) {
646 bsxbpb = (struct bsxbpb *)(img + x1);
647 mk4(bsxbpb->bspf, bpb.bspf);
648 mk2(bsxbpb->xflg, 0);
649 mk2(bsxbpb->vers, 0);
650 mk4(bsxbpb->rdcl, bpb.rdcl);
651 mk2(bsxbpb->infs, bpb.infs);
652 mk2(bsxbpb->bkbs, bpb.bkbs);
653 x1 += BSXBPB_SIZE;
654 }
655 bsx = (struct bsx *)(img + x1);
656 mk1(bsx->sig, 0x29);
657 if (Iflag)
658 x = opt_I;
659 else
660 x = (((u_int)(1 + tm->tm_mon) << 8 |
661 (u_int)tm->tm_mday) +
662 ((u_int)tm->tm_sec << 8 |
663 (u_int)(tv.tv_usec / 10))) << 16 |
664 ((u_int)(1900 + tm->tm_year) +
665 ((u_int)tm->tm_hour << 8 |
666 (u_int)tm->tm_min));
667 mk4(bsx->volid, x);
668 mklabel(bsx->label, opt_L ? opt_L : "NO_NAME");
669 snprintf(buf, sizeof(buf), "FAT%u", fat);
670 setstr(bsx->type, buf, sizeof(bsx->type));
671 if (!opt_B) {
672 x1 += BSX_SIZE;
673 bs = (struct bs *)img;
674 mk1(bs->jmp[0], 0xeb);
675 mk1(bs->jmp[1], x1 - 2);
676 mk1(bs->jmp[2], 0x90);
677 setstr(bs->oem, opt_O ? opt_O : "NetBSD",
678 sizeof(bs->oem));
679 memcpy(img + x1, bootcode, sizeof(bootcode));
680 mk2(img + bpb.bps - 2, DOSMAGIC);
681 }
682 } else if (fat == 32 && bpb.infs != MAXU16 &&
683 (lsn == bpb.infs ||
684 (bpb.bkbs != MAXU16 &&
685 lsn == bpb.bkbs + bpb.infs))) {
686 mk4(img, 0x41615252);
687 mk4(img + bpb.bps - 28, 0x61417272);
688 mk4(img + bpb.bps - 24, 0xffffffff);
689 mk4(img + bpb.bps - 20, bpb.rdcl);
690 mk2(img + bpb.bps - 2, DOSMAGIC);
691 } else if (lsn >= bpb.res && lsn < dir &&
692 !((lsn - bpb.res) %
693 (bpb.spf ? bpb.spf : bpb.bspf))) {
694 mk1(img[0], bpb.mid);
695 for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++)
696 mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff);
697 } else if (lsn == dir && opt_L) {
698 de = (struct de *)img;
699 mklabel(de->namext, opt_L);
700 mk1(de->attr, 050);
701 x = (u_int)tm->tm_hour << 11 |
702 (u_int)tm->tm_min << 5 |
703 (u_int)tm->tm_sec >> 1;
704 mk2(de->time, x);
705 x = (u_int)(tm->tm_year - 80) << 9 |
706 (u_int)(tm->tm_mon + 1) << 5 |
707 (u_int)tm->tm_mday;
708 mk2(de->date, x);
709 }
710 if ((n = write(fd, img, bpb.bps)) == -1)
711 fprintf(stderr, "%s\n", fname);
712 if (n != bpb.bps)
713 fprintf(stderr, "%s: can't write sector %u\n", fname, lsn);
714 }
715 }
716 return 0;
717}
718
719/*
720 * Print out BPB values.
721 */
722static void
723print_bpb(struct bpb *bpb)
724{
725 printf("bps=%u spc=%u res=%u nft=%u", bpb->bps, bpb->spc, bpb->res,
726 bpb->nft);
727 if (bpb->rde)
728 printf(" rde=%u", bpb->rde);
729 if (bpb->sec)
730 printf(" sec=%u", bpb->sec);
731 printf(" mid=%#x", bpb->mid);
732 if (bpb->spf)
733 printf(" spf=%u", bpb->spf);
734 printf(" spt=%u hds=%u hid=%u", bpb->spt, bpb->hds, bpb->hid);
735 if (bpb->bsec)
736 printf(" bsec=%u", bpb->bsec);
737 if (!bpb->spf) {
738 printf(" bspf=%u rdcl=%u", bpb->bspf, bpb->rdcl);
739 printf(" infs=");
740 printf(bpb->infs == MAXU16 ? "%#x" : "%u", bpb->infs);
741 printf(" bkbs=");
742 printf(bpb->bkbs == MAXU16 ? "%#x" : "%u", bpb->bkbs);
743 }
744 printf("\n");
745}
746
747/*
748 * Check a disk geometry value.
749 */
750static u_int
751ckgeom(const char *fname, u_int val, const char *msg)
752{
753 if (!val)
754 fprintf(stderr, "%s: no default %s\n", fname, msg);
755 if (val > MAXU16)
756 fprintf(stderr, "%s: illegal %s\n", fname, msg);
757 return val;
758}
759
760/*
761 * Convert and check a numeric option argument.
762 */
763static u_int
764argtou(const char *arg, u_int lo, u_int hi, const char *msg)
765{
766 char *s;
767 u_long x;
768
769 errno = 0;
770 x = strtoul(arg, &s, 0);
771 if (errno || !*arg || *s || x < lo || x > hi)
772 fprintf(stderr, "%s: bad %s\n", arg, msg);
773 return x;
774}
775
776/*
777 * Check a volume label.
778 */
779static int
780oklabel(const char *src)
781{
782 int c, i;
783
784 for (i = 0; i <= 11; i++) {
785 c = (u_char)*src++;
786 if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
787 break;
788 }
789 return i && !c;
790}
791
792/*
793 * Make a volume label.
794 */
795static void
796mklabel(u_int8_t *dest, const char *src)
797{
798 int c, i;
799
800 for (i = 0; i < 11; i++) {
801 c = *src ? toupper((unsigned char)*src++) : ' ';
802 *dest++ = !i && c == '\xe5' ? 5 : c;
803 }
804}
805
806/*
807 * Copy string, padding with spaces.
808 */
809static void
810setstr(u_int8_t *dest, const char *src, size_t len)
811{
812 while (len--)
813 *dest++ = *src ? *src++ : ' ';
814}
815
816/*
817 * Print usage message.
818 */
819static void
820usage(char* progname)
821{
822 fprintf(stderr,
823 "usage: %s [ -options ] special [disktype]\n", progname);
824 fprintf(stderr, "where the options are:\n");
825 fprintf(stderr, "\t-N don't create file system: "
826 "just print out parameters\n");
827 fprintf(stderr, "\t-B get bootstrap from file\n");
828 fprintf(stderr, "\t-F FAT type (12, 16, or 32)\n");
829 fprintf(stderr, "\t-I volume ID\n");
830 fprintf(stderr, "\t-L volume label\n");
831 fprintf(stderr, "\t-O OEM string\n");
832 fprintf(stderr, "\t-S bytes/sector\n");
833 fprintf(stderr, "\t-a sectors/FAT\n");
834 fprintf(stderr, "\t-b block size\n");
835 fprintf(stderr, "\t-c sectors/cluster\n");
836 fprintf(stderr, "\t-e root directory entries\n");
837 fprintf(stderr, "\t-h drive heads\n");
838 fprintf(stderr, "\t-i file system info sector\n");
839 fprintf(stderr, "\t-k backup boot sector\n");
840 fprintf(stderr, "\t-m media descriptor\n");
841 fprintf(stderr, "\t-n number of FATs\n");
842 fprintf(stderr, "\t-o hidden sectors\n");
843 fprintf(stderr, "\t-r reserved sectors\n");
844 fprintf(stderr, "\t-s file system size (sectors)\n");
845 fprintf(stderr, "\t-u sectors/track\n");
846 exit(1);
847}
848
849