[PATCH 13/15] RFC: lib: Support a binary prefix 0y

Simon Glass
2021-07-20T13:32:57Z

In some cases it is useful to be able to supply a binary value to a
command. Use the '0y' prefix for this (binarY).

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

doc/usage/cmdline.rst | 6 ++++++
lib/strto.c | 3 +++
test/str_ut.c | 8 ++++++++
3 files changed, 17 insertions(+)

diff --git a/doc/usage/cmdline.rst b/doc/usage/cmdline.rst
index 7f2cfad2a0f..05ce65958c4 100644
--- a/doc/usage/cmdline.rst
+++ b/doc/usage/cmdline.rst
@@ -96,3 +96,9 @@ convenient::

=> i2c speed 0x30000
Setting bus speed to 196608 Hz
+
+U-Boot also supports a `0y` for binary base 2 ("binarY")::
+
+ => mw 100 0y11010011
+ => md 100 1
+ 00000100: 000000d3 ....
diff --git a/lib/strto.c b/lib/strto.c
index 120214d83f8..a7531e877f5 100644
--- a/lib/strto.c
+++ b/lib/strto.c
@@ -26,6 +26,9 @@ static const char *_parse_integer_fixup_radix(const char *s, uint *basep)
} else if (ch == 'm') {
*basep = 10;
s += 2;
+ } else if (ch == 'y') {
+ *basep = 2;
+ 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 716f7c45c38..731ef0c49ae 100644
--- a/test/str_ut.c
+++ b/test/str_ut.c
@@ -117,6 +117,10 @@ static int str_simple_strtoul(struct unit_test_state *uts)
ut_assertok(run_strtoul(uts, "0x123fg", 0, 0x123f, 6, false));
ut_assertok(run_strtoul(uts, "0m123a", 16, 123, 5, false));

+ /* check binary */
+ ut_assertok(run_strtoul(uts, "1011b", 2, 0xb, 4, false));
+ ut_assertok(run_strtoul(uts, "0y111more", 0, 7, 5, false));
+
return 0;
}
STR_TEST(str_simple_strtoul, 0);
@@ -186,6 +190,10 @@ static int str_simple_strtoull(struct unit_test_state *uts)
ut_assertok(run_strtoull(uts, "0x123fg", 0, 0x123f, 6, false));
ut_assertok(run_strtoull(uts, "0m123a", 16, 123, 5, false));

+ /* check binary */
+ ut_assertok(run_strtoull(uts, "1011b", 2, 0xb, 4, false));
+ ut_assertok(run_strtoull(uts, "0y111more", 0, 7, 5, false));
+
return 0;
}
STR_TEST(str_simple_strtoull, 0);
--
2.32.0.402.g57bb445576-goog