blob: 572c6b5bf6304f8cbed8e62a38af792b5b073f3f [file] [log] [blame]
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Defines APIs that allow an OS to interact with UEFI firmware to query
4 * information about the device.
5 * https://trustedcomputinggroup.org/resource/tcg-efi-protocol-specification/
6 *
7 * Copyright (c) 2020, Linaro Limited
8 */
9
10#define LOG_CATEGORY LOGC_EFI
Ilias Apalodimas590fef62020-11-11 11:18:11 +020011#include <dm.h>
12#include <efi_loader.h>
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +020013#include <efi_variable.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020014#include <efi_tcg2.h>
15#include <log.h>
Masahisa Kojima70be5a62021-05-26 12:09:58 +090016#include <malloc.h>
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +090017#include <smbios.h>
Pali Rohárba87ddf2021-08-02 15:18:31 +020018#include <version_string.h>
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +020019#include <tpm_api.h>
Masahisa Kojima70be5a62021-05-26 12:09:58 +090020#include <u-boot/hash-checksum.h>
Masahisa Kojimad420d8d2021-11-03 11:04:09 +090021#include <linux/unaligned/be_byteshift.h>
22#include <linux/unaligned/le_byteshift.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020023#include <linux/unaligned/generic.h>
Ilias Apalodimas967650d2020-11-30 11:47:40 +020024#include <hexdump.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020025
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020026/**
27 * struct event_log_buffer - internal eventlog management structure
28 *
29 * @buffer: eventlog buffer
30 * @final_buffer: finalevent config table buffer
31 * @pos: current position of 'buffer'
32 * @final_pos: current position of 'final_buffer'
33 * @get_event_called: true if GetEventLog has been invoked at least once
34 * @ebs_called: true if ExitBootServices has been invoked
35 * @truncated: true if the 'buffer' is truncated
36 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +020037struct event_log_buffer {
38 void *buffer;
39 void *final_buffer;
40 size_t pos; /* eventlog position */
41 size_t final_pos; /* final events config table position */
42 size_t last_event_size;
43 bool get_event_called;
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020044 bool ebs_called;
Ilias Apalodimas967650d2020-11-30 11:47:40 +020045 bool truncated;
46};
Ilias Apalodimas590fef62020-11-11 11:18:11 +020047
Ilias Apalodimas967650d2020-11-30 11:47:40 +020048static struct event_log_buffer event_log;
Masahisa Kojima8173cd42021-08-13 16:12:40 +090049static bool tcg2_efi_app_invoked;
Ilias Apalodimas590fef62020-11-11 11:18:11 +020050/*
51 * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset.
52 * Since the current tpm2_get_capability() response buffers starts at
53 * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate
54 * the response size and offset once for all consumers
55 */
56#define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \
57 offsetof(struct tpms_capability_data, data))
58#define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \
59 offsetof(struct tpms_tagged_property, value))
60
Ilias Apalodimas967650d2020-11-30 11:47:40 +020061static const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID;
62static const efi_guid_t efi_guid_final_events = EFI_TCG2_FINAL_EVENTS_TABLE_GUID;
63
Masahisa Kojima21684522021-10-26 17:27:26 +090064struct variable_info {
65 const u16 *name;
66 bool accept_empty;
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +090067 u32 pcr_index;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +090068};
69
Masahisa Kojima21684522021-10-26 17:27:26 +090070static struct variable_info secure_variables[] = {
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +090071 {u"SecureBoot", true, 7},
72 {u"PK", true, 7},
73 {u"KEK", true, 7},
74 {u"db", true, 7},
75 {u"dbx", true, 7},
76 {u"dbt", false, 7},
77 {u"dbr", false, 7},
78 {u"DeployedMode", false, 1},
79 {u"AuditMode", false, 1},
Masahisa Kojima21684522021-10-26 17:27:26 +090080};
81
Masahisa Kojima0fd43792021-12-07 14:15:31 +090082static bool is_tcg2_protocol_installed(void)
83{
84 struct efi_handler *handler;
85 efi_status_t ret;
86
87 ret = efi_search_protocol(efi_root, &efi_guid_tcg2_protocol, &handler);
88 return ret == EFI_SUCCESS;
89}
90
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020091/* tcg2_agile_log_append - Append an agile event to an eventlog
92 *
93 * @pcr_index: PCR index
94 * @event_type: type of event added
95 * @digest_list: list of digest algorithms to add
96 * @size: size of event
97 * @event: event to add
98 * @log: log buffer to append the event
99 *
100 * @Return: status code
101 */
102static efi_status_t tcg2_agile_log_append(u32 pcr_index, u32 event_type,
103 struct tpml_digest_values *digest_list,
104 u32 size, u8 event[])
105{
106 void *log = (void *)((uintptr_t)event_log.buffer + event_log.pos);
Eddie James8ed7bb32023-10-24 10:43:49 -0500107 u32 event_size = size + tcg2_event_get_size(digest_list);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200108 struct efi_tcg2_final_events_table *final_event;
109 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200110
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200111 /* if ExitBootServices hasn't been called update the normal log */
112 if (!event_log.ebs_called) {
113 if (event_log.truncated ||
114 event_log.pos + event_size > TPM2_EVENT_LOG_SIZE) {
115 event_log.truncated = true;
116 return EFI_VOLUME_FULL;
117 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500118 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200119 event_log.pos += event_size;
120 event_log.last_event_size = event_size;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200121 }
122
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200123 if (!event_log.get_event_called)
124 return ret;
125
126 /* if GetEventLog has been called update FinalEventLog as well */
127 if (event_log.final_pos + event_size > TPM2_EVENT_LOG_SIZE)
128 return EFI_VOLUME_FULL;
129
130 log = (void *)((uintptr_t)event_log.final_buffer + event_log.final_pos);
Eddie James8ed7bb32023-10-24 10:43:49 -0500131 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200132
133 final_event = event_log.final_buffer;
134 final_event->number_of_events++;
135 event_log.final_pos += event_size;
136
137 return ret;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200138}
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200139
140/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200141 * tpm2_get_max_command_size() - get the supported max command size
142 *
143 * @dev: TPM device
144 * @max_command_size: output buffer for the size
145 *
146 * Return: 0 on success, -1 on error
147 */
148static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size)
149{
150 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
151 u32 ret;
152
153 memset(response, 0, sizeof(response));
154 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
155 TPM2_PT_MAX_COMMAND_SIZE, response, 1);
156 if (ret)
157 return -1;
158
159 *max_command_size = (uint16_t)get_unaligned_be32(response +
160 properties_offset);
161
162 return 0;
163}
164
165/**
166 * tpm2_get_max_response_size() - get the supported max response size
167 *
168 * @dev: TPM device
169 * @max_response_size: output buffer for the size
170 *
171 * Return: 0 on success, -1 on error
172 */
173static int tpm2_get_max_response_size(struct udevice *dev,
174 u16 *max_response_size)
175{
176 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
177 u32 ret;
178
179 memset(response, 0, sizeof(response));
180 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
181 TPM2_PT_MAX_RESPONSE_SIZE, response, 1);
182 if (ret)
183 return -1;
184
185 *max_response_size = (uint16_t)get_unaligned_be32(response +
186 properties_offset);
187
188 return 0;
189}
190
191/**
192 * tpm2_get_manufacturer_id() - get the manufacturer ID
193 *
194 * @dev: TPM device
195 * @manufacturer_id: output buffer for the id
196 *
197 * Return: 0 on success, -1 on error
198 */
199static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id)
200{
201 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
202 u32 ret;
203
204 memset(response, 0, sizeof(response));
205 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
206 TPM2_PT_MANUFACTURER, response, 1);
207 if (ret)
208 return -1;
209
210 *manufacturer_id = get_unaligned_be32(response + properties_offset);
211
212 return 0;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200213}
214
215/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200216 * efi_tcg2_get_capability() - protocol capability information and state information
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200217 *
218 * @this: TCG2 protocol instance
219 * @capability: caller allocated memory with size field to the size of
220 * the structure allocated
221
222 * Return: status code
223 */
224static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200225efi_tcg2_get_capability(struct efi_tcg2_protocol *this,
226 struct efi_tcg2_boot_service_capability *capability)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200227{
228 struct udevice *dev;
229 efi_status_t efi_ret;
230 int ret;
231
232 EFI_ENTRY("%p, %p", this, capability);
233
234 if (!this || !capability) {
235 efi_ret = EFI_INVALID_PARAMETER;
236 goto out;
237 }
238
Masahisa Kojima9cc82932021-09-06 12:04:12 +0900239 if (capability->size < BOOT_SERVICE_CAPABILITY_MIN) {
240 capability->size = BOOT_SERVICE_CAPABILITY_MIN;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200241 efi_ret = EFI_BUFFER_TOO_SMALL;
242 goto out;
243 }
244
245 if (capability->size < sizeof(*capability)) {
246 capability->size = sizeof(*capability);
247 efi_ret = EFI_BUFFER_TOO_SMALL;
248 goto out;
249 }
250
251 capability->structure_version.major = 1;
252 capability->structure_version.minor = 1;
253 capability->protocol_version.major = 1;
254 capability->protocol_version.minor = 1;
255
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300256 ret = tcg2_platform_get_tpm2(&dev);
257 if (ret) {
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200258 capability->supported_event_logs = 0;
259 capability->hash_algorithm_bitmap = 0;
260 capability->tpm_present_flag = false;
261 capability->max_command_size = 0;
262 capability->max_response_size = 0;
263 capability->manufacturer_id = 0;
264 capability->number_of_pcr_banks = 0;
265 capability->active_pcr_banks = 0;
266
267 efi_ret = EFI_SUCCESS;
268 goto out;
269 }
270
271 /* We only allow a TPMv2 device to register the EFI protocol */
272 capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2;
273
274 capability->tpm_present_flag = true;
275
276 /* Supported and active PCRs */
277 capability->hash_algorithm_bitmap = 0;
278 capability->active_pcr_banks = 0;
Ilias Apalodimascb356612024-06-23 14:48:17 +0300279 ret = tcg2_get_pcr_info(dev, &capability->hash_algorithm_bitmap,
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200280 &capability->active_pcr_banks,
281 &capability->number_of_pcr_banks);
282 if (ret) {
283 efi_ret = EFI_DEVICE_ERROR;
284 goto out;
285 }
286
287 /* Max command size */
288 ret = tpm2_get_max_command_size(dev, &capability->max_command_size);
289 if (ret) {
290 efi_ret = EFI_DEVICE_ERROR;
291 goto out;
292 }
293
294 /* Max response size */
295 ret = tpm2_get_max_response_size(dev, &capability->max_response_size);
296 if (ret) {
297 efi_ret = EFI_DEVICE_ERROR;
298 goto out;
299 }
300
301 /* Manufacturer ID */
302 ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id);
303 if (ret) {
304 efi_ret = EFI_DEVICE_ERROR;
305 goto out;
306 }
307
308 return EFI_EXIT(EFI_SUCCESS);
309out:
310 return EFI_EXIT(efi_ret);
311}
312
313/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200314 * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its
315 * last entry
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200316 *
317 * @this: TCG2 protocol instance
318 * @log_format: type of event log format
319 * @event_log_location: pointer to the memory address of the event log
320 * @event_log_last_entry: pointer to the address of the start of the last
321 * entry in the event log in memory, if log contains
322 * more than 1 entry
323 * @event_log_truncated: set to true, if the Event Log is missing at i
324 * least one entry
325 *
326 * Return: status code
327 */
328static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200329efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this,
330 efi_tcg_event_log_format log_format,
331 u64 *event_log_location, u64 *event_log_last_entry,
332 bool *event_log_truncated)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200333{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200334 efi_status_t ret = EFI_SUCCESS;
335 struct udevice *dev;
336
337 EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location,
338 event_log_last_entry, event_log_truncated);
339
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900340 if (!this || !event_log_location || !event_log_last_entry ||
341 !event_log_truncated) {
342 ret = EFI_INVALID_PARAMETER;
343 goto out;
344 }
345
346 /* Only support TPMV2 */
347 if (log_format != TCG2_EVENT_LOG_FORMAT_TCG_2) {
348 ret = EFI_INVALID_PARAMETER;
349 goto out;
350 }
351
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300352 if (tcg2_platform_get_tpm2(&dev)) {
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200353 event_log_location = NULL;
354 event_log_last_entry = NULL;
355 *event_log_truncated = false;
356 ret = EFI_SUCCESS;
357 goto out;
358 }
359 *event_log_location = (uintptr_t)event_log.buffer;
360 *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos -
361 event_log.last_event_size);
362 *event_log_truncated = event_log.truncated;
363 event_log.get_event_called = true;
364
365out:
366 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200367}
368
369/**
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900370 * tcg2_hash_pe_image() - calculate PE/COFF image hash
371 *
372 * @efi: pointer to the EFI binary
373 * @efi_size: size of @efi binary
374 * @digest_list: list of digest algorithms to extend
375 *
376 * Return: status code
377 */
378static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size,
379 struct tpml_digest_values *digest_list)
380{
381 WIN_CERTIFICATE *wincerts = NULL;
382 size_t wincerts_len;
383 struct efi_image_regions *regs = NULL;
384 void *new_efi = NULL;
385 u8 hash[TPM2_SHA512_DIGEST_SIZE];
Eddie James8ed7bb32023-10-24 10:43:49 -0500386 struct udevice *dev;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300387 efi_status_t ret = EFI_SUCCESS;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900388 u32 active;
389 int i;
390
391 new_efi = efi_prepare_aligned_image(efi, &efi_size);
392 if (!new_efi)
393 return EFI_OUT_OF_RESOURCES;
394
395 if (!efi_image_parse(new_efi, efi_size, &regs, &wincerts,
396 &wincerts_len)) {
397 log_err("Parsing PE executable image failed\n");
398 ret = EFI_UNSUPPORTED;
399 goto out;
400 }
401
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300402 if (tcg2_platform_get_tpm2(&dev)) {
403 ret = EFI_DEVICE_ERROR;
Eddie James8ed7bb32023-10-24 10:43:49 -0500404 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300405 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500406
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300407 if (tcg2_get_active_pcr_banks(dev, &active)) {
408 ret = EFI_DEVICE_ERROR;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900409 goto out;
410 }
411
412 digest_list->count = 0;
Tim Harvey6ea1e052024-05-25 13:00:48 -0700413 for (i = 0; i < ARRAY_SIZE(hash_algo_list); i++) {
414 u16 hash_alg = hash_algo_list[i].hash_alg;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900415
Tim Harvey6ea1e052024-05-25 13:00:48 -0700416 if (!(active & hash_algo_list[i].hash_mask))
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900417 continue;
418 switch (hash_alg) {
419 case TPM2_ALG_SHA1:
420 hash_calculate("sha1", regs->reg, regs->num, hash);
421 break;
422 case TPM2_ALG_SHA256:
423 hash_calculate("sha256", regs->reg, regs->num, hash);
424 break;
425 case TPM2_ALG_SHA384:
426 hash_calculate("sha384", regs->reg, regs->num, hash);
427 break;
428 case TPM2_ALG_SHA512:
429 hash_calculate("sha512", regs->reg, regs->num, hash);
430 break;
431 default:
Heinrich Schuchardt09ec9f72023-07-31 14:11:34 +0200432 continue;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900433 }
Ruchika Guptae53007b2021-09-14 12:14:31 +0530434 digest_list->digests[digest_list->count].hash_alg = hash_alg;
435 memcpy(&digest_list->digests[digest_list->count].digest, hash,
Eddie James8ed7bb32023-10-24 10:43:49 -0500436 (u32)tpm2_algorithm_to_len(hash_alg));
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900437 digest_list->count++;
438 }
439
440out:
441 if (new_efi != efi)
442 free(new_efi);
443 free(regs);
444
445 return ret;
446}
447
448/**
449 * tcg2_measure_pe_image() - measure PE/COFF image
450 *
451 * @efi: pointer to the EFI binary
452 * @efi_size: size of @efi binary
453 * @handle: loaded image handle
454 * @loaded_image: loaded image protocol
455 *
456 * Return: status code
457 */
458efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size,
459 struct efi_loaded_image_obj *handle,
460 struct efi_loaded_image *loaded_image)
461{
462 struct tpml_digest_values digest_list;
463 efi_status_t ret;
464 struct udevice *dev;
465 u32 pcr_index, event_type, event_size;
466 struct uefi_image_load_event *image_load_event;
467 struct efi_device_path *device_path;
468 u32 device_path_length;
469 IMAGE_DOS_HEADER *dos;
470 IMAGE_NT_HEADERS32 *nt;
471 struct efi_handler *handler;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300472 int rc;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900473
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +0900474 if (!is_tcg2_protocol_installed())
475 return EFI_SUCCESS;
476
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300477 if (tcg2_platform_get_tpm2(&dev))
Masahisa Kojima38155ea2021-12-07 14:15:33 +0900478 return EFI_SECURITY_VIOLATION;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900479
480 switch (handle->image_type) {
481 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
482 pcr_index = 4;
483 event_type = EV_EFI_BOOT_SERVICES_APPLICATION;
484 break;
485 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
486 pcr_index = 2;
487 event_type = EV_EFI_BOOT_SERVICES_DRIVER;
488 break;
489 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
490 pcr_index = 2;
491 event_type = EV_EFI_RUNTIME_SERVICES_DRIVER;
492 break;
493 default:
494 return EFI_UNSUPPORTED;
495 }
496
497 ret = tcg2_hash_pe_image(efi, efi_size, &digest_list);
498 if (ret != EFI_SUCCESS)
499 return ret;
500
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300501 rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
502 if (rc)
503 return EFI_DEVICE_ERROR;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900504
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300505 ret = efi_search_protocol(&handle->header,
506 &efi_guid_loaded_image_device_path, &handler);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900507 if (ret != EFI_SUCCESS)
508 return ret;
509
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300510 device_path = handler->protocol_interface;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900511 device_path_length = efi_dp_size(device_path);
512 if (device_path_length > 0) {
513 /* add end node size */
514 device_path_length += sizeof(struct efi_device_path);
515 }
516 event_size = sizeof(struct uefi_image_load_event) + device_path_length;
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300517 image_load_event = calloc(1, event_size);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900518 if (!image_load_event)
519 return EFI_OUT_OF_RESOURCES;
520
521 image_load_event->image_location_in_memory = (uintptr_t)efi;
522 image_load_event->image_length_in_memory = efi_size;
523 image_load_event->length_of_device_path = device_path_length;
524
525 dos = (IMAGE_DOS_HEADER *)efi;
526 nt = (IMAGE_NT_HEADERS32 *)(efi + dos->e_lfanew);
527 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
528 IMAGE_NT_HEADERS64 *nt64 = (IMAGE_NT_HEADERS64 *)nt;
529
530 image_load_event->image_link_time_address =
531 nt64->OptionalHeader.ImageBase;
532 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
533 image_load_event->image_link_time_address =
534 nt->OptionalHeader.ImageBase;
535 } else {
536 ret = EFI_INVALID_PARAMETER;
537 goto out;
538 }
539
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300540 /* device_path_length might be zero */
541 memcpy(image_load_event->device_path, device_path, device_path_length);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900542
543 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
544 event_size, (u8 *)image_load_event);
545
546out:
547 free(image_load_event);
548
549 return ret;
550}
551
552/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200553 * efi_tcg2_hash_log_extend_event() - extend and optionally log events
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200554 *
555 * @this: TCG2 protocol instance
556 * @flags: bitmap providing additional information on the
557 * operation
558 * @data_to_hash: physical address of the start of the data buffer
559 * to be hashed
560 * @data_to_hash_len: the length in bytes of the buffer referenced by
561 * data_to_hash
562 * @efi_tcg_event: pointer to data buffer containing information
563 * about the event
564 *
565 * Return: status code
566 */
567static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200568efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags,
569 u64 data_to_hash, u64 data_to_hash_len,
570 struct efi_tcg2_event *efi_tcg_event)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200571{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200572 struct udevice *dev;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300573 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200574 u32 event_type, pcr_index, event_size;
575 struct tpml_digest_values digest_list;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300576 int rc = 0;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200577
578 EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash,
579 data_to_hash_len, efi_tcg_event);
580
581 if (!this || !data_to_hash || !efi_tcg_event) {
582 ret = EFI_INVALID_PARAMETER;
583 goto out;
584 }
585
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300586 if (tcg2_platform_get_tpm2(&dev)) {
587 ret = EFI_DEVICE_ERROR;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200588 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300589 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200590
591 if (efi_tcg_event->size < efi_tcg_event->header.header_size +
592 sizeof(u32)) {
593 ret = EFI_INVALID_PARAMETER;
594 goto out;
595 }
596
Masahisa Kojimab8074912021-09-03 10:55:52 +0900597 if (efi_tcg_event->header.pcr_index > EFI_TCG2_MAX_PCR_INDEX) {
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200598 ret = EFI_INVALID_PARAMETER;
599 goto out;
600 }
601
602 /*
603 * if PE_COFF_IMAGE is set we need to make sure the image is not
604 * corrupted, verify it and hash the PE/COFF image in accordance with
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900605 * the procedure specified in "Calculating the PE Image Hash"
606 * section of the "Windows Authenticode Portable Executable Signature
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200607 * Format"
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200608 */
609 if (flags & PE_COFF_IMAGE) {
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900610 ret = efi_check_pe((void *)(uintptr_t)data_to_hash,
Heinrich Schuchardtebe75212024-11-05 05:20:45 +0100611 data_to_hash_len, NULL);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900612 if (ret != EFI_SUCCESS) {
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900613 ret = EFI_UNSUPPORTED;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900614 goto out;
615 }
616 ret = tcg2_hash_pe_image((void *)(uintptr_t)data_to_hash,
617 data_to_hash_len, &digest_list);
618 } else {
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300619 rc = tcg2_create_digest(dev, (u8 *)(uintptr_t)data_to_hash,
620 data_to_hash_len, &digest_list);
621 if (rc)
622 ret = EFI_DEVICE_ERROR;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900623 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200624
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200625 if (ret != EFI_SUCCESS)
626 goto out;
627
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900628 pcr_index = efi_tcg_event->header.pcr_index;
629 event_type = efi_tcg_event->header.event_type;
630
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300631 rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
632 if (rc) {
633 ret = EFI_DEVICE_ERROR;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200634 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300635 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200636
637 if (flags & EFI_TCG2_EXTEND_ONLY) {
638 if (event_log.truncated)
639 ret = EFI_VOLUME_FULL;
640 goto out;
641 }
642
643 /*
644 * The efi_tcg_event size includes the size component and the
645 * headersize
646 */
647 event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) -
648 efi_tcg_event->header.header_size;
649 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
650 event_size, efi_tcg_event->event);
651out:
652 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200653}
654
655/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200656 * efi_tcg2_submit_command() - Send command to the TPM
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200657 *
658 * @this: TCG2 protocol instance
659 * @input_param_block_size: size of the TPM input parameter block
660 * @input_param_block: pointer to the TPM input parameter block
661 * @output_param_block_size: size of the TPM output parameter block
662 * @output_param_block: pointer to the TPM output parameter block
663 *
664 * Return: status code
665 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200666static efi_status_t EFIAPI
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900667efi_tcg2_submit_command(struct efi_tcg2_protocol *this,
668 u32 input_param_block_size,
669 u8 *input_param_block,
670 u32 output_param_block_size,
671 u8 *output_param_block)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200672{
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900673 struct udevice *dev;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300674 efi_status_t ret = EFI_SUCCESS;
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900675 u32 rc;
676 size_t resp_buf_size = output_param_block_size;
677
678 EFI_ENTRY("%p, %u, %p, %u, %p", this, input_param_block_size,
679 input_param_block, output_param_block_size, output_param_block);
680
681 if (!this || !input_param_block || !input_param_block_size) {
682 ret = EFI_INVALID_PARAMETER;
683 goto out;
684 }
685
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300686 if (tcg2_platform_get_tpm2(&dev)) {
687 ret = EFI_DEVICE_ERROR;
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900688 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300689 }
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900690
691 rc = tpm2_submit_command(dev, input_param_block,
692 output_param_block, &resp_buf_size);
693 if (rc) {
694 ret = (rc == -ENOSPC) ? EFI_OUT_OF_RESOURCES : EFI_DEVICE_ERROR;
695
696 goto out;
697 }
698
699out:
700 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200701}
702
703/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200704 * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200705 *
706 * @this: TCG2 protocol instance
707 * @active_pcr_banks: pointer for receiving the bitmap of currently
708 * active PCR banks
709 *
710 * Return: status code
711 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200712static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200713efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
714 u32 *active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200715{
Eddie James8ed7bb32023-10-24 10:43:49 -0500716 struct udevice *dev;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300717 efi_status_t ret = EFI_INVALID_PARAMETER;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200718
Ilias Apalodimas918a6ea2023-10-24 10:43:53 -0500719 EFI_ENTRY("%p, %p", this, active_pcr_banks);
720
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300721 if (!this || !active_pcr_banks)
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900722 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300723
724 if (tcg2_platform_get_tpm2(&dev))
725 goto out;
726
727 /*
728 * EFI_INVALID_PARAMETER does not convey the problem type.
729 * but that's what currently the spec specifies.
730 * EFI_DEVICE_ERROR would be better
731 */
732 if (tcg2_get_active_pcr_banks(dev, active_pcr_banks))
Eddie James8ed7bb32023-10-24 10:43:49 -0500733 goto out;
734
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300735 ret = EFI_SUCCESS;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200736
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900737out:
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200738 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200739}
740
741/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200742 * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200743 *
744 * @this: TCG2 protocol instance
745 * @active_pcr_banks: bitmap of the requested active PCR banks
746 *
747 * Return: status code
748 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200749static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300750efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
751 u32 __maybe_unused active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200752{
753 return EFI_UNSUPPORTED;
754}
755
756/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200757 * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous
758 * set_active_pcr_banks()
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200759 *
760 * @this: TCG2 protocol instance
761 * @operation_present: non-zero value to indicate a
762 * set_active_pcr_banks operation was
763 * invoked during last boot
764 * @response: result value could be returned
765 *
766 * Return: status code
767 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200768static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300769efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
770 u32 __maybe_unused *operation_present,
771 u32 __maybe_unused *response)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200772{
773 return EFI_UNSUPPORTED;
774}
775
776static const struct efi_tcg2_protocol efi_tcg2_protocol = {
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200777 .get_capability = efi_tcg2_get_capability,
778 .get_eventlog = efi_tcg2_get_eventlog,
779 .hash_log_extend_event = efi_tcg2_hash_log_extend_event,
780 .submit_command = efi_tcg2_submit_command,
781 .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks,
782 .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks,
783 .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks,
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200784};
785
786/**
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200787 * tcg2_uninit - remove the final event table and free efi memory on failures
788 */
Ilias Apalodimas14e7ed92024-10-30 22:40:59 +0200789static void tcg2_uninit(void)
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200790{
791 efi_status_t ret;
792
793 ret = efi_install_configuration_table(&efi_guid_final_events, NULL);
794 if (ret != EFI_SUCCESS)
795 log_err("Failed to delete final events config table\n");
796
797 efi_free_pool(event_log.buffer);
798 event_log.buffer = NULL;
799 efi_free_pool(event_log.final_buffer);
800 event_log.final_buffer = NULL;
Masahisa Kojima0fd43792021-12-07 14:15:31 +0900801
802 if (!is_tcg2_protocol_installed())
803 return;
804
Ilias Apalodimas4953c992023-06-19 14:14:02 +0300805 ret = efi_uninstall_multiple_protocol_interfaces(efi_root, &efi_guid_tcg2_protocol,
806 &efi_tcg2_protocol, NULL);
Masahisa Kojima0fd43792021-12-07 14:15:31 +0900807 if (ret != EFI_SUCCESS)
808 log_err("Failed to remove EFI TCG2 protocol\n");
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200809}
810
811/**
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200812 * create_final_event() - Create the final event and install the config
813 * defined by the TCG EFI spec
814 */
815static efi_status_t create_final_event(void)
816{
817 struct efi_tcg2_final_events_table *final_event;
818 efi_status_t ret;
819
820 /*
821 * All events generated after the invocation of
822 * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an
823 * EFI_CONFIGURATION_TABLE
824 */
825 ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE,
826 &event_log.final_buffer);
827 if (ret != EFI_SUCCESS)
828 goto out;
829
830 memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE);
831 final_event = event_log.final_buffer;
832 final_event->number_of_events = 0;
833 final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
834 event_log.final_pos = sizeof(*final_event);
835 ret = efi_install_configuration_table(&efi_guid_final_events,
836 final_event);
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +0300837 if (ret != EFI_SUCCESS) {
838 efi_free_pool(event_log.final_buffer);
839 event_log.final_buffer = NULL;
840 }
841
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200842out:
843 return ret;
844}
845
846/**
Eddie James8ed7bb32023-10-24 10:43:49 -0500847 * measure_event() - common function to add event log and extend PCR
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900848 *
849 * @dev: TPM device
850 * @pcr_index: PCR index
851 * @event_type: type of event added
852 * @size: event size
853 * @event: event data
854 *
855 * Return: status code
856 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500857static efi_status_t measure_event(struct udevice *dev, u32 pcr_index,
858 u32 event_type, u32 size, u8 event[])
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900859{
860 struct tpml_digest_values digest_list;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300861 efi_status_t ret = EFI_DEVICE_ERROR;
862 int rc;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900863
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300864 rc = tcg2_create_digest(dev, event, size, &digest_list);
865 if (rc)
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900866 goto out;
867
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300868 rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
869 if (rc)
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900870 goto out;
871
872 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
873 size, event);
874
875out:
876 return ret;
877}
878
879/**
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200880 * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the
881 * eventlog and extend the PCRs
882 *
883 * @dev: TPM device
884 *
885 * @Return: status code
886 */
887static efi_status_t efi_append_scrtm_version(struct udevice *dev)
888{
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200889 efi_status_t ret;
890
Eddie James8ed7bb32023-10-24 10:43:49 -0500891 ret = measure_event(dev, 0, EV_S_CRTM_VERSION,
892 strlen(version_string) + 1, (u8 *)version_string);
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200893
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530894 return ret;
895}
896
897/**
898 * efi_init_event_log() - initialize an eventlog
899 *
900 * Return: status code
901 */
902static efi_status_t efi_init_event_log(void)
903{
904 /*
905 * vendor_info_size is currently set to 0, we need to change the length
906 * and allocate the flexible array member if this changes
907 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500908 struct tcg2_event_log elog;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530909 struct udevice *dev;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530910 efi_status_t ret;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300911 int rc;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530912
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300913 if (tcg2_platform_get_tpm2(&dev))
914 return EFI_DEVICE_ERROR;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530915
916 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE,
917 (void **)&event_log.buffer);
918 if (ret != EFI_SUCCESS)
919 return ret;
920
921 /*
922 * initialize log area as 0xff so the OS can easily figure out the
923 * last log entry
924 */
925 memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE);
926
927 /*
928 * The log header is defined to be in SHA1 event log entry format.
929 * Setup event header
930 */
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530931 event_log.pos = 0;
932 event_log.last_event_size = 0;
933 event_log.get_event_called = false;
934 event_log.ebs_called = false;
935 event_log.truncated = false;
936
937 /*
938 * Check if earlier firmware have passed any eventlog. Different
939 * platforms can use different ways to do so.
940 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500941 elog.log = event_log.buffer;
942 elog.log_size = TPM2_EVENT_LOG_SIZE;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300943 rc = tcg2_log_prepare_buffer(dev, &elog, false);
944 if (rc) {
945 ret = (rc == -ENOBUFS) ? EFI_BUFFER_TOO_SMALL : EFI_DEVICE_ERROR;
Eddie James8ed7bb32023-10-24 10:43:49 -0500946 goto free_pool;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300947 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500948
949 event_log.pos = elog.log_position;
950
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530951 /*
Eddie James8ed7bb32023-10-24 10:43:49 -0500952 * Add SCRTM version to the log if previous firmmware
953 * doesn't pass an eventlog.
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530954 */
Ilias Apalodimas12c15f52023-11-07 13:31:34 +0200955 if (!elog.found) {
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530956 ret = efi_append_scrtm_version(dev);
Ilias Apalodimas12c15f52023-11-07 13:31:34 +0200957 if (ret != EFI_SUCCESS)
958 goto free_pool;
959 }
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530960
961 ret = create_final_event();
962 if (ret != EFI_SUCCESS)
963 goto free_pool;
964
965 return ret;
966
967free_pool:
968 efi_free_pool(event_log.buffer);
969 event_log.buffer = NULL;
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200970 return ret;
971}
972
973/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900974 * tcg2_measure_variable() - add variable event log and extend PCR
975 *
976 * @dev: TPM device
977 * @pcr_index: PCR index
978 * @event_type: type of event added
979 * @var_name: variable name
980 * @guid: guid
981 * @data_size: variable data size
982 * @data: variable data
983 *
984 * Return: status code
985 */
986static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index,
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +0200987 u32 event_type, const u16 *var_name,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900988 const efi_guid_t *guid,
989 efi_uintn_t data_size, u8 *data)
990{
991 u32 event_size;
992 efi_status_t ret;
993 struct efi_tcg2_uefi_variable_data *event;
994
995 event_size = sizeof(event->variable_name) +
996 sizeof(event->unicode_name_length) +
997 sizeof(event->variable_data_length) +
998 (u16_strlen(var_name) * sizeof(u16)) + data_size;
999 event = malloc(event_size);
1000 if (!event)
1001 return EFI_OUT_OF_RESOURCES;
1002
1003 guidcpy(&event->variable_name, guid);
1004 event->unicode_name_length = u16_strlen(var_name);
1005 event->variable_data_length = data_size;
1006 memcpy(event->unicode_name, var_name,
1007 (event->unicode_name_length * sizeof(u16)));
1008 if (data) {
1009 memcpy((u16 *)event->unicode_name + event->unicode_name_length,
1010 data, data_size);
1011 }
Eddie James8ed7bb32023-10-24 10:43:49 -05001012 ret = measure_event(dev, pcr_index, event_type, event_size,
1013 (u8 *)event);
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001014 free(event);
1015 return ret;
1016}
1017
1018/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001019 * tcg2_measure_boot_variable() - measure boot variables
1020 *
1021 * @dev: TPM device
1022 *
1023 * Return: status code
1024 */
1025static efi_status_t tcg2_measure_boot_variable(struct udevice *dev)
1026{
1027 u16 *boot_order;
1028 u16 *boot_index;
Simon Glass90975372022-01-23 12:55:12 -07001029 u16 var_name[] = u"BootOrder";
1030 u16 boot_name[] = u"Boot####";
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001031 u8 *bootvar;
1032 efi_uintn_t var_data_size;
1033 u32 count, i;
1034 efi_status_t ret;
1035
1036 boot_order = efi_get_var(var_name, &efi_global_variable_guid,
1037 &var_data_size);
1038 if (!boot_order) {
Masahisa Kojimad1325932021-11-09 18:44:54 +09001039 /* If "BootOrder" is not defined, skip the boot variable measurement */
1040 return EFI_SUCCESS;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001041 }
1042
1043 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name,
1044 &efi_global_variable_guid, var_data_size,
1045 (u8 *)boot_order);
1046 if (ret != EFI_SUCCESS)
1047 goto error;
1048
1049 count = var_data_size / sizeof(*boot_order);
1050 boot_index = boot_order;
1051 for (i = 0; i < count; i++) {
1052 efi_create_indexed_name(boot_name, sizeof(boot_name),
1053 "Boot", *boot_index++);
1054
1055 bootvar = efi_get_var(boot_name, &efi_global_variable_guid,
1056 &var_data_size);
1057
1058 if (!bootvar) {
Masahisa Kojimaaca20c82021-11-09 20:35:53 +09001059 log_debug("%ls not found\n", boot_name);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001060 continue;
1061 }
1062
1063 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2,
1064 boot_name,
1065 &efi_global_variable_guid,
1066 var_data_size, bootvar);
1067 free(bootvar);
1068 if (ret != EFI_SUCCESS)
1069 goto error;
1070 }
1071
1072error:
1073 free(boot_order);
1074 return ret;
1075}
1076
1077/**
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001078 * tcg2_measure_smbios() - measure smbios table
1079 *
1080 * @dev: TPM device
1081 * @entry: pointer to the smbios_entry structure
1082 *
1083 * Return: status code
1084 */
1085static efi_status_t
1086tcg2_measure_smbios(struct udevice *dev,
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001087 const struct smbios3_entry *entry)
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001088{
1089 efi_status_t ret;
1090 struct smbios_header *smbios_copy;
1091 struct smbios_handoff_table_pointers2 *event = NULL;
1092 u32 event_size;
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001093 const char smbios3_anchor[] = "_SM3_";
1094
1095 /* We only support SMBIOS 3.0 Entry Point structure */
1096 if (memcmp(entry->anchor, smbios3_anchor, sizeof(smbios3_anchor) - 1))
1097 return EFI_UNSUPPORTED;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001098
1099 /*
1100 * TCG PC Client PFP Spec says
1101 * "SMBIOS structures that contain static configuration information
1102 * (e.g. Platform Manufacturer Enterprise Number assigned by IANA,
1103 * platform model number, Vendor and Device IDs for each SMBIOS table)
1104 * that is relevant to the security of the platform MUST be measured".
1105 * Device dependent parameters such as serial number are cleared to
1106 * zero or spaces for the measurement.
1107 */
1108 event_size = sizeof(struct smbios_handoff_table_pointers2) +
1109 FIELD_SIZEOF(struct efi_configuration_table, guid) +
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +01001110 entry->table_maximum_size;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001111 event = calloc(1, event_size);
1112 if (!event) {
1113 ret = EFI_OUT_OF_RESOURCES;
1114 goto out;
1115 }
1116
1117 event->table_description_size = sizeof(SMBIOS_HANDOFF_TABLE_DESC);
1118 memcpy(event->table_description, SMBIOS_HANDOFF_TABLE_DESC,
1119 sizeof(SMBIOS_HANDOFF_TABLE_DESC));
1120 put_unaligned_le64(1, &event->number_of_tables);
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001121 guidcpy(&event->table_entry[0].guid, &smbios3_guid);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001122 smbios_copy = (struct smbios_header *)((uintptr_t)&event->table_entry[0].table);
1123 memcpy(&event->table_entry[0].table,
1124 (void *)((uintptr_t)entry->struct_table_address),
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +01001125 entry->table_maximum_size);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001126
1127 smbios_prepare_measurement(entry, smbios_copy);
1128
Eddie James8ed7bb32023-10-24 10:43:49 -05001129 ret = measure_event(dev, 1, EV_EFI_HANDOFF_TABLES2, event_size,
1130 (u8 *)event);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001131 if (ret != EFI_SUCCESS)
1132 goto out;
1133
1134out:
1135 free(event);
1136
1137 return ret;
1138}
1139
1140/**
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001141 * tcg2_measure_gpt_table() - measure gpt table
1142 *
1143 * @dev: TPM device
1144 * @loaded_image: handle to the loaded image
1145 *
1146 * Return: status code
1147 */
1148static efi_status_t
1149tcg2_measure_gpt_data(struct udevice *dev,
1150 struct efi_loaded_image_obj *loaded_image)
1151{
1152 efi_status_t ret;
1153 efi_handle_t handle;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001154 struct efi_handler *dp_handler, *io_handler;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001155 struct efi_device_path *orig_device_path;
1156 struct efi_device_path *device_path;
1157 struct efi_device_path *dp;
1158 struct efi_block_io *block_io;
1159 struct efi_gpt_data *event = NULL;
1160 efi_guid_t null_guid = NULL_GUID;
1161 gpt_header *gpt_h;
1162 gpt_entry *entry = NULL;
1163 gpt_entry *gpt_e;
1164 u32 num_of_valid_entry = 0;
1165 u32 event_size;
1166 u32 i;
1167 u32 total_gpt_entry_size;
1168
1169 ret = efi_search_protocol(&loaded_image->header,
1170 &efi_guid_loaded_image_device_path,
1171 &dp_handler);
1172 if (ret != EFI_SUCCESS)
1173 return ret;
1174
1175 orig_device_path = dp_handler->protocol_interface;
1176 if (!orig_device_path) /* no device path, skip GPT measurement */
1177 return EFI_SUCCESS;
1178
1179 device_path = efi_dp_dup(orig_device_path);
1180 if (!device_path)
1181 return EFI_OUT_OF_RESOURCES;
1182
1183 dp = search_gpt_dp_node(device_path);
1184 if (!dp) {
1185 /* no GPT device path node found, skip GPT measurement */
1186 ret = EFI_SUCCESS;
1187 goto out1;
1188 }
1189
1190 /* read GPT header */
1191 dp->type = DEVICE_PATH_TYPE_END;
1192 dp->sub_type = DEVICE_PATH_SUB_TYPE_END;
1193 dp = device_path;
1194 ret = EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_guid,
1195 &dp, &handle));
1196 if (ret != EFI_SUCCESS)
1197 goto out1;
1198
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001199 ret = efi_search_protocol(handle, &efi_block_io_guid, &io_handler);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001200 if (ret != EFI_SUCCESS)
1201 goto out1;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001202 block_io = io_handler->protocol_interface;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001203
1204 gpt_h = memalign(block_io->media->io_align, block_io->media->block_size);
1205 if (!gpt_h) {
1206 ret = EFI_OUT_OF_RESOURCES;
1207 goto out2;
1208 }
1209
1210 ret = block_io->read_blocks(block_io, block_io->media->media_id, 1,
1211 block_io->media->block_size, gpt_h);
1212 if (ret != EFI_SUCCESS)
1213 goto out2;
1214
1215 /* read GPT entry */
1216 total_gpt_entry_size = gpt_h->num_partition_entries *
1217 gpt_h->sizeof_partition_entry;
1218 entry = memalign(block_io->media->io_align, total_gpt_entry_size);
1219 if (!entry) {
1220 ret = EFI_OUT_OF_RESOURCES;
1221 goto out2;
1222 }
1223
1224 ret = block_io->read_blocks(block_io, block_io->media->media_id,
1225 gpt_h->partition_entry_lba,
1226 total_gpt_entry_size, entry);
1227 if (ret != EFI_SUCCESS)
1228 goto out2;
1229
1230 /* count valid GPT entry */
1231 gpt_e = entry;
1232 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1233 if (guidcmp(&null_guid, &gpt_e->partition_type_guid))
1234 num_of_valid_entry++;
1235
1236 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1237 }
1238
1239 /* prepare event data for measurement */
1240 event_size = sizeof(struct efi_gpt_data) +
1241 (num_of_valid_entry * gpt_h->sizeof_partition_entry);
1242 event = calloc(1, event_size);
1243 if (!event) {
1244 ret = EFI_OUT_OF_RESOURCES;
1245 goto out2;
1246 }
1247 memcpy(event, gpt_h, sizeof(gpt_header));
1248 put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions);
1249
1250 /* copy valid GPT entry */
1251 gpt_e = entry;
1252 num_of_valid_entry = 0;
1253 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1254 if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) {
1255 memcpy((u8 *)event->partitions +
1256 (num_of_valid_entry * gpt_h->sizeof_partition_entry),
1257 gpt_e, gpt_h->sizeof_partition_entry);
1258 num_of_valid_entry++;
1259 }
1260
1261 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1262 }
1263
Eddie James8ed7bb32023-10-24 10:43:49 -05001264 ret = measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *)event);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001265
1266out2:
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001267 free(gpt_h);
1268 free(entry);
1269 free(event);
1270out1:
1271 efi_free_pool(device_path);
1272
1273 return ret;
1274}
1275
Etienne Carriereb9064352023-02-16 17:29:48 +01001276/* Return the byte size of reserved map area in DTB or -1 upon error */
1277static ssize_t size_of_rsvmap(void *dtb)
1278{
1279 struct fdt_reserve_entry e;
1280 ssize_t size_max;
1281 ssize_t size;
1282 u8 *rsvmap_base;
1283
1284 rsvmap_base = (u8 *)dtb + fdt_off_mem_rsvmap(dtb);
1285 size_max = fdt_totalsize(dtb) - fdt_off_mem_rsvmap(dtb);
1286 size = 0;
1287
1288 do {
1289 memcpy(&e, rsvmap_base + size, sizeof(e));
1290 size += sizeof(e);
1291 if (size > size_max)
1292 return -1;
1293 } while (e.size);
1294
1295 return size;
1296}
1297
1298/**
1299 * efi_tcg2_measure_dtb() - measure DTB passed to the OS
1300 *
1301 * @dtb: pointer to the device tree blob
1302 *
1303 * Return: status code
1304 */
1305efi_status_t efi_tcg2_measure_dtb(void *dtb)
1306{
1307 struct uefi_platform_firmware_blob2 *blob;
1308 struct fdt_header *header;
1309 sha256_context hash_ctx;
1310 struct udevice *dev;
1311 ssize_t rsvmap_size;
1312 efi_status_t ret;
1313 u32 event_size;
1314
1315 if (!is_tcg2_protocol_installed())
1316 return EFI_SUCCESS;
1317
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001318 if (tcg2_platform_get_tpm2(&dev))
Etienne Carriereb9064352023-02-16 17:29:48 +01001319 return EFI_SECURITY_VIOLATION;
1320
1321 rsvmap_size = size_of_rsvmap(dtb);
1322 if (rsvmap_size < 0)
1323 return EFI_SECURITY_VIOLATION;
1324
1325 event_size = sizeof(*blob) + sizeof(EFI_DTB_EVENT_STRING) + SHA256_SUM_LEN;
1326 blob = calloc(1, event_size);
1327 if (!blob)
1328 return EFI_OUT_OF_RESOURCES;
1329
1330 blob->blob_description_size = sizeof(EFI_DTB_EVENT_STRING);
1331 memcpy(blob->data, EFI_DTB_EVENT_STRING, blob->blob_description_size);
1332
1333 /* Measure populated areas of the DTB */
1334 header = dtb;
1335 sha256_starts(&hash_ctx);
1336 sha256_update(&hash_ctx, (u8 *)header, sizeof(struct fdt_header));
1337 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_struct(dtb), fdt_size_dt_strings(dtb));
1338 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_strings(dtb), fdt_size_dt_struct(dtb));
1339 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_mem_rsvmap(dtb), rsvmap_size);
1340 sha256_finish(&hash_ctx, blob->data + blob->blob_description_size);
1341
Ilias Apalodimasc90c1952024-06-14 15:09:50 +03001342 ret = measure_event(dev, 1, EV_POST_CODE, event_size, (u8 *)blob);
Etienne Carriereb9064352023-02-16 17:29:48 +01001343
1344 free(blob);
1345 return ret;
1346}
1347
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001348/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001349 * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation
1350 *
1351 * Return: status code
1352 */
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001353efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_image_obj *handle)
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001354{
1355 efi_status_t ret;
1356 u32 pcr_index;
1357 struct udevice *dev;
1358 u32 event = 0;
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001359 struct smbios3_entry *entry;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001360
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001361 if (!is_tcg2_protocol_installed())
1362 return EFI_SUCCESS;
1363
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001364 if (tcg2_efi_app_invoked)
1365 return EFI_SUCCESS;
1366
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001367 if (tcg2_platform_get_tpm2(&dev))
Masahisa Kojima38155ea2021-12-07 14:15:33 +09001368 return EFI_SECURITY_VIOLATION;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001369
1370 ret = tcg2_measure_boot_variable(dev);
1371 if (ret != EFI_SUCCESS)
1372 goto out;
1373
Eddie James8ed7bb32023-10-24 10:43:49 -05001374 ret = measure_event(dev, 4, EV_EFI_ACTION,
1375 strlen(EFI_CALLING_EFI_APPLICATION),
1376 (u8 *)EFI_CALLING_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001377 if (ret != EFI_SUCCESS)
1378 goto out;
1379
Heinrich Schuchardt10899c82024-01-26 09:13:22 +01001380 entry = efi_get_configuration_table(&smbios3_guid);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001381 if (entry) {
1382 ret = tcg2_measure_smbios(dev, entry);
1383 if (ret != EFI_SUCCESS)
1384 goto out;
1385 }
1386
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001387 ret = tcg2_measure_gpt_data(dev, handle);
1388 if (ret != EFI_SUCCESS)
1389 goto out;
1390
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001391 for (pcr_index = 0; pcr_index <= 7; pcr_index++) {
Eddie James8ed7bb32023-10-24 10:43:49 -05001392 ret = measure_event(dev, pcr_index, EV_SEPARATOR,
1393 sizeof(event), (u8 *)&event);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001394 if (ret != EFI_SUCCESS)
1395 goto out;
1396 }
1397
1398 tcg2_efi_app_invoked = true;
1399out:
1400 return ret;
1401}
1402
1403/**
1404 * efi_tcg2_measure_efi_app_exit() - measure efi app exit
1405 *
1406 * Return: status code
1407 */
1408efi_status_t efi_tcg2_measure_efi_app_exit(void)
1409{
1410 efi_status_t ret;
1411 struct udevice *dev;
1412
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001413 if (!is_tcg2_protocol_installed())
1414 return EFI_SUCCESS;
1415
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001416 if (tcg2_platform_get_tpm2(&dev))
1417 return EFI_SECURITY_VIOLATION;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001418
Eddie James8ed7bb32023-10-24 10:43:49 -05001419 ret = measure_event(dev, 4, EV_EFI_ACTION,
1420 strlen(EFI_RETURNING_FROM_EFI_APPLICATION),
1421 (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001422 return ret;
1423}
1424
1425/**
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001426 * efi_tcg2_notify_exit_boot_services() - ExitBootService callback
1427 *
1428 * @event: callback event
1429 * @context: callback context
1430 */
1431static void EFIAPI
1432efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context)
1433{
1434 efi_status_t ret;
1435 struct udevice *dev;
1436
1437 EFI_ENTRY("%p, %p", event, context);
1438
Ilias Apalodimas24e841a2021-11-18 09:03:39 +02001439 event_log.ebs_called = true;
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001440
1441 if (!is_tcg2_protocol_installed()) {
1442 ret = EFI_SUCCESS;
1443 goto out;
1444 }
1445
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001446 if (tcg2_platform_get_tpm2(&dev)) {
1447 ret = EFI_SECURITY_VIOLATION;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001448 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001449 }
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001450
Eddie James8ed7bb32023-10-24 10:43:49 -05001451 ret = measure_event(dev, 5, EV_EFI_ACTION,
1452 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1453 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001454 if (ret != EFI_SUCCESS)
1455 goto out;
1456
Eddie James8ed7bb32023-10-24 10:43:49 -05001457 ret = measure_event(dev, 5, EV_EFI_ACTION,
1458 strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED),
1459 (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001460
1461out:
1462 EFI_EXIT(ret);
1463}
1464
1465/**
1466 * efi_tcg2_notify_exit_boot_services_failed()
1467 * - notify ExitBootServices() is failed
1468 *
1469 * Return: status code
1470 */
1471efi_status_t efi_tcg2_notify_exit_boot_services_failed(void)
1472{
1473 struct udevice *dev;
1474 efi_status_t ret;
1475
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001476 if (!is_tcg2_protocol_installed())
1477 return EFI_SUCCESS;
1478
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001479 if (tcg2_platform_get_tpm2(&dev))
1480 return EFI_SECURITY_VIOLATION;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001481
Eddie James8ed7bb32023-10-24 10:43:49 -05001482 ret = measure_event(dev, 5, EV_EFI_ACTION,
1483 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1484 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001485 if (ret != EFI_SUCCESS)
1486 goto out;
1487
Eddie James8ed7bb32023-10-24 10:43:49 -05001488 ret = measure_event(dev, 5, EV_EFI_ACTION,
1489 strlen(EFI_EXIT_BOOT_SERVICES_FAILED),
1490 (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001491
1492out:
1493 return ret;
1494}
1495
1496/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001497 * tcg2_measure_secure_boot_variable() - measure secure boot variables
1498 *
1499 * @dev: TPM device
1500 *
1501 * Return: status code
1502 */
1503static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev)
1504{
1505 u8 *data;
1506 efi_uintn_t data_size;
1507 u32 count, i;
1508 efi_status_t ret;
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001509 u8 deployed_mode;
1510 efi_uintn_t size;
1511 u32 deployed_audit_pcr_index = 1;
1512
1513 size = sizeof(deployed_mode);
1514 ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid,
1515 NULL, &size, &deployed_mode, NULL);
1516 if (ret != EFI_SUCCESS || !deployed_mode)
1517 deployed_audit_pcr_index = 7;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001518
1519 count = ARRAY_SIZE(secure_variables);
1520 for (i = 0; i < count; i++) {
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001521 const efi_guid_t *guid;
1522
Masahisa Kojima21684522021-10-26 17:27:26 +09001523 guid = efi_auth_var_get_guid(secure_variables[i].name);
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001524
Masahisa Kojima21684522021-10-26 17:27:26 +09001525 data = efi_get_var(secure_variables[i].name, guid, &data_size);
1526 if (!data && !secure_variables[i].accept_empty)
1527 continue;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001528
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001529 if (u16_strcmp(u"DeployedMode", secure_variables[i].name))
1530 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1531 if (u16_strcmp(u"AuditMode", secure_variables[i].name))
1532 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1533
1534 ret = tcg2_measure_variable(dev, secure_variables[i].pcr_index,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001535 EV_EFI_VARIABLE_DRIVER_CONFIG,
Masahisa Kojima21684522021-10-26 17:27:26 +09001536 secure_variables[i].name, guid,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001537 data_size, data);
1538 free(data);
1539 if (ret != EFI_SUCCESS)
1540 goto error;
1541 }
1542
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001543error:
1544 return ret;
1545}
1546
1547/**
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001548 * efi_tcg2_do_initial_measurement() - do initial measurement
1549 *
1550 * Return: status code
1551 */
1552efi_status_t efi_tcg2_do_initial_measurement(void)
1553{
1554 efi_status_t ret;
1555 struct udevice *dev;
1556
1557 if (!is_tcg2_protocol_installed())
1558 return EFI_SUCCESS;
1559
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001560 if (tcg2_platform_get_tpm2(&dev))
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001561 return EFI_SECURITY_VIOLATION;
1562
1563 ret = tcg2_measure_secure_boot_variable(dev);
1564 if (ret != EFI_SUCCESS)
1565 goto out;
1566
1567out:
1568 return ret;
1569}
1570
1571/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001572 * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
1573 *
1574 * If a TPM2 device is available, the TPM TCG2 Protocol is registered
1575 *
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001576 * Return: status code
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001577 */
1578efi_status_t efi_tcg2_register(void)
1579{
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001580 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001581 struct udevice *dev;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001582 struct efi_event *event;
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001583 u32 err;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001584
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001585 if (tcg2_platform_get_tpm2(&dev)) {
Ilias Apalodimaseb1b6b42023-01-19 16:29:15 +02001586 log_warning("Missing TPMv2 device for EFI_TCG_PROTOCOL\n");
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001587 return EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001588 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001589
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001590 /* initialize the TPM as early as possible. */
Ilias Apalodimas0c95d222023-01-25 13:06:03 +02001591 err = tpm_auto_start(dev);
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001592 if (err) {
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001593 ret = EFI_DEVICE_ERROR;
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001594 log_err("TPM startup failed\n");
1595 goto fail;
1596 }
1597
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001598 ret = efi_init_event_log();
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001599 if (ret != EFI_SUCCESS) {
1600 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001601 goto fail;
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001602 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001603
Ilias Apalodimas4953c992023-06-19 14:14:02 +03001604 ret = efi_install_multiple_protocol_interfaces(&efi_root, &efi_guid_tcg2_protocol,
1605 &efi_tcg2_protocol, NULL);
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001606 if (ret != EFI_SUCCESS) {
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001607 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001608 goto fail;
1609 }
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001610
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001611 ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
1612 efi_tcg2_notify_exit_boot_services, NULL,
1613 NULL, &event);
1614 if (ret != EFI_SUCCESS) {
1615 tcg2_uninit();
1616 goto fail;
1617 }
1618
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001619 return ret;
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001620
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001621fail:
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001622 log_err("Cannot install EFI_TCG2_PROTOCOL\n");
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001623 return ret;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001624}