]> git.proxmox.com Git - mirror_edk2.git/blame - QuarkPlatformPkg/Feature/Capsule/Library/PlatformFlashAccessLib/PlatformFlashAccessLibDxe.c
QuarkPlatformPkg/PlatformFlashAccessLib: Add progress API
[mirror_edk2.git] / QuarkPlatformPkg / Feature / Capsule / Library / PlatformFlashAccessLib / PlatformFlashAccessLibDxe.c
CommitLineData
63b9a685
JY
1/** @file\r
2 Platform Flash Access library.\r
3\r
99758ffd 4 Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r
63b9a685
JY
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. 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#include <PiDxe.h>\r
16\r
17#include <Library/BaseLib.h>\r
18#include <Library/BaseMemoryLib.h>\r
19#include <Library/DebugLib.h>\r
20#include <Library/PcdLib.h>\r
21#include <Library/PlatformFlashAccessLib.h>\r
22#include <Library/UefiBootServicesTableLib.h>\r
23#include <Protocol/Spi.h>\r
24\r
25//\r
26// SPI default opcode slots\r
27//\r
28#define SPI_OPCODE_JEDEC_ID_INDEX 0\r
29#define SPI_OPCODE_READ_ID_INDEX 1\r
30#define SPI_OPCODE_WRITE_S_INDEX 2\r
31#define SPI_OPCODE_WRITE_INDEX 3\r
32#define SPI_OPCODE_READ_INDEX 4\r
33#define SPI_OPCODE_ERASE_INDEX 5\r
34#define SPI_OPCODE_READ_S_INDEX 6\r
35#define SPI_OPCODE_CHIP_ERASE_INDEX 7\r
36\r
37#define SPI_ERASE_SECTOR_SIZE SIZE_4KB //This is the chipset requirement\r
38\r
39STATIC EFI_PHYSICAL_ADDRESS mInternalFdAddress;\r
40EFI_SPI_PROTOCOL *mSpiProtocol;\r
41\r
42/**\r
43 Writes specified number of bytes from the input buffer to the address\r
44\r
45 @param[in] WriteAddress The flash address to be written.\r
46 @param[in, out] NumBytes The number of bytes.\r
47 @param[in] Buffer The data buffer to be written.\r
48\r
49 @return The status of flash write.\r
50**/\r
51EFI_STATUS\r
52FlashFdWrite (\r
53 IN UINTN WriteAddress,\r
54 IN OUT UINTN *NumBytes,\r
55 IN UINT8 *Buffer\r
56 )\r
57{\r
58 EFI_STATUS Status;\r
59\r
60 Status = EFI_SUCCESS;\r
61\r
62 Status = mSpiProtocol->Execute (\r
63 mSpiProtocol,\r
64 SPI_OPCODE_WRITE_INDEX, // OpcodeIndex\r
65 0, // PrefixOpcodeIndex\r
66 TRUE, // DataCycle\r
67 TRUE, // Atomic\r
68 TRUE, // ShiftOut\r
69 WriteAddress, // Address\r
70 (UINT32) (*NumBytes), // Data Number\r
71 Buffer,\r
72 EnumSpiRegionBios\r
73 );\r
74 DEBUG((DEBUG_INFO, "FlashFdWrite - 0x%x - %r\n", (UINTN)WriteAddress, Status));\r
75\r
76 AsmWbinvd ();\r
77\r
78 return Status;\r
79}\r
80\r
81/**\r
82 Erase a certain block from address LbaWriteAddress\r
83\r
84 @param[in] WriteAddress The flash address to be erased.\r
85\r
86 @return The status of flash erase.\r
87**/\r
88EFI_STATUS\r
89FlashFdErase (\r
90 IN UINTN WriteAddress\r
91 )\r
92{\r
93 EFI_STATUS Status;\r
94\r
95 Status = mSpiProtocol->Execute (\r
96 mSpiProtocol,\r
97 SPI_OPCODE_ERASE_INDEX, // OpcodeIndex\r
98 0, // PrefixOpcodeIndex\r
99 FALSE, // DataCycle\r
100 TRUE, // Atomic\r
101 FALSE, // ShiftOut\r
102 WriteAddress, // Address\r
103 0, // Data Number\r
104 NULL,\r
105 EnumSpiRegionBios // SPI_REGION_TYPE\r
106 );\r
107 DEBUG((DEBUG_INFO, "FlashFdErase - 0x%x - %r\n", (UINTN)WriteAddress, Status));\r
108\r
109 AsmWbinvd ();\r
110\r
111 return Status;\r
112}\r
113\r
114/**\r
99758ffd
MK
115 Perform flash write operation with progress indicator. The start and end\r
116 completion percentage values are passed into this function. If the requested\r
117 flash write operation is broken up, then completion percentage between the\r
118 start and end values may be passed to the provided Progress function. The\r
119 caller of this function is required to call the Progress function for the\r
120 start and end completion percentage values. This allows the Progress,\r
121 StartPercentage, and EndPercentage parameters to be ignored if the requested\r
122 flash write operation can not be broken up\r
63b9a685
JY
123\r
124 @param[in] FirmwareType The type of firmware.\r
125 @param[in] FlashAddress The address of flash device to be accessed.\r
126 @param[in] FlashAddressType The type of flash device address.\r
127 @param[in] Buffer The pointer to the data buffer.\r
128 @param[in] Length The length of data buffer in bytes.\r
99758ffd
MK
129 @param[in] Progress A function used report the progress of the\r
130 firmware update. This is an optional parameter\r
131 that may be NULL.\r
132 @param[in] StartPercentage The start completion percentage value that may\r
133 be used to report progress during the flash\r
134 write operation.\r
135 @param[in] EndPercentage The end completion percentage value that may\r
136 be used to report progress during the flash\r
137 write operation.\r
63b9a685
JY
138\r
139 @retval EFI_SUCCESS The operation returns successfully.\r
140 @retval EFI_WRITE_PROTECTED The flash device is read only.\r
141 @retval EFI_UNSUPPORTED The flash device access is unsupported.\r
142 @retval EFI_INVALID_PARAMETER The input parameter is not valid.\r
143**/\r
144EFI_STATUS\r
145EFIAPI\r
99758ffd
MK
146PerformFlashWriteWithProgress (\r
147 IN PLATFORM_FIRMWARE_TYPE FirmwareType,\r
148 IN EFI_PHYSICAL_ADDRESS FlashAddress,\r
149 IN FLASH_ADDRESS_TYPE FlashAddressType,\r
150 IN VOID *Buffer,\r
151 IN UINTN Length,\r
152 IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress, OPTIONAL\r
153 IN UINTN StartPercentage,\r
154 IN UINTN EndPercentage\r
63b9a685
JY
155 )\r
156{\r
157 EFI_STATUS Status;\r
158 UINTN SectorNum;\r
159 UINTN Index;\r
160 UINTN NumBytes;\r
161\r
162 DEBUG((DEBUG_INFO, "PerformFlashWrite - 0x%x(%x) - 0x%x\n", (UINTN)FlashAddress, (UINTN)FlashAddressType, Length));\r
163 if (FlashAddressType == FlashAddressTypeAbsoluteAddress) {\r
164 FlashAddress = FlashAddress - mInternalFdAddress;\r
165 }\r
166\r
167 //\r
168 // Erase & Write\r
169 //\r
170 SectorNum = Length / SPI_ERASE_SECTOR_SIZE;\r
171 for (Index = 0; Index < SectorNum; Index++){\r
99758ffd
MK
172 if (Progress != NULL) {\r
173 Progress (StartPercentage + ((Index * (EndPercentage - StartPercentage)) / SectorNum));\r
174 }\r
175\r
63b9a685
JY
176 if (CompareMem(\r
177 (UINT8 *)(UINTN)(FlashAddress + mInternalFdAddress) + Index * SPI_ERASE_SECTOR_SIZE,\r
178 (UINT8 *)Buffer + Index * SPI_ERASE_SECTOR_SIZE,\r
179 SPI_ERASE_SECTOR_SIZE) == 0) {\r
180 DEBUG((DEBUG_INFO, "Sector - 0x%x - skip\n", Index));\r
181 continue;\r
182 }\r
183 DEBUG((DEBUG_INFO, "Sector - 0x%x - update...\n", Index));\r
184\r
185 Status = FlashFdErase (\r
186 (UINTN)FlashAddress + Index * SPI_ERASE_SECTOR_SIZE\r
187 );\r
188 if (Status != EFI_SUCCESS){\r
189 break;\r
190 }\r
191 NumBytes = SPI_ERASE_SECTOR_SIZE;\r
192 Status = FlashFdWrite (\r
193 (UINTN)FlashAddress + Index * SPI_ERASE_SECTOR_SIZE,\r
194 &NumBytes,\r
195 (UINT8 *)Buffer + Index * SPI_ERASE_SECTOR_SIZE\r
196 );\r
197 if (Status != EFI_SUCCESS){\r
198 break;\r
199 }\r
200 }\r
99758ffd
MK
201 if (Progress != NULL) {\r
202 Progress (EndPercentage);\r
203 }\r
63b9a685
JY
204\r
205 return EFI_SUCCESS;\r
206}\r
207\r
99758ffd
MK
208/**\r
209 Perform flash write operation.\r
210\r
211 @param[in] FirmwareType The type of firmware.\r
212 @param[in] FlashAddress The address of flash device to be accessed.\r
213 @param[in] FlashAddressType The type of flash device address.\r
214 @param[in] Buffer The pointer to the data buffer.\r
215 @param[in] Length The length of data buffer in bytes.\r
216\r
217 @retval EFI_SUCCESS The operation returns successfully.\r
218 @retval EFI_WRITE_PROTECTED The flash device is read only.\r
219 @retval EFI_UNSUPPORTED The flash device access is unsupported.\r
220 @retval EFI_INVALID_PARAMETER The input parameter is not valid.\r
221**/\r
222EFI_STATUS\r
223EFIAPI\r
224PerformFlashWrite (\r
225 IN PLATFORM_FIRMWARE_TYPE FirmwareType,\r
226 IN EFI_PHYSICAL_ADDRESS FlashAddress,\r
227 IN FLASH_ADDRESS_TYPE FlashAddressType,\r
228 IN VOID *Buffer,\r
229 IN UINTN Length\r
230 )\r
231{\r
232 return PerformFlashWriteWithProgress (\r
233 FirmwareType,\r
234 FlashAddress,\r
235 FlashAddressType,\r
236 Buffer,\r
237 Length,\r
238 NULL,\r
239 0,\r
240 0\r
241 );\r
242}\r
243\r
63b9a685
JY
244/**\r
245 Platform Flash Access Lib Constructor.\r
246\r
247 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
248 @param[in] SystemTable A pointer to the EFI System Table.\r
249\r
250 @retval EFI_SUCCESS Constructor returns successfully.\r
251**/\r
252EFI_STATUS\r
253EFIAPI\r
254PerformFlashAccessLibConstructor (\r
255 IN EFI_HANDLE ImageHandle,\r
256 IN EFI_SYSTEM_TABLE *SystemTable\r
257 )\r
258{\r
259 EFI_STATUS Status;\r
260\r
261 mInternalFdAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)PcdGet32(PcdFlashAreaBaseAddress);\r
262 DEBUG((DEBUG_INFO, "PcdFlashAreaBaseAddress - 0x%x\n", mInternalFdAddress));\r
263\r
264 Status = gBS->LocateProtocol(&gEfiSpiProtocolGuid, NULL, (VOID **)&mSpiProtocol);\r
265 ASSERT_EFI_ERROR(Status);\r
266\r
267 return EFI_SUCCESS;\r
268}\r