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.
32 * $NetBSD: fat.c,v 1.12 2000/10/10 20:24:52 is Exp $
33 * $FreeBSD: src/sbin/fsck_msdosfs/fat.c,v 1.1.2.1 2001/08/01 05:47:56 obrien Exp $
34 * $DragonFly: src/sbin/fsck_msdosfs/fat.c,v 1.2 2003/06/17 04:27:32 dillon Exp $
38 #include <sys/cdefs.h>
49 static int checkclnum __P((struct bootblock *, int, cl_t, cl_t *));
50 static int clustdiffer __P((cl_t, cl_t *, cl_t *, int));
51 static int tryclear __P((struct bootblock *, struct fatEntry *, cl_t, cl_t *));
52 static int _readfat __P((int, struct bootblock *, int, u_char **));
55 * Check a cluster number for valid value
58 checkclnum(boot, fat, cl, next)
59 struct bootblock *boot;
64 if (*next >= (CLUST_RSRVD&boot->ClustMask))
65 *next |= ~boot->ClustMask;
66 if (*next == CLUST_FREE) {
70 if (*next == CLUST_BAD) {
74 if (*next < CLUST_FIRST
75 || (*next >= boot->NumClusters && *next < CLUST_EOFS)) {
76 pwarn("Cluster %u in FAT %d continues with %s cluster number %u\n",
78 *next < CLUST_RSRVD ? "out of range" : "reserved",
79 *next&boot->ClustMask);
80 if (ask(0, "Truncate")) {
90 * Read a FAT from disk. Returns 1 if successful, 0 otherwise.
93 _readfat(fs, boot, no, buffer)
95 struct bootblock *boot;
101 *buffer = malloc(boot->FATsecs * boot->BytesPerSec);
102 if (*buffer == NULL) {
103 perror("No space for FAT");
107 off = boot->ResSectors + no * boot->FATsecs;
108 off *= boot->BytesPerSec;
110 if (lseek(fs, off, SEEK_SET) != off) {
111 perror("Unable to read FAT");
115 if (read(fs, *buffer, boot->FATsecs * boot->BytesPerSec)
116 != boot->FATsecs * boot->BytesPerSec) {
117 perror("Unable to read FAT");
129 * Read a FAT and decode it into internal format
132 readfat(fs, boot, no, fp)
134 struct bootblock *boot;
136 struct fatEntry **fp;
138 struct fatEntry *fat;
143 boot->NumFree = boot->NumBad = 0;
145 if (!_readfat(fs, boot, no, &buffer))
148 fat = calloc(boot->NumClusters, sizeof(struct fatEntry));
150 perror("No space for FAT");
155 if (buffer[0] != boot->Media
156 || buffer[1] != 0xff || buffer[2] != 0xff
157 || (boot->ClustMask == CLUST16_MASK && buffer[3] != 0xff)
158 || (boot->ClustMask == CLUST32_MASK
159 && ((buffer[3]&0x0f) != 0x0f
160 || buffer[4] != 0xff || buffer[5] != 0xff
161 || buffer[6] != 0xff || (buffer[7]&0x0f) != 0x0f))) {
163 /* Windows 95 OSR2 (and possibly any later) changes
164 * the FAT signature to 0xXXffff7f for FAT16 and to
165 * 0xXXffff0fffffff07 for FAT32 upon boot, to know that the
166 * filesystem is dirty if it doesn't reboot cleanly.
167 * Check this special condition before errorring out.
169 if (buffer[0] == boot->Media && buffer[1] == 0xff
171 && ((boot->ClustMask == CLUST16_MASK && buffer[3] == 0x7f)
172 || (boot->ClustMask == CLUST32_MASK
173 && buffer[3] == 0x0f && buffer[4] == 0xff
174 && buffer[5] == 0xff && buffer[6] == 0xff
175 && buffer[7] == 0x07)))
178 /* just some odd byte sequence in FAT */
180 switch (boot->ClustMask) {
182 pwarn("%s (%02x%02x%02x%02x%02x%02x%02x%02x)\n",
183 "FAT starts with odd byte sequence",
184 buffer[0], buffer[1], buffer[2], buffer[3],
185 buffer[4], buffer[5], buffer[6], buffer[7]);
188 pwarn("%s (%02x%02x%02x%02x)\n",
189 "FAT starts with odd byte sequence",
190 buffer[0], buffer[1], buffer[2], buffer[3]);
193 pwarn("%s (%02x%02x%02x)\n",
194 "FAT starts with odd byte sequence",
195 buffer[0], buffer[1], buffer[2]);
200 if (ask(1, "Correct"))
204 switch (boot->ClustMask) {
215 for (cl = CLUST_FIRST; cl < boot->NumClusters;) {
216 switch (boot->ClustMask) {
218 fat[cl].next = p[0] + (p[1] << 8)
219 + (p[2] << 16) + (p[3] << 24);
220 fat[cl].next &= boot->ClustMask;
221 ret |= checkclnum(boot, no, cl, &fat[cl].next);
226 fat[cl].next = p[0] + (p[1] << 8);
227 ret |= checkclnum(boot, no, cl, &fat[cl].next);
232 fat[cl].next = (p[0] + (p[1] << 8)) & 0x0fff;
233 ret |= checkclnum(boot, no, cl, &fat[cl].next);
235 if (cl >= boot->NumClusters)
237 fat[cl].next = ((p[1] >> 4) + (p[2] << 4)) & 0x0fff;
238 ret |= checkclnum(boot, no, cl, &fat[cl].next);
251 * Get type of reserved cluster
257 if (cl == CLUST_FREE)
267 clustdiffer(cl, cp1, cp2, fatnum)
273 if (*cp1 == CLUST_FREE || *cp1 >= CLUST_RSRVD) {
274 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
275 if ((*cp1 != CLUST_FREE && *cp1 < CLUST_BAD
276 && *cp2 != CLUST_FREE && *cp2 < CLUST_BAD)
277 || (*cp1 > CLUST_BAD && *cp2 > CLUST_BAD)) {
278 pwarn("Cluster %u is marked %s with different indicators, ",
279 cl, rsrvdcltype(*cp1));
286 pwarn("Cluster %u is marked %s in FAT 0, %s in FAT %d\n",
287 cl, rsrvdcltype(*cp1), rsrvdcltype(*cp2), fatnum);
288 if (ask(0, "use FAT 0's entry")) {
292 if (ask(0, "use FAT %d's entry", fatnum)) {
298 pwarn("Cluster %u is marked %s in FAT 0, but continues with cluster %u in FAT %d\n",
299 cl, rsrvdcltype(*cp1), *cp2, fatnum);
300 if (ask(0, "Use continuation from FAT %d", fatnum)) {
304 if (ask(0, "Use mark from FAT 0")) {
310 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
311 pwarn("Cluster %u continues with cluster %u in FAT 0, but is marked %s in FAT %d\n",
312 cl, *cp1, rsrvdcltype(*cp2), fatnum);
313 if (ask(0, "Use continuation from FAT 0")) {
317 if (ask(0, "Use mark from FAT %d", fatnum)) {
323 pwarn("Cluster %u continues with cluster %u in FAT 0, but with cluster %u in FAT %d\n",
324 cl, *cp1, *cp2, fatnum);
325 if (ask(0, "Use continuation from FAT 0")) {
329 if (ask(0, "Use continuation from FAT %d", fatnum)) {
337 * Compare two FAT copies in memory. Resolve any conflicts and merge them
338 * into the first one.
341 comparefat(boot, first, second, fatnum)
342 struct bootblock *boot;
343 struct fatEntry *first;
344 struct fatEntry *second;
350 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++)
351 if (first[cl].next != second[cl].next)
352 ret |= clustdiffer(cl, &first[cl].next, &second[cl].next, fatnum);
357 clearchain(boot, fat, head)
358 struct bootblock *boot;
359 struct fatEntry *fat;
364 for (p = head; p >= CLUST_FIRST && p < boot->NumClusters; p = q) {
365 if (fat[p].head != head)
368 fat[p].next = fat[p].head = CLUST_FREE;
374 tryclear(boot, fat, head, trunc)
375 struct bootblock *boot;
376 struct fatEntry *fat;
380 if (ask(0, "Clear chain starting at %u", head)) {
381 clearchain(boot, fat, head);
383 } else if (ask(0, "Truncate")) {
391 * Check a complete FAT in-memory for crosslinks
395 struct bootblock *boot;
396 struct fatEntry *fat;
404 * pass 1: figure out the cluster chains.
406 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
407 /* find next untravelled chain */
408 if (fat[head].head != 0 /* cluster already belongs to some chain */
409 || fat[head].next == CLUST_FREE
410 || fat[head].next == CLUST_BAD)
411 continue; /* skip it. */
413 /* follow the chain and mark all clusters on the way */
414 for (len = 0, p = head;
415 p >= CLUST_FIRST && p < boot->NumClusters;
421 /* the head record gets the length */
422 fat[head].length = fat[head].next == CLUST_FREE ? 0 : len;
426 * pass 2: check for crosslinked chains (we couldn't do this in pass 1 because
427 * we didn't know the real start of the chain then - would have treated partial
428 * chains as interlinked with their main chain)
430 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
431 /* find next untravelled chain */
432 if (fat[head].head != head)
435 /* follow the chain to its end (hopefully) */
437 (n = fat[p].next) >= CLUST_FIRST && n < boot->NumClusters;
439 if (fat[n].head != head)
444 if (n == CLUST_FREE || n >= CLUST_RSRVD) {
445 pwarn("Cluster chain starting at %u ends with cluster marked %s\n",
446 head, rsrvdcltype(n));
447 ret |= tryclear(boot, fat, head, &fat[p].next);
450 if (n < CLUST_FIRST || n >= boot->NumClusters) {
451 pwarn("Cluster chain starting at %u ends with cluster out of range (%u)\n",
453 ret |= tryclear(boot, fat, head, &fat[p].next);
456 pwarn("Cluster chains starting at %u and %u are linked at cluster %u\n",
457 head, fat[n].head, n);
458 conf = tryclear(boot, fat, head, &fat[p].next);
459 if (ask(0, "Clear chain starting at %u", h = fat[n].head)) {
460 if (conf == FSERROR) {
462 * Transfer the common chain to the one not cleared above.
465 p >= CLUST_FIRST && p < boot->NumClusters;
467 if (h != fat[p].head) {
469 * Have to reexamine this chain.
477 clearchain(boot, fat, h);
487 * Write out FATs encoding them from the internal format
490 writefat(fs, boot, fat, correct_fat)
492 struct bootblock *boot;
493 struct fatEntry *fat;
503 buffer = malloc(fatsz = boot->FATsecs * boot->BytesPerSec);
504 if (buffer == NULL) {
505 perror("No space for FAT");
508 memset(buffer, 0, fatsz);
512 *p++ = (u_char)boot->Media;
515 switch (boot->ClustMask) {
528 /* use same FAT signature as the old FAT has */
532 switch (boot->ClustMask) {
544 if (!_readfat(fs, boot, boot->ValidFat >= 0 ? boot->ValidFat :0,
550 memcpy(p, old_fat, count);
555 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++) {
556 switch (boot->ClustMask) {
558 if (fat[cl].next == CLUST_FREE)
560 *p++ = (u_char)fat[cl].next;
561 *p++ = (u_char)(fat[cl].next >> 8);
562 *p++ = (u_char)(fat[cl].next >> 16);
564 *p++ |= (fat[cl].next >> 24)&0x0f;
567 if (fat[cl].next == CLUST_FREE)
569 *p++ = (u_char)fat[cl].next;
570 *p++ = (u_char)(fat[cl].next >> 8);
573 if (fat[cl].next == CLUST_FREE)
575 if (cl + 1 < boot->NumClusters
576 && fat[cl + 1].next == CLUST_FREE)
578 *p++ = (u_char)fat[cl].next;
579 *p++ = (u_char)((fat[cl].next >> 8) & 0xf)
580 |(u_char)(fat[cl+1].next << 4);
581 *p++ = (u_char)(fat[++cl].next >> 4);
585 for (i = 0; i < boot->FATs; i++) {
586 off = boot->ResSectors + i * boot->FATsecs;
587 off *= boot->BytesPerSec;
588 if (lseek(fs, off, SEEK_SET) != off
589 || write(fs, buffer, fatsz) != fatsz) {
590 perror("Unable to write FAT");
591 ret = FSFATAL; /* Return immediately? XXX */
599 * Check a complete in-memory FAT for lost cluster chains
602 checklost(dosfs, boot, fat)
604 struct bootblock *boot;
605 struct fatEntry *fat;
611 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
612 /* find next untravelled chain */
613 if (fat[head].head != head
614 || fat[head].next == CLUST_FREE
615 || (fat[head].next >= CLUST_RSRVD
616 && fat[head].next < CLUST_EOFS)
617 || (fat[head].flags & FAT_USED))
620 pwarn("Lost cluster chain at cluster %u\n%d Cluster(s) lost\n",
621 head, fat[head].length);
622 mod |= ret = reconnect(dosfs, boot, fat, head);
625 if (ret == FSERROR && ask(0, "Clear")) {
626 clearchain(boot, fat, head);
634 if (boot->FSFree != boot->NumFree) {
635 pwarn("Free space in FSInfo block (%d) not correct (%d)\n",
636 boot->FSFree, boot->NumFree);
638 boot->FSFree = boot->NumFree;
642 if (boot->NumFree && fat[boot->FSNext].next != CLUST_FREE) {
643 pwarn("Next free cluster in FSInfo block (%u) not free\n",
646 for (head = CLUST_FIRST; head < boot->NumClusters; head++)
647 if (fat[head].next == CLUST_FREE) {
654 mod |= writefsinfo(dosfs, boot);