1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef __LINUX_TPM_EVENTLOG_H__
4 #define __LINUX_TPM_EVENTLOG_H__
8 #define TCG_EVENT_NAME_LEN_MAX 255
9 #define MAX_TEXT_EVENT 1000 /* Max event string length */
10 #define ACPI_TCPA_SIG "TCPA" /* 0x41504354 /'TCPA' */
12 #define EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2 0x1
13 #define EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 0x2
16 #define do_endian_conversion(x) be32_to_cpu(x)
18 #define do_endian_conversion(x) x
21 enum bios_platform_class {
29 u8 pcr_value[20]; /* SHA1 */
34 enum tcpa_event_types {
45 PLATFORM_CONFIG_FLAGS,
55 struct tcpa_pc_event {
61 enum tcpa_pc_event_ids {
70 OPTION_ROM_MICROCODE = 10,
74 HOST_TABLE_OF_DEVICES,
77 /* http://www.trustedcomputinggroup.org/tcg-efi-protocol-specification/ */
79 struct tcg_efi_specid_event_algs {
84 struct tcg_efi_specid_event_head {
87 u8 spec_version_minor;
88 u8 spec_version_major;
92 struct tcg_efi_specid_event_algs digest_sizes[];
95 struct tcg_pcr_event {
103 struct tcg_event_field {
108 struct tcg_pcr_event2_head {
112 struct tpm_digest digests[];
115 struct tcg_algorithm_size {
120 struct tcg_algorithm_info {
123 u8 spec_version_minor;
124 u8 spec_version_major;
127 u32 number_of_algorithms;
128 struct tcg_algorithm_size digest_sizes[];
132 #define TPM_MEMREMAP(start, size) NULL
136 #define TPM_MEMUNMAP(start, size) do{} while(0)
140 * __calc_tpm2_event_size - calculate the size of a TPM2 event log entry
141 * @event: Pointer to the event whose size should be calculated
142 * @event_header: Pointer to the initial event containing the digest lengths
143 * @do_mapping: Whether or not the event needs to be mapped
145 * The TPM2 event log format can contain multiple digests corresponding to
146 * separate PCR banks, and also contains a variable length of the data that
147 * was measured. This requires knowledge of how long each digest type is,
148 * and this information is contained within the first event in the log.
150 * We calculate the length by examining the number of events, and then looking
151 * at each event in turn to determine how much space is used for events in
152 * total. Once we've done this we know the offset of the data length field,
153 * and can calculate the total size of the event.
155 * Return: size of the event on success, 0 on failure
158 static inline int __calc_tpm2_event_size(struct tcg_pcr_event2_head *event,
159 struct tcg_pcr_event *event_header,
162 struct tcg_efi_specid_event_head *efispecid;
163 struct tcg_event_field *event_field;
164 void *mapping = NULL;
173 u32 count, event_type;
176 marker_start = marker;
177 marker = marker + sizeof(event->pcr_idx) + sizeof(event->event_type)
178 + sizeof(event->count);
180 /* Map the event header */
182 mapping_size = marker - marker_start;
183 mapping = TPM_MEMREMAP((unsigned long)marker_start,
190 mapping = marker_start;
193 event = (struct tcg_pcr_event2_head *)mapping;
195 * The loop below will unmap these fields if the log is larger than
196 * one page, so save them here for reference:
198 count = READ_ONCE(event->count);
199 event_type = READ_ONCE(event->event_type);
201 efispecid = (struct tcg_efi_specid_event_head *)event_header->event;
203 /* Check if event is malformed. */
204 if (count > efispecid->num_algs) {
209 for (i = 0; i < count; i++) {
210 halg_size = sizeof(event->digests[i].alg_id);
212 /* Map the digest's algorithm identifier */
214 TPM_MEMUNMAP(mapping, mapping_size);
215 mapping_size = halg_size;
216 mapping = TPM_MEMREMAP((unsigned long)marker,
226 memcpy(&halg, mapping, halg_size);
227 marker = marker + halg_size;
229 for (j = 0; j < efispecid->num_algs; j++) {
230 if (halg == efispecid->digest_sizes[j].alg_id) {
232 efispecid->digest_sizes[j].digest_size;
236 /* Algorithm without known length. Such event is unparseable. */
237 if (j == efispecid->num_algs) {
244 * Map the event size - we don't read from the event itself, so
245 * we don't need to map it
248 TPM_MEMUNMAP(mapping, mapping_size);
249 mapping_size += sizeof(event_field->event_size);
250 mapping = TPM_MEMREMAP((unsigned long)marker,
260 event_field = (struct tcg_event_field *)mapping;
262 marker = marker + sizeof(event_field->event_size)
263 + event_field->event_size;
264 size = marker - marker_start;
266 if (event_type == 0 && event_field->event_size == 0)
271 TPM_MEMUNMAP(mapping, mapping_size);