fs: boot: Update fs_load_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:
13
cmd/cat.c
13
cmd/cat.c
@@ -4,6 +4,7 @@
|
||||
* Roger Knecht <rknecht@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;
|
||||
}
|
||||
|
||||
18
cmd/cedit.c
18
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;
|
||||
|
||||
11
fs/fs.c
11
fs/fs.c
@@ -5,6 +5,7 @@
|
||||
|
||||
#define LOG_CATEGORY LOGC_CORE
|
||||
|
||||
#include <abuf.h>
|
||||
#include <bootstd.h>
|
||||
#include <command.h>
|
||||
#include <config.h>
|
||||
@@ -1153,11 +1154,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;
|
||||
|
||||
@@ -1174,11 +1173,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;
|
||||
}
|
||||
|
||||
10
include/fs.h
10
include/fs.h
@@ -359,15 +359,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 */
|
||||
|
||||
Reference in New Issue
Block a user