1
10
13
14
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
117
123
124
125
126
128
129
130
131
135
136
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
164
165
166
172
173
174
175
176
177
178
179
180
181
182
183
184
188
189
190
191
195
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
235
236
237
238
239
240
241
242
248
249
250
251
252
253
254
255
261
262
263
264
265
266
267
268
269
270
271
272
279
280
281
282
286
287
288
289
290
291
292
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_utility.h"
#include "fx_fault_tolerant.h"
#ifdef FX_ENABLE_FAULT_TOLERANT
...
UINT _fx_fault_tolerant_cleanup_FAT_chain(FX_MEDIA *media_ptr, UINT operation)
{
UINT status;
ULONG current_cluster;
ULONG next_cluster = 0;
ULONG head_cluster;
ULONG tail_cluster;
ULONG cache_count;
ULONG cache_max = FX_FAULT_TOLERANT_CACHE_SIZE >> 2;
ULONG *cache_ptr = media_ptr -> fx_media_fault_tolerant_cache;
ULONG next_session;
USHORT checksum;
ULONG i;
FX_FAULT_TOLERANT_FAT_CHAIN *FAT_chain;
ULONG FAT_sector;
ULONG last_FAT_sector;
FAT_chain = (FX_FAULT_TOLERANT_FAT_CHAIN *)(media_ptr -> fx_media_fault_tolerant_memory_buffer + FX_FAULT_TOLERANT_FAT_CHAIN_OFFSET);
if (operation == FX_FAULT_TOLERANT_FAT_CHAIN_RECOVER)
{
head_cluster = _fx_utility_32_unsigned_read((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_head_new);
}if (operation == FX_FAULT_TOLERANT_FAT_CHAIN_RECOVER) { ... }
else
{
head_cluster = _fx_utility_32_unsigned_read((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_head_original);
}else { ... }
/* ... */
tail_cluster = _fx_utility_32_unsigned_read((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_insertion_back);
if ((head_cluster < FX_FAT_ENTRY_START) || (head_cluster >= media_ptr -> fx_media_fat_reserved))
{
return(FX_SUCCESS);
}if ((head_cluster < FX_FAT_ENTRY_START) || (head_cluster >= media_ptr -> fx_media_fat_reserved)) { ... }
/* ... */
next_session = _fx_utility_32_unsigned_read((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_next_deletion);
while ((head_cluster >= FX_FAT_ENTRY_START) && (head_cluster < media_ptr -> fx_media_fat_reserved))
{
current_cluster = head_cluster;
cache_count = 0;
do
{
status = _fx_utility_FAT_entry_read(media_ptr, current_cluster, &next_cluster);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
if (next_cluster == FX_FREE_CLUSTER)
{
break;
}if (next_cluster == FX_FREE_CLUSTER) { ... }
cache_ptr[cache_count++] = current_cluster;
current_cluster = next_cluster;
...} while ((next_cluster >= FX_FAT_ENTRY_START) &&
(next_cluster < media_ptr -> fx_media_fat_reserved) &&
(next_cluster != tail_cluster) &&
(cache_count < cache_max));
if (cache_count == cache_max)
{
next_session = next_cluster;
}if (cache_count == cache_max) { ... }
_fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_next_deletion, next_session);
if (operation == FX_FAULT_TOLERANT_FAT_CHAIN_RECOVER)
{
_fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_head_new, head_cluster);
}if (operation == FX_FAULT_TOLERANT_FAT_CHAIN_RECOVER) { ... }
else
{
_fx_utility_32_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_head_original, head_cluster);
}else { ... }
_fx_utility_16_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_checksumm, 0);
checksum = _fx_fault_tolerant_calculate_checksum((UCHAR *)FAT_chain, FX_FAULT_TOLERANT_FAT_CHAIN_SIZE);
_fx_utility_16_unsigned_write((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_checksumm, checksum);
_fx_fault_tolerant_write_log_file(media_ptr, 0);
if (cache_count > 0)
{
last_FAT_sector = _fx_utility_FAT_sector_get(media_ptr, cache_ptr[cache_count - 1]);
i = cache_count;
while (i > 0)
{
i--;
FAT_sector = _fx_utility_FAT_sector_get(media_ptr, cache_ptr[i]);
if (FAT_sector != last_FAT_sector)
{
_fx_utility_FAT_flush(media_ptr);
#ifdef FX_ENABLE_EXFAT
if (media_ptr -> fx_media_FAT_type == FX_exFAT)
{
_fx_utility_exFAT_bitmap_flush(media_ptr);
}if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... }
/* ... */#endif
last_FAT_sector = FAT_sector;
}if (FAT_sector != last_FAT_sector) { ... }
status = _fx_utility_FAT_entry_write(media_ptr, cache_ptr[i], FX_FREE_CLUSTER);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
#ifdef FX_ENABLE_EXFAT
if (media_ptr -> fx_media_FAT_type == FX_exFAT)
{
status = _fx_utility_exFAT_cluster_state_set(media_ptr, cache_ptr[i], FX_EXFAT_BITMAP_CLUSTER_FREE);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
}if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... }
/* ... */#endif
}while (i > 0) { ... }
media_ptr -> fx_media_available_clusters += cache_count;
}if (cache_count > 0) { ... }
_fx_utility_FAT_flush(media_ptr);
#ifdef FX_ENABLE_EXFAT
if (media_ptr -> fx_media_FAT_type == FX_exFAT)
{
_fx_utility_exFAT_bitmap_flush(media_ptr);
}if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... }
/* ... */#endif
if (head_cluster == next_session)
{
break;
}if (head_cluster == next_session) { ... }
head_cluster = next_session;
}while ((head_cluster >= FX_FAT_ENTRY_START) && (head_cluster < media_ptr -> fx_media_fat_reserved)) { ... }
return(FX_SUCCESS);
}_fx_fault_tolerant_cleanup_FAT_chain (FX_MEDIA *media_ptr, UINT operation) { ... }
/* ... */#endif