treewide: replace GPLv2 long form headers with SPDX header
[coreboot.git] / src / lib / hexdump.c
blob90446c1481bb6f0b1ef3ed1624dfdcf47fa9ce8c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 #include <console/console.h>
4 #include <ctype.h>
5 #include <lib.h>
7 void hexdump(const void *memory, size_t length)
9 size_t i, j;
10 uint8_t *line;
11 int all_zero = 0;
12 int all_one = 0;
13 size_t num_bytes;
15 for (i = 0; i < length; i += 16) {
16 num_bytes = MIN(length - i, 16);
17 line = ((uint8_t *)memory) + i;
19 all_zero++;
20 all_one++;
21 for (j = 0; j < num_bytes; j++) {
22 if (line[j] != 0) {
23 all_zero = 0;
24 break;
28 for (j = 0; j < num_bytes; j++) {
29 if (line[j] != 0xff) {
30 all_one = 0;
31 break;
35 if ((all_zero < 2) && (all_one < 2)) {
36 printk(BIOS_DEBUG, "%p:", memory + i);
37 for (j = 0; j < num_bytes; j++)
38 printk(BIOS_DEBUG, " %02x", line[j]);
39 for (; j < 16; j++)
40 printk(BIOS_DEBUG, " ");
41 printk(BIOS_DEBUG, " ");
42 for (j = 0; j < num_bytes; j++)
43 printk(BIOS_DEBUG, "%c",
44 isprint(line[j]) ? line[j] : '.');
45 printk(BIOS_DEBUG, "\n");
46 } else if ((all_zero == 2) || (all_one == 2)) {
47 printk(BIOS_DEBUG, "...\n");
52 void hexdump32(char LEVEL, const void *d, size_t len)
54 size_t count = 0;
56 while (len > 0) {
57 if (count % 8 == 0) {
58 printk(LEVEL, "\n");
59 printk(LEVEL, "%p:", d);
61 printk(LEVEL, " 0x%08lx", *(unsigned long *)d);
62 count++;
63 len--;
64 d += 4;
67 printk(LEVEL, "\n\n");