File: bcread.c

package info (click to toggle)
dosemu-freedos 1%3A0.0.b9r5a%2Betch.1-0etch1
  • links: PTS
  • area: contrib
  • in suites: etch
  • size: 19,744 kB
  • ctags: 23,279
  • sloc: ansic: 143,864; asm: 20,397; makefile: 3,868; perl: 1,106; yacc: 690; sh: 553; pascal: 297; xml: 150; cpp: 67
file content (264 lines) | stat: -rw-r--r-- 9,074 bytes parent folder | download | duplicates (2)
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
/*
// Program:    Format
// Version:    0.91p
// Written by: Eric Auer
// Copyright:  2004 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 */

void BadClustPreserve32(void);
void BadClustPreserve16(void);
void 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("\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.
 */
void BadClustPreserve(void)
{
  if (parameter_block.bpb.bytes_per_sector != 512) {
    printf(" *** Cannot preserve bad cluster list - not 512 bytes per sector. ***\n");
    return;
  }
  if (param.fat_type == FAT32) {
    BadClustPreserve32();
  } else {
    if (param.fat_type == FAT16) {
      BadClustPreserve16();
    } else {
      BadClustPreserve12();
    }
  }
}



void 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;

  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(" Scanning FAT Sectors %lu to %lu for bad cluster marks...\n",
      fatstart, fatstart+fatsize);
  else
    printf(" Scanning old FAT to create bad cluster list...\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 *)(&sector_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 */
        if (buf[i] > 0x0ffffff7UL) countitems++; /* last cluster of 1 item */
      } else {
        if (check_too_bad(bad_sector_map_pointer)) return; /* too many bad clusters? */
        bad_sector_map[bad_sector_map_pointer] =
          cluststart + ((j+i-2) * parameter_block.bpb.sectors_per_cluster);
        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"); */
      Display_Percentage_Formatted(100);
    }

  printf("\n Scan completed: %lu used clusters in %lu items, %lu bad clusters.\n",
    countused, countitems, countbad);
} /* BadClustPreserve32 */



void 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;

  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 ... %lu for bad cluster marks...\n",
      fatstart, fatstart+fatsize);

  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 *)(&sector_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 */
        if (buf[i] > 0x0fff7UL) countitems++; /* last cluster of 1 item */
      } else {
        if (check_too_bad(bad_sector_map_pointer)) return; /* too many bad clusters? */
        bad_sector_map[bad_sector_map_pointer] =
          cluststart + ((j+i-2) * parameter_block.bpb.sectors_per_cluster);
        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(" Scan completed: %lu used clusters in %lu items, %lu bad clusters.\n",
    countused, countitems, countbad);
} /* BadClustPreserve16 */



void 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 *)(&sector_buffer[0]);

  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(" *** Cannot preserve bad cluster list - FAT12 too big. ***\n");
    return;
  }

  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)) return; /* too many bad clusters? */
      bad_sector_map[bad_sector_map_pointer] =
        cluststart + ((clust-2) * parameter_block.bpb.sectors_per_cluster);
      bad_sector_map_pointer++;
    }
    if (clustb > 0xff0) {
      clustb = 0;
      countbad++;
      if (check_too_bad(bad_sector_map_pointer)) return; /* too many bad clusters? */
      bad_sector_map[bad_sector_map_pointer] =
        cluststart + ((clust+1-2) * parameter_block.bpb.sectors_per_cluster);
      bad_sector_map_pointer++;
    }
    countused += ((clusta > 0) ? 1 : 0) + ((clustb > 0) ? 1 : 0);
    index += 3;
  }
  printf(" Scan completed: %lu used clusters in %lu items, %lu bad clusters.\n",
    countused, countitems, countbad);

} /* BadClustPreserve12 */