newfs_msdos.c (225736) | newfs_msdos.c (229550) |
---|---|
1/* 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 --- 13 unchanged lines hidden (view full) --- 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[] = | 1/* 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 --- 13 unchanged lines hidden (view full) --- 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[] = |
30 "$FreeBSD: stable/9/sbin/newfs_msdos/newfs_msdos.c 223946 2011-07-12 10:07:07Z ae $"; | 30 "$FreeBSD: stable/9/sbin/newfs_msdos/newfs_msdos.c 229550 2012-01-05 01:40:42Z pfg $"; |
31#endif /* not lint */ 32 33#include <sys/param.h> 34#include <sys/fdcio.h> 35#include <sys/disk.h> 36#include <sys/disklabel.h> 37#include <sys/mount.h> 38#include <sys/stat.h> --- 22 unchanged lines hidden (view full) --- 61#define MAXNFT 16 /* maximum number of FATs */ 62#define DEFBLK 4096 /* default block size */ 63#define DEFBLK16 2048 /* default block size FAT16 */ 64#define DEFRDE 512 /* default root directory entries */ 65#define RESFTE 2 /* reserved FAT entries */ 66#define MINCLS12 1U /* minimum FAT12 clusters */ 67#define MINCLS16 0x1000U /* minimum FAT16 clusters */ 68#define MINCLS32 2U /* minimum FAT32 clusters */ | 31#endif /* not lint */ 32 33#include <sys/param.h> 34#include <sys/fdcio.h> 35#include <sys/disk.h> 36#include <sys/disklabel.h> 37#include <sys/mount.h> 38#include <sys/stat.h> --- 22 unchanged lines hidden (view full) --- 61#define MAXNFT 16 /* maximum number of FATs */ 62#define DEFBLK 4096 /* default block size */ 63#define DEFBLK16 2048 /* default block size FAT16 */ 64#define DEFRDE 512 /* default root directory entries */ 65#define RESFTE 2 /* reserved FAT entries */ 66#define MINCLS12 1U /* minimum FAT12 clusters */ 67#define MINCLS16 0x1000U /* minimum FAT16 clusters */ 68#define MINCLS32 2U /* minimum FAT32 clusters */ |
69#define MAXCLS12 0xfedU /* maximum FAT12 clusters */ | 69#define MAXCLS12 0xfedU /* maximum FAT12 clusters */ |
70#define MAXCLS16 0xfff5U /* maximum FAT16 clusters */ 71#define MAXCLS32 0xffffff5U /* maximum FAT32 clusters */ 72 73#define mincls(fat) ((fat) == 12 ? MINCLS12 : \ 74 (fat) == 16 ? MINCLS16 : \ 75 MINCLS32) 76 77#define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \ --- 14 unchanged lines hidden (view full) --- 92 (p)[3] = (u_int8_t)((x) >> 030) 93 94#define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg) 95#define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg) 96#define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg) 97#define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg) 98 99struct bs { | 70#define MAXCLS16 0xfff5U /* maximum FAT16 clusters */ 71#define MAXCLS32 0xffffff5U /* maximum FAT32 clusters */ 72 73#define mincls(fat) ((fat) == 12 ? MINCLS12 : \ 74 (fat) == 16 ? MINCLS16 : \ 75 MINCLS32) 76 77#define maxcls(fat) ((fat) == 12 ? MAXCLS12 : \ --- 14 unchanged lines hidden (view full) --- 92 (p)[3] = (u_int8_t)((x) >> 030) 93 94#define argto1(arg, lo, msg) argtou(arg, lo, 0xff, msg) 95#define argto2(arg, lo, msg) argtou(arg, lo, 0xffff, msg) 96#define argto4(arg, lo, msg) argtou(arg, lo, 0xffffffff, msg) 97#define argtox(arg, lo, msg) argtou(arg, lo, UINT_MAX, msg) 98 99struct bs { |
100 u_int8_t bsJump[3]; /* bootstrap entry point */ | 100 u_int8_t bsJump[3]; /* bootstrap entry point */ |
101 u_int8_t bsOemName[8]; /* OEM name and version */ 102}; 103 104struct bsbpb { 105 u_int8_t bpbBytesPerSec[2]; /* bytes per sector */ 106 u_int8_t bpbSecPerClust; /* sectors per cluster */ 107 u_int8_t bpbResSectors[2]; /* reserved sectors */ 108 u_int8_t bpbFATs; /* number of FATs */ --- 9 unchanged lines hidden (view full) --- 118 119struct bsxbpb { 120 u_int8_t bpbBigFATsecs[4]; /* big sectors per FAT */ 121 u_int8_t bpbExtFlags[2]; /* FAT control flags */ 122 u_int8_t bpbFSVers[2]; /* file system version */ 123 u_int8_t bpbRootClust[4]; /* root directory start cluster */ 124 u_int8_t bpbFSInfo[2]; /* file system info sector */ 125 u_int8_t bpbBackup[2]; /* backup boot sector */ | 101 u_int8_t bsOemName[8]; /* OEM name and version */ 102}; 103 104struct bsbpb { 105 u_int8_t bpbBytesPerSec[2]; /* bytes per sector */ 106 u_int8_t bpbSecPerClust; /* sectors per cluster */ 107 u_int8_t bpbResSectors[2]; /* reserved sectors */ 108 u_int8_t bpbFATs; /* number of FATs */ --- 9 unchanged lines hidden (view full) --- 118 119struct bsxbpb { 120 u_int8_t bpbBigFATsecs[4]; /* big sectors per FAT */ 121 u_int8_t bpbExtFlags[2]; /* FAT control flags */ 122 u_int8_t bpbFSVers[2]; /* file system version */ 123 u_int8_t bpbRootClust[4]; /* root directory start cluster */ 124 u_int8_t bpbFSInfo[2]; /* file system info sector */ 125 u_int8_t bpbBackup[2]; /* backup boot sector */ |
126 u_int8_t bpbReserved[12]; /* reserved */ | 126 u_int8_t bpbReserved[12]; /* reserved */ |
127}; 128 129struct bsx { 130 u_int8_t exDriveNumber; /* drive number */ 131 u_int8_t exReserved1; /* reserved */ 132 u_int8_t exBootSignature; /* extended boot signature */ 133 u_int8_t exVolumeID[4]; /* volume ID number */ 134 u_int8_t exVolumeLabel[11]; /* volume label */ 135 u_int8_t exFileSysType[8]; /* file system type */ 136}; 137 138struct de { | 127}; 128 129struct bsx { 130 u_int8_t exDriveNumber; /* drive number */ 131 u_int8_t exReserved1; /* reserved */ 132 u_int8_t exBootSignature; /* extended boot signature */ 133 u_int8_t exVolumeID[4]; /* volume ID number */ 134 u_int8_t exVolumeLabel[11]; /* volume label */ 135 u_int8_t exFileSysType[8]; /* file system type */ 136}; 137 138struct de { |
139 u_int8_t deName[11]; /* name and extension */ 140 u_int8_t deAttributes; /* attributes */ 141 u_int8_t rsvd[10]; /* reserved */ 142 u_int8_t deMTime[2]; /* creation time */ 143 u_int8_t deMDate[2]; /* creation date */ 144 u_int8_t deStartCluster[2]; /* starting cluster */ 145 u_int8_t deFileSize[4]; /* size */ | 139 u_int8_t deName[11]; /* name and extension */ 140 u_int8_t deAttributes; /* attributes */ 141 u_int8_t rsvd[10]; /* reserved */ 142 u_int8_t deMTime[2]; /* creation time */ 143 u_int8_t deMDate[2]; /* creation date */ 144 u_int8_t deStartCluster[2]; /* starting cluster */ 145 u_int8_t deFileSize[4]; /* size */ |
146}; 147 148struct bpb { 149 u_int bpbBytesPerSec; /* bytes per sector */ 150 u_int bpbSecPerClust; /* sectors per cluster */ 151 u_int bpbResSectors; /* reserved sectors */ 152 u_int bpbFATs; /* number of FATs */ 153 u_int bpbRootDirEnts; /* root directory entries */ 154 u_int bpbSectors; /* total sectors */ 155 u_int bpbMedia; /* media descriptor */ 156 u_int bpbFATsecs; /* sectors per FAT */ 157 u_int bpbSecPerTrack; /* sectors per track */ 158 u_int bpbHeads; /* drive heads */ 159 u_int bpbHiddenSecs; /* hidden sectors */ 160 u_int bpbHugeSectors; /* big total sectors */ 161 u_int bpbBigFATsecs; /* big sectors per FAT */ 162 u_int bpbRootClust; /* root directory start cluster */ | 146}; 147 148struct bpb { 149 u_int bpbBytesPerSec; /* bytes per sector */ 150 u_int bpbSecPerClust; /* sectors per cluster */ 151 u_int bpbResSectors; /* reserved sectors */ 152 u_int bpbFATs; /* number of FATs */ 153 u_int bpbRootDirEnts; /* root directory entries */ 154 u_int bpbSectors; /* total sectors */ 155 u_int bpbMedia; /* media descriptor */ 156 u_int bpbFATsecs; /* sectors per FAT */ 157 u_int bpbSecPerTrack; /* sectors per track */ 158 u_int bpbHeads; /* drive heads */ 159 u_int bpbHiddenSecs; /* hidden sectors */ 160 u_int bpbHugeSectors; /* big total sectors */ 161 u_int bpbBigFATsecs; /* big sectors per FAT */ 162 u_int bpbRootClust; /* root directory start cluster */ |
163 u_int bpbFSInfo; /* file system info sector */ 164 u_int bpbBackup; /* backup boot sector */ | 163 u_int bpbFSInfo; /* file system info sector */ 164 u_int bpbBackup; /* backup boot sector */ |
165}; 166 167#define BPBGAP 0, 0, 0, 0, 0, 0 168 | 165}; 166 167#define BPBGAP 0, 0, 0, 0, 0, 0 168 |
169#define INIT(a, b, c, d, e, f, g, h, i, j) \ 170 { .bpbBytesPerSec = a, .bpbSecPerClust = b, .bpbResSectors = c, .bpbFATs = d, .bpbRootDirEnts = e, \ 171 .bpbSectors = f, .bpbMedia = g, .bpbFATsecs = h, .bpbSecPerTrack = i, .bpbHeads = j, } | |
172static struct { 173 const char *name; 174 struct bpb bpb; 175} const stdfmt[] = { | 169static struct { 170 const char *name; 171 struct bpb bpb; 172} const stdfmt[] = { |
176 {"160", INIT(512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1)}, 177 {"180", INIT(512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1)}, 178 {"320", INIT(512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2)}, 179 {"360", INIT(512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2)}, 180 {"640", INIT(512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2)}, 181 {"720", INIT(512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2)}, 182 {"1200", INIT(512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2)}, 183 {"1232", INIT(1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2)}, 184 {"1440", INIT(512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2)}, 185 {"2880", INIT(512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2)} | 173 {"160", {512, 1, 1, 2, 64, 320, 0xfe, 1, 8, 1, BPBGAP}}, 174 {"180", {512, 1, 1, 2, 64, 360, 0xfc, 2, 9, 1, BPBGAP}}, 175 {"320", {512, 2, 1, 2, 112, 640, 0xff, 1, 8, 2, BPBGAP}}, 176 {"360", {512, 2, 1, 2, 112, 720, 0xfd, 2, 9, 2, BPBGAP}}, 177 {"640", {512, 2, 1, 2, 112, 1280, 0xfb, 2, 8, 2, BPBGAP}}, 178 {"720", {512, 2, 1, 2, 112, 1440, 0xf9, 3, 9, 2, BPBGAP}}, 179 {"1200", {512, 1, 1, 2, 224, 2400, 0xf9, 7, 15, 2, BPBGAP}}, 180 {"1232", {1024,1, 1, 2, 192, 1232, 0xfe, 2, 8, 2, BPBGAP}}, 181 {"1440", {512, 1, 1, 2, 224, 2880, 0xf0, 9, 18, 2, BPBGAP}}, 182 {"2880", {512, 2, 1, 2, 240, 5760, 0xf0, 9, 36, 2, BPBGAP}} |
186}; 187 188static const u_int8_t bootcode[] = { 189 0xfa, /* cli */ 190 0x31, 0xc0, /* xor ax,ax */ 191 0x8e, 0xd0, /* mov ss,ax */ 192 0xbc, 0x00, 0x7c, /* mov sp,7c00h */ 193 0xfb, /* sti */ --- 295 unchanged lines hidden (view full) --- 489 if (!strchr(bname, '/')) { 490 snprintf(buf, sizeof(buf), "/boot/%s", bname); 491 if (!(bname = strdup(buf))) 492 err(1, NULL); 493 } 494 if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb)) 495 err(1, "%s", bname); 496 if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bpbBytesPerSec || | 183}; 184 185static const u_int8_t bootcode[] = { 186 0xfa, /* cli */ 187 0x31, 0xc0, /* xor ax,ax */ 188 0x8e, 0xd0, /* mov ss,ax */ 189 0xbc, 0x00, 0x7c, /* mov sp,7c00h */ 190 0xfb, /* sti */ --- 295 unchanged lines hidden (view full) --- 486 if (!strchr(bname, '/')) { 487 snprintf(buf, sizeof(buf), "/boot/%s", bname); 488 if (!(bname = strdup(buf))) 489 err(1, NULL); 490 } 491 if ((fd1 = open(bname, O_RDONLY)) == -1 || fstat(fd1, &sb)) 492 err(1, "%s", bname); 493 if (!S_ISREG(sb.st_mode) || sb.st_size % bpb.bpbBytesPerSec || |
497 sb.st_size < bpb.bpbBytesPerSec || sb.st_size > bpb.bpbBytesPerSec * MAXU16) | 494 sb.st_size < bpb.bpbBytesPerSec || 495 sb.st_size > bpb.bpbBytesPerSec * MAXU16) |
498 errx(1, "%s: inappropriate file type or format", bname); 499 bss = sb.st_size / bpb.bpbBytesPerSec; 500 } 501 if (!bpb.bpbFATs) 502 bpb.bpbFATs = 2; 503 if (!fat) { 504 if (bpb.bpbHugeSectors < (bpb.bpbResSectors ? bpb.bpbResSectors : bss) + 505 howmany((RESFTE + (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1)) * | 496 errx(1, "%s: inappropriate file type or format", bname); 497 bss = sb.st_size / bpb.bpbBytesPerSec; 498 } 499 if (!bpb.bpbFATs) 500 bpb.bpbFATs = 2; 501 if (!fat) { 502 if (bpb.bpbHugeSectors < (bpb.bpbResSectors ? bpb.bpbResSectors : bss) + 503 howmany((RESFTE + (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1)) * |
506 ((bpb.bpbSecPerClust ? 16 : 12) / BPN), bpb.bpbBytesPerSec * NPB) * | 504 (bpb.bpbSecPerClust ? 16 : 12) / BPN, 505 bpb.bpbBytesPerSec * NPB) * |
507 bpb.bpbFATs + 508 howmany(bpb.bpbRootDirEnts ? bpb.bpbRootDirEnts : DEFRDE, 509 bpb.bpbBytesPerSec / sizeof(struct de)) + 510 (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1) * | 506 bpb.bpbFATs + 507 howmany(bpb.bpbRootDirEnts ? bpb.bpbRootDirEnts : DEFRDE, 508 bpb.bpbBytesPerSec / sizeof(struct de)) + 509 (bpb.bpbSecPerClust ? MINCLS16 : MAXCLS12 + 1) * |
511 (bpb.bpbSecPerClust ? bpb.bpbSecPerClust : howmany(DEFBLK, bpb.bpbBytesPerSec))) | 510 (bpb.bpbSecPerClust ? bpb.bpbSecPerClust : 511 howmany(DEFBLK, bpb.bpbBytesPerSec))) |
512 fat = 12; 513 else if (bpb.bpbRootDirEnts || bpb.bpbHugeSectors < 514 (bpb.bpbResSectors ? bpb.bpbResSectors : bss) + | 512 fat = 12; 513 else if (bpb.bpbRootDirEnts || bpb.bpbHugeSectors < 514 (bpb.bpbResSectors ? bpb.bpbResSectors : bss) + |
515 howmany((RESFTE + MAXCLS16) * 2, bpb.bpbBytesPerSec) * bpb.bpbFATs + | 515 howmany((RESFTE + MAXCLS16) * 2, bpb.bpbBytesPerSec) * 516 bpb.bpbFATs + |
516 howmany(DEFRDE, bpb.bpbBytesPerSec / sizeof(struct de)) + 517 (MAXCLS16 + 1) * | 517 howmany(DEFRDE, bpb.bpbBytesPerSec / sizeof(struct de)) + 518 (MAXCLS16 + 1) * |
518 (bpb.bpbSecPerClust ? bpb.bpbSecPerClust : howmany(8192, bpb.bpbBytesPerSec))) | 519 (bpb.bpbSecPerClust ? bpb.bpbSecPerClust : 520 howmany(8192, bpb.bpbBytesPerSec))) |
519 fat = 16; 520 else 521 fat = 32; 522 } 523 x = bss; 524 if (fat == 32) { 525 if (!bpb.bpbFSInfo) { 526 if (x == MAXU16 || x == bpb.bpbBackup) --- 7 unchanged lines hidden (view full) --- 534 errx(1, "no room for backup sector"); 535 bpb.bpbBackup = x; 536 } else if (bpb.bpbBackup != MAXU16 && bpb.bpbBackup == bpb.bpbFSInfo) 537 errx(1, "backup sector would overwrite info sector"); 538 if (bpb.bpbBackup != MAXU16 && x <= bpb.bpbBackup) 539 x = bpb.bpbBackup + 1; 540 } 541 if (!bpb.bpbResSectors) | 521 fat = 16; 522 else 523 fat = 32; 524 } 525 x = bss; 526 if (fat == 32) { 527 if (!bpb.bpbFSInfo) { 528 if (x == MAXU16 || x == bpb.bpbBackup) --- 7 unchanged lines hidden (view full) --- 536 errx(1, "no room for backup sector"); 537 bpb.bpbBackup = x; 538 } else if (bpb.bpbBackup != MAXU16 && bpb.bpbBackup == bpb.bpbFSInfo) 539 errx(1, "backup sector would overwrite info sector"); 540 if (bpb.bpbBackup != MAXU16 && x <= bpb.bpbBackup) 541 x = bpb.bpbBackup + 1; 542 } 543 if (!bpb.bpbResSectors) |
542 bpb.bpbResSectors = fat == 32 ? MAX(x, MAX(16384 / bpb.bpbBytesPerSec, 4)) : x; | 544 bpb.bpbResSectors = fat == 32 ? 545 MAX(x, MAX(16384 / bpb.bpbBytesPerSec, 4)) : x; |
543 else if (bpb.bpbResSectors < x) | 546 else if (bpb.bpbResSectors < x) |
544 errx(1, "too few reserved sectors (need %d have %d)", x, bpb.bpbResSectors); | 547 errx(1, "too few reserved sectors (need %d have %d)", x, 548 bpb.bpbResSectors); |
545 if (fat != 32 && !bpb.bpbRootDirEnts) 546 bpb.bpbRootDirEnts = DEFRDE; 547 rds = howmany(bpb.bpbRootDirEnts, bpb.bpbBytesPerSec / sizeof(struct de)); 548 if (!bpb.bpbSecPerClust) | 549 if (fat != 32 && !bpb.bpbRootDirEnts) 550 bpb.bpbRootDirEnts = DEFRDE; 551 rds = howmany(bpb.bpbRootDirEnts, bpb.bpbBytesPerSec / sizeof(struct de)); 552 if (!bpb.bpbSecPerClust) |
549 for (bpb.bpbSecPerClust = howmany(fat == 16 ? DEFBLK16 : DEFBLK, bpb.bpbBytesPerSec); | 553 for (bpb.bpbSecPerClust = howmany(fat == 16 ? DEFBLK16 : 554 DEFBLK, bpb.bpbBytesPerSec); |
550 bpb.bpbSecPerClust < MAXSPC && 551 bpb.bpbResSectors + 552 howmany((RESFTE + maxcls(fat)) * (fat / BPN), | 555 bpb.bpbSecPerClust < MAXSPC && 556 bpb.bpbResSectors + 557 howmany((RESFTE + maxcls(fat)) * (fat / BPN), |
553 bpb.bpbBytesPerSec * NPB) * bpb.bpbFATs + | 558 bpb.bpbBytesPerSec * NPB) * 559 bpb.bpbFATs + |
554 rds + | 560 rds + |
555 (u_int64_t)(maxcls(fat) + 1) * bpb.bpbSecPerClust <= bpb.bpbHugeSectors; 556 bpb.bpbSecPerClust <<= 1); | 561 (u_int64_t) (maxcls(fat) + 1) * 562 bpb.bpbSecPerClust <= bpb.bpbHugeSectors; 563 bpb.bpbSecPerClust <<= 1) 564 continue; |
557 if (fat != 32 && bpb.bpbBigFATsecs > MAXU16) 558 errx(1, "too many sectors/FAT for FAT12/16"); 559 x1 = bpb.bpbResSectors + rds; 560 x = bpb.bpbBigFATsecs ? bpb.bpbBigFATsecs : 1; 561 if (x1 + (u_int64_t)x * bpb.bpbFATs > bpb.bpbHugeSectors) 562 errx(1, "meta data exceeds file system size"); 563 x1 += x * bpb.bpbFATs; 564 x = (u_int64_t)(bpb.bpbHugeSectors - x1) * bpb.bpbBytesPerSec * NPB / | 565 if (fat != 32 && bpb.bpbBigFATsecs > MAXU16) 566 errx(1, "too many sectors/FAT for FAT12/16"); 567 x1 = bpb.bpbResSectors + rds; 568 x = bpb.bpbBigFATsecs ? bpb.bpbBigFATsecs : 1; 569 if (x1 + (u_int64_t)x * bpb.bpbFATs > bpb.bpbHugeSectors) 570 errx(1, "meta data exceeds file system size"); 571 x1 += x * bpb.bpbFATs; 572 x = (u_int64_t)(bpb.bpbHugeSectors - x1) * bpb.bpbBytesPerSec * NPB / |
565 (bpb.bpbSecPerClust * bpb.bpbBytesPerSec * NPB + fat / BPN * bpb.bpbFATs); | 573 (bpb.bpbSecPerClust * bpb.bpbBytesPerSec * NPB + fat / 574 BPN * bpb.bpbFATs); |
566 x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN), 567 bpb.bpbBytesPerSec * NPB); 568 if (!bpb.bpbBigFATsecs) { 569 bpb.bpbBigFATsecs = x2; 570 x1 += (bpb.bpbBigFATsecs - 1) * bpb.bpbFATs; 571 } 572 cls = (bpb.bpbHugeSectors - x1) / bpb.bpbSecPerClust; | 575 x2 = howmany((RESFTE + MIN(x, maxcls(fat))) * (fat / BPN), 576 bpb.bpbBytesPerSec * NPB); 577 if (!bpb.bpbBigFATsecs) { 578 bpb.bpbBigFATsecs = x2; 579 x1 += (bpb.bpbBigFATsecs - 1) * bpb.bpbFATs; 580 } 581 cls = (bpb.bpbHugeSectors - x1) / bpb.bpbSecPerClust; |
573 x = (u_int64_t)bpb.bpbBigFATsecs * bpb.bpbBytesPerSec * NPB / (fat / BPN) - RESFTE; | 582 x = (u_int64_t)bpb.bpbBigFATsecs * bpb.bpbBytesPerSec * NPB / (fat / BPN) - 583 RESFTE; |
574 if (cls > x) 575 cls = x; 576 if (bpb.bpbBigFATsecs < x2) 577 warnx("warning: sectors/FAT limits file system to %u clusters", 578 cls); 579 if (cls < mincls(fat)) 580 errx(1, "%u clusters too few clusters for FAT%u, need %u", cls, fat, 581 mincls(fat)); --- 21 unchanged lines hidden (view full) --- 603 } 604 print_bpb(&bpb); 605 if (!opt_N) { 606 gettimeofday(&tv, NULL); 607 now = tv.tv_sec; 608 tm = localtime(&now); 609 if (!(img = malloc(bpb.bpbBytesPerSec))) 610 err(1, NULL); | 584 if (cls > x) 585 cls = x; 586 if (bpb.bpbBigFATsecs < x2) 587 warnx("warning: sectors/FAT limits file system to %u clusters", 588 cls); 589 if (cls < mincls(fat)) 590 errx(1, "%u clusters too few clusters for FAT%u, need %u", cls, fat, 591 mincls(fat)); --- 21 unchanged lines hidden (view full) --- 613 } 614 print_bpb(&bpb); 615 if (!opt_N) { 616 gettimeofday(&tv, NULL); 617 now = tv.tv_sec; 618 tm = localtime(&now); 619 if (!(img = malloc(bpb.bpbBytesPerSec))) 620 err(1, NULL); |
611 dir = bpb.bpbResSectors + (bpb.bpbFATsecs ? bpb.bpbFATsecs : bpb.bpbBigFATsecs) * bpb.bpbFATs; | 621 dir = bpb.bpbResSectors + (bpb.bpbFATsecs ? bpb.bpbFATsecs : 622 bpb.bpbBigFATsecs) * bpb.bpbFATs; |
612 memset(&si_sa, 0, sizeof(si_sa)); 613 si_sa.sa_handler = infohandler; 614 if (sigaction(SIGINFO, &si_sa, NULL) == -1) 615 err(1, "sigaction SIGINFO"); 616 for (lsn = 0; lsn < dir + (fat == 32 ? bpb.bpbSecPerClust : rds); lsn++) { 617 if (got_siginfo) { 618 fprintf(stderr,"%s: writing sector %u of %u (%u%%)\n", 619 fname, lsn, --- 13 unchanged lines hidden (view full) --- 633 if (opt_B && x < bss) { 634 if ((n = read(fd1, img, bpb.bpbBytesPerSec)) == -1) 635 err(1, "%s", bname); 636 if ((unsigned)n != bpb.bpbBytesPerSec) 637 errx(1, "%s: can't read sector %u", bname, x); 638 } else 639 memset(img, 0, bpb.bpbBytesPerSec); 640 if (!lsn || | 623 memset(&si_sa, 0, sizeof(si_sa)); 624 si_sa.sa_handler = infohandler; 625 if (sigaction(SIGINFO, &si_sa, NULL) == -1) 626 err(1, "sigaction SIGINFO"); 627 for (lsn = 0; lsn < dir + (fat == 32 ? bpb.bpbSecPerClust : rds); lsn++) { 628 if (got_siginfo) { 629 fprintf(stderr,"%s: writing sector %u of %u (%u%%)\n", 630 fname, lsn, --- 13 unchanged lines hidden (view full) --- 644 if (opt_B && x < bss) { 645 if ((n = read(fd1, img, bpb.bpbBytesPerSec)) == -1) 646 err(1, "%s", bname); 647 if ((unsigned)n != bpb.bpbBytesPerSec) 648 errx(1, "%s: can't read sector %u", bname, x); 649 } else 650 memset(img, 0, bpb.bpbBytesPerSec); 651 if (!lsn || |
641 (fat == 32 && bpb.bpbBackup != MAXU16 && lsn == bpb.bpbBackup)) { | 652 (fat == 32 && bpb.bpbBackup != MAXU16 && 653 lsn == bpb.bpbBackup)) { |
642 x1 = sizeof(struct bs); 643 bsbpb = (struct bsbpb *)(img + x1); 644 mk2(bsbpb->bpbBytesPerSec, bpb.bpbBytesPerSec); 645 mk1(bsbpb->bpbSecPerClust, bpb.bpbSecPerClust); 646 mk2(bsbpb->bpbResSectors, bpb.bpbResSectors); 647 mk1(bsbpb->bpbFATs, bpb.bpbFATs); 648 mk2(bsbpb->bpbRootDirEnts, bpb.bpbRootDirEnts); 649 mk2(bsbpb->bpbSectors, bpb.bpbSectors); --- 47 unchanged lines hidden (view full) --- 697 lsn == bpb.bpbBackup + bpb.bpbFSInfo))) { 698 mk4(img, 0x41615252); 699 mk4(img + MINBPS - 28, 0x61417272); 700 mk4(img + MINBPS - 24, 0xffffffff); 701 mk4(img + MINBPS - 20, bpb.bpbRootClust); 702 mk2(img + MINBPS - 2, DOSMAGIC); 703 } else if (lsn >= bpb.bpbResSectors && lsn < dir && 704 !((lsn - bpb.bpbResSectors) % | 654 x1 = sizeof(struct bs); 655 bsbpb = (struct bsbpb *)(img + x1); 656 mk2(bsbpb->bpbBytesPerSec, bpb.bpbBytesPerSec); 657 mk1(bsbpb->bpbSecPerClust, bpb.bpbSecPerClust); 658 mk2(bsbpb->bpbResSectors, bpb.bpbResSectors); 659 mk1(bsbpb->bpbFATs, bpb.bpbFATs); 660 mk2(bsbpb->bpbRootDirEnts, bpb.bpbRootDirEnts); 661 mk2(bsbpb->bpbSectors, bpb.bpbSectors); --- 47 unchanged lines hidden (view full) --- 709 lsn == bpb.bpbBackup + bpb.bpbFSInfo))) { 710 mk4(img, 0x41615252); 711 mk4(img + MINBPS - 28, 0x61417272); 712 mk4(img + MINBPS - 24, 0xffffffff); 713 mk4(img + MINBPS - 20, bpb.bpbRootClust); 714 mk2(img + MINBPS - 2, DOSMAGIC); 715 } else if (lsn >= bpb.bpbResSectors && lsn < dir && 716 !((lsn - bpb.bpbResSectors) % |
705 (bpb.bpbFATsecs ? bpb.bpbFATsecs : bpb.bpbBigFATsecs))) { | 717 (bpb.bpbFATsecs ? bpb.bpbFATsecs : 718 bpb.bpbBigFATsecs))) { |
706 mk1(img[0], bpb.bpbMedia); 707 for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++) 708 mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff); 709 } else if (lsn == dir && opt_L) { 710 de = (struct de *)img; 711 mklabel(de->deName, opt_L); 712 mk1(de->deAttributes, 050); 713 x = (u_int)tm->tm_hour << 11 | --- 98 unchanged lines hidden (view full) --- 812 } 813 } 814 815 /* Maybe it's a fixed drive */ 816 if (lp == NULL) { 817 if (bpb->bpbBytesPerSec) 818 dlp.d_secsize = bpb->bpbBytesPerSec; 819 if (ioctl(fd, DIOCGDINFO, &dlp) == -1) { | 719 mk1(img[0], bpb.bpbMedia); 720 for (x = 1; x < fat * (fat == 32 ? 3 : 2) / 8; x++) 721 mk1(img[x], fat == 32 && x % 4 == 3 ? 0x0f : 0xff); 722 } else if (lsn == dir && opt_L) { 723 de = (struct de *)img; 724 mklabel(de->deName, opt_L); 725 mk1(de->deAttributes, 050); 726 x = (u_int)tm->tm_hour << 11 | --- 98 unchanged lines hidden (view full) --- 825 } 826 } 827 828 /* Maybe it's a fixed drive */ 829 if (lp == NULL) { 830 if (bpb->bpbBytesPerSec) 831 dlp.d_secsize = bpb->bpbBytesPerSec; 832 if (ioctl(fd, DIOCGDINFO, &dlp) == -1) { |
820 if (bpb->bpbBytesPerSec == 0 && ioctl(fd, DIOCGSECTORSIZE, &dlp.d_secsize) == -1) | 833 if (bpb->bpbBytesPerSec == 0 && ioctl(fd, DIOCGSECTORSIZE, 834 &dlp.d_secsize) == -1) |
821 errx(1, "Cannot get sector size, %s", strerror(errno)); 822 823 dlp.d_secperunit = ms / dlp.d_secsize; 824 | 835 errx(1, "Cannot get sector size, %s", strerror(errno)); 836 837 dlp.d_secperunit = ms / dlp.d_secsize; 838 |
825 if (bpb->bpbSecPerTrack == 0 && ioctl(fd, DIOCGFWSECTORS, &dlp.d_nsectors) == -1) { | 839 if (bpb->bpbSecPerTrack == 0 && ioctl(fd, DIOCGFWSECTORS, 840 &dlp.d_nsectors) == -1) { |
826 warnx("Cannot get number of sectors per track, %s", strerror(errno)); 827 dlp.d_nsectors = 63; 828 } | 841 warnx("Cannot get number of sectors per track, %s", strerror(errno)); 842 dlp.d_nsectors = 63; 843 } |
829 if (bpb->bpbHeads == 0 && ioctl(fd, DIOCGFWHEADS, &dlp.d_ntracks) == -1) { | 844 if (bpb->bpbHeads == 0 && 845 ioctl(fd, DIOCGFWHEADS, &dlp.d_ntracks) == -1) { |
830 warnx("Cannot get number of heads, %s", strerror(errno)); 831 if (dlp.d_secperunit <= 63*1*1024) 832 dlp.d_ntracks = 1; 833 else if (dlp.d_secperunit <= 63*16*1024) 834 dlp.d_ntracks = 16; 835 else 836 dlp.d_ntracks = 255; 837 } --- 16 unchanged lines hidden (view full) --- 854} 855 856/* 857 * Print out BPB values. 858 */ 859static void 860print_bpb(struct bpb *bpb) 861{ | 846 warnx("Cannot get number of heads, %s", strerror(errno)); 847 if (dlp.d_secperunit <= 63*1*1024) 848 dlp.d_ntracks = 1; 849 else if (dlp.d_secperunit <= 63*16*1024) 850 dlp.d_ntracks = 16; 851 else 852 dlp.d_ntracks = 255; 853 } --- 16 unchanged lines hidden (view full) --- 870} 871 872/* 873 * Print out BPB values. 874 */ 875static void 876print_bpb(struct bpb *bpb) 877{ |
862 printf("BytesPerSec=%u SecPerClust=%u ResSectors=%u FATs=%u", bpb->bpbBytesPerSec, bpb->bpbSecPerClust, bpb->bpbResSectors, | 878 printf("BytesPerSec=%u SecPerClust=%u ResSectors=%u FATs=%u", 879 bpb->bpbBytesPerSec, bpb->bpbSecPerClust, bpb->bpbResSectors, |
863 bpb->bpbFATs); 864 if (bpb->bpbRootDirEnts) 865 printf(" RootDirEnts=%u", bpb->bpbRootDirEnts); 866 if (bpb->bpbSectors) 867 printf(" Sectors=%u", bpb->bpbSectors); 868 printf(" Media=%#x", bpb->bpbMedia); 869 if (bpb->bpbFATsecs) 870 printf(" FATsecs=%u", bpb->bpbFATsecs); | 880 bpb->bpbFATs); 881 if (bpb->bpbRootDirEnts) 882 printf(" RootDirEnts=%u", bpb->bpbRootDirEnts); 883 if (bpb->bpbSectors) 884 printf(" Sectors=%u", bpb->bpbSectors); 885 printf(" Media=%#x", bpb->bpbMedia); 886 if (bpb->bpbFATsecs) 887 printf(" FATsecs=%u", bpb->bpbFATsecs); |
871 printf(" SecPerTrack=%u Heads=%u HiddenSecs=%u", bpb->bpbSecPerTrack, bpb->bpbHeads, bpb->bpbHiddenSecs); | 888 printf(" SecPerTrack=%u Heads=%u HiddenSecs=%u", bpb->bpbSecPerTrack, 889 bpb->bpbHeads, bpb->bpbHiddenSecs); |
872 if (bpb->bpbHugeSectors) 873 printf(" HugeSectors=%u", bpb->bpbHugeSectors); 874 if (!bpb->bpbFATsecs) { | 890 if (bpb->bpbHugeSectors) 891 printf(" HugeSectors=%u", bpb->bpbHugeSectors); 892 if (!bpb->bpbFATsecs) { |
875 printf(" FATsecs=%u RootCluster=%u", bpb->bpbBigFATsecs, bpb->bpbRootClust); | 893 printf(" FATsecs=%u RootCluster=%u", bpb->bpbBigFATsecs, 894 bpb->bpbRootClust); |
876 printf(" FSInfo="); 877 printf(bpb->bpbFSInfo == MAXU16 ? "%#x" : "%u", bpb->bpbFSInfo); 878 printf(" Backup="); 879 printf(bpb->bpbBackup == MAXU16 ? "%#x" : "%u", bpb->bpbBackup); 880 } 881 printf("\n"); 882} 883 --- 30 unchanged lines hidden (view full) --- 914 * Same for off_t, with optional skmgpP suffix 915 */ 916static off_t 917argtooff(const char *arg, const char *msg) 918{ 919 char *s; 920 off_t x; 921 | 895 printf(" FSInfo="); 896 printf(bpb->bpbFSInfo == MAXU16 ? "%#x" : "%u", bpb->bpbFSInfo); 897 printf(" Backup="); 898 printf(bpb->bpbBackup == MAXU16 ? "%#x" : "%u", bpb->bpbBackup); 899 } 900 printf("\n"); 901} 902 --- 30 unchanged lines hidden (view full) --- 933 * Same for off_t, with optional skmgpP suffix 934 */ 935static off_t 936argtooff(const char *arg, const char *msg) 937{ 938 char *s; 939 off_t x; 940 |
941 errno = 0; |
|
922 x = strtoll(arg, &s, 0); 923 /* allow at most one extra char */ 924 if (errno || x < 0 || (s[0] && s[1]) ) 925 errx(1, "%s: bad %s", arg, msg); 926 if (*s) { /* the extra char is the multiplier */ 927 switch (*s) { 928 default: 929 errx(1, "%s: bad %s", arg, msg); 930 /* notreached */ 931 | 942 x = strtoll(arg, &s, 0); 943 /* allow at most one extra char */ 944 if (errno || x < 0 || (s[0] && s[1]) ) 945 errx(1, "%s: bad %s", arg, msg); 946 if (*s) { /* the extra char is the multiplier */ 947 switch (*s) { 948 default: 949 errx(1, "%s: bad %s", arg, msg); 950 /* notreached */ 951 |
932 case 's': /* sector */ | 952 case 's': /* sector */ |
933 case 'S': | 953 case 'S': |
934 x <<= 9; /* times 512 */ | 954 x <<= 9; /* times 512 */ |
935 break; 936 | 955 break; 956 |
937 case 'k': /* kilobyte */ | 957 case 'k': /* kilobyte */ |
938 case 'K': | 958 case 'K': |
939 x <<= 10; /* times 1024 */ | 959 x <<= 10; /* times 1024 */ |
940 break; 941 | 960 break; 961 |
942 case 'm': /* megabyte */ | 962 case 'm': /* megabyte */ |
943 case 'M': | 963 case 'M': |
944 x <<= 20; /* times 1024*1024 */ | 964 x <<= 20; /* times 1024*1024 */ |
945 break; 946 | 965 break; 966 |
947 case 'g': /* gigabyte */ | 967 case 'g': /* gigabyte */ |
948 case 'G': | 968 case 'G': |
949 x <<= 30; /* times 1024*1024*1024 */ | 969 x <<= 30; /* times 1024*1024*1024 */ |
950 break; 951 | 970 break; 971 |
952 case 'p': /* partition start */ 953 case 'P': /* partition start */ 954 case 'l': /* partition length */ 955 case 'L': /* partition length */ | 972 case 'p': /* partition start */ 973 case 'P': 974 case 'l': /* partition length */ 975 case 'L': |
956 errx(1, "%s: not supported yet %s", arg, msg); 957 /* notreached */ 958 } 959 } 960 return x; 961} 962 963/* --- 80 unchanged lines hidden --- | 976 errx(1, "%s: not supported yet %s", arg, msg); 977 /* notreached */ 978 } 979 } 980 return x; 981} 982 983/* --- 80 unchanged lines hidden --- |