1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
9 #include <asm/byteorder.h>
11 enum cbfs_result file_cbfs_result;
12 static const u32 good_magic = 0x4f524243;
13 static const u8 good_file_magic[] = "LARCHIVE";
14 static int initialized;
15 static struct cbfs_header cbfs_header;
16 static struct cbfs_cachenode *file_cache;
18 const char *file_cbfs_error(void)
20 switch (file_cbfs_result) {
23 case CBFS_NOT_INITIALIZED:
24 return "CBFS not initialized";
26 return "Bad CBFS header";
28 return "Bad CBFS file";
29 case CBFS_FILE_NOT_FOUND:
30 return "File not found";
36 /* Do endian conversion on the CBFS header structure. */
37 static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
39 dest->magic = be32_to_cpu(src->magic);
40 dest->version = be32_to_cpu(src->version);
41 dest->rom_size = be32_to_cpu(src->rom_size);
42 dest->boot_block_size = be32_to_cpu(src->boot_block_size);
43 dest->align = be32_to_cpu(src->align);
44 dest->offset = be32_to_cpu(src->offset);
47 /* Do endian conversion on a CBFS file header. */
48 static void swap_file_header(struct cbfs_fileheader *dest,
49 const struct cbfs_fileheader *src)
51 memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
52 dest->len = be32_to_cpu(src->len);
53 dest->type = be32_to_cpu(src->type);
54 dest->attributes_offset = be32_to_cpu(src->attributes_offset);
55 dest->offset = be32_to_cpu(src->offset);
59 * Given a starting position in memory, scan forward, bounded by a size, and
60 * find the next valid CBFS file. No memory is allocated by this function. The
61 * caller is responsible for allocating space for the new file structure.
63 * @param start The location in memory to start from.
64 * @param size The size of the memory region to search.
65 * @param align The alignment boundaries to check on.
66 * @param newNode A pointer to the file structure to load.
67 * @param used A pointer to the count of of bytes scanned through,
68 * including the file if one is found.
70 * @return 1 if a file is found, 0 if one isn't.
72 static int file_cbfs_next_file(u8 *start, u32 size, u32 align,
73 struct cbfs_cachenode *newNode, u32 *used)
75 struct cbfs_fileheader header;
79 while (size >= align) {
80 const struct cbfs_fileheader *fileHeader =
81 (const struct cbfs_fileheader *)start;
85 /* Check if there's a file here. */
86 if (memcmp(good_file_magic, &(fileHeader->magic),
87 sizeof(fileHeader->magic))) {
94 swap_file_header(&header, fileHeader);
95 if (header.offset < sizeof(struct cbfs_fileheader)) {
96 file_cbfs_result = CBFS_BAD_FILE;
100 newNode->type = header.type;
101 newNode->data = start + header.offset;
102 newNode->data_length = header.len;
103 name_len = header.offset - sizeof(struct cbfs_fileheader);
104 newNode->name = (char *)fileHeader +
105 sizeof(struct cbfs_fileheader);
106 newNode->name_length = name_len;
107 newNode->attributes_offset = header.attributes_offset;
111 step = step + align - step % align;
119 /* Look through a CBFS instance and copy file metadata into regular memory. */
120 static void file_cbfs_fill_cache(u8 *start, u32 size, u32 align)
122 struct cbfs_cachenode *cache_node;
123 struct cbfs_cachenode *newNode;
124 struct cbfs_cachenode **cache_tail = &file_cache;
126 /* Clear out old information. */
127 cache_node = file_cache;
129 struct cbfs_cachenode *oldNode = cache_node;
130 cache_node = cache_node->next;
135 while (size >= align) {
139 newNode = (struct cbfs_cachenode *)
140 malloc(sizeof(struct cbfs_cachenode));
141 result = file_cbfs_next_file(start, size, align,
147 } else if (result == 0) {
151 *cache_tail = newNode;
152 cache_tail = &newNode->next;
157 file_cbfs_result = CBFS_SUCCESS;
160 /* Get the CBFS header out of the ROM and do endian conversion. */
161 static int file_cbfs_load_header(uintptr_t end_of_rom,
162 struct cbfs_header *header)
164 struct cbfs_header *header_in_rom;
165 int32_t offset = *(u32 *)(end_of_rom - 3);
167 header_in_rom = (struct cbfs_header *)(end_of_rom + offset + 1);
168 swap_header(header, header_in_rom);
170 if (header->magic != good_magic || header->offset >
171 header->rom_size - header->boot_block_size) {
172 file_cbfs_result = CBFS_BAD_HEADER;
178 void file_cbfs_init(uintptr_t end_of_rom)
183 if (file_cbfs_load_header(end_of_rom, &cbfs_header))
186 start_of_rom = (u8 *)(end_of_rom + 1 - cbfs_header.rom_size);
188 file_cbfs_fill_cache(start_of_rom, cbfs_header.rom_size,
190 if (file_cbfs_result == CBFS_SUCCESS)
194 const struct cbfs_header *file_cbfs_get_header(void)
197 file_cbfs_result = CBFS_SUCCESS;
200 file_cbfs_result = CBFS_NOT_INITIALIZED;
205 const struct cbfs_cachenode *file_cbfs_get_first(void)
208 file_cbfs_result = CBFS_NOT_INITIALIZED;
211 file_cbfs_result = CBFS_SUCCESS;
216 void file_cbfs_get_next(const struct cbfs_cachenode **file)
219 file_cbfs_result = CBFS_NOT_INITIALIZED;
225 *file = (*file)->next;
226 file_cbfs_result = CBFS_SUCCESS;
229 const struct cbfs_cachenode *file_cbfs_find(const char *name)
231 struct cbfs_cachenode *cache_node = file_cache;
234 file_cbfs_result = CBFS_NOT_INITIALIZED;
239 if (!strcmp(name, cache_node->name))
241 cache_node = cache_node->next;
244 file_cbfs_result = CBFS_FILE_NOT_FOUND;
246 file_cbfs_result = CBFS_SUCCESS;
251 const struct cbfs_cachenode *file_cbfs_find_uncached(uintptr_t end_of_rom,
257 static struct cbfs_cachenode node;
259 if (file_cbfs_load_header(end_of_rom, &cbfs_header))
262 start = (u8 *)(end_of_rom + 1 - cbfs_header.rom_size);
263 size = cbfs_header.rom_size;
264 align = cbfs_header.align;
266 while (size >= align) {
270 result = file_cbfs_next_file(start, size, align, &node, &used);
274 else if (result == 0)
277 if (!strcmp(name, node.name))
283 file_cbfs_result = CBFS_FILE_NOT_FOUND;
287 const char *file_cbfs_name(const struct cbfs_cachenode *file)
289 file_cbfs_result = CBFS_SUCCESS;
293 u32 file_cbfs_size(const struct cbfs_cachenode *file)
295 file_cbfs_result = CBFS_SUCCESS;
296 return file->data_length;
299 u32 file_cbfs_type(const struct cbfs_cachenode *file)
301 file_cbfs_result = CBFS_SUCCESS;
305 long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
306 unsigned long maxsize)
310 size = file->data_length;
311 if (maxsize && size > maxsize)
314 memcpy(buffer, file->data, size);
316 file_cbfs_result = CBFS_SUCCESS;