\r
#include <Library/BaseLib.h>\r
#include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/RngLib.h>\r
#include <Protocol/Rng.h>\r
\r
#include "RngDxeInternals.h"\r
\r
+// Maximum number of Rng algorithms.\r
+#define RNG_AVAILABLE_ALGO_MAX 1\r
+\r
+/** Allocate and initialize mAvailableAlgoArray with the available\r
+ Rng algorithms. Also update mAvailableAlgoArrayCount.\r
+\r
+ @retval EFI_SUCCESS The function completed successfully.\r
+ @retval EFI_OUT_OF_RESOURCES Could not allocate memory.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetAvailableAlgorithms (\r
+ VOID\r
+ )\r
+{\r
+ UINT64 DummyRand;\r
+\r
+ // Allocate RNG_AVAILABLE_ALGO_MAX entries to avoid evaluating\r
+ // Rng algorithms 2 times, one for the allocation, one to populate.\r
+ mAvailableAlgoArray = AllocateZeroPool (RNG_AVAILABLE_ALGO_MAX);\r
+ if (mAvailableAlgoArray == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ // Check RngGetBytes() before advertising PcdCpuRngSupportedAlgorithm.\r
+ if (!EFI_ERROR (RngGetBytes (sizeof (DummyRand), (UINT8 *)&DummyRand))) {\r
+ CopyMem (\r
+ &mAvailableAlgoArray[mAvailableAlgoArrayCount],\r
+ PcdGetPtr (PcdCpuRngSupportedAlgorithm),\r
+ sizeof (EFI_RNG_ALGORITHM)\r
+ );\r
+ mAvailableAlgoArrayCount++;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/** Free mAvailableAlgoArray.\r
+**/\r
+VOID\r
+EFIAPI\r
+FreeAvailableAlgorithms (\r
+ VOID\r
+ )\r
+{\r
+ FreePool (mAvailableAlgoArray);\r
+ return;\r
+}\r
+\r
/**\r
Produces and returns an RNG value using either the default or specified RNG algorithm.\r
\r
)\r
{\r
EFI_STATUS Status;\r
+ UINTN Index;\r
\r
if ((This == NULL) || (RNGValueLength == 0) || (RNGValue == NULL)) {\r
return EFI_INVALID_PARAMETER;\r
//\r
// Use the default RNG algorithm if RNGAlgorithm is NULL.\r
//\r
- RNGAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);\r
+ for (Index = 0; Index < mAvailableAlgoArrayCount; Index++) {\r
+ if (!IsZeroGuid (&mAvailableAlgoArray[Index])) {\r
+ RNGAlgorithm = &mAvailableAlgoArray[Index];\r
+ goto FoundAlgo;\r
+ }\r
+ }\r
+\r
+ if (Index == mAvailableAlgoArrayCount) {\r
+ // No algorithm available.\r
+ ASSERT (Index != mAvailableAlgoArrayCount);\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
}\r
\r
+FoundAlgo:\r
if (CompareGuid (RNGAlgorithm, PcdGetPtr (PcdCpuRngSupportedAlgorithm))) {\r
Status = RngGetBytes (RNGValueLength, RNGValue);\r
return Status;\r
OUT EFI_RNG_ALGORITHM *RNGAlgorithmList\r
)\r
{\r
- UINTN RequiredSize;\r
- EFI_RNG_ALGORITHM *CpuRngSupportedAlgorithm;\r
-\r
- RequiredSize = sizeof (EFI_RNG_ALGORITHM);\r
+ UINTN RequiredSize;\r
\r
if ((This == NULL) || (RNGAlgorithmListSize == NULL)) {\r
return EFI_INVALID_PARAMETER;\r
}\r
\r
+ RequiredSize = mAvailableAlgoArrayCount * sizeof (EFI_RNG_ALGORITHM);\r
+\r
+ if (RequiredSize == 0) {\r
+ // No supported algorithms found.\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+\r
if (*RNGAlgorithmListSize < RequiredSize) {\r
*RNGAlgorithmListSize = RequiredSize;\r
return EFI_BUFFER_TOO_SMALL;\r
}\r
\r
- CpuRngSupportedAlgorithm = PcdGetPtr (PcdCpuRngSupportedAlgorithm);\r
-\r
- CopyMem (&RNGAlgorithmList[0], CpuRngSupportedAlgorithm, sizeof (EFI_RNG_ALGORITHM));\r
+ if (RNGAlgorithmList == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
\r
+ // There is no gap in the array, so copy the block.\r
+ CopyMem (RNGAlgorithmList, mAvailableAlgoArray, RequiredSize);\r
*RNGAlgorithmListSize = RequiredSize;\r
return EFI_SUCCESS;\r
}\r
\r
#include "RngDxeInternals.h"\r
\r
+//\r
+// Array containing the validated Rng algorithm.\r
+// The entry with the lowest index will be the default algorithm.\r
+//\r
+UINTN mAvailableAlgoArrayCount;\r
+EFI_RNG_ALGORITHM *mAvailableAlgoArray;\r
+\r
//\r
// The Random Number Generator (RNG) protocol\r
//\r
&mRngRdRand,\r
NULL\r
);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Get the list of available algorithm.\r
+ //\r
+ return GetAvailableAlgorithms ();\r
+}\r
+\r
+/**\r
+ This is the unload handle for RndgDxe module.\r
+\r
+ Disconnect the driver specified by ImageHandle from all the devices in the handle database.\r
+ Uninstall all the protocols installed in the driver entry point.\r
\r
- return Status;\r
+ @param[in] ImageHandle The drivers' driver image.\r
+\r
+ @retval EFI_SUCCESS The image is unloaded.\r
+ @retval Others Failed to unload the image.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+RngDriverUnLoad (\r
+ IN EFI_HANDLE ImageHandle\r
+ )\r
+{\r
+ //\r
+ // Free the list of available algorithm.\r
+ //\r
+ FreeAvailableAlgorithms ();\r
+ return EFI_SUCCESS;\r
}\r
\r
/**\r