fx_utility_FAT_flush.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*************************************************************************** | ||
| 2 | * Copyright (c) 2024 Microsoft Corporation | ||
| 3 | * Copyright (c) 2026-present Eclipse ThreadX contributors | ||
| 4 | * | ||
| 5 | * This program and the accompanying materials are made available under the | ||
| 6 | * terms of the MIT License which is available at | ||
| 7 | * https://opensource.org/licenses/MIT. | ||
| 8 | * | ||
| 9 | * SPDX-License-Identifier: MIT | ||
| 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 | /**************************************************************************/ | ||
| 69 | 177790692 | UINT _fx_utility_FAT_flush(FX_MEDIA *media_ptr) | |
| 70 | { | ||
| 71 | |||
| 72 | ULONG FAT_sector; | ||
| 73 | ULONG byte_offset; | ||
| 74 | UCHAR *FAT_ptr; | ||
| 75 | UINT temp, i; | ||
| 76 | UINT status, index, ind; | ||
| 77 | ULONG cluster, next_cluster; | ||
| 78 | UCHAR sectors_per_bit; | ||
| 79 | INT multi_sector_entry; | ||
| 80 | ULONG sector; | ||
| 81 | |||
| 82 | #ifndef FX_MEDIA_STATISTICS_DISABLE | ||
| 83 | /* Increment the number of cache flush requests. */ | ||
| 84 | 177790692 | media_ptr -> fx_media_fat_cache_flushes++; | |
| 85 | #endif | ||
| 86 | |||
| 87 | /* Loop through the media's FAT cache and flush out dirty entries. */ | ||
| 88 |
2/2✓ Branch 84 → 3 taken 11347890279 times.
✓ Branch 84 → 85 taken 177252738 times.
|
11525143017 | for (index = 0; index < FX_MAX_FAT_CACHE; index++) |
| 89 | { | ||
| 90 | |||
| 91 | /* Determine if the entry is dirty. */ | ||
| 92 |
2/2✓ Branch 3 → 4 taken 11334096234 times.
✓ Branch 3 → 5 taken 13794045 times.
|
11347890279 | if ((media_ptr -> fx_media_fat_cache[index].fx_fat_cache_entry_dirty) == 0) |
| 93 | { | ||
| 94 | |||
| 95 | /* No, just advance to the next entry. */ | ||
| 96 | 11334096234 | continue; | |
| 97 | } | ||
| 98 | |||
| 99 | /* Otherwise, the entry is indeed dirty and must be flushed out. Process | ||
| 100 | relative to the type of FAT that is being used. */ | ||
| 101 | |||
| 102 | /* Pickup the contents of the FAT cache entry. */ | ||
| 103 | 13794045 | cluster = media_ptr -> fx_media_fat_cache[index].fx_fat_cache_entry_cluster; | |
| 104 | |||
| 105 | /* Determine which type of FAT is present. */ | ||
| 106 |
2/2✓ Branch 5 → 6 taken 1034395 times.
✓ Branch 5 → 44 taken 12759650 times.
|
13794045 | if (media_ptr -> fx_media_12_bit_FAT) |
| 107 | { | ||
| 108 | |||
| 109 | /* Calculate the byte offset to the cluster entry. */ | ||
| 110 | 1034395 | byte_offset = (((ULONG)cluster << 1) + cluster) >> 1; | |
| 111 | |||
| 112 | /* Calculate the FAT sector the requested FAT entry resides in. */ | ||
| 113 | 1034395 | FAT_sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) + | |
| 114 | 1034395 | (ULONG)media_ptr -> fx_media_reserved_sectors; | |
| 115 | |||
| 116 | /* Initialize as not written. */ | ||
| 117 | 1034395 | multi_sector_entry = -1; | |
| 118 | |||
| 119 | for (;;) | ||
| 120 | { | ||
| 121 | |||
| 122 | /* Pickup the FAT sector. */ | ||
| 123 | 44481 | status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector, | |
| 124 | 1078876 | media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); | |
| 125 | |||
| 126 | /* Determine if an error occurred. */ | ||
| 127 |
2/2✓ Branch 8 → 9 taken 115288 times.
✓ Branch 8 → 10 taken 963588 times.
|
1078876 | if (status != FX_SUCCESS) |
| 128 | { | ||
| 129 | |||
| 130 | /* Return the error status. */ | ||
| 131 | 115288 | return(status); | |
| 132 | } | ||
| 133 | |||
| 134 | /* Determine if a mulit-sector FAT update is present. */ | ||
| 135 |
2/2✓ Branch 10 → 11 taken 30135 times.
✓ Branch 10 → 15 taken 933453 times.
|
963588 | if (multi_sector_entry != -1) |
| 136 | { | ||
| 137 | |||
| 138 | /* Yes, store the remaining portion of the new FAT entry in the | ||
| 139 | next FAT sector. */ | ||
| 140 | |||
| 141 | /* Setup a pointer into the buffer. */ | ||
| 142 | 30135 | FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer; | |
| 143 | |||
| 144 | /* Pickup the cluster and next cluster. */ | ||
| 145 | 30135 | cluster = (media_ptr -> fx_media_fat_cache[multi_sector_entry].fx_fat_cache_entry_cluster); | |
| 146 | 30135 | next_cluster = media_ptr -> fx_media_fat_cache[multi_sector_entry].fx_fat_cache_entry_value; | |
| 147 | |||
| 148 | /* Determine if the cluster entry is odd or even. */ | ||
| 149 |
2/2✓ Branch 11 → 12 taken 16093 times.
✓ Branch 11 → 13 taken 14042 times.
|
30135 | if (cluster & 1) |
| 150 | { | ||
| 151 | |||
| 152 | /* Store the upper 8 bits of the FAT entry. */ | ||
| 153 | 16093 | *FAT_ptr = (UCHAR)((next_cluster >> 4) & 0xFF); | |
| 154 | } | ||
| 155 | else | ||
| 156 | { | ||
| 157 | |||
| 158 | /* Store the upper 4 bits of the FAT entry. */ | ||
| 159 | 14042 | temp = ((UINT)*FAT_ptr) & 0xF0; | |
| 160 | 14042 | *FAT_ptr = (UCHAR)(temp | ((next_cluster >> 8) & 0xF)); | |
| 161 | } | ||
| 162 | |||
| 163 | /* Clear the multi-sector flag. */ | ||
| 164 | 30135 | multi_sector_entry = -1; | |
| 165 | } | ||
| 166 | |||
| 167 | /* Loop through the remainder of the cache to check for multiple entries | ||
| 168 | within the same FAT sector being written out. */ | ||
| 169 |
2/2✓ Branch 30 → 16 taken 42572772 times.
✓ Branch 30 → 31 taken 963588 times.
|
43536360 | for (i = index; i < FX_MAX_FAT_CACHE; i++) |
| 170 | { | ||
| 171 | |||
| 172 | /* Is the cache entry dirty? */ | ||
| 173 |
2/2✓ Branch 16 → 17 taken 31511203 times.
✓ Branch 16 → 18 taken 11061569 times.
|
42572772 | if ((media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_dirty) == 0) |
| 174 | { | ||
| 175 | |||
| 176 | /* Not dirty, does not need to be flushed. */ | ||
| 177 | 31511203 | continue; | |
| 178 | } | ||
| 179 | |||
| 180 | /* Isolate the cluster. */ | ||
| 181 | 11061569 | cluster = (media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_cluster); | |
| 182 | |||
| 183 | /* Calculate the byte offset to the cluster entry. */ | ||
| 184 | 11061569 | byte_offset = (((ULONG)cluster << 1) + cluster) >> 1; | |
| 185 | |||
| 186 | /* Pickup the sector. */ | ||
| 187 | 11061569 | sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) + | |
| 188 | 11061569 | (ULONG)media_ptr -> fx_media_reserved_sectors; | |
| 189 | |||
| 190 | /* Is it the current FAT sector? */ | ||
| 191 |
2/2✓ Branch 18 → 19 taken 2435953 times.
✓ Branch 18 → 20 taken 8625616 times.
|
11061569 | if (sector != FAT_sector) |
| 192 | { | ||
| 193 | |||
| 194 | /* Different FAT sector - not in this pass of the loop. */ | ||
| 195 | 2435953 | continue; | |
| 196 | } | ||
| 197 | |||
| 198 | /* Pickup new value for this FAT entry. */ | ||
| 199 | 8625616 | next_cluster = media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_value; | |
| 200 | |||
| 201 | /* Now calculate the byte offset into this FAT sector. */ | ||
| 202 | 8625616 | byte_offset = byte_offset - | |
| 203 | 8625616 | ((FAT_sector - (ULONG)media_ptr -> fx_media_reserved_sectors) * | |
| 204 | 8625616 | media_ptr -> fx_media_bytes_per_sector); | |
| 205 | |||
| 206 | /* Determine if we are now past the end of the FAT buffer in memory. */ | ||
| 207 |
2/2✓ Branch 20 → 21 taken 55433 times.
✓ Branch 20 → 22 taken 8570183 times.
|
8625616 | if (byte_offset == (ULONG)(media_ptr -> fx_media_bytes_per_sector - 1)) |
| 208 | { | ||
| 209 | |||
| 210 | /* Yes, we need to read the next sector */ | ||
| 211 | 55433 | multi_sector_entry = (INT)i; | |
| 212 | } | ||
| 213 | |||
| 214 | /* Setup a pointer into the buffer. */ | ||
| 215 | 8625616 | FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer + (UINT)byte_offset; | |
| 216 | |||
| 217 | /* Clear the dirty flag. */ | ||
| 218 | 8625616 | media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_dirty = 0; | |
| 219 | |||
| 220 | /* Determine if the cluster entry is odd or even. */ | ||
| 221 |
2/2✓ Branch 22 → 23 taken 4316451 times.
✓ Branch 22 → 26 taken 4309165 times.
|
8625616 | if (cluster & 1) |
| 222 | { | ||
| 223 | |||
| 224 | /* Odd cluster number. */ | ||
| 225 | |||
| 226 | /* Pickup the upper nibble of the FAT entry. */ | ||
| 227 | |||
| 228 | /* First, set the lower nibble of the FAT entry. */ | ||
| 229 | 4316451 | temp = (((UINT)*FAT_ptr) & 0x0F); | |
| 230 | 4316451 | *FAT_ptr = (UCHAR)(temp | ((next_cluster << 4) & 0xF0)); | |
| 231 | |||
| 232 | /* Determine if this is a mulit-sector entry. */ | ||
| 233 |
2/2✓ Branch 23 → 24 taken 29531 times.
✓ Branch 23 → 25 taken 4286920 times.
|
4316451 | if ((multi_sector_entry) == (INT)i) |
| 234 | { | ||
| 235 | |||
| 236 | /* Yes, requires multiple sector - will write rest of the part later. */ | ||
| 237 | 29531 | continue; | |
| 238 | } | ||
| 239 | |||
| 240 | /* Move to the next byte of the FAT entry. */ | ||
| 241 | 4286920 | FAT_ptr++; | |
| 242 | |||
| 243 | /* Store the upper 8 bits of the FAT entry. */ | ||
| 244 | 4286920 | *FAT_ptr = (UCHAR)((next_cluster >> 4) & 0xFF); | |
| 245 | } | ||
| 246 | else | ||
| 247 | { | ||
| 248 | |||
| 249 | /* Even cluster number. */ | ||
| 250 | |||
| 251 | /* Store the lower byte of the FAT entry. */ | ||
| 252 | 4309165 | *FAT_ptr = (UCHAR)(next_cluster & 0xFF); | |
| 253 | |||
| 254 | /* Determine if this is a mulit-sector entry. */ | ||
| 255 |
2/2✓ Branch 26 → 27 taken 25902 times.
✓ Branch 26 → 28 taken 4283263 times.
|
4309165 | if ((multi_sector_entry) == (INT)i) |
| 256 | { | ||
| 257 | |||
| 258 | /* Yes, requires multiple sector - will write rest of the part later. */ | ||
| 259 | 25902 | continue; | |
| 260 | } | ||
| 261 | |||
| 262 | /* Move to the next nibble of the FAT entry. */ | ||
| 263 | 4283263 | FAT_ptr++; | |
| 264 | |||
| 265 | /* Store the upper 4 bits of the FAT entry. */ | ||
| 266 | 4283263 | temp = ((UINT)*FAT_ptr) & 0xF0; | |
| 267 | 4283263 | *FAT_ptr = (UCHAR)(temp | ((next_cluster >> 8) & 0xF)); | |
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | /* First, write out the current sector. */ | ||
| 272 | 963588 | status = _fx_utility_logical_sector_write(media_ptr, (ULONG64) FAT_sector, | |
| 273 | 963588 | media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); | |
| 274 | /* Determine if an error occurred. */ | ||
| 275 |
2/2✓ Branch 32 → 33 taken 36415 times.
✓ Branch 32 → 34 taken 927121 times.
|
963536 | if (status != FX_SUCCESS) |
| 276 | { | ||
| 277 | |||
| 278 | /* Return the error status. */ | ||
| 279 | 36415 | return(status); | |
| 280 | } | ||
| 281 | |||
| 282 | /* Mark the FAT sector update bit map to indicate this sector has been written. */ | ||
| 283 |
2/2✓ Branch 34 → 35 taken 191154 times.
✓ Branch 34 → 36 taken 735967 times.
|
927121 | if (media_ptr -> fx_media_sectors_per_FAT % (FX_FAT_MAP_SIZE << 3) == 0) |
| 284 | { | ||
| 285 | 191154 | sectors_per_bit = (UCHAR)((UINT)media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3)); | |
| 286 | } | ||
| 287 | else | ||
| 288 | { | ||
| 289 | 735967 | sectors_per_bit = (UCHAR)((UINT)media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3) + 1); | |
| 290 | } | ||
| 291 | |||
| 292 | /* Check for invalid value. */ | ||
| 293 |
2/2✓ Branch 37 → 38 taken 9 times.
✓ Branch 37 → 39 taken 927112 times.
|
927121 | if (sectors_per_bit == 0) |
| 294 | { | ||
| 295 | |||
| 296 | /* Invalid media, return error. */ | ||
| 297 | 9 | return(FX_MEDIA_INVALID); | |
| 298 | } | ||
| 299 | |||
| 300 | 927112 | ind = ((FAT_sector - media_ptr -> fx_media_reserved_sectors) / sectors_per_bit) >> 3; | |
| 301 | 927112 | media_ptr -> fx_media_fat_secondary_update_map[ind] = | |
| 302 | 927112 | (UCHAR)((INT)media_ptr -> fx_media_fat_secondary_update_map[ind] | |
| 303 | 927112 | | (1 <<(((FAT_sector - media_ptr -> fx_media_reserved_sectors) / sectors_per_bit) & 7))); | |
| 304 | |||
| 305 | /* Determine if the multi-sector flag is set. */ | ||
| 306 |
2/2✓ Branch 39 → 40 taken 44481 times.
✓ Branch 39 → 41 taken 882631 times.
|
927112 | if (multi_sector_entry != -1) |
| 307 | { | ||
| 308 | |||
| 309 | /* Yes, position to the next sector and read it in. */ | ||
| 310 | 44481 | FAT_sector++; | |
| 311 | } | ||
| 312 | else | ||
| 313 | { | ||
| 314 | |||
| 315 | /* No, we are finished with this loop. */ | ||
| 316 | 882631 | break; | |
| 317 | } | ||
| 318 | } | ||
| 319 | } | ||
| 320 |
2/2✓ Branch 44 → 45 taken 3774740 times.
✓ Branch 44 → 64 taken 8984910 times.
|
12759650 | else if (!media_ptr -> fx_media_32_bit_FAT) |
| 321 | { | ||
| 322 | |||
| 323 | /* 16-bit FAT is present. */ | ||
| 324 | |||
| 325 | /* Calculate the byte offset to the cluster entry. */ | ||
| 326 | 3774740 | byte_offset = (((ULONG)cluster) << 1); | |
| 327 | |||
| 328 | /* Calculate the FAT sector the requested FAT entry resides in. */ | ||
| 329 | 3774740 | FAT_sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) + | |
| 330 | 3774740 | (ULONG)media_ptr -> fx_media_reserved_sectors; | |
| 331 | |||
| 332 | /* Read the FAT sector. */ | ||
| 333 | 3774740 | status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector, | |
| 334 | 3774740 | media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); | |
| 335 | |||
| 336 | /* Determine if an error occurred. */ | ||
| 337 |
2/2✓ Branch 46 → 47 taken 121502 times.
✓ Branch 46 → 48 taken 3653238 times.
|
3774740 | if (status != FX_SUCCESS) |
| 338 | { | ||
| 339 | |||
| 340 | /* Return the error status. */ | ||
| 341 | 121502 | return(status); | |
| 342 | } | ||
| 343 | |||
| 344 | /* Loop through the remainder of the cache to check for multiple entries | ||
| 345 | within the same FAT sector being written out. */ | ||
| 346 |
2/2✓ Branch 56 → 49 taken 145789429 times.
✓ Branch 56 → 57 taken 3653238 times.
|
149442667 | for (i = index; i < FX_MAX_FAT_CACHE; i++) |
| 347 | { | ||
| 348 | |||
| 349 | /* Determine if the entry is dirty. */ | ||
| 350 |
2/2✓ Branch 49 → 50 taken 126200672 times.
✓ Branch 49 → 51 taken 19588757 times.
|
145789429 | if (media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_dirty == 0) |
| 351 | { | ||
| 352 | |||
| 353 | /* Not dirty, does not need to be flushed. */ | ||
| 354 | 126200672 | continue; | |
| 355 | } | ||
| 356 | |||
| 357 | /* Isolate the cluster. */ | ||
| 358 | 19588757 | cluster = (media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_cluster); | |
| 359 | |||
| 360 | /* Calculate the byte offset to the cluster entry. */ | ||
| 361 | 19588757 | byte_offset = (((ULONG)cluster) * 2); | |
| 362 | |||
| 363 | /* Pickup the sector. */ | ||
| 364 | 19588757 | sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) + | |
| 365 | 19588757 | (ULONG)media_ptr -> fx_media_reserved_sectors; | |
| 366 | |||
| 367 | /* Is it the current FAT sector? */ | ||
| 368 |
2/2✓ Branch 51 → 52 taken 4015589 times.
✓ Branch 51 → 53 taken 15573168 times.
|
19588757 | if (sector != FAT_sector) |
| 369 | { | ||
| 370 | |||
| 371 | /* Different FAT sector - not in this pass of the loop. */ | ||
| 372 | 4015589 | continue; | |
| 373 | } | ||
| 374 | |||
| 375 | /* Now calculate the byte offset into this FAT sector. */ | ||
| 376 | 15573168 | byte_offset = byte_offset - | |
| 377 | 15573168 | ((FAT_sector - (ULONG)media_ptr -> fx_media_reserved_sectors) * | |
| 378 | 15573168 | media_ptr -> fx_media_bytes_per_sector); | |
| 379 | |||
| 380 | /* Setup a pointer into the buffer. */ | ||
| 381 | 15573168 | FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer + (UINT)byte_offset; | |
| 382 | |||
| 383 | /* Pickup new value for this FAT entry. */ | ||
| 384 | 15573168 | next_cluster = media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_value; | |
| 385 | |||
| 386 | /* Store the FAT entry. */ | ||
| 387 | 15573168 | _fx_utility_16_unsigned_write(FAT_ptr, (UINT)next_cluster); | |
| 388 | |||
| 389 | /* Clear the dirty flag. */ | ||
| 390 | 15573168 | media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_dirty = 0; | |
| 391 | } | ||
| 392 | |||
| 393 | /* Write the last written FAT sector out. */ | ||
| 394 | 3653238 | status = _fx_utility_logical_sector_write(media_ptr, (ULONG64) FAT_sector, | |
| 395 | 3653238 | media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); | |
| 396 | |||
| 397 | /* Determine if an error occurred. */ | ||
| 398 |
2/2✓ Branch 58 → 59 taken 39909 times.
✓ Branch 58 → 60 taken 3613281 times.
|
3653190 | if (status != FX_SUCCESS) |
| 399 | { | ||
| 400 | /* Return the error status. */ | ||
| 401 | 39909 | return(status); | |
| 402 | } | ||
| 403 | |||
| 404 | /* Mark the FAT sector update bit map to indicate this sector has been | ||
| 405 | written. */ | ||
| 406 |
2/2✓ Branch 60 → 61 taken 127 times.
✓ Branch 60 → 62 taken 3613154 times.
|
3613281 | if (media_ptr -> fx_media_sectors_per_FAT % (FX_FAT_MAP_SIZE << 3) == 0) |
| 407 | { | ||
| 408 | 127 | sectors_per_bit = (UCHAR)(media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3)); | |
| 409 | } | ||
| 410 | else | ||
| 411 | { | ||
| 412 | 3613154 | sectors_per_bit = (UCHAR)((media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3)) + 1); | |
| 413 | } | ||
| 414 | 3613281 | ind = ((FAT_sector - media_ptr -> fx_media_reserved_sectors) / sectors_per_bit) >> 3; | |
| 415 | 3613281 | media_ptr -> fx_media_fat_secondary_update_map[ind] = | |
| 416 | 3613281 | (UCHAR)((INT)media_ptr -> fx_media_fat_secondary_update_map[ind] | |
| 417 | 3613281 | | (1 <<(((FAT_sector - media_ptr -> fx_media_reserved_sectors) / sectors_per_bit) & 7))); | |
| 418 | } | ||
| 419 | else | ||
| 420 | { | ||
| 421 | |||
| 422 | /* 32-bit FAT is present. */ | ||
| 423 | |||
| 424 | /* Calculate the byte offset to the cluster entry. */ | ||
| 425 | 8984910 | byte_offset = (((ULONG)cluster) * 4); | |
| 426 | |||
| 427 | /* Calculate the FAT sector the requested FAT entry resides in. */ | ||
| 428 | 8984910 | FAT_sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) + | |
| 429 | 8984910 | (ULONG)media_ptr -> fx_media_reserved_sectors; | |
| 430 | |||
| 431 | /* Read the FAT sector. */ | ||
| 432 | 8984910 | status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector, | |
| 433 | 8984910 | media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); | |
| 434 | |||
| 435 | /* Determine if an error occurred. */ | ||
| 436 |
2/2✓ Branch 65 → 66 taken 170760 times.
✓ Branch 65 → 67 taken 8814150 times.
|
8984910 | if (status != FX_SUCCESS) |
| 437 | { | ||
| 438 | |||
| 439 | /* Return the error status. */ | ||
| 440 | 170760 | return(status); | |
| 441 | } | ||
| 442 | |||
| 443 | /* Loop through the remainder of the cache to check for multiple entries | ||
| 444 | within the same FAT sector being written out. */ | ||
| 445 |
2/2✓ Branch 75 → 68 taken 422808354 times.
✓ Branch 75 → 76 taken 8814150 times.
|
431622504 | for (i = index; i < FX_MAX_FAT_CACHE; i++) |
| 446 | { | ||
| 447 | |||
| 448 | /* Determine if the entry is dirty. */ | ||
| 449 |
2/2✓ Branch 68 → 69 taken 332407552 times.
✓ Branch 68 → 70 taken 90400802 times.
|
422808354 | if (media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_dirty == 0) |
| 450 | { | ||
| 451 | |||
| 452 | /* Not dirty, does not need to be flushed. */ | ||
| 453 | 332407552 | continue; | |
| 454 | } | ||
| 455 | |||
| 456 | /* Isolate the cluster. */ | ||
| 457 | 90400802 | cluster = (media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_cluster); | |
| 458 | |||
| 459 | /* Calculate the byte offset to the cluster entry. */ | ||
| 460 | 90400802 | byte_offset = (((ULONG)cluster) * 4); | |
| 461 | |||
| 462 | /* Pickup the sector. */ | ||
| 463 | 90400802 | sector = (byte_offset / media_ptr -> fx_media_bytes_per_sector) + | |
| 464 | 90400802 | (ULONG)media_ptr -> fx_media_reserved_sectors; | |
| 465 | |||
| 466 | /* Is it the current FAT sector? */ | ||
| 467 |
2/2✓ Branch 70 → 71 taken 7320544 times.
✓ Branch 70 → 72 taken 83080258 times.
|
90400802 | if (sector != FAT_sector) |
| 468 | { | ||
| 469 | |||
| 470 | /* Different FAT sector - not in this pass of the loop. */ | ||
| 471 | 7320544 | continue; | |
| 472 | } | ||
| 473 | |||
| 474 | /* Now calculate the byte offset into this FAT sector. */ | ||
| 475 | 83080258 | byte_offset = byte_offset - | |
| 476 | 83080258 | ((FAT_sector - (ULONG)media_ptr -> fx_media_reserved_sectors) * | |
| 477 | 83080258 | media_ptr -> fx_media_bytes_per_sector); | |
| 478 | |||
| 479 | /* Setup a pointer into the buffer. */ | ||
| 480 | 83080258 | FAT_ptr = (UCHAR *)media_ptr -> fx_media_memory_buffer + (UINT)byte_offset; | |
| 481 | |||
| 482 | /* Pickup new value for this FAT entry. */ | ||
| 483 | 83080258 | next_cluster = media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_value; | |
| 484 | |||
| 485 | /* Store the FAT entry. */ | ||
| 486 | 83080258 | _fx_utility_32_unsigned_write(FAT_ptr, next_cluster); | |
| 487 | |||
| 488 | /* Clear the dirty flag. */ | ||
| 489 | 83080258 | media_ptr -> fx_media_fat_cache[i].fx_fat_cache_entry_dirty = 0; | |
| 490 | } | ||
| 491 | |||
| 492 | /* Write the last written FAT sector out. */ | ||
| 493 | 8814150 | status = _fx_utility_logical_sector_write(media_ptr, (ULONG64) FAT_sector, | |
| 494 | 8814150 | media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); | |
| 495 | |||
| 496 | /* Determine if an error occurred. */ | ||
| 497 |
2/2✓ Branch 77 → 78 taken 53901 times.
✓ Branch 77 → 79 taken 8760179 times.
|
8814080 | if (status != FX_SUCCESS) |
| 498 | { | ||
| 499 | |||
| 500 | /* Return the error status. */ | ||
| 501 | 53901 | return(status); | |
| 502 | } | ||
| 503 | |||
| 504 | |||
| 505 | /* Mark the FAT sector update bit map to indicate this sector has been | ||
| 506 | written. */ | ||
| 507 |
2/2✓ Branch 79 → 80 taken 268346 times.
✓ Branch 79 → 81 taken 8491833 times.
|
8760179 | if (media_ptr -> fx_media_sectors_per_FAT % (FX_FAT_MAP_SIZE << 3) == 0) |
| 508 | { | ||
| 509 | 268346 | sectors_per_bit = (UCHAR)(media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3)); | |
| 510 | } | ||
| 511 | else | ||
| 512 | { | ||
| 513 | 8491833 | sectors_per_bit = (UCHAR)((media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3)) + 1); | |
| 514 | } | ||
| 515 | 8760179 | ind = ((FAT_sector - media_ptr -> fx_media_reserved_sectors) / sectors_per_bit) >> 3; | |
| 516 | 8760179 | media_ptr -> fx_media_fat_secondary_update_map[ind] = | |
| 517 | 8760179 | (UCHAR)((INT)media_ptr -> fx_media_fat_secondary_update_map[ind] | |
| 518 | 8760179 | | (1 <<(((FAT_sector - media_ptr -> fx_media_reserved_sectors) / sectors_per_bit) & 7))); | |
| 519 | } | ||
| 520 | } | ||
| 521 | |||
| 522 | #ifdef FX_ENABLE_FAULT_TOLERANT | ||
| 523 | |||
| 524 | /* While fault_tolerant is enabled, this function will be called before the return of all APIs. */ | ||
| 525 |
2/2✓ Branch 85 → 86 taken 173805725 times.
✓ Branch 85 → 87 taken 2082434 times.
|
175888159 | if (media_ptr -> fx_media_fault_tolerant_enabled) |
| 526 | { | ||
| 527 | |||
| 528 | /* Delete the record of FAT sector since all FAT entries are flushed. */ | ||
| 529 | 173805725 | media_ptr -> fx_media_fault_tolerant_cached_FAT_sector = 0; | |
| 530 | } | ||
| 531 | #endif /* FX_ENABLE_FAULT_TOLERANT */ | ||
| 532 | |||
| 533 | /* Return successful status. */ | ||
| 534 | 177252738 | return(FX_SUCCESS); | |
| 535 | } | ||
| 536 | |||
| 537 |