3 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/MemoryAllocationLib.h>
12 #include <Library/UefiBootServicesTableLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/DebugLib.h>
15 #include <Protocol/LockBox.h>
16 #include <LockBoxLib.h>
19 Allocate memory below 4G memory address.
21 This function allocates memory below 4G memory address.
23 @param MemoryType Memory type of memory to allocate.
24 @param Size Size of memory to allocate.
26 @return Allocated address for output.
31 AllocateMemoryBelow4G (
32 IN EFI_MEMORY_TYPE MemoryType
,
37 EFI_PHYSICAL_ADDRESS Address
;
42 Pages
= EFI_SIZE_TO_PAGES (Size
);
46 // Since we need to use gBS->AllocatePages to get a buffer below
47 // 4GB, there is a good chance that space will be wasted for very
48 // small allocation. We keep track of unused portions of the page
49 // allocations, and use these to allocate memory for small buffers.
51 ASSERT (mLockBoxGlobal
->Signature
== LOCK_BOX_GLOBAL_SIGNATURE
);
52 if ((UINTN
) mLockBoxGlobal
->SubPageRemaining
>= Size
) {
53 Buffer
= (VOID
*)(UINTN
) mLockBoxGlobal
->SubPageBuffer
;
54 mLockBoxGlobal
->SubPageBuffer
+= (UINT32
) Size
;
55 mLockBoxGlobal
->SubPageRemaining
-= (UINT32
) Size
;
59 Status
= gBS
->AllocatePages (
65 if (EFI_ERROR (Status
)) {
69 Buffer
= (VOID
*) (UINTN
) Address
;
70 ZeroMem (Buffer
, EFI_PAGES_TO_SIZE (Pages
));
72 AllocRemaining
= EFI_PAGES_TO_SIZE (Pages
) - Size
;
73 if (AllocRemaining
> (UINTN
) mLockBoxGlobal
->SubPageRemaining
) {
74 mLockBoxGlobal
->SubPageBuffer
= (UINT32
) (Address
+ Size
);
75 mLockBoxGlobal
->SubPageRemaining
= (UINT32
) AllocRemaining
;
83 Allocates a buffer of type EfiACPIMemoryNVS.
85 Allocates the number bytes specified by AllocationSize of type
86 EfiACPIMemoryNVS and returns a pointer to the allocated buffer.
87 If AllocationSize is 0, then a valid buffer of 0 size is
88 returned. If there is not enough memory remaining to satisfy
89 the request, then NULL is returned.
91 @param AllocationSize The number of bytes to allocate.
93 @return A pointer to the allocated buffer or NULL if allocation fails.
99 IN UINTN AllocationSize
102 return AllocateMemoryBelow4G (EfiACPIMemoryNVS
, AllocationSize
);
108 LockBoxDxeLibInitialize (
109 IN EFI_HANDLE ImageHandle
,
110 IN EFI_SYSTEM_TABLE
*SystemTable
116 Status
= LockBoxLibInitialize ();
117 if (!EFI_ERROR (Status
)) {
118 if (PcdGetBool (PcdAcpiS3Enable
)) {
120 // When S3 enabled, the first driver run with this library linked will
121 // have this library constructor to install LockBox protocol on the
122 // ImageHandle. As other drivers may have gEfiLockBoxProtocolGuid
123 // dependency, the first driver should run before them.
125 Status
= gBS
->LocateProtocol (&gEfiLockBoxProtocolGuid
, NULL
, &Interface
);
126 if (EFI_ERROR (Status
)) {
127 Status
= gBS
->InstallProtocolInterface (
129 &gEfiLockBoxProtocolGuid
,
130 EFI_NATIVE_INTERFACE
,
133 ASSERT_EFI_ERROR (Status
);