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