2 Provide Hob Library functions for Pei phase.
4 Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Guid/MemoryAllocationHob.h>
19 #include <Library/HobLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/PeiServicesLib.h>
22 #include <Library/BaseMemoryLib.h>
25 Returns the pointer to the HOB list.
27 This function returns the pointer to first HOB in the list.
28 For PEI phase, the PEI service GetHobList() can be used to retrieve the pointer
29 to the HOB list. For the DXE phase, the HOB list pointer can be retrieved through
30 the EFI System Table by looking up theHOB list GUID in the System Configuration Table.
31 Since the System Configuration Table does not exist that the time the DXE Core is
32 launched, the DXE Core uses a global variable from the DXE Core Entry Point Library
33 to manage the pointer to the HOB list.
35 If the pointer to the HOB list is NULL, then ASSERT().
37 @return The pointer to the HOB list.
49 Status
= PeiServicesGetHobList (&HobList
);
50 ASSERT_EFI_ERROR (Status
);
51 ASSERT (HobList
!= NULL
);
57 Returns the next instance of a HOB type from the starting HOB.
59 This function searches the first instance of a HOB type from the starting HOB pointer.
60 If there does not exist such HOB type from the starting HOB pointer, it will return NULL.
61 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
62 unconditionally: it returns HobStart back if HobStart itself meets the requirement;
63 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
65 If HobStart is NULL, then ASSERT().
67 @param Type The HOB type to return.
68 @param HobStart The starting HOB pointer to search from.
70 @return The next instance of a HOB type from the starting HOB.
77 IN CONST VOID
*HobStart
80 EFI_PEI_HOB_POINTERS Hob
;
82 ASSERT (HobStart
!= NULL
);
84 Hob
.Raw
= (UINT8
*) HobStart
;
86 // Parse the HOB list until end of list or matching type is found.
88 while (!END_OF_HOB_LIST (Hob
)) {
89 if (Hob
.Header
->HobType
== Type
) {
92 Hob
.Raw
= GET_NEXT_HOB (Hob
);
98 Returns the first instance of a HOB type among the whole HOB list.
100 This function searches the first instance of a HOB type among the whole HOB list.
101 If there does not exist such HOB type in the HOB list, it will return NULL.
103 If the pointer to the HOB list is NULL, then ASSERT().
105 @param Type The HOB type to return.
107 @return The next instance of a HOB type from the starting HOB.
118 HobList
= GetHobList ();
119 return GetNextHob (Type
, HobList
);
123 Returns the next instance of the matched GUID HOB from the starting HOB.
125 This function searches the first instance of a HOB from the starting HOB pointer.
126 Such HOB should satisfy two conditions:
127 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
128 If there does not exist such HOB from the starting HOB pointer, it will return NULL.
129 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
130 to extract the data section and its size information, respectively.
131 In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
132 unconditionally: it returns HobStart back if HobStart itself meets the requirement;
133 caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
135 If Guid is NULL, then ASSERT().
136 If HobStart is NULL, then ASSERT().
138 @param Guid The GUID to match with in the HOB list.
139 @param HobStart A pointer to a Guid.
141 @return The next instance of the matched GUID HOB from the starting HOB.
147 IN CONST EFI_GUID
*Guid
,
148 IN CONST VOID
*HobStart
151 EFI_PEI_HOB_POINTERS GuidHob
;
153 GuidHob
.Raw
= (UINT8
*) HobStart
;
154 while ((GuidHob
.Raw
= GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION
, GuidHob
.Raw
)) != NULL
) {
155 if (CompareGuid (Guid
, &GuidHob
.Guid
->Name
)) {
158 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
164 Returns the first instance of the matched GUID HOB among the whole HOB list.
166 This function searches the first instance of a HOB among the whole HOB list.
167 Such HOB should satisfy two conditions:
168 its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
169 If there does not exist such HOB from the starting HOB pointer, it will return NULL.
170 Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
171 to extract the data section and its size information, respectively.
173 If the pointer to the HOB list is NULL, then ASSERT().
174 If Guid is NULL, then ASSERT().
176 @param Guid The GUID to match with in the HOB list.
178 @return The first instance of the matched GUID HOB among the whole HOB list.
184 IN CONST EFI_GUID
*Guid
189 HobList
= GetHobList ();
190 return GetNextGuidHob (Guid
, HobList
);
194 Get the system boot mode from the HOB list.
196 This function returns the system boot mode information from the
197 PHIT HOB in HOB list.
199 If the pointer to the HOB list is NULL, then ASSERT().
203 @return The Boot Mode.
213 EFI_BOOT_MODE BootMode
;
215 Status
= PeiServicesGetBootMode (&BootMode
);
216 ASSERT_EFI_ERROR (Status
);
222 Adds a new HOB to the HOB List.
224 This internal function enables PEIMs to create various types of HOBs.
226 @param Type Type of the new HOB.
227 @param Length Length of the new HOB to allocate.
229 @retval NULL The HOB could not be allocated.
230 @retval others The address of new HOB.
235 InternalPeiCreateHob (
243 Status
= PeiServicesCreateHob (Type
, Length
, &Hob
);
244 if (EFI_ERROR (Status
)) {
248 // Assume the process of HOB building is always successful.
250 ASSERT (Hob
!= NULL
);
255 Builds a HOB for a loaded PE32 module.
257 This function builds a HOB for a loaded PE32 module.
258 It can only be invoked during PEI phase;
259 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
261 If ModuleName is NULL, then ASSERT().
262 If there is no additional space for HOB creation, then ASSERT().
264 @param ModuleName The GUID File Name of the module.
265 @param MemoryAllocationModule The 64 bit physical address of the module.
266 @param ModuleLength The length of the module in bytes.
267 @param EntryPoint The 64 bit physical address of the module entry point.
273 IN CONST EFI_GUID
*ModuleName
,
274 IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule
,
275 IN UINT64 ModuleLength
,
276 IN EFI_PHYSICAL_ADDRESS EntryPoint
279 EFI_HOB_MEMORY_ALLOCATION_MODULE
*Hob
;
281 ASSERT (((MemoryAllocationModule
& (EFI_PAGE_SIZE
- 1)) == 0) &&
282 ((ModuleLength
& (EFI_PAGE_SIZE
- 1)) == 0));
284 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, (UINT16
) sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE
));
289 CopyGuid (&(Hob
->MemoryAllocationHeader
.Name
), &gEfiHobMemoryAllocModuleGuid
);
290 Hob
->MemoryAllocationHeader
.MemoryBaseAddress
= MemoryAllocationModule
;
291 Hob
->MemoryAllocationHeader
.MemoryLength
= ModuleLength
;
292 Hob
->MemoryAllocationHeader
.MemoryType
= EfiBootServicesCode
;
295 // Zero the reserved space to match HOB spec
297 ZeroMem (Hob
->MemoryAllocationHeader
.Reserved
, sizeof (Hob
->MemoryAllocationHeader
.Reserved
));
299 CopyGuid (&Hob
->ModuleName
, ModuleName
);
300 Hob
->EntryPoint
= EntryPoint
;
304 Builds a HOB that describes a chunk of system memory with Owner GUID.
306 This function builds a HOB that describes a chunk of system memory.
307 It can only be invoked during PEI phase;
308 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
310 If there is no additional space for HOB creation, then ASSERT().
312 @param ResourceType The type of resource described by this HOB.
313 @param ResourceAttribute The resource attributes of the memory described by this HOB.
314 @param PhysicalStart The 64 bit physical address of memory described by this HOB.
315 @param NumberOfBytes The length of the memory described by this HOB in bytes.
316 @param OwnerGUID GUID for the owner of this resource.
321 BuildResourceDescriptorWithOwnerHob (
322 IN EFI_RESOURCE_TYPE ResourceType
,
323 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute
,
324 IN EFI_PHYSICAL_ADDRESS PhysicalStart
,
325 IN UINT64 NumberOfBytes
,
326 IN EFI_GUID
*OwnerGUID
329 EFI_HOB_RESOURCE_DESCRIPTOR
*Hob
;
331 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
, (UINT16
) sizeof (EFI_HOB_RESOURCE_DESCRIPTOR
));
336 Hob
->ResourceType
= ResourceType
;
337 Hob
->ResourceAttribute
= ResourceAttribute
;
338 Hob
->PhysicalStart
= PhysicalStart
;
339 Hob
->ResourceLength
= NumberOfBytes
;
341 CopyGuid (&Hob
->Owner
, OwnerGUID
);
345 Builds a HOB that describes a chunk of system memory.
347 This function builds a HOB that describes a chunk of system memory.
348 It can only be invoked during PEI phase;
349 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
351 If there is no additional space for HOB creation, then ASSERT().
353 @param ResourceType The type of resource described by this HOB.
354 @param ResourceAttribute The resource attributes of the memory described by this HOB.
355 @param PhysicalStart The 64 bit physical address of memory described by this HOB.
356 @param NumberOfBytes The length of the memory described by this HOB in bytes.
361 BuildResourceDescriptorHob (
362 IN EFI_RESOURCE_TYPE ResourceType
,
363 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute
,
364 IN EFI_PHYSICAL_ADDRESS PhysicalStart
,
365 IN UINT64 NumberOfBytes
368 EFI_HOB_RESOURCE_DESCRIPTOR
*Hob
;
370 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
, (UINT16
) sizeof (EFI_HOB_RESOURCE_DESCRIPTOR
));
375 Hob
->ResourceType
= ResourceType
;
376 Hob
->ResourceAttribute
= ResourceAttribute
;
377 Hob
->PhysicalStart
= PhysicalStart
;
378 Hob
->ResourceLength
= NumberOfBytes
;
379 ZeroMem (&(Hob
->Owner
), sizeof (EFI_GUID
));
383 Builds a customized HOB tagged with a GUID for identification and returns
384 the start address of GUID HOB data.
386 This function builds a customized HOB tagged with a GUID for identification
387 and returns the start address of GUID HOB data so that caller can fill the customized data.
388 The HOB Header and Name field is already stripped.
389 It can only be invoked during PEI phase;
390 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
392 If Guid is NULL, then ASSERT().
393 If there is no additional space for HOB creation, then ASSERT().
394 If DataLength > (0xFFF8 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
395 HobLength is UINT16 and multiples of 8 bytes, so the max HobLength is 0xFFF8.
397 @param Guid The GUID to tag the customized HOB.
398 @param DataLength The size of the data payload for the GUID HOB.
400 @retval NULL The GUID HOB could not be allocated.
401 @retval others The start address of GUID HOB data.
407 IN CONST EFI_GUID
*Guid
,
411 EFI_HOB_GUID_TYPE
*Hob
;
414 // Make sure Guid is valid
416 ASSERT (Guid
!= NULL
);
419 // Make sure that data length is not too long.
421 ASSERT (DataLength
<= (0xFFF8 - sizeof (EFI_HOB_GUID_TYPE
)));
423 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_GUID_EXTENSION
, (UINT16
) (sizeof (EFI_HOB_GUID_TYPE
) + DataLength
));
427 CopyGuid (&Hob
->Name
, Guid
);
432 Builds a customized HOB tagged with a GUID for identification, copies the input data to the HOB
433 data field, and returns the start address of the GUID HOB data.
435 This function builds a customized HOB tagged with a GUID for identification and copies the input
436 data to the HOB data field and returns the start address of the GUID HOB data. It can only be
437 invoked during PEI phase; for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
438 The HOB Header and Name field is already stripped.
439 It can only be invoked during PEI phase;
440 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
442 If Guid is NULL, then ASSERT().
443 If Data is NULL and DataLength > 0, then ASSERT().
444 If there is no additional space for HOB creation, then ASSERT().
445 If DataLength > (0xFFF8 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
446 HobLength is UINT16 and multiples of 8 bytes, so the max HobLength is 0xFFF8.
448 @param Guid The GUID to tag the customized HOB.
449 @param Data The data to be copied into the data field of the GUID HOB.
450 @param DataLength The size of the data payload for the GUID HOB.
452 @retval NULL The GUID HOB could not be allocated.
453 @retval others The start address of GUID HOB data.
459 IN CONST EFI_GUID
*Guid
,
466 ASSERT (Data
!= NULL
|| DataLength
== 0);
468 HobData
= BuildGuidHob (Guid
, DataLength
);
469 if (HobData
== NULL
) {
473 return CopyMem (HobData
, Data
, DataLength
);
477 Builds a Firmware Volume HOB.
479 This function builds a Firmware Volume HOB.
480 It can only be invoked during PEI phase;
481 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
483 If there is no additional space for HOB creation, then ASSERT().
485 @param BaseAddress The base address of the Firmware Volume.
486 @param Length The size of the Firmware Volume in bytes.
492 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
496 EFI_HOB_FIRMWARE_VOLUME
*Hob
;
498 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_FV
, (UINT16
) sizeof (EFI_HOB_FIRMWARE_VOLUME
));
503 Hob
->BaseAddress
= BaseAddress
;
504 Hob
->Length
= Length
;
508 Builds a EFI_HOB_TYPE_FV2 HOB.
510 This function builds a EFI_HOB_TYPE_FV2 HOB.
511 It can only be invoked during PEI phase;
512 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
514 If there is no additional space for HOB creation, then ASSERT().
516 @param BaseAddress The base address of the Firmware Volume.
517 @param Length The size of the Firmware Volume in bytes.
518 @param FvName The name of the Firmware Volume.
519 @param FileName The name of the file.
525 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
527 IN CONST EFI_GUID
*FvName
,
528 IN CONST EFI_GUID
*FileName
531 EFI_HOB_FIRMWARE_VOLUME2
*Hob
;
533 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_FV2
, (UINT16
) sizeof (EFI_HOB_FIRMWARE_VOLUME2
));
538 Hob
->BaseAddress
= BaseAddress
;
539 Hob
->Length
= Length
;
540 CopyGuid (&Hob
->FvName
, FvName
);
541 CopyGuid (&Hob
->FileName
, FileName
);
545 Builds a Capsule Volume HOB.
547 This function builds a Capsule Volume HOB.
548 It can only be invoked during PEI phase;
549 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
551 If the platform does not support Capsule Volume HOBs, then ASSERT().
552 If there is no additional space for HOB creation, then ASSERT().
554 @param BaseAddress The base address of the Capsule Volume.
555 @param Length The size of the Capsule Volume in bytes.
561 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
565 EFI_HOB_UEFI_CAPSULE
*Hob
;
567 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_UEFI_CAPSULE
, (UINT16
) sizeof (EFI_HOB_UEFI_CAPSULE
));
572 Hob
->BaseAddress
= BaseAddress
;
573 Hob
->Length
= Length
;
577 Builds a HOB for the CPU.
579 This function builds a HOB for the CPU.
580 It can only be invoked during PEI phase;
581 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
583 If there is no additional space for HOB creation, then ASSERT().
585 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
586 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
592 IN UINT8 SizeOfMemorySpace
,
593 IN UINT8 SizeOfIoSpace
598 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_CPU
, (UINT16
) sizeof (EFI_HOB_CPU
));
603 Hob
->SizeOfMemorySpace
= SizeOfMemorySpace
;
604 Hob
->SizeOfIoSpace
= SizeOfIoSpace
;
607 // Zero the reserved space to match HOB spec
609 ZeroMem (Hob
->Reserved
, sizeof (Hob
->Reserved
));
613 Builds a HOB for the Stack.
615 This function builds a HOB for the stack.
616 It can only be invoked during PEI phase;
617 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
619 If there is no additional space for HOB creation, then ASSERT().
621 @param BaseAddress The 64 bit physical address of the Stack.
622 @param Length The length of the stack in bytes.
628 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
632 EFI_HOB_MEMORY_ALLOCATION_STACK
*Hob
;
634 ASSERT (((BaseAddress
& (EFI_PAGE_SIZE
- 1)) == 0) &&
635 ((Length
& (EFI_PAGE_SIZE
- 1)) == 0));
637 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, (UINT16
) sizeof (EFI_HOB_MEMORY_ALLOCATION_STACK
));
642 CopyGuid (&(Hob
->AllocDescriptor
.Name
), &gEfiHobMemoryAllocStackGuid
);
643 Hob
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
644 Hob
->AllocDescriptor
.MemoryLength
= Length
;
645 Hob
->AllocDescriptor
.MemoryType
= EfiBootServicesData
;
648 // Zero the reserved space to match HOB spec
650 ZeroMem (Hob
->AllocDescriptor
.Reserved
, sizeof (Hob
->AllocDescriptor
.Reserved
));
654 Builds a HOB for the BSP store.
656 This function builds a HOB for BSP store.
657 It can only be invoked during PEI phase;
658 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
660 If there is no additional space for HOB creation, then ASSERT().
662 @param BaseAddress The 64 bit physical address of the BSP.
663 @param Length The length of the BSP store in bytes.
664 @param MemoryType The type of memory allocated by this HOB.
670 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
672 IN EFI_MEMORY_TYPE MemoryType
675 EFI_HOB_MEMORY_ALLOCATION_BSP_STORE
*Hob
;
677 ASSERT (((BaseAddress
& (EFI_PAGE_SIZE
- 1)) == 0) &&
678 ((Length
& (EFI_PAGE_SIZE
- 1)) == 0));
680 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, (UINT16
) sizeof (EFI_HOB_MEMORY_ALLOCATION_BSP_STORE
));
685 CopyGuid (&(Hob
->AllocDescriptor
.Name
), &gEfiHobMemoryAllocBspStoreGuid
);
686 Hob
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
687 Hob
->AllocDescriptor
.MemoryLength
= Length
;
688 Hob
->AllocDescriptor
.MemoryType
= MemoryType
;
691 // Zero the reserved space to match HOB spec
693 ZeroMem (Hob
->AllocDescriptor
.Reserved
, sizeof (Hob
->AllocDescriptor
.Reserved
));
697 Builds a HOB for the memory allocation.
699 This function builds a HOB for the memory allocation.
700 It can only be invoked during PEI phase;
701 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
703 If there is no additional space for HOB creation, then ASSERT().
705 @param BaseAddress The 64 bit physical address of the memory.
706 @param Length The length of the memory allocation in bytes.
707 @param MemoryType The type of memory allocated by this HOB.
712 BuildMemoryAllocationHob (
713 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
715 IN EFI_MEMORY_TYPE MemoryType
718 EFI_HOB_MEMORY_ALLOCATION
*Hob
;
720 ASSERT (((BaseAddress
& (EFI_PAGE_SIZE
- 1)) == 0) &&
721 ((Length
& (EFI_PAGE_SIZE
- 1)) == 0));
723 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, (UINT16
) sizeof (EFI_HOB_MEMORY_ALLOCATION
));
728 ZeroMem (&(Hob
->AllocDescriptor
.Name
), sizeof (EFI_GUID
));
729 Hob
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
730 Hob
->AllocDescriptor
.MemoryLength
= Length
;
731 Hob
->AllocDescriptor
.MemoryType
= MemoryType
;
733 // Zero the reserved space to match HOB spec
735 ZeroMem (Hob
->AllocDescriptor
.Reserved
, sizeof (Hob
->AllocDescriptor
.Reserved
));