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 | /**
|
---|
142 | Allocates a buffer of type EfiBootServicesData.
|
---|
143 |
|
---|
144 | Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
|
---|
145 | pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
|
---|
146 | returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
|
---|
147 |
|
---|
148 | @param AllocationSize The number of bytes to allocate.
|
---|
149 |
|
---|
150 | @return A pointer to the allocated buffer or NULL if allocation fails.
|
---|
151 |
|
---|
152 | **/
|
---|
153 | VOID *
|
---|
154 | EFIAPI
|
---|
155 | AllocatePool (
|
---|
156 | IN UINTN AllocationSize
|
---|
157 | )
|
---|
158 | {
|
---|
159 | EFI_HOB_MEMORY_POOL *Hob;
|
---|
160 |
|
---|
161 | if (AllocationSize > 0x4000) {
|
---|
162 | // Please use AllocatePages for big allocations
|
---|
163 | return NULL;
|
---|
164 | }
|
---|
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 | Allocates and zeros a buffer of type EfiBootServicesData.
|
---|
172 |
|
---|
173 | Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
|
---|
174 | buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
|
---|
175 | valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
|
---|
176 | request, then NULL is returned.
|
---|
177 |
|
---|
178 | @param AllocationSize The number of bytes to allocate and zero.
|
---|
179 |
|
---|
180 | @return A pointer to the allocated buffer or NULL if allocation fails.
|
---|
181 |
|
---|
182 | **/
|
---|
183 | VOID *
|
---|
184 | EFIAPI
|
---|
185 | AllocateZeroPool (
|
---|
186 | IN UINTN AllocationSize
|
---|
187 | )
|
---|
188 | {
|
---|
189 | VOID *Buffer;
|
---|
190 |
|
---|
191 | Buffer = AllocatePool (AllocationSize);
|
---|
192 | if (Buffer == NULL) {
|
---|
193 | return NULL;
|
---|
194 | }
|
---|
195 |
|
---|
196 | ZeroMem (Buffer, AllocationSize);
|
---|
197 |
|
---|
198 | return Buffer;
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|