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)
96 /* Check the header is large enough */
97 if (header->offset < sizeof(struct cbfs_fileheader))
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;
108 node->comp_algo = CBFS_COMPRESS_NONE;
109 node->decomp_size = 0;
111 for (offset = node->attr_offset; offset < header->offset;) {
112 const struct cbfs_file_attribute *attr;
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;
121 comp = start + offset;
122 node->comp_algo = be32_to_cpu(comp->compression);
124 be32_to_cpu(comp->decompressed_size);
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.
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.
141 * @param node A pointer to the file structure to load.
142 * @param used A pointer to the count of of bytes scanned through,
143 * including the file if one is found.
145 * @return 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header
148 static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size,
149 int align, struct cbfs_cachenode *node,
152 struct cbfs_fileheader header;
156 while (size >= align) {
157 const struct cbfs_fileheader *file_header = start;
160 /* Check if there's a file here. */
161 if (memcmp(good_file_magic, &file_header->magic,
162 sizeof(file_header->magic))) {
169 swap_file_header(&header, file_header);
170 ret = fill_node(node, start, &header);
172 priv->result = CBFS_BAD_FILE;
173 return log_msg_ret("fill", ret);
176 *used += ALIGN(header.len, align);
183 /* Look through a CBFS instance and copy file metadata into regular memory. */
184 static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align)
186 struct cbfs_cachenode *cache_node;
187 struct cbfs_cachenode *node;
188 struct cbfs_cachenode **cache_tail = &priv->file_cache;
191 /* Clear out old information. */
192 cache_node = priv->file_cache;
194 struct cbfs_cachenode *old_node = cache_node;
195 cache_node = cache_node->next;
198 priv->file_cache = NULL;
201 while (size >= align) {
205 node = (struct cbfs_cachenode *)
206 malloc(sizeof(struct cbfs_cachenode));
209 ret = file_cbfs_next_file(priv, start, size, align, node,
219 cache_tail = &node->next;
224 priv->result = CBFS_SUCCESS;
230 * load_header() - Load the CBFS header
232 * Get the CBFS header out of the ROM and do endian conversion.
234 * @priv: Private data, which is inited by this function
235 * @addr: Address of CBFS header in memory-mapped SPI flash
236 * @return 0 if OK, -ENXIO if the header is bad
238 static int load_header(struct cbfs_priv *priv, ulong addr)
240 struct cbfs_header *header = &priv->header;
241 struct cbfs_header *header_in_rom;
243 memset(priv, '\0', sizeof(*priv));
244 header_in_rom = (struct cbfs_header *)addr;
245 swap_header(header, header_in_rom);
247 if (header->magic != good_magic || header->offset >
248 header->rom_size - header->boot_block_size) {
249 priv->result = CBFS_BAD_HEADER;
257 * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end
259 * @priv: Private data, which is inited by this function
260 * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff)
261 * @return 0 if OK, -ENXIO if the header is bad
263 static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
265 int offset = *(u32 *)(end_of_rom - 3);
268 ret = load_header(priv, end_of_rom + offset + 1);
271 priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size);
277 * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base
279 * @priv: Private data, which is inited by this function
280 * @base: Address of the first byte of the ROM (e.g. 0xff000000)
281 * @return 0 if OK, -ENXIO if the header is bad
283 static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
287 ret = load_header(priv, base + MASTER_HDR_OFFSET);
290 priv->start = (void *)base;
295 static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
299 ret = file_cbfs_load_header(priv, end_of_rom);
303 ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
307 priv->initialized = true;
312 int file_cbfs_init(ulong end_of_rom)
314 return cbfs_init(&cbfs_s, end_of_rom);
317 int cbfs_init_mem(ulong base, ulong size, bool require_hdr,
318 struct cbfs_priv **privp)
320 struct cbfs_priv priv_s, *priv = &priv_s;
324 * Use a local variable to start with until we know that the * CBFS is
325 * valid. Note that size is detected from the header, if present,
326 * meaning the parameter is ignored.
328 ret = cbfs_load_header_ptr(priv, base);
330 if (require_hdr || size == CBFS_SIZE_UNKNOWN)
332 memset(priv, '\0', sizeof(struct cbfs_priv));
333 priv->header.rom_size = size;
334 priv->header.align = CBFS_ALIGN_SIZE;
335 priv->start = (void *)base;
338 ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
341 return log_msg_ret("fill", ret);
343 priv->initialized = true;
344 priv = malloc(sizeof(priv_s));
347 memcpy(priv, &priv_s, sizeof(priv_s));
353 const struct cbfs_header *file_cbfs_get_header(void)
355 struct cbfs_priv *priv = &cbfs_s;
357 if (priv->initialized) {
358 priv->result = CBFS_SUCCESS;
359 return &priv->header;
361 priv->result = CBFS_NOT_INITIALIZED;
366 const struct cbfs_cachenode *cbfs_get_first(const struct cbfs_priv *priv)
368 return priv->file_cache;
371 void cbfs_get_next(const struct cbfs_cachenode **filep)
374 *filep = (*filep)->next;
377 const struct cbfs_cachenode *file_cbfs_get_first(void)
379 struct cbfs_priv *priv = &cbfs_s;
381 if (!priv->initialized) {
382 priv->result = CBFS_NOT_INITIALIZED;
385 priv->result = CBFS_SUCCESS;
386 return priv->file_cache;
390 void file_cbfs_get_next(const struct cbfs_cachenode **file)
392 struct cbfs_priv *priv = &cbfs_s;
394 if (!priv->initialized) {
395 priv->result = CBFS_NOT_INITIALIZED;
401 *file = (*file)->next;
402 priv->result = CBFS_SUCCESS;
405 const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
408 struct cbfs_cachenode *cache_node = priv->file_cache;
410 if (!priv->initialized) {
411 priv->result = CBFS_NOT_INITIALIZED;
416 if (!strcmp(name, cache_node->name))
418 cache_node = cache_node->next;
421 priv->result = CBFS_FILE_NOT_FOUND;
423 priv->result = CBFS_SUCCESS;
428 const struct cbfs_cachenode *file_cbfs_find(const char *name)
430 return cbfs_find_file(&cbfs_s, name);
433 static int find_uncached(struct cbfs_priv *priv, const char *name, void *start,
434 struct cbfs_cachenode *node)
436 int size = priv->header.rom_size;
437 int align = priv->header.align;
439 while (size >= align) {
443 ret = file_cbfs_next_file(priv, start, size, align, node,
449 if (!strcmp(name, node->name))
455 priv->result = CBFS_FILE_NOT_FOUND;
460 int file_cbfs_find_uncached(ulong end_of_rom, const char *name,
461 struct cbfs_cachenode *node)
463 struct cbfs_priv priv;
467 ret = file_cbfs_load_header(&priv, end_of_rom);
472 return find_uncached(&priv, name, start, node);
475 int file_cbfs_find_uncached_base(ulong base, const char *name,
476 struct cbfs_cachenode *node)
478 struct cbfs_priv priv;
481 ret = cbfs_load_header_ptr(&priv, base);
485 return find_uncached(&priv, name, (void *)base, node);
488 const char *file_cbfs_name(const struct cbfs_cachenode *file)
490 cbfs_s.result = CBFS_SUCCESS;
495 u32 file_cbfs_size(const struct cbfs_cachenode *file)
497 cbfs_s.result = CBFS_SUCCESS;
499 return file->data_length;
502 u32 file_cbfs_type(const struct cbfs_cachenode *file)
504 cbfs_s.result = CBFS_SUCCESS;
509 long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
510 unsigned long maxsize)
514 size = file->data_length;
515 if (maxsize && size > maxsize)
518 memcpy(buffer, file->data, size);
519 cbfs_s.result = CBFS_SUCCESS;