hw/char: avr: Add limited support for USART peripheral
[qemu/ar7.git] / tpm.c
blobfe03b24858434b33c474c5905f5eea6cee2038b4
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
15 #include "qemu/osdep.h"
17 #include "qapi/error.h"
18 #include "qapi/qapi-commands-tpm.h"
19 #include "qapi/qmp/qerror.h"
20 #include "sysemu/tpm_backend.h"
21 #include "sysemu/tpm.h"
22 #include "qemu/config-file.h"
23 #include "qemu/error-report.h"
25 static QLIST_HEAD(, TPMBackend) tpm_backends =
26 QLIST_HEAD_INITIALIZER(tpm_backends);
28 static const TPMBackendClass *
29 tpm_be_find_by_type(enum TpmType type)
31 ObjectClass *oc;
32 char *typename = g_strdup_printf("tpm-%s", TpmType_str(type));
34 oc = object_class_by_name(typename);
35 g_free(typename);
37 if (!object_class_dynamic_cast(oc, TYPE_TPM_BACKEND)) {
38 return NULL;
41 return TPM_BACKEND_CLASS(oc);
45 * Walk the list of available TPM backend drivers and display them on the
46 * screen.
48 static void tpm_display_backend_drivers(void)
50 int i;
52 fprintf(stderr, "Supported TPM types (choose only one):\n");
54 for (i = 0; i < TPM_TYPE__MAX; i++) {
55 const TPMBackendClass *bc = tpm_be_find_by_type(i);
56 if (!bc) {
57 continue;
59 fprintf(stderr, "%12s %s\n", TpmType_str(i), bc->desc);
61 fprintf(stderr, "\n");
65 * Find the TPM with the given Id
67 TPMBackend *qemu_find_tpm_be(const char *id)
69 TPMBackend *drv;
71 if (id) {
72 QLIST_FOREACH(drv, &tpm_backends, list) {
73 if (!strcmp(drv->id, id)) {
74 return drv;
79 return NULL;
82 static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
84 const char *value;
85 const char *id;
86 const TPMBackendClass *be;
87 TPMBackend *drv;
88 int i;
90 if (!QLIST_EMPTY(&tpm_backends)) {
91 error_setg(errp, "Only one TPM is allowed.");
92 return 1;
95 id = qemu_opts_id(opts);
96 if (id == NULL) {
97 error_setg(errp, QERR_MISSING_PARAMETER, "id");
98 return 1;
101 value = qemu_opt_get(opts, "type");
102 if (!value) {
103 error_setg(errp, QERR_MISSING_PARAMETER, "type");
104 tpm_display_backend_drivers();
105 return 1;
108 i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL);
109 be = i >= 0 ? tpm_be_find_by_type(i) : NULL;
110 if (be == NULL) {
111 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
112 "a TPM backend type");
113 tpm_display_backend_drivers();
114 return 1;
117 /* validate backend specific opts */
118 if (!qemu_opts_validate(opts, be->opts, errp)) {
119 return 1;
122 drv = be->create(opts);
123 if (!drv) {
124 return 1;
127 drv->id = g_strdup(id);
128 QLIST_INSERT_HEAD(&tpm_backends, drv, list);
130 return 0;
134 * Walk the list of TPM backend drivers that are in use and call their
135 * destroy function to have them cleaned up.
137 void tpm_cleanup(void)
139 TPMBackend *drv, *next;
141 QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) {
142 QLIST_REMOVE(drv, list);
143 object_unref(OBJECT(drv));
148 * Initialize the TPM. Process the tpmdev command line options describing the
149 * TPM backend.
151 void tpm_init(void)
153 qemu_opts_foreach(qemu_find_opts("tpmdev"),
154 tpm_init_tpmdev, NULL, &error_fatal);
158 * Parse the TPM configuration options.
159 * To display all available TPM backends the user may use '-tpmdev help'
161 int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
163 QemuOpts *opts;
165 if (!strcmp(optarg, "help")) {
166 tpm_display_backend_drivers();
167 return -1;
169 opts = qemu_opts_parse_noisily(opts_list, optarg, true);
170 if (!opts) {
171 return -1;
173 return 0;
177 * Walk the list of active TPM backends and collect information about them.
179 TPMInfoList *qmp_query_tpm(Error **errp)
181 TPMBackend *drv;
182 TPMInfoList *info, *head = NULL, *cur_item = NULL;
184 QLIST_FOREACH(drv, &tpm_backends, list) {
185 if (!drv->tpmif) {
186 continue;
189 info = g_new0(TPMInfoList, 1);
190 info->value = tpm_backend_query_tpm(drv);
192 if (!cur_item) {
193 head = cur_item = info;
194 } else {
195 cur_item->next = info;
196 cur_item = info;
200 return head;
203 TpmTypeList *qmp_query_tpm_types(Error **errp)
205 unsigned int i = 0;
206 TpmTypeList *head = NULL, *prev = NULL, *cur_item;
208 for (i = 0; i < TPM_TYPE__MAX; i++) {
209 if (!tpm_be_find_by_type(i)) {
210 continue;
212 cur_item = g_new0(TpmTypeList, 1);
213 cur_item->value = i;
215 if (prev) {
216 prev->next = cur_item;
218 if (!head) {
219 head = cur_item;
221 prev = cur_item;
224 return head;
226 TpmModelList *qmp_query_tpm_models(Error **errp)
228 TpmModelList *head = NULL, *prev = NULL, *cur_item;
229 GSList *e, *l = object_class_get_list(TYPE_TPM_IF, false);
231 for (e = l; e; e = e->next) {
232 TPMIfClass *c = TPM_IF_CLASS(e->data);
234 cur_item = g_new0(TpmModelList, 1);
235 cur_item->value = c->model;
237 if (prev) {
238 prev->next = cur_item;
240 if (!head) {
241 head = cur_item;
243 prev = cur_item;
245 g_slist_free(l);
247 return head;