blob: daae71629706f45f52d4cb68fbde1102e129af7a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gabe Black84cd9322012-10-12 14:26:11 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Gabe Black84cd9322012-10-12 14:26:11 +00004 */
5
Guillaume GARDET7a77e902016-06-17 11:45:37 +02006#include <common.h>
Gabe Black84cd9322012-10-12 14:26:11 +00007#include <cbfs.h>
8#include <malloc.h>
9#include <asm/byteorder.h>
10
11enum cbfs_result file_cbfs_result;
Simon Glassfc7b9e12019-08-14 19:56:11 -060012static const u32 good_magic = 0x4f524243;
13static const u8 good_file_magic[] = "LARCHIVE";
Simon Glass02e4af62019-08-14 19:56:12 -060014
15struct cbfs_priv {
16 int initialized;
17 struct cbfs_header header;
18 struct cbfs_cachenode *file_cache;
19};
20
21static struct cbfs_priv cbfs_s;
Gabe Black84cd9322012-10-12 14:26:11 +000022
23const char *file_cbfs_error(void)
24{
25 switch (file_cbfs_result) {
26 case CBFS_SUCCESS:
27 return "Success";
28 case CBFS_NOT_INITIALIZED:
29 return "CBFS not initialized";
30 case CBFS_BAD_HEADER:
31 return "Bad CBFS header";
32 case CBFS_BAD_FILE:
33 return "Bad CBFS file";
34 case CBFS_FILE_NOT_FOUND:
35 return "File not found";
36 default:
37 return "Unknown";
38 }
39}
40
Gabe Black84cd9322012-10-12 14:26:11 +000041/* Do endian conversion on the CBFS header structure. */
42static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
43{
44 dest->magic = be32_to_cpu(src->magic);
45 dest->version = be32_to_cpu(src->version);
46 dest->rom_size = be32_to_cpu(src->rom_size);
47 dest->boot_block_size = be32_to_cpu(src->boot_block_size);
48 dest->align = be32_to_cpu(src->align);
49 dest->offset = be32_to_cpu(src->offset);
50}
51
52/* Do endian conversion on a CBFS file header. */
53static void swap_file_header(struct cbfs_fileheader *dest,
54 const struct cbfs_fileheader *src)
55{
56 memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
57 dest->len = be32_to_cpu(src->len);
58 dest->type = be32_to_cpu(src->type);
Simon Glassababe102019-07-08 13:18:22 -060059 dest->attributes_offset = be32_to_cpu(src->attributes_offset);
Gabe Black84cd9322012-10-12 14:26:11 +000060 dest->offset = be32_to_cpu(src->offset);
61}
62
63/*
64 * Given a starting position in memory, scan forward, bounded by a size, and
65 * find the next valid CBFS file. No memory is allocated by this function. The
66 * caller is responsible for allocating space for the new file structure.
67 *
68 * @param start The location in memory to start from.
69 * @param size The size of the memory region to search.
70 * @param align The alignment boundaries to check on.
71 * @param newNode A pointer to the file structure to load.
72 * @param used A pointer to the count of of bytes scanned through,
73 * including the file if one is found.
74 *
75 * @return 1 if a file is found, 0 if one isn't.
76 */
Simon Glass02e4af62019-08-14 19:56:12 -060077static int file_cbfs_next_file(struct cbfs_priv *priv, u8 *start, u32 size,
78 u32 align, struct cbfs_cachenode *newNode,
79 u32 *used)
Gabe Black84cd9322012-10-12 14:26:11 +000080{
81 struct cbfs_fileheader header;
82
83 *used = 0;
84
85 while (size >= align) {
86 const struct cbfs_fileheader *fileHeader =
87 (const struct cbfs_fileheader *)start;
88 u32 name_len;
89 u32 step;
90
91 /* Check if there's a file here. */
92 if (memcmp(good_file_magic, &(fileHeader->magic),
93 sizeof(fileHeader->magic))) {
94 *used += align;
95 size -= align;
96 start += align;
97 continue;
98 }
99
100 swap_file_header(&header, fileHeader);
Christian Gmeiner9914c732018-12-22 01:55:48 -0800101 if (header.offset < sizeof(struct cbfs_fileheader)) {
Gabe Black84cd9322012-10-12 14:26:11 +0000102 file_cbfs_result = CBFS_BAD_FILE;
103 return -1;
104 }
105 newNode->next = NULL;
106 newNode->type = header.type;
107 newNode->data = start + header.offset;
108 newNode->data_length = header.len;
Yaroslav Kcc7ed262016-08-08 20:32:15 -0700109 name_len = header.offset - sizeof(struct cbfs_fileheader);
Gabe Black84cd9322012-10-12 14:26:11 +0000110 newNode->name = (char *)fileHeader +
Yaroslav Kcc7ed262016-08-08 20:32:15 -0700111 sizeof(struct cbfs_fileheader);
Gabe Black84cd9322012-10-12 14:26:11 +0000112 newNode->name_length = name_len;
Simon Glassababe102019-07-08 13:18:22 -0600113 newNode->attributes_offset = header.attributes_offset;
Gabe Black84cd9322012-10-12 14:26:11 +0000114
115 step = header.len;
116 if (step % align)
117 step = step + align - step % align;
118
119 *used += step;
120 return 1;
121 }
122 return 0;
123}
124
125/* Look through a CBFS instance and copy file metadata into regular memory. */
Simon Glass02e4af62019-08-14 19:56:12 -0600126static void file_cbfs_fill_cache(struct cbfs_priv *priv, u8 *start, u32 size,
127 u32 align)
Gabe Black84cd9322012-10-12 14:26:11 +0000128{
129 struct cbfs_cachenode *cache_node;
130 struct cbfs_cachenode *newNode;
Simon Glass02e4af62019-08-14 19:56:12 -0600131 struct cbfs_cachenode **cache_tail = &priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000132
133 /* Clear out old information. */
Simon Glass02e4af62019-08-14 19:56:12 -0600134 cache_node = priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000135 while (cache_node) {
136 struct cbfs_cachenode *oldNode = cache_node;
137 cache_node = cache_node->next;
138 free(oldNode);
139 }
Simon Glass02e4af62019-08-14 19:56:12 -0600140 priv->file_cache = NULL;
Gabe Black84cd9322012-10-12 14:26:11 +0000141
142 while (size >= align) {
143 int result;
144 u32 used;
145
146 newNode = (struct cbfs_cachenode *)
147 malloc(sizeof(struct cbfs_cachenode));
Simon Glass02e4af62019-08-14 19:56:12 -0600148 result = file_cbfs_next_file(priv, start, size, align, newNode,
149 &used);
Gabe Black84cd9322012-10-12 14:26:11 +0000150
151 if (result < 0) {
152 free(newNode);
153 return;
154 } else if (result == 0) {
155 free(newNode);
156 break;
157 }
158 *cache_tail = newNode;
159 cache_tail = &newNode->next;
160
161 size -= used;
162 start += used;
163 }
164 file_cbfs_result = CBFS_SUCCESS;
165}
166
167/* Get the CBFS header out of the ROM and do endian conversion. */
168static int file_cbfs_load_header(uintptr_t end_of_rom,
169 struct cbfs_header *header)
170{
171 struct cbfs_header *header_in_rom;
Andre Heider44683172018-02-15 07:40:11 +0100172 int32_t offset = *(u32 *)(end_of_rom - 3);
Gabe Black84cd9322012-10-12 14:26:11 +0000173
Andre Heider44683172018-02-15 07:40:11 +0100174 header_in_rom = (struct cbfs_header *)(end_of_rom + offset + 1);
Gabe Black84cd9322012-10-12 14:26:11 +0000175 swap_header(header, header_in_rom);
176
177 if (header->magic != good_magic || header->offset >
178 header->rom_size - header->boot_block_size) {
179 file_cbfs_result = CBFS_BAD_HEADER;
180 return 1;
181 }
182 return 0;
183}
184
Simon Glass02e4af62019-08-14 19:56:12 -0600185static void cbfs_init(struct cbfs_priv *priv, uintptr_t end_of_rom)
Gabe Black84cd9322012-10-12 14:26:11 +0000186{
187 u8 *start_of_rom;
Gabe Black84cd9322012-10-12 14:26:11 +0000188
Simon Glass02e4af62019-08-14 19:56:12 -0600189 priv->initialized = 0;
190
191 if (file_cbfs_load_header(end_of_rom, &priv->header))
Gabe Black84cd9322012-10-12 14:26:11 +0000192 return;
193
Simon Glass02e4af62019-08-14 19:56:12 -0600194 start_of_rom = (u8 *)(end_of_rom + 1 - priv->header.rom_size);
Gabe Black84cd9322012-10-12 14:26:11 +0000195
Simon Glass02e4af62019-08-14 19:56:12 -0600196 file_cbfs_fill_cache(priv, start_of_rom, priv->header.rom_size,
197 priv->header.align);
Gabe Black84cd9322012-10-12 14:26:11 +0000198 if (file_cbfs_result == CBFS_SUCCESS)
Simon Glass02e4af62019-08-14 19:56:12 -0600199 priv->initialized = 1;
200}
201
202void file_cbfs_init(uintptr_t end_of_rom)
203{
204 cbfs_init(&cbfs_s, end_of_rom);
Gabe Black84cd9322012-10-12 14:26:11 +0000205}
206
207const struct cbfs_header *file_cbfs_get_header(void)
208{
Simon Glass02e4af62019-08-14 19:56:12 -0600209 struct cbfs_priv *priv = &cbfs_s;
210
211 if (priv->initialized) {
Gabe Black84cd9322012-10-12 14:26:11 +0000212 file_cbfs_result = CBFS_SUCCESS;
Simon Glass02e4af62019-08-14 19:56:12 -0600213 return &priv->header;
Gabe Black84cd9322012-10-12 14:26:11 +0000214 } else {
215 file_cbfs_result = CBFS_NOT_INITIALIZED;
216 return NULL;
217 }
218}
219
220const struct cbfs_cachenode *file_cbfs_get_first(void)
221{
Simon Glass02e4af62019-08-14 19:56:12 -0600222 struct cbfs_priv *priv = &cbfs_s;
223
224 if (!priv->initialized) {
Gabe Black84cd9322012-10-12 14:26:11 +0000225 file_cbfs_result = CBFS_NOT_INITIALIZED;
226 return NULL;
227 } else {
228 file_cbfs_result = CBFS_SUCCESS;
Simon Glass02e4af62019-08-14 19:56:12 -0600229 return priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000230 }
231}
232
233void file_cbfs_get_next(const struct cbfs_cachenode **file)
234{
Simon Glass02e4af62019-08-14 19:56:12 -0600235 struct cbfs_priv *priv = &cbfs_s;
236
237 if (!priv->initialized) {
Gabe Black84cd9322012-10-12 14:26:11 +0000238 file_cbfs_result = CBFS_NOT_INITIALIZED;
Simon Glass02e4af62019-08-14 19:56:12 -0600239 *file = NULL;
Gabe Black84cd9322012-10-12 14:26:11 +0000240 return;
241 }
242
243 if (*file)
244 *file = (*file)->next;
245 file_cbfs_result = CBFS_SUCCESS;
246}
247
Simon Glass02e4af62019-08-14 19:56:12 -0600248const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
249 const char *name)
Gabe Black84cd9322012-10-12 14:26:11 +0000250{
Simon Glass02e4af62019-08-14 19:56:12 -0600251 struct cbfs_cachenode *cache_node = priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000252
Simon Glass02e4af62019-08-14 19:56:12 -0600253 if (!priv->initialized) {
Gabe Black84cd9322012-10-12 14:26:11 +0000254 file_cbfs_result = CBFS_NOT_INITIALIZED;
255 return NULL;
256 }
257
258 while (cache_node) {
259 if (!strcmp(name, cache_node->name))
260 break;
261 cache_node = cache_node->next;
262 }
263 if (!cache_node)
264 file_cbfs_result = CBFS_FILE_NOT_FOUND;
265 else
266 file_cbfs_result = CBFS_SUCCESS;
267
268 return cache_node;
269}
270
Simon Glass02e4af62019-08-14 19:56:12 -0600271const struct cbfs_cachenode *file_cbfs_find(const char *name)
272{
273 return cbfs_find_file(&cbfs_s, name);
274}
275
Gabe Black84cd9322012-10-12 14:26:11 +0000276const struct cbfs_cachenode *file_cbfs_find_uncached(uintptr_t end_of_rom,
277 const char *name)
278{
Simon Glass02e4af62019-08-14 19:56:12 -0600279 struct cbfs_priv *priv = &cbfs_s;
Gabe Black84cd9322012-10-12 14:26:11 +0000280 u8 *start;
281 u32 size;
282 u32 align;
283 static struct cbfs_cachenode node;
284
Simon Glass02e4af62019-08-14 19:56:12 -0600285 if (file_cbfs_load_header(end_of_rom, &priv->header))
Gabe Black84cd9322012-10-12 14:26:11 +0000286 return NULL;
287
Simon Glass02e4af62019-08-14 19:56:12 -0600288 start = (u8 *)(end_of_rom + 1 - priv->header.rom_size);
289 size = priv->header.rom_size;
290 align = priv->header.align;
Gabe Black84cd9322012-10-12 14:26:11 +0000291
292 while (size >= align) {
293 int result;
294 u32 used;
295
Simon Glass02e4af62019-08-14 19:56:12 -0600296 result = file_cbfs_next_file(priv, start, size, align, &node,
297 &used);
Gabe Black84cd9322012-10-12 14:26:11 +0000298
299 if (result < 0)
300 return NULL;
301 else if (result == 0)
302 break;
303
304 if (!strcmp(name, node.name))
305 return &node;
306
307 size -= used;
308 start += used;
309 }
310 file_cbfs_result = CBFS_FILE_NOT_FOUND;
311 return NULL;
312}
313
314const char *file_cbfs_name(const struct cbfs_cachenode *file)
315{
316 file_cbfs_result = CBFS_SUCCESS;
317 return file->name;
318}
319
320u32 file_cbfs_size(const struct cbfs_cachenode *file)
321{
322 file_cbfs_result = CBFS_SUCCESS;
323 return file->data_length;
324}
325
326u32 file_cbfs_type(const struct cbfs_cachenode *file)
327{
328 file_cbfs_result = CBFS_SUCCESS;
329 return file->type;
330}
331
332long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
333 unsigned long maxsize)
334{
335 u32 size;
336
337 size = file->data_length;
338 if (maxsize && size > maxsize)
339 size = maxsize;
340
341 memcpy(buffer, file->data, size);
342
343 file_cbfs_result = CBFS_SUCCESS;
344 return size;
345}