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