2
0

tpm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_report("Only one TPM is allowed.");
  78. return 1;
  79. }
  80. id = qemu_opts_id(opts);
  81. if (id == NULL) {
  82. error_report(QERR_MISSING_PARAMETER, "id");
  83. return 1;
  84. }
  85. value = qemu_opt_get(opts, "type");
  86. if (!value) {
  87. error_report(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_report(QERR_INVALID_PARAMETER_VALUE,
  95. "type", "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_report_err(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. int tpm_init(void)
  130. {
  131. if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
  132. tpm_init_tpmdev, NULL, NULL)) {
  133. return -1;
  134. }
  135. return 0;
  136. }
  137. /*
  138. * Parse the TPM configuration options.
  139. * To display all available TPM backends the user may use '-tpmdev help'
  140. */
  141. int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
  142. {
  143. QemuOpts *opts;
  144. if (!strcmp(optarg, "help")) {
  145. tpm_display_backend_drivers();
  146. return -1;
  147. }
  148. opts = qemu_opts_parse_noisily(opts_list, optarg, true);
  149. if (!opts) {
  150. return -1;
  151. }
  152. return 0;
  153. }
  154. /*
  155. * Walk the list of active TPM backends and collect information about them.
  156. */
  157. TPMInfoList *qmp_query_tpm(Error **errp)
  158. {
  159. TPMBackend *drv;
  160. TPMInfoList *info, *head = NULL, *cur_item = NULL;
  161. QLIST_FOREACH(drv, &tpm_backends, list) {
  162. if (!drv->tpmif) {
  163. continue;
  164. }
  165. info = g_new0(TPMInfoList, 1);
  166. info->value = tpm_backend_query_tpm(drv);
  167. if (!cur_item) {
  168. head = cur_item = info;
  169. } else {
  170. cur_item->next = info;
  171. cur_item = info;
  172. }
  173. }
  174. return head;
  175. }
  176. TpmTypeList *qmp_query_tpm_types(Error **errp)
  177. {
  178. unsigned int i = 0;
  179. TpmTypeList *head = NULL, *prev = NULL, *cur_item;
  180. for (i = 0; i < TPM_TYPE__MAX; i++) {
  181. if (!tpm_be_find_by_type(i)) {
  182. continue;
  183. }
  184. cur_item = g_new0(TpmTypeList, 1);
  185. cur_item->value = i;
  186. if (prev) {
  187. prev->next = cur_item;
  188. }
  189. if (!head) {
  190. head = cur_item;
  191. }
  192. prev = cur_item;
  193. }
  194. return head;
  195. }
  196. TpmModelList *qmp_query_tpm_models(Error **errp)
  197. {
  198. TpmModelList *head = NULL, *prev = NULL, *cur_item;
  199. GSList *e, *l = object_class_get_list(TYPE_TPM_IF, false);
  200. for (e = l; e; e = e->next) {
  201. TPMIfClass *c = TPM_IF_CLASS(e->data);
  202. cur_item = g_new0(TpmModelList, 1);
  203. cur_item->value = c->model;
  204. if (prev) {
  205. prev->next = cur_item;
  206. }
  207. if (!head) {
  208. head = cur_item;
  209. }
  210. prev = cur_item;
  211. }
  212. g_slist_free(l);
  213. return head;
  214. }