2 * Copyright (C) 2005, 2012 IBM Corporation
5 * Kent Yoder <key@linux.vnet.ibm.com>
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>
10 * Nayna Jain <nayna@linux.vnet.ibm.com>
12 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
14 * Access to the event log created by a system's firmware / BIOS
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.
23 #include <linux/seq_file.h>
24 #include <linux/efi.h>
26 #include <linux/security.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/tpm_eventlog.h>
34 static const char* tcpa_event_type_strings
[] = {
45 "Platform Config Flags",
55 static const char* tcpa_pc_event_id_strings
[] = {
66 "Option ROM microcode ",
73 /* returns pointer to start of pos. entry of tcg log */
74 static void *tpm_bios_measurements_start(struct seq_file
*m
, loff_t
*pos
)
77 struct tpm_chip
*chip
= m
->private;
78 struct tpm_bios_log
*log
= &chip
->log
;
79 void *addr
= log
->bios_event_log
;
80 void *limit
= log
->bios_event_log_end
;
81 struct tcpa_event
*event
;
82 u32 converted_event_size
;
83 u32 converted_event_type
;
86 /* read over *pos measurements */
87 for (i
= 0; i
< *pos
; i
++) {
90 converted_event_size
=
91 do_endian_conversion(event
->event_size
);
92 converted_event_type
=
93 do_endian_conversion(event
->event_type
);
95 if ((addr
+ sizeof(struct tcpa_event
)) < limit
) {
96 if ((converted_event_type
== 0) &&
97 (converted_event_size
== 0))
99 addr
+= (sizeof(struct tcpa_event
) +
100 converted_event_size
);
104 /* now check if current entry is valid */
105 if ((addr
+ sizeof(struct tcpa_event
)) >= limit
)
110 converted_event_size
= do_endian_conversion(event
->event_size
);
111 converted_event_type
= do_endian_conversion(event
->event_type
);
113 if (((converted_event_type
== 0) && (converted_event_size
== 0))
114 || ((addr
+ sizeof(struct tcpa_event
) + converted_event_size
)
121 static void *tpm_bios_measurements_next(struct seq_file
*m
, void *v
,
124 struct tcpa_event
*event
= v
;
125 struct tpm_chip
*chip
= m
->private;
126 struct tpm_bios_log
*log
= &chip
->log
;
127 void *limit
= log
->bios_event_log_end
;
128 u32 converted_event_size
;
129 u32 converted_event_type
;
131 converted_event_size
= do_endian_conversion(event
->event_size
);
133 v
+= sizeof(struct tcpa_event
) + converted_event_size
;
135 /* now check if current entry is valid */
136 if ((v
+ sizeof(struct tcpa_event
)) >= limit
)
141 converted_event_size
= do_endian_conversion(event
->event_size
);
142 converted_event_type
= do_endian_conversion(event
->event_type
);
144 if (((converted_event_type
== 0) && (converted_event_size
== 0)) ||
145 ((v
+ sizeof(struct tcpa_event
) + converted_event_size
) >= limit
))
152 static void tpm_bios_measurements_stop(struct seq_file
*m
, void *v
)
156 static int get_event_name(char *dest
, struct tcpa_event
*event
,
157 unsigned char * event_entry
)
159 const char *name
= "";
160 /* 41 so there is room for 40 data and 1 nul */
162 int i
, n_len
= 0, d_len
= 0;
163 struct tcpa_pc_event
*pc_event
;
165 switch (do_endian_conversion(event
->event_type
)) {
173 case PLATFORM_CONFIG_FLAGS
:
174 case TABLE_OF_DEVICES
:
177 case IPL_PARTITION_DATA
:
181 name
= tcpa_event_type_strings
[do_endian_conversion
182 (event
->event_type
)];
183 n_len
= strlen(name
);
188 do_endian_conversion(event
->event_size
)) {
190 n_len
= do_endian_conversion(event
->event_size
);
194 pc_event
= (struct tcpa_pc_event
*)event_entry
;
196 /* ToDo Row data -> Base64 */
198 switch (do_endian_conversion(pc_event
->event_id
)) {
203 case OPTION_ROM_EXEC
:
204 case OPTION_ROM_CONFIG
:
206 name
= tcpa_pc_event_id_strings
[do_endian_conversion
207 (pc_event
->event_id
)];
208 n_len
= strlen(name
);
213 case OPTION_ROM_MICROCODE
:
214 case S_CRTM_CONTENTS
:
216 name
= tcpa_pc_event_id_strings
[do_endian_conversion
217 (pc_event
->event_id
)];
218 n_len
= strlen(name
);
219 for (i
= 0; i
< 20; i
++)
220 d_len
+= sprintf(&data
[2*i
], "%02x",
221 pc_event
->event_data
[i
]);
230 return snprintf(dest
, MAX_TEXT_EVENT
, "[%.*s%.*s]",
231 n_len
, name
, d_len
, data
);
235 static int tpm_binary_bios_measurements_show(struct seq_file
*m
, void *v
)
237 struct tcpa_event
*event
= v
;
238 struct tcpa_event temp_event
;
242 memcpy(&temp_event
, event
, sizeof(struct tcpa_event
));
244 /* convert raw integers for endianness */
245 temp_event
.pcr_index
= do_endian_conversion(event
->pcr_index
);
246 temp_event
.event_type
= do_endian_conversion(event
->event_type
);
247 temp_event
.event_size
= do_endian_conversion(event
->event_size
);
249 temp_ptr
= (char *) &temp_event
;
251 for (i
= 0; i
< (sizeof(struct tcpa_event
) - 1) ; i
++)
252 seq_putc(m
, temp_ptr
[i
]);
254 temp_ptr
= (char *) v
;
256 for (i
= (sizeof(struct tcpa_event
) - 1);
257 i
< (sizeof(struct tcpa_event
) + temp_event
.event_size
); i
++)
258 seq_putc(m
, temp_ptr
[i
]);
264 static int tpm_bios_measurements_release(struct inode
*inode
,
267 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
268 struct tpm_chip
*chip
= (struct tpm_chip
*)seq
->private;
270 put_device(&chip
->dev
);
272 return seq_release(inode
, file
);
275 static int tpm_ascii_bios_measurements_show(struct seq_file
*m
, void *v
)
279 struct tcpa_event
*event
= v
;
280 unsigned char *event_entry
=
281 (unsigned char *)(v
+ sizeof(struct tcpa_event
));
283 eventname
= kmalloc(MAX_TEXT_EVENT
, GFP_KERNEL
);
285 printk(KERN_ERR
"%s: ERROR - No Memory for event name\n ",
291 seq_printf(m
, "%2d ", do_endian_conversion(event
->pcr_index
));
294 seq_printf(m
, "%20phN", event
->pcr_value
);
296 /* 3rd: event type identifier */
297 seq_printf(m
, " %02x", do_endian_conversion(event
->event_type
));
299 len
+= get_event_name(eventname
, event
, event_entry
);
301 /* 4th: eventname <= max + \'0' delimiter */
302 seq_printf(m
, " %s\n", eventname
);
308 static const struct seq_operations tpm_ascii_b_measurements_seqops
= {
309 .start
= tpm_bios_measurements_start
,
310 .next
= tpm_bios_measurements_next
,
311 .stop
= tpm_bios_measurements_stop
,
312 .show
= tpm_ascii_bios_measurements_show
,
315 static const struct seq_operations tpm_binary_b_measurements_seqops
= {
316 .start
= tpm_bios_measurements_start
,
317 .next
= tpm_bios_measurements_next
,
318 .stop
= tpm_bios_measurements_stop
,
319 .show
= tpm_binary_bios_measurements_show
,
322 static int tpm_bios_measurements_open(struct inode
*inode
,
326 struct seq_file
*seq
;
327 struct tpm_chip_seqops
*chip_seqops
;
328 const struct seq_operations
*seqops
;
329 struct tpm_chip
*chip
;
332 if (!inode
->i_private
) {
336 chip_seqops
= (struct tpm_chip_seqops
*)inode
->i_private
;
337 seqops
= chip_seqops
->seqops
;
338 chip
= chip_seqops
->chip
;
339 get_device(&chip
->dev
);
342 /* now register seq file */
343 err
= seq_open(file
, seqops
);
345 seq
= file
->private_data
;
352 static const struct file_operations tpm_bios_measurements_ops
= {
353 .owner
= THIS_MODULE
,
354 .open
= tpm_bios_measurements_open
,
357 .release
= tpm_bios_measurements_release
,
360 static int tpm_read_log(struct tpm_chip
*chip
)
364 if (chip
->log
.bios_event_log
!= NULL
) {
366 "%s: ERROR - event log already initialized\n",
371 rc
= tpm_read_log_acpi(chip
);
375 rc
= tpm_read_log_efi(chip
);
379 return tpm_read_log_of(chip
);
383 * tpm_bios_log_setup() - Read the event log from the firmware
384 * @chip: TPM chip to use.
386 * If an event log is found then the securityfs files are setup to
387 * export it to userspace, otherwise nothing is done.
389 * Returns -ENODEV if the firmware has no event log or securityfs is not
392 int tpm_bios_log_setup(struct tpm_chip
*chip
)
394 const char *name
= dev_name(&chip
->dev
);
399 rc
= tpm_read_log(chip
);
405 chip
->bios_dir
[cnt
] = securityfs_create_dir(name
, NULL
);
406 /* NOTE: securityfs_create_dir can return ENODEV if securityfs is
407 * compiled out. The caller should ignore the ENODEV return code.
409 if (IS_ERR(chip
->bios_dir
[cnt
]))
413 chip
->bin_log_seqops
.chip
= chip
;
414 if (log_version
== EFI_TCG2_EVENT_LOG_FORMAT_TCG_2
)
415 chip
->bin_log_seqops
.seqops
=
416 &tpm2_binary_b_measurements_seqops
;
418 chip
->bin_log_seqops
.seqops
=
419 &tpm_binary_b_measurements_seqops
;
422 chip
->bios_dir
[cnt
] =
423 securityfs_create_file("binary_bios_measurements",
424 0440, chip
->bios_dir
[0],
425 (void *)&chip
->bin_log_seqops
,
426 &tpm_bios_measurements_ops
);
427 if (IS_ERR(chip
->bios_dir
[cnt
]))
431 if (!(chip
->flags
& TPM_CHIP_FLAG_TPM2
)) {
433 chip
->ascii_log_seqops
.chip
= chip
;
434 chip
->ascii_log_seqops
.seqops
=
435 &tpm_ascii_b_measurements_seqops
;
437 chip
->bios_dir
[cnt
] =
438 securityfs_create_file("ascii_bios_measurements",
439 0440, chip
->bios_dir
[0],
440 (void *)&chip
->ascii_log_seqops
,
441 &tpm_bios_measurements_ops
);
442 if (IS_ERR(chip
->bios_dir
[cnt
]))
450 rc
= PTR_ERR(chip
->bios_dir
[cnt
]);
451 chip
->bios_dir
[cnt
] = NULL
;
452 tpm_bios_log_teardown(chip
);
456 void tpm_bios_log_teardown(struct tpm_chip
*chip
)
461 /* securityfs_remove currently doesn't take care of handling sync
462 * between removal and opening of pseudo files. To handle this, a
463 * workaround is added by making i_private = NULL here during removal
464 * and to check it during open(), both within inode_lock()/unlock().
465 * This design ensures that open() either safely gets kref or fails.
467 for (i
= (TPM_NUM_EVENT_LOG_FILES
- 1); i
>= 0; i
--) {
468 if (chip
->bios_dir
[i
]) {
469 inode
= d_inode(chip
->bios_dir
[i
]);
471 inode
->i_private
= NULL
;
473 securityfs_remove(chip
->bios_dir
[i
]);