From patchwork Sat Jul 24 15:03:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1509518 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=LstHxVjv; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GX8fS2szpz9sX1 for ; Sun, 25 Jul 2021 01:06:40 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 99B8C82EB7; Sat, 24 Jul 2021 17:06:37 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="LstHxVjv"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 1C6CA82EB7; Sat, 24 Jul 2021 17:05:11 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ot1-x336.google.com (mail-ot1-x336.google.com [IPv6:2607:f8b0:4864:20::336]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 5E68582F26 for ; Sat, 24 Jul 2021 17:03:59 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-ot1-x336.google.com with SMTP id o2-20020a9d22020000b0290462f0ab0800so436954ota.11 for ; Sat, 24 Jul 2021 08:03:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=unoz4I/qTUyzsX+rhCxTjdrPhhu0wdWeAWZ9VHJ0zg4=; b=LstHxVjvGwXnZnU4B2UiqY1jGEkde/kKVs1vR8BH5B0EGQQV05iZV7U+3jP9OqmUiR iOpv9/GHwGTHeCf4doVmnwlLP07TKeMMxttVKzZnXv447nI4ja+HKar64ryi3fB5RNlh tuY0Bcy+qF9UndcAhojT8QVzaOJfJ6FfnTs5Y= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=unoz4I/qTUyzsX+rhCxTjdrPhhu0wdWeAWZ9VHJ0zg4=; b=hCby2z7m2jeecOj05n76rcU6MrwkM/WKdF9tv1nslWPgRUansonHPsthn5PioF2kv4 zmBkBfgl+bQLeykRV8/do2fXRy4WvXSYUj00dHe0ZZgshMKeRM3Qg8zRvNO5YZzLGDim 6jGuoyhRYO6geynx0zGtCA3X6nnsJqfejqM/PUF6nsaGY8YzFly0sFqVJuo38xjfIHrF VYs7MdJfjT064jmM54rplDDhKyySTIZIgfL3Zc748gZOQcosOu51Amwm7qq1mTqDTxrH +6ZgOrk0wMbInvkr9+mRAL5RO9M48gykpL7cGG5gX33UAiNLBL/0LUy7iSNlrFlaCvQN NtEA== X-Gm-Message-State: AOAM531elCvfzyavJ22l23t8+tts+qAIsHcaDYO7TJybqUg4yrrVDCC+ s5ymqMGvMGx2li2z4Q/ryLbR70wKyj1R2mKV X-Google-Smtp-Source: ABdhPJwVuqdF+M9MVKE/PZOUZ+WIcFZdmuJf/I4P5CecAYefgcyB89NYGwI4o3EDd6kRNLSZxhNKig== X-Received: by 2002:a05:6830:619:: with SMTP id w25mr6388389oti.150.1627139037768; Sat, 24 Jul 2021 08:03:57 -0700 (PDT) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id w190sm6704082oif.17.2021.07.24.08.03.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Jul 2021 08:03:57 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , "Jonathan A . Kollasch" , Marek Vasut , Heinrich Schuchardt , =?utf-8?q?Marek_Beh=C3=BAn?= , Eugeniu Rosca , Roland Gaudig , Stefan Herbrechtsmeier , Sean Anderson , Simon Glass Subject: [PATCH v2 11/14] lib: Allow using 0x when a decimal value is requested Date: Sat, 24 Jul 2021 09:03:38 -0600 Message-Id: <20210724150341.243074-12-sjg@chromium.org> X-Mailer: git-send-email 2.32.0.432.gabb21c7263-goog In-Reply-To: <20210724150341.243074-1-sjg@chromium.org> References: <20210724150341.243074-1-sjg@chromium.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean 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 --- (no changes since v1) doc/usage/cmdline.rst | 6 ++++++ include/vsprintf.h | 16 ++++++++++++---- lib/strto.c | 28 +++++++++++++++++----------- test/str_ut.c | 4 ++-- 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/doc/usage/cmdline.rst b/doc/usage/cmdline.rst index 2d5127c9554..88f18c974c8 100644 --- a/doc/usage/cmdline.rst +++ b/doc/usage/cmdline.rst @@ -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 diff --git a/include/vsprintf.h b/include/vsprintf.h index 0e112b5d5ba..604963dad61 100644 --- a/include/vsprintf.h +++ b/include/vsprintf.h @@ -22,8 +22,12 @@ * 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 diff --git a/lib/strto.c b/lib/strto.c index c3f0f8e8c6c..54ee3e81f6a 100644 --- a/lib/strto.c +++ b/lib/strto.c @@ -14,19 +14,25 @@ #include /* 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; } diff --git a/test/str_ut.c b/test/str_ut.c index 0d1bf398099..d2840d51524 100644 --- a/test/str_ut.c +++ b/test/str_ut.c @@ -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));