]> git.proxmox.com Git - mirror_qemu.git/blame - tpm/tpm.c
qemu-char: rewrite io_channel_send_all and drop the '_all' suffix
[mirror_qemu.git] / tpm / 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 */
14#include "config-host.h"
15
16#include "monitor/monitor.h"
17#include "qapi/qmp/qerror.h"
18#include "tpm_int.h"
19#include "tpm/tpm.h"
20#include "qemu/config-file.h"
21#include "qmp-commands.h"
22
23static QLIST_HEAD(, TPMBackend) tpm_backends =
24 QLIST_HEAD_INITIALIZER(tpm_backends);
25
26
27#define TPM_MAX_MODELS 1
28#define TPM_MAX_DRIVERS 1
29
30static TPMDriverOps const *be_drivers[TPM_MAX_DRIVERS] = {
31 NULL,
32};
33
34static enum TpmModel tpm_models[TPM_MAX_MODELS] = {
35 -1,
36};
37
38int tpm_register_model(enum TpmModel model)
39{
40 int i;
41
42 for (i = 0; i < TPM_MAX_MODELS; i++) {
43 if (tpm_models[i] == -1) {
44 tpm_models[i] = model;
45 return 0;
46 }
47 }
48 error_report("Could not register TPM model");
49 return 1;
50}
51
52static bool tpm_model_is_registered(enum TpmModel model)
53{
54 int i;
55
56 for (i = 0; i < TPM_MAX_MODELS; i++) {
57 if (tpm_models[i] == model) {
58 return true;
59 }
60 }
61 return false;
62}
4549a8b7
SB
63
64/*
65 * Write an error message in the given output buffer.
66 */
67void tpm_write_fatal_error_response(uint8_t *out, uint32_t out_len)
68{
69 if (out_len >= sizeof(struct tpm_resp_hdr)) {
70 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)out;
71
72 resp->tag = cpu_to_be16(TPM_TAG_RSP_COMMAND);
73 resp->len = cpu_to_be32(sizeof(struct tpm_resp_hdr));
74 resp->errcode = cpu_to_be32(TPM_FAIL);
75 }
76}
d1a0cf73
SB
77
78const TPMDriverOps *tpm_get_backend_driver(const char *type)
79{
80 int i;
81
82 for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
83 if (!strcmp(TpmType_lookup[be_drivers[i]->type], type)) {
84 return be_drivers[i];
85 }
86 }
87
88 return NULL;
89}
90
91#ifdef CONFIG_TPM
92
93int tpm_register_driver(const TPMDriverOps *tdo)
94{
95 int i;
96
97 for (i = 0; i < TPM_MAX_DRIVERS; i++) {
98 if (!be_drivers[i]) {
99 be_drivers[i] = tdo;
100 return 0;
101 }
102 }
103 error_report("Could not register TPM driver");
104 return 1;
105}
106
107/*
108 * Walk the list of available TPM backend drivers and display them on the
109 * screen.
110 */
111void tpm_display_backend_drivers(void)
112{
113 int i;
114
115 fprintf(stderr, "Supported TPM types (choose only one):\n");
116
117 for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
118 fprintf(stderr, "%12s %s\n",
119 TpmType_lookup[be_drivers[i]->type], be_drivers[i]->desc());
120 }
121 fprintf(stderr, "\n");
122}
123
124/*
125 * Find the TPM with the given Id
126 */
127TPMBackend *qemu_find_tpm(const char *id)
128{
129 TPMBackend *drv;
130
131 if (id) {
132 QLIST_FOREACH(drv, &tpm_backends, list) {
133 if (!strcmp(drv->id, id)) {
134 return drv;
135 }
136 }
137 }
138
139 return NULL;
140}
141
142static int configure_tpm(QemuOpts *opts)
143{
144 const char *value;
145 const char *id;
146 const TPMDriverOps *be;
147 TPMBackend *drv;
148
149 if (!QLIST_EMPTY(&tpm_backends)) {
150 error_report("Only one TPM is allowed.\n");
151 return 1;
152 }
153
154 id = qemu_opts_id(opts);
155 if (id == NULL) {
156 qerror_report(QERR_MISSING_PARAMETER, "id");
157 return 1;
158 }
159
160 value = qemu_opt_get(opts, "type");
161 if (!value) {
162 qerror_report(QERR_MISSING_PARAMETER, "type");
163 tpm_display_backend_drivers();
164 return 1;
165 }
166
167 be = tpm_get_backend_driver(value);
168 if (be == NULL) {
169 qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
170 "a TPM backend type");
171 tpm_display_backend_drivers();
172 return 1;
173 }
174
175 drv = be->create(opts, id);
176 if (!drv) {
177 return 1;
178 }
179
180 QLIST_INSERT_HEAD(&tpm_backends, drv, list);
181
182 return 0;
183}
184
185static int tpm_init_tpmdev(QemuOpts *opts, void *dummy)
186{
187 return configure_tpm(opts);
188}
189
190/*
191 * Walk the list of TPM backend drivers that are in use and call their
192 * destroy function to have them cleaned up.
193 */
194void tpm_cleanup(void)
195{
196 TPMBackend *drv, *next;
197
198 QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
199 QLIST_REMOVE(drv, list);
200 drv->ops->destroy(drv);
201 }
202}
203
204/*
205 * Initialize the TPM. Process the tpmdev command line options describing the
206 * TPM backend.
207 */
208int tpm_init(void)
209{
210 if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
211 tpm_init_tpmdev, NULL, 1) != 0) {
212 return -1;
213 }
214
215 atexit(tpm_cleanup);
216
217 return 0;
218}
219
220/*
221 * Parse the TPM configuration options.
222 * To display all available TPM backends the user may use '-tpmdev help'
223 */
224int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
225{
226 QemuOpts *opts;
227
228 if (!strcmp(optarg, "help")) {
229 tpm_display_backend_drivers();
230 return -1;
231 }
232 opts = qemu_opts_parse(opts_list, optarg, 1);
233 if (!opts) {
234 return -1;
235 }
236 return 0;
237}
238
239#endif /* CONFIG_TPM */
240
241static const TPMDriverOps *tpm_driver_find_by_type(enum TpmType type)
242{
243 int i;
244
245 for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
246 if (be_drivers[i]->type == type) {
247 return be_drivers[i];
248 }
249 }
250 return NULL;
251}
252
253static TPMInfo *qmp_query_tpm_inst(TPMBackend *drv)
254{
255 TPMInfo *res = g_new0(TPMInfo, 1);
256 TPMPassthroughOptions *tpo;
257
258 res->id = g_strdup(drv->id);
259 res->model = drv->fe_model;
88ca7bcf 260 res->options = g_new0(TpmTypeOptions, 1);
d1a0cf73 261
88ca7bcf 262 switch (drv->ops->type) {
d1a0cf73 263 case TPM_TYPE_PASSTHROUGH:
88ca7bcf 264 res->options->kind = TPM_TYPE_OPTIONS_KIND_PASSTHROUGH;
d1a0cf73 265 tpo = g_new0(TPMPassthroughOptions, 1);
88ca7bcf 266 res->options->passthrough = tpo;
d1a0cf73
SB
267 if (drv->path) {
268 tpo->path = g_strdup(drv->path);
269 tpo->has_path = true;
270 }
271 if (drv->cancel_path) {
272 tpo->cancel_path = g_strdup(drv->cancel_path);
273 tpo->has_cancel_path = true;
274 }
275 break;
276 case TPM_TYPE_MAX:
277 break;
278 }
279
280 return res;
281}
282
283/*
284 * Walk the list of active TPM backends and collect information about them
285 * following the schema description in qapi-schema.json.
286 */
287TPMInfoList *qmp_query_tpm(Error **errp)
288{
289 TPMBackend *drv;
290 TPMInfoList *info, *head = NULL, *cur_item = NULL;
291
292 QLIST_FOREACH(drv, &tpm_backends, list) {
293 if (!tpm_model_is_registered(drv->fe_model)) {
294 continue;
295 }
296 info = g_new0(TPMInfoList, 1);
297 info->value = qmp_query_tpm_inst(drv);
298
299 if (!cur_item) {
300 head = cur_item = info;
301 } else {
302 cur_item->next = info;
303 cur_item = info;
304 }
305 }
306
307 return head;
308}
309
310TpmTypeList *qmp_query_tpm_types(Error **errp)
311{
312 unsigned int i = 0;
313 TpmTypeList *head = NULL, *prev = NULL, *cur_item;
314
315 for (i = 0; i < TPM_TYPE_MAX; i++) {
316 if (!tpm_driver_find_by_type(i)) {
317 continue;
318 }
319 cur_item = g_new0(TpmTypeList, 1);
320 cur_item->value = i;
321
322 if (prev) {
323 prev->next = cur_item;
324 }
325 if (!head) {
326 head = cur_item;
327 }
328 prev = cur_item;
329 }
330
331 return head;
332}
333
334TpmModelList *qmp_query_tpm_models(Error **errp)
335{
336 unsigned int i = 0;
337 TpmModelList *head = NULL, *prev = NULL, *cur_item;
338
339 for (i = 0; i < TPM_MODEL_MAX; i++) {
340 if (!tpm_model_is_registered(i)) {
341 continue;
342 }
343 cur_item = g_new0(TpmModelList, 1);
344 cur_item->value = i;
345
346 if (prev) {
347 prev->next = cur_item;
348 }
349 if (!head) {
350 head = cur_item;
351 }
352 prev = cur_item;
353 }
354
355 return head;
356}