]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleService.c
MdeModulePkg/PciBusDxe: Fix small memory leak in FreePciDevice
[mirror_edk2.git] / MdeModulePkg / Universal / CapsuleRuntimeDxe / CapsuleService.c
... / ...
CommitLineData
1/** @file\r
2 Capsule Runtime Driver produces two UEFI capsule runtime services.\r
3 (UpdateCapsule, QueryCapsuleCapabilities)\r
4 It installs the Capsule Architectural Protocol defined in PI1.0a to signify\r
5 the capsule runtime services are ready.\r
6\r
7Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
8This program and the accompanying materials\r
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
16**/\r
17\r
18#include "CapsuleService.h"\r
19\r
20//\r
21// Handle for the installation of Capsule Architecture Protocol.\r
22//\r
23EFI_HANDLE mNewHandle = NULL;\r
24\r
25//\r
26// The times of calling UpdateCapsule ()\r
27//\r
28UINTN mTimes = 0;\r
29\r
30UINT32 mMaxSizePopulateCapsule = 0;\r
31UINT32 mMaxSizeNonPopulateCapsule = 0;\r
32\r
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
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
56 @retval EFI_UNSUPPORTED CapsuleImage is not recognized by the firmware.\r
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
59 in runtime. The caller may resubmit the capsule prior to ExitBootServices().\r
60 @retval EFI_OUT_OF_RESOURCES When ExitBootServices() has not been previously called then this error indicates\r
61 the capsule is compatible with this platform but there are insufficient resources to process.\r
62\r
63**/\r
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
71{\r
72 UINTN ArrayNumber;\r
73 EFI_STATUS Status;\r
74 EFI_CAPSULE_HEADER *CapsuleHeader;\r
75 BOOLEAN NeedReset;\r
76 BOOLEAN InitiateReset;\r
77 CHAR16 CapsuleVarName[30];\r
78 CHAR16 *TempVarName;\r
79\r
80 //\r
81 // Capsule Count can't be less than one.\r
82 //\r
83 if (CapsuleCount < 1) {\r
84 return EFI_INVALID_PARAMETER;\r
85 }\r
86\r
87 NeedReset = FALSE;\r
88 InitiateReset = FALSE;\r
89 CapsuleHeader = NULL;\r
90 CapsuleVarName[0] = 0;\r
91\r
92 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
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
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
101 //\r
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
108\r
109 //\r
110 // Check FMP capsule flag\r
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
117 //\r
118 // Check Capsule image without populate flag by firmware support capsule function\r
119 //\r
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
125 }\r
126 }\r
127\r
128 //\r
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
131 //\r
132 for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {\r
133 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
134 //\r
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
137 //\r
138 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) == 0) {\r
139 if (EfiAtRuntime ()) {\r
140 Status = EFI_OUT_OF_RESOURCES;\r
141 } else {\r
142 Status = ProcessCapsuleImage(CapsuleHeader);\r
143 }\r
144 if (EFI_ERROR(Status)) {\r
145 return Status;\r
146 }\r
147 } else {\r
148 NeedReset = TRUE;\r
149 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_INITIATE_RESET) != 0) {\r
150 InitiateReset = TRUE;\r
151 }\r
152 }\r
153 }\r
154\r
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
162\r
163 //\r
164 // ScatterGatherList is only referenced if the capsules are defined to persist across\r
165 // system reset.\r
166 //\r
167 if (ScatterGatherList == (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {\r
168 return EFI_INVALID_PARAMETER;\r
169 }\r
170\r
171 //\r
172 // Check if the platform supports update capsule across a system reset\r
173 //\r
174 if (!IsPersistAcrossResetCapsuleSupported ()) {\r
175 return EFI_UNSUPPORTED;\r
176 }\r
177\r
178 CapsuleCacheWriteBack (ScatterGatherList);\r
179\r
180 //\r
181 // Construct variable name CapsuleUpdateData, CapsuleUpdateData1, CapsuleUpdateData2...\r
182 // if user calls UpdateCapsule multiple times.\r
183 //\r
184 StrCpyS (CapsuleVarName, sizeof(CapsuleVarName)/sizeof(CHAR16), EFI_CAPSULE_VARIABLE_NAME);\r
185 TempVarName = CapsuleVarName + StrLen (CapsuleVarName);\r
186 if (mTimes > 0) {\r
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
194 }\r
195\r
196 //\r
197 // ScatterGatherList is only referenced if the capsules are defined to persist across\r
198 // system reset. Set its value into NV storage to let pre-boot driver to pick it up\r
199 // after coming through a system reset.\r
200 //\r
201 Status = EfiSetVariable (\r
202 CapsuleVarName,\r
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
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
216 // will initiate a reset of the platform which is compatible with the passed-in capsule request and will\r
217 // not return back to the caller.\r
218 //\r
219 EfiResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
220 }\r
221 }\r
222 return Status;\r
223}\r
224\r
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
236\r
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
242\r
243**/\r
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
252{\r
253 EFI_STATUS Status;\r
254 UINTN ArrayNumber;\r
255 EFI_CAPSULE_HEADER *CapsuleHeader;\r
256 BOOLEAN NeedReset;\r
257\r
258 //\r
259 // Capsule Count can't be less than one.\r
260 //\r
261 if (CapsuleCount < 1) {\r
262 return EFI_INVALID_PARAMETER;\r
263 }\r
264\r
265 //\r
266 // Check whether input parameter is valid\r
267 //\r
268 if ((MaxiumCapsuleSize == NULL) ||(ResetType == NULL)) {\r
269 return EFI_INVALID_PARAMETER;\r
270 }\r
271\r
272 CapsuleHeader = NULL;\r
273 NeedReset = FALSE;\r
274\r
275 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
276 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
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
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
284 //\r
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
291\r
292 //\r
293 // Check FMP capsule flag\r
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
300 //\r
301 // Check Capsule image without populate flag is supported by firmware\r
302 //\r
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
308 }\r
309 }\r
310\r
311 //\r
312 // Find out whether there is any capsule defined to persist across system reset.\r
313 //\r
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
321\r
322 if (NeedReset) {\r
323 //\r
324 //Check if the platform supports update capsule across a system reset\r
325 //\r
326 if (!IsPersistAcrossResetCapsuleSupported ()) {\r
327 return EFI_UNSUPPORTED;\r
328 }\r
329 *ResetType = EfiResetWarm;\r
330 *MaxiumCapsuleSize = (UINT64) mMaxSizePopulateCapsule;\r
331 } else {\r
332 //\r
333 // For non-reset capsule image.\r
334 //\r
335 *ResetType = EfiResetCold;\r
336 *MaxiumCapsuleSize = (UINT64) mMaxSizeNonPopulateCapsule;\r
337 }\r
338\r
339 return EFI_SUCCESS;\r
340}\r
341\r
342\r
343/**\r
344\r
345 This code installs UEFI capsule runtime service.\r
346\r
347 @param ImageHandle The firmware allocated handle for the EFI image.\r
348 @param SystemTable A pointer to the EFI System Table.\r
349\r
350 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully.\r
351\r
352**/\r
353EFI_STATUS\r
354EFIAPI\r
355CapsuleServiceInitialize (\r
356 IN EFI_HANDLE ImageHandle,\r
357 IN EFI_SYSTEM_TABLE *SystemTable\r
358 )\r
359{\r
360 EFI_STATUS Status;\r
361\r
362 mMaxSizePopulateCapsule = PcdGet32(PcdMaxSizePopulateCapsule);\r
363 mMaxSizeNonPopulateCapsule = PcdGet32(PcdMaxSizeNonPopulateCapsule);\r
364\r
365 //\r
366 // When PEI phase is IA32, DXE phase is X64, it is possible that capsule data are\r
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
373\r
374 //\r
375 // Install capsule runtime services into UEFI runtime service tables.\r
376 //\r
377 gRT->UpdateCapsule = UpdateCapsule;\r
378 gRT->QueryCapsuleCapabilities = QueryCapsuleCapabilities;\r
379\r
380 //\r
381 // Install the Capsule Architectural Protocol on a new handle\r
382 // to signify the capsule runtime services are ready.\r
383 //\r
384 Status = gBS->InstallMultipleProtocolInterfaces (\r
385 &mNewHandle,\r
386 &gEfiCapsuleArchProtocolGuid,\r
387 NULL,\r
388 NULL\r
389 );\r
390 ASSERT_EFI_ERROR (Status);\r
391\r
392 return Status;\r
393}\r