2 HOB Library implementation for Standalone MM Core.
4 Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2017 - 2018, ARM Limited. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Library/HobLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/BaseMemoryLib.h>
17 #include <Guid/MemoryAllocationHob.h>
20 // Cache copy of HobList pointer.
22 VOID
*gHobList
= NULL
;
30 EFI_HOB_HANDOFF_INFO_TABLE
*HandOffHob
;
31 EFI_HOB_GENERIC_HEADER
*HobEnd
;
32 EFI_PHYSICAL_ADDRESS FreeMemory
;
35 HandOffHob
= GetHobList ();
37 HobLength
= (UINT16
)((HobLength
+ 0x7) & (~0x7));
39 FreeMemory
= HandOffHob
->EfiFreeMemoryTop
- HandOffHob
->EfiFreeMemoryBottom
;
41 if (FreeMemory
< HobLength
) {
45 Hob
= (VOID
*) (UINTN
) HandOffHob
->EfiEndOfHobList
;
46 ((EFI_HOB_GENERIC_HEADER
*) Hob
)->HobType
= HobType
;
47 ((EFI_HOB_GENERIC_HEADER
*) Hob
)->HobLength
= HobLength
;
48 ((EFI_HOB_GENERIC_HEADER
*) Hob
)->Reserved
= 0;
50 HobEnd
= (EFI_HOB_GENERIC_HEADER
*) ((UINTN
)Hob
+ HobLength
);
51 HandOffHob
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HobEnd
;
53 HobEnd
->HobType
= EFI_HOB_TYPE_END_OF_HOB_LIST
;
54 HobEnd
->HobLength
= sizeof (EFI_HOB_GENERIC_HEADER
);
57 HandOffHob
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HobEnd
;
63 Builds a HOB for a loaded PE32 module.
65 This function builds a HOB for a loaded PE32 module.
66 If ModuleName is NULL, then ASSERT().
67 If there is no additional space for HOB creation, then ASSERT().
69 @param ModuleName The GUID File Name of the module.
70 @param MemoryAllocationModule The 64 bit physical address of the module.
71 @param ModuleLength The length of the module in bytes.
72 @param EntryPoint The 64 bit physical address of the module entry point.
78 IN CONST EFI_GUID
*ModuleName
,
79 IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule
,
80 IN UINT64 ModuleLength
,
81 IN EFI_PHYSICAL_ADDRESS EntryPoint
84 EFI_HOB_MEMORY_ALLOCATION_MODULE
*Hob
;
86 ASSERT (((MemoryAllocationModule
& (EFI_PAGE_SIZE
- 1)) == 0) &&
87 ((ModuleLength
& (EFI_PAGE_SIZE
- 1)) == 0));
89 Hob
= CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE
));
91 CopyGuid (&(Hob
->MemoryAllocationHeader
.Name
), &gEfiHobMemoryAllocModuleGuid
);
92 Hob
->MemoryAllocationHeader
.MemoryBaseAddress
= MemoryAllocationModule
;
93 Hob
->MemoryAllocationHeader
.MemoryLength
= ModuleLength
;
94 Hob
->MemoryAllocationHeader
.MemoryType
= EfiBootServicesCode
;
97 // Zero the reserved space to match HOB spec
99 ZeroMem (Hob
->MemoryAllocationHeader
.Reserved
, sizeof (Hob
->MemoryAllocationHeader
.Reserved
));
101 CopyGuid (&Hob
->ModuleName
, ModuleName
);
102 Hob
->EntryPoint
= EntryPoint
;
106 Builds a HOB that describes a chunk of system memory.
108 This function builds a HOB that describes a chunk of system memory.
109 If there is no additional space for HOB creation, then ASSERT().
111 @param ResourceType The type of resource described by this HOB.
112 @param ResourceAttribute The resource attributes of the memory described by this HOB.
113 @param PhysicalStart The 64 bit physical address of memory described by this HOB.
114 @param NumberOfBytes The length of the memory described by this HOB in bytes.
119 BuildResourceDescriptorHob (
120 IN EFI_RESOURCE_TYPE ResourceType
,
121 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute
,
122 IN EFI_PHYSICAL_ADDRESS PhysicalStart
,
123 IN UINT64 NumberOfBytes
126 EFI_HOB_RESOURCE_DESCRIPTOR
*Hob
;
128 Hob
= CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR
));
129 ASSERT (Hob
!= NULL
);
131 Hob
->ResourceType
= ResourceType
;
132 Hob
->ResourceAttribute
= ResourceAttribute
;
133 Hob
->PhysicalStart
= PhysicalStart
;
134 Hob
->ResourceLength
= NumberOfBytes
;
138 Builds a GUID HOB with a certain data length.
140 This function builds a customized HOB tagged with a GUID for identification
141 and returns the start address of GUID HOB data so that caller can fill the customized data.
142 The HOB Header and Name field is already stripped.
143 If Guid is NULL, then ASSERT().
144 If there is no additional space for HOB creation, then ASSERT().
145 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
147 @param Guid The GUID to tag the customized HOB.
148 @param DataLength The size of the data payload for the GUID HOB.
150 @return The start address of GUID HOB data.
156 IN CONST EFI_GUID
*Guid
,
160 EFI_HOB_GUID_TYPE
*Hob
;
163 // Make sure that data length is not too long.
165 ASSERT (DataLength
<= (0xffff - sizeof (EFI_HOB_GUID_TYPE
)));
167 Hob
= CreateHob (EFI_HOB_TYPE_GUID_EXTENSION
, (UINT16
) (sizeof (EFI_HOB_GUID_TYPE
) + DataLength
));
168 CopyGuid (&Hob
->Name
, Guid
);
174 Copies a data buffer to a newly-built HOB.
176 This function builds a customized HOB tagged with a GUID for identification,
177 copies the input data to the HOB data field and returns the start address of the GUID HOB data.
178 The HOB Header and Name field is already stripped.
179 If Guid is NULL, then ASSERT().
180 If Data is NULL and DataLength > 0, then ASSERT().
181 If there is no additional space for HOB creation, then ASSERT().
182 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
184 @param Guid The GUID to tag the customized HOB.
185 @param Data The data to be copied into the data field of the GUID HOB.
186 @param DataLength The size of the data payload for the GUID HOB.
188 @return The start address of GUID HOB data.
194 IN CONST EFI_GUID
*Guid
,
201 ASSERT (Data
!= NULL
|| DataLength
== 0);
203 HobData
= BuildGuidHob (Guid
, DataLength
);
205 return CopyMem (HobData
, Data
, DataLength
);
209 Builds a Firmware Volume HOB.
211 This function builds a Firmware Volume HOB.
212 If there is no additional space for HOB creation, then ASSERT().
214 @param BaseAddress The base address of the Firmware Volume.
215 @param Length The size of the Firmware Volume in bytes.
221 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
225 EFI_HOB_FIRMWARE_VOLUME
*Hob
;
227 Hob
= CreateHob (EFI_HOB_TYPE_FV
, sizeof (EFI_HOB_FIRMWARE_VOLUME
));
229 Hob
->BaseAddress
= BaseAddress
;
230 Hob
->Length
= Length
;
235 Builds a EFI_HOB_TYPE_FV2 HOB.
237 This function builds a EFI_HOB_TYPE_FV2 HOB.
238 If there is no additional space for HOB creation, then ASSERT().
240 @param BaseAddress The base address of the Firmware Volume.
241 @param Length The size of the Firmware Volume in bytes.
242 @param FvName The name of the Firmware Volume.
243 @param FileName The name of the file.
249 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
251 IN CONST EFI_GUID
*FvName
,
252 IN CONST EFI_GUID
*FileName
255 EFI_HOB_FIRMWARE_VOLUME2
*Hob
;
257 Hob
= CreateHob (EFI_HOB_TYPE_FV2
, sizeof (EFI_HOB_FIRMWARE_VOLUME2
));
259 Hob
->BaseAddress
= BaseAddress
;
260 Hob
->Length
= Length
;
261 CopyGuid (&Hob
->FvName
, FvName
);
262 CopyGuid (&Hob
->FileName
, FileName
);
267 Builds a HOB for the CPU.
269 This function builds a HOB for the CPU.
270 If there is no additional space for HOB creation, then ASSERT().
272 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
273 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
279 IN UINT8 SizeOfMemorySpace
,
280 IN UINT8 SizeOfIoSpace
285 Hob
= CreateHob (EFI_HOB_TYPE_CPU
, sizeof (EFI_HOB_CPU
));
287 Hob
->SizeOfMemorySpace
= SizeOfMemorySpace
;
288 Hob
->SizeOfIoSpace
= SizeOfIoSpace
;
291 // Zero the reserved space to match HOB spec
293 ZeroMem (Hob
->Reserved
, sizeof (Hob
->Reserved
));
297 Builds a HOB for the memory allocation.
299 This function builds a HOB for the memory allocation.
300 If there is no additional space for HOB creation, then ASSERT().
302 @param BaseAddress The 64 bit physical address of the memory.
303 @param Length The length of the memory allocation in bytes.
304 @param MemoryType Type of memory allocated by this HOB.
309 BuildMemoryAllocationHob (
310 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
312 IN EFI_MEMORY_TYPE MemoryType
315 EFI_HOB_MEMORY_ALLOCATION
*Hob
;
317 ASSERT (((BaseAddress
& (EFI_PAGE_SIZE
- 1)) == 0) &&
318 ((Length
& (EFI_PAGE_SIZE
- 1)) == 0));
320 Hob
= CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION
));
322 ZeroMem (&(Hob
->AllocDescriptor
.Name
), sizeof (EFI_GUID
));
323 Hob
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
324 Hob
->AllocDescriptor
.MemoryLength
= Length
;
325 Hob
->AllocDescriptor
.MemoryType
= MemoryType
;
327 // Zero the reserved space to match HOB spec
329 ZeroMem (Hob
->AllocDescriptor
.Reserved
, sizeof (Hob
->AllocDescriptor
.Reserved
));