17.4 GetDriver() Implementation

The example below shows an example implementation of the GetDriver() function of the Bus Specific Driver Override Protocol. The first step is to retrieve the private context structure from the This pointer using the CR() macro defined in Section 17.3 above. If no image handles are registered, EFI_NOT_FOUND. is returned. If DriverImageHandle is a pointer to NULL, the first image handle from HandleBuffer is returned. If DriverImageHandle is not a pointer to NULL, a search is made through HandleBuffer to find a matching handle. If a matching handle is not found, EFI_INVALID_PARAMETER is returned. If a matching handle is found, the next handle in the array is returned. If the matching handle is the last handle in the array, EFI_NOT_FOUND is returned.

Example 167-GetDriver() Function of a Bus Specific Driver Override Protocol
#include <Uefi.h>
#include <Protocol/BusSpecificDriverOverride.h>

EFI_STATUS
EFIAPI
AbcGetDriver (
  IN     EFI_BUS_SPECIFIC_DRIVER_OVERRIDE_PROTOCOL  *This,
  IN OUT EFI_HANDLE                                 *DriverImageHandle
  )
{
  UINTN                                             Index;
  ABC_PRIVATE_DATA                                  *Private;

  Private = ABC_PRIVATE_DATA_FROM_BUS_SPECIFIC_DRIVER_OVERRIDE_THIS (This);

  if (Private->NumberOfHandles == 0) {
    return EFI_NOT_FOUND;
  }

  if (DriverImageHandle == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  if (*DriverImageHandle == NULL) {
    *DriverImageHandle = Private->HandleBuffer[0];
    return EFI_SUCCESS;
  }

  for (Index = 0;
       Index < Private->NumberOfHandles;
       Index++) {
    if (*DriverImageHandle == Private->HandleBuffer[Index]) {
      Index++;
      if (Index < Private->NumberOfHandles) {
        *DriverImageHandle = Private->HandleBuffer[Index];
        return EFI_SUCCESS;
      } else {
        return EFI_NOT_FOUND;
      }
    }
  }

  return EFI_INVALID_PARAMETER;
}