blob: 8db35d0b3c859ceb6d6edf66614c74b79440888e [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>
Ilias Apalodimas967650d2020-11-30 11:47:40 +020022#include <u-boot/sha1.h>
23#include <u-boot/sha256.h>
24#include <u-boot/sha512.h>
Masahisa Kojimad420d8d2021-11-03 11:04:09 +090025#include <linux/unaligned/be_byteshift.h>
26#include <linux/unaligned/le_byteshift.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020027#include <linux/unaligned/generic.h>
Ilias Apalodimas967650d2020-11-30 11:47:40 +020028#include <hexdump.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020029
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020030/**
31 * struct event_log_buffer - internal eventlog management structure
32 *
33 * @buffer: eventlog buffer
34 * @final_buffer: finalevent config table buffer
35 * @pos: current position of 'buffer'
36 * @final_pos: current position of 'final_buffer'
37 * @get_event_called: true if GetEventLog has been invoked at least once
38 * @ebs_called: true if ExitBootServices has been invoked
39 * @truncated: true if the 'buffer' is truncated
40 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +020041struct event_log_buffer {
42 void *buffer;
43 void *final_buffer;
44 size_t pos; /* eventlog position */
45 size_t final_pos; /* final events config table position */
46 size_t last_event_size;
47 bool get_event_called;
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020048 bool ebs_called;
Ilias Apalodimas967650d2020-11-30 11:47:40 +020049 bool truncated;
50};
Ilias Apalodimas590fef62020-11-11 11:18:11 +020051
Ilias Apalodimas967650d2020-11-30 11:47:40 +020052static struct event_log_buffer event_log;
Masahisa Kojima8173cd42021-08-13 16:12:40 +090053static bool tcg2_efi_app_invoked;
Ilias Apalodimas590fef62020-11-11 11:18:11 +020054/*
55 * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset.
56 * Since the current tpm2_get_capability() response buffers starts at
57 * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate
58 * the response size and offset once for all consumers
59 */
60#define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \
61 offsetof(struct tpms_capability_data, data))
62#define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \
63 offsetof(struct tpms_tagged_property, value))
64
Ilias Apalodimas967650d2020-11-30 11:47:40 +020065static const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID;
66static const efi_guid_t efi_guid_final_events = EFI_TCG2_FINAL_EVENTS_TABLE_GUID;
67
Masahisa Kojima21684522021-10-26 17:27:26 +090068struct variable_info {
69 const u16 *name;
70 bool accept_empty;
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +090071 u32 pcr_index;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +090072};
73
Masahisa Kojima21684522021-10-26 17:27:26 +090074static struct variable_info secure_variables[] = {
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +090075 {u"SecureBoot", true, 7},
76 {u"PK", true, 7},
77 {u"KEK", true, 7},
78 {u"db", true, 7},
79 {u"dbx", true, 7},
80 {u"dbt", false, 7},
81 {u"dbr", false, 7},
82 {u"DeployedMode", false, 1},
83 {u"AuditMode", false, 1},
Masahisa Kojima21684522021-10-26 17:27:26 +090084};
85
Masahisa Kojima0fd43792021-12-07 14:15:31 +090086static bool is_tcg2_protocol_installed(void)
87{
88 struct efi_handler *handler;
89 efi_status_t ret;
90
91 ret = efi_search_protocol(efi_root, &efi_guid_tcg2_protocol, &handler);
92 return ret == EFI_SUCCESS;
93}
94
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020095/* tcg2_agile_log_append - Append an agile event to an eventlog
96 *
97 * @pcr_index: PCR index
98 * @event_type: type of event added
99 * @digest_list: list of digest algorithms to add
100 * @size: size of event
101 * @event: event to add
102 * @log: log buffer to append the event
103 *
104 * @Return: status code
105 */
106static efi_status_t tcg2_agile_log_append(u32 pcr_index, u32 event_type,
107 struct tpml_digest_values *digest_list,
108 u32 size, u8 event[])
109{
110 void *log = (void *)((uintptr_t)event_log.buffer + event_log.pos);
Eddie James8ed7bb32023-10-24 10:43:49 -0500111 u32 event_size = size + tcg2_event_get_size(digest_list);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200112 struct efi_tcg2_final_events_table *final_event;
113 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200114
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200115 /* if ExitBootServices hasn't been called update the normal log */
116 if (!event_log.ebs_called) {
117 if (event_log.truncated ||
118 event_log.pos + event_size > TPM2_EVENT_LOG_SIZE) {
119 event_log.truncated = true;
120 return EFI_VOLUME_FULL;
121 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500122 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200123 event_log.pos += event_size;
124 event_log.last_event_size = event_size;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200125 }
126
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200127 if (!event_log.get_event_called)
128 return ret;
129
130 /* if GetEventLog has been called update FinalEventLog as well */
131 if (event_log.final_pos + event_size > TPM2_EVENT_LOG_SIZE)
132 return EFI_VOLUME_FULL;
133
134 log = (void *)((uintptr_t)event_log.final_buffer + event_log.final_pos);
Eddie James8ed7bb32023-10-24 10:43:49 -0500135 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200136
137 final_event = event_log.final_buffer;
138 final_event->number_of_events++;
139 event_log.final_pos += event_size;
140
141 return ret;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200142}
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200143
144/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200145 * tpm2_get_max_command_size() - get the supported max command size
146 *
147 * @dev: TPM device
148 * @max_command_size: output buffer for the size
149 *
150 * Return: 0 on success, -1 on error
151 */
152static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size)
153{
154 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
155 u32 ret;
156
157 memset(response, 0, sizeof(response));
158 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
159 TPM2_PT_MAX_COMMAND_SIZE, response, 1);
160 if (ret)
161 return -1;
162
163 *max_command_size = (uint16_t)get_unaligned_be32(response +
164 properties_offset);
165
166 return 0;
167}
168
169/**
170 * tpm2_get_max_response_size() - get the supported max response size
171 *
172 * @dev: TPM device
173 * @max_response_size: output buffer for the size
174 *
175 * Return: 0 on success, -1 on error
176 */
177static int tpm2_get_max_response_size(struct udevice *dev,
178 u16 *max_response_size)
179{
180 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
181 u32 ret;
182
183 memset(response, 0, sizeof(response));
184 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
185 TPM2_PT_MAX_RESPONSE_SIZE, response, 1);
186 if (ret)
187 return -1;
188
189 *max_response_size = (uint16_t)get_unaligned_be32(response +
190 properties_offset);
191
192 return 0;
193}
194
195/**
196 * tpm2_get_manufacturer_id() - get the manufacturer ID
197 *
198 * @dev: TPM device
199 * @manufacturer_id: output buffer for the id
200 *
201 * Return: 0 on success, -1 on error
202 */
203static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id)
204{
205 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
206 u32 ret;
207
208 memset(response, 0, sizeof(response));
209 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
210 TPM2_PT_MANUFACTURER, response, 1);
211 if (ret)
212 return -1;
213
214 *manufacturer_id = get_unaligned_be32(response + properties_offset);
215
216 return 0;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200217}
218
219/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200220 * efi_tcg2_get_capability() - protocol capability information and state information
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200221 *
222 * @this: TCG2 protocol instance
223 * @capability: caller allocated memory with size field to the size of
224 * the structure allocated
225
226 * Return: status code
227 */
228static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200229efi_tcg2_get_capability(struct efi_tcg2_protocol *this,
230 struct efi_tcg2_boot_service_capability *capability)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200231{
232 struct udevice *dev;
233 efi_status_t efi_ret;
234 int ret;
235
236 EFI_ENTRY("%p, %p", this, capability);
237
238 if (!this || !capability) {
239 efi_ret = EFI_INVALID_PARAMETER;
240 goto out;
241 }
242
Masahisa Kojima9cc82932021-09-06 12:04:12 +0900243 if (capability->size < BOOT_SERVICE_CAPABILITY_MIN) {
244 capability->size = BOOT_SERVICE_CAPABILITY_MIN;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200245 efi_ret = EFI_BUFFER_TOO_SMALL;
246 goto out;
247 }
248
249 if (capability->size < sizeof(*capability)) {
250 capability->size = sizeof(*capability);
251 efi_ret = EFI_BUFFER_TOO_SMALL;
252 goto out;
253 }
254
255 capability->structure_version.major = 1;
256 capability->structure_version.minor = 1;
257 capability->protocol_version.major = 1;
258 capability->protocol_version.minor = 1;
259
Eddie James8ed7bb32023-10-24 10:43:49 -0500260 efi_ret = tcg2_platform_get_tpm2(&dev);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200261 if (efi_ret != EFI_SUCCESS) {
262 capability->supported_event_logs = 0;
263 capability->hash_algorithm_bitmap = 0;
264 capability->tpm_present_flag = false;
265 capability->max_command_size = 0;
266 capability->max_response_size = 0;
267 capability->manufacturer_id = 0;
268 capability->number_of_pcr_banks = 0;
269 capability->active_pcr_banks = 0;
270
271 efi_ret = EFI_SUCCESS;
272 goto out;
273 }
274
275 /* We only allow a TPMv2 device to register the EFI protocol */
276 capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2;
277
278 capability->tpm_present_flag = true;
279
280 /* Supported and active PCRs */
281 capability->hash_algorithm_bitmap = 0;
282 capability->active_pcr_banks = 0;
283 ret = tpm2_get_pcr_info(dev, &capability->hash_algorithm_bitmap,
284 &capability->active_pcr_banks,
285 &capability->number_of_pcr_banks);
286 if (ret) {
287 efi_ret = EFI_DEVICE_ERROR;
288 goto out;
289 }
290
291 /* Max command size */
292 ret = tpm2_get_max_command_size(dev, &capability->max_command_size);
293 if (ret) {
294 efi_ret = EFI_DEVICE_ERROR;
295 goto out;
296 }
297
298 /* Max response size */
299 ret = tpm2_get_max_response_size(dev, &capability->max_response_size);
300 if (ret) {
301 efi_ret = EFI_DEVICE_ERROR;
302 goto out;
303 }
304
305 /* Manufacturer ID */
306 ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id);
307 if (ret) {
308 efi_ret = EFI_DEVICE_ERROR;
309 goto out;
310 }
311
312 return EFI_EXIT(EFI_SUCCESS);
313out:
314 return EFI_EXIT(efi_ret);
315}
316
317/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200318 * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its
319 * last entry
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200320 *
321 * @this: TCG2 protocol instance
322 * @log_format: type of event log format
323 * @event_log_location: pointer to the memory address of the event log
324 * @event_log_last_entry: pointer to the address of the start of the last
325 * entry in the event log in memory, if log contains
326 * more than 1 entry
327 * @event_log_truncated: set to true, if the Event Log is missing at i
328 * least one entry
329 *
330 * Return: status code
331 */
332static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200333efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this,
334 efi_tcg_event_log_format log_format,
335 u64 *event_log_location, u64 *event_log_last_entry,
336 bool *event_log_truncated)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200337{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200338 efi_status_t ret = EFI_SUCCESS;
339 struct udevice *dev;
340
341 EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location,
342 event_log_last_entry, event_log_truncated);
343
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900344 if (!this || !event_log_location || !event_log_last_entry ||
345 !event_log_truncated) {
346 ret = EFI_INVALID_PARAMETER;
347 goto out;
348 }
349
350 /* Only support TPMV2 */
351 if (log_format != TCG2_EVENT_LOG_FORMAT_TCG_2) {
352 ret = EFI_INVALID_PARAMETER;
353 goto out;
354 }
355
Eddie James8ed7bb32023-10-24 10:43:49 -0500356 ret = tcg2_platform_get_tpm2(&dev);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200357 if (ret != EFI_SUCCESS) {
358 event_log_location = NULL;
359 event_log_last_entry = NULL;
360 *event_log_truncated = false;
361 ret = EFI_SUCCESS;
362 goto out;
363 }
364 *event_log_location = (uintptr_t)event_log.buffer;
365 *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos -
366 event_log.last_event_size);
367 *event_log_truncated = event_log.truncated;
368 event_log.get_event_called = true;
369
370out:
371 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200372}
373
374/**
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900375 * tcg2_hash_pe_image() - calculate PE/COFF image hash
376 *
377 * @efi: pointer to the EFI binary
378 * @efi_size: size of @efi binary
379 * @digest_list: list of digest algorithms to extend
380 *
381 * Return: status code
382 */
383static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size,
384 struct tpml_digest_values *digest_list)
385{
386 WIN_CERTIFICATE *wincerts = NULL;
387 size_t wincerts_len;
388 struct efi_image_regions *regs = NULL;
389 void *new_efi = NULL;
390 u8 hash[TPM2_SHA512_DIGEST_SIZE];
Eddie James8ed7bb32023-10-24 10:43:49 -0500391 struct udevice *dev;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900392 efi_status_t ret;
393 u32 active;
394 int i;
395
396 new_efi = efi_prepare_aligned_image(efi, &efi_size);
397 if (!new_efi)
398 return EFI_OUT_OF_RESOURCES;
399
400 if (!efi_image_parse(new_efi, efi_size, &regs, &wincerts,
401 &wincerts_len)) {
402 log_err("Parsing PE executable image failed\n");
403 ret = EFI_UNSUPPORTED;
404 goto out;
405 }
406
Eddie James8ed7bb32023-10-24 10:43:49 -0500407 ret = tcg2_platform_get_tpm2(&dev);
408 if (ret != EFI_SUCCESS)
409 goto out;
410
411 ret = tcg2_get_active_pcr_banks(dev, &active);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900412 if (ret != EFI_SUCCESS) {
413 goto out;
414 }
415
416 digest_list->count = 0;
Eddie James8ed7bb32023-10-24 10:43:49 -0500417 for (i = 0; i < ARRAY_SIZE(tpm2_supported_algorithms); i++) {
418 u16 hash_alg = tpm2_supported_algorithms[i];
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900419
Eddie James8ed7bb32023-10-24 10:43:49 -0500420 if (!(active & tpm2_algorithm_to_mask(hash_alg)))
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900421 continue;
422 switch (hash_alg) {
423 case TPM2_ALG_SHA1:
424 hash_calculate("sha1", regs->reg, regs->num, hash);
425 break;
426 case TPM2_ALG_SHA256:
427 hash_calculate("sha256", regs->reg, regs->num, hash);
428 break;
429 case TPM2_ALG_SHA384:
430 hash_calculate("sha384", regs->reg, regs->num, hash);
431 break;
432 case TPM2_ALG_SHA512:
433 hash_calculate("sha512", regs->reg, regs->num, hash);
434 break;
435 default:
Heinrich Schuchardt09ec9f72023-07-31 14:11:34 +0200436 continue;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900437 }
Ruchika Guptae53007b2021-09-14 12:14:31 +0530438 digest_list->digests[digest_list->count].hash_alg = hash_alg;
439 memcpy(&digest_list->digests[digest_list->count].digest, hash,
Eddie James8ed7bb32023-10-24 10:43:49 -0500440 (u32)tpm2_algorithm_to_len(hash_alg));
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900441 digest_list->count++;
442 }
443
444out:
445 if (new_efi != efi)
446 free(new_efi);
447 free(regs);
448
449 return ret;
450}
451
452/**
453 * tcg2_measure_pe_image() - measure PE/COFF image
454 *
455 * @efi: pointer to the EFI binary
456 * @efi_size: size of @efi binary
457 * @handle: loaded image handle
458 * @loaded_image: loaded image protocol
459 *
460 * Return: status code
461 */
462efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size,
463 struct efi_loaded_image_obj *handle,
464 struct efi_loaded_image *loaded_image)
465{
466 struct tpml_digest_values digest_list;
467 efi_status_t ret;
468 struct udevice *dev;
469 u32 pcr_index, event_type, event_size;
470 struct uefi_image_load_event *image_load_event;
471 struct efi_device_path *device_path;
472 u32 device_path_length;
473 IMAGE_DOS_HEADER *dos;
474 IMAGE_NT_HEADERS32 *nt;
475 struct efi_handler *handler;
476
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +0900477 if (!is_tcg2_protocol_installed())
478 return EFI_SUCCESS;
479
Eddie James8ed7bb32023-10-24 10:43:49 -0500480 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900481 if (ret != EFI_SUCCESS)
Masahisa Kojima38155ea2021-12-07 14:15:33 +0900482 return EFI_SECURITY_VIOLATION;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900483
484 switch (handle->image_type) {
485 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
486 pcr_index = 4;
487 event_type = EV_EFI_BOOT_SERVICES_APPLICATION;
488 break;
489 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
490 pcr_index = 2;
491 event_type = EV_EFI_BOOT_SERVICES_DRIVER;
492 break;
493 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
494 pcr_index = 2;
495 event_type = EV_EFI_RUNTIME_SERVICES_DRIVER;
496 break;
497 default:
498 return EFI_UNSUPPORTED;
499 }
500
501 ret = tcg2_hash_pe_image(efi, efi_size, &digest_list);
502 if (ret != EFI_SUCCESS)
503 return ret;
504
505 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
506 if (ret != EFI_SUCCESS)
507 return ret;
508
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300509 ret = efi_search_protocol(&handle->header,
510 &efi_guid_loaded_image_device_path, &handler);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900511 if (ret != EFI_SUCCESS)
512 return ret;
513
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300514 device_path = handler->protocol_interface;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900515 device_path_length = efi_dp_size(device_path);
516 if (device_path_length > 0) {
517 /* add end node size */
518 device_path_length += sizeof(struct efi_device_path);
519 }
520 event_size = sizeof(struct uefi_image_load_event) + device_path_length;
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300521 image_load_event = calloc(1, event_size);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900522 if (!image_load_event)
523 return EFI_OUT_OF_RESOURCES;
524
525 image_load_event->image_location_in_memory = (uintptr_t)efi;
526 image_load_event->image_length_in_memory = efi_size;
527 image_load_event->length_of_device_path = device_path_length;
528
529 dos = (IMAGE_DOS_HEADER *)efi;
530 nt = (IMAGE_NT_HEADERS32 *)(efi + dos->e_lfanew);
531 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
532 IMAGE_NT_HEADERS64 *nt64 = (IMAGE_NT_HEADERS64 *)nt;
533
534 image_load_event->image_link_time_address =
535 nt64->OptionalHeader.ImageBase;
536 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
537 image_load_event->image_link_time_address =
538 nt->OptionalHeader.ImageBase;
539 } else {
540 ret = EFI_INVALID_PARAMETER;
541 goto out;
542 }
543
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300544 /* device_path_length might be zero */
545 memcpy(image_load_event->device_path, device_path, device_path_length);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900546
547 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
548 event_size, (u8 *)image_load_event);
549
550out:
551 free(image_load_event);
552
553 return ret;
554}
555
556/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200557 * efi_tcg2_hash_log_extend_event() - extend and optionally log events
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200558 *
559 * @this: TCG2 protocol instance
560 * @flags: bitmap providing additional information on the
561 * operation
562 * @data_to_hash: physical address of the start of the data buffer
563 * to be hashed
564 * @data_to_hash_len: the length in bytes of the buffer referenced by
565 * data_to_hash
566 * @efi_tcg_event: pointer to data buffer containing information
567 * about the event
568 *
569 * Return: status code
570 */
571static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200572efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags,
573 u64 data_to_hash, u64 data_to_hash_len,
574 struct efi_tcg2_event *efi_tcg_event)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200575{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200576 struct udevice *dev;
577 efi_status_t ret;
578 u32 event_type, pcr_index, event_size;
579 struct tpml_digest_values digest_list;
580
581 EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash,
582 data_to_hash_len, efi_tcg_event);
583
584 if (!this || !data_to_hash || !efi_tcg_event) {
585 ret = EFI_INVALID_PARAMETER;
586 goto out;
587 }
588
Eddie James8ed7bb32023-10-24 10:43:49 -0500589 ret = tcg2_platform_get_tpm2(&dev);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200590 if (ret != EFI_SUCCESS)
591 goto out;
592
593 if (efi_tcg_event->size < efi_tcg_event->header.header_size +
594 sizeof(u32)) {
595 ret = EFI_INVALID_PARAMETER;
596 goto out;
597 }
598
Masahisa Kojimab8074912021-09-03 10:55:52 +0900599 if (efi_tcg_event->header.pcr_index > EFI_TCG2_MAX_PCR_INDEX) {
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200600 ret = EFI_INVALID_PARAMETER;
601 goto out;
602 }
603
604 /*
605 * if PE_COFF_IMAGE is set we need to make sure the image is not
606 * corrupted, verify it and hash the PE/COFF image in accordance with
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900607 * the procedure specified in "Calculating the PE Image Hash"
608 * section of the "Windows Authenticode Portable Executable Signature
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200609 * Format"
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200610 */
611 if (flags & PE_COFF_IMAGE) {
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900612 IMAGE_NT_HEADERS32 *nt;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200613
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900614 ret = efi_check_pe((void *)(uintptr_t)data_to_hash,
615 data_to_hash_len, (void **)&nt);
616 if (ret != EFI_SUCCESS) {
617 log_err("Not a valid PE-COFF file\n");
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900618 ret = EFI_UNSUPPORTED;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900619 goto out;
620 }
621 ret = tcg2_hash_pe_image((void *)(uintptr_t)data_to_hash,
622 data_to_hash_len, &digest_list);
623 } else {
Eddie James8ed7bb32023-10-24 10:43:49 -0500624 ret = tcg2_create_digest(dev, (u8 *)(uintptr_t)data_to_hash,
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900625 data_to_hash_len, &digest_list);
626 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200627
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200628 if (ret != EFI_SUCCESS)
629 goto out;
630
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900631 pcr_index = efi_tcg_event->header.pcr_index;
632 event_type = efi_tcg_event->header.event_type;
633
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200634 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
635 if (ret != EFI_SUCCESS)
636 goto out;
637
638 if (flags & EFI_TCG2_EXTEND_ONLY) {
639 if (event_log.truncated)
640 ret = EFI_VOLUME_FULL;
641 goto out;
642 }
643
644 /*
645 * The efi_tcg_event size includes the size component and the
646 * headersize
647 */
648 event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) -
649 efi_tcg_event->header.header_size;
650 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
651 event_size, efi_tcg_event->event);
652out:
653 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200654}
655
656/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200657 * efi_tcg2_submit_command() - Send command to the TPM
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200658 *
659 * @this: TCG2 protocol instance
660 * @input_param_block_size: size of the TPM input parameter block
661 * @input_param_block: pointer to the TPM input parameter block
662 * @output_param_block_size: size of the TPM output parameter block
663 * @output_param_block: pointer to the TPM output parameter block
664 *
665 * Return: status code
666 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200667static efi_status_t EFIAPI
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900668efi_tcg2_submit_command(struct efi_tcg2_protocol *this,
669 u32 input_param_block_size,
670 u8 *input_param_block,
671 u32 output_param_block_size,
672 u8 *output_param_block)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200673{
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900674 struct udevice *dev;
675 efi_status_t ret;
676 u32 rc;
677 size_t resp_buf_size = output_param_block_size;
678
679 EFI_ENTRY("%p, %u, %p, %u, %p", this, input_param_block_size,
680 input_param_block, output_param_block_size, output_param_block);
681
682 if (!this || !input_param_block || !input_param_block_size) {
683 ret = EFI_INVALID_PARAMETER;
684 goto out;
685 }
686
Eddie James8ed7bb32023-10-24 10:43:49 -0500687 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900688 if (ret != EFI_SUCCESS)
689 goto out;
690
691 rc = tpm2_submit_command(dev, input_param_block,
692 output_param_block, &resp_buf_size);
693 if (rc) {
694 ret = (rc == -ENOSPC) ? EFI_OUT_OF_RESOURCES : EFI_DEVICE_ERROR;
695
696 goto out;
697 }
698
699out:
700 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200701}
702
703/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200704 * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200705 *
706 * @this: TCG2 protocol instance
707 * @active_pcr_banks: pointer for receiving the bitmap of currently
708 * active PCR banks
709 *
710 * Return: status code
711 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200712static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200713efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
714 u32 *active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200715{
Eddie James8ed7bb32023-10-24 10:43:49 -0500716 struct udevice *dev;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200717 efi_status_t ret;
718
Ilias Apalodimas918a6ea2023-10-24 10:43:53 -0500719 EFI_ENTRY("%p, %p", this, active_pcr_banks);
720
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900721 if (!this || !active_pcr_banks) {
722 ret = EFI_INVALID_PARAMETER;
723 goto out;
724 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500725 ret = tcg2_platform_get_tpm2(&dev);
726 if (ret != EFI_SUCCESS)
727 goto out;
728
Eddie James8ed7bb32023-10-24 10:43:49 -0500729 ret = tcg2_get_active_pcr_banks(dev, active_pcr_banks);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200730
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900731out:
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200732 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200733}
734
735/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200736 * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200737 *
738 * @this: TCG2 protocol instance
739 * @active_pcr_banks: bitmap of the requested active PCR banks
740 *
741 * Return: status code
742 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200743static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300744efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
745 u32 __maybe_unused active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200746{
747 return EFI_UNSUPPORTED;
748}
749
750/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200751 * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous
752 * set_active_pcr_banks()
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200753 *
754 * @this: TCG2 protocol instance
755 * @operation_present: non-zero value to indicate a
756 * set_active_pcr_banks operation was
757 * invoked during last boot
758 * @response: result value could be returned
759 *
760 * Return: status code
761 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200762static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300763efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
764 u32 __maybe_unused *operation_present,
765 u32 __maybe_unused *response)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200766{
767 return EFI_UNSUPPORTED;
768}
769
770static const struct efi_tcg2_protocol efi_tcg2_protocol = {
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200771 .get_capability = efi_tcg2_get_capability,
772 .get_eventlog = efi_tcg2_get_eventlog,
773 .hash_log_extend_event = efi_tcg2_hash_log_extend_event,
774 .submit_command = efi_tcg2_submit_command,
775 .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks,
776 .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks,
777 .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks,
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200778};
779
780/**
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200781 * tcg2_uninit - remove the final event table and free efi memory on failures
782 */
783void tcg2_uninit(void)
784{
785 efi_status_t ret;
786
787 ret = efi_install_configuration_table(&efi_guid_final_events, NULL);
788 if (ret != EFI_SUCCESS)
789 log_err("Failed to delete final events config table\n");
790
791 efi_free_pool(event_log.buffer);
792 event_log.buffer = NULL;
793 efi_free_pool(event_log.final_buffer);
794 event_log.final_buffer = NULL;
Masahisa Kojima0fd43792021-12-07 14:15:31 +0900795
796 if (!is_tcg2_protocol_installed())
797 return;
798
Ilias Apalodimas4953c992023-06-19 14:14:02 +0300799 ret = efi_uninstall_multiple_protocol_interfaces(efi_root, &efi_guid_tcg2_protocol,
800 &efi_tcg2_protocol, NULL);
Masahisa Kojima0fd43792021-12-07 14:15:31 +0900801 if (ret != EFI_SUCCESS)
802 log_err("Failed to remove EFI TCG2 protocol\n");
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200803}
804
805/**
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200806 * create_final_event() - Create the final event and install the config
807 * defined by the TCG EFI spec
808 */
809static efi_status_t create_final_event(void)
810{
811 struct efi_tcg2_final_events_table *final_event;
812 efi_status_t ret;
813
814 /*
815 * All events generated after the invocation of
816 * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an
817 * EFI_CONFIGURATION_TABLE
818 */
819 ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE,
820 &event_log.final_buffer);
821 if (ret != EFI_SUCCESS)
822 goto out;
823
824 memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE);
825 final_event = event_log.final_buffer;
826 final_event->number_of_events = 0;
827 final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
828 event_log.final_pos = sizeof(*final_event);
829 ret = efi_install_configuration_table(&efi_guid_final_events,
830 final_event);
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +0300831 if (ret != EFI_SUCCESS) {
832 efi_free_pool(event_log.final_buffer);
833 event_log.final_buffer = NULL;
834 }
835
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200836out:
837 return ret;
838}
839
840/**
Eddie James8ed7bb32023-10-24 10:43:49 -0500841 * measure_event() - common function to add event log and extend PCR
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900842 *
843 * @dev: TPM device
844 * @pcr_index: PCR index
845 * @event_type: type of event added
846 * @size: event size
847 * @event: event data
848 *
849 * Return: status code
850 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500851static efi_status_t measure_event(struct udevice *dev, u32 pcr_index,
852 u32 event_type, u32 size, u8 event[])
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900853{
854 struct tpml_digest_values digest_list;
855 efi_status_t ret;
856
Eddie James8ed7bb32023-10-24 10:43:49 -0500857 ret = tcg2_create_digest(dev, event, size, &digest_list);
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900858 if (ret != EFI_SUCCESS)
859 goto out;
860
861 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
862 if (ret != EFI_SUCCESS)
863 goto out;
864
865 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
866 size, event);
867
868out:
869 return ret;
870}
871
872/**
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200873 * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the
874 * eventlog and extend the PCRs
875 *
876 * @dev: TPM device
877 *
878 * @Return: status code
879 */
880static efi_status_t efi_append_scrtm_version(struct udevice *dev)
881{
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200882 efi_status_t ret;
883
Eddie James8ed7bb32023-10-24 10:43:49 -0500884 ret = measure_event(dev, 0, EV_S_CRTM_VERSION,
885 strlen(version_string) + 1, (u8 *)version_string);
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200886
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530887 return ret;
888}
889
890/**
891 * efi_init_event_log() - initialize an eventlog
892 *
893 * Return: status code
894 */
895static efi_status_t efi_init_event_log(void)
896{
897 /*
898 * vendor_info_size is currently set to 0, we need to change the length
899 * and allocate the flexible array member if this changes
900 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500901 struct tcg2_event_log elog;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530902 struct udevice *dev;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530903 efi_status_t ret;
904
Eddie James8ed7bb32023-10-24 10:43:49 -0500905 ret = tcg2_platform_get_tpm2(&dev);
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530906 if (ret != EFI_SUCCESS)
907 return ret;
908
909 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE,
910 (void **)&event_log.buffer);
911 if (ret != EFI_SUCCESS)
912 return ret;
913
914 /*
915 * initialize log area as 0xff so the OS can easily figure out the
916 * last log entry
917 */
918 memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE);
919
920 /*
921 * The log header is defined to be in SHA1 event log entry format.
922 * Setup event header
923 */
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530924 event_log.pos = 0;
925 event_log.last_event_size = 0;
926 event_log.get_event_called = false;
927 event_log.ebs_called = false;
928 event_log.truncated = false;
929
930 /*
931 * Check if earlier firmware have passed any eventlog. Different
932 * platforms can use different ways to do so.
933 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500934 elog.log = event_log.buffer;
935 elog.log_size = TPM2_EVENT_LOG_SIZE;
936 ret = tcg2_log_prepare_buffer(dev, &elog, false);
937 if (ret != EFI_SUCCESS)
938 goto free_pool;
939
940 event_log.pos = elog.log_position;
941
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530942 /*
Eddie James8ed7bb32023-10-24 10:43:49 -0500943 * Add SCRTM version to the log if previous firmmware
944 * doesn't pass an eventlog.
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530945 */
Ilias Apalodimas12c15f52023-11-07 13:31:34 +0200946 if (!elog.found) {
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530947 ret = efi_append_scrtm_version(dev);
Ilias Apalodimas12c15f52023-11-07 13:31:34 +0200948 if (ret != EFI_SUCCESS)
949 goto free_pool;
950 }
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530951
952 ret = create_final_event();
953 if (ret != EFI_SUCCESS)
954 goto free_pool;
955
956 return ret;
957
958free_pool:
959 efi_free_pool(event_log.buffer);
960 event_log.buffer = NULL;
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200961 return ret;
962}
963
964/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900965 * tcg2_measure_variable() - add variable event log and extend PCR
966 *
967 * @dev: TPM device
968 * @pcr_index: PCR index
969 * @event_type: type of event added
970 * @var_name: variable name
971 * @guid: guid
972 * @data_size: variable data size
973 * @data: variable data
974 *
975 * Return: status code
976 */
977static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index,
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +0200978 u32 event_type, const u16 *var_name,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900979 const efi_guid_t *guid,
980 efi_uintn_t data_size, u8 *data)
981{
982 u32 event_size;
983 efi_status_t ret;
984 struct efi_tcg2_uefi_variable_data *event;
985
986 event_size = sizeof(event->variable_name) +
987 sizeof(event->unicode_name_length) +
988 sizeof(event->variable_data_length) +
989 (u16_strlen(var_name) * sizeof(u16)) + data_size;
990 event = malloc(event_size);
991 if (!event)
992 return EFI_OUT_OF_RESOURCES;
993
994 guidcpy(&event->variable_name, guid);
995 event->unicode_name_length = u16_strlen(var_name);
996 event->variable_data_length = data_size;
997 memcpy(event->unicode_name, var_name,
998 (event->unicode_name_length * sizeof(u16)));
999 if (data) {
1000 memcpy((u16 *)event->unicode_name + event->unicode_name_length,
1001 data, data_size);
1002 }
Eddie James8ed7bb32023-10-24 10:43:49 -05001003 ret = measure_event(dev, pcr_index, event_type, event_size,
1004 (u8 *)event);
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001005 free(event);
1006 return ret;
1007}
1008
1009/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001010 * tcg2_measure_boot_variable() - measure boot variables
1011 *
1012 * @dev: TPM device
1013 *
1014 * Return: status code
1015 */
1016static efi_status_t tcg2_measure_boot_variable(struct udevice *dev)
1017{
1018 u16 *boot_order;
1019 u16 *boot_index;
Simon Glass90975372022-01-23 12:55:12 -07001020 u16 var_name[] = u"BootOrder";
1021 u16 boot_name[] = u"Boot####";
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001022 u8 *bootvar;
1023 efi_uintn_t var_data_size;
1024 u32 count, i;
1025 efi_status_t ret;
1026
1027 boot_order = efi_get_var(var_name, &efi_global_variable_guid,
1028 &var_data_size);
1029 if (!boot_order) {
Masahisa Kojimad1325932021-11-09 18:44:54 +09001030 /* If "BootOrder" is not defined, skip the boot variable measurement */
1031 return EFI_SUCCESS;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001032 }
1033
1034 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name,
1035 &efi_global_variable_guid, var_data_size,
1036 (u8 *)boot_order);
1037 if (ret != EFI_SUCCESS)
1038 goto error;
1039
1040 count = var_data_size / sizeof(*boot_order);
1041 boot_index = boot_order;
1042 for (i = 0; i < count; i++) {
1043 efi_create_indexed_name(boot_name, sizeof(boot_name),
1044 "Boot", *boot_index++);
1045
1046 bootvar = efi_get_var(boot_name, &efi_global_variable_guid,
1047 &var_data_size);
1048
1049 if (!bootvar) {
Masahisa Kojimaaca20c82021-11-09 20:35:53 +09001050 log_debug("%ls not found\n", boot_name);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001051 continue;
1052 }
1053
1054 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2,
1055 boot_name,
1056 &efi_global_variable_guid,
1057 var_data_size, bootvar);
1058 free(bootvar);
1059 if (ret != EFI_SUCCESS)
1060 goto error;
1061 }
1062
1063error:
1064 free(boot_order);
1065 return ret;
1066}
1067
1068/**
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001069 * tcg2_measure_smbios() - measure smbios table
1070 *
1071 * @dev: TPM device
1072 * @entry: pointer to the smbios_entry structure
1073 *
1074 * Return: status code
1075 */
1076static efi_status_t
1077tcg2_measure_smbios(struct udevice *dev,
1078 const struct smbios_entry *entry)
1079{
1080 efi_status_t ret;
1081 struct smbios_header *smbios_copy;
1082 struct smbios_handoff_table_pointers2 *event = NULL;
1083 u32 event_size;
1084
1085 /*
1086 * TCG PC Client PFP Spec says
1087 * "SMBIOS structures that contain static configuration information
1088 * (e.g. Platform Manufacturer Enterprise Number assigned by IANA,
1089 * platform model number, Vendor and Device IDs for each SMBIOS table)
1090 * that is relevant to the security of the platform MUST be measured".
1091 * Device dependent parameters such as serial number are cleared to
1092 * zero or spaces for the measurement.
1093 */
1094 event_size = sizeof(struct smbios_handoff_table_pointers2) +
1095 FIELD_SIZEOF(struct efi_configuration_table, guid) +
1096 entry->struct_table_length;
1097 event = calloc(1, event_size);
1098 if (!event) {
1099 ret = EFI_OUT_OF_RESOURCES;
1100 goto out;
1101 }
1102
1103 event->table_description_size = sizeof(SMBIOS_HANDOFF_TABLE_DESC);
1104 memcpy(event->table_description, SMBIOS_HANDOFF_TABLE_DESC,
1105 sizeof(SMBIOS_HANDOFF_TABLE_DESC));
1106 put_unaligned_le64(1, &event->number_of_tables);
1107 guidcpy(&event->table_entry[0].guid, &smbios_guid);
1108 smbios_copy = (struct smbios_header *)((uintptr_t)&event->table_entry[0].table);
1109 memcpy(&event->table_entry[0].table,
1110 (void *)((uintptr_t)entry->struct_table_address),
1111 entry->struct_table_length);
1112
1113 smbios_prepare_measurement(entry, smbios_copy);
1114
Eddie James8ed7bb32023-10-24 10:43:49 -05001115 ret = measure_event(dev, 1, EV_EFI_HANDOFF_TABLES2, event_size,
1116 (u8 *)event);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001117 if (ret != EFI_SUCCESS)
1118 goto out;
1119
1120out:
1121 free(event);
1122
1123 return ret;
1124}
1125
1126/**
1127 * find_smbios_table() - find smbios table
1128 *
1129 * Return: pointer to the smbios table
1130 */
1131static void *find_smbios_table(void)
1132{
1133 u32 i;
1134
1135 for (i = 0; i < systab.nr_tables; i++) {
1136 if (!guidcmp(&smbios_guid, &systab.tables[i].guid))
1137 return systab.tables[i].table;
1138 }
1139
1140 return NULL;
1141}
1142
1143/**
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001144 * tcg2_measure_gpt_table() - measure gpt table
1145 *
1146 * @dev: TPM device
1147 * @loaded_image: handle to the loaded image
1148 *
1149 * Return: status code
1150 */
1151static efi_status_t
1152tcg2_measure_gpt_data(struct udevice *dev,
1153 struct efi_loaded_image_obj *loaded_image)
1154{
1155 efi_status_t ret;
1156 efi_handle_t handle;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001157 struct efi_handler *dp_handler, *io_handler;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001158 struct efi_device_path *orig_device_path;
1159 struct efi_device_path *device_path;
1160 struct efi_device_path *dp;
1161 struct efi_block_io *block_io;
1162 struct efi_gpt_data *event = NULL;
1163 efi_guid_t null_guid = NULL_GUID;
1164 gpt_header *gpt_h;
1165 gpt_entry *entry = NULL;
1166 gpt_entry *gpt_e;
1167 u32 num_of_valid_entry = 0;
1168 u32 event_size;
1169 u32 i;
1170 u32 total_gpt_entry_size;
1171
1172 ret = efi_search_protocol(&loaded_image->header,
1173 &efi_guid_loaded_image_device_path,
1174 &dp_handler);
1175 if (ret != EFI_SUCCESS)
1176 return ret;
1177
1178 orig_device_path = dp_handler->protocol_interface;
1179 if (!orig_device_path) /* no device path, skip GPT measurement */
1180 return EFI_SUCCESS;
1181
1182 device_path = efi_dp_dup(orig_device_path);
1183 if (!device_path)
1184 return EFI_OUT_OF_RESOURCES;
1185
1186 dp = search_gpt_dp_node(device_path);
1187 if (!dp) {
1188 /* no GPT device path node found, skip GPT measurement */
1189 ret = EFI_SUCCESS;
1190 goto out1;
1191 }
1192
1193 /* read GPT header */
1194 dp->type = DEVICE_PATH_TYPE_END;
1195 dp->sub_type = DEVICE_PATH_SUB_TYPE_END;
1196 dp = device_path;
1197 ret = EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_guid,
1198 &dp, &handle));
1199 if (ret != EFI_SUCCESS)
1200 goto out1;
1201
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001202 ret = efi_search_protocol(handle, &efi_block_io_guid, &io_handler);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001203 if (ret != EFI_SUCCESS)
1204 goto out1;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001205 block_io = io_handler->protocol_interface;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001206
1207 gpt_h = memalign(block_io->media->io_align, block_io->media->block_size);
1208 if (!gpt_h) {
1209 ret = EFI_OUT_OF_RESOURCES;
1210 goto out2;
1211 }
1212
1213 ret = block_io->read_blocks(block_io, block_io->media->media_id, 1,
1214 block_io->media->block_size, gpt_h);
1215 if (ret != EFI_SUCCESS)
1216 goto out2;
1217
1218 /* read GPT entry */
1219 total_gpt_entry_size = gpt_h->num_partition_entries *
1220 gpt_h->sizeof_partition_entry;
1221 entry = memalign(block_io->media->io_align, total_gpt_entry_size);
1222 if (!entry) {
1223 ret = EFI_OUT_OF_RESOURCES;
1224 goto out2;
1225 }
1226
1227 ret = block_io->read_blocks(block_io, block_io->media->media_id,
1228 gpt_h->partition_entry_lba,
1229 total_gpt_entry_size, entry);
1230 if (ret != EFI_SUCCESS)
1231 goto out2;
1232
1233 /* count valid GPT entry */
1234 gpt_e = entry;
1235 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1236 if (guidcmp(&null_guid, &gpt_e->partition_type_guid))
1237 num_of_valid_entry++;
1238
1239 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1240 }
1241
1242 /* prepare event data for measurement */
1243 event_size = sizeof(struct efi_gpt_data) +
1244 (num_of_valid_entry * gpt_h->sizeof_partition_entry);
1245 event = calloc(1, event_size);
1246 if (!event) {
1247 ret = EFI_OUT_OF_RESOURCES;
1248 goto out2;
1249 }
1250 memcpy(event, gpt_h, sizeof(gpt_header));
1251 put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions);
1252
1253 /* copy valid GPT entry */
1254 gpt_e = entry;
1255 num_of_valid_entry = 0;
1256 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1257 if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) {
1258 memcpy((u8 *)event->partitions +
1259 (num_of_valid_entry * gpt_h->sizeof_partition_entry),
1260 gpt_e, gpt_h->sizeof_partition_entry);
1261 num_of_valid_entry++;
1262 }
1263
1264 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1265 }
1266
Eddie James8ed7bb32023-10-24 10:43:49 -05001267 ret = measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *)event);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001268
1269out2:
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001270 free(gpt_h);
1271 free(entry);
1272 free(event);
1273out1:
1274 efi_free_pool(device_path);
1275
1276 return ret;
1277}
1278
Etienne Carriereb9064352023-02-16 17:29:48 +01001279/* Return the byte size of reserved map area in DTB or -1 upon error */
1280static ssize_t size_of_rsvmap(void *dtb)
1281{
1282 struct fdt_reserve_entry e;
1283 ssize_t size_max;
1284 ssize_t size;
1285 u8 *rsvmap_base;
1286
1287 rsvmap_base = (u8 *)dtb + fdt_off_mem_rsvmap(dtb);
1288 size_max = fdt_totalsize(dtb) - fdt_off_mem_rsvmap(dtb);
1289 size = 0;
1290
1291 do {
1292 memcpy(&e, rsvmap_base + size, sizeof(e));
1293 size += sizeof(e);
1294 if (size > size_max)
1295 return -1;
1296 } while (e.size);
1297
1298 return size;
1299}
1300
1301/**
1302 * efi_tcg2_measure_dtb() - measure DTB passed to the OS
1303 *
1304 * @dtb: pointer to the device tree blob
1305 *
1306 * Return: status code
1307 */
1308efi_status_t efi_tcg2_measure_dtb(void *dtb)
1309{
1310 struct uefi_platform_firmware_blob2 *blob;
1311 struct fdt_header *header;
1312 sha256_context hash_ctx;
1313 struct udevice *dev;
1314 ssize_t rsvmap_size;
1315 efi_status_t ret;
1316 u32 event_size;
1317
1318 if (!is_tcg2_protocol_installed())
1319 return EFI_SUCCESS;
1320
Eddie James8ed7bb32023-10-24 10:43:49 -05001321 ret = tcg2_platform_get_tpm2(&dev);
Etienne Carriereb9064352023-02-16 17:29:48 +01001322 if (ret != EFI_SUCCESS)
1323 return EFI_SECURITY_VIOLATION;
1324
1325 rsvmap_size = size_of_rsvmap(dtb);
1326 if (rsvmap_size < 0)
1327 return EFI_SECURITY_VIOLATION;
1328
1329 event_size = sizeof(*blob) + sizeof(EFI_DTB_EVENT_STRING) + SHA256_SUM_LEN;
1330 blob = calloc(1, event_size);
1331 if (!blob)
1332 return EFI_OUT_OF_RESOURCES;
1333
1334 blob->blob_description_size = sizeof(EFI_DTB_EVENT_STRING);
1335 memcpy(blob->data, EFI_DTB_EVENT_STRING, blob->blob_description_size);
1336
1337 /* Measure populated areas of the DTB */
1338 header = dtb;
1339 sha256_starts(&hash_ctx);
1340 sha256_update(&hash_ctx, (u8 *)header, sizeof(struct fdt_header));
1341 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_struct(dtb), fdt_size_dt_strings(dtb));
1342 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_strings(dtb), fdt_size_dt_struct(dtb));
1343 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_mem_rsvmap(dtb), rsvmap_size);
1344 sha256_finish(&hash_ctx, blob->data + blob->blob_description_size);
1345
Eddie James8ed7bb32023-10-24 10:43:49 -05001346 ret = measure_event(dev, 0, EV_POST_CODE, event_size, (u8 *)blob);
Etienne Carriereb9064352023-02-16 17:29:48 +01001347
1348 free(blob);
1349 return ret;
1350}
1351
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001352/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001353 * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation
1354 *
1355 * Return: status code
1356 */
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001357efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_image_obj *handle)
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001358{
1359 efi_status_t ret;
1360 u32 pcr_index;
1361 struct udevice *dev;
1362 u32 event = 0;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001363 struct smbios_entry *entry;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001364
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001365 if (!is_tcg2_protocol_installed())
1366 return EFI_SUCCESS;
1367
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001368 if (tcg2_efi_app_invoked)
1369 return EFI_SUCCESS;
1370
Eddie James8ed7bb32023-10-24 10:43:49 -05001371 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001372 if (ret != EFI_SUCCESS)
Masahisa Kojima38155ea2021-12-07 14:15:33 +09001373 return EFI_SECURITY_VIOLATION;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001374
1375 ret = tcg2_measure_boot_variable(dev);
1376 if (ret != EFI_SUCCESS)
1377 goto out;
1378
Eddie James8ed7bb32023-10-24 10:43:49 -05001379 ret = measure_event(dev, 4, EV_EFI_ACTION,
1380 strlen(EFI_CALLING_EFI_APPLICATION),
1381 (u8 *)EFI_CALLING_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001382 if (ret != EFI_SUCCESS)
1383 goto out;
1384
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001385 entry = (struct smbios_entry *)find_smbios_table();
1386 if (entry) {
1387 ret = tcg2_measure_smbios(dev, entry);
1388 if (ret != EFI_SUCCESS)
1389 goto out;
1390 }
1391
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001392 ret = tcg2_measure_gpt_data(dev, handle);
1393 if (ret != EFI_SUCCESS)
1394 goto out;
1395
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001396 for (pcr_index = 0; pcr_index <= 7; pcr_index++) {
Eddie James8ed7bb32023-10-24 10:43:49 -05001397 ret = measure_event(dev, pcr_index, EV_SEPARATOR,
1398 sizeof(event), (u8 *)&event);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001399 if (ret != EFI_SUCCESS)
1400 goto out;
1401 }
1402
1403 tcg2_efi_app_invoked = true;
1404out:
1405 return ret;
1406}
1407
1408/**
1409 * efi_tcg2_measure_efi_app_exit() - measure efi app exit
1410 *
1411 * Return: status code
1412 */
1413efi_status_t efi_tcg2_measure_efi_app_exit(void)
1414{
1415 efi_status_t ret;
1416 struct udevice *dev;
1417
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001418 if (!is_tcg2_protocol_installed())
1419 return EFI_SUCCESS;
1420
Eddie James8ed7bb32023-10-24 10:43:49 -05001421 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001422 if (ret != EFI_SUCCESS)
1423 return ret;
1424
Eddie James8ed7bb32023-10-24 10:43:49 -05001425 ret = measure_event(dev, 4, EV_EFI_ACTION,
1426 strlen(EFI_RETURNING_FROM_EFI_APPLICATION),
1427 (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001428 return ret;
1429}
1430
1431/**
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001432 * efi_tcg2_notify_exit_boot_services() - ExitBootService callback
1433 *
1434 * @event: callback event
1435 * @context: callback context
1436 */
1437static void EFIAPI
1438efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context)
1439{
1440 efi_status_t ret;
1441 struct udevice *dev;
1442
1443 EFI_ENTRY("%p, %p", event, context);
1444
Ilias Apalodimas24e841a2021-11-18 09:03:39 +02001445 event_log.ebs_called = true;
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001446
1447 if (!is_tcg2_protocol_installed()) {
1448 ret = EFI_SUCCESS;
1449 goto out;
1450 }
1451
Eddie James8ed7bb32023-10-24 10:43:49 -05001452 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001453 if (ret != EFI_SUCCESS)
1454 goto out;
1455
Eddie James8ed7bb32023-10-24 10:43:49 -05001456 ret = measure_event(dev, 5, EV_EFI_ACTION,
1457 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1458 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001459 if (ret != EFI_SUCCESS)
1460 goto out;
1461
Eddie James8ed7bb32023-10-24 10:43:49 -05001462 ret = measure_event(dev, 5, EV_EFI_ACTION,
1463 strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED),
1464 (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001465
1466out:
1467 EFI_EXIT(ret);
1468}
1469
1470/**
1471 * efi_tcg2_notify_exit_boot_services_failed()
1472 * - notify ExitBootServices() is failed
1473 *
1474 * Return: status code
1475 */
1476efi_status_t efi_tcg2_notify_exit_boot_services_failed(void)
1477{
1478 struct udevice *dev;
1479 efi_status_t ret;
1480
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001481 if (!is_tcg2_protocol_installed())
1482 return EFI_SUCCESS;
1483
Eddie James8ed7bb32023-10-24 10:43:49 -05001484 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001485 if (ret != EFI_SUCCESS)
1486 goto out;
1487
Eddie James8ed7bb32023-10-24 10:43:49 -05001488 ret = measure_event(dev, 5, EV_EFI_ACTION,
1489 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1490 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001491 if (ret != EFI_SUCCESS)
1492 goto out;
1493
Eddie James8ed7bb32023-10-24 10:43:49 -05001494 ret = measure_event(dev, 5, EV_EFI_ACTION,
1495 strlen(EFI_EXIT_BOOT_SERVICES_FAILED),
1496 (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001497
1498out:
1499 return ret;
1500}
1501
1502/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001503 * tcg2_measure_secure_boot_variable() - measure secure boot variables
1504 *
1505 * @dev: TPM device
1506 *
1507 * Return: status code
1508 */
1509static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev)
1510{
1511 u8 *data;
1512 efi_uintn_t data_size;
1513 u32 count, i;
1514 efi_status_t ret;
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001515 u8 deployed_mode;
1516 efi_uintn_t size;
1517 u32 deployed_audit_pcr_index = 1;
1518
1519 size = sizeof(deployed_mode);
1520 ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid,
1521 NULL, &size, &deployed_mode, NULL);
1522 if (ret != EFI_SUCCESS || !deployed_mode)
1523 deployed_audit_pcr_index = 7;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001524
1525 count = ARRAY_SIZE(secure_variables);
1526 for (i = 0; i < count; i++) {
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001527 const efi_guid_t *guid;
1528
Masahisa Kojima21684522021-10-26 17:27:26 +09001529 guid = efi_auth_var_get_guid(secure_variables[i].name);
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001530
Masahisa Kojima21684522021-10-26 17:27:26 +09001531 data = efi_get_var(secure_variables[i].name, guid, &data_size);
1532 if (!data && !secure_variables[i].accept_empty)
1533 continue;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001534
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001535 if (u16_strcmp(u"DeployedMode", secure_variables[i].name))
1536 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1537 if (u16_strcmp(u"AuditMode", secure_variables[i].name))
1538 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1539
1540 ret = tcg2_measure_variable(dev, secure_variables[i].pcr_index,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001541 EV_EFI_VARIABLE_DRIVER_CONFIG,
Masahisa Kojima21684522021-10-26 17:27:26 +09001542 secure_variables[i].name, guid,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001543 data_size, data);
1544 free(data);
1545 if (ret != EFI_SUCCESS)
1546 goto error;
1547 }
1548
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001549error:
1550 return ret;
1551}
1552
1553/**
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001554 * efi_tcg2_do_initial_measurement() - do initial measurement
1555 *
1556 * Return: status code
1557 */
1558efi_status_t efi_tcg2_do_initial_measurement(void)
1559{
1560 efi_status_t ret;
1561 struct udevice *dev;
1562
1563 if (!is_tcg2_protocol_installed())
1564 return EFI_SUCCESS;
1565
Eddie James8ed7bb32023-10-24 10:43:49 -05001566 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001567 if (ret != EFI_SUCCESS)
1568 return EFI_SECURITY_VIOLATION;
1569
1570 ret = tcg2_measure_secure_boot_variable(dev);
1571 if (ret != EFI_SUCCESS)
1572 goto out;
1573
1574out:
1575 return ret;
1576}
1577
1578/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001579 * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
1580 *
1581 * If a TPM2 device is available, the TPM TCG2 Protocol is registered
1582 *
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001583 * Return: status code
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001584 */
1585efi_status_t efi_tcg2_register(void)
1586{
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001587 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001588 struct udevice *dev;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001589 struct efi_event *event;
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001590 u32 err;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001591
Eddie James8ed7bb32023-10-24 10:43:49 -05001592 ret = tcg2_platform_get_tpm2(&dev);
Ilias Apalodimasc67fef62020-11-16 08:52:41 +02001593 if (ret != EFI_SUCCESS) {
Ilias Apalodimaseb1b6b42023-01-19 16:29:15 +02001594 log_warning("Missing TPMv2 device for EFI_TCG_PROTOCOL\n");
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001595 return EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001596 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001597
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001598 /* initialize the TPM as early as possible. */
Ilias Apalodimas0c95d222023-01-25 13:06:03 +02001599 err = tpm_auto_start(dev);
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001600 if (err) {
1601 log_err("TPM startup failed\n");
1602 goto fail;
1603 }
1604
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001605 ret = efi_init_event_log();
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001606 if (ret != EFI_SUCCESS) {
1607 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001608 goto fail;
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001609 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001610
Ilias Apalodimas4953c992023-06-19 14:14:02 +03001611 ret = efi_install_multiple_protocol_interfaces(&efi_root, &efi_guid_tcg2_protocol,
1612 &efi_tcg2_protocol, NULL);
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001613 if (ret != EFI_SUCCESS) {
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001614 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001615 goto fail;
1616 }
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001617
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001618 ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
1619 efi_tcg2_notify_exit_boot_services, NULL,
1620 NULL, &event);
1621 if (ret != EFI_SUCCESS) {
1622 tcg2_uninit();
1623 goto fail;
1624 }
1625
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001626 return ret;
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001627
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001628fail:
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001629 log_err("Cannot install EFI_TCG2_PROTOCOL\n");
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001630 return ret;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001631}