2 Implement defer image load services for user identification in UEFI2.2.
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
8 #include "Defer3rdPartyImageLoad.h"
11 // The structure to save the deferred 3rd party image information.
14 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
17 } DEFERRED_3RD_PARTY_IMAGE_INFO
;
20 // The table to save the deferred 3rd party image item.
23 UINTN Count
; ///< deferred 3rd party image count
24 DEFERRED_3RD_PARTY_IMAGE_INFO
*ImageInfo
; ///< deferred 3rd party image item
25 } DEFERRED_3RD_PARTY_IMAGE_TABLE
;
27 BOOLEAN mImageLoadedAfterEndOfDxe
= FALSE
;
28 BOOLEAN mEndOfDxe
= FALSE
;
29 DEFERRED_3RD_PARTY_IMAGE_TABLE mDeferred3rdPartyImage
= {
30 0, // Deferred image count
31 NULL
// The deferred image info
34 EFI_DEFERRED_IMAGE_LOAD_PROTOCOL mDeferredImageLoad
= {
39 Return whether the file comes from FV.
41 @param[in] File This is a pointer to the device path of the file
42 that is being dispatched.
44 @retval TRUE File comes from FV.
45 @retval FALSE File doesn't come from FV.
49 IN CONST EFI_DEVICE_PATH_PROTOCOL
*File
53 EFI_HANDLE DeviceHandle
;
54 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
57 // First check to see if File is from a Firmware Volume
60 TempDevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*)File
;
61 Status
= gBS
->LocateDevicePath (
62 &gEfiFirmwareVolume2ProtocolGuid
,
66 if (!EFI_ERROR (Status
)) {
67 Status
= gBS
->OpenProtocol (
69 &gEfiFirmwareVolume2ProtocolGuid
,
73 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
75 if (!EFI_ERROR (Status
)) {
84 Find the deferred image which matches the device path.
86 @param[in] ImageDevicePath A pointer to the device path of a image.
87 @param[in] BootOption Whether the image is a boot option.
89 @return Pointer to the found deferred image or NULL if not found.
91 DEFERRED_3RD_PARTY_IMAGE_INFO
*
93 IN CONST EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
,
100 DevicePathSize
= GetDevicePathSize (ImageDevicePath
);
102 for (Index
= 0; Index
< mDeferred3rdPartyImage
.Count
; Index
++) {
103 if (CompareMem (ImageDevicePath
, mDeferred3rdPartyImage
.ImageInfo
[Index
].ImageDevicePath
, DevicePathSize
) == 0) {
104 ASSERT (mDeferred3rdPartyImage
.ImageInfo
[Index
].BootOption
== BootOption
);
105 return &mDeferred3rdPartyImage
.ImageInfo
[Index
];
113 Add the image info to a deferred image list.
115 @param[in] ImageDevicePath A pointer to the device path of a image.
116 @param[in] BootOption Whether the image is a boot option.
121 IN CONST EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
,
122 IN BOOLEAN BootOption
125 DEFERRED_3RD_PARTY_IMAGE_INFO
*ImageInfo
;
128 // Expand memory for the new deferred image.
130 ImageInfo
= ReallocatePool (
131 mDeferred3rdPartyImage
.Count
* sizeof (DEFERRED_3RD_PARTY_IMAGE_INFO
),
132 (mDeferred3rdPartyImage
.Count
+ 1) * sizeof (DEFERRED_3RD_PARTY_IMAGE_INFO
),
133 mDeferred3rdPartyImage
.ImageInfo
135 if (ImageInfo
== NULL
) {
139 mDeferred3rdPartyImage
.ImageInfo
= ImageInfo
;
142 // Save the deferred image information.
144 ImageInfo
= &mDeferred3rdPartyImage
.ImageInfo
[mDeferred3rdPartyImage
.Count
];
145 ImageInfo
->ImageDevicePath
= DuplicateDevicePath (ImageDevicePath
);
146 if (ImageInfo
->ImageDevicePath
== NULL
) {
150 ImageInfo
->BootOption
= BootOption
;
151 ImageInfo
->Loaded
= FALSE
;
152 mDeferred3rdPartyImage
.Count
++;
156 Returns information about a deferred image.
158 This function returns information about a single deferred image. The deferred images are
159 numbered consecutively, starting with 0. If there is no image which corresponds to
160 ImageIndex, then EFI_NOT_FOUND is returned. All deferred images may be returned by
161 iteratively calling this function until EFI_NOT_FOUND is returned.
162 Image may be NULL and ImageSize set to 0 if the decision to defer execution was made
163 because of the location of the executable image, rather than its actual contents.
165 @param[in] This Points to this instance of the EFI_DEFERRED_IMAGE_LOAD_PROTOCOL.
166 @param[in] ImageIndex Zero-based index of the deferred index.
167 @param[out] ImageDevicePath On return, points to a pointer to the device path of the image.
168 The device path should not be freed by the caller.
169 @param[out] Image On return, points to the first byte of the image or NULL if the
170 image is not available. The image should not be freed by the caller
171 unless LoadImage() has been successfully called.
172 @param[out] ImageSize On return, the size of the image, or 0 if the image is not available.
173 @param[out] BootOption On return, points to TRUE if the image was intended as a boot option
174 or FALSE if it was not intended as a boot option.
176 @retval EFI_SUCCESS Image information returned successfully.
177 @retval EFI_NOT_FOUND ImageIndex does not refer to a valid image.
178 @retval EFI_INVALID_PARAMETER ImageDevicePath is NULL or Image is NULL or ImageSize is NULL or
184 GetDefferedImageInfo (
185 IN EFI_DEFERRED_IMAGE_LOAD_PROTOCOL
*This
,
187 OUT EFI_DEVICE_PATH_PROTOCOL
**ImageDevicePath
,
189 OUT UINTN
*ImageSize
,
190 OUT BOOLEAN
*BootOption
196 if ((This
== NULL
) || (ImageSize
== NULL
) || (Image
== NULL
)) {
197 return EFI_INVALID_PARAMETER
;
200 if ((ImageDevicePath
== NULL
) || (BootOption
== NULL
)) {
201 return EFI_INVALID_PARAMETER
;
205 // Remove the loaded images from the defer list in the first call.
207 if (ImageIndex
== 0) {
209 for (Index
= 0; Index
< mDeferred3rdPartyImage
.Count
; Index
++) {
210 if (!mDeferred3rdPartyImage
.ImageInfo
[Index
].Loaded
) {
212 &mDeferred3rdPartyImage
.ImageInfo
[NewCount
],
213 &mDeferred3rdPartyImage
.ImageInfo
[Index
],
214 sizeof (DEFERRED_3RD_PARTY_IMAGE_INFO
)
220 mDeferred3rdPartyImage
.Count
= NewCount
;
223 if (ImageIndex
>= mDeferred3rdPartyImage
.Count
) {
224 return EFI_NOT_FOUND
;
228 // Get the request deferred image.
230 *ImageDevicePath
= mDeferred3rdPartyImage
.ImageInfo
[ImageIndex
].ImageDevicePath
;
231 *BootOption
= mDeferred3rdPartyImage
.ImageInfo
[ImageIndex
].BootOption
;
239 Callback function executed when the EndOfDxe event group is signaled.
241 @param[in] Event Event whose notification function is being invoked.
242 @param[in] Context The pointer to the notification function's context, which
243 is implementation-dependent.
256 Event notification for gEfiDxeSmmReadyToLockProtocolGuid event.
258 This function reports failure if any deferred image is loaded before
260 Platform should publish ReadyToLock protocol immediately after signaling
261 of the End of DXE Event.
263 @param Event The Event that is being processed, not used.
264 @param Context Event Context, not used.
277 Status
= gBS
->LocateProtocol (&gEfiDxeSmmReadyToLockProtocolGuid
, NULL
, &Interface
);
278 if (EFI_ERROR (Status
)) {
282 gBS
->CloseEvent (Event
);
284 if (mImageLoadedAfterEndOfDxe
) {
286 // Platform should not dispatch the 3rd party images after signaling EndOfDxe event
287 // but before publishing DxeSmmReadyToLock protocol.
291 "[Security] 3rd party images must be dispatched after DxeSmmReadyToLock Protocol installation!\n"
294 EFI_ERROR_CODE
| EFI_ERROR_UNRECOVERED
,
295 (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_EC_ILLEGAL_SOFTWARE_STATE
)
303 Defer the 3rd party image load and installs Deferred Image Load Protocol.
305 @param[in] File This is a pointer to the device path of the file that
306 is being dispatched. This will optionally be used for
308 @param[in] BootPolicy A boot policy that was used to call LoadImage() UEFI service.
310 @retval EFI_SUCCESS The file is not 3rd party image and can be loaded immediately.
311 @retval EFI_ACCESS_DENIED The file is 3rd party image and needs deferred.
314 Defer3rdPartyImageLoad (
315 IN CONST EFI_DEVICE_PATH_PROTOCOL
*File
,
316 IN BOOLEAN BootPolicy
319 DEFERRED_3RD_PARTY_IMAGE_INFO
*ImageInfo
;
322 // Ignore if File is NULL.
328 if (FileFromFv (File
)) {
332 ImageInfo
= LookupImage (File
, BootPolicy
);
335 CHAR16
*DevicePathStr
;
337 DevicePathStr
= ConvertDevicePathToText (File
, FALSE
, FALSE
);
340 "[Security] 3rd party image[%p] %s EndOfDxe: %s.\n",
342 mEndOfDxe
? L
"can be loaded after" : L
"is deferred to load before",
345 if (DevicePathStr
!= NULL
) {
346 FreePool (DevicePathStr
);
352 mImageLoadedAfterEndOfDxe
= TRUE
;
354 // The image might be first time loaded after EndOfDxe,
355 // So ImageInfo can be NULL.
357 if (ImageInfo
!= NULL
) {
358 ImageInfo
->Loaded
= TRUE
;
364 // The image might be second time loaded before EndOfDxe,
365 // So ImageInfo can be non-NULL.
367 if (ImageInfo
== NULL
) {
368 QueueImage (File
, BootPolicy
);
371 return EFI_ACCESS_DENIED
;
376 Installs DeferredImageLoad Protocol and listens EndOfDxe event.
379 Defer3rdPartyImageLoadInitialize (
389 Status
= gBS
->InstallMultipleProtocolInterfaces (
391 &gEfiDeferredImageLoadProtocolGuid
,
395 ASSERT_EFI_ERROR (Status
);
397 Status
= gBS
->CreateEventEx (
402 &gEfiEndOfDxeEventGroupGuid
,
405 ASSERT_EFI_ERROR (Status
);
407 EfiCreateProtocolNotifyEvent (
408 &gEfiDxeSmmReadyToLockProtocolGuid
,