1 /* 2 * QEMU TPM Backend 3 * 4 * Copyright IBM, Corp. 2013 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 backends/rng.c by Anthony Liguori 13 */ 14 15 #include "qemu/osdep.h" 16 #include "sysemu/tpm_backend.h" 17 #include "qapi/error.h" 18 #include "qapi/qmp/qerror.h" 19 #include "sysemu/tpm.h" 20 #include "qemu/thread.h" 21 22 static void tpm_backend_worker_thread(gpointer data, gpointer user_data) 23 { 24 TPMBackend *s = TPM_BACKEND(user_data); 25 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 26 27 assert(k->handle_request != NULL); 28 k->handle_request(s, (TPMBackendCmd *)data); 29 } 30 31 static void tpm_backend_thread_end(TPMBackend *s) 32 { 33 if (s->thread_pool) { 34 g_thread_pool_free(s->thread_pool, FALSE, TRUE); 35 s->thread_pool = NULL; 36 } 37 } 38 39 enum TpmType tpm_backend_get_type(TPMBackend *s) 40 { 41 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 42 43 return k->type; 44 } 45 46 int tpm_backend_init(TPMBackend *s, TPMIf *tpmif) 47 { 48 if (s->tpmif) { 49 return -1; 50 } 51 52 s->tpmif = tpmif; 53 object_ref(OBJECT(tpmif)); 54 55 s->had_startup_error = false; 56 57 return 0; 58 } 59 60 int tpm_backend_startup_tpm(TPMBackend *s) 61 { 62 int res = 0; 63 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 64 65 /* terminate a running TPM */ 66 tpm_backend_thread_end(s); 67 68 s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE, 69 NULL); 70 71 res = k->startup_tpm ? k->startup_tpm(s) : 0; 72 73 s->had_startup_error = (res != 0); 74 75 return res; 76 } 77 78 bool tpm_backend_had_startup_error(TPMBackend *s) 79 { 80 return s->had_startup_error; 81 } 82 83 void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd) 84 { 85 g_thread_pool_push(s->thread_pool, cmd, NULL); 86 } 87 88 void tpm_backend_reset(TPMBackend *s) 89 { 90 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 91 92 if (k->reset) { 93 k->reset(s); 94 } 95 96 tpm_backend_thread_end(s); 97 98 s->had_startup_error = false; 99 } 100 101 void tpm_backend_cancel_cmd(TPMBackend *s) 102 { 103 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 104 105 assert(k->cancel_cmd); 106 107 k->cancel_cmd(s); 108 } 109 110 bool tpm_backend_get_tpm_established_flag(TPMBackend *s) 111 { 112 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 113 114 return k->get_tpm_established_flag ? 115 k->get_tpm_established_flag(s) : false; 116 } 117 118 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty) 119 { 120 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 121 122 return k->reset_tpm_established_flag ? 123 k->reset_tpm_established_flag(s, locty) : 0; 124 } 125 126 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s) 127 { 128 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 129 130 assert(k->get_tpm_version); 131 132 return k->get_tpm_version(s); 133 } 134 135 TPMInfo *tpm_backend_query_tpm(TPMBackend *s) 136 { 137 TPMInfo *info = g_new0(TPMInfo, 1); 138 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 139 140 info->id = g_strdup(s->id); 141 info->model = s->fe_model; 142 if (k->get_tpm_options) { 143 info->options = k->get_tpm_options(s); 144 } 145 146 return info; 147 } 148 149 static bool tpm_backend_prop_get_opened(Object *obj, Error **errp) 150 { 151 TPMBackend *s = TPM_BACKEND(obj); 152 153 return s->opened; 154 } 155 156 void tpm_backend_open(TPMBackend *s, Error **errp) 157 { 158 object_property_set_bool(OBJECT(s), true, "opened", errp); 159 } 160 161 static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp) 162 { 163 TPMBackend *s = TPM_BACKEND(obj); 164 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 165 Error *local_err = NULL; 166 167 if (value == s->opened) { 168 return; 169 } 170 171 if (!value && s->opened) { 172 error_setg(errp, QERR_PERMISSION_DENIED); 173 return; 174 } 175 176 if (k->opened) { 177 k->opened(s, &local_err); 178 if (local_err) { 179 error_propagate(errp, local_err); 180 return; 181 } 182 } 183 184 s->opened = true; 185 } 186 187 static void tpm_backend_instance_init(Object *obj) 188 { 189 TPMBackend *s = TPM_BACKEND(obj); 190 191 object_property_add_bool(obj, "opened", 192 tpm_backend_prop_get_opened, 193 tpm_backend_prop_set_opened, 194 NULL); 195 s->fe_model = -1; 196 } 197 198 static void tpm_backend_instance_finalize(Object *obj) 199 { 200 TPMBackend *s = TPM_BACKEND(obj); 201 202 object_unref(OBJECT(s->tpmif)); 203 g_free(s->id); 204 tpm_backend_thread_end(s); 205 } 206 207 static const TypeInfo tpm_backend_info = { 208 .name = TYPE_TPM_BACKEND, 209 .parent = TYPE_OBJECT, 210 .instance_size = sizeof(TPMBackend), 211 .instance_init = tpm_backend_instance_init, 212 .instance_finalize = tpm_backend_instance_finalize, 213 .class_size = sizeof(TPMBackendClass), 214 .abstract = true, 215 }; 216 217 static const TypeInfo tpm_if_info = { 218 .name = TYPE_TPM_IF, 219 .parent = TYPE_INTERFACE, 220 .class_size = sizeof(TPMIfClass), 221 }; 222 223 static void register_types(void) 224 { 225 type_register_static(&tpm_backend_info); 226 type_register_static(&tpm_if_info); 227 } 228 229 type_init(register_types); 230