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