Angel Pons | 118a9c7 | 2020-04-02 23:48:34 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 2 | |
Kyösti Mälkki | 1d7541f | 2014-02-17 21:34:42 +0200 | [diff] [blame] | 3 | #include <console/cbmem_console.h> |
Raul E Rangel | bf99311 | 2022-01-11 12:48:50 -0700 | [diff] [blame] | 4 | #include <console/console.h> |
Vadim Bendebury | 6e20e2f | 2015-04-10 18:04:04 -0700 | [diff] [blame] | 5 | #include <console/uart.h> |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 6 | #include <cbmem.h> |
Julius Werner | ec5e5e0 | 2014-08-20 15:29:56 -0700 | [diff] [blame] | 7 | #include <symbols.h> |
Raul E Rangel | bf99311 | 2022-01-11 12:48:50 -0700 | [diff] [blame] | 8 | #include <types.h> |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 9 | |
| 10 | /* |
| 11 | * Structure describing console buffer. It is overlaid on a flat memory area, |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 12 | * with body covering the extent of the memory. Once the buffer is full, |
| 13 | * output will wrap back around to the start of the buffer. The high bit of the |
| 14 | * cursor field gets set to indicate that this happened. If the underlying |
| 15 | * storage allows this, the buffer will persist across multiple boots and append |
| 16 | * to the previous log. |
Julius Werner | a915cea | 2017-04-24 17:08:06 -0700 | [diff] [blame] | 17 | * |
| 18 | * NOTE: These are known implementations accessing this console that need to be |
| 19 | * updated in case of structure/API changes: |
| 20 | * |
| 21 | * cbmem: [coreboot]/src/util/cbmem/cbmem.c |
| 22 | * libpayload: [coreboot]/payloads/libpayload/drivers/cbmem_console.c |
| 23 | * coreinfo: [coreboot]/payloads/coreinfo/bootlog_module.c |
| 24 | * Linux: drivers/firmware/google/memconsole-coreboot.c |
| 25 | * SeaBIOS: src/firmware/coreboot.c |
| 26 | * GRUB: grub-core/term/i386/coreboot/cbmemc.c |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 27 | */ |
| 28 | struct cbmem_console { |
Julius Werner | d09dc6b | 2017-02-03 12:45:21 -0800 | [diff] [blame] | 29 | u32 size; |
| 30 | u32 cursor; |
| 31 | u8 body[0]; |
Stefan Reinauer | 6a00113 | 2017-07-13 02:20:27 +0200 | [diff] [blame] | 32 | } __packed; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 33 | |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 34 | #define MAX_SIZE (1 << 28) /* can't be changed without breaking readers! */ |
| 35 | #define CURSOR_MASK (MAX_SIZE - 1) /* bits 31-28 are reserved for flags */ |
Ryan Salsamendi | fce582f | 2017-06-09 19:47:57 -0700 | [diff] [blame] | 36 | #define OVERFLOW (1UL << 31) /* set if in ring-buffer mode */ |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 37 | _Static_assert(CONFIG_CONSOLE_CBMEM_BUFFER_SIZE <= MAX_SIZE, |
| 38 | "cbmem_console format cannot support buffers larger than 256MB!"); |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 39 | |
Patrick Georgi | a8582c4 | 2019-11-30 10:49:17 +0100 | [diff] [blame] | 40 | static struct cbmem_console *current_console; |
Kyösti Mälkki | 8659e40 | 2014-12-21 08:55:47 +0200 | [diff] [blame] | 41 | |
Raul E Rangel | bf99311 | 2022-01-11 12:48:50 -0700 | [diff] [blame] | 42 | static bool console_paused; |
| 43 | |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 44 | /* |
| 45 | * While running from ROM, before DRAM is initialized, some area in cache as |
Elyes HAOUAS | 91e0e3c | 2016-07-30 15:51:13 +0200 | [diff] [blame] | 46 | * RAM space is used for the console buffer storage. The size and location of |
Julius Werner | ec5e5e0 | 2014-08-20 15:29:56 -0700 | [diff] [blame] | 47 | * the area are defined by the linker script with _(e)preram_cbmem_console. |
Kyösti Mälkki | e3acc8f | 2019-09-13 10:49:20 +0300 | [diff] [blame] | 48 | * |
Kyösti Mälkki | 513a1a8 | 2018-06-03 12:29:50 +0300 | [diff] [blame] | 49 | * When running from RAM, some console output is generated before CBMEM is |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 50 | * reinitialized. This static buffer is used to store that output temporarily, |
| 51 | * to be concatenated with the CBMEM console buffer contents accumulated |
| 52 | * during the ROM stage, once CBMEM becomes available at RAM stage. |
| 53 | */ |
Stefan Reinauer | 19d06b2 | 2013-09-09 11:22:18 -0700 | [diff] [blame] | 54 | |
Stefan Reinauer | 19d06b2 | 2013-09-09 11:22:18 -0700 | [diff] [blame] | 55 | #define STATIC_CONSOLE_SIZE 1024 |
Stefan Reinauer | 19d06b2 | 2013-09-09 11:22:18 -0700 | [diff] [blame] | 56 | static u8 static_console[STATIC_CONSOLE_SIZE]; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 57 | |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 58 | static int buffer_valid(struct cbmem_console *cbm_cons_p, u32 total_space) |
| 59 | { |
| 60 | return (cbm_cons_p->cursor & CURSOR_MASK) < cbm_cons_p->size && |
| 61 | cbm_cons_p->size <= MAX_SIZE && |
| 62 | cbm_cons_p->size == total_space - sizeof(struct cbmem_console); |
| 63 | } |
| 64 | |
| 65 | static void init_console_ptr(void *storage, u32 total_space) |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 66 | { |
| 67 | struct cbmem_console *cbm_cons_p = storage; |
| 68 | |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 69 | if (!cbm_cons_p || total_space <= sizeof(struct cbmem_console)) { |
Patrick Georgi | a8582c4 | 2019-11-30 10:49:17 +0100 | [diff] [blame] | 70 | current_console = NULL; |
Kyösti Mälkki | 8659e40 | 2014-12-21 08:55:47 +0200 | [diff] [blame] | 71 | return; |
| 72 | } |
| 73 | |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 74 | if (!buffer_valid(cbm_cons_p, total_space)) { |
Julius Werner | d09dc6b | 2017-02-03 12:45:21 -0800 | [diff] [blame] | 75 | cbm_cons_p->size = total_space - sizeof(struct cbmem_console); |
| 76 | cbm_cons_p->cursor = 0; |
Kyösti Mälkki | 8659e40 | 2014-12-21 08:55:47 +0200 | [diff] [blame] | 77 | } |
Kyösti Mälkki | 8659e40 | 2014-12-21 08:55:47 +0200 | [diff] [blame] | 78 | |
Patrick Georgi | a8582c4 | 2019-11-30 10:49:17 +0100 | [diff] [blame] | 79 | current_console = cbm_cons_p; |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void cbmemc_init(void) |
| 83 | { |
Kyösti Mälkki | e3acc8f | 2019-09-13 10:49:20 +0300 | [diff] [blame] | 84 | if (ENV_ROMSTAGE_OR_BEFORE) { |
| 85 | /* Pre-RAM environments use special buffer placed by linker script. */ |
| 86 | init_console_ptr(_preram_cbmem_console, REGION_SIZE(preram_cbmem_console)); |
| 87 | } else { |
| 88 | /* Post-RAM uses static (BSS) buffer before CBMEM is reinitialized. */ |
| 89 | init_console_ptr(static_console, sizeof(static_console)); |
| 90 | } |
Aaron Durbin | 2ad6bd2 | 2013-05-10 00:45:37 -0500 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void cbmemc_tx_byte(unsigned char data) |
| 94 | { |
Raul E Rangel | bf99311 | 2022-01-11 12:48:50 -0700 | [diff] [blame] | 95 | if (!current_console || !current_console->size || console_paused) |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 96 | return; |
| 97 | |
Patrick Georgi | a8582c4 | 2019-11-30 10:49:17 +0100 | [diff] [blame] | 98 | u32 flags = current_console->cursor & ~CURSOR_MASK; |
| 99 | u32 cursor = current_console->cursor & CURSOR_MASK; |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 100 | |
Patrick Georgi | a8582c4 | 2019-11-30 10:49:17 +0100 | [diff] [blame] | 101 | current_console->body[cursor++] = data; |
| 102 | if (cursor >= current_console->size) { |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 103 | cursor = 0; |
| 104 | flags |= OVERFLOW; |
| 105 | } |
| 106 | |
Patrick Georgi | a8582c4 | 2019-11-30 10:49:17 +0100 | [diff] [blame] | 107 | current_console->cursor = flags | cursor; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Raul Rangel | 03b1d3e | 2022-01-21 16:46:55 +0000 | [diff] [blame] | 110 | /* |
| 111 | * Copy the current console buffer (either from the cache as RAM area or from |
| 112 | * the static buffer, pointed at by src_cons_p) into the newly initialized CBMEM |
| 113 | * console. The use of cbmemc_tx_byte() ensures that all special cases for the |
| 114 | * target console (e.g. overflow) will be handled. If there had been an |
| 115 | * overflow in the source console, log a message to that effect. |
| 116 | */ |
| 117 | static void copy_console_buffer(struct cbmem_console *src_cons_p) |
| 118 | { |
| 119 | u32 c; |
| 120 | |
| 121 | if (!src_cons_p) |
| 122 | return; |
| 123 | |
| 124 | if (src_cons_p->cursor & OVERFLOW) { |
| 125 | const char overflow_warning[] = "\n*** Pre-CBMEM " ENV_STRING |
| 126 | " console overflowed, log truncated! ***\n"; |
| 127 | for (c = 0; c < sizeof(overflow_warning) - 1; c++) |
| 128 | cbmemc_tx_byte(overflow_warning[c]); |
| 129 | for (c = src_cons_p->cursor & CURSOR_MASK; |
| 130 | c < src_cons_p->size; c++) |
| 131 | cbmemc_tx_byte(src_cons_p->body[c]); |
| 132 | } |
| 133 | |
| 134 | for (c = 0; c < (src_cons_p->cursor & CURSOR_MASK); c++) |
| 135 | cbmemc_tx_byte(src_cons_p->body[c]); |
| 136 | |
| 137 | /* Invalidate the source console, so it will be reinitialized on the |
| 138 | next reboot. Otherwise, we might copy the same bytes again. */ |
| 139 | src_cons_p->size = 0; |
| 140 | } |
| 141 | |
Raul E Rangel | 1e1aa0c | 2022-01-13 09:46:21 -0700 | [diff] [blame] | 142 | void cbmemc_copy_in(void *buffer, size_t size) |
| 143 | { |
| 144 | struct cbmem_console *previous = (void *)buffer; |
| 145 | |
| 146 | if (!buffer_valid(previous, size)) |
| 147 | return; |
| 148 | |
| 149 | copy_console_buffer(previous); |
| 150 | } |
| 151 | |
Aaron Durbin | 41607a4 | 2015-06-09 13:54:10 -0500 | [diff] [blame] | 152 | static void cbmemc_reinit(int is_recovery) |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 153 | { |
Aaron Durbin | d70bf7c | 2015-04-20 15:24:19 -0500 | [diff] [blame] | 154 | const size_t size = CONFIG_CONSOLE_CBMEM_BUFFER_SIZE; |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 155 | /* If CBMEM entry already existed, old contents are not altered. */ |
| 156 | struct cbmem_console *cbmem_cons_p = cbmem_add(CBMEM_ID_CONSOLE, size); |
Patrick Georgi | a8582c4 | 2019-11-30 10:49:17 +0100 | [diff] [blame] | 157 | struct cbmem_console *previous_cons_p = current_console; |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 158 | |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 159 | init_console_ptr(cbmem_cons_p, size); |
| 160 | copy_console_buffer(previous_cons_p); |
Vadim Bendebury | 32da8be | 2011-09-29 17:27:15 -0700 | [diff] [blame] | 161 | } |
Julius Werner | 913a47a | 2021-05-20 17:00:46 -0700 | [diff] [blame] | 162 | |
| 163 | /* Run the romstage hook early so that the console region is one of the earliest created, and |
| 164 | therefore more likely to stay in the same place even across different boot modes where some |
| 165 | other regions may sometimes not get created (e.g. RW_MCACHE in vboot recovery mode). */ |
| 166 | ROMSTAGE_CBMEM_INIT_HOOK_EARLY(cbmemc_reinit) |
Kyösti Mälkki | 4fbac46 | 2015-01-07 04:48:43 +0200 | [diff] [blame] | 167 | RAMSTAGE_CBMEM_INIT_HOOK(cbmemc_reinit) |
Aaron Durbin | 1e9a914 | 2016-09-16 16:23:21 -0500 | [diff] [blame] | 168 | POSTCAR_CBMEM_INIT_HOOK(cbmemc_reinit) |
Aaron Durbin | d70bf7c | 2015-04-20 15:24:19 -0500 | [diff] [blame] | 169 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 170 | #if CONFIG(CONSOLE_CBMEM_DUMP_TO_UART) |
Raul E Rangel | 41a1a9e | 2022-01-11 12:44:38 -0700 | [diff] [blame] | 171 | void cbmem_dump_console_to_uart(void) |
Vadim Bendebury | 6e20e2f | 2015-04-10 18:04:04 -0700 | [diff] [blame] | 172 | { |
Julius Werner | d67c687 | 2017-02-02 17:32:00 -0800 | [diff] [blame] | 173 | u32 cursor; |
Raul E Rangel | 6ec3dd2 | 2022-01-21 13:34:21 -0700 | [diff] [blame] | 174 | unsigned int console_index; |
| 175 | |
Patrick Georgi | a8582c4 | 2019-11-30 10:49:17 +0100 | [diff] [blame] | 176 | if (!current_console) |
Vadim Bendebury | 6e20e2f | 2015-04-10 18:04:04 -0700 | [diff] [blame] | 177 | return; |
| 178 | |
Raul E Rangel | 6ec3dd2 | 2022-01-21 13:34:21 -0700 | [diff] [blame] | 179 | console_index = get_uart_for_console(); |
| 180 | |
| 181 | uart_init(console_index); |
| 182 | if (current_console->cursor & OVERFLOW) { |
Patrick Georgi | a8582c4 | 2019-11-30 10:49:17 +0100 | [diff] [blame] | 183 | for (cursor = current_console->cursor & CURSOR_MASK; |
Raul E Rangel | 6ec3dd2 | 2022-01-21 13:34:21 -0700 | [diff] [blame] | 184 | cursor < current_console->size; cursor++) { |
Julius Werner | 984d03c | 2022-01-21 15:33:47 -0800 | [diff] [blame] | 185 | if (BIOS_LOG_IS_MARKER(current_console->body[cursor])) |
| 186 | continue; |
Raul E Rangel | 6ec3dd2 | 2022-01-21 13:34:21 -0700 | [diff] [blame] | 187 | if (current_console->body[cursor] == '\n') |
| 188 | uart_tx_byte(console_index, '\r'); |
| 189 | uart_tx_byte(console_index, current_console->body[cursor]); |
| 190 | } |
| 191 | } |
| 192 | for (cursor = 0; cursor < (current_console->cursor & CURSOR_MASK); cursor++) { |
Julius Werner | 984d03c | 2022-01-21 15:33:47 -0800 | [diff] [blame] | 193 | if (BIOS_LOG_IS_MARKER(current_console->body[cursor])) |
| 194 | continue; |
Raul E Rangel | 6ec3dd2 | 2022-01-21 13:34:21 -0700 | [diff] [blame] | 195 | if (current_console->body[cursor] == '\n') |
| 196 | uart_tx_byte(console_index, '\r'); |
| 197 | uart_tx_byte(console_index, current_console->body[cursor]); |
| 198 | } |
Vadim Bendebury | 6e20e2f | 2015-04-10 18:04:04 -0700 | [diff] [blame] | 199 | } |
| 200 | #endif |
Raul E Rangel | bf99311 | 2022-01-11 12:48:50 -0700 | [diff] [blame] | 201 | |
| 202 | void cbmem_dump_console(void) |
| 203 | { |
| 204 | u32 cursor; |
| 205 | if (!current_console) |
| 206 | return; |
| 207 | |
| 208 | console_paused = true; |
| 209 | |
| 210 | if (current_console->cursor & OVERFLOW) |
| 211 | for (cursor = current_console->cursor & CURSOR_MASK; |
| 212 | cursor < current_console->size; cursor++) |
Julius Werner | 984d03c | 2022-01-21 15:33:47 -0800 | [diff] [blame] | 213 | if (!BIOS_LOG_IS_MARKER(current_console->body[cursor])) |
| 214 | do_putchar(current_console->body[cursor]); |
Raul E Rangel | bf99311 | 2022-01-11 12:48:50 -0700 | [diff] [blame] | 215 | for (cursor = 0; cursor < (current_console->cursor & CURSOR_MASK); cursor++) |
Julius Werner | 984d03c | 2022-01-21 15:33:47 -0800 | [diff] [blame] | 216 | if (!BIOS_LOG_IS_MARKER(current_console->body[cursor])) |
| 217 | do_putchar(current_console->body[cursor]); |
Raul E Rangel | bf99311 | 2022-01-11 12:48:50 -0700 | [diff] [blame] | 218 | |
| 219 | console_paused = false; |
| 220 | } |