xref: /linux/drivers/char/tpm/tpm_ppi.c (revision 84d4e8b613e073d9dfde782c471aedbcefdede6c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012-2014 Intel Corporation
4  *
5  * Authors:
6  * Xiaoyan Zhang <xiaoyan.zhang@intel.com>
7  * Jiang Liu <jiang.liu@linux.intel.com>
8  * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
9  *
10  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
11  *
12  * This file contains implementation of the sysfs interface for PPI.
13  */
14 
15 
16 #include <linux/acpi.h>
17 #include "tpm.h"
18 
19 #define TPM_PPI_REVISION_ID_1	1
20 #define TPM_PPI_REVISION_ID_2	2
21 #define TPM_PPI_FN_VERSION	1
22 #define TPM_PPI_FN_SUBREQ	2
23 #define TPM_PPI_FN_GETREQ	3
24 #define TPM_PPI_FN_GETACT	4
25 #define TPM_PPI_FN_GETRSP	5
26 #define TPM_PPI_FN_SUBREQ2	7
27 #define TPM_PPI_FN_GETOPR	8
28 #define PPI_TPM_REQ_MAX		101 /* PPI 1.3 for TPM 2 */
29 #define PPI_VS_REQ_START	128
30 #define PPI_VS_REQ_END		255
31 
32 static const guid_t tpm_ppi_guid =
33 	GUID_INIT(0x3DDDFAA6, 0x361B, 0x4EB4,
34 		  0xA4, 0x24, 0x8D, 0x10, 0x08, 0x9D, 0x16, 0x53);
35 
36 static const char * const tpm_ppi_info[] = {
37 	"Not implemented",
38 	"BIOS only",
39 	"Blocked for OS by system firmware",
40 	"User required",
41 	"User not required",
42 };
43 
44 /* A spinlock to protect access to the cache from concurrent reads */
45 static DEFINE_MUTEX(tpm_ppi_lock);
46 
47 static u32 ppi_operations_cache[PPI_VS_REQ_END + 1];
48 static bool ppi_cache_populated;
49 
tpm_ppi_req_has_parameter(u64 req)50 static bool tpm_ppi_req_has_parameter(u64 req)
51 {
52 	return req == 23;
53 }
54 
55 static inline union acpi_object *
tpm_eval_dsm(acpi_handle ppi_handle,int func,acpi_object_type type,union acpi_object * argv4,u64 rev)56 tpm_eval_dsm(acpi_handle ppi_handle, int func, acpi_object_type type,
57 	     union acpi_object *argv4, u64 rev)
58 {
59 	BUG_ON(!ppi_handle);
60 	return acpi_evaluate_dsm_typed(ppi_handle, &tpm_ppi_guid,
61 				       rev, func, argv4, type);
62 }
63 
tpm_show_ppi_version(struct device * dev,struct device_attribute * attr,char * buf)64 static ssize_t tpm_show_ppi_version(struct device *dev,
65 				    struct device_attribute *attr, char *buf)
66 {
67 	struct tpm_chip *chip = to_tpm_chip(dev);
68 
69 	return sysfs_emit(buf, "%s\n", chip->ppi_version);
70 }
71 
tpm_show_ppi_request(struct device * dev,struct device_attribute * attr,char * buf)72 static ssize_t tpm_show_ppi_request(struct device *dev,
73 				    struct device_attribute *attr, char *buf)
74 {
75 	ssize_t size = -EINVAL;
76 	union acpi_object *obj;
77 	struct tpm_chip *chip = to_tpm_chip(dev);
78 	u64 rev = TPM_PPI_REVISION_ID_2;
79 	u64 req;
80 
81 	if (strcmp(chip->ppi_version, "1.2") < 0)
82 		rev = TPM_PPI_REVISION_ID_1;
83 
84 	obj = tpm_eval_dsm(chip->acpi_dev_handle, TPM_PPI_FN_GETREQ,
85 			   ACPI_TYPE_PACKAGE, NULL, rev);
86 	if (!obj)
87 		return -ENXIO;
88 
89 	/*
90 	 * output.pointer should be of package type, including two integers.
91 	 * The first is function return code, 0 means success and 1 means
92 	 * error. The second is pending TPM operation requested by the OS, 0
93 	 * means none and >0 means operation value.
94 	 */
95 	if (obj->package.count == 3 &&
96 	    obj->package.elements[0].type == ACPI_TYPE_INTEGER &&
97 	    obj->package.elements[1].type == ACPI_TYPE_INTEGER &&
98 	    obj->package.elements[2].type == ACPI_TYPE_INTEGER) {
99 		if (obj->package.elements[0].integer.value)
100 			size = -EFAULT;
101 		else {
102 			req = obj->package.elements[1].integer.value;
103 			if (tpm_ppi_req_has_parameter(req))
104 				size = sysfs_emit(buf, "%llu %llu\n", req,
105 						  obj->package.elements[2].integer.value);
106 			else
107 				size = sysfs_emit(buf, "%llu\n", req);
108 		}
109 	} else if (obj->package.count == 2 &&
110 	    obj->package.elements[0].type == ACPI_TYPE_INTEGER &&
111 	    obj->package.elements[1].type == ACPI_TYPE_INTEGER) {
112 		if (obj->package.elements[0].integer.value)
113 			size = -EFAULT;
114 		else
115 			size = sysfs_emit(buf, "%llu\n",
116 					  obj->package.elements[1].integer.value);
117 	}
118 
119 	ACPI_FREE(obj);
120 
121 	return size;
122 }
123 
tpm_store_ppi_request(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)124 static ssize_t tpm_store_ppi_request(struct device *dev,
125 				     struct device_attribute *attr,
126 				     const char *buf, size_t count)
127 {
128 	u32 req;
129 	u64 ret;
130 	int func = TPM_PPI_FN_SUBREQ;
131 	union acpi_object *obj, tmp[2];
132 	union acpi_object argv4 = ACPI_INIT_DSM_ARGV4(2, tmp);
133 	struct tpm_chip *chip = to_tpm_chip(dev);
134 	u64 rev = TPM_PPI_REVISION_ID_1;
135 
136 	/*
137 	 * the function to submit TPM operation request to pre-os environment
138 	 * is updated with function index from SUBREQ to SUBREQ2 since PPI
139 	 * version 1.1
140 	 */
141 	if (acpi_check_dsm(chip->acpi_dev_handle, &tpm_ppi_guid,
142 			   TPM_PPI_REVISION_ID_1, 1 << TPM_PPI_FN_SUBREQ2))
143 		func = TPM_PPI_FN_SUBREQ2;
144 
145 	/*
146 	 * PPI spec defines params[3].type as ACPI_TYPE_PACKAGE. Some BIOS
147 	 * accept buffer/string/integer type, but some BIOS accept buffer/
148 	 * string/package type. For PPI version 1.0 and 1.1, use buffer type
149 	 * for compatibility, and use package type since 1.2 according to spec.
150 	 */
151 	if (strcmp(chip->ppi_version, "1.3") == 0) {
152 		if (sscanf(buf, "%llu %llu", &tmp[0].integer.value,
153 			   &tmp[1].integer.value) != 2)
154 			goto ppi12;
155 		rev = TPM_PPI_REVISION_ID_2;
156 		tmp[0].type = ACPI_TYPE_INTEGER;
157 		tmp[1].type = ACPI_TYPE_INTEGER;
158 	} else if (strcmp(chip->ppi_version, "1.2") < 0) {
159 		if (sscanf(buf, "%d", &req) != 1)
160 			return -EINVAL;
161 		argv4.type = ACPI_TYPE_BUFFER;
162 		argv4.buffer.length = sizeof(req);
163 		argv4.buffer.pointer = (u8 *)&req;
164 	} else {
165 ppi12:
166 		argv4.package.count = 1;
167 		tmp[0].type = ACPI_TYPE_INTEGER;
168 		if (sscanf(buf, "%llu", &tmp[0].integer.value) != 1)
169 			return -EINVAL;
170 	}
171 
172 	obj = tpm_eval_dsm(chip->acpi_dev_handle, func, ACPI_TYPE_INTEGER,
173 			   &argv4, rev);
174 	if (!obj) {
175 		return -ENXIO;
176 	} else {
177 		ret = obj->integer.value;
178 		ACPI_FREE(obj);
179 	}
180 
181 	if (ret == 0)
182 		return (acpi_status)count;
183 
184 	return (ret == 1) ? -EPERM : -EFAULT;
185 }
186 
tpm_show_ppi_transition_action(struct device * dev,struct device_attribute * attr,char * buf)187 static ssize_t tpm_show_ppi_transition_action(struct device *dev,
188 					      struct device_attribute *attr,
189 					      char *buf)
190 {
191 	u32 ret;
192 	acpi_status status;
193 	union acpi_object *obj = NULL;
194 	union acpi_object tmp = {
195 		.buffer.type = ACPI_TYPE_BUFFER,
196 		.buffer.length = 0,
197 		.buffer.pointer = NULL
198 	};
199 	struct tpm_chip *chip = to_tpm_chip(dev);
200 
201 	static char *info[] = {
202 		"None",
203 		"Shutdown",
204 		"Reboot",
205 		"OS Vendor-specific",
206 		"Error",
207 	};
208 
209 	/*
210 	 * PPI spec defines params[3].type as empty package, but some platforms
211 	 * (e.g. Capella with PPI 1.0) need integer/string/buffer type, so for
212 	 * compatibility, define params[3].type as buffer, if PPI version < 1.2
213 	 */
214 	if (strcmp(chip->ppi_version, "1.2") < 0)
215 		obj = &tmp;
216 	obj = tpm_eval_dsm(chip->acpi_dev_handle, TPM_PPI_FN_GETACT,
217 			   ACPI_TYPE_INTEGER, obj, TPM_PPI_REVISION_ID_1);
218 	if (!obj) {
219 		return -ENXIO;
220 	} else {
221 		ret = obj->integer.value;
222 		ACPI_FREE(obj);
223 	}
224 
225 	if (ret < ARRAY_SIZE(info) - 1)
226 		status = sysfs_emit(buf, "%d: %s\n", ret, info[ret]);
227 	else
228 		status = sysfs_emit(buf, "%d: %s\n", ret,
229 				    info[ARRAY_SIZE(info) - 1]);
230 	return status;
231 }
232 
tpm_show_ppi_response(struct device * dev,struct device_attribute * attr,char * buf)233 static ssize_t tpm_show_ppi_response(struct device *dev,
234 				     struct device_attribute *attr,
235 				     char *buf)
236 {
237 	acpi_status status = -EINVAL;
238 	union acpi_object *obj, *ret_obj;
239 	u64 req, res;
240 	struct tpm_chip *chip = to_tpm_chip(dev);
241 
242 	obj = tpm_eval_dsm(chip->acpi_dev_handle, TPM_PPI_FN_GETRSP,
243 			   ACPI_TYPE_PACKAGE, NULL, TPM_PPI_REVISION_ID_1);
244 	if (!obj)
245 		return -ENXIO;
246 
247 	/*
248 	 * parameter output.pointer should be of package type, including
249 	 * 3 integers. The first means function return code, the second means
250 	 * most recent TPM operation request, and the last means response to
251 	 * the most recent TPM operation request. Only if the first is 0, and
252 	 * the second integer is not 0, the response makes sense.
253 	 */
254 	ret_obj = obj->package.elements;
255 	if (obj->package.count < 3 ||
256 	    ret_obj[0].type != ACPI_TYPE_INTEGER ||
257 	    ret_obj[1].type != ACPI_TYPE_INTEGER ||
258 	    ret_obj[2].type != ACPI_TYPE_INTEGER)
259 		goto cleanup;
260 
261 	if (ret_obj[0].integer.value) {
262 		status = -EFAULT;
263 		goto cleanup;
264 	}
265 
266 	req = ret_obj[1].integer.value;
267 	res = ret_obj[2].integer.value;
268 	if (req) {
269 		if (res == 0)
270 			status = sysfs_emit(buf, "%llu %s\n", req,
271 					    "0: Success");
272 		else if (res == 0xFFFFFFF0)
273 			status = sysfs_emit(buf, "%llu %s\n", req,
274 					    "0xFFFFFFF0: User Abort");
275 		else if (res == 0xFFFFFFF1)
276 			status = sysfs_emit(buf, "%llu %s\n", req,
277 					    "0xFFFFFFF1: BIOS Failure");
278 		else if (res >= 1 && res <= 0x00000FFF)
279 			status = sysfs_emit(buf, "%llu %llu: %s\n",
280 					    req, res, "Corresponding TPM error");
281 		else
282 			status = sysfs_emit(buf, "%llu %llu: %s\n",
283 					    req, res, "Error");
284 	} else {
285 		status = sysfs_emit(buf, "%llu: %s\n",
286 				    req, "No Recent Request");
287 	}
288 
289 cleanup:
290 	ACPI_FREE(obj);
291 	return status;
292 }
293 
cache_ppi_operations(acpi_handle dev_handle,char * buf)294 static ssize_t cache_ppi_operations(acpi_handle dev_handle, char *buf)
295 {
296 	int i;
297 	u32 ret;
298 	int len = 0;
299 	union acpi_object *obj, tmp;
300 	union acpi_object argv = ACPI_INIT_DSM_ARGV4(1, &tmp);
301 
302 	if (!acpi_check_dsm(dev_handle, &tpm_ppi_guid, TPM_PPI_REVISION_ID_1,
303 			    1 << TPM_PPI_FN_GETOPR))
304 		return -EPERM;
305 
306 	tmp.integer.type = ACPI_TYPE_INTEGER;
307 	for (i = 0; i <= PPI_VS_REQ_END; i++) {
308 		tmp.integer.value = i;
309 		obj = tpm_eval_dsm(dev_handle, TPM_PPI_FN_GETOPR,
310 				   ACPI_TYPE_INTEGER, &argv,
311 				   TPM_PPI_REVISION_ID_1);
312 		if (!obj)
313 			return -ENOMEM;
314 
315 		ret = obj->integer.value;
316 		ppi_operations_cache[i] = ret;
317 		ACPI_FREE(obj);
318 	}
319 
320 	return len;
321 }
322 
tpm_show_ppi_tcg_operations(struct device * dev,struct device_attribute * attr,char * buf)323 static ssize_t tpm_show_ppi_tcg_operations(struct device *dev,
324 					   struct device_attribute *attr,
325 					   char *buf)
326 {
327 	struct tpm_chip *chip = to_tpm_chip(dev);
328 	ssize_t len = 0;
329 	u32 ret;
330 	int i;
331 
332 	mutex_lock(&tpm_ppi_lock);
333 	if (!ppi_cache_populated) {
334 		len = cache_ppi_operations(chip->acpi_dev_handle, buf);
335 		if (len < 0) {
336 			mutex_unlock(&tpm_ppi_lock);
337 			return len;
338 		}
339 
340 		ppi_cache_populated = true;
341 	}
342 
343 	for (i = 0; i <= PPI_TPM_REQ_MAX; i++) {
344 		ret = ppi_operations_cache[i];
345 		if (ret >= 0 && ret < ARRAY_SIZE(tpm_ppi_info))
346 			len += sysfs_emit_at(buf, len, "%d %d: %s\n",
347 							i, ret, tpm_ppi_info[ret]);
348 	}
349 	mutex_unlock(&tpm_ppi_lock);
350 
351 	return len;
352 }
353 
tpm_show_ppi_vs_operations(struct device * dev,struct device_attribute * attr,char * buf)354 static ssize_t tpm_show_ppi_vs_operations(struct device *dev,
355 					  struct device_attribute *attr,
356 					  char *buf)
357 {
358 	struct tpm_chip *chip = to_tpm_chip(dev);
359 	ssize_t len = 0;
360 	u32 ret;
361 	int i;
362 
363 	mutex_lock(&tpm_ppi_lock);
364 	if (!ppi_cache_populated) {
365 		len = cache_ppi_operations(chip->acpi_dev_handle, buf);
366 		if (len < 0) {
367 			mutex_unlock(&tpm_ppi_lock);
368 			return len;
369 		}
370 
371 		ppi_cache_populated = true;
372 	}
373 
374 	for (i = PPI_VS_REQ_START; i <= PPI_VS_REQ_END; i++) {
375 		ret = ppi_operations_cache[i];
376 		if (ret >= 0 && ret < ARRAY_SIZE(tpm_ppi_info))
377 			len += sysfs_emit_at(buf, len, "%d %d: %s\n",
378 							i, ret, tpm_ppi_info[ret]);
379 	}
380 	mutex_unlock(&tpm_ppi_lock);
381 
382 	return len;
383 }
384 
385 static DEVICE_ATTR(version, S_IRUGO, tpm_show_ppi_version, NULL);
386 static DEVICE_ATTR(request, S_IRUGO | S_IWUSR | S_IWGRP,
387 		   tpm_show_ppi_request, tpm_store_ppi_request);
388 static DEVICE_ATTR(transition_action, S_IRUGO,
389 		   tpm_show_ppi_transition_action, NULL);
390 static DEVICE_ATTR(response, S_IRUGO, tpm_show_ppi_response, NULL);
391 static DEVICE_ATTR(tcg_operations, S_IRUGO, tpm_show_ppi_tcg_operations, NULL);
392 static DEVICE_ATTR(vs_operations, S_IRUGO, tpm_show_ppi_vs_operations, NULL);
393 
394 static struct attribute *ppi_attrs[] = {
395 	&dev_attr_version.attr,
396 	&dev_attr_request.attr,
397 	&dev_attr_transition_action.attr,
398 	&dev_attr_response.attr,
399 	&dev_attr_tcg_operations.attr,
400 	&dev_attr_vs_operations.attr, NULL,
401 };
402 static const struct attribute_group ppi_attr_grp = {
403 	.name = "ppi",
404 	.attrs = ppi_attrs
405 };
406 
tpm_add_ppi(struct tpm_chip * chip)407 void tpm_add_ppi(struct tpm_chip *chip)
408 {
409 	union acpi_object *obj;
410 
411 	if (!chip->acpi_dev_handle)
412 		return;
413 
414 	if (!acpi_check_dsm(chip->acpi_dev_handle, &tpm_ppi_guid,
415 			    TPM_PPI_REVISION_ID_1, 1 << TPM_PPI_FN_VERSION))
416 		return;
417 
418 	/* Cache PPI version string. */
419 	obj = acpi_evaluate_dsm_typed(chip->acpi_dev_handle, &tpm_ppi_guid,
420 				      TPM_PPI_REVISION_ID_1,
421 				      TPM_PPI_FN_VERSION,
422 				      NULL, ACPI_TYPE_STRING);
423 	if (obj) {
424 		strscpy(chip->ppi_version, obj->string.pointer,
425 			sizeof(chip->ppi_version));
426 		ACPI_FREE(obj);
427 	}
428 
429 	chip->groups[chip->groups_cnt++] = &ppi_attr_grp;
430 }
431