3 Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Library/FlashDeviceLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/BaseLib.h>
16 #include <Library/UefiBootServicesTableLib.h>
17 #include <Library/UefiRuntimeServicesTableLib.h>
18 #include <Library/BaseMemoryLib.h>
19 #include <Library/UefiRuntimeLib.h>
20 #include <Protocol/SmmBase2.h>
21 #include <Guid/EventGroup.h>
22 #include "SpiChipDefinitions.h"
24 extern UINTN FlashDeviceBase
;
26 extern EFI_SPI_PROTOCOL
*mSpiProtocol
;
30 LibFvbFlashDeviceVirtualAddressChangeNotifyEvent (
35 gRT
->ConvertPointer (0, (VOID
**) &mSpiProtocol
);
36 gRT
->ConvertPointer (0, (VOID
**) &FlashDeviceBase
);
41 The library constructuor.
43 The function does the necessary initialization work for this library
44 instance. Please put all initialization works in it.
46 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
47 @param[in] SystemTable A pointer to the EFI system table.
49 @retval EFI_SUCCESS The function always return EFI_SUCCESS for now.
50 It will ASSERT on error for debug version.
51 @retval EFI_ERROR Please reference LocateProtocol for error code details.
56 LibFvbFlashDeviceSupportInit (
57 IN EFI_HANDLE ImageHandle
,
58 IN EFI_SYSTEM_TABLE
*SystemTable
66 UINT8 SpiNotMatchError
;
67 EFI_SMM_BASE2_PROTOCOL
*SmmBase
;
71 SpiNotMatchError
= 0x00;
74 Status
= gBS
->LocateProtocol (
75 &gEfiSmmBase2ProtocolGuid
,
79 if (!EFI_ERROR(Status
)) {
80 Status
= SmmBase
->InSmm(SmmBase
, &InSmm
);
81 if (EFI_ERROR(Status
)) {
87 Status
= gBS
->LocateProtocol (
90 (VOID
**)&mSpiProtocol
92 ASSERT_EFI_ERROR (Status
);
94 Status
= gBS
->CreateEventEx (
97 LibFvbFlashDeviceVirtualAddressChangeNotifyEvent
,
99 &gEfiEventVirtualAddressChangeGuid
,
102 ASSERT_EFI_ERROR (Status
);
105 Status
= gBS
->LocateProtocol (
106 &gEfiSmmSpiProtocolGuid
,
108 (VOID
**)&mSpiProtocol
110 ASSERT_EFI_ERROR (Status
);
114 for (FlashIndex
= EnumSpiFlashW25Q64
; FlashIndex
< EnumSpiFlashMax
; FlashIndex
++) {
115 Status
= mSpiProtocol
->Init (mSpiProtocol
, &(mInitTable
[FlashIndex
]));
116 if (!EFI_ERROR (Status
)) {
118 // Read Vendor/Device IDs to check if the driver supports the Serial Flash device.
120 Status
= mSpiProtocol
->Execute (
132 if (!EFI_ERROR (Status
)) {
133 if ((SfId
[0] == mInitTable
[FlashIndex
].VendorId
) &&
134 (SfId
[1] == mInitTable
[FlashIndex
].DeviceId0
) &&
135 (SfId
[2] == mInitTable
[FlashIndex
].DeviceId1
)) {
137 // Found a matching SPI device, FlashIndex now contains flash device.
139 DEBUG ((EFI_D_ERROR
, "OK - Found SPI Flash Type in SPI Flash Driver, Device Type ID 0 = 0x%02x!\n", mInitTable
[FlashIndex
].DeviceId0
));
140 DEBUG ((EFI_D_ERROR
, "Device Type ID 1 = 0x%02x!\n", mInitTable
[FlashIndex
].DeviceId1
));
142 if (mInitTable
[FlashIndex
].BiosStartOffset
== (UINTN
) (-1)) {
143 DEBUG ((EFI_D_ERROR
, "ERROR - The size of BIOS image is bigger than SPI Flash device!\n"));
156 DEBUG ((EFI_D_ERROR
, "SPI flash chip VID = 0x%X, DID0 = 0x%X, DID1 = 0x%X\n", SfId
[0], SfId
[1], SfId
[2]));
158 if (FlashIndex
< EnumSpiFlashMax
) {
161 if (SpiReadError
!= 0) {
162 DEBUG ((EFI_D_ERROR
, "ERROR - SPI Read ID execution failed! Error Count = %d\n", SpiReadError
));
165 if (SpiNotMatchError
!= 0) {
166 DEBUG ((EFI_D_ERROR
, "ERROR - No supported SPI flash chip found! Error Count = %d\n", SpiNotMatchError
));
167 DEBUG ((EFI_D_ERROR
, "SPI flash chip VID = 0x%X, DID0 = 0x%X, DID1 = 0x%X\n", SfId
[0], SfId
[1], SfId
[2]));
170 return EFI_UNSUPPORTED
;