From patchwork Wed Jul 20 12:29:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 591865 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5817:0:0:0:0 with SMTP id j23csp4893617max; Wed, 20 Jul 2022 05:31:14 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vzcGyIEB4NGYPeqNOnpKKeG8KH7VJ76+Ts4ruofY/jaH9tR7VGz8ySDC33t4/EVACbiUB/ X-Received: by 2002:a02:a918:0:b0:33f:2a28:a2c6 with SMTP id n24-20020a02a918000000b0033f2a28a2c6mr20017358jam.8.1658320274794; Wed, 20 Jul 2022 05:31:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658320274; cv=none; d=google.com; s=arc-20160816; b=xpbxkbf74d5kNxRQjtSg0YfXeVe+jmEmbzOAyfdn+lWzmzKlmQVVB5iM76VV4yxbuT /1vdOOokzIqpTPxRTtIZSWXSTjb7G9+Lf/mF/nHhG8HZQBbnqV5+9mfIGh+mIVRALCFX SEndTLCgpcilASSjPACZCCX2xl+xk5nmSvo8yaljgwNAGf7aF0FhJr+uWtFJvqoJVz01 2OYRlg8kZ+r9YTfJtDh9B6U0ipd3ZcKqvZsL8heRJpxt0IFpRqMPGbFor9ztkjso0uPj MkoQtbIMwyjY94j4HUdsifr73iRlM6RhmoGL5Z5RTp5K2f6/4A29GnoZC4K5G/hQKuGr Mi5g== 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=EIE0Dl8alStccCZIBhOAOzjBtdJ/t5yzdV6ktawK4kc=; b=uC36DnbFtk+swnWrKN5S0sBCzNtpPuhQtPGAX4EXI64BQ3WQKhEZgKiiIiBcglOLFo oXrEPGYp1bicUctOyU2GP4EvTM9qp1qh2Wnk+Ogp9RXvMchjhEzxt+THrP5m5LJE/ERz 5+93tlEa/DQNBKEwrgaNLmgeuPLwmPIUS36wFv+SWCOAq7lzI0mkNOoTV6usa+Ni9x5u sYJP181/XcD2uzYudTPZVhnnl9NazRtUHmx8ZYOzkeUiIqxvOhu9+FKU7SpepkHL/nPE MQV3zbO33bSLipvD3IlsEs+XhdU4X5vTEqVZjsETg5KhvWH/BA1MeDQg72IVf2TO8Fu3 M99g== 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 t2-20020a056638204200b00339e612b77fsi27173983jaj.29.2022.07.20.05.31.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Jul 2022 05:31: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 D3CAC840CB; Wed, 20 Jul 2022 14:30:34 +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 219D9840A8; Wed, 20 Jul 2022 14:30:19 +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 3103984084 for ; Wed, 20 Jul 2022 14:30: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 29F811576; Wed, 20 Jul 2022 05:30:15 -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 AC5EE3F73D; Wed, 20 Jul 2022 05:30:12 -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 5/7] cmd: rng: Use a statically allocated array for random bytes Date: Wed, 20 Jul 2022 17:59:24 +0530 Message-Id: <20220720122926.415659-6-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 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 --- Changes since V6: 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(