2 Implementation of the 6 PEI Ffs (FV) APIs in library form.
4 Copyright (c) 2008-2009, Apple Inc. All rights reserved.
6 All rights reserved. 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.
21 Allocates one or more 4KB pages of type EfiBootServicesData.
23 Allocates the number of 4KB pages of MemoryType and returns a pointer to the
24 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
25 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
28 @param Pages The number of 4 KB pages to allocate.
30 @return A pointer to the allocated buffer or NULL if allocation fails.
39 EFI_PEI_HOB_POINTERS Hob
;
40 EFI_PHYSICAL_ADDRESS Offset
;
42 Hob
.Raw
= GetHobList ();
44 // Check to see if on 4k boundary
45 Offset
= Hob
.HandoffInformationTable
->EfiFreeMemoryTop
& 0xFFF;
47 // If not aligned, make the allocation aligned.
48 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
-= Offset
;
52 // Verify that there is sufficient memory to satisfy the allocation
54 if (Hob
.HandoffInformationTable
->EfiFreeMemoryTop
- ((Pages
* EFI_PAGE_SIZE
) + sizeof (EFI_HOB_MEMORY_ALLOCATION
)) < Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
) {
58 // Update the PHIT to reflect the memory usage
60 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
-= Pages
* EFI_PAGE_SIZE
;
62 // This routine used to create a memory allocation HOB a la PEI, but that's not
65 return (VOID
*)(UINTN
)Hob
.HandoffInformationTable
->EfiFreeMemoryTop
;
71 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
73 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
74 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
75 returned. If there is not enough memory at the specified alignment remaining to satisfy the
76 request, then NULL is returned.
77 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
79 @param Pages The number of 4 KB pages to allocate.
80 @param Alignment The requested alignment of the allocation. Must be a power of two.
81 If Alignment is zero, then byte alignment is used.
83 @return A pointer to the allocated buffer or NULL if allocation fails.
88 AllocateAlignedPages (
97 // Alignment must be a power of two or zero.
99 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
105 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
107 ASSERT (Pages
<= (MAX_ADDRESS
- EFI_SIZE_TO_PAGES (Alignment
)));
109 // We would rather waste some memory to save PEI code size.
111 Memory
= (VOID
*)(UINTN
)AllocatePages (Pages
+ EFI_SIZE_TO_PAGES (Alignment
));
112 if (Alignment
== 0) {
113 AlignmentMask
= Alignment
;
115 AlignmentMask
= Alignment
- 1;
117 return (VOID
*) (UINTN
) (((UINTN
) Memory
+ AlignmentMask
) & ~AlignmentMask
);
124 Allocates a buffer of type EfiBootServicesData.
126 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
127 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
128 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
130 @param AllocationSize The number of bytes to allocate.
132 @return A pointer to the allocated buffer or NULL if allocation fails.
138 IN UINTN AllocationSize
141 EFI_HOB_MEMORY_POOL
*Hob
;
147 // Verify that there is sufficient memory to satisfy the allocation
149 if (AllocationSize
> 0x10000) {
150 // Please call AllcoatePages for big allocations
154 Hob
= (EFI_HOB_MEMORY_POOL
*)CreateHob (EFI_HOB_TYPE_MEMORY_POOL
, (UINT16
)(sizeof (EFI_HOB_TYPE_MEMORY_POOL
) + AllocationSize
));
155 return (VOID
*)(Hob
+ 1);