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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: fat.c,v 1.18 2006/06/05 16:51:18 christos Exp $");
30 static const char rcsid[] =
43 static int checkclnum(struct bootblock *, u_int, cl_t, cl_t *);
44 static int clustdiffer(cl_t, cl_t *, cl_t *, u_int);
45 static int tryclear(struct bootblock *, struct fatEntry *, cl_t, cl_t *);
46 static int _readfat(int, struct bootblock *, u_int, u_char **);
49 * The first 2 FAT entries contain pseudo-cluster numbers with the following
52 * 31...... ........ ........ .......0
53 * rrrr1111 11111111 11111111 mmmmmmmm FAT32 entry 0
54 * rrrrsh11 11111111 11111111 11111xxx FAT32 entry 1
56 * 11111111 mmmmmmmm FAT16 entry 0
57 * sh111111 11111xxx FAT16 entry 1
60 * m = BPB media ID byte
61 * s = clean flag (1 = dismounted; 0 = still mounted)
62 * h = hard error flag (1 = ok; 0 = I/O error)
67 checkdirty(int fs, struct bootblock *boot)
74 if (boot->ClustMask != CLUST16_MASK && boot->ClustMask != CLUST32_MASK)
77 off = boot->bpbResSectors;
78 off *= boot->bpbBytesPerSec;
80 buffer = malloc(len = boot->bpbBytesPerSec);
82 perr("No space for FAT sectors (%zu)", len);
86 if (lseek(fs, off, SEEK_SET) != off) {
87 perr("Unable to read FAT");
91 if ((size_t)read(fs, buffer, boot->bpbBytesPerSec) !=
92 boot->bpbBytesPerSec) {
93 perr("Unable to read FAT");
98 * If we don't understand the FAT, then the file system must be
99 * assumed to be unclean.
101 if (buffer[0] != boot->bpbMedia || buffer[1] != 0xff)
103 if (boot->ClustMask == CLUST16_MASK) {
104 if ((buffer[2] & 0xf8) != 0xf8 || (buffer[3] & 0x3f) != 0x3f)
107 if (buffer[2] != 0xff || (buffer[3] & 0x0f) != 0x0f
108 || (buffer[4] & 0xf8) != 0xf8 || buffer[5] != 0xff
109 || buffer[6] != 0xff || (buffer[7] & 0x03) != 0x03)
114 * Now check the actual clean flag (and the no-error flag).
116 if (boot->ClustMask == CLUST16_MASK) {
117 if ((buffer[3] & 0xc0) == 0xc0)
120 if ((buffer[7] & 0x0c) == 0x0c)
130 * Check a cluster number for valid value
133 checkclnum(struct bootblock *boot, u_int fat, cl_t cl, cl_t *next)
135 if (*next >= (CLUST_RSRVD&boot->ClustMask))
136 *next |= ~boot->ClustMask;
137 if (*next == CLUST_FREE) {
141 if (*next == CLUST_BAD) {
145 if (*next < CLUST_FIRST
146 || (*next >= boot->NumClusters && *next < CLUST_EOFS)) {
147 pwarn("Cluster %u in FAT %d continues with %s cluster number %u\n",
149 *next < CLUST_RSRVD ? "out of range" : "reserved",
150 *next&boot->ClustMask);
151 if (ask(0, "Truncate")) {
161 * Read a FAT from disk. Returns 1 if successful, 0 otherwise.
164 _readfat(int fs, struct bootblock *boot, u_int no, u_char **buffer)
169 *buffer = malloc(len = boot->FATsecs * boot->bpbBytesPerSec);
170 if (*buffer == NULL) {
171 perr("No space for FAT sectors (%zu)", len);
175 off = boot->bpbResSectors + no * boot->FATsecs;
176 off *= boot->bpbBytesPerSec;
178 if (lseek(fs, off, SEEK_SET) != off) {
179 perr("Unable to read FAT");
183 if ((size_t)read(fs, *buffer, boot->FATsecs * boot->bpbBytesPerSec)
184 != boot->FATsecs * boot->bpbBytesPerSec) {
185 perr("Unable to read FAT");
197 * Read a FAT and decode it into internal format
200 readfat(int fs, struct bootblock *boot, u_int no, struct fatEntry **fp)
202 struct fatEntry *fat;
208 boot->NumFree = boot->NumBad = 0;
210 if (!_readfat(fs, boot, no, &buffer))
213 fat = malloc(len = boot->NumClusters * sizeof(struct fatEntry));
215 perr("No space for FAT clusters (%zu)", len);
219 (void)memset(fat, 0, len);
221 if (buffer[0] != boot->bpbMedia
222 || buffer[1] != 0xff || buffer[2] != 0xff
223 || (boot->ClustMask == CLUST16_MASK && buffer[3] != 0xff)
224 || (boot->ClustMask == CLUST32_MASK
225 && ((buffer[3]&0x0f) != 0x0f
226 || buffer[4] != 0xff || buffer[5] != 0xff
227 || buffer[6] != 0xff || (buffer[7]&0x0f) != 0x0f))) {
229 /* Windows 95 OSR2 (and possibly any later) changes
230 * the FAT signature to 0xXXffff7f for FAT16 and to
231 * 0xXXffff0fffffff07 for FAT32 upon boot, to know that the
232 * file system is dirty if it doesn't reboot cleanly.
233 * Check this special condition before errorring out.
235 if (buffer[0] == boot->bpbMedia && buffer[1] == 0xff
237 && ((boot->ClustMask == CLUST16_MASK && buffer[3] == 0x7f)
238 || (boot->ClustMask == CLUST32_MASK
239 && buffer[3] == 0x0f && buffer[4] == 0xff
240 && buffer[5] == 0xff && buffer[6] == 0xff
241 && buffer[7] == 0x07)))
244 /* just some odd byte sequence in FAT */
246 switch (boot->ClustMask) {
248 pwarn("%s (%02x%02x%02x%02x%02x%02x%02x%02x)\n",
249 "FAT starts with odd byte sequence",
250 buffer[0], buffer[1], buffer[2], buffer[3],
251 buffer[4], buffer[5], buffer[6], buffer[7]);
254 pwarn("%s (%02x%02x%02x%02x)\n",
255 "FAT starts with odd byte sequence",
256 buffer[0], buffer[1], buffer[2], buffer[3]);
259 pwarn("%s (%02x%02x%02x)\n",
260 "FAT starts with odd byte sequence",
261 buffer[0], buffer[1], buffer[2]);
266 if (ask(1, "Correct"))
270 switch (boot->ClustMask) {
281 for (cl = CLUST_FIRST; cl < boot->NumClusters;) {
282 switch (boot->ClustMask) {
284 fat[cl].next = p[0] + (p[1] << 8)
285 + (p[2] << 16) + (p[3] << 24);
286 fat[cl].next &= boot->ClustMask;
287 ret |= checkclnum(boot, no, cl, &fat[cl].next);
292 fat[cl].next = p[0] + (p[1] << 8);
293 ret |= checkclnum(boot, no, cl, &fat[cl].next);
298 fat[cl].next = (p[0] + (p[1] << 8)) & 0x0fff;
299 ret |= checkclnum(boot, no, cl, &fat[cl].next);
301 if (cl >= boot->NumClusters)
303 fat[cl].next = ((p[1] >> 4) + (p[2] << 4)) & 0x0fff;
304 ret |= checkclnum(boot, no, cl, &fat[cl].next);
321 * Get type of reserved cluster
326 if (cl == CLUST_FREE)
336 clustdiffer(cl_t cl, cl_t *cp1, cl_t *cp2, u_int fatnum)
338 if (*cp1 == CLUST_FREE || *cp1 >= CLUST_RSRVD) {
339 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
340 if ((*cp1 != CLUST_FREE && *cp1 < CLUST_BAD
341 && *cp2 != CLUST_FREE && *cp2 < CLUST_BAD)
342 || (*cp1 > CLUST_BAD && *cp2 > CLUST_BAD)) {
343 pwarn("Cluster %u is marked %s with different indicators\n",
344 cl, rsrvdcltype(*cp1));
351 pwarn("Cluster %u is marked %s in FAT 0, %s in FAT %u\n",
352 cl, rsrvdcltype(*cp1), rsrvdcltype(*cp2), fatnum);
353 if (ask(0, "Use FAT 0's entry")) {
357 if (ask(0, "Use FAT %u's entry", fatnum)) {
363 pwarn("Cluster %u is marked %s in FAT 0, but continues with cluster %u in FAT %d\n",
364 cl, rsrvdcltype(*cp1), *cp2, fatnum);
365 if (ask(0, "Use continuation from FAT %u", fatnum)) {
369 if (ask(0, "Use mark from FAT 0")) {
375 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
376 pwarn("Cluster %u continues with cluster %u in FAT 0, but is marked %s in FAT %u\n",
377 cl, *cp1, rsrvdcltype(*cp2), fatnum);
378 if (ask(0, "Use continuation from FAT 0")) {
382 if (ask(0, "Use mark from FAT %d", fatnum)) {
388 pwarn("Cluster %u continues with cluster %u in FAT 0, but with cluster %u in FAT %u\n",
389 cl, *cp1, *cp2, fatnum);
390 if (ask(0, "Use continuation from FAT 0")) {
394 if (ask(0, "Use continuation from FAT %u", fatnum)) {
402 * Compare two FAT copies in memory. Resolve any conflicts and merge them
403 * into the first one.
406 comparefat(struct bootblock *boot, struct fatEntry *first,
407 struct fatEntry *second, u_int fatnum)
412 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++)
413 if (first[cl].next != second[cl].next)
414 ret |= clustdiffer(cl, &first[cl].next, &second[cl].next, fatnum);
419 clearchain(struct bootblock *boot, struct fatEntry *fat, cl_t head)
423 for (p = head; p >= CLUST_FIRST && p < boot->NumClusters; p = q) {
424 if (fat[p].head != head)
427 fat[p].next = fat[p].head = CLUST_FREE;
433 tryclear(struct bootblock *boot, struct fatEntry *fat, cl_t head, cl_t *truncp)
435 if (ask(0, "Clear chain starting at %u", head)) {
436 clearchain(boot, fat, head);
438 } else if (ask(0, "Truncate")) {
442 for (p = head, len = 0;
443 p >= CLUST_FIRST && p < boot->NumClusters;
444 p = fat[p].next, len++)
447 fat[head].length = len;
454 * Check a complete FAT in-memory for crosslinks
457 checkfat(struct bootblock *boot, struct fatEntry *fat)
465 * pass 1: figure out the cluster chains.
467 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
468 /* find next untravelled chain */
469 if (fat[head].head != 0 /* cluster already belongs to some chain */
470 || fat[head].next == CLUST_FREE
471 || fat[head].next == CLUST_BAD)
472 continue; /* skip it. */
474 /* follow the chain and mark all clusters on the way */
475 for (len = 0, p = head;
476 p >= CLUST_FIRST && p < boot->NumClusters &&
483 /* the head record gets the length */
484 fat[head].length = fat[head].next == CLUST_FREE ? 0 : len;
488 * pass 2: check for crosslinked chains (we couldn't do this in pass 1 because
489 * we didn't know the real start of the chain then - would have treated partial
490 * chains as interlinked with their main chain)
492 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
493 /* find next untravelled chain */
494 if (fat[head].head != head)
497 /* follow the chain to its end (hopefully) */
498 for (len = fat[head].length, p = head;
499 (n = fat[p].next) >= CLUST_FIRST && n < boot->NumClusters;
501 if (fat[n].head != head || len-- < 2)
506 if (n == CLUST_FREE || n >= CLUST_RSRVD) {
507 pwarn("Cluster chain starting at %u ends with cluster marked %s\n",
508 head, rsrvdcltype(n));
510 ret |= tryclear(boot, fat, head, &fat[p].next);
513 if (n < CLUST_FIRST || n >= boot->NumClusters) {
514 pwarn("Cluster chain starting at %u ends with cluster out of range (%u)\n",
518 if (head == fat[n].head) {
519 pwarn("Cluster chain starting at %u loops at cluster %u\n",
524 pwarn("Cluster chains starting at %u and %u are linked at cluster %u\n",
525 head, fat[n].head, n);
526 conf = tryclear(boot, fat, head, &fat[p].next);
527 if (ask(0, "Clear chain starting at %u", h = fat[n].head)) {
528 if (conf == FSERROR) {
530 * Transfer the common chain to the one not cleared above.
533 p >= CLUST_FIRST && p < boot->NumClusters;
535 if (h != fat[p].head) {
537 * Have to reexamine this chain.
545 clearchain(boot, fat, h);
555 * Write out FATs encoding them from the internal format
558 writefat(int fs, struct bootblock *boot, struct fatEntry *fat, int correct_fat)
567 buffer = malloc(fatsz = boot->FATsecs * boot->bpbBytesPerSec);
568 if (buffer == NULL) {
569 perr("No space for FAT sectors (%zu)", fatsz);
572 memset(buffer, 0, fatsz);
576 *p++ = (u_char)boot->bpbMedia;
579 switch (boot->ClustMask) {
592 /* use same FAT signature as the old FAT has */
596 switch (boot->ClustMask) {
608 if (!_readfat(fs, boot, boot->ValidFat >= 0 ? boot->ValidFat :0,
614 memcpy(p, old_fat, count);
619 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++) {
620 switch (boot->ClustMask) {
622 if (fat[cl].next == CLUST_FREE)
624 *p++ = (u_char)fat[cl].next;
625 *p++ = (u_char)(fat[cl].next >> 8);
626 *p++ = (u_char)(fat[cl].next >> 16);
628 *p++ |= (fat[cl].next >> 24)&0x0f;
631 if (fat[cl].next == CLUST_FREE)
633 *p++ = (u_char)fat[cl].next;
634 *p++ = (u_char)(fat[cl].next >> 8);
637 if (fat[cl].next == CLUST_FREE)
639 *p++ = (u_char)fat[cl].next;
640 *p = (u_char)((fat[cl].next >> 8) & 0xf);
642 if (cl >= boot->NumClusters)
644 if (fat[cl].next == CLUST_FREE)
646 *p++ |= (u_char)(fat[cl + 1].next << 4);
647 *p++ = (u_char)(fat[cl + 1].next >> 4);
651 for (i = 0; i < boot->bpbFATs; i++) {
652 off = boot->bpbResSectors + i * boot->FATsecs;
653 off *= boot->bpbBytesPerSec;
654 if (lseek(fs, off, SEEK_SET) != off
655 || (size_t)write(fs, buffer, fatsz) != fatsz) {
656 perr("Unable to write FAT");
657 ret = FSFATAL; /* Return immediately? XXX */
665 * Check a complete in-memory FAT for lost cluster chains
668 checklost(int dosfs, struct bootblock *boot, struct fatEntry *fat)
674 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
675 /* find next untravelled chain */
676 if (fat[head].head != head
677 || fat[head].next == CLUST_FREE
678 || (fat[head].next >= CLUST_RSRVD
679 && fat[head].next < CLUST_EOFS)
680 || (fat[head].flags & FAT_USED))
683 pwarn("Lost cluster chain at cluster %u\n%d Cluster(s) lost\n",
684 head, fat[head].length);
685 mod |= ret = reconnect(dosfs, boot, fat, head);
688 if (ret == FSERROR && ask(0, "Clear")) {
689 clearchain(boot, fat, head);
695 if (boot->bpbFSInfo) {
697 if (boot->FSFree != 0xffffffffU &&
698 boot->FSFree != boot->NumFree) {
699 pwarn("Free space in FSInfo block (%u) not correct (%u)\n",
700 boot->FSFree, boot->NumFree);
702 boot->FSFree = boot->NumFree;
707 mod |= writefsinfo(dosfs, boot);