blob: 1aa6f8ee8473058847b75a18bc1400be54557f5a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gabe Black7f8574c2012-10-12 14:26:11 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Gabe Black7f8574c2012-10-12 14:26:11 +00004 */
5
Guillaume GARDET647ffed2016-06-17 11:45:37 +02006#include <common.h>
Gabe Black7f8574c2012-10-12 14:26:11 +00007#include <cbfs.h>
8#include <malloc.h>
9#include <asm/byteorder.h>
10
Simon Glass7b5ee962019-08-14 19:56:11 -060011static const u32 good_magic = 0x4f524243;
12static const u8 good_file_magic[] = "LARCHIVE";
Simon Glassbc94f0a2019-08-14 19:56:12 -060013
14struct cbfs_priv {
15 int initialized;
16 struct cbfs_header header;
17 struct cbfs_cachenode *file_cache;
Simon Glassf152bf02019-08-14 19:56:13 -060018 enum cbfs_result result;
Simon Glassbc94f0a2019-08-14 19:56:12 -060019};
20
21static struct cbfs_priv cbfs_s;
Gabe Black7f8574c2012-10-12 14:26:11 +000022
23const char *file_cbfs_error(void)
24{
Simon Glassf152bf02019-08-14 19:56:13 -060025 switch (cbfs_s.result) {
Gabe Black7f8574c2012-10-12 14:26:11 +000026 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
Simon Glassf152bf02019-08-14 19:56:13 -060041enum cbfs_result cbfs_get_result(void)
42{
43 return cbfs_s.result;
44}
45
Gabe Black7f8574c2012-10-12 14:26:11 +000046/* Do endian conversion on the CBFS header structure. */
47static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
48{
49 dest->magic = be32_to_cpu(src->magic);
50 dest->version = be32_to_cpu(src->version);
51 dest->rom_size = be32_to_cpu(src->rom_size);
52 dest->boot_block_size = be32_to_cpu(src->boot_block_size);
53 dest->align = be32_to_cpu(src->align);
54 dest->offset = be32_to_cpu(src->offset);
55}
56
57/* Do endian conversion on a CBFS file header. */
58static void swap_file_header(struct cbfs_fileheader *dest,
59 const struct cbfs_fileheader *src)
60{
61 memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
62 dest->len = be32_to_cpu(src->len);
63 dest->type = be32_to_cpu(src->type);
Simon Glass87d15042019-07-08 13:18:22 -060064 dest->attributes_offset = be32_to_cpu(src->attributes_offset);
Gabe Black7f8574c2012-10-12 14:26:11 +000065 dest->offset = be32_to_cpu(src->offset);
66}
67
68/*
69 * Given a starting position in memory, scan forward, bounded by a size, and
70 * find the next valid CBFS file. No memory is allocated by this function. The
71 * caller is responsible for allocating space for the new file structure.
72 *
73 * @param start The location in memory to start from.
74 * @param size The size of the memory region to search.
75 * @param align The alignment boundaries to check on.
Simon Glassefbd7b12019-08-14 19:56:15 -060076 * @param new_node A pointer to the file structure to load.
Gabe Black7f8574c2012-10-12 14:26:11 +000077 * @param used A pointer to the count of of bytes scanned through,
78 * including the file if one is found.
79 *
80 * @return 1 if a file is found, 0 if one isn't.
81 */
Simon Glassbc94f0a2019-08-14 19:56:12 -060082static int file_cbfs_next_file(struct cbfs_priv *priv, u8 *start, u32 size,
Simon Glassefbd7b12019-08-14 19:56:15 -060083 u32 align, struct cbfs_cachenode *new_node,
Simon Glassbc94f0a2019-08-14 19:56:12 -060084 u32 *used)
Gabe Black7f8574c2012-10-12 14:26:11 +000085{
86 struct cbfs_fileheader header;
87
88 *used = 0;
89
90 while (size >= align) {
Simon Glassefbd7b12019-08-14 19:56:15 -060091 const struct cbfs_fileheader *file_header =
Gabe Black7f8574c2012-10-12 14:26:11 +000092 (const struct cbfs_fileheader *)start;
93 u32 name_len;
94 u32 step;
95
96 /* Check if there's a file here. */
Simon Glassefbd7b12019-08-14 19:56:15 -060097 if (memcmp(good_file_magic, &file_header->magic,
98 sizeof(file_header->magic))) {
Gabe Black7f8574c2012-10-12 14:26:11 +000099 *used += align;
100 size -= align;
101 start += align;
102 continue;
103 }
104
Simon Glassefbd7b12019-08-14 19:56:15 -0600105 swap_file_header(&header, file_header);
Christian Gmeiner81ff3bb2018-12-22 01:55:48 -0800106 if (header.offset < sizeof(struct cbfs_fileheader)) {
Simon Glassf152bf02019-08-14 19:56:13 -0600107 priv->result = CBFS_BAD_FILE;
Gabe Black7f8574c2012-10-12 14:26:11 +0000108 return -1;
109 }
Simon Glassefbd7b12019-08-14 19:56:15 -0600110 new_node->next = NULL;
111 new_node->type = header.type;
112 new_node->data = start + header.offset;
113 new_node->data_length = header.len;
Yaroslav K9999bb82016-08-08 20:32:15 -0700114 name_len = header.offset - sizeof(struct cbfs_fileheader);
Simon Glassefbd7b12019-08-14 19:56:15 -0600115 new_node->name = (char *)file_header +
Yaroslav K9999bb82016-08-08 20:32:15 -0700116 sizeof(struct cbfs_fileheader);
Simon Glassefbd7b12019-08-14 19:56:15 -0600117 new_node->name_length = name_len;
118 new_node->attributes_offset = header.attributes_offset;
Gabe Black7f8574c2012-10-12 14:26:11 +0000119
120 step = header.len;
121 if (step % align)
122 step = step + align - step % align;
123
124 *used += step;
125 return 1;
126 }
127 return 0;
128}
129
130/* Look through a CBFS instance and copy file metadata into regular memory. */
Simon Glassbc94f0a2019-08-14 19:56:12 -0600131static void file_cbfs_fill_cache(struct cbfs_priv *priv, u8 *start, u32 size,
132 u32 align)
Gabe Black7f8574c2012-10-12 14:26:11 +0000133{
134 struct cbfs_cachenode *cache_node;
Simon Glassefbd7b12019-08-14 19:56:15 -0600135 struct cbfs_cachenode *new_node;
Simon Glassbc94f0a2019-08-14 19:56:12 -0600136 struct cbfs_cachenode **cache_tail = &priv->file_cache;
Gabe Black7f8574c2012-10-12 14:26:11 +0000137
138 /* Clear out old information. */
Simon Glassbc94f0a2019-08-14 19:56:12 -0600139 cache_node = priv->file_cache;
Gabe Black7f8574c2012-10-12 14:26:11 +0000140 while (cache_node) {
Simon Glassefbd7b12019-08-14 19:56:15 -0600141 struct cbfs_cachenode *old_node = cache_node;
Gabe Black7f8574c2012-10-12 14:26:11 +0000142 cache_node = cache_node->next;
Simon Glassefbd7b12019-08-14 19:56:15 -0600143 free(old_node);
Gabe Black7f8574c2012-10-12 14:26:11 +0000144 }
Simon Glassbc94f0a2019-08-14 19:56:12 -0600145 priv->file_cache = NULL;
Gabe Black7f8574c2012-10-12 14:26:11 +0000146
147 while (size >= align) {
148 int result;
149 u32 used;
150
Simon Glassefbd7b12019-08-14 19:56:15 -0600151 new_node = (struct cbfs_cachenode *)
Gabe Black7f8574c2012-10-12 14:26:11 +0000152 malloc(sizeof(struct cbfs_cachenode));
Simon Glassefbd7b12019-08-14 19:56:15 -0600153 result = file_cbfs_next_file(priv, start, size, align, new_node,
Simon Glassbc94f0a2019-08-14 19:56:12 -0600154 &used);
Gabe Black7f8574c2012-10-12 14:26:11 +0000155
156 if (result < 0) {
Simon Glassefbd7b12019-08-14 19:56:15 -0600157 free(new_node);
Gabe Black7f8574c2012-10-12 14:26:11 +0000158 return;
159 } else if (result == 0) {
Simon Glassefbd7b12019-08-14 19:56:15 -0600160 free(new_node);
Gabe Black7f8574c2012-10-12 14:26:11 +0000161 break;
162 }
Simon Glassefbd7b12019-08-14 19:56:15 -0600163 *cache_tail = new_node;
164 cache_tail = &new_node->next;
Gabe Black7f8574c2012-10-12 14:26:11 +0000165
166 size -= used;
167 start += used;
168 }
Simon Glassf152bf02019-08-14 19:56:13 -0600169 priv->result = CBFS_SUCCESS;
Gabe Black7f8574c2012-10-12 14:26:11 +0000170}
171
172/* Get the CBFS header out of the ROM and do endian conversion. */
173static int file_cbfs_load_header(uintptr_t end_of_rom,
174 struct cbfs_header *header)
175{
176 struct cbfs_header *header_in_rom;
Andre Heider78acb332018-02-15 07:40:11 +0100177 int32_t offset = *(u32 *)(end_of_rom - 3);
Gabe Black7f8574c2012-10-12 14:26:11 +0000178
Andre Heider78acb332018-02-15 07:40:11 +0100179 header_in_rom = (struct cbfs_header *)(end_of_rom + offset + 1);
Gabe Black7f8574c2012-10-12 14:26:11 +0000180 swap_header(header, header_in_rom);
181
182 if (header->magic != good_magic || header->offset >
183 header->rom_size - header->boot_block_size) {
Simon Glassf152bf02019-08-14 19:56:13 -0600184 cbfs_s.result = CBFS_BAD_HEADER;
Gabe Black7f8574c2012-10-12 14:26:11 +0000185 return 1;
186 }
187 return 0;
188}
189
Simon Glassaa4274e2019-08-14 19:56:14 -0600190static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base,
191 struct cbfs_header *header)
192{
193 struct cbfs_header *header_in_rom;
194
195 header_in_rom = (struct cbfs_header *)base;
196 swap_header(header, header_in_rom);
197
198 if (header->magic != good_magic || header->offset >
199 header->rom_size - header->boot_block_size) {
200 priv->result = CBFS_BAD_HEADER;
201 return -EFAULT;
202 }
203
204 return 0;
205}
206
Simon Glassbc94f0a2019-08-14 19:56:12 -0600207static void cbfs_init(struct cbfs_priv *priv, uintptr_t end_of_rom)
Gabe Black7f8574c2012-10-12 14:26:11 +0000208{
209 u8 *start_of_rom;
Simon Glassbc94f0a2019-08-14 19:56:12 -0600210
211 priv->initialized = 0;
Gabe Black7f8574c2012-10-12 14:26:11 +0000212
Simon Glassbc94f0a2019-08-14 19:56:12 -0600213 if (file_cbfs_load_header(end_of_rom, &priv->header))
Gabe Black7f8574c2012-10-12 14:26:11 +0000214 return;
215
Simon Glassbc94f0a2019-08-14 19:56:12 -0600216 start_of_rom = (u8 *)(end_of_rom + 1 - priv->header.rom_size);
Gabe Black7f8574c2012-10-12 14:26:11 +0000217
Simon Glassbc94f0a2019-08-14 19:56:12 -0600218 file_cbfs_fill_cache(priv, start_of_rom, priv->header.rom_size,
219 priv->header.align);
Simon Glassf152bf02019-08-14 19:56:13 -0600220 if (priv->result == CBFS_SUCCESS)
Simon Glassbc94f0a2019-08-14 19:56:12 -0600221 priv->initialized = 1;
222}
223
224void file_cbfs_init(uintptr_t end_of_rom)
225{
226 cbfs_init(&cbfs_s, end_of_rom);
Gabe Black7f8574c2012-10-12 14:26:11 +0000227}
228
Simon Glassaa4274e2019-08-14 19:56:14 -0600229int cbfs_init_mem(ulong base, ulong size, struct cbfs_priv **privp)
230{
231 struct cbfs_priv priv_s, *priv = &priv_s;
232 int ret;
233
234 /*
235 * Use a local variable to start with until we know that the CBFS is
236 * valid. Assume that a master header appears at the start, at offset
237 * 0x38.
238 */
239 ret = cbfs_load_header_ptr(priv, base + 0x38, &priv->header);
240 if (ret)
241 return ret;
242
243 file_cbfs_fill_cache(priv, (u8 *)base, priv->header.rom_size,
244 priv->header.align);
245 if (priv->result != CBFS_SUCCESS)
246 return -EINVAL;
247
248 priv->initialized = 1;
249 priv = malloc(sizeof(priv_s));
250 if (!priv)
251 return -ENOMEM;
252 memcpy(priv, &priv_s, sizeof(priv_s));
253 *privp = priv;
254
255 return 0;
256}
257
Gabe Black7f8574c2012-10-12 14:26:11 +0000258const struct cbfs_header *file_cbfs_get_header(void)
259{
Simon Glassbc94f0a2019-08-14 19:56:12 -0600260 struct cbfs_priv *priv = &cbfs_s;
261
262 if (priv->initialized) {
Simon Glassf152bf02019-08-14 19:56:13 -0600263 priv->result = CBFS_SUCCESS;
Simon Glassbc94f0a2019-08-14 19:56:12 -0600264 return &priv->header;
Gabe Black7f8574c2012-10-12 14:26:11 +0000265 } else {
Simon Glassf152bf02019-08-14 19:56:13 -0600266 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black7f8574c2012-10-12 14:26:11 +0000267 return NULL;
268 }
269}
270
271const struct cbfs_cachenode *file_cbfs_get_first(void)
272{
Simon Glassbc94f0a2019-08-14 19:56:12 -0600273 struct cbfs_priv *priv = &cbfs_s;
274
275 if (!priv->initialized) {
Simon Glassf152bf02019-08-14 19:56:13 -0600276 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black7f8574c2012-10-12 14:26:11 +0000277 return NULL;
278 } else {
Simon Glassf152bf02019-08-14 19:56:13 -0600279 priv->result = CBFS_SUCCESS;
Simon Glassbc94f0a2019-08-14 19:56:12 -0600280 return priv->file_cache;
Gabe Black7f8574c2012-10-12 14:26:11 +0000281 }
282}
283
284void file_cbfs_get_next(const struct cbfs_cachenode **file)
285{
Simon Glassbc94f0a2019-08-14 19:56:12 -0600286 struct cbfs_priv *priv = &cbfs_s;
287
288 if (!priv->initialized) {
Simon Glassf152bf02019-08-14 19:56:13 -0600289 priv->result = CBFS_NOT_INITIALIZED;
Simon Glassbc94f0a2019-08-14 19:56:12 -0600290 *file = NULL;
Gabe Black7f8574c2012-10-12 14:26:11 +0000291 return;
292 }
293
294 if (*file)
295 *file = (*file)->next;
Simon Glassf152bf02019-08-14 19:56:13 -0600296 priv->result = CBFS_SUCCESS;
Gabe Black7f8574c2012-10-12 14:26:11 +0000297}
298
Simon Glassbc94f0a2019-08-14 19:56:12 -0600299const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
300 const char *name)
Gabe Black7f8574c2012-10-12 14:26:11 +0000301{
Simon Glassbc94f0a2019-08-14 19:56:12 -0600302 struct cbfs_cachenode *cache_node = priv->file_cache;
Gabe Black7f8574c2012-10-12 14:26:11 +0000303
Simon Glassbc94f0a2019-08-14 19:56:12 -0600304 if (!priv->initialized) {
Simon Glassf152bf02019-08-14 19:56:13 -0600305 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black7f8574c2012-10-12 14:26:11 +0000306 return NULL;
307 }
308
309 while (cache_node) {
310 if (!strcmp(name, cache_node->name))
311 break;
312 cache_node = cache_node->next;
313 }
314 if (!cache_node)
Simon Glassf152bf02019-08-14 19:56:13 -0600315 priv->result = CBFS_FILE_NOT_FOUND;
Gabe Black7f8574c2012-10-12 14:26:11 +0000316 else
Simon Glassf152bf02019-08-14 19:56:13 -0600317 priv->result = CBFS_SUCCESS;
Gabe Black7f8574c2012-10-12 14:26:11 +0000318
319 return cache_node;
320}
321
Simon Glassbc94f0a2019-08-14 19:56:12 -0600322const struct cbfs_cachenode *file_cbfs_find(const char *name)
323{
324 return cbfs_find_file(&cbfs_s, name);
325}
326
Gabe Black7f8574c2012-10-12 14:26:11 +0000327const struct cbfs_cachenode *file_cbfs_find_uncached(uintptr_t end_of_rom,
328 const char *name)
329{
Simon Glassbc94f0a2019-08-14 19:56:12 -0600330 struct cbfs_priv *priv = &cbfs_s;
Gabe Black7f8574c2012-10-12 14:26:11 +0000331 u8 *start;
332 u32 size;
333 u32 align;
334 static struct cbfs_cachenode node;
335
Simon Glassbc94f0a2019-08-14 19:56:12 -0600336 if (file_cbfs_load_header(end_of_rom, &priv->header))
Gabe Black7f8574c2012-10-12 14:26:11 +0000337 return NULL;
338
Simon Glassbc94f0a2019-08-14 19:56:12 -0600339 start = (u8 *)(end_of_rom + 1 - priv->header.rom_size);
340 size = priv->header.rom_size;
341 align = priv->header.align;
Gabe Black7f8574c2012-10-12 14:26:11 +0000342
343 while (size >= align) {
344 int result;
345 u32 used;
346
Simon Glassbc94f0a2019-08-14 19:56:12 -0600347 result = file_cbfs_next_file(priv, start, size, align, &node,
348 &used);
Gabe Black7f8574c2012-10-12 14:26:11 +0000349
350 if (result < 0)
351 return NULL;
352 else if (result == 0)
353 break;
354
355 if (!strcmp(name, node.name))
356 return &node;
357
358 size -= used;
359 start += used;
360 }
Simon Glassf152bf02019-08-14 19:56:13 -0600361 cbfs_s.result = CBFS_FILE_NOT_FOUND;
Gabe Black7f8574c2012-10-12 14:26:11 +0000362 return NULL;
363}
364
365const char *file_cbfs_name(const struct cbfs_cachenode *file)
366{
Simon Glassf152bf02019-08-14 19:56:13 -0600367 cbfs_s.result = CBFS_SUCCESS;
368
Gabe Black7f8574c2012-10-12 14:26:11 +0000369 return file->name;
370}
371
372u32 file_cbfs_size(const struct cbfs_cachenode *file)
373{
Simon Glassf152bf02019-08-14 19:56:13 -0600374 cbfs_s.result = CBFS_SUCCESS;
375
Gabe Black7f8574c2012-10-12 14:26:11 +0000376 return file->data_length;
377}
378
379u32 file_cbfs_type(const struct cbfs_cachenode *file)
380{
Simon Glassf152bf02019-08-14 19:56:13 -0600381 cbfs_s.result = CBFS_SUCCESS;
382
Gabe Black7f8574c2012-10-12 14:26:11 +0000383 return file->type;
384}
385
386long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
387 unsigned long maxsize)
388{
389 u32 size;
390
391 size = file->data_length;
392 if (maxsize && size > maxsize)
393 size = maxsize;
394
395 memcpy(buffer, file->data, size);
Simon Glassf152bf02019-08-14 19:56:13 -0600396 cbfs_s.result = CBFS_SUCCESS;
Gabe Black7f8574c2012-10-12 14:26:11 +0000397
Gabe Black7f8574c2012-10-12 14:26:11 +0000398 return size;
399}