2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2012-10-12 14:26:11 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
fs: cbfs: Fix build of fs/cbfs/cbfs.c when building u-boot sandbox on x86 32-bit
Fix the following build errors when building sandbox on x86 32-bit:
In file included from fs/cbfs/cbfs.c:8:0:
include/malloc.h:364:7: error: conflicting types for 'memset'
void* memset(void*, int, size_t);
^
In file included from include/compiler.h:123:0,
from include/cbfs.h:10,
from fs/cbfs/cbfs.c:7:
include/linux/string.h:78:15: note: previous declaration of 'memset' was here
extern void * memset(void *,int,__kernel_size_t);
^
In file included from fs/cbfs/cbfs.c:8:0:
include/malloc.h:365:7: error: conflicting types for 'memcpy'
void* memcpy(void*, const void*, size_t);
^
In file included from include/compiler.h:123:0,
from include/cbfs.h:10,
from fs/cbfs/cbfs.c:7:
include/linux/string.h:81:15: note: previous declaration of 'memcpy' was here
extern void * memcpy(void *,const void *,__kernel_size_t);
^
scripts/Makefile.build:280: recipe for target 'fs/cbfs/cbfs.o' failed
Signed-off-by: Guillaume GARDET <guillaume.gardet@free.fr>
Cc: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
2016-06-17 09:45:37 +00:00
|
|
|
#include <common.h>
|
2012-10-12 14:26:11 +00:00
|
|
|
#include <cbfs.h>
|
2020-05-24 23:38:24 +00:00
|
|
|
#include <log.h>
|
2012-10-12 14:26:11 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <asm/byteorder.h>
|
|
|
|
|
2020-05-24 23:38:17 +00:00
|
|
|
/* Offset of master header from the start of a coreboot ROM */
|
|
|
|
#define MASTER_HDR_OFFSET 0x38
|
|
|
|
|
2019-08-15 01:56:11 +00:00
|
|
|
static const u32 good_magic = 0x4f524243;
|
|
|
|
static const u8 good_file_magic[] = "LARCHIVE";
|
2019-08-15 01:56:12 +00:00
|
|
|
|
2020-05-24 23:38:20 +00:00
|
|
|
/**
|
|
|
|
* struct cbfs_priv - Private data for this driver
|
|
|
|
*
|
|
|
|
* @initialised: true if this CBFS has been inited
|
|
|
|
* @start: Start position of CBFS in memory, typically memory-mapped SPI flash
|
|
|
|
* @header: Header read from the CBFS, byte-swapped so U-Boot can access it
|
|
|
|
* @file_cache: List of file headers read from CBFS
|
|
|
|
* @result: Success/error result
|
|
|
|
*/
|
2019-08-15 01:56:12 +00:00
|
|
|
struct cbfs_priv {
|
2020-05-24 23:38:14 +00:00
|
|
|
bool initialized;
|
2020-05-24 23:38:20 +00:00
|
|
|
void *start;
|
2019-08-15 01:56:12 +00:00
|
|
|
struct cbfs_header header;
|
|
|
|
struct cbfs_cachenode *file_cache;
|
2019-08-15 01:56:13 +00:00
|
|
|
enum cbfs_result result;
|
2019-08-15 01:56:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct cbfs_priv cbfs_s;
|
2012-10-12 14:26:11 +00:00
|
|
|
|
|
|
|
const char *file_cbfs_error(void)
|
|
|
|
{
|
2019-08-15 01:56:13 +00:00
|
|
|
switch (cbfs_s.result) {
|
2012-10-12 14:26:11 +00:00
|
|
|
case CBFS_SUCCESS:
|
|
|
|
return "Success";
|
|
|
|
case CBFS_NOT_INITIALIZED:
|
|
|
|
return "CBFS not initialized";
|
|
|
|
case CBFS_BAD_HEADER:
|
|
|
|
return "Bad CBFS header";
|
|
|
|
case CBFS_BAD_FILE:
|
|
|
|
return "Bad CBFS file";
|
|
|
|
case CBFS_FILE_NOT_FOUND:
|
|
|
|
return "File not found";
|
|
|
|
default:
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 01:56:13 +00:00
|
|
|
enum cbfs_result cbfs_get_result(void)
|
|
|
|
{
|
|
|
|
return cbfs_s.result;
|
|
|
|
}
|
|
|
|
|
2012-10-12 14:26:11 +00:00
|
|
|
/* Do endian conversion on the CBFS header structure. */
|
|
|
|
static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
|
|
|
|
{
|
|
|
|
dest->magic = be32_to_cpu(src->magic);
|
|
|
|
dest->version = be32_to_cpu(src->version);
|
|
|
|
dest->rom_size = be32_to_cpu(src->rom_size);
|
|
|
|
dest->boot_block_size = be32_to_cpu(src->boot_block_size);
|
|
|
|
dest->align = be32_to_cpu(src->align);
|
|
|
|
dest->offset = be32_to_cpu(src->offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do endian conversion on a CBFS file header. */
|
|
|
|
static void swap_file_header(struct cbfs_fileheader *dest,
|
|
|
|
const struct cbfs_fileheader *src)
|
|
|
|
{
|
|
|
|
memcpy(&dest->magic, &src->magic, sizeof(dest->magic));
|
|
|
|
dest->len = be32_to_cpu(src->len);
|
|
|
|
dest->type = be32_to_cpu(src->type);
|
2019-07-08 19:18:22 +00:00
|
|
|
dest->attributes_offset = be32_to_cpu(src->attributes_offset);
|
2012-10-12 14:26:11 +00:00
|
|
|
dest->offset = be32_to_cpu(src->offset);
|
|
|
|
}
|
|
|
|
|
2021-03-15 05:00:14 +00:00
|
|
|
/**
|
|
|
|
* fill_node() - Fill a node struct with information from the CBFS
|
|
|
|
*
|
|
|
|
* @node: Node to fill
|
|
|
|
* @start: Pointer to the start of the CBFS file in memory
|
|
|
|
* @header: Pointer to the header information (in our enddianess)
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: 0 if OK, -EBADF if the header is too small
|
2021-03-15 05:00:14 +00:00
|
|
|
*/
|
|
|
|
static int fill_node(struct cbfs_cachenode *node, void *start,
|
|
|
|
struct cbfs_fileheader *header)
|
|
|
|
{
|
|
|
|
uint name_len;
|
2021-03-15 05:00:16 +00:00
|
|
|
uint offset;
|
2021-03-15 05:00:14 +00:00
|
|
|
|
|
|
|
/* Check the header is large enough */
|
|
|
|
if (header->offset < sizeof(struct cbfs_fileheader))
|
|
|
|
return -EBADF;
|
|
|
|
|
|
|
|
node->next = NULL;
|
|
|
|
node->type = header->type;
|
|
|
|
node->data = start + header->offset;
|
|
|
|
node->data_length = header->len;
|
|
|
|
name_len = header->offset - sizeof(struct cbfs_fileheader);
|
|
|
|
node->name = start + sizeof(struct cbfs_fileheader);
|
|
|
|
node->name_length = name_len;
|
|
|
|
node->attr_offset = header->attributes_offset;
|
2021-03-15 05:00:16 +00:00
|
|
|
node->comp_algo = CBFS_COMPRESS_NONE;
|
|
|
|
node->decomp_size = 0;
|
|
|
|
|
|
|
|
for (offset = node->attr_offset; offset < header->offset;) {
|
|
|
|
const struct cbfs_file_attribute *attr;
|
|
|
|
uint tag, len;
|
|
|
|
|
|
|
|
attr = start + offset;
|
|
|
|
tag = be32_to_cpu(attr->tag);
|
|
|
|
len = be32_to_cpu(attr->len);
|
|
|
|
if (tag == CBFS_FILE_ATTR_TAG_COMPRESSION) {
|
|
|
|
struct cbfs_file_attr_compression *comp;
|
|
|
|
|
|
|
|
comp = start + offset;
|
|
|
|
node->comp_algo = be32_to_cpu(comp->compression);
|
|
|
|
node->decomp_size =
|
|
|
|
be32_to_cpu(comp->decompressed_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += len;
|
|
|
|
}
|
2021-03-15 05:00:14 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-12 14:26:11 +00:00
|
|
|
/*
|
|
|
|
* Given a starting position in memory, scan forward, bounded by a size, and
|
|
|
|
* find the next valid CBFS file. No memory is allocated by this function. The
|
|
|
|
* caller is responsible for allocating space for the new file structure.
|
|
|
|
*
|
|
|
|
* @param start The location in memory to start from.
|
|
|
|
* @param size The size of the memory region to search.
|
|
|
|
* @param align The alignment boundaries to check on.
|
2021-03-15 05:00:10 +00:00
|
|
|
* @param node A pointer to the file structure to load.
|
2012-10-12 14:26:11 +00:00
|
|
|
* @param used A pointer to the count of of bytes scanned through,
|
|
|
|
* including the file if one is found.
|
|
|
|
*
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: 0 if a file is found, -ENOENT if one isn't, -EBADF if a bad header
|
2020-05-24 23:38:15 +00:00
|
|
|
* is found.
|
2012-10-12 14:26:11 +00:00
|
|
|
*/
|
2020-05-24 23:38:19 +00:00
|
|
|
static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size,
|
2021-03-15 05:00:10 +00:00
|
|
|
int align, struct cbfs_cachenode *node,
|
2020-05-24 23:38:15 +00:00
|
|
|
int *used)
|
2012-10-12 14:26:11 +00:00
|
|
|
{
|
|
|
|
struct cbfs_fileheader header;
|
|
|
|
|
|
|
|
*used = 0;
|
|
|
|
|
|
|
|
while (size >= align) {
|
2020-05-24 23:38:19 +00:00
|
|
|
const struct cbfs_fileheader *file_header = start;
|
2021-03-15 05:00:14 +00:00
|
|
|
int ret;
|
2012-10-12 14:26:11 +00:00
|
|
|
|
|
|
|
/* Check if there's a file here. */
|
2019-08-15 01:56:15 +00:00
|
|
|
if (memcmp(good_file_magic, &file_header->magic,
|
|
|
|
sizeof(file_header->magic))) {
|
2012-10-12 14:26:11 +00:00
|
|
|
*used += align;
|
|
|
|
size -= align;
|
|
|
|
start += align;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-15 01:56:15 +00:00
|
|
|
swap_file_header(&header, file_header);
|
2021-05-14 01:39:27 +00:00
|
|
|
if (header.offset >= size)
|
|
|
|
return log_msg_ret("range", -E2BIG);
|
2021-03-15 05:00:14 +00:00
|
|
|
ret = fill_node(node, start, &header);
|
|
|
|
if (ret) {
|
2019-08-15 01:56:13 +00:00
|
|
|
priv->result = CBFS_BAD_FILE;
|
2021-03-15 05:00:14 +00:00
|
|
|
return log_msg_ret("fill", ret);
|
2012-10-12 14:26:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 05:00:15 +00:00
|
|
|
*used += ALIGN(header.len, align);
|
2020-05-24 23:38:15 +00:00
|
|
|
return 0;
|
2012-10-12 14:26:11 +00:00
|
|
|
}
|
2020-05-24 23:38:15 +00:00
|
|
|
|
|
|
|
return -ENOENT;
|
2012-10-12 14:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Look through a CBFS instance and copy file metadata into regular memory. */
|
2020-05-24 23:38:20 +00:00
|
|
|
static int file_cbfs_fill_cache(struct cbfs_priv *priv, int size, int align)
|
2012-10-12 14:26:11 +00:00
|
|
|
{
|
|
|
|
struct cbfs_cachenode *cache_node;
|
2021-03-15 05:00:10 +00:00
|
|
|
struct cbfs_cachenode *node;
|
2019-08-15 01:56:12 +00:00
|
|
|
struct cbfs_cachenode **cache_tail = &priv->file_cache;
|
2020-05-24 23:38:20 +00:00
|
|
|
void *start;
|
2012-10-12 14:26:11 +00:00
|
|
|
|
|
|
|
/* Clear out old information. */
|
2019-08-15 01:56:12 +00:00
|
|
|
cache_node = priv->file_cache;
|
2012-10-12 14:26:11 +00:00
|
|
|
while (cache_node) {
|
2019-08-15 01:56:15 +00:00
|
|
|
struct cbfs_cachenode *old_node = cache_node;
|
2012-10-12 14:26:11 +00:00
|
|
|
cache_node = cache_node->next;
|
2019-08-15 01:56:15 +00:00
|
|
|
free(old_node);
|
2012-10-12 14:26:11 +00:00
|
|
|
}
|
2019-08-15 01:56:12 +00:00
|
|
|
priv->file_cache = NULL;
|
2012-10-12 14:26:11 +00:00
|
|
|
|
2020-05-24 23:38:20 +00:00
|
|
|
start = priv->start;
|
2012-10-12 14:26:11 +00:00
|
|
|
while (size >= align) {
|
2020-05-24 23:38:15 +00:00
|
|
|
int used;
|
2020-05-24 23:38:12 +00:00
|
|
|
int ret;
|
2012-10-12 14:26:11 +00:00
|
|
|
|
2021-03-15 05:00:17 +00:00
|
|
|
node = malloc(sizeof(struct cbfs_cachenode));
|
2021-03-15 05:00:10 +00:00
|
|
|
if (!node)
|
2020-05-24 23:38:15 +00:00
|
|
|
return -ENOMEM;
|
2021-03-15 05:00:10 +00:00
|
|
|
ret = file_cbfs_next_file(priv, start, size, align, node,
|
2020-05-24 23:38:12 +00:00
|
|
|
&used);
|
2012-10-12 14:26:11 +00:00
|
|
|
|
2020-05-24 23:38:12 +00:00
|
|
|
if (ret < 0) {
|
2021-03-15 05:00:10 +00:00
|
|
|
free(node);
|
2020-05-24 23:38:15 +00:00
|
|
|
if (ret == -ENOENT)
|
|
|
|
break;
|
|
|
|
return ret;
|
2012-10-12 14:26:11 +00:00
|
|
|
}
|
2021-03-15 05:00:10 +00:00
|
|
|
*cache_tail = node;
|
|
|
|
cache_tail = &node->next;
|
2012-10-12 14:26:11 +00:00
|
|
|
|
|
|
|
size -= used;
|
|
|
|
start += used;
|
|
|
|
}
|
2019-08-15 01:56:13 +00:00
|
|
|
priv->result = CBFS_SUCCESS;
|
2020-05-24 23:38:15 +00:00
|
|
|
|
|
|
|
return 0;
|
2012-10-12 14:26:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 23:38:18 +00:00
|
|
|
/**
|
|
|
|
* load_header() - Load the CBFS header
|
|
|
|
*
|
|
|
|
* Get the CBFS header out of the ROM and do endian conversion.
|
|
|
|
*
|
|
|
|
* @priv: Private data, which is inited by this function
|
|
|
|
* @addr: Address of CBFS header in memory-mapped SPI flash
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: 0 if OK, -ENXIO if the header is bad
|
2020-05-24 23:38:18 +00:00
|
|
|
*/
|
|
|
|
static int load_header(struct cbfs_priv *priv, ulong addr)
|
2012-10-12 14:26:11 +00:00
|
|
|
{
|
2020-05-24 23:38:16 +00:00
|
|
|
struct cbfs_header *header = &priv->header;
|
2012-10-12 14:26:11 +00:00
|
|
|
struct cbfs_header *header_in_rom;
|
|
|
|
|
2020-05-24 23:38:18 +00:00
|
|
|
memset(priv, '\0', sizeof(*priv));
|
|
|
|
header_in_rom = (struct cbfs_header *)addr;
|
2012-10-12 14:26:11 +00:00
|
|
|
swap_header(header, header_in_rom);
|
|
|
|
|
|
|
|
if (header->magic != good_magic || header->offset >
|
|
|
|
header->rom_size - header->boot_block_size) {
|
2020-05-24 23:38:16 +00:00
|
|
|
priv->result = CBFS_BAD_HEADER;
|
2020-05-24 23:38:18 +00:00
|
|
|
return -ENXIO;
|
2012-10-12 14:26:11 +00:00
|
|
|
}
|
2020-05-24 23:38:18 +00:00
|
|
|
|
2012-10-12 14:26:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-24 23:38:18 +00:00
|
|
|
/**
|
|
|
|
* file_cbfs_load_header() - Get the CBFS header out of the ROM, given the end
|
|
|
|
*
|
|
|
|
* @priv: Private data, which is inited by this function
|
|
|
|
* @end_of_rom: Address of the last byte of the ROM (typically 0xffffffff)
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: 0 if OK, -ENXIO if the header is bad
|
2020-05-24 23:38:18 +00:00
|
|
|
*/
|
|
|
|
static int file_cbfs_load_header(struct cbfs_priv *priv, ulong end_of_rom)
|
2019-08-15 01:56:14 +00:00
|
|
|
{
|
2020-05-24 23:38:18 +00:00
|
|
|
int offset = *(u32 *)(end_of_rom - 3);
|
2020-05-24 23:38:20 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = load_header(priv, end_of_rom + offset + 1);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
priv->start = (void *)(end_of_rom + 1 - priv->header.rom_size);
|
2019-08-15 01:56:14 +00:00
|
|
|
|
2020-05-24 23:38:20 +00:00
|
|
|
return 0;
|
2020-05-24 23:38:18 +00:00
|
|
|
}
|
2019-08-15 01:56:14 +00:00
|
|
|
|
2020-05-24 23:38:18 +00:00
|
|
|
/**
|
|
|
|
* cbfs_load_header_ptr() - Get the CBFS header out of the ROM, given the base
|
|
|
|
*
|
|
|
|
* @priv: Private data, which is inited by this function
|
|
|
|
* @base: Address of the first byte of the ROM (e.g. 0xff000000)
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: 0 if OK, -ENXIO if the header is bad
|
2020-05-24 23:38:18 +00:00
|
|
|
*/
|
|
|
|
static int cbfs_load_header_ptr(struct cbfs_priv *priv, ulong base)
|
|
|
|
{
|
2020-05-24 23:38:20 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = load_header(priv, base + MASTER_HDR_OFFSET);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
priv->start = (void *)base;
|
|
|
|
|
|
|
|
return 0;
|
2019-08-15 01:56:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 23:38:21 +00:00
|
|
|
static int cbfs_init(struct cbfs_priv *priv, ulong end_of_rom)
|
2012-10-12 14:26:11 +00:00
|
|
|
{
|
2020-05-24 23:38:21 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = file_cbfs_load_header(priv, end_of_rom);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2012-10-12 14:26:11 +00:00
|
|
|
|
2020-05-24 23:38:21 +00:00
|
|
|
ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
|
|
|
|
priv->header.align);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
priv->initialized = true;
|
|
|
|
|
|
|
|
return 0;
|
2019-08-15 01:56:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 23:38:21 +00:00
|
|
|
int file_cbfs_init(ulong end_of_rom)
|
2019-08-15 01:56:12 +00:00
|
|
|
{
|
2020-05-24 23:38:21 +00:00
|
|
|
return cbfs_init(&cbfs_s, end_of_rom);
|
2012-10-12 14:26:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 05:00:12 +00:00
|
|
|
int cbfs_init_mem(ulong base, ulong size, bool require_hdr,
|
|
|
|
struct cbfs_priv **privp)
|
2019-08-15 01:56:14 +00:00
|
|
|
{
|
|
|
|
struct cbfs_priv priv_s, *priv = &priv_s;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
2021-03-15 05:00:12 +00:00
|
|
|
* Use a local variable to start with until we know that the * CBFS is
|
|
|
|
* valid. Note that size is detected from the header, if present,
|
|
|
|
* meaning the parameter is ignored.
|
2019-08-15 01:56:14 +00:00
|
|
|
*/
|
2020-05-24 23:38:18 +00:00
|
|
|
ret = cbfs_load_header_ptr(priv, base);
|
2021-03-15 05:00:12 +00:00
|
|
|
if (ret) {
|
|
|
|
if (require_hdr || size == CBFS_SIZE_UNKNOWN)
|
|
|
|
return ret;
|
|
|
|
memset(priv, '\0', sizeof(struct cbfs_priv));
|
|
|
|
priv->header.rom_size = size;
|
|
|
|
priv->header.align = CBFS_ALIGN_SIZE;
|
|
|
|
priv->start = (void *)base;
|
|
|
|
}
|
2019-08-15 01:56:14 +00:00
|
|
|
|
2020-05-24 23:38:24 +00:00
|
|
|
ret = file_cbfs_fill_cache(priv, priv->header.rom_size,
|
|
|
|
priv->header.align);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("fill", ret);
|
2019-08-15 01:56:14 +00:00
|
|
|
|
2020-05-24 23:38:14 +00:00
|
|
|
priv->initialized = true;
|
2019-08-15 01:56:14 +00:00
|
|
|
priv = malloc(sizeof(priv_s));
|
|
|
|
if (!priv)
|
|
|
|
return -ENOMEM;
|
|
|
|
memcpy(priv, &priv_s, sizeof(priv_s));
|
|
|
|
*privp = priv;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-12 14:26:11 +00:00
|
|
|
const struct cbfs_header *file_cbfs_get_header(void)
|
|
|
|
{
|
2019-08-15 01:56:12 +00:00
|
|
|
struct cbfs_priv *priv = &cbfs_s;
|
|
|
|
|
|
|
|
if (priv->initialized) {
|
2019-08-15 01:56:13 +00:00
|
|
|
priv->result = CBFS_SUCCESS;
|
2019-08-15 01:56:12 +00:00
|
|
|
return &priv->header;
|
2012-10-12 14:26:11 +00:00
|
|
|
} else {
|
2019-08-15 01:56:13 +00:00
|
|
|
priv->result = CBFS_NOT_INITIALIZED;
|
2012-10-12 14:26:11 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 05:00:13 +00:00
|
|
|
const struct cbfs_cachenode *cbfs_get_first(const struct cbfs_priv *priv)
|
|
|
|
{
|
|
|
|
return priv->file_cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cbfs_get_next(const struct cbfs_cachenode **filep)
|
|
|
|
{
|
|
|
|
if (*filep)
|
|
|
|
*filep = (*filep)->next;
|
|
|
|
}
|
|
|
|
|
2012-10-12 14:26:11 +00:00
|
|
|
const struct cbfs_cachenode *file_cbfs_get_first(void)
|
|
|
|
{
|
2019-08-15 01:56:12 +00:00
|
|
|
struct cbfs_priv *priv = &cbfs_s;
|
|
|
|
|
|
|
|
if (!priv->initialized) {
|
2019-08-15 01:56:13 +00:00
|
|
|
priv->result = CBFS_NOT_INITIALIZED;
|
2012-10-12 14:26:11 +00:00
|
|
|
return NULL;
|
|
|
|
} else {
|
2019-08-15 01:56:13 +00:00
|
|
|
priv->result = CBFS_SUCCESS;
|
2019-08-15 01:56:12 +00:00
|
|
|
return priv->file_cache;
|
2012-10-12 14:26:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_cbfs_get_next(const struct cbfs_cachenode **file)
|
|
|
|
{
|
2019-08-15 01:56:12 +00:00
|
|
|
struct cbfs_priv *priv = &cbfs_s;
|
|
|
|
|
|
|
|
if (!priv->initialized) {
|
2019-08-15 01:56:13 +00:00
|
|
|
priv->result = CBFS_NOT_INITIALIZED;
|
2019-08-15 01:56:12 +00:00
|
|
|
*file = NULL;
|
2012-10-12 14:26:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*file)
|
|
|
|
*file = (*file)->next;
|
2019-08-15 01:56:13 +00:00
|
|
|
priv->result = CBFS_SUCCESS;
|
2012-10-12 14:26:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 01:56:12 +00:00
|
|
|
const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
|
|
|
|
const char *name)
|
2012-10-12 14:26:11 +00:00
|
|
|
{
|
2019-08-15 01:56:12 +00:00
|
|
|
struct cbfs_cachenode *cache_node = priv->file_cache;
|
2012-10-12 14:26:11 +00:00
|
|
|
|
2019-08-15 01:56:12 +00:00
|
|
|
if (!priv->initialized) {
|
2019-08-15 01:56:13 +00:00
|
|
|
priv->result = CBFS_NOT_INITIALIZED;
|
2012-10-12 14:26:11 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (cache_node) {
|
|
|
|
if (!strcmp(name, cache_node->name))
|
|
|
|
break;
|
|
|
|
cache_node = cache_node->next;
|
|
|
|
}
|
|
|
|
if (!cache_node)
|
2019-08-15 01:56:13 +00:00
|
|
|
priv->result = CBFS_FILE_NOT_FOUND;
|
2012-10-12 14:26:11 +00:00
|
|
|
else
|
2019-08-15 01:56:13 +00:00
|
|
|
priv->result = CBFS_SUCCESS;
|
2012-10-12 14:26:11 +00:00
|
|
|
|
|
|
|
return cache_node;
|
|
|
|
}
|
|
|
|
|
2019-08-15 01:56:12 +00:00
|
|
|
const struct cbfs_cachenode *file_cbfs_find(const char *name)
|
|
|
|
{
|
|
|
|
return cbfs_find_file(&cbfs_s, name);
|
|
|
|
}
|
|
|
|
|
2020-05-24 23:38:22 +00:00
|
|
|
static int find_uncached(struct cbfs_priv *priv, const char *name, void *start,
|
|
|
|
struct cbfs_cachenode *node)
|
2012-10-12 14:26:11 +00:00
|
|
|
{
|
2020-05-24 23:38:22 +00:00
|
|
|
int size = priv->header.rom_size;
|
|
|
|
int align = priv->header.align;
|
2012-10-12 14:26:11 +00:00
|
|
|
|
|
|
|
while (size >= align) {
|
2020-05-24 23:38:15 +00:00
|
|
|
int used;
|
2020-05-24 23:38:22 +00:00
|
|
|
int ret;
|
2012-10-12 14:26:11 +00:00
|
|
|
|
2020-05-24 23:38:22 +00:00
|
|
|
ret = file_cbfs_next_file(priv, start, size, align, node,
|
2020-05-24 23:38:12 +00:00
|
|
|
&used);
|
2020-05-24 23:38:15 +00:00
|
|
|
if (ret == -ENOENT)
|
2012-10-12 14:26:11 +00:00
|
|
|
break;
|
2020-05-24 23:38:15 +00:00
|
|
|
else if (ret)
|
2020-05-24 23:38:22 +00:00
|
|
|
return ret;
|
|
|
|
if (!strcmp(name, node->name))
|
|
|
|
return 0;
|
2012-10-12 14:26:11 +00:00
|
|
|
|
|
|
|
size -= used;
|
|
|
|
start += used;
|
|
|
|
}
|
2020-05-24 23:38:22 +00:00
|
|
|
priv->result = CBFS_FILE_NOT_FOUND;
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
int file_cbfs_find_uncached(ulong end_of_rom, const char *name,
|
|
|
|
struct cbfs_cachenode *node)
|
|
|
|
{
|
|
|
|
struct cbfs_priv priv;
|
|
|
|
void *start;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = file_cbfs_load_header(&priv, end_of_rom);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
start = priv.start;
|
|
|
|
|
|
|
|
return find_uncached(&priv, name, start, node);
|
2012-10-12 14:26:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 23:38:23 +00:00
|
|
|
int file_cbfs_find_uncached_base(ulong base, const char *name,
|
|
|
|
struct cbfs_cachenode *node)
|
|
|
|
{
|
|
|
|
struct cbfs_priv priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = cbfs_load_header_ptr(&priv, base);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return find_uncached(&priv, name, (void *)base, node);
|
|
|
|
}
|
|
|
|
|
2012-10-12 14:26:11 +00:00
|
|
|
const char *file_cbfs_name(const struct cbfs_cachenode *file)
|
|
|
|
{
|
2019-08-15 01:56:13 +00:00
|
|
|
cbfs_s.result = CBFS_SUCCESS;
|
|
|
|
|
2012-10-12 14:26:11 +00:00
|
|
|
return file->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 file_cbfs_size(const struct cbfs_cachenode *file)
|
|
|
|
{
|
2019-08-15 01:56:13 +00:00
|
|
|
cbfs_s.result = CBFS_SUCCESS;
|
|
|
|
|
2012-10-12 14:26:11 +00:00
|
|
|
return file->data_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 file_cbfs_type(const struct cbfs_cachenode *file)
|
|
|
|
{
|
2019-08-15 01:56:13 +00:00
|
|
|
cbfs_s.result = CBFS_SUCCESS;
|
|
|
|
|
2012-10-12 14:26:11 +00:00
|
|
|
return file->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
|
|
|
|
unsigned long maxsize)
|
|
|
|
{
|
|
|
|
u32 size;
|
|
|
|
|
|
|
|
size = file->data_length;
|
|
|
|
if (maxsize && size > maxsize)
|
|
|
|
size = maxsize;
|
|
|
|
|
|
|
|
memcpy(buffer, file->data, size);
|
2019-08-15 01:56:13 +00:00
|
|
|
cbfs_s.result = CBFS_SUCCESS;
|
2012-10-12 14:26:11 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|