Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_utility.h"
#include "fx_fault_tolerant.h"
...
Files
loading...
SourceVuSTM32 Libraries and Samplesfilexcommon/src/fx_fault_tolerant_apply_logs.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
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
335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** FileX Component */ /** */ /** Fault Tolerant */ /** */... /**************************************************************************/ /**************************************************************************/ #define FX_SOURCE_CODE #include "fx_api.h" #include "fx_utility.h" #include "fx_fault_tolerant.h" #ifdef FX_ENABLE_FAULT_TOLERANT... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _fx_fault_tolerant_apply_logs PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function applies changes to the file system. The changes are */ /* already recorded in the fault tolerant log file. Therefore this */ /* function reads the content of the log entries and apply these */ /* changes to the file system. */ /* */ /* INPUT */ /* */ /* media_ptr Media control block pointer */ /* */ /* OUTPUT */ /* */ /* return status */ /* */ /* CALLS */ /* */ /* _fx_utility_logical_sector_write Write a logical sector */ /* _fx_utility_logical_sector_read Read a logical sector */ /* _fx_utility_16_unsigned_read Read a USHORT from memory */ /* _fx_utility_32_unsigned_read Read a ULONG from memory */ /* _fx_utility_64_unsigned_read Read a ULONG64 from memory */ /* _fx_utility_FAT_entry_write Write a FAT entry */ /* _fx_utility_exFAT_cluster_state_set Set state of exFAT cluster */ /* _fx_fault_tolerant_cleanup_FAT_chain Cleanup FAT chain */ /* memcpy Memory Copy */ /* _fx_utility_exFAT_bitmap_flush Flush exFAT allocation bitmap */ /* _fx_utility_FAT_flush Flush written FAT entries */ /* _fx_utility_logical_sector_flush Flush written logical sectors */ /* */ /* CALLED BY */ /* */ /* _fx_fault_tolerant_enable */ /* _fx_fault_tolerant_transaction_end */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 William E. Lamie Initial Version 6.0 */ /* 09-30-2020 William E. Lamie Modified comment(s), verified */ /* memcpy usage, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _fx_fault_tolerant_apply_logs(FX_MEDIA *media_ptr) { UINT status; ULONG64 log_sector; ULONG remaining_logs; ULONG copy_size; ULONG copy_offset; UCHAR *current_ptr; ULONG size; USHORT log_type; ULONG log_len; FX_FAULT_TOLERANT_LOG_HEADER *log_header; FX_FAULT_TOLERANT_FAT_CHAIN *FAT_chain; FX_FAULT_TOLERANT_LOG_CONTENT *log_content; FX_FAULT_TOLERANT_FAT_LOG *fat_log; FX_FAULT_TOLERANT_DIR_LOG *dir_log; #ifdef FX_ENABLE_EXFAT FX_FAULT_TOLERANT_BITMAP_LOG *bitmap_log; ULONG current_cluster; ULONG head_cluster; ULONG tail_cluster;/* ... */ #endif /* FX_ENABLE_EXFAT */ /* Set log header, FAT chain and log content pointer. */ log_header = (FX_FAULT_TOLERANT_LOG_HEADER *)media_ptr -> fx_media_fault_tolerant_memory_buffer; FAT_chain = (FX_FAULT_TOLERANT_FAT_CHAIN *)(media_ptr -> fx_media_fault_tolerant_memory_buffer + FX_FAULT_TOLERANT_FAT_CHAIN_OFFSET); log_content = (FX_FAULT_TOLERANT_LOG_CONTENT *)(media_ptr -> fx_media_fault_tolerant_memory_buffer + FX_FAULT_TOLERANT_LOG_CONTENT_OFFSET); /* Find the size of the log file. */ size = _fx_utility_16_unsigned_read((UCHAR *)&log_header -> fx_fault_tolerant_log_header_total_size); /* Extended port-specific processing macro, which is by default defined to white space. */ FX_FAULT_TOLERANT_APPLY_LOGS_EXTENSION size -= FX_FAULT_TOLERANT_LOG_CONTENT_HEADER_SIZE; /* Find the number of log entries. */ remaining_logs = _fx_utility_16_unsigned_read((UCHAR *)&log_content -> fx_fault_tolerant_log_content_count); /* Get start address of logs. */ current_ptr = (UCHAR *)log_content + FX_FAULT_TOLERANT_LOG_CONTENT_HEADER_SIZE; /* Loop through all the logs to apply the changes. */ while (remaining_logs) { /* Obtain log type of this entry. */ log_type = (USHORT)_fx_utility_16_unsigned_read(current_ptr); /* Read log length. */ log_len = _fx_utility_16_unsigned_read(current_ptr + 2); /* Validate log entry size. */ if (log_len > size) { /* Something wrong with log file. */ return(FX_FILE_CORRUPT); }if (log_len > size) { ... } /* Reduce the total log file size. */ size -= log_len; /* Process log entry by type. */ switch (log_type) { case FX_FAULT_TOLERANT_FAT_LOG_TYPE: /* This is a FAT log. */ fat_log = (FX_FAULT_TOLERANT_FAT_LOG *)current_ptr; /* Write FAT entry. */ status = _fx_utility_FAT_entry_write(media_ptr, _fx_utility_32_unsigned_read((UCHAR *)&fat_log -> fx_fault_tolerant_FAT_log_cluster), _fx_utility_32_unsigned_read((UCHAR *)&fat_log -> fx_fault_tolerant_FAT_log_value)); if (status != FX_SUCCESS) { /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } break; #ifdef FX_ENABLE_EXFATcase FX_FAULT_TOLERANT_FAT_LOG_TYPE: case FX_FAULT_TOLERANT_BITMAP_LOG_TYPE: /* This is a bitmap log. */ bitmap_log = (FX_FAULT_TOLERANT_BITMAP_LOG *)current_ptr; /* Set bitmap entry. */ status = _fx_utility_exFAT_cluster_state_set(media_ptr, _fx_utility_32_unsigned_read((UCHAR *)&bitmap_log -> fx_fault_tolerant_bitmap_log_cluster), (UCHAR)_fx_utility_32_unsigned_read((UCHAR *)&bitmap_log -> fx_fault_tolerant_bitmap_log_value)); if (status != FX_SUCCESS) { /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } break; /* ... */ #endif /* FX_ENABLE_EXFAT */case FX_FAULT_TOLERANT_BITMAP_LOG_TYPE: case FX_FAULT_TOLERANT_DIR_LOG_TYPE: /* This is a DIR log. */ dir_log = (FX_FAULT_TOLERANT_DIR_LOG *)current_ptr; log_sector = _fx_utility_64_unsigned_read((UCHAR *)&dir_log -> fx_fault_tolerant_dir_log_sector); /* Get the destination sector. */ status = _fx_utility_logical_sector_read(media_ptr, log_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DATA_SECTOR); if (status != FX_SUCCESS) { /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Set copy information. */ copy_offset = _fx_utility_32_unsigned_read((UCHAR *)&dir_log -> fx_fault_tolerant_dir_log_offset); copy_size = log_len - FX_FAULT_TOLERANT_DIR_LOG_ENTRY_SIZE; if ((copy_offset + copy_size) > media_ptr -> fx_media_memory_size) { return(FX_FILE_CORRUPT); }if ((copy_offset + copy_size) > media_ptr -> fx_media_memory_size) { ... } /* Copy data into destination sector. */ memcpy(media_ptr -> fx_media_memory_buffer + copy_offset, /* Use case of memcpy is verified. */ current_ptr + FX_FAULT_TOLERANT_DIR_LOG_ENTRY_SIZE, copy_size); /* Write back the current logical sector. */ status = _fx_utility_logical_sector_write(media_ptr, log_sector, media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_DIRECTORY_SECTOR); if (status != FX_SUCCESS) { /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } break; case FX_FAULT_TOLERANT_DIR_LOG_TYPE: default: /* Wrong type. */ return(FX_SECTOR_INVALID);default }switch (log_type) { ... } /* Decrement the remaining log counter. */ remaining_logs--; /* Move to start position of next log entry. */ current_ptr += log_len; }while (remaining_logs) { ... } /* Check whether or not to process FAT chain. */ if (FAT_chain -> fx_fault_tolerant_FAT_chain_flag & FX_FAULT_TOLERANT_FLAG_FAT_CHAIN_VALID) { /* Free old link of FAT. */ #ifdef FX_ENABLE_EXFAT if (FAT_chain -> fx_fault_tolerant_FAT_chain_flag & FX_FAULT_TOLERANT_FLAG_BITMAP_USED) { /* Process FAT chain. */ /* Get head and tail cluster from FAT chain. */ head_cluster = _fx_utility_32_unsigned_read((UCHAR *)&FAT_chain -> fx_fault_tolerant_FAT_chain_head_original); 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)) { for (current_cluster = head_cluster; current_cluster < tail_cluster; current_cluster++) { /* Free bitmap. */ status = _fx_utility_exFAT_cluster_state_set(media_ptr, current_cluster, FX_EXFAT_BITMAP_CLUSTER_FREE); if (status != FX_SUCCESS) { /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Increase the available clusters in the media control block. */ media_ptr -> fx_media_available_clusters++; }for (current_cluster = head_cluster; current_cluster < tail_cluster; current_cluster++) { ... } }if ((head_cluster >= FX_FAT_ENTRY_START) && (head_cluster < media_ptr -> fx_media_fat_reserved)) { ... } }if (FAT_chain -> fx_fault_tolerant_FAT_chain_flag & FX_FAULT_TOLERANT_FLAG_BITMAP_USED) { ... } else { #endif /* FX_ENABLE_EXFAT */ status = _fx_fault_tolerant_cleanup_FAT_chain(media_ptr, FX_FAULT_TOLERANT_FAT_CHAIN_CLEANUP); if (status != FX_SUCCESS) { /* Return the error status. */ return(status); }if (status != FX_SUCCESS) { ... } #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ }if (FAT_chain -> fx_fault_tolerant_FAT_chain_flag & FX_FAULT_TOLERANT_FLAG_FAT_CHAIN_VALID) { ... } /* Flush the internal logical sector cache. */ status = _fx_utility_logical_sector_flush(media_ptr, 1, (ULONG64) media_ptr -> fx_media_total_sectors, FX_FALSE); /* Check for a bad status. */ if (status != FX_SUCCESS) { /* Return the bad status. */ return(status); }if (status != FX_SUCCESS) { ... } /* Flush FAT table. */ #ifdef FX_FAULT_TOLERANT #ifdef FX_ENABLE_EXFAT if (media_ptr -> fx_media_FAT_type == FX_exFAT) { /* Flush exFAT bitmap. */ _fx_utility_exFAT_bitmap_flush(media_ptr); }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ /* Ensure the new FAT chain is properly written to the media. */ /* Flush the cached individual FAT entries */ _fx_utility_FAT_flush(media_ptr);/* ... */ #endif /* Return success. */ return(FX_SUCCESS); }_fx_fault_tolerant_apply_logs (FX_MEDIA *media_ptr) { ... } /* ... */#endif /* FX_ENABLE_FAULT_TOLERANT */
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.