| /** @file | |
| Copyright (c) 2010, Apple Inc. All rights reserved.<BR> | |
| Copyright (c) 2017 - 2021, Intel Corporation. All rights reserved.<BR> | |
| Copyright (c) Microsoft Corporation. | |
| SPDX-License-Identifier: BSD-2-Clause-Patent | |
| **/ | |
| #include <PiPei.h> | |
| #include <Library/HobLib.h> | |
| #include <Library/DebugLib.h> | |
| #include <Library/PayloadEntryHelperLib.h> | |
| /** | |
| Add a new HOB to the HOB List. | |
| @param HobType Type of the new HOB. | |
| @param HobLength Length of the new HOB to allocate. | |
| @return NULL if there is no space to create a hob. | |
| @return The address point to the new created hob. | |
| **/ | |
| VOID * | |
| EFIAPI | |
| CreateHob ( | |
| IN UINT16 HobType, | |
| IN UINT16 HobLength | |
| ) | |
| { | |
| EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob; | |
| EFI_HOB_GENERIC_HEADER *HobEnd; | |
| EFI_PHYSICAL_ADDRESS FreeMemory; | |
| VOID *Hob; | |
| HandOffHob = GetHobList (); | |
| if (HandOffHob == NULL) { | |
| ASSERT (HandOffHob != NULL); | |
| return NULL; | |
| } | |
| // | |
| // Check Length to avoid data overflow. | |
| // | |
| if (HobLength > MAX_UINT16 - 0x7) { | |
| return NULL; | |
| } | |
| HobLength = (UINT16)((HobLength + 0x7) & (~0x7)); | |
| FreeMemory = HandOffHob->EfiFreeMemoryTop - HandOffHob->EfiFreeMemoryBottom; | |
| if (FreeMemory < HobLength) { | |
| return NULL; | |
| } | |
| Hob = (VOID *)(UINTN)HandOffHob->EfiEndOfHobList; | |
| ((EFI_HOB_GENERIC_HEADER *)Hob)->HobType = HobType; | |
| ((EFI_HOB_GENERIC_HEADER *)Hob)->HobLength = HobLength; | |
| ((EFI_HOB_GENERIC_HEADER *)Hob)->Reserved = 0; | |
| HobEnd = (EFI_HOB_GENERIC_HEADER *)((UINTN)Hob + HobLength); | |
| HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd; | |
| HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST; | |
| HobEnd->HobLength = sizeof (EFI_HOB_GENERIC_HEADER); | |
| HobEnd->Reserved = 0; | |
| HobEnd++; | |
| HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd; | |
| return Hob; | |
| } |