Browse Source

lib: Add hexdump

Often during debugging session it's very interesting to see
what data we were dealing with. For example what we write or read
to/from memory or peripherals.

This change introduces functions that allow to dump binary
data with one simple function invocation like:
------------------->8----------------
print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
------------------->8----------------

which gives us the following:
------------------->8----------------
00000000: f2 b7 c9 88 62 61 75 64 72 61 74 65 3d 31 31 35  ....baudrate=115
00000010: 32 30 30 00 62 6f 6f 74 61 72 67 73 3d 63 6f 6e  200.bootargs=con
00000020: 73 6f 6c 65 3d 74 74 79 53 33 2c 31 31 35 32 30  sole=ttyS3,11520
00000030: 30 6e 38 00 62 6f 6f 74 64 65 6c 61 79 3d 33 00  0n8.bootdelay=3.
00000040: 62 6f 6f 74 66 69 6c 65 3d 75 49 6d 61 67 65 00  bootfile=uImage.
00000050: 66 64 74 63 6f 6e 74 72 6f 6c 61 64 64 72 3d 39  fdtcontroladdr=9
00000060: 66 66 62 31 62 61 30 00 6c 6f 61 64 61 64 64 72  ffb1ba0.loadaddr
00000070: 3d 30 78 38 32 30 30 30 30 30 30 00 73 74 64 65  =0x82000000.stde
00000080: 72 72 3d 73 65 72 69 61 6c 30 40 65 30 30 32 32  rr=serial0@e0022
00000090: 30 30 30 00 73 74 64 69 6e 3d 73 65 72 69 61 6c  000.stdin=serial
000000a0: 30 40 65 30 30 32 32 30 30 30 00 73 74 64 6f 75  0@e0022000.stdou
000000b0: 74 3d 73 65 72 69 61 6c 30 40 65 30 30 32 32 30  t=serial0@e00220
000000c0: 30 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00  00..............
...
------------------->8----------------

Source of hexdump.c was copied from Linux kernel v4.7-rc2.

Signed-off-by: Alexey Brodkin <abrodkin@synopsys.com>
Cc: Anatolij Gustschin <agust@denx.de>
Cc: Mario Six <mario.six@gdsys.cc>
Cc: Simon Glass <sjg@chromium.org>
Cc: Tom Rini <trini@konsulko.com>
Cc: Stefan Roese <sr@denx.de>
Alexey Brodkin 4 years ago
parent
commit
f8c987f8f1

+ 1 - 1
drivers/mtd/ubi/attach.c

@@ -789,7 +789,7 @@ static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
789 789
 	ubi_dump_vid_hdr(vid_hdr);
790 790
 	pr_err("hexdump of PEB %d offset %d, length %d",
791 791
 	       pnum, ubi->leb_start, ubi->leb_size);
792
-	ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
792
+	ubi_dbg_print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
793 793
 			       ubi->peb_buf, ubi->leb_size, 1);
794 794
 	err = 1;
795 795
 

+ 4 - 3
drivers/mtd/ubi/debug.c

@@ -5,6 +5,7 @@
5 5
  * Author: Artem Bityutskiy (Битюцкий Артём)
6 6
  */
7 7
 
8
+#include <hexdump.h>
8 9
 #include <ubi_uboot.h>
9 10
 #include "ubi.h"
10 11
 #ifndef __UBOOT__
@@ -39,7 +40,7 @@ void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
39 40
 
40 41
 	ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d",
41 42
 		len, pnum, offset);
42
-	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
43
+	print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
43 44
 out:
44 45
 	vfree(buf);
45 46
 	return;
@@ -60,7 +61,7 @@ void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
60 61
 	pr_err("\timage_seq      %d\n", be32_to_cpu(ec_hdr->image_seq));
61 62
 	pr_err("\thdr_crc        %#08x\n", be32_to_cpu(ec_hdr->hdr_crc));
62 63
 	pr_err("erase counter header hexdump:\n");
63
-	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
64
+	print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
64 65
 		       ec_hdr, UBI_EC_HDR_SIZE, 1);
65 66
 }
66 67
 
@@ -85,7 +86,7 @@ void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
85 86
 		(unsigned long long)be64_to_cpu(vid_hdr->sqnum));
86 87
 	pr_err("\thdr_crc   %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
87 88
 	pr_err("Volume identifier header hexdump:\n");
88
-	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
89
+	print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
89 90
 		       vid_hdr, UBI_VID_HDR_SIZE, 1);
90 91
 }
91 92
 

+ 4 - 2
drivers/mtd/ubi/debug.h

@@ -16,6 +16,8 @@ void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr);
16 16
 #include <linux/random.h>
17 17
 #endif
18 18
 
19
+#include <hexdump.h>
20
+
19 21
 #define ubi_assert(expr)  do {                                               \
20 22
 	if (unlikely(!(expr))) {                                             \
21 23
 		pr_crit("UBI assert failed in %s at %u (pid %d)\n",          \
@@ -24,8 +26,8 @@ void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr);
24 26
 	}                                                                    \
25 27
 } while (0)
26 28
 
27
-#define ubi_dbg_print_hex_dump(l, ps, pt, r, g, b, len, a)                   \
28
-		print_hex_dump(l, ps, pt, r, g, b, len, a)
29
+#define ubi_dbg_print_hex_dump(ps, pt, r, g, b, len, a)                      \
30
+		print_hex_dump(ps, pt, r, g, b, len, a)
29 31
 
30 32
 #define ubi_dbg_msg(type, fmt, ...) \
31 33
 	pr_debug("UBI DBG " type " (pid %d): " fmt "\n", current->pid,       \

+ 4 - 3
drivers/mtd/ubi/io.c

@@ -78,6 +78,7 @@
78 78
 #include <linux/err.h>
79 79
 #include <linux/slab.h>
80 80
 #else
81
+#include <hexdump.h>
81 82
 #include <ubi_uboot.h>
82 83
 #endif
83 84
 
@@ -1353,11 +1354,11 @@ static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum,
1353 1354
 		ubi_msg(ubi, "data differ at position %d", i);
1354 1355
 		ubi_msg(ubi, "hex dump of the original buffer from %d to %d",
1355 1356
 			i, i + dump_len);
1356
-		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1357
+		print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
1357 1358
 			       buf + i, dump_len, 1);
1358 1359
 		ubi_msg(ubi, "hex dump of the read buffer from %d to %d",
1359 1360
 			i, i + dump_len);
1360
-		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1361
+		print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
1361 1362
 			       buf1 + i, dump_len, 1);
1362 1363
 		dump_stack();
1363 1364
 		err = -EINVAL;
@@ -1419,7 +1420,7 @@ int ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
1419 1420
 fail:
1420 1421
 	ubi_err(ubi, "self-check failed for PEB %d", pnum);
1421 1422
 	ubi_msg(ubi, "hex dump of the %d-%d region", offset, offset + len);
1422
-	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
1423
+	print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
1423 1424
 	err = -EINVAL;
1424 1425
 error:
1425 1426
 	dump_stack();

+ 1 - 0
drivers/usb/gadget/f_mass_storage.c

@@ -240,6 +240,7 @@
240 240
 /* #define DUMP_MSGS */
241 241
 
242 242
 #include <config.h>
243
+#include <hexdump.h>
243 244
 #include <malloc.h>
244 245
 #include <common.h>
245 246
 #include <console.h>

+ 2 - 2
drivers/usb/gadget/storage_common.c

@@ -124,7 +124,7 @@
124 124
 # define dump_msg(fsg, label, buf, length) do {                         \
125 125
 	if (length < 512) {						\
126 126
 		DBG(fsg, "%s, length %u:\n", label, length);		\
127
-		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,	\
127
+		print_hex_dump("", DUMP_PREFIX_OFFSET,	\
128 128
 			       16, 1, buf, length, 0);			\
129 129
 	}								\
130 130
 } while (0)
@@ -139,7 +139,7 @@
139 139
 #  ifdef VERBOSE_DEBUG
140 140
 
141 141
 #    define dump_cdb(fsg)						\
142
-	print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE,	\
142
+	print_hex_dump("SCSI CDB: ", DUMP_PREFIX_NONE,	\
143 143
 		       16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0)		\
144 144
 
145 145
 #  else

+ 1 - 0
examples/api/Makefile

@@ -32,6 +32,7 @@ OBJ-y += libgenwrap.o
32 32
 EXT_COBJ-y += lib/crc32.o
33 33
 EXT_COBJ-y += lib/ctype.o
34 34
 EXT_COBJ-y += lib/div64.o
35
+EXT_COBJ-y += lib/hexdump.o
35 36
 EXT_COBJ-y += lib/string.o
36 37
 EXT_COBJ-y += lib/time.o
37 38
 EXT_COBJ-y += lib/vsprintf.o

+ 4 - 2
fs/ubifs/debug.c

@@ -15,6 +15,8 @@
15 15
  * various local functions of those subsystems.
16 16
  */
17 17
 
18
+#include <hexdump.h>
19
+
18 20
 #ifndef __UBOOT__
19 21
 #include <linux/module.h>
20 22
 #include <linux/debugfs.h>
@@ -307,7 +309,7 @@ void ubifs_dump_node(const struct ubifs_info *c, const void *node)
307 309
 	/* If the magic is incorrect, just hexdump the first bytes */
308 310
 	if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
309 311
 		pr_err("Not a node, first %zu bytes:", UBIFS_CH_SZ);
310
-		print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 32, 1,
312
+		print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
311 313
 			       (void *)node, UBIFS_CH_SZ, 1);
312 314
 		return;
313 315
 	}
@@ -482,7 +484,7 @@ void ubifs_dump_node(const struct ubifs_info *c, const void *node)
482 484
 		       (int)le16_to_cpu(dn->compr_type));
483 485
 		pr_err("\tdata size      %d\n", dlen);
484 486
 		pr_err("\tdata:\n");
485
-		print_hex_dump(KERN_ERR, "\t", DUMP_PREFIX_OFFSET, 32, 1,
487
+		print_hex_dump("\t", DUMP_PREFIX_OFFSET, 32, 1,
486 488
 			       (void *)&dn->data, dlen, 0);
487 489
 		break;
488 490
 	}

+ 2 - 1
fs/ubifs/scan.c

@@ -16,6 +16,7 @@
16 16
  */
17 17
 
18 18
 #ifdef __UBOOT__
19
+#include <hexdump.h>
19 20
 #include <linux/err.h>
20 21
 #endif
21 22
 #include "ubifs.h"
@@ -236,7 +237,7 @@ void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
236 237
 	if (len > 8192)
237 238
 		len = 8192;
238 239
 	ubifs_err(c, "first %d bytes from LEB %d:%d", len, lnum, offs);
239
-	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1);
240
+	print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1);
240 241
 }
241 242
 
242 243
 /**

+ 91 - 0
include/hexdump.h

@@ -0,0 +1,91 @@
1
+/* SPDX-License-Identifier: GPL-2.0+ */
2
+/*
3
+ * Copyright (C) 2018 Synopsys, Inc. All rights reserved.
4
+ *
5
+ */
6
+
7
+#ifndef HEXDUMP_H
8
+#define HEXDUMP_H
9
+
10
+#include <linux/ctype.h>
11
+#include <linux/types.h>
12
+
13
+enum {
14
+	DUMP_PREFIX_NONE,
15
+	DUMP_PREFIX_ADDRESS,
16
+	DUMP_PREFIX_OFFSET
17
+};
18
+
19
+extern const char hex_asc[];
20
+#define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
21
+#define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
22
+
23
+static inline char *hex_byte_pack(char *buf, u8 byte)
24
+{
25
+	*buf++ = hex_asc_hi(byte);
26
+	*buf++ = hex_asc_lo(byte);
27
+	return buf;
28
+}
29
+
30
+/**
31
+ * hex_to_bin - convert a hex digit to its real value
32
+ * @ch: ascii character represents hex digit
33
+ *
34
+ * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
35
+ * input.
36
+ */
37
+static inline int hex_to_bin(char ch)
38
+{
39
+	if ((ch >= '0') && (ch <= '9'))
40
+		return ch - '0';
41
+	ch = tolower(ch);
42
+	if ((ch >= 'a') && (ch <= 'f'))
43
+		return ch - 'a' + 10;
44
+	return -1;
45
+}
46
+
47
+/**
48
+ * hex2bin - convert an ascii hexadecimal string to its binary representation
49
+ * @dst: binary result
50
+ * @src: ascii hexadecimal string
51
+ * @count: result length
52
+ *
53
+ * Return 0 on success, -1 in case of bad input.
54
+ */
55
+static inline int hex2bin(u8 *dst, const char *src, size_t count)
56
+{
57
+	while (count--) {
58
+		int hi = hex_to_bin(*src++);
59
+		int lo = hex_to_bin(*src++);
60
+
61
+		if ((hi < 0) || (lo < 0))
62
+			return -1;
63
+
64
+		*dst++ = (hi << 4) | lo;
65
+	}
66
+	return 0;
67
+}
68
+
69
+/**
70
+ * bin2hex - convert binary data to an ascii hexadecimal string
71
+ * @dst: ascii hexadecimal result
72
+ * @src: binary data
73
+ * @count: binary data length
74
+ */
75
+static inline char *bin2hex(char *dst, const void *src, size_t count)
76
+{
77
+	const unsigned char *_src = src;
78
+
79
+	while (count--)
80
+		dst = hex_byte_pack(dst, *_src++);
81
+	return dst;
82
+}
83
+
84
+int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
85
+		       char *linebuf, size_t linebuflen, bool ascii);
86
+void print_hex_dump(const char *prefix_str, int prefix_type, int rowsize,
87
+		    int groupsize, const void *buf, size_t len, bool ascii);
88
+void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
89
+			  const void *buf, size_t len);
90
+
91
+#endif /* HEXDUMP_H */

+ 0 - 1
include/linux/compat.h

@@ -193,7 +193,6 @@ typedef unsigned long blkcnt_t;
193 193
 #define init_waitqueue_head(...)	do { } while (0)
194 194
 #define wait_event_interruptible(...)	0
195 195
 #define wake_up_interruptible(...)	do { } while (0)
196
-#define print_hex_dump(...)		do { } while (0)
197 196
 #define dump_stack(...)			do { } while (0)
198 197
 
199 198
 #define task_pid_nr(x)			0

+ 5 - 0
lib/Kconfig

@@ -285,6 +285,11 @@ config ERRNO_STR
285 285
 	  - if errno is null or positive number - a pointer to "Success" message
286 286
 	  - if errno is negative - a pointer to errno related message
287 287
 
288
+config HEXDUMP
289
+	bool "Enable hexdump"
290
+	help
291
+	  This enables functions for printing dumps of binary data.
292
+
288 293
 config OF_LIBFDT
289 294
 	bool "Enable the FDT library"
290 295
 	default y if OF_CONTROL

+ 1 - 0
lib/Makefile

@@ -86,6 +86,7 @@ obj-$(CONFIG_REGEX) += slre.o
86 86
 obj-y += string.o
87 87
 obj-y += tables_csum.o
88 88
 obj-y += time.o
89
+obj-y += hexdump.o
89 90
 obj-$(CONFIG_TRACE) += trace.o
90 91
 obj-$(CONFIG_LIB_UUID) += uuid.o
91 92
 obj-$(CONFIG_LIB_RAND) += rand.o

+ 245 - 0
lib/hexdump.c

@@ -0,0 +1,245 @@
1
+// SPDX-License-Identifier: GPL-2.0+
2
+/*
3
+ * lib/hexdump.c
4
+ *
5
+ * This program is free software; you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License version 2 as
7
+ * published by the Free Software Foundation. See README and COPYING for
8
+ * more details.
9
+ */
10
+
11
+#include <common.h>
12
+#include <hexdump.h>
13
+#include <linux/ctype.h>
14
+#include <linux/compat.h>
15
+#include <linux/log2.h>
16
+#include <asm/unaligned.h>
17
+
18
+const char hex_asc[] = "0123456789abcdef";
19
+const char hex_asc_upper[] = "0123456789ABCDEF";
20
+
21
+#ifdef CONFIG_HEXDUMP
22
+/**
23
+ * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
24
+ * @buf: data blob to dump
25
+ * @len: number of bytes in the @buf
26
+ * @rowsize: number of bytes to print per line; must be 16 or 32
27
+ * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
28
+ * @linebuf: where to put the converted data
29
+ * @linebuflen: total size of @linebuf, including space for terminating NUL
30
+ * @ascii: include ASCII after the hex output
31
+ *
32
+ * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
33
+ * 16 or 32 bytes of input data converted to hex + ASCII output.
34
+ *
35
+ * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
36
+ * to a hex + ASCII dump at the supplied memory location.
37
+ * The converted output is always NUL-terminated.
38
+ *
39
+ * E.g.:
40
+ *   hex_dump_to_buffer(frame->data, frame->len, 16, 1,
41
+ *			linebuf, sizeof(linebuf), true);
42
+ *
43
+ * example output buffer:
44
+ * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
45
+ *
46
+ * Return:
47
+ * The amount of bytes placed in the buffer without terminating NUL. If the
48
+ * output was truncated, then the return value is the number of bytes
49
+ * (excluding the terminating NUL) which would have been written to the final
50
+ * string if enough space had been available.
51
+ */
52
+int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
53
+		       char *linebuf, size_t linebuflen, bool ascii)
54
+{
55
+	const u8 *ptr = buf;
56
+	int ngroups;
57
+	u8 ch;
58
+	int j, lx = 0;
59
+	int ascii_column;
60
+	int ret;
61
+
62
+	if (rowsize != 16 && rowsize != 32)
63
+		rowsize = 16;
64
+
65
+	if (len > rowsize)		/* limit to one line at a time */
66
+		len = rowsize;
67
+	if (!is_power_of_2(groupsize) || groupsize > 8)
68
+		groupsize = 1;
69
+	if ((len % groupsize) != 0)	/* no mixed size output */
70
+		groupsize = 1;
71
+
72
+	ngroups = len / groupsize;
73
+	ascii_column = rowsize * 2 + rowsize / groupsize + 1;
74
+
75
+	if (!linebuflen)
76
+		goto overflow1;
77
+
78
+	if (!len)
79
+		goto nil;
80
+
81
+	if (groupsize == 8) {
82
+		const u64 *ptr8 = buf;
83
+
84
+		for (j = 0; j < ngroups; j++) {
85
+			ret = snprintf(linebuf + lx, linebuflen - lx,
86
+				       "%s%16.16llx", j ? " " : "",
87
+				       get_unaligned(ptr8 + j));
88
+			if (ret >= linebuflen - lx)
89
+				goto overflow1;
90
+			lx += ret;
91
+		}
92
+	} else if (groupsize == 4) {
93
+		const u32 *ptr4 = buf;
94
+
95
+		for (j = 0; j < ngroups; j++) {
96
+			ret = snprintf(linebuf + lx, linebuflen - lx,
97
+				       "%s%8.8x", j ? " " : "",
98
+				       get_unaligned(ptr4 + j));
99
+			if (ret >= linebuflen - lx)
100
+				goto overflow1;
101
+			lx += ret;
102
+		}
103
+	} else if (groupsize == 2) {
104
+		const u16 *ptr2 = buf;
105
+
106
+		for (j = 0; j < ngroups; j++) {
107
+			ret = snprintf(linebuf + lx, linebuflen - lx,
108
+				       "%s%4.4x", j ? " " : "",
109
+				       get_unaligned(ptr2 + j));
110
+			if (ret >= linebuflen - lx)
111
+				goto overflow1;
112
+			lx += ret;
113
+		}
114
+	} else {
115
+		for (j = 0; j < len; j++) {
116
+			if (linebuflen < lx + 2)
117
+				goto overflow2;
118
+			ch = ptr[j];
119
+			linebuf[lx++] = hex_asc_hi(ch);
120
+			if (linebuflen < lx + 2)
121
+				goto overflow2;
122
+			linebuf[lx++] = hex_asc_lo(ch);
123
+			if (linebuflen < lx + 2)
124
+				goto overflow2;
125
+			linebuf[lx++] = ' ';
126
+		}
127
+		if (j)
128
+			lx--;
129
+	}
130
+	if (!ascii)
131
+		goto nil;
132
+
133
+	while (lx < ascii_column) {
134
+		if (linebuflen < lx + 2)
135
+			goto overflow2;
136
+		linebuf[lx++] = ' ';
137
+	}
138
+	for (j = 0; j < len; j++) {
139
+		if (linebuflen < lx + 2)
140
+			goto overflow2;
141
+		ch = ptr[j];
142
+		linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
143
+	}
144
+nil:
145
+	linebuf[lx] = '\0';
146
+	return lx;
147
+overflow2:
148
+	linebuf[lx++] = '\0';
149
+overflow1:
150
+	return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
151
+}
152
+
153
+/**
154
+ * print_hex_dump - print a text hex dump to syslog for a binary blob of data
155
+ * @prefix_str: string to prefix each line with;
156
+ *  caller supplies trailing spaces for alignment if desired
157
+ * @prefix_type: controls whether prefix of an offset, address, or none
158
+ *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
159
+ * @rowsize: number of bytes to print per line; must be 16 or 32
160
+ * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
161
+ * @buf: data blob to dump
162
+ * @len: number of bytes in the @buf
163
+ * @ascii: include ASCII after the hex output
164
+ *
165
+ * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
166
+ * to the stdio, with an optional leading prefix.
167
+ *
168
+ * print_hex_dump() works on one "line" of output at a time, i.e.,
169
+ * 16 or 32 bytes of input data converted to hex + ASCII output.
170
+ * print_hex_dump() iterates over the entire input @buf, breaking it into
171
+ * "line size" chunks to format and print.
172
+ *
173
+ * E.g.:
174
+ *   print_hex_dump("raw data: ", DUMP_PREFIX_ADDRESS, 16, 1, frame->data,
175
+ *                  frame->len, true);
176
+ *
177
+ * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
178
+ * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
179
+ * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
180
+ * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c  pqrstuvwxyz{|}~.
181
+ */
182
+void print_hex_dump(const char *prefix_str, int prefix_type, int rowsize,
183
+		    int groupsize, const void *buf, size_t len, bool ascii)
184
+{
185
+	const u8 *ptr = buf;
186
+	int i, linelen, remaining = len;
187
+	char linebuf[32 * 3 + 2 + 32 + 1];
188
+
189
+	if (rowsize != 16 && rowsize != 32)
190
+		rowsize = 16;
191
+
192
+	for (i = 0; i < len; i += rowsize) {
193
+		linelen = min(remaining, rowsize);
194
+		remaining -= rowsize;
195
+
196
+		hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
197
+				   linebuf, sizeof(linebuf), ascii);
198
+
199
+		switch (prefix_type) {
200
+		case DUMP_PREFIX_ADDRESS:
201
+			printf("%s%p: %s\n", prefix_str, ptr + i, linebuf);
202
+			break;
203
+		case DUMP_PREFIX_OFFSET:
204
+			printf("%s%.8x: %s\n", prefix_str, i, linebuf);
205
+			break;
206
+		default:
207
+			printf("%s%s\n", prefix_str, linebuf);
208
+			break;
209
+		}
210
+	}
211
+}
212
+
213
+/**
214
+ * print_hex_dump_bytes - shorthand form of print_hex_dump() with default params
215
+ * @prefix_str: string to prefix each line with;
216
+ *  caller supplies trailing spaces for alignment if desired
217
+ * @prefix_type: controls whether prefix of an offset, address, or none
218
+ *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
219
+ * @buf: data blob to dump
220
+ * @len: number of bytes in the @buf
221
+ *
222
+ * Calls print_hex_dump(), rowsize of 16, groupsize of 1,
223
+ * and ASCII output included.
224
+ */
225
+void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
226
+			  const void *buf, size_t len)
227
+{
228
+	print_hex_dump(prefix_str, prefix_type, 16, 1, buf, len, true);
229
+}
230
+#else
231
+/*
232
+ * Some code in U-Boot copy-pasted from Linux kernel uses both
233
+ * functions below so to keep stuff compilable we keep these stubs here.
234
+ */
235
+void print_hex_dump(const char *prefix_str, int prefix_type,
236
+				  int rowsize, int groupsize, const void *buf,
237
+				  size_t len, bool ascii)
238
+{
239
+}
240
+
241
+void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
242
+					const void *buf, size_t len)
243
+{
244
+}
245
+#endif /* CONFIG_HEXDUMP */

+ 4 - 14
lib/vsprintf.c

@@ -15,6 +15,7 @@
15 15
 #include <charset.h>
16 16
 #include <efi_loader.h>
17 17
 #include <div64.h>
18
+#include <hexdump.h>
18 19
 #include <uuid.h>
19 20
 #include <stdarg.h>
20 21
 #include <linux/ctype.h>
@@ -315,17 +316,6 @@ static char *device_path_string(char *buf, char *end, void *dp, int field_width,
315 316
 #endif
316 317
 
317 318
 #ifdef CONFIG_CMD_NET
318
-static const char hex_asc[] = "0123456789abcdef";
319
-#define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
320
-#define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
321
-
322
-static inline char *pack_hex_byte(char *buf, u8 byte)
323
-{
324
-	*buf++ = hex_asc_hi(byte);
325
-	*buf++ = hex_asc_lo(byte);
326
-	return buf;
327
-}
328
-
329 319
 static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
330 320
 				int precision, int flags)
331 321
 {
@@ -335,7 +325,7 @@ static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
335 325
 	int i;
336 326
 
337 327
 	for (i = 0; i < 6; i++) {
338
-		p = pack_hex_byte(p, addr[i]);
328
+		p = hex_byte_pack(p, addr[i]);
339 329
 		if (!(flags & SPECIAL) && i != 5)
340 330
 			*p++ = ':';
341 331
 	}
@@ -354,8 +344,8 @@ static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
354 344
 	int i;
355 345
 
356 346
 	for (i = 0; i < 8; i++) {
357
-		p = pack_hex_byte(p, addr[2 * i]);
358
-		p = pack_hex_byte(p, addr[2 * i + 1]);
347
+		p = hex_byte_pack(p, addr[2 * i]);
348
+		p = hex_byte_pack(p, addr[2 * i + 1]);
359 349
 		if (!(flags & SPECIAL) && i != 7)
360 350
 			*p++ = ':';
361 351
 	}