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