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>
6 Copyright (c) 2018, Linaro, Ltd. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
14 #include <Library/HobLib.h>
15 #include <Library/DebugLib.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/MmServicesTableLib.h>
20 // Cache copy of HobList pointer.
22 STATIC VOID
*gHobList
= NULL
;
25 The constructor function caches the pointer to HOB list.
27 The constructor function gets the start address of HOB list from system configuration table.
28 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
30 @param ImageHandle The firmware allocated handle for the image.
31 @param MmSystemTable A pointer to the MM System Table.
33 @retval EFI_SUCCESS The constructor successfully gets HobList.
34 @retval Other value The constructor can't get HobList.
40 IN EFI_HANDLE ImageHandle
,
41 IN EFI_MM_SYSTEM_TABLE
*MmSystemTable
46 for (Index
= 0; Index
< gMmst
->NumberOfTableEntries
; Index
++) {
47 if (CompareGuid (&gEfiHobListGuid
, &gMmst
->MmConfigurationTable
[Index
].VendorGuid
)) {
48 gHobList
= gMmst
->MmConfigurationTable
[Index
].VendorTable
;
57 Returns the pointer to the HOB list.
59 This function returns the pointer to first HOB in the list.
60 If the pointer to the HOB list is NULL, then ASSERT().
62 @return The pointer to the HOB list.
73 if (gHobList
== NULL
) {
74 for (Index
= 0; Index
< gMmst
->NumberOfTableEntries
; Index
++) {
75 if (CompareGuid (&gEfiHobListGuid
, &gMmst
->MmConfigurationTable
[Index
].VendorGuid
)) {
76 gHobList
= gMmst
->MmConfigurationTable
[Index
].VendorTable
;
82 ASSERT (gHobList
!= NULL
);
87 Returns the next instance of a HOB type from the starting HOB.
89 This function searches the first instance of a HOB type from the starting HOB pointer.
90 If there does not exist such HOB type from the starting HOB pointer, it will return NULL.
91 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
92 unconditionally: it returns HobStart back if HobStart itself meets the requirement;
93 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
95 If HobStart is NULL, then ASSERT().
97 @param Type The HOB type to return.
98 @param HobStart The starting HOB pointer to search from.
100 @return The next instance of a HOB type from the starting HOB.
107 IN CONST VOID
*HobStart
110 EFI_PEI_HOB_POINTERS Hob
;
112 ASSERT (HobStart
!= NULL
);
114 Hob
.Raw
= (UINT8
*)HobStart
;
116 // Parse the HOB list until end of list or matching type is found.
118 while (!END_OF_HOB_LIST (Hob
)) {
119 if (Hob
.Header
->HobType
== Type
) {
123 Hob
.Raw
= GET_NEXT_HOB (Hob
);
130 Returns the first instance of a HOB type among the whole HOB list.
132 This function searches the first instance of a HOB type among the whole HOB list.
133 If there does not exist such HOB type in the HOB list, it will return NULL.
135 If the pointer to the HOB list is NULL, then ASSERT().
137 @param Type The HOB type to return.
139 @return The next instance of a HOB type from the starting HOB.
150 HobList
= GetHobList ();
151 return GetNextHob (Type
, HobList
);
155 Returns the next instance of the matched GUID HOB from the starting HOB.
157 This function searches the first instance of a HOB from the starting HOB pointer.
158 Such HOB should satisfy two conditions:
159 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION, and its GUID Name equals to the input Guid.
160 If such a HOB from the starting HOB pointer does not exist, it will return NULL.
161 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
162 to extract the data section and its size information, respectively.
163 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
164 unconditionally: it returns HobStart back if HobStart itself meets the requirement;
165 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
167 If Guid is NULL, then ASSERT().
168 If HobStart is NULL, then ASSERT().
170 @param Guid The GUID to match with in the HOB list.
171 @param HobStart A pointer to a Guid.
173 @return The next instance of the matched GUID HOB from the starting HOB.
179 IN CONST EFI_GUID
*Guid
,
180 IN CONST VOID
*HobStart
183 EFI_PEI_HOB_POINTERS GuidHob
;
185 GuidHob
.Raw
= (UINT8
*)HobStart
;
186 while ((GuidHob
.Raw
= GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION
, GuidHob
.Raw
)) != NULL
) {
187 if (CompareGuid (Guid
, &GuidHob
.Guid
->Name
)) {
191 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
198 Returns the first instance of the matched GUID HOB among the whole HOB list.
200 This function searches the first instance of a HOB among the whole HOB list.
201 Such HOB should satisfy two conditions:
202 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
203 If such a HOB from the starting HOB pointer does not exist, it will return NULL.
204 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
205 to extract the data section and its size information, respectively.
207 If the pointer to the HOB list is NULL, then ASSERT().
208 If Guid is NULL, then ASSERT().
210 @param Guid The GUID to match with in the HOB list.
212 @return The first instance of the matched GUID HOB among the whole HOB list.
218 IN CONST EFI_GUID
*Guid
223 HobList
= GetHobList ();
224 return GetNextGuidHob (Guid
, HobList
);
228 Get the system boot mode from the HOB list.
230 This function returns the system boot mode information from the
231 PHIT HOB in HOB list.
233 If the pointer to the HOB list is NULL, then ASSERT().
237 @return The Boot Mode.
246 EFI_HOB_HANDOFF_INFO_TABLE
*HandOffHob
;
248 HandOffHob
= (EFI_HOB_HANDOFF_INFO_TABLE
*)GetHobList ();
250 return HandOffHob
->BootMode
;
259 EFI_HOB_HANDOFF_INFO_TABLE
*HandOffHob
;
260 EFI_HOB_GENERIC_HEADER
*HobEnd
;
261 EFI_PHYSICAL_ADDRESS FreeMemory
;
264 HandOffHob
= GetHobList ();
266 HobLength
= (UINT16
)((HobLength
+ 0x7) & (~0x7));
268 FreeMemory
= HandOffHob
->EfiFreeMemoryTop
- HandOffHob
->EfiFreeMemoryBottom
;
270 if (FreeMemory
< HobLength
) {
274 Hob
= (VOID
*)(UINTN
)HandOffHob
->EfiEndOfHobList
;
275 ((EFI_HOB_GENERIC_HEADER
*)Hob
)->HobType
= HobType
;
276 ((EFI_HOB_GENERIC_HEADER
*)Hob
)->HobLength
= HobLength
;
277 ((EFI_HOB_GENERIC_HEADER
*)Hob
)->Reserved
= 0;
279 HobEnd
= (EFI_HOB_GENERIC_HEADER
*)((UINTN
)Hob
+ HobLength
);
280 HandOffHob
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)HobEnd
;
282 HobEnd
->HobType
= EFI_HOB_TYPE_END_OF_HOB_LIST
;
283 HobEnd
->HobLength
= sizeof (EFI_HOB_GENERIC_HEADER
);
284 HobEnd
->Reserved
= 0;
286 HandOffHob
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)HobEnd
;
292 Builds a HOB for a loaded PE32 module.
294 This function builds a HOB for a loaded PE32 module.
295 If ModuleName is NULL, then ASSERT().
296 If there is no additional space for HOB creation, then ASSERT().
298 @param ModuleName The GUID File Name of the module.
299 @param MemoryAllocationModule The 64 bit physical address of the module.
300 @param ModuleLength The length of the module in bytes.
301 @param EntryPoint The 64 bit physical address of the module entry point.
307 IN CONST EFI_GUID
*ModuleName
,
308 IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule
,
309 IN UINT64 ModuleLength
,
310 IN EFI_PHYSICAL_ADDRESS EntryPoint
313 EFI_HOB_MEMORY_ALLOCATION_MODULE
*Hob
;
316 ((MemoryAllocationModule
& (EFI_PAGE_SIZE
- 1)) == 0) &&
317 ((ModuleLength
& (EFI_PAGE_SIZE
- 1)) == 0)
320 Hob
= CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE
));
322 CopyGuid (&(Hob
->MemoryAllocationHeader
.Name
), &gEfiHobMemoryAllocModuleGuid
);
323 Hob
->MemoryAllocationHeader
.MemoryBaseAddress
= MemoryAllocationModule
;
324 Hob
->MemoryAllocationHeader
.MemoryLength
= ModuleLength
;
325 Hob
->MemoryAllocationHeader
.MemoryType
= EfiBootServicesCode
;
328 // Zero the reserved space to match HOB spec
330 ZeroMem (Hob
->MemoryAllocationHeader
.Reserved
, sizeof (Hob
->MemoryAllocationHeader
.Reserved
));
332 CopyGuid (&Hob
->ModuleName
, ModuleName
);
333 Hob
->EntryPoint
= EntryPoint
;
337 Builds a HOB that describes a chunk of system memory.
339 This function builds a HOB that describes a chunk of system memory.
340 If there is no additional space for HOB creation, then ASSERT().
342 @param ResourceType The type of resource described by this HOB.
343 @param ResourceAttribute The resource attributes of the memory described by this HOB.
344 @param PhysicalStart The 64 bit physical address of memory described by this HOB.
345 @param NumberOfBytes The length of the memory described by this HOB in bytes.
350 BuildResourceDescriptorHob (
351 IN EFI_RESOURCE_TYPE ResourceType
,
352 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute
,
353 IN EFI_PHYSICAL_ADDRESS PhysicalStart
,
354 IN UINT64 NumberOfBytes
357 EFI_HOB_RESOURCE_DESCRIPTOR
*Hob
;
359 Hob
= CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR
));
360 ASSERT (Hob
!= NULL
);
362 Hob
->ResourceType
= ResourceType
;
363 Hob
->ResourceAttribute
= ResourceAttribute
;
364 Hob
->PhysicalStart
= PhysicalStart
;
365 Hob
->ResourceLength
= NumberOfBytes
;
369 Builds a GUID HOB with a certain data length.
371 This function builds a customized HOB tagged with a GUID for identification
372 and returns the start address of GUID HOB data so that caller can fill the customized data.
373 The HOB Header and Name field is already stripped.
374 If Guid is NULL, then ASSERT().
375 If there is no additional space for HOB creation, then ASSERT().
376 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
378 @param Guid The GUID to tag the customized HOB.
379 @param DataLength The size of the data payload for the GUID HOB.
381 @return The start address of GUID HOB data.
387 IN CONST EFI_GUID
*Guid
,
391 EFI_HOB_GUID_TYPE
*Hob
;
394 // Make sure that data length is not too long.
396 ASSERT (DataLength
<= (0xffff - sizeof (EFI_HOB_GUID_TYPE
)));
398 Hob
= CreateHob (EFI_HOB_TYPE_GUID_EXTENSION
, (UINT16
)(sizeof (EFI_HOB_GUID_TYPE
) + DataLength
));
399 CopyGuid (&Hob
->Name
, Guid
);
404 Copies a data buffer to a newly-built HOB.
406 This function builds a customized HOB tagged with a GUID for identification,
407 copies the input data to the HOB data field and returns the start address of the GUID HOB data.
408 The HOB Header and Name field is already stripped.
409 If Guid is NULL, then ASSERT().
410 If Data is NULL and DataLength > 0, then ASSERT().
411 If there is no additional space for HOB creation, then ASSERT().
412 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
414 @param Guid The GUID to tag the customized HOB.
415 @param Data The data to be copied into the data field of the GUID HOB.
416 @param DataLength The size of the data payload for the GUID HOB.
418 @return The start address of GUID HOB data.
424 IN CONST EFI_GUID
*Guid
,
431 ASSERT (Data
!= NULL
|| DataLength
== 0);
433 HobData
= BuildGuidHob (Guid
, DataLength
);
435 return CopyMem (HobData
, Data
, DataLength
);
439 Builds a Firmware Volume HOB.
441 This function builds a Firmware Volume HOB.
442 If there is no additional space for HOB creation, then ASSERT().
444 @param BaseAddress The base address of the Firmware Volume.
445 @param Length The size of the Firmware Volume in bytes.
451 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
455 EFI_HOB_FIRMWARE_VOLUME
*Hob
;
457 Hob
= CreateHob (EFI_HOB_TYPE_FV
, sizeof (EFI_HOB_FIRMWARE_VOLUME
));
459 Hob
->BaseAddress
= BaseAddress
;
460 Hob
->Length
= Length
;
464 Builds a EFI_HOB_TYPE_FV2 HOB.
466 This function builds a EFI_HOB_TYPE_FV2 HOB.
467 If there is no additional space for HOB creation, then ASSERT().
469 @param BaseAddress The base address of the Firmware Volume.
470 @param Length The size of the Firmware Volume in bytes.
471 @param FvName The name of the Firmware Volume.
472 @param FileName The name of the file.
478 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
480 IN CONST EFI_GUID
*FvName
,
481 IN CONST EFI_GUID
*FileName
484 EFI_HOB_FIRMWARE_VOLUME2
*Hob
;
486 Hob
= CreateHob (EFI_HOB_TYPE_FV2
, sizeof (EFI_HOB_FIRMWARE_VOLUME2
));
488 Hob
->BaseAddress
= BaseAddress
;
489 Hob
->Length
= Length
;
490 CopyGuid (&Hob
->FvName
, FvName
);
491 CopyGuid (&Hob
->FileName
, FileName
);
495 Builds a HOB for the CPU.
497 This function builds a HOB for the CPU.
498 If there is no additional space for HOB creation, then ASSERT().
500 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
501 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
507 IN UINT8 SizeOfMemorySpace
,
508 IN UINT8 SizeOfIoSpace
513 Hob
= CreateHob (EFI_HOB_TYPE_CPU
, sizeof (EFI_HOB_CPU
));
515 Hob
->SizeOfMemorySpace
= SizeOfMemorySpace
;
516 Hob
->SizeOfIoSpace
= SizeOfIoSpace
;
519 // Zero the reserved space to match HOB spec
521 ZeroMem (Hob
->Reserved
, sizeof (Hob
->Reserved
));
525 Builds a HOB for the memory allocation.
527 This function builds a HOB for the memory allocation.
528 If there is no additional space for HOB creation, then ASSERT().
530 @param BaseAddress The 64 bit physical address of the memory.
531 @param Length The length of the memory allocation in bytes.
532 @param MemoryType Type of memory allocated by this HOB.
537 BuildMemoryAllocationHob (
538 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
540 IN EFI_MEMORY_TYPE MemoryType
543 EFI_HOB_MEMORY_ALLOCATION
*Hob
;
546 ((BaseAddress
& (EFI_PAGE_SIZE
- 1)) == 0) &&
547 ((Length
& (EFI_PAGE_SIZE
- 1)) == 0)
550 Hob
= CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION
));
552 ZeroMem (&(Hob
->AllocDescriptor
.Name
), sizeof (EFI_GUID
));
553 Hob
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
554 Hob
->AllocDescriptor
.MemoryLength
= Length
;
555 Hob
->AllocDescriptor
.MemoryType
= MemoryType
;
557 // Zero the reserved space to match HOB spec
559 ZeroMem (Hob
->AllocDescriptor
.Reserved
, sizeof (Hob
->AllocDescriptor
.Reserved
));
563 Builds a HOB that describes a chunk of system memory with Owner GUID.
565 This function builds a HOB that describes a chunk of system memory.
566 If there is no additional space for HOB creation, then ASSERT().
568 @param ResourceType The type of resource described by this HOB.
569 @param ResourceAttribute The resource attributes of the memory described by this HOB.
570 @param PhysicalStart The 64 bit physical address of memory described by this HOB.
571 @param NumberOfBytes The length of the memory described by this HOB in bytes.
572 @param OwnerGUID GUID for the owner of this resource.
577 BuildResourceDescriptorWithOwnerHob (
578 IN EFI_RESOURCE_TYPE ResourceType
,
579 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute
,
580 IN EFI_PHYSICAL_ADDRESS PhysicalStart
,
581 IN UINT64 NumberOfBytes
,
582 IN EFI_GUID
*OwnerGUID
589 Builds a Capsule Volume HOB.
591 This function builds a Capsule Volume HOB.
592 If the platform does not support Capsule Volume HOBs, then ASSERT().
593 If there is no additional space for HOB creation, then ASSERT().
595 @param BaseAddress The base address of the Capsule Volume.
596 @param Length The size of the Capsule Volume in bytes.
602 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
610 Builds a HOB for the BSP store.
612 This function builds a HOB for BSP store.
613 If there is no additional space for HOB creation, then ASSERT().
615 @param BaseAddress The 64 bit physical address of the BSP.
616 @param Length The length of the BSP store in bytes.
617 @param MemoryType Type of memory allocated by this HOB.
623 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
625 IN EFI_MEMORY_TYPE MemoryType
632 Builds a HOB for the Stack.
634 This function builds a HOB for the stack.
635 If there is no additional space for HOB creation, then ASSERT().
637 @param BaseAddress The 64 bit physical address of the Stack.
638 @param Length The length of the stack in bytes.
644 IN EFI_PHYSICAL_ADDRESS BaseAddress
,