]> git.proxmox.com Git - mirror_edk2.git/blobdiff - CryptoPkg/Library/BaseCryptLibOnProtocolPpi/DxeCryptLib.c
CryptoPkg/Library: Add BaseCryptLibOnProtocolPpi instances
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLibOnProtocolPpi / DxeCryptLib.c
diff --git a/CryptoPkg/Library/BaseCryptLibOnProtocolPpi/DxeCryptLib.c b/CryptoPkg/Library/BaseCryptLibOnProtocolPpi/DxeCryptLib.c
new file mode 100644 (file)
index 0000000..34d5f41
--- /dev/null
@@ -0,0 +1,68 @@
+/** @file\r
+  Implements the GetCryptoServices() API that retuns a pointer to the EDK II\r
+  Crypto Protocol.\r
+\r
+  Copyright (C) Microsoft Corporation. All rights reserved.\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+#include <PiDxe.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Protocol/Crypto.h>\r
+\r
+EDKII_CRYPTO_PROTOCOL  *mCryptoProtocol = NULL;\r
+\r
+/**\r
+  Internal worker function that returns the pointer to an EDK II Crypto\r
+  Protocol/PPI.  The layout of the PPI, DXE Protocol, and SMM Protocol are\r
+  identical which allows the implementation of the BaseCryptLib functions that\r
+  call through a Protocol/PPI to be shared for the PEI, DXE, and SMM\r
+  implementations.\r
+\r
+  This DXE implementation returns the pointer to the EDK II Crypto Protocol\r
+  that was found in the library constructor DxeCryptLibConstructor().\r
+**/\r
+VOID *\r
+GetCryptoServices (\r
+  VOID\r
+  )\r
+{\r
+  return (VOID *)mCryptoProtocol;\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+DxeCryptLibConstructor (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       Version;\r
+\r
+  Status = gBS->LocateProtocol (\r
+                  &gEdkiiCryptoProtocolGuid,\r
+                  NULL,\r
+                  (VOID **)&mCryptoProtocol\r
+                  );\r
+\r
+  if (EFI_ERROR (Status) || mCryptoProtocol == NULL) {\r
+    DEBUG((DEBUG_ERROR, "[DxeCryptLib] Failed to locate Crypto Protocol. Status = %r\n", Status));\r
+    ASSERT_EFI_ERROR (Status);\r
+    ASSERT (mCryptoProtocol != NULL);\r
+    mCryptoProtocol = NULL;\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  Version = mCryptoProtocol->GetVersion ();\r
+  if (Version < EDKII_CRYPTO_VERSION) {\r
+    DEBUG((DEBUG_ERROR, "[DxeCryptLib] Crypto Protocol unsupported version %d\n", Version));\r
+    ASSERT (Version >= EDKII_CRYPTO_VERSION);\r
+    mCryptoProtocol = NULL;\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r