AArch64Algo.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /** @file
  2. Aarch64 specific code.
  3. Copyright (c) 2022, Arm Limited. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Library/BaseLib.h>
  7. #include <Library/BaseMemoryLib.h>
  8. #include <Library/DebugLib.h>
  9. #include <Library/MemoryAllocationLib.h>
  10. #include <Library/ArmTrngLib.h>
  11. #include "RngDxeInternals.h"
  12. // Maximum number of Rng algorithms.
  13. #define RNG_AVAILABLE_ALGO_MAX 2
  14. /** Allocate and initialize mAvailableAlgoArray with the available
  15. Rng algorithms. Also update mAvailableAlgoArrayCount.
  16. @retval EFI_SUCCESS The function completed successfully.
  17. @retval EFI_OUT_OF_RESOURCES Could not allocate memory.
  18. **/
  19. EFI_STATUS
  20. EFIAPI
  21. GetAvailableAlgorithms (
  22. VOID
  23. )
  24. {
  25. UINT64 DummyRand;
  26. UINT16 MajorRevision;
  27. UINT16 MinorRevision;
  28. // Rng algorithms 2 times, one for the allocation, one to populate.
  29. mAvailableAlgoArray = AllocateZeroPool (RNG_AVAILABLE_ALGO_MAX);
  30. if (mAvailableAlgoArray == NULL) {
  31. return EFI_OUT_OF_RESOURCES;
  32. }
  33. // Check RngGetBytes() before advertising PcdCpuRngSupportedAlgorithm.
  34. if (!EFI_ERROR (RngGetBytes (sizeof (DummyRand), (UINT8 *)&DummyRand))) {
  35. CopyMem (
  36. &mAvailableAlgoArray[mAvailableAlgoArrayCount],
  37. PcdGetPtr (PcdCpuRngSupportedAlgorithm),
  38. sizeof (EFI_RNG_ALGORITHM)
  39. );
  40. mAvailableAlgoArrayCount++;
  41. DEBUG_CODE_BEGIN ();
  42. if (IsZeroGuid (PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {
  43. DEBUG ((
  44. DEBUG_WARN,
  45. "PcdCpuRngSupportedAlgorithm should be a non-zero GUID\n"
  46. ));
  47. }
  48. DEBUG_CODE_END ();
  49. }
  50. // Raw algorithm (Trng)
  51. if (!EFI_ERROR (GetArmTrngVersion (&MajorRevision, &MinorRevision))) {
  52. CopyMem (
  53. &mAvailableAlgoArray[mAvailableAlgoArrayCount],
  54. &gEfiRngAlgorithmRaw,
  55. sizeof (EFI_RNG_ALGORITHM)
  56. );
  57. mAvailableAlgoArrayCount++;
  58. }
  59. return EFI_SUCCESS;
  60. }