]>
Commit | Line | Data |
---|---|---|
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 | |
7 | Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\r | |
8 | This program and the accompanying materials\r | |
9 | are licensed and made available under the terms and conditions of the BSD License\r | |
10 | which accompanies this distribution. The full text of the license may be found at\r | |
11 | http://opensource.org/licenses/bsd-license.php\r | |
12 | \r | |
13 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
14 | WITHOUT 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 | #include <Guid/FmpCapsule.h>\r | |
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 | |
31 | #include <Library/BaseLib.h>\r | |
32 | #include <Library/PrintLib.h>\r | |
33 | #include <Library/BaseMemoryLib.h>\r | |
34 | //\r | |
35 | // Handle for the installation of Capsule Architecture Protocol.\r | |
36 | //\r | |
37 | EFI_HANDLE mNewHandle = NULL;\r | |
38 | \r | |
39 | //\r | |
40 | // The times of calling UpdateCapsule ()\r | |
41 | //\r | |
42 | UINTN mTimes = 0;\r | |
43 | \r | |
44 | UINT32 mMaxSizePopulateCapsule = 0;\r | |
45 | UINT32 mMaxSizeNonPopulateCapsule = 0;\r | |
46 | \r | |
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 | |
51 | VOID\r | |
52 | SaveLongModeContext (\r | |
53 | VOID\r | |
54 | );\r | |
55 | \r | |
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 | |
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 | |
79 | @retval EFI_UNSUPPORTED CapsuleImage is not recognized by the firmware.\r | |
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 | |
85 | \r | |
86 | **/\r | |
87 | EFI_STATUS\r | |
88 | EFIAPI\r | |
89 | UpdateCapsule (\r | |
90 | IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,\r | |
91 | IN UINTN CapsuleCount,\r | |
92 | IN EFI_PHYSICAL_ADDRESS ScatterGatherList OPTIONAL\r | |
93 | )\r | |
94 | {\r | |
95 | UINTN ArrayNumber;\r | |
96 | EFI_STATUS Status;\r | |
97 | EFI_CAPSULE_HEADER *CapsuleHeader;\r | |
98 | BOOLEAN NeedReset;\r | |
99 | BOOLEAN InitiateReset;\r | |
100 | CHAR16 CapsuleVarName[30];\r | |
101 | CHAR16 *TempVarName; \r | |
102 | \r | |
103 | //\r | |
104 | // Capsule Count can't be less than one.\r | |
105 | //\r | |
106 | if (CapsuleCount < 1) {\r | |
107 | return EFI_INVALID_PARAMETER;\r | |
108 | }\r | |
109 | \r | |
110 | NeedReset = FALSE;\r | |
111 | InitiateReset = FALSE;\r | |
112 | CapsuleHeader = NULL;\r | |
113 | CapsuleVarName[0] = 0;\r | |
114 | \r | |
115 | for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r | |
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 | |
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 | |
124 | //\r | |
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 | |
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 | |
140 | //\r | |
141 | // Check Capsule image without populate flag by firmware support capsule function \r | |
142 | //\r | |
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 | |
148 | }\r | |
149 | }\r | |
150 | \r | |
151 | //\r | |
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 | |
154 | //\r | |
155 | for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {\r | |
156 | CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r | |
157 | //\r | |
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 | |
160 | //\r | |
161 | if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) == 0) {\r | |
162 | if (EfiAtRuntime ()) { \r | |
163 | Status = EFI_OUT_OF_RESOURCES;\r | |
164 | } else {\r | |
165 | Status = ProcessCapsuleImage(CapsuleHeader);\r | |
166 | }\r | |
167 | if (EFI_ERROR(Status)) {\r | |
168 | return Status;\r | |
169 | }\r | |
170 | } else {\r | |
171 | NeedReset = TRUE;\r | |
172 | if ((CapsuleHeader->Flags & CAPSULE_FLAGS_INITIATE_RESET) != 0) {\r | |
173 | InitiateReset = TRUE;\r | |
174 | }\r | |
175 | }\r | |
176 | }\r | |
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 | |
185 | \r | |
186 | //\r | |
187 | // ScatterGatherList is only referenced if the capsules are defined to persist across\r | |
188 | // system reset. \r | |
189 | //\r | |
190 | if (ScatterGatherList == (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {\r | |
191 | return EFI_INVALID_PARAMETER;\r | |
192 | }\r | |
193 | \r | |
194 | //\r | |
195 | // Check if the platform supports update capsule across a system reset\r | |
196 | //\r | |
197 | if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {\r | |
198 | return EFI_UNSUPPORTED;\r | |
199 | }\r | |
200 | \r | |
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 | |
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 | |
216 | Status = EfiSetVariable (\r | |
217 | CapsuleVarName,\r | |
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 | |
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 | |
236 | }\r | |
237 | return Status;\r | |
238 | }\r | |
239 | \r | |
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 | |
251 | \r | |
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 | |
257 | \r | |
258 | **/\r | |
259 | EFI_STATUS\r | |
260 | EFIAPI\r | |
261 | QueryCapsuleCapabilities (\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 | |
267 | {\r | |
268 | EFI_STATUS Status;\r | |
269 | UINTN ArrayNumber;\r | |
270 | EFI_CAPSULE_HEADER *CapsuleHeader;\r | |
271 | BOOLEAN NeedReset;\r | |
272 | \r | |
273 | //\r | |
274 | // Capsule Count can't be less than one.\r | |
275 | //\r | |
276 | if (CapsuleCount < 1) {\r | |
277 | return EFI_INVALID_PARAMETER;\r | |
278 | }\r | |
279 | \r | |
280 | //\r | |
281 | // Check whether input parameter is valid\r | |
282 | //\r | |
283 | if ((MaxiumCapsuleSize == NULL) ||(ResetType == NULL)) {\r | |
284 | return EFI_INVALID_PARAMETER;\r | |
285 | }\r | |
286 | \r | |
287 | CapsuleHeader = NULL;\r | |
288 | NeedReset = FALSE;\r | |
289 | \r | |
290 | for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {\r | |
291 | CapsuleHeader = CapsuleHeaderArray[ArrayNumber];\r | |
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 | |
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 | |
299 | //\r | |
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 | |
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 | |
315 | //\r | |
316 | // Check Capsule image without populate flag is supported by firmware\r | |
317 | //\r | |
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 | |
323 | }\r | |
324 | }\r | |
325 | \r | |
326 | //\r | |
327 | // Find out whether there is any capsule defined to persist across system reset. \r | |
328 | //\r | |
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 | |
336 | \r | |
337 | if (NeedReset) {\r | |
338 | //\r | |
339 | //Check if the platform supports update capsule across a system reset\r | |
340 | //\r | |
341 | if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {\r | |
342 | return EFI_UNSUPPORTED;\r | |
343 | }\r | |
344 | *ResetType = EfiResetWarm;\r | |
345 | *MaxiumCapsuleSize = (UINT64) mMaxSizePopulateCapsule;\r | |
346 | } else {\r | |
347 | //\r | |
348 | // For non-reset capsule image.\r | |
349 | //\r | |
350 | *ResetType = EfiResetCold;\r | |
351 | *MaxiumCapsuleSize = (UINT64) mMaxSizeNonPopulateCapsule;\r | |
352 | }\r | |
353 | \r | |
354 | return EFI_SUCCESS;\r | |
355 | }\r | |
356 | \r | |
357 | \r | |
358 | /**\r | |
359 | \r | |
360 | This code installs UEFI capsule runtime service.\r | |
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 | |
368 | EFI_STATUS\r | |
369 | EFIAPI\r | |
370 | CapsuleServiceInitialize (\r | |
371 | IN EFI_HANDLE ImageHandle,\r | |
372 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
373 | )\r | |
374 | {\r | |
375 | EFI_STATUS Status;\r | |
376 | \r | |
377 | mMaxSizePopulateCapsule = PcdGet32(PcdMaxSizePopulateCapsule);\r | |
378 | mMaxSizeNonPopulateCapsule = PcdGet32(PcdMaxSizeNonPopulateCapsule);\r | |
379 | \r | |
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 | |
389 | //\r | |
390 | // Install capsule runtime services into UEFI runtime service tables.\r | |
391 | //\r | |
392 | gRT->UpdateCapsule = UpdateCapsule;\r | |
393 | gRT->QueryCapsuleCapabilities = QueryCapsuleCapabilities;\r | |
394 | \r | |
395 | //\r | |
396 | // Install the Capsule Architectural Protocol on a new handle\r | |
397 | // to signify the capsule runtime services are ready.\r | |
398 | //\r | |
399 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
400 | &mNewHandle,\r | |
401 | &gEfiCapsuleArchProtocolGuid,\r | |
402 | NULL,\r | |
403 | NULL\r | |
404 | );\r | |
405 | ASSERT_EFI_ERROR (Status);\r | |
406 | \r | |
407 | return Status;\r | |
408 | }\r |