xref: /openbmc/linux/drivers/char/tpm/eventlog/tpm1.c (revision 3213486f)
1 /*
2  * Copyright (C) 2005, 2012 IBM Corporation
3  *
4  * Authors:
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>
11  *
12  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
13  *
14  * Access to the event log created by a system's firmware / BIOS
15  *
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.
20  *
21  */
22 
23 #include <linux/seq_file.h>
24 #include <linux/efi.h>
25 #include <linux/fs.h>
26 #include <linux/security.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/tpm_eventlog.h>
30 
31 #include "../tpm.h"
32 #include "common.h"
33 
34 
35 static const char* tcpa_event_type_strings[] = {
36 	"PREBOOT",
37 	"POST CODE",
38 	"",
39 	"NO ACTION",
40 	"SEPARATOR",
41 	"ACTION",
42 	"EVENT TAG",
43 	"S-CRTM Contents",
44 	"S-CRTM Version",
45 	"CPU Microcode",
46 	"Platform Config Flags",
47 	"Table of Devices",
48 	"Compact Hash",
49 	"IPL",
50 	"IPL Partition Data",
51 	"Non-Host Code",
52 	"Non-Host Config",
53 	"Non-Host Info"
54 };
55 
56 static const char* tcpa_pc_event_id_strings[] = {
57 	"",
58 	"SMBIOS",
59 	"BIS Certificate",
60 	"POST BIOS ",
61 	"ESCD ",
62 	"CMOS",
63 	"NVRAM",
64 	"Option ROM",
65 	"Option ROM config",
66 	"",
67 	"Option ROM microcode ",
68 	"S-CRTM Version",
69 	"S-CRTM Contents ",
70 	"POST Contents ",
71 	"Table of Devices",
72 };
73 
74 /* returns pointer to start of pos. entry of tcg log */
75 static void *tpm1_bios_measurements_start(struct seq_file *m, loff_t *pos)
76 {
77 	loff_t i = 0;
78 	struct tpm_chip *chip = m->private;
79 	struct tpm_bios_log *log = &chip->log;
80 	void *addr = log->bios_event_log;
81 	void *limit = log->bios_event_log_end;
82 	struct tcpa_event *event;
83 	u32 converted_event_size;
84 	u32 converted_event_type;
85 
86 	/* read over *pos measurements */
87 	do {
88 		event = addr;
89 
90 		/* check if current entry is valid */
91 		if (addr + sizeof(struct tcpa_event) > limit)
92 			return NULL;
93 
94 		converted_event_size =
95 		    do_endian_conversion(event->event_size);
96 		converted_event_type =
97 		    do_endian_conversion(event->event_type);
98 
99 		if (((converted_event_type == 0) && (converted_event_size == 0))
100 		    || ((addr + sizeof(struct tcpa_event) + converted_event_size)
101 			> limit))
102 			return NULL;
103 
104 		if (i++ == *pos)
105 			break;
106 
107 		addr += (sizeof(struct tcpa_event) + converted_event_size);
108 	} while (1);
109 
110 	return addr;
111 }
112 
113 static void *tpm1_bios_measurements_next(struct seq_file *m, void *v,
114 					loff_t *pos)
115 {
116 	struct tcpa_event *event = v;
117 	struct tpm_chip *chip = m->private;
118 	struct tpm_bios_log *log = &chip->log;
119 	void *limit = log->bios_event_log_end;
120 	u32 converted_event_size;
121 	u32 converted_event_type;
122 
123 	converted_event_size = do_endian_conversion(event->event_size);
124 
125 	v += sizeof(struct tcpa_event) + converted_event_size;
126 
127 	/* now check if current entry is valid */
128 	if ((v + sizeof(struct tcpa_event)) > limit)
129 		return NULL;
130 
131 	event = v;
132 
133 	converted_event_size = do_endian_conversion(event->event_size);
134 	converted_event_type = do_endian_conversion(event->event_type);
135 
136 	if (((converted_event_type == 0) && (converted_event_size == 0)) ||
137 	    ((v + sizeof(struct tcpa_event) + converted_event_size) > limit))
138 		return NULL;
139 
140 	(*pos)++;
141 	return v;
142 }
143 
144 static void tpm1_bios_measurements_stop(struct seq_file *m, void *v)
145 {
146 }
147 
148 static int get_event_name(char *dest, struct tcpa_event *event,
149 			unsigned char * event_entry)
150 {
151 	const char *name = "";
152 	/* 41 so there is room for 40 data and 1 nul */
153 	char data[41] = "";
154 	int i, n_len = 0, d_len = 0;
155 	struct tcpa_pc_event *pc_event;
156 
157 	switch (do_endian_conversion(event->event_type)) {
158 	case PREBOOT:
159 	case POST_CODE:
160 	case UNUSED:
161 	case NO_ACTION:
162 	case SCRTM_CONTENTS:
163 	case SCRTM_VERSION:
164 	case CPU_MICROCODE:
165 	case PLATFORM_CONFIG_FLAGS:
166 	case TABLE_OF_DEVICES:
167 	case COMPACT_HASH:
168 	case IPL:
169 	case IPL_PARTITION_DATA:
170 	case NONHOST_CODE:
171 	case NONHOST_CONFIG:
172 	case NONHOST_INFO:
173 		name = tcpa_event_type_strings[do_endian_conversion
174 						(event->event_type)];
175 		n_len = strlen(name);
176 		break;
177 	case SEPARATOR:
178 	case ACTION:
179 		if (MAX_TEXT_EVENT >
180 		    do_endian_conversion(event->event_size)) {
181 			name = event_entry;
182 			n_len = do_endian_conversion(event->event_size);
183 		}
184 		break;
185 	case EVENT_TAG:
186 		pc_event = (struct tcpa_pc_event *)event_entry;
187 
188 		/* ToDo Row data -> Base64 */
189 
190 		switch (do_endian_conversion(pc_event->event_id)) {
191 		case SMBIOS:
192 		case BIS_CERT:
193 		case CMOS:
194 		case NVRAM:
195 		case OPTION_ROM_EXEC:
196 		case OPTION_ROM_CONFIG:
197 		case S_CRTM_VERSION:
198 			name = tcpa_pc_event_id_strings[do_endian_conversion
199 							(pc_event->event_id)];
200 			n_len = strlen(name);
201 			break;
202 		/* hash data */
203 		case POST_BIOS_ROM:
204 		case ESCD:
205 		case OPTION_ROM_MICROCODE:
206 		case S_CRTM_CONTENTS:
207 		case POST_CONTENTS:
208 			name = tcpa_pc_event_id_strings[do_endian_conversion
209 							(pc_event->event_id)];
210 			n_len = strlen(name);
211 			for (i = 0; i < 20; i++)
212 				d_len += sprintf(&data[2*i], "%02x",
213 						pc_event->event_data[i]);
214 			break;
215 		default:
216 			break;
217 		}
218 	default:
219 		break;
220 	}
221 
222 	return snprintf(dest, MAX_TEXT_EVENT, "[%.*s%.*s]",
223 			n_len, name, d_len, data);
224 
225 }
226 
227 static int tpm1_binary_bios_measurements_show(struct seq_file *m, void *v)
228 {
229 	struct tcpa_event *event = v;
230 	struct tcpa_event temp_event;
231 	char *temp_ptr;
232 	int i;
233 
234 	memcpy(&temp_event, event, sizeof(struct tcpa_event));
235 
236 	/* convert raw integers for endianness */
237 	temp_event.pcr_index = do_endian_conversion(event->pcr_index);
238 	temp_event.event_type = do_endian_conversion(event->event_type);
239 	temp_event.event_size = do_endian_conversion(event->event_size);
240 
241 	temp_ptr = (char *) &temp_event;
242 
243 	for (i = 0; i < (sizeof(struct tcpa_event) - 1) ; i++)
244 		seq_putc(m, temp_ptr[i]);
245 
246 	temp_ptr = (char *) v;
247 
248 	for (i = (sizeof(struct tcpa_event) - 1);
249 	     i < (sizeof(struct tcpa_event) + temp_event.event_size); i++)
250 		seq_putc(m, temp_ptr[i]);
251 
252 	return 0;
253 
254 }
255 
256 static int tpm1_ascii_bios_measurements_show(struct seq_file *m, void *v)
257 {
258 	int len = 0;
259 	char *eventname;
260 	struct tcpa_event *event = v;
261 	unsigned char *event_entry =
262 	    (unsigned char *)(v + sizeof(struct tcpa_event));
263 
264 	eventname = kmalloc(MAX_TEXT_EVENT, GFP_KERNEL);
265 	if (!eventname) {
266 		printk(KERN_ERR "%s: ERROR - No Memory for event name\n ",
267 		       __func__);
268 		return -EFAULT;
269 	}
270 
271 	/* 1st: PCR */
272 	seq_printf(m, "%2d ", do_endian_conversion(event->pcr_index));
273 
274 	/* 2nd: SHA1 */
275 	seq_printf(m, "%20phN", event->pcr_value);
276 
277 	/* 3rd: event type identifier */
278 	seq_printf(m, " %02x", do_endian_conversion(event->event_type));
279 
280 	len += get_event_name(eventname, event, event_entry);
281 
282 	/* 4th: eventname <= max + \'0' delimiter */
283 	seq_printf(m, " %s\n", eventname);
284 
285 	kfree(eventname);
286 	return 0;
287 }
288 
289 const struct seq_operations tpm1_ascii_b_measurements_seqops = {
290 	.start = tpm1_bios_measurements_start,
291 	.next = tpm1_bios_measurements_next,
292 	.stop = tpm1_bios_measurements_stop,
293 	.show = tpm1_ascii_bios_measurements_show,
294 };
295 
296 const struct seq_operations tpm1_binary_b_measurements_seqops = {
297 	.start = tpm1_bios_measurements_start,
298 	.next = tpm1_bios_measurements_next,
299 	.stop = tpm1_bios_measurements_stop,
300 	.show = tpm1_binary_bios_measurements_show,
301 };
302