]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLibOnProtocolPpi/SmmCryptLib.c
ef6078dfa4175a12a5717b77ef474305d877c79f
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLibOnProtocolPpi / SmmCryptLib.c
1 /** @file
2 Implements the GetCryptoServices() API that retuns a pointer to the EDK II
3 SMM Crypto Protocol.
4
5 Copyright (C) Microsoft Corporation. All rights reserved.
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <PiSmm.h>
11 #include <Library/BaseLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/SmmServicesTableLib.h>
14 #include <Protocol/SmmCrypto.h>
15
16 EDKII_SMM_CRYPTO_PROTOCOL *mSmmCryptoProtocol = NULL;
17
18 /**
19 Internal worker function that returns the pointer to an EDK II Crypto
20 Protocol/PPI. The layout of the PPI, DXE Protocol, and SMM Protocol are
21 identical which allows the implementation of the BaseCryptLib functions that
22 call through a Protocol/PPI to be shared for the PEI, DXE, and SMM
23 implementations.
24
25 This SMM implementation returns the pointer to the EDK II SMM Crypto Protocol
26 that was found in the library constructor SmmCryptLibConstructor().
27 **/
28 VOID *
29 GetCryptoServices (
30 VOID
31 )
32 {
33 return (VOID *)mSmmCryptoProtocol;
34 }
35
36 /**
37 Constructor looks up the EDK II SMM Crypto Protocol and verifies that it is
38 not NULL and has a high enough version value to support all the BaseCryptLib
39 functions.
40
41 @param ImageHandle The firmware allocated handle for the EFI image.
42 @param SystemTable A pointer to the EFI System Table.
43
44 @retval EFI_SUCCESS The EDK II SMM Crypto Protocol was found.
45 @retval EFI_NOT_FOUND The EDK II SMM Crypto Protocol was not found.
46 **/
47 EFI_STATUS
48 EFIAPI
49 SmmCryptLibConstructor (
50 IN EFI_HANDLE ImageHandle,
51 IN EFI_SYSTEM_TABLE *SystemTable
52 )
53 {
54 EFI_STATUS Status;
55 UINTN Version;
56
57 Status = gSmst->SmmLocateProtocol (
58 &gEdkiiSmmCryptoProtocolGuid,
59 NULL,
60 (VOID **)&mSmmCryptoProtocol
61 );
62 if (EFI_ERROR (Status) || mSmmCryptoProtocol == NULL) {
63 DEBUG((DEBUG_ERROR, "[SmmCryptLib] Failed to locate Crypto SMM Protocol. Status = %r\n", Status));
64 ASSERT_EFI_ERROR (Status);
65 ASSERT (mSmmCryptoProtocol != NULL);
66 mSmmCryptoProtocol = NULL;
67 return EFI_NOT_FOUND;
68 }
69
70 Version = mSmmCryptoProtocol->GetVersion ();
71 if (Version < EDKII_CRYPTO_VERSION) {
72 DEBUG((DEBUG_ERROR, "[SmmCryptLib] Crypto SMM Protocol unsupported version %d\n", Version));
73 ASSERT (Version >= EDKII_CRYPTO_VERSION);
74 mSmmCryptoProtocol = NULL;
75 return EFI_NOT_FOUND;
76 }
77
78 return EFI_SUCCESS;
79 }