blob: 9c9e20bbb7e070e864d5d36e99bebafe0ee37dee [file] [log] [blame]
Stefan Bergerd1a0cf72013-02-27 12:47:49 -05001/*
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 */
Markus Armbrustere688df62018-02-01 12:18:31 +010014
Peter Maydelld38ea872016-01-29 17:50:05 +000015#include "qemu/osdep.h"
Stefan Bergerd1a0cf72013-02-27 12:47:49 -050016
Markus Armbrustere688df62018-02-01 12:18:31 +010017#include "qapi/error.h"
Markus Armbruster9af23982018-02-11 10:36:01 +010018#include "qapi/qapi-commands-tpm.h"
Stefan Bergerd1a0cf72013-02-27 12:47:49 -050019#include "qapi/qmp/qerror.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020020#include "sysemu/tpm_backend.h"
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020021#include "sysemu/tpm.h"
Stefan Bergerd1a0cf72013-02-27 12:47:49 -050022#include "qemu/config-file.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010023#include "qemu/error-report.h"
Stefan Bergerd1a0cf72013-02-27 12:47:49 -050024
25static QLIST_HEAD(, TPMBackend) tpm_backends =
26 QLIST_HEAD_INITIALIZER(tpm_backends);
27
Marc-André Lureaud31076b2017-10-10 00:55:49 +020028static const TPMBackendClass *
29tpm_be_find_by_type(enum TpmType type)
Marc-André Lureauf3faa1d2017-10-10 00:55:45 +020030{
31 ObjectClass *oc;
Marc-André Lureauf3faa1d2017-10-10 00:55:45 +020032 char *typename = g_strdup_printf("tpm-%s", TpmType_str(type));
33
34 oc = object_class_by_name(typename);
35 g_free(typename);
36
37 if (!object_class_dynamic_cast(oc, TYPE_TPM_BACKEND)) {
38 return NULL;
39 }
40
Marc-André Lureaud31076b2017-10-10 00:55:49 +020041 return TPM_BACKEND_CLASS(oc);
Marc-André Lureauf3faa1d2017-10-10 00:55:45 +020042}
43
Stefan Bergerd1a0cf72013-02-27 12:47:49 -050044/*
45 * Walk the list of available TPM backend drivers and display them on the
46 * screen.
47 */
Paolo Bonzinibdee56f2013-04-02 18:28:41 +020048static void tpm_display_backend_drivers(void)
Stefan Bergerd1a0cf72013-02-27 12:47:49 -050049{
50 int i;
51
52 fprintf(stderr, "Supported TPM types (choose only one):\n");
53
Marc-André Lureaua9a72ae2017-08-24 10:45:58 +020054 for (i = 0; i < TPM_TYPE__MAX; i++) {
Marc-André Lureaud31076b2017-10-10 00:55:49 +020055 const TPMBackendClass *bc = tpm_be_find_by_type(i);
56 if (!bc) {
Marc-André Lureaua9a72ae2017-08-24 10:45:58 +020057 continue;
58 }
Marc-André Lureaud31076b2017-10-10 00:55:49 +020059 fprintf(stderr, "%12s %s\n", TpmType_str(i), bc->desc);
Stefan Bergerd1a0cf72013-02-27 12:47:49 -050060 }
61 fprintf(stderr, "\n");
62}
63
64/*
65 * Find the TPM with the given Id
66 */
Marc-André Lureaud36e7db2017-11-06 19:39:14 +010067TPMBackend *qemu_find_tpm_be(const char *id)
Stefan Bergerd1a0cf72013-02-27 12:47:49 -050068{
69 TPMBackend *drv;
70
71 if (id) {
72 QLIST_FOREACH(drv, &tpm_backends, list) {
73 if (!strcmp(drv->id, id)) {
74 return drv;
75 }
76 }
77 }
78
79 return NULL;
80}
81
Marc-André Lureau76ae76b2017-10-10 00:55:51 +020082static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
Stefan Bergerd1a0cf72013-02-27 12:47:49 -050083{
84 const char *value;
85 const char *id;
Marc-André Lureaud31076b2017-10-10 00:55:49 +020086 const TPMBackendClass *be;
Stefan Bergerd1a0cf72013-02-27 12:47:49 -050087 TPMBackend *drv;
Stefan Berger8f0605c2013-03-28 07:26:21 -040088 Error *local_err = NULL;
Marc-André Lureaud91a7a52017-10-10 00:55:46 +020089 int i;
Stefan Bergerd1a0cf72013-02-27 12:47:49 -050090
91 if (!QLIST_EMPTY(&tpm_backends)) {
Markus Armbrusterd10e05f2018-10-17 10:26:53 +020092 error_setg(errp, "Only one TPM is allowed.");
Stefan Bergerd1a0cf72013-02-27 12:47:49 -050093 return 1;
94 }
95
96 id = qemu_opts_id(opts);
97 if (id == NULL) {
Markus Armbrusterd10e05f2018-10-17 10:26:53 +020098 error_setg(errp, QERR_MISSING_PARAMETER, "id");
Stefan Bergerd1a0cf72013-02-27 12:47:49 -050099 return 1;
100 }
101
102 value = qemu_opt_get(opts, "type");
103 if (!value) {
Markus Armbrusterd10e05f2018-10-17 10:26:53 +0200104 error_setg(errp, QERR_MISSING_PARAMETER, "type");
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500105 tpm_display_backend_drivers();
106 return 1;
107 }
108
Marc-André Lureaud91a7a52017-10-10 00:55:46 +0200109 i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL);
Marc-André Lureaud31076b2017-10-10 00:55:49 +0200110 be = i >= 0 ? tpm_be_find_by_type(i) : NULL;
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500111 if (be == NULL) {
Markus Armbrusterd10e05f2018-10-17 10:26:53 +0200112 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
113 "a TPM backend type");
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500114 tpm_display_backend_drivers();
115 return 1;
116 }
117
Stefan Bergerbb716232013-04-22 10:41:39 -0400118 /* validate backend specific opts */
119 qemu_opts_validate(opts, be->opts, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100120 if (local_err) {
Markus Armbrusterd10e05f2018-10-17 10:26:53 +0200121 error_propagate(errp, local_err);
Stefan Bergerbb716232013-04-22 10:41:39 -0400122 return 1;
123 }
124
Marc-André Lureau9f7c0ef2017-11-06 19:39:09 +0100125 drv = be->create(opts);
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500126 if (!drv) {
127 return 1;
128 }
129
Marc-André Lureau9f7c0ef2017-11-06 19:39:09 +0100130 drv->id = g_strdup(id);
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500131 QLIST_INSERT_HEAD(&tpm_backends, drv, list);
132
133 return 0;
134}
135
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500136/*
137 * Walk the list of TPM backend drivers that are in use and call their
138 * destroy function to have them cleaned up.
139 */
140void tpm_cleanup(void)
141{
142 TPMBackend *drv, *next;
143
144 QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
145 QLIST_REMOVE(drv, list);
Amarnath Vallurif35fe5c2017-09-29 14:10:15 +0300146 object_unref(OBJECT(drv));
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500147 }
148}
149
150/*
151 * Initialize the TPM. Process the tpmdev command line options describing the
152 * TPM backend.
153 */
Markus Armbrusterd10e05f2018-10-17 10:26:53 +0200154void tpm_init(void)
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500155{
Markus Armbrusterd10e05f2018-10-17 10:26:53 +0200156 qemu_opts_foreach(qemu_find_opts("tpmdev"),
157 tpm_init_tpmdev, NULL, &error_fatal);
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500158}
159
160/*
161 * Parse the TPM configuration options.
162 * To display all available TPM backends the user may use '-tpmdev help'
163 */
164int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
165{
166 QemuOpts *opts;
167
168 if (!strcmp(optarg, "help")) {
169 tpm_display_backend_drivers();
170 return -1;
171 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100172 opts = qemu_opts_parse_noisily(opts_list, optarg, true);
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500173 if (!opts) {
174 return -1;
175 }
176 return 0;
177}
178
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500179/*
Markus Armbrustereb815e22018-02-11 10:36:05 +0100180 * Walk the list of active TPM backends and collect information about them.
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500181 */
182TPMInfoList *qmp_query_tpm(Error **errp)
183{
184 TPMBackend *drv;
185 TPMInfoList *info, *head = NULL, *cur_item = NULL;
186
187 QLIST_FOREACH(drv, &tpm_backends, list) {
Marc-André Lureau191adc92017-11-06 19:39:04 +0100188 if (!drv->tpmif) {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500189 continue;
190 }
Marc-André Lureau191adc92017-11-06 19:39:04 +0100191
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500192 info = g_new0(TPMInfoList, 1);
Amarnath Vallurif59864b2017-09-29 14:10:17 +0300193 info->value = tpm_backend_query_tpm(drv);
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500194
195 if (!cur_item) {
196 head = cur_item = info;
197 } else {
198 cur_item->next = info;
199 cur_item = info;
200 }
201 }
202
203 return head;
204}
205
206TpmTypeList *qmp_query_tpm_types(Error **errp)
207{
208 unsigned int i = 0;
209 TpmTypeList *head = NULL, *prev = NULL, *cur_item;
210
Eric Blake7fb1cf12015-11-18 01:52:57 -0700211 for (i = 0; i < TPM_TYPE__MAX; i++) {
Marc-André Lureaud31076b2017-10-10 00:55:49 +0200212 if (!tpm_be_find_by_type(i)) {
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500213 continue;
214 }
215 cur_item = g_new0(TpmTypeList, 1);
216 cur_item->value = i;
217
218 if (prev) {
219 prev->next = cur_item;
220 }
221 if (!head) {
222 head = cur_item;
223 }
224 prev = cur_item;
225 }
226
227 return head;
228}
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500229TpmModelList *qmp_query_tpm_models(Error **errp)
230{
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500231 TpmModelList *head = NULL, *prev = NULL, *cur_item;
Marc-André Lureaud3fd9532017-11-06 19:39:25 +0100232 GSList *e, *l = object_class_get_list(TYPE_TPM_IF, false);
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500233
Marc-André Lureaud3fd9532017-11-06 19:39:25 +0100234 for (e = l; e; e = e->next) {
235 TPMIfClass *c = TPM_IF_CLASS(e->data);
236
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500237 cur_item = g_new0(TpmModelList, 1);
Marc-André Lureaud3fd9532017-11-06 19:39:25 +0100238 cur_item->value = c->model;
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500239
240 if (prev) {
241 prev->next = cur_item;
242 }
243 if (!head) {
244 head = cur_item;
245 }
246 prev = cur_item;
247 }
Marc-André Lureaud3fd9532017-11-06 19:39:25 +0100248 g_slist_free(l);
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500249
250 return head;
251}