1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
9 #include <asm/byteorder.h>
11 /* Offset of master header from the start of a coreboot ROM */
12 #define MASTER_HDR_OFFSET 0x38
14 static const u32 good_magic = 0x4f524243;
15 static const u8 good_file_magic[] = "LARCHIVE";
19 struct cbfs_header header;
20 struct cbfs_cachenode *file_cache;
21 enum cbfs_result result;
24 static struct cbfs_priv cbfs_s;
26 const char *file_cbfs_error(void)
28 switch (cbfs_s.result) {
31 case CBFS_NOT_INITIALIZED:
32 return "CBFS not initialized";
34 return "Bad CBFS header";
36 return "Bad CBFS file";
37 case CBFS_FILE_NOT_FOUND:
38 return "File not found";
44 enum cbfs_result cbfs_get_result(void)
49 /* Do endian conversion on the CBFS header structure. */
50 static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
52 dest->magic = be32_to_cpu(src->magic);
53 dest->version = be32_to_cpu(src->version);
54 dest->rom_size = be32_to_cpu(src->rom_size);
55 dest->boot_block_size = be32_to_cpu(src->boot_block_size);
56 dest->align = be32_to_cpu(src->align);
57 dest->offset = be32_to_cpu(src->offset);
60 /* Do endian conversion on a CBFS file header. */
61 static void swap_file_header(struct cbfs_fileheader *dest,
62 const struct cbfs_fileheader *src)
64 memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
65 dest->len = be32_to_cpu(src->len);
66 dest->type = be32_to_cpu(src->type);
67 dest->attributes_offset = be32_to_cpu(src->attributes_offset);
68 dest->offset = be32_to_cpu(src->offset);
72 * Given a starting position in memory, scan forward, bounded by a size, and
73 * find the next valid CBFS file. No memory is allocated by this function. The
74 * caller is responsible for allocating space for the new file structure.
76 * @param start The location in memory to start from.
77 * @param size The size of the memory region to search.
78 * @param align The alignment boundaries to check on.
79 * @param new_node A pointer to the file structure to load.
80 * @param used A pointer to the count of of bytes scanned through,
81 * including the file if one is found.
83 * @return 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header
86 static int file_cbfs_next_file(struct cbfs_priv *priv, u8 *start, int size,
87 int align, struct cbfs_cachenode *new_node,
90 struct cbfs_fileheader header;
94 while (size >= align) {
95 const struct cbfs_fileheader *file_header =
96 (const struct cbfs_fileheader *)start;
100 /* Check if there's a file here. */
101 if (memcmp(good_file_magic, &file_header->magic,
102 sizeof(file_header->magic))) {
109 swap_file_header(&header, file_header);
110 if (header.offset < sizeof(struct cbfs_fileheader)) {
111 priv->result = CBFS_BAD_FILE;
114 new_node->next = NULL;
115 new_node->type = header.type;
116 new_node->data = start + header.offset;
117 new_node->data_length = header.len;
118 name_len = header.offset - sizeof(struct cbfs_fileheader);
119 new_node->name = (char *)file_header +
120 sizeof(struct cbfs_fileheader);
121 new_node->name_length = name_len;
122 new_node->attributes_offset = header.attributes_offset;
126 step = step + align - step % align;
135 /* Look through a CBFS instance and copy file metadata into regular memory. */
136 static int file_cbfs_fill_cache(struct cbfs_priv *priv, u8 *start, u32 size,
139 struct cbfs_cachenode *cache_node;
140 struct cbfs_cachenode *new_node;
141 struct cbfs_cachenode **cache_tail = &priv->file_cache;
143 /* Clear out old information. */
144 cache_node = priv->file_cache;
146 struct cbfs_cachenode *old_node = cache_node;
147 cache_node = cache_node->next;
150 priv->file_cache = NULL;
152 while (size >= align) {
156 new_node = (struct cbfs_cachenode *)
157 malloc(sizeof(struct cbfs_cachenode));
160 ret = file_cbfs_next_file(priv, start, size, align, new_node,
169 *cache_tail = new_node;
170 cache_tail = &new_node->next;
175 priv->result = CBFS_SUCCESS;
181 * load_header() - Load the CBFS header
183 * Get the CBFS header out of the ROM and do endian conversion.
185 * @priv: Private data, which is inited by this function
186 * @addr: Address of CBFS header in memory-mapped SPI flash
187 * @return 0 if OK, -ENXIO if the header is bad
189 static int load_header(struct cbfs_priv *priv, ulong addr)
191 struct cbfs_header *header = &priv->header;
192 struct cbfs_header *header_in_rom;
194 memset(priv, '\0', sizeof(*priv));
195 header_in_rom = (struct cbfs_header *)addr;
196 swap_header(header, header_in_rom);
198 if (header->magic != good_magic || header->offset >
199 header->rom_size - header->boot_block_size) {
200 priv->result = CBFS_BAD_HEADER;
208 * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end
210 * @priv: Private data, which is inited by this function
211 * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff)
212 * @return 0 if OK, -ENXIO if the header is bad
214 static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
216 int offset = *(u32 *)(end_of_rom - 3);
218 return load_header(priv, end_of_rom + offset + 1);
222 * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base
224 * @priv: Private data, which is inited by this function
225 * @base: Address of the first byte of the ROM (e.g. 0xff000000)
226 * @return 0 if OK, -ENXIO if the header is bad
228 static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
230 return load_header(priv, base + MASTER_HDR_OFFSET);
233 static void cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
237 if (file_cbfs_load_header(priv, end_of_rom))
240 start_of_rom = (u8 *)(end_of_rom + 1 - priv->header.rom_size);
242 file_cbfs_fill_cache(priv, start_of_rom, priv->header.rom_size,
244 if (priv->result == CBFS_SUCCESS)
245 priv->initialized = true;
248 void file_cbfs_init(ulong end_of_rom)
250 cbfs_init(&cbfs_s, end_of_rom);
253 int cbfs_init_mem(ulong base, ulong size, struct cbfs_priv **privp)
255 struct cbfs_priv priv_s, *priv = &priv_s;
259 * Use a local variable to start with until we know that the CBFS is
262 ret = cbfs_load_header_ptr(priv, base);
266 file_cbfs_fill_cache(priv, (u8 *)base, priv->header.rom_size,
268 if (priv->result != CBFS_SUCCESS)
271 priv->initialized = true;
272 priv = malloc(sizeof(priv_s));
275 memcpy(priv, &priv_s, sizeof(priv_s));
281 const struct cbfs_header *file_cbfs_get_header(void)
283 struct cbfs_priv *priv = &cbfs_s;
285 if (priv->initialized) {
286 priv->result = CBFS_SUCCESS;
287 return &priv->header;
289 priv->result = CBFS_NOT_INITIALIZED;
294 const struct cbfs_cachenode *file_cbfs_get_first(void)
296 struct cbfs_priv *priv = &cbfs_s;
298 if (!priv->initialized) {
299 priv->result = CBFS_NOT_INITIALIZED;
302 priv->result = CBFS_SUCCESS;
303 return priv->file_cache;
307 void file_cbfs_get_next(const struct cbfs_cachenode **file)
309 struct cbfs_priv *priv = &cbfs_s;
311 if (!priv->initialized) {
312 priv->result = CBFS_NOT_INITIALIZED;
318 *file = (*file)->next;
319 priv->result = CBFS_SUCCESS;
322 const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
325 struct cbfs_cachenode *cache_node = priv->file_cache;
327 if (!priv->initialized) {
328 priv->result = CBFS_NOT_INITIALIZED;
333 if (!strcmp(name, cache_node->name))
335 cache_node = cache_node->next;
338 priv->result = CBFS_FILE_NOT_FOUND;
340 priv->result = CBFS_SUCCESS;
345 const struct cbfs_cachenode *file_cbfs_find(const char *name)
347 return cbfs_find_file(&cbfs_s, name);
350 const struct cbfs_cachenode *file_cbfs_find_uncached(ulong end_of_rom,
353 struct cbfs_priv *priv = &cbfs_s;
357 static struct cbfs_cachenode node;
359 if (file_cbfs_load_header(priv, end_of_rom))
362 start = (u8 *)(end_of_rom + 1 - priv->header.rom_size);
363 size = priv->header.rom_size;
364 align = priv->header.align;
366 while (size >= align) {
370 ret = file_cbfs_next_file(priv, start, size, align, &node,
376 if (!strcmp(name, node.name))
382 cbfs_s.result = CBFS_FILE_NOT_FOUND;
386 const char *file_cbfs_name(const struct cbfs_cachenode *file)
388 cbfs_s.result = CBFS_SUCCESS;
393 u32 file_cbfs_size(const struct cbfs_cachenode *file)
395 cbfs_s.result = CBFS_SUCCESS;
397 return file->data_length;
400 u32 file_cbfs_type(const struct cbfs_cachenode *file)
402 cbfs_s.result = CBFS_SUCCESS;
407 long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
408 unsigned long maxsize)
412 size = file->data_length;
413 if (maxsize && size > maxsize)
416 memcpy(buffer, file->data, size);
417 cbfs_s.result = CBFS_SUCCESS;