183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
284cd9327SGabe Black /*
384cd9327SGabe Black * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
484cd9327SGabe Black */
584cd9327SGabe Black
67a77e909SGuillaume GARDET #include <common.h>
784cd9327SGabe Black #include <cbfs.h>
884cd9327SGabe Black #include <malloc.h>
984cd9327SGabe Black #include <asm/byteorder.h>
1084cd9327SGabe Black
1184cd9327SGabe Black enum cbfs_result file_cbfs_result;
1284cd9327SGabe Black
file_cbfs_error(void)1384cd9327SGabe Black const char *file_cbfs_error(void)
1484cd9327SGabe Black {
1584cd9327SGabe Black switch (file_cbfs_result) {
1684cd9327SGabe Black case CBFS_SUCCESS:
1784cd9327SGabe Black return "Success";
1884cd9327SGabe Black case CBFS_NOT_INITIALIZED:
1984cd9327SGabe Black return "CBFS not initialized";
2084cd9327SGabe Black case CBFS_BAD_HEADER:
2184cd9327SGabe Black return "Bad CBFS header";
2284cd9327SGabe Black case CBFS_BAD_FILE:
2384cd9327SGabe Black return "Bad CBFS file";
2484cd9327SGabe Black case CBFS_FILE_NOT_FOUND:
2584cd9327SGabe Black return "File not found";
2684cd9327SGabe Black default:
2784cd9327SGabe Black return "Unknown";
2884cd9327SGabe Black }
2984cd9327SGabe Black }
3084cd9327SGabe Black
3184cd9327SGabe Black
3284cd9327SGabe Black static const u32 good_magic = 0x4f524243;
3384cd9327SGabe Black static const u8 good_file_magic[] = "LARCHIVE";
3484cd9327SGabe Black
3584cd9327SGabe Black
3684cd9327SGabe Black static int initialized;
3784cd9327SGabe Black static struct cbfs_header cbfs_header;
3884cd9327SGabe Black static struct cbfs_cachenode *file_cache;
3984cd9327SGabe Black
4084cd9327SGabe Black /* Do endian conversion on the CBFS header structure. */
swap_header(struct cbfs_header * dest,struct cbfs_header * src)4184cd9327SGabe Black static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
4284cd9327SGabe Black {
4384cd9327SGabe Black dest->magic = be32_to_cpu(src->magic);
4484cd9327SGabe Black dest->version = be32_to_cpu(src->version);
4584cd9327SGabe Black dest->rom_size = be32_to_cpu(src->rom_size);
4684cd9327SGabe Black dest->boot_block_size = be32_to_cpu(src->boot_block_size);
4784cd9327SGabe Black dest->align = be32_to_cpu(src->align);
4884cd9327SGabe Black dest->offset = be32_to_cpu(src->offset);
4984cd9327SGabe Black }
5084cd9327SGabe Black
5184cd9327SGabe Black /* Do endian conversion on a CBFS file header. */
swap_file_header(struct cbfs_fileheader * dest,const struct cbfs_fileheader * src)5284cd9327SGabe Black static void swap_file_header(struct cbfs_fileheader *dest,
5384cd9327SGabe Black const struct cbfs_fileheader *src)
5484cd9327SGabe Black {
5584cd9327SGabe Black memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
5684cd9327SGabe Black dest->len = be32_to_cpu(src->len);
5784cd9327SGabe Black dest->type = be32_to_cpu(src->type);
5884cd9327SGabe Black dest->checksum = be32_to_cpu(src->checksum);
5984cd9327SGabe Black dest->offset = be32_to_cpu(src->offset);
6084cd9327SGabe Black }
6184cd9327SGabe Black
6284cd9327SGabe Black /*
6384cd9327SGabe Black * Given a starting position in memory, scan forward, bounded by a size, and
6484cd9327SGabe Black * find the next valid CBFS file. No memory is allocated by this function. The
6584cd9327SGabe Black * caller is responsible for allocating space for the new file structure.
6684cd9327SGabe Black *
6784cd9327SGabe Black * @param start The location in memory to start from.
6884cd9327SGabe Black * @param size The size of the memory region to search.
6984cd9327SGabe Black * @param align The alignment boundaries to check on.
7084cd9327SGabe Black * @param newNode A pointer to the file structure to load.
7184cd9327SGabe Black * @param used A pointer to the count of of bytes scanned through,
7284cd9327SGabe Black * including the file if one is found.
7384cd9327SGabe Black *
7484cd9327SGabe Black * @return 1 if a file is found, 0 if one isn't.
7584cd9327SGabe Black */
file_cbfs_next_file(u8 * start,u32 size,u32 align,struct cbfs_cachenode * newNode,u32 * used)7684cd9327SGabe Black static int file_cbfs_next_file(u8 *start, u32 size, u32 align,
7784cd9327SGabe Black struct cbfs_cachenode *newNode, u32 *used)
7884cd9327SGabe Black {
7984cd9327SGabe Black struct cbfs_fileheader header;
8084cd9327SGabe Black
8184cd9327SGabe Black *used = 0;
8284cd9327SGabe Black
8384cd9327SGabe Black while (size >= align) {
8484cd9327SGabe Black const struct cbfs_fileheader *fileHeader =
8584cd9327SGabe Black (const struct cbfs_fileheader *)start;
8684cd9327SGabe Black u32 name_len;
8784cd9327SGabe Black u32 step;
8884cd9327SGabe Black
8984cd9327SGabe Black /* Check if there's a file here. */
9084cd9327SGabe Black if (memcmp(good_file_magic, &(fileHeader->magic),
9184cd9327SGabe Black sizeof(fileHeader->magic))) {
9284cd9327SGabe Black *used += align;
9384cd9327SGabe Black size -= align;
9484cd9327SGabe Black start += align;
9584cd9327SGabe Black continue;
9684cd9327SGabe Black }
9784cd9327SGabe Black
9884cd9327SGabe Black swap_file_header(&header, fileHeader);
999914c732SChristian Gmeiner if (header.offset < sizeof(struct cbfs_fileheader)) {
10084cd9327SGabe Black file_cbfs_result = CBFS_BAD_FILE;
10184cd9327SGabe Black return -1;
10284cd9327SGabe Black }
10384cd9327SGabe Black newNode->next = NULL;
10484cd9327SGabe Black newNode->type = header.type;
10584cd9327SGabe Black newNode->data = start + header.offset;
10684cd9327SGabe Black newNode->data_length = header.len;
107cc7ed269SYaroslav K name_len = header.offset - sizeof(struct cbfs_fileheader);
10884cd9327SGabe Black newNode->name = (char *)fileHeader +
109cc7ed269SYaroslav K sizeof(struct cbfs_fileheader);
11084cd9327SGabe Black newNode->name_length = name_len;
11184cd9327SGabe Black newNode->checksum = header.checksum;
11284cd9327SGabe Black
11384cd9327SGabe Black step = header.len;
11484cd9327SGabe Black if (step % align)
11584cd9327SGabe Black step = step + align - step % align;
11684cd9327SGabe Black
11784cd9327SGabe Black *used += step;
11884cd9327SGabe Black return 1;
11984cd9327SGabe Black }
12084cd9327SGabe Black return 0;
12184cd9327SGabe Black }
12284cd9327SGabe Black
12384cd9327SGabe Black /* Look through a CBFS instance and copy file metadata into regular memory. */
file_cbfs_fill_cache(u8 * start,u32 size,u32 align)12484cd9327SGabe Black static void file_cbfs_fill_cache(u8 *start, u32 size, u32 align)
12584cd9327SGabe Black {
12684cd9327SGabe Black struct cbfs_cachenode *cache_node;
12784cd9327SGabe Black struct cbfs_cachenode *newNode;
12884cd9327SGabe Black struct cbfs_cachenode **cache_tail = &file_cache;
12984cd9327SGabe Black
13084cd9327SGabe Black /* Clear out old information. */
13184cd9327SGabe Black cache_node = file_cache;
13284cd9327SGabe Black while (cache_node) {
13384cd9327SGabe Black struct cbfs_cachenode *oldNode = cache_node;
13484cd9327SGabe Black cache_node = cache_node->next;
13584cd9327SGabe Black free(oldNode);
13684cd9327SGabe Black }
13784cd9327SGabe Black file_cache = NULL;
13884cd9327SGabe Black
13984cd9327SGabe Black while (size >= align) {
14084cd9327SGabe Black int result;
14184cd9327SGabe Black u32 used;
14284cd9327SGabe Black
14384cd9327SGabe Black newNode = (struct cbfs_cachenode *)
14484cd9327SGabe Black malloc(sizeof(struct cbfs_cachenode));
14584cd9327SGabe Black result = file_cbfs_next_file(start, size, align,
14684cd9327SGabe Black newNode, &used);
14784cd9327SGabe Black
14884cd9327SGabe Black if (result < 0) {
14984cd9327SGabe Black free(newNode);
15084cd9327SGabe Black return;
15184cd9327SGabe Black } else if (result == 0) {
15284cd9327SGabe Black free(newNode);
15384cd9327SGabe Black break;
15484cd9327SGabe Black }
15584cd9327SGabe Black *cache_tail = newNode;
15684cd9327SGabe Black cache_tail = &newNode->next;
15784cd9327SGabe Black
15884cd9327SGabe Black size -= used;
15984cd9327SGabe Black start += used;
16084cd9327SGabe Black }
16184cd9327SGabe Black file_cbfs_result = CBFS_SUCCESS;
16284cd9327SGabe Black }
16384cd9327SGabe Black
16484cd9327SGabe Black /* Get the CBFS header out of the ROM and do endian conversion. */
file_cbfs_load_header(uintptr_t end_of_rom,struct cbfs_header * header)16584cd9327SGabe Black static int file_cbfs_load_header(uintptr_t end_of_rom,
16684cd9327SGabe Black struct cbfs_header *header)
16784cd9327SGabe Black {
16884cd9327SGabe Black struct cbfs_header *header_in_rom;
16944683170SAndre Heider int32_t offset = *(u32 *)(end_of_rom - 3);
17084cd9327SGabe Black
17144683170SAndre Heider header_in_rom = (struct cbfs_header *)(end_of_rom + offset + 1);
17284cd9327SGabe Black swap_header(header, header_in_rom);
17384cd9327SGabe Black
17484cd9327SGabe Black if (header->magic != good_magic || header->offset >
17584cd9327SGabe Black header->rom_size - header->boot_block_size) {
17684cd9327SGabe Black file_cbfs_result = CBFS_BAD_HEADER;
17784cd9327SGabe Black return 1;
17884cd9327SGabe Black }
17984cd9327SGabe Black return 0;
18084cd9327SGabe Black }
18184cd9327SGabe Black
file_cbfs_init(uintptr_t end_of_rom)18284cd9327SGabe Black void file_cbfs_init(uintptr_t end_of_rom)
18384cd9327SGabe Black {
18484cd9327SGabe Black u8 *start_of_rom;
18584cd9327SGabe Black initialized = 0;
18684cd9327SGabe Black
18784cd9327SGabe Black if (file_cbfs_load_header(end_of_rom, &cbfs_header))
18884cd9327SGabe Black return;
18984cd9327SGabe Black
19084cd9327SGabe Black start_of_rom = (u8 *)(end_of_rom + 1 - cbfs_header.rom_size);
19184cd9327SGabe Black
192*d94bf13cSBin Meng file_cbfs_fill_cache(start_of_rom, cbfs_header.rom_size,
193*d94bf13cSBin Meng cbfs_header.align);
19484cd9327SGabe Black if (file_cbfs_result == CBFS_SUCCESS)
19584cd9327SGabe Black initialized = 1;
19684cd9327SGabe Black }
19784cd9327SGabe Black
file_cbfs_get_header(void)19884cd9327SGabe Black const struct cbfs_header *file_cbfs_get_header(void)
19984cd9327SGabe Black {
20084cd9327SGabe Black if (initialized) {
20184cd9327SGabe Black file_cbfs_result = CBFS_SUCCESS;
20284cd9327SGabe Black return &cbfs_header;
20384cd9327SGabe Black } else {
20484cd9327SGabe Black file_cbfs_result = CBFS_NOT_INITIALIZED;
20584cd9327SGabe Black return NULL;
20684cd9327SGabe Black }
20784cd9327SGabe Black }
20884cd9327SGabe Black
file_cbfs_get_first(void)20984cd9327SGabe Black const struct cbfs_cachenode *file_cbfs_get_first(void)
21084cd9327SGabe Black {
21184cd9327SGabe Black if (!initialized) {
21284cd9327SGabe Black file_cbfs_result = CBFS_NOT_INITIALIZED;
21384cd9327SGabe Black return NULL;
21484cd9327SGabe Black } else {
21584cd9327SGabe Black file_cbfs_result = CBFS_SUCCESS;
21684cd9327SGabe Black return file_cache;
21784cd9327SGabe Black }
21884cd9327SGabe Black }
21984cd9327SGabe Black
file_cbfs_get_next(const struct cbfs_cachenode ** file)22084cd9327SGabe Black void file_cbfs_get_next(const struct cbfs_cachenode **file)
22184cd9327SGabe Black {
22284cd9327SGabe Black if (!initialized) {
22384cd9327SGabe Black file_cbfs_result = CBFS_NOT_INITIALIZED;
22484cd9327SGabe Black file = NULL;
22584cd9327SGabe Black return;
22684cd9327SGabe Black }
22784cd9327SGabe Black
22884cd9327SGabe Black if (*file)
22984cd9327SGabe Black *file = (*file)->next;
23084cd9327SGabe Black file_cbfs_result = CBFS_SUCCESS;
23184cd9327SGabe Black }
23284cd9327SGabe Black
file_cbfs_find(const char * name)23384cd9327SGabe Black const struct cbfs_cachenode *file_cbfs_find(const char *name)
23484cd9327SGabe Black {
23584cd9327SGabe Black struct cbfs_cachenode *cache_node = file_cache;
23684cd9327SGabe Black
23784cd9327SGabe Black if (!initialized) {
23884cd9327SGabe Black file_cbfs_result = CBFS_NOT_INITIALIZED;
23984cd9327SGabe Black return NULL;
24084cd9327SGabe Black }
24184cd9327SGabe Black
24284cd9327SGabe Black while (cache_node) {
24384cd9327SGabe Black if (!strcmp(name, cache_node->name))
24484cd9327SGabe Black break;
24584cd9327SGabe Black cache_node = cache_node->next;
24684cd9327SGabe Black }
24784cd9327SGabe Black if (!cache_node)
24884cd9327SGabe Black file_cbfs_result = CBFS_FILE_NOT_FOUND;
24984cd9327SGabe Black else
25084cd9327SGabe Black file_cbfs_result = CBFS_SUCCESS;
25184cd9327SGabe Black
25284cd9327SGabe Black return cache_node;
25384cd9327SGabe Black }
25484cd9327SGabe Black
file_cbfs_find_uncached(uintptr_t end_of_rom,const char * name)25584cd9327SGabe Black const struct cbfs_cachenode *file_cbfs_find_uncached(uintptr_t end_of_rom,
25684cd9327SGabe Black const char *name)
25784cd9327SGabe Black {
25884cd9327SGabe Black u8 *start;
25984cd9327SGabe Black u32 size;
26084cd9327SGabe Black u32 align;
26184cd9327SGabe Black static struct cbfs_cachenode node;
26284cd9327SGabe Black
26384cd9327SGabe Black if (file_cbfs_load_header(end_of_rom, &cbfs_header))
26484cd9327SGabe Black return NULL;
26584cd9327SGabe Black
26684cd9327SGabe Black start = (u8 *)(end_of_rom + 1 - cbfs_header.rom_size);
26784cd9327SGabe Black size = cbfs_header.rom_size;
26884cd9327SGabe Black align = cbfs_header.align;
26984cd9327SGabe Black
27084cd9327SGabe Black while (size >= align) {
27184cd9327SGabe Black int result;
27284cd9327SGabe Black u32 used;
27384cd9327SGabe Black
27484cd9327SGabe Black result = file_cbfs_next_file(start, size, align, &node, &used);
27584cd9327SGabe Black
27684cd9327SGabe Black if (result < 0)
27784cd9327SGabe Black return NULL;
27884cd9327SGabe Black else if (result == 0)
27984cd9327SGabe Black break;
28084cd9327SGabe Black
28184cd9327SGabe Black if (!strcmp(name, node.name))
28284cd9327SGabe Black return &node;
28384cd9327SGabe Black
28484cd9327SGabe Black size -= used;
28584cd9327SGabe Black start += used;
28684cd9327SGabe Black }
28784cd9327SGabe Black file_cbfs_result = CBFS_FILE_NOT_FOUND;
28884cd9327SGabe Black return NULL;
28984cd9327SGabe Black }
29084cd9327SGabe Black
file_cbfs_name(const struct cbfs_cachenode * file)29184cd9327SGabe Black const char *file_cbfs_name(const struct cbfs_cachenode *file)
29284cd9327SGabe Black {
29384cd9327SGabe Black file_cbfs_result = CBFS_SUCCESS;
29484cd9327SGabe Black return file->name;
29584cd9327SGabe Black }
29684cd9327SGabe Black
file_cbfs_size(const struct cbfs_cachenode * file)29784cd9327SGabe Black u32 file_cbfs_size(const struct cbfs_cachenode *file)
29884cd9327SGabe Black {
29984cd9327SGabe Black file_cbfs_result = CBFS_SUCCESS;
30084cd9327SGabe Black return file->data_length;
30184cd9327SGabe Black }
30284cd9327SGabe Black
file_cbfs_type(const struct cbfs_cachenode * file)30384cd9327SGabe Black u32 file_cbfs_type(const struct cbfs_cachenode *file)
30484cd9327SGabe Black {
30584cd9327SGabe Black file_cbfs_result = CBFS_SUCCESS;
30684cd9327SGabe Black return file->type;
30784cd9327SGabe Black }
30884cd9327SGabe Black
file_cbfs_read(const struct cbfs_cachenode * file,void * buffer,unsigned long maxsize)30984cd9327SGabe Black long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
31084cd9327SGabe Black unsigned long maxsize)
31184cd9327SGabe Black {
31284cd9327SGabe Black u32 size;
31384cd9327SGabe Black
31484cd9327SGabe Black size = file->data_length;
31584cd9327SGabe Black if (maxsize && size > maxsize)
31684cd9327SGabe Black size = maxsize;
31784cd9327SGabe Black
31884cd9327SGabe Black memcpy(buffer, file->data, size);
31984cd9327SGabe Black
32084cd9327SGabe Black file_cbfs_result = CBFS_SUCCESS;
32184cd9327SGabe Black return size;
32284cd9327SGabe Black }
323