cmd: rng: Use a statically allocated array for random bytes

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 <sughosh.ganu@linaro.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
This commit is contained in:
Sughosh Ganu 2022-07-22 21:32:07 +05:30 committed by Ilias Apalodimas
parent 87ab234c1c
commit c79e274c7c

View File

@ -14,9 +14,9 @@
static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
{ {
size_t n; size_t n;
struct udevice *dev; u8 buf[64];
void *buf;
int devnum; int devnum;
struct udevice *dev;
int ret = CMD_RET_SUCCESS; int ret = CMD_RET_SUCCESS;
switch (argc) { 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; return CMD_RET_FAILURE;
} }
buf = malloc(n); if (!n)
if (!buf) { return 0;
printf("Out of memory\n");
return CMD_RET_FAILURE; n = min(n, sizeof(buf));
}
if (dm_rng_read(dev, buf, n)) { if (dm_rng_read(dev, buf, n)) {
printf("Reading RNG failed\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); print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, n);
} }
free(buf);
return ret; return ret;
} }
#ifdef CONFIG_SYS_LONGHELP #ifdef CONFIG_SYS_LONGHELP
static char rng_help_text[] = static char rng_help_text[] =
"[dev [n]]\n" "[dev [n]]\n"
" - print n random bytes read from dev\n"; " - print n random bytes(max 64) read from dev\n";
#endif #endif
U_BOOT_CMD( U_BOOT_CMD(