summaryrefslogtreecommitdiff
path: root/OvmfPkg/Library/NvVarsFileLib/VarBuffer.c
blob: 4f32d27db8a78bd7843a14d959c38525a5edf786 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/** @file
  File System Access

  Copyright (c) 2004 - 2009, Intel Corporation. <BR>
  All rights reserved. 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 "NvVarsFileLib.h"

#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>


/**
  Writes the variable into the file so it can be restored from
  the file on future boots of the system.

  @param[in]  File - The file to write to
  @param[in]  Name - Variable name string
  @param[in]  NameSize - Size of Name in bytes
  @param[in]  Guid - GUID of variable
  @param[in]  Attributes - Attributes of variable
  @param[in]  Data - Buffer containing Data for variable
  @param[in]  DataSize - Size of Data in bytes

  @return     EFI_STATUS based on the success or failure of the operation

**/
EFI_STATUS
PackVariableIntoFile (
  IN EFI_FILE_HANDLE  File,
  IN CHAR16           *Name,
  IN UINT32           NameSize,
  IN EFI_GUID         *Guid,
  IN UINT32           Attributes,
  IN VOID             *Data,
  IN UINT32           DataSize
  )
{
  EFI_STATUS  Status;
  UINTN       WriteSize;

  WriteSize = sizeof (NameSize);
  Status = FileHandleWrite (File, &WriteSize, &NameSize);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  WriteSize = NameSize;
  Status = FileHandleWrite (File, &WriteSize, (VOID*) Name);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  WriteSize = sizeof (*Guid);
  Status = FileHandleWrite (File, &WriteSize, (VOID*) Guid);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  WriteSize = sizeof (Attributes);
  Status = FileHandleWrite (File, &WriteSize, &Attributes);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  WriteSize = sizeof (DataSize);
  Status = FileHandleWrite (File, &WriteSize, &DataSize);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  WriteSize = DataSize;
  Status = FileHandleWrite (File, &WriteSize, Data);

  return Status;
}


/**
  Unpacks the next variable from the NvVars file data

  @param[in]  Buffer - Buffer pointing to the next variable instance
                       On subsequent calls, the pointer should be incremented
                       by the returned SizeUsed value.
  @param[in]  MaxSize - Max allowable size for the variable data
                        On subsequent calls, this should be decremented
                        by the returned SizeUsed value.
  @param[out] Name - Variable name string (address in Buffer)
  @param[out] NameSize - Size of Name in bytes
  @param[out] Guid - GUID of variable (address in Buffer)
  @param[out] Attributes - Attributes of variable
  @param[out] Data - Buffer containing Data for variable (address in Buffer)
  @param[out] DataSize - Size of Data in bytes
  @param[out] SizeUsed - Total size used for this variable instance in Buffer

  @return     EFI_STATUS based on the success or failure of the operation

**/
EFI_STATUS
UnpackVariableFromBuffer (
  IN  VOID     *Buffer,
  IN  UINTN    MaxSize,
  OUT CHAR16   **Name,
  OUT UINT32   *NameSize,
  OUT EFI_GUID **Guid,
  OUT UINT32   *Attributes,
  OUT UINT32   *DataSize,
  OUT VOID     **Data,
  OUT UINTN    *SizeUsed
  )
{
  UINT8  *BytePtr;
  UINTN  Offset;

  BytePtr = (UINT8*)Buffer;
  Offset = 0;

  *NameSize = *(UINT32*) (BytePtr + Offset);
  Offset = Offset + sizeof (UINT32);

  if (Offset > MaxSize) {
    return EFI_INVALID_PARAMETER;
  }

  *Name = (CHAR16*) (BytePtr + Offset);
  Offset = Offset + *(UINT32*)BytePtr;
  if (Offset > MaxSize) {
    return EFI_INVALID_PARAMETER;
  }

  *Guid = (EFI_GUID*) (BytePtr + Offset);
  Offset = Offset + sizeof (EFI_GUID);
  if (Offset > MaxSize) {
    return EFI_INVALID_PARAMETER;
  }

  *Attributes = *(UINT32*) (BytePtr + Offset);
  Offset = Offset + sizeof (UINT32);
  if (Offset > MaxSize) {
    return EFI_INVALID_PARAMETER;
  }

  *DataSize = *(UINT32*) (BytePtr + Offset);
  Offset = Offset + sizeof (UINT32);
  if (Offset > MaxSize) {
    return EFI_INVALID_PARAMETER;
  }

  *Data = (VOID*) (BytePtr + Offset);
  Offset = Offset + *DataSize;
  if (Offset > MaxSize) {
    return EFI_INVALID_PARAMETER;
  }

  *SizeUsed = Offset;

  return EFI_SUCCESS;
}


/**
  Examines the NvVars file contents, and updates variables based on it.

  @param[in]  Buffer - Buffer with NvVars data
  @param[in]  MaxSize - Size of Buffer in bytes
  @param[in]  DryRun - If TRUE, then no variable modifications should be made
                       (If TRUE, the Buffer is still parsed for validity.)

  @return     EFI_STATUS based on the success or failure of the operation

**/
EFI_STATUS
UnpackVariablesFromBuffer (
  IN  VOID     *Buffer,
  IN  UINTN    MaxSize,
  IN  BOOLEAN  DryRun
  )
{
  EFI_STATUS  Status;
  UINTN       Count;
  UINTN       TotalSizeUsed;
  UINTN       SizeUsed;

  CHAR16      *Name;
  UINT32      NameSize;
  CHAR16      *AlignedName;
  UINT32      AlignedNameMaxSize;
  EFI_GUID    *Guid;
  UINT32      Attributes;
  UINT32      DataSize;
  VOID        *Data;

  AlignedName = NULL;
  AlignedNameMaxSize = 0;

  for (
    Status = EFI_SUCCESS, Count = 0, TotalSizeUsed = 0;
    !EFI_ERROR (Status) && (TotalSizeUsed < MaxSize);
    ) {
    Status = UnpackVariableFromBuffer (
               (VOID*) ((UINT8*) Buffer + TotalSizeUsed),
               (MaxSize - TotalSizeUsed),
               &Name,
               &NameSize,
               &Guid,
               &Attributes,
               &DataSize,
               &Data,
               &SizeUsed
               );
    if (EFI_ERROR (Status)) {
      return Status;
    }

    //
    // We copy the name to a separately allocated buffer,
    // to be sure it is 16-bit aligned.
    //
    if (NameSize > AlignedNameMaxSize) {
      if (AlignedName != NULL) {
        FreePool (AlignedName);
      }
      AlignedName = AllocatePool (NameSize);
    }
    if (AlignedName == NULL) {
      return EFI_OUT_OF_RESOURCES;
    }
    CopyMem (AlignedName, Name, NameSize);

    DEBUG ((
      EFI_D_INFO,
      "Unpacked variable %g:%s\n",
      Guid,
      AlignedName
      ));

    TotalSizeUsed = TotalSizeUsed + SizeUsed;

    DEBUG ((
      EFI_D_INFO,
      "TotalSizeUsed(%d); MaxSize(%d)\n",
      TotalSizeUsed,
      MaxSize
      ));

    if (!DryRun) {
      //
      // Set the variable contents
      //
      gRT->SetVariable (
             AlignedName,
             Guid,
             Attributes,
             DataSize,
             Data
             );

      Count++;

      DEBUG ((
        EFI_D_INFO,
        "Restored variable %g:%s\n",
        Guid,
        AlignedName
        ));
    }

  }

  if (AlignedName != NULL) {
    FreePool (AlignedName);
  }

  //
  // Make sure the entire buffer was used, or else return an error
  //
  if (TotalSizeUsed != MaxSize) {
    DEBUG ((
      EFI_D_INFO,
      "TotalSizeUsed(%d) != MaxSize(%d)\n",
      TotalSizeUsed,
      MaxSize
      ));
    return EFI_INVALID_PARAMETER;
  }

  if (Count > 0) {
    DEBUG ((
      EFI_D_INFO,
      "Restored %d Variables\n",
      Count
      ));
  }

  return EFI_SUCCESS;
}


/**
  Examines the NvVars file contents, and updates variables based on it.

  @param[in]  VarsBuffer - Buffer with NvVars data
  @param[in]  VarsBufferSize - Size of VarsBuffer in bytes

  @return     EFI_STATUS based on the success or failure of the operation

**/
EFI_STATUS
SetVariablesFromBuffer (
  IN VOID   *VarsBuffer,
  IN UINTN  VarsBufferSize
  )
{
  EFI_STATUS  Status;

  //
  // First test to make sure the entire buffer is in a good state
  //
  Status = UnpackVariablesFromBuffer (VarsBuffer, VarsBufferSize, TRUE);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_INFO, "NvVars buffer format was invalid\n"));
    return Status;
  }

  //
  // Now, actually restore the variables.
  //
  Status = UnpackVariablesFromBuffer (VarsBuffer, VarsBufferSize, FALSE);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  return Status;
}