⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bot.c

📁 Next BIOS Source code : Extensible Firmware Interface
💻 C
📖 第 1 页 / 共 2 页
字号:
/*++

Copyright (c)  1999 - 2002 Intel Corporation. All rights reserved
This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.


Module Name:

  BOT.c

Abstract:

--*/

#include "Efi.h"
#include "EfiDriverLib.h"
#include "usb.h"

//
// Driver Consumed Protocol Prototypes
//
#include EFI_PROTOCOL_DEFINITION(DriverBinding)
#include EFI_PROTOCOL_DEFINITION(UsbIo)


//
// Driver Consumed Protocol Prototypes
//
#include EFI_PROTOCOL_DEFINITION(UsbAtapi)

#include "usblib.h"
#include "bot.h"

//
// Function prototypes
//
EFI_STATUS
UsbBotDriverEntryPoint(
  IN EFI_HANDLE           ImageHandle,
  IN EFI_SYSTEM_TABLE     *SystemTable
  );

//
// Bot Driver Binding Protocol
//
static EFI_STATUS
BotDriverBindingSupported (
  IN EFI_DRIVER_BINDING_PROTOCOL    *This,
  IN EFI_HANDLE                     ControllerHandle,
  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath
  );

static EFI_STATUS
BotDriverBindingStart (
  IN EFI_DRIVER_BINDING_PROTOCOL    *This,
  IN EFI_HANDLE                     ControllerHandle,
  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath
  );

static EFI_STATUS
BotDriverBindingStop (
  IN  EFI_DRIVER_BINDING_PROTOCOL    *This,
  IN  EFI_HANDLE                     ControllerHandle,
  IN  UINTN                          NumberOfChildren,
  IN  EFI_HANDLE                     *ChildHandleBuffer
  );

EFI_DRIVER_BINDING_PROTOCOL gUsbBotDriverBinding = {
  BotDriverBindingSupported,
  BotDriverBindingStart,
  BotDriverBindingStop,
  0x10,
  NULL,
  NULL
};

//
// Bot Protocol
//
static EFI_STATUS
BotCommandPhase (
  IN  USB_BOT_DEVICE            *UsbBotDev,
  IN  VOID                      *Command,
  IN  UINT8                     CommandSize,
  IN  UINT32                    DataTransferLength,
  IN  EFI_USB_DATA_DIRECTION    Direction,
  IN  UINT16                    Timeout
);

static EFI_STATUS
BotDataPhase (
  IN  USB_BOT_DEVICE          *UsbBotDev,
  IN  UINT32                  *DataSize,
  IN  OUT VOID                *DataBuffer,
  IN  EFI_USB_DATA_DIRECTION  Direction,
  IN  UINT16                  Timeout
);

static EFI_STATUS
BotStatusPhase (
  IN  USB_BOT_DEVICE      *UsbBotDev,
  OUT UINT8               *TransferStatus,
  IN  UINT16              Timeout
);

//
// USB Atapi protocol prototype
//
static EFI_STATUS
BotAtapiCommand (
  IN  EFI_USB_ATAPI_PROTOCOL    *This,
  IN  VOID                      *Command,
  IN  UINT8                     CommandSize,
  IN  VOID                      *DataBuffer,
  IN  UINT32                    BufferLength,
  IN  EFI_USB_DATA_DIRECTION    Direction,
  IN  UINT16                    TimeOutInMilliSeconds
);

static EFI_STATUS
BotMassStorageReset (
  IN  EFI_USB_ATAPI_PROTOCOL    *This,
  IN  BOOLEAN                   ExtendedVerification
);

static EFI_USB_ATAPI_PROTOCOL BotAtapiProtocol = {
  BotAtapiCommand,
  BotMassStorageReset,
  0
};


EFI_DRIVER_ENTRY_POINT(UsbBotDriverEntryPoint)

EFI_STATUS
UsbBotDriverEntryPoint(
  IN EFI_HANDLE           ImageHandle,
  IN EFI_SYSTEM_TABLE     *SystemTable
  )
/*++

Routine Description:
  Register Driver Binding protocol for this driver.

Arguments:
  (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)

Returns:
  EFI_SUCCESS - Driver loaded
  other       - Driver not loaded

--*/
{
  return EfiLibInstallAllDriverProtocols (
           ImageHandle, 
           SystemTable, 
           &gUsbBotDriverBinding, 
           ImageHandle,
           &gUsbBotComponentName,
           NULL,
           NULL
         );
}

static EFI_STATUS
BotDriverBindingSupported (
  IN EFI_DRIVER_BINDING_PROTOCOL    *This,
  IN EFI_HANDLE                     ControllerHandle,
  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath
  )
/*++

  Routine Description:
    Test to see if this driver supports ControllerHandle. Any ControllerHandle
    than contains a BlockIo and DiskIo protocol can be supported.

  Arguments:
    This                - Protocol instance pointer.
    ControllerHandle    - Handle of device to test
    RemainingDevicePath - Not used

  Returns:
    EFI_SUCCES          - This driver supports this device
    EFI_ALREADY_STARTED - This driver is already running on this device
    other               - This driver does not support this device

--*/
{
  EFI_STATUS                    Status;
  EFI_USB_IO_PROTOCOL           *UsbIo;
  EFI_USB_INTERFACE_DESCRIPTOR  InterfaceDescriptor;

  //
  // Check if the Controller supports USB IO protocol
  //
  Status = gBS->OpenProtocol (
                 ControllerHandle,
                 &gEfiUsbIoProtocolGuid,
                 &UsbIo,
                 This->DriverBindingHandle,
                 ControllerHandle,
                 EFI_OPEN_PROTOCOL_BY_DRIVER
                );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  //
  // Get the Default interface descriptor, now we only
  // suppose interface 1
  //
  Status = UsbIo->UsbGetInterfaceDescriptor(
                    UsbIo,
                    &InterfaceDescriptor
                  );
  if(EFI_ERROR(Status)) {
    goto Exit;
  }
  //
  // Check if it is a BOT type Mass Storage Device
  //
  if((InterfaceDescriptor.InterfaceClass != 0x08)
    || (InterfaceDescriptor.InterfaceProtocol != BOT))
  {
    Status = EFI_UNSUPPORTED;
    goto Exit;
  }

Exit:
  gBS->CloseProtocol(
        ControllerHandle,
        &gEfiUsbIoProtocolGuid,
        This->DriverBindingHandle,
        ControllerHandle
       );
  return Status;
}

static EFI_STATUS
BotDriverBindingStart (
  IN EFI_DRIVER_BINDING_PROTOCOL    *This,
  IN EFI_HANDLE                     ControllerHandle,
  IN EFI_DEVICE_PATH_PROTOCOL       *RemainingDevicePath
  )
/*++

  Routine Description:
    Start this driver on ControllerHandle by opening a Block IO and Disk IO
    protocol, reading Device Path, and creating a child handle with a
    Disk IO and device path protocol.

  Arguments:
    This                - Protocol instance pointer.
    ControllerHandle    - Handle of device to bind driver to
    RemainingDevicePath - Not used

  Returns:
    EFI_SUCCES          - This driver is added to DeviceHandle
    EFI_ALREADY_STARTED - This driver is already running on DeviceHandle
    other               - This driver does not support this device

--*/
{
  USB_BOT_DEVICE                *UsbBotDev;
  UINT8                         i;
  EFI_USB_ENDPOINT_DESCRIPTOR   *EndpointDescriptor;
  EFI_USB_INTERFACE_DESCRIPTOR  *InterfaceDescriptor;
  EFI_STATUS                    Status;
  EFI_USB_IO_PROTOCOL           *UsbIo;

  //
  // Check if the Controller supports USB IO protocol
  //
  UsbBotDev = NULL;

  Status = gBS->OpenProtocol (
                  ControllerHandle,
                  &gEfiUsbIoProtocolGuid,
                  &UsbIo,
                  This->DriverBindingHandle,
                  ControllerHandle,
                  EFI_OPEN_PROTOCOL_BY_DRIVER
                );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = gBS->AllocatePool (
                  EfiBootServicesData,
                  sizeof(EFI_USB_INTERFACE_DESCRIPTOR),
                  &InterfaceDescriptor
                );

  if (EFI_ERROR(Status)) {
    gBS->CloseProtocol (
         ControllerHandle,
         &gEfiUsbIoProtocolGuid,
         This->DriverBindingHandle,
         ControllerHandle
       );
    return EFI_OUT_OF_RESOURCES;
  }

  //
  // Get the controller interface descriptor,
  //
  Status = UsbIo->UsbGetInterfaceDescriptor(
                    UsbIo,
                    InterfaceDescriptor
                  );
  if (EFI_ERROR(Status)) {
    gBS->FreePool (InterfaceDescriptor);
    goto ErrorExit;
  }

  BotAtapiProtocol.CommandProtocol = InterfaceDescriptor->InterfaceSubClass;
  
  Status = gBS->AllocatePool(
                  EfiBootServicesData,
                  sizeof(USB_BOT_DEVICE),
                  &UsbBotDev
                );

  if (EFI_ERROR(Status)) {
    gBS->FreePool (InterfaceDescriptor);
    goto ErrorExit;
  }

  EfiZeroMem(UsbBotDev, sizeof(USB_BOT_DEVICE));

  UsbBotDev->Signature = USB_BOT_DEVICE_SIGNATURE;
  UsbBotDev->UsbIo = UsbIo;
  UsbBotDev->InterfaceDescriptor = InterfaceDescriptor;
  UsbBotDev->UsbAtapiProtocol = BotAtapiProtocol;

  for (i = 0; i < InterfaceDescriptor->NumEndpoints; i++) {
    Status = gBS->AllocatePool (
                    EfiBootServicesData,
                    sizeof(EFI_USB_INTERFACE_DESCRIPTOR),
                    &EndpointDescriptor
                  );
    if (EFI_ERROR(Status)) {
      Status = EFI_OUT_OF_RESOURCES;
      goto ErrorExit;
    }

    UsbIo->UsbGetEndpointDescriptor(
            UsbIo,
            i,
            EndpointDescriptor
           );

    //
    // We parse bulk endpoint
    //
    if (EndpointDescriptor->Attributes == 0x02) {
      if ((EndpointDescriptor->EndpointAddress & 0x80) != 0) {
        UsbBotDev->BulkInEndpointDescriptor = EndpointDescriptor;
      } else {
        UsbBotDev->BulkOutEndpointDescriptor = EndpointDescriptor;
      }
      continue;
    }

    gBS->FreePool (EndpointDescriptor);
  }

  //
  // Double check we have these endpoint descriptors
  //
  if(!(UsbBotDev->BulkInEndpointDescriptor
    && UsbBotDev->BulkOutEndpointDescriptor))
  {
    Status = EFI_DEVICE_ERROR;
    goto ErrorExit;
  }

  //
  // Install Usb-Atapi Protocol onto the handle
  //
  Status = gBS->InstallProtocolInterface (
                  &ControllerHandle,
                  &gEfiUsbAtapiProtocolGuid,
                  EFI_NATIVE_INTERFACE,
                  &UsbBotDev->UsbAtapiProtocol
                 );

  if(EFI_ERROR(Status)) {
    goto ErrorExit;
  }

  UsbBotDev->ControllerNameTable = NULL;
  EfiLibAddUnicodeString (
    "eng", 
    gUsbBotComponentName.SupportedLanguages, 
    &UsbBotDev->ControllerNameTable, 
    L"Usb Bot Mass Storage"
  );
    
  return EFI_SUCCESS;

ErrorExit:
  gBS->CloseProtocol (
         ControllerHandle,
         &gEfiUsbIoProtocolGuid,
         This->DriverBindingHandle,
         ControllerHandle
       );

  if (UsbBotDev != NULL) {
    if (UsbBotDev->InterfaceDescriptor != NULL) {
      gBS->FreePool (UsbBotDev->InterfaceDescriptor);
    }

    if (UsbBotDev->BulkInEndpointDescriptor != NULL) {
      gBS->FreePool (UsbBotDev->BulkInEndpointDescriptor);
    }

    if (UsbBotDev->BulkOutEndpointDescriptor != NULL) {
      gBS->FreePool (UsbBotDev->BulkOutEndpointDescriptor);
    }

    gBS->FreePool(UsbBotDev);
  }

  return Status;
}

static EFI_STATUS
BotDriverBindingStop (
  IN  EFI_DRIVER_BINDING_PROTOCOL    *This,
  IN  EFI_HANDLE                     ControllerHandle,
  IN  UINTN                          NumberOfChildren,
  IN  EFI_HANDLE                     *ChildHandleBuffer
  )
/*++

  Routine Description:
    Stop this driver on ControllerHandle. Support stoping any child handles
    created by this driver.

  Arguments:
    This              - Protocol instance pointer.
    DeviceHandle      - Handle of device to stop driver on
    NumberOfChildren  - Number of Children in the ChildHandleBuffer
    ChildHandleBuffer - List of handles for the children we need to stop.

  Returns:
    EFI_SUCCES          - This driver is removed DeviceHandle
    other               - This driver was not removed from this device

--*/
{
  EFI_STATUS                    Status;
  EFI_USB_ATAPI_PROTOCOL        *BotAtapiProtocol;
  USB_BOT_DEVICE                *UsbBotDev;

  //
  // Get our context back.
  //
  Status = gBS->OpenProtocol (
                  ControllerHandle,
                  &gEfiUsbAtapiProtocolGuid,
                  &BotAtapiProtocol,
                  This->DriverBindingHandle,
                  ControllerHandle,
                  EFI_OPEN_PROTOCOL_GET_PROTOCOL
                );
  if (EFI_ERROR (Status)) {
    return EFI_UNSUPPORTED;
  }

  UsbBotDev = USB_BOT_DEVICE_FROM_THIS(BotAtapiProtocol);

  //
  // Uninstall protocol
  //
  Status = gBS->UninstallProtocolInterface (
                  ControllerHandle,
                  &gEfiUsbAtapiProtocolGuid,
                  &UsbBotDev->UsbAtapiProtocol
                );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = gBS->CloseProtocol (

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -