]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleService.c
1. Add "CAPSULE_FLAGS_INITIATE_RESET" flag support.
[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 - 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
12
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.
15
16 **/
17
18 #include <Uefi.h>
19
20 #include <Protocol/Capsule.h>
21 #include <Guid/CapsuleVendor.h>
22
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
31 //
32 // Handle for the installation of Capsule Architecture Protocol.
33 //
34 EFI_HANDLE mNewHandle = NULL;
35
36 /**
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.
42
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
46 CaspuleHeaderArray.
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.
50
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.
60
61 **/
62 EFI_STATUS
63 EFIAPI
64 UpdateCapsule (
65 IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,
66 IN UINTN CapsuleCount,
67 IN EFI_PHYSICAL_ADDRESS ScatterGatherList OPTIONAL
68 )
69 {
70 UINTN ArrayNumber;
71 EFI_STATUS Status;
72 EFI_CAPSULE_HEADER *CapsuleHeader;
73 BOOLEAN NeedReset;
74 BOOLEAN InitiateReset;
75
76 //
77 // Capsule Count can't be less than one.
78 //
79 if (CapsuleCount < 1) {
80 return EFI_INVALID_PARAMETER;
81 }
82
83 NeedReset = FALSE;
84 InitiateReset = FALSE;
85 CapsuleHeader = NULL;
86
87 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {
88 //
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.
91 //
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;
95 }
96 //
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.
99 //
100 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {
101 return EFI_INVALID_PARAMETER;
102 }
103 //
104 // Check Capsule image without populate flag by firmware support capsule function
105 //
106 if (((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) &&
107 (SupportCapsuleImage (CapsuleHeader) != EFI_SUCCESS)) {
108 return EFI_UNSUPPORTED;
109 }
110 }
111
112 //
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.
115 //
116 for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {
117 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
118 //
119 // Here should be in the boot-time for non-reset capsule image
120 // Platform specific update for the non-reset capsule image.
121 //
122 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) == 0) {
123 if (EfiAtRuntime ()) {
124 Status = EFI_UNSUPPORTED;
125 } else {
126 Status = ProcessCapsuleImage(CapsuleHeader);
127 }
128 if (EFI_ERROR(Status)) {
129 return Status;
130 }
131 } else {
132 NeedReset = TRUE;
133 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_INITIATE_RESET) != 0) {
134 InitiateReset = TRUE;
135 }
136 }
137 }
138
139 //
140 // After launching all capsules who has no reset flag, if no more capsules claims
141 // for a system reset just return.
142 //
143 if (!NeedReset) {
144 return EFI_SUCCESS;
145 }
146
147 //
148 // ScatterGatherList is only referenced if the capsules are defined to persist across
149 // system reset.
150 //
151 if (ScatterGatherList == (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {
152 return EFI_INVALID_PARAMETER;
153 }
154
155 //
156 // Check if the platform supports update capsule across a system reset
157 //
158 if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {
159 return EFI_UNSUPPORTED;
160 }
161
162 //
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.
166 //
167 Status = gRT->SetVariable (
168 EFI_CAPSULE_VARIABLE_NAME,
169 &gEfiCapsuleVendorGuid,
170 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,
171 sizeof (UINTN),
172 (VOID *) &ScatterGatherList
173 );
174 if (!EFI_ERROR (Status) && InitiateReset) {
175 //
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.
179 //
180 gRT->ResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);
181 }
182 return Status;
183 }
184
185 /**
186 Returns if the capsule can be supported via UpdateCapsule().
187
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
191 CaspuleHeaderArray.
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.
196
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.
202
203 **/
204 EFI_STATUS
205 EFIAPI
206 QueryCapsuleCapabilities (
207 IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,
208 IN UINTN CapsuleCount,
209 OUT UINT64 *MaxiumCapsuleSize,
210 OUT EFI_RESET_TYPE *ResetType
211 )
212 {
213 UINTN ArrayNumber;
214 EFI_CAPSULE_HEADER *CapsuleHeader;
215
216 //
217 // Capsule Count can't be less than one.
218 //
219 if (CapsuleCount < 1) {
220 return EFI_INVALID_PARAMETER;
221 }
222
223 //
224 // Check whether input parameter is valid
225 //
226 if ((MaxiumCapsuleSize == NULL) ||(ResetType == NULL)) {
227 return EFI_INVALID_PARAMETER;
228 }
229
230 CapsuleHeader = NULL;
231
232 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {
233 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
234 //
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.
237 //
238 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {
239 return EFI_INVALID_PARAMETER;
240 }
241 //
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.
244 //
245 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {
246 return EFI_INVALID_PARAMETER;
247 }
248 //
249 // Check Capsule image without populate flag is supported by firmware
250 //
251 if (((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) &&
252 (SupportCapsuleImage (CapsuleHeader) != EFI_SUCCESS)) {
253 return EFI_UNSUPPORTED;
254 }
255 }
256
257 //
258 // Assume that capsules have the same flags on reseting or not.
259 //
260 CapsuleHeader = CapsuleHeaderArray[0];
261 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {
262 //
263 //Check if the platform supports update capsule across a system reset
264 //
265 if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {
266 return EFI_UNSUPPORTED;
267 }
268 *ResetType = EfiResetWarm;
269 } else {
270 //
271 // For non-reset capsule image.
272 //
273 *ResetType = EfiResetCold;
274 }
275
276 //
277 // The support max capsule image size
278 //
279 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {
280 *MaxiumCapsuleSize = PcdGet32(PcdMaxSizePopulateCapsule);
281 } else {
282 *MaxiumCapsuleSize = PcdGet32(PcdMaxSizeNonPopulateCapsule);
283 }
284
285 return EFI_SUCCESS;
286 }
287
288
289 /**
290
291 This code installs UEFI capsule runtime service.
292
293 @param ImageHandle The firmware allocated handle for the EFI image.
294 @param SystemTable A pointer to the EFI System Table.
295
296 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully.
297
298 **/
299 EFI_STATUS
300 EFIAPI
301 CapsuleServiceInitialize (
302 IN EFI_HANDLE ImageHandle,
303 IN EFI_SYSTEM_TABLE *SystemTable
304 )
305 {
306 EFI_STATUS Status;
307
308 //
309 // Install capsule runtime services into UEFI runtime service tables.
310 //
311 gRT->UpdateCapsule = UpdateCapsule;
312 gRT->QueryCapsuleCapabilities = QueryCapsuleCapabilities;
313
314 //
315 // Install the Capsule Architectural Protocol on a new handle
316 // to signify the capsule runtime services are ready.
317 //
318 Status = gBS->InstallMultipleProtocolInterfaces (
319 &mNewHandle,
320 &gEfiCapsuleArchProtocolGuid,
321 NULL,
322 NULL
323 );
324 ASSERT_EFI_ERROR (Status);
325
326 return Status;
327 }