blob: 714f4baafc9e89434991ec2aec8dbfb0cbd67dd5 [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)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010088 * Return: 0 if OK, -EBADF if the header is too small
Simon Glass70a394a2021-03-15 18:00:14 +130089 */
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 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100145 * Return: 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header
Simon Glassc7bef7c2020-05-24 17:38:15 -0600146 * 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 Glass99eaf1f2021-05-13 19:39:27 -0600170 if (header.offset >= size)
171 return log_msg_ret("range", -E2BIG);
Simon Glass70a394a2021-03-15 18:00:14 +1300172 ret = fill_node(node, start, &header);
173 if (ret) {
Simon Glassc7f16932019-08-14 19:56:13 -0600174 priv->result = CBFS_BAD_FILE;
Simon Glass70a394a2021-03-15 18:00:14 +1300175 return log_msg_ret("fill", ret);
Gabe Black84cd9322012-10-12 14:26:11 +0000176 }
Gabe Black84cd9322012-10-12 14:26:11 +0000177
Simon Glass0e2fee52021-03-15 18:00:15 +1300178 *used += ALIGN(header.len, align);
Simon Glassc7bef7c2020-05-24 17:38:15 -0600179 return 0;
Gabe Black84cd9322012-10-12 14:26:11 +0000180 }
Simon Glassc7bef7c2020-05-24 17:38:15 -0600181
182 return -ENOENT;
Gabe Black84cd9322012-10-12 14:26:11 +0000183}
184
185/* Look through a CBFS instance and copy file metadata into regular memory. */
Simon Glassc685f8b2020-05-24 17:38:20 -0600186static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align)
Gabe Black84cd9322012-10-12 14:26:11 +0000187{
188 struct cbfs_cachenode *cache_node;
Simon Glass11a38a22021-03-15 18:00:10 +1300189 struct cbfs_cachenode *node;
Simon Glass02e4af62019-08-14 19:56:12 -0600190 struct cbfs_cachenode **cache_tail = &priv->file_cache;
Simon Glassc685f8b2020-05-24 17:38:20 -0600191 void *start;
Gabe Black84cd9322012-10-12 14:26:11 +0000192
193 /* Clear out old information. */
Simon Glass02e4af62019-08-14 19:56:12 -0600194 cache_node = priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000195 while (cache_node) {
Simon Glassad79d602019-08-14 19:56:15 -0600196 struct cbfs_cachenode *old_node = cache_node;
Gabe Black84cd9322012-10-12 14:26:11 +0000197 cache_node = cache_node->next;
Simon Glassad79d602019-08-14 19:56:15 -0600198 free(old_node);
Gabe Black84cd9322012-10-12 14:26:11 +0000199 }
Simon Glass02e4af62019-08-14 19:56:12 -0600200 priv->file_cache = NULL;
Gabe Black84cd9322012-10-12 14:26:11 +0000201
Simon Glassc685f8b2020-05-24 17:38:20 -0600202 start = priv->start;
Gabe Black84cd9322012-10-12 14:26:11 +0000203 while (size >= align) {
Simon Glassc7bef7c2020-05-24 17:38:15 -0600204 int used;
Simon Glassea38ee92020-05-24 17:38:12 -0600205 int ret;
Gabe Black84cd9322012-10-12 14:26:11 +0000206
Simon Glassad663232021-03-15 18:00:17 +1300207 node = malloc(sizeof(struct cbfs_cachenode));
Simon Glass11a38a22021-03-15 18:00:10 +1300208 if (!node)
Simon Glassc7bef7c2020-05-24 17:38:15 -0600209 return -ENOMEM;
Simon Glass11a38a22021-03-15 18:00:10 +1300210 ret = file_cbfs_next_file(priv, start, size, align, node,
Simon Glassea38ee92020-05-24 17:38:12 -0600211 &used);
Gabe Black84cd9322012-10-12 14:26:11 +0000212
Simon Glassea38ee92020-05-24 17:38:12 -0600213 if (ret < 0) {
Simon Glass11a38a22021-03-15 18:00:10 +1300214 free(node);
Simon Glassc7bef7c2020-05-24 17:38:15 -0600215 if (ret == -ENOENT)
216 break;
217 return ret;
Gabe Black84cd9322012-10-12 14:26:11 +0000218 }
Simon Glass11a38a22021-03-15 18:00:10 +1300219 *cache_tail = node;
220 cache_tail = &node->next;
Gabe Black84cd9322012-10-12 14:26:11 +0000221
222 size -= used;
223 start += used;
224 }
Simon Glassc7f16932019-08-14 19:56:13 -0600225 priv->result = CBFS_SUCCESS;
Simon Glassc7bef7c2020-05-24 17:38:15 -0600226
227 return 0;
Gabe Black84cd9322012-10-12 14:26:11 +0000228}
229
Simon Glass9dc23552020-05-24 17:38:18 -0600230/**
231 * load_header() - Load the CBFS header
232 *
233 * Get the CBFS header out of the ROM and do endian conversion.
234 *
235 * @priv: Private data, which is inited by this function
236 * @addr: Address of CBFS header in memory-mapped SPI flash
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100237 * Return: 0 if OK, -ENXIO if the header is bad
Simon Glass9dc23552020-05-24 17:38:18 -0600238 */
239static int load_header(struct cbfs_priv *priv, ulong addr)
Gabe Black84cd9322012-10-12 14:26:11 +0000240{
Simon Glass54e19252020-05-24 17:38:16 -0600241 struct cbfs_header *header = &priv->header;
Gabe Black84cd9322012-10-12 14:26:11 +0000242 struct cbfs_header *header_in_rom;
243
Simon Glass9dc23552020-05-24 17:38:18 -0600244 memset(priv, '\0', sizeof(*priv));
245 header_in_rom = (struct cbfs_header *)addr;
Gabe Black84cd9322012-10-12 14:26:11 +0000246 swap_header(header, header_in_rom);
247
248 if (header->magic != good_magic || header->offset >
249 header->rom_size - header->boot_block_size) {
Simon Glass54e19252020-05-24 17:38:16 -0600250 priv->result = CBFS_BAD_HEADER;
Simon Glass9dc23552020-05-24 17:38:18 -0600251 return -ENXIO;
Gabe Black84cd9322012-10-12 14:26:11 +0000252 }
Simon Glass9dc23552020-05-24 17:38:18 -0600253
Gabe Black84cd9322012-10-12 14:26:11 +0000254 return 0;
255}
256
Simon Glass9dc23552020-05-24 17:38:18 -0600257/**
258 * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end
259 *
260 * @priv: Private data, which is inited by this function
261 * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100262 * Return: 0 if OK, -ENXIO if the header is bad
Simon Glass9dc23552020-05-24 17:38:18 -0600263 */
264static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
265{
266 int offset = *(u32 *)(end_of_rom - 3);
Simon Glassc685f8b2020-05-24 17:38:20 -0600267 int ret;
Simon Glass9dc23552020-05-24 17:38:18 -0600268
Simon Glassc685f8b2020-05-24 17:38:20 -0600269 ret = load_header(priv, end_of_rom + offset + 1);
270 if (ret)
271 return ret;
272 priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size);
273
274 return 0;
Simon Glass9dc23552020-05-24 17:38:18 -0600275}
276
277/**
278 * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base
279 *
280 * @priv: Private data, which is inited by this function
281 * @base: Address of the first byte of the ROM (e.g. 0xff000000)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100282 * Return: 0 if OK, -ENXIO if the header is bad
Simon Glass9dc23552020-05-24 17:38:18 -0600283 */
Simon Glassa2c528f2020-05-24 17:38:17 -0600284static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
Simon Glass630b2f32019-08-14 19:56:14 -0600285{
Simon Glassc685f8b2020-05-24 17:38:20 -0600286 int ret;
287
288 ret = load_header(priv, base + MASTER_HDR_OFFSET);
289 if (ret)
290 return ret;
291 priv->start = (void *)base;
292
293 return 0;
Simon Glass630b2f32019-08-14 19:56:14 -0600294}
295
Simon Glass0e7b6312020-05-24 17:38:21 -0600296static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
Gabe Black84cd9322012-10-12 14:26:11 +0000297{
Simon Glass0e7b6312020-05-24 17:38:21 -0600298 int ret;
Gabe Black84cd9322012-10-12 14:26:11 +0000299
Simon Glass0e7b6312020-05-24 17:38:21 -0600300 ret = file_cbfs_load_header(priv, end_of_rom);
301 if (ret)
302 return ret;
303
304 ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
305 priv->header.align);
306 if (ret)
307 return ret;
308 priv->initialized = true;
309
310 return 0;
Simon Glass02e4af62019-08-14 19:56:12 -0600311}
312
Simon Glass0e7b6312020-05-24 17:38:21 -0600313int file_cbfs_init(ulong end_of_rom)
Simon Glass02e4af62019-08-14 19:56:12 -0600314{
Simon Glass0e7b6312020-05-24 17:38:21 -0600315 return cbfs_init(&cbfs_s, end_of_rom);
Gabe Black84cd9322012-10-12 14:26:11 +0000316}
317
Simon Glass5536f122021-03-15 18:00:12 +1300318int cbfs_init_mem(ulong base, ulong size, bool require_hdr,
319 struct cbfs_priv **privp)
Simon Glass630b2f32019-08-14 19:56:14 -0600320{
321 struct cbfs_priv priv_s, *priv = &priv_s;
322 int ret;
323
324 /*
Simon Glass5536f122021-03-15 18:00:12 +1300325 * Use a local variable to start with until we know that the * CBFS is
326 * valid. Note that size is detected from the header, if present,
327 * meaning the parameter is ignored.
Simon Glass630b2f32019-08-14 19:56:14 -0600328 */
Simon Glass9dc23552020-05-24 17:38:18 -0600329 ret = cbfs_load_header_ptr(priv, base);
Simon Glass5536f122021-03-15 18:00:12 +1300330 if (ret) {
331 if (require_hdr || size == CBFS_SIZE_UNKNOWN)
332 return ret;
333 memset(priv, '\0', sizeof(struct cbfs_priv));
334 priv->header.rom_size = size;
335 priv->header.align = CBFS_ALIGN_SIZE;
336 priv->start = (void *)base;
337 }
Simon Glass630b2f32019-08-14 19:56:14 -0600338
Simon Glass0621b5e2020-05-24 17:38:24 -0600339 ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
340 priv->header.align);
341 if (ret)
342 return log_msg_ret("fill", ret);
Simon Glass630b2f32019-08-14 19:56:14 -0600343
Simon Glass381e1132020-05-24 17:38:14 -0600344 priv->initialized = true;
Simon Glass630b2f32019-08-14 19:56:14 -0600345 priv = malloc(sizeof(priv_s));
346 if (!priv)
347 return -ENOMEM;
348 memcpy(priv, &priv_s, sizeof(priv_s));
349 *privp = priv;
350
351 return 0;
352}
353
Gabe Black84cd9322012-10-12 14:26:11 +0000354const struct cbfs_header *file_cbfs_get_header(void)
355{
Simon Glass02e4af62019-08-14 19:56:12 -0600356 struct cbfs_priv *priv = &cbfs_s;
357
358 if (priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600359 priv->result = CBFS_SUCCESS;
Simon Glass02e4af62019-08-14 19:56:12 -0600360 return &priv->header;
Gabe Black84cd9322012-10-12 14:26:11 +0000361 } else {
Simon Glassc7f16932019-08-14 19:56:13 -0600362 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black84cd9322012-10-12 14:26:11 +0000363 return NULL;
364 }
365}
366
Simon Glassc4f5b5d2021-03-15 18:00:13 +1300367const struct cbfs_cachenode *cbfs_get_first(const struct cbfs_priv *priv)
368{
369 return priv->file_cache;
370}
371
372void cbfs_get_next(const struct cbfs_cachenode **filep)
373{
374 if (*filep)
375 *filep = (*filep)->next;
376}
377
Gabe Black84cd9322012-10-12 14:26:11 +0000378const struct cbfs_cachenode *file_cbfs_get_first(void)
379{
Simon Glass02e4af62019-08-14 19:56:12 -0600380 struct cbfs_priv *priv = &cbfs_s;
381
382 if (!priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600383 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black84cd9322012-10-12 14:26:11 +0000384 return NULL;
385 } else {
Simon Glassc7f16932019-08-14 19:56:13 -0600386 priv->result = CBFS_SUCCESS;
Simon Glass02e4af62019-08-14 19:56:12 -0600387 return priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000388 }
389}
390
391void file_cbfs_get_next(const struct cbfs_cachenode **file)
392{
Simon Glass02e4af62019-08-14 19:56:12 -0600393 struct cbfs_priv *priv = &cbfs_s;
394
395 if (!priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600396 priv->result = CBFS_NOT_INITIALIZED;
Simon Glass02e4af62019-08-14 19:56:12 -0600397 *file = NULL;
Gabe Black84cd9322012-10-12 14:26:11 +0000398 return;
399 }
400
401 if (*file)
402 *file = (*file)->next;
Simon Glassc7f16932019-08-14 19:56:13 -0600403 priv->result = CBFS_SUCCESS;
Gabe Black84cd9322012-10-12 14:26:11 +0000404}
405
Simon Glass02e4af62019-08-14 19:56:12 -0600406const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
407 const char *name)
Gabe Black84cd9322012-10-12 14:26:11 +0000408{
Simon Glass02e4af62019-08-14 19:56:12 -0600409 struct cbfs_cachenode *cache_node = priv->file_cache;
Gabe Black84cd9322012-10-12 14:26:11 +0000410
Simon Glass02e4af62019-08-14 19:56:12 -0600411 if (!priv->initialized) {
Simon Glassc7f16932019-08-14 19:56:13 -0600412 priv->result = CBFS_NOT_INITIALIZED;
Gabe Black84cd9322012-10-12 14:26:11 +0000413 return NULL;
414 }
415
416 while (cache_node) {
417 if (!strcmp(name, cache_node->name))
418 break;
419 cache_node = cache_node->next;
420 }
421 if (!cache_node)
Simon Glassc7f16932019-08-14 19:56:13 -0600422 priv->result = CBFS_FILE_NOT_FOUND;
Gabe Black84cd9322012-10-12 14:26:11 +0000423 else
Simon Glassc7f16932019-08-14 19:56:13 -0600424 priv->result = CBFS_SUCCESS;
Gabe Black84cd9322012-10-12 14:26:11 +0000425
426 return cache_node;
427}
428
Simon Glass02e4af62019-08-14 19:56:12 -0600429const struct cbfs_cachenode *file_cbfs_find(const char *name)
430{
431 return cbfs_find_file(&cbfs_s, name);
432}
433
Simon Glass924e3462020-05-24 17:38:22 -0600434static int find_uncached(struct cbfs_priv *priv, const char *name, void *start,
435 struct cbfs_cachenode *node)
Gabe Black84cd9322012-10-12 14:26:11 +0000436{
Simon Glass924e3462020-05-24 17:38:22 -0600437 int size = priv->header.rom_size;
438 int align = priv->header.align;
Gabe Black84cd9322012-10-12 14:26:11 +0000439
440 while (size >= align) {
Simon Glassc7bef7c2020-05-24 17:38:15 -0600441 int used;
Simon Glass924e3462020-05-24 17:38:22 -0600442 int ret;
Gabe Black84cd9322012-10-12 14:26:11 +0000443
Simon Glass924e3462020-05-24 17:38:22 -0600444 ret = file_cbfs_next_file(priv, start, size, align, node,
Simon Glassea38ee92020-05-24 17:38:12 -0600445 &used);
Simon Glassc7bef7c2020-05-24 17:38:15 -0600446 if (ret == -ENOENT)
Gabe Black84cd9322012-10-12 14:26:11 +0000447 break;
Simon Glassc7bef7c2020-05-24 17:38:15 -0600448 else if (ret)
Simon Glass924e3462020-05-24 17:38:22 -0600449 return ret;
450 if (!strcmp(name, node->name))
451 return 0;
Gabe Black84cd9322012-10-12 14:26:11 +0000452
453 size -= used;
454 start += used;
455 }
Simon Glass924e3462020-05-24 17:38:22 -0600456 priv->result = CBFS_FILE_NOT_FOUND;
457
458 return -ENOENT;
459}
460
461int file_cbfs_find_uncached(ulong end_of_rom, const char *name,
462 struct cbfs_cachenode *node)
463{
464 struct cbfs_priv priv;
465 void *start;
466 int ret;
467
468 ret = file_cbfs_load_header(&priv, end_of_rom);
469 if (ret)
470 return ret;
471 start = priv.start;
472
473 return find_uncached(&priv, name, start, node);
Gabe Black84cd9322012-10-12 14:26:11 +0000474}
475
Simon Glass03d4c292020-05-24 17:38:23 -0600476int file_cbfs_find_uncached_base(ulong base, const char *name,
477 struct cbfs_cachenode *node)
478{
479 struct cbfs_priv priv;
480 int ret;
481
482 ret = cbfs_load_header_ptr(&priv, base);
483 if (ret)
484 return ret;
485
486 return find_uncached(&priv, name, (void *)base, node);
487}
488
Gabe Black84cd9322012-10-12 14:26:11 +0000489const char *file_cbfs_name(const struct cbfs_cachenode *file)
490{
Simon Glassc7f16932019-08-14 19:56:13 -0600491 cbfs_s.result = CBFS_SUCCESS;
492
Gabe Black84cd9322012-10-12 14:26:11 +0000493 return file->name;
494}
495
496u32 file_cbfs_size(const struct cbfs_cachenode *file)
497{
Simon Glassc7f16932019-08-14 19:56:13 -0600498 cbfs_s.result = CBFS_SUCCESS;
499
Gabe Black84cd9322012-10-12 14:26:11 +0000500 return file->data_length;
501}
502
503u32 file_cbfs_type(const struct cbfs_cachenode *file)
504{
Simon Glassc7f16932019-08-14 19:56:13 -0600505 cbfs_s.result = CBFS_SUCCESS;
506
Gabe Black84cd9322012-10-12 14:26:11 +0000507 return file->type;
508}
509
510long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
511 unsigned long maxsize)
512{
513 u32 size;
514
515 size = file->data_length;
516 if (maxsize && size > maxsize)
517 size = maxsize;
518
519 memcpy(buffer, file->data, size);
Simon Glassc7f16932019-08-14 19:56:13 -0600520 cbfs_s.result = CBFS_SUCCESS;
Gabe Black84cd9322012-10-12 14:26:11 +0000521
Gabe Black84cd9322012-10-12 14:26:11 +0000522 return size;
523}