1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
10 #include <asm/byteorder.h>
12 /* Offset of master header from the start of a coreboot ROM */
13 #define MASTER_HDR_OFFSET 0x38
15 static const u32 good_magic = 0x4f524243;
16 static const u8 good_file_magic[] = "LARCHIVE";
19 * struct cbfs_priv - Private data for this driver
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
30 struct cbfs_header header;
31 struct cbfs_cachenode *file_cache;
32 enum cbfs_result result;
35 static struct cbfs_priv cbfs_s;
37 const char *file_cbfs_error(void)
39 switch (cbfs_s.result) {
42 case CBFS_NOT_INITIALIZED:
43 return "CBFS not initialized";
45 return "Bad CBFS header";
47 return "Bad CBFS file";
48 case CBFS_FILE_NOT_FOUND:
49 return "File not found";
55 enum cbfs_result cbfs_get_result(void)
60 /* Do endian conversion on the CBFS header structure. */
61 static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
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);
71 /* Do endian conversion on a CBFS file header. */
72 static void swap_file_header(struct cbfs_fileheader *dest,
73 const struct cbfs_fileheader *src)
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);
78 dest->attributes_offset = be32_to_cpu(src->attributes_offset);
79 dest->offset = be32_to_cpu(src->offset);
83 * fill_node() - Fill a node struct with information from the CBFS
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
90 static int fill_node(struct cbfs_cachenode *node, void *start,
91 struct cbfs_fileheader *header)
95 /* Check the header is large enough */
96 if (header->offset < sizeof(struct cbfs_fileheader))
100 node->type = header->type;
101 node->data = start + header->offset;
102 node->data_length = header->len;
103 name_len = header->offset - sizeof(struct cbfs_fileheader);
104 node->name = start + sizeof(struct cbfs_fileheader);
105 node->name_length = name_len;
106 node->attr_offset = header->attributes_offset;
112 * Given a starting position in memory, scan forward, bounded by a size, and
113 * find the next valid CBFS file. No memory is allocated by this function. The
114 * caller is responsible for allocating space for the new file structure.
116 * @param start The location in memory to start from.
117 * @param size The size of the memory region to search.
118 * @param align The alignment boundaries to check on.
119 * @param node A pointer to the file structure to load.
120 * @param used A pointer to the count of of bytes scanned through,
121 * including the file if one is found.
123 * @return 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header
126 static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size,
127 int align, struct cbfs_cachenode *node,
130 struct cbfs_fileheader header;
134 while (size >= align) {
135 const struct cbfs_fileheader *file_header = start;
139 /* Check if there's a file here. */
140 if (memcmp(good_file_magic, &file_header->magic,
141 sizeof(file_header->magic))) {
148 swap_file_header(&header, file_header);
149 ret = fill_node(node, start, &header);
151 priv->result = CBFS_BAD_FILE;
152 return log_msg_ret("fill", ret);
157 step = step + align - step % align;
166 /* Look through a CBFS instance and copy file metadata into regular memory. */
167 static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align)
169 struct cbfs_cachenode *cache_node;
170 struct cbfs_cachenode *node;
171 struct cbfs_cachenode **cache_tail = &priv->file_cache;
174 /* Clear out old information. */
175 cache_node = priv->file_cache;
177 struct cbfs_cachenode *old_node = cache_node;
178 cache_node = cache_node->next;
181 priv->file_cache = NULL;
184 while (size >= align) {
188 node = (struct cbfs_cachenode *)
189 malloc(sizeof(struct cbfs_cachenode));
192 ret = file_cbfs_next_file(priv, start, size, align, node,
202 cache_tail = &node->next;
207 priv->result = CBFS_SUCCESS;
213 * load_header() - Load the CBFS header
215 * Get the CBFS header out of the ROM and do endian conversion.
217 * @priv: Private data, which is inited by this function
218 * @addr: Address of CBFS header in memory-mapped SPI flash
219 * @return 0 if OK, -ENXIO if the header is bad
221 static int load_header(struct cbfs_priv *priv, ulong addr)
223 struct cbfs_header *header = &priv->header;
224 struct cbfs_header *header_in_rom;
226 memset(priv, '\0', sizeof(*priv));
227 header_in_rom = (struct cbfs_header *)addr;
228 swap_header(header, header_in_rom);
230 if (header->magic != good_magic || header->offset >
231 header->rom_size - header->boot_block_size) {
232 priv->result = CBFS_BAD_HEADER;
240 * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end
242 * @priv: Private data, which is inited by this function
243 * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff)
244 * @return 0 if OK, -ENXIO if the header is bad
246 static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
248 int offset = *(u32 *)(end_of_rom - 3);
251 ret = load_header(priv, end_of_rom + offset + 1);
254 priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size);
260 * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base
262 * @priv: Private data, which is inited by this function
263 * @base: Address of the first byte of the ROM (e.g. 0xff000000)
264 * @return 0 if OK, -ENXIO if the header is bad
266 static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
270 ret = load_header(priv, base + MASTER_HDR_OFFSET);
273 priv->start = (void *)base;
278 static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
282 ret = file_cbfs_load_header(priv, end_of_rom);
286 ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
290 priv->initialized = true;
295 int file_cbfs_init(ulong end_of_rom)
297 return cbfs_init(&cbfs_s, end_of_rom);
300 int cbfs_init_mem(ulong base, ulong size, bool require_hdr,
301 struct cbfs_priv **privp)
303 struct cbfs_priv priv_s, *priv = &priv_s;
307 * Use a local variable to start with until we know that the * CBFS is
308 * valid. Note that size is detected from the header, if present,
309 * meaning the parameter is ignored.
311 ret = cbfs_load_header_ptr(priv, base);
313 if (require_hdr || size == CBFS_SIZE_UNKNOWN)
315 memset(priv, '\0', sizeof(struct cbfs_priv));
316 priv->header.rom_size = size;
317 priv->header.align = CBFS_ALIGN_SIZE;
318 priv->start = (void *)base;
321 ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
324 return log_msg_ret("fill", ret);
326 priv->initialized = true;
327 priv = malloc(sizeof(priv_s));
330 memcpy(priv, &priv_s, sizeof(priv_s));
336 const struct cbfs_header *file_cbfs_get_header(void)
338 struct cbfs_priv *priv = &cbfs_s;
340 if (priv->initialized) {
341 priv->result = CBFS_SUCCESS;
342 return &priv->header;
344 priv->result = CBFS_NOT_INITIALIZED;
349 const struct cbfs_cachenode *cbfs_get_first(const struct cbfs_priv *priv)
351 return priv->file_cache;
354 void cbfs_get_next(const struct cbfs_cachenode **filep)
357 *filep = (*filep)->next;
360 const struct cbfs_cachenode *file_cbfs_get_first(void)
362 struct cbfs_priv *priv = &cbfs_s;
364 if (!priv->initialized) {
365 priv->result = CBFS_NOT_INITIALIZED;
368 priv->result = CBFS_SUCCESS;
369 return priv->file_cache;
373 void file_cbfs_get_next(const struct cbfs_cachenode **file)
375 struct cbfs_priv *priv = &cbfs_s;
377 if (!priv->initialized) {
378 priv->result = CBFS_NOT_INITIALIZED;
384 *file = (*file)->next;
385 priv->result = CBFS_SUCCESS;
388 const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
391 struct cbfs_cachenode *cache_node = priv->file_cache;
393 if (!priv->initialized) {
394 priv->result = CBFS_NOT_INITIALIZED;
399 if (!strcmp(name, cache_node->name))
401 cache_node = cache_node->next;
404 priv->result = CBFS_FILE_NOT_FOUND;
406 priv->result = CBFS_SUCCESS;
411 const struct cbfs_cachenode *file_cbfs_find(const char *name)
413 return cbfs_find_file(&cbfs_s, name);
416 static int find_uncached(struct cbfs_priv *priv, const char *name, void *start,
417 struct cbfs_cachenode *node)
419 int size = priv->header.rom_size;
420 int align = priv->header.align;
422 while (size >= align) {
426 ret = file_cbfs_next_file(priv, start, size, align, node,
432 if (!strcmp(name, node->name))
438 priv->result = CBFS_FILE_NOT_FOUND;
443 int file_cbfs_find_uncached(ulong end_of_rom, const char *name,
444 struct cbfs_cachenode *node)
446 struct cbfs_priv priv;
450 ret = file_cbfs_load_header(&priv, end_of_rom);
455 return find_uncached(&priv, name, start, node);
458 int file_cbfs_find_uncached_base(ulong base, const char *name,
459 struct cbfs_cachenode *node)
461 struct cbfs_priv priv;
464 ret = cbfs_load_header_ptr(&priv, base);
468 return find_uncached(&priv, name, (void *)base, node);
471 const char *file_cbfs_name(const struct cbfs_cachenode *file)
473 cbfs_s.result = CBFS_SUCCESS;
478 u32 file_cbfs_size(const struct cbfs_cachenode *file)
480 cbfs_s.result = CBFS_SUCCESS;
482 return file->data_length;
485 u32 file_cbfs_type(const struct cbfs_cachenode *file)
487 cbfs_s.result = CBFS_SUCCESS;
492 long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
493 unsigned long maxsize)
497 size = file->data_length;
498 if (maxsize && size > maxsize)
501 memcpy(buffer, file->data, size);
502 cbfs_s.result = CBFS_SUCCESS;