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 Hob
.Header
->HobLength
= (UINT16
)Length
;
277 CopyMem(&Hob
.Guid
->Name
, Guid
, sizeof(EFI_GUID
));
278 CopyMem(Hob
.Raw
+ sizeof(EFI_HOB_GUID_TYPE
), Buffer
, BufferSize
);
281 HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
282 Hob
= BuildHobEndOfHobList(Hob
.Raw
);
283 HandOffHob
.HandoffInformationTable
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
288 BuildHobFvDescriptor (
290 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
297 Builds a Firmware Volume HOB
301 HobStart - Start pointer of hob list
303 BaseAddress - The base address of the Firmware Volume
305 Length - The size of the Firmware Volume in bytes
310 EFI_NOT_AVAILABLE_YET
314 EFI_PEI_HOB_POINTERS Hob
;
315 EFI_PEI_HOB_POINTERS HandOffHob
;
317 HandOffHob
.Raw
= HobStart
;
318 Hob
.Raw
= (VOID
*)(UINTN
)(HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
);
320 Hob
.Header
->HobType
= EFI_HOB_TYPE_FV
;
321 Hob
.Header
->HobLength
= sizeof(EFI_HOB_FIRMWARE_VOLUME
);
323 Hob
.FirmwareVolume
->BaseAddress
= BaseAddress
;
324 Hob
.FirmwareVolume
->Length
= Length
;
326 Hob
.FirmwareVolume
++;
328 HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
329 Hob
= BuildHobEndOfHobList(Hob
.Raw
);
330 HandOffHob
.HandoffInformationTable
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
337 IN UINT8 SizeOfMemorySpace
,
338 IN UINT8 SizeOfIoSpace
344 Builds a HOB for the CPU
348 HobStart - Start pointer of hob list
350 SizeOfMemorySpace - Identifies the maximum
351 physical memory addressibility of the processor.
353 SizeOfIoSpace - Identifies the maximum physical I/O addressibility
359 EFI_NOT_AVAILABLE_YET
363 EFI_PEI_HOB_POINTERS Hob
;
364 EFI_PEI_HOB_POINTERS HandOffHob
;
366 HandOffHob
.Raw
= HobStart
;
367 Hob
.Raw
= (VOID
*)(UINTN
)HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
;
369 Hob
.Header
->HobType
= EFI_HOB_TYPE_CPU
;
370 Hob
.Header
->HobLength
= sizeof(EFI_HOB_CPU
);
372 Hob
.Cpu
->SizeOfMemorySpace
= SizeOfMemorySpace
;
373 Hob
.Cpu
->SizeOfIoSpace
= SizeOfIoSpace
;
376 HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
377 Hob
= BuildHobEndOfHobList(Hob
.Raw
);
378 HandOffHob
.HandoffInformationTable
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
387 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
394 Builds a HOB for the Stack
398 HobStart - Start pointer of hob list
400 BaseAddress - The 64 bit physical address of the Stack
402 Length - The length of the stack in bytes
407 EFI_NOT_AVAILABLE_YET
411 EFI_PEI_HOB_POINTERS Hob
;
412 EFI_PEI_HOB_POINTERS HandOffHob
;
414 HandOffHob
.Raw
= HobStart
;
415 Hob
.Raw
= (VOID
*)(UINTN
)HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
;
417 Hob
.Header
->HobType
= EFI_HOB_TYPE_MEMORY_ALLOCATION
;
418 Hob
.Header
->HobLength
= sizeof(EFI_HOB_MEMORY_ALLOCATION_STACK
);
420 CopyMem(&(Hob
.MemoryAllocationStack
->AllocDescriptor
.Name
), &gEfiHobMemeryAllocStackGuid
, sizeof(EFI_GUID
));
421 (Hob
.MemoryAllocationStack
->AllocDescriptor
).MemoryBaseAddress
= BaseAddress
;
422 (Hob
.MemoryAllocationStack
->AllocDescriptor
).MemoryLength
= Length
;
423 (Hob
.MemoryAllocationStack
->AllocDescriptor
).MemoryType
= EfiBootServicesData
;
425 Hob
.MemoryAllocationStack
++;
426 HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
427 Hob
= BuildHobEndOfHobList(Hob
.Raw
);
428 HandOffHob
.HandoffInformationTable
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
437 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
439 IN EFI_MEMORY_TYPE MemoryType
445 Builds a HOB for the bsp store
449 HobStart - Start pointer of hob list
451 BaseAddress - The 64 bit physical address of bsp store
453 Length - The length of the bsp store in bytes
455 MemoryType - Memory type of the bsp store
460 EFI_NOT_AVAILABLE_YET
464 EFI_PEI_HOB_POINTERS Hob
;
465 EFI_PEI_HOB_POINTERS HandOffHob
;
467 HandOffHob
.Raw
= HobStart
;
468 Hob
.Raw
= (VOID
*)(UINTN
)HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
;
469 Hob
.Header
->HobType
= EFI_HOB_TYPE_MEMORY_ALLOCATION
;
470 Hob
.Header
->HobLength
= sizeof(EFI_HOB_MEMORY_ALLOCATION_BSP_STORE
);
472 (Hob
.MemoryAllocationBspStore
->AllocDescriptor
).MemoryBaseAddress
= BaseAddress
;
473 (Hob
.MemoryAllocationBspStore
->AllocDescriptor
).MemoryLength
= Length
;
474 (Hob
.MemoryAllocationBspStore
->AllocDescriptor
).MemoryType
= MemoryType
;
475 CopyMem(&(Hob
.MemoryAllocationBspStore
->AllocDescriptor
).Name
, &gEfiHobMemeryAllocBspStoreGuid
, sizeof(EFI_GUID
));
476 Hob
.MemoryAllocationBspStore
++;
478 HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
479 Hob
= BuildHobEndOfHobList(Hob
.Raw
);
480 HandOffHob
.HandoffInformationTable
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
486 BuildMemoryAllocationHob (
488 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
491 IN EFI_MEMORY_TYPE MemoryType
497 Builds a HOB for memory allocation
501 HobStart - Start pointer of hob list
503 BaseAddress - The base address of memory allocated by this HOB.
505 Length - The length in bytes of memory allocated by this HOB.
507 Name - A GUID that defines the memory allocation region's type and purpose,
508 as well as other fields within the memory allocation HOB.
510 MemoryType - Defines the type of memory allocated by this HOB.
515 EFI_NOT_AVAILABLE_YET
519 EFI_PEI_HOB_POINTERS Hob
;
520 EFI_PEI_HOB_POINTERS HandOffHob
;
523 HandOffHob
.Raw
= HobStart
;
524 Hob
.Raw
= (VOID
*)(UINTN
)HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
;
526 Hob
.Header
->HobType
= EFI_HOB_TYPE_MEMORY_ALLOCATION
;
527 Hob
.Header
->HobLength
= sizeof(EFI_HOB_MEMORY_ALLOCATION
);
530 CopyMem(&(Hob
.MemoryAllocation
->AllocDescriptor
.Name
), &Name
, sizeof(EFI_GUID
));
532 ZeroMem(&Hob
.MemoryAllocation
->AllocDescriptor
.Name
, sizeof(EFI_GUID
));
535 (Hob
.MemoryAllocation
->AllocDescriptor
).MemoryBaseAddress
= BaseAddress
;
536 (Hob
.MemoryAllocation
->AllocDescriptor
).MemoryLength
= Length
;
537 (Hob
.MemoryAllocation
->AllocDescriptor
).MemoryType
= MemoryType
;
539 Hob
.MemoryAllocation
++;
540 HandOffHob
.HandoffInformationTable
->EfiEndOfHobList
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
541 Hob
= BuildHobEndOfHobList(Hob
.Raw
);
542 HandOffHob
.HandoffInformationTable
->EfiFreeMemoryBottom
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Hob
.Raw
;
551 OUT UINTN
*BufferSize OPTIONAL
557 This function searches the first instance of a HOB among the whole HOB list.
561 HobStart - A pointer to the start pointer of hob list.
563 Guid - A pointer to the GUID to match with in the HOB list.
565 Buffer - A pointer to the pointer to the data for the custom HOB type.
567 BufferSize - A Pointer to the size in byte of BufferSize.
571 The first instance of the matched GUID HOB among the whole HOB list
576 EFI_PEI_HOB_POINTERS GuidHob
;
578 GuidHob
.Raw
= *HobStart
;
580 for (Status
= EFI_NOT_FOUND
; EFI_ERROR (Status
); ) {
582 if (END_OF_HOB_LIST (GuidHob
)) {
583 return EFI_NOT_FOUND
;
586 if (GuidHob
.Header
->HobType
== EFI_HOB_TYPE_GUID_EXTENSION
) {
587 if ( ((INT32
*)Guid
)[0] == ((INT32
*)&GuidHob
.Guid
->Name
)[0] &&
588 ((INT32
*)Guid
)[1] == ((INT32
*)&GuidHob
.Guid
->Name
)[1] &&
589 ((INT32
*)Guid
)[2] == ((INT32
*)&GuidHob
.Guid
->Name
)[2] &&
590 ((INT32
*)Guid
)[3] == ((INT32
*)&GuidHob
.Guid
->Name
)[3] ) {
591 Status
= EFI_SUCCESS
;
592 *Buffer
= (VOID
*)((UINT8
*)(&GuidHob
.Guid
->Name
) + sizeof (EFI_GUID
));
594 *BufferSize
= GuidHob
.Header
->HobLength
- sizeof (EFI_HOB_GUID_TYPE
);
599 GuidHob
.Raw
= GET_NEXT_HOB (GuidHob
);
614 This function returns the first instance of a HOB type in a HOB list.
618 Type The HOB type to return.
619 HobStart The first HOB in the HOB list.
623 HobStart There were no HOBs found with the requested type.
624 else Returns the first HOB with the matching type.
628 EFI_PEI_HOB_POINTERS Hob
;
632 // Return input if not found
634 if (HobStart
== NULL
) {
639 // Parse the HOB list, stop if end of list or matching type found.
641 while (!END_OF_HOB_LIST (Hob
)) {
643 if (Hob
.Header
->HobType
== Type
) {
647 Hob
.Raw
= GET_NEXT_HOB (Hob
);
651 // Return input if not found
653 if (END_OF_HOB_LIST (Hob
)) {
657 return (VOID
*) (Hob
.Raw
);
662 IN OUT VOID
**HobStart
,
665 OUT UINTN
*BufferSize OPTIONAL
670 Get the next guid hob.
673 HobStart A pointer to the start hob.
674 Guid A pointer to a guid.
675 Buffer A pointer to the buffer.
676 BufferSize Buffer size.
681 EFI_NOT_FOUND - Next Guid hob not found
683 EFI_SUCCESS - Next Guid hob found and data for this Guid got
685 EFI_INVALID_PARAMETER - invalid parameter
690 EFI_PEI_HOB_POINTERS GuidHob
;
692 if (Buffer
== NULL
) {
693 return EFI_INVALID_PARAMETER
;
696 for (Status
= EFI_NOT_FOUND
; EFI_ERROR (Status
);) {
698 GuidHob
.Raw
= *HobStart
;
699 if (END_OF_HOB_LIST (GuidHob
)) {
700 return EFI_NOT_FOUND
;
703 GuidHob
.Raw
= GetHob (EFI_HOB_TYPE_GUID_EXTENSION
, *HobStart
);
704 if (GuidHob
.Header
->HobType
== EFI_HOB_TYPE_GUID_EXTENSION
) {
705 if ( ((INT32
*)Guid
)[0] == ((INT32
*)&GuidHob
.Guid
->Name
)[0] &&
706 ((INT32
*)Guid
)[1] == ((INT32
*)&GuidHob
.Guid
->Name
)[1] &&
707 ((INT32
*)Guid
)[2] == ((INT32
*)&GuidHob
.Guid
->Name
)[2] &&
708 ((INT32
*)Guid
)[3] == ((INT32
*)&GuidHob
.Guid
->Name
)[3] ) {
709 Status
= EFI_SUCCESS
;
710 *Buffer
= (VOID
*) ((UINT8
*) (&GuidHob
.Guid
->Name
) + sizeof (EFI_GUID
));
711 if (BufferSize
!= NULL
) {
712 *BufferSize
= GuidHob
.Header
->HobLength
- sizeof (EFI_HOB_GUID_TYPE
);
717 *HobStart
= GET_NEXT_HOB (GuidHob
);