Exemple #1
0
/**
  Gets LBA of block and offset by given address.

  This function gets the Logical Block Address (LBA) of firmware
  volume block containing the given address, and the offset of
  address on the block.

  @param[in]  Address    Address which should be contained
                         by returned FVB handle.
  @param[out] Lba        The pointer to LBA for output.
  @param[out] Offset     The pointer to offset for output.

  @retval EFI_SUCCESS    LBA and offset successfully returned.
  @retval EFI_NOT_FOUND  Failed to find FVB handle by address.
  @retval EFI_ABORTED    Failed to find valid LBA and offset.

**/
EFI_STATUS
GetLbaAndOffsetByAddress (
  IN  EFI_PHYSICAL_ADDRESS   Address,
  OUT EFI_LBA                *Lba,
  OUT UINTN                  *Offset
  )
{
  EFI_STATUS                          Status;
  EFI_HANDLE                          FvbHandle;
  EFI_PHYSICAL_ADDRESS                FvbBaseAddress;
  EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL  *Fvb;
  EFI_FIRMWARE_VOLUME_HEADER          *FwVolHeader;
  EFI_FV_BLOCK_MAP_ENTRY              *FvbMapEntry;
  UINT32                              LbaIndex;

  *Lba    = (EFI_LBA) (-1);
  *Offset = 0;

  //
  // Gets firmware volume block handle by given address.
  //
  Status = GetFvbHandleByAddress (Address, &FvbHandle);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = gBS->HandleProtocol (
                  FvbHandle,
                  &gEfiFirmwareVolumeBlockProtocolGuid,
                  (VOID **) &Fvb
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }
  //
  // Get the Base Address of FV
  //
  Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);

  //
  // Get the (LBA, Offset) of Address
  //
  if ((Address >= FvbBaseAddress) && (Address <= (FvbBaseAddress + FwVolHeader->FvLength))) {
    if ((FwVolHeader->FvLength) > (FwVolHeader->HeaderLength)) {
      //
      // BUGBUG: Assume one FV has one type of BlockLength
      //
      FvbMapEntry = &FwVolHeader->BlockMap[0];
      for (LbaIndex = 1; LbaIndex <= FvbMapEntry->NumBlocks; LbaIndex += 1) {
        if (Address < (FvbBaseAddress + FvbMapEntry->Length * LbaIndex)) {
          //
          // Found the (Lba, Offset)
          //
          *Lba    = LbaIndex - 1;
          *Offset = (UINTN) (Address - (FvbBaseAddress + FvbMapEntry->Length * (LbaIndex - 1)));
          return EFI_SUCCESS;
        }
      }
    }
  }

  return EFI_ABORTED;
}
Exemple #2
0
/**
  Writes a buffer to variable storage space.

  This function writes a buffer to variable storage space into firmware
  volume block device. The destination is specified by parameter
  VariableBase. Fault Tolerant Write protocol is used for writing.

  @param[in] VariableBase The base address of the variable to write.
  @param[in] Buffer       Points to the data buffer.
  @param[in] BufferSize   The number of bytes of the data Buffer.

  @retval EFI_SUCCESS     The function completed successfully.
  @retval EFI_NOT_FOUND   Fail to locate Fault Tolerant Write protocol.
  @retval Other           The function could not complete successfully.

**/
EFI_STATUS
FtwVariableSpace (
  IN EFI_PHYSICAL_ADDRESS   VariableBase,
  IN UINT8                  *Buffer,
  IN UINTN                  BufferSize
  )
{
  EFI_STATUS            Status;
  EFI_HANDLE            FvbHandle;
  EFI_LBA               VarLba;
  UINTN                 VarOffset;
  UINT8                 *FtwBuffer;
  UINTN                 FtwBufferSize;
  EFI_FAULT_TOLERANT_WRITE_PROTOCOL  *FtwProtocol;

  //
  // Locate Fault Tolerant Write protocol
  //
  Status = gBS->LocateProtocol (
                  &gEfiFaultTolerantWriteProtocolGuid,
                  NULL,
                  (VOID **) &FtwProtocol
                  );
  if (EFI_ERROR (Status)) {
    return EFI_NOT_FOUND;
  }
  //
  // Gets firmware volume block handle by VariableBase.
  //
  Status = GetFvbHandleByAddress (VariableBase, &FvbHandle);
  if (EFI_ERROR (Status)) {
    return Status;
  }
  //
  // Gets LBA of block and offset by VariableBase.
  //
  Status = GetLbaAndOffsetByAddress (VariableBase, &VarLba, &VarOffset);
  if (EFI_ERROR (Status)) {
    return EFI_ABORTED;
  }
  //
  // Prepare for the variable data
  //
  FtwBufferSize = ((VARIABLE_STORE_HEADER *) ((UINTN) VariableBase))->Size;
  FtwBuffer     = AllocatePool (FtwBufferSize);
  if (FtwBuffer == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  SetMem (FtwBuffer, FtwBufferSize, (UINT8) 0xff);
  CopyMem (FtwBuffer, Buffer, BufferSize);

  //
  // FTW write record
  //
  Status = FtwProtocol->Write (
                          FtwProtocol,
                          VarLba,         // LBA
                          VarOffset,      // Offset
                          FtwBufferSize,  // NumBytes,
                          NULL,
                          FvbHandle,
                          FtwBuffer
                          );

  FreePool (FtwBuffer);
  return Status;
}
Exemple #3
0
EFI_STATUS
FtwVariableSpace (
  IN EFI_PHYSICAL_ADDRESS   VariableBase,
  IN UINT8                  *Buffer,
  IN UINTN                  BufferSize
  )
/*++

Routine Description:
    Write a buffer to Variable space, in the working block.

Arguments:
    FvbHandle        - Indicates a handle to FVB to access variable store
    Buffer           - Point to the input buffer
    BufferSize       - The number of bytes of the input Buffer

Returns:
    EFI_SUCCESS            - The function completed successfully
    EFI_ABORTED            - The function could not complete successfully
    EFI_NOT_FOUND          - Locate FVB protocol by handle fails

--*/
{
  EFI_STATUS            Status;
  EFI_HANDLE            FvbHandle;
  EFI_FTW_LITE_PROTOCOL *FtwLiteProtocol;
  EFI_LBA               VarLba;
  UINTN                 VarOffset;
  UINT8                 *FtwBuffer;
  UINTN                 FtwBufferSize;

  //
  // Locate fault tolerant write protocol
  //
  Status = gBS->LocateProtocol (
                  &gEfiFaultTolerantWriteLiteProtocolGuid,
                  NULL,
                  &FtwLiteProtocol
                  );
  if (EFI_ERROR (Status)) {
    return EFI_NOT_FOUND;
  }
  //
  // Locate Fvb handle by address
  //
  Status = GetFvbHandleByAddress (VariableBase, &FvbHandle);
  if (EFI_ERROR (Status)) {
    return Status;
  }
  //
  // Get LBA and Offset by address
  //
  Status = GetLbaAndOffsetByAddress (VariableBase, &VarLba, &VarOffset);
  if (EFI_ERROR (Status)) {
    return EFI_ABORTED;
  }
  //
  // Prepare for the variable data
  //
  FtwBufferSize = ((VARIABLE_STORE_HEADER *) ((UINTN) VariableBase))->Size;
  Status        = gBS->AllocatePool (EfiRuntimeServicesData, FtwBufferSize, &FtwBuffer);
  if (EFI_ERROR (Status)) {
    return EFI_OUT_OF_RESOURCES;
  }

  EfiSetMem (FtwBuffer, FtwBufferSize, (UINT8) 0xff);
  EfiCopyMem (FtwBuffer, Buffer, BufferSize);

  //
  // FTW write record
  //
  Status = FtwLiteProtocol->Write (
                              FtwLiteProtocol,
                              FvbHandle,
                              VarLba,         // LBA
                              VarOffset,      // Offset
                              &FtwBufferSize, // NumBytes,
                              FtwBuffer
                              );

  gBS->FreePool (FtwBuffer);
  return Status;
}