rng.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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;
  16. u8 buf[64];
  17. int devnum;
  18. struct udevice *dev;
  19. int ret = CMD_RET_SUCCESS;
  20. switch (argc) {
  21. case 1:
  22. devnum = 0;
  23. n = 0x40;
  24. break;
  25. case 2:
  26. devnum = hextoul(argv[1], NULL);
  27. n = 0x40;
  28. break;
  29. case 3:
  30. devnum = hextoul(argv[1], NULL);
  31. n = hextoul(argv[2], NULL);
  32. break;
  33. default:
  34. return CMD_RET_USAGE;
  35. }
  36. if (uclass_get_device_by_seq(UCLASS_RNG, devnum, &dev) || !dev) {
  37. printf("No RNG device\n");
  38. return CMD_RET_FAILURE;
  39. }
  40. if (!n)
  41. return 0;
  42. n = min(n, sizeof(buf));
  43. if (dm_rng_read(dev, buf, n)) {
  44. printf("Reading RNG failed\n");
  45. ret = CMD_RET_FAILURE;
  46. } else {
  47. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, n);
  48. }
  49. return ret;
  50. }
  51. #ifdef CONFIG_SYS_LONGHELP
  52. static char rng_help_text[] =
  53. "[dev [n]]\n"
  54. " - print n random bytes(max 64) read from dev\n";
  55. #endif
  56. U_BOOT_CMD(
  57. rng, 3, 0, do_rng,
  58. "print bytes from the hardware random number generator",
  59. rng_help_text
  60. );