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