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 |
|
14162 |
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 |
|
14162 |
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 |
|
14162 |
error = 0; |
83 |
|
|
|
84 |
|
|
/* Setup at the first cluster. */ |
85 |
|
14162 |
cluster = starting_cluster; |
86 |
|
|
|
87 |
|
|
/* Initialize the previous cluster. */ |
88 |
|
14162 |
prev_cluster = 0; |
89 |
|
|
|
90 |
|
|
/* Initialize the count to 0. */ |
91 |
|
14162 |
count = 0; |
92 |
|
|
|
93 |
|
|
/* Loop to walk through the FAT chain. */ |
94 |
✓✓ |
23757 |
while ((cluster >= (ULONG)FX_FAT_ENTRY_START) && |
95 |
✓✓ |
12769 |
(cluster < (FX_FAT_ENTRY_START + media_ptr -> fx_media_total_clusters))) |
96 |
|
|
{ |
97 |
|
|
|
98 |
|
11401 |
cluster_number = cluster; |
99 |
|
|
|
100 |
|
|
|
101 |
|
|
/* Determine if this cluster is already in the logical FAT bit map. */ |
102 |
✓✓ |
11401 |
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 |
|
808 |
error = FX_FAT_CHAIN_ERROR; |
108 |
|
808 |
break; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
/* Now read the contents of the cluster. */ |
112 |
|
10593 |
status = _fx_utility_FAT_entry_read(media_ptr, cluster, &next_clust); |
113 |
|
|
|
114 |
|
|
/* Check the return status. */ |
115 |
✓✓ |
10593 |
if (status != FX_SUCCESS) |
116 |
|
|
{ |
117 |
|
|
|
118 |
|
|
/* Yes, the cluster is already being used by another file or |
119 |
|
|
sub-directory. */ |
120 |
|
250 |
error = FX_IO_ERROR; |
121 |
|
250 |
break; |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
/* Determine if the link is circular or the count is greater than the |
125 |
|
|
total clusters. */ |
126 |
✓✓ |
10343 |
if ((cluster == next_clust) || |
127 |
✓✓ |
10342 |
(next_clust < (ULONG)FX_FAT_ENTRY_START) || |
128 |
✓✓ |
9975 |
((next_clust >= (FX_FAT_ENTRY_START + media_ptr -> fx_media_total_clusters)) && |
129 |
✓✓ |
1748 |
(next_clust != media_ptr -> fx_media_fat_last))) |
130 |
|
|
{ |
131 |
|
|
|
132 |
|
748 |
error = FX_FAT_CHAIN_ERROR; |
133 |
|
748 |
break; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
/* Everything is good with the chain at this point. Mark it as valid. */ |
137 |
|
9595 |
logical_fat[cluster_number >> 3] = (UCHAR)(logical_fat[cluster_number >> 3] | (1 << (cluster_number & 7))); |
138 |
|
|
|
139 |
|
|
/* Move the cluster variable forward. */ |
140 |
|
9595 |
prev_cluster = cluster; |
141 |
|
9595 |
cluster = next_clust; |
142 |
|
|
|
143 |
|
|
/* Increment the number of valid clusters. */ |
144 |
|
9595 |
count++; |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
/* Return the last valid cluster and the total valid cluster count. */ |
148 |
|
14162 |
*last_valid_cluster = prev_cluster; |
149 |
|
14162 |
*total_valid_clusters = count; |
150 |
|
|
|
151 |
|
|
/* Return error code. */ |
152 |
|
14162 |
return(error); |
153 |
|
|
} |
154 |
|
|
|