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>
25 #include <linux/security.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
30 #include "tpm_eventlog.h"
33 static const char* tcpa_event_type_strings
[] = {
44 "Platform Config Flags",
54 static const char* tcpa_pc_event_id_strings
[] = {
65 "Option ROM microcode ",
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
)
76 struct tpm_chip
*chip
= m
->private;
77 struct tpm_bios_log
*log
= &chip
->log
;
78 void *addr
= log
->bios_event_log
;
79 void *limit
= log
->bios_event_log_end
;
80 struct tcpa_event
*event
;
81 u32 converted_event_size
;
82 u32 converted_event_type
;
85 /* read over *pos measurements */
86 for (i
= 0; i
< *pos
; i
++) {
89 converted_event_size
=
90 do_endian_conversion(event
->event_size
);
91 converted_event_type
=
92 do_endian_conversion(event
->event_type
);
94 if ((addr
+ sizeof(struct tcpa_event
)) < limit
) {
95 if ((converted_event_type
== 0) &&
96 (converted_event_size
== 0))
98 addr
+= (sizeof(struct tcpa_event
) +
99 converted_event_size
);
103 /* now check if current entry is valid */
104 if ((addr
+ sizeof(struct tcpa_event
)) >= limit
)
109 converted_event_size
= do_endian_conversion(event
->event_size
);
110 converted_event_type
= do_endian_conversion(event
->event_type
);
112 if (((converted_event_type
== 0) && (converted_event_size
== 0))
113 || ((addr
+ sizeof(struct tcpa_event
) + converted_event_size
)
120 static void *tpm_bios_measurements_next(struct seq_file
*m
, void *v
,
123 struct tcpa_event
*event
= v
;
124 struct tpm_chip
*chip
= m
->private;
125 struct tpm_bios_log
*log
= &chip
->log
;
126 void *limit
= log
->bios_event_log_end
;
127 u32 converted_event_size
;
128 u32 converted_event_type
;
130 converted_event_size
= do_endian_conversion(event
->event_size
);
132 v
+= sizeof(struct tcpa_event
) + converted_event_size
;
134 /* now check if current entry is valid */
135 if ((v
+ sizeof(struct tcpa_event
)) >= limit
)
140 converted_event_size
= do_endian_conversion(event
->event_size
);
141 converted_event_type
= do_endian_conversion(event
->event_type
);
143 if (((converted_event_type
== 0) && (converted_event_size
== 0)) ||
144 ((v
+ sizeof(struct tcpa_event
) + converted_event_size
) >= limit
))
151 static void tpm_bios_measurements_stop(struct seq_file
*m
, void *v
)
155 static int get_event_name(char *dest
, struct tcpa_event
*event
,
156 unsigned char * event_entry
)
158 const char *name
= "";
159 /* 41 so there is room for 40 data and 1 nul */
161 int i
, n_len
= 0, d_len
= 0;
162 struct tcpa_pc_event
*pc_event
;
164 switch (do_endian_conversion(event
->event_type
)) {
172 case PLATFORM_CONFIG_FLAGS
:
173 case TABLE_OF_DEVICES
:
176 case IPL_PARTITION_DATA
:
180 name
= tcpa_event_type_strings
[do_endian_conversion
181 (event
->event_type
)];
182 n_len
= strlen(name
);
187 do_endian_conversion(event
->event_size
)) {
189 n_len
= do_endian_conversion(event
->event_size
);
193 pc_event
= (struct tcpa_pc_event
*)event_entry
;
195 /* ToDo Row data -> Base64 */
197 switch (do_endian_conversion(pc_event
->event_id
)) {
202 case OPTION_ROM_EXEC
:
203 case OPTION_ROM_CONFIG
:
205 name
= tcpa_pc_event_id_strings
[do_endian_conversion
206 (pc_event
->event_id
)];
207 n_len
= strlen(name
);
212 case OPTION_ROM_MICROCODE
:
213 case S_CRTM_CONTENTS
:
215 name
= tcpa_pc_event_id_strings
[do_endian_conversion
216 (pc_event
->event_id
)];
217 n_len
= strlen(name
);
218 for (i
= 0; i
< 20; i
++)
219 d_len
+= sprintf(&data
[2*i
], "%02x",
220 pc_event
->event_data
[i
]);
229 return snprintf(dest
, MAX_TEXT_EVENT
, "[%.*s%.*s]",
230 n_len
, name
, d_len
, data
);
234 static int tpm_binary_bios_measurements_show(struct seq_file
*m
, void *v
)
236 struct tcpa_event
*event
= v
;
237 struct tcpa_event temp_event
;
241 memcpy(&temp_event
, event
, sizeof(struct tcpa_event
));
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
);
248 temp_ptr
= (char *) &temp_event
;
250 for (i
= 0; i
< (sizeof(struct tcpa_event
) - 1) ; i
++)
251 seq_putc(m
, temp_ptr
[i
]);
253 temp_ptr
= (char *) v
;
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
]);
263 static int tpm_bios_measurements_release(struct inode
*inode
,
266 struct seq_file
*seq
= (struct seq_file
*)file
->private_data
;
267 struct tpm_chip
*chip
= (struct tpm_chip
*)seq
->private;
269 put_device(&chip
->dev
);
271 return seq_release(inode
, file
);
274 static int tpm_ascii_bios_measurements_show(struct seq_file
*m
, void *v
)
278 struct tcpa_event
*event
= v
;
279 unsigned char *event_entry
=
280 (unsigned char *)(v
+ sizeof(struct tcpa_event
));
282 eventname
= kmalloc(MAX_TEXT_EVENT
, GFP_KERNEL
);
284 printk(KERN_ERR
"%s: ERROR - No Memory for event name\n ",
290 seq_printf(m
, "%2d ", do_endian_conversion(event
->pcr_index
));
293 seq_printf(m
, "%20phN", event
->pcr_value
);
295 /* 3rd: event type identifier */
296 seq_printf(m
, " %02x", do_endian_conversion(event
->event_type
));
298 len
+= get_event_name(eventname
, event
, event_entry
);
300 /* 4th: eventname <= max + \'0' delimiter */
301 seq_printf(m
, " %s\n", eventname
);
307 static const struct seq_operations tpm_ascii_b_measurements_seqops
= {
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
,
314 static const struct seq_operations tpm_binary_b_measurements_seqops
= {
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
,
321 static int tpm_bios_measurements_open(struct inode
*inode
,
325 struct seq_file
*seq
;
326 struct tpm_chip_seqops
*chip_seqops
;
327 const struct seq_operations
*seqops
;
328 struct tpm_chip
*chip
;
331 if (!inode
->i_private
) {
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
);
341 /* now register seq file */
342 err
= seq_open(file
, seqops
);
344 seq
= file
->private_data
;
351 static const struct file_operations tpm_bios_measurements_ops
= {
352 .owner
= THIS_MODULE
,
353 .open
= tpm_bios_measurements_open
,
356 .release
= tpm_bios_measurements_release
,
359 static int tpm_read_log(struct tpm_chip
*chip
)
363 if (chip
->log
.bios_event_log
!= NULL
) {
365 "%s: ERROR - event log already initialized\n",
370 rc
= tpm_read_log_acpi(chip
);
374 return tpm_read_log_of(chip
);
378 * tpm_bios_log_setup() - Read the event log from the firmware
379 * @chip: TPM chip to use.
381 * If an event log is found then the securityfs files are setup to
382 * export it to userspace, otherwise nothing is done.
384 * Returns -ENODEV if the firmware has no event log or securityfs is not
387 int tpm_bios_log_setup(struct tpm_chip
*chip
)
389 const char *name
= dev_name(&chip
->dev
);
393 if (chip
->flags
& TPM_CHIP_FLAG_TPM2
)
396 rc
= tpm_read_log(chip
);
401 chip
->bios_dir
[cnt
] = securityfs_create_dir(name
, NULL
);
402 /* NOTE: securityfs_create_dir can return ENODEV if securityfs is
403 * compiled out. The caller should ignore the ENODEV return code.
405 if (IS_ERR(chip
->bios_dir
[cnt
]))
409 chip
->bin_log_seqops
.chip
= chip
;
410 chip
->bin_log_seqops
.seqops
= &tpm_binary_b_measurements_seqops
;
412 chip
->bios_dir
[cnt
] =
413 securityfs_create_file("binary_bios_measurements",
414 0440, chip
->bios_dir
[0],
415 (void *)&chip
->bin_log_seqops
,
416 &tpm_bios_measurements_ops
);
417 if (IS_ERR(chip
->bios_dir
[cnt
]))
421 chip
->ascii_log_seqops
.chip
= chip
;
422 chip
->ascii_log_seqops
.seqops
= &tpm_ascii_b_measurements_seqops
;
424 chip
->bios_dir
[cnt
] =
425 securityfs_create_file("ascii_bios_measurements",
426 0440, chip
->bios_dir
[0],
427 (void *)&chip
->ascii_log_seqops
,
428 &tpm_bios_measurements_ops
);
429 if (IS_ERR(chip
->bios_dir
[cnt
]))
436 rc
= PTR_ERR(chip
->bios_dir
[cnt
]);
437 chip
->bios_dir
[cnt
] = NULL
;
438 tpm_bios_log_teardown(chip
);
442 void tpm_bios_log_teardown(struct tpm_chip
*chip
)
447 /* securityfs_remove currently doesn't take care of handling sync
448 * between removal and opening of pseudo files. To handle this, a
449 * workaround is added by making i_private = NULL here during removal
450 * and to check it during open(), both within inode_lock()/unlock().
451 * This design ensures that open() either safely gets kref or fails.
453 for (i
= (TPM_NUM_EVENT_LOG_FILES
- 1); i
>= 0; i
--) {
454 if (chip
->bios_dir
[i
]) {
455 inode
= d_inode(chip
->bios_dir
[i
]);
457 inode
->i_private
= NULL
;
459 securityfs_remove(chip
->bios_dir
[i
]);