tpm.c 4.4 KB

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