Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Stefan Reinauer | 2f38b07 | 2013-07-18 16:24:08 -0700 | [diff] [blame] | 2 | |
| 3 | #include <console/console.h> |
Joel Kitching | 393c71c | 2019-06-16 16:09:42 +0800 | [diff] [blame] | 4 | #include <ctype.h> |
Stefan Reinauer | 2f38b07 | 2013-07-18 16:24:08 -0700 | [diff] [blame] | 5 | #include <lib.h> |
Stefan Reinauer | 2f38b07 | 2013-07-18 16:24:08 -0700 | [diff] [blame] | 6 | |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 7 | void hexdump(const void *memory, size_t length) |
Stefan Reinauer | 2f38b07 | 2013-07-18 16:24:08 -0700 | [diff] [blame] | 8 | { |
Elyes HAOUAS | 65fe294 | 2019-06-26 09:52:17 +0200 | [diff] [blame] | 9 | size_t i, j; |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 10 | uint8_t *line; |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 11 | int all_zero = 0; |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 12 | int all_one = 0; |
| 13 | size_t num_bytes; |
Stefan Reinauer | 2f38b07 | 2013-07-18 16:24:08 -0700 | [diff] [blame] | 14 | |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 15 | for (i = 0; i < length; i += 16) { |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 16 | num_bytes = MIN(length - i, 16); |
| 17 | line = ((uint8_t *)memory) + i; |
Stefan Reinauer | 2f38b07 | 2013-07-18 16:24:08 -0700 | [diff] [blame] | 18 | |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 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; |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 25 | } |
| 26 | } |
Stefan Reinauer | 2f38b07 | 2013-07-18 16:24:08 -0700 | [diff] [blame] | 27 | |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 28 | 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 Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 36 | printk(BIOS_DEBUG, "%p:", memory + i); |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 37 | for (j = 0; j < num_bytes; j++) |
| 38 | printk(BIOS_DEBUG, " %02x", line[j]); |
| 39 | for (; j < 16; j++) |
Ben Gardner | b50d8fb | 2015-11-19 11:43:05 -0600 | [diff] [blame] | 40 | printk(BIOS_DEBUG, " "); |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 41 | printk(BIOS_DEBUG, " "); |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 42 | for (j = 0; j < num_bytes; j++) |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 43 | printk(BIOS_DEBUG, "%c", |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 44 | isprint(line[j]) ? line[j] : '.'); |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 45 | printk(BIOS_DEBUG, "\n"); |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 46 | } else if ((all_zero == 2) || (all_one == 2)) { |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 47 | printk(BIOS_DEBUG, "...\n"); |
| 48 | } |
| 49 | } |
Stefan Reinauer | 2f38b07 | 2013-07-18 16:24:08 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Paul Menzel | 60ec2ff | 2014-05-03 16:21:34 +0200 | [diff] [blame] | 52 | void hexdump32(char LEVEL, const void *d, size_t len) |
Kyösti Mälkki | 972d5cf | 2014-01-26 14:44:18 +0200 | [diff] [blame] | 53 | { |
Elyes HAOUAS | 65fe294 | 2019-06-26 09:52:17 +0200 | [diff] [blame] | 54 | size_t count = 0; |
Stefan Reinauer | 2f38b07 | 2013-07-18 16:24:08 -0700 | [diff] [blame] | 55 | |
Kyösti Mälkki | 972d5cf | 2014-01-26 14:44:18 +0200 | [diff] [blame] | 56 | while (len > 0) { |
| 57 | if (count % 8 == 0) { |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 58 | printk(LEVEL, "\n"); |
Kyösti Mälkki | 972d5cf | 2014-01-26 14:44:18 +0200 | [diff] [blame] | 59 | printk(LEVEL, "%p:", d); |
| 60 | } |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 61 | printk(LEVEL, " 0x%08lx", *(unsigned long *)d); |
Kyösti Mälkki | 972d5cf | 2014-01-26 14:44:18 +0200 | [diff] [blame] | 62 | count++; |
| 63 | len--; |
| 64 | d += 4; |
| 65 | } |
| 66 | |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 67 | printk(LEVEL, "\n\n"); |
Kyösti Mälkki | 972d5cf | 2014-01-26 14:44:18 +0200 | [diff] [blame] | 68 | } |