2 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 #include <asm/byteorder.h>
24 enum cbfs_result file_cbfs_result;
26 const char *file_cbfs_error(void)
28 switch (file_cbfs_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";
45 static const u32 good_magic = 0x4f524243;
46 static const u8 good_file_magic[] = "LARCHIVE";
49 static int initialized;
50 static struct cbfs_header cbfs_header;
51 static struct cbfs_cachenode *file_cache;
53 /* Do endian conversion on the CBFS header structure. */
54 static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
56 dest->magic = be32_to_cpu(src->magic);
57 dest->version = be32_to_cpu(src->version);
58 dest->rom_size = be32_to_cpu(src->rom_size);
59 dest->boot_block_size = be32_to_cpu(src->boot_block_size);
60 dest->align = be32_to_cpu(src->align);
61 dest->offset = be32_to_cpu(src->offset);
64 /* Do endian conversion on a CBFS file header. */
65 static void swap_file_header(struct cbfs_fileheader *dest,
66 const struct cbfs_fileheader *src)
68 memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
69 dest->len = be32_to_cpu(src->len);
70 dest->type = be32_to_cpu(src->type);
71 dest->checksum = be32_to_cpu(src->checksum);
72 dest->offset = be32_to_cpu(src->offset);
76 * Given a starting position in memory, scan forward, bounded by a size, and
77 * find the next valid CBFS file. No memory is allocated by this function. The
78 * caller is responsible for allocating space for the new file structure.
80 * @param start The location in memory to start from.
81 * @param size The size of the memory region to search.
82 * @param align The alignment boundaries to check on.
83 * @param newNode A pointer to the file structure to load.
84 * @param used A pointer to the count of of bytes scanned through,
85 * including the file if one is found.
87 * @return 1 if a file is found, 0 if one isn't.
89 static int file_cbfs_next_file(u8 *start, u32 size, u32 align,
90 struct cbfs_cachenode *newNode, u32 *used)
92 struct cbfs_fileheader header;
96 while (size >= align) {
97 const struct cbfs_fileheader *fileHeader =
98 (const struct cbfs_fileheader *)start;
102 /* Check if there's a file here. */
103 if (memcmp(good_file_magic, &(fileHeader->magic),
104 sizeof(fileHeader->magic))) {
111 swap_file_header(&header, fileHeader);
112 if (header.offset < sizeof(const struct cbfs_cachenode *) ||
113 header.offset > header.len) {
114 file_cbfs_result = CBFS_BAD_FILE;
117 newNode->next = NULL;
118 newNode->type = header.type;
119 newNode->data = start + header.offset;
120 newNode->data_length = header.len;
121 name_len = header.offset - sizeof(struct cbfs_cachenode *);
122 newNode->name = (char *)fileHeader +
123 sizeof(struct cbfs_cachenode *);
124 newNode->name_length = name_len;
125 newNode->checksum = header.checksum;
129 step = step + align - step % align;
137 /* Look through a CBFS instance and copy file metadata into regular memory. */
138 static void file_cbfs_fill_cache(u8 *start, u32 size, u32 align)
140 struct cbfs_cachenode *cache_node;
141 struct cbfs_cachenode *newNode;
142 struct cbfs_cachenode **cache_tail = &file_cache;
144 /* Clear out old information. */
145 cache_node = file_cache;
147 struct cbfs_cachenode *oldNode = cache_node;
148 cache_node = cache_node->next;
153 while (size >= align) {
157 newNode = (struct cbfs_cachenode *)
158 malloc(sizeof(struct cbfs_cachenode));
159 result = file_cbfs_next_file(start, size, align,
165 } else if (result == 0) {
169 *cache_tail = newNode;
170 cache_tail = &newNode->next;
175 file_cbfs_result = CBFS_SUCCESS;
178 /* Get the CBFS header out of the ROM and do endian conversion. */
179 static int file_cbfs_load_header(uintptr_t end_of_rom,
180 struct cbfs_header *header)
182 struct cbfs_header *header_in_rom;
184 header_in_rom = (struct cbfs_header *)(uintptr_t)
185 *(u32 *)(end_of_rom - 3);
186 swap_header(header, header_in_rom);
188 if (header->magic != good_magic || header->offset >
189 header->rom_size - header->boot_block_size) {
190 file_cbfs_result = CBFS_BAD_HEADER;
196 void file_cbfs_init(uintptr_t end_of_rom)
201 if (file_cbfs_load_header(end_of_rom, &cbfs_header))
204 start_of_rom = (u8 *)(end_of_rom + 1 - cbfs_header.rom_size);
206 file_cbfs_fill_cache(start_of_rom + cbfs_header.offset,
207 cbfs_header.rom_size, cbfs_header.align);
208 if (file_cbfs_result == CBFS_SUCCESS)
212 const struct cbfs_header *file_cbfs_get_header(void)
215 file_cbfs_result = CBFS_SUCCESS;
218 file_cbfs_result = CBFS_NOT_INITIALIZED;
223 const struct cbfs_cachenode *file_cbfs_get_first(void)
226 file_cbfs_result = CBFS_NOT_INITIALIZED;
229 file_cbfs_result = CBFS_SUCCESS;
234 void file_cbfs_get_next(const struct cbfs_cachenode **file)
237 file_cbfs_result = CBFS_NOT_INITIALIZED;
243 *file = (*file)->next;
244 file_cbfs_result = CBFS_SUCCESS;
247 const struct cbfs_cachenode *file_cbfs_find(const char *name)
249 struct cbfs_cachenode *cache_node = file_cache;
252 file_cbfs_result = CBFS_NOT_INITIALIZED;
257 if (!strcmp(name, cache_node->name))
259 cache_node = cache_node->next;
262 file_cbfs_result = CBFS_FILE_NOT_FOUND;
264 file_cbfs_result = CBFS_SUCCESS;
269 const struct cbfs_cachenode *file_cbfs_find_uncached(uintptr_t end_of_rom,
275 static struct cbfs_cachenode node;
277 if (file_cbfs_load_header(end_of_rom, &cbfs_header))
280 start = (u8 *)(end_of_rom + 1 - cbfs_header.rom_size);
281 size = cbfs_header.rom_size;
282 align = cbfs_header.align;
284 while (size >= align) {
288 result = file_cbfs_next_file(start, size, align, &node, &used);
292 else if (result == 0)
295 if (!strcmp(name, node.name))
301 file_cbfs_result = CBFS_FILE_NOT_FOUND;
305 const char *file_cbfs_name(const struct cbfs_cachenode *file)
307 file_cbfs_result = CBFS_SUCCESS;
311 u32 file_cbfs_size(const struct cbfs_cachenode *file)
313 file_cbfs_result = CBFS_SUCCESS;
314 return file->data_length;
317 u32 file_cbfs_type(const struct cbfs_cachenode *file)
319 file_cbfs_result = CBFS_SUCCESS;
323 long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
324 unsigned long maxsize)
328 size = file->data_length;
329 if (maxsize && size > maxsize)
332 memcpy(buffer, file->data, size);
334 file_cbfs_result = CBFS_SUCCESS;