[PATCH v4 0/9] Various minor fixes

This series collects a few minor fixes and improvements that have been hanging around in a branch for a while.
Changes in v4: - Indicate that hd does not show the offset correctly - Add an xxd + sed example too - Fix 'existing' typo
Changes in v3: - Show phase only for IH_OS_U_BOOT, drop the last debug() - Add new patch to describe the md command - Add this to the docs for the md command - Add comments as to why we have os_malloc() and os_realloc()
Changes in v2: - Drop the file instead of fixing it up - Add new patch to add os_realloc() - Drop unnecessary check before calling os_free() - Update the comment for os_free() - Also free the RAM buffer - Convert the rest of the allocations in os.c, etc.
Simon Glass (9): spl: Drop duplicate 'Jumping to U-Boot' message binman: Indicate how to make binman verbose doc: describe the md command doc: Add a note about producing 'md.b' output using hexdump s5p4418_nanopi2: Drop dead code sandbox: Add os_realloc() sandbox: Avoid using malloc() for system state sandbox: Write out bloblist when exiting bootm: Fix duplicate debugging in bootm_process_cmdline()
Makefile | 1 + arch/arm/mach-nexell/Makefile | 1 - arch/arm/mach-nexell/cmd_boot_linux.c | 144 -------------------------- arch/sandbox/cpu/os.c | 72 ++++++++++--- arch/sandbox/cpu/spl.c | 10 +- arch/sandbox/cpu/start.c | 12 ++- arch/sandbox/cpu/state.c | 23 ++-- common/bootm.c | 2 +- common/spl/spl.c | 3 +- doc/Makefile | 1 - doc/usage/index.rst | 1 + doc/usage/md.rst | 106 +++++++++++++++++++ include/os.h | 15 ++- tools/binman/README | 4 +- 14 files changed, 214 insertions(+), 181 deletions(-) delete mode 100644 arch/arm/mach-nexell/cmd_boot_linux.c create mode 100644 doc/usage/md.rst

This is printed twice but we only need one message, since there is very little processing in between them. Drop the second one, since all branches of the switch() already have output. Update the U-Boot message to include the phase being jumped to.
Signed-off-by: Simon Glass sjg@chromium.org ---
(no changes since v3)
Changes in v3: - Show phase only for IH_OS_U_BOOT, drop the last debug()
common/spl/spl.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/common/spl/spl.c b/common/spl/spl.c index cdd7b05f279..42004506446 100644 --- a/common/spl/spl.c +++ b/common/spl/spl.c @@ -693,7 +693,7 @@ void board_init_r(gd_t *dummy1, ulong dummy2) #endif switch (spl_image.os) { case IH_OS_U_BOOT: - debug("Jumping to U-Boot\n"); + debug("Jumping to %s...\n", spl_phase_name(spl_next_phase())); break; #if CONFIG_IS_ENABLED(ATF) case IH_OS_ARM_TRUSTED_FIRMWARE: @@ -740,7 +740,6 @@ void board_init_r(gd_t *dummy1, ulong dummy2) debug("Failed to stash bootstage: err=%d\n", ret); #endif
- debug("loaded - jumping to %s...\n", spl_phase_name(spl_next_phase())); spl_board_prepare_for_boot(); jump_to_image_no_args(&spl_image); }

On Sat, Feb 06, 2021 at 09:57:27AM -0700, Simon Glass wrote:
This is printed twice but we only need one message, since there is very little processing in between them. Drop the second one, since all branches of the switch() already have output. Update the U-Boot message to include the phase being jumped to.
Signed-off-by: Simon Glass sjg@chromium.org
Applied to u-boot/next, thanks!

Add notes about how to make binman produce verbose logging when building.
Add a comment on how to do this.
Signed-off-by: Simon Glass sjg@chromium.org ---
(no changes since v1)
Makefile | 1 + tools/binman/README | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile index ebbedb1fb1a..2917af20ecd 100644 --- a/Makefile +++ b/Makefile @@ -1330,6 +1330,7 @@ u-boot.ldr: u-boot # binman # --------------------------------------------------------------------------- # Use 'make BINMAN_DEBUG=1' to enable debugging +# Use 'make BINMAN_VERBOSE=3' to set vebosity level default_dt := $(if $(DEVICE_TREE),$(DEVICE_TREE),$(CONFIG_DEFAULT_DEVICE_TREE)) quiet_cmd_binman = BINMAN $@ cmd_binman = $(srctree)/tools/binman/binman $(if $(BINMAN_DEBUG),-D) \ diff --git a/tools/binman/README b/tools/binman/README index a00c9026163..45f0a0c2cd3 100644 --- a/tools/binman/README +++ b/tools/binman/README @@ -637,7 +637,8 @@ Logging
Binman normally operates silently unless there is an error, in which case it just displays the error. The -D/--debug option can be used to create a full -backtrace when errors occur. +backtrace when errors occur. You can use BINMAN_DEBUG=1 when building to select +this.
Internally binman logs some output while it is running. This can be displayed by increasing the -v/--verbosity from the default of 1: @@ -649,6 +650,7 @@ by increasing the -v/--verbosity from the default of 1: 4: detailed information about each operation 5: debug (all output)
+You can use BINMAN_VERBOSE=5 (for example) when building to select this.
Hashing Entries ---------------

On Sat, Feb 06, 2021 at 09:57:28AM -0700, Simon Glass wrote:
Add notes about how to make binman produce verbose logging when building.
Add a comment on how to do this.
Signed-off-by: Simon Glass sjg@chromium.org
Applied to u-boot/next, thanks!

Provide a man-page for the md command.
Signed-off-by: Simon Glass sjg@chromium.org ---
(no changes since v3)
Changes in v3: - Add new patch to describe the md command
doc/Makefile | 1 - doc/usage/index.rst | 1 + doc/usage/md.rst | 96 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 doc/usage/md.rst
diff --git a/doc/Makefile b/doc/Makefile index a686d4728ec..683e4b56099 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -56,7 +56,6 @@ quiet_cmd_sphinx = SPHINX $@ --> file://$(abspath $(BUILDDIR)/$3/$4) PYTHONDONTWRITEBYTECODE=1 \ BUILDDIR=$(abspath $(BUILDDIR)) SPHINX_CONF=$(abspath $(srctree)/$(src)/$5/$(SPHINX_CONF)) \ $(SPHINXBUILD) \ - -W \ -b $2 \ -c $(abspath $(srctree)/$(src)) \ -d $(abspath $(BUILDDIR)/.doctrees/$3) \ diff --git a/doc/usage/index.rst b/doc/usage/index.rst index 5754958d7e4..8f302c26688 100644 --- a/doc/usage/index.rst +++ b/doc/usage/index.rst @@ -26,6 +26,7 @@ Shell commands load loady mbr + md pstore sbi true diff --git a/doc/usage/md.rst b/doc/usage/md.rst new file mode 100644 index 00000000000..3951b0d58f5 --- /dev/null +++ b/doc/usage/md.rst @@ -0,0 +1,96 @@ +.. SPDX-License-Identifier: GPL-2.0+: + +md command +========== + +Synopis +------- + +:: + + md <address>[<data_size>] [<length>] + +Description +----------- + +The md command is used to dump the contents of memory. It uses a standard +format that includes the address, hex data and ASCII display. It supports +various data sizes and uses the endianness of the target. + +The specified data_size and length become the defaults for future memory +commands commands. + +address + start address to display + +data_size + size of each value to display (defaults to .l): + + ========= =================== + data_size Output size + ========= =================== + .b byte + .w word (16 bits) + .l long (32 bits) + .q quadword (64 bits) + ========= =================== + +length + number of values to dump. Defaults to 40 (0d64). Note that this is not + the same as the number of bytes, unless .b is used. + + +Example +------- + +:: + + => md 10000 + 00010000: 00010000 00000000 f0f30f00 00005596 .............U.. + 00010010: 10011010 00000000 10011010 00000000 ................ + 00010020: 10011050 00000000 b96d4cd8 00007fff P........Lm..... + 00010030: 00000000 00000000 f0f30f18 00005596 .............U.. + 00010040: 10011040 00000000 10011040 00000000 @.......@....... + 00010050: b96d4cd8 00007fff 10011020 00000000 .Lm..... ....... + 00010060: 00000003 000000c3 00000000 00000000 ................ + 00010070: 00000000 00000000 f0e892f3 00005596 .............U.. + 00010080: 00000000 000000a1 00000000 00000000 ................ + 00010090: 00000000 00000000 f0e38aa6 00005596 .............U.. + 000100a0: 00000000 000000a6 00000022 00000000 ........"....... + 000100b0: 00000001 00000000 f0e38aa1 00005596 .............U.. + 000100c0: 00000000 000000be 00000000 00000000 ................ + 000100d0: 00000000 00000000 00000000 00000000 ................ + 000100e0: 00000000 00000000 00000000 00000000 ................ + 000100f0: 00000000 00000000 00000000 00000000 ................ + => md.b 10000 + 00010000: 00 00 01 00 00 00 00 00 00 0f f3 f0 96 55 00 00 .............U.. + 00010010: 10 10 01 10 00 00 00 00 10 10 01 10 00 00 00 00 ................ + 00010020: 50 10 01 10 00 00 00 00 d8 4c 6d b9 ff 7f 00 00 P........Lm..... + 00010030: 00 00 00 00 00 00 00 00 18 0f f3 f0 96 55 00 00 .............U.. + => md.b 10000 10 + 00010000: 00 00 01 00 00 00 00 00 00 0f f3 f0 96 55 00 00 .............U.. + => + 00010010: 10 10 01 10 00 00 00 00 10 10 01 10 00 00 00 00 ................ + => + 00010020: 50 10 01 10 00 00 00 00 d8 4c 6d b9 ff 7f 00 00 P........Lm..... + => + => md.q 10000 + 00010000: 0000000000010000 00005596f0f30f00 .............U.. + 00010010: 0000000010011010 0000000010011010 ................ + 00010020: 0000000010011050 00007fffb96d4cd8 P........Lm..... + 00010030: 0000000000000000 00005596f0f30f18 .............U.. + 00010040: 0000000010011040 0000000010011040 @.......@....... + 00010050: 00007fffb96d4cd8 0000000010011020 .Lm..... ....... + 00010060: 000000c300000003 0000000000000000 ................ + 00010070: 0000000000000000 00005596f0e892f3 .............U.. + +The empty commands cause a 'repeat', so that md shows the next available data +in the same format as before. + + +Return value +------------ + +The return value $? is always 0 (true). + +

On Sat, Feb 06, 2021 at 09:57:29AM -0700, Simon Glass wrote:
Provide a man-page for the md command.
Signed-off-by: Simon Glass sjg@chromium.org
Applied to u-boot/next, thanks!

Comparing a hex dump on the U-Boot command line with the contents of a file on the host system is fairly easy and convenient to do manually if it is small. But the format used hexdump by default differs from that shown by U-Boot. Add a note about how to make them the same.
(For large dumps, writing the data to the network with tftpput, or to a USB stick with ext4save is easiest.)
Signed-off-by: Simon Glass sjg@chromium.org ---
Changes in v4: - Indicate that hd does not show the offset correctly - Add an xxd + sed example too
Changes in v3: - Add this to the docs for the md command
doc/usage/md.rst | 10 ++++++++++ 1 file changed, 10 insertions(+)
diff --git a/doc/usage/md.rst b/doc/usage/md.rst index 3951b0d58f5..4c1073ea354 100644 --- a/doc/usage/md.rst +++ b/doc/usage/md.rst @@ -39,6 +39,16 @@ length number of values to dump. Defaults to 40 (0d64). Note that this is not the same as the number of bytes, unless .b is used.
+Note that the format of 'md.b' can be emulated from linux with:: + + # This works but requires using sed to get the extra spaces + # <addr> is the address, <f> is the filename + xxd -o <addr> -g1 <f> |sed 's/ / /' >bad + + # This uses a single tool but the offset always starts at 0 + # <f> is the filename + hexdump -v -e '"%08.8_ax: " 16/1 "%02x " " "' -e '16/1 "%_p" "\n" ' <f> +
Example -------

On Sat, Feb 06, 2021 at 09:57:30AM -0700, Simon Glass wrote:
Comparing a hex dump on the U-Boot command line with the contents of a file on the host system is fairly easy and convenient to do manually if it is small. But the format used hexdump by default differs from that shown by U-Boot. Add a note about how to make them the same.
(For large dumps, writing the data to the network with tftpput, or to a USB stick with ext4save is easiest.)
Signed-off-by: Simon Glass sjg@chromium.org
Applied to u-boot/next, thanks!

This code is still using the old command typedef. It was not noticed since this file is not currently built. It is using a non-existent option in the Makefile.
Drop this file since it is not needed for correct operation.
Signed-off-by: Simon Glass sjg@chromium.org ---
(no changes since v2)
Changes in v2: - Drop the file instead of fixing it up
arch/arm/mach-nexell/Makefile | 1 - arch/arm/mach-nexell/cmd_boot_linux.c | 144 -------------------------- 2 files changed, 145 deletions(-) delete mode 100644 arch/arm/mach-nexell/cmd_boot_linux.c
diff --git a/arch/arm/mach-nexell/Makefile b/arch/arm/mach-nexell/Makefile index 10b3963ed10..dda16dbb8e6 100644 --- a/arch/arm/mach-nexell/Makefile +++ b/arch/arm/mach-nexell/Makefile @@ -10,4 +10,3 @@ obj-y += nx_gpio.o obj-y += tieoff.o obj-$(CONFIG_ARCH_S5P4418) += reg-call.o obj-$(CONFIG_ARCH_S5P4418) += nx_sec_reg.o -obj-$(CONFIG_CMD_BOOTL) += cmd_boot_linux.o diff --git a/arch/arm/mach-nexell/cmd_boot_linux.c b/arch/arm/mach-nexell/cmd_boot_linux.c deleted file mode 100644 index f2dedfe1625..00000000000 --- a/arch/arm/mach-nexell/cmd_boot_linux.c +++ /dev/null @@ -1,144 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0+ -/* - * (C) Copyright 2016 nexell - * jhkim jhkim@nexell.co.kr - */ - -#include <common.h> -#include <bootm.h> -#include <command.h> -#include <environment.h> -#include <errno.h> -#include <image.h> -#include <fdt_support.h> - -#if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_CLI_FRAMEWORK) - -DECLARE_GLOBAL_DATA_PTR; - -static bootm_headers_t linux_images; - -static void boot_go_set_os(cmd_tbl_t *cmdtp, int flag, int argc, - char * const argv[], - bootm_headers_t *images) -{ - char * const img_addr = argv[0]; - - images->os.type = IH_TYPE_KERNEL; - images->os.comp = IH_COMP_NONE; - images->os.os = IH_OS_LINUX; - images->os.load = simple_strtoul(img_addr, NULL, 16); - images->ep = images->os.load; -#if defined(CONFIG_ARM) - images->os.arch = IH_ARCH_ARM; -#elif defined(CONFIG_ARM64) - images->os.arch = IH_ARCH_ARM64; -#else - #error "Not support architecture ..." -#endif - if (!IS_ENABLED(CONFIG_OF_LIBFDT) && !IS_ENABLED(CONFIG_SPL_BUILD)) { - /* set DTB address for linux kernel */ - if (argc > 2) { - unsigned long ft_addr; - - ft_addr = simple_strtol(argv[2], NULL, 16); - images->ft_addr = (char *)ft_addr; - - /* - * if not defined IMAGE_ENABLE_OF_LIBFDT, - * must be set to fdt address - */ - if (!IMAGE_ENABLE_OF_LIBFDT) - gd->bd->bi_boot_params = ft_addr; - - debug("## set ft:%08lx and boot params:%08lx [control of:%s]" - "...\n", ft_addr, gd->bd->bi_boot_params, - IMAGE_ENABLE_OF_LIBFDT ? "on" : "off"); - } - } -} - -#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_LMB) -static void boot_start_lmb(bootm_headers_t *images) -{ - ulong mem_start; - phys_size_t mem_size; - - lmb_init(&images->lmb); - - mem_start = getenv_bootm_low(); - mem_size = getenv_bootm_size(); - - lmb_add(&images->lmb, (phys_addr_t)mem_start, mem_size); - - arch_lmb_reserve(&images->lmb); - board_lmb_reserve(&images->lmb); -} -#else -#define lmb_reserve(lmb, base, size) -static inline void boot_start_lmb(bootm_headers_t *images) { } -#endif - -int do_boot_linux(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) -{ - boot_os_fn *boot_fn; - bootm_headers_t *images = &linux_images; - int flags; - int ret; - - boot_start_lmb(images); - - flags = BOOTM_STATE_START; - - argc--; argv++; - boot_go_set_os(cmdtp, flag, argc, argv, images); - - if (IS_ENABLED(CONFIG_OF_LIBFDT)) { - /* find flattened device tree */ - ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, images, - &images->ft_addr, &images->ft_len); - if (ret) { - puts("Could not find a valid device tree\n"); - return 1; - } - set_working_fdt_addr((ulong)images->ft_addr); - } - - if (!IS_ENABLED(CONFIG_OF_LIBFDT)) - flags |= BOOTM_STATE_OS_GO; - - boot_fn = do_bootm_linux; - ret = boot_fn(flags, argc, argv, images); - - if (ret == BOOTM_ERR_UNIMPLEMENTED) - show_boot_progress(BOOTSTAGE_ID_DECOMP_UNIMPL); - else if (ret == BOOTM_ERR_RESET) - do_reset(cmdtp, flag, argc, argv); - - return ret; -} - -U_BOOT_CMD(bootl, CONFIG_SYS_MAXARGS, 1, do_boot_linux, - "boot linux image from memory", - "[addr [arg ...]]\n - boot linux image stored in memory\n" - "\tuse a '-' for the DTB address\n" -); -#endif - -#if defined(CONFIG_CMD_BOOTD) && !defined(CONFIG_CMD_BOOTM) -int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) -{ - return run_command(env_get("bootcmd"), flag); -} - -U_BOOT_CMD(boot, 1, 1, do_bootd, - "boot default, i.e., run 'bootcmd'", - "" -); - -/* keep old command name "bootd" for backward compatibility */ -U_BOOT_CMD(bootd, 1, 1, do_bootd, - "boot default, i.e., run 'bootcmd'", - "" -); -#endif

On Sat, Feb 06, 2021 at 09:57:31AM -0700, Simon Glass wrote:
This code is still using the old command typedef. It was not noticed since this file is not currently built. It is using a non-existent option in the Makefile.
Drop this file since it is not needed for correct operation.
Signed-off-by: Simon Glass sjg@chromium.org
Applied to u-boot/next, thanks!

We provide os_malloc() and os_free() but not os_realloc(). Add this, following the usual semantics. Also update os_malloc() to behave correctly when passed a zero size.
Signed-off-by: Simon Glass sjg@chromium.org Reviewed-by: Heinrich Schuchardt xypron.glpk@gmx.de ---
(no changes since v3)
Changes in v3: - Add comments as to why we have os_malloc() and os_realloc()
Changes in v2: - Add new patch to add os_realloc()
arch/sandbox/cpu/os.c | 48 +++++++++++++++++++++++++++++++++++++++++++ include/os.h | 12 ++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-)
diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c index 3d8af0a52bb..d23aad318be 100644 --- a/arch/sandbox/cpu/os.c +++ b/arch/sandbox/cpu/os.c @@ -267,11 +267,18 @@ void os_tty_raw(int fd, bool allow_sigs) signal(SIGINT, os_sigint_handler); }
+/* + * Provide our own malloc so we don't use space in the sandbox ram_buf for + * allocations that are internal to sandbox, or need to be done before U-Boot's + * malloc() is ready. + */ void *os_malloc(size_t length) { int page_size = getpagesize(); struct os_mem_hdr *hdr;
+ if (!length) + return NULL; /* * Use an address that is hopefully available to us so that pointers * to this memory are fairly obvious. If we end up with a different @@ -298,6 +305,47 @@ void os_free(void *ptr) } }
+/* These macros are from kernel.h but not accessible in this file */ +#define ALIGN(x, a) __ALIGN_MASK((x), (typeof(x))(a) - 1) +#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) + +/* + * Provide our own malloc so we don't use space in the sandbox ram_buf for + * allocations that are internal to sandbox, or need to be done before U-Boot's + * malloc() is ready. + */ +void *os_realloc(void *ptr, size_t length) +{ + int page_size = getpagesize(); + struct os_mem_hdr *hdr; + void *new_ptr; + + /* Reallocating a NULL pointer is just an alloc */ + if (!ptr) + return os_malloc(length); + + /* Changing a length to 0 is just a free */ + if (length) { + os_free(ptr); + return NULL; + } + + /* + * If the new size is the same number of pages as the old, nothing to + * do. There isn't much point in shrinking things + */ + hdr = ptr - page_size; + if (ALIGN(length, page_size) <= ALIGN(hdr->length, page_size)) + return ptr; + + /* We have to grow it, so allocate something new */ + new_ptr = os_malloc(length); + memcpy(new_ptr, ptr, hdr->length); + os_free(ptr); + + return new_ptr; +} + void os_usleep(unsigned long usec) { usleep(usec); diff --git a/include/os.h b/include/os.h index 65bcb232cab..fd010cfee83 100644 --- a/include/os.h +++ b/include/os.h @@ -114,7 +114,7 @@ void os_fd_restore(void); * os_malloc() - aquires some memory from the underlying os. * * @length: Number of bytes to be allocated - * Return: Pointer to length bytes or NULL on error + * Return: Pointer to length bytes or NULL if @length is 0 or on error */ void *os_malloc(size_t length);
@@ -127,6 +127,16 @@ void *os_malloc(size_t length); */ void os_free(void *ptr);
+/** + * os_realloc() - reallocate memory + * + * This follows the semantics of realloc(), so can perform an os_malloc() or + * os_free() depending on @ptr and @length. + * + * Return: Pointer to reallocated memory or NULL if @length is 0 + */ +void *os_realloc(void *ptr, size_t length); + /** * os_usleep() - access to the usleep function of the os *

On Sat, Feb 06, 2021 at 09:57:32AM -0700, Simon Glass wrote:
We provide os_malloc() and os_free() but not os_realloc(). Add this, following the usual semantics. Also update os_malloc() to behave correctly when passed a zero size.
Signed-off-by: Simon Glass sjg@chromium.org Reviewed-by: Heinrich Schuchardt xypron.glpk@gmx.de
Applied to u-boot/next, thanks!

This state is not accessible to the running U-Boot but at present it is allocated in the emulated SDRAM. This doesn't seem very useful. Adjust it to allocate from the OS instead.
The RAM buffer is currently not freed, but should be, so add that into state_uninit(). Update the comment for os_free() to indicate that NULL is a valid parameter value.
Note that the strdup() in spl_board_load_image() is changed as well, since strdup() allocates memory in the RAM buffer.
Signed-off-by: Simon Glass sjg@chromium.org ---
(no changes since v2)
Changes in v2: - Drop unnecessary check before calling os_free() - Update the comment for os_free() - Also free the RAM buffer - Convert the rest of the allocations in os.c, etc.
arch/sandbox/cpu/os.c | 24 ++++++++++++------------ arch/sandbox/cpu/spl.c | 10 +++++++--- arch/sandbox/cpu/start.c | 12 +++++++----- arch/sandbox/cpu/state.c | 18 +++++++++--------- include/os.h | 3 ++- 5 files changed, 37 insertions(+), 30 deletions(-)
diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c index d23aad318be..f5000e64966 100644 --- a/arch/sandbox/cpu/os.c +++ b/arch/sandbox/cpu/os.c @@ -153,7 +153,7 @@ int os_read_file(const char *fname, void **bufp, int *sizep) printf("Cannot seek to start of file '%s'\n", fname); goto err; } - *bufp = malloc(size); + *bufp = os_malloc(size); if (!*bufp) { printf("Not enough memory to read file '%s'\n", fname); ret = -ENOMEM; @@ -391,8 +391,8 @@ int os_parse_args(struct sandbox_state *state, int argc, char *argv[]) state->argv = argv;
/* dynamically construct the arguments to the system getopt_long */ - short_opts = malloc(sizeof(*short_opts) * num_options * 2 + 1); - long_opts = malloc(sizeof(*long_opts) * (num_options + 1)); + short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1); + long_opts = os_malloc(sizeof(*long_opts) * (num_options + 1)); if (!short_opts || !long_opts) return 1;
@@ -471,7 +471,7 @@ void os_dirent_free(struct os_dirent_node *node)
while (node) { next = node->next; - free(node); + os_free(node); node = next; } } @@ -496,7 +496,7 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp) /* Create a buffer upfront, with typically sufficient size */ dirlen = strlen(dirname) + 2; len = dirlen + 256; - fname = malloc(len); + fname = os_malloc(len); if (!fname) { ret = -ENOMEM; goto done; @@ -509,7 +509,7 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp) ret = errno; break; } - next = malloc(sizeof(*node) + strlen(entry->d_name) + 1); + next = os_malloc(sizeof(*node) + strlen(entry->d_name) + 1); if (!next) { os_dirent_free(head); ret = -ENOMEM; @@ -518,10 +518,10 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp) if (dirlen + strlen(entry->d_name) > len) { len = dirlen + strlen(entry->d_name); old_fname = fname; - fname = realloc(fname, len); + fname = os_realloc(fname, len); if (!fname) { - free(old_fname); - free(next); + os_free(old_fname); + os_free(next); os_dirent_free(head); ret = -ENOMEM; goto done; @@ -555,7 +555,7 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
done: closedir(dir); - free(fname); + os_free(fname); return ret; }
@@ -672,7 +672,7 @@ static int add_args(char ***argvp, char *add_args[], int count) for (argc = 0; (*argvp)[argc]; argc++) ;
- argv = malloc((argc + count + 1) * sizeof(char *)); + argv = os_malloc((argc + count + 1) * sizeof(char *)); if (!argv) { printf("Out of memory for %d argv\n", count); return -ENOMEM; @@ -755,7 +755,7 @@ static int os_jump_to_file(const char *fname) os_exit(2);
err = execv(fname, argv); - free(argv); + os_free(argv); if (err) { perror("Unable to run image"); printf("Image filename '%s'\n", fname); diff --git a/arch/sandbox/cpu/spl.c b/arch/sandbox/cpu/spl.c index 9a77da15619..197b98c9828 100644 --- a/arch/sandbox/cpu/spl.c +++ b/arch/sandbox/cpu/spl.c @@ -42,10 +42,14 @@ static int spl_board_load_image(struct spl_image_info *spl_image, return ret; }
- /* Set up spl_image to boot from jump_to_image_no_args() */ - spl_image->arg = strdup(fname); + /* + * Set up spl_image to boot from jump_to_image_no_args(). Allocate this + * outsdide the RAM buffer (i.e. don't use strdup()). + */ + spl_image->arg = os_malloc(strlen(fname) + 1); if (!spl_image->arg) - return log_msg_ret("Setup exec filename", -ENOMEM); + return log_msg_ret("exec", -ENOMEM); + strcpy(spl_image->arg, fname);
return 0; } diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c index 25425809747..295f36c0bda 100644 --- a/arch/sandbox/cpu/start.c +++ b/arch/sandbox/cpu/start.c @@ -87,7 +87,7 @@ int sandbox_early_getopt_check(void)
/* Sort the options */ size = sizeof(*sorted_opt) * num_options; - sorted_opt = malloc(size); + sorted_opt = os_malloc(size); if (!sorted_opt) { printf("No memory to sort options\n"); os_exit(1); @@ -187,7 +187,7 @@ static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state, int len;
len = strlen(state->argv[0]) + strlen(fmt) + 1; - fname = malloc(len); + fname = os_malloc(len); if (!fname) return -ENOMEM; snprintf(fname, len, fmt, state->argv[0]); @@ -207,7 +207,7 @@ static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state, int len;
len = strlen(state->argv[0]) + strlen(fmt) + 1; - fname = malloc(len); + fname = os_malloc(len); if (!fname) return -ENOMEM; strcpy(fname, state->argv[0]); @@ -435,16 +435,18 @@ int main(int argc, char *argv[]) { struct sandbox_state *state; gd_t data; + int size; int ret;
/* * Copy argv[] so that we can pass the arguments in the original * sequence when resetting the sandbox. */ - os_argv = calloc(argc + 1, sizeof(char *)); + size = sizeof(char *) * (argc + 1); + os_argv = os_malloc(size); if (!os_argv) os_exit(1); - memcpy(os_argv, argv, sizeof(char *) * (argc + 1)); + memcpy(os_argv, argv, size);
memset(&data, '\0', sizeof(data)); gd = &data; diff --git a/arch/sandbox/cpu/state.c b/arch/sandbox/cpu/state.c index b2901b7a8ca..4ffaf163789 100644 --- a/arch/sandbox/cpu/state.c +++ b/arch/sandbox/cpu/state.c @@ -29,17 +29,17 @@ static int state_ensure_space(int extra_size) return 0;
size = used + extra_size; - buf = malloc(size); + buf = os_malloc(size); if (!buf) return -ENOMEM;
ret = fdt_open_into(blob, buf, size); if (ret) { - free(buf); + os_free(buf); return -EIO; }
- free(blob); + os_free(blob); state->state_fdt = buf; return 0; } @@ -55,7 +55,7 @@ static int state_read_file(struct sandbox_state *state, const char *fname) printf("Cannot find sandbox state file '%s'\n", fname); return -ENOENT; } - state->state_fdt = malloc(size); + state->state_fdt = os_malloc(size); if (!state->state_fdt) { puts("No memory to read sandbox state\n"); return -ENOMEM; @@ -77,7 +77,7 @@ static int state_read_file(struct sandbox_state *state, const char *fname) err_read: os_close(fd); err_open: - free(state->state_fdt); + os_free(state->state_fdt); state->state_fdt = NULL;
return ret; @@ -244,7 +244,7 @@ int sandbox_write_state(struct sandbox_state *state, const char *fname) /* Create a state FDT if we don't have one */ if (!state->state_fdt) { size = 0x4000; - state->state_fdt = malloc(size); + state->state_fdt = os_malloc(size); if (!state->state_fdt) { puts("No memory to create FDT\n"); return -ENOMEM; @@ -302,7 +302,7 @@ int sandbox_write_state(struct sandbox_state *state, const char *fname) err_write: os_close(fd); err_create: - free(state->state_fdt); + os_free(state->state_fdt);
return ret; } @@ -419,8 +419,8 @@ int state_uninit(void) if (state->jumped_fname) os_unlink(state->jumped_fname);
- if (state->state_fdt) - free(state->state_fdt); + os_free(state->state_fdt); + os_free(state->ram_buf); memset(state, '\0', sizeof(*state));
return 0; diff --git a/include/os.h b/include/os.h index fd010cfee83..d2a4afeca0f 100644 --- a/include/os.h +++ b/include/os.h @@ -123,7 +123,8 @@ void *os_malloc(size_t length); * * This returns the memory to the OS. * - * @ptr: Pointer to memory block to free + * @ptr: Pointer to memory block to free. If this is NULL then this + * function does nothing */ void os_free(void *ptr);

On Sat, Feb 06, 2021 at 09:57:33AM -0700, Simon Glass wrote:
This state is not accessible to the running U-Boot but at present it is allocated in the emulated SDRAM. This doesn't seem very useful. Adjust it to allocate from the OS instead.
The RAM buffer is currently not freed, but should be, so add that into state_uninit(). Update the comment for os_free() to indicate that NULL is a valid parameter value.
Note that the strdup() in spl_board_load_image() is changed as well, since strdup() allocates memory in the RAM buffer.
Signed-off-by: Simon Glass sjg@chromium.org
Applied to u-boot/next, thanks!

On 2/6/21 5:57 PM, Simon Glass wrote:
This state is not accessible to the running U-Boot but at present it is allocated in the emulated SDRAM. This doesn't seem very useful. Adjust it to allocate from the OS instead.
The RAM buffer is currently not freed, but should be, so add that into state_uninit(). Update the comment for os_free() to indicate that NULL is a valid parameter value.
Note that the strdup() in spl_board_load_image() is changed as well, since strdup() allocates memory in the RAM buffer.
Signed-off-by: Simon Glass sjg@chromium.org
This patch was merged as commit b308d9fd18fa4b9b.
It breaks the exception handling:
Before the patch if an exception occured pc_reloc would point to the address in u-boot.map:
=> exception undef Illegal instruction pc = 0x55e4ef74c434, pc_reloc = 0x55434
With your patch pc_reloc is incorrect:
=> exception undef Illegal instruction pc = 0x56003896cd13, pc_reloc = 0x56002896cd13
Why is gd->reloc_off not filled correctly anymore?
os_find_text_base() has this comment:
"This code assumes that the first line of /proc/self/maps holds information about the text."
$ cat /proc/389589/maps 10000000-10002000 rwxp 00000000 00:00 0 560038916000-560038947000 r--p 00000000 fd:01 13376716 560038947000-560038aa4000 r-xp 00031000 fd:01 13376716 560038aa4000-560038ba8000 r--p 0018e000 fd:01 13376716 560038ba8000-560038bb1000 r--p 00291000 fd:01 13376716 560038bb1000-560038bd4000 rw-p 0029a000 fd:01 13376716
=> bdinfo relocaddr = 0x0000000007858000 reloc off = 0x0000000010000000
The problem is that you first call mmap() to allocate space for the state at address 0x10000000 and then call os_find_text_base(). Calling os_find_text_base() must be the first thing you do!
Best regards
Heinrich
(no changes since v2)
Changes in v2:
Drop unnecessary check before calling os_free()
Update the comment for os_free()
Also free the RAM buffer
Convert the rest of the allocations in os.c, etc.
arch/sandbox/cpu/os.c | 24 ++++++++++++------------ arch/sandbox/cpu/spl.c | 10 +++++++--- arch/sandbox/cpu/start.c | 12 +++++++----- arch/sandbox/cpu/state.c | 18 +++++++++--------- include/os.h | 3 ++- 5 files changed, 37 insertions(+), 30 deletions(-)
diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c index d23aad318be..f5000e64966 100644 --- a/arch/sandbox/cpu/os.c +++ b/arch/sandbox/cpu/os.c @@ -153,7 +153,7 @@ int os_read_file(const char *fname, void **bufp, int *sizep) printf("Cannot seek to start of file '%s'\n", fname); goto err; }
- *bufp = malloc(size);
- *bufp = os_malloc(size); if (!*bufp) { printf("Not enough memory to read file '%s'\n", fname); ret = -ENOMEM;
@@ -391,8 +391,8 @@ int os_parse_args(struct sandbox_state *state, int argc, char *argv[]) state->argv = argv;
/* dynamically construct the arguments to the system getopt_long */
- short_opts = malloc(sizeof(*short_opts) * num_options * 2 + 1);
- long_opts = malloc(sizeof(*long_opts) * (num_options + 1));
- short_opts = os_malloc(sizeof(*short_opts) * num_options * 2 + 1);
- long_opts = os_malloc(sizeof(*long_opts) * (num_options + 1)); if (!short_opts || !long_opts) return 1;
@@ -471,7 +471,7 @@ void os_dirent_free(struct os_dirent_node *node)
while (node) { next = node->next;
free(node);
node = next; } }os_free(node);
@@ -496,7 +496,7 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp) /* Create a buffer upfront, with typically sufficient size */ dirlen = strlen(dirname) + 2; len = dirlen + 256;
- fname = malloc(len);
- fname = os_malloc(len); if (!fname) { ret = -ENOMEM; goto done;
@@ -509,7 +509,7 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp) ret = errno; break; }
next = malloc(sizeof(*node) + strlen(entry->d_name) + 1);
if (!next) { os_dirent_free(head); ret = -ENOMEM;next = os_malloc(sizeof(*node) + strlen(entry->d_name) + 1);
@@ -518,10 +518,10 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp) if (dirlen + strlen(entry->d_name) > len) { len = dirlen + strlen(entry->d_name); old_fname = fname;
fname = realloc(fname, len);
fname = os_realloc(fname, len); if (!fname) {
free(old_fname);
free(next);
os_free(old_fname);
os_free(next); os_dirent_free(head); ret = -ENOMEM; goto done;
@@ -555,7 +555,7 @@ int os_dirent_ls(const char *dirname, struct os_dirent_node **headp)
done: closedir(dir);
- free(fname);
- os_free(fname); return ret; }
@@ -672,7 +672,7 @@ static int add_args(char ***argvp, char *add_args[], int count) for (argc = 0; (*argvp)[argc]; argc++) ;
- argv = malloc((argc + count + 1) * sizeof(char *));
- argv = os_malloc((argc + count + 1) * sizeof(char *)); if (!argv) { printf("Out of memory for %d argv\n", count); return -ENOMEM;
@@ -755,7 +755,7 @@ static int os_jump_to_file(const char *fname) os_exit(2);
err = execv(fname, argv);
- free(argv);
- os_free(argv); if (err) { perror("Unable to run image"); printf("Image filename '%s'\n", fname);
diff --git a/arch/sandbox/cpu/spl.c b/arch/sandbox/cpu/spl.c index 9a77da15619..197b98c9828 100644 --- a/arch/sandbox/cpu/spl.c +++ b/arch/sandbox/cpu/spl.c @@ -42,10 +42,14 @@ static int spl_board_load_image(struct spl_image_info *spl_image, return ret; }
- /* Set up spl_image to boot from jump_to_image_no_args() */
- spl_image->arg = strdup(fname);
- /*
* Set up spl_image to boot from jump_to_image_no_args(). Allocate this
* outsdide the RAM buffer (i.e. don't use strdup()).
*/
- spl_image->arg = os_malloc(strlen(fname) + 1); if (!spl_image->arg)
return log_msg_ret("Setup exec filename", -ENOMEM);
return log_msg_ret("exec", -ENOMEM);
strcpy(spl_image->arg, fname);
return 0; }
diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c index 25425809747..295f36c0bda 100644 --- a/arch/sandbox/cpu/start.c +++ b/arch/sandbox/cpu/start.c @@ -87,7 +87,7 @@ int sandbox_early_getopt_check(void)
/* Sort the options */ size = sizeof(*sorted_opt) * num_options;
- sorted_opt = malloc(size);
- sorted_opt = os_malloc(size); if (!sorted_opt) { printf("No memory to sort options\n"); os_exit(1);
@@ -187,7 +187,7 @@ static int sandbox_cmdline_cb_default_fdt(struct sandbox_state *state, int len;
len = strlen(state->argv[0]) + strlen(fmt) + 1;
- fname = malloc(len);
- fname = os_malloc(len); if (!fname) return -ENOMEM; snprintf(fname, len, fmt, state->argv[0]);
@@ -207,7 +207,7 @@ static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state, int len;
len = strlen(state->argv[0]) + strlen(fmt) + 1;
- fname = malloc(len);
- fname = os_malloc(len); if (!fname) return -ENOMEM; strcpy(fname, state->argv[0]);
@@ -435,16 +435,18 @@ int main(int argc, char *argv[]) { struct sandbox_state *state; gd_t data;
int size; int ret;
/*
- Copy argv[] so that we can pass the arguments in the original
- sequence when resetting the sandbox.
*/
- os_argv = calloc(argc + 1, sizeof(char *));
- size = sizeof(char *) * (argc + 1);
- os_argv = os_malloc(size); if (!os_argv) os_exit(1);
- memcpy(os_argv, argv, sizeof(char *) * (argc + 1));
memcpy(os_argv, argv, size);
memset(&data, '\0', sizeof(data)); gd = &data;
diff --git a/arch/sandbox/cpu/state.c b/arch/sandbox/cpu/state.c index b2901b7a8ca..4ffaf163789 100644 --- a/arch/sandbox/cpu/state.c +++ b/arch/sandbox/cpu/state.c @@ -29,17 +29,17 @@ static int state_ensure_space(int extra_size) return 0;
size = used + extra_size;
- buf = malloc(size);
buf = os_malloc(size); if (!buf) return -ENOMEM;
ret = fdt_open_into(blob, buf, size); if (ret) {
free(buf);
return -EIO; }os_free(buf);
- free(blob);
- os_free(blob); state->state_fdt = buf; return 0; }
@@ -55,7 +55,7 @@ static int state_read_file(struct sandbox_state *state, const char *fname) printf("Cannot find sandbox state file '%s'\n", fname); return -ENOENT; }
- state->state_fdt = malloc(size);
- state->state_fdt = os_malloc(size); if (!state->state_fdt) { puts("No memory to read sandbox state\n"); return -ENOMEM;
@@ -77,7 +77,7 @@ static int state_read_file(struct sandbox_state *state, const char *fname) err_read: os_close(fd); err_open:
- free(state->state_fdt);
os_free(state->state_fdt); state->state_fdt = NULL;
return ret;
@@ -244,7 +244,7 @@ int sandbox_write_state(struct sandbox_state *state, const char *fname) /* Create a state FDT if we don't have one */ if (!state->state_fdt) { size = 0x4000;
state->state_fdt = malloc(size);
if (!state->state_fdt) { puts("No memory to create FDT\n"); return -ENOMEM;state->state_fdt = os_malloc(size);
@@ -302,7 +302,7 @@ int sandbox_write_state(struct sandbox_state *state, const char *fname) err_write: os_close(fd); err_create:
- free(state->state_fdt);
os_free(state->state_fdt);
return ret; }
@@ -419,8 +419,8 @@ int state_uninit(void) if (state->jumped_fname) os_unlink(state->jumped_fname);
- if (state->state_fdt)
free(state->state_fdt);
os_free(state->state_fdt);
os_free(state->ram_buf); memset(state, '\0', sizeof(*state));
return 0;
diff --git a/include/os.h b/include/os.h index fd010cfee83..d2a4afeca0f 100644 --- a/include/os.h +++ b/include/os.h @@ -123,7 +123,8 @@ void *os_malloc(size_t length);
- This returns the memory to the OS.
- @ptr: Pointer to memory block to free
- @ptr: Pointer to memory block to free. If this is NULL then this
*/ void os_free(void *ptr);
function does nothing

Sandbox provides a way to write out its emulated memory on exit. This makes it possible to pass a bloblist from one phase (e.g. SPL) to the next.
However the bloblist is not closed off, so the checksum is generally invalid. Fix this by finishing up the bloblist before writing the memory file.
Signed-off-by: Simon Glass sjg@chromium.org ---
Changes in v4: - Fix 'existing' typo
arch/sandbox/cpu/state.c | 5 +++++ 1 file changed, 5 insertions(+)
diff --git a/arch/sandbox/cpu/state.c b/arch/sandbox/cpu/state.c index 4ffaf163789..f63cfd38ee4 100644 --- a/arch/sandbox/cpu/state.c +++ b/arch/sandbox/cpu/state.c @@ -4,6 +4,7 @@ */
#include <common.h> +#include <bloblist.h> #include <errno.h> #include <fdtdec.h> #include <log.h> @@ -398,8 +399,12 @@ int state_uninit(void) { int err;
+ log_info("Writing sandbox state\n"); state = &main_state;
+ /* Finish the bloblist, so that it is correct before writing memory */ + bloblist_finish(); + if (state->write_ram_buf) { err = os_write_ram_buf(state->ram_buf_fname); if (err) {

On Sat, Feb 06, 2021 at 09:57:34AM -0700, Simon Glass wrote:
Sandbox provides a way to write out its emulated memory on exit. This makes it possible to pass a bloblist from one phase (e.g. SPL) to the next.
However the bloblist is not closed off, so the checksum is generally invalid. Fix this by finishing up the bloblist before writing the memory file.
Signed-off-by: Simon Glass sjg@chromium.org
Applied to u-boot/next, thanks!

These two returns use the same string so are not distinguishable with LOG_ERROR_RETURN. Fix it.
Signed-off-by: Simon Glass sjg@chromium.org ---
(no changes since v1)
common/bootm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/common/bootm.c b/common/bootm.c index 8298693900d..48a5b04cd7a 100644 --- a/common/bootm.c +++ b/common/bootm.c @@ -585,7 +585,7 @@ int bootm_process_cmdline(char *buf, int maxlen, int flags) if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && (flags & BOOTM_CL_SUBST)) { ret = process_subst(buf, maxlen); if (ret) - return log_msg_ret("silent", ret); + return log_msg_ret("subst", ret); }
return 0;

On Sat, Feb 06, 2021 at 09:57:35AM -0700, Simon Glass wrote:
These two returns use the same string so are not distinguishable with LOG_ERROR_RETURN. Fix it.
Signed-off-by: Simon Glass sjg@chromium.org
Applied to u-boot/next, thanks!
participants (3)
-
Heinrich Schuchardt
-
Simon Glass
-
Tom Rini