141fe9d1Toomas Soome/*
241fe9d1Toomas Soome * Copyright (c) 2017 Eric McCorkle
341fe9d1Toomas Soome * All rights reserved.
441fe9d1Toomas Soome *
541fe9d1Toomas Soome * Redistribution and use in source and binary forms, with or without
641fe9d1Toomas Soome * modification, are permitted provided that the following conditions
741fe9d1Toomas Soome * are met:
841fe9d1Toomas Soome * 1. Redistributions of source code must retain the above copyright
941fe9d1Toomas Soome *    notice, this list of conditions and the following disclaimer.
1041fe9d1Toomas Soome * 2. Redistributions in binary form must reproduce the above copyright
1141fe9d1Toomas Soome *    notice, this list of conditions and the following disclaimer in the
1241fe9d1Toomas Soome *    documentation and/or other materials provided with the distribution.
1341fe9d1Toomas Soome *
1441fe9d1Toomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1541fe9d1Toomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1641fe9d1Toomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1741fe9d1Toomas Soome * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1841fe9d1Toomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1941fe9d1Toomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2041fe9d1Toomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2141fe9d1Toomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2241fe9d1Toomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2341fe9d1Toomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2441fe9d1Toomas Soome * SUCH DAMAGE.
2541fe9d1Toomas Soome */
2641fe9d1Toomas Soome
2741fe9d1Toomas Soome#include <stdbool.h>
2841fe9d1Toomas Soome
2941fe9d1Toomas Soome#include <efi.h>
3041fe9d1Toomas Soome#include <efilib.h>
3141fe9d1Toomas Soome
3241fe9d1Toomas Soome#include "efi_driver_utils.h"
3341fe9d1Toomas Soome
3441fe9d1Toomas Soomestatic EFI_GUID DriverBindingProtocolGUID = DRIVER_BINDING_PROTOCOL;
3541fe9d1Toomas Soome
3641fe9d1Toomas SoomeEFI_STATUS
3741fe9d1Toomas Soomeconnect_controllers(EFI_GUID *filter)
3841fe9d1Toomas Soome{
3941fe9d1Toomas Soome        EFI_STATUS status;
4041fe9d1Toomas Soome        EFI_HANDLE *handles;
4141fe9d1Toomas Soome        UINTN nhandles, i, hsize;
4241fe9d1Toomas Soome
4341fe9d1Toomas Soome        nhandles = 0;
4441fe9d1Toomas Soome        hsize = 0;
4541fe9d1Toomas Soome        status = BS->LocateHandle(ByProtocol, filter, NULL,
4641fe9d1Toomas Soome                     &hsize, NULL);
4741fe9d1Toomas Soome
4841fe9d1Toomas Soome        if(status != EFI_BUFFER_TOO_SMALL) {
4941fe9d1Toomas Soome                return (status);
5041fe9d1Toomas Soome        }
5141fe9d1Toomas Soome
5241fe9d1Toomas Soome        handles = malloc(hsize);
5341fe9d1Toomas Soome        nhandles = hsize / sizeof(EFI_HANDLE);
5441fe9d1Toomas Soome
5541fe9d1Toomas Soome        status = BS->LocateHandle(ByProtocol, filter, NULL,
5641fe9d1Toomas Soome                     &hsize, handles);
5741fe9d1Toomas Soome
5841fe9d1Toomas Soome        if(EFI_ERROR(status)) {
5941fe9d1Toomas Soome                return (status);
6041fe9d1Toomas Soome        }
6141fe9d1Toomas Soome
6241fe9d1Toomas Soome        for(i = 0; i < nhandles; i++) {
6341fe9d1Toomas Soome                BS->ConnectController(handles[i], NULL, NULL, true);
6441fe9d1Toomas Soome        }
6541fe9d1Toomas Soome
6641fe9d1Toomas Soome        free(handles);
6741fe9d1Toomas Soome
6841fe9d1Toomas Soome        return (status);
6941fe9d1Toomas Soome}
7041fe9d1Toomas Soome
7141fe9d1Toomas SoomeEFI_STATUS
7241fe9d1Toomas Soomeinstall_driver(EFI_DRIVER_BINDING *driver)
7341fe9d1Toomas Soome{
7441fe9d1Toomas Soome        EFI_STATUS status;
7541fe9d1Toomas Soome
7641fe9d1Toomas Soome        driver->ImageHandle = IH;
7741fe9d1Toomas Soome        driver->DriverBindingHandle = NULL;
7841fe9d1Toomas Soome        status = BS->InstallMultipleProtocolInterfaces(
7941fe9d1Toomas Soome            &(driver->DriverBindingHandle),
8041fe9d1Toomas Soome            &DriverBindingProtocolGUID, driver,
8141fe9d1Toomas Soome            NULL);
8241fe9d1Toomas Soome
8341fe9d1Toomas Soome        if (EFI_ERROR(status)) {
8441fe9d1Toomas Soome                printf("Failed to install driver (%ld)!\n",
8541fe9d1Toomas Soome                    EFI_ERROR_CODE(status));
8641fe9d1Toomas Soome        }
8741fe9d1Toomas Soome
8841fe9d1Toomas Soome        return (status);
8941fe9d1Toomas Soome}
90