LoadDxeCore.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /** @file
  2. Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include "UefiPayloadEntry.h"
  6. /**
  7. Allocate pages for code.
  8. @param[in] Pages Number of pages to be allocated.
  9. @return Allocated memory.
  10. **/
  11. VOID *
  12. AllocateCodePages (
  13. IN UINTN Pages
  14. )
  15. {
  16. VOID *Alloc;
  17. EFI_PEI_HOB_POINTERS Hob;
  18. Alloc = AllocatePages (Pages);
  19. if (Alloc == NULL) {
  20. return NULL;
  21. }
  22. // find the HOB we just created, and change the type to EfiBootServicesCode
  23. Hob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);
  24. while (Hob.Raw != NULL) {
  25. if (Hob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress == (UINTN)Alloc) {
  26. Hob.MemoryAllocation->AllocDescriptor.MemoryType = EfiBootServicesCode;
  27. return Alloc;
  28. }
  29. Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, GET_NEXT_HOB (Hob));
  30. }
  31. ASSERT (FALSE);
  32. FreePages (Alloc, Pages);
  33. return NULL;
  34. }
  35. /**
  36. Loads and relocates a PE/COFF image
  37. @param[in] PeCoffImage Point to a Pe/Coff image.
  38. @param[out] ImageAddress The image memory address after relocation.
  39. @param[out] ImageSize The image size.
  40. @param[out] EntryPoint The image entry point.
  41. @return EFI_SUCCESS If the image is loaded and relocated successfully.
  42. @return Others If the image failed to load or relocate.
  43. **/
  44. EFI_STATUS
  45. LoadPeCoffImage (
  46. IN VOID *PeCoffImage,
  47. OUT EFI_PHYSICAL_ADDRESS *ImageAddress,
  48. OUT UINT64 *ImageSize,
  49. OUT EFI_PHYSICAL_ADDRESS *EntryPoint
  50. )
  51. {
  52. RETURN_STATUS Status;
  53. PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
  54. VOID *Buffer;
  55. ZeroMem (&ImageContext, sizeof (ImageContext));
  56. ImageContext.Handle = PeCoffImage;
  57. ImageContext.ImageRead = PeCoffLoaderImageReadFromMemory;
  58. Status = PeCoffLoaderGetImageInfo (&ImageContext);
  59. if (EFI_ERROR (Status)) {
  60. ASSERT_EFI_ERROR (Status);
  61. return Status;
  62. }
  63. //
  64. // Allocate Memory for the image
  65. //
  66. Buffer = AllocateCodePages (EFI_SIZE_TO_PAGES ((UINT32)ImageContext.ImageSize));
  67. if (Buffer == NULL) {
  68. return EFI_OUT_OF_RESOURCES;
  69. }
  70. ImageContext.ImageAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer;
  71. //
  72. // Load the image to our new buffer
  73. //
  74. Status = PeCoffLoaderLoadImage (&ImageContext);
  75. if (EFI_ERROR (Status)) {
  76. ASSERT_EFI_ERROR (Status);
  77. return Status;
  78. }
  79. //
  80. // Relocate the image in our new buffer
  81. //
  82. Status = PeCoffLoaderRelocateImage (&ImageContext);
  83. if (EFI_ERROR (Status)) {
  84. ASSERT_EFI_ERROR (Status);
  85. return Status;
  86. }
  87. *ImageAddress = ImageContext.ImageAddress;
  88. *ImageSize = ImageContext.ImageSize;
  89. *EntryPoint = ImageContext.EntryPoint;
  90. return EFI_SUCCESS;
  91. }
  92. /**
  93. This function searchs a given file type with a given Guid within a valid FV.
  94. If input Guid is NULL, will locate the first section having the given file type
  95. @param FvHeader A pointer to firmware volume header that contains the set of files
  96. to be searched.
  97. @param FileType File type to be searched.
  98. @param Guid Will ignore if it is NULL.
  99. @param FileHeader A pointer to the discovered file, if successful.
  100. @retval EFI_SUCCESS Successfully found FileType
  101. @retval EFI_NOT_FOUND File type can't be found.
  102. **/
  103. EFI_STATUS
  104. FvFindFileByTypeGuid (
  105. IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader,
  106. IN EFI_FV_FILETYPE FileType,
  107. IN EFI_GUID *Guid OPTIONAL,
  108. OUT EFI_FFS_FILE_HEADER **FileHeader
  109. )
  110. {
  111. EFI_PHYSICAL_ADDRESS CurrentAddress;
  112. EFI_PHYSICAL_ADDRESS EndOfFirmwareVolume;
  113. EFI_FFS_FILE_HEADER *File;
  114. UINT32 Size;
  115. EFI_PHYSICAL_ADDRESS EndOfFile;
  116. CurrentAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)FvHeader;
  117. EndOfFirmwareVolume = CurrentAddress + FvHeader->FvLength;
  118. //
  119. // Loop through the FFS files
  120. //
  121. for (EndOfFile = CurrentAddress + FvHeader->HeaderLength; ; ) {
  122. CurrentAddress = (EndOfFile + 7) & 0xfffffffffffffff8ULL;
  123. if (CurrentAddress > EndOfFirmwareVolume) {
  124. break;
  125. }
  126. File = (EFI_FFS_FILE_HEADER *)(UINTN)CurrentAddress;
  127. if (IS_FFS_FILE2 (File)) {
  128. Size = FFS_FILE2_SIZE (File);
  129. if (Size <= 0x00FFFFFF) {
  130. break;
  131. }
  132. } else {
  133. Size = FFS_FILE_SIZE (File);
  134. if (Size < sizeof (EFI_FFS_FILE_HEADER)) {
  135. break;
  136. }
  137. }
  138. EndOfFile = CurrentAddress + Size;
  139. if (EndOfFile > EndOfFirmwareVolume) {
  140. break;
  141. }
  142. //
  143. // Look for file type
  144. //
  145. if (File->Type == FileType) {
  146. if ((Guid == NULL) || CompareGuid (&File->Name, Guid)) {
  147. *FileHeader = File;
  148. return EFI_SUCCESS;
  149. }
  150. }
  151. }
  152. return EFI_NOT_FOUND;
  153. }
  154. /**
  155. This function searchs a given section type within a valid FFS file.
  156. @param FileHeader A pointer to the file header that contains the set of sections to
  157. be searched.
  158. @param SectionType The value of the section type to search.
  159. @param SectionData A pointer to the discovered section, if successful.
  160. @retval EFI_SUCCESS The section was found.
  161. @retval EFI_NOT_FOUND The section was not found.
  162. **/
  163. EFI_STATUS
  164. FileFindSection (
  165. IN EFI_FFS_FILE_HEADER *FileHeader,
  166. IN EFI_SECTION_TYPE SectionType,
  167. OUT VOID **SectionData
  168. )
  169. {
  170. UINT32 FileSize;
  171. EFI_COMMON_SECTION_HEADER *Section;
  172. UINT32 SectionSize;
  173. UINT32 Index;
  174. if (IS_FFS_FILE2 (FileHeader)) {
  175. FileSize = FFS_FILE2_SIZE (FileHeader);
  176. } else {
  177. FileSize = FFS_FILE_SIZE (FileHeader);
  178. }
  179. FileSize -= sizeof (EFI_FFS_FILE_HEADER);
  180. Section = (EFI_COMMON_SECTION_HEADER *)(FileHeader + 1);
  181. Index = 0;
  182. while (Index < FileSize) {
  183. if (Section->Type == SectionType) {
  184. if (IS_SECTION2 (Section)) {
  185. *SectionData = (VOID *)((UINT8 *)Section + sizeof (EFI_COMMON_SECTION_HEADER2));
  186. } else {
  187. *SectionData = (VOID *)((UINT8 *)Section + sizeof (EFI_COMMON_SECTION_HEADER));
  188. }
  189. return EFI_SUCCESS;
  190. }
  191. if (IS_SECTION2 (Section)) {
  192. SectionSize = SECTION2_SIZE (Section);
  193. } else {
  194. SectionSize = SECTION_SIZE (Section);
  195. }
  196. SectionSize = GET_OCCUPIED_SIZE (SectionSize, 4);
  197. ASSERT (SectionSize != 0);
  198. Index += SectionSize;
  199. Section = (EFI_COMMON_SECTION_HEADER *)((UINT8 *)Section + SectionSize);
  200. }
  201. return EFI_NOT_FOUND;
  202. }
  203. /**
  204. Find DXE core from FV and build DXE core HOBs.
  205. @param[out] DxeCoreEntryPoint DXE core entry point
  206. @retval EFI_SUCCESS If it completed successfully.
  207. @retval EFI_NOT_FOUND If it failed to load DXE FV.
  208. **/
  209. EFI_STATUS
  210. LoadDxeCore (
  211. OUT PHYSICAL_ADDRESS *DxeCoreEntryPoint
  212. )
  213. {
  214. EFI_STATUS Status;
  215. EFI_FIRMWARE_VOLUME_HEADER *PayloadFv;
  216. EFI_FIRMWARE_VOLUME_HEADER *DxeCoreFv;
  217. EFI_FFS_FILE_HEADER *FileHeader;
  218. VOID *PeCoffImage;
  219. EFI_PHYSICAL_ADDRESS ImageAddress;
  220. UINT64 ImageSize;
  221. PayloadFv = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)PcdGet32 (PcdPayloadFdMemBase);
  222. //
  223. // DXE FV is inside Payload FV. Here find DXE FV from Payload FV
  224. //
  225. Status = FvFindFileByTypeGuid (PayloadFv, EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, NULL, &FileHeader);
  226. if (EFI_ERROR (Status)) {
  227. return Status;
  228. }
  229. Status = FileFindSection (FileHeader, EFI_SECTION_FIRMWARE_VOLUME_IMAGE, (VOID **)&DxeCoreFv);
  230. if (EFI_ERROR (Status)) {
  231. return Status;
  232. }
  233. //
  234. // Report DXE FV to DXE core
  235. //
  236. BuildFvHob ((EFI_PHYSICAL_ADDRESS)(UINTN)DxeCoreFv, DxeCoreFv->FvLength);
  237. //
  238. // Find DXE core file from DXE FV
  239. //
  240. Status = FvFindFileByTypeGuid (DxeCoreFv, EFI_FV_FILETYPE_DXE_CORE, NULL, &FileHeader);
  241. if (EFI_ERROR (Status)) {
  242. return Status;
  243. }
  244. Status = FileFindSection (FileHeader, EFI_SECTION_PE32, (VOID **)&PeCoffImage);
  245. if (EFI_ERROR (Status)) {
  246. return Status;
  247. }
  248. //
  249. // Get DXE core info
  250. //
  251. Status = LoadPeCoffImage (PeCoffImage, &ImageAddress, &ImageSize, DxeCoreEntryPoint);
  252. if (EFI_ERROR (Status)) {
  253. return Status;
  254. }
  255. BuildModuleHob (&FileHeader->Name, ImageAddress, EFI_SIZE_TO_PAGES ((UINT32)ImageSize) * EFI_PAGE_SIZE, *DxeCoreEntryPoint);
  256. return EFI_SUCCESS;
  257. }
  258. /**
  259. Find DXE core from FV and build DXE core HOBs.
  260. @param[in] DxeFv The FV where to find the DXE core.
  261. @param[out] DxeCoreEntryPoint DXE core entry point
  262. @retval EFI_SUCCESS If it completed successfully.
  263. @retval EFI_NOT_FOUND If it failed to load DXE FV.
  264. **/
  265. EFI_STATUS
  266. UniversalLoadDxeCore (
  267. IN EFI_FIRMWARE_VOLUME_HEADER *DxeFv,
  268. OUT PHYSICAL_ADDRESS *DxeCoreEntryPoint
  269. )
  270. {
  271. EFI_STATUS Status;
  272. EFI_FFS_FILE_HEADER *FileHeader;
  273. VOID *PeCoffImage;
  274. EFI_PHYSICAL_ADDRESS ImageAddress;
  275. UINT64 ImageSize;
  276. //
  277. // Find DXE core file from DXE FV
  278. //
  279. Status = FvFindFileByTypeGuid (DxeFv, EFI_FV_FILETYPE_DXE_CORE, NULL, &FileHeader);
  280. if (EFI_ERROR (Status)) {
  281. return Status;
  282. }
  283. Status = FileFindSection (FileHeader, EFI_SECTION_PE32, (VOID **)&PeCoffImage);
  284. if (EFI_ERROR (Status)) {
  285. return Status;
  286. }
  287. //
  288. // Get DXE core info
  289. //
  290. Status = LoadPeCoffImage (PeCoffImage, &ImageAddress, &ImageSize, DxeCoreEntryPoint);
  291. if (EFI_ERROR (Status)) {
  292. return Status;
  293. }
  294. BuildModuleHob (&FileHeader->Name, ImageAddress, EFI_SIZE_TO_PAGES ((UINT32)ImageSize) * EFI_PAGE_SIZE, *DxeCoreEntryPoint);
  295. return EFI_SUCCESS;
  296. }