summaryrefslogtreecommitdiff
path: root/Platform/BroxtonPlatformPkg/Common/SaveMemoryConfigDxe/SaveMemoryConfig.c
blob: 01a73bda84f489e7906a570947fd33cc0c2e3aac (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/** @file
  This is the driver that locates the MemoryConfigurationData HOB, if it
  exists, and saves the data to NVRAM.

  Copyright (c) 1999 - 2016, Intel Corporation. All rights reserved.<BR>

  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php.

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <PlatformBootMode.h>
#include <Protocol/SetupMode.h>
#include <Protocol/MemInfo.h>
#include <Guid/PlatformInfo.h>
#include <Guid/HobList.h>
#include <Guid/MemoryConfigData.h>
#include <Guid/GlobalVariable.h>
#include <Guid/BxtVariable.h>
#include <Library/HobLib.h>
#include <Library/DebugLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BaseLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/UefiLib.h>
#include <Library/MemoryAllocationLib.h>

#define MRC_DATA_REQUIRED_FROM_OUTSIDE
#include "MmrcData.h"

#define EFI_UNLOAD_IMAGE   EFIERR(29)

GLOBAL_REMOVE_IF_UNREFERENCED CHAR16  mMemoryConfigVariable[] = L"MemoryConfig";
GLOBAL_REMOVE_IF_UNREFERENCED CHAR16  mMemoryBootVariable[]   = L"MemoryBootData";

VOID
PrintBinaryBuffer (
  IN        UINT8*      Buffer,
  IN        UINTN       BufferSize
)
{
  UINTN    CurrentByte = 0;

  if (BufferSize == 0) {
    DEBUG ((EFI_D_INFO, "Skipping print of 0 size buffer\n"));
    return;
  }
  DEBUG ((EFI_D_INFO, "Base  0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F\n"));
  DEBUG ((EFI_D_INFO, "%4d %2x ", CurrentByte/16, Buffer[0]));
  for (CurrentByte = 1; CurrentByte < BufferSize; CurrentByte++) {
    if ((CurrentByte % 16) == 0) {
      DEBUG ((EFI_D_INFO, "\n%4d %2x ", CurrentByte/16, Buffer[CurrentByte]));
    } else {
      DEBUG ((EFI_D_INFO, "%2x ", Buffer[CurrentByte]));
    }
  }
  DEBUG ((EFI_D_INFO, "\n"));

  return;
}


/**
  This function takes in an gEfiMemoryConfigVariableGuid Variable name and a pointer
  to the data that will be saved. The data is first compared against the currently
  stored variable for differences. If no difference is found, the save will be skipped.

  @param[in]  VariableName       The variable name to save using the Variable Service.
  @param[in]  Buffer             Pointer to the data to save
  @param[in]  BufferSize         The size of the data to save

  @retval     EFI_SUCCESS        If the data is successfully saved or there was no data
  @retval     EFI_UNLOAD_IMAGE   It is not success

**/
EFI_STATUS
SaveMrcData(
  IN        CHAR16      VariableName[],
  IN        UINT8*      Buffer,
  IN        UINTN       BufferSize
)
{
  EFI_STATUS               Status;
  VOID                     *CurrentVariableData = NULL;
  UINTN                    CurrentVariableSize  = 0;
  BOOLEAN                  SaveConfig           = FALSE;

  CurrentVariableSize = 1;

  CurrentVariableData = AllocatePool (CurrentVariableSize);

  DEBUG ((EFI_D_INFO, "SaveMrcData %s\n", VariableName));

  if (CurrentVariableData == NULL) {
    ASSERT (CurrentVariableData != NULL);
    return EFI_UNLOAD_IMAGE;
  }

  Status = gRT->GetVariable (
                  VariableName,
                  &gEfiMemoryConfigVariableGuid,
                  NULL,
                  &CurrentVariableSize,
                  CurrentVariableData
                  );

  if (Status == EFI_BUFFER_TOO_SMALL) {
    FreePool (CurrentVariableData);
    CurrentVariableData = AllocatePool (CurrentVariableSize);

    if (CurrentVariableData == NULL) {
        DEBUG ((EFI_D_ERROR, "AllocatePool failed\n"));
        return EFI_UNLOAD_IMAGE;
    }

    Status = gRT->GetVariable (
                    VariableName,
                    &gEfiMemoryConfigVariableGuid,
                    NULL,
                    &CurrentVariableSize,
                    CurrentVariableData
                    );
  }

  //
  // The Memory Configuration will not exist during first boot or if memory/MRC
  // firmware changes have been made.
  //
  if (EFI_ERROR (Status) || CompareMem (Buffer, CurrentVariableData, BufferSize)) {
    SaveConfig = TRUE;
    DEBUG ((EFI_D_INFO, "GetVariable Status: %r\n", Status));
    DEBUG ((EFI_D_INFO, "Saving %s\n", VariableName));
  } else {
    DEBUG ((EFI_D_INFO, "Skip %s\n", VariableName));
  }

  if (SaveConfig) {
    Status = gRT->SetVariable (
                    VariableName,
                    &gEfiMemoryConfigVariableGuid,
                    (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS),
                    BufferSize,
                    (UINT8 *) Buffer
                    );

    ASSERT_EFI_ERROR (Status);

    if (EFI_ERROR (Status)) {
      DEBUG ((EFI_D_ERROR, "Failed to write %s\n", VariableName));
      ASSERT_EFI_ERROR (Status);
    }
  }

  return EFI_SUCCESS;
}


/**
  This is the standard EFI driver point that detects whether there is a
  MemoryConfigurationData HOB and, if so, saves its data to nvRAM.

  @param[in]  ImageHandle        Handle for the image of this driver
  @param[in]  SystemTable        Pointer to the EFI System Table

  @retval     EFI_SUCCESS        If the data is successfully saved or there was no data
  @retval     EFI_NOT_FOUND      If the HOB list could not be located.
  @retval     EFI_UNLOAD_IMAGE   It is not success

**/
EFI_STATUS
EFIAPI
SaveMemoryConfigEntryPoint (
  IN EFI_HANDLE            ImageHandle,
  IN EFI_SYSTEM_TABLE      *SystemTable
  )
{
  EFI_STATUS               Status;
  EFI_HANDLE               Handle                       = NULL;
  EFI_HOB_GUID_TYPE        *GuidHob                     = NULL;
  EFI_PLATFORM_INFO_HOB    *PlatformInfoHob             = NULL;
  EFI_PLATFORM_SETUP_ID    *BootModeBuffer              = NULL;
  MEM_INFO_PROTOCOL        *MemInfoHobProtocol          = NULL;
  MRC_NV_DATA_FRAME        *MemoryConfigHobData         = NULL;
  UINTN                    MemoryConfigHobDataSize      = 0;
  UINT8                    Channel                      = 0;
  UINT8                    Slot                         = 0;

  //
  // Search for the Memory Configuration GUID HOB.  If it is not present, then
  // there's nothing we can do. It may not exist on the update path.
  //
  GuidHob = GetFirstGuidHob (&gEfiPlatformInfoGuid);

  if (GuidHob == NULL) {
    ASSERT (GuidHob != NULL);
    return EFI_NOT_FOUND;
  }

  PlatformInfoHob = GET_GUID_HOB_DATA (GuidHob);

  GuidHob = GetFirstGuidHob (&gEfiPlatformBootModeGuid);

  if (GuidHob == NULL) {
    ASSERT (GuidHob != NULL);
    return EFI_NOT_FOUND;
  }

  BootModeBuffer = GET_GUID_HOB_DATA (GuidHob);

  if (!CompareMem (&BootModeBuffer->SetupName, MANUFACTURE_SETUP_NAME, StrSize (MANUFACTURE_SETUP_NAME))) {
    //
    // Don't save Memory Configuration in Manufacturing Mode.
    // Clear memory configuration.
    //
    DEBUG ((EFI_D_INFO, "Invalidating the MRC SaveParam Data for MfgMode...\n"));

    //
    // This driver does not produce any protocol services, so always unload it.
    //
    return EFI_UNLOAD_IMAGE;
  }

  //
  // Search for the Memory Configuration GUID HOB.  If it is not present, then
  // there's nothing we can do. It may not exist on the update path.
  //
  if ((GuidHob = GetFirstGuidHob (&gFspNonVolatileStorageHobGuid)) != NULL) {
    MemoryConfigHobData = GET_GUID_HOB_DATA (GuidHob);
    MemoryConfigHobDataSize = GET_GUID_HOB_DATA_SIZE (GuidHob);
  }

  if (MemoryConfigHobData == NULL) {
    return EFI_NOT_FOUND;
  }

  //
  // Populate and install the MemInfoHobProtocol
  //
  MemInfoHobProtocol = (MEM_INFO_PROTOCOL*) AllocateZeroPool (sizeof (MEM_INFO_PROTOCOL));

  if (MemInfoHobProtocol != NULL) {
    MemInfoHobProtocol->MemInfoData.memSize = 0;

    for (Channel = 0; Channel < CH_NUM; Channel++) {
      for (Slot = 0; Slot < DIMM_NUM; Slot++) {
        MemInfoHobProtocol->MemInfoData.memSize += MemoryConfigHobData->MrcParamsSaveRestore.Channel[Channel].SlotMem[Slot];
        MemInfoHobProtocol->MemInfoData.dimmSize[Slot + (Channel * DIMM_NUM)] = MemoryConfigHobData->MrcParamsSaveRestore.Channel[Channel].SlotMem[Slot];
        MemInfoHobProtocol->MemInfoData.DimmPresent[Slot + (Channel * DIMM_NUM)] = MemoryConfigHobData->MrcParamsSaveRestore.Channel[Channel].DimmPresent[Slot];
        if (MemInfoHobProtocol->MemInfoData.DimmPresent[Slot + (Channel * DIMM_NUM)]) {
          MemInfoHobProtocol->MemInfoData.DimmsSpdData[Slot + (Channel * DIMM_NUM)] = MemoryConfigHobData->MrcParamsSaveRestore.Channel[Channel].SpdData[Slot].Buffer;
        } else {
          MemInfoHobProtocol->MemInfoData.DimmsSpdData[Slot + (Channel * DIMM_NUM)] = NULL;
        }
      }
    }

    MemInfoHobProtocol->MemInfoData.ddrFreq   = MemoryConfigHobData->MrcParamsSaveRestore.CurrentFrequency;
    MemInfoHobProtocol->MemInfoData.memSize   = MemoryConfigHobData->MrcParamsSaveRestore.SystemMemorySize;
    MemInfoHobProtocol->MemInfoData.ddrType   = MemoryConfigHobData->MrcParamsSaveRestore.Channel[0].DramType;
    MemInfoHobProtocol->MemInfoData.BusWidth  = MemoryConfigHobData->MrcParamsSaveRestore.BusWidth;

    DEBUG ((EFI_D_INFO, "SaveMemoryConfigEntryPoint - Freq:0x%x\n", MemInfoHobProtocol->MemInfoData.ddrFreq));
    DEBUG ((EFI_D_INFO, "SaveMemoryConfigEntryPoint - Memsize:0x%x\n", MemInfoHobProtocol->MemInfoData.memSize));

    Status = gBS->InstallMultipleProtocolInterfaces (
                    &Handle,
                    &gMemInfoProtocolGuid,
                    MemInfoHobProtocol,
                    NULL
                    );
  }

  Status = SaveMrcData (mMemoryConfigVariable, (UINT8 *) &(MemoryConfigHobData->MrcParamsSaveRestore), sizeof (MRC_PARAMS_SAVE_RESTORE));
  if (EFI_ERROR(Status)) {
    return Status;
  }

  Status = SaveMrcData (mMemoryBootVariable, (UINT8 *) &(MemoryConfigHobData->BootVariableNvData), sizeof (BOOT_VARIABLE_NV_DATA));
  if (EFI_ERROR (Status)) {
    return Status;
  }

  //
  // This driver does not produce any protocol services, so always unload it.
  //
  return EFI_UNLOAD_IMAGE;
}