]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/SecurityStubDxe/Defer3rdPartyImageLoad.c
MdeModulePkg/Universal/Acpi/BootScriptExecutorDxe: Add support for PCD PcdPteMemoryEn...
[mirror_edk2.git] / MdeModulePkg / Universal / SecurityStubDxe / Defer3rdPartyImageLoad.c
CommitLineData
8be37a5c
RN
1/** @file\r
2 Implement defer image load services for user identification in UEFI2.2.\r
3\r
4Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14#include "Defer3rdPartyImageLoad.h"\r
15\r
16//\r
17// The structure to save the deferred 3rd party image information.\r
18//\r
19typedef struct {\r
20 EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;\r
21 BOOLEAN BootOption;\r
22 BOOLEAN Loaded;\r
23} DEFERRED_3RD_PARTY_IMAGE_INFO;\r
24\r
25//\r
26// The table to save the deferred 3rd party image item.\r
27//\r
28typedef struct {\r
29 UINTN Count; ///< deferred 3rd party image count\r
30 DEFERRED_3RD_PARTY_IMAGE_INFO *ImageInfo; ///< deferred 3rd party image item\r
31} DEFERRED_3RD_PARTY_IMAGE_TABLE;\r
32\r
e048823f 33BOOLEAN mImageLoadedAfterEndOfDxe = FALSE;\r
8be37a5c
RN
34BOOLEAN mEndOfDxe = FALSE;\r
35DEFERRED_3RD_PARTY_IMAGE_TABLE mDeferred3rdPartyImage = {\r
36 0, // Deferred image count\r
37 NULL // The deferred image info\r
38};\r
39\r
40EFI_DEFERRED_IMAGE_LOAD_PROTOCOL mDeferredImageLoad = {\r
41 GetDefferedImageInfo\r
42};\r
43\r
44/**\r
45 Return whether the file comes from FV.\r
46\r
47 @param[in] File This is a pointer to the device path of the file\r
48 that is being dispatched.\r
49\r
50 @retval TRUE File comes from FV.\r
51 @retval FALSE File doesn't come from FV.\r
52**/\r
53BOOLEAN\r
54FileFromFv (\r
55 IN CONST EFI_DEVICE_PATH_PROTOCOL *File\r
56 )\r
57{\r
58 EFI_STATUS Status;\r
59 EFI_HANDLE DeviceHandle;\r
60 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
61\r
62 //\r
63 // First check to see if File is from a Firmware Volume\r
64 //\r
65 DeviceHandle = NULL;\r
66 TempDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) File;\r
67 Status = gBS->LocateDevicePath (\r
68 &gEfiFirmwareVolume2ProtocolGuid,\r
69 &TempDevicePath,\r
70 &DeviceHandle\r
71 );\r
72 if (!EFI_ERROR (Status)) {\r
73 Status = gBS->OpenProtocol (\r
74 DeviceHandle,\r
75 &gEfiFirmwareVolume2ProtocolGuid,\r
76 NULL,\r
77 NULL,\r
78 NULL,\r
79 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
80 );\r
81 if (!EFI_ERROR (Status)) {\r
82 return TRUE;\r
83 }\r
84 }\r
85\r
86 return FALSE;\r
87}\r
88\r
89/**\r
90 Find the deferred image which matches the device path.\r
91\r
92 @param[in] ImageDevicePath A pointer to the device path of a image.\r
93 @param[in] BootOption Whether the image is a boot option.\r
94\r
95 @return Pointer to the found deferred image or NULL if not found.\r
96**/\r
97DEFERRED_3RD_PARTY_IMAGE_INFO *\r
98LookupImage (\r
99 IN CONST EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath,\r
100 IN BOOLEAN BootOption\r
101 )\r
102{\r
103 UINTN Index;\r
104 UINTN DevicePathSize;\r
105\r
106 DevicePathSize = GetDevicePathSize (ImageDevicePath);\r
107\r
108 for (Index = 0; Index < mDeferred3rdPartyImage.Count; Index++) {\r
109 if (CompareMem (ImageDevicePath, mDeferred3rdPartyImage.ImageInfo[Index].ImageDevicePath, DevicePathSize) == 0) {\r
110 ASSERT (mDeferred3rdPartyImage.ImageInfo[Index].BootOption == BootOption);\r
111 return &mDeferred3rdPartyImage.ImageInfo[Index];\r
112 }\r
113 }\r
114\r
115 return NULL;\r
116}\r
117\r
118/**\r
119 Add the image info to a deferred image list.\r
120\r
121 @param[in] ImageDevicePath A pointer to the device path of a image.\r
122 @param[in] BootOption Whether the image is a boot option.\r
123\r
124**/\r
125VOID\r
126QueueImage (\r
127 IN CONST EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath,\r
128 IN BOOLEAN BootOption\r
129 )\r
130{\r
131 DEFERRED_3RD_PARTY_IMAGE_INFO *ImageInfo;\r
132\r
133 //\r
134 // Expand memory for the new deferred image.\r
135 //\r
136 ImageInfo = ReallocatePool (\r
137 mDeferred3rdPartyImage.Count * sizeof (DEFERRED_3RD_PARTY_IMAGE_INFO),\r
138 (mDeferred3rdPartyImage.Count + 1) * sizeof (DEFERRED_3RD_PARTY_IMAGE_INFO),\r
139 mDeferred3rdPartyImage.ImageInfo\r
140 );\r
141 if (ImageInfo == NULL) {\r
142 return;\r
143 }\r
144 mDeferred3rdPartyImage.ImageInfo = ImageInfo;\r
145\r
146 //\r
147 // Save the deferred image information.\r
148 //\r
149 ImageInfo = &mDeferred3rdPartyImage.ImageInfo[mDeferred3rdPartyImage.Count];\r
150 ImageInfo->ImageDevicePath = DuplicateDevicePath (ImageDevicePath);\r
151 if (ImageInfo->ImageDevicePath == NULL) {\r
152 return;\r
153 }\r
154 ImageInfo->BootOption = BootOption;\r
155 ImageInfo->Loaded = FALSE;\r
156 mDeferred3rdPartyImage.Count++;\r
157}\r
158\r
159\r
160/**\r
161 Returns information about a deferred image.\r
162\r
163 This function returns information about a single deferred image. The deferred images are\r
164 numbered consecutively, starting with 0. If there is no image which corresponds to\r
165 ImageIndex, then EFI_NOT_FOUND is returned. All deferred images may be returned by\r
166 iteratively calling this function until EFI_NOT_FOUND is returned.\r
167 Image may be NULL and ImageSize set to 0 if the decision to defer execution was made\r
168 because of the location of the executable image, rather than its actual contents.\r
169\r
170 @param[in] This Points to this instance of the EFI_DEFERRED_IMAGE_LOAD_PROTOCOL.\r
171 @param[in] ImageIndex Zero-based index of the deferred index.\r
172 @param[out] ImageDevicePath On return, points to a pointer to the device path of the image.\r
173 The device path should not be freed by the caller.\r
174 @param[out] Image On return, points to the first byte of the image or NULL if the\r
175 image is not available. The image should not be freed by the caller\r
176 unless LoadImage() has been successfully called.\r
177 @param[out] ImageSize On return, the size of the image, or 0 if the image is not available.\r
178 @param[out] BootOption On return, points to TRUE if the image was intended as a boot option\r
179 or FALSE if it was not intended as a boot option.\r
180\r
181 @retval EFI_SUCCESS Image information returned successfully.\r
182 @retval EFI_NOT_FOUND ImageIndex does not refer to a valid image.\r
183 @retval EFI_INVALID_PARAMETER ImageDevicePath is NULL or Image is NULL or ImageSize is NULL or\r
184 BootOption is NULL.\r
185\r
186**/\r
187EFI_STATUS\r
188EFIAPI\r
189GetDefferedImageInfo (\r
190 IN EFI_DEFERRED_IMAGE_LOAD_PROTOCOL *This,\r
191 IN UINTN ImageIndex,\r
192 OUT EFI_DEVICE_PATH_PROTOCOL **ImageDevicePath,\r
193 OUT VOID **Image,\r
194 OUT UINTN *ImageSize,\r
195 OUT BOOLEAN *BootOption\r
196 )\r
197{\r
198 UINTN Index;\r
199 UINTN NewCount;\r
200\r
201 if ((This == NULL) || (ImageSize == NULL) || (Image == NULL)) {\r
202 return EFI_INVALID_PARAMETER;\r
203 }\r
204\r
205 if ((ImageDevicePath == NULL) || (BootOption == NULL)) {\r
206 return EFI_INVALID_PARAMETER;\r
207 }\r
208\r
209 //\r
210 // Remove the loaded images from the defer list in the first call.\r
211 //\r
212 if (ImageIndex == 0) {\r
213 NewCount = 0;\r
214 for (Index = 0; Index < mDeferred3rdPartyImage.Count; Index++) {\r
215 if (!mDeferred3rdPartyImage.ImageInfo[Index].Loaded) {\r
216 CopyMem (\r
217 &mDeferred3rdPartyImage.ImageInfo[NewCount],\r
218 &mDeferred3rdPartyImage.ImageInfo[Index],\r
219 sizeof (DEFERRED_3RD_PARTY_IMAGE_INFO)\r
220 );\r
221 NewCount++;\r
222 }\r
223 }\r
224\r
225 mDeferred3rdPartyImage.Count = NewCount;\r
226 }\r
227\r
228 if (ImageIndex >= mDeferred3rdPartyImage.Count) {\r
229 return EFI_NOT_FOUND;\r
230 }\r
231\r
232 //\r
233 // Get the request deferred image.\r
234 //\r
235 *ImageDevicePath = mDeferred3rdPartyImage.ImageInfo[ImageIndex].ImageDevicePath;\r
236 *BootOption = mDeferred3rdPartyImage.ImageInfo[ImageIndex].BootOption;\r
237 *Image = NULL;\r
238 *ImageSize = 0;\r
239\r
240 return EFI_SUCCESS;\r
241}\r
242\r
243/**\r
244 Callback function executed when the EndOfDxe event group is signaled.\r
245\r
246 @param[in] Event Event whose notification function is being invoked.\r
247 @param[in] Context The pointer to the notification function's context, which\r
248 is implementation-dependent.\r
249**/\r
250VOID\r
251EFIAPI\r
252EndOfDxe (\r
253 IN EFI_EVENT Event,\r
254 IN VOID *Context\r
255 )\r
256{\r
257 mEndOfDxe = TRUE;\r
258}\r
259\r
e048823f
RN
260/**\r
261 Event notification for gEfiDxeSmmReadyToLockProtocolGuid event.\r
262\r
263 This function reports failure if any deferred image is loaded before\r
264 this callback.\r
265 Platform should publish ReadyToLock protocol immediately after signaling\r
266 of the End of DXE Event.\r
267\r
268 @param Event The Event that is being processed, not used.\r
269 @param Context Event Context, not used.\r
270\r
271**/\r
272VOID\r
273EFIAPI\r
274DxeSmmReadyToLock (\r
275 IN EFI_EVENT Event,\r
276 IN VOID *Context\r
277 )\r
278{\r
279 EFI_STATUS Status;\r
280 VOID *Interface;\r
281\r
282 Status = gBS->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid, NULL, &Interface);\r
283 if (EFI_ERROR (Status)) {\r
284 return;\r
285 }\r
286\r
287 gBS->CloseEvent (Event);\r
288\r
289 if (mImageLoadedAfterEndOfDxe) {\r
290 //\r
291 // Platform should not dispatch the 3rd party images after signaling EndOfDxe event\r
292 // but before publishing DxeSmmReadyToLock protocol.\r
293 //\r
294 DEBUG ((\r
295 DEBUG_ERROR,\r
296 "[Security] 3rd party images must be dispatched after DxeSmmReadyToLock Protocol installation!\n"\r
297 ));\r
298 REPORT_STATUS_CODE (\r
299 EFI_ERROR_CODE | EFI_ERROR_UNRECOVERED,\r
300 (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_EC_ILLEGAL_SOFTWARE_STATE)\r
301 );\r
302 ASSERT (FALSE);\r
303 CpuDeadLoop ();\r
304 }\r
305}\r
306\r
8be37a5c
RN
307/**\r
308 Defer the 3rd party image load and installs Deferred Image Load Protocol.\r
309\r
310 @param[in] File This is a pointer to the device path of the file that\r
311 is being dispatched. This will optionally be used for\r
312 logging.\r
313 @param[in] BootPolicy A boot policy that was used to call LoadImage() UEFI service.\r
314\r
315 @retval EFI_SUCCESS The file is not 3rd party image and can be loaded immediately.\r
316 @retval EFI_ACCESS_DENIED The file is 3rd party image and needs deferred.\r
317**/\r
318EFI_STATUS\r
319Defer3rdPartyImageLoad (\r
320 IN CONST EFI_DEVICE_PATH_PROTOCOL *File,\r
321 IN BOOLEAN BootPolicy\r
322 )\r
323{\r
324 DEFERRED_3RD_PARTY_IMAGE_INFO *ImageInfo;\r
325\r
326 //\r
327 // Ignore if File is NULL.\r
328 //\r
329 if (File == NULL) {\r
330 return EFI_SUCCESS;\r
331 }\r
332\r
333 if (FileFromFv (File)) {\r
334 return EFI_SUCCESS;\r
335 }\r
336\r
337 ImageInfo = LookupImage (File, BootPolicy);\r
338\r
339 DEBUG_CODE (\r
340 CHAR16 *DevicePathStr;\r
341 DevicePathStr = ConvertDevicePathToText (File, FALSE, FALSE);\r
342 DEBUG ((\r
343 DEBUG_INFO,\r
344 "[Security] 3rd party image[%p] %s EndOfDxe: %s.\n", ImageInfo,\r
345 mEndOfDxe ? L"can be loaded after": L"is deferred to load before",\r
346 DevicePathStr\r
347 ));\r
348 if (DevicePathStr != NULL) {\r
349 FreePool (DevicePathStr);\r
350 }\r
351 );\r
352\r
353 if (mEndOfDxe) {\r
e048823f 354 mImageLoadedAfterEndOfDxe = TRUE;\r
8be37a5c
RN
355 //\r
356 // The image might be first time loaded after EndOfDxe,\r
357 // So ImageInfo can be NULL.\r
358 //\r
359 if (ImageInfo != NULL) {\r
360 ImageInfo->Loaded = TRUE;\r
361 }\r
362 return EFI_SUCCESS;\r
363 } else {\r
364 //\r
365 // The image might be second time loaded before EndOfDxe,\r
366 // So ImageInfo can be non-NULL.\r
367 //\r
368 if (ImageInfo == NULL) {\r
369 QueueImage (File, BootPolicy);\r
370 }\r
371 return EFI_ACCESS_DENIED;\r
372 }\r
373}\r
374\r
375/**\r
376 Installs DeferredImageLoad Protocol and listens EndOfDxe event.\r
377**/\r
378VOID\r
379Defer3rdPartyImageLoadInitialize (\r
380 VOID\r
381 )\r
382{\r
383 EFI_STATUS Status;\r
384 EFI_HANDLE Handle;\r
385 EFI_EVENT Event;\r
e048823f 386 VOID *Registration;\r
8be37a5c
RN
387\r
388 Handle = NULL;\r
389 Status = gBS->InstallMultipleProtocolInterfaces (\r
390 &Handle,\r
391 &gEfiDeferredImageLoadProtocolGuid,\r
392 &mDeferredImageLoad,\r
393 NULL\r
394 );\r
395 ASSERT_EFI_ERROR (Status);\r
396\r
397 Status = gBS->CreateEventEx (\r
398 EVT_NOTIFY_SIGNAL,\r
399 TPL_CALLBACK,\r
400 EndOfDxe,\r
401 NULL,\r
402 &gEfiEndOfDxeEventGroupGuid,\r
403 &Event\r
404 );\r
405 ASSERT_EFI_ERROR (Status);\r
e048823f
RN
406\r
407 EfiCreateProtocolNotifyEvent (\r
408 &gEfiDxeSmmReadyToLockProtocolGuid,\r
409 TPL_CALLBACK,\r
410 DxeSmmReadyToLock,\r
411 NULL,\r
412 &Registration\r
413 );\r
8be37a5c 414}\r