1/*
2 * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
3 * Copyright (c) 1995 Martin Husemann
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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.
31 */
32
33
34#include <sys/cdefs.h>
35#ifndef lint
36__RCSID("$NetBSD: fat.c,v 1.12 2000/10/10 20:24:52 is Exp $");
37static const char rcsid[] =
38 "$FreeBSD: head/sbin/fsck_msdosfs/fat.c 125469 2004-02-05 06:32:16Z bde $";
39#endif /* not lint */
40
41#include <stdlib.h>
42#include <string.h>
43#include <ctype.h>
44#include <stdio.h>
45#include <unistd.h>
46
47#include "ext.h"
48#include "fsutil.h"
49
50static int checkclnum(struct bootblock *, int, cl_t, cl_t *);
51static int clustdiffer(cl_t, cl_t *, cl_t *, int);
52static int tryclear(struct bootblock *, struct fatEntry *, cl_t, cl_t *);
53static int _readfat(int, struct bootblock *, int, u_char **);
54
55int
56checkdirty(int fs, struct bootblock *boot)
57{
58 off_t off;
59 u_char *buffer;
60 int ret = 0;
61
62 if (boot->ClustMask == CLUST12_MASK)
63 return 0;
64
65 off = boot->ResSectors;
66 off *= boot->BytesPerSec;
67
68 buffer = malloc(boot->BytesPerSec);
69 if (buffer == NULL) {
70 perror("No space for FAT");
71 return 1;
72 }
73
74 if (lseek(fs, off, SEEK_SET) != off) {
75 perror("Unable to read FAT");
76 goto err;
77 }
78
79 if (read(fs, buffer, boot->BytesPerSec) != boot->BytesPerSec) {
80 perror("Unable to read FAT");
81 goto err;
82 }
83
84 if (buffer[0] == boot->Media && buffer[1] == 0xff && buffer[2] == 0xff
85 && ((boot->ClustMask == CLUST16_MASK && buffer[3] == 0x7f)
86 || (boot->ClustMask == CLUST32_MASK && buffer[3] == 0x0f
87 && buffer[4] == 0xff && buffer[5] == 0xff
88 && buffer[6] == 0xff && buffer[7] == 0x07)))
89 ret = 0;
90 else
91 ret = 1;
92
93err:
94 free(buffer);
95 return ret;
96}
97
98/*
99 * Check a cluster number for valid value
100 */
101static int
102checkclnum(struct bootblock *boot, int fat, cl_t cl, cl_t *next)
103{
104 if (*next >= (CLUST_RSRVD&boot->ClustMask))
105 *next |= ~boot->ClustMask;
106 if (*next == CLUST_FREE) {
107 boot->NumFree++;
108 return FSOK;
109 }
110 if (*next == CLUST_BAD) {
111 boot->NumBad++;
112 return FSOK;
113 }
114 if (*next < CLUST_FIRST
115 || (*next >= boot->NumClusters && *next < CLUST_EOFS)) {
116 pwarn("Cluster %u in FAT %d continues with %s cluster number %u\n",
117 cl, fat,
118 *next < CLUST_RSRVD ? "out of range" : "reserved",
119 *next&boot->ClustMask);
120 if (ask(0, "Truncate")) {
121 *next = CLUST_EOF;
122 return FSFATMOD;
123 }
124 return FSERROR;
125 }
126 return FSOK;
127}
128
129/*
130 * Read a FAT from disk. Returns 1 if successful, 0 otherwise.
131 */
132static int
133_readfat(int fs, struct bootblock *boot, int no, u_char **buffer)
134{
135 off_t off;
136
137 *buffer = malloc(boot->FATsecs * boot->BytesPerSec);
138 if (*buffer == NULL) {
139 perror("No space for FAT");
140 return 0;
141 }
142
143 off = boot->ResSectors + no * boot->FATsecs;
144 off *= boot->BytesPerSec;
145
146 if (lseek(fs, off, SEEK_SET) != off) {
147 perror("Unable to read FAT");
148 goto err;
149 }
150
151 if (read(fs, *buffer, boot->FATsecs * boot->BytesPerSec)
152 != boot->FATsecs * boot->BytesPerSec) {
153 perror("Unable to read FAT");
154 goto err;
155 }
156
157 return 1;
158
159 err:
160 free(*buffer);
161 return 0;
162}
163
164/*
165 * Read a FAT and decode it into internal format
166 */
167int
168readfat(int fs, struct bootblock *boot, int no, struct fatEntry **fp)
169{
170 struct fatEntry *fat;
171 u_char *buffer, *p;
172 cl_t cl;
173 int ret = FSOK;
174
175 boot->NumFree = boot->NumBad = 0;
176
177 if (!_readfat(fs, boot, no, &buffer))
178 return FSFATAL;
179
180 fat = calloc(boot->NumClusters, sizeof(struct fatEntry));
181 if (fat == NULL) {
182 perror("No space for FAT");
183 free(buffer);
184 return FSFATAL;
185 }
186
187 if (buffer[0] != boot->Media
188 || buffer[1] != 0xff || buffer[2] != 0xff
189 || (boot->ClustMask == CLUST16_MASK && buffer[3] != 0xff)
190 || (boot->ClustMask == CLUST32_MASK
191 && ((buffer[3]&0x0f) != 0x0f
192 || buffer[4] != 0xff || buffer[5] != 0xff
193 || buffer[6] != 0xff || (buffer[7]&0x0f) != 0x0f))) {
194
195 /* Windows 95 OSR2 (and possibly any later) changes
196 * the FAT signature to 0xXXffff7f for FAT16 and to
197 * 0xXXffff0fffffff07 for FAT32 upon boot, to know that the
198 * file system is dirty if it doesn't reboot cleanly.
199 * Check this special condition before errorring out.
200 */
201 if (buffer[0] == boot->Media && buffer[1] == 0xff
202 && buffer[2] == 0xff
203 && ((boot->ClustMask == CLUST16_MASK && buffer[3] == 0x7f)
204 || (boot->ClustMask == CLUST32_MASK
205 && buffer[3] == 0x0f && buffer[4] == 0xff
206 && buffer[5] == 0xff && buffer[6] == 0xff
207 && buffer[7] == 0x07)))
208 ret |= FSDIRTY;
209 else {
210 /* just some odd byte sequence in FAT */
211
212 switch (boot->ClustMask) {
213 case CLUST32_MASK:
214 pwarn("%s (%02x%02x%02x%02x%02x%02x%02x%02x)\n",
215 "FAT starts with odd byte sequence",
216 buffer[0], buffer[1], buffer[2], buffer[3],
217 buffer[4], buffer[5], buffer[6], buffer[7]);
218 break;
219 case CLUST16_MASK:
220 pwarn("%s (%02x%02x%02x%02x)\n",
221 "FAT starts with odd byte sequence",
222 buffer[0], buffer[1], buffer[2], buffer[3]);
223 break;
224 default:
225 pwarn("%s (%02x%02x%02x)\n",
226 "FAT starts with odd byte sequence",
227 buffer[0], buffer[1], buffer[2]);
228 break;
229 }
230
231
232 if (ask(1, "Correct"))
233 ret |= FSFIXFAT;
234 }
235 }
236 switch (boot->ClustMask) {
237 case CLUST32_MASK:
238 p = buffer + 8;
239 break;
240 case CLUST16_MASK:
241 p = buffer + 4;
242 break;
243 default:
244 p = buffer + 3;
245 break;
246 }
247 for (cl = CLUST_FIRST; cl < boot->NumClusters;) {
248 switch (boot->ClustMask) {
249 case CLUST32_MASK:
250 fat[cl].next = p[0] + (p[1] << 8)
251 + (p[2] << 16) + (p[3] << 24);
252 fat[cl].next &= boot->ClustMask;
253 ret |= checkclnum(boot, no, cl, &fat[cl].next);
254 cl++;
255 p += 4;
256 break;
257 case CLUST16_MASK:
258 fat[cl].next = p[0] + (p[1] << 8);
259 ret |= checkclnum(boot, no, cl, &fat[cl].next);
260 cl++;
261 p += 2;
262 break;
263 default:
264 fat[cl].next = (p[0] + (p[1] << 8)) & 0x0fff;
265 ret |= checkclnum(boot, no, cl, &fat[cl].next);
266 cl++;
267 if (cl >= boot->NumClusters)
268 break;
269 fat[cl].next = ((p[1] >> 4) + (p[2] << 4)) & 0x0fff;
270 ret |= checkclnum(boot, no, cl, &fat[cl].next);
271 cl++;
272 p += 3;
273 break;
274 }
275 }
276
277 free(buffer);
278 *fp = fat;
279 return ret;
280}
281
282/*
283 * Get type of reserved cluster
284 */
285char *
286rsrvdcltype(cl_t cl)
287{
288 if (cl == CLUST_FREE)
289 return "free";
290 if (cl < CLUST_BAD)
291 return "reserved";
292 if (cl > CLUST_BAD)
293 return "as EOF";
294 return "bad";
295}
296
297static int
298clustdiffer(cl_t cl, cl_t *cp1, cl_t *cp2, int fatnum)
299{
300 if (*cp1 == CLUST_FREE || *cp1 >= CLUST_RSRVD) {
301 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
302 if ((*cp1 != CLUST_FREE && *cp1 < CLUST_BAD
303 && *cp2 != CLUST_FREE && *cp2 < CLUST_BAD)
304 || (*cp1 > CLUST_BAD && *cp2 > CLUST_BAD)) {
305 pwarn("Cluster %u is marked %s with different indicators, ",
306 cl, rsrvdcltype(*cp1));
307 if (ask(1, "fix")) {
308 *cp2 = *cp1;
309 return FSFATMOD;
310 }
311 return FSFATAL;
312 }
313 pwarn("Cluster %u is marked %s in FAT 0, %s in FAT %d\n",
314 cl, rsrvdcltype(*cp1), rsrvdcltype(*cp2), fatnum);
315 if (ask(0, "use FAT 0's entry")) {
316 *cp2 = *cp1;
317 return FSFATMOD;
318 }
319 if (ask(0, "use FAT %d's entry", fatnum)) {
320 *cp1 = *cp2;
321 return FSFATMOD;
322 }
323 return FSFATAL;
324 }
325 pwarn("Cluster %u is marked %s in FAT 0, but continues with cluster %u in FAT %d\n",
326 cl, rsrvdcltype(*cp1), *cp2, fatnum);
327 if (ask(0, "Use continuation from FAT %d", fatnum)) {
328 *cp1 = *cp2;
329 return FSFATMOD;
330 }
331 if (ask(0, "Use mark from FAT 0")) {
332 *cp2 = *cp1;
333 return FSFATMOD;
334 }
335 return FSFATAL;
336 }
337 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
338 pwarn("Cluster %u continues with cluster %u in FAT 0, but is marked %s in FAT %d\n",
339 cl, *cp1, rsrvdcltype(*cp2), fatnum);
340 if (ask(0, "Use continuation from FAT 0")) {
341 *cp2 = *cp1;
342 return FSFATMOD;
343 }
344 if (ask(0, "Use mark from FAT %d", fatnum)) {
345 *cp1 = *cp2;
346 return FSFATMOD;
347 }
348 return FSERROR;
349 }
350 pwarn("Cluster %u continues with cluster %u in FAT 0, but with cluster %u in FAT %d\n",
351 cl, *cp1, *cp2, fatnum);
352 if (ask(0, "Use continuation from FAT 0")) {
353 *cp2 = *cp1;
354 return FSFATMOD;
355 }
356 if (ask(0, "Use continuation from FAT %d", fatnum)) {
357 *cp1 = *cp2;
358 return FSFATMOD;
359 }
360 return FSERROR;
361}
362
363/*
364 * Compare two FAT copies in memory. Resolve any conflicts and merge them
365 * into the first one.
366 */
367int
368comparefat(struct bootblock *boot, struct fatEntry *first,
369 struct fatEntry *second, int fatnum)
370{
371 cl_t cl;
372 int ret = FSOK;
373
374 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++)
375 if (first[cl].next != second[cl].next)
376 ret |= clustdiffer(cl, &first[cl].next, &second[cl].next, fatnum);
377 return ret;
378}
379
380void
381clearchain(struct bootblock *boot, struct fatEntry *fat, cl_t head)
382{
383 cl_t p, q;
384
385 for (p = head; p >= CLUST_FIRST && p < boot->NumClusters; p = q) {
386 if (fat[p].head != head)
387 break;
388 q = fat[p].next;
389 fat[p].next = fat[p].head = CLUST_FREE;
390 fat[p].length = 0;
391 }
392}
393
394int
395tryclear(struct bootblock *boot, struct fatEntry *fat, cl_t head, cl_t *trunc)
396{
397 if (ask(0, "Clear chain starting at %u", head)) {
398 clearchain(boot, fat, head);
399 return FSFATMOD;
400 } else if (ask(0, "Truncate")) {
401 *trunc = CLUST_EOF;
402 return FSFATMOD;
403 } else
404 return FSERROR;
405}
406
407/*
408 * Check a complete FAT in-memory for crosslinks
409 */
410int
411checkfat(struct bootblock *boot, struct fatEntry *fat)
412{
413 cl_t head, p, h, n;
414 u_int len;
415 int ret = 0;
416 int conf;
417
418 /*
419 * pass 1: figure out the cluster chains.
420 */
421 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
422 /* find next untravelled chain */
423 if (fat[head].head != 0 /* cluster already belongs to some chain */
424 || fat[head].next == CLUST_FREE
425 || fat[head].next == CLUST_BAD)
426 continue; /* skip it. */
427
428 /* follow the chain and mark all clusters on the way */
429 for (len = 0, p = head;
430 p >= CLUST_FIRST && p < boot->NumClusters;
431 p = fat[p].next) {
432 fat[p].head = head;
433 len++;
434 }
435
436 /* the head record gets the length */
437 fat[head].length = fat[head].next == CLUST_FREE ? 0 : len;
438 }
439
440 /*
441 * pass 2: check for crosslinked chains (we couldn't do this in pass 1 because
442 * we didn't know the real start of the chain then - would have treated partial
443 * chains as interlinked with their main chain)
444 */
445 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
446 /* find next untravelled chain */
447 if (fat[head].head != head)
448 continue;
449
450 /* follow the chain to its end (hopefully) */
451 for (p = head;
452 (n = fat[p].next) >= CLUST_FIRST && n < boot->NumClusters;
453 p = n)
454 if (fat[n].head != head)
455 break;
456 if (n >= CLUST_EOFS)
457 continue;
458
459 if (n == CLUST_FREE || n >= CLUST_RSRVD) {
460 pwarn("Cluster chain starting at %u ends with cluster marked %s\n",
461 head, rsrvdcltype(n));
462 ret |= tryclear(boot, fat, head, &fat[p].next);
463 continue;
464 }
465 if (n < CLUST_FIRST || n >= boot->NumClusters) {
466 pwarn("Cluster chain starting at %u ends with cluster out of range (%u)\n",
467 head, n);
468 ret |= tryclear(boot, fat, head, &fat[p].next);
469 continue;
470 }
471 pwarn("Cluster chains starting at %u and %u are linked at cluster %u\n",
472 head, fat[n].head, n);
473 conf = tryclear(boot, fat, head, &fat[p].next);
474 if (ask(0, "Clear chain starting at %u", h = fat[n].head)) {
475 if (conf == FSERROR) {
476 /*
477 * Transfer the common chain to the one not cleared above.
478 */
479 for (p = n;
480 p >= CLUST_FIRST && p < boot->NumClusters;
481 p = fat[p].next) {
482 if (h != fat[p].head) {
483 /*
484 * Have to reexamine this chain.
485 */
486 head--;
487 break;
488 }
489 fat[p].head = head;
490 }
491 }
492 clearchain(boot, fat, h);
493 conf |= FSFATMOD;
494 }
495 ret |= conf;
496 }
497
498 return ret;
499}
500
501/*
502 * Write out FATs encoding them from the internal format
503 */
504int
505writefat(int fs, struct bootblock *boot, struct fatEntry *fat, int correct_fat)
506{
507 u_char *buffer, *p;
508 cl_t cl;
509 int i;
510 u_int32_t fatsz;
511 off_t off;
512 int ret = FSOK;
513
514 buffer = malloc(fatsz = boot->FATsecs * boot->BytesPerSec);
515 if (buffer == NULL) {
516 perror("No space for FAT");
517 return FSFATAL;
518 }
519 memset(buffer, 0, fatsz);
520 boot->NumFree = 0;
521 p = buffer;
522 if (correct_fat) {
523 *p++ = (u_char)boot->Media;
524 *p++ = 0xff;
525 *p++ = 0xff;
526 switch (boot->ClustMask) {
527 case CLUST16_MASK:
528 *p++ = 0xff;
529 break;
530 case CLUST32_MASK:
531 *p++ = 0x0f;
532 *p++ = 0xff;
533 *p++ = 0xff;
534 *p++ = 0xff;
535 *p++ = 0x0f;
536 break;
537 }
538 } else {
539 /* use same FAT signature as the old FAT has */
540 int count;
541 u_char *old_fat;
542
543 switch (boot->ClustMask) {
544 case CLUST32_MASK:
545 count = 8;
546 break;
547 case CLUST16_MASK:
548 count = 4;
549 break;
550 default:
551 count = 3;
552 break;
553 }
554
555 if (!_readfat(fs, boot, boot->ValidFat >= 0 ? boot->ValidFat :0,
556 &old_fat)) {
557 free(buffer);
558 return FSFATAL;
559 }
560
561 memcpy(p, old_fat, count);
562 free(old_fat);
563 p += count;
564 }
565
566 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++) {
567 switch (boot->ClustMask) {
568 case CLUST32_MASK:
569 if (fat[cl].next == CLUST_FREE)
570 boot->NumFree++;
571 *p++ = (u_char)fat[cl].next;
572 *p++ = (u_char)(fat[cl].next >> 8);
573 *p++ = (u_char)(fat[cl].next >> 16);
574 *p &= 0xf0;
575 *p++ |= (fat[cl].next >> 24)&0x0f;
576 break;
577 case CLUST16_MASK:
578 if (fat[cl].next == CLUST_FREE)
579 boot->NumFree++;
580 *p++ = (u_char)fat[cl].next;
581 *p++ = (u_char)(fat[cl].next >> 8);
582 break;
583 default:
584 if (fat[cl].next == CLUST_FREE)
585 boot->NumFree++;
586 if (cl + 1 < boot->NumClusters
587 && fat[cl + 1].next == CLUST_FREE)
588 boot->NumFree++;
589 *p++ = (u_char)fat[cl].next;
590 *p++ = (u_char)((fat[cl].next >> 8) & 0xf)
591 |(u_char)(fat[cl+1].next << 4);
592 *p++ = (u_char)(fat[++cl].next >> 4);
593 break;
594 }
595 }
596 for (i = 0; i < boot->FATs; i++) {
597 off = boot->ResSectors + i * boot->FATsecs;
598 off *= boot->BytesPerSec;
599 if (lseek(fs, off, SEEK_SET) != off
600 || write(fs, buffer, fatsz) != fatsz) {
601 perror("Unable to write FAT");
602 ret = FSFATAL; /* Return immediately? XXX */
603 }
604 }
605 free(buffer);
606 return ret;
607}
608
609/*
610 * Check a complete in-memory FAT for lost cluster chains
611 */
612int
613checklost(int dosfs, struct bootblock *boot, struct fatEntry *fat)
614{
615 cl_t head;
616 int mod = FSOK;
617 int ret;
618
619 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
620 /* find next untravelled chain */
621 if (fat[head].head != head
622 || fat[head].next == CLUST_FREE
623 || (fat[head].next >= CLUST_RSRVD
624 && fat[head].next < CLUST_EOFS)
625 || (fat[head].flags & FAT_USED))
626 continue;
627
628 pwarn("Lost cluster chain at cluster %u\n%d Cluster(s) lost\n",
629 head, fat[head].length);
630 mod |= ret = reconnect(dosfs, boot, fat, head);
631 if (mod & FSFATAL)
632 break;
633 if (ret == FSERROR && ask(0, "Clear")) {
634 clearchain(boot, fat, head);
635 mod |= FSFATMOD;
636 }
637 }
638 finishlf();
639
640 if (boot->FSInfo) {
641 ret = 0;
642 if (boot->FSFree != boot->NumFree) {
643 pwarn("Free space in FSInfo block (%d) not correct (%d)\n",
644 boot->FSFree, boot->NumFree);
645 if (ask(1, "fix")) {
646 boot->FSFree = boot->NumFree;
647 ret = 1;
648 }
649 }
650 if (boot->NumFree && fat[boot->FSNext].next != CLUST_FREE) {
651 pwarn("Next free cluster in FSInfo block (%u) not free\n",
652 boot->FSNext);
653 if (ask(1, "fix"))
654 for (head = CLUST_FIRST; head < boot->NumClusters; head++)
655 if (fat[head].next == CLUST_FREE) {
656 boot->FSNext = head;
657 ret = 1;
658 break;
659 }
660 }
661 if (ret)
662 mod |= writefsinfo(dosfs, boot);
663 }
664
665 return mod;
666}
2 * Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
3 * Copyright (c) 1995 Martin Husemann
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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.
31 */
32
33
34#include <sys/cdefs.h>
35#ifndef lint
36__RCSID("$NetBSD: fat.c,v 1.12 2000/10/10 20:24:52 is Exp $");
37static const char rcsid[] =
38 "$FreeBSD: head/sbin/fsck_msdosfs/fat.c 125469 2004-02-05 06:32:16Z bde $";
39#endif /* not lint */
40
41#include <stdlib.h>
42#include <string.h>
43#include <ctype.h>
44#include <stdio.h>
45#include <unistd.h>
46
47#include "ext.h"
48#include "fsutil.h"
49
50static int checkclnum(struct bootblock *, int, cl_t, cl_t *);
51static int clustdiffer(cl_t, cl_t *, cl_t *, int);
52static int tryclear(struct bootblock *, struct fatEntry *, cl_t, cl_t *);
53static int _readfat(int, struct bootblock *, int, u_char **);
54
55int
56checkdirty(int fs, struct bootblock *boot)
57{
58 off_t off;
59 u_char *buffer;
60 int ret = 0;
61
62 if (boot->ClustMask == CLUST12_MASK)
63 return 0;
64
65 off = boot->ResSectors;
66 off *= boot->BytesPerSec;
67
68 buffer = malloc(boot->BytesPerSec);
69 if (buffer == NULL) {
70 perror("No space for FAT");
71 return 1;
72 }
73
74 if (lseek(fs, off, SEEK_SET) != off) {
75 perror("Unable to read FAT");
76 goto err;
77 }
78
79 if (read(fs, buffer, boot->BytesPerSec) != boot->BytesPerSec) {
80 perror("Unable to read FAT");
81 goto err;
82 }
83
84 if (buffer[0] == boot->Media && buffer[1] == 0xff && buffer[2] == 0xff
85 && ((boot->ClustMask == CLUST16_MASK && buffer[3] == 0x7f)
86 || (boot->ClustMask == CLUST32_MASK && buffer[3] == 0x0f
87 && buffer[4] == 0xff && buffer[5] == 0xff
88 && buffer[6] == 0xff && buffer[7] == 0x07)))
89 ret = 0;
90 else
91 ret = 1;
92
93err:
94 free(buffer);
95 return ret;
96}
97
98/*
99 * Check a cluster number for valid value
100 */
101static int
102checkclnum(struct bootblock *boot, int fat, cl_t cl, cl_t *next)
103{
104 if (*next >= (CLUST_RSRVD&boot->ClustMask))
105 *next |= ~boot->ClustMask;
106 if (*next == CLUST_FREE) {
107 boot->NumFree++;
108 return FSOK;
109 }
110 if (*next == CLUST_BAD) {
111 boot->NumBad++;
112 return FSOK;
113 }
114 if (*next < CLUST_FIRST
115 || (*next >= boot->NumClusters && *next < CLUST_EOFS)) {
116 pwarn("Cluster %u in FAT %d continues with %s cluster number %u\n",
117 cl, fat,
118 *next < CLUST_RSRVD ? "out of range" : "reserved",
119 *next&boot->ClustMask);
120 if (ask(0, "Truncate")) {
121 *next = CLUST_EOF;
122 return FSFATMOD;
123 }
124 return FSERROR;
125 }
126 return FSOK;
127}
128
129/*
130 * Read a FAT from disk. Returns 1 if successful, 0 otherwise.
131 */
132static int
133_readfat(int fs, struct bootblock *boot, int no, u_char **buffer)
134{
135 off_t off;
136
137 *buffer = malloc(boot->FATsecs * boot->BytesPerSec);
138 if (*buffer == NULL) {
139 perror("No space for FAT");
140 return 0;
141 }
142
143 off = boot->ResSectors + no * boot->FATsecs;
144 off *= boot->BytesPerSec;
145
146 if (lseek(fs, off, SEEK_SET) != off) {
147 perror("Unable to read FAT");
148 goto err;
149 }
150
151 if (read(fs, *buffer, boot->FATsecs * boot->BytesPerSec)
152 != boot->FATsecs * boot->BytesPerSec) {
153 perror("Unable to read FAT");
154 goto err;
155 }
156
157 return 1;
158
159 err:
160 free(*buffer);
161 return 0;
162}
163
164/*
165 * Read a FAT and decode it into internal format
166 */
167int
168readfat(int fs, struct bootblock *boot, int no, struct fatEntry **fp)
169{
170 struct fatEntry *fat;
171 u_char *buffer, *p;
172 cl_t cl;
173 int ret = FSOK;
174
175 boot->NumFree = boot->NumBad = 0;
176
177 if (!_readfat(fs, boot, no, &buffer))
178 return FSFATAL;
179
180 fat = calloc(boot->NumClusters, sizeof(struct fatEntry));
181 if (fat == NULL) {
182 perror("No space for FAT");
183 free(buffer);
184 return FSFATAL;
185 }
186
187 if (buffer[0] != boot->Media
188 || buffer[1] != 0xff || buffer[2] != 0xff
189 || (boot->ClustMask == CLUST16_MASK && buffer[3] != 0xff)
190 || (boot->ClustMask == CLUST32_MASK
191 && ((buffer[3]&0x0f) != 0x0f
192 || buffer[4] != 0xff || buffer[5] != 0xff
193 || buffer[6] != 0xff || (buffer[7]&0x0f) != 0x0f))) {
194
195 /* Windows 95 OSR2 (and possibly any later) changes
196 * the FAT signature to 0xXXffff7f for FAT16 and to
197 * 0xXXffff0fffffff07 for FAT32 upon boot, to know that the
198 * file system is dirty if it doesn't reboot cleanly.
199 * Check this special condition before errorring out.
200 */
201 if (buffer[0] == boot->Media && buffer[1] == 0xff
202 && buffer[2] == 0xff
203 && ((boot->ClustMask == CLUST16_MASK && buffer[3] == 0x7f)
204 || (boot->ClustMask == CLUST32_MASK
205 && buffer[3] == 0x0f && buffer[4] == 0xff
206 && buffer[5] == 0xff && buffer[6] == 0xff
207 && buffer[7] == 0x07)))
208 ret |= FSDIRTY;
209 else {
210 /* just some odd byte sequence in FAT */
211
212 switch (boot->ClustMask) {
213 case CLUST32_MASK:
214 pwarn("%s (%02x%02x%02x%02x%02x%02x%02x%02x)\n",
215 "FAT starts with odd byte sequence",
216 buffer[0], buffer[1], buffer[2], buffer[3],
217 buffer[4], buffer[5], buffer[6], buffer[7]);
218 break;
219 case CLUST16_MASK:
220 pwarn("%s (%02x%02x%02x%02x)\n",
221 "FAT starts with odd byte sequence",
222 buffer[0], buffer[1], buffer[2], buffer[3]);
223 break;
224 default:
225 pwarn("%s (%02x%02x%02x)\n",
226 "FAT starts with odd byte sequence",
227 buffer[0], buffer[1], buffer[2]);
228 break;
229 }
230
231
232 if (ask(1, "Correct"))
233 ret |= FSFIXFAT;
234 }
235 }
236 switch (boot->ClustMask) {
237 case CLUST32_MASK:
238 p = buffer + 8;
239 break;
240 case CLUST16_MASK:
241 p = buffer + 4;
242 break;
243 default:
244 p = buffer + 3;
245 break;
246 }
247 for (cl = CLUST_FIRST; cl < boot->NumClusters;) {
248 switch (boot->ClustMask) {
249 case CLUST32_MASK:
250 fat[cl].next = p[0] + (p[1] << 8)
251 + (p[2] << 16) + (p[3] << 24);
252 fat[cl].next &= boot->ClustMask;
253 ret |= checkclnum(boot, no, cl, &fat[cl].next);
254 cl++;
255 p += 4;
256 break;
257 case CLUST16_MASK:
258 fat[cl].next = p[0] + (p[1] << 8);
259 ret |= checkclnum(boot, no, cl, &fat[cl].next);
260 cl++;
261 p += 2;
262 break;
263 default:
264 fat[cl].next = (p[0] + (p[1] << 8)) & 0x0fff;
265 ret |= checkclnum(boot, no, cl, &fat[cl].next);
266 cl++;
267 if (cl >= boot->NumClusters)
268 break;
269 fat[cl].next = ((p[1] >> 4) + (p[2] << 4)) & 0x0fff;
270 ret |= checkclnum(boot, no, cl, &fat[cl].next);
271 cl++;
272 p += 3;
273 break;
274 }
275 }
276
277 free(buffer);
278 *fp = fat;
279 return ret;
280}
281
282/*
283 * Get type of reserved cluster
284 */
285char *
286rsrvdcltype(cl_t cl)
287{
288 if (cl == CLUST_FREE)
289 return "free";
290 if (cl < CLUST_BAD)
291 return "reserved";
292 if (cl > CLUST_BAD)
293 return "as EOF";
294 return "bad";
295}
296
297static int
298clustdiffer(cl_t cl, cl_t *cp1, cl_t *cp2, int fatnum)
299{
300 if (*cp1 == CLUST_FREE || *cp1 >= CLUST_RSRVD) {
301 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
302 if ((*cp1 != CLUST_FREE && *cp1 < CLUST_BAD
303 && *cp2 != CLUST_FREE && *cp2 < CLUST_BAD)
304 || (*cp1 > CLUST_BAD && *cp2 > CLUST_BAD)) {
305 pwarn("Cluster %u is marked %s with different indicators, ",
306 cl, rsrvdcltype(*cp1));
307 if (ask(1, "fix")) {
308 *cp2 = *cp1;
309 return FSFATMOD;
310 }
311 return FSFATAL;
312 }
313 pwarn("Cluster %u is marked %s in FAT 0, %s in FAT %d\n",
314 cl, rsrvdcltype(*cp1), rsrvdcltype(*cp2), fatnum);
315 if (ask(0, "use FAT 0's entry")) {
316 *cp2 = *cp1;
317 return FSFATMOD;
318 }
319 if (ask(0, "use FAT %d's entry", fatnum)) {
320 *cp1 = *cp2;
321 return FSFATMOD;
322 }
323 return FSFATAL;
324 }
325 pwarn("Cluster %u is marked %s in FAT 0, but continues with cluster %u in FAT %d\n",
326 cl, rsrvdcltype(*cp1), *cp2, fatnum);
327 if (ask(0, "Use continuation from FAT %d", fatnum)) {
328 *cp1 = *cp2;
329 return FSFATMOD;
330 }
331 if (ask(0, "Use mark from FAT 0")) {
332 *cp2 = *cp1;
333 return FSFATMOD;
334 }
335 return FSFATAL;
336 }
337 if (*cp2 == CLUST_FREE || *cp2 >= CLUST_RSRVD) {
338 pwarn("Cluster %u continues with cluster %u in FAT 0, but is marked %s in FAT %d\n",
339 cl, *cp1, rsrvdcltype(*cp2), fatnum);
340 if (ask(0, "Use continuation from FAT 0")) {
341 *cp2 = *cp1;
342 return FSFATMOD;
343 }
344 if (ask(0, "Use mark from FAT %d", fatnum)) {
345 *cp1 = *cp2;
346 return FSFATMOD;
347 }
348 return FSERROR;
349 }
350 pwarn("Cluster %u continues with cluster %u in FAT 0, but with cluster %u in FAT %d\n",
351 cl, *cp1, *cp2, fatnum);
352 if (ask(0, "Use continuation from FAT 0")) {
353 *cp2 = *cp1;
354 return FSFATMOD;
355 }
356 if (ask(0, "Use continuation from FAT %d", fatnum)) {
357 *cp1 = *cp2;
358 return FSFATMOD;
359 }
360 return FSERROR;
361}
362
363/*
364 * Compare two FAT copies in memory. Resolve any conflicts and merge them
365 * into the first one.
366 */
367int
368comparefat(struct bootblock *boot, struct fatEntry *first,
369 struct fatEntry *second, int fatnum)
370{
371 cl_t cl;
372 int ret = FSOK;
373
374 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++)
375 if (first[cl].next != second[cl].next)
376 ret |= clustdiffer(cl, &first[cl].next, &second[cl].next, fatnum);
377 return ret;
378}
379
380void
381clearchain(struct bootblock *boot, struct fatEntry *fat, cl_t head)
382{
383 cl_t p, q;
384
385 for (p = head; p >= CLUST_FIRST && p < boot->NumClusters; p = q) {
386 if (fat[p].head != head)
387 break;
388 q = fat[p].next;
389 fat[p].next = fat[p].head = CLUST_FREE;
390 fat[p].length = 0;
391 }
392}
393
394int
395tryclear(struct bootblock *boot, struct fatEntry *fat, cl_t head, cl_t *trunc)
396{
397 if (ask(0, "Clear chain starting at %u", head)) {
398 clearchain(boot, fat, head);
399 return FSFATMOD;
400 } else if (ask(0, "Truncate")) {
401 *trunc = CLUST_EOF;
402 return FSFATMOD;
403 } else
404 return FSERROR;
405}
406
407/*
408 * Check a complete FAT in-memory for crosslinks
409 */
410int
411checkfat(struct bootblock *boot, struct fatEntry *fat)
412{
413 cl_t head, p, h, n;
414 u_int len;
415 int ret = 0;
416 int conf;
417
418 /*
419 * pass 1: figure out the cluster chains.
420 */
421 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
422 /* find next untravelled chain */
423 if (fat[head].head != 0 /* cluster already belongs to some chain */
424 || fat[head].next == CLUST_FREE
425 || fat[head].next == CLUST_BAD)
426 continue; /* skip it. */
427
428 /* follow the chain and mark all clusters on the way */
429 for (len = 0, p = head;
430 p >= CLUST_FIRST && p < boot->NumClusters;
431 p = fat[p].next) {
432 fat[p].head = head;
433 len++;
434 }
435
436 /* the head record gets the length */
437 fat[head].length = fat[head].next == CLUST_FREE ? 0 : len;
438 }
439
440 /*
441 * pass 2: check for crosslinked chains (we couldn't do this in pass 1 because
442 * we didn't know the real start of the chain then - would have treated partial
443 * chains as interlinked with their main chain)
444 */
445 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
446 /* find next untravelled chain */
447 if (fat[head].head != head)
448 continue;
449
450 /* follow the chain to its end (hopefully) */
451 for (p = head;
452 (n = fat[p].next) >= CLUST_FIRST && n < boot->NumClusters;
453 p = n)
454 if (fat[n].head != head)
455 break;
456 if (n >= CLUST_EOFS)
457 continue;
458
459 if (n == CLUST_FREE || n >= CLUST_RSRVD) {
460 pwarn("Cluster chain starting at %u ends with cluster marked %s\n",
461 head, rsrvdcltype(n));
462 ret |= tryclear(boot, fat, head, &fat[p].next);
463 continue;
464 }
465 if (n < CLUST_FIRST || n >= boot->NumClusters) {
466 pwarn("Cluster chain starting at %u ends with cluster out of range (%u)\n",
467 head, n);
468 ret |= tryclear(boot, fat, head, &fat[p].next);
469 continue;
470 }
471 pwarn("Cluster chains starting at %u and %u are linked at cluster %u\n",
472 head, fat[n].head, n);
473 conf = tryclear(boot, fat, head, &fat[p].next);
474 if (ask(0, "Clear chain starting at %u", h = fat[n].head)) {
475 if (conf == FSERROR) {
476 /*
477 * Transfer the common chain to the one not cleared above.
478 */
479 for (p = n;
480 p >= CLUST_FIRST && p < boot->NumClusters;
481 p = fat[p].next) {
482 if (h != fat[p].head) {
483 /*
484 * Have to reexamine this chain.
485 */
486 head--;
487 break;
488 }
489 fat[p].head = head;
490 }
491 }
492 clearchain(boot, fat, h);
493 conf |= FSFATMOD;
494 }
495 ret |= conf;
496 }
497
498 return ret;
499}
500
501/*
502 * Write out FATs encoding them from the internal format
503 */
504int
505writefat(int fs, struct bootblock *boot, struct fatEntry *fat, int correct_fat)
506{
507 u_char *buffer, *p;
508 cl_t cl;
509 int i;
510 u_int32_t fatsz;
511 off_t off;
512 int ret = FSOK;
513
514 buffer = malloc(fatsz = boot->FATsecs * boot->BytesPerSec);
515 if (buffer == NULL) {
516 perror("No space for FAT");
517 return FSFATAL;
518 }
519 memset(buffer, 0, fatsz);
520 boot->NumFree = 0;
521 p = buffer;
522 if (correct_fat) {
523 *p++ = (u_char)boot->Media;
524 *p++ = 0xff;
525 *p++ = 0xff;
526 switch (boot->ClustMask) {
527 case CLUST16_MASK:
528 *p++ = 0xff;
529 break;
530 case CLUST32_MASK:
531 *p++ = 0x0f;
532 *p++ = 0xff;
533 *p++ = 0xff;
534 *p++ = 0xff;
535 *p++ = 0x0f;
536 break;
537 }
538 } else {
539 /* use same FAT signature as the old FAT has */
540 int count;
541 u_char *old_fat;
542
543 switch (boot->ClustMask) {
544 case CLUST32_MASK:
545 count = 8;
546 break;
547 case CLUST16_MASK:
548 count = 4;
549 break;
550 default:
551 count = 3;
552 break;
553 }
554
555 if (!_readfat(fs, boot, boot->ValidFat >= 0 ? boot->ValidFat :0,
556 &old_fat)) {
557 free(buffer);
558 return FSFATAL;
559 }
560
561 memcpy(p, old_fat, count);
562 free(old_fat);
563 p += count;
564 }
565
566 for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++) {
567 switch (boot->ClustMask) {
568 case CLUST32_MASK:
569 if (fat[cl].next == CLUST_FREE)
570 boot->NumFree++;
571 *p++ = (u_char)fat[cl].next;
572 *p++ = (u_char)(fat[cl].next >> 8);
573 *p++ = (u_char)(fat[cl].next >> 16);
574 *p &= 0xf0;
575 *p++ |= (fat[cl].next >> 24)&0x0f;
576 break;
577 case CLUST16_MASK:
578 if (fat[cl].next == CLUST_FREE)
579 boot->NumFree++;
580 *p++ = (u_char)fat[cl].next;
581 *p++ = (u_char)(fat[cl].next >> 8);
582 break;
583 default:
584 if (fat[cl].next == CLUST_FREE)
585 boot->NumFree++;
586 if (cl + 1 < boot->NumClusters
587 && fat[cl + 1].next == CLUST_FREE)
588 boot->NumFree++;
589 *p++ = (u_char)fat[cl].next;
590 *p++ = (u_char)((fat[cl].next >> 8) & 0xf)
591 |(u_char)(fat[cl+1].next << 4);
592 *p++ = (u_char)(fat[++cl].next >> 4);
593 break;
594 }
595 }
596 for (i = 0; i < boot->FATs; i++) {
597 off = boot->ResSectors + i * boot->FATsecs;
598 off *= boot->BytesPerSec;
599 if (lseek(fs, off, SEEK_SET) != off
600 || write(fs, buffer, fatsz) != fatsz) {
601 perror("Unable to write FAT");
602 ret = FSFATAL; /* Return immediately? XXX */
603 }
604 }
605 free(buffer);
606 return ret;
607}
608
609/*
610 * Check a complete in-memory FAT for lost cluster chains
611 */
612int
613checklost(int dosfs, struct bootblock *boot, struct fatEntry *fat)
614{
615 cl_t head;
616 int mod = FSOK;
617 int ret;
618
619 for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
620 /* find next untravelled chain */
621 if (fat[head].head != head
622 || fat[head].next == CLUST_FREE
623 || (fat[head].next >= CLUST_RSRVD
624 && fat[head].next < CLUST_EOFS)
625 || (fat[head].flags & FAT_USED))
626 continue;
627
628 pwarn("Lost cluster chain at cluster %u\n%d Cluster(s) lost\n",
629 head, fat[head].length);
630 mod |= ret = reconnect(dosfs, boot, fat, head);
631 if (mod & FSFATAL)
632 break;
633 if (ret == FSERROR && ask(0, "Clear")) {
634 clearchain(boot, fat, head);
635 mod |= FSFATMOD;
636 }
637 }
638 finishlf();
639
640 if (boot->FSInfo) {
641 ret = 0;
642 if (boot->FSFree != boot->NumFree) {
643 pwarn("Free space in FSInfo block (%d) not correct (%d)\n",
644 boot->FSFree, boot->NumFree);
645 if (ask(1, "fix")) {
646 boot->FSFree = boot->NumFree;
647 ret = 1;
648 }
649 }
650 if (boot->NumFree && fat[boot->FSNext].next != CLUST_FREE) {
651 pwarn("Next free cluster in FSInfo block (%u) not free\n",
652 boot->FSNext);
653 if (ask(1, "fix"))
654 for (head = CLUST_FIRST; head < boot->NumClusters; head++)
655 if (fat[head].next == CLUST_FREE) {
656 boot->FSNext = head;
657 ret = 1;
658 break;
659 }
660 }
661 if (ret)
662 mod |= writefsinfo(dosfs, boot);
663 }
664
665 return mod;
666}