]> git.proxmox.com Git - mirror_edk2.git/blob - SignedCapsulePkg/Universal/SystemFirmwareUpdate/SystemFirmwareReportDxe.c
9d5832fed27817a607cae931f308d322ee8f5358
[mirror_edk2.git] / SignedCapsulePkg / Universal / SystemFirmwareUpdate / SystemFirmwareReportDxe.c
1 /** @file
2 SetImage instance to report system firmware and act as agent to system update.
3
4 Caution: This module requires additional review when modified.
5 This module will have external input - capsule image.
6 This external input must be validated carefully to avoid security issue like
7 buffer overflow, integer overflow.
8
9 FmpSetImage() will receive untrusted input and do basic validation.
10
11 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
12 This program and the accompanying materials
13 are licensed and made available under the terms and conditions of the BSD License
14 which accompanies this distribution. The full text of the license may be found at
15 http://opensource.org/licenses/bsd-license.php
16
17 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
18 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19
20 **/
21
22 #include "SystemFirmwareDxe.h"
23
24 //
25 // SystemFmp driver private data
26 //
27 SYSTEM_FMP_PRIVATE_DATA *mSystemFmpPrivate = NULL;
28
29 /**
30 Dispatch system FMP images.
31
32 Caution: This function may receive untrusted input.
33
34 @param[in] Image The EDKII system FMP capsule image.
35 @param[in] ImageSize The size of the EDKII system FMP capsule image in bytes.
36 @param[out] LastAttemptVersion The last attempt version, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
37 @param[out] LastAttemptStatus The last attempt status, which will be recorded in ESRT and FMP EFI_FIRMWARE_IMAGE_DESCRIPTOR.
38
39 @retval EFI_SUCESS Process Capsule Image successfully.
40 @retval EFI_UNSUPPORTED Capsule image is not supported by the firmware.
41 @retval EFI_VOLUME_CORRUPTED FV volume in the capsule is corrupted.
42 @retval EFI_OUT_OF_RESOURCES Not enough memory.
43 **/
44 EFI_STATUS
45 DispatchSystemFmpImages (
46 IN VOID *Image,
47 IN UINTN ImageSize,
48 OUT UINT32 *LastAttemptVersion,
49 OUT UINT32 *LastAttemptStatus
50 )
51 {
52 EFI_STATUS Status;
53 VOID *AuthenticatedImage;
54 UINTN AuthenticatedImageSize;
55 VOID *DispatchFvImage;
56 UINTN DispatchFvImageSize;
57 EFI_HANDLE FvProtocolHandle;
58 EFI_FIRMWARE_VOLUME_HEADER *FvImage;
59 BOOLEAN Result;
60
61 DEBUG((DEBUG_INFO, "DispatchSystemFmpImages\n"));
62
63 //
64 // Verify
65 //
66 Status = CapsuleAuthenticateSystemFirmware(Image, ImageSize, FALSE, LastAttemptVersion, LastAttemptStatus, &AuthenticatedImage, &AuthenticatedImageSize);
67 if (EFI_ERROR(Status)) {
68 DEBUG((DEBUG_INFO, "SystemFirmwareAuthenticateImage - %r\n", Status));
69 return Status;
70 }
71
72 //
73 // Get FV
74 //
75 Result = ExtractDriverFvImage(AuthenticatedImage, AuthenticatedImageSize, &DispatchFvImage, &DispatchFvImageSize);
76 if (Result) {
77 DEBUG((DEBUG_INFO, "ExtractDriverFvImage\n"));
78 //
79 // Dispatch
80 //
81 if (((EFI_FIRMWARE_VOLUME_HEADER *)DispatchFvImage)->FvLength == DispatchFvImageSize) {
82 FvImage = AllocatePages(EFI_SIZE_TO_PAGES(DispatchFvImageSize));
83 if (FvImage != NULL) {
84 CopyMem(FvImage, DispatchFvImage, DispatchFvImageSize);
85 Status = gDS->ProcessFirmwareVolume(
86 (VOID *)FvImage,
87 (UINTN)FvImage->FvLength,
88 &FvProtocolHandle
89 );
90 DEBUG((DEBUG_INFO, "ProcessFirmwareVolume - %r\n", Status));
91 if (!EFI_ERROR(Status)) {
92 gDS->Dispatch();
93 DEBUG((DEBUG_INFO, "Dispatch Done\n"));
94 }
95 }
96 }
97 }
98
99 return EFI_SUCCESS;
100 }
101
102 /**
103 Updates the firmware image of the device.
104
105 This function updates the hardware with the new firmware image.
106 This function returns EFI_UNSUPPORTED if the firmware image is not updatable.
107 If the firmware image is updatable, the function should perform the following minimal validations
108 before proceeding to do the firmware image update.
109 - Validate the image authentication if image has attribute
110 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED. The function returns
111 EFI_SECURITY_VIOLATION if the validation fails.
112 - Validate the image is a supported image for this device. The function returns EFI_ABORTED if
113 the image is unsupported. The function can optionally provide more detailed information on
114 why the image is not a supported image.
115 - Validate the data from VendorCode if not null. Image validation must be performed before
116 VendorCode data validation. VendorCode data is ignored or considered invalid if image
117 validation failed. The function returns EFI_ABORTED if the data is invalid.
118
119 VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if
120 the caller did not specify the policy or use the default policy. As an example, vendor can implement
121 a policy to allow an option to force a firmware image update when the abort reason is due to the new
122 firmware image version is older than the current firmware image version or bad image checksum.
123 Sensitive operations such as those wiping the entire firmware image and render the device to be
124 non-functional should be encoded in the image itself rather than passed with the VendorCode.
125 AbortReason enables vendor to have the option to provide a more detailed description of the abort
126 reason to the caller.
127
128 @param[in] This A pointer to the EFI_FIRMWARE_MANAGEMENT_PROTOCOL instance.
129 @param[in] ImageIndex A unique number identifying the firmware image(s) within the device.
130 The number is between 1 and DescriptorCount.
131 @param[in] Image Points to the new image.
132 @param[in] ImageSize Size of the new image in bytes.
133 @param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.
134 Null indicates the caller did not specify the policy or use the default policy.
135 @param[in] Progress A function used by the driver to report the progress of the firmware update.
136 @param[out] AbortReason A pointer to a pointer to a null-terminated string providing more
137 details for the aborted operation. The buffer is allocated by this function
138 with AllocatePool(), and it is the caller's responsibility to free it with a
139 call to FreePool().
140
141 @retval EFI_SUCCESS The device was successfully updated with the new image.
142 @retval EFI_ABORTED The operation is aborted.
143 @retval EFI_INVALID_PARAMETER The Image was NULL.
144 @retval EFI_UNSUPPORTED The operation is not supported.
145 @retval EFI_SECURITY_VIOLATIO The operation could not be performed due to an authentication failure.
146
147 **/
148 EFI_STATUS
149 EFIAPI
150 FmpSetImage (
151 IN EFI_FIRMWARE_MANAGEMENT_PROTOCOL *This,
152 IN UINT8 ImageIndex,
153 IN CONST VOID *Image,
154 IN UINTN ImageSize,
155 IN CONST VOID *VendorCode,
156 IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,
157 OUT CHAR16 **AbortReason
158 )
159 {
160 SYSTEM_FMP_PRIVATE_DATA *SystemFmpPrivate;
161 EFI_FIRMWARE_MANAGEMENT_PROTOCOL *SystemFmp;
162 EFI_STATUS Status;
163 EFI_STATUS VarStatus;
164
165 if (Image == NULL || ImageSize == 0 || AbortReason == NULL) {
166 return EFI_INVALID_PARAMETER;
167 }
168
169 SystemFmpPrivate = SYSTEM_FMP_PRIVATE_DATA_FROM_FMP(This);
170 *AbortReason = NULL;
171
172 if (ImageIndex == 0 || ImageIndex > SystemFmpPrivate->DescriptorCount) {
173 return EFI_INVALID_PARAMETER;
174 }
175
176 //
177 // Process FV
178 //
179 Status = DispatchSystemFmpImages((VOID *)Image, ImageSize, &SystemFmpPrivate->LastAttempt.LastAttemptVersion, &SystemFmpPrivate->LastAttempt.LastAttemptStatus);
180 DEBUG((DEBUG_INFO, "(Agent)SetImage - LastAttemp Version - 0x%x, State - 0x%x\n", SystemFmpPrivate->LastAttempt.LastAttemptVersion, SystemFmpPrivate->LastAttempt.LastAttemptStatus));
181 if (EFI_ERROR(Status)) {
182 VarStatus = gRT->SetVariable(
183 SYSTEM_FMP_LAST_ATTEMPT_VARIABLE_NAME,
184 &gSystemFmpLastAttemptVariableGuid,
185 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
186 sizeof(SystemFmpPrivate->LastAttempt),
187 &SystemFmpPrivate->LastAttempt
188 );
189 DEBUG((DEBUG_INFO, "(Agent)SetLastAttemp - %r\n", VarStatus));
190 return Status;
191 }
192
193 //
194 // Pass Thru
195 //
196 Status = gBS->LocateProtocol(&gSystemFmpProtocolGuid, NULL, (VOID **)&SystemFmp);
197 if (EFI_ERROR(Status)) {
198 DEBUG((DEBUG_INFO, "(Agent)SetImage - SystemFmpProtocol - %r\n", Status));
199 SystemFmpPrivate->LastAttempt.LastAttemptStatus = LAST_ATTEMPT_STATUS_ERROR_INVALID_FORMAT;
200 VarStatus = gRT->SetVariable(
201 SYSTEM_FMP_LAST_ATTEMPT_VARIABLE_NAME,
202 &gSystemFmpLastAttemptVariableGuid,
203 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
204 sizeof(SystemFmpPrivate->LastAttempt),
205 &SystemFmpPrivate->LastAttempt
206 );
207 DEBUG((DEBUG_INFO, "(Agent)SetLastAttemp - %r\n", VarStatus));
208 return Status;
209 }
210
211 return SystemFmp->SetImage(SystemFmp, ImageIndex, Image, ImageSize, VendorCode, Progress, AbortReason);
212 }
213
214 /**
215 System FMP module entrypoint
216
217 @param[in] ImageHandle The firmware allocated handle for the EFI image.
218 @param[in] SystemTable A pointer to the EFI System Table.
219
220 @return EFI_SUCCESS System FMP module is initialized.
221 **/
222 EFI_STATUS
223 EFIAPI
224 SystemFirmwareReportMainDxe (
225 IN EFI_HANDLE ImageHandle,
226 IN EFI_SYSTEM_TABLE *SystemTable
227 )
228 {
229 EFI_STATUS Status;
230
231 //
232 // Initialize SystemFmpPrivateData
233 //
234 mSystemFmpPrivate = AllocateZeroPool (sizeof(SYSTEM_FMP_PRIVATE_DATA));
235 if (mSystemFmpPrivate == NULL) {
236 return EFI_OUT_OF_RESOURCES;
237 }
238
239 Status = InitializePrivateData(mSystemFmpPrivate);
240 if (EFI_ERROR(Status)) {
241 FreePool(mSystemFmpPrivate);
242 mSystemFmpPrivate = NULL;
243 return Status;
244 }
245
246 //
247 // Install FMP protocol.
248 //
249 Status = gBS->InstallProtocolInterface (
250 &mSystemFmpPrivate->Handle,
251 &gEfiFirmwareManagementProtocolGuid,
252 EFI_NATIVE_INTERFACE,
253 &mSystemFmpPrivate->Fmp
254 );
255 if (EFI_ERROR (Status)) {
256 FreePool(mSystemFmpPrivate);
257 mSystemFmpPrivate = NULL;
258 return Status;
259 }
260
261 return Status;
262 }