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.12 2000/10/10 20:24:52 is 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)
73 if (boot->ClustMask != CLUST16_MASK && boot->ClustMask != CLUST32_MASK)
76 off = boot->bpbResSectors;
77 off *= boot->bpbBytesPerSec;
79 buffer = malloc(boot->bpbBytesPerSec);
81 perror("No space for FAT");
85 if (lseek(fs, off, SEEK_SET) != off) {
86 perror("Unable to read FAT");
90 if (read(fs, buffer, boot->bpbBytesPerSec) != boot->bpbBytesPerSec) {
91 perror("Unable to read FAT");
96 * If we don't understand the FAT, then the file system must be
97 * assumed to be unclean.
99 if (buffer[0] != boot->bpbMedia || buffer[1] != 0xff)
101 if (boot->ClustMask == CLUST16_MASK) {
102 if ((buffer[2] & 0xf8) != 0xf8 || (buffer[3] & 0x3f) != 0x3f)
105 if (buffer[2] != 0xff || (buffer[3] & 0x0f) != 0x0f
106 || (buffer[4] & 0xf8) != 0xf8 || buffer[5] != 0xff
107 || buffer[6] != 0xff || (buffer[7] & 0x03) != 0x03)
112 * Now check the actual clean flag (and the no-error flag).
114 if (boot->ClustMask == CLUST16_MASK) {
115 if ((buffer[3] & 0xc0) == 0xc0)
118 if ((buffer[7] & 0x0c) == 0x0c)
128 * Check a cluster number for valid value
131 checkclnum(struct bootblock *boot, u_int fat, cl_t cl, cl_t *next)
133 if (*next >= (CLUST_RSRVD&boot->ClustMask))
134 *next |= ~boot->ClustMask;
135 if (*next == CLUST_FREE) {
139 if (*next == CLUST_BAD) {
143 if (*next < CLUST_FIRST
144 || (*next >= boot->NumClusters && *next < CLUST_EOFS)) {
145 pwarn("Cluster %u in FAT %d continues with %s cluster number %u\n",
147 *next < CLUST_RSRVD ? "out of range" : "reserved",
148 *next&boot->ClustMask);
149 if (ask(0, "Truncate")) {
159 * Read a FAT from disk. Returns 1 if successful, 0 otherwise.
162 _readfat(int fs, struct bootblock *boot, u_int no, u_char **buffer)
166 *buffer = malloc(boot->FATsecs * boot->bpbBytesPerSec);
167 if (*buffer == NULL) {
168 perror("No space for FAT");
172 off = boot->bpbResSectors + no * boot->FATsecs;
173 off *= boot->bpbBytesPerSec;
175 if (lseek(fs, off, SEEK_SET) != off) {
176 perror("Unable to read FAT");
180 if ((size_t)read(fs, *buffer, boot->FATsecs * boot->bpbBytesPerSec)
181 != boot->FATsecs * boot->bpbBytesPerSec) {
182 perror("Unable to read FAT");
194 * Read a FAT and decode it into internal format
197 readfat(int fs, struct bootblock *boot, u_int no, struct fatEntry **fp)
199 struct fatEntry *fat;
205 boot->NumFree = boot->NumBad = 0;
207 if (!_readfat(fs, boot, no, &buffer))
210 fat = malloc(len = boot->NumClusters * sizeof(struct fatEntry));
212 perror("No space for FAT");
216 (void)memset(fat, 0, len);
218 if (buffer[0] != boot->bpbMedia
219 || buffer[1] != 0xff || buffer[2] != 0xff
220 || (boot->ClustMask == CLUST16_MASK && buffer[3] != 0xff)
221 || (boot->ClustMask == CLUST32_MASK
222 && ((buffer[3]&0x0f) != 0x0f
223 || buffer[4] != 0xff || buffer[5] != 0xff
224 || buffer[6] != 0xff || (buffer[7]&0x0f) != 0x0f))) {
226 /* Windows 95 OSR2 (and possibly any later) changes
227 * the FAT signature to 0xXXffff7f for FAT16 and to
228 * 0xXXffff0fffffff07 for FAT32 upon boot, to know that the
229 * file system is dirty if it doesn't reboot cleanly.
230 * Check this special condition before errorring out.
232 if (buffer[0] == boot->bpbMedia && buffer[1] == 0xff
234 && ((boot->ClustMask == CLUST16_MASK && buffer[3] == 0x7f)
235 || (boot->ClustMask == CLUST32_MASK
236 && buffer[3] == 0x0f && buffer[4] == 0xff
237 && buffer[5] == 0xff && buffer[6] == 0xff
238 && buffer[7] == 0x07)))
241 /* just some odd byte sequence in FAT */
243 switch (boot->ClustMask) {
245 pwarn("%s (%02x%02x%02x%02x%02x%02x%02x%02x)\n",
246 "FAT starts with odd byte sequence",
247 buffer[0], buffer[1], buffer[2], buffer[3],
248 buffer[4], buffer[5], buffer[6], buffer[7]);
251 pwarn("%s (%02x%02x%02x%02x)\n",
252 "FAT starts with odd byte sequence",
253 buffer[0], buffer[1], buffer[2], buffer[3]);
256 pwarn("%s (%02x%02x%02x)\n",
257 "FAT starts with odd byte sequence",
258 buffer[0], buffer[1], buffer[2]);
263 if (ask(1, "Correct"))
267 switch (boot->ClustMask) {
278 for (cl = CLUST_FIRST; cl < boot->NumClusters;) {
279 switch (boot->ClustMask) {
281 fat[cl].next = p[0] + (p[1] << 8)
282 + (p[2] << 16) + (p[3] << 24);
283 fat[cl].next &= boot->ClustMask;
284 ret |= checkclnum(boot, no, cl, &fat[cl].next);
289 fat[cl].next = p[0] + (p[1] << 8);
290 ret |= checkclnum(boot, no, cl, &fat[cl].next);
295 fat[cl].next = (p[0] + (p[1] << 8)) & 0x0fff;
296 ret |= checkclnum(boot, no, cl, &fat[cl].next);
298 if (cl >= boot->NumClusters)
300 fat[cl].next = ((p[1] >> 4) + (p[2] << 4)) & 0x0fff;
301 ret |= checkclnum(boot, no, cl, &fat[cl].next);
318 * Get type of reserved cluster
323 if (cl == CLUST_FREE)
333 clustdiffer(cl_t cl, cl_t *cp1, cl_t *cp2, u_int fatnum)
335 if (*cp1 == CLUST_FREE || *cp1 >= CLUST_RSRVD) {
336 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
337 if ((*cp1 != CLUST_FREE && *cp1 < CLUST_BAD
338 && *cp2 != CLUST_FREE && *cp2 < CLUST_BAD)
339 || (*cp1 > CLUST_BAD && *cp2 > CLUST_BAD)) {
340 pwarn("Cluster %u is marked %s with different indicators\n",
341 cl, rsrvdcltype(*cp1));
348 pwarn("Cluster %u is marked %s in FAT 0, %s in FAT %u\n",
349 cl, rsrvdcltype(*cp1), rsrvdcltype(*cp2), fatnum);
350 if (ask(0, "Use FAT 0's entry")) {
354 if (ask(0, "Use FAT %u's entry", fatnum)) {
360 pwarn("Cluster %u is marked %s in FAT 0, but continues with cluster %u in FAT %d\n",
361 cl, rsrvdcltype(*cp1), *cp2, fatnum);
362 if (ask(0, "Use continuation from FAT %u", fatnum)) {
366 if (ask(0, "Use mark from FAT 0")) {
372 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
373 pwarn("Cluster %u continues with cluster %u in FAT 0, but is marked %s in FAT %u\n",
374 cl, *cp1, rsrvdcltype(*cp2), fatnum);
375 if (ask(0, "Use continuation from FAT 0")) {
379 if (ask(0, "Use mark from FAT %d", fatnum)) {
385 pwarn("Cluster %u continues with cluster %u in FAT 0, but with cluster %u in FAT %u\n",
386 cl, *cp1, *cp2, fatnum);
387 if (ask(0, "Use continuation from FAT 0")) {
391 if (ask(0, "Use continuation from FAT %u", fatnum)) {
399 * Compare two FAT copies in memory. Resolve any conflicts and merge them
400 * into the first one.
403 comparefat(struct bootblock *boot, struct fatEntry *first,
404 struct fatEntry *second, u_int fatnum)
409 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++)
410 if (first[cl].next != second[cl].next)
411 ret |= clustdiffer(cl, &first[cl].next, &second[cl].next, fatnum);
416 clearchain(struct bootblock *boot, struct fatEntry *fat, cl_t head)
420 for (p = head; p >= CLUST_FIRST && p < boot->NumClusters; p = q) {
421 if (fat[p].head != head)
424 fat[p].next = fat[p].head = CLUST_FREE;
430 tryclear(struct bootblock *boot, struct fatEntry *fat, cl_t head, cl_t *trunc)
432 if (ask(0, "Clear chain starting at %u", head)) {
433 clearchain(boot, fat, head);
435 } else if (ask(0, "Truncate")) {
443 * Check a complete FAT in-memory for crosslinks
446 checkfat(struct bootblock *boot, struct fatEntry *fat)
454 * pass 1: figure out the cluster chains.
456 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
457 /* find next untravelled chain */
458 if (fat[head].head != 0 /* cluster already belongs to some chain */
459 || fat[head].next == CLUST_FREE
460 || fat[head].next == CLUST_BAD)
461 continue; /* skip it. */
463 /* follow the chain and mark all clusters on the way */
464 for (len = 0, p = head;
465 p >= CLUST_FIRST && p < boot->NumClusters;
471 /* the head record gets the length */
472 fat[head].length = fat[head].next == CLUST_FREE ? 0 : len;
476 * pass 2: check for crosslinked chains (we couldn't do this in pass 1 because
477 * we didn't know the real start of the chain then - would have treated partial
478 * chains as interlinked with their main chain)
480 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
481 /* find next untravelled chain */
482 if (fat[head].head != head)
485 /* follow the chain to its end (hopefully) */
487 (n = fat[p].next) >= CLUST_FIRST && n < boot->NumClusters;
489 if (fat[n].head != head)
494 if (n == CLUST_FREE || n >= CLUST_RSRVD) {
495 pwarn("Cluster chain starting at %u ends with cluster marked %s\n",
496 head, rsrvdcltype(n));
497 ret |= tryclear(boot, fat, head, &fat[p].next);
500 if (n < CLUST_FIRST || n >= boot->NumClusters) {
501 pwarn("Cluster chain starting at %u ends with cluster out of range (%u)\n",
503 ret |= tryclear(boot, fat, head, &fat[p].next);
506 pwarn("Cluster chains starting at %u and %u are linked at cluster %u\n",
507 head, fat[n].head, n);
508 conf = tryclear(boot, fat, head, &fat[p].next);
509 if (ask(0, "Clear chain starting at %u", h = fat[n].head)) {
510 if (conf == FSERROR) {
512 * Transfer the common chain to the one not cleared above.
515 p >= CLUST_FIRST && p < boot->NumClusters;
517 if (h != fat[p].head) {
519 * Have to reexamine this chain.
527 clearchain(boot, fat, h);
537 * Write out FATs encoding them from the internal format
540 writefat(int fs, struct bootblock *boot, struct fatEntry *fat, int correct_fat)
549 buffer = malloc(fatsz = boot->FATsecs * boot->bpbBytesPerSec);
550 if (buffer == NULL) {
551 perror("No space for FAT");
554 memset(buffer, 0, fatsz);
558 *p++ = (u_char)boot->bpbMedia;
561 switch (boot->ClustMask) {
574 /* use same FAT signature as the old FAT has */
578 switch (boot->ClustMask) {
590 if (!_readfat(fs, boot, boot->ValidFat >= 0 ? boot->ValidFat :0,
596 memcpy(p, old_fat, count);
601 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++) {
602 switch (boot->ClustMask) {
604 if (fat[cl].next == CLUST_FREE)
606 *p++ = (u_char)fat[cl].next;
607 *p++ = (u_char)(fat[cl].next >> 8);
608 *p++ = (u_char)(fat[cl].next >> 16);
610 *p++ |= (fat[cl].next >> 24)&0x0f;
613 if (fat[cl].next == CLUST_FREE)
615 *p++ = (u_char)fat[cl].next;
616 *p++ = (u_char)(fat[cl].next >> 8);
619 if (fat[cl].next == CLUST_FREE)
621 if (cl + 1 < boot->NumClusters
622 && fat[cl + 1].next == CLUST_FREE)
624 *p++ = (u_char)fat[cl].next;
625 *p++ = (u_char)((fat[cl].next >> 8) & 0xf)
626 |(u_char)(fat[cl+1].next << 4);
627 *p++ = (u_char)(fat[++cl].next >> 4);
631 for (i = 0; i < boot->bpbFATs; i++) {
632 off = boot->bpbResSectors + i * boot->FATsecs;
633 off *= boot->bpbBytesPerSec;
634 if (lseek(fs, off, SEEK_SET) != off
635 || (size_t)write(fs, buffer, fatsz) != fatsz) {
636 perror("Unable to write FAT");
637 ret = FSFATAL; /* Return immediately? XXX */
645 * Check a complete in-memory FAT for lost cluster chains
648 checklost(int dosfs, struct bootblock *boot, struct fatEntry *fat)
654 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
655 /* find next untravelled chain */
656 if (fat[head].head != head
657 || fat[head].next == CLUST_FREE
658 || (fat[head].next >= CLUST_RSRVD
659 && fat[head].next < CLUST_EOFS)
660 || (fat[head].flags & FAT_USED))
663 pwarn("Lost cluster chain at cluster %u\n%d Cluster(s) lost\n",
664 head, fat[head].length);
665 mod |= ret = reconnect(dosfs, boot, fat, head);
668 if (ret == FSERROR && ask(0, "Clear")) {
669 clearchain(boot, fat, head);
675 if (boot->bpbFSInfo) {
677 if (boot->FSFree != boot->NumFree) {
678 pwarn("Free space in FSInfo block (%d) not correct (%d)\n",
679 boot->FSFree, boot->NumFree);
681 boot->FSFree = boot->NumFree;
686 mod |= writefsinfo(dosfs, boot);