From patchwork Wed May 8 07:01:41 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alastair D'Silva X-Patchwork-Id: 10934473 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4E531924 for ; Wed, 8 May 2019 07:03:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3A91028892 for ; Wed, 8 May 2019 07:03:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2E34B28901; Wed, 8 May 2019 07:03:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B71F328892 for ; Wed, 8 May 2019 07:03:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Message-Id:MIME-Version:References: In-Reply-To:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=sfvnVJ5Byx+3tT7pZ0taml8Pcj85Qp/YhZARPvmkrnc=; b=DBYRXWG2fulpPW iJG2YBoooiCgn3w5XkVqIfmIu457o0ibxquGjJ2QNf8t8acCYqcsgMRZr+4bnSGXaNXKk4diA5RiB kXgXuJBydIZOasu0+NgCVpddMKC5qXwdDmS5PvUh+IUB/9cKOREogUqBlt9gZ5nvVmCFn3WSJCqAW 1Ag7eRpXkcZ1kWjBkYsQ3HaIxfxC8kuKWIZ7gosxBTmJvkJFHo70dyC9wYMI6OIrc1aJiTSkg9b6W Kb8y85WIUFCr0rQhcck8oQCRECUXK2qRjTDSiXagT0bW2C5NIgQNz8ykekI3XQK2kZRHlZ3XgEgyt 6VZMra2x8JUm94q9z7Fw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hOGbQ-0005hc-Az; Wed, 08 May 2019 07:02:56 +0000 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hOGbK-0005d9-4J for ath10k@lists.infradead.org; Wed, 08 May 2019 07:02:51 +0000 Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x48727EW051381 for ; Wed, 8 May 2019 03:02:49 -0400 Received: from e06smtp03.uk.ibm.com (e06smtp03.uk.ibm.com [195.75.94.99]) by mx0a-001b2d01.pphosted.com with ESMTP id 2sbs0tuxqc-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 08 May 2019 03:02:48 -0400 Received: from localhost by e06smtp03.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 8 May 2019 08:02:46 +0100 Received: from b06cxnps3074.portsmouth.uk.ibm.com (9.149.109.194) by e06smtp03.uk.ibm.com (192.168.101.133) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Wed, 8 May 2019 08:02:39 +0100 Received: from d06av24.portsmouth.uk.ibm.com (mk.ibm.com [9.149.105.60]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x4872cK750200764 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 8 May 2019 07:02:38 GMT Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 18D2242047; Wed, 8 May 2019 07:02:38 +0000 (GMT) Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 64E874204B; Wed, 8 May 2019 07:02:37 +0000 (GMT) Received: from ozlabs.au.ibm.com (unknown [9.192.253.14]) by d06av24.portsmouth.uk.ibm.com (Postfix) with ESMTP; Wed, 8 May 2019 07:02:37 +0000 (GMT) Received: from adsilva.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id 1AAB5A03AA; Wed, 8 May 2019 17:02:35 +1000 (AEST) From: "Alastair D'Silva" To: alastair@d-silva.org Subject: [PATCH v2 1/7] lib/hexdump.c: Fix selftests Date: Wed, 8 May 2019 17:01:41 +1000 X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190508070148.23130-1-alastair@au1.ibm.com> References: <20190508070148.23130-1-alastair@au1.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 x-cbid: 19050807-0012-0000-0000-000003196F9F X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19050807-0013-0000-0000-00002151EFD5 Message-Id: <20190508070148.23130-2-alastair@au1.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-05-08_05:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=950 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905080046 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190508_000250_175966_96085404 X-CRM114-Status: GOOD ( 24.69 ) X-BeenThere: ath10k@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-fbdev@vger.kernel.org, Stanislaw Gruszka , Petr Mladek , David Airlie , Joonas Lahtinen , dri-devel@lists.freedesktop.org, devel@driverdev.osuosl.org, linux-scsi@vger.kernel.org, Jassi Brar , ath10k@lists.infradead.org, intel-gfx@lists.freedesktop.org, Dan Carpenter , Jose Abreu , Tom Lendacky , "James E.J. Bottomley" , Jani Nikula , linux-fsdevel@vger.kernel.org, Steven Rostedt , Rodrigo Vivi , Benson Leung , Kalle Valo , Karsten Keil , "Martin K. Petersen" , Greg Kroah-Hartman , linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky , David Laight , Daniel Vetter , netdev@vger.kernel.org, Enric Balletbo i Serra , Andrew Morton , "David S. Miller" , Alexander Viro Sender: "ath10k" Errors-To: ath10k-bounces+patchwork-ath10k=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Alastair D'Silva The overflow tests did not account for the situation where no overflow occurs and len < rowsize. This patch renames the cryptic variables and accounts for the above case. The selftests now pass. Signed-off-by: Alastair D'Silva --- lib/test_hexdump.c | 47 ++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/lib/test_hexdump.c b/lib/test_hexdump.c index 5144899d3c6b..d78ddd62ffd0 100644 --- a/lib/test_hexdump.c +++ b/lib/test_hexdump.c @@ -163,45 +163,52 @@ static void __init test_hexdump_overflow(size_t buflen, size_t len, { char test[TEST_HEXDUMP_BUF_SIZE]; char buf[TEST_HEXDUMP_BUF_SIZE]; - int rs = rowsize, gs = groupsize; - int ae, he, e, f, r; - bool a; + int ascii_len, hex_len, expected_len, fill_point, ngroups, rc; + bool match; total_tests++; memset(buf, FILL_CHAR, sizeof(buf)); - r = hex_dump_to_buffer(data_b, len, rs, gs, buf, buflen, ascii); + rc = hex_dump_to_buffer(data_b, len, rowsize, groupsize, buf, buflen, ascii); /* * Caller must provide the data length multiple of groupsize. The * calculations below are made with that assumption in mind. */ - ae = rs * 2 /* hex */ + rs / gs /* spaces */ + 1 /* space */ + len /* ascii */; - he = (gs * 2 /* hex */ + 1 /* space */) * len / gs - 1 /* no trailing space */; + ngroups = rowsize / groupsize; + hex_len = (groupsize * 2 /* hex */ + 1 /* spaces */) * ngroups + - 1 /* no trailing space */; + ascii_len = hex_len + 2 /* space */ + len /* ascii */; + + if (len < rowsize) { + ngroups = len / groupsize; + hex_len = (groupsize * 2 /* hex */ + 1 /* spaces */) * ngroups + - 1 /* no trailing space */; + } - if (ascii) - e = ae; - else - e = he; + expected_len = (ascii) ? ascii_len : hex_len; - f = min_t(int, e + 1, buflen); + fill_point = min_t(int, expected_len + 1, buflen); if (buflen) { - test_hexdump_prepare_test(len, rs, gs, test, sizeof(test), ascii); - test[f - 1] = '\0'; + test_hexdump_prepare_test(len, rowsize, groupsize, test, + sizeof(test), ascii); + test[fill_point - 1] = '\0'; } - memset(test + f, FILL_CHAR, sizeof(test) - f); + memset(test + fill_point, FILL_CHAR, sizeof(test) - fill_point); - a = r == e && !memcmp(test, buf, TEST_HEXDUMP_BUF_SIZE); + match = rc == expected_len && !memcmp(test, buf, TEST_HEXDUMP_BUF_SIZE); buf[sizeof(buf) - 1] = '\0'; - if (!a) { - pr_err("Len: %zu buflen: %zu strlen: %zu\n", - len, buflen, strnlen(buf, sizeof(buf))); - pr_err("Result: %d '%s'\n", r, buf); - pr_err("Expect: %d '%s'\n", e, test); + if (!match) { + pr_err("rowsize: %u groupsize: %u ascii: %d Len: %zu buflen: %zu strlen: %zu\n", + rowsize, groupsize, ascii, len, buflen, + strnlen(buf, sizeof(buf))); + pr_err("Result: %d '%-.*s'\n", rc, (int)buflen, buf); + pr_err("Expect: %d '%-.*s'\n", expected_len, (int)buflen, test); failed_tests++; + } } From patchwork Wed May 8 07:01:42 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alastair D'Silva X-Patchwork-Id: 10934487 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A1DF792A for ; Wed, 8 May 2019 07:03:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8E9CF28892 for ; Wed, 8 May 2019 07:03:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 824DE288EF; Wed, 8 May 2019 07:03:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D049A28892 for ; Wed, 8 May 2019 07:03:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Message-Id:MIME-Version:References: In-Reply-To:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=KkT6mUpZaPzkdz559BQMkxYEK8ntupIbEQyaim/Za7s=; b=Cg3uVUVTiVjRcF t2Fu7HU1QjzURaeQHL0QqGKDBiMD/K2oTeS32yQSQTNp4BTe5h45UGgyKq07umAFBGyl1cIadW6oS Z83uR1IDUVXdQ88Vce71m8TLT/0OTnCSOmgj0iKHDkobqPkCcBrb4mcXJzkGUw1KJV71odsuxVUF5 5pfWeuynXQn8WTdBEEHJM2My+raG+m0UqkZv1Ct/C9xygc38jLYutUD2QmP8owWEXTyW7viH1JYYm 0ywvQ3pG3nGESIw+pGhabpLcldXr51B+eicnsyN8ZZV+utlCu1FQhul3pRHYNTmZtNCAjTnd7LqnE TAwQ/sw0stw5k4Figvng==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hOGbY-0005sX-S9; Wed, 08 May 2019 07:03:04 +0000 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hOGbL-0005eM-LF for ath10k@lists.infradead.org; Wed, 08 May 2019 07:02:54 +0000 Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x48726Rr051306 for ; Wed, 8 May 2019 03:02:51 -0400 Received: from e06smtp07.uk.ibm.com (e06smtp07.uk.ibm.com [195.75.94.103]) by mx0a-001b2d01.pphosted.com with ESMTP id 2sbs0tuxt1-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 08 May 2019 03:02:51 -0400 Received: from localhost by e06smtp07.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 8 May 2019 08:02:48 +0100 Received: from b06cxnps3075.portsmouth.uk.ibm.com (9.149.109.195) by e06smtp07.uk.ibm.com (192.168.101.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Wed, 8 May 2019 08:02:39 +0100 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x4872cXS60227740 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 8 May 2019 07:02:38 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1117511C050; Wed, 8 May 2019 07:02:38 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 644D911C05B; Wed, 8 May 2019 07:02:37 +0000 (GMT) Received: from ozlabs.au.ibm.com (unknown [9.192.253.14]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Wed, 8 May 2019 07:02:37 +0000 (GMT) Received: from adsilva.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id 25E83A03B1; Wed, 8 May 2019 17:02:35 +1000 (AEST) From: "Alastair D'Silva" To: alastair@d-silva.org Subject: [PATCH v2 2/7] lib/hexdump.c: Relax rowsize checks in hex_dump_to_buffer Date: Wed, 8 May 2019 17:01:42 +1000 X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190508070148.23130-1-alastair@au1.ibm.com> References: <20190508070148.23130-1-alastair@au1.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 x-cbid: 19050807-0028-0000-0000-0000036B7178 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19050807-0029-0000-0000-0000242AED6D Message-Id: <20190508070148.23130-3-alastair@au1.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-05-08_05:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905080046 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190508_000251_797506_4D862F70 X-CRM114-Status: GOOD ( 32.34 ) X-BeenThere: ath10k@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-fbdev@vger.kernel.org, Stanislaw Gruszka , Petr Mladek , David Airlie , Joonas Lahtinen , dri-devel@lists.freedesktop.org, devel@driverdev.osuosl.org, linux-scsi@vger.kernel.org, Jassi Brar , ath10k@lists.infradead.org, intel-gfx@lists.freedesktop.org, Dan Carpenter , Jose Abreu , Tom Lendacky , "James E.J. Bottomley" , Jani Nikula , linux-fsdevel@vger.kernel.org, Steven Rostedt , Rodrigo Vivi , Benson Leung , Kalle Valo , Karsten Keil , "Martin K. Petersen" , Greg Kroah-Hartman , linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky , David Laight , Daniel Vetter , netdev@vger.kernel.org, Enric Balletbo i Serra , Andrew Morton , "David S. Miller" , Alexander Viro Sender: "ath10k" Errors-To: ath10k-bounces+patchwork-ath10k=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Alastair D'Silva This patch removes the hardcoded row limits and allows for other lengths. These lengths must still be a multiple of groupsize. This allows structs that are not 16/32 bytes to display on a single line. This patch also expands the self-tests to test row sizes up to 64 bytes (though they can now be arbitrarily long). Signed-off-by: Alastair D'Silva --- lib/hexdump.c | 48 ++++++++++++++++++++++++++++-------------- lib/test_hexdump.c | 52 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 75 insertions(+), 25 deletions(-) diff --git a/lib/hexdump.c b/lib/hexdump.c index 81b70ed37209..3943507bc0e9 100644 --- a/lib/hexdump.c +++ b/lib/hexdump.c @@ -12,6 +12,7 @@ #include #include #include +#include #include const char hex_asc[] = "0123456789abcdef"; @@ -80,14 +81,15 @@ EXPORT_SYMBOL(bin2hex); * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory * @buf: data blob to dump * @len: number of bytes in the @buf - * @rowsize: number of bytes to print per line; must be 16 or 32 + * @rowsize: number of bytes to print per line; must be a multiple of groupsize * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1) * @linebuf: where to put the converted data * @linebuflen: total size of @linebuf, including space for terminating NUL * @ascii: include ASCII after the hex output * - * hex_dump_to_buffer() works on one "line" of output at a time, i.e., - * 16 or 32 bytes of input data converted to hex + ASCII output. + * hex_dump_to_buffer() works on one "line" of output at a time, converting + * bytes of input to hexadecimal (and optionally printable ASCII) + * until bytes have been emitted. * * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data * to a hex + ASCII dump at the supplied memory location. @@ -116,16 +118,17 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, int ascii_column; int ret; - if (rowsize != 16 && rowsize != 32) - rowsize = 16; - - if (len > rowsize) /* limit to one line at a time */ - len = rowsize; if (!is_power_of_2(groupsize) || groupsize > 8) groupsize = 1; if ((len % groupsize) != 0) /* no mixed size output */ groupsize = 1; + if (rowsize % groupsize) + rowsize -= rowsize % groupsize; + + if (len > rowsize) /* limit to one line at a time */ + len = rowsize; + ngroups = len / groupsize; ascii_column = rowsize * 2 + rowsize / groupsize + 1; @@ -216,7 +219,7 @@ EXPORT_SYMBOL(hex_dump_to_buffer); * caller supplies trailing spaces for alignment if desired * @prefix_type: controls whether prefix of an offset, address, or none * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE) - * @rowsize: number of bytes to print per line; must be 16 or 32 + * @rowsize: number of bytes to print per line; must be a multiple of groupsize * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1) * @buf: data blob to dump * @len: number of bytes in the @buf @@ -226,10 +229,9 @@ EXPORT_SYMBOL(hex_dump_to_buffer); * to the kernel log at the specified kernel log level, with an optional * leading prefix. * - * print_hex_dump() works on one "line" of output at a time, i.e., - * 16 or 32 bytes of input data converted to hex + ASCII output. * print_hex_dump() iterates over the entire input @buf, breaking it into - * "line size" chunks to format and print. + * lines of rowsize/groupsize groups of input data converted to hex + + * (optionally) ASCII output. * * E.g.: * print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS, @@ -246,17 +248,29 @@ void print_hex_dump(const char *level, const char *prefix_str, int prefix_type, { const u8 *ptr = buf; int i, linelen, remaining = len; - unsigned char linebuf[32 * 3 + 2 + 32 + 1]; + unsigned char *linebuf; + unsigned int linebuf_len; - if (rowsize != 16 && rowsize != 32) - rowsize = 16; + if (rowsize % groupsize) + rowsize -= rowsize % groupsize; + + /* Worst case line length: + * 2 hex chars + space per byte in, 2 spaces, 1 char per byte in, NULL + */ + linebuf_len = rowsize * 3 + 2 + rowsize + 1; + linebuf = kzalloc(linebuf_len, GFP_KERNEL); + if (!linebuf) { + printk("%s%shexdump: Could not alloc %u bytes for buffer\n", + level, prefix_str, linebuf_len); + return; + } for (i = 0; i < len; i += rowsize) { linelen = min(remaining, rowsize); remaining -= rowsize; hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize, - linebuf, sizeof(linebuf), ascii); + linebuf, linebuf_len, ascii); switch (prefix_type) { case DUMP_PREFIX_ADDRESS: @@ -271,6 +285,8 @@ void print_hex_dump(const char *level, const char *prefix_str, int prefix_type, break; } } + + kfree(linebuf); } EXPORT_SYMBOL(print_hex_dump); diff --git a/lib/test_hexdump.c b/lib/test_hexdump.c index d78ddd62ffd0..6ab75a209b43 100644 --- a/lib/test_hexdump.c +++ b/lib/test_hexdump.c @@ -14,15 +14,25 @@ static const unsigned char data_b[] = { '\x70', '\xba', '\xc4', '\x24', '\x7d', '\x83', '\x34', '\x9b', /* 08 - 0f */ '\xa6', '\x9c', '\x31', '\xad', '\x9c', '\x0f', '\xac', '\xe9', /* 10 - 17 */ '\x4c', '\xd1', '\x19', '\x99', '\x43', '\xb1', '\xaf', '\x0c', /* 18 - 1f */ + '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', /* 20 - 27 */ + '\x0f', '\x0e', '\x0d', '\x0c', '\x0b', '\x0a', '\x09', '\x08', /* 28 - 2f */ + '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', /* 30 - 37 */ + '\x1f', '\x1e', '\x1d', '\x1c', '\x1b', '\x1a', '\x19', '\x18', /* 38 - 3f */ }; -static const unsigned char data_a[] = ".2.{....p..$}.4...1.....L...C..."; +static const unsigned char data_a[] = ".2.{....p..$}.4...1.....L...C..." + "................................"; static const char * const test_data_1[] __initconst = { "be", "32", "db", "7b", "0a", "18", "93", "b2", "70", "ba", "c4", "24", "7d", "83", "34", "9b", "a6", "9c", "31", "ad", "9c", "0f", "ac", "e9", "4c", "d1", "19", "99", "43", "b1", "af", "0c", + "00", "01", "02", "03", "04", "05", "06", "07", + "0f", "0e", "0d", "0c", "0b", "0a", "09", "08", + "10", "11", "12", "13", "14", "15", "16", "17", + "1f", "1e", "1d", "1c", "1b", "1a", "19", "18", + NULL }; static const char * const test_data_2_le[] __initconst = { @@ -30,6 +40,11 @@ static const char * const test_data_2_le[] __initconst = { "ba70", "24c4", "837d", "9b34", "9ca6", "ad31", "0f9c", "e9ac", "d14c", "9919", "b143", "0caf", + "0100", "0302", "0504", "0706", + "0e0f", "0c0d", "0a0b", "0809", + "1110", "1312", "1514", "1716", + "1e1f", "1c1d", "1a1b", "1819", + NULL }; static const char * const test_data_2_be[] __initconst = { @@ -37,26 +52,43 @@ static const char * const test_data_2_be[] __initconst = { "70ba", "c424", "7d83", "349b", "a69c", "31ad", "9c0f", "ace9", "4cd1", "1999", "43b1", "af0c", + "0001", "0203", "0405", "0607", + "0f0e", "0d0c", "0b0a", "0908", + "1011", "1213", "1415", "1617", + "1f1e", "1d1c", "1b1a", "1918", + NULL }; static const char * const test_data_4_le[] __initconst = { "7bdb32be", "b293180a", "24c4ba70", "9b34837d", "ad319ca6", "e9ac0f9c", "9919d14c", "0cafb143", + "03020100", "07060504", "0c0d0e0f", "08090a0b", + "13121110", "17161514", "1c1d1e1f", "18191a1b", + NULL }; static const char * const test_data_4_be[] __initconst = { "be32db7b", "0a1893b2", "70bac424", "7d83349b", "a69c31ad", "9c0face9", "4cd11999", "43b1af0c", + "00010203", "04050607", "0f0e0d0c", "0b0a0908", + "10111213", "14151617", "1f1e1d1c", "1b1a1918", + NULL }; static const char * const test_data_8_le[] __initconst = { "b293180a7bdb32be", "9b34837d24c4ba70", "e9ac0f9cad319ca6", "0cafb1439919d14c", + "0706050403020100", "08090a0b0c0d0e0f", + "1716151413121110", "18191a1b1c1d1e1f", + NULL }; static const char * const test_data_8_be[] __initconst = { "be32db7b0a1893b2", "70bac4247d83349b", "a69c31ad9c0face9", "4cd1199943b1af0c", + "0001020304050607", "0f0e0d0c0b0a0908", + "1011121314151617", "1f1e1d1c1b1a1918", + NULL }; #define FILL_CHAR '#' @@ -75,9 +107,6 @@ static void __init test_hexdump_prepare_test(size_t len, int rowsize, unsigned int i; const bool is_be = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN); - if (rs != 16 && rs != 32) - rs = 16; - if (l > rs) l = rs; @@ -97,7 +126,12 @@ static void __init test_hexdump_prepare_test(size_t len, int rowsize, p = test; for (i = 0; i < l / gs; i++) { const char *q = *result++; - size_t amount = strlen(q); + size_t amount; + + if (!q) + break; + + amount = strlen(q); memcpy(p, q, amount); p += amount; @@ -120,7 +154,7 @@ static void __init test_hexdump_prepare_test(size_t len, int rowsize, *p = '\0'; } -#define TEST_HEXDUMP_BUF_SIZE (32 * 3 + 2 + 32 + 1) +#define TEST_HEXDUMP_BUF_SIZE (64 * 3 + 2 + 64 + 1) static void __init test_hexdump(size_t len, int rowsize, int groupsize, bool ascii) @@ -215,7 +249,7 @@ static void __init test_hexdump_overflow(size_t buflen, size_t len, static void __init test_hexdump_overflow_set(size_t buflen, bool ascii) { unsigned int i = 0; - int rs = (get_random_int() % 2 + 1) * 16; + int rs = (get_random_int() % 4 + 1) * 16; do { int gs = 1 << i; @@ -230,11 +264,11 @@ static int __init test_hexdump_init(void) unsigned int i; int rowsize; - rowsize = (get_random_int() % 2 + 1) * 16; + rowsize = (get_random_int() % 4 + 1) * 16; for (i = 0; i < 16; i++) test_hexdump_set(rowsize, false); - rowsize = (get_random_int() % 2 + 1) * 16; + rowsize = (get_random_int() % 4 + 1) * 16; for (i = 0; i < 16; i++) test_hexdump_set(rowsize, true); From patchwork Wed May 8 07:01:43 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alastair D'Silva X-Patchwork-Id: 10934525 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 198AA924 for ; Wed, 8 May 2019 07:04:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0339827F3E for ; Wed, 8 May 2019 07:04:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EB50E288E8; Wed, 8 May 2019 07:04:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 649BB27F3E for ; Wed, 8 May 2019 07:04:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Message-Id:MIME-Version:References: In-Reply-To:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=8qdVk+nkeC5sofSk5CArB4Zv3Jl0EPmtCGY8f9QYNKE=; b=BGvhUbcLVgUEAO gwbyTbv8MdY5v22hMrnnKHrG59AzG8lyc1jNzjKT9DNYDjK/LBq/1ikxCLDL3lrcAxwOVAIXOSe94 aZuz00msR9eManNza10+O22p3GCPs7ZrEw9m5CkSCp34MvXzQfZUk+npkneZHN3LjU0/NFVaehlcJ /3JgJKJtCakNWtDr0HfNNFUd68dlwMMJLQiyqP/nZOilVasO83ge07G4YAtquZDF688VkrJ//qfdL kEcmNbVk9U340nLzs0qXDmyy5SiUTUa4e58CcBA6z3N4e19kR8ax7D5Oj9f2bXonCsa30h9TuW7Eu Jr8/wFXz1kLtlcmCI9hQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hOGd3-0006jk-V0; Wed, 08 May 2019 07:04:37 +0000 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hOGd1-0006jK-10 for ath10k@lists.infradead.org; Wed, 08 May 2019 07:04:36 +0000 Received: from pps.filterd (m0098394.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x4874RXK126302 for ; Wed, 8 May 2019 03:04:34 -0400 Received: from e06smtp03.uk.ibm.com (e06smtp03.uk.ibm.com [195.75.94.99]) by mx0a-001b2d01.pphosted.com with ESMTP id 2sbryukx0k-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 08 May 2019 03:04:28 -0400 Received: from localhost by e06smtp03.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 8 May 2019 08:02:47 +0100 Received: from b06cxnps4075.portsmouth.uk.ibm.com (9.149.109.197) by e06smtp03.uk.ibm.com (192.168.101.133) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Wed, 8 May 2019 08:02:39 +0100 Received: from d06av22.portsmouth.uk.ibm.com (d06av22.portsmouth.uk.ibm.com [9.149.105.58]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x4872cJb58392592 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 8 May 2019 07:02:38 GMT Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 142EB4C044; Wed, 8 May 2019 07:02:38 +0000 (GMT) Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6538A4C04E; Wed, 8 May 2019 07:02:37 +0000 (GMT) Received: from ozlabs.au.ibm.com (unknown [9.192.253.14]) by d06av22.portsmouth.uk.ibm.com (Postfix) with ESMTP; Wed, 8 May 2019 07:02:37 +0000 (GMT) Received: from adsilva.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id 38BE9A03B2; Wed, 8 May 2019 17:02:35 +1000 (AEST) From: "Alastair D'Silva" To: alastair@d-silva.org Subject: [PATCH v2 3/7] lib/hexdump.c: Optionally suppress lines of repeated bytes Date: Wed, 8 May 2019 17:01:43 +1000 X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190508070148.23130-1-alastair@au1.ibm.com> References: <20190508070148.23130-1-alastair@au1.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 x-cbid: 19050807-0012-0000-0000-000003196FA0 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19050807-0013-0000-0000-00002151EFD4 Message-Id: <20190508070148.23130-4-alastair@au1.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-05-08_05:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905080046 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190508_000435_071701_E73C1155 X-CRM114-Status: GOOD ( 28.34 ) X-BeenThere: ath10k@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-fbdev@vger.kernel.org, Stanislaw Gruszka , Petr Mladek , David Airlie , Joonas Lahtinen , dri-devel@lists.freedesktop.org, devel@driverdev.osuosl.org, linux-scsi@vger.kernel.org, Jassi Brar , ath10k@lists.infradead.org, intel-gfx@lists.freedesktop.org, Dan Carpenter , Jose Abreu , Tom Lendacky , "James E.J. Bottomley" , Jani Nikula , linux-fsdevel@vger.kernel.org, Steven Rostedt , Rodrigo Vivi , Benson Leung , Kalle Valo , Karsten Keil , "Martin K. Petersen" , Greg Kroah-Hartman , linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky , David Laight , Daniel Vetter , netdev@vger.kernel.org, Enric Balletbo i Serra , Andrew Morton , "David S. Miller" , Alexander Viro Sender: "ath10k" Errors-To: ath10k-bounces+patchwork-ath10k=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Alastair D'Silva Some buffers may only be partially filled with useful data, while the rest is padded (typically with 0x00 or 0xff). This patch introduces a flag to allow the supression of lines of repeated bytes, which are replaced with '** Skipped %u bytes of value 0x%x **' An inline wrapper function is provided for backwards compatibility with existing code, which maintains the original behaviour. Signed-off-by: Alastair D'Silva --- include/linux/printk.h | 25 +++++++++--- lib/hexdump.c | 91 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 99 insertions(+), 17 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index d7c77ed1a4cb..938a67580d78 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -479,13 +479,18 @@ enum { DUMP_PREFIX_ADDRESS, DUMP_PREFIX_OFFSET }; + extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, char *linebuf, size_t linebuflen, bool ascii); + +#define HEXDUMP_ASCII (1 << 0) +#define HEXDUMP_SUPPRESS_REPEATED (1 << 1) + #ifdef CONFIG_PRINTK -extern void print_hex_dump(const char *level, const char *prefix_str, +extern void print_hex_dump_ext(const char *level, const char *prefix_str, int prefix_type, int rowsize, int groupsize, - const void *buf, size_t len, bool ascii); + const void *buf, size_t len, u64 flags); #if defined(CONFIG_DYNAMIC_DEBUG) #define print_hex_dump_bytes(prefix_str, prefix_type, buf, len) \ dynamic_hex_dump(prefix_str, prefix_type, 16, 1, buf, len, true) @@ -494,18 +499,28 @@ extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type, const void *buf, size_t len); #endif /* defined(CONFIG_DYNAMIC_DEBUG) */ #else -static inline void print_hex_dump(const char *level, const char *prefix_str, +static inline void print_hex_dump_ext(const char *level, const char *prefix_str, int prefix_type, int rowsize, int groupsize, - const void *buf, size_t len, bool ascii) + const void *buf, size_t len, u64 flags) { } static inline void print_hex_dump_bytes(const char *prefix_str, int prefix_type, const void *buf, size_t len) { } - #endif +static __always_inline void print_hex_dump(const char *level, + const char *prefix_str, + int prefix_type, int rowsize, + int groupsize, const void *buf, + size_t len, bool ascii) +{ + print_hex_dump_ext(level, prefix_str, prefix_type, rowsize, groupsize, + buf, len, ascii ? HEXDUMP_ASCII : 0); +} + + #if defined(CONFIG_DYNAMIC_DEBUG) #define print_hex_dump_debug(prefix_str, prefix_type, rowsize, \ groupsize, buf, len, ascii) \ diff --git a/lib/hexdump.c b/lib/hexdump.c index 3943507bc0e9..d61a1e4f19fa 100644 --- a/lib/hexdump.c +++ b/lib/hexdump.c @@ -212,8 +212,44 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, EXPORT_SYMBOL(hex_dump_to_buffer); #ifdef CONFIG_PRINTK + +/** + * Check if a buffer contains only a single byte value + * @buf: pointer to the buffer + * @len: the size of the buffer in bytes + * @val: outputs the value if if the bytes are identical + */ +static bool buf_is_all(const u8 *buf, size_t len, u8 *val_out) +{ + size_t i; + u8 val; + + if (len <= 1) + return false; + + val = buf[0]; + + for (i = 1; i < len; i++) { + if (buf[i] != val) + return false; + } + + *val_out = val; + return true; +} + +static void announce_skipped(const char *level, const char *prefix_str, + u8 val, size_t count) +{ + if (count == 0) + return; + + printk("%s%s ** Skipped %lu bytes of value 0x%x **\n", + level, prefix_str, count, val); +} + /** - * print_hex_dump - print a text hex dump to syslog for a binary blob of data + * print_hex_dump_ext: dump a binary blob of data to syslog in hexadecimal * @level: kernel log level (e.g. KERN_DEBUG) * @prefix_str: string to prefix each line with; * caller supplies trailing spaces for alignment if desired @@ -224,6 +260,10 @@ EXPORT_SYMBOL(hex_dump_to_buffer); * @buf: data blob to dump * @len: number of bytes in the @buf * @ascii: include ASCII after the hex output + * @flags: A bitwise OR of the following flags: + * HEXDUMP_ASCII: include ASCII after the hex output + * HEXDUMP_SUPPRESS_REPEATED: suppress repeated lines of identical + * bytes * * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump * to the kernel log at the specified kernel log level, with an optional @@ -234,22 +274,25 @@ EXPORT_SYMBOL(hex_dump_to_buffer); * (optionally) ASCII output. * * E.g.: - * print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS, - * 16, 1, frame->data, frame->len, true); + * print_hex_dump_ext(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS, + * 16, 1, frame->data, frame->len, HEXDUMP_ASCII); * * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode: * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode: * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c pqrstuvwxyz{|}~. */ -void print_hex_dump(const char *level, const char *prefix_str, int prefix_type, - int rowsize, int groupsize, - const void *buf, size_t len, bool ascii) +void print_hex_dump_ext(const char *level, const char *prefix_str, + int prefix_type, int rowsize, int groupsize, + const void *buf, size_t len, u64 flags) { const u8 *ptr = buf; - int i, linelen, remaining = len; + int i, remaining = len; unsigned char *linebuf; unsigned int linebuf_len; + u8 skipped_val = 0; + size_t skipped = 0; + if (rowsize % groupsize) rowsize -= rowsize % groupsize; @@ -266,11 +309,35 @@ void print_hex_dump(const char *level, const char *prefix_str, int prefix_type, } for (i = 0; i < len; i += rowsize) { - linelen = min(remaining, rowsize); + int linelen = min(remaining, rowsize); remaining -= rowsize; + if (flags & HEXDUMP_SUPPRESS_REPEATED) { + u8 new_val; + + if (buf_is_all(ptr + i, linelen, &new_val)) { + if (new_val == skipped_val) { + skipped += linelen; + continue; + } else { + announce_skipped(level, prefix_str, + skipped_val, skipped); + skipped_val = new_val; + skipped = linelen; + continue; + } + } + } + + if (skipped) { + announce_skipped(level, prefix_str, skipped_val, + skipped); + skipped = 0; + } + hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize, - linebuf, linebuf_len, ascii); + linebuf, linebuf_len, + flags & HEXDUMP_ASCII); switch (prefix_type) { case DUMP_PREFIX_ADDRESS: @@ -288,7 +355,7 @@ void print_hex_dump(const char *level, const char *prefix_str, int prefix_type, kfree(linebuf); } -EXPORT_SYMBOL(print_hex_dump); +EXPORT_SYMBOL(print_hex_dump_ext); #if !defined(CONFIG_DYNAMIC_DEBUG) /** @@ -306,8 +373,8 @@ EXPORT_SYMBOL(print_hex_dump); void print_hex_dump_bytes(const char *prefix_str, int prefix_type, const void *buf, size_t len) { - print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, 16, 1, - buf, len, true); + print_hex_dump_ext(KERN_DEBUG, prefix_str, prefix_type, 16, 1, + buf, len, HEXDUMP_ASCII); } EXPORT_SYMBOL(print_hex_dump_bytes); #endif /* !defined(CONFIG_DYNAMIC_DEBUG) */ From patchwork Wed May 8 07:01:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alastair D'Silva X-Patchwork-Id: 10934483 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D47111390 for ; Wed, 8 May 2019 07:03:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BFBD82890A for ; Wed, 8 May 2019 07:03:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B33C5288EE; Wed, 8 May 2019 07:03:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E1B2D288E8 for ; Wed, 8 May 2019 07:03:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Message-Id:MIME-Version:References: In-Reply-To:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=EJmDCjky0r8aeNV2H8E0b5dnZxjWCsm9bzajjvBjOcw=; b=a5FcnXUsKht07p CdHr03hXcf3uWABONJj4oah+nqIKFwTdEoRmv1t+NrmR6Ita6rGYraRuHpF41pf5ev4xLz5XX0YP/ xwHj375ciTwBWgaDz2ImeMTMR6CN70nfmDIl4jWZJcWBPl2sw/bkptUegGyKlM+oLbuu07SEzF1la 9QfupPym17wQSN1/ZFuB1gMngYz/ovo/X+gt6HNcJTFQ2sg8PH94jQl5L2qhPWDSycuYaEmt8ZeJY w/ewltPHrtwmEQd7qpcdHjA8KeSwyepYgYxMdNiWfaL8as8pC9WVmn+DDhDqVjuAnNizD2vFvO9n/ 0h32W4uj0nAPHOD0IFNg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hOGbU-0005mR-Sq; Wed, 08 May 2019 07:03:00 +0000 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5] helo=mx0a-001b2d01.pphosted.com) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hOGbL-0005e8-JR for ath10k@lists.infradead.org; Wed, 08 May 2019 07:02:54 +0000 Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x487239f075984 for ; Wed, 8 May 2019 03:02:50 -0400 Received: from e06smtp02.uk.ibm.com (e06smtp02.uk.ibm.com [195.75.94.98]) by mx0b-001b2d01.pphosted.com with ESMTP id 2sbt87rewn-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 08 May 2019 03:02:50 -0400 Received: from localhost by e06smtp02.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 8 May 2019 08:02:48 +0100 Received: from b06cxnps3075.portsmouth.uk.ibm.com (9.149.109.195) by e06smtp02.uk.ibm.com (192.168.101.132) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Wed, 8 May 2019 08:02:39 +0100 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x4872ceO61603912 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 8 May 2019 07:02:38 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0D14A11C04A; Wed, 8 May 2019 07:02:38 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6451211C05C; Wed, 8 May 2019 07:02:37 +0000 (GMT) Received: from ozlabs.au.ibm.com (unknown [9.192.253.14]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Wed, 8 May 2019 07:02:37 +0000 (GMT) Received: from adsilva.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id 4B179A03B5; Wed, 8 May 2019 17:02:35 +1000 (AEST) From: "Alastair D'Silva" To: alastair@d-silva.org Subject: [PATCH v2 4/7] lib/hexdump.c: Replace ascii bool in hex_dump_to_buffer with flags Date: Wed, 8 May 2019 17:01:44 +1000 X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190508070148.23130-1-alastair@au1.ibm.com> References: <20190508070148.23130-1-alastair@au1.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 x-cbid: 19050807-0008-0000-0000-000002E46A05 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19050807-0009-0000-0000-00002250E9FB Message-Id: <20190508070148.23130-5-alastair@au1.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-05-08_05:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905080046 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190508_000251_796427_00F45849 X-CRM114-Status: GOOD ( 26.85 ) X-BeenThere: ath10k@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-fbdev@vger.kernel.org, Stanislaw Gruszka , Petr Mladek , David Airlie , Joonas Lahtinen , dri-devel@lists.freedesktop.org, devel@driverdev.osuosl.org, linux-scsi@vger.kernel.org, Jassi Brar , ath10k@lists.infradead.org, intel-gfx@lists.freedesktop.org, Dan Carpenter , Jose Abreu , Tom Lendacky , "James E.J. Bottomley" , Jani Nikula , linux-fsdevel@vger.kernel.org, Steven Rostedt , Rodrigo Vivi , Benson Leung , Kalle Valo , Karsten Keil , "Martin K. Petersen" , Greg Kroah-Hartman , linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky , David Laight , Daniel Vetter , netdev@vger.kernel.org, Enric Balletbo i Serra , Andrew Morton , "David S. Miller" , Alexander Viro Sender: "ath10k" Errors-To: ath10k-bounces+patchwork-ath10k=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Alastair D'Silva In order to support additional features in hex_dump_to_buffer, replace the ascii bool parameter with flags. Signed-off-by: Alastair D'Silva Acked-by: Jani Nikula Reviewed-by: Greg Kroah-Hartman --- drivers/gpu/drm/i915/intel_engine_cs.c | 2 +- drivers/isdn/hardware/mISDN/mISDNisar.c | 6 ++++-- drivers/mailbox/mailbox-test.c | 2 +- drivers/net/ethernet/amd/xgbe/xgbe-drv.c | 2 +- drivers/net/ethernet/synopsys/dwc-xlgmac-common.c | 2 +- drivers/net/wireless/ath/ath10k/debug.c | 3 ++- drivers/net/wireless/intel/iwlegacy/3945-mac.c | 2 +- drivers/platform/chrome/wilco_ec/debugfs.c | 2 +- drivers/scsi/scsi_logging.c | 8 +++----- drivers/staging/fbtft/fbtft-core.c | 2 +- fs/seq_file.c | 3 ++- include/linux/printk.h | 8 ++++---- lib/hexdump.c | 15 ++++++++------- lib/test_hexdump.c | 5 +++-- 14 files changed, 33 insertions(+), 29 deletions(-) diff --git a/drivers/gpu/drm/i915/intel_engine_cs.c b/drivers/gpu/drm/i915/intel_engine_cs.c index 49fa43ff02ba..fb133e729f9a 100644 --- a/drivers/gpu/drm/i915/intel_engine_cs.c +++ b/drivers/gpu/drm/i915/intel_engine_cs.c @@ -1318,7 +1318,7 @@ static void hexdump(struct drm_printer *m, const void *buf, size_t len) WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos, rowsize, sizeof(u32), line, sizeof(line), - false) >= sizeof(line)); + 0) >= sizeof(line)); drm_printf(m, "[%04zx] %s\n", pos, line); prev = buf + pos; diff --git a/drivers/isdn/hardware/mISDN/mISDNisar.c b/drivers/isdn/hardware/mISDN/mISDNisar.c index 386731ec2489..f13f34db6c17 100644 --- a/drivers/isdn/hardware/mISDN/mISDNisar.c +++ b/drivers/isdn/hardware/mISDN/mISDNisar.c @@ -84,7 +84,8 @@ send_mbox(struct isar_hw *isar, u8 his, u8 creg, u8 len, u8 *msg) while (l < (int)len) { hex_dump_to_buffer(msg + l, len - l, 32, 1, - isar->log, 256, 1); + isar->log, 256, + HEXDUMP_ASCII); pr_debug("%s: %s %02x: %s\n", isar->name, __func__, l, isar->log); l += 32; @@ -113,7 +114,8 @@ rcv_mbox(struct isar_hw *isar, u8 *msg) while (l < (int)isar->clsb) { hex_dump_to_buffer(msg + l, isar->clsb - l, 32, - 1, isar->log, 256, 1); + 1, isar->log, 256, + HEXDUMP_ASCII); pr_debug("%s: %s %02x: %s\n", isar->name, __func__, l, isar->log); l += 32; diff --git a/drivers/mailbox/mailbox-test.c b/drivers/mailbox/mailbox-test.c index 4e4ac4be6423..2f9a094d0259 100644 --- a/drivers/mailbox/mailbox-test.c +++ b/drivers/mailbox/mailbox-test.c @@ -213,7 +213,7 @@ static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf, hex_dump_to_buffer(ptr, MBOX_BYTES_PER_LINE, MBOX_BYTES_PER_LINE, 1, touser + l, - MBOX_HEXDUMP_LINE_LEN, true); + MBOX_HEXDUMP_LINE_LEN, HEXDUMP_ASCII); ptr += MBOX_BYTES_PER_LINE; l += MBOX_HEXDUMP_LINE_LEN; diff --git a/drivers/net/ethernet/amd/xgbe/xgbe-drv.c b/drivers/net/ethernet/amd/xgbe/xgbe-drv.c index 0cc911f928b1..e954a31cee0c 100644 --- a/drivers/net/ethernet/amd/xgbe/xgbe-drv.c +++ b/drivers/net/ethernet/amd/xgbe/xgbe-drv.c @@ -2992,7 +2992,7 @@ void xgbe_print_pkt(struct net_device *netdev, struct sk_buff *skb, bool tx_rx) unsigned int len = min(skb->len - i, 32U); hex_dump_to_buffer(&skb->data[i], len, 32, 1, - buffer, sizeof(buffer), false); + buffer, sizeof(buffer), 0); netdev_dbg(netdev, " %#06x: %s\n", i, buffer); } diff --git a/drivers/net/ethernet/synopsys/dwc-xlgmac-common.c b/drivers/net/ethernet/synopsys/dwc-xlgmac-common.c index eb1c6b03c329..b80adfa1f890 100644 --- a/drivers/net/ethernet/synopsys/dwc-xlgmac-common.c +++ b/drivers/net/ethernet/synopsys/dwc-xlgmac-common.c @@ -349,7 +349,7 @@ void xlgmac_print_pkt(struct net_device *netdev, unsigned int len = min(skb->len - i, 32U); hex_dump_to_buffer(&skb->data[i], len, 32, 1, - buffer, sizeof(buffer), false); + buffer, sizeof(buffer), 0); netdev_dbg(netdev, " %#06x: %s\n", i, buffer); } diff --git a/drivers/net/wireless/ath/ath10k/debug.c b/drivers/net/wireless/ath/ath10k/debug.c index 32d967a31c65..4c99ea03226d 100644 --- a/drivers/net/wireless/ath/ath10k/debug.c +++ b/drivers/net/wireless/ath/ath10k/debug.c @@ -2662,7 +2662,8 @@ void ath10k_dbg_dump(struct ath10k *ar, (unsigned int)(ptr - buf)); hex_dump_to_buffer(ptr, len - (ptr - buf), 16, 1, linebuf + linebuflen, - sizeof(linebuf) - linebuflen, true); + sizeof(linebuf) - linebuflen, + HEXDUMP_ASCII); dev_printk(KERN_DEBUG, ar->dev, "%s\n", linebuf); } } diff --git a/drivers/net/wireless/intel/iwlegacy/3945-mac.c b/drivers/net/wireless/intel/iwlegacy/3945-mac.c index 271977f7fbb0..acbe26d22c34 100644 --- a/drivers/net/wireless/intel/iwlegacy/3945-mac.c +++ b/drivers/net/wireless/intel/iwlegacy/3945-mac.c @@ -3247,7 +3247,7 @@ il3945_show_measurement(struct device *d, struct device_attribute *attr, while (size && PAGE_SIZE - len) { hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len, - PAGE_SIZE - len, true); + PAGE_SIZE - len, HEXDUMP_ASCII); len = strlen(buf); if (PAGE_SIZE - len) buf[len++] = '\n'; diff --git a/drivers/platform/chrome/wilco_ec/debugfs.c b/drivers/platform/chrome/wilco_ec/debugfs.c index c090db2cd5be..26d9ae5c2dc2 100644 --- a/drivers/platform/chrome/wilco_ec/debugfs.c +++ b/drivers/platform/chrome/wilco_ec/debugfs.c @@ -174,7 +174,7 @@ static ssize_t raw_read(struct file *file, char __user *user_buf, size_t count, fmt_len = hex_dump_to_buffer(debug_info->raw_data, debug_info->response_size, 16, 1, debug_info->formatted_data, - FORMATTED_BUFFER_SIZE, true); + FORMATTED_BUFFER_SIZE, HEXDUMP_ASCII); /* Only return response the first time it is read */ debug_info->response_size = 0; } diff --git a/drivers/scsi/scsi_logging.c b/drivers/scsi/scsi_logging.c index bd70339c1242..fce542bb40e6 100644 --- a/drivers/scsi/scsi_logging.c +++ b/drivers/scsi/scsi_logging.c @@ -263,7 +263,7 @@ void scsi_print_command(struct scsi_cmnd *cmd) "CDB[%02x]: ", k); hex_dump_to_buffer(&cmd->cmnd[k], linelen, 16, 1, logbuf + off, - logbuf_len - off, false); + logbuf_len - off, 0); } dev_printk(KERN_INFO, &cmd->device->sdev_gendev, "%s", logbuf); @@ -274,8 +274,7 @@ void scsi_print_command(struct scsi_cmnd *cmd) if (!WARN_ON(off > logbuf_len - 49)) { off += scnprintf(logbuf + off, logbuf_len - off, " "); hex_dump_to_buffer(cmd->cmnd, cmd->cmd_len, 16, 1, - logbuf + off, logbuf_len - off, - false); + logbuf + off, logbuf_len - off, 0); } out_printk: dev_printk(KERN_INFO, &cmd->device->sdev_gendev, "%s", logbuf); @@ -354,8 +353,7 @@ scsi_log_dump_sense(const struct scsi_device *sdev, const char *name, int tag, off = sdev_format_header(logbuf, logbuf_len, name, tag); hex_dump_to_buffer(&sense_buffer[i], len, 16, 1, - logbuf + off, logbuf_len - off, - false); + logbuf + off, logbuf_len - off, 0); dev_printk(KERN_INFO, &sdev->sdev_gendev, "%s", logbuf); } scsi_log_release_buffer(logbuf); diff --git a/drivers/staging/fbtft/fbtft-core.c b/drivers/staging/fbtft/fbtft-core.c index 9b07badf4c6c..2e5df5cc9d61 100644 --- a/drivers/staging/fbtft/fbtft-core.c +++ b/drivers/staging/fbtft/fbtft-core.c @@ -61,7 +61,7 @@ void fbtft_dbg_hex(const struct device *dev, int groupsize, va_end(args); hex_dump_to_buffer(buf, len, 32, groupsize, text + text_len, - 512 - text_len, false); + 512 - text_len, 0); if (len > 32) dev_info(dev, "%s ...\n", text); diff --git a/fs/seq_file.c b/fs/seq_file.c index 1dea7a8a5255..a0213637af3e 100644 --- a/fs/seq_file.c +++ b/fs/seq_file.c @@ -873,7 +873,8 @@ void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type, size = seq_get_buf(m, &buffer); ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize, - buffer, size, ascii); + buffer, size, + ascii ? HEXDUMP_ASCII : 0); seq_commit(m, ret < size ? ret : -1); seq_putc(m, '\n'); diff --git a/include/linux/printk.h b/include/linux/printk.h index 938a67580d78..00a82e468643 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -480,13 +480,13 @@ enum { DUMP_PREFIX_OFFSET }; -extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, - int groupsize, char *linebuf, size_t linebuflen, - bool ascii); - #define HEXDUMP_ASCII (1 << 0) #define HEXDUMP_SUPPRESS_REPEATED (1 << 1) +extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, + int groupsize, char *linebuf, size_t linebuflen, + u64 flags); + #ifdef CONFIG_PRINTK extern void print_hex_dump_ext(const char *level, const char *prefix_str, int prefix_type, int rowsize, int groupsize, diff --git a/lib/hexdump.c b/lib/hexdump.c index d61a1e4f19fa..ddd1697e5f9b 100644 --- a/lib/hexdump.c +++ b/lib/hexdump.c @@ -85,7 +85,8 @@ EXPORT_SYMBOL(bin2hex); * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1) * @linebuf: where to put the converted data * @linebuflen: total size of @linebuf, including space for terminating NUL - * @ascii: include ASCII after the hex output + * @flags: A bitwise OR of the following flags: + * HEXDUMP_ASCII: include ASCII after the hex output * * hex_dump_to_buffer() works on one "line" of output at a time, converting * bytes of input to hexadecimal (and optionally printable ASCII) @@ -97,7 +98,7 @@ EXPORT_SYMBOL(bin2hex); * * E.g.: * hex_dump_to_buffer(frame->data, frame->len, 16, 1, - * linebuf, sizeof(linebuf), true); + * linebuf, sizeof(linebuf), HEXDUMP_ASCII); * * example output buffer: * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO @@ -109,7 +110,7 @@ EXPORT_SYMBOL(bin2hex); * string if enough space had been available. */ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, - char *linebuf, size_t linebuflen, bool ascii) + char *linebuf, size_t linebuflen, u64 flags) { const u8 *ptr = buf; int ngroups; @@ -187,7 +188,7 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, if (j) lx--; } - if (!ascii) + if (!(flags & HEXDUMP_ASCII)) goto nil; while (lx < ascii_column) { @@ -207,7 +208,8 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, overflow2: linebuf[lx++] = '\0'; overflow1: - return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1; + return (flags & HEXDUMP_ASCII) ? ascii_column + len : + (groupsize * 2 + 1) * ngroups - 1; } EXPORT_SYMBOL(hex_dump_to_buffer); @@ -336,8 +338,7 @@ void print_hex_dump_ext(const char *level, const char *prefix_str, } hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize, - linebuf, linebuf_len, - flags & HEXDUMP_ASCII); + linebuf, linebuf_len, flags); switch (prefix_type) { case DUMP_PREFIX_ADDRESS: diff --git a/lib/test_hexdump.c b/lib/test_hexdump.c index 6ab75a209b43..ae340c5c1c6f 100644 --- a/lib/test_hexdump.c +++ b/lib/test_hexdump.c @@ -166,7 +166,7 @@ static void __init test_hexdump(size_t len, int rowsize, int groupsize, memset(real, FILL_CHAR, sizeof(real)); hex_dump_to_buffer(data_b, len, rowsize, groupsize, real, sizeof(real), - ascii); + ascii ? HEXDUMP_ASCII : 0); memset(test, FILL_CHAR, sizeof(test)); test_hexdump_prepare_test(len, rowsize, groupsize, test, sizeof(test), @@ -204,7 +204,8 @@ static void __init test_hexdump_overflow(size_t buflen, size_t len, memset(buf, FILL_CHAR, sizeof(buf)); - rc = hex_dump_to_buffer(data_b, len, rowsize, groupsize, buf, buflen, ascii); + rc = hex_dump_to_buffer(data_b, len, rowsize, groupsize, buf, buflen, + ascii ? HEXDUMP_ASCII : 0); /* * Caller must provide the data length multiple of groupsize. The From patchwork Wed May 8 07:01:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alastair D'Silva X-Patchwork-Id: 10934495 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0A6541390 for ; Wed, 8 May 2019 07:03:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EA5FE288E8 for ; Wed, 8 May 2019 07:03:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DE3242890A; Wed, 8 May 2019 07:03:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 641AA288E8 for ; Wed, 8 May 2019 07:03:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Message-Id:MIME-Version:References: In-Reply-To:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=g1Hwbv5TMVygQ3+X4daElifXjlU0voGZMfZIqLjihU8=; b=mhW/IBIA8HUL+q bQYiBR1hL+Y4ci/kuboxjyD62oVCVG1zPHjftEAgXOVBX3kbJ8M0mxjjadBo55djfIZ0HEuKz42l/ at+qjIWFnudNdg5Yb0V9sSGh1T9fWakbV2HDaHp6WJbmbS3wWl9AAILO4ZNyMNccvLGAQxRQGWY2d HHqnmFRtHsVdyLPbT/2hWxdB7sm6aI6AI49S81LJoDs95PW1GkVpRk06NfdoliNiIfHlfRySOfXJ/ G6nhqJI+L/gcxzDIigq9Zjmr30gTKMHdL/mOq/NF5pUnrp4YrVLki5o2xcLwhGfLXjDqqqEIx8+qC 7qJqsIUZ3jVfsrUtE9tw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hOGbp-0006DP-1Y; Wed, 08 May 2019 07:03:21 +0000 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hOGbN-0005fO-Gw for ath10k@lists.infradead.org; Wed, 08 May 2019 07:02:58 +0000 Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x48723vG020285 for ; Wed, 8 May 2019 03:02:53 -0400 Received: from e06smtp03.uk.ibm.com (e06smtp03.uk.ibm.com [195.75.94.99]) by mx0a-001b2d01.pphosted.com with ESMTP id 2sbrxkm0fn-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 08 May 2019 03:02:52 -0400 Received: from localhost by e06smtp03.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 8 May 2019 08:02:50 +0100 Received: from b06cxnps4076.portsmouth.uk.ibm.com (9.149.109.198) by e06smtp03.uk.ibm.com (192.168.101.133) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Wed, 8 May 2019 08:02:40 +0100 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x4872dhS48038106 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 8 May 2019 07:02:40 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C0C4011C04C; Wed, 8 May 2019 07:02:39 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2753A11C050; Wed, 8 May 2019 07:02:39 +0000 (GMT) Received: from ozlabs.au.ibm.com (unknown [9.192.253.14]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Wed, 8 May 2019 07:02:39 +0000 (GMT) Received: from adsilva.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id 5D665A03B6; Wed, 8 May 2019 17:02:35 +1000 (AEST) From: "Alastair D'Silva" To: alastair@d-silva.org Subject: [PATCH v2 5/7] lib/hexdump.c: Allow multiple groups to be separated by lines '|' Date: Wed, 8 May 2019 17:01:45 +1000 X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190508070148.23130-1-alastair@au1.ibm.com> References: <20190508070148.23130-1-alastair@au1.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 x-cbid: 19050807-0012-0000-0000-000003196FA2 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19050807-0013-0000-0000-00002151EFD6 Message-Id: <20190508070148.23130-6-alastair@au1.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-05-08_05:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905080046 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190508_000253_682165_0B605932 X-CRM114-Status: GOOD ( 26.63 ) X-BeenThere: ath10k@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-fbdev@vger.kernel.org, Stanislaw Gruszka , Petr Mladek , David Airlie , Joonas Lahtinen , dri-devel@lists.freedesktop.org, devel@driverdev.osuosl.org, linux-scsi@vger.kernel.org, Jassi Brar , ath10k@lists.infradead.org, intel-gfx@lists.freedesktop.org, Dan Carpenter , Jose Abreu , Tom Lendacky , "James E.J. Bottomley" , Jani Nikula , linux-fsdevel@vger.kernel.org, Steven Rostedt , Rodrigo Vivi , Benson Leung , Kalle Valo , Karsten Keil , "Martin K. Petersen" , Greg Kroah-Hartman , linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky , David Laight , Daniel Vetter , netdev@vger.kernel.org, Enric Balletbo i Serra , Andrew Morton , "David S. Miller" , Alexander Viro Sender: "ath10k" Errors-To: ath10k-bounces+patchwork-ath10k=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Alastair D'Silva With the wider display format, it can become hard to identify how many bytes into the line you are looking at. The patch adds new flags to hex_dump_to_buffer() and print_hex_dump() to print vertical lines to separate every N groups of bytes. eg. buf:00000000: 454d414e 43415053|4e495f45 00584544 NAMESPAC|E_INDEX. buf:00000010: 00000000 00000002|00000000 00000000 ........|........ Signed-off-by: Alastair D'Silva --- include/linux/printk.h | 3 +++ lib/hexdump.c | 59 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index 00a82e468643..dc693aec394c 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -482,6 +482,9 @@ enum { #define HEXDUMP_ASCII (1 << 0) #define HEXDUMP_SUPPRESS_REPEATED (1 << 1) +#define HEXDUMP_2_GRP_LINES (1 << 2) +#define HEXDUMP_4_GRP_LINES (1 << 3) +#define HEXDUMP_8_GRP_LINES (1 << 4) extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, char *linebuf, size_t linebuflen, diff --git a/lib/hexdump.c b/lib/hexdump.c index ddd1697e5f9b..6f4d1176c332 100644 --- a/lib/hexdump.c +++ b/lib/hexdump.c @@ -77,6 +77,23 @@ char *bin2hex(char *dst, const void *src, size_t count) } EXPORT_SYMBOL(bin2hex); +static const char *group_separator(int group, u64 flags) +{ + if (group == 0) + return " "; + + if ((flags & HEXDUMP_8_GRP_LINES) && !((group) % 8)) + return "|"; + + if ((flags & HEXDUMP_4_GRP_LINES) && !((group) % 4)) + return "|"; + + if ((flags & HEXDUMP_2_GRP_LINES) && !((group) % 2)) + return "|"; + + return " "; +} + /** * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory * @buf: data blob to dump @@ -87,6 +104,9 @@ EXPORT_SYMBOL(bin2hex); * @linebuflen: total size of @linebuf, including space for terminating NUL * @flags: A bitwise OR of the following flags: * HEXDUMP_ASCII: include ASCII after the hex output + * HEXDUMP_2_GRP_LINES: insert a '|' after every 2 groups + * HEXDUMP_4_GRP_LINES: insert a '|' after every 4 groups + * HEXDUMP_8_GRP_LINES: insert a '|' after every 8 groups * * hex_dump_to_buffer() works on one "line" of output at a time, converting * bytes of input to hexadecimal (and optionally printable ASCII) @@ -118,6 +138,7 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, int j, lx = 0; int ascii_column; int ret; + int line_chars = 0; if (!is_power_of_2(groupsize) || groupsize > 8) groupsize = 1; @@ -144,7 +165,8 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, for (j = 0; j < ngroups; j++) { ret = snprintf(linebuf + lx, linebuflen - lx, - "%s%16.16llx", j ? " " : "", + "%s%16.16llx", + j ? group_separator(j, flags) : "", get_unaligned(ptr8 + j)); if (ret >= linebuflen - lx) goto overflow1; @@ -155,7 +177,8 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, for (j = 0; j < ngroups; j++) { ret = snprintf(linebuf + lx, linebuflen - lx, - "%s%8.8x", j ? " " : "", + "%s%8.8x", + j ? group_separator(j, flags) : "", get_unaligned(ptr4 + j)); if (ret >= linebuflen - lx) goto overflow1; @@ -166,7 +189,8 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, for (j = 0; j < ngroups; j++) { ret = snprintf(linebuf + lx, linebuflen - lx, - "%s%4.4x", j ? " " : "", + "%s%4.4x", + j ? group_separator(j, flags) : "", get_unaligned(ptr2 + j)); if (ret >= linebuflen - lx) goto overflow1; @@ -196,11 +220,26 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, goto overflow2; linebuf[lx++] = ' '; } + + if (flags & HEXDUMP_2_GRP_LINES) + line_chars = groupsize * 2; + if (flags & HEXDUMP_4_GRP_LINES) + line_chars = groupsize * 4; + if (flags & HEXDUMP_8_GRP_LINES) + line_chars = groupsize * 8; + for (j = 0; j < len; j++) { if (linebuflen < lx + 2) goto overflow2; ch = ptr[j]; linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.'; + + if (line_chars && ((j + 1) < len) && + ((j + 1) % line_chars == 0)) { + if (linebuflen < lx + 2) + goto overflow2; + linebuf[lx++] = '|'; + } } nil: linebuf[lx] = '\0'; @@ -208,7 +247,8 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, overflow2: linebuf[lx++] = '\0'; overflow1: - return (flags & HEXDUMP_ASCII) ? ascii_column + len : + return (flags & HEXDUMP_ASCII) ? ascii_column + len + + (len - 1) / line_chars : (groupsize * 2 + 1) * ngroups - 1; } EXPORT_SYMBOL(hex_dump_to_buffer); @@ -246,7 +286,7 @@ static void announce_skipped(const char *level, const char *prefix_str, if (count == 0) return; - printk("%s%s ** Skipped %lu bytes of value 0x%x **\n", + printk("%s%s ** Skipped %lu bytes of value 0x%02x **\n", level, prefix_str, count, val); } @@ -266,6 +306,9 @@ static void announce_skipped(const char *level, const char *prefix_str, * HEXDUMP_ASCII: include ASCII after the hex output * HEXDUMP_SUPPRESS_REPEATED: suppress repeated lines of identical * bytes + * HEXDUMP_2_GRP_LINES: insert a '|' after every 2 groups + * HEXDUMP_4_GRP_LINES: insert a '|' after every 4 groups + * HEXDUMP_8_GRP_LINES: insert a '|' after every 8 groups * * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump * to the kernel log at the specified kernel log level, with an optional @@ -295,14 +338,14 @@ void print_hex_dump_ext(const char *level, const char *prefix_str, u8 skipped_val = 0; size_t skipped = 0; - if (rowsize % groupsize) rowsize -= rowsize % groupsize; /* Worst case line length: - * 2 hex chars + space per byte in, 2 spaces, 1 char per byte in, NULL + * 2 hex chars + space per byte in, 2 spaces, 1 char per byte in, + * 1 char per N groups, NULL */ - linebuf_len = rowsize * 3 + 2 + rowsize + 1; + linebuf_len = rowsize * 3 + 2 + rowsize + rowsize / groupsize + 1; linebuf = kzalloc(linebuf_len, GFP_KERNEL); if (!linebuf) { printk("%s%shexdump: Could not alloc %u bytes for buffer\n", From patchwork Wed May 8 07:01:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alastair D'Silva X-Patchwork-Id: 10934485 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BC056924 for ; Wed, 8 May 2019 07:03:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A8FA728892 for ; Wed, 8 May 2019 07:03:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9BB80288EF; Wed, 8 May 2019 07:03:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 10A8D28892 for ; Wed, 8 May 2019 07:03:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Message-Id:MIME-Version:References: In-Reply-To:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=lEtwDWQ7B0ql4N0MwUg4KHzl9CyzDRzkTderisgbEsE=; b=KAwOzooz9cwE9v 1dxqpt7bO/6B5+Zbshjq+tGE0GwtQ+qOFopmobiwGgrl/FHlLwJCUlKsOw78Ly29Z4D8aDmzfyRSZ I3rwPZAG3/Df6hB+MiYiUDNWNgP+WCAuoBwxmIBlmu8e8g7eBCuJ10mfSRM3byn2V6+yOfutlqXZG Rp0y+MENcVmlywjSI3CQSeuuZ1Cen/8tkPklDbzAb22NaWut1yB92IEqnvBSdlaih2uNKOrZd2nAn 6aWpAoG59KIoqoyPgRlhPOBxzhs2WUagGFVho2Y/aZ+h5NN8+QIO+XPkM/pwXOnobr2q+dVLotMWv tFhlQ+GqV+mOHoKNhR/Q==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hOGbk-00067m-Oh; Wed, 08 May 2019 07:03:16 +0000 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5] helo=mx0a-001b2d01.pphosted.com) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hOGbN-0005f8-Da for ath10k@lists.infradead.org; Wed, 08 May 2019 07:02:58 +0000 Received: from pps.filterd (m0098413.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x48725kM128086 for ; Wed, 8 May 2019 03:02:52 -0400 Received: from e06smtp05.uk.ibm.com (e06smtp05.uk.ibm.com [195.75.94.101]) by mx0b-001b2d01.pphosted.com with ESMTP id 2sbq4h89cq-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 08 May 2019 03:02:52 -0400 Received: from localhost by e06smtp05.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 8 May 2019 08:02:50 +0100 Received: from b06cxnps4075.portsmouth.uk.ibm.com (9.149.109.197) by e06smtp05.uk.ibm.com (192.168.101.135) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Wed, 8 May 2019 08:02:40 +0100 Received: from b06wcsmtp001.portsmouth.uk.ibm.com (b06wcsmtp001.portsmouth.uk.ibm.com [9.149.105.160]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x4872dLI58195990 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 8 May 2019 07:02:39 GMT Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C0235A405F; Wed, 8 May 2019 07:02:39 +0000 (GMT) Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 28D9FA4062; Wed, 8 May 2019 07:02:39 +0000 (GMT) Received: from ozlabs.au.ibm.com (unknown [9.192.253.14]) by b06wcsmtp001.portsmouth.uk.ibm.com (Postfix) with ESMTP; Wed, 8 May 2019 07:02:39 +0000 (GMT) Received: from adsilva.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id 681BEA03BC; Wed, 8 May 2019 17:02:35 +1000 (AEST) From: "Alastair D'Silva" To: alastair@d-silva.org Subject: [PATCH v2 6/7] lib/hexdump.c: Allow multiple groups to be separated by spaces Date: Wed, 8 May 2019 17:01:46 +1000 X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190508070148.23130-1-alastair@au1.ibm.com> References: <20190508070148.23130-1-alastair@au1.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 x-cbid: 19050807-0020-0000-0000-0000033A6C76 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19050807-0021-0000-0000-0000218D0BCB Message-Id: <20190508070148.23130-7-alastair@au1.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-05-08_05:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=988 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905080046 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190508_000253_711629_4EA98EB7 X-CRM114-Status: GOOD ( 26.32 ) X-BeenThere: ath10k@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-fbdev@vger.kernel.org, Stanislaw Gruszka , Petr Mladek , David Airlie , Joonas Lahtinen , dri-devel@lists.freedesktop.org, devel@driverdev.osuosl.org, linux-scsi@vger.kernel.org, Jassi Brar , ath10k@lists.infradead.org, intel-gfx@lists.freedesktop.org, Dan Carpenter , Jose Abreu , Tom Lendacky , "James E.J. Bottomley" , Jani Nikula , linux-fsdevel@vger.kernel.org, Steven Rostedt , Rodrigo Vivi , Benson Leung , Kalle Valo , Karsten Keil , "Martin K. Petersen" , Greg Kroah-Hartman , linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky , David Laight , Daniel Vetter , netdev@vger.kernel.org, Enric Balletbo i Serra , Andrew Morton , "David S. Miller" , Alexander Viro Sender: "ath10k" Errors-To: ath10k-bounces+patchwork-ath10k=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Alastair D'Silva Similar to the previous patch, this patch separates groups by 2 spaces for the hex fields, and 1 space for the ASCII field. eg. buf:00000000: 454d414e 43415053 4e495f45 00584544 NAMESPAC E_INDEX. buf:00000010: 00000000 00000002 00000000 00000000 ........ ........ Signed-off-by: Alastair D'Silva --- include/linux/printk.h | 3 ++ lib/hexdump.c | 65 +++++++++++++++++++++++++++++++----------- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index dc693aec394c..5231a14e4593 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -485,6 +485,9 @@ enum { #define HEXDUMP_2_GRP_LINES (1 << 2) #define HEXDUMP_4_GRP_LINES (1 << 3) #define HEXDUMP_8_GRP_LINES (1 << 4) +#define HEXDUMP_2_GRP_SPACES (1 << 5) +#define HEXDUMP_4_GRP_SPACES (1 << 6) +#define HEXDUMP_8_GRP_SPACES (1 << 7) extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, char *linebuf, size_t linebuflen, diff --git a/lib/hexdump.c b/lib/hexdump.c index 6f4d1176c332..febd614406d1 100644 --- a/lib/hexdump.c +++ b/lib/hexdump.c @@ -91,9 +91,37 @@ static const char *group_separator(int group, u64 flags) if ((flags & HEXDUMP_2_GRP_LINES) && !((group) % 2)) return "|"; + if ((flags & HEXDUMP_8_GRP_SPACES) && !((group) % 8)) + return " "; + + if ((flags & HEXDUMP_4_GRP_SPACES) && !((group) % 4)) + return " "; + + if ((flags & HEXDUMP_2_GRP_SPACES) && !((group) % 2)) + return " "; + return " "; } +static void separator_parameters(u64 flags, int groupsize, int *sep_chars, + char *sep) +{ + if (flags & (HEXDUMP_2_GRP_LINES | HEXDUMP_2_GRP_SPACES)) + *sep_chars = groupsize * 2; + if (flags & (HEXDUMP_4_GRP_LINES | HEXDUMP_4_GRP_SPACES)) + *sep_chars = groupsize * 4; + if (flags & (HEXDUMP_8_GRP_LINES | HEXDUMP_8_GRP_SPACES)) + *sep_chars = groupsize * 8; + + if (flags & (HEXDUMP_2_GRP_LINES | HEXDUMP_4_GRP_LINES | + HEXDUMP_8_GRP_LINES)) + *sep = '|'; + + if (flags & (HEXDUMP_2_GRP_SPACES | HEXDUMP_4_GRP_SPACES | + HEXDUMP_8_GRP_SPACES)) + *sep = ' '; +} + /** * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory * @buf: data blob to dump @@ -107,6 +135,9 @@ static const char *group_separator(int group, u64 flags) * HEXDUMP_2_GRP_LINES: insert a '|' after every 2 groups * HEXDUMP_4_GRP_LINES: insert a '|' after every 4 groups * HEXDUMP_8_GRP_LINES: insert a '|' after every 8 groups + * HEXDUMP_2_GRP_SPACES: insert a ' ' after every 2 groups + * HEXDUMP_4_GRP_SPACES: insert a ' ' after every 4 groups + * HEXDUMP_8_GRP_SPACES: insert a ' ' after every 8 groups * * hex_dump_to_buffer() works on one "line" of output at a time, converting * bytes of input to hexadecimal (and optionally printable ASCII) @@ -138,7 +169,8 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, int j, lx = 0; int ascii_column; int ret; - int line_chars = 0; + int sep_chars = 0; + char sep = 0; if (!is_power_of_2(groupsize) || groupsize > 8) groupsize = 1; @@ -152,8 +184,14 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, len = rowsize; ngroups = len / groupsize; + ascii_column = rowsize * 2 + rowsize / groupsize + 1; + // space separators use 2 spaces in the hex output + separator_parameters(flags, groupsize, &sep_chars, &sep); + if (sep == ' ') + ascii_column += rowsize / sep_chars; + if (!linebuflen) goto overflow1; @@ -221,24 +259,17 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, linebuf[lx++] = ' '; } - if (flags & HEXDUMP_2_GRP_LINES) - line_chars = groupsize * 2; - if (flags & HEXDUMP_4_GRP_LINES) - line_chars = groupsize * 4; - if (flags & HEXDUMP_8_GRP_LINES) - line_chars = groupsize * 8; - for (j = 0; j < len; j++) { if (linebuflen < lx + 2) goto overflow2; ch = ptr[j]; linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.'; - if (line_chars && ((j + 1) < len) && - ((j + 1) % line_chars == 0)) { + if (sep_chars && ((j + 1) < len) && + ((j + 1) % sep_chars == 0)) { if (linebuflen < lx + 2) goto overflow2; - linebuf[lx++] = '|'; + linebuf[lx++] = sep; } } nil: @@ -247,9 +278,11 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, overflow2: linebuf[lx++] = '\0'; overflow1: - return (flags & HEXDUMP_ASCII) ? ascii_column + len + - (len - 1) / line_chars : - (groupsize * 2 + 1) * ngroups - 1; + if (flags & HEXDUMP_ASCII) + return ascii_column + len + (len - 1) / sep_chars; + + return groupsize * 2 * ngroups + + ((sep == ' ') ? 2 : 1) * (ngroups - 1); } EXPORT_SYMBOL(hex_dump_to_buffer); @@ -343,9 +376,9 @@ void print_hex_dump_ext(const char *level, const char *prefix_str, /* Worst case line length: * 2 hex chars + space per byte in, 2 spaces, 1 char per byte in, - * 1 char per N groups, NULL + * 2 char per N groups, NULL */ - linebuf_len = rowsize * 3 + 2 + rowsize + rowsize / groupsize + 1; + linebuf_len = rowsize * 3 + 2 + rowsize + 2 * rowsize / groupsize + 1; linebuf = kzalloc(linebuf_len, GFP_KERNEL); if (!linebuf) { printk("%s%shexdump: Could not alloc %u bytes for buffer\n", From patchwork Wed May 8 07:01:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alastair D'Silva X-Patchwork-Id: 10934475 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id ACCFF1390 for ; Wed, 8 May 2019 07:03:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9A30A28892 for ; Wed, 8 May 2019 07:03:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8CF4F288EE; Wed, 8 May 2019 07:03:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D3173288E8 for ; Wed, 8 May 2019 07:03:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Message-Id:MIME-Version:References: In-Reply-To:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=NMsSvYe1y+B1fT4+6aaUNhTqb3tClTephV4w4G6lOhM=; b=N7zvJDs6cveKKy 2xPFJ01l6B6Rs1o3cjvDeBB7gwJcAZ0QEKxIeaFOAbxr+T2RGuyPzqTq65JUFB9IYLBo0L2AwJOzR k8CfOW18M32MKx2nfAjp9PReqzjLSF9l/rg8QR7mGF2c2Q5vwBGX/4OQgwlruEKvau8+ZS9US9Mir fEEW+VgW99VG+QUDNyX4NqWvkKnaDZif2Q0IpkfryAMgQ4m0qn18H+CC31ZwlqJ5hZLJ8DUPBccPA 5RjDM5fdw0u+L0PQpBN32rkTDfxxM7XFMdnWHfFzUQGrQP5kKmts3F7eJL0krCpJyKUN5LmElbZFu mQo4Nqfy2OH2pE67VnSA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hOGbb-0005wj-R3; Wed, 08 May 2019 07:03:07 +0000 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hOGbN-0005f9-4Y for ath10k@lists.infradead.org; Wed, 08 May 2019 07:02:56 +0000 Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x48725tZ020514 for ; Wed, 8 May 2019 03:02:52 -0400 Received: from e06smtp01.uk.ibm.com (e06smtp01.uk.ibm.com [195.75.94.97]) by mx0a-001b2d01.pphosted.com with ESMTP id 2sbrxkm0fr-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 08 May 2019 03:02:52 -0400 Received: from localhost by e06smtp01.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 8 May 2019 08:02:50 +0100 Received: from b06cxnps3074.portsmouth.uk.ibm.com (9.149.109.194) by e06smtp01.uk.ibm.com (192.168.101.131) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Wed, 8 May 2019 08:02:41 +0100 Received: from d06av26.portsmouth.uk.ibm.com (d06av26.portsmouth.uk.ibm.com [9.149.105.62]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x4872euL55181412 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 8 May 2019 07:02:40 GMT Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 130A8AE058; Wed, 8 May 2019 07:02:40 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2C072AE053; Wed, 8 May 2019 07:02:39 +0000 (GMT) Received: from ozlabs.au.ibm.com (unknown [9.192.253.14]) by d06av26.portsmouth.uk.ibm.com (Postfix) with ESMTP; Wed, 8 May 2019 07:02:39 +0000 (GMT) Received: from adsilva.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id 7E655A03C4; Wed, 8 May 2019 17:02:35 +1000 (AEST) From: "Alastair D'Silva" To: alastair@d-silva.org Subject: [PATCH v2 7/7] lib/hexdump.c: Optionally retain byte ordering Date: Wed, 8 May 2019 17:01:47 +1000 X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190508070148.23130-1-alastair@au1.ibm.com> References: <20190508070148.23130-1-alastair@au1.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 x-cbid: 19050807-4275-0000-0000-00000332844E X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19050807-4276-0000-0000-00003841F1F4 Message-Id: <20190508070148.23130-8-alastair@au1.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-05-08_05:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1905080046 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190508_000253_254127_CE1783C0 X-CRM114-Status: GOOD ( 29.68 ) X-BeenThere: ath10k@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-fbdev@vger.kernel.org, Stanislaw Gruszka , Petr Mladek , David Airlie , Joonas Lahtinen , dri-devel@lists.freedesktop.org, devel@driverdev.osuosl.org, linux-scsi@vger.kernel.org, Jassi Brar , ath10k@lists.infradead.org, intel-gfx@lists.freedesktop.org, Dan Carpenter , Jose Abreu , Tom Lendacky , "James E.J. Bottomley" , Jani Nikula , linux-fsdevel@vger.kernel.org, Steven Rostedt , Rodrigo Vivi , Benson Leung , Kalle Valo , Karsten Keil , "Martin K. Petersen" , Greg Kroah-Hartman , linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky , David Laight , Daniel Vetter , netdev@vger.kernel.org, Enric Balletbo i Serra , Andrew Morton , "David S. Miller" , Alexander Viro Sender: "ath10k" Errors-To: ath10k-bounces+patchwork-ath10k=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Alastair D'Silva The behaviour of hexdump groups is to print the data out as if it was a native-endian number. This patch tweaks the documentation to make this clear, and also adds the HEXDUMP_RETAIN_BYTE_ORDER flag to allow groups of multiple bytes to be printed without affecting the ordering of the printed bytes. Signed-off-by: Alastair D'Silva --- include/linux/printk.h | 1 + lib/hexdump.c | 30 +++++++++++++++++---- lib/test_hexdump.c | 60 +++++++++++++++++++++++++++++------------- 3 files changed, 68 insertions(+), 23 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index 5231a14e4593..15277d50159c 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -488,6 +488,7 @@ enum { #define HEXDUMP_2_GRP_SPACES (1 << 5) #define HEXDUMP_4_GRP_SPACES (1 << 6) #define HEXDUMP_8_GRP_SPACES (1 << 7) +#define HEXDUMP_RETAIN_BYTE_ORDER (1 << 8) extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, char *linebuf, size_t linebuflen, diff --git a/lib/hexdump.c b/lib/hexdump.c index febd614406d1..bfc9800630ae 100644 --- a/lib/hexdump.c +++ b/lib/hexdump.c @@ -127,7 +127,8 @@ static void separator_parameters(u64 flags, int groupsize, int *sep_chars, * @buf: data blob to dump * @len: number of bytes in the @buf * @rowsize: number of bytes to print per line; must be a multiple of groupsize - * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1) + * @groupsize: number of bytes to convert to a native endian number and print: + * 1, 2, 4, 8; default = 1 * @linebuf: where to put the converted data * @linebuflen: total size of @linebuf, including space for terminating NUL * @flags: A bitwise OR of the following flags: @@ -138,6 +139,9 @@ static void separator_parameters(u64 flags, int groupsize, int *sep_chars, * HEXDUMP_2_GRP_SPACES: insert a ' ' after every 2 groups * HEXDUMP_4_GRP_SPACES: insert a ' ' after every 4 groups * HEXDUMP_8_GRP_SPACES: insert a ' ' after every 8 groups + * HEXDUMP_RETAIN_BYTE_ORDER: Retain the byte ordering of groups + * instead of treating each group as a + * native-endian number * * hex_dump_to_buffer() works on one "line" of output at a time, converting * bytes of input to hexadecimal (and optionally printable ASCII) @@ -171,6 +175,7 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, int ret; int sep_chars = 0; char sep = 0; + bool big_endian = (flags & HEXDUMP_RETAIN_BYTE_ORDER) ? 1 : 0; if (!is_power_of_2(groupsize) || groupsize > 8) groupsize = 1; @@ -202,10 +207,13 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, const u64 *ptr8 = buf; for (j = 0; j < ngroups; j++) { + u64 val = big_endian ? + be64_to_cpu(get_unaligned(ptr8 + j)) : + get_unaligned(ptr8 + j); ret = snprintf(linebuf + lx, linebuflen - lx, "%s%16.16llx", j ? group_separator(j, flags) : "", - get_unaligned(ptr8 + j)); + val); if (ret >= linebuflen - lx) goto overflow1; lx += ret; @@ -214,10 +222,14 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, const u32 *ptr4 = buf; for (j = 0; j < ngroups; j++) { + u32 val = big_endian ? + be32_to_cpu(get_unaligned(ptr4 + j)) : + get_unaligned(ptr4 + j); + ret = snprintf(linebuf + lx, linebuflen - lx, "%s%8.8x", j ? group_separator(j, flags) : "", - get_unaligned(ptr4 + j)); + val); if (ret >= linebuflen - lx) goto overflow1; lx += ret; @@ -226,10 +238,14 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, const u16 *ptr2 = buf; for (j = 0; j < ngroups; j++) { + u16 val = big_endian ? + be16_to_cpu(get_unaligned(ptr2 + j)) : + get_unaligned(ptr2 + j); + ret = snprintf(linebuf + lx, linebuflen - lx, "%s%4.4x", j ? group_separator(j, flags) : "", - get_unaligned(ptr2 + j)); + val); if (ret >= linebuflen - lx) goto overflow1; lx += ret; @@ -331,7 +347,8 @@ static void announce_skipped(const char *level, const char *prefix_str, * @prefix_type: controls whether prefix of an offset, address, or none * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE) * @rowsize: number of bytes to print per line; must be a multiple of groupsize - * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1) + * @groupsize: number of bytes to convert to a native endian number and print: + * 1, 2, 4, 8; default = 1 * @buf: data blob to dump * @len: number of bytes in the @buf * @ascii: include ASCII after the hex output @@ -342,6 +359,9 @@ static void announce_skipped(const char *level, const char *prefix_str, * HEXDUMP_2_GRP_LINES: insert a '|' after every 2 groups * HEXDUMP_4_GRP_LINES: insert a '|' after every 4 groups * HEXDUMP_8_GRP_LINES: insert a '|' after every 8 groups + * HEXDUMP_RETAIN_BYTE_ORDER: Retain the byte ordering of groups + * instead of treating each group as a + * native-endian number * * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump * to the kernel log at the specified kernel log level, with an optional diff --git a/lib/test_hexdump.c b/lib/test_hexdump.c index ae340c5c1c6f..1e510e934568 100644 --- a/lib/test_hexdump.c +++ b/lib/test_hexdump.c @@ -98,14 +98,15 @@ static unsigned failed_tests __initdata; static void __init test_hexdump_prepare_test(size_t len, int rowsize, int groupsize, char *test, - size_t testlen, bool ascii) + size_t testlen, u64 flags) { char *p; const char * const *result; size_t l = len; int gs = groupsize, rs = rowsize; unsigned int i; - const bool is_be = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN); + const bool is_be = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) || + (flags & HEXDUMP_RETAIN_BYTE_ORDER); if (l > rs) l = rs; @@ -142,7 +143,7 @@ static void __init test_hexdump_prepare_test(size_t len, int rowsize, p--; /* ASCII part */ - if (ascii) { + if (flags & HEXDUMP_ASCII) { do { *p++ = ' '; } while (p < test + rs * 2 + rs / gs + 1); @@ -157,7 +158,7 @@ static void __init test_hexdump_prepare_test(size_t len, int rowsize, #define TEST_HEXDUMP_BUF_SIZE (64 * 3 + 2 + 64 + 1) static void __init test_hexdump(size_t len, int rowsize, int groupsize, - bool ascii) + u64 flags) { char test[TEST_HEXDUMP_BUF_SIZE]; char real[TEST_HEXDUMP_BUF_SIZE]; @@ -166,11 +167,11 @@ static void __init test_hexdump(size_t len, int rowsize, int groupsize, memset(real, FILL_CHAR, sizeof(real)); hex_dump_to_buffer(data_b, len, rowsize, groupsize, real, sizeof(real), - ascii ? HEXDUMP_ASCII : 0); + flags); memset(test, FILL_CHAR, sizeof(test)); test_hexdump_prepare_test(len, rowsize, groupsize, test, sizeof(test), - ascii); + flags); if (memcmp(test, real, TEST_HEXDUMP_BUF_SIZE)) { pr_err("Len: %zu row: %d group: %d\n", len, rowsize, groupsize); @@ -193,7 +194,7 @@ static void __init test_hexdump_set(int rowsize, bool ascii) static void __init test_hexdump_overflow(size_t buflen, size_t len, int rowsize, int groupsize, - bool ascii) + u64 flags) { char test[TEST_HEXDUMP_BUF_SIZE]; char buf[TEST_HEXDUMP_BUF_SIZE]; @@ -205,7 +206,7 @@ static void __init test_hexdump_overflow(size_t buflen, size_t len, memset(buf, FILL_CHAR, sizeof(buf)); rc = hex_dump_to_buffer(data_b, len, rowsize, groupsize, buf, buflen, - ascii ? HEXDUMP_ASCII : 0); + flags); /* * Caller must provide the data length multiple of groupsize. The @@ -222,12 +223,12 @@ static void __init test_hexdump_overflow(size_t buflen, size_t len, - 1 /* no trailing space */; } - expected_len = (ascii) ? ascii_len : hex_len; + expected_len = (flags & HEXDUMP_ASCII) ? ascii_len : hex_len; fill_point = min_t(int, expected_len + 1, buflen); if (buflen) { test_hexdump_prepare_test(len, rowsize, groupsize, test, - sizeof(test), ascii); + sizeof(test), flags); test[fill_point - 1] = '\0'; } memset(test + fill_point, FILL_CHAR, sizeof(test) - fill_point); @@ -237,8 +238,8 @@ static void __init test_hexdump_overflow(size_t buflen, size_t len, buf[sizeof(buf) - 1] = '\0'; if (!match) { - pr_err("rowsize: %u groupsize: %u ascii: %d Len: %zu buflen: %zu strlen: %zu\n", - rowsize, groupsize, ascii, len, buflen, + pr_err("rowsize: %u groupsize: %u flags: %llx Len: %zu buflen: %zu strlen: %zu\n", + rowsize, groupsize, flags, len, buflen, strnlen(buf, sizeof(buf))); pr_err("Result: %d '%-.*s'\n", rc, (int)buflen, buf); pr_err("Expect: %d '%-.*s'\n", expected_len, (int)buflen, test); @@ -247,7 +248,7 @@ static void __init test_hexdump_overflow(size_t buflen, size_t len, } } -static void __init test_hexdump_overflow_set(size_t buflen, bool ascii) +static void __init test_hexdump_overflow_set(size_t buflen, u64 flags) { unsigned int i = 0; int rs = (get_random_int() % 4 + 1) * 16; @@ -256,7 +257,7 @@ static void __init test_hexdump_overflow_set(size_t buflen, bool ascii) int gs = 1 << i; size_t len = get_random_int() % rs + gs; - test_hexdump_overflow(buflen, rounddown(len, gs), rs, gs, ascii); + test_hexdump_overflow(buflen, rounddown(len, gs), rs, gs, flags); } while (i++ < 3); } @@ -264,20 +265,43 @@ static int __init test_hexdump_init(void) { unsigned int i; int rowsize; + u64 flags; + flags = 0; rowsize = (get_random_int() % 4 + 1) * 16; for (i = 0; i < 16; i++) - test_hexdump_set(rowsize, false); + test_hexdump_set(rowsize, flags); + flags = HEXDUMP_ASCII; rowsize = (get_random_int() % 4 + 1) * 16; for (i = 0; i < 16; i++) - test_hexdump_set(rowsize, true); + test_hexdump_set(rowsize, flags); + flags = HEXDUMP_RETAIN_BYTE_ORDER; + rowsize = (get_random_int() % 2 + 1) * 16; + for (i = 0; i < 16; i++) + test_hexdump_set(rowsize, flags); + + flags = HEXDUMP_ASCII | HEXDUMP_RETAIN_BYTE_ORDER; + rowsize = (get_random_int() % 2 + 1) * 16; + for (i = 0; i < 16; i++) + test_hexdump_set(rowsize, flags); + + flags = 0; + for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++) + test_hexdump_overflow_set(i, flags); + + flags = HEXDUMP_ASCII; + for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++) + test_hexdump_overflow_set(i, flags); + + flags = HEXDUMP_RETAIN_BYTE_ORDER; for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++) - test_hexdump_overflow_set(i, false); + test_hexdump_overflow_set(i, flags); + flags = HEXDUMP_ASCII | HEXDUMP_RETAIN_BYTE_ORDER; for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++) - test_hexdump_overflow_set(i, true); + test_hexdump_overflow_set(i, flags); if (failed_tests == 0) pr_info("all %u tests passed\n", total_tests);