3 Copyright (c) 2010, Apple Inc. All rights reserved.<BR>
4 Copyright (c) 2017 - 2021, 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 to
43 EfiMemoryTop. And EfiFreeMemoryBottom and EfiFreeMemoryTop should
44 be inside the HOB region.
46 @param[in] EfiMemoryBottom Total memory start address
47 @param[in] EfiMemoryTop Total memory end address.
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
*EfiMemoryBottom
,
58 IN VOID
*EfiMemoryTop
,
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
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) EfiMemoryTop
;
81 Hob
->EfiMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) EfiMemoryBottom
;
82 Hob
->EfiFreeMemoryTop
= (EFI_PHYSICAL_ADDRESS
) (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
;
172 Returns the next instance of a HOB type from the starting HOB.
174 This function searches the first instance of a HOB type from the starting HOB pointer.
175 If there does not exist such HOB type from the starting HOB pointer, it will return NULL.
176 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
177 unconditionally: it returns HobStart back if HobStart itself meets the requirement;
178 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
179 If HobStart is NULL, then ASSERT().
181 @param Type The HOB type to return.
182 @param HobStart The starting HOB pointer to search from.
184 @return The next instance of a HOB type from the starting HOB.
191 IN CONST VOID
*HobStart
194 EFI_PEI_HOB_POINTERS Hob
;
196 ASSERT (HobStart
!= NULL
);
198 Hob
.Raw
= (UINT8
*) HobStart
;
200 // Parse the HOB list until end of list or matching type is found.
202 while (!END_OF_HOB_LIST (Hob
)) {
203 if (Hob
.Header
->HobType
== Type
) {
206 Hob
.Raw
= GET_NEXT_HOB (Hob
);
214 Returns the first instance of a HOB type among the whole HOB list.
216 This function searches the first instance of a HOB type among the whole HOB list.
217 If there does not exist such HOB type in the HOB list, it will return NULL.
219 @param Type The HOB type to return.
221 @return The next instance of a HOB type from the starting HOB.
232 HobList
= GetHobList ();
233 return GetNextHob (Type
, HobList
);
238 This function searches the first instance of a HOB from the starting HOB pointer.
239 Such HOB should satisfy two conditions:
240 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
241 If there does not exist such HOB from the starting HOB pointer, it will return NULL.
242 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
243 to extract the data section and its size info respectively.
244 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
245 unconditionally: it returns HobStart back if HobStart itself meets the requirement;
246 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
247 If Guid is NULL, then ASSERT().
248 If HobStart is NULL, then ASSERT().
250 @param Guid The GUID to match with in the HOB list.
251 @param HobStart A pointer to a Guid.
253 @return The next instance of the matched GUID HOB from the starting HOB.
259 IN CONST EFI_GUID
*Guid
,
260 IN CONST VOID
*HobStart
263 EFI_PEI_HOB_POINTERS GuidHob
;
265 GuidHob
.Raw
= (UINT8
*) HobStart
;
266 while ((GuidHob
.Raw
= GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION
, GuidHob
.Raw
)) != NULL
) {
267 if (CompareGuid (Guid
, &GuidHob
.Guid
->Name
)) {
270 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
277 This function searches the first instance of a HOB among the whole HOB list.
278 Such HOB should satisfy two conditions:
279 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
280 If there does not exist such HOB from the starting HOB pointer, it will return NULL.
281 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
282 to extract the data section and its size info respectively.
283 If Guid is NULL, then ASSERT().
285 @param Guid The GUID to match with in the HOB list.
287 @return The first instance of the matched GUID HOB among the whole HOB list.
293 IN CONST EFI_GUID
*Guid
298 HobList
= GetHobList ();
299 return GetNextGuidHob (Guid
, HobList
);
306 Builds a HOB for a loaded PE32 module.
308 This function builds a HOB for a loaded PE32 module.
309 It can only be invoked during PEI phase;
310 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
311 If ModuleName is NULL, then ASSERT().
312 If there is no additional space for HOB creation, then ASSERT().
314 @param ModuleName The GUID File Name of the module.
315 @param MemoryAllocationModule The 64 bit physical address of the module.
316 @param ModuleLength The length of the module in bytes.
317 @param EntryPoint The 64 bit physical address of the module entry point.
323 IN CONST EFI_GUID
*ModuleName
,
324 IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule
,
325 IN UINT64 ModuleLength
,
326 IN EFI_PHYSICAL_ADDRESS EntryPoint
329 EFI_HOB_MEMORY_ALLOCATION_MODULE
*Hob
;
331 ASSERT (((MemoryAllocationModule
& (EFI_PAGE_SIZE
- 1)) == 0) &&
332 ((ModuleLength
& (EFI_PAGE_SIZE
- 1)) == 0));
334 Hob
= CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE
));
336 CopyGuid (&(Hob
->MemoryAllocationHeader
.Name
), &gEfiHobMemoryAllocModuleGuid
);
337 Hob
->MemoryAllocationHeader
.MemoryBaseAddress
= MemoryAllocationModule
;
338 Hob
->MemoryAllocationHeader
.MemoryLength
= ModuleLength
;
339 Hob
->MemoryAllocationHeader
.MemoryType
= EfiBootServicesCode
;
342 // Zero the reserved space to match HOB spec
344 ZeroMem (Hob
->MemoryAllocationHeader
.Reserved
, sizeof (Hob
->MemoryAllocationHeader
.Reserved
));
346 CopyGuid (&Hob
->ModuleName
, ModuleName
);
347 Hob
->EntryPoint
= EntryPoint
;
351 Builds a GUID HOB with a certain data length.
353 This function builds a customized HOB tagged with a GUID for identification
354 and returns the start address of GUID HOB data so that caller can fill the customized data.
355 The HOB Header and Name field is already stripped.
356 It can only be invoked during PEI phase;
357 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
358 If Guid is NULL, then ASSERT().
359 If there is no additional space for HOB creation, then ASSERT().
360 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
362 @param Guid The GUID to tag the customized HOB.
363 @param DataLength The size of the data payload for the GUID HOB.
365 @return The start address of GUID HOB data.
371 IN CONST EFI_GUID
*Guid
,
375 EFI_HOB_GUID_TYPE
*Hob
;
378 // Make sure that data length is not too long.
380 ASSERT (DataLength
<= (0xffff - sizeof (EFI_HOB_GUID_TYPE
)));
382 Hob
= CreateHob (EFI_HOB_TYPE_GUID_EXTENSION
, (UINT16
) (sizeof (EFI_HOB_GUID_TYPE
) + DataLength
));
383 CopyGuid (&Hob
->Name
, Guid
);
389 Copies a data buffer to a newly-built HOB.
391 This function builds a customized HOB tagged with a GUID for identification,
392 copies the input data to the HOB data field and returns the start address of the GUID HOB data.
393 The HOB Header and Name field is already stripped.
394 It can only be invoked during PEI phase;
395 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
396 If Guid is NULL, then ASSERT().
397 If Data is NULL and DataLength > 0, then ASSERT().
398 If there is no additional space for HOB creation, then ASSERT().
399 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
401 @param Guid The GUID to tag the customized HOB.
402 @param Data The data to be copied into the data field of the GUID HOB.
403 @param DataLength The size of the data payload for the GUID HOB.
405 @return The start address of GUID HOB data.
411 IN CONST EFI_GUID
*Guid
,
418 ASSERT (Data
!= NULL
|| DataLength
== 0);
420 HobData
= BuildGuidHob (Guid
, DataLength
);
422 return CopyMem (HobData
, Data
, DataLength
);
427 Builds a Firmware Volume HOB.
429 This function builds a Firmware Volume HOB.
430 It can only be invoked during PEI phase;
431 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
432 If there is no additional space for HOB creation, then ASSERT().
434 @param BaseAddress The base address of the Firmware Volume.
435 @param Length The size of the Firmware Volume in bytes.
441 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
445 EFI_HOB_FIRMWARE_VOLUME
*Hob
;
447 Hob
= CreateHob (EFI_HOB_TYPE_FV
, sizeof (EFI_HOB_FIRMWARE_VOLUME
));
449 Hob
->BaseAddress
= BaseAddress
;
450 Hob
->Length
= Length
;
455 Builds a EFI_HOB_TYPE_FV2 HOB.
457 This function builds a EFI_HOB_TYPE_FV2 HOB.
458 It can only be invoked during PEI phase;
459 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
460 If there is no additional space for HOB creation, then ASSERT().
462 @param BaseAddress The base address of the Firmware Volume.
463 @param Length The size of the Firmware Volume in bytes.
464 @param FvName The name of the Firmware Volume.
465 @param FileName The name of the file.
471 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
473 IN CONST EFI_GUID
*FvName
,
474 IN CONST EFI_GUID
*FileName
477 EFI_HOB_FIRMWARE_VOLUME2
*Hob
;
479 Hob
= CreateHob (EFI_HOB_TYPE_FV2
, sizeof (EFI_HOB_FIRMWARE_VOLUME2
));
481 Hob
->BaseAddress
= BaseAddress
;
482 Hob
->Length
= Length
;
483 CopyGuid (&Hob
->FvName
, FvName
);
484 CopyGuid (&Hob
->FileName
, FileName
);
488 Builds a EFI_HOB_TYPE_FV3 HOB.
490 This function builds a EFI_HOB_TYPE_FV3 HOB.
491 It can only be invoked during PEI phase;
492 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
494 If there is no additional space for HOB creation, then ASSERT().
496 @param BaseAddress The base address of the Firmware Volume.
497 @param Length The size of the Firmware Volume in bytes.
498 @param AuthenticationStatus The authentication status.
499 @param ExtractedFv TRUE if the FV was extracted as a file within
500 another firmware volume. FALSE otherwise.
501 @param FvName The name of the Firmware Volume.
502 Valid only if IsExtractedFv is TRUE.
503 @param FileName The name of the file.
504 Valid only if IsExtractedFv is TRUE.
510 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
512 IN UINT32 AuthenticationStatus
,
513 IN BOOLEAN ExtractedFv
,
514 IN CONST EFI_GUID
*FvName OPTIONAL
,
515 IN CONST EFI_GUID
*FileName OPTIONAL
518 EFI_HOB_FIRMWARE_VOLUME3
*Hob
;
520 Hob
= CreateHob (EFI_HOB_TYPE_FV3
, sizeof (EFI_HOB_FIRMWARE_VOLUME3
));
522 Hob
->BaseAddress
= BaseAddress
;
523 Hob
->Length
= Length
;
524 Hob
->AuthenticationStatus
= AuthenticationStatus
;
525 Hob
->ExtractedFv
= ExtractedFv
;
527 CopyGuid (&Hob
->FvName
, FvName
);
528 CopyGuid (&Hob
->FileName
, FileName
);
534 Builds a HOB for the CPU.
536 This function builds a HOB for the CPU.
537 It can only be invoked during PEI phase;
538 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
539 If there is no additional space for HOB creation, then ASSERT().
541 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
542 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
548 IN UINT8 SizeOfMemorySpace
,
549 IN UINT8 SizeOfIoSpace
554 Hob
= CreateHob (EFI_HOB_TYPE_CPU
, sizeof (EFI_HOB_CPU
));
556 Hob
->SizeOfMemorySpace
= SizeOfMemorySpace
;
557 Hob
->SizeOfIoSpace
= SizeOfIoSpace
;
560 // Zero the reserved space to match HOB spec
562 ZeroMem (Hob
->Reserved
, sizeof (Hob
->Reserved
));
567 Builds a HOB for the Stack.
569 This function builds a HOB for the stack.
570 It can only be invoked during PEI phase;
571 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
572 If there is no additional space for HOB creation, then ASSERT().
574 @param BaseAddress The 64 bit physical address of the Stack.
575 @param Length The length of the stack in bytes.
581 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
585 EFI_HOB_MEMORY_ALLOCATION_STACK
*Hob
;
587 ASSERT (((BaseAddress
& (EFI_PAGE_SIZE
- 1)) == 0) &&
588 ((Length
& (EFI_PAGE_SIZE
- 1)) == 0));
590 Hob
= CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION_STACK
));
592 CopyGuid (&(Hob
->AllocDescriptor
.Name
), &gEfiHobMemoryAllocStackGuid
);
593 Hob
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
594 Hob
->AllocDescriptor
.MemoryLength
= Length
;
595 Hob
->AllocDescriptor
.MemoryType
= EfiBootServicesData
;
598 // Zero the reserved space to match HOB spec
600 ZeroMem (Hob
->AllocDescriptor
.Reserved
, sizeof (Hob
->AllocDescriptor
.Reserved
));
605 Update the Stack Hob if the stack has been moved
607 @param BaseAddress The 64 bit physical address of the Stack.
608 @param Length The length of the stack in bytes.
614 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
618 EFI_PEI_HOB_POINTERS Hob
;
620 Hob
.Raw
= GetHobList ();
621 while ((Hob
.Raw
= GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, Hob
.Raw
)) != NULL
) {
622 if (CompareGuid (&gEfiHobMemoryAllocStackGuid
, &(Hob
.MemoryAllocationStack
->AllocDescriptor
.Name
))) {
624 // Build a new memory allocation HOB with old stack info with EfiConventionalMemory type
625 // to be reclaimed by DXE core.
627 BuildMemoryAllocationHob (
628 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryBaseAddress
,
629 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryLength
,
630 EfiConventionalMemory
633 // Update the BSP Stack Hob to reflect the new stack info.
635 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
636 Hob
.MemoryAllocationStack
->AllocDescriptor
.MemoryLength
= Length
;
639 Hob
.Raw
= GET_NEXT_HOB (Hob
);
646 Builds a HOB for the memory allocation.
648 This function builds a HOB for the memory allocation.
649 It can only be invoked during PEI phase;
650 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
651 If there is no additional space for HOB creation, then ASSERT().
653 @param BaseAddress The 64 bit physical address of the memory.
654 @param Length The length of the memory allocation in bytes.
655 @param MemoryType Type of memory allocated by this HOB.
660 BuildMemoryAllocationHob (
661 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
663 IN EFI_MEMORY_TYPE MemoryType
666 EFI_HOB_MEMORY_ALLOCATION
*Hob
;
668 ASSERT (((BaseAddress
& (EFI_PAGE_SIZE
- 1)) == 0) &&
669 ((Length
& (EFI_PAGE_SIZE
- 1)) == 0));
671 Hob
= CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION
));
673 ZeroMem (&(Hob
->AllocDescriptor
.Name
), sizeof (EFI_GUID
));
674 Hob
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
675 Hob
->AllocDescriptor
.MemoryLength
= Length
;
676 Hob
->AllocDescriptor
.MemoryType
= MemoryType
;
678 // Zero the reserved space to match HOB spec
680 ZeroMem (Hob
->AllocDescriptor
.Reserved
, sizeof (Hob
->AllocDescriptor
.Reserved
));