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
14 #include <linux/ctype.h>
15 #include <linux/compat.h>
16 #include <linux/log2.h>
17 #include <asm/unaligned.h>
19 const char hex_asc[] = "0123456789abcdef";
20 const char hex_asc_upper[] = "0123456789ABCDEF";
22 #if CONFIG_IS_ENABLED(HEXDUMP)
23 int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
24 char *linebuf, size_t linebuflen, bool ascii)
33 if (rowsize != 16 && rowsize != 32)
36 if (len > rowsize) /* limit to one line at a time */
38 if (!is_power_of_2(groupsize) || groupsize > 8)
40 if ((len % groupsize) != 0) /* no mixed size output */
43 ngroups = len / groupsize;
44 ascii_column = rowsize * 2 + rowsize / groupsize + 1;
53 const u64 *ptr8 = buf;
55 for (j = 0; j < ngroups; j++) {
56 ret = snprintf(linebuf + lx, linebuflen - lx,
57 "%s%16.16llx", j ? " " : "",
58 get_unaligned(ptr8 + j));
59 if (ret >= linebuflen - lx)
63 } else if (groupsize == 4) {
64 const u32 *ptr4 = buf;
66 for (j = 0; j < ngroups; j++) {
67 ret = snprintf(linebuf + lx, linebuflen - lx,
68 "%s%8.8x", j ? " " : "",
69 get_unaligned(ptr4 + j));
70 if (ret >= linebuflen - lx)
74 } else if (groupsize == 2) {
75 const u16 *ptr2 = buf;
77 for (j = 0; j < ngroups; j++) {
78 ret = snprintf(linebuf + lx, linebuflen - lx,
79 "%s%4.4x", j ? " " : "",
80 get_unaligned(ptr2 + j));
81 if (ret >= linebuflen - lx)
86 for (j = 0; j < len; j++) {
87 if (linebuflen < lx + 2)
90 linebuf[lx++] = hex_asc_hi(ch);
91 if (linebuflen < lx + 2)
93 linebuf[lx++] = hex_asc_lo(ch);
94 if (linebuflen < lx + 2)
104 while (lx < ascii_column) {
105 if (linebuflen < lx + 2)
109 for (j = 0; j < len; j++) {
110 if (linebuflen < lx + 2)
113 linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
119 linebuf[lx++] = '\0';
121 return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
124 void print_hex_dump(const char *prefix_str, int prefix_type, int rowsize,
125 int groupsize, const void *buf, size_t len, bool ascii)
128 int i, linelen, remaining = len;
129 char linebuf[32 * 3 + 2 + 32 + 1];
131 if (rowsize != 16 && rowsize != 32)
134 for (i = 0; i < len; i += rowsize) {
135 linelen = min(remaining, rowsize);
136 remaining -= rowsize;
138 hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
139 linebuf, sizeof(linebuf), ascii);
141 switch (prefix_type) {
142 case DUMP_PREFIX_ADDRESS:
143 printf("%s%0*lx: %s\n", prefix_str,
144 IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8,
145 (ulong)map_to_sysmem(ptr) + i, linebuf);
147 case DUMP_PREFIX_OFFSET:
148 printf("%s%.8x: %s\n", prefix_str, i, linebuf);
151 printf("%s%s\n", prefix_str, linebuf);
157 void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
158 const void *buf, size_t len)
160 print_hex_dump(prefix_str, prefix_type, 16, 1, buf, len, true);
164 * Some code in U-Boot copy-pasted from Linux kernel uses both
165 * functions below so to keep stuff compilable we keep these stubs here.
167 void print_hex_dump(const char *prefix_str, int prefix_type, int rowsize,
168 int groupsize, const void *buf, size_t len, bool ascii)
172 void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
173 const void *buf, size_t len)
176 #endif /* CONFIG_HEXDUMP */