blob: 415ea28b871d36f95cecf7ee1b3ba46cbb36a39e [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>
Simon Glass0621b5e2020-05-24 17:38:24 -06008#include <log.h>
Gabe Black84cd9322012-10-12 14:26:11 +00009#include <malloc.h>
10#include <asm/byteorder.h>
11
Simon Glassa2c528f2020-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 Glassfc7b9e12019-08-14 19:56:11 -060015static const u32 good_magic = 0x4f524243;
16static const u8 good_file_magic[] = "LARCHIVE";
Simon Glass02e4af62019-08-14 19:56:12 -060017
Simon Glassc685f8b2020-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 Glass02e4af62019-08-14 19:56:12 -060027struct cbfs_priv {
Simon Glass381e1132020-05-24 17:38:14 -060028 bool initialized;
Simon Glassc685f8b2020-05-24 17:38:20 -060029 void *start;
Simon Glass02e4af62019-08-14 19:56:12 -060030 struct cbfs_header header;
31 struct cbfs_cachenode *file_cache;
Simon Glassc7f16932019-08-14 19:56:13 -060032 enum cbfs_result result;
Simon Glass02e4af62019-08-14 19:56:12 -060033};
34
35static struct cbfs_priv cbfs_s;
Gabe Black84cd9322012-10-12 14:26:11 +000036
37const char *file_cbfs_error(void)
38{
Simon Glassc7f16932019-08-14 19:56:13 -060039 switch (cbfs_s.result) {
Gabe Black84cd9322012-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 Glassc7f16932019-08-14 19:56:13 -060055enum cbfs_result cbfs_get_result(void)
56{
57 return cbfs_s.result;
58}
59
Gabe Black84cd9322012-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 Glassababe102019-07-08 13:18:22 -060078 dest->attributes_offset = be32_to_cpu(src->attributes_offset);
Gabe Black84cd9322012-10-12 14:26:11 +000079 dest->offset = be32_to_cpu(src->offset);
80}
81
Simon Glass70a394a2021-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 Glassa202f172021-03-15 18:00:16 +130094 uint offset;
Simon Glass70a394a2021-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 Glassa202f172021-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 Glass70a394a2021-03-15 18:00:14 +1300129
130 return 0;
131}
132
Gabe Black84cd9322012-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 Glass11a38a22021-03-15 18:00:10 +1300141 * @param node A pointer to the file structure to load.
Gabe Black84cd9322012-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 Glassc7bef7c2020-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 Black84cd9322012-10-12 14:26:11 +0000147 */
Simon Glasse82ab512020-05-24 17:38:19 -0600148static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size,
Simon Glass11a38a22021-03-15 18:00:10 +1300149 int align, struct cbfs_cachenode *node,
Simon Glassc7bef7c2020-05-24 17:38:15 -0600150 int *used)
Gabe Black84cd9322012-10-12 14:26:11 +0000151{
152 struct cbfs_fileheader header;
153
154 *used = 0;
155
156 while (size >= align) {
Simon Glasse82ab512020-05-24 17:38:19 -0600157 const struct cbfs_fileheader *file_header = start;
Simon Glass70a394a2021-03-15 18:00:14 +1300158 int ret;
Gabe Black84cd9322012-10-12 14:26:11 +0000159
160 /* Check if there's a file here. */
Simon Glassad79d602019-08-14 19:56:15 -0600161 if (memcmp(good_file_magic, &file_header->magic,
162 sizeof(file_header->magic))) {
Gabe Black84cd9322012-10-12 14:26:11 +0000163 *used += align;
164 size -= align;
165 start += align;
166 continue;
167 }
168
Simon Glassad79d602019-08-14 19:56:15 -0600169 swap_file_header(&header, file_header);
Simon Glass70a394a2021-03-15 18:00:14 +1300170 ret = fill_node(node, start, &header);
171 if (ret) {
Simon Glassc7f16932019-08-14 19:56:13 -0600172 priv->result = CBFS_BAD_FILE;
Simon Glass70a394a2021-03-15 18:00:14 +1300173 return log_msg_ret("fill", ret);
Gabe Black84cd9322012-10-12 14:26:11 +0000174 }
Gabe Black84cd9322012-10-12 14:26:11 +0000175
Simon Glass0e2fee52021-03-15 18:00:15 +1300176 *used += ALIGN(header.len, align);
Simon Glassc7bef7c2020-05-24 17:38:15 -0600177 return 0;
Gabe Black84cd9322012-10-12 14:26:11 +0000178 }
Simon Glassc7bef7c2020-05-24 17:38:15 -0600179
180 return -ENOENT;
Gabe Black84cd9322012-10-12 14:26:11 +0000181}
182
183/* Look through a CBFS instance and copy file metadata into regular memory. */
Simon Glassc685f8b2020-05-24 17:38:20 -0600184static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align)
Gabe Black84cd9322012-10-12 14:26:11 +0000185{
186 struct cbfs_cachenode *cache_node;
Simon Glass11a38a22021-03-15 18:00:10 +1300187 struct cbfs_cachenode *node;
Simon Glass02e4af62019-08-14 19:56:12 -0600188 struct cbfs_cachenode **cache_tail = &priv->file_cache;
Simon Glassc685f8b2020-05-24 17:38:20 -0600189 void *start;
Gabe Black84cd9322012-10-12 14:26:11 +0000190
191 /* Clear out old information. */
Simon Glass02e4af62019-08-14 19:56:12 -0600192 cache_node = priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000193 while (cache_node) {
Simon Glassad79d602019-08-14 19:56:15 -0600194 struct cbfs_cachenode *old_node = cache_node;
Gabe Black84cd9322012-10-12 14:26:11 +0000195 cache_node = cache_node->next;
Simon Glassad79d602019-08-14 19:56:15 -0600196 free(old_node);
Gabe Black84cd9322012-10-12 14:26:11 +0000197 }
Simon Glass02e4af62019-08-14 19:56:12 -0600198 priv->file_cache = NULL;
Gabe Black84cd9322012-10-12 14:26:11 +0000199
Simon Glassc685f8b2020-05-24 17:38:20 -0600200 start = priv->start;
Gabe Black84cd9322012-10-12 14:26:11 +0000201 while (size >= align) {
Simon Glassc7bef7c2020-05-24 17:38:15 -0600202 int used;
Simon Glassea38ee92020-05-24 17:38:12 -0600203 int ret;
Gabe Black84cd9322012-10-12 14:26:11 +0000204
Simon Glassad663232021-03-15 18:00:17 +1300205 node = malloc(sizeof(struct cbfs_cachenode));
Simon Glass11a38a22021-03-15 18:00:10 +1300206 if (!node)
Simon Glassc7bef7c2020-05-24 17:38:15 -0600207 return -ENOMEM;
Simon Glass11a38a22021-03-15 18:00:10 +1300208 ret = file_cbfs_next_file(priv, start, size, align, node,
Simon Glassea38ee92020-05-24 17:38:12 -0600209 &used);
Gabe Black84cd9322012-10-12 14:26:11 +0000210
Simon Glassea38ee92020-05-24 17:38:12 -0600211 if (ret < 0) {
Simon Glass11a38a22021-03-15 18:00:10 +1300212 free(node);
Simon Glassc7bef7c2020-05-24 17:38:15 -0600213 if (ret == -ENOENT)
214 break;
215 return ret;
Gabe Black84cd9322012-10-12 14:26:11 +0000216 }
Simon Glass11a38a22021-03-15 18:00:10 +1300217 *cache_tail = node;
218 cache_tail = &node->next;
Gabe Black84cd9322012-10-12 14:26:11 +0000219
220 size -= used;
221 start += used;
222 }
Simon Glassc7f16932019-08-14 19:56:13 -0600223 priv->result = CBFS_SUCCESS;
Simon Glassc7bef7c2020-05-24 17:38:15 -0600224
225 return 0;
Gabe Black84cd9322012-10-12 14:26:11 +0000226}
227
Simon Glass9dc23552020-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 Black84cd9322012-10-12 14:26:11 +0000238{
Simon Glass54e19252020-05-24 17:38:16 -0600239 struct cbfs_header *header = &priv->header;
Gabe Black84cd9322012-10-12 14:26:11 +0000240 struct cbfs_header *header_in_rom;
241
Simon Glass9dc23552020-05-24 17:38:18 -0600242 memset(priv, '\0', sizeof(*priv));
243 header_in_rom = (struct cbfs_header *)addr;
Gabe Black84cd9322012-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 Glass54e19252020-05-24 17:38:16 -0600248 priv->result = CBFS_BAD_HEADER;
Simon Glass9dc23552020-05-24 17:38:18 -0600249 return -ENXIO;
Gabe Black84cd9322012-10-12 14:26:11 +0000250 }
Simon Glass9dc23552020-05-24 17:38:18 -0600251
Gabe Black84cd9322012-10-12 14:26:11 +0000252 return 0;
253}
254
Simon Glass9dc23552020-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)
263{
264 int offset = *(u32 *)(end_of_rom - 3);
Simon Glassc685f8b2020-05-24 17:38:20 -0600265 int ret;
Simon Glass9dc23552020-05-24 17:38:18 -0600266
Simon Glassc685f8b2020-05-24 17:38:20 -0600267 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);
271
272 return 0;
Simon Glass9dc23552020-05-24 17:38:18 -0600273}
274
275/**
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 */
Simon Glassa2c528f2020-05-24 17:38:17 -0600282static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
Simon Glass630b2f32019-08-14 19:56:14 -0600283{
Simon Glassc685f8b2020-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 Glass630b2f32019-08-14 19:56:14 -0600292}
293
Simon Glass0e7b6312020-05-24 17:38:21 -0600294static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
Gabe Black84cd9322012-10-12 14:26:11 +0000295{
Simon Glass0e7b6312020-05-24 17:38:21 -0600296 int ret;
Gabe Black84cd9322012-10-12 14:26:11 +0000297
Simon Glass0e7b6312020-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 Glass02e4af62019-08-14 19:56:12 -0600309}
310
Simon Glass0e7b6312020-05-24 17:38:21 -0600311int file_cbfs_init(ulong end_of_rom)
Simon Glass02e4af62019-08-14 19:56:12 -0600312{
Simon Glass0e7b6312020-05-24 17:38:21 -0600313 return cbfs_init(&cbfs_s, end_of_rom);
Gabe Black84cd9322012-10-12 14:26:11 +0000314}
315
Simon Glass5536f122021-03-15 18:00:12 +1300316int cbfs_init_mem(ulong base, ulong size, bool require_hdr,
317 struct cbfs_priv **privp)
Simon Glass630b2f32019-08-14 19:56:14 -0600318{
319 struct cbfs_priv priv_s, *priv = &priv_s;
320 int ret;
321
322 /*
Simon Glass5536f122021-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 Glass630b2f32019-08-14 19:56:14 -0600326 */
Simon Glass9dc23552020-05-24 17:38:18 -0600327 ret = cbfs_load_header_ptr(priv, base);
Simon Glass5536f122021-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 Glass630b2f32019-08-14 19:56:14 -0600336
Simon Glass0621b5e2020-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 Glass630b2f32019-08-14 19:56:14 -0600341
Simon Glass381e1132020-05-24 17:38:14 -0600342 priv->initialized = true;
Simon Glass630b2f32019-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 Black84cd9322012-10-12 14:26:11 +0000352const struct cbfs_header *file_cbfs_get_header(void)
353{
Simon Glass02e4af62019-08-14 19:56:12 -0600354 struct cbfs_priv *priv = &cbfs_s;
355
356 if (priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600357 priv->result = CBFS_SUCCESS;
Simon Glass02e4af62019-08-14 19:56:12 -0600358 return &priv->header;
Gabe Black84cd9322012-10-12 14:26:11 +0000359 } else {
Simon Glassc7f16932019-08-14 19:56:13 -0600360 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black84cd9322012-10-12 14:26:11 +0000361 return NULL;
362 }
363}
364
Simon Glassc4f5b5d2021-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 Black84cd9322012-10-12 14:26:11 +0000376const struct cbfs_cachenode *file_cbfs_get_first(void)
377{
Simon Glass02e4af62019-08-14 19:56:12 -0600378 struct cbfs_priv *priv = &cbfs_s;
379
380 if (!priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600381 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black84cd9322012-10-12 14:26:11 +0000382 return NULL;
383 } else {
Simon Glassc7f16932019-08-14 19:56:13 -0600384 priv->result = CBFS_SUCCESS;
Simon Glass02e4af62019-08-14 19:56:12 -0600385 return priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000386 }
387}
388
389void file_cbfs_get_next(const struct cbfs_cachenode **file)
390{
Simon Glass02e4af62019-08-14 19:56:12 -0600391 struct cbfs_priv *priv = &cbfs_s;
392
393 if (!priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600394 priv->result = CBFS_NOT_INITIALIZED;
Simon Glass02e4af62019-08-14 19:56:12 -0600395 *file = NULL;
Gabe Black84cd9322012-10-12 14:26:11 +0000396 return;
397 }
398
399 if (*file)
400 *file = (*file)->next;
Simon Glassc7f16932019-08-14 19:56:13 -0600401 priv->result = CBFS_SUCCESS;
Gabe Black84cd9322012-10-12 14:26:11 +0000402}
403
Simon Glass02e4af62019-08-14 19:56:12 -0600404const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
405 const char *name)
Gabe Black84cd9322012-10-12 14:26:11 +0000406{
Simon Glass02e4af62019-08-14 19:56:12 -0600407 struct cbfs_cachenode *cache_node = priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000408
Simon Glass02e4af62019-08-14 19:56:12 -0600409 if (!priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600410 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black84cd9322012-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 Glassc7f16932019-08-14 19:56:13 -0600420 priv->result = CBFS_FILE_NOT_FOUND;
Gabe Black84cd9322012-10-12 14:26:11 +0000421 else
Simon Glassc7f16932019-08-14 19:56:13 -0600422 priv->result = CBFS_SUCCESS;
Gabe Black84cd9322012-10-12 14:26:11 +0000423
424 return cache_node;
425}
426
Simon Glass02e4af62019-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 Glass924e3462020-05-24 17:38:22 -0600432static int find_uncached(struct cbfs_priv *priv, const char *name, void *start,
433 struct cbfs_cachenode *node)
Gabe Black84cd9322012-10-12 14:26:11 +0000434{
Simon Glass924e3462020-05-24 17:38:22 -0600435 int size = priv->header.rom_size;
436 int align = priv->header.align;
Gabe Black84cd9322012-10-12 14:26:11 +0000437
438 while (size >= align) {
Simon Glassc7bef7c2020-05-24 17:38:15 -0600439 int used;
Simon Glass924e3462020-05-24 17:38:22 -0600440 int ret;
Gabe Black84cd9322012-10-12 14:26:11 +0000441
Simon Glass924e3462020-05-24 17:38:22 -0600442 ret = file_cbfs_next_file(priv, start, size, align, node,
Simon Glassea38ee92020-05-24 17:38:12 -0600443 &used);
Simon Glassc7bef7c2020-05-24 17:38:15 -0600444 if (ret == -ENOENT)
Gabe Black84cd9322012-10-12 14:26:11 +0000445 break;
Simon Glassc7bef7c2020-05-24 17:38:15 -0600446 else if (ret)
Simon Glass924e3462020-05-24 17:38:22 -0600447 return ret;
448 if (!strcmp(name, node->name))
449 return 0;
Gabe Black84cd9322012-10-12 14:26:11 +0000450
451 size -= used;
452 start += used;
453 }
Simon Glass924e3462020-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 Black84cd9322012-10-12 14:26:11 +0000472}
473
Simon Glass03d4c292020-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 Black84cd9322012-10-12 14:26:11 +0000487const char *file_cbfs_name(const struct cbfs_cachenode *file)
488{
Simon Glassc7f16932019-08-14 19:56:13 -0600489 cbfs_s.result = CBFS_SUCCESS;
490
Gabe Black84cd9322012-10-12 14:26:11 +0000491 return file->name;
492}
493
494u32 file_cbfs_size(const struct cbfs_cachenode *file)
495{
Simon Glassc7f16932019-08-14 19:56:13 -0600496 cbfs_s.result = CBFS_SUCCESS;
497
Gabe Black84cd9322012-10-12 14:26:11 +0000498 return file->data_length;
499}
500
501u32 file_cbfs_type(const struct cbfs_cachenode *file)
502{
Simon Glassc7f16932019-08-14 19:56:13 -0600503 cbfs_s.result = CBFS_SUCCESS;
504
Gabe Black84cd9322012-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 Glassc7f16932019-08-14 19:56:13 -0600518 cbfs_s.result = CBFS_SUCCESS;
Gabe Black84cd9322012-10-12 14:26:11 +0000519
Gabe Black84cd9322012-10-12 14:26:11 +0000520 return size;
521}