OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
OpenCoreNvram.c
Go to the documentation of this file.
1
15#include <Library/OcMainLib.h>
16
17#include <Guid/OcVariable.h>
18
19#include <Library/BaseLib.h>
20#include <Library/DebugLib.h>
21#include <Library/MemoryAllocationLib.h>
23#include <Library/UefiLib.h>
24#include <Library/UefiRuntimeServicesTableLib.h>
25
26STATIC
27VOID
29 IN OC_GLOBAL_CONFIG *Config
30 )
31{
32 CONST CHAR8 *Version;
33
35
36 DEBUG ((DEBUG_INFO, "OC: Current version is %a\n", Version));
37
38 if ((Config->Misc.Security.ExposeSensitiveData & OCS_EXPOSE_VERSION_VAR) != 0) {
42 AsciiStrLen (Version),
43 (VOID *)Version,
44 NULL
45 );
46 } else {
50 L_STR_LEN ("UNK-000-0000-00-00"),
51 "UNK-000-0000-00-00",
52 NULL
53 );
54 }
55}
56
57STATIC
58VOID
60 IN OC_GLOBAL_CONFIG *Config
61 )
62{
63 EFI_STATUS Status;
64 UINT32 DeleteGuidIndex;
65 UINT32 AddGuidIndex;
66 UINT32 DeleteVariableIndex;
67 UINT32 AddVariableIndex;
68 CONST CHAR8 *AsciiVariableName;
69 CHAR16 *UnicodeVariableName;
70 GUID VariableGuid;
71 OC_ASSOC *VariableMap;
72 VOID *CurrentValue;
73 UINTN CurrentValueSize;
74 BOOLEAN SameContents;
75
76 for (DeleteGuidIndex = 0; DeleteGuidIndex < Config->Nvram.Delete.Count; ++DeleteGuidIndex) {
77 Status = OcProcessVariableGuid (
78 OC_BLOB_GET (Config->Nvram.Delete.Keys[DeleteGuidIndex]),
79 &VariableGuid,
80 NULL,
81 NULL
82 );
83
84 if (EFI_ERROR (Status)) {
85 continue;
86 }
87
88 //
89 // When variable is set and non-volatile variable setting is used,
90 // we do not want a variable to be constantly removed and added every reboot,
91 // as it will negatively impact flash memory. In case the variable is already set
92 // and has the same value we do not delete it.
93 //
94 for (AddGuidIndex = 0; AddGuidIndex < Config->Nvram.Add.Count; ++AddGuidIndex) {
95 if (AsciiStrCmp (
96 OC_BLOB_GET (Config->Nvram.Delete.Keys[DeleteGuidIndex]),
97 OC_BLOB_GET (Config->Nvram.Add.Keys[AddGuidIndex])
98 ) == 0)
99 {
100 break;
101 }
102 }
103
104 for (DeleteVariableIndex = 0; DeleteVariableIndex < Config->Nvram.Delete.Values[DeleteGuidIndex]->Count; ++DeleteVariableIndex) {
105 AsciiVariableName = OC_BLOB_GET (Config->Nvram.Delete.Values[DeleteGuidIndex]->Values[DeleteVariableIndex]);
106
107 //
108 // '#' is filtered in all keys, but for values we need to do it ourselves.
109 //
110 if (AsciiVariableName[0] == '#') {
111 DEBUG ((DEBUG_INFO, "OC: Variable skip deleting %a\n", AsciiVariableName));
112 continue;
113 }
114
115 UnicodeVariableName = AsciiStrCopyToUnicode (AsciiVariableName, 0);
116
117 if (UnicodeVariableName == NULL) {
118 DEBUG ((DEBUG_WARN, "OC: Failed to convert NVRAM variable name %a\n", AsciiVariableName));
119 continue;
120 }
121
122 if (AddGuidIndex != Config->Nvram.Add.Count) {
123 VariableMap = NULL;
124 for (AddVariableIndex = 0; AddVariableIndex < Config->Nvram.Add.Values[AddGuidIndex]->Count; ++AddVariableIndex) {
125 if (AsciiStrCmp (AsciiVariableName, OC_BLOB_GET (Config->Nvram.Add.Values[AddGuidIndex]->Keys[AddVariableIndex])) == 0) {
126 VariableMap = Config->Nvram.Add.Values[AddGuidIndex];
127 break;
128 }
129 }
130
131 if (VariableMap != NULL) {
132 Status = GetVariable2 (UnicodeVariableName, &VariableGuid, &CurrentValue, &CurrentValueSize);
133
134 if (!EFI_ERROR (Status)) {
135 SameContents = CurrentValueSize == VariableMap->Values[AddVariableIndex]->Size
136 && CompareMem (OC_BLOB_GET (VariableMap->Values[AddVariableIndex]), CurrentValue, CurrentValueSize) == 0;
137 FreePool (CurrentValue);
138 } else if ((Status == EFI_NOT_FOUND) && (VariableMap->Values[AddVariableIndex]->Size == 0)) {
139 SameContents = TRUE;
140 } else {
141 SameContents = FALSE;
142 }
143
144 if (SameContents) {
145 DEBUG ((DEBUG_INFO, "OC: Not deleting NVRAM %g:%a, matches add\n", &VariableGuid, AsciiVariableName));
146 FreePool (UnicodeVariableName);
147 continue;
148 }
149 }
150 }
151
152 Status = gRT->SetVariable (UnicodeVariableName, &VariableGuid, 0, 0, 0);
153 DEBUG ((
154 EFI_ERROR (Status) && Status != EFI_NOT_FOUND ? DEBUG_WARN : DEBUG_INFO,
155 "OC: Deleting NVRAM %g:%a - %r\n",
156 &VariableGuid,
157 AsciiVariableName,
158 Status
159 ));
160
161 FreePool (UnicodeVariableName);
162 }
163 }
164}
165
166STATIC
167VOID
169 IN OC_GLOBAL_CONFIG *Config
170 )
171{
172 EFI_STATUS Status;
173 UINT32 GuidIndex;
174 UINT32 VariableIndex;
175 GUID VariableGuid;
176 OC_ASSOC *VariableMap;
177
178 for (GuidIndex = 0; GuidIndex < Config->Nvram.Add.Count; ++GuidIndex) {
179 Status = OcProcessVariableGuid (
180 OC_BLOB_GET (Config->Nvram.Add.Keys[GuidIndex]),
181 &VariableGuid,
182 NULL,
183 NULL
184 );
185
186 if (EFI_ERROR (Status)) {
187 continue;
188 }
189
190 VariableMap = Config->Nvram.Add.Values[GuidIndex];
191
192 for (VariableIndex = 0; VariableIndex < VariableMap->Count; ++VariableIndex) {
194 OC_BLOB_GET (VariableMap->Keys[VariableIndex]),
195 &VariableGuid,
196 Config->Nvram.WriteFlash ? OPEN_CORE_NVRAM_NV_ATTR : OPEN_CORE_NVRAM_ATTR,
197 VariableMap->Values[VariableIndex]->Size,
198 OC_BLOB_GET (VariableMap->Values[VariableIndex]),
199 NULL,
200 FALSE
201 );
202 }
203 }
204}
205
206VOID
208 IN OC_STORAGE_CONTEXT *Storage,
209 IN OC_GLOBAL_CONFIG *Config
210 )
211{
213 Storage,
214 &Config->Nvram.Legacy,
215 Config->Nvram.LegacyOverwrite,
216 Config->Uefi.Quirks.RequestBootVarRouting
217 );
218
219 OcDeleteNvram (Config);
220
221 OcAddNvram (Config);
222
223 OcReportVersion (Config);
224}
UINT32 Version
#define OCS_EXPOSE_VERSION_VAR
CONST CHAR8 * OcMiscGetVersionString(VOID)
#define L_STR_LEN(String)
Definition OcStringLib.h:26
CHAR16 * AsciiStrCopyToUnicode(IN CONST CHAR8 *String, IN UINTN Length)
Definition OcAsciiLib.c:119
#define OC_BLOB_GET(Blob)
#define OC_VERSION_VARIABLE_NAME
Definition OcVariable.h:55
EFI_STATUS OcSetSystemVariable(IN CHAR16 *VariableName, IN UINT32 Attributes, IN UINTN DataSize, IN VOID *Data, IN EFI_GUID *VendorGuid OPTIONAL)
#define OPEN_CORE_NVRAM_ATTR
VOID OcSetNvramVariable(IN CONST CHAR8 *AsciiVariableName, IN EFI_GUID *VariableGuid, IN UINT32 Attributes, IN UINT32 VariableSize, IN VOID *VariableData, IN OC_NVRAM_LEGACY_ENTRY *SchemaEntry OPTIONAL, IN BOOLEAN Overwrite)
EFI_STATUS OcProcessVariableGuid(IN CONST CHAR8 *AsciiVariableGuid, OUT GUID *VariableGuid, IN OC_NVRAM_LEGACY_MAP *Schema OPTIONAL, OUT OC_NVRAM_LEGACY_ENTRY **SchemaEntry OPTIONAL)
#define OPEN_CORE_NVRAM_NV_ATTR
VOID OcLoadLegacyNvram(IN OC_STORAGE_CONTEXT *Storage, IN OC_NVRAM_LEGACY_MAP *LegacyMap, IN BOOLEAN LegacyOverwrite, IN BOOLEAN RequestBootVarRouting)
STATIC VOID OcReportVersion(IN OC_GLOBAL_CONFIG *Config)
VOID OcLoadNvramSupport(IN OC_STORAGE_CONTEXT *Storage, IN OC_GLOBAL_CONFIG *Config)
STATIC VOID OcDeleteNvram(IN OC_GLOBAL_CONFIG *Config)
STATIC VOID OcAddNvram(IN OC_GLOBAL_CONFIG *Config)
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
EFI_RUNTIME_SERVICES * gRT