]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleService.c
Update UpdateCapsule() service return status to follow UEFI2.3.1 D.
[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 - 2013, 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
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 #include <Library/BaseLib.h>
31 #include <Library/PrintLib.h>
32
33 //
34 // Handle for the installation of Capsule Architecture Protocol.
35 //
36 EFI_HANDLE mNewHandle = NULL;
37
38 //
39 // The times of calling UpdateCapsule ()
40 //
41 UINTN mTimes = 0;
42
43 /**
44 Create the variable to save the base address of page table and stack
45 for transferring into long mode in IA32 PEI.
46 **/
47 VOID
48 SaveLongModeContext (
49 VOID
50 );
51
52 /**
53 Passes capsules to the firmware with both virtual and physical mapping. Depending on the intended
54 consumption, the firmware may process the capsule immediately. If the payload should persist
55 across a system reset, the reset value returned from EFI_QueryCapsuleCapabilities must
56 be passed into ResetSystem() and will cause the capsule to be processed by the firmware as
57 part of the reset process.
58
59 @param CapsuleHeaderArray Virtual pointer to an array of virtual pointers to the capsules
60 being passed into update capsule.
61 @param CapsuleCount Number of pointers to EFI_CAPSULE_HEADER in
62 CaspuleHeaderArray.
63 @param ScatterGatherList Physical pointer to a set of
64 EFI_CAPSULE_BLOCK_DESCRIPTOR that describes the
65 location in physical memory of a set of capsules.
66
67 @retval EFI_SUCCESS Valid capsule was passed. If
68 CAPSULE_FLAGS_PERSIT_ACROSS_RESET is not set, the
69 capsule has been successfully processed by the firmware.
70 @retval EFI_DEVICE_ERROR The capsule update was started, but failed due to a device error.
71 @retval EFI_INVALID_PARAMETER CapsuleSize is NULL, or an incompatible set of flags were
72 set in the capsule header.
73 @retval EFI_INVALID_PARAMETER CapsuleCount is Zero.
74 @retval EFI_INVALID_PARAMETER For across reset capsule image, ScatterGatherList is NULL.
75 @retval EFI_UNSUPPORTED CapsuleImage is not recognized by the firmware.
76 @retval EFI_OUT_OF_RESOURCES When ExitBootServices() has been previously called this error indicates the capsule
77 is compatible with this platform but is not capable of being submitted or processed
78 in runtime. The caller may resubmit the capsule prior to ExitBootServices().
79 @retval EFI_OUT_OF_RESOURCES When ExitBootServices() has not been previously called then this error indicates
80 the capsule is compatible with this platform but there are insufficient resources to process.
81
82 **/
83 EFI_STATUS
84 EFIAPI
85 UpdateCapsule (
86 IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,
87 IN UINTN CapsuleCount,
88 IN EFI_PHYSICAL_ADDRESS ScatterGatherList OPTIONAL
89 )
90 {
91 UINTN ArrayNumber;
92 EFI_STATUS Status;
93 EFI_CAPSULE_HEADER *CapsuleHeader;
94 BOOLEAN NeedReset;
95 BOOLEAN InitiateReset;
96 CHAR16 CapsuleVarName[30];
97 CHAR16 *TempVarName;
98
99 //
100 // Capsule Count can't be less than one.
101 //
102 if (CapsuleCount < 1) {
103 return EFI_INVALID_PARAMETER;
104 }
105
106 NeedReset = FALSE;
107 InitiateReset = FALSE;
108 CapsuleHeader = NULL;
109 CapsuleVarName[0] = 0;
110
111 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {
112 //
113 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have
114 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
115 //
116 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
117 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {
118 return EFI_INVALID_PARAMETER;
119 }
120 //
121 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have
122 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
123 //
124 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {
125 return EFI_INVALID_PARAMETER;
126 }
127 //
128 // Check Capsule image without populate flag by firmware support capsule function
129 //
130 if (((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) &&
131 (SupportCapsuleImage (CapsuleHeader) != EFI_SUCCESS)) {
132 return EFI_UNSUPPORTED;
133 }
134 }
135
136 //
137 // Walk through all capsules, record whether there is a capsule needs reset
138 // or initiate reset. And then process capsules which has no reset flag directly.
139 //
140 for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {
141 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
142 //
143 // Here should be in the boot-time for non-reset capsule image
144 // Platform specific update for the non-reset capsule image.
145 //
146 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) == 0) {
147 if (EfiAtRuntime ()) {
148 Status = EFI_OUT_OF_RESOURCES;
149 } else {
150 Status = ProcessCapsuleImage(CapsuleHeader);
151 }
152 if (EFI_ERROR(Status)) {
153 return Status;
154 }
155 } else {
156 NeedReset = TRUE;
157 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_INITIATE_RESET) != 0) {
158 InitiateReset = TRUE;
159 }
160 }
161 }
162
163 //
164 // After launching all capsules who has no reset flag, if no more capsules claims
165 // for a system reset just return.
166 //
167 if (!NeedReset) {
168 return EFI_SUCCESS;
169 }
170
171 //
172 // ScatterGatherList is only referenced if the capsules are defined to persist across
173 // system reset.
174 //
175 if (ScatterGatherList == (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {
176 return EFI_INVALID_PARAMETER;
177 }
178
179 //
180 // Check if the platform supports update capsule across a system reset
181 //
182 if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {
183 return EFI_UNSUPPORTED;
184 }
185
186 //
187 // Construct variable name CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...
188 // if user calls UpdateCapsule multiple times.
189 //
190 StrCpy (CapsuleVarName, EFI_CAPSULE_VARIABLE_NAME);
191 TempVarName = CapsuleVarName + StrLen (CapsuleVarName);
192 if (mTimes > 0) {
193 UnicodeValueToString (TempVarName, 0, mTimes, 0);
194 }
195
196 //
197 // ScatterGatherList is only referenced if the capsules are defined to persist across
198 // system reset. Set its value into NV storage to let pre-boot driver to pick it up
199 // after coming through a system reset.
200 //
201 Status = EfiSetVariable (
202 CapsuleVarName,
203 &gEfiCapsuleVendorGuid,
204 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,
205 sizeof (UINTN),
206 (VOID *) &ScatterGatherList
207 );
208 if (!EFI_ERROR (Status)) {
209 //
210 // Variable has been set successfully, increase variable index.
211 //
212 mTimes++;
213 if(InitiateReset) {
214 //
215 // Firmware that encounters a capsule which has the CAPSULE_FLAGS_INITIATE_RESET Flag set in its header
216 // will initiate a reset of the platform which is compatible with the passed-in capsule request and will
217 // not return back to the caller.
218 //
219 EfiResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);
220 }
221 }
222 return Status;
223 }
224
225 /**
226 Returns if the capsule can be supported via UpdateCapsule().
227
228 @param CapsuleHeaderArray Virtual pointer to an array of virtual pointers to the capsules
229 being passed into update capsule.
230 @param CapsuleCount Number of pointers to EFI_CAPSULE_HEADER in
231 CaspuleHeaderArray.
232 @param MaxiumCapsuleSize On output the maximum size that UpdateCapsule() can
233 support as an argument to UpdateCapsule() via
234 CapsuleHeaderArray and ScatterGatherList.
235 @param ResetType Returns the type of reset required for the capsule update.
236
237 @retval EFI_SUCCESS Valid answer returned.
238 @retval EFI_UNSUPPORTED The capsule image is not supported on this platform, and
239 MaximumCapsuleSize and ResetType are undefined.
240 @retval EFI_INVALID_PARAMETER MaximumCapsuleSize is NULL, or ResetTyep is NULL,
241 Or CapsuleCount is Zero, or CapsuleImage is not valid.
242
243 **/
244 EFI_STATUS
245 EFIAPI
246 QueryCapsuleCapabilities (
247 IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,
248 IN UINTN CapsuleCount,
249 OUT UINT64 *MaxiumCapsuleSize,
250 OUT EFI_RESET_TYPE *ResetType
251 )
252 {
253 UINTN ArrayNumber;
254 EFI_CAPSULE_HEADER *CapsuleHeader;
255 BOOLEAN NeedReset;
256
257 //
258 // Capsule Count can't be less than one.
259 //
260 if (CapsuleCount < 1) {
261 return EFI_INVALID_PARAMETER;
262 }
263
264 //
265 // Check whether input parameter is valid
266 //
267 if ((MaxiumCapsuleSize == NULL) ||(ResetType == NULL)) {
268 return EFI_INVALID_PARAMETER;
269 }
270
271 CapsuleHeader = NULL;
272 NeedReset = FALSE;
273
274 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {
275 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
276 //
277 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have
278 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
279 //
280 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {
281 return EFI_INVALID_PARAMETER;
282 }
283 //
284 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have
285 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
286 //
287 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {
288 return EFI_INVALID_PARAMETER;
289 }
290 //
291 // Check Capsule image without populate flag is supported by firmware
292 //
293 if (((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) &&
294 (SupportCapsuleImage (CapsuleHeader) != EFI_SUCCESS)) {
295 return EFI_UNSUPPORTED;
296 }
297 }
298
299 //
300 // Find out whether there is any capsule defined to persist across system reset.
301 //
302 for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {
303 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
304 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {
305 NeedReset = TRUE;
306 break;
307 }
308 }
309
310 if (NeedReset) {
311 //
312 //Check if the platform supports update capsule across a system reset
313 //
314 if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {
315 return EFI_UNSUPPORTED;
316 }
317 *ResetType = EfiResetWarm;
318 *MaxiumCapsuleSize = FixedPcdGet32(PcdMaxSizePopulateCapsule);
319 } else {
320 //
321 // For non-reset capsule image.
322 //
323 *ResetType = EfiResetCold;
324 *MaxiumCapsuleSize = FixedPcdGet32(PcdMaxSizeNonPopulateCapsule);
325 }
326
327 return EFI_SUCCESS;
328 }
329
330
331 /**
332
333 This code installs UEFI capsule runtime service.
334
335 @param ImageHandle The firmware allocated handle for the EFI image.
336 @param SystemTable A pointer to the EFI System Table.
337
338 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully.
339
340 **/
341 EFI_STATUS
342 EFIAPI
343 CapsuleServiceInitialize (
344 IN EFI_HANDLE ImageHandle,
345 IN EFI_SYSTEM_TABLE *SystemTable
346 )
347 {
348 EFI_STATUS Status;
349
350 //
351 // When PEI phase is IA32, DXE phase is X64, it is possible that capsule data are
352 // put above 4GB, so capsule PEI will transfer to long mode to get capsule data.
353 // The page table and stack is used to transfer processor mode from IA32 to long mode.
354 // Create the base address of page table and stack, and save them into variable.
355 // This is not needed when capsule with reset type is not supported.
356 //
357 SaveLongModeContext ();
358
359 //
360 // Install capsule runtime services into UEFI runtime service tables.
361 //
362 gRT->UpdateCapsule = UpdateCapsule;
363 gRT->QueryCapsuleCapabilities = QueryCapsuleCapabilities;
364
365 //
366 // Install the Capsule Architectural Protocol on a new handle
367 // to signify the capsule runtime services are ready.
368 //
369 Status = gBS->InstallMultipleProtocolInterfaces (
370 &mNewHandle,
371 &gEfiCapsuleArchProtocolGuid,
372 NULL,
373 NULL
374 );
375 ASSERT_EFI_ERROR (Status);
376
377 return Status;
378 }