1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
9 #include <asm/byteorder.h>
11 static const u32 good_magic = 0x4f524243;
12 static const u8 good_file_magic[] = "LARCHIVE";
16 struct cbfs_header header;
17 struct cbfs_cachenode *file_cache;
18 enum cbfs_result result;
21 static struct cbfs_priv cbfs_s;
23 const char *file_cbfs_error(void)
25 switch (cbfs_s.result) {
28 case CBFS_NOT_INITIALIZED:
29 return "CBFS not initialized";
31 return "Bad CBFS header";
33 return "Bad CBFS file";
34 case CBFS_FILE_NOT_FOUND:
35 return "File not found";
41 enum cbfs_result cbfs_get_result(void)
46 /* Do endian conversion on the CBFS header structure. */
47 static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
49 dest->magic = be32_to_cpu(src->magic);
50 dest->version = be32_to_cpu(src->version);
51 dest->rom_size = be32_to_cpu(src->rom_size);
52 dest->boot_block_size = be32_to_cpu(src->boot_block_size);
53 dest->align = be32_to_cpu(src->align);
54 dest->offset = be32_to_cpu(src->offset);
57 /* Do endian conversion on a CBFS file header. */
58 static void swap_file_header(struct cbfs_fileheader *dest,
59 const struct cbfs_fileheader *src)
61 memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
62 dest->len = be32_to_cpu(src->len);
63 dest->type = be32_to_cpu(src->type);
64 dest->attributes_offset = be32_to_cpu(src->attributes_offset);
65 dest->offset = be32_to_cpu(src->offset);
69 * Given a starting position in memory, scan forward, bounded by a size, and
70 * find the next valid CBFS file. No memory is allocated by this function. The
71 * caller is responsible for allocating space for the new file structure.
73 * @param start The location in memory to start from.
74 * @param size The size of the memory region to search.
75 * @param align The alignment boundaries to check on.
76 * @param new_node A pointer to the file structure to load.
77 * @param used A pointer to the count of of bytes scanned through,
78 * including the file if one is found.
80 * @return 1 if a file is found, 0 if one isn't.
82 static int file_cbfs_next_file(struct cbfs_priv *priv, u8 *start, u32 size,
83 u32 align, struct cbfs_cachenode *new_node,
86 struct cbfs_fileheader header;
90 while (size >= align) {
91 const struct cbfs_fileheader *file_header =
92 (const struct cbfs_fileheader *)start;
96 /* Check if there's a file here. */
97 if (memcmp(good_file_magic, &file_header->magic,
98 sizeof(file_header->magic))) {
105 swap_file_header(&header, file_header);
106 if (header.offset < sizeof(struct cbfs_fileheader)) {
107 priv->result = CBFS_BAD_FILE;
110 new_node->next = NULL;
111 new_node->type = header.type;
112 new_node->data = start + header.offset;
113 new_node->data_length = header.len;
114 name_len = header.offset - sizeof(struct cbfs_fileheader);
115 new_node->name = (char *)file_header +
116 sizeof(struct cbfs_fileheader);
117 new_node->name_length = name_len;
118 new_node->attributes_offset = header.attributes_offset;
122 step = step + align - step % align;
130 /* Look through a CBFS instance and copy file metadata into regular memory. */
131 static void file_cbfs_fill_cache(struct cbfs_priv *priv, u8 *start, u32 size,
134 struct cbfs_cachenode *cache_node;
135 struct cbfs_cachenode *new_node;
136 struct cbfs_cachenode **cache_tail = &priv->file_cache;
138 /* Clear out old information. */
139 cache_node = priv->file_cache;
141 struct cbfs_cachenode *old_node = cache_node;
142 cache_node = cache_node->next;
145 priv->file_cache = NULL;
147 while (size >= align) {
151 new_node = (struct cbfs_cachenode *)
152 malloc(sizeof(struct cbfs_cachenode));
153 ret = file_cbfs_next_file(priv, start, size, align, new_node,
159 } else if (ret == 0) {
163 *cache_tail = new_node;
164 cache_tail = &new_node->next;
169 priv->result = CBFS_SUCCESS;
172 /* Get the CBFS header out of the ROM and do endian conversion. */
173 static int file_cbfs_load_header(ulong end_of_rom, struct cbfs_header *header)
175 struct cbfs_header *header_in_rom;
176 int32_t offset = *(u32 *)(end_of_rom - 3);
178 header_in_rom = (struct cbfs_header *)(end_of_rom + offset + 1);
179 swap_header(header, header_in_rom);
181 if (header->magic != good_magic || header->offset >
182 header->rom_size - header->boot_block_size) {
183 cbfs_s.result = CBFS_BAD_HEADER;
189 static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base,
190 struct cbfs_header *header)
192 struct cbfs_header *header_in_rom;
194 header_in_rom = (struct cbfs_header *)base;
195 swap_header(header, header_in_rom);
197 if (header->magic != good_magic || header->offset >
198 header->rom_size - header->boot_block_size) {
199 priv->result = CBFS_BAD_HEADER;
206 static void cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
210 priv->initialized = false;
212 if (file_cbfs_load_header(end_of_rom, &priv->header))
215 start_of_rom = (u8 *)(end_of_rom + 1 - priv->header.rom_size);
217 file_cbfs_fill_cache(priv, start_of_rom, priv->header.rom_size,
219 if (priv->result == CBFS_SUCCESS)
220 priv->initialized = true;
223 void file_cbfs_init(ulong end_of_rom)
225 cbfs_init(&cbfs_s, end_of_rom);
228 int cbfs_init_mem(ulong base, ulong size, struct cbfs_priv **privp)
230 struct cbfs_priv priv_s, *priv = &priv_s;
234 * Use a local variable to start with until we know that the CBFS is
235 * valid. Assume that a master header appears at the start, at offset
238 ret = cbfs_load_header_ptr(priv, base + 0x38, &priv->header);
242 file_cbfs_fill_cache(priv, (u8 *)base, priv->header.rom_size,
244 if (priv->result != CBFS_SUCCESS)
247 priv->initialized = true;
248 priv = malloc(sizeof(priv_s));
251 memcpy(priv, &priv_s, sizeof(priv_s));
257 const struct cbfs_header *file_cbfs_get_header(void)
259 struct cbfs_priv *priv = &cbfs_s;
261 if (priv->initialized) {
262 priv->result = CBFS_SUCCESS;
263 return &priv->header;
265 priv->result = CBFS_NOT_INITIALIZED;
270 const struct cbfs_cachenode *file_cbfs_get_first(void)
272 struct cbfs_priv *priv = &cbfs_s;
274 if (!priv->initialized) {
275 priv->result = CBFS_NOT_INITIALIZED;
278 priv->result = CBFS_SUCCESS;
279 return priv->file_cache;
283 void file_cbfs_get_next(const struct cbfs_cachenode **file)
285 struct cbfs_priv *priv = &cbfs_s;
287 if (!priv->initialized) {
288 priv->result = CBFS_NOT_INITIALIZED;
294 *file = (*file)->next;
295 priv->result = CBFS_SUCCESS;
298 const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
301 struct cbfs_cachenode *cache_node = priv->file_cache;
303 if (!priv->initialized) {
304 priv->result = CBFS_NOT_INITIALIZED;
309 if (!strcmp(name, cache_node->name))
311 cache_node = cache_node->next;
314 priv->result = CBFS_FILE_NOT_FOUND;
316 priv->result = CBFS_SUCCESS;
321 const struct cbfs_cachenode *file_cbfs_find(const char *name)
323 return cbfs_find_file(&cbfs_s, name);
326 const struct cbfs_cachenode *file_cbfs_find_uncached(ulong end_of_rom,
329 struct cbfs_priv *priv = &cbfs_s;
333 static struct cbfs_cachenode node;
335 if (file_cbfs_load_header(end_of_rom, &priv->header))
338 start = (u8 *)(end_of_rom + 1 - priv->header.rom_size);
339 size = priv->header.rom_size;
340 align = priv->header.align;
342 while (size >= align) {
346 ret = file_cbfs_next_file(priv, start, size, align, &node,
354 if (!strcmp(name, node.name))
360 cbfs_s.result = CBFS_FILE_NOT_FOUND;
364 const char *file_cbfs_name(const struct cbfs_cachenode *file)
366 cbfs_s.result = CBFS_SUCCESS;
371 u32 file_cbfs_size(const struct cbfs_cachenode *file)
373 cbfs_s.result = CBFS_SUCCESS;
375 return file->data_length;
378 u32 file_cbfs_type(const struct cbfs_cachenode *file)
380 cbfs_s.result = CBFS_SUCCESS;
385 long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
386 unsigned long maxsize)
390 size = file->data_length;
391 if (maxsize && size > maxsize)
394 memcpy(buffer, file->data, size);
395 cbfs_s.result = CBFS_SUCCESS;