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.
43 static int checkclnum(struct bootblock *, int, cl_t, cl_t *);
44 static int clustdiffer(cl_t, cl_t *, cl_t *, int);
45 static int tryclear(struct bootblock *, struct fatEntry *, cl_t, cl_t *);
46 static int _readfat(int, struct bootblock *, 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->ResSectors;
77 off *= boot->BytesPerSec;
79 buffer = malloc(boot->BytesPerSec);
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->BytesPerSec) != boot->BytesPerSec) {
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->Media || 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, 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(1, "Truncate")) {
159 * Read a FAT from disk. Returns 1 if successful, 0 otherwise.
162 _readfat(int fs, struct bootblock *boot, int no, u_char **buffer)
166 printf("Attempting to allocate %u KB for FAT\n",
167 (boot->FATsecs * boot->BytesPerSec) / 1024);
169 *buffer = malloc(boot->FATsecs * boot->BytesPerSec);
170 if (*buffer == NULL) {
171 perror("No space for FAT");
175 off = boot->ResSectors + no * boot->FATsecs;
176 off *= boot->BytesPerSec;
178 if (lseek(fs, off, SEEK_SET) != off) {
179 perror("Unable to read FAT");
183 if (read(fs, *buffer, boot->FATsecs * boot->BytesPerSec)
184 != boot->FATsecs * boot->BytesPerSec) {
185 perror("Unable to read FAT");
197 * Read a FAT and decode it into internal format
200 readfat(int fs, struct bootblock *boot, int no, struct fatEntry **fp)
202 struct fatEntry *fat;
207 boot->NumFree = boot->NumBad = 0;
209 if (!_readfat(fs, boot, no, &buffer))
212 fat = calloc(boot->NumClusters, sizeof(struct fatEntry));
214 perror("No space for FAT");
219 if (buffer[0] != boot->Media
220 || buffer[1] != 0xff || buffer[2] != 0xff
221 || (boot->ClustMask == CLUST16_MASK && buffer[3] != 0xff)
222 || (boot->ClustMask == CLUST32_MASK
223 && ((buffer[3]&0x0f) != 0x0f
224 || buffer[4] != 0xff || buffer[5] != 0xff
225 || buffer[6] != 0xff || (buffer[7]&0x0f) != 0x0f))) {
227 /* Windows 95 OSR2 (and possibly any later) changes
228 * the FAT signature to 0xXXffff7f for FAT16 and to
229 * 0xXXffff0fffffff07 for FAT32 upon boot, to know that the
230 * file system is dirty if it doesn't reboot cleanly.
231 * Check this special condition before errorring out.
233 if (buffer[0] == boot->Media && buffer[1] == 0xff
235 && ((boot->ClustMask == CLUST16_MASK && buffer[3] == 0x7f)
236 || (boot->ClustMask == CLUST32_MASK
237 && buffer[3] == 0x0f && buffer[4] == 0xff
238 && buffer[5] == 0xff && buffer[6] == 0xff
239 && buffer[7] == 0x07)))
242 /* just some odd byte sequence in FAT */
244 switch (boot->ClustMask) {
246 pwarn("%s (%02x%02x%02x%02x%02x%02x%02x%02x)\n",
247 "FAT starts with odd byte sequence",
248 buffer[0], buffer[1], buffer[2], buffer[3],
249 buffer[4], buffer[5], buffer[6], buffer[7]);
252 pwarn("%s (%02x%02x%02x%02x)\n",
253 "FAT starts with odd byte sequence",
254 buffer[0], buffer[1], buffer[2], buffer[3]);
257 pwarn("%s (%02x%02x%02x)\n",
258 "FAT starts with odd byte sequence",
259 buffer[0], buffer[1], buffer[2]);
264 if (ask(1, "Correct"))
268 switch (boot->ClustMask) {
279 for (cl = CLUST_FIRST; cl < boot->NumClusters;) {
280 switch (boot->ClustMask) {
282 fat[cl].next = p[0] + (p[1] << 8)
283 + (p[2] << 16) + (p[3] << 24);
284 fat[cl].next &= boot->ClustMask;
285 ret |= checkclnum(boot, no, cl, &fat[cl].next);
290 fat[cl].next = p[0] + (p[1] << 8);
291 ret |= checkclnum(boot, no, cl, &fat[cl].next);
296 fat[cl].next = (p[0] + (p[1] << 8)) & 0x0fff;
297 ret |= checkclnum(boot, no, cl, &fat[cl].next);
299 if (cl >= boot->NumClusters)
301 fat[cl].next = ((p[1] >> 4) + (p[2] << 4)) & 0x0fff;
302 ret |= checkclnum(boot, no, cl, &fat[cl].next);
315 * Get type of reserved cluster
320 if (cl == CLUST_FREE)
330 clustdiffer(cl_t cl, cl_t *cp1, cl_t *cp2, int fatnum)
332 if (*cp1 == CLUST_FREE || *cp1 >= CLUST_RSRVD) {
333 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
334 if ((*cp1 != CLUST_FREE && *cp1 < CLUST_BAD
335 && *cp2 != CLUST_FREE && *cp2 < CLUST_BAD)
336 || (*cp1 > CLUST_BAD && *cp2 > CLUST_BAD)) {
337 pwarn("Cluster %u is marked %s with different indicators\n",
338 cl, rsrvdcltype(*cp1));
345 pwarn("Cluster %u is marked %s in FAT 0, %s in FAT %d\n",
346 cl, rsrvdcltype(*cp1), rsrvdcltype(*cp2), fatnum);
347 if (ask(1, "Use FAT 0's entry")) {
351 if (ask(1, "Use FAT %d's entry", fatnum)) {
357 pwarn("Cluster %u is marked %s in FAT 0, but continues with cluster %u in FAT %d\n",
358 cl, rsrvdcltype(*cp1), *cp2, fatnum);
359 if (ask(1, "Use continuation from FAT %d", fatnum)) {
363 if (ask(1, "Use mark from FAT 0")) {
369 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
370 pwarn("Cluster %u continues with cluster %u in FAT 0, but is marked %s in FAT %d\n",
371 cl, *cp1, rsrvdcltype(*cp2), fatnum);
372 if (ask(1, "Use continuation from FAT 0")) {
376 if (ask(1, "Use mark from FAT %d", fatnum)) {
382 pwarn("Cluster %u continues with cluster %u in FAT 0, but with cluster %u in FAT %d\n",
383 cl, *cp1, *cp2, fatnum);
384 if (ask(1, "Use continuation from FAT 0")) {
388 if (ask(1, "Use continuation from FAT %d", fatnum)) {
396 * Compare two FAT copies in memory. Resolve any conflicts and merge them
397 * into the first one.
400 comparefat(struct bootblock *boot, struct fatEntry *first,
401 struct fatEntry *second, int fatnum)
406 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++)
407 if (first[cl].next != second[cl].next)
408 ret |= clustdiffer(cl, &first[cl].next, &second[cl].next, fatnum);
413 clearchain(struct bootblock *boot, struct fatEntry *fat, cl_t head)
417 for (p = head; p >= CLUST_FIRST && p < boot->NumClusters; p = q) {
418 if (fat[p].head != head)
421 fat[p].next = fat[p].head = CLUST_FREE;
427 tryclear(struct bootblock *boot, struct fatEntry *fat, cl_t head, cl_t *trunc)
429 if (ask(1, "Clear chain starting at %u", head)) {
430 clearchain(boot, fat, head);
432 } else if (ask(1, "Truncate")) {
440 * Check a complete FAT in-memory for crosslinks
443 checkfat(struct bootblock *boot, struct fatEntry *fat)
445 cl_t head, p, h, n, wdk;
451 * pass 1: figure out the cluster chains.
453 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
454 /* find next untravelled chain */
455 if (fat[head].head != 0 /* cluster already belongs to some chain */
456 || fat[head].next == CLUST_FREE
457 || fat[head].next == CLUST_BAD)
458 continue; /* skip it. */
460 /* follow the chain and mark all clusters on the way */
461 for (len = 0, p = head;
462 p >= CLUST_FIRST && p < boot->NumClusters;
464 /* we have to check the len, to avoid infinite loop */
465 if (len > boot->NumClusters) {
466 printf("detect cluster chain loop: head %u for p %u\n", head, p);
474 /* the head record gets the length */
475 fat[head].length = fat[head].next == CLUST_FREE ? 0 : len;
479 * pass 2: check for crosslinked chains (we couldn't do this in pass 1 because
480 * we didn't know the real start of the chain then - would have treated partial
481 * chains as interlinked with their main chain)
483 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
484 /* find next untravelled chain */
485 if (fat[head].head != head)
488 /* follow the chain to its end (hopefully) */
489 /* also possible infinite loop, that's why I insert wdk counter */
490 for (p = head,wdk=boot->NumClusters;
491 (n = fat[p].next) >= CLUST_FIRST && n < boot->NumClusters && wdk;
493 if (fat[n].head != head)
500 if (n == CLUST_FREE || n >= CLUST_RSRVD) {
501 pwarn("Cluster chain starting at %u ends with cluster marked %s\n",
502 head, rsrvdcltype(n));
503 ret |= tryclear(boot, fat, head, &fat[p].next);
506 if (n < CLUST_FIRST || n >= boot->NumClusters) {
507 pwarn("Cluster chain starting at %u ends with cluster out of range (%u)\n",
509 ret |= tryclear(boot, fat, head, &fat[p].next);
512 pwarn("Cluster chains starting at %u and %u are linked at cluster %u\n",
513 head, fat[n].head, n);
514 conf = tryclear(boot, fat, head, &fat[p].next);
515 if (ask(1, "Clear chain starting at %u", h = fat[n].head)) {
516 if (conf == FSERROR) {
518 * Transfer the common chain to the one not cleared above.
521 p >= CLUST_FIRST && p < boot->NumClusters;
523 if (h != fat[p].head) {
525 * Have to reexamine this chain.
533 clearchain(boot, fat, h);
543 * Write out FATs encoding them from the internal format
546 writefat(int fs, struct bootblock *boot, struct fatEntry *fat, int correct_fat)
555 buffer = malloc(fatsz = boot->FATsecs * boot->BytesPerSec);
556 if (buffer == NULL) {
557 perror("No space for FAT");
560 memset(buffer, 0, fatsz);
564 *p++ = (u_char)boot->Media;
567 switch (boot->ClustMask) {
580 /* use same FAT signature as the old FAT has */
584 switch (boot->ClustMask) {
596 if (!_readfat(fs, boot, boot->ValidFat >= 0 ? boot->ValidFat :0,
602 memcpy(p, old_fat, count);
607 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++) {
608 switch (boot->ClustMask) {
610 if (fat[cl].next == CLUST_FREE)
612 *p++ = (u_char)fat[cl].next;
613 *p++ = (u_char)(fat[cl].next >> 8);
614 *p++ = (u_char)(fat[cl].next >> 16);
616 *p++ |= (fat[cl].next >> 24)&0x0f;
619 if (fat[cl].next == CLUST_FREE)
621 *p++ = (u_char)fat[cl].next;
622 *p++ = (u_char)(fat[cl].next >> 8);
625 if (fat[cl].next == CLUST_FREE)
627 if (cl + 1 < boot->NumClusters
628 && fat[cl + 1].next == CLUST_FREE)
630 *p++ = (u_char)fat[cl].next;
631 *p++ = (u_char)((fat[cl].next >> 8) & 0xf)
632 |(u_char)(fat[cl+1].next << 4);
633 *p++ = (u_char)(fat[++cl].next >> 4);
637 for (i = 0; i < boot->FATs; i++) {
638 off = boot->ResSectors + i * boot->FATsecs;
639 off *= boot->BytesPerSec;
640 if (lseek(fs, off, SEEK_SET) != off
641 || write(fs, buffer, fatsz) != fatsz) {
642 perror("Unable to write FAT");
643 ret = FSFATAL; /* Return immediately? XXX */
651 * Check a complete in-memory FAT for lost cluster chains
654 checklost(int dosfs, struct bootblock *boot, struct fatEntry *fat)
660 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
661 /* find next untravelled chain */
662 if (fat[head].head != head
663 || fat[head].next == CLUST_FREE
664 || (fat[head].next >= CLUST_RSRVD
665 && fat[head].next < CLUST_EOFS)
666 || (fat[head].flags & FAT_USED))
669 pwarn("Lost cluster chain at cluster %u\n%d Cluster(s) lost\n",
670 head, fat[head].length);
671 mod |= ret = reconnect(dosfs, boot, fat, head);
673 /* If the reconnect failed, then just clear the chain */
674 pwarn("Error reconnecting chain - clearing\n");
676 clearchain(boot, fat, head);
680 if (ret == FSERROR && ask(1, "Clear")) {
681 clearchain(boot, fat, head);
689 if (boot->FSFree != boot->NumFree) {
690 pwarn("Free space in FSInfo block (%d) not correct (%d)\n",
691 boot->FSFree, boot->NumFree);
693 boot->FSFree = boot->NumFree;
699 if ((boot->FSNext >= boot->NumClusters) || (fat[boot->FSNext].next != CLUST_FREE)) {
700 pwarn("Next free cluster in FSInfo block (%u) not free\n",
703 for (head = CLUST_FIRST; head < boot->NumClusters; head++)
704 if (fat[head].next == CLUST_FREE) {
712 if (boot->FSNext > boot->NumClusters ) {
713 pwarn("FSNext block (%d) not correct NumClusters (%d)\n",
714 boot->FSNext, boot->NumClusters);
715 boot->FSNext=CLUST_FIRST; // boot->FSNext can have -1 value.
718 if (boot->NumFree && fat[boot->FSNext].next != CLUST_FREE) {
719 pwarn("Next free cluster in FSInfo block (%u) not free\n",
722 for (head = CLUST_FIRST; head < boot->NumClusters; head++)
723 if (fat[head].next == CLUST_FREE) {
731 mod |= writefsinfo(dosfs, boot);