Lines Matching defs:boot

31 __RCSID("$NetBSD: boot.c,v 1.22 2020/01/11 16:29:07 christos Exp $");
33 "$FreeBSD: stable/11/sbin/fsck_msdosfs/boot.c 360490 2020-04-30 06:34:34Z delphij $";
48 readboot(int dosfs, struct bootblock *boot)
55 perr("could not read boot block");
60 pfatal("Invalid signature in boot block: %02x%02x",
65 memset(boot, 0, sizeof *boot);
66 boot->ValidFat = -1;
71 boot->bpbBytesPerSec = block[11] + (block[12] << 8);
72 if (boot->bpbBytesPerSec < DOSBOOTBLOCKSIZE_REAL ||
73 boot->bpbBytesPerSec > DOSBOOTBLOCKSIZE ||
74 !powerof2(boot->bpbBytesPerSec)) {
75 pfatal("Invalid sector size: %u", boot->bpbBytesPerSec);
80 boot->bpbSecPerClust = block[13];
81 if (boot->bpbSecPerClust == 0 || !powerof2(boot->bpbSecPerClust)) {
82 pfatal("Invalid cluster size: %u", boot->bpbSecPerClust);
87 boot->bpbResSectors = block[14] + (block[15] << 8);
88 if (boot->bpbResSectors < 1) {
90 boot->bpbResSectors);
95 boot->bpbFATs = block[16];
96 if (boot->bpbFATs == 0) {
97 pfatal("Invalid number of FATs: %u", boot->bpbFATs);
102 boot->bpbRootDirEnts = block[17] + (block[18] << 8);
103 if (!boot->bpbRootDirEnts) {
105 boot->flags |= FAT32;
109 boot->bpbSectors = block[19] + (block[20] << 8);
110 if (boot->bpbSectors != 0 && (boot->flags & FAT32)) {
112 boot->bpbSectors);
117 boot->bpbMedia = block[21];
120 boot->bpbFATsmall = block[22] + (block[23] << 8);
121 if (boot->bpbFATsmall != 0 && (boot->flags & FAT32)) {
123 boot->bpbFATsmall);
128 boot->SecPerTrack = block[24] + (block[25] << 8);
129 boot->bpbHeads = block[26] + (block[27] << 8);
132 boot->bpbHiddenSecs = block[28] + (block[29] << 8) +
136 boot->bpbHugeSectors = block[32] + (block[33] << 8) +
138 if (boot->bpbHugeSectors == 0) {
139 if (boot->flags & FAT32) {
142 } else if (boot->bpbSectors == 0) {
146 boot->NumSectors = boot->bpbSectors;
148 if (boot->bpbSectors != 0) {
152 boot->NumSectors = boot->bpbHugeSectors;
155 if (boot->flags & FAT32) {
163 boot->FATsecs = block[36] + (block[37] << 8)
167 boot->ValidFat = block[40] & 0x0f;
181 boot->bpbRootClust = block[44] + (block[45] << 8)
185 boot->bpbFSInfo = block[48] + (block[49] << 8);
187 /* Sector number of the backup boot block, ignored */
188 boot->bpbBackup = block[50] + (block[51] << 8);
191 if (boot->bpbFSInfo == 0) {
202 if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec,
203 SEEK_SET) != boot->bpbFSInfo * boot->bpbBytesPerSec
228 if (lseek(dosfs, boot->bpbFSInfo *
229 boot->bpbBytesPerSec, SEEK_SET)
230 != boot->bpbFSInfo * boot->bpbBytesPerSec
238 boot->bpbFSInfo = 0;
241 boot->FSFree = fsinfo[0x1e8] + (fsinfo[0x1e9] << 8)
244 boot->FSNext = fsinfo[0x1ec] + (fsinfo[0x1ed] << 8)
250 boot->FATsecs = boot->bpbFATsmall;
253 if (boot->FATsecs < 1 || boot->FATsecs > UINT32_MAX / boot->bpbFATs) {
255 boot->bpbFATs, (size_t)boot->FATsecs);
259 boot->FirstCluster = (boot->bpbRootDirEnts * 32 +
260 boot->bpbBytesPerSec - 1) / boot->bpbBytesPerSec +
261 boot->bpbResSectors + boot->bpbFATs * boot->FATsecs;
263 if (boot->FirstCluster + boot->bpbSecPerClust > boot->NumSectors) {
265 boot->FirstCluster);
273 boot->NumClusters =
274 (boot->NumSectors - boot->FirstCluster) / boot->bpbSecPerClust;
276 if (boot->flags & FAT32) {
277 if (boot->NumClusters > (CLUST_RSRVD & CLUST32_MASK)) {
279 boot->NumClusters);
282 if (boot->NumClusters < (CLUST_RSRVD & CLUST16_MASK)) {
284 boot->NumClusters);
287 boot->ClustMask = CLUST32_MASK;
289 if (boot->bpbRootClust < CLUST_FIRST ||
290 boot->bpbRootClust >= boot->NumClusters) {
292 boot->bpbRootClust);
295 } else if (boot->NumClusters < (CLUST_RSRVD&CLUST12_MASK)) {
296 boot->ClustMask = CLUST12_MASK;
297 } else if (boot->NumClusters < (CLUST_RSRVD&CLUST16_MASK)) {
298 boot->ClustMask = CLUST16_MASK;
301 boot->NumClusters);
305 switch (boot->ClustMask) {
307 boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 4;
310 boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec) / 2;
313 boot->NumFatEntries = (boot->FATsecs * boot->bpbBytesPerSec * 2) / 3;
317 if (boot->NumFatEntries < boot->NumClusters) {
319 boot->NumClusters, boot->FATsecs);
328 boot->NumClusters += CLUST_FIRST;
330 boot->ClusterSize = boot->bpbBytesPerSec * boot->bpbSecPerClust;
332 boot->NumFiles = 1;
333 boot->NumFree = 0;
339 writefsinfo(int dosfs, struct bootblock *boot)
343 if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET)
344 != boot->bpbFSInfo * boot->bpbBytesPerSec
349 fsinfo[0x1e8] = (u_char)boot->FSFree;
350 fsinfo[0x1e9] = (u_char)(boot->FSFree >> 8);
351 fsinfo[0x1ea] = (u_char)(boot->FSFree >> 16);
352 fsinfo[0x1eb] = (u_char)(boot->FSFree >> 24);
353 fsinfo[0x1ec] = (u_char)boot->FSNext;
354 fsinfo[0x1ed] = (u_char)(boot->FSNext >> 8);
355 fsinfo[0x1ee] = (u_char)(boot->FSNext >> 16);
356 fsinfo[0x1ef] = (u_char)(boot->FSNext >> 24);
357 if (lseek(dosfs, boot->bpbFSInfo * boot->bpbBytesPerSec, SEEK_SET)
358 != boot->bpbFSInfo * boot->bpbBytesPerSec