You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tpm.c 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "sysemu/tpm_backend.h"
  19. #include "sysemu/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. int i;
  43. fprintf(stderr, "Supported TPM types (choose only one):\n");
  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. fprintf(stderr, "%12s %s\n", TpmType_str(i), bc->desc);
  50. }
  51. fprintf(stderr, "\n");
  52. }
  53. /*
  54. * Find the TPM with the given Id
  55. */
  56. TPMBackend *qemu_find_tpm_be(const char *id)
  57. {
  58. TPMBackend *drv;
  59. if (id) {
  60. QLIST_FOREACH(drv, &tpm_backends, list) {
  61. if (!strcmp(drv->id, id)) {
  62. return drv;
  63. }
  64. }
  65. }
  66. return NULL;
  67. }
  68. static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
  69. {
  70. const char *value;
  71. const char *id;
  72. const TPMBackendClass *be;
  73. TPMBackend *drv;
  74. Error *local_err = NULL;
  75. int i;
  76. if (!QLIST_EMPTY(&tpm_backends)) {
  77. error_setg(errp, "Only one TPM is allowed.");
  78. return 1;
  79. }
  80. id = qemu_opts_id(opts);
  81. if (id == NULL) {
  82. error_setg(errp, QERR_MISSING_PARAMETER, "id");
  83. return 1;
  84. }
  85. value = qemu_opt_get(opts, "type");
  86. if (!value) {
  87. error_setg(errp, QERR_MISSING_PARAMETER, "type");
  88. tpm_display_backend_drivers();
  89. return 1;
  90. }
  91. i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL);
  92. be = i >= 0 ? tpm_be_find_by_type(i) : NULL;
  93. if (be == NULL) {
  94. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
  95. "a TPM backend type");
  96. tpm_display_backend_drivers();
  97. return 1;
  98. }
  99. /* validate backend specific opts */
  100. qemu_opts_validate(opts, be->opts, &local_err);
  101. if (local_err) {
  102. error_propagate(errp, local_err);
  103. return 1;
  104. }
  105. drv = be->create(opts);
  106. if (!drv) {
  107. return 1;
  108. }
  109. drv->id = g_strdup(id);
  110. QLIST_INSERT_HEAD(&tpm_backends, drv, list);
  111. return 0;
  112. }
  113. /*
  114. * Walk the list of TPM backend drivers that are in use and call their
  115. * destroy function to have them cleaned up.
  116. */
  117. void tpm_cleanup(void)
  118. {
  119. TPMBackend *drv, *next;
  120. QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
  121. QLIST_REMOVE(drv, list);
  122. object_unref(OBJECT(drv));
  123. }
  124. }
  125. /*
  126. * Initialize the TPM. Process the tpmdev command line options describing the
  127. * TPM backend.
  128. */
  129. void tpm_init(void)
  130. {
  131. qemu_opts_foreach(qemu_find_opts("tpmdev"),
  132. tpm_init_tpmdev, NULL, &error_fatal);
  133. }
  134. /*
  135. * Parse the TPM configuration options.
  136. * To display all available TPM backends the user may use '-tpmdev help'
  137. */
  138. int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
  139. {
  140. QemuOpts *opts;
  141. if (!strcmp(optarg, "help")) {
  142. tpm_display_backend_drivers();
  143. return -1;
  144. }
  145. opts = qemu_opts_parse_noisily(opts_list, optarg, true);
  146. if (!opts) {
  147. return -1;
  148. }
  149. return 0;
  150. }
  151. /*
  152. * Walk the list of active TPM backends and collect information about them.
  153. */
  154. TPMInfoList *qmp_query_tpm(Error **errp)
  155. {
  156. TPMBackend *drv;
  157. TPMInfoList *info, *head = NULL, *cur_item = NULL;
  158. QLIST_FOREACH(drv, &tpm_backends, list) {
  159. if (!drv->tpmif) {
  160. continue;
  161. }
  162. info = g_new0(TPMInfoList, 1);
  163. info->value = tpm_backend_query_tpm(drv);
  164. if (!cur_item) {
  165. head = cur_item = info;
  166. } else {
  167. cur_item->next = info;
  168. cur_item = info;
  169. }
  170. }
  171. return head;
  172. }
  173. TpmTypeList *qmp_query_tpm_types(Error **errp)
  174. {
  175. unsigned int i = 0;
  176. TpmTypeList *head = NULL, *prev = NULL, *cur_item;
  177. for (i = 0; i < TPM_TYPE__MAX; i++) {
  178. if (!tpm_be_find_by_type(i)) {
  179. continue;
  180. }
  181. cur_item = g_new0(TpmTypeList, 1);
  182. cur_item->value = i;
  183. if (prev) {
  184. prev->next = cur_item;
  185. }
  186. if (!head) {
  187. head = cur_item;
  188. }
  189. prev = cur_item;
  190. }
  191. return head;
  192. }
  193. TpmModelList *qmp_query_tpm_models(Error **errp)
  194. {
  195. TpmModelList *head = NULL, *prev = NULL, *cur_item;
  196. GSList *e, *l = object_class_get_list(TYPE_TPM_IF, false);
  197. for (e = l; e; e = e->next) {
  198. TPMIfClass *c = TPM_IF_CLASS(e->data);
  199. cur_item = g_new0(TpmModelList, 1);
  200. cur_item->value = c->model;
  201. if (prev) {
  202. prev->next = cur_item;
  203. }
  204. if (!head) {
  205. head = cur_item;
  206. }
  207. prev = cur_item;
  208. }
  209. g_slist_free(l);
  210. return head;
  211. }