Re: [PATCH v15 1/6] x86/boot: Copy kstrtoull() to boot/string.c instead of using simple_strtoull()

From: Borislav Petkov
Date: Wed Jan 09 2019 - 07:49:05 EST


On Mon, Jan 07, 2019 at 11:22:38AM +0800, Chao Fan wrote:
> Copy kstrtoull() and necessary functions from lib/kstrtox.c to
> boot/string.c so that code in boot/ can use kstrtoull() and the old
> simple_strtoull() can be replaced.
>
> In boot/string.c, using div_u64() from math64.h directly will cause the
> dividend handled as 64-bit value and bring ld error. The solution is to
> separate the dividend to upper and lower in boot/string.o. So copy the
> useful div_u64() and div_u64_rem() to boot/string.c also. To avoid
> redefinition in i386, rename them as __div_u64() and __div_u64_rem().
>
> Signed-off-by: Chao Fan <fanc.fnst@xxxxxxxxxxxxxx>
> ---
> arch/x86/boot/string.c | 137 +++++++++++++++++++++++++++++++++++++++++
> arch/x86/boot/string.h | 2 +
> 2 files changed, 139 insertions(+)

...

> +static inline char _tolower(const char c)
> +{
> + return c | 0x20;
> +}
> +
> +const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)

static

> +{
> + if (*base == 0) {
> + if (s[0] == '0') {
> + if (_tolower(s[1]) == 'x' && isxdigit(s[2]))
> + *base = 16;
> + else
> + *base = 8;
> + } else
> + *base = 10;
> + }
> + if (*base == 16 && s[0] == '0' && _tolower(s[1]) == 'x')
> + s += 2;
> + return s;
> +}
> +
> +/*
> + * Convert non-negative integer string representation in explicitly given radix
> + * to an integer.
> + * Return number of characters consumed maybe or-ed with overflow bit.
> + * If overflow occurs, result integer (incorrect) is still returned.
> + *
> + * Don't you dare use this function.
> + */
> +unsigned int _parse_integer(const char *s, unsigned int base, unsigned long long *p)

static

> +{
> + unsigned long long res;
> + unsigned int rv;
> +
> + res = 0;
> + rv = 0;
> + while (1) {
> + unsigned int c = *s;
> + unsigned int lc = c | 0x20; /* don't tolower() this line */
> + unsigned int val;
> +
> + if ('0' <= c && c <= '9')
> + val = c - '0';
> + else if ('a' <= lc && lc <= 'f')
> + val = lc - 'a' + 10;
> + else
> + break;
> +
> + if (val >= base)
> + break;
> + /*
> + * Check for overflow only if we are within range of
> + * it in the max base we support (16)
> + */
> + if (unlikely(res & (~0ull << 60))) {
> + if (res > __div_u64(ULLONG_MAX - val, base))
> + rv |= KSTRTOX_OVERFLOW;
> + }
> + res = res * base + val;
> + rv++;
> + s++;
> + }
> + *p = res;
> + return rv;
> +}


--
Regards/Gruss,
Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.