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 - 2010, Intel Corporation. All rights reserved.<BR>
8 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>
30 #include <Library/BaseLib.h>
31 #include <Library/PrintLib.h>
34 // Handle for the installation of Capsule Architecture Protocol.
36 EFI_HANDLE mNewHandle
= NULL
;
39 // The times of calling UpdateCapsule ()
44 Passes capsules to the firmware with both virtual and physical mapping. Depending on the intended
45 consumption, the firmware may process the capsule immediately. If the payload should persist
46 across a system reset, the reset value returned from EFI_QueryCapsuleCapabilities must
47 be passed into ResetSystem() and will cause the capsule to be processed by the firmware as
48 part of the reset process.
50 @param CapsuleHeaderArray Virtual pointer to an array of virtual pointers to the capsules
51 being passed into update capsule.
52 @param CapsuleCount Number of pointers to EFI_CAPSULE_HEADER in
54 @param ScatterGatherList Physical pointer to a set of
55 EFI_CAPSULE_BLOCK_DESCRIPTOR that describes the
56 location in physical memory of a set of capsules.
58 @retval EFI_SUCCESS Valid capsule was passed. If
59 CAPSULE_FLAGS_PERSIT_ACROSS_RESET is not set, the
60 capsule has been successfully processed by the firmware.
61 @retval EFI_DEVICE_ERROR The capsule update was started, but failed due to a device error.
62 @retval EFI_INVALID_PARAMETER CapsuleSize is NULL, or an incompatible set of flags were
63 set in the capsule header.
64 @retval EFI_INVALID_PARAMETER CapsuleCount is Zero.
65 @retval EFI_INVALID_PARAMETER For across reset capsule image, ScatterGatherList is NULL.
66 @retval EFI_UNSUPPORTED CapsuleImage is not recognized by the firmware.
72 IN EFI_CAPSULE_HEADER
**CapsuleHeaderArray
,
73 IN UINTN CapsuleCount
,
74 IN EFI_PHYSICAL_ADDRESS ScatterGatherList OPTIONAL
79 EFI_CAPSULE_HEADER
*CapsuleHeader
;
81 BOOLEAN InitiateReset
;
82 CHAR16 CapsuleVarName
[30];
86 // Capsule Count can't be less than one.
88 if (CapsuleCount
< 1) {
89 return EFI_INVALID_PARAMETER
;
93 InitiateReset
= FALSE
;
95 CapsuleVarName
[0] = 0;
97 for (ArrayNumber
= 0; ArrayNumber
< CapsuleCount
; ArrayNumber
++) {
99 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have
100 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
102 CapsuleHeader
= CapsuleHeaderArray
[ArrayNumber
];
103 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) {
104 return EFI_INVALID_PARAMETER
;
107 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have
108 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
110 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_INITIATE_RESET
)) == CAPSULE_FLAGS_INITIATE_RESET
) {
111 return EFI_INVALID_PARAMETER
;
114 // Check Capsule image without populate flag by firmware support capsule function
116 if (((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) == 0) &&
117 (SupportCapsuleImage (CapsuleHeader
) != EFI_SUCCESS
)) {
118 return EFI_UNSUPPORTED
;
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_UNSUPPORTED
;
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 (!FeaturePcdGet(PcdSupportUpdateCapsuleReset
)) {
169 return EFI_UNSUPPORTED
;
173 // Construct variable name CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...
174 // if user calls UpdateCapsule multiple times.
176 StrCpy (CapsuleVarName
, EFI_CAPSULE_VARIABLE_NAME
);
177 TempVarName
= CapsuleVarName
+ StrLen (CapsuleVarName
);
179 UnicodeValueToString (TempVarName
, 0, mTimes
, 0);
183 // ScatterGatherList is only referenced if the capsules are defined to persist across
184 // system reset. Set its value into NV storage to let pre-boot driver to pick it up
185 // after coming through a system reset.
187 Status
= EfiSetVariable (
189 &gEfiCapsuleVendorGuid
,
190 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_RUNTIME_ACCESS
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
192 (VOID
*) &ScatterGatherList
194 if (!EFI_ERROR (Status
)) {
196 // Variable has been set successfully, increase variable index.
201 // Firmware that encounters a capsule which has the CAPSULE_FLAGS_INITIATE_RESET Flag set in its header
202 // will initiate a reset of the platform which is compatible with the passed-in capsule request and will
203 // not return back to the caller.
205 EfiResetSystem (EfiResetWarm
, EFI_SUCCESS
, 0, NULL
);
212 Returns if the capsule can be supported via UpdateCapsule().
214 @param CapsuleHeaderArray Virtual pointer to an array of virtual pointers to the capsules
215 being passed into update capsule.
216 @param CapsuleCount Number of pointers to EFI_CAPSULE_HEADER in
218 @param MaxiumCapsuleSize On output the maximum size that UpdateCapsule() can
219 support as an argument to UpdateCapsule() via
220 CapsuleHeaderArray and ScatterGatherList.
221 @param ResetType Returns the type of reset required for the capsule update.
223 @retval EFI_SUCCESS Valid answer returned.
224 @retval EFI_UNSUPPORTED The capsule image is not supported on this platform, and
225 MaximumCapsuleSize and ResetType are undefined.
226 @retval EFI_INVALID_PARAMETER MaximumCapsuleSize is NULL, or ResetTyep is NULL,
227 Or CapsuleCount is Zero, or CapsuleImage is not valid.
232 QueryCapsuleCapabilities (
233 IN EFI_CAPSULE_HEADER
**CapsuleHeaderArray
,
234 IN UINTN CapsuleCount
,
235 OUT UINT64
*MaxiumCapsuleSize
,
236 OUT EFI_RESET_TYPE
*ResetType
240 EFI_CAPSULE_HEADER
*CapsuleHeader
;
243 // Capsule Count can't be less than one.
245 if (CapsuleCount
< 1) {
246 return EFI_INVALID_PARAMETER
;
250 // Check whether input parameter is valid
252 if ((MaxiumCapsuleSize
== NULL
) ||(ResetType
== NULL
)) {
253 return EFI_INVALID_PARAMETER
;
256 CapsuleHeader
= NULL
;
258 for (ArrayNumber
= 0; ArrayNumber
< CapsuleCount
; ArrayNumber
++) {
259 CapsuleHeader
= CapsuleHeaderArray
[ArrayNumber
];
261 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have
262 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
264 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) {
265 return EFI_INVALID_PARAMETER
;
268 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have
269 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
271 if ((CapsuleHeader
->Flags
& (CAPSULE_FLAGS_PERSIST_ACROSS_RESET
| CAPSULE_FLAGS_INITIATE_RESET
)) == CAPSULE_FLAGS_INITIATE_RESET
) {
272 return EFI_INVALID_PARAMETER
;
275 // Check Capsule image without populate flag is supported by firmware
277 if (((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) == 0) &&
278 (SupportCapsuleImage (CapsuleHeader
) != EFI_SUCCESS
)) {
279 return EFI_UNSUPPORTED
;
284 // Assume that capsules have the same flags on reseting or not.
286 CapsuleHeader
= CapsuleHeaderArray
[0];
287 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_PERSIST_ACROSS_RESET
) != 0) {
289 //Check if the platform supports update capsule across a system reset
291 if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset
)) {
292 return EFI_UNSUPPORTED
;
294 *ResetType
= EfiResetWarm
;
297 // For non-reset capsule image.
299 *ResetType
= EfiResetCold
;
303 // The support max capsule image size
305 if ((CapsuleHeader
->Flags
& CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE
) != 0) {
306 *MaxiumCapsuleSize
= PcdGet32(PcdMaxSizePopulateCapsule
);
308 *MaxiumCapsuleSize
= PcdGet32(PcdMaxSizeNonPopulateCapsule
);
317 This code installs UEFI capsule runtime service.
319 @param ImageHandle The firmware allocated handle for the EFI image.
320 @param SystemTable A pointer to the EFI System Table.
322 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully.
327 CapsuleServiceInitialize (
328 IN EFI_HANDLE ImageHandle
,
329 IN EFI_SYSTEM_TABLE
*SystemTable
335 // Install capsule runtime services into UEFI runtime service tables.
337 gRT
->UpdateCapsule
= UpdateCapsule
;
338 gRT
->QueryCapsuleCapabilities
= QueryCapsuleCapabilities
;
341 // Install the Capsule Architectural Protocol on a new handle
342 // to signify the capsule runtime services are ready.
344 Status
= gBS
->InstallMultipleProtocolInterfaces (
346 &gEfiCapsuleArchProtocolGuid
,
350 ASSERT_EFI_ERROR (Status
);