3 Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include <Library/FlashDeviceLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/BaseLib.h>
22 #include <Library/BaseMemoryLib.h>
23 #include <Guid/EventGroup.h>
24 #include <Library/SpiFlash.H>
26 #define FLASH_SIZE 0x400000
28 #define FLASH_DEVICE_BASE_ADDRESS (0xFFFFFFFF-FLASH_SIZE+1)
29 UINTN FlashDeviceBase
= FLASH_DEVICE_BASE_ADDRESS
;
31 EFI_SPI_PROTOCOL
*mSpiProtocol
= NULL
;
39 EFI_STATUS Status
= EFI_SUCCESS
;
43 SpiAddress
= (UINT32
)(UINTN
)(BaseAddress
) - (UINT32
)FlashDeviceBase
;
44 SectorSize
= SECTOR_SIZE_4KB
;
45 while ( (NumBytes
> 0) && (NumBytes
<= MAX_FWH_SIZE
) ) {
46 Status
= mSpiProtocol
->Execute (
58 if (EFI_ERROR (Status
)) {
61 SpiAddress
+= SectorSize
;
62 NumBytes
-= SectorSize
;
75 EFI_STATUS Status
= EFI_SUCCESS
;
79 SpiAddress
= (UINT32
)(UINTN
)(BaseAddress
) - (UINT32
)FlashDeviceBase
;
80 SectorSize
= SECTOR_SIZE_64KB
;
81 while ( (NumBytes
> 0) && (NumBytes
<= MAX_FWH_SIZE
) ) {
82 Status
= mSpiProtocol
->Execute (
94 if (EFI_ERROR (Status
)) {
97 SpiAddress
+= SectorSize
;
98 NumBytes
-= SectorSize
;
114 UINT32 NumBytes
= (UINT32
)Length
;
118 SpiAddress
= (UINT32
)(UINTN
)(DstBufferPtr
) - (UINT32
)FlashDeviceBase
;
119 Status
= mSpiProtocol
->Execute (
135 Read the Serial Flash Status Registers.
137 @param SpiStatus Pointer to a caller-allocated UINT8. On successful return, it contains the
138 status data read from the Serial Flash Status Register.
141 @retval EFI_SUCCESS Operation success, status is returned in SpiStatus.
142 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and the operation failed.
152 Status
= mSpiProtocol
->Execute (
169 IN UINT8
*BaseAddress
,
185 // Always disable block protection to workaround tool issue.
186 // Feature may be re-enabled in a future bios.
189 Status
= mSpiProtocol
->Execute (
201 if (EFI_ERROR (Status
)) {
205 Status
= ReadStatusRegister (&SpiStatus
);
206 if (EFI_ERROR (Status
)) {
210 if ((SpiStatus
& SpiData
) != SpiData
) {
211 Status
= EFI_DEVICE_ERROR
;
219 Read NumBytes bytes of data from the address specified by
220 PAddress into Buffer.
222 @param[in] PAddress The starting physical address of the read.
223 @param[in,out] NumBytes On input, the number of bytes to read. On output, the number
224 of bytes actually read.
225 @param[out] Buffer The destination data buffer for the read.
227 @retval EFI_SUCCESS. Opertion is successful.
228 @retval EFI_DEVICE_ERROR If there is any device errors.
233 LibFvbFlashDeviceRead (
235 IN OUT UINTN
*NumBytes
,
239 CopyMem(Buffer
, (VOID
*)PAddress
, *NumBytes
);
245 Write NumBytes bytes of data from Buffer to the address specified by
248 @param[in] PAddress The starting physical address of the write.
249 @param[in,out] NumBytes On input, the number of bytes to write. On output,
250 the actual number of bytes written.
251 @param[in] Buffer The source data buffer for the write.
253 @retval EFI_SUCCESS. Opertion is successful.
254 @retval EFI_DEVICE_ERROR If there is any device errors.
259 LibFvbFlashDeviceWrite (
261 IN OUT UINTN
*NumBytes
,
266 Status
= SpiFlashWrite((UINT8
*)PAddress
, Buffer
, *NumBytes
);
272 Erase the block staring at PAddress.
274 @param[in] PAddress The starting physical address of the block to be erased.
275 This library assume that caller garantee that the PAddress
276 is at the starting address of this block.
277 @param[in] LbaLength The length of the logical block to be erased.
279 @retval EFI_SUCCESS. Opertion is successful.
280 @retval EFI_DEVICE_ERROR If there is any device errors.
285 LibFvbFlashDeviceBlockErase (
291 Status
= SpiFlashBlockErase((UINT8
*)PAddress
, LbaLength
);
298 Lock or unlock the block staring at PAddress.
300 @param[in] PAddress The starting physical address of region to be (un)locked.
301 @param[in] LbaLength The length of the logical block to be erased.
302 @param[in] Lock TRUE to lock. FALSE to unlock.
304 @retval EFI_SUCCESS. Opertion is successful.
305 @retval EFI_DEVICE_ERROR If there is any device errors.
310 LibFvbFlashDeviceBlockLock (
318 Status
= SpiFlashLock((UINT8
*)PAddress
, LbaLength
, Lock
);