2 * File to implement an full version of EFI_BOOT_SERVICES, to go beyond
3 * what GNU-EFI provides. These functions were taken, with modification,
4 * from various EDK2 and earlier files (see comments preceding the
5 * functions and other blocks of code). The original source files
6 * bore the following copyright notice:
8 * Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
9 * This program and the accompanying materials are licensed and made available under
10 * the terms and conditions of the BSD License that accompanies this distribution.
11 * The full text of the license may be found at
12 * http://opensource.org/licenses/bsd-license.php.
14 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include "refit_call_wrapper.h"
22 // Following "global" constants are from AutoGen.c....
23 EFI_GUID gEfiLoadedImageProtocolGuid
= { 0x5B1B31A1, 0x9562, 0x11D2, { 0x8E, 0x3F, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B }};
24 EFI_GUID gEfiDriverBindingProtocolGuid
= { 0x18A031AB, 0xB443, 0x4D1A, { 0xA5, 0xC0, 0x0C, 0x09, 0x26, 0x1E, 0x9F, 0x71 }};
25 EFI_GUID gEfiDriverConfiguration2ProtocolGuid
= { 0xBFD7DC1D, 0x24F1, 0x40D9, { 0x82, 0xE7, 0x2E, 0x09, 0xBB, 0x6B, 0x4E, 0xBE }};
26 EFI_GUID gEfiDriverConfigurationProtocolGuid
= { 0x107A772B, 0xD5E1, 0x11D4, { 0x9A, 0x46, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }};
27 EFI_GUID gEfiDriverDiagnosticsProtocolGuid
= { 0x0784924F, 0xE296, 0x11D4, { 0x9A, 0x49, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }};
28 EFI_GUID gEfiDriverDiagnostics2ProtocolGuid
= { 0x4D330321, 0x025F, 0x4AAC, { 0x90, 0xD8, 0x5E, 0xD9, 0x00, 0x17, 0x3B, 0x63 }};
29 EFI_GUID gEfiComponentNameProtocolGuid
= { 0x107A772C, 0xD5E1, 0x11D4, { 0x9A, 0x46, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }};
30 EFI_GUID gEfiComponentName2ProtocolGuid
= { 0x6A7A5CFF, 0xE8D9, 0x4F70, { 0xBA, 0xDA, 0x75, 0xAB, 0x30, 0x25, 0xCE, 0x14 }};
31 EFI_GUID gEfiDevicePathProtocolGuid
= { 0x09576E91, 0x6D3F, 0x11D2, { 0x8E, 0x39, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B }};
33 // Below is from http://git.etherboot.org/?p=mirror/efi/shell/.git;a=commitdiff;h=b1b0c63423cac54dc964c2930e04aebb46a946ec;
34 // Seems to have been replaced by ParseHandleDatabaseByRelationshipWithType(), but the latter isn't working for me....
36 LibScanHandleDatabase (
37 EFI_HANDLE DriverBindingHandle
, OPTIONAL
38 UINT32
*DriverBindingHandleIndex
, OPTIONAL
39 EFI_HANDLE ControllerHandle
, OPTIONAL
40 UINT32
*ControllerHandleIndex
, OPTIONAL
42 EFI_HANDLE
**HandleBuffer
,
49 EFI_GUID
**ProtocolGuidArray
;
52 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY
*OpenInfo
;
56 BOOLEAN DriverBindingHandleIndexValid
;
57 BOOLEAN ControllerHandleIndexValid
;
59 DriverBindingHandleIndexValid
= FALSE
;
60 if (DriverBindingHandleIndex
!= NULL
) {
61 *DriverBindingHandleIndex
= 0xffffffff;
64 ControllerHandleIndexValid
= FALSE
;
65 if (ControllerHandleIndex
!= NULL
) {
66 *ControllerHandleIndex
= 0xffffffff;
74 // Retrieve the list of all handles from the handle database
77 Status
= refit_call5_wrapper(gBS
->LocateHandleBuffer
,
84 if (EFI_ERROR (Status
)) {
88 *HandleType
= AllocatePool (*HandleCount
* sizeof (UINT32
));
89 if (*HandleType
== NULL
) {
93 for (HandleIndex
= 0; HandleIndex
< *HandleCount
; HandleIndex
++) {
95 // Assume that the handle type is unknown
97 (*HandleType
)[HandleIndex
] = EFI_HANDLE_TYPE_UNKNOWN
;
99 if (DriverBindingHandle
!= NULL
&&
100 DriverBindingHandleIndex
!= NULL
&&
101 (*HandleBuffer
)[HandleIndex
] == DriverBindingHandle
103 *DriverBindingHandleIndex
= (UINT32
) HandleIndex
;
104 DriverBindingHandleIndexValid
= TRUE
;
107 if (ControllerHandle
!= NULL
&& ControllerHandleIndex
!= NULL
&& (*HandleBuffer
)[HandleIndex
] == ControllerHandle
) {
108 *ControllerHandleIndex
= (UINT32
) HandleIndex
;
109 ControllerHandleIndexValid
= TRUE
;
114 for (HandleIndex
= 0; HandleIndex
< *HandleCount
; HandleIndex
++) {
116 // Retrieve the list of all the protocols on each handle
119 Status
= refit_call3_wrapper(gBS
->ProtocolsPerHandle
,
120 (*HandleBuffer
)[HandleIndex
],
124 if (!EFI_ERROR (Status
)) {
126 for (ProtocolIndex
= 0; ProtocolIndex
< ArrayCount
; ProtocolIndex
++) {
128 if (CompareGuid (ProtocolGuidArray
[ProtocolIndex
], &gEfiLoadedImageProtocolGuid
) == 0) {
129 (*HandleType
)[HandleIndex
] |= EFI_HANDLE_TYPE_IMAGE_HANDLE
;
132 if (CompareGuid (ProtocolGuidArray
[ProtocolIndex
], &gEfiDriverBindingProtocolGuid
) == 0) {
133 (*HandleType
)[HandleIndex
] |= EFI_HANDLE_TYPE_DRIVER_BINDING_HANDLE
;
136 if (CompareGuid (ProtocolGuidArray
[ProtocolIndex
], &gEfiDriverConfigurationProtocolGuid
) == 0) {
137 (*HandleType
)[HandleIndex
] |= EFI_HANDLE_TYPE_DRIVER_CONFIGURATION_HANDLE
;
140 if (CompareGuid (ProtocolGuidArray
[ProtocolIndex
], &gEfiDriverDiagnosticsProtocolGuid
) == 0) {
141 (*HandleType
)[HandleIndex
] |= EFI_HANDLE_TYPE_DRIVER_DIAGNOSTICS_HANDLE
;
144 if (CompareGuid (ProtocolGuidArray
[ProtocolIndex
], &gEfiComponentNameProtocolGuid
) == 0) {
145 (*HandleType
)[HandleIndex
] |= EFI_HANDLE_TYPE_COMPONENT_NAME_HANDLE
;
148 if (CompareGuid (ProtocolGuidArray
[ProtocolIndex
], &gEfiDevicePathProtocolGuid
) == 0) {
149 (*HandleType
)[HandleIndex
] |= EFI_HANDLE_TYPE_DEVICE_HANDLE
;
152 // Retrieve the list of agents that have opened each protocol
155 Status
= refit_call4_wrapper(gBS
->OpenProtocolInformation
,
156 (*HandleBuffer
)[HandleIndex
],
157 ProtocolGuidArray
[ProtocolIndex
],
161 if (!EFI_ERROR (Status
)) {
163 for (OpenInfoIndex
= 0; OpenInfoIndex
< OpenInfoCount
; OpenInfoIndex
++) {
164 if (DriverBindingHandle
!= NULL
&& OpenInfo
[OpenInfoIndex
].AgentHandle
== DriverBindingHandle
) {
165 if ((OpenInfo
[OpenInfoIndex
].Attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) == EFI_OPEN_PROTOCOL_BY_DRIVER
) {
167 // Mark the device handle as being managed by the driver specified by DriverBindingHandle
169 (*HandleType
)[HandleIndex
] |= (EFI_HANDLE_TYPE_DEVICE_HANDLE
| EFI_HANDLE_TYPE_CONTROLLER_HANDLE
);
171 // Mark the DriverBindingHandle as being a driver that is managing at least one controller
173 if (DriverBindingHandleIndexValid
) {
174 (*HandleType
)[*DriverBindingHandleIndex
] |= EFI_HANDLE_TYPE_DEVICE_DRIVER
;
179 OpenInfo
[OpenInfoIndex
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
180 ) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
183 // Mark the DriverBindingHandle as being a driver that is managing at least one child controller
185 if (DriverBindingHandleIndexValid
) {
186 (*HandleType
)[*DriverBindingHandleIndex
] |= EFI_HANDLE_TYPE_BUS_DRIVER
;
190 if (ControllerHandle
!= NULL
&& (*HandleBuffer
)[HandleIndex
] == ControllerHandle
) {
192 OpenInfo
[OpenInfoIndex
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
193 ) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
195 for (ChildIndex
= 0; ChildIndex
< *HandleCount
; ChildIndex
++) {
196 if ((*HandleBuffer
)[ChildIndex
] == OpenInfo
[OpenInfoIndex
].ControllerHandle
) {
197 (*HandleType
)[ChildIndex
] |= (EFI_HANDLE_TYPE_DEVICE_HANDLE
| EFI_HANDLE_TYPE_CHILD_HANDLE
);
204 if (DriverBindingHandle
== NULL
&& OpenInfo
[OpenInfoIndex
].ControllerHandle
== ControllerHandle
) {
205 if ((OpenInfo
[OpenInfoIndex
].Attributes
& EFI_OPEN_PROTOCOL_BY_DRIVER
) == EFI_OPEN_PROTOCOL_BY_DRIVER
) {
206 for (ChildIndex
= 0; ChildIndex
< *HandleCount
; ChildIndex
++) {
207 if ((*HandleBuffer
)[ChildIndex
] == OpenInfo
[OpenInfoIndex
].AgentHandle
) {
208 (*HandleType
)[ChildIndex
] |= EFI_HANDLE_TYPE_DEVICE_DRIVER
;
214 OpenInfo
[OpenInfoIndex
].Attributes
& EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
215 ) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
217 (*HandleType
)[HandleIndex
] |= EFI_HANDLE_TYPE_PARENT_HANDLE
;
218 for (ChildIndex
= 0; ChildIndex
< *HandleCount
; ChildIndex
++) {
219 if ((*HandleBuffer
)[ChildIndex
] == OpenInfo
[OpenInfoIndex
].AgentHandle
) {
220 (*HandleType
)[ChildIndex
] |= EFI_HANDLE_TYPE_BUS_DRIVER
;
231 FreePool (ProtocolGuidArray
);
238 if (*HandleType
!= NULL
) {
239 FreePool (*HandleType
);
242 if (*HandleBuffer
!= NULL
) {
243 FreePool (*HandleBuffer
);
247 *HandleBuffer
= NULL
;
251 } /* EFI_STATUS LibScanHandleDatabase() */