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