2 * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
3 * Copyright (c) 1995 Martin Husemann
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Martin Husemann
16 * and Wolfgang Solfrank.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: fat.c,v 1.12 2000/10/10 20:24:52 is Exp $");
37 static const char rcsid[] =
38 "$FreeBSD: src/sbin/fsck_msdosfs/fat.c,v 1.1.2.1 2001/08/01 05:47:56 obrien Exp $";
50 static int checkclnum __P((struct bootblock *, int, cl_t, cl_t *));
51 static int clustdiffer __P((cl_t, cl_t *, cl_t *, int));
52 static int tryclear __P((struct bootblock *, struct fatEntry *, cl_t, cl_t *));
53 static int _readfat __P((int, struct bootblock *, int, u_char **));
56 * Check a cluster number for valid value
59 checkclnum(boot, fat, cl, next)
60 struct bootblock *boot;
65 if (*next >= (CLUST_RSRVD&boot->ClustMask))
66 *next |= ~boot->ClustMask;
67 if (*next == CLUST_FREE) {
71 if (*next == CLUST_BAD) {
75 if (*next < CLUST_FIRST
76 || (*next >= boot->NumClusters && *next < CLUST_EOFS)) {
77 pwarn("Cluster %u in FAT %d continues with %s cluster number %u\n",
79 *next < CLUST_RSRVD ? "out of range" : "reserved",
80 *next&boot->ClustMask);
81 if (ask(0, "Truncate")) {
91 * Read a FAT from disk. Returns 1 if successful, 0 otherwise.
94 _readfat(fs, boot, no, buffer)
96 struct bootblock *boot;
102 *buffer = malloc(boot->FATsecs * boot->BytesPerSec);
103 if (*buffer == NULL) {
104 perror("No space for FAT");
108 off = boot->ResSectors + no * boot->FATsecs;
109 off *= boot->BytesPerSec;
111 if (lseek(fs, off, SEEK_SET) != off) {
112 perror("Unable to read FAT");
116 if (read(fs, *buffer, boot->FATsecs * boot->BytesPerSec)
117 != boot->FATsecs * boot->BytesPerSec) {
118 perror("Unable to read FAT");
130 * Read a FAT and decode it into internal format
133 readfat(fs, boot, no, fp)
135 struct bootblock *boot;
137 struct fatEntry **fp;
139 struct fatEntry *fat;
144 boot->NumFree = boot->NumBad = 0;
146 if (!_readfat(fs, boot, no, &buffer))
149 fat = calloc(boot->NumClusters, sizeof(struct fatEntry));
151 perror("No space for FAT");
156 if (buffer[0] != boot->Media
157 || buffer[1] != 0xff || buffer[2] != 0xff
158 || (boot->ClustMask == CLUST16_MASK && buffer[3] != 0xff)
159 || (boot->ClustMask == CLUST32_MASK
160 && ((buffer[3]&0x0f) != 0x0f
161 || buffer[4] != 0xff || buffer[5] != 0xff
162 || buffer[6] != 0xff || (buffer[7]&0x0f) != 0x0f))) {
164 /* Windows 95 OSR2 (and possibly any later) changes
165 * the FAT signature to 0xXXffff7f for FAT16 and to
166 * 0xXXffff0fffffff07 for FAT32 upon boot, to know that the
167 * filesystem is dirty if it doesn't reboot cleanly.
168 * Check this special condition before errorring out.
170 if (buffer[0] == boot->Media && buffer[1] == 0xff
172 && ((boot->ClustMask == CLUST16_MASK && buffer[3] == 0x7f)
173 || (boot->ClustMask == CLUST32_MASK
174 && buffer[3] == 0x0f && buffer[4] == 0xff
175 && buffer[5] == 0xff && buffer[6] == 0xff
176 && buffer[7] == 0x07)))
179 /* just some odd byte sequence in FAT */
181 switch (boot->ClustMask) {
183 pwarn("%s (%02x%02x%02x%02x%02x%02x%02x%02x)\n",
184 "FAT starts with odd byte sequence",
185 buffer[0], buffer[1], buffer[2], buffer[3],
186 buffer[4], buffer[5], buffer[6], buffer[7]);
189 pwarn("%s (%02x%02x%02x%02x)\n",
190 "FAT starts with odd byte sequence",
191 buffer[0], buffer[1], buffer[2], buffer[3]);
194 pwarn("%s (%02x%02x%02x)\n",
195 "FAT starts with odd byte sequence",
196 buffer[0], buffer[1], buffer[2]);
201 if (ask(1, "Correct"))
205 switch (boot->ClustMask) {
216 for (cl = CLUST_FIRST; cl < boot->NumClusters;) {
217 switch (boot->ClustMask) {
219 fat[cl].next = p[0] + (p[1] << 8)
220 + (p[2] << 16) + (p[3] << 24);
221 fat[cl].next &= boot->ClustMask;
222 ret |= checkclnum(boot, no, cl, &fat[cl].next);
227 fat[cl].next = p[0] + (p[1] << 8);
228 ret |= checkclnum(boot, no, cl, &fat[cl].next);
233 fat[cl].next = (p[0] + (p[1] << 8)) & 0x0fff;
234 ret |= checkclnum(boot, no, cl, &fat[cl].next);
236 if (cl >= boot->NumClusters)
238 fat[cl].next = ((p[1] >> 4) + (p[2] << 4)) & 0x0fff;
239 ret |= checkclnum(boot, no, cl, &fat[cl].next);
252 * Get type of reserved cluster
258 if (cl == CLUST_FREE)
268 clustdiffer(cl, cp1, cp2, fatnum)
274 if (*cp1 == CLUST_FREE || *cp1 >= CLUST_RSRVD) {
275 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
276 if ((*cp1 != CLUST_FREE && *cp1 < CLUST_BAD
277 && *cp2 != CLUST_FREE && *cp2 < CLUST_BAD)
278 || (*cp1 > CLUST_BAD && *cp2 > CLUST_BAD)) {
279 pwarn("Cluster %u is marked %s with different indicators, ",
280 cl, rsrvdcltype(*cp1));
287 pwarn("Cluster %u is marked %s in FAT 0, %s in FAT %d\n",
288 cl, rsrvdcltype(*cp1), rsrvdcltype(*cp2), fatnum);
289 if (ask(0, "use FAT 0's entry")) {
293 if (ask(0, "use FAT %d's entry", fatnum)) {
299 pwarn("Cluster %u is marked %s in FAT 0, but continues with cluster %u in FAT %d\n",
300 cl, rsrvdcltype(*cp1), *cp2, fatnum);
301 if (ask(0, "Use continuation from FAT %d", fatnum)) {
305 if (ask(0, "Use mark from FAT 0")) {
311 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
312 pwarn("Cluster %u continues with cluster %u in FAT 0, but is marked %s in FAT %d\n",
313 cl, *cp1, rsrvdcltype(*cp2), fatnum);
314 if (ask(0, "Use continuation from FAT 0")) {
318 if (ask(0, "Use mark from FAT %d", fatnum)) {
324 pwarn("Cluster %u continues with cluster %u in FAT 0, but with cluster %u in FAT %d\n",
325 cl, *cp1, *cp2, fatnum);
326 if (ask(0, "Use continuation from FAT 0")) {
330 if (ask(0, "Use continuation from FAT %d", fatnum)) {
338 * Compare two FAT copies in memory. Resolve any conflicts and merge them
339 * into the first one.
342 comparefat(boot, first, second, fatnum)
343 struct bootblock *boot;
344 struct fatEntry *first;
345 struct fatEntry *second;
351 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++)
352 if (first[cl].next != second[cl].next)
353 ret |= clustdiffer(cl, &first[cl].next, &second[cl].next, fatnum);
358 clearchain(boot, fat, head)
359 struct bootblock *boot;
360 struct fatEntry *fat;
365 for (p = head; p >= CLUST_FIRST && p < boot->NumClusters; p = q) {
366 if (fat[p].head != head)
369 fat[p].next = fat[p].head = CLUST_FREE;
375 tryclear(boot, fat, head, trunc)
376 struct bootblock *boot;
377 struct fatEntry *fat;
381 if (ask(0, "Clear chain starting at %u", head)) {
382 clearchain(boot, fat, head);
384 } else if (ask(0, "Truncate")) {
392 * Check a complete FAT in-memory for crosslinks
396 struct bootblock *boot;
397 struct fatEntry *fat;
405 * pass 1: figure out the cluster chains.
407 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
408 /* find next untravelled chain */
409 if (fat[head].head != 0 /* cluster already belongs to some chain */
410 || fat[head].next == CLUST_FREE
411 || fat[head].next == CLUST_BAD)
412 continue; /* skip it. */
414 /* follow the chain and mark all clusters on the way */
415 for (len = 0, p = head;
416 p >= CLUST_FIRST && p < boot->NumClusters;
422 /* the head record gets the length */
423 fat[head].length = fat[head].next == CLUST_FREE ? 0 : len;
427 * pass 2: check for crosslinked chains (we couldn't do this in pass 1 because
428 * we didn't know the real start of the chain then - would have treated partial
429 * chains as interlinked with their main chain)
431 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
432 /* find next untravelled chain */
433 if (fat[head].head != head)
436 /* follow the chain to its end (hopefully) */
438 (n = fat[p].next) >= CLUST_FIRST && n < boot->NumClusters;
440 if (fat[n].head != head)
445 if (n == CLUST_FREE || n >= CLUST_RSRVD) {
446 pwarn("Cluster chain starting at %u ends with cluster marked %s\n",
447 head, rsrvdcltype(n));
448 ret |= tryclear(boot, fat, head, &fat[p].next);
451 if (n < CLUST_FIRST || n >= boot->NumClusters) {
452 pwarn("Cluster chain starting at %u ends with cluster out of range (%u)\n",
454 ret |= tryclear(boot, fat, head, &fat[p].next);
457 pwarn("Cluster chains starting at %u and %u are linked at cluster %u\n",
458 head, fat[n].head, n);
459 conf = tryclear(boot, fat, head, &fat[p].next);
460 if (ask(0, "Clear chain starting at %u", h = fat[n].head)) {
461 if (conf == FSERROR) {
463 * Transfer the common chain to the one not cleared above.
466 p >= CLUST_FIRST && p < boot->NumClusters;
468 if (h != fat[p].head) {
470 * Have to reexamine this chain.
478 clearchain(boot, fat, h);
488 * Write out FATs encoding them from the internal format
491 writefat(fs, boot, fat, correct_fat)
493 struct bootblock *boot;
494 struct fatEntry *fat;
504 buffer = malloc(fatsz = boot->FATsecs * boot->BytesPerSec);
505 if (buffer == NULL) {
506 perror("No space for FAT");
509 memset(buffer, 0, fatsz);
513 *p++ = (u_char)boot->Media;
516 switch (boot->ClustMask) {
529 /* use same FAT signature as the old FAT has */
533 switch (boot->ClustMask) {
545 if (!_readfat(fs, boot, boot->ValidFat >= 0 ? boot->ValidFat :0,
551 memcpy(p, old_fat, count);
556 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++) {
557 switch (boot->ClustMask) {
559 if (fat[cl].next == CLUST_FREE)
561 *p++ = (u_char)fat[cl].next;
562 *p++ = (u_char)(fat[cl].next >> 8);
563 *p++ = (u_char)(fat[cl].next >> 16);
565 *p++ |= (fat[cl].next >> 24)&0x0f;
568 if (fat[cl].next == CLUST_FREE)
570 *p++ = (u_char)fat[cl].next;
571 *p++ = (u_char)(fat[cl].next >> 8);
574 if (fat[cl].next == CLUST_FREE)
576 if (cl + 1 < boot->NumClusters
577 && fat[cl + 1].next == CLUST_FREE)
579 *p++ = (u_char)fat[cl].next;
580 *p++ = (u_char)((fat[cl].next >> 8) & 0xf)
581 |(u_char)(fat[cl+1].next << 4);
582 *p++ = (u_char)(fat[++cl].next >> 4);
586 for (i = 0; i < boot->FATs; i++) {
587 off = boot->ResSectors + i * boot->FATsecs;
588 off *= boot->BytesPerSec;
589 if (lseek(fs, off, SEEK_SET) != off
590 || write(fs, buffer, fatsz) != fatsz) {
591 perror("Unable to write FAT");
592 ret = FSFATAL; /* Return immediately? XXX */
600 * Check a complete in-memory FAT for lost cluster chains
603 checklost(dosfs, boot, fat)
605 struct bootblock *boot;
606 struct fatEntry *fat;
612 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
613 /* find next untravelled chain */
614 if (fat[head].head != head
615 || fat[head].next == CLUST_FREE
616 || (fat[head].next >= CLUST_RSRVD
617 && fat[head].next < CLUST_EOFS)
618 || (fat[head].flags & FAT_USED))
621 pwarn("Lost cluster chain at cluster %u\n%d Cluster(s) lost\n",
622 head, fat[head].length);
623 mod |= ret = reconnect(dosfs, boot, fat, head);
626 if (ret == FSERROR && ask(0, "Clear")) {
627 clearchain(boot, fat, head);
635 if (boot->FSFree != boot->NumFree) {
636 pwarn("Free space in FSInfo block (%d) not correct (%d)\n",
637 boot->FSFree, boot->NumFree);
639 boot->FSFree = boot->NumFree;
643 if (boot->NumFree && fat[boot->FSNext].next != CLUST_FREE) {
644 pwarn("Next free cluster in FSInfo block (%u) not free\n",
647 for (head = CLUST_FIRST; head < boot->NumClusters; head++)
648 if (fat[head].next == CLUST_FREE) {
655 mod |= writefsinfo(dosfs, boot);