]>
Commit | Line | Data |
---|---|---|
7a0a32f1 JY |
1 | /** @file\r |
2 | \r | |
3 | Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r | |
4 | \r | |
5 | This program and the accompanying materials are licensed and made available under\r | |
6 | the terms and conditions of the BSD License that accompanies this distribution.\r | |
7 | The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php.\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | \r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | #include <PiDxe.h>\r | |
18 | \r | |
19 | #include <Library/FlashDeviceLib.h>\r | |
20 | #include <Library/DebugLib.h>\r | |
21 | #include <Library/BaseLib.h>\r | |
22 | #include <Library/UefiBootServicesTableLib.h>\r | |
23 | #include <Library/UefiRuntimeServicesTableLib.h>\r | |
24 | #include <Library/BaseMemoryLib.h>\r | |
25 | #include <Library/UefiRuntimeLib.h>\r | |
26 | #include <Protocol/SmmBase2.h>\r | |
27 | #include <Guid/EventGroup.h>\r | |
28 | #include "SpiChipDefinitions.h"\r | |
29 | \r | |
30 | extern UINTN FlashDeviceBase;\r | |
31 | \r | |
32 | extern EFI_SPI_PROTOCOL *mSpiProtocol;\r | |
33 | \r | |
34 | VOID\r | |
35 | EFIAPI\r | |
36 | LibFvbFlashDeviceVirtualAddressChangeNotifyEvent (\r | |
37 | IN EFI_EVENT Event,\r | |
38 | IN VOID *Context\r | |
39 | )\r | |
40 | {\r | |
41 | gRT->ConvertPointer (0, (VOID **) &mSpiProtocol);\r | |
42 | gRT->ConvertPointer (0, (VOID **) &FlashDeviceBase);\r | |
43 | }\r | |
44 | \r | |
45 | \r | |
46 | /**\r | |
47 | The library constructuor.\r | |
48 | \r | |
49 | The function does the necessary initialization work for this library\r | |
50 | instance. Please put all initialization works in it.\r | |
51 | \r | |
52 | @param[in] ImageHandle The firmware allocated handle for the UEFI image.\r | |
53 | @param[in] SystemTable A pointer to the EFI system table.\r | |
54 | \r | |
55 | @retval EFI_SUCCESS The function always return EFI_SUCCESS for now.\r | |
56 | It will ASSERT on error for debug version.\r | |
57 | @retval EFI_ERROR Please reference LocateProtocol for error code details.\r | |
58 | \r | |
59 | **/\r | |
60 | EFI_STATUS\r | |
61 | EFIAPI\r | |
62 | LibFvbFlashDeviceSupportInit (\r | |
63 | IN EFI_HANDLE ImageHandle,\r | |
64 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
65 | )\r | |
66 | {\r | |
67 | EFI_STATUS Status;\r | |
68 | EFI_EVENT Event;\r | |
69 | UINT8 SfId[3];\r | |
70 | UINT8 FlashIndex;\r | |
71 | UINT8 SpiReadError;\r | |
72 | UINT8 SpiNotMatchError;\r | |
73 | EFI_SMM_BASE2_PROTOCOL *SmmBase;\r | |
74 | BOOLEAN InSmm;\r | |
75 | \r | |
76 | SpiReadError = 0x00;\r | |
77 | SpiNotMatchError = 0x00;\r | |
78 | \r | |
79 | InSmm = FALSE;\r | |
80 | Status = gBS->LocateProtocol (\r | |
81 | &gEfiSmmBase2ProtocolGuid,\r | |
82 | NULL,\r | |
83 | (void **)&SmmBase\r | |
84 | );\r | |
85 | if (!EFI_ERROR(Status)) {\r | |
86 | Status = SmmBase->InSmm(SmmBase, &InSmm);\r | |
87 | if (EFI_ERROR(Status)) {\r | |
88 | InSmm = FALSE;\r | |
89 | }\r | |
90 | }\r | |
91 | \r | |
92 | if (!InSmm) {\r | |
93 | Status = gBS->LocateProtocol (\r | |
94 | &gEfiSpiProtocolGuid,\r | |
95 | NULL,\r | |
96 | (VOID **)&mSpiProtocol\r | |
97 | );\r | |
98 | ASSERT_EFI_ERROR (Status);\r | |
99 | \r | |
100 | Status = gBS->CreateEventEx (\r | |
101 | EVT_NOTIFY_SIGNAL,\r | |
102 | TPL_NOTIFY,\r | |
103 | LibFvbFlashDeviceVirtualAddressChangeNotifyEvent,\r | |
104 | NULL,\r | |
105 | &gEfiEventVirtualAddressChangeGuid,\r | |
106 | &Event\r | |
107 | );\r | |
108 | ASSERT_EFI_ERROR (Status);\r | |
109 | \r | |
110 | } else {\r | |
111 | Status = gBS->LocateProtocol (\r | |
112 | &gEfiSmmSpiProtocolGuid,\r | |
113 | NULL,\r | |
114 | (VOID **)&mSpiProtocol\r | |
115 | );\r | |
116 | ASSERT_EFI_ERROR (Status);\r | |
117 | }\r | |
118 | \r | |
119 | \r | |
120 | for (FlashIndex = EnumSpiFlashW25Q64; FlashIndex < EnumSpiFlashMax; FlashIndex++) {\r | |
121 | Status = mSpiProtocol->Init (mSpiProtocol, &(mInitTable[FlashIndex]));\r | |
122 | if (!EFI_ERROR (Status)) {\r | |
123 | //\r | |
124 | // Read Vendor/Device IDs to check if the driver supports the Serial Flash device.\r | |
125 | //\r | |
126 | Status = mSpiProtocol->Execute (\r | |
127 | mSpiProtocol,\r | |
128 | SPI_READ_ID,\r | |
129 | SPI_WREN,\r | |
130 | TRUE,\r | |
131 | FALSE,\r | |
132 | FALSE,\r | |
133 | 0,\r | |
134 | 3,\r | |
135 | SfId,\r | |
136 | EnumSpiRegionAll\r | |
137 | );\r | |
138 | if (!EFI_ERROR (Status)) {\r | |
139 | if ((SfId[0] == mInitTable[FlashIndex].VendorId) &&\r | |
140 | (SfId[1] == mInitTable[FlashIndex].DeviceId0) &&\r | |
141 | (SfId[2] == mInitTable[FlashIndex].DeviceId1)) {\r | |
142 | //\r | |
143 | // Found a matching SPI device, FlashIndex now contains flash device.\r | |
144 | //\r | |
145 | DEBUG ((EFI_D_ERROR, "OK - Found SPI Flash Type in SPI Flash Driver, Device Type ID 0 = 0x%02x!\n", mInitTable[FlashIndex].DeviceId0));\r | |
146 | DEBUG ((EFI_D_ERROR, "Device Type ID 1 = 0x%02x!\n", mInitTable[FlashIndex].DeviceId1));\r | |
147 | \r | |
148 | if (mInitTable[FlashIndex].BiosStartOffset == (UINTN) (-1)) {\r | |
149 | DEBUG ((EFI_D_ERROR, "ERROR - The size of BIOS image is bigger than SPI Flash device!\n"));\r | |
150 | CpuDeadLoop ();\r | |
151 | }\r | |
152 | break;\r | |
153 | } else {\r | |
154 | SpiNotMatchError++;\r | |
155 | }\r | |
156 | } else {\r | |
157 | SpiReadError++;\r | |
158 | }\r | |
159 | }\r | |
160 | }\r | |
161 | \r | |
162 | DEBUG ((EFI_D_ERROR, "SPI flash chip VID = 0x%X, DID0 = 0x%X, DID1 = 0x%X\n", SfId[0], SfId[1], SfId[2]));\r | |
163 | \r | |
164 | if (FlashIndex < EnumSpiFlashMax) {\r | |
165 | return EFI_SUCCESS;\r | |
166 | } else {\r | |
167 | if (SpiReadError != 0) {\r | |
168 | DEBUG ((EFI_D_ERROR, "ERROR - SPI Read ID execution failed! Error Count = %d\n", SpiReadError));\r | |
169 | }\r | |
170 | else {\r | |
171 | if (SpiNotMatchError != 0) {\r | |
172 | DEBUG ((EFI_D_ERROR, "ERROR - No supported SPI flash chip found! Error Count = %d\n", SpiNotMatchError));\r | |
173 | DEBUG ((EFI_D_ERROR, "SPI flash chip VID = 0x%X, DID0 = 0x%X, DID1 = 0x%X\n", SfId[0], SfId[1], SfId[2]));\r | |
174 | }\r | |
175 | }\r | |
176 | return EFI_UNSUPPORTED;\r | |
177 | }\r | |
178 | }\r | |
179 | \r |