kaslrseed.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * The 'kaslrseed' command takes bytes from the hardware random number
  4. * generator and uses them to set the kaslr-seed value in the chosen node.
  5. *
  6. * Copyright (c) 2021, Chris Morgan <macromorgan@hotmail.com>
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <dm.h>
  11. #include <hexdump.h>
  12. #include <malloc.h>
  13. #include <rng.h>
  14. #include <fdt_support.h>
  15. static int do_kaslr_seed(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  16. {
  17. size_t n = 0x8;
  18. struct udevice *dev;
  19. u64 *buf;
  20. int nodeoffset;
  21. int ret = CMD_RET_SUCCESS;
  22. if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) {
  23. printf("No RNG device\n");
  24. return CMD_RET_FAILURE;
  25. }
  26. buf = malloc(n);
  27. if (!buf) {
  28. printf("Out of memory\n");
  29. return CMD_RET_FAILURE;
  30. }
  31. if (dm_rng_read(dev, buf, n)) {
  32. printf("Reading RNG failed\n");
  33. return CMD_RET_FAILURE;
  34. }
  35. if (!working_fdt) {
  36. printf("No FDT memory address configured. Please configure\n"
  37. "the FDT address via \"fdt addr <address>\" command.\n"
  38. "Aborting!\n");
  39. return CMD_RET_FAILURE;
  40. }
  41. ret = fdt_check_header(working_fdt);
  42. if (ret < 0) {
  43. printf("fdt_chosen: %s\n", fdt_strerror(ret));
  44. return CMD_RET_FAILURE;
  45. }
  46. nodeoffset = fdt_find_or_add_subnode(working_fdt, 0, "chosen");
  47. if (nodeoffset < 0) {
  48. printf("Reading chosen node failed\n");
  49. return CMD_RET_FAILURE;
  50. }
  51. ret = fdt_setprop(working_fdt, nodeoffset, "kaslr-seed", buf, sizeof(buf));
  52. if (ret < 0) {
  53. printf("Unable to set kaslr-seed on chosen node: %s\n", fdt_strerror(ret));
  54. return CMD_RET_FAILURE;
  55. }
  56. free(buf);
  57. return ret;
  58. }
  59. #ifdef CONFIG_SYS_LONGHELP
  60. static char kaslrseed_help_text[] =
  61. "[n]\n"
  62. " - append random bytes to chosen kaslr-seed node\n";
  63. #endif
  64. U_BOOT_CMD(
  65. kaslrseed, 1, 0, do_kaslr_seed,
  66. "feed bytes from the hardware random number generator to the kaslr-seed",
  67. kaslrseed_help_text
  68. );