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