Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 1 | /* |
Kent Yoder | e5dcd87 | 2012-07-11 10:08:12 -0500 | [diff] [blame] | 2 | * Copyright (C) 2005, 2012 IBM Corporation |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 3 | * |
| 4 | * Authors: |
Kent Yoder | e5dcd87 | 2012-07-11 10:08:12 -0500 | [diff] [blame] | 5 | * Kent Yoder <key@linux.vnet.ibm.com> |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 6 | * Seiji Munetoh <munetoh@jp.ibm.com> |
| 7 | * Stefan Berger <stefanb@us.ibm.com> |
| 8 | * Reiner Sailer <sailer@watson.ibm.com> |
| 9 | * Kylene Hall <kjhall@us.ibm.com> |
Nayna Jain | 2528a64 | 2016-10-18 20:49:39 -0400 | [diff] [blame] | 10 | * Nayna Jain <nayna@linux.vnet.ibm.com> |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 11 | * |
Kent Yoder | 8e81cc1 | 2007-08-22 14:01:04 -0700 | [diff] [blame] | 12 | * Maintained by: <tpmdd-devel@lists.sourceforge.net> |
| 13 | * |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 14 | * Access to the event log created by a system's firmware / BIOS |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 15 | * |
| 16 | * This program is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU General Public License |
| 18 | * as published by the Free Software Foundation; either version |
| 19 | * 2 of the License, or (at your option) any later version. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <linux/seq_file.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <linux/security.h> |
| 26 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Kent Yoder | e5dcd87 | 2012-07-11 10:08:12 -0500 | [diff] [blame] | 28 | |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 29 | #include "tpm.h" |
Kent Yoder | e5dcd87 | 2012-07-11 10:08:12 -0500 | [diff] [blame] | 30 | #include "tpm_eventlog.h" |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 31 | |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 32 | |
| 33 | static const char* tcpa_event_type_strings[] = { |
| 34 | "PREBOOT", |
| 35 | "POST CODE", |
| 36 | "", |
| 37 | "NO ACTION", |
| 38 | "SEPARATOR", |
| 39 | "ACTION", |
| 40 | "EVENT TAG", |
| 41 | "S-CRTM Contents", |
| 42 | "S-CRTM Version", |
| 43 | "CPU Microcode", |
| 44 | "Platform Config Flags", |
| 45 | "Table of Devices", |
| 46 | "Compact Hash", |
| 47 | "IPL", |
| 48 | "IPL Partition Data", |
| 49 | "Non-Host Code", |
| 50 | "Non-Host Config", |
| 51 | "Non-Host Info" |
| 52 | }; |
| 53 | |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 54 | static const char* tcpa_pc_event_id_strings[] = { |
Seiji Munetoh | de66a69 | 2006-05-30 21:25:47 -0700 | [diff] [blame] | 55 | "", |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 56 | "SMBIOS", |
| 57 | "BIS Certificate", |
| 58 | "POST BIOS ", |
| 59 | "ESCD ", |
| 60 | "CMOS", |
| 61 | "NVRAM", |
| 62 | "Option ROM", |
| 63 | "Option ROM config", |
Seiji Munetoh | de66a69 | 2006-05-30 21:25:47 -0700 | [diff] [blame] | 64 | "", |
| 65 | "Option ROM microcode ", |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 66 | "S-CRTM Version", |
Seiji Munetoh | de66a69 | 2006-05-30 21:25:47 -0700 | [diff] [blame] | 67 | "S-CRTM Contents ", |
| 68 | "POST Contents ", |
| 69 | "Table of Devices", |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 72 | /* returns pointer to start of pos. entry of tcg log */ |
| 73 | static void *tpm_bios_measurements_start(struct seq_file *m, loff_t *pos) |
| 74 | { |
| 75 | loff_t i; |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 76 | struct tpm_chip *chip = m->private; |
| 77 | struct tpm_bios_log *log = &chip->log; |
Kylene Jo Hall | d09cf7d | 2006-01-08 01:03:48 -0800 | [diff] [blame] | 78 | void *addr = log->bios_event_log; |
| 79 | void *limit = log->bios_event_log_end; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 80 | struct tcpa_event *event; |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 81 | u32 converted_event_size; |
| 82 | u32 converted_event_type; |
| 83 | |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 84 | |
| 85 | /* read over *pos measurements */ |
| 86 | for (i = 0; i < *pos; i++) { |
| 87 | event = addr; |
| 88 | |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 89 | converted_event_size = |
| 90 | do_endian_conversion(event->event_size); |
| 91 | converted_event_type = |
| 92 | do_endian_conversion(event->event_type); |
| 93 | |
Kylene Jo Hall | d09cf7d | 2006-01-08 01:03:48 -0800 | [diff] [blame] | 94 | if ((addr + sizeof(struct tcpa_event)) < limit) { |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 95 | if ((converted_event_type == 0) && |
| 96 | (converted_event_size == 0)) |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 97 | return NULL; |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 98 | addr += (sizeof(struct tcpa_event) + |
| 99 | converted_event_size); |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | /* now check if current entry is valid */ |
Kylene Jo Hall | d09cf7d | 2006-01-08 01:03:48 -0800 | [diff] [blame] | 104 | if ((addr + sizeof(struct tcpa_event)) >= limit) |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 105 | return NULL; |
| 106 | |
| 107 | event = addr; |
| 108 | |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 109 | converted_event_size = do_endian_conversion(event->event_size); |
| 110 | converted_event_type = do_endian_conversion(event->event_type); |
| 111 | |
| 112 | if (((converted_event_type == 0) && (converted_event_size == 0)) |
| 113 | || ((addr + sizeof(struct tcpa_event) + converted_event_size) |
| 114 | >= limit)) |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 115 | return NULL; |
| 116 | |
| 117 | return addr; |
| 118 | } |
| 119 | |
| 120 | static void *tpm_bios_measurements_next(struct seq_file *m, void *v, |
| 121 | loff_t *pos) |
| 122 | { |
| 123 | struct tcpa_event *event = v; |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 124 | struct tpm_chip *chip = m->private; |
| 125 | struct tpm_bios_log *log = &chip->log; |
Kylene Jo Hall | d09cf7d | 2006-01-08 01:03:48 -0800 | [diff] [blame] | 126 | void *limit = log->bios_event_log_end; |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 127 | u32 converted_event_size; |
| 128 | u32 converted_event_type; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 129 | |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 130 | converted_event_size = do_endian_conversion(event->event_size); |
| 131 | |
| 132 | v += sizeof(struct tcpa_event) + converted_event_size; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 133 | |
| 134 | /* now check if current entry is valid */ |
Kylene Jo Hall | d09cf7d | 2006-01-08 01:03:48 -0800 | [diff] [blame] | 135 | if ((v + sizeof(struct tcpa_event)) >= limit) |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 136 | return NULL; |
| 137 | |
| 138 | event = v; |
| 139 | |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 140 | converted_event_size = do_endian_conversion(event->event_size); |
| 141 | converted_event_type = do_endian_conversion(event->event_type); |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 142 | |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 143 | if (((converted_event_type == 0) && (converted_event_size == 0)) || |
| 144 | ((v + sizeof(struct tcpa_event) + converted_event_size) >= limit)) |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 145 | return NULL; |
| 146 | |
| 147 | (*pos)++; |
| 148 | return v; |
| 149 | } |
| 150 | |
| 151 | static void tpm_bios_measurements_stop(struct seq_file *m, void *v) |
| 152 | { |
| 153 | } |
| 154 | |
| 155 | static int get_event_name(char *dest, struct tcpa_event *event, |
| 156 | unsigned char * event_entry) |
| 157 | { |
| 158 | const char *name = ""; |
Eric Paris | fbaa586 | 2009-05-13 12:50:40 -0400 | [diff] [blame] | 159 | /* 41 so there is room for 40 data and 1 nul */ |
| 160 | char data[41] = ""; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 161 | int i, n_len = 0, d_len = 0; |
Seiji Munetoh | de66a69 | 2006-05-30 21:25:47 -0700 | [diff] [blame] | 162 | struct tcpa_pc_event *pc_event; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 163 | |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 164 | switch (do_endian_conversion(event->event_type)) { |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 165 | case PREBOOT: |
| 166 | case POST_CODE: |
| 167 | case UNUSED: |
| 168 | case NO_ACTION: |
| 169 | case SCRTM_CONTENTS: |
| 170 | case SCRTM_VERSION: |
| 171 | case CPU_MICROCODE: |
| 172 | case PLATFORM_CONFIG_FLAGS: |
| 173 | case TABLE_OF_DEVICES: |
| 174 | case COMPACT_HASH: |
| 175 | case IPL: |
| 176 | case IPL_PARTITION_DATA: |
| 177 | case NONHOST_CODE: |
| 178 | case NONHOST_CONFIG: |
| 179 | case NONHOST_INFO: |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 180 | name = tcpa_event_type_strings[do_endian_conversion |
| 181 | (event->event_type)]; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 182 | n_len = strlen(name); |
| 183 | break; |
| 184 | case SEPARATOR: |
| 185 | case ACTION: |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 186 | if (MAX_TEXT_EVENT > |
| 187 | do_endian_conversion(event->event_size)) { |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 188 | name = event_entry; |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 189 | n_len = do_endian_conversion(event->event_size); |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 190 | } |
| 191 | break; |
| 192 | case EVENT_TAG: |
Seiji Munetoh | de66a69 | 2006-05-30 21:25:47 -0700 | [diff] [blame] | 193 | pc_event = (struct tcpa_pc_event *)event_entry; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 194 | |
| 195 | /* ToDo Row data -> Base64 */ |
| 196 | |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 197 | switch (do_endian_conversion(pc_event->event_id)) { |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 198 | case SMBIOS: |
| 199 | case BIS_CERT: |
| 200 | case CMOS: |
| 201 | case NVRAM: |
| 202 | case OPTION_ROM_EXEC: |
| 203 | case OPTION_ROM_CONFIG: |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 204 | case S_CRTM_VERSION: |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 205 | name = tcpa_pc_event_id_strings[do_endian_conversion |
| 206 | (pc_event->event_id)]; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 207 | n_len = strlen(name); |
| 208 | break; |
Seiji Munetoh | de66a69 | 2006-05-30 21:25:47 -0700 | [diff] [blame] | 209 | /* hash data */ |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 210 | case POST_BIOS_ROM: |
| 211 | case ESCD: |
Seiji Munetoh | de66a69 | 2006-05-30 21:25:47 -0700 | [diff] [blame] | 212 | case OPTION_ROM_MICROCODE: |
| 213 | case S_CRTM_CONTENTS: |
| 214 | case POST_CONTENTS: |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 215 | name = tcpa_pc_event_id_strings[do_endian_conversion |
| 216 | (pc_event->event_id)]; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 217 | n_len = strlen(name); |
| 218 | for (i = 0; i < 20; i++) |
Seiji Munetoh | de66a69 | 2006-05-30 21:25:47 -0700 | [diff] [blame] | 219 | d_len += sprintf(&data[2*i], "%02x", |
| 220 | pc_event->event_data[i]); |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 221 | break; |
| 222 | default: |
| 223 | break; |
| 224 | } |
| 225 | default: |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | return snprintf(dest, MAX_TEXT_EVENT, "[%.*s%.*s]", |
| 230 | n_len, name, d_len, data); |
| 231 | |
| 232 | } |
| 233 | |
| 234 | static int tpm_binary_bios_measurements_show(struct seq_file *m, void *v) |
| 235 | { |
Seiji Munetoh | 44d7aff | 2006-05-30 21:25:52 -0700 | [diff] [blame] | 236 | struct tcpa_event *event = v; |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 237 | struct tcpa_event temp_event; |
Harald Hoyer | 186d124 | 2016-02-06 15:44:42 +0100 | [diff] [blame] | 238 | char *temp_ptr; |
Seiji Munetoh | 44d7aff | 2006-05-30 21:25:52 -0700 | [diff] [blame] | 239 | int i; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 240 | |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 241 | memcpy(&temp_event, event, sizeof(struct tcpa_event)); |
| 242 | |
| 243 | /* convert raw integers for endianness */ |
| 244 | temp_event.pcr_index = do_endian_conversion(event->pcr_index); |
| 245 | temp_event.event_type = do_endian_conversion(event->event_type); |
| 246 | temp_event.event_size = do_endian_conversion(event->event_size); |
| 247 | |
Harald Hoyer | 186d124 | 2016-02-06 15:44:42 +0100 | [diff] [blame] | 248 | temp_ptr = (char *) &temp_event; |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 249 | |
Harald Hoyer | 186d124 | 2016-02-06 15:44:42 +0100 | [diff] [blame] | 250 | for (i = 0; i < (sizeof(struct tcpa_event) - 1) ; i++) |
| 251 | seq_putc(m, temp_ptr[i]); |
| 252 | |
| 253 | temp_ptr = (char *) v; |
| 254 | |
| 255 | for (i = (sizeof(struct tcpa_event) - 1); |
| 256 | i < (sizeof(struct tcpa_event) + temp_event.event_size); i++) |
| 257 | seq_putc(m, temp_ptr[i]); |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 258 | |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 259 | return 0; |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 260 | |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static int tpm_bios_measurements_release(struct inode *inode, |
| 264 | struct file *file) |
| 265 | { |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 266 | struct seq_file *seq = (struct seq_file *)file->private_data; |
| 267 | struct tpm_chip *chip = (struct tpm_chip *)seq->private; |
Kylene Jo Hall | d09cf7d | 2006-01-08 01:03:48 -0800 | [diff] [blame] | 268 | |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 269 | put_device(&chip->dev); |
Kylene Jo Hall | d09cf7d | 2006-01-08 01:03:48 -0800 | [diff] [blame] | 270 | |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 271 | return seq_release(inode, file); |
| 272 | } |
| 273 | |
| 274 | static int tpm_ascii_bios_measurements_show(struct seq_file *m, void *v) |
| 275 | { |
| 276 | int len = 0; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 277 | char *eventname; |
| 278 | struct tcpa_event *event = v; |
| 279 | unsigned char *event_entry = |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 280 | (unsigned char *)(v + sizeof(struct tcpa_event)); |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 281 | |
| 282 | eventname = kmalloc(MAX_TEXT_EVENT, GFP_KERNEL); |
| 283 | if (!eventname) { |
| 284 | printk(KERN_ERR "%s: ERROR - No Memory for event name\n ", |
| 285 | __func__); |
| 286 | return -EFAULT; |
| 287 | } |
| 288 | |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 289 | /* 1st: PCR */ |
| 290 | seq_printf(m, "%2d ", do_endian_conversion(event->pcr_index)); |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 291 | |
| 292 | /* 2nd: SHA1 */ |
Andy Shevchenko | a3d64df | 2014-07-09 15:34:41 +0300 | [diff] [blame] | 293 | seq_printf(m, "%20phN", event->pcr_value); |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 294 | |
| 295 | /* 3rd: event type identifier */ |
Hon Ching \(Vicky\) Lo | 0cc698a | 2015-06-17 18:17:08 -0400 | [diff] [blame] | 296 | seq_printf(m, " %02x", do_endian_conversion(event->event_type)); |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 297 | |
| 298 | len += get_event_name(eventname, event, event_entry); |
| 299 | |
| 300 | /* 4th: eventname <= max + \'0' delimiter */ |
| 301 | seq_printf(m, " %s\n", eventname); |
| 302 | |
Kylene Jo Hall | 59e89f3 | 2006-04-22 02:36:35 -0700 | [diff] [blame] | 303 | kfree(eventname); |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 304 | return 0; |
| 305 | } |
| 306 | |
Nayna Jain | 2528a64 | 2016-10-18 20:49:39 -0400 | [diff] [blame] | 307 | static const struct seq_operations tpm_ascii_b_measurements_seqops = { |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 308 | .start = tpm_bios_measurements_start, |
| 309 | .next = tpm_bios_measurements_next, |
| 310 | .stop = tpm_bios_measurements_stop, |
| 311 | .show = tpm_ascii_bios_measurements_show, |
| 312 | }; |
| 313 | |
Nayna Jain | 2528a64 | 2016-10-18 20:49:39 -0400 | [diff] [blame] | 314 | static const struct seq_operations tpm_binary_b_measurements_seqops = { |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 315 | .start = tpm_bios_measurements_start, |
| 316 | .next = tpm_bios_measurements_next, |
| 317 | .stop = tpm_bios_measurements_stop, |
| 318 | .show = tpm_binary_bios_measurements_show, |
| 319 | }; |
| 320 | |
Nayna Jain | 2528a64 | 2016-10-18 20:49:39 -0400 | [diff] [blame] | 321 | static int tpm_bios_measurements_open(struct inode *inode, |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 322 | struct file *file) |
| 323 | { |
| 324 | int err; |
Kylene Jo Hall | d09cf7d | 2006-01-08 01:03:48 -0800 | [diff] [blame] | 325 | struct seq_file *seq; |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 326 | struct tpm_chip_seqops *chip_seqops; |
| 327 | const struct seq_operations *seqops; |
| 328 | struct tpm_chip *chip; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 329 | |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 330 | inode_lock(inode); |
| 331 | if (!inode->i_private) { |
| 332 | inode_unlock(inode); |
| 333 | return -ENODEV; |
| 334 | } |
| 335 | chip_seqops = (struct tpm_chip_seqops *)inode->i_private; |
| 336 | seqops = chip_seqops->seqops; |
| 337 | chip = chip_seqops->chip; |
| 338 | get_device(&chip->dev); |
| 339 | inode_unlock(inode); |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 340 | |
| 341 | /* now register seq file */ |
Nayna Jain | 2528a64 | 2016-10-18 20:49:39 -0400 | [diff] [blame] | 342 | err = seq_open(file, seqops); |
Kylene Jo Hall | d09cf7d | 2006-01-08 01:03:48 -0800 | [diff] [blame] | 343 | if (!err) { |
| 344 | seq = file->private_data; |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 345 | seq->private = chip; |
Kylene Jo Hall | d09cf7d | 2006-01-08 01:03:48 -0800 | [diff] [blame] | 346 | } |
Jesper Juhl | 178554a | 2007-07-20 00:31:48 -0700 | [diff] [blame] | 347 | |
Kylene Jo Hall | d09cf7d | 2006-01-08 01:03:48 -0800 | [diff] [blame] | 348 | return err; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Nayna Jain | 2528a64 | 2016-10-18 20:49:39 -0400 | [diff] [blame] | 351 | static const struct file_operations tpm_bios_measurements_ops = { |
| 352 | .open = tpm_bios_measurements_open, |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 353 | .read = seq_read, |
| 354 | .llseek = seq_lseek, |
| 355 | .release = tpm_bios_measurements_release, |
| 356 | }; |
| 357 | |
Jarkko Sakkinen | cd9b763 | 2016-11-14 05:00:50 -0500 | [diff] [blame] | 358 | int tpm_bios_log_setup(struct tpm_chip *chip) |
Andrew Morton | ca4a031f | 2006-02-01 03:05:01 -0800 | [diff] [blame] | 359 | { |
Jarkko Sakkinen | cd9b763 | 2016-11-14 05:00:50 -0500 | [diff] [blame] | 360 | const char *name = dev_name(&chip->dev); |
| 361 | unsigned int cnt; |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 362 | int rc = 0; |
Andrew Morton | ca4a031f | 2006-02-01 03:05:01 -0800 | [diff] [blame] | 363 | |
Jarkko Sakkinen | 7518a21 | 2016-11-14 05:00:51 -0500 | [diff] [blame] | 364 | if (chip->flags & TPM_CHIP_FLAG_TPM2) |
| 365 | return 0; |
| 366 | |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 367 | rc = read_log(chip); |
| 368 | /* |
| 369 | * read_log failure means event log is not supported except for ENOMEM. |
| 370 | */ |
| 371 | if (rc < 0) { |
| 372 | if (rc == -ENOMEM) |
| 373 | return -ENODEV; |
| 374 | else |
| 375 | return rc; |
| 376 | } |
| 377 | |
Jarkko Sakkinen | cd9b763 | 2016-11-14 05:00:50 -0500 | [diff] [blame] | 378 | cnt = 0; |
| 379 | chip->bios_dir[cnt] = securityfs_create_dir(name, NULL); |
| 380 | /* NOTE: securityfs_create_dir can return ENODEV if securityfs is |
| 381 | * compiled out. The caller should ignore the ENODEV return code. |
| 382 | */ |
| 383 | if (IS_ERR(chip->bios_dir[cnt])) |
| 384 | goto err; |
| 385 | cnt++; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 386 | |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 387 | chip->bin_log_seqops.chip = chip; |
| 388 | chip->bin_log_seqops.seqops = &tpm_binary_b_measurements_seqops; |
| 389 | |
Jarkko Sakkinen | cd9b763 | 2016-11-14 05:00:50 -0500 | [diff] [blame] | 390 | chip->bios_dir[cnt] = |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 391 | securityfs_create_file("binary_bios_measurements", |
Jarkko Sakkinen | cd9b763 | 2016-11-14 05:00:50 -0500 | [diff] [blame] | 392 | 0440, chip->bios_dir[0], |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 393 | (void *)&chip->bin_log_seqops, |
Nayna Jain | 2528a64 | 2016-10-18 20:49:39 -0400 | [diff] [blame] | 394 | &tpm_bios_measurements_ops); |
Jarkko Sakkinen | cd9b763 | 2016-11-14 05:00:50 -0500 | [diff] [blame] | 395 | if (IS_ERR(chip->bios_dir[cnt])) |
| 396 | goto err; |
| 397 | cnt++; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 398 | |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 399 | chip->ascii_log_seqops.chip = chip; |
| 400 | chip->ascii_log_seqops.seqops = &tpm_ascii_b_measurements_seqops; |
| 401 | |
Jarkko Sakkinen | cd9b763 | 2016-11-14 05:00:50 -0500 | [diff] [blame] | 402 | chip->bios_dir[cnt] = |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 403 | securityfs_create_file("ascii_bios_measurements", |
Jarkko Sakkinen | cd9b763 | 2016-11-14 05:00:50 -0500 | [diff] [blame] | 404 | 0440, chip->bios_dir[0], |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 405 | (void *)&chip->ascii_log_seqops, |
Nayna Jain | 2528a64 | 2016-10-18 20:49:39 -0400 | [diff] [blame] | 406 | &tpm_bios_measurements_ops); |
Jarkko Sakkinen | cd9b763 | 2016-11-14 05:00:50 -0500 | [diff] [blame] | 407 | if (IS_ERR(chip->bios_dir[cnt])) |
| 408 | goto err; |
| 409 | cnt++; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 410 | |
Jarkko Sakkinen | cd9b763 | 2016-11-14 05:00:50 -0500 | [diff] [blame] | 411 | return 0; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 412 | |
Jarkko Sakkinen | cd9b763 | 2016-11-14 05:00:50 -0500 | [diff] [blame] | 413 | err: |
| 414 | chip->bios_dir[cnt] = NULL; |
| 415 | tpm_bios_log_teardown(chip); |
| 416 | return -EIO; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 417 | } |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 418 | |
Jarkko Sakkinen | cd9b763 | 2016-11-14 05:00:50 -0500 | [diff] [blame] | 419 | void tpm_bios_log_teardown(struct tpm_chip *chip) |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 420 | { |
| 421 | int i; |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 422 | struct inode *inode; |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 423 | |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 424 | /* securityfs_remove currently doesn't take care of handling sync |
| 425 | * between removal and opening of pseudo files. To handle this, a |
| 426 | * workaround is added by making i_private = NULL here during removal |
| 427 | * and to check it during open(), both within inode_lock()/unlock(). |
| 428 | * This design ensures that open() either safely gets kref or fails. |
| 429 | */ |
| 430 | for (i = (TPM_NUM_EVENT_LOG_FILES - 1); i >= 0; i--) { |
| 431 | inode = d_inode(chip->bios_dir[i]); |
| 432 | inode_lock(inode); |
| 433 | inode->i_private = NULL; |
| 434 | inode_unlock(inode); |
Jarkko Sakkinen | cd9b763 | 2016-11-14 05:00:50 -0500 | [diff] [blame] | 435 | securityfs_remove(chip->bios_dir[i]); |
Nayna Jain | 748935e | 2016-11-14 05:00:52 -0500 | [diff] [blame^] | 436 | } |
Kylene Jo Hall | 55a82ab | 2006-01-08 01:03:15 -0800 | [diff] [blame] | 437 | } |