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, TPMState *state, 47 TPMRecvDataCB *datacb) 48 { 49 s->tpm_state = state; 50 s->recv_data_callback = datacb; 51 s->had_startup_error = false; 52 53 return 0; 54 } 55 56 int tpm_backend_startup_tpm(TPMBackend *s) 57 { 58 int res = 0; 59 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 60 61 /* terminate a running TPM */ 62 tpm_backend_thread_end(s); 63 64 s->thread_pool = g_thread_pool_new(tpm_backend_worker_thread, s, 1, TRUE, 65 NULL); 66 67 res = k->startup_tpm ? k->startup_tpm(s) : 0; 68 69 s->had_startup_error = (res != 0); 70 71 return res; 72 } 73 74 bool tpm_backend_had_startup_error(TPMBackend *s) 75 { 76 return s->had_startup_error; 77 } 78 79 void tpm_backend_deliver_request(TPMBackend *s, TPMBackendCmd *cmd) 80 { 81 g_thread_pool_push(s->thread_pool, cmd, NULL); 82 } 83 84 void tpm_backend_reset(TPMBackend *s) 85 { 86 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 87 88 if (k->reset) { 89 k->reset(s); 90 } 91 92 tpm_backend_thread_end(s); 93 94 s->had_startup_error = false; 95 } 96 97 void tpm_backend_cancel_cmd(TPMBackend *s) 98 { 99 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 100 101 assert(k->cancel_cmd); 102 103 k->cancel_cmd(s); 104 } 105 106 bool tpm_backend_get_tpm_established_flag(TPMBackend *s) 107 { 108 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 109 110 return k->get_tpm_established_flag ? 111 k->get_tpm_established_flag(s) : false; 112 } 113 114 int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty) 115 { 116 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 117 118 return k->reset_tpm_established_flag ? 119 k->reset_tpm_established_flag(s, locty) : 0; 120 } 121 122 TPMVersion tpm_backend_get_tpm_version(TPMBackend *s) 123 { 124 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 125 126 assert(k->get_tpm_version); 127 128 return k->get_tpm_version(s); 129 } 130 131 TPMInfo *tpm_backend_query_tpm(TPMBackend *s) 132 { 133 TPMInfo *info = g_new0(TPMInfo, 1); 134 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 135 136 info->id = g_strdup(s->id); 137 info->model = s->fe_model; 138 if (k->get_tpm_options) { 139 info->options = k->get_tpm_options(s); 140 } 141 142 return info; 143 } 144 145 static bool tpm_backend_prop_get_opened(Object *obj, Error **errp) 146 { 147 TPMBackend *s = TPM_BACKEND(obj); 148 149 return s->opened; 150 } 151 152 void tpm_backend_open(TPMBackend *s, Error **errp) 153 { 154 object_property_set_bool(OBJECT(s), true, "opened", errp); 155 } 156 157 static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp) 158 { 159 TPMBackend *s = TPM_BACKEND(obj); 160 TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s); 161 Error *local_err = NULL; 162 163 if (value == s->opened) { 164 return; 165 } 166 167 if (!value && s->opened) { 168 error_setg(errp, QERR_PERMISSION_DENIED); 169 return; 170 } 171 172 if (k->opened) { 173 k->opened(s, &local_err); 174 if (local_err) { 175 error_propagate(errp, local_err); 176 return; 177 } 178 } 179 180 s->opened = true; 181 } 182 183 static void tpm_backend_instance_init(Object *obj) 184 { 185 TPMBackend *s = TPM_BACKEND(obj); 186 187 object_property_add_bool(obj, "opened", 188 tpm_backend_prop_get_opened, 189 tpm_backend_prop_set_opened, 190 NULL); 191 s->fe_model = -1; 192 } 193 194 static void tpm_backend_instance_finalize(Object *obj) 195 { 196 TPMBackend *s = TPM_BACKEND(obj); 197 198 g_free(s->id); 199 tpm_backend_thread_end(s); 200 } 201 202 static const TypeInfo tpm_backend_info = { 203 .name = TYPE_TPM_BACKEND, 204 .parent = TYPE_OBJECT, 205 .instance_size = sizeof(TPMBackend), 206 .instance_init = tpm_backend_instance_init, 207 .instance_finalize = tpm_backend_instance_finalize, 208 .class_size = sizeof(TPMBackendClass), 209 .abstract = true, 210 }; 211 212 static void register_types(void) 213 { 214 type_register_static(&tpm_backend_info); 215 } 216 217 type_init(register_types); 218