tpm_ppi.c 11 KB

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