From patchwork Mon Jul 4 13:34:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 587138 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:1ec:0:0:0:0 with SMTP id 12csp839209map; Mon, 4 Jul 2022 06:35:22 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uKbMIZWDVDJ0uO81POcE2SquvepEyUCMcjxpH/ys4Z0X6pryaks7cqd1yAzV9Yympe9hZl X-Received: by 2002:a05:6512:2814:b0:481:981:23d7 with SMTP id cf20-20020a056512281400b00481098123d7mr20200687lfb.75.1656941721998; Mon, 04 Jul 2022 06:35:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656941721; cv=none; d=google.com; s=arc-20160816; b=txvGzadKfUIu8niWk1UvqdBXIDFqBr8LtlGxECmJNOeN17EltGYBnxzFbRncpupEax V/Wx/3RSQD6QEYKu55O15y6Kfaqdn27ju/Flf74CV55g03nBQdtsca86flrBSLJsDfsu kMOrZeKQ4kJRISLeYbrLbdzr6HszG7glGUWr9jvfgcAmxGdPr45LwZGYoEHSy6R2ab89 plrzXLnCVkQHUe13lg0qhJSvC98QZeHQ6YsvZnrQMLpNo1WMd6FeAjVWYkcf4PD8WYYt KNnB16ytO0GSjgdMoUzXfTaDQ6N6iBdAb1Q6N3QYZNq6jN9hvVa05RfGeArjv0zHfBZq 1+/g== 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=MKv/mzCBKX+nOLoynGEKASPI+NdzBkClTRIWq7+4mGw=; b=d2Tp30avp23l8rVh0e0lalXfWhfo1aaW8Lk0ZB91T1795rzWBGBLRKwVmd9lvlNhZw Hgxj3Su8sqF4e3+Oc5dXyGQ6GjAC+LHBOQA4O5VQGA+WvwOfBpI01XXFm+m5+Oei3Arr woJLiLTeoQ45JCEm+pCmFhz+06xpZF0k6MwWglar49p7YCc30S2LDu3nDF9jow1B33pG rd2rfeXUCh0vy6ZylqFvy5azlUmi29IZ8kMKgA9XMcKHMP10Vyagq0B/2E+/WOx1y/Fy 8DoUye3jB6tYoNjWKogvKsCGJyuiTrxMbkF4M7r2nqiWV7zSFse2aiTezCW7/wq/B/Az el2g== 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 b5-20020a0565120b8500b0047fa93d8fd9si20812349lfv.532.2022.07.04.06.35.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 06:35:21 -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 6846C8443D; Mon, 4 Jul 2022 15:35:19 +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 385F3844B2; Mon, 4 Jul 2022 15:35:18 +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 30F22844B1 for ; Mon, 4 Jul 2022 15:35:15 +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 AC26A23A; Mon, 4 Jul 2022 06:35:14 -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 7E61D3F66F; Mon, 4 Jul 2022 06:35:12 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Simon Glass , Tom Rini Subject: [PATCH v6 1/7] tpm: Export the TPM-version functions Date: Mon, 4 Jul 2022 19:04:38 +0530 Message-Id: <20220704133444.1110715-2-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 From: Simon Glass These functions should really be available outside the TPM code, so that other callers can find out which version the TPM is. Rename them to have a tpm_ prefix() and add them to the header file. Signed-off-by: Simon Glass Reviewed-by: Ilias Apalodimas --- Changes since V5: None include/tpm_api.h | 10 ++++++ lib/tpm_api.c | 92 +++++++++++++++++++++-------------------------- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/include/tpm_api.h b/include/tpm_api.h index ef45b43a8f..11aa14eb79 100644 --- a/include/tpm_api.h +++ b/include/tpm_api.h @@ -319,4 +319,14 @@ u32 tpm_write_lock(struct udevice *dev, u32 index); */ u32 tpm_resume(struct udevice *dev); +static inline bool tpm_is_v1(struct udevice *dev) +{ + return IS_ENABLED(CONFIG_TPM_V1) && tpm_get_version(dev) == TPM_V1; +} + +static inline bool tpm_is_v2(struct udevice *dev) +{ + return IS_ENABLED(CONFIG_TPM_V2) && tpm_get_version(dev) == TPM_V2; +} + #endif /* __TPM_API_H */ diff --git a/lib/tpm_api.c b/lib/tpm_api.c index 4c662640a9..4ac4612c81 100644 --- a/lib/tpm_api.c +++ b/lib/tpm_api.c @@ -11,21 +11,11 @@ #include #include -static bool is_tpm1(struct udevice *dev) -{ - return IS_ENABLED(CONFIG_TPM_V1) && tpm_get_version(dev) == TPM_V1; -} - -static bool is_tpm2(struct udevice *dev) -{ - return IS_ENABLED(CONFIG_TPM_V2) && tpm_get_version(dev) == TPM_V2; -} - u32 tpm_startup(struct udevice *dev, enum tpm_startup_type mode) { - if (is_tpm1(dev)) { + if (tpm_is_v1(dev)) { return tpm1_startup(dev, mode); - } else if (is_tpm2(dev)) { + } else if (tpm_is_v2(dev)) { enum tpm2_startup_types type; switch (mode) { @@ -47,9 +37,9 @@ u32 tpm_startup(struct udevice *dev, enum tpm_startup_type mode) u32 tpm_resume(struct udevice *dev) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_startup(dev, TPM_ST_STATE); - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return tpm2_startup(dev, TPM2_SU_STATE); else return -ENOSYS; @@ -57,9 +47,9 @@ u32 tpm_resume(struct udevice *dev) u32 tpm_self_test_full(struct udevice *dev) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_self_test_full(dev); - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return tpm2_self_test(dev, TPMI_YES); else return -ENOSYS; @@ -67,9 +57,9 @@ u32 tpm_self_test_full(struct udevice *dev) u32 tpm_continue_self_test(struct udevice *dev) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_continue_self_test(dev); - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return tpm2_self_test(dev, TPMI_NO); else return -ENOSYS; @@ -86,7 +76,7 @@ u32 tpm_clear_and_reenable(struct udevice *dev) return ret; } - if (is_tpm1(dev)) { + if (tpm_is_v1(dev)) { ret = tpm1_physical_enable(dev); if (ret != TPM_SUCCESS) { log_err("TPM: Can't set enabled state\n"); @@ -105,9 +95,9 @@ u32 tpm_clear_and_reenable(struct udevice *dev) u32 tpm_nv_enable_locking(struct udevice *dev) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_nv_define_space(dev, TPM_NV_INDEX_LOCK, 0, 0); - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return -ENOSYS; else return -ENOSYS; @@ -115,9 +105,9 @@ u32 tpm_nv_enable_locking(struct udevice *dev) u32 tpm_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_nv_read_value(dev, index, data, count); - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return tpm2_nv_read_value(dev, index, data, count); else return -ENOSYS; @@ -126,9 +116,9 @@ u32 tpm_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count) u32 tpm_nv_write_value(struct udevice *dev, u32 index, const void *data, u32 count) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_nv_write_value(dev, index, data, count); - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return tpm2_nv_write_value(dev, index, data, count); else return -ENOSYS; @@ -141,9 +131,9 @@ u32 tpm_set_global_lock(struct udevice *dev) u32 tpm_write_lock(struct udevice *dev, u32 index) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return -ENOSYS; - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return tpm2_write_lock(dev, index); else return -ENOSYS; @@ -152,9 +142,9 @@ u32 tpm_write_lock(struct udevice *dev, u32 index) u32 tpm_pcr_extend(struct udevice *dev, u32 index, const void *in_digest, void *out_digest) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_extend(dev, index, in_digest, out_digest); - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return tpm2_pcr_extend(dev, index, TPM2_ALG_SHA256, in_digest, TPM2_DIGEST_LEN); else @@ -163,9 +153,9 @@ u32 tpm_pcr_extend(struct udevice *dev, u32 index, const void *in_digest, u32 tpm_pcr_read(struct udevice *dev, u32 index, void *data, size_t count) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_pcr_read(dev, index, data, count); - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return -ENOSYS; else return -ENOSYS; @@ -173,14 +163,14 @@ u32 tpm_pcr_read(struct udevice *dev, u32 index, void *data, size_t count) u32 tpm_tsc_physical_presence(struct udevice *dev, u16 presence) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_tsc_physical_presence(dev, presence); /* * Nothing to do on TPM2 for this; use platform hierarchy availability * instead. */ - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return 0; else return -ENOSYS; @@ -188,11 +178,11 @@ u32 tpm_tsc_physical_presence(struct udevice *dev, u16 presence) u32 tpm_finalise_physical_presence(struct udevice *dev) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_finalise_physical_presence(dev); /* Nothing needs to be done with tpm2 */ - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return 0; else return -ENOSYS; @@ -200,9 +190,9 @@ u32 tpm_finalise_physical_presence(struct udevice *dev) u32 tpm_read_pubek(struct udevice *dev, void *data, size_t count) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_read_pubek(dev, data, count); - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return -ENOSYS; /* not implemented yet */ else return -ENOSYS; @@ -210,9 +200,9 @@ u32 tpm_read_pubek(struct udevice *dev, void *data, size_t count) u32 tpm_force_clear(struct udevice *dev) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_force_clear(dev); - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return tpm2_clear(dev, TPM2_RH_PLATFORM, NULL, 0); else return -ENOSYS; @@ -220,11 +210,11 @@ u32 tpm_force_clear(struct udevice *dev) u32 tpm_physical_enable(struct udevice *dev) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_physical_enable(dev); /* Nothing needs to be done with tpm2 */ - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return 0; else return -ENOSYS; @@ -232,11 +222,11 @@ u32 tpm_physical_enable(struct udevice *dev) u32 tpm_physical_disable(struct udevice *dev) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_physical_disable(dev); /* Nothing needs to be done with tpm2 */ - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return 0; else return -ENOSYS; @@ -244,10 +234,10 @@ u32 tpm_physical_disable(struct udevice *dev) u32 tpm_physical_set_deactivated(struct udevice *dev, u8 state) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_physical_set_deactivated(dev, state); /* Nothing needs to be done with tpm2 */ - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return 0; else return -ENOSYS; @@ -256,9 +246,9 @@ u32 tpm_physical_set_deactivated(struct udevice *dev, u8 state) u32 tpm_get_capability(struct udevice *dev, u32 cap_area, u32 sub_cap, void *cap, size_t count) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_get_capability(dev, cap_area, sub_cap, cap, count); - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return tpm2_get_capability(dev, cap_area, sub_cap, cap, count); else return -ENOSYS; @@ -266,9 +256,9 @@ u32 tpm_get_capability(struct udevice *dev, u32 cap_area, u32 sub_cap, u32 tpm_get_permissions(struct udevice *dev, u32 index, u32 *perm) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_get_permissions(dev, index, perm); - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return -ENOSYS; /* not implemented yet */ else return -ENOSYS; @@ -276,9 +266,9 @@ u32 tpm_get_permissions(struct udevice *dev, u32 index, u32 *perm) u32 tpm_get_random(struct udevice *dev, void *data, u32 count) { - if (is_tpm1(dev)) + if (tpm_is_v1(dev)) return tpm1_get_random(dev, data, count); - else if (is_tpm2(dev)) + else if (tpm_is_v2(dev)) return -ENOSYS; /* not implemented yet */ else return -ENOSYS; 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; } From patchwork Mon Jul 4 13:34:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 587141 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:1ec:0:0:0:0 with SMTP id 12csp839641map; Mon, 4 Jul 2022 06:35:52 -0700 (PDT) X-Google-Smtp-Source: AGRyM1urxF5k4ug2Bwz9mXLrnnOk7OUmDkU8R5Xd0ZDe/bJMg7aN64HyOtp/bgirRkXGmLUcft6n X-Received: by 2002:a05:6512:3d08:b0:47f:79a9:66f0 with SMTP id d8-20020a0565123d0800b0047f79a966f0mr18974660lfv.576.1656941752634; Mon, 04 Jul 2022 06:35:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656941752; cv=none; d=google.com; s=arc-20160816; b=UU6nipOZYzTla+ofvafEGwWsSjBSV13s71i2rnclLbnpxvZ+r7P9iZNIpqihjkgHzI h6gbyzl00Mg0Se2FrqzzmLzqBRGkF6jZpZm6PSIa4NpPYLIxkaqmaMkxDE0xDz8lNkpe lc4IuBx4z02b9eCTvxfE9pWbnmghw/otuHb1h1+Q1StXOAsZmNO4cpIFSGSpAcY3vRya vPEK2sgMUed6YqNgx5Jt9GqxdKUJikPY8gtTRWgOnlHa5Px6lKae7IJrrcclkjXWb3dT pjs/5uT49xTmnIgCMFH18zcu6eHbY1RyXrFeE8scTt9iDqxOCcTlFPVH1ZxUDHApLW7k +KJw== 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=jgxZ6Ri+4NcnnZzvnsgS3LS4abHkFReRMi9QV4jZTG8=; b=W2qzbhLtviXc+BaQiHwQEOrgVup7GIwHV0DZcrUMPap+xabDY9+SFsF+NhU7uvwJDS a8Sj795uy4K1L8ok98ENe25ejxziSB6o6DWK+tVvxYBC8HTLgzY1dFw8cY9wItu0Up96 IZ0F1djD9twxco56zopBGR4onEex+q/ijkYnAiJOXfBQVLCw3+xBKXK9RBfZWakMQfYx cqKWpJniyyiEGHIiyFdfj6PCM4TFzbFsx3/9FBZ/jp+zbZrk68BAxbWolJGOuYnK/aVV FUA88CfL1mum3CxFnmn/Bkj8HbJ28y1KRi0ZgkXad9NVCeQEhQUKCc09qf6eNCswaDNY FmWw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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. [85.214.62.61]) by mx.google.com with ESMTPS id w2-20020a05651234c200b004796750377fsi24937055lfr.220.2022.07.04.06.35.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 06:35:52 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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 2D8B9844C0; Mon, 4 Jul 2022 15:35:24 +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 52C2E844B9; Mon, 4 Jul 2022 15:35:22 +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 C3B5684499 for ; Mon, 4 Jul 2022 15:35:19 +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 5DA2023A; Mon, 4 Jul 2022 06:35:19 -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 2FF293F66F; Mon, 4 Jul 2022 06:35:16 -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 3/7] tpm: Add the RNG child device Date: Mon, 4 Jul 2022 19:04:40 +0530 Message-Id: <20220704133444.1110715-4-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 comes with the random number generator(RNG) functionality which is built into the TPM device. Add logic to add the RNG child device in the TPM uclass post probe callback. The RNG device can then be used to pass a set of random bytes to the linux kernel, need for address space randomisation through the EFI_RNG_PROTOCOL interface. Signed-off-by: Sughosh Ganu --- Changes since V5: * Check if the TPM RNG device has already been added, through a call to device_find_first_child_by_uclass() drivers/tpm/tpm-uclass.c | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/drivers/tpm/tpm-uclass.c b/drivers/tpm/tpm-uclass.c index f67fe1019b..e1f1ef01e1 100644 --- a/drivers/tpm/tpm-uclass.c +++ b/drivers/tpm/tpm-uclass.c @@ -11,10 +11,15 @@ #include #include #include +#include #include #include #include "tpm_internal.h" +#include + +#define TPM_RNG_DRV_NAME "tpm-rng" + int tpm_open(struct udevice *dev) { struct tpm_ops *ops = tpm_get_ops(dev); @@ -136,12 +141,36 @@ int tpm_xfer(struct udevice *dev, const uint8_t *sendbuf, size_t send_size, return 0; } +static int tpm_uclass_post_probe(struct udevice *dev) +{ + int ret; + const char *drv = TPM_RNG_DRV_NAME; + struct udevice *child; + + if (CONFIG_IS_ENABLED(TPM_RNG)) { + ret = device_find_first_child_by_uclass(dev, UCLASS_RNG, + &child); + + if (ret != -ENODEV) { + log_debug("RNG child already added to the TPM device\n"); + return ret; + } + + ret = device_bind_driver(dev, drv, "tpm-rng0", &child); + if (ret) + return log_msg_ret("bind", ret); + } + + return 0; +} + UCLASS_DRIVER(tpm) = { - .id = UCLASS_TPM, - .name = "tpm", - .flags = DM_UC_FLAG_SEQ_ALIAS, + .id = UCLASS_TPM, + .name = "tpm", + .flags = DM_UC_FLAG_SEQ_ALIAS, #if CONFIG_IS_ENABLED(OF_REAL) - .post_bind = dm_scan_fdt_dev, + .post_bind = dm_scan_fdt_dev, #endif + .post_probe = tpm_uclass_post_probe, .per_device_auto = sizeof(struct tpm_chip_priv), }; From patchwork Mon Jul 4 13:34:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 587142 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:1ec:0:0:0:0 with SMTP id 12csp839798map; Mon, 4 Jul 2022 06:36:03 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uWSroxnWoTVIaKSeiN3yD32zmgGhXbAI6st7OwZGqxdSoOVzaRnr2PzntXSU42tRhxyEnk X-Received: by 2002:a19:ee0e:0:b0:481:4727:be80 with SMTP id g14-20020a19ee0e000000b004814727be80mr16963786lfb.162.1656941763319; Mon, 04 Jul 2022 06:36:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656941763; cv=none; d=google.com; s=arc-20160816; b=nKQHChlJyCaKEeREpl0JNpN491b7jpCadHW7bCU/WipLNdVICL9xvI4+UAKMhr5sfA wxG36Lx4XlFV6K1CWOgAANaTO5uRepUMZdFluQ/DeCK3gUzTY7ECeVDhRhN+TSfjwdxn L1lrEInpg7mORnM8Dw/eFwfV7VMCd0/2ISgtRVCYQuzbEyF02QUSjuPbPejTTA/d2s4X qimEwkOIj1PaP1Vxnt6Kffb3BWFK3zt59kYgQ5nQcw8gkdY1lBtif2BStwcTvBqvyRPr /PGc3U2CKda8y4fjBvXXDvIBaHZD/Kpx0Uv8HKqRrJaoNZ49zCywuwEW5R4WKcJrYka4 ab0w== 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=Merauy5FzUzqyHNlodO9glQweghwtkB3IIBk4ECId04=; b=b6WbOdcSebWutEUR8eKtWWWN9NAUbr9fwuoNq0cbOeEIbGW4hUrV0TEhlgKThGZUdP VbAq14mtdvjQ9Rw+JxEJXKLOrzP0I/8/Yt/F75DXpCbj565nmtbHV/uMIwxN4XIIfYdT r2ykOmaaK6t4dKQWw0BldP8Jfty4zN0Ewf3JnbhlU5vW3mo9sutLGYE69kebBODOOpYM on961v3yFu5hGcOiT0D5PRU3WHw3T3TcdkYkqCkTd2X2JYq/X57OcQSy6dgvFP1uOHEF SP6rVAXIqKJBz4xsFy0/beGM4BP1BjppmQMAHQen7jN8pugkvIjnlaRsy1HEHrjiZZh6 dVNA== 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 b24-20020a0565120b9800b00478efadf891si15668042lfv.434.2022.07.04.06.36.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 06:36:03 -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 E9CC6844CB; Mon, 4 Jul 2022 15:35:25 +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 6FD1D844C7; Mon, 4 Jul 2022 15:35:24 +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 EBB23844AE for ; Mon, 4 Jul 2022 15:35:21 +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 B02D223A; Mon, 4 Jul 2022 06:35:21 -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 8290A3F66F; Mon, 4 Jul 2022 06:35:19 -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 4/7] cmd: rng: Add support for selecting RNG device Date: Mon, 4 Jul 2022 19:04:41 +0530 Message-Id: <20220704133444.1110715-5-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 'rng' u-boot command is used for printing a select number of random bytes on the console. Currently, the RNG device from which the random bytes are read is fixed. However, a platform can have multiple RNG devices, one example being qemu, which has a virtio RNG device and the RNG pseudo device through the TPM chip. Extend the 'rng' command so that the user can provide the RNG device number from which the random bytes are to be read. This will be the device index under the RNG uclass. Tested-by: Heinrich Schuchardt Reviewed-by: Ilias Apalodimas Signed-off-by: Sughosh Ganu --- Changes since V5: None cmd/rng.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/cmd/rng.c b/cmd/rng.c index 1ad5a096c0..2ddf27545f 100644 --- a/cmd/rng.c +++ b/cmd/rng.c @@ -13,19 +13,34 @@ static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { - size_t n = 0x40; + size_t n; struct udevice *dev; void *buf; + int devnum; int ret = CMD_RET_SUCCESS; - if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) { + switch (argc) { + case 1: + devnum = 0; + n = 0x40; + break; + case 2: + devnum = hextoul(argv[1], NULL); + n = 0x40; + break; + case 3: + devnum = hextoul(argv[1], NULL); + n = hextoul(argv[2], NULL); + break; + default: + return CMD_RET_USAGE; + } + + if (uclass_get_device_by_seq(UCLASS_RNG, devnum, &dev) || !dev) { printf("No RNG device\n"); return CMD_RET_FAILURE; } - if (argc >= 2) - n = hextoul(argv[1], NULL); - buf = malloc(n); if (!buf) { printf("Out of memory\n"); @@ -46,12 +61,12 @@ static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) #ifdef CONFIG_SYS_LONGHELP static char rng_help_text[] = - "[n]\n" - " - print n random bytes\n"; + "[dev [n]]\n" + " - print n random bytes read from dev\n"; #endif U_BOOT_CMD( - rng, 2, 0, do_rng, + rng, 3, 0, do_rng, "print bytes from the hardware random number generator", rng_help_text ); From patchwork Mon Jul 4 13:34:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 587143 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:1ec:0:0:0:0 with SMTP id 12csp839970map; Mon, 4 Jul 2022 06:36:14 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t362tHqMaxXcsZqeU5ol7nlnrgbuuVwvx7cXuqHcJR82slkG6+Rq7Zi6yNPqb/AxM5yYWC X-Received: by 2002:a2e:a483:0:b0:25b:c3c6:8559 with SMTP id h3-20020a2ea483000000b0025bc3c68559mr17007372lji.273.1656941774105; Mon, 04 Jul 2022 06:36:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656941774; cv=none; d=google.com; s=arc-20160816; b=N4MRT908ZmI2zrwp1sgdXlkV1lwfIaXYlnlfkgD3XrdjAxjVK+B/fM/njqKS+rEvv/ BfOTiEk4M1jm7TVyYi54vbBhXvPA1LWeFvePevJZOM517AqviKH7m8tW0r4wIqW4Y5r2 JQ8dLeGdAlHrGIpgnv+2fKShjkmUEqWjVkvngOqJ47KcKlsiLcYbp4YuoPWBOhDuVE9j OVl8a+RNE3Qqm1WkiZSL8KAinnren9oNvzy6cE4wuXM30Gdl7ClwVVYp8oJHyth199Jr lOXpSjxYU4A6MYdEH+P8m3cUZrUgHqJ2iPyKhG5F8daf4J2qwdQo+fAhH2CdXAeeEb9I uC3g== 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=RPAQEokbq4Bdzi8NsEh3O0PDihqqfUHrl7/HMEeXYgs=; b=tG9AtkuodJ9DuGVEtn/M9mdXnhMXsHLHR+l4ebJ81rBREhWeihyd2L7AxInZXItQ0k 53of4UeX7Ct4xaJwbf9coo5Yk8CZ1eouAifjFBl/ULUpEOs8mMlBYfXDnqIhAuBqs+BX KFwdA2B5jKTY4QTvAFBTix8SKVkjvmuAtH57PNh3+C8aU4D7pmo/Z0YrYm4rurfWyY56 IrMIQI0oMQ7UxWLfOTIt1lvi9yOR1JRX3lhinEjaHLnkVspxwCXMJyPjcw76Fh++0qBC /JfJRn2ZdvURrx/CcKHs2hW78PhvFxmlBPZlIT9f/YUEuvOq7LL6WJ7M5BRbMbQ53z/J O2Cw== 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 z3-20020a195043000000b00478f3461013si29380002lfj.72.2022.07.04.06.36.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 06:36:14 -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 38535844E7; Mon, 4 Jul 2022 15:35:28 +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 BA9F5844E5; Mon, 4 Jul 2022 15:35:26 +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 631F3844C1 for ; Mon, 4 Jul 2022 15:35:24 +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 0E7EF23A; Mon, 4 Jul 2022 06:35:24 -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 D52983F66F; Mon, 4 Jul 2022 06:35:21 -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 5/7] cmd: rng: Use a statically allocated array for random bytes Date: Mon, 4 Jul 2022 19:04:42 +0530 Message-Id: <20220704133444.1110715-6-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 Use a statically allocated buffer on stack instead of using malloc for reading the random bytes. Using a local array is faster than allocating heap memory on every initiation of the command. Signed-off-by: Sughosh Ganu Reviewed-by: Simon Glass Reviewed-by: Ilias Apalodimas --- Changes since V5: None cmd/rng.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/cmd/rng.c b/cmd/rng.c index 2ddf27545f..81a23964b8 100644 --- a/cmd/rng.c +++ b/cmd/rng.c @@ -14,9 +14,9 @@ static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { size_t n; - struct udevice *dev; - void *buf; + u8 buf[64]; int devnum; + struct udevice *dev; int ret = CMD_RET_SUCCESS; switch (argc) { @@ -41,11 +41,10 @@ static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) return CMD_RET_FAILURE; } - buf = malloc(n); - if (!buf) { - printf("Out of memory\n"); - return CMD_RET_FAILURE; - } + if (!n) + return 0; + + n = min(n, sizeof(buf)); if (dm_rng_read(dev, buf, n)) { printf("Reading RNG failed\n"); @@ -54,15 +53,13 @@ static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, n); } - free(buf); - return ret; } #ifdef CONFIG_SYS_LONGHELP static char rng_help_text[] = "[dev [n]]\n" - " - print n random bytes read from dev\n"; + " - print n random bytes(max 64) read from dev\n"; #endif U_BOOT_CMD( From patchwork Mon Jul 4 13:34:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 587144 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:1ec:0:0:0:0 with SMTP id 12csp840126map; Mon, 4 Jul 2022 06:36:24 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tIPvypjJ2dnkaI6Gp7OVgW8sIAHpfvvQHN7YUan447AvFasxmeT0dm7jwn6oc3/hRya6kb X-Received: by 2002:a2e:9591:0:b0:25a:7304:449b with SMTP id w17-20020a2e9591000000b0025a7304449bmr17707063ljh.453.1656941784087; Mon, 04 Jul 2022 06:36:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656941784; cv=none; d=google.com; s=arc-20160816; b=BhzNZhYMHLaoap4xb0d3INe+gNAnlRH8U37/u41/kC1Xy/RQPhWUYWUiCz0m0yZwUx k3DFxGcygMEQeWb88cWGKvWB5Bnm4FT0Kea7WpA7+r632J2hk4ijewu0uu1Ajlpwo8rN gkdYrG0/F6xkYTvrLvpZc3B3SFxJMVTTwyPgdXWFt7qmA7gmAzM61swgERcERjB6Xa6g qC1evqXV/prXSv2LgX8LTGTFdBZ1UdepLbCYsHsADlGyj9HPqRUUKwfbNJcN+nlu3QFC ou3DhCwIH3ECmfwbb63odLcaRfYFzrUY54dNz1W/CbnXs5JtUaW2qxoalA6bxh+T4MIO diYA== 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=5V+B33ej5qO7BHvxdJlNx6EIrGtgKEvdhZPJLyESgX4=; b=gSgJrLnJhb4tjZVbRi+dlcPqXq2Ac39GOWSS1wtgA0KiD0beL4IGij8t/GFLjOXvaU F3K4cKeE2Z8Cz3/rn/7sktMVA9YnIHLkhD2BeKnHzfm1VDHDwsyGwYqo8tndXxHbldeW 80luwInW4BadaR9ySUEO52AiT/t3Xr7omu3f7ytC+daUQkOpxNcb/107d477WTgHhhTZ 6pB8TFJ83dMoBTzQN3F7SFJGP3fRO2110usWMdP3/JyvIxhArQ1MDZcqA7HnqYZi9kOl bfze1HhL746V8lOGx+WS9wsrIzsM3LWMBls8YIztjbfQnw1qxIkEL2Tr1pxu1Pktky5n X9Lg== 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 g10-20020a056512118a00b004795607b0b8si10083970lfr.523.2022.07.04.06.36.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 06:36:24 -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 78561844D3; Mon, 4 Jul 2022 15:35:31 +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 71DBA844D3; Mon, 4 Jul 2022 15:35:29 +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 95AE6844E1 for ; Mon, 4 Jul 2022 15:35:26 +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 6107823A; Mon, 4 Jul 2022 06:35:26 -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 337423F66F; Mon, 4 Jul 2022 06:35:23 -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 6/7] doc: rng: Add documentation for the rng command Date: Mon, 4 Jul 2022 19:04:43 +0530 Message-Id: <20220704133444.1110715-7-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 Add a usage document for the 'rng' u-boot command. Reviewed-by: Ilias Apalodimas Reviewed-by: Simon Glass Signed-off-by: Sughosh Ganu --- Changes since V5: None doc/usage/cmd/rng.rst | 26 ++++++++++++++++++++++++++ doc/usage/index.rst | 1 + 2 files changed, 27 insertions(+) create mode 100644 doc/usage/cmd/rng.rst diff --git a/doc/usage/cmd/rng.rst b/doc/usage/cmd/rng.rst new file mode 100644 index 0000000000..1a352da41a --- /dev/null +++ b/doc/usage/cmd/rng.rst @@ -0,0 +1,26 @@ +.. SPDX-License-Identifier: GPL-2.0+ + +rng command +=========== + +Synopsis +-------- + +:: + + rng [devnum [n]] + +Description +----------- + +The *rng* command reads the random number generator(RNG) device and +prints the random bytes read on the console. A maximum of 64 bytes can +be read in one invocation of the command. + +devnum + The RNG device from which the random bytes are to be + read. Defaults to 0. + +n + Number of random bytes to be read and displayed on the + console. Default value is 0x40. Max value is 0x40. diff --git a/doc/usage/index.rst b/doc/usage/index.rst index 770418434a..ed3a78aa14 100644 --- a/doc/usage/index.rst +++ b/doc/usage/index.rst @@ -53,6 +53,7 @@ Shell commands cmd/pstore cmd/qfw cmd/reset + cmd/rng cmd/sbi cmd/sf cmd/scp03 From patchwork Mon Jul 4 13:34:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 587145 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:1ec:0:0:0:0 with SMTP id 12csp840240map; Mon, 4 Jul 2022 06:36:34 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tQo+6dlSQ39jlaVHv0zRjwFQVX35PbS9LVpP7EHCXxkBlY15GsiwIZuQrfzw/zTuJqFpgR X-Received: by 2002:ac2:4c2d:0:b0:47f:6b84:35a3 with SMTP id u13-20020ac24c2d000000b0047f6b8435a3mr18360388lfq.652.1656941794520; Mon, 04 Jul 2022 06:36:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656941794; cv=none; d=google.com; s=arc-20160816; b=QG71y3mqwaNI1MnJbbfGtJDSpI+HvA93jYnzYcF9wIZJkBxMavyXhUOMm16JMwQ5K7 qo0muv3gRkb8x2+qSsa4ua8Ty39asC/alKfdyRDKNWrCoMnS8ChYQdD2kahkgM4GpYIt Ak7pKoHKMfQT7i2dH4KP2ISgbFMjCZ4KQmqtZZCW9i9F3QUY05jh352PDPrO6z9gisKn CSqX6ptWB7qFy07Vbd0xydTXhKuMaSgrnBGFeqn7nnhfhOStfztE8m7LcZWCzK+uW+E9 po2BPhi+59pXT0tRQ0jP9Z2EPOlEahPA36CrFh9fFcbwBM5ncy4NbJkYU51RxVmFLE3w ZpnA== 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=s4BBGGAR++tOj4fZ8Ny5ruDP+dyD5CLB9mftvKC4Hz8=; b=SjFoejaKO8CWoLrgHwZda9sq6E3c36f7tNMNWDm00nXuIpRytgDzmQ4TiM/5OPSIjT Nj7ZkqzCVnSqPTNexUOOPbcXwftHxo7L553LYnqmrF6dboufnLnJr8QW05MFGcAyY3i0 TbzlJwrkK/gkGVu0dvX4bonk64c8cMN0MYU1kSKduc6NMNYMgdnIRvsV9wNV7dwOHiH+ oEQbEeaJqIAhw6D8t5IhPToZsS+jALOmM6t88NqviegUYhUM/UFQ+56U3v78M9jyX1eS xRJ4bQHVdZ+vqyqNCq9YdHDRpT0D8RYrNGxd7zZzIqP232xSErwNiCPtB3ha02IYc0Ty PHsw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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. [85.214.62.61]) by mx.google.com with ESMTPS id a15-20020a2e860f000000b0025bec7c2780si17917267lji.586.2022.07.04.06.36.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Jul 2022 06:36:34 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 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 CE293844E1; Mon, 4 Jul 2022 15:35:32 +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 F0802844DB; Mon, 4 Jul 2022 15:35:31 +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 E78F1844EC for ; Mon, 4 Jul 2022 15:35:28 +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 B3A6523A; Mon, 4 Jul 2022 06:35:28 -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 85E903F66F; Mon, 4 Jul 2022 06:35:26 -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 7/7] test: rng: Add a UT testcase for the rng command Date: Mon, 4 Jul 2022 19:04:44 +0530 Message-Id: <20220704133444.1110715-8-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 'rng' command dumps a number of random bytes on the console. Add a set of tests for the 'rng' command. The test function performs basic sanity testing of the command. Since a unit test is being added for the command, enable it by default in the sandbox platforms. Reviewed-by: Simon Glass Signed-off-by: Sughosh Ganu Acked-by: Ilias Apalodimas --- cmd/Kconfig | 1 + test/dm/rng.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/cmd/Kconfig b/cmd/Kconfig index 09193b61b9..eee5d44348 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -1916,6 +1916,7 @@ config CMD_GETTIME config CMD_RNG bool "rng command" depends on DM_RNG + default y if SANDBOX select HEXDUMP help Print bytes from the hardware random number generator. diff --git a/test/dm/rng.c b/test/dm/rng.c index 5b34c93ed6..6d1f68848d 100644 --- a/test/dm/rng.c +++ b/test/dm/rng.c @@ -25,3 +25,32 @@ static int dm_test_rng_read(struct unit_test_state *uts) return 0; } DM_TEST(dm_test_rng_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); + +/* Test the rng command */ +static int dm_test_rng_cmd(struct unit_test_state *uts) +{ + struct udevice *dev; + + ut_assertok(uclass_get_device(UCLASS_RNG, 0, &dev)); + ut_assertnonnull(dev); + + ut_assertok(console_record_reset_enable()); + + run_command("rng", 0); + ut_assert_nextlinen("00000000:"); + ut_assert_nextlinen("00000010:"); + ut_assert_nextlinen("00000020:"); + ut_assert_nextlinen("00000030:"); + ut_assert_console_end(); + + run_command("rng 0 10", 0); + ut_assert_nextlinen("00000000:"); + ut_assert_console_end(); + + run_command("rng 20", 0); + ut_assert_nextlinen("No RNG device"); + ut_assert_console_end(); + + return 0; +} +DM_TEST(dm_test_rng_cmd, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);