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
;
25 Returns the pointer to the HOB list.
27 This function returns the pointer to first HOB in the list.
28 If the pointer to the HOB list is NULL, then ASSERT().
30 @return The pointer to the HOB list.
39 ASSERT (gHobList
!= NULL
);
44 Returns the next instance of a HOB type from the starting HOB.
46 This function searches the first instance of a HOB type from the starting HOB pointer.
47 If there does not exist such HOB type from the starting HOB pointer, it will return NULL.
48 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
49 unconditionally: it returns HobStart back if HobStart itself meets the requirement;
50 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
52 If HobStart is NULL, then ASSERT().
54 @param Type The HOB type to return.
55 @param HobStart The starting HOB pointer to search from.
57 @return The next instance of a HOB type from the starting HOB.
64 IN CONST VOID
*HobStart
67 EFI_PEI_HOB_POINTERS Hob
;
69 ASSERT (HobStart
!= NULL
);
71 Hob
.Raw
= (UINT8
*) HobStart
;
73 // Parse the HOB list until end of list or matching type is found.
75 while (!END_OF_HOB_LIST (Hob
)) {
76 if (Hob
.Header
->HobType
== Type
) {
79 Hob
.Raw
= GET_NEXT_HOB (Hob
);
85 Returns the first instance of a HOB type among the whole HOB list.
87 This function searches the first instance of a HOB type among the whole HOB list.
88 If there does not exist such HOB type in the HOB list, it will return NULL.
90 If the pointer to the HOB list is NULL, then ASSERT().
92 @param Type The HOB type to return.
94 @return The next instance of a HOB type from the starting HOB.
105 HobList
= GetHobList ();
106 return GetNextHob (Type
, HobList
);
110 Returns the next instance of the matched GUID HOB from the starting HOB.
112 This function searches the first instance of a HOB from the starting HOB pointer.
113 Such HOB should satisfy two conditions:
114 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION, and its GUID Name equals to the input Guid.
115 If such a HOB from the starting HOB pointer does not exist, it will return NULL.
116 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
117 to extract the data section and its size information, respectively.
118 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
119 unconditionally: it returns HobStart back if HobStart itself meets the requirement;
120 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
122 If Guid is NULL, then ASSERT().
123 If HobStart is NULL, then ASSERT().
125 @param Guid The GUID to match with in the HOB list.
126 @param HobStart A pointer to a Guid.
128 @return The next instance of the matched GUID HOB from the starting HOB.
134 IN CONST EFI_GUID
*Guid
,
135 IN CONST VOID
*HobStart
138 EFI_PEI_HOB_POINTERS GuidHob
;
140 GuidHob
.Raw
= (UINT8
*) HobStart
;
141 while ((GuidHob
.Raw
= GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION
, GuidHob
.Raw
)) != NULL
) {
142 if (CompareGuid (Guid
, &GuidHob
.Guid
->Name
)) {
145 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
151 Returns the first instance of the matched GUID HOB among the whole HOB list.
153 This function searches the first instance of a HOB among the whole HOB list.
154 Such HOB should satisfy two conditions:
155 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
156 If such a HOB from the starting HOB pointer does not exist, it will return NULL.
157 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
158 to extract the data section and its size information, respectively.
160 If the pointer to the HOB list is NULL, then ASSERT().
161 If Guid is NULL, then ASSERT().
163 @param Guid The GUID to match with in the HOB list.
165 @return The first instance of the matched GUID HOB among the whole HOB list.
171 IN CONST EFI_GUID
*Guid
176 HobList
= GetHobList ();
177 return GetNextGuidHob (Guid
, HobList
);
181 Get the system boot mode from the HOB list.
183 This function returns the system boot mode information from the
184 PHIT HOB in HOB list.
186 If the pointer to the HOB list is NULL, then ASSERT().
190 @return The Boot Mode.
199 EFI_HOB_HANDOFF_INFO_TABLE
*HandOffHob
;
201 HandOffHob
= (EFI_HOB_HANDOFF_INFO_TABLE
*) GetHobList ();
203 return HandOffHob
->BootMode
;
212 EFI_HOB_HANDOFF_INFO_TABLE
*HandOffHob
;
213 EFI_HOB_GENERIC_HEADER
*HobEnd
;
214 EFI_PHYSICAL_ADDRESS FreeMemory
;
217 HandOffHob
= GetHobList ();
219 HobLength
= (UINT16
)((HobLength
+ 0x7) & (~0x7));
221 FreeMemory
= HandOffHob
->EfiFreeMemoryTop
- HandOffHob
->EfiFreeMemoryBottom
;
223 if (FreeMemory
< HobLength
) {
227 Hob
= (VOID
*) (UINTN
) HandOffHob
->EfiEndOfHobList
;
228 ((EFI_HOB_GENERIC_HEADER
*) Hob
)->HobType
= HobType
;
229 ((EFI_HOB_GENERIC_HEADER
*) Hob
)->HobLength
= HobLength
;
230 ((EFI_HOB_GENERIC_HEADER
*) Hob
)->Reserved
= 0;
232 HobEnd
= (EFI_HOB_GENERIC_HEADER
*) ((UINTN
)Hob
+ HobLength
);
233 HandOffHob
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HobEnd
;
235 HobEnd
->HobType
= EFI_HOB_TYPE_END_OF_HOB_LIST
;
236 HobEnd
->HobLength
= sizeof (EFI_HOB_GENERIC_HEADER
);
237 HobEnd
->Reserved
= 0;
239 HandOffHob
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HobEnd
;
245 Builds a HOB for a loaded PE32 module.
247 This function builds a HOB for a loaded PE32 module.
248 If ModuleName is NULL, then ASSERT().
249 If there is no additional space for HOB creation, then ASSERT().
251 @param ModuleName The GUID File Name of the module.
252 @param MemoryAllocationModule The 64 bit physical address of the module.
253 @param ModuleLength The length of the module in bytes.
254 @param EntryPoint The 64 bit physical address of the module entry point.
260 IN CONST EFI_GUID
*ModuleName
,
261 IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule
,
262 IN UINT64 ModuleLength
,
263 IN EFI_PHYSICAL_ADDRESS EntryPoint
266 EFI_HOB_MEMORY_ALLOCATION_MODULE
*Hob
;
268 ASSERT (((MemoryAllocationModule
& (EFI_PAGE_SIZE
- 1)) == 0) &&
269 ((ModuleLength
& (EFI_PAGE_SIZE
- 1)) == 0));
271 Hob
= CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE
));
273 CopyGuid (&(Hob
->MemoryAllocationHeader
.Name
), &gEfiHobMemoryAllocModuleGuid
);
274 Hob
->MemoryAllocationHeader
.MemoryBaseAddress
= MemoryAllocationModule
;
275 Hob
->MemoryAllocationHeader
.MemoryLength
= ModuleLength
;
276 Hob
->MemoryAllocationHeader
.MemoryType
= EfiBootServicesCode
;
279 // Zero the reserved space to match HOB spec
281 ZeroMem (Hob
->MemoryAllocationHeader
.Reserved
, sizeof (Hob
->MemoryAllocationHeader
.Reserved
));
283 CopyGuid (&Hob
->ModuleName
, ModuleName
);
284 Hob
->EntryPoint
= EntryPoint
;
288 Builds a HOB that describes a chunk of system memory.
290 This function builds a HOB that describes a chunk of system memory.
291 If there is no additional space for HOB creation, then ASSERT().
293 @param ResourceType The type of resource described by this HOB.
294 @param ResourceAttribute The resource attributes of the memory described by this HOB.
295 @param PhysicalStart The 64 bit physical address of memory described by this HOB.
296 @param NumberOfBytes The length of the memory described by this HOB in bytes.
301 BuildResourceDescriptorHob (
302 IN EFI_RESOURCE_TYPE ResourceType
,
303 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute
,
304 IN EFI_PHYSICAL_ADDRESS PhysicalStart
,
305 IN UINT64 NumberOfBytes
308 EFI_HOB_RESOURCE_DESCRIPTOR
*Hob
;
310 Hob
= CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR
));
311 ASSERT (Hob
!= NULL
);
313 Hob
->ResourceType
= ResourceType
;
314 Hob
->ResourceAttribute
= ResourceAttribute
;
315 Hob
->PhysicalStart
= PhysicalStart
;
316 Hob
->ResourceLength
= NumberOfBytes
;
320 Builds a GUID HOB with a certain data length.
322 This function builds a customized HOB tagged with a GUID for identification
323 and returns the start address of GUID HOB data so that caller can fill the customized data.
324 The HOB Header and Name field is already stripped.
325 If Guid is NULL, then ASSERT().
326 If there is no additional space for HOB creation, then ASSERT().
327 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
329 @param Guid The GUID to tag the customized HOB.
330 @param DataLength The size of the data payload for the GUID HOB.
332 @return The start address of GUID HOB data.
338 IN CONST EFI_GUID
*Guid
,
342 EFI_HOB_GUID_TYPE
*Hob
;
345 // Make sure that data length is not too long.
347 ASSERT (DataLength
<= (0xffff - sizeof (EFI_HOB_GUID_TYPE
)));
349 Hob
= CreateHob (EFI_HOB_TYPE_GUID_EXTENSION
, (UINT16
) (sizeof (EFI_HOB_GUID_TYPE
) + DataLength
));
350 CopyGuid (&Hob
->Name
, Guid
);
356 Copies a data buffer to a newly-built HOB.
358 This function builds a customized HOB tagged with a GUID for identification,
359 copies the input data to the HOB data field and returns the start address of the GUID HOB data.
360 The HOB Header and Name field is already stripped.
361 If Guid is NULL, then ASSERT().
362 If Data is NULL and DataLength > 0, then ASSERT().
363 If there is no additional space for HOB creation, then ASSERT().
364 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
366 @param Guid The GUID to tag the customized HOB.
367 @param Data The data to be copied into the data field of the GUID HOB.
368 @param DataLength The size of the data payload for the GUID HOB.
370 @return The start address of GUID HOB data.
376 IN CONST EFI_GUID
*Guid
,
383 ASSERT (Data
!= NULL
|| DataLength
== 0);
385 HobData
= BuildGuidHob (Guid
, DataLength
);
387 return CopyMem (HobData
, Data
, DataLength
);
391 Builds a Firmware Volume HOB.
393 This function builds a Firmware Volume HOB.
394 If there is no additional space for HOB creation, then ASSERT().
396 @param BaseAddress The base address of the Firmware Volume.
397 @param Length The size of the Firmware Volume in bytes.
403 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
407 EFI_HOB_FIRMWARE_VOLUME
*Hob
;
409 Hob
= CreateHob (EFI_HOB_TYPE_FV
, sizeof (EFI_HOB_FIRMWARE_VOLUME
));
411 Hob
->BaseAddress
= BaseAddress
;
412 Hob
->Length
= Length
;
417 Builds a EFI_HOB_TYPE_FV2 HOB.
419 This function builds a EFI_HOB_TYPE_FV2 HOB.
420 If there is no additional space for HOB creation, then ASSERT().
422 @param BaseAddress The base address of the Firmware Volume.
423 @param Length The size of the Firmware Volume in bytes.
424 @param FvName The name of the Firmware Volume.
425 @param FileName The name of the file.
431 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
433 IN CONST EFI_GUID
*FvName
,
434 IN CONST EFI_GUID
*FileName
437 EFI_HOB_FIRMWARE_VOLUME2
*Hob
;
439 Hob
= CreateHob (EFI_HOB_TYPE_FV2
, sizeof (EFI_HOB_FIRMWARE_VOLUME2
));
441 Hob
->BaseAddress
= BaseAddress
;
442 Hob
->Length
= Length
;
443 CopyGuid (&Hob
->FvName
, FvName
);
444 CopyGuid (&Hob
->FileName
, FileName
);
449 Builds a HOB for the CPU.
451 This function builds a HOB for the CPU.
452 If there is no additional space for HOB creation, then ASSERT().
454 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
455 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
461 IN UINT8 SizeOfMemorySpace
,
462 IN UINT8 SizeOfIoSpace
467 Hob
= CreateHob (EFI_HOB_TYPE_CPU
, sizeof (EFI_HOB_CPU
));
469 Hob
->SizeOfMemorySpace
= SizeOfMemorySpace
;
470 Hob
->SizeOfIoSpace
= SizeOfIoSpace
;
473 // Zero the reserved space to match HOB spec
475 ZeroMem (Hob
->Reserved
, sizeof (Hob
->Reserved
));
479 Builds a HOB for the memory allocation.
481 This function builds a HOB for the memory allocation.
482 If there is no additional space for HOB creation, then ASSERT().
484 @param BaseAddress The 64 bit physical address of the memory.
485 @param Length The length of the memory allocation in bytes.
486 @param MemoryType Type of memory allocated by this HOB.
491 BuildMemoryAllocationHob (
492 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
494 IN EFI_MEMORY_TYPE MemoryType
497 EFI_HOB_MEMORY_ALLOCATION
*Hob
;
499 ASSERT (((BaseAddress
& (EFI_PAGE_SIZE
- 1)) == 0) &&
500 ((Length
& (EFI_PAGE_SIZE
- 1)) == 0));
502 Hob
= CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION
));
504 ZeroMem (&(Hob
->AllocDescriptor
.Name
), sizeof (EFI_GUID
));
505 Hob
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
506 Hob
->AllocDescriptor
.MemoryLength
= Length
;
507 Hob
->AllocDescriptor
.MemoryType
= MemoryType
;
509 // Zero the reserved space to match HOB spec
511 ZeroMem (Hob
->AllocDescriptor
.Reserved
, sizeof (Hob
->AllocDescriptor
.Reserved
));
515 Builds a HOB that describes a chunk of system memory with Owner GUID.
517 This function builds a HOB that describes a chunk of system memory.
518 If there is no additional space for HOB creation, then ASSERT().
520 @param ResourceType The type of resource described by this HOB.
521 @param ResourceAttribute The resource attributes of the memory described by this HOB.
522 @param PhysicalStart The 64 bit physical address of memory described by this HOB.
523 @param NumberOfBytes The length of the memory described by this HOB in bytes.
524 @param OwnerGUID GUID for the owner of this resource.
529 BuildResourceDescriptorWithOwnerHob (
530 IN EFI_RESOURCE_TYPE ResourceType
,
531 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute
,
532 IN EFI_PHYSICAL_ADDRESS PhysicalStart
,
533 IN UINT64 NumberOfBytes
,
534 IN EFI_GUID
*OwnerGUID
541 Builds a Capsule Volume HOB.
543 This function builds a Capsule Volume HOB.
544 If the platform does not support Capsule Volume HOBs, then ASSERT().
545 If there is no additional space for HOB creation, then ASSERT().
547 @param BaseAddress The base address of the Capsule Volume.
548 @param Length The size of the Capsule Volume in bytes.
554 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
563 Builds a HOB for the BSP store.
565 This function builds a HOB for BSP store.
566 If there is no additional space for HOB creation, then ASSERT().
568 @param BaseAddress The 64 bit physical address of the BSP.
569 @param Length The length of the BSP store in bytes.
570 @param MemoryType Type of memory allocated by this HOB.
576 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
578 IN EFI_MEMORY_TYPE MemoryType
585 Builds a HOB for the Stack.
587 This function builds a HOB for the stack.
588 If there is no additional space for HOB creation, then ASSERT().
590 @param BaseAddress The 64 bit physical address of the Stack.
591 @param Length The length of the stack in bytes.
597 IN EFI_PHYSICAL_ADDRESS BaseAddress
,