From patchwork Mon Jul 4 13:34:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 587140 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:1ec:0:0:0:0 with SMTP id 12csp839484map; Mon, 4 Jul 2022 06:35:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t2Va/6Yz3o9EGPkI2RPf1mN4yHEIyFRLAZdKzB/S/lWgw5sZzT6k8OyK0vwHp0TlpMHzf9 X-Received: by 2002:a05:6512:3593:b0:47f:879a:56d1 with SMTP id m19-20020a056512359300b0047f879a56d1mr19323039lfr.36.1656941741755; Mon, 04 Jul 2022 06:35:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656941741; cv=none; d=google.com; s=arc-20160816; b=AVFqfyen0fNpwVSNrTgTWDpW4CSEgdtdC9Wxp5FgTviXU+UdiKlJY+7kPMQv72fugQ FK69dNGR/6K+Qov5KyBbLsE7BOGpmjSrmvyz3I84wz3ou9LLaheHmMnpVzNKzLrrhSV3 oT9JG0M+XS4fNsHIzpqneiTLzrsaJZP6ci5xCfFRa9thi34pQUA2qnLD0bbhHp9DIeoS kqQcVL9df7MgXaXGXTpRLkNIU4VguTmmALn24hoct6m/FoYaCn0dHYGJMRmpH1Nf2KCd jm09bJrOFr3VWBORzcYPrTQuvki6YkVLM2fnHKmaLES3dR3AsM2rAyhl9tesAdaTViqz +O4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=cQVb9H/zS5exwaVayh3t1CZ4aLcFajLiD1wnBbfJdto=; b=hCVzNEifK/D4j9dhlMnFi+8cecAOeiS2uQ01zeWld1sslkROvCFJMrba3wgAbbJkpX efpGsQYnaGJ+BwUL4RpHO1+ctXc1RXViW1CFc2EVZtBdmkAUiiIiHdMrlUsbWVgZ8H9U VhqazM3DfhccKkSSTKO54DXsrebAcEqrSnvOtSZzSeDyAP91PKhb9AhGkRB2k2vqzCJG u9Q9n5cD4SRbANS5rXtSkp/6q0WgAAVErsECw8qWGfAIOqSZwC9RpznO5a2+qTiOgnGf SeIvTK2T1GnwJ5BjgQtDqVv6Nu8hG1vAI1gAw7mQLw+qbXvyfP8Z1++/JnRVpFj6uHzW R+Nw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id w10-20020a2e998a000000b0025a7c067a10si27053937lji.98.2022.07.04.06.35.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 06:35:41 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A3D2E8449C; Mon, 4 Jul 2022 15:35:22 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id DA54C844A8; Mon, 4 Jul 2022 15:35:20 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_SOFTFAIL,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by phobos.denx.de (Postfix) with ESMTP id 63F2584307 for ; Mon, 4 Jul 2022 15:35:17 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sughosh.ganu@linaro.org Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0B16723A; Mon, 4 Jul 2022 06:35:17 -0700 (PDT) Received: from a076522.blr.arm.com (a076522.blr.arm.com [10.162.16.44]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D1C583F66F; Mon, 4 Jul 2022 06:35:14 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Simon Glass , Tom Rini , Sughosh Ganu Subject: [PATCH v6 2/7] tpm: rng: Add driver model interface for TPM RNG device Date: Mon, 4 Jul 2022 19:04:39 +0530 Message-Id: <20220704133444.1110715-3-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220704133444.1110715-1-sughosh.ganu@linaro.org> References: <20220704133444.1110715-1-sughosh.ganu@linaro.org> MIME-Version: 1.0 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 The TPM device has a builtin random number generator(RNG) functionality. Expose the RNG functions of the TPM device to the driver model so that they can be used by the EFI_RNG_PROTOCOL if the protocol is installed. Also change the function arguments and return type of the random number functions to comply with the driver model api. Signed-off-by: Sughosh Ganu --- Changes since V5: * Use the dev_get_parent() interface for getting the TPM device when calling the tpm_get_random() function drivers/rng/Kconfig | 11 +++++++++++ drivers/rng/Makefile | 1 + drivers/rng/tpm_rng.c | 23 +++++++++++++++++++++++ lib/Kconfig | 1 + lib/tpm-v1.c | 13 +++++++------ lib/tpm-v2.c | 6 +++--- lib/tpm_api.c | 6 +++--- 7 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 drivers/rng/tpm_rng.c diff --git a/drivers/rng/Kconfig b/drivers/rng/Kconfig index c10f7d345b..67c65311c7 100644 --- a/drivers/rng/Kconfig +++ b/drivers/rng/Kconfig @@ -58,4 +58,15 @@ config RNG_IPROC200 depends on DM_RNG help Enable random number generator for RPI4. + +config TPM_RNG + bool "Enable random number generator on TPM device" + depends on TPM + default y + help + The TPM device has an inbuilt random number generator + functionality. Enable random number generator on TPM + devices. + + endif diff --git a/drivers/rng/Makefile b/drivers/rng/Makefile index 435b3b965a..e4ca9c4149 100644 --- a/drivers/rng/Makefile +++ b/drivers/rng/Makefile @@ -11,3 +11,4 @@ obj-$(CONFIG_RNG_OPTEE) += optee_rng.o obj-$(CONFIG_RNG_STM32MP1) += stm32mp1_rng.o obj-$(CONFIG_RNG_ROCKCHIP) += rockchip_rng.o obj-$(CONFIG_RNG_IPROC200) += iproc_rng200.o +obj-$(CONFIG_TPM_RNG) += tpm_rng.o diff --git a/drivers/rng/tpm_rng.c b/drivers/rng/tpm_rng.c new file mode 100644 index 0000000000..1a5e9e2e4b --- /dev/null +++ b/drivers/rng/tpm_rng.c @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2022, Linaro Limited + */ + +#include +#include +#include + +static int rng_tpm_random_read(struct udevice *dev, void *data, size_t count) +{ + return tpm_get_random(dev_get_parent(dev), data, count); +} + +static const struct dm_rng_ops tpm_rng_ops = { + .read = rng_tpm_random_read, +}; + +U_BOOT_DRIVER(tpm_rng) = { + .name = "tpm-rng", + .id = UCLASS_RNG, + .ops = &tpm_rng_ops, +}; diff --git a/lib/Kconfig b/lib/Kconfig index acc0ac081a..17efaa4c80 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -358,6 +358,7 @@ source lib/crypt/Kconfig config TPM bool "Trusted Platform Module (TPM) Support" depends on DM + imply DM_RNG help This enables support for TPMs which can be used to provide security features for your board. The TPM can be connected via LPC or I2C diff --git a/lib/tpm-v1.c b/lib/tpm-v1.c index 22a769c587..f7091e5bc7 100644 --- a/lib/tpm-v1.c +++ b/lib/tpm-v1.c @@ -9,12 +9,13 @@ #include #include #include -#include -#include #include #include #include "tpm-utils.h" +#include +#include + #ifdef CONFIG_TPM_AUTH_SESSIONS #ifndef CONFIG_SHA1 @@ -892,19 +893,19 @@ u32 tpm1_get_random(struct udevice *dev, void *data, u32 count) if (pack_byte_string(buf, sizeof(buf), "sd", 0, command, sizeof(command), length_offset, this_bytes)) - return TPM_LIB_ERROR; + return -EIO; err = tpm_sendrecv_command(dev, buf, response, &response_length); if (err) return err; if (unpack_byte_string(response, response_length, "d", data_size_offset, &data_size)) - return TPM_LIB_ERROR; + return -EIO; if (data_size > count) - return TPM_LIB_ERROR; + return -EIO; if (unpack_byte_string(response, response_length, "s", data_offset, out, data_size)) - return TPM_LIB_ERROR; + return -EIO; count -= data_size; out += data_size; diff --git a/lib/tpm-v2.c b/lib/tpm-v2.c index 1bf627853a..abca9a14b0 100644 --- a/lib/tpm-v2.c +++ b/lib/tpm-v2.c @@ -585,19 +585,19 @@ u32 tpm2_get_random(struct udevice *dev, void *data, u32 count) if (pack_byte_string(buf, sizeof(buf), "sw", 0, command_v2, sizeof(command_v2), sizeof(command_v2), this_bytes)) - return TPM_LIB_ERROR; + return -EIO; err = tpm_sendrecv_command(dev, buf, response, &response_length); if (err) return err; if (unpack_byte_string(response, response_length, "w", data_size_offset, &data_size)) - return TPM_LIB_ERROR; + return -EIO; if (data_size > this_bytes) return TPM_LIB_ERROR; if (unpack_byte_string(response, response_length, "s", data_offset, out, data_size)) - return TPM_LIB_ERROR; + return -EIO; count -= data_size; out += data_size; diff --git a/lib/tpm_api.c b/lib/tpm_api.c index 4ac4612c81..032f383ca0 100644 --- a/lib/tpm_api.c +++ b/lib/tpm_api.c @@ -269,7 +269,7 @@ u32 tpm_get_random(struct udevice *dev, void *data, u32 count) if (tpm_is_v1(dev)) return tpm1_get_random(dev, data, count); else if (tpm_is_v2(dev)) - return -ENOSYS; /* not implemented yet */ - else - return -ENOSYS; + return tpm2_get_random(dev, data, count); + + return -ENOSYS; }