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