]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleService.c
Update the copyright notice format
[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 - 2010, 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 <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
30#include <Library/BaseLib.h>\r
31#include <Library/PrintLib.h>\r
32\r
33//\r
34// Handle for the installation of Capsule Architecture Protocol.\r
35//\r
36EFI_HANDLE mNewHandle = NULL;\r
37\r
38//\r
39// The times of calling UpdateCapsule ()\r
40//\r
41UINTN mTimes = 0;\r
42\r
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
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
66 @retval EFI_UNSUPPORTED CapsuleImage is not recognized by the firmware.\r
67\r
68**/\r
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
76{\r
77 UINTN ArrayNumber;\r
78 EFI_STATUS Status;\r
79 EFI_CAPSULE_HEADER *CapsuleHeader;\r
80 BOOLEAN NeedReset;\r
81 BOOLEAN InitiateReset;\r
82 CHAR16 CapsuleVarName[30];\r
83 CHAR16 *TempVarName; \r
84 \r
85 //\r
86 // Capsule Count can't be less than one.\r
87 //\r
88 if (CapsuleCount < 1) {\r
89 return EFI_INVALID_PARAMETER;\r
90 }\r
91\r
92 NeedReset = FALSE;\r
93 InitiateReset = FALSE;\r
94 CapsuleHeader = NULL;\r
95 CapsuleVarName[0] = 0;\r
96\r
97 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
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
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
106 //\r
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
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
120 }\r
121\r
122 //\r
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
125 //\r
126 for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {\r
127 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
128 //\r
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
131 //\r
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
139 return Status;\r
140 }\r
141 } else {\r
142 NeedReset = TRUE;\r
143 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_INITIATE_RESET) != 0) {\r
144 InitiateReset = TRUE;\r
145 }\r
146 }\r
147 }\r
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
156\r
157 //\r
158 // ScatterGatherList is only referenced if the capsules are defined to persist across\r
159 // system reset. \r
160 //\r
161 if (ScatterGatherList == (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {\r
162 return EFI_INVALID_PARAMETER;\r
163 }\r
164\r
165 //\r
166 // Check if the platform supports update capsule across a system reset\r
167 //\r
168 if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {\r
169 return EFI_UNSUPPORTED;\r
170 }\r
171\r
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
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
187 Status = EfiSetVariable (\r
188 CapsuleVarName,\r
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
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
207 }\r
208 return Status;\r
209}\r
210\r
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
222\r
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
228\r
229**/\r
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
238{\r
239 UINTN ArrayNumber;\r
240 EFI_CAPSULE_HEADER *CapsuleHeader;\r
241\r
242 //\r
243 // Capsule Count can't be less than one.\r
244 //\r
245 if (CapsuleCount < 1) {\r
246 return EFI_INVALID_PARAMETER;\r
247 }\r
248 \r
249 //\r
250 // Check whether input parameter is valid\r
251 //\r
252 if ((MaxiumCapsuleSize == NULL) ||(ResetType == NULL)) {\r
253 return EFI_INVALID_PARAMETER;\r
254 }\r
255\r
256 CapsuleHeader = NULL;\r
257\r
258 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r
259 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r
260 //\r
261 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have\r
262 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.\r
263 //\r
264 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {\r
265 return EFI_INVALID_PARAMETER;\r
266 }\r
267 //\r
268 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have\r
269 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.\r
270 //\r
271 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {\r
272 return EFI_INVALID_PARAMETER;\r
273 }\r
274 //\r
275 // Check Capsule image without populate flag is supported by firmware\r
276 //\r
277 if (((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) && \r
278 (SupportCapsuleImage (CapsuleHeader) != EFI_SUCCESS)) {\r
279 return EFI_UNSUPPORTED;\r
280 }\r
281 }\r
282\r
283 //\r
284 // Assume that capsules have the same flags on reseting or not.\r
285 //\r
286 CapsuleHeader = CapsuleHeaderArray[0];\r
287 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {\r
288 //\r
289 //Check if the platform supports update capsule across a system reset\r
290 //\r
291 if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {\r
292 return EFI_UNSUPPORTED;\r
293 }\r
294 *ResetType = EfiResetWarm; \r
295 } else {\r
296 //\r
297 // For non-reset capsule image.\r
298 //\r
299 *ResetType = EfiResetCold;\r
300 }\r
301 \r
302 //\r
303 // The support max capsule image size\r
304 //\r
305 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {\r
306 *MaxiumCapsuleSize = PcdGet32(PcdMaxSizePopulateCapsule);\r
307 } else {\r
308 *MaxiumCapsuleSize = PcdGet32(PcdMaxSizeNonPopulateCapsule);\r
309 }\r
310\r
311 return EFI_SUCCESS;\r
312}\r
313\r
314\r
315/**\r
316\r
317 This code installs UEFI capsule runtime service.\r
318\r
319 @param ImageHandle The firmware allocated handle for the EFI image. \r
320 @param SystemTable A pointer to the EFI System Table.\r
321\r
322 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully. \r
323\r
324**/\r
325EFI_STATUS\r
326EFIAPI\r
327CapsuleServiceInitialize (\r
328 IN EFI_HANDLE ImageHandle,\r
329 IN EFI_SYSTEM_TABLE *SystemTable\r
330 )\r
331{\r
332 EFI_STATUS Status;\r
333 \r
334 //\r
335 // Install capsule runtime services into UEFI runtime service tables.\r
336 //\r
337 gRT->UpdateCapsule = UpdateCapsule;\r
338 gRT->QueryCapsuleCapabilities = QueryCapsuleCapabilities;\r
339\r
340 //\r
341 // Install the Capsule Architectural Protocol on a new handle\r
342 // to signify the capsule runtime services are ready.\r
343 //\r
344 Status = gBS->InstallMultipleProtocolInterfaces (\r
345 &mNewHandle,\r
346 &gEfiCapsuleArchProtocolGuid,\r
347 NULL,\r
348 NULL\r
349 );\r
350 ASSERT_EFI_ERROR (Status);\r
351\r
352 return Status;\r
353}\r