Coverage Report

Created: 2026-03-11 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/u-boot/lib/efi_loader/efi_tcg2.c
Line
Count
Source
1
// 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
12
#include <dm.h>
13
#include <efi_device_path.h>
14
#include <efi_loader.h>
15
#include <efi_variable.h>
16
#include <efi_tcg2.h>
17
#include <log.h>
18
#include <malloc.h>
19
#include <smbios.h>
20
#include <version_string.h>
21
#include <tpm_api.h>
22
#include <u-boot/hash-checksum.h>
23
#include <linux/unaligned/be_byteshift.h>
24
#include <linux/unaligned/le_byteshift.h>
25
#include <linux/unaligned/generic.h>
26
#include <hexdump.h>
27
28
/**
29
 * struct event_log_buffer - internal eventlog management structure
30
 *
31
 * @buffer:   eventlog buffer
32
 * @final_buffer: finalevent config table buffer
33
 * @pos:    current position of 'buffer'
34
 * @final_pos:    current position of 'final_buffer'
35
 * @get_event_called: true if GetEventLog has been invoked at least once
36
 * @ebs_called:   true if ExitBootServices has been invoked
37
 * @truncated:    true if the 'buffer' is truncated
38
 */
39
struct event_log_buffer {
40
  void *buffer;
41
  void *final_buffer;
42
  size_t pos; /* eventlog position */
43
  size_t final_pos; /* final events config table position */
44
  size_t last_event_size;
45
  bool get_event_called;
46
  bool ebs_called;
47
  bool truncated;
48
};
49
50
static struct event_log_buffer event_log;
51
static bool tcg2_efi_app_invoked;
52
/*
53
 * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset.
54
 * Since the current tpm2_get_capability() response buffers starts at
55
 * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate
56
 * the response size and offset once for all consumers
57
 */
58
#define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \
59
           offsetof(struct tpms_capability_data, data))
60
0
#define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \
61
0
         offsetof(struct tpms_tagged_property, value))
62
63
static const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID;
64
static const efi_guid_t efi_guid_final_events = EFI_TCG2_FINAL_EVENTS_TABLE_GUID;
65
66
struct variable_info {
67
  const u16 *name;
68
  bool    accept_empty;
69
  u32   pcr_index;
70
};
71
72
static struct variable_info secure_variables[] = {
73
  {u"SecureBoot",   true, 7},
74
  {u"PK",     true, 7},
75
  {u"KEK",    true, 7},
76
  {u"db",     true, 7},
77
  {u"dbx",    true, 7},
78
  {u"dbt",    false,  7},
79
  {u"dbr",    false,  7},
80
  {u"DeployedMode", false,  1},
81
  {u"AuditMode",    false,  1},
82
};
83
84
static bool is_tcg2_protocol_installed(void)
85
0
{
86
0
  struct efi_handler *handler;
87
0
  efi_status_t ret;
88
89
0
  ret = efi_search_protocol(efi_root, &efi_guid_tcg2_protocol, &handler);
90
0
  return ret == EFI_SUCCESS;
91
0
}
92
93
/* tcg2_agile_log_append - Append an agile event to an eventlog
94
 *
95
 * @pcr_index:    PCR index
96
 * @event_type:   type of event added
97
 * @digest_list:  list of digest algorithms to add
98
 * @size:   size of event
99
 * @event:    event to add
100
 * @log:    log buffer to append the event
101
 *
102
 * @Return: status code
103
 */
104
static efi_status_t tcg2_agile_log_append(u32 pcr_index, u32 event_type,
105
            struct tpml_digest_values *digest_list,
106
            u32 size, u8 event[])
107
0
{
108
0
  void *log = (void *)((uintptr_t)event_log.buffer + event_log.pos);
109
0
  u32 event_size = size + tcg2_event_get_size(digest_list);
110
0
  struct efi_tcg2_final_events_table *final_event;
111
0
  efi_status_t ret = EFI_SUCCESS;
112
113
  /* if ExitBootServices hasn't been called update the normal log */
114
0
  if (!event_log.ebs_called) {
115
0
    if (event_log.truncated ||
116
0
        event_log.pos + event_size > CONFIG_TPM2_EVENT_LOG_SIZE) {
117
0
      event_log.truncated = true;
118
0
      return EFI_VOLUME_FULL;
119
0
    }
120
0
    tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
121
0
    event_log.pos += event_size;
122
0
    event_log.last_event_size = event_size;
123
0
  }
124
125
0
  if (!event_log.get_event_called)
126
0
    return ret;
127
128
  /* if GetEventLog has been called update FinalEventLog as well */
129
0
  if (event_log.final_pos + event_size > CONFIG_TPM2_EVENT_LOG_SIZE)
130
0
    return EFI_VOLUME_FULL;
131
132
0
  log = (void *)((uintptr_t)event_log.final_buffer + event_log.final_pos);
133
0
  tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
134
135
0
  final_event = event_log.final_buffer;
136
0
  final_event->number_of_events++;
137
0
  event_log.final_pos += event_size;
138
139
0
  return ret;
140
0
}
141
142
/**
143
 * tpm2_get_max_command_size() - get the supported max command size
144
 *
145
 * @dev:    TPM device
146
 * @max_command_size: output buffer for the size
147
 *
148
 * Return: 0 on success, -1 on error
149
 */
150
static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size)
151
0
{
152
0
  u8 response[TPM2_RESPONSE_BUFFER_SIZE];
153
0
  u32 ret;
154
155
0
  memset(response, 0, sizeof(response));
156
0
  ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
157
0
          TPM2_PT_MAX_COMMAND_SIZE, response, 1);
158
0
  if (ret)
159
0
    return -1;
160
161
0
  *max_command_size = (uint16_t)get_unaligned_be32(response +
162
0
               properties_offset);
163
164
0
  return 0;
165
0
}
166
167
/**
168
 * tpm2_get_max_response_size() - get the supported max response size
169
 *
170
 * @dev:    TPM device
171
 * @max_response_size:  output buffer for the size
172
 *
173
 * Return: 0 on success, -1 on error
174
 */
175
static int tpm2_get_max_response_size(struct udevice *dev,
176
              u16 *max_response_size)
177
0
{
178
0
  u8 response[TPM2_RESPONSE_BUFFER_SIZE];
179
0
  u32 ret;
180
181
0
  memset(response, 0, sizeof(response));
182
0
  ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
183
0
          TPM2_PT_MAX_RESPONSE_SIZE, response, 1);
184
0
  if (ret)
185
0
    return -1;
186
187
0
  *max_response_size = (uint16_t)get_unaligned_be32(response +
188
0
                properties_offset);
189
190
0
  return 0;
191
0
}
192
193
/**
194
 * tpm2_get_manufacturer_id() - get the manufacturer ID
195
 *
196
 * @dev:    TPM device
197
 * @manufacturer_id:  output buffer for the id
198
 *
199
 * Return: 0 on success, -1 on error
200
 */
201
static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id)
202
0
{
203
0
  u8 response[TPM2_RESPONSE_BUFFER_SIZE];
204
0
  u32 ret;
205
206
0
  memset(response, 0, sizeof(response));
207
0
  ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
208
0
          TPM2_PT_MANUFACTURER, response, 1);
209
0
  if (ret)
210
0
    return -1;
211
212
0
  *manufacturer_id = get_unaligned_be32(response + properties_offset);
213
214
0
  return 0;
215
0
}
216
217
/**
218
 * efi_tcg2_get_capability() - protocol capability information and state information
219
 *
220
 * @this:   TCG2 protocol instance
221
 * @capability:   caller allocated memory with size field to the size of
222
 *      the structure allocated
223
224
 * Return:  status code
225
 */
226
static efi_status_t EFIAPI
227
efi_tcg2_get_capability(struct efi_tcg2_protocol *this,
228
      struct efi_tcg2_boot_service_capability *capability)
229
{
230
  struct udevice *dev;
231
  efi_status_t efi_ret;
232
  int ret;
233
234
  EFI_ENTRY("%p, %p", this, capability);
235
236
  if (!this || !capability) {
237
    efi_ret = EFI_INVALID_PARAMETER;
238
    goto out;
239
  }
240
241
  if (capability->size < BOOT_SERVICE_CAPABILITY_MIN) {
242
    capability->size = BOOT_SERVICE_CAPABILITY_MIN;
243
    efi_ret = EFI_BUFFER_TOO_SMALL;
244
    goto out;
245
  }
246
247
  if (capability->size < sizeof(*capability)) {
248
    capability->size = sizeof(*capability);
249
    efi_ret = EFI_BUFFER_TOO_SMALL;
250
    goto out;
251
  }
252
253
  capability->structure_version.major = 1;
254
  capability->structure_version.minor = 1;
255
  capability->protocol_version.major = 1;
256
  capability->protocol_version.minor = 1;
257
258
  ret = tcg2_platform_get_tpm2(&dev);
259
  if (ret) {
260
    capability->supported_event_logs = 0;
261
    capability->hash_algorithm_bitmap = 0;
262
    capability->tpm_present_flag = false;
263
    capability->max_command_size = 0;
264
    capability->max_response_size = 0;
265
    capability->manufacturer_id = 0;
266
    capability->number_of_pcr_banks = 0;
267
    capability->active_pcr_banks = 0;
268
269
    efi_ret = EFI_SUCCESS;
270
    goto out;
271
  }
272
273
  /* We only allow a TPMv2 device to register the EFI protocol */
274
  capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2;
275
276
  capability->tpm_present_flag = true;
277
278
  /* Supported and active PCRs */
279
  capability->hash_algorithm_bitmap = 0;
280
  capability->active_pcr_banks = 0;
281
  ret = tcg2_get_pcr_info(dev, &capability->hash_algorithm_bitmap,
282
        &capability->active_pcr_banks,
283
        &capability->number_of_pcr_banks);
284
  if (ret) {
285
    efi_ret = EFI_DEVICE_ERROR;
286
    goto out;
287
  }
288
289
  /* Max command size */
290
  ret = tpm2_get_max_command_size(dev, &capability->max_command_size);
291
  if (ret) {
292
    efi_ret = EFI_DEVICE_ERROR;
293
    goto out;
294
  }
295
296
  /* Max response size */
297
  ret = tpm2_get_max_response_size(dev, &capability->max_response_size);
298
  if (ret) {
299
    efi_ret = EFI_DEVICE_ERROR;
300
    goto out;
301
  }
302
303
  /* Manufacturer ID */
304
  ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id);
305
  if (ret) {
306
    efi_ret = EFI_DEVICE_ERROR;
307
    goto out;
308
  }
309
310
  return EFI_EXIT(EFI_SUCCESS);
311
out:
312
  return EFI_EXIT(efi_ret);
313
}
314
315
/**
316
 * efi_tcg2_get_eventlog() -  retrieve the the address of an event log and its
317
 *        last entry
318
 *
319
 * @this:     TCG2 protocol instance
320
 * @log_format:     type of event log format
321
 * @event_log_location:   pointer to the memory address of the event log
322
 * @event_log_last_entry: pointer to the address of the start of the last
323
 *        entry in the event log in memory, if log contains
324
 *        more than 1 entry
325
 * @event_log_truncated:  set to true, if the Event Log is missing at i
326
 *        least one entry
327
 *
328
 * Return:  status code
329
 */
330
static efi_status_t EFIAPI
331
efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this,
332
          efi_tcg_event_log_format log_format,
333
          u64 *event_log_location, u64 *event_log_last_entry,
334
          bool *event_log_truncated)
335
{
336
  efi_status_t ret = EFI_SUCCESS;
337
  struct udevice *dev;
338
339
  EFI_ENTRY("%p, %u, %p, %p,  %p", this, log_format, event_log_location,
340
      event_log_last_entry, event_log_truncated);
341
342
  if (!this || !event_log_location || !event_log_last_entry ||
343
      !event_log_truncated) {
344
    ret = EFI_INVALID_PARAMETER;
345
    goto out;
346
  }
347
348
  /* Only support TPMV2 */
349
  if (log_format != TCG2_EVENT_LOG_FORMAT_TCG_2) {
350
    ret = EFI_INVALID_PARAMETER;
351
    goto out;
352
  }
353
354
  if (tcg2_platform_get_tpm2(&dev)) {
355
    event_log_location = NULL;
356
    event_log_last_entry = NULL;
357
    *event_log_truncated = false;
358
    ret = EFI_SUCCESS;
359
    goto out;
360
  }
361
  *event_log_location = (uintptr_t)event_log.buffer;
362
  *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos -
363
              event_log.last_event_size);
364
  *event_log_truncated = event_log.truncated;
365
  event_log.get_event_called = true;
366
367
out:
368
  return EFI_EXIT(ret);
369
}
370
371
/**
372
 * tcg2_hash_pe_image() - calculate PE/COFF image hash
373
 *
374
 * @efi:    pointer to the EFI binary
375
 * @efi_size:   size of @efi binary
376
 * @digest_list:  list of digest algorithms to extend
377
 *
378
 * Return:  status code
379
 */
380
static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size,
381
               struct tpml_digest_values *digest_list)
382
0
{
383
0
  WIN_CERTIFICATE *wincerts = NULL;
384
0
  size_t wincerts_len;
385
0
  struct efi_image_regions *regs = NULL;
386
0
  void *new_efi = NULL;
387
0
  u8 hash[TPM2_SHA512_DIGEST_SIZE];
388
0
  struct udevice *dev;
389
0
  efi_status_t ret = EFI_SUCCESS;
390
0
  u32 active;
391
0
  int i;
392
393
0
  new_efi = efi_prepare_aligned_image(efi, &efi_size);
394
0
  if (!new_efi)
395
0
    return EFI_OUT_OF_RESOURCES;
396
397
0
  if (!efi_image_parse(new_efi, efi_size, &regs, &wincerts,
398
0
           &wincerts_len)) {
399
0
    log_err("Parsing PE executable image failed\n");
400
0
    ret = EFI_UNSUPPORTED;
401
0
    goto out;
402
0
  }
403
404
0
  if (tcg2_platform_get_tpm2(&dev)) {
405
0
    ret = EFI_DEVICE_ERROR;
406
0
    goto out;
407
0
  }
408
409
0
  if (tcg2_get_active_pcr_banks(dev, &active)) {
410
0
    ret = EFI_DEVICE_ERROR;
411
0
    goto out;
412
0
  }
413
414
0
  digest_list->count = 0;
415
0
  for (i = 0; i < ARRAY_SIZE(hash_algo_list); i++) {
416
0
    u16 hash_alg = hash_algo_list[i].hash_alg;
417
418
0
    if (!(active & hash_algo_list[i].hash_mask))
419
0
      continue;
420
0
    switch (hash_alg) {
421
0
    case TPM2_ALG_SHA1:
422
0
      hash_calculate("sha1", regs->reg, regs->num, hash);
423
0
      break;
424
0
    case TPM2_ALG_SHA256:
425
0
      hash_calculate("sha256", regs->reg, regs->num, hash);
426
0
      break;
427
0
    case TPM2_ALG_SHA384:
428
0
      hash_calculate("sha384", regs->reg, regs->num, hash);
429
0
      break;
430
0
    case TPM2_ALG_SHA512:
431
0
      hash_calculate("sha512", regs->reg, regs->num, hash);
432
0
      break;
433
0
    case TPM2_ALG_SM3_256:
434
0
      hash_calculate("sm3_256", regs->reg, regs->num, hash);
435
0
      break;
436
0
    default:
437
0
      continue;
438
0
    }
439
0
    digest_list->digests[digest_list->count].hash_alg = hash_alg;
440
0
    memcpy(&digest_list->digests[digest_list->count].digest, hash,
441
0
           (u32)tpm2_algorithm_to_len(hash_alg));
442
0
    digest_list->count++;
443
0
  }
444
445
0
out:
446
0
  if (new_efi != efi)
447
0
    free(new_efi);
448
0
  free(regs);
449
450
0
  return ret;
451
0
}
452
453
/**
454
 * tcg2_measure_pe_image() - measure PE/COFF image
455
 *
456
 * @efi:    pointer to the EFI binary
457
 * @efi_size:   size of @efi binary
458
 * @handle:   loaded image handle
459
 * @loaded_image: loaded image protocol
460
 *
461
 * Return:  status code
462
 */
463
efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size,
464
           struct efi_loaded_image_obj *handle,
465
           struct efi_loaded_image *loaded_image)
466
0
{
467
0
  struct tpml_digest_values digest_list;
468
0
  efi_status_t ret;
469
0
  struct udevice *dev;
470
0
  u32 pcr_index, event_type, event_size;
471
0
  struct uefi_image_load_event *image_load_event;
472
0
  struct efi_device_path *device_path;
473
0
  u32 device_path_length;
474
0
  IMAGE_DOS_HEADER *dos;
475
0
  IMAGE_NT_HEADERS32 *nt;
476
0
  struct efi_handler *handler;
477
0
  int rc;
478
479
0
  if (!is_tcg2_protocol_installed())
480
0
    return EFI_SUCCESS;
481
482
0
  if (tcg2_platform_get_tpm2(&dev))
483
0
    return EFI_SECURITY_VIOLATION;
484
485
0
  switch (handle->image_type) {
486
0
  case IMAGE_SUBSYSTEM_EFI_APPLICATION:
487
0
    pcr_index = 4;
488
0
    event_type = EV_EFI_BOOT_SERVICES_APPLICATION;
489
0
    break;
490
0
  case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
491
0
    pcr_index = 2;
492
0
    event_type = EV_EFI_BOOT_SERVICES_DRIVER;
493
0
    break;
494
0
  case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
495
0
    pcr_index = 2;
496
0
    event_type = EV_EFI_RUNTIME_SERVICES_DRIVER;
497
0
    break;
498
0
  default:
499
0
    return EFI_UNSUPPORTED;
500
0
  }
501
502
0
  ret = tcg2_hash_pe_image(efi, efi_size, &digest_list);
503
0
  if (ret != EFI_SUCCESS)
504
0
    return ret;
505
506
0
  rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
507
0
  if (rc)
508
0
    return EFI_DEVICE_ERROR;
509
510
0
  ret = efi_search_protocol(&handle->header,
511
0
          &efi_guid_loaded_image_device_path, &handler);
512
0
  if (ret != EFI_SUCCESS)
513
0
    return ret;
514
515
0
  device_path = handler->protocol_interface;
516
0
  device_path_length = efi_dp_size(device_path);
517
0
  if (device_path_length > 0) {
518
    /* add end node size */
519
0
    device_path_length += sizeof(struct efi_device_path);
520
0
  }
521
0
  event_size = sizeof(struct uefi_image_load_event) + device_path_length;
522
0
  image_load_event = calloc(1, event_size);
523
0
  if (!image_load_event)
524
0
    return EFI_OUT_OF_RESOURCES;
525
526
0
  image_load_event->image_location_in_memory = (uintptr_t)efi;
527
0
  image_load_event->image_length_in_memory = efi_size;
528
0
  image_load_event->length_of_device_path = device_path_length;
529
530
0
  dos = (IMAGE_DOS_HEADER *)efi;
531
0
  nt = (IMAGE_NT_HEADERS32 *)(efi + dos->e_lfanew);
532
0
  if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
533
0
    IMAGE_NT_HEADERS64 *nt64 = (IMAGE_NT_HEADERS64 *)nt;
534
535
0
    image_load_event->image_link_time_address =
536
0
        nt64->OptionalHeader.ImageBase;
537
0
  } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
538
0
    image_load_event->image_link_time_address =
539
0
        nt->OptionalHeader.ImageBase;
540
0
  } else {
541
0
    ret = EFI_INVALID_PARAMETER;
542
0
    goto out;
543
0
  }
544
545
  /* device_path_length might be zero */
546
0
  memcpy(image_load_event->device_path, device_path, device_path_length);
547
548
0
  ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
549
0
            event_size, (u8 *)image_load_event);
550
551
0
out:
552
0
  free(image_load_event);
553
554
0
  return ret;
555
0
}
556
557
/**
558
 * efi_tcg2_hash_log_extend_event() - extend and optionally log events
559
 *
560
 * @this:     TCG2 protocol instance
561
 * @flags:      bitmap providing additional information on the
562
 *        operation
563
 * @data_to_hash:   physical address of the start of the data buffer
564
 *        to be hashed
565
 * @data_to_hash_len:   the length in bytes of the buffer referenced by
566
 *        data_to_hash
567
 * @efi_tcg_event:    pointer to data buffer containing information
568
 *        about the event
569
 *
570
 * Return:  status code
571
 */
572
static efi_status_t EFIAPI
573
efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags,
574
             u64 data_to_hash, u64 data_to_hash_len,
575
             struct efi_tcg2_event *efi_tcg_event)
576
{
577
  struct udevice *dev;
578
  efi_status_t ret = EFI_SUCCESS;
579
  u32 event_type, pcr_index, event_size;
580
  struct tpml_digest_values digest_list;
581
  int rc = 0;
582
583
  EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash,
584
      data_to_hash_len, efi_tcg_event);
585
586
  if (!this || !data_to_hash || !efi_tcg_event) {
587
    ret = EFI_INVALID_PARAMETER;
588
    goto out;
589
  }
590
591
  if (tcg2_platform_get_tpm2(&dev)) {
592
    ret = EFI_DEVICE_ERROR;
593
    goto out;
594
  }
595
596
  if (efi_tcg_event->size < efi_tcg_event->header.header_size +
597
      sizeof(u32)) {
598
    ret = EFI_INVALID_PARAMETER;
599
    goto out;
600
  }
601
602
  if (efi_tcg_event->header.pcr_index > EFI_TCG2_MAX_PCR_INDEX) {
603
    ret = EFI_INVALID_PARAMETER;
604
    goto out;
605
  }
606
607
  /*
608
   * if PE_COFF_IMAGE is set we need to make sure the image is not
609
   * corrupted, verify it and hash the PE/COFF image in accordance with
610
   * the procedure specified in "Calculating the PE Image Hash"
611
   * section of the "Windows Authenticode Portable Executable Signature
612
   * Format"
613
   */
614
  if (flags & PE_COFF_IMAGE) {
615
    ret = efi_check_pe((void *)(uintptr_t)data_to_hash,
616
           data_to_hash_len, NULL);
617
    if (ret != EFI_SUCCESS) {
618
      ret = EFI_UNSUPPORTED;
619
      goto out;
620
    }
621
    ret = tcg2_hash_pe_image((void *)(uintptr_t)data_to_hash,
622
           data_to_hash_len, &digest_list);
623
  } else {
624
    rc = tcg2_create_digest(dev, (u8 *)(uintptr_t)data_to_hash,
625
          data_to_hash_len, &digest_list);
626
    if (rc)
627
      ret = EFI_DEVICE_ERROR;
628
  }
629
630
  if (ret != EFI_SUCCESS)
631
    goto out;
632
633
  pcr_index = efi_tcg_event->header.pcr_index;
634
  event_type = efi_tcg_event->header.event_type;
635
636
  rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
637
  if (rc) {
638
    ret = EFI_DEVICE_ERROR;
639
    goto out;
640
  }
641
642
  if (flags & EFI_TCG2_EXTEND_ONLY) {
643
    if (event_log.truncated)
644
      ret = EFI_VOLUME_FULL;
645
    goto out;
646
  }
647
648
  /*
649
   * The efi_tcg_event size includes the size component and the
650
   * headersize
651
   */
652
  event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) -
653
    efi_tcg_event->header.header_size;
654
  ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
655
            event_size, efi_tcg_event->event);
656
out:
657
  return EFI_EXIT(ret);
658
}
659
660
/**
661
 * efi_tcg2_submit_command() - Send command to the TPM
662
 *
663
 * @this:     TCG2 protocol instance
664
 * @input_param_block_size: size of the TPM input parameter block
665
 * @input_param_block:    pointer to the TPM input parameter block
666
 * @output_param_block_size:  size of the TPM output parameter block
667
 * @output_param_block:   pointer to the TPM output parameter block
668
 *
669
 * Return:  status code
670
 */
671
static efi_status_t EFIAPI
672
efi_tcg2_submit_command(struct efi_tcg2_protocol *this,
673
      u32 input_param_block_size,
674
      u8 *input_param_block,
675
      u32 output_param_block_size,
676
      u8 *output_param_block)
677
{
678
  struct udevice *dev;
679
  efi_status_t ret = EFI_SUCCESS;
680
  u32 rc;
681
  size_t resp_buf_size = output_param_block_size;
682
683
  EFI_ENTRY("%p, %u, %p, %u, %p", this, input_param_block_size,
684
      input_param_block, output_param_block_size, output_param_block);
685
686
  if (!this || !input_param_block || !input_param_block_size) {
687
    ret = EFI_INVALID_PARAMETER;
688
    goto out;
689
  }
690
691
  if (tcg2_platform_get_tpm2(&dev)) {
692
    ret = EFI_DEVICE_ERROR;
693
    goto out;
694
  }
695
696
  rc = tpm2_submit_command(dev, input_param_block,
697
         output_param_block, &resp_buf_size);
698
  if (rc) {
699
    ret = (rc == -ENOSPC) ? EFI_OUT_OF_RESOURCES : EFI_DEVICE_ERROR;
700
701
    goto out;
702
  }
703
704
out:
705
  return EFI_EXIT(ret);
706
}
707
708
/**
709
 * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks
710
 *
711
 * @this:     TCG2 protocol instance
712
 * @active_pcr_banks:   pointer for receiving the bitmap of currently
713
 *        active PCR banks
714
 *
715
 * Return:  status code
716
 */
717
static efi_status_t EFIAPI
718
efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
719
            u32 *active_pcr_banks)
720
{
721
  struct udevice *dev;
722
  efi_status_t ret = EFI_INVALID_PARAMETER;
723
724
  EFI_ENTRY("%p, %p", this, active_pcr_banks);
725
726
  if (!this || !active_pcr_banks)
727
    goto out;
728
729
  if (tcg2_platform_get_tpm2(&dev))
730
    goto out;
731
732
  /*
733
   * EFI_INVALID_PARAMETER does not convey the problem type.
734
   * but that's what currently the spec specifies.
735
   * EFI_DEVICE_ERROR would be better
736
   */
737
  if (tcg2_get_active_pcr_banks(dev, active_pcr_banks))
738
    goto out;
739
740
  ret = EFI_SUCCESS;
741
742
out:
743
  return EFI_EXIT(ret);
744
}
745
746
/**
747
 * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks
748
 *
749
 * @this:     TCG2 protocol instance
750
 * @active_pcr_banks:   bitmap of the requested active PCR banks
751
 *
752
 * Return:  status code
753
 */
754
static efi_status_t EFIAPI
755
efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
756
            u32 __maybe_unused active_pcr_banks)
757
0
{
758
0
  return EFI_UNSUPPORTED;
759
0
}
760
761
/**
762
 * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous
763
 *               set_active_pcr_banks()
764
 *
765
 * @this:     TCG2 protocol instance
766
 * @operation_present:    non-zero value to indicate a
767
 *        set_active_pcr_banks operation was
768
 *        invoked during last boot
769
 * @response:     result value could be returned
770
 *
771
 * Return:  status code
772
 */
773
static efi_status_t EFIAPI
774
efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
775
              u32 __maybe_unused *operation_present,
776
              u32 __maybe_unused *response)
777
0
{
778
0
  return EFI_UNSUPPORTED;
779
0
}
780
781
static const struct efi_tcg2_protocol efi_tcg2_protocol = {
782
  .get_capability = efi_tcg2_get_capability,
783
  .get_eventlog = efi_tcg2_get_eventlog,
784
  .hash_log_extend_event = efi_tcg2_hash_log_extend_event,
785
  .submit_command = efi_tcg2_submit_command,
786
  .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks,
787
  .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks,
788
  .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks,
789
};
790
791
/**
792
 * tcg2_uninit - remove the final event table and free efi memory on failures
793
 */
794
static void tcg2_uninit(void)
795
0
{
796
0
  efi_status_t ret;
797
798
0
  ret = efi_install_configuration_table(&efi_guid_final_events, NULL);
799
0
  if (ret != EFI_SUCCESS && ret != EFI_NOT_FOUND)
800
0
    log_err("Failed to delete final events config table\n");
801
802
0
  efi_free_pool(event_log.buffer);
803
0
  event_log.buffer = NULL;
804
0
  efi_free_pool(event_log.final_buffer);
805
0
  event_log.final_buffer = NULL;
806
807
0
  if (!is_tcg2_protocol_installed())
808
0
    return;
809
810
0
  ret = efi_uninstall_multiple_protocol_interfaces(efi_root, &efi_guid_tcg2_protocol,
811
0
               &efi_tcg2_protocol, NULL);
812
0
  if (ret != EFI_SUCCESS)
813
0
    log_err("Failed to remove EFI TCG2 protocol\n");
814
0
}
815
816
/**
817
 * create_final_event() - Create the final event and install the config
818
 *      defined by the TCG EFI spec
819
 */
820
static efi_status_t create_final_event(void)
821
0
{
822
0
  struct efi_tcg2_final_events_table *final_event;
823
0
  efi_status_t ret;
824
825
  /*
826
   * All events generated after the invocation of
827
   * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an
828
   * EFI_CONFIGURATION_TABLE
829
   */
830
0
  ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, CONFIG_TPM2_EVENT_LOG_SIZE,
831
0
        &event_log.final_buffer);
832
0
  if (ret != EFI_SUCCESS)
833
0
    goto out;
834
835
0
  memset(event_log.final_buffer, 0xff, CONFIG_TPM2_EVENT_LOG_SIZE);
836
0
  final_event = event_log.final_buffer;
837
0
  final_event->number_of_events = 0;
838
0
  final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
839
0
  event_log.final_pos = sizeof(*final_event);
840
0
  ret = efi_install_configuration_table(&efi_guid_final_events,
841
0
                final_event);
842
0
  if (ret != EFI_SUCCESS) {
843
0
    efi_free_pool(event_log.final_buffer);
844
0
    event_log.final_buffer = NULL;
845
0
  }
846
847
0
out:
848
0
  return ret;
849
0
}
850
851
/**
852
 * measure_event() - common function to add event log and extend PCR
853
 *
854
 * @dev:    TPM device
855
 * @pcr_index:    PCR index
856
 * @event_type:   type of event added
857
 * @size:   event size
858
 * @event:    event data
859
 *
860
 * Return:  status code
861
 */
862
static efi_status_t measure_event(struct udevice *dev, u32 pcr_index,
863
          u32 event_type, u32 size, u8 event[])
864
0
{
865
0
  struct tpml_digest_values digest_list;
866
0
  efi_status_t ret = EFI_DEVICE_ERROR;
867
0
  int rc;
868
869
0
  rc = tcg2_create_digest(dev, event, size, &digest_list);
870
0
  if (rc)
871
0
    goto out;
872
873
0
  rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
874
0
  if (rc)
875
0
    goto out;
876
877
0
  ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
878
0
            size, event);
879
880
0
out:
881
0
  return ret;
882
0
}
883
884
/**
885
 * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the
886
 *            eventlog and extend the PCRs
887
 *
888
 * @dev:  TPM device
889
 *
890
 * @Return: status code
891
 */
892
static efi_status_t efi_append_scrtm_version(struct udevice *dev)
893
0
{
894
0
  efi_status_t ret;
895
896
0
  ret = measure_event(dev, 0, EV_S_CRTM_VERSION,
897
0
          strlen(version_string) + 1, (u8 *)version_string);
898
899
0
  return ret;
900
0
}
901
902
/**
903
 * efi_init_event_log() - initialize an eventlog
904
 *
905
 * Return:    status code
906
 */
907
static efi_status_t efi_init_event_log(void)
908
0
{
909
  /*
910
   * vendor_info_size is currently set to 0, we need to change the length
911
   * and allocate the flexible array member if this changes
912
   */
913
0
  struct tcg2_event_log elog;
914
0
  struct udevice *dev;
915
0
  efi_status_t ret;
916
0
  int rc;
917
918
0
  if (tcg2_platform_get_tpm2(&dev))
919
0
    return EFI_DEVICE_ERROR;
920
921
0
  ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA,
922
0
        CONFIG_TPM2_EVENT_LOG_SIZE,
923
0
        (void **)&event_log.buffer);
924
0
  if (ret != EFI_SUCCESS)
925
0
    return ret;
926
927
  /*
928
   * initialize log area as 0xff so the OS can easily figure out the
929
   * last log entry
930
   */
931
0
  memset(event_log.buffer, 0xff, CONFIG_TPM2_EVENT_LOG_SIZE);
932
933
  /*
934
   * The log header is defined to be in SHA1 event log entry format.
935
   * Setup event header
936
   */
937
0
  event_log.pos = 0;
938
0
  event_log.last_event_size = 0;
939
0
  event_log.get_event_called = false;
940
0
  event_log.ebs_called = false;
941
0
  event_log.truncated = false;
942
943
  /*
944
   * Check if earlier firmware have passed any eventlog. Different
945
   * platforms can use different ways to do so.
946
   */
947
0
  elog.log = event_log.buffer;
948
0
  elog.log_size = CONFIG_TPM2_EVENT_LOG_SIZE;
949
0
  rc = tcg2_log_prepare_buffer(dev, &elog, false);
950
0
  if (rc) {
951
0
    ret = (rc == -ENOBUFS) ? EFI_BUFFER_TOO_SMALL : EFI_DEVICE_ERROR;
952
0
    goto free_pool;
953
0
  }
954
955
0
  event_log.pos = elog.log_position;
956
957
  /*
958
   * Add SCRTM version to the log if previous firmmware
959
   * doesn't pass an eventlog.
960
   */
961
0
  if (!elog.found) {
962
0
    ret = efi_append_scrtm_version(dev);
963
0
    if (ret != EFI_SUCCESS)
964
0
      goto free_pool;
965
0
  }
966
967
0
  ret = create_final_event();
968
0
  if (ret != EFI_SUCCESS)
969
0
    goto free_pool;
970
971
0
  return ret;
972
973
0
free_pool:
974
0
  efi_free_pool(event_log.buffer);
975
0
  event_log.buffer = NULL;
976
0
  return ret;
977
0
}
978
979
/**
980
 * tcg2_measure_variable() - add variable event log and extend PCR
981
 *
982
 * @dev:    TPM device
983
 * @pcr_index:    PCR index
984
 * @event_type:   type of event added
985
 * @var_name:   variable name
986
 * @guid:   guid
987
 * @data_size:    variable data size
988
 * @data:   variable data
989
 *
990
 * Return:  status code
991
 */
992
static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index,
993
            u32 event_type, const u16 *var_name,
994
            const efi_guid_t *guid,
995
            efi_uintn_t data_size, u8 *data)
996
0
{
997
0
  u32 event_size;
998
0
  efi_status_t ret;
999
0
  struct efi_tcg2_uefi_variable_data *event;
1000
1001
0
  event_size = sizeof(event->variable_name) +
1002
0
         sizeof(event->unicode_name_length) +
1003
0
         sizeof(event->variable_data_length) +
1004
0
         (u16_strlen(var_name) * sizeof(u16)) + data_size;
1005
0
  event = malloc(event_size);
1006
0
  if (!event)
1007
0
    return EFI_OUT_OF_RESOURCES;
1008
1009
0
  guidcpy(&event->variable_name, guid);
1010
0
  event->unicode_name_length = u16_strlen(var_name);
1011
0
  event->variable_data_length = data_size;
1012
0
  memcpy(event->unicode_name, var_name,
1013
0
         (event->unicode_name_length * sizeof(u16)));
1014
0
  if (data) {
1015
0
    memcpy((u16 *)event->unicode_name + event->unicode_name_length,
1016
0
           data, data_size);
1017
0
  }
1018
0
  ret = measure_event(dev, pcr_index, event_type, event_size,
1019
0
          (u8 *)event);
1020
0
  free(event);
1021
0
  return ret;
1022
0
}
1023
1024
/**
1025
 * tcg2_measure_boot_variable() - measure boot variables
1026
 *
1027
 * @dev:  TPM device
1028
 *
1029
 * Return:  status code
1030
 */
1031
static efi_status_t tcg2_measure_boot_variable(struct udevice *dev)
1032
0
{
1033
0
  u16 *boot_order;
1034
0
  u16 *boot_index;
1035
0
  u16 var_name[] = u"BootOrder";
1036
0
  u16 boot_name[] = u"Boot####";
1037
0
  u8 *bootvar;
1038
0
  efi_uintn_t var_data_size;
1039
0
  u32 count, i;
1040
0
  efi_status_t ret;
1041
1042
0
  boot_order = efi_get_var(var_name, &efi_global_variable_guid,
1043
0
         &var_data_size);
1044
0
  if (!boot_order) {
1045
    /* If "BootOrder" is not defined, skip the boot variable measurement */
1046
0
    return EFI_SUCCESS;
1047
0
  }
1048
1049
0
  ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name,
1050
0
            &efi_global_variable_guid, var_data_size,
1051
0
            (u8 *)boot_order);
1052
0
  if (ret != EFI_SUCCESS)
1053
0
    goto error;
1054
1055
0
  count = var_data_size / sizeof(*boot_order);
1056
0
  boot_index = boot_order;
1057
0
  for (i = 0; i < count; i++) {
1058
0
    efi_create_indexed_name(boot_name, sizeof(boot_name),
1059
0
          "Boot", *boot_index++);
1060
1061
0
    bootvar = efi_get_var(boot_name, &efi_global_variable_guid,
1062
0
              &var_data_size);
1063
1064
0
    if (!bootvar) {
1065
0
      log_debug("%ls not found\n", boot_name);
1066
0
      continue;
1067
0
    }
1068
1069
0
    ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2,
1070
0
              boot_name,
1071
0
              &efi_global_variable_guid,
1072
0
              var_data_size, bootvar);
1073
0
    free(bootvar);
1074
0
    if (ret != EFI_SUCCESS)
1075
0
      goto error;
1076
0
  }
1077
1078
0
error:
1079
0
  free(boot_order);
1080
0
  return ret;
1081
0
}
1082
1083
/**
1084
 * tcg2_measure_smbios() - measure smbios table
1085
 *
1086
 * @dev:  TPM device
1087
 * @entry:  pointer to the smbios_entry structure
1088
 *
1089
 * Return:  status code
1090
 */
1091
static efi_status_t
1092
tcg2_measure_smbios(struct udevice *dev,
1093
        const struct smbios3_entry *entry)
1094
0
{
1095
0
  efi_status_t ret;
1096
0
  struct smbios_header *smbios_copy;
1097
0
  struct smbios_handoff_table_pointers2 *event = NULL;
1098
0
  u32 event_size;
1099
0
  const char smbios3_anchor[] = "_SM3_";
1100
1101
  /* We only support SMBIOS 3.0 Entry Point structure */
1102
0
  if (memcmp(entry->anchor, smbios3_anchor, sizeof(smbios3_anchor) - 1))
1103
0
    return EFI_UNSUPPORTED;
1104
1105
  /*
1106
   * TCG PC Client PFP Spec says
1107
   * "SMBIOS structures that contain static configuration information
1108
   * (e.g. Platform Manufacturer Enterprise Number assigned by IANA,
1109
   * platform model number, Vendor and Device IDs for each SMBIOS table)
1110
   * that is relevant to the security of the platform MUST be measured".
1111
   * Device dependent parameters such as serial number are cleared to
1112
   * zero or spaces for the measurement.
1113
   */
1114
0
  event_size = sizeof(struct smbios_handoff_table_pointers2) +
1115
0
         FIELD_SIZEOF(struct efi_configuration_table, guid) +
1116
0
         entry->table_maximum_size;
1117
0
  event = calloc(1, event_size);
1118
0
  if (!event) {
1119
0
    ret = EFI_OUT_OF_RESOURCES;
1120
0
    goto out;
1121
0
  }
1122
1123
0
  event->table_description_size = sizeof(SMBIOS_HANDOFF_TABLE_DESC);
1124
0
  memcpy(event->table_description, SMBIOS_HANDOFF_TABLE_DESC,
1125
0
         sizeof(SMBIOS_HANDOFF_TABLE_DESC));
1126
0
  put_unaligned_le64(1, &event->number_of_tables);
1127
0
  guidcpy(&event->table_entry[0].guid, &smbios3_guid);
1128
0
  smbios_copy = (struct smbios_header *)((uintptr_t)&event->table_entry[0].table);
1129
0
  memcpy(&event->table_entry[0].table,
1130
0
         (void *)((uintptr_t)entry->struct_table_address),
1131
0
         entry->table_maximum_size);
1132
1133
0
  smbios_prepare_measurement(entry, smbios_copy);
1134
1135
0
  ret = measure_event(dev, 1, EV_EFI_HANDOFF_TABLES2, event_size,
1136
0
          (u8 *)event);
1137
0
  if (ret != EFI_SUCCESS)
1138
0
    goto out;
1139
1140
0
out:
1141
0
  free(event);
1142
1143
0
  return ret;
1144
0
}
1145
1146
/**
1147
 * tcg2_measure_gpt_table() - measure gpt table
1148
 *
1149
 * @dev:    TPM device
1150
 * @loaded_image: handle to the loaded image
1151
 *
1152
 * Return:  status code
1153
 */
1154
static efi_status_t
1155
tcg2_measure_gpt_data(struct udevice *dev,
1156
          struct efi_loaded_image_obj *loaded_image)
1157
0
{
1158
0
  efi_status_t ret;
1159
0
  efi_handle_t handle;
1160
0
  struct efi_handler *dp_handler, *io_handler;
1161
0
  struct efi_device_path *orig_device_path;
1162
0
  struct efi_device_path *device_path;
1163
0
  struct efi_device_path *dp;
1164
0
  struct efi_block_io *block_io;
1165
0
  struct efi_gpt_data *event = NULL;
1166
0
  efi_guid_t null_guid = NULL_GUID;
1167
0
  gpt_header *gpt_h;
1168
0
  gpt_entry *entry = NULL;
1169
0
  gpt_entry *gpt_e;
1170
0
  u32 num_of_valid_entry = 0;
1171
0
  u32 event_size;
1172
0
  u32 i;
1173
0
  u32 total_gpt_entry_size;
1174
1175
0
  ret = efi_search_protocol(&loaded_image->header,
1176
0
          &efi_guid_loaded_image_device_path,
1177
0
          &dp_handler);
1178
0
  if (ret != EFI_SUCCESS)
1179
0
    return ret;
1180
1181
0
  orig_device_path = dp_handler->protocol_interface;
1182
0
  if (!orig_device_path) /* no device path, skip GPT measurement */
1183
0
    return EFI_SUCCESS;
1184
1185
0
  device_path = efi_dp_dup(orig_device_path);
1186
0
  if (!device_path)
1187
0
    return EFI_OUT_OF_RESOURCES;
1188
1189
0
  dp = search_gpt_dp_node(device_path);
1190
0
  if (!dp) {
1191
    /* no GPT device path node found, skip GPT measurement */
1192
0
    ret = EFI_SUCCESS;
1193
0
    goto out1;
1194
0
  }
1195
1196
  /* read GPT header */
1197
0
  dp->type = DEVICE_PATH_TYPE_END;
1198
0
  dp->sub_type = DEVICE_PATH_SUB_TYPE_END;
1199
0
  dp = device_path;
1200
0
  ret = EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_guid,
1201
0
                 &dp, &handle));
1202
0
  if (ret != EFI_SUCCESS)
1203
0
    goto out1;
1204
1205
0
  ret = efi_search_protocol(handle, &efi_block_io_guid, &io_handler);
1206
0
  if (ret != EFI_SUCCESS)
1207
0
    goto out1;
1208
0
  block_io = io_handler->protocol_interface;
1209
1210
0
  gpt_h = memalign(block_io->media->io_align, block_io->media->block_size);
1211
0
  if (!gpt_h) {
1212
0
    ret = EFI_OUT_OF_RESOURCES;
1213
0
    goto out2;
1214
0
  }
1215
1216
0
  ret = EFI_CALL(block_io->read_blocks(block_io,
1217
0
               block_io->media->media_id, 1,
1218
0
               block_io->media->block_size,
1219
0
               gpt_h));
1220
0
  if (ret != EFI_SUCCESS)
1221
0
    goto out2;
1222
1223
  /* read GPT entry */
1224
0
  total_gpt_entry_size = gpt_h->num_partition_entries *
1225
0
             gpt_h->sizeof_partition_entry;
1226
0
  entry = memalign(block_io->media->io_align, total_gpt_entry_size);
1227
0
  if (!entry) {
1228
0
    ret = EFI_OUT_OF_RESOURCES;
1229
0
    goto out2;
1230
0
  }
1231
1232
0
  ret = EFI_CALL(block_io->read_blocks(block_io,
1233
0
               block_io->media->media_id,
1234
0
               gpt_h->partition_entry_lba,
1235
0
               total_gpt_entry_size, entry));
1236
0
  if (ret != EFI_SUCCESS)
1237
0
    goto out2;
1238
1239
  /* count valid GPT entry */
1240
0
  gpt_e = entry;
1241
0
  for (i = 0; i < gpt_h->num_partition_entries; i++) {
1242
0
    if (guidcmp(&null_guid, &gpt_e->partition_type_guid))
1243
0
      num_of_valid_entry++;
1244
1245
0
    gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1246
0
  }
1247
1248
  /* prepare event data for measurement */
1249
0
  event_size = sizeof(struct efi_gpt_data) +
1250
0
    (num_of_valid_entry * gpt_h->sizeof_partition_entry);
1251
0
  event = calloc(1, event_size);
1252
0
  if (!event) {
1253
0
    ret = EFI_OUT_OF_RESOURCES;
1254
0
    goto out2;
1255
0
  }
1256
0
  memcpy(event, gpt_h, sizeof(gpt_header));
1257
0
  put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions);
1258
1259
  /* copy valid GPT entry */
1260
0
  gpt_e = entry;
1261
0
  num_of_valid_entry = 0;
1262
0
  for (i = 0; i < gpt_h->num_partition_entries; i++) {
1263
0
    if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) {
1264
0
      memcpy((u8 *)event->partitions +
1265
0
             (num_of_valid_entry * gpt_h->sizeof_partition_entry),
1266
0
             gpt_e, gpt_h->sizeof_partition_entry);
1267
0
      num_of_valid_entry++;
1268
0
    }
1269
1270
0
    gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1271
0
  }
1272
1273
0
  ret = measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *)event);
1274
1275
0
out2:
1276
0
  free(gpt_h);
1277
0
  free(entry);
1278
0
  free(event);
1279
0
out1:
1280
0
  efi_free_pool(device_path);
1281
1282
0
  return ret;
1283
0
}
1284
1285
/* Return the byte size of reserved map area in DTB or -1 upon error */
1286
static ssize_t size_of_rsvmap(void *dtb)
1287
0
{
1288
0
  struct fdt_reserve_entry e;
1289
0
  ssize_t size_max;
1290
0
  ssize_t size;
1291
0
  u8 *rsvmap_base;
1292
1293
0
  rsvmap_base = (u8 *)dtb + fdt_off_mem_rsvmap(dtb);
1294
0
  size_max = fdt_totalsize(dtb) - fdt_off_mem_rsvmap(dtb);
1295
0
  size = 0;
1296
1297
0
  do {
1298
0
    memcpy(&e, rsvmap_base + size, sizeof(e));
1299
0
    size += sizeof(e);
1300
0
    if (size > size_max)
1301
0
      return -1;
1302
0
  } while (e.size);
1303
1304
0
  return size;
1305
0
}
1306
1307
/**
1308
 * efi_tcg2_measure_dtb() - measure DTB passed to the OS
1309
 *
1310
 * @dtb: pointer to the device tree blob
1311
 *
1312
 * Return:  status code
1313
 */
1314
efi_status_t efi_tcg2_measure_dtb(void *dtb)
1315
0
{
1316
0
  struct uefi_platform_firmware_blob2 *blob;
1317
0
  struct fdt_header *header;
1318
0
  sha256_context hash_ctx;
1319
0
  struct udevice *dev;
1320
0
  ssize_t rsvmap_size;
1321
0
  efi_status_t ret;
1322
0
  u32 event_size;
1323
1324
0
  if (!is_tcg2_protocol_installed())
1325
0
    return EFI_SUCCESS;
1326
1327
0
  if (tcg2_platform_get_tpm2(&dev))
1328
0
    return EFI_SECURITY_VIOLATION;
1329
1330
0
  rsvmap_size = size_of_rsvmap(dtb);
1331
0
  if (rsvmap_size < 0)
1332
0
    return EFI_SECURITY_VIOLATION;
1333
1334
0
  event_size = sizeof(*blob) + sizeof(EFI_DTB_EVENT_STRING) + SHA256_SUM_LEN;
1335
0
  blob = calloc(1, event_size);
1336
0
  if (!blob)
1337
0
    return EFI_OUT_OF_RESOURCES;
1338
1339
0
  blob->blob_description_size = sizeof(EFI_DTB_EVENT_STRING);
1340
0
  memcpy(blob->data, EFI_DTB_EVENT_STRING, blob->blob_description_size);
1341
1342
  /* Measure populated areas of the DTB */
1343
0
  header = dtb;
1344
0
  sha256_starts(&hash_ctx);
1345
0
  sha256_update(&hash_ctx, (u8 *)header, sizeof(struct fdt_header));
1346
0
  sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_struct(dtb), fdt_size_dt_strings(dtb));
1347
0
  sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_strings(dtb), fdt_size_dt_struct(dtb));
1348
0
  sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_mem_rsvmap(dtb), rsvmap_size);
1349
0
  sha256_finish(&hash_ctx, blob->data + blob->blob_description_size);
1350
1351
0
  ret = measure_event(dev, 1, EV_POST_CODE, event_size, (u8 *)blob);
1352
1353
0
  free(blob);
1354
0
  return ret;
1355
0
}
1356
1357
/**
1358
 * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation
1359
 *
1360
 * Return:  status code
1361
 */
1362
efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_image_obj *handle)
1363
0
{
1364
0
  efi_status_t ret;
1365
0
  u32 pcr_index;
1366
0
  struct udevice *dev;
1367
0
  u32 event = 0;
1368
0
  struct smbios3_entry *entry;
1369
1370
0
  if (!is_tcg2_protocol_installed())
1371
0
    return EFI_SUCCESS;
1372
1373
0
  if (tcg2_efi_app_invoked)
1374
0
    return EFI_SUCCESS;
1375
1376
0
  if (tcg2_platform_get_tpm2(&dev))
1377
0
    return EFI_SECURITY_VIOLATION;
1378
1379
0
  ret = tcg2_measure_boot_variable(dev);
1380
0
  if (ret != EFI_SUCCESS)
1381
0
    goto out;
1382
1383
0
  ret = measure_event(dev, 4, EV_EFI_ACTION,
1384
0
          strlen(EFI_CALLING_EFI_APPLICATION),
1385
0
          (u8 *)EFI_CALLING_EFI_APPLICATION);
1386
0
  if (ret != EFI_SUCCESS)
1387
0
    goto out;
1388
1389
0
  entry = efi_get_configuration_table(&smbios3_guid);
1390
0
  if (entry) {
1391
0
    ret = tcg2_measure_smbios(dev, entry);
1392
0
    if (ret != EFI_SUCCESS)
1393
0
      goto out;
1394
0
  }
1395
1396
0
  ret = tcg2_measure_gpt_data(dev, handle);
1397
0
  if (ret != EFI_SUCCESS)
1398
0
    goto out;
1399
1400
0
  for (pcr_index = 0; pcr_index <= 7; pcr_index++) {
1401
0
    ret = measure_event(dev, pcr_index, EV_SEPARATOR,
1402
0
            sizeof(event), (u8 *)&event);
1403
0
    if (ret != EFI_SUCCESS)
1404
0
      goto out;
1405
0
  }
1406
1407
0
  tcg2_efi_app_invoked = true;
1408
0
out:
1409
0
  return ret;
1410
0
}
1411
1412
/**
1413
 * efi_tcg2_measure_efi_app_exit() - measure efi app exit
1414
 *
1415
 * Return:  status code
1416
 */
1417
efi_status_t efi_tcg2_measure_efi_app_exit(void)
1418
0
{
1419
0
  efi_status_t ret;
1420
0
  struct udevice *dev;
1421
1422
0
  if (!is_tcg2_protocol_installed())
1423
0
    return EFI_SUCCESS;
1424
1425
0
  if (tcg2_platform_get_tpm2(&dev))
1426
0
    return EFI_SECURITY_VIOLATION;
1427
1428
0
  ret = measure_event(dev, 4, EV_EFI_ACTION,
1429
0
          strlen(EFI_RETURNING_FROM_EFI_APPLICATION),
1430
0
          (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION);
1431
0
  return ret;
1432
0
}
1433
1434
/**
1435
 * efi_tcg2_notify_exit_boot_services() - ExitBootService callback
1436
 *
1437
 * @event:  callback event
1438
 * @context:  callback context
1439
 */
1440
static void EFIAPI
1441
efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context)
1442
{
1443
  efi_status_t ret;
1444
  struct udevice *dev;
1445
1446
  EFI_ENTRY("%p, %p", event, context);
1447
1448
  event_log.ebs_called = true;
1449
1450
  if (!is_tcg2_protocol_installed()) {
1451
    ret = EFI_SUCCESS;
1452
    goto out;
1453
  }
1454
1455
  if (tcg2_platform_get_tpm2(&dev)) {
1456
    ret = EFI_SECURITY_VIOLATION;
1457
    goto out;
1458
  }
1459
1460
  ret = measure_event(dev, 5, EV_EFI_ACTION,
1461
          strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1462
          (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
1463
  if (ret != EFI_SUCCESS)
1464
    goto out;
1465
1466
  ret = measure_event(dev, 5, EV_EFI_ACTION,
1467
          strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED),
1468
          (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED);
1469
1470
out:
1471
  EFI_EXIT(ret);
1472
}
1473
1474
/**
1475
 * efi_tcg2_notify_exit_boot_services_failed()
1476
 *  - notify ExitBootServices() is failed
1477
 *
1478
 * Return:  status code
1479
 */
1480
efi_status_t efi_tcg2_notify_exit_boot_services_failed(void)
1481
0
{
1482
0
  struct udevice *dev;
1483
0
  efi_status_t ret;
1484
1485
0
  if (!is_tcg2_protocol_installed())
1486
0
    return EFI_SUCCESS;
1487
1488
0
  if (tcg2_platform_get_tpm2(&dev))
1489
0
    return EFI_SECURITY_VIOLATION;
1490
1491
0
  ret = measure_event(dev, 5, EV_EFI_ACTION,
1492
0
          strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1493
0
          (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
1494
0
  if (ret != EFI_SUCCESS)
1495
0
    goto out;
1496
1497
0
  ret = measure_event(dev, 5, EV_EFI_ACTION,
1498
0
          strlen(EFI_EXIT_BOOT_SERVICES_FAILED),
1499
0
          (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED);
1500
1501
0
out:
1502
0
  return ret;
1503
0
}
1504
1505
/**
1506
 * tcg2_measure_secure_boot_variable() - measure secure boot variables
1507
 *
1508
 * @dev:  TPM device
1509
 *
1510
 * Return:  status code
1511
 */
1512
static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev)
1513
0
{
1514
0
  u8 *data;
1515
0
  efi_uintn_t data_size;
1516
0
  u32 count, i;
1517
0
  efi_status_t ret;
1518
0
  u8 deployed_mode;
1519
0
  efi_uintn_t size;
1520
0
  u32 deployed_audit_pcr_index = 1;
1521
1522
0
  size = sizeof(deployed_mode);
1523
0
  ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid,
1524
0
           NULL, &size, &deployed_mode, NULL);
1525
0
  if (ret != EFI_SUCCESS || !deployed_mode)
1526
0
    deployed_audit_pcr_index = 7;
1527
1528
0
  count = ARRAY_SIZE(secure_variables);
1529
0
  for (i = 0; i < count; i++) {
1530
0
    const efi_guid_t *guid;
1531
1532
0
    guid = efi_auth_var_get_guid(secure_variables[i].name);
1533
1534
0
    data = efi_get_var(secure_variables[i].name, guid, &data_size);
1535
0
    if (!data && !secure_variables[i].accept_empty)
1536
0
      continue;
1537
1538
0
    if (u16_strcmp(u"DeployedMode", secure_variables[i].name))
1539
0
      secure_variables[i].pcr_index = deployed_audit_pcr_index;
1540
0
    if (u16_strcmp(u"AuditMode", secure_variables[i].name))
1541
0
      secure_variables[i].pcr_index = deployed_audit_pcr_index;
1542
1543
0
    ret = tcg2_measure_variable(dev, secure_variables[i].pcr_index,
1544
0
              EV_EFI_VARIABLE_DRIVER_CONFIG,
1545
0
              secure_variables[i].name, guid,
1546
0
              data_size, data);
1547
0
    free(data);
1548
0
    if (ret != EFI_SUCCESS)
1549
0
      goto error;
1550
0
  }
1551
1552
0
error:
1553
0
  return ret;
1554
0
}
1555
1556
/**
1557
 * efi_tcg2_do_initial_measurement() - do initial measurement
1558
 *
1559
 * Return:  status code
1560
 */
1561
efi_status_t efi_tcg2_do_initial_measurement(void)
1562
0
{
1563
0
  efi_status_t ret;
1564
0
  struct udevice *dev;
1565
1566
0
  if (!is_tcg2_protocol_installed())
1567
0
    return EFI_SUCCESS;
1568
1569
0
  if (tcg2_platform_get_tpm2(&dev))
1570
0
    return EFI_SECURITY_VIOLATION;
1571
1572
0
  ret = tcg2_measure_secure_boot_variable(dev);
1573
0
  if (ret != EFI_SUCCESS)
1574
0
    goto out;
1575
1576
0
out:
1577
0
  return ret;
1578
0
}
1579
1580
/**
1581
 * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
1582
 *
1583
 * If a TPM2 device is available, the TPM TCG2 Protocol is registered
1584
 *
1585
 * Return:  status code
1586
 */
1587
efi_status_t efi_tcg2_register(void)
1588
0
{
1589
0
  efi_status_t ret = EFI_SUCCESS;
1590
0
  struct udevice *dev;
1591
0
  struct efi_event *event;
1592
0
  u32 err;
1593
1594
0
  if (tcg2_platform_get_tpm2(&dev)) {
1595
0
    log_warning("Missing TPMv2 device for EFI_TCG_PROTOCOL\n");
1596
0
    return EFI_SUCCESS;
1597
0
  }
1598
1599
  /* initialize the TPM as early as possible. */
1600
0
  err = tpm_auto_start(dev);
1601
0
  if (err) {
1602
0
    ret = EFI_DEVICE_ERROR;
1603
0
    log_err("TPM startup failed\n");
1604
0
    goto fail;
1605
0
  }
1606
1607
0
  ret = efi_init_event_log();
1608
0
  if (ret != EFI_SUCCESS) {
1609
0
    tcg2_uninit();
1610
0
    goto fail;
1611
0
  }
1612
1613
0
  ret = efi_install_multiple_protocol_interfaces(&efi_root, &efi_guid_tcg2_protocol,
1614
0
                   &efi_tcg2_protocol, NULL);
1615
0
  if (ret != EFI_SUCCESS) {
1616
0
    tcg2_uninit();
1617
0
    goto fail;
1618
0
  }
1619
1620
0
  ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
1621
0
             efi_tcg2_notify_exit_boot_services, NULL,
1622
0
             NULL, &event);
1623
0
  if (ret != EFI_SUCCESS) {
1624
0
    tcg2_uninit();
1625
0
    goto fail;
1626
0
  }
1627
1628
0
  return ret;
1629
1630
0
fail:
1631
0
  log_err("Cannot install EFI_TCG2_PROTOCOL\n");
1632
0
  return ret;
1633
0
}