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 - 2018, 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 // Capsule Count can't be less than one.
77 if (CapsuleCount
< 1) {
78 return EFI_INVALID_PARAMETER
;
82 InitiateReset
= FALSE
;
84 CapsuleVarName
[0] = 0;
86 for (ArrayNumber
= 0; ArrayNumber
< CapsuleCount
; ArrayNumber
++) {
88 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have
89 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
91 CapsuleHeader
= CapsuleHeaderArray
[ArrayNumber
];
92 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) {
93 return EFI_INVALID_PARAMETER
;
96 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have
97 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
99 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_INITIATE_RESET
)) == CAPSULE_FLAGS_INITIATE_RESET
) {
100 return EFI_INVALID_PARAMETER
;
104 // Check FMP capsule flag
106 if (CompareGuid(&CapsuleHeader
->CapsuleGuid
, &gEfiFmpCapsuleGuid
)
107 && (CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) != 0 ) {
108 return EFI_INVALID_PARAMETER
;
112 // Check Capsule image without populate flag by firmware support capsule function
114 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) == 0) {
115 Status
= SupportCapsuleImage (CapsuleHeader
);
116 if (EFI_ERROR(Status
)) {
123 // Walk through all capsules, record whether there is a capsule needs reset
124 // or initiate reset. And then process capsules which has no reset flag directly.
126 for (ArrayNumber
= 0; ArrayNumber
< CapsuleCount
; ArrayNumber
++) {
127 CapsuleHeader
= CapsuleHeaderArray
[ArrayNumber
];
129 // Here should be in the boot-time for non-reset capsule image
130 // Platform specific update for the non-reset capsule image.
132 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_PERSIST_ACROSS_RESET
) == 0) {
133 if (EfiAtRuntime ()) {
134 Status
= EFI_OUT_OF_RESOURCES
;
136 Status
= ProcessCapsuleImage(CapsuleHeader
);
138 if (EFI_ERROR(Status
)) {
143 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_INITIATE_RESET
) != 0) {
144 InitiateReset
= TRUE
;
150 // After launching all capsules who has no reset flag, if no more capsules claims
151 // for a system reset just return.
158 // ScatterGatherList is only referenced if the capsules are defined to persist across
161 if (ScatterGatherList
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
) {
162 return EFI_INVALID_PARAMETER
;
166 // Check if the platform supports update capsule across a system reset
168 if (!IsPersistAcrossResetCapsuleSupported ()) {
169 return EFI_UNSUPPORTED
;
172 CapsuleCacheWriteBack (ScatterGatherList
);
175 // Construct variable name CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...
176 // if user calls UpdateCapsule multiple times.
178 StrCpyS (CapsuleVarName
, sizeof(CapsuleVarName
)/sizeof(CHAR16
), EFI_CAPSULE_VARIABLE_NAME
);
179 TempVarName
= CapsuleVarName
+ StrLen (CapsuleVarName
);
181 UnicodeValueToStringS (
183 sizeof (CapsuleVarName
) - ((UINTN
)TempVarName
- (UINTN
)CapsuleVarName
),
191 // ScatterGatherList is only referenced if the capsules are defined to persist across
192 // system reset. Set its value into NV storage to let pre-boot driver to pick it up
193 // after coming through a system reset.
195 Status
= EfiSetVariable (
197 &gEfiCapsuleVendorGuid
,
198 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
200 (VOID
*) &ScatterGatherList
202 if (!EFI_ERROR (Status
)) {
204 // Variable has been set successfully, increase variable index.
209 // Firmware that encounters a capsule which has the CAPSULE_FLAGS_INITIATE_RESET Flag set in its header
210 // will initiate a reset of the platform which is compatible with the passed-in capsule request and will
211 // not return back to the caller.
213 EfiResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
220 Returns if the capsule can be supported via UpdateCapsule().
222 @param CapsuleHeaderArray Virtual pointer to an array of virtual pointers to the capsules
223 being passed into update capsule.
224 @param CapsuleCount Number of pointers to EFI_CAPSULE_HEADER in
226 @param MaxiumCapsuleSize On output the maximum size that UpdateCapsule() can
227 support as an argument to UpdateCapsule() via
228 CapsuleHeaderArray and ScatterGatherList.
229 @param ResetType Returns the type of reset required for the capsule update.
231 @retval EFI_SUCCESS Valid answer returned.
232 @retval EFI_UNSUPPORTED The capsule image is not supported on this platform, and
233 MaximumCapsuleSize and ResetType are undefined.
234 @retval EFI_INVALID_PARAMETER MaximumCapsuleSize is NULL, or ResetTyep is NULL,
235 Or CapsuleCount is Zero, or CapsuleImage is not valid.
240 QueryCapsuleCapabilities (
241 IN EFI_CAPSULE_HEADER
**CapsuleHeaderArray
,
242 IN UINTN CapsuleCount
,
243 OUT UINT64
*MaxiumCapsuleSize
,
244 OUT EFI_RESET_TYPE
*ResetType
249 EFI_CAPSULE_HEADER
*CapsuleHeader
;
253 // Capsule Count can't be less than one.
255 if (CapsuleCount
< 1) {
256 return EFI_INVALID_PARAMETER
;
260 // Check whether input parameter is valid
262 if ((MaxiumCapsuleSize
== NULL
) ||(ResetType
== NULL
)) {
263 return EFI_INVALID_PARAMETER
;
266 CapsuleHeader
= NULL
;
269 for (ArrayNumber
= 0; ArrayNumber
< CapsuleCount
; ArrayNumber
++) {
270 CapsuleHeader
= CapsuleHeaderArray
[ArrayNumber
];
272 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have
273 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
275 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) {
276 return EFI_INVALID_PARAMETER
;
279 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have
280 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
282 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_INITIATE_RESET
)) == CAPSULE_FLAGS_INITIATE_RESET
) {
283 return EFI_INVALID_PARAMETER
;
287 // Check FMP capsule flag
289 if (CompareGuid(&CapsuleHeader
->CapsuleGuid
, &gEfiFmpCapsuleGuid
)
290 && (CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) != 0 ) {
291 return EFI_INVALID_PARAMETER
;
295 // Check Capsule image without populate flag is supported by firmware
297 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) == 0) {
298 Status
= SupportCapsuleImage (CapsuleHeader
);
299 if (EFI_ERROR(Status
)) {
306 // Find out whether there is any capsule defined to persist across system reset.
308 for (ArrayNumber
= 0; ArrayNumber
< CapsuleCount
; ArrayNumber
++) {
309 CapsuleHeader
= CapsuleHeaderArray
[ArrayNumber
];
310 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_PERSIST_ACROSS_RESET
) != 0) {
318 //Check if the platform supports update capsule across a system reset
320 if (!IsPersistAcrossResetCapsuleSupported ()) {
321 return EFI_UNSUPPORTED
;
323 *ResetType
= EfiResetWarm
;
324 *MaxiumCapsuleSize
= (UINT64
) mMaxSizePopulateCapsule
;
327 // For non-reset capsule image.
329 *ResetType
= EfiResetCold
;
330 *MaxiumCapsuleSize
= (UINT64
) mMaxSizeNonPopulateCapsule
;
339 This code installs UEFI capsule runtime service.
341 @param ImageHandle The firmware allocated handle for the EFI image.
342 @param SystemTable A pointer to the EFI System Table.
344 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully.
349 CapsuleServiceInitialize (
350 IN EFI_HANDLE ImageHandle
,
351 IN EFI_SYSTEM_TABLE
*SystemTable
356 mMaxSizePopulateCapsule
= PcdGet32(PcdMaxSizePopulateCapsule
);
357 mMaxSizeNonPopulateCapsule
= PcdGet32(PcdMaxSizeNonPopulateCapsule
);
360 // When PEI phase is IA32, DXE phase is X64, it is possible that capsule data are
361 // put above 4GB, so capsule PEI will transfer to long mode to get capsule data.
362 // The page table and stack is used to transfer processor mode from IA32 to long mode.
363 // Create the base address of page table and stack, and save them into variable.
364 // This is not needed when capsule with reset type is not supported.
366 SaveLongModeContext ();
369 // Install capsule runtime services into UEFI runtime service tables.
371 gRT
->UpdateCapsule
= UpdateCapsule
;
372 gRT
->QueryCapsuleCapabilities
= QueryCapsuleCapabilities
;
375 // Install the Capsule Architectural Protocol on a new handle
376 // to signify the capsule runtime services are ready.
378 Status
= gBS
->InstallMultipleProtocolInterfaces (
380 &gEfiCapsuleArchProtocolGuid
,
384 ASSERT_EFI_ERROR (Status
);