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 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
15 * Access to the event log created by a system's firmware / BIOS
18 #include <linux/seq_file.h>
19 #include <linux/efi.h>
21 #include <linux/security.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/tpm_eventlog.h>
30 static const char* tcpa_event_type_strings
[] = {
41 "Platform Config Flags",
51 static const char* tcpa_pc_event_id_strings
[] = {
62 "Option ROM microcode ",
69 /* returns pointer to start of pos. entry of tcg log */
70 static void *tpm1_bios_measurements_start(struct seq_file
*m
, loff_t
*pos
)
73 struct tpm_chip
*chip
= m
->private;
74 struct tpm_bios_log
*log
= &chip
->log
;
75 void *addr
= log
->bios_event_log
;
76 void *limit
= log
->bios_event_log_end
;
77 struct tcpa_event
*event
;
78 u32 converted_event_size
;
79 u32 converted_event_type
;
81 /* read over *pos measurements */
85 /* check if current entry is valid */
86 if (addr
+ sizeof(struct tcpa_event
) > limit
)
89 converted_event_size
=
90 do_endian_conversion(event
->event_size
);
91 converted_event_type
=
92 do_endian_conversion(event
->event_type
);
94 if (((converted_event_type
== 0) && (converted_event_size
== 0))
95 || ((addr
+ sizeof(struct tcpa_event
) + converted_event_size
)
102 addr
+= (sizeof(struct tcpa_event
) + converted_event_size
);
108 static void *tpm1_bios_measurements_next(struct seq_file
*m
, void *v
,
111 struct tcpa_event
*event
= v
;
112 struct tpm_chip
*chip
= m
->private;
113 struct tpm_bios_log
*log
= &chip
->log
;
114 void *limit
= log
->bios_event_log_end
;
115 u32 converted_event_size
;
116 u32 converted_event_type
;
119 converted_event_size
= do_endian_conversion(event
->event_size
);
121 v
+= sizeof(struct tcpa_event
) + converted_event_size
;
123 /* now check if current entry is valid */
124 if ((v
+ sizeof(struct tcpa_event
)) > limit
)
129 converted_event_size
= do_endian_conversion(event
->event_size
);
130 converted_event_type
= do_endian_conversion(event
->event_type
);
132 if (((converted_event_type
== 0) && (converted_event_size
== 0)) ||
133 ((v
+ sizeof(struct tcpa_event
) + converted_event_size
) > limit
))
139 static void tpm1_bios_measurements_stop(struct seq_file
*m
, void *v
)
143 static int get_event_name(char *dest
, struct tcpa_event
*event
,
144 unsigned char * event_entry
)
146 const char *name
= "";
147 /* 41 so there is room for 40 data and 1 nul */
149 int i
, n_len
= 0, d_len
= 0;
150 struct tcpa_pc_event
*pc_event
;
152 switch (do_endian_conversion(event
->event_type
)) {
160 case PLATFORM_CONFIG_FLAGS
:
161 case TABLE_OF_DEVICES
:
164 case IPL_PARTITION_DATA
:
168 name
= tcpa_event_type_strings
[do_endian_conversion
169 (event
->event_type
)];
170 n_len
= strlen(name
);
175 do_endian_conversion(event
->event_size
)) {
177 n_len
= do_endian_conversion(event
->event_size
);
181 pc_event
= (struct tcpa_pc_event
*)event_entry
;
183 /* ToDo Row data -> Base64 */
185 switch (do_endian_conversion(pc_event
->event_id
)) {
190 case OPTION_ROM_EXEC
:
191 case OPTION_ROM_CONFIG
:
193 name
= tcpa_pc_event_id_strings
[do_endian_conversion
194 (pc_event
->event_id
)];
195 n_len
= strlen(name
);
200 case OPTION_ROM_MICROCODE
:
201 case S_CRTM_CONTENTS
:
203 name
= tcpa_pc_event_id_strings
[do_endian_conversion
204 (pc_event
->event_id
)];
205 n_len
= strlen(name
);
206 for (i
= 0; i
< 20; i
++)
207 d_len
+= sprintf(&data
[2*i
], "%02x",
208 pc_event
->event_data
[i
]);
218 return snprintf(dest
, MAX_TEXT_EVENT
, "[%.*s%.*s]",
219 n_len
, name
, d_len
, data
);
223 static int tpm1_binary_bios_measurements_show(struct seq_file
*m
, void *v
)
225 struct tcpa_event
*event
= v
;
226 struct tcpa_event temp_event
;
230 memcpy(&temp_event
, event
, sizeof(struct tcpa_event
));
232 /* convert raw integers for endianness */
233 temp_event
.pcr_index
= do_endian_conversion(event
->pcr_index
);
234 temp_event
.event_type
= do_endian_conversion(event
->event_type
);
235 temp_event
.event_size
= do_endian_conversion(event
->event_size
);
237 temp_ptr
= (char *) &temp_event
;
239 for (i
= 0; i
< (sizeof(struct tcpa_event
) - 1) ; i
++)
240 seq_putc(m
, temp_ptr
[i
]);
242 temp_ptr
= (char *) v
;
244 for (i
= (sizeof(struct tcpa_event
) - 1);
245 i
< (sizeof(struct tcpa_event
) + temp_event
.event_size
); i
++)
246 seq_putc(m
, temp_ptr
[i
]);
252 static int tpm1_ascii_bios_measurements_show(struct seq_file
*m
, void *v
)
255 struct tcpa_event
*event
= v
;
256 unsigned char *event_entry
=
257 (unsigned char *)(v
+ sizeof(struct tcpa_event
));
259 eventname
= kmalloc(MAX_TEXT_EVENT
, GFP_KERNEL
);
261 printk(KERN_ERR
"%s: ERROR - No Memory for event name\n ",
267 seq_printf(m
, "%2d ", do_endian_conversion(event
->pcr_index
));
270 seq_printf(m
, "%20phN", event
->pcr_value
);
272 /* 3rd: event type identifier */
273 seq_printf(m
, " %02x", do_endian_conversion(event
->event_type
));
275 get_event_name(eventname
, event
, event_entry
);
277 /* 4th: eventname <= max + \'0' delimiter */
278 seq_printf(m
, " %s\n", eventname
);
284 const struct seq_operations tpm1_ascii_b_measurements_seqops
= {
285 .start
= tpm1_bios_measurements_start
,
286 .next
= tpm1_bios_measurements_next
,
287 .stop
= tpm1_bios_measurements_stop
,
288 .show
= tpm1_ascii_bios_measurements_show
,
291 const struct seq_operations tpm1_binary_b_measurements_seqops
= {
292 .start
= tpm1_bios_measurements_start
,
293 .next
= tpm1_bios_measurements_next
,
294 .stop
= tpm1_bios_measurements_stop
,
295 .show
= tpm1_binary_bios_measurements_show
,