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