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)
240 struct tcpa_event *event = v;
241 unsigned char *event_entry =
242 (unsigned char *) (v + sizeof(struct tcpa_event));
244 eventname = kmalloc(MAX_TEXT_EVENT, GFP_KERNEL);
246 printk(KERN_ERR "%s: ERROR - No Memory for event name\n ",
251 seq_printf(m, "%2d ", event->pcr_index);
254 for (i = 0; i < 20; i++)
255 seq_printf(m, "%02x", event->pcr_value[i]);
257 /* 3rd: event type identifier */
258 seq_printf(m, " %02x", event->event_type);
260 len += get_event_name(eventname, event, event_entry);
262 /* 4th: eventname <= max + \'0' delimiter */
263 seq_printf(m, " %s\n", eventname);
269 static const struct seq_operations tpm_ascii_b_measurments_seqops = {
270 .start = tpm_bios_measurements_start,
271 .next = tpm_bios_measurements_next,
272 .stop = tpm_bios_measurements_stop,
273 .show = tpm_ascii_bios_measurements_show,
276 static const struct seq_operations tpm_binary_b_measurments_seqops = {
277 .start = tpm_bios_measurements_start,
278 .next = tpm_bios_measurements_next,
279 .stop = tpm_bios_measurements_stop,
280 .show = tpm_binary_bios_measurements_show,
283 static int tpm_ascii_bios_measurements_open(struct inode *inode,
287 struct tpm_bios_log *log;
288 struct seq_file *seq;
290 log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
294 if ((err = read_log(log)))
297 /* now register seq file */
298 err = seq_open(file, &tpm_ascii_b_measurments_seqops);
300 seq = file->private_data;
309 kfree(log->bios_event_log);
314 static const struct file_operations tpm_ascii_bios_measurements_ops = {
315 .open = tpm_ascii_bios_measurements_open,
318 .release = tpm_bios_measurements_release,
321 static int tpm_binary_bios_measurements_open(struct inode *inode,
325 struct tpm_bios_log *log;
326 struct seq_file *seq;
328 log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
332 if ((err = read_log(log)))
335 /* now register seq file */
336 err = seq_open(file, &tpm_binary_b_measurments_seqops);
338 seq = file->private_data;
347 kfree(log->bios_event_log);
352 static const struct file_operations tpm_binary_bios_measurements_ops = {
353 .open = tpm_binary_bios_measurements_open,
356 .release = tpm_bios_measurements_release,
359 static int is_bad(void *p)
363 if (IS_ERR(p) && (PTR_ERR(p) != -ENODEV))
368 struct dentry **tpm_bios_log_setup(char *name)
370 struct dentry **ret = NULL, *tpm_dir, *bin_file, *ascii_file;
372 tpm_dir = securityfs_create_dir(name, NULL);
377 securityfs_create_file("binary_bios_measurements",
378 S_IRUSR | S_IRGRP, tpm_dir, NULL,
379 &tpm_binary_bios_measurements_ops);
380 if (is_bad(bin_file))
384 securityfs_create_file("ascii_bios_measurements",
385 S_IRUSR | S_IRGRP, tpm_dir, NULL,
386 &tpm_ascii_bios_measurements_ops);
387 if (is_bad(ascii_file))
390 ret = kmalloc(3 * sizeof(struct dentry *), GFP_KERNEL);
401 securityfs_remove(ascii_file);
403 securityfs_remove(bin_file);
405 securityfs_remove(tpm_dir);
409 EXPORT_SYMBOL_GPL(tpm_bios_log_setup);
411 void tpm_bios_log_teardown(struct dentry **lst)
415 for (i = 0; i < 3; i++)
416 securityfs_remove(lst[i]);
418 EXPORT_SYMBOL_GPL(tpm_bios_log_teardown);
419 MODULE_LICENSE("GPL");