]> git.proxmox.com Git - mirror_edk2.git/blame - StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/AArch64/SetPermissions.c
StandaloneMmPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / StandaloneMmPkg / Library / StandaloneMmCoreEntryPoint / AArch64 / SetPermissions.c
CommitLineData
184558d0
SV
1/** @file\r
2 Locate, get and update PE/COFF permissions during Standalone MM\r
3 Foundation Entry point 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 <PiPei.h>\r
14#include <Guid/MmramMemoryReserve.h>\r
15#include <Guid/MpInformation.h>\r
16\r
17#include <Library/AArch64/StandaloneMmCoreEntryPoint.h>\r
18#include <Library/ArmMmuLib.h>\r
19#include <Library/ArmSvcLib.h>\r
20#include <Library/DebugLib.h>\r
21#include <Library/HobLib.h>\r
22#include <Library/BaseLib.h>\r
23#include <Library/BaseMemoryLib.h>\r
24#include <Library/SerialPortLib.h>\r
25\r
26#include <IndustryStandard/ArmStdSmc.h>\r
27\r
28EFI_STATUS\r
29EFIAPI\r
30UpdateMmFoundationPeCoffPermissions (\r
31 IN CONST PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r
32 IN UINT32 SectionHeaderOffset,\r
33 IN CONST UINT16 NumberOfSections,\r
34 IN REGION_PERMISSION_UPDATE_FUNC TextUpdater,\r
35 IN REGION_PERMISSION_UPDATE_FUNC ReadOnlyUpdater,\r
36 IN REGION_PERMISSION_UPDATE_FUNC ReadWriteUpdater\r
37 )\r
38{\r
39 EFI_IMAGE_SECTION_HEADER SectionHeader;\r
40 RETURN_STATUS Status;\r
41 EFI_PHYSICAL_ADDRESS Base;\r
42 UINTN Size;\r
43 UINTN ReadSize;\r
44 UINTN Index;\r
45\r
46 ASSERT (ImageContext != NULL);\r
47\r
48 //\r
49 // Iterate over the sections\r
50 //\r
51 for (Index = 0; Index < NumberOfSections; Index++) {\r
52 //\r
53 // Read section header from file\r
54 //\r
55 Size = sizeof (EFI_IMAGE_SECTION_HEADER);\r
56 ReadSize = Size;\r
57 Status = ImageContext->ImageRead (\r
58 ImageContext->Handle,\r
59 SectionHeaderOffset,\r
60 &Size,\r
61 &SectionHeader\r
62 );\r
63\r
64 if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r
65 DEBUG ((DEBUG_ERROR,\r
66 "%a: ImageContext->ImageRead () failed (Status = %r)\n",\r
67 __FUNCTION__, Status));\r
68 return Status;\r
69 }\r
70\r
71 DEBUG ((DEBUG_INFO,\r
72 "%a: Section %d of image at 0x%lx has 0x%x permissions\n",\r
73 __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.Characteristics));\r
74 DEBUG ((DEBUG_INFO,\r
41915a19 75 "%a: Section %d of image at 0x%lx has %a name\n",\r
184558d0
SV
76 __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.Name));\r
77 DEBUG ((DEBUG_INFO,\r
78 "%a: Section %d of image at 0x%lx has 0x%x address\n",\r
79 __FUNCTION__, Index, ImageContext->ImageAddress,\r
80 ImageContext->ImageAddress + SectionHeader.VirtualAddress));\r
81 DEBUG ((DEBUG_INFO,\r
82 "%a: Section %d of image at 0x%lx has 0x%x data\n",\r
83 __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.PointerToRawData));\r
84\r
85 //\r
86 // If the section is marked as XN then remove the X attribute. Furthermore,\r
87 // if it is a writeable section then mark it appropriately as well.\r
88 //\r
89 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_MEM_EXECUTE) == 0) {\r
90 Base = ImageContext->ImageAddress + SectionHeader.VirtualAddress;\r
91\r
92 TextUpdater (Base, SectionHeader.Misc.VirtualSize);\r
93\r
94 if ((SectionHeader.Characteristics & EFI_IMAGE_SCN_MEM_WRITE) != 0) {\r
95 ReadWriteUpdater (Base, SectionHeader.Misc.VirtualSize);\r
96 DEBUG ((DEBUG_INFO,\r
97 "%a: Mapping section %d of image at 0x%lx with RW-XN permissions\n",\r
98 __FUNCTION__, Index, ImageContext->ImageAddress));\r
99 } else {\r
100 DEBUG ((DEBUG_INFO,\r
101 "%a: Mapping section %d of image at 0x%lx with RO-XN permissions\n",\r
102 __FUNCTION__, Index, ImageContext->ImageAddress));\r
103 }\r
104 } else {\r
105 DEBUG ((DEBUG_INFO,\r
106 "%a: Ignoring section %d of image at 0x%lx with 0x%x permissions\n",\r
107 __FUNCTION__, Index, ImageContext->ImageAddress, SectionHeader.Characteristics));\r
108 }\r
109 SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER);\r
110 }\r
111\r
112 return RETURN_SUCCESS;\r
113}\r
114\r
115EFI_STATUS\r
116EFIAPI\r
117LocateStandaloneMmCorePeCoffData (\r
118 IN EFI_FIRMWARE_VOLUME_HEADER *BfvAddress,\r
119 IN OUT VOID **TeData,\r
120 IN OUT UINTN *TeDataSize\r
121 )\r
122{\r
123 EFI_FFS_FILE_HEADER *FileHeader = NULL;\r
124 EFI_STATUS Status;\r
125\r
126 Status = FfsFindNextFile (\r
127 EFI_FV_FILETYPE_SECURITY_CORE,\r
128 BfvAddress,\r
129 &FileHeader\r
130 );\r
131\r
132 if (EFI_ERROR (Status)) {\r
133 DEBUG ((DEBUG_ERROR, "Unable to locate Standalone MM FFS file - 0x%x\n",\r
134 Status));\r
135 return Status;\r
136 }\r
137\r
138 Status = FfsFindSectionData (EFI_SECTION_PE32, FileHeader, TeData, TeDataSize);\r
139 if (EFI_ERROR (Status)) {\r
4b28452d
AB
140 Status = FfsFindSectionData (EFI_SECTION_TE, FileHeader, TeData, TeDataSize);\r
141 if (EFI_ERROR (Status)) {\r
142 DEBUG ((DEBUG_ERROR, "Unable to locate Standalone MM Section data - %r\n",\r
143 Status));\r
144 return Status;\r
145 }\r
184558d0
SV
146 }\r
147\r
148 DEBUG ((DEBUG_INFO, "Found Standalone MM PE data - 0x%x\n", *TeData));\r
149 return Status;\r
150}\r
151\r
152STATIC\r
153EFI_STATUS\r
154GetPeCoffSectionInformation (\r
4b28452d
AB
155 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r
156 OUT UINT32 *SectionHeaderOffset,\r
157 OUT UINT16 *NumberOfSections\r
184558d0
SV
158 )\r
159{\r
160 RETURN_STATUS Status;\r
161 EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;\r
162 EFI_IMAGE_OPTIONAL_HEADER_UNION HdrData;\r
163 UINTN Size;\r
164 UINTN ReadSize;\r
165\r
166 ASSERT (ImageContext != NULL);\r
184558d0
SV
167 ASSERT (SectionHeaderOffset != NULL);\r
168 ASSERT (NumberOfSections != NULL);\r
169\r
4b28452d
AB
170 Status = PeCoffLoaderGetImageInfo (ImageContext);\r
171 if (RETURN_ERROR (Status)) {\r
172 DEBUG ((DEBUG_ERROR,\r
173 "%a: PeCoffLoaderGetImageInfo () failed (Status == %r)\n",\r
174 __FUNCTION__, Status));\r
175 return Status;\r
184558d0
SV
176 }\r
177\r
4b28452d 178 if (ImageContext->SectionAlignment < EFI_PAGE_SIZE) {\r
184558d0
SV
179 //\r
180 // The sections need to be at least 4 KB aligned, since that is the\r
181 // granularity at which we can tighten permissions.\r
182 //\r
4b28452d 183 if (!ImageContext->IsTeImage) {\r
184558d0
SV
184 DEBUG ((DEBUG_WARN,\r
185 "%a: non-TE Image at 0x%lx has SectionAlignment < 4 KB (%lu)\n",\r
4b28452d
AB
186 __FUNCTION__, ImageContext->ImageAddress, ImageContext->SectionAlignment));\r
187 return RETURN_UNSUPPORTED;\r
184558d0 188 }\r
4b28452d 189 ImageContext->SectionAlignment = EFI_PAGE_SIZE;\r
184558d0
SV
190 }\r
191\r
192 //\r
193 // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much\r
194 // data, but that should not hurt anything. Hdr.Pe32->OptionalHeader.Magic\r
195 // determines if this is a PE32 or PE32+ image. The magic is in the same\r
196 // location in both images.\r
197 //\r
198 Hdr.Union = &HdrData;\r
199 Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);\r
200 ReadSize = Size;\r
4b28452d
AB
201 Status = ImageContext->ImageRead (\r
202 ImageContext->Handle,\r
203 ImageContext->PeCoffHeaderOffset,\r
184558d0
SV
204 &Size,\r
205 Hdr.Pe32\r
206 );\r
207\r
208 if (RETURN_ERROR (Status) || (Size != ReadSize)) {\r
209 DEBUG ((DEBUG_ERROR,\r
210 "%a: TmpContext->ImageRead () failed (Status = %r)\n",\r
211 __FUNCTION__, Status));\r
212 return Status;\r
213 }\r
214\r
4b28452d
AB
215 if (!ImageContext->IsTeImage) {\r
216 ASSERT (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE);\r
217\r
218 *SectionHeaderOffset = ImageContext->PeCoffHeaderOffset + sizeof (UINT32) +\r
219 sizeof (EFI_IMAGE_FILE_HEADER);\r
220 *NumberOfSections = Hdr.Pe32->FileHeader.NumberOfSections;\r
221\r
222 switch (Hdr.Pe32->OptionalHeader.Magic) {\r
223 case EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC:\r
224 *SectionHeaderOffset += Hdr.Pe32->FileHeader.SizeOfOptionalHeader;\r
225 break;\r
226 case EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC:\r
227 *SectionHeaderOffset += Hdr.Pe32Plus->FileHeader.SizeOfOptionalHeader;\r
228 break;\r
229 default:\r
230 ASSERT (FALSE);\r
231 }\r
232 } else {\r
233 *SectionHeaderOffset = (UINTN)(sizeof (EFI_TE_IMAGE_HEADER));\r
234 *NumberOfSections = Hdr.Te->NumberOfSections;\r
235 ImageContext->ImageAddress -= (UINT32)Hdr.Te->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER);\r
184558d0 236 }\r
184558d0
SV
237 return RETURN_SUCCESS;\r
238}\r
239\r
240EFI_STATUS\r
241EFIAPI\r
242GetStandaloneMmCorePeCoffSections (\r
243 IN VOID *TeData,\r
244 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,\r
245 IN OUT UINT32 *SectionHeaderOffset,\r
246 IN OUT UINT16 *NumberOfSections\r
247 )\r
248{\r
249 EFI_STATUS Status;\r
184558d0
SV
250\r
251 // Initialize the Image Context\r
252 ZeroMem (ImageContext, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT));\r
253 ImageContext->Handle = TeData;\r
254 ImageContext->ImageRead = PeCoffLoaderImageReadFromMemory;\r
255\r
256 DEBUG ((DEBUG_INFO, "Found Standalone MM PE data - 0x%x\n", TeData));\r
257\r
4b28452d 258 Status = GetPeCoffSectionInformation (ImageContext, SectionHeaderOffset, NumberOfSections);\r
184558d0 259 if (EFI_ERROR (Status)) {\r
4b28452d 260 DEBUG ((DEBUG_ERROR, "Unable to locate Standalone MM Core PE-COFF Section information - %r\n", Status));\r
184558d0
SV
261 return Status;\r
262 }\r
263\r
264 DEBUG ((DEBUG_INFO, "Standalone MM Core PE-COFF SectionHeaderOffset - 0x%x, NumberOfSections - %d\n",\r
265 *SectionHeaderOffset, *NumberOfSections));\r
266\r
267 return Status;\r
268}\r