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/UefiBootServicesTableLib.h>
23 #include <Library/UefiRuntimeServicesTableLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/UefiRuntimeLib.h>
26 #include <Protocol/SmmBase2.h>
27 #include <Guid/EventGroup.h>
28 #include "SpiChipDefinitions.h"
30 UINTN FlashDeviceBase
= FLASH_DEVICE_BASE_ADDRESS
;
32 EFI_SPI_PROTOCOL
*mSpiProtocol
= NULL
;
40 EFI_STATUS Status
= EFI_SUCCESS
;
44 SpiAddress
= (UINT32
)(UINTN
)(BaseAddress
) - (UINT32
)FlashDeviceBase
;
45 SectorSize
= SECTOR_SIZE_4KB
;
46 while ( (NumBytes
> 0) && (NumBytes
<= MAX_FWH_SIZE
) ) {
47 Status
= mSpiProtocol
->Execute (
59 if (EFI_ERROR (Status
)) {
62 SpiAddress
+= SectorSize
;
63 NumBytes
-= SectorSize
;
76 EFI_STATUS Status
= EFI_SUCCESS
;
80 SpiAddress
= (UINT32
)(UINTN
)(BaseAddress
) - (UINT32
)FlashDeviceBase
;
81 SectorSize
= SECTOR_SIZE_4KB
;
82 while ( (NumBytes
> 0) && (NumBytes
<= MAX_FWH_SIZE
) ) {
83 Status
= mSpiProtocol
->Execute (
95 if (EFI_ERROR (Status
)) {
98 SpiAddress
+= SectorSize
;
99 NumBytes
-= SectorSize
;
115 UINT32 NumBytes
= (UINT32
)Length
;
119 SpiAddress
= (UINT32
)(UINTN
)(DstBufferPtr
) - (UINT32
)FlashDeviceBase
;
120 Status
= mSpiProtocol
->Execute (
136 Read the Serial Flash Status Registers.
138 @param SpiStatus Pointer to a caller-allocated UINT8. On successful return, it contains the
139 status data read from the Serial Flash Status Register.
142 @retval EFI_SUCCESS Operation success, status is returned in SpiStatus.
143 @retval EFI_DEVICE_ERROR The block device is not functioning correctly and the operation failed.
153 Status
= mSpiProtocol
->Execute (
170 IN UINT8
*BaseAddress
,
186 // Always disable block protection to workaround tool issue.
187 // Feature may be re-enabled in a future bios.
190 Status
= mSpiProtocol
->Execute (
202 if (EFI_ERROR (Status
)) {
206 Status
= ReadStatusRegister (&SpiStatus
);
207 if (EFI_ERROR (Status
)) {
211 if ((SpiStatus
& SpiData
) != SpiData
) {
212 Status
= EFI_DEVICE_ERROR
;
220 Read NumBytes bytes of data from the address specified by
221 PAddress into Buffer.
223 @param[in] PAddress The starting physical address of the read.
224 @param[in,out] NumBytes On input, the number of bytes to read. On output, the number
225 of bytes actually read.
226 @param[out] Buffer The destination data buffer for the read.
228 @retval EFI_SUCCESS. Opertion is successful.
229 @retval EFI_DEVICE_ERROR If there is any device errors.
234 LibFvbFlashDeviceRead (
236 IN OUT UINTN
*NumBytes
,
240 CopyMem(Buffer
, (VOID
*)PAddress
, *NumBytes
);
246 Write NumBytes bytes of data from Buffer to the address specified by
249 @param[in] PAddress The starting physical address of the write.
250 @param[in,out] NumBytes On input, the number of bytes to write. On output,
251 the actual number of bytes written.
252 @param[in] Buffer The source data buffer for the write.
254 @retval EFI_SUCCESS. Opertion is successful.
255 @retval EFI_DEVICE_ERROR If there is any device errors.
260 LibFvbFlashDeviceWrite (
262 IN OUT UINTN
*NumBytes
,
267 Status
= SpiFlashWrite((UINT8
*)PAddress
, Buffer
, *NumBytes
);
273 Erase the block staring at PAddress.
275 @param[in] PAddress The starting physical address of the block to be erased.
276 This library assume that caller garantee that the PAddress
277 is at the starting address of this block.
278 @param[in] LbaLength The length of the logical block to be erased.
280 @retval EFI_SUCCESS. Opertion is successful.
281 @retval EFI_DEVICE_ERROR If there is any device errors.
286 LibFvbFlashDeviceBlockErase (
292 Status
= SpiFlashBlockErase((UINT8
*)PAddress
, LbaLength
);
299 Lock or unlock the block staring at PAddress.
301 @param[in] PAddress The starting physical address of region to be (un)locked.
302 @param[in] LbaLength The length of the logical block to be erased.
303 @param[in] Lock TRUE to lock. FALSE to unlock.
305 @retval EFI_SUCCESS. Opertion is successful.
306 @retval EFI_DEVICE_ERROR If there is any device errors.
311 LibFvbFlashDeviceBlockLock (
319 Status
= SpiFlashLock((UINT8
*)PAddress
, LbaLength
, Lock
);
325 LibFvbFlashDeviceVirtualAddressChangeNotifyEvent (
330 gRT
->ConvertPointer (0, (VOID
**) &mSpiProtocol
);
331 gRT
->ConvertPointer (0, (VOID
**) &FlashDeviceBase
);
336 The library constructuor.
338 The function does the necessary initialization work for this library
339 instance. Please put all initialization works in it.
341 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
342 @param[in] SystemTable A pointer to the EFI system table.
344 @retval EFI_SUCCESS The function always return EFI_SUCCESS for now.
345 It will ASSERT on error for debug version.
346 @retval EFI_ERROR Please reference LocateProtocol for error code details.
351 LibFvbFlashDeviceSupportInit (
352 IN EFI_HANDLE ImageHandle
,
353 IN EFI_SYSTEM_TABLE
*SystemTable
361 UINT8 SpiNotMatchError
;
362 EFI_SMM_BASE2_PROTOCOL
*SmmBase
;
366 SpiNotMatchError
= 0x00;
369 Status
= gBS
->LocateProtocol (
370 &gEfiSmmBase2ProtocolGuid
,
374 if (!EFI_ERROR(Status
)) {
375 Status
= SmmBase
->InSmm(SmmBase
, &InSmm
);
376 if (EFI_ERROR(Status
)) {
382 Status
= gBS
->LocateProtocol (
383 &gEfiSpiProtocolGuid
,
385 (VOID
**)&mSpiProtocol
387 ASSERT_EFI_ERROR (Status
);
389 Status
= gBS
->CreateEventEx (
392 LibFvbFlashDeviceVirtualAddressChangeNotifyEvent
,
394 &gEfiEventVirtualAddressChangeGuid
,
397 ASSERT_EFI_ERROR (Status
);
399 Status
= gBS
->LocateProtocol (
400 &gEfiSmmSpiProtocolGuid
,
402 (VOID
**)&mSpiProtocol
404 ASSERT_EFI_ERROR (Status
);
408 for (FlashIndex
= EnumSpiFlashW25Q64
; FlashIndex
< EnumSpiFlashMax
; FlashIndex
++) {
409 Status
= mSpiProtocol
->Init (mSpiProtocol
, &(mInitTable
[FlashIndex
]));
410 if (!EFI_ERROR (Status
)) {
412 // Read Vendor/Device IDs to check if the driver supports the Serial Flash device.
414 Status
= mSpiProtocol
->Execute (
426 if (!EFI_ERROR (Status
)) {
427 if ((SfId
[0] == mInitTable
[FlashIndex
].VendorId
) &&
428 (SfId
[1] == mInitTable
[FlashIndex
].DeviceId0
) &&
429 (SfId
[2] == mInitTable
[FlashIndex
].DeviceId1
)) {
431 // Found a matching SPI device, FlashIndex now contains flash device.
433 DEBUG ((DEBUG_ERROR
, "OK - Found SPI Flash Type in SPI Flash Driver, Device Type ID 0 = 0x%02x!\n", mInitTable
[FlashIndex
].DeviceId0
));
434 DEBUG ((DEBUG_ERROR
, "Device Type ID 1 = 0x%02x!\n", mInitTable
[FlashIndex
].DeviceId1
));
436 if (mInitTable
[FlashIndex
].BiosStartOffset
== (UINTN
) (-1)) {
437 DEBUG ((DEBUG_ERROR
, "ERROR - The size of BIOS image is bigger than SPI Flash device!\n"));
450 DEBUG ((DEBUG_ERROR
, "SPI flash chip VID = 0x%X, DID0 = 0x%X, DID1 = 0x%X\n", SfId
[0], SfId
[1], SfId
[2]));
452 if (FlashIndex
< EnumSpiFlashMax
) {
455 if (SpiReadError
!= 0) {
456 DEBUG ((DEBUG_ERROR
, "ERROR - SPI Read ID execution failed! Error Count = %d\n", SpiReadError
));
459 if (SpiNotMatchError
!= 0) {
460 DEBUG ((DEBUG_ERROR
, "ERROR - No supported SPI flash chip found! Error Count = %d\n", SpiNotMatchError
));
461 DEBUG ((DEBUG_ERROR
, "SPI flash chip VID = 0x%X, DID0 = 0x%X, DID1 = 0x%X\n", SfId
[0], SfId
[1], SfId
[2]));
464 return EFI_UNSUPPORTED
;