winntblockio.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 1,122 行 · 第 1/3 页

C
1,122
字号
/*++

Copyright (c) 2004 - 2005, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:

  WinNtBlockIo.c

Abstract:

  Produce block IO abstractions for real devices on your PC using Win32 APIs.
  The configuration of what devices to mount or emulate comes from NT 
  environment variables. The variables must be visible to the Microsoft* 
  Developer Studio for them to work.

  <F>ixed       - Fixed disk like a hard drive.
  <R>emovable   - Removable media like a floppy or CD-ROM.
  Read <O>nly   - Write protected device.
  Read <W>rite  - Read write device.
  <block count> - Decimal number of blocks a device supports.
  <block size>  - Decimal number of bytes per block.

  NT envirnonment variable contents. '<' and '>' are not part of the variable, 
  they are just used to make this help more readable. There should be no 
  spaces between the ';'. Extra spaces will break the variable. A '!' is 
  used to seperate multiple devices in a variable.

  EFI_WIN_NT_VIRTUAL_DISKS = 
    <F | R><O | W>;<block count>;<block size>[!...]

  EFI_WIN_NT_PHYSICAL_DISKS =
    <drive letter>:<F | R><O | W>;<block count>;<block size>[!...]

  Virtual Disks: These devices use a file to emulate a hard disk or removable
                 media device. 
                 
    Thus a 20 MB emulated hard drive would look like:
    EFI_WIN_NT_VIRTUAL_DISKS=FW;40960;512

    A 1.44MB emulated floppy with a block size of 1024 would look like:
    EFI_WIN_NT_VIRTUAL_DISKS=RW;1440;1024

  Physical Disks: These devices use NT to open a real device in your system

    Thus a 120 MB floppy would look like:
    EFI_WIN_NT_PHYSICAL_DISKS=B:RW;245760;512

    Thus a standard CD-ROM floppy would look like:
    EFI_WIN_NT_PHYSICAL_DISKS=Z:RO;307200;2048


  * Other names and brands may be claimed as the property of others.

--*/

#include "WinNtBlockIo.h"

EFI_DRIVER_BINDING_PROTOCOL gWinNtBlockIoDriverBinding = {
  WinNtBlockIoDriverBindingSupported,
  WinNtBlockIoDriverBindingStart,
  WinNtBlockIoDriverBindingStop,
  0x10,
  NULL,
  NULL
};

EFI_DRIVER_ENTRY_POINT (InitializeWinNtBlockIo)

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

Routine Description:

  Intialize Win32 windows to act as EFI SimpleTextOut and SimpleTextIn windows. . 

Arguments:

  (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)

Returns: 

  EFI_STATUS

--*/
// TODO:    ImageHandle - add argument and description to function comment
// TODO:    SystemTable - add argument and description to function comment
{
  return EfiLibInstallAllDriverProtocols (
          ImageHandle,
          SystemTable,
          &gWinNtBlockIoDriverBinding,
          ImageHandle,
          &gWinNtBlockIoComponentName,
          &gWinNtBlockIoDriverConfiguration,
          &gWinNtBlockIoDriverDiagnostics
          );
}

EFI_STATUS
EFIAPI
WinNtBlockIoDriverBindingSupported (
  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,
  IN  EFI_HANDLE                   Handle,
  IN  EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath
  )
/*++

Routine Description:

Arguments:

Returns:

  None

--*/
// TODO:    This - add argument and description to function comment
// TODO:    Handle - add argument and description to function comment
// TODO:    RemainingDevicePath - add argument and description to function comment
{
  EFI_STATUS              Status;
  EFI_WIN_NT_IO_PROTOCOL  *WinNtIo;

  //
  // Open the IO Abstraction(s) needed to perform the supported test
  //
  Status = gBS->OpenProtocol (
                  Handle,
                  &gEfiWinNtIoProtocolGuid,
                  &WinNtIo,
                  This->DriverBindingHandle,
                  Handle,
                  EFI_OPEN_PROTOCOL_BY_DRIVER
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  //
  // Make sure the WinNtThunkProtocol is valid
  //
  Status = EFI_UNSUPPORTED;
  if (WinNtIo->WinNtThunk->Signature == EFI_WIN_NT_THUNK_PROTOCOL_SIGNATURE) {

    //
    // Check the GUID to see if this is a handle type the driver supports
    //
    if (EfiCompareGuid (WinNtIo->TypeGuid, &gEfiWinNtVirtualDisksGuid) ||
        EfiCompareGuid (WinNtIo->TypeGuid, &gEfiWinNtPhysicalDisksGuid) ) {
      Status = EFI_SUCCESS;
    }
  }

  //
  // Close the I/O Abstraction(s) used to perform the supported test
  //
  gBS->CloseProtocol (
        Handle,
        &gEfiWinNtIoProtocolGuid,
        This->DriverBindingHandle,
        Handle
        );

  return Status;
}

EFI_STATUS
EFIAPI
WinNtBlockIoDriverBindingStart (
  IN  EFI_DRIVER_BINDING_PROTOCOL   *This,
  IN  EFI_HANDLE                    Handle,
  IN  EFI_DEVICE_PATH_PROTOCOL      *RemainingDevicePath
  )
/*++

Routine Description:

Arguments:

Returns:

  None

--*/
// TODO:    This - add argument and description to function comment
// TODO:    Handle - add argument and description to function comment
// TODO:    RemainingDevicePath - add argument and description to function comment
{
  EFI_STATUS                  Status;
  EFI_WIN_NT_IO_PROTOCOL      *WinNtIo;
  WIN_NT_RAW_DISK_DEVICE_TYPE DiskType;
  UINT16                      Buffer[FILENAME_BUFFER_SIZE];
  CHAR16                      *Str;
  BOOLEAN                     RemovableMedia;
  BOOLEAN                     WriteProtected;
  UINTN                       NumberOfBlocks;
  UINTN                       BlockSize;

  //
  // Grab the protocols we need
  //
  Status = gBS->OpenProtocol (
                  Handle,
                  &gEfiWinNtIoProtocolGuid,
                  &WinNtIo,
                  This->DriverBindingHandle,
                  Handle,
                  EFI_OPEN_PROTOCOL_BY_DRIVER
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  //
  // Set DiskType
  //
  if (EfiCompareGuid (WinNtIo->TypeGuid, &gEfiWinNtVirtualDisksGuid)) {
    DiskType = EfiWinNtVirtualDisks;
  } else if (EfiCompareGuid (WinNtIo->TypeGuid, &gEfiWinNtPhysicalDisksGuid)) {
    DiskType = EfiWinNtPhysicalDisks;
  } else {
    Status = EFI_UNSUPPORTED;
    goto Done;
  }

  Status  = EFI_NOT_FOUND;
  Str     = WinNtIo->EnvString;
  if (DiskType == EfiWinNtVirtualDisks) {
    WinNtIo->WinNtThunk->SPrintf (
                          Buffer,
                          L"Diskfile%d",
                          WinNtIo->InstanceNumber
                          );
  } else {
    if (*Str >= 'A' && *Str <= 'Z' || *Str >= 'a' && *Str <= 'z') {
      WinNtIo->WinNtThunk->SPrintf (Buffer, L"\\\\.\\%c:", *Str);
    } else {
      WinNtIo->WinNtThunk->SPrintf (Buffer, L"\\\\.\\PHYSICALDRIVE%c", *Str);
    }

    Str++;
    if (*Str != ':') {
      Status = EFI_NOT_FOUND;
      goto Done;
    }

    Str++;
  }

  if (*Str == 'R' || *Str == 'F') {
    RemovableMedia = (BOOLEAN) (*Str == 'R');
    Str++;
    if (*Str == 'O' || *Str == 'W') {
      WriteProtected  = (BOOLEAN) (*Str == 'O');
      Str             = GetNextElementPastTerminator (Str, ';');

      NumberOfBlocks  = Atoi (Str);
      if (NumberOfBlocks != 0) {
        Str       = GetNextElementPastTerminator (Str, ';');
        BlockSize = Atoi (Str);
        if (BlockSize != 0) {
          //
          // If we get here the variable is valid so do the work.
          //
          Status = WinNtBlockIoCreateMapping (
                    WinNtIo,
                    Handle,
                    Buffer,
                    WriteProtected,
                    RemovableMedia,
                    NumberOfBlocks,
                    BlockSize,
                    DiskType
                    );

        }
      }
    }
  }

Done:
  if (EFI_ERROR (Status)) {
    gBS->CloseProtocol (
          Handle,
          &gEfiWinNtIoProtocolGuid,
          This->DriverBindingHandle,
          Handle
          );
  }

  return Status;
}

EFI_STATUS
EFIAPI
WinNtBlockIoDriverBindingStop (
  IN  EFI_DRIVER_BINDING_PROTOCOL  *This,
  IN  EFI_HANDLE                   Handle,
  IN  UINTN                        NumberOfChildren,
  IN  EFI_HANDLE                   *ChildHandleBuffer
  )
/*++

Routine Description:

  TODO: Add function description

Arguments:

  This              - TODO: add argument description
  Handle            - TODO: add argument description
  NumberOfChildren  - TODO: add argument description
  ChildHandleBuffer - TODO: add argument description

Returns:

  EFI_UNSUPPORTED - TODO: Add description for return value

--*/
{
  EFI_BLOCK_IO_PROTOCOL   *BlockIo;
  EFI_STATUS              Status;
  WIN_NT_BLOCK_IO_PRIVATE *Private;

  //
  // Get our context back
  //
  Status = gBS->OpenProtocol (
                  Handle,
                  &gEfiBlockIoProtocolGuid,
                  &BlockIo,
                  This->DriverBindingHandle,
                  Handle,
                  EFI_OPEN_PROTOCOL_GET_PROTOCOL
                  );
  if (EFI_ERROR (Status)) {
    return EFI_UNSUPPORTED;
  }

  Private = WIN_NT_BLOCK_IO_PRIVATE_DATA_FROM_THIS (BlockIo);

  //
  // BugBug: If we need to kick people off, we need to make Uninstall Close the handles.
  //         We could pass in our image handle or FLAG our open to be closed via
  //         Unistall (== to saying any CloseProtocol will close our open)
  //
  Status = gBS->UninstallMultipleProtocolInterfaces (
                  Private->EfiHandle,
                  &gEfiBlockIoProtocolGuid,
                  &Private->BlockIo,
                  NULL
                  );
  if (!EFI_ERROR (Status)) {

    Status = gBS->CloseProtocol (
                    Handle,
                    &gEfiWinNtIoProtocolGuid,
                    This->DriverBindingHandle,
                    Handle
                    );

    //

⌨️ 快捷键说明

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