2 * Copyright (C) 2005 IBM Corporation
5 * Seiji Munetoh <munetoh@jp.ibm.com>
6 * Stefan Berger <stefanb@us.ibm.com>
7 * Reiner Sailer <sailer@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>
10 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
12 * Access to the eventlog extended by the TCG BIOS of PC platform
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
21 #include <linux/seq_file.h>
23 #include <linux/security.h>
24 #include <linux/module.h>
25 #include <acpi/acpi.h>
28 #define TCG_EVENT_NAME_LEN_MAX 255
29 #define MAX_TEXT_EVENT 1000 /* Max event string length */
30 #define ACPI_TCPA_SIG "TCPA" /* 0x41504354 /'TCPA' */
32 enum bios_platform_class {
39 void *bios_event_log_end;
43 struct acpi_table_header hdr;
47 u32 log_max_len __attribute__ ((packed));
48 u64 log_start_addr __attribute__ ((packed));
52 u64 log_max_len __attribute__ ((packed));
53 u64 log_start_addr __attribute__ ((packed));
61 u8 pcr_value[20]; /* SHA1 */
66 enum tcpa_event_types {
77 PLATFORM_CONFIG_FLAGS,
87 static const char* tcpa_event_type_strings[] = {
98 "Platform Config Flags",
102 "IPL Partition Data",
108 struct tcpa_pc_event {
114 enum tcpa_pc_event_ids {
123 OPTION_ROM_MICROCODE = 10,
127 HOST_TABLE_OF_DEVICES,
130 static const char* tcpa_pc_event_id_strings[] = {
141 "Option ROM microcode ",
148 /* returns pointer to start of pos. entry of tcg log */
149 static void *tpm_bios_measurements_start(struct seq_file *m, loff_t *pos)
152 struct tpm_bios_log *log = m->private;
153 void *addr = log->bios_event_log;
154 void *limit = log->bios_event_log_end;
155 struct tcpa_event *event;
157 /* read over *pos measurements */
158 for (i = 0; i < *pos; i++) {
161 if ((addr + sizeof(struct tcpa_event)) < limit) {
162 if (event->event_type == 0 && event->event_size == 0)
164 addr += sizeof(struct tcpa_event) + event->event_size;
168 /* now check if current entry is valid */
169 if ((addr + sizeof(struct tcpa_event)) >= limit)
174 if ((event->event_type == 0 && event->event_size == 0) ||
175 ((addr + sizeof(struct tcpa_event) + event->event_size) >= limit))
181 static void *tpm_bios_measurements_next(struct seq_file *m, void *v,
184 struct tcpa_event *event = v;
185 struct tpm_bios_log *log = m->private;
186 void *limit = log->bios_event_log_end;
188 v += sizeof(struct tcpa_event) + event->event_size;
190 /* now check if current entry is valid */
191 if ((v + sizeof(struct tcpa_event)) >= limit)
196 if (event->event_type == 0 && event->event_size == 0)
199 if ((event->event_type == 0 && event->event_size == 0) ||
200 ((v + sizeof(struct tcpa_event) + event->event_size) >= limit))
207 static void tpm_bios_measurements_stop(struct seq_file *m, void *v)
211 static int get_event_name(char *dest, struct tcpa_event *event,
212 unsigned char * event_entry)
214 const char *name = "";
215 /* 41 so there is room for 40 data and 1 nul */
217 int i, n_len = 0, d_len = 0;
218 struct tcpa_pc_event *pc_event;
220 switch(event->event_type) {
228 case PLATFORM_CONFIG_FLAGS:
229 case TABLE_OF_DEVICES:
232 case IPL_PARTITION_DATA:
236 name = tcpa_event_type_strings[event->event_type];
237 n_len = strlen(name);
241 if (MAX_TEXT_EVENT > event->event_size) {
243 n_len = event->event_size;
247 pc_event = (struct tcpa_pc_event *)event_entry;
249 /* ToDo Row data -> Base64 */
251 switch (pc_event->event_id) {
256 case OPTION_ROM_EXEC:
257 case OPTION_ROM_CONFIG:
259 name = tcpa_pc_event_id_strings[pc_event->event_id];
260 n_len = strlen(name);
265 case OPTION_ROM_MICROCODE:
266 case S_CRTM_CONTENTS:
268 name = tcpa_pc_event_id_strings[pc_event->event_id];
269 n_len = strlen(name);
270 for (i = 0; i < 20; i++)
271 d_len += sprintf(&data[2*i], "%02x",
272 pc_event->event_data[i]);
281 return snprintf(dest, MAX_TEXT_EVENT, "[%.*s%.*s]",
282 n_len, name, d_len, data);
286 static int tpm_binary_bios_measurements_show(struct seq_file *m, void *v)
288 struct tcpa_event *event = v;
292 for (i = 0; i < sizeof(struct tcpa_event) + event->event_size; i++)
293 seq_putc(m, data[i]);
298 static int tpm_bios_measurements_release(struct inode *inode,
301 struct seq_file *seq = file->private_data;
302 struct tpm_bios_log *log = seq->private;
305 kfree(log->bios_event_log);
309 return seq_release(inode, file);
312 static int tpm_ascii_bios_measurements_show(struct seq_file *m, void *v)
317 struct tcpa_event *event = v;
318 unsigned char *event_entry =
319 (unsigned char *) (v + sizeof(struct tcpa_event));
321 eventname = kmalloc(MAX_TEXT_EVENT, GFP_KERNEL);
323 printk(KERN_ERR "%s: ERROR - No Memory for event name\n ",
328 seq_printf(m, "%2d ", event->pcr_index);
331 for (i = 0; i < 20; i++)
332 seq_printf(m, "%02x", event->pcr_value[i]);
334 /* 3rd: event type identifier */
335 seq_printf(m, " %02x", event->event_type);
337 len += get_event_name(eventname, event, event_entry);
339 /* 4th: eventname <= max + \'0' delimiter */
340 seq_printf(m, " %s\n", eventname);
346 static struct seq_operations tpm_ascii_b_measurments_seqops = {
347 .start = tpm_bios_measurements_start,
348 .next = tpm_bios_measurements_next,
349 .stop = tpm_bios_measurements_stop,
350 .show = tpm_ascii_bios_measurements_show,
353 static struct seq_operations tpm_binary_b_measurments_seqops = {
354 .start = tpm_bios_measurements_start,
355 .next = tpm_bios_measurements_next,
356 .stop = tpm_bios_measurements_stop,
357 .show = tpm_binary_bios_measurements_show,
360 /* read binary bios log */
361 static int read_log(struct tpm_bios_log *log)
363 struct acpi_tcpa *buff;
365 struct acpi_table_header *virt;
368 if (log->bios_event_log != NULL) {
370 "%s: ERROR - Eventlog already initialized\n",
375 /* Find TCPA entry in RSDT (ACPI_LOGICAL_ADDRESSING) */
376 status = acpi_get_table(ACPI_SIG_TCPA, 1,
377 (struct acpi_table_header **)&buff);
379 if (ACPI_FAILURE(status)) {
380 printk(KERN_ERR "%s: ERROR - Could not get TCPA table\n",
385 switch(buff->platform_class) {
387 len = buff->server.log_max_len;
388 start = buff->server.log_start_addr;
392 len = buff->client.log_max_len;
393 start = buff->client.log_start_addr;
397 printk(KERN_ERR "%s: ERROR - TCPA log area empty\n", __func__);
401 /* malloc EventLog space */
402 log->bios_event_log = kmalloc(len, GFP_KERNEL);
403 if (!log->bios_event_log) {
404 printk("%s: ERROR - Not enough Memory for BIOS measurements\n",
409 log->bios_event_log_end = log->bios_event_log + len;
411 virt = acpi_os_map_memory(start, len);
413 memcpy(log->bios_event_log, virt, len);
415 acpi_os_unmap_memory(virt, len);
419 static int tpm_ascii_bios_measurements_open(struct inode *inode,
423 struct tpm_bios_log *log;
424 struct seq_file *seq;
426 log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
430 if ((err = read_log(log)))
433 /* now register seq file */
434 err = seq_open(file, &tpm_ascii_b_measurments_seqops);
436 seq = file->private_data;
445 kfree(log->bios_event_log);
450 static const struct file_operations tpm_ascii_bios_measurements_ops = {
451 .open = tpm_ascii_bios_measurements_open,
454 .release = tpm_bios_measurements_release,
457 static int tpm_binary_bios_measurements_open(struct inode *inode,
461 struct tpm_bios_log *log;
462 struct seq_file *seq;
464 log = kzalloc(sizeof(struct tpm_bios_log), GFP_KERNEL);
468 if ((err = read_log(log)))
471 /* now register seq file */
472 err = seq_open(file, &tpm_binary_b_measurments_seqops);
474 seq = file->private_data;
483 kfree(log->bios_event_log);
488 static const struct file_operations tpm_binary_bios_measurements_ops = {
489 .open = tpm_binary_bios_measurements_open,
492 .release = tpm_bios_measurements_release,
495 static int is_bad(void *p)
499 if (IS_ERR(p) && (PTR_ERR(p) != -ENODEV))
504 struct dentry **tpm_bios_log_setup(char *name)
506 struct dentry **ret = NULL, *tpm_dir, *bin_file, *ascii_file;
508 tpm_dir = securityfs_create_dir(name, NULL);
513 securityfs_create_file("binary_bios_measurements",
514 S_IRUSR | S_IRGRP, tpm_dir, NULL,
515 &tpm_binary_bios_measurements_ops);
516 if (is_bad(bin_file))
520 securityfs_create_file("ascii_bios_measurements",
521 S_IRUSR | S_IRGRP, tpm_dir, NULL,
522 &tpm_ascii_bios_measurements_ops);
523 if (is_bad(ascii_file))
526 ret = kmalloc(3 * sizeof(struct dentry *), GFP_KERNEL);
537 securityfs_remove(ascii_file);
539 securityfs_remove(bin_file);
541 securityfs_remove(tpm_dir);
545 EXPORT_SYMBOL_GPL(tpm_bios_log_setup);
547 void tpm_bios_log_teardown(struct dentry **lst)
551 for (i = 0; i < 3; i++)
552 securityfs_remove(lst[i]);
554 EXPORT_SYMBOL_GPL(tpm_bios_log_teardown);
555 MODULE_LICENSE("GPL");