| 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 |
#include <libpayload.h> |
| 30 |
#include <stdint.h> |
| 31 |
|
| 32 |
struct cbmem_console { |
| 33 |
uint32_t size; |
| 34 |
uint32_t cursor; |
| 35 |
uint8_t body[]; |
| 36 |
} __packed; |
| 37 |
|
| 38 |
#define CURSOR_MASK ((1 << 28) - 1) |
| 39 |
#define OVERFLOW (1 << 31) |
| 40 |
|
| 41 |
static uintptr_t cbmem_console_p; |
| 42 |
|
| 43 |
static struct console_output_driver cbmem_console_driver = |
| 44 |
{ |
| 45 |
.write = &cbmem_console_write, |
| 46 |
}; |
| 47 |
|
| 48 |
static void do_write(const void *buffer, size_t count) |
| 49 |
{ |
| 50 |
struct cbmem_console *const cbmem_cons = phys_to_virt(cbmem_console_p); |
| 51 |
|
| 52 |
memcpy(cbmem_cons->body + (cbmem_cons->cursor & CURSOR_MASK), buffer, count); |
| 53 |
cbmem_cons->cursor += count; |
| 54 |
} |
| 55 |
|
| 56 |
void cbmem_console_init(void) |
| 57 |
{ |
| 58 |
const struct cbmem_console *const cbmem_cons = phys_to_virt(lib_sysinfo.cbmem_cons); |
| 59 |
|
| 60 |
cbmem_console_p = lib_sysinfo.cbmem_cons; |
| 61 |
|
| 62 |
if (cbmem_console_p && cbmem_cons->size) |
| 63 |
console_add_output_driver(&cbmem_console_driver); |
| 64 |
} |
| 65 |
|
| 66 |
void cbmem_console_write(const void *buffer, size_t count) |
| 67 |
{ |
| 68 |
struct cbmem_console *const cbmem_cons = phys_to_virt(cbmem_console_p); |
| 69 |
|
| 70 |
while ((cbmem_cons->cursor & CURSOR_MASK) + count >= |
| 71 |
cbmem_cons->size) { |
| 72 |
size_t still_fits = cbmem_cons->size - (cbmem_cons->cursor & CURSOR_MASK); |
| 73 |
do_write(buffer, still_fits); |
| 74 |
cbmem_cons->cursor &= ~CURSOR_MASK; |
| 75 |
cbmem_cons->cursor |= OVERFLOW; |
| 76 |
buffer += still_fits; |
| 77 |
count -= still_fits; |
| 78 |
} |
| 79 |
|
| 80 |
do_write(buffer, count); |
| 81 |
} |
| 82 |
|
| 83 |
static void snapshot_putc(char *console, uint32_t *cursor, char c) |
| 84 |
{ |
| 85 |
|
| 86 |
|
| 87 |
if (c >= 0x10 && c <= 0x18) |
| 88 |
return; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
if (!isprint(c) && !isspace(c)) |
| 93 |
console[*cursor] = '?'; |
| 94 |
else |
| 95 |
console[*cursor] = c; |
| 96 |
|
| 97 |
*cursor += 1; |
| 98 |
} |
| 99 |
|
| 100 |
char *cbmem_console_snapshot(void) |
| 101 |
{ |
| 102 |
const struct cbmem_console *const console_p = phys_to_virt(cbmem_console_p); |
| 103 |
char *console_c; |
| 104 |
uint32_t size, cursor, overflow, newc, oldc; |
| 105 |
|
| 106 |
if (!console_p) { |
| 107 |
printf("ERROR: No cbmem console found in coreboot table\n"); |
| 108 |
return NULL; |
| 109 |
} |
| 110 |
|
| 111 |
cursor = console_p->cursor & CURSOR_MASK; |
| 112 |
overflow = console_p->cursor & OVERFLOW; |
| 113 |
if (!overflow && cursor < console_p->size) |
| 114 |
size = cursor; |
| 115 |
else |
| 116 |
size = console_p->size; |
| 117 |
|
| 118 |
console_c = malloc(size + 1); |
| 119 |
if (!console_c) { |
| 120 |
printf("ERROR: Not enough memory for console (size = %u)\n", |
| 121 |
size); |
| 122 |
return NULL; |
| 123 |
} |
| 124 |
|
| 125 |
newc = 0; |
| 126 |
if (overflow) { |
| 127 |
if (cursor >= size) { |
| 128 |
printf("ERROR: CBMEM console struct is corrupted\n"); |
| 129 |
free(console_c); |
| 130 |
return NULL; |
| 131 |
} |
| 132 |
for (oldc = cursor; oldc < size; oldc++) |
| 133 |
snapshot_putc(console_c, &newc, console_p->body[oldc]); |
| 134 |
} |
| 135 |
for (oldc = 0; oldc < size && oldc < cursor; oldc++) |
| 136 |
snapshot_putc(console_c, &newc, console_p->body[oldc]); |
| 137 |
console_c[newc] = '\0'; |
| 138 |
|
| 139 |
return console_c; |
| 140 |
} |