Ricardo Quesada | d45e70c | 2021-07-16 16:42:59 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: BSD-3-Clause */ |
| 2 | |
Ricardo Quesada | e929a75 | 2021-08-16 10:45:42 -0700 | [diff] [blame] | 3 | #include <commonlib/bsd/bcd.h> |
Ricardo Quesada | d45e70c | 2021-07-16 16:42:59 -0700 | [diff] [blame] | 4 | #include <commonlib/bsd/elog.h> |
| 5 | #include <stddef.h> |
| 6 | |
| 7 | /* |
| 8 | * verify and validate if header is a valid coreboot Event Log header. |
| 9 | * return CB_ERR if invalid, otherwise CB_SUCCESS. |
| 10 | */ |
| 11 | enum cb_err elog_verify_header(const struct elog_header *header) |
| 12 | { |
| 13 | if (header == NULL) |
| 14 | return CB_ERR; |
| 15 | |
| 16 | if (header->magic != ELOG_SIGNATURE) |
| 17 | return CB_ERR; |
| 18 | |
| 19 | if (header->version != ELOG_VERSION) |
| 20 | return CB_ERR; |
| 21 | |
| 22 | if (header->header_size != sizeof(*header)) |
| 23 | return CB_ERR; |
| 24 | |
| 25 | return CB_SUCCESS; |
| 26 | } |
Ricardo Quesada | c2cf394 | 2021-07-16 16:49:04 -0700 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * return the next elog event. |
| 30 | * return NULL if event is invalid. |
| 31 | */ |
| 32 | const struct event_header *elog_get_next_event(const struct event_header *event) |
| 33 | { |
| 34 | if (!event) |
| 35 | return NULL; |
| 36 | |
| 37 | /* Point to next event */ |
| 38 | return (const struct event_header *)((const void *)(event) + event->length); |
| 39 | } |
| 40 | |
| 41 | /* return the data associated to the event_header. */ |
| 42 | const void *event_get_data(const struct event_header *event) |
| 43 | { |
Ricardo Quesada | e929a75 | 2021-08-16 10:45:42 -0700 | [diff] [blame] | 44 | /* |
| 45 | * Pointing to the next event returns the data, since data is the first |
| 46 | * field right after the header. |
| 47 | */ |
Ricardo Quesada | c2cf394 | 2021-07-16 16:49:04 -0700 | [diff] [blame] | 48 | return (const void *)(&event[1]); |
| 49 | } |
Ricardo Quesada | e929a75 | 2021-08-16 10:45:42 -0700 | [diff] [blame] | 50 | |
| 51 | /* Populate timestamp in event header with given time. */ |
| 52 | void elog_fill_timestamp(struct event_header *event, uint8_t sec, uint8_t min, |
Ricardo Quesada | 425fdeb | 2021-09-21 13:55:53 -0700 | [diff] [blame] | 53 | uint8_t hour, uint8_t mday, uint8_t mon, uint16_t year) |
Ricardo Quesada | e929a75 | 2021-08-16 10:45:42 -0700 | [diff] [blame] | 54 | { |
| 55 | event->second = bin2bcd(sec); |
| 56 | event->minute = bin2bcd(min); |
| 57 | event->hour = bin2bcd(hour); |
| 58 | event->day = bin2bcd(mday); |
| 59 | event->month = bin2bcd(mon); |
| 60 | event->year = bin2bcd(year % 100); |
| 61 | |
| 62 | /* Basic check of expected ranges. */ |
| 63 | if (event->month > 0x12 || event->day > 0x31 || event->hour > 0x23 || |
| 64 | event->minute > 0x59 || event->second > 0x59) { |
| 65 | event->year = 0; |
| 66 | event->month = 0; |
| 67 | event->day = 0; |
| 68 | event->hour = 0; |
| 69 | event->minute = 0; |
| 70 | event->second = 0; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void elog_update_checksum(struct event_header *event, uint8_t checksum) |
| 75 | { |
| 76 | uint8_t *event_data = (uint8_t *)event; |
| 77 | event_data[event->length - 1] = checksum; |
| 78 | } |
| 79 | |
| 80 | uint8_t elog_checksum_event(const struct event_header *event) |
| 81 | { |
| 82 | uint8_t index, checksum = 0; |
| 83 | const uint8_t *data = (const uint8_t *)event; |
| 84 | |
| 85 | for (index = 0; index < event->length; index++) |
| 86 | checksum += data[index]; |
| 87 | return checksum; |
| 88 | } |