/** @file
Floppy Peim to support Recovery function from Floppy device.
Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
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
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "FloppyPeim.h"
//
// DMA2: Clear Byte Ptr, Enable
//
{
0
},
{
0
},
//
// DMA1: Clear Byte Ptr, Enable
//
{
0
},
{
0
},
//
// Configure Channel 4 for Cascade Mode
// Clear DMA Request and enable DREQ
//
{
},
{
0
},
{
0
},
//
// Configure DMA1 (Channels 0-3) for Single Mode
// Clear DMA Request and enable DREQ
//
{
},
{
0
},
{
0
},
{
},
{
1
},
{
1
},
{
},
{
2
},
{
2
},
{
},
{
3
},
{
3
},
//
// Configure DMA2 (Channels 5-7) for Single Mode
// Clear DMA Request and enable DREQ
//
{
},
{
1
},
{
1
},
{
},
{
2
},
{
2
},
{
},
{
3
},
{
3
}
};
//
// Table of diskette parameters of various diskette types
//
{
0x09,
0x50,
0xff,
0x2,
0x27,
0x4,
0x25,
0x14,
0x80
},
{
0x09,
0x2a,
0xff,
0x2,
0x27,
0x4,
0x25,
0x0f,
0x40
},
{
0x0f,
0x54,
0xff,
0x2,
0x4f,
0x4,
0x25,
0x0f,
0x0
},
{
0x09,
0x50,
0xff,
0x2,
0x4f,
0x4,
0x25,
0x0f,
0x80
},
{
0x09,
0x2a,
0xff,
0x2,
0x4f,
0x4,
0x25,
0x0f,
0x80
},
{
0x12,
0x1b,
0xff,
0x2,
0x4f,
0x4,
0x25,
0x0f,
0x0
},
{
0x09,
0x2a,
0xff,
0x2,
0x4f,
0x4,
0x25,
0x0f,
0x80
},
{
0x12,
0x1b,
0xff,
0x2,
0x4f,
0x4,
0x25,
0x0f,
0x0
},
{
0x24,
0x1b,
0xff,
0x2,
0x4f,
0x4,
0x25,
0x0f,
0xc0
}
};
//
// Byte per sector corresponding to various device types.
//
{
},
{
},
0,
{{0}}
};
/**
Wait and check if bits for DIO and RQM of FDC Main Status Register
indicates FDC is ready for read or write.
Before writing to FDC or reading from FDC, the Host must examine
the bit7(RQM) and bit6(DIO) of the Main Status Register.
That is to say:
Command bytes can not be written to Data Register unless RQM is 1 and DIO is 0.
Result bytes can not be read from Data Register unless RQM is 1 and DIO is 1.
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
@param DataIn Indicates data input or output.
TRUE means input.
FALSE means output.
@param TimeoutInMseconds Timeout value to wait.
@retval EFI_SUCCESS FDC is ready.
@retval EFI_NOT_READY FDC is not ready within the specified time period.
**/
)
{
//
// Check bit6 of Main Status Register.
//
BitInOut = 0;
if (DataIn) {
}
do {
//
// FDC is ready
//
break;
}
} while (--Delay > 0);
if (Delay == 0) {
//
// FDC is not ready within the specified time period
//
return EFI_NOT_READY;
}
return EFI_SUCCESS;
}
/**
Read a byte from FDC data register.
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
@param Pointer Pointer to buffer to hold data read from FDC.
@retval EFI_SUCCESS Byte successfully read.
@retval EFI_DEVICE_ERROR FDC is not ready.
**/
)
{
//
// Wait for 1ms and detect the FDC is ready to be read
//
//
// FDC is not ready.
//
return EFI_DEVICE_ERROR;
}
return EFI_SUCCESS;
}
/**
Write a byte to FDC data register.
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
@param Pointer Pointer to data to write.
@retval EFI_SUCCESS Byte successfully written.
@retval EFI_DEVICE_ERROR FDC is not ready.
**/
)
{
//
// Wait for 1ms and detect the FDC is ready to be written
//
//
// FDC is not ready.
//
return EFI_DEVICE_ERROR;
}
return EFI_SUCCESS;
}
/**
Get Sts0 and Pcn status from FDC
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV
@param Sts0 Value of Sts0
@param Pcn Value of Pcn
@retval EFI_SUCCESS Successfully retrieved status value of Sts0 and Pcn.
@retval EFI_DEVICE_ERROR Fail to send SENSE_INT_STATUS_CMD.
@retval EFI_DEVICE_ERROR Fail to read Sts0.
@retval EFI_DEVICE_ERROR Fail to read Pcn.
**/
)
{
return EFI_DEVICE_ERROR;
}
return EFI_DEVICE_ERROR;
}
return EFI_DEVICE_ERROR;
}
return EFI_SUCCESS;
}
/**
Issue Specify command.
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
@retval EFI_SUCCESS Specify command successfully issued.
@retval EFI_DEVICE_ERROR FDC device has errors.
**/
Specify (
)
{
//
// set SRT, HUT
//
//
// 0xdf;
// set HLT and DMA
//
return EFI_DEVICE_ERROR;
}
}
return EFI_SUCCESS;
}
/**
Wait until busy bit is cleared.
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
@param DevPos Position of FDC (Driver A or B)
@param TimeoutInMseconds Timeout value to wait.
@retval EFI_SUCCESS Busy bit has been cleared before timeout.
@retval EFI_TIMEOUT Time goes out before busy bit is cleared.
**/
)
{
//
// How to determine drive and command are busy or not: by the bits of Main Status Register
// bit0: Drive 0 busy (drive A)
// bit1: Drive 1 busy (drive B)
// bit4: Command busy
//
// set mask: for drive A set bit0 & bit4; for drive B set bit1 & bit4
//
do {
//
// not busy
//
break;
}
} while (--Delay > 0);
if (Delay == 0) {
return EFI_TIMEOUT;
}
return EFI_SUCCESS;
}
/**
Reset FDC device.
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV
@param DevPos Index of FDC device.
@retval EFI_SUCCESS FDC device successfully reset.
@retval EFI_DEVICE_ERROR Fail to reset FDC device.
**/
FdcReset (
)
{
//
// Reset specified Floppy Logic Drive according to Fdd -> Disk
// Set Digital Output Register(DOR) to do reset work
// bit0 & bit1 of DOR : Drive Select
// bit2 : Reset bit
// bit3 : DMA and Int bit
// Reset : A "0" written to bit2 resets the FDC, this reset will remain active until
// a "1" is written to this bit.
// Reset step 1:
// use bit0 & bit1 to select the logic drive
// write "0" to bit2
//
Data = 0x0;
//
// Wait some time, at least 120us.
//
//
// Reset step 2:
// write "1" to bit2
// write "1" to bit3 : enable DMA
//
Data |= 0x0C;
//
// Wait until specified floppy logic drive is not busy
//
return EFI_DEVICE_ERROR;
}
//
// Set the Transfer Data Rate
//
//
// Issue Sense interrupt command for each drive (totally 4 drives)
//
return EFI_DEVICE_ERROR;
}
}
//
// Issue Specify command
//
return EFI_DEVICE_ERROR;
}
return EFI_SUCCESS;
}
/**
Turn on the motor of floppy drive.
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
@param Info Information of floppy device.
@retval EFI_SUCCESS Motor is successfully turned on.
@retval EFI_SUCCESS Motor is already on.
@retval EFI_DEVICE_ERROR Busy bit of FDC cannot be cleared.
**/
MotorOn (
)
{
//
// Control of the floppy drive motors is a big pain. If motor is off, you have to turn it
// on first. But you can not leave the motor on all the time, since that would wear out the
// disk. On the other hand, if you turn the motor off after each operation, the system performance
// will be awful. The compromise used in this driver is to leave the motor on for 2 seconds after
// each operation. If a new operation is started in that interval(2s), the motor need not be
// turned on again. If no new operation is started, a timer goes off and the motor is turned off.
//
//
// If the Motor is already on, just return EFI_SUCCESS.
//
return EFI_SUCCESS;
}
//
// The drive's motor is off, so need turn it on.
// First check if command and drive are busy or not.
//
return EFI_DEVICE_ERROR;
}
//
// for drive A: 1CH, drive B: 2DH
//
Data = 0x0C;
if (DevPos == 0) {
Data |= DRVA_MOTOR_ON;
} else {
Data |= DRVB_MOTOR_ON;
}
//
// Turn on the motor and wait for some time to ensure it takes effect.
//
return EFI_SUCCESS;
}
/**
Turn off the motor of floppy drive.
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
@param Info Information of floppy device.
**/
MotorOff (
)
{
return;
}
//
// The motor is on, so need motor off
//
Data = 0x0C;
}
/**
Recalibrate the FDC device.
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
@param Info Information of floppy device.
@retval EFI_SUCCESS FDC successfully recalibrated.
@retval EFI_DEVICE_ERROR Fail to send RECALIBRATE_CMD.
@retval EFI_DEVICE_ERROR Fail to get status value of Sts0 and Pcn.
@retval EFI_DEVICE_ERROR Fail to recalibrate FDC device.
**/
)
{
//
// We would try twice.
//
Count = 2;
while (Count > 0) {
//
// drive select
//
if (DevPos == 0) {
Command.DiskHeadSel = 0;
} else {
}
return EFI_DEVICE_ERROR;
}
}
return EFI_DEVICE_ERROR;
}
//
// Recalibration is successful.
//
return EFI_SUCCESS;
} else {
//
// Recalibration is not successful. Try again.
// If trial is used out, return EFI_DEVICE_ERROR.
//
Count--;
if (Count == 0) {
return EFI_DEVICE_ERROR;
}
}
}
return EFI_SUCCESS;
}
/**
Seek for the cylinder according to given LBA.
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
@param Info Information of floppy device.
@param Lba LBA for which to seek for cylinder.
@retval EFI_SUCCESS Successfully moved to the destination cylinder.
@retval EFI_SUCCESS Destination cylinder is just the present cylinder.
@retval EFI_DEVICE_ERROR Fail to move to the destination cylinder.
**/
Seek (
)
{
if (Info->NeedRecalibrate) {
return EFI_DEVICE_ERROR;
}
//
// Recalibrate Success
//
}
//
// Get the base of disk parameter information corresponding to its type.
//
//
// Calculate cylinder based on Lba and EOT
//
//
// If the dest cylinder is the present cylinder, unnecessary to do the seek operation
//
return EFI_SUCCESS;
}
//
// Calculate the head : 0 or 1
//
if (DevPos == 0) {
Command.DiskHeadSel = 0;
} else {
}
//
// Send command to move to destination cylinder.
//
return EFI_DEVICE_ERROR;
}
}
//
// Calculate waiting time, which is proportional to the gap between destination
// cylinder and present cylinder.
//
} else {
}
//
// Confirm if the new cylinder is the destination and status is correct.
//
return EFI_DEVICE_ERROR;
}
return EFI_SUCCESS;
} else {
return EFI_DEVICE_ERROR;
}
}
/**
Check if diskette is changed.
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV
@param Info Information of floppy device.
@retval EFI_SUCCESS Diskette is not changed.
@retval EFI_MEDIA_CHANGED Diskette is changed.
@retval EFI_NO_MEDIA No diskette.
@retval EFI_DEVICE_ERROR Fail to do the seek or recalibrate operation.
**/
)
{
//
// Check change line
//
} else {
}
if (Status != EFI_SUCCESS) {
//
// Fail to do the seek or recalibrate operation
//
return EFI_DEVICE_ERROR;
}
return EFI_NO_MEDIA;
}
return EFI_MEDIA_CHANGED;
}
return EFI_SUCCESS;
}
/**
Detects if FDC device exists.
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV
@param Info Information of floppy device.
@param MediaInfo Information of floppy media.
@retval TRUE FDC device exists and is working properly.
@retval FALSE FDC device does not exist or cannot work properly.
**/
)
{
if (Status != EFI_SUCCESS) {
return FALSE;
}
if (Status != EFI_SUCCESS) {
return FALSE;
}
//
// Set Media Parameter
//
//
// Check Media
//
switch (Status) {
case EFI_NO_MEDIA:
//
// No diskette in floppy.
//
break;
case EFI_MEDIA_CHANGED:
case EFI_SUCCESS:
//
// Diskette exists in floppy.
//
break;
default:
//
// EFI_DEVICE_ERROR
//
return FALSE;
}
//
// Get the base of disk parameter information corresponding to its type.
//
return TRUE;
}
/**
Enumerate floppy device
@param FdcBlkIoDev Instance of floppy device controller
@return Number of FDC devices.
**/
)
{
DevNo = 0;
//
// DevPos=0 means Drive A, 1 means Drive B.
//
//
// Detecting device presence
//
//
// Reset FDC
//
continue;
}
//
// Discover FDC device
//
CopyMem (
sizeof (EFI_PEI_BLOCK_IO_MEDIA)
);
DevNo++;
} else {
//
// Assume controller error
//
);
}
}
return DevNo;
}
/**
Checks result reflected by FDC_RESULT_PACKET.
@param Result FDC_RESULT_PACKET read from FDC after certain operation.
@param Info Information of floppy device.
@retval EFI_SUCCESS Result is healthy.
@retval EFI_DEVICE_ERROR Result is not healthy.
**/
)
{
//
// Seek error
//
}
return EFI_DEVICE_ERROR;
}
//
// Check Status Register1
//
return EFI_DEVICE_ERROR;
}
//
// Check Status Register2
//
return EFI_DEVICE_ERROR;
}
return EFI_SUCCESS;
}
/**
Fill parameters for command packet.
@param Info Information of floppy device.
@param Lba Logical block address.
@param Command Command for which for fill parameters.
**/
FillPara (
)
{
//
// Get the base of disk parameter information corresponding to its type.
//
if (DevPos == 0) {
Command->DiskHeadSel = 0;
} else {
}
}
/**
Setup specifed FDC device.
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
@param DevPos Index of FDC device.
@retval EFI_SUCCESS FDC device successfully set up.
@retval EFI_DEVICE_ERROR FDC device has errors.
**/
Setup (
)
{
return Status;
}
/**
Setup DMA channels to read data.
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
@param Buffer Memory buffer for DMA transfer.
@param BlockSize the number of the bytes in one block.
@param NumberOfBlocks Number of blocks to read.
**/
SetDMA (
)
{
//
// Mask DMA channel 2;
//
//
//
//
// Set mode
//
//
// Set base address and page register
//
//
// Set count register
//
//
// Clear channel 2 mask
//
}
/**
According to the block range specified by Lba and NumberOfBlocks, calculate
the number of blocks in the same sector, which can be transferred in a batch.
@param Info Information of floppy device.
@param Lba Start address of block range.
@param NumberOfBlocks Number of blocks of the range.
@return Number of blocks in the same sector.
**/
)
{
//
// Get the base of disk parameter information corresponding to its type.
//
if (SectorsInTrack < NumberOfBlocks) {
//
// Not all the block range locates in the same sector
//
return SectorsInTrack;
} else {
//
// All the block range is in the same sector.
//
return NumberOfBlocks;
}
}
/**
Read data sector from FDC device.
@param FdcBlkIoDev Instance of FDC_BLK_IO_DEV.
@param Info Information of floppy device.
@param Buffer Buffer to setup for DMA.
@param Lba The start address to read.
@param NumberOfBlocks Number of blocks to read.
@retval EFI_SUCCESS Data successfully read out.
@retval EFI_DEVICE_ERROR FDC device has errors.
@retval EFI_TIMEOUT Command does not take effect in time.
**/
)
{
if (Status != EFI_SUCCESS) {
return EFI_DEVICE_ERROR;
}
//
// Set up DMA
//
//
// Allocate Read command packet
//
//
// Fill parameters for command.
//
//
// Write command bytes to FDC
//
return EFI_DEVICE_ERROR;
}
}
//
// Wait for some time until command takes effect.
//
do {
break;
}
} while (--Times > 0);
if (Times == 0) {
//
// Command fails to take effect in time, return EFI_TIMEOUT.
//
return EFI_TIMEOUT;
}
//
// Read result bytes from FDC
//
return EFI_DEVICE_ERROR;
}
}
}
/**
Gets the count of block I/O devices that one specific block driver detects.
This function is used for getting the count of block I/O devices that one
specific block driver detects. To the PEI ATAPI driver, it returns the number
of all the detected ATAPI devices it detects during the enumeration process.
To the PEI legacy floppy driver, it returns the number of all the legacy
devices it finds during its enumeration process. If no device is detected,
then the function will return zero.
@param[in] PeiServices General-purpose services that are available
to every PEIM.
@param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI
instance.
@param[out] NumberBlockDevices The number of block I/O devices discovered.
@retval EFI_SUCCESS Operation performed successfully.
**/
)
{
FdcBlkIoDev = NULL;
return EFI_SUCCESS;
}
/**
Gets a block device's media information.
This function will provide the caller with the specified block device's media
information. If the media changes, calling this function will update the media
information accordingly.
@param[in] PeiServices General-purpose services that are available to every
PEIM
@param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
@param[in] DeviceIndex Specifies the block device to which the function wants
to talk. Because the driver that implements Block I/O
PPIs will manage multiple block devices, the PPIs that
want to talk to a single device must specify the
device index that was assigned during the enumeration
process. This index is a number from one to
NumberBlockDevices.
@param[out] MediaInfo The media information of the specified block media.
The caller is responsible for the ownership of this
data structure.
@retval EFI_SUCCESS Media information about the specified block device
was obtained successfully.
@retval EFI_DEVICE_ERROR Cannot get the media information due to a hardware
error.
@retval Others Other failure occurs.
**/
)
{
FdcBlkIoDev = NULL;
return EFI_INVALID_PARAMETER;
}
//
// DeviceIndex is a value from 1 to NumberBlockDevices.
//
return EFI_INVALID_PARAMETER;
}
//
// Probe media and retrieve latest media information
//
);
if (!Healthy) {
return EFI_DEVICE_ERROR;
}
CopyMem (
sizeof (EFI_PEI_BLOCK_IO_MEDIA)
);
return EFI_SUCCESS;
}
/**
Reads the requested number of blocks from the specified block device.
The function reads the requested number of blocks from the device. All the
blocks are read, or an error is returned. If there is no media in the device,
the function returns EFI_NO_MEDIA.
@param[in] PeiServices General-purpose services that are available to
every PEIM.
@param[in] This Indicates the EFI_PEI_RECOVERY_BLOCK_IO_PPI instance.
@param[in] DeviceIndex Specifies the block device to which the function wants
to talk. Because the driver that implements Block I/O
PPIs will manage multiple block devices, the PPIs that
want to talk to a single device must specify the device
index that was assigned during the enumeration process.
This index is a number from one to NumberBlockDevices.
@param[in] StartLBA The starting logical block address (LBA) to read from
on the device
@param[in] BufferSize The size of the Buffer in bytes. This number must be
a multiple of the intrinsic block size of the device.
@param[out] Buffer A pointer to the destination buffer for the data.
The caller is responsible for the ownership of the
buffer.
@retval EFI_SUCCESS The data was read correctly from the device.
@retval EFI_DEVICE_ERROR The device reported an error while attempting
to perform the read operation.
@retval EFI_INVALID_PARAMETER The read request contains LBAs that are not
valid, or the buffer is not properly aligned.
@retval EFI_NO_MEDIA There is no media in the device.
@retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of
the intrinsic block size of the device.
**/
)
{
FdcBlkIoDev = NULL;
return EFI_INVALID_PARAMETER;
}
return EFI_INVALID_PARAMETER;
}
if (Status != EFI_SUCCESS) {
return EFI_DEVICE_ERROR;
}
if (!MediaInfo.MediaPresent) {
return EFI_NO_MEDIA;
}
//
// If BufferSize cannot be divided by block size of FDC device,
// return EFI_BAD_BUFFER_SIZE.
//
if (BufferSize % BlockSize != 0) {
return EFI_BAD_BUFFER_SIZE;
}
if ((StartLBA + NumberOfBlocks - 1) > FdcBlkIoDev->DeviceInfo[DeviceIndex - 1].MediaInfo.LastBlock) {
return EFI_INVALID_PARAMETER;
}
//
// If fail to allocate memory under ISA_MAX_MEMORY_ADDRESS, designate the address space for DMA
//
}
if (Status != EFI_SUCCESS) {
return EFI_DEVICE_ERROR;
}
if (Status != EFI_SUCCESS) {
return EFI_DEVICE_ERROR;
}
//
// Read data in batches.
// Blocks in the same cylinder are read out in a batch.
//
while ((Count = GetTransferBlockCount (
)) != 0 && Status == EFI_SUCCESS) {
Status = ReadDataSector (
);
NumberOfBlocks -= Count;
}
switch (Status) {
case EFI_SUCCESS:
return EFI_SUCCESS;
default:
return EFI_DEVICE_ERROR;
}
}
/**
Initializes the floppy disk controller and installs FDC Block I/O PPI.
@param FileHandle Handle of the file being invoked.
@param PeiServices Describes the list of possible PEI Services.
@retval EFI_SUCCESS Successfully initialized FDC and installed PPI.
@retval EFI_NOT_FOUND Cannot find FDC device.
@retval EFI_OUT_OF_RESOURCES Have no enough memory to create instance or descriptors.
@retval Other Fail to install FDC Block I/O PPI.
**/
)
{
return Status;
}
//
// Allocate memory for instance of FDC_BLK_IO_DEV and copy initial value
// from template to it.
//
if (FdcBlkIoDev == NULL) {
return EFI_OUT_OF_RESOURCES;
}
//
// Initialize DMA controller to enable all channels.
//
}
//
// Enumerate FDC devices.
//
if (DeviceCount == 0) {
return EFI_NOT_FOUND;
}
}