SaveMemoryConfig.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. Module Name:
  5. SaveMemoryConfig.c
  6. Abstract:
  7. This is the driver that locates the MemoryConfigurationData HOB, if it
  8. exists, and saves the data to nvRAM.
  9. --*/
  10. #include "SaveMemoryConfig.h"
  11. CHAR16 EfiMemoryConfigVariable[] = L"MemoryConfig";
  12. EFI_STATUS
  13. EFIAPI
  14. SaveMemoryConfigEntryPoint (
  15. IN EFI_HANDLE ImageHandle,
  16. IN EFI_SYSTEM_TABLE *SystemTable
  17. )
  18. /*++
  19. Routine Description:
  20. This is the standard EFI driver point that detects whether there is a
  21. MemoryConfigurationData HOB and, if so, saves its data to nvRAM.
  22. Arguments:
  23. ImageHandle - Handle for the image of this driver
  24. SystemTable - Pointer to the EFI System Table
  25. Returns:
  26. EFI_SUCCESS - if the data is successfully saved or there was no data
  27. EFI_NOT_FOUND - if the HOB list could not be located.
  28. EFI_UNLOAD_IMAGE - It is not success
  29. --*/
  30. {
  31. EFI_STATUS Status=EFI_SUCCESS;
  32. VOID *MemHobData;
  33. VOID *VariableData;
  34. UINTN BufferSize;
  35. BOOLEAN MfgMode;
  36. EFI_PLATFORM_SETUP_ID *BootModeBuffer;
  37. EFI_PLATFORM_INFO_HOB *PlatformInfoHobPtr;
  38. MEM_INFO_PROTOCOL *MemInfoProtocol;
  39. EFI_HANDLE Handle;
  40. UINT8 Channel, Slot;
  41. VOID *GuidHob;
  42. VariableData = NULL;
  43. MfgMode = FALSE;
  44. Handle = NULL;
  45. BootModeBuffer = NULL;
  46. MemHobData = NULL;
  47. PlatformInfoHobPtr = NULL;
  48. BufferSize = 0;
  49. //
  50. // Get Platform Info HOB
  51. //
  52. GuidHob = GetFirstGuidHob (&gEfiPlatformInfoGuid);
  53. if (GuidHob == NULL) {
  54. Status = EFI_NOT_FOUND;
  55. }
  56. ASSERT_EFI_ERROR (Status);
  57. PlatformInfoHobPtr = GET_GUID_HOB_DATA (GuidHob);
  58. //
  59. // Get the BootMode guid hob
  60. //
  61. GuidHob = GetFirstGuidHob (&gEfiPlatformBootModeGuid);
  62. if (GuidHob == NULL) {
  63. Status = EFI_NOT_FOUND;
  64. }
  65. ASSERT_EFI_ERROR (Status);
  66. BootModeBuffer = GET_GUID_HOB_DATA (GuidHob);
  67. //
  68. // Check whether in Manufacturing Mode
  69. //
  70. if (BootModeBuffer) {
  71. if ( !CompareMem ( //EfiCompareMem
  72. &BootModeBuffer->SetupName,
  73. MANUFACTURE_SETUP_NAME,
  74. StrSize (MANUFACTURE_SETUP_NAME) //EfiStrSize
  75. ) ) {
  76. MfgMode = TRUE;
  77. }
  78. }
  79. if (MfgMode) {
  80. //
  81. // Don't save Memory Configuration in Manufacturing Mode. Clear memory configuration.
  82. //
  83. Status = gRT->SetVariable (
  84. EfiMemoryConfigVariable,
  85. &gEfiVlv2VariableGuid,
  86. EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
  87. 0,
  88. NULL
  89. );
  90. } else {
  91. MemInfoProtocol = (MEM_INFO_PROTOCOL*)AllocateZeroPool(sizeof(MEM_INFO_PROTOCOL));
  92. if (PlatformInfoHobPtr != NULL) {
  93. MemInfoProtocol->MemInfoData.memSize = 0;
  94. for (Channel = 0; Channel < CH_NUM; Channel ++){
  95. for (Slot = 0; Slot < DIMM_NUM; Slot ++){
  96. MemInfoProtocol->MemInfoData.dimmSize[Slot + (Channel * DIMM_NUM)] = PlatformInfoHobPtr->MemData.DimmSize[Slot + (Channel * DIMM_NUM)];
  97. }
  98. }
  99. MemInfoProtocol->MemInfoData.memSize = PlatformInfoHobPtr->MemData.MemSize;
  100. MemInfoProtocol->MemInfoData.EccSupport = PlatformInfoHobPtr->MemData.EccSupport;
  101. MemInfoProtocol->MemInfoData.ddrFreq = PlatformInfoHobPtr->MemData.DdrFreq;
  102. MemInfoProtocol->MemInfoData.ddrType = PlatformInfoHobPtr->MemData.DdrType;
  103. if (MemInfoProtocol->MemInfoData.memSize == 0){
  104. //
  105. // We hardcode if MRC didn't fill these info in
  106. //
  107. MemInfoProtocol->MemInfoData.memSize = 0x800; //per 1MB
  108. MemInfoProtocol->MemInfoData.dimmSize[0] = 0x800;
  109. MemInfoProtocol->MemInfoData.dimmSize[1] = 0;
  110. MemInfoProtocol->MemInfoData.EccSupport = FALSE;
  111. MemInfoProtocol->MemInfoData.ddrType = 5; //DDRType_LPDDR3
  112. }
  113. Status = gBS->InstallMultipleProtocolInterfaces (
  114. &Handle,
  115. &gMemInfoProtocolGuid,
  116. MemInfoProtocol,
  117. NULL
  118. );
  119. }
  120. Status = EFI_SUCCESS;
  121. if (BOOT_WITH_MINIMAL_CONFIGURATION != GetBootModeHob()){
  122. //
  123. // Get the Memory Config guid hob
  124. //
  125. GuidHob = GetFirstGuidHob (&gEfiMemoryConfigDataGuid);
  126. if (GuidHob == NULL) {
  127. Status = EFI_NOT_FOUND;
  128. }
  129. ASSERT_EFI_ERROR (Status);
  130. MemHobData = GET_GUID_HOB_DATA (GuidHob);
  131. BufferSize = GET_GUID_HOB_DATA_SIZE (GuidHob);
  132. Status = gRT->GetVariable (
  133. EfiMemoryConfigVariable,
  134. &gEfiVlv2VariableGuid,
  135. NULL,
  136. &BufferSize,
  137. VariableData
  138. );
  139. if (EFI_ERROR(Status) && (MemHobData != NULL)) {
  140. Status = gRT->SetVariable (
  141. EfiMemoryConfigVariable,
  142. &gEfiVlv2VariableGuid,
  143. (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS),
  144. BufferSize,
  145. MemHobData
  146. );
  147. }
  148. }
  149. } // if-else MfgMode
  150. return EFI_SUCCESS;
  151. }