tpm_ppi.c 11 KB

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