Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 1 | /* |
| 2 | * TPM configuration |
| 3 | * |
| 4 | * Copyright (C) 2011-2013 IBM Corporation |
| 5 | * |
| 6 | * Authors: |
| 7 | * Stefan Berger <stefanb@us.ibm.com> |
| 8 | * |
| 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. |
| 11 | * |
| 12 | * Based on net.c |
| 13 | */ |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 15 | |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 16 | #include "qapi/qmp/qerror.h" |
Paolo Bonzini | dccfcd0 | 2013-04-08 16:55:25 +0200 | [diff] [blame] | 17 | #include "sysemu/tpm_backend.h" |
Paolo Bonzini | bdee56f | 2013-04-02 18:28:41 +0200 | [diff] [blame] | 18 | #include "sysemu/tpm.h" |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 19 | #include "qemu/config-file.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 20 | #include "qemu/error-report.h" |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 21 | #include "qmp-commands.h" |
| 22 | |
| 23 | static QLIST_HEAD(, TPMBackend) tpm_backends = |
| 24 | QLIST_HEAD_INITIALIZER(tpm_backends); |
| 25 | |
| 26 | |
| 27 | #define TPM_MAX_MODELS 1 |
| 28 | #define TPM_MAX_DRIVERS 1 |
| 29 | |
| 30 | static TPMDriverOps const *be_drivers[TPM_MAX_DRIVERS] = { |
| 31 | NULL, |
| 32 | }; |
| 33 | |
| 34 | static enum TpmModel tpm_models[TPM_MAX_MODELS] = { |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 35 | TPM_MODEL__MAX, |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | int tpm_register_model(enum TpmModel model) |
| 39 | { |
| 40 | int i; |
| 41 | |
| 42 | for (i = 0; i < TPM_MAX_MODELS; i++) { |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 43 | if (tpm_models[i] == TPM_MODEL__MAX) { |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 44 | tpm_models[i] = model; |
| 45 | return 0; |
| 46 | } |
| 47 | } |
| 48 | error_report("Could not register TPM model"); |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | static bool tpm_model_is_registered(enum TpmModel model) |
| 53 | { |
| 54 | int i; |
| 55 | |
| 56 | for (i = 0; i < TPM_MAX_MODELS; i++) { |
| 57 | if (tpm_models[i] == model) { |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 64 | const TPMDriverOps *tpm_get_backend_driver(const char *type) |
| 65 | { |
| 66 | int i; |
| 67 | |
| 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]; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | #ifdef CONFIG_TPM |
| 78 | |
| 79 | int tpm_register_driver(const TPMDriverOps *tdo) |
| 80 | { |
| 81 | int i; |
| 82 | |
| 83 | for (i = 0; i < TPM_MAX_DRIVERS; i++) { |
| 84 | if (!be_drivers[i]) { |
| 85 | be_drivers[i] = tdo; |
| 86 | return 0; |
| 87 | } |
| 88 | } |
| 89 | error_report("Could not register TPM driver"); |
| 90 | return 1; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Walk the list of available TPM backend drivers and display them on the |
| 95 | * screen. |
| 96 | */ |
Paolo Bonzini | bdee56f | 2013-04-02 18:28:41 +0200 | [diff] [blame] | 97 | static void tpm_display_backend_drivers(void) |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 98 | { |
| 99 | int i; |
| 100 | |
| 101 | fprintf(stderr, "Supported TPM types (choose only one):\n"); |
| 102 | |
| 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()); |
| 106 | } |
| 107 | fprintf(stderr, "\n"); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Find the TPM with the given Id |
| 112 | */ |
| 113 | TPMBackend *qemu_find_tpm(const char *id) |
| 114 | { |
| 115 | TPMBackend *drv; |
| 116 | |
| 117 | if (id) { |
| 118 | QLIST_FOREACH(drv, &tpm_backends, list) { |
| 119 | if (!strcmp(drv->id, id)) { |
| 120 | return drv; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | static int configure_tpm(QemuOpts *opts) |
| 129 | { |
| 130 | const char *value; |
| 131 | const char *id; |
| 132 | const TPMDriverOps *be; |
| 133 | TPMBackend *drv; |
Stefan Berger | 8f0605c | 2013-03-28 07:26:21 -0400 | [diff] [blame] | 134 | Error *local_err = NULL; |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 135 | |
| 136 | if (!QLIST_EMPTY(&tpm_backends)) { |
Gonglei | 27215a2 | 2015-02-25 12:22:35 +0800 | [diff] [blame] | 137 | error_report("Only one TPM is allowed."); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | id = qemu_opts_id(opts); |
| 142 | if (id == NULL) { |
Markus Armbruster | 8b53a19 | 2015-03-17 12:09:02 +0100 | [diff] [blame] | 143 | error_report(QERR_MISSING_PARAMETER, "id"); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | value = qemu_opt_get(opts, "type"); |
| 148 | if (!value) { |
Markus Armbruster | 8b53a19 | 2015-03-17 12:09:02 +0100 | [diff] [blame] | 149 | error_report(QERR_MISSING_PARAMETER, "type"); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 150 | tpm_display_backend_drivers(); |
| 151 | return 1; |
| 152 | } |
| 153 | |
| 154 | be = tpm_get_backend_driver(value); |
| 155 | if (be == NULL) { |
Markus Armbruster | 8b53a19 | 2015-03-17 12:09:02 +0100 | [diff] [blame] | 156 | error_report(QERR_INVALID_PARAMETER_VALUE, |
| 157 | "type", "a TPM backend type"); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 158 | tpm_display_backend_drivers(); |
| 159 | return 1; |
| 160 | } |
| 161 | |
Stefan Berger | bb71623 | 2013-04-22 10:41:39 -0400 | [diff] [blame] | 162 | /* validate backend specific opts */ |
| 163 | qemu_opts_validate(opts, be->opts, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 164 | if (local_err) { |
Markus Armbruster | bc09a28 | 2015-02-10 15:11:17 +0100 | [diff] [blame] | 165 | error_report_err(local_err); |
Stefan Berger | bb71623 | 2013-04-22 10:41:39 -0400 | [diff] [blame] | 166 | return 1; |
| 167 | } |
| 168 | |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 169 | drv = be->create(opts, id); |
| 170 | if (!drv) { |
| 171 | return 1; |
| 172 | } |
| 173 | |
Stefan Berger | 8f0605c | 2013-03-28 07:26:21 -0400 | [diff] [blame] | 174 | tpm_backend_open(drv, &local_err); |
| 175 | if (local_err) { |
Markus Armbruster | bc09a28 | 2015-02-10 15:11:17 +0100 | [diff] [blame] | 176 | error_report_err(local_err); |
Stefan Berger | 8f0605c | 2013-03-28 07:26:21 -0400 | [diff] [blame] | 177 | return 1; |
| 178 | } |
| 179 | |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 180 | QLIST_INSERT_HEAD(&tpm_backends, drv, list); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Markus Armbruster | 28d0de7 | 2015-03-13 13:35:14 +0100 | [diff] [blame] | 185 | static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp) |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 186 | { |
| 187 | return configure_tpm(opts); |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Walk the list of TPM backend drivers that are in use and call their |
| 192 | * destroy function to have them cleaned up. |
| 193 | */ |
| 194 | void tpm_cleanup(void) |
| 195 | { |
| 196 | TPMBackend *drv, *next; |
| 197 | |
| 198 | QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) { |
| 199 | QLIST_REMOVE(drv, list); |
Stefan Berger | 8f0605c | 2013-03-28 07:26:21 -0400 | [diff] [blame] | 200 | tpm_backend_destroy(drv); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Initialize the TPM. Process the tpmdev command line options describing the |
| 206 | * TPM backend. |
| 207 | */ |
| 208 | int tpm_init(void) |
| 209 | { |
Markus Armbruster | 28d0de7 | 2015-03-13 13:35:14 +0100 | [diff] [blame] | 210 | if (qemu_opts_foreach(qemu_find_opts("tpmdev"), |
| 211 | tpm_init_tpmdev, NULL, NULL)) { |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | atexit(tpm_cleanup); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Parse the TPM configuration options. |
| 221 | * To display all available TPM backends the user may use '-tpmdev help' |
| 222 | */ |
| 223 | int tpm_config_parse(QemuOptsList *opts_list, const char *optarg) |
| 224 | { |
| 225 | QemuOpts *opts; |
| 226 | |
| 227 | if (!strcmp(optarg, "help")) { |
| 228 | tpm_display_backend_drivers(); |
| 229 | return -1; |
| 230 | } |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 231 | opts = qemu_opts_parse_noisily(opts_list, optarg, true); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 232 | if (!opts) { |
| 233 | return -1; |
| 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | #endif /* CONFIG_TPM */ |
| 239 | |
| 240 | static const TPMDriverOps *tpm_driver_find_by_type(enum TpmType type) |
| 241 | { |
| 242 | int i; |
| 243 | |
| 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]; |
| 247 | } |
| 248 | } |
| 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | static TPMInfo *qmp_query_tpm_inst(TPMBackend *drv) |
| 253 | { |
| 254 | TPMInfo *res = g_new0(TPMInfo, 1); |
| 255 | TPMPassthroughOptions *tpo; |
| 256 | |
| 257 | res->id = g_strdup(drv->id); |
| 258 | res->model = drv->fe_model; |
Corey Bryant | 88ca7bc | 2013-03-20 12:34:48 -0400 | [diff] [blame] | 259 | res->options = g_new0(TpmTypeOptions, 1); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 260 | |
Corey Bryant | 88ca7bc | 2013-03-20 12:34:48 -0400 | [diff] [blame] | 261 | switch (drv->ops->type) { |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 262 | case TPM_TYPE_PASSTHROUGH: |
Eric Blake | ce21131 | 2015-10-26 16:35:00 -0600 | [diff] [blame] | 263 | res->options->type = TPM_TYPE_OPTIONS_KIND_PASSTHROUGH; |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 264 | tpo = g_new0(TPMPassthroughOptions, 1); |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 265 | res->options->u.passthrough.data = tpo; |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 266 | if (drv->path) { |
| 267 | tpo->path = g_strdup(drv->path); |
| 268 | tpo->has_path = true; |
| 269 | } |
| 270 | if (drv->cancel_path) { |
| 271 | tpo->cancel_path = g_strdup(drv->cancel_path); |
| 272 | tpo->has_cancel_path = true; |
| 273 | } |
| 274 | break; |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 275 | case TPM_TYPE__MAX: |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 276 | break; |
| 277 | } |
| 278 | |
| 279 | return res; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Walk the list of active TPM backends and collect information about them |
| 284 | * following the schema description in qapi-schema.json. |
| 285 | */ |
| 286 | TPMInfoList *qmp_query_tpm(Error **errp) |
| 287 | { |
| 288 | TPMBackend *drv; |
| 289 | TPMInfoList *info, *head = NULL, *cur_item = NULL; |
| 290 | |
| 291 | QLIST_FOREACH(drv, &tpm_backends, list) { |
| 292 | if (!tpm_model_is_registered(drv->fe_model)) { |
| 293 | continue; |
| 294 | } |
| 295 | info = g_new0(TPMInfoList, 1); |
| 296 | info->value = qmp_query_tpm_inst(drv); |
| 297 | |
| 298 | if (!cur_item) { |
| 299 | head = cur_item = info; |
| 300 | } else { |
| 301 | cur_item->next = info; |
| 302 | cur_item = info; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | return head; |
| 307 | } |
| 308 | |
| 309 | TpmTypeList *qmp_query_tpm_types(Error **errp) |
| 310 | { |
| 311 | unsigned int i = 0; |
| 312 | TpmTypeList *head = NULL, *prev = NULL, *cur_item; |
| 313 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 314 | for (i = 0; i < TPM_TYPE__MAX; i++) { |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 315 | if (!tpm_driver_find_by_type(i)) { |
| 316 | continue; |
| 317 | } |
| 318 | cur_item = g_new0(TpmTypeList, 1); |
| 319 | cur_item->value = i; |
| 320 | |
| 321 | if (prev) { |
| 322 | prev->next = cur_item; |
| 323 | } |
| 324 | if (!head) { |
| 325 | head = cur_item; |
| 326 | } |
| 327 | prev = cur_item; |
| 328 | } |
| 329 | |
| 330 | return head; |
| 331 | } |
| 332 | |
| 333 | TpmModelList *qmp_query_tpm_models(Error **errp) |
| 334 | { |
| 335 | unsigned int i = 0; |
| 336 | TpmModelList *head = NULL, *prev = NULL, *cur_item; |
| 337 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 338 | for (i = 0; i < TPM_MODEL__MAX; i++) { |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 339 | if (!tpm_model_is_registered(i)) { |
| 340 | continue; |
| 341 | } |
| 342 | cur_item = g_new0(TpmModelList, 1); |
| 343 | cur_item->value = i; |
| 344 | |
| 345 | if (prev) { |
| 346 | prev->next = cur_item; |
| 347 | } |
| 348 | if (!head) { |
| 349 | head = cur_item; |
| 350 | } |
| 351 | prev = cur_item; |
| 352 | } |
| 353 | |
| 354 | return head; |
| 355 | } |