From nobody Tue Feb 10 10:19:02 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 (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1500541912144467.23096999063876; Thu, 20 Jul 2017 02:11:52 -0700 (PDT) Received: from localhost ([::1]:36823 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dY7Ur-0004t9-Py for importer@patchew.org; Thu, 20 Jul 2017 05:11:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44373) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dY7N9-0006Mo-OL for qemu-devel@nongnu.org; Thu, 20 Jul 2017 05:03:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dY7N8-0004Pv-Nb for qemu-devel@nongnu.org; Thu, 20 Jul 2017 05:03:51 -0400 Received: from manul.sfritsch.de ([2a01:4f8:172:195f:112::2]:36998) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dY7N8-0004PM-IV for qemu-devel@nongnu.org; Thu, 20 Jul 2017 05:03:50 -0400 From: Stefan Fritsch To: Gerd Hoffmann , qemu-devel@nongnu.org Date: Thu, 20 Jul 2017 11:02:46 +0200 Message-Id: <1500541371-24788-4-git-send-email-sf@sfritsch.de> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1500541371-24788-1-git-send-email-sf@sfritsch.de> References: <1500541371-24788-1-git-send-email-sf@sfritsch.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a01:4f8:172:195f:112::2 Subject: [Qemu-devel] [PATCH v2 3/8] usb-ccid: Set protocol parameters based on card ATR 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: , 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" From: Stefan Fritsch For the ATR interface bytes encoding see ISO/IEC 7816-3. For the interpretation of the protocol parameter block see the CCID specification. - Values that are not present in the ATR default to zero. - TA_1 is used to fill bmFindexDindex. - The high nibble of bmTCCKST0 must be the protocol (T=3D0 or T=3D1). - For T=3D0 the bWaitingInteger can be found in the global byte TC_2, for T=3D1 the waiting integer is in TB_3 and bIFSC is in TA_3. Signed-off-by: Stefan Fritsch Signed-off-by: Christian Ehrhardt --- hw/usb/dev-smartcard-reader.c | 66 +++++++++++++++++++++++++++++++++++----= ---- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/hw/usb/dev-smartcard-reader.c b/hw/usb/dev-smartcard-reader.c index 49791492ea..769949144b 100644 --- a/hw/usb/dev-smartcard-reader.c +++ b/hw/usb/dev-smartcard-reader.c @@ -801,7 +801,7 @@ static void ccid_write_parameters(USBCCIDState *s, CCID= _Header *recv) return; } h->b.hdr.bMessageType =3D CCID_MESSAGE_TYPE_RDR_to_PC_Parameters; - h->b.hdr.dwLength =3D 0; + h->b.hdr.dwLength =3D (s->bProtocolNum =3D=3D 1) ? 7 : 5; h->b.hdr.bSlot =3D recv->bSlot; h->b.hdr.bSeq =3D recv->bSeq; h->b.bStatus =3D ccid_calc_status(s); @@ -871,6 +871,49 @@ static uint8_t atr_get_protocol_num(const uint8_t *atr= , uint32_t len) return atr[i] & 0x0f; } =20 +/* + * Get a specific interface byte TX_y (X=3D'A'..'D' y=3D1..3) + * See ISO/IEC 7816-3:2006 Chapter 8 for details. + */ +static uint8_t atr_get_TXY(const uint8_t *atr, uint32_t len, char x, int y) +{ + int pos; + uint8_t t0, ti, i; + + if (x < 'A' || x > 'D') { + return 0; + } + x -=3D 'A'; + + if (len < 2) { + return 0; + } + t0 =3D atr[1] >> 4; + pos =3D 2; + ti =3D 1; + /* Skip TA_i..TD_i if present while not at requested index y */ + while (ti < y) { + pos +=3D !!(t0 & 0x01) + !!(t0 & 0x02) + !!(t0 & 0x04); + if (pos >=3D len || !(t0 & 0x08)) { + return 0; + } + t0 =3D atr[pos++] >> 4; + ti++; + } + if (!(t0 & 0x01 << x)) { + return 0; + } + for (i =3D 0; i < x; ++i) { + if (t0 & (1 << i)) { + pos++; + } + } + if (pos >=3D len) { + return 0; + } + return atr[pos]; +} + static void ccid_write_data_block_atr(USBCCIDState *s, CCID_Header *recv) { const uint8_t *atr =3D NULL; @@ -890,21 +933,20 @@ static void ccid_write_data_block_atr(USBCCIDState *s= , CCID_Header *recv) : s->bProtocolNum); switch (atr_protocol_num) { case 0: - /* TODO: unimplemented ATR T0 parameters */ - t0->bmFindexDindex =3D 0; - t0->bmTCCKST0 =3D 0; + t0->bmFindexDindex =3D atr_get_TXY(atr, len, 'A', 1); + t0->bmTCCKST0 =3D 0; /* T0 */ t0->bGuardTimeT0 =3D 0; - t0->bWaitingIntegerT0 =3D 0; + t0->bWaitingIntegerT0 =3D atr_get_TXY(atr, len, 'C', 2); t0->bClockStop =3D 0; break; case 1: - /* TODO: unimplemented ATR T1 parameters */ - t1->bmFindexDindex =3D 0; - t1->bmTCCKST1 =3D 0; - t1->bGuardTimeT1 =3D 0; - t1->bWaitingIntegerT1 =3D 0; - t1->bClockStop =3D 0; - t1->bIFSC =3D 0; + /* NOTE: If present TD2 should specify protocl T=3D1 */ + t1->bmFindexDindex =3D atr_get_TXY(atr, len, 'A', 1); + t1->bmTCCKST1 =3D 0x10; /* T1 */ + t1->bGuardTimeT1 =3D atr_get_TXY(atr, len, 'C', 1); + t1->bWaitingIntegerT1 =3D atr_get_TXY(atr, len, 'B', 3); + t1->bClockStop =3D 0x00; + t1->bIFSC =3D atr_get_TXY(atr, len, 'A', 3); t1->bNadValue =3D 0; break; default: --=20 2.11.0