^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * lib/hexdump.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/minmax.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) const char hex_asc[] = "0123456789abcdef";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) EXPORT_SYMBOL(hex_asc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) const char hex_asc_upper[] = "0123456789ABCDEF";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) EXPORT_SYMBOL(hex_asc_upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * hex_to_bin - convert a hex digit to its real value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * @ch: ascii character represents hex digit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * input.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int hex_to_bin(char ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if ((ch >= '0') && (ch <= '9'))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return ch - '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) ch = tolower(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if ((ch >= 'a') && (ch <= 'f'))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return ch - 'a' + 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) EXPORT_SYMBOL(hex_to_bin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * hex2bin - convert an ascii hexadecimal string to its binary representation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * @dst: binary result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * @src: ascii hexadecimal string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * @count: result length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * Return 0 on success, -EINVAL in case of bad input.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int hex2bin(u8 *dst, const char *src, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) while (count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int hi = hex_to_bin(*src++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int lo = hex_to_bin(*src++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if ((hi < 0) || (lo < 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *dst++ = (hi << 4) | lo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) EXPORT_SYMBOL(hex2bin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * bin2hex - convert binary data to an ascii hexadecimal string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * @dst: ascii hexadecimal result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * @src: binary data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * @count: binary data length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) char *bin2hex(char *dst, const void *src, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) const unsigned char *_src = src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) while (count--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) dst = hex_byte_pack(dst, *_src++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) EXPORT_SYMBOL(bin2hex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * @buf: data blob to dump
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * @len: number of bytes in the @buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * @rowsize: number of bytes to print per line; must be 16 or 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * @linebuf: where to put the converted data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * @linebuflen: total size of @linebuf, including space for terminating NUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * @ascii: include ASCII after the hex output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * 16 or 32 bytes of input data converted to hex + ASCII output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * to a hex + ASCII dump at the supplied memory location.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * The converted output is always NUL-terminated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * E.g.:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * hex_dump_to_buffer(frame->data, frame->len, 16, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * linebuf, sizeof(linebuf), true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * example output buffer:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * The amount of bytes placed in the buffer without terminating NUL. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * output was truncated, then the return value is the number of bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * (excluding the terminating NUL) which would have been written to the final
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * string if enough space had been available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) char *linebuf, size_t linebuflen, bool ascii)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) const u8 *ptr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int ngroups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) u8 ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int j, lx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int ascii_column;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (rowsize != 16 && rowsize != 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) rowsize = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (len > rowsize) /* limit to one line at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) len = rowsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (!is_power_of_2(groupsize) || groupsize > 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) groupsize = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if ((len % groupsize) != 0) /* no mixed size output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) groupsize = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ngroups = len / groupsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) ascii_column = rowsize * 2 + rowsize / groupsize + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!linebuflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) goto overflow1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) goto nil;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (groupsize == 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) const u64 *ptr8 = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) for (j = 0; j < ngroups; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) ret = snprintf(linebuf + lx, linebuflen - lx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) "%s%16.16llx", j ? " " : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) get_unaligned(ptr8 + j));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (ret >= linebuflen - lx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) goto overflow1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) lx += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) } else if (groupsize == 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) const u32 *ptr4 = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) for (j = 0; j < ngroups; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ret = snprintf(linebuf + lx, linebuflen - lx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) "%s%8.8x", j ? " " : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) get_unaligned(ptr4 + j));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (ret >= linebuflen - lx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) goto overflow1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) lx += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) } else if (groupsize == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) const u16 *ptr2 = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) for (j = 0; j < ngroups; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ret = snprintf(linebuf + lx, linebuflen - lx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) "%s%4.4x", j ? " " : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) get_unaligned(ptr2 + j));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (ret >= linebuflen - lx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) goto overflow1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) lx += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) for (j = 0; j < len; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (linebuflen < lx + 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) goto overflow2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ch = ptr[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) linebuf[lx++] = hex_asc_hi(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (linebuflen < lx + 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) goto overflow2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) linebuf[lx++] = hex_asc_lo(ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (linebuflen < lx + 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) goto overflow2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) linebuf[lx++] = ' ';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (j)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) lx--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (!ascii)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) goto nil;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) while (lx < ascii_column) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (linebuflen < lx + 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) goto overflow2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) linebuf[lx++] = ' ';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) for (j = 0; j < len; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (linebuflen < lx + 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) goto overflow2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) ch = ptr[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) nil:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) linebuf[lx] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return lx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) overflow2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) linebuf[lx++] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) overflow1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) EXPORT_SYMBOL(hex_dump_to_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) #ifdef CONFIG_PRINTK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * print_hex_dump - print a text hex dump to syslog for a binary blob of data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * @level: kernel log level (e.g. KERN_DEBUG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * @prefix_str: string to prefix each line with;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * caller supplies trailing spaces for alignment if desired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * @prefix_type: controls whether prefix of an offset, address, or none
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * @rowsize: number of bytes to print per line; must be 16 or 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * @buf: data blob to dump
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * @len: number of bytes in the @buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * @ascii: include ASCII after the hex output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * to the kernel log at the specified kernel log level, with an optional
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * leading prefix.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * print_hex_dump() works on one "line" of output at a time, i.e.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * 16 or 32 bytes of input data converted to hex + ASCII output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * print_hex_dump() iterates over the entire input @buf, breaking it into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * "line size" chunks to format and print.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * E.g.:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * 16, 1, frame->data, frame->len, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c pqrstuvwxyz{|}~.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) int rowsize, int groupsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) const void *buf, size_t len, bool ascii)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) const u8 *ptr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) int i, linelen, remaining = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) unsigned char linebuf[32 * 3 + 2 + 32 + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (rowsize != 16 && rowsize != 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) rowsize = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) for (i = 0; i < len; i += rowsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) linelen = min(remaining, rowsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) remaining -= rowsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) linebuf, sizeof(linebuf), ascii);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) switch (prefix_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) case DUMP_PREFIX_ADDRESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) printk("%s%s%p: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) level, prefix_str, ptr + i, linebuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) case DUMP_PREFIX_OFFSET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) printk("%s%s%s\n", level, prefix_str, linebuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) EXPORT_SYMBOL(print_hex_dump);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) #endif /* defined(CONFIG_PRINTK) */