VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/UefiPayloadPkg/Library/HobParseLib/HobParseLib.c@ 108794

Last change on this file since 108794 was 108794, checked in by vboxsync, 2 weeks ago

Devices/EFI/FirmwareNew: Merge edk2-stable202502 from the vendor branch and make it build for the important platforms, bugref:4643

  • Property svn:eol-style set to native
File size: 7.8 KB
Line 
1/** @file
2 Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
3 SPDX-License-Identifier: BSD-2-Clause-Patent
4**/
5
6#include <PiPei.h>
7#include <Library/BaseLib.h>
8#include <Library/BaseMemoryLib.h>
9#include <Library/MemoryAllocationLib.h>
10#include <Library/DebugLib.h>
11#include <Library/HobLib.h>
12#include <Library/PcdLib.h>
13#include <Guid/MemoryAllocationHob.h>
14#include <Library/IoLib.h>
15#include <Library/CpuLib.h>
16#include <IndustryStandard/Acpi.h>
17#include <IndustryStandard/MemoryMappedConfigurationSpaceAccessTable.h>
18#include <Guid/AcpiBoardInfoGuid.h>
19#include <UniversalPayload/AcpiTable.h>
20#include <UniversalPayload/UniversalPayload.h>
21#include <UniversalPayload/ExtraData.h>
22
23#define MEMORY_ATTRIBUTE_MASK (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
24 EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \
25 EFI_RESOURCE_ATTRIBUTE_TESTED | \
26 EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED | \
27 EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED | \
28 EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED | \
29 EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED | \
30 EFI_RESOURCE_ATTRIBUTE_16_BIT_IO | \
31 EFI_RESOURCE_ATTRIBUTE_32_BIT_IO | \
32 EFI_RESOURCE_ATTRIBUTE_64_BIT_IO | \
33 EFI_RESOURCE_ATTRIBUTE_PERSISTENT )
34
35#define TESTED_MEMORY_ATTRIBUTES (EFI_RESOURCE_ATTRIBUTE_PRESENT | \
36 EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \
37 EFI_RESOURCE_ATTRIBUTE_TESTED )
38
39extern VOID *mHobList;
40
41/**
42 Add a new HOB to the HOB List.
43
44 @param HobType Type of the new HOB.
45 @param HobLength Length of the new HOB to allocate.
46
47 @return NULL if there is no space to create a hob.
48 @return The address point to the new created hob.
49
50**/
51VOID *
52EFIAPI
53CreateHob (
54 IN UINT16 HobType,
55 IN UINT16 HobLength
56 );
57
58/**
59 Build a Handoff Information Table HOB
60
61 This function initialize a HOB region from EfiMemoryBegin to
62 EfiMemoryTop. And EfiFreeMemoryBottom and EfiFreeMemoryTop should
63 be inside the HOB region.
64
65 @param[in] EfiMemoryBottom Total memory start address
66 @param[in] EfiMemoryTop Total memory end address.
67 @param[in] EfiFreeMemoryBottom Free memory start address
68 @param[in] EfiFreeMemoryTop Free memory end address.
69
70 @return The pointer to the handoff HOB table.
71
72**/
73EFI_HOB_HANDOFF_INFO_TABLE *
74EFIAPI
75HobConstructor (
76 IN VOID *EfiMemoryBottom,
77 IN VOID *EfiMemoryTop,
78 IN VOID *EfiFreeMemoryBottom,
79 IN VOID *EfiFreeMemoryTop
80 );
81
82/**
83 Build ACPI board info HOB using infomation from ACPI table
84
85 @param AcpiTableBase ACPI table start address in memory
86
87 @retval A pointer to ACPI board HOB ACPI_BOARD_INFO. Null if build HOB failure.
88**/
89ACPI_BOARD_INFO *
90BuildHobFromAcpi (
91 IN UINT64 AcpiTableBase
92 );
93
94/**
95 *
96 Add HOB into HOB list
97
98 @param[in] Hob The HOB to be added into the HOB list.
99**/
100VOID
101AddNewHob (
102 IN EFI_PEI_HOB_POINTERS *Hob
103 )
104{
105 EFI_PEI_HOB_POINTERS NewHob;
106
107 if (Hob->Raw == NULL) {
108 return;
109 }
110
111 NewHob.Header = CreateHob (Hob->Header->HobType, Hob->Header->HobLength);
112
113 if (NewHob.Header != NULL) {
114 CopyMem (NewHob.Header + 1, Hob->Header + 1, Hob->Header->HobLength - sizeof (EFI_HOB_GENERIC_HEADER));
115 }
116}
117
118/**
119 Found the Resource Descriptor HOB that contains a range (Base, Top)
120
121 @param[in] HobList Hob start address
122 @param[in] Base Memory start address
123 @param[in] Top Memory end address.
124
125 @retval The pointer to the Resource Descriptor HOB.
126**/
127EFI_HOB_RESOURCE_DESCRIPTOR *
128FindResourceDescriptorByRange (
129 IN VOID *HobList,
130 IN EFI_PHYSICAL_ADDRESS Base,
131 IN EFI_PHYSICAL_ADDRESS Top
132 )
133{
134 EFI_PEI_HOB_POINTERS Hob;
135 EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;
136
137 for (Hob.Raw = (UINT8 *)HobList; !END_OF_HOB_LIST (Hob); Hob.Raw = GET_NEXT_HOB (Hob)) {
138 //
139 // Skip all HOBs except Resource Descriptor HOBs
140 //
141 if (GET_HOB_TYPE (Hob) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
142 continue;
143 }
144
145 //
146 // Skip Resource Descriptor HOBs that do not describe tested system memory
147 //
148 ResourceHob = Hob.ResourceDescriptor;
149 if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY) {
150 continue;
151 }
152
153 if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) != TESTED_MEMORY_ATTRIBUTES) {
154 continue;
155 }
156
157 //
158 // Skip Resource Descriptor HOBs that do not contain the PHIT range EfiFreeMemoryBottom..EfiFreeMemoryTop
159 //
160 if (Base < ResourceHob->PhysicalStart) {
161 continue;
162 }
163
164 if (Top > (ResourceHob->PhysicalStart + ResourceHob->ResourceLength)) {
165 continue;
166 }
167
168 return ResourceHob;
169 }
170
171 return NULL;
172}
173
174/**
175 Find the highest below 4G memory resource descriptor, except the input Resource Descriptor.
176
177 @param[in] HobList Hob start address
178 @param[in] MinimalNeededSize Minimal needed size.
179 @param[in] ExceptResourceHob Ignore this Resource Descriptor.
180
181 @retval The pointer to the Resource Descriptor HOB.
182**/
183EFI_HOB_RESOURCE_DESCRIPTOR *
184FindAnotherHighestBelow4GResourceDescriptor (
185 IN VOID *HobList,
186 IN UINTN MinimalNeededSize,
187 IN EFI_HOB_RESOURCE_DESCRIPTOR *ExceptResourceHob
188 )
189{
190 EFI_PEI_HOB_POINTERS Hob;
191 EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;
192 EFI_HOB_RESOURCE_DESCRIPTOR *ReturnResourceHob;
193
194 ReturnResourceHob = NULL;
195
196 for (Hob.Raw = (UINT8 *)HobList; !END_OF_HOB_LIST (Hob); Hob.Raw = GET_NEXT_HOB (Hob)) {
197 //
198 // Skip all HOBs except Resource Descriptor HOBs
199 //
200 if (GET_HOB_TYPE (Hob) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
201 continue;
202 }
203
204 //
205 // Skip Resource Descriptor HOBs that do not describe tested system memory
206 //
207 ResourceHob = Hob.ResourceDescriptor;
208 if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY) {
209 continue;
210 }
211
212 if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) != TESTED_MEMORY_ATTRIBUTES) {
213 continue;
214 }
215
216 //
217 // Skip if the Resource Descriptor HOB equals to ExceptResourceHob
218 //
219 if (ResourceHob == ExceptResourceHob) {
220 continue;
221 }
222
223 //
224 // Skip Resource Descriptor HOBs that are beyond 4G
225 //
226 if ((ResourceHob->PhysicalStart + ResourceHob->ResourceLength) > BASE_4GB) {
227 continue;
228 }
229
230 //
231 // Skip Resource Descriptor HOBs that are too small
232 //
233 if (ResourceHob->ResourceLength < MinimalNeededSize) {
234 continue;
235 }
236
237 //
238 // Return the topest Resource Descriptor
239 //
240 if (ReturnResourceHob == NULL) {
241 ReturnResourceHob = ResourceHob;
242 } else {
243 if (ReturnResourceHob->PhysicalStart < ResourceHob->PhysicalStart) {
244 ReturnResourceHob = ResourceHob;
245 }
246 }
247 }
248
249 return ReturnResourceHob;
250}
251
252/**
253 Check the HOB and decide if it is need inside Payload
254
255 Payload maintainer may make decision which HOB is need or needn't
256 Then add the check logic in the function.
257
258 @param[in] Hob The HOB to check
259
260 @retval TRUE If HOB is need inside Payload
261 @retval FALSE If HOB is needn't inside Payload
262**/
263BOOLEAN
264IsHobNeed (
265 EFI_PEI_HOB_POINTERS Hob
266 )
267{
268 if (Hob.Header->HobType == EFI_HOB_TYPE_HANDOFF) {
269 return FALSE;
270 }
271
272 if (Hob.Header->HobType == EFI_HOB_TYPE_MEMORY_ALLOCATION) {
273 if (CompareGuid (&Hob.MemoryAllocationModule->MemoryAllocationHeader.Name, &gEfiHobMemoryAllocModuleGuid)) {
274 return FALSE;
275 }
276 }
277
278 // Arrive here mean the HOB is need
279 return TRUE;
280}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette