]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleService.c
Update the copyright notice format
[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
242 //
243 // Capsule Count can't be less than one.
244 //
245 if (CapsuleCount < 1) {
246 return EFI_INVALID_PARAMETER;
247 }
248
249 //
250 // Check whether input parameter is valid
251 //
252 if ((MaxiumCapsuleSize == NULL) ||(ResetType == NULL)) {
253 return EFI_INVALID_PARAMETER;
254 }
255
256 CapsuleHeader = NULL;
257
258 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {
259 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
260 //
261 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have
262 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
263 //
264 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {
265 return EFI_INVALID_PARAMETER;
266 }
267 //
268 // A capsule which has the CAPSULE_FLAGS_INITIATE_RESET flag must have
269 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
270 //
271 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_INITIATE_RESET)) == CAPSULE_FLAGS_INITIATE_RESET) {
272 return EFI_INVALID_PARAMETER;
273 }
274 //
275 // Check Capsule image without populate flag is supported by firmware
276 //
277 if (((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) &&
278 (SupportCapsuleImage (CapsuleHeader) != EFI_SUCCESS)) {
279 return EFI_UNSUPPORTED;
280 }
281 }
282
283 //
284 // Assume that capsules have the same flags on reseting or not.
285 //
286 CapsuleHeader = CapsuleHeaderArray[0];
287 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {
288 //
289 //Check if the platform supports update capsule across a system reset
290 //
291 if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {
292 return EFI_UNSUPPORTED;
293 }
294 *ResetType = EfiResetWarm;
295 } else {
296 //
297 // For non-reset capsule image.
298 //
299 *ResetType = EfiResetCold;
300 }
301
302 //
303 // The support max capsule image size
304 //
305 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {
306 *MaxiumCapsuleSize = PcdGet32(PcdMaxSizePopulateCapsule);
307 } else {
308 *MaxiumCapsuleSize = PcdGet32(PcdMaxSizeNonPopulateCapsule);
309 }
310
311 return EFI_SUCCESS;
312 }
313
314
315 /**
316
317 This code installs UEFI capsule runtime service.
318
319 @param ImageHandle The firmware allocated handle for the EFI image.
320 @param SystemTable A pointer to the EFI System Table.
321
322 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully.
323
324 **/
325 EFI_STATUS
326 EFIAPI
327 CapsuleServiceInitialize (
328 IN EFI_HANDLE ImageHandle,
329 IN EFI_SYSTEM_TABLE *SystemTable
330 )
331 {
332 EFI_STATUS Status;
333
334 //
335 // Install capsule runtime services into UEFI runtime service tables.
336 //
337 gRT->UpdateCapsule = UpdateCapsule;
338 gRT->QueryCapsuleCapabilities = QueryCapsuleCapabilities;
339
340 //
341 // Install the Capsule Architectural Protocol on a new handle
342 // to signify the capsule runtime services are ready.
343 //
344 Status = gBS->InstallMultipleProtocolInterfaces (
345 &mNewHandle,
346 &gEfiCapsuleArchProtocolGuid,
347 NULL,
348 NULL
349 );
350 ASSERT_EFI_ERROR (Status);
351
352 return Status;
353 }