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