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 | */ |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 14 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 15 | #include "qemu/osdep.h" |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 16 | |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 17 | #include "qapi/error.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 18 | #include "qapi/qapi-commands-tpm.h" |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 19 | #include "qapi/qmp/qerror.h" |
Paolo Bonzini | dccfcd0 | 2013-04-08 16:55:25 +0200 | [diff] [blame] | 20 | #include "sysemu/tpm_backend.h" |
Paolo Bonzini | bdee56f | 2013-04-02 18:28:41 +0200 | [diff] [blame] | 21 | #include "sysemu/tpm.h" |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 22 | #include "qemu/config-file.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 23 | #include "qemu/error-report.h" |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 24 | |
| 25 | static QLIST_HEAD(, TPMBackend) tpm_backends = |
| 26 | QLIST_HEAD_INITIALIZER(tpm_backends); |
| 27 | |
Marc-André Lureau | d31076b | 2017-10-10 00:55:49 +0200 | [diff] [blame] | 28 | static const TPMBackendClass * |
| 29 | tpm_be_find_by_type(enum TpmType type) |
Marc-André Lureau | f3faa1d | 2017-10-10 00:55:45 +0200 | [diff] [blame] | 30 | { |
| 31 | ObjectClass *oc; |
Marc-André Lureau | f3faa1d | 2017-10-10 00:55:45 +0200 | [diff] [blame] | 32 | char *typename = g_strdup_printf("tpm-%s", TpmType_str(type)); |
| 33 | |
| 34 | oc = object_class_by_name(typename); |
| 35 | g_free(typename); |
| 36 | |
| 37 | if (!object_class_dynamic_cast(oc, TYPE_TPM_BACKEND)) { |
| 38 | return NULL; |
| 39 | } |
| 40 | |
Marc-André Lureau | d31076b | 2017-10-10 00:55:49 +0200 | [diff] [blame] | 41 | return TPM_BACKEND_CLASS(oc); |
Marc-André Lureau | f3faa1d | 2017-10-10 00:55:45 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 44 | /* |
| 45 | * Walk the list of available TPM backend drivers and display them on the |
| 46 | * screen. |
| 47 | */ |
Paolo Bonzini | bdee56f | 2013-04-02 18:28:41 +0200 | [diff] [blame] | 48 | static void tpm_display_backend_drivers(void) |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 49 | { |
| 50 | int i; |
| 51 | |
| 52 | fprintf(stderr, "Supported TPM types (choose only one):\n"); |
| 53 | |
Marc-André Lureau | a9a72ae | 2017-08-24 10:45:58 +0200 | [diff] [blame] | 54 | for (i = 0; i < TPM_TYPE__MAX; i++) { |
Marc-André Lureau | d31076b | 2017-10-10 00:55:49 +0200 | [diff] [blame] | 55 | const TPMBackendClass *bc = tpm_be_find_by_type(i); |
| 56 | if (!bc) { |
Marc-André Lureau | a9a72ae | 2017-08-24 10:45:58 +0200 | [diff] [blame] | 57 | continue; |
| 58 | } |
Marc-André Lureau | d31076b | 2017-10-10 00:55:49 +0200 | [diff] [blame] | 59 | fprintf(stderr, "%12s %s\n", TpmType_str(i), bc->desc); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 60 | } |
| 61 | fprintf(stderr, "\n"); |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Find the TPM with the given Id |
| 66 | */ |
Marc-André Lureau | d36e7db | 2017-11-06 19:39:14 +0100 | [diff] [blame] | 67 | TPMBackend *qemu_find_tpm_be(const char *id) |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 68 | { |
| 69 | TPMBackend *drv; |
| 70 | |
| 71 | if (id) { |
| 72 | QLIST_FOREACH(drv, &tpm_backends, list) { |
| 73 | if (!strcmp(drv->id, id)) { |
| 74 | return drv; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return NULL; |
| 80 | } |
| 81 | |
Marc-André Lureau | 76ae76b | 2017-10-10 00:55:51 +0200 | [diff] [blame] | 82 | static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp) |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 83 | { |
| 84 | const char *value; |
| 85 | const char *id; |
Marc-André Lureau | d31076b | 2017-10-10 00:55:49 +0200 | [diff] [blame] | 86 | const TPMBackendClass *be; |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 87 | TPMBackend *drv; |
Stefan Berger | 8f0605c | 2013-03-28 07:26:21 -0400 | [diff] [blame] | 88 | Error *local_err = NULL; |
Marc-André Lureau | d91a7a5 | 2017-10-10 00:55:46 +0200 | [diff] [blame] | 89 | int i; |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 90 | |
| 91 | if (!QLIST_EMPTY(&tpm_backends)) { |
Gonglei | 27215a2 | 2015-02-25 12:22:35 +0800 | [diff] [blame] | 92 | error_report("Only one TPM is allowed."); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | id = qemu_opts_id(opts); |
| 97 | if (id == NULL) { |
Markus Armbruster | 8b53a19 | 2015-03-17 12:09:02 +0100 | [diff] [blame] | 98 | error_report(QERR_MISSING_PARAMETER, "id"); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | value = qemu_opt_get(opts, "type"); |
| 103 | if (!value) { |
Markus Armbruster | 8b53a19 | 2015-03-17 12:09:02 +0100 | [diff] [blame] | 104 | error_report(QERR_MISSING_PARAMETER, "type"); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 105 | tpm_display_backend_drivers(); |
| 106 | return 1; |
| 107 | } |
| 108 | |
Marc-André Lureau | d91a7a5 | 2017-10-10 00:55:46 +0200 | [diff] [blame] | 109 | i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL); |
Marc-André Lureau | d31076b | 2017-10-10 00:55:49 +0200 | [diff] [blame] | 110 | be = i >= 0 ? tpm_be_find_by_type(i) : NULL; |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 111 | if (be == NULL) { |
Markus Armbruster | 8b53a19 | 2015-03-17 12:09:02 +0100 | [diff] [blame] | 112 | error_report(QERR_INVALID_PARAMETER_VALUE, |
| 113 | "type", "a TPM backend type"); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 114 | tpm_display_backend_drivers(); |
| 115 | return 1; |
| 116 | } |
| 117 | |
Stefan Berger | bb71623 | 2013-04-22 10:41:39 -0400 | [diff] [blame] | 118 | /* validate backend specific opts */ |
| 119 | qemu_opts_validate(opts, be->opts, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 120 | if (local_err) { |
Markus Armbruster | bc09a28 | 2015-02-10 15:11:17 +0100 | [diff] [blame] | 121 | error_report_err(local_err); |
Stefan Berger | bb71623 | 2013-04-22 10:41:39 -0400 | [diff] [blame] | 122 | return 1; |
| 123 | } |
| 124 | |
Marc-André Lureau | 9f7c0ef | 2017-11-06 19:39:09 +0100 | [diff] [blame] | 125 | drv = be->create(opts); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 126 | if (!drv) { |
| 127 | return 1; |
| 128 | } |
| 129 | |
Marc-André Lureau | 9f7c0ef | 2017-11-06 19:39:09 +0100 | [diff] [blame] | 130 | drv->id = g_strdup(id); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 131 | QLIST_INSERT_HEAD(&tpm_backends, drv, list); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 136 | /* |
| 137 | * Walk the list of TPM backend drivers that are in use and call their |
| 138 | * destroy function to have them cleaned up. |
| 139 | */ |
| 140 | void tpm_cleanup(void) |
| 141 | { |
| 142 | TPMBackend *drv, *next; |
| 143 | |
| 144 | QLIST_FOREACH_SAFE(drv, &tpm_backends, list, next) { |
| 145 | QLIST_REMOVE(drv, list); |
Amarnath Valluri | f35fe5c | 2017-09-29 14:10:15 +0300 | [diff] [blame] | 146 | object_unref(OBJECT(drv)); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Initialize the TPM. Process the tpmdev command line options describing the |
| 152 | * TPM backend. |
| 153 | */ |
| 154 | int tpm_init(void) |
| 155 | { |
Markus Armbruster | 28d0de7 | 2015-03-13 13:35:14 +0100 | [diff] [blame] | 156 | if (qemu_opts_foreach(qemu_find_opts("tpmdev"), |
| 157 | tpm_init_tpmdev, NULL, NULL)) { |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 158 | return -1; |
| 159 | } |
| 160 | |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Parse the TPM configuration options. |
| 166 | * To display all available TPM backends the user may use '-tpmdev help' |
| 167 | */ |
| 168 | int tpm_config_parse(QemuOptsList *opts_list, const char *optarg) |
| 169 | { |
| 170 | QemuOpts *opts; |
| 171 | |
| 172 | if (!strcmp(optarg, "help")) { |
| 173 | tpm_display_backend_drivers(); |
| 174 | return -1; |
| 175 | } |
Markus Armbruster | 70b9433 | 2015-02-13 12:50:26 +0100 | [diff] [blame] | 176 | opts = qemu_opts_parse_noisily(opts_list, optarg, true); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 177 | if (!opts) { |
| 178 | return -1; |
| 179 | } |
| 180 | return 0; |
| 181 | } |
| 182 | |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 183 | /* |
Markus Armbruster | eb815e2 | 2018-02-11 10:36:05 +0100 | [diff] [blame] | 184 | * Walk the list of active TPM backends and collect information about them. |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 185 | */ |
| 186 | TPMInfoList *qmp_query_tpm(Error **errp) |
| 187 | { |
| 188 | TPMBackend *drv; |
| 189 | TPMInfoList *info, *head = NULL, *cur_item = NULL; |
| 190 | |
| 191 | QLIST_FOREACH(drv, &tpm_backends, list) { |
Marc-André Lureau | 191adc9 | 2017-11-06 19:39:04 +0100 | [diff] [blame] | 192 | if (!drv->tpmif) { |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 193 | continue; |
| 194 | } |
Marc-André Lureau | 191adc9 | 2017-11-06 19:39:04 +0100 | [diff] [blame] | 195 | |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 196 | info = g_new0(TPMInfoList, 1); |
Amarnath Valluri | f59864b | 2017-09-29 14:10:17 +0300 | [diff] [blame] | 197 | info->value = tpm_backend_query_tpm(drv); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 198 | |
| 199 | if (!cur_item) { |
| 200 | head = cur_item = info; |
| 201 | } else { |
| 202 | cur_item->next = info; |
| 203 | cur_item = info; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return head; |
| 208 | } |
| 209 | |
| 210 | TpmTypeList *qmp_query_tpm_types(Error **errp) |
| 211 | { |
| 212 | unsigned int i = 0; |
| 213 | TpmTypeList *head = NULL, *prev = NULL, *cur_item; |
| 214 | |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 215 | for (i = 0; i < TPM_TYPE__MAX; i++) { |
Marc-André Lureau | d31076b | 2017-10-10 00:55:49 +0200 | [diff] [blame] | 216 | if (!tpm_be_find_by_type(i)) { |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 217 | continue; |
| 218 | } |
| 219 | cur_item = g_new0(TpmTypeList, 1); |
| 220 | cur_item->value = i; |
| 221 | |
| 222 | if (prev) { |
| 223 | prev->next = cur_item; |
| 224 | } |
| 225 | if (!head) { |
| 226 | head = cur_item; |
| 227 | } |
| 228 | prev = cur_item; |
| 229 | } |
| 230 | |
| 231 | return head; |
| 232 | } |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 233 | TpmModelList *qmp_query_tpm_models(Error **errp) |
| 234 | { |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 235 | TpmModelList *head = NULL, *prev = NULL, *cur_item; |
Marc-André Lureau | d3fd953 | 2017-11-06 19:39:25 +0100 | [diff] [blame] | 236 | GSList *e, *l = object_class_get_list(TYPE_TPM_IF, false); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 237 | |
Marc-André Lureau | d3fd953 | 2017-11-06 19:39:25 +0100 | [diff] [blame] | 238 | for (e = l; e; e = e->next) { |
| 239 | TPMIfClass *c = TPM_IF_CLASS(e->data); |
| 240 | |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 241 | cur_item = g_new0(TpmModelList, 1); |
Marc-André Lureau | d3fd953 | 2017-11-06 19:39:25 +0100 | [diff] [blame] | 242 | cur_item->value = c->model; |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 243 | |
| 244 | if (prev) { |
| 245 | prev->next = cur_item; |
| 246 | } |
| 247 | if (!head) { |
| 248 | head = cur_item; |
| 249 | } |
| 250 | prev = cur_item; |
| 251 | } |
Marc-André Lureau | d3fd953 | 2017-11-06 19:39:25 +0100 | [diff] [blame] | 252 | g_slist_free(l); |
Stefan Berger | d1a0cf7 | 2013-02-27 12:47:49 -0500 | [diff] [blame] | 253 | |
| 254 | return head; |
| 255 | } |