From patchwork Sun Mar 20 19:34:42 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Ricard X-Patchwork-Id: 8628661 Return-Path: X-Original-To: patchwork-tpmdd-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 12B609F3D1 for ; Sun, 20 Mar 2016 19:35:21 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 101FC20221 for ; Sun, 20 Mar 2016 19:35:20 +0000 (UTC) Received: from lists.sourceforge.net (lists.sourceforge.net [216.34.181.88]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1903220204 for ; Sun, 20 Mar 2016 19:35:18 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=sfs-ml-3.v29.ch3.sourceforge.com) by sfs-ml-3.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1ahj88-0003IG-TH; Sun, 20 Mar 2016 19:35:16 +0000 Received: from sog-mx-2.v43.ch3.sourceforge.com ([172.29.43.192] helo=mx.sourceforge.net) by sfs-ml-3.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1ahj87-0003Hi-Gr for tpmdd-devel@lists.sourceforge.net; Sun, 20 Mar 2016 19:35:15 +0000 Received-SPF: pass (sog-mx-2.v43.ch3.sourceforge.com: domain of gmail.com designates 74.125.82.53 as permitted sender) client-ip=74.125.82.53; envelope-from=christophe.ricard@gmail.com; helo=mail-wm0-f53.google.com; Received: from mail-wm0-f53.google.com ([74.125.82.53]) by sog-mx-2.v43.ch3.sourceforge.com with esmtps (TLSv1:RC4-SHA:128) (Exim 4.76) id 1ahj86-0007MC-Fr for tpmdd-devel@lists.sourceforge.net; Sun, 20 Mar 2016 19:35:15 +0000 Received: by mail-wm0-f53.google.com with SMTP id l68so99061489wml.1 for ; Sun, 20 Mar 2016 12:35:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=jtMVewQJo3Su2s5el+DxQad+SvCuCrClqcut907JSMc=; b=BarvYmG9Jwm5IxBFioeLAVlfZNfP5XpLuu1GbY8XTHIMiKArWZLyemmytp34PeLpJ8 DRJdreIyac2ocGkqv8QmYaQMfCe3o1Txv54BaI8y03DSvUCrDoRn6RI0SiLbIkkhyufe lIspLKiHahhPx68poAzQ/jlgMCw8/FPvTCwI0230Xu4RDYkHvdXNFFSirKLX1kUyjSIo lnmYfe4eBUEzEOsWgGyou9IK+JdGviMzyXdjkVYUEGxMOMScvWES0h0SRExiphVCHgB8 ephZdK/lIQgbxSa/ZnkMwDAUoFbX0d7FAb/h8RsT/vc7sjePUBFFKA7MHgZzDoatk/Qp mtrQ== X-Gm-Message-State: AD7BkJIyM8ITw/by9my9yIATaadVNDXZ2ZfS3yGX+g7oTsoYO2CNUEAjPb8G5yQIHoi/WQ== X-Received: by 10.194.85.161 with SMTP id i1mr28783101wjz.95.1458502508520; Sun, 20 Mar 2016 12:35:08 -0700 (PDT) Received: from localhost.localdomain (AMarseille-654-1-498-218.w86-210.abo.wanadoo.fr. [86.210.138.218]) by smtp.gmail.com with ESMTPSA id w203sm7560646wmg.14.2016.03.20.12.35.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 20 Mar 2016 12:35:07 -0700 (PDT) From: Christophe Ricard X-Google-Original-From: Christophe Ricard To: jarkko.sakkinen@linux.intel.com Date: Sun, 20 Mar 2016 20:34:42 +0100 Message-Id: <1458502483-16887-12-git-send-email-christophe-h.ricard@st.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1458502483-16887-1-git-send-email-christophe-h.ricard@st.com> References: <1458502483-16887-1-git-send-email-christophe-h.ricard@st.com> X-Spam-Score: -1.6 (-) X-Headers-End: 1ahj86-0007MC-Fr Cc: jean-luc.blanc@st.com, ashley@ashleylai.com, tpmdd-devel@lists.sourceforge.net, christophe-h.ricard@st.com, benoit.houyere@st.com Subject: [tpmdd-devel] [PATCH 11/12] tpm: Add check_data handle to tpm_class_ops in order to check data integrity X-BeenThere: tpmdd-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list List-Id: Tpm Device Driver maintainance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: tpmdd-devel-bounces@lists.sourceforge.net X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,HK_RANDOM_ENVFROM,RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP For example, in order to compute crc over the data sent in lower layer (i2c for instance), provide a specific handle. The current principles: - When sending command: 1) Host writes TPM_STS.commandReady 2) Host writes command 3) Host checks TPM received data correctly 4) if not go to step 1 - When receiving data: 1) Host check TPM_STS.dataAvail is set 2) Host get data 3) Host check received data are correct. 4) if not Host write TPM_STS.responseRetry and go to step 1. Signed-off-by: Christophe Ricard --- drivers/char/tpm/tpm_tis_core.c | 81 ++++++++++++++++++++++++++--------------- include/linux/tpm.h | 1 + 2 files changed, 52 insertions(+), 30 deletions(-) diff --git a/drivers/char/tpm/tpm_tis_core.c b/drivers/char/tpm/tpm_tis_core.c index baa9ab1..9efb3ac 100644 --- a/drivers/char/tpm/tpm_tis_core.c +++ b/drivers/char/tpm/tpm_tis_core.c @@ -126,6 +126,14 @@ again: return -1; } +static bool tpm_tis_check_data(struct tpm_chip *chip, u8 *buf, size_t len) +{ + if (chip->ops->check_data) + return chip->ops->check_data(chip, buf, len); + + return true; +} + u8 tpm_tis_status(struct tpm_chip *chip) { return tpm_read_byte(chip, TPM_STS(chip->vendor.locality)); @@ -197,7 +205,8 @@ static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count) { int size = 0; - int expected, status; + int expected, status, i; + bool check_data = false; if (count < TPM_HEADER_SIZE) { size = -EIO; @@ -206,34 +215,41 @@ int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count) tpm_tis_clear_int(chip); - /* read first 10 bytes, including tag, paramsize, and result */ - size = recv_data(chip, buf, TPM_HEADER_SIZE); - if (size < TPM_HEADER_SIZE) { - dev_err(&chip->dev, "Unable to read header\n"); - goto out; - } + for (i = 0; i < TPM_RETRY && !check_data; i++) { + /* read first 10 bytes, including tag, paramsize, and result */ + size = recv_data(chip, buf, TPM_HEADER_SIZE); + if (size < TPM_HEADER_SIZE) { + dev_err(&chip->dev, "Unable to read header\n"); + goto out; + } - expected = be32_to_cpu(*(__be32 *) (buf + 2)); - if (expected > count) { - size = -EIO; - goto out; - } + expected = be32_to_cpu(*(__be32 *) (buf + 2)); + if (expected > count) { + size = -EIO; + goto out; + } - size += recv_data(chip, &buf[TPM_HEADER_SIZE], - expected - TPM_HEADER_SIZE); - if (size < expected) { - dev_err(&chip->dev, "Unable to read remainder of result\n"); - size = -ETIME; - goto out; - } + size += recv_data(chip, &buf[TPM_HEADER_SIZE], + expected - TPM_HEADER_SIZE); + if (size < expected) { + dev_err(&chip->dev, "Unable to read remainder of result\n"); + size = -ETIME; + goto out; + } - wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, - &chip->vendor.int_queue, false); - status = tpm_tis_status(chip); - if (status & TPM_STS_DATA_AVAIL) { /* retry? */ - dev_err(&chip->dev, "Error left over data\n"); - size = -EIO; - goto out; + wait_for_tpm_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, + &chip->vendor.int_queue, false); + status = tpm_tis_status(chip); + if (status & TPM_STS_DATA_AVAIL) { /* retry? */ + dev_err(&chip->dev, "Error left over data\n"); + size = -EIO; + goto out; + } + + check_data = tpm_tis_check_data(chip, buf, size); + if (!check_data) + tpm_write_byte(chip, TPM_STS(chip->vendor.locality), + TPM_STS_RESPONSE_RETRY); } out: @@ -323,15 +339,20 @@ static void disable_interrupts(struct tpm_chip *chip) */ static int tpm_tis_send_main(struct tpm_chip *chip, u8 *buf, size_t len) { - int rc; + int rc, i; u32 ordinal; unsigned long dur; + bool data_valid = false; tpm_tis_clear_int(chip); - rc = tpm_tis_send_data(chip, buf, len); - if (rc < 0) - return rc; + for (i = 0; i < TPM_RETRY && !data_valid; i++) { + rc = tpm_tis_send_data(chip, buf, len); + if (rc < 0) + return rc; + + data_valid = tpm_tis_check_data(chip, buf, len); + } /* go and do it */ tpm_write_byte(chip, TPM_STS(chip->vendor.locality), TPM_STS_GO); diff --git a/include/linux/tpm.h b/include/linux/tpm.h index f78e4fc..8e871dc 100644 --- a/include/linux/tpm.h +++ b/include/linux/tpm.h @@ -43,6 +43,7 @@ struct tpm_class_ops { u8 (*status) (struct tpm_chip *chip); bool (*update_timeouts)(struct tpm_chip *chip, unsigned long *timeout_cap); + bool (*check_data)(struct tpm_chip *chip, u8 *buf, size_t len); int (*read_bytes)(struct tpm_chip *chip, u32 addr, size_t len, u8 size, u8 *dst);