Skip to contentSkip to conversation

Introduce hexdump

PATCH Superseded Alexey Brodkin 1 participant 3 messages Jun 10, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover Introduce hexdump

This tiny series introduces functionality for either convertion a blob of data to "hex ASCII" in memory or direct printing in stdout.

The first patch is required to safe existing users of dummy blobs (as of today) that will become a real functions.

Note even though entire series is tagges as v2

Cc Stefan Roese <[email protected]>
Cc Tom Rini <[email protected]>

Alexey Brodkin (2):
linux-compat: Use original kern_levels.h for kernel debug levels
lib: Add hexdump

Changes v1 -> v2:
* Introduced linux/kern_levels.h so KERNEL_XXX symbols are really defined
* Move hexdump definitions to a separate header.
* Move helper funcions to the header so they might be used even if CONFIG_HEXDUMP is not enabled and if not used they will not occupy extra bytes of memory being "static inline" ones.
* While at it replace its own hex_asc[], hex_asc_{lo|hi}() and pack_hex_byte() in lib/vsprintf.c with generic ones from hexdump.
* In print_hex_dump() revert back first parametr for debug level, otherwise existing users of the function will be broken.

 include/common.h            |   1 +
 include/hexdump.h           | 108 ++++++++++++++++++++
 include/linux/compat.h      |  10 +-
 include/linux/kern_levels.h |  38 ++++++++
 lib/Kconfig                 |   5 +
 lib/Makefile                |   1 +
 lib/hexdump.c               | 233 ++++++++++++++++++++++++++++++++++++++++++++
 lib/vsprintf.c              |  17 +---
 8 files changed, 390 insertions(+), 23 deletions(-)
 create mode 100644 include/hexdump.h
 create mode 100644 include/linux/kern_levels.h
 create mode 100644 lib/hexdump.c
Signature · 1 line
2.5.5
#1 lore raw

PATCH v2 1/2 linux-compat: Use original kern_levels.h for kernel debug levels

Even currently in U-Boot we don't really use kernel debug levels we do have quite a lot of code that was copy-pasted from Linux kernel and so contains symbols like KERN_DEBUG and alike.

We used to just define those symbols which is fine if it is used in constructions like:
----------------->8--------------

    printk(KERN_DEBUG "My debug message");

----------------->8--------------

But in other places where the symbol gets passed as a separate argument (which we also have but luckily all of them are intself dummy stubs in U-Boot) that won't work.

Now as a preparation to introduction of a real implementation of print_hex_dump() we need those symbols to be defined as something real so we're taking them as they are from Linux kernel.

---

Changes v1 -> v2:
* This patch was only introduced in v2.

 include/linux/compat.h      |  9 +--------
 include/linux/kern_levels.h | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 8 deletions(-)
 create mode 100644 include/linux/kern_levels.h
include/linux/compat.h +1 -8
index e561ee3..0ce7d65 100644
@@ -28,14 +28,7 @@ extern struct p_current *current;
 #define printk	printf
 #define printk_once	printf
 
-#define KERN_EMERG
-#define KERN_ALERT
-#define KERN_CRIT
-#define KERN_ERR
-#define KERN_WARNING
-#define KERN_NOTICE
-#define KERN_INFO
-#define KERN_DEBUG
+#include <linux/kern_levels.h>
 
 #define GFP_ATOMIC ((gfp_t) 0)
 #define GFP_KERNEL ((gfp_t) 0)
include/linux/kern_levels.h new file +38 -0
new file mode 100644
index 0000000..c2ce155
@@ -0,0 +1,38 @@
+#ifndef __KERN_LEVELS_H__
+#define __KERN_LEVELS_H__
+
+#define KERN_SOH	"\001"		/* ASCII Start Of Header */
+#define KERN_SOH_ASCII	'\001'
+
+#define KERN_EMERG	KERN_SOH "0"	/* system is unusable */
+#define KERN_ALERT	KERN_SOH "1"	/* action must be taken immediately */
+#define KERN_CRIT	KERN_SOH "2"	/* critical conditions */
+#define KERN_ERR	KERN_SOH "3"	/* error conditions */
+#define KERN_WARNING	KERN_SOH "4"	/* warning conditions */
+#define KERN_NOTICE	KERN_SOH "5"	/* normal but significant condition */
+#define KERN_INFO	KERN_SOH "6"	/* informational */
+#define KERN_DEBUG	KERN_SOH "7"	/* debug-level messages */
+
+#define KERN_DEFAULT	KERN_SOH "d"	/* the default kernel loglevel */
+
+/*
+ * Annotation for a "continued" line of log printout (only done after a
+ * line that had no enclosing \n). Only to be used by core/arch code
+ * during early bootup (a continued line is not SMP-safe otherwise).
+ */
+#define KERN_CONT	""
+
+/* integer equivalents of KERN_<LEVEL> */
+#define LOGLEVEL_SCHED		-2	/* Deferred messages from sched code
+					 * are set to this special level */
+#define LOGLEVEL_DEFAULT	-1	/* default (or last) loglevel */
+#define LOGLEVEL_EMERG		0	/* system is unusable */
+#define LOGLEVEL_ALERT		1	/* action must be taken immediately */
+#define LOGLEVEL_CRIT		2	/* critical conditions */
+#define LOGLEVEL_ERR		3	/* error conditions */
+#define LOGLEVEL_WARNING	4	/* warning conditions */
+#define LOGLEVEL_NOTICE		5	/* normal but significant condition */
+#define LOGLEVEL_INFO		6	/* informational */
+#define LOGLEVEL_DEBUG		7	/* debug-level messages */
+
+#endif
Signature · 1 line
2.5.5
#2 lore raw

PATCH v2 2/2 lib: Add hexdump

In reply to Alexey Brodkin

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 at 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 at e0022000.stdou
000000b0: 74 3d 73 65 72 69 61 6c 30 40 65 30 30 32 32 30  t=serial0 at 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.

---

Changes v1 -> v2:
* Move hexdump definitions to a separate header.
* Move helper funcions to the header so they might be used even if CONFIG_HEXDUMP is not enabled and if not used they will not occupy extra bytes of memory being "static inline" ones.
* While at it replace its own hex_asc[], hex_asc_{lo|hi}() and pack_hex_byte() in lib/vsprintf.c with generic ones from hexdump.
* In print_hex_dump() revert back first parametr for debug level, otherwise existing users of the function will be broken.

 include/common.h       |   1 +
 include/hexdump.h      | 108 +++++++++++++++++++++++
 include/linux/compat.h |   1 -
 lib/Kconfig            |   5 ++
 lib/Makefile           |   1 +
 lib/hexdump.c          | 233 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/vsprintf.c         |  17 +---
 7 files changed, 351 insertions(+), 15 deletions(-)
 create mode 100644 include/hexdump.h
 create mode 100644 lib/hexdump.c
include/common.h +1 -0
index f9f4605..51a438c 100644
@@ -23,6 +23,7 @@ typedef volatile unsigned char	vu_char;
 #include <linux/stringify.h>
 #include <asm/ptrace.h>
 #include <stdarg.h>
+#include <hexdump.h>
 #include <linux/kernel.h>
 #if defined(CONFIG_PCI) && defined(CONFIG_4xx)
 #include <pci.h>
include/hexdump.h new file +108 -0

108 lines not on this page · raw message

include/linux/compat.h +0 -1
index 0ce7d65..074a85e 100644
@@ -193,7 +193,6 @@ typedef unsigned long blkcnt_t;
 #define init_waitqueue_head(...)	do { } while (0)
 #define wait_event_interruptible(...)	0
 #define wake_up_interruptible(...)	do { } while (0)
-#define print_hex_dump(...)		do { } while (0)
 #define dump_stack(...)			do { } while (0)
 
 #define task_pid_nr(x)			0
lib/Kconfig +5 -0
index 2b97c2b..a563b5e 100644
@@ -128,6 +128,11 @@ config ERRNO_STR
 	  - if errno is null or positive number - a pointer to "Success" message
 	  - if errno is negative - a pointer to errno related message
 
+config HEXDUMP
+	bool "Enable hexdump"
+	help
+	  This enables functions for printing dumps of binary data.
+
 config OF_LIBFDT
 	bool "Enable the FDT library"
 	default y if OF_CONTROL
lib/Makefile +1 -0
index 02dfa29..1bf7c37 100644
@@ -76,6 +76,7 @@ obj-y += membuff.o
 obj-$(CONFIG_REGEX) += slre.o
 obj-y += string.o
 obj-y += time.o
+obj-y += hexdump.o
 obj-$(CONFIG_TRACE) += trace.o
 obj-$(CONFIG_LIB_UUID) += uuid.o
 obj-$(CONFIG_LIB_RAND) += rand.o
lib/hexdump.c new file +233 -0

233 lines not on this page · raw message

lib/vsprintf.c +3 -14
index 874a295..c0a0aaa 100644
@@ -271,17 +271,6 @@ static char *string(char *buf, char *end, char *s, int field_width,
 }
 
 #ifdef CONFIG_CMD_NET
-static const char hex_asc[] = "0123456789abcdef";
-#define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
-#define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
-
-static inline char *pack_hex_byte(char *buf, u8 byte)
-{
-	*buf++ = hex_asc_hi(byte);
-	*buf++ = hex_asc_lo(byte);
-	return buf;
-}
-
 static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
 				int precision, int flags)
 {
@@ -291,7 +280,7 @@ static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
 	int i;
 
 	for (i = 0; i < 6; i++) {
-		p = pack_hex_byte(p, addr[i]);
+		p = hex_byte_pack(p, addr[i]);
 		if (!(flags & SPECIAL) && i != 5)
 			*p++ = ':';
 	}
@@ -310,8 +299,8 @@ static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
 	int i;
 
 	for (i = 0; i < 8; i++) {
-		p = pack_hex_byte(p, addr[2 * i]);
-		p = pack_hex_byte(p, addr[2 * i + 1]);
+		p = hex_byte_pack(p, addr[2 * i]);
+		p = hex_byte_pack(p, addr[2 * i + 1]);
 		if (!(flags & SPECIAL) && i != 7)
 			*p++ = ':';
 	}
Signature · 1 line
2.5.5