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