]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - lib/hexdump.c
1 // SPDX-License-Identifier: GPL-2.0+
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
13 #include <linux/ctype.h>
14 #include <linux/compat.h>
15 #include <linux/log2.h>
16 #include <asm/unaligned.h>
18 const char hex_asc
[] = "0123456789abcdef";
19 const char hex_asc_upper
[] = "0123456789ABCDEF";
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
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.
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.
40 * hex_dump_to_buffer(frame->data, frame->len, 16, 1,
41 * linebuf, sizeof(linebuf), true);
43 * example output buffer:
44 * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
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.
52 int hex_dump_to_buffer(const void *buf
, size_t len
, int rowsize
, int groupsize
,
53 char *linebuf
, size_t linebuflen
, bool ascii
)
62 if (rowsize
!= 16 && rowsize
!= 32)
65 if (len
> rowsize
) /* limit to one line at a time */
67 if (!is_power_of_2(groupsize
) || groupsize
> 8)
69 if ((len
% groupsize
) != 0) /* no mixed size output */
72 ngroups
= len
/ groupsize
;
73 ascii_column
= rowsize
* 2 + rowsize
/ groupsize
+ 1;
82 const u64
*ptr8
= buf
;
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
)
92 } else if (groupsize
== 4) {
93 const u32
*ptr4
= buf
;
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
)
103 } else if (groupsize
== 2) {
104 const u16
*ptr2
= buf
;
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
)
115 for (j
= 0; j
< len
; j
++) {
116 if (linebuflen
< lx
+ 2)
119 linebuf
[lx
++] = hex_asc_hi(ch
);
120 if (linebuflen
< lx
+ 2)
122 linebuf
[lx
++] = hex_asc_lo(ch
);
123 if (linebuflen
< lx
+ 2)
133 while (lx
< ascii_column
) {
134 if (linebuflen
< lx
+ 2)
138 for (j
= 0; j
< len
; j
++) {
139 if (linebuflen
< lx
+ 2)
142 linebuf
[lx
++] = (isascii(ch
) && isprint(ch
)) ? ch
: '.';
148 linebuf
[lx
++] = '\0';
150 return ascii
? ascii_column
+ len
: (groupsize
* 2 + 1) * ngroups
- 1;
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
165 * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
166 * to the stdio, with an optional leading prefix.
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.
174 * print_hex_dump("raw data: ", DUMP_PREFIX_ADDRESS, 16, 1, frame->data,
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{|}~.
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
)
186 int i
, linelen
, remaining
= len
;
187 char linebuf
[32 * 3 + 2 + 32 + 1];
189 if (rowsize
!= 16 && rowsize
!= 32)
192 for (i
= 0; i
< len
; i
+= rowsize
) {
193 linelen
= min(remaining
, rowsize
);
194 remaining
-= rowsize
;
196 hex_dump_to_buffer(ptr
+ i
, linelen
, rowsize
, groupsize
,
197 linebuf
, sizeof(linebuf
), ascii
);
199 switch (prefix_type
) {
200 case DUMP_PREFIX_ADDRESS
:
201 printf("%s%p: %s\n", prefix_str
, ptr
+ i
, linebuf
);
203 case DUMP_PREFIX_OFFSET
:
204 printf("%s%.8x: %s\n", prefix_str
, i
, linebuf
);
207 printf("%s%s\n", prefix_str
, linebuf
);
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
222 * Calls print_hex_dump(), rowsize of 16, groupsize of 1,
223 * and ASCII output included.
225 void print_hex_dump_bytes(const char *prefix_str
, int prefix_type
,
226 const void *buf
, size_t len
)
228 print_hex_dump(prefix_str
, prefix_type
, 16, 1, buf
, len
, true);
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.
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
)
241 void print_hex_dump_bytes(const char *prefix_str
, int prefix_type
,
242 const void *buf
, size_t len
)
245 #endif /* CONFIG_HEXDUMP */