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.
7 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include "CapsuleService.h"
15 // Handle for the installation of Capsule Architecture Protocol.
17 EFI_HANDLE mNewHandle
= NULL
;
20 // The times of calling UpdateCapsule ()
24 UINT32 mMaxSizePopulateCapsule
= 0;
25 UINT32 mMaxSizeNonPopulateCapsule
= 0;
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.
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
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.
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.
61 IN EFI_CAPSULE_HEADER
**CapsuleHeaderArray
,
62 IN UINTN CapsuleCount
,
63 IN EFI_PHYSICAL_ADDRESS ScatterGatherList OPTIONAL
68 EFI_CAPSULE_HEADER
*CapsuleHeader
;
70 BOOLEAN InitiateReset
;
71 CHAR16 CapsuleVarName
[30];
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.
78 if (!PcdGetBool(PcdCapsuleInRamSupport
)) {
79 return EFI_UNSUPPORTED
;
83 // Capsule Count can't be less than one.
85 if (CapsuleCount
< 1) {
86 return EFI_INVALID_PARAMETER
;
90 InitiateReset
= FALSE
;
92 CapsuleVarName
[0] = 0;
94 for (ArrayNumber
= 0; ArrayNumber
< CapsuleCount
; ArrayNumber
++) {
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.
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
;
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.
107 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_INITIATE_RESET
)) == CAPSULE_FLAGS_INITIATE_RESET
) {
108 return EFI_INVALID_PARAMETER
;
112 // Check FMP capsule flag
114 if (CompareGuid(&CapsuleHeader
->CapsuleGuid
, &gEfiFmpCapsuleGuid
)
115 && (CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) != 0 ) {
116 return EFI_INVALID_PARAMETER
;
120 // Check Capsule image without populate flag by firmware support capsule function
122 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) == 0) {
123 Status
= SupportCapsuleImage (CapsuleHeader
);
124 if (EFI_ERROR(Status
)) {
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.
134 for (ArrayNumber
= 0; ArrayNumber
< CapsuleCount
; ArrayNumber
++) {
135 CapsuleHeader
= CapsuleHeaderArray
[ArrayNumber
];
137 // Here should be in the boot-time for non-reset capsule image
138 // Platform specific update for the non-reset capsule image.
140 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_PERSIST_ACROSS_RESET
) == 0) {
141 if (EfiAtRuntime ()) {
142 Status
= EFI_OUT_OF_RESOURCES
;
144 Status
= ProcessCapsuleImage(CapsuleHeader
);
146 if (EFI_ERROR(Status
)) {
151 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_INITIATE_RESET
) != 0) {
152 InitiateReset
= TRUE
;
158 // After launching all capsules who has no reset flag, if no more capsules claims
159 // for a system reset just return.
166 // ScatterGatherList is only referenced if the capsules are defined to persist across
169 if (ScatterGatherList
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
) {
170 return EFI_INVALID_PARAMETER
;
174 // Check if the platform supports update capsule across a system reset
176 if (!IsPersistAcrossResetCapsuleSupported ()) {
177 return EFI_UNSUPPORTED
;
180 CapsuleCacheWriteBack (ScatterGatherList
);
183 // Construct variable name CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...
184 // if user calls UpdateCapsule multiple times.
186 StrCpyS (CapsuleVarName
, sizeof(CapsuleVarName
)/sizeof(CHAR16
), EFI_CAPSULE_VARIABLE_NAME
);
187 TempVarName
= CapsuleVarName
+ StrLen (CapsuleVarName
);
189 UnicodeValueToStringS (
191 sizeof (CapsuleVarName
) - ((UINTN
)TempVarName
- (UINTN
)CapsuleVarName
),
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.
203 Status
= EfiSetVariable (
205 &gEfiCapsuleVendorGuid
,
206 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
208 (VOID
*) &ScatterGatherList
210 if (!EFI_ERROR (Status
)) {
212 // Variable has been set successfully, increase variable index.
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.
221 EfiResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
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().
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
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.
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.
250 QueryCapsuleCapabilities (
251 IN EFI_CAPSULE_HEADER
**CapsuleHeaderArray
,
252 IN UINTN CapsuleCount
,
253 OUT UINT64
*MaxiumCapsuleSize
,
254 OUT EFI_RESET_TYPE
*ResetType
259 EFI_CAPSULE_HEADER
*CapsuleHeader
;
263 // Capsule Count can't be less than one.
265 if (CapsuleCount
< 1) {
266 return EFI_INVALID_PARAMETER
;
270 // Check whether input parameter is valid
272 if ((MaxiumCapsuleSize
== NULL
) ||(ResetType
== NULL
)) {
273 return EFI_INVALID_PARAMETER
;
276 CapsuleHeader
= NULL
;
279 for (ArrayNumber
= 0; ArrayNumber
< CapsuleCount
; ArrayNumber
++) {
280 CapsuleHeader
= CapsuleHeaderArray
[ArrayNumber
];
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.
285 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) {
286 return EFI_INVALID_PARAMETER
;
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.
292 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_INITIATE_RESET
)) == CAPSULE_FLAGS_INITIATE_RESET
) {
293 return EFI_INVALID_PARAMETER
;
297 // Check FMP capsule flag
299 if (CompareGuid(&CapsuleHeader
->CapsuleGuid
, &gEfiFmpCapsuleGuid
)
300 && (CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) != 0 ) {
301 return EFI_INVALID_PARAMETER
;
305 // Check Capsule image without populate flag is supported by firmware
307 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) == 0) {
308 Status
= SupportCapsuleImage (CapsuleHeader
);
309 if (EFI_ERROR(Status
)) {
316 // Find out whether there is any capsule defined to persist across system reset.
318 for (ArrayNumber
= 0; ArrayNumber
< CapsuleCount
; ArrayNumber
++) {
319 CapsuleHeader
= CapsuleHeaderArray
[ArrayNumber
];
320 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_PERSIST_ACROSS_RESET
) != 0) {
328 //Check if the platform supports update capsule across a system reset
330 if (!IsPersistAcrossResetCapsuleSupported ()) {
331 return EFI_UNSUPPORTED
;
333 *ResetType
= EfiResetWarm
;
334 *MaxiumCapsuleSize
= (UINT64
) mMaxSizePopulateCapsule
;
337 // For non-reset capsule image.
339 *ResetType
= EfiResetCold
;
340 *MaxiumCapsuleSize
= (UINT64
) mMaxSizeNonPopulateCapsule
;
349 This code installs UEFI capsule runtime service.
351 @param ImageHandle The firmware allocated handle for the EFI image.
352 @param SystemTable A pointer to the EFI System Table.
354 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully.
359 CapsuleServiceInitialize (
360 IN EFI_HANDLE ImageHandle
,
361 IN EFI_SYSTEM_TABLE
*SystemTable
366 mMaxSizePopulateCapsule
= PcdGet32(PcdMaxSizePopulateCapsule
);
367 mMaxSizeNonPopulateCapsule
= PcdGet32(PcdMaxSizeNonPopulateCapsule
);
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.
376 SaveLongModeContext ();
379 // Install capsule runtime services into UEFI runtime service tables.
381 gRT
->UpdateCapsule
= UpdateCapsule
;
382 gRT
->QueryCapsuleCapabilities
= QueryCapsuleCapabilities
;
385 // Install the Capsule Architectural Protocol on a new handle
386 // to signify the capsule runtime services are ready.
388 Status
= gBS
->InstallMultipleProtocolInterfaces (
390 &gEfiCapsuleArchProtocolGuid
,
394 ASSERT_EFI_ERROR (Status
);