blob: 5f71b188a0374f531003f0ded7294f96c618e2f1 [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
11#include <common.h>
12#include <dm.h>
13#include <efi_loader.h>
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +020014#include <efi_variable.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020015#include <efi_tcg2.h>
16#include <log.h>
Masahisa Kojima70be5a62021-05-26 12:09:58 +090017#include <malloc.h>
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +090018#include <smbios.h>
Pali Rohárba87ddf2021-08-02 15:18:31 +020019#include <version_string.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020020#include <tpm-v2.h>
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +020021#include <tpm_api.h>
Masahisa Kojima70be5a62021-05-26 12:09:58 +090022#include <u-boot/hash-checksum.h>
Ilias Apalodimas967650d2020-11-30 11:47:40 +020023#include <u-boot/sha1.h>
24#include <u-boot/sha256.h>
25#include <u-boot/sha512.h>
Masahisa Kojimad420d8d2021-11-03 11:04:09 +090026#include <linux/unaligned/be_byteshift.h>
27#include <linux/unaligned/le_byteshift.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020028#include <linux/unaligned/generic.h>
Ilias Apalodimas967650d2020-11-30 11:47:40 +020029#include <hexdump.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020030
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020031/**
32 * struct event_log_buffer - internal eventlog management structure
33 *
34 * @buffer: eventlog buffer
35 * @final_buffer: finalevent config table buffer
36 * @pos: current position of 'buffer'
37 * @final_pos: current position of 'final_buffer'
38 * @get_event_called: true if GetEventLog has been invoked at least once
39 * @ebs_called: true if ExitBootServices has been invoked
40 * @truncated: true if the 'buffer' is truncated
41 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +020042struct event_log_buffer {
43 void *buffer;
44 void *final_buffer;
45 size_t pos; /* eventlog position */
46 size_t final_pos; /* final events config table position */
47 size_t last_event_size;
48 bool get_event_called;
Ilias Apalodimas24e841a2021-11-18 09:03:39 +020049 bool ebs_called;
Ilias Apalodimas967650d2020-11-30 11:47:40 +020050 bool truncated;
51};
Ilias Apalodimas590fef62020-11-11 11:18:11 +020052
Ilias Apalodimas967650d2020-11-30 11:47:40 +020053static struct event_log_buffer event_log;
Masahisa Kojima8173cd42021-08-13 16:12:40 +090054static bool tcg2_efi_app_invoked;
Ilias Apalodimas590fef62020-11-11 11:18:11 +020055/*
56 * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset.
57 * Since the current tpm2_get_capability() response buffers starts at
58 * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate
59 * the response size and offset once for all consumers
60 */
61#define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \
62 offsetof(struct tpms_capability_data, data))
63#define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \
64 offsetof(struct tpms_tagged_property, value))
65
Ilias Apalodimas967650d2020-11-30 11:47:40 +020066static const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID;
67static const efi_guid_t efi_guid_final_events = EFI_TCG2_FINAL_EVENTS_TABLE_GUID;
68
69struct digest_info {
Ilias Apalodimasc67fef62020-11-16 08:52:41 +020070 u16 hash_alg;
71 u32 hash_mask;
Ilias Apalodimas967650d2020-11-30 11:47:40 +020072 u16 hash_len;
73};
74
Ilias Apalodimas190b0a22021-05-25 14:35:31 +030075static const struct digest_info hash_algo_list[] = {
Ilias Apalodimasc67fef62020-11-16 08:52:41 +020076 {
77 TPM2_ALG_SHA1,
78 EFI_TCG2_BOOT_HASH_ALG_SHA1,
Ilias Apalodimas967650d2020-11-30 11:47:40 +020079 TPM2_SHA1_DIGEST_SIZE,
Ilias Apalodimasc67fef62020-11-16 08:52:41 +020080 },
81 {
82 TPM2_ALG_SHA256,
83 EFI_TCG2_BOOT_HASH_ALG_SHA256,
Ilias Apalodimas967650d2020-11-30 11:47:40 +020084 TPM2_SHA256_DIGEST_SIZE,
Ilias Apalodimasc67fef62020-11-16 08:52:41 +020085 },
86 {
87 TPM2_ALG_SHA384,
88 EFI_TCG2_BOOT_HASH_ALG_SHA384,
Ilias Apalodimas967650d2020-11-30 11:47:40 +020089 TPM2_SHA384_DIGEST_SIZE,
Ilias Apalodimasc67fef62020-11-16 08:52:41 +020090 },
91 {
92 TPM2_ALG_SHA512,
93 EFI_TCG2_BOOT_HASH_ALG_SHA512,
Ilias Apalodimas967650d2020-11-30 11:47:40 +020094 TPM2_SHA512_DIGEST_SIZE,
Ilias Apalodimasc67fef62020-11-16 08:52:41 +020095 },
96};
97
Masahisa Kojima21684522021-10-26 17:27:26 +090098struct variable_info {
99 const u16 *name;
100 bool accept_empty;
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +0900101 u32 pcr_index;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +0900102};
103
Masahisa Kojima21684522021-10-26 17:27:26 +0900104static struct variable_info secure_variables[] = {
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +0900105 {u"SecureBoot", true, 7},
106 {u"PK", true, 7},
107 {u"KEK", true, 7},
108 {u"db", true, 7},
109 {u"dbx", true, 7},
110 {u"dbt", false, 7},
111 {u"dbr", false, 7},
112 {u"DeployedMode", false, 1},
113 {u"AuditMode", false, 1},
Masahisa Kojima21684522021-10-26 17:27:26 +0900114};
115
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200116#define MAX_HASH_COUNT ARRAY_SIZE(hash_algo_list)
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200117
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200118/**
119 * alg_to_mask - Get a TCG hash mask for algorithms
120 *
121 * @hash_alg: TCG defined algorithm
122 *
123 * @Return: TCG hashing algorithm bitmaps, 0 if the algorithm is not supported
124 */
125static u32 alg_to_mask(u16 hash_alg)
126{
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300127 size_t i;
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200128
129 for (i = 0; i < MAX_HASH_COUNT; i++) {
130 if (hash_algo_list[i].hash_alg == hash_alg)
131 return hash_algo_list[i].hash_mask;
132 }
133
134 return 0;
135}
136
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200137/**
138 * alg_to_len - Get a TCG hash len for algorithms
139 *
140 * @hash_alg: TCG defined algorithm
141 *
142 * @Return: len of chosen algorithm, 0 if the algorithm is not supported
143 */
144static u16 alg_to_len(u16 hash_alg)
145{
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300146 size_t i;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200147
148 for (i = 0; i < MAX_HASH_COUNT; i++) {
149 if (hash_algo_list[i].hash_alg == hash_alg)
150 return hash_algo_list[i].hash_len;
151 }
152
153 return 0;
154}
155
156static u32 tcg_event_final_size(struct tpml_digest_values *digest_list)
157{
158 u32 len;
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300159 size_t i;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200160
161 len = offsetof(struct tcg_pcr_event2, digests);
162 len += offsetof(struct tpml_digest_values, digests);
163 for (i = 0; i < digest_list->count; i++) {
164 u16 hash_alg = digest_list->digests[i].hash_alg;
165
166 len += offsetof(struct tpmt_ha, digest);
167 len += alg_to_len(hash_alg);
168 }
169 len += sizeof(u32); /* tcg_pcr_event2 event_size*/
170
171 return len;
172}
173
174/* tcg2_pcr_extend - Extend PCRs for a TPM2 device for a given tpml_digest_values
175 *
176 * @dev: device
177 * @digest_list: list of digest algorithms to extend
178 *
179 * @Return: status code
180 */
181static efi_status_t tcg2_pcr_extend(struct udevice *dev, u32 pcr_index,
182 struct tpml_digest_values *digest_list)
183{
184 u32 rc;
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300185 size_t i;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200186
187 for (i = 0; i < digest_list->count; i++) {
188 u32 alg = digest_list->digests[i].hash_alg;
189
190 rc = tpm2_pcr_extend(dev, pcr_index, alg,
191 (u8 *)&digest_list->digests[i].digest,
192 alg_to_len(alg));
193 if (rc) {
194 EFI_PRINT("Failed to extend PCR\n");
195 return EFI_DEVICE_ERROR;
196 }
197 }
198
199 return EFI_SUCCESS;
200}
201
Ruchika Gupta9d0b5d02021-11-29 13:09:46 +0530202/* tcg2_pcr_read - Read PCRs for a TPM2 device for a given tpml_digest_values
203 *
204 * @dev: device
205 * @pcr_index: PCR index
206 * @digest_list: list of digest algorithms to extend
207 *
208 * @Return: status code
209 */
210static efi_status_t tcg2_pcr_read(struct udevice *dev, u32 pcr_index,
211 struct tpml_digest_values *digest_list)
212{
213 struct tpm_chip_priv *priv;
214 unsigned int updates, pcr_select_min;
215 u32 rc;
216 size_t i;
217
218 priv = dev_get_uclass_priv(dev);
219 if (!priv)
220 return EFI_DEVICE_ERROR;
221
222 pcr_select_min = priv->pcr_select_min;
223
224 for (i = 0; i < digest_list->count; i++) {
225 u16 hash_alg = digest_list->digests[i].hash_alg;
226 u8 *digest = (u8 *)&digest_list->digests[i].digest;
227
228 rc = tpm2_pcr_read(dev, pcr_index, pcr_select_min,
229 hash_alg, digest, alg_to_len(hash_alg),
230 &updates);
231 if (rc) {
232 EFI_PRINT("Failed to read PCR\n");
233 return EFI_DEVICE_ERROR;
234 }
235 }
236
237 return EFI_SUCCESS;
238}
239
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200240/* put_event - Append an agile event to an eventlog
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200241 *
242 * @pcr_index: PCR index
243 * @event_type: type of event added
244 * @digest_list: list of digest algorithms to add
245 * @size: size of event
246 * @event: event to add
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200247 * @log: log buffer to append the event
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200248 *
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200249 */
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200250static void put_event(u32 pcr_index, u32 event_type,
251 struct tpml_digest_values *digest_list, u32 size,
252 u8 event[], void *log)
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200253{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200254 size_t pos;
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300255 size_t i;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200256 u32 event_size;
257
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200258 /*
259 * size refers to the length of event[] only, we need to check against
260 * the final tcg_pcr_event2 size
261 */
262 event_size = size + tcg_event_final_size(digest_list);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200263
264 put_unaligned_le32(pcr_index, log);
265 pos = offsetof(struct tcg_pcr_event2, event_type);
Ilias Apalodimas4390e762021-03-30 00:42:36 +0300266 put_unaligned_le32(event_type, (void *)((uintptr_t)log + pos));
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200267 pos = offsetof(struct tcg_pcr_event2, digests); /* count */
Ilias Apalodimas4390e762021-03-30 00:42:36 +0300268 put_unaligned_le32(digest_list->count, (void *)((uintptr_t)log + pos));
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200269
270 pos += offsetof(struct tpml_digest_values, digests);
271 for (i = 0; i < digest_list->count; i++) {
272 u16 hash_alg = digest_list->digests[i].hash_alg;
273 u8 *digest = (u8 *)&digest_list->digests[i].digest;
274
Ilias Apalodimas4390e762021-03-30 00:42:36 +0300275 put_unaligned_le16(hash_alg, (void *)((uintptr_t)log + pos));
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200276 pos += offsetof(struct tpmt_ha, digest);
Ilias Apalodimas4390e762021-03-30 00:42:36 +0300277 memcpy((void *)((uintptr_t)log + pos), digest, alg_to_len(hash_alg));
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200278 pos += alg_to_len(hash_alg);
279 }
280
Ilias Apalodimas4390e762021-03-30 00:42:36 +0300281 put_unaligned_le32(size, (void *)((uintptr_t)log + pos));
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200282 pos += sizeof(u32); /* tcg_pcr_event2 event_size*/
Ilias Apalodimas4390e762021-03-30 00:42:36 +0300283 memcpy((void *)((uintptr_t)log + pos), event, size);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200284 pos += size;
285
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200286 /*
287 * make sure the calculated buffer is what we checked against
288 * This check should never fail. It checks the code above is
289 * calculating the right length for the event we are adding
290 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200291 if (pos != event_size)
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200292 log_err("Appending to the EventLog failed\n");
293}
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200294
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200295/* tcg2_agile_log_append - Append an agile event to an eventlog
296 *
297 * @pcr_index: PCR index
298 * @event_type: type of event added
299 * @digest_list: list of digest algorithms to add
300 * @size: size of event
301 * @event: event to add
302 * @log: log buffer to append the event
303 *
304 * @Return: status code
305 */
306static efi_status_t tcg2_agile_log_append(u32 pcr_index, u32 event_type,
307 struct tpml_digest_values *digest_list,
308 u32 size, u8 event[])
309{
310 void *log = (void *)((uintptr_t)event_log.buffer + event_log.pos);
311 u32 event_size = size + tcg_event_final_size(digest_list);
312 struct efi_tcg2_final_events_table *final_event;
313 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200314
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200315 /* if ExitBootServices hasn't been called update the normal log */
316 if (!event_log.ebs_called) {
317 if (event_log.truncated ||
318 event_log.pos + event_size > TPM2_EVENT_LOG_SIZE) {
319 event_log.truncated = true;
320 return EFI_VOLUME_FULL;
321 }
322 put_event(pcr_index, event_type, digest_list, size, event, log);
323 event_log.pos += event_size;
324 event_log.last_event_size = event_size;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200325 }
326
Ilias Apalodimas24e841a2021-11-18 09:03:39 +0200327 if (!event_log.get_event_called)
328 return ret;
329
330 /* if GetEventLog has been called update FinalEventLog as well */
331 if (event_log.final_pos + event_size > TPM2_EVENT_LOG_SIZE)
332 return EFI_VOLUME_FULL;
333
334 log = (void *)((uintptr_t)event_log.final_buffer + event_log.final_pos);
335 put_event(pcr_index, event_type, digest_list, size, event, log);
336
337 final_event = event_log.final_buffer;
338 final_event->number_of_events++;
339 event_log.final_pos += event_size;
340
341 return ret;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200342}
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200343
344/**
345 * platform_get_tpm_device() - retrieve TPM device
346 *
347 * This function retrieves the udevice implementing a TPM
348 *
349 * This function may be overridden if special initialization is needed.
350 *
351 * @dev: udevice
352 * Return: status code
353 */
354__weak efi_status_t platform_get_tpm2_device(struct udevice **dev)
355{
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200356 for_each_tpm_device(*dev) {
357 /* Only support TPMv2 devices */
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200358 if (tpm_get_version(*dev) == TPM_V2)
359 return EFI_SUCCESS;
360 }
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200361
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200362 return EFI_NOT_FOUND;
363}
364
365/**
Ruchika Guptabc9495c2021-11-29 13:09:44 +0530366 * platform_get_eventlog() - retrieve the eventlog address and size
367 *
368 * This function retrieves the eventlog address and size if the underlying
369 * firmware has done some measurements and passed them.
370 *
371 * This function may be overridden based on platform specific method of
372 * passing the eventlog address and size.
373 *
374 * @dev: udevice
375 * @addr: eventlog address
376 * @sz: eventlog size
377 * Return: status code
378 */
379__weak efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr,
380 u32 *sz)
381{
382 const u64 *basep;
383 const u32 *sizep;
384
385 basep = dev_read_prop(dev, "tpm_event_log_addr", NULL);
386 if (!basep)
387 return EFI_NOT_FOUND;
388
389 *addr = be64_to_cpup((__force __be64 *)basep);
390
391 sizep = dev_read_prop(dev, "tpm_event_log_size", NULL);
392 if (!sizep)
393 return EFI_NOT_FOUND;
394
395 *sz = be32_to_cpup((__force __be32 *)sizep);
396 if (*sz == 0) {
397 log_debug("event log empty\n");
398 return EFI_NOT_FOUND;
399 }
400
401 return EFI_SUCCESS;
402}
403
404/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200405 * tpm2_get_max_command_size() - get the supported max command size
406 *
407 * @dev: TPM device
408 * @max_command_size: output buffer for the size
409 *
410 * Return: 0 on success, -1 on error
411 */
412static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size)
413{
414 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
415 u32 ret;
416
417 memset(response, 0, sizeof(response));
418 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
419 TPM2_PT_MAX_COMMAND_SIZE, response, 1);
420 if (ret)
421 return -1;
422
423 *max_command_size = (uint16_t)get_unaligned_be32(response +
424 properties_offset);
425
426 return 0;
427}
428
429/**
430 * tpm2_get_max_response_size() - get the supported max response size
431 *
432 * @dev: TPM device
433 * @max_response_size: output buffer for the size
434 *
435 * Return: 0 on success, -1 on error
436 */
437static int tpm2_get_max_response_size(struct udevice *dev,
438 u16 *max_response_size)
439{
440 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
441 u32 ret;
442
443 memset(response, 0, sizeof(response));
444 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
445 TPM2_PT_MAX_RESPONSE_SIZE, response, 1);
446 if (ret)
447 return -1;
448
449 *max_response_size = (uint16_t)get_unaligned_be32(response +
450 properties_offset);
451
452 return 0;
453}
454
455/**
456 * tpm2_get_manufacturer_id() - get the manufacturer ID
457 *
458 * @dev: TPM device
459 * @manufacturer_id: output buffer for the id
460 *
461 * Return: 0 on success, -1 on error
462 */
463static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id)
464{
465 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
466 u32 ret;
467
468 memset(response, 0, sizeof(response));
469 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
470 TPM2_PT_MANUFACTURER, response, 1);
471 if (ret)
472 return -1;
473
474 *manufacturer_id = get_unaligned_be32(response + properties_offset);
475
476 return 0;
477}
478
479/**
480 * tpm2_get_num_pcr() - get the number of PCRs
481 *
482 * @dev: TPM device
483 * @manufacturer_id: output buffer for the number
484 *
485 * Return: 0 on success, -1 on error
486 */
487static int tpm2_get_num_pcr(struct udevice *dev, u32 *num_pcr)
488{
489 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
490 u32 ret;
491
492 memset(response, 0, sizeof(response));
493 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
494 TPM2_PT_PCR_COUNT, response, 1);
495 if (ret)
496 return -1;
497
498 *num_pcr = get_unaligned_be32(response + properties_offset);
499 if (*num_pcr > TPM2_MAX_PCRS)
500 return -1;
501
502 return 0;
503}
504
505/**
506 * is_active_pcr() - Check if a supported algorithm is active
507 *
508 * @dev: TPM device
509 * @selection: struct of PCR information
510 *
511 * Return: true if PCR is active
512 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200513static bool is_active_pcr(struct tpms_pcr_selection *selection)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200514{
515 int i;
516 /*
517 * check the pcr_select. If at least one of the PCRs supports the
518 * algorithm add it on the active ones
519 */
520 for (i = 0; i < selection->size_of_select; i++) {
521 if (selection->pcr_select[i])
522 return true;
523 }
524
525 return false;
526}
527
528/**
529 * tpm2_get_pcr_info() - get the supported, active PCRs and number of banks
530 *
531 * @dev: TPM device
532 * @supported_pcr: bitmask with the algorithms supported
533 * @active_pcr: bitmask with the active algorithms
534 * @pcr_banks: number of PCR banks
535 *
536 * Return: 0 on success, -1 on error
537 */
538static int tpm2_get_pcr_info(struct udevice *dev, u32 *supported_pcr,
539 u32 *active_pcr, u32 *pcr_banks)
540{
541 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
542 struct tpml_pcr_selection pcrs;
543 u32 ret, num_pcr;
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300544 size_t i;
545 int tpm_ret;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200546
Ilias Apalodimas09402b12021-05-26 21:01:00 +0300547 *supported_pcr = 0;
548 *active_pcr = 0;
549 *pcr_banks = 0;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200550 memset(response, 0, sizeof(response));
551 ret = tpm2_get_capability(dev, TPM2_CAP_PCRS, 0, response, 1);
552 if (ret)
553 goto out;
554
555 pcrs.count = get_unaligned_be32(response);
556 /*
557 * We only support 5 algorithms for now so check against that
558 * instead of TPM2_NUM_PCR_BANKS
559 */
560 if (pcrs.count > MAX_HASH_COUNT || pcrs.count < 1)
561 goto out;
562
563 tpm_ret = tpm2_get_num_pcr(dev, &num_pcr);
564 if (tpm_ret)
565 goto out;
566
567 for (i = 0; i < pcrs.count; i++) {
568 /*
569 * Definition of TPMS_PCR_SELECTION Structure
570 * hash: u16
571 * size_of_select: u8
572 * pcr_select: u8 array
573 *
574 * The offsets depend on the number of the device PCRs
575 * so we have to calculate them based on that
576 */
577 u32 hash_offset = offsetof(struct tpml_pcr_selection, selection) +
578 i * offsetof(struct tpms_pcr_selection, pcr_select) +
579 i * ((num_pcr + 7) / 8);
580 u32 size_select_offset =
581 hash_offset + offsetof(struct tpms_pcr_selection,
582 size_of_select);
583 u32 pcr_select_offset =
584 hash_offset + offsetof(struct tpms_pcr_selection,
585 pcr_select);
586
587 pcrs.selection[i].hash =
588 get_unaligned_be16(response + hash_offset);
589 pcrs.selection[i].size_of_select =
590 __get_unaligned_be(response + size_select_offset);
591 if (pcrs.selection[i].size_of_select > TPM2_PCR_SELECT_MAX)
592 goto out;
593 /* copy the array of pcr_select */
594 memcpy(pcrs.selection[i].pcr_select, response + pcr_select_offset,
595 pcrs.selection[i].size_of_select);
596 }
597
598 for (i = 0; i < pcrs.count; i++) {
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200599 u32 hash_mask = alg_to_mask(pcrs.selection[i].hash);
600
601 if (hash_mask) {
602 *supported_pcr |= hash_mask;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200603 if (is_active_pcr(&pcrs.selection[i]))
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200604 *active_pcr |= hash_mask;
605 } else {
606 EFI_PRINT("Unknown algorithm %x\n", pcrs.selection[i].hash);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200607 }
608 }
609
610 *pcr_banks = pcrs.count;
611
612 return 0;
613out:
614 return -1;
615}
616
617/**
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200618 * __get_active_pcr_banks() - returns the currently active PCR banks
619 *
620 * @active_pcr_banks: pointer for receiving the bitmap of currently
621 * active PCR banks
622 *
623 * Return: status code
624 */
625static efi_status_t __get_active_pcr_banks(u32 *active_pcr_banks)
626{
627 struct udevice *dev;
Ilias Apalodimas09402b12021-05-26 21:01:00 +0300628 u32 active = 0, supported = 0, pcr_banks = 0;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200629 efi_status_t ret;
630 int err;
631
632 ret = platform_get_tpm2_device(&dev);
633 if (ret != EFI_SUCCESS)
634 goto out;
635
636 err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_banks);
637 if (err) {
638 ret = EFI_DEVICE_ERROR;
639 goto out;
640 }
641
642 *active_pcr_banks = active;
643
644out:
645 return ret;
646}
647
648/* tcg2_create_digest - create a list of digests of the supported PCR banks
649 * for a given memory range
650 *
651 * @input: input memory
652 * @length: length of buffer to calculate the digest
653 * @digest_list: list of digests to fill in
654 *
655 * Return: status code
656 */
657static efi_status_t tcg2_create_digest(const u8 *input, u32 length,
658 struct tpml_digest_values *digest_list)
659{
660 sha1_context ctx;
661 sha256_context ctx_256;
662 sha512_context ctx_512;
Masahisa Kojimaeb74a902021-04-14 11:55:49 +0900663 u8 final[TPM2_SHA512_DIGEST_SIZE];
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200664 efi_status_t ret;
665 u32 active;
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300666 size_t i;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200667
668 ret = __get_active_pcr_banks(&active);
669 if (ret != EFI_SUCCESS)
670 return ret;
671
672 digest_list->count = 0;
673 for (i = 0; i < MAX_HASH_COUNT; i++) {
674 u16 hash_alg = hash_algo_list[i].hash_alg;
675
676 if (!(active & alg_to_mask(hash_alg)))
677 continue;
678 switch (hash_alg) {
679 case TPM2_ALG_SHA1:
680 sha1_starts(&ctx);
681 sha1_update(&ctx, input, length);
682 sha1_finish(&ctx, final);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200683 break;
684 case TPM2_ALG_SHA256:
685 sha256_starts(&ctx_256);
686 sha256_update(&ctx_256, input, length);
687 sha256_finish(&ctx_256, final);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200688 break;
689 case TPM2_ALG_SHA384:
690 sha384_starts(&ctx_512);
691 sha384_update(&ctx_512, input, length);
692 sha384_finish(&ctx_512, final);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200693 break;
694 case TPM2_ALG_SHA512:
695 sha512_starts(&ctx_512);
696 sha512_update(&ctx_512, input, length);
697 sha512_finish(&ctx_512, final);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200698 break;
699 default:
700 EFI_PRINT("Unsupported algorithm %x\n", hash_alg);
701 return EFI_INVALID_PARAMETER;
702 }
Ruchika Guptae53007b2021-09-14 12:14:31 +0530703 digest_list->digests[digest_list->count].hash_alg = hash_alg;
704 memcpy(&digest_list->digests[digest_list->count].digest, final,
705 (u32)alg_to_len(hash_alg));
Ilias Apalodimas754b3a42021-04-22 14:32:14 +0300706 digest_list->count++;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200707 }
708
709 return EFI_SUCCESS;
710}
711
712/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200713 * efi_tcg2_get_capability() - protocol capability information and state information
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200714 *
715 * @this: TCG2 protocol instance
716 * @capability: caller allocated memory with size field to the size of
717 * the structure allocated
718
719 * Return: status code
720 */
721static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200722efi_tcg2_get_capability(struct efi_tcg2_protocol *this,
723 struct efi_tcg2_boot_service_capability *capability)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200724{
725 struct udevice *dev;
726 efi_status_t efi_ret;
727 int ret;
728
729 EFI_ENTRY("%p, %p", this, capability);
730
731 if (!this || !capability) {
732 efi_ret = EFI_INVALID_PARAMETER;
733 goto out;
734 }
735
Masahisa Kojima9cc82932021-09-06 12:04:12 +0900736 if (capability->size < BOOT_SERVICE_CAPABILITY_MIN) {
737 capability->size = BOOT_SERVICE_CAPABILITY_MIN;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200738 efi_ret = EFI_BUFFER_TOO_SMALL;
739 goto out;
740 }
741
742 if (capability->size < sizeof(*capability)) {
743 capability->size = sizeof(*capability);
744 efi_ret = EFI_BUFFER_TOO_SMALL;
745 goto out;
746 }
747
748 capability->structure_version.major = 1;
749 capability->structure_version.minor = 1;
750 capability->protocol_version.major = 1;
751 capability->protocol_version.minor = 1;
752
753 efi_ret = platform_get_tpm2_device(&dev);
754 if (efi_ret != EFI_SUCCESS) {
755 capability->supported_event_logs = 0;
756 capability->hash_algorithm_bitmap = 0;
757 capability->tpm_present_flag = false;
758 capability->max_command_size = 0;
759 capability->max_response_size = 0;
760 capability->manufacturer_id = 0;
761 capability->number_of_pcr_banks = 0;
762 capability->active_pcr_banks = 0;
763
764 efi_ret = EFI_SUCCESS;
765 goto out;
766 }
767
768 /* We only allow a TPMv2 device to register the EFI protocol */
769 capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2;
770
771 capability->tpm_present_flag = true;
772
773 /* Supported and active PCRs */
774 capability->hash_algorithm_bitmap = 0;
775 capability->active_pcr_banks = 0;
776 ret = tpm2_get_pcr_info(dev, &capability->hash_algorithm_bitmap,
777 &capability->active_pcr_banks,
778 &capability->number_of_pcr_banks);
779 if (ret) {
780 efi_ret = EFI_DEVICE_ERROR;
781 goto out;
782 }
783
784 /* Max command size */
785 ret = tpm2_get_max_command_size(dev, &capability->max_command_size);
786 if (ret) {
787 efi_ret = EFI_DEVICE_ERROR;
788 goto out;
789 }
790
791 /* Max response size */
792 ret = tpm2_get_max_response_size(dev, &capability->max_response_size);
793 if (ret) {
794 efi_ret = EFI_DEVICE_ERROR;
795 goto out;
796 }
797
798 /* Manufacturer ID */
799 ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id);
800 if (ret) {
801 efi_ret = EFI_DEVICE_ERROR;
802 goto out;
803 }
804
805 return EFI_EXIT(EFI_SUCCESS);
806out:
807 return EFI_EXIT(efi_ret);
808}
809
810/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200811 * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its
812 * last entry
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200813 *
814 * @this: TCG2 protocol instance
815 * @log_format: type of event log format
816 * @event_log_location: pointer to the memory address of the event log
817 * @event_log_last_entry: pointer to the address of the start of the last
818 * entry in the event log in memory, if log contains
819 * more than 1 entry
820 * @event_log_truncated: set to true, if the Event Log is missing at i
821 * least one entry
822 *
823 * Return: status code
824 */
825static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200826efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this,
827 efi_tcg_event_log_format log_format,
828 u64 *event_log_location, u64 *event_log_last_entry,
829 bool *event_log_truncated)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200830{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200831 efi_status_t ret = EFI_SUCCESS;
832 struct udevice *dev;
833
834 EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location,
835 event_log_last_entry, event_log_truncated);
836
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +0900837 if (!this || !event_log_location || !event_log_last_entry ||
838 !event_log_truncated) {
839 ret = EFI_INVALID_PARAMETER;
840 goto out;
841 }
842
843 /* Only support TPMV2 */
844 if (log_format != TCG2_EVENT_LOG_FORMAT_TCG_2) {
845 ret = EFI_INVALID_PARAMETER;
846 goto out;
847 }
848
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200849 ret = platform_get_tpm2_device(&dev);
850 if (ret != EFI_SUCCESS) {
851 event_log_location = NULL;
852 event_log_last_entry = NULL;
853 *event_log_truncated = false;
854 ret = EFI_SUCCESS;
855 goto out;
856 }
857 *event_log_location = (uintptr_t)event_log.buffer;
858 *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos -
859 event_log.last_event_size);
860 *event_log_truncated = event_log.truncated;
861 event_log.get_event_called = true;
862
863out:
864 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200865}
866
867/**
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900868 * tcg2_hash_pe_image() - calculate PE/COFF image hash
869 *
870 * @efi: pointer to the EFI binary
871 * @efi_size: size of @efi binary
872 * @digest_list: list of digest algorithms to extend
873 *
874 * Return: status code
875 */
876static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size,
877 struct tpml_digest_values *digest_list)
878{
879 WIN_CERTIFICATE *wincerts = NULL;
880 size_t wincerts_len;
881 struct efi_image_regions *regs = NULL;
882 void *new_efi = NULL;
883 u8 hash[TPM2_SHA512_DIGEST_SIZE];
884 efi_status_t ret;
885 u32 active;
886 int i;
887
888 new_efi = efi_prepare_aligned_image(efi, &efi_size);
889 if (!new_efi)
890 return EFI_OUT_OF_RESOURCES;
891
892 if (!efi_image_parse(new_efi, efi_size, &regs, &wincerts,
893 &wincerts_len)) {
894 log_err("Parsing PE executable image failed\n");
895 ret = EFI_UNSUPPORTED;
896 goto out;
897 }
898
899 ret = __get_active_pcr_banks(&active);
900 if (ret != EFI_SUCCESS) {
901 goto out;
902 }
903
904 digest_list->count = 0;
905 for (i = 0; i < MAX_HASH_COUNT; i++) {
906 u16 hash_alg = hash_algo_list[i].hash_alg;
907
908 if (!(active & alg_to_mask(hash_alg)))
909 continue;
910 switch (hash_alg) {
911 case TPM2_ALG_SHA1:
912 hash_calculate("sha1", regs->reg, regs->num, hash);
913 break;
914 case TPM2_ALG_SHA256:
915 hash_calculate("sha256", regs->reg, regs->num, hash);
916 break;
917 case TPM2_ALG_SHA384:
918 hash_calculate("sha384", regs->reg, regs->num, hash);
919 break;
920 case TPM2_ALG_SHA512:
921 hash_calculate("sha512", regs->reg, regs->num, hash);
922 break;
923 default:
924 EFI_PRINT("Unsupported algorithm %x\n", hash_alg);
925 return EFI_INVALID_PARAMETER;
926 }
Ruchika Guptae53007b2021-09-14 12:14:31 +0530927 digest_list->digests[digest_list->count].hash_alg = hash_alg;
928 memcpy(&digest_list->digests[digest_list->count].digest, hash,
929 (u32)alg_to_len(hash_alg));
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900930 digest_list->count++;
931 }
932
933out:
934 if (new_efi != efi)
935 free(new_efi);
936 free(regs);
937
938 return ret;
939}
940
941/**
942 * tcg2_measure_pe_image() - measure PE/COFF image
943 *
944 * @efi: pointer to the EFI binary
945 * @efi_size: size of @efi binary
946 * @handle: loaded image handle
947 * @loaded_image: loaded image protocol
948 *
949 * Return: status code
950 */
951efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size,
952 struct efi_loaded_image_obj *handle,
953 struct efi_loaded_image *loaded_image)
954{
955 struct tpml_digest_values digest_list;
956 efi_status_t ret;
957 struct udevice *dev;
958 u32 pcr_index, event_type, event_size;
959 struct uefi_image_load_event *image_load_event;
960 struct efi_device_path *device_path;
961 u32 device_path_length;
962 IMAGE_DOS_HEADER *dos;
963 IMAGE_NT_HEADERS32 *nt;
964 struct efi_handler *handler;
965
966 ret = platform_get_tpm2_device(&dev);
967 if (ret != EFI_SUCCESS)
968 return ret;
969
970 switch (handle->image_type) {
971 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
972 pcr_index = 4;
973 event_type = EV_EFI_BOOT_SERVICES_APPLICATION;
974 break;
975 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
976 pcr_index = 2;
977 event_type = EV_EFI_BOOT_SERVICES_DRIVER;
978 break;
979 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
980 pcr_index = 2;
981 event_type = EV_EFI_RUNTIME_SERVICES_DRIVER;
982 break;
983 default:
984 return EFI_UNSUPPORTED;
985 }
986
987 ret = tcg2_hash_pe_image(efi, efi_size, &digest_list);
988 if (ret != EFI_SUCCESS)
989 return ret;
990
991 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
992 if (ret != EFI_SUCCESS)
993 return ret;
994
Ilias Apalodimas26753c02021-09-09 00:30:49 +0300995 ret = efi_search_protocol(&handle->header,
996 &efi_guid_loaded_image_device_path, &handler);
Masahisa Kojima70be5a62021-05-26 12:09:58 +0900997 if (ret != EFI_SUCCESS)
998 return ret;
999
Ilias Apalodimas26753c02021-09-09 00:30:49 +03001000 device_path = handler->protocol_interface;
Masahisa Kojima70be5a62021-05-26 12:09:58 +09001001 device_path_length = efi_dp_size(device_path);
1002 if (device_path_length > 0) {
1003 /* add end node size */
1004 device_path_length += sizeof(struct efi_device_path);
1005 }
1006 event_size = sizeof(struct uefi_image_load_event) + device_path_length;
Ilias Apalodimas26753c02021-09-09 00:30:49 +03001007 image_load_event = calloc(1, event_size);
Masahisa Kojima70be5a62021-05-26 12:09:58 +09001008 if (!image_load_event)
1009 return EFI_OUT_OF_RESOURCES;
1010
1011 image_load_event->image_location_in_memory = (uintptr_t)efi;
1012 image_load_event->image_length_in_memory = efi_size;
1013 image_load_event->length_of_device_path = device_path_length;
1014
1015 dos = (IMAGE_DOS_HEADER *)efi;
1016 nt = (IMAGE_NT_HEADERS32 *)(efi + dos->e_lfanew);
1017 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
1018 IMAGE_NT_HEADERS64 *nt64 = (IMAGE_NT_HEADERS64 *)nt;
1019
1020 image_load_event->image_link_time_address =
1021 nt64->OptionalHeader.ImageBase;
1022 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
1023 image_load_event->image_link_time_address =
1024 nt->OptionalHeader.ImageBase;
1025 } else {
1026 ret = EFI_INVALID_PARAMETER;
1027 goto out;
1028 }
1029
Ilias Apalodimas26753c02021-09-09 00:30:49 +03001030 /* device_path_length might be zero */
1031 memcpy(image_load_event->device_path, device_path, device_path_length);
Masahisa Kojima70be5a62021-05-26 12:09:58 +09001032
1033 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
1034 event_size, (u8 *)image_load_event);
1035
1036out:
1037 free(image_load_event);
1038
1039 return ret;
1040}
1041
1042/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +02001043 * efi_tcg2_hash_log_extend_event() - extend and optionally log events
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001044 *
1045 * @this: TCG2 protocol instance
1046 * @flags: bitmap providing additional information on the
1047 * operation
1048 * @data_to_hash: physical address of the start of the data buffer
1049 * to be hashed
1050 * @data_to_hash_len: the length in bytes of the buffer referenced by
1051 * data_to_hash
1052 * @efi_tcg_event: pointer to data buffer containing information
1053 * about the event
1054 *
1055 * Return: status code
1056 */
1057static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +02001058efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags,
1059 u64 data_to_hash, u64 data_to_hash_len,
1060 struct efi_tcg2_event *efi_tcg_event)
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001061{
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001062 struct udevice *dev;
1063 efi_status_t ret;
1064 u32 event_type, pcr_index, event_size;
1065 struct tpml_digest_values digest_list;
1066
1067 EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash,
1068 data_to_hash_len, efi_tcg_event);
1069
1070 if (!this || !data_to_hash || !efi_tcg_event) {
1071 ret = EFI_INVALID_PARAMETER;
1072 goto out;
1073 }
1074
1075 ret = platform_get_tpm2_device(&dev);
1076 if (ret != EFI_SUCCESS)
1077 goto out;
1078
1079 if (efi_tcg_event->size < efi_tcg_event->header.header_size +
1080 sizeof(u32)) {
1081 ret = EFI_INVALID_PARAMETER;
1082 goto out;
1083 }
1084
Masahisa Kojimab8074912021-09-03 10:55:52 +09001085 if (efi_tcg_event->header.pcr_index > EFI_TCG2_MAX_PCR_INDEX) {
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001086 ret = EFI_INVALID_PARAMETER;
1087 goto out;
1088 }
1089
1090 /*
1091 * if PE_COFF_IMAGE is set we need to make sure the image is not
1092 * corrupted, verify it and hash the PE/COFF image in accordance with
Masahisa Kojima70be5a62021-05-26 12:09:58 +09001093 * the procedure specified in "Calculating the PE Image Hash"
1094 * section of the "Windows Authenticode Portable Executable Signature
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001095 * Format"
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001096 */
1097 if (flags & PE_COFF_IMAGE) {
Masahisa Kojima70be5a62021-05-26 12:09:58 +09001098 IMAGE_NT_HEADERS32 *nt;
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001099
Masahisa Kojima70be5a62021-05-26 12:09:58 +09001100 ret = efi_check_pe((void *)(uintptr_t)data_to_hash,
1101 data_to_hash_len, (void **)&nt);
1102 if (ret != EFI_SUCCESS) {
1103 log_err("Not a valid PE-COFF file\n");
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +09001104 ret = EFI_UNSUPPORTED;
Masahisa Kojima70be5a62021-05-26 12:09:58 +09001105 goto out;
1106 }
1107 ret = tcg2_hash_pe_image((void *)(uintptr_t)data_to_hash,
1108 data_to_hash_len, &digest_list);
1109 } else {
1110 ret = tcg2_create_digest((u8 *)(uintptr_t)data_to_hash,
1111 data_to_hash_len, &digest_list);
1112 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001113
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001114 if (ret != EFI_SUCCESS)
1115 goto out;
1116
Masahisa Kojima70be5a62021-05-26 12:09:58 +09001117 pcr_index = efi_tcg_event->header.pcr_index;
1118 event_type = efi_tcg_event->header.event_type;
1119
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001120 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
1121 if (ret != EFI_SUCCESS)
1122 goto out;
1123
1124 if (flags & EFI_TCG2_EXTEND_ONLY) {
1125 if (event_log.truncated)
1126 ret = EFI_VOLUME_FULL;
1127 goto out;
1128 }
1129
1130 /*
1131 * The efi_tcg_event size includes the size component and the
1132 * headersize
1133 */
1134 event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) -
1135 efi_tcg_event->header.header_size;
1136 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
1137 event_size, efi_tcg_event->event);
1138out:
1139 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001140}
1141
1142/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +02001143 * efi_tcg2_submit_command() - Send command to the TPM
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001144 *
1145 * @this: TCG2 protocol instance
1146 * @input_param_block_size: size of the TPM input parameter block
1147 * @input_param_block: pointer to the TPM input parameter block
1148 * @output_param_block_size: size of the TPM output parameter block
1149 * @output_param_block: pointer to the TPM output parameter block
1150 *
1151 * Return: status code
1152 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001153static efi_status_t EFIAPI
Masahisa Kojima06ef6b62021-11-04 22:59:16 +09001154efi_tcg2_submit_command(struct efi_tcg2_protocol *this,
1155 u32 input_param_block_size,
1156 u8 *input_param_block,
1157 u32 output_param_block_size,
1158 u8 *output_param_block)
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001159{
Masahisa Kojima06ef6b62021-11-04 22:59:16 +09001160 struct udevice *dev;
1161 efi_status_t ret;
1162 u32 rc;
1163 size_t resp_buf_size = output_param_block_size;
1164
1165 EFI_ENTRY("%p, %u, %p, %u, %p", this, input_param_block_size,
1166 input_param_block, output_param_block_size, output_param_block);
1167
1168 if (!this || !input_param_block || !input_param_block_size) {
1169 ret = EFI_INVALID_PARAMETER;
1170 goto out;
1171 }
1172
1173 ret = platform_get_tpm2_device(&dev);
1174 if (ret != EFI_SUCCESS)
1175 goto out;
1176
1177 rc = tpm2_submit_command(dev, input_param_block,
1178 output_param_block, &resp_buf_size);
1179 if (rc) {
1180 ret = (rc == -ENOSPC) ? EFI_OUT_OF_RESOURCES : EFI_DEVICE_ERROR;
1181
1182 goto out;
1183 }
1184
1185out:
1186 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001187}
1188
1189/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +02001190 * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001191 *
1192 * @this: TCG2 protocol instance
1193 * @active_pcr_banks: pointer for receiving the bitmap of currently
1194 * active PCR banks
1195 *
1196 * Return: status code
1197 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001198static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +02001199efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
1200 u32 *active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001201{
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001202 efi_status_t ret;
1203
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +09001204 if (!this || !active_pcr_banks) {
1205 ret = EFI_INVALID_PARAMETER;
1206 goto out;
1207 }
1208
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001209 EFI_ENTRY("%p, %p", this, active_pcr_banks);
1210 ret = __get_active_pcr_banks(active_pcr_banks);
1211
Masahisa Kojima7c5fccd2021-09-03 10:55:50 +09001212out:
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001213 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001214}
1215
1216/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +02001217 * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001218 *
1219 * @this: TCG2 protocol instance
1220 * @active_pcr_banks: bitmap of the requested active PCR banks
1221 *
1222 * Return: status code
1223 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001224static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +03001225efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
1226 u32 __maybe_unused active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001227{
1228 return EFI_UNSUPPORTED;
1229}
1230
1231/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +02001232 * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous
1233 * set_active_pcr_banks()
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001234 *
1235 * @this: TCG2 protocol instance
1236 * @operation_present: non-zero value to indicate a
1237 * set_active_pcr_banks operation was
1238 * invoked during last boot
1239 * @response: result value could be returned
1240 *
1241 * Return: status code
1242 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001243static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +03001244efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
1245 u32 __maybe_unused *operation_present,
1246 u32 __maybe_unused *response)
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001247{
1248 return EFI_UNSUPPORTED;
1249}
1250
1251static const struct efi_tcg2_protocol efi_tcg2_protocol = {
Ilias Apalodimasc67fef62020-11-16 08:52:41 +02001252 .get_capability = efi_tcg2_get_capability,
1253 .get_eventlog = efi_tcg2_get_eventlog,
1254 .hash_log_extend_event = efi_tcg2_hash_log_extend_event,
1255 .submit_command = efi_tcg2_submit_command,
1256 .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks,
1257 .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks,
1258 .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks,
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001259};
1260
1261/**
Ruchika Guptabc9495c2021-11-29 13:09:44 +05301262 * parse_event_log_header() - Parse and verify the event log header fields
1263 *
1264 * @buffer: Pointer to the start of the eventlog
1265 * @size: Size of the eventlog
1266 * @pos: Return offset of the next event in buffer right
1267 * after the event header i.e specID
1268 *
1269 * Return: status code
1270 */
1271static efi_status_t parse_event_log_header(void *buffer, u32 size, u32 *pos)
1272{
1273 struct tcg_pcr_event *event_header = (struct tcg_pcr_event *)buffer;
1274 int i = 0;
1275
1276 if (size < sizeof(*event_header))
1277 return EFI_COMPROMISED_DATA;
1278
1279 if (get_unaligned_le32(&event_header->pcr_index) != 0 ||
1280 get_unaligned_le32(&event_header->event_type) != EV_NO_ACTION)
1281 return EFI_COMPROMISED_DATA;
1282
1283 for (i = 0; i < sizeof(event_header->digest); i++) {
1284 if (event_header->digest[i])
1285 return EFI_COMPROMISED_DATA;
1286 }
1287
1288 *pos += sizeof(*event_header);
1289
1290 return EFI_SUCCESS;
1291}
1292
1293/**
1294 * parse_specid_event() - Parse and verify the specID Event in the eventlog
1295 *
1296 * @dev: udevice
1297 * @buffer: Pointer to the start of the eventlog
1298 * @log_size: Size of the eventlog
1299 * @pos: [in] Offset of specID event in the eventlog buffer
1300 * [out] Return offset of the next event in the buffer
1301 * after the specID
1302 * @digest_list: list of digests in the event
1303 *
1304 * Return: status code
1305 * @pos Offset in the eventlog where the specID event ends
1306 * @digest_list: list of digests in the event
1307 */
1308static efi_status_t parse_specid_event(struct udevice *dev, void *buffer,
1309 u32 log_size, u32 *pos,
1310 struct tpml_digest_values *digest_list)
1311{
1312 struct tcg_efi_spec_id_event *spec_event;
1313 struct tcg_pcr_event *event_header = (struct tcg_pcr_event *)buffer;
1314 size_t spec_event_size;
1315 u32 active = 0, supported = 0, pcr_count = 0, alg_count = 0;
1316 u32 spec_active = 0;
1317 u16 hash_alg;
1318 u8 vendor_sz;
1319 int err, i;
1320
1321 if (*pos >= log_size || (*pos + sizeof(*spec_event)) > log_size)
1322 return EFI_COMPROMISED_DATA;
1323
1324 /* Check specID event data */
1325 spec_event = (struct tcg_efi_spec_id_event *)((uintptr_t)buffer + *pos);
1326 /* Check for signature */
1327 if (memcmp(spec_event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03,
1328 sizeof(TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03))) {
1329 log_err("specID Event: Signature mismatch\n");
1330 return EFI_COMPROMISED_DATA;
1331 }
1332
1333 if (spec_event->spec_version_minor !=
1334 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2 ||
1335 spec_event->spec_version_major !=
1336 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2)
1337 return EFI_COMPROMISED_DATA;
1338
1339 if (spec_event->number_of_algorithms > MAX_HASH_COUNT ||
1340 spec_event->number_of_algorithms < 1) {
1341 log_err("specID Event: Number of algorithms incorrect\n");
1342 return EFI_COMPROMISED_DATA;
1343 }
1344
1345 alg_count = spec_event->number_of_algorithms;
1346
1347 err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_count);
1348 if (err)
1349 return EFI_DEVICE_ERROR;
1350
1351 digest_list->count = 0;
1352 /*
1353 * We have to take care that the sequence of algorithms that we record
1354 * in digest_list matches the sequence in eventlog.
1355 */
1356 for (i = 0; i < alg_count; i++) {
1357 hash_alg =
1358 get_unaligned_le16(&spec_event->digest_sizes[i].algorithm_id);
1359
1360 if (!(supported & alg_to_mask(hash_alg))) {
1361 log_err("specID Event: Unsupported algorithm\n");
1362 return EFI_COMPROMISED_DATA;
1363 }
1364 digest_list->digests[digest_list->count++].hash_alg = hash_alg;
1365
1366 spec_active |= alg_to_mask(hash_alg);
1367 }
1368
1369 /*
1370 * TCG specification expects the event log to have hashes for all
1371 * active PCR's
1372 */
1373 if (spec_active != active) {
1374 /*
1375 * Previous stage bootloader should know all the active PCR's
1376 * and use them in the Eventlog.
1377 */
1378 log_err("specID Event: All active hash alg not present\n");
1379 return EFI_COMPROMISED_DATA;
1380 }
1381
1382 /*
1383 * the size of the spec event and placement of vendor_info_size
1384 * depends on supported algoriths
1385 */
1386 spec_event_size =
1387 offsetof(struct tcg_efi_spec_id_event, digest_sizes) +
1388 alg_count * sizeof(spec_event->digest_sizes[0]);
1389
1390 if (*pos + spec_event_size >= log_size)
1391 return EFI_COMPROMISED_DATA;
1392
1393 vendor_sz = *(uint8_t *)((uintptr_t)buffer + *pos + spec_event_size);
1394
1395 spec_event_size += sizeof(vendor_sz) + vendor_sz;
1396 *pos += spec_event_size;
1397
1398 if (get_unaligned_le32(&event_header->event_size) != spec_event_size) {
1399 log_err("specID event: header event size mismatch\n");
1400 /* Right way to handle this can be to call SetActive PCR's */
1401 return EFI_COMPROMISED_DATA;
1402 }
1403
1404 return EFI_SUCCESS;
1405}
1406
1407/**
1408 * tcg2_parse_event() - Parse the event in the eventlog
1409 *
1410 * @dev: udevice
1411 * @buffer: Pointer to the start of the eventlog
1412 * @log_size: Size of the eventlog
1413 * @offset: [in] Offset of the event in the eventlog buffer
1414 * [out] Return offset of the next event in the buffer
1415 * @digest_list: list of digests in the event
1416 * @pcr Index of the PCR in the event
1417 *
1418 * Return: status code
1419 */
1420static efi_status_t tcg2_parse_event(struct udevice *dev, void *buffer,
1421 u32 log_size, u32 *offset,
1422 struct tpml_digest_values *digest_list,
1423 u32 *pcr)
1424{
1425 struct tcg_pcr_event2 *event = NULL;
1426 u32 count, size, event_size;
1427 size_t pos;
1428
1429 event_size = tcg_event_final_size(digest_list);
1430 if (*offset >= log_size || *offset + event_size > log_size) {
1431 log_err("Event exceeds log size\n");
1432 return EFI_COMPROMISED_DATA;
1433 }
1434
1435 event = (struct tcg_pcr_event2 *)((uintptr_t)buffer + *offset);
1436 *pcr = get_unaligned_le32(&event->pcr_index);
1437
1438 /* get the count */
1439 count = get_unaligned_le32(&event->digests.count);
1440 if (count != digest_list->count)
1441 return EFI_COMPROMISED_DATA;
1442
1443 pos = offsetof(struct tcg_pcr_event2, digests);
1444 pos += offsetof(struct tpml_digest_values, digests);
1445
1446 for (int i = 0; i < digest_list->count; i++) {
1447 u16 alg;
1448 u16 hash_alg = digest_list->digests[i].hash_alg;
1449 u8 *digest = (u8 *)&digest_list->digests[i].digest;
1450
1451 alg = get_unaligned_le16((void *)((uintptr_t)event + pos));
1452
1453 if (alg != hash_alg)
1454 return EFI_COMPROMISED_DATA;
1455
1456 pos += offsetof(struct tpmt_ha, digest);
1457 memcpy(digest, (void *)((uintptr_t)event + pos), alg_to_len(hash_alg));
1458 pos += alg_to_len(hash_alg);
1459 }
1460
1461 size = get_unaligned_le32((void *)((uintptr_t)event + pos));
1462 event_size += size;
1463 pos += sizeof(u32); /* tcg_pcr_event2 event_size*/
1464 pos += size;
1465
1466 /* make sure the calculated buffer is what we checked against */
1467 if (pos != event_size)
1468 return EFI_COMPROMISED_DATA;
1469
1470 if (pos > log_size)
1471 return EFI_COMPROMISED_DATA;
1472
1473 *offset += pos;
1474
1475 return EFI_SUCCESS;
1476}
1477
1478/**
1479 * tcg2_get_fw_eventlog() - Get the eventlog address and size
1480 *
1481 * If the previous firmware has passed some eventlog, this function get it's
1482 * location and check for it's validity.
1483 *
1484 * @dev: udevice
1485 * @log_buffer: eventlog address
1486 * @log_sz: eventlog size
1487 *
1488 * Return: status code
1489 */
1490static efi_status_t tcg2_get_fw_eventlog(struct udevice *dev, void *log_buffer,
1491 size_t *log_sz)
1492{
1493 struct tpml_digest_values digest_list;
1494 void *buffer;
1495 efi_status_t ret;
1496 u32 pcr, pos;
1497 u64 base;
1498 u32 sz;
Ruchika Gupta9d0b5d02021-11-29 13:09:46 +05301499 bool extend_pcr = false;
1500 int i;
Ruchika Guptabc9495c2021-11-29 13:09:44 +05301501
1502 ret = platform_get_eventlog(dev, &base, &sz);
1503 if (ret != EFI_SUCCESS)
1504 return ret;
1505
1506 if (sz > TPM2_EVENT_LOG_SIZE)
1507 return EFI_VOLUME_FULL;
1508
1509 buffer = (void *)(uintptr_t)base;
1510 pos = 0;
1511 /* Parse the eventlog to check for its validity */
1512 ret = parse_event_log_header(buffer, sz, &pos);
1513 if (ret)
1514 return ret;
1515
1516 ret = parse_specid_event(dev, buffer, sz, &pos, &digest_list);
1517 if (ret) {
1518 log_err("Error parsing SPEC ID Event\n");
1519 return ret;
1520 }
1521
Ruchika Gupta9d0b5d02021-11-29 13:09:46 +05301522 ret = tcg2_pcr_read(dev, 0, &digest_list);
1523 if (ret) {
1524 log_err("Error reading PCR 0\n");
1525 return ret;
1526 }
1527
1528 /*
1529 * If PCR0 is 0, previous firmware didn't have the capability
1530 * to extend the PCR. In this scenario, extend the PCR as
1531 * the eventlog is parsed.
1532 */
1533 for (i = 0; i < digest_list.count; i++) {
1534 u8 hash_buf[TPM2_SHA512_DIGEST_SIZE] = { 0 };
1535 u16 hash_alg = digest_list.digests[i].hash_alg;
1536
1537 if (!memcmp((u8 *)&digest_list.digests[i].digest, hash_buf,
1538 alg_to_len(hash_alg)))
1539 extend_pcr = true;
1540 }
1541
Ruchika Guptabc9495c2021-11-29 13:09:44 +05301542 while (pos < sz) {
1543 ret = tcg2_parse_event(dev, buffer, sz, &pos, &digest_list,
1544 &pcr);
1545 if (ret) {
1546 log_err("Error parsing event\n");
1547 return ret;
1548 }
Ruchika Gupta9d0b5d02021-11-29 13:09:46 +05301549 if (extend_pcr) {
1550 ret = tcg2_pcr_extend(dev, pcr, &digest_list);
1551 if (ret != EFI_SUCCESS) {
1552 log_err("Error in extending PCR\n");
1553 return ret;
1554 }
1555
1556 /* Clear the digest for next event */
1557 for (i = 0; i < digest_list.count; i++) {
1558 u16 hash_alg = digest_list.digests[i].hash_alg;
1559 u8 *digest =
1560 (u8 *)&digest_list.digests[i].digest;
1561
1562 memset(digest, 0, alg_to_len(hash_alg));
1563 }
1564 }
Ruchika Guptabc9495c2021-11-29 13:09:44 +05301565 }
1566
1567 memcpy(log_buffer, buffer, sz);
1568 *log_sz = sz;
1569
1570 return ret;
1571}
1572
1573/**
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001574 * create_specid_event() - Create the first event in the eventlog
1575 *
1576 * @dev: tpm device
1577 * @event_header: Pointer to the final event header
1578 * @event_size: final spec event size
1579 *
1580 * Return: status code
1581 */
1582static efi_status_t create_specid_event(struct udevice *dev, void *buffer,
1583 size_t *event_size)
1584{
1585 struct tcg_efi_spec_id_event *spec_event;
1586 size_t spec_event_size;
1587 efi_status_t ret = EFI_DEVICE_ERROR;
Ruchika Guptae53007b2021-09-14 12:14:31 +05301588 u32 active = 0, supported = 0, pcr_count = 0, alg_count = 0;
Ilias Apalodimas190b0a22021-05-25 14:35:31 +03001589 int err;
1590 size_t i;
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001591
1592 /*
1593 * Create Spec event. This needs to be the first event in the log
1594 * according to the TCG EFI protocol spec
1595 */
1596
1597 /* Setup specID event data */
1598 spec_event = (struct tcg_efi_spec_id_event *)buffer;
1599 memcpy(spec_event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03,
1600 sizeof(spec_event->signature));
1601 put_unaligned_le32(0, &spec_event->platform_class); /* type client */
1602 spec_event->spec_version_minor =
1603 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2;
1604 spec_event->spec_version_major =
1605 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2;
1606 spec_event->spec_errata =
1607 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_ERRATA_TPM2;
1608 spec_event->uintn_size = sizeof(efi_uintn_t) / sizeof(u32);
1609
Ruchika Guptae53007b2021-09-14 12:14:31 +05301610 err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_count);
1611
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001612 if (err)
1613 goto out;
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001614
Ruchika Guptae53007b2021-09-14 12:14:31 +05301615 for (i = 0; i < pcr_count; i++) {
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001616 u16 hash_alg = hash_algo_list[i].hash_alg;
1617 u16 hash_len = hash_algo_list[i].hash_len;
1618
Ruchika Guptae53007b2021-09-14 12:14:31 +05301619 if (active & alg_to_mask(hash_alg)) {
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001620 put_unaligned_le16(hash_alg,
Ruchika Guptae53007b2021-09-14 12:14:31 +05301621 &spec_event->digest_sizes[alg_count].algorithm_id);
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001622 put_unaligned_le16(hash_len,
Ruchika Guptae53007b2021-09-14 12:14:31 +05301623 &spec_event->digest_sizes[alg_count].digest_size);
1624 alg_count++;
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001625 }
1626 }
Ruchika Guptae53007b2021-09-14 12:14:31 +05301627
1628 spec_event->number_of_algorithms = alg_count;
1629 if (spec_event->number_of_algorithms > MAX_HASH_COUNT ||
1630 spec_event->number_of_algorithms < 1)
1631 goto out;
1632
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001633 /*
1634 * the size of the spec event and placement of vendor_info_size
1635 * depends on supported algoriths
1636 */
1637 spec_event_size =
1638 offsetof(struct tcg_efi_spec_id_event, digest_sizes) +
1639 spec_event->number_of_algorithms * sizeof(spec_event->digest_sizes[0]);
1640 /* no vendor info for us */
Ruchika Guptae53007b2021-09-14 12:14:31 +05301641 memset(buffer + spec_event_size, 0, 1);
1642 /* add a byte for vendor_info_size in the spec event */
1643 spec_event_size += 1;
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001644 *event_size = spec_event_size;
1645
1646 return EFI_SUCCESS;
1647
1648out:
1649 return ret;
1650}
1651
1652/**
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001653 * tcg2_uninit - remove the final event table and free efi memory on failures
1654 */
1655void tcg2_uninit(void)
1656{
1657 efi_status_t ret;
1658
1659 ret = efi_install_configuration_table(&efi_guid_final_events, NULL);
1660 if (ret != EFI_SUCCESS)
1661 log_err("Failed to delete final events config table\n");
1662
1663 efi_free_pool(event_log.buffer);
1664 event_log.buffer = NULL;
1665 efi_free_pool(event_log.final_buffer);
1666 event_log.final_buffer = NULL;
1667}
1668
1669/**
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001670 * create_final_event() - Create the final event and install the config
1671 * defined by the TCG EFI spec
1672 */
1673static efi_status_t create_final_event(void)
1674{
1675 struct efi_tcg2_final_events_table *final_event;
1676 efi_status_t ret;
1677
1678 /*
1679 * All events generated after the invocation of
1680 * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an
1681 * EFI_CONFIGURATION_TABLE
1682 */
1683 ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE,
1684 &event_log.final_buffer);
1685 if (ret != EFI_SUCCESS)
1686 goto out;
1687
1688 memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE);
1689 final_event = event_log.final_buffer;
1690 final_event->number_of_events = 0;
1691 final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
1692 event_log.final_pos = sizeof(*final_event);
1693 ret = efi_install_configuration_table(&efi_guid_final_events,
1694 final_event);
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001695 if (ret != EFI_SUCCESS) {
1696 efi_free_pool(event_log.final_buffer);
1697 event_log.final_buffer = NULL;
1698 }
1699
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001700out:
1701 return ret;
1702}
1703
1704/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001705 * tcg2_measure_event() - common function to add event log and extend PCR
1706 *
1707 * @dev: TPM device
1708 * @pcr_index: PCR index
1709 * @event_type: type of event added
1710 * @size: event size
1711 * @event: event data
1712 *
1713 * Return: status code
1714 */
1715static efi_status_t
1716tcg2_measure_event(struct udevice *dev, u32 pcr_index, u32 event_type,
1717 u32 size, u8 event[])
1718{
1719 struct tpml_digest_values digest_list;
1720 efi_status_t ret;
1721
1722 ret = tcg2_create_digest(event, size, &digest_list);
1723 if (ret != EFI_SUCCESS)
1724 goto out;
1725
1726 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
1727 if (ret != EFI_SUCCESS)
1728 goto out;
1729
1730 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
1731 size, event);
1732
1733out:
1734 return ret;
1735}
1736
1737/**
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +02001738 * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the
1739 * eventlog and extend the PCRs
1740 *
1741 * @dev: TPM device
1742 *
1743 * @Return: status code
1744 */
1745static efi_status_t efi_append_scrtm_version(struct udevice *dev)
1746{
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +02001747 efi_status_t ret;
1748
Pali Rohár144d6422021-08-02 15:18:30 +02001749 ret = tcg2_measure_event(dev, 0, EV_S_CRTM_VERSION,
1750 strlen(version_string) + 1,
1751 (u8 *)version_string);
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +02001752
Ruchika Guptabc9495c2021-11-29 13:09:44 +05301753 return ret;
1754}
1755
1756/**
1757 * efi_init_event_log() - initialize an eventlog
1758 *
1759 * Return: status code
1760 */
1761static efi_status_t efi_init_event_log(void)
1762{
1763 /*
1764 * vendor_info_size is currently set to 0, we need to change the length
1765 * and allocate the flexible array member if this changes
1766 */
1767 struct tcg_pcr_event *event_header = NULL;
1768 struct udevice *dev;
1769 size_t spec_event_size;
1770 efi_status_t ret;
1771
1772 ret = platform_get_tpm2_device(&dev);
1773 if (ret != EFI_SUCCESS)
1774 return ret;
1775
1776 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE,
1777 (void **)&event_log.buffer);
1778 if (ret != EFI_SUCCESS)
1779 return ret;
1780
1781 /*
1782 * initialize log area as 0xff so the OS can easily figure out the
1783 * last log entry
1784 */
1785 memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE);
1786
1787 /*
1788 * The log header is defined to be in SHA1 event log entry format.
1789 * Setup event header
1790 */
1791 event_header = (struct tcg_pcr_event *)event_log.buffer;
1792 event_log.pos = 0;
1793 event_log.last_event_size = 0;
1794 event_log.get_event_called = false;
1795 event_log.ebs_called = false;
1796 event_log.truncated = false;
1797
1798 /*
1799 * Check if earlier firmware have passed any eventlog. Different
1800 * platforms can use different ways to do so.
1801 */
1802 ret = tcg2_get_fw_eventlog(dev, event_log.buffer, &event_log.pos);
1803 /*
1804 * If earlier firmware hasn't passed any eventlog, go ahead and
1805 * create the eventlog header.
1806 */
1807 if (ret == EFI_NOT_FOUND) {
1808 put_unaligned_le32(0, &event_header->pcr_index);
1809 put_unaligned_le32(EV_NO_ACTION, &event_header->event_type);
1810 memset(&event_header->digest, 0, sizeof(event_header->digest));
1811 ret = create_specid_event(dev,
1812 (void *)((uintptr_t)event_log.buffer +
1813 sizeof(*event_header)),
1814 &spec_event_size);
1815 if (ret != EFI_SUCCESS)
1816 goto free_pool;
1817 put_unaligned_le32(spec_event_size, &event_header->event_size);
1818 event_log.pos = spec_event_size + sizeof(*event_header);
1819 event_log.last_event_size = event_log.pos;
1820
1821 /*
1822 * Add SCRTM version to the log if previous firmmware
1823 * doesn't pass an eventlog.
1824 */
1825 ret = efi_append_scrtm_version(dev);
1826 }
1827
1828 if (ret != EFI_SUCCESS)
1829 goto free_pool;
1830
1831 ret = create_final_event();
1832 if (ret != EFI_SUCCESS)
1833 goto free_pool;
1834
1835 return ret;
1836
1837free_pool:
1838 efi_free_pool(event_log.buffer);
1839 event_log.buffer = NULL;
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +02001840 return ret;
1841}
1842
1843/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001844 * tcg2_measure_variable() - add variable event log and extend PCR
1845 *
1846 * @dev: TPM device
1847 * @pcr_index: PCR index
1848 * @event_type: type of event added
1849 * @var_name: variable name
1850 * @guid: guid
1851 * @data_size: variable data size
1852 * @data: variable data
1853 *
1854 * Return: status code
1855 */
1856static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index,
Heinrich Schuchardt1ad2f0d2021-09-09 07:12:14 +02001857 u32 event_type, const u16 *var_name,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09001858 const efi_guid_t *guid,
1859 efi_uintn_t data_size, u8 *data)
1860{
1861 u32 event_size;
1862 efi_status_t ret;
1863 struct efi_tcg2_uefi_variable_data *event;
1864
1865 event_size = sizeof(event->variable_name) +
1866 sizeof(event->unicode_name_length) +
1867 sizeof(event->variable_data_length) +
1868 (u16_strlen(var_name) * sizeof(u16)) + data_size;
1869 event = malloc(event_size);
1870 if (!event)
1871 return EFI_OUT_OF_RESOURCES;
1872
1873 guidcpy(&event->variable_name, guid);
1874 event->unicode_name_length = u16_strlen(var_name);
1875 event->variable_data_length = data_size;
1876 memcpy(event->unicode_name, var_name,
1877 (event->unicode_name_length * sizeof(u16)));
1878 if (data) {
1879 memcpy((u16 *)event->unicode_name + event->unicode_name_length,
1880 data, data_size);
1881 }
1882 ret = tcg2_measure_event(dev, pcr_index, event_type, event_size,
1883 (u8 *)event);
1884 free(event);
1885 return ret;
1886}
1887
1888/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001889 * tcg2_measure_boot_variable() - measure boot variables
1890 *
1891 * @dev: TPM device
1892 *
1893 * Return: status code
1894 */
1895static efi_status_t tcg2_measure_boot_variable(struct udevice *dev)
1896{
1897 u16 *boot_order;
1898 u16 *boot_index;
1899 u16 var_name[] = L"BootOrder";
1900 u16 boot_name[] = L"Boot####";
1901 u8 *bootvar;
1902 efi_uintn_t var_data_size;
1903 u32 count, i;
1904 efi_status_t ret;
1905
1906 boot_order = efi_get_var(var_name, &efi_global_variable_guid,
1907 &var_data_size);
1908 if (!boot_order) {
Masahisa Kojimad1325932021-11-09 18:44:54 +09001909 /* If "BootOrder" is not defined, skip the boot variable measurement */
1910 return EFI_SUCCESS;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001911 }
1912
1913 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name,
1914 &efi_global_variable_guid, var_data_size,
1915 (u8 *)boot_order);
1916 if (ret != EFI_SUCCESS)
1917 goto error;
1918
1919 count = var_data_size / sizeof(*boot_order);
1920 boot_index = boot_order;
1921 for (i = 0; i < count; i++) {
1922 efi_create_indexed_name(boot_name, sizeof(boot_name),
1923 "Boot", *boot_index++);
1924
1925 bootvar = efi_get_var(boot_name, &efi_global_variable_guid,
1926 &var_data_size);
1927
1928 if (!bootvar) {
Masahisa Kojimaaca20c82021-11-09 20:35:53 +09001929 log_debug("%ls not found\n", boot_name);
Masahisa Kojima8173cd42021-08-13 16:12:40 +09001930 continue;
1931 }
1932
1933 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2,
1934 boot_name,
1935 &efi_global_variable_guid,
1936 var_data_size, bootvar);
1937 free(bootvar);
1938 if (ret != EFI_SUCCESS)
1939 goto error;
1940 }
1941
1942error:
1943 free(boot_order);
1944 return ret;
1945}
1946
1947/**
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09001948 * tcg2_measure_smbios() - measure smbios table
1949 *
1950 * @dev: TPM device
1951 * @entry: pointer to the smbios_entry structure
1952 *
1953 * Return: status code
1954 */
1955static efi_status_t
1956tcg2_measure_smbios(struct udevice *dev,
1957 const struct smbios_entry *entry)
1958{
1959 efi_status_t ret;
1960 struct smbios_header *smbios_copy;
1961 struct smbios_handoff_table_pointers2 *event = NULL;
1962 u32 event_size;
1963
1964 /*
1965 * TCG PC Client PFP Spec says
1966 * "SMBIOS structures that contain static configuration information
1967 * (e.g. Platform Manufacturer Enterprise Number assigned by IANA,
1968 * platform model number, Vendor and Device IDs for each SMBIOS table)
1969 * that is relevant to the security of the platform MUST be measured".
1970 * Device dependent parameters such as serial number are cleared to
1971 * zero or spaces for the measurement.
1972 */
1973 event_size = sizeof(struct smbios_handoff_table_pointers2) +
1974 FIELD_SIZEOF(struct efi_configuration_table, guid) +
1975 entry->struct_table_length;
1976 event = calloc(1, event_size);
1977 if (!event) {
1978 ret = EFI_OUT_OF_RESOURCES;
1979 goto out;
1980 }
1981
1982 event->table_description_size = sizeof(SMBIOS_HANDOFF_TABLE_DESC);
1983 memcpy(event->table_description, SMBIOS_HANDOFF_TABLE_DESC,
1984 sizeof(SMBIOS_HANDOFF_TABLE_DESC));
1985 put_unaligned_le64(1, &event->number_of_tables);
1986 guidcpy(&event->table_entry[0].guid, &smbios_guid);
1987 smbios_copy = (struct smbios_header *)((uintptr_t)&event->table_entry[0].table);
1988 memcpy(&event->table_entry[0].table,
1989 (void *)((uintptr_t)entry->struct_table_address),
1990 entry->struct_table_length);
1991
1992 smbios_prepare_measurement(entry, smbios_copy);
1993
1994 ret = tcg2_measure_event(dev, 1, EV_EFI_HANDOFF_TABLES2, event_size,
1995 (u8 *)event);
1996 if (ret != EFI_SUCCESS)
1997 goto out;
1998
1999out:
2000 free(event);
2001
2002 return ret;
2003}
2004
2005/**
2006 * find_smbios_table() - find smbios table
2007 *
2008 * Return: pointer to the smbios table
2009 */
2010static void *find_smbios_table(void)
2011{
2012 u32 i;
2013
2014 for (i = 0; i < systab.nr_tables; i++) {
2015 if (!guidcmp(&smbios_guid, &systab.tables[i].guid))
2016 return systab.tables[i].table;
2017 }
2018
2019 return NULL;
2020}
2021
2022/**
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09002023 * tcg2_measure_gpt_table() - measure gpt table
2024 *
2025 * @dev: TPM device
2026 * @loaded_image: handle to the loaded image
2027 *
2028 * Return: status code
2029 */
2030static efi_status_t
2031tcg2_measure_gpt_data(struct udevice *dev,
2032 struct efi_loaded_image_obj *loaded_image)
2033{
2034 efi_status_t ret;
2035 efi_handle_t handle;
2036 struct efi_handler *dp_handler;
2037 struct efi_device_path *orig_device_path;
2038 struct efi_device_path *device_path;
2039 struct efi_device_path *dp;
2040 struct efi_block_io *block_io;
2041 struct efi_gpt_data *event = NULL;
2042 efi_guid_t null_guid = NULL_GUID;
2043 gpt_header *gpt_h;
2044 gpt_entry *entry = NULL;
2045 gpt_entry *gpt_e;
2046 u32 num_of_valid_entry = 0;
2047 u32 event_size;
2048 u32 i;
2049 u32 total_gpt_entry_size;
2050
2051 ret = efi_search_protocol(&loaded_image->header,
2052 &efi_guid_loaded_image_device_path,
2053 &dp_handler);
2054 if (ret != EFI_SUCCESS)
2055 return ret;
2056
2057 orig_device_path = dp_handler->protocol_interface;
2058 if (!orig_device_path) /* no device path, skip GPT measurement */
2059 return EFI_SUCCESS;
2060
2061 device_path = efi_dp_dup(orig_device_path);
2062 if (!device_path)
2063 return EFI_OUT_OF_RESOURCES;
2064
2065 dp = search_gpt_dp_node(device_path);
2066 if (!dp) {
2067 /* no GPT device path node found, skip GPT measurement */
2068 ret = EFI_SUCCESS;
2069 goto out1;
2070 }
2071
2072 /* read GPT header */
2073 dp->type = DEVICE_PATH_TYPE_END;
2074 dp->sub_type = DEVICE_PATH_SUB_TYPE_END;
2075 dp = device_path;
2076 ret = EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_guid,
2077 &dp, &handle));
2078 if (ret != EFI_SUCCESS)
2079 goto out1;
2080
2081 ret = EFI_CALL(efi_handle_protocol(handle,
2082 &efi_block_io_guid, (void **)&block_io));
2083 if (ret != EFI_SUCCESS)
2084 goto out1;
2085
2086 gpt_h = memalign(block_io->media->io_align, block_io->media->block_size);
2087 if (!gpt_h) {
2088 ret = EFI_OUT_OF_RESOURCES;
2089 goto out2;
2090 }
2091
2092 ret = block_io->read_blocks(block_io, block_io->media->media_id, 1,
2093 block_io->media->block_size, gpt_h);
2094 if (ret != EFI_SUCCESS)
2095 goto out2;
2096
2097 /* read GPT entry */
2098 total_gpt_entry_size = gpt_h->num_partition_entries *
2099 gpt_h->sizeof_partition_entry;
2100 entry = memalign(block_io->media->io_align, total_gpt_entry_size);
2101 if (!entry) {
2102 ret = EFI_OUT_OF_RESOURCES;
2103 goto out2;
2104 }
2105
2106 ret = block_io->read_blocks(block_io, block_io->media->media_id,
2107 gpt_h->partition_entry_lba,
2108 total_gpt_entry_size, entry);
2109 if (ret != EFI_SUCCESS)
2110 goto out2;
2111
2112 /* count valid GPT entry */
2113 gpt_e = entry;
2114 for (i = 0; i < gpt_h->num_partition_entries; i++) {
2115 if (guidcmp(&null_guid, &gpt_e->partition_type_guid))
2116 num_of_valid_entry++;
2117
2118 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
2119 }
2120
2121 /* prepare event data for measurement */
2122 event_size = sizeof(struct efi_gpt_data) +
2123 (num_of_valid_entry * gpt_h->sizeof_partition_entry);
2124 event = calloc(1, event_size);
2125 if (!event) {
2126 ret = EFI_OUT_OF_RESOURCES;
2127 goto out2;
2128 }
2129 memcpy(event, gpt_h, sizeof(gpt_header));
2130 put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions);
2131
2132 /* copy valid GPT entry */
2133 gpt_e = entry;
2134 num_of_valid_entry = 0;
2135 for (i = 0; i < gpt_h->num_partition_entries; i++) {
2136 if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) {
2137 memcpy((u8 *)event->partitions +
2138 (num_of_valid_entry * gpt_h->sizeof_partition_entry),
2139 gpt_e, gpt_h->sizeof_partition_entry);
2140 num_of_valid_entry++;
2141 }
2142
2143 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
2144 }
2145
2146 ret = tcg2_measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *)event);
2147 if (ret != EFI_SUCCESS)
2148 goto out2;
2149
2150out2:
2151 EFI_CALL(efi_close_protocol((efi_handle_t)block_io, &efi_block_io_guid,
2152 NULL, NULL));
2153 free(gpt_h);
2154 free(entry);
2155 free(event);
2156out1:
2157 efi_free_pool(device_path);
2158
2159 return ret;
2160}
2161
2162/**
Masahisa Kojima8173cd42021-08-13 16:12:40 +09002163 * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation
2164 *
2165 * Return: status code
2166 */
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09002167efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_image_obj *handle)
Masahisa Kojima8173cd42021-08-13 16:12:40 +09002168{
2169 efi_status_t ret;
2170 u32 pcr_index;
2171 struct udevice *dev;
2172 u32 event = 0;
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09002173 struct smbios_entry *entry;
Masahisa Kojima8173cd42021-08-13 16:12:40 +09002174
2175 if (tcg2_efi_app_invoked)
2176 return EFI_SUCCESS;
2177
2178 ret = platform_get_tpm2_device(&dev);
2179 if (ret != EFI_SUCCESS)
2180 return ret;
2181
2182 ret = tcg2_measure_boot_variable(dev);
2183 if (ret != EFI_SUCCESS)
2184 goto out;
2185
2186 ret = tcg2_measure_event(dev, 4, EV_EFI_ACTION,
2187 strlen(EFI_CALLING_EFI_APPLICATION),
2188 (u8 *)EFI_CALLING_EFI_APPLICATION);
2189 if (ret != EFI_SUCCESS)
2190 goto out;
2191
Masahisa Kojimacd1fe7d2021-10-26 17:27:24 +09002192 entry = (struct smbios_entry *)find_smbios_table();
2193 if (entry) {
2194 ret = tcg2_measure_smbios(dev, entry);
2195 if (ret != EFI_SUCCESS)
2196 goto out;
2197 }
2198
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09002199 ret = tcg2_measure_gpt_data(dev, handle);
2200 if (ret != EFI_SUCCESS)
2201 goto out;
2202
Masahisa Kojima8173cd42021-08-13 16:12:40 +09002203 for (pcr_index = 0; pcr_index <= 7; pcr_index++) {
2204 ret = tcg2_measure_event(dev, pcr_index, EV_SEPARATOR,
2205 sizeof(event), (u8 *)&event);
2206 if (ret != EFI_SUCCESS)
2207 goto out;
2208 }
2209
2210 tcg2_efi_app_invoked = true;
2211out:
2212 return ret;
2213}
2214
2215/**
2216 * efi_tcg2_measure_efi_app_exit() - measure efi app exit
2217 *
2218 * Return: status code
2219 */
2220efi_status_t efi_tcg2_measure_efi_app_exit(void)
2221{
2222 efi_status_t ret;
2223 struct udevice *dev;
2224
2225 ret = platform_get_tpm2_device(&dev);
2226 if (ret != EFI_SUCCESS)
2227 return ret;
2228
2229 ret = tcg2_measure_event(dev, 4, EV_EFI_ACTION,
2230 strlen(EFI_RETURNING_FROM_EFI_APPLICATION),
2231 (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION);
2232 return ret;
2233}
2234
2235/**
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09002236 * efi_tcg2_notify_exit_boot_services() - ExitBootService callback
2237 *
2238 * @event: callback event
2239 * @context: callback context
2240 */
2241static void EFIAPI
2242efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context)
2243{
2244 efi_status_t ret;
2245 struct udevice *dev;
2246
2247 EFI_ENTRY("%p, %p", event, context);
2248
Ilias Apalodimas24e841a2021-11-18 09:03:39 +02002249 event_log.ebs_called = true;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09002250 ret = platform_get_tpm2_device(&dev);
2251 if (ret != EFI_SUCCESS)
2252 goto out;
2253
2254 ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
2255 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
2256 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
2257 if (ret != EFI_SUCCESS)
2258 goto out;
2259
2260 ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
2261 strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED),
2262 (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED);
2263
2264out:
2265 EFI_EXIT(ret);
2266}
2267
2268/**
2269 * efi_tcg2_notify_exit_boot_services_failed()
2270 * - notify ExitBootServices() is failed
2271 *
2272 * Return: status code
2273 */
2274efi_status_t efi_tcg2_notify_exit_boot_services_failed(void)
2275{
2276 struct udevice *dev;
2277 efi_status_t ret;
2278
2279 ret = platform_get_tpm2_device(&dev);
2280 if (ret != EFI_SUCCESS)
2281 goto out;
2282
2283 ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
2284 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
2285 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
2286 if (ret != EFI_SUCCESS)
2287 goto out;
2288
2289 ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION,
2290 strlen(EFI_EXIT_BOOT_SERVICES_FAILED),
2291 (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED);
2292
2293out:
2294 return ret;
2295}
2296
2297/**
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09002298 * tcg2_measure_secure_boot_variable() - measure secure boot variables
2299 *
2300 * @dev: TPM device
2301 *
2302 * Return: status code
2303 */
2304static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev)
2305{
2306 u8 *data;
2307 efi_uintn_t data_size;
2308 u32 count, i;
2309 efi_status_t ret;
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09002310 u8 deployed_mode;
2311 efi_uintn_t size;
2312 u32 deployed_audit_pcr_index = 1;
2313
2314 size = sizeof(deployed_mode);
2315 ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid,
2316 NULL, &size, &deployed_mode, NULL);
2317 if (ret != EFI_SUCCESS || !deployed_mode)
2318 deployed_audit_pcr_index = 7;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09002319
2320 count = ARRAY_SIZE(secure_variables);
2321 for (i = 0; i < count; i++) {
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02002322 const efi_guid_t *guid;
2323
Masahisa Kojima21684522021-10-26 17:27:26 +09002324 guid = efi_auth_var_get_guid(secure_variables[i].name);
Heinrich Schuchardt6f26e7c2021-09-09 08:50:01 +02002325
Masahisa Kojima21684522021-10-26 17:27:26 +09002326 data = efi_get_var(secure_variables[i].name, guid, &data_size);
2327 if (!data && !secure_variables[i].accept_empty)
2328 continue;
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09002329
Masahisa Kojimaf3e0c552021-10-26 17:27:27 +09002330 if (u16_strcmp(u"DeployedMode", secure_variables[i].name))
2331 secure_variables[i].pcr_index = deployed_audit_pcr_index;
2332 if (u16_strcmp(u"AuditMode", secure_variables[i].name))
2333 secure_variables[i].pcr_index = deployed_audit_pcr_index;
2334
2335 ret = tcg2_measure_variable(dev, secure_variables[i].pcr_index,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09002336 EV_EFI_VARIABLE_DRIVER_CONFIG,
Masahisa Kojima21684522021-10-26 17:27:26 +09002337 secure_variables[i].name, guid,
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09002338 data_size, data);
2339 free(data);
2340 if (ret != EFI_SUCCESS)
2341 goto error;
2342 }
2343
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09002344error:
2345 return ret;
2346}
2347
2348/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +02002349 * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
2350 *
2351 * If a TPM2 device is available, the TPM TCG2 Protocol is registered
2352 *
2353 * Return: An error status is only returned if adding the protocol fails.
2354 */
2355efi_status_t efi_tcg2_register(void)
2356{
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02002357 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02002358 struct udevice *dev;
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09002359 struct efi_event *event;
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02002360 u32 err;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02002361
2362 ret = platform_get_tpm2_device(&dev);
Ilias Apalodimasc67fef62020-11-16 08:52:41 +02002363 if (ret != EFI_SUCCESS) {
2364 log_warning("Unable to find TPMv2 device\n");
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03002365 return EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02002366 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02002367
Ilias Apalodimas1d16f1e2021-11-18 10:13:42 +02002368 /* initialize the TPM as early as possible. */
2369 err = tpm_startup(dev, TPM_ST_CLEAR);
2370 if (err) {
2371 log_err("TPM startup failed\n");
2372 goto fail;
2373 }
2374
Ilias Apalodimas967650d2020-11-30 11:47:40 +02002375 ret = efi_init_event_log();
2376 if (ret != EFI_SUCCESS)
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02002377 goto fail;
Ilias Apalodimas967650d2020-11-30 11:47:40 +02002378
Ilias Apalodimas590fef62020-11-11 11:18:11 +02002379 ret = efi_add_protocol(efi_root, &efi_guid_tcg2_protocol,
2380 (void *)&efi_tcg2_protocol);
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02002381 if (ret != EFI_SUCCESS) {
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03002382 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02002383 goto fail;
2384 }
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09002385
Masahisa Kojima1ac19bb2021-08-13 16:12:41 +09002386 ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
2387 efi_tcg2_notify_exit_boot_services, NULL,
2388 NULL, &event);
2389 if (ret != EFI_SUCCESS) {
2390 tcg2_uninit();
2391 goto fail;
2392 }
2393
Masahisa Kojima1d2a6562021-08-13 16:12:39 +09002394 ret = tcg2_measure_secure_boot_variable(dev);
2395 if (ret != EFI_SUCCESS) {
2396 tcg2_uninit();
2397 goto fail;
2398 }
2399
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02002400 return ret;
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03002401
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02002402fail:
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03002403 log_err("Cannot install EFI_TCG2_PROTOCOL\n");
2404 /*
2405 * Return EFI_SUCCESS and don't stop the EFI subsystem.
2406 * That's done for 2 reasons
2407 * - If the protocol is not installed the PCRs won't be extended. So
2408 * someone later in the boot flow will notice that and take the
2409 * necessary actions.
2410 * - The TPM sandbox is limited and we won't be able to run any efi
2411 * related tests with TCG2 enabled
2412 */
2413 return EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02002414}