xref: /onnv-gate/usr/src/cmd/fs.d/pcfs/fsck/fat.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * fsck_pcfs -- routines for manipulating the FAT.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate #include <stdio.h>
33*0Sstevel@tonic-gate #include <unistd.h>
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <libintl.h>
36*0Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
37*0Sstevel@tonic-gate #include <sys/fs/pc_fs.h>
38*0Sstevel@tonic-gate #include <sys/fs/pc_dir.h>
39*0Sstevel@tonic-gate #include <sys/fs/pc_label.h>
40*0Sstevel@tonic-gate #include "pcfs_common.h"
41*0Sstevel@tonic-gate #include "fsck_pcfs.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate extern	int32_t	BytesPerCluster;
44*0Sstevel@tonic-gate extern	int32_t	TotalClusters;
45*0Sstevel@tonic-gate extern	int32_t	LastCluster;
46*0Sstevel@tonic-gate extern	off64_t	FirstClusterOffset;
47*0Sstevel@tonic-gate extern	off64_t	PartitionOffset;
48*0Sstevel@tonic-gate extern	bpb_t	TheBIOSParameterBlock;
49*0Sstevel@tonic-gate extern	int	ReadOnly;
50*0Sstevel@tonic-gate extern	int	IsFAT32;
51*0Sstevel@tonic-gate extern	int	Verbose;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate static	uchar_t	*TheFAT;
54*0Sstevel@tonic-gate static	int	FATRewriteNeeded = 0;
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate int32_t		FATSize;
57*0Sstevel@tonic-gate short		FATEntrySize;
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate static off64_t
seekFAT(int fd)60*0Sstevel@tonic-gate seekFAT(int fd)
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate 	off64_t seekto;
63*0Sstevel@tonic-gate 	/*
64*0Sstevel@tonic-gate 	 *  The FAT(s) immediately follows the reserved sectors.
65*0Sstevel@tonic-gate 	 */
66*0Sstevel@tonic-gate 	seekto = TheBIOSParameterBlock.bpb.resv_sectors *
67*0Sstevel@tonic-gate 		TheBIOSParameterBlock.bpb.bytes_per_sector + PartitionOffset;
68*0Sstevel@tonic-gate 	return (lseek64(fd, seekto, SEEK_SET));
69*0Sstevel@tonic-gate }
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate void
getFAT(int fd)72*0Sstevel@tonic-gate getFAT(int fd)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate 	ssize_t bytesRead;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	if (TheFAT != NULL) {
77*0Sstevel@tonic-gate 		return;
78*0Sstevel@tonic-gate 	} else if ((TheFAT = (uchar_t *)malloc(FATSize)) == NULL) {
79*0Sstevel@tonic-gate 		mountSanityCheckFails();
80*0Sstevel@tonic-gate 		perror(gettext("No memory for a copy of the FAT"));
81*0Sstevel@tonic-gate 		(void) close(fd);
82*0Sstevel@tonic-gate 		exit(7);
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate 	if (seekFAT(fd) < 0) {
85*0Sstevel@tonic-gate 		mountSanityCheckFails();
86*0Sstevel@tonic-gate 		perror(gettext("Cannot seek to FAT"));
87*0Sstevel@tonic-gate 		(void) close(fd);
88*0Sstevel@tonic-gate 		exit(7);
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 	if (Verbose)
91*0Sstevel@tonic-gate 		(void) fprintf(stderr,
92*0Sstevel@tonic-gate 		    gettext("Reading FAT\n"));
93*0Sstevel@tonic-gate 	if ((bytesRead = read(fd, TheFAT, FATSize)) != FATSize) {
94*0Sstevel@tonic-gate 		mountSanityCheckFails();
95*0Sstevel@tonic-gate 		if (bytesRead < 0) {
96*0Sstevel@tonic-gate 			perror(gettext("Cannot read a FAT"));
97*0Sstevel@tonic-gate 		} else {
98*0Sstevel@tonic-gate 			(void) fprintf(stderr,
99*0Sstevel@tonic-gate 			    gettext("Short read of FAT."));
100*0Sstevel@tonic-gate 		}
101*0Sstevel@tonic-gate 		(void) close(fd);
102*0Sstevel@tonic-gate 		exit(7);
103*0Sstevel@tonic-gate 	}
104*0Sstevel@tonic-gate 	/*
105*0Sstevel@tonic-gate 	 * XXX - might want to read the other copies of the FAT
106*0Sstevel@tonic-gate 	 * for comparison and/or to use if the first one seems hosed.
107*0Sstevel@tonic-gate 	 */
108*0Sstevel@tonic-gate 	if (Verbose) {
109*0Sstevel@tonic-gate 		(void) fprintf(stderr,
110*0Sstevel@tonic-gate 		    gettext("Dump of FAT's first 32 bytes.\n"));
111*0Sstevel@tonic-gate 		header_for_dump();
112*0Sstevel@tonic-gate 		dump_bytes(TheFAT, 32);
113*0Sstevel@tonic-gate 	}
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate void
writeFATMods(int fd)117*0Sstevel@tonic-gate writeFATMods(int fd)
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate 	ssize_t bytesWritten;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	if (TheFAT == NULL) {
122*0Sstevel@tonic-gate 		(void) fprintf(stderr,
123*0Sstevel@tonic-gate 		    gettext("Internal error: No FAT to write\n"));
124*0Sstevel@tonic-gate 		(void) close(fd);
125*0Sstevel@tonic-gate 		exit(11);
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 	if (!FATRewriteNeeded) {
128*0Sstevel@tonic-gate 		if (Verbose) {
129*0Sstevel@tonic-gate 			(void) fprintf(stderr,
130*0Sstevel@tonic-gate 			    gettext("No FAT changes need to be written.\n"));
131*0Sstevel@tonic-gate 		}
132*0Sstevel@tonic-gate 		return;
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 	if (ReadOnly)
135*0Sstevel@tonic-gate 		return;
136*0Sstevel@tonic-gate 	if (Verbose)
137*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Writing FAT\n"));
138*0Sstevel@tonic-gate 	if (seekFAT(fd) < 0) {
139*0Sstevel@tonic-gate 		perror(gettext("Cannot seek to FAT"));
140*0Sstevel@tonic-gate 		(void) close(fd);
141*0Sstevel@tonic-gate 		exit(11);
142*0Sstevel@tonic-gate 	}
143*0Sstevel@tonic-gate 	if ((bytesWritten = write(fd, TheFAT, FATSize)) != FATSize) {
144*0Sstevel@tonic-gate 		if (bytesWritten < 0) {
145*0Sstevel@tonic-gate 			perror(gettext("Cannot write FAT"));
146*0Sstevel@tonic-gate 		} else {
147*0Sstevel@tonic-gate 			(void) fprintf(stderr,
148*0Sstevel@tonic-gate 			    gettext("Short write of FAT."));
149*0Sstevel@tonic-gate 		}
150*0Sstevel@tonic-gate 		(void) close(fd);
151*0Sstevel@tonic-gate 		exit(11);
152*0Sstevel@tonic-gate 	}
153*0Sstevel@tonic-gate 	FATRewriteNeeded = 0;
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate /*
157*0Sstevel@tonic-gate  *  checkFAT32CleanBit()
158*0Sstevel@tonic-gate  *	Return non-zero if the bit indicating proper Windows shutdown has
159*0Sstevel@tonic-gate  *	been set.
160*0Sstevel@tonic-gate  */
161*0Sstevel@tonic-gate int
checkFAT32CleanBit(int fd)162*0Sstevel@tonic-gate checkFAT32CleanBit(int fd)
163*0Sstevel@tonic-gate {
164*0Sstevel@tonic-gate 	getFAT(fd);
165*0Sstevel@tonic-gate 	return (TheFAT[WIN_SHUTDOWN_STATUS_BYTE] & WIN_SHUTDOWN_BIT_MASK);
166*0Sstevel@tonic-gate }
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate static uchar_t *
findClusterEntryInFAT(int32_t currentCluster)169*0Sstevel@tonic-gate findClusterEntryInFAT(int32_t currentCluster)
170*0Sstevel@tonic-gate {
171*0Sstevel@tonic-gate 	int32_t idx;
172*0Sstevel@tonic-gate 	if (FATEntrySize == 32) {
173*0Sstevel@tonic-gate 		idx = currentCluster * 4;
174*0Sstevel@tonic-gate 	} else if (FATEntrySize == 16) {
175*0Sstevel@tonic-gate 		idx = currentCluster * 2;
176*0Sstevel@tonic-gate 	} else {
177*0Sstevel@tonic-gate 		idx = currentCluster + currentCluster/2;
178*0Sstevel@tonic-gate 	}
179*0Sstevel@tonic-gate 	return (TheFAT + idx);
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate /*
183*0Sstevel@tonic-gate  *  {read,write}FATentry
184*0Sstevel@tonic-gate  *	For the 16 and 32 bit FATs these routines are relatively easy
185*0Sstevel@tonic-gate  *	to follow.
186*0Sstevel@tonic-gate  *
187*0Sstevel@tonic-gate  *	12 bit FATs are kind of strange, though.  The magic index for
188*0Sstevel@tonic-gate  *	12 bit FATS computed below, 1.5 * clusterNum, is a
189*0Sstevel@tonic-gate  *	simplification that there are 8 bits in a byte, so you need
190*0Sstevel@tonic-gate  *	1.5 bytes per entry.
191*0Sstevel@tonic-gate  *
192*0Sstevel@tonic-gate  *	It's easiest to think about FAT12 entries in pairs:
193*0Sstevel@tonic-gate  *
194*0Sstevel@tonic-gate  *	---------------------------------------------
195*0Sstevel@tonic-gate  *	| mid1 | low1 | low2 | high1 | high2 | mid2 |
196*0Sstevel@tonic-gate  *	---------------------------------------------
197*0Sstevel@tonic-gate  *
198*0Sstevel@tonic-gate  *	Each box in the diagram represents a nibble (4 bits) of a FAT
199*0Sstevel@tonic-gate  *	entry.  A FAT entry is made up of three nibbles.  So if you
200*0Sstevel@tonic-gate  *	look closely, you'll see that first byte of the pair of
201*0Sstevel@tonic-gate  *	entries contains the low and middle nibbles of the first
202*0Sstevel@tonic-gate  *	entry.  The second byte has the low nibble of the second entry
203*0Sstevel@tonic-gate  *	and the high nibble of the first entry.  Those two bytes alone
204*0Sstevel@tonic-gate  *	are enough to read the first entry.  The second FAT entry is
205*0Sstevel@tonic-gate  *	finished out by the last nibble pair.
206*0Sstevel@tonic-gate  */
207*0Sstevel@tonic-gate int32_t
readFATEntry(int32_t currentCluster)208*0Sstevel@tonic-gate readFATEntry(int32_t currentCluster)
209*0Sstevel@tonic-gate {
210*0Sstevel@tonic-gate 	int32_t value;
211*0Sstevel@tonic-gate 	uchar_t *ep;
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	ep = findClusterEntryInFAT(currentCluster);
214*0Sstevel@tonic-gate 	if (FATEntrySize == 32) {
215*0Sstevel@tonic-gate 		read_32_bits(ep, (uint32_t *)&value);
216*0Sstevel@tonic-gate 	} else if (FATEntrySize == 16) {
217*0Sstevel@tonic-gate 		read_16_bits(ep, (uint32_t *)&value);
218*0Sstevel@tonic-gate 		/*
219*0Sstevel@tonic-gate 		 *  Convert 16 bit entry to 32 bit if we are
220*0Sstevel@tonic-gate 		 *  into the reserved or higher values.
221*0Sstevel@tonic-gate 		 */
222*0Sstevel@tonic-gate 		if (value >= PCF_RESCLUSTER)
223*0Sstevel@tonic-gate 			value |= 0xFFF0000;
224*0Sstevel@tonic-gate 	} else {
225*0Sstevel@tonic-gate 		value = 0;
226*0Sstevel@tonic-gate 		if (currentCluster & 1) {
227*0Sstevel@tonic-gate 			/*
228*0Sstevel@tonic-gate 			 * Odd numbered cluster
229*0Sstevel@tonic-gate 			 */
230*0Sstevel@tonic-gate 			value = (((unsigned int)*ep++ & 0xf0) >> 4);
231*0Sstevel@tonic-gate 			value += (*ep << 4);
232*0Sstevel@tonic-gate 		} else {
233*0Sstevel@tonic-gate 			value = *ep++;
234*0Sstevel@tonic-gate 			value += ((*ep & 0x0f) << 8);
235*0Sstevel@tonic-gate 		}
236*0Sstevel@tonic-gate 		/*
237*0Sstevel@tonic-gate 		 *  Convert 12 bit entry to 32 bit if we are
238*0Sstevel@tonic-gate 		 *  into the reserved or higher values.
239*0Sstevel@tonic-gate 		 */
240*0Sstevel@tonic-gate 		if (value >= PCF_12BCLUSTER)
241*0Sstevel@tonic-gate 			value |= 0xFFFF000;
242*0Sstevel@tonic-gate 	}
243*0Sstevel@tonic-gate 	return (value);
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate void
writeFATEntry(int32_t currentCluster,int32_t value)247*0Sstevel@tonic-gate writeFATEntry(int32_t currentCluster, int32_t value)
248*0Sstevel@tonic-gate {
249*0Sstevel@tonic-gate 	uchar_t *ep;
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	FATRewriteNeeded = 1;
252*0Sstevel@tonic-gate 	ep = findClusterEntryInFAT(currentCluster);
253*0Sstevel@tonic-gate 	if (FATEntrySize == 32) {
254*0Sstevel@tonic-gate 		store_32_bits(&ep, value);
255*0Sstevel@tonic-gate 	} else if (FATEntrySize == 16) {
256*0Sstevel@tonic-gate 		store_16_bits(&ep, value);
257*0Sstevel@tonic-gate 	} else {
258*0Sstevel@tonic-gate 		if (currentCluster & 1) {
259*0Sstevel@tonic-gate 			/*
260*0Sstevel@tonic-gate 			 * Odd numbered cluster
261*0Sstevel@tonic-gate 			 */
262*0Sstevel@tonic-gate 			*ep = (*ep & 0x0f) | ((value << 4) & 0xf0);
263*0Sstevel@tonic-gate 			ep++;
264*0Sstevel@tonic-gate 			*ep = (value >> 4) & 0xff;
265*0Sstevel@tonic-gate 		} else {
266*0Sstevel@tonic-gate 			*ep++ = value & 0xff;
267*0Sstevel@tonic-gate 			*ep = (*ep & 0xf0) | ((value >> 8) & 0x0f);
268*0Sstevel@tonic-gate 		}
269*0Sstevel@tonic-gate 	}
270*0Sstevel@tonic-gate }
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate /*
273*0Sstevel@tonic-gate  * reservedInFAT - Is this cluster marked in the reserved range?
274*0Sstevel@tonic-gate  *	The range from PCF_RESCLUSTER32 to PCF_BADCLUSTER32 - 1,
275*0Sstevel@tonic-gate  *	have been reserved by Microsoft.  No cluster should be
276*0Sstevel@tonic-gate  *	marked with these; they are effectively invalid cluster values.
277*0Sstevel@tonic-gate  */
278*0Sstevel@tonic-gate int
reservedInFAT(int32_t clusterNum)279*0Sstevel@tonic-gate reservedInFAT(int32_t clusterNum)
280*0Sstevel@tonic-gate {
281*0Sstevel@tonic-gate 	int32_t e;
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	e = readFATEntry(clusterNum);
284*0Sstevel@tonic-gate 	return (e >= PCF_RESCLUSTER32 && e < PCF_BADCLUSTER32);
285*0Sstevel@tonic-gate }
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate /*
288*0Sstevel@tonic-gate  *  badInFAT - Is this cluster marked as bad?  I.e., is it inaccessible?
289*0Sstevel@tonic-gate  */
290*0Sstevel@tonic-gate int
badInFAT(int32_t clusterNum)291*0Sstevel@tonic-gate badInFAT(int32_t clusterNum)
292*0Sstevel@tonic-gate {
293*0Sstevel@tonic-gate 	return (readFATEntry(clusterNum) == PCF_BADCLUSTER32);
294*0Sstevel@tonic-gate }
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate /*
297*0Sstevel@tonic-gate  *  lastInFAT - Is this cluster marked as free?  I.e., is it available
298*0Sstevel@tonic-gate  *	for use?
299*0Sstevel@tonic-gate  */
300*0Sstevel@tonic-gate int
freeInFAT(int32_t clusterNum)301*0Sstevel@tonic-gate freeInFAT(int32_t clusterNum)
302*0Sstevel@tonic-gate {
303*0Sstevel@tonic-gate 	return (readFATEntry(clusterNum) == PCF_FREECLUSTER);
304*0Sstevel@tonic-gate }
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate /*
307*0Sstevel@tonic-gate  *  lastInFAT - Is this cluster the last in its cluster chain?
308*0Sstevel@tonic-gate  */
309*0Sstevel@tonic-gate int
lastInFAT(int32_t clusterNum)310*0Sstevel@tonic-gate lastInFAT(int32_t clusterNum)
311*0Sstevel@tonic-gate {
312*0Sstevel@tonic-gate 	return (readFATEntry(clusterNum) == PCF_LASTCLUSTER32);
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate /*
316*0Sstevel@tonic-gate  *  markLastInFAT - Mark this cluster as the last in its cluster chain.
317*0Sstevel@tonic-gate  */
318*0Sstevel@tonic-gate void
markLastInFAT(int32_t clusterNum)319*0Sstevel@tonic-gate markLastInFAT(int32_t clusterNum)
320*0Sstevel@tonic-gate {
321*0Sstevel@tonic-gate 	writeFATEntry(clusterNum, PCF_LASTCLUSTER32);
322*0Sstevel@tonic-gate }
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate void
markFreeInFAT(int32_t clusterNum)325*0Sstevel@tonic-gate markFreeInFAT(int32_t clusterNum)
326*0Sstevel@tonic-gate {
327*0Sstevel@tonic-gate 	writeFATEntry(clusterNum, PCF_FREECLUSTER);
328*0Sstevel@tonic-gate }
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate void
markBadInFAT(int32_t clusterNum)331*0Sstevel@tonic-gate markBadInFAT(int32_t clusterNum)
332*0Sstevel@tonic-gate {
333*0Sstevel@tonic-gate 	writeFATEntry(clusterNum, PCF_BADCLUSTER32);
334*0Sstevel@tonic-gate }
335