target/arm: Add SVE state to TB->FLAGS
[qemu/ar7.git] / tpm.c
blob61a434185abd69cffd6de2be80e21722ea78e621
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 "qemu/osdep.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);
26 static const TPMBackendClass *
27 tpm_be_find_by_type(enum TpmType type)
29 ObjectClass *oc;
30 char *typename = g_strdup_printf("tpm-%s", TpmType_str(type));
32 oc = object_class_by_name(typename);
33 g_free(typename);
35 if (!object_class_dynamic_cast(oc, TYPE_TPM_BACKEND)) {
36 return NULL;
39 return TPM_BACKEND_CLASS(oc);
43 * Walk the list of available TPM backend drivers and display them on the
44 * screen.
46 static void tpm_display_backend_drivers(void)
48 int i;
50 fprintf(stderr, "Supported TPM types (choose only one):\n");
52 for (i = 0; i < TPM_TYPE__MAX; i++) {
53 const TPMBackendClass *bc = tpm_be_find_by_type(i);
54 if (!bc) {
55 continue;
57 fprintf(stderr, "%12s %s\n", TpmType_str(i), bc->desc);
59 fprintf(stderr, "\n");
63 * Find the TPM with the given Id
65 TPMBackend *qemu_find_tpm_be(const char *id)
67 TPMBackend *drv;
69 if (id) {
70 QLIST_FOREACH(drv, &tpm_backends, list) {
71 if (!strcmp(drv->id, id)) {
72 return drv;
77 return NULL;
80 static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
82 const char *value;
83 const char *id;
84 const TPMBackendClass *be;
85 TPMBackend *drv;
86 Error *local_err = NULL;
87 int i;
89 if (!QLIST_EMPTY(&tpm_backends)) {
90 error_report("Only one TPM is allowed.");
91 return 1;
94 id = qemu_opts_id(opts);
95 if (id == NULL) {
96 error_report(QERR_MISSING_PARAMETER, "id");
97 return 1;
100 value = qemu_opt_get(opts, "type");
101 if (!value) {
102 error_report(QERR_MISSING_PARAMETER, "type");
103 tpm_display_backend_drivers();
104 return 1;
107 i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL);
108 be = i >= 0 ? tpm_be_find_by_type(i) : NULL;
109 if (be == NULL) {
110 error_report(QERR_INVALID_PARAMETER_VALUE,
111 "type", "a TPM backend type");
112 tpm_display_backend_drivers();
113 return 1;
116 /* validate backend specific opts */
117 qemu_opts_validate(opts, be->opts, &local_err);
118 if (local_err) {
119 error_report_err(local_err);
120 return 1;
123 drv = be->create(opts);
124 if (!drv) {
125 return 1;
128 drv->id = g_strdup(id);
129 QLIST_INSERT_HEAD(&tpm_backends, drv, list);
131 return 0;
135 * Walk the list of TPM backend drivers that are in use and call their
136 * destroy function to have them cleaned up.
138 void tpm_cleanup(void)
140 TPMBackend *drv, *next;
142 QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
143 QLIST_REMOVE(drv, list);
144 object_unref(OBJECT(drv));
149 * Initialize the TPM. Process the tpmdev command line options describing the
150 * TPM backend.
152 int tpm_init(void)
154 if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
155 tpm_init_tpmdev, NULL, NULL)) {
156 return -1;
159 return 0;
163 * Parse the TPM configuration options.
164 * To display all available TPM backends the user may use '-tpmdev help'
166 int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
168 QemuOpts *opts;
170 if (!strcmp(optarg, "help")) {
171 tpm_display_backend_drivers();
172 return -1;
174 opts = qemu_opts_parse_noisily(opts_list, optarg, true);
175 if (!opts) {
176 return -1;
178 return 0;
182 * Walk the list of active TPM backends and collect information about them
183 * following the schema description in qapi-schema.json.
185 TPMInfoList *qmp_query_tpm(Error **errp)
187 TPMBackend *drv;
188 TPMInfoList *info, *head = NULL, *cur_item = NULL;
190 QLIST_FOREACH(drv, &tpm_backends, list) {
191 if (!drv->tpmif) {
192 continue;
195 info = g_new0(TPMInfoList, 1);
196 info->value = tpm_backend_query_tpm(drv);
198 if (!cur_item) {
199 head = cur_item = info;
200 } else {
201 cur_item->next = info;
202 cur_item = info;
206 return head;
209 TpmTypeList *qmp_query_tpm_types(Error **errp)
211 unsigned int i = 0;
212 TpmTypeList *head = NULL, *prev = NULL, *cur_item;
214 for (i = 0; i < TPM_TYPE__MAX; i++) {
215 if (!tpm_be_find_by_type(i)) {
216 continue;
218 cur_item = g_new0(TpmTypeList, 1);
219 cur_item->value = i;
221 if (prev) {
222 prev->next = cur_item;
224 if (!head) {
225 head = cur_item;
227 prev = cur_item;
230 return head;
232 TpmModelList *qmp_query_tpm_models(Error **errp)
234 TpmModelList *head = NULL, *prev = NULL, *cur_item;
235 GSList *e, *l = object_class_get_list(TYPE_TPM_IF, false);
237 for (e = l; e; e = e->next) {
238 TPMIfClass *c = TPM_IF_CLASS(e->data);
240 cur_item = g_new0(TpmModelList, 1);
241 cur_item->value = c->model;
243 if (prev) {
244 prev->next = cur_item;
246 if (!head) {
247 head = cur_item;
249 prev = cur_item;
251 g_slist_free(l);
253 return head;