diff options
author | Roger Knecht <rknecht@pm.me> | 2022-09-03 11:34:53 +0000 |
---|---|---|
committer | Tom Rini <trini@konsulko.com> | 2022-10-11 15:40:48 -0400 |
commit | 690a1d694899c55c7cc6a168c54984f765ff761e (patch) | |
tree | 0d1949060afcdda676a4a8e7bfc89053a391f1a4 /cmd | |
parent | 7eda1a95331d1ccd17e5af6fb18bf5577f81a451 (diff) | |
download | u-boot-690a1d694899c55c7cc6a168c54984f765ff761e.zip u-boot-690a1d694899c55c7cc6a168c54984f765ff761e.tar.gz u-boot-690a1d694899c55c7cc6a168c54984f765ff761e.tar.bz2 |
cmd: cat: add new command
Add cat command to print file content to standard out
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Roger Knecht <rknecht@pm.me>
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/Kconfig | 5 | ||||
-rw-r--r-- | cmd/Makefile | 1 | ||||
-rw-r--r-- | cmd/cat.c | 85 |
3 files changed, 91 insertions, 0 deletions
diff --git a/cmd/Kconfig b/cmd/Kconfig index 6f00bd9..3267811 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -1533,6 +1533,11 @@ endmenu menu "Shell scripting commands" +config CMD_CAT + bool "cat" + help + Print file to standard output + config CMD_ECHO bool "echo" default y diff --git a/cmd/Makefile b/cmd/Makefile index cf6ce1b..4bd52bb 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -38,6 +38,7 @@ obj-$(CONFIG_CMD_BOOTZ) += bootz.o obj-$(CONFIG_CMD_BOOTI) += booti.o obj-$(CONFIG_CMD_BTRFS) += btrfs.o obj-$(CONFIG_CMD_BUTTON) += button.o +obj-$(CONFIG_CMD_CAT) += cat.o obj-$(CONFIG_CMD_CACHE) += cache.o obj-$(CONFIG_CMD_CBFS) += cbfs.o obj-$(CONFIG_CMD_CLK) += clk.o diff --git a/cmd/cat.c b/cmd/cat.c new file mode 100644 index 0000000..1273a26 --- /dev/null +++ b/cmd/cat.c @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2022 + * Roger Knecht <rknecht@pm.de> + */ + +#include <common.h> +#include <command.h> +#include <fs.h> +#include <malloc.h> +#include <mapmem.h> + +static int do_cat(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + char *ifname; + char *dev; + char *file; + char *buffer; + phys_addr_t addr; + loff_t file_size; + + if (argc < 4) + return CMD_RET_USAGE; + + ifname = argv[1]; + dev = argv[2]; + file = argv[3]; + + // check file exists + if (fs_set_blk_dev(ifname, dev, FS_TYPE_ANY)) + return CMD_RET_FAILURE; + + if (!fs_exists(file)) { + log_err("File does not exist: ifname=%s dev=%s file=%s\n", ifname, dev, file); + return CMD_RET_FAILURE; + } + + // get file size + if (fs_set_blk_dev(ifname, dev, FS_TYPE_ANY)) + return CMD_RET_FAILURE; + + if (fs_size(file, &file_size)) { + log_err("Cannot read file size: ifname=%s dev=%s file=%s\n", ifname, dev, file); + return CMD_RET_FAILURE; + } + + // allocate memory for file content + buffer = calloc(sizeof(char), file_size + 1); + if (!buffer) { + log_err("Out of memory\n"); + return CMD_RET_FAILURE; + } + + // map pointer to system memory + addr = map_to_sysmem(buffer); + + // read file to memory + if (fs_set_blk_dev(ifname, dev, FS_TYPE_ANY)) + return CMD_RET_FAILURE; + + if (fs_read(file, addr, 0, 0, &file_size)) { + log_err("Cannot read file: ifname=%s dev=%s file=%s\n", ifname, dev, file); + return CMD_RET_FAILURE; + } + + // print file content + buffer[file_size] = '\0'; + puts(buffer); + + free(buffer); + + return 0; +} + +#ifdef CONFIG_SYS_LONGHELP +static char cat_help_text[] = + "<interface> <dev[:part]> <file>\n" + " - Print file from 'dev' on 'interface' to standard output\n"; +#endif + +U_BOOT_CMD(cat, 4, 1, do_cat, + "Print file to standard output", + cat_help_text +); |