4 Copyright (c) 2004 - 2009, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "NvVarsFileLib.h"
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/UefiRuntimeServicesTableLib.h>
24 Writes the variable into the file so it can be restored from
25 the file on future boots of the system.
27 @param[in] File - The file to write to
28 @param[in] Name - Variable name string
29 @param[in] NameSize - Size of Name in bytes
30 @param[in] Guid - GUID of variable
31 @param[in] Attributes - Attributes of variable
32 @param[in] Data - Buffer containing Data for variable
33 @param[in] DataSize - Size of Data in bytes
35 @return EFI_STATUS based on the success or failure of the operation
39 PackVariableIntoFile (
40 IN EFI_FILE_HANDLE File
,
52 WriteSize
= sizeof (NameSize
);
53 Status
= FileHandleWrite (File
, &WriteSize
, &NameSize
);
54 if (EFI_ERROR (Status
)) {
59 Status
= FileHandleWrite (File
, &WriteSize
, (VOID
*) Name
);
60 if (EFI_ERROR (Status
)) {
64 WriteSize
= sizeof (*Guid
);
65 Status
= FileHandleWrite (File
, &WriteSize
, (VOID
*) Guid
);
66 if (EFI_ERROR (Status
)) {
70 WriteSize
= sizeof (Attributes
);
71 Status
= FileHandleWrite (File
, &WriteSize
, &Attributes
);
72 if (EFI_ERROR (Status
)) {
76 WriteSize
= sizeof (DataSize
);
77 Status
= FileHandleWrite (File
, &WriteSize
, &DataSize
);
78 if (EFI_ERROR (Status
)) {
83 Status
= FileHandleWrite (File
, &WriteSize
, Data
);
90 Unpacks the next variable from the NvVars file data
92 @param[in] Buffer - Buffer pointing to the next variable instance
93 On subsequent calls, the pointer should be incremented
94 by the returned SizeUsed value.
95 @param[in] MaxSize - Max allowable size for the variable data
96 On subsequent calls, this should be decremented
97 by the returned SizeUsed value.
98 @param[out] Name - Variable name string (address in Buffer)
99 @param[out] NameSize - Size of Name in bytes
100 @param[out] Guid - GUID of variable (address in Buffer)
101 @param[out] Attributes - Attributes of variable
102 @param[out] Data - Buffer containing Data for variable (address in Buffer)
103 @param[out] DataSize - Size of Data in bytes
104 @param[out] SizeUsed - Total size used for this variable instance in Buffer
106 @return EFI_STATUS based on the success or failure of the operation
110 UnpackVariableFromBuffer (
114 OUT UINT32
*NameSize
,
116 OUT UINT32
*Attributes
,
117 OUT UINT32
*DataSize
,
125 BytePtr
= (UINT8
*)Buffer
;
128 *NameSize
= *(UINT32
*) (BytePtr
+ Offset
);
129 Offset
= Offset
+ sizeof (UINT32
);
131 if (Offset
> MaxSize
) {
132 return EFI_INVALID_PARAMETER
;
135 *Name
= (CHAR16
*) (BytePtr
+ Offset
);
136 Offset
= Offset
+ *(UINT32
*)BytePtr
;
137 if (Offset
> MaxSize
) {
138 return EFI_INVALID_PARAMETER
;
141 *Guid
= (EFI_GUID
*) (BytePtr
+ Offset
);
142 Offset
= Offset
+ sizeof (EFI_GUID
);
143 if (Offset
> MaxSize
) {
144 return EFI_INVALID_PARAMETER
;
147 *Attributes
= *(UINT32
*) (BytePtr
+ Offset
);
148 Offset
= Offset
+ sizeof (UINT32
);
149 if (Offset
> MaxSize
) {
150 return EFI_INVALID_PARAMETER
;
153 *DataSize
= *(UINT32
*) (BytePtr
+ Offset
);
154 Offset
= Offset
+ sizeof (UINT32
);
155 if (Offset
> MaxSize
) {
156 return EFI_INVALID_PARAMETER
;
159 *Data
= (VOID
*) (BytePtr
+ Offset
);
160 Offset
= Offset
+ *DataSize
;
161 if (Offset
> MaxSize
) {
162 return EFI_INVALID_PARAMETER
;
172 Examines the NvVars file contents, and updates variables based on it.
174 @param[in] Buffer - Buffer with NvVars data
175 @param[in] MaxSize - Size of Buffer in bytes
176 @param[in] DryRun - If TRUE, then no variable modifications should be made
177 (If TRUE, the Buffer is still parsed for validity.)
179 @return EFI_STATUS based on the success or failure of the operation
183 UnpackVariablesFromBuffer (
197 UINT32 AlignedNameMaxSize
;
204 AlignedNameMaxSize
= 0;
207 Status
= EFI_SUCCESS
, Count
= 0, TotalSizeUsed
= 0;
208 !EFI_ERROR (Status
) && (TotalSizeUsed
< MaxSize
);
210 Status
= UnpackVariableFromBuffer (
211 (VOID
*) ((UINT8
*) Buffer
+ TotalSizeUsed
),
212 (MaxSize
- TotalSizeUsed
),
221 if (EFI_ERROR (Status
)) {
226 // We copy the name to a separately allocated buffer,
227 // to be sure it is 16-bit aligned.
229 if (NameSize
> AlignedNameMaxSize
) {
230 if (AlignedName
!= NULL
) {
231 FreePool (AlignedName
);
233 AlignedName
= AllocatePool (NameSize
);
235 if (AlignedName
== NULL
) {
236 return EFI_OUT_OF_RESOURCES
;
238 CopyMem (AlignedName
, Name
, NameSize
);
242 "Unpacked variable %g:%s\n",
247 TotalSizeUsed
= TotalSizeUsed
+ SizeUsed
;
251 "TotalSizeUsed(%d); MaxSize(%d)\n",
258 // Set the variable contents
272 "Restored variable %g:%s\n",
280 if (AlignedName
!= NULL
) {
281 FreePool (AlignedName
);
285 // Make sure the entire buffer was used, or else return an error
287 if (TotalSizeUsed
!= MaxSize
) {
290 "TotalSizeUsed(%d) != MaxSize(%d)\n",
294 return EFI_INVALID_PARAMETER
;
300 "Restored %d Variables\n",
310 Examines the NvVars file contents, and updates variables based on it.
312 @param[in] VarsBuffer - Buffer with NvVars data
313 @param[in] VarsBufferSize - Size of VarsBuffer in bytes
315 @return EFI_STATUS based on the success or failure of the operation
319 SetVariablesFromBuffer (
321 IN UINTN VarsBufferSize
327 // First test to make sure the entire buffer is in a good state
329 Status
= UnpackVariablesFromBuffer (VarsBuffer
, VarsBufferSize
, TRUE
);
330 if (EFI_ERROR (Status
)) {
331 DEBUG ((EFI_D_INFO
, "NvVars buffer format was invalid\n"));
336 // Now, actually restore the variables.
338 Status
= UnpackVariablesFromBuffer (VarsBuffer
, VarsBufferSize
, FALSE
);
339 if (EFI_ERROR (Status
)) {