]> git.proxmox.com Git - mirror_edk2.git/blob - Vlv2TbltDevicePkg/Library/FlashDeviceLib/FlashDeviceLibDxeRuntimeSmm.c
df395fbc0336a716078d85cf80ac29303895381a
[mirror_edk2.git] / Vlv2TbltDevicePkg / Library / FlashDeviceLib / FlashDeviceLibDxeRuntimeSmm.c
1 /** @file
2
3 Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>
4
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7
8
9 **/
10
11 #include <PiDxe.h>
12
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"
23
24 extern UINTN FlashDeviceBase;
25
26 extern EFI_SPI_PROTOCOL *mSpiProtocol;
27
28 VOID
29 EFIAPI
30 LibFvbFlashDeviceVirtualAddressChangeNotifyEvent (
31 IN EFI_EVENT Event,
32 IN VOID *Context
33 )
34 {
35 gRT->ConvertPointer (0, (VOID **) &mSpiProtocol);
36 gRT->ConvertPointer (0, (VOID **) &FlashDeviceBase);
37 }
38
39
40 /**
41 The library constructuor.
42
43 The function does the necessary initialization work for this library
44 instance. Please put all initialization works in it.
45
46 @param[in] ImageHandle The firmware allocated handle for the UEFI image.
47 @param[in] SystemTable A pointer to the EFI system table.
48
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.
52
53 **/
54 EFI_STATUS
55 EFIAPI
56 LibFvbFlashDeviceSupportInit (
57 IN EFI_HANDLE ImageHandle,
58 IN EFI_SYSTEM_TABLE *SystemTable
59 )
60 {
61 EFI_STATUS Status;
62 EFI_EVENT Event;
63 UINT8 SfId[3];
64 UINT8 FlashIndex;
65 UINT8 SpiReadError;
66 UINT8 SpiNotMatchError;
67 EFI_SMM_BASE2_PROTOCOL *SmmBase;
68 BOOLEAN InSmm;
69
70 SpiReadError = 0x00;
71 SpiNotMatchError = 0x00;
72
73 InSmm = FALSE;
74 Status = gBS->LocateProtocol (
75 &gEfiSmmBase2ProtocolGuid,
76 NULL,
77 (void **)&SmmBase
78 );
79 if (!EFI_ERROR(Status)) {
80 Status = SmmBase->InSmm(SmmBase, &InSmm);
81 if (EFI_ERROR(Status)) {
82 InSmm = FALSE;
83 }
84 }
85
86 if (!InSmm) {
87 Status = gBS->LocateProtocol (
88 &gEfiSpiProtocolGuid,
89 NULL,
90 (VOID **)&mSpiProtocol
91 );
92 ASSERT_EFI_ERROR (Status);
93
94 Status = gBS->CreateEventEx (
95 EVT_NOTIFY_SIGNAL,
96 TPL_NOTIFY,
97 LibFvbFlashDeviceVirtualAddressChangeNotifyEvent,
98 NULL,
99 &gEfiEventVirtualAddressChangeGuid,
100 &Event
101 );
102 ASSERT_EFI_ERROR (Status);
103
104 } else {
105 Status = gBS->LocateProtocol (
106 &gEfiSmmSpiProtocolGuid,
107 NULL,
108 (VOID **)&mSpiProtocol
109 );
110 ASSERT_EFI_ERROR (Status);
111 }
112
113
114 for (FlashIndex = EnumSpiFlashW25Q64; FlashIndex < EnumSpiFlashMax; FlashIndex++) {
115 Status = mSpiProtocol->Init (mSpiProtocol, &(mInitTable[FlashIndex]));
116 if (!EFI_ERROR (Status)) {
117 //
118 // Read Vendor/Device IDs to check if the driver supports the Serial Flash device.
119 //
120 Status = mSpiProtocol->Execute (
121 mSpiProtocol,
122 SPI_READ_ID,
123 SPI_WREN,
124 TRUE,
125 FALSE,
126 FALSE,
127 0,
128 3,
129 SfId,
130 EnumSpiRegionAll
131 );
132 if (!EFI_ERROR (Status)) {
133 if ((SfId[0] == mInitTable[FlashIndex].VendorId) &&
134 (SfId[1] == mInitTable[FlashIndex].DeviceId0) &&
135 (SfId[2] == mInitTable[FlashIndex].DeviceId1)) {
136 //
137 // Found a matching SPI device, FlashIndex now contains flash device.
138 //
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));
141
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"));
144 CpuDeadLoop ();
145 }
146 break;
147 } else {
148 SpiNotMatchError++;
149 }
150 } else {
151 SpiReadError++;
152 }
153 }
154 }
155
156 DEBUG ((EFI_D_ERROR, "SPI flash chip VID = 0x%X, DID0 = 0x%X, DID1 = 0x%X\n", SfId[0], SfId[1], SfId[2]));
157
158 if (FlashIndex < EnumSpiFlashMax) {
159 return EFI_SUCCESS;
160 } else {
161 if (SpiReadError != 0) {
162 DEBUG ((EFI_D_ERROR, "ERROR - SPI Read ID execution failed! Error Count = %d\n", SpiReadError));
163 }
164 else {
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]));
168 }
169 }
170 return EFI_UNSUPPORTED;
171 }
172 }
173