S3NvramSave.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /** @file
  2. @copyright
  3. Copyright 1999 - 2021 Intel Corporation. <BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "S3NvramSave.h"
  7. #include <Library/MemoryAllocationLib.h>
  8. #include <Library/LargeVariableReadLib.h>
  9. #include <Library/LargeVariableWriteLib.h>
  10. /**
  11. Verify the SysHost structure size.
  12. Verifies that the size of the SysHost structure in PEI is
  13. the same as the size of the SysHost structure and DXE and
  14. ASSERT's is the size is not the same.
  15. This is typically caused by the use of pointers or UINTNs
  16. in the SysHost structure, neither of those datatypes are
  17. allowed in SysHost.
  18. @param None
  19. @retval None
  20. **/
  21. VOID
  22. VerifySysHostStructureSize (
  23. VOID
  24. )
  25. {
  26. if (PcdGet32 (PcdPeiSyshostMemorySize) != sizeof (SYSHOST)) {
  27. DEBUG ((EFI_D_ERROR, "ERROR: In DXE sizeof SysHost = %d, in PEI sizeof SysHost = %d\n",
  28. sizeof (SYSHOST),
  29. PcdGet32 (PcdPeiSyshostMemorySize)
  30. ));
  31. DEBUG ((EFI_D_ERROR, "Size of SysHost must match in PEI and DXE\n"));
  32. ASSERT (FALSE);
  33. }
  34. return;
  35. } // VerifySysHostStructureSize
  36. /**
  37. Saves the FSP Non-Volatile Storage HOB to the UEFI Variable Services
  38. @param None
  39. @retval EFI_SUCCESS The FSP Non-Volatile Storage HOB was successfully saved.
  40. @retval EFI_ERROR The FSP Non-Volatile Storage HOB was not successfully saved.
  41. **/
  42. EFI_STATUS
  43. SaveFspNonVolatileStorageHob (
  44. VOID
  45. )
  46. {
  47. EFI_STATUS Status;
  48. EFI_HOB_GUID_TYPE *GuidHob;
  49. VOID *HobData;
  50. VOID *VariableData;
  51. UINTN DataSize;
  52. UINTN FspNvsBufferSize;
  53. BOOLEAN DataIsIdentical;
  54. FspNvsBufferSize = 0;
  55. DataSize = 0;
  56. VariableData = NULL;
  57. GuidHob = NULL;
  58. HobData = NULL;
  59. DataIsIdentical = FALSE;
  60. Status = EFI_SUCCESS;
  61. DEBUG ((DEBUG_INFO, "Saving FSP / MRC Training Data\n"));
  62. GuidHob = GetFirstGuidHob (&gFspNonVolatileStorageHobGuid);
  63. if (GuidHob != NULL) {
  64. HobData = GET_GUID_HOB_DATA (GuidHob);
  65. DataSize = GET_GUID_HOB_DATA_SIZE (GuidHob);
  66. if (DataSize > 0) {
  67. //
  68. // Check if the presently saved data is identical to the data given by MRC/FSP
  69. //
  70. Status = GetLargeVariable (L"FspNvsBuffer", &gFspNonVolatileStorageHobGuid, &FspNvsBufferSize, NULL);
  71. if (Status == EFI_BUFFER_TOO_SMALL) {
  72. if (FspNvsBufferSize == DataSize) {
  73. VariableData = AllocatePool (FspNvsBufferSize);
  74. if (VariableData != NULL) {
  75. Status = GetLargeVariable (L"FspNvsBuffer", &gFspNonVolatileStorageHobGuid, &FspNvsBufferSize, VariableData);
  76. if (!EFI_ERROR (Status) && (FspNvsBufferSize == DataSize) && (0 == CompareMem (HobData, VariableData, DataSize))) {
  77. DataIsIdentical = TRUE;
  78. }
  79. FreePool (VariableData);
  80. }
  81. }
  82. }
  83. Status = EFI_SUCCESS;
  84. if (!DataIsIdentical) {
  85. Status = SetLargeVariable (L"FspNvsBuffer", &gFspNonVolatileStorageHobGuid, TRUE, DataSize, HobData);
  86. ASSERT_EFI_ERROR (Status);
  87. DEBUG ((DEBUG_INFO, "Saved size of FSP / MRC Training Data: 0x%x\n", DataSize));
  88. } else {
  89. DEBUG ((DEBUG_INFO, "FSP / MRC Training Data is identical to data from last boot, no need to save.\n"));
  90. }
  91. }
  92. }
  93. return Status;
  94. }
  95. EFI_STATUS
  96. EFIAPI
  97. S3NvramSaveEntry (
  98. IN EFI_HANDLE ImageHandle,
  99. IN EFI_SYSTEM_TABLE *SystemTable
  100. )
  101. /**
  102. This is the main entry point of the S3 NVRAM Save module.
  103. @param ImageHandle - Handle for the image of this driver.
  104. @param SystemTable - Pointer to the EFI System Table.
  105. @retval EFI_SUCCESS - Module launched successfully.
  106. **/
  107. {
  108. EFI_STATUS Status = EFI_SUCCESS;
  109. //
  110. // Check that SysHost size in DXE is the same as PEI.
  111. //
  112. #ifndef FSP_API_MODE
  113. VerifySysHostStructureSize ();
  114. #endif
  115. //
  116. // Save structures into NVRAM if needed
  117. //
  118. Status = SaveFspNonVolatileStorageHob ();
  119. if (EFI_ERROR (Status)) {
  120. DEBUG ((DEBUG_ERROR, "Error: FSP NVRAM Data NOT Saved! Status: %r\n", Status));
  121. Status = EFI_SUCCESS;
  122. }
  123. return Status;
  124. }