tpm_ppi.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #include <linux/acpi.h>
  2. #include "tpm.h"
  3. #define TPM_PPI_REVISION_ID 1
  4. #define TPM_PPI_FN_VERSION 1
  5. #define TPM_PPI_FN_SUBREQ 2
  6. #define TPM_PPI_FN_GETREQ 3
  7. #define TPM_PPI_FN_GETACT 4
  8. #define TPM_PPI_FN_GETRSP 5
  9. #define TPM_PPI_FN_SUBREQ2 7
  10. #define TPM_PPI_FN_GETOPR 8
  11. #define PPI_TPM_REQ_MAX 22
  12. #define PPI_VS_REQ_START 128
  13. #define PPI_VS_REQ_END 255
  14. #define PPI_VERSION_LEN 3
  15. static const u8 tpm_ppi_uuid[] = {
  16. 0xA6, 0xFA, 0xDD, 0x3D,
  17. 0x1B, 0x36,
  18. 0xB4, 0x4E,
  19. 0xA4, 0x24,
  20. 0x8D, 0x10, 0x08, 0x9D, 0x16, 0x53
  21. };
  22. static char tpm_ppi_version[PPI_VERSION_LEN + 1];
  23. static acpi_handle tpm_ppi_handle;
  24. static acpi_status ppi_callback(acpi_handle handle, u32 level, void *context,
  25. void **return_value)
  26. {
  27. union acpi_object *obj;
  28. if (!acpi_check_dsm(handle, tpm_ppi_uuid, TPM_PPI_REVISION_ID,
  29. 1 << TPM_PPI_FN_VERSION))
  30. return AE_OK;
  31. /* Cache version string */
  32. obj = acpi_evaluate_dsm_typed(handle, tpm_ppi_uuid,
  33. TPM_PPI_REVISION_ID, TPM_PPI_FN_VERSION,
  34. NULL, ACPI_TYPE_STRING);
  35. if (obj) {
  36. strlcpy(tpm_ppi_version, obj->string.pointer,
  37. PPI_VERSION_LEN + 1);
  38. ACPI_FREE(obj);
  39. }
  40. *return_value = handle;
  41. return AE_CTRL_TERMINATE;
  42. }
  43. static inline union acpi_object *
  44. tpm_eval_dsm(int func, acpi_object_type type, union acpi_object *argv4)
  45. {
  46. BUG_ON(!tpm_ppi_handle);
  47. return acpi_evaluate_dsm_typed(tpm_ppi_handle, tpm_ppi_uuid,
  48. TPM_PPI_REVISION_ID, func, argv4, type);
  49. }
  50. static ssize_t tpm_show_ppi_version(struct device *dev,
  51. struct device_attribute *attr, char *buf)
  52. {
  53. return scnprintf(buf, PAGE_SIZE, "%s\n", tpm_ppi_version);
  54. }
  55. static ssize_t tpm_show_ppi_request(struct device *dev,
  56. struct device_attribute *attr, char *buf)
  57. {
  58. ssize_t size = -EINVAL;
  59. union acpi_object *obj;
  60. obj = tpm_eval_dsm(TPM_PPI_FN_GETREQ, ACPI_TYPE_PACKAGE, NULL);
  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 == 2 &&
  70. obj->package.elements[0].type == ACPI_TYPE_INTEGER &&
  71. obj->package.elements[1].type == ACPI_TYPE_INTEGER) {
  72. if (obj->package.elements[0].integer.value)
  73. size = -EFAULT;
  74. else
  75. size = scnprintf(buf, PAGE_SIZE, "%llu\n",
  76. obj->package.elements[1].integer.value);
  77. }
  78. ACPI_FREE(obj);
  79. return size;
  80. }
  81. static ssize_t tpm_store_ppi_request(struct device *dev,
  82. struct device_attribute *attr,
  83. const char *buf, size_t count)
  84. {
  85. u32 req;
  86. u64 ret;
  87. int func = TPM_PPI_FN_SUBREQ;
  88. union acpi_object *obj, tmp;
  89. union acpi_object argv4 = ACPI_INIT_DSM_ARGV4(1, &tmp);
  90. /*
  91. * the function to submit TPM operation request to pre-os environment
  92. * is updated with function index from SUBREQ to SUBREQ2 since PPI
  93. * version 1.1
  94. */
  95. if (acpi_check_dsm(tpm_ppi_handle, tpm_ppi_uuid, TPM_PPI_REVISION_ID,
  96. 1 << TPM_PPI_FN_SUBREQ2))
  97. func = TPM_PPI_FN_SUBREQ2;
  98. /*
  99. * PPI spec defines params[3].type as ACPI_TYPE_PACKAGE. Some BIOS
  100. * accept buffer/string/integer type, but some BIOS accept buffer/
  101. * string/package type. For PPI version 1.0 and 1.1, use buffer type
  102. * for compatibility, and use package type since 1.2 according to spec.
  103. */
  104. if (strcmp(tpm_ppi_version, "1.2") < 0) {
  105. if (sscanf(buf, "%d", &req) != 1)
  106. return -EINVAL;
  107. argv4.type = ACPI_TYPE_BUFFER;
  108. argv4.buffer.length = sizeof(req);
  109. argv4.buffer.pointer = (u8 *)&req;
  110. } else {
  111. tmp.type = ACPI_TYPE_INTEGER;
  112. if (sscanf(buf, "%llu", &tmp.integer.value) != 1)
  113. return -EINVAL;
  114. }
  115. obj = tpm_eval_dsm(func, ACPI_TYPE_INTEGER, &argv4);
  116. if (!obj) {
  117. return -ENXIO;
  118. } else {
  119. ret = obj->integer.value;
  120. ACPI_FREE(obj);
  121. }
  122. if (ret == 0)
  123. return (acpi_status)count;
  124. return (ret == 1) ? -EPERM : -EFAULT;
  125. }
  126. static ssize_t tpm_show_ppi_transition_action(struct device *dev,
  127. struct device_attribute *attr,
  128. char *buf)
  129. {
  130. u32 ret;
  131. acpi_status status;
  132. union acpi_object *obj = NULL;
  133. union acpi_object tmp = {
  134. .buffer.type = ACPI_TYPE_BUFFER,
  135. .buffer.length = 0,
  136. .buffer.pointer = NULL
  137. };
  138. static char *info[] = {
  139. "None",
  140. "Shutdown",
  141. "Reboot",
  142. "OS Vendor-specific",
  143. "Error",
  144. };
  145. /*
  146. * PPI spec defines params[3].type as empty package, but some platforms
  147. * (e.g. Capella with PPI 1.0) need integer/string/buffer type, so for
  148. * compatibility, define params[3].type as buffer, if PPI version < 1.2
  149. */
  150. if (strcmp(tpm_ppi_version, "1.2") < 0)
  151. obj = &tmp;
  152. obj = tpm_eval_dsm(TPM_PPI_FN_GETACT, ACPI_TYPE_INTEGER, obj);
  153. if (!obj) {
  154. return -ENXIO;
  155. } else {
  156. ret = obj->integer.value;
  157. ACPI_FREE(obj);
  158. }
  159. if (ret < ARRAY_SIZE(info) - 1)
  160. status = scnprintf(buf, PAGE_SIZE, "%d: %s\n", ret, info[ret]);
  161. else
  162. status = scnprintf(buf, PAGE_SIZE, "%d: %s\n", ret,
  163. info[ARRAY_SIZE(info)-1]);
  164. return status;
  165. }
  166. static ssize_t tpm_show_ppi_response(struct device *dev,
  167. struct device_attribute *attr,
  168. char *buf)
  169. {
  170. acpi_status status = -EINVAL;
  171. union acpi_object *obj, *ret_obj;
  172. u64 req, res;
  173. obj = tpm_eval_dsm(TPM_PPI_FN_GETRSP, ACPI_TYPE_PACKAGE, NULL);
  174. if (!obj)
  175. return -ENXIO;
  176. /*
  177. * parameter output.pointer should be of package type, including
  178. * 3 integers. The first means function return code, the second means
  179. * most recent TPM operation request, and the last means response to
  180. * the most recent TPM operation request. Only if the first is 0, and
  181. * the second integer is not 0, the response makes sense.
  182. */
  183. ret_obj = obj->package.elements;
  184. if (obj->package.count < 3 ||
  185. ret_obj[0].type != ACPI_TYPE_INTEGER ||
  186. ret_obj[1].type != ACPI_TYPE_INTEGER ||
  187. ret_obj[2].type != ACPI_TYPE_INTEGER)
  188. goto cleanup;
  189. if (ret_obj[0].integer.value) {
  190. status = -EFAULT;
  191. goto cleanup;
  192. }
  193. req = ret_obj[1].integer.value;
  194. res = ret_obj[2].integer.value;
  195. if (req) {
  196. if (res == 0)
  197. status = scnprintf(buf, PAGE_SIZE, "%llu %s\n", req,
  198. "0: Success");
  199. else if (res == 0xFFFFFFF0)
  200. status = scnprintf(buf, PAGE_SIZE, "%llu %s\n", req,
  201. "0xFFFFFFF0: User Abort");
  202. else if (res == 0xFFFFFFF1)
  203. status = scnprintf(buf, PAGE_SIZE, "%llu %s\n", req,
  204. "0xFFFFFFF1: BIOS Failure");
  205. else if (res >= 1 && res <= 0x00000FFF)
  206. status = scnprintf(buf, PAGE_SIZE, "%llu %llu: %s\n",
  207. req, res, "Corresponding TPM error");
  208. else
  209. status = scnprintf(buf, PAGE_SIZE, "%llu %llu: %s\n",
  210. req, res, "Error");
  211. } else {
  212. status = scnprintf(buf, PAGE_SIZE, "%llu: %s\n",
  213. req, "No Recent Request");
  214. }
  215. cleanup:
  216. ACPI_FREE(obj);
  217. return status;
  218. }
  219. static ssize_t show_ppi_operations(char *buf, u32 start, u32 end)
  220. {
  221. int i;
  222. u32 ret;
  223. char *str = buf;
  224. union acpi_object *obj, tmp;
  225. union acpi_object argv = ACPI_INIT_DSM_ARGV4(1, &tmp);
  226. static char *info[] = {
  227. "Not implemented",
  228. "BIOS only",
  229. "Blocked for OS by BIOS",
  230. "User required",
  231. "User not required",
  232. };
  233. if (!acpi_check_dsm(tpm_ppi_handle, tpm_ppi_uuid, TPM_PPI_REVISION_ID,
  234. 1 << TPM_PPI_FN_GETOPR))
  235. return -EPERM;
  236. tmp.integer.type = ACPI_TYPE_INTEGER;
  237. for (i = start; i <= end; i++) {
  238. tmp.integer.value = i;
  239. obj = tpm_eval_dsm(TPM_PPI_FN_GETOPR, ACPI_TYPE_INTEGER, &argv);
  240. if (!obj) {
  241. return -ENOMEM;
  242. } else {
  243. ret = obj->integer.value;
  244. ACPI_FREE(obj);
  245. }
  246. if (ret > 0 && ret < ARRAY_SIZE(info))
  247. str += scnprintf(str, PAGE_SIZE, "%d %d: %s\n",
  248. i, ret, info[ret]);
  249. }
  250. return str - buf;
  251. }
  252. static ssize_t tpm_show_ppi_tcg_operations(struct device *dev,
  253. struct device_attribute *attr,
  254. char *buf)
  255. {
  256. return show_ppi_operations(buf, 0, PPI_TPM_REQ_MAX);
  257. }
  258. static ssize_t tpm_show_ppi_vs_operations(struct device *dev,
  259. struct device_attribute *attr,
  260. char *buf)
  261. {
  262. return show_ppi_operations(buf, PPI_VS_REQ_START, PPI_VS_REQ_END);
  263. }
  264. static DEVICE_ATTR(version, S_IRUGO, tpm_show_ppi_version, NULL);
  265. static DEVICE_ATTR(request, S_IRUGO | S_IWUSR | S_IWGRP,
  266. tpm_show_ppi_request, tpm_store_ppi_request);
  267. static DEVICE_ATTR(transition_action, S_IRUGO,
  268. tpm_show_ppi_transition_action, NULL);
  269. static DEVICE_ATTR(response, S_IRUGO, tpm_show_ppi_response, NULL);
  270. static DEVICE_ATTR(tcg_operations, S_IRUGO, tpm_show_ppi_tcg_operations, NULL);
  271. static DEVICE_ATTR(vs_operations, S_IRUGO, tpm_show_ppi_vs_operations, NULL);
  272. static struct attribute *ppi_attrs[] = {
  273. &dev_attr_version.attr,
  274. &dev_attr_request.attr,
  275. &dev_attr_transition_action.attr,
  276. &dev_attr_response.attr,
  277. &dev_attr_tcg_operations.attr,
  278. &dev_attr_vs_operations.attr, NULL,
  279. };
  280. static struct attribute_group ppi_attr_grp = {
  281. .name = "ppi",
  282. .attrs = ppi_attrs
  283. };
  284. int tpm_add_ppi(struct kobject *parent)
  285. {
  286. /* Cache TPM ACPI handle and version string */
  287. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
  288. ppi_callback, NULL, NULL, &tpm_ppi_handle);
  289. return tpm_ppi_handle ? sysfs_create_group(parent, &ppi_attr_grp) : 0;
  290. }
  291. void tpm_remove_ppi(struct kobject *parent)
  292. {
  293. if (tpm_ppi_handle)
  294. sysfs_remove_group(parent, &ppi_attr_grp);
  295. }