2019-12-25 05:17:37 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
/*
|
|
|
|
* The 'rng' command prints bytes from the hardware random number generator.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019, Heinrich Schuchardt <xypron.glpk@gmx.de>
|
|
|
|
*/
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <hexdump.h>
|
2020-02-03 22:36:16 +08:00
|
|
|
#include <malloc.h>
|
2019-12-25 05:17:37 +08:00
|
|
|
#include <rng.h>
|
|
|
|
|
2020-05-11 01:40:03 +08:00
|
|
|
static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
|
2019-12-25 05:17:37 +08:00
|
|
|
{
|
2022-07-23 00:02:06 +08:00
|
|
|
size_t n;
|
2022-07-23 00:02:07 +08:00
|
|
|
u8 buf[64];
|
2022-07-23 00:02:06 +08:00
|
|
|
int devnum;
|
2022-07-23 00:02:07 +08:00
|
|
|
struct udevice *dev;
|
2024-04-04 15:51:05 +08:00
|
|
|
int ret = CMD_RET_SUCCESS, err;
|
2019-12-25 05:17:37 +08:00
|
|
|
|
2024-03-04 22:42:42 +08:00
|
|
|
if (argc == 2 && !strcmp(argv[1], "list")) {
|
|
|
|
int idx = 0;
|
|
|
|
|
|
|
|
uclass_foreach_dev_probe(UCLASS_RNG, dev) {
|
|
|
|
idx++;
|
|
|
|
printf("RNG #%d - %s\n", dev->seq_, dev->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!idx) {
|
|
|
|
log_err("No RNG device\n");
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CMD_RET_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2022-07-23 00:02:06 +08:00
|
|
|
switch (argc) {
|
|
|
|
case 1:
|
|
|
|
devnum = 0;
|
|
|
|
n = 0x40;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
devnum = hextoul(argv[1], NULL);
|
|
|
|
n = 0x40;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
devnum = hextoul(argv[1], NULL);
|
|
|
|
n = hextoul(argv[2], NULL);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uclass_get_device_by_seq(UCLASS_RNG, devnum, &dev) || !dev) {
|
2019-12-25 05:17:37 +08:00
|
|
|
printf("No RNG device\n");
|
|
|
|
return CMD_RET_FAILURE;
|
|
|
|
}
|
|
|
|
|
2022-07-23 00:02:07 +08:00
|
|
|
if (!n)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
n = min(n, sizeof(buf));
|
2019-12-25 05:17:37 +08:00
|
|
|
|
2024-04-04 15:51:05 +08:00
|
|
|
err = dm_rng_read(dev, buf, n);
|
|
|
|
if (err) {
|
|
|
|
puts(err == -EINTR ? "Abort\n" : "Reading RNG failed\n");
|
2019-12-25 05:17:37 +08:00
|
|
|
ret = CMD_RET_FAILURE;
|
|
|
|
} else {
|
|
|
|
print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_BOOT_CMD(
|
2022-07-23 00:02:06 +08:00
|
|
|
rng, 3, 0, do_rng,
|
2019-12-25 05:17:37 +08:00
|
|
|
"print bytes from the hardware random number generator",
|
2024-03-04 22:42:42 +08:00
|
|
|
"list - list all the probed rng devices\n"
|
|
|
|
"rng [dev] [n] - print n random bytes(max 64) read from dev\n"
|
2019-12-25 05:17:37 +08:00
|
|
|
);
|