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 if (header.offset >= size)
171 return log_msg_ret("range", -E2BIG);
172 ret = fill_node(node, start, &header);
174 priv->result = CBFS_BAD_FILE;
175 return log_msg_ret("fill", ret);
178 *used += ALIGN(header.len, align);
185 /* Look through a CBFS instance and copy file metadata into regular memory. */
186 static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align)
188 struct cbfs_cachenode *cache_node;
189 struct cbfs_cachenode *node;
190 struct cbfs_cachenode **cache_tail = &priv->file_cache;
193 /* Clear out old information. */
194 cache_node = priv->file_cache;
196 struct cbfs_cachenode *old_node = cache_node;
197 cache_node = cache_node->next;
200 priv->file_cache = NULL;
203 while (size >= align) {
207 node = malloc(sizeof(struct cbfs_cachenode));
210 ret = file_cbfs_next_file(priv, start, size, align, node,
220 cache_tail = &node->next;
225 priv->result = CBFS_SUCCESS;
231 * load_header() - Load the CBFS header
233 * Get the CBFS header out of the ROM and do endian conversion.
235 * @priv: Private data, which is inited by this function
236 * @addr: Address of CBFS header in memory-mapped SPI flash
237 * Return: 0 if OK, -ENXIO if the header is bad
239 static int load_header(struct cbfs_priv *priv, ulong addr)
241 struct cbfs_header *header = &priv->header;
242 struct cbfs_header *header_in_rom;
244 memset(priv, '\0', sizeof(*priv));
245 header_in_rom = (struct cbfs_header *)addr;
246 swap_header(header, header_in_rom);
248 if (header->magic != good_magic || header->offset >
249 header->rom_size - header->boot_block_size) {
250 priv->result = CBFS_BAD_HEADER;
258 * file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end
260 * @priv: Private data, which is inited by this function
261 * @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff)
262 * Return: 0 if OK, -ENXIO if the header is bad
264 static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
266 int offset = *(u32 *)(end_of_rom - 3);
269 ret = load_header(priv, end_of_rom + offset + 1);
272 priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size);
278 * cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base
280 * @priv: Private data, which is inited by this function
281 * @base: Address of the first byte of the ROM (e.g. 0xff000000)
282 * Return: 0 if OK, -ENXIO if the header is bad
284 static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
288 ret = load_header(priv, base + MASTER_HDR_OFFSET);
291 priv->start = (void *)base;
296 static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
300 ret = file_cbfs_load_header(priv, end_of_rom);
304 ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
308 priv->initialized = true;
313 int file_cbfs_init(ulong end_of_rom)
315 return cbfs_init(&cbfs_s, end_of_rom);
318 int cbfs_init_mem(ulong base, ulong size, bool require_hdr,
319 struct cbfs_priv **privp)
321 struct cbfs_priv priv_s, *priv = &priv_s;
325 * 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.
329 ret = cbfs_load_header_ptr(priv, base);
331 if (require_hdr || size == CBFS_SIZE_UNKNOWN)
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;
339 ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
342 return log_msg_ret("fill", ret);
344 priv->initialized = true;
345 priv = malloc(sizeof(priv_s));
348 memcpy(priv, &priv_s, sizeof(priv_s));
354 const struct cbfs_header *file_cbfs_get_header(void)
356 struct cbfs_priv *priv = &cbfs_s;
358 if (priv->initialized) {
359 priv->result = CBFS_SUCCESS;
360 return &priv->header;
362 priv->result = CBFS_NOT_INITIALIZED;
367 const struct cbfs_cachenode *cbfs_get_first(const struct cbfs_priv *priv)
369 return priv->file_cache;
372 void cbfs_get_next(const struct cbfs_cachenode **filep)
375 *filep = (*filep)->next;
378 const struct cbfs_cachenode *file_cbfs_get_first(void)
380 struct cbfs_priv *priv = &cbfs_s;
382 if (!priv->initialized) {
383 priv->result = CBFS_NOT_INITIALIZED;
386 priv->result = CBFS_SUCCESS;
387 return priv->file_cache;
391 void file_cbfs_get_next(const struct cbfs_cachenode **file)
393 struct cbfs_priv *priv = &cbfs_s;
395 if (!priv->initialized) {
396 priv->result = CBFS_NOT_INITIALIZED;
402 *file = (*file)->next;
403 priv->result = CBFS_SUCCESS;
406 const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
409 struct cbfs_cachenode *cache_node = priv->file_cache;
411 if (!priv->initialized) {
412 priv->result = CBFS_NOT_INITIALIZED;
417 if (!strcmp(name, cache_node->name))
419 cache_node = cache_node->next;
422 priv->result = CBFS_FILE_NOT_FOUND;
424 priv->result = CBFS_SUCCESS;
429 const struct cbfs_cachenode *file_cbfs_find(const char *name)
431 return cbfs_find_file(&cbfs_s, name);
434 static int find_uncached(struct cbfs_priv *priv, const char *name, void *start,
435 struct cbfs_cachenode *node)
437 int size = priv->header.rom_size;
438 int align = priv->header.align;
440 while (size >= align) {
444 ret = file_cbfs_next_file(priv, start, size, align, node,
450 if (!strcmp(name, node->name))
456 priv->result = CBFS_FILE_NOT_FOUND;
461 int file_cbfs_find_uncached(ulong end_of_rom, const char *name,
462 struct cbfs_cachenode *node)
464 struct cbfs_priv priv;
468 ret = file_cbfs_load_header(&priv, end_of_rom);
473 return find_uncached(&priv, name, start, node);
476 int file_cbfs_find_uncached_base(ulong base, const char *name,
477 struct cbfs_cachenode *node)
479 struct cbfs_priv priv;
482 ret = cbfs_load_header_ptr(&priv, base);
486 return find_uncached(&priv, name, (void *)base, node);
489 const char *file_cbfs_name(const struct cbfs_cachenode *file)
491 cbfs_s.result = CBFS_SUCCESS;
496 u32 file_cbfs_size(const struct cbfs_cachenode *file)
498 cbfs_s.result = CBFS_SUCCESS;
500 return file->data_length;
503 u32 file_cbfs_type(const struct cbfs_cachenode *file)
505 cbfs_s.result = CBFS_SUCCESS;
510 long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
511 unsigned long maxsize)
515 size = file->data_length;
516 if (maxsize && size > maxsize)
519 memcpy(buffer, file->data, size);
520 cbfs_s.result = CBFS_SUCCESS;