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 <s...@chromium.org> --- boot/bootmeth-uclass.c | 19 ++++++++++--------- fs/fs.c | 25 +++++++++++-------------- include/fs.h | 8 +++++--- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/boot/bootmeth-uclass.c b/boot/bootmeth-uclass.c index 014b7588e8d..78a3671f96a 100644 --- a/boot/bootmeth-uclass.c +++ b/boot/bootmeth-uclass.c @@ -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; } diff --git a/fs/fs.c b/fs/fs.c index 77f7879276a..e904b93258a 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -1010,31 +1010,27 @@ 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) + abuf_init(buf); + if (!abuf_realloc(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; } @@ -1043,8 +1039,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)) @@ -1063,8 +1060,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; } diff --git a/include/fs.h b/include/fs.h index 2474880385d..7ff649da821 100644 --- a/include/fs.h +++ b/include/fs.h @@ -7,6 +7,7 @@ #include <rtc.h> +struct abuf; struct cmd_tbl; #define FS_TYPE_ANY 0 @@ -326,11 +327,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 -- 2.43.0