]> git.proxmox.com Git - mirror_edk2.git/blame - Vlv2TbltDevicePkg/Feature/Capsule/Library/FmpDeviceLibSample/FmpDeviceLib.c
Vlv2TbltDevicePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / Vlv2TbltDevicePkg / Feature / Capsule / Library / FmpDeviceLibSample / FmpDeviceLib.c
CommitLineData
dc65dd5b
MK
1/**\r
2\r
3Copyright (c) 2016, Microsoft Corporation\r
4\r
5All rights reserved.\r
9dc8036d 6SPDX-License-Identifier: BSD-2-Clause-Patent\r
dc65dd5b
MK
7\r
8**/\r
9\r
10\r
11#include <PiDxe.h>\r
12#include <Library/DebugLib.h>\r
13#include <Protocol/FirmwareManagement.h>\r
14#include <Library/BaseLib.h>\r
15#include <Library/MemoryAllocationLib.h>\r
16#include <Library/BaseMemoryLib.h>\r
17#include <Library/FmpDeviceLib.h>\r
18#include <Library/UefiBootServicesTableLib.h>\r
19\r
20/**\r
21 Used to pass the FMP install function to this lib.\r
22 This allows the library to have control of the handle\r
23 that the FMP instance is installed on. This allows the library\r
24 to use DriverBinding protocol model to locate its device(s) in the\r
25 system.\r
26\r
27 @param[in] Function pointer to FMP install function.\r
28\r
29 @retval EFI_SUCCESS Library has saved function pointer and will call function pointer on each DriverBinding Start.\r
30 @retval EFI_UNSUPPORTED Library doesn't use driver binding and only supports a single instance.\r
31 @retval other error Error occurred. Don't install FMP\r
32\r
33**/\r
34EFI_STATUS\r
35EFIAPI\r
36RegisterFmpInstaller(\r
37IN FMP_DEVICE_LIB_REGISTER_FMP_INSTALLER Func\r
38)\r
39{\r
40 // Because this is a sample lib with very simple fake device we don't use\r
41 // the driverbinding protocol to locate our device.\r
42 //\r
43 return EFI_UNSUPPORTED;\r
44}\r
45\r
46\r
47/**\r
48Used to get the size of the image in bytes.\r
49NOTE - Do not return zero as that will identify the device as\r
50not updatable.\r
51\r
52@retval UINTN that represents the size of the firmware.\r
53\r
54**/\r
55EFI_STATUS\r
56EFIAPI\r
57FmpDeviceGetSize (\r
58 IN UINTN *Size\r
59 )\r
60{\r
61 if (Size == NULL) {\r
62 return EFI_INVALID_PARAMETER;\r
63 }\r
64 *Size = 0x1000;\r
65 return EFI_SUCCESS;\r
66}\r
67\r
68/**\r
69Used to return a library supplied guid that will be the ImageTypeId guid of the FMP descriptor.\r
70This is optional but can be used if at runtime the guid needs to be determined.\r
71\r
72@param Guid: Double Guid Ptr that will be updated to point to guid. This should be from static memory\r
73and will not be freed.\r
74@return EFI_UNSUPPORTED: if you library instance doesn't need dynamic guid return this.\r
75@return Error: Any error will cause the wrapper to use the GUID defined by PCD\r
76@return EFI_SUCCESS: Guid ptr should be updated to point to static memeory which contains a valid guid\r
77**/\r
78EFI_STATUS\r
79EFIAPI\r
80FmpDeviceGetImageTypeIdGuidPtr(\r
81 OUT EFI_GUID** Guid)\r
82{\r
83 //this instance doesn't need dynamic guid detection.\r
84 return EFI_UNSUPPORTED;\r
85}\r
86\r
87/**\r
88 Returns values used to fill in the AttributesSupported and AttributesSettings\r
89 fields of the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the\r
90 GetImageInfo() service of the Firmware Management Protocol. The following\r
91 bit values from the Firmware Management Protocol may be combined:\r
92 IMAGE_ATTRIBUTE_IMAGE_UPDATABLE\r
93 IMAGE_ATTRIBUTE_RESET_REQUIRED\r
94 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED\r
95 IMAGE_ATTRIBUTE_IN_USE\r
96 IMAGE_ATTRIBUTE_UEFI_IMAGE\r
97\r
98 @param[out] Supported Attributes supported by this firmware device.\r
99 @param[out] Setting Attributes settings for this firmware device.\r
100\r
101 @retval EFI_SUCCESS The attributes supported by the firmware\r
102 device were returned.\r
103 @retval EFI_INVALID_PARAMETER Supported is NULL.\r
104 @retval EFI_INVALID_PARAMETER Setting is NULL.\r
105\r
106**/\r
107EFI_STATUS\r
108EFIAPI\r
109FmpDeviceGetAttributes (\r
110 IN OUT UINT64 *Supported,\r
111 IN OUT UINT64 *Setting\r
112 )\r
113{\r
114 if (Supported == NULL || Setting == NULL) {\r
115 return EFI_INVALID_PARAMETER;\r
116 }\r
117 *Supported = (IMAGE_ATTRIBUTE_IMAGE_UPDATABLE | IMAGE_ATTRIBUTE_IN_USE);\r
118 *Setting = (IMAGE_ATTRIBUTE_IMAGE_UPDATABLE | IMAGE_ATTRIBUTE_IN_USE);\r
119 return EFI_SUCCESS;\r
120}\r
121\r
122/**\r
123Gets the current Lowest Supported Version.\r
124This is a protection mechanism so that a previous version with known issue is not\r
125applied.\r
126\r
127ONLY implement this if your running firmware has a method to return this at runtime.\r
128\r
129@param[out] Version On return this value represents the\r
130current Lowest Supported Version (in same format as GetVersion).\r
131\r
132@retval EFI_SUCCESS The Lowest Supported Version was correctly retrieved\r
133@retval EFI_UNSUPPORTED Device firmware doesn't support reporting LSV\r
134@retval EFI_DEVICE_ERROR Error occurred when trying to get the LSV\r
135**/\r
136EFI_STATUS\r
137EFIAPI\r
138FmpDeviceGetLowestSupportedVersion (\r
139 IN OUT UINT32* LowestSupportedVersion\r
140 )\r
141{\r
142 return EFI_UNSUPPORTED;\r
143}\r
144\r
145\r
146/**\r
147 Returns the Null-terminated Unicode string that is used to fill in the\r
148 VersionName field of the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is\r
149 returned by the GetImageInfo() service of the Firmware Management Protocol.\r
150 The returned string must be allocated using EFI_BOOT_SERVICES.AllocatePool().\r
151\r
152 @note It is recommended that all firmware devices support a method to report\r
153 the VersionName string from the currently stored firmware image.\r
154\r
155 @param[out] VersionString The version string retrieved from the currently\r
156 stored firmware image.\r
157\r
158 @retval EFI_SUCCESS The version string of currently stored\r
159 firmware image was returned in Version.\r
160 @retval EFI_INVALID_PARAMETER VersionString is NULL.\r
161 @retval EFI_UNSUPPORTED The firmware device does not support a method\r
162 to report the version string of the currently\r
163 stored firmware image.\r
164 @retval EFI_DEVICE_ERROR An error occurred attempting to retrieve the\r
165 version string of the currently stored\r
166 firmware image.\r
167 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate the\r
168 buffer for the version string of the currently\r
169 stored firmware image.\r
170\r
171**/\r
172EFI_STATUS\r
173EFIAPI\r
174FmpDeviceGetVersionString (\r
175 OUT CHAR16 **VersionString\r
176 )\r
177{\r
178 if (VersionString == NULL) {\r
179 return EFI_INVALID_PARAMETER;\r
180 }\r
181 *VersionString = NULL;\r
182 return EFI_UNSUPPORTED;\r
183}\r
184\r
185/**\r
186Gets the current running version.\r
187ONLY implement this if your running firmware has a method to return this at runtime.\r
188\r
189@param[out] Version On return this value represents the current running version\r
190\r
191@retval EFI_SUCCESS The version was correctly retrieved\r
192@retval EFI_UNSUPPORTED Device firmware doesn't support reporting current version\r
193@retval EFI_DEVICE_ERROR Error occurred when trying to get the version\r
194**/\r
195EFI_STATUS\r
196EFIAPI\r
197FmpDeviceGetVersion(\r
198IN OUT UINT32* Version\r
199)\r
200{\r
201 return EFI_UNSUPPORTED;\r
202}\r
203\r
204\r
205/**\r
206Retrieves a copy of the current firmware image of the device.\r
207\r
208This function allows a copy of the current firmware image to be created and saved.\r
209The saved copy could later been used, for example, in firmware image recovery or rollback.\r
210\r
211@param[out] Image Points to the buffer where the current image is copied to.\r
212@param[out] ImageSize On entry, points to the size of the buffer pointed to by Image, in bytes.\r
213On return, points to the length of the image, in bytes.\r
214\r
215@retval EFI_SUCCESS The device was successfully updated with the new image.\r
216@retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small to hold the\r
217image. The current buffer size needed to hold the image is returned\r
218in ImageSize.\r
219@retval EFI_INVALID_PARAMETER The Image was NULL.\r
220@retval EFI_NOT_FOUND The current image is not copied to the buffer.\r
221@retval EFI_UNSUPPORTED The operation is not supported.\r
222\r
223**/\r
224EFI_STATUS\r
225EFIAPI\r
226FmpDeviceGetImage(\r
227IN OUT VOID *Image,\r
228IN OUT UINTN *ImageSize\r
229)\r
230/*++\r
231\r
232Routine Description:\r
233\r
234 This is a function used to read the current firmware from the device into memory.\r
235 This is an optional function and can return EFI_UNSUPPORTED. This is useful for\r
236 test and diagnostics.\r
237\r
238Arguments:\r
239 Image -- Buffer to place the image into.\r
240 ImageSize -- Size of the Image buffer.\r
241\r
242Return Value:\r
243\r
244 EFI_STATUS code.\r
245 If not possible or not practical return EFI_UNSUPPORTED.\r
246\r
247--*/\r
248{\r
249 return EFI_UNSUPPORTED;\r
250}//GetImage()\r
251\r
252\r
253/**\r
254Updates the firmware image of the device.\r
255\r
256This function updates the hardware with the new firmware image.\r
257This function returns EFI_UNSUPPORTED if the firmware image is not updatable.\r
258If the firmware image is updatable, the function should perform the following minimal validations\r
259before proceeding to do the firmware image update.\r
260- Validate the image is a supported image for this device. The function returns EFI_ABORTED if\r
261the image is unsupported. The function can optionally provide more detailed information on\r
262why the image is not a supported image.\r
263- Validate the data from VendorCode if not null. Image validation must be performed before\r
264VendorCode data validation. VendorCode data is ignored or considered invalid if image\r
265validation failed. The function returns EFI_ABORTED if the data is invalid.\r
266\r
267VendorCode enables vendor to implement vendor-specific firmware image update policy. Null if\r
268the caller did not specify the policy or use the default policy. As an example, vendor can implement\r
269a policy to allow an option to force a firmware image update when the abort reason is due to the new\r
270firmware image version is older than the current firmware image version or bad image checksum.\r
271Sensitive operations such as those wiping the entire firmware image and render the device to be\r
272non-functional should be encoded in the image itself rather than passed with the VendorCode.\r
273AbortReason enables vendor to have the option to provide a more detailed description of the abort\r
274reason to the caller.\r
275\r
276@param[in] Image Points to the new image.\r
277@param[in] ImageSize Size of the new image in bytes.\r
278@param[in] VendorCode This enables vendor to implement vendor-specific firmware image update policy.\r
279Null indicates the caller did not specify the policy or use the default policy.\r
280@param[in] Progress A function used by the driver to report the progress of the firmware update.\r
281@param[in] CapsuleFwVersion FMP Payload Header version of the image\r
282@param[out] AbortReason A pointer to a pointer to a null-terminated string providing more\r
283details for the aborted operation. The buffer is allocated by this function\r
284with AllocatePool(), and it is the caller's responsibility to free it with a\r
285call to FreePool().\r
286\r
287@retval EFI_SUCCESS The device was successfully updated with the new image.\r
288@retval EFI_ABORTED The operation is aborted.\r
289@retval EFI_INVALID_PARAMETER The Image was NULL.\r
290@retval EFI_UNSUPPORTED The operation is not supported.\r
291\r
292**/\r
293EFI_STATUS\r
294EFIAPI\r
295FmpDeviceSetImage (\r
296IN CONST VOID *Image,\r
297IN UINTN ImageSize,\r
298IN CONST VOID *VendorCode,\r
299IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,\r
300IN UINT32 CapsuleFwVersion,\r
301OUT CHAR16 **AbortReason\r
302)\r
303{\r
304 EFI_STATUS Status = EFI_SUCCESS;\r
305 UINT32 Updateable = 0;\r
306\r
307 Status = FmpDeviceCheckImage(Image, ImageSize, &Updateable);\r
308 if (EFI_ERROR(Status))\r
309 {\r
310 DEBUG((DEBUG_ERROR, "SetImage - Check Image failed with %r.\n", Status));\r
311 goto cleanup;\r
312 }\r
313\r
314 if (Updateable != IMAGE_UPDATABLE_VALID)\r
315 {\r
316 DEBUG((DEBUG_ERROR, "SetImage - Check Image returned that the Image was not valid for update. Updatable value = 0x%X.\n", Updateable));\r
317 Status = EFI_ABORTED;\r
318 goto cleanup;\r
319 }\r
320\r
321 if (Progress == NULL)\r
322 {\r
323 DEBUG((DEBUG_ERROR, "SetImage - Invalid progress callback\n"));\r
324 Status = EFI_INVALID_PARAMETER;\r
325 goto cleanup;\r
326 }\r
327\r
328 Status = Progress(15);\r
329 if (EFI_ERROR(Status))\r
330 {\r
331 DEBUG((DEBUG_ERROR, "SetImage - Progress Callback failed with Status %r.\n", Status));\r
332 }\r
333\r
334 {\r
335 UINTN p;\r
336\r
337 for (p = 20; p < 100; p++) {\r
338 gBS->Stall (100000); //us = 0.1 seconds\r
339 Progress (p);\r
340 }\r
341 }\r
342\r
343 //TODO: add support for VendorCode, and AbortReason\r
344cleanup:\r
345 return Status;\r
346}// SetImage()\r
347\r
348\r
349\r
350/**\r
351Checks if the firmware image is valid for the device.\r
352\r
353This function allows firmware update application to validate the firmware image without\r
354invoking the SetImage() first.\r
355\r
356@param[in] Image Points to the new image.\r
357@param[in] ImageSize Size of the new image in bytes.\r
358@param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,\r
359if available, additional information if the image is invalid.\r
360\r
361@retval EFI_SUCCESS The image was successfully checked.\r
362@retval EFI_INVALID_PARAMETER The Image was NULL.\r
363\r
364**/\r
365EFI_STATUS\r
366EFIAPI\r
367FmpDeviceCheckImage(\r
368IN CONST VOID *Image,\r
369IN UINTN ImageSize,\r
370OUT UINT32 *ImageUpdateable\r
371)\r
372{\r
373 EFI_STATUS status = EFI_SUCCESS;\r
374\r
375 if (ImageUpdateable == NULL)\r
376 {\r
377 DEBUG((DEBUG_ERROR, "CheckImage - ImageUpdateable Pointer Parameter is NULL.\n"));\r
378 status = EFI_INVALID_PARAMETER;\r
379 goto cleanup;\r
380 }\r
381\r
382 //\r
383 //Set to valid and then if any tests fail it will update this flag.\r
384 //\r
385 *ImageUpdateable = IMAGE_UPDATABLE_VALID;\r
386\r
387 if (Image == NULL)\r
388 {\r
389 DEBUG((DEBUG_ERROR, "CheckImage - Image Pointer Parameter is NULL.\n"));\r
390 *ImageUpdateable = IMAGE_UPDATABLE_INVALID; //not sure if this is needed\r
391 return EFI_INVALID_PARAMETER;\r
392 }\r
393\r
394cleanup:\r
395 return status;\r
396}// CheckImage()\r
397\r
398/**\r
399Device firmware should trigger lock mechanism so that device fw can not be updated or tampered with.\r
400This lock mechanism is generally only cleared by a full system reset (not just sleep state/low power mode)\r
401\r
402@retval EFI_SUCCESS The device was successfully locked.\r
403@retval EFI_UNSUPPORTED The hardware device/firmware doesn't support locking\r
404\r
405**/\r
406EFI_STATUS\r
407EFIAPI\r
408FmpDeviceLock(\r
409)\r
410{\r
411 return EFI_SUCCESS;\r
412}\r