Commit 02e4af63a9f5531b7975d21828edea04d6d519d2
Committed by
Bin Meng
1 parent
fc7b9e16a0
Exists in
smarc_8mq_lf_v2020.04
and in
9 other branches
cbfs: Move static variables into a struct
At present there are a number of static variables in BSS. This cannot work with SPL, at least until BSS is available in board_init_r(). Move the variables into a struct, so it is possible to malloc() it and use it before BSS is available. Signed-off-by: Simon Glass <sjg@chromium.org> Reviewed-by: Bin Meng <bmeng.cn@gmail.com> Tested-by: Bin Meng <bmeng.cn@gmail.com>
Showing 1 changed file with 59 additions and 32 deletions Side-by-side Diff
fs/cbfs/cbfs.c
... | ... | @@ -11,10 +11,15 @@ |
11 | 11 | enum cbfs_result file_cbfs_result; |
12 | 12 | static const u32 good_magic = 0x4f524243; |
13 | 13 | static const u8 good_file_magic[] = "LARCHIVE"; |
14 | -static int initialized; | |
15 | -static struct cbfs_header cbfs_header; | |
16 | -static struct cbfs_cachenode *file_cache; | |
17 | 14 | |
15 | +struct cbfs_priv { | |
16 | + int initialized; | |
17 | + struct cbfs_header header; | |
18 | + struct cbfs_cachenode *file_cache; | |
19 | +}; | |
20 | + | |
21 | +static struct cbfs_priv cbfs_s; | |
22 | + | |
18 | 23 | const char *file_cbfs_error(void) |
19 | 24 | { |
20 | 25 | switch (file_cbfs_result) { |
... | ... | @@ -69,8 +74,9 @@ |
69 | 74 | * |
70 | 75 | * @return 1 if a file is found, 0 if one isn't. |
71 | 76 | */ |
72 | -static int file_cbfs_next_file(u8 *start, u32 size, u32 align, | |
73 | - struct cbfs_cachenode *newNode, u32 *used) | |
77 | +static int file_cbfs_next_file(struct cbfs_priv *priv, u8 *start, u32 size, | |
78 | + u32 align, struct cbfs_cachenode *newNode, | |
79 | + u32 *used) | |
74 | 80 | { |
75 | 81 | struct cbfs_fileheader header; |
76 | 82 | |
77 | 83 | |
78 | 84 | |
79 | 85 | |
... | ... | @@ -117,20 +123,21 @@ |
117 | 123 | } |
118 | 124 | |
119 | 125 | /* Look through a CBFS instance and copy file metadata into regular memory. */ |
120 | -static void file_cbfs_fill_cache(u8 *start, u32 size, u32 align) | |
126 | +static void file_cbfs_fill_cache(struct cbfs_priv *priv, u8 *start, u32 size, | |
127 | + u32 align) | |
121 | 128 | { |
122 | 129 | struct cbfs_cachenode *cache_node; |
123 | 130 | struct cbfs_cachenode *newNode; |
124 | - struct cbfs_cachenode **cache_tail = &file_cache; | |
131 | + struct cbfs_cachenode **cache_tail = &priv->file_cache; | |
125 | 132 | |
126 | 133 | /* Clear out old information. */ |
127 | - cache_node = file_cache; | |
134 | + cache_node = priv->file_cache; | |
128 | 135 | while (cache_node) { |
129 | 136 | struct cbfs_cachenode *oldNode = cache_node; |
130 | 137 | cache_node = cache_node->next; |
131 | 138 | free(oldNode); |
132 | 139 | } |
133 | - file_cache = NULL; | |
140 | + priv->file_cache = NULL; | |
134 | 141 | |
135 | 142 | while (size >= align) { |
136 | 143 | int result; |
... | ... | @@ -138,8 +145,8 @@ |
138 | 145 | |
139 | 146 | newNode = (struct cbfs_cachenode *) |
140 | 147 | malloc(sizeof(struct cbfs_cachenode)); |
141 | - result = file_cbfs_next_file(start, size, align, | |
142 | - newNode, &used); | |
148 | + result = file_cbfs_next_file(priv, start, size, align, newNode, | |
149 | + &used); | |
143 | 150 | |
144 | 151 | if (result < 0) { |
145 | 152 | free(newNode); |
146 | 153 | |
147 | 154 | |
148 | 155 | |
149 | 156 | |
150 | 157 | |
151 | 158 | |
152 | 159 | |
153 | 160 | |
... | ... | @@ -175,27 +182,35 @@ |
175 | 182 | return 0; |
176 | 183 | } |
177 | 184 | |
178 | -void file_cbfs_init(uintptr_t end_of_rom) | |
185 | +static void cbfs_init(struct cbfs_priv *priv, uintptr_t end_of_rom) | |
179 | 186 | { |
180 | 187 | u8 *start_of_rom; |
181 | - initialized = 0; | |
182 | 188 | |
183 | - if (file_cbfs_load_header(end_of_rom, &cbfs_header)) | |
189 | + priv->initialized = 0; | |
190 | + | |
191 | + if (file_cbfs_load_header(end_of_rom, &priv->header)) | |
184 | 192 | return; |
185 | 193 | |
186 | - start_of_rom = (u8 *)(end_of_rom + 1 - cbfs_header.rom_size); | |
194 | + start_of_rom = (u8 *)(end_of_rom + 1 - priv->header.rom_size); | |
187 | 195 | |
188 | - file_cbfs_fill_cache(start_of_rom, cbfs_header.rom_size, | |
189 | - cbfs_header.align); | |
196 | + file_cbfs_fill_cache(priv, start_of_rom, priv->header.rom_size, | |
197 | + priv->header.align); | |
190 | 198 | if (file_cbfs_result == CBFS_SUCCESS) |
191 | - initialized = 1; | |
199 | + priv->initialized = 1; | |
192 | 200 | } |
193 | 201 | |
202 | +void file_cbfs_init(uintptr_t end_of_rom) | |
203 | +{ | |
204 | + cbfs_init(&cbfs_s, end_of_rom); | |
205 | +} | |
206 | + | |
194 | 207 | const struct cbfs_header *file_cbfs_get_header(void) |
195 | 208 | { |
196 | - if (initialized) { | |
209 | + struct cbfs_priv *priv = &cbfs_s; | |
210 | + | |
211 | + if (priv->initialized) { | |
197 | 212 | file_cbfs_result = CBFS_SUCCESS; |
198 | - return &cbfs_header; | |
213 | + return &priv->header; | |
199 | 214 | } else { |
200 | 215 | file_cbfs_result = CBFS_NOT_INITIALIZED; |
201 | 216 | return NULL; |
202 | 217 | |
203 | 218 | |
204 | 219 | |
... | ... | @@ -204,20 +219,24 @@ |
204 | 219 | |
205 | 220 | const struct cbfs_cachenode *file_cbfs_get_first(void) |
206 | 221 | { |
207 | - if (!initialized) { | |
222 | + struct cbfs_priv *priv = &cbfs_s; | |
223 | + | |
224 | + if (!priv->initialized) { | |
208 | 225 | file_cbfs_result = CBFS_NOT_INITIALIZED; |
209 | 226 | return NULL; |
210 | 227 | } else { |
211 | 228 | file_cbfs_result = CBFS_SUCCESS; |
212 | - return file_cache; | |
229 | + return priv->file_cache; | |
213 | 230 | } |
214 | 231 | } |
215 | 232 | |
216 | 233 | void file_cbfs_get_next(const struct cbfs_cachenode **file) |
217 | 234 | { |
218 | - if (!initialized) { | |
235 | + struct cbfs_priv *priv = &cbfs_s; | |
236 | + | |
237 | + if (!priv->initialized) { | |
219 | 238 | file_cbfs_result = CBFS_NOT_INITIALIZED; |
220 | - file = NULL; | |
239 | + *file = NULL; | |
221 | 240 | return; |
222 | 241 | } |
223 | 242 | |
224 | 243 | |
225 | 244 | |
... | ... | @@ -226,11 +245,12 @@ |
226 | 245 | file_cbfs_result = CBFS_SUCCESS; |
227 | 246 | } |
228 | 247 | |
229 | -const struct cbfs_cachenode *file_cbfs_find(const char *name) | |
248 | +const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv, | |
249 | + const char *name) | |
230 | 250 | { |
231 | - struct cbfs_cachenode *cache_node = file_cache; | |
251 | + struct cbfs_cachenode *cache_node = priv->file_cache; | |
232 | 252 | |
233 | - if (!initialized) { | |
253 | + if (!priv->initialized) { | |
234 | 254 | file_cbfs_result = CBFS_NOT_INITIALIZED; |
235 | 255 | return NULL; |
236 | 256 | } |
237 | 257 | |
238 | 258 | |
239 | 259 | |
240 | 260 | |
... | ... | @@ -248,26 +268,33 @@ |
248 | 268 | return cache_node; |
249 | 269 | } |
250 | 270 | |
271 | +const struct cbfs_cachenode *file_cbfs_find(const char *name) | |
272 | +{ | |
273 | + return cbfs_find_file(&cbfs_s, name); | |
274 | +} | |
275 | + | |
251 | 276 | const struct cbfs_cachenode *file_cbfs_find_uncached(uintptr_t end_of_rom, |
252 | 277 | const char *name) |
253 | 278 | { |
279 | + struct cbfs_priv *priv = &cbfs_s; | |
254 | 280 | u8 *start; |
255 | 281 | u32 size; |
256 | 282 | u32 align; |
257 | 283 | static struct cbfs_cachenode node; |
258 | 284 | |
259 | - if (file_cbfs_load_header(end_of_rom, &cbfs_header)) | |
285 | + if (file_cbfs_load_header(end_of_rom, &priv->header)) | |
260 | 286 | return NULL; |
261 | 287 | |
262 | - start = (u8 *)(end_of_rom + 1 - cbfs_header.rom_size); | |
263 | - size = cbfs_header.rom_size; | |
264 | - align = cbfs_header.align; | |
288 | + start = (u8 *)(end_of_rom + 1 - priv->header.rom_size); | |
289 | + size = priv->header.rom_size; | |
290 | + align = priv->header.align; | |
265 | 291 | |
266 | 292 | while (size >= align) { |
267 | 293 | int result; |
268 | 294 | u32 used; |
269 | 295 | |
270 | - result = file_cbfs_next_file(start, size, align, &node, &used); | |
296 | + result = file_cbfs_next_file(priv, start, size, align, &node, | |
297 | + &used); | |
271 | 298 | |
272 | 299 | if (result < 0) |
273 | 300 | return NULL; |