Skip to content

Commit f8c987f

Browse files
abrodkintrini
authored andcommitted
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>
1 parent f559180 commit f8c987f

File tree

15 files changed

+369
-29
lines changed

15 files changed

+369
-29
lines changed

drivers/mtd/ubi/attach.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
789789
ubi_dump_vid_hdr(vid_hdr);
790790
pr_err("hexdump of PEB %d offset %d, length %d",
791791
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,
793793
ubi->peb_buf, ubi->leb_size, 1);
794794
err = 1;
795795

drivers/mtd/ubi/debug.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Author: Artem Bityutskiy (Битюцкий Артём)
66
*/
77

8+
#include <hexdump.h>
89
#include <ubi_uboot.h>
910
#include "ubi.h"
1011
#ifndef __UBOOT__
@@ -39,7 +40,7 @@ void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len)
3940

4041
ubi_msg(ubi, "dumping %d bytes of data from PEB %d, offset %d",
4142
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);
4344
out:
4445
vfree(buf);
4546
return;
@@ -60,7 +61,7 @@ void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
6061
pr_err("\timage_seq %d\n", be32_to_cpu(ec_hdr->image_seq));
6162
pr_err("\thdr_crc %#08x\n", be32_to_cpu(ec_hdr->hdr_crc));
6263
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,
6465
ec_hdr, UBI_EC_HDR_SIZE, 1);
6566
}
6667

@@ -85,7 +86,7 @@ void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
8586
(unsigned long long)be64_to_cpu(vid_hdr->sqnum));
8687
pr_err("\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
8788
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,
8990
vid_hdr, UBI_VID_HDR_SIZE, 1);
9091
}
9192

drivers/mtd/ubi/debug.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr);
1616
#include <linux/random.h>
1717
#endif
1818

19+
#include <hexdump.h>
20+
1921
#define ubi_assert(expr) do { \
2022
if (unlikely(!(expr))) { \
2123
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);
2426
} \
2527
} while (0)
2628

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)
2931

3032
#define ubi_dbg_msg(type, fmt, ...) \
3133
pr_debug("UBI DBG " type " (pid %d): " fmt "\n", current->pid, \

drivers/mtd/ubi/io.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
#include <linux/err.h>
7979
#include <linux/slab.h>
8080
#else
81+
#include <hexdump.h>
8182
#include <ubi_uboot.h>
8283
#endif
8384

@@ -1353,11 +1354,11 @@ static int self_check_write(struct ubi_device *ubi, const void *buf, int pnum,
13531354
ubi_msg(ubi, "data differ at position %d", i);
13541355
ubi_msg(ubi, "hex dump of the original buffer from %d to %d",
13551356
i, i + dump_len);
1356-
print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1357+
print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
13571358
buf + i, dump_len, 1);
13581359
ubi_msg(ubi, "hex dump of the read buffer from %d to %d",
13591360
i, i + dump_len);
1360-
print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1361+
print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
13611362
buf1 + i, dump_len, 1);
13621363
dump_stack();
13631364
err = -EINVAL;
@@ -1419,7 +1420,7 @@ int ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
14191420
fail:
14201421
ubi_err(ubi, "self-check failed for PEB %d", pnum);
14211422
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);
14231424
err = -EINVAL;
14241425
error:
14251426
dump_stack();

drivers/usb/gadget/f_mass_storage.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@
240240
/* #define DUMP_MSGS */
241241

242242
#include <config.h>
243+
#include <hexdump.h>
243244
#include <malloc.h>
244245
#include <common.h>
245246
#include <console.h>

drivers/usb/gadget/storage_common.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
# define dump_msg(fsg, label, buf, length) do { \
125125
if (length < 512) { \
126126
DBG(fsg, "%s, length %u:\n", label, length); \
127-
print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, \
127+
print_hex_dump("", DUMP_PREFIX_OFFSET, \
128128
16, 1, buf, length, 0); \
129129
} \
130130
} while (0)
@@ -139,7 +139,7 @@
139139
# ifdef VERBOSE_DEBUG
140140

141141
# define dump_cdb(fsg) \
142-
print_hex_dump(KERN_DEBUG, "SCSI CDB: ", DUMP_PREFIX_NONE, \
142+
print_hex_dump("SCSI CDB: ", DUMP_PREFIX_NONE, \
143143
16, 1, (fsg)->cmnd, (fsg)->cmnd_size, 0) \
144144

145145
# else

examples/api/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ OBJ-y += libgenwrap.o
3232
EXT_COBJ-y += lib/crc32.o
3333
EXT_COBJ-y += lib/ctype.o
3434
EXT_COBJ-y += lib/div64.o
35+
EXT_COBJ-y += lib/hexdump.o
3536
EXT_COBJ-y += lib/string.o
3637
EXT_COBJ-y += lib/time.o
3738
EXT_COBJ-y += lib/vsprintf.o

fs/ubifs/debug.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* various local functions of those subsystems.
1616
*/
1717

18+
#include <hexdump.h>
19+
1820
#ifndef __UBOOT__
1921
#include <linux/module.h>
2022
#include <linux/debugfs.h>
@@ -307,7 +309,7 @@ void ubifs_dump_node(const struct ubifs_info *c, const void *node)
307309
/* If the magic is incorrect, just hexdump the first bytes */
308310
if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
309311
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,
311313
(void *)node, UBIFS_CH_SZ, 1);
312314
return;
313315
}
@@ -482,7 +484,7 @@ void ubifs_dump_node(const struct ubifs_info *c, const void *node)
482484
(int)le16_to_cpu(dn->compr_type));
483485
pr_err("\tdata size %d\n", dlen);
484486
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,
486488
(void *)&dn->data, dlen, 0);
487489
break;
488490
}

fs/ubifs/scan.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
#ifdef __UBOOT__
19+
#include <hexdump.h>
1920
#include <linux/err.h>
2021
#endif
2122
#include "ubifs.h"
@@ -236,7 +237,7 @@ void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
236237
if (len > 8192)
237238
len = 8192;
238239
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);
240241
}
241242

242243
/**

include/hexdump.h

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 commit comments

Comments
 (0)