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