+ EFI_STATUS Status;\r
+ UINTN NoHandles;\r
+ EFI_HANDLE *HandleBuffer;\r
+ UINTN Index;\r
+\r
+ //\r
+ // Check input parameters\r
+ //\r
+ if (Protocol == NULL || NoProtocols == NULL || Buffer == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Initialze output parameters\r
+ //\r
+ *NoProtocols = 0;\r
+ *Buffer = NULL;\r
+\r
+ //\r
+ // Retrieve the array of handles that support Protocol\r
+ //\r
+ Status = gBS->LocateHandleBuffer (\r
+ ByProtocol,\r
+ Protocol,\r
+ NULL,\r
+ &NoHandles,\r
+ &HandleBuffer\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Allocate array of protocol instances\r
+ //\r
+ Status = gBS->AllocatePool (\r
+ EfiBootServicesData,\r
+ NoHandles * sizeof (VOID *),\r
+ (VOID **)Buffer\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ //\r
+ // Free the handle buffer\r
+ //\r
+ gBS->FreePool (HandleBuffer);\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ ZeroMem (*Buffer, NoHandles * sizeof (VOID *));\r
+\r
+ //\r
+ // Lookup Protocol on each handle in HandleBuffer to fill in the array of\r
+ // protocol instances. Handle case where protocol instance was present when\r
+ // LocateHandleBuffer() was called, but is not present when HandleProtocol()\r
+ // is called.\r
+ //\r
+ for (Index = 0, *NoProtocols = 0; Index < NoHandles; Index++) {\r
+ Status = gBS->HandleProtocol (\r
+ HandleBuffer[Index],\r
+ Protocol,\r
+ &((*Buffer)[*NoProtocols])\r
+ );\r
+ if (!EFI_ERROR (Status)) {\r
+ (*NoProtocols)++;\r
+ }\r
+ }\r
+\r
+ //\r
+ // Free the handle buffer\r
+ //\r
+ gBS->FreePool (HandleBuffer);\r
+\r
+ //\r
+ // Make sure at least one protocol instance was found\r
+ //\r
+ if (*NoProtocols == 0) {\r
+ gBS->FreePool (*Buffer);\r
+ *Buffer = NULL;\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r