lib: Allow using 0x when a decimal value is requested
authorSimon Glass <sjg@chromium.org>
Sat, 24 Jul 2021 15:03:38 +0000 (09:03 -0600)
committerTom Rini <trini@konsulko.com>
Mon, 2 Aug 2021 17:32:14 +0000 (13:32 -0400)
U-Boot mostly uses hex for value input, largely because addresses are much
easier to understand in hex.

But in some cases a decimal value is requested, such as where the value is
small or hex does not make sense in the context. In these cases it is
sometimes useful to be able to provide a hex value in any case, if only to
resolve any ambiguity.

Add this functionality, for increased flexibility.

Signed-off-by: Simon Glass <sjg@chromium.org>
doc/usage/cmdline.rst
include/vsprintf.h
lib/strto.c
test/str_ut.c

index 2d5127c..88f18c9 100644 (file)
@@ -85,3 +85,9 @@ useful::
   => pmic write 2 0177
   => pmic read 2
   0x02: 0x00007f
+
+It is possible to use a `0x` prefix to use a hex value if that is more
+convenient::
+
+  => i2c speed 0x30000
+  Setting bus speed to 196608 Hz
index debf977..83d187e 100644 (file)
  * the end these are ignored. In the worst case, if all characters are invalid,
  * 0 is returned
  *
- * If @base is 0, octal or hex prefixes are supported (e.g. 0777, 0x123) to
- * select a particular base. By default decimal is used.
+ * A hex prefix is supported (e.g. 0x123) regardless of the value of @base.
+ * If found, the base is set to hex (16).
+ *
+ * If @base is 0:
+ *    - an octal '0' prefix (e.g. 0777) sets the base to octal (8).
+ *    - otherwise the base defaults to decimal (10).
  */
 ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
 
@@ -71,8 +75,12 @@ unsigned long dectoul(const char *cp, char **endp);
  *
  * echo will append a newline to the tail.
  *
- * If @base is 0, octal or hex prefixes are supported (e.g. 0777, 0x123) to
- * select a particular base. By default decimal is used.
+ * A hex prefix is supported (e.g. 0x123) regardless of the value of @base.
+ * If found, the base is set to hex (16).
+ *
+ * If @base is 0:
+ *    - an octal '0' prefix (e.g. 0777) sets the base to octal (8).
+ *    - otherwise the base defaults to decimal (10).
  *
  * Copied this function from Linux 2.6.38 commit ID:
  * 521cb40b0c44418a4fd36dc633f575813d59a43d
index b056b20..7bba1e3 100644 (file)
 #include <linux/ctype.h>
 
 /* from lib/kstrtox.c */
-static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
+static const char *_parse_integer_fixup_radix(const char *s, uint *basep)
 {
-       if (*base == 0) {
-               if (s[0] == '0') {
-                       if (tolower(s[1]) == 'x')
-                               *base = 16;
-                       else
-                               *base = 8;
-               } else
-                       *base = 10;
+       /* Look for a 0x prefix */
+       if (s[0] == '0') {
+               int ch = tolower(s[1]);
+
+               if (ch == 'x') {
+                       *basep = 16;
+                       s += 2;
+               } else if (!*basep) {
+                       /* Only select octal if we don't have a base */
+                       *basep = 8;
+               }
        }
-       if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x')
-               s += 2;
+
+       /* Use decimal by default */
+       if (!*basep)
+               *basep = 10;
+
        return s;
 }
 
index 0d1bf39..d2840d5 100644 (file)
@@ -89,7 +89,7 @@ static int str_simple_strtoul(struct unit_test_state *uts)
                ut_assertok(run_strtoul(uts, str2, 10, 1099, 4, upper));
                ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6, upper));
                ut_assertok(run_strtoul(uts, str3, 16, 0xb, 3, upper));
-               ut_assertok(run_strtoul(uts, str3, 10, 0, 1, upper));
+               ut_assertok(run_strtoul(uts, str3, 10, 0xb, 3, upper));
 
                /* Octal */
                ut_assertok(run_strtoul(uts, str6, 0, 63, 3, upper));
@@ -144,7 +144,7 @@ static int str_simple_strtoull(struct unit_test_state *uts)
                ut_assertok(run_strtoull(uts, str2, 10, 1099, 4, upper));
                ut_assertok(run_strtoull(uts, str2, 16, 0x1099ab, 6, upper));
                ut_assertok(run_strtoull(uts, str3, 16, 0xb, 3, upper));
-               ut_assertok(run_strtoull(uts, str3, 10, 0, 1, upper));
+               ut_assertok(run_strtoull(uts, str3, 10, 0xb, 3, upper));
 
                /* Octal */
                ut_assertok(run_strtoull(uts, str6, 0, 63, 3, upper));