tpm.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. #define TPM_MAX_MODELS 1
  24. #define TPM_MAX_DRIVERS 1
  25. static TPMDriverOps const *be_drivers[TPM_MAX_DRIVERS] = {
  26. NULL,
  27. };
  28. static enum TpmModel tpm_models[TPM_MAX_MODELS] = {
  29. TPM_MODEL__MAX,
  30. };
  31. int tpm_register_model(enum TpmModel model)
  32. {
  33. int i;
  34. for (i = 0; i < TPM_MAX_MODELS; i++) {
  35. if (tpm_models[i] == TPM_MODEL__MAX) {
  36. tpm_models[i] = model;
  37. return 0;
  38. }
  39. }
  40. error_report("Could not register TPM model");
  41. return 1;
  42. }
  43. static bool tpm_model_is_registered(enum TpmModel model)
  44. {
  45. int i;
  46. for (i = 0; i < TPM_MAX_MODELS; i++) {
  47. if (tpm_models[i] == model) {
  48. return true;
  49. }
  50. }
  51. return false;
  52. }
  53. const TPMDriverOps *tpm_get_backend_driver(const char *type)
  54. {
  55. int i;
  56. for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
  57. if (!strcmp(TpmType_lookup[be_drivers[i]->type], type)) {
  58. return be_drivers[i];
  59. }
  60. }
  61. return NULL;
  62. }
  63. #ifdef CONFIG_TPM
  64. int tpm_register_driver(const TPMDriverOps *tdo)
  65. {
  66. int i;
  67. for (i = 0; i < TPM_MAX_DRIVERS; i++) {
  68. if (!be_drivers[i]) {
  69. be_drivers[i] = tdo;
  70. return 0;
  71. }
  72. }
  73. error_report("Could not register TPM driver");
  74. return 1;
  75. }
  76. /*
  77. * Walk the list of available TPM backend drivers and display them on the
  78. * screen.
  79. */
  80. static void tpm_display_backend_drivers(void)
  81. {
  82. int i;
  83. fprintf(stderr, "Supported TPM types (choose only one):\n");
  84. for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
  85. fprintf(stderr, "%12s %s\n",
  86. TpmType_lookup[be_drivers[i]->type], be_drivers[i]->desc());
  87. }
  88. fprintf(stderr, "\n");
  89. }
  90. /*
  91. * Find the TPM with the given Id
  92. */
  93. TPMBackend *qemu_find_tpm(const char *id)
  94. {
  95. TPMBackend *drv;
  96. if (id) {
  97. QLIST_FOREACH(drv, &tpm_backends, list) {
  98. if (!strcmp(drv->id, id)) {
  99. return drv;
  100. }
  101. }
  102. }
  103. return NULL;
  104. }
  105. static int configure_tpm(QemuOpts *opts)
  106. {
  107. const char *value;
  108. const char *id;
  109. const TPMDriverOps *be;
  110. TPMBackend *drv;
  111. Error *local_err = NULL;
  112. if (!QLIST_EMPTY(&tpm_backends)) {
  113. error_report("Only one TPM is allowed.");
  114. return 1;
  115. }
  116. id = qemu_opts_id(opts);
  117. if (id == NULL) {
  118. error_report(QERR_MISSING_PARAMETER, "id");
  119. return 1;
  120. }
  121. value = qemu_opt_get(opts, "type");
  122. if (!value) {
  123. error_report(QERR_MISSING_PARAMETER, "type");
  124. tpm_display_backend_drivers();
  125. return 1;
  126. }
  127. be = tpm_get_backend_driver(value);
  128. if (be == NULL) {
  129. error_report(QERR_INVALID_PARAMETER_VALUE,
  130. "type", "a TPM backend type");
  131. tpm_display_backend_drivers();
  132. return 1;
  133. }
  134. /* validate backend specific opts */
  135. qemu_opts_validate(opts, be->opts, &local_err);
  136. if (local_err) {
  137. error_report_err(local_err);
  138. return 1;
  139. }
  140. drv = be->create(opts, id);
  141. if (!drv) {
  142. return 1;
  143. }
  144. tpm_backend_open(drv, &local_err);
  145. if (local_err) {
  146. error_report_err(local_err);
  147. return 1;
  148. }
  149. QLIST_INSERT_HEAD(&tpm_backends, drv, list);
  150. return 0;
  151. }
  152. static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
  153. {
  154. return configure_tpm(opts);
  155. }
  156. /*
  157. * Walk the list of TPM backend drivers that are in use and call their
  158. * destroy function to have them cleaned up.
  159. */
  160. void tpm_cleanup(void)
  161. {
  162. TPMBackend *drv, *next;
  163. QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
  164. QLIST_REMOVE(drv, list);
  165. tpm_backend_destroy(drv);
  166. }
  167. }
  168. /*
  169. * Initialize the TPM. Process the tpmdev command line options describing the
  170. * TPM backend.
  171. */
  172. int tpm_init(void)
  173. {
  174. if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
  175. tpm_init_tpmdev, NULL, NULL)) {
  176. return -1;
  177. }
  178. atexit(tpm_cleanup);
  179. return 0;
  180. }
  181. /*
  182. * Parse the TPM configuration options.
  183. * To display all available TPM backends the user may use '-tpmdev help'
  184. */
  185. int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
  186. {
  187. QemuOpts *opts;
  188. if (!strcmp(optarg, "help")) {
  189. tpm_display_backend_drivers();
  190. return -1;
  191. }
  192. opts = qemu_opts_parse_noisily(opts_list, optarg, true);
  193. if (!opts) {
  194. return -1;
  195. }
  196. return 0;
  197. }
  198. #endif /* CONFIG_TPM */
  199. static const TPMDriverOps *tpm_driver_find_by_type(enum TpmType type)
  200. {
  201. int i;
  202. for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
  203. if (be_drivers[i]->type == type) {
  204. return be_drivers[i];
  205. }
  206. }
  207. return NULL;
  208. }
  209. static TPMInfo *qmp_query_tpm_inst(TPMBackend *drv)
  210. {
  211. TPMInfo *res = g_new0(TPMInfo, 1);
  212. TPMPassthroughOptions *tpo;
  213. res->id = g_strdup(drv->id);
  214. res->model = drv->fe_model;
  215. res->options = g_new0(TpmTypeOptions, 1);
  216. switch (drv->ops->type) {
  217. case TPM_TYPE_PASSTHROUGH:
  218. res->options->type = TPM_TYPE_OPTIONS_KIND_PASSTHROUGH;
  219. tpo = g_new0(TPMPassthroughOptions, 1);
  220. res->options->u.passthrough = tpo;
  221. if (drv->path) {
  222. tpo->path = g_strdup(drv->path);
  223. tpo->has_path = true;
  224. }
  225. if (drv->cancel_path) {
  226. tpo->cancel_path = g_strdup(drv->cancel_path);
  227. tpo->has_cancel_path = true;
  228. }
  229. break;
  230. case TPM_TYPE__MAX:
  231. break;
  232. }
  233. return res;
  234. }
  235. /*
  236. * Walk the list of active TPM backends and collect information about them
  237. * following the schema description in qapi-schema.json.
  238. */
  239. TPMInfoList *qmp_query_tpm(Error **errp)
  240. {
  241. TPMBackend *drv;
  242. TPMInfoList *info, *head = NULL, *cur_item = NULL;
  243. QLIST_FOREACH(drv, &tpm_backends, list) {
  244. if (!tpm_model_is_registered(drv->fe_model)) {
  245. continue;
  246. }
  247. info = g_new0(TPMInfoList, 1);
  248. info->value = qmp_query_tpm_inst(drv);
  249. if (!cur_item) {
  250. head = cur_item = info;
  251. } else {
  252. cur_item->next = info;
  253. cur_item = info;
  254. }
  255. }
  256. return head;
  257. }
  258. TpmTypeList *qmp_query_tpm_types(Error **errp)
  259. {
  260. unsigned int i = 0;
  261. TpmTypeList *head = NULL, *prev = NULL, *cur_item;
  262. for (i = 0; i < TPM_TYPE__MAX; i++) {
  263. if (!tpm_driver_find_by_type(i)) {
  264. continue;
  265. }
  266. cur_item = g_new0(TpmTypeList, 1);
  267. cur_item->value = i;
  268. if (prev) {
  269. prev->next = cur_item;
  270. }
  271. if (!head) {
  272. head = cur_item;
  273. }
  274. prev = cur_item;
  275. }
  276. return head;
  277. }
  278. TpmModelList *qmp_query_tpm_models(Error **errp)
  279. {
  280. unsigned int i = 0;
  281. TpmModelList *head = NULL, *prev = NULL, *cur_item;
  282. for (i = 0; i < TPM_MODEL__MAX; i++) {
  283. if (!tpm_model_is_registered(i)) {
  284. continue;
  285. }
  286. cur_item = g_new0(TpmModelList, 1);
  287. cur_item->value = i;
  288. if (prev) {
  289. prev->next = cur_item;
  290. }
  291. if (!head) {
  292. head = cur_item;
  293. }
  294. prev = cur_item;
  295. }
  296. return head;
  297. }