Heinrich Schuchardt | 4f24ac0 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 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> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
Heinrich Schuchardt | 4f24ac0 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 12 | #include <rng.h> |
| 13 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 14 | static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Heinrich Schuchardt | 4f24ac0 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 15 | { |
Sughosh Ganu | 87ab234 | 2022-07-22 21:32:06 +0530 | [diff] [blame^] | 16 | size_t n; |
Heinrich Schuchardt | 4f24ac0 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 17 | struct udevice *dev; |
| 18 | void *buf; |
Sughosh Ganu | 87ab234 | 2022-07-22 21:32:06 +0530 | [diff] [blame^] | 19 | int devnum; |
Heinrich Schuchardt | 4f24ac0 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 20 | int ret = CMD_RET_SUCCESS; |
| 21 | |
Sughosh Ganu | 87ab234 | 2022-07-22 21:32:06 +0530 | [diff] [blame^] | 22 | switch (argc) { |
| 23 | case 1: |
| 24 | devnum = 0; |
| 25 | n = 0x40; |
| 26 | break; |
| 27 | case 2: |
| 28 | devnum = hextoul(argv[1], NULL); |
| 29 | n = 0x40; |
| 30 | break; |
| 31 | case 3: |
| 32 | devnum = hextoul(argv[1], NULL); |
| 33 | n = hextoul(argv[2], NULL); |
| 34 | break; |
| 35 | default: |
| 36 | return CMD_RET_USAGE; |
| 37 | } |
| 38 | |
| 39 | if (uclass_get_device_by_seq(UCLASS_RNG, devnum, &dev) || !dev) { |
Heinrich Schuchardt | 4f24ac0 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 40 | printf("No RNG device\n"); |
| 41 | return CMD_RET_FAILURE; |
| 42 | } |
| 43 | |
Heinrich Schuchardt | 4f24ac0 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 44 | buf = malloc(n); |
| 45 | if (!buf) { |
| 46 | printf("Out of memory\n"); |
| 47 | return CMD_RET_FAILURE; |
| 48 | } |
| 49 | |
| 50 | if (dm_rng_read(dev, buf, n)) { |
| 51 | printf("Reading RNG failed\n"); |
| 52 | ret = CMD_RET_FAILURE; |
| 53 | } else { |
| 54 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, n); |
| 55 | } |
| 56 | |
| 57 | free(buf); |
| 58 | |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | #ifdef CONFIG_SYS_LONGHELP |
| 63 | static char rng_help_text[] = |
Sughosh Ganu | 87ab234 | 2022-07-22 21:32:06 +0530 | [diff] [blame^] | 64 | "[dev [n]]\n" |
| 65 | " - print n random bytes read from dev\n"; |
Heinrich Schuchardt | 4f24ac0 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 66 | #endif |
| 67 | |
| 68 | U_BOOT_CMD( |
Sughosh Ganu | 87ab234 | 2022-07-22 21:32:06 +0530 | [diff] [blame^] | 69 | rng, 3, 0, do_rng, |
Heinrich Schuchardt | 4f24ac0 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 70 | "print bytes from the hardware random number generator", |
| 71 | rng_help_text |
| 72 | ); |