2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
5 * Copyright (c) 1995 Martin Husemann
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 static int checkclnum(struct bootblock *, u_int, cl_t, cl_t *);
38 static int clustdiffer(cl_t, cl_t *, cl_t *, u_int);
39 static int tryclear(struct bootblock *, struct fatEntry *, cl_t, cl_t *);
40 static int _readfat(int, struct bootblock *, u_int, u_char **);
43 * Check a cluster number for valid value
46 checkclnum(struct bootblock *boot, u_int fat, cl_t cl, cl_t *next)
48 if (*next >= (CLUST_RSRVD&boot->ClustMask))
49 *next |= ~boot->ClustMask;
50 if (*next == CLUST_FREE) {
54 if (*next == CLUST_BAD) {
58 if (*next < CLUST_FIRST
59 || (*next >= boot->NumClusters && *next < CLUST_EOFS)) {
60 pwarn("Cluster %u in FAT %d continues with %s cluster number %u\n",
62 *next < CLUST_RSRVD ? "out of range" : "reserved",
63 *next&boot->ClustMask);
64 if (ask(0, "Truncate")) {
74 * Read a FAT from disk. Returns 1 if successful, 0 otherwise.
77 _readfat(int fs, struct bootblock *boot, u_int no, u_char **buffer)
81 *buffer = calloc(boot->FATsecs, boot->bpbBytesPerSec);
82 if (*buffer == NULL) {
83 perr("No space for FAT sectors (%zu)",
84 (size_t)boot->FATsecs);
88 off = boot->bpbResSectors + no * boot->FATsecs;
89 off *= boot->bpbBytesPerSec;
91 if (lseek(fs, off, SEEK_SET) != off) {
92 perr("Unable to read FAT");
96 if ((size_t)read(fs, *buffer, boot->FATsecs * boot->bpbBytesPerSec)
97 != boot->FATsecs * boot->bpbBytesPerSec) {
98 perr("Unable to read FAT");
110 * Read a FAT and decode it into internal format
113 readfat(int fs, struct bootblock *boot, u_int no, struct fatEntry **fp)
115 struct fatEntry *fat;
120 boot->NumFree = boot->NumBad = 0;
122 if (!_readfat(fs, boot, no, &buffer))
125 fat = calloc(boot->NumClusters, sizeof(struct fatEntry));
127 perr("No space for FAT clusters (%zu)",
128 (size_t)boot->NumClusters);
133 if (buffer[0] != boot->bpbMedia
134 || buffer[1] != 0xff || buffer[2] != 0xff
135 || (boot->ClustMask == CLUST16_MASK && buffer[3] != 0xff)
136 || (boot->ClustMask == CLUST32_MASK
137 && ((buffer[3]&0x0f) != 0x0f
138 || buffer[4] != 0xff || buffer[5] != 0xff
139 || buffer[6] != 0xff || (buffer[7]&0x0f) != 0x0f))) {
141 /* Windows 95 OSR2 (and possibly any later) changes
142 * the FAT signature to 0xXXffff7f for FAT16 and to
143 * 0xXXffff0fffffff07 for FAT32 upon boot, to know that the
144 * file system is dirty if it doesn't reboot cleanly.
145 * Check this special condition before errorring out.
147 if (buffer[0] == boot->bpbMedia && buffer[1] == 0xff
149 && ((boot->ClustMask == CLUST16_MASK && buffer[3] == 0x7f)
150 || (boot->ClustMask == CLUST32_MASK
151 && buffer[3] == 0x0f && buffer[4] == 0xff
152 && buffer[5] == 0xff && buffer[6] == 0xff
153 && buffer[7] == 0x07)))
156 /* just some odd byte sequence in FAT */
157 switch (boot->ClustMask) {
159 pwarn("%s (%02x%02x%02x%02x%02x%02x%02x%02x)\n",
160 "FAT starts with odd byte sequence",
161 buffer[0], buffer[1], buffer[2], buffer[3],
162 buffer[4], buffer[5], buffer[6], buffer[7]);
165 pwarn("%s (%02x%02x%02x%02x)\n",
166 "FAT starts with odd byte sequence",
167 buffer[0], buffer[1], buffer[2], buffer[3]);
170 pwarn("%s (%02x%02x%02x)\n",
171 "FAT starts with odd byte sequence",
172 buffer[0], buffer[1], buffer[2]);
176 if (ask(1, "Correct"))
180 switch (boot->ClustMask) {
191 for (cl = CLUST_FIRST; cl < boot->NumClusters;) {
192 switch (boot->ClustMask) {
194 fat[cl].next = p[0] + (p[1] << 8)
195 + (p[2] << 16) + (p[3] << 24);
196 fat[cl].next &= boot->ClustMask;
197 ret |= checkclnum(boot, no, cl, &fat[cl].next);
202 fat[cl].next = p[0] + (p[1] << 8);
203 ret |= checkclnum(boot, no, cl, &fat[cl].next);
208 fat[cl].next = (p[0] + (p[1] << 8)) & 0x0fff;
209 ret |= checkclnum(boot, no, cl, &fat[cl].next);
211 if (cl >= boot->NumClusters)
213 fat[cl].next = ((p[1] >> 4) + (p[2] << 4)) & 0x0fff;
214 ret |= checkclnum(boot, no, cl, &fat[cl].next);
231 * Get type of reserved cluster
236 if (cl == CLUST_FREE)
246 clustdiffer(cl_t cl, cl_t *cp1, cl_t *cp2, u_int fatnum)
248 if (*cp1 == CLUST_FREE || *cp1 >= CLUST_RSRVD) {
249 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
250 if ((*cp1 != CLUST_FREE && *cp1 < CLUST_BAD
251 && *cp2 != CLUST_FREE && *cp2 < CLUST_BAD)
252 || (*cp1 > CLUST_BAD && *cp2 > CLUST_BAD)) {
253 pwarn("Cluster %u is marked %s with different indicators\n",
254 cl, rsrvdcltype(*cp1));
261 pwarn("Cluster %u is marked %s in FAT 0, %s in FAT %u\n",
262 cl, rsrvdcltype(*cp1), rsrvdcltype(*cp2), fatnum);
263 if (ask(0, "Use FAT 0's entry")) {
267 if (ask(0, "Use FAT %u's entry", fatnum)) {
273 pwarn("Cluster %u is marked %s in FAT 0, but continues with cluster %u in FAT %d\n",
274 cl, rsrvdcltype(*cp1), *cp2, fatnum);
275 if (ask(0, "Use continuation from FAT %u", fatnum)) {
279 if (ask(0, "Use mark from FAT 0")) {
285 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
286 pwarn("Cluster %u continues with cluster %u in FAT 0, but is marked %s in FAT %u\n",
287 cl, *cp1, rsrvdcltype(*cp2), fatnum);
288 if (ask(0, "Use continuation from FAT 0")) {
292 if (ask(0, "Use mark from FAT %d", fatnum)) {
298 pwarn("Cluster %u continues with cluster %u in FAT 0, but with cluster %u in FAT %u\n",
299 cl, *cp1, *cp2, fatnum);
300 if (ask(0, "Use continuation from FAT 0")) {
304 if (ask(0, "Use continuation from FAT %u", fatnum)) {
312 * Compare two FAT copies in memory. Resolve any conflicts and merge them
313 * into the first one.
316 comparefat(struct bootblock *boot, struct fatEntry *first,
317 struct fatEntry *second, u_int fatnum)
322 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++)
323 if (first[cl].next != second[cl].next)
324 ret |= clustdiffer(cl, &first[cl].next, &second[cl].next, fatnum);
329 clearchain(struct bootblock *boot, struct fatEntry *fat, cl_t head)
333 for (p = head; p >= CLUST_FIRST && p < boot->NumClusters; p = q) {
334 if (fat[p].head != head)
337 fat[p].next = fat[p].head = CLUST_FREE;
343 tryclear(struct bootblock *boot, struct fatEntry *fat, cl_t head, cl_t *truncp)
345 if (ask(0, "Clear chain starting at %u", head)) {
346 clearchain(boot, fat, head);
348 } else if (ask(0, "Truncate")) {
356 * Check a complete FAT in-memory for crosslinks
359 checkfat(struct bootblock *boot, struct fatEntry *fat)
367 * pass 1: figure out the cluster chains.
369 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
370 /* find next untravelled chain */
371 if (fat[head].head != 0 /* cluster already belongs to some chain */
372 || fat[head].next == CLUST_FREE
373 || fat[head].next == CLUST_BAD)
374 continue; /* skip it. */
376 /* follow the chain and mark all clusters on the way */
377 for (len = 0, p = head;
378 p >= CLUST_FIRST && p < boot->NumClusters;
384 /* the head record gets the length */
385 fat[head].length = fat[head].next == CLUST_FREE ? 0 : len;
389 * pass 2: check for crosslinked chains (we couldn't do this in pass 1 because
390 * we didn't know the real start of the chain then - would have treated partial
391 * chains as interlinked with their main chain)
393 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
394 /* find next untravelled chain */
395 if (fat[head].head != head)
398 /* follow the chain to its end (hopefully) */
400 (n = fat[p].next) >= CLUST_FIRST && n < boot->NumClusters;
402 if (fat[n].head != head)
407 if (n == CLUST_FREE || n >= CLUST_RSRVD) {
408 pwarn("Cluster chain starting at %u ends with cluster marked %s\n",
409 head, rsrvdcltype(n));
410 ret |= tryclear(boot, fat, head, &fat[p].next);
413 if (n < CLUST_FIRST || n >= boot->NumClusters) {
414 pwarn("Cluster chain starting at %u ends with cluster out of range (%u)\n",
416 ret |= tryclear(boot, fat, head, &fat[p].next);
419 pwarn("Cluster chains starting at %u and %u are linked at cluster %u\n",
420 head, fat[n].head, n);
421 conf = tryclear(boot, fat, head, &fat[p].next);
422 if (ask(0, "Clear chain starting at %u", h = fat[n].head)) {
423 if (conf == FSERROR) {
425 * Transfer the common chain to the one not cleared above.
428 p >= CLUST_FIRST && p < boot->NumClusters;
430 if (h != fat[p].head) {
432 * Have to reexamine this chain.
440 clearchain(boot, fat, h);
450 * Write out FATs encoding them from the internal format
453 writefat(int fs, struct bootblock *boot, struct fatEntry *fat, int correct_fat)
462 fatsz = boot->FATsecs * boot->bpbBytesPerSec;
463 buffer = calloc(boot->FATsecs, boot->bpbBytesPerSec);
464 if (buffer == NULL) {
465 perr("No space for FAT sectors (%zu)",
466 (size_t)boot->FATsecs);
472 *p++ = (u_char)boot->bpbMedia;
475 switch (boot->ClustMask) {
488 /* use same FAT signature as the old FAT has */
492 switch (boot->ClustMask) {
504 if (!_readfat(fs, boot, boot->ValidFat >= 0 ? boot->ValidFat :0,
510 memcpy(p, old_fat, count);
515 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++) {
516 switch (boot->ClustMask) {
518 if (fat[cl].next == CLUST_FREE)
520 *p++ = (u_char)fat[cl].next;
521 *p++ = (u_char)(fat[cl].next >> 8);
522 *p++ = (u_char)(fat[cl].next >> 16);
524 *p++ |= (fat[cl].next >> 24)&0x0f;
527 if (fat[cl].next == CLUST_FREE)
529 *p++ = (u_char)fat[cl].next;
530 *p++ = (u_char)(fat[cl].next >> 8);
533 if (fat[cl].next == CLUST_FREE)
535 if (cl + 1 < boot->NumClusters
536 && fat[cl + 1].next == CLUST_FREE)
538 *p++ = (u_char)fat[cl].next;
539 *p++ = (u_char)((fat[cl].next >> 8) & 0xf)
540 |(u_char)(fat[cl+1].next << 4);
541 *p++ = (u_char)(fat[++cl].next >> 4);
545 for (i = 0; i < boot->bpbFATs; i++) {
546 off = boot->bpbResSectors + i * boot->FATsecs;
547 off *= boot->bpbBytesPerSec;
548 if (lseek(fs, off, SEEK_SET) != off
549 || (size_t)write(fs, buffer, fatsz) != fatsz) {
550 perr("Unable to write FAT");
551 ret = FSFATAL; /* Return immediately? XXX */
559 * Check a complete in-memory FAT for lost cluster chains
562 checklost(int dosfs, struct bootblock *boot, struct fatEntry *fat)
568 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
569 /* find next untravelled chain */
570 if (fat[head].head != head
571 || fat[head].next == CLUST_FREE
572 || (fat[head].next >= CLUST_RSRVD
573 && fat[head].next < CLUST_EOFS)
574 || (fat[head].flags & FAT_USED))
577 pwarn("Lost cluster chain at cluster %u\n%d Cluster(s) lost\n",
578 head, fat[head].length);
579 mod |= ret = reconnect(dosfs, boot, fat, head);
582 if (ret == FSERROR && ask(0, "Clear")) {
583 clearchain(boot, fat, head);
589 if (boot->bpbFSInfo) {
591 if (boot->FSFree != boot->NumFree) {
592 pwarn("Free space in FSInfo block (%d) not correct (%d)\n",
593 boot->FSFree, boot->NumFree);
595 boot->FSFree = boot->NumFree;
600 mod |= writefsinfo(dosfs, boot);