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