]> git.proxmox.com Git - mirror_edk2.git/blame - StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/AArch64/StandaloneMmCoreEntryPoint.c
StandaloneMmPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / StandaloneMmPkg / Library / StandaloneMmCoreEntryPoint / AArch64 / StandaloneMmCoreEntryPoint.c
CommitLineData
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
5Copyright (c) 2017 - 2018, ARM Ltd. All rights reserved.<BR>\r
86094561 6SPDX-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
29\r
30#define SPM_MAJOR_VER_MASK 0xFFFF0000\r
31#define SPM_MINOR_VER_MASK 0x0000FFFF\r
32#define SPM_MAJOR_VER_SHIFT 16\r
33\r
34CONST UINT32 SPM_MAJOR_VER = 0;\r
35CONST UINT32 SPM_MINOR_VER = 1;\r
36\r
37CONST UINT8 BOOT_PAYLOAD_VERSION = 1;\r
38\r
39PI_MM_ARM_TF_CPU_DRIVER_ENTRYPOINT CpuDriverEntryPoint = NULL;\r
40\r
41/**\r
42 Retrieve a pointer to and print the boot information passed by privileged\r
43 secure firmware\r
44\r
45 @param SharedBufAddress The pointer memory shared with privileged firmware\r
46\r
47**/\r
48EFI_SECURE_PARTITION_BOOT_INFO *\r
49GetAndPrintBootinformation (\r
50 IN VOID *SharedBufAddress\r
51)\r
52{\r
53 EFI_SECURE_PARTITION_BOOT_INFO *PayloadBootInfo;\r
54 EFI_SECURE_PARTITION_CPU_INFO *PayloadCpuInfo;\r
55 UINTN Index;\r
56\r
57 PayloadBootInfo = (EFI_SECURE_PARTITION_BOOT_INFO *) SharedBufAddress;\r
58\r
59 if (PayloadBootInfo == NULL) {\r
60 DEBUG ((DEBUG_ERROR, "PayloadBootInfo NULL\n"));\r
61 return NULL;\r
62 }\r
63\r
64 if (PayloadBootInfo->Header.Version != BOOT_PAYLOAD_VERSION) {\r
65 DEBUG ((DEBUG_ERROR, "Boot Information Version Mismatch. Current=0x%x, Expected=0x%x.\n",\r
66 PayloadBootInfo->Header.Version, BOOT_PAYLOAD_VERSION));\r
67 return NULL;\r
68 }\r
69\r
70 DEBUG ((DEBUG_INFO, "NumSpMemRegions - 0x%x\n", PayloadBootInfo->NumSpMemRegions));\r
71 DEBUG ((DEBUG_INFO, "SpMemBase - 0x%lx\n", PayloadBootInfo->SpMemBase));\r
72 DEBUG ((DEBUG_INFO, "SpMemLimit - 0x%lx\n", PayloadBootInfo->SpMemLimit));\r
73 DEBUG ((DEBUG_INFO, "SpImageBase - 0x%lx\n", PayloadBootInfo->SpImageBase));\r
74 DEBUG ((DEBUG_INFO, "SpStackBase - 0x%lx\n", PayloadBootInfo->SpStackBase));\r
75 DEBUG ((DEBUG_INFO, "SpHeapBase - 0x%lx\n", PayloadBootInfo->SpHeapBase));\r
76 DEBUG ((DEBUG_INFO, "SpNsCommBufBase - 0x%lx\n", PayloadBootInfo->SpNsCommBufBase));\r
77 DEBUG ((DEBUG_INFO, "SpSharedBufBase - 0x%lx\n", PayloadBootInfo->SpSharedBufBase));\r
78\r
79 DEBUG ((DEBUG_INFO, "SpImageSize - 0x%x\n", PayloadBootInfo->SpImageSize));\r
80 DEBUG ((DEBUG_INFO, "SpPcpuStackSize - 0x%x\n", PayloadBootInfo->SpPcpuStackSize));\r
81 DEBUG ((DEBUG_INFO, "SpHeapSize - 0x%x\n", PayloadBootInfo->SpHeapSize));\r
82 DEBUG ((DEBUG_INFO, "SpNsCommBufSize - 0x%x\n", PayloadBootInfo->SpNsCommBufSize));\r
83 DEBUG ((DEBUG_INFO, "SpPcpuSharedBufSize - 0x%x\n", PayloadBootInfo->SpPcpuSharedBufSize));\r
84\r
85 DEBUG ((DEBUG_INFO, "NumCpus - 0x%x\n", PayloadBootInfo->NumCpus));\r
86 DEBUG ((DEBUG_INFO, "CpuInfo - 0x%p\n", PayloadBootInfo->CpuInfo));\r
87\r
88 PayloadCpuInfo = (EFI_SECURE_PARTITION_CPU_INFO *) PayloadBootInfo->CpuInfo;\r
89\r
90 if (PayloadCpuInfo == NULL) {\r
91 DEBUG ((DEBUG_ERROR, "PayloadCpuInfo NULL\n"));\r
92 return NULL;\r
93 }\r
94\r
95 for (Index = 0; Index < PayloadBootInfo->NumCpus; Index++) {\r
96 DEBUG ((DEBUG_INFO, "Mpidr - 0x%lx\n", PayloadCpuInfo[Index].Mpidr));\r
97 DEBUG ((DEBUG_INFO, "LinearId - 0x%x\n", PayloadCpuInfo[Index].LinearId));\r
98 DEBUG ((DEBUG_INFO, "Flags - 0x%x\n", PayloadCpuInfo[Index].Flags));\r
99 }\r
100\r
101 return PayloadBootInfo;\r
102}\r
103\r
104VOID\r
105EFIAPI\r
106DelegatedEventLoop (\r
107 IN ARM_SVC_ARGS *EventCompleteSvcArgs\r
108 )\r
109{\r
110 EFI_STATUS Status;\r
111 UINTN SvcStatus;\r
112\r
113 while (TRUE) {\r
114 ArmCallSvc (EventCompleteSvcArgs);\r
115\r
116 DEBUG ((DEBUG_INFO, "Received delegated event\n"));\r
117 DEBUG ((DEBUG_INFO, "X0 : 0x%x\n", (UINT32) EventCompleteSvcArgs->Arg0));\r
118 DEBUG ((DEBUG_INFO, "X1 : 0x%x\n", (UINT32) EventCompleteSvcArgs->Arg1));\r
119 DEBUG ((DEBUG_INFO, "X2 : 0x%x\n", (UINT32) EventCompleteSvcArgs->Arg2));\r
120 DEBUG ((DEBUG_INFO, "X3 : 0x%x\n", (UINT32) EventCompleteSvcArgs->Arg3));\r
121\r
122 Status = CpuDriverEntryPoint (\r
123 EventCompleteSvcArgs->Arg0,\r
124 EventCompleteSvcArgs->Arg3,\r
125 EventCompleteSvcArgs->Arg1\r
126 );\r
127\r
128 if (EFI_ERROR (Status)) {\r
129 DEBUG ((DEBUG_ERROR, "Failed delegated event 0x%x, Status 0x%x\n",\r
130 EventCompleteSvcArgs->Arg0, Status));\r
131 }\r
132\r
133 switch (Status) {\r
134 case EFI_SUCCESS:\r
135 SvcStatus = ARM_SVC_SPM_RET_SUCCESS;\r
136 break;\r
137 case EFI_INVALID_PARAMETER:\r
138 SvcStatus = ARM_SVC_SPM_RET_INVALID_PARAMS;\r
139 break;\r
140 case EFI_ACCESS_DENIED:\r
141 SvcStatus = ARM_SVC_SPM_RET_DENIED;\r
142 break;\r
143 case EFI_OUT_OF_RESOURCES:\r
144 SvcStatus = ARM_SVC_SPM_RET_NO_MEMORY;\r
145 break;\r
146 case EFI_UNSUPPORTED:\r
147 SvcStatus = ARM_SVC_SPM_RET_NOT_SUPPORTED;\r
148 break;\r
149 default:\r
150 SvcStatus = ARM_SVC_SPM_RET_NOT_SUPPORTED;\r
151 break;\r
152 }\r
153\r
154 EventCompleteSvcArgs->Arg0 = ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64;\r
155 EventCompleteSvcArgs->Arg1 = SvcStatus;\r
156 }\r
157}\r
158\r
159STATIC\r
160EFI_STATUS\r
161GetSpmVersion (VOID)\r
162{\r
163 EFI_STATUS Status;\r
164 UINT16 SpmMajorVersion;\r
165 UINT16 SpmMinorVersion;\r
166 UINT32 SpmVersion;\r
167 ARM_SVC_ARGS SpmVersionArgs;\r
168\r
169 SpmVersionArgs.Arg0 = ARM_SVC_ID_SPM_VERSION_AARCH32;\r
170\r
171 ArmCallSvc (&SpmVersionArgs);\r
172\r
173 SpmVersion = SpmVersionArgs.Arg0;\r
174\r
175 SpmMajorVersion = ((SpmVersion & SPM_MAJOR_VER_MASK) >> SPM_MAJOR_VER_SHIFT);\r
176 SpmMinorVersion = ((SpmVersion & SPM_MINOR_VER_MASK) >> 0);\r
177\r
178 // Different major revision values indicate possibly incompatible functions.\r
179 // For two revisions, A and B, for which the major revision values are\r
180 // identical, if the minor revision value of revision B is greater than\r
181 // the minor revision value of revision A, then every function in\r
182 // revision A must work in a compatible way with revision B.\r
183 // However, it is possible for revision B to have a higher\r
184 // function count than revision A.\r
185 if ((SpmMajorVersion == SPM_MAJOR_VER) &&\r
186 (SpmMinorVersion >= SPM_MINOR_VER))\r
187 {\r
188 DEBUG ((DEBUG_INFO, "SPM Version: Major=0x%x, Minor=0x%x\n",\r
189 SpmMajorVersion, SpmMinorVersion));\r
190 Status = EFI_SUCCESS;\r
191 }\r
192 else\r
193 {\r
194 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
195 SpmMajorVersion, SpmMinorVersion, SPM_MAJOR_VER, SPM_MINOR_VER));\r
196 Status = EFI_UNSUPPORTED;\r
197 }\r
198\r
199 return Status;\r
200}\r
201\r
202/**\r
203 The entry point of Standalone MM Foundation.\r
204\r
205 @param SharedBufAddress Pointer to the Buffer between SPM and SP.\r
206 @param cookie1.\r
207 @param cookie2.\r
208\r
209**/\r
210VOID\r
211EFIAPI\r
212_ModuleEntryPoint (\r
213 IN VOID *SharedBufAddress,\r
214 IN UINT64 SharedBufSize,\r
215 IN UINT64 cookie1,\r
216 IN UINT64 cookie2\r
217 )\r
218{\r
219 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;\r
220 EFI_SECURE_PARTITION_BOOT_INFO *PayloadBootInfo;\r
d4951425 221 ARM_SVC_ARGS InitMmFoundationSvcArgs;\r
184558d0
SV
222 EFI_STATUS Status;\r
223 UINT32 SectionHeaderOffset;\r
224 UINT16 NumberOfSections;\r
225 VOID *HobStart;\r
226 VOID *TeData;\r
227 UINTN TeDataSize;\r
228\r
184558d0
SV
229 // Get Secure Partition Manager Version Information\r
230 Status = GetSpmVersion ();\r
231 if (EFI_ERROR (Status)) {\r
232 goto finish;\r
233 }\r
234\r
235 PayloadBootInfo = GetAndPrintBootinformation (SharedBufAddress);\r
236 if (PayloadBootInfo == NULL) {\r
237 Status = EFI_UNSUPPORTED;\r
238 goto finish;\r
239 }\r
240\r
241 // Locate PE/COFF File information for the Standalone MM core module\r
242 Status = LocateStandaloneMmCorePeCoffData (\r
243 (EFI_FIRMWARE_VOLUME_HEADER *) PayloadBootInfo->SpImageBase,\r
244 &TeData,\r
245 &TeDataSize\r
246 );\r
247\r
248 if (EFI_ERROR (Status)) {\r
249 goto finish;\r
250 }\r
251\r
252 // Obtain the PE/COFF Section information for the Standalone MM core module\r
253 Status = GetStandaloneMmCorePeCoffSections (\r
254 TeData,\r
255 &ImageContext,\r
256 &SectionHeaderOffset,\r
257 &NumberOfSections\r
258 );\r
259\r
260 if (EFI_ERROR (Status)) {\r
261 goto finish;\r
262 }\r
263\r
264 // Update the memory access permissions of individual sections in the\r
265 // Standalone MM core module\r
266 Status = UpdateMmFoundationPeCoffPermissions (\r
267 &ImageContext,\r
268 SectionHeaderOffset,\r
269 NumberOfSections,\r
270 ArmSetMemoryRegionNoExec,\r
271 ArmSetMemoryRegionReadOnly,\r
272 ArmClearMemoryRegionReadOnly\r
273 );\r
274\r
275 if (EFI_ERROR (Status)) {\r
276 goto finish;\r
277 }\r
278\r
279 //\r
280 // Create Hoblist based upon boot information passed by privileged software\r
281 //\r
282 HobStart = CreateHobListFromBootInfo (&CpuDriverEntryPoint, PayloadBootInfo);\r
283\r
284 //\r
285 // Call the MM Core entry point\r
286 //\r
287 ProcessModuleEntryPointList (HobStart);\r
288\r
184558d0
SV
289 DEBUG ((DEBUG_INFO, "Shared Cpu Driver EP 0x%lx\n", (UINT64) CpuDriverEntryPoint));\r
290\r
291finish:\r
d4951425 292 ZeroMem (&InitMmFoundationSvcArgs, sizeof(InitMmFoundationSvcArgs));\r
184558d0
SV
293 InitMmFoundationSvcArgs.Arg0 = ARM_SVC_ID_SP_EVENT_COMPLETE_AARCH64;\r
294 InitMmFoundationSvcArgs.Arg1 = Status;\r
295 DelegatedEventLoop (&InitMmFoundationSvcArgs);\r
184558d0 296}\r