]> git.proxmox.com Git - mirror_edk2.git/blob - Vlv2TbltDevicePkg/Feature/Capsule/Library/FmpDeviceLib/FmpDeviceLib.c
Vlv2TbltDevicePkg/Capsule: Do not clear UEFI varstore
[mirror_edk2.git] / Vlv2TbltDevicePkg / Feature / Capsule / Library / FmpDeviceLib / FmpDeviceLib.c
1 /**
2
3 Copyright (c) 2016, Microsoft Corporation. All rights reserved.
4 Copyright (c) 2019, Intel Corporation. All rights reserved.
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <PiDxe.h>
11
12 #include <Library/FmpDeviceLib.h>
13
14 #include <Library/DebugLib.h>
15 #include <Library/BaseLib.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/MemoryAllocationLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19
20 #include <Library/PlatformFlashAccessLib.h>
21
22 //#include <Protocol/FirmwareManagement.h>
23
24 //#include <Guid/SystemResourceTable.h>
25
26 typedef struct {
27 PLATFORM_FIRMWARE_TYPE FirmwareType;
28 FLASH_ADDRESS_TYPE AddressType;
29 EFI_PHYSICAL_ADDRESS BaseAddress;
30 UINTN Length;
31 UINTN ImageOffset;
32 } UPDATE_CONFIG_DATA;
33
34 UPDATE_CONFIG_DATA mUpdateConfigData[] = {
35 { PlatformFirmwareTypeSystemFirmware, FlashAddressTypeRelativeAddress, 0x00000000, 0x00040000, 0x00000000 },
36 { PlatformFirmwareTypeSystemFirmware, FlashAddressTypeRelativeAddress, 0x000C0000, 0x00050000, 0x000C0000 },
37 { PlatformFirmwareTypeSystemFirmware, FlashAddressTypeRelativeAddress, 0x00110000, 0x00210000, 0x00110000 },
38 { PlatformFirmwareTypeSystemFirmware, FlashAddressTypeRelativeAddress, 0x00320000, 0x00070000, 0x00320000 },
39 { PlatformFirmwareTypeSystemFirmware, FlashAddressTypeRelativeAddress, 0x00390000, 0x00070000, 0x00390000 }
40 };
41
42 /**
43 Used to pass the FMP install function to this lib. This allows the library to
44 have control of the handle that the FMP instance is installed on. This allows
45 the library to use DriverBinding protocol model to locate its device(s) in the
46 system.
47
48 @param[in] Func Function pointer to FMP install function.
49
50 @retval EFI_SUCCESS Library has saved function pointer and will call
51 function pointer on each DriverBinding Start.
52 @retval EFI_UNSUPPORTED Library doesn't use driver binding and only supports
53 a single instance.
54 @retval other error Error occurred. Don't install FMP
55
56 **/
57 EFI_STATUS
58 EFIAPI
59 RegisterFmpInstaller (
60 IN FMP_DEVICE_LIB_REGISTER_FMP_INSTALLER Func
61 )
62 {
63 //
64 // This is a system firmware update that does not use Driver Binding Protocol
65 //
66 return EFI_UNSUPPORTED;
67 }
68
69
70 /**
71 Returns the size, in bytes, of the firmware image currently stored in the
72 firmware device. This function is used to by the GetImage() and
73 GetImageInfo() services of the Firmware Management Protocol. If the image
74 size can not be determined from the firmware device, then 0 must be returned.
75
76 @param[out] Size Pointer to the size, in bytes, of the firmware image
77 currently stored in the firmware device.
78
79 @retval EFI_SUCCESS The size of the firmware image currently
80 stored in the firmware device was returned.
81 @retval EFI_INVALID_PARAMETER Size is NULL.
82 @retval EFI_UNSUPPORTED The firmware device does not support reporting
83 the size of the currently stored firmware image.
84 @retval EFI_DEVICE_ERROR An error occured attempting to determine the
85 size of the firmware image currently stored in
86 in the firmware device.
87
88 **/
89 EFI_STATUS
90 EFIAPI
91 FmpDeviceGetSize (
92 IN UINTN *Size
93 )
94 {
95 if (Size == NULL) {
96 return EFI_INVALID_PARAMETER;
97 }
98 *Size = PcdGet32 (PcdBiosRomBase);
99 return EFI_SUCCESS;
100 }
101
102 /**
103 Used to return a library supplied guid that will be the ImageTypeId guid of
104 the FMP descriptor. This is optional but can be used if at runtime the guid
105 needs to be determined.
106
107 @param[out] Guid Double Guid Ptr that will be updated to point to guid.
108 This should be from static memory and will not be freed.
109
110 @return EFI_UNSUPPORTED Library instance doesn't need dynamic guid.
111 @return Error Any error will cause the wrapper to use the GUID
112 defined by PCD.
113 @return EFI_SUCCESS Guid ptr should be updated to point to static memeory
114 which contains a valid guid.
115
116 **/
117 EFI_STATUS
118 EFIAPI
119 FmpDeviceGetImageTypeIdGuidPtr (
120 OUT EFI_GUID **Guid
121 )
122 {
123 return EFI_UNSUPPORTED;
124 }
125
126 /**
127 Returns values used to fill in the AttributesSupported and AttributesSettings
128 fields of the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is returned by the
129 GetImageInfo() service of the Firmware Management Protocol. The following
130 bit values from the Firmware Management Protocol may be combined:
131 IMAGE_ATTRIBUTE_IMAGE_UPDATABLE
132 IMAGE_ATTRIBUTE_RESET_REQUIRED
133 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED
134 IMAGE_ATTRIBUTE_IN_USE
135 IMAGE_ATTRIBUTE_UEFI_IMAGE
136
137 @param[out] Supported Attributes supported by this firmware device.
138 @param[out] Setting Attributes settings for this firmware device.
139
140 @retval EFI_SUCCESS The attributes supported by the firmware
141 device were returned.
142 @retval EFI_INVALID_PARAMETER Supported is NULL.
143 @retval EFI_INVALID_PARAMETER Setting is NULL.
144
145 **/
146 EFI_STATUS
147 EFIAPI
148 FmpDeviceGetAttributes (
149 IN OUT UINT64 *Supported,
150 IN OUT UINT64 *Setting
151 )
152 {
153 if (Supported == NULL || Setting == NULL) {
154 return EFI_INVALID_PARAMETER;
155 }
156 *Supported = (IMAGE_ATTRIBUTE_IMAGE_UPDATABLE |
157 IMAGE_ATTRIBUTE_RESET_REQUIRED |
158 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED |
159 IMAGE_ATTRIBUTE_IN_USE
160 );
161 *Setting = (IMAGE_ATTRIBUTE_IMAGE_UPDATABLE |
162 IMAGE_ATTRIBUTE_RESET_REQUIRED |
163 IMAGE_ATTRIBUTE_AUTHENTICATION_REQUIRED |
164 IMAGE_ATTRIBUTE_IN_USE
165 );
166 return EFI_SUCCESS;
167 }
168
169 /**
170 Gets the current Lowest Supported Version.
171
172 This is a protection mechanism so that a previous version with known issue is
173 not applied. ONLY implement this if your running firmware has a method to
174 return this at runtime. If EFI_UNSUPPORTED is returned, then the Lowest
175 Supported Version is stored in a UEFI Variable.
176
177 @param[out] Version On return this value represents the current Lowest
178 Supported Version (in same format as GetVersion).
179
180 @retval EFI_SUCCESS The Lowest Supported Version was correctly retrieved
181 @retval EFI_UNSUPPORTED Device firmware doesn't support reporting LSV
182 @retval EFI_DEVICE_ERROR Error occurred when trying to get the LSV
183 **/
184 EFI_STATUS
185 EFIAPI
186 FmpDeviceGetLowestSupportedVersion (
187 IN OUT UINT32 *LowestSupportedVersion
188 )
189 {
190 //
191 // Retrieve the lowest support version from a PCD
192 // NOTE: This method of using a PCD can only be used for the system firmware
193 // FMP instance that is updated every time the system firmware is
194 // updated. If system firmware updates support partial updates that
195 // would not include the system firmware FMP instance, then a PCD can
196 // not be used and the value must come from the currently running system
197 // firmware image.
198 //
199 *LowestSupportedVersion = PcdGet32 (PcdSystemFirmwareFmpLowestSupportedVersion);
200 return EFI_SUCCESS;
201 }
202
203
204 /**
205 Returns the Null-terminated Unicode string that is used to fill in the
206 VersionName field of the EFI_FIRMWARE_IMAGE_DESCRIPTOR structure that is
207 returned by the GetImageInfo() service of the Firmware Management Protocol.
208 The returned string must be allocated using EFI_BOOT_SERVICES.AllocatePool().
209
210 @note It is recommended that all firmware devices support a method to report
211 the VersionName string from the currently stored firmware image.
212
213 @param[out] VersionString The version string retrieved from the currently
214 stored firmware image.
215
216 @retval EFI_SUCCESS The version string of currently stored
217 firmware image was returned in Version.
218 @retval EFI_INVALID_PARAMETER VersionString is NULL.
219 @retval EFI_UNSUPPORTED The firmware device does not support a method
220 to report the version string of the currently
221 stored firmware image.
222 @retval EFI_DEVICE_ERROR An error occurred attempting to retrieve the
223 version string of the currently stored
224 firmware image.
225 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate the
226 buffer for the version string of the currently
227 stored firmware image.
228
229 **/
230 EFI_STATUS
231 EFIAPI
232 FmpDeviceGetVersionString (
233 OUT CHAR16 **VersionString
234 )
235 {
236 if (VersionString == NULL) {
237 return EFI_INVALID_PARAMETER;
238 }
239
240 //
241 // Retrieve the version string from a PCD
242 // NOTE: This method of using a PCD can only be used for the system firmware
243 // FMP instance that is updated every time the system firmware is
244 // updated. If system firmware updates support partial updates that
245 // would not include the system firmware FMP instance, then a PCD can
246 // not be used and the value must come from the currently running system
247 // firmware image.
248 //
249 *VersionString = (CHAR16 *)AllocateCopyPool (
250 PcdGetSize (PcdSystemFirmwareFmpVersionString),
251 PcdGetPtr (PcdSystemFirmwareFmpVersionString)
252 );
253 if (*VersionString == NULL) {
254 return EFI_OUT_OF_RESOURCES;
255 }
256 return EFI_SUCCESS;
257 }
258
259 /**
260 Gets the current running version.
261
262 ONLY implement this if your running firmware has a method to return this at
263 runtime.
264
265 @param[out] Version On return this value represents the current running
266 version.
267
268 @retval EFI_SUCCESS The version was correctly retrieved.
269 @retval EFI_UNSUPPORTED Device firmware doesn't support reporting current
270 version.
271 @retval EFI_DEVICE_ERROR Error occurred when trying to get the version.
272 **/
273 EFI_STATUS
274 EFIAPI
275 FmpDeviceGetVersion (
276 IN OUT UINT32 *Version
277 )
278 {
279 //
280 // Retrieve the version string from a PCD
281 // NOTE: This method of using a PCD can only be used for the system firmware
282 // FMP instance that is updated every time the system firmware is
283 // updated. If system firmware updates support partial updates that
284 // would not include the system firmware FMP instance, then a PCD can
285 // not be used and the value must come from the currently running system
286 // firmware image.
287 //
288 *Version = PcdGet32 (PcdSystemFirmwareFmpVersion);
289 return EFI_SUCCESS;
290 }
291
292
293 /**
294 Retrieves a copy of the current firmware image of the device.
295
296 This function allows a copy of the current firmware image to be created and
297 saved. The saved copy could later been used, for example, in firmware image
298 recovery or rollback.
299
300 @param[out] Image Points to the buffer where the current image is copied
301 to.
302 @param[out] ImageSize On entry, points to the size of the buffer pointed to
303 by Image, in bytes. On return, points to the length of
304 the image, in bytes.
305
306 @retval EFI_SUCCESS The image was successfully read from the device.
307 @retval EFI_BUFFER_TOO_SMALL The buffer specified by ImageSize is too small
308 to hold the image. The current buffer size
309 needed to hold the image is returned in
310 ImageSize.
311 @retval EFI_INVALID_PARAMETER The Image was NULL.
312 @retval EFI_NOT_FOUND The current image is not copied to the buffer.
313 @retval EFI_UNSUPPORTED The operation is not supported.
314
315 **/
316 EFI_STATUS
317 EFIAPI
318 FmpDeviceGetImage (
319 IN OUT VOID *Image,
320 IN OUT UINTN *ImageSize
321 )
322 {
323 //
324 // Check for invalid p;arameters
325 //
326 if (Image == NULL || ImageSize == NULL) {
327 return EFI_INVALID_PARAMETER;
328 }
329
330 //
331 // Make sure the buffer is big enough to hold the device image
332 //
333 if (*ImageSize < PcdGet32 (PcdBiosRomSize)) {
334 *ImageSize = PcdGet32 (PcdBiosRomSize);
335 return EFI_BUFFER_TOO_SMALL;
336 }
337
338 //
339 // Copy the device image to the buffer
340 //
341 *ImageSize = PcdGet32 (PcdBiosRomSize);
342 CopyMem (
343 Image,
344 (VOID *)(UINTN)PcdGet32 (PcdBiosRomBase),
345 *ImageSize
346 );
347
348 return EFI_SUCCESS;
349 }
350
351 /**
352 Updates the firmware image of the device.
353
354 This function updates the hardware with the new firmware image. This function
355 returns EFI_UNSUPPORTED if the firmware image is not updatable. If the
356 firmware image is updatable, the function should perform the following minimal
357 validations before proceeding to do the firmware image update.
358 - Validate the image is a supported image for this device. The function
359 returns EFI_ABORTED if the image is unsupported. The function can
360 optionally provide more detailed information on why the image is not a
361 supported image.
362 - Validate the data from VendorCode if not null. Image validation must be
363 performed before VendorCode data validation. VendorCode data is ignored
364 or considered invalid if image validation failed. The function returns
365 EFI_ABORTED if the data is invalid.
366
367 VendorCode enables vendor to implement vendor-specific firmware image update
368 policy. Null if the caller did not specify the policy or use the default
369 policy. As an example, vendor can implement a policy to allow an option to
370 force a firmware image update when the abort reason is due to the new firmware
371 image version is older than the current firmware image version or bad image
372 checksum. Sensitive operations such as those wiping the entire firmware image
373 and render the device to be non-functional should be encoded in the image
374 itself rather than passed with the VendorCode. AbortReason enables vendor to
375 have the option to provide a more detailed description of the abort reason to
376 the caller.
377
378 @param[in] Image Points to the new image.
379 @param[in] ImageSize Size of the new image in bytes.
380 @param[in] VendorCode This enables vendor to implement vendor-specific
381 firmware image update policy. Null indicates the
382 caller did not specify the policy or use the
383 default policy.
384 @param[in] Progress A function used by the driver to report the
385 progress of the firmware update.
386 @param[in] CapsuleFwVersion FMP Payload Header version of the image.
387 @param[out] AbortReason A pointer to a pointer to a null-terminated
388 string providing more details for the aborted
389 operation. The buffer is allocated by this
390 function with AllocatePool(), and it is the
391 caller's responsibility to free it with a call
392 to FreePool().
393
394 @retval EFI_SUCCESS The device was successfully updated with the
395 new image.
396 @retval EFI_ABORTED The operation is aborted.
397 @retval EFI_INVALID_PARAMETER The Image was NULL.
398 @retval EFI_UNSUPPORTED The operation is not supported.
399
400 **/
401 EFI_STATUS
402 EFIAPI
403 FmpDeviceSetImage (
404 IN CONST VOID *Image,
405 IN UINTN ImageSize,
406 IN CONST VOID *VendorCode,
407 IN EFI_FIRMWARE_MANAGEMENT_UPDATE_IMAGE_PROGRESS Progress,
408 IN UINT32 CapsuleFwVersion,
409 OUT CHAR16 **AbortReason
410 )
411 {
412 EFI_STATUS Status;
413 UINT32 Updateable;
414 UINTN Percentage;
415 UINTN Index;
416 UPDATE_CONFIG_DATA *ConfigData;
417 UINTN TotalSize;
418 UINTN BytesWritten;
419
420 Updateable = 0;
421 Status = FmpDeviceCheckImage (Image, ImageSize, &Updateable);
422 if (EFI_ERROR (Status)) {
423 DEBUG((DEBUG_ERROR, "FmpDeviceSetImage - Check Image failed with %r.\n", Status));
424 return Status;
425 }
426
427 if (Updateable != IMAGE_UPDATABLE_VALID) {
428 DEBUG((DEBUG_ERROR, "FmpDeviceSetImage - Check Image returned that the Image was not valid for update. Updatable value = 0x%X.\n", Updateable));
429 return EFI_ABORTED;
430 }
431
432 if (Progress == NULL) {
433 DEBUG((DEBUG_ERROR, "FmpDeviceSetImage - Invalid progress callback\n"));
434 return EFI_INVALID_PARAMETER;
435 }
436
437 Status = Progress (15);
438 if (EFI_ERROR (Status)) {
439 DEBUG((DEBUG_ERROR, "FmpDeviceSetImage - Progress Callback failed with Status %r.\n", Status));
440 }
441
442 //
443 // Write the image to the firmware device
444 //
445 Progress (20);
446 if (EFI_ERROR (Status)) {
447 DEBUG((DEBUG_ERROR, "FmpDeviceSetImage - Progress Callback failed with Status %r.\n", Status));
448 }
449
450 //
451 // Simulate update with delays between progress updates
452 //
453 for (Percentage = 20; Percentage <= 100; Percentage++) {
454 //
455 // Wait 0.05 seconds
456 //
457 // gBS->Stall (50000);
458
459 // Progress (Percentage);
460 // if (EFI_ERROR (Status)) {
461 // DEBUG((DEBUG_ERROR, "FmpDeviceSetImage - Progress Callback failed with Status %r.\n", Status));
462 // }
463 }
464
465 DEBUG ((DEBUG_INFO, "FmpDeviceSetImage - %d Images ...\n", ARRAY_SIZE (mUpdateConfigData)));
466
467 if (ARRAY_SIZE (mUpdateConfigData) == 0) {
468 DEBUG((DEBUG_INFO, "PlatformUpdate: BaseAddress - 0x%lx ImageOffset - 0x%x Length - 0x%x\n", 0, 0, ImageSize));
469 Status = PerformFlashWriteWithProgress (
470 PlatformFirmwareTypeSystemFirmware, // FirmwareType
471 0x00000000, // FlashAddress
472 FlashAddressTypeRelativeAddress, // FlashAddressType
473 (VOID *)(UINTN)Image, // Buffer
474 ImageSize, // BufferLength
475 Progress, // Progress
476 20, // StartPercentage
477 100 // EndPercentage
478 );
479 }
480
481
482 //
483 // Compute total size of update
484 //
485 for (Index = 0, TotalSize = 0; Index < ARRAY_SIZE (mUpdateConfigData); Index++) {
486 TotalSize += mUpdateConfigData[Index].Length;
487 }
488
489 BytesWritten = 0;
490 for (Index = 0, ConfigData = mUpdateConfigData; Index < ARRAY_SIZE (mUpdateConfigData); Index++, ConfigData++) {
491 DEBUG((DEBUG_INFO, "PlatformUpdate(%d): BaseAddress - 0x%lx ImageOffset - 0x%x Length - 0x%x\n",
492 Index,
493 ConfigData->BaseAddress,
494 ConfigData->ImageOffset,
495 ConfigData->Length
496 ));
497 Status = PerformFlashWriteWithProgress (
498 ConfigData->FirmwareType, // FirmwareType
499 ConfigData->BaseAddress, // FlashAddress
500 ConfigData->AddressType, // FlashAddressType
501 (VOID *)((UINTN)Image + (UINTN)ConfigData->ImageOffset), // Buffer
502 ConfigData->Length, // BufferLength
503 Progress, // Progress
504 20 + (BytesWritten * 80) / TotalSize, // StartPercentage
505 20 + ((BytesWritten + ConfigData->Length) * 80) / TotalSize // EndPercentage
506 );
507 if (EFI_ERROR(Status)) {
508 break;
509 }
510 BytesWritten += ConfigData->Length;
511 }
512
513 DEBUG ((DEBUG_INFO, "FmpDeviceSetImage - %r\n", Status));
514
515 return Status;
516 }
517
518 /**
519 Checks if the firmware image is valid for the device.
520
521 This function allows firmware update application to validate the firmware image without
522 invoking the SetImage() first.
523
524 @param[in] Image Points to the new image.
525 @param[in] ImageSize Size of the new image in bytes.
526 @param[out] ImageUpdatable Indicates if the new image is valid for update. It also provides,
527 if available, additional information if the image is invalid.
528
529 @retval EFI_SUCCESS The image was successfully checked.
530 @retval EFI_INVALID_PARAMETER The Image was NULL.
531
532 **/
533 EFI_STATUS
534 EFIAPI
535 FmpDeviceCheckImage (
536 IN CONST VOID *Image,
537 IN UINTN ImageSize,
538 OUT UINT32 *ImageUpdateable
539 )
540 {
541 if (ImageUpdateable == NULL) {
542 DEBUG((DEBUG_ERROR, "CheckImage - ImageUpdateable Pointer Parameter is NULL.\n"));
543 return EFI_INVALID_PARAMETER;
544 }
545
546 //
547 //Set to valid and then if any tests fail it will update this flag.
548 //
549 *ImageUpdateable = IMAGE_UPDATABLE_VALID;
550
551 if (Image == NULL) {
552 DEBUG((DEBUG_ERROR, "CheckImage - Image Pointer Parameter is NULL.\n"));
553 //
554 // Not sure if this is needed
555 //
556 *ImageUpdateable = IMAGE_UPDATABLE_INVALID;
557 return EFI_INVALID_PARAMETER;
558 }
559
560 //
561 // Make sure the image size is correct
562 //
563 if (ImageSize != PcdGet32 (PcdBiosRomSize)) {
564 *ImageUpdateable = IMAGE_UPDATABLE_INVALID;
565 return EFI_INVALID_PARAMETER;
566 }
567
568 return EFI_SUCCESS;
569 }
570
571 /**
572 Device firmware should trigger lock mechanism so that device fw can not be
573 updated or tampered with. This lock mechanism is generally only cleared by a
574 full system reset (not just sleep state/low power mode)
575
576 @retval EFI_SUCCESS The device was successfully locked.
577 @retval EFI_UNSUPPORTED The hardware device/firmware doesn't support locking
578
579 **/
580 EFI_STATUS
581 EFIAPI
582 FmpDeviceLock (
583 VOID
584 )
585 {
586 DEBUG ((DEBUG_INFO, "VLV2: FmpDeviceLock() for system FLASH\n"));
587 // TODO: Add lock logic
588 return EFI_UNSUPPORTED;
589 }