]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
ArmPlatformPkg: Remove PcdStandalone from Sec module and Introduce ArmPlatformSecExtr...
[mirror_edk2.git] / EmbeddedPkg / Library / PrePiMemoryAllocationLib / MemoryAllocationLib.c
1 /** @file
2 Implementation of the 6 PEI Ffs (FV) APIs in library form.
3
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5
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
10
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.
13
14 **/
15
16 #include <PiPei.h>
17
18 #include <Library/BaseLib.h>
19 #include <Library/PrePiLib.h>
20 #include <Library/DebugLib.h>
21
22
23
24 /**
25 Allocates one or more 4KB pages of type EfiBootServicesData.
26
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
30 returned.
31
32 @param Pages The number of 4 KB pages to allocate.
33
34 @return A pointer to the allocated buffer or NULL if allocation fails.
35
36 **/
37 VOID *
38 EFIAPI
39 AllocatePages (
40 IN UINTN Pages
41 )
42 {
43 EFI_PEI_HOB_POINTERS Hob;
44 EFI_PHYSICAL_ADDRESS Offset;
45
46 Hob.Raw = GetHobList ();
47
48 // Check to see if on 4k boundary
49 Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;
50 if (Offset != 0) {
51 // If not aligned, make the allocation aligned.
52 Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;
53 }
54
55 //
56 // Verify that there is sufficient memory to satisfy the allocation
57 //
58 if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) {
59 return 0;
60 } else {
61 //
62 // Update the PHIT to reflect the memory usage
63 //
64 Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
65
66 // This routine used to create a memory allocation HOB a la PEI, but that's not
67 // necessary for us.
68
69 //
70 // Create a memory allocation HOB.
71 //
72 BuildMemoryAllocationHob (
73 Hob.HandoffInformationTable->EfiFreeMemoryTop,
74 Pages * EFI_PAGE_SIZE,
75 EfiBootServicesData
76 );
77 return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
78 }
79 }
80
81
82 /**
83 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
84
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().
90
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.
94
95 @return A pointer to the allocated buffer or NULL if allocation fails.
96
97 **/
98 VOID *
99 EFIAPI
100 AllocateAlignedPages (
101 IN UINTN Pages,
102 IN UINTN Alignment
103 )
104 {
105 VOID *Memory;
106 UINTN AlignmentMask;
107
108 //
109 // Alignment must be a power of two or zero.
110 //
111 ASSERT ((Alignment & (Alignment - 1)) == 0);
112
113 if (Pages == 0) {
114 return NULL;
115 }
116 //
117 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
118 //
119 ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));
120 //
121 // We would rather waste some memory to save PEI code size.
122 //
123 Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));
124 if (Alignment == 0) {
125 AlignmentMask = Alignment;
126 } else {
127 AlignmentMask = Alignment - 1;
128 }
129 return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask);
130 }
131
132
133
134
135 /**
136 Allocates a buffer of type EfiBootServicesData.
137
138 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
139 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
140 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
141
142 @param AllocationSize The number of bytes to allocate.
143
144 @return A pointer to the allocated buffer or NULL if allocation fails.
145
146 **/
147 VOID *
148 EFIAPI
149 AllocatePool (
150 IN UINTN AllocationSize
151 )
152 {
153 EFI_HOB_MEMORY_POOL *Hob;
154
155 Hob = GetHobList ();
156
157
158 //
159 // Verify that there is sufficient memory to satisfy the allocation
160 //
161 if (AllocationSize > 0x10000) {
162 // Please call AllcoatePages for big allocations
163 return 0;
164 } else {
165
166 Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (EFI_HOB_TYPE_MEMORY_POOL, (UINT16)(sizeof (EFI_HOB_TYPE_MEMORY_POOL) + AllocationSize));
167 return (VOID *)(Hob + 1);
168 }
169 }
170
171 /**
172 Frees a buffer that was previously allocated with one of the pool allocation functions in the
173 Memory Allocation Library.
174
175 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
176 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
177 resources, then this function will perform no actions.
178
179 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
180 then ASSERT().
181
182 @param Buffer Pointer to the buffer to free.
183
184 **/
185 VOID
186 EFIAPI
187 FreePool (
188 IN VOID *Buffer
189 )
190 {
191 // Not implemented yet
192 }