fwvolblock.c

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

C
622
字号
/*++

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:

  FwVolBlock.c

Abstract:

  Firmware Volume Block protocol..  Consumes FV hobs and creates
  appropriate block protocols.

  Also consumes NT_NON_MM_FV envinronment variable and produces appropriate
  block protocols fro them also... (this is TBD)

--*/

#include "Tiano.h"
#include "FwVolBlock.h"


EFI_FW_VOL_BLOCK_DEVICE  mFwVolBlock = {
  FVB_DEVICE_SIGNATURE,
  NULL,
  {
    {
      HARDWARE_DEVICE_PATH,
      HW_MEMMAP_DP,
      (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),
      (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8),
      EfiMemoryMappedIO,
      (EFI_PHYSICAL_ADDRESS)0,
      (EFI_PHYSICAL_ADDRESS)0,
    },
    {
      END_DEVICE_PATH_TYPE,
      END_ENTIRE_DEVICE_PATH_SUBTYPE,
      END_DEVICE_PATH_LENGTH,
      0      
    },
  },
  {
    FwVolBlockGetAttributes,
    (EFI_FVB_SET_ATTRIBUTES)FwVolBlockSetAttributes,
    FwVolBlockGetPhysicalAddress,
    FwVolBlockGetBlockSize,
    FwVolBlockReadBlock,
    (EFI_FVB_WRITE)FwVolBlockWriteBlock,
    (EFI_FVB_ERASE_BLOCKS)FwVolBlockEraseBlock,
    NULL      
  },
  0,
  NULL,
  0,
  0
};




EFI_STATUS
EFIAPI
FwVolBlockGetAttributes (
  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL   *This,
  OUT EFI_FVB_ATTRIBUTES                          *Attributes
  )
/*++

Routine Description:
    Retrieves Volume attributes.  No polarity translations are done.

Arguments:
    This - Calling context
    Attributes - output buffer which contains attributes

Returns:
    EFI_SUCCESS - The firmware volume attributes were returned.

--*/
{
  EFI_FW_VOL_BLOCK_DEVICE               *FvbDevice;
  
  FvbDevice = FVB_DEVICE_FROM_THIS (This);

  //
  // Since we are read only, it's safe to get attributes data from our in-memory copy.
  //
  *Attributes = FvbDevice->FvbAttributes;

  return EFI_SUCCESS;
}


EFI_STATUS
EFIAPI
FwVolBlockSetAttributes (
  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL   *This,
  IN EFI_FVB_ATTRIBUTES                   *Attributes
  )
/*++

Routine Description:
  Modifies the current settings of the firmware volume according to the input parameter.

Arguments:
  This - Calling context
  Attributes - input buffer which contains attributes

Returns:
  EFI_SUCCESS -  The firmware volume attributes were returned.
  EFI_INVALID_PARAMETER  -  The attributes requested are in conflict with the capabilities as
                             declared in the firmware volume header.
  EFI_UNSUPPORTED        -  Not supported.
--*/
{
  return EFI_UNSUPPORTED;
}


EFI_STATUS
EFIAPI
FwVolBlockEraseBlock (
  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL    *This,
  ...
  )
/*++

Routine Description:
  The EraseBlock() function erases one or more blocks as denoted by the 
variable argument list. The entire parameter list of blocks must be verified
prior to erasing any blocks.  If a block is requested that does not exist 
within the associated firmware volume (it has a larger index than the last 
block of the firmware volume), the EraseBlock() function must return
EFI_INVALID_PARAMETER without modifying the contents of the firmware volume.

Arguments:
  This - Calling context
  ...  - Starting LBA followed by Number of Lba to erase. a -1 to terminate
           the list.
    
Returns:
  EFI_SUCCESS   -  The erase request was successfully completed.
  EFI_ACCESS_DENIED   -  The firmware volume is in the WriteDisabled state.
  EFI_DEVICE_ERROR    -  The block device is not functioning correctly and could not be
                         written. The firmware device may have been partially erased.
  EFI_INVALID_PARAMETER  -  One or more of the LBAs listed in the variable argument list do
  EFI_UNSUPPORTED        -  Not supported.
    
--*/
{
  return EFI_UNSUPPORTED;
}


EFI_STATUS
EFIAPI
FwVolBlockReadBlock (
  IN     EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL     *This,
  IN     EFI_LBA                                Lba,
  IN     UINTN                                  Offset,
  IN OUT UINTN                                  *NumBytes,
  IN     UINT8                                  *Buffer
  )
/*++

Routine Description:
  Read the specified number of bytes from the block to the input buffer.

Arguments:
  This          -  Indicates the calling context.
  Lba           -  The starting logical block index to read.
  Offset        -  Offset into the block at which to begin reading.
  NumBytes      -  Pointer to a UINT32. At entry, *NumBytes contains the
                   total size of the buffer. At exit, *NumBytes contains the
                   total number of bytes actually read.
  Buffer        -  Pinter to a caller-allocated buffer that contains the destine
                   for the read.    

Returns:      
  EFI_SUCCESS  -  The firmware volume was read successfully.
  EFI_BAD_BUFFER_SIZE -  The read was attempted across an LBA boundary.
  EFI_ACCESS_DENIED  -  Access denied.
  EFI_DEVICE_ERROR   -  The block device is malfunctioning and could not be read.
--*/
{
  EFI_FW_VOL_BLOCK_DEVICE               *FvbDevice;
  EFI_FIRMWARE_VOLUME_HEADER            *FwVolHeader;
  UINT8                                 *LbaOffset;
  UINTN                                 LbaStart;
  UINTN                                 NumOfBytesRead;
  UINTN                                 LbaIndex;
  
  FvbDevice = FVB_DEVICE_FROM_THIS (This);

  //
  // Check if This FW can be read
  //
  if ((FvbDevice->FvbAttributes & EFI_FVB_READ_STATUS) == 0) {
    return EFI_ACCESS_DENIED;
  }
  
  LbaIndex = (UINTN)Lba;
  if (LbaIndex >= FvbDevice->NumBlocks) {
    //
    // Invalid Lba, read nothing.
    //
    *NumBytes = 0;
    return EFI_BAD_BUFFER_SIZE;
  }
  
  if (Offset > FvbDevice->LbaCache[LbaIndex].Length) {
    //
    // all exceed boundry, read nothing.
    //
    *NumBytes = 0;
    return EFI_BAD_BUFFER_SIZE;
  }
  
  NumOfBytesRead = *NumBytes;
  if (Offset + NumOfBytesRead > FvbDevice->LbaCache[LbaIndex].Length) {
    //
    // partial exceed boundry, read data from current postion to end.
    //
    NumOfBytesRead = FvbDevice->LbaCache[LbaIndex].Length - Offset;
  }
  
  LbaStart = FvbDevice->LbaCache[LbaIndex].Base;
  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)((UINTN)FvbDevice->BaseAddress);
  LbaOffset = (UINT8 *)FwVolHeader + LbaStart + Offset;

  //
  // Perform read operation
  //
  EfiCommonLibCopyMem (Buffer, LbaOffset, NumOfBytesRead);
  
  if (NumOfBytesRead == *NumBytes) {
    return EFI_SUCCESS;
  }
    
  *NumBytes = NumOfBytesRead;
  return EFI_BAD_BUFFER_SIZE;
}
  

EFI_STATUS
EFIAPI
FwVolBlockWriteBlock (
  IN     EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL   *This,
  IN     EFI_LBA                              Lba,
  IN     UINTN                                Offset,
  IN OUT UINTN                                *NumBytes,
  IN     UINT8                                *Buffer
  )
/*++

Routine Description:
  Writes the specified number of bytes from the input buffer to the block.

Arguments:
  This          -  Indicates the calling context.
  Lba           -  The starting logical block index to write to.
  Offset        -  Offset into the block at which to begin writing.
  NumBytes      -  Pointer to a UINT32. At entry, *NumBytes contains the
                   total size of the buffer. At exit, *NumBytes contains the
                   total number of bytes actually written.
  Buffer        -  Pinter to a caller-allocated buffer that contains the source
                   for the write.    

Returns:     
  EFI_SUCCESS  -  The firmware volume was written successfully.
  EFI_BAD_BUFFER_SIZE -  The write was attempted across an LBA boundary. On output,
                         NumBytes contains the total number of bytes actually written.
  EFI_ACCESS_DENIED  -  The firmware volume is in the WriteDisabled state.
  EFI_DEVICE_ERROR   -  The block device is malfunctioning and could not be written.
  EFI_UNSUPPORTED    -  Not supported.
--*/
{
  return EFI_UNSUPPORTED;
}
 

EFI_STATUS
EFIAPI
FwVolBlockGetPhysicalAddress (
  IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *This,
  OUT EFI_PHYSICAL_ADDRESS                        *Address
  )
/*++

Routine Description:
  Get Fvb's base address.

Arguments:
  This          -  Indicates the calling context.
  Address       -  Fvb device base address.

Returns:     
  EFI_SUCCESS  -  Successfully got Fvb's base address.
  EFI_UNSUPPORTED -  Not supported.
--*/
{
  EFI_FW_VOL_BLOCK_DEVICE               *FvbDevice;
  

⌨️ 快捷键说明

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