blob: 1a794d90ab2a9f3b26543ac429fafec567d8c6d2 [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)) {
213 printk(BIOS_ERR, "ELOG: header size mismatch %u != %u\n",
214 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 */
379 if (!elog_is_header_valid(elog->staging_header)) {
380 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,
393 u8 *buffer, u32 size,
394 struct elog_header *header)
395{
396 elog_debug("elog_init_descriptor(type=%u buffer=0x%p size=%u)\n",
397 type, buffer, size);
398
399 elog->type = type;
400 elog->area_state = ELOG_AREA_UNDEFINED;
401 elog->header_state = ELOG_HEADER_INVALID;
402 elog->event_buffer_state = ELOG_EVENT_BUFFER_OK;
403 elog->backing_store = buffer;
404 elog->total_size = size;
405
Duncan Laurie215f27852012-10-10 14:34:49 -0700406 /* Fill memory buffer by reading from SPI */
407 if (type == ELOG_DESCRIPTOR_FLASH)
408 elog_spi->read(elog_spi, elog->flash_base, size, buffer);
409
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700410 /* Get staging header from backing store */
411 elog->staging_header = header;
412 memcpy(header, buffer, sizeof(struct elog_header));
413
414 /* Data starts immediately after header */
415 elog->data = &buffer[sizeof(struct elog_header)];
416 elog->data_size = size - sizeof(struct elog_header);
417
418 elog->next_event_offset = 0;
419 elog->last_event_offset = 0;
420 elog->last_event_size = 0;
421 elog->event_count = 0;
422
423 elog_validate_and_fill(elog);
424}
425
426/*
427 * Re-initialize an existing ELOG descriptor
428 */
429static void elog_reinit_descriptor(struct elog_descriptor *elog)
430{
431 elog_debug("elog_reinit_descriptor()\n");
432 elog_init_descriptor(elog, elog->type, elog->backing_store,
433 elog->total_size, elog->staging_header);
434}
435
436/*
437 * Create ELOG descriptor data structures for all ELOG areas.
438 */
439static int elog_setup_descriptors(u32 flash_base, u32 area_size)
440{
441 struct elog_header *staging_header;
442 u8 *area;
443
444 elog_debug("elog_setup_descriptors(base=0x%08x size=%u)\n",
445 flash_base, area_size);
446
447 /* Prepare flash descriptors */
448 if (flash_base == 0) {
449 printk(BIOS_ERR, "ELOG: Invalid flash base\n");
450 return -1;
451 }
452
453 staging_header = malloc(sizeof(struct elog_header));
454 if (!staging_header) {
455 printk(BIOS_ERR, "ELOG: Unable to allocate header\n");
456 return -1;
457 }
458
Duncan Laurie215f27852012-10-10 14:34:49 -0700459 area = malloc(area_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700460 if (!area) {
461 printk(BIOS_ERR, "ELOG: Unable to determine flash address\n");
462 return -1;
463 }
Duncan Laurie215f27852012-10-10 14:34:49 -0700464 elog_get_flash()->flash_base = flash_base;
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700465 elog_init_descriptor(elog_get_flash(), ELOG_DESCRIPTOR_FLASH,
466 area, area_size, staging_header);
467
468 /* Initialize the memory area to look like a cleared flash area */
469 area = malloc(area_size);
470 if (!area) {
471 printk(BIOS_ERR, "ELOG: Unable to allocate mem area\n");
472 return -1;
473 }
474 memset(area, ELOG_TYPE_EOL, area_size);
475 elog_init_descriptor(elog_get_mem(), ELOG_DESCRIPTOR_MEMORY,
476 area, area_size, (struct elog_header *)area);
477
478 return 0;
479}
480
481static void elog_flash_erase_area(void)
482{
483 struct elog_descriptor *elog = elog_get_flash();
484
485 elog_debug("elog_flash_erase_area()\n");
486
487 elog_flash_erase(elog->backing_store, elog->total_size);
Duncan Laurie215f27852012-10-10 14:34:49 -0700488 memset(elog->backing_store, ELOG_TYPE_EOL, elog->total_size);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700489 elog_reinit_descriptor(elog);
490}
491
492static void elog_prepare_empty(struct elog_descriptor *elog,
493 u8 *data, u32 data_size)
494{
495 struct elog_header *header;
496
497 elog_debug("elog_prepare_empty(%u bytes)\n", data_size);
498
499 if (!elog_is_area_clear(elog))
500 return;
501
502 /* Write out the header */
503 header = elog->staging_header;
504 header->magic = ELOG_SIGNATURE;
505 header->version = ELOG_VERSION;
506 header->header_size = sizeof(struct elog_header);
507 header->reserved[0] = ELOG_TYPE_EOL;
508 header->reserved[1] = ELOG_TYPE_EOL;
509 elog_flash_write(elog->backing_store, (u8*)header,
510 header->header_size);
511
512 /* Write out the data */
513 if (data)
514 elog_flash_write(elog->data, data, data_size);
515
516 elog_reinit_descriptor(elog);
517
518 /* Clear the log if corrupt */
519 if (!elog_is_area_valid(elog))
520 elog_flash_erase_area();
521}
522
523static int elog_sync_flash_to_mem(void)
524{
525 struct elog_descriptor *mem = elog_get_mem();
526 struct elog_descriptor *flash = elog_get_flash();
527
528 elog_debug("elog_sync_flash_to_mem()\n");
529
530 /* Fill with empty pattern first */
531 memset(mem->backing_store, ELOG_TYPE_EOL, mem->total_size);
532
Duncan Laurie215f27852012-10-10 14:34:49 -0700533 /* Read the header from SPI to memory */
534 elog_spi->read(elog_spi, flash->flash_base,
535 sizeof(struct elog_header), mem->backing_store);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700536
Duncan Laurie215f27852012-10-10 14:34:49 -0700537 /* Read the valid flash contents from SPI to memory */
538 elog_spi->read(elog_spi, flash->flash_base + sizeof(struct elog_header),
539 flash->next_event_offset, mem->data);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700540
541 elog_reinit_descriptor(mem);
542
543 return elog_is_area_valid(mem) ? 0 : -1;
544}
545
546static int elog_sync_mem_to_flash(void)
547{
548 struct elog_descriptor *mem = elog_get_mem();
549 struct elog_descriptor *flash = elog_get_flash();
550 u8 *src, *dest;
551 u32 size;
552
553 elog_debug("elog_sync_mem_to_flash()\n");
554
555 /*
556 * In the case of a BIOS flash the active area will be cleared.
557 * One can catch this case and log the proper shutdown event by
558 * checking if the active flash elog is empty. Note that if the
559 * header size changes we will have corrupted the flash area.
560 * However that will be corrected on the next boot.
561 */
562 if (elog_is_area_clear(flash)) {
563 elog_prepare_empty(flash,
564 (u8*)elog_get_last_event_base(mem),
565 mem->last_event_size);
566 elog_sync_flash_to_mem();
567 return 0;
568 }
569
570 /* Calculate the destination and source bases */
571 dest = (u8*)elog_get_next_event_base(flash);
572 src = (u8*)elog_get_event_base(mem, flash->next_event_offset);
573
574 /* Calculate how much data to sync */
575 size = mem->next_event_offset - flash->next_event_offset;
576
577 /* Write the log data */
578 elog_flash_write(dest, src, size);
579
580 /* Update descriptor */
581 flash->event_count = mem->event_count;
582 flash->next_event_offset = mem->next_event_offset;
583 flash->last_event_offset = mem->last_event_offset;
584 flash->last_event_size = mem->last_event_size;
585
586 return 0;
587}
588
589/*
590 * Called during ELOG entry handler to prepare state for flash.
591 */
592static int elog_flash_area_bootstrap(void)
593{
594 struct elog_descriptor *elog = elog_get_flash();
595
596 elog_debug("elog_flash_area_bootstrap()\n");
597
598 switch (elog->area_state) {
599 case ELOG_AREA_UNDEFINED:
600 printk(BIOS_ERR, "ELOG: flash area undefined\n");
601 return -1;
602
603 case ELOG_AREA_EMPTY:
604 /* Write a new header with no data */
605 elog_prepare_empty(elog, NULL, 0);
606 break;
607
608 case ELOG_AREA_HAS_CONTENT:
609 break;
610 }
611
612 if (elog->header_state == ELOG_HEADER_INVALID) {
613 /* If the header is invalid no events can be salvaged
614 * so erase the entire area. */
615 printk(BIOS_ERR, "ELOG: flash area header invalid\n");
616 elog_flash_erase_area();
617 elog_prepare_empty(elog, NULL, 0);
618 }
619
620 if (elog->event_buffer_state == ELOG_EVENT_BUFFER_CORRUPTED) {
621 /* Wipe the source flash area */
622 elog_flash_erase_area();
623 elog_prepare_empty(elog, elog_get_mem()->data,
624 elog_get_mem()->next_event_offset);
625 }
626
627 return 0;
628}
629
630/*
631 * Shrink the log, deleting old entries and moving the
632 * remining ones to the front of the log.
633 */
634static int elog_shrink(void)
635{
636 struct elog_descriptor *mem = elog_get_mem();
637 struct event_header *event;
638 u16 discard_count = 0;
639 u16 offset = 0;
640
641 elog_debug("elog_shrink()\n");
642
643 if (mem->next_event_offset < CONFIG_ELOG_SHRINK_SIZE)
644 return 0;
645
646 while (1) {
647 /* Next event has exceeded constraints */
648 if (offset > CONFIG_ELOG_SHRINK_SIZE)
649 break;
650
651 event = elog_get_event_base(mem, offset);
652
653 /* Reached the end of the area */
654 if (!event || event->type == ELOG_TYPE_EOL)
655 break;
656
657 offset += event->length;
658 discard_count++;
659 }
660
661 /* Erase flash area */
662 elog_flash_erase_area();
663
664 /* Write new flash area */
665 elog_prepare_empty(elog_get_flash(),
666 (u8*)elog_get_event_base(mem, offset),
667 mem->next_event_offset - offset);
668
669 /* Update memory area from flash */
670 if (elog_sync_flash_to_mem() < 0) {
671 printk(BIOS_ERR, "Unable to update memory area from flash\n");
672 return -1;
673 }
674
675 /* Add clear event */
676 elog_add_event_word(ELOG_TYPE_LOG_CLEAR, offset);
677
678 return 0;
679}
680
681/*
682 * Initialize the SPI bus and probe for a flash chip
683 */
684static int elog_spi_init(void)
685{
686 elog_debug("elog_spi_init()\n");
687
688 /* Prepare SPI subsystem */
689 spi_init();
690
691 /* Look for flash chip */
692 elog_spi = spi_flash_probe(0, 0, 0, 0);
693
694 return elog_spi ? 0 : -1;
695}
696
Duncan Laurie215f27852012-10-10 14:34:49 -0700697#ifndef __SMM__
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700698/*
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700699 * Fill out SMBIOS Type 15 table entry so the
700 * event log can be discovered at runtime.
701 */
702int elog_smbios_write_type15(unsigned long *current, int handle)
703{
Duncan Laurie215f27852012-10-10 14:34:49 -0700704 struct elog_descriptor *flash = elog_get_flash();
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700705 struct smbios_type15 *t = (struct smbios_type15 *)*current;
706 int len = sizeof(struct smbios_type15);
707
Duncan Laurie215f27852012-10-10 14:34:49 -0700708#if CONFIG_ELOG_CBMEM
709 /* Save event log buffer into CBMEM for the OS to read */
710 void *cbmem = cbmem_add(CBMEM_ID_ELOG, flash->total_size);
711 if (!cbmem)
712 return 0;
713 memcpy(cbmem, flash->backing_store, flash->total_size);
714#endif
715
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700716 memset(t, 0, len);
717 t->type = SMBIOS_EVENT_LOG;
718 t->length = len - 2;
719 t->handle = handle;
Duncan Laurie215f27852012-10-10 14:34:49 -0700720 t->area_length = flash->total_size - 1;
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700721 t->header_offset = 0;
722 t->data_offset = sizeof(struct elog_header);
723 t->access_method = SMBIOS_EVENTLOG_ACCESS_METHOD_MMIO32;
724 t->log_status = SMBIOS_EVENTLOG_STATUS_VALID;
725 t->change_token = 0;
Duncan Laurie215f27852012-10-10 14:34:49 -0700726#if CONFIG_ELOG_CBMEM
727 t->address = (u32)cbmem;
728#else
729 t->address = (u32)elog_flash_offset_to_address(flash->flash_base);
730#endif
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700731 t->header_format = ELOG_HEADER_TYPE_OEM;
732 t->log_type_descriptors = 0;
733 t->log_type_descriptor_length = 2;
734
735 *current += len;
736 return len;
737}
Duncan Laurie215f27852012-10-10 14:34:49 -0700738#endif
Duncan Laurie472ec9c2012-06-23 16:13:42 -0700739
740/*
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700741 * Clear the entire event log
742 */
743int elog_clear(void)
744{
745 struct elog_descriptor *flash = elog_get_flash();
746
747 elog_debug("elog_clear()\n");
748
749 /* Erase flash area */
750 elog_flash_erase_area();
751
752 /* Prepare new empty area */
753 elog_prepare_empty(flash, NULL, 0);
754
755 /* Update memory area from flash */
756 if (elog_sync_flash_to_mem() < 0)
757 return -1;
758
759 /* Log the clear event */
760 elog_add_event_word(ELOG_TYPE_LOG_CLEAR, flash->total_size);
761
762 return 0;
763}
764
765/*
766 * Event log main entry point
767 */
768int elog_init(void)
769{
Duncan Laurie86bf3f52012-08-15 13:14:58 -0700770 u32 flash_base = CONFIG_ELOG_FLASH_BASE;
771 int flash_size = CONFIG_ELOG_AREA_SIZE;
772#if CONFIG_CHROMEOS
773 u8 *flash_base_ptr;
774#endif
775
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700776 if (elog_initialized)
777 return 0;
778
779 elog_debug("elog_init()\n");
780
781 /* Find SPI flash chip for backing store */
782 if (elog_spi_init() < 0) {
783 printk(BIOS_ERR, "ELOG: Unable to find SPI flash\n");
784 return -1;
785 }
786
Duncan Laurie86bf3f52012-08-15 13:14:58 -0700787#if CONFIG_CHROMEOS
788 /* Find the ELOG base and size in FMAP */
789 flash_size = find_fmap_entry("RW_ELOG", (void **)&flash_base_ptr);
790 if (flash_size < 0) {
791 printk(BIOS_WARNING, "ELOG: Unable to find RW_ELOG in FMAP, "
792 "using CONFIG_ELOG_FLASH_BASE instead\n");
793 flash_size = CONFIG_ELOG_AREA_SIZE;
794 } else {
795 flash_base = elog_flash_address_to_offset(flash_base_ptr);
796
797 /* Use configured size if smaller than FMAP size */
798 if (flash_size > CONFIG_ELOG_AREA_SIZE)
799 flash_size = CONFIG_ELOG_AREA_SIZE;
800 }
801#endif
802
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700803 /* Setup descriptors for flash and memory areas */
Duncan Laurie86bf3f52012-08-15 13:14:58 -0700804 if (elog_setup_descriptors(flash_base, flash_size) < 0) {
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700805 printk(BIOS_ERR, "ELOG: Unable to initialize descriptors\n");
806 return -1;
807 }
808
809 /* Bootstrap the flash area */
810 if (elog_flash_area_bootstrap() < 0) {
811 printk(BIOS_ERR, "ELOG: Unable to bootstrap flash area\n");
812 return -1;
813 }
814
815 /* Initialize the memory area */
816 if (elog_sync_flash_to_mem() < 0) {
817 printk(BIOS_ERR, "ELOG: Unable to initialize memory area\n");
818 return -1;
819 }
820
821 elog_initialized = 1;
822
Duncan Laurie215f27852012-10-10 14:34:49 -0700823 printk(BIOS_INFO, "ELOG: MEM @0x%p FLASH @0x%p [SPI 0x%08x]\n",
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700824 elog_get_mem()->backing_store,
Duncan Laurie215f27852012-10-10 14:34:49 -0700825 elog_get_flash()->backing_store, elog_get_flash()->flash_base);
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700826
827 printk(BIOS_INFO, "ELOG: areas are %d bytes, full threshold %d,"
828 " shrink size %d\n", CONFIG_ELOG_AREA_SIZE,
829 CONFIG_ELOG_FULL_THRESHOLD, CONFIG_ELOG_SHRINK_SIZE);
830
831 /* Log a clear event if necessary */
832 if (elog_get_flash()->event_count == 0)
833 elog_add_event_word(ELOG_TYPE_LOG_CLEAR,
834 elog_get_flash()->total_size);
835
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700836 /* Shrink the log if we are getting too full */
837 if (elog_get_mem()->next_event_offset >= CONFIG_ELOG_FULL_THRESHOLD)
838 elog_shrink();
839
Duncan Laurief4d36232012-06-23 16:37:45 -0700840#if CONFIG_ELOG_BOOT_COUNT && !defined(__SMM__)
841 /* Log boot count event except in S3 resume */
842 if (acpi_slp_type != 3)
843 elog_add_event_dword(ELOG_TYPE_BOOT, boot_count_read());
844#endif
845
Duncan Laurie1fc34612012-09-09 19:14:45 -0700846#if CONFIG_CMOS_POST && !defined(__SMM__)
847 /* Check and log POST codes from previous boot */
848 cmos_post_log();
849#endif
850
Duncan Laurie7d2b81c2012-06-23 16:08:47 -0700851 return 0;
852}
853
854/*
855 * Populate timestamp in event header with current time
856 */
857static void elog_fill_timestamp(struct event_header *event)
858{
859 event->second = cmos_read(RTC_CLK_SECOND);
860 event->minute = cmos_read(RTC_CLK_MINUTE);
861 event->hour = cmos_read(RTC_CLK_HOUR);
862 event->day = cmos_read(RTC_CLK_DAYOFMONTH);
863 event->month = cmos_read(RTC_CLK_MONTH);
864 event->year = cmos_read(RTC_CLK_YEAR);
865
866 /* Basic sanity check of expected ranges */
867 if (event->month > 0x12 || event->day > 0x31 || event->hour > 0x23 ||
868 event->minute > 0x59 || event->second > 0x59) {
869 event->year = 0;
870 event->month = 0;
871 event->day = 0;
872 event->hour = 0;
873 event->minute = 0;
874 event->second = 0;
875 }
876}
877
878/*
879 * Add an event to the memory area
880 */
881static int elog_add_event_mem(u8 event_type, void *data, u8 data_size)
882{
883 struct event_header *event;
884 struct elog_descriptor *mem = elog_get_mem();
885 u8 event_size;
886
887 elog_debug("elog_add_event_mem(type=%X)\n", event_type);
888
889 /* Make sure ELOG structures are initialized */
890 if (elog_init() < 0)
891 return -1;
892
893 /* Header + Data + Checksum */
894 event_size = sizeof(*event) + data_size + 1;
895 if (event_size > MAX_EVENT_SIZE) {
896 printk(BIOS_ERR, "ELOG: Event(%X) data size too "
897 "big (%d)\n", event_type, event_size);
898 return -1;
899 }
900
901 /* Make sure event data can fit */
902 if ((mem->next_event_offset + event_size) >= mem->data_size) {
903 printk(BIOS_ERR, "ELOG: Event(%X) does not fit\n",
904 event_type);
905 return -1;
906 }
907
908 /* Fill out event data */
909 event = elog_get_next_event_base(mem);
910 event->type = event_type;
911 event->length = event_size;
912 elog_fill_timestamp(event);
913
914 if (data_size)
915 memcpy(&event[1], data, data_size);
916
917 /* Zero the checksum byte and then compute checksum */
918 elog_update_checksum(event, 0);
919 elog_update_checksum(event, -(elog_checksum_event(event)));
920
921 /* Update memory descriptor parameters */
922 mem->event_count++;
923 mem->last_event_offset = mem->next_event_offset;
924 mem->last_event_size = event_size;
925 mem->next_event_offset += event_size;
926
927 printk(BIOS_INFO, "ELOG: Event(%X) added with size %d\n",
928 event_type, event_size);
929 return 0;
930}
931
932void elog_add_event_raw(u8 event_type, void *data, u8 data_size)
933{
934 elog_debug("elog_add_event_raw(type=%X)\n", event_type);
935
936 /* Add event to the memory area */
937 if (elog_add_event_mem(event_type, data, data_size) < 0) {
938 printk(BIOS_ERR, "Unable to add event to memory area\n");
939 return;
940 }
941
942 /* Sync the memory buffer to flash */
943 elog_sync_mem_to_flash();
944
945 /* Shrink the log if we are getting too full */
946 if (elog_get_mem()->next_event_offset >= CONFIG_ELOG_FULL_THRESHOLD)
947 elog_shrink();
948}
949
950void elog_add_event(u8 event_type)
951{
952 elog_add_event_raw(event_type, NULL, 0);
953}
954
955void elog_add_event_byte(u8 event_type, u8 data)
956{
957 elog_add_event_raw(event_type, &data, sizeof(data));
958}
959
960void elog_add_event_word(u8 event_type, u16 data)
961{
962 elog_add_event_raw(event_type, &data, sizeof(data));
963}
964
965void elog_add_event_dword(u8 event_type, u32 data)
966{
967 elog_add_event_raw(event_type, &data, sizeof(data));
968}
969
970void elog_add_event_wake(u8 source, u32 instance)
971{
972 struct elog_event_data_wake wake = {
973 .source = source,
974 .instance = instance
975 };
976 elog_add_event_raw(ELOG_TYPE_WAKE_SOURCE, &wake, sizeof(wake));
977}