blob: 447147977e3bfaa51d020cd04c68a65da1c10dba [file] [log] [blame]
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Defines APIs that allow an OS to interact with UEFI firmware to query
4 * information about the device.
5 * https://trustedcomputinggroup.org/resource/tcg-efi-protocol-specification/
6 *
7 * Copyright (c) 2020, Linaro Limited
8 */
9
10#define LOG_CATEGORY LOGC_EFI
11#include <common.h>
12#include <dm.h>
13#include <efi_loader.h>
Heinrich Schuchardta45dac12021-09-09 08:50:01 +020014#include <efi_variable.h>
Ilias Apalodimasc1c02102020-11-11 11:18:11 +020015#include <efi_tcg2.h>
16#include <log.h>
Masahisa Kojima163a0d72021-05-26 12:09:58 +090017#include <malloc.h>
Masahisa Kojima3d49ee82021-10-26 17:27:24 +090018#include <smbios.h>
Pali Rohárbdfb6d72021-08-02 15:18:31 +020019#include <version_string.h>
Ilias Apalodimasc1c02102020-11-11 11:18:11 +020020#include <tpm-v2.h>
Ilias Apalodimasd6b55a42021-11-18 10:13:42 +020021#include <tpm_api.h>
Masahisa Kojima163a0d72021-05-26 12:09:58 +090022#include <u-boot/hash-checksum.h>
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020023#include <u-boot/sha1.h>
24#include <u-boot/sha256.h>
25#include <u-boot/sha512.h>
Masahisa Kojima14cbb332021-11-03 11:04:09 +090026#include <linux/unaligned/be_byteshift.h>
27#include <linux/unaligned/le_byteshift.h>
Ilias Apalodimasc1c02102020-11-11 11:18:11 +020028#include <linux/unaligned/generic.h>
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020029#include <hexdump.h>
Ilias Apalodimasc1c02102020-11-11 11:18:11 +020030
Ilias Apalodimas5ba03972021-11-18 09:03:39 +020031/**
32 * struct event_log_buffer - internal eventlog management structure
33 *
34 * @buffer: eventlog buffer
35 * @final_buffer: finalevent config table buffer
36 * @pos: current position of 'buffer'
37 * @final_pos: current position of 'final_buffer'
38 * @get_event_called: true if GetEventLog has been invoked at least once
39 * @ebs_called: true if ExitBootServices has been invoked
40 * @truncated: true if the 'buffer' is truncated
41 */
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020042struct event_log_buffer {
43 void *buffer;
44 void *final_buffer;
45 size_t pos; /* eventlog position */
46 size_t final_pos; /* final events config table position */
47 size_t last_event_size;
48 bool get_event_called;
Ilias Apalodimas5ba03972021-11-18 09:03:39 +020049 bool ebs_called;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020050 bool truncated;
51};
Ilias Apalodimasc1c02102020-11-11 11:18:11 +020052
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020053static struct event_log_buffer event_log;
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +090054static bool tcg2_efi_app_invoked;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +020055/*
56 * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset.
57 * Since the current tpm2_get_capability() response buffers starts at
58 * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate
59 * the response size and offset once for all consumers
60 */
61#define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \
62 offsetof(struct tpms_capability_data, data))
63#define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \
64 offsetof(struct tpms_tagged_property, value))
65
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020066static const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID;
67static const efi_guid_t efi_guid_final_events = EFI_TCG2_FINAL_EVENTS_TABLE_GUID;
68
69struct digest_info {
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +020070 u16 hash_alg;
71 u32 hash_mask;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020072 u16 hash_len;
73};
74
Ilias Apalodimas1f6871d2021-05-25 14:35:31 +030075static const struct digest_info hash_algo_list[] = {
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +020076 {
77 TPM2_ALG_SHA1,
78 EFI_TCG2_BOOT_HASH_ALG_SHA1,
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020079 TPM2_SHA1_DIGEST_SIZE,
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +020080 },
81 {
82 TPM2_ALG_SHA256,
83 EFI_TCG2_BOOT_HASH_ALG_SHA256,
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020084 TPM2_SHA256_DIGEST_SIZE,
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +020085 },
86 {
87 TPM2_ALG_SHA384,
88 EFI_TCG2_BOOT_HASH_ALG_SHA384,
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020089 TPM2_SHA384_DIGEST_SIZE,
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +020090 },
91 {
92 TPM2_ALG_SHA512,
93 EFI_TCG2_BOOT_HASH_ALG_SHA512,
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020094 TPM2_SHA512_DIGEST_SIZE,
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +020095 },
96};
97
Masahisa Kojima96485d22021-10-26 17:27:26 +090098struct variable_info {
99 const u16 *name;
100 bool accept_empty;
Masahisa Kojima65aa2592021-10-26 17:27:27 +0900101 u32 pcr_index;
Masahisa Kojima96485d22021-10-26 17:27:26 +0900102};
103
104static struct variable_info secure_variables[] = {
Masahisa Kojima65aa2592021-10-26 17:27:27 +0900105 {u"SecureBoot", true, 7},
106 {u"PK", true, 7},
107 {u"KEK", true, 7},
108 {u"db", true, 7},
109 {u"dbx", true, 7},
110 {u"dbt", false, 7},
111 {u"dbr", false, 7},
112 {u"DeployedMode", false, 1},
113 {u"AuditMode", false, 1},
Masahisa Kojimacfbcf052021-08-13 16:12:39 +0900114};
115
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200116#define MAX_HASH_COUNT ARRAY_SIZE(hash_algo_list)
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200117
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200118/**
119 * alg_to_mask - Get a TCG hash mask for algorithms
120 *
121 * @hash_alg: TCG defined algorithm
122 *
123 * @Return: TCG hashing algorithm bitmaps, 0 if the algorithm is not supported
124 */
125static u32 alg_to_mask(u16 hash_alg)
126{
Ilias Apalodimas1f6871d2021-05-25 14:35:31 +0300127 size_t i;
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200128
129 for (i = 0; i < MAX_HASH_COUNT; i++) {
130 if (hash_algo_list[i].hash_alg == hash_alg)
131 return hash_algo_list[i].hash_mask;
132 }
133
134 return 0;
135}
136
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200137/**
138 * alg_to_len - Get a TCG hash len for algorithms
139 *
140 * @hash_alg: TCG defined algorithm
141 *
142 * @Return: len of chosen algorithm, 0 if the algorithm is not supported
143 */
144static u16 alg_to_len(u16 hash_alg)
145{
Ilias Apalodimas1f6871d2021-05-25 14:35:31 +0300146 size_t i;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200147
148 for (i = 0; i < MAX_HASH_COUNT; i++) {
149 if (hash_algo_list[i].hash_alg == hash_alg)
150 return hash_algo_list[i].hash_len;
151 }
152
153 return 0;
154}
155
156static u32 tcg_event_final_size(struct tpml_digest_values *digest_list)
157{
158 u32 len;
Ilias Apalodimas1f6871d2021-05-25 14:35:31 +0300159 size_t i;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200160
161 len = offsetof(struct tcg_pcr_event2, digests);
162 len += offsetof(struct tpml_digest_values, digests);
163 for (i = 0; i < digest_list->count; i++) {
164 u16 hash_alg = digest_list->digests[i].hash_alg;
165
166 len += offsetof(struct tpmt_ha, digest);
167 len += alg_to_len(hash_alg);
168 }
169 len += sizeof(u32); /* tcg_pcr_event2 event_size*/
170
171 return len;
172}
173
174/* tcg2_pcr_extend - Extend PCRs for a TPM2 device for a given tpml_digest_values
175 *
176 * @dev: device
177 * @digest_list: list of digest algorithms to extend
178 *
179 * @Return: status code
180 */
181static efi_status_t tcg2_pcr_extend(struct udevice *dev, u32 pcr_index,
182 struct tpml_digest_values *digest_list)
183{
184 u32 rc;
Ilias Apalodimas1f6871d2021-05-25 14:35:31 +0300185 size_t i;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200186
187 for (i = 0; i < digest_list->count; i++) {
188 u32 alg = digest_list->digests[i].hash_alg;
189
190 rc = tpm2_pcr_extend(dev, pcr_index, alg,
191 (u8 *)&digest_list->digests[i].digest,
192 alg_to_len(alg));
193 if (rc) {
194 EFI_PRINT("Failed to extend PCR\n");
195 return EFI_DEVICE_ERROR;
196 }
197 }
198
199 return EFI_SUCCESS;
200}
201
Ilias Apalodimas5ba03972021-11-18 09:03:39 +0200202/* put_event - Append an agile event to an eventlog
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200203 *
204 * @pcr_index: PCR index
205 * @event_type: type of event added
206 * @digest_list: list of digest algorithms to add
207 * @size: size of event
208 * @event: event to add
Ilias Apalodimas5ba03972021-11-18 09:03:39 +0200209 * @log: log buffer to append the event
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200210 *
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200211 */
Ilias Apalodimas5ba03972021-11-18 09:03:39 +0200212static void put_event(u32 pcr_index, u32 event_type,
213 struct tpml_digest_values *digest_list, u32 size,
214 u8 event[], void *log)
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200215{
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200216 size_t pos;
Ilias Apalodimas1f6871d2021-05-25 14:35:31 +0300217 size_t i;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200218 u32 event_size;
219
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200220 /*
221 * size refers to the length of event[] only, we need to check against
222 * the final tcg_pcr_event2 size
223 */
224 event_size = size + tcg_event_final_size(digest_list);
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200225
226 put_unaligned_le32(pcr_index, log);
227 pos = offsetof(struct tcg_pcr_event2, event_type);
Ilias Apalodimasf8cd72d2021-03-30 00:42:36 +0300228 put_unaligned_le32(event_type, (void *)((uintptr_t)log + pos));
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200229 pos = offsetof(struct tcg_pcr_event2, digests); /* count */
Ilias Apalodimasf8cd72d2021-03-30 00:42:36 +0300230 put_unaligned_le32(digest_list->count, (void *)((uintptr_t)log + pos));
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200231
232 pos += offsetof(struct tpml_digest_values, digests);
233 for (i = 0; i < digest_list->count; i++) {
234 u16 hash_alg = digest_list->digests[i].hash_alg;
235 u8 *digest = (u8 *)&digest_list->digests[i].digest;
236
Ilias Apalodimasf8cd72d2021-03-30 00:42:36 +0300237 put_unaligned_le16(hash_alg, (void *)((uintptr_t)log + pos));
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200238 pos += offsetof(struct tpmt_ha, digest);
Ilias Apalodimasf8cd72d2021-03-30 00:42:36 +0300239 memcpy((void *)((uintptr_t)log + pos), digest, alg_to_len(hash_alg));
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200240 pos += alg_to_len(hash_alg);
241 }
242
Ilias Apalodimasf8cd72d2021-03-30 00:42:36 +0300243 put_unaligned_le32(size, (void *)((uintptr_t)log + pos));
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200244 pos += sizeof(u32); /* tcg_pcr_event2 event_size*/
Ilias Apalodimasf8cd72d2021-03-30 00:42:36 +0300245 memcpy((void *)((uintptr_t)log + pos), event, size);
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200246 pos += size;
247
Ilias Apalodimas5ba03972021-11-18 09:03:39 +0200248 /*
249 * make sure the calculated buffer is what we checked against
250 * This check should never fail. It checks the code above is
251 * calculating the right length for the event we are adding
252 */
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200253 if (pos != event_size)
Ilias Apalodimas5ba03972021-11-18 09:03:39 +0200254 log_err("Appending to the EventLog failed\n");
255}
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200256
Ilias Apalodimas5ba03972021-11-18 09:03:39 +0200257/* tcg2_agile_log_append - Append an agile event to an eventlog
258 *
259 * @pcr_index: PCR index
260 * @event_type: type of event added
261 * @digest_list: list of digest algorithms to add
262 * @size: size of event
263 * @event: event to add
264 * @log: log buffer to append the event
265 *
266 * @Return: status code
267 */
268static efi_status_t tcg2_agile_log_append(u32 pcr_index, u32 event_type,
269 struct tpml_digest_values *digest_list,
270 u32 size, u8 event[])
271{
272 void *log = (void *)((uintptr_t)event_log.buffer + event_log.pos);
273 u32 event_size = size + tcg_event_final_size(digest_list);
274 struct efi_tcg2_final_events_table *final_event;
275 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200276
Ilias Apalodimas5ba03972021-11-18 09:03:39 +0200277 /* if ExitBootServices hasn't been called update the normal log */
278 if (!event_log.ebs_called) {
279 if (event_log.truncated ||
280 event_log.pos + event_size > TPM2_EVENT_LOG_SIZE) {
281 event_log.truncated = true;
282 return EFI_VOLUME_FULL;
283 }
284 put_event(pcr_index, event_type, digest_list, size, event, log);
285 event_log.pos += event_size;
286 event_log.last_event_size = event_size;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200287 }
288
Ilias Apalodimas5ba03972021-11-18 09:03:39 +0200289 if (!event_log.get_event_called)
290 return ret;
291
292 /* if GetEventLog has been called update FinalEventLog as well */
293 if (event_log.final_pos + event_size > TPM2_EVENT_LOG_SIZE)
294 return EFI_VOLUME_FULL;
295
296 log = (void *)((uintptr_t)event_log.final_buffer + event_log.final_pos);
297 put_event(pcr_index, event_type, digest_list, size, event, log);
298
299 final_event = event_log.final_buffer;
300 final_event->number_of_events++;
301 event_log.final_pos += event_size;
302
303 return ret;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200304}
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200305
306/**
307 * platform_get_tpm_device() - retrieve TPM device
308 *
309 * This function retrieves the udevice implementing a TPM
310 *
311 * This function may be overridden if special initialization is needed.
312 *
313 * @dev: udevice
314 * Return: status code
315 */
316__weak efi_status_t platform_get_tpm2_device(struct udevice **dev)
317{
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200318 for_each_tpm_device(*dev) {
319 /* Only support TPMv2 devices */
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200320 if (tpm_get_version(*dev) == TPM_V2)
321 return EFI_SUCCESS;
322 }
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200323
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200324 return EFI_NOT_FOUND;
325}
326
327/**
Ruchika Gupta34287ef2021-11-29 13:09:44 +0530328 * platform_get_eventlog() - retrieve the eventlog address and size
329 *
330 * This function retrieves the eventlog address and size if the underlying
331 * firmware has done some measurements and passed them.
332 *
333 * This function may be overridden based on platform specific method of
334 * passing the eventlog address and size.
335 *
336 * @dev: udevice
337 * @addr: eventlog address
338 * @sz: eventlog size
339 * Return: status code
340 */
341__weak efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr,
342 u32 *sz)
343{
344 const u64 *basep;
345 const u32 *sizep;
346
347 basep = dev_read_prop(dev, "tpm_event_log_addr", NULL);
348 if (!basep)
349 return EFI_NOT_FOUND;
350
351 *addr = be64_to_cpup((__force __be64 *)basep);
352
353 sizep = dev_read_prop(dev, "tpm_event_log_size", NULL);
354 if (!sizep)
355 return EFI_NOT_FOUND;
356
357 *sz = be32_to_cpup((__force __be32 *)sizep);
358 if (*sz == 0) {
359 log_debug("event log empty\n");
360 return EFI_NOT_FOUND;
361 }
362
363 return EFI_SUCCESS;
364}
365
366/**
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200367 * tpm2_get_max_command_size() - get the supported max command size
368 *
369 * @dev: TPM device
370 * @max_command_size: output buffer for the size
371 *
372 * Return: 0 on success, -1 on error
373 */
374static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size)
375{
376 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
377 u32 ret;
378
379 memset(response, 0, sizeof(response));
380 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
381 TPM2_PT_MAX_COMMAND_SIZE, response, 1);
382 if (ret)
383 return -1;
384
385 *max_command_size = (uint16_t)get_unaligned_be32(response +
386 properties_offset);
387
388 return 0;
389}
390
391/**
392 * tpm2_get_max_response_size() - get the supported max response size
393 *
394 * @dev: TPM device
395 * @max_response_size: output buffer for the size
396 *
397 * Return: 0 on success, -1 on error
398 */
399static int tpm2_get_max_response_size(struct udevice *dev,
400 u16 *max_response_size)
401{
402 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
403 u32 ret;
404
405 memset(response, 0, sizeof(response));
406 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
407 TPM2_PT_MAX_RESPONSE_SIZE, response, 1);
408 if (ret)
409 return -1;
410
411 *max_response_size = (uint16_t)get_unaligned_be32(response +
412 properties_offset);
413
414 return 0;
415}
416
417/**
418 * tpm2_get_manufacturer_id() - get the manufacturer ID
419 *
420 * @dev: TPM device
421 * @manufacturer_id: output buffer for the id
422 *
423 * Return: 0 on success, -1 on error
424 */
425static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id)
426{
427 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
428 u32 ret;
429
430 memset(response, 0, sizeof(response));
431 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
432 TPM2_PT_MANUFACTURER, response, 1);
433 if (ret)
434 return -1;
435
436 *manufacturer_id = get_unaligned_be32(response + properties_offset);
437
438 return 0;
439}
440
441/**
442 * tpm2_get_num_pcr() - get the number of PCRs
443 *
444 * @dev: TPM device
445 * @manufacturer_id: output buffer for the number
446 *
447 * Return: 0 on success, -1 on error
448 */
449static int tpm2_get_num_pcr(struct udevice *dev, u32 *num_pcr)
450{
451 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
452 u32 ret;
453
454 memset(response, 0, sizeof(response));
455 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
456 TPM2_PT_PCR_COUNT, response, 1);
457 if (ret)
458 return -1;
459
460 *num_pcr = get_unaligned_be32(response + properties_offset);
461 if (*num_pcr > TPM2_MAX_PCRS)
462 return -1;
463
464 return 0;
465}
466
467/**
468 * is_active_pcr() - Check if a supported algorithm is active
469 *
470 * @dev: TPM device
471 * @selection: struct of PCR information
472 *
473 * Return: true if PCR is active
474 */
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200475static bool is_active_pcr(struct tpms_pcr_selection *selection)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200476{
477 int i;
478 /*
479 * check the pcr_select. If at least one of the PCRs supports the
480 * algorithm add it on the active ones
481 */
482 for (i = 0; i < selection->size_of_select; i++) {
483 if (selection->pcr_select[i])
484 return true;
485 }
486
487 return false;
488}
489
490/**
491 * tpm2_get_pcr_info() - get the supported, active PCRs and number of banks
492 *
493 * @dev: TPM device
494 * @supported_pcr: bitmask with the algorithms supported
495 * @active_pcr: bitmask with the active algorithms
496 * @pcr_banks: number of PCR banks
497 *
498 * Return: 0 on success, -1 on error
499 */
500static int tpm2_get_pcr_info(struct udevice *dev, u32 *supported_pcr,
501 u32 *active_pcr, u32 *pcr_banks)
502{
503 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
504 struct tpml_pcr_selection pcrs;
505 u32 ret, num_pcr;
Ilias Apalodimas1f6871d2021-05-25 14:35:31 +0300506 size_t i;
507 int tpm_ret;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200508
Ilias Apalodimas38de6802021-05-26 21:01:00 +0300509 *supported_pcr = 0;
510 *active_pcr = 0;
511 *pcr_banks = 0;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200512 memset(response, 0, sizeof(response));
513 ret = tpm2_get_capability(dev, TPM2_CAP_PCRS, 0, response, 1);
514 if (ret)
515 goto out;
516
517 pcrs.count = get_unaligned_be32(response);
518 /*
519 * We only support 5 algorithms for now so check against that
520 * instead of TPM2_NUM_PCR_BANKS
521 */
522 if (pcrs.count > MAX_HASH_COUNT || pcrs.count < 1)
523 goto out;
524
525 tpm_ret = tpm2_get_num_pcr(dev, &num_pcr);
526 if (tpm_ret)
527 goto out;
528
529 for (i = 0; i < pcrs.count; i++) {
530 /*
531 * Definition of TPMS_PCR_SELECTION Structure
532 * hash: u16
533 * size_of_select: u8
534 * pcr_select: u8 array
535 *
536 * The offsets depend on the number of the device PCRs
537 * so we have to calculate them based on that
538 */
539 u32 hash_offset = offsetof(struct tpml_pcr_selection, selection) +
540 i * offsetof(struct tpms_pcr_selection, pcr_select) +
541 i * ((num_pcr + 7) / 8);
542 u32 size_select_offset =
543 hash_offset + offsetof(struct tpms_pcr_selection,
544 size_of_select);
545 u32 pcr_select_offset =
546 hash_offset + offsetof(struct tpms_pcr_selection,
547 pcr_select);
548
549 pcrs.selection[i].hash =
550 get_unaligned_be16(response + hash_offset);
551 pcrs.selection[i].size_of_select =
552 __get_unaligned_be(response + size_select_offset);
553 if (pcrs.selection[i].size_of_select > TPM2_PCR_SELECT_MAX)
554 goto out;
555 /* copy the array of pcr_select */
556 memcpy(pcrs.selection[i].pcr_select, response + pcr_select_offset,
557 pcrs.selection[i].size_of_select);
558 }
559
560 for (i = 0; i < pcrs.count; i++) {
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200561 u32 hash_mask = alg_to_mask(pcrs.selection[i].hash);
562
563 if (hash_mask) {
564 *supported_pcr |= hash_mask;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200565 if (is_active_pcr(&pcrs.selection[i]))
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200566 *active_pcr |= hash_mask;
567 } else {
568 EFI_PRINT("Unknown algorithm %x\n", pcrs.selection[i].hash);
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200569 }
570 }
571
572 *pcr_banks = pcrs.count;
573
574 return 0;
575out:
576 return -1;
577}
578
579/**
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200580 * __get_active_pcr_banks() - returns the currently active PCR banks
581 *
582 * @active_pcr_banks: pointer for receiving the bitmap of currently
583 * active PCR banks
584 *
585 * Return: status code
586 */
587static efi_status_t __get_active_pcr_banks(u32 *active_pcr_banks)
588{
589 struct udevice *dev;
Ilias Apalodimas38de6802021-05-26 21:01:00 +0300590 u32 active = 0, supported = 0, pcr_banks = 0;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200591 efi_status_t ret;
592 int err;
593
594 ret = platform_get_tpm2_device(&dev);
595 if (ret != EFI_SUCCESS)
596 goto out;
597
598 err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_banks);
599 if (err) {
600 ret = EFI_DEVICE_ERROR;
601 goto out;
602 }
603
604 *active_pcr_banks = active;
605
606out:
607 return ret;
608}
609
610/* tcg2_create_digest - create a list of digests of the supported PCR banks
611 * for a given memory range
612 *
613 * @input: input memory
614 * @length: length of buffer to calculate the digest
615 * @digest_list: list of digests to fill in
616 *
617 * Return: status code
618 */
619static efi_status_t tcg2_create_digest(const u8 *input, u32 length,
620 struct tpml_digest_values *digest_list)
621{
622 sha1_context ctx;
623 sha256_context ctx_256;
624 sha512_context ctx_512;
Masahisa Kojimab1a7a5e2021-04-14 11:55:49 +0900625 u8 final[TPM2_SHA512_DIGEST_SIZE];
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200626 efi_status_t ret;
627 u32 active;
Ilias Apalodimas1f6871d2021-05-25 14:35:31 +0300628 size_t i;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200629
630 ret = __get_active_pcr_banks(&active);
631 if (ret != EFI_SUCCESS)
632 return ret;
633
634 digest_list->count = 0;
635 for (i = 0; i < MAX_HASH_COUNT; i++) {
636 u16 hash_alg = hash_algo_list[i].hash_alg;
637
638 if (!(active & alg_to_mask(hash_alg)))
639 continue;
640 switch (hash_alg) {
641 case TPM2_ALG_SHA1:
642 sha1_starts(&ctx);
643 sha1_update(&ctx, input, length);
644 sha1_finish(&ctx, final);
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200645 break;
646 case TPM2_ALG_SHA256:
647 sha256_starts(&ctx_256);
648 sha256_update(&ctx_256, input, length);
649 sha256_finish(&ctx_256, final);
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200650 break;
651 case TPM2_ALG_SHA384:
652 sha384_starts(&ctx_512);
653 sha384_update(&ctx_512, input, length);
654 sha384_finish(&ctx_512, final);
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200655 break;
656 case TPM2_ALG_SHA512:
657 sha512_starts(&ctx_512);
658 sha512_update(&ctx_512, input, length);
659 sha512_finish(&ctx_512, final);
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200660 break;
661 default:
662 EFI_PRINT("Unsupported algorithm %x\n", hash_alg);
663 return EFI_INVALID_PARAMETER;
664 }
Ruchika Gupta346cee32021-09-14 12:14:31 +0530665 digest_list->digests[digest_list->count].hash_alg = hash_alg;
666 memcpy(&digest_list->digests[digest_list->count].digest, final,
667 (u32)alg_to_len(hash_alg));
Ilias Apalodimas6fe8b4a2021-04-22 14:32:14 +0300668 digest_list->count++;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200669 }
670
671 return EFI_SUCCESS;
672}
673
674/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200675 * efi_tcg2_get_capability() - protocol capability information and state information
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200676 *
677 * @this: TCG2 protocol instance
678 * @capability: caller allocated memory with size field to the size of
679 * the structure allocated
680
681 * Return: status code
682 */
683static efi_status_t EFIAPI
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200684efi_tcg2_get_capability(struct efi_tcg2_protocol *this,
685 struct efi_tcg2_boot_service_capability *capability)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200686{
687 struct udevice *dev;
688 efi_status_t efi_ret;
689 int ret;
690
691 EFI_ENTRY("%p, %p", this, capability);
692
693 if (!this || !capability) {
694 efi_ret = EFI_INVALID_PARAMETER;
695 goto out;
696 }
697
Masahisa Kojimabad49da2021-09-06 12:04:12 +0900698 if (capability->size < BOOT_SERVICE_CAPABILITY_MIN) {
699 capability->size = BOOT_SERVICE_CAPABILITY_MIN;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200700 efi_ret = EFI_BUFFER_TOO_SMALL;
701 goto out;
702 }
703
704 if (capability->size < sizeof(*capability)) {
705 capability->size = sizeof(*capability);
706 efi_ret = EFI_BUFFER_TOO_SMALL;
707 goto out;
708 }
709
710 capability->structure_version.major = 1;
711 capability->structure_version.minor = 1;
712 capability->protocol_version.major = 1;
713 capability->protocol_version.minor = 1;
714
715 efi_ret = platform_get_tpm2_device(&dev);
716 if (efi_ret != EFI_SUCCESS) {
717 capability->supported_event_logs = 0;
718 capability->hash_algorithm_bitmap = 0;
719 capability->tpm_present_flag = false;
720 capability->max_command_size = 0;
721 capability->max_response_size = 0;
722 capability->manufacturer_id = 0;
723 capability->number_of_pcr_banks = 0;
724 capability->active_pcr_banks = 0;
725
726 efi_ret = EFI_SUCCESS;
727 goto out;
728 }
729
730 /* We only allow a TPMv2 device to register the EFI protocol */
731 capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2;
732
733 capability->tpm_present_flag = true;
734
735 /* Supported and active PCRs */
736 capability->hash_algorithm_bitmap = 0;
737 capability->active_pcr_banks = 0;
738 ret = tpm2_get_pcr_info(dev, &capability->hash_algorithm_bitmap,
739 &capability->active_pcr_banks,
740 &capability->number_of_pcr_banks);
741 if (ret) {
742 efi_ret = EFI_DEVICE_ERROR;
743 goto out;
744 }
745
746 /* Max command size */
747 ret = tpm2_get_max_command_size(dev, &capability->max_command_size);
748 if (ret) {
749 efi_ret = EFI_DEVICE_ERROR;
750 goto out;
751 }
752
753 /* Max response size */
754 ret = tpm2_get_max_response_size(dev, &capability->max_response_size);
755 if (ret) {
756 efi_ret = EFI_DEVICE_ERROR;
757 goto out;
758 }
759
760 /* Manufacturer ID */
761 ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id);
762 if (ret) {
763 efi_ret = EFI_DEVICE_ERROR;
764 goto out;
765 }
766
767 return EFI_EXIT(EFI_SUCCESS);
768out:
769 return EFI_EXIT(efi_ret);
770}
771
772/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200773 * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its
774 * last entry
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200775 *
776 * @this: TCG2 protocol instance
777 * @log_format: type of event log format
778 * @event_log_location: pointer to the memory address of the event log
779 * @event_log_last_entry: pointer to the address of the start of the last
780 * entry in the event log in memory, if log contains
781 * more than 1 entry
782 * @event_log_truncated: set to true, if the Event Log is missing at i
783 * least one entry
784 *
785 * Return: status code
786 */
787static efi_status_t EFIAPI
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200788efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this,
789 efi_tcg_event_log_format log_format,
790 u64 *event_log_location, u64 *event_log_last_entry,
791 bool *event_log_truncated)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200792{
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200793 efi_status_t ret = EFI_SUCCESS;
794 struct udevice *dev;
795
796 EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location,
797 event_log_last_entry, event_log_truncated);
798
Masahisa Kojima580d7242021-09-03 10:55:50 +0900799 if (!this || !event_log_location || !event_log_last_entry ||
800 !event_log_truncated) {
801 ret = EFI_INVALID_PARAMETER;
802 goto out;
803 }
804
805 /* Only support TPMV2 */
806 if (log_format != TCG2_EVENT_LOG_FORMAT_TCG_2) {
807 ret = EFI_INVALID_PARAMETER;
808 goto out;
809 }
810
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200811 ret = platform_get_tpm2_device(&dev);
812 if (ret != EFI_SUCCESS) {
813 event_log_location = NULL;
814 event_log_last_entry = NULL;
815 *event_log_truncated = false;
816 ret = EFI_SUCCESS;
817 goto out;
818 }
819 *event_log_location = (uintptr_t)event_log.buffer;
820 *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos -
821 event_log.last_event_size);
822 *event_log_truncated = event_log.truncated;
823 event_log.get_event_called = true;
824
825out:
826 return EFI_EXIT(ret);
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200827}
828
829/**
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900830 * tcg2_hash_pe_image() - calculate PE/COFF image hash
831 *
832 * @efi: pointer to the EFI binary
833 * @efi_size: size of @efi binary
834 * @digest_list: list of digest algorithms to extend
835 *
836 * Return: status code
837 */
838static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size,
839 struct tpml_digest_values *digest_list)
840{
841 WIN_CERTIFICATE *wincerts = NULL;
842 size_t wincerts_len;
843 struct efi_image_regions *regs = NULL;
844 void *new_efi = NULL;
845 u8 hash[TPM2_SHA512_DIGEST_SIZE];
846 efi_status_t ret;
847 u32 active;
848 int i;
849
850 new_efi = efi_prepare_aligned_image(efi, &efi_size);
851 if (!new_efi)
852 return EFI_OUT_OF_RESOURCES;
853
854 if (!efi_image_parse(new_efi, efi_size, &regs, &wincerts,
855 &wincerts_len)) {
856 log_err("Parsing PE executable image failed\n");
857 ret = EFI_UNSUPPORTED;
858 goto out;
859 }
860
861 ret = __get_active_pcr_banks(&active);
862 if (ret != EFI_SUCCESS) {
863 goto out;
864 }
865
866 digest_list->count = 0;
867 for (i = 0; i < MAX_HASH_COUNT; i++) {
868 u16 hash_alg = hash_algo_list[i].hash_alg;
869
870 if (!(active & alg_to_mask(hash_alg)))
871 continue;
872 switch (hash_alg) {
873 case TPM2_ALG_SHA1:
874 hash_calculate("sha1", regs->reg, regs->num, hash);
875 break;
876 case TPM2_ALG_SHA256:
877 hash_calculate("sha256", regs->reg, regs->num, hash);
878 break;
879 case TPM2_ALG_SHA384:
880 hash_calculate("sha384", regs->reg, regs->num, hash);
881 break;
882 case TPM2_ALG_SHA512:
883 hash_calculate("sha512", regs->reg, regs->num, hash);
884 break;
885 default:
886 EFI_PRINT("Unsupported algorithm %x\n", hash_alg);
887 return EFI_INVALID_PARAMETER;
888 }
Ruchika Gupta346cee32021-09-14 12:14:31 +0530889 digest_list->digests[digest_list->count].hash_alg = hash_alg;
890 memcpy(&digest_list->digests[digest_list->count].digest, hash,
891 (u32)alg_to_len(hash_alg));
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900892 digest_list->count++;
893 }
894
895out:
896 if (new_efi != efi)
897 free(new_efi);
898 free(regs);
899
900 return ret;
901}
902
903/**
904 * tcg2_measure_pe_image() - measure PE/COFF image
905 *
906 * @efi: pointer to the EFI binary
907 * @efi_size: size of @efi binary
908 * @handle: loaded image handle
909 * @loaded_image: loaded image protocol
910 *
911 * Return: status code
912 */
913efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size,
914 struct efi_loaded_image_obj *handle,
915 struct efi_loaded_image *loaded_image)
916{
917 struct tpml_digest_values digest_list;
918 efi_status_t ret;
919 struct udevice *dev;
920 u32 pcr_index, event_type, event_size;
921 struct uefi_image_load_event *image_load_event;
922 struct efi_device_path *device_path;
923 u32 device_path_length;
924 IMAGE_DOS_HEADER *dos;
925 IMAGE_NT_HEADERS32 *nt;
926 struct efi_handler *handler;
927
928 ret = platform_get_tpm2_device(&dev);
929 if (ret != EFI_SUCCESS)
930 return ret;
931
932 switch (handle->image_type) {
933 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
934 pcr_index = 4;
935 event_type = EV_EFI_BOOT_SERVICES_APPLICATION;
936 break;
937 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
938 pcr_index = 2;
939 event_type = EV_EFI_BOOT_SERVICES_DRIVER;
940 break;
941 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
942 pcr_index = 2;
943 event_type = EV_EFI_RUNTIME_SERVICES_DRIVER;
944 break;
945 default:
946 return EFI_UNSUPPORTED;
947 }
948
949 ret = tcg2_hash_pe_image(efi, efi_size, &digest_list);
950 if (ret != EFI_SUCCESS)
951 return ret;
952
953 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
954 if (ret != EFI_SUCCESS)
955 return ret;
956
Ilias Apalodimas0bf538c2021-09-09 00:30:49 +0300957 ret = efi_search_protocol(&handle->header,
958 &efi_guid_loaded_image_device_path, &handler);
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900959 if (ret != EFI_SUCCESS)
960 return ret;
961
Ilias Apalodimas0bf538c2021-09-09 00:30:49 +0300962 device_path = handler->protocol_interface;
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900963 device_path_length = efi_dp_size(device_path);
964 if (device_path_length > 0) {
965 /* add end node size */
966 device_path_length += sizeof(struct efi_device_path);
967 }
968 event_size = sizeof(struct uefi_image_load_event) + device_path_length;
Ilias Apalodimas0bf538c2021-09-09 00:30:49 +0300969 image_load_event = calloc(1, event_size);
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900970 if (!image_load_event)
971 return EFI_OUT_OF_RESOURCES;
972
973 image_load_event->image_location_in_memory = (uintptr_t)efi;
974 image_load_event->image_length_in_memory = efi_size;
975 image_load_event->length_of_device_path = device_path_length;
976
977 dos = (IMAGE_DOS_HEADER *)efi;
978 nt = (IMAGE_NT_HEADERS32 *)(efi + dos->e_lfanew);
979 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
980 IMAGE_NT_HEADERS64 *nt64 = (IMAGE_NT_HEADERS64 *)nt;
981
982 image_load_event->image_link_time_address =
983 nt64->OptionalHeader.ImageBase;
984 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
985 image_load_event->image_link_time_address =
986 nt->OptionalHeader.ImageBase;
987 } else {
988 ret = EFI_INVALID_PARAMETER;
989 goto out;
990 }
991
Ilias Apalodimas0bf538c2021-09-09 00:30:49 +0300992 /* device_path_length might be zero */
993 memcpy(image_load_event->device_path, device_path, device_path_length);
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900994
995 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
996 event_size, (u8 *)image_load_event);
997
998out:
999 free(image_load_event);
1000
1001 return ret;
1002}
1003
1004/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +02001005 * efi_tcg2_hash_log_extend_event() - extend and optionally log events
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001006 *
1007 * @this: TCG2 protocol instance
1008 * @flags: bitmap providing additional information on the
1009 * operation
1010 * @data_to_hash: physical address of the start of the data buffer
1011 * to be hashed
1012 * @data_to_hash_len: the length in bytes of the buffer referenced by
1013 * data_to_hash
1014 * @efi_tcg_event: pointer to data buffer containing information
1015 * about the event
1016 *
1017 * Return: status code
1018 */
1019static efi_status_t EFIAPI
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +02001020efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags,
1021 u64 data_to_hash, u64 data_to_hash_len,
1022 struct efi_tcg2_event *efi_tcg_event)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001023{
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001024 struct udevice *dev;
1025 efi_status_t ret;
1026 u32 event_type, pcr_index, event_size;
1027 struct tpml_digest_values digest_list;
1028
1029 EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash,
1030 data_to_hash_len, efi_tcg_event);
1031
1032 if (!this || !data_to_hash || !efi_tcg_event) {
1033 ret = EFI_INVALID_PARAMETER;
1034 goto out;
1035 }
1036
1037 ret = platform_get_tpm2_device(&dev);
1038 if (ret != EFI_SUCCESS)
1039 goto out;
1040
1041 if (efi_tcg_event->size < efi_tcg_event->header.header_size +
1042 sizeof(u32)) {
1043 ret = EFI_INVALID_PARAMETER;
1044 goto out;
1045 }
1046
Masahisa Kojima538c0f22021-09-03 10:55:52 +09001047 if (efi_tcg_event->header.pcr_index > EFI_TCG2_MAX_PCR_INDEX) {
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001048 ret = EFI_INVALID_PARAMETER;
1049 goto out;
1050 }
1051
1052 /*
1053 * if PE_COFF_IMAGE is set we need to make sure the image is not
1054 * corrupted, verify it and hash the PE/COFF image in accordance with
Masahisa Kojima163a0d72021-05-26 12:09:58 +09001055 * the procedure specified in "Calculating the PE Image Hash"
1056 * section of the "Windows Authenticode Portable Executable Signature
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001057 * Format"
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001058 */
1059 if (flags & PE_COFF_IMAGE) {
Masahisa Kojima163a0d72021-05-26 12:09:58 +09001060 IMAGE_NT_HEADERS32 *nt;
1061
1062 ret = efi_check_pe((void *)(uintptr_t)data_to_hash,
1063 data_to_hash_len, (void **)&nt);
1064 if (ret != EFI_SUCCESS) {
1065 log_err("Not a valid PE-COFF file\n");
Masahisa Kojima580d7242021-09-03 10:55:50 +09001066 ret = EFI_UNSUPPORTED;
Masahisa Kojima163a0d72021-05-26 12:09:58 +09001067 goto out;
1068 }
1069 ret = tcg2_hash_pe_image((void *)(uintptr_t)data_to_hash,
1070 data_to_hash_len, &digest_list);
1071 } else {
1072 ret = tcg2_create_digest((u8 *)(uintptr_t)data_to_hash,
1073 data_to_hash_len, &digest_list);
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001074 }
1075
Masahisa Kojima163a0d72021-05-26 12:09:58 +09001076 if (ret != EFI_SUCCESS)
1077 goto out;
1078
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001079 pcr_index = efi_tcg_event->header.pcr_index;
1080 event_type = efi_tcg_event->header.event_type;
1081
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001082 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
1083 if (ret != EFI_SUCCESS)
1084 goto out;
1085
1086 if (flags & EFI_TCG2_EXTEND_ONLY) {
1087 if (event_log.truncated)
1088 ret = EFI_VOLUME_FULL;
1089 goto out;
1090 }
1091
1092 /*
1093 * The efi_tcg_event size includes the size component and the
1094 * headersize
1095 */
1096 event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) -
1097 efi_tcg_event->header.header_size;
1098 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
1099 event_size, efi_tcg_event->event);
1100out:
1101 return EFI_EXIT(ret);
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001102}
1103
1104/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +02001105 * efi_tcg2_submit_command() - Send command to the TPM
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001106 *
1107 * @this: TCG2 protocol instance
1108 * @input_param_block_size: size of the TPM input parameter block
1109 * @input_param_block: pointer to the TPM input parameter block
1110 * @output_param_block_size: size of the TPM output parameter block
1111 * @output_param_block: pointer to the TPM output parameter block
1112 *
1113 * Return: status code
1114 */
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001115static efi_status_t EFIAPI
Masahisa Kojima7fc93ca2021-11-04 22:59:16 +09001116efi_tcg2_submit_command(struct efi_tcg2_protocol *this,
1117 u32 input_param_block_size,
1118 u8 *input_param_block,
1119 u32 output_param_block_size,
1120 u8 *output_param_block)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001121{
Masahisa Kojima7fc93ca2021-11-04 22:59:16 +09001122 struct udevice *dev;
1123 efi_status_t ret;
1124 u32 rc;
1125 size_t resp_buf_size = output_param_block_size;
1126
1127 EFI_ENTRY("%p, %u, %p, %u, %p", this, input_param_block_size,
1128 input_param_block, output_param_block_size, output_param_block);
1129
1130 if (!this || !input_param_block || !input_param_block_size) {
1131 ret = EFI_INVALID_PARAMETER;
1132 goto out;
1133 }
1134
1135 ret = platform_get_tpm2_device(&dev);
1136 if (ret != EFI_SUCCESS)
1137 goto out;
1138
1139 rc = tpm2_submit_command(dev, input_param_block,
1140 output_param_block, &resp_buf_size);
1141 if (rc) {
1142 ret = (rc == -ENOSPC) ? EFI_OUT_OF_RESOURCES : EFI_DEVICE_ERROR;
1143
1144 goto out;
1145 }
1146
1147out:
1148 return EFI_EXIT(ret);
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001149}
1150
1151/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +02001152 * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001153 *
1154 * @this: TCG2 protocol instance
1155 * @active_pcr_banks: pointer for receiving the bitmap of currently
1156 * active PCR banks
1157 *
1158 * Return: status code
1159 */
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001160static efi_status_t EFIAPI
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +02001161efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
1162 u32 *active_pcr_banks)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001163{
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001164 efi_status_t ret;
1165
Masahisa Kojima580d7242021-09-03 10:55:50 +09001166 if (!this || !active_pcr_banks) {
1167 ret = EFI_INVALID_PARAMETER;
1168 goto out;
1169 }
1170
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001171 EFI_ENTRY("%p, %p", this, active_pcr_banks);
1172 ret = __get_active_pcr_banks(active_pcr_banks);
1173
Masahisa Kojima580d7242021-09-03 10:55:50 +09001174out:
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001175 return EFI_EXIT(ret);
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001176}
1177
1178/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +02001179 * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001180 *
1181 * @this: TCG2 protocol instance
1182 * @active_pcr_banks: bitmap of the requested active PCR banks
1183 *
1184 * Return: status code
1185 */
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001186static efi_status_t EFIAPI
Ilias Apalodimas1f6871d2021-05-25 14:35:31 +03001187efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
1188 u32 __maybe_unused active_pcr_banks)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001189{
1190 return EFI_UNSUPPORTED;
1191}
1192
1193/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +02001194 * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous
1195 * set_active_pcr_banks()
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001196 *
1197 * @this: TCG2 protocol instance
1198 * @operation_present: non-zero value to indicate a
1199 * set_active_pcr_banks operation was
1200 * invoked during last boot
1201 * @response: result value could be returned
1202 *
1203 * Return: status code
1204 */
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001205static efi_status_t EFIAPI
Ilias Apalodimas1f6871d2021-05-25 14:35:31 +03001206efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
1207 u32 __maybe_unused *operation_present,
1208 u32 __maybe_unused *response)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001209{
1210 return EFI_UNSUPPORTED;
1211}
1212
1213static const struct efi_tcg2_protocol efi_tcg2_protocol = {
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +02001214 .get_capability = efi_tcg2_get_capability,
1215 .get_eventlog = efi_tcg2_get_eventlog,
1216 .hash_log_extend_event = efi_tcg2_hash_log_extend_event,
1217 .submit_command = efi_tcg2_submit_command,
1218 .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks,
1219 .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks,
1220 .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks,
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001221};
1222
1223/**
Ruchika Gupta34287ef2021-11-29 13:09:44 +05301224 * parse_event_log_header() - Parse and verify the event log header fields
1225 *
1226 * @buffer: Pointer to the start of the eventlog
1227 * @size: Size of the eventlog
1228 * @pos: Return offset of the next event in buffer right
1229 * after the event header i.e specID
1230 *
1231 * Return: status code
1232 */
1233static efi_status_t parse_event_log_header(void *buffer, u32 size, u32 *pos)
1234{
1235 struct tcg_pcr_event *event_header = (struct tcg_pcr_event *)buffer;
1236 int i = 0;
1237
1238 if (size < sizeof(*event_header))
1239 return EFI_COMPROMISED_DATA;
1240
1241 if (get_unaligned_le32(&event_header->pcr_index) != 0 ||
1242 get_unaligned_le32(&event_header->event_type) != EV_NO_ACTION)
1243 return EFI_COMPROMISED_DATA;
1244
1245 for (i = 0; i < sizeof(event_header->digest); i++) {
1246 if (event_header->digest[i])
1247 return EFI_COMPROMISED_DATA;
1248 }
1249
1250 *pos += sizeof(*event_header);
1251
1252 return EFI_SUCCESS;
1253}
1254
1255/**
1256 * parse_specid_event() - Parse and verify the specID Event in the eventlog
1257 *
1258 * @dev: udevice
1259 * @buffer: Pointer to the start of the eventlog
1260 * @log_size: Size of the eventlog
1261 * @pos: [in] Offset of specID event in the eventlog buffer
1262 * [out] Return offset of the next event in the buffer
1263 * after the specID
1264 * @digest_list: list of digests in the event
1265 *
1266 * Return: status code
1267 * @pos Offset in the eventlog where the specID event ends
1268 * @digest_list: list of digests in the event
1269 */
1270static efi_status_t parse_specid_event(struct udevice *dev, void *buffer,
1271 u32 log_size, u32 *pos,
1272 struct tpml_digest_values *digest_list)
1273{
1274 struct tcg_efi_spec_id_event *spec_event;
1275 struct tcg_pcr_event *event_header = (struct tcg_pcr_event *)buffer;
1276 size_t spec_event_size;
1277 u32 active = 0, supported = 0, pcr_count = 0, alg_count = 0;
1278 u32 spec_active = 0;
1279 u16 hash_alg;
1280 u8 vendor_sz;
1281 int err, i;
1282
1283 if (*pos >= log_size || (*pos + sizeof(*spec_event)) > log_size)
1284 return EFI_COMPROMISED_DATA;
1285
1286 /* Check specID event data */
1287 spec_event = (struct tcg_efi_spec_id_event *)((uintptr_t)buffer + *pos);
1288 /* Check for signature */
1289 if (memcmp(spec_event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03,
1290 sizeof(TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03))) {
1291 log_err("specID Event: Signature mismatch\n");
1292 return EFI_COMPROMISED_DATA;
1293 }
1294
1295 if (spec_event->spec_version_minor !=
1296 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2 ||
1297 spec_event->spec_version_major !=
1298 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2)
1299 return EFI_COMPROMISED_DATA;
1300
1301 if (spec_event->number_of_algorithms > MAX_HASH_COUNT ||
1302 spec_event->number_of_algorithms < 1) {
1303 log_err("specID Event: Number of algorithms incorrect\n");
1304 return EFI_COMPROMISED_DATA;
1305 }
1306
1307 alg_count = spec_event->number_of_algorithms;
1308
1309 err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_count);
1310 if (err)
1311 return EFI_DEVICE_ERROR;
1312
1313 digest_list->count = 0;
1314 /*
1315 * We have to take care that the sequence of algorithms that we record
1316 * in digest_list matches the sequence in eventlog.
1317 */
1318 for (i = 0; i < alg_count; i++) {
1319 hash_alg =
1320 get_unaligned_le16(&spec_event->digest_sizes[i].algorithm_id);
1321
1322 if (!(supported & alg_to_mask(hash_alg))) {
1323 log_err("specID Event: Unsupported algorithm\n");
1324 return EFI_COMPROMISED_DATA;
1325 }
1326 digest_list->digests[digest_list->count++].hash_alg = hash_alg;
1327
1328 spec_active |= alg_to_mask(hash_alg);
1329 }
1330
1331 /*
1332 * TCG specification expects the event log to have hashes for all
1333 * active PCR's
1334 */
1335 if (spec_active != active) {
1336 /*
1337 * Previous stage bootloader should know all the active PCR's
1338 * and use them in the Eventlog.
1339 */
1340 log_err("specID Event: All active hash alg not present\n");
1341 return EFI_COMPROMISED_DATA;
1342 }
1343
1344 /*
1345 * the size of the spec event and placement of vendor_info_size
1346 * depends on supported algoriths
1347 */
1348 spec_event_size =
1349 offsetof(struct tcg_efi_spec_id_event, digest_sizes) +
1350 alg_count * sizeof(spec_event->digest_sizes[0]);
1351
1352 if (*pos + spec_event_size >= log_size)
1353 return EFI_COMPROMISED_DATA;
1354
1355 vendor_sz = *(uint8_t *)((uintptr_t)buffer + *pos + spec_event_size);
1356
1357 spec_event_size += sizeof(vendor_sz) + vendor_sz;
1358 *pos += spec_event_size;
1359
1360 if (get_unaligned_le32(&event_header->event_size) != spec_event_size) {
1361 log_err("specID event: header event size mismatch\n");
1362 /* Right way to handle this can be to call SetActive PCR's */
1363 return EFI_COMPROMISED_DATA;
1364 }
1365
1366 return EFI_SUCCESS;
1367}
1368
1369/**
1370 * tcg2_parse_event() - Parse the event in the eventlog
1371 *
1372 * @dev: udevice
1373 * @buffer: Pointer to the start of the eventlog
1374 * @log_size: Size of the eventlog
1375 * @offset: [in] Offset of the event in the eventlog buffer
1376 * [out] Return offset of the next event in the buffer
1377 * @digest_list: list of digests in the event
1378 * @pcr Index of the PCR in the event
1379 *
1380 * Return: status code
1381 */
1382static efi_status_t tcg2_parse_event(struct udevice *dev, void *buffer,
1383 u32 log_size, u32 *offset,
1384 struct tpml_digest_values *digest_list,
1385 u32 *pcr)
1386{
1387 struct tcg_pcr_event2 *event = NULL;
1388 u32 count, size, event_size;
1389 size_t pos;
1390
1391 event_size = tcg_event_final_size(digest_list);
1392 if (*offset >= log_size || *offset + event_size > log_size) {
1393 log_err("Event exceeds log size\n");
1394 return EFI_COMPROMISED_DATA;
1395 }
1396
1397 event = (struct tcg_pcr_event2 *)((uintptr_t)buffer + *offset);
1398 *pcr = get_unaligned_le32(&event->pcr_index);
1399
1400 /* get the count */
1401 count = get_unaligned_le32(&event->digests.count);
1402 if (count != digest_list->count)
1403 return EFI_COMPROMISED_DATA;
1404
1405 pos = offsetof(struct tcg_pcr_event2, digests);
1406 pos += offsetof(struct tpml_digest_values, digests);
1407
1408 for (int i = 0; i < digest_list->count; i++) {
1409 u16 alg;
1410 u16 hash_alg = digest_list->digests[i].hash_alg;
1411 u8 *digest = (u8 *)&digest_list->digests[i].digest;
1412
1413 alg = get_unaligned_le16((void *)((uintptr_t)event + pos));
1414
1415 if (alg != hash_alg)
1416 return EFI_COMPROMISED_DATA;
1417
1418 pos += offsetof(struct tpmt_ha, digest);
1419 memcpy(digest, (void *)((uintptr_t)event + pos), alg_to_len(hash_alg));
1420 pos += alg_to_len(hash_alg);
1421 }
1422
1423 size = get_unaligned_le32((void *)((uintptr_t)event + pos));
1424 event_size += size;
1425 pos += sizeof(u32); /* tcg_pcr_event2 event_size*/
1426 pos += size;
1427
1428 /* make sure the calculated buffer is what we checked against */
1429 if (pos != event_size)
1430 return EFI_COMPROMISED_DATA;
1431
1432 if (pos > log_size)
1433 return EFI_COMPROMISED_DATA;
1434
1435 *offset += pos;
1436
1437 return EFI_SUCCESS;
1438}
1439
1440/**
1441 * tcg2_get_fw_eventlog() - Get the eventlog address and size
1442 *
1443 * If the previous firmware has passed some eventlog, this function get it's
1444 * location and check for it's validity.
1445 *
1446 * @dev: udevice
1447 * @log_buffer: eventlog address
1448 * @log_sz: eventlog size
1449 *
1450 * Return: status code
1451 */
1452static efi_status_t tcg2_get_fw_eventlog(struct udevice *dev, void *log_buffer,
1453 size_t *log_sz)
1454{
1455 struct tpml_digest_values digest_list;
1456 void *buffer;
1457 efi_status_t ret;
1458 u32 pcr, pos;
1459 u64 base;
1460 u32 sz;
1461
1462 ret = platform_get_eventlog(dev, &base, &sz);
1463 if (ret != EFI_SUCCESS)
1464 return ret;
1465
1466 if (sz > TPM2_EVENT_LOG_SIZE)
1467 return EFI_VOLUME_FULL;
1468
1469 buffer = (void *)(uintptr_t)base;
1470 pos = 0;
1471 /* Parse the eventlog to check for its validity */
1472 ret = parse_event_log_header(buffer, sz, &pos);
1473 if (ret)
1474 return ret;
1475
1476 ret = parse_specid_event(dev, buffer, sz, &pos, &digest_list);
1477 if (ret) {
1478 log_err("Error parsing SPEC ID Event\n");
1479 return ret;
1480 }
1481
1482 while (pos < sz) {
1483 ret = tcg2_parse_event(dev, buffer, sz, &pos, &digest_list,
1484 &pcr);
1485 if (ret) {
1486 log_err("Error parsing event\n");
1487 return ret;
1488 }
1489 }
1490
1491 memcpy(log_buffer, buffer, sz);
1492 *log_sz = sz;
1493
1494 return ret;
1495}
1496
1497/**
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001498 * create_specid_event() - Create the first event in the eventlog
1499 *
1500 * @dev: tpm device
1501 * @event_header: Pointer to the final event header
1502 * @event_size: final spec event size
1503 *
1504 * Return: status code
1505 */
1506static efi_status_t create_specid_event(struct udevice *dev, void *buffer,
1507 size_t *event_size)
1508{
1509 struct tcg_efi_spec_id_event *spec_event;
1510 size_t spec_event_size;
1511 efi_status_t ret = EFI_DEVICE_ERROR;
Ruchika Gupta346cee32021-09-14 12:14:31 +05301512 u32 active = 0, supported = 0, pcr_count = 0, alg_count = 0;
Ilias Apalodimas1f6871d2021-05-25 14:35:31 +03001513 int err;
1514 size_t i;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001515
1516 /*
1517 * Create Spec event. This needs to be the first event in the log
1518 * according to the TCG EFI protocol spec
1519 */
1520
1521 /* Setup specID event data */
1522 spec_event = (struct tcg_efi_spec_id_event *)buffer;
1523 memcpy(spec_event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03,
1524 sizeof(spec_event->signature));
1525 put_unaligned_le32(0, &spec_event->platform_class); /* type client */
1526 spec_event->spec_version_minor =
1527 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2;
1528 spec_event->spec_version_major =
1529 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2;
1530 spec_event->spec_errata =
1531 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_ERRATA_TPM2;
1532 spec_event->uintn_size = sizeof(efi_uintn_t) / sizeof(u32);
1533
Ruchika Gupta346cee32021-09-14 12:14:31 +05301534 err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_count);
1535
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001536 if (err)
1537 goto out;
Ruchika Gupta346cee32021-09-14 12:14:31 +05301538
1539 for (i = 0; i < pcr_count; i++) {
1540 u16 hash_alg = hash_algo_list[i].hash_alg;
1541 u16 hash_len = hash_algo_list[i].hash_len;
1542
1543 if (active & alg_to_mask(hash_alg)) {
1544 put_unaligned_le16(hash_alg,
1545 &spec_event->digest_sizes[alg_count].algorithm_id);
1546 put_unaligned_le16(hash_len,
1547 &spec_event->digest_sizes[alg_count].digest_size);
1548 alg_count++;
1549 }
1550 }
1551
1552 spec_event->number_of_algorithms = alg_count;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001553 if (spec_event->number_of_algorithms > MAX_HASH_COUNT ||
1554 spec_event->number_of_algorithms < 1)
1555 goto out;
1556
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001557 /*
1558 * the size of the spec event and placement of vendor_info_size
1559 * depends on supported algoriths
1560 */
1561 spec_event_size =
1562 offsetof(struct tcg_efi_spec_id_event, digest_sizes) +
1563 spec_event->number_of_algorithms * sizeof(spec_event->digest_sizes[0]);
1564 /* no vendor info for us */
Ruchika Gupta346cee32021-09-14 12:14:31 +05301565 memset(buffer + spec_event_size, 0, 1);
1566 /* add a byte for vendor_info_size in the spec event */
1567 spec_event_size += 1;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001568 *event_size = spec_event_size;
1569
1570 return EFI_SUCCESS;
1571
1572out:
1573 return ret;
1574}
1575
1576/**
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001577 * tcg2_uninit - remove the final event table and free efi memory on failures
1578 */
1579void tcg2_uninit(void)
1580{
1581 efi_status_t ret;
1582
1583 ret = efi_install_configuration_table(&efi_guid_final_events, NULL);
1584 if (ret != EFI_SUCCESS)
1585 log_err("Failed to delete final events config table\n");
1586
1587 efi_free_pool(event_log.buffer);
1588 event_log.buffer = NULL;
1589 efi_free_pool(event_log.final_buffer);
1590 event_log.final_buffer = NULL;
1591}
1592
1593/**
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001594 * create_final_event() - Create the final event and install the config
1595 * defined by the TCG EFI spec
1596 */
1597static efi_status_t create_final_event(void)
1598{
1599 struct efi_tcg2_final_events_table *final_event;
1600 efi_status_t ret;
1601
1602 /*
1603 * All events generated after the invocation of
1604 * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an
1605 * EFI_CONFIGURATION_TABLE
1606 */
1607 ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE,
1608 &event_log.final_buffer);
1609 if (ret != EFI_SUCCESS)
1610 goto out;
1611
1612 memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE);
1613 final_event = event_log.final_buffer;
1614 final_event->number_of_events = 0;
1615 final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
1616 event_log.final_pos = sizeof(*final_event);
1617 ret = efi_install_configuration_table(&efi_guid_final_events,
1618 final_event);
Ilias Apalodimas20527592021-05-12 00:03:41 +03001619 if (ret != EFI_SUCCESS) {
1620 efi_free_pool(event_log.final_buffer);
1621 event_log.final_buffer = NULL;
1622 }
1623
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001624out:
1625 return ret;
1626}
1627
1628/**
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09001629 * tcg2_measure_event() - common function to add event log and extend PCR
1630 *
1631 * @dev: TPM device
1632 * @pcr_index: PCR index
1633 * @event_type: type of event added
1634 * @size: event size
1635 * @event: event data
1636 *
1637 * Return: status code
1638 */
1639static efi_status_t
1640tcg2_measure_event(struct udevice *dev, u32 pcr_index, u32 event_type,
1641 u32 size, u8 event[])
1642{
1643 struct tpml_digest_values digest_list;
1644 efi_status_t ret;
1645
1646 ret = tcg2_create_digest(event, size, &digest_list);
1647 if (ret != EFI_SUCCESS)
1648 goto out;
1649
1650 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
1651 if (ret != EFI_SUCCESS)
1652 goto out;
1653
1654 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
1655 size, event);
1656
1657out:
1658 return ret;
1659}
1660
1661/**
Ilias Apalodimasf69a2012021-03-24 16:50:46 +02001662 * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the
1663 * eventlog and extend the PCRs
1664 *
1665 * @dev: TPM device
1666 *
1667 * @Return: status code
1668 */
1669static efi_status_t efi_append_scrtm_version(struct udevice *dev)
1670{
Ilias Apalodimasf69a2012021-03-24 16:50:46 +02001671 efi_status_t ret;
1672
Pali Rohárfa9c5da2021-08-02 15:18:30 +02001673 ret = tcg2_measure_event(dev, 0, EV_S_CRTM_VERSION,
1674 strlen(version_string) + 1,
1675 (u8 *)version_string);
Ilias Apalodimasf69a2012021-03-24 16:50:46 +02001676
Ilias Apalodimasf69a2012021-03-24 16:50:46 +02001677 return ret;
1678}
1679
1680/**
Ruchika Gupta34287ef2021-11-29 13:09:44 +05301681 * efi_init_event_log() - initialize an eventlog
1682 *
1683 * Return: status code
1684 */
1685static efi_status_t efi_init_event_log(void)
1686{
1687 /*
1688 * vendor_info_size is currently set to 0, we need to change the length
1689 * and allocate the flexible array member if this changes
1690 */
1691 struct tcg_pcr_event *event_header = NULL;
1692 struct udevice *dev;
1693 size_t spec_event_size;
1694 efi_status_t ret;
1695
1696 ret = platform_get_tpm2_device(&dev);
1697 if (ret != EFI_SUCCESS)
1698 return ret;
1699
1700 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE,
1701 (void **)&event_log.buffer);
1702 if (ret != EFI_SUCCESS)
1703 return ret;
1704
1705 /*
1706 * initialize log area as 0xff so the OS can easily figure out the
1707 * last log entry
1708 */
1709 memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE);
1710
1711 /*
1712 * The log header is defined to be in SHA1 event log entry format.
1713 * Setup event header
1714 */
1715 event_header = (struct tcg_pcr_event *)event_log.buffer;
1716 event_log.pos = 0;
1717 event_log.last_event_size = 0;
1718 event_log.get_event_called = false;
1719 event_log.ebs_called = false;
1720 event_log.truncated = false;
1721
1722 /*
1723 * Check if earlier firmware have passed any eventlog. Different
1724 * platforms can use different ways to do so.
1725 */
1726 ret = tcg2_get_fw_eventlog(dev, event_log.buffer, &event_log.pos);
1727 /*
1728 * If earlier firmware hasn't passed any eventlog, go ahead and
1729 * create the eventlog header.
1730 */
1731 if (ret == EFI_NOT_FOUND) {
1732 put_unaligned_le32(0, &event_header->pcr_index);
1733 put_unaligned_le32(EV_NO_ACTION, &event_header->event_type);
1734 memset(&event_header->digest, 0, sizeof(event_header->digest));
1735 ret = create_specid_event(dev,
1736 (void *)((uintptr_t)event_log.buffer +
1737 sizeof(*event_header)),
1738 &spec_event_size);
1739 if (ret != EFI_SUCCESS)
1740 goto free_pool;
1741 put_unaligned_le32(spec_event_size, &event_header->event_size);
1742 event_log.pos = spec_event_size + sizeof(*event_header);
1743 event_log.last_event_size = event_log.pos;
1744
1745 /*
1746 * Add SCRTM version to the log if previous firmmware
1747 * doesn't pass an eventlog.
1748 */
1749 ret = efi_append_scrtm_version(dev);
1750 }
1751
1752 if (ret != EFI_SUCCESS)
1753 goto free_pool;
1754
1755 ret = create_final_event();
1756 if (ret != EFI_SUCCESS)
1757 goto free_pool;
1758
1759 return ret;
1760
1761free_pool:
1762 efi_free_pool(event_log.buffer);
1763 event_log.buffer = NULL;
1764 return ret;
1765}
1766
1767/**
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09001768 * tcg2_measure_variable() - add variable event log and extend PCR
1769 *
1770 * @dev: TPM device
1771 * @pcr_index: PCR index
1772 * @event_type: type of event added
1773 * @var_name: variable name
1774 * @guid: guid
1775 * @data_size: variable data size
1776 * @data: variable data
1777 *
1778 * Return: status code
1779 */
1780static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index,
Heinrich Schuchardtd47671c2021-09-09 07:12:14 +02001781 u32 event_type, const u16 *var_name,
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09001782 const efi_guid_t *guid,
1783 efi_uintn_t data_size, u8 *data)
1784{
1785 u32 event_size;
1786 efi_status_t ret;
1787 struct efi_tcg2_uefi_variable_data *event;
1788
1789 event_size = sizeof(event->variable_name) +
1790 sizeof(event->unicode_name_length) +
1791 sizeof(event->variable_data_length) +
1792 (u16_strlen(var_name) * sizeof(u16)) + data_size;
1793 event = malloc(event_size);
1794 if (!event)
1795 return EFI_OUT_OF_RESOURCES;
1796
1797 guidcpy(&event->variable_name, guid);
1798 event->unicode_name_length = u16_strlen(var_name);
1799 event->variable_data_length = data_size;
1800 memcpy(event->unicode_name, var_name,
1801 (event->unicode_name_length * sizeof(u16)));
1802 if (data) {
1803 memcpy((u16 *)event->unicode_name + event->unicode_name_length,
1804 data, data_size);
1805 }
1806 ret = tcg2_measure_event(dev, pcr_index, event_type, event_size,
1807 (u8 *)event);
1808 free(event);
1809 return ret;
1810}
1811
1812/**
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001813 * tcg2_measure_boot_variable() - measure boot variables
1814 *
1815 * @dev: TPM device
1816 *
1817 * Return: status code
1818 */
1819static efi_status_t tcg2_measure_boot_variable(struct udevice *dev)
1820{
1821 u16 *boot_order;
1822 u16 *boot_index;
1823 u16 var_name[] = L"BootOrder";
1824 u16 boot_name[] = L"Boot####";
1825 u8 *bootvar;
1826 efi_uintn_t var_data_size;
1827 u32 count, i;
1828 efi_status_t ret;
1829
1830 boot_order = efi_get_var(var_name, &efi_global_variable_guid,
1831 &var_data_size);
1832 if (!boot_order) {
Masahisa Kojimac9c1cdb2021-11-09 18:44:54 +09001833 /* If "BootOrder" is not defined, skip the boot variable measurement */
1834 return EFI_SUCCESS;
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001835 }
1836
1837 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name,
1838 &efi_global_variable_guid, var_data_size,
1839 (u8 *)boot_order);
1840 if (ret != EFI_SUCCESS)
1841 goto error;
1842
1843 count = var_data_size / sizeof(*boot_order);
1844 boot_index = boot_order;
1845 for (i = 0; i < count; i++) {
1846 efi_create_indexed_name(boot_name, sizeof(boot_name),
1847 "Boot", *boot_index++);
1848
1849 bootvar = efi_get_var(boot_name, &efi_global_variable_guid,
1850 &var_data_size);
1851
1852 if (!bootvar) {
Masahisa Kojima3961bd92021-11-09 20:35:53 +09001853 log_debug("%ls not found\n", boot_name);
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001854 continue;
1855 }
1856
1857 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2,
1858 boot_name,
1859 &efi_global_variable_guid,
1860 var_data_size, bootvar);
1861 free(bootvar);
1862 if (ret != EFI_SUCCESS)
1863 goto error;
1864 }
1865
1866error:
1867 free(boot_order);
1868 return ret;
1869}
1870
1871/**
Masahisa Kojima3d49ee82021-10-26 17:27:24 +09001872 * tcg2_measure_smbios() - measure smbios table
1873 *
1874 * @dev: TPM device
1875 * @entry: pointer to the smbios_entry structure
1876 *
1877 * Return: status code
1878 */
1879static efi_status_t
1880tcg2_measure_smbios(struct udevice *dev,
1881 const struct smbios_entry *entry)
1882{
1883 efi_status_t ret;
1884 struct smbios_header *smbios_copy;
1885 struct smbios_handoff_table_pointers2 *event = NULL;
1886 u32 event_size;
1887
1888 /*
1889 * TCG PC Client PFP Spec says
1890 * "SMBIOS structures that contain static configuration information
1891 * (e.g. Platform Manufacturer Enterprise Number assigned by IANA,
1892 * platform model number, Vendor and Device IDs for each SMBIOS table)
1893 * that is relevant to the security of the platform MUST be measured".
1894 * Device dependent parameters such as serial number are cleared to
1895 * zero or spaces for the measurement.
1896 */
1897 event_size = sizeof(struct smbios_handoff_table_pointers2) +
1898 FIELD_SIZEOF(struct efi_configuration_table, guid) +
1899 entry->struct_table_length;
1900 event = calloc(1, event_size);
1901 if (!event) {
1902 ret = EFI_OUT_OF_RESOURCES;
1903 goto out;
1904 }
1905
1906 event->table_description_size = sizeof(SMBIOS_HANDOFF_TABLE_DESC);
1907 memcpy(event->table_description, SMBIOS_HANDOFF_TABLE_DESC,
1908 sizeof(SMBIOS_HANDOFF_TABLE_DESC));
1909 put_unaligned_le64(1, &event->number_of_tables);
1910 guidcpy(&event->table_entry[0].guid, &smbios_guid);
1911 smbios_copy = (struct smbios_header *)((uintptr_t)&event->table_entry[0].table);
1912 memcpy(&event->table_entry[0].table,
1913 (void *)((uintptr_t)entry->struct_table_address),
1914 entry->struct_table_length);
1915
1916 smbios_prepare_measurement(entry, smbios_copy);
1917
1918 ret = tcg2_measure_event(dev, 1, EV_EFI_HANDOFF_TABLES2, event_size,
1919 (u8 *)event);
1920 if (ret != EFI_SUCCESS)
1921 goto out;
1922
1923out:
1924 free(event);
1925
1926 return ret;
1927}
1928
1929/**
1930 * find_smbios_table() - find smbios table
1931 *
1932 * Return: pointer to the smbios table
1933 */
1934static void *find_smbios_table(void)
1935{
1936 u32 i;
1937
1938 for (i = 0; i < systab.nr_tables; i++) {
1939 if (!guidcmp(&smbios_guid, &systab.tables[i].guid))
1940 return systab.tables[i].table;
1941 }
1942
1943 return NULL;
1944}
1945
1946/**
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09001947 * tcg2_measure_gpt_table() - measure gpt table
1948 *
1949 * @dev: TPM device
1950 * @loaded_image: handle to the loaded image
1951 *
1952 * Return: status code
1953 */
1954static efi_status_t
1955tcg2_measure_gpt_data(struct udevice *dev,
1956 struct efi_loaded_image_obj *loaded_image)
1957{
1958 efi_status_t ret;
1959 efi_handle_t handle;
1960 struct efi_handler *dp_handler;
1961 struct efi_device_path *orig_device_path;
1962 struct efi_device_path *device_path;
1963 struct efi_device_path *dp;
1964 struct efi_block_io *block_io;
1965 struct efi_gpt_data *event = NULL;
1966 efi_guid_t null_guid = NULL_GUID;
1967 gpt_header *gpt_h;
1968 gpt_entry *entry = NULL;
1969 gpt_entry *gpt_e;
1970 u32 num_of_valid_entry = 0;
1971 u32 event_size;
1972 u32 i;
1973 u32 total_gpt_entry_size;
1974
1975 ret = efi_search_protocol(&loaded_image->header,
1976 &efi_guid_loaded_image_device_path,
1977 &dp_handler);
1978 if (ret != EFI_SUCCESS)
1979 return ret;
1980
1981 orig_device_path = dp_handler->protocol_interface;
1982 if (!orig_device_path) /* no device path, skip GPT measurement */
1983 return EFI_SUCCESS;
1984
1985 device_path = efi_dp_dup(orig_device_path);
1986 if (!device_path)
1987 return EFI_OUT_OF_RESOURCES;
1988
1989 dp = search_gpt_dp_node(device_path);
1990 if (!dp) {
1991 /* no GPT device path node found, skip GPT measurement */
1992 ret = EFI_SUCCESS;
1993 goto out1;
1994 }
1995
1996 /* read GPT header */
1997 dp->type = DEVICE_PATH_TYPE_END;
1998 dp->sub_type = DEVICE_PATH_SUB_TYPE_END;
1999 dp = device_path;
2000 ret = EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_guid,
2001 &dp, &handle));
2002 if (ret != EFI_SUCCESS)
2003 goto out1;
2004
2005 ret = EFI_CALL(efi_handle_protocol(handle,
2006 &efi_block_io_guid, (void **)&block_io));
2007 if (ret != EFI_SUCCESS)
2008 goto out1;
2009
2010 gpt_h = memalign(block_io->media->io_align, block_io->media->block_size);
2011 if (!gpt_h) {
2012 ret = EFI_OUT_OF_RESOURCES;
2013 goto out2;
2014 }
2015
2016 ret = block_io->read_blocks(block_io, block_io->media->media_id, 1,
2017 block_io->media->block_size, gpt_h);
2018 if (ret != EFI_SUCCESS)
2019 goto out2;
2020
2021 /* read GPT entry */
2022 total_gpt_entry_size = gpt_h->num_partition_entries *
2023 gpt_h->sizeof_partition_entry;
2024 entry = memalign(block_io->media->io_align, total_gpt_entry_size);
2025 if (!entry) {
2026 ret = EFI_OUT_OF_RESOURCES;
2027 goto out2;
2028 }
2029
2030 ret = block_io->read_blocks(block_io, block_io->media->media_id,
2031 gpt_h->partition_entry_lba,
2032 total_gpt_entry_size, entry);
2033 if (ret != EFI_SUCCESS)
2034 goto out2;
2035
2036 /* count valid GPT entry */
2037 gpt_e = entry;
2038 for (i = 0; i < gpt_h->num_partition_entries; i++) {
2039 if (guidcmp(&null_guid, &gpt_e->partition_type_guid))
2040 num_of_valid_entry++;
2041
2042 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
2043 }
2044
2045 /* prepare event data for measurement */
2046 event_size = sizeof(struct efi_gpt_data) +
2047 (num_of_valid_entry * gpt_h->sizeof_partition_entry);
2048 event = calloc(1, event_size);
2049 if (!event) {
2050 ret = EFI_OUT_OF_RESOURCES;
2051 goto out2;
2052 }
2053 memcpy(event, gpt_h, sizeof(gpt_header));
2054 put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions);
2055
2056 /* copy valid GPT entry */
2057 gpt_e = entry;
2058 num_of_valid_entry = 0;
2059 for (i = 0; i < gpt_h->num_partition_entries; i++) {
2060 if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) {
2061 memcpy((u8 *)event->partitions +
2062 (num_of_valid_entry * gpt_h->sizeof_partition_entry),
2063 gpt_e, gpt_h->sizeof_partition_entry);
2064 num_of_valid_entry++;
2065 }
2066
2067 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
2068 }
2069
2070 ret = tcg2_measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *)event);
2071 if (ret != EFI_SUCCESS)
2072 goto out2;
2073
2074out2:
2075 EFI_CALL(efi_close_protocol((efi_handle_t)block_io, &efi_block_io_guid,
2076 NULL, NULL));
2077 free(gpt_h);
2078 free(entry);
2079 free(event);
2080out1:
2081 efi_free_pool(device_path);
2082
2083 return ret;
2084}
2085
2086/**
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09002087 * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation
2088 *
2089 * Return: status code
2090 */
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09002091efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_image_obj *handle)
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09002092{
2093 efi_status_t ret;
2094 u32 pcr_index;
2095 struct udevice *dev;
2096 u32 event = 0;
Masahisa Kojima3d49ee82021-10-26 17:27:24 +09002097 struct smbios_entry *entry;
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09002098
2099 if (tcg2_efi_app_invoked)
2100 return EFI_SUCCESS;
2101
2102 ret = platform_get_tpm2_device(&dev);
2103 if (ret != EFI_SUCCESS)
2104 return ret;
2105
2106 ret = tcg2_measure_boot_variable(dev);
2107 if (ret != EFI_SUCCESS)
2108 goto out;
2109
2110 ret = tcg2_measure_event(dev, 4, EV_EFI_ACTION,
2111 strlen(EFI_CALLING_EFI_APPLICATION),
2112 (u8 *)EFI_CALLING_EFI_APPLICATION);
2113 if (ret != EFI_SUCCESS)
2114 goto out;
2115
Masahisa Kojima3d49ee82021-10-26 17:27:24 +09002116 entry = (struct smbios_entry *)find_smbios_table();
2117 if (entry) {
2118 ret = tcg2_measure_smbios(dev, entry);
2119 if (ret != EFI_SUCCESS)
2120 goto out;
2121 }
2122
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09002123 ret = tcg2_measure_gpt_data(dev, handle);
2124 if (ret != EFI_SUCCESS)
2125 goto out;
2126
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09002127 for (pcr_index = 0; pcr_index <= 7; pcr_index++) {
2128 ret = tcg2_measure_event(dev, pcr_index, EV_SEPARATOR,
2129 sizeof(event), (u8 *)&event);
2130 if (ret != EFI_SUCCESS)
2131 goto out;
2132 }
2133
2134 tcg2_efi_app_invoked = true;
2135out:
2136 return ret;
2137}
2138
2139/**
2140 * efi_tcg2_measure_efi_app_exit() - measure efi app exit
2141 *
2142 * Return: status code
2143 */
2144efi_status_t efi_tcg2_measure_efi_app_exit(void)
2145{
2146 efi_status_t ret;
2147 struct udevice *dev;
2148
2149 ret = platform_get_tpm2_device(&dev);
2150 if (ret != EFI_SUCCESS)
2151 return ret;
2152
2153 ret = tcg2_measure_event(dev, 4, EV_EFI_ACTION,
2154 strlen(EFI_RETURNING_FROM_EFI_APPLICATION),
2155 (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION);
2156 return ret;
2157}
2158
2159/**
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09002160 * efi_tcg2_notify_exit_boot_services() - ExitBootService callback
2161 *
2162 * @event: callback event
2163 * @context: callback context
2164 */
2165static void EFIAPI
2166efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context)
2167{
2168 efi_status_t ret;
2169 struct udevice *dev;
2170
2171 EFI_ENTRY("%p, %p", event, context);
2172
Ilias Apalodimas5ba03972021-11-18 09:03:39 +02002173 event_log.ebs_called = true;
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09002174 ret = platform_get_tpm2_device(&dev);
2175 if (ret != EFI_SUCCESS)
2176 goto out;
2177
2178 ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
2179 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
2180 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
2181 if (ret != EFI_SUCCESS)
2182 goto out;
2183
2184 ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
2185 strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED),
2186 (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED);
2187
2188out:
2189 EFI_EXIT(ret);
2190}
2191
2192/**
2193 * efi_tcg2_notify_exit_boot_services_failed()
2194 * - notify ExitBootServices() is failed
2195 *
2196 * Return: status code
2197 */
2198efi_status_t efi_tcg2_notify_exit_boot_services_failed(void)
2199{
2200 struct udevice *dev;
2201 efi_status_t ret;
2202
2203 ret = platform_get_tpm2_device(&dev);
2204 if (ret != EFI_SUCCESS)
2205 goto out;
2206
2207 ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
2208 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
2209 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
2210 if (ret != EFI_SUCCESS)
2211 goto out;
2212
2213 ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
2214 strlen(EFI_EXIT_BOOT_SERVICES_FAILED),
2215 (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED);
2216
2217out:
2218 return ret;
2219}
2220
2221/**
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09002222 * tcg2_measure_secure_boot_variable() - measure secure boot variables
2223 *
2224 * @dev: TPM device
2225 *
2226 * Return: status code
2227 */
2228static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev)
2229{
2230 u8 *data;
2231 efi_uintn_t data_size;
2232 u32 count, i;
2233 efi_status_t ret;
Masahisa Kojima65aa2592021-10-26 17:27:27 +09002234 u8 deployed_mode;
2235 efi_uintn_t size;
2236 u32 deployed_audit_pcr_index = 1;
2237
2238 size = sizeof(deployed_mode);
2239 ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid,
2240 NULL, &size, &deployed_mode, NULL);
2241 if (ret != EFI_SUCCESS || !deployed_mode)
2242 deployed_audit_pcr_index = 7;
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09002243
2244 count = ARRAY_SIZE(secure_variables);
2245 for (i = 0; i < count; i++) {
Heinrich Schuchardta45dac12021-09-09 08:50:01 +02002246 const efi_guid_t *guid;
2247
Masahisa Kojima96485d22021-10-26 17:27:26 +09002248 guid = efi_auth_var_get_guid(secure_variables[i].name);
Heinrich Schuchardta45dac12021-09-09 08:50:01 +02002249
Masahisa Kojima96485d22021-10-26 17:27:26 +09002250 data = efi_get_var(secure_variables[i].name, guid, &data_size);
2251 if (!data && !secure_variables[i].accept_empty)
2252 continue;
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09002253
Masahisa Kojima65aa2592021-10-26 17:27:27 +09002254 if (u16_strcmp(u"DeployedMode", secure_variables[i].name))
2255 secure_variables[i].pcr_index = deployed_audit_pcr_index;
2256 if (u16_strcmp(u"AuditMode", secure_variables[i].name))
2257 secure_variables[i].pcr_index = deployed_audit_pcr_index;
2258
2259 ret = tcg2_measure_variable(dev, secure_variables[i].pcr_index,
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09002260 EV_EFI_VARIABLE_DRIVER_CONFIG,
Masahisa Kojima96485d22021-10-26 17:27:26 +09002261 secure_variables[i].name, guid,
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09002262 data_size, data);
2263 free(data);
2264 if (ret != EFI_SUCCESS)
2265 goto error;
2266 }
2267
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09002268error:
2269 return ret;
2270}
2271
2272/**
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02002273 * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
2274 *
2275 * If a TPM2 device is available, the TPM TCG2 Protocol is registered
2276 *
2277 * Return: An error status is only returned if adding the protocol fails.
2278 */
2279efi_status_t efi_tcg2_register(void)
2280{
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02002281 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02002282 struct udevice *dev;
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09002283 struct efi_event *event;
Ilias Apalodimasd6b55a42021-11-18 10:13:42 +02002284 u32 err;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02002285
2286 ret = platform_get_tpm2_device(&dev);
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +02002287 if (ret != EFI_SUCCESS) {
2288 log_warning("Unable to find TPMv2 device\n");
Ilias Apalodimas97f446a2021-05-10 21:19:14 +03002289 return EFI_SUCCESS;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02002290 }
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02002291
Ilias Apalodimasd6b55a42021-11-18 10:13:42 +02002292 /* initialize the TPM as early as possible. */
2293 err = tpm_startup(dev, TPM_ST_CLEAR);
2294 if (err) {
2295 log_err("TPM startup failed\n");
2296 goto fail;
2297 }
2298
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02002299 ret = efi_init_event_log();
2300 if (ret != EFI_SUCCESS)
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02002301 goto fail;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02002302
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02002303 ret = efi_add_protocol(efi_root, &efi_guid_tcg2_protocol,
2304 (void *)&efi_tcg2_protocol);
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02002305 if (ret != EFI_SUCCESS) {
Ilias Apalodimas20527592021-05-12 00:03:41 +03002306 tcg2_uninit();
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02002307 goto fail;
2308 }
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09002309
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09002310 ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
2311 efi_tcg2_notify_exit_boot_services, NULL,
2312 NULL, &event);
2313 if (ret != EFI_SUCCESS) {
2314 tcg2_uninit();
2315 goto fail;
2316 }
2317
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09002318 ret = tcg2_measure_secure_boot_variable(dev);
2319 if (ret != EFI_SUCCESS) {
2320 tcg2_uninit();
2321 goto fail;
2322 }
2323
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02002324 return ret;
Ilias Apalodimas97f446a2021-05-10 21:19:14 +03002325
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02002326fail:
Ilias Apalodimas20527592021-05-12 00:03:41 +03002327 log_err("Cannot install EFI_TCG2_PROTOCOL\n");
2328 /*
2329 * Return EFI_SUCCESS and don't stop the EFI subsystem.
2330 * That's done for 2 reasons
2331 * - If the protocol is not installed the PCRs won't be extended. So
2332 * someone later in the boot flow will notice that and take the
2333 * necessary actions.
2334 * - The TPM sandbox is limited and we won't be able to run any efi
2335 * related tests with TCG2 enabled
2336 */
2337 return EFI_SUCCESS;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02002338}