]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleService.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Universal / CapsuleRuntimeDxe / CapsuleService.c
... / ...
CommitLineData
1/** @file\r
2 Capsule Runtime Driver produces two UEFI capsule runtime services.\r
3 (UpdateCapsule, QueryCapsuleCapabilities)\r
4 It installs the Capsule Architectural Protocol defined in PI1.0a to signify\r
5 the capsule runtime services are ready.\r
6\r
7Copyright (c) 2006 - 2020, Intel Corporation. All rights reserved.<BR>\r
8SPDX-License-Identifier: BSD-2-Clause-Patent\r
9\r
10**/\r
11\r
12#include "CapsuleService.h"\r
13\r
14//\r
15// Handle for the installation of Capsule Architecture Protocol.\r
16//\r
17EFI_HANDLE mNewHandle = NULL;\r
18\r
19//\r
20// The times of calling UpdateCapsule ()\r
21//\r
22UINTN mTimes = 0;\r
23\r
24UINT32 mMaxSizePopulateCapsule = 0;\r
25UINT32 mMaxSizeNonPopulateCapsule = 0;\r
26\r
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
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
50 @retval EFI_UNSUPPORTED CapsuleImage is not recognized by the firmware.\r
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
53 in runtime. The caller may resubmit the capsule prior to ExitBootServices().\r
54 @retval EFI_OUT_OF_RESOURCES When ExitBootServices() has not been previously called then this error indicates\r
55 the capsule is compatible with this platform but there are insufficient resources to process.\r
56\r
57**/\r
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
65{\r
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
73\r
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
82 //\r
83 // Capsule Count can't be less than one.\r
84 //\r
85 if (CapsuleCount < 1) {\r
86 return EFI_INVALID_PARAMETER;\r
87 }\r
88\r
89 NeedReset = FALSE;\r
90 InitiateReset = FALSE;\r
91 CapsuleHeader = NULL;\r
92 CapsuleVarName[0] = 0;\r
93\r
94 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
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
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
103\r
104 //\r
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
111\r
112 //\r
113 // Check FMP capsule flag\r
114 //\r
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
119 }\r
120\r
121 //\r
122 // Check Capsule image without populate flag by firmware support capsule function\r
123 //\r
124 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {\r
125 Status = SupportCapsuleImage (CapsuleHeader);\r
126 if (EFI_ERROR (Status)) {\r
127 return Status;\r
128 }\r
129 }\r
130 }\r
131\r
132 //\r
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
135 //\r
136 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
137 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
138 //\r
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
141 //\r
142 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) == 0) {\r
143 if (EfiAtRuntime () && !FeaturePcdGet (PcdSupportProcessCapsuleAtRuntime)) {\r
144 Status = EFI_OUT_OF_RESOURCES;\r
145 } else {\r
146 Status = ProcessCapsuleImage (CapsuleHeader);\r
147 }\r
148\r
149 if (EFI_ERROR (Status)) {\r
150 return Status;\r
151 }\r
152 } else {\r
153 NeedReset = TRUE;\r
154 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_INITIATE_RESET) != 0) {\r
155 InitiateReset = TRUE;\r
156 }\r
157 }\r
158 }\r
159\r
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
167\r
168 //\r
169 // ScatterGatherList is only referenced if the capsules are defined to persist across\r
170 // system reset.\r
171 //\r
172 if (ScatterGatherList == (EFI_PHYSICAL_ADDRESS)(UINTN)NULL) {\r
173 return EFI_INVALID_PARAMETER;\r
174 }\r
175\r
176 //\r
177 // Check if the platform supports update capsule across a system reset\r
178 //\r
179 if (!IsPersistAcrossResetCapsuleSupported ()) {\r
180 return EFI_UNSUPPORTED;\r
181 }\r
182\r
183 CapsuleCacheWriteBack (ScatterGatherList);\r
184\r
185 //\r
186 // Construct variable name CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...\r
187 // if user calls UpdateCapsule multiple times.\r
188 //\r
189 StrCpyS (CapsuleVarName, sizeof (CapsuleVarName)/sizeof (CHAR16), EFI_CAPSULE_VARIABLE_NAME);\r
190 TempVarName = CapsuleVarName + StrLen (CapsuleVarName);\r
191 if (mTimes > 0) {\r
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
199 }\r
200\r
201 //\r
202 // ScatterGatherList is only referenced if the capsules are defined to persist across\r
203 // system reset. Set its value into NV storage to let pre-boot driver to pick it up\r
204 // after coming through a system reset.\r
205 //\r
206 Status = EfiSetVariable (\r
207 CapsuleVarName,\r
208 &gEfiCapsuleVendorGuid,\r
209 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
210 sizeof (UINTN),\r
211 (VOID *)&ScatterGatherList\r
212 );\r
213 if (!EFI_ERROR (Status)) {\r
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
226 }\r
227\r
228 return Status;\r
229}\r
230\r
231/**\r
232 Returns if the capsule can be supported via UpdateCapsule().\r
233 Notice: When PcdCapsuleInRamSupport is unsupported, even this routine returns a valid answer,\r
234 the capsule still is unsupported via UpdateCapsule().\r
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
244\r
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
250\r
251**/\r
252EFI_STATUS\r
253EFIAPI\r
254QueryCapsuleCapabilities (\r
255 IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,\r
256 IN UINTN CapsuleCount,\r
257 OUT UINT64 *MaxiumCapsuleSize,\r
258 OUT EFI_RESET_TYPE *ResetType\r
259 )\r
260{\r
261 EFI_STATUS Status;\r
262 UINTN ArrayNumber;\r
263 EFI_CAPSULE_HEADER *CapsuleHeader;\r
264 BOOLEAN NeedReset;\r
265\r
266 //\r
267 // Capsule Count can't be less than one.\r
268 //\r
269 if (CapsuleCount < 1) {\r
270 return EFI_INVALID_PARAMETER;\r
271 }\r
272\r
273 //\r
274 // Check whether input parameter is valid\r
275 //\r
276 if ((MaxiumCapsuleSize == NULL) || (ResetType == NULL)) {\r
277 return EFI_INVALID_PARAMETER;\r
278 }\r
279\r
280 CapsuleHeader = NULL;\r
281 NeedReset = FALSE;\r
282\r
283 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
284 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
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
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
292\r
293 //\r
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
300\r
301 //\r
302 // Check FMP capsule flag\r
303 //\r
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
308 }\r
309\r
310 //\r
311 // Check Capsule image without populate flag is supported by firmware\r
312 //\r
313 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {\r
314 Status = SupportCapsuleImage (CapsuleHeader);\r
315 if (EFI_ERROR (Status)) {\r
316 return Status;\r
317 }\r
318 }\r
319 }\r
320\r
321 //\r
322 // Find out whether there is any capsule defined to persist across system reset.\r
323 //\r
324 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
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
331\r
332 if (NeedReset) {\r
333 //\r
334 // Check if the platform supports update capsule across a system reset\r
335 //\r
336 if (!IsPersistAcrossResetCapsuleSupported ()) {\r
337 return EFI_UNSUPPORTED;\r
338 }\r
339\r
340 *ResetType = EfiResetWarm;\r
341 *MaxiumCapsuleSize = (UINT64)mMaxSizePopulateCapsule;\r
342 } else {\r
343 //\r
344 // For non-reset capsule image.\r
345 //\r
346 *ResetType = EfiResetCold;\r
347 *MaxiumCapsuleSize = (UINT64)mMaxSizeNonPopulateCapsule;\r
348 }\r
349\r
350 return EFI_SUCCESS;\r
351}\r
352\r
353/**\r
354\r
355 This code installs UEFI capsule runtime service.\r
356\r
357 @param ImageHandle The firmware allocated handle for the EFI image.\r
358 @param SystemTable A pointer to the EFI System Table.\r
359\r
360 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully.\r
361\r
362**/\r
363EFI_STATUS\r
364EFIAPI\r
365CapsuleServiceInitialize (\r
366 IN EFI_HANDLE ImageHandle,\r
367 IN EFI_SYSTEM_TABLE *SystemTable\r
368 )\r
369{\r
370 EFI_STATUS Status;\r
371\r
372 mMaxSizePopulateCapsule = PcdGet32 (PcdMaxSizePopulateCapsule);\r
373 mMaxSizeNonPopulateCapsule = PcdGet32 (PcdMaxSizeNonPopulateCapsule);\r
374\r
375 //\r
376 // When PEI phase is IA32, DXE phase is X64, it is possible that capsule data are\r
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
383\r
384 //\r
385 // Install capsule runtime services into UEFI runtime service tables.\r
386 //\r
387 gRT->UpdateCapsule = UpdateCapsule;\r
388 gRT->QueryCapsuleCapabilities = QueryCapsuleCapabilities;\r
389\r
390 //\r
391 // Install the Capsule Architectural Protocol on a new handle\r
392 // to signify the capsule runtime services are ready.\r
393 //\r
394 Status = gBS->InstallMultipleProtocolInterfaces (\r
395 &mNewHandle,\r
396 &gEfiCapsuleArchProtocolGuid,\r
397 NULL,\r
398 NULL\r
399 );\r
400 ASSERT_EFI_ERROR (Status);\r
401\r
402 return Status;\r
403}\r