[PATCH 12/15] lib: Support a decimal prefix 0m

Simon Glass sjg at chromium.org
Tue Jul 20 15:29:36 CEST 2021


U-Boot mostly uses hex for value input, largely because addresses are much
easier to understand in hex.

However sometimes it is useful to be able to supply a decimal value when a
hex value is expected.

Add this functionality, for increased flexibility.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 doc/usage/cmdline.rst |  7 +++++++
 include/vsprintf.h    |  6 ++++--
 lib/strto.c           |  3 +++
 test/str_ut.c         | 12 ++++++++++++
 4 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/doc/usage/cmdline.rst b/doc/usage/cmdline.rst
index 3228be37bb7..7f2cfad2a0f 100644
--- a/doc/usage/cmdline.rst
+++ b/doc/usage/cmdline.rst
@@ -63,6 +63,13 @@ use of addresses, for example::
   00001000: 2c786f62 00697073 03000000 0c000000  box,spi.........
   00001010: 67020000 00000000                    ...g....
 
+In these cases it is possible to use a `0m` prefix ('deciMal') to use a decimal
+value if that is more convenient. For example, this shows 19 bytes (0x13)::
+
+  => md.b 1000 0m19
+  00001000: 62 6f 78 2c 73 70 69 00 00 00 00 03 00 00 00 0c  box,spi.........
+  00001010: 00 00 02                                         ...
+
 There is no need to add a `0x` prefix to the arguments and the output is shown
 in hex also, without any prefixes. This helps to avoid clutter.
 
diff --git a/include/vsprintf.h b/include/vsprintf.h
index 604963dad61..91822ecbe6a 100644
--- a/include/vsprintf.h
+++ b/include/vsprintf.h
@@ -23,7 +23,8 @@
  * 0 is returned
  *
  * A hex prefix is supported (e.g. 0x123) regardless of the value of @base.
- * If found, the base is set to hex (16).
+ * If found, the base is set to hex (16). Similarly a decimal prefix (e.g. 0d12)
+ * causes the base to be set to decimal (10).
  *
  * If @base is 0:
  *    - an octal '0' prefix (e.g. 0777) sets the base to octal (8).
@@ -76,7 +77,8 @@ unsigned long dectoul(const char *cp, char **endp);
  * echo will append a newline to the tail.
  *
  * A hex prefix is supported (e.g. 0x123) regardless of the value of @base.
- * If found, the base is set to hex (16).
+ * If found, the base is set to hex (16). Similarly a decimal prefix (e.g. 0d12)
+ * causes the base to be set to decimal (10).
  *
  * If @base is 0:
  *    - an octal '0' prefix (e.g. 0777) sets the base to octal (8).
diff --git a/lib/strto.c b/lib/strto.c
index 54ee3e81f6a..120214d83f8 100644
--- a/lib/strto.c
+++ b/lib/strto.c
@@ -23,6 +23,9 @@ static const char *_parse_integer_fixup_radix(const char *s, uint *basep)
 		if (ch == 'x') {
 			*basep = 16;
 			s += 2;
+		} else if (ch == 'm') {
+			*basep = 10;
+			s += 2;
 		} else if (!*basep) {
 			/* Only select octal if we don't have a base */
 			*basep = 8;
diff --git a/test/str_ut.c b/test/str_ut.c
index d2840d51524..716f7c45c38 100644
--- a/test/str_ut.c
+++ b/test/str_ut.c
@@ -111,6 +111,12 @@ static int str_simple_strtoul(struct unit_test_state *uts)
 	/* Check endp being NULL */
 	ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
 
+	/* check decimal */
+	ut_assertok(run_strtoul(uts, "123fg", 0, 123, 3, false));
+	ut_assertok(run_strtoul(uts, "123a", 10, 123, 3, false));
+	ut_assertok(run_strtoul(uts, "0x123fg", 0, 0x123f, 6, false));
+	ut_assertok(run_strtoul(uts, "0m123a", 16, 123, 5, false));
+
 	return 0;
 }
 STR_TEST(str_simple_strtoul, 0);
@@ -174,6 +180,12 @@ static int str_simple_strtoull(struct unit_test_state *uts)
 	/* Check endp being NULL */
 	ut_asserteq(1099, simple_strtoull(str2, NULL, 0));
 
+	/* check decimal */
+	ut_assertok(run_strtoull(uts, "123fg", 0, 123, 3, false));
+	ut_assertok(run_strtoull(uts, "123a", 10, 123, 3, false));
+	ut_assertok(run_strtoull(uts, "0x123fg", 0, 0x123f, 6, false));
+	ut_assertok(run_strtoull(uts, "0m123a", 16, 123, 5, false));
+
 	return 0;
 }
 STR_TEST(str_simple_strtoull, 0);
-- 
2.32.0.402.g57bb445576-goog



More information about the U-Boot mailing list