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