blob: c697b53441a5ee8f7735aaf31c5054bef3681c55 [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 ||
115 event_log.pos + event_size > TPM2_EVENT_LOG_SIZE) {
116 event_log.truncated = true;
117 return EFI_VOLUME_FULL;
118 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500119 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200120 event_log.pos += event_size;
121 event_log.last_event_size = event_size;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200122 }
123
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200124 if (!event_log.get_event_called)
125 return ret;
126
127 /* if GetEventLog has been called update FinalEventLog as well */
128 if (event_log.final_pos + event_size > TPM2_EVENT_LOG_SIZE)
129 return EFI_VOLUME_FULL;
130
131 log = (void *)((uintptr_t)event_log.final_buffer + event_log.final_pos);
Eddie James8ed7bb32023-10-24 10:43:49 -0500132 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200133
134 final_event = event_log.final_buffer;
135 final_event->number_of_events++;
136 event_log.final_pos += event_size;
137
138 return ret;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200139}
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200140
141/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200142 * tpm2_get_max_command_size() - get the supported max command size
143 *
144 * @dev: TPM device
145 * @max_command_size: output buffer for the size
146 *
147 * Return: 0 on success, -1 on error
148 */
149static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size)
150{
151 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
152 u32 ret;
153
154 memset(response, 0, sizeof(response));
155 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
156 TPM2_PT_MAX_COMMAND_SIZE, response, 1);
157 if (ret)
158 return -1;
159
160 *max_command_size = (uint16_t)get_unaligned_be32(response +
161 properties_offset);
162
163 return 0;
164}
165
166/**
167 * tpm2_get_max_response_size() - get the supported max response size
168 *
169 * @dev: TPM device
170 * @max_response_size: output buffer for the size
171 *
172 * Return: 0 on success, -1 on error
173 */
174static int tpm2_get_max_response_size(struct udevice *dev,
175 u16 *max_response_size)
176{
177 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
178 u32 ret;
179
180 memset(response, 0, sizeof(response));
181 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
182 TPM2_PT_MAX_RESPONSE_SIZE, response, 1);
183 if (ret)
184 return -1;
185
186 *max_response_size = (uint16_t)get_unaligned_be32(response +
187 properties_offset);
188
189 return 0;
190}
191
192/**
193 * tpm2_get_manufacturer_id() - get the manufacturer ID
194 *
195 * @dev: TPM device
196 * @manufacturer_id: output buffer for the id
197 *
198 * Return: 0 on success, -1 on error
199 */
200static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id)
201{
202 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
203 u32 ret;
204
205 memset(response, 0, sizeof(response));
206 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
207 TPM2_PT_MANUFACTURER, response, 1);
208 if (ret)
209 return -1;
210
211 *manufacturer_id = get_unaligned_be32(response + properties_offset);
212
213 return 0;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200214}
215
216/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200217 * efi_tcg2_get_capability() - protocol capability information and state information
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200218 *
219 * @this: TCG2 protocol instance
220 * @capability: caller allocated memory with size field to the size of
221 * the structure allocated
222
223 * Return: status code
224 */
225static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200226efi_tcg2_get_capability(struct efi_tcg2_protocol *this,
227 struct efi_tcg2_boot_service_capability *capability)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200228{
229 struct udevice *dev;
230 efi_status_t efi_ret;
231 int ret;
232
233 EFI_ENTRY("%p, %p", this, capability);
234
235 if (!this || !capability) {
236 efi_ret = EFI_INVALID_PARAMETER;
237 goto out;
238 }
239
Masahisa Kojima9cc82932021-09-06 12:04:12 +0900240 if (capability->size < BOOT_SERVICE_CAPABILITY_MIN) {
241 capability->size = BOOT_SERVICE_CAPABILITY_MIN;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200242 efi_ret = EFI_BUFFER_TOO_SMALL;
243 goto out;
244 }
245
246 if (capability->size < sizeof(*capability)) {
247 capability->size = sizeof(*capability);
248 efi_ret = EFI_BUFFER_TOO_SMALL;
249 goto out;
250 }
251
252 capability->structure_version.major = 1;
253 capability->structure_version.minor = 1;
254 capability->protocol_version.major = 1;
255 capability->protocol_version.minor = 1;
256
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 */
826 ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE,
827 &event_log.final_buffer);
828 if (ret != EFI_SUCCESS)
829 goto out;
830
831 memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE);
832 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
917 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE,
918 (void **)&event_log.buffer);
919 if (ret != EFI_SUCCESS)
920 return ret;
921
922 /*
923 * initialize log area as 0xff so the OS can easily figure out the
924 * last log entry
925 */
926 memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE);
927
928 /*
929 * The log header is defined to be in SHA1 event log entry format.
930 * Setup event header
931 */
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530932 event_log.pos = 0;
933 event_log.last_event_size = 0;
934 event_log.get_event_called = false;
935 event_log.ebs_called = false;
936 event_log.truncated = false;
937
938 /*
939 * Check if earlier firmware have passed any eventlog. Different
940 * platforms can use different ways to do so.
941 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500942 elog.log = event_log.buffer;
943 elog.log_size = TPM2_EVENT_LOG_SIZE;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300944 rc = tcg2_log_prepare_buffer(dev, &elog, false);
945 if (rc) {
946 ret = (rc == -ENOBUFS) ? EFI_BUFFER_TOO_SMALL : EFI_DEVICE_ERROR;
Eddie James8ed7bb32023-10-24 10:43:49 -0500947 goto free_pool;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300948 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500949
950 event_log.pos = elog.log_position;
951
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530952 /*
Eddie James8ed7bb32023-10-24 10:43:49 -0500953 * Add SCRTM version to the log if previous firmmware
954 * doesn't pass an eventlog.
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530955 */
Ilias Apalodimas12c15f52023-11-07 13:31:34 +0200956 if (!elog.found) {
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530957 ret = efi_append_scrtm_version(dev);
Ilias Apalodimas12c15f52023-11-07 13:31:34 +0200958 if (ret != EFI_SUCCESS)
959 goto free_pool;
960 }
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530961
962 ret = create_final_event();
963 if (ret != EFI_SUCCESS)
964 goto free_pool;
965
966 return ret;
967
968free_pool:
969 efi_free_pool(event_log.buffer);
970 event_log.buffer = NULL;
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200971 return ret;
972}
973
974/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900975 * tcg2_measure_variable() - add variable event log and extend PCR
976 *
977 * @dev: TPM device
978 * @pcr_index: PCR index
979 * @event_type: type of event added
980 * @var_name: variable name
981 * @guid: guid
982 * @data_size: variable data size
983 * @data: variable data
984 *
985 * Return: status code
986 */
987static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index,
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +0200988 u32 event_type, const u16 *var_name,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900989 const efi_guid_t *guid,
990 efi_uintn_t data_size, u8 *data)
991{
992 u32 event_size;
993 efi_status_t ret;
994 struct efi_tcg2_uefi_variable_data *event;
995
996 event_size = sizeof(event->variable_name) +
997 sizeof(event->unicode_name_length) +
998 sizeof(event->variable_data_length) +
999 (u16_strlen(var_name) * sizeof(u16)) + data_size;
1000 event = malloc(event_size);
1001 if (!event)
1002 return EFI_OUT_OF_RESOURCES;
1003
1004 guidcpy(&event->variable_name, guid);
1005 event->unicode_name_length = u16_strlen(var_name);
1006 event->variable_data_length = data_size;
1007 memcpy(event->unicode_name, var_name,
1008 (event->unicode_name_length * sizeof(u16)));
1009 if (data) {
1010 memcpy((u16 *)event->unicode_name + event->unicode_name_length,
1011 data, data_size);
1012 }
Eddie James8ed7bb32023-10-24 10:43:49 -05001013 ret = measure_event(dev, pcr_index, event_type, event_size,
1014 (u8 *)event);
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001015 free(event);
1016 return ret;
1017}
1018
1019/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001020 * tcg2_measure_boot_variable() - measure boot variables
1021 *
1022 * @dev: TPM device
1023 *
1024 * Return: status code
1025 */
1026static efi_status_t tcg2_measure_boot_variable(struct udevice *dev)
1027{
1028 u16 *boot_order;
1029 u16 *boot_index;
Simon Glass90975372022-01-23 12:55:12 -07001030 u16 var_name[] = u"BootOrder";
1031 u16 boot_name[] = u"Boot####";
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001032 u8 *bootvar;
1033 efi_uintn_t var_data_size;
1034 u32 count, i;
1035 efi_status_t ret;
1036
1037 boot_order = efi_get_var(var_name, &efi_global_variable_guid,
1038 &var_data_size);
1039 if (!boot_order) {
Masahisa Kojimad1325932021-11-09 18:44:54 +09001040 /* If "BootOrder" is not defined, skip the boot variable measurement */
1041 return EFI_SUCCESS;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001042 }
1043
1044 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name,
1045 &efi_global_variable_guid, var_data_size,
1046 (u8 *)boot_order);
1047 if (ret != EFI_SUCCESS)
1048 goto error;
1049
1050 count = var_data_size / sizeof(*boot_order);
1051 boot_index = boot_order;
1052 for (i = 0; i < count; i++) {
1053 efi_create_indexed_name(boot_name, sizeof(boot_name),
1054 "Boot", *boot_index++);
1055
1056 bootvar = efi_get_var(boot_name, &efi_global_variable_guid,
1057 &var_data_size);
1058
1059 if (!bootvar) {
Masahisa Kojimaaca20c82021-11-09 20:35:53 +09001060 log_debug("%ls not found\n", boot_name);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001061 continue;
1062 }
1063
1064 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2,
1065 boot_name,
1066 &efi_global_variable_guid,
1067 var_data_size, bootvar);
1068 free(bootvar);
1069 if (ret != EFI_SUCCESS)
1070 goto error;
1071 }
1072
1073error:
1074 free(boot_order);
1075 return ret;
1076}
1077
1078/**
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001079 * tcg2_measure_smbios() - measure smbios table
1080 *
1081 * @dev: TPM device
1082 * @entry: pointer to the smbios_entry structure
1083 *
1084 * Return: status code
1085 */
1086static efi_status_t
1087tcg2_measure_smbios(struct udevice *dev,
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001088 const struct smbios3_entry *entry)
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001089{
1090 efi_status_t ret;
1091 struct smbios_header *smbios_copy;
1092 struct smbios_handoff_table_pointers2 *event = NULL;
1093 u32 event_size;
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001094 const char smbios3_anchor[] = "_SM3_";
1095
1096 /* We only support SMBIOS 3.0 Entry Point structure */
1097 if (memcmp(entry->anchor, smbios3_anchor, sizeof(smbios3_anchor) - 1))
1098 return EFI_UNSUPPORTED;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001099
1100 /*
1101 * TCG PC Client PFP Spec says
1102 * "SMBIOS structures that contain static configuration information
1103 * (e.g. Platform Manufacturer Enterprise Number assigned by IANA,
1104 * platform model number, Vendor and Device IDs for each SMBIOS table)
1105 * that is relevant to the security of the platform MUST be measured".
1106 * Device dependent parameters such as serial number are cleared to
1107 * zero or spaces for the measurement.
1108 */
1109 event_size = sizeof(struct smbios_handoff_table_pointers2) +
1110 FIELD_SIZEOF(struct efi_configuration_table, guid) +
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +01001111 entry->table_maximum_size;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001112 event = calloc(1, event_size);
1113 if (!event) {
1114 ret = EFI_OUT_OF_RESOURCES;
1115 goto out;
1116 }
1117
1118 event->table_description_size = sizeof(SMBIOS_HANDOFF_TABLE_DESC);
1119 memcpy(event->table_description, SMBIOS_HANDOFF_TABLE_DESC,
1120 sizeof(SMBIOS_HANDOFF_TABLE_DESC));
1121 put_unaligned_le64(1, &event->number_of_tables);
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001122 guidcpy(&event->table_entry[0].guid, &smbios3_guid);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001123 smbios_copy = (struct smbios_header *)((uintptr_t)&event->table_entry[0].table);
1124 memcpy(&event->table_entry[0].table,
1125 (void *)((uintptr_t)entry->struct_table_address),
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +01001126 entry->table_maximum_size);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001127
1128 smbios_prepare_measurement(entry, smbios_copy);
1129
Eddie James8ed7bb32023-10-24 10:43:49 -05001130 ret = measure_event(dev, 1, EV_EFI_HANDOFF_TABLES2, event_size,
1131 (u8 *)event);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001132 if (ret != EFI_SUCCESS)
1133 goto out;
1134
1135out:
1136 free(event);
1137
1138 return ret;
1139}
1140
1141/**
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001142 * tcg2_measure_gpt_table() - measure gpt table
1143 *
1144 * @dev: TPM device
1145 * @loaded_image: handle to the loaded image
1146 *
1147 * Return: status code
1148 */
1149static efi_status_t
1150tcg2_measure_gpt_data(struct udevice *dev,
1151 struct efi_loaded_image_obj *loaded_image)
1152{
1153 efi_status_t ret;
1154 efi_handle_t handle;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001155 struct efi_handler *dp_handler, *io_handler;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001156 struct efi_device_path *orig_device_path;
1157 struct efi_device_path *device_path;
1158 struct efi_device_path *dp;
1159 struct efi_block_io *block_io;
1160 struct efi_gpt_data *event = NULL;
1161 efi_guid_t null_guid = NULL_GUID;
1162 gpt_header *gpt_h;
1163 gpt_entry *entry = NULL;
1164 gpt_entry *gpt_e;
1165 u32 num_of_valid_entry = 0;
1166 u32 event_size;
1167 u32 i;
1168 u32 total_gpt_entry_size;
1169
1170 ret = efi_search_protocol(&loaded_image->header,
1171 &efi_guid_loaded_image_device_path,
1172 &dp_handler);
1173 if (ret != EFI_SUCCESS)
1174 return ret;
1175
1176 orig_device_path = dp_handler->protocol_interface;
1177 if (!orig_device_path) /* no device path, skip GPT measurement */
1178 return EFI_SUCCESS;
1179
1180 device_path = efi_dp_dup(orig_device_path);
1181 if (!device_path)
1182 return EFI_OUT_OF_RESOURCES;
1183
1184 dp = search_gpt_dp_node(device_path);
1185 if (!dp) {
1186 /* no GPT device path node found, skip GPT measurement */
1187 ret = EFI_SUCCESS;
1188 goto out1;
1189 }
1190
1191 /* read GPT header */
1192 dp->type = DEVICE_PATH_TYPE_END;
1193 dp->sub_type = DEVICE_PATH_SUB_TYPE_END;
1194 dp = device_path;
1195 ret = EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_guid,
1196 &dp, &handle));
1197 if (ret != EFI_SUCCESS)
1198 goto out1;
1199
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001200 ret = efi_search_protocol(handle, &efi_block_io_guid, &io_handler);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001201 if (ret != EFI_SUCCESS)
1202 goto out1;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001203 block_io = io_handler->protocol_interface;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001204
1205 gpt_h = memalign(block_io->media->io_align, block_io->media->block_size);
1206 if (!gpt_h) {
1207 ret = EFI_OUT_OF_RESOURCES;
1208 goto out2;
1209 }
1210
1211 ret = block_io->read_blocks(block_io, block_io->media->media_id, 1,
1212 block_io->media->block_size, gpt_h);
1213 if (ret != EFI_SUCCESS)
1214 goto out2;
1215
1216 /* read GPT entry */
1217 total_gpt_entry_size = gpt_h->num_partition_entries *
1218 gpt_h->sizeof_partition_entry;
1219 entry = memalign(block_io->media->io_align, total_gpt_entry_size);
1220 if (!entry) {
1221 ret = EFI_OUT_OF_RESOURCES;
1222 goto out2;
1223 }
1224
1225 ret = block_io->read_blocks(block_io, block_io->media->media_id,
1226 gpt_h->partition_entry_lba,
1227 total_gpt_entry_size, entry);
1228 if (ret != EFI_SUCCESS)
1229 goto out2;
1230
1231 /* count valid GPT entry */
1232 gpt_e = entry;
1233 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1234 if (guidcmp(&null_guid, &gpt_e->partition_type_guid))
1235 num_of_valid_entry++;
1236
1237 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1238 }
1239
1240 /* prepare event data for measurement */
1241 event_size = sizeof(struct efi_gpt_data) +
1242 (num_of_valid_entry * gpt_h->sizeof_partition_entry);
1243 event = calloc(1, event_size);
1244 if (!event) {
1245 ret = EFI_OUT_OF_RESOURCES;
1246 goto out2;
1247 }
1248 memcpy(event, gpt_h, sizeof(gpt_header));
1249 put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions);
1250
1251 /* copy valid GPT entry */
1252 gpt_e = entry;
1253 num_of_valid_entry = 0;
1254 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1255 if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) {
1256 memcpy((u8 *)event->partitions +
1257 (num_of_valid_entry * gpt_h->sizeof_partition_entry),
1258 gpt_e, gpt_h->sizeof_partition_entry);
1259 num_of_valid_entry++;
1260 }
1261
1262 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1263 }
1264
Eddie James8ed7bb32023-10-24 10:43:49 -05001265 ret = measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *)event);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001266
1267out2:
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001268 free(gpt_h);
1269 free(entry);
1270 free(event);
1271out1:
1272 efi_free_pool(device_path);
1273
1274 return ret;
1275}
1276
Etienne Carriereb9064352023-02-16 17:29:48 +01001277/* Return the byte size of reserved map area in DTB or -1 upon error */
1278static ssize_t size_of_rsvmap(void *dtb)
1279{
1280 struct fdt_reserve_entry e;
1281 ssize_t size_max;
1282 ssize_t size;
1283 u8 *rsvmap_base;
1284
1285 rsvmap_base = (u8 *)dtb + fdt_off_mem_rsvmap(dtb);
1286 size_max = fdt_totalsize(dtb) - fdt_off_mem_rsvmap(dtb);
1287 size = 0;
1288
1289 do {
1290 memcpy(&e, rsvmap_base + size, sizeof(e));
1291 size += sizeof(e);
1292 if (size > size_max)
1293 return -1;
1294 } while (e.size);
1295
1296 return size;
1297}
1298
1299/**
1300 * efi_tcg2_measure_dtb() - measure DTB passed to the OS
1301 *
1302 * @dtb: pointer to the device tree blob
1303 *
1304 * Return: status code
1305 */
1306efi_status_t efi_tcg2_measure_dtb(void *dtb)
1307{
1308 struct uefi_platform_firmware_blob2 *blob;
1309 struct fdt_header *header;
1310 sha256_context hash_ctx;
1311 struct udevice *dev;
1312 ssize_t rsvmap_size;
1313 efi_status_t ret;
1314 u32 event_size;
1315
1316 if (!is_tcg2_protocol_installed())
1317 return EFI_SUCCESS;
1318
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001319 if (tcg2_platform_get_tpm2(&dev))
Etienne Carriereb9064352023-02-16 17:29:48 +01001320 return EFI_SECURITY_VIOLATION;
1321
1322 rsvmap_size = size_of_rsvmap(dtb);
1323 if (rsvmap_size < 0)
1324 return EFI_SECURITY_VIOLATION;
1325
1326 event_size = sizeof(*blob) + sizeof(EFI_DTB_EVENT_STRING) + SHA256_SUM_LEN;
1327 blob = calloc(1, event_size);
1328 if (!blob)
1329 return EFI_OUT_OF_RESOURCES;
1330
1331 blob->blob_description_size = sizeof(EFI_DTB_EVENT_STRING);
1332 memcpy(blob->data, EFI_DTB_EVENT_STRING, blob->blob_description_size);
1333
1334 /* Measure populated areas of the DTB */
1335 header = dtb;
1336 sha256_starts(&hash_ctx);
1337 sha256_update(&hash_ctx, (u8 *)header, sizeof(struct fdt_header));
1338 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_struct(dtb), fdt_size_dt_strings(dtb));
1339 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_strings(dtb), fdt_size_dt_struct(dtb));
1340 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_mem_rsvmap(dtb), rsvmap_size);
1341 sha256_finish(&hash_ctx, blob->data + blob->blob_description_size);
1342
Ilias Apalodimasc90c1952024-06-14 15:09:50 +03001343 ret = measure_event(dev, 1, EV_POST_CODE, event_size, (u8 *)blob);
Etienne Carriereb9064352023-02-16 17:29:48 +01001344
1345 free(blob);
1346 return ret;
1347}
1348
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001349/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001350 * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation
1351 *
1352 * Return: status code
1353 */
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001354efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_image_obj *handle)
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001355{
1356 efi_status_t ret;
1357 u32 pcr_index;
1358 struct udevice *dev;
1359 u32 event = 0;
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001360 struct smbios3_entry *entry;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001361
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001362 if (!is_tcg2_protocol_installed())
1363 return EFI_SUCCESS;
1364
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001365 if (tcg2_efi_app_invoked)
1366 return EFI_SUCCESS;
1367
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001368 if (tcg2_platform_get_tpm2(&dev))
Masahisa Kojima38155ea2021-12-07 14:15:33 +09001369 return EFI_SECURITY_VIOLATION;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001370
1371 ret = tcg2_measure_boot_variable(dev);
1372 if (ret != EFI_SUCCESS)
1373 goto out;
1374
Eddie James8ed7bb32023-10-24 10:43:49 -05001375 ret = measure_event(dev, 4, EV_EFI_ACTION,
1376 strlen(EFI_CALLING_EFI_APPLICATION),
1377 (u8 *)EFI_CALLING_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001378 if (ret != EFI_SUCCESS)
1379 goto out;
1380
Heinrich Schuchardt10899c82024-01-26 09:13:22 +01001381 entry = efi_get_configuration_table(&smbios3_guid);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001382 if (entry) {
1383 ret = tcg2_measure_smbios(dev, entry);
1384 if (ret != EFI_SUCCESS)
1385 goto out;
1386 }
1387
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001388 ret = tcg2_measure_gpt_data(dev, handle);
1389 if (ret != EFI_SUCCESS)
1390 goto out;
1391
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001392 for (pcr_index = 0; pcr_index <= 7; pcr_index++) {
Eddie James8ed7bb32023-10-24 10:43:49 -05001393 ret = measure_event(dev, pcr_index, EV_SEPARATOR,
1394 sizeof(event), (u8 *)&event);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001395 if (ret != EFI_SUCCESS)
1396 goto out;
1397 }
1398
1399 tcg2_efi_app_invoked = true;
1400out:
1401 return ret;
1402}
1403
1404/**
1405 * efi_tcg2_measure_efi_app_exit() - measure efi app exit
1406 *
1407 * Return: status code
1408 */
1409efi_status_t efi_tcg2_measure_efi_app_exit(void)
1410{
1411 efi_status_t ret;
1412 struct udevice *dev;
1413
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001414 if (!is_tcg2_protocol_installed())
1415 return EFI_SUCCESS;
1416
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001417 if (tcg2_platform_get_tpm2(&dev))
1418 return EFI_SECURITY_VIOLATION;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001419
Eddie James8ed7bb32023-10-24 10:43:49 -05001420 ret = measure_event(dev, 4, EV_EFI_ACTION,
1421 strlen(EFI_RETURNING_FROM_EFI_APPLICATION),
1422 (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001423 return ret;
1424}
1425
1426/**
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001427 * efi_tcg2_notify_exit_boot_services() - ExitBootService callback
1428 *
1429 * @event: callback event
1430 * @context: callback context
1431 */
1432static void EFIAPI
1433efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context)
1434{
1435 efi_status_t ret;
1436 struct udevice *dev;
1437
1438 EFI_ENTRY("%p, %p", event, context);
1439
Ilias Apalodimas24e841a2021-11-18 09:03:39 +02001440 event_log.ebs_called = true;
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001441
1442 if (!is_tcg2_protocol_installed()) {
1443 ret = EFI_SUCCESS;
1444 goto out;
1445 }
1446
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001447 if (tcg2_platform_get_tpm2(&dev)) {
1448 ret = EFI_SECURITY_VIOLATION;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001449 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001450 }
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001451
Eddie James8ed7bb32023-10-24 10:43:49 -05001452 ret = measure_event(dev, 5, EV_EFI_ACTION,
1453 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1454 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001455 if (ret != EFI_SUCCESS)
1456 goto out;
1457
Eddie James8ed7bb32023-10-24 10:43:49 -05001458 ret = measure_event(dev, 5, EV_EFI_ACTION,
1459 strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED),
1460 (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001461
1462out:
1463 EFI_EXIT(ret);
1464}
1465
1466/**
1467 * efi_tcg2_notify_exit_boot_services_failed()
1468 * - notify ExitBootServices() is failed
1469 *
1470 * Return: status code
1471 */
1472efi_status_t efi_tcg2_notify_exit_boot_services_failed(void)
1473{
1474 struct udevice *dev;
1475 efi_status_t ret;
1476
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001477 if (!is_tcg2_protocol_installed())
1478 return EFI_SUCCESS;
1479
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001480 if (tcg2_platform_get_tpm2(&dev))
1481 return EFI_SECURITY_VIOLATION;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001482
Eddie James8ed7bb32023-10-24 10:43:49 -05001483 ret = measure_event(dev, 5, EV_EFI_ACTION,
1484 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1485 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001486 if (ret != EFI_SUCCESS)
1487 goto out;
1488
Eddie James8ed7bb32023-10-24 10:43:49 -05001489 ret = measure_event(dev, 5, EV_EFI_ACTION,
1490 strlen(EFI_EXIT_BOOT_SERVICES_FAILED),
1491 (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001492
1493out:
1494 return ret;
1495}
1496
1497/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001498 * tcg2_measure_secure_boot_variable() - measure secure boot variables
1499 *
1500 * @dev: TPM device
1501 *
1502 * Return: status code
1503 */
1504static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev)
1505{
1506 u8 *data;
1507 efi_uintn_t data_size;
1508 u32 count, i;
1509 efi_status_t ret;
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001510 u8 deployed_mode;
1511 efi_uintn_t size;
1512 u32 deployed_audit_pcr_index = 1;
1513
1514 size = sizeof(deployed_mode);
1515 ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid,
1516 NULL, &size, &deployed_mode, NULL);
1517 if (ret != EFI_SUCCESS || !deployed_mode)
1518 deployed_audit_pcr_index = 7;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001519
1520 count = ARRAY_SIZE(secure_variables);
1521 for (i = 0; i < count; i++) {
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001522 const efi_guid_t *guid;
1523
Masahisa Kojima21684522021-10-26 17:27:26 +09001524 guid = efi_auth_var_get_guid(secure_variables[i].name);
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001525
Masahisa Kojima21684522021-10-26 17:27:26 +09001526 data = efi_get_var(secure_variables[i].name, guid, &data_size);
1527 if (!data && !secure_variables[i].accept_empty)
1528 continue;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001529
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001530 if (u16_strcmp(u"DeployedMode", secure_variables[i].name))
1531 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1532 if (u16_strcmp(u"AuditMode", secure_variables[i].name))
1533 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1534
1535 ret = tcg2_measure_variable(dev, secure_variables[i].pcr_index,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001536 EV_EFI_VARIABLE_DRIVER_CONFIG,
Masahisa Kojima21684522021-10-26 17:27:26 +09001537 secure_variables[i].name, guid,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001538 data_size, data);
1539 free(data);
1540 if (ret != EFI_SUCCESS)
1541 goto error;
1542 }
1543
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001544error:
1545 return ret;
1546}
1547
1548/**
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001549 * efi_tcg2_do_initial_measurement() - do initial measurement
1550 *
1551 * Return: status code
1552 */
1553efi_status_t efi_tcg2_do_initial_measurement(void)
1554{
1555 efi_status_t ret;
1556 struct udevice *dev;
1557
1558 if (!is_tcg2_protocol_installed())
1559 return EFI_SUCCESS;
1560
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001561 if (tcg2_platform_get_tpm2(&dev))
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001562 return EFI_SECURITY_VIOLATION;
1563
1564 ret = tcg2_measure_secure_boot_variable(dev);
1565 if (ret != EFI_SUCCESS)
1566 goto out;
1567
1568out:
1569 return ret;
1570}
1571
1572/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001573 * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
1574 *
1575 * If a TPM2 device is available, the TPM TCG2 Protocol is registered
1576 *
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001577 * Return: status code
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001578 */
1579efi_status_t efi_tcg2_register(void)
1580{
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001581 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001582 struct udevice *dev;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001583 struct efi_event *event;
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001584 u32 err;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001585
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001586 if (tcg2_platform_get_tpm2(&dev)) {
Ilias Apalodimaseb1b6b42023-01-19 16:29:15 +02001587 log_warning("Missing TPMv2 device for EFI_TCG_PROTOCOL\n");
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001588 return EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001589 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001590
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001591 /* initialize the TPM as early as possible. */
Ilias Apalodimas0c95d222023-01-25 13:06:03 +02001592 err = tpm_auto_start(dev);
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001593 if (err) {
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001594 ret = EFI_DEVICE_ERROR;
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001595 log_err("TPM startup failed\n");
1596 goto fail;
1597 }
1598
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001599 ret = efi_init_event_log();
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001600 if (ret != EFI_SUCCESS) {
1601 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001602 goto fail;
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001603 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001604
Ilias Apalodimas4953c992023-06-19 14:14:02 +03001605 ret = efi_install_multiple_protocol_interfaces(&efi_root, &efi_guid_tcg2_protocol,
1606 &efi_tcg2_protocol, NULL);
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001607 if (ret != EFI_SUCCESS) {
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001608 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001609 goto fail;
1610 }
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001611
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001612 ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
1613 efi_tcg2_notify_exit_boot_services, NULL,
1614 NULL, &event);
1615 if (ret != EFI_SUCCESS) {
1616 tcg2_uninit();
1617 goto fail;
1618 }
1619
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001620 return ret;
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001621
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001622fail:
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001623 log_err("Cannot install EFI_TCG2_PROTOCOL\n");
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001624 return ret;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001625}