blob: 45f451ef6b6dcf9ceab809f0aeb0b4792b5b070a [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 IMAGE_NT_HEADERS32 *nt;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200611
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900612 ret = efi_check_pe((void *)(uintptr_t)data_to_hash,
613 data_to_hash_len, (void **)&nt);
614 if (ret != EFI_SUCCESS) {
615 log_err("Not a valid PE-COFF file\n");
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900616 ret = EFI_UNSUPPORTED;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900617 goto out;
618 }
619 ret = tcg2_hash_pe_image((void *)(uintptr_t)data_to_hash,
620 data_to_hash_len, &digest_list);
621 } else {
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300622 rc = tcg2_create_digest(dev, (u8 *)(uintptr_t)data_to_hash,
623 data_to_hash_len, &digest_list);
624 if (rc)
625 ret = EFI_DEVICE_ERROR;
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900626 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200627
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200628 if (ret != EFI_SUCCESS)
629 goto out;
630
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900631 pcr_index = efi_tcg_event->header.pcr_index;
632 event_type = efi_tcg_event->header.event_type;
633
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300634 rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
635 if (rc) {
636 ret = EFI_DEVICE_ERROR;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200637 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300638 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200639
640 if (flags & EFI_TCG2_EXTEND_ONLY) {
641 if (event_log.truncated)
642 ret = EFI_VOLUME_FULL;
643 goto out;
644 }
645
646 /*
647 * The efi_tcg_event size includes the size component and the
648 * headersize
649 */
650 event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) -
651 efi_tcg_event->header.header_size;
652 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
653 event_size, efi_tcg_event->event);
654out:
655 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200656}
657
658/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200659 * efi_tcg2_submit_command() - Send command to the TPM
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200660 *
661 * @this: TCG2 protocol instance
662 * @input_param_block_size: size of the TPM input parameter block
663 * @input_param_block: pointer to the TPM input parameter block
664 * @output_param_block_size: size of the TPM output parameter block
665 * @output_param_block: pointer to the TPM output parameter block
666 *
667 * Return: status code
668 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200669static efi_status_t EFIAPI
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900670efi_tcg2_submit_command(struct efi_tcg2_protocol *this,
671 u32 input_param_block_size,
672 u8 *input_param_block,
673 u32 output_param_block_size,
674 u8 *output_param_block)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200675{
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900676 struct udevice *dev;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300677 efi_status_t ret = EFI_SUCCESS;
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900678 u32 rc;
679 size_t resp_buf_size = output_param_block_size;
680
681 EFI_ENTRY("%p, %u, %p, %u, %p", this, input_param_block_size,
682 input_param_block, output_param_block_size, output_param_block);
683
684 if (!this || !input_param_block || !input_param_block_size) {
685 ret = EFI_INVALID_PARAMETER;
686 goto out;
687 }
688
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300689 if (tcg2_platform_get_tpm2(&dev)) {
690 ret = EFI_DEVICE_ERROR;
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900691 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300692 }
Masahisa Kojima06ef6b62021-11-04 22:59:16 +0900693
694 rc = tpm2_submit_command(dev, input_param_block,
695 output_param_block, &resp_buf_size);
696 if (rc) {
697 ret = (rc == -ENOSPC) ? EFI_OUT_OF_RESOURCES : EFI_DEVICE_ERROR;
698
699 goto out;
700 }
701
702out:
703 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200704}
705
706/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200707 * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200708 *
709 * @this: TCG2 protocol instance
710 * @active_pcr_banks: pointer for receiving the bitmap of currently
711 * active PCR banks
712 *
713 * Return: status code
714 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200715static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200716efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
717 u32 *active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200718{
Eddie James8ed7bb32023-10-24 10:43:49 -0500719 struct udevice *dev;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300720 efi_status_t ret = EFI_INVALID_PARAMETER;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200721
Ilias Apalodimas918a6ea2023-10-24 10:43:53 -0500722 EFI_ENTRY("%p, %p", this, active_pcr_banks);
723
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300724 if (!this || !active_pcr_banks)
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900725 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300726
727 if (tcg2_platform_get_tpm2(&dev))
728 goto out;
729
730 /*
731 * EFI_INVALID_PARAMETER does not convey the problem type.
732 * but that's what currently the spec specifies.
733 * EFI_DEVICE_ERROR would be better
734 */
735 if (tcg2_get_active_pcr_banks(dev, active_pcr_banks))
Eddie James8ed7bb32023-10-24 10:43:49 -0500736 goto out;
737
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300738 ret = EFI_SUCCESS;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200739
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900740out:
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200741 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200742}
743
744/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200745 * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200746 *
747 * @this: TCG2 protocol instance
748 * @active_pcr_banks: bitmap of the requested active PCR banks
749 *
750 * Return: status code
751 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200752static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300753efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
754 u32 __maybe_unused active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200755{
756 return EFI_UNSUPPORTED;
757}
758
759/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200760 * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous
761 * set_active_pcr_banks()
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200762 *
763 * @this: TCG2 protocol instance
764 * @operation_present: non-zero value to indicate a
765 * set_active_pcr_banks operation was
766 * invoked during last boot
767 * @response: result value could be returned
768 *
769 * Return: status code
770 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200771static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300772efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
773 u32 __maybe_unused *operation_present,
774 u32 __maybe_unused *response)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200775{
776 return EFI_UNSUPPORTED;
777}
778
779static const struct efi_tcg2_protocol efi_tcg2_protocol = {
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200780 .get_capability = efi_tcg2_get_capability,
781 .get_eventlog = efi_tcg2_get_eventlog,
782 .hash_log_extend_event = efi_tcg2_hash_log_extend_event,
783 .submit_command = efi_tcg2_submit_command,
784 .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks,
785 .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks,
786 .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks,
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200787};
788
789/**
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200790 * tcg2_uninit - remove the final event table and free efi memory on failures
791 */
792void tcg2_uninit(void)
793{
794 efi_status_t ret;
795
796 ret = efi_install_configuration_table(&efi_guid_final_events, NULL);
797 if (ret != EFI_SUCCESS)
798 log_err("Failed to delete final events config table\n");
799
800 efi_free_pool(event_log.buffer);
801 event_log.buffer = NULL;
802 efi_free_pool(event_log.final_buffer);
803 event_log.final_buffer = NULL;
Masahisa Kojima0fd43792021-12-07 14:15:31 +0900804
805 if (!is_tcg2_protocol_installed())
806 return;
807
Ilias Apalodimas4953c992023-06-19 14:14:02 +0300808 ret = efi_uninstall_multiple_protocol_interfaces(efi_root, &efi_guid_tcg2_protocol,
809 &efi_tcg2_protocol, NULL);
Masahisa Kojima0fd43792021-12-07 14:15:31 +0900810 if (ret != EFI_SUCCESS)
811 log_err("Failed to remove EFI TCG2 protocol\n");
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200812}
813
814/**
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200815 * create_final_event() - Create the final event and install the config
816 * defined by the TCG EFI spec
817 */
818static efi_status_t create_final_event(void)
819{
820 struct efi_tcg2_final_events_table *final_event;
821 efi_status_t ret;
822
823 /*
824 * All events generated after the invocation of
825 * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an
826 * EFI_CONFIGURATION_TABLE
827 */
828 ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE,
829 &event_log.final_buffer);
830 if (ret != EFI_SUCCESS)
831 goto out;
832
833 memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE);
834 final_event = event_log.final_buffer;
835 final_event->number_of_events = 0;
836 final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
837 event_log.final_pos = sizeof(*final_event);
838 ret = efi_install_configuration_table(&efi_guid_final_events,
839 final_event);
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +0300840 if (ret != EFI_SUCCESS) {
841 efi_free_pool(event_log.final_buffer);
842 event_log.final_buffer = NULL;
843 }
844
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200845out:
846 return ret;
847}
848
849/**
Eddie James8ed7bb32023-10-24 10:43:49 -0500850 * measure_event() - common function to add event log and extend PCR
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900851 *
852 * @dev: TPM device
853 * @pcr_index: PCR index
854 * @event_type: type of event added
855 * @size: event size
856 * @event: event data
857 *
858 * Return: status code
859 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500860static efi_status_t measure_event(struct udevice *dev, u32 pcr_index,
861 u32 event_type, u32 size, u8 event[])
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900862{
863 struct tpml_digest_values digest_list;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300864 efi_status_t ret = EFI_DEVICE_ERROR;
865 int rc;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900866
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300867 rc = tcg2_create_digest(dev, event, size, &digest_list);
868 if (rc)
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900869 goto out;
870
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300871 rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
872 if (rc)
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900873 goto out;
874
875 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
876 size, event);
877
878out:
879 return ret;
880}
881
882/**
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200883 * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the
884 * eventlog and extend the PCRs
885 *
886 * @dev: TPM device
887 *
888 * @Return: status code
889 */
890static efi_status_t efi_append_scrtm_version(struct udevice *dev)
891{
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200892 efi_status_t ret;
893
Eddie James8ed7bb32023-10-24 10:43:49 -0500894 ret = measure_event(dev, 0, EV_S_CRTM_VERSION,
895 strlen(version_string) + 1, (u8 *)version_string);
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200896
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530897 return ret;
898}
899
900/**
901 * efi_init_event_log() - initialize an eventlog
902 *
903 * Return: status code
904 */
905static efi_status_t efi_init_event_log(void)
906{
907 /*
908 * vendor_info_size is currently set to 0, we need to change the length
909 * and allocate the flexible array member if this changes
910 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500911 struct tcg2_event_log elog;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530912 struct udevice *dev;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530913 efi_status_t ret;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300914 int rc;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530915
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300916 if (tcg2_platform_get_tpm2(&dev))
917 return EFI_DEVICE_ERROR;
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530918
919 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE,
920 (void **)&event_log.buffer);
921 if (ret != EFI_SUCCESS)
922 return ret;
923
924 /*
925 * initialize log area as 0xff so the OS can easily figure out the
926 * last log entry
927 */
928 memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE);
929
930 /*
931 * The log header is defined to be in SHA1 event log entry format.
932 * Setup event header
933 */
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530934 event_log.pos = 0;
935 event_log.last_event_size = 0;
936 event_log.get_event_called = false;
937 event_log.ebs_called = false;
938 event_log.truncated = false;
939
940 /*
941 * Check if earlier firmware have passed any eventlog. Different
942 * platforms can use different ways to do so.
943 */
Eddie James8ed7bb32023-10-24 10:43:49 -0500944 elog.log = event_log.buffer;
945 elog.log_size = TPM2_EVENT_LOG_SIZE;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300946 rc = tcg2_log_prepare_buffer(dev, &elog, false);
947 if (rc) {
948 ret = (rc == -ENOBUFS) ? EFI_BUFFER_TOO_SMALL : EFI_DEVICE_ERROR;
Eddie James8ed7bb32023-10-24 10:43:49 -0500949 goto free_pool;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +0300950 }
Eddie James8ed7bb32023-10-24 10:43:49 -0500951
952 event_log.pos = elog.log_position;
953
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530954 /*
Eddie James8ed7bb32023-10-24 10:43:49 -0500955 * Add SCRTM version to the log if previous firmmware
956 * doesn't pass an eventlog.
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530957 */
Ilias Apalodimas12c15f52023-11-07 13:31:34 +0200958 if (!elog.found) {
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530959 ret = efi_append_scrtm_version(dev);
Ilias Apalodimas12c15f52023-11-07 13:31:34 +0200960 if (ret != EFI_SUCCESS)
961 goto free_pool;
962 }
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530963
964 ret = create_final_event();
965 if (ret != EFI_SUCCESS)
966 goto free_pool;
967
968 return ret;
969
970free_pool:
971 efi_free_pool(event_log.buffer);
972 event_log.buffer = NULL;
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +0200973 return ret;
974}
975
976/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900977 * tcg2_measure_variable() - add variable event log and extend PCR
978 *
979 * @dev: TPM device
980 * @pcr_index: PCR index
981 * @event_type: type of event added
982 * @var_name: variable name
983 * @guid: guid
984 * @data_size: variable data size
985 * @data: variable data
986 *
987 * Return: status code
988 */
989static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index,
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +0200990 u32 event_type, const u16 *var_name,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900991 const efi_guid_t *guid,
992 efi_uintn_t data_size, u8 *data)
993{
994 u32 event_size;
995 efi_status_t ret;
996 struct efi_tcg2_uefi_variable_data *event;
997
998 event_size = sizeof(event->variable_name) +
999 sizeof(event->unicode_name_length) +
1000 sizeof(event->variable_data_length) +
1001 (u16_strlen(var_name) * sizeof(u16)) + data_size;
1002 event = malloc(event_size);
1003 if (!event)
1004 return EFI_OUT_OF_RESOURCES;
1005
1006 guidcpy(&event->variable_name, guid);
1007 event->unicode_name_length = u16_strlen(var_name);
1008 event->variable_data_length = data_size;
1009 memcpy(event->unicode_name, var_name,
1010 (event->unicode_name_length * sizeof(u16)));
1011 if (data) {
1012 memcpy((u16 *)event->unicode_name + event->unicode_name_length,
1013 data, data_size);
1014 }
Eddie James8ed7bb32023-10-24 10:43:49 -05001015 ret = measure_event(dev, pcr_index, event_type, event_size,
1016 (u8 *)event);
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001017 free(event);
1018 return ret;
1019}
1020
1021/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001022 * tcg2_measure_boot_variable() - measure boot variables
1023 *
1024 * @dev: TPM device
1025 *
1026 * Return: status code
1027 */
1028static efi_status_t tcg2_measure_boot_variable(struct udevice *dev)
1029{
1030 u16 *boot_order;
1031 u16 *boot_index;
Simon Glass90975372022-01-23 12:55:12 -07001032 u16 var_name[] = u"BootOrder";
1033 u16 boot_name[] = u"Boot####";
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001034 u8 *bootvar;
1035 efi_uintn_t var_data_size;
1036 u32 count, i;
1037 efi_status_t ret;
1038
1039 boot_order = efi_get_var(var_name, &efi_global_variable_guid,
1040 &var_data_size);
1041 if (!boot_order) {
Masahisa Kojimad1325932021-11-09 18:44:54 +09001042 /* If "BootOrder" is not defined, skip the boot variable measurement */
1043 return EFI_SUCCESS;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001044 }
1045
1046 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name,
1047 &efi_global_variable_guid, var_data_size,
1048 (u8 *)boot_order);
1049 if (ret != EFI_SUCCESS)
1050 goto error;
1051
1052 count = var_data_size / sizeof(*boot_order);
1053 boot_index = boot_order;
1054 for (i = 0; i < count; i++) {
1055 efi_create_indexed_name(boot_name, sizeof(boot_name),
1056 "Boot", *boot_index++);
1057
1058 bootvar = efi_get_var(boot_name, &efi_global_variable_guid,
1059 &var_data_size);
1060
1061 if (!bootvar) {
Masahisa Kojimaaca20c82021-11-09 20:35:53 +09001062 log_debug("%ls not found\n", boot_name);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001063 continue;
1064 }
1065
1066 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2,
1067 boot_name,
1068 &efi_global_variable_guid,
1069 var_data_size, bootvar);
1070 free(bootvar);
1071 if (ret != EFI_SUCCESS)
1072 goto error;
1073 }
1074
1075error:
1076 free(boot_order);
1077 return ret;
1078}
1079
1080/**
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001081 * tcg2_measure_smbios() - measure smbios table
1082 *
1083 * @dev: TPM device
1084 * @entry: pointer to the smbios_entry structure
1085 *
1086 * Return: status code
1087 */
1088static efi_status_t
1089tcg2_measure_smbios(struct udevice *dev,
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001090 const struct smbios3_entry *entry)
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001091{
1092 efi_status_t ret;
1093 struct smbios_header *smbios_copy;
1094 struct smbios_handoff_table_pointers2 *event = NULL;
1095 u32 event_size;
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001096 const char smbios3_anchor[] = "_SM3_";
1097
1098 /* We only support SMBIOS 3.0 Entry Point structure */
1099 if (memcmp(entry->anchor, smbios3_anchor, sizeof(smbios3_anchor) - 1))
1100 return EFI_UNSUPPORTED;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001101
1102 /*
1103 * TCG PC Client PFP Spec says
1104 * "SMBIOS structures that contain static configuration information
1105 * (e.g. Platform Manufacturer Enterprise Number assigned by IANA,
1106 * platform model number, Vendor and Device IDs for each SMBIOS table)
1107 * that is relevant to the security of the platform MUST be measured".
1108 * Device dependent parameters such as serial number are cleared to
1109 * zero or spaces for the measurement.
1110 */
1111 event_size = sizeof(struct smbios_handoff_table_pointers2) +
1112 FIELD_SIZEOF(struct efi_configuration_table, guid) +
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +01001113 entry->table_maximum_size;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001114 event = calloc(1, event_size);
1115 if (!event) {
1116 ret = EFI_OUT_OF_RESOURCES;
1117 goto out;
1118 }
1119
1120 event->table_description_size = sizeof(SMBIOS_HANDOFF_TABLE_DESC);
1121 memcpy(event->table_description, SMBIOS_HANDOFF_TABLE_DESC,
1122 sizeof(SMBIOS_HANDOFF_TABLE_DESC));
1123 put_unaligned_le64(1, &event->number_of_tables);
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001124 guidcpy(&event->table_entry[0].guid, &smbios3_guid);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001125 smbios_copy = (struct smbios_header *)((uintptr_t)&event->table_entry[0].table);
1126 memcpy(&event->table_entry[0].table,
1127 (void *)((uintptr_t)entry->struct_table_address),
Heinrich Schuchardt68e948a2024-01-31 23:49:34 +01001128 entry->table_maximum_size);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001129
1130 smbios_prepare_measurement(entry, smbios_copy);
1131
Eddie James8ed7bb32023-10-24 10:43:49 -05001132 ret = measure_event(dev, 1, EV_EFI_HANDOFF_TABLES2, event_size,
1133 (u8 *)event);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001134 if (ret != EFI_SUCCESS)
1135 goto out;
1136
1137out:
1138 free(event);
1139
1140 return ret;
1141}
1142
1143/**
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001144 * tcg2_measure_gpt_table() - measure gpt table
1145 *
1146 * @dev: TPM device
1147 * @loaded_image: handle to the loaded image
1148 *
1149 * Return: status code
1150 */
1151static efi_status_t
1152tcg2_measure_gpt_data(struct udevice *dev,
1153 struct efi_loaded_image_obj *loaded_image)
1154{
1155 efi_status_t ret;
1156 efi_handle_t handle;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001157 struct efi_handler *dp_handler, *io_handler;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001158 struct efi_device_path *orig_device_path;
1159 struct efi_device_path *device_path;
1160 struct efi_device_path *dp;
1161 struct efi_block_io *block_io;
1162 struct efi_gpt_data *event = NULL;
1163 efi_guid_t null_guid = NULL_GUID;
1164 gpt_header *gpt_h;
1165 gpt_entry *entry = NULL;
1166 gpt_entry *gpt_e;
1167 u32 num_of_valid_entry = 0;
1168 u32 event_size;
1169 u32 i;
1170 u32 total_gpt_entry_size;
1171
1172 ret = efi_search_protocol(&loaded_image->header,
1173 &efi_guid_loaded_image_device_path,
1174 &dp_handler);
1175 if (ret != EFI_SUCCESS)
1176 return ret;
1177
1178 orig_device_path = dp_handler->protocol_interface;
1179 if (!orig_device_path) /* no device path, skip GPT measurement */
1180 return EFI_SUCCESS;
1181
1182 device_path = efi_dp_dup(orig_device_path);
1183 if (!device_path)
1184 return EFI_OUT_OF_RESOURCES;
1185
1186 dp = search_gpt_dp_node(device_path);
1187 if (!dp) {
1188 /* no GPT device path node found, skip GPT measurement */
1189 ret = EFI_SUCCESS;
1190 goto out1;
1191 }
1192
1193 /* read GPT header */
1194 dp->type = DEVICE_PATH_TYPE_END;
1195 dp->sub_type = DEVICE_PATH_SUB_TYPE_END;
1196 dp = device_path;
1197 ret = EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_guid,
1198 &dp, &handle));
1199 if (ret != EFI_SUCCESS)
1200 goto out1;
1201
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001202 ret = efi_search_protocol(handle, &efi_block_io_guid, &io_handler);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001203 if (ret != EFI_SUCCESS)
1204 goto out1;
Heinrich Schuchardt1100d152022-10-07 14:28:18 +02001205 block_io = io_handler->protocol_interface;
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001206
1207 gpt_h = memalign(block_io->media->io_align, block_io->media->block_size);
1208 if (!gpt_h) {
1209 ret = EFI_OUT_OF_RESOURCES;
1210 goto out2;
1211 }
1212
1213 ret = block_io->read_blocks(block_io, block_io->media->media_id, 1,
1214 block_io->media->block_size, gpt_h);
1215 if (ret != EFI_SUCCESS)
1216 goto out2;
1217
1218 /* read GPT entry */
1219 total_gpt_entry_size = gpt_h->num_partition_entries *
1220 gpt_h->sizeof_partition_entry;
1221 entry = memalign(block_io->media->io_align, total_gpt_entry_size);
1222 if (!entry) {
1223 ret = EFI_OUT_OF_RESOURCES;
1224 goto out2;
1225 }
1226
1227 ret = block_io->read_blocks(block_io, block_io->media->media_id,
1228 gpt_h->partition_entry_lba,
1229 total_gpt_entry_size, entry);
1230 if (ret != EFI_SUCCESS)
1231 goto out2;
1232
1233 /* count valid GPT entry */
1234 gpt_e = entry;
1235 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1236 if (guidcmp(&null_guid, &gpt_e->partition_type_guid))
1237 num_of_valid_entry++;
1238
1239 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1240 }
1241
1242 /* prepare event data for measurement */
1243 event_size = sizeof(struct efi_gpt_data) +
1244 (num_of_valid_entry * gpt_h->sizeof_partition_entry);
1245 event = calloc(1, event_size);
1246 if (!event) {
1247 ret = EFI_OUT_OF_RESOURCES;
1248 goto out2;
1249 }
1250 memcpy(event, gpt_h, sizeof(gpt_header));
1251 put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions);
1252
1253 /* copy valid GPT entry */
1254 gpt_e = entry;
1255 num_of_valid_entry = 0;
1256 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1257 if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) {
1258 memcpy((u8 *)event->partitions +
1259 (num_of_valid_entry * gpt_h->sizeof_partition_entry),
1260 gpt_e, gpt_h->sizeof_partition_entry);
1261 num_of_valid_entry++;
1262 }
1263
1264 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1265 }
1266
Eddie James8ed7bb32023-10-24 10:43:49 -05001267 ret = measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *)event);
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001268
1269out2:
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001270 free(gpt_h);
1271 free(entry);
1272 free(event);
1273out1:
1274 efi_free_pool(device_path);
1275
1276 return ret;
1277}
1278
Etienne Carriereb9064352023-02-16 17:29:48 +01001279/* Return the byte size of reserved map area in DTB or -1 upon error */
1280static ssize_t size_of_rsvmap(void *dtb)
1281{
1282 struct fdt_reserve_entry e;
1283 ssize_t size_max;
1284 ssize_t size;
1285 u8 *rsvmap_base;
1286
1287 rsvmap_base = (u8 *)dtb + fdt_off_mem_rsvmap(dtb);
1288 size_max = fdt_totalsize(dtb) - fdt_off_mem_rsvmap(dtb);
1289 size = 0;
1290
1291 do {
1292 memcpy(&e, rsvmap_base + size, sizeof(e));
1293 size += sizeof(e);
1294 if (size > size_max)
1295 return -1;
1296 } while (e.size);
1297
1298 return size;
1299}
1300
1301/**
1302 * efi_tcg2_measure_dtb() - measure DTB passed to the OS
1303 *
1304 * @dtb: pointer to the device tree blob
1305 *
1306 * Return: status code
1307 */
1308efi_status_t efi_tcg2_measure_dtb(void *dtb)
1309{
1310 struct uefi_platform_firmware_blob2 *blob;
1311 struct fdt_header *header;
1312 sha256_context hash_ctx;
1313 struct udevice *dev;
1314 ssize_t rsvmap_size;
1315 efi_status_t ret;
1316 u32 event_size;
1317
1318 if (!is_tcg2_protocol_installed())
1319 return EFI_SUCCESS;
1320
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001321 if (tcg2_platform_get_tpm2(&dev))
Etienne Carriereb9064352023-02-16 17:29:48 +01001322 return EFI_SECURITY_VIOLATION;
1323
1324 rsvmap_size = size_of_rsvmap(dtb);
1325 if (rsvmap_size < 0)
1326 return EFI_SECURITY_VIOLATION;
1327
1328 event_size = sizeof(*blob) + sizeof(EFI_DTB_EVENT_STRING) + SHA256_SUM_LEN;
1329 blob = calloc(1, event_size);
1330 if (!blob)
1331 return EFI_OUT_OF_RESOURCES;
1332
1333 blob->blob_description_size = sizeof(EFI_DTB_EVENT_STRING);
1334 memcpy(blob->data, EFI_DTB_EVENT_STRING, blob->blob_description_size);
1335
1336 /* Measure populated areas of the DTB */
1337 header = dtb;
1338 sha256_starts(&hash_ctx);
1339 sha256_update(&hash_ctx, (u8 *)header, sizeof(struct fdt_header));
1340 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_struct(dtb), fdt_size_dt_strings(dtb));
1341 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_strings(dtb), fdt_size_dt_struct(dtb));
1342 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_mem_rsvmap(dtb), rsvmap_size);
1343 sha256_finish(&hash_ctx, blob->data + blob->blob_description_size);
1344
Ilias Apalodimasc90c1952024-06-14 15:09:50 +03001345 ret = measure_event(dev, 1, EV_POST_CODE, event_size, (u8 *)blob);
Etienne Carriereb9064352023-02-16 17:29:48 +01001346
1347 free(blob);
1348 return ret;
1349}
1350
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001351/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001352 * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation
1353 *
1354 * Return: status code
1355 */
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001356efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_image_obj *handle)
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001357{
1358 efi_status_t ret;
1359 u32 pcr_index;
1360 struct udevice *dev;
1361 u32 event = 0;
Masahisa Kojimad8733f32024-01-26 09:53:42 +09001362 struct smbios3_entry *entry;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001363
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001364 if (!is_tcg2_protocol_installed())
1365 return EFI_SUCCESS;
1366
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001367 if (tcg2_efi_app_invoked)
1368 return EFI_SUCCESS;
1369
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001370 if (tcg2_platform_get_tpm2(&dev))
Masahisa Kojima38155ea2021-12-07 14:15:33 +09001371 return EFI_SECURITY_VIOLATION;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001372
1373 ret = tcg2_measure_boot_variable(dev);
1374 if (ret != EFI_SUCCESS)
1375 goto out;
1376
Eddie James8ed7bb32023-10-24 10:43:49 -05001377 ret = measure_event(dev, 4, EV_EFI_ACTION,
1378 strlen(EFI_CALLING_EFI_APPLICATION),
1379 (u8 *)EFI_CALLING_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001380 if (ret != EFI_SUCCESS)
1381 goto out;
1382
Heinrich Schuchardt10899c82024-01-26 09:13:22 +01001383 entry = efi_get_configuration_table(&smbios3_guid);
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001384 if (entry) {
1385 ret = tcg2_measure_smbios(dev, entry);
1386 if (ret != EFI_SUCCESS)
1387 goto out;
1388 }
1389
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001390 ret = tcg2_measure_gpt_data(dev, handle);
1391 if (ret != EFI_SUCCESS)
1392 goto out;
1393
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001394 for (pcr_index = 0; pcr_index <= 7; pcr_index++) {
Eddie James8ed7bb32023-10-24 10:43:49 -05001395 ret = measure_event(dev, pcr_index, EV_SEPARATOR,
1396 sizeof(event), (u8 *)&event);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001397 if (ret != EFI_SUCCESS)
1398 goto out;
1399 }
1400
1401 tcg2_efi_app_invoked = true;
1402out:
1403 return ret;
1404}
1405
1406/**
1407 * efi_tcg2_measure_efi_app_exit() - measure efi app exit
1408 *
1409 * Return: status code
1410 */
1411efi_status_t efi_tcg2_measure_efi_app_exit(void)
1412{
1413 efi_status_t ret;
1414 struct udevice *dev;
1415
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001416 if (!is_tcg2_protocol_installed())
1417 return EFI_SUCCESS;
1418
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001419 if (tcg2_platform_get_tpm2(&dev))
1420 return EFI_SECURITY_VIOLATION;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001421
Eddie James8ed7bb32023-10-24 10:43:49 -05001422 ret = measure_event(dev, 4, EV_EFI_ACTION,
1423 strlen(EFI_RETURNING_FROM_EFI_APPLICATION),
1424 (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001425 return ret;
1426}
1427
1428/**
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001429 * efi_tcg2_notify_exit_boot_services() - ExitBootService callback
1430 *
1431 * @event: callback event
1432 * @context: callback context
1433 */
1434static void EFIAPI
1435efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context)
1436{
1437 efi_status_t ret;
1438 struct udevice *dev;
1439
1440 EFI_ENTRY("%p, %p", event, context);
1441
Ilias Apalodimas24e841a2021-11-18 09:03:39 +02001442 event_log.ebs_called = true;
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001443
1444 if (!is_tcg2_protocol_installed()) {
1445 ret = EFI_SUCCESS;
1446 goto out;
1447 }
1448
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001449 if (tcg2_platform_get_tpm2(&dev)) {
1450 ret = EFI_SECURITY_VIOLATION;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001451 goto out;
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001452 }
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001453
Eddie James8ed7bb32023-10-24 10:43:49 -05001454 ret = measure_event(dev, 5, EV_EFI_ACTION,
1455 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1456 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001457 if (ret != EFI_SUCCESS)
1458 goto out;
1459
Eddie James8ed7bb32023-10-24 10:43:49 -05001460 ret = measure_event(dev, 5, EV_EFI_ACTION,
1461 strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED),
1462 (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001463
1464out:
1465 EFI_EXIT(ret);
1466}
1467
1468/**
1469 * efi_tcg2_notify_exit_boot_services_failed()
1470 * - notify ExitBootServices() is failed
1471 *
1472 * Return: status code
1473 */
1474efi_status_t efi_tcg2_notify_exit_boot_services_failed(void)
1475{
1476 struct udevice *dev;
1477 efi_status_t ret;
1478
Masahisa Kojimafd19a7e2021-12-07 14:15:32 +09001479 if (!is_tcg2_protocol_installed())
1480 return EFI_SUCCESS;
1481
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001482 if (tcg2_platform_get_tpm2(&dev))
1483 return EFI_SECURITY_VIOLATION;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001484
Eddie James8ed7bb32023-10-24 10:43:49 -05001485 ret = measure_event(dev, 5, EV_EFI_ACTION,
1486 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1487 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001488 if (ret != EFI_SUCCESS)
1489 goto out;
1490
Eddie James8ed7bb32023-10-24 10:43:49 -05001491 ret = measure_event(dev, 5, EV_EFI_ACTION,
1492 strlen(EFI_EXIT_BOOT_SERVICES_FAILED),
1493 (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED);
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001494
1495out:
1496 return ret;
1497}
1498
1499/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001500 * tcg2_measure_secure_boot_variable() - measure secure boot variables
1501 *
1502 * @dev: TPM device
1503 *
1504 * Return: status code
1505 */
1506static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev)
1507{
1508 u8 *data;
1509 efi_uintn_t data_size;
1510 u32 count, i;
1511 efi_status_t ret;
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001512 u8 deployed_mode;
1513 efi_uintn_t size;
1514 u32 deployed_audit_pcr_index = 1;
1515
1516 size = sizeof(deployed_mode);
1517 ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid,
1518 NULL, &size, &deployed_mode, NULL);
1519 if (ret != EFI_SUCCESS || !deployed_mode)
1520 deployed_audit_pcr_index = 7;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001521
1522 count = ARRAY_SIZE(secure_variables);
1523 for (i = 0; i < count; i++) {
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001524 const efi_guid_t *guid;
1525
Masahisa Kojima21684522021-10-26 17:27:26 +09001526 guid = efi_auth_var_get_guid(secure_variables[i].name);
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02001527
Masahisa Kojima21684522021-10-26 17:27:26 +09001528 data = efi_get_var(secure_variables[i].name, guid, &data_size);
1529 if (!data && !secure_variables[i].accept_empty)
1530 continue;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001531
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09001532 if (u16_strcmp(u"DeployedMode", secure_variables[i].name))
1533 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1534 if (u16_strcmp(u"AuditMode", secure_variables[i].name))
1535 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1536
1537 ret = tcg2_measure_variable(dev, secure_variables[i].pcr_index,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001538 EV_EFI_VARIABLE_DRIVER_CONFIG,
Masahisa Kojima21684522021-10-26 17:27:26 +09001539 secure_variables[i].name, guid,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001540 data_size, data);
1541 free(data);
1542 if (ret != EFI_SUCCESS)
1543 goto error;
1544 }
1545
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001546error:
1547 return ret;
1548}
1549
1550/**
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001551 * efi_tcg2_do_initial_measurement() - do initial measurement
1552 *
1553 * Return: status code
1554 */
1555efi_status_t efi_tcg2_do_initial_measurement(void)
1556{
1557 efi_status_t ret;
1558 struct udevice *dev;
1559
1560 if (!is_tcg2_protocol_installed())
1561 return EFI_SUCCESS;
1562
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001563 if (tcg2_platform_get_tpm2(&dev))
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001564 return EFI_SECURITY_VIOLATION;
1565
1566 ret = tcg2_measure_secure_boot_variable(dev);
1567 if (ret != EFI_SUCCESS)
1568 goto out;
1569
1570out:
1571 return ret;
1572}
1573
1574/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001575 * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
1576 *
1577 * If a TPM2 device is available, the TPM TCG2 Protocol is registered
1578 *
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001579 * Return: status code
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001580 */
1581efi_status_t efi_tcg2_register(void)
1582{
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001583 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001584 struct udevice *dev;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001585 struct efi_event *event;
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001586 u32 err;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001587
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001588 if (tcg2_platform_get_tpm2(&dev)) {
Ilias Apalodimaseb1b6b42023-01-19 16:29:15 +02001589 log_warning("Missing TPMv2 device for EFI_TCG_PROTOCOL\n");
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001590 return EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001591 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001592
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001593 /* initialize the TPM as early as possible. */
Ilias Apalodimas0c95d222023-01-25 13:06:03 +02001594 err = tpm_auto_start(dev);
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001595 if (err) {
Ilias Apalodimasfa3ab342024-06-22 17:35:38 +03001596 ret = EFI_DEVICE_ERROR;
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02001597 log_err("TPM startup failed\n");
1598 goto fail;
1599 }
1600
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001601 ret = efi_init_event_log();
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001602 if (ret != EFI_SUCCESS) {
1603 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001604 goto fail;
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001605 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001606
Ilias Apalodimas4953c992023-06-19 14:14:02 +03001607 ret = efi_install_multiple_protocol_interfaces(&efi_root, &efi_guid_tcg2_protocol,
1608 &efi_tcg2_protocol, NULL);
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001609 if (ret != EFI_SUCCESS) {
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001610 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001611 goto fail;
1612 }
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001613
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09001614 ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
1615 efi_tcg2_notify_exit_boot_services, NULL,
1616 NULL, &event);
1617 if (ret != EFI_SUCCESS) {
1618 tcg2_uninit();
1619 goto fail;
1620 }
1621
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001622 return ret;
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001623
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001624fail:
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001625 log_err("Cannot install EFI_TCG2_PROTOCOL\n");
Masahisa Kojima0fd43792021-12-07 14:15:31 +09001626 return ret;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001627}