1 |
|
|
/**************************************************************************/ |
2 |
|
|
/* */ |
3 |
|
|
/* Copyright (c) Microsoft Corporation. All rights reserved. */ |
4 |
|
|
/* */ |
5 |
|
|
/* This software is licensed under the Microsoft Software License */ |
6 |
|
|
/* Terms for Microsoft Azure RTOS. Full text of the license can be */ |
7 |
|
|
/* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ |
8 |
|
|
/* and in the root directory of this software. */ |
9 |
|
|
/* */ |
10 |
|
|
/**************************************************************************/ |
11 |
|
|
|
12 |
|
|
|
13 |
|
|
/**************************************************************************/ |
14 |
|
|
/**************************************************************************/ |
15 |
|
|
/** */ |
16 |
|
|
/** FileX Component */ |
17 |
|
|
/** */ |
18 |
|
|
/** Utility */ |
19 |
|
|
/** */ |
20 |
|
|
/**************************************************************************/ |
21 |
|
|
/**************************************************************************/ |
22 |
|
|
|
23 |
|
|
#define FX_SOURCE_CODE |
24 |
|
|
|
25 |
|
|
|
26 |
|
|
/* Include necessary system files. */ |
27 |
|
|
|
28 |
|
|
#include "fx_api.h" |
29 |
|
|
#include "fx_system.h" |
30 |
|
|
#include "fx_utility.h" |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
/**************************************************************************/ |
34 |
|
|
/* */ |
35 |
|
|
/* FUNCTION RELEASE */ |
36 |
|
|
/* */ |
37 |
|
|
/* _fx_utility_FAT_flush PORTABLE C */ |
38 |
|
|
/* 6.1.2 */ |
39 |
|
|
/* AUTHOR */ |
40 |
|
|
/* */ |
41 |
|
|
/* William E. Lamie, Microsoft Corporation */ |
42 |
|
|
/* */ |
43 |
|
|
/* DESCRIPTION */ |
44 |
|
|
/* */ |
45 |
|
|
/* This function flushes the contents of the FAT cache to the media. */ |
46 |
|
|
/* 12-bit, 16-bit and 32-bit FAT writing is supported. */ |
47 |
|
|
/* */ |
48 |
|
|
/* INPUT */ |
49 |
|
|
/* */ |
50 |
|
|
/* media_ptr Media control block pointer */ |
51 |
|
|
/* */ |
52 |
|
|
/* OUTPUT */ |
53 |
|
|
/* */ |
54 |
|
|
/* return status */ |
55 |
|
|
/* */ |
56 |
|
|
/* CALLS */ |
57 |
|
|
/* */ |
58 |
|
|
/* _fx_utility_16_unsigned_write Write a UINT into buffer */ |
59 |
|
|
/* _fx_utility_32_unsigned_read Read a ULONG from buffer */ |
60 |
|
|
/* _fx_utility_32_unsigned_write Write a ULONG into buffer */ |
61 |
|
|
/* _fx_utility_logical_sector_read Read FAT sector into memory */ |
62 |
|
|
/* _fx_utility_logical_sector_write Write FAT sector back to disk */ |
63 |
|
|
/* */ |
64 |
|
|
/* CALLED BY */ |
65 |
|
|
/* */ |
66 |
|
|
/* FileX System Functions */ |
67 |
|
|
/* */ |
68 |
|
|
/* RELEASE HISTORY */ |
69 |
|
|
/* */ |
70 |
|
|
/* DATE NAME DESCRIPTION */ |
71 |
|
|
/* */ |
72 |
|
|
/* 05-19-2020 William E. Lamie Initial Version 6.0 */ |
73 |
|
|
/* 09-30-2020 William E. Lamie Modified comment(s), */ |
74 |
|
|
/* resulting in version 6.1 */ |
75 |
|
|
/* 11-09-2020 William E. Lamie Modified comment(s), */ |
76 |
|
|
/* updated logic for */ |
77 |
|
|
/* FAT secondary update map, */ |
78 |
|
|
/* resulting in version 6.1.2 */ |
79 |
|
|
/* */ |
80 |
|
|
/**************************************************************************/ |
81 |
|
471818 |
UINT _fx_utility_FAT_flush(FX_MEDIA *media_ptr) |
82 |
|
|
{ |
83 |
|
|
|
84 |
|
|
ULONG FAT_sector; |
85 |
|
|
ULONG byte_offset; |
86 |
|
|
UCHAR *FAT_ptr; |
87 |
|
|
UINT temp, i; |
88 |
|
|
UINT status, index, ind; |
89 |
|
|
ULONG cluster, next_cluster; |
90 |
|
|
UCHAR sectors_per_bit; |
91 |
|
|
INT multi_sector_entry; |
92 |
|
|
ULONG sector; |
93 |
|
|
|
94 |
|
|
#ifndef FX_MEDIA_STATISTICS_DISABLE |
95 |
|
|
/* Increment the number of cache flush requests. */ |
96 |
|
471818 |
media_ptr -> fx_media_fat_cache_flushes++; |
97 |
|
|
#endif |
98 |
|
|
|
99 |
|
|
/* Loop through the media's FAT cache and flush out dirty entries. */ |
100 |
✓✓ |
26793961 |
for (index = 0; index < FX_MAX_FAT_CACHE; index++) |
101 |
|
|
{ |
102 |
|
|
|
103 |
|
|
/* Determine if the entry is dirty. */ |
104 |
✓✓ |
26389883 |
if ((media_ptr -> fx_media_fat_cache[index].fx_fat_cache_entry_dirty) == 0) |
105 |
|
|
{ |
106 |
|
|
|
107 |
|
|
/* No, just advance to the next entry. */ |
108 |
|
25945006 |
continue; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
/* Otherwise, the entry is indeed dirty and must be flushed out. Process |
112 |
|
|
relative to the type of FAT that is being used. */ |
113 |
|
|
|
114 |
|
|
/* Pickup the contents of the FAT cache entry. */ |
115 |
|
444877 |
cluster = media_ptr -> fx_media_fat_cache[index].fx_fat_cache_entry_cluster; |
116 |
|
|
|
117 |
|
|
/* Determine which type of FAT is present. */ |
118 |
|
|
#ifdef FX_ENABLE_EXFAT |
119 |
|
|
if (media_ptr -> fx_media_FAT_type == FX_FAT12) |
120 |
|
|
#else |
121 |
✓✓ |
444877 |
if (media_ptr -> fx_media_12_bit_FAT) |
122 |
|
|
#endif /* FX_ENABLE_EXFAT */ |
123 |
|
|
{ |
124 |
|
|
|
125 |
|
|
/* Calculate the byte offset to the cluster entry. */ |
126 |
|
42809 |
byte_offset = (((ULONG)cluster << 1) + cluster) >> 1; |
127 |
|
|
|
128 |
|
|
/* Calculate the FAT sector the requested FAT entry resides in. */ |
129 |
|
42809 |
FAT_sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) + |
130 |
|
42809 |
(ULONG)media_ptr -> fx_media_reserved_sectors; |
131 |
|
|
|
132 |
|
|
/* Initialize as not written. */ |
133 |
|
42809 |
multi_sector_entry = -1; |
134 |
|
|
|
135 |
|
|
for (;;) |
136 |
|
|
{ |
137 |
|
|
|
138 |
|
|
/* Pickup the FAT sector. */ |
139 |
|
7777 |
status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector, |
140 |
|
50586 |
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); |
141 |
|
|
|
142 |
|
|
/* Determine if an error occurred. */ |
143 |
✓✓ |
50586 |
if (status != FX_SUCCESS) |
144 |
|
|
{ |
145 |
|
|
|
146 |
|
|
/* Return the error status. */ |
147 |
|
19464 |
return(status); |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
/* Determine if a mulit-sector FAT update is present. */ |
151 |
✓✓ |
31122 |
if (multi_sector_entry != -1) |
152 |
|
|
{ |
153 |
|
|
|
154 |
|
|
/* Yes, store the remaining portion of the new FAT entry in the |
155 |
|
|
next FAT sector. */ |
156 |
|
|
|
157 |
|
|
/* Setup a pointer into the buffer. */ |
158 |
|
4191 |
FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer; |
159 |
|
|
|
160 |
|
|
/* Pickup the cluster and next cluster. */ |
161 |
|
4191 |
cluster = (media_ptr -> fx_media_fat_cache[multi_sector_entry].fx_fat_cache_entry_cluster); |
162 |
|
4191 |
next_cluster = media_ptr -> fx_media_fat_cache[multi_sector_entry].fx_fat_cache_entry_value; |
163 |
|
|
|
164 |
|
|
/* Determine if the cluster entry is odd or even. */ |
165 |
✓✓ |
4191 |
if (cluster & 1) |
166 |
|
|
{ |
167 |
|
|
|
168 |
|
|
/* Store the upper 8 bits of the FAT entry. */ |
169 |
|
2215 |
*FAT_ptr = (UCHAR)((next_cluster >> 4) & 0xFF); |
170 |
|
|
} |
171 |
|
|
else |
172 |
|
|
{ |
173 |
|
|
|
174 |
|
|
/* Store the upper 4 bits of the FAT entry. */ |
175 |
|
1976 |
temp = ((UINT)*FAT_ptr) & 0xF0; |
176 |
|
1976 |
*FAT_ptr = (UCHAR)(temp | ((next_cluster >> 8) & 0xF)); |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
/* Clear the multi-sector flag. */ |
180 |
|
4191 |
multi_sector_entry = -1; |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
/* Loop through the remainder of the cache to check for multiple entries |
184 |
|
|
within the same FAT sector being written out. */ |
185 |
✓✓ |
1943515 |
for (i = index; i < FX_MAX_FAT_CACHE; i++) |
186 |
|
|
{ |
187 |
|
|
|
188 |
|
|
/* Is the cache entry dirty? */ |
189 |
✓✓ |
1912393 |
if ((media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_dirty) == 0) |
190 |
|
|
{ |
191 |
|
|
|
192 |
|
|
/* Not dirty, does not need to be flushed. */ |
193 |
|
566837 |
continue; |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
/* Isolate the cluster. */ |
197 |
|
1345556 |
cluster = (media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_cluster); |
198 |
|
|
|
199 |
|
|
/* Calculate the byte offset to the cluster entry. */ |
200 |
|
1345556 |
byte_offset = (((ULONG)cluster << 1) + cluster) >> 1; |
201 |
|
|
|
202 |
|
|
/* Pickup the sector. */ |
203 |
|
1345556 |
sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) + |
204 |
|
1345556 |
(ULONG)media_ptr -> fx_media_reserved_sectors; |
205 |
|
|
|
206 |
|
|
/* Is it the current FAT sector? */ |
207 |
✓✓ |
1345556 |
if (sector != FAT_sector) |
208 |
|
|
{ |
209 |
|
|
|
210 |
|
|
/* Different FAT sector - not in this pass of the loop. */ |
211 |
|
346287 |
continue; |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
/* Pickup new value for this FAT entry. */ |
215 |
|
999269 |
next_cluster = media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_value; |
216 |
|
|
|
217 |
|
|
/* Now calculate the byte offset into this FAT sector. */ |
218 |
|
999269 |
byte_offset = byte_offset - |
219 |
|
999269 |
((FAT_sector - (ULONG)media_ptr -> fx_media_reserved_sectors) * |
220 |
|
999269 |
media_ptr -> fx_media_bytes_per_sector); |
221 |
|
|
|
222 |
|
|
/* Determine if we are now past the end of the FAT buffer in memory. */ |
223 |
✓✓ |
999269 |
if (byte_offset == (ULONG)(media_ptr -> fx_media_bytes_per_sector - 1)) |
224 |
|
|
{ |
225 |
|
|
|
226 |
|
|
/* Yes, we need to read the next sector */ |
227 |
|
7777 |
multi_sector_entry = (INT)i; |
228 |
|
|
} |
229 |
|
|
|
230 |
|
|
/* Setup a pointer into the buffer. */ |
231 |
|
999269 |
FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer + (UINT)byte_offset; |
232 |
|
|
|
233 |
|
|
/* Clear the dirty flag. */ |
234 |
|
999269 |
media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_dirty = 0; |
235 |
|
|
|
236 |
|
|
/* Determine if the cluster entry is odd or even. */ |
237 |
✓✓ |
999269 |
if (cluster & 1) |
238 |
|
|
{ |
239 |
|
|
|
240 |
|
|
/* Odd cluster number. */ |
241 |
|
|
|
242 |
|
|
/* Pickup the upper nibble of the FAT entry. */ |
243 |
|
|
|
244 |
|
|
/* First, set the lower nibble of the FAT entry. */ |
245 |
|
500112 |
temp = (((UINT)*FAT_ptr) & 0x0F); |
246 |
|
500112 |
*FAT_ptr = (UCHAR)(temp | ((next_cluster << 4) & 0xF0)); |
247 |
|
|
|
248 |
|
|
/* Determine if this is a mulit-sector entry. */ |
249 |
✓✓ |
500112 |
if ((multi_sector_entry) == (INT)i) |
250 |
|
|
{ |
251 |
|
|
|
252 |
|
|
/* Yes, requires multiple sector - will write rest of the part later. */ |
253 |
|
4143 |
continue; |
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
/* Move to the next byte of the FAT entry. */ |
257 |
|
495969 |
FAT_ptr++; |
258 |
|
|
|
259 |
|
|
/* Store the upper 8 bits of the FAT entry. */ |
260 |
|
495969 |
*FAT_ptr = (UCHAR)((next_cluster >> 4) & 0xFF); |
261 |
|
|
} |
262 |
|
|
else |
263 |
|
|
{ |
264 |
|
|
|
265 |
|
|
/* Even cluster number. */ |
266 |
|
|
|
267 |
|
|
/* Store the lower byte of the FAT entry. */ |
268 |
|
499157 |
*FAT_ptr = (UCHAR)(next_cluster & 0xFF); |
269 |
|
|
|
270 |
|
|
/* Determine if this is a mulit-sector entry. */ |
271 |
✓✓ |
499157 |
if ((multi_sector_entry) == (INT)i) |
272 |
|
|
{ |
273 |
|
|
|
274 |
|
|
/* Yes, requires multiple sector - will write rest of the part later. */ |
275 |
|
3634 |
continue; |
276 |
|
|
} |
277 |
|
|
|
278 |
|
|
/* Move to the next nibble of the FAT entry. */ |
279 |
|
495523 |
FAT_ptr++; |
280 |
|
|
|
281 |
|
|
/* Store the upper 4 bits of the FAT entry. */ |
282 |
|
495523 |
temp = ((UINT)*FAT_ptr) & 0xF0; |
283 |
|
495523 |
*FAT_ptr = (UCHAR)(temp | ((next_cluster >> 8) & 0xF)); |
284 |
|
|
} |
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
/* First, write out the current sector. */ |
288 |
|
31122 |
status = _fx_utility_logical_sector_write(media_ptr, (ULONG64) FAT_sector, |
289 |
|
31122 |
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); |
290 |
|
|
/* Determine if an error occurred. */ |
291 |
✓✓ |
31122 |
if (status != FX_SUCCESS) |
292 |
|
|
{ |
293 |
|
|
|
294 |
|
|
/* Return the error status. */ |
295 |
|
1 |
return(status); |
296 |
|
|
} |
297 |
|
|
|
298 |
|
|
/* Mark the FAT sector update bit map to indicate this sector has been written. */ |
299 |
✓✓ |
31121 |
if (media_ptr -> fx_media_sectors_per_FAT % (FX_FAT_MAP_SIZE << 3) == 0) |
300 |
|
|
{ |
301 |
|
30762 |
sectors_per_bit = (UCHAR)((UINT)media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3)); |
302 |
|
|
} |
303 |
|
|
else |
304 |
|
|
{ |
305 |
|
359 |
sectors_per_bit = (UCHAR)((UINT)media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3) + 1); |
306 |
|
|
} |
307 |
|
|
|
308 |
|
|
/* Check for invalid value. */ |
309 |
✓✓ |
31121 |
if (sectors_per_bit == 0) |
310 |
|
|
{ |
311 |
|
|
|
312 |
|
|
/* Invalid media, return error. */ |
313 |
|
1 |
return(FX_MEDIA_INVALID); |
314 |
|
|
} |
315 |
|
|
|
316 |
|
31120 |
ind = ((FAT_sector - media_ptr -> fx_media_reserved_sectors) / sectors_per_bit) >> 3; |
317 |
|
31120 |
media_ptr -> fx_media_fat_secondary_update_map[ind] = |
318 |
|
31120 |
(UCHAR)((INT)media_ptr -> fx_media_fat_secondary_update_map[ind] |
319 |
|
31120 |
| (1 <<(((FAT_sector - media_ptr -> fx_media_reserved_sectors) / sectors_per_bit) & 7))); |
320 |
|
|
|
321 |
|
|
/* Determine if the multi-sector flag is set. */ |
322 |
✓✓ |
31120 |
if (multi_sector_entry != -1) |
323 |
|
|
{ |
324 |
|
|
|
325 |
|
|
/* Yes, position to the next sector and read it in. */ |
326 |
|
7777 |
FAT_sector++; |
327 |
|
|
} |
328 |
|
|
else |
329 |
|
|
{ |
330 |
|
|
|
331 |
|
|
/* No, we are finished with this loop. */ |
332 |
|
23343 |
break; |
333 |
|
|
} |
334 |
|
|
} |
335 |
|
|
} |
336 |
|
|
#ifdef FX_ENABLE_EXFAT |
337 |
|
|
else if (media_ptr -> fx_media_FAT_type == FX_FAT16) |
338 |
|
|
#else |
339 |
✓✓ |
402068 |
else if (!media_ptr -> fx_media_32_bit_FAT) |
340 |
|
|
#endif /* FX_ENABLE_EXFAT */ |
341 |
|
|
{ |
342 |
|
|
|
343 |
|
|
/* 16-bit FAT is present. */ |
344 |
|
|
|
345 |
|
|
/* Calculate the byte offset to the cluster entry. */ |
346 |
|
322340 |
byte_offset = (((ULONG)cluster) << 1); |
347 |
|
|
|
348 |
|
|
/* Calculate the FAT sector the requested FAT entry resides in. */ |
349 |
|
322340 |
FAT_sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) + |
350 |
|
322340 |
(ULONG)media_ptr -> fx_media_reserved_sectors; |
351 |
|
|
|
352 |
|
|
/* Read the FAT sector. */ |
353 |
|
322340 |
status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector, |
354 |
|
322340 |
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); |
355 |
|
|
|
356 |
|
|
/* Determine if an error occurred. */ |
357 |
✓✓ |
322340 |
if (status != FX_SUCCESS) |
358 |
|
|
{ |
359 |
|
|
|
360 |
|
|
/* Return the error status. */ |
361 |
|
19548 |
return(status); |
362 |
|
|
} |
363 |
|
|
|
364 |
|
|
/* Loop through the remainder of the cache to check for multiple entries |
365 |
|
|
within the same FAT sector being written out. */ |
366 |
✓✓ |
11884775 |
for (i = index; i < FX_MAX_FAT_CACHE; i++) |
367 |
|
|
{ |
368 |
|
|
|
369 |
|
|
/* Determine if the entry is dirty. */ |
370 |
✓✓ |
11581983 |
if (media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_dirty == 0) |
371 |
|
|
{ |
372 |
|
|
|
373 |
|
|
/* Not dirty, does not need to be flushed. */ |
374 |
|
9622433 |
continue; |
375 |
|
|
} |
376 |
|
|
|
377 |
|
|
/* Isolate the cluster. */ |
378 |
|
1959550 |
cluster = (media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_cluster); |
379 |
|
|
|
380 |
|
|
/* Calculate the byte offset to the cluster entry. */ |
381 |
|
1959550 |
byte_offset = (((ULONG)cluster) * 2); |
382 |
|
|
|
383 |
|
|
/* Pickup the sector. */ |
384 |
|
1959550 |
sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) + |
385 |
|
1959550 |
(ULONG)media_ptr -> fx_media_reserved_sectors; |
386 |
|
|
|
387 |
|
|
/* Is it the current FAT sector? */ |
388 |
✓✓ |
1959550 |
if (sector != FAT_sector) |
389 |
|
|
{ |
390 |
|
|
|
391 |
|
|
/* Different FAT sector - not in this pass of the loop. */ |
392 |
|
561471 |
continue; |
393 |
|
|
} |
394 |
|
|
|
395 |
|
|
/* Now calculate the byte offset into this FAT sector. */ |
396 |
|
1398079 |
byte_offset = byte_offset - |
397 |
|
1398079 |
((FAT_sector - (ULONG)media_ptr -> fx_media_reserved_sectors) * |
398 |
|
1398079 |
media_ptr -> fx_media_bytes_per_sector); |
399 |
|
|
|
400 |
|
|
/* Setup a pointer into the buffer. */ |
401 |
|
1398079 |
FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer + (UINT)byte_offset; |
402 |
|
|
|
403 |
|
|
/* Pickup new value for this FAT entry. */ |
404 |
|
1398079 |
next_cluster = media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_value; |
405 |
|
|
|
406 |
|
|
/* Store the FAT entry. */ |
407 |
|
1398079 |
_fx_utility_16_unsigned_write(FAT_ptr, (UINT)next_cluster); |
408 |
|
|
|
409 |
|
|
/* Clear the dirty flag. */ |
410 |
|
1398079 |
media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_dirty = 0; |
411 |
|
|
} |
412 |
|
|
|
413 |
|
|
/* Write the last written FAT sector out. */ |
414 |
|
302792 |
status = _fx_utility_logical_sector_write(media_ptr, (ULONG64) FAT_sector, |
415 |
|
302792 |
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); |
416 |
|
|
|
417 |
|
|
/* Determine if an error occurred. */ |
418 |
✓✓ |
302792 |
if (status != FX_SUCCESS) |
419 |
|
|
{ |
420 |
|
|
/* Return the error status. */ |
421 |
|
1 |
return(status); |
422 |
|
|
} |
423 |
|
|
|
424 |
|
|
/* Mark the FAT sector update bit map to indicate this sector has been |
425 |
|
|
written. */ |
426 |
✓✓ |
302791 |
if (media_ptr -> fx_media_sectors_per_FAT % (FX_FAT_MAP_SIZE << 3) == 0) |
427 |
|
|
{ |
428 |
|
3 |
sectors_per_bit = (UCHAR)(media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3)); |
429 |
|
|
} |
430 |
|
|
else |
431 |
|
|
{ |
432 |
|
302788 |
sectors_per_bit = (UCHAR)((media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3)) + 1); |
433 |
|
|
} |
434 |
|
302791 |
ind = ((FAT_sector - media_ptr -> fx_media_reserved_sectors) / sectors_per_bit) >> 3; |
435 |
|
302791 |
media_ptr -> fx_media_fat_secondary_update_map[ind] = |
436 |
|
302791 |
(UCHAR)((INT)media_ptr -> fx_media_fat_secondary_update_map[ind] |
437 |
|
302791 |
| (1 <<(((FAT_sector - media_ptr -> fx_media_reserved_sectors) / sectors_per_bit) & 7))); |
438 |
|
|
} |
439 |
|
|
else |
440 |
|
|
{ |
441 |
|
|
|
442 |
|
|
/* 32-bit FAT or exFAT are present. */ |
443 |
|
|
|
444 |
|
|
/* Calculate the byte offset to the cluster entry. */ |
445 |
|
79728 |
byte_offset = (((ULONG)cluster) * 4); |
446 |
|
|
|
447 |
|
|
/* Calculate the FAT sector the requested FAT entry resides in. */ |
448 |
|
79728 |
FAT_sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) + |
449 |
|
79728 |
(ULONG)media_ptr -> fx_media_reserved_sectors; |
450 |
|
|
|
451 |
|
|
/* Read the FAT sector. */ |
452 |
|
79728 |
status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector, |
453 |
|
79728 |
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); |
454 |
|
|
|
455 |
|
|
/* Determine if an error occurred. */ |
456 |
✓✓ |
79728 |
if (status != FX_SUCCESS) |
457 |
|
|
{ |
458 |
|
|
|
459 |
|
|
/* Return the error status. */ |
460 |
|
28724 |
return(status); |
461 |
|
|
} |
462 |
|
|
|
463 |
|
|
/* Loop through the remainder of the cache to check for multiple entries |
464 |
|
|
within the same FAT sector being written out. */ |
465 |
✓✓ |
2997881 |
for (i = index; i < FX_MAX_FAT_CACHE; i++) |
466 |
|
|
{ |
467 |
|
|
|
468 |
|
|
/* Determine if the entry is dirty. */ |
469 |
✓✓ |
2946877 |
if (media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_dirty == 0) |
470 |
|
|
{ |
471 |
|
|
|
472 |
|
|
/* Not dirty, does not need to be flushed. */ |
473 |
|
867904 |
continue; |
474 |
|
|
} |
475 |
|
|
|
476 |
|
|
/* Isolate the cluster. */ |
477 |
|
2078973 |
cluster = (media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_cluster); |
478 |
|
|
|
479 |
|
|
/* Calculate the byte offset to the cluster entry. */ |
480 |
|
2078973 |
byte_offset = (((ULONG)cluster) * 4); |
481 |
|
|
|
482 |
|
|
/* Pickup the sector. */ |
483 |
|
2078973 |
sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) + |
484 |
|
2078973 |
(ULONG)media_ptr -> fx_media_reserved_sectors; |
485 |
|
|
|
486 |
|
|
/* Is it the current FAT sector? */ |
487 |
✓✓ |
2078973 |
if (sector != FAT_sector) |
488 |
|
|
{ |
489 |
|
|
|
490 |
|
|
/* Different FAT sector - not in this pass of the loop. */ |
491 |
|
1004363 |
continue; |
492 |
|
|
} |
493 |
|
|
|
494 |
|
|
/* Now calculate the byte offset into this FAT sector. */ |
495 |
|
1074610 |
byte_offset = byte_offset - |
496 |
|
1074610 |
((FAT_sector - (ULONG)media_ptr -> fx_media_reserved_sectors) * |
497 |
|
1074610 |
media_ptr -> fx_media_bytes_per_sector); |
498 |
|
|
|
499 |
|
|
/* Setup a pointer into the buffer. */ |
500 |
|
1074610 |
FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer + (UINT)byte_offset; |
501 |
|
|
|
502 |
|
|
/* Pickup new value for this FAT entry. */ |
503 |
|
1074610 |
next_cluster = media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_value; |
504 |
|
|
|
505 |
|
|
/* Store the FAT entry. */ |
506 |
|
1074610 |
_fx_utility_32_unsigned_write(FAT_ptr, next_cluster); |
507 |
|
|
|
508 |
|
|
/* Clear the dirty flag. */ |
509 |
|
1074610 |
media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_dirty = 0; |
510 |
|
|
} |
511 |
|
|
|
512 |
|
|
/* Write the last written FAT sector out. */ |
513 |
|
51004 |
status = _fx_utility_logical_sector_write(media_ptr, (ULONG64) FAT_sector, |
514 |
|
51004 |
media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); |
515 |
|
|
|
516 |
|
|
/* Determine if an error occurred. */ |
517 |
✓✓ |
51004 |
if (status != FX_SUCCESS) |
518 |
|
|
{ |
519 |
|
|
|
520 |
|
|
/* Return the error status. */ |
521 |
|
1 |
return(status); |
522 |
|
|
} |
523 |
|
|
|
524 |
|
|
#ifdef FX_ENABLE_EXFAT |
525 |
|
|
/* We are not using fx_media_fat_secondary_update_map for exFAT. */ |
526 |
|
|
if (media_ptr -> fx_media_FAT_type == FX_FAT32) |
527 |
|
|
{ |
528 |
|
|
#endif /* FX_ENABLE_EXFAT */ |
529 |
|
|
|
530 |
|
|
/* Mark the FAT sector update bit map to indicate this sector has been |
531 |
|
|
written. */ |
532 |
✓✓ |
51003 |
if (media_ptr -> fx_media_sectors_per_FAT % (FX_FAT_MAP_SIZE << 3) == 0) |
533 |
|
|
{ |
534 |
|
46254 |
sectors_per_bit = (UCHAR)(media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3)); |
535 |
|
|
} |
536 |
|
|
else |
537 |
|
|
{ |
538 |
|
4749 |
sectors_per_bit = (UCHAR)((media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3)) + 1); |
539 |
|
|
} |
540 |
|
51003 |
ind = ((FAT_sector - media_ptr -> fx_media_reserved_sectors) / sectors_per_bit) >> 3; |
541 |
|
51003 |
media_ptr -> fx_media_fat_secondary_update_map[ind] = |
542 |
|
51003 |
(UCHAR)((INT)media_ptr -> fx_media_fat_secondary_update_map[ind] |
543 |
|
51003 |
| (1 <<(((FAT_sector - media_ptr -> fx_media_reserved_sectors) / sectors_per_bit) & 7))); |
544 |
|
|
#ifdef FX_ENABLE_EXFAT |
545 |
|
|
} |
546 |
|
|
#endif /* FX_ENABLE_EXFAT */ |
547 |
|
|
} |
548 |
|
|
} |
549 |
|
|
|
550 |
|
|
#ifdef FX_ENABLE_FAULT_TOLERANT |
551 |
|
|
|
552 |
|
|
/* While fault_tolerant is enabled, this function will be called before the return of all APIs. */ |
553 |
|
|
if (media_ptr -> fx_media_fault_tolerant_enabled) |
554 |
|
|
{ |
555 |
|
|
|
556 |
|
|
/* Delete the record of FAT sector since all FAT entries are flushed. */ |
557 |
|
|
media_ptr -> fx_media_fault_tolerant_cached_FAT_sector = 0; |
558 |
|
|
} |
559 |
|
|
#endif /* FX_ENABLE_FAULT_TOLERANT */ |
560 |
|
|
|
561 |
|
|
/* Return successful status. */ |
562 |
|
404078 |
return(FX_SUCCESS); |
563 |
|
|
} |
564 |
|
|
|