1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2005, 2012 IBM Corporation
6 * Kent Yoder <key@linux.vnet.ibm.com>
7 * Seiji Munetoh <munetoh@jp.ibm.com>
8 * Stefan Berger <stefanb@us.ibm.com>
9 * Reiner Sailer <sailer@watson.ibm.com>
10 * Kylene Hall <kjhall@us.ibm.com>
11 * Nayna Jain <nayna@linux.vnet.ibm.com>
13 * Access to the event log created by a system's firmware / BIOS
16 #include <linux/seq_file.h>
18 #include <linux/security.h>
19 #include <linux/module.h>
20 #include <linux/tpm_eventlog.h>
25 static int tpm_bios_measurements_open(struct inode
*inode
,
30 struct tpm_chip_seqops
*chip_seqops
;
31 const struct seq_operations
*seqops
;
32 struct tpm_chip
*chip
;
35 if (!inode
->i_private
) {
39 chip_seqops
= inode
->i_private
;
40 seqops
= chip_seqops
->seqops
;
41 chip
= chip_seqops
->chip
;
42 get_device(&chip
->dev
);
45 /* now register seq file */
46 err
= seq_open(file
, seqops
);
48 seq
= file
->private_data
;
51 put_device(&chip
->dev
);
57 static int tpm_bios_measurements_release(struct inode
*inode
,
60 struct seq_file
*seq
= file
->private_data
;
61 struct tpm_chip
*chip
= seq
->private;
63 put_device(&chip
->dev
);
65 return seq_release(inode
, file
);
68 static const struct file_operations tpm_bios_measurements_ops
= {
70 .open
= tpm_bios_measurements_open
,
73 .release
= tpm_bios_measurements_release
,
76 static int tpm_read_log(struct tpm_chip
*chip
)
80 if (chip
->log
.bios_event_log
!= NULL
) {
82 "%s: ERROR - event log already initialized\n",
87 rc
= tpm_read_log_acpi(chip
);
91 rc
= tpm_read_log_efi(chip
);
95 return tpm_read_log_of(chip
);
99 * tpm_bios_log_setup() - Read the event log from the firmware
100 * @chip: TPM chip to use.
102 * If an event log is found then the securityfs files are setup to
103 * export it to userspace, otherwise nothing is done.
105 void tpm_bios_log_setup(struct tpm_chip
*chip
)
107 const char *name
= dev_name(&chip
->dev
);
112 if (chip
->flags
& TPM_CHIP_FLAG_VIRTUAL
)
115 rc
= tpm_read_log(chip
);
121 chip
->bios_dir
[cnt
] = securityfs_create_dir(name
, NULL
);
122 /* NOTE: securityfs_create_dir can return ENODEV if securityfs is
123 * compiled out. The caller should ignore the ENODEV return code.
125 if (IS_ERR(chip
->bios_dir
[cnt
]))
129 chip
->bin_log_seqops
.chip
= chip
;
130 if (log_version
== EFI_TCG2_EVENT_LOG_FORMAT_TCG_2
)
131 chip
->bin_log_seqops
.seqops
=
132 &tpm2_binary_b_measurements_seqops
;
134 chip
->bin_log_seqops
.seqops
=
135 &tpm1_binary_b_measurements_seqops
;
138 chip
->bios_dir
[cnt
] =
139 securityfs_create_file("binary_bios_measurements",
140 0440, chip
->bios_dir
[0],
141 (void *)&chip
->bin_log_seqops
,
142 &tpm_bios_measurements_ops
);
143 if (IS_ERR(chip
->bios_dir
[cnt
]))
147 if (!(chip
->flags
& TPM_CHIP_FLAG_TPM2
)) {
149 chip
->ascii_log_seqops
.chip
= chip
;
150 chip
->ascii_log_seqops
.seqops
=
151 &tpm1_ascii_b_measurements_seqops
;
153 chip
->bios_dir
[cnt
] =
154 securityfs_create_file("ascii_bios_measurements",
155 0440, chip
->bios_dir
[0],
156 (void *)&chip
->ascii_log_seqops
,
157 &tpm_bios_measurements_ops
);
158 if (IS_ERR(chip
->bios_dir
[cnt
]))
166 chip
->bios_dir
[cnt
] = NULL
;
167 tpm_bios_log_teardown(chip
);
171 void tpm_bios_log_teardown(struct tpm_chip
*chip
)
176 /* securityfs_remove currently doesn't take care of handling sync
177 * between removal and opening of pseudo files. To handle this, a
178 * workaround is added by making i_private = NULL here during removal
179 * and to check it during open(), both within inode_lock()/unlock().
180 * This design ensures that open() either safely gets kref or fails.
182 for (i
= (TPM_NUM_EVENT_LOG_FILES
- 1); i
>= 0; i
--) {
183 if (chip
->bios_dir
[i
]) {
184 inode
= d_inode(chip
->bios_dir
[i
]);
186 inode
->i_private
= NULL
;
188 securityfs_remove(chip
->bios_dir
[i
]);