blob: 1832eeb5dce70285816f5d36215a0a69bf2bc37f [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>
Simon Glass37972f42025-05-24 11:28:21 -060013#include <efi_device_path.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020014#include <efi_loader.h>
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +020015#include <efi_variable.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020016#include <efi_tcg2.h>
17#include <log.h>
Masahisa Kojima70be5a62021-05-26 12:09:58 +090018#include <malloc.h>
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +090019#include <smbios.h>
Pali Rohárba87ddf2021-08-02 15:18:31 +020020#include <version_string.h>
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +020021#include <tpm_api.h>
Masahisa Kojima70be5a62021-05-26 12:09:58 +090022#include <u-boot/hash-checksum.h>
Masahisa Kojimad420d8d2021-11-03 11:04:09 +090023#include <linux/unaligned/be_byteshift.h>
24#include <linux/unaligned/le_byteshift.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020025#include <linux/unaligned/generic.h>
Ilias Apalodimas967650d2020-11-30 11:47:40 +020026#include <hexdump.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020027
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020028/**
29 * struct event_log_buffer - internal eventlog management structure
30 *
31 * @buffer: eventlog buffer
32 * @final_buffer: finalevent config table buffer
33 * @pos: current position of 'buffer'
34 * @final_pos: current position of 'final_buffer'
35 * @get_event_called: true if GetEventLog has been invoked at least once
36 * @ebs_called: true if ExitBootServices has been invoked
37 * @truncated: true if the 'buffer' is truncated
38 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +020039struct event_log_buffer {
40 void *buffer;
41 void *final_buffer;
42 size_t pos; /* eventlog position */
43 size_t final_pos; /* final events config table position */
44 size_t last_event_size;
45 bool get_event_called;
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020046 bool ebs_called;
Ilias Apalodimas967650d2020-11-30 11:47:40 +020047 bool truncated;
48};
Ilias Apalodimas590fef62020-11-11 11:18:11 +020049
Ilias Apalodimas967650d2020-11-30 11:47:40 +020050static struct event_log_buffer event_log;
Masahisa Kojima8173cd42021-08-13 16:12:40 +090051static bool tcg2_efi_app_invoked;
Ilias Apalodimas590fef62020-11-11 11:18:11 +020052/*
53 * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset.
54 * Since the current tpm2_get_capability() response buffers starts at
55 * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate
56 * the response size and offset once for all consumers
57 */
58#define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \
59 offsetof(struct tpms_capability_data, data))
60#define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \
61 offsetof(struct tpms_tagged_property, value))
62
Ilias Apalodimas967650d2020-11-30 11:47:40 +020063static const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID;
64static const efi_guid_t efi_guid_final_events = EFI_TCG2_FINAL_EVENTS_TABLE_GUID;
65
Masahisa Kojima21684522021-10-26 17:27:26 +090066struct variable_info {
67 const u16 *name;
68 bool accept_empty;
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +090069 u32 pcr_index;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +090070};
71
Masahisa Kojima21684522021-10-26 17:27:26 +090072static struct variable_info secure_variables[] = {
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +090073 {u"SecureBoot", true, 7},
74 {u"PK", true, 7},
75 {u"KEK", true, 7},
76 {u"db", true, 7},
77 {u"dbx", true, 7},
78 {u"dbt", false, 7},
79 {u"dbr", false, 7},
80 {u"DeployedMode", false, 1},
81 {u"AuditMode", false, 1},
Masahisa Kojima21684522021-10-26 17:27:26 +090082};
83
Masahisa Kojima0fd43792021-12-07 14:15:31 +090084static bool is_tcg2_protocol_installed(void)
85{
86 struct efi_handler *handler;
87 efi_status_t ret;
88
89 ret = efi_search_protocol(efi_root, &efi_guid_tcg2_protocol, &handler);
90 return ret == EFI_SUCCESS;
91}
92
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020093/* tcg2_agile_log_append - Append an agile event to an eventlog
94 *
95 * @pcr_index: PCR index
96 * @event_type: type of event added
97 * @digest_list: list of digest algorithms to add
98 * @size: size of event
99 * @event: event to add
100 * @log: log buffer to append the event
101 *
102 * @Return: status code
103 */
104static efi_status_t tcg2_agile_log_append(u32 pcr_index, u32 event_type,
105 struct tpml_digest_values *digest_list,
106 u32 size, u8 event[])
107{
108 void *log = (void *)((uintptr_t)event_log.buffer + event_log.pos);
Eddie James8ed7bb32023-10-24 10:43:49 -0500109 u32 event_size = size + tcg2_event_get_size(digest_list);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200110 struct efi_tcg2_final_events_table *final_event;
111 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200112
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200113 /* if ExitBootServices hasn't been called update the normal log */
114 if (!event_log.ebs_called) {
115 if (event_log.truncated ||
Raymond Mao63242182025-01-27 06:49:35 -0800116 event_log.pos + event_size > CONFIG_TPM2_EVENT_LOG_SIZE) {
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200117 event_log.truncated = true;
118 return EFI_VOLUME_FULL;
119 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500120 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200121 event_log.pos += event_size;
122 event_log.last_event_size = event_size;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200123 }
124
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200125 if (!event_log.get_event_called)
126 return ret;
127
128 /* if GetEventLog has been called update FinalEventLog as well */
Raymond Mao63242182025-01-27 06:49:35 -0800129 if (event_log.final_pos + event_size > CONFIG_TPM2_EVENT_LOG_SIZE)
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200130 return EFI_VOLUME_FULL;
131
132 log = (void *)((uintptr_t)event_log.final_buffer + event_log.final_pos);
Eddie James8ed7bb32023-10-24 10:43:49 -0500133 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200134
135 final_event = event_log.final_buffer;
136 final_event->number_of_events++;
137 event_log.final_pos += event_size;
138
139 return ret;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200140}
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200141
142/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200143 * tpm2_get_max_command_size() - get the supported max command size
144 *
145 * @dev: TPM device
146 * @max_command_size: output buffer for the size
147 *
148 * Return: 0 on success, -1 on error
149 */
150static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size)
151{
152 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
153 u32 ret;
154
155 memset(response, 0, sizeof(response));
156 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
157 TPM2_PT_MAX_COMMAND_SIZE, response, 1);
158 if (ret)
159 return -1;
160
161 *max_command_size = (uint16_t)get_unaligned_be32(response +
162 properties_offset);
163
164 return 0;
165}
166
167/**
168 * tpm2_get_max_response_size() - get the supported max response size
169 *
170 * @dev: TPM device
171 * @max_response_size: output buffer for the size
172 *
173 * Return: 0 on success, -1 on error
174 */
175static int tpm2_get_max_response_size(struct udevice *dev,
176 u16 *max_response_size)
177{
178 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
179 u32 ret;
180
181 memset(response, 0, sizeof(response));
182 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
183 TPM2_PT_MAX_RESPONSE_SIZE, response, 1);
184 if (ret)
185 return -1;
186
187 *max_response_size = (uint16_t)get_unaligned_be32(response +
188 properties_offset);
189
190 return 0;
191}
192
193/**
194 * tpm2_get_manufacturer_id() - get the manufacturer ID
195 *
196 * @dev: TPM device
197 * @manufacturer_id: output buffer for the id
198 *
199 * Return: 0 on success, -1 on error
200 */
201static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id)
202{
203 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
204 u32 ret;
205
206 memset(response, 0, sizeof(response));
207 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
208 TPM2_PT_MANUFACTURER, response, 1);
209 if (ret)
210 return -1;
211
212 *manufacturer_id = get_unaligned_be32(response + properties_offset);
213
214 return 0;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200215}
216
217/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200218 * efi_tcg2_get_capability() - protocol capability information and state information
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200219 *
220 * @this: TCG2 protocol instance
221 * @capability: caller allocated memory with size field to the size of
222 * the structure allocated
223
224 * Return: status code
225 */
226static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200227efi_tcg2_get_capability(struct efi_tcg2_protocol *this,
228 struct efi_tcg2_boot_service_capability *capability)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200229{
230 struct udevice *dev;
231 efi_status_t efi_ret;
232 int ret;
233
234 EFI_ENTRY("%p, %p", this, capability);
235
236 if (!this || !capability) {
237 efi_ret = EFI_INVALID_PARAMETER;
238 goto out;
239 }
240
Masahisa Kojima9cc82932021-09-06 12:04:12 +0900241 if (capability->size < BOOT_SERVICE_CAPABILITY_MIN) {
242 capability->size = BOOT_SERVICE_CAPABILITY_MIN;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200243 efi_ret = EFI_BUFFER_TOO_SMALL;
244 goto out;
245 }
246
247 if (capability->size < sizeof(*capability)) {
248 capability->size = sizeof(*capability);
249 efi_ret = EFI_BUFFER_TOO_SMALL;
250 goto out;
251 }
252
253 capability->structure_version.major = 1;
254 capability->structure_version.minor = 1;
255 capability->protocol_version.major = 1;
256 capability->protocol_version.minor = 1;
257
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300258 ret = tcg2_platform_get_tpm2(&dev);
259 if (ret) {
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200260 capability->supported_event_logs = 0;
261 capability->hash_algorithm_bitmap = 0;
262 capability->tpm_present_flag = false;
263 capability->max_command_size = 0;
264 capability->max_response_size = 0;
265 capability->manufacturer_id = 0;
266 capability->number_of_pcr_banks = 0;
267 capability->active_pcr_banks = 0;
268
269 efi_ret = EFI_SUCCESS;
270 goto out;
271 }
272
273 /* We only allow a TPMv2 device to register the EFI protocol */
274 capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2;
275
276 capability->tpm_present_flag = true;
277
278 /* Supported and active PCRs */
279 capability->hash_algorithm_bitmap = 0;
280 capability->active_pcr_banks = 0;
Ilias Apalodimascb356612024-06-23 14:48:17 +0300281 ret = tcg2_get_pcr_info(dev, &capability->hash_algorithm_bitmap,
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200282 &capability->active_pcr_banks,
283 &capability->number_of_pcr_banks);
284 if (ret) {
285 efi_ret = EFI_DEVICE_ERROR;
286 goto out;
287 }
288
289 /* Max command size */
290 ret = tpm2_get_max_command_size(dev, &capability->max_command_size);
291 if (ret) {
292 efi_ret = EFI_DEVICE_ERROR;
293 goto out;
294 }
295
296 /* Max response size */
297 ret = tpm2_get_max_response_size(dev, &capability->max_response_size);
298 if (ret) {
299 efi_ret = EFI_DEVICE_ERROR;
300 goto out;
301 }
302
303 /* Manufacturer ID */
304 ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id);
305 if (ret) {
306 efi_ret = EFI_DEVICE_ERROR;
307 goto out;
308 }
309
310 return EFI_EXIT(EFI_SUCCESS);
311out:
312 return EFI_EXIT(efi_ret);
313}
314
315/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200316 * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its
317 * last entry
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200318 *
319 * @this: TCG2 protocol instance
320 * @log_format: type of event log format
321 * @event_log_location: pointer to the memory address of the event log
322 * @event_log_last_entry: pointer to the address of the start of the last
323 * entry in the event log in memory, if log contains
324 * more than 1 entry
325 * @event_log_truncated: set to true, if the Event Log is missing at i
326 * least one entry
327 *
328 * Return: status code
329 */
330static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200331efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this,
332 efi_tcg_event_log_format log_format,
333 u64 *event_log_location, u64 *event_log_last_entry,
334 bool *event_log_truncated)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200335{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200336 efi_status_t ret = EFI_SUCCESS;
337 struct udevice *dev;
338
339 EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location,
340 event_log_last_entry, event_log_truncated);
341
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900342 if (!this || !event_log_location || !event_log_last_entry ||
343 !event_log_truncated) {
344 ret = EFI_INVALID_PARAMETER;
345 goto out;
346 }
347
348 /* Only support TPMV2 */
349 if (log_format != TCG2_EVENT_LOG_FORMAT_TCG_2) {
350 ret = EFI_INVALID_PARAMETER;
351 goto out;
352 }
353
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300354 if (tcg2_platform_get_tpm2(&dev)) {
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200355 event_log_location = NULL;
356 event_log_last_entry = NULL;
357 *event_log_truncated = false;
358 ret = EFI_SUCCESS;
359 goto out;
360 }
361 *event_log_location = (uintptr_t)event_log.buffer;
362 *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos -
363 event_log.last_event_size);
364 *event_log_truncated = event_log.truncated;
365 event_log.get_event_called = true;
366
367out:
368 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200369}
370
371/**
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900372 * tcg2_hash_pe_image() - calculate PE/COFF image hash
373 *
374 * @efi: pointer to the EFI binary
375 * @efi_size: size of @efi binary
376 * @digest_list: list of digest algorithms to extend
377 *
378 * Return: status code
379 */
380static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size,
381 struct tpml_digest_values *digest_list)
382{
383 WIN_CERTIFICATE *wincerts = NULL;
384 size_t wincerts_len;
385 struct efi_image_regions *regs = NULL;
386 void *new_efi = NULL;
387 u8 hash[TPM2_SHA512_DIGEST_SIZE];
Eddie James8ed7bb32023-10-24 10:43:49 -0500388 struct udevice *dev;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300389 efi_status_t ret = EFI_SUCCESS;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900390 u32 active;
391 int i;
392
393 new_efi = efi_prepare_aligned_image(efi, &efi_size);
394 if (!new_efi)
395 return EFI_OUT_OF_RESOURCES;
396
397 if (!efi_image_parse(new_efi, efi_size, &regs, &wincerts,
398 &wincerts_len)) {
399 log_err("Parsing PE executable image failed\n");
400 ret = EFI_UNSUPPORTED;
401 goto out;
402 }
403
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300404 if (tcg2_platform_get_tpm2(&dev)) {
405 ret = EFI_DEVICE_ERROR;
Eddie James8ed7bb32023-10-24 10:43:49 -0500406 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300407 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500408
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300409 if (tcg2_get_active_pcr_banks(dev, &active)) {
410 ret = EFI_DEVICE_ERROR;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900411 goto out;
412 }
413
414 digest_list->count = 0;
Tim Harvey6ea1e052024-05-25 13:00:48 -0700415 for (i = 0; i < ARRAY_SIZE(hash_algo_list); i++) {
416 u16 hash_alg = hash_algo_list[i].hash_alg;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900417
Tim Harvey6ea1e052024-05-25 13:00:48 -0700418 if (!(active & hash_algo_list[i].hash_mask))
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900419 continue;
420 switch (hash_alg) {
421 case TPM2_ALG_SHA1:
422 hash_calculate("sha1", regs->reg, regs->num, hash);
423 break;
424 case TPM2_ALG_SHA256:
425 hash_calculate("sha256", regs->reg, regs->num, hash);
426 break;
427 case TPM2_ALG_SHA384:
428 hash_calculate("sha384", regs->reg, regs->num, hash);
429 break;
430 case TPM2_ALG_SHA512:
431 hash_calculate("sha512", regs->reg, regs->num, hash);
432 break;
433 default:
Heinrich Schuchardt09ec9f72023-07-31 14:11:34 +0200434 continue;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900435 }
Ruchika Guptae53007b2021-09-14 12:14:31 +0530436 digest_list->digests[digest_list->count].hash_alg = hash_alg;
437 memcpy(&digest_list->digests[digest_list->count].digest, hash,
Eddie James8ed7bb32023-10-24 10:43:49 -0500438 (u32)tpm2_algorithm_to_len(hash_alg));
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900439 digest_list->count++;
440 }
441
442out:
443 if (new_efi != efi)
444 free(new_efi);
445 free(regs);
446
447 return ret;
448}
449
450/**
451 * tcg2_measure_pe_image() - measure PE/COFF image
452 *
453 * @efi: pointer to the EFI binary
454 * @efi_size: size of @efi binary
455 * @handle: loaded image handle
456 * @loaded_image: loaded image protocol
457 *
458 * Return: status code
459 */
460efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size,
461 struct efi_loaded_image_obj *handle,
462 struct efi_loaded_image *loaded_image)
463{
464 struct tpml_digest_values digest_list;
465 efi_status_t ret;
466 struct udevice *dev;
467 u32 pcr_index, event_type, event_size;
468 struct uefi_image_load_event *image_load_event;
469 struct efi_device_path *device_path;
470 u32 device_path_length;
471 IMAGE_DOS_HEADER *dos;
472 IMAGE_NT_HEADERS32 *nt;
473 struct efi_handler *handler;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300474 int rc;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900475
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +0900476 if (!is_tcg2_protocol_installed())
477 return EFI_SUCCESS;
478
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300479 if (tcg2_platform_get_tpm2(&dev))
Masahisa Kojima38155ea2021-12-07 14:15:33 +0900480 return EFI_SECURITY_VIOLATION;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900481
482 switch (handle->image_type) {
483 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
484 pcr_index = 4;
485 event_type = EV_EFI_BOOT_SERVICES_APPLICATION;
486 break;
487 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
488 pcr_index = 2;
489 event_type = EV_EFI_BOOT_SERVICES_DRIVER;
490 break;
491 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
492 pcr_index = 2;
493 event_type = EV_EFI_RUNTIME_SERVICES_DRIVER;
494 break;
495 default:
496 return EFI_UNSUPPORTED;
497 }
498
499 ret = tcg2_hash_pe_image(efi, efi_size, &digest_list);
500 if (ret != EFI_SUCCESS)
501 return ret;
502
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300503 rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
504 if (rc)
505 return EFI_DEVICE_ERROR;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900506
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300507 ret = efi_search_protocol(&handle->header,
508 &efi_guid_loaded_image_device_path, &handler);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900509 if (ret != EFI_SUCCESS)
510 return ret;
511
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300512 device_path = handler->protocol_interface;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900513 device_path_length = efi_dp_size(device_path);
514 if (device_path_length > 0) {
515 /* add end node size */
516 device_path_length += sizeof(struct efi_device_path);
517 }
518 event_size = sizeof(struct uefi_image_load_event) + device_path_length;
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300519 image_load_event = calloc(1, event_size);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900520 if (!image_load_event)
521 return EFI_OUT_OF_RESOURCES;
522
523 image_load_event->image_location_in_memory = (uintptr_t)efi;
524 image_load_event->image_length_in_memory = efi_size;
525 image_load_event->length_of_device_path = device_path_length;
526
527 dos = (IMAGE_DOS_HEADER *)efi;
528 nt = (IMAGE_NT_HEADERS32 *)(efi + dos->e_lfanew);
529 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
530 IMAGE_NT_HEADERS64 *nt64 = (IMAGE_NT_HEADERS64 *)nt;
531
532 image_load_event->image_link_time_address =
533 nt64->OptionalHeader.ImageBase;
534 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
535 image_load_event->image_link_time_address =
536 nt->OptionalHeader.ImageBase;
537 } else {
538 ret = EFI_INVALID_PARAMETER;
539 goto out;
540 }
541
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300542 /* device_path_length might be zero */
543 memcpy(image_load_event->device_path, device_path, device_path_length);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900544
545 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
546 event_size, (u8 *)image_load_event);
547
548out:
549 free(image_load_event);
550
551 return ret;
552}
553
554/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200555 * efi_tcg2_hash_log_extend_event() - extend and optionally log events
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200556 *
557 * @this: TCG2 protocol instance
558 * @flags: bitmap providing additional information on the
559 * operation
560 * @data_to_hash: physical address of the start of the data buffer
561 * to be hashed
562 * @data_to_hash_len: the length in bytes of the buffer referenced by
563 * data_to_hash
564 * @efi_tcg_event: pointer to data buffer containing information
565 * about the event
566 *
567 * Return: status code
568 */
569static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200570efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags,
571 u64 data_to_hash, u64 data_to_hash_len,
572 struct efi_tcg2_event *efi_tcg_event)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200573{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200574 struct udevice *dev;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300575 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200576 u32 event_type, pcr_index, event_size;
577 struct tpml_digest_values digest_list;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300578 int rc = 0;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200579
580 EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash,
581 data_to_hash_len, efi_tcg_event);
582
583 if (!this || !data_to_hash || !efi_tcg_event) {
584 ret = EFI_INVALID_PARAMETER;
585 goto out;
586 }
587
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300588 if (tcg2_platform_get_tpm2(&dev)) {
589 ret = EFI_DEVICE_ERROR;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200590 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300591 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200592
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 ret = efi_check_pe((void *)(uintptr_t)data_to_hash,
Heinrich Schuchardtebe75212024-11-05 05:20:45 +0100613 data_to_hash_len, NULL);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900614 if (ret != EFI_SUCCESS) {
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900615 ret = EFI_UNSUPPORTED;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900616 goto out;
617 }
618 ret = tcg2_hash_pe_image((void *)(uintptr_t)data_to_hash,
619 data_to_hash_len, &digest_list);
620 } else {
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300621 rc = tcg2_create_digest(dev, (u8 *)(uintptr_t)data_to_hash,
622 data_to_hash_len, &digest_list);
623 if (rc)
624 ret = EFI_DEVICE_ERROR;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900625 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200626
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200627 if (ret != EFI_SUCCESS)
628 goto out;
629
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900630 pcr_index = efi_tcg_event->header.pcr_index;
631 event_type = efi_tcg_event->header.event_type;
632
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300633 rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
634 if (rc) {
635 ret = EFI_DEVICE_ERROR;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200636 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300637 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200638
639 if (flags & EFI_TCG2_EXTEND_ONLY) {
640 if (event_log.truncated)
641 ret = EFI_VOLUME_FULL;
642 goto out;
643 }
644
645 /*
646 * The efi_tcg_event size includes the size component and the
647 * headersize
648 */
649 event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) -
650 efi_tcg_event->header.header_size;
651 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
652 event_size, efi_tcg_event->event);
653out:
654 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200655}
656
657/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200658 * efi_tcg2_submit_command() - Send command to the TPM
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200659 *
660 * @this: TCG2 protocol instance
661 * @input_param_block_size: size of the TPM input parameter block
662 * @input_param_block: pointer to the TPM input parameter block
663 * @output_param_block_size: size of the TPM output parameter block
664 * @output_param_block: pointer to the TPM output parameter block
665 *
666 * Return: status code
667 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200668static efi_status_t EFIAPI
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900669efi_tcg2_submit_command(struct efi_tcg2_protocol *this,
670 u32 input_param_block_size,
671 u8 *input_param_block,
672 u32 output_param_block_size,
673 u8 *output_param_block)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200674{
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900675 struct udevice *dev;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300676 efi_status_t ret = EFI_SUCCESS;
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900677 u32 rc;
678 size_t resp_buf_size = output_param_block_size;
679
680 EFI_ENTRY("%p, %u, %p, %u, %p", this, input_param_block_size,
681 input_param_block, output_param_block_size, output_param_block);
682
683 if (!this || !input_param_block || !input_param_block_size) {
684 ret = EFI_INVALID_PARAMETER;
685 goto out;
686 }
687
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300688 if (tcg2_platform_get_tpm2(&dev)) {
689 ret = EFI_DEVICE_ERROR;
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900690 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300691 }
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900692
693 rc = tpm2_submit_command(dev, input_param_block,
694 output_param_block, &resp_buf_size);
695 if (rc) {
696 ret = (rc == -ENOSPC) ? EFI_OUT_OF_RESOURCES : EFI_DEVICE_ERROR;
697
698 goto out;
699 }
700
701out:
702 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200703}
704
705/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200706 * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200707 *
708 * @this: TCG2 protocol instance
709 * @active_pcr_banks: pointer for receiving the bitmap of currently
710 * active PCR banks
711 *
712 * Return: status code
713 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200714static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200715efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
716 u32 *active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200717{
Eddie James8ed7bb32023-10-24 10:43:49 -0500718 struct udevice *dev;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300719 efi_status_t ret = EFI_INVALID_PARAMETER;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200720
Ilias Apalodimas918a6ea2023-10-24 10:43:53 -0500721 EFI_ENTRY("%p, %p", this, active_pcr_banks);
722
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300723 if (!this || !active_pcr_banks)
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900724 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300725
726 if (tcg2_platform_get_tpm2(&dev))
727 goto out;
728
729 /*
730 * EFI_INVALID_PARAMETER does not convey the problem type.
731 * but that's what currently the spec specifies.
732 * EFI_DEVICE_ERROR would be better
733 */
734 if (tcg2_get_active_pcr_banks(dev, active_pcr_banks))
Eddie James8ed7bb32023-10-24 10:43:49 -0500735 goto out;
736
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300737 ret = EFI_SUCCESS;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200738
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900739out:
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200740 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200741}
742
743/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200744 * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200745 *
746 * @this: TCG2 protocol instance
747 * @active_pcr_banks: bitmap of the requested active PCR banks
748 *
749 * Return: status code
750 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200751static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300752efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
753 u32 __maybe_unused active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200754{
755 return EFI_UNSUPPORTED;
756}
757
758/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200759 * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous
760 * set_active_pcr_banks()
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200761 *
762 * @this: TCG2 protocol instance
763 * @operation_present: non-zero value to indicate a
764 * set_active_pcr_banks operation was
765 * invoked during last boot
766 * @response: result value could be returned
767 *
768 * Return: status code
769 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200770static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300771efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
772 u32 __maybe_unused *operation_present,
773 u32 __maybe_unused *response)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200774{
775 return EFI_UNSUPPORTED;
776}
777
778static const struct efi_tcg2_protocol efi_tcg2_protocol = {
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200779 .get_capability = efi_tcg2_get_capability,
780 .get_eventlog = efi_tcg2_get_eventlog,
781 .hash_log_extend_event = efi_tcg2_hash_log_extend_event,
782 .submit_command = efi_tcg2_submit_command,
783 .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks,
784 .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks,
785 .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks,
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200786};
787
788/**
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200789 * tcg2_uninit - remove the final event table and free efi memory on failures
790 */
Ilias Apalodimas14e7ed92024-10-30 22:40:59 +0200791static void tcg2_uninit(void)
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200792{
793 efi_status_t ret;
794
795 ret = efi_install_configuration_table(&efi_guid_final_events, NULL);
Ilias Apalodimasf583b7e2024-11-28 09:11:20 +0200796 if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200797 log_err("Failed to delete final events config table\n");
798
799 efi_free_pool(event_log.buffer);
800 event_log.buffer = NULL;
801 efi_free_pool(event_log.final_buffer);
802 event_log.final_buffer = NULL;
Masahisa Kojima0fd43792021-12-07 14:15:31 +0900803
804 if (!is_tcg2_protocol_installed())
805 return;
806
Ilias Apalodimas4953c992023-06-19 14:14:02 +0300807 ret = efi_uninstall_multiple_protocol_interfaces(efi_root, &efi_guid_tcg2_protocol,
808 &efi_tcg2_protocol, NULL);
Masahisa Kojima0fd43792021-12-07 14:15:31 +0900809 if (ret != EFI_SUCCESS)
810 log_err("Failed to remove EFI TCG2 protocol\n");
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200811}
812
813/**
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200814 * create_final_event() - Create the final event and install the config
815 * defined by the TCG EFI spec
816 */
817static efi_status_t create_final_event(void)
818{
819 struct efi_tcg2_final_events_table *final_event;
820 efi_status_t ret;
821
822 /*
823 * All events generated after the invocation of
824 * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an
825 * EFI_CONFIGURATION_TABLE
826 */
Raymond Mao63242182025-01-27 06:49:35 -0800827 ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, CONFIG_TPM2_EVENT_LOG_SIZE,
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200828 &event_log.final_buffer);
829 if (ret != EFI_SUCCESS)
830 goto out;
831
Raymond Mao63242182025-01-27 06:49:35 -0800832 memset(event_log.final_buffer, 0xff, CONFIG_TPM2_EVENT_LOG_SIZE);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200833 final_event = event_log.final_buffer;
834 final_event->number_of_events = 0;
835 final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
836 event_log.final_pos = sizeof(*final_event);
837 ret = efi_install_configuration_table(&efi_guid_final_events,
838 final_event);
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +0300839 if (ret != EFI_SUCCESS) {
840 efi_free_pool(event_log.final_buffer);
841 event_log.final_buffer = NULL;
842 }
843
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200844out:
845 return ret;
846}
847
848/**
Eddie James8ed7bb32023-10-24 10:43:49 -0500849 * measure_event() - common function to add event log and extend PCR
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900850 *
851 * @dev: TPM device
852 * @pcr_index: PCR index
853 * @event_type: type of event added
854 * @size: event size
855 * @event: event data
856 *
857 * Return: status code
858 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500859static efi_status_t measure_event(struct udevice *dev, u32 pcr_index,
860 u32 event_type, u32 size, u8 event[])
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900861{
862 struct tpml_digest_values digest_list;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300863 efi_status_t ret = EFI_DEVICE_ERROR;
864 int rc;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900865
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300866 rc = tcg2_create_digest(dev, event, size, &digest_list);
867 if (rc)
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900868 goto out;
869
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300870 rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
871 if (rc)
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900872 goto out;
873
874 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
875 size, event);
876
877out:
878 return ret;
879}
880
881/**
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200882 * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the
883 * eventlog and extend the PCRs
884 *
885 * @dev: TPM device
886 *
887 * @Return: status code
888 */
889static efi_status_t efi_append_scrtm_version(struct udevice *dev)
890{
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200891 efi_status_t ret;
892
Eddie James8ed7bb32023-10-24 10:43:49 -0500893 ret = measure_event(dev, 0, EV_S_CRTM_VERSION,
894 strlen(version_string) + 1, (u8 *)version_string);
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200895
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530896 return ret;
897}
898
899/**
900 * efi_init_event_log() - initialize an eventlog
901 *
902 * Return: status code
903 */
904static efi_status_t efi_init_event_log(void)
905{
906 /*
907 * vendor_info_size is currently set to 0, we need to change the length
908 * and allocate the flexible array member if this changes
909 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500910 struct tcg2_event_log elog;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530911 struct udevice *dev;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530912 efi_status_t ret;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300913 int rc;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530914
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300915 if (tcg2_platform_get_tpm2(&dev))
916 return EFI_DEVICE_ERROR;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530917
Raymond Mao63242182025-01-27 06:49:35 -0800918 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
919 CONFIG_TPM2_EVENT_LOG_SIZE,
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530920 (void **)&event_log.buffer);
921 if (ret != EFI_SUCCESS)
922 return ret;
923
924 /*
925 * initialize log area as 0xff so the OS can easily figure out the
926 * last log entry
927 */
Raymond Mao63242182025-01-27 06:49:35 -0800928 memset(event_log.buffer, 0xff, CONFIG_TPM2_EVENT_LOG_SIZE);
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530929
930 /*
931 * The log header is defined to be in SHA1 event log entry format.
932 * Setup event header
933 */
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530934 event_log.pos = 0;
935 event_log.last_event_size = 0;
936 event_log.get_event_called = false;
937 event_log.ebs_called = false;
938 event_log.truncated = false;
939
940 /*
941 * Check if earlier firmware have passed any eventlog. Different
942 * platforms can use different ways to do so.
943 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500944 elog.log = event_log.buffer;
Raymond Mao63242182025-01-27 06:49:35 -0800945 elog.log_size = CONFIG_TPM2_EVENT_LOG_SIZE;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300946 rc = tcg2_log_prepare_buffer(dev, &elog, false);
947 if (rc) {
948 ret = (rc == -ENOBUFS) ? EFI_BUFFER_TOO_SMALL : EFI_DEVICE_ERROR;
Eddie James8ed7bb32023-10-24 10:43:49 -0500949 goto free_pool;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300950 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500951
952 event_log.pos = elog.log_position;
953
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530954 /*
Eddie James8ed7bb32023-10-24 10:43:49 -0500955 * Add SCRTM version to the log if previous firmmware
956 * doesn't pass an eventlog.
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530957 */
Ilias Apalodimas12c15f52023-11-07 13:31:34 +0200958 if (!elog.found) {
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530959 ret = efi_append_scrtm_version(dev);
Ilias Apalodimas12c15f52023-11-07 13:31:34 +0200960 if (ret != EFI_SUCCESS)
961 goto free_pool;
962 }
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530963
964 ret = create_final_event();
965 if (ret != EFI_SUCCESS)
966 goto free_pool;
967
968 return ret;
969
970free_pool:
971 efi_free_pool(event_log.buffer);
972 event_log.buffer = NULL;
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200973 return ret;
974}
975
976/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900977 * tcg2_measure_variable() - add variable event log and extend PCR
978 *
979 * @dev: TPM device
980 * @pcr_index: PCR index
981 * @event_type: type of event added
982 * @var_name: variable name
983 * @guid: guid
984 * @data_size: variable data size
985 * @data: variable data
986 *
987 * Return: status code
988 */
989static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index,
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +0200990 u32 event_type, const u16 *var_name,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900991 const efi_guid_t *guid,
992 efi_uintn_t data_size, u8 *data)
993{
994 u32 event_size;
995 efi_status_t ret;
996 struct efi_tcg2_uefi_variable_data *event;
997
998 event_size = sizeof(event->variable_name) +
999 sizeof(event->unicode_name_length) +
1000 sizeof(event->variable_data_length) +
1001 (u16_strlen(var_name) * sizeof(u16)) + data_size;
1002 event = malloc(event_size);
1003 if (!event)
1004 return EFI_OUT_OF_RESOURCES;
1005
1006 guidcpy(&event->variable_name, guid);
1007 event->unicode_name_length = u16_strlen(var_name);
1008 event->variable_data_length = data_size;
1009 memcpy(event->unicode_name, var_name,
1010 (event->unicode_name_length * sizeof(u16)));
1011 if (data) {
1012 memcpy((u16 *)event->unicode_name + event->unicode_name_length,
1013 data, data_size);
1014 }
Eddie James8ed7bb32023-10-24 10:43:49 -05001015 ret = measure_event(dev, pcr_index, event_type, event_size,
1016 (u8 *)event);
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001017 free(event);
1018 return ret;
1019}
1020
1021/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001022 * tcg2_measure_boot_variable() - measure boot variables
1023 *
1024 * @dev: TPM device
1025 *
1026 * Return: status code
1027 */
1028static efi_status_t tcg2_measure_boot_variable(struct udevice *dev)
1029{
1030 u16 *boot_order;
1031 u16 *boot_index;
Simon Glass90975372022-01-23 12:55:12 -07001032 u16 var_name[] = u"BootOrder";
1033 u16 boot_name[] = u"Boot####";
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001034 u8 *bootvar;
1035 efi_uintn_t var_data_size;
1036 u32 count, i;
1037 efi_status_t ret;
1038
1039 boot_order = efi_get_var(var_name, &efi_global_variable_guid,
1040 &var_data_size);
1041 if (!boot_order) {
Masahisa Kojimad1325932021-11-09 18:44:54 +09001042 /* If "BootOrder" is not defined, skip the boot variable measurement */
1043 return EFI_SUCCESS;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001044 }
1045
1046 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name,
1047 &efi_global_variable_guid, var_data_size,
1048 (u8 *)boot_order);
1049 if (ret != EFI_SUCCESS)
1050 goto error;
1051
1052 count = var_data_size / sizeof(*boot_order);
1053 boot_index = boot_order;
1054 for (i = 0; i < count; i++) {
1055 efi_create_indexed_name(boot_name, sizeof(boot_name),
1056 "Boot", *boot_index++);
1057
1058 bootvar = efi_get_var(boot_name, &efi_global_variable_guid,
1059 &var_data_size);
1060
1061 if (!bootvar) {
Masahisa Kojimaaca20c82021-11-09 20:35:53 +09001062 log_debug("%ls not found\n", boot_name);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001063 continue;
1064 }
1065
1066 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2,
1067 boot_name,
1068 &efi_global_variable_guid,
1069 var_data_size, bootvar);
1070 free(bootvar);
1071 if (ret != EFI_SUCCESS)
1072 goto error;
1073 }
1074
1075error:
1076 free(boot_order);
1077 return ret;
1078}
1079
1080/**
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001081 * tcg2_measure_smbios() - measure smbios table
1082 *
1083 * @dev: TPM device
1084 * @entry: pointer to the smbios_entry structure
1085 *
1086 * Return: status code
1087 */
1088static efi_status_t
1089tcg2_measure_smbios(struct udevice *dev,
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001090 const struct smbios3_entry *entry)
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001091{
1092 efi_status_t ret;
1093 struct smbios_header *smbios_copy;
1094 struct smbios_handoff_table_pointers2 *event = NULL;
1095 u32 event_size;
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001096 const char smbios3_anchor[] = "_SM3_";
1097
1098 /* We only support SMBIOS 3.0 Entry Point structure */
1099 if (memcmp(entry->anchor, smbios3_anchor, sizeof(smbios3_anchor) - 1))
1100 return EFI_UNSUPPORTED;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001101
1102 /*
1103 * TCG PC Client PFP Spec says
1104 * "SMBIOS structures that contain static configuration information
1105 * (e.g. Platform Manufacturer Enterprise Number assigned by IANA,
1106 * platform model number, Vendor and Device IDs for each SMBIOS table)
1107 * that is relevant to the security of the platform MUST be measured".
1108 * Device dependent parameters such as serial number are cleared to
1109 * zero or spaces for the measurement.
1110 */
1111 event_size = sizeof(struct smbios_handoff_table_pointers2) +
1112 FIELD_SIZEOF(struct efi_configuration_table, guid) +
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +01001113 entry->table_maximum_size;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001114 event = calloc(1, event_size);
1115 if (!event) {
1116 ret = EFI_OUT_OF_RESOURCES;
1117 goto out;
1118 }
1119
1120 event->table_description_size = sizeof(SMBIOS_HANDOFF_TABLE_DESC);
1121 memcpy(event->table_description, SMBIOS_HANDOFF_TABLE_DESC,
1122 sizeof(SMBIOS_HANDOFF_TABLE_DESC));
1123 put_unaligned_le64(1, &event->number_of_tables);
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001124 guidcpy(&event->table_entry[0].guid, &smbios3_guid);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001125 smbios_copy = (struct smbios_header *)((uintptr_t)&event->table_entry[0].table);
1126 memcpy(&event->table_entry[0].table,
1127 (void *)((uintptr_t)entry->struct_table_address),
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +01001128 entry->table_maximum_size);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001129
1130 smbios_prepare_measurement(entry, smbios_copy);
1131
Eddie James8ed7bb32023-10-24 10:43:49 -05001132 ret = measure_event(dev, 1, EV_EFI_HANDOFF_TABLES2, event_size,
1133 (u8 *)event);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001134 if (ret != EFI_SUCCESS)
1135 goto out;
1136
1137out:
1138 free(event);
1139
1140 return ret;
1141}
1142
1143/**
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001144 * tcg2_measure_gpt_table() - measure gpt table
1145 *
1146 * @dev: TPM device
1147 * @loaded_image: handle to the loaded image
1148 *
1149 * Return: status code
1150 */
1151static efi_status_t
1152tcg2_measure_gpt_data(struct udevice *dev,
1153 struct efi_loaded_image_obj *loaded_image)
1154{
1155 efi_status_t ret;
1156 efi_handle_t handle;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001157 struct efi_handler *dp_handler, *io_handler;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001158 struct efi_device_path *orig_device_path;
1159 struct efi_device_path *device_path;
1160 struct efi_device_path *dp;
1161 struct efi_block_io *block_io;
1162 struct efi_gpt_data *event = NULL;
1163 efi_guid_t null_guid = NULL_GUID;
1164 gpt_header *gpt_h;
1165 gpt_entry *entry = NULL;
1166 gpt_entry *gpt_e;
1167 u32 num_of_valid_entry = 0;
1168 u32 event_size;
1169 u32 i;
1170 u32 total_gpt_entry_size;
1171
1172 ret = efi_search_protocol(&loaded_image->header,
1173 &efi_guid_loaded_image_device_path,
1174 &dp_handler);
1175 if (ret != EFI_SUCCESS)
1176 return ret;
1177
1178 orig_device_path = dp_handler->protocol_interface;
1179 if (!orig_device_path) /* no device path, skip GPT measurement */
1180 return EFI_SUCCESS;
1181
1182 device_path = efi_dp_dup(orig_device_path);
1183 if (!device_path)
1184 return EFI_OUT_OF_RESOURCES;
1185
1186 dp = search_gpt_dp_node(device_path);
1187 if (!dp) {
1188 /* no GPT device path node found, skip GPT measurement */
1189 ret = EFI_SUCCESS;
1190 goto out1;
1191 }
1192
1193 /* read GPT header */
1194 dp->type = DEVICE_PATH_TYPE_END;
1195 dp->sub_type = DEVICE_PATH_SUB_TYPE_END;
1196 dp = device_path;
1197 ret = EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_guid,
1198 &dp, &handle));
1199 if (ret != EFI_SUCCESS)
1200 goto out1;
1201
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001202 ret = efi_search_protocol(handle, &efi_block_io_guid, &io_handler);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001203 if (ret != EFI_SUCCESS)
1204 goto out1;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001205 block_io = io_handler->protocol_interface;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001206
1207 gpt_h = memalign(block_io->media->io_align, block_io->media->block_size);
1208 if (!gpt_h) {
1209 ret = EFI_OUT_OF_RESOURCES;
1210 goto out2;
1211 }
1212
1213 ret = block_io->read_blocks(block_io, block_io->media->media_id, 1,
1214 block_io->media->block_size, gpt_h);
1215 if (ret != EFI_SUCCESS)
1216 goto out2;
1217
1218 /* read GPT entry */
1219 total_gpt_entry_size = gpt_h->num_partition_entries *
1220 gpt_h->sizeof_partition_entry;
1221 entry = memalign(block_io->media->io_align, total_gpt_entry_size);
1222 if (!entry) {
1223 ret = EFI_OUT_OF_RESOURCES;
1224 goto out2;
1225 }
1226
1227 ret = block_io->read_blocks(block_io, block_io->media->media_id,
1228 gpt_h->partition_entry_lba,
1229 total_gpt_entry_size, entry);
1230 if (ret != EFI_SUCCESS)
1231 goto out2;
1232
1233 /* count valid GPT entry */
1234 gpt_e = entry;
1235 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1236 if (guidcmp(&null_guid, &gpt_e->partition_type_guid))
1237 num_of_valid_entry++;
1238
1239 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1240 }
1241
1242 /* prepare event data for measurement */
1243 event_size = sizeof(struct efi_gpt_data) +
1244 (num_of_valid_entry * gpt_h->sizeof_partition_entry);
1245 event = calloc(1, event_size);
1246 if (!event) {
1247 ret = EFI_OUT_OF_RESOURCES;
1248 goto out2;
1249 }
1250 memcpy(event, gpt_h, sizeof(gpt_header));
1251 put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions);
1252
1253 /* copy valid GPT entry */
1254 gpt_e = entry;
1255 num_of_valid_entry = 0;
1256 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1257 if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) {
1258 memcpy((u8 *)event->partitions +
1259 (num_of_valid_entry * gpt_h->sizeof_partition_entry),
1260 gpt_e, gpt_h->sizeof_partition_entry);
1261 num_of_valid_entry++;
1262 }
1263
1264 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1265 }
1266
Eddie James8ed7bb32023-10-24 10:43:49 -05001267 ret = measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *)event);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001268
1269out2:
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001270 free(gpt_h);
1271 free(entry);
1272 free(event);
1273out1:
1274 efi_free_pool(device_path);
1275
1276 return ret;
1277}
1278
Etienne Carriereb9064352023-02-16 17:29:48 +01001279/* Return the byte size of reserved map area in DTB or -1 upon error */
1280static ssize_t size_of_rsvmap(void *dtb)
1281{
1282 struct fdt_reserve_entry e;
1283 ssize_t size_max;
1284 ssize_t size;
1285 u8 *rsvmap_base;
1286
1287 rsvmap_base = (u8 *)dtb + fdt_off_mem_rsvmap(dtb);
1288 size_max = fdt_totalsize(dtb) - fdt_off_mem_rsvmap(dtb);
1289 size = 0;
1290
1291 do {
1292 memcpy(&e, rsvmap_base + size, sizeof(e));
1293 size += sizeof(e);
1294 if (size > size_max)
1295 return -1;
1296 } while (e.size);
1297
1298 return size;
1299}
1300
1301/**
1302 * efi_tcg2_measure_dtb() - measure DTB passed to the OS
1303 *
1304 * @dtb: pointer to the device tree blob
1305 *
1306 * Return: status code
1307 */
1308efi_status_t efi_tcg2_measure_dtb(void *dtb)
1309{
1310 struct uefi_platform_firmware_blob2 *blob;
1311 struct fdt_header *header;
1312 sha256_context hash_ctx;
1313 struct udevice *dev;
1314 ssize_t rsvmap_size;
1315 efi_status_t ret;
1316 u32 event_size;
1317
1318 if (!is_tcg2_protocol_installed())
1319 return EFI_SUCCESS;
1320
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001321 if (tcg2_platform_get_tpm2(&dev))
Etienne Carriereb9064352023-02-16 17:29:48 +01001322 return EFI_SECURITY_VIOLATION;
1323
1324 rsvmap_size = size_of_rsvmap(dtb);
1325 if (rsvmap_size < 0)
1326 return EFI_SECURITY_VIOLATION;
1327
1328 event_size = sizeof(*blob) + sizeof(EFI_DTB_EVENT_STRING) + SHA256_SUM_LEN;
1329 blob = calloc(1, event_size);
1330 if (!blob)
1331 return EFI_OUT_OF_RESOURCES;
1332
1333 blob->blob_description_size = sizeof(EFI_DTB_EVENT_STRING);
1334 memcpy(blob->data, EFI_DTB_EVENT_STRING, blob->blob_description_size);
1335
1336 /* Measure populated areas of the DTB */
1337 header = dtb;
1338 sha256_starts(&hash_ctx);
1339 sha256_update(&hash_ctx, (u8 *)header, sizeof(struct fdt_header));
1340 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_struct(dtb), fdt_size_dt_strings(dtb));
1341 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_strings(dtb), fdt_size_dt_struct(dtb));
1342 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_mem_rsvmap(dtb), rsvmap_size);
1343 sha256_finish(&hash_ctx, blob->data + blob->blob_description_size);
1344
Ilias Apalodimasc90c1952024-06-14 15:09:50 +03001345 ret = measure_event(dev, 1, EV_POST_CODE, event_size, (u8 *)blob);
Etienne Carriereb9064352023-02-16 17:29:48 +01001346
1347 free(blob);
1348 return ret;
1349}
1350
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001351/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001352 * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation
1353 *
1354 * Return: status code
1355 */
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001356efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_image_obj *handle)
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001357{
1358 efi_status_t ret;
1359 u32 pcr_index;
1360 struct udevice *dev;
1361 u32 event = 0;
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001362 struct smbios3_entry *entry;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001363
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001364 if (!is_tcg2_protocol_installed())
1365 return EFI_SUCCESS;
1366
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001367 if (tcg2_efi_app_invoked)
1368 return EFI_SUCCESS;
1369
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001370 if (tcg2_platform_get_tpm2(&dev))
Masahisa Kojima38155ea2021-12-07 14:15:33 +09001371 return EFI_SECURITY_VIOLATION;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001372
1373 ret = tcg2_measure_boot_variable(dev);
1374 if (ret != EFI_SUCCESS)
1375 goto out;
1376
Eddie James8ed7bb32023-10-24 10:43:49 -05001377 ret = measure_event(dev, 4, EV_EFI_ACTION,
1378 strlen(EFI_CALLING_EFI_APPLICATION),
1379 (u8 *)EFI_CALLING_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001380 if (ret != EFI_SUCCESS)
1381 goto out;
1382
Heinrich Schuchardt10899c82024-01-26 09:13:22 +01001383 entry = efi_get_configuration_table(&smbios3_guid);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001384 if (entry) {
1385 ret = tcg2_measure_smbios(dev, entry);
1386 if (ret != EFI_SUCCESS)
1387 goto out;
1388 }
1389
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001390 ret = tcg2_measure_gpt_data(dev, handle);
1391 if (ret != EFI_SUCCESS)
1392 goto out;
1393
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001394 for (pcr_index = 0; pcr_index <= 7; pcr_index++) {
Eddie James8ed7bb32023-10-24 10:43:49 -05001395 ret = measure_event(dev, pcr_index, EV_SEPARATOR,
1396 sizeof(event), (u8 *)&event);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001397 if (ret != EFI_SUCCESS)
1398 goto out;
1399 }
1400
1401 tcg2_efi_app_invoked = true;
1402out:
1403 return ret;
1404}
1405
1406/**
1407 * efi_tcg2_measure_efi_app_exit() - measure efi app exit
1408 *
1409 * Return: status code
1410 */
1411efi_status_t efi_tcg2_measure_efi_app_exit(void)
1412{
1413 efi_status_t ret;
1414 struct udevice *dev;
1415
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001416 if (!is_tcg2_protocol_installed())
1417 return EFI_SUCCESS;
1418
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001419 if (tcg2_platform_get_tpm2(&dev))
1420 return EFI_SECURITY_VIOLATION;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001421
Eddie James8ed7bb32023-10-24 10:43:49 -05001422 ret = measure_event(dev, 4, EV_EFI_ACTION,
1423 strlen(EFI_RETURNING_FROM_EFI_APPLICATION),
1424 (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001425 return ret;
1426}
1427
1428/**
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001429 * efi_tcg2_notify_exit_boot_services() - ExitBootService callback
1430 *
1431 * @event: callback event
1432 * @context: callback context
1433 */
1434static void EFIAPI
1435efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context)
1436{
1437 efi_status_t ret;
1438 struct udevice *dev;
1439
1440 EFI_ENTRY("%p, %p", event, context);
1441
Ilias Apalodimas24e841a2021-11-18 09:03:39 +02001442 event_log.ebs_called = true;
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001443
1444 if (!is_tcg2_protocol_installed()) {
1445 ret = EFI_SUCCESS;
1446 goto out;
1447 }
1448
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001449 if (tcg2_platform_get_tpm2(&dev)) {
1450 ret = EFI_SECURITY_VIOLATION;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001451 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001452 }
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001453
Eddie James8ed7bb32023-10-24 10:43:49 -05001454 ret = measure_event(dev, 5, EV_EFI_ACTION,
1455 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1456 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001457 if (ret != EFI_SUCCESS)
1458 goto out;
1459
Eddie James8ed7bb32023-10-24 10:43:49 -05001460 ret = measure_event(dev, 5, EV_EFI_ACTION,
1461 strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED),
1462 (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001463
1464out:
1465 EFI_EXIT(ret);
1466}
1467
1468/**
1469 * efi_tcg2_notify_exit_boot_services_failed()
1470 * - notify ExitBootServices() is failed
1471 *
1472 * Return: status code
1473 */
1474efi_status_t efi_tcg2_notify_exit_boot_services_failed(void)
1475{
1476 struct udevice *dev;
1477 efi_status_t ret;
1478
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001479 if (!is_tcg2_protocol_installed())
1480 return EFI_SUCCESS;
1481
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001482 if (tcg2_platform_get_tpm2(&dev))
1483 return EFI_SECURITY_VIOLATION;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001484
Eddie James8ed7bb32023-10-24 10:43:49 -05001485 ret = measure_event(dev, 5, EV_EFI_ACTION,
1486 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1487 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001488 if (ret != EFI_SUCCESS)
1489 goto out;
1490
Eddie James8ed7bb32023-10-24 10:43:49 -05001491 ret = measure_event(dev, 5, EV_EFI_ACTION,
1492 strlen(EFI_EXIT_BOOT_SERVICES_FAILED),
1493 (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001494
1495out:
1496 return ret;
1497}
1498
1499/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001500 * tcg2_measure_secure_boot_variable() - measure secure boot variables
1501 *
1502 * @dev: TPM device
1503 *
1504 * Return: status code
1505 */
1506static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev)
1507{
1508 u8 *data;
1509 efi_uintn_t data_size;
1510 u32 count, i;
1511 efi_status_t ret;
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001512 u8 deployed_mode;
1513 efi_uintn_t size;
1514 u32 deployed_audit_pcr_index = 1;
1515
1516 size = sizeof(deployed_mode);
1517 ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid,
1518 NULL, &size, &deployed_mode, NULL);
1519 if (ret != EFI_SUCCESS || !deployed_mode)
1520 deployed_audit_pcr_index = 7;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001521
1522 count = ARRAY_SIZE(secure_variables);
1523 for (i = 0; i < count; i++) {
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001524 const efi_guid_t *guid;
1525
Masahisa Kojima21684522021-10-26 17:27:26 +09001526 guid = efi_auth_var_get_guid(secure_variables[i].name);
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001527
Masahisa Kojima21684522021-10-26 17:27:26 +09001528 data = efi_get_var(secure_variables[i].name, guid, &data_size);
1529 if (!data && !secure_variables[i].accept_empty)
1530 continue;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001531
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001532 if (u16_strcmp(u"DeployedMode", secure_variables[i].name))
1533 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1534 if (u16_strcmp(u"AuditMode", secure_variables[i].name))
1535 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1536
1537 ret = tcg2_measure_variable(dev, secure_variables[i].pcr_index,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001538 EV_EFI_VARIABLE_DRIVER_CONFIG,
Masahisa Kojima21684522021-10-26 17:27:26 +09001539 secure_variables[i].name, guid,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001540 data_size, data);
1541 free(data);
1542 if (ret != EFI_SUCCESS)
1543 goto error;
1544 }
1545
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001546error:
1547 return ret;
1548}
1549
1550/**
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001551 * efi_tcg2_do_initial_measurement() - do initial measurement
1552 *
1553 * Return: status code
1554 */
1555efi_status_t efi_tcg2_do_initial_measurement(void)
1556{
1557 efi_status_t ret;
1558 struct udevice *dev;
1559
1560 if (!is_tcg2_protocol_installed())
1561 return EFI_SUCCESS;
1562
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001563 if (tcg2_platform_get_tpm2(&dev))
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001564 return EFI_SECURITY_VIOLATION;
1565
1566 ret = tcg2_measure_secure_boot_variable(dev);
1567 if (ret != EFI_SUCCESS)
1568 goto out;
1569
1570out:
1571 return ret;
1572}
1573
1574/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001575 * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
1576 *
1577 * If a TPM2 device is available, the TPM TCG2 Protocol is registered
1578 *
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001579 * Return: status code
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001580 */
1581efi_status_t efi_tcg2_register(void)
1582{
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001583 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001584 struct udevice *dev;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001585 struct efi_event *event;
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001586 u32 err;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001587
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001588 if (tcg2_platform_get_tpm2(&dev)) {
Ilias Apalodimaseb1b6b42023-01-19 16:29:15 +02001589 log_warning("Missing TPMv2 device for EFI_TCG_PROTOCOL\n");
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001590 return EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001591 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001592
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001593 /* initialize the TPM as early as possible. */
Ilias Apalodimas0c95d222023-01-25 13:06:03 +02001594 err = tpm_auto_start(dev);
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001595 if (err) {
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001596 ret = EFI_DEVICE_ERROR;
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001597 log_err("TPM startup failed\n");
1598 goto fail;
1599 }
1600
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001601 ret = efi_init_event_log();
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001602 if (ret != EFI_SUCCESS) {
1603 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001604 goto fail;
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001605 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001606
Ilias Apalodimas4953c992023-06-19 14:14:02 +03001607 ret = efi_install_multiple_protocol_interfaces(&efi_root, &efi_guid_tcg2_protocol,
1608 &efi_tcg2_protocol, NULL);
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001609 if (ret != EFI_SUCCESS) {
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001610 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001611 goto fail;
1612 }
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001613
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001614 ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
1615 efi_tcg2_notify_exit_boot_services, NULL,
1616 NULL, &event);
1617 if (ret != EFI_SUCCESS) {
1618 tcg2_uninit();
1619 goto fail;
1620 }
1621
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001622 return ret;
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001623
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001624fail:
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001625 log_err("Cannot install EFI_TCG2_PROTOCOL\n");
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001626 return ret;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001627}