xilinx_spips: Add support for the ZynqMP Generic QSPI
[qemu/ar7.git] / tpm.c
blobab5d29e91e89543939cba3d7868df140adfe63eb
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 bool tpm_models[TPM_MODEL__MAX];
28 void tpm_register_model(enum TpmModel model)
30 tpm_models[model] = true;
33 static const TPMBackendClass *
34 tpm_be_find_by_type(enum TpmType type)
36 ObjectClass *oc;
37 char *typename = g_strdup_printf("tpm-%s", TpmType_str(type));
39 oc = object_class_by_name(typename);
40 g_free(typename);
42 if (!object_class_dynamic_cast(oc, TYPE_TPM_BACKEND)) {
43 return NULL;
46 return TPM_BACKEND_CLASS(oc);
50 * Walk the list of available TPM backend drivers and display them on the
51 * screen.
53 static void tpm_display_backend_drivers(void)
55 int i;
57 fprintf(stderr, "Supported TPM types (choose only one):\n");
59 for (i = 0; i < TPM_TYPE__MAX; i++) {
60 const TPMBackendClass *bc = tpm_be_find_by_type(i);
61 if (!bc) {
62 continue;
64 fprintf(stderr, "%12s %s\n", TpmType_str(i), bc->desc);
66 fprintf(stderr, "\n");
70 * Find the TPM with the given Id
72 TPMBackend *qemu_find_tpm(const char *id)
74 TPMBackend *drv;
76 if (id) {
77 QLIST_FOREACH(drv, &tpm_backends, list) {
78 if (!strcmp(drv->id, id)) {
79 return drv;
84 return NULL;
87 static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
89 const char *value;
90 const char *id;
91 const TPMBackendClass *be;
92 TPMBackend *drv;
93 Error *local_err = NULL;
94 int i;
96 if (!QLIST_EMPTY(&tpm_backends)) {
97 error_report("Only one TPM is allowed.");
98 return 1;
101 id = qemu_opts_id(opts);
102 if (id == NULL) {
103 error_report(QERR_MISSING_PARAMETER, "id");
104 return 1;
107 value = qemu_opt_get(opts, "type");
108 if (!value) {
109 error_report(QERR_MISSING_PARAMETER, "type");
110 tpm_display_backend_drivers();
111 return 1;
114 i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL);
115 be = i >= 0 ? tpm_be_find_by_type(i) : NULL;
116 if (be == NULL) {
117 error_report(QERR_INVALID_PARAMETER_VALUE,
118 "type", "a TPM backend type");
119 tpm_display_backend_drivers();
120 return 1;
123 /* validate backend specific opts */
124 qemu_opts_validate(opts, be->opts, &local_err);
125 if (local_err) {
126 error_report_err(local_err);
127 return 1;
130 drv = be->create(opts, id);
131 if (!drv) {
132 return 1;
135 tpm_backend_open(drv, &local_err);
136 if (local_err) {
137 error_report_err(local_err);
138 return 1;
141 QLIST_INSERT_HEAD(&tpm_backends, drv, list);
143 return 0;
147 * Walk the list of TPM backend drivers that are in use and call their
148 * destroy function to have them cleaned up.
150 void tpm_cleanup(void)
152 TPMBackend *drv, *next;
154 QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
155 QLIST_REMOVE(drv, list);
156 object_unref(OBJECT(drv));
161 * Initialize the TPM. Process the tpmdev command line options describing the
162 * TPM backend.
164 int tpm_init(void)
166 if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
167 tpm_init_tpmdev, NULL, NULL)) {
168 return -1;
171 return 0;
175 * Parse the TPM configuration options.
176 * To display all available TPM backends the user may use '-tpmdev help'
178 int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
180 QemuOpts *opts;
182 if (!strcmp(optarg, "help")) {
183 tpm_display_backend_drivers();
184 return -1;
186 opts = qemu_opts_parse_noisily(opts_list, optarg, true);
187 if (!opts) {
188 return -1;
190 return 0;
194 * Walk the list of active TPM backends and collect information about them
195 * following the schema description in qapi-schema.json.
197 TPMInfoList *qmp_query_tpm(Error **errp)
199 TPMBackend *drv;
200 TPMInfoList *info, *head = NULL, *cur_item = NULL;
202 QLIST_FOREACH(drv, &tpm_backends, list) {
203 if (!tpm_models[drv->fe_model]) {
204 continue;
206 info = g_new0(TPMInfoList, 1);
207 info->value = tpm_backend_query_tpm(drv);
209 if (!cur_item) {
210 head = cur_item = info;
211 } else {
212 cur_item->next = info;
213 cur_item = info;
217 return head;
220 TpmTypeList *qmp_query_tpm_types(Error **errp)
222 unsigned int i = 0;
223 TpmTypeList *head = NULL, *prev = NULL, *cur_item;
225 for (i = 0; i < TPM_TYPE__MAX; i++) {
226 if (!tpm_be_find_by_type(i)) {
227 continue;
229 cur_item = g_new0(TpmTypeList, 1);
230 cur_item->value = i;
232 if (prev) {
233 prev->next = cur_item;
235 if (!head) {
236 head = cur_item;
238 prev = cur_item;
241 return head;
244 TpmModelList *qmp_query_tpm_models(Error **errp)
246 unsigned int i = 0;
247 TpmModelList *head = NULL, *prev = NULL, *cur_item;
249 for (i = 0; i < TPM_MODEL__MAX; i++) {
250 if (!tpm_models[i]) {
251 continue;
253 cur_item = g_new0(TpmModelList, 1);
254 cur_item->value = i;
256 if (prev) {
257 prev->next = cur_item;
259 if (!head) {
260 head = cur_item;
262 prev = cur_item;
265 return head;