fs: boot: Update fs_read_alloc() to use abuf

Using an abuf for this function simplifies returning the size and also
makes it easier to free memory afterwards. Update the API and callers.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass
2025-05-01 07:37:02 -06:00
parent 45dc64e095
commit 0fa5b24f73
3 changed files with 25 additions and 26 deletions

View File

@@ -332,7 +332,8 @@ int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align,
enum bootflow_img_t type)
{
struct blk_desc *desc = NULL;
void *buf;
struct abuf buf;
ulong addr;
uint size;
int ret;
@@ -346,13 +347,13 @@ int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align,
return log_msg_ret("all", ret);
bflow->state = BOOTFLOWST_READY;
bflow->buf = buf;
addr = abuf_addr(&buf);
bflow->buf = abuf_uninit_move(&buf, NULL);
if (bflow->blk)
desc = dev_get_uclass_plat(bflow->blk);
if (!bootflow_img_add(bflow, bflow->fname, type, map_to_sysmem(buf),
size))
if (!bootflow_img_add(bflow, bflow->fname, type, addr, size))
return log_msg_ret("bai", -ENOMEM);
return 0;
@@ -362,9 +363,10 @@ int bootmeth_alloc_other(struct bootflow *bflow, const char *fname,
enum bootflow_img_t type, void **bufp, uint *sizep)
{
struct blk_desc *desc = NULL;
struct abuf buf;
char path[200];
loff_t size;
void *buf;
size_t bsize;
int ret;
snprintf(path, sizeof(path), "%s%s", bflow->subdir, fname);
@@ -388,12 +390,11 @@ int bootmeth_alloc_other(struct bootflow *bflow, const char *fname,
if (ret)
return log_msg_ret("all", ret);
if (!bootflow_img_add(bflow, bflow->fname, type, map_to_sysmem(buf),
size))
if (!bootflow_img_add(bflow, bflow->fname, type, abuf_addr(&buf), size))
return log_msg_ret("boi", -ENOMEM);
*bufp = buf;
*sizep = size;
*bufp = abuf_uninit_move(&buf, &bsize);
*sizep = bsize;
return 0;
}

24
fs/fs.c
View File

@@ -1128,31 +1128,26 @@ int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
return CMD_RET_SUCCESS;
}
int fs_read_alloc(const char *fname, ulong size, uint align, void **bufp)
int fs_read_alloc(const char *fname, ulong size, uint align, struct abuf *buf)
{
loff_t bytes_read;
ulong addr;
char *buf;
int ret;
if (!align)
align = ARCH_DMA_MINALIGN;
buf = memalign(align, size + 1);
if (!buf)
if (!abuf_init_size(buf, size + 1))
return log_msg_ret("buf", -ENOMEM);
addr = map_to_sysmem(buf);
buf->size--;
ret = fs_read(fname, addr, 0, size, &bytes_read);
ret = fs_read(fname, abuf_addr(buf), 0, size, &bytes_read);
if (ret) {
free(buf);
abuf_uninit(buf);
return log_msg_ret("read", ret);
}
if (size != bytes_read)
return log_msg_ret("bread", -EIO);
buf[size] = '\0';
*bufp = buf;
((char *)buf->data)[size] = '\0';
return 0;
}
@@ -1161,8 +1156,9 @@ int fs_load_alloc(const char *ifname, const char *dev_part_str,
const char *fname, ulong max_size, ulong align, void **bufp,
ulong *sizep)
{
struct abuf buf;
size_t bsize;
loff_t size;
void *buf;
int ret;
if (fs_set_blk_dev(ifname, dev_part_str, FS_TYPE_ANY))
@@ -1181,8 +1177,8 @@ int fs_load_alloc(const char *ifname, const char *dev_part_str,
ret = fs_read_alloc(fname, size, align, &buf);
if (ret)
return log_msg_ret("al", ret);
*sizep = size;
*bufp = buf;
*bufp = abuf_uninit_move(&buf, &bsize);
*sizep = bsize;
return 0;
}

View File

@@ -7,6 +7,7 @@
#include <rtc.h>
struct abuf;
struct cmd_tbl;
#define FS_TYPE_ANY 0
@@ -341,11 +342,12 @@ int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
* @fname: Filename to read
* @size: Size of file to read (must be correct!)
* @align: Alignment to use for memory allocation (0 for default: ARCH_DMA_MINALIGN)
* @bufp: On success, returns the allocated buffer with the nul-terminated file
* in it
* @buf: On success, returns the allocated buffer with the nul-terminated file
* in it. The buffer size is set to the size excluding the terminator. The
* buffer is inited by this function and must be uninited by the caller
* Return: 0 if OK, -ENOMEM if out of memory, -EIO if read failed
*/
int fs_read_alloc(const char *fname, ulong size, uint align, void **bufp);
int fs_read_alloc(const char *fname, ulong size, uint align, struct abuf *buf);
/**
* fs_load_alloc() - Load a file into allocated space