Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define FX_SOURCE_CODE
#include "fx_api.h"
#include "fx_system.h"
#include "fx_media.h"
#include "fx_file.h"
#include "fx_directory.h"
#include "fx_utility.h"
...
...
_fx_media_flush(FX_MEDIA *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesfilexcommon/src/fx_media_flush.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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 */ /** */ /** Media */ /** */... /**************************************************************************/ /**************************************************************************/ #define FX_SOURCE_CODE /* Include necessary system files. */ #include "fx_api.h" #include "fx_system.h" #include "fx_media.h" #include "fx_file.h" #include "fx_directory.h" #include "fx_utility.h" 6 includes ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _fx_media_flush PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* William E. Lamie, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function examines the list of open files for this media and */ /* "flushes" each written open file to the underlying media. After */ /* the open files have been flushed, the internal logical sector is */ /* flushed. Finally, the attached driver is sent a flush command so */ /* that it can flush its sector cache (if any) to the media. */ /* */ /* INPUT */ /* */ /* media_ptr Media control block pointer */ /* */ /* OUTPUT */ /* */ /* return status */ /* */ /* CALLS */ /* */ /* _fx_directory_entry_write Write the directory entry */ /* _fx_utility_FAT_flush Flush cached FAT entries */ /* _fx_utility_FAT_map_flush Flush primary FAT changes to */ /* secondary FAT(s) */ /* _fx_utility_logical_sector_flush Flush logical sector cache */ /* _fx_utility_32_unsigned_read Read 32-bit unsigned */ /* _fx_utility_32_unsigned_write Write 32-bit unsigned */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 William E. Lamie Initial Version 6.0 */ /* 09-30-2020 William E. Lamie Modified comment(s), and */ /* added conditional to */ /* disable cache, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _fx_media_flush(FX_MEDIA *media_ptr) { UINT status; ULONG open_count; FX_FILE *file_ptr; FX_INT_SAVE_AREA #ifndef FX_MEDIA_STATISTICS_DISABLE /* Increment the number of times this service has been called. */ media_ptr -> fx_media_flushes++;/* ... */ #endif /* Check the media to make sure it is open. */ if (media_ptr -> fx_media_id != FX_MEDIA_ID) { /* Return the media not opened error. */ return(FX_MEDIA_NOT_OPEN); }if (media_ptr -> fx_media_id != FX_MEDIA_ID) { ... } /* If trace is enabled, insert this event into the trace buffer. */ FX_TRACE_IN_LINE_INSERT(FX_TRACE_MEDIA_FLUSH, media_ptr, 0, 0, 0, FX_TRACE_MEDIA_EVENTS, 0, 0) /* Protect against other threads accessing the media. */ FX_PROTECT /* Check for write protect at the media level (set by driver). */ if (media_ptr -> fx_media_driver_write_protect) { /* Release media protection. */ FX_UNPROTECT /* Return write protect error. */ return(FX_WRITE_PROTECT); }if (media_ptr -> fx_media_driver_write_protect) { ... } /* Loop through the media's open files. */ open_count = media_ptr -> fx_media_opened_file_count; file_ptr = media_ptr -> fx_media_opened_file_list; while (open_count) { /* Look at each opened file to see if the same file is opened for writing and has been written to. *//* ... */ if ((file_ptr -> fx_file_open_mode == FX_OPEN_FOR_WRITE) && (file_ptr -> fx_file_modified)) { /* Protect against update. */ FX_DISABLE_INTS /* Set the new time and date. */ file_ptr -> fx_file_dir_entry.fx_dir_entry_time = _fx_system_time; file_ptr -> fx_file_dir_entry.fx_dir_entry_date = _fx_system_date; /* Restore interrupts. */ FX_RESTORE_INTS /* Copy the new file size into the directory entry. */ file_ptr -> fx_file_dir_entry.fx_dir_entry_file_size = file_ptr -> fx_file_current_file_size; /* Write the directory entry to the media. */ #ifdef FX_ENABLE_EXFAT if (media_ptr -> fx_media_FAT_type == FX_exFAT) { status = _fx_directory_exFAT_entry_write( media_ptr, &(file_ptr -> fx_file_dir_entry), UPDATE_STREAM); }if (media_ptr -> fx_media_FAT_type == FX_exFAT) { ... } else { #endif /* FX_ENABLE_EXFAT */ status = _fx_directory_entry_write(media_ptr, &(file_ptr -> fx_file_dir_entry)); #ifdef FX_ENABLE_EXFAT }else { ... } #endif /* FX_ENABLE_EXFAT */ /* Check for a good status. */ if (status != FX_SUCCESS) { /* Release media protection. */ FX_UNPROTECT /* Error writing the directory. */ return(status); }if (status != FX_SUCCESS) { ... } /* Clear the file modified flag. */ file_ptr -> fx_file_modified = FX_FALSE; }if ((file_ptr -> fx_file_open_mode == FX_OPEN_FOR_WRITE) && (file_ptr -> fx_file_modified)) { ... } /* Adjust the pointer and decrement the opened count. */ file_ptr = file_ptr -> fx_file_opened_next; open_count--; }while (open_count) { ... } /* Flush the cached individual FAT entries */ _fx_utility_FAT_flush(media_ptr); /* Flush changed sector(s) in the primary FAT to secondary FATs. */ _fx_utility_FAT_map_flush(media_ptr); #ifdef FX_ENABLE_EXFAT if ((media_ptr -> fx_media_FAT_type == FX_exFAT) && (FX_TRUE == media_ptr -> fx_media_exfat_bitmap_cache_dirty)) { /* Flush bitmap. */ _fx_utility_exFAT_bitmap_flush(media_ptr); }if ((media_ptr -> fx_media_FAT_type == FX_exFAT) && (FX_TRUE == media_ptr -> fx_media_exfat_bitmap_cache_dirty)) { ... } /* ... */#endif /* FX_ENABLE_EXFAT */ /* Flush the internal logical sector cache. */ status = _fx_utility_logical_sector_flush(media_ptr, ((ULONG64) 1), (ULONG64) (media_ptr -> fx_media_total_sectors), FX_FALSE); /* Check for a good status. */ if (status != FX_SUCCESS) { /* Release media protection. */ FX_UNPROTECT /* Error writing the directory. */ return(status); }if (status != FX_SUCCESS) { ... } /* Determine if the media needs to have the additional information sector updated. This will only be the case for 32-bit FATs. The logic here only needs to be done if the last reported available cluster count is different that the currently available clusters. *//* ... */ if ((media_ptr -> fx_media_FAT32_additional_info_sector) && (media_ptr -> fx_media_FAT32_additional_info_last_available != media_ptr -> fx_media_available_clusters)) { UCHAR *buffer_ptr; ULONG signature; #ifndef FX_DISABLE_CACHE /* Setup a pointer to the first cached entry's buffer. */ buffer_ptr = (media_ptr -> fx_media_sector_cache_list_ptr) -> fx_cached_sector_memory_buffer; /* Invalidate this cache entry. */ (media_ptr -> fx_media_sector_cache_list_ptr) -> fx_cached_sector = (~(ULONG64)0); (media_ptr -> fx_media_sector_cache_list_ptr) -> fx_cached_sector_valid = FX_FALSE;/* ... */ #else buffer_ptr = media_ptr -> fx_media_memory_buffer; #endif /* FX_DISABLE_CACHE */ /* Read the FAT32 additional information sector from the device. */ media_ptr -> fx_media_driver_request = FX_DRIVER_READ; media_ptr -> fx_media_driver_status = FX_IO_ERROR; media_ptr -> fx_media_driver_buffer = buffer_ptr; media_ptr -> fx_media_driver_logical_sector = media_ptr -> fx_media_FAT32_additional_info_sector; media_ptr -> fx_media_driver_sectors = 1; media_ptr -> fx_media_driver_sector_type = FX_DIRECTORY_SECTOR; #ifndef FX_MEDIA_STATISTICS_DISABLE /* Increment the number of driver read sector(s) requests. */ media_ptr -> fx_media_driver_read_requests++;/* ... */ #endif /* If trace is enabled, insert this event into the trace buffer. */ FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_READ, media_ptr, media_ptr -> fx_media_FAT32_additional_info_sector, 1, buffer_ptr, FX_TRACE_INTERNAL_EVENTS, 0, 0) /* Invoke the driver to read the FAT32 additional information sector. */ (media_ptr -> fx_media_driver_entry) (media_ptr); /* Determine if the FAT32 sector was read correctly. */ if (media_ptr -> fx_media_driver_status != FX_SUCCESS) { /* Release media protection. */ FX_UNPROTECT /* Return the error status. */ return(FX_IO_ERROR); }if (media_ptr -> fx_media_driver_status != FX_SUCCESS) { ... } /* Setup a pointer into the FAT32 additional information sector. */ buffer_ptr = media_ptr -> fx_media_driver_buffer; /* Pickup the first signature long word. */ signature = _fx_utility_32_unsigned_read(&buffer_ptr[0]); /* Determine if the signature is correct. */ if (signature == 0x41615252) { /* Yes, the first signature is correct, now pickup the next signature. */ signature = _fx_utility_32_unsigned_read(&buffer_ptr[484]); /* Determine if this signature is correct. */ if (signature == 0x61417272) { /* Yes, we have a good FAT32 additional information sector. */ /* Set the free cluster count to the available clusters in the media control block. */ _fx_utility_32_unsigned_write(&buffer_ptr[488], media_ptr -> fx_media_available_clusters); /* Set the next free cluster number hint to starting search cluster in the media control block. */ _fx_utility_32_unsigned_write(&buffer_ptr[492], media_ptr -> fx_media_cluster_search_start); /* Now write the sector back out to the media. */ media_ptr -> fx_media_driver_request = FX_DRIVER_WRITE; media_ptr -> fx_media_driver_status = FX_IO_ERROR; media_ptr -> fx_media_driver_buffer = buffer_ptr; media_ptr -> fx_media_driver_logical_sector = media_ptr -> fx_media_FAT32_additional_info_sector; media_ptr -> fx_media_driver_sectors = 1; media_ptr -> fx_media_driver_sector_type = FX_DIRECTORY_SECTOR; /* Set the system write flag since we are writing a directory sector. */ media_ptr -> fx_media_driver_system_write = FX_TRUE; #ifndef FX_MEDIA_STATISTICS_DISABLE /* Increment the number of driver write sector(s) requests. */ media_ptr -> fx_media_driver_write_requests++;/* ... */ #endif /* If trace is enabled, insert this event into the trace buffer. */ FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_WRITE, media_ptr, media_ptr -> fx_media_FAT32_additional_info_sector, 1, buffer_ptr, FX_TRACE_INTERNAL_EVENTS, 0, 0) /* Invoke the driver to write the FAT32 additional information sector. */ (media_ptr -> fx_media_driver_entry) (media_ptr); /* Clear the system write flag. */ media_ptr -> fx_media_driver_system_write = FX_FALSE; /* Determine if the FAT32 sector was written correctly. */ if (media_ptr -> fx_media_driver_status != FX_SUCCESS) { /* Release media protection. */ FX_UNPROTECT /* Return the sector IO error status. */ return(FX_IO_ERROR); }if (media_ptr -> fx_media_driver_status != FX_SUCCESS) { ... } /* Successful update of the FAT32 additional information sector. Update the last written available cluster count. *//* ... */ media_ptr -> fx_media_FAT32_additional_info_last_available = media_ptr -> fx_media_available_clusters; }if (signature == 0x61417272) { ... } }if (signature == 0x41615252) { ... } }if ((media_ptr -> fx_media_FAT32_additional_info_sector) && (media_ptr -> fx_media_FAT32_additional_info_last_available != media_ptr -> fx_media_available_clusters)) { ... } #ifndef FX_MEDIA_STATISTICS_DISABLE /* Increment the number of driver flush requests. */ media_ptr -> fx_media_driver_flush_requests++;/* ... */ #endif /* Build the "flush" I/O driver request. */ media_ptr -> fx_media_driver_request = FX_DRIVER_FLUSH; media_ptr -> fx_media_driver_status = FX_IO_ERROR; /* If trace is enabled, insert this event into the trace buffer. */ FX_TRACE_IN_LINE_INSERT(FX_TRACE_INTERNAL_IO_DRIVER_FLUSH, media_ptr, 0, 0, 0, FX_TRACE_INTERNAL_EVENTS, 0, 0) /* Call the specified I/O driver with the flush request. */ (media_ptr -> fx_media_driver_entry) (media_ptr); /* Determine if the I/O driver flushed successfully. */ if (media_ptr -> fx_media_driver_status != FX_SUCCESS) { /* Release media protection. */ FX_UNPROTECT /* Return the driver error status. */ return(FX_IO_ERROR); }if (media_ptr -> fx_media_driver_status != FX_SUCCESS) { ... } /* Release media protection. */ FX_UNPROTECT /* If we get here, return successful status to the caller. */ return(FX_SUCCESS); }{ ... }