coreboot/src/lib/hexdump.c
Ben Gardner b50d8fbb6e hexdump: Fix output if length is not a multiple of 16
hexdump currently rounds up length to a multiple of 16.
So, hexdump(ptr, 12) prints 16 hex digits, including 4 garbage bytes.
That isn't desirable and is easy to fix.

Change-Id: I86415fa9bc6cdc84b111e5e1968e39f570f294d9
Signed-off-by: Ben Gardner <gardner.ben@gmail.com>
Reviewed-on: http://review.coreboot.org/12486
Tested-by: build bot (Jenkins)
Reviewed-by: Martin Roth <martinroth@google.com>
Reviewed-by: Paul Menzel <paulepanter@users.sourceforge.net>
2015-11-21 03:41:53 +01:00

80 lines
1.7 KiB
C

/*
* Copyright 2013 Google Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but without any warranty; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <console/console.h>
#include <lib.h>
static int isprint(int c)
{
return (c >= 32 && c <= 126);
}
void hexdump(const void *memory, size_t length)
{
int i;
uint8_t *m;
int all_zero = 0;
m = (uint8_t *) memory;
for (i = 0; i < length; i += 16) {
int j;
int left = MIN(length - i, 16);
if (left < 16) {
all_zero = 0;
} else {
all_zero++;
for (j = 0; j < 16; j++) {
if (m[i + j] != 0) {
all_zero = 0;
break;
}
}
}
if (all_zero < 2) {
printk(BIOS_DEBUG, "%p:", memory + i);
for (j = 0; j < left; j++)
printk(BIOS_DEBUG, " %02x", m[i + j]);
for (j = left; j < 16; j++)
printk(BIOS_DEBUG, " ");
printk(BIOS_DEBUG, " ");
for (j = 0; j < left; j++)
printk(BIOS_DEBUG, "%c",
isprint(m[i + j]) ? m[i + j] : '.');
printk(BIOS_DEBUG, "\n");
} else if (all_zero == 2) {
printk(BIOS_DEBUG, "...\n");
}
}
}
void hexdump32(char LEVEL, const void *d, size_t len)
{
int count = 0;
while (len > 0) {
if (count % 8 == 0) {
printk(LEVEL, "\n");
printk(LEVEL, "%p:", d);
}
printk(LEVEL, " 0x%08lx", *(unsigned long *)d);
count++;
len--;
d += 4;
}
printk(LEVEL, "\n\n");
}