target/ppc: Check mask when setting cap_ppc_safe_indirect_branch
[qemu/kevin.git] / hw / tpm / tpm_util.c
blob2de52a0f1be6dfac3b7c18aeea798f470bd67db9
1 /*
2 * TPM utility functions
4 * Copyright (c) 2010 - 2015 IBM Corporation
5 * Authors:
6 * Stefan Berger <stefanb@us.ibm.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>
22 #include "qemu/osdep.h"
23 #include "qemu/error-report.h"
24 #include "qapi/error.h"
25 #include "qapi/visitor.h"
26 #include "tpm_util.h"
27 #include "tpm_int.h"
28 #include "exec/memory.h"
29 #include "sysemu/tpm_backend.h"
30 #include "hw/qdev.h"
32 #define DEBUG_TPM 0
34 #define DPRINTF(fmt, ...) do { \
35 if (DEBUG_TPM) { \
36 fprintf(stderr, "tpm-util:"fmt"\n", ## __VA_ARGS__); \
37 } \
38 } while (0)
40 /* tpm backend property */
42 static void get_tpm(Object *obj, Visitor *v, const char *name, void *opaque,
43 Error **errp)
45 DeviceState *dev = DEVICE(obj);
46 TPMBackend **be = qdev_get_prop_ptr(dev, opaque);
47 char *p;
49 p = g_strdup(*be ? (*be)->id : "");
50 visit_type_str(v, name, &p, errp);
51 g_free(p);
54 static void set_tpm(Object *obj, Visitor *v, const char *name, void *opaque,
55 Error **errp)
57 DeviceState *dev = DEVICE(obj);
58 Error *local_err = NULL;
59 Property *prop = opaque;
60 TPMBackend *s, **be = qdev_get_prop_ptr(dev, prop);
61 char *str;
63 if (dev->realized) {
64 qdev_prop_set_after_realize(dev, name, errp);
65 return;
68 visit_type_str(v, name, &str, &local_err);
69 if (local_err) {
70 error_propagate(errp, local_err);
71 return;
74 s = qemu_find_tpm_be(str);
75 if (s == NULL) {
76 error_setg(errp, "Property '%s.%s' can't find value '%s'",
77 object_get_typename(obj), prop->name, str);
78 } else if (tpm_backend_init(s, TPM_IF(obj), errp) == 0) {
79 *be = s; /* weak reference, avoid cyclic ref */
81 g_free(str);
84 static void release_tpm(Object *obj, const char *name, void *opaque)
86 DeviceState *dev = DEVICE(obj);
87 Property *prop = opaque;
88 TPMBackend **be = qdev_get_prop_ptr(dev, prop);
90 if (*be) {
91 tpm_backend_reset(*be);
95 const PropertyInfo qdev_prop_tpm = {
96 .name = "str",
97 .description = "ID of a tpm to use as a backend",
98 .get = get_tpm,
99 .set = set_tpm,
100 .release = release_tpm,
104 * Write an error message in the given output buffer.
106 void tpm_util_write_fatal_error_response(uint8_t *out, uint32_t out_len)
108 if (out_len >= sizeof(struct tpm_resp_hdr)) {
109 tpm_cmd_set_tag(out, TPM_TAG_RSP_COMMAND);
110 tpm_cmd_set_size(out, sizeof(struct tpm_resp_hdr));
111 tpm_cmd_set_error(out, TPM_FAIL);
115 bool tpm_util_is_selftest(const uint8_t *in, uint32_t in_len)
117 if (in_len >= sizeof(struct tpm_req_hdr)) {
118 return tpm_cmd_get_ordinal(in) == TPM_ORD_ContinueSelfTest;
121 return false;
125 * Send request to a TPM device. We expect a response within one second.
127 static int tpm_util_request(int fd,
128 const void *request,
129 size_t requestlen,
130 void *response,
131 size_t responselen)
133 fd_set readfds;
134 int n;
135 struct timeval tv = {
136 .tv_sec = 1,
137 .tv_usec = 0,
140 n = write(fd, request, requestlen);
141 if (n < 0) {
142 return -errno;
144 if (n != requestlen) {
145 return -EFAULT;
148 FD_ZERO(&readfds);
149 FD_SET(fd, &readfds);
151 /* wait for a second */
152 n = select(fd + 1, &readfds, NULL, NULL, &tv);
153 if (n != 1) {
154 return -errno;
157 n = read(fd, response, responselen);
158 if (n < sizeof(struct tpm_resp_hdr)) {
159 return -EFAULT;
162 /* check the header */
163 if (tpm_cmd_get_size(response) != n) {
164 return -EMSGSIZE;
167 return 0;
171 * A basic test of a TPM device. We expect a well formatted response header
172 * (error response is fine).
174 static int tpm_util_test(int fd,
175 const void *request,
176 size_t requestlen,
177 uint16_t *return_tag)
179 char buf[1024];
180 ssize_t ret;
182 ret = tpm_util_request(fd, request, requestlen,
183 buf, sizeof(buf));
184 if (ret < 0) {
185 return ret;
188 *return_tag = tpm_cmd_get_tag(buf);
190 return 0;
194 * Probe for the TPM device in the back
195 * Returns 0 on success with the version of the probed TPM set, 1 on failure.
197 int tpm_util_test_tpmdev(int tpm_fd, TPMVersion *tpm_version)
200 * Sending a TPM1.2 command to a TPM2 should return a TPM1.2
201 * header (tag = 0xc4) and error code (TPM_BADTAG = 0x1e)
203 * Sending a TPM2 command to a TPM 2 will give a TPM 2 tag in the
204 * header.
205 * Sending a TPM2 command to a TPM 1.2 will give a TPM 1.2 tag
206 * in the header and an error code.
208 const struct tpm_req_hdr test_req = {
209 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
210 .len = cpu_to_be32(sizeof(test_req)),
211 .ordinal = cpu_to_be32(TPM_ORD_GetTicks),
214 const struct tpm_req_hdr test_req_tpm2 = {
215 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
216 .len = cpu_to_be32(sizeof(test_req_tpm2)),
217 .ordinal = cpu_to_be32(TPM2_CC_ReadClock),
219 uint16_t return_tag;
220 int ret;
222 /* Send TPM 2 command */
223 ret = tpm_util_test(tpm_fd, &test_req_tpm2,
224 sizeof(test_req_tpm2), &return_tag);
225 /* TPM 2 would respond with a tag of TPM2_ST_NO_SESSIONS */
226 if (!ret && return_tag == TPM2_ST_NO_SESSIONS) {
227 *tpm_version = TPM_VERSION_2_0;
228 return 0;
231 /* Send TPM 1.2 command */
232 ret = tpm_util_test(tpm_fd, &test_req,
233 sizeof(test_req), &return_tag);
234 if (!ret && return_tag == TPM_TAG_RSP_COMMAND) {
235 *tpm_version = TPM_VERSION_1_2;
236 /* this is a TPM 1.2 */
237 return 0;
240 *tpm_version = TPM_VERSION_UNSPEC;
242 return 1;
245 int tpm_util_get_buffer_size(int tpm_fd, TPMVersion tpm_version,
246 size_t *buffersize)
248 int ret;
250 switch (tpm_version) {
251 case TPM_VERSION_1_2: {
252 const struct tpm_req_get_buffer_size {
253 struct tpm_req_hdr hdr;
254 uint32_t capability;
255 uint32_t len;
256 uint32_t subcap;
257 } QEMU_PACKED tpm_get_buffer_size = {
258 .hdr = {
259 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
260 .len = cpu_to_be32(sizeof(tpm_get_buffer_size)),
261 .ordinal = cpu_to_be32(TPM_ORD_GetCapability),
263 .capability = cpu_to_be32(TPM_CAP_PROPERTY),
264 .len = cpu_to_be32(sizeof(uint32_t)),
265 .subcap = cpu_to_be32(TPM_CAP_PROP_INPUT_BUFFER),
267 struct tpm_resp_get_buffer_size {
268 struct tpm_resp_hdr hdr;
269 uint32_t len;
270 uint32_t buffersize;
271 } QEMU_PACKED tpm_resp;
273 ret = tpm_util_request(tpm_fd, &tpm_get_buffer_size,
274 sizeof(tpm_get_buffer_size),
275 &tpm_resp, sizeof(tpm_resp));
276 if (ret < 0) {
277 return ret;
280 if (be32_to_cpu(tpm_resp.hdr.len) != sizeof(tpm_resp) ||
281 be32_to_cpu(tpm_resp.len) != sizeof(uint32_t)) {
282 DPRINTF("tpm_resp->hdr.len = %u, expected = %zu\n",
283 be32_to_cpu(tpm_resp.hdr.len), sizeof(tpm_resp));
284 DPRINTF("tpm_resp->len = %u, expected = %zu\n",
285 be32_to_cpu(tpm_resp.len), sizeof(uint32_t));
286 error_report("tpm_util: Got unexpected response to "
287 "TPM_GetCapability; errcode: 0x%x",
288 be32_to_cpu(tpm_resp.hdr.errcode));
289 return -EFAULT;
291 *buffersize = be32_to_cpu(tpm_resp.buffersize);
292 break;
294 case TPM_VERSION_2_0: {
295 const struct tpm2_req_get_buffer_size {
296 struct tpm_req_hdr hdr;
297 uint32_t capability;
298 uint32_t property;
299 uint32_t count;
300 } QEMU_PACKED tpm2_get_buffer_size = {
301 .hdr = {
302 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
303 .len = cpu_to_be32(sizeof(tpm2_get_buffer_size)),
304 .ordinal = cpu_to_be32(TPM2_CC_GetCapability),
306 .capability = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES),
307 .property = cpu_to_be32(TPM2_PT_MAX_COMMAND_SIZE),
308 .count = cpu_to_be32(2), /* also get TPM2_PT_MAX_RESPONSE_SIZE */
310 struct tpm2_resp_get_buffer_size {
311 struct tpm_resp_hdr hdr;
312 uint8_t more;
313 uint32_t capability;
314 uint32_t count;
315 uint32_t property1;
316 uint32_t value1;
317 uint32_t property2;
318 uint32_t value2;
319 } QEMU_PACKED tpm2_resp;
321 ret = tpm_util_request(tpm_fd, &tpm2_get_buffer_size,
322 sizeof(tpm2_get_buffer_size),
323 &tpm2_resp, sizeof(tpm2_resp));
324 if (ret < 0) {
325 return ret;
328 if (be32_to_cpu(tpm2_resp.hdr.len) != sizeof(tpm2_resp) ||
329 be32_to_cpu(tpm2_resp.count) != 2) {
330 DPRINTF("tpm2_resp->hdr.len = %u, expected = %zu\n",
331 be32_to_cpu(tpm2_resp.hdr.len), sizeof(tpm2_resp));
332 DPRINTF("tpm2_resp->len = %u, expected = %u\n",
333 be32_to_cpu(tpm2_resp.count), 2);
334 error_report("tpm_util: Got unexpected response to "
335 "TPM2_GetCapability; errcode: 0x%x",
336 be32_to_cpu(tpm2_resp.hdr.errcode));
337 return -EFAULT;
339 *buffersize = MAX(be32_to_cpu(tpm2_resp.value1),
340 be32_to_cpu(tpm2_resp.value2));
341 break;
343 case TPM_VERSION_UNSPEC:
344 return -EFAULT;
347 DPRINTF("buffersize of device: %zu\n", *buffersize);
349 return 0;
352 void tpm_sized_buffer_reset(TPMSizedBuffer *tsb)
354 g_free(tsb->buffer);
355 tsb->buffer = NULL;
356 tsb->size = 0;