From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5842EC38145 for ; Sat, 3 Sep 2022 13:51:35 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5E714845E7; Sat, 3 Sep 2022 15:51:32 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="OqSd9JEz"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5E25F845E7; Sat, 3 Sep 2022 15:51:30 +0200 (CEST) Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 3549284382 for ; Sat, 3 Sep 2022 15:51:27 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ilias.apalodimas@linaro.org Received: by mail-ej1-x631.google.com with SMTP id og21so8865057ejc.2 for ; Sat, 03 Sep 2022 06:51:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=+ea9oLAhOlj6z+2Pv2fVs7rZLJT4uqYeKCxHf37vcoQ=; b=OqSd9JEzV2LEXUEVeaUNF25co0HAw0ZZb3YKe4kDFxlql6kiblUi/YCT1wlwUH/YPg GEih9pA7XMsd8yA5MI510ikj2ErBT0fDW8zbX8t2vzTSWhsnXSdk0dfCj5nDll60eP6+ kixGK0McQ+8glyNQrBqLAA+wBN1p+h3ho7KV8WeAdtrnhyjBqXC9oHtUFJej40LT+sjl coNCXgVkOiql2f02RSDRCDqMy5ElsHYl2SJ5EpRphDnLAezX1ORC/j58vZmcZ+efX61f LevElcO4H1E87tWEpqSNyBg3uSgo+L4yfOsRdhhhvq1FbVFSQmTXLvIvLA/ld89riXsU U1tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=+ea9oLAhOlj6z+2Pv2fVs7rZLJT4uqYeKCxHf37vcoQ=; b=WPlo5wf4S7iXNFKwA4d8yFRP+k9M9UWWDs/9DeedsPBYw6rEfxTX3aDMMrKha1Ii9E x3c81xrZqyXguvzmtSohsTf403JX/tdPInB/uq49OdjRCVJhyAnqHdNeJADWFPImdni0 isPWBWoJ1QrOYaTkpnP891w19UfgMgghA+4ldPtIHngxlysgz6zQzVzMLPcAD4XhJdgK FoeVbgxCkbQ5+BEt0LuLr3Bu3LtGjXt/x4DSnCEifqxUFLcgbYvmxBJdqOZ/iQBa7NE0 PrO/qJ74GQcs95fv6JUIIAnu3ZQCiawW9RGy+dnrt+fdvH5gOl8E2hekp8VairKQf6hu 6TSA== X-Gm-Message-State: ACgBeo0acNnF3S9kXM45oGkJiejXe4kyFc7Eky2eTdBFPyQnkyF/VrAO TqimAkCvEMDLvc3CaNFRc/rRBA== X-Google-Smtp-Source: AA6agR6c3BWpnUQVxFxjRibelrfprWBVhfqxKGbvkcAwSrUBuomuf1pEeB07Lko0rRWxPXPmprNriA== X-Received: by 2002:a17:907:60c7:b0:731:4b42:4e3e with SMTP id hv7-20020a17090760c700b007314b424e3emr30347050ejc.236.1662213086739; Sat, 03 Sep 2022 06:51:26 -0700 (PDT) Received: from hera ([46.103.15.185]) by smtp.gmail.com with ESMTPSA id lb11-20020a170907784b00b0073d6d6e698bsm2537257ejc.187.2022.09.03.06.51.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Sep 2022 06:51:25 -0700 (PDT) Date: Sat, 3 Sep 2022 16:51:23 +0300 From: Ilias Apalodimas To: Simon Glass Cc: U-Boot Mailing List , Heinrich Schuchardt , Masahisa Kojima , Ruchika Gupta Subject: Re: [PATCH v4 6/7] tpm: Implement state command for Cr50 Message-ID: References: <20220823171240.386269-1-sjg@chromium.org> <20220823171240.386269-7-sjg@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220823171240.386269-7-sjg@chromium.org> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean On Tue, Aug 23, 2022 at 10:12:39AM -0700, Simon Glass wrote: > Add a vendor-specific TPM2 command for this and implement it for Cr50. > Note: This is not part of the TPM spec, but is a Cr50 extension. > > Signed-off-by: Simon Glass > --- > > Changes in v4: > - Rename function and add arguments for the command/subcmd > > drivers/tpm/cr50_i2c.c | 163 +++++++++++++++++++++++++++++++++++++++++ > include/tpm-v2.h | 13 ++++ > lib/tpm-v2.c | 25 +++++++ > 3 files changed, 201 insertions(+) > > diff --git a/drivers/tpm/cr50_i2c.c b/drivers/tpm/cr50_i2c.c > index f8c30878947..08d417a844c 100644 > --- a/drivers/tpm/cr50_i2c.c > +++ b/drivers/tpm/cr50_i2c.c > @@ -13,11 +13,13 @@ > #include > #include > #include > +#include > #include > #include > #include > #include > #include > +#include > #include > #include > > @@ -37,6 +39,50 @@ enum { > CR50_MAX_BUF_SIZE = 63, > }; > > +/* > + * Operations specific to the Cr50 TPM used on Chromium OS and Android devices > + * > + * FIXME: below is not enough to differentiate between vendors commands > + * of numerous devices. However, the current tpm2 APIs aren't very amenable > + * to extending generically because the marshaling code is assuming all > + * knowledge of all commands. > + */ > +#define TPM2_CC_VENDOR_BIT_MASK 0x20000000 > + > +#define TPM2_CR50_VENDOR_COMMAND (TPM2_CC_VENDOR_BIT_MASK | 0) > +#define TPM2_CR50_SUB_CMD_IMMEDIATE_RESET 19 > +#define TPM2_CR50_SUB_CMD_NVMEM_ENABLE_COMMITS 21 > +#define TPM2_CR50_SUB_CMD_REPORT_TPM_STATE 23 > +#define TPM2_CR50_SUB_CMD_TURN_UPDATE_ON 24 > +#define TPM2_CR50_SUB_CMD_GET_REC_BTN 29 > +#define TPM2_CR50_SUB_CMD_TPM_MODE 40 > +#define TPM2_CR50_SUB_CMD_GET_BOOT_MODE 52 > +#define TPM2_CR50_SUB_CMD_RESET_EC 53 > + > +/* Cr50 vendor-specific error codes. */ > +#define VENDOR_RC_ERR 0x00000500 > +enum cr50_vendor_rc { > + VENDOR_RC_INTERNAL_ERROR = (VENDOR_RC_ERR | 6), > + VENDOR_RC_NO_SUCH_SUBCOMMAND = (VENDOR_RC_ERR | 8), > + VENDOR_RC_NO_SUCH_COMMAND = (VENDOR_RC_ERR | 127), > +}; > + > +enum cr50_tpm_mode { > + /* > + * Default state: TPM is enabled, and may be set to either > + * TPM_MODE_ENABLED or TPM_MODE_DISABLED. > + */ > + TPM_MODE_ENABLED_TENTATIVE = 0, > + > + /* TPM is enabled, and mode may not be changed. */ > + TPM_MODE_ENABLED = 1, > + > + /* TPM is disabled, and mode may not be changed. */ > + TPM_MODE_DISABLED = 2, > + > + TPM_MODE_INVALID, > +}; > + > /** > * struct cr50_priv - Private driver data > * > @@ -54,6 +100,41 @@ struct cr50_priv { > bool use_irq; > }; > > +/* > + * The below structure represents the body of the response to the 'report tpm > + * state' vendor command. > + * > + * It is transferred over the wire, so it needs to be serialized/deserialized, > + * and it is likely to change, so its contents must be versioned. > + */ > +#define TPM_STATE_VERSION 1 > +struct tpm_vendor_state { > + u32 version; > + /* > + * The following three fields are set by the TPM in case of an assert. > + * There is no other processing than setting the source code line > + * number, error code and the first 4 characters of the function name. > + * > + * We don't expect this happening, but it is included in the report > + * just in case. > + */ > + u32 fail_line; /* s_failLIne */ > + u32 fail_code; /* s_failCode */ > + char func_name[4]; /* s_failFunction, limited to 4 chars */ > + > + /* > + * The following two fields are the current time filtered value of the > + * 'failed tries' TPM counter, and the maximum allowed value of the > + * counter. > + * > + * failed_tries == max_tries is the definition of the TPM lockout > + * condition. > + */ > + u32 failed_tries; /* gp.failedTries */ > + u32 max_tries; /* gp.maxTries */ > + /* The below fields are present in version 2 and above */ > +}; > + > /* Wait for interrupt to indicate TPM is ready */ > static int cr50_i2c_wait_tpm_ready(struct udevice *dev) > { > @@ -573,6 +654,87 @@ static int cr50_i2c_get_desc(struct udevice *dev, char *buf, int size) > return len; > } > > +static int stringify_state(char *buf, int len, char *str, size_t max_size) > +{ > + struct tpm_vendor_state state; > + size_t text_size = 0; > + > + state.version = get_unaligned_be32(buf + > + offsetof(struct tpm_vendor_state, version)); > + state.fail_line = get_unaligned_be32(buf + > + offsetof(struct tpm_vendor_state, fail_line)); > + state.fail_code = get_unaligned_be32(buf + > + offsetof(struct tpm_vendor_state, fail_code)); > + memcpy(state.func_name, > + buf + offsetof(struct tpm_vendor_state, func_name), > + sizeof(state.func_name)); > + state.failed_tries = get_unaligned_be32(buf + > + offsetof(struct tpm_vendor_state, failed_tries)); > + state.max_tries = get_unaligned_be32(buf + > + offsetof(struct tpm_vendor_state, max_tries)); > + > + text_size += snprintf(str + text_size, max_size - text_size, > + "v=%d", state.version); > + if (text_size >= max_size) > + return -ENOSPC; > + > + if (state.version > TPM_STATE_VERSION) > + text_size += snprintf(str + text_size, > + max_size - text_size, > + " not fully supported\n"); > + if (text_size >= max_size) > + return -ENOSPC; > + > + if (state.version == 0) > + return -EINVAL; /* This should never happen */ > + > + text_size += snprintf(str + text_size, > + max_size - text_size, > + " failed_tries=%d max_tries=%d\n", > + state.failed_tries, state.max_tries); > + if (text_size >= max_size) > + return -ENOSPC; > + > + if (state.fail_line) { > + /* make sure function name is zero terminated. */ > + char func_name[sizeof(state.func_name) + 1]; > + > + memcpy(func_name, state.func_name, sizeof(state.func_name)); > + func_name[sizeof(state.func_name)] = '\0'; > + > + text_size += snprintf(str + text_size, > + max_size - text_size, > + "tpm failed: f %s line %d code %d", > + func_name, > + state.fail_line, > + state.fail_code); > + if (text_size >= max_size) > + return -ENOSPC; > + } > + > + return 0; > +} > + > +static int cr50_i2c_report_state(struct udevice *dev, char *str, int str_max) > +{ > + char buf[50]; > + int buf_size = sizeof(buf); > + int ret; > + > + ret = tpm2_report_state(dev, TPM2_CR50_VENDOR_COMMAND, > + TPM2_CR50_SUB_CMD_REPORT_TPM_STATE, > + buf, &buf_size); > + if (ret) > + return ret; > + > + /* TPM responded as expected */ > + ret = stringify_state(buf, buf_size, str, str_max); > + if (ret) > + return ret; > + > + return 0; > +} > + > static int cr50_i2c_open(struct udevice *dev) > { > char buf[80]; > @@ -730,6 +892,7 @@ struct acpi_ops cr50_acpi_ops = { > static const struct tpm_ops cr50_i2c_ops = { > .open = cr50_i2c_open, > .get_desc = cr50_i2c_get_desc, > + .report_state = cr50_i2c_report_state, > .send = cr50_i2c_send, > .recv = cr50_i2c_recv, > .cleanup = cr50_i2c_cleanup, > diff --git a/include/tpm-v2.h b/include/tpm-v2.h > index e79c90b9395..36c6ac0be6e 100644 > --- a/include/tpm-v2.h > +++ b/include/tpm-v2.h > @@ -658,4 +658,17 @@ u32 tpm2_disable_platform_hierarchy(struct udevice *dev); > u32 tpm2_submit_command(struct udevice *dev, const u8 *sendbuf, > u8 *recvbuf, size_t *recv_size); > > +/** > + * tpm_cr50_report_state() - Report the Cr50 internal state > + * > + * @dev: TPM device > + * @vendor_cmd: Vendor command number to send > + * @vendor_subcmd: Vendor sub-command number to send > + * @recvbuf: Buffer to save the response to > + * @recv_size: Pointer to the size of the response buffer > + * Return: result of the operation > + */ > +u32 tpm2_report_state(struct udevice *dev, uint vendor_cmd, uint vendor_subcmd, > + u8 *recvbuf, size_t *recv_size); > + > #endif /* __TPM_V2_H */ > diff --git a/lib/tpm-v2.c b/lib/tpm-v2.c > index 3e240bb4c67..edee9854a7c 100644 > --- a/lib/tpm-v2.c > +++ b/lib/tpm-v2.c > @@ -679,3 +679,28 @@ u32 tpm2_submit_command(struct udevice *dev, const u8 *sendbuf, > { > return tpm_sendrecv_command(dev, sendbuf, recvbuf, recv_size); > } > + > +u32 tpm2_report_state(struct udevice *dev, uint vendor_cmd, uint vendor_subcmd, > + u8 *recvbuf, size_t *recv_size) > +{ > + u8 command_v2[COMMAND_BUFFER_SIZE] = { > + /* header 10 bytes */ > + tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */ > + tpm_u32(10 + 2), /* Length */ > + tpm_u32(vendor_cmd), /* Command code */ > + > + tpm_u16(vendor_subcmd), > + }; > + int ret; > + > + ret = tpm_sendrecv_command(dev, command_v2, recvbuf, recv_size); > + log_debug("ret=%s, %x\n", dev->name, ret); > + if (ret) > + return ret; > + if (*recv_size < 12) > + return -ENODATA; > + *recv_size -= 12; > + memcpy(recvbuf, recvbuf + 12, *recv_size); > + > + return 0; > +} > -- > 2.37.2.609.g9ff673ca1a-goog > Reviewed-by: Ilias Apalodimas