blob: ab255d8d551c8092539d229a2ff837bcaa7a1576 [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
20#include <arch/acpi.h>
Duncan Laurie215f27852012-10-10 14:34:49 -070021#include <cbmem.h>
Duncan Laurie7d2b81c2012-06-23 16:08:47 -070022#include <console/console.h>
23#include <pc80/mc146818rtc.h>
Duncan Laurie472ec9c2012-06-23 16:13:42 -070024#include <smbios.h>
Zheng Bao600784e2013-02-07 17:30:23 +080025#include <spi-generic.h>
Duncan Laurie7d2b81c2012-06-23 16:08:47 -070026#include <spi_flash.h>
27#include <stdint.h>
28#include <string.h>
29#include <elog.h>
30#include "elog_internal.h"
31
Duncan Laurie86bf3f52012-08-15 13:14:58 -070032#if CONFIG_CHROMEOS
33#include <vendorcode/google/chromeos/fmap.h>
34#elif CONFIG_ELOG_FLASH_BASE == 0
Duncan Laurie7d2b81c2012-06-23 16:08:47 -070035#error "CONFIG_ELOG_FLASH_BASE is invalid"
36#endif
37#if CONFIG_ELOG_FULL_THRESHOLD >= CONFIG_ELOG_AREA_SIZE
38#error "CONFIG_ELOG_FULL_THRESHOLD is larger than CONFIG_ELOG_AREA_SIZE"
39#endif
40#if (CONFIG_ELOG_AREA_SIZE - CONFIG_ELOG_FULL_THRESHOLD) < (MAX_EVENT_SIZE + 1)
41#error "CONFIG_ELOG_FULL_THRESHOLD is too small"
42#endif
43#if CONFIG_ELOG_SHRINK_SIZE >= CONFIG_ELOG_AREA_SIZE
44#error "CONFIG_ELOG_SHRINK_SIZE is larger than CONFIG_ELOG_AREA_SIZE"
45#endif
46#if (CONFIG_ELOG_AREA_SIZE - CONFIG_ELOG_SHRINK_SIZE) > \
47 CONFIG_ELOG_FULL_THRESHOLD
48#error "CONFIG_ELOG_SHRINK_SIZE is too large"
49#endif
50
51#if CONFIG_ELOG_DEBUG
52#define elog_debug(STR...) printk(BIOS_DEBUG, STR)
53#else
54#define elog_debug(STR...)
55#endif
56
57/*
58 * Static variables for ELOG state
59 */
60static int elog_initialized;
61static struct spi_flash *elog_spi;
62static struct elog_descriptor elog_flash_area;
63static struct elog_descriptor elog_mem_area;
64
65static inline struct elog_descriptor* elog_get_mem(void)
66{
67 return &elog_mem_area;
68}
69
70static inline struct elog_descriptor* elog_get_flash(void)
71{
72 return &elog_flash_area;
73}
74
75/*
76 * Convert a memory mapped flash address into a flash offset
77 */
78static inline u32 elog_flash_address_to_offset(u8 *address)
79{
80 if (!elog_spi)
81 return 0;
82 return (u32)address - ((u32)~0UL - elog_spi->size + 1);
83}
84
85/*
86 * Convert a flash offset into a memory mapped flash address
87 */
88static inline u8* elog_flash_offset_to_address(u32 offset)
89{
90 if (!elog_spi)
91 return NULL;
92 return (u8*)((u32)~0UL - elog_spi->size + 1 + offset);
93}
94
95/*
96 * The ELOG header is at the very beginning of the area
97 */
98static inline struct elog_header*
99elog_get_header(struct elog_descriptor *elog)
100{
101 return elog->backing_store;
102}
103
104/*
105 * Pointer to an event log header in the event data area
106 */
107static inline struct event_header*
108elog_get_event_base(struct elog_descriptor *elog, u32 offset)
109{
110 return (struct event_header *)&elog->data[offset];
111}
112
113/*
114 * Pointer to where the next event should be stored
115 */
116static inline struct event_header*
117elog_get_next_event_base(struct elog_descriptor *elog)
118{
119 return elog_get_event_base(elog, elog->next_event_offset);
120}
121
122/*
123 * Pointer to the last logged event
124 */
125static inline struct event_header*
126elog_get_last_event_base(struct elog_descriptor *elog)
127{
128 return elog_get_event_base(elog, elog->last_event_offset);
129}
130
131/*
132 * Update the checksum at the last byte
133 */
134static void elog_update_checksum(struct event_header *event, u8 checksum)
135{
136 u8 *event_data = (u8*)event;
137 event_data[event->length - 1] = checksum;
138}
139
140/*
141 * Simple byte checksum for events
142 */
143static u8 elog_checksum_event(struct event_header *event)
144{
145 u8 index, checksum = 0;
146 u8 *data = (u8*)event;
147
148 for (index = 0; index < event->length; index++)
149 checksum += data[index];
150 return checksum;
151}
152
153/*
154 * Check if a raw buffer is filled with ELOG_TYPE_EOL byte
155 */
156static int elog_is_buffer_clear(u8 *base, u32 size)
157{
158 u8 *current = base;
159 u8 *end = current + size;
160
161 elog_debug("elog_is_buffer_clear(base=0x%p size=%u)\n", base, size);
162
163 for (; current != end; current++) {
164 if (*current != ELOG_TYPE_EOL)
165 return 0;
166 }
167 return 1;
168}
169
170/*
171 * Verify whether ELOG area is filled with ELOG_TYPE_EOL byte
172 */
173static int elog_is_area_clear(struct elog_descriptor *elog)
174{
175 return elog_is_buffer_clear(elog->backing_store, elog->total_size);
176}
177
178/*
179 * Check that the ELOG area has been initialized and is valid.
180 */
181static int elog_is_area_valid(struct elog_descriptor *elog)
182{
183 elog_debug("elog_is_area_valid()\n");
184
185 if (elog->area_state != ELOG_AREA_HAS_CONTENT)
186 return 0;
187 if (elog->header_state != ELOG_HEADER_VALID)
188 return 0;
189 if (elog->event_buffer_state != ELOG_EVENT_BUFFER_OK)
190 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 */
223static int elog_is_event_valid(struct elog_descriptor *elog, u32 offset)
224{
225 struct event_header *event;
226
227 event = elog_get_event_base(elog, offset);
228 if (!event)
229 return 0;
230
231 /* Validate event length */
232 if ((offsetof(struct event_header, type) +
233 sizeof(event->type) - 1 + offset) >= elog->data_size)
234 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) +
242 sizeof(event->length) - 1 + offset) >= elog->data_size)
243 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 */
249 if ((event->length + offset) >= elog->data_size)
250 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/*
265 * Write 'size' bytes of data provided in 'buffer' into flash
266 * device at offset 'offset'. This will not erase the flash and
267 * it assumes the flash area is erased appropriately.
268 */
269static void elog_flash_write(u8 *address, u8 *buffer, u32 size)
270{
Duncan Laurie215f27852012-10-10 14:34:49 -0700271 struct elog_descriptor *flash = elog_get_flash();
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700272 u32 offset;
273
274 if (!address || !buffer || !size || !elog_spi)
275 return;
276
Duncan Laurie215f27852012-10-10 14:34:49 -0700277 offset = flash->flash_base;
278 offset += address - (u8*)flash->backing_store;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700279
280 elog_debug("elog_flash_write(address=0x%p offset=0x%08x buffer=0x%p "
281 "size=%u)\n", address, offset, buffer, size);
282
283 /* Write the data to flash */
284 elog_spi->write(elog_spi, offset, size, buffer);
Duncan Laurie215f27852012-10-10 14:34:49 -0700285
286 /* Update the copy in memory */
287 memcpy(address, buffer, size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700288}
289
290/*
291 * Erase the first block specified in the address.
292 * Only handles flash area within a single flash block.
293 */
294static void elog_flash_erase(u8 *address, u32 size)
295{
Duncan Laurie215f27852012-10-10 14:34:49 -0700296 struct elog_descriptor *flash = elog_get_flash();
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700297 u32 offset;
298
299 if (!address || !size || !elog_spi)
300 return;
301
Duncan Laurie215f27852012-10-10 14:34:49 -0700302 offset = flash->flash_base;
303 offset += address - (u8*)flash->backing_store;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700304
305 elog_debug("elog_flash_erase(address=0x%p offset=0x%08x size=%u)\n",
306 address, offset, size);
307
308 /* Erase the sectors in this region */
309 elog_spi->erase(elog_spi, offset, size);
310}
311
312/*
313 * Scan the event area and validate each entry and
314 * update the ELOG descriptor state.
315 */
316static void elog_update_event_buffer_state(struct elog_descriptor *elog)
317{
318 u32 count = 0;
319 u32 offset = 0;
320 u32 last_offset = 0;
321 u32 last_event_size = 0;
322 struct event_header *event;
323
324 elog_debug("elog_update_event_buffer_state()\n");
325
326 /* Go through each event and validate it */
327 while (1) {
328 event = elog_get_event_base(elog, offset);
329
330 /* Do not de-reference anything past the area length */
331 if ((offsetof(struct event_header, type) +
332 sizeof(event->type) - 1 + offset) >= elog->data_size) {
333 elog->event_buffer_state = ELOG_EVENT_BUFFER_CORRUPTED;
334 break;
335 }
336
337 /* The end of the event marker has been found */
338 if (event->type == ELOG_TYPE_EOL)
339 break;
340
341 /* Validate the event */
342 if (!elog_is_event_valid(elog, offset)) {
343 elog->event_buffer_state = ELOG_EVENT_BUFFER_CORRUPTED;
344 break;
345 }
346
347 /* Move to the next event */
348 count++;
349 last_offset = offset;
350 last_event_size = event->length;
351 offset += event->length;
352 }
353
354 /* Ensure the remaining buffer is empty */
355 if (!elog_is_buffer_clear(&elog->data[offset],
356 elog->data_size - offset))
357 elog->event_buffer_state = ELOG_EVENT_BUFFER_CORRUPTED;
358
359 /* Update data into elog descriptor */
360 elog->event_count = count;
361 elog->next_event_offset = offset;
362 elog->last_event_offset = last_offset;
363 elog->last_event_size = last_event_size;
364}
365
366static void elog_validate_and_fill(struct elog_descriptor *elog)
367{
368 elog_debug("elog_validate_and_fill()\n");
369
370 /* Check if the area is empty or not */
371 if (elog_is_area_clear(elog)) {
372 elog->area_state = ELOG_AREA_EMPTY;
373 return;
374 }
375
376 elog->area_state = ELOG_AREA_HAS_CONTENT;
377
378 /* Validate the header */
Gabe Blacke62e0362013-04-23 19:36:01 -0700379 if (!elog_is_header_valid(elog_get_header(elog))) {
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700380 elog->header_state = ELOG_HEADER_INVALID;
381 return;
382 }
383
384 elog->header_state = ELOG_HEADER_VALID;
385 elog_update_event_buffer_state(elog);
386}
387
388/*
389 * Initialize a new ELOG descriptor
390 */
391static void elog_init_descriptor(struct elog_descriptor *elog,
392 elog_descriptor_type type,
Gabe Blacke62e0362013-04-23 19:36:01 -0700393 u8 *buffer, u32 size)
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700394{
395 elog_debug("elog_init_descriptor(type=%u buffer=0x%p size=%u)\n",
396 type, buffer, size);
397
398 elog->type = type;
399 elog->area_state = ELOG_AREA_UNDEFINED;
400 elog->header_state = ELOG_HEADER_INVALID;
401 elog->event_buffer_state = ELOG_EVENT_BUFFER_OK;
402 elog->backing_store = buffer;
403 elog->total_size = size;
404
Duncan Laurie215f27852012-10-10 14:34:49 -0700405 /* Fill memory buffer by reading from SPI */
406 if (type == ELOG_DESCRIPTOR_FLASH)
407 elog_spi->read(elog_spi, elog->flash_base, size, buffer);
408
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700409 /* Data starts immediately after header */
410 elog->data = &buffer[sizeof(struct elog_header)];
411 elog->data_size = size - sizeof(struct elog_header);
412
413 elog->next_event_offset = 0;
414 elog->last_event_offset = 0;
415 elog->last_event_size = 0;
416 elog->event_count = 0;
417
418 elog_validate_and_fill(elog);
419}
420
421/*
422 * Re-initialize an existing ELOG descriptor
423 */
424static void elog_reinit_descriptor(struct elog_descriptor *elog)
425{
426 elog_debug("elog_reinit_descriptor()\n");
427 elog_init_descriptor(elog, elog->type, elog->backing_store,
Gabe Blacke62e0362013-04-23 19:36:01 -0700428 elog->total_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700429}
430
431/*
432 * Create ELOG descriptor data structures for all ELOG areas.
433 */
434static int elog_setup_descriptors(u32 flash_base, u32 area_size)
435{
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700436 u8 *area;
437
438 elog_debug("elog_setup_descriptors(base=0x%08x size=%u)\n",
439 flash_base, area_size);
440
441 /* Prepare flash descriptors */
442 if (flash_base == 0) {
443 printk(BIOS_ERR, "ELOG: Invalid flash base\n");
444 return -1;
445 }
446
Duncan Laurie215f27852012-10-10 14:34:49 -0700447 area = malloc(area_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700448 if (!area) {
Gabe Blacke62e0362013-04-23 19:36:01 -0700449 printk(BIOS_ERR, "ELOG: Unable to allocate backing store\n");
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700450 return -1;
451 }
Duncan Laurie215f27852012-10-10 14:34:49 -0700452 elog_get_flash()->flash_base = flash_base;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700453 elog_init_descriptor(elog_get_flash(), ELOG_DESCRIPTOR_FLASH,
Gabe Blacke62e0362013-04-23 19:36:01 -0700454 area, area_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700455
456 /* Initialize the memory area to look like a cleared flash area */
457 area = malloc(area_size);
458 if (!area) {
459 printk(BIOS_ERR, "ELOG: Unable to allocate mem area\n");
460 return -1;
461 }
462 memset(area, ELOG_TYPE_EOL, area_size);
Gabe Blacke62e0362013-04-23 19:36:01 -0700463 elog_init_descriptor(elog_get_mem(), ELOG_DESCRIPTOR_MEMORY,area, area_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700464
465 return 0;
466}
467
468static void elog_flash_erase_area(void)
469{
470 struct elog_descriptor *elog = elog_get_flash();
471
472 elog_debug("elog_flash_erase_area()\n");
473
474 elog_flash_erase(elog->backing_store, elog->total_size);
Duncan Laurie215f27852012-10-10 14:34:49 -0700475 memset(elog->backing_store, ELOG_TYPE_EOL, elog->total_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700476 elog_reinit_descriptor(elog);
477}
478
479static void elog_prepare_empty(struct elog_descriptor *elog,
480 u8 *data, u32 data_size)
481{
482 struct elog_header *header;
483
484 elog_debug("elog_prepare_empty(%u bytes)\n", data_size);
485
486 if (!elog_is_area_clear(elog))
487 return;
488
489 /* Write out the header */
Gabe Blacke62e0362013-04-23 19:36:01 -0700490 header = elog_get_header(elog);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700491 header->magic = ELOG_SIGNATURE;
492 header->version = ELOG_VERSION;
493 header->header_size = sizeof(struct elog_header);
494 header->reserved[0] = ELOG_TYPE_EOL;
495 header->reserved[1] = ELOG_TYPE_EOL;
496 elog_flash_write(elog->backing_store, (u8*)header,
497 header->header_size);
498
499 /* Write out the data */
500 if (data)
501 elog_flash_write(elog->data, data, data_size);
502
503 elog_reinit_descriptor(elog);
504
505 /* Clear the log if corrupt */
506 if (!elog_is_area_valid(elog))
507 elog_flash_erase_area();
508}
509
510static int elog_sync_flash_to_mem(void)
511{
512 struct elog_descriptor *mem = elog_get_mem();
513 struct elog_descriptor *flash = elog_get_flash();
514
515 elog_debug("elog_sync_flash_to_mem()\n");
516
517 /* Fill with empty pattern first */
518 memset(mem->backing_store, ELOG_TYPE_EOL, mem->total_size);
519
Duncan Laurie215f27852012-10-10 14:34:49 -0700520 /* Read the header from SPI to memory */
521 elog_spi->read(elog_spi, flash->flash_base,
522 sizeof(struct elog_header), mem->backing_store);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700523
Duncan Laurie215f27852012-10-10 14:34:49 -0700524 /* Read the valid flash contents from SPI to memory */
525 elog_spi->read(elog_spi, flash->flash_base + sizeof(struct elog_header),
526 flash->next_event_offset, mem->data);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700527
528 elog_reinit_descriptor(mem);
529
530 return elog_is_area_valid(mem) ? 0 : -1;
531}
532
533static int elog_sync_mem_to_flash(void)
534{
535 struct elog_descriptor *mem = elog_get_mem();
536 struct elog_descriptor *flash = elog_get_flash();
537 u8 *src, *dest;
538 u32 size;
539
540 elog_debug("elog_sync_mem_to_flash()\n");
541
542 /*
543 * In the case of a BIOS flash the active area will be cleared.
544 * One can catch this case and log the proper shutdown event by
545 * checking if the active flash elog is empty. Note that if the
546 * header size changes we will have corrupted the flash area.
547 * However that will be corrected on the next boot.
548 */
549 if (elog_is_area_clear(flash)) {
550 elog_prepare_empty(flash,
551 (u8*)elog_get_last_event_base(mem),
552 mem->last_event_size);
553 elog_sync_flash_to_mem();
554 return 0;
555 }
556
557 /* Calculate the destination and source bases */
558 dest = (u8*)elog_get_next_event_base(flash);
559 src = (u8*)elog_get_event_base(mem, flash->next_event_offset);
560
561 /* Calculate how much data to sync */
562 size = mem->next_event_offset - flash->next_event_offset;
563
564 /* Write the log data */
565 elog_flash_write(dest, src, size);
566
567 /* Update descriptor */
568 flash->event_count = mem->event_count;
569 flash->next_event_offset = mem->next_event_offset;
570 flash->last_event_offset = mem->last_event_offset;
571 flash->last_event_size = mem->last_event_size;
572
573 return 0;
574}
575
576/*
577 * Called during ELOG entry handler to prepare state for flash.
578 */
579static int elog_flash_area_bootstrap(void)
580{
581 struct elog_descriptor *elog = elog_get_flash();
582
583 elog_debug("elog_flash_area_bootstrap()\n");
584
585 switch (elog->area_state) {
586 case ELOG_AREA_UNDEFINED:
587 printk(BIOS_ERR, "ELOG: flash area undefined\n");
588 return -1;
589
590 case ELOG_AREA_EMPTY:
591 /* Write a new header with no data */
592 elog_prepare_empty(elog, NULL, 0);
593 break;
594
595 case ELOG_AREA_HAS_CONTENT:
596 break;
597 }
598
599 if (elog->header_state == ELOG_HEADER_INVALID) {
600 /* If the header is invalid no events can be salvaged
601 * so erase the entire area. */
602 printk(BIOS_ERR, "ELOG: flash area header invalid\n");
603 elog_flash_erase_area();
604 elog_prepare_empty(elog, NULL, 0);
605 }
606
607 if (elog->event_buffer_state == ELOG_EVENT_BUFFER_CORRUPTED) {
608 /* Wipe the source flash area */
609 elog_flash_erase_area();
610 elog_prepare_empty(elog, elog_get_mem()->data,
611 elog_get_mem()->next_event_offset);
612 }
613
614 return 0;
615}
616
617/*
618 * Shrink the log, deleting old entries and moving the
Martin Roth56889792013-07-09 21:39:46 -0600619 * remaining ones to the front of the log.
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700620 */
621static int elog_shrink(void)
622{
623 struct elog_descriptor *mem = elog_get_mem();
Gabe Blackbfae4aa2013-04-24 03:16:21 -0700624 struct elog_descriptor *flash = elog_get_flash();
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700625 struct event_header *event;
626 u16 discard_count = 0;
627 u16 offset = 0;
Gabe Blackbfae4aa2013-04-24 03:16:21 -0700628 u16 new_size = 0;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700629
630 elog_debug("elog_shrink()\n");
631
632 if (mem->next_event_offset < CONFIG_ELOG_SHRINK_SIZE)
633 return 0;
634
635 while (1) {
636 /* Next event has exceeded constraints */
637 if (offset > CONFIG_ELOG_SHRINK_SIZE)
638 break;
639
640 event = elog_get_event_base(mem, offset);
641
642 /* Reached the end of the area */
643 if (!event || event->type == ELOG_TYPE_EOL)
644 break;
645
646 offset += event->length;
647 discard_count++;
648 }
649
Gabe Blackbfae4aa2013-04-24 03:16:21 -0700650 new_size = mem->next_event_offset - offset;
651 memmove(&mem->data[0], &mem->data[offset], new_size);
652 memset(&mem->data[new_size], ELOG_TYPE_EOL, mem->data_size - new_size);
653 elog_reinit_descriptor(mem);
654
655 elog_flash_erase(flash->backing_store, flash->total_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700656
Duncan Laurie032be822013-05-23 07:23:09 -0700657 /* Ensure the area was successfully erased */
Gabe Blackbfae4aa2013-04-24 03:16:21 -0700658 if (mem->next_event_offset >= CONFIG_ELOG_FULL_THRESHOLD) {
Duncan Laurie032be822013-05-23 07:23:09 -0700659 printk(BIOS_ERR, "ELOG: Flash area was not erased!\n");
660 return -1;
661 }
662
Gabe Blackbfae4aa2013-04-24 03:16:21 -0700663 elog_flash_write(flash->backing_store, mem->backing_store,
664 mem->total_size);
665 elog_reinit_descriptor(flash);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700666
667 /* Add clear event */
668 elog_add_event_word(ELOG_TYPE_LOG_CLEAR, offset);
669
670 return 0;
671}
672
673/*
674 * Initialize the SPI bus and probe for a flash chip
675 */
676static int elog_spi_init(void)
677{
678 elog_debug("elog_spi_init()\n");
679
680 /* Prepare SPI subsystem */
681 spi_init();
682
683 /* Look for flash chip */
684 elog_spi = spi_flash_probe(0, 0, 0, 0);
685
686 return elog_spi ? 0 : -1;
687}
688
Duncan Laurie215f27852012-10-10 14:34:49 -0700689#ifndef __SMM__
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700690/*
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700691 * Fill out SMBIOS Type 15 table entry so the
692 * event log can be discovered at runtime.
693 */
694int elog_smbios_write_type15(unsigned long *current, int handle)
695{
Duncan Laurie215f27852012-10-10 14:34:49 -0700696 struct elog_descriptor *flash = elog_get_flash();
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700697 struct smbios_type15 *t = (struct smbios_type15 *)*current;
698 int len = sizeof(struct smbios_type15);
699
Duncan Laurie215f27852012-10-10 14:34:49 -0700700#if CONFIG_ELOG_CBMEM
701 /* Save event log buffer into CBMEM for the OS to read */
702 void *cbmem = cbmem_add(CBMEM_ID_ELOG, flash->total_size);
703 if (!cbmem)
704 return 0;
705 memcpy(cbmem, flash->backing_store, flash->total_size);
706#endif
707
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700708 memset(t, 0, len);
709 t->type = SMBIOS_EVENT_LOG;
710 t->length = len - 2;
711 t->handle = handle;
Duncan Laurie215f27852012-10-10 14:34:49 -0700712 t->area_length = flash->total_size - 1;
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700713 t->header_offset = 0;
714 t->data_offset = sizeof(struct elog_header);
715 t->access_method = SMBIOS_EVENTLOG_ACCESS_METHOD_MMIO32;
716 t->log_status = SMBIOS_EVENTLOG_STATUS_VALID;
717 t->change_token = 0;
Duncan Laurie215f27852012-10-10 14:34:49 -0700718#if CONFIG_ELOG_CBMEM
719 t->address = (u32)cbmem;
720#else
721 t->address = (u32)elog_flash_offset_to_address(flash->flash_base);
722#endif
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700723 t->header_format = ELOG_HEADER_TYPE_OEM;
724 t->log_type_descriptors = 0;
725 t->log_type_descriptor_length = 2;
726
727 *current += len;
728 return len;
729}
Duncan Laurie215f27852012-10-10 14:34:49 -0700730#endif
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700731
732/*
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700733 * Clear the entire event log
734 */
735int elog_clear(void)
736{
737 struct elog_descriptor *flash = elog_get_flash();
738
739 elog_debug("elog_clear()\n");
740
Gabe Black8f4baec2013-04-25 17:21:58 -0700741 /* Make sure ELOG structures are initialized */
742 if (elog_init() < 0)
743 return -1;
744
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700745 /* Erase flash area */
746 elog_flash_erase_area();
747
748 /* Prepare new empty area */
749 elog_prepare_empty(flash, NULL, 0);
750
751 /* Update memory area from flash */
752 if (elog_sync_flash_to_mem() < 0)
753 return -1;
754
755 /* Log the clear event */
756 elog_add_event_word(ELOG_TYPE_LOG_CLEAR, flash->total_size);
757
758 return 0;
759}
760
761/*
762 * Event log main entry point
763 */
764int elog_init(void)
765{
Duncan Laurie86bf3f52012-08-15 13:14:58 -0700766 u32 flash_base = CONFIG_ELOG_FLASH_BASE;
767 int flash_size = CONFIG_ELOG_AREA_SIZE;
768#if CONFIG_CHROMEOS
769 u8 *flash_base_ptr;
770#endif
771
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700772 if (elog_initialized)
773 return 0;
774
775 elog_debug("elog_init()\n");
776
777 /* Find SPI flash chip for backing store */
778 if (elog_spi_init() < 0) {
779 printk(BIOS_ERR, "ELOG: Unable to find SPI flash\n");
780 return -1;
781 }
782
Duncan Laurie86bf3f52012-08-15 13:14:58 -0700783#if CONFIG_CHROMEOS
784 /* Find the ELOG base and size in FMAP */
785 flash_size = find_fmap_entry("RW_ELOG", (void **)&flash_base_ptr);
786 if (flash_size < 0) {
787 printk(BIOS_WARNING, "ELOG: Unable to find RW_ELOG in FMAP, "
788 "using CONFIG_ELOG_FLASH_BASE instead\n");
789 flash_size = CONFIG_ELOG_AREA_SIZE;
790 } else {
791 flash_base = elog_flash_address_to_offset(flash_base_ptr);
792
793 /* Use configured size if smaller than FMAP size */
794 if (flash_size > CONFIG_ELOG_AREA_SIZE)
795 flash_size = CONFIG_ELOG_AREA_SIZE;
796 }
797#endif
798
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700799 /* Setup descriptors for flash and memory areas */
Duncan Laurie86bf3f52012-08-15 13:14:58 -0700800 if (elog_setup_descriptors(flash_base, flash_size) < 0) {
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700801 printk(BIOS_ERR, "ELOG: Unable to initialize descriptors\n");
802 return -1;
803 }
804
805 /* Bootstrap the flash area */
806 if (elog_flash_area_bootstrap() < 0) {
807 printk(BIOS_ERR, "ELOG: Unable to bootstrap flash area\n");
808 return -1;
809 }
810
811 /* Initialize the memory area */
812 if (elog_sync_flash_to_mem() < 0) {
813 printk(BIOS_ERR, "ELOG: Unable to initialize memory area\n");
814 return -1;
815 }
816
817 elog_initialized = 1;
818
Duncan Laurie215f27852012-10-10 14:34:49 -0700819 printk(BIOS_INFO, "ELOG: MEM @0x%p FLASH @0x%p [SPI 0x%08x]\n",
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700820 elog_get_mem()->backing_store,
Duncan Laurie215f27852012-10-10 14:34:49 -0700821 elog_get_flash()->backing_store, elog_get_flash()->flash_base);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700822
823 printk(BIOS_INFO, "ELOG: areas are %d bytes, full threshold %d,"
824 " shrink size %d\n", CONFIG_ELOG_AREA_SIZE,
825 CONFIG_ELOG_FULL_THRESHOLD, CONFIG_ELOG_SHRINK_SIZE);
826
827 /* Log a clear event if necessary */
828 if (elog_get_flash()->event_count == 0)
829 elog_add_event_word(ELOG_TYPE_LOG_CLEAR,
830 elog_get_flash()->total_size);
831
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700832 /* Shrink the log if we are getting too full */
833 if (elog_get_mem()->next_event_offset >= CONFIG_ELOG_FULL_THRESHOLD)
Duncan Laurie032be822013-05-23 07:23:09 -0700834 if (elog_shrink() < 0)
835 return -1;
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700836
Duncan Laurief4d36232012-06-23 16:37:45 -0700837#if CONFIG_ELOG_BOOT_COUNT && !defined(__SMM__)
838 /* Log boot count event except in S3 resume */
839 if (acpi_slp_type != 3)
840 elog_add_event_dword(ELOG_TYPE_BOOT, boot_count_read());
841#endif
842
Duncan Laurie1fc34612012-09-09 19:14:45 -0700843#if CONFIG_CMOS_POST && !defined(__SMM__)
844 /* Check and log POST codes from previous boot */
845 cmos_post_log();
846#endif
847
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700848 return 0;
849}
850
851/*
852 * Populate timestamp in event header with current time
853 */
854static void elog_fill_timestamp(struct event_header *event)
855{
856 event->second = cmos_read(RTC_CLK_SECOND);
857 event->minute = cmos_read(RTC_CLK_MINUTE);
858 event->hour = cmos_read(RTC_CLK_HOUR);
859 event->day = cmos_read(RTC_CLK_DAYOFMONTH);
860 event->month = cmos_read(RTC_CLK_MONTH);
861 event->year = cmos_read(RTC_CLK_YEAR);
862
863 /* Basic sanity check of expected ranges */
864 if (event->month > 0x12 || event->day > 0x31 || event->hour > 0x23 ||
865 event->minute > 0x59 || event->second > 0x59) {
866 event->year = 0;
867 event->month = 0;
868 event->day = 0;
869 event->hour = 0;
870 event->minute = 0;
871 event->second = 0;
872 }
873}
874
875/*
876 * Add an event to the memory area
877 */
878static int elog_add_event_mem(u8 event_type, void *data, u8 data_size)
879{
880 struct event_header *event;
881 struct elog_descriptor *mem = elog_get_mem();
882 u8 event_size;
883
884 elog_debug("elog_add_event_mem(type=%X)\n", event_type);
885
886 /* Make sure ELOG structures are initialized */
887 if (elog_init() < 0)
888 return -1;
889
890 /* Header + Data + Checksum */
891 event_size = sizeof(*event) + data_size + 1;
892 if (event_size > MAX_EVENT_SIZE) {
893 printk(BIOS_ERR, "ELOG: Event(%X) data size too "
894 "big (%d)\n", event_type, event_size);
895 return -1;
896 }
897
898 /* Make sure event data can fit */
899 if ((mem->next_event_offset + event_size) >= mem->data_size) {
900 printk(BIOS_ERR, "ELOG: Event(%X) does not fit\n",
901 event_type);
902 return -1;
903 }
904
905 /* Fill out event data */
906 event = elog_get_next_event_base(mem);
907 event->type = event_type;
908 event->length = event_size;
909 elog_fill_timestamp(event);
910
911 if (data_size)
912 memcpy(&event[1], data, data_size);
913
914 /* Zero the checksum byte and then compute checksum */
915 elog_update_checksum(event, 0);
916 elog_update_checksum(event, -(elog_checksum_event(event)));
917
918 /* Update memory descriptor parameters */
919 mem->event_count++;
920 mem->last_event_offset = mem->next_event_offset;
921 mem->last_event_size = event_size;
922 mem->next_event_offset += event_size;
923
924 printk(BIOS_INFO, "ELOG: Event(%X) added with size %d\n",
925 event_type, event_size);
926 return 0;
927}
928
929void elog_add_event_raw(u8 event_type, void *data, u8 data_size)
930{
931 elog_debug("elog_add_event_raw(type=%X)\n", event_type);
932
933 /* Add event to the memory area */
934 if (elog_add_event_mem(event_type, data, data_size) < 0) {
935 printk(BIOS_ERR, "Unable to add event to memory area\n");
936 return;
937 }
938
939 /* Sync the memory buffer to flash */
940 elog_sync_mem_to_flash();
941
942 /* Shrink the log if we are getting too full */
943 if (elog_get_mem()->next_event_offset >= CONFIG_ELOG_FULL_THRESHOLD)
944 elog_shrink();
945}
946
947void elog_add_event(u8 event_type)
948{
949 elog_add_event_raw(event_type, NULL, 0);
950}
951
952void elog_add_event_byte(u8 event_type, u8 data)
953{
954 elog_add_event_raw(event_type, &data, sizeof(data));
955}
956
957void elog_add_event_word(u8 event_type, u16 data)
958{
959 elog_add_event_raw(event_type, &data, sizeof(data));
960}
961
962void elog_add_event_dword(u8 event_type, u32 data)
963{
964 elog_add_event_raw(event_type, &data, sizeof(data));
965}
966
967void elog_add_event_wake(u8 source, u32 instance)
968{
969 struct elog_event_data_wake wake = {
970 .source = source,
971 .instance = instance
972 };
973 elog_add_event_raw(ELOG_TYPE_WAKE_SOURCE, &wake, sizeof(wake));
974}