tpm.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. * Based on backends/rng.c by Anthony Liguori
  13. */
  14. #include "qemu/osdep.h"
  15. #include "sysemu/tpm_backend.h"
  16. #include "qapi/error.h"
  17. #include "qapi/qmp/qerror.h"
  18. #include "sysemu/tpm.h"
  19. #include "qemu/thread.h"
  20. #include "sysemu/tpm_backend_int.h"
  21. enum TpmType tpm_backend_get_type(TPMBackend *s)
  22. {
  23. TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
  24. return k->ops->type;
  25. }
  26. const char *tpm_backend_get_desc(TPMBackend *s)
  27. {
  28. TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
  29. return k->ops->desc();
  30. }
  31. void tpm_backend_destroy(TPMBackend *s)
  32. {
  33. TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
  34. k->ops->destroy(s);
  35. }
  36. int tpm_backend_init(TPMBackend *s, TPMState *state,
  37. TPMRecvDataCB *datacb)
  38. {
  39. TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
  40. return k->ops->init(s, state, datacb);
  41. }
  42. int tpm_backend_startup_tpm(TPMBackend *s)
  43. {
  44. TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
  45. return k->ops->startup_tpm(s);
  46. }
  47. bool tpm_backend_had_startup_error(TPMBackend *s)
  48. {
  49. TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
  50. return k->ops->had_startup_error(s);
  51. }
  52. size_t tpm_backend_realloc_buffer(TPMBackend *s, TPMSizedBuffer *sb)
  53. {
  54. TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
  55. return k->ops->realloc_buffer(sb);
  56. }
  57. void tpm_backend_deliver_request(TPMBackend *s)
  58. {
  59. TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
  60. k->ops->deliver_request(s);
  61. }
  62. void tpm_backend_reset(TPMBackend *s)
  63. {
  64. TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
  65. k->ops->reset(s);
  66. }
  67. void tpm_backend_cancel_cmd(TPMBackend *s)
  68. {
  69. TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
  70. k->ops->cancel_cmd(s);
  71. }
  72. bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
  73. {
  74. TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
  75. return k->ops->get_tpm_established_flag(s);
  76. }
  77. int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
  78. {
  79. TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
  80. return k->ops->reset_tpm_established_flag(s, locty);
  81. }
  82. TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
  83. {
  84. TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
  85. return k->ops->get_tpm_version(s);
  86. }
  87. static bool tpm_backend_prop_get_opened(Object *obj, Error **errp)
  88. {
  89. TPMBackend *s = TPM_BACKEND(obj);
  90. return s->opened;
  91. }
  92. void tpm_backend_open(TPMBackend *s, Error **errp)
  93. {
  94. object_property_set_bool(OBJECT(s), true, "opened", errp);
  95. }
  96. static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp)
  97. {
  98. TPMBackend *s = TPM_BACKEND(obj);
  99. TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
  100. Error *local_err = NULL;
  101. if (value == s->opened) {
  102. return;
  103. }
  104. if (!value && s->opened) {
  105. error_setg(errp, QERR_PERMISSION_DENIED);
  106. return;
  107. }
  108. if (k->opened) {
  109. k->opened(s, &local_err);
  110. if (local_err) {
  111. error_propagate(errp, local_err);
  112. return;
  113. }
  114. }
  115. s->opened = true;
  116. }
  117. static void tpm_backend_instance_init(Object *obj)
  118. {
  119. object_property_add_bool(obj, "opened",
  120. tpm_backend_prop_get_opened,
  121. tpm_backend_prop_set_opened,
  122. NULL);
  123. }
  124. void tpm_backend_thread_deliver_request(TPMBackendThread *tbt)
  125. {
  126. g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD, NULL);
  127. }
  128. void tpm_backend_thread_create(TPMBackendThread *tbt,
  129. GFunc func, gpointer user_data)
  130. {
  131. if (!tbt->pool) {
  132. tbt->pool = g_thread_pool_new(func, user_data, 1, TRUE, NULL);
  133. g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL);
  134. }
  135. }
  136. void tpm_backend_thread_end(TPMBackendThread *tbt)
  137. {
  138. if (tbt->pool) {
  139. g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_END, NULL);
  140. g_thread_pool_free(tbt->pool, FALSE, TRUE);
  141. tbt->pool = NULL;
  142. }
  143. }
  144. static const TypeInfo tpm_backend_info = {
  145. .name = TYPE_TPM_BACKEND,
  146. .parent = TYPE_OBJECT,
  147. .instance_size = sizeof(TPMBackend),
  148. .instance_init = tpm_backend_instance_init,
  149. .class_size = sizeof(TPMBackendClass),
  150. .abstract = true,
  151. };
  152. static void register_types(void)
  153. {
  154. type_register_static(&tpm_backend_info);
  155. }
  156. type_init(register_types);