4 Copyright (c) 2004 - 2007, Intel Corporation
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.
13 MemoryAllocationModule Name:
25 #include "PeiHobLib.h"
27 #include EFI_GUID_DEFINITION(MemoryAllocationHob)
31 BuildHobEndOfHobList (
38 Builds an end of HOB list HOB
42 HobStart - The HOB to build
46 A pointer to the next HOB
50 EFI_PEI_HOB_POINTERS Hob
;
54 Hob
.Header
->HobType
= EFI_HOB_TYPE_END_OF_HOB_LIST
;
55 Hob
.Header
->HobLength
= sizeof(EFI_HOB_GENERIC_HEADER
);
62 BuildHobHandoffInfoTable (
65 IN EFI_BOOT_MODE BootMode
,
66 IN EFI_PHYSICAL_ADDRESS EfiMemoryTop
,
67 IN EFI_PHYSICAL_ADDRESS EfiMemoryBottom
,
68 IN EFI_PHYSICAL_ADDRESS EfiFreeMemoryTop
,
69 IN EFI_PHYSICAL_ADDRESS EfiFreeMemoryBottom
75 Builds a HandoffInformationTable Information Table HOB
79 HobStart - Start pointer of hob list
80 Version - The version number pertaining to the PHIT HOB definition.
81 BootMode - The system boot mode as determined during the HOB producer phase.
82 EfiMemoryTop - The highest address location of memory that is allocated for use by the HOB
84 EfiMemoryBottom - The lowest address location of memory that is allocated for use by the HOB
86 EfiFreeMemoryTop - The highest address location of free memory that is currently available for use
87 by the HOB producer phase.
88 EfiFreeMemoryBottom - The lowest address location of free memory that is available for
89 use by the HOB producer phase.
97 EFI_PEI_HOB_POINTERS HandOffHob
;
98 EFI_PEI_HOB_POINTERS Hob
;
99 EFI_PEI_HOB_POINTERS HobEnd
;
102 HandOffHob
.Raw
= HobStart
;
104 Hob
.Header
->HobType
= EFI_HOB_TYPE_HANDOFF
;
105 Hob
.Header
->HobLength
= sizeof(EFI_HOB_HANDOFF_INFO_TABLE
);
107 Hob
.HandoffInformationTable
->Version
= Version
;
108 Hob
.HandoffInformationTable
->BootMode
= BootMode
;
110 Hob
.HandoffInformationTable
->EfiMemoryTop
= EfiMemoryTop
;
111 Hob
.HandoffInformationTable
->EfiMemoryBottom
= EfiMemoryBottom
;
112 Hob
.HandoffInformationTable
->EfiFreeMemoryTop
= EfiFreeMemoryTop
;
113 Hob
.HandoffInformationTable
->EfiFreeMemoryBottom
= EfiFreeMemoryBottom
;
115 HobEnd
.Raw
= (VOID
*)(Hob
.HandoffInformationTable
+ 1);
116 Hob
.HandoffInformationTable
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HobEnd
.Raw
;
117 Hob
= BuildHobEndOfHobList (HobEnd
.Raw
);
118 HandOffHob
.HandoffInformationTable
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
127 IN EFI_GUID
*ModuleName
,
128 IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule
,
129 IN UINT64 ModuleLength
,
130 IN EFI_PHYSICAL_ADDRESS EntryPoint
136 Builds a HOB for a loaded PE32 module
140 HobStart - Start pointer of hob list
142 ModuleName - The GUID File Name of the HON from the Firmware Volume
144 MemoryAllocationModule - The 64 bit physical address of the module
146 ModuleLength - The length of the module in bytes
148 EntryPoint - The 64 bit physical address of the entry point to the module
153 EFI_NOT_AVAILABLE_YET
157 EFI_PEI_HOB_POINTERS Hob
;
158 EFI_PEI_HOB_POINTERS HandOffHob
;
160 HandOffHob
.Raw
= HobStart
;
161 Hob
.Raw
= (VOID
*)(UINTN
)(HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
);
163 Hob
.Header
->HobType
= EFI_HOB_TYPE_MEMORY_ALLOCATION
;
164 Hob
.Header
->HobLength
= sizeof(EFI_HOB_MEMORY_ALLOCATION_MODULE
);
166 CopyMem(&(Hob
.MemoryAllocationModule
->ModuleName
), ModuleName
, sizeof(EFI_GUID
));
167 CopyMem(&(Hob
.MemoryAllocationModule
->MemoryAllocationHeader
.Name
), &gEfiHobMemeryAllocModuleGuid
, sizeof(EFI_GUID
));
168 Hob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryBaseAddress
= MemoryAllocationModule
;
169 Hob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryLength
= ModuleLength
;
170 Hob
.MemoryAllocationModule
->MemoryAllocationHeader
.MemoryType
= EfiConventionalMemory
;
172 Hob
.MemoryAllocationModule
->EntryPoint
= EntryPoint
;
174 Hob
.MemoryAllocationModule
++;
175 HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
176 Hob
= BuildHobEndOfHobList(Hob
.Raw
);
177 HandOffHob
.HandoffInformationTable
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
182 BuildHobResourceDescriptor (
184 IN EFI_RESOURCE_TYPE ResourceType
,
185 IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute
,
186 IN EFI_PHYSICAL_ADDRESS PhysicalStart
,
187 IN UINT64 NumberOfBytes
193 Builds a HOB that describes a chunck of system memory
197 HobStart - Start pointer of hob list
199 ResourceType - The type of memory described by this HOB
201 ResourceAttribute - The memory attributes of the memory described by this HOB
203 PhysicalStart - The 64 bit physical address of memory described by this HOB
205 NumberOfBytes - The length of the memoty described by this HOB in bytes
210 EFI_NOT_AVAILABLE_YET
214 EFI_PEI_HOB_POINTERS Hob
;
215 EFI_PEI_HOB_POINTERS HandOffHob
;
217 HandOffHob
.Raw
= HobStart
;
218 Hob
.Raw
= (VOID
*)(UINTN
)(HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
);
220 Hob
.Header
->HobType
= EFI_HOB_TYPE_RESOURCE_DESCRIPTOR
;
221 Hob
.Header
->HobLength
= sizeof(EFI_HOB_RESOURCE_DESCRIPTOR
);
223 Hob
.ResourceDescriptor
->ResourceType
= ResourceType
;
224 Hob
.ResourceDescriptor
->ResourceAttribute
= ResourceAttribute
;
225 Hob
.ResourceDescriptor
->PhysicalStart
= PhysicalStart
;
226 Hob
.ResourceDescriptor
->ResourceLength
= NumberOfBytes
;
228 Hob
.ResourceDescriptor
++;
229 HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
230 Hob
= BuildHobEndOfHobList(Hob
.Raw
);
231 HandOffHob
.HandoffInformationTable
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
246 Builds a custom HOB that is tagged with a GUID for identification
250 HobStart - Start pointer of hob list
252 Guid - The GUID of the custome HOB type
254 Buffer - A pointer to the data for the custom HOB type
256 BufferSize - The size in byte of BufferSize
261 EFI_NOT_AVAILABLE_YET
265 EFI_PEI_HOB_POINTERS Hob
;
266 EFI_PEI_HOB_POINTERS HandOffHob
;
270 HandOffHob
.Raw
= HobStart
;
271 Hob
.Raw
= (VOID
*)(UINTN
)HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
;
274 Hob
.Header
->HobType
= EFI_HOB_TYPE_GUID_EXTENSION
;
275 Length
= sizeof(EFI_HOB_GUID_TYPE
) + BufferSize
;
276 Length
= (Length
+ 0x7) & (~0x7);
277 Hob
.Header
->HobLength
= (UINT16
)Length
;
278 CopyMem(&Hob
.Guid
->Name
, Guid
, sizeof(EFI_GUID
));
279 CopyMem(Hob
.Raw
+ sizeof(EFI_HOB_GUID_TYPE
), Buffer
, BufferSize
);
282 HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
283 Hob
= BuildHobEndOfHobList(Hob
.Raw
);
284 HandOffHob
.HandoffInformationTable
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
289 BuildHobFvDescriptor (
291 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
298 Builds a Firmware Volume HOB
302 HobStart - Start pointer of hob list
304 BaseAddress - The base address of the Firmware Volume
306 Length - The size of the Firmware Volume in bytes
311 EFI_NOT_AVAILABLE_YET
315 EFI_PEI_HOB_POINTERS Hob
;
316 EFI_PEI_HOB_POINTERS HandOffHob
;
318 HandOffHob
.Raw
= HobStart
;
319 Hob
.Raw
= (VOID
*)(UINTN
)(HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
);
321 Hob
.Header
->HobType
= EFI_HOB_TYPE_FV
;
322 Hob
.Header
->HobLength
= sizeof(EFI_HOB_FIRMWARE_VOLUME
);
324 Hob
.FirmwareVolume
->BaseAddress
= BaseAddress
;
325 Hob
.FirmwareVolume
->Length
= Length
;
327 Hob
.FirmwareVolume
++;
329 HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
330 Hob
= BuildHobEndOfHobList(Hob
.Raw
);
331 HandOffHob
.HandoffInformationTable
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
338 IN UINT8 SizeOfMemorySpace
,
339 IN UINT8 SizeOfIoSpace
345 Builds a HOB for the CPU
349 HobStart - Start pointer of hob list
351 SizeOfMemorySpace - Identifies the maximum
352 physical memory addressibility of the processor.
354 SizeOfIoSpace - Identifies the maximum physical I/O addressibility
360 EFI_NOT_AVAILABLE_YET
364 EFI_PEI_HOB_POINTERS Hob
;
365 EFI_PEI_HOB_POINTERS HandOffHob
;
367 HandOffHob
.Raw
= HobStart
;
368 Hob
.Raw
= (VOID
*)(UINTN
)HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
;
370 Hob
.Header
->HobType
= EFI_HOB_TYPE_CPU
;
371 Hob
.Header
->HobLength
= sizeof(EFI_HOB_CPU
);
373 Hob
.Cpu
->SizeOfMemorySpace
= SizeOfMemorySpace
;
374 Hob
.Cpu
->SizeOfIoSpace
= SizeOfIoSpace
;
377 HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
378 Hob
= BuildHobEndOfHobList(Hob
.Raw
);
379 HandOffHob
.HandoffInformationTable
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
388 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
395 Builds a HOB for the Stack
399 HobStart - Start pointer of hob list
401 BaseAddress - The 64 bit physical address of the Stack
403 Length - The length of the stack in bytes
408 EFI_NOT_AVAILABLE_YET
412 EFI_PEI_HOB_POINTERS Hob
;
413 EFI_PEI_HOB_POINTERS HandOffHob
;
415 HandOffHob
.Raw
= HobStart
;
416 Hob
.Raw
= (VOID
*)(UINTN
)HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
;
418 Hob
.Header
->HobType
= EFI_HOB_TYPE_MEMORY_ALLOCATION
;
419 Hob
.Header
->HobLength
= sizeof(EFI_HOB_MEMORY_ALLOCATION_STACK
);
421 CopyMem(&(Hob
.MemoryAllocationStack
->AllocDescriptor
.Name
), &gEfiHobMemeryAllocStackGuid
, sizeof(EFI_GUID
));
422 (Hob
.MemoryAllocationStack
->AllocDescriptor
).MemoryBaseAddress
= BaseAddress
;
423 (Hob
.MemoryAllocationStack
->AllocDescriptor
).MemoryLength
= Length
;
424 (Hob
.MemoryAllocationStack
->AllocDescriptor
).MemoryType
= EfiBootServicesData
;
426 Hob
.MemoryAllocationStack
++;
427 HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
428 Hob
= BuildHobEndOfHobList(Hob
.Raw
);
429 HandOffHob
.HandoffInformationTable
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
438 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
440 IN EFI_MEMORY_TYPE MemoryType
446 Builds a HOB for the bsp store
450 HobStart - Start pointer of hob list
452 BaseAddress - The 64 bit physical address of bsp store
454 Length - The length of the bsp store in bytes
456 MemoryType - Memory type of the bsp store
461 EFI_NOT_AVAILABLE_YET
465 EFI_PEI_HOB_POINTERS Hob
;
466 EFI_PEI_HOB_POINTERS HandOffHob
;
468 HandOffHob
.Raw
= HobStart
;
469 Hob
.Raw
= (VOID
*)(UINTN
)HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
;
470 Hob
.Header
->HobType
= EFI_HOB_TYPE_MEMORY_ALLOCATION
;
471 Hob
.Header
->HobLength
= sizeof(EFI_HOB_MEMORY_ALLOCATION_BSP_STORE
);
473 (Hob
.MemoryAllocationBspStore
->AllocDescriptor
).MemoryBaseAddress
= BaseAddress
;
474 (Hob
.MemoryAllocationBspStore
->AllocDescriptor
).MemoryLength
= Length
;
475 (Hob
.MemoryAllocationBspStore
->AllocDescriptor
).MemoryType
= MemoryType
;
476 CopyMem(&(Hob
.MemoryAllocationBspStore
->AllocDescriptor
).Name
, &gEfiHobMemeryAllocBspStoreGuid
, sizeof(EFI_GUID
));
477 Hob
.MemoryAllocationBspStore
++;
479 HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
480 Hob
= BuildHobEndOfHobList(Hob
.Raw
);
481 HandOffHob
.HandoffInformationTable
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
487 BuildMemoryAllocationHob (
489 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
492 IN EFI_MEMORY_TYPE MemoryType
498 Builds a HOB for memory allocation
502 HobStart - Start pointer of hob list
504 BaseAddress - The base address of memory allocated by this HOB.
506 Length - The length in bytes of memory allocated by this HOB.
508 Name - A GUID that defines the memory allocation region's type and purpose,
509 as well as other fields within the memory allocation HOB.
511 MemoryType - Defines the type of memory allocated by this HOB.
516 EFI_NOT_AVAILABLE_YET
520 EFI_PEI_HOB_POINTERS Hob
;
521 EFI_PEI_HOB_POINTERS HandOffHob
;
524 HandOffHob
.Raw
= HobStart
;
525 Hob
.Raw
= (VOID
*)(UINTN
)HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
;
527 Hob
.Header
->HobType
= EFI_HOB_TYPE_MEMORY_ALLOCATION
;
528 Hob
.Header
->HobLength
= sizeof(EFI_HOB_MEMORY_ALLOCATION
);
531 CopyMem(&(Hob
.MemoryAllocation
->AllocDescriptor
.Name
), &Name
, sizeof(EFI_GUID
));
533 ZeroMem(&Hob
.MemoryAllocation
->AllocDescriptor
.Name
, sizeof(EFI_GUID
));
536 (Hob
.MemoryAllocation
->AllocDescriptor
).MemoryBaseAddress
= BaseAddress
;
537 (Hob
.MemoryAllocation
->AllocDescriptor
).MemoryLength
= Length
;
538 (Hob
.MemoryAllocation
->AllocDescriptor
).MemoryType
= MemoryType
;
540 Hob
.MemoryAllocation
++;
541 HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
542 Hob
= BuildHobEndOfHobList(Hob
.Raw
);
543 HandOffHob
.HandoffInformationTable
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
552 OUT UINTN
*BufferSize OPTIONAL
558 This function searches the first instance of a HOB among the whole HOB list.
562 HobStart - A pointer to the start pointer of hob list.
564 Guid - A pointer to the GUID to match with in the HOB list.
566 Buffer - A pointer to the pointer to the data for the custom HOB type.
568 BufferSize - A Pointer to the size in byte of BufferSize.
572 The first instance of the matched GUID HOB among the whole HOB list
577 EFI_PEI_HOB_POINTERS GuidHob
;
579 GuidHob
.Raw
= *HobStart
;
581 for (Status
= EFI_NOT_FOUND
; EFI_ERROR (Status
); ) {
583 if (END_OF_HOB_LIST (GuidHob
)) {
584 return EFI_NOT_FOUND
;
587 if (GuidHob
.Header
->HobType
== EFI_HOB_TYPE_GUID_EXTENSION
) {
588 if ( ((INT32
*)Guid
)[0] == ((INT32
*)&GuidHob
.Guid
->Name
)[0] &&
589 ((INT32
*)Guid
)[1] == ((INT32
*)&GuidHob
.Guid
->Name
)[1] &&
590 ((INT32
*)Guid
)[2] == ((INT32
*)&GuidHob
.Guid
->Name
)[2] &&
591 ((INT32
*)Guid
)[3] == ((INT32
*)&GuidHob
.Guid
->Name
)[3] ) {
592 Status
= EFI_SUCCESS
;
593 *Buffer
= (VOID
*)((UINT8
*)(&GuidHob
.Guid
->Name
) + sizeof (EFI_GUID
));
595 *BufferSize
= GuidHob
.Header
->HobLength
- sizeof (EFI_HOB_GUID_TYPE
);
600 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
615 This function returns the first instance of a HOB type in a HOB list.
619 Type The HOB type to return.
620 HobStart The first HOB in the HOB list.
624 HobStart There were no HOBs found with the requested type.
625 else Returns the first HOB with the matching type.
629 EFI_PEI_HOB_POINTERS Hob
;
633 // Return input if not found
635 if (HobStart
== NULL
) {
640 // Parse the HOB list, stop if end of list or matching type found.
642 while (!END_OF_HOB_LIST (Hob
)) {
644 if (Hob
.Header
->HobType
== Type
) {
648 Hob
.Raw
= GET_NEXT_HOB (Hob
);
652 // Return input if not found
654 if (END_OF_HOB_LIST (Hob
)) {
658 return (VOID
*) (Hob
.Raw
);
663 IN OUT VOID
**HobStart
,
666 OUT UINTN
*BufferSize OPTIONAL
671 Get the next guid hob.
674 HobStart A pointer to the start hob.
675 Guid A pointer to a guid.
676 Buffer A pointer to the buffer.
677 BufferSize Buffer size.
682 EFI_NOT_FOUND - Next Guid hob not found
684 EFI_SUCCESS - Next Guid hob found and data for this Guid got
686 EFI_INVALID_PARAMETER - invalid parameter
691 EFI_PEI_HOB_POINTERS GuidHob
;
693 if (Buffer
== NULL
) {
694 return EFI_INVALID_PARAMETER
;
697 for (Status
= EFI_NOT_FOUND
; EFI_ERROR (Status
);) {
699 GuidHob
.Raw
= *HobStart
;
700 if (END_OF_HOB_LIST (GuidHob
)) {
701 return EFI_NOT_FOUND
;
704 GuidHob
.Raw
= GetHob (EFI_HOB_TYPE_GUID_EXTENSION
, *HobStart
);
705 if (GuidHob
.Header
->HobType
== EFI_HOB_TYPE_GUID_EXTENSION
) {
706 if ( ((INT32
*)Guid
)[0] == ((INT32
*)&GuidHob
.Guid
->Name
)[0] &&
707 ((INT32
*)Guid
)[1] == ((INT32
*)&GuidHob
.Guid
->Name
)[1] &&
708 ((INT32
*)Guid
)[2] == ((INT32
*)&GuidHob
.Guid
->Name
)[2] &&
709 ((INT32
*)Guid
)[3] == ((INT32
*)&GuidHob
.Guid
->Name
)[3] ) {
710 Status
= EFI_SUCCESS
;
711 *Buffer
= (VOID
*) ((UINT8
*) (&GuidHob
.Guid
->Name
) + sizeof (EFI_GUID
));
712 if (BufferSize
!= NULL
) {
713 *BufferSize
= GuidHob
.Header
->HobLength
- sizeof (EFI_HOB_GUID_TYPE
);
718 *HobStart
= GET_NEXT_HOB (GuidHob
);