blob: 415ea28b871d36f95cecf7ee1b3ba46cbb36a39e [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>
Simon Glassba06a1f2020-05-24 17:38:24 -06008#include <log.h>
Gabe Black7f8574c2012-10-12 14:26:11 +00009#include <malloc.h>
10#include <asm/byteorder.h>
11
Simon Glasse78f9bb2020-05-24 17:38:17 -060012/* Offset of master header from the start of a coreboot ROM */
13#define MASTER_HDR_OFFSET 0x38
14
Simon Glass7b5ee962019-08-14 19:56:11 -060015static const u32 good_magic = 0x4f524243;
16static const u8 good_file_magic[] = "LARCHIVE";
Simon Glassbc94f0a2019-08-14 19:56:12 -060017
Simon Glassec36d7a2020-05-24 17:38:20 -060018/**
19 * struct cbfs_priv - Private data for this driver
20 *
21 * @initialised: true if this CBFS has been inited
22 * @start: Start position of CBFS in memory, typically memory-mapped SPI flash
23 * @header: Header read from the CBFS, byte-swapped so U-Boot can access it
24 * @file_cache: List of file headers read from CBFS
25 * @result: Success/error result
26 */
Simon Glassbc94f0a2019-08-14 19:56:12 -060027struct cbfs_priv {
Simon Glass2029a6d2020-05-24 17:38:14 -060028 bool initialized;
Simon Glassec36d7a2020-05-24 17:38:20 -060029 void *start;
Simon Glassbc94f0a2019-08-14 19:56:12 -060030 struct cbfs_header header;
31 struct cbfs_cachenode *file_cache;
Simon Glassf152bf02019-08-14 19:56:13 -060032 enum cbfs_result result;
Simon Glassbc94f0a2019-08-14 19:56:12 -060033};
34
35static struct cbfs_priv cbfs_s;
Gabe Black7f8574c2012-10-12 14:26:11 +000036
37const char *file_cbfs_error(void)
38{
Simon Glassf152bf02019-08-14 19:56:13 -060039 switch (cbfs_s.result) {
Gabe Black7f8574c2012-10-12 14:26:11 +000040 case CBFS_SUCCESS:
41 return "Success";
42 case CBFS_NOT_INITIALIZED:
43 return "CBFS not initialized";
44 case CBFS_BAD_HEADER:
45 return "Bad CBFS header";
46 case CBFS_BAD_FILE:
47 return "Bad CBFS file";
48 case CBFS_FILE_NOT_FOUND:
49 return "File not found";
50 default:
51 return "Unknown";
52 }
53}
54
Simon Glassf152bf02019-08-14 19:56:13 -060055enum cbfs_result cbfs_get_result(void)
56{
57 return cbfs_s.result;
58}
59
Gabe Black7f8574c2012-10-12 14:26:11 +000060/* Do endian conversion on the CBFS header structure. */
61static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
62{
63 dest->magic = be32_to_cpu(src->magic);
64 dest->version = be32_to_cpu(src->version);
65 dest->rom_size = be32_to_cpu(src->rom_size);
66 dest->boot_block_size = be32_to_cpu(src->boot_block_size);
67 dest->align = be32_to_cpu(src->align);
68 dest->offset = be32_to_cpu(src->offset);
69}
70
71/* Do endian conversion on a CBFS file header. */
72static void swap_file_header(struct cbfs_fileheader *dest,
73 const struct cbfs_fileheader *src)
74{
75 memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
76 dest->len = be32_to_cpu(src->len);
77 dest->type = be32_to_cpu(src->type);
Simon Glass87d15042019-07-08 13:18:22 -060078 dest->attributes_offset = be32_to_cpu(src->attributes_offset);
Gabe Black7f8574c2012-10-12 14:26:11 +000079 dest->offset = be32_to_cpu(src->offset);
80}
81
Simon Glass1567db02021-03-15 18:00:14 +130082/**
83 * fill_node() - Fill a node struct with information from the CBFS
84 *
85 * @node: Node to fill
86 * @start: Pointer to the start of the CBFS file in memory
87 * @header: Pointer to the header information (in our enddianess)
88 * @return 0 if OK, -EBADF if the header is too small
89 */
90static int fill_node(struct cbfs_cachenode *node, void *start,
91 struct cbfs_fileheader *header)
92{
93 uint name_len;
Simon Glass2d507d22021-03-15 18:00:16 +130094 uint offset;
Simon Glass1567db02021-03-15 18:00:14 +130095
96 /* Check the header is large enough */
97 if (header->offset < sizeof(struct cbfs_fileheader))
98 return -EBADF;
99
100 node->next = NULL;
101 node->type = header->type;
102 node->data = start + header->offset;
103 node->data_length = header->len;
104 name_len = header->offset - sizeof(struct cbfs_fileheader);
105 node->name = start + sizeof(struct cbfs_fileheader);
106 node->name_length = name_len;
107 node->attr_offset = header->attributes_offset;
Simon Glass2d507d22021-03-15 18:00:16 +1300108 node->comp_algo = CBFS_COMPRESS_NONE;
109 node->decomp_size = 0;
110
111 for (offset = node->attr_offset; offset < header->offset;) {
112 const struct cbfs_file_attribute *attr;
113 uint tag, len;
114
115 attr = start + offset;
116 tag = be32_to_cpu(attr->tag);
117 len = be32_to_cpu(attr->len);
118 if (tag == CBFS_FILE_ATTR_TAG_COMPRESSION) {
119 struct cbfs_file_attr_compression *comp;
120
121 comp = start + offset;
122 node->comp_algo = be32_to_cpu(comp->compression);
123 node->decomp_size =
124 be32_to_cpu(comp->decompressed_size);
125 }
126
127 offset += len;
128 }
Simon Glass1567db02021-03-15 18:00:14 +1300129
130 return 0;
131}
132
Gabe Black7f8574c2012-10-12 14:26:11 +0000133/*
134 * Given a starting position in memory, scan forward, bounded by a size, and
135 * find the next valid CBFS file. No memory is allocated by this function. The
136 * caller is responsible for allocating space for the new file structure.
137 *
138 * @param start The location in memory to start from.
139 * @param size The size of the memory region to search.
140 * @param align The alignment boundaries to check on.
Simon Glass7a02c812021-03-15 18:00:10 +1300141 * @param node A pointer to the file structure to load.
Gabe Black7f8574c2012-10-12 14:26:11 +0000142 * @param used A pointer to the count of of bytes scanned through,
143 * including the file if one is found.
144 *
Simon Glassc72d4a82020-05-24 17:38:15 -0600145 * @return 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header
146 * is found.
Gabe Black7f8574c2012-10-12 14:26:11 +0000147 */
Simon Glass6ca4c382020-05-24 17:38:19 -0600148static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size,
Simon Glass7a02c812021-03-15 18:00:10 +1300149 int align, struct cbfs_cachenode *node,
Simon Glassc72d4a82020-05-24 17:38:15 -0600150 int *used)
Gabe Black7f8574c2012-10-12 14:26:11 +0000151{
152 struct cbfs_fileheader header;
153
154 *used = 0;
155
156 while (size >= align) {
Simon Glass6ca4c382020-05-24 17:38:19 -0600157 const struct cbfs_fileheader *file_header = start;
Simon Glass1567db02021-03-15 18:00:14 +1300158 int ret;
Gabe Black7f8574c2012-10-12 14:26:11 +0000159
160 /* Check if there's a file here. */
Simon Glassefbd7b12019-08-14 19:56:15 -0600161 if (memcmp(good_file_magic, &file_header->magic,
162 sizeof(file_header->magic))) {
Gabe Black7f8574c2012-10-12 14:26:11 +0000163 *used += align;
164 size -= align;
165 start += align;
166 continue;
167 }
168
Simon Glassefbd7b12019-08-14 19:56:15 -0600169 swap_file_header(&header, file_header);
Simon Glass1567db02021-03-15 18:00:14 +1300170 ret = fill_node(node, start, &header);
171 if (ret) {
Simon Glassf152bf02019-08-14 19:56:13 -0600172 priv->result = CBFS_BAD_FILE;
Simon Glass1567db02021-03-15 18:00:14 +1300173 return log_msg_ret("fill", ret);
Gabe Black7f8574c2012-10-12 14:26:11 +0000174 }
Gabe Black7f8574c2012-10-12 14:26:11 +0000175
Simon Glass0352edd2021-03-15 18:00:15 +1300176 *used += ALIGN(header.len, align);
Simon Glassc72d4a82020-05-24 17:38:15 -0600177 return 0;
Gabe Black7f8574c2012-10-12 14:26:11 +0000178 }
Simon Glassc72d4a82020-05-24 17:38:15 -0600179
180 return -ENOENT;
Gabe Black7f8574c2012-10-12 14:26:11 +0000181}
182
183/* Look through a CBFS instance and copy file metadata into regular memory. */
Simon Glassec36d7a2020-05-24 17:38:20 -0600184static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align)
Gabe Black7f8574c2012-10-12 14:26:11 +0000185{
186 struct cbfs_cachenode *cache_node;
Simon Glass7a02c812021-03-15 18:00:10 +1300187 struct cbfs_cachenode *node;
Simon Glassbc94f0a2019-08-14 19:56:12 -0600188 struct cbfs_cachenode **cache_tail = &priv->file_cache;
Simon Glassec36d7a2020-05-24 17:38:20 -0600189 void *start;
Gabe Black7f8574c2012-10-12 14:26:11 +0000190
191 /* Clear out old information. */
Simon Glassbc94f0a2019-08-14 19:56:12 -0600192 cache_node = priv->file_cache;
Gabe Black7f8574c2012-10-12 14:26:11 +0000193 while (cache_node) {
Simon Glassefbd7b12019-08-14 19:56:15 -0600194 struct cbfs_cachenode *old_node = cache_node;
Gabe Black7f8574c2012-10-12 14:26:11 +0000195 cache_node = cache_node->next;
Simon Glassefbd7b12019-08-14 19:56:15 -0600196 free(old_node);
Gabe Black7f8574c2012-10-12 14:26:11 +0000197 }
Simon Glassbc94f0a2019-08-14 19:56:12 -0600198 priv->file_cache = NULL;
Gabe Black7f8574c2012-10-12 14:26:11 +0000199
Simon Glassec36d7a2020-05-24 17:38:20 -0600200 start = priv->start;
Gabe Black7f8574c2012-10-12 14:26:11 +0000201 while (size >= align) {
Simon Glassc72d4a82020-05-24 17:38:15 -0600202 int used;
Simon Glass650b2412020-05-24 17:38:12 -0600203 int ret;
Gabe Black7f8574c2012-10-12 14:26:11 +0000204
Simon Glass7907c4b2021-03-15 18:00:17 +1300205 node = malloc(sizeof(struct cbfs_cachenode));
Simon Glass7a02c812021-03-15 18:00:10 +1300206 if (!node)
Simon Glassc72d4a82020-05-24 17:38:15 -0600207 return -ENOMEM;
Simon Glass7a02c812021-03-15 18:00:10 +1300208 ret = file_cbfs_next_file(priv, start, size, align, node,
Simon Glass650b2412020-05-24 17:38:12 -0600209 &used);
Gabe Black7f8574c2012-10-12 14:26:11 +0000210
Simon Glass650b2412020-05-24 17:38:12 -0600211 if (ret < 0) {
Simon Glass7a02c812021-03-15 18:00:10 +1300212 free(node);
Simon Glassc72d4a82020-05-24 17:38:15 -0600213 if (ret == -ENOENT)
214 break;
215 return ret;
Gabe Black7f8574c2012-10-12 14:26:11 +0000216 }
Simon Glass7a02c812021-03-15 18:00:10 +1300217 *cache_tail = node;
218 cache_tail = &node->next;
Gabe Black7f8574c2012-10-12 14:26:11 +0000219
220 size -= used;
221 start += used;
222 }
Simon Glassf152bf02019-08-14 19:56:13 -0600223 priv->result = CBFS_SUCCESS;
Simon Glassc72d4a82020-05-24 17:38:15 -0600224
225 return 0;
Gabe Black7f8574c2012-10-12 14:26:11 +0000226}
227
Simon Glass51541002020-05-24 17:38:18 -0600228/**
229 * load_header() - Load the CBFS header
230 *
231 * Get the CBFS header out of the ROM and do endian conversion.
232 *
233 * @priv: Private data, which is inited by this function
234 * @addr: Address of CBFS header in memory-mapped SPI flash
235 * @return 0 if OK, -ENXIO if the header is bad
236 */
237static int load_header(struct cbfs_priv *priv, ulong addr)
Gabe Black7f8574c2012-10-12 14:26:11 +0000238{
Simon Glass9ad16d62020-05-24 17:38:16 -0600239 struct cbfs_header *header = &priv->header;
Gabe Black7f8574c2012-10-12 14:26:11 +0000240 struct cbfs_header *header_in_rom;
241
Simon Glass51541002020-05-24 17:38:18 -0600242 memset(priv, '\0', sizeof(*priv));
243 header_in_rom = (struct cbfs_header *)addr;
Gabe Black7f8574c2012-10-12 14:26:11 +0000244 swap_header(header, header_in_rom);
245
246 if (header->magic != good_magic || header->offset >
247 header->rom_size - header->boot_block_size) {
Simon Glass9ad16d62020-05-24 17:38:16 -0600248 priv->result = CBFS_BAD_HEADER;
Simon Glass51541002020-05-24 17:38:18 -0600249 return -ENXIO;
Gabe Black7f8574c2012-10-12 14:26:11 +0000250 }
Simon Glass51541002020-05-24 17:38:18 -0600251
Gabe Black7f8574c2012-10-12 14:26:11 +0000252 return 0;
253}
254
Simon Glass51541002020-05-24 17:38:18 -0600255/**
256 * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end
257 *
258 * @priv: Private data, which is inited by this function
259 * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff)
260 * @return 0 if OK, -ENXIO if the header is bad
261 */
262static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
Simon Glassaa4274e2019-08-14 19:56:14 -0600263{
Simon Glass51541002020-05-24 17:38:18 -0600264 int offset = *(u32 *)(end_of_rom - 3);
Simon Glassec36d7a2020-05-24 17:38:20 -0600265 int ret;
266
267 ret = load_header(priv, end_of_rom + offset + 1);
268 if (ret)
269 return ret;
270 priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size);
Simon Glassaa4274e2019-08-14 19:56:14 -0600271
Simon Glassec36d7a2020-05-24 17:38:20 -0600272 return 0;
Simon Glass51541002020-05-24 17:38:18 -0600273}
Simon Glassaa4274e2019-08-14 19:56:14 -0600274
Simon Glass51541002020-05-24 17:38:18 -0600275/**
276 * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base
277 *
278 * @priv: Private data, which is inited by this function
279 * @base: Address of the first byte of the ROM (e.g. 0xff000000)
280 * @return 0 if OK, -ENXIO if the header is bad
281 */
282static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
283{
Simon Glassec36d7a2020-05-24 17:38:20 -0600284 int ret;
285
286 ret = load_header(priv, base + MASTER_HDR_OFFSET);
287 if (ret)
288 return ret;
289 priv->start = (void *)base;
290
291 return 0;
Simon Glassaa4274e2019-08-14 19:56:14 -0600292}
293
Simon Glass04923172020-05-24 17:38:21 -0600294static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
Gabe Black7f8574c2012-10-12 14:26:11 +0000295{
Simon Glass04923172020-05-24 17:38:21 -0600296 int ret;
Gabe Black7f8574c2012-10-12 14:26:11 +0000297
Simon Glass04923172020-05-24 17:38:21 -0600298 ret = file_cbfs_load_header(priv, end_of_rom);
299 if (ret)
300 return ret;
301
302 ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
303 priv->header.align);
304 if (ret)
305 return ret;
306 priv->initialized = true;
307
308 return 0;
Simon Glassbc94f0a2019-08-14 19:56:12 -0600309}
310
Simon Glass04923172020-05-24 17:38:21 -0600311int file_cbfs_init(ulong end_of_rom)
Simon Glassbc94f0a2019-08-14 19:56:12 -0600312{
Simon Glass04923172020-05-24 17:38:21 -0600313 return cbfs_init(&cbfs_s, end_of_rom);
Gabe Black7f8574c2012-10-12 14:26:11 +0000314}
315
Simon Glass94649092021-03-15 18:00:12 +1300316int cbfs_init_mem(ulong base, ulong size, bool require_hdr,
317 struct cbfs_priv **privp)
Simon Glassaa4274e2019-08-14 19:56:14 -0600318{
319 struct cbfs_priv priv_s, *priv = &priv_s;
320 int ret;
321
322 /*
Simon Glass94649092021-03-15 18:00:12 +1300323 * Use a local variable to start with until we know that the * CBFS is
324 * valid. Note that size is detected from the header, if present,
325 * meaning the parameter is ignored.
Simon Glassaa4274e2019-08-14 19:56:14 -0600326 */
Simon Glass51541002020-05-24 17:38:18 -0600327 ret = cbfs_load_header_ptr(priv, base);
Simon Glass94649092021-03-15 18:00:12 +1300328 if (ret) {
329 if (require_hdr || size == CBFS_SIZE_UNKNOWN)
330 return ret;
331 memset(priv, '\0', sizeof(struct cbfs_priv));
332 priv->header.rom_size = size;
333 priv->header.align = CBFS_ALIGN_SIZE;
334 priv->start = (void *)base;
335 }
Simon Glassaa4274e2019-08-14 19:56:14 -0600336
Simon Glassba06a1f2020-05-24 17:38:24 -0600337 ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
338 priv->header.align);
339 if (ret)
340 return log_msg_ret("fill", ret);
Simon Glassaa4274e2019-08-14 19:56:14 -0600341
Simon Glass2029a6d2020-05-24 17:38:14 -0600342 priv->initialized = true;
Simon Glassaa4274e2019-08-14 19:56:14 -0600343 priv = malloc(sizeof(priv_s));
344 if (!priv)
345 return -ENOMEM;
346 memcpy(priv, &priv_s, sizeof(priv_s));
347 *privp = priv;
348
349 return 0;
350}
351
Gabe Black7f8574c2012-10-12 14:26:11 +0000352const struct cbfs_header *file_cbfs_get_header(void)
353{
Simon Glassbc94f0a2019-08-14 19:56:12 -0600354 struct cbfs_priv *priv = &cbfs_s;
355
356 if (priv->initialized) {
Simon Glassf152bf02019-08-14 19:56:13 -0600357 priv->result = CBFS_SUCCESS;
Simon Glassbc94f0a2019-08-14 19:56:12 -0600358 return &priv->header;
Gabe Black7f8574c2012-10-12 14:26:11 +0000359 } else {
Simon Glassf152bf02019-08-14 19:56:13 -0600360 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black7f8574c2012-10-12 14:26:11 +0000361 return NULL;
362 }
363}
364
Simon Glassf8142fb2021-03-15 18:00:13 +1300365const struct cbfs_cachenode *cbfs_get_first(const struct cbfs_priv *priv)
366{
367 return priv->file_cache;
368}
369
370void cbfs_get_next(const struct cbfs_cachenode **filep)
371{
372 if (*filep)
373 *filep = (*filep)->next;
374}
375
Gabe Black7f8574c2012-10-12 14:26:11 +0000376const struct cbfs_cachenode *file_cbfs_get_first(void)
377{
Simon Glassbc94f0a2019-08-14 19:56:12 -0600378 struct cbfs_priv *priv = &cbfs_s;
379
380 if (!priv->initialized) {
Simon Glassf152bf02019-08-14 19:56:13 -0600381 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black7f8574c2012-10-12 14:26:11 +0000382 return NULL;
383 } else {
Simon Glassf152bf02019-08-14 19:56:13 -0600384 priv->result = CBFS_SUCCESS;
Simon Glassbc94f0a2019-08-14 19:56:12 -0600385 return priv->file_cache;
Gabe Black7f8574c2012-10-12 14:26:11 +0000386 }
387}
388
389void file_cbfs_get_next(const struct cbfs_cachenode **file)
390{
Simon Glassbc94f0a2019-08-14 19:56:12 -0600391 struct cbfs_priv *priv = &cbfs_s;
392
393 if (!priv->initialized) {
Simon Glassf152bf02019-08-14 19:56:13 -0600394 priv->result = CBFS_NOT_INITIALIZED;
Simon Glassbc94f0a2019-08-14 19:56:12 -0600395 *file = NULL;
Gabe Black7f8574c2012-10-12 14:26:11 +0000396 return;
397 }
398
399 if (*file)
400 *file = (*file)->next;
Simon Glassf152bf02019-08-14 19:56:13 -0600401 priv->result = CBFS_SUCCESS;
Gabe Black7f8574c2012-10-12 14:26:11 +0000402}
403
Simon Glassbc94f0a2019-08-14 19:56:12 -0600404const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
405 const char *name)
Gabe Black7f8574c2012-10-12 14:26:11 +0000406{
Simon Glassbc94f0a2019-08-14 19:56:12 -0600407 struct cbfs_cachenode *cache_node = priv->file_cache;
Gabe Black7f8574c2012-10-12 14:26:11 +0000408
Simon Glassbc94f0a2019-08-14 19:56:12 -0600409 if (!priv->initialized) {
Simon Glassf152bf02019-08-14 19:56:13 -0600410 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black7f8574c2012-10-12 14:26:11 +0000411 return NULL;
412 }
413
414 while (cache_node) {
415 if (!strcmp(name, cache_node->name))
416 break;
417 cache_node = cache_node->next;
418 }
419 if (!cache_node)
Simon Glassf152bf02019-08-14 19:56:13 -0600420 priv->result = CBFS_FILE_NOT_FOUND;
Gabe Black7f8574c2012-10-12 14:26:11 +0000421 else
Simon Glassf152bf02019-08-14 19:56:13 -0600422 priv->result = CBFS_SUCCESS;
Gabe Black7f8574c2012-10-12 14:26:11 +0000423
424 return cache_node;
425}
426
Simon Glassbc94f0a2019-08-14 19:56:12 -0600427const struct cbfs_cachenode *file_cbfs_find(const char *name)
428{
429 return cbfs_find_file(&cbfs_s, name);
430}
431
Simon Glass9c422152020-05-24 17:38:22 -0600432static int find_uncached(struct cbfs_priv *priv, const char *name, void *start,
433 struct cbfs_cachenode *node)
Gabe Black7f8574c2012-10-12 14:26:11 +0000434{
Simon Glass9c422152020-05-24 17:38:22 -0600435 int size = priv->header.rom_size;
436 int align = priv->header.align;
Gabe Black7f8574c2012-10-12 14:26:11 +0000437
438 while (size >= align) {
Simon Glassc72d4a82020-05-24 17:38:15 -0600439 int used;
Simon Glass9c422152020-05-24 17:38:22 -0600440 int ret;
Gabe Black7f8574c2012-10-12 14:26:11 +0000441
Simon Glass9c422152020-05-24 17:38:22 -0600442 ret = file_cbfs_next_file(priv, start, size, align, node,
Simon Glass650b2412020-05-24 17:38:12 -0600443 &used);
Simon Glassc72d4a82020-05-24 17:38:15 -0600444 if (ret == -ENOENT)
Gabe Black7f8574c2012-10-12 14:26:11 +0000445 break;
Simon Glassc72d4a82020-05-24 17:38:15 -0600446 else if (ret)
Simon Glass9c422152020-05-24 17:38:22 -0600447 return ret;
448 if (!strcmp(name, node->name))
449 return 0;
Gabe Black7f8574c2012-10-12 14:26:11 +0000450
451 size -= used;
452 start += used;
453 }
Simon Glass9c422152020-05-24 17:38:22 -0600454 priv->result = CBFS_FILE_NOT_FOUND;
455
456 return -ENOENT;
457}
458
459int file_cbfs_find_uncached(ulong end_of_rom, const char *name,
460 struct cbfs_cachenode *node)
461{
462 struct cbfs_priv priv;
463 void *start;
464 int ret;
465
466 ret = file_cbfs_load_header(&priv, end_of_rom);
467 if (ret)
468 return ret;
469 start = priv.start;
470
471 return find_uncached(&priv, name, start, node);
Gabe Black7f8574c2012-10-12 14:26:11 +0000472}
473
Simon Glass333a97d2020-05-24 17:38:23 -0600474int file_cbfs_find_uncached_base(ulong base, const char *name,
475 struct cbfs_cachenode *node)
476{
477 struct cbfs_priv priv;
478 int ret;
479
480 ret = cbfs_load_header_ptr(&priv, base);
481 if (ret)
482 return ret;
483
484 return find_uncached(&priv, name, (void *)base, node);
485}
486
Gabe Black7f8574c2012-10-12 14:26:11 +0000487const char *file_cbfs_name(const struct cbfs_cachenode *file)
488{
Simon Glassf152bf02019-08-14 19:56:13 -0600489 cbfs_s.result = CBFS_SUCCESS;
490
Gabe Black7f8574c2012-10-12 14:26:11 +0000491 return file->name;
492}
493
494u32 file_cbfs_size(const struct cbfs_cachenode *file)
495{
Simon Glassf152bf02019-08-14 19:56:13 -0600496 cbfs_s.result = CBFS_SUCCESS;
497
Gabe Black7f8574c2012-10-12 14:26:11 +0000498 return file->data_length;
499}
500
501u32 file_cbfs_type(const struct cbfs_cachenode *file)
502{
Simon Glassf152bf02019-08-14 19:56:13 -0600503 cbfs_s.result = CBFS_SUCCESS;
504
Gabe Black7f8574c2012-10-12 14:26:11 +0000505 return file->type;
506}
507
508long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
509 unsigned long maxsize)
510{
511 u32 size;
512
513 size = file->data_length;
514 if (maxsize && size > maxsize)
515 size = maxsize;
516
517 memcpy(buffer, file->data, size);
Simon Glassf152bf02019-08-14 19:56:13 -0600518 cbfs_s.result = CBFS_SUCCESS;
Gabe Black7f8574c2012-10-12 14:26:11 +0000519
Gabe Black7f8574c2012-10-12 14:26:11 +0000520 return size;
521}