Skip to contentSkip to conversation

cmd: xxd: add new command

PATCH In review Roger Knecht 2 participants 2 messages Sep 3, 2022 – Oct 11, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cmd: xxd: add new command

Add xxd command to print file content as hexdump to standard out

---
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
00000000: 68 65 6c 6c 6f 20 77 6f 72 6c 64 0a 00 01 02 03  hello world.....
00000010: 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
MAINTAINERS +7 -0
index 36a2b69fcb..467c76f854 100644
@@ -1486,6 +1486,13 @@ M:	Max Filippov <[email protected]>
 S:	Maintained
 F:	arch/xtensa/
 
+XXD
+M:	Roger Knecht <[email protected]>
+S:	Maintained
+F:	cmd/xxd.c
+F:	doc/usage/cmd/xxd.rst
+F:	test/py/tests/test_xxd/
+
 THE REST
 M:	Tom Rini <[email protected]>
 L:	[email protected]
cmd/Kconfig +5 -0
index 8ea064b8d2..4a68c41594 100644
@@ -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
cmd/Makefile +1 -0
index 6e87522b62..48a2ee2e6e 100644
@@ -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
cmd/xxd.c new file +85 -0

85 lines not on this page · raw message

configs/sandbox64_defconfig +1 -0
index 290d1506c2..6f8574e0c2 100644
@@ -54,6 +54,7 @@ CONFIG_CMD_READ=y
 CONFIG_CMD_REMOTEPROC=y
 CONFIG_CMD_SPI=y
 CONFIG_CMD_USB=y
+CONFIG_CMD_XXD=y
 CONFIG_BOOTP_DNS2=y
 CONFIG_CMD_TFTPPUT=y
 CONFIG_CMD_TFTPSRV=y
configs/sandbox_defconfig +1 -0
index ab5d3f19bf..2fa6896c4b 100644
@@ -60,6 +60,7 @@ CONFIG_CMD_MEM_SEARCH=y
 CONFIG_CMD_MX_CYCLIC=y
 CONFIG_CMD_MEMTEST=y
 CONFIG_CMD_UNZIP=y
+CONFIG_CMD_XXD=y
 CONFIG_CMD_BIND=y
 CONFIG_CMD_DEMO=y
 CONFIG_CMD_GPIO=y
doc/usage/cmd/xxd.rst new file +50 -0

50 lines not on this page · raw message

doc/usage/index.rst +1 -0
index 28f9683a3e..08297415c8 100644
@@ -66,6 +66,7 @@ Shell commands
    cmd/true
    cmd/ums
    cmd/wdt
+   cmd/xxd
 
 Booting OS
 ----------
test/py/tests/test_xxd/conftest.py new file +35 -0
new file mode 100644
index 0000000000..59285aadf4
@@ -0,0 +1,35 @@
+# SPDX-License-Identifier:      GPL-2.0+
+
+"""Fixture for xxd command test
+"""
+
+import os
+import shutil
+from subprocess import check_call, CalledProcessError
+import pytest
+
[email protected](scope='session')
+def xxd_data(u_boot_config):
+    """Set up a file system to be used in xxd tests
+
+    Args:
+        u_boot_config -- U-boot configuration.
+    """
+    mnt_point = u_boot_config.persistent_data_dir + '/test_xxd'
+    image_path = u_boot_config.persistent_data_dir + '/xxd.img'
+
+    try:
+        os.mkdir(mnt_point, mode = 0o755)
+
+        with open(mnt_point + '/hello', 'w', encoding = 'ascii') as file:
+            file.write('hello world\n\x00\x01\x02\x03\x04\x05')
+
+        check_call(f'virt-make-fs --partition=gpt --size=+1M --type=vfat {mnt_point} {image_path}',
+                   shell=True)
+
+        yield image_path
+    except CalledProcessError:
+        pytest.skip('Setup failed')
+    finally:
+        shutil.rmtree(mnt_point)
+        os.remove(image_path)
test/py/tests/test_xxd/test_xxd.py new file +23 -0
new file mode 100644
index 0000000000..06b9cfc000
@@ -0,0 +1,23 @@
+# SPDX-License-Identifier:      GPL-2.0+
+
+""" Unit test for xxd command
+"""
+
+import pytest
+
[email protected]('sandbox')
[email protected]('cmd_xxd')
+def test_xxd(u_boot_console, xxd_data):
+    """ Unit test for xxd
+
+    Args:
+        u_boot_console -- U-Boot console
+        xxd_data -- Path to the disk image used for testing.
+    """
+    response = u_boot_console.run_command_list([
+        f'host bind 0 {xxd_data}',
+        'xxd host 0 hello'])
+
+    assert '00000000: 68 65 6c 6c 6f 20 77 6f 72 6c 64 0a 00 01 02 03  hello world.....\r\r\n' + \
+           '00000010: 04 05                                            ..' \
+           in response

--
2.25.1