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 6D387C00140 for ; Sun, 21 Aug 2022 15:06:24 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 451B3844D6; Sun, 21 Aug 2022 17:05:56 +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="a6vNiDze"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 33D3983FD9; Sun, 21 Aug 2022 16:54:25 +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 4C44984125 for ; Sun, 21 Aug 2022 16:54:22 +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: Sun, 21 Aug 2022 14:54:13 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pm.me; s=protonmail3; t=1661093658; x=1661352858; bh=iqMQtkRG6bXk+DMGv6YUGjrCEHUHeZQhl60yH3MNIvA=; h=Date:To:From:Cc:Reply-To:Subject:Message-ID:Feedback-ID:From:To: Cc:Date:Subject:Reply-To:Feedback-ID:Message-ID; b=a6vNiDzer5G6FYHDi/ljalEpnmBa5ZGbYgEo24UFRcxxXymgZNU5YX626wf22LFut t9LC9ZWS4q5gL6uw6DEir0jeoU3cxa0RIgtRkJ0GimK2DNSJ01LBA287D2qMsAufJ4 WKx9JylKHr8h4Sb7afSBtAIaS+rEoxx46kRF7TdlCP+5Vw9XpVsdGdfGfucO9d/E18 PHIXyGX+4aUBIEF/lyFzC5jVvgrnEgZbZ9sXeZcJPVf2ZhKQf8tLwpS8hUHrpX8uPN Mtaug+2GtyJ7JQP/WnEB3Ka32reaP2szJzrOzpmepmgHwG3jg7ZNpmt+NcC8dMI/Jq jn/ZP4oVVdQvw== To: u-boot@lists.denx.de From: Roger Knecht Cc: Tom Rini , Heinrich Schuchardt , Simon Glass , Roger Knecht Subject: [PATCH v5] cmd: cat: add new command Message-ID: <20220821145339.324-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-Mailman-Approved-At: Sun, 21 Aug 2022 17:05:45 +0200 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 cat command to print file content to standard out Reviewed-by: Simon Glass Signed-off-by: Roger Knecht --- v5: - Removed "default n" from Kconfig - Renamed 'buffer_sysmem_addr' in 'addr' - User yield instead of return in conftest.py - Catch 'CalledProcessError' in conftest.py - Clean up files after test execution - Use u_boot_console.run_command_list() instead of u_boot_console.run_comm= and() - Removed unnecessary unmap_sysmem() call - Added Reviewed-by Simon v4: - Return only values from enum command_ret_t in do_cat() - Use calloc() instead of malloc() for zero initialized memory - Make use of CONFIG_SYS_LONGHELP - Improved error messages v3: - Disable 'cat' by default (CONFIG_CMD_CAT=3Dn) - Enable 'cat' in sandbox and sandbox64 defconfig - Use map_to_sysmem() to fix "phys_to_virt: Cannot map sandbox address" - Use puts() instead of a loop - Added python test - Added usage documentation v2: - Moved cat from boot to shell commands - Added MAINTAINERS entry - Added comments - Improved variable naming MAINTAINERS | 5 ++ cmd/Kconfig | 5 ++ cmd/Makefile | 1 + cmd/cat.c | 85 ++++++++++++++++++++++++++++++ configs/sandbox64_defconfig | 1 + configs/sandbox_defconfig | 1 + doc/usage/cmd/cat.rst | 49 +++++++++++++++++ test/py/tests/test_cat/conftest.py | 35 ++++++++++++ test/py/tests/test_cat/test_cat.py | 20 +++++++ 9 files changed, 202 insertions(+) create mode 100644 cmd/cat.c create mode 100644 doc/usage/cmd/cat.rst create mode 100644 test/py/tests/test_cat/conftest.py create mode 100644 test/py/tests/test_cat/test_cat.py diff --git a/MAINTAINERS b/MAINTAINERS index 5857fbf398..2864f84274 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -765,6 +765,11 @@ M:=09Simon Glass S:=09Maintained F:=09tools/buildman/ +CAT +M:=09Roger Knecht +S:=09Maintained +F:=09cmd/cat.c + CFI FLASH M:=09Stefan Roese S:=09Maintained diff --git a/cmd/Kconfig b/cmd/Kconfig index 211ebe9c87..18affe45cb 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -1531,6 +1531,11 @@ endmenu menu "Shell scripting commands" +config CMD_CAT +=09bool "cat" +=09help +=09 Print file to standard output + config CMD_ECHO =09bool "echo" =09default y diff --git a/cmd/Makefile b/cmd/Makefile index 6e87522b62..1d2590e958 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -38,6 +38,7 @@ obj-$(CONFIG_CMD_BOOTZ) +=3D bootz.o obj-$(CONFIG_CMD_BOOTI) +=3D booti.o obj-$(CONFIG_CMD_BTRFS) +=3D btrfs.o obj-$(CONFIG_CMD_BUTTON) +=3D button.o +obj-$(CONFIG_CMD_CAT) +=3D cat.o obj-$(CONFIG_CMD_CACHE) +=3D cache.o obj-$(CONFIG_CMD_CBFS) +=3D cbfs.o obj-$(CONFIG_CMD_CLK) +=3D clk.o diff --git a/cmd/cat.c b/cmd/cat.c new file mode 100644 index 0000000000..1273a26b14 --- /dev/null +++ b/cmd/cat.c @@ -0,0 +1,85 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2022 + * Roger Knecht + */ + +#include +#include +#include +#include +#include + +static int do_cat(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 + 1); +=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 +=09buffer[file_size] =3D '\0'; +=09puts(buffer); + +=09free(buffer); + +=09return 0; +} + +#ifdef CONFIG_SYS_LONGHELP +static char cat_help_text[] =3D +=09" \n" +=09" - Print file from 'dev' on 'interface' to standard output\n"; +#endif + +U_BOOT_CMD(cat, 4, 1, do_cat, +=09 "Print file to standard output", +=09 cat_help_text +); diff --git a/configs/sandbox64_defconfig b/configs/sandbox64_defconfig index 6553568e76..b2c9f19f11 100644 --- a/configs/sandbox64_defconfig +++ b/configs/sandbox64_defconfig @@ -22,6 +22,7 @@ CONFIG_CONSOLE_RECORD=3Dy CONFIG_CONSOLE_RECORD_OUT_SIZE=3D0x1000 CONFIG_PRE_CONSOLE_BUFFER=3Dy CONFIG_DISPLAY_BOARDINFO_LATE=3Dy +CONFIG_CMD_CAT=3Dy CONFIG_CMD_CPU=3Dy CONFIG_CMD_LICENSE=3Dy CONFIG_CMD_BOOTZ=3Dy diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig index eba7bcbb48..2136c76fe3 100644 --- a/configs/sandbox_defconfig +++ b/configs/sandbox_defconfig @@ -36,6 +36,7 @@ CONFIG_LOG_DEFAULT_LEVEL=3D6 CONFIG_DISPLAY_BOARDINFO_LATE=3Dy CONFIG_STACKPROTECTOR=3Dy CONFIG_ANDROID_AB=3Dy +CONFIG_CMD_CAT=3Dy CONFIG_CMD_CPU=3Dy CONFIG_CMD_LICENSE=3Dy CONFIG_CMD_BOOTM_PRE_LOAD=3Dy diff --git a/doc/usage/cmd/cat.rst b/doc/usage/cmd/cat.rst new file mode 100644 index 0000000000..5ef4731fe3 --- /dev/null +++ b/doc/usage/cmd/cat.rst @@ -0,0 +1,49 @@ +.. SPDX-License-Identifier: GPL-2.0+: + +cat command +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Synopsis +-------- + +:: + + cat + +Description +----------- + +The cat command prints the file content 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> cat mmc 0:1 hello + hello world + =3D> + +Configuration +------------- + +The cat command is only available if CONFIG_CMD_CAT=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_cat/conftest.py b/test/py/tests/test_cat/co= nftest.py new file mode 100644 index 0000000000..acb1488208 --- /dev/null +++ b/test/py/tests/test_cat/conftest.py @@ -0,0 +1,35 @@ +# SPDX-License-Identifier: GPL-2.0+ + +"""Fixture for cat command test +""" + +import os +import shutil +from subprocess import check_call +import pytest + +@pytest.fixture(scope=3D'session') +def cat_data(u_boot_config): + """Set up a file system to be used in cat tests + + Args: + u_boot_config -- U-boot configuration. + """ + mnt_point =3D u_boot_config.persistent_data_dir + '/test_cat' + image_path =3D u_boot_config.persistent_data_dir + '/cat.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') + + 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_cat/test_cat.py b/test/py/tests/test_cat/te= st_cat.py new file mode 100644 index 0000000000..132527bd4c --- /dev/null +++ b/test/py/tests/test_cat/test_cat.py @@ -0,0 +1,20 @@ +# SPDX-License-Identifier: GPL-2.0+ + +""" Unit test for cat command +""" + +import pytest + +@pytest.mark.boardspec('sandbox') +@pytest.mark.buildconfigspec('cmd_cat') +def test_cat(u_boot_console, cat_data): + """ Unit test for cat + + Args: + u_boot_console -- U-Boot console + cat_data -- Path to the disk image used for testing. + """ + response =3D u_boot_console.run_command_list([ + f'host bind 0 {cat_data}', + 'cat host 0 hello']) + assert 'hello world' in response -- 2.25.1