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