libqos: allow zero-size allocations
[qemu/ar7.git] / tpm.c
blob0a3e3d503238b9121d12f7fa1eb9a2ce709bf50a
1 /*
2 * TPM configuration
4 * Copyright (C) 2011-2013 IBM Corporation
6 * Authors:
7 * Stefan Berger <stefanb@us.ibm.com>
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.
12 * Based on net.c
14 #include "config-host.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 "qemu/error-report.h"
21 #include "qmp-commands.h"
23 static QLIST_HEAD(, TPMBackend) tpm_backends =
24 QLIST_HEAD_INITIALIZER(tpm_backends);
27 #define TPM_MAX_MODELS 1
28 #define TPM_MAX_DRIVERS 1
30 static TPMDriverOps const *be_drivers[TPM_MAX_DRIVERS] = {
31 NULL,
34 static enum TpmModel tpm_models[TPM_MAX_MODELS] = {
35 TPM_MODEL__MAX,
38 int tpm_register_model(enum TpmModel model)
40 int i;
42 for (i = 0; i < TPM_MAX_MODELS; i++) {
43 if (tpm_models[i] == TPM_MODEL__MAX) {
44 tpm_models[i] = model;
45 return 0;
48 error_report("Could not register TPM model");
49 return 1;
52 static bool tpm_model_is_registered(enum TpmModel model)
54 int i;
56 for (i = 0; i < TPM_MAX_MODELS; i++) {
57 if (tpm_models[i] == model) {
58 return true;
61 return false;
64 const TPMDriverOps *tpm_get_backend_driver(const char *type)
66 int i;
68 for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
69 if (!strcmp(TpmType_lookup[be_drivers[i]->type], type)) {
70 return be_drivers[i];
74 return NULL;
77 #ifdef CONFIG_TPM
79 int tpm_register_driver(const TPMDriverOps *tdo)
81 int i;
83 for (i = 0; i < TPM_MAX_DRIVERS; i++) {
84 if (!be_drivers[i]) {
85 be_drivers[i] = tdo;
86 return 0;
89 error_report("Could not register TPM driver");
90 return 1;
94 * Walk the list of available TPM backend drivers and display them on the
95 * screen.
97 static void tpm_display_backend_drivers(void)
99 int i;
101 fprintf(stderr, "Supported TPM types (choose only one):\n");
103 for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
104 fprintf(stderr, "%12s %s\n",
105 TpmType_lookup[be_drivers[i]->type], be_drivers[i]->desc());
107 fprintf(stderr, "\n");
111 * Find the TPM with the given Id
113 TPMBackend *qemu_find_tpm(const char *id)
115 TPMBackend *drv;
117 if (id) {
118 QLIST_FOREACH(drv, &tpm_backends, list) {
119 if (!strcmp(drv->id, id)) {
120 return drv;
125 return NULL;
128 static int configure_tpm(QemuOpts *opts)
130 const char *value;
131 const char *id;
132 const TPMDriverOps *be;
133 TPMBackend *drv;
134 Error *local_err = NULL;
136 if (!QLIST_EMPTY(&tpm_backends)) {
137 error_report("Only one TPM is allowed.");
138 return 1;
141 id = qemu_opts_id(opts);
142 if (id == NULL) {
143 error_report(QERR_MISSING_PARAMETER, "id");
144 return 1;
147 value = qemu_opt_get(opts, "type");
148 if (!value) {
149 error_report(QERR_MISSING_PARAMETER, "type");
150 tpm_display_backend_drivers();
151 return 1;
154 be = tpm_get_backend_driver(value);
155 if (be == NULL) {
156 error_report(QERR_INVALID_PARAMETER_VALUE,
157 "type", "a TPM backend type");
158 tpm_display_backend_drivers();
159 return 1;
162 /* validate backend specific opts */
163 qemu_opts_validate(opts, be->opts, &local_err);
164 if (local_err) {
165 error_report_err(local_err);
166 return 1;
169 drv = be->create(opts, id);
170 if (!drv) {
171 return 1;
174 tpm_backend_open(drv, &local_err);
175 if (local_err) {
176 error_report_err(local_err);
177 return 1;
180 QLIST_INSERT_HEAD(&tpm_backends, drv, list);
182 return 0;
185 static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
187 return configure_tpm(opts);
191 * Walk the list of TPM backend drivers that are in use and call their
192 * destroy function to have them cleaned up.
194 void tpm_cleanup(void)
196 TPMBackend *drv, *next;
198 QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
199 QLIST_REMOVE(drv, list);
200 tpm_backend_destroy(drv);
205 * Initialize the TPM. Process the tpmdev command line options describing the
206 * TPM backend.
208 int tpm_init(void)
210 if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
211 tpm_init_tpmdev, NULL, NULL)) {
212 return -1;
215 atexit(tpm_cleanup);
216 return 0;
220 * Parse the TPM configuration options.
221 * To display all available TPM backends the user may use '-tpmdev help'
223 int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
225 QemuOpts *opts;
227 if (!strcmp(optarg, "help")) {
228 tpm_display_backend_drivers();
229 return -1;
231 opts = qemu_opts_parse_noisily(opts_list, optarg, true);
232 if (!opts) {
233 return -1;
235 return 0;
238 #endif /* CONFIG_TPM */
240 static const TPMDriverOps *tpm_driver_find_by_type(enum TpmType type)
242 int i;
244 for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
245 if (be_drivers[i]->type == type) {
246 return be_drivers[i];
249 return NULL;
252 static TPMInfo *qmp_query_tpm_inst(TPMBackend *drv)
254 TPMInfo *res = g_new0(TPMInfo, 1);
255 TPMPassthroughOptions *tpo;
257 res->id = g_strdup(drv->id);
258 res->model = drv->fe_model;
259 res->options = g_new0(TpmTypeOptions, 1);
261 switch (drv->ops->type) {
262 case TPM_TYPE_PASSTHROUGH:
263 res->options->type = TPM_TYPE_OPTIONS_KIND_PASSTHROUGH;
264 tpo = g_new0(TPMPassthroughOptions, 1);
265 res->options->u.passthrough = tpo;
266 if (drv->path) {
267 tpo->path = g_strdup(drv->path);
268 tpo->has_path = true;
270 if (drv->cancel_path) {
271 tpo->cancel_path = g_strdup(drv->cancel_path);
272 tpo->has_cancel_path = true;
274 break;
275 case TPM_TYPE__MAX:
276 break;
279 return res;
283 * Walk the list of active TPM backends and collect information about them
284 * following the schema description in qapi-schema.json.
286 TPMInfoList *qmp_query_tpm(Error **errp)
288 TPMBackend *drv;
289 TPMInfoList *info, *head = NULL, *cur_item = NULL;
291 QLIST_FOREACH(drv, &tpm_backends, list) {
292 if (!tpm_model_is_registered(drv->fe_model)) {
293 continue;
295 info = g_new0(TPMInfoList, 1);
296 info->value = qmp_query_tpm_inst(drv);
298 if (!cur_item) {
299 head = cur_item = info;
300 } else {
301 cur_item->next = info;
302 cur_item = info;
306 return head;
309 TpmTypeList *qmp_query_tpm_types(Error **errp)
311 unsigned int i = 0;
312 TpmTypeList *head = NULL, *prev = NULL, *cur_item;
314 for (i = 0; i < TPM_TYPE__MAX; i++) {
315 if (!tpm_driver_find_by_type(i)) {
316 continue;
318 cur_item = g_new0(TpmTypeList, 1);
319 cur_item->value = i;
321 if (prev) {
322 prev->next = cur_item;
324 if (!head) {
325 head = cur_item;
327 prev = cur_item;
330 return head;
333 TpmModelList *qmp_query_tpm_models(Error **errp)
335 unsigned int i = 0;
336 TpmModelList *head = NULL, *prev = NULL, *cur_item;
338 for (i = 0; i < TPM_MODEL__MAX; i++) {
339 if (!tpm_model_is_registered(i)) {
340 continue;
342 cur_item = g_new0(TpmModelList, 1);
343 cur_item->value = i;
345 if (prev) {
346 prev->next = cur_item;
348 if (!head) {
349 head = cur_item;
351 prev = cur_item;
354 return head;