tpm.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. if (value == s->opened) {
  89. return;
  90. }
  91. if (!value && s->opened) {
  92. error_set(errp, QERR_PERMISSION_DENIED);
  93. return;
  94. }
  95. if (k->opened) {
  96. k->opened(s, errp);
  97. }
  98. if (!error_is_set(errp)) {
  99. s->opened = value;
  100. }
  101. }
  102. static void tpm_backend_instance_init(Object *obj)
  103. {
  104. object_property_add_bool(obj, "opened",
  105. tpm_backend_prop_get_opened,
  106. tpm_backend_prop_set_opened,
  107. NULL);
  108. }
  109. void tpm_backend_thread_deliver_request(TPMBackendThread *tbt)
  110. {
  111. g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD, NULL);
  112. }
  113. void tpm_backend_thread_create(TPMBackendThread *tbt,
  114. GFunc func, gpointer user_data)
  115. {
  116. if (!tbt->pool) {
  117. tbt->pool = g_thread_pool_new(func, user_data, 1, TRUE, NULL);
  118. g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL);
  119. }
  120. }
  121. void tpm_backend_thread_end(TPMBackendThread *tbt)
  122. {
  123. if (tbt->pool) {
  124. g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_END, NULL);
  125. g_thread_pool_free(tbt->pool, FALSE, TRUE);
  126. tbt->pool = NULL;
  127. }
  128. }
  129. void tpm_backend_thread_tpm_reset(TPMBackendThread *tbt,
  130. GFunc func, gpointer user_data)
  131. {
  132. if (!tbt->pool) {
  133. tpm_backend_thread_create(tbt, func, user_data);
  134. } else {
  135. g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_TPM_RESET,
  136. NULL);
  137. }
  138. }
  139. static const TypeInfo tpm_backend_info = {
  140. .name = TYPE_TPM_BACKEND,
  141. .parent = TYPE_OBJECT,
  142. .instance_size = sizeof(TPMBackend),
  143. .instance_init = tpm_backend_instance_init,
  144. .class_size = sizeof(TPMBackendClass),
  145. .abstract = true,
  146. };
  147. static void register_types(void)
  148. {
  149. type_register_static(&tpm_backend_info);
  150. }
  151. type_init(register_types);