console: Add loglevel marker codes to stored consoles

In order to provide the same loglevel prefixes and highlighting that
were recently introduced for "interactive" consoles (e.g. UART) to
"stored" consoles (e.g. CBMEM) but minimize the amont of extra storage
space wasted on this info, this patch will write a 1-byte control
character marker indicating the loglevel to the start of every line
logged in those consoles. The `cbmem` utility will then interpret those
markers and translate them back into loglevel prefixes and escape
sequences as needed.

Since coreboot and userspace log readers aren't always in sync,
occasionally an older reader may come across these markers and not know
how to interpret them... but that should usually be fine, as the range
chosen contains non-printable ASCII characters that normally have no
effect on the terminal. At worst the outdated reader would display one
garbled character at the start of every line which isn't that bad.
(Older versions of the `cbmem` utility will translate non-printable
characters into `?` question marks.)

Signed-off-by: Julius Werner <jwerner@chromium.org>
Change-Id: I86073f48aaf1e0a58e97676fb80e2475ec418ffc
Reviewed-on: https://review.coreboot.org/c/coreboot/+/61308
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Raul Rangel <rrangel@chromium.org>
This commit is contained in:
Julius Werner
2022-01-21 15:33:47 -08:00
parent a120e0defd
commit 984d03c492
4 changed files with 51 additions and 5 deletions

View File

@@ -18,6 +18,7 @@
#include <assert.h>
#include <regex.h>
#include <commonlib/bsd/cbmem_id.h>
#include <commonlib/loglevel.h>
#include <commonlib/timestamp_serialized.h>
#include <commonlib/tcpa_log_serialized.h>
#include <commonlib/coreboot_tables.h>
@@ -783,7 +784,8 @@ static void dump_console(enum console_print_type type)
/* Slight memory corruption may occur between reboots and give us a few
unprintable characters like '\0'. Replace them with '?' on output. */
for (cursor = 0; cursor < size; cursor++)
if (!isprint(console_c[cursor]) && !isspace(console_c[cursor]))
if (!isprint(console_c[cursor]) && !isspace(console_c[cursor])
&& !BIOS_LOG_IS_MARKER(console_c[cursor]))
console_c[cursor] = '?';
/* We detect the reboot cutoff by looking for a bootblock, romstage or
@@ -822,7 +824,23 @@ static void dump_console(enum console_print_type type)
cursor = previous;
}
puts(console_c + cursor);
char c;
int tty = isatty(fileno(stdout));
while ((c = console_c[cursor++])) {
if (BIOS_LOG_IS_MARKER(c)) {
int lvl = BIOS_LOG_MARKER_TO_LEVEL(c);
if (tty)
printf(BIOS_LOG_ESCAPE_PATTERN, bios_log_escape[lvl]);
printf(BIOS_LOG_PREFIX_PATTERN, bios_log_prefix[lvl]);
} else {
putchar(c);
if (tty && c == '\n')
printf(BIOS_LOG_ESCAPE_RESET);
}
}
if (tty)
printf(BIOS_LOG_ESCAPE_RESET);
free(console_c);
unmap_memory(&console_mapping);
}