]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleService.c
Add 'file not found' debug message to MTFTP.
[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
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
205 StrCpy (CapsuleVarName, EFI_CAPSULE_VARIABLE_NAME);\r
206 TempVarName = CapsuleVarName + StrLen (CapsuleVarName);\r
207 if (mTimes > 0) {\r
208 UnicodeValueToString (TempVarName, 0, mTimes, 0);\r
209 }\r
210\r
409d47b4
LG
211 //\r
212 // ScatterGatherList is only referenced if the capsules are defined to persist across\r
213 // system reset. Set its value into NV storage to let pre-boot driver to pick it up \r
214 // after coming through a system reset.\r
215 //\r
b3b1a4cf 216 Status = EfiSetVariable (\r
f03ccf59 217 CapsuleVarName,\r
b3b1a4cf 218 &gEfiCapsuleVendorGuid,\r
219 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,\r
220 sizeof (UINTN),\r
221 (VOID *) &ScatterGatherList\r
222 );\r
f03ccf59 223 if (!EFI_ERROR (Status)) {\r
224 //\r
225 // Variable has been set successfully, increase variable index.\r
226 //\r
227 mTimes++;\r
228 if(InitiateReset) {\r
229 //\r
230 // Firmware that encounters a capsule which has the CAPSULE_FLAGS_INITIATE_RESET Flag set in its header\r
231 // will initiate a reset of the platform which is compatible with the passed-in capsule request and will \r
232 // not return back to the caller.\r
233 //\r
234 EfiResetSystem (EfiResetWarm, EFI_SUCCESS, 0, NULL);\r
235 }\r
3f42faa7 236 }\r
409d47b4 237 return Status;\r
74fea867 238}\r
239\r
5d69642d
LG
240/**\r
241 Returns if the capsule can be supported via UpdateCapsule().\r
242\r
243 @param CapsuleHeaderArray Virtual pointer to an array of virtual pointers to the capsules\r
244 being passed into update capsule.\r
245 @param CapsuleCount Number of pointers to EFI_CAPSULE_HEADER in\r
246 CaspuleHeaderArray.\r
247 @param MaxiumCapsuleSize On output the maximum size that UpdateCapsule() can\r
248 support as an argument to UpdateCapsule() via\r
249 CapsuleHeaderArray and ScatterGatherList.\r
250 @param ResetType Returns the type of reset required for the capsule update.\r
74fea867 251\r
5d69642d
LG
252 @retval EFI_SUCCESS Valid answer returned.\r
253 @retval EFI_UNSUPPORTED The capsule image is not supported on this platform, and\r
254 MaximumCapsuleSize and ResetType are undefined.\r
255 @retval EFI_INVALID_PARAMETER MaximumCapsuleSize is NULL, or ResetTyep is NULL,\r
256 Or CapsuleCount is Zero, or CapsuleImage is not valid.\r
74fea867 257\r
5d69642d 258**/\r
74fea867 259EFI_STATUS\r
260EFIAPI\r
261QueryCapsuleCapabilities (\r
262 IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,\r
263 IN UINTN CapsuleCount,\r
264 OUT UINT64 *MaxiumCapsuleSize,\r
265 OUT EFI_RESET_TYPE *ResetType\r
266 )\r
74fea867 267{\r
566771b0 268 EFI_STATUS Status;\r
74fea867 269 UINTN ArrayNumber;\r
270 EFI_CAPSULE_HEADER *CapsuleHeader;\r
0ef42f88 271 BOOLEAN NeedReset;\r
74fea867 272\r
5d69642d
LG
273 //\r
274 // Capsule Count can't be less than one.\r
275 //\r
74fea867 276 if (CapsuleCount < 1) {\r
277 return EFI_INVALID_PARAMETER;\r
278 }\r
5d69642d
LG
279 \r
280 //\r
3f42faa7 281 // Check whether input parameter is valid\r
5d69642d 282 //\r
74fea867 283 if ((MaxiumCapsuleSize == NULL) ||(ResetType == NULL)) {\r
284 return EFI_INVALID_PARAMETER;\r
285 }\r
286\r
287 CapsuleHeader = NULL;\r
0ef42f88 288 NeedReset = FALSE;\r
74fea867 289\r
290 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
291 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
d12f75fe
LG
292 //\r
293 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have\r
294 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.\r
295 //\r
74fea867 296 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {\r
297 return EFI_INVALID_PARAMETER;\r
298 }\r
d12f75fe 299 //\r
3f42faa7 300 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have\r
301 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.\r
302 //\r
303 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {\r
304 return EFI_INVALID_PARAMETER;\r
305 }\r
566771b0 306\r
307 //\r
308 // Check FMP capsule flag \r
309 //\r
310 if (CompareGuid(&CapsuleHeader->CapsuleGuid, &gEfiFmpCapsuleGuid)\r
311 && (CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0 ) {\r
312 return EFI_INVALID_PARAMETER;\r
313 }\r
314\r
3f42faa7 315 //\r
5d69642d 316 // Check Capsule image without populate flag is supported by firmware\r
6ee65722 317 //\r
566771b0 318 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) {\r
319 Status = SupportCapsuleImage (CapsuleHeader);\r
320 if (EFI_ERROR(Status)) {\r
321 return Status;\r
322 }\r
6ee65722 323 }\r
74fea867 324 }\r
325\r
326 //\r
0ef42f88 327 // Find out whether there is any capsule defined to persist across system reset. \r
74fea867 328 //\r
0ef42f88 329 for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {\r
330 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
331 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {\r
332 NeedReset = TRUE;\r
333 break;\r
334 }\r
335 }\r
566771b0 336\r
0ef42f88 337 if (NeedReset) {\r
74fea867 338 //\r
339 //Check if the platform supports update capsule across a system reset\r
340 //\r
109e9a61 341 if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {\r
74fea867 342 return EFI_UNSUPPORTED;\r
343 }\r
0ef42f88 344 *ResetType = EfiResetWarm;\r
f67eb9d8 345 *MaxiumCapsuleSize = (UINT64) mMaxSizePopulateCapsule;\r
74fea867 346 } else {\r
5d69642d
LG
347 //\r
348 // For non-reset capsule image.\r
349 //\r
74fea867 350 *ResetType = EfiResetCold;\r
f67eb9d8 351 *MaxiumCapsuleSize = (UINT64) mMaxSizeNonPopulateCapsule;\r
74fea867 352 }\r
5c526736 353\r
74fea867 354 return EFI_SUCCESS;\r
355}\r
356\r
357\r
5d69642d
LG
358/**\r
359\r
109e9a61 360 This code installs UEFI capsule runtime service.\r
5d69642d
LG
361\r
362 @param ImageHandle The firmware allocated handle for the EFI image. \r
363 @param SystemTable A pointer to the EFI System Table.\r
364\r
365 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully. \r
366\r
367**/\r
74fea867 368EFI_STATUS\r
369EFIAPI\r
370CapsuleServiceInitialize (\r
371 IN EFI_HANDLE ImageHandle,\r
372 IN EFI_SYSTEM_TABLE *SystemTable\r
373 )\r
74fea867 374{\r
375 EFI_STATUS Status;\r
f67eb9d8
SZ
376\r
377 mMaxSizePopulateCapsule = PcdGet32(PcdMaxSizePopulateCapsule);\r
378 mMaxSizeNonPopulateCapsule = PcdGet32(PcdMaxSizeNonPopulateCapsule);\r
379\r
ab7017fe 380 //\r
381 // When PEI phase is IA32, DXE phase is X64, it is possible that capsule data are \r
382 // put above 4GB, so capsule PEI will transfer to long mode to get capsule data.\r
383 // The page table and stack is used to transfer processor mode from IA32 to long mode.\r
384 // Create the base address of page table and stack, and save them into variable.\r
385 // This is not needed when capsule with reset type is not supported.\r
386 //\r
387 SaveLongModeContext ();\r
388 \r
5d69642d
LG
389 //\r
390 // Install capsule runtime services into UEFI runtime service tables.\r
391 //\r
109e9a61
LG
392 gRT->UpdateCapsule = UpdateCapsule;\r
393 gRT->QueryCapsuleCapabilities = QueryCapsuleCapabilities;\r
74fea867 394\r
395 //\r
5d69642d
LG
396 // Install the Capsule Architectural Protocol on a new handle\r
397 // to signify the capsule runtime services are ready.\r
74fea867 398 //\r
74fea867 399 Status = gBS->InstallMultipleProtocolInterfaces (\r
3f42faa7 400 &mNewHandle,\r
74fea867 401 &gEfiCapsuleArchProtocolGuid,\r
402 NULL,\r
403 NULL\r
404 );\r
405 ASSERT_EFI_ERROR (Status);\r
406\r
3f42faa7 407 return Status;\r
74fea867 408}\r