[PATCH 02/17] fs: boot: Update fs_load_alloc() to use abuf
Simon Glass
sjg at chromium.org
Wed Mar 19 15:37:56 CET 2025
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 at chromium.org>
---
cmd/cat.c | 13 ++++++-------
cmd/cedit.c | 18 ++++++++----------
fs/fs.c | 11 ++++-------
include/fs.h | 10 +++++-----
4 files changed, 23 insertions(+), 29 deletions(-)
diff --git a/cmd/cat.c b/cmd/cat.c
index 6828b7b364e..24983cb9ca0 100644
--- a/cmd/cat.c
+++ b/cmd/cat.c
@@ -4,6 +4,7 @@
* Roger Knecht <rknecht at pm.de>
*/
+#include <abuf.h>
#include <command.h>
#include <fs.h>
#include <malloc.h>
@@ -12,11 +13,10 @@
static int do_cat(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
+ struct abuf buf;
char *ifname;
char *dev;
char *file;
- char *buffer;
- ulong file_size;
int ret;
if (argc < 4)
@@ -26,8 +26,7 @@ static int do_cat(struct cmd_tbl *cmdtp, int flag, int argc,
dev = argv[2];
file = argv[3];
- ret = fs_load_alloc(ifname, dev, file, 0, 0, (void **)&buffer,
- &file_size);
+ ret = fs_load_alloc(ifname, dev, file, 0, 0, &buf);
// check file exists
switch (ret) {
@@ -51,10 +50,10 @@ static int do_cat(struct cmd_tbl *cmdtp, int flag, int argc,
}
// print file content
- buffer[file_size] = '\0';
- puts(buffer);
+ ((char *)buf.data)[buf.size] = '\0';
+ puts(buf.data);
- free(buffer);
+ abuf_uninit(&buf);
return 0;
}
diff --git a/cmd/cedit.c b/cmd/cedit.c
index f696356419e..b0eca7b4daf 100644
--- a/cmd/cedit.c
+++ b/cmd/cedit.c
@@ -34,22 +34,21 @@ static int do_cedit_load(struct cmd_tbl *cmdtp, int flag, int argc,
{
const char *fname;
struct expo *exp;
+ struct abuf buf;
oftree tree;
- ulong size;
- void *buf;
int ret;
if (argc < 4)
return CMD_RET_USAGE;
fname = argv[3];
- ret = fs_load_alloc(argv[1], argv[2], argv[3], SZ_1M, 0, &buf, &size);
+ ret = fs_load_alloc(argv[1], argv[2], argv[3], SZ_1M, 0, &buf);
if (ret) {
printf("File not found\n");
return CMD_RET_FAILURE;
}
- tree = oftree_from_fdt(buf);
+ tree = oftree_from_fdt(abuf_uninit_move(&buf, NULL));
if (!oftree_valid(tree)) {
printf("Cannot create oftree\n");
return CMD_RET_FAILURE;
@@ -125,31 +124,30 @@ static int do_cedit_read_fdt(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
const char *fname;
- void *buf;
+ struct abuf buf;
oftree tree;
- ulong size;
int ret;
if (argc < 4)
return CMD_RET_USAGE;
fname = argv[3];
- ret = fs_load_alloc(argv[1], argv[2], argv[3], SZ_1M, 0, &buf, &size);
+ ret = fs_load_alloc(argv[1], argv[2], argv[3], SZ_1M, 0, &buf);
if (ret) {
printf("File not found\n");
return CMD_RET_FAILURE;
}
- tree = oftree_from_fdt(buf);
+ tree = oftree_from_fdt(buf.data);
if (!oftree_valid(tree)) {
- free(buf);
+ abuf_uninit(&buf);
printf("Cannot create oftree\n");
return CMD_RET_FAILURE;
}
ret = cedit_read_settings(cur_exp, tree);
oftree_dispose(tree);
- free(buf);
+ abuf_uninit(&buf);
if (ret) {
printf("Failed to read settings: %dE\n", ret);
return CMD_RET_FAILURE;
diff --git a/fs/fs.c b/fs/fs.c
index e904b93258a..3505e698ba2 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -5,6 +5,7 @@
#define LOG_CATEGORY LOGC_CORE
+#include <abuf.h>
#include <bootstd.h>
#include <command.h>
#include <config.h>
@@ -1036,11 +1037,9 @@ int fs_read_alloc(const char *fname, ulong size, uint align, struct abuf *buf)
}
int fs_load_alloc(const char *ifname, const char *dev_part_str,
- const char *fname, ulong max_size, ulong align, void **bufp,
- ulong *sizep)
+ const char *fname, ulong max_size, ulong align,
+ struct abuf *buf)
{
- struct abuf buf;
- size_t bsize;
loff_t size;
int ret;
@@ -1057,11 +1056,9 @@ int fs_load_alloc(const char *ifname, const char *dev_part_str,
if (fs_set_blk_dev(ifname, dev_part_str, FS_TYPE_ANY))
return log_msg_ret("set", -ENOMEDIUM);
- ret = fs_read_alloc(fname, size, align, &buf);
+ ret = fs_read_alloc(fname, size, align, buf);
if (ret)
return log_msg_ret("al", ret);
- *bufp = abuf_uninit_move(&buf, &bsize);
- *sizep = bsize;
return 0;
}
diff --git a/include/fs.h b/include/fs.h
index 7ff649da821..653e6e37738 100644
--- a/include/fs.h
+++ b/include/fs.h
@@ -344,15 +344,15 @@ int fs_read_alloc(const char *fname, ulong size, uint align, struct abuf *buf);
* @fname: Filename to read
* @max_size: Maximum allowed size for the file (use 0 for 1GB)
* @align: Alignment to use for memory allocation (0 for default)
- * @bufp: On success, returns the allocated buffer with the nul-terminated file
- * in it
- * @sizep: On success, returns the size of the file
+ * @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, -ENOENT if the file does not
* exist, -ENOMEDIUM if the device does not exist, -E2BIG if the file is too
* large (greater than @max_size), -EIO if read failed
*/
int fs_load_alloc(const char *ifname, const char *dev_part_str,
- const char *fname, ulong max_size, ulong align, void **bufp,
- ulong *sizep);
+ const char *fname, ulong max_size, ulong align,
+ struct abuf *buf);
#endif /* _FS_H */
--
2.43.0
More information about the U-Boot
mailing list