summaryrefslogtreecommitdiff
path: root/Silicon/BroxtonSoC/BroxtonSiPkg/Library/Private/DxeSmmCseVariableStorageSelectorLib/CseVariableStorageSelectorLib.c
blob: 09e6d6842341820b4364a39e3cb74495d62f5a1b (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
/** @file
  DXE SMM CSE Variable Storage Selector Library.

  Copyright (c) 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 "CseVariableStorageSelectorLibInternal.h"

EFI_HECI_PROTOCOL *mHeci2Protocol = NULL;

/**
  Returns the CSE NVM file used for the requested variable.

  @param[in]  VariableName         Name of the variable.
  @param[in]  VendorGuid           Guid of the variable.
  @param[in]  CseVariableFileInfo  An array of pointers to CSE
                                   variable file information.

  @return     The type of CSE NVM file used for this variable.

**/
CSE_VARIABLE_FILE_TYPE
EFIAPI
GetCseVariableStoreFileType (
  IN CONST CHAR16                 *VariableName,
  IN CONST EFI_GUID               *VendorGuid,
  IN CONST CSE_VARIABLE_FILE_INFO **CseVariableFileInfo
  )
{
  CSE_VARIABLE_FILE_TYPE  Type;

  if (VariableName[0] == 0) {
    //
    // Return the first available CSE file store
    //
    DEBUG ((EFI_D_INFO, "CseVariableStorageSelectorLib - Variable name is NULL\n"));
    for (Type = (CSE_VARIABLE_FILE_TYPE) 0; Type < CseVariableFileTypeMax; Type++) {
      if (CseVariableFileInfo[Type]->FileEnabled) {
        return Type;
      }
    }
    //
    // There should always be at least one CSE file store enabled
    //
    ASSERT (FALSE);
    return (CSE_VARIABLE_FILE_TYPE) 0;
  } else if ((StrCmp (VariableName, L"Setup") == 0) &&
    CompareGuid (VendorGuid, &gEfiSetupVariableGuid)) {
    //
    // Setup variable is stored in the MRC training data file
    // if it is enabled.
    //
    DEBUG ((EFI_D_INFO, "CseVariableStorageSelectorLib - Variable is Setup\n"));
    return (CseVariableFileInfo[CseVariableFileTypePreMemoryFile]->FileEnabled ?
                                                                   CseVariableFileTypePreMemoryFile :
                                                                   CseVariableFileTypePrimaryIndexFile
                                                                   );
  } else if ((StrCmp (VariableName, L"MemoryConfig") == 0 || StrCmp (VariableName, L"MemoryBootData") == 0) &&
    CompareGuid (VendorGuid, &gEfiMemoryConfigVariableGuid)) {
    //
    // Memory configuration data is preferred to be stored in
    // the MRC training data file.
    //
    DEBUG ((EFI_D_INFO, "CseVariableStorageSelectorLib - Variable is MemoryConfig\n"));
    return (CseVariableFileInfo[CseVariableFileTypePreMemoryFile]->FileEnabled ?
                                                                   CseVariableFileTypePreMemoryFile :
                                                                   CseVariableFileTypePrimaryIndexFile
                                                                   );
  } else {
    //
    // All other variables are stored in individual CSE files
    //
    DEBUG ((EFI_D_INFO, "CseVariableStorageSelectorLib - Variable is not Setup\n"));
    return CseVariableFileTypePrimaryIndexFile;
  }
}


/**
  Returns the base offset for variable services in the CSE NVM file
  specified for this file type.

  @param[in]  CseVariableFileType  The type of the CSE NVM file.

  @param[out] CseFileOffset        The offset to base a variable store
                                   in the CSE file for this variable.

  @retval     EFI_SUCCESS          Offset successfully returned for the variable.
  @retval     Others               An error occurred.

**/
EFI_STATUS
EFIAPI
GetCseVariableStoreFileOffset (
  IN  CSE_VARIABLE_FILE_TYPE  CseVariableFileType,
  OUT UINT32                  *CseFileOffset
  )
{
  if (CseFileOffset == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  switch (CseVariableFileType) {
    case CseVariableFileTypePreMemoryFile:
      *CseFileOffset = CSE_PRE_MEMORY_FILE_STORE_OFFSET;
      break;

    case CseVariableFileTypePrimaryIndexFile:
      *CseFileOffset = CSE_PRIMARY_NVM_FILE_STORE_OFFSET;
      break;

    default:
      return EFI_NOT_FOUND;
  }

  return EFI_SUCCESS;
}


/**
  Returns the HECI protocol used to access the CSE NVM file based on the current point in the boot.

  @param[out]  HeciProtocol              A pointer to the HECI2 protocol if it is active.
                                         If HECI1 is active, the value is NULL.

  @return    CSE_VARIABLE_HECI_PROTOCOL  The HECI protocol to currently use.

**/
CSE_VARIABLE_HECI_PROTOCOL
EFIAPI
GetCseHeciProtocol (
  OUT      EFI_HECI_PROTOCOL  **Heci2Protocol
  )
{
  *Heci2Protocol = mHeci2Protocol;

  return (*Heci2Protocol == NULL) ? CseVariableHeci1Protocol : CseVariableHeci2Protocol;
}


/**
  Returns the HECI protocol used to access the CSE NVM file based on a given varaible.

  @param[in]  VariableName               Name of the variable.
  @param[in]  VendorGuid                 Guid of the variable.

  @return    CSE_VARIABLE_HECI_PROTOCOL  The HECI protocol to currently use.

**/
CSE_VARIABLE_HECI_PROTOCOL
EFIAPI
GetCseVariableHeciProtocol (
  IN CONST CHAR16             *VariableName,
  IN CONST EFI_GUID           *VendorGuid
  )
{
  EFI_HECI_PROTOCOL           *HeciProtocol;
  CSE_VARIABLE_HECI_PROTOCOL  HeciProtocolSelector;

  //
  // Custom logic can be added here to choose an appropriate HECI protocol
  //

  HeciProtocolSelector = GetCseHeciProtocol (&HeciProtocol);

  return HeciProtocolSelector;
}