fx_utility_FAT_map_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_map_flush PORTABLE C */ | ||
| 38 | /* 6.1 */ | ||
| 39 | /* AUTHOR */ | ||
| 40 | /* */ | ||
| 41 | /* William E. Lamie, Microsoft Corporation */ | ||
| 42 | /* */ | ||
| 43 | /* DESCRIPTION */ | ||
| 44 | /* */ | ||
| 45 | /* This function updates mirrors changes in the primary FAT to each of */ | ||
| 46 | /* secondary FATs in the media. */ | ||
| 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_logical_sector_read Read FAT sector into memory */ | ||
| 59 | /* _fx_utility_logical_sector_write Write FAT sector back to disk */ | ||
| 60 | /* */ | ||
| 61 | /* CALLED BY */ | ||
| 62 | /* */ | ||
| 63 | /* FileX System Functions */ | ||
| 64 | /* */ | ||
| 65 | /**************************************************************************/ | ||
| 66 | 2534793 | UINT _fx_utility_FAT_map_flush(FX_MEDIA *media_ptr) | |
| 67 | { | ||
| 68 | |||
| 69 | ULONG FAT_sector, last_sector; | ||
| 70 | UINT i, status, FATs; | ||
| 71 | UCHAR sectors_per_bit; | ||
| 72 | |||
| 73 | |||
| 74 | /* Determine how many FAT sectors each bit in the bit map represents. Depending on | ||
| 75 | the number of sectors in the primary FAT, each bit in this map may represent one | ||
| 76 | or more primary FAT sectors. Because of this, it is possible some FAT sectors that | ||
| 77 | were not changed may get flushed out to the secondary FAT. However, this method | ||
| 78 | provides very nice performance benefits during normal operation and is much more | ||
| 79 | reasonable than performing a total copy of the primary FAT to each secondary FAT | ||
| 80 | on media flush and media close. */ | ||
| 81 |
2/2✓ Branch 2 → 3 taken 36330 times.
✓ Branch 2 → 4 taken 2498463 times.
|
2534793 | if (media_ptr -> fx_media_sectors_per_FAT % (FX_FAT_MAP_SIZE << 3) == 0) |
| 82 | { | ||
| 83 | 36330 | sectors_per_bit = (UCHAR)(media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3)); | |
| 84 | } | ||
| 85 | else | ||
| 86 | { | ||
| 87 | 2498463 | sectors_per_bit = (UCHAR)(media_ptr -> fx_media_sectors_per_FAT / (FX_FAT_MAP_SIZE << 3) + 1); | |
| 88 | } | ||
| 89 | |||
| 90 | /* Loop through the FAT update map to mirror primary FAT sectors to secondary FAT(s). */ | ||
| 91 |
2/2✓ Branch 23 → 6 taken 20278155 times.
✓ Branch 23 → 24 taken 2534766 times.
|
22812921 | for (i = 0; i < FX_FAT_MAP_SIZE << 3; i++) |
| 92 | { | ||
| 93 | |||
| 94 | /* Determine if there are FAT changes specified by this entry. */ | ||
| 95 |
2/2✓ Branch 6 → 7 taken 18613096 times.
✓ Branch 6 → 8 taken 1665059 times.
|
20278155 | if ((media_ptr -> fx_media_fat_secondary_update_map[i >> 3] & (1 << (i & 7))) == 0) |
| 96 | { | ||
| 97 | |||
| 98 | /* No, look at the next bit map entry. */ | ||
| 99 | 18613096 | continue; | |
| 100 | } | ||
| 101 | |||
| 102 | /* Setup the parameters for performing the update. */ | ||
| 103 | 1665059 | FAT_sector = i * sectors_per_bit + media_ptr -> fx_media_reserved_sectors; | |
| 104 | 1665059 | last_sector = FAT_sector + sectors_per_bit; | |
| 105 | |||
| 106 | /* Make sure the last update sector is within range. */ | ||
| 107 |
2/2✓ Branch 8 → 9 taken 181 times.
✓ Branch 8 → 10 taken 1664878 times.
|
1665059 | if (last_sector > (media_ptr -> fx_media_sectors_per_FAT + media_ptr -> fx_media_reserved_sectors)) |
| 108 | { | ||
| 109 | 181 | last_sector = media_ptr -> fx_media_sectors_per_FAT + media_ptr -> fx_media_reserved_sectors; | |
| 110 | } | ||
| 111 | |||
| 112 | /* Loop to mirror primary FAT sectors to secondary FAT(s). */ | ||
| 113 |
2/2✓ Branch 21 → 11 taken 23345084 times.
✓ Branch 21 → 22 taken 1665032 times.
|
25010116 | for (; FAT_sector < last_sector; FAT_sector++) |
| 114 | { | ||
| 115 | |||
| 116 | /* Read the FAT sector. */ | ||
| 117 | 23345084 | status = _fx_utility_logical_sector_read(media_ptr, (ULONG64) FAT_sector, | |
| 118 | 23345084 | media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); | |
| 119 | |||
| 120 | /* Determine if an error occurred. */ | ||
| 121 |
2/2✓ Branch 12 → 13 taken 18 times.
✓ Branch 12 → 14 taken 23345066 times.
|
23345084 | if (status != FX_SUCCESS) |
| 122 | { | ||
| 123 | /* Return the error status. */ | ||
| 124 | 18 | return(status); | |
| 125 | } | ||
| 126 | |||
| 127 | /* Pickup how many secondary FATs there are. */ | ||
| 128 | 23345066 | FATs = media_ptr -> fx_media_number_of_FATs - 1; | |
| 129 | |||
| 130 | /* Loop to update additional FAT entries. */ | ||
| 131 |
2/2✓ Branch 19 → 15 taken 22114281 times.
✓ Branch 19 → 20 taken 23345057 times.
|
45459338 | while (FATs) |
| 132 | { | ||
| 133 | |||
| 134 | /* Mirror main FAT sector write into the additional FATs. */ | ||
| 135 | 22114281 | status = _fx_utility_logical_sector_write(media_ptr, | |
| 136 | 22114281 | ((ULONG64) FAT_sector) + ((ULONG64)FATs * (ULONG64)(media_ptr -> fx_media_sectors_per_FAT)), | |
| 137 | 22114281 | media_ptr -> fx_media_memory_buffer, ((ULONG) 1), FX_FAT_SECTOR); | |
| 138 | |||
| 139 | /* Determine if an error occurred. */ | ||
| 140 |
2/2✓ Branch 16 → 17 taken 9 times.
✓ Branch 16 → 18 taken 22114272 times.
|
22114281 | if (status != FX_SUCCESS) |
| 141 | { | ||
| 142 | |||
| 143 | /* Return the error status. */ | ||
| 144 | 9 | return(status); | |
| 145 | } | ||
| 146 | |||
| 147 | /* Decrement the number of FATs. */ | ||
| 148 | 22114272 | FATs--; | |
| 149 | } | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | /* Clear the bit map that indicates primary FAT updates. */ | ||
| 154 |
2/2✓ Branch 26 → 25 taken 2534766 times.
✓ Branch 26 → 27 taken 2534766 times.
|
5069532 | for (i = 0; i < FX_FAT_MAP_SIZE; i++) |
| 155 | { | ||
| 156 | |||
| 157 | /* Clear each entry in the bit map. */ | ||
| 158 | 2534766 | media_ptr -> fx_media_fat_secondary_update_map[i] = 0; | |
| 159 | } | ||
| 160 | |||
| 161 | /* Return a successful completion. */ | ||
| 162 | 2534766 | return(FX_SUCCESS); | |
| 163 | } | ||
| 164 | |||
| 165 |