Complete migration of MTDPARTS_DEFAULT / MTDIDS_DEFAULT, include in environment
[platform/kernel/u-boot.git] / cmd / rng.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * The 'rng' command prints bytes from the hardware random number generator.
4  *
5  * Copyright (c) 2019, Heinrich Schuchardt <xypron.glpk@gmx.de>
6  */
7 #include <common.h>
8 #include <command.h>
9 #include <dm.h>
10 #include <hexdump.h>
11 #include <malloc.h>
12 #include <rng.h>
13
14 static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
15 {
16         size_t n = 0x40;
17         struct udevice *dev;
18         void *buf;
19         int ret = CMD_RET_SUCCESS;
20
21         if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) {
22                 printf("No RNG device\n");
23                 return CMD_RET_FAILURE;
24         }
25
26         if (argc >= 2)
27                 n = hextoul(argv[1], NULL);
28
29         buf = malloc(n);
30         if (!buf) {
31                 printf("Out of memory\n");
32                 return CMD_RET_FAILURE;
33         }
34
35         if (dm_rng_read(dev, buf, n)) {
36                 printf("Reading RNG failed\n");
37                 ret = CMD_RET_FAILURE;
38         } else {
39                 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, n);
40         }
41
42         free(buf);
43
44         return ret;
45 }
46
47 #ifdef CONFIG_SYS_LONGHELP
48 static char rng_help_text[] =
49         "[n]\n"
50         "  - print n random bytes\n";
51 #endif
52
53 U_BOOT_CMD(
54         rng, 2, 0, do_rng,
55         "print bytes from the hardware random number generator",
56         rng_help_text
57 );