+/*
+ * File to implement an full version of EFI_BOOT_SERVICES, to go beyond
+ * what GNU-EFI provides. These functions were taken, with modification,
+ * from various EDK2 and earlier files (see comments preceding the
+ * functions and other blocks of code). The original source files
+ * bore the following copyright notice:
+ *
+ * Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
+ * This program and the accompanying materials are licensed and made available under
+ * the terms and conditions of the BSD License that accompanies this distribution.
+ * The full text of the license may be found at
+ * http://opensource.org/licenses/bsd-license.php.
+ *
+ * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+ *
+ */
+
+#include "bootsvcs.h"
+#include "refit_call_wrapper.h"
+
+// Following "global" constants are from AutoGen.c....
+EFI_GUID gEfiLoadedImageProtocolGuid = { 0x5B1B31A1, 0x9562, 0x11D2, { 0x8E, 0x3F, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B }};
+EFI_GUID gEfiDriverBindingProtocolGuid = { 0x18A031AB, 0xB443, 0x4D1A, { 0xA5, 0xC0, 0x0C, 0x09, 0x26, 0x1E, 0x9F, 0x71 }};
+EFI_GUID gEfiDriverConfiguration2ProtocolGuid = { 0xBFD7DC1D, 0x24F1, 0x40D9, { 0x82, 0xE7, 0x2E, 0x09, 0xBB, 0x6B, 0x4E, 0xBE }};
+EFI_GUID gEfiDriverConfigurationProtocolGuid = { 0x107A772B, 0xD5E1, 0x11D4, { 0x9A, 0x46, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }};
+EFI_GUID gEfiDriverDiagnosticsProtocolGuid = { 0x0784924F, 0xE296, 0x11D4, { 0x9A, 0x49, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }};
+EFI_GUID gEfiDriverDiagnostics2ProtocolGuid = { 0x4D330321, 0x025F, 0x4AAC, { 0x90, 0xD8, 0x5E, 0xD9, 0x00, 0x17, 0x3B, 0x63 }};
+EFI_GUID gEfiComponentNameProtocolGuid = { 0x107A772C, 0xD5E1, 0x11D4, { 0x9A, 0x46, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }};
+EFI_GUID gEfiComponentName2ProtocolGuid = { 0x6A7A5CFF, 0xE8D9, 0x4F70, { 0xBA, 0xDA, 0x75, 0xAB, 0x30, 0x25, 0xCE, 0x14 }};
+EFI_GUID gEfiDevicePathProtocolGuid = { 0x09576E91, 0x6D3F, 0x11D2, { 0x8E, 0x39, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B }};
+
+// Below is from http://git.etherboot.org/?p=mirror/efi/shell/.git;a=commitdiff;h=b1b0c63423cac54dc964c2930e04aebb46a946ec;
+// Seems to have been replaced by ParseHandleDatabaseByRelationshipWithType(), but the latter isn't working for me....
+EFI_STATUS
+LibScanHandleDatabase (
+ EFI_HANDLE DriverBindingHandle, OPTIONAL
+ UINT32 *DriverBindingHandleIndex, OPTIONAL
+ EFI_HANDLE ControllerHandle, OPTIONAL
+ UINT32 *ControllerHandleIndex, OPTIONAL
+ UINTN *HandleCount,
+ EFI_HANDLE **HandleBuffer,
+ UINT32 **HandleType
+ )
+
+{
+ EFI_STATUS Status;
+ UINTN HandleIndex;
+ EFI_GUID **ProtocolGuidArray;
+ UINTN ArrayCount;
+ UINTN ProtocolIndex;
+ EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;
+ UINTN OpenInfoCount;
+ UINTN OpenInfoIndex;
+ UINTN ChildIndex;
+ BOOLEAN DriverBindingHandleIndexValid;
+ BOOLEAN ControllerHandleIndexValid;
+
+ DriverBindingHandleIndexValid = FALSE;
+ if (DriverBindingHandleIndex != NULL) {
+ *DriverBindingHandleIndex = 0xffffffff;
+ }
+
+ ControllerHandleIndexValid = FALSE;
+ if (ControllerHandleIndex != NULL) {
+ *ControllerHandleIndex = 0xffffffff;
+ }
+
+ *HandleCount = 0;
+ *HandleBuffer = NULL;
+ *HandleType = NULL;
+
+ //
+ // Retrieve the list of all handles from the handle database
+ //
+
+ Status = refit_call5_wrapper(gBS->LocateHandleBuffer,
+ AllHandles,
+ NULL,
+ NULL,
+ HandleCount,
+ HandleBuffer
+ );
+ if (EFI_ERROR (Status)) {
+ goto Error;
+ }
+
+ *HandleType = AllocatePool (*HandleCount * sizeof (UINT32));
+ if (*HandleType == NULL) {
+ goto Error;
+ }
+
+ for (HandleIndex = 0; HandleIndex < *HandleCount; HandleIndex++) {
+ //
+ // Assume that the handle type is unknown
+ //
+ (*HandleType)[HandleIndex] = EFI_HANDLE_TYPE_UNKNOWN;
+
+ if (DriverBindingHandle != NULL &&
+ DriverBindingHandleIndex != NULL &&
+ (*HandleBuffer)[HandleIndex] == DriverBindingHandle
+ ) {
+ *DriverBindingHandleIndex = (UINT32) HandleIndex;
+ DriverBindingHandleIndexValid = TRUE;
+ }
+
+ if (ControllerHandle != NULL && ControllerHandleIndex != NULL && (*HandleBuffer)[HandleIndex] == ControllerHandle) {
+ *ControllerHandleIndex = (UINT32) HandleIndex;
+ ControllerHandleIndexValid = TRUE;
+ }
+
+ }
+
+ for (HandleIndex = 0; HandleIndex < *HandleCount; HandleIndex++) {
+ //
+ // Retrieve the list of all the protocols on each handle
+ //
+
+ Status = refit_call3_wrapper(gBS->ProtocolsPerHandle,
+ (*HandleBuffer)[HandleIndex],
+ &ProtocolGuidArray,
+ &ArrayCount
+ );
+ if (!EFI_ERROR (Status)) {
+
+ for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {
+
+ if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiLoadedImageProtocolGuid) == 0) {
+ (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_IMAGE_HANDLE;
+ }
+
+ if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverBindingProtocolGuid) == 0) {
+ (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DRIVER_BINDING_HANDLE;
+ }
+
+ if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverConfigurationProtocolGuid) == 0) {
+ (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DRIVER_CONFIGURATION_HANDLE;
+ }
+
+ if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverDiagnosticsProtocolGuid) == 0) {
+ (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DRIVER_DIAGNOSTICS_HANDLE;
+ }
+
+ if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiComponentNameProtocolGuid) == 0) {
+ (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_COMPONENT_NAME_HANDLE;
+ }
+
+ if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDevicePathProtocolGuid) == 0) {
+ (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DEVICE_HANDLE;
+ }
+ //
+ // Retrieve the list of agents that have opened each protocol
+ //
+
+ Status = refit_call4_wrapper(gBS->OpenProtocolInformation,
+ (*HandleBuffer)[HandleIndex],
+ ProtocolGuidArray[ProtocolIndex],
+ &OpenInfo,
+ &OpenInfoCount
+ );
+ if (!EFI_ERROR (Status)) {
+
+ for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
+ if (DriverBindingHandle != NULL && OpenInfo[OpenInfoIndex].AgentHandle == DriverBindingHandle) {
+ if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) == EFI_OPEN_PROTOCOL_BY_DRIVER) {
+ //
+ // Mark the device handle as being managed by the driver specified by DriverBindingHandle
+ //
+ (*HandleType)[HandleIndex] |= (EFI_HANDLE_TYPE_DEVICE_HANDLE | EFI_HANDLE_TYPE_CONTROLLER_HANDLE);
+ //
+ // Mark the DriverBindingHandle as being a driver that is managing at least one controller
+ //
+ if (DriverBindingHandleIndexValid) {
+ (*HandleType)[*DriverBindingHandleIndex] |= EFI_HANDLE_TYPE_DEVICE_DRIVER;
+ }
+ }
+
+ if ((
+ OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
+ ) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
+ ) {
+ //
+ // Mark the DriverBindingHandle as being a driver that is managing at least one child controller
+ //
+ if (DriverBindingHandleIndexValid) {
+ (*HandleType)[*DriverBindingHandleIndex] |= EFI_HANDLE_TYPE_BUS_DRIVER;
+ }
+ }
+
+ if (ControllerHandle != NULL && (*HandleBuffer)[HandleIndex] == ControllerHandle) {
+ if ((
+ OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
+ ) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
+ ) {
+ for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
+ if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].ControllerHandle) {
+ (*HandleType)[ChildIndex] |= (EFI_HANDLE_TYPE_DEVICE_HANDLE | EFI_HANDLE_TYPE_CHILD_HANDLE);
+ }
+ }
+ }
+ }
+ }
+
+ if (DriverBindingHandle == NULL && OpenInfo[OpenInfoIndex].ControllerHandle == ControllerHandle) {
+ if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) == EFI_OPEN_PROTOCOL_BY_DRIVER) {
+ for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
+ if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].AgentHandle) {
+ (*HandleType)[ChildIndex] |= EFI_HANDLE_TYPE_DEVICE_DRIVER;
+ }
+ }
+ }
+
+ if ((
+ OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
+ ) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
+ ) {
+ (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_PARENT_HANDLE;
+ for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
+ if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].AgentHandle) {
+ (*HandleType)[ChildIndex] |= EFI_HANDLE_TYPE_BUS_DRIVER;
+ }
+ }
+ }
+ }
+ }
+
+ FreePool (OpenInfo);
+ }
+ }
+
+ FreePool (ProtocolGuidArray);
+ }
+ }
+
+ return EFI_SUCCESS;
+
+Error:
+ if (*HandleType != NULL) {
+ FreePool (*HandleType);
+ }
+
+ if (*HandleBuffer != NULL) {
+ FreePool (*HandleBuffer);
+ }
+
+ *HandleCount = 0;
+ *HandleBuffer = NULL;
+ *HandleType = NULL;
+
+ return Status;
+} /* EFI_STATUS LibScanHandleDatabase() */