]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/LockBoxLib/LockBoxDxe.c
7dc2eea2395a23ebe3dfa97419f3132250506840
[mirror_edk2.git] / OvmfPkg / Library / LockBoxLib / LockBoxDxe.c
1 /** @file
2
3 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
4
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <Uefi.h>
10
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>
17
18 /**
19 Allocate memory below 4G memory address.
20
21 This function allocates memory below 4G memory address.
22
23 @param MemoryType Memory type of memory to allocate.
24 @param Size Size of memory to allocate.
25
26 @return Allocated address for output.
27
28 **/
29 STATIC
30 VOID *
31 AllocateMemoryBelow4G (
32 IN EFI_MEMORY_TYPE MemoryType,
33 IN UINTN Size
34 )
35 {
36 UINTN Pages;
37 EFI_PHYSICAL_ADDRESS Address;
38 EFI_STATUS Status;
39 VOID* Buffer;
40 UINTN AllocRemaining;
41
42 Pages = EFI_SIZE_TO_PAGES (Size);
43 Address = 0xffffffff;
44
45 //
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.
50 //
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;
56 return Buffer;
57 }
58
59 Status = gBS->AllocatePages (
60 AllocateMaxAddress,
61 MemoryType,
62 Pages,
63 &Address
64 );
65 if (EFI_ERROR (Status)) {
66 return NULL;
67 }
68
69 Buffer = (VOID *) (UINTN) Address;
70 ZeroMem (Buffer, EFI_PAGES_TO_SIZE (Pages));
71
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;
76 }
77
78 return Buffer;
79 }
80
81
82 /**
83 Allocates a buffer of type EfiACPIMemoryNVS.
84
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.
90
91 @param AllocationSize The number of bytes to allocate.
92
93 @return A pointer to the allocated buffer or NULL if allocation fails.
94
95 **/
96 VOID *
97 EFIAPI
98 AllocateAcpiNvsPool (
99 IN UINTN AllocationSize
100 )
101 {
102 return AllocateMemoryBelow4G (EfiACPIMemoryNVS, AllocationSize);
103 }
104
105
106 EFI_STATUS
107 EFIAPI
108 LockBoxDxeLibInitialize (
109 IN EFI_HANDLE ImageHandle,
110 IN EFI_SYSTEM_TABLE *SystemTable
111 )
112 {
113 EFI_STATUS Status;
114 VOID *Interface;
115
116 Status = LockBoxLibInitialize ();
117 if (!EFI_ERROR (Status)) {
118 if (PcdGetBool (PcdAcpiS3Enable)) {
119 //
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.
124 //
125 Status = gBS->LocateProtocol (&gEfiLockBoxProtocolGuid, NULL, &Interface);
126 if (EFI_ERROR (Status)) {
127 Status = gBS->InstallProtocolInterface (
128 &ImageHandle,
129 &gEfiLockBoxProtocolGuid,
130 EFI_NATIVE_INTERFACE,
131 NULL
132 );
133 ASSERT_EFI_ERROR (Status);
134 }
135 }
136 }
137
138 return Status;
139 }