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