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 - 2009, Intel Corporation. <BR>
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 #include <Protocol/Capsule.h>
21 #include <Guid/CapsuleVendor.h>
23 #include <Library/DebugLib.h>
24 #include <Library/PcdLib.h>
25 #include <Library/CapsuleLib.h>
26 #include <Library/UefiDriverEntryPoint.h>
27 #include <Library/UefiBootServicesTableLib.h>
28 #include <Library/UefiRuntimeServicesTableLib.h>
29 #include <Library/UefiRuntimeLib.h>
32 // Handle for the installation of Capsule Architecture Protocol.
34 EFI_HANDLE mNewHandle
= NULL
;
37 Passes capsules to the firmware with both virtual and physical mapping. Depending on the intended
38 consumption, the firmware may process the capsule immediately. If the payload should persist
39 across a system reset, the reset value returned from EFI_QueryCapsuleCapabilities must
40 be passed into ResetSystem() and will cause the capsule to be processed by the firmware as
41 part of the reset process.
43 @param CapsuleHeaderArray Virtual pointer to an array of virtual pointers to the capsules
44 being passed into update capsule.
45 @param CapsuleCount Number of pointers to EFI_CAPSULE_HEADER in
47 @param ScatterGatherList Physical pointer to a set of
48 EFI_CAPSULE_BLOCK_DESCRIPTOR that describes the
49 location in physical memory of a set of capsules.
51 @retval EFI_SUCCESS Valid capsule was passed. If
52 CAPSULE_FLAGS_PERSIT_ACROSS_RESET is not set, the
53 capsule has been successfully processed by the firmware.
54 @retval EFI_DEVICE_ERROR The capsule update was started, but failed due to a device error.
55 @retval EFI_INVALID_PARAMETER CapsuleSize is NULL, or an incompatible set of flags were
56 set in the capsule header.
57 @retval EFI_INVALID_PARAMETER CapsuleCount is Zero.
58 @retval EFI_INVALID_PARAMETER For across reset capsule image, ScatterGatherList is NULL.
59 @retval EFI_UNSUPPORTED CapsuleImage is not recognized by the firmware.
65 IN EFI_CAPSULE_HEADER
**CapsuleHeaderArray
,
66 IN UINTN CapsuleCount
,
67 IN EFI_PHYSICAL_ADDRESS ScatterGatherList OPTIONAL
72 EFI_CAPSULE_HEADER
*CapsuleHeader
;
74 BOOLEAN InitiateReset
;
77 // Capsule Count can't be less than one.
79 if (CapsuleCount
< 1) {
80 return EFI_INVALID_PARAMETER
;
84 InitiateReset
= FALSE
;
87 for (ArrayNumber
= 0; ArrayNumber
< CapsuleCount
; ArrayNumber
++) {
89 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have
90 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
92 CapsuleHeader
= CapsuleHeaderArray
[ArrayNumber
];
93 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) {
94 return EFI_INVALID_PARAMETER
;
97 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have
98 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
100 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_INITIATE_RESET
)) == CAPSULE_FLAGS_INITIATE_RESET
) {
101 return EFI_INVALID_PARAMETER
;
104 // Check Capsule image without populate flag by firmware support capsule function
106 if (((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) == 0) &&
107 (SupportCapsuleImage (CapsuleHeader
) != EFI_SUCCESS
)) {
108 return EFI_UNSUPPORTED
;
113 // Walk through all capsules, record whether there is a capsule needs reset
114 // or initiate reset. And then process capsules which has no reset flag directly.
116 for (ArrayNumber
= 0; ArrayNumber
< CapsuleCount
; ArrayNumber
++) {
117 CapsuleHeader
= CapsuleHeaderArray
[ArrayNumber
];
119 // Here should be in the boot-time for non-reset capsule image
120 // Platform specific update for the non-reset capsule image.
122 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_PERSIST_ACROSS_RESET
) == 0) {
123 if (EfiAtRuntime ()) {
124 Status
= EFI_UNSUPPORTED
;
126 Status
= ProcessCapsuleImage(CapsuleHeader
);
128 if (EFI_ERROR(Status
)) {
133 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_INITIATE_RESET
) != 0) {
134 InitiateReset
= TRUE
;
140 // After launching all capsules who has no reset flag, if no more capsules claims
141 // for a system reset just return.
148 // ScatterGatherList is only referenced if the capsules are defined to persist across
151 if (ScatterGatherList
== (EFI_PHYSICAL_ADDRESS
) (UINTN
) NULL
) {
152 return EFI_INVALID_PARAMETER
;
156 // Check if the platform supports update capsule across a system reset
158 if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset
)) {
159 return EFI_UNSUPPORTED
;
163 // ScatterGatherList is only referenced if the capsules are defined to persist across
164 // system reset. Set its value into NV storage to let pre-boot driver to pick it up
165 // after coming through a system reset.
167 Status
= gRT
->SetVariable (
168 EFI_CAPSULE_VARIABLE_NAME
,
169 &gEfiCapsuleVendorGuid
,
170 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
172 (VOID
*) &ScatterGatherList
174 if (!EFI_ERROR (Status
) && InitiateReset
) {
176 // Firmware that encounters a capsule which has the CAPSULE_FLAGS_INITIATE_RESET Flag set in its header
177 // will initiate a reset of the platform which is compatible with the passed-in capsule request and will
178 // not return back to the caller.
180 gRT
->ResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
186 Returns if the capsule can be supported via UpdateCapsule().
188 @param CapsuleHeaderArray Virtual pointer to an array of virtual pointers to the capsules
189 being passed into update capsule.
190 @param CapsuleCount Number of pointers to EFI_CAPSULE_HEADER in
192 @param MaxiumCapsuleSize On output the maximum size that UpdateCapsule() can
193 support as an argument to UpdateCapsule() via
194 CapsuleHeaderArray and ScatterGatherList.
195 @param ResetType Returns the type of reset required for the capsule update.
197 @retval EFI_SUCCESS Valid answer returned.
198 @retval EFI_UNSUPPORTED The capsule image is not supported on this platform, and
199 MaximumCapsuleSize and ResetType are undefined.
200 @retval EFI_INVALID_PARAMETER MaximumCapsuleSize is NULL, or ResetTyep is NULL,
201 Or CapsuleCount is Zero, or CapsuleImage is not valid.
206 QueryCapsuleCapabilities (
207 IN EFI_CAPSULE_HEADER
**CapsuleHeaderArray
,
208 IN UINTN CapsuleCount
,
209 OUT UINT64
*MaxiumCapsuleSize
,
210 OUT EFI_RESET_TYPE
*ResetType
214 EFI_CAPSULE_HEADER
*CapsuleHeader
;
217 // Capsule Count can't be less than one.
219 if (CapsuleCount
< 1) {
220 return EFI_INVALID_PARAMETER
;
224 // Check whether input parameter is valid
226 if ((MaxiumCapsuleSize
== NULL
) ||(ResetType
== NULL
)) {
227 return EFI_INVALID_PARAMETER
;
230 CapsuleHeader
= NULL
;
232 for (ArrayNumber
= 0; ArrayNumber
< CapsuleCount
; ArrayNumber
++) {
233 CapsuleHeader
= CapsuleHeaderArray
[ArrayNumber
];
235 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have
236 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
238 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) {
239 return EFI_INVALID_PARAMETER
;
242 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have
243 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
245 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_INITIATE_RESET
)) == CAPSULE_FLAGS_INITIATE_RESET
) {
246 return EFI_INVALID_PARAMETER
;
249 // Check Capsule image without populate flag is supported by firmware
251 if (((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) == 0) &&
252 (SupportCapsuleImage (CapsuleHeader
) != EFI_SUCCESS
)) {
253 return EFI_UNSUPPORTED
;
258 // Assume that capsules have the same flags on reseting or not.
260 CapsuleHeader
= CapsuleHeaderArray
[0];
261 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_PERSIST_ACROSS_RESET
) != 0) {
263 //Check if the platform supports update capsule across a system reset
265 if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset
)) {
266 return EFI_UNSUPPORTED
;
268 *ResetType
= EfiResetWarm
;
271 // For non-reset capsule image.
273 *ResetType
= EfiResetCold
;
277 // The support max capsule image size
279 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) != 0) {
280 *MaxiumCapsuleSize
= PcdGet32(PcdMaxSizePopulateCapsule
);
282 *MaxiumCapsuleSize
= PcdGet32(PcdMaxSizeNonPopulateCapsule
);
291 This code installs UEFI capsule runtime service.
293 @param ImageHandle The firmware allocated handle for the EFI image.
294 @param SystemTable A pointer to the EFI System Table.
296 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully.
301 CapsuleServiceInitialize (
302 IN EFI_HANDLE ImageHandle
,
303 IN EFI_SYSTEM_TABLE
*SystemTable
309 // Install capsule runtime services into UEFI runtime service tables.
311 gRT
->UpdateCapsule
= UpdateCapsule
;
312 gRT
->QueryCapsuleCapabilities
= QueryCapsuleCapabilities
;
315 // Install the Capsule Architectural Protocol on a new handle
316 // to signify the capsule runtime services are ready.
318 Status
= gBS
->InstallMultipleProtocolInterfaces (
320 &gEfiCapsuleArchProtocolGuid
,
324 ASSERT_EFI_ERROR (Status
);