winntserialio.c
来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 1,448 行 · 第 1/3 页
C
1,448 行
/*++
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:
WinNtSerialIo.c
Abstract:
Our DriverBinding member functions operate on the handles
created by the NT Bus driver.
Handle(1) - WinNtIo - DevicePath(1)
If a serial port is added to the system this driver creates a new handle.
The new handle is required, since the serial device must add an UART device
pathnode.
Handle(2) - SerialIo - DevicePath(1)\UART
The driver then adds a gEfiWinNtSerialPortGuid as a protocol to Handle(1).
The instance data for this protocol is the private data used to create
Handle(2).
Handle(1) - WinNtIo - DevicePath(1) - WinNtSerialPort
If the driver is unloaded Handle(2) is removed from the system and
gEfiWinNtSerialPortGuid is removed from Handle(1).
Note: Handle(1) is any handle created by the Win NT Bus driver that is passed
into the DriverBinding member functions of this driver. This driver requires
a Handle(1) to contain a WinNtIo protocol, a DevicePath protocol, and
the TypeGuid in the WinNtIo must be gEfiWinNtSerialPortGuid.
If Handle(1) contains a gEfiWinNtSerialPortGuid protocol then the driver is
loaded on the device.
--*/
#include "WinNtSerialIo.h"
EFI_DRIVER_BINDING_PROTOCOL gWinNtSerialIoDriverBinding = {
WinNtSerialIoDriverBindingSupported,
WinNtSerialIoDriverBindingStart,
WinNtSerialIoDriverBindingStop,
0x10,
NULL,
NULL
};
EFI_DRIVER_ENTRY_POINT (InitializeWinNtSerialIo)
EFI_STATUS
EFIAPI
InitializeWinNtSerialIo (
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,
&gWinNtSerialIoDriverBinding,
ImageHandle,
&gWinNtSerialIoComponentName,
NULL,
NULL
);
}
STATIC
EFI_STATUS
EFIAPI
WinNtSerialIoDriverBindingSupported (
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
// TODO: EFI_SUCCESS - add return value to function comment
// TODO: EFI_SUCCESS - add return value to function comment
{
EFI_STATUS Status;
EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
EFI_WIN_NT_IO_PROTOCOL *WinNtIo;
UART_DEVICE_PATH *UartNode;
//
// Open the IO Abstraction(s) needed to perform the supported test
//
Status = gBS->OpenProtocol (
Handle,
&gEfiDevicePathProtocolGuid,
&ParentDevicePath,
This->DriverBindingHandle,
Handle,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (Status == EFI_ALREADY_STARTED) {
return EFI_SUCCESS;
}
if (EFI_ERROR (Status)) {
return Status;
}
gBS->CloseProtocol (
Handle,
&gEfiDevicePathProtocolGuid,
This->DriverBindingHandle,
Handle
);
Status = gBS->OpenProtocol (
Handle,
&gEfiWinNtIoProtocolGuid,
&WinNtIo,
This->DriverBindingHandle,
Handle,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (Status == EFI_ALREADY_STARTED) {
return EFI_SUCCESS;
}
if (EFI_ERROR (Status)) {
return Status;
}
//
// Make sure that the WinNt Thunk Protocol is valid
//
if (WinNtIo->WinNtThunk->Signature != EFI_WIN_NT_THUNK_PROTOCOL_SIGNATURE) {
Status = EFI_UNSUPPORTED;
goto Error;
}
//
// Check the GUID to see if this is a handle type the driver supports
//
if (!EfiCompareGuid (WinNtIo->TypeGuid, &gEfiWinNtSerialPortGuid)) {
Status = EFI_UNSUPPORTED;
goto Error;
}
if (RemainingDevicePath != NULL) {
Status = EFI_UNSUPPORTED;
UartNode = (UART_DEVICE_PATH *) RemainingDevicePath;
if (UartNode->Header.Type != MESSAGING_DEVICE_PATH ||
UartNode->Header.SubType != MSG_UART_DP ||
DevicePathNodeLength((EFI_DEVICE_PATH_PROTOCOL *)UartNode) != sizeof(UART_DEVICE_PATH)) {
goto Error;
}
if (UartNode->BaudRate < 0 || UartNode->BaudRate > SERIAL_PORT_MAX_BAUD_RATE) {
goto Error;
}
if (UartNode->Parity < NoParity || UartNode->Parity > SpaceParity) {
goto Error;
}
if (UartNode->DataBits < 5 || UartNode->DataBits > 8) {
goto Error;
}
if (UartNode->StopBits < OneStopBit || UartNode->StopBits > TwoStopBits) {
goto Error;
}
if ((UartNode->DataBits == 5) && (UartNode->StopBits == TwoStopBits)) {
goto Error;
}
if ((UartNode->DataBits >= 6) && (UartNode->DataBits <= 8) && (UartNode->StopBits == OneFiveStopBits)) {
goto Error;
}
Status = EFI_SUCCESS;
}
Error:
//
// Close the I/O Abstraction(s) used to perform the supported test
//
gBS->CloseProtocol (
Handle,
&gEfiWinNtIoProtocolGuid,
This->DriverBindingHandle,
Handle
);
return Status;
}
STATIC
EFI_STATUS
EFIAPI
WinNtSerialIoDriverBindingStart (
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
// TODO: EFI_SUCCESS - add return value to function comment
// TODO: EFI_SUCCESS - add return value to function comment
{
EFI_STATUS Status;
EFI_WIN_NT_IO_PROTOCOL *WinNtIo;
WIN_NT_SERIAL_IO_PRIVATE_DATA *Private;
HANDLE NtHandle;
UART_DEVICE_PATH Node;
EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfoBuffer;
UINTN EntryCount;
UINTN Index;
EFI_SERIAL_IO_PROTOCOL *SerialIo;
Private = NULL;
NtHandle = INVALID_HANDLE_VALUE;
//
// Grab the protocols we need
//
Status = gBS->OpenProtocol (
Handle,
&gEfiDevicePathProtocolGuid,
&ParentDevicePath,
This->DriverBindingHandle,
Handle,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
return Status;
}
//
// Grab the IO abstraction we need to get any work done
//
Status = gBS->OpenProtocol (
Handle,
&gEfiWinNtIoProtocolGuid,
&WinNtIo,
This->DriverBindingHandle,
Handle,
EFI_OPEN_PROTOCOL_BY_DRIVER
);
if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
gBS->CloseProtocol (
Handle,
&gEfiDevicePathProtocolGuid,
This->DriverBindingHandle,
Handle
);
return Status;
}
if (Status == EFI_ALREADY_STARTED) {
if (RemainingDevicePath == NULL) {
return EFI_SUCCESS;
}
//
// Make sure a child handle does not already exist. This driver can only
// produce one child per serial port.
//
Status = gBS->OpenProtocolInformation (
Handle,
&gEfiWinNtIoProtocolGuid,
&OpenInfoBuffer,
&EntryCount
);
if (EFI_ERROR (Status)) {
return Status;
}
Status = EFI_ALREADY_STARTED;
for (Index = 0; Index < EntryCount; Index++) {
if (OpenInfoBuffer[Index].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
Status = gBS->OpenProtocol (
OpenInfoBuffer[Index].ControllerHandle,
&gEfiSerialIoProtocolGuid,
&SerialIo,
This->DriverBindingHandle,
Handle,
EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
if (!EFI_ERROR (Status)) {
EfiCopyMem (&Node, RemainingDevicePath, sizeof (UART_DEVICE_PATH));
Status = SerialIo->SetAttributes (
SerialIo,
Node.BaudRate,
SerialIo->Mode->ReceiveFifoDepth,
SerialIo->Mode->Timeout,
Node.Parity,
Node.DataBits,
Node.StopBits
);
}
break;
}
}
gBS->FreePool (OpenInfoBuffer);
return Status;
}
//
// Check to see if we can access the hardware device. If it's Open in NT we
// will not get access.
//
NtHandle = WinNtIo->WinNtThunk->CreateFile (
WinNtIo->EnvString,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (NtHandle == INVALID_HANDLE_VALUE) {
Status = EFI_DEVICE_ERROR;
goto Error;
}
//
// Construct Private data
//
Status = gBS->AllocatePool (
EfiBootServicesData,
sizeof (WIN_NT_SERIAL_IO_PRIVATE_DATA),
&Private
);
if (EFI_ERROR (Status)) {
goto Error;
}
//
// This signature must be valid before any member function is called
//
Private->Signature = WIN_NT_SERIAL_IO_PRIVATE_DATA_SIGNATURE;
Private->NtHandle = NtHandle;
Private->ControllerHandle = Handle;
Private->Handle = NULL;
Private->WinNtThunk = WinNtIo->WinNtThunk;
Private->ParentDevicePath = ParentDevicePath;
Private->ControllerNameTable = NULL;
Private->SoftwareLoopbackEnable = FALSE;
Private->HardwareLoopbackEnable = FALSE;
Private->HardwareFlowControl = FALSE;
Private->Fifo.First = 0;
Private->Fifo.Last = 0;
Private->Fifo.Surplus = SERIAL_MAX_BUFFER_SIZE;
EfiLibAddUnicodeString (
"eng",
gWinNtSerialIoComponentName.SupportedLanguages,
&Private->ControllerNameTable,
WinNtIo->EnvString
);
Private->SerialIo.Revision = SERIAL_IO_INTERFACE_REVISION;
Private->SerialIo.Reset = WinNtSerialIoReset;
Private->SerialIo.SetAttributes = WinNtSerialIoSetAttributes;
Private->SerialIo.SetControl = WinNtSerialIoSetControl;
Private->SerialIo.GetControl = WinNtSerialIoGetControl;
Private->SerialIo.Write = WinNtSerialIoWrite;
Private->SerialIo.Read = WinNtSerialIoRead;
Private->SerialIo.Mode = &Private->SerialIoMode;
if (RemainingDevicePath != NULL) {
//
// Match the configuration of the RemainingDevicePath. IsHandleSupported()
// already checked to make sure the RemainingDevicePath contains settings
// that we can support.
//
EfiCopyMem (&Private->UartDevicePath, RemainingDevicePath, sizeof (UART_DEVICE_PATH));
} else {
//
// Build the device path by appending the UART node to the ParentDevicePath
// from the WinNtIo handle. The Uart setings are zero here, since
// SetAttribute() will update them to match the default setings.
//
EfiZeroMem (&Private->UartDevicePath, sizeof (UART_DEVICE_PATH));
Private->UartDevicePath.Header.Type = MESSAGING_DEVICE_PATH;
Private->UartDevicePath.Header.SubType = MSG_UART_DP;
SetDevicePathNodeLength ((EFI_DEVICE_PATH_PROTOCOL *) &Private->UartDevicePath, sizeof (UART_DEVICE_PATH));
}
//
// Build the device path by appending the UART node to the ParentDevicePath
// from the WinNtIo handle. The Uart setings are zero here, since
// SetAttribute() will update them to match the current setings.
//
Private->DevicePath = EfiAppendDevicePathNode (
ParentDevicePath,
(EFI_DEVICE_PATH_PROTOCOL *) &Private->UartDevicePath
);
if (Private->DevicePath == NULL) {
Status = EFI_OUT_OF_RESOURCES;
goto Error;
}
//
// Fill in Serial I/O Mode structure based on either the RemainingDevicePath or defaults.
//
Private->SerialIoMode.ControlMask = SERIAL_CONTROL_MASK;
Private->SerialIoMode.Timeout = SERIAL_TIMEOUT_DEFAULT;
Private->SerialIoMode.BaudRate = Private->UartDevicePath.BaudRate;
Private->SerialIoMode.ReceiveFifoDepth = SERIAL_FIFO_DEFAULT;
Private->SerialIoMode.DataBits = Private->UartDevicePath.DataBits;
Private->SerialIoMode.Parity = Private->UartDevicePath.Parity;
Private->SerialIoMode.StopBits = Private->UartDevicePath.StopBits;
//
// Issue a reset to initialize the COM port
//
Status = Private->SerialIo.Reset (&Private->SerialIo);
if (EFI_ERROR (Status)) {
goto Error;
}
//
// Create new child handle
//
Status = gBS->InstallMultipleProtocolInterfaces (
&Private->Handle,
&gEfiSerialIoProtocolGuid,
&Private->SerialIo,
&gEfiDevicePathProtocolGuid,
Private->DevicePath,
NULL
);
if (EFI_ERROR (Status)) {
goto Error;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?