fx_media_check_FAT_chain_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_FAT_chain_check PORTABLE C */ | ||
| 38 | /* 6.1 */ | ||
| 39 | /* AUTHOR */ | ||
| 40 | /* */ | ||
| 41 | /* William E. Lamie, Microsoft Corporation */ | ||
| 42 | /* */ | ||
| 43 | /* DESCRIPTION */ | ||
| 44 | /* */ | ||
| 45 | /* This function walks the supplied FAT chain looking for cross links */ | ||
| 46 | /* (FAT entries already used in another FAT chain) and abnormal FAT */ | ||
| 47 | /* entries and invalid chain termination. */ | ||
| 48 | /* */ | ||
| 49 | /* INPUT */ | ||
| 50 | /* */ | ||
| 51 | /* media_ptr Pointer to a previously */ | ||
| 52 | /* opened media */ | ||
| 53 | /* starting_cluster Starting cluster of FAT chain */ | ||
| 54 | /* last_valid_cluster Last valid cluster of chain */ | ||
| 55 | /* total_valid_clusters Total number of valid clusters*/ | ||
| 56 | /* logical_fat Pointer to the logical FAT */ | ||
| 57 | /* bit map */ | ||
| 58 | /* */ | ||
| 59 | /* OUTPUT */ | ||
| 60 | /* */ | ||
| 61 | /* error Error code */ | ||
| 62 | /* */ | ||
| 63 | /* CALLS */ | ||
| 64 | /* */ | ||
| 65 | /* _fx_utility_FAT_entry_read Read a FAT entry */ | ||
| 66 | /* */ | ||
| 67 | /* CALLED BY */ | ||
| 68 | /* */ | ||
| 69 | /* _fx_check_media Check media function */ | ||
| 70 | /* */ | ||
| 71 | /**************************************************************************/ | ||
| 72 | 121924 | ULONG _fx_media_check_FAT_chain_check(FX_MEDIA *media_ptr, ULONG starting_cluster, ULONG *last_valid_cluster, ULONG *total_valid_clusters, UCHAR *logical_fat) | |
| 73 | { | ||
| 74 | |||
| 75 | 121924 | ULONG prev_cluster, cluster, next_clust = 0; | |
| 76 | ULONG cluster_number; | ||
| 77 | ULONG count, error; | ||
| 78 | UINT status; | ||
| 79 | |||
| 80 | |||
| 81 | /* Initialize the error code. */ | ||
| 82 | 121924 | error = 0; | |
| 83 | |||
| 84 | /* Setup at the first cluster. */ | ||
| 85 | 121924 | cluster = starting_cluster; | |
| 86 | |||
| 87 | /* Initialize the previous cluster. */ | ||
| 88 | 121924 | prev_cluster = 0; | |
| 89 | |||
| 90 | /* Initialize the count to 0. */ | ||
| 91 | 121924 | count = 0; | |
| 92 | |||
| 93 | /* Loop to walk through the FAT chain. */ | ||
| 94 |
2/2✓ Branch 14 → 15 taken 117465 times.
✓ Branch 14 → 16 taken 93926 times.
|
211391 | while ((cluster >= (ULONG)FX_FAT_ENTRY_START) && |
| 95 |
2/2✓ Branch 15 → 3 taken 105393 times.
✓ Branch 15 → 16 taken 12072 times.
|
117465 | (cluster < (FX_FAT_ENTRY_START + media_ptr -> fx_media_total_clusters))) |
| 96 | { | ||
| 97 | |||
| 98 | 105393 | cluster_number = cluster; | |
| 99 | |||
| 100 | |||
| 101 | /* Determine if this cluster is already in the logical FAT bit map. */ | ||
| 102 |
2/2✓ Branch 3 → 4 taken 7272 times.
✓ Branch 3 → 5 taken 98121 times.
|
105393 | if (logical_fat[cluster_number >> 3] & (1 << (cluster_number & 7))) |
| 103 | { | ||
| 104 | |||
| 105 | /* Yes, the cluster is already being used by another file or | ||
| 106 | sub-directory. */ | ||
| 107 | 7272 | error = FX_FAT_CHAIN_ERROR; | |
| 108 | 7272 | break; | |
| 109 | } | ||
| 110 | |||
| 111 | /* Now read the contents of the cluster. */ | ||
| 112 | 98121 | status = _fx_utility_FAT_entry_read(media_ptr, cluster, &next_clust); | |
| 113 | |||
| 114 | /* Check the return status. */ | ||
| 115 |
2/2✓ Branch 6 → 7 taken 2018 times.
✓ Branch 6 → 8 taken 96103 times.
|
98121 | if (status != FX_SUCCESS) |
| 116 | { | ||
| 117 | |||
| 118 | /* Yes, the cluster is already being used by another file or | ||
| 119 | sub-directory. */ | ||
| 120 | 2018 | error = FX_IO_ERROR; | |
| 121 | 2018 | break; | |
| 122 | } | ||
| 123 | |||
| 124 | /* Determine if the link is circular or the count is greater than the | ||
| 125 | total clusters. */ | ||
| 126 |
2/2✓ Branch 8 → 9 taken 96094 times.
✓ Branch 8 → 12 taken 9 times.
|
96103 | if ((cluster == next_clust) || |
| 127 |
2/2✓ Branch 9 → 10 taken 92887 times.
✓ Branch 9 → 12 taken 3207 times.
|
96094 | (next_clust < (ULONG)FX_FAT_ENTRY_START) || |
| 128 |
2/2✓ Branch 10 → 11 taken 15492 times.
✓ Branch 10 → 13 taken 77395 times.
|
92887 | ((next_clust >= (FX_FAT_ENTRY_START + media_ptr -> fx_media_total_clusters)) && |
| 129 |
2/2✓ Branch 11 → 12 taken 3420 times.
✓ Branch 11 → 13 taken 12072 times.
|
15492 | (next_clust != media_ptr -> fx_media_fat_last))) |
| 130 | { | ||
| 131 | |||
| 132 | 6636 | error = FX_FAT_CHAIN_ERROR; | |
| 133 | 6636 | break; | |
| 134 | } | ||
| 135 | |||
| 136 | /* Everything is good with the chain at this point. Mark it as valid. */ | ||
| 137 | 89467 | logical_fat[cluster_number >> 3] = (UCHAR)(logical_fat[cluster_number >> 3] | (1 << (cluster_number & 7))); | |
| 138 | |||
| 139 | /* Move the cluster variable forward. */ | ||
| 140 | 89467 | prev_cluster = cluster; | |
| 141 | 89467 | cluster = next_clust; | |
| 142 | |||
| 143 | /* Increment the number of valid clusters. */ | ||
| 144 | 89467 | count++; | |
| 145 | } | ||
| 146 | |||
| 147 | /* Return the last valid cluster and the total valid cluster count. */ | ||
| 148 | 121924 | *last_valid_cluster = prev_cluster; | |
| 149 | 121924 | *total_valid_clusters = count; | |
| 150 | |||
| 151 | /* Return error code. */ | ||
| 152 | 121924 | return(error); | |
| 153 | } | ||
| 154 | |||
| 155 |