From patchwork Wed Jul 20 12:29:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 591862 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5817:0:0:0:0 with SMTP id j23csp4893083max; Wed, 20 Jul 2022 05:30:40 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uibILlgQgvejnEbWEP3TF4XmyQcSYrHLtiJcZtfEjfhS6u8JLBHKkZn/7pPKcOo9XceWO1 X-Received: by 2002:a05:6638:150e:b0:33f:8717:9fc5 with SMTP id b14-20020a056638150e00b0033f87179fc5mr18880129jat.205.1658320240480; Wed, 20 Jul 2022 05:30:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658320240; cv=none; d=google.com; s=arc-20160816; b=B4J+tC4W6Ert9XiaDGW5S05MA3ciQx+NapkrXHwkmGqlTKc12Dq6mcBjkYa6N3BgWo mIBRb+XfHToQwWQ9zGV0CN1iqn6qib3AL0OsUbTHq4ZPrgTAoLHROUohMMKSamMyO7Li Kn3zNWlJCDlc26u4mY9/0vL1ZC28fbgG9dBz1QKEQc18rAq/5j0YA+BUhf1a0QmdUu8s UOfji9+EJACroRxFeD71O+U/25gTbRgUK2IyL1WfHa2cofkq2Dpai1UNFu7DiTfnP+lP TYIWBcDAkp+0vDNJJrXExvjFcE0Fc7+bar1vp5adPNiADHqjInLPb5HqAEsI48FpzSMU n5Rw== 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=2fhoVrtzs6Aptg/TI7morIVw/eY2QKos9W6/a/B8wKQ=; b=WeQ6b3dIFf+WbaCOkT7wwmIuYeBm7zlXB+DLI75YV1IuME9wjYXos5le3JMDZ611fy S+vh/K78ck12wcZxYoqS2wKhBAD9cFgNXCVfznMYZQvM9fI4Dm1VNwt34td71zyNljiW wo1I8ToLG2gJx/2sdfO6dIOcWQVn5O9x7fhHWQ2cahl1H8hXHZIqkvXOn5VE9Rx92l7z MWZHztmCama5FltDwW4LkY9SBXhhy1H+N21dnnCZMyhrxyk9vRpC4Ox+M/I2jskRTbAH dHCFyBm8+Z6AJ9soMI+n+7Sb91oM5TotcwT1HGDTSqFwJrWCrZokBNfL0KfpIzpBwZpn S93A== 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 b190-20020a0219c7000000b0034166823f5esi9141822jab.17.2022.07.20.05.30.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Jul 2022 05:30:40 -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 E5476840A6; Wed, 20 Jul 2022 14:30:18 +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 ECE9E84079; Wed, 20 Jul 2022 14:30:10 +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 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 ED1C680361 for ; Wed, 20 Jul 2022 14:30:07 +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 D48861576; Wed, 20 Jul 2022 05:30:07 -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 62E3A3F73D; Wed, 20 Jul 2022 05:30:05 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Simon Glass , Tom Rini , Sughosh Ganu Subject: [PATCH v7 2/7] tpm: rng: Add driver model interface for TPM RNG device Date: Wed, 20 Jul 2022 17:59:21 +0530 Message-Id: <20220720122926.415659-3-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220720122926.415659-1-sughosh.ganu@linaro.org> References: <20220720122926.415659-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 Reviewed-by: Ilias Apalodimas --- Changes since V6: * Remove the changes made in tpm-v[12].c to return -EIO instead of TPM_LIB_ERROR as suggested by Simon drivers/rng/Kconfig | 9 +++++++++ drivers/rng/Makefile | 1 + drivers/rng/tpm_rng.c | 23 +++++++++++++++++++++++ lib/Kconfig | 1 + lib/tpm_api.c | 6 +++--- 5 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 drivers/rng/tpm_rng.c diff --git a/drivers/rng/Kconfig b/drivers/rng/Kconfig index 21a9ff0195..16143681da 100644 --- a/drivers/rng/Kconfig +++ b/drivers/rng/Kconfig @@ -74,4 +74,13 @@ config RNG_SMCCC_TRNG Enable random number generator for platforms that support Arm SMCCC TRNG interface. +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 2494717d7c..78f61051ac 100644 --- a/drivers/rng/Makefile +++ b/drivers/rng/Makefile @@ -13,3 +13,4 @@ obj-$(CONFIG_RNG_STM32MP1) += stm32mp1_rng.o obj-$(CONFIG_RNG_ROCKCHIP) += rockchip_rng.o obj-$(CONFIG_RNG_IPROC200) += iproc_rng200.o obj-$(CONFIG_RNG_SMCCC_TRNG) += smccc_trng.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 7dd777b56a..e888c29245 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -360,6 +360,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_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; }