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_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_END, NULL); 35 g_thread_pool_free(s->thread_pool, FALSE, TRUE); 36 s->thread_pool = NULL; 37 } 38 } 39 40 enum TpmType tpm_backend_get_type(TPMBackend *s) 41 { 42 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 43 44 return k->ops->type; 45 } 46 47 int tpm_backend_init(TPMBackend *s, TPMState *state, 48 TPMRecvDataCB *datacb) 49 { 50 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 51 52 s->tpm_state = state; 53 s->recv_data_callback = datacb; 54 s->had_startup_error = false; 55 56 return k->ops->init ? k->ops->init(s) : 0; 57 } 58 59 int tpm_backend_startup_tpm(TPMBackend *s) 60 { 61 int res = 0; 62 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 63 64 /* terminate a running TPM */ 65 tpm_backend_thread_end(s); 66 67 s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE, 68 NULL); 69 g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL); 70 71 res = k->ops->startup_tpm ? k->ops->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) 84 { 85 g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD, 86 NULL); 87 } 88 89 void tpm_backend_reset(TPMBackend *s) 90 { 91 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 92 93 if (k->ops->reset) { 94 k->ops->reset(s); 95 } 96 97 tpm_backend_thread_end(s); 98 99 s->had_startup_error = false; 100 } 101 102 void tpm_backend_cancel_cmd(TPMBackend *s) 103 { 104 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 105 106 assert(k->ops->cancel_cmd); 107 108 k->ops->cancel_cmd(s); 109 } 110 111 bool tpm_backend_get_tpm_established_flag(TPMBackend *s) 112 { 113 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 114 115 return k->ops->get_tpm_established_flag ? 116 k->ops->get_tpm_established_flag(s) : false; 117 } 118 119 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty) 120 { 121 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 122 123 return k->ops->reset_tpm_established_flag ? 124 k->ops->reset_tpm_established_flag(s, locty) : 0; 125 } 126 127 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s) 128 { 129 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 130 131 assert(k->ops->get_tpm_version); 132 133 return k->ops->get_tpm_version(s); 134 } 135 136 TPMInfo *tpm_backend_query_tpm(TPMBackend *s) 137 { 138 TPMInfo *info = g_new0(TPMInfo, 1); 139 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 140 141 info->id = g_strdup(s->id); 142 info->model = s->fe_model; 143 info->options = k->ops->get_tpm_options ? 144 k->ops->get_tpm_options(s) : NULL; 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 g_free(s->id); 203 tpm_backend_thread_end(s); 204 } 205 206 static const TypeInfo tpm_backend_info = { 207 .name = TYPE_TPM_BACKEND, 208 .parent = TYPE_OBJECT, 209 .instance_size = sizeof(TPMBackend), 210 .instance_init = tpm_backend_instance_init, 211 .instance_finalize = tpm_backend_instance_finalize, 212 .class_size = sizeof(TPMBackendClass), 213 .abstract = true, 214 }; 215 216 static void register_types(void) 217 { 218 type_register_static(&tpm_backend_info); 219 } 220 221 type_init(register_types); 222