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