]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleService.c
MdeModulePkg/CapsuleRuntimeDxe: clean the capsule payload to DRAM
[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 - 2017, 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 "CapsuleService.h"
19
20 //
21 // Handle for the installation of Capsule Architecture Protocol.
22 //
23 EFI_HANDLE mNewHandle = NULL;
24
25 //
26 // The times of calling UpdateCapsule ()
27 //
28 UINTN mTimes = 0;
29
30 UINT32 mMaxSizePopulateCapsule = 0;
31 UINT32 mMaxSizeNonPopulateCapsule = 0;
32
33 /**
34 Passes capsules to the firmware with both virtual and physical mapping. Depending on the intended
35 consumption, the firmware may process the capsule immediately. If the payload should persist
36 across a system reset, the reset value returned from EFI_QueryCapsuleCapabilities must
37 be passed into ResetSystem() and will cause the capsule to be processed by the firmware as
38 part of the reset process.
39
40 @param CapsuleHeaderArray Virtual pointer to an array of virtual pointers to the capsules
41 being passed into update capsule.
42 @param CapsuleCount Number of pointers to EFI_CAPSULE_HEADER in
43 CaspuleHeaderArray.
44 @param ScatterGatherList Physical pointer to a set of
45 EFI_CAPSULE_BLOCK_DESCRIPTOR that describes the
46 location in physical memory of a set of capsules.
47
48 @retval EFI_SUCCESS Valid capsule was passed. If
49 CAPSULE_FLAGS_PERSIT_ACROSS_RESET is not set, the
50 capsule has been successfully processed by the firmware.
51 @retval EFI_DEVICE_ERROR The capsule update was started, but failed due to a device error.
52 @retval EFI_INVALID_PARAMETER CapsuleSize is NULL, or an incompatible set of flags were
53 set in the capsule header.
54 @retval EFI_INVALID_PARAMETER CapsuleCount is Zero.
55 @retval EFI_INVALID_PARAMETER For across reset capsule image, ScatterGatherList is NULL.
56 @retval EFI_UNSUPPORTED CapsuleImage is not recognized by the firmware.
57 @retval EFI_OUT_OF_RESOURCES When ExitBootServices() has been previously called this error indicates the capsule
58 is compatible with this platform but is not capable of being submitted or processed
59 in runtime. The caller may resubmit the capsule prior to ExitBootServices().
60 @retval EFI_OUT_OF_RESOURCES When ExitBootServices() has not been previously called then this error indicates
61 the capsule is compatible with this platform but there are insufficient resources to process.
62
63 **/
64 EFI_STATUS
65 EFIAPI
66 UpdateCapsule (
67 IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,
68 IN UINTN CapsuleCount,
69 IN EFI_PHYSICAL_ADDRESS ScatterGatherList OPTIONAL
70 )
71 {
72 UINTN ArrayNumber;
73 EFI_STATUS Status;
74 EFI_CAPSULE_HEADER *CapsuleHeader;
75 BOOLEAN NeedReset;
76 BOOLEAN InitiateReset;
77 CHAR16 CapsuleVarName[30];
78 CHAR16 *TempVarName;
79
80 //
81 // Capsule Count can't be less than one.
82 //
83 if (CapsuleCount < 1) {
84 return EFI_INVALID_PARAMETER;
85 }
86
87 NeedReset = FALSE;
88 InitiateReset = FALSE;
89 CapsuleHeader = NULL;
90 CapsuleVarName[0] = 0;
91
92 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {
93 //
94 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have
95 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
96 //
97 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
98 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {
99 return EFI_INVALID_PARAMETER;
100 }
101 //
102 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have
103 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
104 //
105 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {
106 return EFI_INVALID_PARAMETER;
107 }
108
109 //
110 // Check FMP capsule flag
111 //
112 if (CompareGuid(&CapsuleHeader->CapsuleGuid, &gEfiFmpCapsuleGuid)
113 && (CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0 ) {
114 return EFI_INVALID_PARAMETER;
115 }
116
117 //
118 // Check Capsule image without populate flag by firmware support capsule function
119 //
120 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {
121 Status = SupportCapsuleImage (CapsuleHeader);
122 if (EFI_ERROR(Status)) {
123 return Status;
124 }
125 }
126 }
127
128 //
129 // Walk through all capsules, record whether there is a capsule needs reset
130 // or initiate reset. And then process capsules which has no reset flag directly.
131 //
132 for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {
133 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
134 //
135 // Here should be in the boot-time for non-reset capsule image
136 // Platform specific update for the non-reset capsule image.
137 //
138 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) == 0) {
139 if (EfiAtRuntime ()) {
140 Status = EFI_OUT_OF_RESOURCES;
141 } else {
142 Status = ProcessCapsuleImage(CapsuleHeader);
143 }
144 if (EFI_ERROR(Status)) {
145 return Status;
146 }
147 } else {
148 NeedReset = TRUE;
149 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_INITIATE_RESET) != 0) {
150 InitiateReset = TRUE;
151 }
152 }
153 }
154
155 //
156 // After launching all capsules who has no reset flag, if no more capsules claims
157 // for a system reset just return.
158 //
159 if (!NeedReset) {
160 return EFI_SUCCESS;
161 }
162
163 //
164 // ScatterGatherList is only referenced if the capsules are defined to persist across
165 // system reset.
166 //
167 if (ScatterGatherList == (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {
168 return EFI_INVALID_PARAMETER;
169 }
170
171 //
172 // Check if the platform supports update capsule across a system reset
173 //
174 if (!IsPersistAcrossResetCapsuleSupported ()) {
175 return EFI_UNSUPPORTED;
176 }
177
178 CapsuleCacheWriteBack (ScatterGatherList);
179
180 //
181 // Construct variable name CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...
182 // if user calls UpdateCapsule multiple times.
183 //
184 StrCpyS (CapsuleVarName, sizeof(CapsuleVarName)/sizeof(CHAR16), EFI_CAPSULE_VARIABLE_NAME);
185 TempVarName = CapsuleVarName + StrLen (CapsuleVarName);
186 if (mTimes > 0) {
187 UnicodeValueToStringS (
188 TempVarName,
189 sizeof (CapsuleVarName) - ((UINTN)TempVarName - (UINTN)CapsuleVarName),
190 0,
191 mTimes,
192 0
193 );
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 EFI_STATUS Status;
254 UINTN ArrayNumber;
255 EFI_CAPSULE_HEADER *CapsuleHeader;
256 BOOLEAN NeedReset;
257
258 //
259 // Capsule Count can't be less than one.
260 //
261 if (CapsuleCount < 1) {
262 return EFI_INVALID_PARAMETER;
263 }
264
265 //
266 // Check whether input parameter is valid
267 //
268 if ((MaxiumCapsuleSize == NULL) ||(ResetType == NULL)) {
269 return EFI_INVALID_PARAMETER;
270 }
271
272 CapsuleHeader = NULL;
273 NeedReset = FALSE;
274
275 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {
276 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
277 //
278 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have
279 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
280 //
281 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {
282 return EFI_INVALID_PARAMETER;
283 }
284 //
285 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have
286 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
287 //
288 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {
289 return EFI_INVALID_PARAMETER;
290 }
291
292 //
293 // Check FMP capsule flag
294 //
295 if (CompareGuid(&CapsuleHeader->CapsuleGuid, &gEfiFmpCapsuleGuid)
296 && (CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0 ) {
297 return EFI_INVALID_PARAMETER;
298 }
299
300 //
301 // Check Capsule image without populate flag is supported by firmware
302 //
303 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {
304 Status = SupportCapsuleImage (CapsuleHeader);
305 if (EFI_ERROR(Status)) {
306 return Status;
307 }
308 }
309 }
310
311 //
312 // Find out whether there is any capsule defined to persist across system reset.
313 //
314 for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {
315 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
316 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {
317 NeedReset = TRUE;
318 break;
319 }
320 }
321
322 if (NeedReset) {
323 //
324 //Check if the platform supports update capsule across a system reset
325 //
326 if (!IsPersistAcrossResetCapsuleSupported ()) {
327 return EFI_UNSUPPORTED;
328 }
329 *ResetType = EfiResetWarm;
330 *MaxiumCapsuleSize = (UINT64) mMaxSizePopulateCapsule;
331 } else {
332 //
333 // For non-reset capsule image.
334 //
335 *ResetType = EfiResetCold;
336 *MaxiumCapsuleSize = (UINT64) mMaxSizeNonPopulateCapsule;
337 }
338
339 return EFI_SUCCESS;
340 }
341
342
343 /**
344
345 This code installs UEFI capsule runtime service.
346
347 @param ImageHandle The firmware allocated handle for the EFI image.
348 @param SystemTable A pointer to the EFI System Table.
349
350 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully.
351
352 **/
353 EFI_STATUS
354 EFIAPI
355 CapsuleServiceInitialize (
356 IN EFI_HANDLE ImageHandle,
357 IN EFI_SYSTEM_TABLE *SystemTable
358 )
359 {
360 EFI_STATUS Status;
361
362 mMaxSizePopulateCapsule = PcdGet32(PcdMaxSizePopulateCapsule);
363 mMaxSizeNonPopulateCapsule = PcdGet32(PcdMaxSizeNonPopulateCapsule);
364
365 //
366 // When PEI phase is IA32, DXE phase is X64, it is possible that capsule data are
367 // put above 4GB, so capsule PEI will transfer to long mode to get capsule data.
368 // The page table and stack is used to transfer processor mode from IA32 to long mode.
369 // Create the base address of page table and stack, and save them into variable.
370 // This is not needed when capsule with reset type is not supported.
371 //
372 SaveLongModeContext ();
373
374 //
375 // Install capsule runtime services into UEFI runtime service tables.
376 //
377 gRT->UpdateCapsule = UpdateCapsule;
378 gRT->QueryCapsuleCapabilities = QueryCapsuleCapabilities;
379
380 //
381 // Install the Capsule Architectural Protocol on a new handle
382 // to signify the capsule runtime services are ready.
383 //
384 Status = gBS->InstallMultipleProtocolInterfaces (
385 &mNewHandle,
386 &gEfiCapsuleArchProtocolGuid,
387 NULL,
388 NULL
389 );
390 ASSERT_EFI_ERROR (Status);
391
392 return Status;
393 }