From nobody Sun Feb 8 20:34:28 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1510016510964603.537609613862; Mon, 6 Nov 2017 17:01:50 -0800 (PST) Received: from localhost ([::1]:50893 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eBsGi-0006op-9n for importer@patchew.org; Mon, 06 Nov 2017 20:01:32 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55424) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eBsEU-0005J5-2k for qemu-devel@nongnu.org; Mon, 06 Nov 2017 19:59:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eBsEQ-0003mB-SP for qemu-devel@nongnu.org; Mon, 06 Nov 2017 19:59:14 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:37212 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eBsEQ-0003m0-NS for qemu-devel@nongnu.org; Mon, 06 Nov 2017 19:59:10 -0500 Received: from pps.filterd (m0098413.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id vA70sDAk063540 for ; Mon, 6 Nov 2017 19:59:08 -0500 Received: from e11.ny.us.ibm.com (e11.ny.us.ibm.com [129.33.205.201]) by mx0b-001b2d01.pphosted.com with ESMTP id 2e31k7k53s-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 06 Nov 2017 19:59:08 -0500 Received: from localhost by e11.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 6 Nov 2017 19:59:07 -0500 Received: from b01cxnp23034.gho.pok.ibm.com (9.57.198.29) by e11.ny.us.ibm.com (146.89.104.198) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 6 Nov 2017 19:59:04 -0500 Received: from b01ledav006.gho.pok.ibm.com (b01ledav006.gho.pok.ibm.com [9.57.199.111]) by b01cxnp23034.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id vA70x4dj41812132; Tue, 7 Nov 2017 00:59:04 GMT Received: from b01ledav006.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4DC05AC048; Mon, 6 Nov 2017 19:59:53 -0500 (EST) Received: from sbct-3.watson.ibm.com (unknown [9.47.158.153]) by b01ledav006.gho.pok.ibm.com (Postfix) with ESMTP id 40AA8AC041; Mon, 6 Nov 2017 19:59:53 -0500 (EST) From: Stefan Berger To: qemu-devel@nongnu.org, marcandre.lureau@redhat.com Date: Mon, 6 Nov 2017 19:58:54 -0500 X-Mailer: git-send-email 2.5.5 In-Reply-To: <1510016336-4086-1-git-send-email-stefanb@linux.vnet.ibm.com> References: <1510016336-4086-1-git-send-email-stefanb@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 17110700-2213-0000-0000-0000023764B8 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00008022; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000239; SDB=6.00942183; UDB=6.00475248; IPR=6.00722471; BA=6.00005674; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00017888; XFM=3.00000015; UTC=2017-11-07 00:59:06 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17110700-2214-0000-0000-00005814E86D Message-Id: <1510016336-4086-4-git-send-email-stefanb@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-11-06_08:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1707230000 definitions=main-1711070011 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.158.5 Subject: [Qemu-devel] [PATCH 3/5] tpm: tpm_passthrough: Read the buffer size from the host device X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: amarnath.valluri@intel.com, Stefan Berger Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Rather than hard coding the buffer size in the tpm_passthrough backend read the TPM I/O buffer size from the host device. Signed-off-by: Stefan Berger --- hw/tpm/tpm_int.h | 9 ++++ hw/tpm/tpm_passthrough.c | 11 ++++- hw/tpm/tpm_util.c | 116 +++++++++++++++++++++++++++++++++++++++++++= ++++ hw/tpm/tpm_util.h | 3 ++ 4 files changed, 138 insertions(+), 1 deletion(-) diff --git a/hw/tpm/tpm_int.h b/hw/tpm/tpm_int.h index 1df5883..f838535 100644 --- a/hw/tpm/tpm_int.h +++ b/hw/tpm/tpm_int.h @@ -45,11 +45,20 @@ struct tpm_resp_hdr { =20 #define TPM_ORD_ContinueSelfTest 0x53 #define TPM_ORD_GetTicks 0xf1 +#define TPM_ORD_GetCapability 0x65 =20 +#define TPM_CAP_PROPERTY 0x05 + +#define TPM_CAP_PROP_INPUT_BUFFER 0x124 =20 /* TPM2 defines */ #define TPM2_ST_NO_SESSIONS 0x8001 =20 #define TPM2_CC_ReadClock 0x00000181 +#define TPM2_CC_GetCapability 0x0000017a + +#define TPM2_CAP_TPM_PROPERTIES 0x6 + +#define TPM2_PT_INPUT_BUFFER 0x10d =20 #endif /* TPM_TPM_INT_H */ diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c index 7ff9249..ec755fe 100644 --- a/hw/tpm/tpm_passthrough.c +++ b/hw/tpm/tpm_passthrough.c @@ -57,6 +57,7 @@ struct TPMPassthruState { int cancel_fd; =20 TPMVersion tpm_version; + uint32_t tpm_buffersize; }; =20 typedef struct TPMPassthruState TPMPassthruState; @@ -201,7 +202,15 @@ static TPMVersion tpm_passthrough_get_tpm_version(TPMB= ackend *tb) =20 static uint32_t tpm_passthrough_get_buffer_size(TPMBackend *tb) { - return 4096; + TPMPassthruState *tpm_pt =3D TPM_PASSTHROUGH(tb); + int ret; + + ret =3D tpm_util_get_buffer_size(tpm_pt->tpm_fd, tpm_pt->tpm_version, + &tpm_pt->tpm_buffersize); + if (ret < 0) { + tpm_pt->tpm_buffersize =3D 4096; + } + return tpm_pt->tpm_buffersize; } =20 /* diff --git a/hw/tpm/tpm_util.c b/hw/tpm/tpm_util.c index 396e793..3c861ab 100644 --- a/hw/tpm/tpm_util.c +++ b/hw/tpm/tpm_util.c @@ -20,10 +20,19 @@ */ =20 #include "qemu/osdep.h" +#include "qemu/error-report.h" #include "tpm_util.h" #include "tpm_int.h" #include "exec/memory.h" =20 +#define DEBUG_TPM 0 + +#define DPRINTF(fmt, ...) do { \ + if (DEBUG_TPM) { \ + fprintf(stderr, "tpm-util:"fmt"\n", ## __VA_ARGS__); \ + } \ +} while (0) + /* * Write an error message in the given output buffer. */ @@ -170,3 +179,110 @@ int tpm_util_test_tpmdev(int tpm_fd, TPMVersion *tpm_= version) =20 return 1; } + +int tpm_util_get_buffer_size(int tpm_fd, TPMVersion tpm_version, + uint32_t *buffersize) +{ + unsigned char buf[1024]; + const struct tpm_req_get_buffer_size { + struct tpm_req_hdr hdr; + uint32_t capability; + uint32_t len; + uint32_t subcap; + } QEMU_PACKED tpm_get_buffer_size =3D { + .hdr =3D { + .tag =3D cpu_to_be16(TPM_TAG_RQU_COMMAND), + .len =3D cpu_to_be32(sizeof(tpm_get_buffer_size)), + .ordinal =3D cpu_to_be32(TPM_ORD_GetCapability), + }, + .capability =3D cpu_to_be32(TPM_CAP_PROPERTY), + .len =3D cpu_to_be32(sizeof(uint32_t)), + .subcap =3D cpu_to_be32(TPM_CAP_PROP_INPUT_BUFFER), + }; + const struct tpm2_req_get_buffer_size { + struct tpm_req_hdr hdr; + uint32_t capability; + uint32_t property; + uint32_t count; + } QEMU_PACKED tpm2_get_buffer_size =3D { + .hdr =3D { + .tag =3D cpu_to_be16(TPM2_ST_NO_SESSIONS), + .len =3D cpu_to_be32(sizeof(tpm2_get_buffer_size)), + .ordinal =3D cpu_to_be32(TPM2_CC_GetCapability), + }, + .capability =3D cpu_to_be32(TPM2_CAP_TPM_PROPERTIES), + .property =3D cpu_to_be32(TPM2_PT_INPUT_BUFFER), + .count =3D cpu_to_be32(1), + }; + struct tpm_resp_get_buffer_size { + struct tpm_resp_hdr hdr; + uint32_t len; + uint32_t buffersize; + } QEMU_PACKED *tpm_resp =3D (struct tpm_resp_get_buffer_size *)buf; + struct tpm2_resp_get_buffer_size { + struct tpm_resp_hdr hdr; + uint8_t more; + uint32_t capability; + uint32_t count; + uint32_t property; + uint32_t value; + } QEMU_PACKED *tpm2_resp =3D (struct tpm2_resp_get_buffer_size *)buf; + unsigned char *request =3D NULL; + size_t requestlen; + int ret; + + switch (tpm_version) { + case TPM_VERSION_1_2: + request =3D (unsigned char *)&tpm_get_buffer_size; + requestlen =3D sizeof(tpm_get_buffer_size); + break; + case TPM_VERSION_2_0: + request =3D (unsigned char *)&tpm2_get_buffer_size; + requestlen =3D sizeof(tpm2_get_buffer_size); + break; + case TPM_VERSION_UNSPEC: + return -EFAULT; + } + + ret =3D tpm_util_tx(tpm_fd, request, requestlen, buf, sizeof(buf)); + if (ret < 0) { + return ret; + } + + switch (tpm_version) { + case TPM_VERSION_1_2: + if (be32_to_cpu(tpm_resp->hdr.len) !=3D sizeof(*tpm_resp) || + be32_to_cpu(tpm_resp->len) !=3D sizeof(uint32_t)) { + DPRINTF("tpm_resp->hdr.len =3D %u, expected =3D %zu\n", + be32_to_cpu(tpm_resp->hdr.len), sizeof(*tpm_resp)); + DPRINTF("tpm_resp->len =3D %u, expected =3D %zu\n", + be32_to_cpu(tpm_resp->len), sizeof(uint32_t)); + error_report("tpm_util: Got malformed response to " + "TPM_GetCapability; errcode: 0x%x", + be32_to_cpu(tpm_resp->hdr.errcode)); + return -EFAULT; + } + *buffersize =3D be32_to_cpu(tpm_resp->buffersize); + break; + case TPM_VERSION_2_0: + if (be32_to_cpu(tpm2_resp->hdr.len) !=3D sizeof(*tpm2_resp) || + be32_to_cpu(tpm2_resp->count) !=3D 1) { + DPRINTF("tpm2_resp->hdr.len =3D %u, expected =3D %zu\n", + be32_to_cpu(tpm2_resp->hdr.len), sizeof(*tpm2_resp)); + DPRINTF("tpm2_resp->len =3D %u, expected =3D %u\n", + be32_to_cpu(tpm2_resp->count), 1); + error_report("tpm_util: Got malformed response to " + "TPM2_GetCapability; errcode: 0x%x", + be32_to_cpu(tpm2_resp->hdr.errcode)); + return -EFAULT; + } + *buffersize =3D be32_to_cpu(tpm2_resp->value); + break; + case TPM_VERSION_UNSPEC: + break; + } + + DPRINTF("buffersize of device: %u\n", *buffersize); + + return 0; +} diff --git a/hw/tpm/tpm_util.h b/hw/tpm/tpm_util.h index aca10c9..e4fba32 100644 --- a/hw/tpm/tpm_util.h +++ b/hw/tpm/tpm_util.h @@ -36,4 +36,7 @@ static inline uint32_t tpm_cmd_get_size(const void *b) return be32_to_cpu(*(const uint32_t *)(b + 2)); } =20 +int tpm_util_get_buffer_size(int tpm_fd, TPMVersion tpm_version, + uint32_t *buffersize); + #endif /* TPM_TPM_UTIL_H */ --=20 2.5.5