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