]>
Commit | Line | Data |
---|---|---|
184558d0 SV |
1 | /** @file\r |
2 | Entry point to the Standalone MM Foundation when initialized during the SEC\r | |
3 | phase on ARM platforms\r | |
4 | \r | |
764942a2 | 5 | Copyright (c) 2017 - 2021, Arm Ltd. All rights reserved.<BR>\r |
86094561 | 6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
184558d0 SV |
7 | \r |
8 | **/\r | |
9 | \r | |
10 | \r | |
11 | #include <PiMm.h>\r | |
12 | \r | |
13 | #include <Library/AArch64/StandaloneMmCoreEntryPoint.h>\r | |
14 | \r | |
15 | #include <PiPei.h>\r | |
16 | #include <Guid/MmramMemoryReserve.h>\r | |
17 | #include <Guid/MpInformation.h>\r | |
18 | \r | |
19 | #include <Library/ArmMmuLib.h>\r | |
20 | #include <Library/ArmSvcLib.h>\r | |
21 | #include <Library/DebugLib.h>\r | |
22 | #include <Library/HobLib.h>\r | |
23 | #include <Library/BaseLib.h>\r | |
24 | #include <Library/BaseMemoryLib.h>\r | |
25 | #include <Library/SerialPortLib.h>\r | |
26 | \r | |
27 | #include <IndustryStandard/ArmStdSmc.h>\r | |
28 | #include <IndustryStandard/ArmMmSvc.h>\r | |
82abb8f9 | 29 | #include <IndustryStandard/ArmFfaSvc.h>\r |
184558d0 SV |
30 | \r |
31 | #define SPM_MAJOR_VER_MASK 0xFFFF0000\r | |
32 | #define SPM_MINOR_VER_MASK 0x0000FFFF\r | |
33 | #define SPM_MAJOR_VER_SHIFT 16\r | |
34 | \r | |
053cd598 SG |
35 | STATIC CONST UINT32 mSpmMajorVer = SPM_MAJOR_VERSION;\r |
36 | STATIC CONST UINT32 mSpmMinorVer = SPM_MINOR_VERSION;\r | |
184558d0 | 37 | \r |
05270532 | 38 | #define BOOT_PAYLOAD_VERSION 1\r |
184558d0 SV |
39 | \r |
40 | PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT CpuDriverEntryPoint = NULL;\r | |
41 | \r | |
42 | /**\r | |
43 | Retrieve a pointer to and print the boot information passed by privileged\r | |
561c3630 | 44 | secure firmware.\r |
184558d0 | 45 | \r |
561c3630 SM |
46 | @param [in] SharedBufAddress The pointer memory shared with privileged\r |
47 | firmware.\r | |
184558d0 SV |
48 | \r |
49 | **/\r | |
50 | EFI_SECURE_PARTITION_BOOT_INFO *\r | |
51 | GetAndPrintBootinformation (\r | |
52 | IN VOID *SharedBufAddress\r | |
53 | )\r | |
54 | {\r | |
55 | EFI_SECURE_PARTITION_BOOT_INFO *PayloadBootInfo;\r | |
56 | EFI_SECURE_PARTITION_CPU_INFO *PayloadCpuInfo;\r | |
57 | UINTN Index;\r | |
58 | \r | |
59 | PayloadBootInfo = (EFI_SECURE_PARTITION_BOOT_INFO *) SharedBufAddress;\r | |
60 | \r | |
61 | if (PayloadBootInfo == NULL) {\r | |
62 | DEBUG ((DEBUG_ERROR, "PayloadBootInfo NULL\n"));\r | |
63 | return NULL;\r | |
64 | }\r | |
65 | \r | |
66 | if (PayloadBootInfo->Header.Version != BOOT_PAYLOAD_VERSION) {\r | |
67 | DEBUG ((DEBUG_ERROR, "Boot Information Version Mismatch. Current=0x%x, Expected=0x%x.\n",\r | |
68 | PayloadBootInfo->Header.Version, BOOT_PAYLOAD_VERSION));\r | |
69 | return NULL;\r | |
70 | }\r | |
71 | \r | |
72 | DEBUG ((DEBUG_INFO, "NumSpMemRegions - 0x%x\n", PayloadBootInfo->NumSpMemRegions));\r | |
73 | DEBUG ((DEBUG_INFO, "SpMemBase - 0x%lx\n", PayloadBootInfo->SpMemBase));\r | |
74 | DEBUG ((DEBUG_INFO, "SpMemLimit - 0x%lx\n", PayloadBootInfo->SpMemLimit));\r | |
75 | DEBUG ((DEBUG_INFO, "SpImageBase - 0x%lx\n", PayloadBootInfo->SpImageBase));\r | |
76 | DEBUG ((DEBUG_INFO, "SpStackBase - 0x%lx\n", PayloadBootInfo->SpStackBase));\r | |
77 | DEBUG ((DEBUG_INFO, "SpHeapBase - 0x%lx\n", PayloadBootInfo->SpHeapBase));\r | |
78 | DEBUG ((DEBUG_INFO, "SpNsCommBufBase - 0x%lx\n", PayloadBootInfo->SpNsCommBufBase));\r | |
79 | DEBUG ((DEBUG_INFO, "SpSharedBufBase - 0x%lx\n", PayloadBootInfo->SpSharedBufBase));\r | |
80 | \r | |
81 | DEBUG ((DEBUG_INFO, "SpImageSize - 0x%x\n", PayloadBootInfo->SpImageSize));\r | |
82 | DEBUG ((DEBUG_INFO, "SpPcpuStackSize - 0x%x\n", PayloadBootInfo->SpPcpuStackSize));\r | |
83 | DEBUG ((DEBUG_INFO, "SpHeapSize - 0x%x\n", PayloadBootInfo->SpHeapSize));\r | |
84 | DEBUG ((DEBUG_INFO, "SpNsCommBufSize - 0x%x\n", PayloadBootInfo->SpNsCommBufSize));\r | |
85 | DEBUG ((DEBUG_INFO, "SpPcpuSharedBufSize - 0x%x\n", PayloadBootInfo->SpPcpuSharedBufSize));\r | |
86 | \r | |
87 | DEBUG ((DEBUG_INFO, "NumCpus - 0x%x\n", PayloadBootInfo->NumCpus));\r | |
88 | DEBUG ((DEBUG_INFO, "CpuInfo - 0x%p\n", PayloadBootInfo->CpuInfo));\r | |
89 | \r | |
90 | PayloadCpuInfo = (EFI_SECURE_PARTITION_CPU_INFO *) PayloadBootInfo->CpuInfo;\r | |
91 | \r | |
92 | if (PayloadCpuInfo == NULL) {\r | |
93 | DEBUG ((DEBUG_ERROR, "PayloadCpuInfo NULL\n"));\r | |
94 | return NULL;\r | |
95 | }\r | |
96 | \r | |
97 | for (Index = 0; Index < PayloadBootInfo->NumCpus; Index++) {\r | |
98 | DEBUG ((DEBUG_INFO, "Mpidr - 0x%lx\n", PayloadCpuInfo[Index].Mpidr));\r | |
99 | DEBUG ((DEBUG_INFO, "LinearId - 0x%x\n", PayloadCpuInfo[Index].LinearId));\r | |
100 | DEBUG ((DEBUG_INFO, "Flags - 0x%x\n", PayloadCpuInfo[Index].Flags));\r | |
101 | }\r | |
102 | \r | |
103 | return PayloadBootInfo;\r | |
104 | }\r | |
105 | \r | |
764942a2 SM |
106 | /**\r |
107 | A loop to delegated events.\r | |
108 | \r | |
109 | @param [in] EventCompleteSvcArgs Pointer to the event completion arguments.\r | |
110 | \r | |
111 | **/\r | |
184558d0 SV |
112 | VOID\r |
113 | EFIAPI\r | |
114 | DelegatedEventLoop (\r | |
115 | IN ARM_SVC_ARGS *EventCompleteSvcArgs\r | |
116 | )\r | |
117 | {\r | |
118 | EFI_STATUS Status;\r | |
119 | UINTN SvcStatus;\r | |
120 | \r | |
121 | while (TRUE) {\r | |
122 | ArmCallSvc (EventCompleteSvcArgs);\r | |
123 | \r | |
124 | DEBUG ((DEBUG_INFO, "Received delegated event\n"));\r | |
125 | DEBUG ((DEBUG_INFO, "X0 : 0x%x\n", (UINT32) EventCompleteSvcArgs->Arg0));\r | |
126 | DEBUG ((DEBUG_INFO, "X1 : 0x%x\n", (UINT32) EventCompleteSvcArgs->Arg1));\r | |
127 | DEBUG ((DEBUG_INFO, "X2 : 0x%x\n", (UINT32) EventCompleteSvcArgs->Arg2));\r | |
128 | DEBUG ((DEBUG_INFO, "X3 : 0x%x\n", (UINT32) EventCompleteSvcArgs->Arg3));\r | |
129 | \r | |
130 | Status = CpuDriverEntryPoint (\r | |
131 | EventCompleteSvcArgs->Arg0,\r | |
132 | EventCompleteSvcArgs->Arg3,\r | |
133 | EventCompleteSvcArgs->Arg1\r | |
134 | );\r | |
135 | \r | |
136 | if (EFI_ERROR (Status)) {\r | |
137 | DEBUG ((DEBUG_ERROR, "Failed delegated event 0x%x, Status 0x%x\n",\r | |
138 | EventCompleteSvcArgs->Arg0, Status));\r | |
139 | }\r | |
140 | \r | |
141 | switch (Status) {\r | |
142 | case EFI_SUCCESS:\r | |
143 | SvcStatus = ARM_SVC_SPM_RET_SUCCESS;\r | |
144 | break;\r | |
145 | case EFI_INVALID_PARAMETER:\r | |
146 | SvcStatus = ARM_SVC_SPM_RET_INVALID_PARAMS;\r | |
147 | break;\r | |
148 | case EFI_ACCESS_DENIED:\r | |
149 | SvcStatus = ARM_SVC_SPM_RET_DENIED;\r | |
150 | break;\r | |
151 | case EFI_OUT_OF_RESOURCES:\r | |
152 | SvcStatus = ARM_SVC_SPM_RET_NO_MEMORY;\r | |
153 | break;\r | |
154 | case EFI_UNSUPPORTED:\r | |
155 | SvcStatus = ARM_SVC_SPM_RET_NOT_SUPPORTED;\r | |
156 | break;\r | |
157 | default:\r | |
158 | SvcStatus = ARM_SVC_SPM_RET_NOT_SUPPORTED;\r | |
159 | break;\r | |
160 | }\r | |
161 | \r | |
162 | EventCompleteSvcArgs->Arg0 = ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64;\r | |
163 | EventCompleteSvcArgs->Arg1 = SvcStatus;\r | |
164 | }\r | |
165 | }\r | |
166 | \r | |
764942a2 SM |
167 | /**\r |
168 | Query the SPM version, check compatibility and return success if compatible.\r | |
169 | \r | |
170 | @retval EFI_SUCCESS SPM versions compatible.\r | |
171 | @retval EFI_UNSUPPORTED SPM versions not compatible.\r | |
172 | **/\r | |
184558d0 SV |
173 | STATIC\r |
174 | EFI_STATUS\r | |
175 | GetSpmVersion (VOID)\r | |
176 | {\r | |
177 | EFI_STATUS Status;\r | |
178 | UINT16 SpmMajorVersion;\r | |
179 | UINT16 SpmMinorVersion;\r | |
180 | UINT32 SpmVersion;\r | |
181 | ARM_SVC_ARGS SpmVersionArgs;\r | |
182 | \r | |
183 | SpmVersionArgs.Arg0 = ARM_SVC_ID_SPM_VERSION_AARCH32;\r | |
184 | \r | |
185 | ArmCallSvc (&SpmVersionArgs);\r | |
186 | \r | |
187 | SpmVersion = SpmVersionArgs.Arg0;\r | |
188 | \r | |
189 | SpmMajorVersion = ((SpmVersion & SPM_MAJOR_VER_MASK) >> SPM_MAJOR_VER_SHIFT);\r | |
190 | SpmMinorVersion = ((SpmVersion & SPM_MINOR_VER_MASK) >> 0);\r | |
191 | \r | |
192 | // Different major revision values indicate possibly incompatible functions.\r | |
193 | // For two revisions, A and B, for which the major revision values are\r | |
194 | // identical, if the minor revision value of revision B is greater than\r | |
195 | // the minor revision value of revision A, then every function in\r | |
196 | // revision A must work in a compatible way with revision B.\r | |
197 | // However, it is possible for revision B to have a higher\r | |
198 | // function count than revision A.\r | |
053cd598 SG |
199 | if ((SpmMajorVersion == mSpmMajorVer) &&\r |
200 | (SpmMinorVersion >= mSpmMinorVer))\r | |
184558d0 SV |
201 | {\r |
202 | DEBUG ((DEBUG_INFO, "SPM Version: Major=0x%x, Minor=0x%x\n",\r | |
203 | SpmMajorVersion, SpmMinorVersion));\r | |
204 | Status = EFI_SUCCESS;\r | |
205 | }\r | |
206 | else\r | |
207 | {\r | |
208 | DEBUG ((DEBUG_INFO, "Incompatible SPM Versions.\n Current Version: Major=0x%x, Minor=0x%x.\n Expected: Major=0x%x, Minor>=0x%x.\n",\r | |
053cd598 | 209 | SpmMajorVersion, SpmMinorVersion, mSpmMajorVer, mSpmMinorVer));\r |
184558d0 SV |
210 | Status = EFI_UNSUPPORTED;\r |
211 | }\r | |
212 | \r | |
213 | return Status;\r | |
214 | }\r | |
215 | \r | |
216 | /**\r | |
217 | The entry point of Standalone MM Foundation.\r | |
218 | \r | |
4779bc6c SM |
219 | @param [in] SharedBufAddress Pointer to the Buffer between SPM and SP.\r |
220 | @param [in] SharedBufSize Size of the shared buffer.\r | |
221 | @param [in] cookie1 Cookie 1\r | |
222 | @param [in] cookie2 Cookie 2\r | |
184558d0 SV |
223 | \r |
224 | **/\r | |
225 | VOID\r | |
226 | EFIAPI\r | |
227 | _ModuleEntryPoint (\r | |
228 | IN VOID *SharedBufAddress,\r | |
229 | IN UINT64 SharedBufSize,\r | |
230 | IN UINT64 cookie1,\r | |
231 | IN UINT64 cookie2\r | |
232 | )\r | |
233 | {\r | |
234 | PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r | |
235 | EFI_SECURE_PARTITION_BOOT_INFO *PayloadBootInfo;\r | |
d4951425 | 236 | ARM_SVC_ARGS InitMmFoundationSvcArgs;\r |
184558d0 SV |
237 | EFI_STATUS Status;\r |
238 | UINT32 SectionHeaderOffset;\r | |
239 | UINT16 NumberOfSections;\r | |
240 | VOID *HobStart;\r | |
241 | VOID *TeData;\r | |
242 | UINTN TeDataSize;\r | |
493f2c69 | 243 | EFI_PHYSICAL_ADDRESS ImageBase;\r |
184558d0 | 244 | \r |
184558d0 SV |
245 | // Get Secure Partition Manager Version Information\r |
246 | Status = GetSpmVersion ();\r | |
247 | if (EFI_ERROR (Status)) {\r | |
248 | goto finish;\r | |
249 | }\r | |
250 | \r | |
251 | PayloadBootInfo = GetAndPrintBootinformation (SharedBufAddress);\r | |
252 | if (PayloadBootInfo == NULL) {\r | |
253 | Status = EFI_UNSUPPORTED;\r | |
254 | goto finish;\r | |
255 | }\r | |
256 | \r | |
257 | // Locate PE/COFF File information for the Standalone MM core module\r | |
258 | Status = LocateStandaloneMmCorePeCoffData (\r | |
259 | (EFI_FIRMWARE_VOLUME_HEADER *) PayloadBootInfo->SpImageBase,\r | |
260 | &TeData,\r | |
261 | &TeDataSize\r | |
262 | );\r | |
263 | \r | |
264 | if (EFI_ERROR (Status)) {\r | |
265 | goto finish;\r | |
266 | }\r | |
267 | \r | |
268 | // Obtain the PE/COFF Section information for the Standalone MM core module\r | |
269 | Status = GetStandaloneMmCorePeCoffSections (\r | |
270 | TeData,\r | |
271 | &ImageContext,\r | |
493f2c69 | 272 | &ImageBase,\r |
184558d0 SV |
273 | &SectionHeaderOffset,\r |
274 | &NumberOfSections\r | |
275 | );\r | |
276 | \r | |
277 | if (EFI_ERROR (Status)) {\r | |
278 | goto finish;\r | |
279 | }\r | |
280 | \r | |
493f2c69 AB |
281 | //\r |
282 | // ImageBase may deviate from ImageContext.ImageAddress if we are dealing\r | |
283 | // with a TE image, in which case the latter points to the actual offset\r | |
284 | // of the image, whereas ImageBase refers to the address where the image\r | |
285 | // would start if the stripped PE headers were still in place. In either\r | |
286 | // case, we need to fix up ImageBase so it refers to the actual current\r | |
287 | // load address.\r | |
288 | //\r | |
289 | ImageBase += (UINTN)TeData - ImageContext.ImageAddress;\r | |
290 | \r | |
184558d0 SV |
291 | // Update the memory access permissions of individual sections in the\r |
292 | // Standalone MM core module\r | |
293 | Status = UpdateMmFoundationPeCoffPermissions (\r | |
294 | &ImageContext,\r | |
493f2c69 | 295 | ImageBase,\r |
184558d0 SV |
296 | SectionHeaderOffset,\r |
297 | NumberOfSections,\r | |
298 | ArmSetMemoryRegionNoExec,\r | |
299 | ArmSetMemoryRegionReadOnly,\r | |
300 | ArmClearMemoryRegionReadOnly\r | |
301 | );\r | |
302 | \r | |
303 | if (EFI_ERROR (Status)) {\r | |
304 | goto finish;\r | |
305 | }\r | |
306 | \r | |
493f2c69 AB |
307 | if (ImageContext.ImageAddress != (UINTN)TeData) {\r |
308 | ImageContext.ImageAddress = (UINTN)TeData;\r | |
309 | ArmSetMemoryRegionNoExec (ImageBase, SIZE_4KB);\r | |
310 | ArmClearMemoryRegionReadOnly (ImageBase, SIZE_4KB);\r | |
311 | \r | |
312 | Status = PeCoffLoaderRelocateImage (&ImageContext);\r | |
313 | ASSERT_EFI_ERROR (Status);\r | |
314 | }\r | |
315 | \r | |
184558d0 SV |
316 | //\r |
317 | // Create Hoblist based upon boot information passed by privileged software\r | |
318 | //\r | |
319 | HobStart = CreateHobListFromBootInfo (&CpuDriverEntryPoint, PayloadBootInfo);\r | |
320 | \r | |
321 | //\r | |
322 | // Call the MM Core entry point\r | |
323 | //\r | |
324 | ProcessModuleEntryPointList (HobStart);\r | |
325 | \r | |
184558d0 SV |
326 | DEBUG ((DEBUG_INFO, "Shared Cpu Driver EP 0x%lx\n", (UINT64) CpuDriverEntryPoint));\r |
327 | \r | |
328 | finish:\r | |
d4951425 | 329 | ZeroMem (&InitMmFoundationSvcArgs, sizeof(InitMmFoundationSvcArgs));\r |
184558d0 SV |
330 | InitMmFoundationSvcArgs.Arg0 = ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64;\r |
331 | InitMmFoundationSvcArgs.Arg1 = Status;\r | |
332 | DelegatedEventLoop (&InitMmFoundationSvcArgs);\r | |
184558d0 | 333 | }\r |