Skip to content

Commit

Permalink
Merge pull request tianocore#6 from hzhuang1/fix_block_var
Browse files Browse the repository at this point in the history
Fix block var
  • Loading branch information
hzhuang1 committed Apr 7, 2015
2 parents 766ab4a + cbf165c commit a16e970
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions HisiPkg/HiKeyPkg/Drivers/BlockVariableDxe/BlockVariableDxe.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,29 @@ FvbRead (
EFI_BLOCK_IO_PROTOCOL *BlockIo;
EFI_STATUS Status;
UINTN Bytes;
VOID *DataPtr;

Instance = CR (This, BLOCK_VARIABLE_INSTANCE, FvbProtocol, BLOCK_VARIABLE_SIGNATURE);
BlockIo = Instance->BlockIoProtocol;
Bytes = (Offset + *NumBytes + Instance->Media.BlockSize - 1) / Instance->Media.BlockSize * Instance->Media.BlockSize;
DataPtr = AllocateZeroPool (Bytes);
if (DataPtr == NULL) {
DEBUG ((EFI_D_ERROR, "FvbWrite: failed to allocate buffer.\n"));
return EFI_BUFFER_TOO_SMALL;
}
WriteBackDataCacheRange (DataPtr, Bytes);
InvalidateDataCacheRange (Buffer, *NumBytes);
Status = BlockIo->ReadBlocks (BlockIo, BlockIo->Media->MediaId, Instance->StartLba + Lba,
Bytes, Instance->ShadowBuffer);
Bytes, DataPtr);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "FvbRead StartLba:%x, Lba:%x, Offset:%x, Status:%x\n",
Instance->StartLba, Lba, Offset, Status));
return Status;
goto exit;
}
CopyMem (Buffer, Instance->ShadowBuffer + Offset, *NumBytes);
CopyMem (Buffer, DataPtr + Offset, *NumBytes);
WriteBackDataCacheRange (Buffer, *NumBytes);
exit:
FreePool (DataPtr);
return Status;
}

Expand All @@ -141,29 +151,41 @@ FvbWrite (
EFI_BLOCK_IO_PROTOCOL *BlockIo;
EFI_STATUS Status;
UINTN Bytes;
VOID *DataPtr;

Instance = CR (This, BLOCK_VARIABLE_INSTANCE, FvbProtocol, BLOCK_VARIABLE_SIGNATURE);
BlockIo = Instance->BlockIoProtocol;
Bytes = (Offset + *NumBytes + Instance->Media.BlockSize - 1) / Instance->Media.BlockSize * Instance->Media.BlockSize;
if (*NumBytes < Bytes) {
Status = BlockIo->ReadBlocks (BlockIo, BlockIo->Media->MediaId, Instance->StartLba + Lba,
Bytes, Instance->ShadowBuffer);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "FvbWrite: failed on reading blocks.\n"));
return Status;
}
CopyMem (Instance->ShadowBuffer + Offset, Buffer, *NumBytes);
WriteBackDataCacheRange (Instance->ShadowBuffer, Bytes);
Status = BlockIo->WriteBlocks (BlockIo, BlockIo->Media->MediaId, Instance->StartLba + Lba,
Bytes, Instance->ShadowBuffer);
} else {
Status = BlockIo->WriteBlocks (BlockIo, BlockIo->Media->MediaId, Instance->StartLba + Lba,
Bytes, Buffer);
DataPtr = AllocateZeroPool (Bytes);
if (DataPtr == NULL) {
DEBUG ((EFI_D_ERROR, "FvbWrite: failed to allocate buffer.\n"));
return EFI_BUFFER_TOO_SMALL;
}
WriteBackDataCacheRange (DataPtr, Bytes);
Status = BlockIo->ReadBlocks (BlockIo, BlockIo->Media->MediaId, Instance->StartLba + Lba,
Bytes, DataPtr);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "FvbWrite: failed on reading blocks.\n"));
goto exit;
}
CopyMem (DataPtr + Offset, Buffer, *NumBytes);
WriteBackDataCacheRange (DataPtr, Bytes);
Status = BlockIo->WriteBlocks (BlockIo, BlockIo->Media->MediaId, Instance->StartLba + Lba,
Bytes, DataPtr);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "FvbWrite StartLba:%x, Lba:%x, Offset:%x, Status:%x\n",
Instance->StartLba, Lba, Offset, Status));
}
// Sometimes the variable isn't flushed into block device if it's the last flush operation.
// So flush it again.
Status = BlockIo->WriteBlocks (BlockIo, BlockIo->Media->MediaId, Instance->StartLba + Lba,
Bytes, DataPtr);
if (EFI_ERROR (Status)) {
DEBUG ((EFI_D_ERROR, "FvbWrite StartLba:%x, Lba:%x, Offset:%x, Status:%x\n",
Instance->StartLba, Lba, Offset, Status));
}
exit:
FreePool (DataPtr);
return Status;
}

Expand Down Expand Up @@ -231,7 +253,6 @@ InitNonVolatileVariableStore (
NvStorageData = (UINT8 *) (UINTN) PcdGet32(PcdFlashNvStorageVariableBase);
mMapNvStorageVariableBase = PcdGet32(PcdFlashNvStorageVariableBase);
NvBlockDevicePath = &Instance->DevicePath;
Instance->ShadowBuffer = (VOID *)(NvStorageData + 0x800000);
NvBlockDevicePath = ConvertTextToDevicePath ((CHAR16*)FixedPcdGetPtr (PcdNvStorageVariableBlockDevicePath));
Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &NvBlockDevicePath,
&Instance->Handle);
Expand Down

0 comments on commit a16e970

Please sign in to comment.