tpm_backend.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * QEMU TPM Backend
  3. *
  4. * Copyright IBM, Corp. 2013
  5. *
  6. * Authors:
  7. * Stefan Berger <stefanb@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #ifndef TPM_BACKEND_H
  13. #define TPM_BACKEND_H
  14. #include "qom/object.h"
  15. #include "qemu/option.h"
  16. #include "system/tpm.h"
  17. #include "qapi/error.h"
  18. #ifdef CONFIG_TPM
  19. #define TYPE_TPM_BACKEND "tpm-backend"
  20. OBJECT_DECLARE_TYPE(TPMBackend, TPMBackendClass,
  21. TPM_BACKEND)
  22. typedef struct TPMBackendCmd {
  23. uint8_t locty;
  24. const uint8_t *in;
  25. uint32_t in_len;
  26. uint8_t *out;
  27. uint32_t out_len;
  28. bool selftest_done;
  29. } TPMBackendCmd;
  30. struct TPMBackend {
  31. Object parent;
  32. /*< protected >*/
  33. TPMIf *tpmif;
  34. bool opened;
  35. bool had_startup_error;
  36. TPMBackendCmd *cmd;
  37. /* <public> */
  38. char *id;
  39. QLIST_ENTRY(TPMBackend) list;
  40. };
  41. struct TPMBackendClass {
  42. ObjectClass parent_class;
  43. enum TpmType type;
  44. const QemuOptDesc *opts;
  45. /* get a descriptive text of the backend to display to the user */
  46. const char *desc;
  47. TPMBackend *(*create)(QemuOpts *opts);
  48. /* start up the TPM on the backend - optional */
  49. int (*startup_tpm)(TPMBackend *t, size_t buffersize);
  50. /* optional */
  51. void (*reset)(TPMBackend *t);
  52. void (*cancel_cmd)(TPMBackend *t);
  53. /* optional */
  54. bool (*get_tpm_established_flag)(TPMBackend *t);
  55. /* optional */
  56. int (*reset_tpm_established_flag)(TPMBackend *t, uint8_t locty);
  57. TPMVersion (*get_tpm_version)(TPMBackend *t);
  58. size_t (*get_buffer_size)(TPMBackend *t);
  59. TpmTypeOptions *(*get_tpm_options)(TPMBackend *t);
  60. void (*handle_request)(TPMBackend *s, TPMBackendCmd *cmd, Error **errp);
  61. };
  62. /**
  63. * tpm_backend_get_type:
  64. * @s: the backend
  65. *
  66. * Returns the TpmType of the backend.
  67. */
  68. enum TpmType tpm_backend_get_type(TPMBackend *s);
  69. /**
  70. * tpm_backend_init:
  71. * @s: the backend to initialized
  72. * @tpmif: TPM interface
  73. * @datacb: callback for sending data to frontend
  74. * @errp: a pointer to return the #Error object if an error occurs.
  75. *
  76. * Initialize the backend with the given variables.
  77. *
  78. * Returns 0 on success.
  79. */
  80. int tpm_backend_init(TPMBackend *s, TPMIf *tpmif, Error **errp);
  81. /**
  82. * tpm_backend_startup_tpm:
  83. * @s: the backend whose TPM support is to be started
  84. * @buffersize: the buffer size the TPM is supposed to use,
  85. * 0 to leave it as-is
  86. *
  87. * Returns 0 on success.
  88. */
  89. int tpm_backend_startup_tpm(TPMBackend *s, size_t buffersize);
  90. /**
  91. * tpm_backend_had_startup_error:
  92. * @s: the backend to query for a startup error
  93. *
  94. * Check whether the backend had an error during startup. Returns
  95. * false if no error occurred and the backend can be used, true
  96. * otherwise.
  97. */
  98. bool tpm_backend_had_startup_error(TPMBackend *s);
  99. /**
  100. * tpm_backend_deliver_request:
  101. * @s: the backend to send the request to
  102. * @cmd: the command to deliver
  103. *
  104. * Send a request to the backend. The backend will then send the request
  105. * to the TPM implementation.
  106. */
  107. void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd);
  108. /**
  109. * tpm_backend_reset:
  110. * @s: the backend to reset
  111. *
  112. * Reset the backend into a well defined state with all previous errors
  113. * reset.
  114. */
  115. void tpm_backend_reset(TPMBackend *s);
  116. /**
  117. * tpm_backend_cancel_cmd:
  118. * @s: the backend
  119. *
  120. * Cancel any ongoing command being processed by the TPM implementation
  121. * on behalf of the QEMU guest.
  122. */
  123. void tpm_backend_cancel_cmd(TPMBackend *s);
  124. /**
  125. * tpm_backend_get_tpm_established_flag:
  126. * @s: the backend
  127. *
  128. * Get the TPM establishment flag. This function may be called very
  129. * frequently by the frontend since for example in the TIS implementation
  130. * this flag is part of a register.
  131. */
  132. bool tpm_backend_get_tpm_established_flag(TPMBackend *s);
  133. /**
  134. * tpm_backend_reset_tpm_established_flag:
  135. * @s: the backend
  136. * @locty: the locality number
  137. *
  138. * Reset the TPM establishment flag.
  139. */
  140. int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty);
  141. /**
  142. * tpm_backend_get_tpm_version:
  143. * @s: the backend to call into
  144. *
  145. * Get the TPM Version that is emulated at the backend.
  146. *
  147. * Returns TPMVersion.
  148. */
  149. TPMVersion tpm_backend_get_tpm_version(TPMBackend *s);
  150. /**
  151. * tpm_backend_get_buffer_size:
  152. * @s: the backend to call into
  153. *
  154. * Get the TPM's buffer size.
  155. *
  156. * Returns buffer size.
  157. */
  158. size_t tpm_backend_get_buffer_size(TPMBackend *s);
  159. /**
  160. * tpm_backend_finish_sync:
  161. * @s: the backend to call into
  162. *
  163. * Finish the pending command synchronously (this will call aio_poll()
  164. * on qemu main AIOContext until it ends)
  165. */
  166. void tpm_backend_finish_sync(TPMBackend *s);
  167. /**
  168. * tpm_backend_query_tpm:
  169. * @s: the backend
  170. *
  171. * Query backend tpm info
  172. *
  173. * Returns newly allocated TPMInfo
  174. */
  175. TPMInfo *tpm_backend_query_tpm(TPMBackend *s);
  176. TPMBackend *qemu_find_tpm_be(const char *id);
  177. #endif /* CONFIG_TPM */
  178. #endif /* TPM_BACKEND_H */