]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleService.c
Enhance Capsule driver to update capsule one by one.
[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 - 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 BOOLEAN NeedReset;
67
68 //
69 // Capsule Count can't be less than one.
70 //
71 if (CapsuleCount < 1) {
72 return EFI_INVALID_PARAMETER;
73 }
74 NeedReset = FALSE;
75 CapsuleHeader = NULL;
76
77 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {
78 //
79 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have
80 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
81 //
82 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
83 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {
84 return EFI_INVALID_PARAMETER;
85 }
86 //
87 // Check Capsule image without populate flag by firmware support capsule function
88 //
89 if (((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) &&
90 (SupportCapsuleImage (CapsuleHeader) != EFI_SUCCESS)) {
91 return EFI_UNSUPPORTED;
92 }
93 }
94
95 //
96 // Walk through all capsules, record whether there is a capsule
97 // needs reset. And then process capsules which has no reset flag directly.
98 //
99 for (ArrayNumber = 0; ArrayNumber < CapsuleCount ; ArrayNumber++) {
100 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
101 //
102 // Here should be in the boot-time for non-reset capsule image
103 // Platform specific update for the non-reset capsule image.
104 //
105 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) == 0) {
106 if (EfiAtRuntime ()) {
107 Status = EFI_UNSUPPORTED;
108 } else {
109 Status = ProcessCapsuleImage(CapsuleHeader);
110 }
111 if (EFI_ERROR(Status)) {
112 return Status;
113 }
114 } else {
115 NeedReset = TRUE;
116 }
117 }
118
119 //
120 // After launching all capsules who has no reset flag, if no more capsules claims
121 // for a system reset just return.
122 //
123 if (!NeedReset) {
124 return EFI_SUCCESS;
125 }
126
127 //
128 // ScatterGatherList is only referenced if the capsules are defined to persist across
129 // system reset.
130 //
131 if (ScatterGatherList == (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {
132 return EFI_INVALID_PARAMETER;
133 }
134
135 //
136 // Check if the platform supports update capsule across a system reset
137 //
138 if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {
139 return EFI_UNSUPPORTED;
140 }
141
142 //
143 // ScatterGatherList is only referenced if the capsules are defined to persist across
144 // system reset. Set its value into NV storage to let pre-boot driver to pick it up
145 // after coming through a system reset.
146 //
147 Status = gRT->SetVariable (
148 EFI_CAPSULE_VARIABLE_NAME,
149 &gEfiCapsuleVendorGuid,
150 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS,
151 sizeof (UINTN),
152 (VOID *) &ScatterGatherList
153 );
154
155 return Status;
156 }
157
158 /**
159 Returns if the capsule can be supported via UpdateCapsule().
160
161 @param CapsuleHeaderArray Virtual pointer to an array of virtual pointers to the capsules
162 being passed into update capsule.
163 @param CapsuleCount Number of pointers to EFI_CAPSULE_HEADER in
164 CaspuleHeaderArray.
165 @param MaxiumCapsuleSize On output the maximum size that UpdateCapsule() can
166 support as an argument to UpdateCapsule() via
167 CapsuleHeaderArray and ScatterGatherList.
168 @param ResetType Returns the type of reset required for the capsule update.
169
170 @retval EFI_SUCCESS Valid answer returned.
171 @retval EFI_UNSUPPORTED The capsule image is not supported on this platform, and
172 MaximumCapsuleSize and ResetType are undefined.
173 @retval EFI_INVALID_PARAMETER MaximumCapsuleSize is NULL, or ResetTyep is NULL,
174 Or CapsuleCount is Zero, or CapsuleImage is not valid.
175
176 **/
177 EFI_STATUS
178 EFIAPI
179 QueryCapsuleCapabilities (
180 IN EFI_CAPSULE_HEADER **CapsuleHeaderArray,
181 IN UINTN CapsuleCount,
182 OUT UINT64 *MaxiumCapsuleSize,
183 OUT EFI_RESET_TYPE *ResetType
184 )
185 {
186 UINTN ArrayNumber;
187 EFI_CAPSULE_HEADER *CapsuleHeader;
188
189 //
190 // Capsule Count can't be less than one.
191 //
192 if (CapsuleCount < 1) {
193 return EFI_INVALID_PARAMETER;
194 }
195
196 //
197 // Check whether input paramter is valid
198 //
199 if ((MaxiumCapsuleSize == NULL) ||(ResetType == NULL)) {
200 return EFI_INVALID_PARAMETER;
201 }
202
203 CapsuleHeader = NULL;
204
205 for (ArrayNumber = 0; ArrayNumber < CapsuleCount; ArrayNumber++) {
206 CapsuleHeader = CapsuleHeaderArray[ArrayNumber];
207 //
208 // A capsule which has the CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE flag must have
209 // CAPSULE_FLAGS_PERSIST_ACROSS_RESET set in its header as well.
210 //
211 if ((CapsuleHeader->Flags & (CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE)) == CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) {
212 return EFI_INVALID_PARAMETER;
213 }
214 //
215 // Check Capsule image without populate flag is supported by firmware
216 //
217 if (((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) == 0) &&
218 (SupportCapsuleImage (CapsuleHeader) != EFI_SUCCESS)) {
219 return EFI_UNSUPPORTED;
220 }
221 }
222
223 //
224 // Assume that capsules have the same flags on reseting or not.
225 //
226 CapsuleHeader = CapsuleHeaderArray[0];
227 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_PERSIST_ACROSS_RESET) != 0) {
228 //
229 //Check if the platform supports update capsule across a system reset
230 //
231 if (!FeaturePcdGet(PcdSupportUpdateCapsuleReset)) {
232 return EFI_UNSUPPORTED;
233 }
234 *ResetType = EfiResetWarm;
235 } else {
236 //
237 // For non-reset capsule image.
238 //
239 *ResetType = EfiResetCold;
240 }
241
242 //
243 // The support max capsule image size
244 //
245 if ((CapsuleHeader->Flags & CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE) != 0) {
246 *MaxiumCapsuleSize = PcdGet32(PcdMaxSizePopulateCapsule);
247 } else {
248 *MaxiumCapsuleSize = PcdGet32(PcdMaxSizeNonPopulateCapsule);
249 }
250
251 return EFI_SUCCESS;
252 }
253
254
255 /**
256
257 This code installs UEFI capsule runtime service.
258
259 @param ImageHandle The firmware allocated handle for the EFI image.
260 @param SystemTable A pointer to the EFI System Table.
261
262 @retval EFI_SUCCESS UEFI Capsule Runtime Services are installed successfully.
263
264 **/
265 EFI_STATUS
266 EFIAPI
267 CapsuleServiceInitialize (
268 IN EFI_HANDLE ImageHandle,
269 IN EFI_SYSTEM_TABLE *SystemTable
270 )
271 {
272 EFI_STATUS Status;
273 EFI_HANDLE NewHandle;
274
275 //
276 // Install capsule runtime services into UEFI runtime service tables.
277 //
278 gRT->UpdateCapsule = UpdateCapsule;
279 gRT->QueryCapsuleCapabilities = QueryCapsuleCapabilities;
280
281 //
282 // Install the Capsule Architectural Protocol on a new handle
283 // to signify the capsule runtime services are ready.
284 //
285 NewHandle = NULL;
286
287 Status = gBS->InstallMultipleProtocolInterfaces (
288 &NewHandle,
289 &gEfiCapsuleArchProtocolGuid,
290 NULL,
291 NULL
292 );
293 ASSERT_EFI_ERROR (Status);
294
295 return EFI_SUCCESS;
296 }