blob: 85562c50a1cdbd557fcbe5766117b2116421c5d0 [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,
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001078 const struct smbios3_entry *entry)
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001079{
1080 efi_status_t ret;
1081 struct smbios_header *smbios_copy;
1082 struct smbios_handoff_table_pointers2 *event = NULL;
1083 u32 event_size;
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001084 const char smbios3_anchor[] = "_SM3_";
1085
1086 /* We only support SMBIOS 3.0 Entry Point structure */
1087 if (memcmp(entry->anchor, smbios3_anchor, sizeof(smbios3_anchor) - 1))
1088 return EFI_UNSUPPORTED;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001089
1090 /*
1091 * TCG PC Client PFP Spec says
1092 * "SMBIOS structures that contain static configuration information
1093 * (e.g. Platform Manufacturer Enterprise Number assigned by IANA,
1094 * platform model number, Vendor and Device IDs for each SMBIOS table)
1095 * that is relevant to the security of the platform MUST be measured".
1096 * Device dependent parameters such as serial number are cleared to
1097 * zero or spaces for the measurement.
1098 */
1099 event_size = sizeof(struct smbios_handoff_table_pointers2) +
1100 FIELD_SIZEOF(struct efi_configuration_table, guid) +
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001101 entry->max_struct_size;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001102 event = calloc(1, event_size);
1103 if (!event) {
1104 ret = EFI_OUT_OF_RESOURCES;
1105 goto out;
1106 }
1107
1108 event->table_description_size = sizeof(SMBIOS_HANDOFF_TABLE_DESC);
1109 memcpy(event->table_description, SMBIOS_HANDOFF_TABLE_DESC,
1110 sizeof(SMBIOS_HANDOFF_TABLE_DESC));
1111 put_unaligned_le64(1, &event->number_of_tables);
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001112 guidcpy(&event->table_entry[0].guid, &smbios3_guid);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001113 smbios_copy = (struct smbios_header *)((uintptr_t)&event->table_entry[0].table);
1114 memcpy(&event->table_entry[0].table,
1115 (void *)((uintptr_t)entry->struct_table_address),
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001116 entry->max_struct_size);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001117
1118 smbios_prepare_measurement(entry, smbios_copy);
1119
Eddie James8ed7bb32023-10-24 10:43:49 -05001120 ret = measure_event(dev, 1, EV_EFI_HANDOFF_TABLES2, event_size,
1121 (u8 *)event);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001122 if (ret != EFI_SUCCESS)
1123 goto out;
1124
1125out:
1126 free(event);
1127
1128 return ret;
1129}
1130
1131/**
1132 * find_smbios_table() - find smbios table
1133 *
1134 * Return: pointer to the smbios table
1135 */
1136static void *find_smbios_table(void)
1137{
1138 u32 i;
1139
1140 for (i = 0; i < systab.nr_tables; i++) {
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001141 if (!guidcmp(&smbios3_guid, &systab.tables[i].guid))
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001142 return systab.tables[i].table;
1143 }
1144
1145 return NULL;
1146}
1147
1148/**
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001149 * tcg2_measure_gpt_table() - measure gpt table
1150 *
1151 * @dev: TPM device
1152 * @loaded_image: handle to the loaded image
1153 *
1154 * Return: status code
1155 */
1156static efi_status_t
1157tcg2_measure_gpt_data(struct udevice *dev,
1158 struct efi_loaded_image_obj *loaded_image)
1159{
1160 efi_status_t ret;
1161 efi_handle_t handle;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001162 struct efi_handler *dp_handler, *io_handler;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001163 struct efi_device_path *orig_device_path;
1164 struct efi_device_path *device_path;
1165 struct efi_device_path *dp;
1166 struct efi_block_io *block_io;
1167 struct efi_gpt_data *event = NULL;
1168 efi_guid_t null_guid = NULL_GUID;
1169 gpt_header *gpt_h;
1170 gpt_entry *entry = NULL;
1171 gpt_entry *gpt_e;
1172 u32 num_of_valid_entry = 0;
1173 u32 event_size;
1174 u32 i;
1175 u32 total_gpt_entry_size;
1176
1177 ret = efi_search_protocol(&loaded_image->header,
1178 &efi_guid_loaded_image_device_path,
1179 &dp_handler);
1180 if (ret != EFI_SUCCESS)
1181 return ret;
1182
1183 orig_device_path = dp_handler->protocol_interface;
1184 if (!orig_device_path) /* no device path, skip GPT measurement */
1185 return EFI_SUCCESS;
1186
1187 device_path = efi_dp_dup(orig_device_path);
1188 if (!device_path)
1189 return EFI_OUT_OF_RESOURCES;
1190
1191 dp = search_gpt_dp_node(device_path);
1192 if (!dp) {
1193 /* no GPT device path node found, skip GPT measurement */
1194 ret = EFI_SUCCESS;
1195 goto out1;
1196 }
1197
1198 /* read GPT header */
1199 dp->type = DEVICE_PATH_TYPE_END;
1200 dp->sub_type = DEVICE_PATH_SUB_TYPE_END;
1201 dp = device_path;
1202 ret = EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_guid,
1203 &dp, &handle));
1204 if (ret != EFI_SUCCESS)
1205 goto out1;
1206
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001207 ret = efi_search_protocol(handle, &efi_block_io_guid, &io_handler);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001208 if (ret != EFI_SUCCESS)
1209 goto out1;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001210 block_io = io_handler->protocol_interface;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001211
1212 gpt_h = memalign(block_io->media->io_align, block_io->media->block_size);
1213 if (!gpt_h) {
1214 ret = EFI_OUT_OF_RESOURCES;
1215 goto out2;
1216 }
1217
1218 ret = block_io->read_blocks(block_io, block_io->media->media_id, 1,
1219 block_io->media->block_size, gpt_h);
1220 if (ret != EFI_SUCCESS)
1221 goto out2;
1222
1223 /* read GPT entry */
1224 total_gpt_entry_size = gpt_h->num_partition_entries *
1225 gpt_h->sizeof_partition_entry;
1226 entry = memalign(block_io->media->io_align, total_gpt_entry_size);
1227 if (!entry) {
1228 ret = EFI_OUT_OF_RESOURCES;
1229 goto out2;
1230 }
1231
1232 ret = block_io->read_blocks(block_io, block_io->media->media_id,
1233 gpt_h->partition_entry_lba,
1234 total_gpt_entry_size, entry);
1235 if (ret != EFI_SUCCESS)
1236 goto out2;
1237
1238 /* count valid GPT entry */
1239 gpt_e = entry;
1240 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1241 if (guidcmp(&null_guid, &gpt_e->partition_type_guid))
1242 num_of_valid_entry++;
1243
1244 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1245 }
1246
1247 /* prepare event data for measurement */
1248 event_size = sizeof(struct efi_gpt_data) +
1249 (num_of_valid_entry * gpt_h->sizeof_partition_entry);
1250 event = calloc(1, event_size);
1251 if (!event) {
1252 ret = EFI_OUT_OF_RESOURCES;
1253 goto out2;
1254 }
1255 memcpy(event, gpt_h, sizeof(gpt_header));
1256 put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions);
1257
1258 /* copy valid GPT entry */
1259 gpt_e = entry;
1260 num_of_valid_entry = 0;
1261 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1262 if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) {
1263 memcpy((u8 *)event->partitions +
1264 (num_of_valid_entry * gpt_h->sizeof_partition_entry),
1265 gpt_e, gpt_h->sizeof_partition_entry);
1266 num_of_valid_entry++;
1267 }
1268
1269 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1270 }
1271
Eddie James8ed7bb32023-10-24 10:43:49 -05001272 ret = measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *)event);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001273
1274out2:
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001275 free(gpt_h);
1276 free(entry);
1277 free(event);
1278out1:
1279 efi_free_pool(device_path);
1280
1281 return ret;
1282}
1283
Etienne Carriereb9064352023-02-16 17:29:48 +01001284/* Return the byte size of reserved map area in DTB or -1 upon error */
1285static ssize_t size_of_rsvmap(void *dtb)
1286{
1287 struct fdt_reserve_entry e;
1288 ssize_t size_max;
1289 ssize_t size;
1290 u8 *rsvmap_base;
1291
1292 rsvmap_base = (u8 *)dtb + fdt_off_mem_rsvmap(dtb);
1293 size_max = fdt_totalsize(dtb) - fdt_off_mem_rsvmap(dtb);
1294 size = 0;
1295
1296 do {
1297 memcpy(&e, rsvmap_base + size, sizeof(e));
1298 size += sizeof(e);
1299 if (size > size_max)
1300 return -1;
1301 } while (e.size);
1302
1303 return size;
1304}
1305
1306/**
1307 * efi_tcg2_measure_dtb() - measure DTB passed to the OS
1308 *
1309 * @dtb: pointer to the device tree blob
1310 *
1311 * Return: status code
1312 */
1313efi_status_t efi_tcg2_measure_dtb(void *dtb)
1314{
1315 struct uefi_platform_firmware_blob2 *blob;
1316 struct fdt_header *header;
1317 sha256_context hash_ctx;
1318 struct udevice *dev;
1319 ssize_t rsvmap_size;
1320 efi_status_t ret;
1321 u32 event_size;
1322
1323 if (!is_tcg2_protocol_installed())
1324 return EFI_SUCCESS;
1325
Eddie James8ed7bb32023-10-24 10:43:49 -05001326 ret = tcg2_platform_get_tpm2(&dev);
Etienne Carriereb9064352023-02-16 17:29:48 +01001327 if (ret != EFI_SUCCESS)
1328 return EFI_SECURITY_VIOLATION;
1329
1330 rsvmap_size = size_of_rsvmap(dtb);
1331 if (rsvmap_size < 0)
1332 return EFI_SECURITY_VIOLATION;
1333
1334 event_size = sizeof(*blob) + sizeof(EFI_DTB_EVENT_STRING) + SHA256_SUM_LEN;
1335 blob = calloc(1, event_size);
1336 if (!blob)
1337 return EFI_OUT_OF_RESOURCES;
1338
1339 blob->blob_description_size = sizeof(EFI_DTB_EVENT_STRING);
1340 memcpy(blob->data, EFI_DTB_EVENT_STRING, blob->blob_description_size);
1341
1342 /* Measure populated areas of the DTB */
1343 header = dtb;
1344 sha256_starts(&hash_ctx);
1345 sha256_update(&hash_ctx, (u8 *)header, sizeof(struct fdt_header));
1346 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_struct(dtb), fdt_size_dt_strings(dtb));
1347 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_strings(dtb), fdt_size_dt_struct(dtb));
1348 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_mem_rsvmap(dtb), rsvmap_size);
1349 sha256_finish(&hash_ctx, blob->data + blob->blob_description_size);
1350
Eddie James8ed7bb32023-10-24 10:43:49 -05001351 ret = measure_event(dev, 0, EV_POST_CODE, event_size, (u8 *)blob);
Etienne Carriereb9064352023-02-16 17:29:48 +01001352
1353 free(blob);
1354 return ret;
1355}
1356
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001357/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001358 * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation
1359 *
1360 * Return: status code
1361 */
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001362efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_image_obj *handle)
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001363{
1364 efi_status_t ret;
1365 u32 pcr_index;
1366 struct udevice *dev;
1367 u32 event = 0;
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001368 struct smbios3_entry *entry;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001369
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001370 if (!is_tcg2_protocol_installed())
1371 return EFI_SUCCESS;
1372
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001373 if (tcg2_efi_app_invoked)
1374 return EFI_SUCCESS;
1375
Eddie James8ed7bb32023-10-24 10:43:49 -05001376 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001377 if (ret != EFI_SUCCESS)
Masahisa Kojima38155ea2021-12-07 14:15:33 +09001378 return EFI_SECURITY_VIOLATION;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001379
1380 ret = tcg2_measure_boot_variable(dev);
1381 if (ret != EFI_SUCCESS)
1382 goto out;
1383
Eddie James8ed7bb32023-10-24 10:43:49 -05001384 ret = measure_event(dev, 4, EV_EFI_ACTION,
1385 strlen(EFI_CALLING_EFI_APPLICATION),
1386 (u8 *)EFI_CALLING_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001387 if (ret != EFI_SUCCESS)
1388 goto out;
1389
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001390 entry = (struct smbios3_entry *)find_smbios_table();
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001391 if (entry) {
1392 ret = tcg2_measure_smbios(dev, entry);
1393 if (ret != EFI_SUCCESS)
1394 goto out;
1395 }
1396
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001397 ret = tcg2_measure_gpt_data(dev, handle);
1398 if (ret != EFI_SUCCESS)
1399 goto out;
1400
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001401 for (pcr_index = 0; pcr_index <= 7; pcr_index++) {
Eddie James8ed7bb32023-10-24 10:43:49 -05001402 ret = measure_event(dev, pcr_index, EV_SEPARATOR,
1403 sizeof(event), (u8 *)&event);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001404 if (ret != EFI_SUCCESS)
1405 goto out;
1406 }
1407
1408 tcg2_efi_app_invoked = true;
1409out:
1410 return ret;
1411}
1412
1413/**
1414 * efi_tcg2_measure_efi_app_exit() - measure efi app exit
1415 *
1416 * Return: status code
1417 */
1418efi_status_t efi_tcg2_measure_efi_app_exit(void)
1419{
1420 efi_status_t ret;
1421 struct udevice *dev;
1422
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001423 if (!is_tcg2_protocol_installed())
1424 return EFI_SUCCESS;
1425
Eddie James8ed7bb32023-10-24 10:43:49 -05001426 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001427 if (ret != EFI_SUCCESS)
1428 return ret;
1429
Eddie James8ed7bb32023-10-24 10:43:49 -05001430 ret = measure_event(dev, 4, EV_EFI_ACTION,
1431 strlen(EFI_RETURNING_FROM_EFI_APPLICATION),
1432 (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001433 return ret;
1434}
1435
1436/**
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001437 * efi_tcg2_notify_exit_boot_services() - ExitBootService callback
1438 *
1439 * @event: callback event
1440 * @context: callback context
1441 */
1442static void EFIAPI
1443efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context)
1444{
1445 efi_status_t ret;
1446 struct udevice *dev;
1447
1448 EFI_ENTRY("%p, %p", event, context);
1449
Ilias Apalodimas24e841a2021-11-18 09:03:39 +02001450 event_log.ebs_called = true;
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001451
1452 if (!is_tcg2_protocol_installed()) {
1453 ret = EFI_SUCCESS;
1454 goto out;
1455 }
1456
Eddie James8ed7bb32023-10-24 10:43:49 -05001457 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001458 if (ret != EFI_SUCCESS)
1459 goto out;
1460
Eddie James8ed7bb32023-10-24 10:43:49 -05001461 ret = measure_event(dev, 5, EV_EFI_ACTION,
1462 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1463 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001464 if (ret != EFI_SUCCESS)
1465 goto out;
1466
Eddie James8ed7bb32023-10-24 10:43:49 -05001467 ret = measure_event(dev, 5, EV_EFI_ACTION,
1468 strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED),
1469 (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001470
1471out:
1472 EFI_EXIT(ret);
1473}
1474
1475/**
1476 * efi_tcg2_notify_exit_boot_services_failed()
1477 * - notify ExitBootServices() is failed
1478 *
1479 * Return: status code
1480 */
1481efi_status_t efi_tcg2_notify_exit_boot_services_failed(void)
1482{
1483 struct udevice *dev;
1484 efi_status_t ret;
1485
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001486 if (!is_tcg2_protocol_installed())
1487 return EFI_SUCCESS;
1488
Eddie James8ed7bb32023-10-24 10:43:49 -05001489 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001490 if (ret != EFI_SUCCESS)
1491 goto out;
1492
Eddie James8ed7bb32023-10-24 10:43:49 -05001493 ret = measure_event(dev, 5, EV_EFI_ACTION,
1494 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1495 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001496 if (ret != EFI_SUCCESS)
1497 goto out;
1498
Eddie James8ed7bb32023-10-24 10:43:49 -05001499 ret = measure_event(dev, 5, EV_EFI_ACTION,
1500 strlen(EFI_EXIT_BOOT_SERVICES_FAILED),
1501 (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001502
1503out:
1504 return ret;
1505}
1506
1507/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001508 * tcg2_measure_secure_boot_variable() - measure secure boot variables
1509 *
1510 * @dev: TPM device
1511 *
1512 * Return: status code
1513 */
1514static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev)
1515{
1516 u8 *data;
1517 efi_uintn_t data_size;
1518 u32 count, i;
1519 efi_status_t ret;
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001520 u8 deployed_mode;
1521 efi_uintn_t size;
1522 u32 deployed_audit_pcr_index = 1;
1523
1524 size = sizeof(deployed_mode);
1525 ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid,
1526 NULL, &size, &deployed_mode, NULL);
1527 if (ret != EFI_SUCCESS || !deployed_mode)
1528 deployed_audit_pcr_index = 7;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001529
1530 count = ARRAY_SIZE(secure_variables);
1531 for (i = 0; i < count; i++) {
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001532 const efi_guid_t *guid;
1533
Masahisa Kojima21684522021-10-26 17:27:26 +09001534 guid = efi_auth_var_get_guid(secure_variables[i].name);
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001535
Masahisa Kojima21684522021-10-26 17:27:26 +09001536 data = efi_get_var(secure_variables[i].name, guid, &data_size);
1537 if (!data && !secure_variables[i].accept_empty)
1538 continue;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001539
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001540 if (u16_strcmp(u"DeployedMode", secure_variables[i].name))
1541 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1542 if (u16_strcmp(u"AuditMode", secure_variables[i].name))
1543 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1544
1545 ret = tcg2_measure_variable(dev, secure_variables[i].pcr_index,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001546 EV_EFI_VARIABLE_DRIVER_CONFIG,
Masahisa Kojima21684522021-10-26 17:27:26 +09001547 secure_variables[i].name, guid,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001548 data_size, data);
1549 free(data);
1550 if (ret != EFI_SUCCESS)
1551 goto error;
1552 }
1553
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001554error:
1555 return ret;
1556}
1557
1558/**
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001559 * efi_tcg2_do_initial_measurement() - do initial measurement
1560 *
1561 * Return: status code
1562 */
1563efi_status_t efi_tcg2_do_initial_measurement(void)
1564{
1565 efi_status_t ret;
1566 struct udevice *dev;
1567
1568 if (!is_tcg2_protocol_installed())
1569 return EFI_SUCCESS;
1570
Eddie James8ed7bb32023-10-24 10:43:49 -05001571 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001572 if (ret != EFI_SUCCESS)
1573 return EFI_SECURITY_VIOLATION;
1574
1575 ret = tcg2_measure_secure_boot_variable(dev);
1576 if (ret != EFI_SUCCESS)
1577 goto out;
1578
1579out:
1580 return ret;
1581}
1582
1583/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001584 * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
1585 *
1586 * If a TPM2 device is available, the TPM TCG2 Protocol is registered
1587 *
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001588 * Return: status code
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001589 */
1590efi_status_t efi_tcg2_register(void)
1591{
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001592 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001593 struct udevice *dev;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001594 struct efi_event *event;
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001595 u32 err;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001596
Eddie James8ed7bb32023-10-24 10:43:49 -05001597 ret = tcg2_platform_get_tpm2(&dev);
Ilias Apalodimasc67fef62020-11-16 08:52:41 +02001598 if (ret != EFI_SUCCESS) {
Ilias Apalodimaseb1b6b42023-01-19 16:29:15 +02001599 log_warning("Missing TPMv2 device for EFI_TCG_PROTOCOL\n");
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001600 return EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001601 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001602
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001603 /* initialize the TPM as early as possible. */
Ilias Apalodimas0c95d222023-01-25 13:06:03 +02001604 err = tpm_auto_start(dev);
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001605 if (err) {
1606 log_err("TPM startup failed\n");
1607 goto fail;
1608 }
1609
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001610 ret = efi_init_event_log();
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001611 if (ret != EFI_SUCCESS) {
1612 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001613 goto fail;
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001614 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001615
Ilias Apalodimas4953c992023-06-19 14:14:02 +03001616 ret = efi_install_multiple_protocol_interfaces(&efi_root, &efi_guid_tcg2_protocol,
1617 &efi_tcg2_protocol, NULL);
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001618 if (ret != EFI_SUCCESS) {
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001619 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001620 goto fail;
1621 }
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001622
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001623 ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
1624 efi_tcg2_notify_exit_boot_services, NULL,
1625 NULL, &event);
1626 if (ret != EFI_SUCCESS) {
1627 tcg2_uninit();
1628 goto fail;
1629 }
1630
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001631 return ret;
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001632
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001633fail:
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001634 log_err("Cannot install EFI_TCG2_PROTOCOL\n");
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001635 return ret;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001636}