3 Copyright (c) 2010, Apple Inc. All rights reserved.<BR>
4 Copyright (c) 2017 - 2020, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Library/BaseLib.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/HobLib.h>
16 #include <Library/PcdLib.h>
21 Returns the pointer to the HOB list.
23 This function returns the pointer to first HOB in the list.
25 @return The pointer to the HOB list.
34 ASSERT (mHobList
!= NULL
);
40 Build a Handoff Information Table HOB
42 This function initialize a HOB region from EfiMemoryBegin with length
43 EfiMemoryLength. And EfiFreeMemoryBottom and EfiFreeMemoryTop should
44 be inside the HOB region.
46 @param[in] EfiMemoryBegin Total memory start address
47 @param[in] EfiMemoryLength Total memory length reported in handoff HOB.
48 @param[in] EfiFreeMemoryBottom Free memory start address
49 @param[in] EfiFreeMemoryTop Free memory end address.
51 @return The pointer to the handoff HOB table.
54 EFI_HOB_HANDOFF_INFO_TABLE
*
57 IN VOID
*EfiMemoryBegin
,
58 IN UINTN EfiMemoryLength
,
59 IN VOID
*EfiFreeMemoryBottom
,
60 IN VOID
*EfiFreeMemoryTop
63 EFI_HOB_HANDOFF_INFO_TABLE
*Hob
;
64 EFI_HOB_GENERIC_HEADER
*HobEnd
;
66 Hob
= EfiFreeMemoryBottom
;
67 HobEnd
= (EFI_HOB_GENERIC_HEADER
*)(Hob
+1);
69 Hob
->Header
.HobType
= EFI_HOB_TYPE_HANDOFF
;
70 Hob
->Header
.HobLength
= sizeof(EFI_HOB_HANDOFF_INFO_TABLE
);
71 Hob
->Header
.Reserved
= 0;
73 HobEnd
->HobType
= EFI_HOB_TYPE_END_OF_HOB_LIST
;
74 HobEnd
->HobLength
= sizeof(EFI_HOB_GENERIC_HEADER
);
77 Hob
->Version
= EFI_HOB_HANDOFF_TABLE_VERSION
;
78 Hob
->BootMode
= BOOT_WITH_FULL_CONFIGURATION
;
80 Hob
->EfiMemoryTop
= (UINTN
)EfiMemoryBegin
+ EfiMemoryLength
;
81 Hob
->EfiMemoryBottom
= (UINTN
)EfiMemoryBegin
;
82 Hob
->EfiFreeMemoryTop
= (UINTN
)EfiFreeMemoryTop
;
83 Hob
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)(HobEnd
+1);
84 Hob
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)HobEnd
;
91 Add a new HOB to the HOB List.
93 @param HobType Type of the new HOB.
94 @param HobLength Length of the new HOB to allocate.
96 @return NULL if there is no space to create a hob.
97 @return The address point to the new created hob.
107 EFI_HOB_HANDOFF_INFO_TABLE
*HandOffHob
;
108 EFI_HOB_GENERIC_HEADER
*HobEnd
;
109 EFI_PHYSICAL_ADDRESS FreeMemory
;
112 HandOffHob
= GetHobList ();
114 HobLength
= (UINT16
)((HobLength
+ 0x7) & (~0x7));
116 FreeMemory
= HandOffHob
->EfiFreeMemoryTop
- HandOffHob
->EfiFreeMemoryBottom
;
118 if (FreeMemory
< HobLength
) {
122 Hob
= (VOID
*) (UINTN
) HandOffHob
->EfiEndOfHobList
;
123 ((EFI_HOB_GENERIC_HEADER
*) Hob
)->HobType
= HobType
;
124 ((EFI_HOB_GENERIC_HEADER
*) Hob
)->HobLength
= HobLength
;
125 ((EFI_HOB_GENERIC_HEADER
*) Hob
)->Reserved
= 0;
127 HobEnd
= (EFI_HOB_GENERIC_HEADER
*) ((UINTN
)Hob
+ HobLength
);
128 HandOffHob
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HobEnd
;
130 HobEnd
->HobType
= EFI_HOB_TYPE_END_OF_HOB_LIST
;
131 HobEnd
->HobLength
= sizeof(EFI_HOB_GENERIC_HEADER
);
132 HobEnd
->Reserved
= 0;
134 HandOffHob
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HobEnd
;
140 Builds a HOB that describes a chunk of system memory.
142 This function builds a HOB that describes a chunk of system memory.
143 If there is no additional space for HOB creation, then ASSERT().
145 @param ResourceType The type of resource described by this HOB.
146 @param ResourceAttribute The resource attributes of the memory described by this HOB.
147 @param PhysicalStart The 64 bit physical address of memory described by this HOB.
148 @param NumberOfBytes The length of the memory described by this HOB in bytes.
153 BuildResourceDescriptorHob (
154 IN EFI_RESOURCE_TYPE ResourceType
,
155 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute
,
156 IN EFI_PHYSICAL_ADDRESS PhysicalStart
,
157 IN UINT64 NumberOfBytes
160 EFI_HOB_RESOURCE_DESCRIPTOR
*Hob
;
162 Hob
= CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR
));
165 Hob
->ResourceType
= ResourceType
;
166 Hob
->ResourceAttribute
= ResourceAttribute
;
167 Hob
->PhysicalStart
= PhysicalStart
;
168 Hob
->ResourceLength
= NumberOfBytes
;
174 IN EFI_PHYSICAL_ADDRESS PhysicalStart
,
175 IN UINT64 NumberOfBytes
,
176 IN EFI_RESOURCE_ATTRIBUTE_TYPE
*ResourceAttribute
180 EFI_RESOURCE_ATTRIBUTE_TYPE Resource
;
182 BuildFvHob (PhysicalStart
, NumberOfBytes
);
184 if (ResourceAttribute
== NULL
) {
185 Resource
= (EFI_RESOURCE_ATTRIBUTE_PRESENT
|
186 EFI_RESOURCE_ATTRIBUTE_INITIALIZED
|
187 EFI_RESOURCE_ATTRIBUTE_TESTED
|
188 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE
);
190 Resource
= *ResourceAttribute
;
193 BuildResourceDescriptorHob (EFI_RESOURCE_FIRMWARE_DEVICE
, Resource
, PhysicalStart
, NumberOfBytes
);
197 Returns the next instance of a HOB type from the starting HOB.
199 This function searches the first instance of a HOB type from the starting HOB pointer.
200 If there does not exist such HOB type from the starting HOB pointer, it will return NULL.
201 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
202 unconditionally: it returns HobStart back if HobStart itself meets the requirement;
203 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
204 If HobStart is NULL, then ASSERT().
206 @param Type The HOB type to return.
207 @param HobStart The starting HOB pointer to search from.
209 @return The next instance of a HOB type from the starting HOB.
216 IN CONST VOID
*HobStart
219 EFI_PEI_HOB_POINTERS Hob
;
221 ASSERT (HobStart
!= NULL
);
223 Hob
.Raw
= (UINT8
*) HobStart
;
225 // Parse the HOB list until end of list or matching type is found.
227 while (!END_OF_HOB_LIST (Hob
)) {
228 if (Hob
.Header
->HobType
== Type
) {
231 Hob
.Raw
= GET_NEXT_HOB (Hob
);
239 Returns the first instance of a HOB type among the whole HOB list.
241 This function searches the first instance of a HOB type among the whole HOB list.
242 If there does not exist such HOB type in the HOB list, it will return NULL.
244 @param Type The HOB type to return.
246 @return The next instance of a HOB type from the starting HOB.
257 HobList
= GetHobList ();
258 return GetNextHob (Type
, HobList
);
263 This function searches the first instance of a HOB from the starting HOB pointer.
264 Such HOB should satisfy two conditions:
265 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
266 If there does not exist such HOB from the starting HOB pointer, it will return NULL.
267 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
268 to extract the data section and its size info respectively.
269 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
270 unconditionally: it returns HobStart back if HobStart itself meets the requirement;
271 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
272 If Guid is NULL, then ASSERT().
273 If HobStart is NULL, then ASSERT().
275 @param Guid The GUID to match with in the HOB list.
276 @param HobStart A pointer to a Guid.
278 @return The next instance of the matched GUID HOB from the starting HOB.
284 IN CONST EFI_GUID
*Guid
,
285 IN CONST VOID
*HobStart
287 EFI_PEI_HOB_POINTERS GuidHob
;
289 GuidHob
.Raw
= (UINT8
*) HobStart
;
290 while ((GuidHob
.Raw
= GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION
, GuidHob
.Raw
)) != NULL
) {
291 if (CompareGuid (Guid
, &GuidHob
.Guid
->Name
)) {
294 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
301 This function searches the first instance of a HOB among the whole HOB list.
302 Such HOB should satisfy two conditions:
303 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
304 If there does not exist such HOB from the starting HOB pointer, it will return NULL.
305 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
306 to extract the data section and its size info respectively.
307 If Guid is NULL, then ASSERT().
309 @param Guid The GUID to match with in the HOB list.
311 @return The first instance of the matched GUID HOB among the whole HOB list.
317 IN CONST EFI_GUID
*Guid
322 HobList
= GetHobList ();
323 return GetNextGuidHob (Guid
, HobList
);
330 Builds a HOB for a loaded PE32 module.
332 This function builds a HOB for a loaded PE32 module.
333 It can only be invoked during PEI phase;
334 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
335 If ModuleName is NULL, then ASSERT().
336 If there is no additional space for HOB creation, then ASSERT().
338 @param ModuleName The GUID File Name of the module.
339 @param MemoryAllocationModule The 64 bit physical address of the module.
340 @param ModuleLength The length of the module in bytes.
341 @param EntryPoint The 64 bit physical address of the module entry point.
347 IN CONST EFI_GUID
*ModuleName
,
348 IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule
,
349 IN UINT64 ModuleLength
,
350 IN EFI_PHYSICAL_ADDRESS EntryPoint
353 EFI_HOB_MEMORY_ALLOCATION_MODULE
*Hob
;
355 ASSERT (((MemoryAllocationModule
& (EFI_PAGE_SIZE
- 1)) == 0) &&
356 ((ModuleLength
& (EFI_PAGE_SIZE
- 1)) == 0));
358 Hob
= CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE
));
360 CopyGuid (&(Hob
->MemoryAllocationHeader
.Name
), &gEfiHobMemoryAllocModuleGuid
);
361 Hob
->MemoryAllocationHeader
.MemoryBaseAddress
= MemoryAllocationModule
;
362 Hob
->MemoryAllocationHeader
.MemoryLength
= ModuleLength
;
363 Hob
->MemoryAllocationHeader
.MemoryType
= EfiBootServicesCode
;
366 // Zero the reserved space to match HOB spec
368 ZeroMem (Hob
->MemoryAllocationHeader
.Reserved
, sizeof (Hob
->MemoryAllocationHeader
.Reserved
));
370 CopyGuid (&Hob
->ModuleName
, ModuleName
);
371 Hob
->EntryPoint
= EntryPoint
;
375 Builds a GUID HOB with a certain data length.
377 This function builds a customized HOB tagged with a GUID for identification
378 and returns the start address of GUID HOB data so that caller can fill the customized data.
379 The HOB Header and Name field is already stripped.
380 It can only be invoked during PEI phase;
381 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
382 If Guid is NULL, then ASSERT().
383 If there is no additional space for HOB creation, then ASSERT().
384 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
386 @param Guid The GUID to tag the customized HOB.
387 @param DataLength The size of the data payload for the GUID HOB.
389 @return The start address of GUID HOB data.
395 IN CONST EFI_GUID
*Guid
,
399 EFI_HOB_GUID_TYPE
*Hob
;
402 // Make sure that data length is not too long.
404 ASSERT (DataLength
<= (0xffff - sizeof (EFI_HOB_GUID_TYPE
)));
406 Hob
= CreateHob (EFI_HOB_TYPE_GUID_EXTENSION
, (UINT16
) (sizeof (EFI_HOB_GUID_TYPE
) + DataLength
));
407 CopyGuid (&Hob
->Name
, Guid
);
413 Copies a data buffer to a newly-built HOB.
415 This function builds a customized HOB tagged with a GUID for identification,
416 copies the input data to the HOB data field and returns the start address of the GUID HOB data.
417 The HOB Header and Name field is already stripped.
418 It can only be invoked during PEI phase;
419 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
420 If Guid is NULL, then ASSERT().
421 If Data is NULL and DataLength > 0, then ASSERT().
422 If there is no additional space for HOB creation, then ASSERT().
423 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
425 @param Guid The GUID to tag the customized HOB.
426 @param Data The data to be copied into the data field of the GUID HOB.
427 @param DataLength The size of the data payload for the GUID HOB.
429 @return The start address of GUID HOB data.
435 IN CONST EFI_GUID
*Guid
,
442 ASSERT (Data
!= NULL
|| DataLength
== 0);
444 HobData
= BuildGuidHob (Guid
, DataLength
);
446 return CopyMem (HobData
, Data
, DataLength
);
451 Builds a Firmware Volume HOB.
453 This function builds a Firmware Volume HOB.
454 It can only be invoked during PEI phase;
455 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
456 If there is no additional space for HOB creation, then ASSERT().
458 @param BaseAddress The base address of the Firmware Volume.
459 @param Length The size of the Firmware Volume in bytes.
465 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
469 EFI_HOB_FIRMWARE_VOLUME
*Hob
;
471 Hob
= CreateHob (EFI_HOB_TYPE_FV
, sizeof (EFI_HOB_FIRMWARE_VOLUME
));
473 Hob
->BaseAddress
= BaseAddress
;
474 Hob
->Length
= Length
;
479 Builds a EFI_HOB_TYPE_FV2 HOB.
481 This function builds a EFI_HOB_TYPE_FV2 HOB.
482 It can only be invoked during PEI phase;
483 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
484 If there is no additional space for HOB creation, then ASSERT().
486 @param BaseAddress The base address of the Firmware Volume.
487 @param Length The size of the Firmware Volume in bytes.
488 @param FvName The name of the Firmware Volume.
489 @param FileName The name of the file.
495 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
497 IN CONST EFI_GUID
*FvName
,
498 IN CONST EFI_GUID
*FileName
501 EFI_HOB_FIRMWARE_VOLUME2
*Hob
;
503 Hob
= CreateHob (EFI_HOB_TYPE_FV2
, sizeof (EFI_HOB_FIRMWARE_VOLUME2
));
505 Hob
->BaseAddress
= BaseAddress
;
506 Hob
->Length
= Length
;
507 CopyGuid (&Hob
->FvName
, FvName
);
508 CopyGuid (&Hob
->FileName
, FileName
);
512 Builds a EFI_HOB_TYPE_FV3 HOB.
514 This function builds a EFI_HOB_TYPE_FV3 HOB.
515 It can only be invoked during PEI phase;
516 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
518 If there is no additional space for HOB creation, then ASSERT().
520 @param BaseAddress The base address of the Firmware Volume.
521 @param Length The size of the Firmware Volume in bytes.
522 @param AuthenticationStatus The authentication status.
523 @param ExtractedFv TRUE if the FV was extracted as a file within
524 another firmware volume. FALSE otherwise.
525 @param FvName The name of the Firmware Volume.
526 Valid only if IsExtractedFv is TRUE.
527 @param FileName The name of the file.
528 Valid only if IsExtractedFv is TRUE.
534 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
536 IN UINT32 AuthenticationStatus
,
537 IN BOOLEAN ExtractedFv
,
538 IN CONST EFI_GUID
*FvName
, OPTIONAL
539 IN CONST EFI_GUID
*FileName OPTIONAL
542 EFI_HOB_FIRMWARE_VOLUME3
*Hob
;
544 Hob
= CreateHob (EFI_HOB_TYPE_FV3
, sizeof (EFI_HOB_FIRMWARE_VOLUME3
));
546 Hob
->BaseAddress
= BaseAddress
;
547 Hob
->Length
= Length
;
548 Hob
->AuthenticationStatus
= AuthenticationStatus
;
549 Hob
->ExtractedFv
= ExtractedFv
;
551 CopyGuid (&Hob
->FvName
, FvName
);
552 CopyGuid (&Hob
->FileName
, FileName
);
558 Builds a HOB for the CPU.
560 This function builds a HOB for the CPU.
561 It can only be invoked during PEI phase;
562 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
563 If there is no additional space for HOB creation, then ASSERT().
565 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
566 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
572 IN UINT8 SizeOfMemorySpace
,
573 IN UINT8 SizeOfIoSpace
578 Hob
= CreateHob (EFI_HOB_TYPE_CPU
, sizeof (EFI_HOB_CPU
));
580 Hob
->SizeOfMemorySpace
= SizeOfMemorySpace
;
581 Hob
->SizeOfIoSpace
= SizeOfIoSpace
;
584 // Zero the reserved space to match HOB spec
586 ZeroMem (Hob
->Reserved
, sizeof (Hob
->Reserved
));
591 Builds a HOB for the Stack.
593 This function builds a HOB for the stack.
594 It can only be invoked during PEI phase;
595 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
596 If there is no additional space for HOB creation, then ASSERT().
598 @param BaseAddress The 64 bit physical address of the Stack.
599 @param Length The length of the stack in bytes.
605 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
609 EFI_HOB_MEMORY_ALLOCATION_STACK
*Hob
;
611 ASSERT (((BaseAddress
& (EFI_PAGE_SIZE
- 1)) == 0) &&
612 ((Length
& (EFI_PAGE_SIZE
- 1)) == 0));
614 Hob
= CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION_STACK
));
616 CopyGuid (&(Hob
->AllocDescriptor
.Name
), &gEfiHobMemoryAllocStackGuid
);
617 Hob
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
618 Hob
->AllocDescriptor
.MemoryLength
= Length
;
619 Hob
->AllocDescriptor
.MemoryType
= EfiBootServicesData
;
622 // Zero the reserved space to match HOB spec
624 ZeroMem (Hob
->AllocDescriptor
.Reserved
, sizeof (Hob
->AllocDescriptor
.Reserved
));
629 Update the Stack Hob if the stack has been moved
631 @param BaseAddress The 64 bit physical address of the Stack.
632 @param Length The length of the stack in bytes.
638 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
642 EFI_PEI_HOB_POINTERS Hob
;
644 Hob
.Raw
= GetHobList ();
645 while ((Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
)) != NULL
) {
646 if (CompareGuid (&gEfiHobMemoryAllocStackGuid
, &(Hob
.MemoryAllocationStack
->AllocDescriptor
.Name
))) {
648 // Build a new memory allocation HOB with old stack info with EfiConventionalMemory type
649 // to be reclaimed by DXE core.
651 BuildMemoryAllocationHob (
652 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryBaseAddress
,
653 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryLength
,
654 EfiConventionalMemory
657 // Update the BSP Stack Hob to reflect the new stack info.
659 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
660 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryLength
= Length
;
663 Hob
.Raw
= GET_NEXT_HOB (Hob
);
670 Builds a HOB for the memory allocation.
672 This function builds a HOB for the memory allocation.
673 It can only be invoked during PEI phase;
674 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
675 If there is no additional space for HOB creation, then ASSERT().
677 @param BaseAddress The 64 bit physical address of the memory.
678 @param Length The length of the memory allocation in bytes.
679 @param MemoryType Type of memory allocated by this HOB.
684 BuildMemoryAllocationHob (
685 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
687 IN EFI_MEMORY_TYPE MemoryType
690 EFI_HOB_MEMORY_ALLOCATION
*Hob
;
692 ASSERT (((BaseAddress
& (EFI_PAGE_SIZE
- 1)) == 0) &&
693 ((Length
& (EFI_PAGE_SIZE
- 1)) == 0));
695 Hob
= CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION
));
697 ZeroMem (&(Hob
->AllocDescriptor
.Name
), sizeof (EFI_GUID
));
698 Hob
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
699 Hob
->AllocDescriptor
.MemoryLength
= Length
;
700 Hob
->AllocDescriptor
.MemoryType
= MemoryType
;
702 // Zero the reserved space to match HOB spec
704 ZeroMem (Hob
->AllocDescriptor
.Reserved
, sizeof (Hob
->AllocDescriptor
.Reserved
));