diskio.c
来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 823 行 · 第 1/2 页
C
823 行
Private = DISK_IO_PRIVATE_DATA_FROM_THIS (This);
BlockIo = Private->BlockIo;
Media = BlockIo->Media;
BlockSize = Media->BlockSize;
if (Media->MediaId != MediaId) {
return EFI_MEDIA_CHANGED;
}
WorkingBuffer = Buffer;
WorkingBufferSize = BufferSize;
//
// Allocate a temporary buffer for operation
//
DataBufferSize = BlockSize * DATA_BUFFER_BLOCK_NUM;
if (Media->IoAlign > 1) {
PreData = EfiLibAllocatePool (DataBufferSize + Media->IoAlign);
Data = PreData - ((UINTN) PreData & (Media->IoAlign - 1)) + Media->IoAlign;
} else {
PreData = EfiLibAllocatePool (DataBufferSize);
Data = PreData;
}
if (PreData == NULL) {
return EFI_OUT_OF_RESOURCES;
}
Lba = DivU64x32 (Offset, BlockSize, &UnderRun);
Length = BlockSize - UnderRun;
TransactionComplete = FALSE;
Status = EFI_SUCCESS;
if (UnderRun != 0) {
//
// Offset starts in the middle of an Lba, so read the entire block.
//
Status = BlockIo->ReadBlocks (
BlockIo,
MediaId,
Lba,
BlockSize,
Data
);
if (EFI_ERROR (Status)) {
goto Done;
}
if (Length > BufferSize) {
Length = BufferSize;
TransactionComplete = TRUE;
}
EfiCopyMem (WorkingBuffer, Data + UnderRun, Length);
WorkingBuffer += Length;
WorkingBufferSize -= Length;
if (WorkingBufferSize == 0) {
goto Done;
}
Lba += 1;
}
OverRunLba = Lba + DivU64x32 (WorkingBufferSize, BlockSize, &OverRun);
if (!TransactionComplete && WorkingBufferSize >= BlockSize) {
//
// If the DiskIo maps directly to a BlockIo device do the read.
//
if (OverRun != 0) {
WorkingBufferSize -= OverRun;
}
//
// Check buffer alignment
//
IsBufferAligned = (UINTN) WorkingBuffer & (UINTN) (Media->IoAlign - 1);
if (Media->IoAlign <= 1 || IsBufferAligned == 0) {
//
// Alignment is satisfied, so read them together
//
Status = BlockIo->ReadBlocks (
BlockIo,
MediaId,
Lba,
WorkingBufferSize,
WorkingBuffer
);
if (EFI_ERROR (Status)) {
goto Done;
}
WorkingBuffer += WorkingBufferSize;
} else {
//
// Use the allocated buffer instead of the original buffer
// to avoid alignment issue.
// Here, the allocated buffer (8-byte align) can satisfy the alignment
//
LastRead = FALSE;
do {
if (WorkingBufferSize <= DataBufferSize) {
//
// It is the last calling to readblocks in this loop
//
DataBufferSize = WorkingBufferSize;
LastRead = TRUE;
}
Status = BlockIo->ReadBlocks (
BlockIo,
MediaId,
Lba,
DataBufferSize,
Data
);
if (EFI_ERROR (Status)) {
goto Done;
}
EfiCopyMem (WorkingBuffer, Data, DataBufferSize);
WorkingBufferSize -= DataBufferSize;
WorkingBuffer += DataBufferSize;
Lba += DATA_BUFFER_BLOCK_NUM;
} while (!LastRead);
}
}
if (!TransactionComplete && OverRun != 0) {
//
// Last read is not a complete block.
//
Status = BlockIo->ReadBlocks (
BlockIo,
MediaId,
OverRunLba,
BlockSize,
Data
);
if (EFI_ERROR (Status)) {
goto Done;
}
EfiCopyMem (WorkingBuffer, Data, OverRun);
}
Done:
if (PreData != NULL) {
gBS->FreePool (PreData);
}
return Status;
}
EFI_STATUS
EFIAPI
DiskIoWriteDisk (
IN EFI_DISK_IO_PROTOCOL *This,
IN UINT32 MediaId,
IN UINT64 Offset,
IN UINTN BufferSize,
IN VOID *Buffer
)
/*++
Routine Description:
Read BufferSize bytes from Offset into Buffer.
Writes may require a read modify write to support writes that are not
aligned on sector boundaries. There are three cases:
UnderRun - The first byte is not on a sector boundary or the write request
is less than a sector in length. Read modify write is required.
Aligned - A write of N contiguous sectors.
OverRun - The last byte is not on a sector boundary. Read modified write
required.
Arguments:
This - Protocol instance pointer.
MediaId - Id of the media, changes every time the media is replaced.
Offset - The starting byte offset to read from.
BufferSize - Size of Buffer.
Buffer - Buffer containing read data.
Returns:
EFI_SUCCESS - The data was written correctly to the device.
EFI_WRITE_PROTECTED - The device can not be written to.
EFI_DEVICE_ERROR - The device reported an error while performing the write.
EFI_NO_MEDIA - There is no media in the device.
EFI_MEDIA_CHNAGED - The MediaId does not matched the current device.
EFI_INVALID_PARAMETER - The write request contains device addresses that are not
valid for the device.
EFI_OUT_OF_RESOURCES
--*/
{
EFI_STATUS Status;
DISK_IO_PRIVATE_DATA *Private;
EFI_BLOCK_IO_PROTOCOL *BlockIo;
EFI_BLOCK_IO_MEDIA *Media;
UINT32 BlockSize;
UINT64 Lba;
UINT64 OverRunLba;
UINTN UnderRun;
UINTN OverRun;
BOOLEAN TransactionComplete;
UINTN WorkingBufferSize;
UINT8 *WorkingBuffer;
UINTN Length;
UINT8 *Data;
UINT8 *PreData;
UINTN IsBufferAligned;
UINTN DataBufferSize;
BOOLEAN LastWrite;
Private = DISK_IO_PRIVATE_DATA_FROM_THIS (This);
BlockIo = Private->BlockIo;
Media = BlockIo->Media;
BlockSize = Media->BlockSize;
if (Media->ReadOnly) {
return EFI_WRITE_PROTECTED;
}
if (Media->MediaId != MediaId) {
return EFI_MEDIA_CHANGED;
}
DataBufferSize = BlockSize * DATA_BUFFER_BLOCK_NUM;
if (Media->IoAlign > 1) {
PreData = EfiLibAllocatePool (DataBufferSize + Media->IoAlign);
Data = PreData - ((UINTN) PreData & (Media->IoAlign - 1)) + Media->IoAlign;
} else {
PreData = EfiLibAllocatePool (DataBufferSize);
Data = PreData;
}
if (PreData == NULL) {
return EFI_OUT_OF_RESOURCES;
}
WorkingBuffer = Buffer;
WorkingBufferSize = BufferSize;
Lba = DivU64x32 (Offset, BlockSize, &UnderRun);
Length = BlockSize - UnderRun;
TransactionComplete = FALSE;
Status = EFI_SUCCESS;
if (UnderRun != 0) {
//
// Offset starts in the middle of an Lba, so do read modify write.
//
Status = BlockIo->ReadBlocks (
BlockIo,
MediaId,
Lba,
BlockSize,
Data
);
if (EFI_ERROR (Status)) {
goto Done;
}
if (Length > BufferSize) {
Length = BufferSize;
TransactionComplete = TRUE;
}
EfiCopyMem (Data + UnderRun, WorkingBuffer, Length);
Status = BlockIo->WriteBlocks (
BlockIo,
MediaId,
Lba,
BlockSize,
Data
);
if (EFI_ERROR (Status)) {
goto Done;
}
WorkingBuffer += Length;
WorkingBufferSize -= Length;
if (WorkingBufferSize == 0) {
goto Done;
}
Lba += 1;
}
OverRunLba = Lba + DivU64x32 (WorkingBufferSize, BlockSize, &OverRun);
if (!TransactionComplete && WorkingBufferSize >= BlockSize) {
//
// If the DiskIo maps directly to a BlockIo device do the write.
//
if (OverRun != 0) {
WorkingBufferSize -= OverRun;
}
//
// Check buffer alignment
//
IsBufferAligned = (UINTN) WorkingBuffer & (UINTN) (Media->IoAlign - 1);
if (Media->IoAlign <= 1 || IsBufferAligned == 0) {
//
// Alignment is satisfied, so write them together
//
Status = BlockIo->WriteBlocks (
BlockIo,
MediaId,
Lba,
WorkingBufferSize,
WorkingBuffer
);
if (EFI_ERROR (Status)) {
goto Done;
}
WorkingBuffer += WorkingBufferSize;
} else {
//
// The buffer parameter is not aligned with the request
// So use the allocated instead.
// It can fit almost all the cases.
//
LastWrite = FALSE;
do {
if (WorkingBufferSize <= DataBufferSize) {
//
// It is the last calling to writeblocks in this loop
//
DataBufferSize = WorkingBufferSize;
LastWrite = TRUE;
}
EfiCopyMem (Data, WorkingBuffer, DataBufferSize);
Status = BlockIo->WriteBlocks (
BlockIo,
MediaId,
Lba,
DataBufferSize,
Data
);
if (EFI_ERROR (Status)) {
goto Done;
}
WorkingBufferSize -= DataBufferSize;
WorkingBuffer += DataBufferSize;
Lba += DATA_BUFFER_BLOCK_NUM;
} while (!LastWrite);
}
}
if (!TransactionComplete && OverRun != 0) {
//
// Last bit is not a complete block, so do a read modify write.
//
Status = BlockIo->ReadBlocks (
BlockIo,
MediaId,
OverRunLba,
BlockSize,
Data
);
if (EFI_ERROR (Status)) {
goto Done;
}
EfiCopyMem (Data, WorkingBuffer, OverRun);
Status = BlockIo->WriteBlocks (
BlockIo,
MediaId,
OverRunLba,
BlockSize,
Data
);
if (EFI_ERROR (Status)) {
goto Done;
}
}
Done:
if (PreData != NULL) {
gBS->FreePool (PreData);
}
return Status;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?