]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleService.c
33ca5057fe219aa37a51c1820606fde2a353aeb8
[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 (EDKII definition) to signify
5 the capsule runtime services are ready.
6
7 Copyright (c) 2006 - 2008, Intel Corporation. <BR>
8 All rights reserved. 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
31 /**
32 Passes capsules to the firmware with both virtual and physical mapping. Depending on the intended
33 consumption, the firmware may process the capsule immediately. If the payload should persist
34 across a system reset, the reset value returned from EFI_QueryCapsuleCapabilities must
35 be passed into ResetSystem() and will cause the capsule to be processed by the firmware as
36 part of the reset process.
37
38 @param CapsuleHeaderArray Virtual pointer to an array of virtual pointers to the capsules
39 being passed into update capsule.
40 @param CapsuleCount Number of pointers to EFI_CAPSULE_HEADER in
41 CaspuleHeaderArray.
42 @param ScatterGatherList Physical pointer to a set of
43 EFI_CAPSULE_BLOCK_DESCRIPTOR that describes the
44 location in physical memory of a set of capsules.
45
46 @retval EFI_SUCCESS Valid capsule was passed. If
47 CAPSULE_FLAGS_PERSIT_ACROSS_RESET is not set, the
48 capsule has been successfully processed by the firmware.
49 @retval EFI_DEVICE_ERROR The capsule update was started, but failed due to a device error.
50 @retval EFI_INVALID_PARAMETER CapsuleCount is Zero, or CapsuleImage is not valid.
51 For across reset capsule image, ScatterGatherList is NULL.
52 @retval EFI_UNSUPPORTED CapsuleImage is not recognized by the firmware.
53
54 **/
55 EFI_STATUS
56 EFIAPI
57 UpdateCapsule (
58 IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,
59 IN UINTN CapsuleCount,
60 IN EFI_PHYSICAL_ADDRESS ScatterGatherList OPTIONAL
61 )
62 {
63 UINTN ArrayNumber;
64 EFI_STATUS Status;
65 EFI_CAPSULE_HEADER *CapsuleHeader;
66
67 //
68 // Capsule Count can't be less than one.
69 //
70 if (CapsuleCount < 1) {
71 return EFI_INVALID_PARAMETER;
72 }
73
74 CapsuleHeader = NULL;
75
76 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {
77 //
78 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have
79 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
80 //
81 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
82 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {
83 return EFI_INVALID_PARAMETER;
84 }
85 //
86 // Check Capsule image without populate flag by firmware support capsule function
87 //
88 if (((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) &&
89 (SupportCapsuleImage (CapsuleHeader) != EFI_SUCCESS)) {
90 return EFI_UNSUPPORTED;
91 }
92 }
93
94 //
95 // Assume that capsules have the same flags on reseting or not.
96 //
97 CapsuleHeader = CapsuleHeaderArray[0];
98
99 //
100 // Process across reset capsule image.
101 //
102 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {
103 //
104 // Check if the platform supports update capsule across a system reset
105 //
106 if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {
107 return EFI_UNSUPPORTED;
108 }
109 //
110 // ScatterGatherList is only referenced if the capsules are defined to persist across
111 // system reset.
112 //
113 if (ScatterGatherList == (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {
114 return EFI_INVALID_PARAMETER;
115 } else {
116 //
117 // ScatterGatherList is only referenced if the capsules are defined to persist across
118 // system reset. Set its value into NV storage to let pre-boot driver to pick it up
119 // after coming through a system reset.
120 //
121 Status = gRT->SetVariable (
122 EFI_CAPSULE_VARIABLE_NAME,
123 &gEfiCapsuleVendorGuid,
124 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,
125 sizeof (UINTN),
126 (VOID *) &ScatterGatherList
127 );
128 if (Status != EFI_SUCCESS) {
129 return Status;
130 }
131 //
132 // Successfully set the capsule image address into EFI variable.
133 //
134 return EFI_SUCCESS;
135 }
136 }
137
138 //
139 // Process the non-reset capsule image.
140 //
141 if (EfiAtRuntime ()) {
142 //
143 // Runtime mode doesn't support the non-reset capsule image.
144 //
145 return EFI_UNSUPPORTED;
146 }
147
148 //
149 // Here should be in the boot-time for non-reset capsule image
150 // Platform specific update for the non-reset capsule image.
151 //
152 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {
153 Status = ProcessCapsuleImage (CapsuleHeaderArray[ArrayNumber]);
154 if (EFI_ERROR (Status)) {
155 return Status;
156 }
157 }
158
159 return EFI_SUCCESS;
160 }
161
162 /**
163 Returns if the capsule can be supported via UpdateCapsule().
164
165 @param CapsuleHeaderArray Virtual pointer to an array of virtual pointers to the capsules
166 being passed into update capsule.
167 @param CapsuleCount Number of pointers to EFI_CAPSULE_HEADER in
168 CaspuleHeaderArray.
169 @param MaxiumCapsuleSize On output the maximum size that UpdateCapsule() can
170 support as an argument to UpdateCapsule() via
171 CapsuleHeaderArray and ScatterGatherList.
172 @param ResetType Returns the type of reset required for the capsule update.
173
174 @retval EFI_SUCCESS Valid answer returned.
175 @retval EFI_UNSUPPORTED The capsule image is not supported on this platform, and
176 MaximumCapsuleSize and ResetType are undefined.
177 @retval EFI_INVALID_PARAMETER MaximumCapsuleSize is NULL, or ResetTyep is NULL,
178 Or CapsuleCount is Zero, or CapsuleImage is not valid.
179
180 **/
181 EFI_STATUS
182 EFIAPI
183 QueryCapsuleCapabilities (
184 IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,
185 IN UINTN CapsuleCount,
186 OUT UINT64 *MaxiumCapsuleSize,
187 OUT EFI_RESET_TYPE *ResetType
188 )
189 {
190 UINTN ArrayNumber;
191 EFI_CAPSULE_HEADER *CapsuleHeader;
192
193 //
194 // Capsule Count can't be less than one.
195 //
196 if (CapsuleCount < 1) {
197 return EFI_INVALID_PARAMETER;
198 }
199
200 //
201 // Check whether input paramter is valid
202 //
203 if ((MaxiumCapsuleSize == NULL) ||(ResetType == NULL)) {
204 return EFI_INVALID_PARAMETER;
205 }
206
207 CapsuleHeader = NULL;
208
209 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {
210 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
211 //
212 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have
213 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
214 //
215 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {
216 return EFI_INVALID_PARAMETER;
217 }
218 //
219 // Check Capsule image without populate flag is supported by firmware
220 //
221 if (((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) &&
222 (SupportCapsuleImage (CapsuleHeader) != EFI_SUCCESS)) {
223 return EFI_UNSUPPORTED;
224 }
225 }
226
227 //
228 // Assume that capsules have the same flags on reseting or not.
229 //
230 CapsuleHeader = CapsuleHeaderArray[0];
231 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {
232 //
233 //Check if the platform supports update capsule across a system reset
234 //
235 if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {
236 return EFI_UNSUPPORTED;
237 }
238 *ResetType = EfiResetWarm;
239 } else {
240 //
241 // For non-reset capsule image.
242 //
243 *ResetType = EfiResetCold;
244 }
245
246 //
247 // The support max capsule image size
248 //
249 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {
250 *MaxiumCapsuleSize = FixedPcdGet32(PcdMaxSizePopulateCapsule);
251 } else {
252 *MaxiumCapsuleSize = FixedPcdGet32(PcdMaxSizeNonPopulateCapsule);
253 }
254
255 return EFI_SUCCESS;
256 }
257
258
259 /**
260
261 This code installs UEFI capsule runtime service.
262
263 @param ImageHandle The firmware allocated handle for the EFI image.
264 @param SystemTable A pointer to the EFI System Table.
265
266 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully.
267
268 **/
269 EFI_STATUS
270 EFIAPI
271 CapsuleServiceInitialize (
272 IN EFI_HANDLE ImageHandle,
273 IN EFI_SYSTEM_TABLE *SystemTable
274 )
275 {
276 EFI_STATUS Status;
277 EFI_HANDLE NewHandle;
278
279 //
280 // Install capsule runtime services into UEFI runtime service tables.
281 //
282 gRT->UpdateCapsule = UpdateCapsule;
283 gRT->QueryCapsuleCapabilities = QueryCapsuleCapabilities;
284
285 //
286 // Install the Capsule Architectural Protocol on a new handle
287 // to signify the capsule runtime services are ready.
288 //
289 NewHandle = NULL;
290
291 Status = gBS->InstallMultipleProtocolInterfaces (
292 &NewHandle,
293 &gEfiCapsuleArchProtocolGuid,
294 NULL,
295 NULL
296 );
297 ASSERT_EFI_ERROR (Status);
298
299 return EFI_SUCCESS;
300 }