2 * Copyright (C) 2005, 2012 IBM Corporation
13 * Access to the eventlog created by a system's firmware / BIOS
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
22 #include <linux/seq_file.h>
24 #include <linux/security.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
29 #include "tpm_eventlog.h"
32 static const char* tcpa_event_type_strings[] = {
43 "Platform Config Flags",
53 static const char* tcpa_pc_event_id_strings[] = {
64 "Option ROM microcode ",
71 /* returns pointer to start of pos. entry of tcg log */
72 static void *tpm_bios_measurements_start(struct seq_file *m, loff_t *pos)
75 struct tpm_bios_log *log = m->private;
76 void *addr = log->bios_event_log;
77 void *limit = log->bios_event_log_end;
78 struct tcpa_event *event;
80 /* read over *pos measurements */
81 for (i = 0; i < *pos; i++) {
84 if ((addr + sizeof(struct tcpa_event)) < limit) {
85 if (event->event_type == 0 && event->event_size == 0)
87 addr += sizeof(struct tcpa_event) + event->event_size;
91 /* now check if current entry is valid */
92 if ((addr + sizeof(struct tcpa_event)) >= limit)
97 if ((event->event_type == 0 && event->event_size == 0) ||
98 ((addr + sizeof(struct tcpa_event) + event->event_size) >= limit))
104 static void *tpm_bios_measurements_next(struct seq_file *m, void *v,
107 struct tcpa_event *event = v;
108 struct tpm_bios_log *log = m->private;
109 void *limit = log->bios_event_log_end;
111 v += sizeof(struct tcpa_event) + event->event_size;
113 /* now check if current entry is valid */
114 if ((v + sizeof(struct tcpa_event)) >= limit)
119 if (event->event_type == 0 && event->event_size == 0)
122 if ((event->event_type == 0 && event->event_size == 0) ||
123 ((v + sizeof(struct tcpa_event) + event->event_size) >= limit))
130 static void tpm_bios_measurements_stop(struct seq_file *m, void *v)
134 static int get_event_name(char *dest, struct tcpa_event *event,
135 unsigned char * event_entry)
137 const char *name = "";
138 /* 41 so there is room for 40 data and 1 nul */
140 int i, n_len = 0, d_len = 0;
141 struct tcpa_pc_event *pc_event;
143 switch(event->event_type) {
151 case PLATFORM_CONFIG_FLAGS:
152 case TABLE_OF_DEVICES:
155 case IPL_PARTITION_DATA:
159 name = tcpa_event_type_strings[event->event_type];
160 n_len = strlen(name);
164 if (MAX_TEXT_EVENT > event->event_size) {
166 n_len = event->event_size;
170 pc_event = (struct tcpa_pc_event *)event_entry;
172 /* ToDo Row data -> Base64 */
174 switch (pc_event->event_id) {
179 case OPTION_ROM_EXEC:
180 case OPTION_ROM_CONFIG:
182 name = tcpa_pc_event_id_strings[pc_event->event_id];
183 n_len = strlen(name);
188 case OPTION_ROM_MICROCODE:
189 case S_CRTM_CONTENTS:
191 name = tcpa_pc_event_id_strings[pc_event->event_id];
192 n_len = strlen(name);
193 for (i = 0; i < 20; i++)
194 d_len += sprintf(&data[2*i], "%02x",
195 pc_event->event_data[i]);
204 return snprintf(dest, MAX_TEXT_EVENT, "[%.*s%.*s]",
205 n_len, name, d_len, data);
209 static int tpm_binary_bios_measurements_show(struct seq_file *m, void *v)
211 struct tcpa_event *event = v;
215 for (i = 0; i < sizeof(struct tcpa_event) + event->event_size; i++)
216 seq_putc(m, data[i]);
221 static int tpm_bios_measurements_release(struct inode *inode,
224 struct seq_file *seq = file->private_data;
225 struct tpm_bios_log *log = seq->private;
228 kfree(log->bios_event_log);
232 return seq_release(inode, file);
235 static int tpm_ascii_bios_measurements_show(struct seq_file *m, void *v)
239 struct tcpa_event *event = v;
240 unsigned char *event_entry =
241 (unsigned char *) (v + sizeof(struct tcpa_event));
243 eventname = kmalloc(MAX_TEXT_EVENT, GFP_KERNEL);
245 printk(KERN_ERR "%s: ERROR - No Memory for event name\n ",
250 seq_printf(m, "%2d ", event->pcr_index);
253 seq_printf(m, "%20phN", event->pcr_value);
255 /* 3rd: event type identifier */
256 seq_printf(m, " %02x", event->event_type);
258 len += get_event_name(eventname, event, event_entry);
260 /* 4th: eventname <= max + \'0' delimiter */
261 seq_printf(m, " %s\n", eventname);
267 static const struct seq_operations tpm_ascii_b_measurments_seqops = {
268 .start = tpm_bios_measurements_start,
269 .next = tpm_bios_measurements_next,
270 .stop = tpm_bios_measurements_stop,
271 .show = tpm_ascii_bios_measurements_show,
274 static const struct seq_operations tpm_binary_b_measurments_seqops = {
275 .start = tpm_bios_measurements_start,
276 .next = tpm_bios_measurements_next,
277 .stop = tpm_bios_measurements_stop,
278 .show = tpm_binary_bios_measurements_show,
281 static int tpm_ascii_bios_measurements_open(struct inode *inode,
285 struct tpm_bios_log *log;
286 struct seq_file *seq;
288 log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
292 if ((err = read_log(log)))
295 /* now register seq file */
296 err = seq_open(file, &tpm_ascii_b_measurments_seqops);
298 seq = file->private_data;
307 kfree(log->bios_event_log);
312 static const struct file_operations tpm_ascii_bios_measurements_ops = {
313 .open = tpm_ascii_bios_measurements_open,
316 .release = tpm_bios_measurements_release,
319 static int tpm_binary_bios_measurements_open(struct inode *inode,
323 struct tpm_bios_log *log;
324 struct seq_file *seq;
326 log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
330 if ((err = read_log(log)))
333 /* now register seq file */
334 err = seq_open(file, &tpm_binary_b_measurments_seqops);
336 seq = file->private_data;
345 kfree(log->bios_event_log);
350 static const struct file_operations tpm_binary_bios_measurements_ops = {
351 .open = tpm_binary_bios_measurements_open,
354 .release = tpm_bios_measurements_release,
357 static int is_bad(void *p)
361 if (IS_ERR(p) && (PTR_ERR(p) != -ENODEV))
366 struct dentry **tpm_bios_log_setup(char *name)
368 struct dentry **ret = NULL, *tpm_dir, *bin_file, *ascii_file;
370 tpm_dir = securityfs_create_dir(name, NULL);
375 securityfs_create_file("binary_bios_measurements",
376 S_IRUSR | S_IRGRP, tpm_dir, NULL,
377 &tpm_binary_bios_measurements_ops);
378 if (is_bad(bin_file))
382 securityfs_create_file("ascii_bios_measurements",
383 S_IRUSR | S_IRGRP, tpm_dir, NULL,
384 &tpm_ascii_bios_measurements_ops);
385 if (is_bad(ascii_file))
388 ret = kmalloc(3 * sizeof(struct dentry *), GFP_KERNEL);
399 securityfs_remove(ascii_file);
401 securityfs_remove(bin_file);
403 securityfs_remove(tpm_dir);
408 void tpm_bios_log_teardown(struct dentry **lst)
412 for (i = 0; i < 3; i++)
413 securityfs_remove(lst[i]);