]> code.delx.au - refind/blob - refind/driver_support.c
Clean up of driver support; now requires newer GNU-EFI package.
[refind] / refind / driver_support.c
1 /*
2 * File to implement LibScanHandleDatabase(), which is used by rEFInd's
3 * driver-loading code (inherited from rEFIt), but which has not been
4 * implemented in GNU-EFI and seems to have been dropped from current
5 * versions of the Tianocore library. This function was taken from a git
6 * site with EFI code, but some of the constants it uses were taken from
7 * a more recent EDK2 package (see below for details). The original files
8 * bore the following copyright notice:
9 *
10 * Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
11 * This program and the accompanying materials are licensed and made available under
12 * the terms and conditions of the BSD License that accompanies this distribution.
13 * The full text of the license may be found at
14 * http://opensource.org/licenses/bsd-license.php.
15 *
16 * THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 *
19 */
20
21 #include "driver_support.h"
22 #include "refit_call_wrapper.h"
23
24 // Following "global" constants are from EDK2's AutoGen.c....
25 EFI_GUID gEfiLoadedImageProtocolGuid = { 0x5B1B31A1, 0x9562, 0x11D2, { 0x8E, 0x3F, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B }};
26 EFI_GUID gEfiDriverBindingProtocolGuid = { 0x18A031AB, 0xB443, 0x4D1A, { 0xA5, 0xC0, 0x0C, 0x09, 0x26, 0x1E, 0x9F, 0x71 }};
27 EFI_GUID gEfiDriverConfiguration2ProtocolGuid = { 0xBFD7DC1D, 0x24F1, 0x40D9, { 0x82, 0xE7, 0x2E, 0x09, 0xBB, 0x6B, 0x4E, 0xBE }};
28 EFI_GUID gEfiDriverConfigurationProtocolGuid = { 0x107A772B, 0xD5E1, 0x11D4, { 0x9A, 0x46, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }};
29 EFI_GUID gEfiDriverDiagnosticsProtocolGuid = { 0x0784924F, 0xE296, 0x11D4, { 0x9A, 0x49, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }};
30 EFI_GUID gEfiDriverDiagnostics2ProtocolGuid = { 0x4D330321, 0x025F, 0x4AAC, { 0x90, 0xD8, 0x5E, 0xD9, 0x00, 0x17, 0x3B, 0x63 }};
31 EFI_GUID gEfiComponentNameProtocolGuid = { 0x107A772C, 0xD5E1, 0x11D4, { 0x9A, 0x46, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }};
32 EFI_GUID gEfiComponentName2ProtocolGuid = { 0x6A7A5CFF, 0xE8D9, 0x4F70, { 0xBA, 0xDA, 0x75, 0xAB, 0x30, 0x25, 0xCE, 0x14 }};
33 EFI_GUID gEfiDevicePathProtocolGuid = { 0x09576E91, 0x6D3F, 0x11D2, { 0x8E, 0x39, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B }};
34
35 // Below is from http://git.etherboot.org/?p=mirror/efi/shell/.git;a=commitdiff;h=b1b0c63423cac54dc964c2930e04aebb46a946ec;
36 // Seems to have been replaced by ParseHandleDatabaseByRelationshipWithType(), but the latter isn't working for me....
37 EFI_STATUS
38 LibScanHandleDatabase (
39 EFI_HANDLE DriverBindingHandle, OPTIONAL
40 UINT32 *DriverBindingHandleIndex, OPTIONAL
41 EFI_HANDLE ControllerHandle, OPTIONAL
42 UINT32 *ControllerHandleIndex, OPTIONAL
43 UINTN *HandleCount,
44 EFI_HANDLE **HandleBuffer,
45 UINT32 **HandleType
46 )
47
48 {
49 EFI_STATUS Status;
50 UINTN HandleIndex;
51 EFI_GUID **ProtocolGuidArray;
52 UINTN ArrayCount;
53 UINTN ProtocolIndex;
54 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;
55 UINTN OpenInfoCount;
56 UINTN OpenInfoIndex;
57 UINTN ChildIndex;
58 BOOLEAN DriverBindingHandleIndexValid;
59 BOOLEAN ControllerHandleIndexValid;
60
61 DriverBindingHandleIndexValid = FALSE;
62 if (DriverBindingHandleIndex != NULL) {
63 *DriverBindingHandleIndex = 0xffffffff;
64 }
65
66 ControllerHandleIndexValid = FALSE;
67 if (ControllerHandleIndex != NULL) {
68 *ControllerHandleIndex = 0xffffffff;
69 }
70
71 *HandleCount = 0;
72 *HandleBuffer = NULL;
73 *HandleType = NULL;
74
75 //
76 // Retrieve the list of all handles from the handle database
77 //
78
79 Status = refit_call5_wrapper(BS->LocateHandleBuffer,
80 AllHandles,
81 NULL,
82 NULL,
83 HandleCount,
84 HandleBuffer
85 );
86 if (EFI_ERROR (Status)) {
87 goto Error;
88 }
89
90 *HandleType = AllocatePool (*HandleCount * sizeof (UINT32));
91 if (*HandleType == NULL) {
92 goto Error;
93 }
94
95 for (HandleIndex = 0; HandleIndex < *HandleCount; HandleIndex++) {
96 //
97 // Assume that the handle type is unknown
98 //
99 (*HandleType)[HandleIndex] = EFI_HANDLE_TYPE_UNKNOWN;
100
101 if (DriverBindingHandle != NULL &&
102 DriverBindingHandleIndex != NULL &&
103 (*HandleBuffer)[HandleIndex] == DriverBindingHandle
104 ) {
105 *DriverBindingHandleIndex = (UINT32) HandleIndex;
106 DriverBindingHandleIndexValid = TRUE;
107 }
108
109 if (ControllerHandle != NULL && ControllerHandleIndex != NULL && (*HandleBuffer)[HandleIndex] == ControllerHandle) {
110 *ControllerHandleIndex = (UINT32) HandleIndex;
111 ControllerHandleIndexValid = TRUE;
112 }
113
114 }
115
116 for (HandleIndex = 0; HandleIndex < *HandleCount; HandleIndex++) {
117 //
118 // Retrieve the list of all the protocols on each handle
119 //
120
121 Status = refit_call3_wrapper(BS->ProtocolsPerHandle,
122 (*HandleBuffer)[HandleIndex],
123 &ProtocolGuidArray,
124 &ArrayCount
125 );
126 if (!EFI_ERROR (Status)) {
127
128 for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {
129
130 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiLoadedImageProtocolGuid) == 0) {
131 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_IMAGE_HANDLE;
132 }
133
134 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverBindingProtocolGuid) == 0) {
135 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DRIVER_BINDING_HANDLE;
136 }
137
138 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverConfigurationProtocolGuid) == 0) {
139 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DRIVER_CONFIGURATION_HANDLE;
140 }
141
142 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverDiagnosticsProtocolGuid) == 0) {
143 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DRIVER_DIAGNOSTICS_HANDLE;
144 }
145
146 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiComponentNameProtocolGuid) == 0) {
147 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_COMPONENT_NAME_HANDLE;
148 }
149
150 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDevicePathProtocolGuid) == 0) {
151 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DEVICE_HANDLE;
152 }
153 //
154 // Retrieve the list of agents that have opened each protocol
155 //
156
157 Status = refit_call4_wrapper(BS->OpenProtocolInformation,
158 (*HandleBuffer)[HandleIndex],
159 ProtocolGuidArray[ProtocolIndex],
160 &OpenInfo,
161 &OpenInfoCount
162 );
163 if (!EFI_ERROR (Status)) {
164
165 for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
166 if (DriverBindingHandle != NULL && OpenInfo[OpenInfoIndex].AgentHandle == DriverBindingHandle) {
167 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) == EFI_OPEN_PROTOCOL_BY_DRIVER) {
168 //
169 // Mark the device handle as being managed by the driver specified by DriverBindingHandle
170 //
171 (*HandleType)[HandleIndex] |= (EFI_HANDLE_TYPE_DEVICE_HANDLE | EFI_HANDLE_TYPE_CONTROLLER_HANDLE);
172 //
173 // Mark the DriverBindingHandle as being a driver that is managing at least one controller
174 //
175 if (DriverBindingHandleIndexValid) {
176 (*HandleType)[*DriverBindingHandleIndex] |= EFI_HANDLE_TYPE_DEVICE_DRIVER;
177 }
178 }
179
180 if ((
181 OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
182 ) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
183 ) {
184 //
185 // Mark the DriverBindingHandle as being a driver that is managing at least one child controller
186 //
187 if (DriverBindingHandleIndexValid) {
188 (*HandleType)[*DriverBindingHandleIndex] |= EFI_HANDLE_TYPE_BUS_DRIVER;
189 }
190 }
191
192 if (ControllerHandle != NULL && (*HandleBuffer)[HandleIndex] == ControllerHandle) {
193 if ((
194 OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
195 ) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
196 ) {
197 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
198 if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].ControllerHandle) {
199 (*HandleType)[ChildIndex] |= (EFI_HANDLE_TYPE_DEVICE_HANDLE | EFI_HANDLE_TYPE_CHILD_HANDLE);
200 }
201 }
202 }
203 }
204 }
205
206 if (DriverBindingHandle == NULL && OpenInfo[OpenInfoIndex].ControllerHandle == ControllerHandle) {
207 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) == EFI_OPEN_PROTOCOL_BY_DRIVER) {
208 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
209 if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].AgentHandle) {
210 (*HandleType)[ChildIndex] |= EFI_HANDLE_TYPE_DEVICE_DRIVER;
211 }
212 }
213 }
214
215 if ((
216 OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
217 ) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
218 ) {
219 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_PARENT_HANDLE;
220 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
221 if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].AgentHandle) {
222 (*HandleType)[ChildIndex] |= EFI_HANDLE_TYPE_BUS_DRIVER;
223 }
224 }
225 }
226 }
227 }
228
229 FreePool (OpenInfo);
230 }
231 }
232
233 FreePool (ProtocolGuidArray);
234 }
235 }
236
237 return EFI_SUCCESS;
238
239 Error:
240 if (*HandleType != NULL) {
241 FreePool (*HandleType);
242 }
243
244 if (*HandleBuffer != NULL) {
245 FreePool (*HandleBuffer);
246 }
247
248 *HandleCount = 0;
249 *HandleBuffer = NULL;
250 *HandleType = NULL;
251
252 return Status;
253 } /* EFI_STATUS LibScanHandleDatabase() */