blob: 77019065ed25ce0a1cce855396edb2e067248a0f [file] [log] [blame]
Duncan Laurie7d2b81c2012-06-23 16:08:47 -07001/*
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 Hendricks259e49a2014-03-20 20:31:23 -070020#if CONFIG_HAVE_ACPI_RESUME == 1
Duncan Laurie7d2b81c2012-06-23 16:08:47 -070021#include <arch/acpi.h>
David Hendricks259e49a2014-03-20 20:31:23 -070022#endif
Duncan Laurie215f27852012-10-10 14:34:49 -070023#include <cbmem.h>
Duncan Laurie7d2b81c2012-06-23 16:08:47 -070024#include <console/console.h>
David Hendricks259e49a2014-03-20 20:31:23 -070025#if CONFIG_ARCH_X86
Duncan Laurie7d2b81c2012-06-23 16:08:47 -070026#include <pc80/mc146818rtc.h>
David Hendricks259e49a2014-03-20 20:31:23 -070027#endif
Gabe Blacka4c4b1c2014-04-30 21:41:23 -070028#include <bcd.h>
29#include <rtc.h>
Duncan Laurie472ec9c2012-06-23 16:13:42 -070030#include <smbios.h>
Zheng Bao600784e2013-02-07 17:30:23 +080031#include <spi-generic.h>
Duncan Laurie7d2b81c2012-06-23 16:08:47 -070032#include <spi_flash.h>
33#include <stdint.h>
34#include <string.h>
35#include <elog.h>
36#include "elog_internal.h"
37
Duncan Laurie86bf3f52012-08-15 13:14:58 -070038#include <vendorcode/google/chromeos/fmap.h>
Edward O'Callaghana3119e52014-06-18 14:28:03 +100039
40#if CONFIG_ELOG_FLASH_BASE == 0
Duncan Laurie7d2b81c2012-06-23 16:08:47 -070041#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 Black0bf1feb2013-04-26 03:34:00 -070066static struct elog_area *elog_area;
67static u16 total_size;
68static u16 log_size;
69static u32 flash_base;
70
71static elog_area_state area_state;
72static elog_header_state header_state;
73static elog_event_buffer_state event_buffer_state;
74
75static u16 next_event_offset;
76static u16 event_count;
77
Duncan Laurie7d2b81c2012-06-23 16:08:47 -070078static int elog_initialized;
79static struct spi_flash *elog_spi;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -070080
Aaron Durbin10a070b2013-06-28 12:30:53 -070081
82static 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 Laurie7d2b81c2012-06-23 16:08:47 -070095/*
96 * Convert a memory mapped flash address into a flash offset
97 */
98static inline u32 elog_flash_address_to_offset(u8 *address)
99{
David Hendricks259e49a2014-03-20 20:31:23 -0700100#if CONFIG_ARCH_X86
101 /* For x86, assume address is memory-mapped near 4GB */
Aaron Durbin10a070b2013-06-28 12:30:53 -0700102 u32 rom_size;
103
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700104 if (!elog_spi)
105 return 0;
Aaron Durbin10a070b2013-06-28 12:30:53 -0700106
107 rom_size = get_rom_size();
108
109 return (u32)address - ((u32)~0UL - rom_size + 1);
David Hendricks259e49a2014-03-20 20:31:23 -0700110#else
111 return (u32)address;
112#endif
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700113}
114
115/*
116 * Convert a flash offset into a memory mapped flash address
117 */
118static inline u8* elog_flash_offset_to_address(u32 offset)
119{
Aaron Durbin10a070b2013-06-28 12:30:53 -0700120 u32 rom_size;
121
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700122 if (!elog_spi)
123 return NULL;
Aaron Durbin10a070b2013-06-28 12:30:53 -0700124
125 rom_size = get_rom_size();
126
127 return (u8*)((u32)~0UL - rom_size + 1 + offset);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700128}
129
130/*
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700131 * Pointer to an event log header in the event data area
132 */
133static inline struct event_header*
Gabe Black0bf1feb2013-04-26 03:34:00 -0700134elog_get_event_base(u32 offset)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700135{
Gabe Black0bf1feb2013-04-26 03:34:00 -0700136 return (struct event_header *)&elog_area->data[offset];
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700137}
138
139/*
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700140 * Update the checksum at the last byte
141 */
142static 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 */
151static 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 Black0bf1feb2013-04-26 03:34:00 -0700164static int elog_is_buffer_clear(void *base, u32 size)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700165{
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 Laurie7d2b81c2012-06-23 16:08:47 -0700179 * Check that the ELOG area has been initialized and is valid.
180 */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700181static int elog_is_area_valid(void)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700182{
183 elog_debug("elog_is_area_valid()\n");
184
Gabe Black0bf1feb2013-04-26 03:34:00 -0700185 if (area_state != ELOG_AREA_HAS_CONTENT)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700186 return 0;
Gabe Black0bf1feb2013-04-26 03:34:00 -0700187 if (header_state != ELOG_HEADER_VALID)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700188 return 0;
Gabe Black0bf1feb2013-04-26 03:34:00 -0700189 if (event_buffer_state != ELOG_EVENT_BUFFER_OK)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700190 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 */
198static 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' Cariklibc2c9ef2013-06-26 20:04:49 +0200213 printk(BIOS_ERR, "ELOG: header size mismatch %u != %zu\n",
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700214 header->header_size, sizeof(*header));
215 return 0;
216 }
217 return 1;
218}
219
220/*
221 * Validate the event header and data.
222 */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700223static int elog_is_event_valid(u32 offset)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700224{
225 struct event_header *event;
226
Gabe Black0bf1feb2013-04-26 03:34:00 -0700227 event = elog_get_event_base(offset);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700228 if (!event)
229 return 0;
230
231 /* Validate event length */
232 if ((offsetof(struct event_header, type) +
Gabe Black0bf1feb2013-04-26 03:34:00 -0700233 sizeof(event->type) - 1 + offset) >= log_size)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700234 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 Black0bf1feb2013-04-26 03:34:00 -0700242 sizeof(event->length) - 1 + offset) >= log_size)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700243 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 Black0bf1feb2013-04-26 03:34:00 -0700249 if ((event->length + offset) >= log_size)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700250 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 Black331eb082013-04-24 04:11:40 -0700265 * 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 Laurie7d2b81c2012-06-23 16:08:47 -0700268 */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700269static void elog_flash_write(void *address, u32 size)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700270{
271 u32 offset;
272
Gabe Black331eb082013-04-24 04:11:40 -0700273 if (!address || !size || !elog_spi)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700274 return;
275
Gabe Black0bf1feb2013-04-26 03:34:00 -0700276 offset = flash_base;
277 offset += (u8 *)address - (u8 *)elog_area;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700278
Gabe Black331eb082013-04-24 04:11:40 -0700279 elog_debug("elog_flash_write(address=0x%p offset=0x%08x size=%u)\n",
280 address, offset, size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700281
282 /* Write the data to flash */
Gabe Black331eb082013-04-24 04:11:40 -0700283 elog_spi->write(elog_spi, offset, size, address);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700284}
285
286/*
287 * Erase the first block specified in the address.
288 * Only handles flash area within a single flash block.
289 */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700290static void elog_flash_erase(void *address, u32 size)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700291{
292 u32 offset;
293
294 if (!address || !size || !elog_spi)
295 return;
296
Gabe Black0bf1feb2013-04-26 03:34:00 -0700297 offset = flash_base;
298 offset += (u8 *)address - (u8*)elog_area;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700299
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 Black0bf1feb2013-04-26 03:34:00 -0700308 * Scan the event area and validate each entry and update the ELOG state.
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700309 */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700310static void elog_update_event_buffer_state(void)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700311{
312 u32 count = 0;
313 u32 offset = 0;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700314 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 Black0bf1feb2013-04-26 03:34:00 -0700320 event = elog_get_event_base(offset);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700321
322 /* Do not de-reference anything past the area length */
323 if ((offsetof(struct event_header, type) +
Gabe Black0bf1feb2013-04-26 03:34:00 -0700324 sizeof(event->type) - 1 + offset) >= log_size) {
325 event_buffer_state = ELOG_EVENT_BUFFER_CORRUPTED;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700326 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 Black0bf1feb2013-04-26 03:34:00 -0700334 if (!elog_is_event_valid(offset)) {
335 event_buffer_state = ELOG_EVENT_BUFFER_CORRUPTED;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700336 break;
337 }
338
339 /* Move to the next event */
340 count++;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700341 offset += event->length;
342 }
343
344 /* Ensure the remaining buffer is empty */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700345 if (!elog_is_buffer_clear(&elog_area->data[offset], log_size - offset))
346 event_buffer_state = ELOG_EVENT_BUFFER_CORRUPTED;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700347
Gabe Black0bf1feb2013-04-26 03:34:00 -0700348 /* Update ELOG state */
349 event_count = count;
350 next_event_offset = offset;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700351}
352
Gabe Black0bf1feb2013-04-26 03:34:00 -0700353static void elog_scan_flash(void)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700354{
Gabe Black84a93d12013-04-24 23:31:41 -0700355 elog_debug("elog_scan_flash()\n");
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700356
Gabe Black0bf1feb2013-04-26 03:34:00 -0700357 area_state = ELOG_AREA_UNDEFINED;
358 header_state = ELOG_HEADER_INVALID;
359 event_buffer_state = ELOG_EVENT_BUFFER_OK;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700360
Duncan Laurie215f27852012-10-10 14:34:49 -0700361 /* Fill memory buffer by reading from SPI */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700362 elog_spi->read(elog_spi, flash_base, total_size, elog_area);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700363
Gabe Black0bf1feb2013-04-26 03:34:00 -0700364 next_event_offset = 0;
365 event_count = 0;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700366
Gabe Black455c68e2013-04-24 17:54:37 -0700367 /* Check if the area is empty or not */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700368 if (elog_is_buffer_clear(elog_area, total_size)) {
369 area_state = ELOG_AREA_EMPTY;
Gabe Black455c68e2013-04-24 17:54:37 -0700370 return;
371 }
372
Gabe Black0bf1feb2013-04-26 03:34:00 -0700373 area_state = ELOG_AREA_HAS_CONTENT;
Gabe Black455c68e2013-04-24 17:54:37 -0700374
375 /* Validate the header */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700376 if (!elog_is_header_valid(&elog_area->header)) {
377 header_state = ELOG_HEADER_INVALID;
Gabe Black455c68e2013-04-24 17:54:37 -0700378 return;
379 }
380
Gabe Black0bf1feb2013-04-26 03:34:00 -0700381 header_state = ELOG_HEADER_VALID;
382 elog_update_event_buffer_state();
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700383}
384
Gabe Black0bf1feb2013-04-26 03:34:00 -0700385static void elog_prepare_empty(void)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700386{
387 struct elog_header *header;
388
Gabe Black0bf1feb2013-04-26 03:34:00 -0700389 elog_debug("elog_prepare_empty()\n");
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700390
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700391 /* Write out the header */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700392 header = &elog_area->header;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700393 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 Black0bf1feb2013-04-26 03:34:00 -0700398 elog_flash_write(elog_area, header->header_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700399
Gabe Black0bf1feb2013-04-26 03:34:00 -0700400 elog_scan_flash();
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700401}
402
403/*
404 * Shrink the log, deleting old entries and moving the
Martin Roth56889792013-07-09 21:39:46 -0600405 * remaining ones to the front of the log.
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700406 */
407static int elog_shrink(void)
408{
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700409 struct event_header *event;
410 u16 discard_count = 0;
411 u16 offset = 0;
Gabe Blackbfae4aa2013-04-24 03:16:21 -0700412 u16 new_size = 0;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700413
414 elog_debug("elog_shrink()\n");
415
Gabe Black0bf1feb2013-04-26 03:34:00 -0700416 if (next_event_offset < CONFIG_ELOG_SHRINK_SIZE)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700417 return 0;
418
419 while (1) {
420 /* Next event has exceeded constraints */
421 if (offset > CONFIG_ELOG_SHRINK_SIZE)
422 break;
423
Gabe Black0bf1feb2013-04-26 03:34:00 -0700424 event = elog_get_event_base(offset);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700425
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 Black0bf1feb2013-04-26 03:34:00 -0700434 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 Blackbfae4aa2013-04-24 03:16:21 -0700437
Gabe Black0bf1feb2013-04-26 03:34:00 -0700438 elog_flash_erase(elog_area, total_size);
439 elog_flash_write(elog_area, total_size);
440 elog_scan_flash();
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700441
Duncan Laurie032be822013-05-23 07:23:09 -0700442 /* Ensure the area was successfully erased */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700443 if (next_event_offset >= CONFIG_ELOG_FULL_THRESHOLD) {
Duncan Laurie032be822013-05-23 07:23:09 -0700444 printk(BIOS_ERR, "ELOG: Flash area was not erased!\n");
445 return -1;
446 }
447
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700448 /* Add clear event */
449 elog_add_event_word(ELOG_TYPE_LOG_CLEAR, offset);
450
451 return 0;
452}
453
Duncan Laurie215f27852012-10-10 14:34:49 -0700454#ifndef __SMM__
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700455/*
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700456 * Fill out SMBIOS Type 15 table entry so the
457 * event log can be discovered at runtime.
458 */
459int 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 Laurie215f27852012-10-10 14:34:49 -0700464#if CONFIG_ELOG_CBMEM
465 /* Save event log buffer into CBMEM for the OS to read */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700466 void *cbmem = cbmem_add(CBMEM_ID_ELOG, total_size);
Duncan Laurie215f27852012-10-10 14:34:49 -0700467 if (!cbmem)
468 return 0;
Gabe Black0bf1feb2013-04-26 03:34:00 -0700469 memcpy(cbmem, elog_area, total_size);
Duncan Laurie215f27852012-10-10 14:34:49 -0700470#endif
471
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700472 memset(t, 0, len);
473 t->type = SMBIOS_EVENT_LOG;
474 t->length = len - 2;
475 t->handle = handle;
Gabe Black0bf1feb2013-04-26 03:34:00 -0700476 t->area_length = total_size - 1;
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700477 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 Laurie215f27852012-10-10 14:34:49 -0700482#if CONFIG_ELOG_CBMEM
483 t->address = (u32)cbmem;
484#else
Gabe Black0bf1feb2013-04-26 03:34:00 -0700485 t->address = (u32)elog_flash_offset_to_address(flash_base);
Duncan Laurie215f27852012-10-10 14:34:49 -0700486#endif
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700487 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 Laurie215f27852012-10-10 14:34:49 -0700494#endif
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700495
496/*
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700497 * Clear the entire event log
498 */
499int elog_clear(void)
500{
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700501 elog_debug("elog_clear()\n");
502
Gabe Black8f4baec2013-04-25 17:21:58 -0700503 /* Make sure ELOG structures are initialized */
504 if (elog_init() < 0)
505 return -1;
506
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700507 /* Erase flash area */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700508 elog_flash_erase(elog_area, total_size);
509 elog_prepare_empty();
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700510
Gabe Black0bf1feb2013-04-26 03:34:00 -0700511 if (!elog_is_area_valid())
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700512 return -1;
513
514 /* Log the clear event */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700515 elog_add_event_word(ELOG_TYPE_LOG_CLEAR, total_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700516
517 return 0;
518}
519
Gabe Black0bf1feb2013-04-26 03:34:00 -0700520static void elog_find_flash(void)
Gabe Black84a93d12013-04-24 23:31:41 -0700521{
522#if CONFIG_CHROMEOS
523 u8 *flash_base_ptr;
524#endif
525
Gabe Black0bf1feb2013-04-26 03:34:00 -0700526 elog_debug("elog_find_flash()\n");
Gabe Black84a93d12013-04-24 23:31:41 -0700527
528#if CONFIG_CHROMEOS
529 /* Find the ELOG base and size in FMAP */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700530 total_size = find_fmap_entry("RW_ELOG", (void **)&flash_base_ptr);
531 if (total_size < 0) {
Gabe Black84a93d12013-04-24 23:31:41 -0700532 printk(BIOS_WARNING, "ELOG: Unable to find RW_ELOG in FMAP, "
533 "using CONFIG_ELOG_FLASH_BASE instead\n");
Gabe Black0bf1feb2013-04-26 03:34:00 -0700534 total_size = CONFIG_ELOG_AREA_SIZE;
Gabe Black84a93d12013-04-24 23:31:41 -0700535 } else {
Gabe Black0bf1feb2013-04-26 03:34:00 -0700536 flash_base = elog_flash_address_to_offset(flash_base_ptr);
Gabe Black84a93d12013-04-24 23:31:41 -0700537
538 /* Use configured size if smaller than FMAP size */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700539 if (total_size > CONFIG_ELOG_AREA_SIZE)
540 total_size = CONFIG_ELOG_AREA_SIZE;
Gabe Black84a93d12013-04-24 23:31:41 -0700541 }
542#else
Gabe Black0bf1feb2013-04-26 03:34:00 -0700543 flash_base = CONFIG_ELOG_FLASH_BASE;
544 total_size = CONFIG_ELOG_AREA_SIZE;
Gabe Black84a93d12013-04-24 23:31:41 -0700545#endif
Gabe Black0bf1feb2013-04-26 03:34:00 -0700546 log_size = total_size - sizeof(struct elog_header);
Gabe Black84a93d12013-04-24 23:31:41 -0700547}
548
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700549/*
550 * Event log main entry point
551 */
552int elog_init(void)
553{
554 if (elog_initialized)
555 return 0;
556
557 elog_debug("elog_init()\n");
558
David Hendricks9acbd6f2014-04-13 16:45:31 -0700559 /* Probe SPI chip. SPI controller must already be initialized. */
David Hendricksc9470242014-04-14 14:57:36 -0700560 elog_spi = spi_flash_probe(CONFIG_BOOT_MEDIA_SPI_BUS, 0);
Gabe Black84a93d12013-04-24 23:31:41 -0700561 if (!elog_spi) {
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700562 printk(BIOS_ERR, "ELOG: Unable to find SPI flash\n");
563 return -1;
564 }
565
Gabe Black84a93d12013-04-24 23:31:41 -0700566 /* Set up the backing store */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700567 elog_find_flash();
Gabe Black84a93d12013-04-24 23:31:41 -0700568 if (flash_base == 0) {
569 printk(BIOS_ERR, "ELOG: Invalid flash base\n");
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700570 return -1;
571 }
572
Gabe Black0bf1feb2013-04-26 03:34:00 -0700573 elog_area = malloc(total_size);
574 if (!elog_area) {
Gabe Black84a93d12013-04-24 23:31:41 -0700575 printk(BIOS_ERR, "ELOG: Unable to allocate backing store\n");
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700576 return -1;
577 }
Gabe Black84a93d12013-04-24 23:31:41 -0700578
579 /* Load the log from flash */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700580 elog_scan_flash();
Gabe Black84a93d12013-04-24 23:31:41 -0700581
582 /* Prepare the flash if necessary */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700583 if (header_state == ELOG_HEADER_INVALID ||
584 event_buffer_state == ELOG_EVENT_BUFFER_CORRUPTED) {
Gabe Black84a93d12013-04-24 23:31:41 -0700585 /* 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 Black0bf1feb2013-04-26 03:34:00 -0700588 elog_flash_erase(elog_area, total_size);
589 elog_prepare_empty();
Gabe Black84a93d12013-04-24 23:31:41 -0700590 }
591
Gabe Black0bf1feb2013-04-26 03:34:00 -0700592 if (area_state == ELOG_AREA_EMPTY)
593 elog_prepare_empty();
Gabe Black84a93d12013-04-24 23:31:41 -0700594
Gabe Black0bf1feb2013-04-26 03:34:00 -0700595 if (!elog_is_area_valid()) {
Gabe Black84a93d12013-04-24 23:31:41 -0700596 printk(BIOS_ERR, "ELOG: Unable to prepare flash\n");
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700597 return -1;
598 }
599
600 elog_initialized = 1;
601
Gabe Black331eb082013-04-24 04:11:40 -0700602 printk(BIOS_INFO, "ELOG: FLASH @0x%p [SPI 0x%08x]\n",
Gabe Black0bf1feb2013-04-26 03:34:00 -0700603 elog_area, flash_base);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700604
Gabe Black84a93d12013-04-24 23:31:41 -0700605 printk(BIOS_INFO, "ELOG: area is %d bytes, full threshold %d,"
Gabe Black0bf1feb2013-04-26 03:34:00 -0700606 " shrink size %d\n", total_size,
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700607 CONFIG_ELOG_FULL_THRESHOLD, CONFIG_ELOG_SHRINK_SIZE);
608
609 /* Log a clear event if necessary */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700610 if (event_count == 0)
611 elog_add_event_word(ELOG_TYPE_LOG_CLEAR, total_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700612
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700613 /* Shrink the log if we are getting too full */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700614 if (next_event_offset >= CONFIG_ELOG_FULL_THRESHOLD)
Duncan Laurie032be822013-05-23 07:23:09 -0700615 if (elog_shrink() < 0)
616 return -1;
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700617
Gabe Black84a93d12013-04-24 23:31:41 -0700618#if !defined(__SMM__)
Duncan Laurief4d36232012-06-23 16:37:45 -0700619 /* Log boot count event except in S3 resume */
David Hendricks259e49a2014-03-20 20:31:23 -0700620#if CONFIG_ELOG_BOOT_COUNT == 1
621#if CONFIG_HAVE_ACPI_RESUME == 1
622 if (!acpi_is_wakeup_s3())
623#endif
Duncan Laurief4d36232012-06-23 16:37:45 -0700624 elog_add_event_dword(ELOG_TYPE_BOOT, boot_count_read());
David Hendricks6a29e6c2014-06-18 13:03:03 -0700625#else
626 /* If boot count is not implemented, fake it. */
627 elog_add_event_dword(ELOG_TYPE_BOOT, 0);
David Hendricks259e49a2014-03-20 20:31:23 -0700628#endif
Duncan Laurief4d36232012-06-23 16:37:45 -0700629
David Hendricks259e49a2014-03-20 20:31:23 -0700630#if CONFIG_ARCH_X86
Duncan Laurie1fc34612012-09-09 19:14:45 -0700631 /* Check and log POST codes from previous boot */
Gabe Black84a93d12013-04-24 23:31:41 -0700632 if (CONFIG_CMOS_POST)
633 cmos_post_log();
Duncan Laurie1fc34612012-09-09 19:14:45 -0700634#endif
David Hendricks259e49a2014-03-20 20:31:23 -0700635#endif
Duncan Laurie1fc34612012-09-09 19:14:45 -0700636
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700637 return 0;
638}
639
640/*
641 * Populate timestamp in event header with current time
642 */
643static void elog_fill_timestamp(struct event_header *event)
644{
Gabe Blacka4c4b1c2014-04-30 21:41:23 -0700645 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 Laurie7d2b81c2012-06-23 16:08:47 -0700654
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 Black331eb082013-04-24 04:11:40 -0700668 * Add an event to the log
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700669 */
Gabe Black331eb082013-04-24 04:11:40 -0700670void elog_add_event_raw(u8 event_type, void *data, u8 data_size)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700671{
672 struct event_header *event;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700673 u8 event_size;
674
Gabe Black331eb082013-04-24 04:11:40 -0700675 elog_debug("elog_add_event_raw(type=%X)\n", event_type);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700676
677 /* Make sure ELOG structures are initialized */
678 if (elog_init() < 0)
Gabe Black331eb082013-04-24 04:11:40 -0700679 return;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700680
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 Black331eb082013-04-24 04:11:40 -0700686 return;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700687 }
688
689 /* Make sure event data can fit */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700690 if ((next_event_offset + event_size) >= log_size) {
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700691 printk(BIOS_ERR, "ELOG: Event(%X) does not fit\n",
692 event_type);
Gabe Black331eb082013-04-24 04:11:40 -0700693 return;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700694 }
695
696 /* Fill out event data */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700697 event = elog_get_event_base(next_event_offset);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700698 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 Black0bf1feb2013-04-26 03:34:00 -0700709 /* Update the ELOG state */
710 event_count++;
Gabe Black331eb082013-04-24 04:11:40 -0700711
712 elog_flash_write((void *)event, event_size);
713
Gabe Black0bf1feb2013-04-26 03:34:00 -0700714 next_event_offset += event_size;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700715
716 printk(BIOS_INFO, "ELOG: Event(%X) added with size %d\n",
717 event_type, event_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700718
719 /* Shrink the log if we are getting too full */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700720 if (next_event_offset >= CONFIG_ELOG_FULL_THRESHOLD)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700721 elog_shrink();
722}
723
724void elog_add_event(u8 event_type)
725{
726 elog_add_event_raw(event_type, NULL, 0);
727}
728
729void elog_add_event_byte(u8 event_type, u8 data)
730{
731 elog_add_event_raw(event_type, &data, sizeof(data));
732}
733
734void elog_add_event_word(u8 event_type, u16 data)
735{
736 elog_add_event_raw(event_type, &data, sizeof(data));
737}
738
739void elog_add_event_dword(u8 event_type, u32 data)
740{
741 elog_add_event_raw(event_type, &data, sizeof(data));
742}
743
744void 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}