blob: 8ecba6d512324e49e33be33a21e6bf256e8da07b [file] [log] [blame]
Stefan Reinauer2f38b072013-07-18 16:24:08 -07001/*
2 * Copyright 2013 Google Inc.
3 *
Stefan Reinauer2f38b072013-07-18 16:24:08 -07004 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but without any warranty; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Stefan Reinauer2f38b072013-07-18 16:24:08 -070013 */
14
15#include <console/console.h>
Joel Kitching393c71c2019-06-16 16:09:42 +080016#include <ctype.h>
Stefan Reinauer2f38b072013-07-18 16:24:08 -070017#include <lib.h>
Stefan Reinauer2f38b072013-07-18 16:24:08 -070018
Paul Menzelf0a59912013-12-27 14:43:44 +010019void hexdump(const void *memory, size_t length)
Stefan Reinauer2f38b072013-07-18 16:24:08 -070020{
Elyes HAOUAS65fe2942019-06-26 09:52:17 +020021 size_t i, j;
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070022 uint8_t *line;
Paul Menzelf0a59912013-12-27 14:43:44 +010023 int all_zero = 0;
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070024 int all_one = 0;
25 size_t num_bytes;
Stefan Reinauer2f38b072013-07-18 16:24:08 -070026
Paul Menzelf0a59912013-12-27 14:43:44 +010027 for (i = 0; i < length; i += 16) {
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070028 num_bytes = MIN(length - i, 16);
29 line = ((uint8_t *)memory) + i;
Stefan Reinauer2f38b072013-07-18 16:24:08 -070030
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070031 all_zero++;
32 all_one++;
33 for (j = 0; j < num_bytes; j++) {
34 if (line[j] != 0) {
35 all_zero = 0;
36 break;
Paul Menzelf0a59912013-12-27 14:43:44 +010037 }
38 }
Stefan Reinauer2f38b072013-07-18 16:24:08 -070039
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070040 for (j = 0; j < num_bytes; j++) {
41 if (line[j] != 0xff) {
42 all_one = 0;
43 break;
44 }
45 }
46
47 if ((all_zero < 2) && (all_one < 2)) {
Paul Menzelf0a59912013-12-27 14:43:44 +010048 printk(BIOS_DEBUG, "%p:", memory + i);
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070049 for (j = 0; j < num_bytes; j++)
50 printk(BIOS_DEBUG, " %02x", line[j]);
51 for (; j < 16; j++)
Ben Gardnerb50d8fb2015-11-19 11:43:05 -060052 printk(BIOS_DEBUG, " ");
Paul Menzelf0a59912013-12-27 14:43:44 +010053 printk(BIOS_DEBUG, " ");
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070054 for (j = 0; j < num_bytes; j++)
Paul Menzelf0a59912013-12-27 14:43:44 +010055 printk(BIOS_DEBUG, "%c",
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070056 isprint(line[j]) ? line[j] : '.');
Paul Menzelf0a59912013-12-27 14:43:44 +010057 printk(BIOS_DEBUG, "\n");
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070058 } else if ((all_zero == 2) || (all_one == 2)) {
Paul Menzelf0a59912013-12-27 14:43:44 +010059 printk(BIOS_DEBUG, "...\n");
60 }
61 }
Stefan Reinauer2f38b072013-07-18 16:24:08 -070062}
63
Paul Menzel60ec2ff2014-05-03 16:21:34 +020064void hexdump32(char LEVEL, const void *d, size_t len)
Kyösti Mälkki972d5cf2014-01-26 14:44:18 +020065{
Elyes HAOUAS65fe2942019-06-26 09:52:17 +020066 size_t count = 0;
Stefan Reinauer2f38b072013-07-18 16:24:08 -070067
Kyösti Mälkki972d5cf2014-01-26 14:44:18 +020068 while (len > 0) {
69 if (count % 8 == 0) {
Paul Menzelf0a59912013-12-27 14:43:44 +010070 printk(LEVEL, "\n");
Kyösti Mälkki972d5cf2014-01-26 14:44:18 +020071 printk(LEVEL, "%p:", d);
72 }
Paul Menzelf0a59912013-12-27 14:43:44 +010073 printk(LEVEL, " 0x%08lx", *(unsigned long *)d);
Kyösti Mälkki972d5cf2014-01-26 14:44:18 +020074 count++;
75 len--;
76 d += 4;
77 }
78
Paul Menzelf0a59912013-12-27 14:43:44 +010079 printk(LEVEL, "\n\n");
Kyösti Mälkki972d5cf2014-01-26 14:44:18 +020080}