]> git.proxmox.com Git - mirror_edk2.git/blob - UefiPayloadPkg/UefiPayloadEntry/MemoryAllocation.c
DynamicTablesPkg: Add support to specify FADT minor revision
[mirror_edk2.git] / UefiPayloadPkg / UefiPayloadEntry / MemoryAllocation.c
1 /** @file
2
3
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5 Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include "UefiPayloadEntry.h"
12
13 /**
14 Allocates one or more pages of type EfiBootServicesData.
15
16 Allocates the number of pages of MemoryType and returns a pointer to the
17 allocated buffer. The buffer returned is aligned on a 4KB boundary.
18 If Pages is 0, then NULL is returned.
19 If there is not enough memory availble to satisfy the request, then NULL
20 is returned.
21
22 @param Pages The number of 4 KB pages to allocate.
23 @return A pointer to the allocated buffer or NULL if allocation fails.
24 **/
25 VOID *
26 EFIAPI
27 AllocatePages (
28 IN UINTN Pages
29 )
30 {
31 EFI_PEI_HOB_POINTERS Hob;
32 EFI_PHYSICAL_ADDRESS Offset;
33 EFI_HOB_HANDOFF_INFO_TABLE *HobTable;
34
35 Hob.Raw = GetHobList ();
36 HobTable = Hob.HandoffInformationTable;
37
38 if (Pages == 0) {
39 return NULL;
40 }
41
42 // Make sure allocation address is page alligned.
43 Offset = HobTable->EfiFreeMemoryTop & EFI_PAGE_MASK;
44 if (Offset != 0) {
45 HobTable->EfiFreeMemoryTop -= Offset;
46 }
47
48 //
49 // Check available memory for the allocation
50 //
51 if (HobTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < HobTable->EfiFreeMemoryBottom) {
52 return NULL;
53 }
54
55 HobTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
56 BuildMemoryAllocationHob (HobTable->EfiFreeMemoryTop, Pages * EFI_PAGE_SIZE, EfiBootServicesData);
57
58 return (VOID *)(UINTN)HobTable->EfiFreeMemoryTop;
59 }
60
61 /**
62 Frees one or more 4KB pages that were previously allocated with one of the page allocation
63 functions in the Memory Allocation Library.
64
65 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
66 must have been allocated on a previous call to the page allocation services of the Memory
67 Allocation Library. If it is not possible to free allocated pages, then this function will
68 perform no actions.
69
70 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
71 then ASSERT().
72 If Pages is zero, then ASSERT().
73
74 @param Buffer Pointer to the buffer of pages to free.
75 @param Pages The number of 4 KB pages to free.
76
77 **/
78 VOID
79 EFIAPI
80 FreePages (
81 IN VOID *Buffer,
82 IN UINTN Pages
83 )
84 {
85 }
86
87 /**
88 Allocates one or more pages of type EfiBootServicesData at a specified alignment.
89
90 Allocates the number of pages specified by Pages of type EfiBootServicesData with an
91 alignment specified by Alignment.
92 If Pages is 0, then NULL is returned.
93 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
94 If there is no enough memory at the specified alignment available to satisfy the
95 request, then NULL is returned.
96
97 @param Pages The number of 4 KB pages to allocate.
98 @param Alignment The requested alignment of the allocation.
99
100 @return A pointer to the allocated buffer or NULL if allocation fails.
101 **/
102 VOID *
103 EFIAPI
104 AllocateAlignedPages (
105 IN UINTN Pages,
106 IN UINTN Alignment
107 )
108 {
109 VOID *Memory;
110 UINTN AlignmentMask;
111
112 //
113 // Alignment must be a power of two or zero.
114 //
115 ASSERT ((Alignment & (Alignment - 1)) == 0);
116
117 if (Pages == 0) {
118 return NULL;
119 }
120
121 //
122 // Check overflow.
123 //
124 ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));
125
126 Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));
127 if (Memory == NULL) {
128 return NULL;
129 }
130
131 if (Alignment == 0) {
132 AlignmentMask = Alignment;
133 } else {
134 AlignmentMask = Alignment - 1;
135 }
136
137 return (VOID *)(UINTN)(((UINTN)Memory + AlignmentMask) & ~AlignmentMask);
138 }
139
140 /**
141 Allocates a buffer of type EfiBootServicesData.
142
143 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
144 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
145 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
146
147 @param AllocationSize The number of bytes to allocate.
148
149 @return A pointer to the allocated buffer or NULL if allocation fails.
150
151 **/
152 VOID *
153 EFIAPI
154 AllocatePool (
155 IN UINTN AllocationSize
156 )
157 {
158 EFI_HOB_MEMORY_POOL *Hob;
159
160 if (AllocationSize > 0x4000) {
161 // Please use AllocatePages for big allocations
162 return NULL;
163 }
164
165 Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (EFI_HOB_TYPE_MEMORY_POOL, (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + AllocationSize));
166 return (VOID *)(Hob + 1);
167 }
168
169 /**
170 Allocates and zeros a buffer of type EfiBootServicesData.
171
172 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
173 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
174 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
175 request, then NULL is returned.
176
177 @param AllocationSize The number of bytes to allocate and zero.
178
179 @return A pointer to the allocated buffer or NULL if allocation fails.
180
181 **/
182 VOID *
183 EFIAPI
184 AllocateZeroPool (
185 IN UINTN AllocationSize
186 )
187 {
188 VOID *Buffer;
189
190 Buffer = AllocatePool (AllocationSize);
191 if (Buffer == NULL) {
192 return NULL;
193 }
194
195 ZeroMem (Buffer, AllocationSize);
196
197 return Buffer;
198 }