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