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