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