HobLib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /** @file
  2. HOB Library implementation for Dxe Phase with DebugLib dependency removed
  3. Copyright (c) 2022, Loongson Limited. All rights reserved.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #define ASSERT(Expression) \
  7. do { \
  8. if (!(Expression)) { \
  9. CpuDeadLoop (); \
  10. } \
  11. } while (FALSE)
  12. #include <PiDxe.h>
  13. #include <Guid/HobList.h>
  14. #include <Library/HobLib.h>
  15. #include <Library/UefiLib.h>
  16. #include <Library/BaseMemoryLib.h>
  17. VOID *mHobList = NULL;
  18. /**
  19. The constructor function caches the pointer to HOB list.
  20. The constructor function gets the start address of HOB list from system configuration table.
  21. @param ImageHandle The firmware allocated handle for the EFI image.
  22. @param SystemTable A pointer to the EFI System Table.
  23. @retval EFI_SUCCESS The constructor successfully gets HobList.
  24. @retval Other value The constructor can't get HobList.
  25. **/
  26. EFI_STATUS
  27. EFIAPI
  28. HobLibConstructor (
  29. IN EFI_HANDLE ImageHandle,
  30. IN EFI_SYSTEM_TABLE *SystemTable
  31. )
  32. {
  33. UINTN Index;
  34. for (Index = 0; Index < SystemTable->NumberOfTableEntries; Index++) {
  35. if (CompareGuid (&gEfiHobListGuid, &(SystemTable->ConfigurationTable[Index].VendorGuid))) {
  36. mHobList = SystemTable->ConfigurationTable[Index].VendorTable;
  37. return EFI_SUCCESS;
  38. }
  39. }
  40. return EFI_NOT_FOUND;
  41. }
  42. /**
  43. Returns the pointer to the HOB list.
  44. This function returns the pointer to first HOB in the list.
  45. For PEI phase, the PEI service GetHobList() can be used to retrieve the pointer
  46. to the HOB list. For the DXE phase, the HOB list pointer can be retrieved through
  47. the EFI System Table by looking up theHOB list GUID in the System Configuration Table.
  48. Since the System Configuration Table does not exist that the time the DXE Core is
  49. launched, the DXE Core uses a global variable from the DXE Core Entry Point Library
  50. to manage the pointer to the HOB list.
  51. If the pointer to the HOB list is NULL, then ASSERT().
  52. @return The pointer to the HOB list.
  53. **/
  54. VOID *
  55. EFIAPI
  56. GetHobList (
  57. VOID
  58. )
  59. {
  60. ASSERT (mHobList != NULL);
  61. return mHobList;
  62. }
  63. /**
  64. Returns the next instance of a HOB type from the starting HOB.
  65. This function searches the first instance of a HOB type from the starting HOB pointer.
  66. If there does not exist such HOB type from the starting HOB pointer, it will return NULL.
  67. In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
  68. unconditionally: it returns HobStart back if HobStart itself meets the requirement;
  69. caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
  70. If HobStart is NULL, then ASSERT().
  71. @param Type The HOB type to return.
  72. @param HobStart The starting HOB pointer to search from.
  73. @return The next instance of a HOB type from the starting HOB.
  74. **/
  75. VOID *
  76. EFIAPI
  77. GetNextHob (
  78. IN UINT16 Type,
  79. IN CONST VOID *HobStart
  80. )
  81. {
  82. EFI_PEI_HOB_POINTERS Hob;
  83. ASSERT (HobStart != NULL);
  84. Hob.Raw = (UINT8 *)HobStart;
  85. //
  86. // Parse the HOB list until end of list or matching type is found.
  87. //
  88. while (!END_OF_HOB_LIST (Hob)) {
  89. if (Hob.Header->HobType == Type) {
  90. return Hob.Raw;
  91. }
  92. Hob.Raw = GET_NEXT_HOB (Hob);
  93. }
  94. return NULL;
  95. }
  96. /**
  97. Returns the first instance of a HOB type among the whole HOB list.
  98. This function searches the first instance of a HOB type among the whole HOB list.
  99. If there does not exist such HOB type in the HOB list, it will return NULL.
  100. If the pointer to the HOB list is NULL, then ASSERT().
  101. @param Type The HOB type to return.
  102. @return The next instance of a HOB type from the starting HOB.
  103. **/
  104. VOID *
  105. EFIAPI
  106. GetFirstHob (
  107. IN UINT16 Type
  108. )
  109. {
  110. VOID *HobList;
  111. HobList = GetHobList ();
  112. return GetNextHob (Type, HobList);
  113. }
  114. /**
  115. Returns the next instance of the matched GUID HOB from the starting HOB.
  116. This function searches the first instance of a HOB from the starting HOB pointer.
  117. Such HOB should satisfy two conditions:
  118. its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
  119. If there does not exist such HOB from the starting HOB pointer, it will return NULL.
  120. Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
  121. to extract the data section and its size information, respectively.
  122. In contrast with macro GET_NEXT_HOB(), this function does not skip the starting HOB pointer
  123. unconditionally: it returns HobStart back if HobStart itself meets the requirement;
  124. caller is required to use GET_NEXT_HOB() if it wishes to skip current HobStart.
  125. If Guid is NULL, then ASSERT().
  126. If HobStart is NULL, then ASSERT().
  127. @param Guid The GUID to match with in the HOB list.
  128. @param HobStart A pointer to a Guid.
  129. @return The next instance of the matched GUID HOB from the starting HOB.
  130. **/
  131. VOID *
  132. EFIAPI
  133. GetNextGuidHob (
  134. IN CONST EFI_GUID *Guid,
  135. IN CONST VOID *HobStart
  136. )
  137. {
  138. EFI_PEI_HOB_POINTERS GuidHob;
  139. GuidHob.Raw = (UINT8 *)HobStart;
  140. while ((GuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION, GuidHob.Raw)) != NULL) {
  141. if (CompareGuid (Guid, &GuidHob.Guid->Name)) {
  142. break;
  143. }
  144. GuidHob.Raw = GET_NEXT_HOB (GuidHob);
  145. }
  146. return GuidHob.Raw;
  147. }
  148. /**
  149. Returns the first instance of the matched GUID HOB among the whole HOB list.
  150. This function searches the first instance of a HOB among the whole HOB list.
  151. Such HOB should satisfy two conditions:
  152. its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name equals to the input Guid.
  153. If there does not exist such HOB from the starting HOB pointer, it will return NULL.
  154. Caller is required to apply GET_GUID_HOB_DATA () and GET_GUID_HOB_DATA_SIZE ()
  155. to extract the data section and its size information, respectively.
  156. If the pointer to the HOB list is NULL, then ASSERT().
  157. If Guid is NULL, then ASSERT().
  158. @param Guid The GUID to match with in the HOB list.
  159. @return The first instance of the matched GUID HOB among the whole HOB list.
  160. **/
  161. VOID *
  162. EFIAPI
  163. GetFirstGuidHob (
  164. IN CONST EFI_GUID *Guid
  165. )
  166. {
  167. VOID *HobList;
  168. HobList = GetHobList ();
  169. return GetNextGuidHob (Guid, HobList);
  170. }
  171. /**
  172. Get the system boot mode from the HOB list.
  173. This function returns the system boot mode information from the
  174. PHIT HOB in HOB list.
  175. If the pointer to the HOB list is NULL, then ASSERT().
  176. @param VOID
  177. @return The Boot Mode.
  178. **/
  179. EFI_BOOT_MODE
  180. EFIAPI
  181. GetBootModeHob (
  182. VOID
  183. )
  184. {
  185. EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;
  186. HandOffHob = (EFI_HOB_HANDOFF_INFO_TABLE *)GetHobList ();
  187. return HandOffHob->BootMode;
  188. }
  189. /**
  190. Builds a HOB for a loaded PE32 module.
  191. This function builds a HOB for a loaded PE32 module.
  192. It can only be invoked during PEI phase;
  193. for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
  194. If ModuleName is NULL, then ASSERT().
  195. If there is no additional space for HOB creation, then ASSERT().
  196. @param ModuleName The GUID File Name of the module.
  197. @param MemoryAllocationModule The 64 bit physical address of the module.
  198. @param ModuleLength The length of the module in bytes.
  199. @param EntryPoint The 64 bit physical address of the module entry point.
  200. **/
  201. VOID
  202. EFIAPI
  203. BuildModuleHob (
  204. IN CONST EFI_GUID *ModuleName,
  205. IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule,
  206. IN UINT64 ModuleLength,
  207. IN EFI_PHYSICAL_ADDRESS EntryPoint
  208. )
  209. {
  210. //
  211. // PEI HOB is read only for DXE phase
  212. //
  213. ASSERT (FALSE);
  214. }
  215. /**
  216. Builds a HOB that describes a chunk of system memory.
  217. This function builds a HOB that describes a chunk of system memory.
  218. It can only be invoked during PEI phase;
  219. for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
  220. If there is no additional space for HOB creation, then ASSERT().
  221. @param ResourceType The type of resource described by this HOB.
  222. @param ResourceAttribute The resource attributes of the memory described by this HOB.
  223. @param PhysicalStart The 64 bit physical address of memory described by this HOB.
  224. @param NumberOfBytes The length of the memory described by this HOB in bytes.
  225. **/
  226. VOID
  227. EFIAPI
  228. BuildResourceDescriptorHob (
  229. IN EFI_RESOURCE_TYPE ResourceType,
  230. IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute,
  231. IN EFI_PHYSICAL_ADDRESS PhysicalStart,
  232. IN UINT64 NumberOfBytes
  233. )
  234. {
  235. //
  236. // PEI HOB is read only for DXE phase
  237. //
  238. ASSERT (FALSE);
  239. }
  240. /**
  241. Builds a customized HOB tagged with a GUID for identification and returns
  242. the start address of GUID HOB data.
  243. This function builds a customized HOB tagged with a GUID for identification
  244. and returns the start address of GUID HOB data so that caller can fill the customized data.
  245. The HOB Header and Name field is already stripped.
  246. It can only be invoked during PEI phase;
  247. for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
  248. If Guid is NULL, then ASSERT().
  249. If there is no additional space for HOB creation, then ASSERT().
  250. If DataLength > (0xFFF8 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
  251. HobLength is UINT16 and multiples of 8 bytes, so the max HobLength is 0xFFF8.
  252. @param Guid The GUID to tag the customized HOB.
  253. @param DataLength The size of the data payload for the GUID HOB.
  254. @retval NULL The GUID HOB could not be allocated.
  255. @retval others The start address of GUID HOB data.
  256. **/
  257. VOID *
  258. EFIAPI
  259. BuildGuidHob (
  260. IN CONST EFI_GUID *Guid,
  261. IN UINTN DataLength
  262. )
  263. {
  264. //
  265. // PEI HOB is read only for DXE phase
  266. //
  267. ASSERT (FALSE);
  268. return NULL;
  269. }
  270. /**
  271. Builds a customized HOB tagged with a GUID for identification, copies the input data to the HOB
  272. data field, and returns the start address of the GUID HOB data.
  273. This function builds a customized HOB tagged with a GUID for identification and copies the input
  274. data to the HOB data field and returns the start address of the GUID HOB data. It can only be
  275. invoked during PEI phase; for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
  276. The HOB Header and Name field is already stripped.
  277. It can only be invoked during PEI phase;
  278. for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
  279. If Guid is NULL, then ASSERT().
  280. If Data is NULL and DataLength > 0, then ASSERT().
  281. If there is no additional space for HOB creation, then ASSERT().
  282. If DataLength > (0xFFF8 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().
  283. HobLength is UINT16 and multiples of 8 bytes, so the max HobLength is 0xFFF8.
  284. @param Guid The GUID to tag the customized HOB.
  285. @param Data The data to be copied into the data field of the GUID HOB.
  286. @param DataLength The size of the data payload for the GUID HOB.
  287. @retval NULL The GUID HOB could not be allocated.
  288. @retval others The start address of GUID HOB data.
  289. **/
  290. VOID *
  291. EFIAPI
  292. BuildGuidDataHob (
  293. IN CONST EFI_GUID *Guid,
  294. IN VOID *Data,
  295. IN UINTN DataLength
  296. )
  297. {
  298. //
  299. // PEI HOB is read only for DXE phase
  300. //
  301. ASSERT (FALSE);
  302. return NULL;
  303. }
  304. /**
  305. Builds a Firmware Volume HOB.
  306. This function builds a Firmware Volume HOB.
  307. It can only be invoked during PEI phase;
  308. for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
  309. If there is no additional space for HOB creation, then ASSERT().
  310. If the FvImage buffer is not at its required alignment, then ASSERT().
  311. @param BaseAddress The base address of the Firmware Volume.
  312. @param Length The size of the Firmware Volume in bytes.
  313. **/
  314. VOID
  315. EFIAPI
  316. BuildFvHob (
  317. IN EFI_PHYSICAL_ADDRESS BaseAddress,
  318. IN UINT64 Length
  319. )
  320. {
  321. //
  322. // PEI HOB is read only for DXE phase
  323. //
  324. ASSERT (FALSE);
  325. }
  326. /**
  327. Builds a EFI_HOB_TYPE_FV2 HOB.
  328. This function builds a EFI_HOB_TYPE_FV2 HOB.
  329. It can only be invoked during PEI phase;
  330. for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
  331. If there is no additional space for HOB creation, then ASSERT().
  332. If the FvImage buffer is not at its required alignment, then ASSERT().
  333. @param BaseAddress The base address of the Firmware Volume.
  334. @param Length The size of the Firmware Volume in bytes.
  335. @param FvName The name of the Firmware Volume.
  336. @param FileName The name of the file.
  337. **/
  338. VOID
  339. EFIAPI
  340. BuildFv2Hob (
  341. IN EFI_PHYSICAL_ADDRESS BaseAddress,
  342. IN UINT64 Length,
  343. IN CONST EFI_GUID *FvName,
  344. IN CONST EFI_GUID *FileName
  345. )
  346. {
  347. ASSERT (FALSE);
  348. }
  349. /**
  350. Builds a EFI_HOB_TYPE_FV3 HOB.
  351. This function builds a EFI_HOB_TYPE_FV3 HOB.
  352. It can only be invoked during PEI phase;
  353. for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
  354. If there is no additional space for HOB creation, then ASSERT().
  355. If the FvImage buffer is not at its required alignment, then ASSERT().
  356. @param BaseAddress The base address of the Firmware Volume.
  357. @param Length The size of the Firmware Volume in bytes.
  358. @param AuthenticationStatus The authentication status.
  359. @param ExtractedFv TRUE if the FV was extracted as a file within
  360. another firmware volume. FALSE otherwise.
  361. @param FvName The name of the Firmware Volume.
  362. Valid only if IsExtractedFv is TRUE.
  363. @param FileName The name of the file.
  364. Valid only if IsExtractedFv is TRUE.
  365. **/
  366. VOID
  367. EFIAPI
  368. BuildFv3Hob (
  369. IN EFI_PHYSICAL_ADDRESS BaseAddress,
  370. IN UINT64 Length,
  371. IN UINT32 AuthenticationStatus,
  372. IN BOOLEAN ExtractedFv,
  373. IN CONST EFI_GUID *FvName OPTIONAL,
  374. IN CONST EFI_GUID *FileName OPTIONAL
  375. )
  376. {
  377. ASSERT (FALSE);
  378. }
  379. /**
  380. Builds a Capsule Volume HOB.
  381. This function builds a Capsule Volume HOB.
  382. It can only be invoked during PEI phase;
  383. for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
  384. If the platform does not support Capsule Volume HOBs, then ASSERT().
  385. If there is no additional space for HOB creation, then ASSERT().
  386. @param BaseAddress The base address of the Capsule Volume.
  387. @param Length The size of the Capsule Volume in bytes.
  388. **/
  389. VOID
  390. EFIAPI
  391. BuildCvHob (
  392. IN EFI_PHYSICAL_ADDRESS BaseAddress,
  393. IN UINT64 Length
  394. )
  395. {
  396. //
  397. // PEI HOB is read only for DXE phase
  398. //
  399. ASSERT (FALSE);
  400. }
  401. /**
  402. Builds a HOB for the CPU.
  403. This function builds a HOB for the CPU.
  404. It can only be invoked during PEI phase;
  405. for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
  406. If there is no additional space for HOB creation, then ASSERT().
  407. @param SizeOfMemorySpace The maximum physical memory addressability of the processor.
  408. @param SizeOfIoSpace The maximum physical I/O addressability of the processor.
  409. **/
  410. VOID
  411. EFIAPI
  412. BuildCpuHob (
  413. IN UINT8 SizeOfMemorySpace,
  414. IN UINT8 SizeOfIoSpace
  415. )
  416. {
  417. //
  418. // PEI HOB is read only for DXE phase
  419. //
  420. ASSERT (FALSE);
  421. }
  422. /**
  423. Builds a HOB for the Stack.
  424. This function builds a HOB for the stack.
  425. It can only be invoked during PEI phase;
  426. for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
  427. If there is no additional space for HOB creation, then ASSERT().
  428. @param BaseAddress The 64 bit physical address of the Stack.
  429. @param Length The length of the stack in bytes.
  430. **/
  431. VOID
  432. EFIAPI
  433. BuildStackHob (
  434. IN EFI_PHYSICAL_ADDRESS BaseAddress,
  435. IN UINT64 Length
  436. )
  437. {
  438. //
  439. // PEI HOB is read only for DXE phase
  440. //
  441. ASSERT (FALSE);
  442. }
  443. /**
  444. Builds a HOB for the BSP store.
  445. This function builds a HOB for BSP store.
  446. It can only be invoked during PEI phase;
  447. for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
  448. If there is no additional space for HOB creation, then ASSERT().
  449. @param BaseAddress The 64 bit physical address of the BSP.
  450. @param Length The length of the BSP store in bytes.
  451. @param MemoryType Type of memory allocated by this HOB.
  452. **/
  453. VOID
  454. EFIAPI
  455. BuildBspStoreHob (
  456. IN EFI_PHYSICAL_ADDRESS BaseAddress,
  457. IN UINT64 Length,
  458. IN EFI_MEMORY_TYPE MemoryType
  459. )
  460. {
  461. //
  462. // PEI HOB is read only for DXE phase
  463. //
  464. ASSERT (FALSE);
  465. }
  466. /**
  467. Builds a HOB for the memory allocation.
  468. This function builds a HOB for the memory allocation.
  469. It can only be invoked during PEI phase;
  470. for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.
  471. If there is no additional space for HOB creation, then ASSERT().
  472. @param BaseAddress The 64 bit physical address of the memory.
  473. @param Length The length of the memory allocation in bytes.
  474. @param MemoryType Type of memory allocated by this HOB.
  475. **/
  476. VOID
  477. EFIAPI
  478. BuildMemoryAllocationHob (
  479. IN EFI_PHYSICAL_ADDRESS BaseAddress,
  480. IN UINT64 Length,
  481. IN EFI_MEMORY_TYPE MemoryType
  482. )
  483. {
  484. //
  485. // PEI HOB is read only for DXE phase
  486. //
  487. ASSERT (FALSE);
  488. }