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>
20 #include <console/console.h>
22 #include <pc80/mc146818rtc.h>
28 #include <spi-generic.h>
29 #include <spi_flash.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"
41 #define elog_debug(STR...) printk(BIOS_DEBUG, STR)
43 #define elog_debug(STR...)
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
;
66 ELOG_UNINITIALIZED
= 0,
69 } elog_initialized
= ELOG_UNINITIALIZED
;
71 static inline u32
get_rom_size(void)
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
;
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
];
116 * Check if a raw buffer is filled with ELOG_TYPE_EOL byte
118 static int elog_is_buffer_clear(void *base
, u32 size
)
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
)
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
)
141 if (header_state
!= ELOG_HEADER_VALID
)
143 if (event_buffer_state
!= ELOG_EVENT_BUFFER_OK
)
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
);
161 if (header
->version
!= ELOG_VERSION
) {
162 printk(BIOS_ERR
, "ELOG: header version %u != %u\n",
163 header
->version
, ELOG_VERSION
);
166 if (header
->header_size
!= sizeof(*header
)) {
167 printk(BIOS_ERR
, "ELOG: header size mismatch %u != %zu\n",
168 header
->header_size
, sizeof(*header
));
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
);
185 /* Validate event length */
186 if ((offsetof(struct event_header
, type
) +
187 sizeof(event
->type
) - 1 + offset
) >= log_size
)
190 /* End of event marker has been found */
191 if (event
->type
== ELOG_TYPE_EOL
)
194 /* Check if event fits in area */
195 if ((offsetof(struct event_header
, length
) +
196 sizeof(event
->length
) - 1 + offset
) >= log_size
)
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
)
206 /* Event length must be at least header size + checksum */
207 if (event
->length
< (sizeof(*event
) + 1))
210 /* If event checksum is invalid the area is corrupt */
211 if (elog_checksum_event(event
) != 0)
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
)
227 if (!address
|| !size
|| !elog_spi
)
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
)
248 if (!address
|| !size
|| !elog_spi
)
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)
268 struct event_header
*event
;
270 elog_debug("elog_update_event_buffer_state()\n");
272 /* Go through each event and validate it */
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
;
283 /* The end of the event marker has been found */
284 if (event
->type
== ELOG_TYPE_EOL
)
287 /* Validate the event */
288 if (!elog_is_event_valid(offset
)) {
289 event_buffer_state
= ELOG_EVENT_BUFFER_CORRUPTED
;
293 /* Move to the next event */
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 */
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;
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
;
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
;
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
);
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;
368 elog_debug("elog_shrink()\n");
370 if (next_event_offset
< shrink_size
)
374 /* Next event has exceeded constraints */
375 if (offset
> shrink_size
)
378 event
= elog_get_event_base(offset
);
380 /* Reached the end of the area */
381 if (!event
|| event
->type
== ELOG_TYPE_EOL
)
384 offset
+= event
->length
;
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
);
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");
402 /* Add clear event */
403 elog_add_event_word(ELOG_TYPE_LOG_CLEAR
, offset
);
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
)
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
);
440 memcpy(cbmem
, elog_area
, total_size
);
444 t
->type
= SMBIOS_EVENT_LOG
;
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
;
453 #if CONFIG_ELOG_CBMEM
454 t
->address
= (u32
)cbmem
;
456 t
->address
= (u32
)elog_flash_offset_to_address(flash_base
);
458 t
->header_format
= ELOG_HEADER_TYPE_OEM
;
459 t
->log_type_descriptors
= 0;
460 t
->log_type_descriptor_length
= 2;
469 * Clear the entire event log
473 elog_debug("elog_clear()\n");
475 /* Make sure ELOG structures are initialized */
479 /* Erase flash area */
480 elog_flash_erase(elog_area
, total_size
);
481 elog_prepare_empty();
483 if (!elog_is_area_valid())
486 /* Log the clear event */
487 elog_add_event_word(ELOG_TYPE_LOG_CLEAR
, total_size
);
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 */
500 if (fmap_locate_area("RW_ELOG", &r
) < 0) {
502 "ELOG: Unable to find RW_ELOG in FMAP\n");
503 flash_base
= total_size
= 0;
505 flash_base
= region_offset(&r
);
506 total_size
= MIN(region_sz(&r
), CONFIG_ELOG_AREA_SIZE
);
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,
519 * Event log main entry point
523 switch (elog_initialized
) {
524 case ELOG_UNINITIALIZED
:
526 case ELOG_INITIALIZED
:
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);
538 printk(BIOS_ERR
, "ELOG: Unable to find SPI flash\n");
542 /* Set up the backing store */
544 if (flash_base
== 0) {
545 printk(BIOS_ERR
, "ELOG: Invalid flash base\n");
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");
550 } else if (log_size
- shrink_size
>= full_threshold
) {
552 "ELOG: SHRINK_PERCENTAGE set too small for MIN_AVAILABLE_ENTRIES\n");
556 elog_area
= malloc(total_size
);
558 printk(BIOS_ERR
, "ELOG: Unable to allocate backing store\n");
562 /* Load the log from 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");
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)
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())
606 elog_add_event_dword(ELOG_TYPE_BOOT
, boot_count_read());
608 /* If boot count is not implemented, fake it. */
609 elog_add_event_dword(ELOG_TYPE_BOOT
, 0);
613 /* Check and log POST codes from previous boot */
614 if (CONFIG_CMOS_POST
)
619 elog_initialized
= ELOG_INITIALIZED
;
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
;
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)
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
;
663 elog_debug("elog_add_event_raw(type=%X)\n", event_type
);
665 /* Make sure ELOG structures are initialized */
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
);
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",
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
);
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 */
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
)
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
= {
738 elog_add_event_raw(ELOG_TYPE_WAKE_SOURCE
, &wake
, sizeof(wake
));