-
Notifications
You must be signed in to change notification settings - Fork 11
/
bcread.c
303 lines (267 loc) · 10.1 KB
/
bcread.c
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
/*
// Program: Format
// Version: 0.91t
// Written by: Eric Auer
// Copyright: 2004-2005 under the terms of the GNU GPL, Version 2
// Module Name: bcread.c
// Module Description: Read old FAT to find bad clusters
*/
#include "format.h"
#include "floppy.h"
#include "driveio.h"
#include "btstrct.h"
#include "savefs.h"
#include "userint.h" /* Display_Percentage_Formatted */
/* New in 0.91r: returns number of last used sector -ea */
unsigned long BadClustPreserve32(void);
unsigned long BadClustPreserve16(void);
unsigned long BadClustPreserve12(void);
int check_too_bad(unsigned int bad_count); /* new 0.91p */
int check_too_bad(unsigned int bad_count) /* return 1 if too many bad clusters */
{
if (bad_count == MAX_BAD_SECTORS) {
printf(catgets(catalog, 26, 0, "\n *** Too many bad clusters! Do a surface scan after FORMAT! ***\n"));
return 1;
}
return 0;
} /* check_too_bad */
/*
* Use param.bpb values and parse the existing FAT to fill the
* global bad_sector_map table with already known bad sector numbers.
* Used in Safe Quick format (/Q), called from savefs.c ...
* Full format with surface scan (/U) and unsafe Quick format (/Q /U)
* do not use this code. Extra in 0.91r: Returns max. used data sector number.
*/
unsigned long BadClustPreserve(void)
{
if (parameter_block.bpb.bytes_per_sector != 512) {
printf(catgets(catalog, 26, 1, "BadClustPreserve aborted: not 512 bytes/sector!\n"));
return 0xffffffffUL;
}
if (param.fat_type == FAT32) {
return BadClustPreserve32();
} else {
if (param.fat_type == FAT16) {
return BadClustPreserve16();
} else {
return BadClustPreserve12();
}
}
}
unsigned long BadClustPreserve32(void) /* should use multisector read here */
{
unsigned long fatstart = parameter_block.bpb.reserved_sectors;
unsigned long fatsize;
unsigned long cluststart = fatstart; /* where 1st cluster starts */
unsigned long sect, j;
unsigned long countused = 0;
unsigned long countbad = 0;
unsigned long countitems = 0;
unsigned long * buf = (unsigned long *)(&huge_sector_buffer[0]);
unsigned long chunksize = sizeof(huge_sector_buffer_0) >> 9;
unsigned long percentage = 0;
unsigned long last_used = 2;
fatsize = parameter_block.xbpb.fat_size_high;
fatsize <<= 16;
fatsize |= parameter_block.xbpb.fat_size_low;
cluststart += (fatsize * parameter_block.bpb.number_of_fats);
if (debug_prog == TRUE)
printf(catgets(catalog, 26, 2, " Scanning FAT Sectors %lu to %lu...\n"),
fatstart, fatstart+fatsize);
else
printf(catgets(catalog, 26, 3, " Scanning existing FAT...\n"));
j = 0;
for (sect = fatstart; sect < (fatstart+fatsize); sect += chunksize) {
unsigned int i;
memset((void *)&buf[0], 0, (int)chunksize * 512);
if ((sect + chunksize) > (fatstart+fatsize)) {
chunksize = 1; /* slow down in the end */
buf = (unsigned long *)(§or_buffer[0]);
Drive_IO(READ, sect, 1);
} else {
Drive_IO(READ, sect, (int)chunksize);
}
if (/* debug_prog == */ TRUE)
{
/* printf(" Chunk at %8lu...\r", sect); */
if ( percentage != (100UL * (sect-fatstart) / fatsize) )
{
percentage = 100UL * (sect-fatstart) / fatsize;
Display_Percentage_Formatted(percentage);
} /* using percentage, shorter log - 0.91o */
}
if (sect == fatstart) { /* ignore initial 2 entries */
buf[0] = 0;
buf[1] = 0;
}
for (i = 0; i < (chunksize * (512/4)); i++) {
buf[i] &= 0x0fffffffUL; /* ignore 4 high bits */
if ( (buf[i] < 0x0ffffff0UL) || /* free or used cluster */
(buf[i] > 0x0ffffff7UL) ) { /* last cluster of a chain */
if (buf[i]) {
countused++; /* count to-be-deleted entries */
last_used = j+i;
}
if (buf[i] > 0x0ffffff7UL)
countitems++; /* last cluster of 1 item */
} else {
if (check_too_bad(bad_sector_map_pointer)) {
bad_sector_map_pointer--; /* too-many-bad-clusters workaround! */
}
bad_sector_map[bad_sector_map_pointer] =
cluststart + ((j+i-2) * BPB_SECTORS_PER_CLUSTER(parameter_block.bpb));
bad_sector_map_pointer++;
countbad++;
}
} /* for slots in a sector */
j += chunksize * 512/4; /* done with chunk */
} /* for all FAT sectors */
if (/* debug_prog == */ TRUE)
{
/* printf("\n"); */
if (percentage != 100) Display_Percentage_Formatted(100);
}
printf(catgets(catalog, 26, 4, "\n Cluster stats: %lu used, %lu bad, %lu items, %lu last.\n"),
countused, countbad, countitems, last_used);
return ( cluststart +
( (1+last_used-2) * BPB_SECTORS_PER_CLUSTER(parameter_block.bpb) ) - 1 );
/* we round up to the END of the cluster */
} /* BadClustPreserve32 */
unsigned long BadClustPreserve16(void)
{
unsigned long fatstart = parameter_block.bpb.reserved_sectors;
unsigned long fatsize = parameter_block.bpb.sectors_per_fat;
unsigned long cluststart = fatstart; /* where 1st cluster starts */
unsigned long sect, j;
unsigned long countused = 0;
unsigned long countbad = 0;
unsigned long countitems = 0;
unsigned int * buf = (unsigned int *)(&huge_sector_buffer[0]);
unsigned long chunksize = sizeof(huge_sector_buffer_0) >> 9;
unsigned long last_used = 2;
cluststart += fatsize * parameter_block.bpb.number_of_fats;
cluststart += (parameter_block.bpb.root_directory_entries+15) >> 4;
/* * 32 / 512 -> 16 entries per sector */
if (debug_prog == TRUE)
printf(" Scanning FAT Sectors %lu to %lu...\n",
fatstart, fatstart+fatsize);
/* no scan announcement in non-debug mode: just show the results later. */
j = 0;
for (sect = fatstart; sect < (fatstart+fatsize); sect += chunksize) {
unsigned int i;
memset((void *)&buf[0], 0, (int)chunksize * 512);
if ((sect + chunksize) > (fatstart+fatsize)) {
chunksize = 1; /* slow down in the end */
buf = (unsigned int *)(§or_buffer[0]);
Drive_IO(READ, sect, 1);
} else {
Drive_IO(READ, sect, (int)chunksize);
}
if (debug_prog == TRUE)
printf(" Chunk at %8lu...\r", sect);
if (sect == fatstart) { /* ignore initial 2 entries */
buf[0] = 0;
buf[1] = 0;
}
for (i = 0; i < (chunksize * (512/2)); i++) {
if ( (buf[i] < 0x0fff0UL) || /* free or used cluster */
(buf[i] > 0x0fff7UL) ) { /* last cluster of a chain */
if (buf[i]) {
countused++; /* count to-be-deleted entries */
last_used = j+i;
}
if (buf[i] > 0x0fff7UL)
countitems++; /* last cluster of 1 item */
} else {
if (check_too_bad(bad_sector_map_pointer)) {
bad_sector_map_pointer--; /* too-many-bad-clusters workaround! */
}
bad_sector_map[bad_sector_map_pointer] =
cluststart + ((j+i-2) * BPB_SECTORS_PER_CLUSTER(parameter_block.bpb));
bad_sector_map_pointer++;
countbad++;
}
} /* for slots in a sector */
j += chunksize * 512/2; /* done with chunk */
} /* for all FAT sectors */
if (debug_prog == TRUE) printf("\n");
printf(catgets(catalog, 26, 5, "\n Cluster stats: %lu used, %lu bad, %lu items, %lu last.\n"),
countused, countbad, countitems, last_used);
return ( cluststart +
( (1+last_used-2) * BPB_SECTORS_PER_CLUSTER(parameter_block.bpb) ) - 1 );
/* we round up to the END of the cluster */
} /* BadClustPreserve16 */
unsigned long BadClustPreserve12(void) /* FAT12 is max 12 sectors / FAT, simple */
{
unsigned long fatstart = parameter_block.bpb.reserved_sectors;
unsigned long fatsize = parameter_block.bpb.sectors_per_fat;
unsigned long cluststart = fatstart; /* where 1st cluster starts */
unsigned long countused = 0;
unsigned long countbad = 0;
unsigned long countitems = 0;
unsigned int clust, index;
unsigned char * buf = (unsigned char *)(§or_buffer[0]);
unsigned long last_used = 2;
cluststart += fatsize * parameter_block.bpb.number_of_fats;
cluststart += (parameter_block.bpb.root_directory_entries+15) >> 4;
if (fatsize > 1) buf = (unsigned char *)(&huge_sector_buffer[0]);
if (fatsize > (sizeof(huge_sector_buffer_0) >> 9)) {
printf(catgets(catalog, 26, 6, " Cannot process existing FAT12, too big!\n"));
return 0xffffffffUL;
}
/* no scan announcement: just show the results. FAT12 is tiny. */
Drive_IO(READ, parameter_block.bpb.reserved_sectors, (int)fatsize);
index = 3;
for (clust = 2; clust < 4080; clust+=2) {
/* bytes 12 34 56 -> pair of clusters: first is 412, second is 563 */
unsigned int clusta, clustb;
clusta = buf[index+1] & 0x0f;
clusta <<= 8;
clusta |= buf[index];
clustb = buf[index+2];
clustb <<= 4;
clustb |= buf[index+1] >> 4;
if (clusta > 0xff7) {
clusta = 1; /* end of chain */
countitems++;
}
if (clustb > 0xff7) {
clustb = 1; /* end of chain */
countitems++;
}
if (clusta > 0xff0) {
clusta = 0;
countbad++;
if (check_too_bad(bad_sector_map_pointer)) {
bad_sector_map_pointer--; /* too-many-bad-clusters workaround! */
}
bad_sector_map[bad_sector_map_pointer] =
cluststart + ((clust-2) * BPB_SECTORS_PER_CLUSTER(parameter_block.bpb));
bad_sector_map_pointer++;
}
if (clustb > 0xff0) {
clustb = 0;
countbad++;
if (check_too_bad(bad_sector_map_pointer)) {
bad_sector_map_pointer--; /* too-many-bad-clusters workaround! */
}
bad_sector_map[bad_sector_map_pointer] =
cluststart + ((clust+1-2) * BPB_SECTORS_PER_CLUSTER(parameter_block.bpb));
bad_sector_map_pointer++;
}
if (clusta > 0) {
countused++;
last_used = clust; /* first of a pair */
}
if (clustb > 0) {
countused++;
last_used = clust+1; /* second of a pair */
}
index += 3;
}
printf(catgets(catalog, 26, 7, "\n Cluster stats: %lu used, %lu bad, %lu items, %lu last.\n"),
countused, countbad, countitems, last_used);
return ( cluststart +
( (1+last_used-2) * BPB_SECTORS_PER_CLUSTER(parameter_block.bpb) ) - 1 );
/* we round up to the END of the cluster */
} /* BadClustPreserve12 */