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>
This commit is contained in:
Simon Glass 2021-07-24 09:03:38 -06:00 committed by Tom Rini
parent 5f4b356121
commit e6951139c0
4 changed files with 37 additions and 17 deletions

View file

@ -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;
}