1 | /** @file
|
---|
2 |
|
---|
3 |
|
---|
4 | Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
|
---|
5 | Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
|
---|
6 |
|
---|
7 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
8 |
|
---|
9 | **/
|
---|
10 |
|
---|
11 | #include "UefiPayloadEntry.h"
|
---|
12 |
|
---|
13 | /**
|
---|
14 | Allocates one or more pages of type EfiBootServicesData.
|
---|
15 |
|
---|
16 | Allocates the number of pages of MemoryType and returns a pointer to the
|
---|
17 | allocated buffer. The buffer returned is aligned on a 4KB boundary.
|
---|
18 | If Pages is 0, then NULL is returned.
|
---|
19 | If there is not enough memory availble to satisfy the request, then NULL
|
---|
20 | is returned.
|
---|
21 |
|
---|
22 | @param Pages The number of 4 KB pages to allocate.
|
---|
23 | @param MemoryType The MemoryType
|
---|
24 | @return A pointer to the allocated buffer or NULL if allocation fails.
|
---|
25 | **/
|
---|
26 | VOID *
|
---|
27 | EFIAPI
|
---|
28 | PayloadAllocatePages (
|
---|
29 | IN UINTN Pages,
|
---|
30 | IN EFI_MEMORY_TYPE MemoryType
|
---|
31 | )
|
---|
32 | {
|
---|
33 | EFI_PEI_HOB_POINTERS Hob;
|
---|
34 | EFI_PHYSICAL_ADDRESS Offset;
|
---|
35 | EFI_HOB_HANDOFF_INFO_TABLE *HobTable;
|
---|
36 |
|
---|
37 | Hob.Raw = GetHobList ();
|
---|
38 | HobTable = Hob.HandoffInformationTable;
|
---|
39 |
|
---|
40 | if (Pages == 0) {
|
---|
41 | return NULL;
|
---|
42 | }
|
---|
43 |
|
---|
44 | // Make sure allocation address is page alligned.
|
---|
45 | Offset = HobTable->EfiFreeMemoryTop & EFI_PAGE_MASK;
|
---|
46 | if (Offset != 0) {
|
---|
47 | HobTable->EfiFreeMemoryTop -= Offset;
|
---|
48 | }
|
---|
49 |
|
---|
50 | //
|
---|
51 | // Check available memory for the allocation
|
---|
52 | //
|
---|
53 | if (HobTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < HobTable->EfiFreeMemoryBottom) {
|
---|
54 | return NULL;
|
---|
55 | }
|
---|
56 |
|
---|
57 | HobTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
|
---|
58 | BuildMemoryAllocationHob (HobTable->EfiFreeMemoryTop, Pages * EFI_PAGE_SIZE, MemoryType);
|
---|
59 |
|
---|
60 | return (VOID *)(UINTN)HobTable->EfiFreeMemoryTop;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | Allocates one or more pages of type EfiBootServicesData.
|
---|
65 |
|
---|
66 | Allocates the number of pages of MemoryType and returns a pointer to the
|
---|
67 | allocated buffer. The buffer returned is aligned on a 4KB boundary.
|
---|
68 | If Pages is 0, then NULL is returned.
|
---|
69 | If there is not enough memory availble to satisfy the request, then NULL
|
---|
70 | is returned.
|
---|
71 |
|
---|
72 | @param Pages The number of 4 KB pages to allocate.
|
---|
73 | @return A pointer to the allocated buffer or NULL if allocation fails.
|
---|
74 | **/
|
---|
75 | VOID *
|
---|
76 | EFIAPI
|
---|
77 | AllocatePages (
|
---|
78 | IN UINTN Pages
|
---|
79 | )
|
---|
80 | {
|
---|
81 | EFI_PEI_HOB_POINTERS Hob;
|
---|
82 | EFI_PHYSICAL_ADDRESS Offset;
|
---|
83 | EFI_HOB_HANDOFF_INFO_TABLE *HobTable;
|
---|
84 |
|
---|
85 | Hob.Raw = GetHobList ();
|
---|
86 | HobTable = Hob.HandoffInformationTable;
|
---|
87 |
|
---|
88 | if (Pages == 0) {
|
---|
89 | return NULL;
|
---|
90 | }
|
---|
91 |
|
---|
92 | // Make sure allocation address is page aligned.
|
---|
93 | Offset = HobTable->EfiFreeMemoryTop & EFI_PAGE_MASK;
|
---|
94 | if (Offset != 0) {
|
---|
95 | HobTable->EfiFreeMemoryTop -= Offset;
|
---|
96 | }
|
---|
97 |
|
---|
98 | //
|
---|
99 | // Check available memory for the allocation
|
---|
100 | //
|
---|
101 | if (HobTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < HobTable->EfiFreeMemoryBottom) {
|
---|
102 | return NULL;
|
---|
103 | }
|
---|
104 |
|
---|
105 | HobTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
|
---|
106 | BuildMemoryAllocationHob (HobTable->EfiFreeMemoryTop, Pages * EFI_PAGE_SIZE, EfiBootServicesData);
|
---|
107 |
|
---|
108 | return (VOID *)(UINTN)HobTable->EfiFreeMemoryTop;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | Frees one or more 4KB pages that were previously allocated with one of the page allocation
|
---|
113 | functions in the Memory Allocation Library.
|
---|
114 |
|
---|
115 | Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
|
---|
116 | must have been allocated on a previous call to the page allocation services of the Memory
|
---|
117 | Allocation Library. If it is not possible to free allocated pages, then this function will
|
---|
118 | perform no actions.
|
---|
119 |
|
---|
120 | If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
|
---|
121 | then ASSERT().
|
---|
122 | If Pages is zero, then ASSERT().
|
---|
123 |
|
---|
124 | @param Buffer Pointer to the buffer of pages to free.
|
---|
125 | @param Pages The number of 4 KB pages to free.
|
---|
126 |
|
---|
127 | **/
|
---|
128 | VOID
|
---|
129 | EFIAPI
|
---|
130 | FreePages (
|
---|
131 | IN VOID *Buffer,
|
---|
132 | IN UINTN Pages
|
---|
133 | )
|
---|
134 | {
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | Allocates one or more pages of type EfiBootServicesData at a specified alignment.
|
---|
139 |
|
---|
140 | Allocates the number of pages specified by Pages of type EfiBootServicesData with an
|
---|
141 | alignment specified by Alignment.
|
---|
142 | If Pages is 0, then NULL is returned.
|
---|
143 | If Alignment is not a power of two and Alignment is not zero, then ASSERT().
|
---|
144 | If there is no enough memory at the specified alignment available to satisfy the
|
---|
145 | request, then NULL is returned.
|
---|
146 |
|
---|
147 | @param Pages The number of 4 KB pages to allocate.
|
---|
148 | @param Alignment The requested alignment of the allocation.
|
---|
149 |
|
---|
150 | @return A pointer to the allocated buffer or NULL if allocation fails.
|
---|
151 | **/
|
---|
152 | VOID *
|
---|
153 | EFIAPI
|
---|
154 | AllocateAlignedPages (
|
---|
155 | IN UINTN Pages,
|
---|
156 | IN UINTN Alignment
|
---|
157 | )
|
---|
158 | {
|
---|
159 | VOID *Memory;
|
---|
160 | UINTN AlignmentMask;
|
---|
161 |
|
---|
162 | //
|
---|
163 | // Alignment must be a power of two or zero.
|
---|
164 | //
|
---|
165 | ASSERT ((Alignment & (Alignment - 1)) == 0);
|
---|
166 |
|
---|
167 | if (Pages == 0) {
|
---|
168 | return NULL;
|
---|
169 | }
|
---|
170 |
|
---|
171 | //
|
---|
172 | // Check overflow.
|
---|
173 | //
|
---|
174 | ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));
|
---|
175 |
|
---|
176 | Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));
|
---|
177 | if (Memory == NULL) {
|
---|
178 | return NULL;
|
---|
179 | }
|
---|
180 |
|
---|
181 | if (Alignment == 0) {
|
---|
182 | AlignmentMask = Alignment;
|
---|
183 | } else {
|
---|
184 | AlignmentMask = Alignment - 1;
|
---|
185 | }
|
---|
186 |
|
---|
187 | return (VOID *)(UINTN)(((UINTN)Memory + AlignmentMask) & ~AlignmentMask);
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | Allocates a buffer of type EfiBootServicesData.
|
---|
192 |
|
---|
193 | Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
|
---|
194 | pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
|
---|
195 | returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
|
---|
196 |
|
---|
197 | @param AllocationSize The number of bytes to allocate.
|
---|
198 |
|
---|
199 | @return A pointer to the allocated buffer or NULL if allocation fails.
|
---|
200 |
|
---|
201 | **/
|
---|
202 | VOID *
|
---|
203 | EFIAPI
|
---|
204 | AllocatePool (
|
---|
205 | IN UINTN AllocationSize
|
---|
206 | )
|
---|
207 | {
|
---|
208 | EFI_HOB_MEMORY_POOL *Hob;
|
---|
209 |
|
---|
210 | if (AllocationSize > 0x4000) {
|
---|
211 | // Please use AllocatePages for big allocations
|
---|
212 | return NULL;
|
---|
213 | }
|
---|
214 |
|
---|
215 | Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (EFI_HOB_TYPE_MEMORY_POOL, (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + AllocationSize));
|
---|
216 | return (VOID *)(Hob + 1);
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | Allocates and zeros a buffer of type EfiBootServicesData.
|
---|
221 |
|
---|
222 | Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
|
---|
223 | buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
|
---|
224 | valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
|
---|
225 | request, then NULL is returned.
|
---|
226 |
|
---|
227 | @param AllocationSize The number of bytes to allocate and zero.
|
---|
228 |
|
---|
229 | @return A pointer to the allocated buffer or NULL if allocation fails.
|
---|
230 |
|
---|
231 | **/
|
---|
232 | VOID *
|
---|
233 | EFIAPI
|
---|
234 | AllocateZeroPool (
|
---|
235 | IN UINTN AllocationSize
|
---|
236 | )
|
---|
237 | {
|
---|
238 | VOID *Buffer;
|
---|
239 |
|
---|
240 | Buffer = AllocatePool (AllocationSize);
|
---|
241 | if (Buffer == NULL) {
|
---|
242 | return NULL;
|
---|
243 | }
|
---|
244 |
|
---|
245 | ZeroMem (Buffer, AllocationSize);
|
---|
246 |
|
---|
247 | return Buffer;
|
---|
248 | }
|
---|