]>
Commit | Line | Data |
---|---|---|
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 |
7 | Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r |
8 | This program and the accompanying materials\r | |
74fea867 | 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 | |
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 | |
36 | EFI_HANDLE mNewHandle = NULL;\r | |
37 | \r | |
f03ccf59 | 38 | //\r |
39 | // The times of calling UpdateCapsule ()\r | |
40 | //\r | |
41 | UINTN 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 | 69 | EFI_STATUS\r |
70 | EFIAPI\r | |
71 | UpdateCapsule (\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 | 230 | EFI_STATUS\r |
231 | EFIAPI\r | |
232 | QueryCapsuleCapabilities (\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 | |
241 | \r | |
5d69642d LG |
242 | //\r |
243 | // Capsule Count can't be less than one.\r | |
244 | //\r | |
74fea867 | 245 | if (CapsuleCount < 1) {\r |
246 | return EFI_INVALID_PARAMETER;\r | |
247 | }\r | |
5d69642d LG |
248 | \r |
249 | //\r | |
3f42faa7 | 250 | // Check whether input parameter is valid\r |
5d69642d | 251 | //\r |
74fea867 | 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 | |
d12f75fe LG |
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 | |
74fea867 | 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 | |
d12f75fe | 267 | //\r |
3f42faa7 | 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 | |
5d69642d | 275 | // Check Capsule image without populate flag is supported by firmware\r |
6ee65722 LG |
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 | |
74fea867 | 281 | }\r |
282 | \r | |
283 | //\r | |
5d69642d | 284 | // Assume that capsules have the same flags on reseting or not.\r |
74fea867 | 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 | |
109e9a61 | 291 | if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {\r |
74fea867 | 292 | return EFI_UNSUPPORTED;\r |
293 | }\r | |
5c526736 | 294 | *ResetType = EfiResetWarm; \r |
74fea867 | 295 | } else {\r |
5d69642d LG |
296 | //\r |
297 | // For non-reset capsule image.\r | |
298 | //\r | |
74fea867 | 299 | *ResetType = EfiResetCold;\r |
5c526736 LG |
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 | |
97b2c9b5 | 306 | *MaxiumCapsuleSize = PcdGet32(PcdMaxSizePopulateCapsule);\r |
5c526736 | 307 | } else {\r |
97b2c9b5 | 308 | *MaxiumCapsuleSize = PcdGet32(PcdMaxSizeNonPopulateCapsule);\r |
74fea867 | 309 | }\r |
5c526736 | 310 | \r |
74fea867 | 311 | return EFI_SUCCESS;\r |
312 | }\r | |
313 | \r | |
314 | \r | |
5d69642d LG |
315 | /**\r |
316 | \r | |
109e9a61 | 317 | This code installs UEFI capsule runtime service.\r |
5d69642d LG |
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 | |
74fea867 | 325 | EFI_STATUS\r |
326 | EFIAPI\r | |
327 | CapsuleServiceInitialize (\r | |
328 | IN EFI_HANDLE ImageHandle,\r | |
329 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
330 | )\r | |
74fea867 | 331 | {\r |
332 | EFI_STATUS Status;\r | |
5d69642d LG |
333 | \r |
334 | //\r | |
335 | // Install capsule runtime services into UEFI runtime service tables.\r | |
336 | //\r | |
109e9a61 LG |
337 | gRT->UpdateCapsule = UpdateCapsule;\r |
338 | gRT->QueryCapsuleCapabilities = QueryCapsuleCapabilities;\r | |
74fea867 | 339 | \r |
340 | //\r | |
5d69642d LG |
341 | // Install the Capsule Architectural Protocol on a new handle\r |
342 | // to signify the capsule runtime services are ready.\r | |
74fea867 | 343 | //\r |
74fea867 | 344 | Status = gBS->InstallMultipleProtocolInterfaces (\r |
3f42faa7 | 345 | &mNewHandle,\r |
74fea867 | 346 | &gEfiCapsuleArchProtocolGuid,\r |
347 | NULL,\r | |
348 | NULL\r | |
349 | );\r | |
350 | ASSERT_EFI_ERROR (Status);\r | |
351 | \r | |
3f42faa7 | 352 | return Status;\r |
74fea867 | 353 | }\r |