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