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(