Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4FF89C00140 for ; Wed, 24 Aug 2022 11:20:09 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id DC28B8423A; Wed, 24 Aug 2022 13:20:06 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; secure) header.d=pm.me header.i=@pm.me header.b="KKA2h+24"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B98E884194; Wed, 24 Aug 2022 13:20:05 +0200 (CEST) Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 5CD6F84194 for ; Wed, 24 Aug 2022 13:20:02 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=pm.me Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=rknecht@pm.me Date: Wed, 24 Aug 2022 11:19:46 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1661339998; x=1661599198; bh=SI9X7LdjCQFW0IIQ+Zg1YhQXCYAvKNlKWr0KfGoQKWI=; h=Date:To:From:Cc:Reply-To:Subject:Message-ID:Feedback-ID:From:To: Cc:Date:Subject:Reply-To:Feedback-ID:Message-ID; b=KKA2h+24JtwCT0IS+5u1Swx7TzAeGNcpgObZTu+lU+LeIAEpwluK41Oa8W0eBAoMi uMs5tWZ6q5XNCKPAAo+p7zwvssMkb1gIeXpQuyWaEc0tGRPL2mRC1VT8l306fcWM6U 2Y4nRg2KNJfyteYTuR7WWgbGKtmLXbFmbTb3Huvqa3qBhbNySCihGXgpvifd1KIUmI 8TKzLv6wqb1caOEGoqyx1pGDVup7QqjMPNVSe/1vkHVYuSjF+jxhRH3YOj4D62cCOr UGsktNUs6NQ0pbHRDCIh9gbfMQ1k1jyeReV9MfCiUICsDqkNKj8yJwIIgPVvnyg3MO Wx1lZuaIN1asA== To: u-boot@lists.denx.de From: Roger Knecht Cc: Tom Rini , Heinrich Schuchardt , Simon Glass , Roger Knecht Subject: [PATCH] cmd: xxd: add new command Message-ID: <20220824111857.382-1-rknecht@pm.me> Feedback-ID: 8957232:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Roger Knecht Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Add xxd command to print file content as hexdump to standard out Signed-off-by: Roger Knecht --- The 'xxd' code has a lot in common with my ealier patch for 'cat'. Example: ``` =3D> 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 ++++++++++++++++++ test/py/tests/test_xxd/conftest.py | 35 ++++++++++++ test/py/tests/test_xxd/test_xxd.py | 23 ++++++++ 9 files changed, 208 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:=09Max Filippov S:=09Maintained F:=09arch/xtensa/ +XXD +M:=09Roger Knecht +S:=09Maintained +F:=09cmd/xxd.c +F:=09doc/usage/cmd/xxd.rst +F:=09test/py/tests/test_xxd/ + THE REST M:=09Tom Rini L:=09u-boot@lists.denx.de diff --git a/cmd/Kconfig b/cmd/Kconfig index 211ebe9c87..f7bbcef9e2 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -469,6 +469,11 @@ config CMD_XIMG =09help =09 Extract a part of a multi-image. +config CMD_XXD +=09bool "xxd" +=09help +=09 Print file as hexdump to standard output + config CMD_SPL =09bool "spl export - Export boot information for Falcon boot" =09depends 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) +=3D usb_gadget_sdp.o obj-$(CONFIG_CMD_THOR_DOWNLOAD) +=3D thordown.o obj-$(CONFIG_CMD_VBE) +=3D vbe.o obj-$(CONFIG_CMD_XIMG) +=3D ximg.o +obj-$(CONFIG_CMD_XXD) +=3D xxd.o obj-$(CONFIG_CMD_YAFFS2) +=3D yaffs2.o obj-$(CONFIG_CMD_SPL) +=3D spl.o obj-$(CONFIG_CMD_W1) +=3D 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 + */ + +#include +#include +#include +#include +#include +#include + +static int do_xxd(struct cmd_tbl *cmdtp, int flag, int argc, +=09=09 char *const argv[]) +{ +=09char *ifname; +=09char *dev; +=09char *file; +=09char *buffer; +=09phys_addr_t addr; +=09loff_t file_size; + +=09if (argc < 4) +=09=09return CMD_RET_USAGE; + +=09ifname =3D argv[1]; +=09dev =3D argv[2]; +=09file =3D argv[3]; + +=09// check file exists +=09if (fs_set_blk_dev(ifname, dev, FS_TYPE_ANY)) +=09=09return CMD_RET_FAILURE; + +=09if (!fs_exists(file)) { +=09=09log_err("File does not exist: ifname=3D%s dev=3D%s file=3D%s\n", ifn= ame, dev, file); +=09=09return CMD_RET_FAILURE; +=09} + +=09// get file size +=09if (fs_set_blk_dev(ifname, dev, FS_TYPE_ANY)) +=09=09return CMD_RET_FAILURE; + +=09if (fs_size(file, &file_size)) { +=09=09log_err("Cannot read file size: ifname=3D%s dev=3D%s file=3D%s\n", i= fname, dev, file); +=09=09return CMD_RET_FAILURE; +=09} + +=09// allocate memory for file content +=09buffer =3D calloc(sizeof(char), file_size); +=09if (!buffer) { +=09=09log_err("Out of memory\n"); +=09=09return CMD_RET_FAILURE; +=09} + +=09// map pointer to system memory +=09addr =3D map_to_sysmem(buffer); + +=09// read file to memory +=09if (fs_set_blk_dev(ifname, dev, FS_TYPE_ANY)) +=09=09return CMD_RET_FAILURE; + +=09if (fs_read(file, addr, 0, 0, &file_size)) { +=09=09log_err("Cannot read file: ifname=3D%s dev=3D%s file=3D%s\n", ifname= , dev, file); +=09=09return CMD_RET_FAILURE; +=09} + +=09// print file content +=09print_buffer(0, buffer, sizeof(char), file_size, 0); + +=09free(buffer); + +=09return 0; +} + +#ifdef CONFIG_SYS_LONGHELP +static char xxd_help_text[] =3D +=09" \n" +=09" - Print file from 'dev' on 'interface' as hexdump to standard output= \n"; +#endif + +U_BOOT_CMD(xxd, 4, 1, do_xxd, +=09 "Print file as hexdump to standard output", +=09 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=3Dy CONFIG_CMD_REMOTEPROC=3Dy CONFIG_CMD_SPI=3Dy CONFIG_CMD_USB=3Dy +CONFIG_CMD_XXD=3Dy CONFIG_BOOTP_DNS2=3Dy CONFIG_CMD_TFTPPUT=3Dy CONFIG_CMD_TFTPSRV=3Dy diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig index ab5d3f19bf..2fa6896c4b 100644 --- a/configs/sandbox_defconfig +++ b/configs/sandbox_defconfig @@ -60,6 +60,7 @@ CONFIG_CMD_MEM_SEARCH=3Dy CONFIG_CMD_MX_CYCLIC=3Dy CONFIG_CMD_MEMTEST=3Dy CONFIG_CMD_UNZIP=3Dy +CONFIG_CMD_XXD=3Dy CONFIG_CMD_BIND=3Dy CONFIG_CMD_DEMO=3Dy CONFIG_CMD_GPIO=3Dy diff --git a/doc/usage/cmd/xxd.rst b/doc/usage/cmd/xxd.rst new file mode 100644 index 0000000000..0de1223dce --- /dev/null +++ b/doc/usage/cmd/xxd.rst @@ -0,0 +1,50 @@ +.. SPDX-License-Identifier: GPL-2.0+: + +xxd command +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Synopsis +-------- + +:: + + xxd + +Description +----------- + +The xxd command prints the file content as hexdump to standard out. + +interface + interface for accessing the block device (mmc, sata, scsi, usb, ....) + +dev + device number + +part + partition number, defaults to 1 + +file + path to file + +Example +------- + +Here is the output for a example text file: + +:: + + =3D> 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 .. + =3D> + +Configuration +------------- + +The xxd command is only available if CONFIG_CMD_XXD=3Dy. + +Return value +------------ + +The return value $? is set to 0 (true) if the file is readable, otherwise = it returns a non-zero error code. diff --git a/test/py/tests/test_xxd/conftest.py b/test/py/tests/test_xxd/co= nftest.py new file mode 100644 index 0000000000..6590989bcf --- /dev/null +++ b/test/py/tests/test_xxd/conftest.py @@ -0,0 +1,35 @@ +# SPDX-License-Identifier: GPL-2.0+ + +"""Fixture for xxd command test +""" + +import os +import shutil +from subprocess import check_call +import pytest + +@pytest.fixture(scope=3D'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 =3D u_boot_config.persistent_data_dir + '/test_xxd' + image_path =3D u_boot_config.persistent_data_dir + '/xxd.img' + + try: + os.mkdir(mnt_point, mode =3D 0o755) + + with open(mnt_point + '/hello', 'w', encoding =3D 'ascii') as file= : + file.write('hello world\n\x00\x01\x02\x03\x04\x05') + + check_call(f'virt-make-fs --partition=3Dgpt --size=3D+1M --type=3D= vfat {mnt_point} {image_path}', + shell=3DTrue) + + yield image_path + except CalledProcessError: + pytest.skip('Setup failed') + finally: + shutil.rmtree(mnt_point) + os.remove(image_path) diff --git a/test/py/tests/test_xxd/test_xxd.py b/test/py/tests/test_xxd/te= st_xxd.py new file mode 100644 index 0000000000..06b9cfc000 --- /dev/null +++ b/test/py/tests/test_xxd/test_xxd.py @@ -0,0 +1,23 @@ +# SPDX-License-Identifier: GPL-2.0+ + +""" Unit test for xxd command +""" + +import pytest + +@pytest.mark.boardspec('sandbox') +@pytest.mark.buildconfigspec('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 =3D 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 hel= lo world.....\r\r\n' + \ + '00000010: 04 05 ..'= \ + in response -- 2.25.1