2 * TPM utility functions
4 * Copyright (c) 2010 - 2015 IBM Corporation
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"
25 #include "exec/memory.h"
28 * Write an error message in the given output buffer.
30 void tpm_util_write_fatal_error_response(uint8_t *out
, uint32_t out_len
)
32 if (out_len
>= sizeof(struct tpm_resp_hdr
)) {
33 struct tpm_resp_hdr
*resp
= (struct tpm_resp_hdr
*)out
;
35 resp
->tag
= cpu_to_be16(TPM_TAG_RSP_COMMAND
);
36 resp
->len
= cpu_to_be32(sizeof(struct tpm_resp_hdr
));
37 resp
->errcode
= cpu_to_be32(TPM_FAIL
);
41 bool tpm_util_is_selftest(const uint8_t *in
, uint32_t in_len
)
43 struct tpm_req_hdr
*hdr
= (struct tpm_req_hdr
*)in
;
45 if (in_len
>= sizeof(*hdr
)) {
46 return (be32_to_cpu(hdr
->ordinal
) == TPM_ORD_ContinueSelfTest
);
53 * A basic test of a TPM device. We expect a well formatted response header
54 * (error response is fine) within one second.
56 static int tpm_util_test(int fd
,
57 unsigned char *request
,
61 struct tpm_resp_hdr
*resp
;
68 unsigned char buf
[1024];
70 n
= write(fd
, request
, requestlen
);
74 if (n
!= requestlen
) {
81 /* wait for a second */
82 n
= select(fd
+ 1, &readfds
, NULL
, NULL
, &tv
);
87 n
= read(fd
, &buf
, sizeof(buf
));
88 if (n
< sizeof(struct tpm_resp_hdr
)) {
92 resp
= (struct tpm_resp_hdr
*)buf
;
93 /* check the header */
94 if (be32_to_cpu(resp
->len
) != n
) {
98 *return_tag
= be16_to_cpu(resp
->tag
);
104 * Probe for the TPM device in the back
105 * Returns 0 on success with the version of the probed TPM set, 1 on failure.
107 int tpm_util_test_tpmdev(int tpm_fd
, TPMVersion
*tpm_version
)
110 * Sending a TPM1.2 command to a TPM2 should return a TPM1.2
111 * header (tag = 0xc4) and error code (TPM_BADTAG = 0x1e)
113 * Sending a TPM2 command to a TPM 2 will give a TPM 2 tag in the
115 * Sending a TPM2 command to a TPM 1.2 will give a TPM 1.2 tag
116 * in the header and an error code.
118 const struct tpm_req_hdr test_req
= {
119 .tag
= cpu_to_be16(TPM_TAG_RQU_COMMAND
),
120 .len
= cpu_to_be32(sizeof(test_req
)),
121 .ordinal
= cpu_to_be32(TPM_ORD_GetTicks
),
124 const struct tpm_req_hdr test_req_tpm2
= {
125 .tag
= cpu_to_be16(TPM2_ST_NO_SESSIONS
),
126 .len
= cpu_to_be32(sizeof(test_req_tpm2
)),
127 .ordinal
= cpu_to_be32(TPM2_CC_ReadClock
),
132 /* Send TPM 2 command */
133 ret
= tpm_util_test(tpm_fd
, (unsigned char *)&test_req_tpm2
,
134 sizeof(test_req_tpm2
), &return_tag
);
135 /* TPM 2 would respond with a tag of TPM2_ST_NO_SESSIONS */
136 if (!ret
&& return_tag
== TPM2_ST_NO_SESSIONS
) {
137 *tpm_version
= TPM_VERSION_2_0
;
141 /* Send TPM 1.2 command */
142 ret
= tpm_util_test(tpm_fd
, (unsigned char *)&test_req
,
143 sizeof(test_req
), &return_tag
);
144 if (!ret
&& return_tag
== TPM_TAG_RSP_COMMAND
) {
145 *tpm_version
= TPM_VERSION_1_2
;
146 /* this is a TPM 1.2 */
150 *tpm_version
= TPM_VERSION_UNSPEC
;