blob: 533411f6f697b9c34caccf9c58b80448e3bc18e5 [file] [log] [blame]
Patrick Georgiac959032020-05-05 22:49:26 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Stefan Reinauer2f38b072013-07-18 16:24:08 -07002
3#include <console/console.h>
Joel Kitching393c71c2019-06-16 16:09:42 +08004#include <ctype.h>
Stefan Reinauer2f38b072013-07-18 16:24:08 -07005#include <lib.h>
Stefan Reinauer2f38b072013-07-18 16:24:08 -07006
Paul Menzelf0a59912013-12-27 14:43:44 +01007void hexdump(const void *memory, size_t length)
Stefan Reinauer2f38b072013-07-18 16:24:08 -07008{
Elyes HAOUAS65fe2942019-06-26 09:52:17 +02009 size_t i, j;
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070010 uint8_t *line;
Paul Menzelf0a59912013-12-27 14:43:44 +010011 int all_zero = 0;
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070012 int all_one = 0;
13 size_t num_bytes;
Stefan Reinauer2f38b072013-07-18 16:24:08 -070014
Paul Menzelf0a59912013-12-27 14:43:44 +010015 for (i = 0; i < length; i += 16) {
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070016 num_bytes = MIN(length - i, 16);
17 line = ((uint8_t *)memory) + i;
Stefan Reinauer2f38b072013-07-18 16:24:08 -070018
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070019 all_zero++;
20 all_one++;
21 for (j = 0; j < num_bytes; j++) {
22 if (line[j] != 0) {
23 all_zero = 0;
24 break;
Paul Menzelf0a59912013-12-27 14:43:44 +010025 }
26 }
Stefan Reinauer2f38b072013-07-18 16:24:08 -070027
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070028 for (j = 0; j < num_bytes; j++) {
29 if (line[j] != 0xff) {
30 all_one = 0;
31 break;
32 }
33 }
34
35 if ((all_zero < 2) && (all_one < 2)) {
Paul Menzelf0a59912013-12-27 14:43:44 +010036 printk(BIOS_DEBUG, "%p:", memory + i);
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070037 for (j = 0; j < num_bytes; j++)
38 printk(BIOS_DEBUG, " %02x", line[j]);
39 for (; j < 16; j++)
Ben Gardnerb50d8fb2015-11-19 11:43:05 -060040 printk(BIOS_DEBUG, " ");
Paul Menzelf0a59912013-12-27 14:43:44 +010041 printk(BIOS_DEBUG, " ");
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070042 for (j = 0; j < num_bytes; j++)
Paul Menzelf0a59912013-12-27 14:43:44 +010043 printk(BIOS_DEBUG, "%c",
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070044 isprint(line[j]) ? line[j] : '.');
Paul Menzelf0a59912013-12-27 14:43:44 +010045 printk(BIOS_DEBUG, "\n");
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070046 } else if ((all_zero == 2) || (all_one == 2)) {
Paul Menzelf0a59912013-12-27 14:43:44 +010047 printk(BIOS_DEBUG, "...\n");
48 }
49 }
Stefan Reinauer2f38b072013-07-18 16:24:08 -070050}