2 * Copyright (C) 2016 IBM Corporation
5 * Nayna Jain <nayna@linux.vnet.ibm.com>
7 * Access to TPM 2.0 event log as written by Firmware.
8 * It assumes that writer of event log has followed TCG Specification
9 * for Family "2.0" and written the event data in little endian.
10 * With that, it doesn't need any endian conversion for structure
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
19 #include <linux/seq_file.h>
21 #include <linux/security.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/tpm_eventlog.h>
30 * calc_tpm2_event_size() - calculate the event size, where event
31 * is an entry in the TPM 2.0 event log. The event is of type Crypto
32 * Agile Log Entry Format as defined in TCG EFI Protocol Specification
35 * @event: event whose size is to be calculated.
36 * @event_header: the first event in the event log.
38 * Returns size of the event. If it is an invalid event, returns 0.
40 static size_t calc_tpm2_event_size(struct tcg_pcr_event2 *event,
41 struct tcg_pcr_event *event_header)
43 struct tcg_efi_specid_event *efispecid;
44 struct tcg_event_field *event_field;
54 marker_start = marker;
55 marker = marker + sizeof(event->pcr_idx) + sizeof(event->event_type)
56 + sizeof(event->count);
58 efispecid = (struct tcg_efi_specid_event *)event_header->event;
60 /* Check if event is malformed. */
61 if (event->count > efispecid->num_algs)
64 for (i = 0; i < event->count; i++) {
65 halg_size = sizeof(event->digests[i].alg_id);
66 memcpy(&halg, marker, halg_size);
67 marker = marker + halg_size;
68 for (j = 0; j < efispecid->num_algs; j++) {
69 if (halg == efispecid->digest_sizes[j].alg_id) {
71 efispecid->digest_sizes[j].digest_size;
75 /* Algorithm without known length. Such event is unparseable. */
76 if (j == efispecid->num_algs)
80 event_field = (struct tcg_event_field *)marker;
81 marker = marker + sizeof(event_field->event_size)
82 + event_field->event_size;
83 size = marker - marker_start;
85 if ((event->event_type == 0) && (event_field->event_size == 0))
91 static void *tpm2_bios_measurements_start(struct seq_file *m, loff_t *pos)
93 struct tpm_chip *chip = m->private;
94 struct tpm_bios_log *log = &chip->log;
95 void *addr = log->bios_event_log;
96 void *limit = log->bios_event_log_end;
97 struct tcg_pcr_event *event_header;
98 struct tcg_pcr_event2 *event;
103 size = sizeof(struct tcg_pcr_event) - sizeof(event_header->event)
104 + event_header->event_size;
107 if (addr + size < limit) {
108 if ((event_header->event_type == 0) &&
109 (event_header->event_size == 0))
111 return SEQ_START_TOKEN;
118 size = calc_tpm2_event_size(event, event_header);
119 if ((addr + size >= limit) || (size == 0))
123 for (i = 0; i < (*pos - 1); i++) {
125 size = calc_tpm2_event_size(event, event_header);
127 if ((addr + size >= limit) || (size == 0))
135 static void *tpm2_bios_measurements_next(struct seq_file *m, void *v,
138 struct tcg_pcr_event *event_header;
139 struct tcg_pcr_event2 *event;
140 struct tpm_chip *chip = m->private;
141 struct tpm_bios_log *log = &chip->log;
142 void *limit = log->bios_event_log_end;
147 event_header = log->bios_event_log;
149 if (v == SEQ_START_TOKEN) {
150 event_size = sizeof(struct tcg_pcr_event) -
151 sizeof(event_header->event) + event_header->event_size;
152 marker = event_header;
155 event_size = calc_tpm2_event_size(event, event_header);
161 marker = marker + event_size;
167 event_size = calc_tpm2_event_size(event, event_header);
168 if (((v + event_size) >= limit) || (event_size == 0))
174 static void tpm2_bios_measurements_stop(struct seq_file *m, void *v)
178 static int tpm2_binary_bios_measurements_show(struct seq_file *m, void *v)
180 struct tpm_chip *chip = m->private;
181 struct tpm_bios_log *log = &chip->log;
182 struct tcg_pcr_event *event_header = log->bios_event_log;
183 struct tcg_pcr_event2 *event = v;
187 if (v == SEQ_START_TOKEN) {
188 size = sizeof(struct tcg_pcr_event) -
189 sizeof(event_header->event) + event_header->event_size;
191 temp_ptr = event_header;
194 seq_write(m, temp_ptr, size);
196 size = calc_tpm2_event_size(event, event_header);
199 seq_write(m, temp_ptr, size);
205 const struct seq_operations tpm2_binary_b_measurements_seqops = {
206 .start = tpm2_bios_measurements_start,
207 .next = tpm2_bios_measurements_next,
208 .stop = tpm2_bios_measurements_stop,
209 .show = tpm2_binary_bios_measurements_show,