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 size_t tpm_backend_realloc_buffer(TPMBackend *s, TPMSizedBuffer *sb) 84 { 85 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 86 87 assert(k->ops->realloc_buffer); 88 89 return k->ops->realloc_buffer(sb); 90 } 91 92 void tpm_backend_deliver_request(TPMBackend *s) 93 { 94 g_thread_pool_push(s->thread_pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD, 95 NULL); 96 } 97 98 void tpm_backend_reset(TPMBackend *s) 99 { 100 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 101 102 if (k->ops->reset) { 103 k->ops->reset(s); 104 } 105 106 tpm_backend_thread_end(s); 107 108 s->had_startup_error = false; 109 } 110 111 void tpm_backend_cancel_cmd(TPMBackend *s) 112 { 113 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 114 115 assert(k->ops->cancel_cmd); 116 117 k->ops->cancel_cmd(s); 118 } 119 120 bool tpm_backend_get_tpm_established_flag(TPMBackend *s) 121 { 122 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 123 124 return k->ops->get_tpm_established_flag ? 125 k->ops->get_tpm_established_flag(s) : false; 126 } 127 128 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty) 129 { 130 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 131 132 return k->ops->reset_tpm_established_flag ? 133 k->ops->reset_tpm_established_flag(s, locty) : 0; 134 } 135 136 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s) 137 { 138 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 139 140 assert(k->ops->get_tpm_version); 141 142 return k->ops->get_tpm_version(s); 143 } 144 145 TPMInfo *tpm_backend_query_tpm(TPMBackend *s) 146 { 147 TPMInfo *info = g_new0(TPMInfo, 1); 148 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 149 150 info->id = g_strdup(s->id); 151 info->model = s->fe_model; 152 info->options = k->ops->get_tpm_options ? 153 k->ops->get_tpm_options(s) : NULL; 154 155 return info; 156 } 157 158 static bool tpm_backend_prop_get_opened(Object *obj, Error **errp) 159 { 160 TPMBackend *s = TPM_BACKEND(obj); 161 162 return s->opened; 163 } 164 165 void tpm_backend_open(TPMBackend *s, Error **errp) 166 { 167 object_property_set_bool(OBJECT(s), true, "opened", errp); 168 } 169 170 static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp) 171 { 172 TPMBackend *s = TPM_BACKEND(obj); 173 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 174 Error *local_err = NULL; 175 176 if (value == s->opened) { 177 return; 178 } 179 180 if (!value && s->opened) { 181 error_setg(errp, QERR_PERMISSION_DENIED); 182 return; 183 } 184 185 if (k->opened) { 186 k->opened(s, &local_err); 187 if (local_err) { 188 error_propagate(errp, local_err); 189 return; 190 } 191 } 192 193 s->opened = true; 194 } 195 196 static void tpm_backend_instance_init(Object *obj) 197 { 198 TPMBackend *s = TPM_BACKEND(obj); 199 200 object_property_add_bool(obj, "opened", 201 tpm_backend_prop_get_opened, 202 tpm_backend_prop_set_opened, 203 NULL); 204 s->fe_model = -1; 205 } 206 207 static void tpm_backend_instance_finalize(Object *obj) 208 { 209 TPMBackend *s = TPM_BACKEND(obj); 210 211 g_free(s->id); 212 tpm_backend_thread_end(s); 213 } 214 215 static const TypeInfo tpm_backend_info = { 216 .name = TYPE_TPM_BACKEND, 217 .parent = TYPE_OBJECT, 218 .instance_size = sizeof(TPMBackend), 219 .instance_init = tpm_backend_instance_init, 220 .instance_finalize = tpm_backend_instance_finalize, 221 .class_size = sizeof(TPMBackendClass), 222 .abstract = true, 223 }; 224 225 static void register_types(void) 226 { 227 type_register_static(&tpm_backend_info); 228 } 229 230 type_init(register_types); 231