1*41fe9d1aSToomas Soome /*
2*41fe9d1aSToomas Soome  * Copyright (c) 2017 Eric McCorkle
3*41fe9d1aSToomas Soome  * All rights reserved.
4*41fe9d1aSToomas Soome  *
5*41fe9d1aSToomas Soome  * Redistribution and use in source and binary forms, with or without
6*41fe9d1aSToomas Soome  * modification, are permitted provided that the following conditions
7*41fe9d1aSToomas Soome  * are met:
8*41fe9d1aSToomas Soome  * 1. Redistributions of source code must retain the above copyright
9*41fe9d1aSToomas Soome  *    notice, this list of conditions and the following disclaimer.
10*41fe9d1aSToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11*41fe9d1aSToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12*41fe9d1aSToomas Soome  *    documentation and/or other materials provided with the distribution.
13*41fe9d1aSToomas Soome  *
14*41fe9d1aSToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*41fe9d1aSToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*41fe9d1aSToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*41fe9d1aSToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*41fe9d1aSToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*41fe9d1aSToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*41fe9d1aSToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*41fe9d1aSToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*41fe9d1aSToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*41fe9d1aSToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*41fe9d1aSToomas Soome  * SUCH DAMAGE.
25*41fe9d1aSToomas Soome  */
26*41fe9d1aSToomas Soome 
27*41fe9d1aSToomas Soome #include <stdbool.h>
28*41fe9d1aSToomas Soome 
29*41fe9d1aSToomas Soome #include <efi.h>
30*41fe9d1aSToomas Soome #include <efilib.h>
31*41fe9d1aSToomas Soome 
32*41fe9d1aSToomas Soome #include "efi_driver_utils.h"
33*41fe9d1aSToomas Soome 
34*41fe9d1aSToomas Soome static EFI_GUID DriverBindingProtocolGUID = DRIVER_BINDING_PROTOCOL;
35*41fe9d1aSToomas Soome 
36*41fe9d1aSToomas Soome EFI_STATUS
connect_controllers(EFI_GUID * filter)37*41fe9d1aSToomas Soome connect_controllers(EFI_GUID *filter)
38*41fe9d1aSToomas Soome {
39*41fe9d1aSToomas Soome         EFI_STATUS status;
40*41fe9d1aSToomas Soome         EFI_HANDLE *handles;
41*41fe9d1aSToomas Soome         UINTN nhandles, i, hsize;
42*41fe9d1aSToomas Soome 
43*41fe9d1aSToomas Soome         nhandles = 0;
44*41fe9d1aSToomas Soome         hsize = 0;
45*41fe9d1aSToomas Soome         status = BS->LocateHandle(ByProtocol, filter, NULL,
46*41fe9d1aSToomas Soome                      &hsize, NULL);
47*41fe9d1aSToomas Soome 
48*41fe9d1aSToomas Soome         if(status != EFI_BUFFER_TOO_SMALL) {
49*41fe9d1aSToomas Soome                 return (status);
50*41fe9d1aSToomas Soome         }
51*41fe9d1aSToomas Soome 
52*41fe9d1aSToomas Soome         handles = malloc(hsize);
53*41fe9d1aSToomas Soome         nhandles = hsize / sizeof(EFI_HANDLE);
54*41fe9d1aSToomas Soome 
55*41fe9d1aSToomas Soome         status = BS->LocateHandle(ByProtocol, filter, NULL,
56*41fe9d1aSToomas Soome                      &hsize, handles);
57*41fe9d1aSToomas Soome 
58*41fe9d1aSToomas Soome         if(EFI_ERROR(status)) {
59*41fe9d1aSToomas Soome                 return (status);
60*41fe9d1aSToomas Soome         }
61*41fe9d1aSToomas Soome 
62*41fe9d1aSToomas Soome         for(i = 0; i < nhandles; i++) {
63*41fe9d1aSToomas Soome                 BS->ConnectController(handles[i], NULL, NULL, true);
64*41fe9d1aSToomas Soome         }
65*41fe9d1aSToomas Soome 
66*41fe9d1aSToomas Soome         free(handles);
67*41fe9d1aSToomas Soome 
68*41fe9d1aSToomas Soome         return (status);
69*41fe9d1aSToomas Soome }
70*41fe9d1aSToomas Soome 
71*41fe9d1aSToomas Soome EFI_STATUS
install_driver(EFI_DRIVER_BINDING * driver)72*41fe9d1aSToomas Soome install_driver(EFI_DRIVER_BINDING *driver)
73*41fe9d1aSToomas Soome {
74*41fe9d1aSToomas Soome         EFI_STATUS status;
75*41fe9d1aSToomas Soome 
76*41fe9d1aSToomas Soome         driver->ImageHandle = IH;
77*41fe9d1aSToomas Soome         driver->DriverBindingHandle = NULL;
78*41fe9d1aSToomas Soome         status = BS->InstallMultipleProtocolInterfaces(
79*41fe9d1aSToomas Soome             &(driver->DriverBindingHandle),
80*41fe9d1aSToomas Soome             &DriverBindingProtocolGUID, driver,
81*41fe9d1aSToomas Soome             NULL);
82*41fe9d1aSToomas Soome 
83*41fe9d1aSToomas Soome         if (EFI_ERROR(status)) {
84*41fe9d1aSToomas Soome                 printf("Failed to install driver (%ld)!\n",
85*41fe9d1aSToomas Soome                     EFI_ERROR_CODE(status));
86*41fe9d1aSToomas Soome         }
87*41fe9d1aSToomas Soome 
88*41fe9d1aSToomas Soome         return (status);
89*41fe9d1aSToomas Soome }
90