1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 IBM Corporation
6 * Nayna Jain <nayna@linux.vnet.ibm.com>
8 * Access to TPM 2.0 event log as written by Firmware.
9 * It assumes that writer of event log has followed TCG Specification
10 * for Family "2.0" and written the event data in little endian.
11 * With that, it doesn't need any endian conversion for structure
15 #include <linux/seq_file.h>
17 #include <linux/security.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/tpm_eventlog.h>
26 * calc_tpm2_event_size() - calculate the event size, where event
27 * is an entry in the TPM 2.0 event log. The event is of type Crypto
28 * Agile Log Entry Format as defined in TCG EFI Protocol Specification
31 * @event: event whose size is to be calculated.
32 * @event_header: the first event in the event log.
34 * Returns size of the event. If it is an invalid event, returns 0.
36 static size_t calc_tpm2_event_size(struct tcg_pcr_event2_head *event,
37 struct tcg_pcr_event *event_header)
39 return __calc_tpm2_event_size(event, event_header, false);
42 static void *tpm2_bios_measurements_start(struct seq_file *m, loff_t *pos)
44 struct tpm_chip *chip = m->private;
45 struct tpm_bios_log *log = &chip->log;
46 void *addr = log->bios_event_log;
47 void *limit = log->bios_event_log_end;
48 struct tcg_pcr_event *event_header;
49 struct tcg_pcr_event2_head *event;
54 size = sizeof(struct tcg_pcr_event) - sizeof(event_header->event)
55 + event_header->event_size;
58 if (addr + size < limit) {
59 if ((event_header->event_type == 0) &&
60 (event_header->event_size == 0))
62 return SEQ_START_TOKEN;
69 size = calc_tpm2_event_size(event, event_header);
70 if ((addr + size >= limit) || (size == 0))
74 for (i = 0; i < (*pos - 1); i++) {
76 size = calc_tpm2_event_size(event, event_header);
78 if ((addr + size >= limit) || (size == 0))
86 static void *tpm2_bios_measurements_next(struct seq_file *m, void *v,
89 struct tcg_pcr_event *event_header;
90 struct tcg_pcr_event2_head *event;
91 struct tpm_chip *chip = m->private;
92 struct tpm_bios_log *log = &chip->log;
93 void *limit = log->bios_event_log_end;
98 event_header = log->bios_event_log;
100 if (v == SEQ_START_TOKEN) {
101 event_size = sizeof(struct tcg_pcr_event) -
102 sizeof(event_header->event) + event_header->event_size;
103 marker = event_header;
106 event_size = calc_tpm2_event_size(event, event_header);
112 marker = marker + event_size;
118 event_size = calc_tpm2_event_size(event, event_header);
119 if (((v + event_size) >= limit) || (event_size == 0))
125 static void tpm2_bios_measurements_stop(struct seq_file *m, void *v)
129 static int tpm2_binary_bios_measurements_show(struct seq_file *m, void *v)
131 struct tpm_chip *chip = m->private;
132 struct tpm_bios_log *log = &chip->log;
133 struct tcg_pcr_event *event_header = log->bios_event_log;
134 struct tcg_pcr_event2_head *event = v;
138 if (v == SEQ_START_TOKEN) {
139 size = sizeof(struct tcg_pcr_event) -
140 sizeof(event_header->event) + event_header->event_size;
142 temp_ptr = event_header;
145 seq_write(m, temp_ptr, size);
147 size = calc_tpm2_event_size(event, event_header);
150 seq_write(m, temp_ptr, size);
156 const struct seq_operations tpm2_binary_b_measurements_seqops = {
157 .start = tpm2_bios_measurements_start,
158 .next = tpm2_bios_measurements_next,
159 .stop = tpm2_bios_measurements_stop,
160 .show = tpm2_binary_bios_measurements_show,