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