rng.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. static int do_rng(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  14. {
  15. size_t n = 0x40;
  16. struct udevice *dev;
  17. void *buf;
  18. int ret = CMD_RET_SUCCESS;
  19. if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) {
  20. printf("No RNG device\n");
  21. return CMD_RET_FAILURE;
  22. }
  23. if (argc >= 2)
  24. n = hextoul(argv[1], NULL);
  25. buf = malloc(n);
  26. if (!buf) {
  27. printf("Out of memory\n");
  28. return CMD_RET_FAILURE;
  29. }
  30. if (dm_rng_read(dev, buf, n)) {
  31. printf("Reading RNG failed\n");
  32. ret = CMD_RET_FAILURE;
  33. } else {
  34. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, n);
  35. }
  36. free(buf);
  37. return ret;
  38. }
  39. #ifdef CONFIG_SYS_LONGHELP
  40. static char rng_help_text[] =
  41. "[n]\n"
  42. " - print n random bytes\n";
  43. #endif
  44. U_BOOT_CMD(
  45. rng, 2, 0, do_rng,
  46. "print bytes from the hardware random number generator",
  47. rng_help_text
  48. );