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 Allocates one or more 4KB pages of type EfiBootServicesData.
22 Allocates the number of 4KB pages of MemoryType and returns a pointer to the
23 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
24 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
27 @param Pages The number of 4 KB pages to allocate.
29 @return A pointer to the allocated buffer or NULL if allocation fails.
38 EFI_PEI_HOB_POINTERS Hob
;
39 EFI_PHYSICAL_ADDRESS Offset
;
41 Hob
.Raw
= GetHobList ();
43 // Check to see if on 4k boundary
44 Offset
= Hob
.HandoffInformationTable
->EfiFreeMemoryTop
& 0xFFF;
46 // If not aligned, make the allocation aligned.
47 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
-= Offset
;
51 // Verify that there is sufficient memory to satisfy the allocation
53 if (Hob
.HandoffInformationTable
->EfiFreeMemoryTop
- ((Pages
* EFI_PAGE_SIZE
) + sizeof (EFI_HOB_MEMORY_ALLOCATION
)) < Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
) {
57 // Update the PHIT to reflect the memory usage
59 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
-= Pages
* EFI_PAGE_SIZE
;
61 // This routine used to create a memory allocation HOB a la PEI, but that's not
65 // Create a memory allocation HOB.
67 BuildMemoryAllocationHob (
68 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
,
69 Pages
* EFI_PAGE_SIZE
,
72 return (VOID
*)(UINTN
)Hob
.HandoffInformationTable
->EfiFreeMemoryTop
;
78 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
80 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
81 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
82 returned. If there is not enough memory at the specified alignment remaining to satisfy the
83 request, then NULL is returned.
84 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
86 @param Pages The number of 4 KB pages to allocate.
87 @param Alignment The requested alignment of the allocation. Must be a power of two.
88 If Alignment is zero, then byte alignment is used.
90 @return A pointer to the allocated buffer or NULL if allocation fails.
95 AllocateAlignedPages (
104 // Alignment must be a power of two or zero.
106 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
112 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
114 ASSERT (Pages
<= (MAX_ADDRESS
- EFI_SIZE_TO_PAGES (Alignment
)));
116 // We would rather waste some memory to save PEI code size.
118 Memory
= (VOID
*)(UINTN
)AllocatePages (Pages
+ EFI_SIZE_TO_PAGES (Alignment
));
119 if (Alignment
== 0) {
120 AlignmentMask
= Alignment
;
122 AlignmentMask
= Alignment
- 1;
124 return (VOID
*) (UINTN
) (((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
);
129 Frees one or more 4KB pages that were previously allocated with one of the page allocation
130 functions in the Memory Allocation Library.
132 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
133 must have been allocated on a previous call to the page allocation services of the Memory
134 Allocation Library. If it is not possible to free allocated pages, then this function will
137 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
139 If Pages is zero, then ASSERT().
141 @param Buffer Pointer to the buffer of pages to free.
142 @param Pages The number of 4 KB pages to free.
152 // For now, we do not support the ability to free pages in the PrePei Memory Allocator.
153 // The allocated memory is lost.
157 Allocates a buffer of type EfiBootServicesData.
159 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
160 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
161 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
163 @param AllocationSize The number of bytes to allocate.
165 @return A pointer to the allocated buffer or NULL if allocation fails.
171 IN UINTN AllocationSize
174 EFI_HOB_MEMORY_POOL
*Hob
;
180 // Verify that there is sufficient memory to satisfy the allocation
182 if (AllocationSize
> 0x10000) {
183 // Please call AllcoatePages for big allocations
187 Hob
= (EFI_HOB_MEMORY_POOL
*)CreateHob (EFI_HOB_TYPE_MEMORY_POOL
, (UINT16
)(sizeof (EFI_HOB_TYPE_MEMORY_POOL
) + AllocationSize
));
188 return (VOID
*)(Hob
+ 1);
193 Allocates and zeros a buffer of type EfiBootServicesData.
195 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
196 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
197 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
198 request, then NULL is returned.
200 @param AllocationSize The number of bytes to allocate and zero.
202 @return A pointer to the allocated buffer or NULL if allocation fails.
208 IN UINTN AllocationSize
213 Buffer
= AllocatePool (AllocationSize
);
214 if (Buffer
== NULL
) {
218 ZeroMem (Buffer
, AllocationSize
);
224 Frees a buffer that was previously allocated with one of the pool allocation functions in the
225 Memory Allocation Library.
227 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
228 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
229 resources, then this function will perform no actions.
231 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
234 @param Buffer Pointer to the buffer to free.
243 // Not implemented yet