2 Implementation of the 6 PEI Ffs (FV) APIs in library form.
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Library/BaseLib.h>
19 #include <Library/PrePiLib.h>
20 #include <Library/DebugLib.h>
25 Allocates one or more 4KB pages of type EfiBootServicesData.
27 Allocates the number of 4KB pages of MemoryType and returns a pointer to the
28 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
29 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
32 @param Pages The number of 4 KB pages to allocate.
34 @return A pointer to the allocated buffer or NULL if allocation fails.
43 EFI_PEI_HOB_POINTERS Hob
;
44 EFI_PHYSICAL_ADDRESS Offset
;
46 Hob
.Raw
= GetHobList ();
48 // Check to see if on 4k boundary
49 Offset
= Hob
.HandoffInformationTable
->EfiFreeMemoryTop
& 0xFFF;
51 // If not aligned, make the allocation aligned.
52 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
-= Offset
;
56 // Verify that there is sufficient memory to satisfy the allocation
58 if (Hob
.HandoffInformationTable
->EfiFreeMemoryTop
- ((Pages
* EFI_PAGE_SIZE
) + sizeof (EFI_HOB_MEMORY_ALLOCATION
)) < Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
) {
62 // Update the PHIT to reflect the memory usage
64 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
-= Pages
* EFI_PAGE_SIZE
;
66 // This routine used to create a memory allocation HOB a la PEI, but that's not
70 // Create a memory allocation HOB.
72 BuildMemoryAllocationHob (
73 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
,
74 Pages
* EFI_PAGE_SIZE
,
77 return (VOID
*)(UINTN
)Hob
.HandoffInformationTable
->EfiFreeMemoryTop
;
83 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
85 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
86 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
87 returned. If there is not enough memory at the specified alignment remaining to satisfy the
88 request, then NULL is returned.
89 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
91 @param Pages The number of 4 KB pages to allocate.
92 @param Alignment The requested alignment of the allocation. Must be a power of two.
93 If Alignment is zero, then byte alignment is used.
95 @return A pointer to the allocated buffer or NULL if allocation fails.
100 AllocateAlignedPages (
109 // Alignment must be a power of two or zero.
111 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
117 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
119 ASSERT (Pages
<= (MAX_ADDRESS
- EFI_SIZE_TO_PAGES (Alignment
)));
121 // We would rather waste some memory to save PEI code size.
123 Memory
= (VOID
*)(UINTN
)AllocatePages (Pages
+ EFI_SIZE_TO_PAGES (Alignment
));
124 if (Alignment
== 0) {
125 AlignmentMask
= Alignment
;
127 AlignmentMask
= Alignment
- 1;
129 return (VOID
*) (UINTN
) (((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
);
134 Frees one or more 4KB pages that were previously allocated with one of the page allocation
135 functions in the Memory Allocation Library.
137 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
138 must have been allocated on a previous call to the page allocation services of the Memory
139 Allocation Library. If it is not possible to free allocated pages, then this function will
142 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
144 If Pages is zero, then ASSERT().
146 @param Buffer Pointer to the buffer of pages to free.
147 @param Pages The number of 4 KB pages to free.
157 // For now, we do not support the ability to free pages in the PrePei Memory Allocator.
158 // The allocated memory is lost.
162 Allocates a buffer of type EfiBootServicesData.
164 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
165 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
166 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
168 @param AllocationSize The number of bytes to allocate.
170 @return A pointer to the allocated buffer or NULL if allocation fails.
176 IN UINTN AllocationSize
179 EFI_HOB_MEMORY_POOL
*Hob
;
185 // Verify that there is sufficient memory to satisfy the allocation
187 if (AllocationSize
> 0x10000) {
188 // Please call AllcoatePages for big allocations
192 Hob
= (EFI_HOB_MEMORY_POOL
*)CreateHob (EFI_HOB_TYPE_MEMORY_POOL
, (UINT16
)(sizeof (EFI_HOB_TYPE_MEMORY_POOL
) + AllocationSize
));
193 return (VOID
*)(Hob
+ 1);
198 Frees a buffer that was previously allocated with one of the pool allocation functions in the
199 Memory Allocation Library.
201 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
202 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
203 resources, then this function will perform no actions.
205 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
208 @param Buffer Pointer to the buffer to free.
217 // Not implemented yet