blob: 210a846ebc85215f26a256886a97aae1990cf6c5 [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
Heinrich Schuchardt955a3212025-01-16 20:26:59 +010011
Ilias Apalodimas590fef62020-11-11 11:18:11 +020012#include <dm.h>
13#include <efi_loader.h>
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +020014#include <efi_variable.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020015#include <efi_tcg2.h>
16#include <log.h>
Masahisa Kojima70be5a62021-05-26 12:09:58 +090017#include <malloc.h>
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +090018#include <smbios.h>
Pali Rohárba87ddf2021-08-02 15:18:31 +020019#include <version_string.h>
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +020020#include <tpm_api.h>
Masahisa Kojima70be5a62021-05-26 12:09:58 +090021#include <u-boot/hash-checksum.h>
Masahisa Kojimad420d8d2021-11-03 11:04:09 +090022#include <linux/unaligned/be_byteshift.h>
23#include <linux/unaligned/le_byteshift.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020024#include <linux/unaligned/generic.h>
Ilias Apalodimas967650d2020-11-30 11:47:40 +020025#include <hexdump.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020026
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020027/**
28 * struct event_log_buffer - internal eventlog management structure
29 *
30 * @buffer: eventlog buffer
31 * @final_buffer: finalevent config table buffer
32 * @pos: current position of 'buffer'
33 * @final_pos: current position of 'final_buffer'
34 * @get_event_called: true if GetEventLog has been invoked at least once
35 * @ebs_called: true if ExitBootServices has been invoked
36 * @truncated: true if the 'buffer' is truncated
37 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +020038struct event_log_buffer {
39 void *buffer;
40 void *final_buffer;
41 size_t pos; /* eventlog position */
42 size_t final_pos; /* final events config table position */
43 size_t last_event_size;
44 bool get_event_called;
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020045 bool ebs_called;
Ilias Apalodimas967650d2020-11-30 11:47:40 +020046 bool truncated;
47};
Ilias Apalodimas590fef62020-11-11 11:18:11 +020048
Ilias Apalodimas967650d2020-11-30 11:47:40 +020049static struct event_log_buffer event_log;
Masahisa Kojima8173cd42021-08-13 16:12:40 +090050static bool tcg2_efi_app_invoked;
Ilias Apalodimas590fef62020-11-11 11:18:11 +020051/*
52 * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset.
53 * Since the current tpm2_get_capability() response buffers starts at
54 * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate
55 * the response size and offset once for all consumers
56 */
57#define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \
58 offsetof(struct tpms_capability_data, data))
59#define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \
60 offsetof(struct tpms_tagged_property, value))
61
Ilias Apalodimas967650d2020-11-30 11:47:40 +020062static const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID;
63static const efi_guid_t efi_guid_final_events = EFI_TCG2_FINAL_EVENTS_TABLE_GUID;
64
Masahisa Kojima21684522021-10-26 17:27:26 +090065struct variable_info {
66 const u16 *name;
67 bool accept_empty;
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +090068 u32 pcr_index;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +090069};
70
Masahisa Kojima21684522021-10-26 17:27:26 +090071static struct variable_info secure_variables[] = {
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +090072 {u"SecureBoot", true, 7},
73 {u"PK", true, 7},
74 {u"KEK", true, 7},
75 {u"db", true, 7},
76 {u"dbx", true, 7},
77 {u"dbt", false, 7},
78 {u"dbr", false, 7},
79 {u"DeployedMode", false, 1},
80 {u"AuditMode", false, 1},
Masahisa Kojima21684522021-10-26 17:27:26 +090081};
82
Masahisa Kojima0fd43792021-12-07 14:15:31 +090083static bool is_tcg2_protocol_installed(void)
84{
85 struct efi_handler *handler;
86 efi_status_t ret;
87
88 ret = efi_search_protocol(efi_root, &efi_guid_tcg2_protocol, &handler);
89 return ret == EFI_SUCCESS;
90}
91
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020092/* tcg2_agile_log_append - Append an agile event to an eventlog
93 *
94 * @pcr_index: PCR index
95 * @event_type: type of event added
96 * @digest_list: list of digest algorithms to add
97 * @size: size of event
98 * @event: event to add
99 * @log: log buffer to append the event
100 *
101 * @Return: status code
102 */
103static efi_status_t tcg2_agile_log_append(u32 pcr_index, u32 event_type,
104 struct tpml_digest_values *digest_list,
105 u32 size, u8 event[])
106{
107 void *log = (void *)((uintptr_t)event_log.buffer + event_log.pos);
Eddie James8ed7bb32023-10-24 10:43:49 -0500108 u32 event_size = size + tcg2_event_get_size(digest_list);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200109 struct efi_tcg2_final_events_table *final_event;
110 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200111
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200112 /* if ExitBootServices hasn't been called update the normal log */
113 if (!event_log.ebs_called) {
114 if (event_log.truncated ||
Raymond Mao63242182025-01-27 06:49:35 -0800115 event_log.pos + event_size > CONFIG_TPM2_EVENT_LOG_SIZE) {
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200116 event_log.truncated = true;
117 return EFI_VOLUME_FULL;
118 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500119 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200120 event_log.pos += event_size;
121 event_log.last_event_size = event_size;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200122 }
123
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200124 if (!event_log.get_event_called)
125 return ret;
126
127 /* if GetEventLog has been called update FinalEventLog as well */
Raymond Mao63242182025-01-27 06:49:35 -0800128 if (event_log.final_pos + event_size > CONFIG_TPM2_EVENT_LOG_SIZE)
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200129 return EFI_VOLUME_FULL;
130
131 log = (void *)((uintptr_t)event_log.final_buffer + event_log.final_pos);
Eddie James8ed7bb32023-10-24 10:43:49 -0500132 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200133
134 final_event = event_log.final_buffer;
135 final_event->number_of_events++;
136 event_log.final_pos += event_size;
137
138 return ret;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200139}
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200140
141/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200142 * tpm2_get_max_command_size() - get the supported max command size
143 *
144 * @dev: TPM device
145 * @max_command_size: output buffer for the size
146 *
147 * Return: 0 on success, -1 on error
148 */
149static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size)
150{
151 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
152 u32 ret;
153
154 memset(response, 0, sizeof(response));
155 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
156 TPM2_PT_MAX_COMMAND_SIZE, response, 1);
157 if (ret)
158 return -1;
159
160 *max_command_size = (uint16_t)get_unaligned_be32(response +
161 properties_offset);
162
163 return 0;
164}
165
166/**
167 * tpm2_get_max_response_size() - get the supported max response size
168 *
169 * @dev: TPM device
170 * @max_response_size: output buffer for the size
171 *
172 * Return: 0 on success, -1 on error
173 */
174static int tpm2_get_max_response_size(struct udevice *dev,
175 u16 *max_response_size)
176{
177 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
178 u32 ret;
179
180 memset(response, 0, sizeof(response));
181 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
182 TPM2_PT_MAX_RESPONSE_SIZE, response, 1);
183 if (ret)
184 return -1;
185
186 *max_response_size = (uint16_t)get_unaligned_be32(response +
187 properties_offset);
188
189 return 0;
190}
191
192/**
193 * tpm2_get_manufacturer_id() - get the manufacturer ID
194 *
195 * @dev: TPM device
196 * @manufacturer_id: output buffer for the id
197 *
198 * Return: 0 on success, -1 on error
199 */
200static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id)
201{
202 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
203 u32 ret;
204
205 memset(response, 0, sizeof(response));
206 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
207 TPM2_PT_MANUFACTURER, response, 1);
208 if (ret)
209 return -1;
210
211 *manufacturer_id = get_unaligned_be32(response + properties_offset);
212
213 return 0;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200214}
215
216/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200217 * efi_tcg2_get_capability() - protocol capability information and state information
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200218 *
219 * @this: TCG2 protocol instance
220 * @capability: caller allocated memory with size field to the size of
221 * the structure allocated
222
223 * Return: status code
224 */
225static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200226efi_tcg2_get_capability(struct efi_tcg2_protocol *this,
227 struct efi_tcg2_boot_service_capability *capability)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200228{
229 struct udevice *dev;
230 efi_status_t efi_ret;
231 int ret;
232
233 EFI_ENTRY("%p, %p", this, capability);
234
235 if (!this || !capability) {
236 efi_ret = EFI_INVALID_PARAMETER;
237 goto out;
238 }
239
Masahisa Kojima9cc82932021-09-06 12:04:12 +0900240 if (capability->size < BOOT_SERVICE_CAPABILITY_MIN) {
241 capability->size = BOOT_SERVICE_CAPABILITY_MIN;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200242 efi_ret = EFI_BUFFER_TOO_SMALL;
243 goto out;
244 }
245
246 if (capability->size < sizeof(*capability)) {
247 capability->size = sizeof(*capability);
248 efi_ret = EFI_BUFFER_TOO_SMALL;
249 goto out;
250 }
251
252 capability->structure_version.major = 1;
253 capability->structure_version.minor = 1;
254 capability->protocol_version.major = 1;
255 capability->protocol_version.minor = 1;
256
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300257 ret = tcg2_platform_get_tpm2(&dev);
258 if (ret) {
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200259 capability->supported_event_logs = 0;
260 capability->hash_algorithm_bitmap = 0;
261 capability->tpm_present_flag = false;
262 capability->max_command_size = 0;
263 capability->max_response_size = 0;
264 capability->manufacturer_id = 0;
265 capability->number_of_pcr_banks = 0;
266 capability->active_pcr_banks = 0;
267
268 efi_ret = EFI_SUCCESS;
269 goto out;
270 }
271
272 /* We only allow a TPMv2 device to register the EFI protocol */
273 capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2;
274
275 capability->tpm_present_flag = true;
276
277 /* Supported and active PCRs */
278 capability->hash_algorithm_bitmap = 0;
279 capability->active_pcr_banks = 0;
Ilias Apalodimascb356612024-06-23 14:48:17 +0300280 ret = tcg2_get_pcr_info(dev, &capability->hash_algorithm_bitmap,
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200281 &capability->active_pcr_banks,
282 &capability->number_of_pcr_banks);
283 if (ret) {
284 efi_ret = EFI_DEVICE_ERROR;
285 goto out;
286 }
287
288 /* Max command size */
289 ret = tpm2_get_max_command_size(dev, &capability->max_command_size);
290 if (ret) {
291 efi_ret = EFI_DEVICE_ERROR;
292 goto out;
293 }
294
295 /* Max response size */
296 ret = tpm2_get_max_response_size(dev, &capability->max_response_size);
297 if (ret) {
298 efi_ret = EFI_DEVICE_ERROR;
299 goto out;
300 }
301
302 /* Manufacturer ID */
303 ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id);
304 if (ret) {
305 efi_ret = EFI_DEVICE_ERROR;
306 goto out;
307 }
308
309 return EFI_EXIT(EFI_SUCCESS);
310out:
311 return EFI_EXIT(efi_ret);
312}
313
314/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200315 * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its
316 * last entry
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200317 *
318 * @this: TCG2 protocol instance
319 * @log_format: type of event log format
320 * @event_log_location: pointer to the memory address of the event log
321 * @event_log_last_entry: pointer to the address of the start of the last
322 * entry in the event log in memory, if log contains
323 * more than 1 entry
324 * @event_log_truncated: set to true, if the Event Log is missing at i
325 * least one entry
326 *
327 * Return: status code
328 */
329static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200330efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this,
331 efi_tcg_event_log_format log_format,
332 u64 *event_log_location, u64 *event_log_last_entry,
333 bool *event_log_truncated)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200334{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200335 efi_status_t ret = EFI_SUCCESS;
336 struct udevice *dev;
337
338 EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location,
339 event_log_last_entry, event_log_truncated);
340
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900341 if (!this || !event_log_location || !event_log_last_entry ||
342 !event_log_truncated) {
343 ret = EFI_INVALID_PARAMETER;
344 goto out;
345 }
346
347 /* Only support TPMV2 */
348 if (log_format != TCG2_EVENT_LOG_FORMAT_TCG_2) {
349 ret = EFI_INVALID_PARAMETER;
350 goto out;
351 }
352
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300353 if (tcg2_platform_get_tpm2(&dev)) {
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200354 event_log_location = NULL;
355 event_log_last_entry = NULL;
356 *event_log_truncated = false;
357 ret = EFI_SUCCESS;
358 goto out;
359 }
360 *event_log_location = (uintptr_t)event_log.buffer;
361 *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos -
362 event_log.last_event_size);
363 *event_log_truncated = event_log.truncated;
364 event_log.get_event_called = true;
365
366out:
367 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200368}
369
370/**
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900371 * tcg2_hash_pe_image() - calculate PE/COFF image hash
372 *
373 * @efi: pointer to the EFI binary
374 * @efi_size: size of @efi binary
375 * @digest_list: list of digest algorithms to extend
376 *
377 * Return: status code
378 */
379static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size,
380 struct tpml_digest_values *digest_list)
381{
382 WIN_CERTIFICATE *wincerts = NULL;
383 size_t wincerts_len;
384 struct efi_image_regions *regs = NULL;
385 void *new_efi = NULL;
386 u8 hash[TPM2_SHA512_DIGEST_SIZE];
Eddie James8ed7bb32023-10-24 10:43:49 -0500387 struct udevice *dev;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300388 efi_status_t ret = EFI_SUCCESS;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900389 u32 active;
390 int i;
391
392 new_efi = efi_prepare_aligned_image(efi, &efi_size);
393 if (!new_efi)
394 return EFI_OUT_OF_RESOURCES;
395
396 if (!efi_image_parse(new_efi, efi_size, &regs, &wincerts,
397 &wincerts_len)) {
398 log_err("Parsing PE executable image failed\n");
399 ret = EFI_UNSUPPORTED;
400 goto out;
401 }
402
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300403 if (tcg2_platform_get_tpm2(&dev)) {
404 ret = EFI_DEVICE_ERROR;
Eddie James8ed7bb32023-10-24 10:43:49 -0500405 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300406 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500407
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300408 if (tcg2_get_active_pcr_banks(dev, &active)) {
409 ret = EFI_DEVICE_ERROR;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900410 goto out;
411 }
412
413 digest_list->count = 0;
Tim Harvey6ea1e052024-05-25 13:00:48 -0700414 for (i = 0; i < ARRAY_SIZE(hash_algo_list); i++) {
415 u16 hash_alg = hash_algo_list[i].hash_alg;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900416
Tim Harvey6ea1e052024-05-25 13:00:48 -0700417 if (!(active & hash_algo_list[i].hash_mask))
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900418 continue;
419 switch (hash_alg) {
420 case TPM2_ALG_SHA1:
421 hash_calculate("sha1", regs->reg, regs->num, hash);
422 break;
423 case TPM2_ALG_SHA256:
424 hash_calculate("sha256", regs->reg, regs->num, hash);
425 break;
426 case TPM2_ALG_SHA384:
427 hash_calculate("sha384", regs->reg, regs->num, hash);
428 break;
429 case TPM2_ALG_SHA512:
430 hash_calculate("sha512", regs->reg, regs->num, hash);
431 break;
432 default:
Heinrich Schuchardt09ec9f72023-07-31 14:11:34 +0200433 continue;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900434 }
Ruchika Guptae53007b2021-09-14 12:14:31 +0530435 digest_list->digests[digest_list->count].hash_alg = hash_alg;
436 memcpy(&digest_list->digests[digest_list->count].digest, hash,
Eddie James8ed7bb32023-10-24 10:43:49 -0500437 (u32)tpm2_algorithm_to_len(hash_alg));
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900438 digest_list->count++;
439 }
440
441out:
442 if (new_efi != efi)
443 free(new_efi);
444 free(regs);
445
446 return ret;
447}
448
449/**
450 * tcg2_measure_pe_image() - measure PE/COFF image
451 *
452 * @efi: pointer to the EFI binary
453 * @efi_size: size of @efi binary
454 * @handle: loaded image handle
455 * @loaded_image: loaded image protocol
456 *
457 * Return: status code
458 */
459efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size,
460 struct efi_loaded_image_obj *handle,
461 struct efi_loaded_image *loaded_image)
462{
463 struct tpml_digest_values digest_list;
464 efi_status_t ret;
465 struct udevice *dev;
466 u32 pcr_index, event_type, event_size;
467 struct uefi_image_load_event *image_load_event;
468 struct efi_device_path *device_path;
469 u32 device_path_length;
470 IMAGE_DOS_HEADER *dos;
471 IMAGE_NT_HEADERS32 *nt;
472 struct efi_handler *handler;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300473 int rc;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900474
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +0900475 if (!is_tcg2_protocol_installed())
476 return EFI_SUCCESS;
477
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300478 if (tcg2_platform_get_tpm2(&dev))
Masahisa Kojima38155ea2021-12-07 14:15:33 +0900479 return EFI_SECURITY_VIOLATION;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900480
481 switch (handle->image_type) {
482 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
483 pcr_index = 4;
484 event_type = EV_EFI_BOOT_SERVICES_APPLICATION;
485 break;
486 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
487 pcr_index = 2;
488 event_type = EV_EFI_BOOT_SERVICES_DRIVER;
489 break;
490 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
491 pcr_index = 2;
492 event_type = EV_EFI_RUNTIME_SERVICES_DRIVER;
493 break;
494 default:
495 return EFI_UNSUPPORTED;
496 }
497
498 ret = tcg2_hash_pe_image(efi, efi_size, &digest_list);
499 if (ret != EFI_SUCCESS)
500 return ret;
501
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300502 rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
503 if (rc)
504 return EFI_DEVICE_ERROR;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900505
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300506 ret = efi_search_protocol(&handle->header,
507 &efi_guid_loaded_image_device_path, &handler);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900508 if (ret != EFI_SUCCESS)
509 return ret;
510
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300511 device_path = handler->protocol_interface;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900512 device_path_length = efi_dp_size(device_path);
513 if (device_path_length > 0) {
514 /* add end node size */
515 device_path_length += sizeof(struct efi_device_path);
516 }
517 event_size = sizeof(struct uefi_image_load_event) + device_path_length;
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300518 image_load_event = calloc(1, event_size);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900519 if (!image_load_event)
520 return EFI_OUT_OF_RESOURCES;
521
522 image_load_event->image_location_in_memory = (uintptr_t)efi;
523 image_load_event->image_length_in_memory = efi_size;
524 image_load_event->length_of_device_path = device_path_length;
525
526 dos = (IMAGE_DOS_HEADER *)efi;
527 nt = (IMAGE_NT_HEADERS32 *)(efi + dos->e_lfanew);
528 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
529 IMAGE_NT_HEADERS64 *nt64 = (IMAGE_NT_HEADERS64 *)nt;
530
531 image_load_event->image_link_time_address =
532 nt64->OptionalHeader.ImageBase;
533 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
534 image_load_event->image_link_time_address =
535 nt->OptionalHeader.ImageBase;
536 } else {
537 ret = EFI_INVALID_PARAMETER;
538 goto out;
539 }
540
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300541 /* device_path_length might be zero */
542 memcpy(image_load_event->device_path, device_path, device_path_length);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900543
544 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
545 event_size, (u8 *)image_load_event);
546
547out:
548 free(image_load_event);
549
550 return ret;
551}
552
553/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200554 * efi_tcg2_hash_log_extend_event() - extend and optionally log events
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200555 *
556 * @this: TCG2 protocol instance
557 * @flags: bitmap providing additional information on the
558 * operation
559 * @data_to_hash: physical address of the start of the data buffer
560 * to be hashed
561 * @data_to_hash_len: the length in bytes of the buffer referenced by
562 * data_to_hash
563 * @efi_tcg_event: pointer to data buffer containing information
564 * about the event
565 *
566 * Return: status code
567 */
568static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200569efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags,
570 u64 data_to_hash, u64 data_to_hash_len,
571 struct efi_tcg2_event *efi_tcg_event)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200572{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200573 struct udevice *dev;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300574 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200575 u32 event_type, pcr_index, event_size;
576 struct tpml_digest_values digest_list;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300577 int rc = 0;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200578
579 EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash,
580 data_to_hash_len, efi_tcg_event);
581
582 if (!this || !data_to_hash || !efi_tcg_event) {
583 ret = EFI_INVALID_PARAMETER;
584 goto out;
585 }
586
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300587 if (tcg2_platform_get_tpm2(&dev)) {
588 ret = EFI_DEVICE_ERROR;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200589 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300590 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200591
592 if (efi_tcg_event->size < efi_tcg_event->header.header_size +
593 sizeof(u32)) {
594 ret = EFI_INVALID_PARAMETER;
595 goto out;
596 }
597
Masahisa Kojimab8074912021-09-03 10:55:52 +0900598 if (efi_tcg_event->header.pcr_index > EFI_TCG2_MAX_PCR_INDEX) {
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200599 ret = EFI_INVALID_PARAMETER;
600 goto out;
601 }
602
603 /*
604 * if PE_COFF_IMAGE is set we need to make sure the image is not
605 * corrupted, verify it and hash the PE/COFF image in accordance with
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900606 * the procedure specified in "Calculating the PE Image Hash"
607 * section of the "Windows Authenticode Portable Executable Signature
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200608 * Format"
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200609 */
610 if (flags & PE_COFF_IMAGE) {
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900611 ret = efi_check_pe((void *)(uintptr_t)data_to_hash,
Heinrich Schuchardtebe75212024-11-05 05:20:45 +0100612 data_to_hash_len, NULL);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900613 if (ret != EFI_SUCCESS) {
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900614 ret = EFI_UNSUPPORTED;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900615 goto out;
616 }
617 ret = tcg2_hash_pe_image((void *)(uintptr_t)data_to_hash,
618 data_to_hash_len, &digest_list);
619 } else {
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300620 rc = tcg2_create_digest(dev, (u8 *)(uintptr_t)data_to_hash,
621 data_to_hash_len, &digest_list);
622 if (rc)
623 ret = EFI_DEVICE_ERROR;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900624 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200625
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200626 if (ret != EFI_SUCCESS)
627 goto out;
628
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900629 pcr_index = efi_tcg_event->header.pcr_index;
630 event_type = efi_tcg_event->header.event_type;
631
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300632 rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
633 if (rc) {
634 ret = EFI_DEVICE_ERROR;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200635 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300636 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200637
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;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300675 efi_status_t ret = EFI_SUCCESS;
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900676 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
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300687 if (tcg2_platform_get_tpm2(&dev)) {
688 ret = EFI_DEVICE_ERROR;
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900689 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300690 }
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900691
692 rc = tpm2_submit_command(dev, input_param_block,
693 output_param_block, &resp_buf_size);
694 if (rc) {
695 ret = (rc == -ENOSPC) ? EFI_OUT_OF_RESOURCES : EFI_DEVICE_ERROR;
696
697 goto out;
698 }
699
700out:
701 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200702}
703
704/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200705 * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200706 *
707 * @this: TCG2 protocol instance
708 * @active_pcr_banks: pointer for receiving the bitmap of currently
709 * active PCR banks
710 *
711 * Return: status code
712 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200713static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200714efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
715 u32 *active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200716{
Eddie James8ed7bb32023-10-24 10:43:49 -0500717 struct udevice *dev;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300718 efi_status_t ret = EFI_INVALID_PARAMETER;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200719
Ilias Apalodimas918a6ea2023-10-24 10:43:53 -0500720 EFI_ENTRY("%p, %p", this, active_pcr_banks);
721
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300722 if (!this || !active_pcr_banks)
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900723 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300724
725 if (tcg2_platform_get_tpm2(&dev))
726 goto out;
727
728 /*
729 * EFI_INVALID_PARAMETER does not convey the problem type.
730 * but that's what currently the spec specifies.
731 * EFI_DEVICE_ERROR would be better
732 */
733 if (tcg2_get_active_pcr_banks(dev, active_pcr_banks))
Eddie James8ed7bb32023-10-24 10:43:49 -0500734 goto out;
735
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300736 ret = EFI_SUCCESS;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200737
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900738out:
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200739 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200740}
741
742/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200743 * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200744 *
745 * @this: TCG2 protocol instance
746 * @active_pcr_banks: bitmap of the requested active PCR banks
747 *
748 * Return: status code
749 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200750static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300751efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
752 u32 __maybe_unused active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200753{
754 return EFI_UNSUPPORTED;
755}
756
757/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200758 * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous
759 * set_active_pcr_banks()
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200760 *
761 * @this: TCG2 protocol instance
762 * @operation_present: non-zero value to indicate a
763 * set_active_pcr_banks operation was
764 * invoked during last boot
765 * @response: result value could be returned
766 *
767 * Return: status code
768 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200769static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300770efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
771 u32 __maybe_unused *operation_present,
772 u32 __maybe_unused *response)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200773{
774 return EFI_UNSUPPORTED;
775}
776
777static const struct efi_tcg2_protocol efi_tcg2_protocol = {
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200778 .get_capability = efi_tcg2_get_capability,
779 .get_eventlog = efi_tcg2_get_eventlog,
780 .hash_log_extend_event = efi_tcg2_hash_log_extend_event,
781 .submit_command = efi_tcg2_submit_command,
782 .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks,
783 .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks,
784 .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks,
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200785};
786
787/**
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200788 * tcg2_uninit - remove the final event table and free efi memory on failures
789 */
Ilias Apalodimas14e7ed92024-10-30 22:40:59 +0200790static void tcg2_uninit(void)
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200791{
792 efi_status_t ret;
793
794 ret = efi_install_configuration_table(&efi_guid_final_events, NULL);
Ilias Apalodimasf583b7e2024-11-28 09:11:20 +0200795 if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200796 log_err("Failed to delete final events config table\n");
797
798 efi_free_pool(event_log.buffer);
799 event_log.buffer = NULL;
800 efi_free_pool(event_log.final_buffer);
801 event_log.final_buffer = NULL;
Masahisa Kojima0fd43792021-12-07 14:15:31 +0900802
803 if (!is_tcg2_protocol_installed())
804 return;
805
Ilias Apalodimas4953c992023-06-19 14:14:02 +0300806 ret = efi_uninstall_multiple_protocol_interfaces(efi_root, &efi_guid_tcg2_protocol,
807 &efi_tcg2_protocol, NULL);
Masahisa Kojima0fd43792021-12-07 14:15:31 +0900808 if (ret != EFI_SUCCESS)
809 log_err("Failed to remove EFI TCG2 protocol\n");
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200810}
811
812/**
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200813 * create_final_event() - Create the final event and install the config
814 * defined by the TCG EFI spec
815 */
816static efi_status_t create_final_event(void)
817{
818 struct efi_tcg2_final_events_table *final_event;
819 efi_status_t ret;
820
821 /*
822 * All events generated after the invocation of
823 * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an
824 * EFI_CONFIGURATION_TABLE
825 */
Raymond Mao63242182025-01-27 06:49:35 -0800826 ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, CONFIG_TPM2_EVENT_LOG_SIZE,
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200827 &event_log.final_buffer);
828 if (ret != EFI_SUCCESS)
829 goto out;
830
Raymond Mao63242182025-01-27 06:49:35 -0800831 memset(event_log.final_buffer, 0xff, CONFIG_TPM2_EVENT_LOG_SIZE);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200832 final_event = event_log.final_buffer;
833 final_event->number_of_events = 0;
834 final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
835 event_log.final_pos = sizeof(*final_event);
836 ret = efi_install_configuration_table(&efi_guid_final_events,
837 final_event);
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +0300838 if (ret != EFI_SUCCESS) {
839 efi_free_pool(event_log.final_buffer);
840 event_log.final_buffer = NULL;
841 }
842
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200843out:
844 return ret;
845}
846
847/**
Eddie James8ed7bb32023-10-24 10:43:49 -0500848 * measure_event() - common function to add event log and extend PCR
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900849 *
850 * @dev: TPM device
851 * @pcr_index: PCR index
852 * @event_type: type of event added
853 * @size: event size
854 * @event: event data
855 *
856 * Return: status code
857 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500858static efi_status_t measure_event(struct udevice *dev, u32 pcr_index,
859 u32 event_type, u32 size, u8 event[])
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900860{
861 struct tpml_digest_values digest_list;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300862 efi_status_t ret = EFI_DEVICE_ERROR;
863 int rc;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900864
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300865 rc = tcg2_create_digest(dev, event, size, &digest_list);
866 if (rc)
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900867 goto out;
868
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300869 rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
870 if (rc)
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900871 goto out;
872
873 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
874 size, event);
875
876out:
877 return ret;
878}
879
880/**
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200881 * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the
882 * eventlog and extend the PCRs
883 *
884 * @dev: TPM device
885 *
886 * @Return: status code
887 */
888static efi_status_t efi_append_scrtm_version(struct udevice *dev)
889{
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200890 efi_status_t ret;
891
Eddie James8ed7bb32023-10-24 10:43:49 -0500892 ret = measure_event(dev, 0, EV_S_CRTM_VERSION,
893 strlen(version_string) + 1, (u8 *)version_string);
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200894
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530895 return ret;
896}
897
898/**
899 * efi_init_event_log() - initialize an eventlog
900 *
901 * Return: status code
902 */
903static efi_status_t efi_init_event_log(void)
904{
905 /*
906 * vendor_info_size is currently set to 0, we need to change the length
907 * and allocate the flexible array member if this changes
908 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500909 struct tcg2_event_log elog;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530910 struct udevice *dev;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530911 efi_status_t ret;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300912 int rc;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530913
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300914 if (tcg2_platform_get_tpm2(&dev))
915 return EFI_DEVICE_ERROR;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530916
Raymond Mao63242182025-01-27 06:49:35 -0800917 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
918 CONFIG_TPM2_EVENT_LOG_SIZE,
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530919 (void **)&event_log.buffer);
920 if (ret != EFI_SUCCESS)
921 return ret;
922
923 /*
924 * initialize log area as 0xff so the OS can easily figure out the
925 * last log entry
926 */
Raymond Mao63242182025-01-27 06:49:35 -0800927 memset(event_log.buffer, 0xff, CONFIG_TPM2_EVENT_LOG_SIZE);
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530928
929 /*
930 * The log header is defined to be in SHA1 event log entry format.
931 * Setup event header
932 */
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530933 event_log.pos = 0;
934 event_log.last_event_size = 0;
935 event_log.get_event_called = false;
936 event_log.ebs_called = false;
937 event_log.truncated = false;
938
939 /*
940 * Check if earlier firmware have passed any eventlog. Different
941 * platforms can use different ways to do so.
942 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500943 elog.log = event_log.buffer;
Raymond Mao63242182025-01-27 06:49:35 -0800944 elog.log_size = CONFIG_TPM2_EVENT_LOG_SIZE;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300945 rc = tcg2_log_prepare_buffer(dev, &elog, false);
946 if (rc) {
947 ret = (rc == -ENOBUFS) ? EFI_BUFFER_TOO_SMALL : EFI_DEVICE_ERROR;
Eddie James8ed7bb32023-10-24 10:43:49 -0500948 goto free_pool;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300949 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500950
951 event_log.pos = elog.log_position;
952
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530953 /*
Eddie James8ed7bb32023-10-24 10:43:49 -0500954 * Add SCRTM version to the log if previous firmmware
955 * doesn't pass an eventlog.
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530956 */
Ilias Apalodimas12c15f52023-11-07 13:31:34 +0200957 if (!elog.found) {
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530958 ret = efi_append_scrtm_version(dev);
Ilias Apalodimas12c15f52023-11-07 13:31:34 +0200959 if (ret != EFI_SUCCESS)
960 goto free_pool;
961 }
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530962
963 ret = create_final_event();
964 if (ret != EFI_SUCCESS)
965 goto free_pool;
966
967 return ret;
968
969free_pool:
970 efi_free_pool(event_log.buffer);
971 event_log.buffer = NULL;
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200972 return ret;
973}
974
975/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900976 * tcg2_measure_variable() - add variable event log and extend PCR
977 *
978 * @dev: TPM device
979 * @pcr_index: PCR index
980 * @event_type: type of event added
981 * @var_name: variable name
982 * @guid: guid
983 * @data_size: variable data size
984 * @data: variable data
985 *
986 * Return: status code
987 */
988static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index,
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +0200989 u32 event_type, const u16 *var_name,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900990 const efi_guid_t *guid,
991 efi_uintn_t data_size, u8 *data)
992{
993 u32 event_size;
994 efi_status_t ret;
995 struct efi_tcg2_uefi_variable_data *event;
996
997 event_size = sizeof(event->variable_name) +
998 sizeof(event->unicode_name_length) +
999 sizeof(event->variable_data_length) +
1000 (u16_strlen(var_name) * sizeof(u16)) + data_size;
1001 event = malloc(event_size);
1002 if (!event)
1003 return EFI_OUT_OF_RESOURCES;
1004
1005 guidcpy(&event->variable_name, guid);
1006 event->unicode_name_length = u16_strlen(var_name);
1007 event->variable_data_length = data_size;
1008 memcpy(event->unicode_name, var_name,
1009 (event->unicode_name_length * sizeof(u16)));
1010 if (data) {
1011 memcpy((u16 *)event->unicode_name + event->unicode_name_length,
1012 data, data_size);
1013 }
Eddie James8ed7bb32023-10-24 10:43:49 -05001014 ret = measure_event(dev, pcr_index, event_type, event_size,
1015 (u8 *)event);
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001016 free(event);
1017 return ret;
1018}
1019
1020/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001021 * tcg2_measure_boot_variable() - measure boot variables
1022 *
1023 * @dev: TPM device
1024 *
1025 * Return: status code
1026 */
1027static efi_status_t tcg2_measure_boot_variable(struct udevice *dev)
1028{
1029 u16 *boot_order;
1030 u16 *boot_index;
Simon Glass90975372022-01-23 12:55:12 -07001031 u16 var_name[] = u"BootOrder";
1032 u16 boot_name[] = u"Boot####";
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001033 u8 *bootvar;
1034 efi_uintn_t var_data_size;
1035 u32 count, i;
1036 efi_status_t ret;
1037
1038 boot_order = efi_get_var(var_name, &efi_global_variable_guid,
1039 &var_data_size);
1040 if (!boot_order) {
Masahisa Kojimad1325932021-11-09 18:44:54 +09001041 /* If "BootOrder" is not defined, skip the boot variable measurement */
1042 return EFI_SUCCESS;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001043 }
1044
1045 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name,
1046 &efi_global_variable_guid, var_data_size,
1047 (u8 *)boot_order);
1048 if (ret != EFI_SUCCESS)
1049 goto error;
1050
1051 count = var_data_size / sizeof(*boot_order);
1052 boot_index = boot_order;
1053 for (i = 0; i < count; i++) {
1054 efi_create_indexed_name(boot_name, sizeof(boot_name),
1055 "Boot", *boot_index++);
1056
1057 bootvar = efi_get_var(boot_name, &efi_global_variable_guid,
1058 &var_data_size);
1059
1060 if (!bootvar) {
Masahisa Kojimaaca20c82021-11-09 20:35:53 +09001061 log_debug("%ls not found\n", boot_name);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001062 continue;
1063 }
1064
1065 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2,
1066 boot_name,
1067 &efi_global_variable_guid,
1068 var_data_size, bootvar);
1069 free(bootvar);
1070 if (ret != EFI_SUCCESS)
1071 goto error;
1072 }
1073
1074error:
1075 free(boot_order);
1076 return ret;
1077}
1078
1079/**
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001080 * tcg2_measure_smbios() - measure smbios table
1081 *
1082 * @dev: TPM device
1083 * @entry: pointer to the smbios_entry structure
1084 *
1085 * Return: status code
1086 */
1087static efi_status_t
1088tcg2_measure_smbios(struct udevice *dev,
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001089 const struct smbios3_entry *entry)
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001090{
1091 efi_status_t ret;
1092 struct smbios_header *smbios_copy;
1093 struct smbios_handoff_table_pointers2 *event = NULL;
1094 u32 event_size;
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001095 const char smbios3_anchor[] = "_SM3_";
1096
1097 /* We only support SMBIOS 3.0 Entry Point structure */
1098 if (memcmp(entry->anchor, smbios3_anchor, sizeof(smbios3_anchor) - 1))
1099 return EFI_UNSUPPORTED;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001100
1101 /*
1102 * TCG PC Client PFP Spec says
1103 * "SMBIOS structures that contain static configuration information
1104 * (e.g. Platform Manufacturer Enterprise Number assigned by IANA,
1105 * platform model number, Vendor and Device IDs for each SMBIOS table)
1106 * that is relevant to the security of the platform MUST be measured".
1107 * Device dependent parameters such as serial number are cleared to
1108 * zero or spaces for the measurement.
1109 */
1110 event_size = sizeof(struct smbios_handoff_table_pointers2) +
1111 FIELD_SIZEOF(struct efi_configuration_table, guid) +
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +01001112 entry->table_maximum_size;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001113 event = calloc(1, event_size);
1114 if (!event) {
1115 ret = EFI_OUT_OF_RESOURCES;
1116 goto out;
1117 }
1118
1119 event->table_description_size = sizeof(SMBIOS_HANDOFF_TABLE_DESC);
1120 memcpy(event->table_description, SMBIOS_HANDOFF_TABLE_DESC,
1121 sizeof(SMBIOS_HANDOFF_TABLE_DESC));
1122 put_unaligned_le64(1, &event->number_of_tables);
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001123 guidcpy(&event->table_entry[0].guid, &smbios3_guid);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001124 smbios_copy = (struct smbios_header *)((uintptr_t)&event->table_entry[0].table);
1125 memcpy(&event->table_entry[0].table,
1126 (void *)((uintptr_t)entry->struct_table_address),
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +01001127 entry->table_maximum_size);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001128
1129 smbios_prepare_measurement(entry, smbios_copy);
1130
Eddie James8ed7bb32023-10-24 10:43:49 -05001131 ret = measure_event(dev, 1, EV_EFI_HANDOFF_TABLES2, event_size,
1132 (u8 *)event);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001133 if (ret != EFI_SUCCESS)
1134 goto out;
1135
1136out:
1137 free(event);
1138
1139 return ret;
1140}
1141
1142/**
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001143 * tcg2_measure_gpt_table() - measure gpt table
1144 *
1145 * @dev: TPM device
1146 * @loaded_image: handle to the loaded image
1147 *
1148 * Return: status code
1149 */
1150static efi_status_t
1151tcg2_measure_gpt_data(struct udevice *dev,
1152 struct efi_loaded_image_obj *loaded_image)
1153{
1154 efi_status_t ret;
1155 efi_handle_t handle;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001156 struct efi_handler *dp_handler, *io_handler;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001157 struct efi_device_path *orig_device_path;
1158 struct efi_device_path *device_path;
1159 struct efi_device_path *dp;
1160 struct efi_block_io *block_io;
1161 struct efi_gpt_data *event = NULL;
1162 efi_guid_t null_guid = NULL_GUID;
1163 gpt_header *gpt_h;
1164 gpt_entry *entry = NULL;
1165 gpt_entry *gpt_e;
1166 u32 num_of_valid_entry = 0;
1167 u32 event_size;
1168 u32 i;
1169 u32 total_gpt_entry_size;
1170
1171 ret = efi_search_protocol(&loaded_image->header,
1172 &efi_guid_loaded_image_device_path,
1173 &dp_handler);
1174 if (ret != EFI_SUCCESS)
1175 return ret;
1176
1177 orig_device_path = dp_handler->protocol_interface;
1178 if (!orig_device_path) /* no device path, skip GPT measurement */
1179 return EFI_SUCCESS;
1180
1181 device_path = efi_dp_dup(orig_device_path);
1182 if (!device_path)
1183 return EFI_OUT_OF_RESOURCES;
1184
1185 dp = search_gpt_dp_node(device_path);
1186 if (!dp) {
1187 /* no GPT device path node found, skip GPT measurement */
1188 ret = EFI_SUCCESS;
1189 goto out1;
1190 }
1191
1192 /* read GPT header */
1193 dp->type = DEVICE_PATH_TYPE_END;
1194 dp->sub_type = DEVICE_PATH_SUB_TYPE_END;
1195 dp = device_path;
1196 ret = EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_guid,
1197 &dp, &handle));
1198 if (ret != EFI_SUCCESS)
1199 goto out1;
1200
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001201 ret = efi_search_protocol(handle, &efi_block_io_guid, &io_handler);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001202 if (ret != EFI_SUCCESS)
1203 goto out1;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001204 block_io = io_handler->protocol_interface;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001205
1206 gpt_h = memalign(block_io->media->io_align, block_io->media->block_size);
1207 if (!gpt_h) {
1208 ret = EFI_OUT_OF_RESOURCES;
1209 goto out2;
1210 }
1211
1212 ret = block_io->read_blocks(block_io, block_io->media->media_id, 1,
1213 block_io->media->block_size, gpt_h);
1214 if (ret != EFI_SUCCESS)
1215 goto out2;
1216
1217 /* read GPT entry */
1218 total_gpt_entry_size = gpt_h->num_partition_entries *
1219 gpt_h->sizeof_partition_entry;
1220 entry = memalign(block_io->media->io_align, total_gpt_entry_size);
1221 if (!entry) {
1222 ret = EFI_OUT_OF_RESOURCES;
1223 goto out2;
1224 }
1225
1226 ret = block_io->read_blocks(block_io, block_io->media->media_id,
1227 gpt_h->partition_entry_lba,
1228 total_gpt_entry_size, entry);
1229 if (ret != EFI_SUCCESS)
1230 goto out2;
1231
1232 /* count valid GPT entry */
1233 gpt_e = entry;
1234 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1235 if (guidcmp(&null_guid, &gpt_e->partition_type_guid))
1236 num_of_valid_entry++;
1237
1238 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1239 }
1240
1241 /* prepare event data for measurement */
1242 event_size = sizeof(struct efi_gpt_data) +
1243 (num_of_valid_entry * gpt_h->sizeof_partition_entry);
1244 event = calloc(1, event_size);
1245 if (!event) {
1246 ret = EFI_OUT_OF_RESOURCES;
1247 goto out2;
1248 }
1249 memcpy(event, gpt_h, sizeof(gpt_header));
1250 put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions);
1251
1252 /* copy valid GPT entry */
1253 gpt_e = entry;
1254 num_of_valid_entry = 0;
1255 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1256 if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) {
1257 memcpy((u8 *)event->partitions +
1258 (num_of_valid_entry * gpt_h->sizeof_partition_entry),
1259 gpt_e, gpt_h->sizeof_partition_entry);
1260 num_of_valid_entry++;
1261 }
1262
1263 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1264 }
1265
Eddie James8ed7bb32023-10-24 10:43:49 -05001266 ret = measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *)event);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001267
1268out2:
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001269 free(gpt_h);
1270 free(entry);
1271 free(event);
1272out1:
1273 efi_free_pool(device_path);
1274
1275 return ret;
1276}
1277
Etienne Carriereb9064352023-02-16 17:29:48 +01001278/* Return the byte size of reserved map area in DTB or -1 upon error */
1279static ssize_t size_of_rsvmap(void *dtb)
1280{
1281 struct fdt_reserve_entry e;
1282 ssize_t size_max;
1283 ssize_t size;
1284 u8 *rsvmap_base;
1285
1286 rsvmap_base = (u8 *)dtb + fdt_off_mem_rsvmap(dtb);
1287 size_max = fdt_totalsize(dtb) - fdt_off_mem_rsvmap(dtb);
1288 size = 0;
1289
1290 do {
1291 memcpy(&e, rsvmap_base + size, sizeof(e));
1292 size += sizeof(e);
1293 if (size > size_max)
1294 return -1;
1295 } while (e.size);
1296
1297 return size;
1298}
1299
1300/**
1301 * efi_tcg2_measure_dtb() - measure DTB passed to the OS
1302 *
1303 * @dtb: pointer to the device tree blob
1304 *
1305 * Return: status code
1306 */
1307efi_status_t efi_tcg2_measure_dtb(void *dtb)
1308{
1309 struct uefi_platform_firmware_blob2 *blob;
1310 struct fdt_header *header;
1311 sha256_context hash_ctx;
1312 struct udevice *dev;
1313 ssize_t rsvmap_size;
1314 efi_status_t ret;
1315 u32 event_size;
1316
1317 if (!is_tcg2_protocol_installed())
1318 return EFI_SUCCESS;
1319
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001320 if (tcg2_platform_get_tpm2(&dev))
Etienne Carriereb9064352023-02-16 17:29:48 +01001321 return EFI_SECURITY_VIOLATION;
1322
1323 rsvmap_size = size_of_rsvmap(dtb);
1324 if (rsvmap_size < 0)
1325 return EFI_SECURITY_VIOLATION;
1326
1327 event_size = sizeof(*blob) + sizeof(EFI_DTB_EVENT_STRING) + SHA256_SUM_LEN;
1328 blob = calloc(1, event_size);
1329 if (!blob)
1330 return EFI_OUT_OF_RESOURCES;
1331
1332 blob->blob_description_size = sizeof(EFI_DTB_EVENT_STRING);
1333 memcpy(blob->data, EFI_DTB_EVENT_STRING, blob->blob_description_size);
1334
1335 /* Measure populated areas of the DTB */
1336 header = dtb;
1337 sha256_starts(&hash_ctx);
1338 sha256_update(&hash_ctx, (u8 *)header, sizeof(struct fdt_header));
1339 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_struct(dtb), fdt_size_dt_strings(dtb));
1340 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_strings(dtb), fdt_size_dt_struct(dtb));
1341 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_mem_rsvmap(dtb), rsvmap_size);
1342 sha256_finish(&hash_ctx, blob->data + blob->blob_description_size);
1343
Ilias Apalodimasc90c1952024-06-14 15:09:50 +03001344 ret = measure_event(dev, 1, EV_POST_CODE, event_size, (u8 *)blob);
Etienne Carriereb9064352023-02-16 17:29:48 +01001345
1346 free(blob);
1347 return ret;
1348}
1349
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001350/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001351 * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation
1352 *
1353 * Return: status code
1354 */
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001355efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_image_obj *handle)
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001356{
1357 efi_status_t ret;
1358 u32 pcr_index;
1359 struct udevice *dev;
1360 u32 event = 0;
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001361 struct smbios3_entry *entry;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001362
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001363 if (!is_tcg2_protocol_installed())
1364 return EFI_SUCCESS;
1365
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001366 if (tcg2_efi_app_invoked)
1367 return EFI_SUCCESS;
1368
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001369 if (tcg2_platform_get_tpm2(&dev))
Masahisa Kojima38155ea2021-12-07 14:15:33 +09001370 return EFI_SECURITY_VIOLATION;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001371
1372 ret = tcg2_measure_boot_variable(dev);
1373 if (ret != EFI_SUCCESS)
1374 goto out;
1375
Eddie James8ed7bb32023-10-24 10:43:49 -05001376 ret = measure_event(dev, 4, EV_EFI_ACTION,
1377 strlen(EFI_CALLING_EFI_APPLICATION),
1378 (u8 *)EFI_CALLING_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001379 if (ret != EFI_SUCCESS)
1380 goto out;
1381
Heinrich Schuchardt10899c82024-01-26 09:13:22 +01001382 entry = efi_get_configuration_table(&smbios3_guid);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001383 if (entry) {
1384 ret = tcg2_measure_smbios(dev, entry);
1385 if (ret != EFI_SUCCESS)
1386 goto out;
1387 }
1388
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001389 ret = tcg2_measure_gpt_data(dev, handle);
1390 if (ret != EFI_SUCCESS)
1391 goto out;
1392
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001393 for (pcr_index = 0; pcr_index <= 7; pcr_index++) {
Eddie James8ed7bb32023-10-24 10:43:49 -05001394 ret = measure_event(dev, pcr_index, EV_SEPARATOR,
1395 sizeof(event), (u8 *)&event);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001396 if (ret != EFI_SUCCESS)
1397 goto out;
1398 }
1399
1400 tcg2_efi_app_invoked = true;
1401out:
1402 return ret;
1403}
1404
1405/**
1406 * efi_tcg2_measure_efi_app_exit() - measure efi app exit
1407 *
1408 * Return: status code
1409 */
1410efi_status_t efi_tcg2_measure_efi_app_exit(void)
1411{
1412 efi_status_t ret;
1413 struct udevice *dev;
1414
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001415 if (!is_tcg2_protocol_installed())
1416 return EFI_SUCCESS;
1417
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001418 if (tcg2_platform_get_tpm2(&dev))
1419 return EFI_SECURITY_VIOLATION;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001420
Eddie James8ed7bb32023-10-24 10:43:49 -05001421 ret = measure_event(dev, 4, EV_EFI_ACTION,
1422 strlen(EFI_RETURNING_FROM_EFI_APPLICATION),
1423 (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001424 return ret;
1425}
1426
1427/**
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001428 * efi_tcg2_notify_exit_boot_services() - ExitBootService callback
1429 *
1430 * @event: callback event
1431 * @context: callback context
1432 */
1433static void EFIAPI
1434efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context)
1435{
1436 efi_status_t ret;
1437 struct udevice *dev;
1438
1439 EFI_ENTRY("%p, %p", event, context);
1440
Ilias Apalodimas24e841a2021-11-18 09:03:39 +02001441 event_log.ebs_called = true;
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001442
1443 if (!is_tcg2_protocol_installed()) {
1444 ret = EFI_SUCCESS;
1445 goto out;
1446 }
1447
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001448 if (tcg2_platform_get_tpm2(&dev)) {
1449 ret = EFI_SECURITY_VIOLATION;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001450 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001451 }
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001452
Eddie James8ed7bb32023-10-24 10:43:49 -05001453 ret = measure_event(dev, 5, EV_EFI_ACTION,
1454 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1455 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001456 if (ret != EFI_SUCCESS)
1457 goto out;
1458
Eddie James8ed7bb32023-10-24 10:43:49 -05001459 ret = measure_event(dev, 5, EV_EFI_ACTION,
1460 strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED),
1461 (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001462
1463out:
1464 EFI_EXIT(ret);
1465}
1466
1467/**
1468 * efi_tcg2_notify_exit_boot_services_failed()
1469 * - notify ExitBootServices() is failed
1470 *
1471 * Return: status code
1472 */
1473efi_status_t efi_tcg2_notify_exit_boot_services_failed(void)
1474{
1475 struct udevice *dev;
1476 efi_status_t ret;
1477
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001478 if (!is_tcg2_protocol_installed())
1479 return EFI_SUCCESS;
1480
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001481 if (tcg2_platform_get_tpm2(&dev))
1482 return EFI_SECURITY_VIOLATION;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001483
Eddie James8ed7bb32023-10-24 10:43:49 -05001484 ret = measure_event(dev, 5, EV_EFI_ACTION,
1485 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1486 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001487 if (ret != EFI_SUCCESS)
1488 goto out;
1489
Eddie James8ed7bb32023-10-24 10:43:49 -05001490 ret = measure_event(dev, 5, EV_EFI_ACTION,
1491 strlen(EFI_EXIT_BOOT_SERVICES_FAILED),
1492 (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001493
1494out:
1495 return ret;
1496}
1497
1498/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001499 * tcg2_measure_secure_boot_variable() - measure secure boot variables
1500 *
1501 * @dev: TPM device
1502 *
1503 * Return: status code
1504 */
1505static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev)
1506{
1507 u8 *data;
1508 efi_uintn_t data_size;
1509 u32 count, i;
1510 efi_status_t ret;
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001511 u8 deployed_mode;
1512 efi_uintn_t size;
1513 u32 deployed_audit_pcr_index = 1;
1514
1515 size = sizeof(deployed_mode);
1516 ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid,
1517 NULL, &size, &deployed_mode, NULL);
1518 if (ret != EFI_SUCCESS || !deployed_mode)
1519 deployed_audit_pcr_index = 7;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001520
1521 count = ARRAY_SIZE(secure_variables);
1522 for (i = 0; i < count; i++) {
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001523 const efi_guid_t *guid;
1524
Masahisa Kojima21684522021-10-26 17:27:26 +09001525 guid = efi_auth_var_get_guid(secure_variables[i].name);
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001526
Masahisa Kojima21684522021-10-26 17:27:26 +09001527 data = efi_get_var(secure_variables[i].name, guid, &data_size);
1528 if (!data && !secure_variables[i].accept_empty)
1529 continue;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001530
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001531 if (u16_strcmp(u"DeployedMode", secure_variables[i].name))
1532 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1533 if (u16_strcmp(u"AuditMode", secure_variables[i].name))
1534 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1535
1536 ret = tcg2_measure_variable(dev, secure_variables[i].pcr_index,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001537 EV_EFI_VARIABLE_DRIVER_CONFIG,
Masahisa Kojima21684522021-10-26 17:27:26 +09001538 secure_variables[i].name, guid,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001539 data_size, data);
1540 free(data);
1541 if (ret != EFI_SUCCESS)
1542 goto error;
1543 }
1544
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001545error:
1546 return ret;
1547}
1548
1549/**
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001550 * efi_tcg2_do_initial_measurement() - do initial measurement
1551 *
1552 * Return: status code
1553 */
1554efi_status_t efi_tcg2_do_initial_measurement(void)
1555{
1556 efi_status_t ret;
1557 struct udevice *dev;
1558
1559 if (!is_tcg2_protocol_installed())
1560 return EFI_SUCCESS;
1561
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001562 if (tcg2_platform_get_tpm2(&dev))
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001563 return EFI_SECURITY_VIOLATION;
1564
1565 ret = tcg2_measure_secure_boot_variable(dev);
1566 if (ret != EFI_SUCCESS)
1567 goto out;
1568
1569out:
1570 return ret;
1571}
1572
1573/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001574 * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
1575 *
1576 * If a TPM2 device is available, the TPM TCG2 Protocol is registered
1577 *
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001578 * Return: status code
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001579 */
1580efi_status_t efi_tcg2_register(void)
1581{
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001582 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001583 struct udevice *dev;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001584 struct efi_event *event;
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001585 u32 err;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001586
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001587 if (tcg2_platform_get_tpm2(&dev)) {
Ilias Apalodimaseb1b6b42023-01-19 16:29:15 +02001588 log_warning("Missing TPMv2 device for EFI_TCG_PROTOCOL\n");
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001589 return EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001590 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001591
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001592 /* initialize the TPM as early as possible. */
Ilias Apalodimas0c95d222023-01-25 13:06:03 +02001593 err = tpm_auto_start(dev);
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001594 if (err) {
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001595 ret = EFI_DEVICE_ERROR;
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001596 log_err("TPM startup failed\n");
1597 goto fail;
1598 }
1599
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001600 ret = efi_init_event_log();
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001601 if (ret != EFI_SUCCESS) {
1602 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001603 goto fail;
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001604 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001605
Ilias Apalodimas4953c992023-06-19 14:14:02 +03001606 ret = efi_install_multiple_protocol_interfaces(&efi_root, &efi_guid_tcg2_protocol,
1607 &efi_tcg2_protocol, NULL);
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001608 if (ret != EFI_SUCCESS) {
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001609 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001610 goto fail;
1611 }
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001612
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001613 ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
1614 efi_tcg2_notify_exit_boot_services, NULL,
1615 NULL, &event);
1616 if (ret != EFI_SUCCESS) {
1617 tcg2_uninit();
1618 goto fail;
1619 }
1620
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001621 return ret;
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001622
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001623fail:
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001624 log_err("Cannot install EFI_TCG2_PROTOCOL\n");
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001625 return ret;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001626}