Home | History | Annotate | Line # | Download | only in fsck_msdos
fat.c revision 1.1
      1  1.1  ws /*	$NetBSD: fat.c,v 1.1 1996/05/14 17:39:34 ws Exp $	*/
      2  1.1  ws 
      3  1.1  ws /*
      4  1.1  ws  * Copyright (C) 1995, 1996 Wolfgang Solfrank
      5  1.1  ws  * Copyright (c) 1995 Martin Husemann
      6  1.1  ws  *
      7  1.1  ws  * Redistribution and use in source and binary forms, with or without
      8  1.1  ws  * modification, are permitted provided that the following conditions
      9  1.1  ws  * are met:
     10  1.1  ws  * 1. Redistributions of source code must retain the above copyright
     11  1.1  ws  *    notice, this list of conditions and the following disclaimer.
     12  1.1  ws  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  ws  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  ws  *    documentation and/or other materials provided with the distribution.
     15  1.1  ws  * 3. All advertising materials mentioning features or use of this software
     16  1.1  ws  *    must display the following acknowledgement:
     17  1.1  ws  *	This product includes software developed by Martin Husemann
     18  1.1  ws  *	and Wolfgang Solfrank.
     19  1.1  ws  * 4. Neither the name of the University nor the names of its contributors
     20  1.1  ws  *    may be used to endorse or promote products derived from this software
     21  1.1  ws  *    without specific prior written permission.
     22  1.1  ws  *
     23  1.1  ws  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     24  1.1  ws  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  1.1  ws  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  1.1  ws  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  1.1  ws  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  1.1  ws  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  1.1  ws  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  1.1  ws  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  1.1  ws  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  1.1  ws  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  1.1  ws  */
     34  1.1  ws 
     35  1.1  ws 
     36  1.1  ws #ifndef lint
     37  1.1  ws static char rcsid[] = "$NetBSD: fat.c,v 1.1 1996/05/14 17:39:34 ws Exp $";
     38  1.1  ws #endif /* not lint */
     39  1.1  ws 
     40  1.1  ws #include <stdlib.h>
     41  1.1  ws #include <string.h>
     42  1.1  ws #include <ctype.h>
     43  1.1  ws #include <stdio.h>
     44  1.1  ws #include <unistd.h>
     45  1.1  ws 
     46  1.1  ws #include "ext.h"
     47  1.1  ws 
     48  1.1  ws /*
     49  1.1  ws  * Check a cluster number for valid value
     50  1.1  ws  */
     51  1.1  ws static int
     52  1.1  ws checkclnum(boot, fat, cl, next)
     53  1.1  ws 	struct bootblock *boot;
     54  1.1  ws 	int fat;
     55  1.1  ws 	cl_t cl;
     56  1.1  ws 	cl_t *next;
     57  1.1  ws {
     58  1.1  ws 	if (!boot->Is16BitFat && *next >= (CLUST_RSRVD&0xfff))
     59  1.1  ws 		*next |= 0xf000;
     60  1.1  ws 	if (*next == CLUST_FREE) {
     61  1.1  ws 		boot->NumFree++;
     62  1.1  ws 		return FSOK;
     63  1.1  ws 	}
     64  1.1  ws 	if (*next < CLUST_FIRST
     65  1.1  ws 	    || (*next >= boot->NumClusters && *next < CLUST_EOFS)) {
     66  1.1  ws 		pwarn("Cluster %d in FAT %d continues with %s cluster number %d\n",
     67  1.1  ws 		      cl, fat,
     68  1.1  ws 		      *next < CLUST_RSRVD ? "out of range" : "reserved",
     69  1.1  ws 		      *next);
     70  1.1  ws 		if (ask(0, "Truncate")) {
     71  1.1  ws 			*next = CLUST_EOF;
     72  1.1  ws 			return FSFATMOD;
     73  1.1  ws 		}
     74  1.1  ws 		return FSERROR;
     75  1.1  ws 	}
     76  1.1  ws 	return FSOK;
     77  1.1  ws }
     78  1.1  ws 
     79  1.1  ws /*
     80  1.1  ws  * Read a FAT and decode it into internal format
     81  1.1  ws  */
     82  1.1  ws int
     83  1.1  ws readfat(fs, boot, no, fp)
     84  1.1  ws 	int fs;
     85  1.1  ws 	struct bootblock *boot;
     86  1.1  ws 	int no;
     87  1.1  ws 	struct fatEntry **fp;
     88  1.1  ws {
     89  1.1  ws 	struct fatEntry *fat;
     90  1.1  ws 	u_char *buffer, *p;
     91  1.1  ws 	cl_t cl;
     92  1.1  ws 	off_t off;
     93  1.1  ws 	int size;
     94  1.1  ws 	int ret = FSOK;
     95  1.1  ws 
     96  1.1  ws 	boot->NumFree = 0;
     97  1.1  ws 	fat = malloc(sizeof(struct fatEntry) * boot->NumClusters);
     98  1.1  ws 	buffer = malloc(boot->FATsecs * boot->BytesPerSec);
     99  1.1  ws 	if (fat == NULL || buffer == NULL) {
    100  1.1  ws 		perror("No space for FAT");
    101  1.1  ws 		if (fat)
    102  1.1  ws 			free(fat);
    103  1.1  ws 		return FSFATAL;
    104  1.1  ws 	}
    105  1.1  ws 
    106  1.1  ws 	memset(fat, 0, sizeof(struct fatEntry) * boot->NumClusters);
    107  1.1  ws 
    108  1.1  ws 	off = boot->ResSectors + no * boot->FATsecs;
    109  1.1  ws 	off *= boot->BytesPerSec;
    110  1.1  ws 
    111  1.1  ws 	if (lseek(fs, off, SEEK_SET) != off) {
    112  1.1  ws 		perror("Unable to read FAT");
    113  1.1  ws 		free(buffer);
    114  1.1  ws 		free(fat);
    115  1.1  ws 		return FSFATAL;
    116  1.1  ws 	}
    117  1.1  ws 
    118  1.1  ws 	if ((size = read(fs, buffer, boot->FATsecs * boot->BytesPerSec))
    119  1.1  ws 	    != boot->FATsecs * boot->BytesPerSec) {
    120  1.1  ws 		if (size < 0)
    121  1.1  ws 			perror("Unable to read FAT");
    122  1.1  ws 		else
    123  1.1  ws 			pfatal("Short FAT?");
    124  1.1  ws 		free(buffer);
    125  1.1  ws 		free(fat);
    126  1.1  ws 		return FSFATAL;
    127  1.1  ws 	}
    128  1.1  ws 
    129  1.1  ws 	/*
    130  1.1  ws 	 * Remember start of FAT to allow keeping it in write_fat.
    131  1.1  ws 	 */
    132  1.1  ws 	fat[0].length = buffer[0]|(buffer[1] << 8)|(buffer[2] << 16);
    133  1.1  ws 	if (boot->Is16BitFat)
    134  1.1  ws 		fat[0].length |= buffer[3] << 24;
    135  1.1  ws 	if (buffer[1] != 0xff || buffer[2] != 0xff
    136  1.1  ws 	    || (boot->Is16BitFat && buffer[3] != 0xff)) {
    137  1.1  ws 		char *msg = boot->Is16BitFat
    138  1.1  ws 			? "FAT starts with odd byte sequence (%02x%02x%02x%02x)\n"
    139  1.1  ws 			: "FAT starts with odd byte sequence (%02x%02x%02x)\n";
    140  1.1  ws 		pwarn(msg, buffer[0], buffer[1], buffer[2], buffer[3]);
    141  1.1  ws 		if (ask(1, "Correct")) {
    142  1.1  ws 			fat[0].length = boot->Media|0xffffff;
    143  1.1  ws 			ret |= FSFATMOD;
    144  1.1  ws 		}
    145  1.1  ws 	}
    146  1.1  ws 	p = buffer + (boot->Is16BitFat ? 4 : 3);
    147  1.1  ws 	for (cl = CLUST_FIRST; cl < boot->NumClusters;) {
    148  1.1  ws 		if (boot->Is16BitFat) {
    149  1.1  ws 			fat[cl].next = p[0] + (p[1] << 8);
    150  1.1  ws 			ret |= checkclnum(boot, no, cl, &fat[cl].next);
    151  1.1  ws 			cl++;
    152  1.1  ws 			p += 2;
    153  1.1  ws 		} else {
    154  1.1  ws 			fat[cl].next = (p[0] + (p[1] << 8)) & 0x0fff;
    155  1.1  ws 			ret |= checkclnum(boot, no, cl, &fat[cl].next);
    156  1.1  ws 			cl++;
    157  1.1  ws 			if (cl >= boot->NumClusters)
    158  1.1  ws 				break;
    159  1.1  ws 			fat[cl].next = ((p[1] >> 4) + (p[2] << 4)) & 0x0fff;
    160  1.1  ws 			ret |= checkclnum(boot, no, cl, &fat[cl].next);
    161  1.1  ws 			cl++;
    162  1.1  ws 			p += 3;
    163  1.1  ws 		}
    164  1.1  ws 	}
    165  1.1  ws 
    166  1.1  ws 	free(buffer);
    167  1.1  ws 	*fp = fat;
    168  1.1  ws 	return ret;
    169  1.1  ws }
    170  1.1  ws 
    171  1.1  ws /*
    172  1.1  ws  * Get type of reserved cluster
    173  1.1  ws  */
    174  1.1  ws char *
    175  1.1  ws rsrvdcltype(cl)
    176  1.1  ws 	cl_t cl;
    177  1.1  ws {
    178  1.1  ws 	if (cl < CLUST_BAD)
    179  1.1  ws 		return "reserved";
    180  1.1  ws 	if (cl > CLUST_BAD)
    181  1.1  ws 		return "as EOF";
    182  1.1  ws 	return "bad";
    183  1.1  ws }
    184  1.1  ws 
    185  1.1  ws static int
    186  1.1  ws clustdiffer(cl, cp1, cp2, fatnum)
    187  1.1  ws 	cl_t cl;
    188  1.1  ws 	cl_t *cp1;
    189  1.1  ws 	cl_t *cp2;
    190  1.1  ws 	int fatnum;
    191  1.1  ws {
    192  1.1  ws 	if (*cp1 >= CLUST_RSRVD) {
    193  1.1  ws 		if (*cp2 >= CLUST_RSRVD) {
    194  1.1  ws 			if ((*cp1 < CLUST_BAD && *cp2 < CLUST_BAD)
    195  1.1  ws 			    || (*cp1 > CLUST_BAD && *cp2 > CLUST_BAD)) {
    196  1.1  ws 				pwarn("Cluster %d is marked %s with different indicators, ",
    197  1.1  ws 				      cl, rsrvdcltype(*cp1));
    198  1.1  ws 				if (ask(1, "fix")) {
    199  1.1  ws 					*cp2 = *cp1;
    200  1.1  ws 					return FSFATMOD;
    201  1.1  ws 				}
    202  1.1  ws 				return FSFATAL;
    203  1.1  ws 			}
    204  1.1  ws 			pwarn("Cluster %d is marked %s in FAT 1, %s in FAT %d\n",
    205  1.1  ws 			      cl, rsrvdcltype(*cp1), rsrvdcltype(*cp2), fatnum);
    206  1.1  ws 			if (ask(0, "use FAT #1's entry")) {
    207  1.1  ws 				*cp2 = *cp1;
    208  1.1  ws 				return FSFATMOD;
    209  1.1  ws 			}
    210  1.1  ws 			if (ask(0, "use FAT #%d's entry", fatnum)) {
    211  1.1  ws 				*cp1 = *cp2;
    212  1.1  ws 				return FSFATMOD;
    213  1.1  ws 			}
    214  1.1  ws 			return FSFATAL;
    215  1.1  ws 		}
    216  1.1  ws 		pwarn("Cluster %d is marked %s in FAT 1, but continues with cluster %d in FAT %d\n",
    217  1.1  ws 		      cl, rsrvdcltype(*cp1), *cp2, fatnum);
    218  1.1  ws 		if (ask(0, "Use continuation from FAT %d", fatnum)) {
    219  1.1  ws 			*cp1 = *cp2;
    220  1.1  ws 			return FSFATMOD;
    221  1.1  ws 		}
    222  1.1  ws 		if (ask(0, "Use mark from FAT 1")) {
    223  1.1  ws 			*cp2 = *cp1;
    224  1.1  ws 			return FSFATMOD;
    225  1.1  ws 		}
    226  1.1  ws 		return FSFATAL;
    227  1.1  ws 	}
    228  1.1  ws 	if (*cp2 >= CLUST_RSRVD) {
    229  1.1  ws 		pwarn("Cluster %d continues with cluster %d in FAT 1, but is marked %s in FAT %d\n",
    230  1.1  ws 		      cl, *cp1, rsrvdcltype(*cp2), fatnum);
    231  1.1  ws 		if (ask(0, "Use continuation from FAT 1")) {
    232  1.1  ws 			*cp2 = *cp1;
    233  1.1  ws 			return FSFATMOD;
    234  1.1  ws 		}
    235  1.1  ws 		if (ask(0, "Use mark from FAT %d", fatnum)) {
    236  1.1  ws 			*cp1 = *cp2;
    237  1.1  ws 			return FSFATMOD;
    238  1.1  ws 		}
    239  1.1  ws 		return FSERROR;
    240  1.1  ws 	}
    241  1.1  ws 	pwarn("Cluster %d continues with cluster %d in FAT 1, but with cluster %d in FAT %d\n",
    242  1.1  ws 	      cl, *cp1, *cp2, fatnum);
    243  1.1  ws 	if (ask(0, "Use continuation from FAT 1")) {
    244  1.1  ws 		*cp2 = *cp1;
    245  1.1  ws 		return FSFATMOD;
    246  1.1  ws 	}
    247  1.1  ws 	if (ask(0, "Use continuation from FAT %d", fatnum)) {
    248  1.1  ws 		*cp1 = *cp2;
    249  1.1  ws 		return FSFATMOD;
    250  1.1  ws 	}
    251  1.1  ws 	return FSERROR;
    252  1.1  ws }
    253  1.1  ws 
    254  1.1  ws /*
    255  1.1  ws  * Compare two FAT copies in memory. Resolve any conflicts and merge them
    256  1.1  ws  * into the first one.
    257  1.1  ws  */
    258  1.1  ws int
    259  1.1  ws comparefat(boot, first, second, fatnum)
    260  1.1  ws 	struct bootblock *boot;
    261  1.1  ws 	struct fatEntry *first;
    262  1.1  ws 	struct fatEntry *second;
    263  1.1  ws 	int fatnum;
    264  1.1  ws {
    265  1.1  ws 	cl_t cl;
    266  1.1  ws 	int ret = FSOK;
    267  1.1  ws 
    268  1.1  ws 	if (first[0].next != second[0].next) {
    269  1.1  ws 		pwarn("Media bytes in cluster 1(%02x) and %d(%02x) differ\n",
    270  1.1  ws 		      first[0].next, fatnum, second[0].next);
    271  1.1  ws 		if (ask(1, "Use media byte from FAT 1")) {
    272  1.1  ws 			second[0].next = first[0].next;
    273  1.1  ws 			ret |= FSFATMOD;
    274  1.1  ws 		} else if (ask(0, "Use media byte from FAT %d", fatnum)) {
    275  1.1  ws 			first[0].next = second[0].next;
    276  1.1  ws 			ret |= FSFATMOD;
    277  1.1  ws 		} else
    278  1.1  ws 			ret |= FSERROR;
    279  1.1  ws 	}
    280  1.1  ws 	for (cl = CLUST_FIRST; cl < boot->NumClusters; cl++)
    281  1.1  ws 		if (first[cl].next != second[cl].next)
    282  1.1  ws 			ret |= clustdiffer(cl, &first[cl].next, &second[cl].next, fatnum);
    283  1.1  ws 	return ret;
    284  1.1  ws }
    285  1.1  ws 
    286  1.1  ws void
    287  1.1  ws clearchain(boot, fat, head)
    288  1.1  ws 	struct bootblock *boot;
    289  1.1  ws 	struct fatEntry *fat;
    290  1.1  ws 	cl_t head;
    291  1.1  ws {
    292  1.1  ws 	cl_t p, q;
    293  1.1  ws 
    294  1.1  ws 	for (p = head; p >= CLUST_FIRST && p < boot->NumClusters; p = q) {
    295  1.1  ws 		if (fat[p].head != head)
    296  1.1  ws 			break;
    297  1.1  ws 		q = fat[p].next;
    298  1.1  ws 		fat[p].next = fat[p].head = CLUST_FREE;
    299  1.1  ws 		fat[p].length = 0;
    300  1.1  ws 	}
    301  1.1  ws }
    302  1.1  ws 
    303  1.1  ws /*
    304  1.1  ws  * Check a complete FAT in-memory for crosslinks
    305  1.1  ws  */
    306  1.1  ws int
    307  1.1  ws checkfat(boot, fat)
    308  1.1  ws 	struct bootblock *boot;
    309  1.1  ws 	struct fatEntry *fat;
    310  1.1  ws {
    311  1.1  ws 	cl_t head, p, h;
    312  1.1  ws 	u_int len;
    313  1.1  ws 	int ret = 0;
    314  1.1  ws 	int conf;
    315  1.1  ws 
    316  1.1  ws 	/*
    317  1.1  ws 	 * pass 1: figure out the cluster chains.
    318  1.1  ws 	 */
    319  1.1  ws 	for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
    320  1.1  ws 		/* find next untraveled chain */
    321  1.1  ws 		if (fat[head].head != 0		/* cluster already belongs to some chain*/
    322  1.1  ws 		    || fat[head].next == CLUST_FREE)
    323  1.1  ws 			continue;		/* skip it. */
    324  1.1  ws 
    325  1.1  ws 		/* follow the chain and mark all clusters on the way */
    326  1.1  ws 		for (len = 0, p = head;
    327  1.1  ws 		     p >= CLUST_FIRST && p < boot->NumClusters;
    328  1.1  ws 		     p = fat[p].next) {
    329  1.1  ws 			fat[p].head = head;
    330  1.1  ws 			len++;
    331  1.1  ws 		}
    332  1.1  ws 
    333  1.1  ws 		/* the head record gets the length */
    334  1.1  ws 		fat[head].length = len;
    335  1.1  ws 	}
    336  1.1  ws 
    337  1.1  ws 	/*
    338  1.1  ws 	 * pass 2: check for crosslinked chains (we couldn't do this in pass 1 because
    339  1.1  ws 	 * we didn't know the real start of the chain then - would have treated partial
    340  1.1  ws 	 * chains as interlinked with their main chain)
    341  1.1  ws 	 */
    342  1.1  ws 	for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
    343  1.1  ws 		/* find next untraveled chain */
    344  1.1  ws 		if (fat[head].head != head)
    345  1.1  ws 			continue;
    346  1.1  ws 
    347  1.1  ws 		/* follow the chain to its end (hopefully) */
    348  1.1  ws 		for (p = head;
    349  1.1  ws 		     fat[p].next >= CLUST_FIRST && fat[p].next < boot->NumClusters;
    350  1.1  ws 		     p = fat[p].next)
    351  1.1  ws 			if (fat[fat[p].next].head != head)
    352  1.1  ws 				break;
    353  1.1  ws 		if (fat[p].next >= CLUST_EOFS)
    354  1.1  ws 			continue;
    355  1.1  ws 
    356  1.1  ws 		if (fat[p].next == 0) {
    357  1.1  ws 			pwarn("Cluster chain starting at %d ends with free cluster\n", head);
    358  1.1  ws 			if (ask(0, "Clear chain starting at %d", head)) {
    359  1.1  ws 				clearchain(boot, fat, head);
    360  1.1  ws 				ret |= FSFATMOD;
    361  1.1  ws 			} else
    362  1.1  ws 				ret |= FSERROR;
    363  1.1  ws 			continue;
    364  1.1  ws 		}
    365  1.1  ws 		if (fat[p].next >= CLUST_RSRVD) {
    366  1.1  ws 			pwarn("Cluster chain starting at %d ends with cluster marked %s\n",
    367  1.1  ws 			      head, rsrvdcltype(fat[p].next));
    368  1.1  ws 			if (ask(0, "Clear chain starting at %d", head)) {
    369  1.1  ws 				clearchain(boot, fat, head);
    370  1.1  ws 				ret |= FSFATMOD;
    371  1.1  ws 			} else
    372  1.1  ws 				ret |= FSERROR;
    373  1.1  ws 			continue;
    374  1.1  ws 		}
    375  1.1  ws 		if (fat[p].next < CLUST_FIRST || fat[p].next >= boot->NumClusters) {
    376  1.1  ws 			pwarn("Cluster chain starting at %d ends with cluster out of range (%d)\n",
    377  1.1  ws 			      head, fat[p].next);
    378  1.1  ws 			if (ask(0, "Clear chain starting at %d", head)) {
    379  1.1  ws 				clearchain(boot, fat, head);
    380  1.1  ws 				ret |= FSFATMOD;
    381  1.1  ws 			} else
    382  1.1  ws 				ret |= FSERROR;
    383  1.1  ws 		}
    384  1.1  ws 		pwarn("Cluster chains starting at %d and %d are linked at cluster %d\n",
    385  1.1  ws 		      head, fat[p].head, p);
    386  1.1  ws 		conf = FSERROR;
    387  1.1  ws 		if (ask(0, "Clear chain starting at %d", head)) {
    388  1.1  ws 			clearchain(boot, fat, head);
    389  1.1  ws 			conf = FSFATMOD;
    390  1.1  ws 		}
    391  1.1  ws 		if (ask(0, "Clear chain starting at %d", h = fat[p].head)) {
    392  1.1  ws 			if (conf == FSERROR) {
    393  1.1  ws 				/*
    394  1.1  ws 				 * Transfer the common chain to the one not cleared above.
    395  1.1  ws 				 */
    396  1.1  ws 				for (; p >= CLUST_FIRST && p < boot->NumClusters;
    397  1.1  ws 				     p = fat[p].next) {
    398  1.1  ws 					if (h != fat[p].head) {
    399  1.1  ws 						/*
    400  1.1  ws 						 * Have to reexamine this chain.
    401  1.1  ws 						 */
    402  1.1  ws 						head--;
    403  1.1  ws 						break;
    404  1.1  ws 					}
    405  1.1  ws 					fat[p].head = head;
    406  1.1  ws 				}
    407  1.1  ws 			}
    408  1.1  ws 			clearchain(boot, fat, h);
    409  1.1  ws 			conf |= FSFATMOD;
    410  1.1  ws 		}
    411  1.1  ws 		ret |= conf;
    412  1.1  ws 	}
    413  1.1  ws 
    414  1.1  ws 	return ret;
    415  1.1  ws }
    416  1.1  ws 
    417  1.1  ws /*
    418  1.1  ws  * Write out FATs encoding them from the internal format
    419  1.1  ws  */
    420  1.1  ws int
    421  1.1  ws writefat(fs, boot, fat)
    422  1.1  ws 	int fs;
    423  1.1  ws 	struct bootblock *boot;
    424  1.1  ws 	struct fatEntry *fat;
    425  1.1  ws {
    426  1.1  ws 	u_char *buffer, *p;
    427  1.1  ws 	cl_t cl;
    428  1.1  ws 	int i;
    429  1.1  ws 	u_int32_t fatsz;
    430  1.1  ws 	off_t off;
    431  1.1  ws 	int ret = FSOK;
    432  1.1  ws 
    433  1.1  ws 	buffer = malloc(fatsz = boot->FATsecs * boot->BytesPerSec);
    434  1.1  ws 	if (buffer == NULL) {
    435  1.1  ws 		perror("No space for FAT");
    436  1.1  ws 		return FSFATAL;
    437  1.1  ws 	}
    438  1.1  ws 	memset(buffer, 0, fatsz);
    439  1.1  ws 	boot->NumFree = 0;
    440  1.1  ws 	buffer[0] = (u_char)fat[0].length;
    441  1.1  ws 	buffer[1] = (u_char)(fat[0].length >> 8);
    442  1.1  ws 	if (boot->Is16BitFat)
    443  1.1  ws 		buffer[3] = (u_char)(fat[0].length >> 24);
    444  1.1  ws 	for (cl = CLUST_FIRST, p = buffer; cl < boot->NumClusters;) {
    445  1.1  ws 		if (boot->Is16BitFat) {
    446  1.1  ws 			p[0] = (u_char)fat[cl].next;
    447  1.1  ws 			if (fat[cl].next == CLUST_FREE)
    448  1.1  ws 				boot->NumFree++;
    449  1.1  ws 			p[1] = (u_char)(fat[cl++].next >> 8);
    450  1.1  ws 			p += 2;
    451  1.1  ws 		} else {
    452  1.1  ws 			if (fat[cl].next == CLUST_FREE)
    453  1.1  ws 				boot->NumFree++;
    454  1.1  ws 			if (cl + 1 < boot->NumClusters
    455  1.1  ws 			    && fat[cl + 1].next == CLUST_FREE)
    456  1.1  ws 				boot->NumFree++;
    457  1.1  ws 			p[0] = (u_char)fat[cl].next;
    458  1.1  ws 			p[1] = (u_char)((fat[cl].next >> 8) & 0xf)
    459  1.1  ws 				|(u_char)(fat[cl+1].next << 4);
    460  1.1  ws 			p[2] = (u_char)(fat[cl++].next >> 8);
    461  1.1  ws 			p += 3;
    462  1.1  ws 		}
    463  1.1  ws 	}
    464  1.1  ws 	for (i = 0; i < boot->FATs; i++) {
    465  1.1  ws 		off = boot->ResSectors + i * boot->FATsecs;
    466  1.1  ws 		off *= boot->BytesPerSec;
    467  1.1  ws 		if (lseek(fs, off, SEEK_SET) != off
    468  1.1  ws 		    || write(fs, buffer, fatsz) != fatsz) {
    469  1.1  ws 			perror("Unable to write FAT");
    470  1.1  ws 			ret = FSFATAL; /* Return immediately?		XXX */
    471  1.1  ws 		}
    472  1.1  ws 	}
    473  1.1  ws 	free(buffer);
    474  1.1  ws 	return ret;
    475  1.1  ws }
    476  1.1  ws 
    477  1.1  ws /*
    478  1.1  ws  * Check a complete in-memory FAT for lost cluster chains
    479  1.1  ws  */
    480  1.1  ws int
    481  1.1  ws checklost(dosfs, boot, fat, rootDir)
    482  1.1  ws 	int dosfs;
    483  1.1  ws 	struct bootblock *boot;
    484  1.1  ws 	struct fatEntry *fat;
    485  1.1  ws 	struct dosDirEntry *rootDir;
    486  1.1  ws {
    487  1.1  ws 	cl_t head;
    488  1.1  ws 	struct dosDirEntry *lfdir;
    489  1.1  ws 	int mod = FSOK;
    490  1.1  ws 
    491  1.1  ws 	for (lfdir = rootDir->child; lfdir; lfdir = lfdir->next) {
    492  1.1  ws 		if (!strcmp(lfdir->name, LOSTDIR))
    493  1.1  ws 			break;
    494  1.1  ws 	}
    495  1.1  ws 	for (head = CLUST_FIRST; head < boot->NumClusters; head++) {
    496  1.1  ws 		/* find next untraveled chain */
    497  1.1  ws 		if (fat[head].head != head
    498  1.1  ws 		    || fat[head].next == CLUST_FREE
    499  1.1  ws 		    || (fat[head].next >= CLUST_RSRVD
    500  1.1  ws 			&& fat[head].next < CLUST_EOFS))
    501  1.1  ws 			continue;
    502  1.1  ws 
    503  1.1  ws 		if (fat[head].dirp == NULL) {
    504  1.1  ws 			pwarn("Lost cluster chain at cluster 0x%04x\n%d Cluster(s) lost\n",
    505  1.1  ws 			      head, fat[head].length);
    506  1.1  ws 			mod |= reconnect(dosfs, boot, fat, head, lfdir);
    507  1.1  ws 			if (mod&FSFATAL)
    508  1.1  ws 				break;
    509  1.1  ws 		}
    510  1.1  ws 	}
    511  1.1  ws 	finishlf();
    512  1.1  ws 
    513  1.1  ws 	return mod;
    514  1.1  ws }
    515