blob: 2861d6321e97b6167588e65d86185d2a59fc8f1b [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>
16#include <lib.h>
17
18static int isprint(int c)
19{
20 return (c >= 32 && c <= 126);
21}
22
Paul Menzelf0a59912013-12-27 14:43:44 +010023void hexdump(const void *memory, size_t length)
Stefan Reinauer2f38b072013-07-18 16:24:08 -070024{
Paul Menzelf0a59912013-12-27 14:43:44 +010025 int i;
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070026 uint8_t *line;
Paul Menzelf0a59912013-12-27 14:43:44 +010027 int all_zero = 0;
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070028 int all_one = 0;
29 size_t num_bytes;
Stefan Reinauer2f38b072013-07-18 16:24:08 -070030
Paul Menzelf0a59912013-12-27 14:43:44 +010031 for (i = 0; i < length; i += 16) {
32 int j;
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070033 num_bytes = MIN(length - i, 16);
34 line = ((uint8_t *)memory) + i;
Stefan Reinauer2f38b072013-07-18 16:24:08 -070035
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070036 all_zero++;
37 all_one++;
38 for (j = 0; j < num_bytes; j++) {
39 if (line[j] != 0) {
40 all_zero = 0;
41 break;
Paul Menzelf0a59912013-12-27 14:43:44 +010042 }
43 }
Stefan Reinauer2f38b072013-07-18 16:24:08 -070044
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070045 for (j = 0; j < num_bytes; j++) {
46 if (line[j] != 0xff) {
47 all_one = 0;
48 break;
49 }
50 }
51
52 if ((all_zero < 2) && (all_one < 2)) {
Paul Menzelf0a59912013-12-27 14:43:44 +010053 printk(BIOS_DEBUG, "%p:", memory + i);
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070054 for (j = 0; j < num_bytes; j++)
55 printk(BIOS_DEBUG, " %02x", line[j]);
56 for (; j < 16; j++)
Ben Gardnerb50d8fb2015-11-19 11:43:05 -060057 printk(BIOS_DEBUG, " ");
Paul Menzelf0a59912013-12-27 14:43:44 +010058 printk(BIOS_DEBUG, " ");
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070059 for (j = 0; j < num_bytes; j++)
Paul Menzelf0a59912013-12-27 14:43:44 +010060 printk(BIOS_DEBUG, "%c",
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070061 isprint(line[j]) ? line[j] : '.');
Paul Menzelf0a59912013-12-27 14:43:44 +010062 printk(BIOS_DEBUG, "\n");
Alexandru Gagniucdc6c4312015-10-14 09:56:28 -070063 } else if ((all_zero == 2) || (all_one == 2)) {
Paul Menzelf0a59912013-12-27 14:43:44 +010064 printk(BIOS_DEBUG, "...\n");
65 }
66 }
Stefan Reinauer2f38b072013-07-18 16:24:08 -070067}
68
Paul Menzel60ec2ff2014-05-03 16:21:34 +020069void hexdump32(char LEVEL, const void *d, size_t len)
Kyösti Mälkki972d5cf2014-01-26 14:44:18 +020070{
Paul Menzelf0a59912013-12-27 14:43:44 +010071 int count = 0;
Stefan Reinauer2f38b072013-07-18 16:24:08 -070072
Kyösti Mälkki972d5cf2014-01-26 14:44:18 +020073 while (len > 0) {
74 if (count % 8 == 0) {
Paul Menzelf0a59912013-12-27 14:43:44 +010075 printk(LEVEL, "\n");
Kyösti Mälkki972d5cf2014-01-26 14:44:18 +020076 printk(LEVEL, "%p:", d);
77 }
Paul Menzelf0a59912013-12-27 14:43:44 +010078 printk(LEVEL, " 0x%08lx", *(unsigned long *)d);
Kyösti Mälkki972d5cf2014-01-26 14:44:18 +020079 count++;
80 len--;
81 d += 4;
82 }
83
Paul Menzelf0a59912013-12-27 14:43:44 +010084 printk(LEVEL, "\n\n");
Kyösti Mälkki972d5cf2014-01-26 14:44:18 +020085}