From patchwork Fri Jul 22 16:02:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sughosh Ganu X-Patchwork-Id: 592473 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:b811:0:0:0:0 with SMTP id fc17csp635999mab; Fri, 22 Jul 2022 09:03:56 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uy0GRRS6/T4JT+OQP10CFGO+eAr6CX3A9PfCNyIQzXFxQDjmwe8iUyvHlDuLcwSNCzcd54 X-Received: by 2002:a6b:d90d:0:b0:67c:a0:dc42 with SMTP id r13-20020a6bd90d000000b0067c00a0dc42mr161128ioc.103.1658505836345; Fri, 22 Jul 2022 09:03:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658505836; cv=none; d=google.com; s=arc-20160816; b=facNfKKRpIcaeV326nQEL+HS744BNndpweiDQL/HZBeq70X4Vr5yNGhZubmIdf0K9K ZIsElQPH43hB3ynaCxiNSzu/VQX0fes1ckmi99/aa5qBJ9/MFvBV1t+GQtTvnYeFRHwT UuPv0H9YOhJezDbZGM8omPAMW52LgHcFYpNHYzoi1sHfYOgroIfBjV7qPfc6dx0X+C+n TgCsFf9WppghAWKkAk+SGORXm2y/Vfi5W4vPUNWVuzNvGDtzGzr609bkCut0V8iV1tlJ Hx2J+Qr+FrpZhWAiyMvRs7VooLe8MffbYOW1Rr+zJqFJpI24QszgsHBLcU9LdO1HpNQr rdnQ== 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=t/AyOdEgU5xamN/K6PJVC9OLRerfA+RGhFW9F3PrYfM=; b=eD/BfgvH5hk71RaRMDQW/fAsPUitP7HgLOSxMyeZzLblEm3oMjAKlA1/NbESdXHiQh FAnmK1jMsCV3eWkcNFAOJ+snEPo1cT9+1MC8MHt4GM1jkW71UECTEWdLGxX5hmpcDKhW 1WBDw3gae3c1b7tH73bOYoXJLmT/RJCe5Oy8OzOZH1OyONQI9GDkWy1R5M5x8dg8QKjD 0kkJTMLgf8vyvCIKY7AR0iEC4SIfZHLSD3DbRr3d3qMGQqWmi4cE8wVOpxTN7xaWdRRG 9PKaO/FFJZ6VDwec2bu4g9mcqwWj/oj35TPwJAxkjjvU89xHQxcmXRLewLIF8VNk/syX Jd2w== 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 d5-20020a056e021c4500b002dce2d8dc07si4821060ilg.57.2022.07.22.09.03.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 09:03:56 -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 B35A484106; Fri, 22 Jul 2022 18:03:00 +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 A09A1840BE; Fri, 22 Jul 2022 18:02:47 +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 6CAB983F9B for ; Fri, 22 Jul 2022 18:02:42 +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 5646D1063; Fri, 22 Jul 2022 09:02:42 -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 D2D493F73D; Fri, 22 Jul 2022 09:02:39 -0700 (PDT) From: Sughosh Ganu To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Simon Glass , Tom Rini , Sughosh Ganu Subject: [PATCH v8 6/8] cmd: rng: Use a statically allocated array for random bytes Date: Fri, 22 Jul 2022 21:32:07 +0530 Message-Id: <20220722160209.863840-7-sughosh.ganu@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220722160209.863840-1-sughosh.ganu@linaro.org> References: <20220722160209.863840-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: Ilias Apalodimas Reviewed-by: Simon Glass --- Changes since V7: 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(