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