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