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