]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleService.c
MdeModulePkg/CapsuleRuntimeDxe: Introduce PCD to control this feature.
[mirror_edk2.git] / MdeModulePkg / Universal / CapsuleRuntimeDxe / CapsuleService.c
CommitLineData
13d40edd 1/** @file\r
58612f24 2 Capsule Runtime Driver produces two UEFI capsule runtime services.\r
5d69642d 3 (UpdateCapsule, QueryCapsuleCapabilities)\r
d1102dba 4 It installs the Capsule Architectural Protocol defined in PI1.0a to signify\r
109e9a61 5 the capsule runtime services are ready.\r
74fea867 6\r
4ef0b0ac 7Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
9d510e61 8SPDX-License-Identifier: BSD-2-Clause-Patent\r
74fea867 9\r
13d40edd 10**/\r
74fea867 11\r
e077a93d
AB
12#include "CapsuleService.h"\r
13\r
3f42faa7 14//\r
15// Handle for the installation of Capsule Architecture Protocol.\r
16//\r
17EFI_HANDLE mNewHandle = NULL;\r
18\r
f03ccf59 19//\r
20// The times of calling UpdateCapsule ()\r
21//\r
22UINTN mTimes = 0;\r
23\r
f67eb9d8
SZ
24UINT32 mMaxSizePopulateCapsule = 0;\r
25UINT32 mMaxSizeNonPopulateCapsule = 0;\r
26\r
5d69642d
LG
27/**\r
28 Passes capsules to the firmware with both virtual and physical mapping. Depending on the intended\r
29 consumption, the firmware may process the capsule immediately. If the payload should persist\r
30 across a system reset, the reset value returned from EFI_QueryCapsuleCapabilities must\r
31 be passed into ResetSystem() and will cause the capsule to be processed by the firmware as\r
32 part of the reset process.\r
33\r
34 @param CapsuleHeaderArray Virtual pointer to an array of virtual pointers to the capsules\r
35 being passed into update capsule.\r
36 @param CapsuleCount Number of pointers to EFI_CAPSULE_HEADER in\r
37 CaspuleHeaderArray.\r
38 @param ScatterGatherList Physical pointer to a set of\r
39 EFI_CAPSULE_BLOCK_DESCRIPTOR that describes the\r
40 location in physical memory of a set of capsules.\r
41\r
42 @retval EFI_SUCCESS Valid capsule was passed. If\r
43 CAPSULE_FLAGS_PERSIT_ACROSS_RESET is not set, the\r
44 capsule has been successfully processed by the firmware.\r
45 @retval EFI_DEVICE_ERROR The capsule update was started, but failed due to a device error.\r
3f42faa7 46 @retval EFI_INVALID_PARAMETER CapsuleSize is NULL, or an incompatible set of flags were\r
47 set in the capsule header.\r
48 @retval EFI_INVALID_PARAMETER CapsuleCount is Zero.\r
49 @retval EFI_INVALID_PARAMETER For across reset capsule image, ScatterGatherList is NULL.\r
5d69642d 50 @retval EFI_UNSUPPORTED CapsuleImage is not recognized by the firmware.\r
d1102dba
LG
51 @retval EFI_OUT_OF_RESOURCES When ExitBootServices() has been previously called this error indicates the capsule\r
52 is compatible with this platform but is not capable of being submitted or processed\r
ad3f3656 53 in runtime. The caller may resubmit the capsule prior to ExitBootServices().\r
d1102dba 54 @retval EFI_OUT_OF_RESOURCES When ExitBootServices() has not been previously called then this error indicates\r
ad3f3656 55 the capsule is compatible with this platform but there are insufficient resources to process.\r
5d69642d
LG
56\r
57**/\r
74fea867 58EFI_STATUS\r
59EFIAPI\r
60UpdateCapsule (\r
61 IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,\r
62 IN UINTN CapsuleCount,\r
63 IN EFI_PHYSICAL_ADDRESS ScatterGatherList OPTIONAL\r
64 )\r
74fea867 65{\r
74fea867 66 UINTN ArrayNumber;\r
74fea867 67 EFI_STATUS Status;\r
74fea867 68 EFI_CAPSULE_HEADER *CapsuleHeader;\r
409d47b4 69 BOOLEAN NeedReset;\r
3f42faa7 70 BOOLEAN InitiateReset;\r
f03ccf59 71 CHAR16 CapsuleVarName[30];\r
d1102dba
LG
72 CHAR16 *TempVarName;\r
73\r
4ef0b0ac
WX
74 //\r
75 // Check if platform support Capsule In RAM or not.\r
76 // Platform could choose to drop CapsulePei/CapsuleX64 and do not support Capsule In RAM.\r
77 //\r
78 if (!PcdGetBool(PcdCapsuleInRamSupport)) {\r
79 return EFI_UNSUPPORTED;\r
80 }\r
81\r
5d69642d
LG
82 //\r
83 // Capsule Count can't be less than one.\r
84 //\r
74fea867 85 if (CapsuleCount < 1) {\r
86 return EFI_INVALID_PARAMETER;\r
87 }\r
3f42faa7 88\r
f03ccf59 89 NeedReset = FALSE;\r
90 InitiateReset = FALSE;\r
91 CapsuleHeader = NULL;\r
92 CapsuleVarName[0] = 0;\r
74fea867 93\r
94 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
d12f75fe
LG
95 //\r
96 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have\r
97 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.\r
98 //\r
74fea867 99 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
100 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {\r
101 return EFI_INVALID_PARAMETER;\r
102 }\r
d12f75fe 103 //\r
3f42faa7 104 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have\r
105 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.\r
106 //\r
107 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {\r
108 return EFI_INVALID_PARAMETER;\r
109 }\r
566771b0 110\r
111 //\r
d1102dba 112 // Check FMP capsule flag\r
566771b0 113 //\r
114 if (CompareGuid(&CapsuleHeader->CapsuleGuid, &gEfiFmpCapsuleGuid)\r
115 && (CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0 ) {\r
116 return EFI_INVALID_PARAMETER;\r
117 }\r
118\r
3f42faa7 119 //\r
d1102dba 120 // Check Capsule image without populate flag by firmware support capsule function\r
6ee65722 121 //\r
566771b0 122 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {\r
123 Status = SupportCapsuleImage (CapsuleHeader);\r
124 if (EFI_ERROR(Status)) {\r
125 return Status;\r
126 }\r
6ee65722 127 }\r
74fea867 128 }\r
129\r
d12f75fe 130 //\r
3f42faa7 131 // Walk through all capsules, record whether there is a capsule needs reset\r
132 // or initiate reset. And then process capsules which has no reset flag directly.\r
5d69642d 133 //\r
409d47b4
LG
134 for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {\r
135 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
d12f75fe 136 //\r
409d47b4
LG
137 // Here should be in the boot-time for non-reset capsule image\r
138 // Platform specific update for the non-reset capsule image.\r
d12f75fe 139 //\r
409d47b4 140 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) == 0) {\r
d1102dba 141 if (EfiAtRuntime ()) {\r
ad3f3656 142 Status = EFI_OUT_OF_RESOURCES;\r
409d47b4
LG
143 } else {\r
144 Status = ProcessCapsuleImage(CapsuleHeader);\r
145 }\r
146 if (EFI_ERROR(Status)) {\r
d12f75fe 147 return Status;\r
74fea867 148 }\r
409d47b4
LG
149 } else {\r
150 NeedReset = TRUE;\r
3f42faa7 151 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_INITIATE_RESET) != 0) {\r
152 InitiateReset = TRUE;\r
153 }\r
74fea867 154 }\r
74fea867 155 }\r
d1102dba 156\r
409d47b4
LG
157 //\r
158 // After launching all capsules who has no reset flag, if no more capsules claims\r
159 // for a system reset just return.\r
160 //\r
161 if (!NeedReset) {\r
162 return EFI_SUCCESS;\r
163 }\r
74fea867 164\r
165 //\r
409d47b4 166 // ScatterGatherList is only referenced if the capsules are defined to persist across\r
d1102dba 167 // system reset.\r
74fea867 168 //\r
409d47b4
LG
169 if (ScatterGatherList == (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {\r
170 return EFI_INVALID_PARAMETER;\r
74fea867 171 }\r
172\r
173 //\r
409d47b4 174 // Check if the platform supports update capsule across a system reset\r
74fea867 175 //\r
e077a93d 176 if (!IsPersistAcrossResetCapsuleSupported ()) {\r
409d47b4 177 return EFI_UNSUPPORTED;\r
74fea867 178 }\r
179\r
e077a93d
AB
180 CapsuleCacheWriteBack (ScatterGatherList);\r
181\r
f03ccf59 182 //\r
183 // Construct variable name CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...\r
184 // if user calls UpdateCapsule multiple times.\r
185 //\r
931d4d82 186 StrCpyS (CapsuleVarName, sizeof(CapsuleVarName)/sizeof(CHAR16), EFI_CAPSULE_VARIABLE_NAME);\r
f03ccf59 187 TempVarName = CapsuleVarName + StrLen (CapsuleVarName);\r
188 if (mTimes > 0) {\r
9f4048f7
HW
189 UnicodeValueToStringS (\r
190 TempVarName,\r
191 sizeof (CapsuleVarName) - ((UINTN)TempVarName - (UINTN)CapsuleVarName),\r
192 0,\r
193 mTimes,\r
194 0\r
195 );\r
f03ccf59 196 }\r
197\r
409d47b4
LG
198 //\r
199 // ScatterGatherList is only referenced if the capsules are defined to persist across\r
d1102dba 200 // system reset. Set its value into NV storage to let pre-boot driver to pick it up\r
409d47b4
LG
201 // after coming through a system reset.\r
202 //\r
b3b1a4cf 203 Status = EfiSetVariable (\r
f03ccf59 204 CapsuleVarName,\r
b3b1a4cf 205 &gEfiCapsuleVendorGuid,\r
206 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
207 sizeof (UINTN),\r
208 (VOID *) &ScatterGatherList\r
209 );\r
f03ccf59 210 if (!EFI_ERROR (Status)) {\r
211 //\r
212 // Variable has been set successfully, increase variable index.\r
213 //\r
214 mTimes++;\r
215 if(InitiateReset) {\r
216 //\r
217 // Firmware that encounters a capsule which has the CAPSULE_FLAGS_INITIATE_RESET Flag set in its header\r
d1102dba 218 // will initiate a reset of the platform which is compatible with the passed-in capsule request and will\r
f03ccf59 219 // not return back to the caller.\r
220 //\r
221 EfiResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
222 }\r
3f42faa7 223 }\r
409d47b4 224 return Status;\r
74fea867 225}\r
226\r
5d69642d
LG
227/**\r
228 Returns if the capsule can be supported via UpdateCapsule().\r
4ef0b0ac
WX
229 Notice: When PcdCapsuleInRamSupport is unsupported, even this routine returns a valid answer,\r
230 the capsule still is unsupported via UpdateCapsule().\r
5d69642d
LG
231\r
232 @param CapsuleHeaderArray Virtual pointer to an array of virtual pointers to the capsules\r
233 being passed into update capsule.\r
234 @param CapsuleCount Number of pointers to EFI_CAPSULE_HEADER in\r
235 CaspuleHeaderArray.\r
236 @param MaxiumCapsuleSize On output the maximum size that UpdateCapsule() can\r
237 support as an argument to UpdateCapsule() via\r
238 CapsuleHeaderArray and ScatterGatherList.\r
239 @param ResetType Returns the type of reset required for the capsule update.\r
74fea867 240\r
5d69642d
LG
241 @retval EFI_SUCCESS Valid answer returned.\r
242 @retval EFI_UNSUPPORTED The capsule image is not supported on this platform, and\r
243 MaximumCapsuleSize and ResetType are undefined.\r
244 @retval EFI_INVALID_PARAMETER MaximumCapsuleSize is NULL, or ResetTyep is NULL,\r
245 Or CapsuleCount is Zero, or CapsuleImage is not valid.\r
74fea867 246\r
5d69642d 247**/\r
74fea867 248EFI_STATUS\r
249EFIAPI\r
250QueryCapsuleCapabilities (\r
251 IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,\r
252 IN UINTN CapsuleCount,\r
253 OUT UINT64 *MaxiumCapsuleSize,\r
254 OUT EFI_RESET_TYPE *ResetType\r
255 )\r
74fea867 256{\r
566771b0 257 EFI_STATUS Status;\r
74fea867 258 UINTN ArrayNumber;\r
259 EFI_CAPSULE_HEADER *CapsuleHeader;\r
0ef42f88 260 BOOLEAN NeedReset;\r
74fea867 261\r
5d69642d
LG
262 //\r
263 // Capsule Count can't be less than one.\r
264 //\r
74fea867 265 if (CapsuleCount < 1) {\r
266 return EFI_INVALID_PARAMETER;\r
267 }\r
d1102dba 268\r
5d69642d 269 //\r
3f42faa7 270 // Check whether input parameter is valid\r
5d69642d 271 //\r
74fea867 272 if ((MaxiumCapsuleSize == NULL) ||(ResetType == NULL)) {\r
273 return EFI_INVALID_PARAMETER;\r
274 }\r
275\r
276 CapsuleHeader = NULL;\r
0ef42f88 277 NeedReset = FALSE;\r
74fea867 278\r
279 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
280 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
d12f75fe
LG
281 //\r
282 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have\r
283 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.\r
284 //\r
74fea867 285 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {\r
286 return EFI_INVALID_PARAMETER;\r
287 }\r
d12f75fe 288 //\r
3f42faa7 289 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have\r
290 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.\r
291 //\r
292 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {\r
293 return EFI_INVALID_PARAMETER;\r
294 }\r
566771b0 295\r
296 //\r
d1102dba 297 // Check FMP capsule flag\r
566771b0 298 //\r
299 if (CompareGuid(&CapsuleHeader->CapsuleGuid, &gEfiFmpCapsuleGuid)\r
300 && (CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0 ) {\r
301 return EFI_INVALID_PARAMETER;\r
302 }\r
303\r
3f42faa7 304 //\r
5d69642d 305 // Check Capsule image without populate flag is supported by firmware\r
6ee65722 306 //\r
566771b0 307 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {\r
308 Status = SupportCapsuleImage (CapsuleHeader);\r
309 if (EFI_ERROR(Status)) {\r
310 return Status;\r
311 }\r
6ee65722 312 }\r
74fea867 313 }\r
314\r
315 //\r
d1102dba 316 // Find out whether there is any capsule defined to persist across system reset.\r
74fea867 317 //\r
0ef42f88 318 for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {\r
319 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
320 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {\r
321 NeedReset = TRUE;\r
322 break;\r
323 }\r
324 }\r
566771b0 325\r
0ef42f88 326 if (NeedReset) {\r
74fea867 327 //\r
328 //Check if the platform supports update capsule across a system reset\r
329 //\r
e077a93d 330 if (!IsPersistAcrossResetCapsuleSupported ()) {\r
74fea867 331 return EFI_UNSUPPORTED;\r
332 }\r
0ef42f88 333 *ResetType = EfiResetWarm;\r
f67eb9d8 334 *MaxiumCapsuleSize = (UINT64) mMaxSizePopulateCapsule;\r
74fea867 335 } else {\r
5d69642d
LG
336 //\r
337 // For non-reset capsule image.\r
338 //\r
74fea867 339 *ResetType = EfiResetCold;\r
f67eb9d8 340 *MaxiumCapsuleSize = (UINT64) mMaxSizeNonPopulateCapsule;\r
74fea867 341 }\r
5c526736 342\r
74fea867 343 return EFI_SUCCESS;\r
344}\r
345\r
346\r
5d69642d
LG
347/**\r
348\r
109e9a61 349 This code installs UEFI capsule runtime service.\r
5d69642d 350\r
d1102dba 351 @param ImageHandle The firmware allocated handle for the EFI image.\r
5d69642d
LG
352 @param SystemTable A pointer to the EFI System Table.\r
353\r
d1102dba 354 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully.\r
5d69642d
LG
355\r
356**/\r
74fea867 357EFI_STATUS\r
358EFIAPI\r
359CapsuleServiceInitialize (\r
360 IN EFI_HANDLE ImageHandle,\r
361 IN EFI_SYSTEM_TABLE *SystemTable\r
362 )\r
74fea867 363{\r
364 EFI_STATUS Status;\r
f67eb9d8
SZ
365\r
366 mMaxSizePopulateCapsule = PcdGet32(PcdMaxSizePopulateCapsule);\r
367 mMaxSizeNonPopulateCapsule = PcdGet32(PcdMaxSizeNonPopulateCapsule);\r
368\r
ab7017fe 369 //\r
d1102dba 370 // When PEI phase is IA32, DXE phase is X64, it is possible that capsule data are\r
ab7017fe 371 // put above 4GB, so capsule PEI will transfer to long mode to get capsule data.\r
372 // The page table and stack is used to transfer processor mode from IA32 to long mode.\r
373 // Create the base address of page table and stack, and save them into variable.\r
374 // This is not needed when capsule with reset type is not supported.\r
375 //\r
376 SaveLongModeContext ();\r
d1102dba 377\r
5d69642d
LG
378 //\r
379 // Install capsule runtime services into UEFI runtime service tables.\r
380 //\r
109e9a61
LG
381 gRT->UpdateCapsule = UpdateCapsule;\r
382 gRT->QueryCapsuleCapabilities = QueryCapsuleCapabilities;\r
74fea867 383\r
384 //\r
5d69642d
LG
385 // Install the Capsule Architectural Protocol on a new handle\r
386 // to signify the capsule runtime services are ready.\r
74fea867 387 //\r
74fea867 388 Status = gBS->InstallMultipleProtocolInterfaces (\r
3f42faa7 389 &mNewHandle,\r
74fea867 390 &gEfiCapsuleArchProtocolGuid,\r
391 NULL,\r
392 NULL\r
393 );\r
394 ASSERT_EFI_ERROR (Status);\r
395\r
3f42faa7 396 return Status;\r
74fea867 397}\r