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