tree: drop last paragraph of GPL copyright header
[coreboot.git] / src / drivers / elog / elog.c
blob97abf818918e1c9316537d21c223283e8b2a8750
1 /*
2 * This file is part of the coreboot project.
4 * Copyright (C) 2012 The ChromiumOS Authors. All rights reserved.
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.
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.
16 #if CONFIG_HAVE_ACPI_RESUME == 1
17 #include <arch/acpi.h>
18 #endif
19 #include <cbmem.h>
20 #include <console/console.h>
21 #if CONFIG_ARCH_X86
22 #include <pc80/mc146818rtc.h>
23 #endif
24 #include <bcd.h>
25 #include <fmap.h>
26 #include <rtc.h>
27 #include <smbios.h>
28 #include <spi-generic.h>
29 #include <spi_flash.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <elog.h>
33 #include "elog_internal.h"
36 #if !IS_ENABLED(CONFIG_CHROMEOS) && CONFIG_ELOG_FLASH_BASE == 0
37 #error "CONFIG_ELOG_FLASH_BASE is invalid"
38 #endif
40 #if CONFIG_ELOG_DEBUG
41 #define elog_debug(STR...) printk(BIOS_DEBUG, STR)
42 #else
43 #define elog_debug(STR...)
44 #endif
47 * Static variables for ELOG state
49 static struct elog_area *elog_area;
50 static u16 total_size;
51 static u16 log_size; /* excluding header */
52 static u32 flash_base;
53 static u16 full_threshold; /* from end of header */
54 static u16 shrink_size; /* from end of header */
56 static elog_area_state area_state;
57 static elog_header_state header_state;
58 static elog_event_buffer_state event_buffer_state;
60 static u16 next_event_offset; /* from end of header */
61 static u16 event_count;
63 static struct spi_flash *elog_spi;
65 static enum {
66 ELOG_UNINITIALIZED = 0,
67 ELOG_INITIALIZED,
68 ELOG_BROKEN,
69 } elog_initialized = ELOG_UNINITIALIZED;
71 static inline u32 get_rom_size(void)
73 u32 rom_size;
75 /* Assume the used space of the ROM image starts from 0. The
76 * physical size of the device may not be completely used. */
77 rom_size = elog_spi->size;
78 if (rom_size > CONFIG_ROM_SIZE)
79 rom_size = CONFIG_ROM_SIZE;
81 return rom_size;
85 * Pointer to an event log header in the event data area
87 static inline struct event_header*
88 elog_get_event_base(u32 offset)
90 return (struct event_header *)&elog_area->data[offset];
94 * Update the checksum at the last byte
96 static void elog_update_checksum(struct event_header *event, u8 checksum)
98 u8 *event_data = (u8*)event;
99 event_data[event->length - 1] = checksum;
103 * Simple byte checksum for events
105 static u8 elog_checksum_event(struct event_header *event)
107 u8 index, checksum = 0;
108 u8 *data = (u8*)event;
110 for (index = 0; index < event->length; index++)
111 checksum += data[index];
112 return checksum;
116 * Check if a raw buffer is filled with ELOG_TYPE_EOL byte
118 static int elog_is_buffer_clear(void *base, u32 size)
120 u8 *current = base;
121 u8 *end = current + size;
123 elog_debug("elog_is_buffer_clear(base=0x%p size=%u)\n", base, size);
125 for (; current != end; current++) {
126 if (*current != ELOG_TYPE_EOL)
127 return 0;
129 return 1;
133 * Check that the ELOG area has been initialized and is valid.
135 static int elog_is_area_valid(void)
137 elog_debug("elog_is_area_valid()\n");
139 if (area_state != ELOG_AREA_HAS_CONTENT)
140 return 0;
141 if (header_state != ELOG_HEADER_VALID)
142 return 0;
143 if (event_buffer_state != ELOG_EVENT_BUFFER_OK)
144 return 0;
145 return 1;
149 * Verify the contents of an ELOG Header structure
150 * Returns 1 if the header is valid, 0 otherwise
152 static int elog_is_header_valid(struct elog_header *header)
154 elog_debug("elog_is_header_valid()\n");
156 if (header->magic != ELOG_SIGNATURE) {
157 printk(BIOS_ERR, "ELOG: header magic 0x%X != 0x%X\n",
158 header->magic, ELOG_SIGNATURE);
159 return 0;
161 if (header->version != ELOG_VERSION) {
162 printk(BIOS_ERR, "ELOG: header version %u != %u\n",
163 header->version, ELOG_VERSION);
164 return 0;
166 if (header->header_size != sizeof(*header)) {
167 printk(BIOS_ERR, "ELOG: header size mismatch %u != %zu\n",
168 header->header_size, sizeof(*header));
169 return 0;
171 return 1;
175 * Validate the event header and data.
177 static int elog_is_event_valid(u32 offset)
179 struct event_header *event;
181 event = elog_get_event_base(offset);
182 if (!event)
183 return 0;
185 /* Validate event length */
186 if ((offsetof(struct event_header, type) +
187 sizeof(event->type) - 1 + offset) >= log_size)
188 return 0;
190 /* End of event marker has been found */
191 if (event->type == ELOG_TYPE_EOL)
192 return 0;
194 /* Check if event fits in area */
195 if ((offsetof(struct event_header, length) +
196 sizeof(event->length) - 1 + offset) >= log_size)
197 return 0;
200 * If the current event length + the current offset exceeds
201 * the area size then the event area is corrupt.
203 if ((event->length + offset) >= log_size)
204 return 0;
206 /* Event length must be at least header size + checksum */
207 if (event->length < (sizeof(*event) + 1))
208 return 0;
210 /* If event checksum is invalid the area is corrupt */
211 if (elog_checksum_event(event) != 0)
212 return 0;
214 /* Event is valid */
215 return 1;
219 * Write 'size' bytes of data pointed to by 'address' in the flash backing
220 * store into flash. This will not erase the flash and it assumes the flash
221 * area has been erased appropriately.
223 static void elog_flash_write(void *address, u32 size)
225 u32 offset;
227 if (!address || !size || !elog_spi)
228 return;
230 offset = flash_base;
231 offset += (u8 *)address - (u8 *)elog_area;
233 elog_debug("elog_flash_write(address=0x%p offset=0x%08x size=%u)\n",
234 address, offset, size);
236 /* Write the data to flash */
237 elog_spi->write(elog_spi, offset, size, address);
241 * Erase the first block specified in the address.
242 * Only handles flash area within a single flash block.
244 static void elog_flash_erase(void *address, u32 size)
246 u32 offset;
248 if (!address || !size || !elog_spi)
249 return;
251 offset = flash_base;
252 offset += (u8 *)address - (u8*)elog_area;
254 elog_debug("elog_flash_erase(address=0x%p offset=0x%08x size=%u)\n",
255 address, offset, size);
257 /* Erase the sectors in this region */
258 elog_spi->erase(elog_spi, offset, size);
262 * Scan the event area and validate each entry and update the ELOG state.
264 static void elog_update_event_buffer_state(void)
266 u32 count = 0;
267 u32 offset = 0;
268 struct event_header *event;
270 elog_debug("elog_update_event_buffer_state()\n");
272 /* Go through each event and validate it */
273 while (1) {
274 event = elog_get_event_base(offset);
276 /* Do not de-reference anything past the area length */
277 if ((offsetof(struct event_header, type) +
278 sizeof(event->type) - 1 + offset) >= log_size) {
279 event_buffer_state = ELOG_EVENT_BUFFER_CORRUPTED;
280 break;
283 /* The end of the event marker has been found */
284 if (event->type == ELOG_TYPE_EOL)
285 break;
287 /* Validate the event */
288 if (!elog_is_event_valid(offset)) {
289 event_buffer_state = ELOG_EVENT_BUFFER_CORRUPTED;
290 break;
293 /* Move to the next event */
294 count++;
295 offset += event->length;
298 /* Ensure the remaining buffer is empty */
299 if (!elog_is_buffer_clear(&elog_area->data[offset], log_size - offset))
300 event_buffer_state = ELOG_EVENT_BUFFER_CORRUPTED;
302 /* Update ELOG state */
303 event_count = count;
304 next_event_offset = offset;
307 static void elog_scan_flash(void)
309 elog_debug("elog_scan_flash()\n");
311 area_state = ELOG_AREA_UNDEFINED;
312 header_state = ELOG_HEADER_INVALID;
313 event_buffer_state = ELOG_EVENT_BUFFER_OK;
315 /* Fill memory buffer by reading from SPI */
316 elog_spi->read(elog_spi, flash_base, total_size, elog_area);
318 next_event_offset = 0;
319 event_count = 0;
321 /* Check if the area is empty or not */
322 if (elog_is_buffer_clear(elog_area, total_size)) {
323 area_state = ELOG_AREA_EMPTY;
324 return;
327 area_state = ELOG_AREA_HAS_CONTENT;
329 /* Validate the header */
330 if (!elog_is_header_valid(&elog_area->header)) {
331 header_state = ELOG_HEADER_INVALID;
332 return;
335 header_state = ELOG_HEADER_VALID;
336 elog_update_event_buffer_state();
339 static void elog_prepare_empty(void)
341 struct elog_header *header;
343 elog_debug("elog_prepare_empty()\n");
345 /* Write out the header */
346 header = &elog_area->header;
347 header->magic = ELOG_SIGNATURE;
348 header->version = ELOG_VERSION;
349 header->header_size = sizeof(struct elog_header);
350 header->reserved[0] = ELOG_TYPE_EOL;
351 header->reserved[1] = ELOG_TYPE_EOL;
352 elog_flash_write(elog_area, header->header_size);
354 elog_scan_flash();
358 * Shrink the log, deleting old entries and moving the
359 * remaining ones to the front of the log.
361 static int elog_shrink(void)
363 struct event_header *event;
364 u16 discard_count = 0;
365 u16 offset = 0;
366 u16 new_size = 0;
368 elog_debug("elog_shrink()\n");
370 if (next_event_offset < shrink_size)
371 return 0;
373 while (1) {
374 /* Next event has exceeded constraints */
375 if (offset > shrink_size)
376 break;
378 event = elog_get_event_base(offset);
380 /* Reached the end of the area */
381 if (!event || event->type == ELOG_TYPE_EOL)
382 break;
384 offset += event->length;
385 discard_count++;
388 new_size = next_event_offset - offset;
389 memmove(&elog_area->data[0], &elog_area->data[offset], new_size);
390 memset(&elog_area->data[new_size], ELOG_TYPE_EOL, log_size - new_size);
392 elog_flash_erase(elog_area, total_size);
393 elog_flash_write(elog_area, total_size);
394 elog_scan_flash();
396 /* Ensure the area was successfully erased */
397 if (next_event_offset >= full_threshold) {
398 printk(BIOS_ERR, "ELOG: Flash area was not erased!\n");
399 return -1;
402 /* Add clear event */
403 elog_add_event_word(ELOG_TYPE_LOG_CLEAR, offset);
405 return 0;
408 #ifndef __SMM__
409 #if IS_ENABLED(CONFIG_ARCH_X86)
412 * Convert a flash offset into a memory mapped flash address
414 static inline u8 *elog_flash_offset_to_address(u32 offset)
416 u32 rom_size;
418 if (!elog_spi)
419 return NULL;
421 rom_size = get_rom_size();
423 return (u8 *)((u32)~0UL - rom_size + 1 + offset);
427 * Fill out SMBIOS Type 15 table entry so the
428 * event log can be discovered at runtime.
430 int elog_smbios_write_type15(unsigned long *current, int handle)
432 struct smbios_type15 *t = (struct smbios_type15 *)*current;
433 int len = sizeof(struct smbios_type15);
435 #if CONFIG_ELOG_CBMEM
436 /* Save event log buffer into CBMEM for the OS to read */
437 void *cbmem = cbmem_add(CBMEM_ID_ELOG, total_size);
438 if (!cbmem)
439 return 0;
440 memcpy(cbmem, elog_area, total_size);
441 #endif
443 memset(t, 0, len);
444 t->type = SMBIOS_EVENT_LOG;
445 t->length = len - 2;
446 t->handle = handle;
447 t->area_length = total_size - 1;
448 t->header_offset = 0;
449 t->data_offset = sizeof(struct elog_header);
450 t->access_method = SMBIOS_EVENTLOG_ACCESS_METHOD_MMIO32;
451 t->log_status = SMBIOS_EVENTLOG_STATUS_VALID;
452 t->change_token = 0;
453 #if CONFIG_ELOG_CBMEM
454 t->address = (u32)cbmem;
455 #else
456 t->address = (u32)elog_flash_offset_to_address(flash_base);
457 #endif
458 t->header_format = ELOG_HEADER_TYPE_OEM;
459 t->log_type_descriptors = 0;
460 t->log_type_descriptor_length = 2;
462 *current += len;
463 return len;
465 #endif
466 #endif
469 * Clear the entire event log
471 int elog_clear(void)
473 elog_debug("elog_clear()\n");
475 /* Make sure ELOG structures are initialized */
476 if (elog_init() < 0)
477 return -1;
479 /* Erase flash area */
480 elog_flash_erase(elog_area, total_size);
481 elog_prepare_empty();
483 if (!elog_is_area_valid())
484 return -1;
486 /* Log the clear event */
487 elog_add_event_word(ELOG_TYPE_LOG_CLEAR, total_size);
489 return 0;
492 static void elog_find_flash(void)
494 elog_debug("elog_find_flash()\n");
496 if (IS_ENABLED(CONFIG_CHROMEOS)) {
497 /* Find the ELOG base and size in FMAP */
498 struct region r;
500 if (fmap_locate_area("RW_ELOG", &r) < 0) {
501 printk(BIOS_WARNING,
502 "ELOG: Unable to find RW_ELOG in FMAP\n");
503 flash_base = total_size = 0;
504 } else {
505 flash_base = region_offset(&r);
506 total_size = MIN(region_sz(&r), CONFIG_ELOG_AREA_SIZE);
508 } else {
509 flash_base = CONFIG_ELOG_FLASH_BASE;
510 total_size = CONFIG_ELOG_AREA_SIZE;
512 log_size = total_size - sizeof(struct elog_header);
513 full_threshold = log_size - ELOG_MIN_AVAILABLE_ENTRIES * MAX_EVENT_SIZE;
514 shrink_size = MIN(total_size * ELOG_SHRINK_PERCENTAGE / 100,
515 full_threshold);
519 * Event log main entry point
521 int elog_init(void)
523 switch (elog_initialized) {
524 case ELOG_UNINITIALIZED:
525 break;
526 case ELOG_INITIALIZED:
527 return 0;
528 case ELOG_BROKEN:
529 return -1;
531 elog_initialized = ELOG_BROKEN;
533 elog_debug("elog_init()\n");
535 /* Probe SPI chip. SPI controller must already be initialized. */
536 elog_spi = spi_flash_probe(CONFIG_BOOT_MEDIA_SPI_BUS, 0);
537 if (!elog_spi) {
538 printk(BIOS_ERR, "ELOG: Unable to find SPI flash\n");
539 return -1;
542 /* Set up the backing store */
543 elog_find_flash();
544 if (flash_base == 0) {
545 printk(BIOS_ERR, "ELOG: Invalid flash base\n");
546 return -1;
547 } else if (total_size < sizeof(struct elog_header) + MAX_EVENT_SIZE) {
548 printk(BIOS_ERR, "ELOG: Region too small to hold any events\n");
549 return -1;
550 } else if (log_size - shrink_size >= full_threshold) {
551 printk(BIOS_ERR,
552 "ELOG: SHRINK_PERCENTAGE set too small for MIN_AVAILABLE_ENTRIES\n");
553 return -1;
556 elog_area = malloc(total_size);
557 if (!elog_area) {
558 printk(BIOS_ERR, "ELOG: Unable to allocate backing store\n");
559 return -1;
562 /* Load the log from flash */
563 elog_scan_flash();
565 /* Prepare the flash if necessary */
566 if (header_state == ELOG_HEADER_INVALID ||
567 event_buffer_state == ELOG_EVENT_BUFFER_CORRUPTED) {
568 /* If the header is invalid or the events are corrupted,
569 * no events can be salvaged so erase the entire area. */
570 printk(BIOS_ERR, "ELOG: flash area invalid\n");
571 elog_flash_erase(elog_area, total_size);
572 elog_prepare_empty();
575 if (area_state == ELOG_AREA_EMPTY)
576 elog_prepare_empty();
578 if (!elog_is_area_valid()) {
579 printk(BIOS_ERR, "ELOG: Unable to prepare flash\n");
580 return -1;
583 printk(BIOS_INFO, "ELOG: FLASH @0x%p [SPI 0x%08x]\n",
584 elog_area, flash_base);
586 printk(BIOS_INFO, "ELOG: area is %d bytes, full threshold %d,"
587 " shrink size %d\n", total_size, full_threshold, shrink_size);
589 elog_initialized = ELOG_INITIALIZED;
591 /* Shrink the log if we are getting too full */
592 if (next_event_offset >= full_threshold)
593 if (elog_shrink() < 0)
594 return -1;
596 /* Log a clear event if necessary */
597 if (event_count == 0)
598 elog_add_event_word(ELOG_TYPE_LOG_CLEAR, total_size);
600 #if !defined(__SMM__)
601 /* Log boot count event except in S3 resume */
602 #if CONFIG_ELOG_BOOT_COUNT == 1
603 #if CONFIG_HAVE_ACPI_RESUME == 1
604 if (!acpi_is_wakeup_s3())
605 #endif
606 elog_add_event_dword(ELOG_TYPE_BOOT, boot_count_read());
607 #else
608 /* If boot count is not implemented, fake it. */
609 elog_add_event_dword(ELOG_TYPE_BOOT, 0);
610 #endif
612 #if CONFIG_ARCH_X86
613 /* Check and log POST codes from previous boot */
614 if (CONFIG_CMOS_POST)
615 cmos_post_log();
616 #endif
617 #endif
619 elog_initialized = ELOG_INITIALIZED;
621 return 0;
625 * Populate timestamp in event header with current time
627 static void elog_fill_timestamp(struct event_header *event)
629 #if IS_ENABLED(CONFIG_RTC)
630 struct rtc_time time;
632 rtc_get(&time);
633 event->second = bin2bcd(time.sec);
634 event->minute = bin2bcd(time.min);
635 event->hour = bin2bcd(time.hour);
636 event->day = bin2bcd(time.mday);
637 event->month = bin2bcd(time.mon);
638 event->year = bin2bcd(time.year) & 0xff;
641 /* Basic sanity check of expected ranges */
642 if (event->month > 0x12 || event->day > 0x31 || event->hour > 0x23 ||
643 event->minute > 0x59 || event->second > 0x59)
644 #endif
646 event->year = 0;
647 event->month = 0;
648 event->day = 0;
649 event->hour = 0;
650 event->minute = 0;
651 event->second = 0;
656 * Add an event to the log
658 void elog_add_event_raw(u8 event_type, void *data, u8 data_size)
660 struct event_header *event;
661 u8 event_size;
663 elog_debug("elog_add_event_raw(type=%X)\n", event_type);
665 /* Make sure ELOG structures are initialized */
666 if (elog_init() < 0)
667 return;
669 /* Header + Data + Checksum */
670 event_size = sizeof(*event) + data_size + 1;
671 if (event_size > MAX_EVENT_SIZE) {
672 printk(BIOS_ERR, "ELOG: Event(%X) data size too "
673 "big (%d)\n", event_type, event_size);
674 return;
677 /* Make sure event data can fit */
678 if ((next_event_offset + event_size) >= log_size) {
679 printk(BIOS_ERR, "ELOG: Event(%X) does not fit\n",
680 event_type);
681 return;
684 /* Fill out event data */
685 event = elog_get_event_base(next_event_offset);
686 event->type = event_type;
687 event->length = event_size;
688 elog_fill_timestamp(event);
690 if (data_size)
691 memcpy(&event[1], data, data_size);
693 /* Zero the checksum byte and then compute checksum */
694 elog_update_checksum(event, 0);
695 elog_update_checksum(event, -(elog_checksum_event(event)));
697 /* Update the ELOG state */
698 event_count++;
700 elog_flash_write((void *)event, event_size);
702 next_event_offset += event_size;
704 printk(BIOS_INFO, "ELOG: Event(%X) added with size %d\n",
705 event_type, event_size);
707 /* Shrink the log if we are getting too full */
708 if (next_event_offset >= full_threshold)
709 elog_shrink();
712 void elog_add_event(u8 event_type)
714 elog_add_event_raw(event_type, NULL, 0);
717 void elog_add_event_byte(u8 event_type, u8 data)
719 elog_add_event_raw(event_type, &data, sizeof(data));
722 void elog_add_event_word(u8 event_type, u16 data)
724 elog_add_event_raw(event_type, &data, sizeof(data));
727 void elog_add_event_dword(u8 event_type, u32 data)
729 elog_add_event_raw(event_type, &data, sizeof(data));
732 void elog_add_event_wake(u8 source, u32 instance)
734 struct elog_event_data_wake wake = {
735 .source = source,
736 .instance = instance
738 elog_add_event_raw(ELOG_TYPE_WAKE_SOURCE, &wake, sizeof(wake));