efi_rng.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2019, Linaro Limited
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <efi_loader.h>
  8. #include <efi_rng.h>
  9. #include <rng.h>
  10. DECLARE_GLOBAL_DATA_PTR;
  11. __weak efi_status_t platform_get_rng_device(struct udevice **dev)
  12. {
  13. int ret;
  14. struct udevice *devp;
  15. ret = uclass_get_device(UCLASS_RNG, 0, &devp);
  16. if (ret) {
  17. debug("Unable to get rng device\n");
  18. return EFI_DEVICE_ERROR;
  19. }
  20. *dev = devp;
  21. return EFI_SUCCESS;
  22. }
  23. static efi_status_t EFIAPI rng_getinfo(struct efi_rng_protocol *this,
  24. efi_uintn_t *rng_algorithm_list_size,
  25. efi_guid_t *rng_algorithm_list)
  26. {
  27. efi_status_t ret = EFI_SUCCESS;
  28. efi_guid_t rng_algo_guid = EFI_RNG_ALGORITHM_RAW;
  29. EFI_ENTRY("%p, %p, %p", this, rng_algorithm_list_size,
  30. rng_algorithm_list);
  31. if (!this || !rng_algorithm_list_size) {
  32. ret = EFI_INVALID_PARAMETER;
  33. goto back;
  34. }
  35. if (!rng_algorithm_list ||
  36. *rng_algorithm_list_size < sizeof(*rng_algorithm_list)) {
  37. *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
  38. ret = EFI_BUFFER_TOO_SMALL;
  39. goto back;
  40. }
  41. /*
  42. * For now, use EFI_RNG_ALGORITHM_RAW as the default
  43. * algorithm. If a new algorithm gets added in the
  44. * future through a Kconfig, rng_algo_guid will be set
  45. * based on that Kconfig option
  46. */
  47. *rng_algorithm_list_size = sizeof(*rng_algorithm_list);
  48. guidcpy(rng_algorithm_list, &rng_algo_guid);
  49. back:
  50. return EFI_EXIT(ret);
  51. }
  52. static efi_status_t EFIAPI getrng(struct efi_rng_protocol *this,
  53. efi_guid_t *rng_algorithm,
  54. efi_uintn_t rng_value_length,
  55. uint8_t *rng_value)
  56. {
  57. int ret;
  58. efi_status_t status = EFI_SUCCESS;
  59. struct udevice *dev;
  60. const efi_guid_t rng_raw_guid = EFI_RNG_ALGORITHM_RAW;
  61. EFI_ENTRY("%p, %p, %zu, %p", this, rng_algorithm, rng_value_length,
  62. rng_value);
  63. if (!this || !rng_value || !rng_value_length) {
  64. status = EFI_INVALID_PARAMETER;
  65. goto back;
  66. }
  67. if (rng_algorithm) {
  68. EFI_PRINT("RNG algorithm %pUl\n", rng_algorithm);
  69. if (guidcmp(rng_algorithm, &rng_raw_guid)) {
  70. status = EFI_UNSUPPORTED;
  71. goto back;
  72. }
  73. }
  74. ret = platform_get_rng_device(&dev);
  75. if (ret != EFI_SUCCESS) {
  76. EFI_PRINT("Rng device not found\n");
  77. status = EFI_UNSUPPORTED;
  78. goto back;
  79. }
  80. ret = dm_rng_read(dev, rng_value, rng_value_length);
  81. if (ret < 0) {
  82. EFI_PRINT("Rng device read failed\n");
  83. status = EFI_DEVICE_ERROR;
  84. goto back;
  85. }
  86. back:
  87. return EFI_EXIT(status);
  88. }
  89. const struct efi_rng_protocol efi_rng_protocol = {
  90. .get_info = rng_getinfo,
  91. .get_rng = getrng,
  92. };