blob: 51264c1b998c2b62a347af9ea8498559285a0bdd [file] [log] [blame]
Ilias Apalodimas590fef62020-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
Ilias Apalodimas590fef62020-11-11 11:18:11 +020011#include <dm.h>
12#include <efi_loader.h>
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +020013#include <efi_variable.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020014#include <efi_tcg2.h>
15#include <log.h>
Masahisa Kojima70be5a62021-05-26 12:09:58 +090016#include <malloc.h>
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +090017#include <smbios.h>
Pali Rohárba87ddf2021-08-02 15:18:31 +020018#include <version_string.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020019#include <tpm-v2.h>
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +020020#include <tpm_api.h>
Masahisa Kojima70be5a62021-05-26 12:09:58 +090021#include <u-boot/hash-checksum.h>
Masahisa Kojimad420d8d2021-11-03 11:04:09 +090022#include <linux/unaligned/be_byteshift.h>
23#include <linux/unaligned/le_byteshift.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020024#include <linux/unaligned/generic.h>
Ilias Apalodimas967650d2020-11-30 11:47:40 +020025#include <hexdump.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020026
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020027/**
28 * struct event_log_buffer - internal eventlog management structure
29 *
30 * @buffer: eventlog buffer
31 * @final_buffer: finalevent config table buffer
32 * @pos: current position of 'buffer'
33 * @final_pos: current position of 'final_buffer'
34 * @get_event_called: true if GetEventLog has been invoked at least once
35 * @ebs_called: true if ExitBootServices has been invoked
36 * @truncated: true if the 'buffer' is truncated
37 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +020038struct event_log_buffer {
39 void *buffer;
40 void *final_buffer;
41 size_t pos; /* eventlog position */
42 size_t final_pos; /* final events config table position */
43 size_t last_event_size;
44 bool get_event_called;
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020045 bool ebs_called;
Ilias Apalodimas967650d2020-11-30 11:47:40 +020046 bool truncated;
47};
Ilias Apalodimas590fef62020-11-11 11:18:11 +020048
Ilias Apalodimas967650d2020-11-30 11:47:40 +020049static struct event_log_buffer event_log;
Masahisa Kojima8173cd42021-08-13 16:12:40 +090050static bool tcg2_efi_app_invoked;
Ilias Apalodimas590fef62020-11-11 11:18:11 +020051/*
52 * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset.
53 * Since the current tpm2_get_capability() response buffers starts at
54 * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate
55 * the response size and offset once for all consumers
56 */
57#define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \
58 offsetof(struct tpms_capability_data, data))
59#define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \
60 offsetof(struct tpms_tagged_property, value))
61
Ilias Apalodimas967650d2020-11-30 11:47:40 +020062static const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID;
63static const efi_guid_t efi_guid_final_events = EFI_TCG2_FINAL_EVENTS_TABLE_GUID;
64
Masahisa Kojima21684522021-10-26 17:27:26 +090065struct variable_info {
66 const u16 *name;
67 bool accept_empty;
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +090068 u32 pcr_index;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +090069};
70
Masahisa Kojima21684522021-10-26 17:27:26 +090071static struct variable_info secure_variables[] = {
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +090072 {u"SecureBoot", true, 7},
73 {u"PK", true, 7},
74 {u"KEK", true, 7},
75 {u"db", true, 7},
76 {u"dbx", true, 7},
77 {u"dbt", false, 7},
78 {u"dbr", false, 7},
79 {u"DeployedMode", false, 1},
80 {u"AuditMode", false, 1},
Masahisa Kojima21684522021-10-26 17:27:26 +090081};
82
Masahisa Kojima0fd43792021-12-07 14:15:31 +090083static bool is_tcg2_protocol_installed(void)
84{
85 struct efi_handler *handler;
86 efi_status_t ret;
87
88 ret = efi_search_protocol(efi_root, &efi_guid_tcg2_protocol, &handler);
89 return ret == EFI_SUCCESS;
90}
91
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020092/* tcg2_agile_log_append - Append an agile event to an eventlog
93 *
94 * @pcr_index: PCR index
95 * @event_type: type of event added
96 * @digest_list: list of digest algorithms to add
97 * @size: size of event
98 * @event: event to add
99 * @log: log buffer to append the event
100 *
101 * @Return: status code
102 */
103static efi_status_t tcg2_agile_log_append(u32 pcr_index, u32 event_type,
104 struct tpml_digest_values *digest_list,
105 u32 size, u8 event[])
106{
107 void *log = (void *)((uintptr_t)event_log.buffer + event_log.pos);
Eddie James8ed7bb32023-10-24 10:43:49 -0500108 u32 event_size = size + tcg2_event_get_size(digest_list);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200109 struct efi_tcg2_final_events_table *final_event;
110 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200111
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200112 /* if ExitBootServices hasn't been called update the normal log */
113 if (!event_log.ebs_called) {
114 if (event_log.truncated ||
115 event_log.pos + event_size > TPM2_EVENT_LOG_SIZE) {
116 event_log.truncated = true;
117 return EFI_VOLUME_FULL;
118 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500119 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200120 event_log.pos += event_size;
121 event_log.last_event_size = event_size;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200122 }
123
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200124 if (!event_log.get_event_called)
125 return ret;
126
127 /* if GetEventLog has been called update FinalEventLog as well */
128 if (event_log.final_pos + event_size > TPM2_EVENT_LOG_SIZE)
129 return EFI_VOLUME_FULL;
130
131 log = (void *)((uintptr_t)event_log.final_buffer + event_log.final_pos);
Eddie James8ed7bb32023-10-24 10:43:49 -0500132 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200133
134 final_event = event_log.final_buffer;
135 final_event->number_of_events++;
136 event_log.final_pos += event_size;
137
138 return ret;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200139}
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200140
141/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200142 * tpm2_get_max_command_size() - get the supported max command size
143 *
144 * @dev: TPM device
145 * @max_command_size: output buffer for the size
146 *
147 * Return: 0 on success, -1 on error
148 */
149static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size)
150{
151 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
152 u32 ret;
153
154 memset(response, 0, sizeof(response));
155 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
156 TPM2_PT_MAX_COMMAND_SIZE, response, 1);
157 if (ret)
158 return -1;
159
160 *max_command_size = (uint16_t)get_unaligned_be32(response +
161 properties_offset);
162
163 return 0;
164}
165
166/**
167 * tpm2_get_max_response_size() - get the supported max response size
168 *
169 * @dev: TPM device
170 * @max_response_size: output buffer for the size
171 *
172 * Return: 0 on success, -1 on error
173 */
174static int tpm2_get_max_response_size(struct udevice *dev,
175 u16 *max_response_size)
176{
177 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
178 u32 ret;
179
180 memset(response, 0, sizeof(response));
181 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
182 TPM2_PT_MAX_RESPONSE_SIZE, response, 1);
183 if (ret)
184 return -1;
185
186 *max_response_size = (uint16_t)get_unaligned_be32(response +
187 properties_offset);
188
189 return 0;
190}
191
192/**
193 * tpm2_get_manufacturer_id() - get the manufacturer ID
194 *
195 * @dev: TPM device
196 * @manufacturer_id: output buffer for the id
197 *
198 * Return: 0 on success, -1 on error
199 */
200static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id)
201{
202 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
203 u32 ret;
204
205 memset(response, 0, sizeof(response));
206 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
207 TPM2_PT_MANUFACTURER, response, 1);
208 if (ret)
209 return -1;
210
211 *manufacturer_id = get_unaligned_be32(response + properties_offset);
212
213 return 0;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200214}
215
216/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200217 * efi_tcg2_get_capability() - protocol capability information and state information
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200218 *
219 * @this: TCG2 protocol instance
220 * @capability: caller allocated memory with size field to the size of
221 * the structure allocated
222
223 * Return: status code
224 */
225static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200226efi_tcg2_get_capability(struct efi_tcg2_protocol *this,
227 struct efi_tcg2_boot_service_capability *capability)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200228{
229 struct udevice *dev;
230 efi_status_t efi_ret;
231 int ret;
232
233 EFI_ENTRY("%p, %p", this, capability);
234
235 if (!this || !capability) {
236 efi_ret = EFI_INVALID_PARAMETER;
237 goto out;
238 }
239
Masahisa Kojima9cc82932021-09-06 12:04:12 +0900240 if (capability->size < BOOT_SERVICE_CAPABILITY_MIN) {
241 capability->size = BOOT_SERVICE_CAPABILITY_MIN;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200242 efi_ret = EFI_BUFFER_TOO_SMALL;
243 goto out;
244 }
245
246 if (capability->size < sizeof(*capability)) {
247 capability->size = sizeof(*capability);
248 efi_ret = EFI_BUFFER_TOO_SMALL;
249 goto out;
250 }
251
252 capability->structure_version.major = 1;
253 capability->structure_version.minor = 1;
254 capability->protocol_version.major = 1;
255 capability->protocol_version.minor = 1;
256
Eddie James8ed7bb32023-10-24 10:43:49 -0500257 efi_ret = tcg2_platform_get_tpm2(&dev);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200258 if (efi_ret != EFI_SUCCESS) {
259 capability->supported_event_logs = 0;
260 capability->hash_algorithm_bitmap = 0;
261 capability->tpm_present_flag = false;
262 capability->max_command_size = 0;
263 capability->max_response_size = 0;
264 capability->manufacturer_id = 0;
265 capability->number_of_pcr_banks = 0;
266 capability->active_pcr_banks = 0;
267
268 efi_ret = EFI_SUCCESS;
269 goto out;
270 }
271
272 /* We only allow a TPMv2 device to register the EFI protocol */
273 capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2;
274
275 capability->tpm_present_flag = true;
276
277 /* Supported and active PCRs */
278 capability->hash_algorithm_bitmap = 0;
279 capability->active_pcr_banks = 0;
280 ret = tpm2_get_pcr_info(dev, &capability->hash_algorithm_bitmap,
281 &capability->active_pcr_banks,
282 &capability->number_of_pcr_banks);
283 if (ret) {
284 efi_ret = EFI_DEVICE_ERROR;
285 goto out;
286 }
287
288 /* Max command size */
289 ret = tpm2_get_max_command_size(dev, &capability->max_command_size);
290 if (ret) {
291 efi_ret = EFI_DEVICE_ERROR;
292 goto out;
293 }
294
295 /* Max response size */
296 ret = tpm2_get_max_response_size(dev, &capability->max_response_size);
297 if (ret) {
298 efi_ret = EFI_DEVICE_ERROR;
299 goto out;
300 }
301
302 /* Manufacturer ID */
303 ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id);
304 if (ret) {
305 efi_ret = EFI_DEVICE_ERROR;
306 goto out;
307 }
308
309 return EFI_EXIT(EFI_SUCCESS);
310out:
311 return EFI_EXIT(efi_ret);
312}
313
314/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200315 * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its
316 * last entry
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200317 *
318 * @this: TCG2 protocol instance
319 * @log_format: type of event log format
320 * @event_log_location: pointer to the memory address of the event log
321 * @event_log_last_entry: pointer to the address of the start of the last
322 * entry in the event log in memory, if log contains
323 * more than 1 entry
324 * @event_log_truncated: set to true, if the Event Log is missing at i
325 * least one entry
326 *
327 * Return: status code
328 */
329static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200330efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this,
331 efi_tcg_event_log_format log_format,
332 u64 *event_log_location, u64 *event_log_last_entry,
333 bool *event_log_truncated)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200334{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200335 efi_status_t ret = EFI_SUCCESS;
336 struct udevice *dev;
337
338 EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location,
339 event_log_last_entry, event_log_truncated);
340
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900341 if (!this || !event_log_location || !event_log_last_entry ||
342 !event_log_truncated) {
343 ret = EFI_INVALID_PARAMETER;
344 goto out;
345 }
346
347 /* Only support TPMV2 */
348 if (log_format != TCG2_EVENT_LOG_FORMAT_TCG_2) {
349 ret = EFI_INVALID_PARAMETER;
350 goto out;
351 }
352
Eddie James8ed7bb32023-10-24 10:43:49 -0500353 ret = tcg2_platform_get_tpm2(&dev);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200354 if (ret != EFI_SUCCESS) {
355 event_log_location = NULL;
356 event_log_last_entry = NULL;
357 *event_log_truncated = false;
358 ret = EFI_SUCCESS;
359 goto out;
360 }
361 *event_log_location = (uintptr_t)event_log.buffer;
362 *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos -
363 event_log.last_event_size);
364 *event_log_truncated = event_log.truncated;
365 event_log.get_event_called = true;
366
367out:
368 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200369}
370
371/**
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900372 * tcg2_hash_pe_image() - calculate PE/COFF image hash
373 *
374 * @efi: pointer to the EFI binary
375 * @efi_size: size of @efi binary
376 * @digest_list: list of digest algorithms to extend
377 *
378 * Return: status code
379 */
380static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size,
381 struct tpml_digest_values *digest_list)
382{
383 WIN_CERTIFICATE *wincerts = NULL;
384 size_t wincerts_len;
385 struct efi_image_regions *regs = NULL;
386 void *new_efi = NULL;
387 u8 hash[TPM2_SHA512_DIGEST_SIZE];
Eddie James8ed7bb32023-10-24 10:43:49 -0500388 struct udevice *dev;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900389 efi_status_t ret;
390 u32 active;
391 int i;
392
393 new_efi = efi_prepare_aligned_image(efi, &efi_size);
394 if (!new_efi)
395 return EFI_OUT_OF_RESOURCES;
396
397 if (!efi_image_parse(new_efi, efi_size, &regs, &wincerts,
398 &wincerts_len)) {
399 log_err("Parsing PE executable image failed\n");
400 ret = EFI_UNSUPPORTED;
401 goto out;
402 }
403
Eddie James8ed7bb32023-10-24 10:43:49 -0500404 ret = tcg2_platform_get_tpm2(&dev);
405 if (ret != EFI_SUCCESS)
406 goto out;
407
408 ret = tcg2_get_active_pcr_banks(dev, &active);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900409 if (ret != EFI_SUCCESS) {
410 goto out;
411 }
412
413 digest_list->count = 0;
Tim Harvey6ea1e052024-05-25 13:00:48 -0700414 for (i = 0; i < ARRAY_SIZE(hash_algo_list); i++) {
415 u16 hash_alg = hash_algo_list[i].hash_alg;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900416
Tim Harvey6ea1e052024-05-25 13:00:48 -0700417 if (!(active & hash_algo_list[i].hash_mask))
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900418 continue;
419 switch (hash_alg) {
420 case TPM2_ALG_SHA1:
421 hash_calculate("sha1", regs->reg, regs->num, hash);
422 break;
423 case TPM2_ALG_SHA256:
424 hash_calculate("sha256", regs->reg, regs->num, hash);
425 break;
426 case TPM2_ALG_SHA384:
427 hash_calculate("sha384", regs->reg, regs->num, hash);
428 break;
429 case TPM2_ALG_SHA512:
430 hash_calculate("sha512", regs->reg, regs->num, hash);
431 break;
432 default:
Heinrich Schuchardt09ec9f72023-07-31 14:11:34 +0200433 continue;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900434 }
Ruchika Guptae53007b2021-09-14 12:14:31 +0530435 digest_list->digests[digest_list->count].hash_alg = hash_alg;
436 memcpy(&digest_list->digests[digest_list->count].digest, hash,
Eddie James8ed7bb32023-10-24 10:43:49 -0500437 (u32)tpm2_algorithm_to_len(hash_alg));
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900438 digest_list->count++;
439 }
440
441out:
442 if (new_efi != efi)
443 free(new_efi);
444 free(regs);
445
446 return ret;
447}
448
449/**
450 * tcg2_measure_pe_image() - measure PE/COFF image
451 *
452 * @efi: pointer to the EFI binary
453 * @efi_size: size of @efi binary
454 * @handle: loaded image handle
455 * @loaded_image: loaded image protocol
456 *
457 * Return: status code
458 */
459efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size,
460 struct efi_loaded_image_obj *handle,
461 struct efi_loaded_image *loaded_image)
462{
463 struct tpml_digest_values digest_list;
464 efi_status_t ret;
465 struct udevice *dev;
466 u32 pcr_index, event_type, event_size;
467 struct uefi_image_load_event *image_load_event;
468 struct efi_device_path *device_path;
469 u32 device_path_length;
470 IMAGE_DOS_HEADER *dos;
471 IMAGE_NT_HEADERS32 *nt;
472 struct efi_handler *handler;
473
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +0900474 if (!is_tcg2_protocol_installed())
475 return EFI_SUCCESS;
476
Eddie James8ed7bb32023-10-24 10:43:49 -0500477 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900478 if (ret != EFI_SUCCESS)
Masahisa Kojima38155ea2021-12-07 14:15:33 +0900479 return EFI_SECURITY_VIOLATION;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900480
481 switch (handle->image_type) {
482 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
483 pcr_index = 4;
484 event_type = EV_EFI_BOOT_SERVICES_APPLICATION;
485 break;
486 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
487 pcr_index = 2;
488 event_type = EV_EFI_BOOT_SERVICES_DRIVER;
489 break;
490 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
491 pcr_index = 2;
492 event_type = EV_EFI_RUNTIME_SERVICES_DRIVER;
493 break;
494 default:
495 return EFI_UNSUPPORTED;
496 }
497
498 ret = tcg2_hash_pe_image(efi, efi_size, &digest_list);
499 if (ret != EFI_SUCCESS)
500 return ret;
501
502 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
503 if (ret != EFI_SUCCESS)
504 return ret;
505
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300506 ret = efi_search_protocol(&handle->header,
507 &efi_guid_loaded_image_device_path, &handler);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900508 if (ret != EFI_SUCCESS)
509 return ret;
510
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300511 device_path = handler->protocol_interface;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900512 device_path_length = efi_dp_size(device_path);
513 if (device_path_length > 0) {
514 /* add end node size */
515 device_path_length += sizeof(struct efi_device_path);
516 }
517 event_size = sizeof(struct uefi_image_load_event) + device_path_length;
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300518 image_load_event = calloc(1, event_size);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900519 if (!image_load_event)
520 return EFI_OUT_OF_RESOURCES;
521
522 image_load_event->image_location_in_memory = (uintptr_t)efi;
523 image_load_event->image_length_in_memory = efi_size;
524 image_load_event->length_of_device_path = device_path_length;
525
526 dos = (IMAGE_DOS_HEADER *)efi;
527 nt = (IMAGE_NT_HEADERS32 *)(efi + dos->e_lfanew);
528 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
529 IMAGE_NT_HEADERS64 *nt64 = (IMAGE_NT_HEADERS64 *)nt;
530
531 image_load_event->image_link_time_address =
532 nt64->OptionalHeader.ImageBase;
533 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
534 image_load_event->image_link_time_address =
535 nt->OptionalHeader.ImageBase;
536 } else {
537 ret = EFI_INVALID_PARAMETER;
538 goto out;
539 }
540
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300541 /* device_path_length might be zero */
542 memcpy(image_load_event->device_path, device_path, device_path_length);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900543
544 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
545 event_size, (u8 *)image_load_event);
546
547out:
548 free(image_load_event);
549
550 return ret;
551}
552
553/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200554 * efi_tcg2_hash_log_extend_event() - extend and optionally log events
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200555 *
556 * @this: TCG2 protocol instance
557 * @flags: bitmap providing additional information on the
558 * operation
559 * @data_to_hash: physical address of the start of the data buffer
560 * to be hashed
561 * @data_to_hash_len: the length in bytes of the buffer referenced by
562 * data_to_hash
563 * @efi_tcg_event: pointer to data buffer containing information
564 * about the event
565 *
566 * Return: status code
567 */
568static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200569efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags,
570 u64 data_to_hash, u64 data_to_hash_len,
571 struct efi_tcg2_event *efi_tcg_event)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200572{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200573 struct udevice *dev;
574 efi_status_t ret;
575 u32 event_type, pcr_index, event_size;
576 struct tpml_digest_values digest_list;
577
578 EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash,
579 data_to_hash_len, efi_tcg_event);
580
581 if (!this || !data_to_hash || !efi_tcg_event) {
582 ret = EFI_INVALID_PARAMETER;
583 goto out;
584 }
585
Eddie James8ed7bb32023-10-24 10:43:49 -0500586 ret = tcg2_platform_get_tpm2(&dev);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200587 if (ret != EFI_SUCCESS)
588 goto out;
589
590 if (efi_tcg_event->size < efi_tcg_event->header.header_size +
591 sizeof(u32)) {
592 ret = EFI_INVALID_PARAMETER;
593 goto out;
594 }
595
Masahisa Kojimab8074912021-09-03 10:55:52 +0900596 if (efi_tcg_event->header.pcr_index > EFI_TCG2_MAX_PCR_INDEX) {
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200597 ret = EFI_INVALID_PARAMETER;
598 goto out;
599 }
600
601 /*
602 * if PE_COFF_IMAGE is set we need to make sure the image is not
603 * corrupted, verify it and hash the PE/COFF image in accordance with
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900604 * the procedure specified in "Calculating the PE Image Hash"
605 * section of the "Windows Authenticode Portable Executable Signature
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200606 * Format"
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200607 */
608 if (flags & PE_COFF_IMAGE) {
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900609 IMAGE_NT_HEADERS32 *nt;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200610
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900611 ret = efi_check_pe((void *)(uintptr_t)data_to_hash,
612 data_to_hash_len, (void **)&nt);
613 if (ret != EFI_SUCCESS) {
614 log_err("Not a valid PE-COFF file\n");
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900615 ret = EFI_UNSUPPORTED;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900616 goto out;
617 }
618 ret = tcg2_hash_pe_image((void *)(uintptr_t)data_to_hash,
619 data_to_hash_len, &digest_list);
620 } else {
Eddie James8ed7bb32023-10-24 10:43:49 -0500621 ret = tcg2_create_digest(dev, (u8 *)(uintptr_t)data_to_hash,
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900622 data_to_hash_len, &digest_list);
623 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200624
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200625 if (ret != EFI_SUCCESS)
626 goto out;
627
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900628 pcr_index = efi_tcg_event->header.pcr_index;
629 event_type = efi_tcg_event->header.event_type;
630
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200631 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
632 if (ret != EFI_SUCCESS)
633 goto out;
634
635 if (flags & EFI_TCG2_EXTEND_ONLY) {
636 if (event_log.truncated)
637 ret = EFI_VOLUME_FULL;
638 goto out;
639 }
640
641 /*
642 * The efi_tcg_event size includes the size component and the
643 * headersize
644 */
645 event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) -
646 efi_tcg_event->header.header_size;
647 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
648 event_size, efi_tcg_event->event);
649out:
650 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200651}
652
653/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200654 * efi_tcg2_submit_command() - Send command to the TPM
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200655 *
656 * @this: TCG2 protocol instance
657 * @input_param_block_size: size of the TPM input parameter block
658 * @input_param_block: pointer to the TPM input parameter block
659 * @output_param_block_size: size of the TPM output parameter block
660 * @output_param_block: pointer to the TPM output parameter block
661 *
662 * Return: status code
663 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200664static efi_status_t EFIAPI
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900665efi_tcg2_submit_command(struct efi_tcg2_protocol *this,
666 u32 input_param_block_size,
667 u8 *input_param_block,
668 u32 output_param_block_size,
669 u8 *output_param_block)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200670{
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900671 struct udevice *dev;
672 efi_status_t ret;
673 u32 rc;
674 size_t resp_buf_size = output_param_block_size;
675
676 EFI_ENTRY("%p, %u, %p, %u, %p", this, input_param_block_size,
677 input_param_block, output_param_block_size, output_param_block);
678
679 if (!this || !input_param_block || !input_param_block_size) {
680 ret = EFI_INVALID_PARAMETER;
681 goto out;
682 }
683
Eddie James8ed7bb32023-10-24 10:43:49 -0500684 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900685 if (ret != EFI_SUCCESS)
686 goto out;
687
688 rc = tpm2_submit_command(dev, input_param_block,
689 output_param_block, &resp_buf_size);
690 if (rc) {
691 ret = (rc == -ENOSPC) ? EFI_OUT_OF_RESOURCES : EFI_DEVICE_ERROR;
692
693 goto out;
694 }
695
696out:
697 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200698}
699
700/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200701 * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200702 *
703 * @this: TCG2 protocol instance
704 * @active_pcr_banks: pointer for receiving the bitmap of currently
705 * active PCR banks
706 *
707 * Return: status code
708 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200709static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200710efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
711 u32 *active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200712{
Eddie James8ed7bb32023-10-24 10:43:49 -0500713 struct udevice *dev;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200714 efi_status_t ret;
715
Ilias Apalodimas918a6ea2023-10-24 10:43:53 -0500716 EFI_ENTRY("%p, %p", this, active_pcr_banks);
717
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900718 if (!this || !active_pcr_banks) {
719 ret = EFI_INVALID_PARAMETER;
720 goto out;
721 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500722 ret = tcg2_platform_get_tpm2(&dev);
723 if (ret != EFI_SUCCESS)
724 goto out;
725
Eddie James8ed7bb32023-10-24 10:43:49 -0500726 ret = tcg2_get_active_pcr_banks(dev, active_pcr_banks);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200727
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900728out:
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200729 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200730}
731
732/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200733 * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200734 *
735 * @this: TCG2 protocol instance
736 * @active_pcr_banks: bitmap of the requested active PCR banks
737 *
738 * Return: status code
739 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200740static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300741efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
742 u32 __maybe_unused active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200743{
744 return EFI_UNSUPPORTED;
745}
746
747/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200748 * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous
749 * set_active_pcr_banks()
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200750 *
751 * @this: TCG2 protocol instance
752 * @operation_present: non-zero value to indicate a
753 * set_active_pcr_banks operation was
754 * invoked during last boot
755 * @response: result value could be returned
756 *
757 * Return: status code
758 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200759static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300760efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
761 u32 __maybe_unused *operation_present,
762 u32 __maybe_unused *response)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200763{
764 return EFI_UNSUPPORTED;
765}
766
767static const struct efi_tcg2_protocol efi_tcg2_protocol = {
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200768 .get_capability = efi_tcg2_get_capability,
769 .get_eventlog = efi_tcg2_get_eventlog,
770 .hash_log_extend_event = efi_tcg2_hash_log_extend_event,
771 .submit_command = efi_tcg2_submit_command,
772 .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks,
773 .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks,
774 .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks,
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200775};
776
777/**
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200778 * tcg2_uninit - remove the final event table and free efi memory on failures
779 */
780void tcg2_uninit(void)
781{
782 efi_status_t ret;
783
784 ret = efi_install_configuration_table(&efi_guid_final_events, NULL);
785 if (ret != EFI_SUCCESS)
786 log_err("Failed to delete final events config table\n");
787
788 efi_free_pool(event_log.buffer);
789 event_log.buffer = NULL;
790 efi_free_pool(event_log.final_buffer);
791 event_log.final_buffer = NULL;
Masahisa Kojima0fd43792021-12-07 14:15:31 +0900792
793 if (!is_tcg2_protocol_installed())
794 return;
795
Ilias Apalodimas4953c992023-06-19 14:14:02 +0300796 ret = efi_uninstall_multiple_protocol_interfaces(efi_root, &efi_guid_tcg2_protocol,
797 &efi_tcg2_protocol, NULL);
Masahisa Kojima0fd43792021-12-07 14:15:31 +0900798 if (ret != EFI_SUCCESS)
799 log_err("Failed to remove EFI TCG2 protocol\n");
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200800}
801
802/**
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200803 * create_final_event() - Create the final event and install the config
804 * defined by the TCG EFI spec
805 */
806static efi_status_t create_final_event(void)
807{
808 struct efi_tcg2_final_events_table *final_event;
809 efi_status_t ret;
810
811 /*
812 * All events generated after the invocation of
813 * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an
814 * EFI_CONFIGURATION_TABLE
815 */
816 ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE,
817 &event_log.final_buffer);
818 if (ret != EFI_SUCCESS)
819 goto out;
820
821 memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE);
822 final_event = event_log.final_buffer;
823 final_event->number_of_events = 0;
824 final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
825 event_log.final_pos = sizeof(*final_event);
826 ret = efi_install_configuration_table(&efi_guid_final_events,
827 final_event);
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +0300828 if (ret != EFI_SUCCESS) {
829 efi_free_pool(event_log.final_buffer);
830 event_log.final_buffer = NULL;
831 }
832
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200833out:
834 return ret;
835}
836
837/**
Eddie James8ed7bb32023-10-24 10:43:49 -0500838 * measure_event() - common function to add event log and extend PCR
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900839 *
840 * @dev: TPM device
841 * @pcr_index: PCR index
842 * @event_type: type of event added
843 * @size: event size
844 * @event: event data
845 *
846 * Return: status code
847 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500848static efi_status_t measure_event(struct udevice *dev, u32 pcr_index,
849 u32 event_type, u32 size, u8 event[])
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900850{
851 struct tpml_digest_values digest_list;
852 efi_status_t ret;
853
Eddie James8ed7bb32023-10-24 10:43:49 -0500854 ret = tcg2_create_digest(dev, event, size, &digest_list);
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900855 if (ret != EFI_SUCCESS)
856 goto out;
857
858 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
859 if (ret != EFI_SUCCESS)
860 goto out;
861
862 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
863 size, event);
864
865out:
866 return ret;
867}
868
869/**
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200870 * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the
871 * eventlog and extend the PCRs
872 *
873 * @dev: TPM device
874 *
875 * @Return: status code
876 */
877static efi_status_t efi_append_scrtm_version(struct udevice *dev)
878{
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200879 efi_status_t ret;
880
Eddie James8ed7bb32023-10-24 10:43:49 -0500881 ret = measure_event(dev, 0, EV_S_CRTM_VERSION,
882 strlen(version_string) + 1, (u8 *)version_string);
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200883
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530884 return ret;
885}
886
887/**
888 * efi_init_event_log() - initialize an eventlog
889 *
890 * Return: status code
891 */
892static efi_status_t efi_init_event_log(void)
893{
894 /*
895 * vendor_info_size is currently set to 0, we need to change the length
896 * and allocate the flexible array member if this changes
897 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500898 struct tcg2_event_log elog;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530899 struct udevice *dev;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530900 efi_status_t ret;
901
Eddie James8ed7bb32023-10-24 10:43:49 -0500902 ret = tcg2_platform_get_tpm2(&dev);
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530903 if (ret != EFI_SUCCESS)
904 return ret;
905
906 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE,
907 (void **)&event_log.buffer);
908 if (ret != EFI_SUCCESS)
909 return ret;
910
911 /*
912 * initialize log area as 0xff so the OS can easily figure out the
913 * last log entry
914 */
915 memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE);
916
917 /*
918 * The log header is defined to be in SHA1 event log entry format.
919 * Setup event header
920 */
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530921 event_log.pos = 0;
922 event_log.last_event_size = 0;
923 event_log.get_event_called = false;
924 event_log.ebs_called = false;
925 event_log.truncated = false;
926
927 /*
928 * Check if earlier firmware have passed any eventlog. Different
929 * platforms can use different ways to do so.
930 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500931 elog.log = event_log.buffer;
932 elog.log_size = TPM2_EVENT_LOG_SIZE;
933 ret = tcg2_log_prepare_buffer(dev, &elog, false);
934 if (ret != EFI_SUCCESS)
935 goto free_pool;
936
937 event_log.pos = elog.log_position;
938
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530939 /*
Eddie James8ed7bb32023-10-24 10:43:49 -0500940 * Add SCRTM version to the log if previous firmmware
941 * doesn't pass an eventlog.
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530942 */
Ilias Apalodimas12c15f52023-11-07 13:31:34 +0200943 if (!elog.found) {
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530944 ret = efi_append_scrtm_version(dev);
Ilias Apalodimas12c15f52023-11-07 13:31:34 +0200945 if (ret != EFI_SUCCESS)
946 goto free_pool;
947 }
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530948
949 ret = create_final_event();
950 if (ret != EFI_SUCCESS)
951 goto free_pool;
952
953 return ret;
954
955free_pool:
956 efi_free_pool(event_log.buffer);
957 event_log.buffer = NULL;
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200958 return ret;
959}
960
961/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900962 * tcg2_measure_variable() - add variable event log and extend PCR
963 *
964 * @dev: TPM device
965 * @pcr_index: PCR index
966 * @event_type: type of event added
967 * @var_name: variable name
968 * @guid: guid
969 * @data_size: variable data size
970 * @data: variable data
971 *
972 * Return: status code
973 */
974static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index,
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +0200975 u32 event_type, const u16 *var_name,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900976 const efi_guid_t *guid,
977 efi_uintn_t data_size, u8 *data)
978{
979 u32 event_size;
980 efi_status_t ret;
981 struct efi_tcg2_uefi_variable_data *event;
982
983 event_size = sizeof(event->variable_name) +
984 sizeof(event->unicode_name_length) +
985 sizeof(event->variable_data_length) +
986 (u16_strlen(var_name) * sizeof(u16)) + data_size;
987 event = malloc(event_size);
988 if (!event)
989 return EFI_OUT_OF_RESOURCES;
990
991 guidcpy(&event->variable_name, guid);
992 event->unicode_name_length = u16_strlen(var_name);
993 event->variable_data_length = data_size;
994 memcpy(event->unicode_name, var_name,
995 (event->unicode_name_length * sizeof(u16)));
996 if (data) {
997 memcpy((u16 *)event->unicode_name + event->unicode_name_length,
998 data, data_size);
999 }
Eddie James8ed7bb32023-10-24 10:43:49 -05001000 ret = measure_event(dev, pcr_index, event_type, event_size,
1001 (u8 *)event);
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001002 free(event);
1003 return ret;
1004}
1005
1006/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001007 * tcg2_measure_boot_variable() - measure boot variables
1008 *
1009 * @dev: TPM device
1010 *
1011 * Return: status code
1012 */
1013static efi_status_t tcg2_measure_boot_variable(struct udevice *dev)
1014{
1015 u16 *boot_order;
1016 u16 *boot_index;
Simon Glass90975372022-01-23 12:55:12 -07001017 u16 var_name[] = u"BootOrder";
1018 u16 boot_name[] = u"Boot####";
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001019 u8 *bootvar;
1020 efi_uintn_t var_data_size;
1021 u32 count, i;
1022 efi_status_t ret;
1023
1024 boot_order = efi_get_var(var_name, &efi_global_variable_guid,
1025 &var_data_size);
1026 if (!boot_order) {
Masahisa Kojimad1325932021-11-09 18:44:54 +09001027 /* If "BootOrder" is not defined, skip the boot variable measurement */
1028 return EFI_SUCCESS;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001029 }
1030
1031 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name,
1032 &efi_global_variable_guid, var_data_size,
1033 (u8 *)boot_order);
1034 if (ret != EFI_SUCCESS)
1035 goto error;
1036
1037 count = var_data_size / sizeof(*boot_order);
1038 boot_index = boot_order;
1039 for (i = 0; i < count; i++) {
1040 efi_create_indexed_name(boot_name, sizeof(boot_name),
1041 "Boot", *boot_index++);
1042
1043 bootvar = efi_get_var(boot_name, &efi_global_variable_guid,
1044 &var_data_size);
1045
1046 if (!bootvar) {
Masahisa Kojimaaca20c82021-11-09 20:35:53 +09001047 log_debug("%ls not found\n", boot_name);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001048 continue;
1049 }
1050
1051 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2,
1052 boot_name,
1053 &efi_global_variable_guid,
1054 var_data_size, bootvar);
1055 free(bootvar);
1056 if (ret != EFI_SUCCESS)
1057 goto error;
1058 }
1059
1060error:
1061 free(boot_order);
1062 return ret;
1063}
1064
1065/**
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001066 * tcg2_measure_smbios() - measure smbios table
1067 *
1068 * @dev: TPM device
1069 * @entry: pointer to the smbios_entry structure
1070 *
1071 * Return: status code
1072 */
1073static efi_status_t
1074tcg2_measure_smbios(struct udevice *dev,
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001075 const struct smbios3_entry *entry)
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001076{
1077 efi_status_t ret;
1078 struct smbios_header *smbios_copy;
1079 struct smbios_handoff_table_pointers2 *event = NULL;
1080 u32 event_size;
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001081 const char smbios3_anchor[] = "_SM3_";
1082
1083 /* We only support SMBIOS 3.0 Entry Point structure */
1084 if (memcmp(entry->anchor, smbios3_anchor, sizeof(smbios3_anchor) - 1))
1085 return EFI_UNSUPPORTED;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001086
1087 /*
1088 * TCG PC Client PFP Spec says
1089 * "SMBIOS structures that contain static configuration information
1090 * (e.g. Platform Manufacturer Enterprise Number assigned by IANA,
1091 * platform model number, Vendor and Device IDs for each SMBIOS table)
1092 * that is relevant to the security of the platform MUST be measured".
1093 * Device dependent parameters such as serial number are cleared to
1094 * zero or spaces for the measurement.
1095 */
1096 event_size = sizeof(struct smbios_handoff_table_pointers2) +
1097 FIELD_SIZEOF(struct efi_configuration_table, guid) +
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +01001098 entry->table_maximum_size;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001099 event = calloc(1, event_size);
1100 if (!event) {
1101 ret = EFI_OUT_OF_RESOURCES;
1102 goto out;
1103 }
1104
1105 event->table_description_size = sizeof(SMBIOS_HANDOFF_TABLE_DESC);
1106 memcpy(event->table_description, SMBIOS_HANDOFF_TABLE_DESC,
1107 sizeof(SMBIOS_HANDOFF_TABLE_DESC));
1108 put_unaligned_le64(1, &event->number_of_tables);
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001109 guidcpy(&event->table_entry[0].guid, &smbios3_guid);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001110 smbios_copy = (struct smbios_header *)((uintptr_t)&event->table_entry[0].table);
1111 memcpy(&event->table_entry[0].table,
1112 (void *)((uintptr_t)entry->struct_table_address),
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +01001113 entry->table_maximum_size);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001114
1115 smbios_prepare_measurement(entry, smbios_copy);
1116
Eddie James8ed7bb32023-10-24 10:43:49 -05001117 ret = measure_event(dev, 1, EV_EFI_HANDOFF_TABLES2, event_size,
1118 (u8 *)event);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001119 if (ret != EFI_SUCCESS)
1120 goto out;
1121
1122out:
1123 free(event);
1124
1125 return ret;
1126}
1127
1128/**
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001129 * tcg2_measure_gpt_table() - measure gpt table
1130 *
1131 * @dev: TPM device
1132 * @loaded_image: handle to the loaded image
1133 *
1134 * Return: status code
1135 */
1136static efi_status_t
1137tcg2_measure_gpt_data(struct udevice *dev,
1138 struct efi_loaded_image_obj *loaded_image)
1139{
1140 efi_status_t ret;
1141 efi_handle_t handle;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001142 struct efi_handler *dp_handler, *io_handler;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001143 struct efi_device_path *orig_device_path;
1144 struct efi_device_path *device_path;
1145 struct efi_device_path *dp;
1146 struct efi_block_io *block_io;
1147 struct efi_gpt_data *event = NULL;
1148 efi_guid_t null_guid = NULL_GUID;
1149 gpt_header *gpt_h;
1150 gpt_entry *entry = NULL;
1151 gpt_entry *gpt_e;
1152 u32 num_of_valid_entry = 0;
1153 u32 event_size;
1154 u32 i;
1155 u32 total_gpt_entry_size;
1156
1157 ret = efi_search_protocol(&loaded_image->header,
1158 &efi_guid_loaded_image_device_path,
1159 &dp_handler);
1160 if (ret != EFI_SUCCESS)
1161 return ret;
1162
1163 orig_device_path = dp_handler->protocol_interface;
1164 if (!orig_device_path) /* no device path, skip GPT measurement */
1165 return EFI_SUCCESS;
1166
1167 device_path = efi_dp_dup(orig_device_path);
1168 if (!device_path)
1169 return EFI_OUT_OF_RESOURCES;
1170
1171 dp = search_gpt_dp_node(device_path);
1172 if (!dp) {
1173 /* no GPT device path node found, skip GPT measurement */
1174 ret = EFI_SUCCESS;
1175 goto out1;
1176 }
1177
1178 /* read GPT header */
1179 dp->type = DEVICE_PATH_TYPE_END;
1180 dp->sub_type = DEVICE_PATH_SUB_TYPE_END;
1181 dp = device_path;
1182 ret = EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_guid,
1183 &dp, &handle));
1184 if (ret != EFI_SUCCESS)
1185 goto out1;
1186
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001187 ret = efi_search_protocol(handle, &efi_block_io_guid, &io_handler);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001188 if (ret != EFI_SUCCESS)
1189 goto out1;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001190 block_io = io_handler->protocol_interface;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001191
1192 gpt_h = memalign(block_io->media->io_align, block_io->media->block_size);
1193 if (!gpt_h) {
1194 ret = EFI_OUT_OF_RESOURCES;
1195 goto out2;
1196 }
1197
1198 ret = block_io->read_blocks(block_io, block_io->media->media_id, 1,
1199 block_io->media->block_size, gpt_h);
1200 if (ret != EFI_SUCCESS)
1201 goto out2;
1202
1203 /* read GPT entry */
1204 total_gpt_entry_size = gpt_h->num_partition_entries *
1205 gpt_h->sizeof_partition_entry;
1206 entry = memalign(block_io->media->io_align, total_gpt_entry_size);
1207 if (!entry) {
1208 ret = EFI_OUT_OF_RESOURCES;
1209 goto out2;
1210 }
1211
1212 ret = block_io->read_blocks(block_io, block_io->media->media_id,
1213 gpt_h->partition_entry_lba,
1214 total_gpt_entry_size, entry);
1215 if (ret != EFI_SUCCESS)
1216 goto out2;
1217
1218 /* count valid GPT entry */
1219 gpt_e = entry;
1220 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1221 if (guidcmp(&null_guid, &gpt_e->partition_type_guid))
1222 num_of_valid_entry++;
1223
1224 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1225 }
1226
1227 /* prepare event data for measurement */
1228 event_size = sizeof(struct efi_gpt_data) +
1229 (num_of_valid_entry * gpt_h->sizeof_partition_entry);
1230 event = calloc(1, event_size);
1231 if (!event) {
1232 ret = EFI_OUT_OF_RESOURCES;
1233 goto out2;
1234 }
1235 memcpy(event, gpt_h, sizeof(gpt_header));
1236 put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions);
1237
1238 /* copy valid GPT entry */
1239 gpt_e = entry;
1240 num_of_valid_entry = 0;
1241 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1242 if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) {
1243 memcpy((u8 *)event->partitions +
1244 (num_of_valid_entry * gpt_h->sizeof_partition_entry),
1245 gpt_e, gpt_h->sizeof_partition_entry);
1246 num_of_valid_entry++;
1247 }
1248
1249 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1250 }
1251
Eddie James8ed7bb32023-10-24 10:43:49 -05001252 ret = measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *)event);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001253
1254out2:
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001255 free(gpt_h);
1256 free(entry);
1257 free(event);
1258out1:
1259 efi_free_pool(device_path);
1260
1261 return ret;
1262}
1263
Etienne Carriereb9064352023-02-16 17:29:48 +01001264/* Return the byte size of reserved map area in DTB or -1 upon error */
1265static ssize_t size_of_rsvmap(void *dtb)
1266{
1267 struct fdt_reserve_entry e;
1268 ssize_t size_max;
1269 ssize_t size;
1270 u8 *rsvmap_base;
1271
1272 rsvmap_base = (u8 *)dtb + fdt_off_mem_rsvmap(dtb);
1273 size_max = fdt_totalsize(dtb) - fdt_off_mem_rsvmap(dtb);
1274 size = 0;
1275
1276 do {
1277 memcpy(&e, rsvmap_base + size, sizeof(e));
1278 size += sizeof(e);
1279 if (size > size_max)
1280 return -1;
1281 } while (e.size);
1282
1283 return size;
1284}
1285
1286/**
1287 * efi_tcg2_measure_dtb() - measure DTB passed to the OS
1288 *
1289 * @dtb: pointer to the device tree blob
1290 *
1291 * Return: status code
1292 */
1293efi_status_t efi_tcg2_measure_dtb(void *dtb)
1294{
1295 struct uefi_platform_firmware_blob2 *blob;
1296 struct fdt_header *header;
1297 sha256_context hash_ctx;
1298 struct udevice *dev;
1299 ssize_t rsvmap_size;
1300 efi_status_t ret;
1301 u32 event_size;
1302
1303 if (!is_tcg2_protocol_installed())
1304 return EFI_SUCCESS;
1305
Eddie James8ed7bb32023-10-24 10:43:49 -05001306 ret = tcg2_platform_get_tpm2(&dev);
Etienne Carriereb9064352023-02-16 17:29:48 +01001307 if (ret != EFI_SUCCESS)
1308 return EFI_SECURITY_VIOLATION;
1309
1310 rsvmap_size = size_of_rsvmap(dtb);
1311 if (rsvmap_size < 0)
1312 return EFI_SECURITY_VIOLATION;
1313
1314 event_size = sizeof(*blob) + sizeof(EFI_DTB_EVENT_STRING) + SHA256_SUM_LEN;
1315 blob = calloc(1, event_size);
1316 if (!blob)
1317 return EFI_OUT_OF_RESOURCES;
1318
1319 blob->blob_description_size = sizeof(EFI_DTB_EVENT_STRING);
1320 memcpy(blob->data, EFI_DTB_EVENT_STRING, blob->blob_description_size);
1321
1322 /* Measure populated areas of the DTB */
1323 header = dtb;
1324 sha256_starts(&hash_ctx);
1325 sha256_update(&hash_ctx, (u8 *)header, sizeof(struct fdt_header));
1326 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_struct(dtb), fdt_size_dt_strings(dtb));
1327 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_strings(dtb), fdt_size_dt_struct(dtb));
1328 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_mem_rsvmap(dtb), rsvmap_size);
1329 sha256_finish(&hash_ctx, blob->data + blob->blob_description_size);
1330
Eddie James8ed7bb32023-10-24 10:43:49 -05001331 ret = measure_event(dev, 0, EV_POST_CODE, event_size, (u8 *)blob);
Etienne Carriereb9064352023-02-16 17:29:48 +01001332
1333 free(blob);
1334 return ret;
1335}
1336
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001337/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001338 * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation
1339 *
1340 * Return: status code
1341 */
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001342efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_image_obj *handle)
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001343{
1344 efi_status_t ret;
1345 u32 pcr_index;
1346 struct udevice *dev;
1347 u32 event = 0;
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001348 struct smbios3_entry *entry;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001349
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001350 if (!is_tcg2_protocol_installed())
1351 return EFI_SUCCESS;
1352
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001353 if (tcg2_efi_app_invoked)
1354 return EFI_SUCCESS;
1355
Eddie James8ed7bb32023-10-24 10:43:49 -05001356 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001357 if (ret != EFI_SUCCESS)
Masahisa Kojima38155ea2021-12-07 14:15:33 +09001358 return EFI_SECURITY_VIOLATION;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001359
1360 ret = tcg2_measure_boot_variable(dev);
1361 if (ret != EFI_SUCCESS)
1362 goto out;
1363
Eddie James8ed7bb32023-10-24 10:43:49 -05001364 ret = measure_event(dev, 4, EV_EFI_ACTION,
1365 strlen(EFI_CALLING_EFI_APPLICATION),
1366 (u8 *)EFI_CALLING_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001367 if (ret != EFI_SUCCESS)
1368 goto out;
1369
Heinrich Schuchardt10899c82024-01-26 09:13:22 +01001370 entry = efi_get_configuration_table(&smbios3_guid);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001371 if (entry) {
1372 ret = tcg2_measure_smbios(dev, entry);
1373 if (ret != EFI_SUCCESS)
1374 goto out;
1375 }
1376
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001377 ret = tcg2_measure_gpt_data(dev, handle);
1378 if (ret != EFI_SUCCESS)
1379 goto out;
1380
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001381 for (pcr_index = 0; pcr_index <= 7; pcr_index++) {
Eddie James8ed7bb32023-10-24 10:43:49 -05001382 ret = measure_event(dev, pcr_index, EV_SEPARATOR,
1383 sizeof(event), (u8 *)&event);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001384 if (ret != EFI_SUCCESS)
1385 goto out;
1386 }
1387
1388 tcg2_efi_app_invoked = true;
1389out:
1390 return ret;
1391}
1392
1393/**
1394 * efi_tcg2_measure_efi_app_exit() - measure efi app exit
1395 *
1396 * Return: status code
1397 */
1398efi_status_t efi_tcg2_measure_efi_app_exit(void)
1399{
1400 efi_status_t ret;
1401 struct udevice *dev;
1402
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001403 if (!is_tcg2_protocol_installed())
1404 return EFI_SUCCESS;
1405
Eddie James8ed7bb32023-10-24 10:43:49 -05001406 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001407 if (ret != EFI_SUCCESS)
1408 return ret;
1409
Eddie James8ed7bb32023-10-24 10:43:49 -05001410 ret = measure_event(dev, 4, EV_EFI_ACTION,
1411 strlen(EFI_RETURNING_FROM_EFI_APPLICATION),
1412 (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001413 return ret;
1414}
1415
1416/**
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001417 * efi_tcg2_notify_exit_boot_services() - ExitBootService callback
1418 *
1419 * @event: callback event
1420 * @context: callback context
1421 */
1422static void EFIAPI
1423efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context)
1424{
1425 efi_status_t ret;
1426 struct udevice *dev;
1427
1428 EFI_ENTRY("%p, %p", event, context);
1429
Ilias Apalodimas24e841a2021-11-18 09:03:39 +02001430 event_log.ebs_called = true;
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001431
1432 if (!is_tcg2_protocol_installed()) {
1433 ret = EFI_SUCCESS;
1434 goto out;
1435 }
1436
Eddie James8ed7bb32023-10-24 10:43:49 -05001437 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001438 if (ret != EFI_SUCCESS)
1439 goto out;
1440
Eddie James8ed7bb32023-10-24 10:43:49 -05001441 ret = measure_event(dev, 5, EV_EFI_ACTION,
1442 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1443 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001444 if (ret != EFI_SUCCESS)
1445 goto out;
1446
Eddie James8ed7bb32023-10-24 10:43:49 -05001447 ret = measure_event(dev, 5, EV_EFI_ACTION,
1448 strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED),
1449 (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001450
1451out:
1452 EFI_EXIT(ret);
1453}
1454
1455/**
1456 * efi_tcg2_notify_exit_boot_services_failed()
1457 * - notify ExitBootServices() is failed
1458 *
1459 * Return: status code
1460 */
1461efi_status_t efi_tcg2_notify_exit_boot_services_failed(void)
1462{
1463 struct udevice *dev;
1464 efi_status_t ret;
1465
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001466 if (!is_tcg2_protocol_installed())
1467 return EFI_SUCCESS;
1468
Eddie James8ed7bb32023-10-24 10:43:49 -05001469 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001470 if (ret != EFI_SUCCESS)
1471 goto out;
1472
Eddie James8ed7bb32023-10-24 10:43:49 -05001473 ret = measure_event(dev, 5, EV_EFI_ACTION,
1474 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1475 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001476 if (ret != EFI_SUCCESS)
1477 goto out;
1478
Eddie James8ed7bb32023-10-24 10:43:49 -05001479 ret = measure_event(dev, 5, EV_EFI_ACTION,
1480 strlen(EFI_EXIT_BOOT_SERVICES_FAILED),
1481 (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001482
1483out:
1484 return ret;
1485}
1486
1487/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001488 * tcg2_measure_secure_boot_variable() - measure secure boot variables
1489 *
1490 * @dev: TPM device
1491 *
1492 * Return: status code
1493 */
1494static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev)
1495{
1496 u8 *data;
1497 efi_uintn_t data_size;
1498 u32 count, i;
1499 efi_status_t ret;
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001500 u8 deployed_mode;
1501 efi_uintn_t size;
1502 u32 deployed_audit_pcr_index = 1;
1503
1504 size = sizeof(deployed_mode);
1505 ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid,
1506 NULL, &size, &deployed_mode, NULL);
1507 if (ret != EFI_SUCCESS || !deployed_mode)
1508 deployed_audit_pcr_index = 7;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001509
1510 count = ARRAY_SIZE(secure_variables);
1511 for (i = 0; i < count; i++) {
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001512 const efi_guid_t *guid;
1513
Masahisa Kojima21684522021-10-26 17:27:26 +09001514 guid = efi_auth_var_get_guid(secure_variables[i].name);
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001515
Masahisa Kojima21684522021-10-26 17:27:26 +09001516 data = efi_get_var(secure_variables[i].name, guid, &data_size);
1517 if (!data && !secure_variables[i].accept_empty)
1518 continue;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001519
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001520 if (u16_strcmp(u"DeployedMode", secure_variables[i].name))
1521 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1522 if (u16_strcmp(u"AuditMode", secure_variables[i].name))
1523 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1524
1525 ret = tcg2_measure_variable(dev, secure_variables[i].pcr_index,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001526 EV_EFI_VARIABLE_DRIVER_CONFIG,
Masahisa Kojima21684522021-10-26 17:27:26 +09001527 secure_variables[i].name, guid,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001528 data_size, data);
1529 free(data);
1530 if (ret != EFI_SUCCESS)
1531 goto error;
1532 }
1533
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001534error:
1535 return ret;
1536}
1537
1538/**
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001539 * efi_tcg2_do_initial_measurement() - do initial measurement
1540 *
1541 * Return: status code
1542 */
1543efi_status_t efi_tcg2_do_initial_measurement(void)
1544{
1545 efi_status_t ret;
1546 struct udevice *dev;
1547
1548 if (!is_tcg2_protocol_installed())
1549 return EFI_SUCCESS;
1550
Eddie James8ed7bb32023-10-24 10:43:49 -05001551 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001552 if (ret != EFI_SUCCESS)
1553 return EFI_SECURITY_VIOLATION;
1554
1555 ret = tcg2_measure_secure_boot_variable(dev);
1556 if (ret != EFI_SUCCESS)
1557 goto out;
1558
1559out:
1560 return ret;
1561}
1562
1563/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001564 * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
1565 *
1566 * If a TPM2 device is available, the TPM TCG2 Protocol is registered
1567 *
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001568 * Return: status code
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001569 */
1570efi_status_t efi_tcg2_register(void)
1571{
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001572 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001573 struct udevice *dev;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001574 struct efi_event *event;
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001575 u32 err;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001576
Eddie James8ed7bb32023-10-24 10:43:49 -05001577 ret = tcg2_platform_get_tpm2(&dev);
Ilias Apalodimasc67fef62020-11-16 08:52:41 +02001578 if (ret != EFI_SUCCESS) {
Ilias Apalodimaseb1b6b42023-01-19 16:29:15 +02001579 log_warning("Missing TPMv2 device for EFI_TCG_PROTOCOL\n");
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001580 return EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001581 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001582
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001583 /* initialize the TPM as early as possible. */
Ilias Apalodimas0c95d222023-01-25 13:06:03 +02001584 err = tpm_auto_start(dev);
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001585 if (err) {
1586 log_err("TPM startup failed\n");
1587 goto fail;
1588 }
1589
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001590 ret = efi_init_event_log();
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001591 if (ret != EFI_SUCCESS) {
1592 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001593 goto fail;
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001594 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001595
Ilias Apalodimas4953c992023-06-19 14:14:02 +03001596 ret = efi_install_multiple_protocol_interfaces(&efi_root, &efi_guid_tcg2_protocol,
1597 &efi_tcg2_protocol, NULL);
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001598 if (ret != EFI_SUCCESS) {
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001599 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001600 goto fail;
1601 }
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001602
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001603 ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
1604 efi_tcg2_notify_exit_boot_services, NULL,
1605 NULL, &event);
1606 if (ret != EFI_SUCCESS) {
1607 tcg2_uninit();
1608 goto fail;
1609 }
1610
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001611 return ret;
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001612
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001613fail:
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001614 log_err("Cannot install EFI_TCG2_PROTOCOL\n");
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001615 return ret;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001616}