From: Simon Glass Date: Mon, 15 Mar 2021 05:00:14 +0000 (+1300) Subject: cbfs: Factor out filling a cache node into a new function X-Git-Url: http://git.dujemihanovic.xyz/?a=commitdiff_plain;h=70a394a6b6437e18e67edfd7054302b830e1fe6a;p=u-boot.git cbfs: Factor out filling a cache node into a new function The file_cbfs_next_file() function is already fairly long. Before expanding it further, move the core part into a separate function. Signed-off-by: Simon Glass --- diff --git a/fs/cbfs/cbfs.c b/fs/cbfs/cbfs.c index aed4026d65..a93dc3d0c1 100644 --- a/fs/cbfs/cbfs.c +++ b/fs/cbfs/cbfs.c @@ -79,6 +79,35 @@ static void swap_file_header(struct cbfs_fileheader *dest, dest->offset = be32_to_cpu(src->offset); } +/** + * 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) + * @return 0 if OK, -EBADF if the header is too small + */ +static int fill_node(struct cbfs_cachenode *node, void *start, + struct cbfs_fileheader *header) +{ + uint name_len; + + /* 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; + + return 0; +} + /* * 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 @@ -104,8 +133,8 @@ static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size, while (size >= align) { const struct cbfs_fileheader *file_header = start; - u32 name_len; u32 step; + int ret; /* Check if there's a file here. */ if (memcmp(good_file_magic, &file_header->magic, @@ -117,19 +146,11 @@ static int file_cbfs_next_file(struct cbfs_priv *priv, void *start, int size, } swap_file_header(&header, file_header); - if (header.offset < sizeof(struct cbfs_fileheader)) { + ret = fill_node(node, start, &header); + if (ret) { priv->result = CBFS_BAD_FILE; - return -EBADF; + return log_msg_ret("fill", ret); } - 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 = (char *)file_header + - sizeof(struct cbfs_fileheader); - node->name_length = name_len; - node->attr_offset = header.attributes_offset; step = header.len; if (step % align)