2 Provide Hob Library functions for Pei phase.
4 Copyright (c) 2007 - 2008, Intel Corporation<BR>
5 All rights reserved. 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 info 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 info 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 @return The address of new HOB.
234 InternalPeiCreateHob (
242 Status
= PeiServicesCreateHob (Type
, Length
, &Hob
);
244 // Assume the process of HOB building is always successful.
246 ASSERT_EFI_ERROR (Status
);
251 Builds a HOB for a loaded PE32 module.
253 This function builds a HOB for a loaded PE32 module.
254 It can only be invoked during PEI phase;
255 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
257 If ModuleName is NULL, then ASSERT().
258 If there is no additional space for HOB creation, then ASSERT().
260 @param ModuleName The GUID File Name of the module.
261 @param MemoryAllocationModule The 64 bit physical address of the module.
262 @param ModuleLength The length of the module in bytes.
263 @param EntryPoint The 64 bit physical address of the module entry point.
269 IN CONST EFI_GUID
*ModuleName
,
270 IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule
,
271 IN UINT64 ModuleLength
,
272 IN EFI_PHYSICAL_ADDRESS EntryPoint
275 EFI_HOB_MEMORY_ALLOCATION_MODULE
*Hob
;
277 ASSERT (((MemoryAllocationModule
& (EFI_PAGE_SIZE
- 1)) == 0) &&
278 ((ModuleLength
& (EFI_PAGE_SIZE
- 1)) == 0));
280 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE
));
282 CopyGuid (&(Hob
->MemoryAllocationHeader
.Name
), &gEfiHobMemoryAllocModuleGuid
);
283 Hob
->MemoryAllocationHeader
.MemoryBaseAddress
= MemoryAllocationModule
;
284 Hob
->MemoryAllocationHeader
.MemoryLength
= ModuleLength
;
285 Hob
->MemoryAllocationHeader
.MemoryType
= EfiBootServicesCode
;
288 // Zero the reserved space to match HOB spec
290 ZeroMem (Hob
->MemoryAllocationHeader
.Reserved
, sizeof (Hob
->MemoryAllocationHeader
.Reserved
));
292 CopyGuid (&Hob
->ModuleName
, ModuleName
);
293 Hob
->EntryPoint
= EntryPoint
;
297 Builds a HOB that describes a chunk of system memory.
299 This function builds a HOB that describes a chunk of system memory.
300 It can only be invoked during PEI phase;
301 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
303 If there is no additional space for HOB creation, then ASSERT().
305 @param ResourceType The type of resource described by this HOB.
306 @param ResourceAttribute The resource attributes of the memory described by this HOB.
307 @param PhysicalStart The 64 bit physical address of memory described by this HOB.
308 @param NumberOfBytes The length of the memory described by this HOB in bytes.
313 BuildResourceDescriptorHob (
314 IN EFI_RESOURCE_TYPE ResourceType
,
315 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute
,
316 IN EFI_PHYSICAL_ADDRESS PhysicalStart
,
317 IN UINT64 NumberOfBytes
320 EFI_HOB_RESOURCE_DESCRIPTOR
*Hob
;
322 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR
));
324 Hob
->ResourceType
= ResourceType
;
325 Hob
->ResourceAttribute
= ResourceAttribute
;
326 Hob
->PhysicalStart
= PhysicalStart
;
327 Hob
->ResourceLength
= NumberOfBytes
;
331 Builds a customized HOB tagged with a GUID for identification and returns
332 the start address of GUID HOB data.
334 This function builds a customized HOB tagged with a GUID for identification
335 and returns the start address of GUID HOB data so that caller can fill the customized data.
336 The HOB Header and Name field is already stripped.
337 It can only be invoked during PEI phase;
338 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
340 If Guid is NULL, then ASSERT().
341 If there is no additional space for HOB creation, then ASSERT().
342 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
344 @param Guid The GUID to tag the customized HOB.
345 @param DataLength The size of the data payload for the GUID HOB.
347 @return The start address of GUID HOB data.
353 IN CONST EFI_GUID
*Guid
,
357 EFI_HOB_GUID_TYPE
*Hob
;
360 // Make sure that data length is not too long.
362 ASSERT (DataLength
<= (0xffff - sizeof (EFI_HOB_GUID_TYPE
)));
364 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_GUID_EXTENSION
, (UINT16
) (sizeof (EFI_HOB_GUID_TYPE
) + DataLength
));
365 CopyGuid (&Hob
->Name
, Guid
);
370 Builds a customized HOB tagged with a GUID for identification, copies the input data to the HOB
371 data field, and returns the start address of the GUID HOB data.
373 This function builds a customized HOB tagged with a GUID for identification and copies the input
374 data to the HOB data field and returns the start address of the GUID HOB data. It can only be
375 invoked during PEI phase; for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
376 The HOB Header and Name field is already stripped.
377 It can only be invoked during PEI phase;
378 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
380 If Guid is NULL, then ASSERT().
381 If Data is NULL and DataLength > 0, then ASSERT().
382 If there is no additional space for HOB creation, then ASSERT().
383 If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
385 @param Guid The GUID to tag the customized HOB.
386 @param Data The data to be copied into the data field of the GUID 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
,
402 ASSERT (Data
!= NULL
|| DataLength
== 0);
404 HobData
= BuildGuidHob (Guid
, DataLength
);
406 return CopyMem (HobData
, Data
, DataLength
);
410 Builds a Firmware Volume HOB.
412 This function builds a Firmware Volume HOB.
413 It can only be invoked during PEI phase;
414 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
416 If there is no additional space for HOB creation, then ASSERT().
418 @param BaseAddress The base address of the Firmware Volume.
419 @param Length The size of the Firmware Volume in bytes.
425 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
429 EFI_HOB_FIRMWARE_VOLUME
*Hob
;
431 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_FV
, sizeof (EFI_HOB_FIRMWARE_VOLUME
));
433 Hob
->BaseAddress
= BaseAddress
;
434 Hob
->Length
= Length
;
438 Builds a EFI_HOB_TYPE_FV2 HOB.
440 This function builds a EFI_HOB_TYPE_FV2 HOB.
441 It can only be invoked during PEI phase;
442 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
444 If there is no additional space for HOB creation, then ASSERT().
446 @param BaseAddress The base address of the Firmware Volume.
447 @param Length The size of the Firmware Volume in bytes.
448 @param FvName The name of the Firmware Volume.
449 @param FileName The name of the file.
455 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
457 IN CONST EFI_GUID
*FvName
,
458 IN CONST EFI_GUID
*FileName
461 EFI_HOB_FIRMWARE_VOLUME2
*Hob
;
463 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_FV2
, sizeof (EFI_HOB_FIRMWARE_VOLUME2
));
465 Hob
->BaseAddress
= BaseAddress
;
466 Hob
->Length
= Length
;
467 CopyGuid (&Hob
->FvName
, FvName
);
468 CopyGuid (&Hob
->FileName
, FileName
);
472 Builds a Capsule Volume HOB.
474 This function builds a Capsule Volume HOB.
475 It can only be invoked during PEI phase;
476 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
478 If there is no additional space for HOB creation, then ASSERT().
480 @param BaseAddress The base address of the Capsule Volume.
481 @param Length The size of the Capsule Volume in bytes.
487 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
495 Builds a HOB for the CPU.
497 This function builds a HOB for the CPU.
498 It can only be invoked during PEI phase;
499 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
501 If there is no additional space for HOB creation, then ASSERT().
503 @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
504 @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
510 IN UINT8 SizeOfMemorySpace
,
511 IN UINT8 SizeOfIoSpace
516 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_CPU
, sizeof (EFI_HOB_CPU
));
518 Hob
->SizeOfMemorySpace
= SizeOfMemorySpace
;
519 Hob
->SizeOfIoSpace
= SizeOfIoSpace
;
522 // Zero the reserved space to match HOB spec
524 ZeroMem (Hob
->Reserved
, sizeof (Hob
->Reserved
));
528 Builds a HOB for the Stack.
530 This function builds a HOB for the stack.
531 It can only be invoked during PEI phase;
532 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
534 If there is no additional space for HOB creation, then ASSERT().
536 @param BaseAddress The 64 bit physical address of the Stack.
537 @param Length The length of the stack in bytes.
543 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
547 EFI_HOB_MEMORY_ALLOCATION_STACK
*Hob
;
549 ASSERT (((BaseAddress
& (EFI_PAGE_SIZE
- 1)) == 0) &&
550 ((Length
& (EFI_PAGE_SIZE
- 1)) == 0));
552 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION_STACK
));
554 CopyGuid (&(Hob
->AllocDescriptor
.Name
), &gEfiHobMemoryAllocStackGuid
);
555 Hob
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
556 Hob
->AllocDescriptor
.MemoryLength
= Length
;
557 Hob
->AllocDescriptor
.MemoryType
= EfiBootServicesData
;
560 // Zero the reserved space to match HOB spec
562 ZeroMem (Hob
->AllocDescriptor
.Reserved
, sizeof (Hob
->AllocDescriptor
.Reserved
));
566 Builds a HOB for the BSP store.
568 This function builds a HOB for BSP store.
569 It can only be invoked during PEI phase;
570 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 BSP.
575 @param Length The length of the BSP store in bytes.
576 @param MemoryType Type of memory allocated by this HOB.
582 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
584 IN EFI_MEMORY_TYPE MemoryType
587 EFI_HOB_MEMORY_ALLOCATION_BSP_STORE
*Hob
;
589 ASSERT (((BaseAddress
& (EFI_PAGE_SIZE
- 1)) == 0) &&
590 ((Length
& (EFI_PAGE_SIZE
- 1)) == 0));
592 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION_BSP_STORE
));
594 CopyGuid (&(Hob
->AllocDescriptor
.Name
), &gEfiHobMemoryAllocBspStoreGuid
);
595 Hob
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
596 Hob
->AllocDescriptor
.MemoryLength
= Length
;
597 Hob
->AllocDescriptor
.MemoryType
= MemoryType
;
600 // Zero the reserved space to match HOB spec
602 ZeroMem (Hob
->AllocDescriptor
.Reserved
, sizeof (Hob
->AllocDescriptor
.Reserved
));
606 Builds a HOB for the memory allocation.
608 This function builds a HOB for the memory allocation.
609 It can only be invoked during PEI phase;
610 for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
612 If there is no additional space for HOB creation, then ASSERT().
614 @param BaseAddress The 64 bit physical address of the memory.
615 @param Length The length of the memory allocation in bytes.
616 @param MemoryType Type of memory allocated by this HOB.
621 BuildMemoryAllocationHob (
622 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
624 IN EFI_MEMORY_TYPE MemoryType
627 EFI_HOB_MEMORY_ALLOCATION
*Hob
;
629 ASSERT (((BaseAddress
& (EFI_PAGE_SIZE
- 1)) == 0) &&
630 ((Length
& (EFI_PAGE_SIZE
- 1)) == 0));
632 Hob
= InternalPeiCreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION
, sizeof (EFI_HOB_MEMORY_ALLOCATION
));
634 ZeroMem (&(Hob
->AllocDescriptor
.Name
), sizeof (EFI_GUID
));
635 Hob
->AllocDescriptor
.MemoryBaseAddress
= BaseAddress
;
636 Hob
->AllocDescriptor
.MemoryLength
= Length
;
637 Hob
->AllocDescriptor
.MemoryType
= MemoryType
;
639 // Zero the reserved space to match HOB spec
641 ZeroMem (Hob
->AllocDescriptor
.Reserved
, sizeof (Hob
->AllocDescriptor
.Reserved
));