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>
11 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
13 * Access to the eventlog created by a system's firmware / BIOS
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
22 #include <linux/seq_file.h>
24 #include <linux/security.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
29 #include "tpm_eventlog.h"
32 static const char* tcpa_event_type_strings
[] = {
43 "Platform Config Flags",
53 static const char* tcpa_pc_event_id_strings
[] = {
64 "Option ROM microcode ",
71 /* returns pointer to start of pos. entry of tcg log */
72 static void *tpm_bios_measurements_start(struct seq_file
*m
, loff_t
*pos
)
75 struct tpm_bios_log
*log
= m
->private;
76 void *addr
= log
->bios_event_log
;
77 void *limit
= log
->bios_event_log_end
;
78 struct tcpa_event
*event
;
79 u32 converted_event_size
;
80 u32 converted_event_type
;
83 /* read over *pos measurements */
84 for (i
= 0; i
< *pos
; i
++) {
87 converted_event_size
=
88 do_endian_conversion(event
->event_size
);
89 converted_event_type
=
90 do_endian_conversion(event
->event_type
);
92 if ((addr
+ sizeof(struct tcpa_event
)) < limit
) {
93 if ((converted_event_type
== 0) &&
94 (converted_event_size
== 0))
96 addr
+= (sizeof(struct tcpa_event
) +
97 converted_event_size
);
101 /* now check if current entry is valid */
102 if ((addr
+ sizeof(struct tcpa_event
)) >= limit
)
107 converted_event_size
= do_endian_conversion(event
->event_size
);
108 converted_event_type
= do_endian_conversion(event
->event_type
);
110 if (((converted_event_type
== 0) && (converted_event_size
== 0))
111 || ((addr
+ sizeof(struct tcpa_event
) + converted_event_size
)
118 static void *tpm_bios_measurements_next(struct seq_file
*m
, void *v
,
121 struct tcpa_event
*event
= v
;
122 struct tpm_bios_log
*log
= m
->private;
123 void *limit
= log
->bios_event_log_end
;
124 u32 converted_event_size
;
125 u32 converted_event_type
;
127 converted_event_size
= do_endian_conversion(event
->event_size
);
129 v
+= sizeof(struct tcpa_event
) + converted_event_size
;
131 /* now check if current entry is valid */
132 if ((v
+ sizeof(struct tcpa_event
)) >= limit
)
137 converted_event_size
= do_endian_conversion(event
->event_size
);
138 converted_event_type
= do_endian_conversion(event
->event_type
);
140 if (((converted_event_type
== 0) && (converted_event_size
== 0)) ||
141 ((v
+ sizeof(struct tcpa_event
) + converted_event_size
) >= limit
))
148 static void tpm_bios_measurements_stop(struct seq_file
*m
, void *v
)
152 static int get_event_name(char *dest
, struct tcpa_event
*event
,
153 unsigned char * event_entry
)
155 const char *name
= "";
156 /* 41 so there is room for 40 data and 1 nul */
158 int i
, n_len
= 0, d_len
= 0;
159 struct tcpa_pc_event
*pc_event
;
161 switch (do_endian_conversion(event
->event_type
)) {
169 case PLATFORM_CONFIG_FLAGS
:
170 case TABLE_OF_DEVICES
:
173 case IPL_PARTITION_DATA
:
177 name
= tcpa_event_type_strings
[do_endian_conversion
178 (event
->event_type
)];
179 n_len
= strlen(name
);
184 do_endian_conversion(event
->event_size
)) {
186 n_len
= do_endian_conversion(event
->event_size
);
190 pc_event
= (struct tcpa_pc_event
*)event_entry
;
192 /* ToDo Row data -> Base64 */
194 switch (do_endian_conversion(pc_event
->event_id
)) {
199 case OPTION_ROM_EXEC
:
200 case OPTION_ROM_CONFIG
:
202 name
= tcpa_pc_event_id_strings
[do_endian_conversion
203 (pc_event
->event_id
)];
204 n_len
= strlen(name
);
209 case OPTION_ROM_MICROCODE
:
210 case S_CRTM_CONTENTS
:
212 name
= tcpa_pc_event_id_strings
[do_endian_conversion
213 (pc_event
->event_id
)];
214 n_len
= strlen(name
);
215 for (i
= 0; i
< 20; i
++)
216 d_len
+= sprintf(&data
[2*i
], "%02x",
217 pc_event
->event_data
[i
]);
226 return snprintf(dest
, MAX_TEXT_EVENT
, "[%.*s%.*s]",
227 n_len
, name
, d_len
, data
);
231 static int tpm_binary_bios_measurements_show(struct seq_file
*m
, void *v
)
233 struct tcpa_event
*event
= v
;
234 struct tcpa_event temp_event
;
238 memcpy(&temp_event
, event
, sizeof(struct tcpa_event
));
240 /* convert raw integers for endianness */
241 temp_event
.pcr_index
= do_endian_conversion(event
->pcr_index
);
242 temp_event
.event_type
= do_endian_conversion(event
->event_type
);
243 temp_event
.event_size
= do_endian_conversion(event
->event_size
);
245 temp_ptr
= (char *) &temp_event
;
247 for (i
= 0; i
< (sizeof(struct tcpa_event
) - 1) ; i
++)
248 seq_putc(m
, temp_ptr
[i
]);
250 temp_ptr
= (char *) v
;
252 for (i
= (sizeof(struct tcpa_event
) - 1);
253 i
< (sizeof(struct tcpa_event
) + temp_event
.event_size
); i
++)
254 seq_putc(m
, temp_ptr
[i
]);
260 static int tpm_bios_measurements_release(struct inode
*inode
,
263 struct seq_file
*seq
= file
->private_data
;
264 struct tpm_bios_log
*log
= seq
->private;
267 kfree(log
->bios_event_log
);
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_measurments_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_measurments_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_ascii_bios_measurements_open(struct inode
*inode
,
325 struct tpm_bios_log
*log
;
326 struct seq_file
*seq
;
328 log
= kzalloc(sizeof(struct tpm_bios_log
), GFP_KERNEL
);
332 if ((err
= read_log(log
)))
335 /* now register seq file */
336 err
= seq_open(file
, &tpm_ascii_b_measurments_seqops
);
338 seq
= file
->private_data
;
347 kfree(log
->bios_event_log
);
352 static const struct file_operations tpm_ascii_bios_measurements_ops
= {
353 .open
= tpm_ascii_bios_measurements_open
,
356 .release
= tpm_bios_measurements_release
,
359 static int tpm_binary_bios_measurements_open(struct inode
*inode
,
363 struct tpm_bios_log
*log
;
364 struct seq_file
*seq
;
366 log
= kzalloc(sizeof(struct tpm_bios_log
), GFP_KERNEL
);
370 if ((err
= read_log(log
)))
373 /* now register seq file */
374 err
= seq_open(file
, &tpm_binary_b_measurments_seqops
);
376 seq
= file
->private_data
;
385 kfree(log
->bios_event_log
);
390 static const struct file_operations tpm_binary_bios_measurements_ops
= {
391 .open
= tpm_binary_bios_measurements_open
,
394 .release
= tpm_bios_measurements_release
,
397 static int is_bad(void *p
)
401 if (IS_ERR(p
) && (PTR_ERR(p
) != -ENODEV
))
406 struct dentry
**tpm_bios_log_setup(const char *name
)
408 struct dentry
**ret
= NULL
, *tpm_dir
, *bin_file
, *ascii_file
;
410 tpm_dir
= securityfs_create_dir(name
, NULL
);
415 securityfs_create_file("binary_bios_measurements",
416 S_IRUSR
| S_IRGRP
, tpm_dir
, NULL
,
417 &tpm_binary_bios_measurements_ops
);
418 if (is_bad(bin_file
))
422 securityfs_create_file("ascii_bios_measurements",
423 S_IRUSR
| S_IRGRP
, tpm_dir
, NULL
,
424 &tpm_ascii_bios_measurements_ops
);
425 if (is_bad(ascii_file
))
428 ret
= kmalloc(3 * sizeof(struct dentry
*), GFP_KERNEL
);
439 securityfs_remove(ascii_file
);
441 securityfs_remove(bin_file
);
443 securityfs_remove(tpm_dir
);
448 void tpm_bios_log_teardown(struct dentry
**lst
)
452 for (i
= 0; i
< 3; i
++)
453 securityfs_remove(lst
[i
]);