Skip to contentSkip to conversation

cmd: adding malloc, math, and strcmp commands to u-boot

PATCH New Vladimir Olovyannikov 3 participants 6 messages Nov 19, 2019 – Nov 20, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cmd: adding malloc, math, and strcmp commands to u-boot

cmd: adding malloc, math, and strcmp u-boot commands.
- malloc supports allocation of heap memory and free allocated memory via u-boot command line.
- math provides math commands such as "add", "sub", "mul", "div",

          "shift", ability to convert dec->hex.

- strcmp provides string compare command feature for a script.

All these commands are introduced to be used in u-boot scripts.

---
 cmd/Kconfig  | 21 ++++++++++++++
 cmd/Makefile |  4 +++
 cmd/malloc.c | 54 ++++++++++++++++++++++++++++++++++++
 cmd/math.c   | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 cmd/strcmp.c | 28 +++++++++++++++++++
 5 files changed, 185 insertions(+)
 create mode 100644 cmd/malloc.c
 create mode 100644 cmd/math.c
 create mode 100644 cmd/strcmp.c
cmd/Kconfig +21 -0
index cf982ff65e..f11903fe3d 100644
@@ -1286,6 +1286,20 @@ config CMD_ITEST
 	help
 	  Return true/false on integer compare.
 
+config CMD_MALLOC
+	bool "malloc"
+	default y
+	help
+	  Supports allocation of heap memory and free allocated memory commands.
+	  These commands are used by u-boot scripts.
+
+config CMD_MATH
+	bool "math"
+	default y
+	help
+	  Provides math commands such as add, sub, mul, div, shift,
+	  convert decimal to hex functionalities to be available in the script.
+
 config CMD_SOURCE
 	bool "source"
 	default y
@@ -1301,6 +1315,13 @@ config CMD_SETEXPR
 	  Also supports loading the value at a memory location into a variable.
 	  If CONFIG_REGEX is enabled, setexpr also supports a gsub function.
 
+config CMD_STRCMP
+	bool "strcmp"
+	default y
+	help
+	  Provides string compare command feature to u-boot scripts.
+
+
 endmenu
 
 menu "Android support commands"
cmd/Makefile +4 -0
index 2d723ea0f0..942d60a0a2 100644
@@ -164,6 +164,10 @@ obj-$(CONFIG_CMD_GPT) += gpt.o
 obj-$(CONFIG_CMD_ETHSW) += ethsw.o
 obj-$(CONFIG_CMD_AXI) += axi.o
 
+obj-$(CONFIG_CMD_MALLOC) += malloc.o
+obj-$(CONFIG_CMD_MATH) += math.o
+obj-$(CONFIG_CMD_STRCMP) += strcmp.o
+
 # Power
 obj-$(CONFIG_CMD_PMIC) += pmic.o
 obj-$(CONFIG_CMD_REGULATOR) += regulator.o
cmd/malloc.c new file +54 -0

54 lines not on this page · raw message

cmd/math.c new file +78 -0

78 lines not on this page · raw message

cmd/strcmp.c new file +28 -0
new file mode 100644
index 0000000000..3abd270d40
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2010-2017 Broadcom
+ */
+
+#include <common.h>
+#include <command.h>
+
+static int do_strcmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	if (argc != 3)
+		return cmd_usage(cmdtp);
+
+	if (strlen(argv[1]) != strlen(argv[2]))
+		return CMD_RET_FAILURE;
+
+	/* Compare the temporary string to the given parameter */
+	if (!strncmp(argv[1], argv[2], strlen(argv[2])))
+		return CMD_RET_SUCCESS;
+
+	return CMD_RET_FAILURE;
+}
+
+U_BOOT_CMD(strcmp, 3, 0, do_strcmp,
+	   "String to string comparison.",
+	   "[str] [str]\n"
+	   "    - Returns true if str are same, else returns false.\n"
+);
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/1 cmd: adding malloc, math, and strcmp commands to u-boot

Hi Vladimir,

Quoted 2 lines · Vladimir Olovyannikov

cmd: adding malloc, math, and strcmp u-boot commands.

U-Boot

Quoted 2 lines

- malloc supports allocation of heap memory and free allocated memory via u-boot command line.

U-Boot (please fix globally)

Quoted 45 lines

- math provides math commands such as "add", "sub", "mul", "div",

          "shift", ability to convert dec->hex.

- strcmp provides string compare command feature for a script.

All these commands are introduced to be used in u-boot scripts.

---
 cmd/Kconfig  | 21 ++++++++++++++
 cmd/Makefile |  4 +++
 cmd/malloc.c | 54 ++++++++++++++++++++++++++++++++++++
 cmd/math.c   | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 cmd/strcmp.c | 28 +++++++++++++++++++
 5 files changed, 185 insertions(+)
 create mode 100644 cmd/malloc.c
 create mode 100644 cmd/math.c
 create mode 100644 cmd/strcmp.c
cmd/Kconfig +14 -0
index cf982ff65e..f11903fe3d 100644
@@ -1286,6 +1286,20 @@ config CMD_ITEST
        help
          Return true/false on integer compare.
 
+config CMD_MALLOC
+       bool "malloc"
+       default y
+       help
+         Supports allocation of heap memory and free allocated memory commands.
+         These commands are used by u-boot scripts.
+
+config CMD_MATH
+       bool "math"
+       default y
+       help
+         Provides math commands such as add, sub, mul, div, shift,
+         convert decimal to hex functionalities to be available in the script.
+
 config CMD_SOURCE
        bool "source"
        default y

cmd/Kconfig config CMD_SETEXPR · line 1316

Quoted 2 lines
cmd/Kconfig +0 -0
@@ -1301,6 +1315,13 @@ config CMD_SETEXPR
          Also supports loading the value at a memory location into a variable.
          If CONFIG_REGEX is enabled, setexpr also supports a gsub function.

I think this would be better as three patches.

cmd/Kconfig config CMD_SETEXPR · line 1322

Quoted 6 lines
cmd/Kconfig +5 -0
+config CMD_STRCMP
+       bool "strcmp"
+       default y
+       help
+         Provides string compare command feature to u-boot scripts.

U-Boot

Quoted 28 lines
cmd/Kconfig +2 -0
+
+
 endmenu
 
 menu "Android support commands"
cmd/Makefile +4 -0
index 2d723ea0f0..942d60a0a2 100644
@@ -164,6 +164,10 @@ obj-$(CONFIG_CMD_GPT) += gpt.o
 obj-$(CONFIG_CMD_ETHSW) += ethsw.o
 obj-$(CONFIG_CMD_AXI) += axi.o
 
+obj-$(CONFIG_CMD_MALLOC) += malloc.o
+obj-$(CONFIG_CMD_MATH) += math.o
+obj-$(CONFIG_CMD_STRCMP) += strcmp.o
+
 # Power
 obj-$(CONFIG_CMD_PMIC) += pmic.o
 obj-$(CONFIG_CMD_REGULATOR) += regulator.o
cmd/malloc.c new file +2 -0
new file mode 100644
index 0000000000..e11e030a59
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*

cmd/malloc.c line 12

Quoted 10 lines
cmd/malloc.c new file +10 -0
+ * Copyright 2019 Broadcom
+ */
+
+#include <common.h>
+#include <command.h>
+#include <environment.h>
+#include <errno.h>
+#include <malloc.h>
+
+static unsigned long get_value(const char *val)

Needs a comment

cmd/malloc.c line 25

Quoted 13 lines
cmd/malloc.c new file +13 -0
+{
+       char *env = env_get((char *)val);
+
+       if (env)
+               return simple_strtoul(env, NULL, 16);
+       else
+               return simple_strtoul(val, NULL, 16);
+}
+
+static int do_malloc(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
+{
+       char numberbuf[32];
+       void *mem;

const?

cmd/malloc.c line 32

Quoted 7 lines
cmd/malloc.c new file +7 -0
+
+       if (argc < 3)
+               return cmd_usage(cmdtp);
+
+       mem = memalign(ARCH_DMA_MINALIGN, get_value(argv[2]));
+       if (mem) {
+               sprintf(numberbuf, "%08x", (unsigned int)mem);

I think (ulong) would be better

cmd/malloc.c line 33

Quoted 1 line
cmd/malloc.c new file +1 -0
+               env_set(argv[1], numberbuf);

blank line before return (please fix globally)

cmd/malloc.c line 36

Quoted 3 lines
cmd/malloc.c new file +3 -0
+               return 0;
+       }
+       return -EINVAL;

You can't return errors from command functions. Try printing an error and return CMD_RET_FAILURE instead.

Quoted 5 lines
cmd/malloc.c new file +5 -0
+}
+
+static int do_free(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
+{
+       if (argc < 2)

cmd/malloc.c line 51

Quoted 10 lines
cmd/malloc.c new file +10 -0
+               return cmd_usage(cmdtp);
+
+       free((void *)get_value(argv[1]));
+       env_set(argv[1], "");
+       return 0;
+}
+
+U_BOOT_CMD(malloc, 3, 0, do_malloc,
+          "Allocate memory from u-boot heap and store pointer in environment variable.",
+          "target size\n");

Needs better help - e.g. list arguments

cmd/malloc.c

Quoted 3 lines
cmd/malloc.c new file +3 -0
+
+U_BOOT_CMD(free, 2, 0, do_free,
+          "Release memory from u-boot heap at target.", "target\n");

I wonder if it would be better to have a 'mem' command, then have 'mem alloc', 'mem free', etc.?

Quoted 7 lines
cmd/math.c new file +1 -0
new file mode 100644
index 0000000000..17de5ef70b
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: GPL-2.0+

cmd/math.c line 11

Quoted 10 lines
cmd/math.c new file +10 -0
+/*
+ * Copyright 2010-2017 Broadcom
+ */
+
+#include <common.h>
+#include <command.h>
+#include <environment.h>
+
+unsigned long long simple_strtoull(const char *cp, char **endp,
+                                  unsigned int base);

Use #include <vsprintf.h> instead

cmd/math.c line 13

Quoted 2 lines
cmd/math.c new file +2 -0
+
+static unsigned long long get_value(const char *val)

Function comment

Quoted 14 lines
cmd/math.c new file +14 -0
+{
+       char *env = env_get((char *)val);
+
+       if (env)
+               return simple_strtoull(env, NULL, 16);
+       else
+               return simple_strtoull(val, NULL, 16);
+}
+
+static unsigned long long get_value_base10(const char *val)
+{
+       char *env = env_get((char *)val);
+
+       if (env)

cmd/math.c line 37

Quoted 10 lines
cmd/math.c new file +10 -0
+               return simple_strtoull(env, NULL, 10);
+       else
+               return simple_strtoull(val, NULL, 10);
+}
+
+/*
+ * Top level addenv command
+ */
+#define IS_MATH_CMD(cmd, args) ((strcmp(argv[1], cmd) == 0) && \
+                                 (argc == (args)))

Use a function instead of a macro.

cmd/math.c line 45

Quoted 8 lines
cmd/math.c new file +8 -0
+
+static int do_math(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+       char numberbuf[32];
+       unsigned long long i = 0;
+
+       if (IS_MATH_CMD("add", 5))
+               i = get_value(argv[3]) + get_value(argv[4]);

It might be better to do something like:

const char *left = NULL, *right = NULL;

if (argc > 3)
left = argv[3];
...

Then you can use 'left' and 'right' below.

Quoted 12 lines
cmd/math.c new file +12 -0
+       else if (IS_MATH_CMD("sub", 5))
+               i = get_value(argv[3]) - get_value(argv[4]);
+       else if (IS_MATH_CMD("mul", 5))
+               i = get_value(argv[3]) * get_value(argv[4]);
+       else if (IS_MATH_CMD("div", 5))
+               i = get_value(argv[3]) / get_value(argv[4]);
+       else if (IS_MATH_CMD("shl", 5))
+               i = get_value(argv[3]) << get_value(argv[4]);
+       else if (IS_MATH_CMD("d2h", 4))
+               i = get_value_base10(argv[3]);
+       else
+               return cmd_usage(cmdtp);

cmd/math.c line 67

Quoted 10 lines
cmd/math.c new file +10 -0
+
+       sprintf(numberbuf, "%llx", i);
+       env_set(argv[2], numberbuf);
+       return 0;
+}
+
+U_BOOT_CMD(math, 5, 0, do_math,
+          "Environment variable math.",
+          "add a b c\n"
+          "    - add b to c and store in a.\n"

But also b and c can be numbers, right? You should mention that.

Quoted 33 lines
cmd/math.c new file +11 -0
+          "math sub a b c\n"
+          "    - subtract b from c and store in a.\n"
+          "math mul a b c\n"
+          "    - multiply b by c and store in a.\n"
+          "math div a b c\n"
+          "    - divide b by c and store in a.\n"
+          "math shl a b c\n"
+          "    - shift left b by c and store in a.\n"
+          "math d2h a b\n"
+          "    - Convert b from decimal to hex and store in a.\n"
+);
cmd/strcmp.c new file +16 -0
new file mode 100644
index 0000000000..3abd270d40
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2010-2017 Broadcom
+ */
+
+#include <common.h>
+#include <command.h>
+
+static int do_strcmp(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+       if (argc != 3)
+               return cmd_usage(cmdtp);
+
+       if (strlen(argv[1]) != strlen(argv[2]))
+               return CMD_RET_FAILURE;
+

cmd/strcmp.c line 26

Quoted 10 lines
cmd/strcmp.c new file +10 -0
+       /* Compare the temporary string to the given parameter */
+       if (!strncmp(argv[1], argv[2], strlen(argv[2])))
+               return CMD_RET_SUCCESS;
+
+       return CMD_RET_FAILURE;
+}
+
+U_BOOT_CMD(strcmp, 3, 0, do_strcmp,
+          "String to string comparison.",
+          "[str] [str]\n"

str1 str2, I think

Quoted 5 lines
cmd/strcmp.c new file +2 -1
+          "    - Returns true if str are same, else returns false.\n"
+);
--

2.17.1

Regards,
Simon

#2 lore raw

PATCH 1/1 cmd: adding malloc, math, and strcmp commands to u-boot

Hi Simon,
Thank you for reviewing. I will split the patch as you suggested, and resubmit.

Vladimir

Quoted 335 lines

-----Original Message-----
From: Simon Glass [mailto:sjg at chromium.org]
Sent: Monday, November 18, 2019 5:21 PM
To: Vladimir Olovyannikov <[email protected]>
Cc: U-Boot Mailing List <[email protected]>; Heinrich Schuchardt <[email protected]>; Suji Velupiallai <[email protected]>
Subject: Re: [PATCH v1 1/1] cmd: adding malloc, math, and strcmp commands to u-boot

Hi Vladimir,

Quoted 2 lines · Vladimir Olovyannikov

cmd: adding malloc, math, and strcmp u-boot commands.

U-Boot

Quoted 2 lines

- malloc supports allocation of heap memory and free allocated memory via u-boot command line.

U-Boot (please fix globally)

Quoted 6 lines

- math provides math commands such as "add", "sub", "mul", "div",

          "shift", ability to convert dec->hex.

- strcmp provides string compare command feature for a script.

All these commands are introduced to be used in u-boot scripts.

Quoted 8 lines
Signed-off-by Vladimir Olovyannikov

<[email protected]>

---
 cmd/Kconfig  | 21 ++++++++++++++
 cmd/Makefile |  4 +++
 cmd/malloc.c | 54 ++++++++++++++++++++++++++++++++++++
 cmd/math.c   | 78

++++++++++++++++++++++++++++++++++++++++++++++++++++

Quoted 11 lines
 cmd/strcmp.c | 28 +++++++++++++++++++
 5 files changed, 185 insertions(+)
 create mode 100644 cmd/malloc.c
 create mode 100644 cmd/math.c
 create mode 100644 cmd/strcmp.c
cmd/Kconfig index cf982ff65e..f11903fe3d renamed from cmd/Kconfig +0 -0

100644

cmd/Kconfig config CMD_ITEST · line 1293

Quoted 8 lines
cmd/Kconfig +5 -0
@@ -1286,6 +1286,20 @@ config CMD_ITEST
        help
          Return true/false on integer compare.
 
+config CMD_MALLOC
+       bool "malloc"
+       default y
+       help
+         Supports allocation of heap memory and free allocated memory

commands.

Quoted 15 lines
cmd/Kconfig +8 -0
+         These commands are used by u-boot scripts.
+
+config CMD_MATH
+       bool "math"
+       default y
+       help
+         Provides math commands such as add, sub, mul, div, shift,
+         convert decimal to hex functionalities to be available in the

script.

cmd/Kconfig +1 -0
+
 config CMD_SOURCE
        bool "source"
        default y
@@ -1301,6 +1315,13 @@ config CMD_SETEXPR
          Also supports loading the value at a memory location into a
Quoted 3 lines

variable.
If CONFIG_REGEX is enabled, setexpr also supports a gsub
function.

I think this would be better as three patches.

cmd/Kconfig config CMD_SETEXPR · line 1322

Quoted 6 lines
cmd/Kconfig +5 -0
+config CMD_STRCMP
+       bool "strcmp"
+       default y
+       help
+         Provides string compare command feature to u-boot scripts.

U-Boot

Quoted 26 lines
cmd/Kconfig +2 -0
+
+
 endmenu
 
 menu "Android support commands"
cmd/Makefile index 2d723ea0f0..942d60a0a2 renamed from cmd/Makefile +0 -0

100644

cmd/Makefile +4 -0
@@ -164,6 +164,10 @@ obj-$(CONFIG_CMD_GPT) += gpt.o
 obj-$(CONFIG_CMD_ETHSW) += ethsw.o
 obj-$(CONFIG_CMD_AXI) += axi.o
 
+obj-$(CONFIG_CMD_MALLOC) += malloc.o
+obj-$(CONFIG_CMD_MATH) += math.o
+obj-$(CONFIG_CMD_STRCMP) += strcmp.o
+
 # Power
 obj-$(CONFIG_CMD_PMIC) += pmic.o
 obj-$(CONFIG_CMD_REGULATOR) += regulator.o diff --git a/cmd/malloc.c

b/cmd/malloc.c new file mode 100644 index 0000000000..e11e030a59

cmd/malloc.c new file +2 -0
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*

cmd/malloc.c line 12

Quoted 10 lines
cmd/malloc.c new file +10 -0
+ * Copyright 2019 Broadcom
+ */
+
+#include <common.h>
+#include <command.h>
+#include <environment.h>
+#include <errno.h>
+#include <malloc.h>
+
+static unsigned long get_value(const char *val)

Needs a comment

cmd/malloc.c line 25

Quoted 12 lines
cmd/malloc.c new file +12 -0
+{
+       char *env = env_get((char *)val);
+
+       if (env)
+               return simple_strtoul(env, NULL, 16);
+       else
+               return simple_strtoul(val, NULL, 16); }
+
+static int do_malloc(cmd_tbl_t *cmdtp, int flag, int argc, char
+*const argv[]) {
+       char numberbuf[32];
+       void *mem;

const?

cmd/malloc.c line 32

Quoted 7 lines
cmd/malloc.c new file +7 -0
+
+       if (argc < 3)
+               return cmd_usage(cmdtp);
+
+       mem = memalign(ARCH_DMA_MINALIGN, get_value(argv[2]));
+       if (mem) {
+               sprintf(numberbuf, "%08x", (unsigned int)mem);

I think (ulong) would be better

cmd/malloc.c line 33

Quoted 1 line
cmd/malloc.c new file +1 -0
+               env_set(argv[1], numberbuf);

blank line before return (please fix globally)

cmd/malloc.c line 36

Quoted 3 lines
cmd/malloc.c new file +3 -0
+               return 0;
+       }
+       return -EINVAL;

You can't return errors from command functions. Try printing an error and return CMD_RET_FAILURE instead.

Quoted 4 lines
cmd/malloc.c new file +4 -0
+}
+
+static int do_free(cmd_tbl_t *cmdtp, int flag, int argc, char *const
+argv[]) {

cmd/malloc.c

Quoted 10 lines
cmd/malloc.c new file +10 -0
+       if (argc < 2)
+               return cmd_usage(cmdtp);
+
+       free((void *)get_value(argv[1]));
+       env_set(argv[1], "");
+       return 0;
+}
+
+U_BOOT_CMD(malloc, 3, 0, do_malloc,
+          "Allocate memory from u-boot heap and store pointer in

environment variable.",

cmd/malloc.c line 51

Quoted 1 line
cmd/malloc.c new file +1 -0
+          "target size\n");

Needs better help - e.g. list arguments

cmd/malloc.c

Quoted 3 lines
cmd/malloc.c new file +3 -0
+
+U_BOOT_CMD(free, 2, 0, do_free,
+          "Release memory from u-boot heap at target.", "target\n");

I wonder if it would be better to have a 'mem' command, then have 'mem alloc', 'mem free', etc.?

Quoted 7 lines
cmd/math.c new file +1 -0
new file mode 100644
index 0000000000..17de5ef70b
@@ -0,0 +1,78 @@
+// SPDX-License-Identifier: GPL-2.0+

cmd/math.c line 11

Quoted 10 lines
cmd/math.c new file +10 -0
+/*
+ * Copyright 2010-2017 Broadcom
+ */
+
+#include <common.h>
+#include <command.h>
+#include <environment.h>
+
+unsigned long long simple_strtoull(const char *cp, char **endp,
+                                  unsigned int base);

Use #include <vsprintf.h> instead

cmd/math.c line 13

Quoted 2 lines
cmd/math.c new file +2 -0
+
+static unsigned long long get_value(const char *val)

Function comment

Quoted 11 lines
cmd/math.c new file +11 -0
+{
+       char *env = env_get((char *)val);
+
+       if (env)
+               return simple_strtoull(env, NULL, 16);
+       else
+               return simple_strtoull(val, NULL, 16); }
+
+static unsigned long long get_value_base10(const char *val) {
+       char *env = env_get((char *)val);
+

cmd/math.c line 37

Quoted 10 lines
cmd/math.c new file +10 -0
+       if (env)
+               return simple_strtoull(env, NULL, 10);
+       else
+               return simple_strtoull(val, NULL, 10); }
+
+/*
+ * Top level addenv command
+ */
+#define IS_MATH_CMD(cmd, args) ((strcmp(argv[1], cmd) == 0) && \
+                                 (argc == (args)))

Use a function instead of a macro.

cmd/math.c line 45

Quoted 8 lines
cmd/math.c new file +8 -0
+
+static int do_math(cmd_tbl_t *cmdtp, int flag, int argc, char * const
+argv[]) {
+       char numberbuf[32];
+       unsigned long long i = 0;
+
+       if (IS_MATH_CMD("add", 5))
+               i = get_value(argv[3]) + get_value(argv[4]);

It might be better to do something like:

const char *left = NULL, *right = NULL;

if (argc > 3)
left = argv[3];
...

Then you can use 'left' and 'right' below.

Quoted 12 lines
cmd/math.c new file +12 -0
+       else if (IS_MATH_CMD("sub", 5))
+               i = get_value(argv[3]) - get_value(argv[4]);
+       else if (IS_MATH_CMD("mul", 5))
+               i = get_value(argv[3]) * get_value(argv[4]);
+       else if (IS_MATH_CMD("div", 5))
+               i = get_value(argv[3]) / get_value(argv[4]);
+       else if (IS_MATH_CMD("shl", 5))
+               i = get_value(argv[3]) << get_value(argv[4]);
+       else if (IS_MATH_CMD("d2h", 4))
+               i = get_value_base10(argv[3]);
+       else
+               return cmd_usage(cmdtp);

cmd/math.c line 67

Quoted 10 lines
cmd/math.c new file +10 -0
+
+       sprintf(numberbuf, "%llx", i);
+       env_set(argv[2], numberbuf);
+       return 0;
+}
+
+U_BOOT_CMD(math, 5, 0, do_math,
+          "Environment variable math.",
+          "add a b c\n"
+          "    - add b to c and store in a.\n"

But also b and c can be numbers, right? You should mention that.

Quoted 32 lines
cmd/math.c new file +11 -0
+          "math sub a b c\n"
+          "    - subtract b from c and store in a.\n"
+          "math mul a b c\n"
+          "    - multiply b by c and store in a.\n"
+          "math div a b c\n"
+          "    - divide b by c and store in a.\n"
+          "math shl a b c\n"
+          "    - shift left b by c and store in a.\n"
+          "math d2h a b\n"
+          "    - Convert b from decimal to hex and store in a.\n"
+);
cmd/strcmp.c new file mode 100644 index renamed from cmd/strcmp.c +0 -0

0000000000..3abd270d40

cmd/strcmp.c new file +16 -0
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2010-2017 Broadcom
+ */
+
+#include <common.h>
+#include <command.h>
+
+static int do_strcmp(cmd_tbl_t *cmdtp, int flag, int argc, char *
+const argv[]) {
+       if (argc != 3)
+               return cmd_usage(cmdtp);
+
+       if (strlen(argv[1]) != strlen(argv[2]))
+               return CMD_RET_FAILURE;
+

cmd/strcmp.c line 26

Quoted 10 lines
cmd/strcmp.c new file +10 -0
+       /* Compare the temporary string to the given parameter */
+       if (!strncmp(argv[1], argv[2], strlen(argv[2])))
+               return CMD_RET_SUCCESS;
+
+       return CMD_RET_FAILURE;
+}
+
+U_BOOT_CMD(strcmp, 3, 0, do_strcmp,
+          "String to string comparison.",
+          "[str] [str]\n"

str1 str2, I think

Quoted 5 lines
cmd/strcmp.c new file +2 -1
+          "    - Returns true if str are same, else returns false.\n"
+);
--

2.17.1

Regards,
Simon

#3 lore raw

PATCH 1/1 cmd: adding malloc, math, and strcmp commands to u-boot

In reply to Vladimir Olovyannikov
Quoted 3 lines · Vladimir Olovyannikov

cmd: adding malloc, math, and strcmp u-boot commands.
- malloc supports allocation of heap memory and free allocated memory via u-boot command line.

Can you expand on how this is used in a script? I'm not sure I see that exactly. Also:

cmd/Kconfig config CMD_ITEST · line 1301

Quoted 13 lines
cmd/Kconfig +13 -0
+config CMD_MALLOC
+	bool "malloc"
+	default y
+	help
+	  Supports allocation of heap memory and free allocated memory commands.
+	  These commands are used by u-boot scripts.
+
+config CMD_MATH
+	bool "math"
+	default y
+	help
+	  Provides math commands such as add, sub, mul, div, shift,
+	  convert decimal to hex functionalities to be available in the script.

First, why do we need this, rather than using setexpr ?

cmd/Kconfig config CMD_SETEXPR · line 1322

Quoted 13 lines
cmd/Kconfig +6 -0
+
 config CMD_SOURCE
 	bool "source"
 	default y
@@ -1301,6 +1315,13 @@ config CMD_SETEXPR
 	  Also supports loading the value at a memory location into a variable.
 	  If CONFIG_REGEX is enabled, setexpr also supports a gsub function.
 
+config CMD_STRCMP
+	bool "strcmp"
+	default y
+	help
+	  Provides string compare command feature to u-boot scripts.

Second, new commands must not default to y, but they should be enabled on sandbox and new test.py tests added for them. Thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20191119/a7f5af39/attachment.sig>
#4 lore raw

PATCH 1/1 cmd: adding malloc, math, and strcmp commands to u-boot

Hi Tom,

Quoted 5 lines

-----Original Message-----
From: Tom Rini [mailto:trini at konsulko.com]
Sent: Tuesday, November 19, 2019 12:38 PM
To: Vladimir Olovyannikov <[email protected]>
Cc: u-boot at lists.denx.de; Suji Velupiallai

<[email protected]>;

Quoted 2 lines

Heinrich Schuchardt <[email protected]>
Subject: Re: [U-Boot] [PATCH v1 1/1] cmd: adding malloc, math, and

strcmp

Quoted 10 lines

commands to u-boot

Quoted 3 lines · Vladimir Olovyannikov

cmd: adding malloc, math, and strcmp u-boot commands.
- malloc supports allocation of heap memory and free allocated memory via u-boot command line.

Can you expand on how this is used in a script? I'm not sure I see that exactly.

I am upstreaming the new bcm platform. Here is an excerpt from the script which uses
these:

#define SD_UPDATE \
	"sd_update="\
	"if malloc tmp 2000; then "\
	"else "\
		"echo [sd_update] malloc 2000 bytes ** FAILED **;"\
		"exit;"\
	"fi;"\
	"if fatload mmc ${sd_device_number} ${tmp} "\
	"${sd_update_prefix}.sd-update; then "\
	"else "\
		"echo [sd_update] fatload ${sd_update_prefix}.sd-update "\
		"** FAILED **;"\
		"exit;"\
	"fi;"\
	"if source ${tmp}; then "\
	"else "\
		"echo [sd_update] Executing script ** FAILED **;"\
		"exit;"\
	"fi;"\
	"if free tmp; then "\
	"else " \
		"echo [sd_update] free 2000 bytes ** FAILED **;"\
		"exit;"\
	"fi \0"
;
	"if math add filesize filesize 1FF; then "\
	"else "\
		"echo [mmc_flash_image_rsa] math add command ** FAILED
**;"\
		"exit;"\
	"fi;"\
	"if math div fileblocks filesize 200; then "\
	"else "\
		"echo [mmc_flash_image_rsa] math div command ** FAILED
**;"\
		"exit;"\
	"fi;"\
Quoted 7 lines

Also:

cmd/Kconfig config CMD_ITEST

Quoted 5 lines
cmd/Kconfig +5 -0
+config CMD_MALLOC
+	bool "malloc"
+	default y
+	help
+	  Supports allocation of heap memory and free allocated memory

commands.

Quoted 8 lines

cmd/Kconfig config CMD_ITEST

Quoted 8 lines
cmd/Kconfig +8 -0
+	  These commands are used by u-boot scripts.
+
+config CMD_MATH
+	bool "math"
+	default y
+	help
+	  Provides math commands such as add, sub, mul, div, shift,
+	  convert decimal to hex functionalities to be available in the

script.

Quoted 2 lines

First, why do we need this, rather than using setexpr ?

I agree, the platform needs to use setexpr as it contains all math operations.

Quoted 7 lines

cmd/Kconfig config CMD_SETEXPR · line 1315

Quoted 7 lines
cmd/Kconfig +1 -0
+
 config CMD_SOURCE
 	bool "source"
 	default y
@@ -1301,6 +1315,13 @@ config CMD_SETEXPR
 	  Also supports loading the value at a memory location into a

variable.

Quoted 1 line

Review config CMD_SETEXPR · line 1316

Quoted 1 line
(quoted diff) +0 -0
 	  If CONFIG_REGEX is enabled, setexpr also supports a gsub

function.

Quoted 9 lines

cmd/Kconfig config CMD_SETEXPR · line 1322

Quoted 6 lines
cmd/Kconfig +5 -0
+config CMD_STRCMP
+	bool "strcmp"
+	default y
+	help
+	  Provides string compare command feature to u-boot scripts.

Second, new commands must not default to y, but they should be enabled on sandbox and new test.py tests added for them. Thanks!

OK, thank you.
Is there an example/doc on how to enable these on sandbox and to provide tests?

Vladimir

Quoted 3 lines

--
Tom

#5 lore raw

PATCH 1/1 cmd: adding malloc, math, and strcmp commands to u-boot

Quoted 53 lines · Vladimir Olovyannikov

Hi Tom,

Quoted 5 lines

-----Original Message-----
From: Tom Rini [mailto:trini at konsulko.com]
Sent: Tuesday, November 19, 2019 12:38 PM
To: Vladimir Olovyannikov <[email protected]>
Cc: u-boot at lists.denx.de; Suji Velupiallai

<[email protected]>;

Quoted 2 lines

Heinrich Schuchardt <[email protected]>
Subject: Re: [U-Boot] [PATCH v1 1/1] cmd: adding malloc, math, and

strcmp

Quoted 10 lines

commands to u-boot

Quoted 3 lines · Vladimir Olovyannikov

cmd: adding malloc, math, and strcmp u-boot commands.
- malloc supports allocation of heap memory and free allocated memory via u-boot command line.

Can you expand on how this is used in a script? I'm not sure I see that exactly.

I am upstreaming the new bcm platform. Here is an excerpt from the script which uses
these:

#define SD_UPDATE \
	"sd_update="\
	"if malloc tmp 2000; then "\
	"else "\
		"echo [sd_update] malloc 2000 bytes ** FAILED **;"\
		"exit;"\
	"fi;"\
	"if fatload mmc ${sd_device_number} ${tmp} "\
	"${sd_update_prefix}.sd-update; then "\
	"else "\
		"echo [sd_update] fatload ${sd_update_prefix}.sd-update "\
		"** FAILED **;"\
		"exit;"\
	"fi;"\
	"if source ${tmp}; then "\
	"else "\
		"echo [sd_update] Executing script ** FAILED **;"\
		"exit;"\
	"fi;"\
	"if free tmp; then "\
	"else " \
		"echo [sd_update] free 2000 bytes ** FAILED **;"\
		"exit;"\
	"fi \0"
;
	"if math add filesize filesize 1FF; then "\
	"else "\
		"echo [mmc_flash_image_rsa] math add command ** FAILED
**;"\
Quoted 8 lines
		"exit;"\
	"fi;"\
	"if math div fileblocks filesize 200; then "\
	"else "\
		"echo [mmc_flash_image_rsa] math div command ** FAILED
**;"\
		"exit;"\
	"fi;"\

Ah, interesting. So you're malloc'ing the memory location that you load in to rather than the usual method of using known variables and locations for the SoC. I would discourage this as we generally do not have a large malloc pool available.

Quoted 39 lines
Quoted 7 lines

Also:

cmd/Kconfig config CMD_ITEST

Quoted 5 lines
cmd/Kconfig +5 -0
+config CMD_MALLOC
+	bool "malloc"
+	default y
+	help
+	  Supports allocation of heap memory and free allocated memory

commands.

Quoted 8 lines

cmd/Kconfig config CMD_ITEST

Quoted 8 lines
cmd/Kconfig +8 -0
+	  These commands are used by u-boot scripts.
+
+config CMD_MATH
+	bool "math"
+	default y
+	help
+	  Provides math commands such as add, sub, mul, div, shift,
+	  convert decimal to hex functionalities to be available in the

script.

Quoted 2 lines

First, why do we need this, rather than using setexpr ?

I agree, the platform needs to use setexpr as it contains all math operations.

Quoted 7 lines

cmd/Kconfig config CMD_SETEXPR · line 1315

Quoted 7 lines
cmd/Kconfig +1 -0
+
 config CMD_SOURCE
 	bool "source"
 	default y
@@ -1301,6 +1315,13 @@ config CMD_SETEXPR
 	  Also supports loading the value at a memory location into a

variable.

Quoted 1 line

Review config CMD_SETEXPR · line 1316

Quoted 1 line
(quoted diff) +0 -0
 	  If CONFIG_REGEX is enabled, setexpr also supports a gsub

function.

Quoted 9 lines

cmd/Kconfig config CMD_SETEXPR · line 1322

Quoted 6 lines
cmd/Kconfig +5 -0
+config CMD_STRCMP
+	bool "strcmp"
+	default y
+	help
+	  Provides string compare command feature to u-boot scripts.

Second, new commands must not default to y, but they should be enabled on sandbox and new test.py tests added for them. Thanks!

Quoted 3 lines

OK, thank you.
Is there an example/doc on how to enable these on sandbox and to provide tests?

Unfortunately there is not (but would be greatly appreciated). There are a lot of examples under test/py/ however. Thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20191120/c4bfcfbb/attachment.sig>