summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2021-07-24 18:03:38 +0300
committerTom Rini <trini@konsulko.com>2021-08-02 20:32:14 +0300
commite6951139c0544116330b12e287fe45e30bbab11c (patch)
tree15216d11d6890cd497b6a97176c7aad4c4d63ecf /lib
parent5f4b356121fdb520e4c9bb4fe3483c1a5f93439c (diff)
downloadu-boot-e6951139c0544116330b12e287fe45e30bbab11c.tar.xz
lib: Allow using 0x when a decimal value is requested
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>
Diffstat (limited to 'lib')
-rw-r--r--lib/strto.c28
1 files changed, 17 insertions, 11 deletions
diff --git a/lib/strto.c b/lib/strto.c
index b056b205c8..7bba1e3e54 100644
--- a/lib/strto.c
+++ b/lib/strto.c
@@ -14,19 +14,25 @@
#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;
}