]> code.delx.au - refind/blob - refind/driver_support.c
Fixed bug that caused volume icons to be read only from default icons
[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 "lib.h"
23 #include "../include/refit_call_wrapper.h"
24
25 #ifdef __MAKEWITH_GNUEFI
26 // Following "global" constants are from EDK2's AutoGen.c....
27 EFI_GUID gEfiLoadedImageProtocolGuid = { 0x5B1B31A1, 0x9562, 0x11D2, { 0x8E, 0x3F, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B }};
28 EFI_GUID gEfiDriverBindingProtocolGuid = { 0x18A031AB, 0xB443, 0x4D1A, { 0xA5, 0xC0, 0x0C, 0x09, 0x26, 0x1E, 0x9F, 0x71 }};
29 EFI_GUID gEfiDriverConfiguration2ProtocolGuid = { 0xBFD7DC1D, 0x24F1, 0x40D9, { 0x82, 0xE7, 0x2E, 0x09, 0xBB, 0x6B, 0x4E, 0xBE }};
30 EFI_GUID gEfiDriverConfigurationProtocolGuid = { 0x107A772B, 0xD5E1, 0x11D4, { 0x9A, 0x46, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }};
31 EFI_GUID gEfiDriverDiagnosticsProtocolGuid = { 0x0784924F, 0xE296, 0x11D4, { 0x9A, 0x49, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }};
32 EFI_GUID gEfiDriverDiagnostics2ProtocolGuid = { 0x4D330321, 0x025F, 0x4AAC, { 0x90, 0xD8, 0x5E, 0xD9, 0x00, 0x17, 0x3B, 0x63 }};
33 EFI_GUID gEfiComponentNameProtocolGuid = { 0x107A772C, 0xD5E1, 0x11D4, { 0x9A, 0x46, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }};
34 EFI_GUID gEfiComponentName2ProtocolGuid = { 0x6A7A5CFF, 0xE8D9, 0x4F70, { 0xBA, 0xDA, 0x75, 0xAB, 0x30, 0x25, 0xCE, 0x14 }};
35 EFI_GUID gEfiDevicePathProtocolGuid = { 0x09576E91, 0x6D3F, 0x11D2, { 0x8E, 0x39, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B }};
36 #endif
37
38 // Below is from http://git.etherboot.org/?p=mirror/efi/shell/.git;a=commitdiff;h=b1b0c63423cac54dc964c2930e04aebb46a946ec;
39 // Seems to have been replaced by ParseHandleDatabaseByRelationshipWithType(), but the latter isn't working for me....
40 EFI_STATUS
41 LibScanHandleDatabase (
42 EFI_HANDLE DriverBindingHandle, OPTIONAL
43 UINT32 *DriverBindingHandleIndex, OPTIONAL
44 EFI_HANDLE ControllerHandle, OPTIONAL
45 UINT32 *ControllerHandleIndex, OPTIONAL
46 UINTN *HandleCount,
47 EFI_HANDLE **HandleBuffer,
48 UINT32 **HandleType
49 )
50
51 {
52 EFI_STATUS Status;
53 UINTN HandleIndex;
54 EFI_GUID **ProtocolGuidArray;
55 UINTN ArrayCount;
56 UINTN ProtocolIndex;
57 EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;
58 UINTN OpenInfoCount;
59 UINTN OpenInfoIndex;
60 UINTN ChildIndex;
61 BOOLEAN DriverBindingHandleIndexValid;
62 // BOOLEAN ControllerHandleIndexValid;
63
64 DriverBindingHandleIndexValid = FALSE;
65 if (DriverBindingHandleIndex != NULL) {
66 *DriverBindingHandleIndex = 0xffffffff;
67 }
68
69 // ControllerHandleIndexValid = FALSE;
70 if (ControllerHandleIndex != NULL) {
71 *ControllerHandleIndex = 0xffffffff;
72 }
73
74 *HandleCount = 0;
75 *HandleBuffer = NULL;
76 *HandleType = NULL;
77
78 //
79 // Retrieve the list of all handles from the handle database
80 //
81
82 Status = refit_call5_wrapper(BS->LocateHandleBuffer,
83 AllHandles,
84 NULL,
85 NULL,
86 HandleCount,
87 HandleBuffer
88 );
89 if (EFI_ERROR (Status)) {
90 goto Error;
91 }
92
93 *HandleType = AllocatePool (*HandleCount * sizeof (UINT32));
94 if (*HandleType == NULL) {
95 goto Error;
96 }
97
98 for (HandleIndex = 0; HandleIndex < *HandleCount; HandleIndex++) {
99 //
100 // Assume that the handle type is unknown
101 //
102 (*HandleType)[HandleIndex] = EFI_HANDLE_TYPE_UNKNOWN;
103
104 if (DriverBindingHandle != NULL &&
105 DriverBindingHandleIndex != NULL &&
106 (*HandleBuffer)[HandleIndex] == DriverBindingHandle
107 ) {
108 *DriverBindingHandleIndex = (UINT32) HandleIndex;
109 DriverBindingHandleIndexValid = TRUE;
110 }
111
112 if (ControllerHandle != NULL && ControllerHandleIndex != NULL && (*HandleBuffer)[HandleIndex] == ControllerHandle) {
113 *ControllerHandleIndex = (UINT32) HandleIndex;
114 // ControllerHandleIndexValid = TRUE;
115 }
116
117 }
118
119 for (HandleIndex = 0; HandleIndex < *HandleCount; HandleIndex++) {
120 //
121 // Retrieve the list of all the protocols on each handle
122 //
123
124 Status = refit_call3_wrapper(BS->ProtocolsPerHandle,
125 (*HandleBuffer)[HandleIndex],
126 &ProtocolGuidArray,
127 &ArrayCount
128 );
129 if (!EFI_ERROR (Status)) {
130
131 for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {
132
133 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiLoadedImageProtocolGuid) == 0) {
134 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_IMAGE_HANDLE;
135 }
136
137 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverBindingProtocolGuid) == 0) {
138 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DRIVER_BINDING_HANDLE;
139 }
140
141 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverConfigurationProtocolGuid) == 0) {
142 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DRIVER_CONFIGURATION_HANDLE;
143 }
144
145 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDriverDiagnosticsProtocolGuid) == 0) {
146 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DRIVER_DIAGNOSTICS_HANDLE;
147 }
148
149 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiComponentNameProtocolGuid) == 0) {
150 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_COMPONENT_NAME_HANDLE;
151 }
152
153 if (CompareGuid (ProtocolGuidArray[ProtocolIndex], &gEfiDevicePathProtocolGuid) == 0) {
154 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_DEVICE_HANDLE;
155 }
156 //
157 // Retrieve the list of agents that have opened each protocol
158 //
159
160 Status = refit_call4_wrapper(BS->OpenProtocolInformation,
161 (*HandleBuffer)[HandleIndex],
162 ProtocolGuidArray[ProtocolIndex],
163 &OpenInfo,
164 &OpenInfoCount
165 );
166 if (!EFI_ERROR (Status)) {
167
168 for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
169 if (DriverBindingHandle != NULL && OpenInfo[OpenInfoIndex].AgentHandle == DriverBindingHandle) {
170 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) == EFI_OPEN_PROTOCOL_BY_DRIVER) {
171 //
172 // Mark the device handle as being managed by the driver specified by DriverBindingHandle
173 //
174 (*HandleType)[HandleIndex] |= (EFI_HANDLE_TYPE_DEVICE_HANDLE | EFI_HANDLE_TYPE_CONTROLLER_HANDLE);
175 //
176 // Mark the DriverBindingHandle as being a driver that is managing at least one controller
177 //
178 if (DriverBindingHandleIndexValid) {
179 (*HandleType)[*DriverBindingHandleIndex] |= EFI_HANDLE_TYPE_DEVICE_DRIVER;
180 }
181 }
182
183 if ((
184 OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
185 ) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
186 ) {
187 //
188 // Mark the DriverBindingHandle as being a driver that is managing at least one child controller
189 //
190 if (DriverBindingHandleIndexValid) {
191 (*HandleType)[*DriverBindingHandleIndex] |= EFI_HANDLE_TYPE_BUS_DRIVER;
192 }
193 }
194
195 if (ControllerHandle != NULL && (*HandleBuffer)[HandleIndex] == ControllerHandle) {
196 if ((
197 OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
198 ) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
199 ) {
200 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
201 if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].ControllerHandle) {
202 (*HandleType)[ChildIndex] |= (EFI_HANDLE_TYPE_DEVICE_HANDLE | EFI_HANDLE_TYPE_CHILD_HANDLE);
203 }
204 }
205 }
206 }
207 }
208
209 if (DriverBindingHandle == NULL && OpenInfo[OpenInfoIndex].ControllerHandle == ControllerHandle) {
210 if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) == EFI_OPEN_PROTOCOL_BY_DRIVER) {
211 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
212 if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].AgentHandle) {
213 (*HandleType)[ChildIndex] |= EFI_HANDLE_TYPE_DEVICE_DRIVER;
214 }
215 }
216 }
217
218 if ((
219 OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
220 ) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
221 ) {
222 (*HandleType)[HandleIndex] |= EFI_HANDLE_TYPE_PARENT_HANDLE;
223 for (ChildIndex = 0; ChildIndex < *HandleCount; ChildIndex++) {
224 if ((*HandleBuffer)[ChildIndex] == OpenInfo[OpenInfoIndex].AgentHandle) {
225 (*HandleType)[ChildIndex] |= EFI_HANDLE_TYPE_BUS_DRIVER;
226 }
227 }
228 }
229 }
230 }
231
232 MyFreePool (OpenInfo);
233 }
234 }
235
236 MyFreePool (ProtocolGuidArray);
237 }
238 }
239
240 return EFI_SUCCESS;
241
242 Error:
243 MyFreePool (*HandleType);
244 MyFreePool (*HandleBuffer);
245
246 *HandleCount = 0;
247 *HandleBuffer = NULL;
248 *HandleType = NULL;
249
250 return Status;
251 } /* EFI_STATUS LibScanHandleDatabase() */