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 * Given a starting position in memory, scan forward, bounded by a size, and
84 * find the next valid CBFS file. No memory is allocated by this function. The
85 * caller is responsible for allocating space for the new file structure.
87 * @param start The location in memory to start from.
88 * @param size The size of the memory region to search.
89 * @param align The alignment boundaries to check on.
90 * @param new_node A pointer to the file structure to load.
91 * @param used A pointer to the count of of bytes scanned through,
92 * including the file if one is found.
94 * @return 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header
97 static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size,
98 int align, struct cbfs_cachenode *new_node,
101 struct cbfs_fileheader header;
105 while (size >= align) {
106 const struct cbfs_fileheader *file_header = start;
110 /* Check if there's a file here. */
111 if (memcmp(good_file_magic, &file_header->magic,
112 sizeof(file_header->magic))) {
119 swap_file_header(&header, file_header);
120 if (header.offset < sizeof(struct cbfs_fileheader)) {
121 priv->result = CBFS_BAD_FILE;
124 new_node->next = NULL;
125 new_node->type = header.type;
126 new_node->data = start + header.offset;
127 new_node->data_length = header.len;
128 name_len = header.offset - sizeof(struct cbfs_fileheader);
129 new_node->name = (char *)file_header +
130 sizeof(struct cbfs_fileheader);
131 new_node->name_length = name_len;
132 new_node->attributes_offset = header.attributes_offset;
136 step = step + align - step % align;
145 /* Look through a CBFS instance and copy file metadata into regular memory. */
146 static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align)
148 struct cbfs_cachenode *cache_node;
149 struct cbfs_cachenode *new_node;
150 struct cbfs_cachenode **cache_tail = &priv->file_cache;
153 /* Clear out old information. */
154 cache_node = priv->file_cache;
156 struct cbfs_cachenode *old_node = cache_node;
157 cache_node = cache_node->next;
160 priv->file_cache = NULL;
163 while (size >= align) {
167 new_node = (struct cbfs_cachenode *)
168 malloc(sizeof(struct cbfs_cachenode));
171 ret = file_cbfs_next_file(priv, start, size, align, new_node,
180 *cache_tail = new_node;
181 cache_tail = &new_node->next;
186 priv->result = CBFS_SUCCESS;
192 * load_header() - Load the CBFS header
194 * Get the CBFS header out of the ROM and do endian conversion.
196 * @priv: Private data, which is inited by this function
197 * @addr: Address of CBFS header in memory-mapped SPI flash
198 * @return 0 if OK, -ENXIO if the header is bad
200 static int load_header(struct cbfs_priv *priv, ulong addr)
202 struct cbfs_header *header = &priv->header;
203 struct cbfs_header *header_in_rom;
205 memset(priv, '\0', sizeof(*priv));
206 header_in_rom = (struct cbfs_header *)addr;
207 swap_header(header, header_in_rom);
209 if (header->magic != good_magic || header->offset >
210 header->rom_size - header->boot_block_size) {
211 priv->result = CBFS_BAD_HEADER;
219 * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end
221 * @priv: Private data, which is inited by this function
222 * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff)
223 * @return 0 if OK, -ENXIO if the header is bad
225 static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
227 int offset = *(u32 *)(end_of_rom - 3);
230 ret = load_header(priv, end_of_rom + offset + 1);
233 priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size);
239 * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base
241 * @priv: Private data, which is inited by this function
242 * @base: Address of the first byte of the ROM (e.g. 0xff000000)
243 * @return 0 if OK, -ENXIO if the header is bad
245 static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
249 ret = load_header(priv, base + MASTER_HDR_OFFSET);
252 priv->start = (void *)base;
257 static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
261 ret = file_cbfs_load_header(priv, end_of_rom);
265 ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
269 priv->initialized = true;
274 int file_cbfs_init(ulong end_of_rom)
276 return cbfs_init(&cbfs_s, end_of_rom);
279 int cbfs_init_mem(ulong base, struct cbfs_priv **privp)
281 struct cbfs_priv priv_s, *priv = &priv_s;
285 * Use a local variable to start with until we know that the CBFS is
288 ret = cbfs_load_header_ptr(priv, base);
292 ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
295 return log_msg_ret("fill", ret);
297 priv->initialized = true;
298 priv = malloc(sizeof(priv_s));
301 memcpy(priv, &priv_s, sizeof(priv_s));
307 const struct cbfs_header *file_cbfs_get_header(void)
309 struct cbfs_priv *priv = &cbfs_s;
311 if (priv->initialized) {
312 priv->result = CBFS_SUCCESS;
313 return &priv->header;
315 priv->result = CBFS_NOT_INITIALIZED;
320 const struct cbfs_cachenode *file_cbfs_get_first(void)
322 struct cbfs_priv *priv = &cbfs_s;
324 if (!priv->initialized) {
325 priv->result = CBFS_NOT_INITIALIZED;
328 priv->result = CBFS_SUCCESS;
329 return priv->file_cache;
333 void file_cbfs_get_next(const struct cbfs_cachenode **file)
335 struct cbfs_priv *priv = &cbfs_s;
337 if (!priv->initialized) {
338 priv->result = CBFS_NOT_INITIALIZED;
344 *file = (*file)->next;
345 priv->result = CBFS_SUCCESS;
348 const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
351 struct cbfs_cachenode *cache_node = priv->file_cache;
353 if (!priv->initialized) {
354 priv->result = CBFS_NOT_INITIALIZED;
359 if (!strcmp(name, cache_node->name))
361 cache_node = cache_node->next;
364 priv->result = CBFS_FILE_NOT_FOUND;
366 priv->result = CBFS_SUCCESS;
371 const struct cbfs_cachenode *file_cbfs_find(const char *name)
373 return cbfs_find_file(&cbfs_s, name);
376 static int find_uncached(struct cbfs_priv *priv, const char *name, void *start,
377 struct cbfs_cachenode *node)
379 int size = priv->header.rom_size;
380 int align = priv->header.align;
382 while (size >= align) {
386 ret = file_cbfs_next_file(priv, start, size, align, node,
392 if (!strcmp(name, node->name))
398 priv->result = CBFS_FILE_NOT_FOUND;
403 int file_cbfs_find_uncached(ulong end_of_rom, const char *name,
404 struct cbfs_cachenode *node)
406 struct cbfs_priv priv;
410 ret = file_cbfs_load_header(&priv, end_of_rom);
415 return find_uncached(&priv, name, start, node);
418 int file_cbfs_find_uncached_base(ulong base, const char *name,
419 struct cbfs_cachenode *node)
421 struct cbfs_priv priv;
424 ret = cbfs_load_header_ptr(&priv, base);
428 return find_uncached(&priv, name, (void *)base, node);
431 const char *file_cbfs_name(const struct cbfs_cachenode *file)
433 cbfs_s.result = CBFS_SUCCESS;
438 u32 file_cbfs_size(const struct cbfs_cachenode *file)
440 cbfs_s.result = CBFS_SUCCESS;
442 return file->data_length;
445 u32 file_cbfs_type(const struct cbfs_cachenode *file)
447 cbfs_s.result = CBFS_SUCCESS;
452 long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
453 unsigned long maxsize)
457 size = file->data_length;
458 if (maxsize && size > maxsize)
461 memcpy(buffer, file->data, size);
462 cbfs_s.result = CBFS_SUCCESS;