tpm.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * TPM configuration
  3. *
  4. * Copyright (C) 2011-2013 IBM Corporation
  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 net.c
  13. */
  14. #include "qemu/osdep.h"
  15. #include "qapi/error.h"
  16. #include "qapi/qapi-commands-tpm.h"
  17. #include "qapi/qmp/qerror.h"
  18. #include "system/tpm_backend.h"
  19. #include "system/tpm.h"
  20. #include "qemu/config-file.h"
  21. #include "qemu/error-report.h"
  22. static QLIST_HEAD(, TPMBackend) tpm_backends =
  23. QLIST_HEAD_INITIALIZER(tpm_backends);
  24. static const TPMBackendClass *
  25. tpm_be_find_by_type(enum TpmType type)
  26. {
  27. ObjectClass *oc;
  28. char *typename = g_strdup_printf("tpm-%s", TpmType_str(type));
  29. oc = object_class_by_name(typename);
  30. g_free(typename);
  31. if (!object_class_dynamic_cast(oc, TYPE_TPM_BACKEND)) {
  32. return NULL;
  33. }
  34. return TPM_BACKEND_CLASS(oc);
  35. }
  36. /*
  37. * Walk the list of available TPM backend drivers and display them on the
  38. * screen.
  39. */
  40. static void tpm_display_backend_drivers(void)
  41. {
  42. bool got_one = false;
  43. int i;
  44. for (i = 0; i < TPM_TYPE__MAX; i++) {
  45. const TPMBackendClass *bc = tpm_be_find_by_type(i);
  46. if (!bc) {
  47. continue;
  48. }
  49. if (!got_one) {
  50. error_printf("Supported TPM types (choose only one):\n");
  51. got_one = true;
  52. }
  53. error_printf("%12s %s\n", TpmType_str(i), bc->desc);
  54. }
  55. if (!got_one) {
  56. error_printf("No TPM backend types are available\n");
  57. }
  58. }
  59. /*
  60. * Find the TPM with the given Id
  61. */
  62. TPMBackend *qemu_find_tpm_be(const char *id)
  63. {
  64. TPMBackend *drv;
  65. if (id) {
  66. QLIST_FOREACH(drv, &tpm_backends, list) {
  67. if (!strcmp(drv->id, id)) {
  68. return drv;
  69. }
  70. }
  71. }
  72. return NULL;
  73. }
  74. static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
  75. {
  76. /*
  77. * Use of error_report() in a function with an Error ** parameter
  78. * is suspicious. It is okay here. The parameter only exists to
  79. * make the function usable with qemu_opts_foreach(). It is not
  80. * actually used.
  81. */
  82. const char *value;
  83. const char *id;
  84. const TPMBackendClass *be;
  85. TPMBackend *drv;
  86. Error *local_err = NULL;
  87. int i;
  88. if (!QLIST_EMPTY(&tpm_backends)) {
  89. error_report("Only one TPM is allowed.");
  90. return 1;
  91. }
  92. id = qemu_opts_id(opts);
  93. if (id == NULL) {
  94. error_report(QERR_MISSING_PARAMETER, "id");
  95. return 1;
  96. }
  97. value = qemu_opt_get(opts, "type");
  98. if (!value) {
  99. error_report(QERR_MISSING_PARAMETER, "type");
  100. tpm_display_backend_drivers();
  101. return 1;
  102. }
  103. i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL);
  104. be = i >= 0 ? tpm_be_find_by_type(i) : NULL;
  105. if (be == NULL) {
  106. error_report(QERR_INVALID_PARAMETER_VALUE,
  107. "type", "a TPM backend type");
  108. tpm_display_backend_drivers();
  109. return 1;
  110. }
  111. /* validate backend specific opts */
  112. if (!qemu_opts_validate(opts, be->opts, &local_err)) {
  113. error_report_err(local_err);
  114. return 1;
  115. }
  116. drv = be->create(opts);
  117. if (!drv) {
  118. return 1;
  119. }
  120. drv->id = g_strdup(id);
  121. QLIST_INSERT_HEAD(&tpm_backends, drv, list);
  122. return 0;
  123. }
  124. /*
  125. * Walk the list of TPM backend drivers that are in use and call their
  126. * destroy function to have them cleaned up.
  127. */
  128. void tpm_cleanup(void)
  129. {
  130. TPMBackend *drv, *next;
  131. QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
  132. QLIST_REMOVE(drv, list);
  133. object_unref(OBJECT(drv));
  134. }
  135. }
  136. /*
  137. * Initialize the TPM. Process the tpmdev command line options describing the
  138. * TPM backend.
  139. */
  140. int tpm_init(void)
  141. {
  142. if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
  143. tpm_init_tpmdev, NULL, NULL)) {
  144. return -1;
  145. }
  146. return 0;
  147. }
  148. /*
  149. * Parse the TPM configuration options.
  150. * To display all available TPM backends the user may use '-tpmdev help'
  151. */
  152. int tpm_config_parse(QemuOptsList *opts_list, const char *optstr)
  153. {
  154. QemuOpts *opts;
  155. if (!strcmp(optstr, "help")) {
  156. tpm_display_backend_drivers();
  157. return -1;
  158. }
  159. opts = qemu_opts_parse_noisily(opts_list, optstr, true);
  160. if (!opts) {
  161. return -1;
  162. }
  163. return 0;
  164. }
  165. /*
  166. * Walk the list of active TPM backends and collect information about them.
  167. */
  168. TPMInfoList *qmp_query_tpm(Error **errp)
  169. {
  170. TPMBackend *drv;
  171. TPMInfoList *head = NULL, **tail = &head;
  172. QLIST_FOREACH(drv, &tpm_backends, list) {
  173. if (!drv->tpmif) {
  174. continue;
  175. }
  176. QAPI_LIST_APPEND(tail, tpm_backend_query_tpm(drv));
  177. }
  178. return head;
  179. }
  180. TpmTypeList *qmp_query_tpm_types(Error **errp)
  181. {
  182. unsigned int i = 0;
  183. TpmTypeList *head = NULL, **tail = &head;
  184. for (i = 0; i < TPM_TYPE__MAX; i++) {
  185. if (!tpm_be_find_by_type(i)) {
  186. continue;
  187. }
  188. QAPI_LIST_APPEND(tail, i);
  189. }
  190. return head;
  191. }
  192. TpmModelList *qmp_query_tpm_models(Error **errp)
  193. {
  194. TpmModelList *head = NULL, **tail = &head;
  195. GSList *e, *l = object_class_get_list(TYPE_TPM_IF, false);
  196. for (e = l; e; e = e->next) {
  197. TPMIfClass *c = TPM_IF_CLASS(e->data);
  198. QAPI_LIST_APPEND(tail, c->model);
  199. }
  200. g_slist_free(l);
  201. return head;
  202. }