1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
/*
* Copyright © 2017-2022 The Crust Firmware Authors.
* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only
*/
#include <ctype.h>
#include <debug.h>
#include <division.h>
#include <serial.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#define BYTES_PER_ROW 16
#define BYTES_PER_WORD sizeof(uint32_t)
static char *prefixes[LOG_LEVELS] = {
"SCP/ERR: ",
"SCP/WRN: ",
"SCP/INF: ",
"SCP/DBG: ",
};
static void print_number(uint32_t num, int base, int width, bool zero);
static void print_signed(int32_t num, int base, int width, bool zero);
void
hexdump(uintptr_t addr, uint32_t bytes)
{
uintptr_t start;
/* Always start at a multiple of BYTES_PER_ROW. */
addr &= ~(BYTES_PER_ROW - 1);
for (start = addr; addr - start < bytes; addr += BYTES_PER_ROW) {
uint32_t *words = (uint32_t *)addr;
/* This assumes BYTES_PER_ROW is 16, which it will always be.
* It's more of an informational constant, not a variable. */
log("%08x: %08x %08x %08x %08x ",
addr, words[0], words[1], words[2], words[3]);
/* The ARISC processor's data lines are swapped in hardware for
* compatibility with the little-endian ARM CPUs. To examine
* individual bytes, we must reverse each group of 4 bytes. */
for (int i = 0; i < BYTES_PER_ROW; ++i) {
char c = ((char *)addr)[i ^ 3];
serial_putc(isprint(c) ? c : '.');
}
serial_putc('\n');
}
}
void
log(const char *fmt, ...)
{
bool zero;
char c;
int width;
uintptr_t arg, level;
va_list args;
assert(fmt);
if (!serial_ready())
return;
level = *fmt - 1;
if (level < LOG_LEVELS) {
serial_puts(prefixes[level]);
++fmt;
}
va_start(args, fmt);
while ((c = *fmt++)) {
if (c != '%') {
serial_putc(c);
continue;
}
if (*fmt == '%') {
++fmt;
serial_putc(c);
continue;
}
arg = va_arg(args, uintptr_t);
width = 0;
zero = false;
conversion:
switch ((c = *fmt++)) {
case 'c':
serial_putc(arg);
break;
case 'd':
case 'i':
print_signed(arg, 10, width, zero);
break;
case 'p':
/* "%p" behaves like "0x%08x". */
serial_puts("0x");
print_number(arg, 16, 2 * sizeof(arg), true);
break;
case 'x':
print_number(arg, 16, width, zero);
break;
case 's':
assert(arg);
serial_puts((const char *)arg);
break;
case 'u':
print_number(arg, 10, width, zero);
break;
default:
assert(c);
if (c == '0' && width == 0)
zero = true;
else if (isdigit(c))
width = 10 * width + (c - '0');
goto conversion;
}
}
va_end(args);
if (level < LOG_LEVELS)
serial_putc('\n');
}
static void
print_number(uint32_t num, int base, int width, bool zero)
{
static const char chars[16] = "0123456789abcdef";
char digits[3 * sizeof(num)];
int i = 0;
do {
digits[i++] = chars[udivmod(&num, base)];
} while (num);
while (width-- > i)
serial_putc(zero ? '0' : ' ');
while (i--)
serial_putc(digits[i]);
}
static void
print_signed(int32_t num, int base, int width, bool zero)
{
if (num < 0) {
serial_putc('-');
print_number(-num, base, width ? width - 1 : width, zero);
} else {
print_number(num, base, width, zero);
}
}
|