summaryrefslogtreecommitdiff
path: root/Silicon/BroxtonSoC/BroxtonSiPkg/SampleCode/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c
blob: 00f9eb92d2a10c814f26d4d9be9c733fad9f90fe (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
/** @file
  Provides variable driver extended services.

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


/**
  Finds variable in storage blocks of volatile and non-volatile storage areas.

  This code finds variable in storage blocks of volatile and non-volatile storage areas.
  If VariableName is an empty string, then we just return the first
  qualified variable without comparing VariableName and VendorGuid.

  @param[in]  VariableName              Name of the variable to be found.
  @param[in]  VendorGuid                Variable vendor GUID to be found.
  @param[out] AuthVariableInfo          Pointer to AUTH_VARIABLE_INFO structure for
                                        output of the variable found.

  @retval     EFI_INVALID_PARAMETER     If VariableName is not an empty string,
                                        while VendorGuid is NULL.
  @retval     EFI_SUCCESS               Variable successfully found.
  @retval     EFI_NOT_FOUND             Variable not found.

**/
EFI_STATUS
EFIAPI
VariableExLibFindVariable (
  IN  CHAR16                *VariableName,
  IN  EFI_GUID              *VendorGuid,
  OUT AUTH_VARIABLE_INFO    *AuthVariableInfo
  )
{
  EFI_STATUS                    Status;
  VARIABLE_POINTER_TRACK        Variable;
  AUTHENTICATED_VARIABLE_HEADER *AuthVariable;

  Status = FindVariable (
             VariableName,
             VendorGuid,
             &Variable,
             &mVariableModuleGlobal->VariableGlobal,
             FALSE
             );
  if (EFI_ERROR (Status)) {
    AuthVariableInfo->Data = NULL;
    AuthVariableInfo->DataSize = 0;
    AuthVariableInfo->Attributes = 0;
    AuthVariableInfo->PubKeyIndex = 0;
    AuthVariableInfo->MonotonicCount = 0;
    AuthVariableInfo->TimeStamp = NULL;
    return Status;
  }

  AuthVariableInfo->DataSize        = DataSizeOfVariable (Variable.CurrPtr);
  AuthVariableInfo->Data            = GetVariableDataPtr (Variable.CurrPtr);
  AuthVariableInfo->Attributes      = Variable.CurrPtr->Attributes;
  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
    AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable.CurrPtr;
    AuthVariableInfo->PubKeyIndex     = AuthVariable->PubKeyIndex;
    AuthVariableInfo->MonotonicCount  = ReadUnaligned64 (&(AuthVariable->MonotonicCount));
    AuthVariableInfo->TimeStamp       = &AuthVariable->TimeStamp;
  }

  return EFI_SUCCESS;
}


/**
  Finds next variable in storage blocks of volatile and non-volatile storage areas.

  This code finds next variable in storage blocks of volatile and non-volatile storage areas.
  If VariableName is an empty string, then we just return the first
  qualified variable without comparing VariableName and VendorGuid.

  @param[in]  VariableName              Name of the variable to be found.
  @param[in]  VendorGuid                Variable vendor GUID to be found.
  @param[out] AuthVariableInfo          Pointer to AUTH_VARIABLE_INFO structure for
                                        output of the next variable.

  @retval     EFI_INVALID_PARAMETER     If VariableName is not an empty string,
                                        while VendorGuid is NULL.
  @retval     EFI_SUCCESS               Variable successfully found.
  @retval     EFI_NOT_FOUND             Variable not found

**/
EFI_STATUS
EFIAPI
VariableExLibFindNextVariable (
  IN  CHAR16                *VariableName,
  IN  EFI_GUID              *VendorGuid,
  OUT AUTH_VARIABLE_INFO    *AuthVariableInfo
  )
{
  CHAR16                        TempName[2048];
  EFI_GUID                      TempGuid;
  VARIABLE_POINTER_TRACK        Variable;
  EFI_STATUS                    Status;
  UINTN                         TempNameSize;
  VARIABLE_HEADER               *VariablePtr;
  AUTHENTICATED_VARIABLE_HEADER *AuthVariablePtr;

  ASSERT (StrLen (VariableName) < 2048);
  if (StrLen (VariableName) >= 2048) {
    return EFI_OUT_OF_RESOURCES;
  }
  TempNameSize = 2048 * sizeof (CHAR16);
  StrCpyS ((CHAR16 *) TempName, TempNameSize, VariableName);
  CopyMem (&TempGuid, VendorGuid, sizeof (EFI_GUID));
  Status = VariableServiceGetNextVariableInternal (
             &TempNameSize,
             &TempName[0],
             &TempGuid
             );
  if (EFI_ERROR (Status)) {
    ASSERT (Status != EFI_BUFFER_TOO_SMALL);
    AuthVariableInfo->VariableName = NULL;
    AuthVariableInfo->VendorGuid = NULL;
    AuthVariableInfo->Data = NULL;
    AuthVariableInfo->DataSize = 0;
    AuthVariableInfo->Attributes = 0;
    AuthVariableInfo->PubKeyIndex = 0;
    AuthVariableInfo->MonotonicCount = 0;
    AuthVariableInfo->TimeStamp = NULL;
    return Status;
  } else {
    Status = FindVariable (&TempName[0], &TempGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
    ASSERT_EFI_ERROR (Status);
    if (EFI_ERROR (Status)) {
      AuthVariableInfo->VariableName = NULL;
      AuthVariableInfo->VendorGuid = NULL;
      AuthVariableInfo->Data = NULL;
      AuthVariableInfo->DataSize = 0;
      AuthVariableInfo->Attributes = 0;
      AuthVariableInfo->PubKeyIndex = 0;
      AuthVariableInfo->MonotonicCount = 0;
      AuthVariableInfo->TimeStamp = NULL;
      return Status;
    }
    VariablePtr = Variable.CurrPtr;
  }

  AuthVariableInfo->VariableName    = GetVariableNamePtr (VariablePtr);
  AuthVariableInfo->VendorGuid      = GetVendorGuidPtr (VariablePtr);
  AuthVariableInfo->DataSize        = DataSizeOfVariable (VariablePtr);
  AuthVariableInfo->Data            = GetVariableDataPtr (VariablePtr);
  AuthVariableInfo->Attributes      = VariablePtr->Attributes;
  if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {
    AuthVariablePtr = (AUTHENTICATED_VARIABLE_HEADER *) VariablePtr;
    AuthVariableInfo->PubKeyIndex     = AuthVariablePtr->PubKeyIndex;
    AuthVariableInfo->MonotonicCount  = ReadUnaligned64 (&(AuthVariablePtr->MonotonicCount));
    AuthVariableInfo->TimeStamp       = &AuthVariablePtr->TimeStamp;
  }

  return EFI_SUCCESS;
}


/**
  Update the variable region with Variable information.

  @param[in] AuthVariableInfo          Pointer AUTH_VARIABLE_INFO structure for
                                       input of the variable.

  @retval    EFI_SUCCESS               The update operation is success.
  @retval    EFI_INVALID_PARAMETER     Invalid parameter.
  @retval    EFI_WRITE_PROTECTED       Variable is write-protected.
  @retval    EFI_OUT_OF_RESOURCES      There is not enough resource.

**/
EFI_STATUS
EFIAPI
VariableExLibUpdateVariable (
  IN AUTH_VARIABLE_INFO     *AuthVariableInfo
  )
{
  VARIABLE_POINTER_TRACK    Variable;

  FindVariable (AuthVariableInfo->VariableName, AuthVariableInfo->VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);
  return UpdateVariable (
           AuthVariableInfo->VariableName,
           AuthVariableInfo->VendorGuid,
           AuthVariableInfo->Data,
           AuthVariableInfo->DataSize,
           AuthVariableInfo->Attributes,
           AuthVariableInfo->PubKeyIndex,
           AuthVariableInfo->MonotonicCount,
           &Variable,
           AuthVariableInfo->TimeStamp
           );
}


/**
  Get scratch buffer.

  @param[in, out] ScratchBufferSize Scratch buffer size. If input size is greater than
                                    the maximum supported buffer size, this value contains
                                    the maximum supported buffer size as output.
  @param[out]     ScratchBuffer     Pointer to scratch buffer address.

  @retval         EFI_SUCCESS       Get scratch buffer successfully.
  @retval         EFI_UNSUPPORTED   If input size is greater than the maximum supported buffer size.

**/
EFI_STATUS
EFIAPI
VariableExLibGetScratchBuffer (
  IN OUT UINTN      *ScratchBufferSize,
  OUT    VOID       **ScratchBuffer
  )
{
  UINTN MaxBufferSize;

  MaxBufferSize = mVariableModuleGlobal->ScratchBufferSize;
  if (*ScratchBufferSize > MaxBufferSize) {
    *ScratchBufferSize = MaxBufferSize;
    return EFI_UNSUPPORTED;
  }

  *ScratchBuffer = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));

  return EFI_SUCCESS;
}


/**
  This function is to check if the remaining variable space is enough to set
  all Variables from argument list successfully. The purpose of the check
  is to keep the consistency of the Variables to be in variable storage.

  Note: Variables are assumed to be in same storage.
  The set sequence of Variables will be same with the sequence of VariableEntry from argument list,
  so follow the argument sequence to check the Variables.

  @param[in] Attributes         Variable attributes for Variable entries.
  @param ...                    The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.
                                A NULL terminates the list. The VariableSize of
                                VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.
                                It will be changed to variable total size as output.

  @retval    TRUE               Have enough variable space to set the Variables successfully.
  @retval    FALSE              No enough variable space to set the Variables successfully.

**/
BOOLEAN
EFIAPI
VariableExLibCheckRemainingSpaceForConsistency (
  IN UINT32                     Attributes,
  ...
  )
{
  VA_LIST Marker;
  BOOLEAN Return;

  VA_START (Marker, Attributes);

  Return = CheckRemainingSpaceForConsistencyInternal (Attributes, Marker);

  VA_END (Marker);

  return Return;
}

/**
  Return TRUE if at OS runtime.

  @retval TRUE If at OS runtime.
  @retval FALSE If at boot time.

**/
BOOLEAN
EFIAPI
VariableExLibAtRuntime (
  VOID
  )
{
  return AtRuntime ();
}