blob: 76e8cf71b83b7e0e7457f1b3835a5568167cca10 [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
Stefan Reinauer45a225b2015-03-16 16:47:39 -070040#if !IS_ENABLED(CONFIG_CHROMEOS) && 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 struct spi_flash *elog_spi;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -070079
Julius Werner5d686222015-01-27 15:40:47 -080080static enum {
81 ELOG_UNINITIALIZED = 0,
82 ELOG_INITIALIZED,
83 ELOG_BROKEN,
84} elog_initialized = ELOG_UNINITIALIZED;
Aaron Durbin10a070b2013-06-28 12:30:53 -070085
86static inline u32 get_rom_size(void)
87{
88 u32 rom_size;
89
90 /* Assume the used space of the ROM image starts from 0. The
91 * physical size of the device may not be completely used. */
92 rom_size = elog_spi->size;
93 if (rom_size > CONFIG_ROM_SIZE)
94 rom_size = CONFIG_ROM_SIZE;
95
96 return rom_size;
97}
98
Duncan Laurie7d2b81c2012-06-23 16:08:47 -070099/*
100 * Convert a memory mapped flash address into a flash offset
101 */
102static inline u32 elog_flash_address_to_offset(u8 *address)
103{
David Hendricks259e49a2014-03-20 20:31:23 -0700104#if CONFIG_ARCH_X86
105 /* For x86, assume address is memory-mapped near 4GB */
Aaron Durbin10a070b2013-06-28 12:30:53 -0700106 u32 rom_size;
107
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700108 if (!elog_spi)
109 return 0;
Aaron Durbin10a070b2013-06-28 12:30:53 -0700110
111 rom_size = get_rom_size();
112
113 return (u32)address - ((u32)~0UL - rom_size + 1);
David Hendricks259e49a2014-03-20 20:31:23 -0700114#else
Furquan Shaikhfd2f0302014-11-12 16:19:37 -0800115 return (u32)(uintptr_t)address;
David Hendricks259e49a2014-03-20 20:31:23 -0700116#endif
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700117}
118
119/*
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700120 * Pointer to an event log header in the event data area
121 */
122static inline struct event_header*
Gabe Black0bf1feb2013-04-26 03:34:00 -0700123elog_get_event_base(u32 offset)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700124{
Gabe Black0bf1feb2013-04-26 03:34:00 -0700125 return (struct event_header *)&elog_area->data[offset];
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700126}
127
128/*
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700129 * Update the checksum at the last byte
130 */
131static void elog_update_checksum(struct event_header *event, u8 checksum)
132{
133 u8 *event_data = (u8*)event;
134 event_data[event->length - 1] = checksum;
135}
136
137/*
138 * Simple byte checksum for events
139 */
140static u8 elog_checksum_event(struct event_header *event)
141{
142 u8 index, checksum = 0;
143 u8 *data = (u8*)event;
144
145 for (index = 0; index < event->length; index++)
146 checksum += data[index];
147 return checksum;
148}
149
150/*
151 * Check if a raw buffer is filled with ELOG_TYPE_EOL byte
152 */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700153static int elog_is_buffer_clear(void *base, u32 size)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700154{
155 u8 *current = base;
156 u8 *end = current + size;
157
158 elog_debug("elog_is_buffer_clear(base=0x%p size=%u)\n", base, size);
159
160 for (; current != end; current++) {
161 if (*current != ELOG_TYPE_EOL)
162 return 0;
163 }
164 return 1;
165}
166
167/*
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700168 * Check that the ELOG area has been initialized and is valid.
169 */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700170static int elog_is_area_valid(void)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700171{
172 elog_debug("elog_is_area_valid()\n");
173
Gabe Black0bf1feb2013-04-26 03:34:00 -0700174 if (area_state != ELOG_AREA_HAS_CONTENT)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700175 return 0;
Gabe Black0bf1feb2013-04-26 03:34:00 -0700176 if (header_state != ELOG_HEADER_VALID)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700177 return 0;
Gabe Black0bf1feb2013-04-26 03:34:00 -0700178 if (event_buffer_state != ELOG_EVENT_BUFFER_OK)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700179 return 0;
180 return 1;
181}
182
183/*
184 * Verify the contents of an ELOG Header structure
185 * Returns 1 if the header is valid, 0 otherwise
186 */
187static int elog_is_header_valid(struct elog_header *header)
188{
189 elog_debug("elog_is_header_valid()\n");
190
191 if (header->magic != ELOG_SIGNATURE) {
192 printk(BIOS_ERR, "ELOG: header magic 0x%X != 0x%X\n",
193 header->magic, ELOG_SIGNATURE);
194 return 0;
195 }
196 if (header->version != ELOG_VERSION) {
197 printk(BIOS_ERR, "ELOG: header version %u != %u\n",
198 header->version, ELOG_VERSION);
199 return 0;
200 }
201 if (header->header_size != sizeof(*header)) {
Denis 'GNUtoo' Cariklibc2c9ef2013-06-26 20:04:49 +0200202 printk(BIOS_ERR, "ELOG: header size mismatch %u != %zu\n",
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700203 header->header_size, sizeof(*header));
204 return 0;
205 }
206 return 1;
207}
208
209/*
210 * Validate the event header and data.
211 */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700212static int elog_is_event_valid(u32 offset)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700213{
214 struct event_header *event;
215
Gabe Black0bf1feb2013-04-26 03:34:00 -0700216 event = elog_get_event_base(offset);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700217 if (!event)
218 return 0;
219
220 /* Validate event length */
221 if ((offsetof(struct event_header, type) +
Gabe Black0bf1feb2013-04-26 03:34:00 -0700222 sizeof(event->type) - 1 + offset) >= log_size)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700223 return 0;
224
225 /* End of event marker has been found */
226 if (event->type == ELOG_TYPE_EOL)
227 return 0;
228
229 /* Check if event fits in area */
230 if ((offsetof(struct event_header, length) +
Gabe Black0bf1feb2013-04-26 03:34:00 -0700231 sizeof(event->length) - 1 + offset) >= log_size)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700232 return 0;
233
234 /*
235 * If the current event length + the current offset exceeds
236 * the area size then the event area is corrupt.
237 */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700238 if ((event->length + offset) >= log_size)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700239 return 0;
240
241 /* Event length must be at least header size + checksum */
242 if (event->length < (sizeof(*event) + 1))
243 return 0;
244
245 /* If event checksum is invalid the area is corrupt */
246 if (elog_checksum_event(event) != 0)
247 return 0;
248
249 /* Event is valid */
250 return 1;
251}
252
253/*
Gabe Black331eb082013-04-24 04:11:40 -0700254 * Write 'size' bytes of data pointed to by 'address' in the flash backing
255 * store into flash. This will not erase the flash and it assumes the flash
256 * area has been erased appropriately.
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700257 */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700258static void elog_flash_write(void *address, u32 size)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700259{
260 u32 offset;
261
Gabe Black331eb082013-04-24 04:11:40 -0700262 if (!address || !size || !elog_spi)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700263 return;
264
Gabe Black0bf1feb2013-04-26 03:34:00 -0700265 offset = flash_base;
266 offset += (u8 *)address - (u8 *)elog_area;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700267
Gabe Black331eb082013-04-24 04:11:40 -0700268 elog_debug("elog_flash_write(address=0x%p offset=0x%08x size=%u)\n",
269 address, offset, size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700270
271 /* Write the data to flash */
Gabe Black331eb082013-04-24 04:11:40 -0700272 elog_spi->write(elog_spi, offset, size, address);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700273}
274
275/*
276 * Erase the first block specified in the address.
277 * Only handles flash area within a single flash block.
278 */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700279static void elog_flash_erase(void *address, u32 size)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700280{
281 u32 offset;
282
283 if (!address || !size || !elog_spi)
284 return;
285
Gabe Black0bf1feb2013-04-26 03:34:00 -0700286 offset = flash_base;
287 offset += (u8 *)address - (u8*)elog_area;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700288
289 elog_debug("elog_flash_erase(address=0x%p offset=0x%08x size=%u)\n",
290 address, offset, size);
291
292 /* Erase the sectors in this region */
293 elog_spi->erase(elog_spi, offset, size);
294}
295
296/*
Gabe Black0bf1feb2013-04-26 03:34:00 -0700297 * Scan the event area and validate each entry and update the ELOG state.
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700298 */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700299static void elog_update_event_buffer_state(void)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700300{
301 u32 count = 0;
302 u32 offset = 0;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700303 struct event_header *event;
304
305 elog_debug("elog_update_event_buffer_state()\n");
306
307 /* Go through each event and validate it */
308 while (1) {
Gabe Black0bf1feb2013-04-26 03:34:00 -0700309 event = elog_get_event_base(offset);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700310
311 /* Do not de-reference anything past the area length */
312 if ((offsetof(struct event_header, type) +
Gabe Black0bf1feb2013-04-26 03:34:00 -0700313 sizeof(event->type) - 1 + offset) >= log_size) {
314 event_buffer_state = ELOG_EVENT_BUFFER_CORRUPTED;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700315 break;
316 }
317
318 /* The end of the event marker has been found */
319 if (event->type == ELOG_TYPE_EOL)
320 break;
321
322 /* Validate the event */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700323 if (!elog_is_event_valid(offset)) {
324 event_buffer_state = ELOG_EVENT_BUFFER_CORRUPTED;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700325 break;
326 }
327
328 /* Move to the next event */
329 count++;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700330 offset += event->length;
331 }
332
333 /* Ensure the remaining buffer is empty */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700334 if (!elog_is_buffer_clear(&elog_area->data[offset], log_size - offset))
335 event_buffer_state = ELOG_EVENT_BUFFER_CORRUPTED;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700336
Gabe Black0bf1feb2013-04-26 03:34:00 -0700337 /* Update ELOG state */
338 event_count = count;
339 next_event_offset = offset;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700340}
341
Gabe Black0bf1feb2013-04-26 03:34:00 -0700342static void elog_scan_flash(void)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700343{
Gabe Black84a93d12013-04-24 23:31:41 -0700344 elog_debug("elog_scan_flash()\n");
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700345
Gabe Black0bf1feb2013-04-26 03:34:00 -0700346 area_state = ELOG_AREA_UNDEFINED;
347 header_state = ELOG_HEADER_INVALID;
348 event_buffer_state = ELOG_EVENT_BUFFER_OK;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700349
Duncan Laurie215f27852012-10-10 14:34:49 -0700350 /* Fill memory buffer by reading from SPI */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700351 elog_spi->read(elog_spi, flash_base, total_size, elog_area);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700352
Gabe Black0bf1feb2013-04-26 03:34:00 -0700353 next_event_offset = 0;
354 event_count = 0;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700355
Gabe Black455c68e2013-04-24 17:54:37 -0700356 /* Check if the area is empty or not */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700357 if (elog_is_buffer_clear(elog_area, total_size)) {
358 area_state = ELOG_AREA_EMPTY;
Gabe Black455c68e2013-04-24 17:54:37 -0700359 return;
360 }
361
Gabe Black0bf1feb2013-04-26 03:34:00 -0700362 area_state = ELOG_AREA_HAS_CONTENT;
Gabe Black455c68e2013-04-24 17:54:37 -0700363
364 /* Validate the header */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700365 if (!elog_is_header_valid(&elog_area->header)) {
366 header_state = ELOG_HEADER_INVALID;
Gabe Black455c68e2013-04-24 17:54:37 -0700367 return;
368 }
369
Gabe Black0bf1feb2013-04-26 03:34:00 -0700370 header_state = ELOG_HEADER_VALID;
371 elog_update_event_buffer_state();
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700372}
373
Gabe Black0bf1feb2013-04-26 03:34:00 -0700374static void elog_prepare_empty(void)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700375{
376 struct elog_header *header;
377
Gabe Black0bf1feb2013-04-26 03:34:00 -0700378 elog_debug("elog_prepare_empty()\n");
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700379
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700380 /* Write out the header */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700381 header = &elog_area->header;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700382 header->magic = ELOG_SIGNATURE;
383 header->version = ELOG_VERSION;
384 header->header_size = sizeof(struct elog_header);
385 header->reserved[0] = ELOG_TYPE_EOL;
386 header->reserved[1] = ELOG_TYPE_EOL;
Gabe Black0bf1feb2013-04-26 03:34:00 -0700387 elog_flash_write(elog_area, header->header_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700388
Gabe Black0bf1feb2013-04-26 03:34:00 -0700389 elog_scan_flash();
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700390}
391
392/*
393 * Shrink the log, deleting old entries and moving the
Martin Roth56889792013-07-09 21:39:46 -0600394 * remaining ones to the front of the log.
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700395 */
396static int elog_shrink(void)
397{
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700398 struct event_header *event;
399 u16 discard_count = 0;
400 u16 offset = 0;
Gabe Blackbfae4aa2013-04-24 03:16:21 -0700401 u16 new_size = 0;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700402
403 elog_debug("elog_shrink()\n");
404
Gabe Black0bf1feb2013-04-26 03:34:00 -0700405 if (next_event_offset < CONFIG_ELOG_SHRINK_SIZE)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700406 return 0;
407
408 while (1) {
409 /* Next event has exceeded constraints */
410 if (offset > CONFIG_ELOG_SHRINK_SIZE)
411 break;
412
Gabe Black0bf1feb2013-04-26 03:34:00 -0700413 event = elog_get_event_base(offset);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700414
415 /* Reached the end of the area */
416 if (!event || event->type == ELOG_TYPE_EOL)
417 break;
418
419 offset += event->length;
420 discard_count++;
421 }
422
Gabe Black0bf1feb2013-04-26 03:34:00 -0700423 new_size = next_event_offset - offset;
424 memmove(&elog_area->data[0], &elog_area->data[offset], new_size);
425 memset(&elog_area->data[new_size], ELOG_TYPE_EOL, log_size - new_size);
Gabe Blackbfae4aa2013-04-24 03:16:21 -0700426
Gabe Black0bf1feb2013-04-26 03:34:00 -0700427 elog_flash_erase(elog_area, total_size);
428 elog_flash_write(elog_area, total_size);
429 elog_scan_flash();
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700430
Duncan Laurie032be822013-05-23 07:23:09 -0700431 /* Ensure the area was successfully erased */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700432 if (next_event_offset >= CONFIG_ELOG_FULL_THRESHOLD) {
Duncan Laurie032be822013-05-23 07:23:09 -0700433 printk(BIOS_ERR, "ELOG: Flash area was not erased!\n");
434 return -1;
435 }
436
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700437 /* Add clear event */
438 elog_add_event_word(ELOG_TYPE_LOG_CLEAR, offset);
439
440 return 0;
441}
442
Duncan Laurie215f27852012-10-10 14:34:49 -0700443#ifndef __SMM__
Furquan Shaikh7f4221c2014-11-12 16:19:37 -0800444#if IS_ENABLED(CONFIG_ARCH_X86)
445
446/*
447 * Convert a flash offset into a memory mapped flash address
448 */
449static inline u8 *elog_flash_offset_to_address(u32 offset)
450{
451 u32 rom_size;
452
453 if (!elog_spi)
454 return NULL;
455
456 rom_size = get_rom_size();
457
458 return (u8 *)((u32)~0UL - rom_size + 1 + offset);
459}
460
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700461/*
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700462 * Fill out SMBIOS Type 15 table entry so the
463 * event log can be discovered at runtime.
464 */
465int elog_smbios_write_type15(unsigned long *current, int handle)
466{
467 struct smbios_type15 *t = (struct smbios_type15 *)*current;
468 int len = sizeof(struct smbios_type15);
469
Duncan Laurie215f27852012-10-10 14:34:49 -0700470#if CONFIG_ELOG_CBMEM
471 /* Save event log buffer into CBMEM for the OS to read */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700472 void *cbmem = cbmem_add(CBMEM_ID_ELOG, total_size);
Duncan Laurie215f27852012-10-10 14:34:49 -0700473 if (!cbmem)
474 return 0;
Gabe Black0bf1feb2013-04-26 03:34:00 -0700475 memcpy(cbmem, elog_area, total_size);
Duncan Laurie215f27852012-10-10 14:34:49 -0700476#endif
477
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700478 memset(t, 0, len);
479 t->type = SMBIOS_EVENT_LOG;
480 t->length = len - 2;
481 t->handle = handle;
Gabe Black0bf1feb2013-04-26 03:34:00 -0700482 t->area_length = total_size - 1;
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700483 t->header_offset = 0;
484 t->data_offset = sizeof(struct elog_header);
485 t->access_method = SMBIOS_EVENTLOG_ACCESS_METHOD_MMIO32;
486 t->log_status = SMBIOS_EVENTLOG_STATUS_VALID;
487 t->change_token = 0;
Duncan Laurie215f27852012-10-10 14:34:49 -0700488#if CONFIG_ELOG_CBMEM
489 t->address = (u32)cbmem;
490#else
Gabe Black0bf1feb2013-04-26 03:34:00 -0700491 t->address = (u32)elog_flash_offset_to_address(flash_base);
Duncan Laurie215f27852012-10-10 14:34:49 -0700492#endif
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700493 t->header_format = ELOG_HEADER_TYPE_OEM;
494 t->log_type_descriptors = 0;
495 t->log_type_descriptor_length = 2;
496
497 *current += len;
498 return len;
499}
Duncan Laurie215f27852012-10-10 14:34:49 -0700500#endif
Furquan Shaikh7f4221c2014-11-12 16:19:37 -0800501#endif
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700502
503/*
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700504 * Clear the entire event log
505 */
506int elog_clear(void)
507{
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700508 elog_debug("elog_clear()\n");
509
Gabe Black8f4baec2013-04-25 17:21:58 -0700510 /* Make sure ELOG structures are initialized */
511 if (elog_init() < 0)
512 return -1;
513
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700514 /* Erase flash area */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700515 elog_flash_erase(elog_area, total_size);
516 elog_prepare_empty();
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700517
Gabe Black0bf1feb2013-04-26 03:34:00 -0700518 if (!elog_is_area_valid())
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700519 return -1;
520
521 /* Log the clear event */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700522 elog_add_event_word(ELOG_TYPE_LOG_CLEAR, total_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700523
524 return 0;
525}
526
Gabe Black0bf1feb2013-04-26 03:34:00 -0700527static void elog_find_flash(void)
Gabe Black84a93d12013-04-24 23:31:41 -0700528{
Gabe Black0bf1feb2013-04-26 03:34:00 -0700529 elog_debug("elog_find_flash()\n");
Gabe Black84a93d12013-04-24 23:31:41 -0700530
531#if CONFIG_CHROMEOS
532 /* Find the ELOG base and size in FMAP */
Julius Werner5d686222015-01-27 15:40:47 -0800533 u8 *flash_base_ptr;
534 int fmap_size = find_fmap_entry("RW_ELOG", (void **)&flash_base_ptr);
535 if (fmap_size < 0) {
536 printk(BIOS_WARNING, "ELOG: Unable to find RW_ELOG in FMAP\n");
537 flash_base = total_size = 0;
Gabe Black84a93d12013-04-24 23:31:41 -0700538 } else {
Gabe Black0bf1feb2013-04-26 03:34:00 -0700539 flash_base = elog_flash_address_to_offset(flash_base_ptr);
Julius Werner5d686222015-01-27 15:40:47 -0800540 total_size = MIN(fmap_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{
Julius Werner5d686222015-01-27 15:40:47 -0800554 switch (elog_initialized) {
555 case ELOG_UNINITIALIZED:
556 break;
557 case ELOG_INITIALIZED:
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700558 return 0;
Julius Werner5d686222015-01-27 15:40:47 -0800559 case ELOG_BROKEN:
560 return -1;
561 }
562 elog_initialized = ELOG_BROKEN;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700563
564 elog_debug("elog_init()\n");
565
David Hendricks9acbd6f2014-04-13 16:45:31 -0700566 /* Probe SPI chip. SPI controller must already be initialized. */
David Hendricksc9470242014-04-14 14:57:36 -0700567 elog_spi = spi_flash_probe(CONFIG_BOOT_MEDIA_SPI_BUS, 0);
Gabe Black84a93d12013-04-24 23:31:41 -0700568 if (!elog_spi) {
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700569 printk(BIOS_ERR, "ELOG: Unable to find SPI flash\n");
570 return -1;
571 }
572
Gabe Black84a93d12013-04-24 23:31:41 -0700573 /* Set up the backing store */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700574 elog_find_flash();
Gabe Black84a93d12013-04-24 23:31:41 -0700575 if (flash_base == 0) {
576 printk(BIOS_ERR, "ELOG: Invalid flash base\n");
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700577 return -1;
578 }
579
Gabe Black0bf1feb2013-04-26 03:34:00 -0700580 elog_area = malloc(total_size);
581 if (!elog_area) {
Gabe Black84a93d12013-04-24 23:31:41 -0700582 printk(BIOS_ERR, "ELOG: Unable to allocate backing store\n");
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700583 return -1;
584 }
Gabe Black84a93d12013-04-24 23:31:41 -0700585
586 /* Load the log from flash */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700587 elog_scan_flash();
Gabe Black84a93d12013-04-24 23:31:41 -0700588
589 /* Prepare the flash if necessary */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700590 if (header_state == ELOG_HEADER_INVALID ||
591 event_buffer_state == ELOG_EVENT_BUFFER_CORRUPTED) {
Gabe Black84a93d12013-04-24 23:31:41 -0700592 /* If the header is invalid or the events are corrupted,
593 * no events can be salvaged so erase the entire area. */
594 printk(BIOS_ERR, "ELOG: flash area invalid\n");
Gabe Black0bf1feb2013-04-26 03:34:00 -0700595 elog_flash_erase(elog_area, total_size);
596 elog_prepare_empty();
Gabe Black84a93d12013-04-24 23:31:41 -0700597 }
598
Gabe Black0bf1feb2013-04-26 03:34:00 -0700599 if (area_state == ELOG_AREA_EMPTY)
600 elog_prepare_empty();
Gabe Black84a93d12013-04-24 23:31:41 -0700601
Gabe Black0bf1feb2013-04-26 03:34:00 -0700602 if (!elog_is_area_valid()) {
Gabe Black84a93d12013-04-24 23:31:41 -0700603 printk(BIOS_ERR, "ELOG: Unable to prepare flash\n");
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700604 return -1;
605 }
606
Gabe Black331eb082013-04-24 04:11:40 -0700607 printk(BIOS_INFO, "ELOG: FLASH @0x%p [SPI 0x%08x]\n",
Gabe Black0bf1feb2013-04-26 03:34:00 -0700608 elog_area, flash_base);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700609
Gabe Black84a93d12013-04-24 23:31:41 -0700610 printk(BIOS_INFO, "ELOG: area is %d bytes, full threshold %d,"
Gabe Black0bf1feb2013-04-26 03:34:00 -0700611 " shrink size %d\n", total_size,
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700612 CONFIG_ELOG_FULL_THRESHOLD, CONFIG_ELOG_SHRINK_SIZE);
613
614 /* Log a clear event if necessary */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700615 if (event_count == 0)
616 elog_add_event_word(ELOG_TYPE_LOG_CLEAR, total_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700617
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700618 /* Shrink the log if we are getting too full */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700619 if (next_event_offset >= CONFIG_ELOG_FULL_THRESHOLD)
Duncan Laurie032be822013-05-23 07:23:09 -0700620 if (elog_shrink() < 0)
621 return -1;
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700622
Gabe Black84a93d12013-04-24 23:31:41 -0700623#if !defined(__SMM__)
Duncan Laurief4d36232012-06-23 16:37:45 -0700624 /* Log boot count event except in S3 resume */
David Hendricks259e49a2014-03-20 20:31:23 -0700625#if CONFIG_ELOG_BOOT_COUNT == 1
626#if CONFIG_HAVE_ACPI_RESUME == 1
627 if (!acpi_is_wakeup_s3())
628#endif
Duncan Laurief4d36232012-06-23 16:37:45 -0700629 elog_add_event_dword(ELOG_TYPE_BOOT, boot_count_read());
David Hendricks6a29e6c2014-06-18 13:03:03 -0700630#else
631 /* If boot count is not implemented, fake it. */
632 elog_add_event_dword(ELOG_TYPE_BOOT, 0);
David Hendricks259e49a2014-03-20 20:31:23 -0700633#endif
Duncan Laurief4d36232012-06-23 16:37:45 -0700634
David Hendricks259e49a2014-03-20 20:31:23 -0700635#if CONFIG_ARCH_X86
Duncan Laurie1fc34612012-09-09 19:14:45 -0700636 /* Check and log POST codes from previous boot */
Gabe Black84a93d12013-04-24 23:31:41 -0700637 if (CONFIG_CMOS_POST)
638 cmos_post_log();
Duncan Laurie1fc34612012-09-09 19:14:45 -0700639#endif
David Hendricks259e49a2014-03-20 20:31:23 -0700640#endif
Duncan Laurie1fc34612012-09-09 19:14:45 -0700641
Julius Werner5d686222015-01-27 15:40:47 -0800642 elog_initialized = ELOG_INITIALIZED;
643
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700644 return 0;
645}
646
647/*
648 * Populate timestamp in event header with current time
649 */
650static void elog_fill_timestamp(struct event_header *event)
651{
Gabe Blacka4c4b1c2014-04-30 21:41:23 -0700652 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 Laurie7d2b81c2012-06-23 16:08:47 -0700661
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 Black331eb082013-04-24 04:11:40 -0700675 * Add an event to the log
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700676 */
Gabe Black331eb082013-04-24 04:11:40 -0700677void elog_add_event_raw(u8 event_type, void *data, u8 data_size)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700678{
679 struct event_header *event;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700680 u8 event_size;
681
Gabe Black331eb082013-04-24 04:11:40 -0700682 elog_debug("elog_add_event_raw(type=%X)\n", event_type);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700683
684 /* Make sure ELOG structures are initialized */
685 if (elog_init() < 0)
Gabe Black331eb082013-04-24 04:11:40 -0700686 return;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700687
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 Black331eb082013-04-24 04:11:40 -0700693 return;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700694 }
695
696 /* Make sure event data can fit */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700697 if ((next_event_offset + event_size) >= log_size) {
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700698 printk(BIOS_ERR, "ELOG: Event(%X) does not fit\n",
699 event_type);
Gabe Black331eb082013-04-24 04:11:40 -0700700 return;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700701 }
702
703 /* Fill out event data */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700704 event = elog_get_event_base(next_event_offset);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700705 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 Black0bf1feb2013-04-26 03:34:00 -0700716 /* Update the ELOG state */
717 event_count++;
Gabe Black331eb082013-04-24 04:11:40 -0700718
719 elog_flash_write((void *)event, event_size);
720
Gabe Black0bf1feb2013-04-26 03:34:00 -0700721 next_event_offset += event_size;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700722
723 printk(BIOS_INFO, "ELOG: Event(%X) added with size %d\n",
724 event_type, event_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700725
726 /* Shrink the log if we are getting too full */
Gabe Black0bf1feb2013-04-26 03:34:00 -0700727 if (next_event_offset >= CONFIG_ELOG_FULL_THRESHOLD)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700728 elog_shrink();
729}
730
731void elog_add_event(u8 event_type)
732{
733 elog_add_event_raw(event_type, NULL, 0);
734}
735
736void elog_add_event_byte(u8 event_type, u8 data)
737{
738 elog_add_event_raw(event_type, &data, sizeof(data));
739}
740
741void elog_add_event_word(u8 event_type, u16 data)
742{
743 elog_add_event_raw(event_type, &data, sizeof(data));
744}
745
746void elog_add_event_dword(u8 event_type, u32 data)
747{
748 elog_add_event_raw(event_type, &data, sizeof(data));
749}
750
751void 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}