Skip to content

Commit

Permalink
cbfs: Move result variable into the struct
Browse files Browse the repository at this point in the history
Move the result variable into the struct also, so that it can be used when
BSS is not available. Add a function to read it.

Note that all functions sill use the BSS version of the data.

Signed-off-by: Simon Glass <[email protected]>
Reviewed-by: Bin Meng <[email protected]>
Tested-by: Bin Meng <[email protected]>
  • Loading branch information
sjg20 authored and lbmeng committed Aug 18, 2019
1 parent 02e4af6 commit c7f1693
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
4 changes: 2 additions & 2 deletions cmd/cbfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static int do_cbfs_init(cmd_tbl_t *cmdtp, int flag, int argc,
}
}
file_cbfs_init(end_of_rom);
if (file_cbfs_result != CBFS_SUCCESS) {
if (cbfs_get_result() != CBFS_SUCCESS) {
printf("%s.\n", file_cbfs_error());
return 1;
}
Expand Down Expand Up @@ -67,7 +67,7 @@ static int do_cbfs_fsload(cmd_tbl_t *cmdtp, int flag, int argc,

file = file_cbfs_find(argv[2]);
if (!file) {
if (file_cbfs_result == CBFS_FILE_NOT_FOUND)
if (cbfs_get_result() == CBFS_FILE_NOT_FOUND)
printf("%s: %s\n", file_cbfs_error(), argv[2]);
else
printf("%s.\n", file_cbfs_error());
Expand Down
48 changes: 28 additions & 20 deletions fs/cbfs/cbfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
#include <malloc.h>
#include <asm/byteorder.h>

enum cbfs_result file_cbfs_result;
static const u32 good_magic = 0x4f524243;
static const u8 good_file_magic[] = "LARCHIVE";

struct cbfs_priv {
int initialized;
struct cbfs_header header;
struct cbfs_cachenode *file_cache;
enum cbfs_result result;
};

static struct cbfs_priv cbfs_s;

const char *file_cbfs_error(void)
{
switch (file_cbfs_result) {
switch (cbfs_s.result) {
case CBFS_SUCCESS:
return "Success";
case CBFS_NOT_INITIALIZED:
Expand All @@ -38,6 +38,11 @@ const char *file_cbfs_error(void)
}
}

enum cbfs_result cbfs_get_result(void)
{
return cbfs_s.result;
}

/* Do endian conversion on the CBFS header structure. */
static void swap_header(struct cbfs_header *dest, struct cbfs_header *src)
{
Expand Down Expand Up @@ -99,7 +104,7 @@ static int file_cbfs_next_file(struct cbfs_priv *priv, u8 *start, u32 size,

swap_file_header(&header, fileHeader);
if (header.offset < sizeof(struct cbfs_fileheader)) {
file_cbfs_result = CBFS_BAD_FILE;
priv->result = CBFS_BAD_FILE;
return -1;
}
newNode->next = NULL;
Expand Down Expand Up @@ -161,7 +166,7 @@ static void file_cbfs_fill_cache(struct cbfs_priv *priv, u8 *start, u32 size,
size -= used;
start += used;
}
file_cbfs_result = CBFS_SUCCESS;
priv->result = CBFS_SUCCESS;
}

/* Get the CBFS header out of the ROM and do endian conversion. */
Expand All @@ -176,7 +181,7 @@ static int file_cbfs_load_header(uintptr_t end_of_rom,

if (header->magic != good_magic || header->offset >
header->rom_size - header->boot_block_size) {
file_cbfs_result = CBFS_BAD_HEADER;
cbfs_s.result = CBFS_BAD_HEADER;
return 1;
}
return 0;
Expand All @@ -195,7 +200,7 @@ static void cbfs_init(struct cbfs_priv *priv, uintptr_t end_of_rom)

file_cbfs_fill_cache(priv, start_of_rom, priv->header.rom_size,
priv->header.align);
if (file_cbfs_result == CBFS_SUCCESS)
if (priv->result == CBFS_SUCCESS)
priv->initialized = 1;
}

Expand All @@ -209,10 +214,10 @@ const struct cbfs_header *file_cbfs_get_header(void)
struct cbfs_priv *priv = &cbfs_s;

if (priv->initialized) {
file_cbfs_result = CBFS_SUCCESS;
priv->result = CBFS_SUCCESS;
return &priv->header;
} else {
file_cbfs_result = CBFS_NOT_INITIALIZED;
priv->result = CBFS_NOT_INITIALIZED;
return NULL;
}
}
Expand All @@ -222,10 +227,10 @@ const struct cbfs_cachenode *file_cbfs_get_first(void)
struct cbfs_priv *priv = &cbfs_s;

if (!priv->initialized) {
file_cbfs_result = CBFS_NOT_INITIALIZED;
priv->result = CBFS_NOT_INITIALIZED;
return NULL;
} else {
file_cbfs_result = CBFS_SUCCESS;
priv->result = CBFS_SUCCESS;
return priv->file_cache;
}
}
Expand All @@ -235,14 +240,14 @@ void file_cbfs_get_next(const struct cbfs_cachenode **file)
struct cbfs_priv *priv = &cbfs_s;

if (!priv->initialized) {
file_cbfs_result = CBFS_NOT_INITIALIZED;
priv->result = CBFS_NOT_INITIALIZED;
*file = NULL;
return;
}

if (*file)
*file = (*file)->next;
file_cbfs_result = CBFS_SUCCESS;
priv->result = CBFS_SUCCESS;
}

const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
Expand All @@ -251,7 +256,7 @@ const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
struct cbfs_cachenode *cache_node = priv->file_cache;

if (!priv->initialized) {
file_cbfs_result = CBFS_NOT_INITIALIZED;
priv->result = CBFS_NOT_INITIALIZED;
return NULL;
}

Expand All @@ -261,9 +266,9 @@ const struct cbfs_cachenode *cbfs_find_file(struct cbfs_priv *priv,
cache_node = cache_node->next;
}
if (!cache_node)
file_cbfs_result = CBFS_FILE_NOT_FOUND;
priv->result = CBFS_FILE_NOT_FOUND;
else
file_cbfs_result = CBFS_SUCCESS;
priv->result = CBFS_SUCCESS;

return cache_node;
}
Expand Down Expand Up @@ -307,25 +312,28 @@ const struct cbfs_cachenode *file_cbfs_find_uncached(uintptr_t end_of_rom,
size -= used;
start += used;
}
file_cbfs_result = CBFS_FILE_NOT_FOUND;
cbfs_s.result = CBFS_FILE_NOT_FOUND;
return NULL;
}

const char *file_cbfs_name(const struct cbfs_cachenode *file)
{
file_cbfs_result = CBFS_SUCCESS;
cbfs_s.result = CBFS_SUCCESS;

return file->name;
}

u32 file_cbfs_size(const struct cbfs_cachenode *file)
{
file_cbfs_result = CBFS_SUCCESS;
cbfs_s.result = CBFS_SUCCESS;

return file->data_length;
}

u32 file_cbfs_type(const struct cbfs_cachenode *file)
{
file_cbfs_result = CBFS_SUCCESS;
cbfs_s.result = CBFS_SUCCESS;

return file->type;
}

Expand All @@ -339,7 +347,7 @@ long file_cbfs_read(const struct cbfs_cachenode *file, void *buffer,
size = maxsize;

memcpy(buffer, file->data, size);
cbfs_s.result = CBFS_SUCCESS;

file_cbfs_result = CBFS_SUCCESS;
return size;
}
7 changes: 7 additions & 0 deletions include/cbfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ extern enum cbfs_result file_cbfs_result;
*/
const char *file_cbfs_error(void);

/**
* cbfs_get_result() - Get the result of the last CBFS operation
*
*@return last result
*/
enum cbfs_result cbfs_get_result(void);

/**
* file_cbfs_init() - Initialize the CBFS driver and load metadata into RAM.
*
Expand Down

0 comments on commit c7f1693

Please sign in to comment.