fx_media_check_lost_cluster_check.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 | /** Media */ | ||
| 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_media.h" | ||
| 30 | #include "fx_utility.h" | ||
| 31 | |||
| 32 | |||
| 33 | /**************************************************************************/ | ||
| 34 | /* */ | ||
| 35 | /* FUNCTION RELEASE */ | ||
| 36 | /* */ | ||
| 37 | /* _fx_media_check_lost_cluster_check PORTABLE C */ | ||
| 38 | /* 6.1 */ | ||
| 39 | /* AUTHOR */ | ||
| 40 | /* */ | ||
| 41 | /* William E. Lamie, Microsoft Corporation */ | ||
| 42 | /* */ | ||
| 43 | /* DESCRIPTION */ | ||
| 44 | /* */ | ||
| 45 | /* This function examines all clusters to see if there are any unused */ | ||
| 46 | /* clusters that are also unavailable. If specified, this routine will */ | ||
| 47 | /* also mark the cluster as available. */ | ||
| 48 | /* */ | ||
| 49 | /* INPUT */ | ||
| 50 | /* */ | ||
| 51 | /* media_ptr Pointer to a previously */ | ||
| 52 | /* opened media */ | ||
| 53 | /* logical_fat Pointer to the logical FAT */ | ||
| 54 | /* bit map */ | ||
| 55 | /* total_clusters Total number of clusters */ | ||
| 56 | /* error_correction_option Option for correcting lost */ | ||
| 57 | /* cluster errors */ | ||
| 58 | /* */ | ||
| 59 | /* OUTPUT */ | ||
| 60 | /* */ | ||
| 61 | /* error Error code */ | ||
| 62 | /* */ | ||
| 63 | /* CALLS */ | ||
| 64 | /* */ | ||
| 65 | /* _fx_utility_FAT_entry_read Read a FAT entry */ | ||
| 66 | /* _fx_utility_FAT_entry_write Write a FAT entry */ | ||
| 67 | /* */ | ||
| 68 | /* CALLED BY */ | ||
| 69 | /* */ | ||
| 70 | /* _fx_check_media Check media function */ | ||
| 71 | /* */ | ||
| 72 | /**************************************************************************/ | ||
| 73 | 243 | ULONG _fx_media_check_lost_cluster_check(FX_MEDIA *media_ptr, UCHAR *logical_fat, ULONG total_clusters, ULONG error_correction_option) | |
| 74 | { | ||
| 75 | |||
| 76 | 243 | ULONG cluster, next_cluster = 0; | |
| 77 | ULONG fat_last; | ||
| 78 | ULONG error; | ||
| 79 | UINT status; | ||
| 80 | |||
| 81 | |||
| 82 | /* Calculate the FAT reserved and last sector values. */ | ||
| 83 |
2/2✓ Branch 2 → 3 taken 59 times.
✓ Branch 2 → 4 taken 184 times.
|
243 | if (media_ptr -> fx_media_32_bit_FAT) |
| 84 | { | ||
| 85 | 59 | fat_last = FX_LAST_CLUSTER_1_32; | |
| 86 | } | ||
| 87 | else | ||
| 88 | { | ||
| 89 | 184 | fat_last = FX_LAST_CLUSTER_1; | |
| 90 | } | ||
| 91 | |||
| 92 | /* Initialize the error. */ | ||
| 93 | 243 | error = 0; | |
| 94 | |||
| 95 | /* Loop through all the clusters to see if any clusters NOT in the logical sector FAT have | ||
| 96 | a non zero value. */ | ||
| 97 |
2/2✓ Branch 19 → 6 taken 4457055 times.
✓ Branch 19 → 20 taken 216 times.
|
4457271 | for (cluster = FX_FAT_ENTRY_START; cluster < total_clusters; cluster++) |
| 98 | { | ||
| 99 | |||
| 100 | /* Determine if this cluster is in the logical FAT. */ | ||
| 101 |
2/2✓ Branch 6 → 7 taken 57413 times.
✓ Branch 6 → 8 taken 4399642 times.
|
4457055 | if (logical_fat[cluster >> 3] & (1 << (cluster & 7))) |
| 102 | { | ||
| 103 | |||
| 104 | /* Yes, the cluster is in use by a file or sub-directory. Just continue the loop. */ | ||
| 105 | 57413 | continue; | |
| 106 | } | ||
| 107 | |||
| 108 | /* Otherwise, the cluster is not in use. */ | ||
| 109 | |||
| 110 | /* Read the contents of what should be a free cluster. */ | ||
| 111 | 4399642 | status = _fx_utility_FAT_entry_read(media_ptr, cluster, &next_cluster); | |
| 112 | |||
| 113 | /* Check for a good status. */ | ||
| 114 |
2/2✓ Branch 9 → 10 taken 18 times.
✓ Branch 9 → 11 taken 4399624 times.
|
4399642 | if (status) |
| 115 | { | ||
| 116 | |||
| 117 | /* Set the error code. */ | ||
| 118 | 18 | error = error | FX_IO_ERROR; | |
| 119 | |||
| 120 | /* Return error code. */ | ||
| 121 | 18 | return(error); | |
| 122 | } | ||
| 123 | |||
| 124 | /* Determine if the contents of the cluster is valid. */ | ||
| 125 |
4/4✓ Branch 11 → 12 taken 53533 times.
✓ Branch 11 → 13 taken 4346091 times.
✓ Branch 12 → 13 taken 42 times.
✓ Branch 12 → 14 taken 53491 times.
|
4399624 | if (((next_cluster > (ULONG)FX_FREE_CLUSTER) && (next_cluster < media_ptr -> fx_media_fat_reserved)) || |
| 126 |
2/2✓ Branch 13 → 14 taken 42 times.
✓ Branch 13 → 18 taken 4346091 times.
|
4346133 | (next_cluster >= fat_last)) |
| 127 | { | ||
| 128 | |||
| 129 | /* Lost cluster is present. */ | ||
| 130 | |||
| 131 | /* Set the error code status. */ | ||
| 132 | 53533 | error = FX_LOST_CLUSTER_ERROR; | |
| 133 | |||
| 134 | /* Determine if the lost cluster should be recovered. */ | ||
| 135 |
2/2✓ Branch 14 → 15 taken 53356 times.
✓ Branch 14 → 18 taken 177 times.
|
53533 | if (error_correction_option & FX_LOST_CLUSTER_ERROR) |
| 136 | { | ||
| 137 | |||
| 138 | /* Make the cluster available again. */ | ||
| 139 | 53356 | status = _fx_utility_FAT_entry_write(media_ptr, cluster, FX_FREE_CLUSTER); | |
| 140 | |||
| 141 | /* Check for a good status. */ | ||
| 142 |
2/2✓ Branch 16 → 17 taken 9 times.
✓ Branch 16 → 18 taken 53347 times.
|
53356 | if (status) |
| 143 | { | ||
| 144 | |||
| 145 | /* Increment the available clusters. */ | ||
| 146 | 9 | media_ptr -> fx_media_available_clusters++; | |
| 147 | |||
| 148 | /* Set the error code. */ | ||
| 149 | 9 | error = error | FX_IO_ERROR; | |
| 150 | |||
| 151 | /* Return error code. */ | ||
| 152 | 9 | return(error); | |
| 153 | } | ||
| 154 | } | ||
| 155 | } | ||
| 156 | } | ||
| 157 | |||
| 158 | /* Return error code. */ | ||
| 159 | 216 | return(error); | |
| 160 | } | ||
| 161 | |||
| 162 |