From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5F53C433EF for ; Mon, 27 Sep 2021 15:29:46 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 40AF560E94 for ; Mon, 27 Sep 2021 15:29:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 40AF560E94 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 05BDC832AD; Mon, 27 Sep 2021 17:29:41 +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="WlCR8STR"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id A60FD827C5; Mon, 27 Sep 2021 17:25:17 +0200 (CEST) Received: from mail-il1-x135.google.com (mail-il1-x135.google.com [IPv6:2607:f8b0:4864:20::135]) (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 1901882F34 for ; Mon, 27 Sep 2021 17:24:20 +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-il1-x135.google.com with SMTP id i13so19690806ilm.4 for ; Mon, 27 Sep 2021 08:24:19 -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=r5eINe6iuFZN4YOUSIov0k6HSkqRhEt91zez+GJrXmQ=; b=WlCR8STRHyS8PZg+60MEJSzYrhDKQOHQQDs6e4ozGdoAp9FsulW1ATv7ilTuLxxBkY X36pG+nDEFYeQyczjKxbG1Y+odZQ53/Mehenf1Fptt+y/cbZIGnM+mp+W4Fk/bEQOzYp j1y/Suo4PSmAQ09XIsUEBpBzBBeXgDeQPah8U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=r5eINe6iuFZN4YOUSIov0k6HSkqRhEt91zez+GJrXmQ=; b=FoKbUicD8xvKOmW7iAfjGz711SnAnkuMdP+x2l0C23qjSNd/8xOjAjwqgtx5I7woQT vIBlZLDgzaoayZKTMKd3qfRfrnT44OBLkogg4QI987haByIbguxsf1KJXRBO/5E5k2AL Gz4PBgyinWRMpj0CkmO2yZRIaAZje4SGyHVz6Sqw+DOfI4R2scfOkuQz7syU4l7lKiqf b8ayi3SFW3IOg9LrNXjy6g+XuuVtfMVPbOvy6+9AwbTVIQycjGyDUY2YgJ89vbYxmvFh 8B6ZYrbK6RyNiApXi4aU8IJbT1S1gONweiL/exKCqUE3exL/mYEAt5NoauabCiuYgNwa Rw+A== X-Gm-Message-State: AOAM531aNIoKFZU6ERatnkguMgCZXqKD7gnjKT+YiLZ8TK3gi7pOimME VLl3BMFHfCABVvicWRHXSbupOtLHUNAB7A== X-Google-Smtp-Source: ABdhPJz/WBwNTsqKEgYO5rokeJqGOsXxI9iywgmAFas6pi+sUJaVZqvMfUhPK3bIwaK8RoOHWvQlUw== X-Received: by 2002:a05:6e02:1a22:: with SMTP id g2mr431957ile.135.1632756256128; Mon, 27 Sep 2021 08:24:16 -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 c14sm5587793ioz.21.2021.09.27.08.24.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 08:24:15 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Patrice Chotard , Tom Rini , Simon Glass Subject: [PATCH v2 14/18] lib: Add a function to convert a string to a hex value Date: Mon, 27 Sep 2021 09:23:56 -0600 Message-Id: <20210927092350.v2.14.Ifcd170c380694dd653290f0eb044b279c3593df4@changeid> X-Mailer: git-send-email 2.33.0.685.g46640cef36-goog In-Reply-To: <20210927152400.433569-1-sjg@chromium.org> References: <20210927152400.433569-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Add an xtoa() function, similar to itoa() but for hex instead. Signed-off-by: Simon Glass --- (no changes since v1) include/vsprintf.h | 16 ++++++++++++++-- lib/vsprintf.c | 20 ++++++++++++++++++-- test/print_ut.c | 24 ++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/include/vsprintf.h b/include/vsprintf.h index 4479df0af3f..b4746301462 100644 --- a/include/vsprintf.h +++ b/include/vsprintf.h @@ -177,14 +177,26 @@ int vsprintf(char *buf, const char *fmt, va_list args); * simple_itoa() - convert an unsigned integer to a string * * This returns a static string containing the decimal representation of the - * given value. The returned value may be overwritten by other calls to the - * same function, so should be used immediately + * given value. The returned value may be overwritten by other calls to other + * simple_... functions, so should be used immediately * * @val: Value to convert * @return string containing the decimal representation of @val */ char *simple_itoa(ulong val); +/** + * simple_xtoa() - convert an unsigned integer to a hex string + * + * This returns a static string containing the hexadecimal representation of the + * given value. The returned value may be overwritten by other calls to other + * simple_... functions, so should be used immediately + * + * @val: Value to convert + * @return string containing the hexecimal representation of @val + */ +char *simple_xtoa(ulong num); + /** * Format a string and place it in a buffer * diff --git a/lib/vsprintf.c b/lib/vsprintf.c index d7ee35b4773..e634bd70b66 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -816,11 +816,12 @@ int vprintf(const char *fmt, va_list args) } #endif +static char local_toa[22]; + char *simple_itoa(ulong i) { /* 21 digits plus null terminator, good for 64-bit or smaller ints */ - static char local[22]; - char *p = &local[21]; + char *p = &local_toa[21]; *p-- = '\0'; do { @@ -830,6 +831,21 @@ char *simple_itoa(ulong i) return p + 1; } +char *simple_xtoa(ulong num) +{ + /* 16 digits plus nul terminator, good for 64-bit or smaller ints */ + char *p = &local_toa[17]; + + *--p = '\0'; + do { + p -= 2; + hex_byte_pack(p, num & 0xff); + num >>= 8; + } while (num > 0); + + return p; +} + /* We don't seem to have %'d in U-Boot */ void print_grouped_ull(unsigned long long int_val, int digits) { diff --git a/test/print_ut.c b/test/print_ut.c index 4fbb15b6d3c..152a8c3334f 100644 --- a/test/print_ut.c +++ b/test/print_ut.c @@ -335,16 +335,40 @@ static int print_itoa(struct unit_test_state *uts) ut_asserteq_str("0", simple_itoa(0)); ut_asserteq_str("2147483647", simple_itoa(0x7fffffff)); ut_asserteq_str("4294967295", simple_itoa(0xffffffff)); + + /* Use #ifdef here to avoid a compiler warning on 32-bit machines */ +#ifdef CONFIG_PHYS_64BIT if (sizeof(ulong) == 8) { ut_asserteq_str("9223372036854775807", simple_itoa((1UL << 63) - 1)); ut_asserteq_str("18446744073709551615", simple_itoa(-1)); } +#endif /* CONFIG_PHYS_64BIT */ return 0; } PRINT_TEST(print_itoa, 0); +static int print_xtoa(struct unit_test_state *uts) +{ + ut_asserteq_str("7f", simple_xtoa(127)); + ut_asserteq_str("00", simple_xtoa(0)); + ut_asserteq_str("7fffffff", simple_xtoa(0x7fffffff)); + ut_asserteq_str("ffffffff", simple_xtoa(0xffffffff)); + + /* Use #ifdef here to avoid a compiler warning on 32-bit machines */ +#ifdef CONFIG_PHYS_64BIT + if (sizeof(ulong) == 8) { + ut_asserteq_str("7fffffffffffffff", + simple_xtoa((1UL << 63) - 1)); + ut_asserteq_str("ffffffffffffffff", simple_xtoa(-1)); + } +#endif /* CONFIG_PHYS_64BIT */ + + return 0; +} +PRINT_TEST(print_xtoa, 0); + int do_ut_print(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { struct unit_test *tests = UNIT_TEST_SUITE_START(print_test); -- 2.33.0.685.g46640cef36-goog