2
0

tpm.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/qmp/qerror.h"
  16. #include "sysemu/tpm_backend.h"
  17. #include "sysemu/tpm.h"
  18. #include "qemu/config-file.h"
  19. #include "qemu/error-report.h"
  20. #include "qmp-commands.h"
  21. static QLIST_HEAD(, TPMBackend) tpm_backends =
  22. QLIST_HEAD_INITIALIZER(tpm_backends);
  23. static bool tpm_models[TPM_MODEL__MAX];
  24. void tpm_register_model(enum TpmModel model)
  25. {
  26. tpm_models[model] = true;
  27. }
  28. static const TPMBackendClass *
  29. tpm_be_find_by_type(enum TpmType type)
  30. {
  31. ObjectClass *oc;
  32. char *typename = g_strdup_printf("tpm-%s", TpmType_str(type));
  33. oc = object_class_by_name(typename);
  34. g_free(typename);
  35. if (!object_class_dynamic_cast(oc, TYPE_TPM_BACKEND)) {
  36. return NULL;
  37. }
  38. return TPM_BACKEND_CLASS(oc);
  39. }
  40. /*
  41. * Walk the list of available TPM backend drivers and display them on the
  42. * screen.
  43. */
  44. static void tpm_display_backend_drivers(void)
  45. {
  46. int i;
  47. fprintf(stderr, "Supported TPM types (choose only one):\n");
  48. for (i = 0; i < TPM_TYPE__MAX; i++) {
  49. const TPMBackendClass *bc = tpm_be_find_by_type(i);
  50. if (!bc) {
  51. continue;
  52. }
  53. fprintf(stderr, "%12s %s\n", TpmType_str(i), bc->desc);
  54. }
  55. fprintf(stderr, "\n");
  56. }
  57. /*
  58. * Find the TPM with the given Id
  59. */
  60. TPMBackend *qemu_find_tpm(const char *id)
  61. {
  62. TPMBackend *drv;
  63. if (id) {
  64. QLIST_FOREACH(drv, &tpm_backends, list) {
  65. if (!strcmp(drv->id, id)) {
  66. return drv;
  67. }
  68. }
  69. }
  70. return NULL;
  71. }
  72. static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
  73. {
  74. const char *value;
  75. const char *id;
  76. const TPMBackendClass *be;
  77. TPMBackend *drv;
  78. Error *local_err = NULL;
  79. int i;
  80. if (!QLIST_EMPTY(&tpm_backends)) {
  81. error_report("Only one TPM is allowed.");
  82. return 1;
  83. }
  84. id = qemu_opts_id(opts);
  85. if (id == NULL) {
  86. error_report(QERR_MISSING_PARAMETER, "id");
  87. return 1;
  88. }
  89. value = qemu_opt_get(opts, "type");
  90. if (!value) {
  91. error_report(QERR_MISSING_PARAMETER, "type");
  92. tpm_display_backend_drivers();
  93. return 1;
  94. }
  95. i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL);
  96. be = i >= 0 ? tpm_be_find_by_type(i) : NULL;
  97. if (be == NULL) {
  98. error_report(QERR_INVALID_PARAMETER_VALUE,
  99. "type", "a TPM backend type");
  100. tpm_display_backend_drivers();
  101. return 1;
  102. }
  103. /* validate backend specific opts */
  104. qemu_opts_validate(opts, be->opts, &local_err);
  105. if (local_err) {
  106. error_report_err(local_err);
  107. return 1;
  108. }
  109. drv = be->create(opts, id);
  110. if (!drv) {
  111. return 1;
  112. }
  113. tpm_backend_open(drv, &local_err);
  114. if (local_err) {
  115. error_report_err(local_err);
  116. return 1;
  117. }
  118. QLIST_INSERT_HEAD(&tpm_backends, drv, list);
  119. return 0;
  120. }
  121. /*
  122. * Walk the list of TPM backend drivers that are in use and call their
  123. * destroy function to have them cleaned up.
  124. */
  125. void tpm_cleanup(void)
  126. {
  127. TPMBackend *drv, *next;
  128. QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
  129. QLIST_REMOVE(drv, list);
  130. object_unref(OBJECT(drv));
  131. }
  132. }
  133. /*
  134. * Initialize the TPM. Process the tpmdev command line options describing the
  135. * TPM backend.
  136. */
  137. int tpm_init(void)
  138. {
  139. if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
  140. tpm_init_tpmdev, NULL, NULL)) {
  141. return -1;
  142. }
  143. return 0;
  144. }
  145. /*
  146. * Parse the TPM configuration options.
  147. * To display all available TPM backends the user may use '-tpmdev help'
  148. */
  149. int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
  150. {
  151. QemuOpts *opts;
  152. if (!strcmp(optarg, "help")) {
  153. tpm_display_backend_drivers();
  154. return -1;
  155. }
  156. opts = qemu_opts_parse_noisily(opts_list, optarg, true);
  157. if (!opts) {
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. /*
  163. * Walk the list of active TPM backends and collect information about them
  164. * following the schema description in qapi-schema.json.
  165. */
  166. TPMInfoList *qmp_query_tpm(Error **errp)
  167. {
  168. TPMBackend *drv;
  169. TPMInfoList *info, *head = NULL, *cur_item = NULL;
  170. QLIST_FOREACH(drv, &tpm_backends, list) {
  171. if (!tpm_models[drv->fe_model]) {
  172. continue;
  173. }
  174. info = g_new0(TPMInfoList, 1);
  175. info->value = tpm_backend_query_tpm(drv);
  176. if (!cur_item) {
  177. head = cur_item = info;
  178. } else {
  179. cur_item->next = info;
  180. cur_item = info;
  181. }
  182. }
  183. return head;
  184. }
  185. TpmTypeList *qmp_query_tpm_types(Error **errp)
  186. {
  187. unsigned int i = 0;
  188. TpmTypeList *head = NULL, *prev = NULL, *cur_item;
  189. for (i = 0; i < TPM_TYPE__MAX; i++) {
  190. if (!tpm_be_find_by_type(i)) {
  191. continue;
  192. }
  193. cur_item = g_new0(TpmTypeList, 1);
  194. cur_item->value = i;
  195. if (prev) {
  196. prev->next = cur_item;
  197. }
  198. if (!head) {
  199. head = cur_item;
  200. }
  201. prev = cur_item;
  202. }
  203. return head;
  204. }
  205. TpmModelList *qmp_query_tpm_models(Error **errp)
  206. {
  207. unsigned int i = 0;
  208. TpmModelList *head = NULL, *prev = NULL, *cur_item;
  209. for (i = 0; i < TPM_MODEL__MAX; i++) {
  210. if (!tpm_models[i]) {
  211. continue;
  212. }
  213. cur_item = g_new0(TpmModelList, 1);
  214. cur_item->value = i;
  215. if (prev) {
  216. prev->next = cur_item;
  217. }
  218. if (!head) {
  219. head = cur_item;
  220. }
  221. prev = cur_item;
  222. }
  223. return head;
  224. }