]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | Support routines for memory allocation routines \r | |
3 | based on PeiService for PEI phase drivers.\r | |
4 | \r | |
5 | Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r | |
6 | This program and the accompanying materials \r | |
7 | are licensed and made available under the terms and conditions of the BSD License \r | |
8 | which accompanies this distribution. The full text of the license may be found at \r | |
9 | http://opensource.org/licenses/bsd-license.php. \r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | \r | |
17 | #include <PiPei.h>\r | |
18 | \r | |
19 | \r | |
20 | #include <Library/MemoryAllocationLib.h>\r | |
21 | #include <Library/PeiServicesLib.h>\r | |
22 | #include <Library/BaseMemoryLib.h>\r | |
23 | #include <Library/DebugLib.h>\r | |
24 | #include <Library/HobLib.h>\r | |
25 | \r | |
26 | \r | |
27 | /**\r | |
28 | Allocates one or more 4KB pages of a certain memory type.\r | |
29 | \r | |
30 | Allocates the number of 4KB pages of a certain memory type and returns a pointer to the allocated\r | |
31 | buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL is returned.\r | |
32 | If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
33 | \r | |
34 | @param MemoryType The type of memory to allocate.\r | |
35 | @param Pages The number of 4 KB pages to allocate.\r | |
36 | \r | |
37 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
38 | \r | |
39 | **/\r | |
40 | VOID *\r | |
41 | InternalAllocatePages (\r | |
42 | IN EFI_MEMORY_TYPE MemoryType, \r | |
43 | IN UINTN Pages\r | |
44 | )\r | |
45 | {\r | |
46 | EFI_STATUS Status;\r | |
47 | EFI_PHYSICAL_ADDRESS Memory; \r | |
48 | \r | |
49 | if (Pages == 0) {\r | |
50 | return NULL;\r | |
51 | }\r | |
52 | \r | |
53 | Status = PeiServicesAllocatePages (MemoryType, Pages, &Memory);\r | |
54 | if (EFI_ERROR (Status)) {\r | |
55 | return NULL;\r | |
56 | }\r | |
57 | \r | |
58 | return (VOID *) (UINTN) Memory;\r | |
59 | }\r | |
60 | \r | |
61 | /**\r | |
62 | Allocates one or more 4KB pages of type EfiBootServicesData.\r | |
63 | \r | |
64 | Allocates the number of 4KB pages of type EfiBootServicesData and returns a pointer to the\r | |
65 | allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r | |
66 | is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r | |
67 | returned.\r | |
68 | \r | |
69 | @param Pages The number of 4 KB pages to allocate.\r | |
70 | \r | |
71 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
72 | \r | |
73 | **/\r | |
74 | VOID *\r | |
75 | EFIAPI\r | |
76 | AllocatePages (\r | |
77 | IN UINTN Pages\r | |
78 | )\r | |
79 | {\r | |
80 | return InternalAllocatePages (EfiBootServicesData, Pages);\r | |
81 | }\r | |
82 | \r | |
83 | /**\r | |
84 | Allocates one or more 4KB pages of type EfiRuntimeServicesData.\r | |
85 | \r | |
86 | Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the\r | |
87 | allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r | |
88 | is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r | |
89 | returned.\r | |
90 | \r | |
91 | @param Pages The number of 4 KB pages to allocate.\r | |
92 | \r | |
93 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
94 | \r | |
95 | **/\r | |
96 | VOID *\r | |
97 | EFIAPI\r | |
98 | AllocateRuntimePages (\r | |
99 | IN UINTN Pages\r | |
100 | )\r | |
101 | {\r | |
102 | return InternalAllocatePages (EfiRuntimeServicesData, Pages);\r | |
103 | }\r | |
104 | \r | |
105 | /**\r | |
106 | Allocates one or more 4KB pages of type EfiReservedMemoryType.\r | |
107 | \r | |
108 | Allocates the number of 4KB pages of type EfiReservedMemoryType and returns a pointer to the\r | |
109 | allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r | |
110 | is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r | |
111 | returned.\r | |
112 | \r | |
113 | @param Pages The number of 4 KB pages to allocate.\r | |
114 | \r | |
115 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
116 | \r | |
117 | **/\r | |
118 | VOID *\r | |
119 | EFIAPI\r | |
120 | AllocateReservedPages (\r | |
121 | IN UINTN Pages\r | |
122 | )\r | |
123 | {\r | |
124 | return InternalAllocatePages (EfiReservedMemoryType, Pages);\r | |
125 | }\r | |
126 | \r | |
127 | /**\r | |
128 | Frees one or more 4KB pages that were previously allocated with one of the page allocation\r | |
129 | functions in the Memory Allocation Library.\r | |
130 | \r | |
131 | Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r | |
132 | must have been allocated on a previous call to the page allocation services of the Memory\r | |
133 | Allocation Library. If it is not possible to free allocated pages, then this function will\r | |
134 | perform no actions.\r | |
135 | \r | |
136 | If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r | |
137 | then ASSERT().\r | |
138 | If Pages is zero, then ASSERT().\r | |
139 | \r | |
140 | @param Buffer The pointer to the buffer of pages to free.\r | |
141 | @param Pages The number of 4 KB pages to free.\r | |
142 | \r | |
143 | **/\r | |
144 | VOID\r | |
145 | EFIAPI\r | |
146 | FreePages (\r | |
147 | IN VOID *Buffer,\r | |
148 | IN UINTN Pages\r | |
149 | )\r | |
150 | {\r | |
151 | ASSERT (Pages != 0);\r | |
152 | //\r | |
153 | // PEI phase does not support to free pages, so leave it as NOP.\r | |
154 | //\r | |
155 | }\r | |
156 | \r | |
157 | /**\r | |
158 | Allocates one or more 4KB pages of a certain memory type at a specified alignment.\r | |
159 | \r | |
160 | Allocates the number of 4KB pages specified by Pages of a certain memory type with an alignment\r | |
161 | specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is returned.\r | |
162 | If there is not enough memory at the specified alignment remaining to satisfy the request, then\r | |
163 | NULL is returned.\r | |
164 | If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r | |
165 | If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r | |
166 | \r | |
167 | @param MemoryType The type of memory to allocate.\r | |
168 | @param Pages The number of 4 KB pages to allocate.\r | |
169 | @param Alignment The requested alignment of the allocation. \r | |
170 | Must be a power of two.\r | |
171 | If Alignment is zero, then byte alignment is used.\r | |
172 | \r | |
173 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
174 | \r | |
175 | **/\r | |
176 | VOID *\r | |
177 | InternalAllocateAlignedPages (\r | |
178 | IN EFI_MEMORY_TYPE MemoryType, \r | |
179 | IN UINTN Pages,\r | |
180 | IN UINTN Alignment\r | |
181 | )\r | |
182 | {\r | |
183 | EFI_PHYSICAL_ADDRESS Memory;\r | |
184 | EFI_PHYSICAL_ADDRESS AlignedMemory;\r | |
185 | EFI_PEI_HOB_POINTERS Hob;\r | |
186 | BOOLEAN SkipBeforeMemHob;\r | |
187 | BOOLEAN SkipAfterMemHob;\r | |
188 | EFI_PHYSICAL_ADDRESS HobBaseAddress;\r | |
189 | UINT64 HobLength;\r | |
190 | EFI_MEMORY_TYPE HobMemoryType;\r | |
191 | UINTN TotalPages;\r | |
192 | \r | |
193 | //\r | |
194 | // Alignment must be a power of two or zero.\r | |
195 | //\r | |
196 | ASSERT ((Alignment & (Alignment - 1)) == 0);\r | |
197 | \r | |
198 | if (Pages == 0) {\r | |
199 | return NULL;\r | |
200 | }\r | |
201 | //\r | |
202 | // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r | |
203 | //\r | |
204 | ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment))); \r | |
205 | \r | |
206 | //\r | |
207 | // We would rather waste some memory to save PEI code size.\r | |
208 | // meaning in addition to the requested size for the aligned mem,\r | |
209 | // we simply reserve an overhead memory equal to Alignmemt(page-aligned), no matter what.\r | |
210 | // The overhead mem size could be reduced later with more involved malloc mechanisms\r | |
211 | // (e.g., somthing that can detect the alignment boundary before allocating memory or \r | |
212 | // can request that memory be allocated at a certain address that is aleady aligned).\r | |
213 | //\r | |
214 | TotalPages = Pages + (Alignment <= EFI_PAGE_SIZE ? 0 : EFI_SIZE_TO_PAGES(Alignment));\r | |
215 | Memory = (EFI_PHYSICAL_ADDRESS) (UINTN) InternalAllocatePages (MemoryType, TotalPages);\r | |
216 | if (Memory == 0) {\r | |
217 | DEBUG((DEBUG_INFO, "Out of memory resource! \n"));\r | |
218 | return NULL;\r | |
219 | }\r | |
220 | DEBUG ((DEBUG_INFO, "Allocated Memory unaligned: Address = 0x%LX, Pages = 0x%X, Type = %d \n", Memory, TotalPages, (UINTN) MemoryType));\r | |
221 | \r | |
222 | //\r | |
223 | // Alignment calculation\r | |
224 | //\r | |
225 | AlignedMemory = Memory;\r | |
226 | if (Alignment > EFI_PAGE_SIZE) {\r | |
227 | AlignedMemory = ALIGN_VALUE (Memory, Alignment);\r | |
228 | }\r | |
229 | DEBUG ((DEBUG_INFO, "After aligning to 0x%X bytes: Address = 0x%LX, Pages = 0x%X \n", Alignment, AlignedMemory, Pages));\r | |
230 | \r | |
231 | //\r | |
232 | // In general three HOBs cover the total allocated space.\r | |
233 | // The aligned portion is covered by the aligned mem HOB and\r | |
234 | // the unaligned(to be freed) portions before and after the aligned portion are covered by newly created HOBs.\r | |
235 | //\r | |
236 | // Before mem HOB covers the region between "Memory" and "AlignedMemory"\r | |
237 | // Aligned mem HOB covers the region between "AlignedMemory" and "AlignedMemory + EFI_PAGES_TO_SIZE(Pages)"\r | |
238 | // After mem HOB covers the region between "AlignedMemory + EFI_PAGES_TO_SIZE(Pages)" and "Memory + EFI_PAGES_TO_SIZE(TotalPages)"\r | |
239 | //\r | |
240 | // The before or after mem HOBs need to be skipped under special cases where the aligned portion\r | |
241 | // touches either the top or bottom of the original allocated space.\r | |
242 | //\r | |
243 | SkipBeforeMemHob = FALSE;\r | |
244 | SkipAfterMemHob = FALSE;\r | |
245 | if (Memory == AlignedMemory) {\r | |
246 | SkipBeforeMemHob = TRUE;\r | |
247 | }\r | |
248 | if ((Memory + EFI_PAGES_TO_SIZE(TotalPages)) == (AlignedMemory + EFI_PAGES_TO_SIZE(Pages))) {\r | |
249 | //\r | |
250 | // This condition is never met in the current implementation.\r | |
251 | // There is always some after-mem since the overhead mem(used in TotalPages)\r | |
252 | // is no less than Alignment.\r | |
253 | //\r | |
254 | SkipAfterMemHob = TRUE;\r | |
255 | }\r | |
256 | \r | |
257 | // \r | |
258 | // Search for the mem HOB referring to the original(unaligned) allocation \r | |
259 | // and update the size and type if needed.\r | |
260 | //\r | |
261 | Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);\r | |
262 | while (Hob.Raw != NULL) {\r | |
263 | if (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress == Memory) {\r | |
264 | break;\r | |
265 | }\r | |
266 | Hob.Raw = GET_NEXT_HOB (Hob);\r | |
267 | Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw);\r | |
268 | }\r | |
269 | ASSERT (Hob.Raw != NULL);\r | |
270 | if (SkipBeforeMemHob) {\r | |
271 | //\r | |
272 | // Use this HOB as aligned mem HOB as there is no portion before it.\r | |
273 | //\r | |
274 | HobLength = EFI_PAGES_TO_SIZE(Pages);\r | |
275 | Hob.MemoryAllocation->AllocDescriptor.MemoryLength = HobLength;\r | |
276 | } else {\r | |
277 | //\r | |
278 | // Use this HOB as before mem HOB and create a new HOB for the aligned portion \r | |
279 | //\r | |
280 | HobLength = (AlignedMemory - Memory); \r | |
281 | Hob.MemoryAllocation->AllocDescriptor.MemoryLength = HobLength;\r | |
282 | Hob.MemoryAllocation->AllocDescriptor.MemoryType = EfiConventionalMemory;\r | |
283 | }\r | |
284 | \r | |
285 | HobBaseAddress = Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress;\r | |
286 | HobMemoryType = Hob.MemoryAllocation->AllocDescriptor.MemoryType;\r | |
287 | \r | |
288 | //\r | |
289 | // Build the aligned mem HOB if needed\r | |
290 | //\r | |
291 | if (!SkipBeforeMemHob) {\r | |
292 | DEBUG((DEBUG_INFO, "Updated before-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",\r | |
293 | HobBaseAddress, HobLength, (UINTN) HobMemoryType));\r | |
294 | \r | |
295 | HobBaseAddress = AlignedMemory;\r | |
296 | HobLength = EFI_PAGES_TO_SIZE(Pages);\r | |
297 | HobMemoryType = MemoryType;\r | |
298 | \r | |
299 | BuildMemoryAllocationHob (\r | |
300 | HobBaseAddress,\r | |
301 | HobLength,\r | |
302 | HobMemoryType\r | |
303 | );\r | |
304 | \r | |
305 | DEBUG((DEBUG_INFO, "Created aligned-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",\r | |
306 | HobBaseAddress, HobLength, (UINTN) HobMemoryType));\r | |
307 | } else {\r | |
308 | if (HobBaseAddress != 0) {\r | |
309 | DEBUG((DEBUG_INFO, "Updated aligned-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",\r | |
310 | HobBaseAddress, HobLength, (UINTN) HobMemoryType));\r | |
311 | }\r | |
312 | }\r | |
313 | \r | |
314 | \r | |
315 | //\r | |
316 | // Build the after mem HOB if needed\r | |
317 | //\r | |
318 | if (!SkipAfterMemHob) {\r | |
319 | HobBaseAddress = AlignedMemory + EFI_PAGES_TO_SIZE(Pages);\r | |
320 | HobLength = (Memory + EFI_PAGES_TO_SIZE(TotalPages)) - (AlignedMemory + EFI_PAGES_TO_SIZE(Pages));\r | |
321 | HobMemoryType = EfiConventionalMemory;\r | |
322 | \r | |
323 | BuildMemoryAllocationHob (\r | |
324 | HobBaseAddress,\r | |
325 | HobLength,\r | |
326 | HobMemoryType\r | |
327 | );\r | |
328 | \r | |
329 | DEBUG((DEBUG_INFO, "Created after-mem HOB with BaseAddress = %LX, Length = %LX, MemoryType = %d \n",\r | |
330 | HobBaseAddress, HobLength, (UINTN) HobMemoryType));\r | |
331 | }\r | |
332 | \r | |
333 | return (VOID *) (UINTN) AlignedMemory;\r | |
334 | }\r | |
335 | \r | |
336 | /**\r | |
337 | Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r | |
338 | \r | |
339 | Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r | |
340 | alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r | |
341 | returned. If there is not enough memory at the specified alignment remaining to satisfy the\r | |
342 | request, then NULL is returned.\r | |
343 | \r | |
344 | If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r | |
345 | If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r | |
346 | \r | |
347 | @param Pages The number of 4 KB pages to allocate.\r | |
348 | @param Alignment The requested alignment of the allocation. \r | |
349 | Must be a power of two.\r | |
350 | If Alignment is zero, then byte alignment is used.\r | |
351 | \r | |
352 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
353 | \r | |
354 | **/\r | |
355 | VOID *\r | |
356 | EFIAPI\r | |
357 | AllocateAlignedPages (\r | |
358 | IN UINTN Pages,\r | |
359 | IN UINTN Alignment\r | |
360 | )\r | |
361 | {\r | |
362 | return InternalAllocateAlignedPages (EfiBootServicesData, Pages, Alignment);\r | |
363 | }\r | |
364 | \r | |
365 | /**\r | |
366 | Allocates one or more 4KB pages of type EfiRuntimeServicesData at a specified alignment.\r | |
367 | \r | |
368 | Allocates the number of 4KB pages specified by Pages of type EfiRuntimeServicesData with an\r | |
369 | alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r | |
370 | returned. If there is not enough memory at the specified alignment remaining to satisfy the\r | |
371 | request, then NULL is returned.\r | |
372 | \r | |
373 | If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r | |
374 | If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r | |
375 | \r | |
376 | @param Pages The number of 4 KB pages to allocate.\r | |
377 | @param Alignment The requested alignment of the allocation. \r | |
378 | Must be a power of two.\r | |
379 | If Alignment is zero, then byte alignment is used.\r | |
380 | \r | |
381 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
382 | \r | |
383 | **/\r | |
384 | VOID *\r | |
385 | EFIAPI\r | |
386 | AllocateAlignedRuntimePages (\r | |
387 | IN UINTN Pages,\r | |
388 | IN UINTN Alignment\r | |
389 | )\r | |
390 | {\r | |
391 | return InternalAllocateAlignedPages (EfiRuntimeServicesData, Pages, Alignment);\r | |
392 | }\r | |
393 | \r | |
394 | /**\r | |
395 | Allocates one or more 4KB pages of type EfiReservedMemoryType at a specified alignment.\r | |
396 | \r | |
397 | Allocates the number of 4KB pages specified by Pages of type EfiReservedMemoryType with an\r | |
398 | alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r | |
399 | returned. If there is not enough memory at the specified alignment remaining to satisfy the\r | |
400 | request, then NULL is returned.\r | |
401 | \r | |
402 | If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r | |
403 | If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r | |
404 | \r | |
405 | @param Pages The number of 4 KB pages to allocate.\r | |
406 | @param Alignment The requested alignment of the allocation. \r | |
407 | Must be a power of two.\r | |
408 | If Alignment is zero, then byte alignment is used.\r | |
409 | \r | |
410 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
411 | \r | |
412 | **/\r | |
413 | VOID *\r | |
414 | EFIAPI\r | |
415 | AllocateAlignedReservedPages (\r | |
416 | IN UINTN Pages,\r | |
417 | IN UINTN Alignment\r | |
418 | )\r | |
419 | {\r | |
420 | return InternalAllocateAlignedPages (EfiReservedMemoryType, Pages, Alignment);\r | |
421 | }\r | |
422 | \r | |
423 | /**\r | |
424 | Frees one or more 4KB pages that were previously allocated with one of the aligned page\r | |
425 | allocation functions in the Memory Allocation Library.\r | |
426 | \r | |
427 | Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r | |
428 | must have been allocated on a previous call to the aligned page allocation services of the Memory\r | |
429 | Allocation Library. If it is not possible to free allocated pages, then this function will \r | |
430 | perform no actions.\r | |
431 | \r | |
432 | If Buffer was not allocated with an aligned page allocation function in the Memory Allocation\r | |
433 | Library, then ASSERT().\r | |
434 | If Pages is zero, then ASSERT().\r | |
435 | \r | |
436 | @param Buffer The pointer to the buffer of pages to free.\r | |
437 | @param Pages The number of 4 KB pages to free.\r | |
438 | \r | |
439 | **/\r | |
440 | VOID\r | |
441 | EFIAPI\r | |
442 | FreeAlignedPages (\r | |
443 | IN VOID *Buffer,\r | |
444 | IN UINTN Pages\r | |
445 | )\r | |
446 | {\r | |
447 | ASSERT (Pages != 0);\r | |
448 | //\r | |
449 | // PEI phase does not support to free pages, so leave it as NOP.\r | |
450 | //\r | |
451 | }\r | |
452 | \r | |
453 | /**\r | |
454 | Allocates a buffer of a certain pool type.\r | |
455 | \r | |
456 | Allocates the number bytes specified by AllocationSize of a certain pool type and returns a\r | |
457 | pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r | |
458 | returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
459 | \r | |
460 | @param MemoryType The type of memory to allocate.\r | |
461 | @param AllocationSize The number of bytes to allocate.\r | |
462 | \r | |
463 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
464 | \r | |
465 | **/\r | |
466 | VOID *\r | |
467 | InternalAllocatePool (\r | |
468 | IN EFI_MEMORY_TYPE MemoryType, \r | |
469 | IN UINTN AllocationSize\r | |
470 | )\r | |
471 | {\r | |
472 | //\r | |
473 | // If we need lots of small runtime/reserved memory type from PEI in the future, \r | |
474 | // we can consider providing a more complex algorithm that allocates runtime pages and \r | |
475 | // provide pool allocations from those pages. \r | |
476 | //\r | |
477 | return InternalAllocatePages (MemoryType, EFI_SIZE_TO_PAGES (AllocationSize));\r | |
478 | }\r | |
479 | \r | |
480 | /**\r | |
481 | Allocates a buffer of type EfiBootServicesData.\r | |
482 | \r | |
483 | Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r | |
484 | pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r | |
485 | returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
486 | \r | |
487 | @param AllocationSize The number of bytes to allocate.\r | |
488 | \r | |
489 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
490 | \r | |
491 | **/\r | |
492 | VOID *\r | |
493 | EFIAPI\r | |
494 | AllocatePool (\r | |
495 | IN UINTN AllocationSize\r | |
496 | )\r | |
497 | {\r | |
498 | EFI_STATUS Status;\r | |
499 | VOID *Buffer;\r | |
500 | \r | |
501 | Status = PeiServicesAllocatePool (AllocationSize, &Buffer);\r | |
502 | if (EFI_ERROR (Status)) {\r | |
503 | Buffer = NULL;\r | |
504 | }\r | |
505 | return Buffer;\r | |
506 | }\r | |
507 | \r | |
508 | /**\r | |
509 | Allocates a buffer of type EfiRuntimeServicesData.\r | |
510 | \r | |
511 | Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData and returns\r | |
512 | a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r | |
513 | returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
514 | \r | |
515 | @param AllocationSize The number of bytes to allocate.\r | |
516 | \r | |
517 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
518 | \r | |
519 | **/\r | |
520 | VOID *\r | |
521 | EFIAPI\r | |
522 | AllocateRuntimePool (\r | |
523 | IN UINTN AllocationSize\r | |
524 | )\r | |
525 | {\r | |
526 | return InternalAllocatePool (EfiRuntimeServicesData, AllocationSize);\r | |
527 | }\r | |
528 | \r | |
529 | /**\r | |
530 | Allocates a buffer of type EfiReservedMemoryType.\r | |
531 | \r | |
532 | Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType and returns\r | |
533 | a pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r | |
534 | returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
535 | \r | |
536 | @param AllocationSize The number of bytes to allocate.\r | |
537 | \r | |
538 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
539 | \r | |
540 | **/\r | |
541 | VOID *\r | |
542 | EFIAPI\r | |
543 | AllocateReservedPool (\r | |
544 | IN UINTN AllocationSize\r | |
545 | )\r | |
546 | {\r | |
547 | return InternalAllocatePool (EfiReservedMemoryType, AllocationSize);\r | |
548 | }\r | |
549 | \r | |
550 | /**\r | |
551 | Allocates and zeros a buffer of a certain pool type.\r | |
552 | \r | |
553 | Allocates the number bytes specified by AllocationSize of a certain pool type, clears the buffer\r | |
554 | with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a valid\r | |
555 | buffer of 0 size is returned. If there is not enough memory remaining to satisfy the request,\r | |
556 | then NULL is returned.\r | |
557 | \r | |
558 | @param PoolType The type of memory to allocate.\r | |
559 | @param AllocationSize The number of bytes to allocate and zero.\r | |
560 | \r | |
561 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
562 | \r | |
563 | **/\r | |
564 | VOID *\r | |
565 | InternalAllocateZeroPool (\r | |
566 | IN EFI_MEMORY_TYPE PoolType, \r | |
567 | IN UINTN AllocationSize\r | |
568 | ) \r | |
569 | {\r | |
570 | VOID *Memory;\r | |
571 | \r | |
572 | Memory = InternalAllocatePool (PoolType, AllocationSize);\r | |
573 | if (Memory != NULL) {\r | |
574 | Memory = ZeroMem (Memory, AllocationSize);\r | |
575 | }\r | |
576 | return Memory;\r | |
577 | }\r | |
578 | \r | |
579 | /**\r | |
580 | Allocates and zeros a buffer of type EfiBootServicesData.\r | |
581 | \r | |
582 | Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the\r | |
583 | buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r | |
584 | valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r | |
585 | request, then NULL is returned.\r | |
586 | \r | |
587 | @param AllocationSize The number of bytes to allocate and zero.\r | |
588 | \r | |
589 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
590 | \r | |
591 | **/\r | |
592 | VOID *\r | |
593 | EFIAPI\r | |
594 | AllocateZeroPool (\r | |
595 | IN UINTN AllocationSize\r | |
596 | )\r | |
597 | {\r | |
598 | VOID *Memory;\r | |
599 | \r | |
600 | Memory = AllocatePool (AllocationSize);\r | |
601 | if (Memory != NULL) {\r | |
602 | Memory = ZeroMem (Memory, AllocationSize);\r | |
603 | }\r | |
604 | return Memory;\r | |
605 | }\r | |
606 | \r | |
607 | /**\r | |
608 | Allocates and zeros a buffer of type EfiRuntimeServicesData.\r | |
609 | \r | |
610 | Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, clears the\r | |
611 | buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r | |
612 | valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r | |
613 | request, then NULL is returned.\r | |
614 | \r | |
615 | @param AllocationSize The number of bytes to allocate and zero.\r | |
616 | \r | |
617 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
618 | \r | |
619 | **/\r | |
620 | VOID *\r | |
621 | EFIAPI\r | |
622 | AllocateRuntimeZeroPool (\r | |
623 | IN UINTN AllocationSize\r | |
624 | )\r | |
625 | {\r | |
626 | return InternalAllocateZeroPool (EfiRuntimeServicesData, AllocationSize);\r | |
627 | }\r | |
628 | \r | |
629 | /**\r | |
630 | Allocates and zeros a buffer of type EfiReservedMemoryType.\r | |
631 | \r | |
632 | Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, clears the\r | |
633 | buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a\r | |
634 | valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the\r | |
635 | request, then NULL is returned.\r | |
636 | \r | |
637 | @param AllocationSize The number of bytes to allocate and zero.\r | |
638 | \r | |
639 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
640 | \r | |
641 | **/\r | |
642 | VOID *\r | |
643 | EFIAPI\r | |
644 | AllocateReservedZeroPool (\r | |
645 | IN UINTN AllocationSize\r | |
646 | )\r | |
647 | {\r | |
648 | return InternalAllocateZeroPool (EfiReservedMemoryType, AllocationSize);\r | |
649 | }\r | |
650 | \r | |
651 | /**\r | |
652 | Copies a buffer to an allocated buffer of a certain pool type.\r | |
653 | \r | |
654 | Allocates the number bytes specified by AllocationSize of a certain pool type, copies\r | |
655 | AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r | |
656 | allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r | |
657 | is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
658 | If Buffer is NULL, then ASSERT().\r | |
659 | If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r | |
660 | \r | |
661 | @param PoolType The type of pool to allocate.\r | |
662 | @param AllocationSize The number of bytes to allocate and zero.\r | |
663 | @param Buffer The buffer to copy to the allocated buffer.\r | |
664 | \r | |
665 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
666 | \r | |
667 | **/\r | |
668 | VOID *\r | |
669 | InternalAllocateCopyPool (\r | |
670 | IN EFI_MEMORY_TYPE PoolType, \r | |
671 | IN UINTN AllocationSize,\r | |
672 | IN CONST VOID *Buffer\r | |
673 | ) \r | |
674 | {\r | |
675 | VOID *Memory;\r | |
676 | \r | |
677 | ASSERT (Buffer != NULL);\r | |
678 | ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r | |
679 | \r | |
680 | Memory = InternalAllocatePool (PoolType, AllocationSize);\r | |
681 | if (Memory != NULL) {\r | |
682 | Memory = CopyMem (Memory, Buffer, AllocationSize);\r | |
683 | }\r | |
684 | return Memory;\r | |
685 | } \r | |
686 | \r | |
687 | /**\r | |
688 | Copies a buffer to an allocated buffer of type EfiBootServicesData.\r | |
689 | \r | |
690 | Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, copies\r | |
691 | AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r | |
692 | allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r | |
693 | is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
694 | \r | |
695 | If Buffer is NULL, then ASSERT().\r | |
696 | If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r | |
697 | \r | |
698 | @param AllocationSize The number of bytes to allocate and zero.\r | |
699 | @param Buffer The buffer to copy to the allocated buffer.\r | |
700 | \r | |
701 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
702 | \r | |
703 | **/\r | |
704 | VOID *\r | |
705 | EFIAPI\r | |
706 | AllocateCopyPool (\r | |
707 | IN UINTN AllocationSize,\r | |
708 | IN CONST VOID *Buffer\r | |
709 | )\r | |
710 | {\r | |
711 | VOID *Memory;\r | |
712 | \r | |
713 | ASSERT (Buffer != NULL);\r | |
714 | ASSERT (AllocationSize <= (MAX_ADDRESS - (UINTN) Buffer + 1));\r | |
715 | \r | |
716 | Memory = AllocatePool (AllocationSize);\r | |
717 | if (Memory != NULL) {\r | |
718 | Memory = CopyMem (Memory, Buffer, AllocationSize);\r | |
719 | }\r | |
720 | return Memory;\r | |
721 | }\r | |
722 | \r | |
723 | /**\r | |
724 | Copies a buffer to an allocated buffer of type EfiRuntimeServicesData.\r | |
725 | \r | |
726 | Allocates the number bytes specified by AllocationSize of type EfiRuntimeServicesData, copies\r | |
727 | AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r | |
728 | allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r | |
729 | is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
730 | \r | |
731 | If Buffer is NULL, then ASSERT().\r | |
732 | If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r | |
733 | \r | |
734 | @param AllocationSize The number of bytes to allocate and zero.\r | |
735 | @param Buffer The buffer to copy to the allocated buffer.\r | |
736 | \r | |
737 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
738 | \r | |
739 | **/\r | |
740 | VOID *\r | |
741 | EFIAPI\r | |
742 | AllocateRuntimeCopyPool (\r | |
743 | IN UINTN AllocationSize,\r | |
744 | IN CONST VOID *Buffer\r | |
745 | )\r | |
746 | {\r | |
747 | return InternalAllocateCopyPool (EfiRuntimeServicesData, AllocationSize, Buffer);\r | |
748 | }\r | |
749 | \r | |
750 | /**\r | |
751 | Copies a buffer to an allocated buffer of type EfiReservedMemoryType.\r | |
752 | \r | |
753 | Allocates the number bytes specified by AllocationSize of type EfiReservedMemoryType, copies\r | |
754 | AllocationSize bytes from Buffer to the newly allocated buffer, and returns a pointer to the\r | |
755 | allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there\r | |
756 | is not enough memory remaining to satisfy the request, then NULL is returned.\r | |
757 | \r | |
758 | If Buffer is NULL, then ASSERT().\r | |
759 | If AllocationSize is greater than (MAX_ADDRESS - Buffer + 1), then ASSERT(). \r | |
760 | \r | |
761 | @param AllocationSize The number of bytes to allocate and zero.\r | |
762 | @param Buffer The buffer to copy to the allocated buffer.\r | |
763 | \r | |
764 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
765 | \r | |
766 | **/\r | |
767 | VOID *\r | |
768 | EFIAPI\r | |
769 | AllocateReservedCopyPool (\r | |
770 | IN UINTN AllocationSize,\r | |
771 | IN CONST VOID *Buffer\r | |
772 | )\r | |
773 | {\r | |
774 | return InternalAllocateCopyPool (EfiReservedMemoryType, AllocationSize, Buffer);\r | |
775 | }\r | |
776 | \r | |
777 | /**\r | |
778 | Reallocates a buffer of a specified memory type.\r | |
779 | \r | |
780 | Allocates and zeros the number bytes specified by NewSize from memory of the type\r | |
781 | specified by PoolType. If OldBuffer is not NULL, then the smaller of OldSize and \r | |
782 | NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r | |
783 | OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r | |
784 | If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r | |
785 | enough memory remaining to satisfy the request, then NULL is returned.\r | |
786 | \r | |
787 | If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r | |
788 | is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r | |
789 | \r | |
790 | @param PoolType The type of pool to allocate.\r | |
791 | @param OldSize The size, in bytes, of OldBuffer.\r | |
792 | @param NewSize The size, in bytes, of the buffer to reallocate.\r | |
793 | @param OldBuffer The buffer to copy to the allocated buffer. This is an \r | |
794 | optional parameter that may be NULL.\r | |
795 | \r | |
796 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
797 | \r | |
798 | **/\r | |
799 | VOID *\r | |
800 | InternalReallocatePool (\r | |
801 | IN EFI_MEMORY_TYPE PoolType, \r | |
802 | IN UINTN OldSize,\r | |
803 | IN UINTN NewSize,\r | |
804 | IN VOID *OldBuffer OPTIONAL\r | |
805 | )\r | |
806 | {\r | |
807 | VOID *NewBuffer;\r | |
808 | \r | |
809 | NewBuffer = InternalAllocateZeroPool (PoolType, NewSize);\r | |
810 | if (NewBuffer != NULL && OldBuffer != NULL) {\r | |
811 | CopyMem (NewBuffer, OldBuffer, MIN (OldSize, NewSize));\r | |
812 | FreePool (OldBuffer);\r | |
813 | }\r | |
814 | return NewBuffer;\r | |
815 | }\r | |
816 | \r | |
817 | /**\r | |
818 | Reallocates a buffer of type EfiBootServicesData.\r | |
819 | \r | |
820 | Allocates and zeros the number bytes specified by NewSize from memory of type\r | |
821 | EfiBootServicesData. If OldBuffer is not NULL, then the smaller of OldSize and \r | |
822 | NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r | |
823 | OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r | |
824 | If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r | |
825 | enough memory remaining to satisfy the request, then NULL is returned.\r | |
826 | \r | |
827 | If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r | |
828 | is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r | |
829 | \r | |
830 | @param OldSize The size, in bytes, of OldBuffer.\r | |
831 | @param NewSize The size, in bytes, of the buffer to reallocate.\r | |
832 | @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r | |
833 | parameter that may be NULL.\r | |
834 | \r | |
835 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
836 | \r | |
837 | **/\r | |
838 | VOID *\r | |
839 | EFIAPI\r | |
840 | ReallocatePool (\r | |
841 | IN UINTN OldSize,\r | |
842 | IN UINTN NewSize,\r | |
843 | IN VOID *OldBuffer OPTIONAL\r | |
844 | )\r | |
845 | {\r | |
846 | return InternalReallocatePool (EfiBootServicesData, OldSize, NewSize, OldBuffer);\r | |
847 | }\r | |
848 | \r | |
849 | /**\r | |
850 | Reallocates a buffer of type EfiRuntimeServicesData.\r | |
851 | \r | |
852 | Allocates and zeros the number bytes specified by NewSize from memory of type\r | |
853 | EfiRuntimeServicesData. If OldBuffer is not NULL, then the smaller of OldSize and \r | |
854 | NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r | |
855 | OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r | |
856 | If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r | |
857 | enough memory remaining to satisfy the request, then NULL is returned.\r | |
858 | \r | |
859 | If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r | |
860 | is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r | |
861 | \r | |
862 | @param OldSize The size, in bytes, of OldBuffer.\r | |
863 | @param NewSize The size, in bytes, of the buffer to reallocate.\r | |
864 | @param OldBuffer The buffer to copy to the allocated buffer. This is an optional \r | |
865 | parameter that may be NULL.\r | |
866 | \r | |
867 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
868 | \r | |
869 | **/\r | |
870 | VOID *\r | |
871 | EFIAPI\r | |
872 | ReallocateRuntimePool (\r | |
873 | IN UINTN OldSize,\r | |
874 | IN UINTN NewSize,\r | |
875 | IN VOID *OldBuffer OPTIONAL\r | |
876 | )\r | |
877 | {\r | |
878 | return InternalReallocatePool (EfiRuntimeServicesData, OldSize, NewSize, OldBuffer);\r | |
879 | }\r | |
880 | \r | |
881 | /**\r | |
882 | Reallocates a buffer of type EfiReservedMemoryType.\r | |
883 | \r | |
884 | Allocates and zeros the number bytes specified by NewSize from memory of type\r | |
885 | EfiReservedMemoryType. If OldBuffer is not NULL, then the smaller of OldSize and \r | |
886 | NewSize bytes are copied from OldBuffer to the newly allocated buffer, and \r | |
887 | OldBuffer is freed. A pointer to the newly allocated buffer is returned. \r | |
888 | If NewSize is 0, then a valid buffer of 0 size is returned. If there is not \r | |
889 | enough memory remaining to satisfy the request, then NULL is returned.\r | |
890 | \r | |
891 | If the allocation of the new buffer is successful and the smaller of NewSize and OldSize\r | |
892 | is greater than (MAX_ADDRESS - OldBuffer + 1), then ASSERT().\r | |
893 | \r | |
894 | @param OldSize The size, in bytes, of OldBuffer.\r | |
895 | @param NewSize The size, in bytes, of the buffer to reallocate.\r | |
896 | @param OldBuffer The buffer to copy to the allocated buffer. This is an \r | |
897 | optional parameter that may be NULL.\r | |
898 | \r | |
899 | @return A pointer to the allocated buffer or NULL if allocation fails.\r | |
900 | \r | |
901 | **/\r | |
902 | VOID *\r | |
903 | EFIAPI\r | |
904 | ReallocateReservedPool (\r | |
905 | IN UINTN OldSize,\r | |
906 | IN UINTN NewSize,\r | |
907 | IN VOID *OldBuffer OPTIONAL\r | |
908 | )\r | |
909 | {\r | |
910 | return InternalReallocatePool (EfiReservedMemoryType, OldSize, NewSize, OldBuffer);\r | |
911 | }\r | |
912 | \r | |
913 | /**\r | |
914 | Frees a buffer that was previously allocated with one of the pool allocation functions in the\r | |
915 | Memory Allocation Library.\r | |
916 | \r | |
917 | Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r | |
918 | pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r | |
919 | resources, then this function will perform no actions.\r | |
920 | \r | |
921 | If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r | |
922 | then ASSERT().\r | |
923 | \r | |
924 | @param Buffer The pointer to the buffer to free.\r | |
925 | \r | |
926 | **/\r | |
927 | VOID\r | |
928 | EFIAPI\r | |
929 | FreePool (\r | |
930 | IN VOID *Buffer\r | |
931 | )\r | |
932 | {\r | |
933 | //\r | |
934 | // PEI phase does not support to free pool, so leave it as NOP.\r | |
935 | //\r | |
936 | }\r | |
937 | \r | |
938 | \r |