FindPeiCore.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /** @file
  2. Locate the entry point for the PEI Core
  3. Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <PiPei.h>
  7. #include <Library/BaseLib.h>
  8. #include <Library/PeCoffGetEntryPointLib.h>
  9. #include "SecMain.h"
  10. /**
  11. Find core image base.
  12. @param BootFirmwareVolumePtr Point to the boot firmware volume.
  13. @param SecCoreImageBase The base address of the SEC core image.
  14. @param PeiCoreImageBase The base address of the PEI core image.
  15. **/
  16. EFI_STATUS
  17. EFIAPI
  18. FindImageBase (
  19. IN EFI_FIRMWARE_VOLUME_HEADER *BootFirmwareVolumePtr,
  20. OUT EFI_PHYSICAL_ADDRESS *SecCoreImageBase,
  21. OUT EFI_PHYSICAL_ADDRESS *PeiCoreImageBase
  22. )
  23. {
  24. EFI_PHYSICAL_ADDRESS CurrentAddress;
  25. EFI_PHYSICAL_ADDRESS EndOfFirmwareVolume;
  26. EFI_FFS_FILE_HEADER *File;
  27. UINT32 Size;
  28. EFI_PHYSICAL_ADDRESS EndOfFile;
  29. EFI_COMMON_SECTION_HEADER *Section;
  30. EFI_PHYSICAL_ADDRESS EndOfSection;
  31. *SecCoreImageBase = 0;
  32. *PeiCoreImageBase = 0;
  33. CurrentAddress = (EFI_PHYSICAL_ADDRESS)(UINTN) BootFirmwareVolumePtr;
  34. EndOfFirmwareVolume = CurrentAddress + BootFirmwareVolumePtr->FvLength;
  35. //
  36. // Loop through the FFS files in the Boot Firmware Volume
  37. //
  38. for (EndOfFile = CurrentAddress + BootFirmwareVolumePtr->HeaderLength; ; ) {
  39. CurrentAddress = (EndOfFile + 7) & 0xfffffffffffffff8ULL;
  40. if (CurrentAddress > EndOfFirmwareVolume) {
  41. return EFI_NOT_FOUND;
  42. }
  43. File = (EFI_FFS_FILE_HEADER*)(UINTN) CurrentAddress;
  44. if (IS_FFS_FILE2 (File)) {
  45. Size = FFS_FILE2_SIZE (File);
  46. if (Size <= 0x00FFFFFF) {
  47. return EFI_NOT_FOUND;
  48. }
  49. } else {
  50. Size = FFS_FILE_SIZE (File);
  51. if (Size < sizeof (EFI_FFS_FILE_HEADER)) {
  52. return EFI_NOT_FOUND;
  53. }
  54. }
  55. EndOfFile = CurrentAddress + Size;
  56. if (EndOfFile > EndOfFirmwareVolume) {
  57. return EFI_NOT_FOUND;
  58. }
  59. //
  60. // Look for SEC Core / PEI Core files
  61. //
  62. if (File->Type != EFI_FV_FILETYPE_SECURITY_CORE &&
  63. File->Type != EFI_FV_FILETYPE_PEI_CORE) {
  64. continue;
  65. }
  66. //
  67. // Loop through the FFS file sections within the FFS file
  68. //
  69. if (IS_FFS_FILE2 (File)) {
  70. EndOfSection = (EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) File + sizeof (EFI_FFS_FILE_HEADER2));
  71. } else {
  72. EndOfSection = (EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) File + sizeof (EFI_FFS_FILE_HEADER));
  73. }
  74. for (;;) {
  75. CurrentAddress = (EndOfSection + 3) & 0xfffffffffffffffcULL;
  76. Section = (EFI_COMMON_SECTION_HEADER*)(UINTN) CurrentAddress;
  77. if (IS_SECTION2 (Section)) {
  78. Size = SECTION2_SIZE (Section);
  79. if (Size <= 0x00FFFFFF) {
  80. return EFI_NOT_FOUND;
  81. }
  82. } else {
  83. Size = SECTION_SIZE (Section);
  84. if (Size < sizeof (EFI_COMMON_SECTION_HEADER)) {
  85. return EFI_NOT_FOUND;
  86. }
  87. }
  88. EndOfSection = CurrentAddress + Size;
  89. if (EndOfSection > EndOfFile) {
  90. return EFI_NOT_FOUND;
  91. }
  92. //
  93. // Look for executable sections
  94. //
  95. if (Section->Type == EFI_SECTION_PE32 || Section->Type == EFI_SECTION_TE) {
  96. if (File->Type == EFI_FV_FILETYPE_SECURITY_CORE) {
  97. if (IS_SECTION2 (Section)) {
  98. *SecCoreImageBase = (PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER2));
  99. } else {
  100. *SecCoreImageBase = (PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER));
  101. }
  102. } else {
  103. if (IS_SECTION2 (Section)) {
  104. *PeiCoreImageBase = (PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER2));
  105. } else {
  106. *PeiCoreImageBase = (PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) Section + sizeof (EFI_COMMON_SECTION_HEADER));
  107. }
  108. }
  109. break;
  110. }
  111. }
  112. //
  113. // Both SEC Core and PEI Core images found
  114. //
  115. if (*SecCoreImageBase != 0 && *PeiCoreImageBase != 0) {
  116. return EFI_SUCCESS;
  117. }
  118. }
  119. }
  120. /**
  121. Find and return Pei Core entry point.
  122. It also find SEC and PEI Core file debug information. It will report them if
  123. remote debug is enabled.
  124. @param BootFirmwareVolumePtr Point to the boot firmware volume.
  125. @param PeiCoreEntryPoint The entry point of the PEI core.
  126. **/
  127. VOID
  128. EFIAPI
  129. FindAndReportEntryPoints (
  130. IN EFI_FIRMWARE_VOLUME_HEADER *BootFirmwareVolumePtr,
  131. OUT EFI_PEI_CORE_ENTRY_POINT *PeiCoreEntryPoint
  132. )
  133. {
  134. EFI_STATUS Status;
  135. EFI_PHYSICAL_ADDRESS SecCoreImageBase;
  136. EFI_PHYSICAL_ADDRESS PeiCoreImageBase;
  137. PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
  138. //
  139. // Find SEC Core and PEI Core image base
  140. //
  141. Status = FindImageBase (BootFirmwareVolumePtr, &SecCoreImageBase, &PeiCoreImageBase);
  142. ASSERT_EFI_ERROR (Status);
  143. ZeroMem ((VOID *) &ImageContext, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT));
  144. //
  145. // Report SEC Core debug information when remote debug is enabled
  146. //
  147. ImageContext.ImageAddress = SecCoreImageBase;
  148. ImageContext.PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageContext.ImageAddress);
  149. PeCoffLoaderRelocateImageExtraAction (&ImageContext);
  150. //
  151. // Report PEI Core debug information when remote debug is enabled
  152. //
  153. ImageContext.ImageAddress = PeiCoreImageBase;
  154. ImageContext.PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageContext.ImageAddress);
  155. PeCoffLoaderRelocateImageExtraAction (&ImageContext);
  156. //
  157. // Find PEI Core entry point
  158. //
  159. Status = PeCoffLoaderGetEntryPoint ((VOID *) (UINTN) PeiCoreImageBase, (VOID**) PeiCoreEntryPoint);
  160. if (EFI_ERROR (Status)) {
  161. *PeiCoreEntryPoint = 0;
  162. }
  163. return;
  164. }