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 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php.
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include <Library/HobLib.h>
21 #include <Library/DebugLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Library/MmServicesTableLib.h>
26 // Cache copy of HobList pointer.
28 STATIC VOID
*gHobList
= NULL
;
31 The constructor function caches the pointer to HOB list.
33 The constructor function gets the start address of HOB list from system configuration table.
34 It will ASSERT() if that operation fails and it will always return EFI_SUCCESS.
36 @param ImageHandle The firmware allocated handle for the image.
37 @param MmSystemTable A pointer to the MM System Table.
39 @retval EFI_SUCCESS The constructor successfully gets HobList.
40 @retval Other value The constructor can't get HobList.
46 IN EFI_HANDLE ImageHandle
,
47 IN EFI_MM_SYSTEM_TABLE
*MmSystemTable
52 for (Index
= 0; Index
< gMmst
->NumberOfTableEntries
; Index
++) {
53 if (CompareGuid (&gEfiHobListGuid
, &gMmst
->MmConfigurationTable
[Index
].VendorGuid
)) {
54 gHobList
= gMmst
->MmConfigurationTable
[Index
].VendorTable
;
62 Returns the pointer to the HOB list.
64 This function returns the pointer to first HOB in the list.
65 If the pointer to the HOB list is NULL, then ASSERT().
67 @return The pointer to the HOB list.
78 if (gHobList
== NULL
) {
79 for (Index
= 0; Index
< gMmst
->NumberOfTableEntries
; Index
++) {
80 if (CompareGuid (&gEfiHobListGuid
, &gMmst
->MmConfigurationTable
[Index
].VendorGuid
)) {
81 gHobList
= gMmst
->MmConfigurationTable
[Index
].VendorTable
;
86 ASSERT (gHobList
!= NULL
);
91 Returns the next instance of a HOB type from the starting HOB.
93 This function searches the first instance of a HOB type from the starting HOB pointer.
94 If there does not exist such HOB type from the starting HOB pointer, it will return NULL.
95 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
96 unconditionally: it returns HobStart back if HobStart itself meets the requirement;
97 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
99 If HobStart is NULL, then ASSERT().
101 @param Type The HOB type to return.
102 @param HobStart The starting HOB pointer to search from.
104 @return The next instance of a HOB type from the starting HOB.
111 IN CONST VOID
*HobStart
114 EFI_PEI_HOB_POINTERS Hob
;
116 ASSERT (HobStart
!= NULL
);
118 Hob
.Raw
= (UINT8
*) HobStart
;
120 // Parse the HOB list until end of list or matching type is found.
122 while (!END_OF_HOB_LIST (Hob
)) {
123 if (Hob
.Header
->HobType
== Type
) {
126 Hob
.Raw
= GET_NEXT_HOB (Hob
);
132 Returns the first instance of a HOB type among the whole HOB list.
134 This function searches the first instance of a HOB type among the whole HOB list.
135 If there does not exist such HOB type in the HOB list, it will return NULL.
137 If the pointer to the HOB list is NULL, then ASSERT().
139 @param Type The HOB type to return.
141 @return The next instance of a HOB type from the starting HOB.
152 HobList
= GetHobList ();
153 return GetNextHob (Type
, HobList
);
157 Returns the next instance of the matched GUID HOB from the starting HOB.
159 This function searches the first instance of a HOB from the starting HOB pointer.
160 Such HOB should satisfy two conditions:
161 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION, and its GUID Name equals to the input Guid.
162 If such a HOB from the starting HOB pointer does not exist, it will return NULL.
163 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
164 to extract the data section and its size information, respectively.
165 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
166 unconditionally: it returns HobStart back if HobStart itself meets the requirement;
167 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
169 If Guid is NULL, then ASSERT().
170 If HobStart is NULL, then ASSERT().
172 @param Guid The GUID to match with in the HOB list.
173 @param HobStart A pointer to a Guid.
175 @return The next instance of the matched GUID HOB from the starting HOB.
181 IN CONST EFI_GUID
*Guid
,
182 IN CONST VOID
*HobStart
185 EFI_PEI_HOB_POINTERS GuidHob
;
187 GuidHob
.Raw
= (UINT8
*) HobStart
;
188 while ((GuidHob
.Raw
= GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION
, GuidHob
.Raw
)) != NULL
) {
189 if (CompareGuid (Guid
, &GuidHob
.Guid
->Name
)) {
192 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
;
315 ASSERT (((MemoryAllocationModule
& (EFI_PAGE_SIZE
- 1)) == 0) &&
316 ((ModuleLength
& (EFI_PAGE_SIZE
- 1)) == 0));
318 Hob
= CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE
));
320 CopyGuid (&(Hob
->MemoryAllocationHeader
.Name
), &gEfiHobMemoryAllocModuleGuid
);
321 Hob
->MemoryAllocationHeader
.MemoryBaseAddress
= MemoryAllocationModule
;
322 Hob
->MemoryAllocationHeader
.MemoryLength
= ModuleLength
;
323 Hob
->MemoryAllocationHeader
.MemoryType
= EfiBootServicesCode
;
326 // Zero the reserved space to match HOB spec
328 ZeroMem (Hob
->MemoryAllocationHeader
.Reserved
, sizeof (Hob
->MemoryAllocationHeader
.Reserved
));
330 CopyGuid (&Hob
->ModuleName
, ModuleName
);
331 Hob
->EntryPoint
= EntryPoint
;
335 Builds a HOB that describes a chunk of system memory.
337 This function builds a HOB that describes a chunk of system memory.
338 If there is no additional space for HOB creation, then ASSERT().
340 @param ResourceType The type of resource described by this HOB.
341 @param ResourceAttribute The resource attributes of the memory described by this HOB.
342 @param PhysicalStart The 64 bit physical address of memory described by this HOB.
343 @param NumberOfBytes The length of the memory described by this HOB in bytes.
348 BuildResourceDescriptorHob (
349 IN EFI_RESOURCE_TYPE ResourceType
,
350 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute
,
351 IN EFI_PHYSICAL_ADDRESS PhysicalStart
,
352 IN UINT64 NumberOfBytes
355 EFI_HOB_RESOURCE_DESCRIPTOR
*Hob
;
357 Hob
= CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR
));
360 Hob
->ResourceType
= ResourceType
;
361 Hob
->ResourceAttribute
= ResourceAttribute
;
362 Hob
->PhysicalStart
= PhysicalStart
;
363 Hob
->ResourceLength
= NumberOfBytes
;
367 Builds a GUID HOB with a certain data length.
369 This function builds a customized HOB tagged with a GUID for identification
370 and returns the start address of GUID HOB data so that caller can fill the customized data.
371 The HOB Header and Name field is already stripped.
372 If Guid is NULL, then ASSERT().
373 If there is no additional space for HOB creation, then ASSERT().
374 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
376 @param Guid The GUID to tag the customized HOB.
377 @param DataLength The size of the data payload for the GUID HOB.
379 @return The start address of GUID HOB data.
385 IN CONST EFI_GUID
*Guid
,
389 EFI_HOB_GUID_TYPE
*Hob
;
392 // Make sure that data length is not too long.
394 ASSERT (DataLength
<= (0xffff - sizeof (EFI_HOB_GUID_TYPE
)));
396 Hob
= CreateHob (EFI_HOB_TYPE_GUID_EXTENSION
, (UINT16
) (sizeof (EFI_HOB_GUID_TYPE
) + DataLength
));
397 CopyGuid (&Hob
->Name
, Guid
);
403 Copies a data buffer to a newly-built HOB.
405 This function builds a customized HOB tagged with a GUID for identification,
406 copies the input data to the HOB data field and returns the start address of the GUID HOB data.
407 The HOB Header and Name field is already stripped.
408 If Guid is NULL, then ASSERT().
409 If Data is NULL and DataLength > 0, then ASSERT().
410 If there is no additional space for HOB creation, then ASSERT().
411 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
413 @param Guid The GUID to tag the customized HOB.
414 @param Data The data to be copied into the data field of the GUID HOB.
415 @param DataLength The size of the data payload for the GUID HOB.
417 @return The start address of GUID HOB data.
423 IN CONST EFI_GUID
*Guid
,
430 ASSERT (Data
!= NULL
|| DataLength
== 0);
432 HobData
= BuildGuidHob (Guid
, DataLength
);
434 return CopyMem (HobData
, Data
, DataLength
);
438 Builds a Firmware Volume HOB.
440 This function builds a Firmware Volume HOB.
441 If there is no additional space for HOB creation, then ASSERT().
443 @param BaseAddress The base address of the Firmware Volume.
444 @param Length The size of the Firmware Volume in bytes.
450 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
454 EFI_HOB_FIRMWARE_VOLUME
*Hob
;
456 Hob
= CreateHob (EFI_HOB_TYPE_FV
, sizeof (EFI_HOB_FIRMWARE_VOLUME
));
458 Hob
->BaseAddress
= BaseAddress
;
459 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
);
496 Builds a HOB for the CPU.
498 This function builds a HOB for the CPU.
499 If there is no additional space for HOB creation, then ASSERT().
501 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
502 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
508 IN UINT8 SizeOfMemorySpace
,
509 IN UINT8 SizeOfIoSpace
514 Hob
= CreateHob (EFI_HOB_TYPE_CPU
, sizeof (EFI_HOB_CPU
));
516 Hob
->SizeOfMemorySpace
= SizeOfMemorySpace
;
517 Hob
->SizeOfIoSpace
= SizeOfIoSpace
;
520 // Zero the reserved space to match HOB spec
522 ZeroMem (Hob
->Reserved
, sizeof (Hob
->Reserved
));
526 Builds a HOB for the memory allocation.
528 This function builds a HOB for the memory allocation.
529 If there is no additional space for HOB creation, then ASSERT().
531 @param BaseAddress The 64 bit physical address of the memory.
532 @param Length The length of the memory allocation in bytes.
533 @param MemoryType Type of memory allocated by this HOB.
538 BuildMemoryAllocationHob (
539 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
541 IN EFI_MEMORY_TYPE MemoryType
544 EFI_HOB_MEMORY_ALLOCATION
*Hob
;
546 ASSERT (((BaseAddress
& (EFI_PAGE_SIZE
- 1)) == 0) &&
547 ((Length
& (EFI_PAGE_SIZE
- 1)) == 0));
549 Hob
= CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION
));
551 ZeroMem (&(Hob
->AllocDescriptor
.Name
), sizeof (EFI_GUID
));
552 Hob
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
553 Hob
->AllocDescriptor
.MemoryLength
= Length
;
554 Hob
->AllocDescriptor
.MemoryType
= MemoryType
;
556 // Zero the reserved space to match HOB spec
558 ZeroMem (Hob
->AllocDescriptor
.Reserved
, sizeof (Hob
->AllocDescriptor
.Reserved
));
562 Builds a HOB that describes a chunk of system memory with Owner GUID.
564 This function builds a HOB that describes a chunk of system memory.
565 If there is no additional space for HOB creation, then ASSERT().
567 @param ResourceType The type of resource described by this HOB.
568 @param ResourceAttribute The resource attributes of the memory described by this HOB.
569 @param PhysicalStart The 64 bit physical address of memory described by this HOB.
570 @param NumberOfBytes The length of the memory described by this HOB in bytes.
571 @param OwnerGUID GUID for the owner of this resource.
576 BuildResourceDescriptorWithOwnerHob (
577 IN EFI_RESOURCE_TYPE ResourceType
,
578 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute
,
579 IN EFI_PHYSICAL_ADDRESS PhysicalStart
,
580 IN UINT64 NumberOfBytes
,
581 IN EFI_GUID
*OwnerGUID
588 Builds a Capsule Volume HOB.
590 This function builds a Capsule Volume HOB.
591 If the platform does not support Capsule Volume HOBs, then ASSERT().
592 If there is no additional space for HOB creation, then ASSERT().
594 @param BaseAddress The base address of the Capsule Volume.
595 @param Length The size of the Capsule Volume in bytes.
601 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
,