winntblockio.c
来自「Next BIOS Source code : Extensible Firmw」· C语言 代码 · 共 1,018 行 · 第 1/2 页
C
1,018 行
/*++
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:
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
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
--*/
{
return EfiLibInstallAllDriverProtocols (
ImageHandle,
SystemTable,
&gWinNtBlockIoDriverBinding,
ImageHandle,
&gWinNtBlockIoComponentName,
&gWinNtBlockIoDriverConfiguration,
&gWinNtBlockIoDriverDiagnostics
);
}
EFI_STATUS
WinNtBlockIoDriverBindingSupported (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Handle,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
)
/*++
Routine Description:
Arguments:
Returns:
None
--*/
{
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
WinNtBlockIoDriverBindingStart (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Handle,
IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
)
/*++
Routine Description:
Arguments:
Returns:
None
--*/
{
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
WinNtBlockIoDriverBindingStop (
IN EFI_DRIVER_BINDING_PROTOCOL *This,
IN EFI_HANDLE Handle,
IN UINTN NumberOfChildren,
IN EFI_HANDLE *ChildHandleBuffer
)
{
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
);
//
// Shut down our device
//
Private->WinNtThunk->CloseHandle (Private->NtHandle);
//
// Free our instance data
//
EfiLibFreeUnicodeStringTable (Private->ControllerNameTable);
gBS->FreePool (Private);
}
return Status;
}
STATIC
CHAR16 *
GetNextElementPastTerminator (
IN CHAR16 *EnvironmentVariable,
IN CHAR16 Terminator
)
/*++
Routine Description:
Worker function to parse environment variables.
Arguments:
EnvironmentVariable - Envirnment variable to parse.
Terminator - Terminator to parse for.
Returns:
Pointer to next eliment past the first occurence of Terminator or the '\0'
at the end of the string.
--*/
{
CHAR16 *Ptr;
for (Ptr = EnvironmentVariable; *Ptr != '\0'; Ptr++) {
if (*Ptr == Terminator) {
Ptr++;
break;
}
}
return Ptr;
}
STATIC
EFI_STATUS
WinNtBlockIoCreateMapping (
IN EFI_WIN_NT_IO_PROTOCOL *WinNtIo,
IN EFI_HANDLE EfiDeviceHandle,
IN CHAR16 *Filename,
IN BOOLEAN ReadOnly,
IN BOOLEAN RemovableMedia,
IN UINTN NumberOfBlocks,
IN UINTN BlockSize,
IN WIN_NT_RAW_DISK_DEVICE_TYPE DeviceType
)
{
EFI_STATUS Status;
EFI_BLOCK_IO_PROTOCOL *BlockIo;
WIN_NT_BLOCK_IO_PRIVATE *Private;
UINTN Index;
WinNtIo->WinNtThunk->SetErrorMode (SEM_FAILCRITICALERRORS);
Status = gBS->AllocatePool(
EfiBootServicesData,
sizeof(WIN_NT_BLOCK_IO_PRIVATE),
&Private
);
ASSERT_EFI_ERROR(Status);
EfiInitializeLock (&Private->Lock, EFI_TPL_NOTIFY);
Private->WinNtThunk = WinNtIo->WinNtThunk;
Private->Signature = WIN_NT_BLOCK_IO_PRIVATE_SIGNATURE;
Private->LastBlock = NumberOfBlocks - 1;
Private->BlockSize = BlockSize;
for (Index = 0; Filename[Index] != 0; Index++) {
Private->Filename[Index] = Filename[Index];
}
Private->Filename[Index] = 0;
Private->ReadMode = GENERIC_READ | (ReadOnly ? 0 : GENERIC_WRITE);
Private->ShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
Private->NumberOfBlocks = NumberOfBlocks;
Private->DeviceType = DeviceType;
Private->NtHandle = INVALID_HANDLE_VALUE;
Private->ControllerNameTable = NULL;
EfiLibAddUnicodeString (
"eng",
gWinNtBlockIoComponentName.SupportedLanguages,
&Private->ControllerNameTable,
Private->Filename
);
BlockIo = &Private->BlockIo;
BlockIo->Revision = EFI_BLOCK_IO_PROTOCOL_REVISION;
BlockIo->Media = &Private->Media;
BlockIo->Media->BlockSize = Private->BlockSize;
BlockIo->Media->LastBlock = Private->NumberOfBlocks - 1;
BlockIo->Media->MediaId = 0;;
BlockIo->Reset = WinNtBlockIoResetBlock;
BlockIo->ReadBlocks = WinNtBlockIoReadBlocks;
BlockIo->WriteBlocks = WinNtBlockIoWriteBlocks;
BlockIo->FlushBlocks = WinNtBlockIoFlushBlocks;
BlockIo->Media->ReadOnly = ReadOnly;
BlockIo->Media->RemovableMedia = RemovableMedia;
BlockIo->Media->LogicalPartition = FALSE;
BlockIo->Media->MediaPresent = TRUE;
if (DeviceType == EfiWinNtVirtualDisks) {
BlockIo->Media->IoAlign = 1;
//
// Create a file to use for a virtual disk even if it does not exist.
//
Private->OpenMode = OPEN_ALWAYS;
} else if (DeviceType == EfiWinNtPhysicalDisks) {
//
// Physical disk and floppy devices require 4 byte alignment.
//
BlockIo->Media->IoAlign = 4;
//
// You can only open a physical device if it exists.
//
Private->OpenMode = OPEN_EXISTING;
} else {
ASSERT(FALSE);
}
Private->EfiHandle = EfiDeviceHandle;
Status = WinNtBlockIoOpenDevice (Private);
if (!EFI_ERROR (Status)) {
Status = gBS->InstallMultipleProtocolInterfaces (
&Private->EfiHandle,
&gEfiBlockIoProtocolGuid, &Private->BlockIo,
NULL
);
ASSERT(!EFI_ERROR(Status));
DEBUG ((EFI_D_INIT, "BlockDevice added: %s\n", Filename));
}
return Status;
}
STATIC
EFI_STATUS
WinNtBlockIoOpenDevice (
WIN_NT_BLOCK_IO_PRIVATE *Private
)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?