Harrison Mutai | b674809 | 2025-04-25 16:03:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020-2025, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #ifndef EVENT_LOG_H |
| 8 | #define EVENT_LOG_H |
| 9 | |
| 10 | #include <stddef.h> |
| 11 | #include <stdint.h> |
| 12 | |
Harrison Mutai | 68ecce1 | 2025-05-12 12:38:24 +0000 | [diff] [blame^] | 13 | #include <lib/utils_def.h> |
Harrison Mutai | b674809 | 2025-04-25 16:03:03 +0000 | [diff] [blame] | 14 | #include <drivers/auth/crypto_mod.h> |
| 15 | #include "event_handoff.h" |
| 16 | #include "tcg.h" |
| 17 | |
| 18 | /* |
| 19 | * Set Event Log debug level to one of: |
| 20 | * |
| 21 | * LOG_LEVEL_ERROR |
| 22 | * LOG_LEVEL_INFO |
| 23 | * LOG_LEVEL_WARNING |
| 24 | * LOG_LEVEL_VERBOSE |
| 25 | */ |
| 26 | #if EVENT_LOG_LEVEL == LOG_LEVEL_ERROR |
| 27 | #define LOG_EVENT ERROR |
| 28 | #elif EVENT_LOG_LEVEL == LOG_LEVEL_NOTICE |
| 29 | #define LOG_EVENT NOTICE |
| 30 | #elif EVENT_LOG_LEVEL == LOG_LEVEL_WARNING |
| 31 | #define LOG_EVENT WARN |
| 32 | #elif EVENT_LOG_LEVEL == LOG_LEVEL_INFO |
| 33 | #define LOG_EVENT INFO |
| 34 | #elif EVENT_LOG_LEVEL == LOG_LEVEL_VERBOSE |
| 35 | #define LOG_EVENT VERBOSE |
| 36 | #else |
| 37 | #define LOG_EVENT printf |
| 38 | #endif |
| 39 | |
| 40 | /* Number of hashing algorithms supported */ |
| 41 | #define HASH_ALG_COUNT 1U |
| 42 | |
| 43 | #define EVLOG_INVALID_ID UINT32_MAX |
| 44 | |
| 45 | #define MEMBER_SIZE(type, member) sizeof(((type *)0)->member) |
| 46 | |
| 47 | typedef struct { |
| 48 | unsigned int id; |
| 49 | const char *name; |
| 50 | unsigned int pcr; |
| 51 | } event_log_metadata_t; |
| 52 | |
| 53 | #define ID_EVENT_SIZE (sizeof(id_event_headers_t) + \ |
| 54 | (sizeof(id_event_algorithm_size_t) * HASH_ALG_COUNT) + \ |
| 55 | sizeof(id_event_struct_data_t)) |
| 56 | |
| 57 | #define LOC_EVENT_SIZE (sizeof(event2_header_t) + \ |
| 58 | sizeof(tpmt_ha) + TCG_DIGEST_SIZE + \ |
| 59 | sizeof(event2_data_t) + \ |
| 60 | sizeof(startup_locality_event_t)) |
| 61 | |
| 62 | #define LOG_MIN_SIZE (ID_EVENT_SIZE + LOC_EVENT_SIZE) |
| 63 | |
| 64 | #define EVENT2_HDR_SIZE (sizeof(event2_header_t) + \ |
| 65 | sizeof(tpmt_ha) + TCG_DIGEST_SIZE + \ |
| 66 | sizeof(event2_data_t)) |
| 67 | |
| 68 | /* Functions' declarations */ |
| 69 | |
| 70 | /** |
| 71 | * Initialize the Event Log buffer. |
| 72 | * |
| 73 | * Sets global pointers to manage the Event Log memory region, |
| 74 | * allowing subsequent log operations to write into the buffer. |
| 75 | * |
| 76 | * @param[in] event_log_start Pointer to the start of the Event Log buffer. |
| 77 | * @param[in] event_log_finish Pointer to the end of the buffer |
| 78 | * (i.e., one byte past the last valid address). |
| 79 | * |
| 80 | * @return 0 on success, or -EINVAL if the input range is invalid. |
| 81 | */ |
| 82 | int event_log_buf_init(uint8_t *event_log_start, uint8_t *event_log_finish); |
| 83 | |
| 84 | /** |
| 85 | * Dump the contents of the Event Log. |
| 86 | * |
| 87 | * Outputs the raw contents of the Event Log buffer, typically |
| 88 | * for debugging or audit purposes. |
| 89 | * |
| 90 | * @param[in] log_addr Pointer to the start of the Event Log buffer. |
| 91 | * @param[in] log_size Size of the Event Log buffer in bytes. |
| 92 | * |
| 93 | * @return 0 on success, or a negative error code on failure. |
| 94 | */ |
| 95 | int event_log_dump(uint8_t *log_addr, size_t log_size); |
| 96 | |
| 97 | /** |
| 98 | * Initialize the Event Log subsystem. |
| 99 | * |
| 100 | * Wrapper around `event_log_buf_init()` to configure the memory range |
| 101 | * for the Event Log buffer. |
| 102 | * |
| 103 | * @param[in] event_log_start Pointer to the start of the Event Log buffer. |
| 104 | * @param[in] event_log_finish Pointer to the end of the buffer |
| 105 | * (i.e., one byte past the last valid address). |
| 106 | * |
| 107 | * @return 0 on success, or a negative error code on failure. |
| 108 | */ |
| 109 | int event_log_init(uint8_t *event_log_start, uint8_t *event_log_finish); |
| 110 | |
| 111 | /** |
| 112 | * Measure input data and log its hash to the Event Log. |
| 113 | * |
| 114 | * Computes the cryptographic hash of the specified data and records it |
| 115 | * in the Event Log as a TCG_PCR_EVENT2 structure using event type EV_POST_CODE. |
| 116 | * Useful for firmware or image attestation. |
| 117 | * |
| 118 | * @param[in] data_base Pointer to the base of the data to be measured. |
| 119 | * @param[in] data_size Size of the data in bytes. |
| 120 | * @param[in] data_id Identifier used to match against metadata. |
| 121 | * @param[in] metadata_ptr Pointer to an array of event_log_metadata_t. |
| 122 | * |
| 123 | * @return 0 on success, or a negative error code on failure. |
| 124 | */ |
| 125 | int event_log_measure_and_record(uintptr_t data_base, uint32_t data_size, |
| 126 | uint32_t data_id, |
| 127 | const event_log_metadata_t *metadata_ptr); |
| 128 | |
| 129 | /** |
| 130 | * Measure the input data and return its hash. |
| 131 | * |
| 132 | * Computes the cryptographic hash of the specified memory region using |
| 133 | * the default hashing algorithm configured in the Event Log subsystem. |
| 134 | * |
| 135 | * @param[in] data_base Pointer to the base of the data to be measured. |
| 136 | * @param[in] data_size Size of the data in bytes. |
| 137 | * @param[out] hash_data Buffer to hold the resulting hash output |
| 138 | * (must be at least CRYPTO_MD_MAX_SIZE bytes). |
| 139 | * |
| 140 | * @return 0 on success, or an error code on failure. |
| 141 | */ |
| 142 | int event_log_measure(uintptr_t data_base, uint32_t data_size, |
| 143 | unsigned char hash_data[CRYPTO_MD_MAX_SIZE]); |
| 144 | |
| 145 | /** |
| 146 | * Record a measurement event in the Event Log. |
| 147 | * |
| 148 | * Writes a TCG_PCR_EVENT2 structure to the Event Log using the |
| 149 | * provided hash and metadata. This function assumes the buffer |
| 150 | * has enough space and that `event_log_buf_init()` has been called. |
| 151 | * |
| 152 | * @param[in] hash Pointer to the digest (TCG_DIGEST_SIZE bytes). |
| 153 | * @param[in] event_type Type of the event, as defined in tcg.h. |
| 154 | * @param[in] metadata_ptr Pointer to an event_log_metadata_t structure |
| 155 | * providing event-specific context (e.g., PCR index, name). |
| 156 | * |
| 157 | * @return 0 on success, or -ENOMEM if the buffer has insufficient space. |
| 158 | */ |
| 159 | int event_log_record(const uint8_t *hash, uint32_t event_type, |
| 160 | const event_log_metadata_t *metadata_ptr); |
| 161 | |
| 162 | /** |
| 163 | * Initialize the Event Log with mandatory header events. |
| 164 | * |
| 165 | * Writes the Specification ID (SpecID) and Startup Locality events |
| 166 | * as required by the TCG PC Client Platform Firmware Profile. |
| 167 | * These must be the first entries in the Event Log. |
| 168 | * |
| 169 | * @return 0 on success, or a negative error code on failure. |
| 170 | */ |
| 171 | int event_log_write_header(void); |
| 172 | |
| 173 | /** |
| 174 | * Write the SpecID event to the Event Log. |
| 175 | * |
| 176 | * Records the TCG_EfiSpecIDEventStruct to declare the structure |
| 177 | * and supported algorithms of the Event Log format. |
| 178 | * |
| 179 | * @return 0 on success, or a negative error code on failure. |
| 180 | */ |
| 181 | int event_log_write_specid_event(void); |
| 182 | |
| 183 | /** |
| 184 | * Get the current size of the Event Log. |
| 185 | * |
| 186 | * Calculates how many bytes of the Event Log buffer have been used, |
| 187 | * based on the current log pointer and the start of the buffer. |
| 188 | * |
| 189 | * @param[in] event_log_start Pointer to the start of the Event Log buffer. |
| 190 | * |
| 191 | * @return The number of bytes currently used in the Event Log. |
| 192 | */ |
| 193 | size_t event_log_get_cur_size(uint8_t *event_log_start); |
| 194 | |
| 195 | #endif /* EVENT_LOG_H */ |