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
99
100
101
102
103
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
136
137
138
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
164
165
166
170
171
172
173
174
175
176
177
178
179
187
188
189
190
191
192
193
194
195
196
197
203
204
210
214
215
222
223
224
225
226
228
234
235
236
237
238
239
242
243
244
245
251
252
253
254
255
256
257
258
259
260
261
267
268
269
270
271
272
273
274
275
281
282
283
284
285
286
287
288
289
290
296
297
298
299
300
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
335
336
337
338
339
340
341
342
343
344
345
346
352
353
354
355
356
357
358
369
370
371
372
373
374
375
376
377
378
384
385
386
390
391
392
393
394
395
396
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_utility.h"
#include "fx_directory.h"
#include "fx_fault_tolerant.h"
#ifdef FX_ENABLE_FAULT_TOLERANT...
...
UINT _fx_fault_tolerant_create_log_file(FX_MEDIA *media_ptr)
{
ULONG clusters;
ULONG FAT_index;
ULONG FAT_value = 0;
UINT found;
UINT status;
UINT i;
#ifdef FX_ENABLE_EXFAT
ULONG checksum;
UINT count;
UCHAR *byte_ptr;
UCHAR cluster_state;/* ... */
#endif
if (media_ptr -> fx_media_available_clusters < media_ptr -> fx_media_fault_tolerant_clusters)
{
return(FX_NO_MORE_SPACE);
}if (media_ptr -> fx_media_available_clusters < media_ptr -> fx_media_fault_tolerant_clusters) { ... }
clusters = media_ptr -> fx_media_fault_tolerant_clusters;
FAT_index = FX_FAT_ENTRY_START;
found = FX_FALSE;
while (FAT_index <= (media_ptr -> fx_media_total_clusters - clusters + FX_FAT_ENTRY_START))
{
i = 0;
#ifdef FX_ENABLE_EXFAT
if (media_ptr -> fx_media_FAT_type == FX_exFAT)
{
do
{
status = _fx_utility_exFAT_cluster_state_get(media_ptr, (FAT_index + i), &cluster_state);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
if (cluster_state == FX_EXFAT_BITMAP_CLUSTER_OCCUPIED)
{
break;
}if (cluster_state == FX_EXFAT_BITMAP_CLUSTER_OCCUPIED) { ... }
i++;
...} while (i < clusters);
}if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... }
else
{
#endif
do
{
status = _fx_utility_FAT_entry_read(media_ptr, (FAT_index + i), &FAT_value);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
if (FAT_value != FX_FREE_CLUSTER)
{
break;
}if (FAT_value != FX_FREE_CLUSTER) { ... }
i++;
...} while (i < clusters);
#ifdef FX_ENABLE_EXFAT
}else { ... }
#endif
if (i >= clusters)
{
/* ... */
found = FX_TRUE;
break;
}if (i >= clusters) { ... }
else
{
#ifdef FX_ENABLE_EXFAT
if (media_ptr -> fx_media_FAT_type == FX_exFAT)
{
status = _fx_utility_exFAT_bitmap_free_cluster_find(media_ptr,
FAT_index + i + 1,
&FAT_value);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
if (FAT_value < FAT_index + i + 1)
{
FAT_index = media_ptr -> fx_media_total_clusters + FX_FAT_ENTRY_START;
}if (FAT_value < FAT_index + i + 1) { ... }
else
{
FAT_index = FAT_value;
}else { ... }
}if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... }
else
{
#endif
FAT_index = FAT_index + i + 1;
#ifdef FX_ENABLE_EXFAT
}else { ... }
#endif
}else { ... }
}while (FAT_index <= (media_ptr -> fx_media_total_clusters - clusters + FX_FAT_ENTRY_START)) { ... }
/* ... */
if (!found)
{
return(FX_NO_MORE_SPACE);
}if (!found) { ... }
for (i = 0; i < (clusters - 1); i++)
{
/* ... */
status = _fx_utility_FAT_entry_write(media_ptr, FAT_index + i, FAT_index + i + 1);
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, FAT_index + i, FX_EXFAT_BITMAP_CLUSTER_OCCUPIED);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
}if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... }
/* ... */#endif
}for (i = 0; i < (clusters - 1); i++) { ... }
status = _fx_utility_FAT_entry_write(media_ptr, FAT_index + clusters - 1, media_ptr -> fx_media_fat_last);
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, FAT_index + clusters - 1, FX_EXFAT_BITMAP_CLUSTER_OCCUPIED);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
}if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... }
/* ... */#endif
#ifdef FX_FAULT_TOLERANT
_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 /* ... */
#endif
_fx_utility_32_unsigned_write(media_ptr -> fx_media_fault_tolerant_memory_buffer + FX_FAULT_TOLERANT_BOOT_INDEX, FAT_index);
media_ptr -> fx_media_driver_request = FX_DRIVER_BOOT_WRITE;
media_ptr -> fx_media_driver_status = FX_IO_ERROR;
media_ptr -> fx_media_driver_buffer = media_ptr -> fx_media_fault_tolerant_memory_buffer;
media_ptr -> fx_media_driver_sectors = 1;
media_ptr -> fx_media_driver_sector_type = FX_BOOT_SECTOR;
FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_BOOT_WRITE, media_ptr, media_ptr -> fx_media_fault_tolerant_memory_buffer, 0, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0)
(media_ptr -> fx_media_driver_entry) (media_ptr);
if (media_ptr -> fx_media_driver_status != FX_SUCCESS)
{
return(FX_BOOT_ERROR);
}if (media_ptr -> fx_media_driver_status != FX_SUCCESS) { ... }
#ifdef FX_ENABLE_EXFAT
if (media_ptr -> fx_media_FAT_type == FX_exFAT)
{
status = _fx_utility_exFAT_system_sector_write(media_ptr, media_ptr -> fx_media_fault_tolerant_memory_buffer,
FX_EXFAT_FAT_MAIN_SYSTEM_AREA_SIZE,
1, FX_BOOT_SECTOR);
if (status != FX_SUCCESS)
{
return(FX_BOOT_ERROR);
}if (status != FX_SUCCESS) { ... }
checksum = 0;
byte_ptr = media_ptr -> fx_media_fault_tolerant_memory_buffer;
for (i = 0; i < media_ptr -> fx_media_bytes_per_sector; i++)
{
if ((FX_EF_VOLUME_FLAGS == i) ||
(FX_EF_VOLUME_FLAGS + 1 == i) ||
(FX_EF_PERCENT_IN_USE == i))
{
continue;
}if ((FX_EF_VOLUME_FLAGS == i) || (FX_EF_VOLUME_FLAGS + 1 == i) || (FX_EF_PERCENT_IN_USE == i)) { ... }
checksum = ((checksum >> 1) | (checksum << 31)) + (ULONG)byte_ptr[i];
}for (i = 0; i < media_ptr -> fx_media_bytes_per_sector; i++) { ... }
for (count = 1; count < FX_EXFAT_FAT_CHECK_SUM_OFFSET; count++)
{
status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) count,
media_ptr -> fx_media_fault_tolerant_memory_buffer, ((ULONG) 1), FX_BOOT_SECTOR);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
for (i = 0; i < media_ptr -> fx_media_bytes_per_sector; i++)
{
checksum = ((checksum >> 1) | (checksum << 31)) + (ULONG)byte_ptr[i];
}for (i = 0; i < media_ptr -> fx_media_bytes_per_sector; i++) { ... }
}for (count = 1; count < FX_EXFAT_FAT_CHECK_SUM_OFFSET; count++) { ... }
status = _fx_utility_exFAT_system_area_checksum_write(media_ptr, media_ptr -> fx_media_fault_tolerant_memory_buffer,
&checksum);
if (status != FX_SUCCESS)
{
return(status);
}if (status != FX_SUCCESS) { ... }
}if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... }
/* ... */#endif
media_ptr -> fx_media_fault_tolerant_start_cluster = FAT_index;
media_ptr -> fx_media_available_clusters =
media_ptr -> fx_media_available_clusters - clusters;
return(FX_SUCCESS);
}_fx_fault_tolerant_create_log_file (FX_MEDIA *media_ptr) { ... }
/* ... */#endif