2 Implementation of the 6 PEI Ffs (FV) APIs in library form.
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Library/BaseLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/PrePiLib.h>
15 #include <Library/DebugLib.h>
20 InternalAllocatePages (
22 IN EFI_MEMORY_TYPE MemoryType
25 EFI_PEI_HOB_POINTERS Hob
;
26 EFI_PHYSICAL_ADDRESS NewTop
;
28 Hob
.Raw
= GetHobList ();
30 NewTop
= Hob
.HandoffInformationTable
->EfiFreeMemoryTop
& ~(EFI_PHYSICAL_ADDRESS
)EFI_PAGE_MASK
;
31 NewTop
-= Pages
* EFI_PAGE_SIZE
;
34 // Verify that there is sufficient memory to satisfy the allocation
36 if (NewTop
< (Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
+ sizeof (EFI_HOB_MEMORY_ALLOCATION
))) {
41 // Update the PHIT to reflect the memory usage
43 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
= NewTop
;
46 // Create a memory allocation HOB.
48 BuildMemoryAllocationHob (
49 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
,
50 Pages
* EFI_PAGE_SIZE
,
54 return (VOID
*)(UINTN
)Hob
.HandoffInformationTable
->EfiFreeMemoryTop
;
58 Allocates one or more 4KB pages of type EfiBootServicesData.
60 Allocates the number of 4KB pages of MemoryType and returns a pointer to the
61 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
62 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
65 @param Pages The number of 4 KB pages to allocate.
67 @return A pointer to the allocated buffer or NULL if allocation fails.
76 return InternalAllocatePages (Pages
, EfiBootServicesData
);
80 Allocates one or more 4KB pages of type EfiRuntimeServicesData.
82 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
83 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
84 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
87 @param Pages The number of 4 KB pages to allocate.
89 @return A pointer to the allocated buffer or NULL if allocation fails.
94 AllocateRuntimePages (
98 return InternalAllocatePages (Pages
, EfiRuntimeServicesData
);
102 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
104 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
105 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
106 returned. If there is not enough memory at the specified alignment remaining to satisfy the
107 request, then NULL is returned.
108 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
110 @param Pages The number of 4 KB pages to allocate.
111 @param Alignment The requested alignment of the allocation. Must be a power of two.
112 If Alignment is zero, then byte alignment is used.
114 @return A pointer to the allocated buffer or NULL if allocation fails.
119 AllocateAlignedPages (
128 // Alignment must be a power of two or zero.
130 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
137 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
139 ASSERT (Pages
<= (MAX_ADDRESS
- EFI_SIZE_TO_PAGES (Alignment
)));
141 // We would rather waste some memory to save PEI code size.
143 Memory
= (VOID
*)(UINTN
)AllocatePages (Pages
+ EFI_SIZE_TO_PAGES (Alignment
));
144 if (Alignment
== 0) {
145 AlignmentMask
= Alignment
;
147 AlignmentMask
= Alignment
- 1;
150 return (VOID
*)(UINTN
)(((UINTN
)Memory
+ AlignmentMask
) & ~AlignmentMask
);
154 Frees one or more 4KB pages that were previously allocated with one of the page allocation
155 functions in the Memory Allocation Library.
157 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
158 must have been allocated on a previous call to the page allocation services of the Memory
159 Allocation Library. If it is not possible to free allocated pages, then this function will
162 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
164 If Pages is zero, then ASSERT().
166 @param Buffer Pointer to the buffer of pages to free.
167 @param Pages The number of 4 KB pages to free.
177 // For now, we do not support the ability to free pages in the PrePei Memory Allocator.
178 // The allocated memory is lost.
182 Allocates a buffer of type EfiBootServicesData.
184 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
185 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
186 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
188 @param AllocationSize The number of bytes to allocate.
190 @return A pointer to the allocated buffer or NULL if allocation fails.
196 IN UINTN AllocationSize
199 EFI_HOB_MEMORY_POOL
*Hob
;
204 // Verify that there is sufficient memory to satisfy the allocation
206 if (AllocationSize
> 0x10000) {
207 // Please call AllocatePages for big allocations
210 Hob
= (EFI_HOB_MEMORY_POOL
*)CreateHob (
211 EFI_HOB_TYPE_MEMORY_POOL
,
212 (UINT16
)(sizeof (EFI_HOB_MEMORY_POOL
) +
215 return (VOID
*)(Hob
+ 1);
220 Allocates and zeros a buffer of type EfiBootServicesData.
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.
227 @param AllocationSize The number of bytes to allocate and zero.
229 @return A pointer to the allocated buffer or NULL if allocation fails.
235 IN UINTN AllocationSize
240 Buffer
= AllocatePool (AllocationSize
);
241 if (Buffer
== NULL
) {
245 ZeroMem (Buffer
, AllocationSize
);
251 Frees a buffer that was previously allocated with one of the pool allocation functions in the
252 Memory Allocation Library.
254 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
255 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
256 resources, then this function will perform no actions.
258 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
261 @param Buffer Pointer to the buffer to free.
270 // Not implemented yet