+/**\r
+ Find the image handle whose path equals to ImagePath.\r
+\r
+ @param ImagePath Image path.\r
+\r
+ @return Image handle.\r
+**/\r
+EFI_HANDLE\r
+LocateImageHandle (\r
+ IN EFI_DEVICE_PATH_PROTOCOL *ImagePath\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_HANDLE *Handles;\r
+ UINTN Index;\r
+ UINTN HandleNum;\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
+ UINTN ImagePathSize;\r
+ EFI_HANDLE ImageHandle;\r
+\r
+ Status = gBS->LocateHandleBuffer (\r
+ ByProtocol,\r
+ &gEfiLoadedImageDevicePathProtocolGuid,\r
+ NULL,\r
+ &HandleNum,\r
+ &Handles\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return NULL;\r
+ }\r
+\r
+ ImageHandle = NULL;\r
+ ImagePathSize = GetDevicePathSize (ImagePath);\r
+\r
+ for (Index = 0; Index < HandleNum; Index++) {\r
+ Status = gBS->HandleProtocol (Handles[Index], &gEfiLoadedImageDevicePathProtocolGuid, (VOID **) &DevicePath);\r
+ if (EFI_ERROR (Status)) {\r
+ continue;\r
+ }\r
+ if ((ImagePathSize == GetDevicePathSize (DevicePath)) &&\r
+ (CompareMem (ImagePath, DevicePath, ImagePathSize) == 0)\r
+ ) {\r
+ ImageHandle = Handles[Index];\r
+ break;\r
+ }\r
+ }\r
+\r
+ FreePool (Handles);\r
+ return ImageHandle;\r
+}\r