virtio-blk-ccw switch to new API.
[qemu/agraf.git] / tpm / tpm.c
blobffd24956d775ba778ea43dfc4ce737330167763a
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 "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"
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 -1,
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] == -1) {
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;
65 * Write an error message in the given output buffer.
67 void tpm_write_fatal_error_response(uint8_t *out, uint32_t out_len)
69 if (out_len >= sizeof(struct tpm_resp_hdr)) {
70 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)out;
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);
78 const TPMDriverOps *tpm_get_backend_driver(const char *type)
80 int i;
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];
88 return NULL;
91 #ifdef CONFIG_TPM
93 int tpm_register_driver(const TPMDriverOps *tdo)
95 int i;
97 for (i = 0; i < TPM_MAX_DRIVERS; i++) {
98 if (!be_drivers[i]) {
99 be_drivers[i] = tdo;
100 return 0;
103 error_report("Could not register TPM driver");
104 return 1;
108 * Walk the list of available TPM backend drivers and display them on the
109 * screen.
111 void tpm_display_backend_drivers(void)
113 int i;
115 fprintf(stderr, "Supported TPM types (choose only one):\n");
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());
121 fprintf(stderr, "\n");
125 * Find the TPM with the given Id
127 TPMBackend *qemu_find_tpm(const char *id)
129 TPMBackend *drv;
131 if (id) {
132 QLIST_FOREACH(drv, &tpm_backends, list) {
133 if (!strcmp(drv->id, id)) {
134 return drv;
139 return NULL;
142 static int configure_tpm(QemuOpts *opts)
144 const char *value;
145 const char *id;
146 const TPMDriverOps *be;
147 TPMBackend *drv;
149 if (!QLIST_EMPTY(&tpm_backends)) {
150 error_report("Only one TPM is allowed.\n");
151 return 1;
154 id = qemu_opts_id(opts);
155 if (id == NULL) {
156 qerror_report(QERR_MISSING_PARAMETER, "id");
157 return 1;
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;
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;
175 drv = be->create(opts, id);
176 if (!drv) {
177 return 1;
180 QLIST_INSERT_HEAD(&tpm_backends, drv, list);
182 return 0;
185 static int tpm_init_tpmdev(QemuOpts *opts, void *dummy)
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 drv->ops->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, 1) != 0) {
212 return -1;
215 atexit(tpm_cleanup);
217 return 0;
221 * Parse the TPM configuration options.
222 * To display all available TPM backends the user may use '-tpmdev help'
224 int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
226 QemuOpts *opts;
228 if (!strcmp(optarg, "help")) {
229 tpm_display_backend_drivers();
230 return -1;
232 opts = qemu_opts_parse(opts_list, optarg, 1);
233 if (!opts) {
234 return -1;
236 return 0;
239 #endif /* CONFIG_TPM */
241 static const TPMDriverOps *tpm_driver_find_by_type(enum TpmType type)
243 int i;
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];
250 return NULL;
253 static TPMInfo *qmp_query_tpm_inst(TPMBackend *drv)
255 TPMInfo *res = g_new0(TPMInfo, 1);
256 TPMPassthroughOptions *tpo;
258 res->id = g_strdup(drv->id);
259 res->model = drv->fe_model;
260 res->type = drv->ops->type;
261 res->tpm_options = g_new0(TpmTypeOptions, 1);
263 switch (res->type) {
264 case TPM_TYPE_PASSTHROUGH:
265 res->tpm_options->kind = TPM_TYPE_OPTIONS_KIND_TPM_PASSTHROUGH_OPTIONS;
266 tpo = g_new0(TPMPassthroughOptions, 1);
267 res->tpm_options->tpm_passthrough_options = tpo;
268 if (drv->path) {
269 tpo->path = g_strdup(drv->path);
270 tpo->has_path = true;
272 if (drv->cancel_path) {
273 tpo->cancel_path = g_strdup(drv->cancel_path);
274 tpo->has_cancel_path = true;
276 break;
277 case TPM_TYPE_MAX:
278 break;
281 return res;
285 * Walk the list of active TPM backends and collect information about them
286 * following the schema description in qapi-schema.json.
288 TPMInfoList *qmp_query_tpm(Error **errp)
290 TPMBackend *drv;
291 TPMInfoList *info, *head = NULL, *cur_item = NULL;
293 QLIST_FOREACH(drv, &tpm_backends, list) {
294 if (!tpm_model_is_registered(drv->fe_model)) {
295 continue;
297 info = g_new0(TPMInfoList, 1);
298 info->value = qmp_query_tpm_inst(drv);
300 if (!cur_item) {
301 head = cur_item = info;
302 } else {
303 cur_item->next = info;
304 cur_item = info;
308 return head;
311 TpmTypeList *qmp_query_tpm_types(Error **errp)
313 unsigned int i = 0;
314 TpmTypeList *head = NULL, *prev = NULL, *cur_item;
316 for (i = 0; i < TPM_TYPE_MAX; i++) {
317 if (!tpm_driver_find_by_type(i)) {
318 continue;
320 cur_item = g_new0(TpmTypeList, 1);
321 cur_item->value = i;
323 if (prev) {
324 prev->next = cur_item;
326 if (!head) {
327 head = cur_item;
329 prev = cur_item;
332 return head;
335 TpmModelList *qmp_query_tpm_models(Error **errp)
337 unsigned int i = 0;
338 TpmModelList *head = NULL, *prev = NULL, *cur_item;
340 for (i = 0; i < TPM_MODEL_MAX; i++) {
341 if (!tpm_model_is_registered(i)) {
342 continue;
344 cur_item = g_new0(TpmModelList, 1);
345 cur_item->value = i;
347 if (prev) {
348 prev->next = cur_item;
350 if (!head) {
351 head = cur_item;
353 prev = cur_item;
356 return head;