Skip to contentSkip to conversation

cmd: cat: add new command

PATCH Superseded Roger Knecht 3 participants 6 messages Aug 18, 2022 – Aug 21, 2022

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 cmd: cat: add new command

Add cat command to print file content to standard out

---
v3:
- Disable 'cat' by default (CONFIG_CMD_CAT=n)
- 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
- Addd usage documentation

v2:
- Moved cat from boot to shell commands
- Added MAINTAINERS entry
- Added comments
- Improved variable naming

 MAINTAINERS                        |  5 +++
 cmd/Kconfig                        |  6 +++
 cmd/Makefile                       |  1 +
 cmd/cat.c                          | 67 ++++++++++++++++++++++++++++++
 configs/sandbox64_defconfig        |  1 +
 configs/sandbox_defconfig          |  1 +
 doc/usage/cmd/cat.rst              | 49 ++++++++++++++++++++++
 test/py/tests/test_cat/conftest.py | 33 +++++++++++++++
 test/py/tests/test_cat/test_cat.py | 22 ++++++++++
 9 files changed, 185 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
MAINTAINERS +5 -0
index 5857fbf398..2864f84274 100644
@@ -765,6 +765,11 @@ M:	Simon Glass <[email protected]>
 S:	Maintained
 F:	tools/buildman/
 
+CAT
+M:	Roger Knecht <[email protected]>
+S:	Maintained
+F:	cmd/cat.c
+
 CFI FLASH
 M:	Stefan Roese <[email protected]>
 S:	Maintained
cmd/Kconfig +6 -0
index 211ebe9c87..ce7e876475 100644
@@ -1531,6 +1531,12 @@ endmenu
 
 menu "Shell scripting commands"
 
+config CMD_CAT
+	bool "cat"
+	default n
+	help
+	  Print file to standard output
+
 config CMD_ECHO
 	bool "echo"
 	default y
cmd/Makefile +1 -0
index 6e87522b62..1d2590e958 100644
@@ -38,6 +38,7 @@ obj-$(CONFIG_CMD_BOOTZ) += bootz.o
 obj-$(CONFIG_CMD_BOOTI) += booti.o
 obj-$(CONFIG_CMD_BTRFS) += btrfs.o
 obj-$(CONFIG_CMD_BUTTON) += button.o
+obj-$(CONFIG_CMD_CAT) += cat.o
 obj-$(CONFIG_CMD_CACHE) += cache.o
 obj-$(CONFIG_CMD_CBFS) += cbfs.o
 obj-$(CONFIG_CMD_CLK) += clk.o
cmd/cat.c new file +67 -0

67 lines not on this page · raw message

configs/sandbox64_defconfig +1 -0
index 6553568e76..b2c9f19f11 100644
@@ -22,6 +22,7 @@ CONFIG_CONSOLE_RECORD=y
 CONFIG_CONSOLE_RECORD_OUT_SIZE=0x1000
 CONFIG_PRE_CONSOLE_BUFFER=y
 CONFIG_DISPLAY_BOARDINFO_LATE=y
+CONFIG_CMD_CAT=y
 CONFIG_CMD_CPU=y
 CONFIG_CMD_LICENSE=y
 CONFIG_CMD_BOOTZ=y
configs/sandbox_defconfig +1 -0
index eba7bcbb48..2136c76fe3 100644
@@ -36,6 +36,7 @@ CONFIG_LOG_DEFAULT_LEVEL=6
 CONFIG_DISPLAY_BOARDINFO_LATE=y
 CONFIG_STACKPROTECTOR=y
 CONFIG_ANDROID_AB=y
+CONFIG_CMD_CAT=y
 CONFIG_CMD_CPU=y
 CONFIG_CMD_LICENSE=y
 CONFIG_CMD_BOOTM_PRE_LOAD=y
doc/usage/cmd/cat.rst new file +49 -0

49 lines not on this page · raw message

test/py/tests/test_cat/conftest.py new file +33 -0
new file mode 100644
index 0000000000..426f64485f
@@ -0,0 +1,33 @@
+# SPDX-License-Identifier:      GPL-2.0+
+
+"""Fixture for cat command test
+"""
+
+import os
+import shutil
+from subprocess import check_call
+import pytest
+
[email protected](scope='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.
+
+    Return:
+        A path to disk image to be used for testing
+    """
+    mnt_point = u_boot_config.persistent_data_dir + '/test_cat'
+    image_path = u_boot_config.persistent_data_dir + '/cat.img'
+
+    shutil.rmtree(mnt_point, ignore_errors=True)
+    os.mkdir(mnt_point, mode = 0o755)
+
+    with open(mnt_point + '/hello', 'w', encoding = 'ascii') as file:
+        file.write('hello world\n')
+
+    check_call(f'virt-make-fs --partition=gpt --size=+1M --type=vfat {mnt_point} {image_path}',
+               shell=True)
+
+    return image_path
test/py/tests/test_cat/test_cat.py new file +22 -0
new file mode 100644
index 0000000000..41ddefa40c
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier:      GPL-2.0+
+
+""" Unit test for cat command
+"""
+
+import pytest
+
[email protected]('sandbox')
[email protected]('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.
+    """
+    u_boot_console.run_command(cmd = f'host bind 0 {cat_data}')
+
+    response = u_boot_console.run_command(cmd = 'cat host 0 hello')
+    assert 'hello world' == response
+
+    u_boot_console.run_command(cmd = 'exit', wait_for_echo=False)

--
2.25.1

#1 lore raw
Quoted 110 lines · Roger Knecht

Add cat command to print file content to standard out

---
v3:
- Disable 'cat' by default (CONFIG_CMD_CAT=n)
- 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
- Addd usage documentation

v2:
- Moved cat from boot to shell commands
- Added MAINTAINERS entry
- Added comments
- Improved variable naming

  MAINTAINERS                        |  5 +++
  cmd/Kconfig                        |  6 +++
  cmd/Makefile                       |  1 +
  cmd/cat.c                          | 67 ++++++++++++++++++++++++++++++
  configs/sandbox64_defconfig        |  1 +
  configs/sandbox_defconfig          |  1 +
  doc/usage/cmd/cat.rst              | 49 ++++++++++++++++++++++
  test/py/tests/test_cat/conftest.py | 33 +++++++++++++++
  test/py/tests/test_cat/test_cat.py | 22 ++++++++++
  9 files changed, 185 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

MAINTAINERS +5 -0
index 5857fbf398..2864f84274 100644
@@ -765,6 +765,11 @@ M:	Simon Glass <[email protected]>
  S:	Maintained
  F:	tools/buildman/
 
+CAT
+M:	Roger Knecht <[email protected]>
+S:	Maintained
+F:	cmd/cat.c
+
  CFI FLASH
  M:	Stefan Roese <[email protected]>
  S:	Maintained
cmd/Kconfig +6 -0
index 211ebe9c87..ce7e876475 100644
@@ -1531,6 +1531,12 @@ endmenu
 
  menu "Shell scripting commands"
 
+config CMD_CAT
+	bool "cat"
+	default n
+	help
+	  Print file to standard output
+
  config CMD_ECHO
  	bool "echo"
  	default y
cmd/Makefile +1 -0
index 6e87522b62..1d2590e958 100644
@@ -38,6 +38,7 @@ obj-$(CONFIG_CMD_BOOTZ) += bootz.o
  obj-$(CONFIG_CMD_BOOTI) += booti.o
  obj-$(CONFIG_CMD_BTRFS) += btrfs.o
  obj-$(CONFIG_CMD_BUTTON) += button.o
+obj-$(CONFIG_CMD_CAT) += cat.o
  obj-$(CONFIG_CMD_CACHE) += cache.o
  obj-$(CONFIG_CMD_CBFS) += cbfs.o
  obj-$(CONFIG_CMD_CLK) += clk.o
cmd/cat.c new file +26 -0
new file mode 100644
index 0000000000..c217617cd6
@@ -0,0 +1,67 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2022
+ * Roger Knecht <[email protected]>
+ */
+
+#include <common.h>
+#include <command.h>
+#include <fs.h>
+#include <malloc.h>
+#include <mapmem.h>
+
+static int do_cat(struct cmd_tbl *cmdtp, int flag, int argc,
+		  char *const argv[])
+{
+	char *buffer;
+	phys_addr_t buffer_sysmem_addr;
+	loff_t file_size;
+	int ret;
+
+	if (argc < 4)
+		return CMD_RET_USAGE;
+
+	// get file size
+	ret = fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY);
+	if (ret)

cmd/cat.c line 36

Quoted 10 lines
cmd/cat.c new file +10 -0
+		return ret;
+
+	ret = fs_size(argv[3], &file_size);
+	if (ret)
+		return ret;
+
+	// allocate memory for file content
+	buffer = malloc(file_size + 1);
+	if (!buffer)
+		return -ENOMEM;

Please, do_cat must only return values from enum command_ret_t.

cmd/cat.c line 38

Quoted 2 lines
cmd/cat.c new file +2 -0
+
+	memset(buffer, 0, file_size + 1);

Our calloc() implementation already sets the buffer to zero. So you can save one function call.

Quoted 17 lines
cmd/cat.c new file +17 -0
+
+	// map pointer to system memory
+	buffer_sysmem_addr = map_to_sysmem(buffer);
+
+	// read file to memory
+	ret = fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY);
+	if (ret)
+		return ret;
+
+	ret = fs_read(argv[3], buffer_sysmem_addr, 0, 0, &file_size);
+	if (ret)
+		return ret;
+
+	// unmap system memory
+	unmap_sysmem(buffer);
+
+	// print file content

cmd/cat.c line 65

Quoted 10 lines
cmd/cat.c new file +10 -0
+	buffer[file_size] = '\0';
+	puts(buffer);
+
+	free(buffer);
+
+	return 0;
+}
+
+U_BOOT_CMD(cat, 4, 1, do_cat,
+	   "print file to standard output",

Please, observe CONFIG_SYS_LONGHELP. Have a look at cmd/bootefi.c or others.

Best regards

Heinrich

Quoted 152 lines
cmd/cat.c new file +2 -0
+	   "<interface> <dev[:part]> <file>\n"
+	   "	- Print file from 'dev' on 'interface' to standard output");
configs/sandbox64_defconfig +1 -0
index 6553568e76..b2c9f19f11 100644
@@ -22,6 +22,7 @@ CONFIG_CONSOLE_RECORD=y
  CONFIG_CONSOLE_RECORD_OUT_SIZE=0x1000
  CONFIG_PRE_CONSOLE_BUFFER=y
  CONFIG_DISPLAY_BOARDINFO_LATE=y
+CONFIG_CMD_CAT=y
  CONFIG_CMD_CPU=y
  CONFIG_CMD_LICENSE=y
  CONFIG_CMD_BOOTZ=y
configs/sandbox_defconfig +1 -0
index eba7bcbb48..2136c76fe3 100644
@@ -36,6 +36,7 @@ CONFIG_LOG_DEFAULT_LEVEL=6
  CONFIG_DISPLAY_BOARDINFO_LATE=y
  CONFIG_STACKPROTECTOR=y
  CONFIG_ANDROID_AB=y
+CONFIG_CMD_CAT=y
  CONFIG_CMD_CPU=y
  CONFIG_CMD_LICENSE=y
  CONFIG_CMD_BOOTM_PRE_LOAD=y
doc/usage/cmd/cat.rst new file +49 -0
new file mode 100644
index 0000000000..5ef4731fe3
@@ -0,0 +1,49 @@
+.. SPDX-License-Identifier: GPL-2.0+:
+
+cat command
+===============
+
+Synopsis
+--------
+
+::
+
+    cat <interface> <dev[:part]> <file>
+
+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:
+
+::
+
+    => cat mmc 0:1 hello
+    hello world
+    =>
+
+Configuration
+-------------
+
+The cat command is only available if CONFIG_CMD_CAT=y.
+
+Return value
+------------
+
+The return value $? is set to 0 (true) if the file is readable, otherwise it returns a non-zero error code.
test/py/tests/test_cat/conftest.py new file +33 -0
new file mode 100644
index 0000000000..426f64485f
@@ -0,0 +1,33 @@
+# SPDX-License-Identifier:      GPL-2.0+
+
+"""Fixture for cat command test
+"""
+
+import os
+import shutil
+from subprocess import check_call
+import pytest
+
[email protected](scope='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.
+
+    Return:
+        A path to disk image to be used for testing
+    """
+    mnt_point = u_boot_config.persistent_data_dir + '/test_cat'
+    image_path = u_boot_config.persistent_data_dir + '/cat.img'
+
+    shutil.rmtree(mnt_point, ignore_errors=True)
+    os.mkdir(mnt_point, mode = 0o755)
+
+    with open(mnt_point + '/hello', 'w', encoding = 'ascii') as file:
+        file.write('hello world\n')
+
+    check_call(f'virt-make-fs --partition=gpt --size=+1M --type=vfat {mnt_point} {image_path}',
+               shell=True)
+
+    return image_path
test/py/tests/test_cat/test_cat.py new file +22 -1
new file mode 100644
index 0000000000..41ddefa40c
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier:      GPL-2.0+
+
+""" Unit test for cat command
+"""
+
+import pytest
+
[email protected]('sandbox')
[email protected]('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.
+    """
+    u_boot_console.run_command(cmd = f'host bind 0 {cat_data}')
+
+    response = u_boot_console.run_command(cmd = 'cat host 0 hello')
+    assert 'hello world' == response
+
+    u_boot_console.run_command(cmd = 'exit', wait_for_echo=False)
--

2.25.1

#2 lore raw

Hi,

Quoted 20 lines · Heinrich Schuchardt
Quoted 18 lines · Roger Knecht

Add cat command to print file content to standard out

---
v3:
- Disable 'cat' by default (CONFIG_CMD_CAT=n)
- 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
- Addd usage documentation

v2:
- Moved cat from boot to shell commands
- Added MAINTAINERS entry
- Added comments
- Improved variable naming

Quoted 14 lines
Quoted 10 lines
  MAINTAINERS                        |  5 +++
  cmd/Kconfig                        |  6 +++
  cmd/Makefile                       |  1 +
  cmd/cat.c                          | 67 ++++++++++++++++++++++++++++++
  configs/sandbox64_defconfig        |  1 +
  configs/sandbox_defconfig          |  1 +
  doc/usage/cmd/cat.rst              | 49 ++++++++++++++++++++++
  test/py/tests/test_cat/conftest.py | 33 +++++++++++++++
  test/py/tests/test_cat/test_cat.py | 22 ++++++++++
  9 files changed, 185 insertions(+)
Quoted 4 lines

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

Very nice patch, could be a good example for others to use.

Quoted 23 lines
Quoted 23 lines
MAINTAINERS +5 -0
index 5857fbf398..2864f84274 100644
@@ -765,6 +765,11 @@ M:       Simon Glass <[email protected]>
  S:  Maintained
  F:  tools/buildman/
 
+CAT
+M:   Roger Knecht <[email protected]>
+S:   Maintained
+F:   cmd/cat.c
+
  CFI FLASH
  M:  Stefan Roese <[email protected]>
  S:  Maintained
cmd/Kconfig +0 -0
index 211ebe9c87..ce7e876475 100644
@@ -1531,6 +1531,12 @@ endmenu
 
Quoted 5 lines

cmd/Kconfig endmenu · line 1536

Quoted 5 lines
cmd/Kconfig +3 -0
  menu "Shell scripting commands"
 
+config CMD_CAT
+     bool "cat"
+     default n

Not needed as things default to n

Quoted 33 lines
Quoted 33 lines
cmd/Kconfig +3 -0
+     help
+       Print file to standard output
+
  config CMD_ECHO
      bool "echo"
      default y
cmd/Makefile +1 -0
index 6e87522b62..1d2590e958 100644
@@ -38,6 +38,7 @@ obj-$(CONFIG_CMD_BOOTZ) += bootz.o
  obj-$(CONFIG_CMD_BOOTI) += booti.o
  obj-$(CONFIG_CMD_BTRFS) += btrfs.o
  obj-$(CONFIG_CMD_BUTTON) += button.o
+obj-$(CONFIG_CMD_CAT) += cat.o
  obj-$(CONFIG_CMD_CACHE) += cache.o
  obj-$(CONFIG_CMD_CBFS) += cbfs.o
  obj-$(CONFIG_CMD_CLK) += clk.o
cmd/cat.c new file +9 -0
new file mode 100644
index 0000000000..c217617cd6
@@ -0,0 +1,67 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2022
+ * Roger Knecht <[email protected]>
+ */
+
+#include <common.h>
+#include <command.h>
+#include <fs.h>
Quoted 8 lines

cmd/cat.c line 17

Quoted 8 lines
cmd/cat.c new file +8 -0
+#include <malloc.h>
+#include <mapmem.h>
+
+static int do_cat(struct cmd_tbl *cmdtp, int flag, int argc,
+               char *const argv[])
+{
+     char *buffer;
+     phys_addr_t buffer_sysmem_addr;

'addr' is shorter

Quoted 13 lines
Quoted 13 lines
cmd/cat.c new file +13 -0
+     loff_t file_size;
+     int ret;
+
+     if (argc < 4)
+             return CMD_RET_USAGE;
+
+     // get file size
+     ret = fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY);
+     if (ret)
+             return ret;
+
+     ret = fs_size(argv[3], &file_size);
+     if (ret)
Quoted 8 lines

cmd/cat.c line 36

Quoted 6 lines
cmd/cat.c new file +6 -0
+             return ret;
+
+     // allocate memory for file content
+     buffer = malloc(file_size + 1);
+     if (!buffer)
+             return -ENOMEM;

Please, do_cat must only return values from enum command_ret_t.

The easiest way to do this is to put your code (from the 'get file size' onwards) in a separate function which is called by this function. It can take args like filename and device. Then when it returns an error code you can print it and return CMD_RET_FAILURE

Quoted 7 lines

cmd/cat.c line 38

Quoted 3 lines
cmd/cat.c new file +2 -0
+
+     memset(buffer, 0, file_size + 1);

Our calloc() implementation already sets the buffer to zero. So you can save one function call.

Quoted 15 lines
Quoted 5 lines
cmd/cat.c new file +5 -0
+
+     // map pointer to system memory
+     buffer_sysmem_addr = map_to_sysmem(buffer);
+
+     // read file to memory

cmd/cat.c line 53

Quoted 10 lines
cmd/cat.c new file +10 -0
+     ret = fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY);
+     if (ret)
+             return ret;
+
+     ret = fs_read(argv[3], buffer_sysmem_addr, 0, 0, &file_size);
+     if (ret)
+             return ret;
+
+     // unmap system memory
+     unmap_sysmem(buffer);

Actually map_to_sysmem() does not create a map, so you don't need this. It is only needed with a call to map_sysmem(). I will send a patch to update the docs.

Quoted 6 lines
Quoted 6 lines
cmd/cat.c new file +6 -0
+
+     // print file content
+     buffer[file_size] = '\0';
+     puts(buffer);
+
+     free(buffer);
Quoted 8 lines

cmd/cat.c line 65

Quoted 6 lines
cmd/cat.c new file +6 -0
+
+     return 0;
+}
+
+U_BOOT_CMD(cat, 4, 1, do_cat,
+        "print file to standard output",

Please, observe CONFIG_SYS_LONGHELP. Have a look at cmd/bootefi.c or others.

Also how about doc/usage/cmd/cat.rst ?

Regards,
Simon

#3 lore raw

------- Original Message -------

Quoted 3 lines · Simon Glass

Hi,

Hi Simon,

Quoted 63 lines · On Thu, 18 Aug 2022 at 11:08, Heinrich Schuchardt [email protected] wrote:
Quoted 21 lines · Roger Knecht
Quoted 19 lines

Add cat command to print file content to standard out

---
v3:
- Disable 'cat' by default (CONFIG_CMD_CAT=n)
- 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
- Addd usage documentation

v2:
- Moved cat from boot to shell commands
- Added MAINTAINERS entry
- Added comments
- Improved variable naming

Quoted 14 lines
Quoted 9 lines

MAINTAINERS | 5 +++
cmd/Kconfig | 6 +++
cmd/Makefile | 1 +
cmd/cat.c | 67 ++++++++++++++++++++++++++++++ configs/sandbox64_defconfig | 1 + configs/sandbox_defconfig | 1 + doc/usage/cmd/cat.rst | 49 ++++++++++++++++++++++ test/py/tests/test_cat/conftest.py | 33 +++++++++++++++ test/py/tests/test_cat/test_cat.py | 22 ++++++++++

Quoted 5 lines
9 files changed, 185 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

Very nice patch, could be a good example for others to use.

Quoted 22 lines
Quoted 22 lines
MAINTAINERS +0 -0
index 5857fbf398..2864f84274 100644
@@ -765,6 +765,11 @@ M: Simon Glass [email protected]

S: Maintained
F: tools/buildman/

MAINTAINERS +5 -0
+CAT
+M: Roger Knecht [email protected]
+S: Maintained
+F: cmd/cat.c
+

CFI FLASH
M: Stefan Roese [email protected]
S: Maintained

cmd/Kconfig +0 -0
index 211ebe9c87..ce7e876475 100644
@@ -1531,6 +1531,12 @@ endmenu
 
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

menu "Shell scripting commands"

+config CMD_CAT
+ bool "cat"
+ default n

Not needed as things default to n

Will be fixed in v5.

Quoted 37 lines
Quoted 37 lines
Quoted 37 lines

+ help
+ Print file to standard output
+
config CMD_ECHO
bool "echo"
default y

cmd/Makefile +0 -0
index 6e87522b62..1d2590e958 100644
@@ -38,6 +38,7 @@ obj-$(CONFIG_CMD_BOOTZ) += bootz.o

obj-$(CONFIG_CMD_BOOTI) += booti.o
obj-$(CONFIG_CMD_BTRFS) += btrfs.o
obj-$(CONFIG_CMD_BUTTON) += button.o
+obj-$(CONFIG_CMD_CAT) += cat.o
obj-$(CONFIG_CMD_CACHE) += cache.o
obj-$(CONFIG_CMD_CBFS) += cbfs.o
obj-$(CONFIG_CMD_CLK) += clk.o

cmd/cat.c new file +12 -0
new file mode 100644
index 0000000000..c217617cd6
@@ -0,0 +1,67 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2022
+ * Roger Knecht [email protected]
+ */
+
+#include <common.h>
+#include <command.h>
+#include <fs.h>
+#include <malloc.h>
+#include <mapmem.h>
+
Quoted 8 lines
Quoted 5 lines

cmd/cat.c line 17

Quoted 5 lines
cmd/cat.c new file +5 -0
+static int do_cat(struct cmd_tbl *cmdtp, int flag, int argc,
+ char *const argv[])
+{
+ char *buffer;
+ phys_addr_t buffer_sysmem_addr;

'addr' is shorter

Ok

Quoted 47 lines
Quoted 14 lines
Quoted 14 lines
cmd/cat.c new file +13 -0
+ loff_t file_size;
+ int ret;
+
+ if (argc < 4)
+ return CMD_RET_USAGE;
+
+ // get file size
+ ret = fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY);
+ if (ret)
+ return ret;
+
+ ret = fs_size(argv[3], &file_size);
+ if (ret)
Quoted 8 lines

cmd/cat.c line 36

Quoted 6 lines
cmd/cat.c new file +6 -0
+ return ret;
+
+ // allocate memory for file content
+ buffer = malloc(file_size + 1);
+ if (!buffer)
+ return -ENOMEM;

Please, do_cat must only return values from enum command_ret_t.

The easiest way to do this is to put your code (from the 'get file size' onwards) in a separate function which is called by this function. It can take args like filename and device. Then when it returns an error code you can print it and return CMD_RET_FAILURE

Quoted 18 lines

cmd/cat.c line 38

Quoted 2 lines
cmd/cat.c new file +2 -0
+
+ memset(buffer, 0, file_size + 1);

Our calloc() implementation already sets the buffer to zero. So you can save one function call.

Quoted 12 lines
cmd/cat.c new file +12 -0
+
+ // map pointer to system memory
+ buffer_sysmem_addr = map_to_sysmem(buffer);
+
+ // read file to memory
+ ret = fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY);
+ if (ret)
+ return ret;
+
+ ret = fs_read(argv[3], buffer_sysmem_addr, 0, 0, &file_size);
+ if (ret)
+ return ret;
Quoted 8 lines
Quoted 3 lines

cmd/cat.c line 53

Quoted 3 lines
cmd/cat.c new file +3 -0
+
+ // unmap system memory
+ unmap_sysmem(buffer);

Actually map_to_sysmem() does not create a map, so you don't need this. It is only needed with a call to map_sysmem(). I will send a patch to update the docs.

The map_to_sysmem() is used to solve a problem in the sandbox which I described in more detail here:
https://lists.denx.de/pipermail/u-boot/2022-June/486883.html
Let me know if there is a better solution.

Quoted 17 lines
Quoted 15 lines

cmd/cat.c line 65

Quoted 13 lines
cmd/cat.c new file +12 -0
+
+ // print file content
+ buffer[file_size] = '\0';
+ puts(buffer);
+
+ free(buffer);
+
+ return 0;
+}
+
+U_BOOT_CMD(cat, 4, 1, do_cat,
+ "print file to standard output",

Please, observe CONFIG_SYS_LONGHELP. Have a look at cmd/bootefi.c or others.

Quoted 1 line

Also how about doc/usage/cmd/cat.rst ?

I don't understand this remark. There is already a doc/usage/cmd/cat.rst in the patch.

Quoted 3 lines

Regards,
Simon

Thanks,
Roger

#4 lore raw

Hi Roger,

Quoted 184 lines · Roger Knecht

------- Original Message -------

Quoted 3 lines · Simon Glass

Hi,

Hi Simon,

Quoted 63 lines · On Thu, 18 Aug 2022 at 11:08, Heinrich Schuchardt [email protected] wrote:
Quoted 21 lines · Roger Knecht
Quoted 19 lines

Add cat command to print file content to standard out

---
v3:
- Disable 'cat' by default (CONFIG_CMD_CAT=n)
- 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
- Addd usage documentation

v2:map_to_sysmem
- Moved cat from boot to shell commands
- Added MAINTAINERS entry
- Added comments
- Improved variable naming

Quoted 14 lines
Quoted 9 lines

MAINTAINERS | 5 +++
cmd/Kconfig | 6 +++
cmd/Makefile | 1 +
cmd/cat.c | 67 ++++++++++++++++++++++++++++++ configs/sandbox64_defconfig | 1 + configs/sandbox_defconfig | 1 + doc/usage/cmd/cat.rst | 49 ++++++++++++++++++++++ test/py/tests/test_cat/conftest.py | 33 +++++++++++++++ test/py/tests/test_cat/test_cat.py | 22 ++++++++++

Quoted 5 lines
9 files changed, 185 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

Very nice patch, could be a good example for others to use.

Quoted 22 lines
Quoted 22 lines
MAINTAINERS +0 -0
index 5857fbf398..2864f84274 100644
@@ -765,6 +765,11 @@ M: Simon Glass [email protected]

S: Maintained
F: tools/buildman/

MAINTAINERS +5 -0
+CAT
+M: Roger Knecht [email protected]
+S: Maintained
+F: cmd/cat.c
+

CFI FLASH
M: Stefan Roese [email protected]
S: Maintained

cmd/Kconfig +0 -0
index 211ebe9c87..ce7e876475 100644
@@ -1531,6 +1531,12 @@ endmenu
 
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

menu "Shell scripting commands"

+config CMD_CAT
+ bool "cat"
+ default n

Not needed as things default to n

Will be fixed in v5.

Quoted 37 lines
Quoted 37 lines
Quoted 37 lines

+ help
+ Print file to standard output
+
config CMD_ECHO
bool "echo"
default y

cmd/Makefile +0 -0
index 6e87522b62..1d2590e958 100644
@@ -38,6 +38,7 @@ obj-$(CONFIG_CMD_BOOTZ) += bootz.o

obj-$(CONFIG_CMD_BOOTI) += booti.o
obj-$(CONFIG_CMD_BTRFS) += btrfs.o
obj-$(CONFIG_CMD_BUTTON) += button.o
+obj-$(CONFIG_CMD_CAT) += cat.o
obj-$(CONFIG_CMD_CACHE) += cache.o
obj-$(CONFIG_CMD_CBFS) += cbfs.o
obj-$(CONFIG_CMD_CLK) += clk.o

cmd/cat.c new file +12 -0
new file mode 100644
index 0000000000..c217617cd6
@@ -0,0 +1,67 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2022
+ * Roger Knecht [email protected]
+ */
+
+#include <common.h>
+#include <command.h>
+#include <fs.h>
+#include <malloc.h>
+#include <mapmem.h>
+
Quoted 8 lines
Quoted 5 lines

cmd/cat.c line 17

Quoted 5 lines
cmd/cat.c new file +5 -0
+static int do_cat(struct cmd_tbl *cmdtp, int flag, int argc,
+ char *const argv[])
+{
+ char *buffer;
+ phys_addr_t buffer_sysmem_addr;

'addr' is shorter

Ok

Quoted 47 lines
Quoted 14 lines
Quoted 14 lines
cmd/cat.c new file +13 -0
+ loff_t file_size;
+ int ret;
+
+ if (argc < 4)
+ return CMD_RET_USAGE;
+
+ // get file size
+ ret = fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY);
+ if (ret)
+ return ret;
+
+ ret = fs_size(argv[3], &file_size);
+ if (ret)
Quoted 8 lines

cmd/cat.c line 36

Quoted 6 lines
cmd/cat.c new file +6 -0
+ return ret;
+
+ // allocate memory for file content
+ buffer = malloc(file_size + 1);
+ if (!buffer)
+ return -ENOMEM;

Please, do_cat must only return values from enum command_ret_t.

The easiest way to do this is to put your code (from the 'get file size' onwards) in a separate function which is called by this function. It can take args like filename and device. Then when it returns an error code you can print it and return CMD_RET_FAILURE

Quoted 18 lines

cmd/cat.c line 38

Quoted 2 lines
cmd/cat.c new file +2 -0
+
+ memset(buffer, 0, file_size + 1);

Our calloc() implementation already sets the buffer to zero. So you can save one function call.

Quoted 12 lines
cmd/cat.c new file +12 -0
+
+ // map pointer to system memory
+ buffer_sysmem_addr = map_to_sysmem(buffer);
+
+ // read file to memory
+ ret = fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY);
+ if (ret)
+ return ret;
+
+ ret = fs_read(argv[3], buffer_sysmem_addr, 0, 0, &file_size);
+ if (ret)
+ return ret;
Quoted 8 lines
Quoted 3 lines

cmd/cat.c line 53

Quoted 3 lines
cmd/cat.c new file +3 -0
+
+ // unmap system memory
+ unmap_sysmem(buffer);

Actually map_to_sysmem() does not create a map, so you don't need this. It is only needed with a call to map_sysmem(). I will send a patch to update the docs.

Quoted 4 lines

The map_to_sysmem() is used to solve a problem in the sandbox which I described in more detail here:
https://lists.denx.de/pipermail/u-boot/2022-June/486883.html
Let me know if there is a better solution.

Actually I was talking the unmap_sysmem(), the line above my comment. The unmap is used after a map, where as the map_to_sysmem() is doing something different. Yes you need map_to_sysmem(), but you should not need the unmap_sysmem(). If I am missing something, let me know.

[..]

Quoted 2 lines
Quoted 1 line

Also how about doc/usage/cmd/cat.rst ?

I don't understand this remark. There is already a doc/usage/cmd/cat.rst in the patch.

Yes sorry about that. I thought I deleted that line.

Regards,
Simon

#5 lore raw

------- Original Message -------

Quoted 192 lines · Simon Glass

Hi Roger,

Quoted 174 lines · On Sun, 21 Aug 2022 at 07:27, Roger Knecht [email protected] wrote:

------- Original Message -------

Quoted 162 lines · On Friday, August 19th, 2022 at 16:08, Simon Glass [email protected] wrote:

Hi,
Hi Simon,

Quoted 20 lines · On Thu, 18 Aug 2022 at 11:08, Heinrich Schuchardt [email protected] wrote:
Quoted 18 lines · Roger Knecht

Add cat command to print file content to standard out

---
v3:
- Disable 'cat' by default (CONFIG_CMD_CAT=n)
- 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
- Addd usage documentation

v2:map_to_sysmem
- Moved cat from boot to shell commands
- Added MAINTAINERS entry
- Added comments
- Improved variable naming

Quoted 14 lines
Quoted 9 lines

MAINTAINERS | 5 +++
cmd/Kconfig | 6 +++
cmd/Makefile | 1 +
cmd/cat.c | 67 ++++++++++++++++++++++++++++++ configs/sandbox64_defconfig | 1 + configs/sandbox_defconfig | 1 + doc/usage/cmd/cat.rst | 49 ++++++++++++++++++++++ test/py/tests/test_cat/conftest.py | 33 +++++++++++++++ test/py/tests/test_cat/test_cat.py | 22 ++++++++++

Quoted 5 lines
9 files changed, 185 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

Very nice patch, could be a good example for others to use.

Quoted 22 lines
Quoted 22 lines
MAINTAINERS +0 -0
index 5857fbf398..2864f84274 100644
@@ -765,6 +765,11 @@ M: Simon Glass [email protected]

S: Maintained
F: tools/buildman/

MAINTAINERS +5 -0
+CAT
+M: Roger Knecht [email protected]
+S: Maintained
+F: cmd/cat.c
+

CFI FLASH
M: Stefan Roese [email protected]
S: Maintained

cmd/Kconfig +0 -0
index 211ebe9c87..ce7e876475 100644
@@ -1531,6 +1531,12 @@ endmenu
 
Quoted 5 lines
Quoted 5 lines

menu "Shell scripting commands"

+config CMD_CAT
+ bool "cat"
+ default n

Not needed as things default to n
Will be fixed in v5.

Quoted 33 lines
Quoted 33 lines

+ help
+ Print file to standard output
+
config CMD_ECHO
bool "echo"
default y

cmd/Makefile +0 -0
index 6e87522b62..1d2590e958 100644
@@ -38,6 +38,7 @@ obj-$(CONFIG_CMD_BOOTZ) += bootz.o

obj-$(CONFIG_CMD_BOOTI) += booti.o
obj-$(CONFIG_CMD_BTRFS) += btrfs.o
obj-$(CONFIG_CMD_BUTTON) += button.o
+obj-$(CONFIG_CMD_CAT) += cat.o
obj-$(CONFIG_CMD_CACHE) += cache.o
obj-$(CONFIG_CMD_CBFS) += cbfs.o
obj-$(CONFIG_CMD_CLK) += clk.o

cmd/cat.c new file +9 -0
new file mode 100644
index 0000000000..c217617cd6
@@ -0,0 +1,67 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2022
+ * Roger Knecht [email protected]
+ */
+
+#include <common.h>
+#include <command.h>
+#include <fs.h>
Quoted 8 lines

cmd/cat.c line 17

Quoted 8 lines
cmd/cat.c new file +8 -0
+#include <malloc.h>
+#include <mapmem.h>
+
+static int do_cat(struct cmd_tbl *cmdtp, int flag, int argc,
+ char *const argv[])
+{
+ char *buffer;
+ phys_addr_t buffer_sysmem_addr;

'addr' is shorter
Ok

Quoted 13 lines
Quoted 13 lines
cmd/cat.c new file +13 -0
+ loff_t file_size;
+ int ret;
+
+ if (argc < 4)
+ return CMD_RET_USAGE;
+
+ // get file size
+ ret = fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY);
+ if (ret)
+ return ret;
+
+ ret = fs_size(argv[3], &file_size);
+ if (ret)
Quoted 8 lines

cmd/cat.c line 36

Quoted 6 lines
cmd/cat.c new file +6 -0
+ return ret;
+
+ // allocate memory for file content
+ buffer = malloc(file_size + 1);
+ if (!buffer)
+ return -ENOMEM;

Please, do_cat must only return values from enum command_ret_t.

The easiest way to do this is to put your code (from the 'get file size' onwards) in a separate function which is called by this function. It can take args like filename and device. Then when it returns an error code you can print it and return CMD_RET_FAILURE

Quoted 17 lines

cmd/cat.c line 38

Quoted 2 lines
cmd/cat.c new file +2 -0
+
+ memset(buffer, 0, file_size + 1);

Our calloc() implementation already sets the buffer to zero. So you can save one function call.

Quoted 11 lines
cmd/cat.c new file +11 -0
+
+ // map pointer to system memory
+ buffer_sysmem_addr = map_to_sysmem(buffer);
+
+ // read file to memory
+ ret = fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY);
+ if (ret)
+ return ret;
+
+ ret = fs_read(argv[3], buffer_sysmem_addr, 0, 0, &file_size);
+ if (ret)
Quoted 8 lines
Quoted 4 lines

cmd/cat.c line 53

Quoted 4 lines
cmd/cat.c new file +4 -0
+ return ret;
+
+ // unmap system memory
+ unmap_sysmem(buffer);

Actually map_to_sysmem() does not create a map, so you don't need this. It is only needed with a call to map_sysmem(). I will send a patch to update the docs.

Quoted 3 lines

The map_to_sysmem() is used to solve a problem in the sandbox which I described in more detail here:
https://lists.denx.de/pipermail/u-boot/2022-June/486883.html
Let me know if there is a better solution.

Actually I was talking the unmap_sysmem(), the line above my comment. The unmap is used after a map, where as the map_to_sysmem() is doing something different. Yes you need map_to_sysmem(), but you should not need the unmap_sysmem(). If I am missing something, let me know.

[..]

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

Also how about doc/usage/cmd/cat.rst ?
I don't understand this remark. There is already a doc/usage/cmd/cat.rst in the patch.

Yes sorry about that. I thought I deleted that line.

Regards,
Simon

Thanks for the clarification Simon.
I will remove the unmap_sysmem() call in the next patch.

Regards,
Roger