]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
BaseTools/Capsule: Do not support -o with --dump-info
[mirror_edk2.git] / EmbeddedPkg / Library / PrePiMemoryAllocationLib / MemoryAllocationLib.c
CommitLineData
a6caee65 1/** @file\r
2 Implementation of the 6 PEI Ffs (FV) APIs in library form.\r
3\r
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
3402aac7 5\r
a6caee65 6 This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10\r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <PiPei.h>\r
17\r
18#include <Library/BaseLib.h>\r
19#include <Library/PrePiLib.h>\r
20#include <Library/DebugLib.h>\r
21\r
22\r
23\r
24/**\r
25 Allocates one or more 4KB pages of type EfiBootServicesData.\r
26\r
27 Allocates the number of 4KB pages of MemoryType and returns a pointer to the\r
28 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL\r
29 is returned. If there is not enough memory remaining to satisfy the request, then NULL is\r
30 returned.\r
31\r
32 @param Pages The number of 4 KB pages to allocate.\r
33\r
34 @return A pointer to the allocated buffer or NULL if allocation fails.\r
35\r
36**/\r
37VOID *\r
38EFIAPI\r
39AllocatePages (\r
40 IN UINTN Pages\r
41 )\r
42{\r
43 EFI_PEI_HOB_POINTERS Hob;\r
44 EFI_PHYSICAL_ADDRESS Offset;\r
45\r
46 Hob.Raw = GetHobList ();\r
47\r
48 // Check to see if on 4k boundary\r
49 Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;\r
50 if (Offset != 0) {\r
51 // If not aligned, make the allocation aligned.\r
52 Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;\r
53 }\r
54\r
55 //\r
56 // Verify that there is sufficient memory to satisfy the allocation\r
57 //\r
58 if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) {\r
59 return 0;\r
60 } else {\r
61 //\r
62 // Update the PHIT to reflect the memory usage\r
63 //\r
64 Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;\r
65\r
66 // This routine used to create a memory allocation HOB a la PEI, but that's not\r
67 // necessary for us.\r
68\r
69 //\r
70 // Create a memory allocation HOB.\r
71 //\r
72 BuildMemoryAllocationHob (\r
73 Hob.HandoffInformationTable->EfiFreeMemoryTop,\r
74 Pages * EFI_PAGE_SIZE,\r
75 EfiBootServicesData\r
76 );\r
77 return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;\r
78 }\r
79}\r
80\r
81\r
82/**\r
83 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.\r
84\r
85 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an\r
86 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is\r
87 returned. If there is not enough memory at the specified alignment remaining to satisfy the\r
88 request, then NULL is returned.\r
89 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
90\r
91 @param Pages The number of 4 KB pages to allocate.\r
92 @param Alignment The requested alignment of the allocation. Must be a power of two.\r
93 If Alignment is zero, then byte alignment is used.\r
94\r
95 @return A pointer to the allocated buffer or NULL if allocation fails.\r
96\r
97**/\r
98VOID *\r
99EFIAPI\r
100AllocateAlignedPages (\r
101 IN UINTN Pages,\r
102 IN UINTN Alignment\r
103 )\r
104{\r
105 VOID *Memory;\r
106 UINTN AlignmentMask;\r
107\r
108 //\r
109 // Alignment must be a power of two or zero.\r
110 //\r
111 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
112\r
113 if (Pages == 0) {\r
114 return NULL;\r
115 }\r
116 //\r
117 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r
118 //\r
119 ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));\r
120 //\r
121 // We would rather waste some memory to save PEI code size.\r
122 //\r
123 Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));\r
124 if (Alignment == 0) {\r
125 AlignmentMask = Alignment;\r
126 } else {\r
127 AlignmentMask = Alignment - 1;\r
128 }\r
129 return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask);\r
130}\r
131\r
132\r
0b342ffb
OM
133/**\r
134 Frees one or more 4KB pages that were previously allocated with one of the page allocation\r
135 functions in the Memory Allocation Library.\r
136\r
137 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer\r
138 must have been allocated on a previous call to the page allocation services of the Memory\r
139 Allocation Library. If it is not possible to free allocated pages, then this function will\r
140 perform no actions.\r
141\r
142 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,\r
143 then ASSERT().\r
144 If Pages is zero, then ASSERT().\r
a6caee65 145\r
0b342ffb
OM
146 @param Buffer Pointer to the buffer of pages to free.\r
147 @param Pages The number of 4 KB pages to free.\r
148\r
149**/\r
150VOID\r
151EFIAPI\r
152FreePages (\r
153 IN VOID *Buffer,\r
154 IN UINTN Pages\r
155 )\r
156{\r
157 // For now, we do not support the ability to free pages in the PrePei Memory Allocator.\r
158 // The allocated memory is lost.\r
159}\r
a6caee65 160\r
161/**\r
162 Allocates a buffer of type EfiBootServicesData.\r
163\r
164 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a\r
165 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is\r
166 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.\r
167\r
168 @param AllocationSize The number of bytes to allocate.\r
169\r
170 @return A pointer to the allocated buffer or NULL if allocation fails.\r
171\r
172**/\r
173VOID *\r
174EFIAPI\r
175AllocatePool (\r
176 IN UINTN AllocationSize\r
177 )\r
178{\r
179 EFI_HOB_MEMORY_POOL *Hob;\r
180\r
181 Hob = GetHobList ();\r
182\r
3402aac7 183\r
a6caee65 184 //\r
185 // Verify that there is sufficient memory to satisfy the allocation\r
186 //\r
187 if (AllocationSize > 0x10000) {\r
188 // Please call AllcoatePages for big allocations\r
189 return 0;\r
190 } else {\r
191\r
192 Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (EFI_HOB_TYPE_MEMORY_POOL, (UINT16)(sizeof (EFI_HOB_TYPE_MEMORY_POOL) + AllocationSize));\r
193 return (VOID *)(Hob + 1);\r
194 }\r
195}\r
196\r
197/**\r
198 Frees a buffer that was previously allocated with one of the pool allocation functions in the\r
199 Memory Allocation Library.\r
200\r
201 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the\r
202 pool allocation services of the Memory Allocation Library. If it is not possible to free pool\r
203 resources, then this function will perform no actions.\r
204\r
205 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,\r
206 then ASSERT().\r
207\r
208 @param Buffer Pointer to the buffer to free.\r
209\r
210**/\r
211VOID\r
212EFIAPI\r
213FreePool (\r
214 IN VOID *Buffer\r
215 )\r
216{\r
217 // Not implemented yet\r
218}\r