tpm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* Support for generating ACPI TPM tables
  2. *
  3. * Copyright (C) 2018 IBM, Corp.
  4. * Copyright (C) 2018 Red Hat Inc
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "qemu/osdep.h"
  18. #include "qapi/error.h"
  19. #include "hw/acpi/tpm.h"
  20. void tpm_build_ppi_acpi(TPMIf *tpm, Aml *dev)
  21. {
  22. Aml *method, *field, *ifctx, *ifctx2, *ifctx3, *func_mask,
  23. *not_implemented, *pak, *tpm2, *tpm3, *pprm, *pprq, *zero, *one;
  24. if (!object_property_get_bool(OBJECT(tpm), "ppi", &error_abort)) {
  25. return;
  26. }
  27. zero = aml_int(0);
  28. one = aml_int(1);
  29. func_mask = aml_int(TPM_PPI_FUNC_MASK);
  30. not_implemented = aml_int(TPM_PPI_FUNC_NOT_IMPLEMENTED);
  31. /*
  32. * TPP2 is for the registers that ACPI code used to pass
  33. * the PPI code and parameter (PPRQ, PPRM) to the firmware.
  34. */
  35. aml_append(dev,
  36. aml_operation_region("TPP2", AML_SYSTEM_MEMORY,
  37. aml_int(TPM_PPI_ADDR_BASE + 0x100),
  38. 0x5A));
  39. field = aml_field("TPP2", AML_ANY_ACC, AML_NOLOCK, AML_PRESERVE);
  40. aml_append(field, aml_named_field("PPIN", 8));
  41. aml_append(field, aml_named_field("PPIP", 32));
  42. aml_append(field, aml_named_field("PPRP", 32));
  43. aml_append(field, aml_named_field("PPRQ", 32));
  44. aml_append(field, aml_named_field("PPRM", 32));
  45. aml_append(field, aml_named_field("LPPR", 32));
  46. aml_append(dev, field);
  47. pprq = aml_name("PPRQ");
  48. pprm = aml_name("PPRM");
  49. aml_append(dev,
  50. aml_operation_region(
  51. "TPP3", AML_SYSTEM_MEMORY,
  52. aml_int(TPM_PPI_ADDR_BASE +
  53. 0x15a /* movv, docs/specs/tpm.rst */),
  54. 0x1));
  55. field = aml_field("TPP3", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
  56. aml_append(field, aml_named_field("MOVV", 8));
  57. aml_append(dev, field);
  58. /*
  59. * DerefOf in Windows is broken with SYSTEM_MEMORY. Use a dynamic
  60. * operation region inside of a method for getting FUNC[op].
  61. */
  62. method = aml_method("TPFN", 1, AML_SERIALIZED);
  63. {
  64. Aml *op = aml_arg(0);
  65. ifctx = aml_if(aml_lgreater_equal(op, aml_int(0x100)));
  66. {
  67. aml_append(ifctx, aml_return(zero));
  68. }
  69. aml_append(method, ifctx);
  70. aml_append(method,
  71. aml_operation_region("TPP1", AML_SYSTEM_MEMORY,
  72. aml_add(aml_int(TPM_PPI_ADDR_BASE), op, NULL), 0x1));
  73. field = aml_field("TPP1", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
  74. aml_append(field, aml_named_field("TPPF", 8));
  75. aml_append(method, field);
  76. aml_append(method, aml_return(aml_name("TPPF")));
  77. }
  78. aml_append(dev, method);
  79. /*
  80. * Use global TPM2 & TPM3 variables to workaround Windows ACPI bug
  81. * when returning packages.
  82. */
  83. pak = aml_package(2);
  84. aml_append(pak, zero);
  85. aml_append(pak, zero);
  86. aml_append(dev, aml_name_decl("TPM2", pak));
  87. tpm2 = aml_name("TPM2");
  88. pak = aml_package(3);
  89. aml_append(pak, zero);
  90. aml_append(pak, zero);
  91. aml_append(pak, zero);
  92. aml_append(dev, aml_name_decl("TPM3", pak));
  93. tpm3 = aml_name("TPM3");
  94. method = aml_method("_DSM", 4, AML_SERIALIZED);
  95. {
  96. uint8_t zerobyte[1] = { 0 };
  97. Aml *function, *arguments, *rev, *op, *op_arg, *op_flags, *uuid;
  98. uuid = aml_arg(0);
  99. rev = aml_arg(1);
  100. function = aml_arg(2);
  101. arguments = aml_arg(3);
  102. op = aml_local(0);
  103. op_flags = aml_local(1);
  104. /* Physical Presence Interface */
  105. ifctx = aml_if(
  106. aml_equal(uuid,
  107. aml_touuid("3DDDFAA6-361B-4EB4-A424-8D10089D1653")));
  108. {
  109. /* standard DSM query function */
  110. ifctx2 = aml_if(aml_equal(function, zero));
  111. {
  112. uint8_t byte_list[2] = { 0xff, 0x01 }; /* functions 1-8 */
  113. aml_append(ifctx2,
  114. aml_return(aml_buffer(sizeof(byte_list),
  115. byte_list)));
  116. }
  117. aml_append(ifctx, ifctx2);
  118. /*
  119. * PPI 1.0: 2.1.1 Get Physical Presence Interface Version
  120. *
  121. * Arg 2 (Integer): Function Index = 1
  122. * Arg 3 (Package): Arguments = Empty Package
  123. * Returns: Type: String
  124. */
  125. ifctx2 = aml_if(aml_equal(function, one));
  126. {
  127. aml_append(ifctx2, aml_return(aml_string("1.3")));
  128. }
  129. aml_append(ifctx, ifctx2);
  130. /*
  131. * PPI 1.0: 2.1.3 Submit TPM Operation Request to Pre-OS Environment
  132. *
  133. * Arg 2 (Integer): Function Index = 2
  134. * Arg 3 (Package): Arguments = Package: Type: Integer
  135. * Operation Value of the Request
  136. * Returns: Type: Integer
  137. * 0: Success
  138. * 1: Operation Value of the Request Not Supported
  139. * 2: General Failure
  140. */
  141. ifctx2 = aml_if(aml_equal(function, aml_int(2)));
  142. {
  143. /* get opcode */
  144. aml_append(ifctx2,
  145. aml_store(aml_derefof(aml_index(arguments,
  146. zero)), op));
  147. /* get opcode flags */
  148. aml_append(ifctx2,
  149. aml_store(aml_call1("TPFN", op), op_flags));
  150. /* if func[opcode] & TPM_PPI_FUNC_NOT_IMPLEMENTED */
  151. ifctx3 = aml_if(
  152. aml_equal(
  153. aml_and(op_flags, func_mask, NULL),
  154. not_implemented));
  155. {
  156. /* 1: Operation Value of the Request Not Supported */
  157. aml_append(ifctx3, aml_return(one));
  158. }
  159. aml_append(ifctx2, ifctx3);
  160. aml_append(ifctx2, aml_store(op, pprq));
  161. aml_append(ifctx2, aml_store(zero, pprm));
  162. /* 0: success */
  163. aml_append(ifctx2, aml_return(zero));
  164. }
  165. aml_append(ifctx, ifctx2);
  166. /*
  167. * PPI 1.0: 2.1.4 Get Pending TPM Operation Requested By the OS
  168. *
  169. * Arg 2 (Integer): Function Index = 3
  170. * Arg 3 (Package): Arguments = Empty Package
  171. * Returns: Type: Package of Integers
  172. * Integer 1: Function Return code
  173. * 0: Success
  174. * 1: General Failure
  175. * Integer 2: Pending operation requested by the OS
  176. * 0: None
  177. * >0: Operation Value of the Pending Request
  178. * Integer 3: Optional argument to pending operation
  179. * requested by the OS
  180. * 0: None
  181. * >0: Argument Value of the Pending Request
  182. */
  183. ifctx2 = aml_if(aml_equal(function, aml_int(3)));
  184. {
  185. /*
  186. * Revision ID of 1, no integer parameter beyond
  187. * parameter two are expected
  188. */
  189. ifctx3 = aml_if(aml_equal(rev, one));
  190. {
  191. /* TPM2[1] = PPRQ */
  192. aml_append(ifctx3,
  193. aml_store(pprq, aml_index(tpm2, one)));
  194. aml_append(ifctx3, aml_return(tpm2));
  195. }
  196. aml_append(ifctx2, ifctx3);
  197. /*
  198. * A return value of {0, 23, 1} indicates that
  199. * operation 23 with argument 1 is pending.
  200. */
  201. ifctx3 = aml_if(aml_equal(rev, aml_int(2)));
  202. {
  203. /* TPM3[1] = PPRQ */
  204. aml_append(ifctx3,
  205. aml_store(pprq, aml_index(tpm3, one)));
  206. /* TPM3[2] = PPRM */
  207. aml_append(ifctx3,
  208. aml_store(pprm, aml_index(tpm3, aml_int(2))));
  209. aml_append(ifctx3, aml_return(tpm3));
  210. }
  211. aml_append(ifctx2, ifctx3);
  212. }
  213. aml_append(ifctx, ifctx2);
  214. /*
  215. * PPI 1.0: 2.1.5 Get Platform-Specific Action to Transition to
  216. * Pre-OS Environment
  217. *
  218. * Arg 2 (Integer): Function Index = 4
  219. * Arg 3 (Package): Arguments = Empty Package
  220. * Returns: Type: Integer
  221. * 0: None
  222. * 1: Shutdown
  223. * 2: Reboot
  224. * 3: OS Vendor-specific
  225. */
  226. ifctx2 = aml_if(aml_equal(function, aml_int(4)));
  227. {
  228. /* reboot */
  229. aml_append(ifctx2, aml_return(aml_int(2)));
  230. }
  231. aml_append(ifctx, ifctx2);
  232. /*
  233. * PPI 1.0: 2.1.6 Return TPM Operation Response to OS Environment
  234. *
  235. * Arg 2 (Integer): Function Index = 5
  236. * Arg 3 (Package): Arguments = Empty Package
  237. * Returns: Type: Package of Integer
  238. * Integer 1: Function Return code
  239. * 0: Success
  240. * 1: General Failure
  241. * Integer 2: Most recent operation request
  242. * 0: None
  243. * >0: Operation Value of the most recent request
  244. * Integer 3: Response to the most recent operation request
  245. * 0: Success
  246. * 0x00000001..0x00000FFF: Corresponding TPM
  247. * error code
  248. * 0xFFFFFFF0: User Abort or timeout of dialog
  249. * 0xFFFFFFF1: firmware Failure
  250. */
  251. ifctx2 = aml_if(aml_equal(function, aml_int(5)));
  252. {
  253. /* TPM3[1] = LPPR */
  254. aml_append(ifctx2,
  255. aml_store(aml_name("LPPR"),
  256. aml_index(tpm3, one)));
  257. /* TPM3[2] = PPRP */
  258. aml_append(ifctx2,
  259. aml_store(aml_name("PPRP"),
  260. aml_index(tpm3, aml_int(2))));
  261. aml_append(ifctx2, aml_return(tpm3));
  262. }
  263. aml_append(ifctx, ifctx2);
  264. /*
  265. * PPI 1.0: 2.1.7 Submit preferred user language
  266. *
  267. * Arg 2 (Integer): Function Index = 6
  268. * Arg 3 (Package): Arguments = String Package
  269. * Preferred language code
  270. * Returns: Type: Integer
  271. * Function Return Code
  272. * 3: Not implemented
  273. */
  274. ifctx2 = aml_if(aml_equal(function, aml_int(6)));
  275. {
  276. /* 3 = not implemented */
  277. aml_append(ifctx2, aml_return(aml_int(3)));
  278. }
  279. aml_append(ifctx, ifctx2);
  280. /*
  281. * PPI 1.1: 2.1.7 Submit TPM Operation Request to
  282. * Pre-OS Environment 2
  283. *
  284. * Arg 2 (Integer): Function Index = 7
  285. * Arg 3 (Package): Arguments = Package: Type: Integer
  286. * Integer 1: Operation Value of the Request
  287. * Integer 2: Argument for Operation (optional)
  288. * Returns: Type: Integer
  289. * 0: Success
  290. * 1: Not Implemented
  291. * 2: General Failure
  292. * 3: Operation blocked by current firmware settings
  293. */
  294. ifctx2 = aml_if(aml_equal(function, aml_int(7)));
  295. {
  296. /* get opcode */
  297. aml_append(ifctx2, aml_store(aml_derefof(aml_index(arguments,
  298. zero)),
  299. op));
  300. /* get opcode flags */
  301. aml_append(ifctx2, aml_store(aml_call1("TPFN", op),
  302. op_flags));
  303. /* if func[opcode] & TPM_PPI_FUNC_NOT_IMPLEMENTED */
  304. ifctx3 = aml_if(
  305. aml_equal(
  306. aml_and(op_flags, func_mask, NULL),
  307. not_implemented));
  308. {
  309. /* 1: not implemented */
  310. aml_append(ifctx3, aml_return(one));
  311. }
  312. aml_append(ifctx2, ifctx3);
  313. /* if func[opcode] & TPM_PPI_FUNC_BLOCKED */
  314. ifctx3 = aml_if(
  315. aml_equal(
  316. aml_and(op_flags, func_mask, NULL),
  317. aml_int(TPM_PPI_FUNC_BLOCKED)));
  318. {
  319. /* 3: blocked by firmware */
  320. aml_append(ifctx3, aml_return(aml_int(3)));
  321. }
  322. aml_append(ifctx2, ifctx3);
  323. /* revision to integer */
  324. ifctx3 = aml_if(aml_equal(rev, one));
  325. {
  326. /* revision 1 */
  327. /* PPRQ = op */
  328. aml_append(ifctx3, aml_store(op, pprq));
  329. /* no argument, PPRM = 0 */
  330. aml_append(ifctx3, aml_store(zero, pprm));
  331. }
  332. aml_append(ifctx2, ifctx3);
  333. ifctx3 = aml_if(aml_equal(rev, aml_int(2)));
  334. {
  335. /* revision 2 */
  336. /* PPRQ = op */
  337. op_arg = aml_derefof(aml_index(arguments, one));
  338. aml_append(ifctx3, aml_store(op, pprq));
  339. /* PPRM = arg3[1] */
  340. aml_append(ifctx3, aml_store(op_arg, pprm));
  341. }
  342. aml_append(ifctx2, ifctx3);
  343. /* 0: success */
  344. aml_append(ifctx2, aml_return(zero));
  345. }
  346. aml_append(ifctx, ifctx2);
  347. /*
  348. * PPI 1.1: 2.1.8 Get User Confirmation Status for Operation
  349. *
  350. * Arg 2 (Integer): Function Index = 8
  351. * Arg 3 (Package): Arguments = Package: Type: Integer
  352. * Operation Value that may need user confirmation
  353. * Returns: Type: Integer
  354. * 0: Not implemented
  355. * 1: Firmware only
  356. * 2: Blocked for OS by firmware configuration
  357. * 3: Allowed and physically present user required
  358. * 4: Allowed and physically present user not required
  359. */
  360. ifctx2 = aml_if(aml_equal(function, aml_int(8)));
  361. {
  362. /* get opcode */
  363. aml_append(ifctx2,
  364. aml_store(aml_derefof(aml_index(arguments,
  365. zero)),
  366. op));
  367. /* get opcode flags */
  368. aml_append(ifctx2, aml_store(aml_call1("TPFN", op),
  369. op_flags));
  370. /* return confirmation status code */
  371. aml_append(ifctx2,
  372. aml_return(
  373. aml_and(op_flags, func_mask, NULL)));
  374. }
  375. aml_append(ifctx, ifctx2);
  376. aml_append(ifctx, aml_return(aml_buffer(1, zerobyte)));
  377. }
  378. aml_append(method, ifctx);
  379. /*
  380. * "TCG Platform Reset Attack Mitigation Specification 1.00",
  381. * Chapter 6 "ACPI _DSM Function"
  382. */
  383. ifctx = aml_if(
  384. aml_equal(uuid,
  385. aml_touuid("376054ED-CC13-4675-901C-4756D7F2D45D")));
  386. {
  387. /* standard DSM query function */
  388. ifctx2 = aml_if(aml_equal(function, zero));
  389. {
  390. uint8_t byte_list[1] = { 0x03 }; /* functions 1-2 supported */
  391. aml_append(ifctx2,
  392. aml_return(aml_buffer(sizeof(byte_list),
  393. byte_list)));
  394. }
  395. aml_append(ifctx, ifctx2);
  396. /*
  397. * TCG Platform Reset Attack Mitigation Specification 1.0 Ch.6
  398. *
  399. * Arg 2 (Integer): Function Index = 1
  400. * Arg 3 (Package): Arguments = Package: Type: Integer
  401. * Operation Value of the Request
  402. * Returns: Type: Integer
  403. * 0: Success
  404. * 1: General Failure
  405. */
  406. ifctx2 = aml_if(aml_equal(function, one));
  407. {
  408. aml_append(ifctx2,
  409. aml_store(aml_derefof(aml_index(arguments, zero)),
  410. op));
  411. {
  412. aml_append(ifctx2, aml_store(op, aml_name("MOVV")));
  413. /* 0: success */
  414. aml_append(ifctx2, aml_return(zero));
  415. }
  416. }
  417. aml_append(ifctx, ifctx2);
  418. }
  419. aml_append(method, ifctx);
  420. }
  421. aml_append(dev, method);
  422. }