From patchwork Wed Mar 17 18:18: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: 1454927 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=2a01:238:438b:c500:173d:9f52:ddab:ee01; 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=D0QA7wTq; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (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 4F0z4x4VBHz9sVm for ; Thu, 18 Mar 2021 05:21:37 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4B820828B9; Wed, 17 Mar 2021 19:20:19 +0100 (CET) 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="D0QA7wTq"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B5E03828E9; Wed, 17 Mar 2021 19:19:56 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.2 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-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) (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 BD28C8289B for ; Wed, 17 Mar 2021 19:19:51 +0100 (CET) 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-pf1-x42e.google.com with SMTP id g15so1712509pfq.3 for ; Wed, 17 Mar 2021 11:19:51 -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=bC2injzeNQ1s+byxtIYZSqm0eidfWqinDDTukjoUTeY=; b=D0QA7wTq0Grz0bbi218XNpkNyeYr28kFACZAIzkFukpGIWK5f8tIGgZYocpyb3GJL9 ECgD+K0mqHBqwSr6fqsyamZP7QLZkRPTFUjUed8gUh8OEbLiCI2ZIqVQrV2rYQS/aS1+ MhgF/LsqwykNPA06Bm+0foZF4PLqqQ5JDNlxQ= 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=bC2injzeNQ1s+byxtIYZSqm0eidfWqinDDTukjoUTeY=; b=M8EyE+9jd9KLF11avC3N7/Py7ZqkrSxse0Mf8YM9L2AmqkelAhmp/DU8sRWzD8Gut3 RKmvfj+6+1wAvGmKqsXTSkDmGZSSC7MPOQSoNzNIMG4EoTCgv2xMI5K7zQ9Bj2L2UHwP RMPYPfpEqmg72GP6bGVAlaBpnbmBM+9OtO2PqVaus/n81GcmFh+W9sd7HmJRwlAPYrdl Rm9dBLQSkUjuc0RS2uciRDDeeazvMXWTZpLulECQEZTz81A85K1u/VOKqvsoDNf13N0e DOgUk/hDW1DcIL9dpFjuBftpFkqld7muAdtKRsj2QfjbBI6R/mz/W5GajL995M2QrPh2 sEDA== X-Gm-Message-State: AOAM531yhiW2NeS1IL48M9clrbCSTNmzvPVDq+lbOmOUNaB1N0bPMPoZ I14WemE+udebz5MjvgCrqoSEfwrMcnqAwg== X-Google-Smtp-Source: ABdhPJx6AATe6QAr3+isHsbWNEGMhnMxEcCS0a5oWr56BP2/gMjxC+bQn2pimucZZoxEtwKlggxOig== X-Received: by 2002:a05:6a00:2288:b029:202:2103:3f21 with SMTP id f8-20020a056a002288b029020221033f21mr263227pfe.39.1616005189931; Wed, 17 Mar 2021 11:19:49 -0700 (PDT) Received: from sjg1.roam.corp.google.com (85.72.69.111.dynamic.snap.net.nz. [111.69.72.85]) by smtp.gmail.com with ESMTPSA id s76sm20718298pfc.110.2021.03.17.11.19.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Mar 2021 11:19:49 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Simon Glass Subject: [PATCH 11/13] display_options: Split print_buffer() into two functions Date: Thu, 18 Mar 2021 07:18:38 +1300 Message-Id: <20210317181840.3975687-10-sjg@chromium.org> X-Mailer: git-send-email 2.31.0.rc2.261.g7f71774620-goog In-Reply-To: <20210317181840.3975687-1-sjg@chromium.org> References: <20210317181840.3975687-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.102.4 at phobos.denx.de X-Virus-Status: Clean At present print_buffer() outputs a hex dump but it is not possible to place this dump in a string. Refactor it into a top-level function which does the printing and a utility function that dumps a line into a string. This makes the code more generally useful. Signed-off-by: Simon Glass --- include/display_options.h | 25 +++++++++ lib/display_options.c | 115 +++++++++++++++++++++++--------------- test/print_ut.c | 30 +++++++++- 3 files changed, 123 insertions(+), 47 deletions(-) diff --git a/include/display_options.h b/include/display_options.h index 049688e39e8..43810cbe22f 100644 --- a/include/display_options.h +++ b/include/display_options.h @@ -47,6 +47,31 @@ void print_freq(uint64_t freq, const char *suffix); int print_buffer(ulong addr, const void *data, uint width, uint count, uint linelen); +/* + * Maximum length of an output line is when width == 1 + * 9 for address, + * a space, two hex digits and an ASCII character for each byte + * 2 spaces between the hex and ASCII + * \0 terminator + */ +#define HEXDUMP_MAX_BUF_LENGTH(bytes) (9 + (bytes) * 4 + 3) + +/** + * hexdump_line() - Print out a single line of a hex dump + * + * @addr: Starting address to display at start of line + * @data: pointer to data buffer + * @width: data value width. May be 1, 2, or 4. + * @count: number of values to display + * @linelen: Number of values to print per line; specify 0 for default length + * @out: Output buffer to hold the dump + * @size: Size of output buffer in bytes + * @return number of bytes processed, if OK, -ENOSPC if buffer too small + * + */ +int hexdump_line(ulong addr, const void *data, uint width, uint count, + uint linelen, char *out, int size); + /** * display_options() - display the version string / build tag * diff --git a/lib/display_options.c b/lib/display_options.c index 7752baba2bd..c08a87e3162 100644 --- a/lib/display_options.c +++ b/lib/display_options.c @@ -131,10 +131,11 @@ void print_size(uint64_t size, const char *s) printf (" %ciB%s", c, s); } -#define MAX_LINE_LENGTH_BYTES (64) -#define DEFAULT_LINE_LENGTH_BYTES (16) -int print_buffer(ulong addr, const void *data, uint width, uint count, - uint linelen) +#define MAX_LINE_LENGTH_BYTES 64 +#define DEFAULT_LINE_LENGTH_BYTES 16 + +int hexdump_line(ulong addr, const void *data, uint width, uint count, + uint linelen, char *out, int size) { /* linebuf as a union causes proper alignment */ union linebuf { @@ -143,62 +144,86 @@ int print_buffer(ulong addr, const void *data, uint width, uint count, uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1]; uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1]; } lb; + uint thislinelen; int i; ulong x; + if (linelen * width > MAX_LINE_LENGTH_BYTES) + linelen = MAX_LINE_LENGTH_BYTES / width; + if (linelen < 1) + linelen = DEFAULT_LINE_LENGTH_BYTES / width; + + /* + * Check the size here so that we don't need to use snprintf(). This + * helps to reduce code size + */ + if (size < HEXDUMP_MAX_BUF_LENGTH(linelen * width)) + return -ENOSPC; + + thislinelen = linelen; + out += sprintf(out, "%08lx:", addr); + + /* check for overflow condition */ + if (count < thislinelen) + thislinelen = count; + + /* Copy from memory into linebuf and print hex values */ + for (i = 0; i < thislinelen; i++) { + if (width == 4) + x = lb.ui[i] = *(volatile uint32_t *)data; + else if (MEM_SUPPORT_64BIT_DATA && width == 8) + x = lb.uq[i] = *(volatile ulong *)data; + else if (width == 2) + x = lb.us[i] = *(volatile uint16_t *)data; + else + x = lb.uc[i] = *(volatile uint8_t *)data; + if (CONFIG_IS_ENABLED(USE_TINY_PRINTF)) + out += sprintf(out, " %x", (uint)x); + else + out += sprintf(out, " %0*lx", width * 2, x); + data += width; + } + + /* fill line with whitespace for nice ASCII print */ + for (i = 0; i < (linelen - thislinelen) * (width * 2 + 1); i++) + *out++ = ' '; + + /* Print data in ASCII characters */ + for (i = 0; i < thislinelen * width; i++) { + if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80) + lb.uc[i] = '.'; + } + lb.uc[i] = '\0'; + out += sprintf(out, " %s", lb.uc); + + return thislinelen; +} + +int print_buffer(ulong addr, const void *data, uint width, uint count, + uint linelen) +{ if (linelen*width > MAX_LINE_LENGTH_BYTES) linelen = MAX_LINE_LENGTH_BYTES / width; if (linelen < 1) linelen = DEFAULT_LINE_LENGTH_BYTES / width; while (count) { - uint thislinelen = linelen; - printf("%08lx:", addr); - - /* check for overflow condition */ - if (count < thislinelen) - thislinelen = count; - - /* Copy from memory into linebuf and print hex values */ - for (i = 0; i < thislinelen; i++) { - if (width == 4) - x = lb.ui[i] = *(volatile uint32_t *)data; - else if (MEM_SUPPORT_64BIT_DATA && width == 8) - x = lb.uq[i] = *(volatile ulong *)data; - else if (width == 2) - x = lb.us[i] = *(volatile uint16_t *)data; - else - x = lb.uc[i] = *(volatile uint8_t *)data; - if (CONFIG_IS_ENABLED(USE_TINY_PRINTF)) - printf(" %x", (uint)x); - else - printf(" %0*lx", width * 2, x); - data += width; - } + uint thislinelen; + char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)]; - while (thislinelen < linelen) { - /* fill line with whitespace for nice ASCII print */ - for (i=0; i= 0x80) - lb.uc[i] = '.'; - } - lb.uc[i] = '\0'; - printf(" %s\n", lb.uc); + thislinelen = hexdump_line(addr, data, width, count, linelen, + buf, sizeof(buf)); + assert(thislinelen >= 0); + puts(buf); + putc('\n'); /* update references */ + data += thislinelen * width; addr += thislinelen * width; count -= thislinelen; -#ifndef CONFIG_SPL_BUILD - if (ctrlc()) - return -1; -#endif + if (!IS_ENABLED(CONFIG_SPL_BUILD) && ctrlc()) + return -EINTR; } return 0; diff --git a/test/print_ut.c b/test/print_ut.c index 3a3cdf323e1..079d3f7cb08 100644 --- a/test/print_ut.c +++ b/test/print_ut.c @@ -229,7 +229,33 @@ static int print_display_buffer(struct unit_test_state *uts) } PRINT_TEST(print_display_buffer, UT_TESTF_CONSOLE_REC); -static int print_hex_dump(struct unit_test_state *uts) +static int print_hexdump_line(struct unit_test_state *uts) +{ + char *linebuf; + u8 *buf; + int i; + + buf = map_sysmem(0, BUF_SIZE); + memset(buf, '\0', BUF_SIZE); + for (i = 0; i < 0x11; i++) + buf[i] = i * 0x11; + + /* Check buffer size calculations */ + linebuf = map_sysmem(0x400, BUF_SIZE); + memset(linebuf, '\xff', BUF_SIZE); + ut_asserteq(-ENOSPC, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 75)); + ut_asserteq(-1, linebuf[0]); + ut_asserteq(0x10, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 76)); + ut_asserteq(0, linebuf[75]); + ut_asserteq(-1, linebuf[76]); + + unmap_sysmem(buf); + + return 0; +} +PRINT_TEST(print_hexdump_line, UT_TESTF_CONSOLE_REC); + +static int print_hexdump(struct unit_test_state *uts) { u8 *buf; int i; @@ -302,7 +328,7 @@ static int print_hex_dump(struct unit_test_state *uts) return 0; } -PRINT_TEST(print_hex_dump, UT_TESTF_CONSOLE_REC); +PRINT_TEST(print_hexdump, UT_TESTF_CONSOLE_REC); int do_ut_print(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) {