Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2012 The ChromiumOS Authors. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
David Hendricks | 259e49a | 2014-03-20 20:31:23 -0700 | [diff] [blame] | 16 | #if CONFIG_HAVE_ACPI_RESUME == 1 |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 17 | #include <arch/acpi.h> |
David Hendricks | 259e49a | 2014-03-20 20:31:23 -0700 | [diff] [blame] | 18 | #endif |
Duncan Laurie | 215f2785 | 2012-10-10 14:34:49 -0700 | [diff] [blame] | 19 | #include <cbmem.h> |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 20 | #include <console/console.h> |
David Hendricks | 259e49a | 2014-03-20 20:31:23 -0700 | [diff] [blame] | 21 | #if CONFIG_ARCH_X86 |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 22 | #include <pc80/mc146818rtc.h> |
David Hendricks | 259e49a | 2014-03-20 20:31:23 -0700 | [diff] [blame] | 23 | #endif |
Gabe Black | a4c4b1c | 2014-04-30 21:41:23 -0700 | [diff] [blame] | 24 | #include <bcd.h> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 25 | #include <fmap.h> |
Gabe Black | a4c4b1c | 2014-04-30 21:41:23 -0700 | [diff] [blame] | 26 | #include <rtc.h> |
Duncan Laurie | 472ec9c | 2012-06-23 16:13:42 -0700 | [diff] [blame] | 27 | #include <smbios.h> |
Zheng Bao | 600784e | 2013-02-07 17:30:23 +0800 | [diff] [blame] | 28 | #include <spi-generic.h> |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 29 | #include <spi_flash.h> |
| 30 | #include <stdint.h> |
| 31 | #include <string.h> |
| 32 | #include <elog.h> |
| 33 | #include "elog_internal.h" |
| 34 | |
Edward O'Callaghan | a3119e5 | 2014-06-18 14:28:03 +1000 | [diff] [blame] | 35 | |
Stefan Reinauer | 45a225b | 2015-03-16 16:47:39 -0700 | [diff] [blame] | 36 | #if !IS_ENABLED(CONFIG_CHROMEOS) && CONFIG_ELOG_FLASH_BASE == 0 |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 37 | #error "CONFIG_ELOG_FLASH_BASE is invalid" |
| 38 | #endif |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 39 | |
| 40 | #if CONFIG_ELOG_DEBUG |
| 41 | #define elog_debug(STR...) printk(BIOS_DEBUG, STR) |
| 42 | #else |
| 43 | #define elog_debug(STR...) |
| 44 | #endif |
| 45 | |
| 46 | /* |
| 47 | * Static variables for ELOG state |
| 48 | */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 49 | static struct elog_area *elog_area; |
| 50 | static u16 total_size; |
Sol Boucher | b419149 | 2015-03-12 18:22:31 -0700 | [diff] [blame] | 51 | static u16 log_size; /* excluding header */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 52 | static u32 flash_base; |
Sol Boucher | b419149 | 2015-03-12 18:22:31 -0700 | [diff] [blame] | 53 | static u16 full_threshold; /* from end of header */ |
| 54 | static u16 shrink_size; /* from end of header */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 55 | |
| 56 | static elog_area_state area_state; |
| 57 | static elog_header_state header_state; |
| 58 | static elog_event_buffer_state event_buffer_state; |
| 59 | |
Sol Boucher | b419149 | 2015-03-12 18:22:31 -0700 | [diff] [blame] | 60 | static u16 next_event_offset; /* from end of header */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 61 | static u16 event_count; |
| 62 | |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 63 | static struct spi_flash *elog_spi; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 64 | |
Julius Werner | 5d68622 | 2015-01-27 15:40:47 -0800 | [diff] [blame] | 65 | static enum { |
| 66 | ELOG_UNINITIALIZED = 0, |
| 67 | ELOG_INITIALIZED, |
| 68 | ELOG_BROKEN, |
| 69 | } elog_initialized = ELOG_UNINITIALIZED; |
Aaron Durbin | 10a070b | 2013-06-28 12:30:53 -0700 | [diff] [blame] | 70 | |
| 71 | static inline u32 get_rom_size(void) |
| 72 | { |
| 73 | u32 rom_size; |
| 74 | |
| 75 | /* Assume the used space of the ROM image starts from 0. The |
| 76 | * physical size of the device may not be completely used. */ |
| 77 | rom_size = elog_spi->size; |
| 78 | if (rom_size > CONFIG_ROM_SIZE) |
| 79 | rom_size = CONFIG_ROM_SIZE; |
| 80 | |
| 81 | return rom_size; |
| 82 | } |
| 83 | |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 84 | /* |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 85 | * Pointer to an event log header in the event data area |
| 86 | */ |
| 87 | static inline struct event_header* |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 88 | elog_get_event_base(u32 offset) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 89 | { |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 90 | return (struct event_header *)&elog_area->data[offset]; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 94 | * Update the checksum at the last byte |
| 95 | */ |
| 96 | static void elog_update_checksum(struct event_header *event, u8 checksum) |
| 97 | { |
| 98 | u8 *event_data = (u8*)event; |
| 99 | event_data[event->length - 1] = checksum; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Simple byte checksum for events |
| 104 | */ |
| 105 | static u8 elog_checksum_event(struct event_header *event) |
| 106 | { |
| 107 | u8 index, checksum = 0; |
| 108 | u8 *data = (u8*)event; |
| 109 | |
| 110 | for (index = 0; index < event->length; index++) |
| 111 | checksum += data[index]; |
| 112 | return checksum; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Check if a raw buffer is filled with ELOG_TYPE_EOL byte |
| 117 | */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 118 | static int elog_is_buffer_clear(void *base, u32 size) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 119 | { |
| 120 | u8 *current = base; |
| 121 | u8 *end = current + size; |
| 122 | |
| 123 | elog_debug("elog_is_buffer_clear(base=0x%p size=%u)\n", base, size); |
| 124 | |
| 125 | for (; current != end; current++) { |
| 126 | if (*current != ELOG_TYPE_EOL) |
| 127 | return 0; |
| 128 | } |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | /* |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 133 | * Check that the ELOG area has been initialized and is valid. |
| 134 | */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 135 | static int elog_is_area_valid(void) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 136 | { |
| 137 | elog_debug("elog_is_area_valid()\n"); |
| 138 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 139 | if (area_state != ELOG_AREA_HAS_CONTENT) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 140 | return 0; |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 141 | if (header_state != ELOG_HEADER_VALID) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 142 | return 0; |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 143 | if (event_buffer_state != ELOG_EVENT_BUFFER_OK) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 144 | return 0; |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Verify the contents of an ELOG Header structure |
| 150 | * Returns 1 if the header is valid, 0 otherwise |
| 151 | */ |
| 152 | static int elog_is_header_valid(struct elog_header *header) |
| 153 | { |
| 154 | elog_debug("elog_is_header_valid()\n"); |
| 155 | |
| 156 | if (header->magic != ELOG_SIGNATURE) { |
| 157 | printk(BIOS_ERR, "ELOG: header magic 0x%X != 0x%X\n", |
| 158 | header->magic, ELOG_SIGNATURE); |
| 159 | return 0; |
| 160 | } |
| 161 | if (header->version != ELOG_VERSION) { |
| 162 | printk(BIOS_ERR, "ELOG: header version %u != %u\n", |
| 163 | header->version, ELOG_VERSION); |
| 164 | return 0; |
| 165 | } |
| 166 | if (header->header_size != sizeof(*header)) { |
Denis 'GNUtoo' Carikli | bc2c9ef | 2013-06-26 20:04:49 +0200 | [diff] [blame] | 167 | printk(BIOS_ERR, "ELOG: header size mismatch %u != %zu\n", |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 168 | header->header_size, sizeof(*header)); |
| 169 | return 0; |
| 170 | } |
| 171 | return 1; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Validate the event header and data. |
| 176 | */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 177 | static int elog_is_event_valid(u32 offset) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 178 | { |
| 179 | struct event_header *event; |
| 180 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 181 | event = elog_get_event_base(offset); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 182 | if (!event) |
| 183 | return 0; |
| 184 | |
| 185 | /* Validate event length */ |
| 186 | if ((offsetof(struct event_header, type) + |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 187 | sizeof(event->type) - 1 + offset) >= log_size) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 188 | return 0; |
| 189 | |
| 190 | /* End of event marker has been found */ |
| 191 | if (event->type == ELOG_TYPE_EOL) |
| 192 | return 0; |
| 193 | |
| 194 | /* Check if event fits in area */ |
| 195 | if ((offsetof(struct event_header, length) + |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 196 | sizeof(event->length) - 1 + offset) >= log_size) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 197 | return 0; |
| 198 | |
| 199 | /* |
| 200 | * If the current event length + the current offset exceeds |
| 201 | * the area size then the event area is corrupt. |
| 202 | */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 203 | if ((event->length + offset) >= log_size) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 204 | return 0; |
| 205 | |
| 206 | /* Event length must be at least header size + checksum */ |
| 207 | if (event->length < (sizeof(*event) + 1)) |
| 208 | return 0; |
| 209 | |
| 210 | /* If event checksum is invalid the area is corrupt */ |
| 211 | if (elog_checksum_event(event) != 0) |
| 212 | return 0; |
| 213 | |
| 214 | /* Event is valid */ |
| 215 | return 1; |
| 216 | } |
| 217 | |
| 218 | /* |
Gabe Black | 331eb08 | 2013-04-24 04:11:40 -0700 | [diff] [blame] | 219 | * Write 'size' bytes of data pointed to by 'address' in the flash backing |
| 220 | * store into flash. This will not erase the flash and it assumes the flash |
| 221 | * area has been erased appropriately. |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 222 | */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 223 | static void elog_flash_write(void *address, u32 size) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 224 | { |
| 225 | u32 offset; |
| 226 | |
Gabe Black | 331eb08 | 2013-04-24 04:11:40 -0700 | [diff] [blame] | 227 | if (!address || !size || !elog_spi) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 228 | return; |
| 229 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 230 | offset = flash_base; |
| 231 | offset += (u8 *)address - (u8 *)elog_area; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 232 | |
Gabe Black | 331eb08 | 2013-04-24 04:11:40 -0700 | [diff] [blame] | 233 | elog_debug("elog_flash_write(address=0x%p offset=0x%08x size=%u)\n", |
| 234 | address, offset, size); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 235 | |
| 236 | /* Write the data to flash */ |
Gabe Black | 331eb08 | 2013-04-24 04:11:40 -0700 | [diff] [blame] | 237 | elog_spi->write(elog_spi, offset, size, address); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Erase the first block specified in the address. |
| 242 | * Only handles flash area within a single flash block. |
| 243 | */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 244 | static void elog_flash_erase(void *address, u32 size) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 245 | { |
| 246 | u32 offset; |
| 247 | |
| 248 | if (!address || !size || !elog_spi) |
| 249 | return; |
| 250 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 251 | offset = flash_base; |
| 252 | offset += (u8 *)address - (u8*)elog_area; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 253 | |
| 254 | elog_debug("elog_flash_erase(address=0x%p offset=0x%08x size=%u)\n", |
| 255 | address, offset, size); |
| 256 | |
| 257 | /* Erase the sectors in this region */ |
| 258 | elog_spi->erase(elog_spi, offset, size); |
| 259 | } |
| 260 | |
| 261 | /* |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 262 | * Scan the event area and validate each entry and update the ELOG state. |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 263 | */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 264 | static void elog_update_event_buffer_state(void) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 265 | { |
| 266 | u32 count = 0; |
| 267 | u32 offset = 0; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 268 | struct event_header *event; |
| 269 | |
| 270 | elog_debug("elog_update_event_buffer_state()\n"); |
| 271 | |
| 272 | /* Go through each event and validate it */ |
| 273 | while (1) { |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 274 | event = elog_get_event_base(offset); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 275 | |
| 276 | /* Do not de-reference anything past the area length */ |
| 277 | if ((offsetof(struct event_header, type) + |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 278 | sizeof(event->type) - 1 + offset) >= log_size) { |
| 279 | event_buffer_state = ELOG_EVENT_BUFFER_CORRUPTED; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 280 | break; |
| 281 | } |
| 282 | |
| 283 | /* The end of the event marker has been found */ |
| 284 | if (event->type == ELOG_TYPE_EOL) |
| 285 | break; |
| 286 | |
| 287 | /* Validate the event */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 288 | if (!elog_is_event_valid(offset)) { |
| 289 | event_buffer_state = ELOG_EVENT_BUFFER_CORRUPTED; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 290 | break; |
| 291 | } |
| 292 | |
| 293 | /* Move to the next event */ |
| 294 | count++; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 295 | offset += event->length; |
| 296 | } |
| 297 | |
| 298 | /* Ensure the remaining buffer is empty */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 299 | if (!elog_is_buffer_clear(&elog_area->data[offset], log_size - offset)) |
| 300 | event_buffer_state = ELOG_EVENT_BUFFER_CORRUPTED; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 301 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 302 | /* Update ELOG state */ |
| 303 | event_count = count; |
| 304 | next_event_offset = offset; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 307 | static void elog_scan_flash(void) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 308 | { |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 309 | elog_debug("elog_scan_flash()\n"); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 310 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 311 | area_state = ELOG_AREA_UNDEFINED; |
| 312 | header_state = ELOG_HEADER_INVALID; |
| 313 | event_buffer_state = ELOG_EVENT_BUFFER_OK; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 314 | |
Duncan Laurie | 215f2785 | 2012-10-10 14:34:49 -0700 | [diff] [blame] | 315 | /* Fill memory buffer by reading from SPI */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 316 | elog_spi->read(elog_spi, flash_base, total_size, elog_area); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 317 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 318 | next_event_offset = 0; |
| 319 | event_count = 0; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 320 | |
Gabe Black | 455c68e | 2013-04-24 17:54:37 -0700 | [diff] [blame] | 321 | /* Check if the area is empty or not */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 322 | if (elog_is_buffer_clear(elog_area, total_size)) { |
| 323 | area_state = ELOG_AREA_EMPTY; |
Gabe Black | 455c68e | 2013-04-24 17:54:37 -0700 | [diff] [blame] | 324 | return; |
| 325 | } |
| 326 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 327 | area_state = ELOG_AREA_HAS_CONTENT; |
Gabe Black | 455c68e | 2013-04-24 17:54:37 -0700 | [diff] [blame] | 328 | |
| 329 | /* Validate the header */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 330 | if (!elog_is_header_valid(&elog_area->header)) { |
| 331 | header_state = ELOG_HEADER_INVALID; |
Gabe Black | 455c68e | 2013-04-24 17:54:37 -0700 | [diff] [blame] | 332 | return; |
| 333 | } |
| 334 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 335 | header_state = ELOG_HEADER_VALID; |
| 336 | elog_update_event_buffer_state(); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 339 | static void elog_prepare_empty(void) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 340 | { |
| 341 | struct elog_header *header; |
| 342 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 343 | elog_debug("elog_prepare_empty()\n"); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 344 | |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 345 | /* Write out the header */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 346 | header = &elog_area->header; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 347 | header->magic = ELOG_SIGNATURE; |
| 348 | header->version = ELOG_VERSION; |
| 349 | header->header_size = sizeof(struct elog_header); |
| 350 | header->reserved[0] = ELOG_TYPE_EOL; |
| 351 | header->reserved[1] = ELOG_TYPE_EOL; |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 352 | elog_flash_write(elog_area, header->header_size); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 353 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 354 | elog_scan_flash(); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /* |
| 358 | * Shrink the log, deleting old entries and moving the |
Martin Roth | 5688979 | 2013-07-09 21:39:46 -0600 | [diff] [blame] | 359 | * remaining ones to the front of the log. |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 360 | */ |
| 361 | static int elog_shrink(void) |
| 362 | { |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 363 | struct event_header *event; |
| 364 | u16 discard_count = 0; |
| 365 | u16 offset = 0; |
Gabe Black | bfae4aa | 2013-04-24 03:16:21 -0700 | [diff] [blame] | 366 | u16 new_size = 0; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 367 | |
| 368 | elog_debug("elog_shrink()\n"); |
| 369 | |
Sol Boucher | b419149 | 2015-03-12 18:22:31 -0700 | [diff] [blame] | 370 | if (next_event_offset < shrink_size) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 371 | return 0; |
| 372 | |
| 373 | while (1) { |
| 374 | /* Next event has exceeded constraints */ |
Sol Boucher | b419149 | 2015-03-12 18:22:31 -0700 | [diff] [blame] | 375 | if (offset > shrink_size) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 376 | break; |
| 377 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 378 | event = elog_get_event_base(offset); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 379 | |
| 380 | /* Reached the end of the area */ |
| 381 | if (!event || event->type == ELOG_TYPE_EOL) |
| 382 | break; |
| 383 | |
| 384 | offset += event->length; |
| 385 | discard_count++; |
| 386 | } |
| 387 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 388 | new_size = next_event_offset - offset; |
| 389 | memmove(&elog_area->data[0], &elog_area->data[offset], new_size); |
| 390 | memset(&elog_area->data[new_size], ELOG_TYPE_EOL, log_size - new_size); |
Gabe Black | bfae4aa | 2013-04-24 03:16:21 -0700 | [diff] [blame] | 391 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 392 | elog_flash_erase(elog_area, total_size); |
| 393 | elog_flash_write(elog_area, total_size); |
| 394 | elog_scan_flash(); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 395 | |
Duncan Laurie | 032be82 | 2013-05-23 07:23:09 -0700 | [diff] [blame] | 396 | /* Ensure the area was successfully erased */ |
Sol Boucher | b419149 | 2015-03-12 18:22:31 -0700 | [diff] [blame] | 397 | if (next_event_offset >= full_threshold) { |
Duncan Laurie | 032be82 | 2013-05-23 07:23:09 -0700 | [diff] [blame] | 398 | printk(BIOS_ERR, "ELOG: Flash area was not erased!\n"); |
| 399 | return -1; |
| 400 | } |
| 401 | |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 402 | /* Add clear event */ |
| 403 | elog_add_event_word(ELOG_TYPE_LOG_CLEAR, offset); |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
Duncan Laurie | 215f2785 | 2012-10-10 14:34:49 -0700 | [diff] [blame] | 408 | #ifndef __SMM__ |
Furquan Shaikh | 7f4221c | 2014-11-12 16:19:37 -0800 | [diff] [blame] | 409 | #if IS_ENABLED(CONFIG_ARCH_X86) |
| 410 | |
| 411 | /* |
| 412 | * Convert a flash offset into a memory mapped flash address |
| 413 | */ |
| 414 | static inline u8 *elog_flash_offset_to_address(u32 offset) |
| 415 | { |
| 416 | u32 rom_size; |
| 417 | |
| 418 | if (!elog_spi) |
| 419 | return NULL; |
| 420 | |
| 421 | rom_size = get_rom_size(); |
| 422 | |
| 423 | return (u8 *)((u32)~0UL - rom_size + 1 + offset); |
| 424 | } |
| 425 | |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 426 | /* |
Duncan Laurie | 472ec9c | 2012-06-23 16:13:42 -0700 | [diff] [blame] | 427 | * Fill out SMBIOS Type 15 table entry so the |
| 428 | * event log can be discovered at runtime. |
| 429 | */ |
| 430 | int elog_smbios_write_type15(unsigned long *current, int handle) |
| 431 | { |
| 432 | struct smbios_type15 *t = (struct smbios_type15 *)*current; |
| 433 | int len = sizeof(struct smbios_type15); |
| 434 | |
Duncan Laurie | 215f2785 | 2012-10-10 14:34:49 -0700 | [diff] [blame] | 435 | #if CONFIG_ELOG_CBMEM |
| 436 | /* Save event log buffer into CBMEM for the OS to read */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 437 | void *cbmem = cbmem_add(CBMEM_ID_ELOG, total_size); |
Duncan Laurie | 215f2785 | 2012-10-10 14:34:49 -0700 | [diff] [blame] | 438 | if (!cbmem) |
| 439 | return 0; |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 440 | memcpy(cbmem, elog_area, total_size); |
Duncan Laurie | 215f2785 | 2012-10-10 14:34:49 -0700 | [diff] [blame] | 441 | #endif |
| 442 | |
Duncan Laurie | 472ec9c | 2012-06-23 16:13:42 -0700 | [diff] [blame] | 443 | memset(t, 0, len); |
| 444 | t->type = SMBIOS_EVENT_LOG; |
| 445 | t->length = len - 2; |
| 446 | t->handle = handle; |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 447 | t->area_length = total_size - 1; |
Duncan Laurie | 472ec9c | 2012-06-23 16:13:42 -0700 | [diff] [blame] | 448 | t->header_offset = 0; |
| 449 | t->data_offset = sizeof(struct elog_header); |
| 450 | t->access_method = SMBIOS_EVENTLOG_ACCESS_METHOD_MMIO32; |
| 451 | t->log_status = SMBIOS_EVENTLOG_STATUS_VALID; |
| 452 | t->change_token = 0; |
Duncan Laurie | 215f2785 | 2012-10-10 14:34:49 -0700 | [diff] [blame] | 453 | #if CONFIG_ELOG_CBMEM |
| 454 | t->address = (u32)cbmem; |
| 455 | #else |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 456 | t->address = (u32)elog_flash_offset_to_address(flash_base); |
Duncan Laurie | 215f2785 | 2012-10-10 14:34:49 -0700 | [diff] [blame] | 457 | #endif |
Duncan Laurie | 472ec9c | 2012-06-23 16:13:42 -0700 | [diff] [blame] | 458 | t->header_format = ELOG_HEADER_TYPE_OEM; |
| 459 | t->log_type_descriptors = 0; |
| 460 | t->log_type_descriptor_length = 2; |
| 461 | |
| 462 | *current += len; |
| 463 | return len; |
| 464 | } |
Duncan Laurie | 215f2785 | 2012-10-10 14:34:49 -0700 | [diff] [blame] | 465 | #endif |
Furquan Shaikh | 7f4221c | 2014-11-12 16:19:37 -0800 | [diff] [blame] | 466 | #endif |
Duncan Laurie | 472ec9c | 2012-06-23 16:13:42 -0700 | [diff] [blame] | 467 | |
| 468 | /* |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 469 | * Clear the entire event log |
| 470 | */ |
| 471 | int elog_clear(void) |
| 472 | { |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 473 | elog_debug("elog_clear()\n"); |
| 474 | |
Gabe Black | 8f4baec | 2013-04-25 17:21:58 -0700 | [diff] [blame] | 475 | /* Make sure ELOG structures are initialized */ |
| 476 | if (elog_init() < 0) |
| 477 | return -1; |
| 478 | |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 479 | /* Erase flash area */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 480 | elog_flash_erase(elog_area, total_size); |
| 481 | elog_prepare_empty(); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 482 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 483 | if (!elog_is_area_valid()) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 484 | return -1; |
| 485 | |
| 486 | /* Log the clear event */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 487 | elog_add_event_word(ELOG_TYPE_LOG_CLEAR, total_size); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 492 | static void elog_find_flash(void) |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 493 | { |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 494 | elog_debug("elog_find_flash()\n"); |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 495 | |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 496 | if (IS_ENABLED(CONFIG_CHROMEOS)) { |
| 497 | /* Find the ELOG base and size in FMAP */ |
| 498 | struct region r; |
| 499 | |
| 500 | if (fmap_locate_area("RW_ELOG", &r) < 0) { |
| 501 | printk(BIOS_WARNING, |
| 502 | "ELOG: Unable to find RW_ELOG in FMAP\n"); |
| 503 | flash_base = total_size = 0; |
| 504 | } else { |
| 505 | flash_base = region_offset(&r); |
| 506 | total_size = MIN(region_sz(&r), CONFIG_ELOG_AREA_SIZE); |
| 507 | } |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 508 | } else { |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 509 | flash_base = CONFIG_ELOG_FLASH_BASE; |
| 510 | total_size = CONFIG_ELOG_AREA_SIZE; |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 511 | } |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 512 | log_size = total_size - sizeof(struct elog_header); |
Sol Boucher | b419149 | 2015-03-12 18:22:31 -0700 | [diff] [blame] | 513 | full_threshold = log_size - ELOG_MIN_AVAILABLE_ENTRIES * MAX_EVENT_SIZE; |
| 514 | shrink_size = MIN(total_size * ELOG_SHRINK_PERCENTAGE / 100, |
| 515 | full_threshold); |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 518 | /* |
| 519 | * Event log main entry point |
| 520 | */ |
| 521 | int elog_init(void) |
| 522 | { |
Julius Werner | 5d68622 | 2015-01-27 15:40:47 -0800 | [diff] [blame] | 523 | switch (elog_initialized) { |
| 524 | case ELOG_UNINITIALIZED: |
| 525 | break; |
| 526 | case ELOG_INITIALIZED: |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 527 | return 0; |
Julius Werner | 5d68622 | 2015-01-27 15:40:47 -0800 | [diff] [blame] | 528 | case ELOG_BROKEN: |
| 529 | return -1; |
| 530 | } |
| 531 | elog_initialized = ELOG_BROKEN; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 532 | |
| 533 | elog_debug("elog_init()\n"); |
| 534 | |
David Hendricks | 9acbd6f | 2014-04-13 16:45:31 -0700 | [diff] [blame] | 535 | /* Probe SPI chip. SPI controller must already be initialized. */ |
David Hendricks | c947024 | 2014-04-14 14:57:36 -0700 | [diff] [blame] | 536 | elog_spi = spi_flash_probe(CONFIG_BOOT_MEDIA_SPI_BUS, 0); |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 537 | if (!elog_spi) { |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 538 | printk(BIOS_ERR, "ELOG: Unable to find SPI flash\n"); |
| 539 | return -1; |
| 540 | } |
| 541 | |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 542 | /* Set up the backing store */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 543 | elog_find_flash(); |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 544 | if (flash_base == 0) { |
| 545 | printk(BIOS_ERR, "ELOG: Invalid flash base\n"); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 546 | return -1; |
Sol Boucher | b419149 | 2015-03-12 18:22:31 -0700 | [diff] [blame] | 547 | } else if (total_size < sizeof(struct elog_header) + MAX_EVENT_SIZE) { |
| 548 | printk(BIOS_ERR, "ELOG: Region too small to hold any events\n"); |
| 549 | return -1; |
| 550 | } else if (log_size - shrink_size >= full_threshold) { |
| 551 | printk(BIOS_ERR, |
| 552 | "ELOG: SHRINK_PERCENTAGE set too small for MIN_AVAILABLE_ENTRIES\n"); |
| 553 | return -1; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 556 | elog_area = malloc(total_size); |
| 557 | if (!elog_area) { |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 558 | printk(BIOS_ERR, "ELOG: Unable to allocate backing store\n"); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 559 | return -1; |
| 560 | } |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 561 | |
| 562 | /* Load the log from flash */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 563 | elog_scan_flash(); |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 564 | |
| 565 | /* Prepare the flash if necessary */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 566 | if (header_state == ELOG_HEADER_INVALID || |
| 567 | event_buffer_state == ELOG_EVENT_BUFFER_CORRUPTED) { |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 568 | /* If the header is invalid or the events are corrupted, |
| 569 | * no events can be salvaged so erase the entire area. */ |
| 570 | printk(BIOS_ERR, "ELOG: flash area invalid\n"); |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 571 | elog_flash_erase(elog_area, total_size); |
| 572 | elog_prepare_empty(); |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 575 | if (area_state == ELOG_AREA_EMPTY) |
| 576 | elog_prepare_empty(); |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 577 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 578 | if (!elog_is_area_valid()) { |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 579 | printk(BIOS_ERR, "ELOG: Unable to prepare flash\n"); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 580 | return -1; |
| 581 | } |
| 582 | |
Gabe Black | 331eb08 | 2013-04-24 04:11:40 -0700 | [diff] [blame] | 583 | printk(BIOS_INFO, "ELOG: FLASH @0x%p [SPI 0x%08x]\n", |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 584 | elog_area, flash_base); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 585 | |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 586 | printk(BIOS_INFO, "ELOG: area is %d bytes, full threshold %d," |
Sol Boucher | b419149 | 2015-03-12 18:22:31 -0700 | [diff] [blame] | 587 | " shrink size %d\n", total_size, full_threshold, shrink_size); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 588 | |
Julius Werner | efae69a | 2015-02-05 12:55:45 -0800 | [diff] [blame] | 589 | elog_initialized = ELOG_INITIALIZED; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 590 | |
Duncan Laurie | 5c88c6f | 2012-09-01 14:00:23 -0700 | [diff] [blame] | 591 | /* Shrink the log if we are getting too full */ |
Sol Boucher | b419149 | 2015-03-12 18:22:31 -0700 | [diff] [blame] | 592 | if (next_event_offset >= full_threshold) |
Duncan Laurie | 032be82 | 2013-05-23 07:23:09 -0700 | [diff] [blame] | 593 | if (elog_shrink() < 0) |
| 594 | return -1; |
Duncan Laurie | 5c88c6f | 2012-09-01 14:00:23 -0700 | [diff] [blame] | 595 | |
Julius Werner | efae69a | 2015-02-05 12:55:45 -0800 | [diff] [blame] | 596 | /* Log a clear event if necessary */ |
| 597 | if (event_count == 0) |
| 598 | elog_add_event_word(ELOG_TYPE_LOG_CLEAR, total_size); |
| 599 | |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 600 | #if !defined(__SMM__) |
Duncan Laurie | f4d3623 | 2012-06-23 16:37:45 -0700 | [diff] [blame] | 601 | /* Log boot count event except in S3 resume */ |
David Hendricks | 259e49a | 2014-03-20 20:31:23 -0700 | [diff] [blame] | 602 | #if CONFIG_ELOG_BOOT_COUNT == 1 |
| 603 | #if CONFIG_HAVE_ACPI_RESUME == 1 |
| 604 | if (!acpi_is_wakeup_s3()) |
| 605 | #endif |
Duncan Laurie | f4d3623 | 2012-06-23 16:37:45 -0700 | [diff] [blame] | 606 | elog_add_event_dword(ELOG_TYPE_BOOT, boot_count_read()); |
David Hendricks | 6a29e6c | 2014-06-18 13:03:03 -0700 | [diff] [blame] | 607 | #else |
| 608 | /* If boot count is not implemented, fake it. */ |
| 609 | elog_add_event_dword(ELOG_TYPE_BOOT, 0); |
David Hendricks | 259e49a | 2014-03-20 20:31:23 -0700 | [diff] [blame] | 610 | #endif |
Duncan Laurie | f4d3623 | 2012-06-23 16:37:45 -0700 | [diff] [blame] | 611 | |
David Hendricks | 259e49a | 2014-03-20 20:31:23 -0700 | [diff] [blame] | 612 | #if CONFIG_ARCH_X86 |
Duncan Laurie | 1fc3461 | 2012-09-09 19:14:45 -0700 | [diff] [blame] | 613 | /* Check and log POST codes from previous boot */ |
Gabe Black | 84a93d1 | 2013-04-24 23:31:41 -0700 | [diff] [blame] | 614 | if (CONFIG_CMOS_POST) |
| 615 | cmos_post_log(); |
Duncan Laurie | 1fc3461 | 2012-09-09 19:14:45 -0700 | [diff] [blame] | 616 | #endif |
David Hendricks | 259e49a | 2014-03-20 20:31:23 -0700 | [diff] [blame] | 617 | #endif |
Duncan Laurie | 1fc3461 | 2012-09-09 19:14:45 -0700 | [diff] [blame] | 618 | |
Julius Werner | 5d68622 | 2015-01-27 15:40:47 -0800 | [diff] [blame] | 619 | elog_initialized = ELOG_INITIALIZED; |
| 620 | |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * Populate timestamp in event header with current time |
| 626 | */ |
| 627 | static void elog_fill_timestamp(struct event_header *event) |
| 628 | { |
Patrick Georgi | 6e5f22e | 2015-04-22 13:28:21 +0200 | [diff] [blame] | 629 | #if IS_ENABLED(CONFIG_RTC) |
Gabe Black | a4c4b1c | 2014-04-30 21:41:23 -0700 | [diff] [blame] | 630 | struct rtc_time time; |
| 631 | |
| 632 | rtc_get(&time); |
| 633 | event->second = bin2bcd(time.sec); |
| 634 | event->minute = bin2bcd(time.min); |
| 635 | event->hour = bin2bcd(time.hour); |
| 636 | event->day = bin2bcd(time.mday); |
| 637 | event->month = bin2bcd(time.mon); |
Aaron Durbin | c41c6a4 | 2015-11-06 15:20:23 -0600 | [diff] [blame] | 638 | event->year = bin2bcd(time.year % 100); |
Patrick Georgi | 6e5f22e | 2015-04-22 13:28:21 +0200 | [diff] [blame] | 639 | |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 640 | /* Basic sanity check of expected ranges */ |
| 641 | if (event->month > 0x12 || event->day > 0x31 || event->hour > 0x23 || |
Patrick Georgi | 6e5f22e | 2015-04-22 13:28:21 +0200 | [diff] [blame] | 642 | event->minute > 0x59 || event->second > 0x59) |
| 643 | #endif |
| 644 | { |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 645 | event->year = 0; |
| 646 | event->month = 0; |
| 647 | event->day = 0; |
| 648 | event->hour = 0; |
| 649 | event->minute = 0; |
| 650 | event->second = 0; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /* |
Gabe Black | 331eb08 | 2013-04-24 04:11:40 -0700 | [diff] [blame] | 655 | * Add an event to the log |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 656 | */ |
Gabe Black | 331eb08 | 2013-04-24 04:11:40 -0700 | [diff] [blame] | 657 | void elog_add_event_raw(u8 event_type, void *data, u8 data_size) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 658 | { |
| 659 | struct event_header *event; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 660 | u8 event_size; |
| 661 | |
Gabe Black | 331eb08 | 2013-04-24 04:11:40 -0700 | [diff] [blame] | 662 | elog_debug("elog_add_event_raw(type=%X)\n", event_type); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 663 | |
| 664 | /* Make sure ELOG structures are initialized */ |
| 665 | if (elog_init() < 0) |
Gabe Black | 331eb08 | 2013-04-24 04:11:40 -0700 | [diff] [blame] | 666 | return; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 667 | |
| 668 | /* Header + Data + Checksum */ |
| 669 | event_size = sizeof(*event) + data_size + 1; |
| 670 | if (event_size > MAX_EVENT_SIZE) { |
| 671 | printk(BIOS_ERR, "ELOG: Event(%X) data size too " |
| 672 | "big (%d)\n", event_type, event_size); |
Gabe Black | 331eb08 | 2013-04-24 04:11:40 -0700 | [diff] [blame] | 673 | return; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /* Make sure event data can fit */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 677 | if ((next_event_offset + event_size) >= log_size) { |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 678 | printk(BIOS_ERR, "ELOG: Event(%X) does not fit\n", |
| 679 | event_type); |
Gabe Black | 331eb08 | 2013-04-24 04:11:40 -0700 | [diff] [blame] | 680 | return; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* Fill out event data */ |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 684 | event = elog_get_event_base(next_event_offset); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 685 | event->type = event_type; |
| 686 | event->length = event_size; |
| 687 | elog_fill_timestamp(event); |
| 688 | |
| 689 | if (data_size) |
| 690 | memcpy(&event[1], data, data_size); |
| 691 | |
| 692 | /* Zero the checksum byte and then compute checksum */ |
| 693 | elog_update_checksum(event, 0); |
| 694 | elog_update_checksum(event, -(elog_checksum_event(event))); |
| 695 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 696 | /* Update the ELOG state */ |
| 697 | event_count++; |
Gabe Black | 331eb08 | 2013-04-24 04:11:40 -0700 | [diff] [blame] | 698 | |
| 699 | elog_flash_write((void *)event, event_size); |
| 700 | |
Gabe Black | 0bf1feb | 2013-04-26 03:34:00 -0700 | [diff] [blame] | 701 | next_event_offset += event_size; |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 702 | |
| 703 | printk(BIOS_INFO, "ELOG: Event(%X) added with size %d\n", |
| 704 | event_type, event_size); |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 705 | |
| 706 | /* Shrink the log if we are getting too full */ |
Sol Boucher | b419149 | 2015-03-12 18:22:31 -0700 | [diff] [blame] | 707 | if (next_event_offset >= full_threshold) |
Duncan Laurie | 7d2b81c | 2012-06-23 16:08:47 -0700 | [diff] [blame] | 708 | elog_shrink(); |
| 709 | } |
| 710 | |
| 711 | void elog_add_event(u8 event_type) |
| 712 | { |
| 713 | elog_add_event_raw(event_type, NULL, 0); |
| 714 | } |
| 715 | |
| 716 | void elog_add_event_byte(u8 event_type, u8 data) |
| 717 | { |
| 718 | elog_add_event_raw(event_type, &data, sizeof(data)); |
| 719 | } |
| 720 | |
| 721 | void elog_add_event_word(u8 event_type, u16 data) |
| 722 | { |
| 723 | elog_add_event_raw(event_type, &data, sizeof(data)); |
| 724 | } |
| 725 | |
| 726 | void elog_add_event_dword(u8 event_type, u32 data) |
| 727 | { |
| 728 | elog_add_event_raw(event_type, &data, sizeof(data)); |
| 729 | } |
| 730 | |
| 731 | void elog_add_event_wake(u8 source, u32 instance) |
| 732 | { |
| 733 | struct elog_event_data_wake wake = { |
| 734 | .source = source, |
| 735 | .instance = instance |
| 736 | }; |
| 737 | elog_add_event_raw(ELOG_TYPE_WAKE_SOURCE, &wake, sizeof(wake)); |
| 738 | } |