2
0

tpm.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 "config-host.h"
  15. #include "monitor/monitor.h"
  16. #include "qapi/qmp/qerror.h"
  17. #include "sysemu/tpm_backend.h"
  18. #include "sysemu/tpm.h"
  19. #include "qemu/config-file.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.\n");
  114. return 1;
  115. }
  116. id = qemu_opts_id(opts);
  117. if (id == NULL) {
  118. qerror_report(QERR_MISSING_PARAMETER, "id");
  119. return 1;
  120. }
  121. value = qemu_opt_get(opts, "type");
  122. if (!value) {
  123. qerror_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. qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
  130. "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. qerror_report_err(local_err);
  138. error_free(local_err);
  139. return 1;
  140. }
  141. drv = be->create(opts, id);
  142. if (!drv) {
  143. return 1;
  144. }
  145. tpm_backend_open(drv, &local_err);
  146. if (local_err) {
  147. qerror_report_err(local_err);
  148. error_free(local_err);
  149. return 1;
  150. }
  151. QLIST_INSERT_HEAD(&tpm_backends, drv, list);
  152. return 0;
  153. }
  154. static int tpm_init_tpmdev(QemuOpts *opts, void *dummy)
  155. {
  156. return configure_tpm(opts);
  157. }
  158. /*
  159. * Walk the list of TPM backend drivers that are in use and call their
  160. * destroy function to have them cleaned up.
  161. */
  162. void tpm_cleanup(void)
  163. {
  164. TPMBackend *drv, *next;
  165. QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
  166. QLIST_REMOVE(drv, list);
  167. tpm_backend_destroy(drv);
  168. }
  169. }
  170. /*
  171. * Initialize the TPM. Process the tpmdev command line options describing the
  172. * TPM backend.
  173. */
  174. int tpm_init(void)
  175. {
  176. if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
  177. tpm_init_tpmdev, NULL, 1) != 0) {
  178. return -1;
  179. }
  180. atexit(tpm_cleanup);
  181. return 0;
  182. }
  183. /*
  184. * Parse the TPM configuration options.
  185. * To display all available TPM backends the user may use '-tpmdev help'
  186. */
  187. int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
  188. {
  189. QemuOpts *opts;
  190. if (!strcmp(optarg, "help")) {
  191. tpm_display_backend_drivers();
  192. return -1;
  193. }
  194. opts = qemu_opts_parse(opts_list, optarg, 1);
  195. if (!opts) {
  196. return -1;
  197. }
  198. return 0;
  199. }
  200. #endif /* CONFIG_TPM */
  201. static const TPMDriverOps *tpm_driver_find_by_type(enum TpmType type)
  202. {
  203. int i;
  204. for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
  205. if (be_drivers[i]->type == type) {
  206. return be_drivers[i];
  207. }
  208. }
  209. return NULL;
  210. }
  211. static TPMInfo *qmp_query_tpm_inst(TPMBackend *drv)
  212. {
  213. TPMInfo *res = g_new0(TPMInfo, 1);
  214. TPMPassthroughOptions *tpo;
  215. res->id = g_strdup(drv->id);
  216. res->model = drv->fe_model;
  217. res->options = g_new0(TpmTypeOptions, 1);
  218. switch (drv->ops->type) {
  219. case TPM_TYPE_PASSTHROUGH:
  220. res->options->kind = TPM_TYPE_OPTIONS_KIND_PASSTHROUGH;
  221. tpo = g_new0(TPMPassthroughOptions, 1);
  222. res->options->passthrough = tpo;
  223. if (drv->path) {
  224. tpo->path = g_strdup(drv->path);
  225. tpo->has_path = true;
  226. }
  227. if (drv->cancel_path) {
  228. tpo->cancel_path = g_strdup(drv->cancel_path);
  229. tpo->has_cancel_path = true;
  230. }
  231. break;
  232. case TPM_TYPE_MAX:
  233. break;
  234. }
  235. return res;
  236. }
  237. /*
  238. * Walk the list of active TPM backends and collect information about them
  239. * following the schema description in qapi-schema.json.
  240. */
  241. TPMInfoList *qmp_query_tpm(Error **errp)
  242. {
  243. TPMBackend *drv;
  244. TPMInfoList *info, *head = NULL, *cur_item = NULL;
  245. QLIST_FOREACH(drv, &tpm_backends, list) {
  246. if (!tpm_model_is_registered(drv->fe_model)) {
  247. continue;
  248. }
  249. info = g_new0(TPMInfoList, 1);
  250. info->value = qmp_query_tpm_inst(drv);
  251. if (!cur_item) {
  252. head = cur_item = info;
  253. } else {
  254. cur_item->next = info;
  255. cur_item = info;
  256. }
  257. }
  258. return head;
  259. }
  260. TpmTypeList *qmp_query_tpm_types(Error **errp)
  261. {
  262. unsigned int i = 0;
  263. TpmTypeList *head = NULL, *prev = NULL, *cur_item;
  264. for (i = 0; i < TPM_TYPE_MAX; i++) {
  265. if (!tpm_driver_find_by_type(i)) {
  266. continue;
  267. }
  268. cur_item = g_new0(TpmTypeList, 1);
  269. cur_item->value = i;
  270. if (prev) {
  271. prev->next = cur_item;
  272. }
  273. if (!head) {
  274. head = cur_item;
  275. }
  276. prev = cur_item;
  277. }
  278. return head;
  279. }
  280. TpmModelList *qmp_query_tpm_models(Error **errp)
  281. {
  282. unsigned int i = 0;
  283. TpmModelList *head = NULL, *prev = NULL, *cur_item;
  284. for (i = 0; i < TPM_MODEL_MAX; i++) {
  285. if (!tpm_model_is_registered(i)) {
  286. continue;
  287. }
  288. cur_item = g_new0(TpmModelList, 1);
  289. cur_item->value = i;
  290. if (prev) {
  291. prev->next = cur_item;
  292. }
  293. if (!head) {
  294. head = cur_item;
  295. }
  296. prev = cur_item;
  297. }
  298. return head;
  299. }