Re: [PATCH v2] cmd: xxd: add new command

2022-10-11 Thread Tom Rini
On Sat, Sep 03, 2022 at 01:15:04PM +, Roger Knecht wrote:

> Add xxd command to print file content as hexdump to standard out
> 
> Reviewed-by: Simon Glass 
> Signed-off-by: Roger Knecht 

Applied to u-boot/master, thanks!

-- 
Tom


signature.asc
Description: PGP signature


[PATCH v2] cmd: xxd: add new command

2022-09-03 Thread Roger Knecht
Add xxd command to print file content as hexdump to standard out

Reviewed-by: Simon Glass 
Signed-off-by: Roger Knecht 
---
v2:
 - Fix pylint error "Undefined variable CalledProcessError"
 - Fix htmldoc error "cat.rst: document isn't included in any toctree"
 - Added reviewed by Simon

The 'xxd' code has a lot in common with my ealier patch for 'cat'.

Example:
```
=> xxd mmc 0:1 hello
: 68 65 6c 6c 6f 20 77 6f 72 6c 64 0a 00 01 02 03  hello world.
0010: 04 05
```

 MAINTAINERS|  7 +++
 cmd/Kconfig|  5 ++
 cmd/Makefile   |  1 +
 cmd/xxd.c  | 85 ++
 configs/sandbox64_defconfig|  1 +
 configs/sandbox_defconfig  |  1 +
 doc/usage/cmd/xxd.rst  | 50 ++
 doc/usage/index.rst|  1 +
 test/py/tests/test_xxd/conftest.py | 35 
 test/py/tests/test_xxd/test_xxd.py | 23 
 10 files changed, 209 insertions(+)
 create mode 100644 cmd/xxd.c
 create mode 100644 doc/usage/cmd/xxd.rst
 create mode 100644 test/py/tests/test_xxd/conftest.py
 create mode 100644 test/py/tests/test_xxd/test_xxd.py

diff --git a/MAINTAINERS b/MAINTAINERS
index 36a2b69fcb..467c76f854 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -1486,6 +1486,13 @@ M:   Max Filippov 
 S: Maintained
 F: arch/xtensa/

+XXD
+M: Roger Knecht 
+S: Maintained
+F: cmd/xxd.c
+F: doc/usage/cmd/xxd.rst
+F: test/py/tests/test_xxd/
+
 THE REST
 M: Tom Rini 
 L: u-boot@lists.denx.de
diff --git a/cmd/Kconfig b/cmd/Kconfig
index 8ea064b8d2..4a68c41594 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 6e87522b62..48a2ee2e6e 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -181,6 +181,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 00..742a85c7a9
--- /dev/null
+++ b/cmd/xxd.c
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2022
+ * Roger Knecht 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+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, _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, _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[] =
+   "  \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
+);
diff --git a/configs/sandbox64_defconfig b/configs/sandbox64_defconfig
index 290d1506c2..6f8574e0c2 100644
--- a/configs/sandbox64_defconfig
+++ b/configs/sandbox64_defconfig
@@ -54,6 +54,7 @@ CONFIG_CMD_READ=y
 CONFIG_CMD_REMOTEPROC=y
 CONFIG_CMD_SPI=y