]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleService.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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
1333d8c8 7Copyright (c) 2006 - 2020, 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
1436aea4 22UINTN mTimes = 0;\r
f03ccf59 23\r
1436aea4
MK
24UINT32 mMaxSizePopulateCapsule = 0;\r
25UINT32 mMaxSizeNonPopulateCapsule = 0;\r
f67eb9d8 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
1436aea4
MK
61 IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,\r
62 IN UINTN CapsuleCount,\r
63 IN EFI_PHYSICAL_ADDRESS ScatterGatherList OPTIONAL\r
74fea867 64 )\r
74fea867 65{\r
1436aea4
MK
66 UINTN ArrayNumber;\r
67 EFI_STATUS Status;\r
68 EFI_CAPSULE_HEADER *CapsuleHeader;\r
69 BOOLEAN NeedReset;\r
70 BOOLEAN InitiateReset;\r
71 CHAR16 CapsuleVarName[30];\r
72 CHAR16 *TempVarName;\r
d1102dba 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
1436aea4 78 if (!PcdGetBool (PcdCapsuleInRamSupport)) {\r
4ef0b0ac
WX
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
1436aea4 103\r
d12f75fe 104 //\r
3f42faa7 105 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have\r
106 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.\r
107 //\r
108 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {\r
109 return EFI_INVALID_PARAMETER;\r
110 }\r
566771b0 111\r
112 //\r
d1102dba 113 // Check FMP capsule flag\r
566771b0 114 //\r
1436aea4
MK
115 if ( CompareGuid (&CapsuleHeader->CapsuleGuid, &gEfiFmpCapsuleGuid)\r
116 && ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0))\r
117 {\r
118 return EFI_INVALID_PARAMETER;\r
566771b0 119 }\r
120\r
3f42faa7 121 //\r
d1102dba 122 // Check Capsule image without populate flag by firmware support capsule function\r
6ee65722 123 //\r
566771b0 124 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {\r
125 Status = SupportCapsuleImage (CapsuleHeader);\r
1436aea4 126 if (EFI_ERROR (Status)) {\r
566771b0 127 return Status;\r
128 }\r
6ee65722 129 }\r
74fea867 130 }\r
131\r
d12f75fe 132 //\r
3f42faa7 133 // Walk through all capsules, record whether there is a capsule needs reset\r
134 // or initiate reset. And then process capsules which has no reset flag directly.\r
5d69642d 135 //\r
1436aea4 136 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
409d47b4 137 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
d12f75fe 138 //\r
409d47b4
LG
139 // Here should be in the boot-time for non-reset capsule image\r
140 // Platform specific update for the non-reset capsule image.\r
d12f75fe 141 //\r
409d47b4 142 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) == 0) {\r
1333d8c8 143 if (EfiAtRuntime () && !FeaturePcdGet (PcdSupportProcessCapsuleAtRuntime)) {\r
ad3f3656 144 Status = EFI_OUT_OF_RESOURCES;\r
409d47b4 145 } else {\r
1436aea4 146 Status = ProcessCapsuleImage (CapsuleHeader);\r
409d47b4 147 }\r
1436aea4
MK
148\r
149 if (EFI_ERROR (Status)) {\r
d12f75fe 150 return Status;\r
74fea867 151 }\r
409d47b4
LG
152 } else {\r
153 NeedReset = TRUE;\r
3f42faa7 154 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_INITIATE_RESET) != 0) {\r
155 InitiateReset = TRUE;\r
156 }\r
74fea867 157 }\r
74fea867 158 }\r
d1102dba 159\r
409d47b4
LG
160 //\r
161 // After launching all capsules who has no reset flag, if no more capsules claims\r
162 // for a system reset just return.\r
163 //\r
164 if (!NeedReset) {\r
165 return EFI_SUCCESS;\r
166 }\r
74fea867 167\r
168 //\r
409d47b4 169 // ScatterGatherList is only referenced if the capsules are defined to persist across\r
d1102dba 170 // system reset.\r
74fea867 171 //\r
1436aea4 172 if (ScatterGatherList == (EFI_PHYSICAL_ADDRESS)(UINTN)NULL) {\r
409d47b4 173 return EFI_INVALID_PARAMETER;\r
74fea867 174 }\r
175\r
176 //\r
409d47b4 177 // Check if the platform supports update capsule across a system reset\r
74fea867 178 //\r
e077a93d 179 if (!IsPersistAcrossResetCapsuleSupported ()) {\r
409d47b4 180 return EFI_UNSUPPORTED;\r
74fea867 181 }\r
182\r
e077a93d
AB
183 CapsuleCacheWriteBack (ScatterGatherList);\r
184\r
f03ccf59 185 //\r
186 // Construct variable name CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...\r
187 // if user calls UpdateCapsule multiple times.\r
188 //\r
1436aea4 189 StrCpyS (CapsuleVarName, sizeof (CapsuleVarName)/sizeof (CHAR16), EFI_CAPSULE_VARIABLE_NAME);\r
f03ccf59 190 TempVarName = CapsuleVarName + StrLen (CapsuleVarName);\r
191 if (mTimes > 0) {\r
9f4048f7
HW
192 UnicodeValueToStringS (\r
193 TempVarName,\r
194 sizeof (CapsuleVarName) - ((UINTN)TempVarName - (UINTN)CapsuleVarName),\r
195 0,\r
196 mTimes,\r
197 0\r
198 );\r
f03ccf59 199 }\r
200\r
409d47b4
LG
201 //\r
202 // ScatterGatherList is only referenced if the capsules are defined to persist across\r
d1102dba 203 // system reset. Set its value into NV storage to let pre-boot driver to pick it up\r
409d47b4
LG
204 // after coming through a system reset.\r
205 //\r
b3b1a4cf 206 Status = EfiSetVariable (\r
f03ccf59 207 CapsuleVarName,\r
b3b1a4cf 208 &gEfiCapsuleVendorGuid,\r
209 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
210 sizeof (UINTN),\r
1436aea4 211 (VOID *)&ScatterGatherList\r
b3b1a4cf 212 );\r
f03ccf59 213 if (!EFI_ERROR (Status)) {\r
1436aea4
MK
214 //\r
215 // Variable has been set successfully, increase variable index.\r
216 //\r
217 mTimes++;\r
218 if (InitiateReset) {\r
219 //\r
220 // Firmware that encounters a capsule which has the CAPSULE_FLAGS_INITIATE_RESET Flag set in its header\r
221 // will initiate a reset of the platform which is compatible with the passed-in capsule request and will\r
222 // not return back to the caller.\r
223 //\r
224 EfiResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
225 }\r
3f42faa7 226 }\r
1436aea4 227\r
409d47b4 228 return Status;\r
74fea867 229}\r
230\r
5d69642d
LG
231/**\r
232 Returns if the capsule can be supported via UpdateCapsule().\r
4ef0b0ac
WX
233 Notice: When PcdCapsuleInRamSupport is unsupported, even this routine returns a valid answer,\r
234 the capsule still is unsupported via UpdateCapsule().\r
5d69642d
LG
235\r
236 @param CapsuleHeaderArray Virtual pointer to an array of virtual pointers to the capsules\r
237 being passed into update capsule.\r
238 @param CapsuleCount Number of pointers to EFI_CAPSULE_HEADER in\r
239 CaspuleHeaderArray.\r
240 @param MaxiumCapsuleSize On output the maximum size that UpdateCapsule() can\r
241 support as an argument to UpdateCapsule() via\r
242 CapsuleHeaderArray and ScatterGatherList.\r
243 @param ResetType Returns the type of reset required for the capsule update.\r
74fea867 244\r
5d69642d
LG
245 @retval EFI_SUCCESS Valid answer returned.\r
246 @retval EFI_UNSUPPORTED The capsule image is not supported on this platform, and\r
247 MaximumCapsuleSize and ResetType are undefined.\r
248 @retval EFI_INVALID_PARAMETER MaximumCapsuleSize is NULL, or ResetTyep is NULL,\r
249 Or CapsuleCount is Zero, or CapsuleImage is not valid.\r
74fea867 250\r
5d69642d 251**/\r
74fea867 252EFI_STATUS\r
253EFIAPI\r
254QueryCapsuleCapabilities (\r
1436aea4
MK
255 IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,\r
256 IN UINTN CapsuleCount,\r
257 OUT UINT64 *MaxiumCapsuleSize,\r
258 OUT EFI_RESET_TYPE *ResetType\r
74fea867 259 )\r
74fea867 260{\r
1436aea4
MK
261 EFI_STATUS Status;\r
262 UINTN ArrayNumber;\r
263 EFI_CAPSULE_HEADER *CapsuleHeader;\r
264 BOOLEAN NeedReset;\r
74fea867 265\r
5d69642d
LG
266 //\r
267 // Capsule Count can't be less than one.\r
268 //\r
74fea867 269 if (CapsuleCount < 1) {\r
270 return EFI_INVALID_PARAMETER;\r
271 }\r
d1102dba 272\r
5d69642d 273 //\r
3f42faa7 274 // Check whether input parameter is valid\r
5d69642d 275 //\r
1436aea4 276 if ((MaxiumCapsuleSize == NULL) || (ResetType == NULL)) {\r
74fea867 277 return EFI_INVALID_PARAMETER;\r
278 }\r
279\r
280 CapsuleHeader = NULL;\r
0ef42f88 281 NeedReset = FALSE;\r
74fea867 282\r
283 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
284 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
d12f75fe
LG
285 //\r
286 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have\r
287 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.\r
288 //\r
74fea867 289 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {\r
290 return EFI_INVALID_PARAMETER;\r
291 }\r
1436aea4 292\r
d12f75fe 293 //\r
3f42faa7 294 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have\r
295 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.\r
296 //\r
297 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {\r
298 return EFI_INVALID_PARAMETER;\r
299 }\r
566771b0 300\r
301 //\r
d1102dba 302 // Check FMP capsule flag\r
566771b0 303 //\r
1436aea4
MK
304 if ( CompareGuid (&CapsuleHeader->CapsuleGuid, &gEfiFmpCapsuleGuid)\r
305 && ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0))\r
306 {\r
307 return EFI_INVALID_PARAMETER;\r
566771b0 308 }\r
309\r
3f42faa7 310 //\r
5d69642d 311 // Check Capsule image without populate flag is supported by firmware\r
6ee65722 312 //\r
566771b0 313 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {\r
314 Status = SupportCapsuleImage (CapsuleHeader);\r
1436aea4 315 if (EFI_ERROR (Status)) {\r
566771b0 316 return Status;\r
317 }\r
6ee65722 318 }\r
74fea867 319 }\r
320\r
321 //\r
d1102dba 322 // Find out whether there is any capsule defined to persist across system reset.\r
74fea867 323 //\r
1436aea4 324 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
0ef42f88 325 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
326 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {\r
327 NeedReset = TRUE;\r
328 break;\r
329 }\r
330 }\r
566771b0 331\r
0ef42f88 332 if (NeedReset) {\r
74fea867 333 //\r
1436aea4 334 // Check if the platform supports update capsule across a system reset\r
74fea867 335 //\r
e077a93d 336 if (!IsPersistAcrossResetCapsuleSupported ()) {\r
74fea867 337 return EFI_UNSUPPORTED;\r
338 }\r
1436aea4
MK
339\r
340 *ResetType = EfiResetWarm;\r
341 *MaxiumCapsuleSize = (UINT64)mMaxSizePopulateCapsule;\r
74fea867 342 } else {\r
5d69642d
LG
343 //\r
344 // For non-reset capsule image.\r
345 //\r
1436aea4
MK
346 *ResetType = EfiResetCold;\r
347 *MaxiumCapsuleSize = (UINT64)mMaxSizeNonPopulateCapsule;\r
74fea867 348 }\r
5c526736 349\r
74fea867 350 return EFI_SUCCESS;\r
351}\r
352\r
5d69642d
LG
353/**\r
354\r
109e9a61 355 This code installs UEFI capsule runtime service.\r
5d69642d 356\r
d1102dba 357 @param ImageHandle The firmware allocated handle for the EFI image.\r
5d69642d
LG
358 @param SystemTable A pointer to the EFI System Table.\r
359\r
d1102dba 360 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully.\r
5d69642d
LG
361\r
362**/\r
74fea867 363EFI_STATUS\r
364EFIAPI\r
365CapsuleServiceInitialize (\r
1436aea4
MK
366 IN EFI_HANDLE ImageHandle,\r
367 IN EFI_SYSTEM_TABLE *SystemTable\r
74fea867 368 )\r
74fea867 369{\r
370 EFI_STATUS Status;\r
f67eb9d8 371\r
1436aea4
MK
372 mMaxSizePopulateCapsule = PcdGet32 (PcdMaxSizePopulateCapsule);\r
373 mMaxSizeNonPopulateCapsule = PcdGet32 (PcdMaxSizeNonPopulateCapsule);\r
f67eb9d8 374\r
ab7017fe 375 //\r
d1102dba 376 // When PEI phase is IA32, DXE phase is X64, it is possible that capsule data are\r
ab7017fe 377 // put above 4GB, so capsule PEI will transfer to long mode to get capsule data.\r
378 // The page table and stack is used to transfer processor mode from IA32 to long mode.\r
379 // Create the base address of page table and stack, and save them into variable.\r
380 // This is not needed when capsule with reset type is not supported.\r
381 //\r
382 SaveLongModeContext ();\r
d1102dba 383\r
5d69642d
LG
384 //\r
385 // Install capsule runtime services into UEFI runtime service tables.\r
386 //\r
1436aea4
MK
387 gRT->UpdateCapsule = UpdateCapsule;\r
388 gRT->QueryCapsuleCapabilities = QueryCapsuleCapabilities;\r
74fea867 389\r
390 //\r
5d69642d
LG
391 // Install the Capsule Architectural Protocol on a new handle\r
392 // to signify the capsule runtime services are ready.\r
74fea867 393 //\r
74fea867 394 Status = gBS->InstallMultipleProtocolInterfaces (\r
3f42faa7 395 &mNewHandle,\r
74fea867 396 &gEfiCapsuleArchProtocolGuid,\r
397 NULL,\r
398 NULL\r
399 );\r
400 ASSERT_EFI_ERROR (Status);\r
401\r
3f42faa7 402 return Status;\r
74fea867 403}\r