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