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 extern UINTN FlashDeviceBase
;
32 extern EFI_SPI_PROTOCOL
*mSpiProtocol
;
36 LibFvbFlashDeviceVirtualAddressChangeNotifyEvent (
41 gRT
->ConvertPointer (0, (VOID
**) &mSpiProtocol
);
42 gRT
->ConvertPointer (0, (VOID
**) &FlashDeviceBase
);
47 The library constructuor.
49 The function does the necessary initialization work for this library
50 instance. Please put all initialization works in it.
52 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
53 @param[in] SystemTable A pointer to the EFI system table.
55 @retval EFI_SUCCESS The function always return EFI_SUCCESS for now.
56 It will ASSERT on error for debug version.
57 @retval EFI_ERROR Please reference LocateProtocol for error code details.
62 LibFvbFlashDeviceSupportInit (
63 IN EFI_HANDLE ImageHandle
,
64 IN EFI_SYSTEM_TABLE
*SystemTable
72 UINT8 SpiNotMatchError
;
73 EFI_SMM_BASE2_PROTOCOL
*SmmBase
;
77 SpiNotMatchError
= 0x00;
80 Status
= gBS
->LocateProtocol (
81 &gEfiSmmBase2ProtocolGuid
,
85 if (!EFI_ERROR(Status
)) {
86 Status
= SmmBase
->InSmm(SmmBase
, &InSmm
);
87 if (EFI_ERROR(Status
)) {
93 Status
= gBS
->LocateProtocol (
96 (VOID
**)&mSpiProtocol
98 ASSERT_EFI_ERROR (Status
);
100 Status
= gBS
->CreateEventEx (
103 LibFvbFlashDeviceVirtualAddressChangeNotifyEvent
,
105 &gEfiEventVirtualAddressChangeGuid
,
108 ASSERT_EFI_ERROR (Status
);
111 Status
= gBS
->LocateProtocol (
112 &gEfiSmmSpiProtocolGuid
,
114 (VOID
**)&mSpiProtocol
116 ASSERT_EFI_ERROR (Status
);
120 for (FlashIndex
= EnumSpiFlashW25Q64
; FlashIndex
< EnumSpiFlashMax
; FlashIndex
++) {
121 Status
= mSpiProtocol
->Init (mSpiProtocol
, &(mInitTable
[FlashIndex
]));
122 if (!EFI_ERROR (Status
)) {
124 // Read Vendor/Device IDs to check if the driver supports the Serial Flash device.
126 Status
= mSpiProtocol
->Execute (
138 if (!EFI_ERROR (Status
)) {
139 if ((SfId
[0] == mInitTable
[FlashIndex
].VendorId
) &&
140 (SfId
[1] == mInitTable
[FlashIndex
].DeviceId0
) &&
141 (SfId
[2] == mInitTable
[FlashIndex
].DeviceId1
)) {
143 // Found a matching SPI device, FlashIndex now contains flash device.
145 DEBUG ((EFI_D_ERROR
, "OK - Found SPI Flash Type in SPI Flash Driver, Device Type ID 0 = 0x%02x!\n", mInitTable
[FlashIndex
].DeviceId0
));
146 DEBUG ((EFI_D_ERROR
, "Device Type ID 1 = 0x%02x!\n", mInitTable
[FlashIndex
].DeviceId1
));
148 if (mInitTable
[FlashIndex
].BiosStartOffset
== (UINTN
) (-1)) {
149 DEBUG ((EFI_D_ERROR
, "ERROR - The size of BIOS image is bigger than SPI Flash device!\n"));
162 DEBUG ((EFI_D_ERROR
, "SPI flash chip VID = 0x%X, DID0 = 0x%X, DID1 = 0x%X\n", SfId
[0], SfId
[1], SfId
[2]));
164 if (FlashIndex
< EnumSpiFlashMax
) {
167 if (SpiReadError
!= 0) {
168 DEBUG ((EFI_D_ERROR
, "ERROR - SPI Read ID execution failed! Error Count = %d\n", SpiReadError
));
171 if (SpiNotMatchError
!= 0) {
172 DEBUG ((EFI_D_ERROR
, "ERROR - No supported SPI flash chip found! Error Count = %d\n", SpiNotMatchError
));
173 DEBUG ((EFI_D_ERROR
, "SPI flash chip VID = 0x%X, DID0 = 0x%X, DID1 = 0x%X\n", SfId
[0], SfId
[1], SfId
[2]));
176 return EFI_UNSUPPORTED
;