StandaloneMmCoreHobLib.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /** @file
  2. HOB Library implementation for Standalone MM Core.
  3. Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>
  4. Copyright (c) 2017 - 2018, ARM Limited. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <PiMm.h>
  8. #include <Library/HobLib.h>
  9. #include <Library/DebugLib.h>
  10. #include <Library/BaseMemoryLib.h>
  11. #include <Guid/MemoryAllocationHob.h>
  12. //
  13. // Cache copy of HobList pointer.
  14. //
  15. VOID *gHobList = NULL;
  16. VOID *
  17. CreateHob (
  18. IN UINT16 HobType,
  19. IN UINT16 HobLength
  20. )
  21. {
  22. EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;
  23. EFI_HOB_GENERIC_HEADER *HobEnd;
  24. EFI_PHYSICAL_ADDRESS FreeMemory;
  25. VOID *Hob;
  26. HandOffHob = GetHobList ();
  27. HobLength = (UINT16)((HobLength + 0x7) & (~0x7));
  28. FreeMemory = HandOffHob->EfiFreeMemoryTop - HandOffHob->EfiFreeMemoryBottom;
  29. if (FreeMemory < HobLength) {
  30. return NULL;
  31. }
  32. Hob = (VOID*) (UINTN) HandOffHob->EfiEndOfHobList;
  33. ((EFI_HOB_GENERIC_HEADER*) Hob)->HobType = HobType;
  34. ((EFI_HOB_GENERIC_HEADER*) Hob)->HobLength = HobLength;
  35. ((EFI_HOB_GENERIC_HEADER*) Hob)->Reserved = 0;
  36. HobEnd = (EFI_HOB_GENERIC_HEADER*) ((UINTN)Hob + HobLength);
  37. HandOffHob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
  38. HobEnd->HobType = EFI_HOB_TYPE_END_OF_HOB_LIST;
  39. HobEnd->HobLength = sizeof (EFI_HOB_GENERIC_HEADER);
  40. HobEnd->Reserved = 0;
  41. HobEnd++;
  42. HandOffHob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;
  43. return Hob;
  44. }
  45. /**
  46. Builds a HOB for a loaded PE32 module.
  47. This function builds a HOB for a loaded PE32 module.
  48. If ModuleName is NULL, then ASSERT().
  49. If there is no additional space for HOB creation, then ASSERT().
  50. @param ModuleName The GUID File Name of the module.
  51. @param MemoryAllocationModule The 64 bit physical address of the module.
  52. @param ModuleLength The length of the module in bytes.
  53. @param EntryPoint The 64 bit physical address of the module entry point.
  54. **/
  55. VOID
  56. EFIAPI
  57. BuildModuleHob (
  58. IN CONST EFI_GUID *ModuleName,
  59. IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule,
  60. IN UINT64 ModuleLength,
  61. IN EFI_PHYSICAL_ADDRESS EntryPoint
  62. )
  63. {
  64. EFI_HOB_MEMORY_ALLOCATION_MODULE *Hob;
  65. ASSERT (((MemoryAllocationModule & (EFI_PAGE_SIZE - 1)) == 0) &&
  66. ((ModuleLength & (EFI_PAGE_SIZE - 1)) == 0));
  67. Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION_MODULE));
  68. CopyGuid (&(Hob->MemoryAllocationHeader.Name), &gEfiHobMemoryAllocModuleGuid);
  69. Hob->MemoryAllocationHeader.MemoryBaseAddress = MemoryAllocationModule;
  70. Hob->MemoryAllocationHeader.MemoryLength = ModuleLength;
  71. Hob->MemoryAllocationHeader.MemoryType = EfiBootServicesCode;
  72. //
  73. // Zero the reserved space to match HOB spec
  74. //
  75. ZeroMem (Hob->MemoryAllocationHeader.Reserved, sizeof (Hob->MemoryAllocationHeader.Reserved));
  76. CopyGuid (&Hob->ModuleName, ModuleName);
  77. Hob->EntryPoint = EntryPoint;
  78. }
  79. /**
  80. Builds a HOB that describes a chunk of system memory.
  81. This function builds a HOB that describes a chunk of system memory.
  82. If there is no additional space for HOB creation, then ASSERT().
  83. @param ResourceType The type of resource described by this HOB.
  84. @param ResourceAttribute The resource attributes of the memory described by this HOB.
  85. @param PhysicalStart The 64 bit physical address of memory described by this HOB.
  86. @param NumberOfBytes The length of the memory described by this HOB in bytes.
  87. **/
  88. VOID
  89. EFIAPI
  90. BuildResourceDescriptorHob (
  91. IN EFI_RESOURCE_TYPE ResourceType,
  92. IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute,
  93. IN EFI_PHYSICAL_ADDRESS PhysicalStart,
  94. IN UINT64 NumberOfBytes
  95. )
  96. {
  97. EFI_HOB_RESOURCE_DESCRIPTOR *Hob;
  98. Hob = CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof (EFI_HOB_RESOURCE_DESCRIPTOR));
  99. ASSERT (Hob != NULL);
  100. Hob->ResourceType = ResourceType;
  101. Hob->ResourceAttribute = ResourceAttribute;
  102. Hob->PhysicalStart = PhysicalStart;
  103. Hob->ResourceLength = NumberOfBytes;
  104. }
  105. /**
  106. Builds a GUID HOB with a certain data length.
  107. This function builds a customized HOB tagged with a GUID for identification
  108. and returns the start address of GUID HOB data so that caller can fill the customized data.
  109. The HOB Header and Name field is already stripped.
  110. If Guid is NULL, then ASSERT().
  111. If there is no additional space for HOB creation, then ASSERT().
  112. If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
  113. @param Guid The GUID to tag the customized HOB.
  114. @param DataLength The size of the data payload for the GUID HOB.
  115. @return The start address of GUID HOB data.
  116. **/
  117. VOID *
  118. EFIAPI
  119. BuildGuidHob (
  120. IN CONST EFI_GUID *Guid,
  121. IN UINTN DataLength
  122. )
  123. {
  124. EFI_HOB_GUID_TYPE *Hob;
  125. //
  126. // Make sure that data length is not too long.
  127. //
  128. ASSERT (DataLength <= (0xffff - sizeof (EFI_HOB_GUID_TYPE)));
  129. Hob = CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16) (sizeof (EFI_HOB_GUID_TYPE) + DataLength));
  130. CopyGuid (&Hob->Name, Guid);
  131. return Hob + 1;
  132. }
  133. /**
  134. Copies a data buffer to a newly-built HOB.
  135. This function builds a customized HOB tagged with a GUID for identification,
  136. copies the input data to the HOB data field and returns the start address of the GUID HOB data.
  137. The HOB Header and Name field is already stripped.
  138. If Guid is NULL, then ASSERT().
  139. If Data is NULL and DataLength > 0, then ASSERT().
  140. If there is no additional space for HOB creation, then ASSERT().
  141. If DataLength >= (0x10000 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
  142. @param Guid The GUID to tag the customized HOB.
  143. @param Data The data to be copied into the data field of the GUID HOB.
  144. @param DataLength The size of the data payload for the GUID HOB.
  145. @return The start address of GUID HOB data.
  146. **/
  147. VOID *
  148. EFIAPI
  149. BuildGuidDataHob (
  150. IN CONST EFI_GUID *Guid,
  151. IN VOID *Data,
  152. IN UINTN DataLength
  153. )
  154. {
  155. VOID *HobData;
  156. ASSERT (Data != NULL || DataLength == 0);
  157. HobData = BuildGuidHob (Guid, DataLength);
  158. return CopyMem (HobData, Data, DataLength);
  159. }
  160. /**
  161. Builds a Firmware Volume HOB.
  162. This function builds a Firmware Volume HOB.
  163. If there is no additional space for HOB creation, then ASSERT().
  164. @param BaseAddress The base address of the Firmware Volume.
  165. @param Length The size of the Firmware Volume in bytes.
  166. **/
  167. VOID
  168. EFIAPI
  169. BuildFvHob (
  170. IN EFI_PHYSICAL_ADDRESS BaseAddress,
  171. IN UINT64 Length
  172. )
  173. {
  174. EFI_HOB_FIRMWARE_VOLUME *Hob;
  175. Hob = CreateHob (EFI_HOB_TYPE_FV, sizeof (EFI_HOB_FIRMWARE_VOLUME));
  176. Hob->BaseAddress = BaseAddress;
  177. Hob->Length = Length;
  178. }
  179. /**
  180. Builds a EFI_HOB_TYPE_FV2 HOB.
  181. This function builds a EFI_HOB_TYPE_FV2 HOB.
  182. If there is no additional space for HOB creation, then ASSERT().
  183. @param BaseAddress The base address of the Firmware Volume.
  184. @param Length The size of the Firmware Volume in bytes.
  185. @param FvName The name of the Firmware Volume.
  186. @param FileName The name of the file.
  187. **/
  188. VOID
  189. EFIAPI
  190. BuildFv2Hob (
  191. IN EFI_PHYSICAL_ADDRESS BaseAddress,
  192. IN UINT64 Length,
  193. IN CONST EFI_GUID *FvName,
  194. IN CONST EFI_GUID *FileName
  195. )
  196. {
  197. EFI_HOB_FIRMWARE_VOLUME2 *Hob;
  198. Hob = CreateHob (EFI_HOB_TYPE_FV2, sizeof (EFI_HOB_FIRMWARE_VOLUME2));
  199. Hob->BaseAddress = BaseAddress;
  200. Hob->Length = Length;
  201. CopyGuid (&Hob->FvName, FvName);
  202. CopyGuid (&Hob->FileName, FileName);
  203. }
  204. /**
  205. Builds a HOB for the CPU.
  206. This function builds a HOB for the CPU.
  207. If there is no additional space for HOB creation, then ASSERT().
  208. @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
  209. @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
  210. **/
  211. VOID
  212. EFIAPI
  213. BuildCpuHob (
  214. IN UINT8 SizeOfMemorySpace,
  215. IN UINT8 SizeOfIoSpace
  216. )
  217. {
  218. EFI_HOB_CPU *Hob;
  219. Hob = CreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));
  220. Hob->SizeOfMemorySpace = SizeOfMemorySpace;
  221. Hob->SizeOfIoSpace = SizeOfIoSpace;
  222. //
  223. // Zero the reserved space to match HOB spec
  224. //
  225. ZeroMem (Hob->Reserved, sizeof (Hob->Reserved));
  226. }
  227. /**
  228. Builds a HOB for the memory allocation.
  229. This function builds a HOB for the memory allocation.
  230. If there is no additional space for HOB creation, then ASSERT().
  231. @param BaseAddress The 64 bit physical address of the memory.
  232. @param Length The length of the memory allocation in bytes.
  233. @param MemoryType Type of memory allocated by this HOB.
  234. **/
  235. VOID
  236. EFIAPI
  237. BuildMemoryAllocationHob (
  238. IN EFI_PHYSICAL_ADDRESS BaseAddress,
  239. IN UINT64 Length,
  240. IN EFI_MEMORY_TYPE MemoryType
  241. )
  242. {
  243. EFI_HOB_MEMORY_ALLOCATION *Hob;
  244. ASSERT (((BaseAddress & (EFI_PAGE_SIZE - 1)) == 0) &&
  245. ((Length & (EFI_PAGE_SIZE - 1)) == 0));
  246. Hob = CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMORY_ALLOCATION));
  247. ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));
  248. Hob->AllocDescriptor.MemoryBaseAddress = BaseAddress;
  249. Hob->AllocDescriptor.MemoryLength = Length;
  250. Hob->AllocDescriptor.MemoryType = MemoryType;
  251. //
  252. // Zero the reserved space to match HOB spec
  253. //
  254. ZeroMem (Hob->AllocDescriptor.Reserved, sizeof (Hob->AllocDescriptor.Reserved));
  255. }