Heinrich Schuchardt | a31a594 | 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 | */ |
Heinrich Schuchardt | a31a594 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 7 | #include <command.h> |
| 8 | #include <dm.h> |
| 9 | #include <hexdump.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Heinrich Schuchardt | a31a594 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 11 | #include <rng.h> |
| 12 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 13 | static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Heinrich Schuchardt | a31a594 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 14 | { |
Sughosh Ganu | b746dee | 2022-07-22 21:32:06 +0530 | [diff] [blame] | 15 | size_t n; |
Sughosh Ganu | 407ec09 | 2022-07-22 21:32:07 +0530 | [diff] [blame] | 16 | u8 buf[64]; |
Sughosh Ganu | b746dee | 2022-07-22 21:32:06 +0530 | [diff] [blame] | 17 | int devnum; |
Sughosh Ganu | 407ec09 | 2022-07-22 21:32:07 +0530 | [diff] [blame] | 18 | struct udevice *dev; |
Marek BehĂșn | 5304d5d | 2024-04-04 09:51:05 +0200 | [diff] [blame] | 19 | int ret = CMD_RET_SUCCESS, err; |
Heinrich Schuchardt | a31a594 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 20 | |
Weizhao Ouyang | 168e947 | 2024-03-04 14:42:42 +0000 | [diff] [blame] | 21 | if (argc == 2 && !strcmp(argv[1], "list")) { |
| 22 | int idx = 0; |
| 23 | |
| 24 | uclass_foreach_dev_probe(UCLASS_RNG, dev) { |
| 25 | idx++; |
| 26 | printf("RNG #%d - %s\n", dev->seq_, dev->name); |
| 27 | } |
| 28 | |
| 29 | if (!idx) { |
| 30 | log_err("No RNG device\n"); |
| 31 | return CMD_RET_FAILURE; |
| 32 | } |
| 33 | |
| 34 | return CMD_RET_SUCCESS; |
| 35 | } |
| 36 | |
Sughosh Ganu | b746dee | 2022-07-22 21:32:06 +0530 | [diff] [blame] | 37 | switch (argc) { |
| 38 | case 1: |
| 39 | devnum = 0; |
| 40 | n = 0x40; |
| 41 | break; |
| 42 | case 2: |
| 43 | devnum = hextoul(argv[1], NULL); |
| 44 | n = 0x40; |
| 45 | break; |
| 46 | case 3: |
| 47 | devnum = hextoul(argv[1], NULL); |
| 48 | n = hextoul(argv[2], NULL); |
| 49 | break; |
| 50 | default: |
| 51 | return CMD_RET_USAGE; |
| 52 | } |
| 53 | |
| 54 | if (uclass_get_device_by_seq(UCLASS_RNG, devnum, &dev) || !dev) { |
Heinrich Schuchardt | a31a594 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 55 | printf("No RNG device\n"); |
| 56 | return CMD_RET_FAILURE; |
| 57 | } |
| 58 | |
Sughosh Ganu | 407ec09 | 2022-07-22 21:32:07 +0530 | [diff] [blame] | 59 | if (!n) |
| 60 | return 0; |
| 61 | |
| 62 | n = min(n, sizeof(buf)); |
Heinrich Schuchardt | a31a594 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 63 | |
Marek BehĂșn | 5304d5d | 2024-04-04 09:51:05 +0200 | [diff] [blame] | 64 | err = dm_rng_read(dev, buf, n); |
| 65 | if (err) { |
| 66 | puts(err == -EINTR ? "Abort\n" : "Reading RNG failed\n"); |
Heinrich Schuchardt | a31a594 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 67 | ret = CMD_RET_FAILURE; |
| 68 | } else { |
| 69 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, n); |
| 70 | } |
| 71 | |
Heinrich Schuchardt | a31a594 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 72 | return ret; |
| 73 | } |
| 74 | |
Heinrich Schuchardt | a31a594 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 75 | U_BOOT_CMD( |
Sughosh Ganu | b746dee | 2022-07-22 21:32:06 +0530 | [diff] [blame] | 76 | rng, 3, 0, do_rng, |
Heinrich Schuchardt | a31a594 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 77 | "print bytes from the hardware random number generator", |
Weizhao Ouyang | 168e947 | 2024-03-04 14:42:42 +0000 | [diff] [blame] | 78 | "list - list all the probed rng devices\n" |
| 79 | "rng [dev] [n] - print n random bytes(max 64) read from dev\n" |
Heinrich Schuchardt | a31a594 | 2019-12-24 22:17:37 +0100 | [diff] [blame] | 80 | ); |