summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorRoger Knecht <rknecht@pm.me>2022-09-03 13:15:04 +0000
committerTom Rini <trini@konsulko.com>2022-10-11 15:40:48 -0400
commit23c0df6e7cd3f220a81151b662f0280bba0054b9 (patch)
tree15bac492af33d58a7b244c914cd1714a7ffc3964 /cmd
parent690a1d694899c55c7cc6a168c54984f765ff761e (diff)
downloadu-boot-23c0df6e7cd3f220a81151b662f0280bba0054b9.tar.gz
cmd: xxd: add new command
Add xxd command to print file content as hexdump 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/Kconfig5
-rw-r--r--cmd/Makefile1
-rw-r--r--cmd/xxd.c85
3 files changed, 91 insertions, 0 deletions
diff --git a/cmd/Kconfig b/cmd/Kconfig
index 3267811b25..8eeb7ea081 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -469,6 +469,11 @@ config CMD_XIMG
help
Extract a part of a multi-image.
+config CMD_XXD
+ bool "xxd"
+ help
+ Print file as hexdump to standard output
+
config CMD_SPL
bool "spl export - Export boot information for Falcon boot"
depends on SPL
diff --git a/cmd/Makefile b/cmd/Makefile
index 4bd52bb257..d9bbd0b9fd 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -185,6 +185,7 @@ obj-$(CONFIG_CMD_USB_SDP) += usb_gadget_sdp.o
obj-$(CONFIG_CMD_THOR_DOWNLOAD) += thordown.o
obj-$(CONFIG_CMD_VBE) += vbe.o
obj-$(CONFIG_CMD_XIMG) += ximg.o
+obj-$(CONFIG_CMD_XXD) += xxd.o
obj-$(CONFIG_CMD_YAFFS2) += yaffs2.o
obj-$(CONFIG_CMD_SPL) += spl.o
obj-$(CONFIG_CMD_W1) += w1.o
diff --git a/cmd/xxd.c b/cmd/xxd.c
new file mode 100644
index 0000000000..742a85c7a9
--- /dev/null
+++ b/cmd/xxd.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 <display_options.h>
+#include <fs.h>
+#include <malloc.h>
+#include <mapmem.h>
+
+static int do_xxd(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);
+ 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
+ print_buffer(0, buffer, sizeof(char), file_size, 0);
+
+ free(buffer);
+
+ return 0;
+}
+
+#ifdef CONFIG_SYS_LONGHELP
+static char xxd_help_text[] =
+ "<interface> <dev[:part]> <file>\n"
+ " - Print file from 'dev' on 'interface' as hexdump to standard output\n";
+#endif
+
+U_BOOT_CMD(xxd, 4, 1, do_xxd,
+ "Print file as hexdump to standard output",
+ xxd_help_text
+);