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> |
| 16 | #include <lib.h> |
| 17 | |
| 18 | static int isprint(int c) |
| 19 | { |
| 20 | return (c >= 32 && c <= 126); |
| 21 | } |
| 22 | |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 23 | void hexdump(const void *memory, size_t length) |
Stefan Reinauer | 2f38b07 | 2013-07-18 16:24:08 -0700 | [diff] [blame] | 24 | { |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 25 | int i; |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 26 | uint8_t *line; |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 27 | int all_zero = 0; |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 28 | int all_one = 0; |
| 29 | size_t num_bytes; |
Stefan Reinauer | 2f38b07 | 2013-07-18 16:24:08 -0700 | [diff] [blame] | 30 | |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 31 | for (i = 0; i < length; i += 16) { |
| 32 | int j; |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 33 | num_bytes = MIN(length - i, 16); |
| 34 | line = ((uint8_t *)memory) + i; |
Stefan Reinauer | 2f38b07 | 2013-07-18 16:24:08 -0700 | [diff] [blame] | 35 | |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 36 | 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 Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 42 | } |
| 43 | } |
Stefan Reinauer | 2f38b07 | 2013-07-18 16:24:08 -0700 | [diff] [blame] | 44 | |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 45 | 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 Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 53 | printk(BIOS_DEBUG, "%p:", memory + i); |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 54 | for (j = 0; j < num_bytes; j++) |
| 55 | printk(BIOS_DEBUG, " %02x", line[j]); |
| 56 | for (; j < 16; j++) |
Ben Gardner | b50d8fb | 2015-11-19 11:43:05 -0600 | [diff] [blame] | 57 | printk(BIOS_DEBUG, " "); |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 58 | printk(BIOS_DEBUG, " "); |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 59 | for (j = 0; j < num_bytes; j++) |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 60 | printk(BIOS_DEBUG, "%c", |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 61 | isprint(line[j]) ? line[j] : '.'); |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 62 | printk(BIOS_DEBUG, "\n"); |
Alexandru Gagniuc | dc6c431 | 2015-10-14 09:56:28 -0700 | [diff] [blame] | 63 | } else if ((all_zero == 2) || (all_one == 2)) { |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 64 | printk(BIOS_DEBUG, "...\n"); |
| 65 | } |
| 66 | } |
Stefan Reinauer | 2f38b07 | 2013-07-18 16:24:08 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Paul Menzel | 60ec2ff | 2014-05-03 16:21:34 +0200 | [diff] [blame] | 69 | void hexdump32(char LEVEL, const void *d, size_t len) |
Kyösti Mälkki | 972d5cf | 2014-01-26 14:44:18 +0200 | [diff] [blame] | 70 | { |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 71 | int count = 0; |
Stefan Reinauer | 2f38b07 | 2013-07-18 16:24:08 -0700 | [diff] [blame] | 72 | |
Kyösti Mälkki | 972d5cf | 2014-01-26 14:44:18 +0200 | [diff] [blame] | 73 | while (len > 0) { |
| 74 | if (count % 8 == 0) { |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 75 | printk(LEVEL, "\n"); |
Kyösti Mälkki | 972d5cf | 2014-01-26 14:44:18 +0200 | [diff] [blame] | 76 | printk(LEVEL, "%p:", d); |
| 77 | } |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 78 | printk(LEVEL, " 0x%08lx", *(unsigned long *)d); |
Kyösti Mälkki | 972d5cf | 2014-01-26 14:44:18 +0200 | [diff] [blame] | 79 | count++; |
| 80 | len--; |
| 81 | d += 4; |
| 82 | } |
| 83 | |
Paul Menzel | f0a5991 | 2013-12-27 14:43:44 +0100 | [diff] [blame] | 84 | printk(LEVEL, "\n\n"); |
Kyösti Mälkki | 972d5cf | 2014-01-26 14:44:18 +0200 | [diff] [blame] | 85 | } |