FvbInfo.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**@file
  2. Defines data structure that is the volume header found.
  3. These data is intent to decouple FVB driver with FV header.
  4. Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <PiDxe.h>
  8. #include <Protocol/FirmwareVolumeBlock.h>
  9. #include <Library/PcdLib.h>
  10. #include <Library/DebugLib.h>
  11. #include <Library/BaseLib.h>
  12. #include <Guid/FirmwareFileSystem2.h>
  13. #include <Guid/SystemNvDataGuid.h>
  14. #define FIRMWARE_BLOCK_SIZE 0x8000
  15. #define FVB_MEDIA_BLOCK_SIZE (FIRMWARE_BLOCK_SIZE * 2)
  16. #define FV_RECOVERY_BASE_ADDRESS FixedPcdGet32(PcdFlashFvRecoveryBase)
  17. #define RECOVERY_BIOS_BLOCK_NUM (FixedPcdGet32(PcdFlashFvRecoverySize) / FVB_MEDIA_BLOCK_SIZE)
  18. #define FV_MAIN_BASE_ADDRESS FixedPcdGet32(PcdFlashFvMainBase)
  19. #define MAIN_BIOS_BLOCK_NUM (FixedPcdGet32(PcdFlashFvMainSize) / FVB_MEDIA_BLOCK_SIZE)
  20. #define NV_STORAGE_BASE_ADDRESS FixedPcdGet32(PcdFlashNvStorageVariableBase)
  21. #define SYSTEM_NV_BLOCK_NUM ((FixedPcdGet32(PcdFlashNvStorageVariableSize)+ FixedPcdGet32(PcdFlashNvStorageFtwWorkingSize) + FixedPcdGet32(PcdFlashNvStorageFtwSpareSize))/ FVB_MEDIA_BLOCK_SIZE)
  22. typedef struct {
  23. EFI_PHYSICAL_ADDRESS BaseAddress;
  24. EFI_FIRMWARE_VOLUME_HEADER FvbInfo;
  25. EFI_FV_BLOCK_MAP_ENTRY End[1];
  26. } EFI_FVB2_MEDIA_INFO;
  27. //
  28. // This data structure contains a template of all correct FV headers, which is used to restore
  29. // Fv header if it's corrupted.
  30. //
  31. EFI_FVB2_MEDIA_INFO mPlatformFvbMediaInfo[] = {
  32. //
  33. // Main BIOS FVB
  34. //
  35. {
  36. FV_MAIN_BASE_ADDRESS,
  37. {
  38. {0,}, //ZeroVector[16]
  39. EFI_FIRMWARE_FILE_SYSTEM2_GUID,
  40. FVB_MEDIA_BLOCK_SIZE * MAIN_BIOS_BLOCK_NUM,
  41. EFI_FVH_SIGNATURE,
  42. 0x0004feff, // check MdePkg/Include/Pi/PiFirmwareVolume.h for details on EFI_FVB_ATTRIBUTES_2
  43. sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY),
  44. 0, //CheckSum which will be calucated dynamically.
  45. 0, //ExtHeaderOffset
  46. {0,}, //Reserved[1]
  47. 2, //Revision
  48. {
  49. {
  50. MAIN_BIOS_BLOCK_NUM,
  51. FVB_MEDIA_BLOCK_SIZE,
  52. }
  53. }
  54. },
  55. {
  56. {
  57. 0,
  58. 0
  59. }
  60. }
  61. },
  62. //
  63. // Systen NvStorage FVB
  64. //
  65. {
  66. NV_STORAGE_BASE_ADDRESS,
  67. {
  68. {0,}, //ZeroVector[16]
  69. EFI_SYSTEM_NV_DATA_FV_GUID,
  70. FVB_MEDIA_BLOCK_SIZE * SYSTEM_NV_BLOCK_NUM,
  71. EFI_FVH_SIGNATURE,
  72. 0x0004feff, // check MdePkg/Include/Pi/PiFirmwareVolume.h for details on EFI_FVB_ATTRIBUTES_2
  73. sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY),
  74. 0, //CheckSum which will be calucated dynamically.
  75. 0, //ExtHeaderOffset
  76. {0,}, //Reserved[1]
  77. 2, //Revision
  78. {
  79. {
  80. SYSTEM_NV_BLOCK_NUM,
  81. FVB_MEDIA_BLOCK_SIZE,
  82. }
  83. }
  84. },
  85. {
  86. {
  87. 0,
  88. 0
  89. }
  90. }
  91. },
  92. //
  93. // Recovery BIOS FVB
  94. //
  95. {
  96. FV_RECOVERY_BASE_ADDRESS,
  97. {
  98. {0,}, //ZeroVector[16]
  99. EFI_FIRMWARE_FILE_SYSTEM2_GUID,
  100. FVB_MEDIA_BLOCK_SIZE * RECOVERY_BIOS_BLOCK_NUM,
  101. EFI_FVH_SIGNATURE,
  102. 0x0004feff, // check MdePkg/Include/Pi/PiFirmwareVolume.h for details on EFI_FVB_ATTRIBUTES_2
  103. sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY),
  104. 0, //CheckSum which will be calucated dynamically.
  105. 0, //ExtHeaderOffset
  106. {0,}, //Reserved[1]
  107. 2, //Revision
  108. {
  109. {
  110. RECOVERY_BIOS_BLOCK_NUM,
  111. FVB_MEDIA_BLOCK_SIZE,
  112. }
  113. }
  114. },
  115. {
  116. {
  117. 0,
  118. 0
  119. }
  120. }
  121. }
  122. };
  123. EFI_STATUS
  124. GetFvbInfo (
  125. IN EFI_PHYSICAL_ADDRESS FvBaseAddress,
  126. OUT EFI_FIRMWARE_VOLUME_HEADER **FvbInfo
  127. )
  128. {
  129. UINTN Index;
  130. EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
  131. for (Index = 0; Index < sizeof (mPlatformFvbMediaInfo) / sizeof (EFI_FVB2_MEDIA_INFO); Index += 1) {
  132. if (mPlatformFvbMediaInfo[Index].BaseAddress == FvBaseAddress) {
  133. FvHeader = &mPlatformFvbMediaInfo[Index].FvbInfo;
  134. //
  135. // Update the checksum value of FV header.
  136. //
  137. FvHeader->Checksum = CalculateCheckSum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength);
  138. *FvbInfo = FvHeader;
  139. DEBUG ((EFI_D_INFO, "\nBaseAddr: 0x%lx \n", FvBaseAddress));
  140. DEBUG ((EFI_D_INFO, "FvLength: 0x%lx \n", (*FvbInfo)->FvLength));
  141. DEBUG ((EFI_D_INFO, "HeaderLength: 0x%x \n", (*FvbInfo)->HeaderLength));
  142. DEBUG ((EFI_D_INFO, "FvBlockMap[0].NumBlocks: 0x%x \n", (*FvbInfo)->BlockMap[0].NumBlocks));
  143. DEBUG ((EFI_D_INFO, "FvBlockMap[0].BlockLength: 0x%x \n", (*FvbInfo)->BlockMap[0].Length));
  144. DEBUG ((EFI_D_INFO, "FvBlockMap[1].NumBlocks: 0x%x \n", (*FvbInfo)->BlockMap[1].NumBlocks));
  145. DEBUG ((EFI_D_INFO, "FvBlockMap[1].BlockLength: 0x%x \n\n", (*FvbInfo)->BlockMap[1].Length));
  146. return EFI_SUCCESS;
  147. }
  148. }
  149. return EFI_NOT_FOUND;
  150. }