]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/LockBoxLib/LockBoxDxe.c
OvmfPkg: implement LockBoxLib
[mirror_edk2.git] / OvmfPkg / Library / LockBoxLib / LockBoxDxe.c
CommitLineData
6a7cba79
LE
1/** @file\r
2\r
3 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
4\r
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <Uefi.h>\r
16\r
17#include <Library/MemoryAllocationLib.h>\r
18#include <Library/UefiBootServicesTableLib.h>\r
19#include <Library/BaseMemoryLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <LockBoxLib.h>\r
22\r
23/**\r
24 Allocate memory below 4G memory address.\r
25\r
26 This function allocates memory below 4G memory address.\r
27\r
28 @param MemoryType Memory type of memory to allocate.\r
29 @param Size Size of memory to allocate.\r
30 \r
31 @return Allocated address for output.\r
32\r
33**/\r
34STATIC\r
35VOID *\r
36AllocateMemoryBelow4G (\r
37 IN EFI_MEMORY_TYPE MemoryType,\r
38 IN UINTN Size\r
39 )\r
40{\r
41 UINTN Pages;\r
42 EFI_PHYSICAL_ADDRESS Address;\r
43 EFI_STATUS Status;\r
44 VOID* Buffer;\r
45 UINTN AllocRemaining;\r
46\r
47 Pages = EFI_SIZE_TO_PAGES (Size);\r
48 Address = 0xffffffff;\r
49\r
50 //\r
51 // Since we need to use gBS->AllocatePages to get a buffer below\r
52 // 4GB, there is a good chance that space will be wasted for very\r
53 // small allocation. We keep track of unused portions of the page\r
54 // allocations, and use these to allocate memory for small buffers.\r
55 //\r
56 ASSERT (mLockBoxGlobal->Signature == LOCK_BOX_GLOBAL_SIGNATURE);\r
57 if ((UINTN) mLockBoxGlobal->SubPageRemaining >= Size) {\r
58 Buffer = (VOID*)(UINTN) mLockBoxGlobal->SubPageBuffer;\r
59 mLockBoxGlobal->SubPageBuffer += (UINT32) Size;\r
60 mLockBoxGlobal->SubPageRemaining -= (UINT32) Size;\r
61 return Buffer;\r
62 }\r
63\r
64 Status = gBS->AllocatePages (\r
65 AllocateMaxAddress,\r
66 MemoryType,\r
67 Pages,\r
68 &Address\r
69 );\r
70 if (EFI_ERROR (Status)) {\r
71 return NULL;\r
72 }\r
73\r
74 Buffer = (VOID *) (UINTN) Address;\r
75 ZeroMem (Buffer, EFI_PAGES_TO_SIZE (Pages));\r
76\r
77 AllocRemaining = EFI_PAGES_TO_SIZE (Pages) - Size;\r
78 if (AllocRemaining > (UINTN) mLockBoxGlobal->SubPageRemaining) {\r
79 mLockBoxGlobal->SubPageBuffer = (UINT32) (Address + Size);\r
80 mLockBoxGlobal->SubPageRemaining = (UINT32) AllocRemaining;\r
81 }\r
82\r
83 return Buffer;\r
84}\r
85\r
86\r
87/**\r
88 Allocates a buffer of type EfiACPIMemoryNVS.\r
89\r
90 Allocates the number bytes specified by AllocationSize of type\r
91 EfiACPIMemoryNVS and returns a pointer to the allocated buffer.\r
92 If AllocationSize is 0, then a valid buffer of 0 size is\r
93 returned. If there is not enough memory remaining to satisfy\r
94 the request, then NULL is returned.\r
95\r
96 @param AllocationSize The number of bytes to allocate.\r
97\r
98 @return A pointer to the allocated buffer or NULL if allocation fails.\r
99\r
100**/\r
101VOID *\r
102EFIAPI\r
103AllocateAcpiNvsPool (\r
104 IN UINTN AllocationSize\r
105 )\r
106{\r
107 return AllocateMemoryBelow4G (EfiACPIMemoryNVS, AllocationSize);\r
108}\r
109\r
110\r
111EFI_STATUS\r
112EFIAPI\r
113LockBoxDxeLibInitialize (\r
114 IN EFI_HANDLE ImageHandle,\r
115 IN EFI_SYSTEM_TABLE *SystemTable\r
116 )\r
117{\r
118 return LockBoxLibInitialize ();\r
119}\r