libqos/ahci: fix cmd_sanity for ncq
[qemu.git] / hw / tpm / tpm_util.c
blob4ace5852e001a8e4dbbc5d197a7c5900c6261d6b
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 "tpm_util.h"
23 #include "tpm_int.h"
26 * A basic test of a TPM device. We expect a well formatted response header
27 * (error response is fine) within one second.
29 static int tpm_util_test(int fd,
30 unsigned char *request,
31 size_t requestlen,
32 uint16_t *return_tag)
34 struct tpm_resp_hdr *resp;
35 fd_set readfds;
36 int n;
37 struct timeval tv = {
38 .tv_sec = 1,
39 .tv_usec = 0,
41 unsigned char buf[1024];
43 n = write(fd, request, requestlen);
44 if (n < 0) {
45 return errno;
47 if (n != requestlen) {
48 return EFAULT;
51 FD_ZERO(&readfds);
52 FD_SET(fd, &readfds);
54 /* wait for a second */
55 n = select(fd + 1, &readfds, NULL, NULL, &tv);
56 if (n != 1) {
57 return errno;
60 n = read(fd, &buf, sizeof(buf));
61 if (n < sizeof(struct tpm_resp_hdr)) {
62 return EFAULT;
65 resp = (struct tpm_resp_hdr *)buf;
66 /* check the header */
67 if (be32_to_cpu(resp->len) != n) {
68 return EBADMSG;
71 *return_tag = be16_to_cpu(resp->tag);
73 return 0;
77 * Probe for the TPM device in the back
78 * Returns 0 on success with the version of the probed TPM set, 1 on failure.
80 int tpm_util_test_tpmdev(int tpm_fd, TPMVersion *tpm_version)
83 * Sending a TPM1.2 command to a TPM2 should return a TPM1.2
84 * header (tag = 0xc4) and error code (TPM_BADTAG = 0x1e)
86 * Sending a TPM2 command to a TPM 2 will give a TPM 2 tag in the
87 * header.
88 * Sending a TPM2 command to a TPM 1.2 will give a TPM 1.2 tag
89 * in the header and an error code.
91 const struct tpm_req_hdr test_req = {
92 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
93 .len = cpu_to_be32(sizeof(test_req)),
94 .ordinal = cpu_to_be32(TPM_ORD_GetTicks),
97 const struct tpm_req_hdr test_req_tpm2 = {
98 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
99 .len = cpu_to_be32(sizeof(test_req_tpm2)),
100 .ordinal = cpu_to_be32(TPM2_CC_ReadClock),
102 uint16_t return_tag;
103 int ret;
105 /* Send TPM 2 command */
106 ret = tpm_util_test(tpm_fd, (unsigned char *)&test_req_tpm2,
107 sizeof(test_req_tpm2), &return_tag);
108 /* TPM 2 would respond with a tag of TPM2_ST_NO_SESSIONS */
109 if (!ret && return_tag == TPM2_ST_NO_SESSIONS) {
110 *tpm_version = TPM_VERSION_2_0;
111 return 0;
114 /* Send TPM 1.2 command */
115 ret = tpm_util_test(tpm_fd, (unsigned char *)&test_req,
116 sizeof(test_req), &return_tag);
117 if (!ret && return_tag == TPM_TAG_RSP_COMMAND) {
118 *tpm_version = TPM_VERSION_1_2;
119 /* this is a TPM 1.2 */
120 return 0;
123 *tpm_version = TPM_VERSION_UNSPEC;
125 return 1;