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