]>
Commit | Line | Data |
---|---|---|
052ad7e1 | 1 | /** @file\r |
fa0737a8 | 2 | The common variable operation routines shared by DXE_RUNTIME variable\r |
8a2d4996 | 3 | module and DXE_SMM variable module.\r |
fa0737a8 | 4 | \r |
18a7dbbc SZ |
5 | Caution: This module requires additional review when modified.\r |
6 | This driver will have external input - variable data. They may be input in SMM mode.\r | |
7 | This external input must be validated carefully to avoid security issue like\r | |
8 | buffer overflow, integer overflow.\r | |
9 | \r | |
10 | VariableServiceGetNextVariableName () and VariableServiceQueryVariableInfo() are external API.\r | |
11 | They need check input parameter.\r | |
12 | \r | |
13 | VariableServiceGetVariable() and VariableServiceSetVariable() are external API\r | |
14 | to receive datasize and data buffer. The size should be checked carefully.\r | |
15 | \r | |
fa0737a8 SZ |
16 | VariableServiceSetVariable() should also check authenticate data to avoid buffer overflow,\r |
17 | integer overflow. It should also check attribute to avoid authentication bypass.\r | |
18 | \r | |
809e2bbf | 19 | Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r |
9b18845a | 20 | (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r |
fa0737a8 SZ |
21 | This program and the accompanying materials\r |
22 | are licensed and made available under the terms and conditions of the BSD License\r | |
23 | which accompanies this distribution. The full text of the license may be found at\r | |
24 | http://opensource.org/licenses/bsd-license.php\r | |
504214c4 | 25 | \r |
fa0737a8 SZ |
26 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
27 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
8d3a5c82 | 28 | \r |
052ad7e1 | 29 | **/\r |
8d3a5c82 | 30 | \r |
8d3a5c82 | 31 | #include "Variable.h"\r |
33a5a666 | 32 | \r |
7800593d | 33 | VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;\r |
8d3a5c82 | 34 | \r |
7c80e839 | 35 | ///\r |
8a2d4996 | 36 | /// Define a memory cache that improves the search performance for a variable.\r |
7c80e839 | 37 | ///\r |
ff843847 | 38 | VARIABLE_STORE_HEADER *mNvVariableCache = NULL;\r |
aa79b0b3 | 39 | \r |
9b18845a DL |
40 | ///\r |
41 | /// Memory cache of Fv Header.\r | |
42 | ///\r | |
43 | EFI_FIRMWARE_VOLUME_HEADER *mNvFvHeaderCache = NULL;\r | |
44 | \r | |
8a2d4996 | 45 | ///\r |
46 | /// The memory entry used for variable statistics data.\r | |
47 | ///\r | |
ff843847 RN |
48 | VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r |
49 | \r | |
ff843847 RN |
50 | ///\r |
51 | /// The flag to indicate whether the platform has left the DXE phase of execution.\r | |
52 | ///\r | |
53 | BOOLEAN mEndOfDxe = FALSE;\r | |
54 | \r | |
55 | ///\r | |
8021f4c7 SZ |
56 | /// It indicates the var check request source.\r |
57 | /// In the implementation, DXE is regarded as untrusted, and SMM is trusted.\r | |
ff843847 | 58 | ///\r |
8021f4c7 | 59 | VAR_CHECK_REQUEST_SOURCE mRequestSource = VarCheckFromUntrusted;\r |
8d3a5c82 | 60 | \r |
00ab76e0 SZ |
61 | //\r |
62 | // It will record the current boot error flag before EndOfDxe.\r | |
63 | //\r | |
64 | VAR_ERROR_FLAG mCurrentBootVarErrFlag = VAR_ERROR_FLAG_NO_ERROR;\r | |
33a5a666 | 65 | \r |
fa0737a8 SZ |
66 | VARIABLE_ENTRY_PROPERTY mVariableEntryProperty[] = {\r |
67 | {\r | |
68 | &gEdkiiVarErrorFlagGuid,\r | |
69 | VAR_ERROR_FLAG_NAME,\r | |
70 | {\r | |
71 | VAR_CHECK_VARIABLE_PROPERTY_REVISION,\r | |
72 | VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,\r | |
73 | VARIABLE_ATTRIBUTE_NV_BS_RT,\r | |
74 | sizeof (VAR_ERROR_FLAG),\r | |
75 | sizeof (VAR_ERROR_FLAG)\r | |
76 | }\r | |
77 | },\r | |
78 | };\r | |
79 | \r | |
8021f4c7 | 80 | AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn = {\r |
fa0737a8 | 81 | AUTH_VAR_LIB_CONTEXT_IN_STRUCT_VERSION,\r |
dbd030bb SZ |
82 | //\r |
83 | // StructSize, TO BE FILLED\r | |
84 | //\r | |
85 | 0,\r | |
fa0737a8 SZ |
86 | //\r |
87 | // MaxAuthVariableSize, TO BE FILLED\r | |
88 | //\r | |
89 | 0,\r | |
90 | VariableExLibFindVariable,\r | |
91 | VariableExLibFindNextVariable,\r | |
92 | VariableExLibUpdateVariable,\r | |
93 | VariableExLibGetScratchBuffer,\r | |
94 | VariableExLibCheckRemainingSpaceForConsistency,\r | |
95 | VariableExLibAtRuntime,\r | |
96 | };\r | |
97 | \r | |
8021f4c7 | 98 | AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut;\r |
fa0737a8 | 99 | \r |
2f6aa774 JY |
100 | /**\r |
101 | Initialization for MOR Lock Control.\r | |
102 | \r | |
103 | @retval EFI_SUCEESS MorLock initialization success.\r | |
104 | @return Others Some error occurs.\r | |
105 | **/\r | |
106 | EFI_STATUS\r | |
107 | MorLockInit (\r | |
108 | VOID\r | |
109 | );\r | |
110 | \r | |
111 | /**\r | |
112 | This service is an MOR/MorLock checker handler for the SetVariable().\r | |
113 | \r | |
114 | @param VariableName the name of the vendor's variable, as a\r | |
115 | Null-Terminated Unicode String\r | |
116 | @param VendorGuid Unify identifier for vendor.\r | |
117 | @param Attributes Point to memory location to return the attributes of variable. If the point\r | |
118 | is NULL, the parameter would be ignored.\r | |
119 | @param DataSize The size in bytes of Data-Buffer.\r | |
120 | @param Data Point to the content of the variable.\r | |
121 | \r | |
122 | @retval EFI_SUCCESS The MOR/MorLock check pass, and Variable driver can store the variable data.\r | |
123 | @retval EFI_INVALID_PARAMETER The MOR/MorLock data or data size or attributes is not allowed for MOR variable.\r | |
124 | @retval EFI_ACCESS_DENIED The MOR/MorLock is locked.\r | |
125 | @retval EFI_ALREADY_STARTED The MorLock variable is handled inside this function.\r | |
126 | Variable driver can just return EFI_SUCCESS.\r | |
127 | **/\r | |
128 | EFI_STATUS\r | |
129 | SetVariableCheckHandlerMor (\r | |
130 | IN CHAR16 *VariableName,\r | |
131 | IN EFI_GUID *VendorGuid,\r | |
132 | IN UINT32 Attributes,\r | |
133 | IN UINTN DataSize,\r | |
134 | IN VOID *Data\r | |
135 | );\r | |
136 | \r | |
fa0737a8 SZ |
137 | /**\r |
138 | Routine used to track statistical information about variable usage.\r | |
052ad7e1 | 139 | The data is stored in the EFI system table so it can be accessed later.\r |
fa0737a8 | 140 | VariableInfo.efi can dump out the table. Only Boot Services variable\r |
052ad7e1 | 141 | accesses are tracked by this code. The PcdVariableCollectStatistics\r |
fa0737a8 | 142 | build flag controls if this feature is enabled.\r |
052ad7e1 | 143 | \r |
fa0737a8 | 144 | A read that hits in the cache will have Read and Cache true for\r |
052ad7e1 A |
145 | the transaction. Data is allocated by this routine, but never\r |
146 | freed.\r | |
147 | \r | |
8a2d4996 | 148 | @param[in] VariableName Name of the Variable to track.\r |
149 | @param[in] VendorGuid Guid of the Variable to track.\r | |
150 | @param[in] Volatile TRUE if volatile FALSE if non-volatile.\r | |
151 | @param[in] Read TRUE if GetVariable() was called.\r | |
152 | @param[in] Write TRUE if SetVariable() was called.\r | |
153 | @param[in] Delete TRUE if deleted via SetVariable().\r | |
052ad7e1 A |
154 | @param[in] Cache TRUE for a cache hit.\r |
155 | \r | |
156 | **/\r | |
33a5a666 A |
157 | VOID\r |
158 | UpdateVariableInfo (\r | |
159 | IN CHAR16 *VariableName,\r | |
160 | IN EFI_GUID *VendorGuid,\r | |
161 | IN BOOLEAN Volatile,\r | |
162 | IN BOOLEAN Read,\r | |
163 | IN BOOLEAN Write,\r | |
164 | IN BOOLEAN Delete,\r | |
165 | IN BOOLEAN Cache\r | |
166 | )\r | |
167 | {\r | |
168 | VARIABLE_INFO_ENTRY *Entry;\r | |
169 | \r | |
170 | if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r | |
171 | \r | |
8a2d4996 | 172 | if (AtRuntime ()) {\r |
173 | // Don't collect statistics at runtime.\r | |
33a5a666 A |
174 | return;\r |
175 | }\r | |
176 | \r | |
177 | if (gVariableInfo == NULL) {\r | |
052ad7e1 | 178 | //\r |
8a2d4996 | 179 | // On the first call allocate a entry and place a pointer to it in\r |
180 | // the EFI System Table.\r | |
052ad7e1 | 181 | //\r |
33a5a666 | 182 | gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r |
052ad7e1 A |
183 | ASSERT (gVariableInfo != NULL);\r |
184 | \r | |
33a5a666 | 185 | CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r |
6e1e5405 | 186 | gVariableInfo->Name = AllocateZeroPool (StrSize (VariableName));\r |
e6c4ef13 | 187 | ASSERT (gVariableInfo->Name != NULL);\r |
568a5119 | 188 | StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);\r |
33a5a666 | 189 | gVariableInfo->Volatile = Volatile;\r |
33a5a666 A |
190 | }\r |
191 | \r | |
fa0737a8 | 192 | \r |
33a5a666 A |
193 | for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r |
194 | if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r | |
195 | if (StrCmp (VariableName, Entry->Name) == 0) {\r | |
196 | if (Read) {\r | |
197 | Entry->ReadCount++;\r | |
198 | }\r | |
199 | if (Write) {\r | |
200 | Entry->WriteCount++;\r | |
201 | }\r | |
202 | if (Delete) {\r | |
203 | Entry->DeleteCount++;\r | |
204 | }\r | |
205 | if (Cache) {\r | |
206 | Entry->CacheCount++;\r | |
207 | }\r | |
208 | \r | |
209 | return;\r | |
210 | }\r | |
211 | }\r | |
212 | \r | |
213 | if (Entry->Next == NULL) {\r | |
052ad7e1 A |
214 | //\r |
215 | // If the entry is not in the table add it.\r | |
8a2d4996 | 216 | // Next iteration of the loop will fill in the data.\r |
052ad7e1 | 217 | //\r |
33a5a666 | 218 | Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r |
052ad7e1 | 219 | ASSERT (Entry->Next != NULL);\r |
33a5a666 A |
220 | \r |
221 | CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r | |
6e1e5405 | 222 | Entry->Next->Name = AllocateZeroPool (StrSize (VariableName));\r |
e6c4ef13 | 223 | ASSERT (Entry->Next->Name != NULL);\r |
568a5119 | 224 | StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16), VariableName);\r |
33a5a666 A |
225 | Entry->Next->Volatile = Volatile;\r |
226 | }\r | |
227 | \r | |
228 | }\r | |
229 | }\r | |
230 | }\r | |
231 | \r | |
232 | \r | |
7c80e839 | 233 | /**\r |
8d3a5c82 | 234 | \r |
235 | This code checks if variable header is valid or not.\r | |
236 | \r | |
6ebffb67 SZ |
237 | @param Variable Pointer to the Variable Header.\r |
238 | @param VariableStoreEnd Pointer to the Variable Store End.\r | |
8d3a5c82 | 239 | \r |
6ebffb67 SZ |
240 | @retval TRUE Variable header is valid.\r |
241 | @retval FALSE Variable header is not valid.\r | |
8d3a5c82 | 242 | \r |
7c80e839 | 243 | **/\r |
244 | BOOLEAN\r | |
245 | IsValidVariableHeader (\r | |
6ebffb67 SZ |
246 | IN VARIABLE_HEADER *Variable,\r |
247 | IN VARIABLE_HEADER *VariableStoreEnd\r | |
7c80e839 | 248 | )\r |
8d3a5c82 | 249 | {\r |
6ebffb67 SZ |
250 | if ((Variable == NULL) || (Variable >= VariableStoreEnd) || (Variable->StartId != VARIABLE_DATA)) {\r |
251 | //\r | |
252 | // Variable is NULL or has reached the end of variable store,\r | |
253 | // or the StartId is not correct.\r | |
254 | //\r | |
8d3a5c82 | 255 | return FALSE;\r |
256 | }\r | |
257 | \r | |
258 | return TRUE;\r | |
259 | }\r | |
260 | \r | |
052ad7e1 | 261 | \r |
7c80e839 | 262 | /**\r |
263 | \r | |
264 | This function writes data to the FWH at the correct LBA even if the LBAs\r | |
265 | are fragmented.\r | |
266 | \r | |
8a2d4996 | 267 | @param Global Pointer to VARAIBLE_GLOBAL structure.\r |
268 | @param Volatile Point out the Variable is Volatile or Non-Volatile.\r | |
269 | @param SetByIndex TRUE if target pointer is given as index.\r | |
270 | FALSE if target pointer is absolute.\r | |
271 | @param Fvb Pointer to the writable FVB protocol.\r | |
7c80e839 | 272 | @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER\r |
8a2d4996 | 273 | structure.\r |
274 | @param DataSize Size of data to be written.\r | |
275 | @param Buffer Pointer to the buffer from which data is written.\r | |
7c80e839 | 276 | \r |
8a2d4996 | 277 | @retval EFI_INVALID_PARAMETER Parameters not valid.\r |
278 | @retval EFI_SUCCESS Variable store successfully updated.\r | |
7c80e839 | 279 | \r |
280 | **/\r | |
8d3a5c82 | 281 | EFI_STATUS\r |
8d3a5c82 | 282 | UpdateVariableStore (\r |
8a9e0b72 | 283 | IN VARIABLE_GLOBAL *Global,\r |
284 | IN BOOLEAN Volatile,\r | |
285 | IN BOOLEAN SetByIndex,\r | |
286 | IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb,\r | |
287 | IN UINTN DataPtrIndex,\r | |
288 | IN UINT32 DataSize,\r | |
289 | IN UINT8 *Buffer\r | |
8d3a5c82 | 290 | )\r |
8d3a5c82 | 291 | {\r |
292 | EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r | |
293 | UINTN BlockIndex2;\r | |
294 | UINTN LinearOffset;\r | |
295 | UINTN CurrWriteSize;\r | |
296 | UINTN CurrWritePtr;\r | |
297 | UINT8 *CurrBuffer;\r | |
298 | EFI_LBA LbaNumber;\r | |
299 | UINTN Size;\r | |
300 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
301 | VARIABLE_STORE_HEADER *VolatileBase;\r | |
302 | EFI_PHYSICAL_ADDRESS FvVolHdr;\r | |
303 | EFI_PHYSICAL_ADDRESS DataPtr;\r | |
304 | EFI_STATUS Status;\r | |
305 | \r | |
306 | FwVolHeader = NULL;\r | |
307 | DataPtr = DataPtrIndex;\r | |
308 | \r | |
309 | //\r | |
8a2d4996 | 310 | // Check if the Data is Volatile.\r |
8d3a5c82 | 311 | //\r |
312 | if (!Volatile) {\r | |
fa0737a8 SZ |
313 | if (Fvb == NULL) {\r |
314 | return EFI_INVALID_PARAMETER;\r | |
315 | }\r | |
8a9e0b72 | 316 | Status = Fvb->GetPhysicalAddress(Fvb, &FvVolHdr);\r |
317 | ASSERT_EFI_ERROR (Status);\r | |
318 | \r | |
8d3a5c82 | 319 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r |
320 | //\r | |
321 | // Data Pointer should point to the actual Address where data is to be\r | |
8a2d4996 | 322 | // written.\r |
8d3a5c82 | 323 | //\r |
324 | if (SetByIndex) {\r | |
052ad7e1 | 325 | DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r |
8d3a5c82 | 326 | }\r |
327 | \r | |
328 | if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {\r | |
329 | return EFI_INVALID_PARAMETER;\r | |
330 | }\r | |
331 | } else {\r | |
332 | //\r | |
333 | // Data Pointer should point to the actual Address where data is to be\r | |
8a2d4996 | 334 | // written.\r |
8d3a5c82 | 335 | //\r |
052ad7e1 | 336 | VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r |
8d3a5c82 | 337 | if (SetByIndex) {\r |
052ad7e1 | 338 | DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r |
8d3a5c82 | 339 | }\r |
340 | \r | |
341 | if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r | |
342 | return EFI_INVALID_PARAMETER;\r | |
343 | }\r | |
fa0737a8 | 344 | \r |
c6492839 | 345 | //\r |
346 | // If Volatile Variable just do a simple mem copy.\r | |
fa0737a8 | 347 | //\r |
c6492839 | 348 | CopyMem ((UINT8 *)(UINTN)DataPtr, Buffer, DataSize);\r |
8d3a5c82 | 349 | return EFI_SUCCESS;\r |
350 | }\r | |
fa0737a8 | 351 | \r |
8d3a5c82 | 352 | //\r |
8a2d4996 | 353 | // If we are here we are dealing with Non-Volatile Variables.\r |
8d3a5c82 | 354 | //\r |
355 | LinearOffset = (UINTN) FwVolHeader;\r | |
356 | CurrWritePtr = (UINTN) DataPtr;\r | |
357 | CurrWriteSize = DataSize;\r | |
358 | CurrBuffer = Buffer;\r | |
359 | LbaNumber = 0;\r | |
360 | \r | |
361 | if (CurrWritePtr < LinearOffset) {\r | |
362 | return EFI_INVALID_PARAMETER;\r | |
363 | }\r | |
364 | \r | |
9b18845a | 365 | for (PtrBlockMapEntry = mNvFvHeaderCache->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r |
8d3a5c82 | 366 | for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {\r |
367 | //\r | |
368 | // Check to see if the Variable Writes are spanning through multiple\r | |
369 | // blocks.\r | |
370 | //\r | |
371 | if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {\r | |
372 | if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {\r | |
8a9e0b72 | 373 | Status = Fvb->Write (\r |
374 | Fvb,\r | |
8d3a5c82 | 375 | LbaNumber,\r |
376 | (UINTN) (CurrWritePtr - LinearOffset),\r | |
377 | &CurrWriteSize,\r | |
378 | CurrBuffer\r | |
379 | );\r | |
8a9e0b72 | 380 | return Status;\r |
8d3a5c82 | 381 | } else {\r |
382 | Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r | |
8a9e0b72 | 383 | Status = Fvb->Write (\r |
384 | Fvb,\r | |
8d3a5c82 | 385 | LbaNumber,\r |
386 | (UINTN) (CurrWritePtr - LinearOffset),\r | |
387 | &Size,\r | |
388 | CurrBuffer\r | |
389 | );\r | |
390 | if (EFI_ERROR (Status)) {\r | |
391 | return Status;\r | |
392 | }\r | |
393 | \r | |
394 | CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;\r | |
395 | CurrBuffer = CurrBuffer + Size;\r | |
396 | CurrWriteSize = CurrWriteSize - Size;\r | |
397 | }\r | |
398 | }\r | |
399 | \r | |
400 | LinearOffset += PtrBlockMapEntry->Length;\r | |
401 | LbaNumber++;\r | |
402 | }\r | |
403 | }\r | |
404 | \r | |
405 | return EFI_SUCCESS;\r | |
406 | }\r | |
407 | \r | |
052ad7e1 | 408 | \r |
7c80e839 | 409 | /**\r |
8d3a5c82 | 410 | \r |
411 | This code gets the current status of Variable Store.\r | |
412 | \r | |
7c80e839 | 413 | @param VarStoreHeader Pointer to the Variable Store Header.\r |
8d3a5c82 | 414 | \r |
8a2d4996 | 415 | @retval EfiRaw Variable store status is raw.\r |
416 | @retval EfiValid Variable store status is valid.\r | |
417 | @retval EfiInvalid Variable store status is invalid.\r | |
8d3a5c82 | 418 | \r |
7c80e839 | 419 | **/\r |
420 | VARIABLE_STORE_STATUS\r | |
421 | GetVariableStoreStatus (\r | |
422 | IN VARIABLE_STORE_HEADER *VarStoreHeader\r | |
423 | )\r | |
8d3a5c82 | 424 | {\r |
fa0737a8 SZ |
425 | if ((CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) ||\r |
426 | CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) &&\r | |
8d3a5c82 | 427 | VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r |
428 | VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r | |
429 | ) {\r | |
430 | \r | |
431 | return EfiValid;\r | |
3709c4cd | 432 | } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&\r |
433 | ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&\r | |
434 | ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&\r | |
435 | ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&\r | |
436 | VarStoreHeader->Size == 0xffffffff &&\r | |
437 | VarStoreHeader->Format == 0xff &&\r | |
438 | VarStoreHeader->State == 0xff\r | |
8d3a5c82 | 439 | ) {\r |
440 | \r | |
441 | return EfiRaw;\r | |
442 | } else {\r | |
443 | return EfiInvalid;\r | |
444 | }\r | |
445 | }\r | |
446 | \r | |
fa0737a8 SZ |
447 | /**\r |
448 | This code gets the size of variable header.\r | |
449 | \r | |
450 | @return Size of variable header in bytes in type UINTN.\r | |
451 | \r | |
452 | **/\r | |
453 | UINTN\r | |
454 | GetVariableHeaderSize (\r | |
455 | VOID\r | |
456 | )\r | |
457 | {\r | |
458 | UINTN Value;\r | |
459 | \r | |
460 | if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r | |
461 | Value = sizeof (AUTHENTICATED_VARIABLE_HEADER);\r | |
462 | } else {\r | |
463 | Value = sizeof (VARIABLE_HEADER);\r | |
464 | }\r | |
465 | \r | |
466 | return Value;\r | |
467 | }\r | |
130e2569 | 468 | \r |
7c80e839 | 469 | /**\r |
130e2569 | 470 | \r |
471 | This code gets the size of name of variable.\r | |
472 | \r | |
8a2d4996 | 473 | @param Variable Pointer to the Variable Header.\r |
130e2569 | 474 | \r |
8a2d4996 | 475 | @return UINTN Size of variable in bytes.\r |
130e2569 | 476 | \r |
7c80e839 | 477 | **/\r |
478 | UINTN\r | |
479 | NameSizeOfVariable (\r | |
480 | IN VARIABLE_HEADER *Variable\r | |
481 | )\r | |
130e2569 | 482 | {\r |
fa0737a8 SZ |
483 | AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r |
484 | \r | |
485 | AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r | |
486 | if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r | |
487 | if (AuthVariable->State == (UINT8) (-1) ||\r | |
488 | AuthVariable->DataSize == (UINT32) (-1) ||\r | |
489 | AuthVariable->NameSize == (UINT32) (-1) ||\r | |
490 | AuthVariable->Attributes == (UINT32) (-1)) {\r | |
491 | return 0;\r | |
492 | }\r | |
493 | return (UINTN) AuthVariable->NameSize;\r | |
494 | } else {\r | |
495 | if (Variable->State == (UINT8) (-1) ||\r | |
496 | Variable->DataSize == (UINT32) (-1) ||\r | |
497 | Variable->NameSize == (UINT32) (-1) ||\r | |
498 | Variable->Attributes == (UINT32) (-1)) {\r | |
499 | return 0;\r | |
500 | }\r | |
501 | return (UINTN) Variable->NameSize;\r | |
502 | }\r | |
503 | }\r | |
504 | \r | |
505 | /**\r | |
506 | This code sets the size of name of variable.\r | |
507 | \r | |
508 | @param[in] Variable Pointer to the Variable Header.\r | |
509 | @param[in] NameSize Name size to set.\r | |
510 | \r | |
511 | **/\r | |
512 | VOID\r | |
513 | SetNameSizeOfVariable (\r | |
514 | IN VARIABLE_HEADER *Variable,\r | |
515 | IN UINTN NameSize\r | |
516 | )\r | |
517 | {\r | |
518 | AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r | |
519 | \r | |
520 | AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r | |
521 | if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r | |
522 | AuthVariable->NameSize = (UINT32) NameSize;\r | |
523 | } else {\r | |
524 | Variable->NameSize = (UINT32) NameSize;\r | |
130e2569 | 525 | }\r |
130e2569 | 526 | }\r |
527 | \r | |
7c80e839 | 528 | /**\r |
130e2569 | 529 | \r |
7c80e839 | 530 | This code gets the size of variable data.\r |
130e2569 | 531 | \r |
8a2d4996 | 532 | @param Variable Pointer to the Variable Header.\r |
130e2569 | 533 | \r |
8a2d4996 | 534 | @return Size of variable in bytes.\r |
130e2569 | 535 | \r |
7c80e839 | 536 | **/\r |
537 | UINTN\r | |
538 | DataSizeOfVariable (\r | |
539 | IN VARIABLE_HEADER *Variable\r | |
540 | )\r | |
130e2569 | 541 | {\r |
fa0737a8 SZ |
542 | AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r |
543 | \r | |
544 | AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r | |
545 | if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r | |
546 | if (AuthVariable->State == (UINT8) (-1) ||\r | |
547 | AuthVariable->DataSize == (UINT32) (-1) ||\r | |
548 | AuthVariable->NameSize == (UINT32) (-1) ||\r | |
549 | AuthVariable->Attributes == (UINT32) (-1)) {\r | |
550 | return 0;\r | |
551 | }\r | |
552 | return (UINTN) AuthVariable->DataSize;\r | |
553 | } else {\r | |
554 | if (Variable->State == (UINT8) (-1) ||\r | |
555 | Variable->DataSize == (UINT32) (-1) ||\r | |
556 | Variable->NameSize == (UINT32) (-1) ||\r | |
557 | Variable->Attributes == (UINT32) (-1)) {\r | |
558 | return 0;\r | |
559 | }\r | |
560 | return (UINTN) Variable->DataSize;\r | |
561 | }\r | |
562 | }\r | |
563 | \r | |
564 | /**\r | |
565 | This code sets the size of variable data.\r | |
566 | \r | |
567 | @param[in] Variable Pointer to the Variable Header.\r | |
568 | @param[in] DataSize Data size to set.\r | |
569 | \r | |
570 | **/\r | |
571 | VOID\r | |
572 | SetDataSizeOfVariable (\r | |
573 | IN VARIABLE_HEADER *Variable,\r | |
574 | IN UINTN DataSize\r | |
575 | )\r | |
576 | {\r | |
577 | AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r | |
578 | \r | |
579 | AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r | |
580 | if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r | |
581 | AuthVariable->DataSize = (UINT32) DataSize;\r | |
582 | } else {\r | |
583 | Variable->DataSize = (UINT32) DataSize;\r | |
130e2569 | 584 | }\r |
130e2569 | 585 | }\r |
586 | \r | |
7c80e839 | 587 | /**\r |
130e2569 | 588 | \r |
589 | This code gets the pointer to the variable name.\r | |
590 | \r | |
8a2d4996 | 591 | @param Variable Pointer to the Variable Header.\r |
130e2569 | 592 | \r |
8a2d4996 | 593 | @return Pointer to Variable Name which is Unicode encoding.\r |
130e2569 | 594 | \r |
7c80e839 | 595 | **/\r |
596 | CHAR16 *\r | |
597 | GetVariableNamePtr (\r | |
598 | IN VARIABLE_HEADER *Variable\r | |
599 | )\r | |
130e2569 | 600 | {\r |
fa0737a8 SZ |
601 | return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ());\r |
602 | }\r | |
130e2569 | 603 | \r |
fa0737a8 SZ |
604 | /**\r |
605 | This code gets the pointer to the variable guid.\r | |
606 | \r | |
607 | @param Variable Pointer to the Variable Header.\r | |
608 | \r | |
609 | @return A EFI_GUID* pointer to Vendor Guid.\r | |
610 | \r | |
611 | **/\r | |
612 | EFI_GUID *\r | |
613 | GetVendorGuidPtr (\r | |
614 | IN VARIABLE_HEADER *Variable\r | |
615 | )\r | |
616 | {\r | |
617 | AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r | |
618 | \r | |
619 | AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) Variable;\r | |
620 | if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r | |
621 | return &AuthVariable->VendorGuid;\r | |
622 | } else {\r | |
623 | return &Variable->VendorGuid;\r | |
624 | }\r | |
130e2569 | 625 | }\r |
626 | \r | |
7c80e839 | 627 | /**\r |
8d3a5c82 | 628 | \r |
629 | This code gets the pointer to the variable data.\r | |
630 | \r | |
8a2d4996 | 631 | @param Variable Pointer to the Variable Header.\r |
8d3a5c82 | 632 | \r |
8a2d4996 | 633 | @return Pointer to Variable Data.\r |
8d3a5c82 | 634 | \r |
7c80e839 | 635 | **/\r |
636 | UINT8 *\r | |
637 | GetVariableDataPtr (\r | |
638 | IN VARIABLE_HEADER *Variable\r | |
639 | )\r | |
8d3a5c82 | 640 | {\r |
130e2569 | 641 | UINTN Value;\r |
fa0737a8 | 642 | \r |
8d3a5c82 | 643 | //\r |
8a2d4996 | 644 | // Be careful about pad size for alignment.\r |
8d3a5c82 | 645 | //\r |
130e2569 | 646 | Value = (UINTN) GetVariableNamePtr (Variable);\r |
647 | Value += NameSizeOfVariable (Variable);\r | |
648 | Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r | |
649 | \r | |
650 | return (UINT8 *) Value;\r | |
8d3a5c82 | 651 | }\r |
652 | \r | |
fa0737a8 SZ |
653 | /**\r |
654 | This code gets the variable data offset related to variable header.\r | |
655 | \r | |
656 | @param Variable Pointer to the Variable Header.\r | |
657 | \r | |
658 | @return Variable Data offset.\r | |
659 | \r | |
660 | **/\r | |
661 | UINTN\r | |
662 | GetVariableDataOffset (\r | |
663 | IN VARIABLE_HEADER *Variable\r | |
664 | )\r | |
665 | {\r | |
666 | UINTN Value;\r | |
667 | \r | |
668 | //\r | |
669 | // Be careful about pad size for alignment\r | |
670 | //\r | |
671 | Value = GetVariableHeaderSize ();\r | |
672 | Value += NameSizeOfVariable (Variable);\r | |
673 | Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r | |
674 | \r | |
675 | return Value;\r | |
676 | }\r | |
052ad7e1 | 677 | \r |
7c80e839 | 678 | /**\r |
8d3a5c82 | 679 | \r |
680 | This code gets the pointer to the next variable header.\r | |
681 | \r | |
8a2d4996 | 682 | @param Variable Pointer to the Variable Header.\r |
8d3a5c82 | 683 | \r |
8a2d4996 | 684 | @return Pointer to next variable header.\r |
8d3a5c82 | 685 | \r |
7c80e839 | 686 | **/\r |
687 | VARIABLE_HEADER *\r | |
688 | GetNextVariablePtr (\r | |
689 | IN VARIABLE_HEADER *Variable\r | |
690 | )\r | |
8d3a5c82 | 691 | {\r |
130e2569 | 692 | UINTN Value;\r |
693 | \r | |
130e2569 | 694 | Value = (UINTN) GetVariableDataPtr (Variable);\r |
695 | Value += DataSizeOfVariable (Variable);\r | |
696 | Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));\r | |
697 | \r | |
8d3a5c82 | 698 | //\r |
8a2d4996 | 699 | // Be careful about pad size for alignment.\r |
8d3a5c82 | 700 | //\r |
130e2569 | 701 | return (VARIABLE_HEADER *) HEADER_ALIGN (Value);\r |
8d3a5c82 | 702 | }\r |
703 | \r | |
7c80e839 | 704 | /**\r |
9cad030b | 705 | \r |
7c80e839 | 706 | Gets the pointer to the first variable header in given variable store area.\r |
9cad030b | 707 | \r |
7c80e839 | 708 | @param VarStoreHeader Pointer to the Variable Store Header.\r |
9cad030b | 709 | \r |
8a2d4996 | 710 | @return Pointer to the first variable header.\r |
9cad030b | 711 | \r |
7c80e839 | 712 | **/\r |
713 | VARIABLE_HEADER *\r | |
714 | GetStartPointer (\r | |
715 | IN VARIABLE_STORE_HEADER *VarStoreHeader\r | |
716 | )\r | |
9cad030b | 717 | {\r |
718 | //\r | |
8a2d4996 | 719 | // The end of variable store.\r |
9cad030b | 720 | //\r |
721 | return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);\r | |
722 | }\r | |
052ad7e1 | 723 | \r |
7c80e839 | 724 | /**\r |
8d3a5c82 | 725 | \r |
7c80e839 | 726 | Gets the pointer to the end of the variable storage area.\r |
8d3a5c82 | 727 | \r |
7c80e839 | 728 | This function gets pointer to the end of the variable storage\r |
729 | area, according to the input variable store header.\r | |
8d3a5c82 | 730 | \r |
8a2d4996 | 731 | @param VarStoreHeader Pointer to the Variable Store Header.\r |
8d3a5c82 | 732 | \r |
fa0737a8 | 733 | @return Pointer to the end of the variable storage area.\r |
8d3a5c82 | 734 | \r |
7c80e839 | 735 | **/\r |
736 | VARIABLE_HEADER *\r | |
737 | GetEndPointer (\r | |
738 | IN VARIABLE_STORE_HEADER *VarStoreHeader\r | |
739 | )\r | |
8d3a5c82 | 740 | {\r |
741 | //\r | |
742 | // The end of variable store\r | |
743 | //\r | |
9cad030b | 744 | return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r |
8d3a5c82 | 745 | }\r |
746 | \r | |
4edb1866 SZ |
747 | /**\r |
748 | Record variable error flag.\r | |
749 | \r | |
750 | @param[in] Flag Variable error flag to record.\r | |
751 | @param[in] VariableName Name of variable.\r | |
752 | @param[in] VendorGuid Guid of variable.\r | |
753 | @param[in] Attributes Attributes of the variable.\r | |
754 | @param[in] VariableSize Size of the variable.\r | |
755 | \r | |
756 | **/\r | |
757 | VOID\r | |
758 | RecordVarErrorFlag (\r | |
759 | IN VAR_ERROR_FLAG Flag,\r | |
760 | IN CHAR16 *VariableName,\r | |
761 | IN EFI_GUID *VendorGuid,\r | |
762 | IN UINT32 Attributes,\r | |
763 | IN UINTN VariableSize\r | |
764 | )\r | |
765 | {\r | |
766 | EFI_STATUS Status;\r | |
767 | VARIABLE_POINTER_TRACK Variable;\r | |
768 | VAR_ERROR_FLAG *VarErrFlag;\r | |
769 | VAR_ERROR_FLAG TempFlag;\r | |
770 | \r | |
771 | DEBUG_CODE (\r | |
772 | DEBUG ((EFI_D_ERROR, "RecordVarErrorFlag (0x%02x) %s:%g - 0x%08x - 0x%x\n", Flag, VariableName, VendorGuid, Attributes, VariableSize));\r | |
773 | if (Flag == VAR_ERROR_FLAG_SYSTEM_ERROR) {\r | |
774 | if (AtRuntime ()) {\r | |
775 | DEBUG ((EFI_D_ERROR, "CommonRuntimeVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonRuntimeVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize));\r | |
776 | } else {\r | |
777 | DEBUG ((EFI_D_ERROR, "CommonVariableSpace = 0x%x - CommonVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonVariableTotalSize));\r | |
778 | }\r | |
779 | } else {\r | |
780 | DEBUG ((EFI_D_ERROR, "CommonMaxUserVariableSpace = 0x%x - CommonUserVariableTotalSize = 0x%x\n", mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonUserVariableTotalSize));\r | |
781 | }\r | |
782 | );\r | |
783 | \r | |
00ab76e0 SZ |
784 | if (!mEndOfDxe) {\r |
785 | //\r | |
786 | // Before EndOfDxe, just record the current boot variable error flag to local variable,\r | |
787 | // and leave the variable error flag in NV flash as the last boot variable error flag.\r | |
788 | // After EndOfDxe in InitializeVarErrorFlag (), the variable error flag in NV flash\r | |
789 | // will be initialized to this local current boot variable error flag.\r | |
790 | //\r | |
791 | mCurrentBootVarErrFlag &= Flag;\r | |
792 | return;\r | |
793 | }\r | |
794 | \r | |
4edb1866 SZ |
795 | //\r |
796 | // Record error flag (it should have be initialized).\r | |
797 | //\r | |
798 | Status = FindVariable (\r | |
799 | VAR_ERROR_FLAG_NAME,\r | |
800 | &gEdkiiVarErrorFlagGuid,\r | |
801 | &Variable,\r | |
802 | &mVariableModuleGlobal->VariableGlobal,\r | |
803 | FALSE\r | |
804 | );\r | |
805 | if (!EFI_ERROR (Status)) {\r | |
806 | VarErrFlag = (VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr);\r | |
807 | TempFlag = *VarErrFlag;\r | |
808 | TempFlag &= Flag;\r | |
809 | if (TempFlag == *VarErrFlag) {\r | |
810 | return;\r | |
811 | }\r | |
812 | Status = UpdateVariableStore (\r | |
813 | &mVariableModuleGlobal->VariableGlobal,\r | |
814 | FALSE,\r | |
815 | FALSE,\r | |
816 | mVariableModuleGlobal->FvbInstance,\r | |
817 | (UINTN) VarErrFlag - (UINTN) mNvVariableCache + (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r | |
818 | sizeof (TempFlag),\r | |
819 | &TempFlag\r | |
820 | );\r | |
821 | if (!EFI_ERROR (Status)) {\r | |
822 | //\r | |
823 | // Update the data in NV cache.\r | |
824 | //\r | |
4a23636a | 825 | *VarErrFlag = TempFlag;\r |
4edb1866 SZ |
826 | }\r |
827 | }\r | |
828 | }\r | |
829 | \r | |
830 | /**\r | |
831 | Initialize variable error flag.\r | |
832 | \r | |
833 | Before EndOfDxe, the variable indicates the last boot variable error flag,\r | |
834 | then it means the last boot variable error flag must be got before EndOfDxe.\r | |
835 | After EndOfDxe, the variable indicates the current boot variable error flag,\r | |
836 | then it means the current boot variable error flag must be got after EndOfDxe.\r | |
837 | \r | |
838 | **/\r | |
839 | VOID\r | |
840 | InitializeVarErrorFlag (\r | |
841 | VOID\r | |
842 | )\r | |
843 | {\r | |
844 | EFI_STATUS Status;\r | |
845 | VARIABLE_POINTER_TRACK Variable;\r | |
846 | VAR_ERROR_FLAG Flag;\r | |
847 | VAR_ERROR_FLAG VarErrFlag;\r | |
848 | \r | |
849 | if (!mEndOfDxe) {\r | |
850 | return;\r | |
851 | }\r | |
852 | \r | |
00ab76e0 | 853 | Flag = mCurrentBootVarErrFlag;\r |
4edb1866 SZ |
854 | DEBUG ((EFI_D_INFO, "Initialize variable error flag (%02x)\n", Flag));\r |
855 | \r | |
856 | Status = FindVariable (\r | |
857 | VAR_ERROR_FLAG_NAME,\r | |
858 | &gEdkiiVarErrorFlagGuid,\r | |
859 | &Variable,\r | |
860 | &mVariableModuleGlobal->VariableGlobal,\r | |
861 | FALSE\r | |
862 | );\r | |
863 | if (!EFI_ERROR (Status)) {\r | |
864 | VarErrFlag = *((VAR_ERROR_FLAG *) GetVariableDataPtr (Variable.CurrPtr));\r | |
865 | if (VarErrFlag == Flag) {\r | |
866 | return;\r | |
867 | }\r | |
868 | }\r | |
869 | \r | |
870 | UpdateVariable (\r | |
871 | VAR_ERROR_FLAG_NAME,\r | |
872 | &gEdkiiVarErrorFlagGuid,\r | |
873 | &Flag,\r | |
874 | sizeof (Flag),\r | |
875 | VARIABLE_ATTRIBUTE_NV_BS_RT,\r | |
fa0737a8 SZ |
876 | 0,\r |
877 | 0,\r | |
878 | &Variable,\r | |
879 | NULL\r | |
4edb1866 SZ |
880 | );\r |
881 | }\r | |
882 | \r | |
883 | /**\r | |
884 | Is user variable?\r | |
885 | \r | |
886 | @param[in] Variable Pointer to variable header.\r | |
887 | \r | |
888 | @retval TRUE User variable.\r | |
889 | @retval FALSE System variable.\r | |
890 | \r | |
891 | **/\r | |
892 | BOOLEAN\r | |
893 | IsUserVariable (\r | |
894 | IN VARIABLE_HEADER *Variable\r | |
895 | )\r | |
896 | {\r | |
897 | VAR_CHECK_VARIABLE_PROPERTY Property;\r | |
898 | \r | |
899 | //\r | |
900 | // Only after End Of Dxe, the variables belong to system variable are fixed.\r | |
901 | // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,\r | |
902 | // then no need to check if the variable is user variable or not specially.\r | |
903 | //\r | |
904 | if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) {\r | |
8021f4c7 | 905 | if (VarCheckLibVariablePropertyGet (GetVariableNamePtr (Variable), GetVendorGuidPtr (Variable), &Property) == EFI_NOT_FOUND) {\r |
4edb1866 SZ |
906 | return TRUE;\r |
907 | }\r | |
908 | }\r | |
909 | return FALSE;\r | |
910 | }\r | |
911 | \r | |
912 | /**\r | |
913 | Calculate common user variable total size.\r | |
914 | \r | |
915 | **/\r | |
916 | VOID\r | |
917 | CalculateCommonUserVariableTotalSize (\r | |
918 | VOID\r | |
919 | )\r | |
920 | {\r | |
921 | VARIABLE_HEADER *Variable;\r | |
922 | VARIABLE_HEADER *NextVariable;\r | |
923 | UINTN VariableSize;\r | |
924 | VAR_CHECK_VARIABLE_PROPERTY Property;\r | |
925 | \r | |
926 | //\r | |
927 | // Only after End Of Dxe, the variables belong to system variable are fixed.\r | |
928 | // If PcdMaxUserNvStorageVariableSize is 0, it means user variable share the same NV storage with system variable,\r | |
929 | // then no need to calculate the common user variable total size specially.\r | |
930 | //\r | |
931 | if (mEndOfDxe && (mVariableModuleGlobal->CommonMaxUserVariableSpace != mVariableModuleGlobal->CommonVariableSpace)) {\r | |
932 | Variable = GetStartPointer (mNvVariableCache);\r | |
933 | while (IsValidVariableHeader (Variable, GetEndPointer (mNvVariableCache))) {\r | |
934 | NextVariable = GetNextVariablePtr (Variable);\r | |
935 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r | |
936 | if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
8021f4c7 | 937 | if (VarCheckLibVariablePropertyGet (GetVariableNamePtr (Variable), GetVendorGuidPtr (Variable), &Property) == EFI_NOT_FOUND) {\r |
4edb1866 SZ |
938 | //\r |
939 | // No property, it is user variable.\r | |
940 | //\r | |
941 | mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize;\r | |
942 | }\r | |
943 | }\r | |
944 | \r | |
945 | Variable = NextVariable;\r | |
946 | }\r | |
947 | }\r | |
948 | }\r | |
949 | \r | |
950 | /**\r | |
951 | Initialize variable quota.\r | |
952 | \r | |
953 | **/\r | |
954 | VOID\r | |
955 | InitializeVariableQuota (\r | |
956 | VOID\r | |
957 | )\r | |
958 | {\r | |
8021f4c7 | 959 | if (!mEndOfDxe) {\r |
4edb1866 SZ |
960 | return;\r |
961 | }\r | |
4edb1866 SZ |
962 | \r |
963 | InitializeVarErrorFlag ();\r | |
964 | CalculateCommonUserVariableTotalSize ();\r | |
965 | }\r | |
052ad7e1 | 966 | \r |
7c80e839 | 967 | /**\r |
968 | \r | |
969 | Variable store garbage collection and reclaim operation.\r | |
970 | \r | |
fa0737a8 SZ |
971 | @param[in] VariableBase Base address of variable store.\r |
972 | @param[out] LastVariableOffset Offset of last variable.\r | |
973 | @param[in] IsVolatile The variable store is volatile or not;\r | |
974 | if it is non-volatile, need FTW.\r | |
975 | @param[in, out] UpdatingPtrTrack Pointer to updating variable pointer track structure.\r | |
976 | @param[in] NewVariable Pointer to new variable.\r | |
977 | @param[in] NewVariableSize New variable size.\r | |
7c80e839 | 978 | \r |
fa0737a8 SZ |
979 | @return EFI_SUCCESS Reclaim operation has finished successfully.\r |
980 | @return EFI_OUT_OF_RESOURCES No enough memory resources or variable space.\r | |
981 | @return Others Unexpect error happened during reclaim operation.\r | |
7c80e839 | 982 | \r |
983 | **/\r | |
8d3a5c82 | 984 | EFI_STATUS\r |
8d3a5c82 | 985 | Reclaim (\r |
fa0737a8 SZ |
986 | IN EFI_PHYSICAL_ADDRESS VariableBase,\r |
987 | OUT UINTN *LastVariableOffset,\r | |
988 | IN BOOLEAN IsVolatile,\r | |
989 | IN OUT VARIABLE_POINTER_TRACK *UpdatingPtrTrack,\r | |
990 | IN VARIABLE_HEADER *NewVariable,\r | |
991 | IN UINTN NewVariableSize\r | |
8d3a5c82 | 992 | )\r |
8d3a5c82 | 993 | {\r |
994 | VARIABLE_HEADER *Variable;\r | |
814bae52 | 995 | VARIABLE_HEADER *AddedVariable;\r |
8d3a5c82 | 996 | VARIABLE_HEADER *NextVariable;\r |
814bae52 | 997 | VARIABLE_HEADER *NextAddedVariable;\r |
8d3a5c82 | 998 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r |
999 | UINT8 *ValidBuffer;\r | |
814bae52 | 1000 | UINTN MaximumBufferSize;\r |
8d3a5c82 | 1001 | UINTN VariableSize;\r |
814bae52 | 1002 | UINTN NameSize;\r |
8d3a5c82 | 1003 | UINT8 *CurrPtr;\r |
814bae52 | 1004 | VOID *Point0;\r |
1005 | VOID *Point1;\r | |
1006 | BOOLEAN FoundAdded;\r | |
8d3a5c82 | 1007 | EFI_STATUS Status;\r |
8f3a9e58 | 1008 | UINTN CommonVariableTotalSize;\r |
4edb1866 | 1009 | UINTN CommonUserVariableTotalSize;\r |
8f3a9e58 | 1010 | UINTN HwErrVariableTotalSize;\r |
23b06935 | 1011 | VARIABLE_HEADER *UpdatingVariable;\r |
7baf3c69 | 1012 | VARIABLE_HEADER *UpdatingInDeletedTransition;\r |
23b06935 SZ |
1013 | \r |
1014 | UpdatingVariable = NULL;\r | |
7baf3c69 | 1015 | UpdatingInDeletedTransition = NULL;\r |
23b06935 SZ |
1016 | if (UpdatingPtrTrack != NULL) {\r |
1017 | UpdatingVariable = UpdatingPtrTrack->CurrPtr;\r | |
7baf3c69 | 1018 | UpdatingInDeletedTransition = UpdatingPtrTrack->InDeletedTransitionPtr;\r |
23b06935 | 1019 | }\r |
8d3a5c82 | 1020 | \r |
1021 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);\r | |
8f3a9e58 SZ |
1022 | \r |
1023 | CommonVariableTotalSize = 0;\r | |
4edb1866 | 1024 | CommonUserVariableTotalSize = 0;\r |
8f3a9e58 | 1025 | HwErrVariableTotalSize = 0;\r |
8d3a5c82 | 1026 | \r |
128ef095 SZ |
1027 | if (IsVolatile) {\r |
1028 | //\r | |
1029 | // Start Pointers for the variable.\r | |
1030 | //\r | |
1031 | Variable = GetStartPointer (VariableStoreHeader);\r | |
1032 | MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);\r | |
1033 | \r | |
6ebffb67 | 1034 | while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r |
128ef095 SZ |
1035 | NextVariable = GetNextVariablePtr (Variable);\r |
1036 | if ((Variable->State == VAR_ADDED || Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) &&\r | |
1037 | Variable != UpdatingVariable &&\r | |
1038 | Variable != UpdatingInDeletedTransition\r | |
1039 | ) {\r | |
1040 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r | |
1041 | MaximumBufferSize += VariableSize;\r | |
1042 | }\r | |
8d3a5c82 | 1043 | \r |
128ef095 | 1044 | Variable = NextVariable;\r |
8d3a5c82 | 1045 | }\r |
1046 | \r | |
128ef095 SZ |
1047 | if (NewVariable != NULL) {\r |
1048 | //\r | |
1049 | // Add the new variable size.\r | |
1050 | //\r | |
1051 | MaximumBufferSize += NewVariableSize;\r | |
1052 | }\r | |
8d3a5c82 | 1053 | \r |
7baf3c69 | 1054 | //\r |
128ef095 SZ |
1055 | // Reserve the 1 Bytes with Oxff to identify the\r |
1056 | // end of the variable buffer.\r | |
7baf3c69 | 1057 | //\r |
128ef095 SZ |
1058 | MaximumBufferSize += 1;\r |
1059 | ValidBuffer = AllocatePool (MaximumBufferSize);\r | |
1060 | if (ValidBuffer == NULL) {\r | |
1061 | return EFI_OUT_OF_RESOURCES;\r | |
1062 | }\r | |
1063 | } else {\r | |
1064 | //\r | |
1065 | // For NV variable reclaim, don't allocate pool here and just use mNvVariableCache\r | |
1066 | // as the buffer to reduce SMRAM consumption for SMM variable driver.\r | |
1067 | //\r | |
1068 | MaximumBufferSize = mNvVariableCache->Size;\r | |
1069 | ValidBuffer = (UINT8 *) mNvVariableCache;\r | |
8d3a5c82 | 1070 | }\r |
1071 | \r | |
814bae52 | 1072 | SetMem (ValidBuffer, MaximumBufferSize, 0xff);\r |
8d3a5c82 | 1073 | \r |
1074 | //\r | |
8a2d4996 | 1075 | // Copy variable store header.\r |
8d3a5c82 | 1076 | //\r |
814bae52 | 1077 | CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r |
1078 | CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r | |
8d3a5c82 | 1079 | \r |
814bae52 | 1080 | //\r |
8a2d4996 | 1081 | // Reinstall all ADDED variables as long as they are not identical to Updating Variable.\r |
fa0737a8 | 1082 | //\r |
814bae52 | 1083 | Variable = GetStartPointer (VariableStoreHeader);\r |
6ebffb67 | 1084 | while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r |
8d3a5c82 | 1085 | NextVariable = GetNextVariablePtr (Variable);\r |
7baf3c69 | 1086 | if (Variable != UpdatingVariable && Variable->State == VAR_ADDED) {\r |
8d3a5c82 | 1087 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r |
1088 | CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r | |
1089 | CurrPtr += VariableSize;\r | |
2fcdca1d | 1090 | if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r |
8f3a9e58 | 1091 | HwErrVariableTotalSize += VariableSize;\r |
2fcdca1d | 1092 | } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r |
8f3a9e58 | 1093 | CommonVariableTotalSize += VariableSize;\r |
4edb1866 SZ |
1094 | if (IsUserVariable (Variable)) {\r |
1095 | CommonUserVariableTotalSize += VariableSize;\r | |
1096 | }\r | |
2fcdca1d | 1097 | }\r |
8d3a5c82 | 1098 | }\r |
8d3a5c82 | 1099 | Variable = NextVariable;\r |
1100 | }\r | |
5ead4a07 | 1101 | \r |
814bae52 | 1102 | //\r |
8a2d4996 | 1103 | // Reinstall all in delete transition variables.\r |
fa0737a8 | 1104 | //\r |
7baf3c69 | 1105 | Variable = GetStartPointer (VariableStoreHeader);\r |
6ebffb67 | 1106 | while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r |
814bae52 | 1107 | NextVariable = GetNextVariablePtr (Variable);\r |
7baf3c69 | 1108 | if (Variable != UpdatingVariable && Variable != UpdatingInDeletedTransition && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r |
814bae52 | 1109 | \r |
1110 | //\r | |
fa0737a8 | 1111 | // Buffer has cached all ADDED variable.\r |
814bae52 | 1112 | // Per IN_DELETED variable, we have to guarantee that\r |
fa0737a8 SZ |
1113 | // no ADDED one in previous buffer.\r |
1114 | //\r | |
1115 | \r | |
814bae52 | 1116 | FoundAdded = FALSE;\r |
1117 | AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r | |
6ebffb67 | 1118 | while (IsValidVariableHeader (AddedVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) ValidBuffer))) {\r |
814bae52 | 1119 | NextAddedVariable = GetNextVariablePtr (AddedVariable);\r |
1120 | NameSize = NameSizeOfVariable (AddedVariable);\r | |
fa0737a8 | 1121 | if (CompareGuid (GetVendorGuidPtr (AddedVariable), GetVendorGuidPtr (Variable)) &&\r |
814bae52 | 1122 | NameSize == NameSizeOfVariable (Variable)\r |
1123 | ) {\r | |
1124 | Point0 = (VOID *) GetVariableNamePtr (AddedVariable);\r | |
1125 | Point1 = (VOID *) GetVariableNamePtr (Variable);\r | |
23b06935 | 1126 | if (CompareMem (Point0, Point1, NameSize) == 0) {\r |
814bae52 | 1127 | FoundAdded = TRUE;\r |
1128 | break;\r | |
1129 | }\r | |
1130 | }\r | |
1131 | AddedVariable = NextAddedVariable;\r | |
1132 | }\r | |
1133 | if (!FoundAdded) {\r | |
5ead4a07 | 1134 | //\r |
8a2d4996 | 1135 | // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.\r |
5ead4a07 | 1136 | //\r |
814bae52 | 1137 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r |
1138 | CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r | |
5ead4a07 | 1139 | ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r |
814bae52 | 1140 | CurrPtr += VariableSize;\r |
2fcdca1d | 1141 | if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r |
8f3a9e58 | 1142 | HwErrVariableTotalSize += VariableSize;\r |
2fcdca1d | 1143 | } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r |
8f3a9e58 | 1144 | CommonVariableTotalSize += VariableSize;\r |
4edb1866 SZ |
1145 | if (IsUserVariable (Variable)) {\r |
1146 | CommonUserVariableTotalSize += VariableSize;\r | |
1147 | }\r | |
2fcdca1d | 1148 | }\r |
814bae52 | 1149 | }\r |
1150 | }\r | |
1151 | \r | |
1152 | Variable = NextVariable;\r | |
1153 | }\r | |
8d3a5c82 | 1154 | \r |
7baf3c69 SZ |
1155 | //\r |
1156 | // Install the new variable if it is not NULL.\r | |
1157 | //\r | |
1158 | if (NewVariable != NULL) {\r | |
809e2bbf | 1159 | if (((UINTN) CurrPtr - (UINTN) ValidBuffer) + NewVariableSize > VariableStoreHeader->Size) {\r |
7baf3c69 SZ |
1160 | //\r |
1161 | // No enough space to store the new variable.\r | |
1162 | //\r | |
1163 | Status = EFI_OUT_OF_RESOURCES;\r | |
1164 | goto Done;\r | |
1165 | }\r | |
1166 | if (!IsVolatile) {\r | |
1167 | if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
1168 | HwErrVariableTotalSize += NewVariableSize;\r | |
1169 | } else if ((NewVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
1170 | CommonVariableTotalSize += NewVariableSize;\r | |
4edb1866 SZ |
1171 | if (IsUserVariable (NewVariable)) {\r |
1172 | CommonUserVariableTotalSize += NewVariableSize;\r | |
1173 | }\r | |
7baf3c69 SZ |
1174 | }\r |
1175 | if ((HwErrVariableTotalSize > PcdGet32 (PcdHwErrStorageSize)) ||\r | |
4edb1866 SZ |
1176 | (CommonVariableTotalSize > mVariableModuleGlobal->CommonVariableSpace) ||\r |
1177 | (CommonUserVariableTotalSize > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r | |
7baf3c69 SZ |
1178 | //\r |
1179 | // No enough space to store the new variable by NV or NV+HR attribute.\r | |
1180 | //\r | |
1181 | Status = EFI_OUT_OF_RESOURCES;\r | |
1182 | goto Done;\r | |
1183 | }\r | |
1184 | }\r | |
1185 | \r | |
1186 | CopyMem (CurrPtr, (UINT8 *) NewVariable, NewVariableSize);\r | |
1187 | ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r | |
1188 | if (UpdatingVariable != NULL) {\r | |
1189 | UpdatingPtrTrack->CurrPtr = (VARIABLE_HEADER *)((UINTN)UpdatingPtrTrack->StartPtr + ((UINTN)CurrPtr - (UINTN)GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer)));\r | |
1190 | UpdatingPtrTrack->InDeletedTransitionPtr = NULL;\r | |
1191 | }\r | |
1192 | CurrPtr += NewVariableSize;\r | |
1193 | }\r | |
1194 | \r | |
8d3a5c82 | 1195 | if (IsVolatile) {\r |
1196 | //\r | |
8a2d4996 | 1197 | // If volatile variable store, just copy valid buffer.\r |
8d3a5c82 | 1198 | //\r |
1199 | SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);\r | |
809e2bbf HW |
1200 | CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) CurrPtr - (UINTN) ValidBuffer);\r |
1201 | *LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer;\r | |
8a2d4996 | 1202 | Status = EFI_SUCCESS;\r |
8d3a5c82 | 1203 | } else {\r |
1204 | //\r | |
1205 | // If non-volatile variable store, perform FTW here.\r | |
1206 | //\r | |
1207 | Status = FtwVariableSpace (\r | |
1208 | VariableBase,\r | |
128ef095 | 1209 | (VARIABLE_STORE_HEADER *) ValidBuffer\r |
8d3a5c82 | 1210 | );\r |
128ef095 | 1211 | if (!EFI_ERROR (Status)) {\r |
809e2bbf | 1212 | *LastVariableOffset = (UINTN) CurrPtr - (UINTN) ValidBuffer;\r |
8f3a9e58 SZ |
1213 | mVariableModuleGlobal->HwErrVariableTotalSize = HwErrVariableTotalSize;\r |
1214 | mVariableModuleGlobal->CommonVariableTotalSize = CommonVariableTotalSize;\r | |
4edb1866 | 1215 | mVariableModuleGlobal->CommonUserVariableTotalSize = CommonUserVariableTotalSize;\r |
128ef095 | 1216 | } else {\r |
b2c59ce8 SZ |
1217 | mVariableModuleGlobal->HwErrVariableTotalSize = 0;\r |
1218 | mVariableModuleGlobal->CommonVariableTotalSize = 0;\r | |
1219 | mVariableModuleGlobal->CommonUserVariableTotalSize = 0;\r | |
4edb1866 SZ |
1220 | Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase);\r |
1221 | while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableBase))) {\r | |
1222 | NextVariable = GetNextVariablePtr (Variable);\r | |
1223 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r | |
128ef095 | 1224 | if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r |
4edb1866 | 1225 | mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r |
128ef095 | 1226 | } else if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r |
4edb1866 SZ |
1227 | mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r |
1228 | if (IsUserVariable (Variable)) {\r | |
1229 | mVariableModuleGlobal->CommonUserVariableTotalSize += VariableSize;\r | |
1230 | }\r | |
128ef095 | 1231 | }\r |
8f3a9e58 | 1232 | \r |
4edb1866 | 1233 | Variable = NextVariable;\r |
128ef095 | 1234 | }\r |
4edb1866 | 1235 | *LastVariableOffset = (UINTN) Variable - (UINTN) VariableBase;\r |
8f3a9e58 | 1236 | }\r |
8d3a5c82 | 1237 | }\r |
1238 | \r | |
7baf3c69 | 1239 | Done:\r |
128ef095 SZ |
1240 | if (IsVolatile) {\r |
1241 | FreePool (ValidBuffer);\r | |
1242 | } else {\r | |
1243 | //\r | |
1244 | // For NV variable reclaim, we use mNvVariableCache as the buffer, so copy the data back.\r | |
1245 | //\r | |
1246 | CopyMem (mNvVariableCache, (UINT8 *)(UINTN)VariableBase, VariableStoreHeader->Size);\r | |
1247 | }\r | |
814bae52 | 1248 | \r |
8d3a5c82 | 1249 | return Status;\r |
1250 | }\r | |
1251 | \r | |
0f7aff72 RN |
1252 | /**\r |
1253 | Find the variable in the specified variable store.\r | |
1254 | \r | |
fa0737a8 SZ |
1255 | @param[in] VariableName Name of the variable to be found\r |
1256 | @param[in] VendorGuid Vendor GUID to be found.\r | |
1257 | @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r | |
1258 | check at runtime when searching variable.\r | |
1259 | @param[in, out] PtrTrack Variable Track Pointer structure that contains Variable Information.\r | |
0f7aff72 | 1260 | \r |
fa0737a8 SZ |
1261 | @retval EFI_SUCCESS Variable found successfully\r |
1262 | @retval EFI_NOT_FOUND Variable not found\r | |
0f7aff72 RN |
1263 | **/\r |
1264 | EFI_STATUS\r | |
1265 | FindVariableEx (\r | |
1266 | IN CHAR16 *VariableName,\r | |
1267 | IN EFI_GUID *VendorGuid,\r | |
9622df63 | 1268 | IN BOOLEAN IgnoreRtCheck,\r |
0f7aff72 RN |
1269 | IN OUT VARIABLE_POINTER_TRACK *PtrTrack\r |
1270 | )\r | |
1271 | {\r | |
1272 | VARIABLE_HEADER *InDeletedVariable;\r | |
1273 | VOID *Point;\r | |
1274 | \r | |
23b06935 SZ |
1275 | PtrTrack->InDeletedTransitionPtr = NULL;\r |
1276 | \r | |
0f7aff72 RN |
1277 | //\r |
1278 | // Find the variable by walk through HOB, volatile and non-volatile variable store.\r | |
1279 | //\r | |
1280 | InDeletedVariable = NULL;\r | |
1281 | \r | |
1282 | for ( PtrTrack->CurrPtr = PtrTrack->StartPtr\r | |
6ebffb67 | 1283 | ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr)\r |
0f7aff72 RN |
1284 | ; PtrTrack->CurrPtr = GetNextVariablePtr (PtrTrack->CurrPtr)\r |
1285 | ) {\r | |
fa0737a8 | 1286 | if (PtrTrack->CurrPtr->State == VAR_ADDED ||\r |
0f7aff72 RN |
1287 | PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r |
1288 | ) {\r | |
9622df63 | 1289 | if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r |
0f7aff72 RN |
1290 | if (VariableName[0] == 0) {\r |
1291 | if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r | |
1292 | InDeletedVariable = PtrTrack->CurrPtr;\r | |
1293 | } else {\r | |
23b06935 | 1294 | PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r |
0f7aff72 RN |
1295 | return EFI_SUCCESS;\r |
1296 | }\r | |
1297 | } else {\r | |
fa0737a8 | 1298 | if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack->CurrPtr))) {\r |
0f7aff72 RN |
1299 | Point = (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr);\r |
1300 | \r | |
1301 | ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) != 0);\r | |
1302 | if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTrack->CurrPtr)) == 0) {\r | |
1303 | if (PtrTrack->CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r | |
1304 | InDeletedVariable = PtrTrack->CurrPtr;\r | |
1305 | } else {\r | |
23b06935 | 1306 | PtrTrack->InDeletedTransitionPtr = InDeletedVariable;\r |
0f7aff72 RN |
1307 | return EFI_SUCCESS;\r |
1308 | }\r | |
1309 | }\r | |
1310 | }\r | |
1311 | }\r | |
1312 | }\r | |
1313 | }\r | |
1314 | }\r | |
1315 | \r | |
1316 | PtrTrack->CurrPtr = InDeletedVariable;\r | |
1317 | return (PtrTrack->CurrPtr == NULL) ? EFI_NOT_FOUND : EFI_SUCCESS;\r | |
1318 | }\r | |
1319 | \r | |
33a5a666 | 1320 | \r |
7c80e839 | 1321 | /**\r |
1322 | Finds variable in storage blocks of volatile and non-volatile storage areas.\r | |
1323 | \r | |
1324 | This code finds variable in storage blocks of volatile and non-volatile storage areas.\r | |
1325 | If VariableName is an empty string, then we just return the first\r | |
1326 | qualified variable without comparing VariableName and VendorGuid.\r | |
9622df63 SZ |
1327 | If IgnoreRtCheck is TRUE, then we ignore the EFI_VARIABLE_RUNTIME_ACCESS attribute check\r |
1328 | at runtime when searching existing variable, only VariableName and VendorGuid are compared.\r | |
1329 | Otherwise, variables without EFI_VARIABLE_RUNTIME_ACCESS are not visible at runtime.\r | |
7c80e839 | 1330 | \r |
fa0737a8 SZ |
1331 | @param[in] VariableName Name of the variable to be found.\r |
1332 | @param[in] VendorGuid Vendor GUID to be found.\r | |
1333 | @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,\r | |
7c80e839 | 1334 | including the range searched and the target position.\r |
fa0737a8 | 1335 | @param[in] Global Pointer to VARIABLE_GLOBAL structure, including\r |
7c80e839 | 1336 | base of volatile variable storage area, base of\r |
1337 | NV variable storage area, and a lock.\r | |
fa0737a8 | 1338 | @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS attribute\r |
9622df63 | 1339 | check at runtime when searching variable.\r |
7c80e839 | 1340 | \r |
1341 | @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while\r | |
8a2d4996 | 1342 | VendorGuid is NULL.\r |
1343 | @retval EFI_SUCCESS Variable successfully found.\r | |
255a3f33 | 1344 | @retval EFI_NOT_FOUND Variable not found\r |
33a5a666 | 1345 | \r |
7c80e839 | 1346 | **/\r |
8d3a5c82 | 1347 | EFI_STATUS\r |
8d3a5c82 | 1348 | FindVariable (\r |
1349 | IN CHAR16 *VariableName,\r | |
1350 | IN EFI_GUID *VendorGuid,\r | |
1351 | OUT VARIABLE_POINTER_TRACK *PtrTrack,\r | |
9622df63 SZ |
1352 | IN VARIABLE_GLOBAL *Global,\r |
1353 | IN BOOLEAN IgnoreRtCheck\r | |
8d3a5c82 | 1354 | )\r |
8d3a5c82 | 1355 | {\r |
0f7aff72 RN |
1356 | EFI_STATUS Status;\r |
1357 | VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r | |
1358 | VARIABLE_STORE_TYPE Type;\r | |
1359 | \r | |
1360 | if (VariableName[0] != 0 && VendorGuid == NULL) {\r | |
1361 | return EFI_INVALID_PARAMETER;\r | |
1362 | }\r | |
8d3a5c82 | 1363 | \r |
8d3a5c82 | 1364 | //\r |
0f7aff72 | 1365 | // 0: Volatile, 1: HOB, 2: Non-Volatile.\r |
36873a61 | 1366 | // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName\r |
8a2d4996 | 1367 | // make use of this mapping to implement search algorithm.\r |
8d3a5c82 | 1368 | //\r |
0f7aff72 RN |
1369 | VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) Global->VolatileVariableBase;\r |
1370 | VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) Global->HobVariableBase;\r | |
1371 | VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r | |
8d3a5c82 | 1372 | \r |
1373 | //\r | |
0f7aff72 | 1374 | // Find the variable by walk through HOB, volatile and non-volatile variable store.\r |
8d3a5c82 | 1375 | //\r |
0f7aff72 RN |
1376 | for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r |
1377 | if (VariableStoreHeader[Type] == NULL) {\r | |
1378 | continue;\r | |
1379 | }\r | |
814bae52 | 1380 | \r |
0f7aff72 RN |
1381 | PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r |
1382 | PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r | |
1383 | PtrTrack->Volatile = (BOOLEAN) (Type == VariableStoreTypeVolatile);\r | |
8d3a5c82 | 1384 | \r |
9622df63 | 1385 | Status = FindVariableEx (VariableName, VendorGuid, IgnoreRtCheck, PtrTrack);\r |
0f7aff72 RN |
1386 | if (!EFI_ERROR (Status)) {\r |
1387 | return Status;\r | |
814bae52 | 1388 | }\r |
8d3a5c82 | 1389 | }\r |
8d3a5c82 | 1390 | return EFI_NOT_FOUND;\r |
1391 | }\r | |
1392 | \r | |
7c80e839 | 1393 | /**\r |
72399dae | 1394 | Get index from supported language codes according to language string.\r |
1395 | \r | |
1396 | This code is used to get corresponding index in supported language codes. It can handle\r | |
0254efc0 | 1397 | RFC4646 and ISO639 language tags.\r |
72399dae | 1398 | In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.\r |
0254efc0 | 1399 | In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.\r |
72399dae | 1400 | \r |
1401 | For example:\r | |
1402 | SupportedLang = "engfraengfra"\r | |
1403 | Lang = "eng"\r | |
1404 | Iso639Language = TRUE\r | |
1405 | The return value is "0".\r | |
1406 | Another example:\r | |
1407 | SupportedLang = "en;fr;en-US;fr-FR"\r | |
1408 | Lang = "fr-FR"\r | |
1409 | Iso639Language = FALSE\r | |
1410 | The return value is "3".\r | |
1411 | \r | |
1412 | @param SupportedLang Platform supported language codes.\r | |
1413 | @param Lang Configured language.\r | |
0254efc0 | 1414 | @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r |
72399dae | 1415 | \r |
8a2d4996 | 1416 | @retval The index of language in the language codes.\r |
8d3a5c82 | 1417 | \r |
7c80e839 | 1418 | **/\r |
72399dae | 1419 | UINTN\r |
72399dae | 1420 | GetIndexFromSupportedLangCodes(\r |
1421 | IN CHAR8 *SupportedLang,\r | |
1422 | IN CHAR8 *Lang,\r | |
1423 | IN BOOLEAN Iso639Language\r | |
fa0737a8 | 1424 | )\r |
8d3a5c82 | 1425 | {\r |
72399dae | 1426 | UINTN Index;\r |
255a3f33 RN |
1427 | UINTN CompareLength;\r |
1428 | UINTN LanguageLength;\r | |
72399dae | 1429 | \r |
72399dae | 1430 | if (Iso639Language) {\r |
255a3f33 | 1431 | CompareLength = ISO_639_2_ENTRY_SIZE;\r |
72399dae | 1432 | for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r |
1433 | if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r | |
1434 | //\r | |
1435 | // Successfully find the index of Lang string in SupportedLang string.\r | |
1436 | //\r | |
1437 | Index = Index / CompareLength;\r | |
1438 | return Index;\r | |
1439 | }\r | |
1440 | }\r | |
1441 | ASSERT (FALSE);\r | |
1442 | return 0;\r | |
1443 | } else {\r | |
1444 | //\r | |
0254efc0 | 1445 | // Compare RFC4646 language code\r |
72399dae | 1446 | //\r |
255a3f33 RN |
1447 | Index = 0;\r |
1448 | for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);\r | |
1449 | \r | |
1450 | for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {\r | |
72399dae | 1451 | //\r |
255a3f33 | 1452 | // Skip ';' characters in SupportedLang\r |
72399dae | 1453 | //\r |
255a3f33 RN |
1454 | for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);\r |
1455 | //\r | |
1456 | // Determine the length of the next language code in SupportedLang\r | |
1457 | //\r | |
1458 | for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);\r | |
fa0737a8 SZ |
1459 | \r |
1460 | if ((CompareLength == LanguageLength) &&\r | |
255a3f33 | 1461 | (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {\r |
72399dae | 1462 | //\r |
1463 | // Successfully find the index of Lang string in SupportedLang string.\r | |
1464 | //\r | |
1465 | return Index;\r | |
1466 | }\r | |
72399dae | 1467 | }\r |
1468 | ASSERT (FALSE);\r | |
1469 | return 0;\r | |
8d3a5c82 | 1470 | }\r |
72399dae | 1471 | }\r |
33a5a666 | 1472 | \r |
72399dae | 1473 | /**\r |
1474 | Get language string from supported language codes according to index.\r | |
1475 | \r | |
8a2d4996 | 1476 | This code is used to get corresponding language strings in supported language codes. It can handle\r |
0254efc0 | 1477 | RFC4646 and ISO639 language tags.\r |
72399dae | 1478 | In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.\r |
0254efc0 | 1479 | In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.\r |
72399dae | 1480 | \r |
1481 | For example:\r | |
1482 | SupportedLang = "engfraengfra"\r | |
1483 | Index = "1"\r | |
1484 | Iso639Language = TRUE\r | |
1485 | The return value is "fra".\r | |
1486 | Another example:\r | |
1487 | SupportedLang = "en;fr;en-US;fr-FR"\r | |
1488 | Index = "1"\r | |
1489 | Iso639Language = FALSE\r | |
1490 | The return value is "fr".\r | |
1491 | \r | |
1492 | @param SupportedLang Platform supported language codes.\r | |
8a2d4996 | 1493 | @param Index The index in supported language codes.\r |
0254efc0 | 1494 | @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r |
72399dae | 1495 | \r |
8a2d4996 | 1496 | @retval The language string in the language codes.\r |
8d3a5c82 | 1497 | \r |
72399dae | 1498 | **/\r |
1499 | CHAR8 *\r | |
72399dae | 1500 | GetLangFromSupportedLangCodes (\r |
1501 | IN CHAR8 *SupportedLang,\r | |
1502 | IN UINTN Index,\r | |
1503 | IN BOOLEAN Iso639Language\r | |
1504 | )\r | |
1505 | {\r | |
1506 | UINTN SubIndex;\r | |
255a3f33 | 1507 | UINTN CompareLength;\r |
72399dae | 1508 | CHAR8 *Supported;\r |
8d3a5c82 | 1509 | \r |
72399dae | 1510 | SubIndex = 0;\r |
1511 | Supported = SupportedLang;\r | |
1512 | if (Iso639Language) {\r | |
1513 | //\r | |
8a2d4996 | 1514 | // According to the index of Lang string in SupportedLang string to get the language.\r |
1515 | // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.\r | |
72399dae | 1516 | // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r |
1517 | //\r | |
255a3f33 RN |
1518 | CompareLength = ISO_639_2_ENTRY_SIZE;\r |
1519 | mVariableModuleGlobal->Lang[CompareLength] = '\0';\r | |
72399dae | 1520 | return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);\r |
f68af18e | 1521 | \r |
8d3a5c82 | 1522 | } else {\r |
72399dae | 1523 | while (TRUE) {\r |
1524 | //\r | |
8a2d4996 | 1525 | // Take semicolon as delimitation, sequentially traverse supported language codes.\r |
72399dae | 1526 | //\r |
1527 | for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r | |
1528 | Supported++;\r | |
1529 | }\r | |
1530 | if ((*Supported == '\0') && (SubIndex != Index)) {\r | |
1531 | //\r | |
1532 | // Have completed the traverse, but not find corrsponding string.\r | |
1533 | // This case is not allowed to happen.\r | |
1534 | //\r | |
1535 | ASSERT(FALSE);\r | |
1536 | return NULL;\r | |
1537 | }\r | |
1538 | if (SubIndex == Index) {\r | |
1539 | //\r | |
8a2d4996 | 1540 | // According to the index of Lang string in SupportedLang string to get the language.\r |
72399dae | 1541 | // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r |
1542 | // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r | |
1543 | //\r | |
255a3f33 | 1544 | mVariableModuleGlobal->PlatformLang[CompareLength] = '\0';\r |
72399dae | 1545 | return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);\r |
1546 | }\r | |
1547 | SubIndex++;\r | |
8a2d4996 | 1548 | \r |
5c033766 RN |
1549 | //\r |
1550 | // Skip ';' characters in Supported\r | |
1551 | //\r | |
1552 | for (; *Supported != '\0' && *Supported == ';'; Supported++);\r | |
72399dae | 1553 | }\r |
8d3a5c82 | 1554 | }\r |
8d3a5c82 | 1555 | }\r |
1556 | \r | |
255a3f33 | 1557 | /**\r |
fa0737a8 SZ |
1558 | Returns a pointer to an allocated buffer that contains the best matching language\r |
1559 | from a set of supported languages.\r | |
1560 | \r | |
1561 | This function supports both ISO 639-2 and RFC 4646 language codes, but language\r | |
255a3f33 RN |
1562 | code types may not be mixed in a single call to this function. This function\r |
1563 | supports a variable argument list that allows the caller to pass in a prioritized\r | |
1564 | list of language codes to test against all the language codes in SupportedLanguages.\r | |
1565 | \r | |
1566 | If SupportedLanguages is NULL, then ASSERT().\r | |
1567 | \r | |
1568 | @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r | |
fa0737a8 | 1569 | contains a set of language codes in the format\r |
255a3f33 RN |
1570 | specified by Iso639Language.\r |
1571 | @param[in] Iso639Language If TRUE, then all language codes are assumed to be\r | |
1572 | in ISO 639-2 format. If FALSE, then all language\r | |
1573 | codes are assumed to be in RFC 4646 language format\r | |
fa0737a8 | 1574 | @param[in] ... A variable argument list that contains pointers to\r |
255a3f33 RN |
1575 | Null-terminated ASCII strings that contain one or more\r |
1576 | language codes in the format specified by Iso639Language.\r | |
1577 | The first language code from each of these language\r | |
1578 | code lists is used to determine if it is an exact or\r | |
fa0737a8 | 1579 | close match to any of the language codes in\r |
255a3f33 RN |
1580 | SupportedLanguages. Close matches only apply to RFC 4646\r |
1581 | language codes, and the matching algorithm from RFC 4647\r | |
fa0737a8 | 1582 | is used to determine if a close match is present. If\r |
255a3f33 RN |
1583 | an exact or close match is found, then the matching\r |
1584 | language code from SupportedLanguages is returned. If\r | |
1585 | no matches are found, then the next variable argument\r | |
fa0737a8 | 1586 | parameter is evaluated. The variable argument list\r |
255a3f33 RN |
1587 | is terminated by a NULL.\r |
1588 | \r | |
1589 | @retval NULL The best matching language could not be found in SupportedLanguages.\r | |
fa0737a8 | 1590 | @retval NULL There are not enough resources available to return the best matching\r |
255a3f33 | 1591 | language.\r |
fa0737a8 | 1592 | @retval Other A pointer to a Null-terminated ASCII string that is the best matching\r |
255a3f33 RN |
1593 | language in SupportedLanguages.\r |
1594 | \r | |
1595 | **/\r | |
1596 | CHAR8 *\r | |
e1adae60 | 1597 | EFIAPI\r |
255a3f33 | 1598 | VariableGetBestLanguage (\r |
fa0737a8 | 1599 | IN CONST CHAR8 *SupportedLanguages,\r |
255a3f33 RN |
1600 | IN BOOLEAN Iso639Language,\r |
1601 | ...\r | |
1602 | )\r | |
1603 | {\r | |
1604 | VA_LIST Args;\r | |
1605 | CHAR8 *Language;\r | |
1606 | UINTN CompareLength;\r | |
1607 | UINTN LanguageLength;\r | |
1608 | CONST CHAR8 *Supported;\r | |
1609 | CHAR8 *Buffer;\r | |
1610 | \r | |
fa0737a8 SZ |
1611 | if (SupportedLanguages == NULL) {\r |
1612 | return NULL;\r | |
1613 | }\r | |
255a3f33 RN |
1614 | \r |
1615 | VA_START (Args, Iso639Language);\r | |
1616 | while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r | |
1617 | //\r | |
1618 | // Default to ISO 639-2 mode\r | |
1619 | //\r | |
1620 | CompareLength = 3;\r | |
1621 | LanguageLength = MIN (3, AsciiStrLen (Language));\r | |
1622 | \r | |
1623 | //\r | |
1624 | // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r | |
1625 | //\r | |
1626 | if (!Iso639Language) {\r | |
1627 | for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r | |
1628 | }\r | |
1629 | \r | |
1630 | //\r | |
1631 | // Trim back the length of Language used until it is empty\r | |
1632 | //\r | |
1633 | while (LanguageLength > 0) {\r | |
1634 | //\r | |
1635 | // Loop through all language codes in SupportedLanguages\r | |
1636 | //\r | |
1637 | for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r | |
1638 | //\r | |
1639 | // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r | |
1640 | //\r | |
1641 | if (!Iso639Language) {\r | |
1642 | //\r | |
1643 | // Skip ';' characters in Supported\r | |
1644 | //\r | |
1645 | for (; *Supported != '\0' && *Supported == ';'; Supported++);\r | |
1646 | //\r | |
1647 | // Determine the length of the next language code in Supported\r | |
1648 | //\r | |
1649 | for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r | |
1650 | //\r | |
1651 | // If Language is longer than the Supported, then skip to the next language\r | |
1652 | //\r | |
1653 | if (LanguageLength > CompareLength) {\r | |
1654 | continue;\r | |
1655 | }\r | |
1656 | }\r | |
1657 | //\r | |
1658 | // See if the first LanguageLength characters in Supported match Language\r | |
1659 | //\r | |
1660 | if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r | |
1661 | VA_END (Args);\r | |
1662 | \r | |
1663 | Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;\r | |
1664 | Buffer[CompareLength] = '\0';\r | |
1665 | return CopyMem (Buffer, Supported, CompareLength);\r | |
1666 | }\r | |
1667 | }\r | |
1668 | \r | |
1669 | if (Iso639Language) {\r | |
1670 | //\r | |
1671 | // If ISO 639 mode, then each language can only be tested once\r | |
1672 | //\r | |
1673 | LanguageLength = 0;\r | |
1674 | } else {\r | |
1675 | //\r | |
fa0737a8 | 1676 | // If RFC 4646 mode, then trim Language from the right to the next '-' character\r |
255a3f33 RN |
1677 | //\r |
1678 | for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r | |
1679 | }\r | |
1680 | }\r | |
1681 | }\r | |
1682 | VA_END (Args);\r | |
1683 | \r | |
1684 | //\r | |
fa0737a8 | 1685 | // No matches were found\r |
255a3f33 RN |
1686 | //\r |
1687 | return NULL;\r | |
1688 | }\r | |
1689 | \r | |
b2bd493e SZ |
1690 | /**\r |
1691 | This function is to check if the remaining variable space is enough to set\r | |
1692 | all Variables from argument list successfully. The purpose of the check\r | |
1693 | is to keep the consistency of the Variables to be in variable storage.\r | |
1694 | \r | |
1695 | Note: Variables are assumed to be in same storage.\r | |
1696 | The set sequence of Variables will be same with the sequence of VariableEntry from argument list,\r | |
1697 | so follow the argument sequence to check the Variables.\r | |
1698 | \r | |
1699 | @param[in] Attributes Variable attributes for Variable entries.\r | |
fa0737a8 SZ |
1700 | @param[in] Marker VA_LIST style variable argument list.\r |
1701 | The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.\r | |
1702 | A NULL terminates the list. The VariableSize of\r | |
9a12e582 DG |
1703 | VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.\r |
1704 | It will be changed to variable total size as output.\r | |
b2bd493e SZ |
1705 | \r |
1706 | @retval TRUE Have enough variable space to set the Variables successfully.\r | |
1707 | @retval FALSE No enough variable space to set the Variables successfully.\r | |
1708 | \r | |
1709 | **/\r | |
1710 | BOOLEAN\r | |
1711 | EFIAPI\r | |
fa0737a8 | 1712 | CheckRemainingSpaceForConsistencyInternal (\r |
b2bd493e | 1713 | IN UINT32 Attributes,\r |
fa0737a8 | 1714 | IN VA_LIST Marker\r |
b2bd493e SZ |
1715 | )\r |
1716 | {\r | |
1717 | EFI_STATUS Status;\r | |
1718 | VA_LIST Args;\r | |
1719 | VARIABLE_ENTRY_CONSISTENCY *VariableEntry;\r | |
1720 | UINT64 MaximumVariableStorageSize;\r | |
1721 | UINT64 RemainingVariableStorageSize;\r | |
1722 | UINT64 MaximumVariableSize;\r | |
1723 | UINTN TotalNeededSize;\r | |
1724 | UINTN OriginalVarSize;\r | |
1725 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
1726 | VARIABLE_POINTER_TRACK VariablePtrTrack;\r | |
1727 | VARIABLE_HEADER *NextVariable;\r | |
9a12e582 DG |
1728 | UINTN VarNameSize;\r |
1729 | UINTN VarDataSize;\r | |
b2bd493e SZ |
1730 | \r |
1731 | //\r | |
1732 | // Non-Volatile related.\r | |
1733 | //\r | |
1734 | VariableStoreHeader = mNvVariableCache;\r | |
1735 | \r | |
1736 | Status = VariableServiceQueryVariableInfoInternal (\r | |
1737 | Attributes,\r | |
1738 | &MaximumVariableStorageSize,\r | |
1739 | &RemainingVariableStorageSize,\r | |
1740 | &MaximumVariableSize\r | |
1741 | );\r | |
1742 | ASSERT_EFI_ERROR (Status);\r | |
1743 | \r | |
1744 | TotalNeededSize = 0;\r | |
b7789dc6 | 1745 | VA_COPY (Args, Marker);\r |
b2bd493e SZ |
1746 | VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r |
1747 | while (VariableEntry != NULL) {\r | |
9a12e582 DG |
1748 | //\r |
1749 | // Calculate variable total size.\r | |
1750 | //\r | |
1751 | VarNameSize = StrSize (VariableEntry->Name);\r | |
1752 | VarNameSize += GET_PAD_SIZE (VarNameSize);\r | |
1753 | VarDataSize = VariableEntry->VariableSize;\r | |
1754 | VarDataSize += GET_PAD_SIZE (VarDataSize);\r | |
fa0737a8 | 1755 | VariableEntry->VariableSize = HEADER_ALIGN (GetVariableHeaderSize () + VarNameSize + VarDataSize);\r |
9a12e582 | 1756 | \r |
b2bd493e SZ |
1757 | TotalNeededSize += VariableEntry->VariableSize;\r |
1758 | VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r | |
1759 | }\r | |
415700ec | 1760 | VA_END (Args);\r |
b2bd493e SZ |
1761 | \r |
1762 | if (RemainingVariableStorageSize >= TotalNeededSize) {\r | |
1763 | //\r | |
1764 | // Already have enough space.\r | |
1765 | //\r | |
1766 | return TRUE;\r | |
1767 | } else if (AtRuntime ()) {\r | |
1768 | //\r | |
1769 | // At runtime, no reclaim.\r | |
1770 | // The original variable space of Variables can't be reused.\r | |
1771 | //\r | |
1772 | return FALSE;\r | |
1773 | }\r | |
1774 | \r | |
b7789dc6 | 1775 | VA_COPY (Args, Marker);\r |
b2bd493e SZ |
1776 | VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r |
1777 | while (VariableEntry != NULL) {\r | |
1778 | //\r | |
1779 | // Check if Variable[Index] has been present and get its size.\r | |
1780 | //\r | |
1781 | OriginalVarSize = 0;\r | |
1782 | VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r | |
1783 | VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r | |
1784 | Status = FindVariableEx (\r | |
1785 | VariableEntry->Name,\r | |
1786 | VariableEntry->Guid,\r | |
1787 | FALSE,\r | |
1788 | &VariablePtrTrack\r | |
1789 | );\r | |
1790 | if (!EFI_ERROR (Status)) {\r | |
1791 | //\r | |
1792 | // Get size of Variable[Index].\r | |
1793 | //\r | |
1794 | NextVariable = GetNextVariablePtr (VariablePtrTrack.CurrPtr);\r | |
1795 | OriginalVarSize = (UINTN) NextVariable - (UINTN) VariablePtrTrack.CurrPtr;\r | |
1796 | //\r | |
1797 | // Add the original size of Variable[Index] to remaining variable storage size.\r | |
1798 | //\r | |
1799 | RemainingVariableStorageSize += OriginalVarSize;\r | |
1800 | }\r | |
1801 | if (VariableEntry->VariableSize > RemainingVariableStorageSize) {\r | |
1802 | //\r | |
1803 | // No enough space for Variable[Index].\r | |
1804 | //\r | |
b2bd493e SZ |
1805 | return FALSE;\r |
1806 | }\r | |
1807 | //\r | |
1808 | // Sub the (new) size of Variable[Index] from remaining variable storage size.\r | |
1809 | //\r | |
1810 | RemainingVariableStorageSize -= VariableEntry->VariableSize;\r | |
1811 | VariableEntry = VA_ARG (Args, VARIABLE_ENTRY_CONSISTENCY *);\r | |
1812 | }\r | |
415700ec | 1813 | VA_END (Args);\r |
b2bd493e SZ |
1814 | \r |
1815 | return TRUE;\r | |
1816 | }\r | |
1817 | \r | |
fa0737a8 SZ |
1818 | /**\r |
1819 | This function is to check if the remaining variable space is enough to set\r | |
1820 | all Variables from argument list successfully. The purpose of the check\r | |
1821 | is to keep the consistency of the Variables to be in variable storage.\r | |
1822 | \r | |
1823 | Note: Variables are assumed to be in same storage.\r | |
1824 | The set sequence of Variables will be same with the sequence of VariableEntry from argument list,\r | |
1825 | so follow the argument sequence to check the Variables.\r | |
1826 | \r | |
1827 | @param[in] Attributes Variable attributes for Variable entries.\r | |
1828 | @param ... The variable argument list with type VARIABLE_ENTRY_CONSISTENCY *.\r | |
1829 | A NULL terminates the list. The VariableSize of\r | |
1830 | VARIABLE_ENTRY_CONSISTENCY is the variable data size as input.\r | |
1831 | It will be changed to variable total size as output.\r | |
1832 | \r | |
1833 | @retval TRUE Have enough variable space to set the Variables successfully.\r | |
1834 | @retval FALSE No enough variable space to set the Variables successfully.\r | |
1835 | \r | |
1836 | **/\r | |
1837 | BOOLEAN\r | |
1838 | EFIAPI\r | |
1839 | CheckRemainingSpaceForConsistency (\r | |
1840 | IN UINT32 Attributes,\r | |
1841 | ...\r | |
1842 | )\r | |
1843 | {\r | |
1844 | VA_LIST Marker;\r | |
1845 | BOOLEAN Return;\r | |
1846 | \r | |
1847 | VA_START (Marker, Attributes);\r | |
1848 | \r | |
1849 | Return = CheckRemainingSpaceForConsistencyInternal (Attributes, Marker);\r | |
1850 | \r | |
1851 | VA_END (Marker);\r | |
1852 | \r | |
1853 | return Return;\r | |
1854 | }\r | |
1855 | \r | |
72399dae | 1856 | /**\r |
1857 | Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r | |
052ad7e1 | 1858 | \r |
72399dae | 1859 | When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r |
052ad7e1 | 1860 | \r |
72399dae | 1861 | According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r |
1862 | and are read-only. Therefore, in variable driver, only store the original value for other use.\r | |
8d3a5c82 | 1863 | \r |
8a2d4996 | 1864 | @param[in] VariableName Name of variable.\r |
8d3a5c82 | 1865 | \r |
8a2d4996 | 1866 | @param[in] Data Variable data.\r |
8d3a5c82 | 1867 | \r |
8a2d4996 | 1868 | @param[in] DataSize Size of data. 0 means delete.\r |
72399dae | 1869 | \r |
9bc5dabb SZ |
1870 | @retval EFI_SUCCESS The update operation is successful or ignored.\r |
1871 | @retval EFI_WRITE_PROTECTED Update PlatformLangCodes/LangCodes at runtime.\r | |
1872 | @retval EFI_OUT_OF_RESOURCES No enough variable space to do the update operation.\r | |
1873 | @retval Others Other errors happened during the update operation.\r | |
1874 | \r | |
7c80e839 | 1875 | **/\r |
9bc5dabb | 1876 | EFI_STATUS\r |
d6550260 | 1877 | AutoUpdateLangVariable (\r |
72399dae | 1878 | IN CHAR16 *VariableName,\r |
1879 | IN VOID *Data,\r | |
1880 | IN UINTN DataSize\r | |
052ad7e1 | 1881 | )\r |
8d3a5c82 | 1882 | {\r |
255a3f33 RN |
1883 | EFI_STATUS Status;\r |
1884 | CHAR8 *BestPlatformLang;\r | |
1885 | CHAR8 *BestLang;\r | |
1886 | UINTN Index;\r | |
1887 | UINT32 Attributes;\r | |
72399dae | 1888 | VARIABLE_POINTER_TRACK Variable;\r |
255a3f33 | 1889 | BOOLEAN SetLanguageCodes;\r |
b2bd493e | 1890 | VARIABLE_ENTRY_CONSISTENCY VariableEntry[2];\r |
8d3a5c82 | 1891 | \r |
72399dae | 1892 | //\r |
255a3f33 | 1893 | // Don't do updates for delete operation\r |
72399dae | 1894 | //\r |
255a3f33 | 1895 | if (DataSize == 0) {\r |
9bc5dabb | 1896 | return EFI_SUCCESS;\r |
255a3f33 RN |
1897 | }\r |
1898 | \r | |
1899 | SetLanguageCodes = FALSE;\r | |
8d3a5c82 | 1900 | \r |
6675a21f | 1901 | if (StrCmp (VariableName, EFI_PLATFORM_LANG_CODES_VARIABLE_NAME) == 0) {\r |
255a3f33 RN |
1902 | //\r |
1903 | // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r | |
1904 | //\r | |
8a2d4996 | 1905 | if (AtRuntime ()) {\r |
9bc5dabb | 1906 | return EFI_WRITE_PROTECTED;\r |
255a3f33 RN |
1907 | }\r |
1908 | \r | |
1909 | SetLanguageCodes = TRUE;\r | |
1910 | \r | |
72399dae | 1911 | //\r |
1912 | // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r | |
1913 | // Therefore, in variable driver, only store the original value for other use.\r | |
1914 | //\r | |
255a3f33 RN |
1915 | if (mVariableModuleGlobal->PlatformLangCodes != NULL) {\r |
1916 | FreePool (mVariableModuleGlobal->PlatformLangCodes);\r | |
1917 | }\r | |
1918 | mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r | |
1919 | ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);\r | |
1920 | \r | |
72399dae | 1921 | //\r |
fa0737a8 | 1922 | // PlatformLang holds a single language from PlatformLangCodes,\r |
255a3f33 | 1923 | // so the size of PlatformLangCodes is enough for the PlatformLang.\r |
72399dae | 1924 | //\r |
255a3f33 RN |
1925 | if (mVariableModuleGlobal->PlatformLang != NULL) {\r |
1926 | FreePool (mVariableModuleGlobal->PlatformLang);\r | |
1927 | }\r | |
1928 | mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r | |
1929 | ASSERT (mVariableModuleGlobal->PlatformLang != NULL);\r | |
fdb7765f | 1930 | \r |
6675a21f | 1931 | } else if (StrCmp (VariableName, EFI_LANG_CODES_VARIABLE_NAME) == 0) {\r |
72399dae | 1932 | //\r |
255a3f33 | 1933 | // LangCodes is a volatile variable, so it can not be updated at runtime.\r |
72399dae | 1934 | //\r |
8a2d4996 | 1935 | if (AtRuntime ()) {\r |
9bc5dabb | 1936 | return EFI_WRITE_PROTECTED;\r |
255a3f33 RN |
1937 | }\r |
1938 | \r | |
1939 | SetLanguageCodes = TRUE;\r | |
8d3a5c82 | 1940 | \r |
8d3a5c82 | 1941 | //\r |
255a3f33 RN |
1942 | // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r |
1943 | // Therefore, in variable driver, only store the original value for other use.\r | |
8d3a5c82 | 1944 | //\r |
255a3f33 RN |
1945 | if (mVariableModuleGlobal->LangCodes != NULL) {\r |
1946 | FreePool (mVariableModuleGlobal->LangCodes);\r | |
1947 | }\r | |
1948 | mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r | |
1949 | ASSERT (mVariableModuleGlobal->LangCodes != NULL);\r | |
1950 | }\r | |
8d3a5c82 | 1951 | \r |
fa0737a8 | 1952 | if (SetLanguageCodes\r |
255a3f33 RN |
1953 | && (mVariableModuleGlobal->PlatformLangCodes != NULL)\r |
1954 | && (mVariableModuleGlobal->LangCodes != NULL)) {\r | |
8d3a5c82 | 1955 | //\r |
255a3f33 RN |
1956 | // Update Lang if PlatformLang is already set\r |
1957 | // Update PlatformLang if Lang is already set\r | |
8d3a5c82 | 1958 | //\r |
6675a21f | 1959 | Status = FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r |
255a3f33 RN |
1960 | if (!EFI_ERROR (Status)) {\r |
1961 | //\r | |
1962 | // Update Lang\r | |
1963 | //\r | |
6675a21f | 1964 | VariableName = EFI_PLATFORM_LANG_VARIABLE_NAME;\r |
255a3f33 | 1965 | Data = GetVariableDataPtr (Variable.CurrPtr);\r |
fa0737a8 | 1966 | DataSize = DataSizeOfVariable (Variable.CurrPtr);\r |
255a3f33 | 1967 | } else {\r |
6675a21f | 1968 | Status = FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r |
255a3f33 RN |
1969 | if (!EFI_ERROR (Status)) {\r |
1970 | //\r | |
1971 | // Update PlatformLang\r | |
1972 | //\r | |
6675a21f | 1973 | VariableName = EFI_LANG_VARIABLE_NAME;\r |
255a3f33 | 1974 | Data = GetVariableDataPtr (Variable.CurrPtr);\r |
fa0737a8 | 1975 | DataSize = DataSizeOfVariable (Variable.CurrPtr);\r |
255a3f33 RN |
1976 | } else {\r |
1977 | //\r | |
1978 | // Neither PlatformLang nor Lang is set, directly return\r | |
1979 | //\r | |
9bc5dabb | 1980 | return EFI_SUCCESS;\r |
255a3f33 RN |
1981 | }\r |
1982 | }\r | |
1983 | }\r | |
9bc5dabb SZ |
1984 | \r |
1985 | Status = EFI_SUCCESS;\r | |
1986 | \r | |
255a3f33 RN |
1987 | //\r |
1988 | // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r | |
1989 | //\r | |
1990 | Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r | |
8d3a5c82 | 1991 | \r |
6675a21f | 1992 | if (StrCmp (VariableName, EFI_PLATFORM_LANG_VARIABLE_NAME) == 0) {\r |
8d3a5c82 | 1993 | //\r |
255a3f33 | 1994 | // Update Lang when PlatformLangCodes/LangCodes were set.\r |
8d3a5c82 | 1995 | //\r |
255a3f33 RN |
1996 | if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r |
1997 | //\r | |
1998 | // When setting PlatformLang, firstly get most matched language string from supported language codes.\r | |
1999 | //\r | |
2000 | BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r | |
2001 | if (BestPlatformLang != NULL) {\r | |
2002 | //\r | |
2003 | // Get the corresponding index in language codes.\r | |
2004 | //\r | |
2005 | Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r | |
fdb7765f | 2006 | \r |
255a3f33 RN |
2007 | //\r |
2008 | // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r | |
2009 | //\r | |
2010 | BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);\r | |
8d3a5c82 | 2011 | \r |
255a3f33 | 2012 | //\r |
9a12e582 | 2013 | // Check the variable space for both Lang and PlatformLang variable.\r |
b2bd493e | 2014 | //\r |
9a12e582 | 2015 | VariableEntry[0].VariableSize = ISO_639_2_ENTRY_SIZE + 1;\r |
b2bd493e SZ |
2016 | VariableEntry[0].Guid = &gEfiGlobalVariableGuid;\r |
2017 | VariableEntry[0].Name = EFI_LANG_VARIABLE_NAME;\r | |
fa0737a8 | 2018 | \r |
9a12e582 | 2019 | VariableEntry[1].VariableSize = AsciiStrSize (BestPlatformLang);\r |
b2bd493e SZ |
2020 | VariableEntry[1].Guid = &gEfiGlobalVariableGuid;\r |
2021 | VariableEntry[1].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;\r | |
2022 | if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {\r | |
2023 | //\r | |
2024 | // No enough variable space to set both Lang and PlatformLang successfully.\r | |
2025 | //\r | |
2026 | Status = EFI_OUT_OF_RESOURCES;\r | |
2027 | } else {\r | |
2028 | //\r | |
2029 | // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r | |
2030 | //\r | |
2031 | FindVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r | |
8d3a5c82 | 2032 | \r |
b2bd493e | 2033 | Status = UpdateVariable (EFI_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestLang,\r |
fa0737a8 | 2034 | ISO_639_2_ENTRY_SIZE + 1, Attributes, 0, 0, &Variable, NULL);\r |
b2bd493e | 2035 | }\r |
8d3a5c82 | 2036 | \r |
b2bd493e | 2037 | DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a Status: %r\n", BestPlatformLang, BestLang, Status));\r |
255a3f33 RN |
2038 | }\r |
2039 | }\r | |
72399dae | 2040 | \r |
6675a21f | 2041 | } else if (StrCmp (VariableName, EFI_LANG_VARIABLE_NAME) == 0) {\r |
72399dae | 2042 | //\r |
255a3f33 | 2043 | // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r |
72399dae | 2044 | //\r |
255a3f33 RN |
2045 | if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r |
2046 | //\r | |
2047 | // When setting Lang, firstly get most matched language string from supported language codes.\r | |
2048 | //\r | |
2049 | BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r | |
2050 | if (BestLang != NULL) {\r | |
2051 | //\r | |
2052 | // Get the corresponding index in language codes.\r | |
2053 | //\r | |
2054 | Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r | |
72399dae | 2055 | \r |
255a3f33 RN |
2056 | //\r |
2057 | // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r | |
2058 | //\r | |
2059 | BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r | |
2060 | \r | |
2061 | //\r | |
9a12e582 | 2062 | // Check the variable space for both PlatformLang and Lang variable.\r |
b2bd493e | 2063 | //\r |
9a12e582 | 2064 | VariableEntry[0].VariableSize = AsciiStrSize (BestPlatformLang);\r |
b2bd493e SZ |
2065 | VariableEntry[0].Guid = &gEfiGlobalVariableGuid;\r |
2066 | VariableEntry[0].Name = EFI_PLATFORM_LANG_VARIABLE_NAME;\r | |
9a12e582 DG |
2067 | \r |
2068 | VariableEntry[1].VariableSize = ISO_639_2_ENTRY_SIZE + 1;\r | |
b2bd493e SZ |
2069 | VariableEntry[1].Guid = &gEfiGlobalVariableGuid;\r |
2070 | VariableEntry[1].Name = EFI_LANG_VARIABLE_NAME;\r | |
2071 | if (!CheckRemainingSpaceForConsistency (VARIABLE_ATTRIBUTE_NV_BS_RT, &VariableEntry[0], &VariableEntry[1], NULL)) {\r | |
2072 | //\r | |
2073 | // No enough variable space to set both PlatformLang and Lang successfully.\r | |
2074 | //\r | |
2075 | Status = EFI_OUT_OF_RESOURCES;\r | |
2076 | } else {\r | |
2077 | //\r | |
2078 | // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r | |
2079 | //\r | |
2080 | FindVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r | |
72399dae | 2081 | \r |
fa0737a8 SZ |
2082 | Status = UpdateVariable (EFI_PLATFORM_LANG_VARIABLE_NAME, &gEfiGlobalVariableGuid, BestPlatformLang,\r |
2083 | AsciiStrSize (BestPlatformLang), Attributes, 0, 0, &Variable, NULL);\r | |
b2bd493e | 2084 | }\r |
72399dae | 2085 | \r |
9bc5dabb | 2086 | DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a Status: %r\n", BestLang, BestPlatformLang, Status));\r |
255a3f33 RN |
2087 | }\r |
2088 | }\r | |
72399dae | 2089 | }\r |
9bc5dabb | 2090 | \r |
b2bd493e SZ |
2091 | if (SetLanguageCodes) {\r |
2092 | //\r | |
2093 | // Continue to set PlatformLangCodes or LangCodes.\r | |
2094 | //\r | |
2095 | return EFI_SUCCESS;\r | |
2096 | } else {\r | |
2097 | return Status;\r | |
2098 | }\r | |
8d3a5c82 | 2099 | }\r |
2100 | \r | |
7c80e839 | 2101 | /**\r |
fa0737a8 SZ |
2102 | Compare two EFI_TIME data.\r |
2103 | \r | |
2104 | \r | |
2105 | @param FirstTime A pointer to the first EFI_TIME data.\r | |
2106 | @param SecondTime A pointer to the second EFI_TIME data.\r | |
2107 | \r | |
2108 | @retval TRUE The FirstTime is not later than the SecondTime.\r | |
2109 | @retval FALSE The FirstTime is later than the SecondTime.\r | |
2110 | \r | |
2111 | **/\r | |
2112 | BOOLEAN\r | |
2113 | VariableCompareTimeStampInternal (\r | |
2114 | IN EFI_TIME *FirstTime,\r | |
2115 | IN EFI_TIME *SecondTime\r | |
2116 | )\r | |
2117 | {\r | |
2118 | if (FirstTime->Year != SecondTime->Year) {\r | |
2119 | return (BOOLEAN) (FirstTime->Year < SecondTime->Year);\r | |
2120 | } else if (FirstTime->Month != SecondTime->Month) {\r | |
2121 | return (BOOLEAN) (FirstTime->Month < SecondTime->Month);\r | |
2122 | } else if (FirstTime->Day != SecondTime->Day) {\r | |
2123 | return (BOOLEAN) (FirstTime->Day < SecondTime->Day);\r | |
2124 | } else if (FirstTime->Hour != SecondTime->Hour) {\r | |
2125 | return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour);\r | |
2126 | } else if (FirstTime->Minute != SecondTime->Minute) {\r | |
2127 | return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute);\r | |
2128 | }\r | |
2129 | \r | |
2130 | return (BOOLEAN) (FirstTime->Second <= SecondTime->Second);\r | |
2131 | }\r | |
2132 | \r | |
2133 | /**\r | |
2134 | Update the variable region with Variable information. If EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is set,\r | |
2135 | index of associated public key is needed.\r | |
052ad7e1 | 2136 | \r |
8a2d4996 | 2137 | @param[in] VariableName Name of variable.\r |
2138 | @param[in] VendorGuid Guid of variable.\r | |
2139 | @param[in] Data Variable data.\r | |
2140 | @param[in] DataSize Size of data. 0 means delete.\r | |
fa0737a8 SZ |
2141 | @param[in] Attributes Attributes of the variable.\r |
2142 | @param[in] KeyIndex Index of associated public key.\r | |
2143 | @param[in] MonotonicCount Value of associated monotonic count.\r | |
23b06935 | 2144 | @param[in, out] CacheVariable The variable information which is used to keep track of variable usage.\r |
fa0737a8 SZ |
2145 | @param[in] TimeStamp Value of associated TimeStamp.\r |
2146 | \r | |
72399dae | 2147 | @retval EFI_SUCCESS The update operation is success.\r |
72399dae | 2148 | @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r |
8d3a5c82 | 2149 | \r |
7c80e839 | 2150 | **/\r |
052ad7e1 | 2151 | EFI_STATUS\r |
72399dae | 2152 | UpdateVariable (\r |
8a2d4996 | 2153 | IN CHAR16 *VariableName,\r |
2154 | IN EFI_GUID *VendorGuid,\r | |
2155 | IN VOID *Data,\r | |
2156 | IN UINTN DataSize,\r | |
2157 | IN UINT32 Attributes OPTIONAL,\r | |
fa0737a8 SZ |
2158 | IN UINT32 KeyIndex OPTIONAL,\r |
2159 | IN UINT64 MonotonicCount OPTIONAL,\r | |
2160 | IN OUT VARIABLE_POINTER_TRACK *CacheVariable,\r | |
2161 | IN EFI_TIME *TimeStamp OPTIONAL\r | |
052ad7e1 | 2162 | )\r |
8d3a5c82 | 2163 | {\r |
8a9e0b72 | 2164 | EFI_STATUS Status;\r |
2165 | VARIABLE_HEADER *NextVariable;\r | |
72399dae | 2166 | UINTN ScratchSize;\r |
fa0737a8 | 2167 | UINTN MaxDataSize;\r |
8a9e0b72 | 2168 | UINTN VarNameOffset;\r |
2169 | UINTN VarDataOffset;\r | |
72399dae | 2170 | UINTN VarNameSize;\r |
8a9e0b72 | 2171 | UINTN VarSize;\r |
72399dae | 2172 | BOOLEAN Volatile;\r |
2173 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r | |
8a9e0b72 | 2174 | UINT8 State;\r |
8a2d4996 | 2175 | VARIABLE_POINTER_TRACK *Variable;\r |
2176 | VARIABLE_POINTER_TRACK NvVariable;\r | |
2177 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
2178 | UINTN CacheOffset;\r | |
fa0737a8 SZ |
2179 | UINT8 *BufferForMerge;\r |
2180 | UINTN MergedBufSize;\r | |
2181 | BOOLEAN DataReady;\r | |
2182 | UINTN DataOffset;\r | |
4edb1866 SZ |
2183 | BOOLEAN IsCommonVariable;\r |
2184 | BOOLEAN IsCommonUserVariable;\r | |
fa0737a8 | 2185 | AUTHENTICATED_VARIABLE_HEADER *AuthVariable;\r |
fdb7765f | 2186 | \r |
fa0737a8 | 2187 | if (mVariableModuleGlobal->FvbInstance == NULL) {\r |
5456306f | 2188 | //\r |
fa0737a8 | 2189 | // The FVB protocol is not ready, so the EFI_VARIABLE_WRITE_ARCH_PROTOCOL is not installed.\r |
5456306f | 2190 | //\r |
fa0737a8 SZ |
2191 | if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r |
2192 | //\r | |
2193 | // Trying to update NV variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL\r | |
2194 | //\r | |
2195 | DEBUG ((EFI_D_ERROR, "Update NV variable before EFI_VARIABLE_WRITE_ARCH_PROTOCOL ready - %r\n", EFI_NOT_AVAILABLE_YET));\r | |
2196 | return EFI_NOT_AVAILABLE_YET;\r | |
2197 | } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r | |
2198 | //\r | |
2199 | // Trying to update volatile authenticated variable prior to the installation of EFI_VARIABLE_WRITE_ARCH_PROTOCOL\r | |
2200 | // The authenticated variable perhaps is not initialized, just return here.\r | |
2201 | //\r | |
2202 | DEBUG ((EFI_D_ERROR, "Update AUTH variable before EFI_VARIABLE_WRITE_ARCH_PROTOCOL ready - %r\n", EFI_NOT_AVAILABLE_YET));\r | |
2203 | return EFI_NOT_AVAILABLE_YET;\r | |
2204 | }\r | |
5456306f | 2205 | }\r |
2206 | \r | |
31349131 SZ |
2207 | //\r |
2208 | // Check if CacheVariable points to the variable in variable HOB.\r | |
2209 | // If yes, let CacheVariable points to the variable in NV variable cache.\r | |
2210 | //\r | |
2211 | if ((CacheVariable->CurrPtr != NULL) &&\r | |
2212 | (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) &&\r | |
2213 | (CacheVariable->StartPtr == GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase))\r | |
2214 | ) {\r | |
2215 | CacheVariable->StartPtr = GetStartPointer (mNvVariableCache);\r | |
2216 | CacheVariable->EndPtr = GetEndPointer (mNvVariableCache);\r | |
2217 | CacheVariable->Volatile = FALSE;\r | |
2218 | Status = FindVariableEx (VariableName, VendorGuid, FALSE, CacheVariable);\r | |
2219 | if (CacheVariable->CurrPtr == NULL || EFI_ERROR (Status)) {\r | |
2220 | //\r | |
2221 | // There is no matched variable in NV variable cache.\r | |
2222 | //\r | |
2223 | if ((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0)) || (Attributes == 0)) {\r | |
2224 | //\r | |
2225 | // It is to delete variable,\r | |
2226 | // go to delete this variable in variable HOB and\r | |
2227 | // try to flush other variables from HOB to flash.\r | |
2228 | //\r | |
38eb83b8 | 2229 | UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, FALSE, TRUE, FALSE);\r |
31349131 SZ |
2230 | FlushHobVariableToFlash (VariableName, VendorGuid);\r |
2231 | return EFI_SUCCESS;\r | |
2232 | }\r | |
2233 | }\r | |
2234 | }\r | |
2235 | \r | |
5456306f | 2236 | if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {\r |
8a2d4996 | 2237 | Variable = CacheVariable;\r |
2238 | } else {\r | |
8a2d4996 | 2239 | //\r |
5456306f | 2240 | // Update/Delete existing NV variable.\r |
8a2d4996 | 2241 | // CacheVariable points to the variable in the memory copy of Flash area\r |
2242 | // Now let Variable points to the same variable in Flash area.\r | |
2243 | //\r | |
2244 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r | |
fa0737a8 | 2245 | Variable = &NvVariable;\r |
8a2d4996 | 2246 | Variable->StartPtr = GetStartPointer (VariableStoreHeader);\r |
9b18845a DL |
2247 | Variable->EndPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->EndPtr - (UINTN)CacheVariable->StartPtr));\r |
2248 | \r | |
5456306f | 2249 | Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));\r |
23b06935 SZ |
2250 | if (CacheVariable->InDeletedTransitionPtr != NULL) {\r |
2251 | Variable->InDeletedTransitionPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->InDeletedTransitionPtr - (UINTN)CacheVariable->StartPtr));\r | |
2252 | } else {\r | |
2253 | Variable->InDeletedTransitionPtr = NULL;\r | |
2254 | }\r | |
5456306f | 2255 | Variable->Volatile = FALSE;\r |
fa0737a8 | 2256 | }\r |
5456306f | 2257 | \r |
2258 | Fvb = mVariableModuleGlobal->FvbInstance;\r | |
fdb7765f | 2259 | \r |
fa0737a8 SZ |
2260 | //\r |
2261 | // Tricky part: Use scratch data area at the end of volatile variable store\r | |
2262 | // as a temporary storage.\r | |
2263 | //\r | |
2264 | NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r | |
2265 | ScratchSize = mVariableModuleGlobal->ScratchBufferSize;\r | |
2266 | SetMem (NextVariable, ScratchSize, 0xff);\r | |
2267 | DataReady = FALSE;\r | |
2268 | \r | |
72399dae | 2269 | if (Variable->CurrPtr != NULL) {\r |
8d3a5c82 | 2270 | //\r |
8a2d4996 | 2271 | // Update/Delete existing variable.\r |
8d3a5c82 | 2272 | //\r |
fa0737a8 | 2273 | if (AtRuntime ()) {\r |
c6492839 | 2274 | //\r |
fa0737a8 SZ |
2275 | // If AtRuntime and the variable is Volatile and Runtime Access,\r |
2276 | // the volatile is ReadOnly, and SetVariable should be aborted and\r | |
c6492839 | 2277 | // return EFI_WRITE_PROTECTED.\r |
2278 | //\r | |
72399dae | 2279 | if (Variable->Volatile) {\r |
c6492839 | 2280 | Status = EFI_WRITE_PROTECTED;\r |
2281 | goto Done;\r | |
2282 | }\r | |
2283 | //\r | |
fa0737a8 SZ |
2284 | // Only variable that have NV attributes can be updated/deleted in Runtime.\r |
2285 | //\r | |
9b18845a | 2286 | if ((CacheVariable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r |
fa0737a8 SZ |
2287 | Status = EFI_INVALID_PARAMETER;\r |
2288 | goto Done;\r | |
2289 | }\r | |
2290 | \r | |
2291 | //\r | |
2292 | // Only variable that have RT attributes can be updated/deleted in Runtime.\r | |
c6492839 | 2293 | //\r |
9b18845a | 2294 | if ((CacheVariable->CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) {\r |
c6492839 | 2295 | Status = EFI_INVALID_PARAMETER;\r |
fa0737a8 | 2296 | goto Done;\r |
c6492839 | 2297 | }\r |
2298 | }\r | |
8a2d4996 | 2299 | \r |
8d3a5c82 | 2300 | //\r |
c6492839 | 2301 | // Setting a data variable with no access, or zero DataSize attributes\r |
8a2d4996 | 2302 | // causes it to be deleted.\r |
fa0737a8 SZ |
2303 | // When the EFI_VARIABLE_APPEND_WRITE attribute is set, DataSize of zero will\r |
2304 | // not delete the variable.\r | |
8d3a5c82 | 2305 | //\r |
fa0737a8 | 2306 | if ((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0))|| ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0)) {\r |
23b06935 SZ |
2307 | if (Variable->InDeletedTransitionPtr != NULL) {\r |
2308 | //\r | |
2309 | // Both ADDED and IN_DELETED_TRANSITION variable are present,\r | |
2310 | // set IN_DELETED_TRANSITION one to DELETED state first.\r | |
2311 | //\r | |
ae658d91 | 2312 | ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r |
9b18845a | 2313 | State = CacheVariable->InDeletedTransitionPtr->State;\r |
23b06935 SZ |
2314 | State &= VAR_DELETED;\r |
2315 | Status = UpdateVariableStore (\r | |
2316 | &mVariableModuleGlobal->VariableGlobal,\r | |
2317 | Variable->Volatile,\r | |
2318 | FALSE,\r | |
2319 | Fvb,\r | |
2320 | (UINTN) &Variable->InDeletedTransitionPtr->State,\r | |
2321 | sizeof (UINT8),\r | |
2322 | &State\r | |
2323 | );\r | |
2324 | if (!EFI_ERROR (Status)) {\r | |
2325 | if (!Variable->Volatile) {\r | |
2326 | CacheVariable->InDeletedTransitionPtr->State = State;\r | |
2327 | }\r | |
2328 | } else {\r | |
2329 | goto Done;\r | |
2330 | }\r | |
2331 | }\r | |
2332 | \r | |
9b18845a | 2333 | State = CacheVariable->CurrPtr->State;\r |
c6492839 | 2334 | State &= VAR_DELETED;\r |
2335 | \r | |
2336 | Status = UpdateVariableStore (\r | |
052ad7e1 | 2337 | &mVariableModuleGlobal->VariableGlobal,\r |
72399dae | 2338 | Variable->Volatile,\r |
c6492839 | 2339 | FALSE,\r |
8a9e0b72 | 2340 | Fvb,\r |
72399dae | 2341 | (UINTN) &Variable->CurrPtr->State,\r |
c6492839 | 2342 | sizeof (UINT8),\r |
2343 | &State\r | |
fa0737a8 | 2344 | );\r |
33a5a666 | 2345 | if (!EFI_ERROR (Status)) {\r |
8a2d4996 | 2346 | UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r |
2347 | if (!Variable->Volatile) {\r | |
2348 | CacheVariable->CurrPtr->State = State;\r | |
335e2681 | 2349 | FlushHobVariableToFlash (VariableName, VendorGuid);\r |
8a2d4996 | 2350 | }\r |
33a5a666 | 2351 | }\r |
fa0737a8 | 2352 | goto Done;\r |
c6492839 | 2353 | }\r |
8d3a5c82 | 2354 | //\r |
8a2d4996 | 2355 | // If the variable is marked valid, and the same data has been passed in,\r |
c6492839 | 2356 | // then return to the caller immediately.\r |
8d3a5c82 | 2357 | //\r |
9b18845a DL |
2358 | if (DataSizeOfVariable (CacheVariable->CurrPtr) == DataSize &&\r |
2359 | (CompareMem (Data, GetVariableDataPtr (CacheVariable->CurrPtr), DataSize) == 0) &&\r | |
fa0737a8 SZ |
2360 | ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) &&\r |
2361 | (TimeStamp == NULL)) {\r | |
2362 | //\r | |
2363 | // Variable content unchanged and no need to update timestamp, just return.\r | |
2364 | //\r | |
8a2d4996 | 2365 | UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r |
c6492839 | 2366 | Status = EFI_SUCCESS;\r |
2367 | goto Done;\r | |
9b18845a DL |
2368 | } else if ((CacheVariable->CurrPtr->State == VAR_ADDED) ||\r |
2369 | (CacheVariable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r | |
814bae52 | 2370 | \r |
fa0737a8 SZ |
2371 | //\r |
2372 | // EFI_VARIABLE_APPEND_WRITE attribute only effects for existing variable.\r | |
2373 | //\r | |
2374 | if ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0) {\r | |
2375 | //\r | |
2376 | // NOTE: From 0 to DataOffset of NextVariable is reserved for Variable Header and Name.\r | |
2377 | // From DataOffset of NextVariable is to save the existing variable data.\r | |
2378 | //\r | |
9b18845a | 2379 | DataOffset = GetVariableDataOffset (CacheVariable->CurrPtr);\r |
fa0737a8 | 2380 | BufferForMerge = (UINT8 *) ((UINTN) NextVariable + DataOffset);\r |
9b18845a | 2381 | CopyMem (BufferForMerge, (UINT8 *) ((UINTN) CacheVariable->CurrPtr + DataOffset), DataSizeOfVariable (CacheVariable->CurrPtr));\r |
fa0737a8 SZ |
2382 | \r |
2383 | //\r | |
2384 | // Set Max Common/Auth Variable Data Size as default MaxDataSize.\r | |
2385 | //\r | |
2386 | if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r | |
2387 | MaxDataSize = mVariableModuleGlobal->MaxAuthVariableSize - DataOffset;\r | |
2388 | } else {\r | |
2389 | MaxDataSize = mVariableModuleGlobal->MaxVariableSize - DataOffset;\r | |
2390 | }\r | |
2391 | \r | |
2392 | //\r | |
2393 | // Append the new data to the end of existing data.\r | |
2394 | // Max Harware error record variable data size is different from common/auth variable.\r | |
2395 | //\r | |
2396 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
2397 | MaxDataSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - DataOffset;\r | |
2398 | }\r | |
2399 | \r | |
9b18845a | 2400 | if (DataSizeOfVariable (CacheVariable->CurrPtr) + DataSize > MaxDataSize) {\r |
fa0737a8 SZ |
2401 | //\r |
2402 | // Existing data size + new data size exceed maximum variable size limitation.\r | |
2403 | //\r | |
2404 | Status = EFI_INVALID_PARAMETER;\r | |
2405 | goto Done;\r | |
2406 | }\r | |
9b18845a DL |
2407 | CopyMem ((UINT8*) ((UINTN) BufferForMerge + DataSizeOfVariable (CacheVariable->CurrPtr)), Data, DataSize);\r |
2408 | MergedBufSize = DataSizeOfVariable (CacheVariable->CurrPtr) + DataSize;\r | |
fa0737a8 SZ |
2409 | \r |
2410 | //\r | |
2411 | // BufferForMerge(from DataOffset of NextVariable) has included the merged existing and new data.\r | |
2412 | //\r | |
2413 | Data = BufferForMerge;\r | |
2414 | DataSize = MergedBufSize;\r | |
2415 | DataReady = TRUE;\r | |
2416 | }\r | |
2417 | \r | |
c6492839 | 2418 | //\r |
8a2d4996 | 2419 | // Mark the old variable as in delete transition.\r |
c6492839 | 2420 | //\r |
9b18845a | 2421 | State = CacheVariable->CurrPtr->State;\r |
c6492839 | 2422 | State &= VAR_IN_DELETED_TRANSITION;\r |
2423 | \r | |
2424 | Status = UpdateVariableStore (\r | |
052ad7e1 | 2425 | &mVariableModuleGlobal->VariableGlobal,\r |
72399dae | 2426 | Variable->Volatile,\r |
c6492839 | 2427 | FALSE,\r |
8a9e0b72 | 2428 | Fvb,\r |
72399dae | 2429 | (UINTN) &Variable->CurrPtr->State,\r |
c6492839 | 2430 | sizeof (UINT8),\r |
2431 | &State\r | |
fa0737a8 | 2432 | );\r |
c6492839 | 2433 | if (EFI_ERROR (Status)) {\r |
fa0737a8 SZ |
2434 | goto Done;\r |
2435 | }\r | |
8a2d4996 | 2436 | if (!Variable->Volatile) {\r |
2437 | CacheVariable->CurrPtr->State = State;\r | |
2438 | }\r | |
fa0737a8 | 2439 | }\r |
72399dae | 2440 | } else {\r |
8d3a5c82 | 2441 | //\r |
8a2d4996 | 2442 | // Not found existing variable. Create a new variable.\r |
fa0737a8 SZ |
2443 | //\r |
2444 | \r | |
2445 | if ((DataSize == 0) && ((Attributes & EFI_VARIABLE_APPEND_WRITE) != 0)) {\r | |
2446 | Status = EFI_SUCCESS;\r | |
2447 | goto Done;\r | |
2448 | }\r | |
2449 | \r | |
8d3a5c82 | 2450 | //\r |
c6492839 | 2451 | // Make sure we are trying to create a new variable.\r |
fa0737a8 | 2452 | // Setting a data variable with zero DataSize or no access attributes means to delete it.\r |
8d3a5c82 | 2453 | //\r |
c6492839 | 2454 | if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r |
2455 | Status = EFI_NOT_FOUND;\r | |
2456 | goto Done;\r | |
2457 | }\r | |
fa0737a8 | 2458 | \r |
8d3a5c82 | 2459 | //\r |
8a2d4996 | 2460 | // Only variable have NV|RT attribute can be created in Runtime.\r |
c6492839 | 2461 | //\r |
8a2d4996 | 2462 | if (AtRuntime () &&\r |
45f6c85b | 2463 | (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r |
c6492839 | 2464 | Status = EFI_INVALID_PARAMETER;\r |
2465 | goto Done;\r | |
fa0737a8 | 2466 | }\r |
c6492839 | 2467 | }\r |
2468 | \r | |
2469 | //\r | |
2470 | // Function part - create a new variable and copy the data.\r | |
2471 | // Both update a variable and create a variable will come here.\r | |
c6492839 | 2472 | //\r |
c6492839 | 2473 | NextVariable->StartId = VARIABLE_DATA;\r |
c6492839 | 2474 | //\r |
2475 | // NextVariable->State = VAR_ADDED;\r | |
2476 | //\r | |
8a2d4996 | 2477 | NextVariable->Reserved = 0;\r |
fa0737a8 SZ |
2478 | if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r |
2479 | AuthVariable = (AUTHENTICATED_VARIABLE_HEADER *) NextVariable;\r | |
2480 | AuthVariable->PubKeyIndex = KeyIndex;\r | |
2481 | AuthVariable->MonotonicCount = MonotonicCount;\r | |
2482 | ZeroMem (&AuthVariable->TimeStamp, sizeof (EFI_TIME));\r | |
2483 | \r | |
2484 | if (((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) &&\r | |
2485 | (TimeStamp != NULL)) {\r | |
2486 | if ((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) {\r | |
2487 | CopyMem (&AuthVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r | |
2488 | } else {\r | |
2489 | //\r | |
2490 | // In the case when the EFI_VARIABLE_APPEND_WRITE attribute is set, only\r | |
2491 | // when the new TimeStamp value is later than the current timestamp associated\r | |
2492 | // with the variable, we need associate the new timestamp with the updated value.\r | |
2493 | //\r | |
2494 | if (Variable->CurrPtr != NULL) {\r | |
9b18845a | 2495 | if (VariableCompareTimeStampInternal (&(((AUTHENTICATED_VARIABLE_HEADER *) CacheVariable->CurrPtr)->TimeStamp), TimeStamp)) {\r |
fa0737a8 SZ |
2496 | CopyMem (&AuthVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r |
2497 | }\r | |
2498 | }\r | |
2499 | }\r | |
2500 | }\r | |
2501 | }\r | |
2502 | \r | |
2503 | //\r | |
2504 | // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned\r | |
2505 | // Attributes bitmask parameter of a GetVariable() call.\r | |
2506 | //\r | |
2507 | NextVariable->Attributes = Attributes & (~EFI_VARIABLE_APPEND_WRITE);\r | |
2508 | \r | |
2509 | VarNameOffset = GetVariableHeaderSize ();\r | |
8a2d4996 | 2510 | VarNameSize = StrSize (VariableName);\r |
c6492839 | 2511 | CopyMem (\r |
2512 | (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r | |
2513 | VariableName,\r | |
2514 | VarNameSize\r | |
2515 | );\r | |
2516 | VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r | |
fa0737a8 SZ |
2517 | \r |
2518 | //\r | |
2519 | // If DataReady is TRUE, it means the variable data has been saved into\r | |
2520 | // NextVariable during EFI_VARIABLE_APPEND_WRITE operation preparation.\r | |
2521 | //\r | |
2522 | if (!DataReady) {\r | |
2523 | CopyMem (\r | |
2524 | (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r | |
2525 | Data,\r | |
2526 | DataSize\r | |
2527 | );\r | |
2528 | }\r | |
2529 | \r | |
2530 | CopyMem (GetVendorGuidPtr (NextVariable), VendorGuid, sizeof (EFI_GUID));\r | |
c6492839 | 2531 | //\r |
2532 | // There will be pad bytes after Data, the NextVariable->NameSize and\r | |
2533 | // NextVariable->DataSize should not include pad size so that variable\r | |
8a2d4996 | 2534 | // service can get actual size in GetVariable.\r |
c6492839 | 2535 | //\r |
fa0737a8 SZ |
2536 | SetNameSizeOfVariable (NextVariable, VarNameSize);\r |
2537 | SetDataSizeOfVariable (NextVariable, DataSize);\r | |
c6492839 | 2538 | \r |
2539 | //\r | |
2540 | // The actual size of the variable that stores in storage should\r | |
2541 | // include pad size.\r | |
2542 | //\r | |
2543 | VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r | |
45f6c85b | 2544 | if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r |
8d3a5c82 | 2545 | //\r |
8a2d4996 | 2546 | // Create a nonvolatile variable.\r |
8d3a5c82 | 2547 | //\r |
fd51bf70 | 2548 | Volatile = FALSE;\r |
4edb1866 SZ |
2549 | \r |
2550 | IsCommonVariable = FALSE;\r | |
2551 | IsCommonUserVariable = FALSE;\r | |
2552 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) {\r | |
2553 | IsCommonVariable = TRUE;\r | |
2554 | IsCommonUserVariable = IsUserVariable (NextVariable);\r | |
2555 | }\r | |
2556 | if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)\r | |
188e4e84 | 2557 | && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r |
4edb1866 SZ |
2558 | || (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace))\r |
2559 | || (IsCommonVariable && AtRuntime () && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace))\r | |
2560 | || (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace))) {\r | |
8a2d4996 | 2561 | if (AtRuntime ()) {\r |
4edb1866 SZ |
2562 | if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r |
2563 | RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r | |
2564 | }\r | |
2565 | if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace)) {\r | |
2566 | RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r | |
2567 | }\r | |
c6492839 | 2568 | Status = EFI_OUT_OF_RESOURCES;\r |
2569 | goto Done;\r | |
2570 | }\r | |
2571 | //\r | |
7baf3c69 | 2572 | // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r |
c6492839 | 2573 | //\r |
fa0737a8 SZ |
2574 | Status = Reclaim (\r |
2575 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r | |
2576 | &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r | |
2577 | FALSE,\r | |
2578 | Variable,\r | |
2579 | NextVariable,\r | |
2580 | HEADER_ALIGN (VarSize)\r | |
2581 | );\r | |
7baf3c69 SZ |
2582 | if (!EFI_ERROR (Status)) {\r |
2583 | //\r | |
2584 | // The new variable has been integrated successfully during reclaiming.\r | |
2585 | //\r | |
2586 | if (Variable->CurrPtr != NULL) {\r | |
2587 | CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r | |
2588 | CacheVariable->InDeletedTransitionPtr = NULL;\r | |
2589 | }\r | |
2590 | UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);\r | |
2591 | FlushHobVariableToFlash (VariableName, VendorGuid);\r | |
4edb1866 SZ |
2592 | } else {\r |
2593 | if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r | |
2594 | RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r | |
2595 | }\r | |
2596 | if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace)) {\r | |
2597 | RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r | |
2598 | }\r | |
23b06935 | 2599 | }\r |
7baf3c69 | 2600 | goto Done;\r |
8d3a5c82 | 2601 | }\r |
2602 | //\r | |
8a2d4996 | 2603 | // Four steps\r |
c6492839 | 2604 | // 1. Write variable header\r |
fa0737a8 | 2605 | // 2. Set variable state to header valid\r |
130e2569 | 2606 | // 3. Write variable data\r |
2607 | // 4. Set variable state to valid\r | |
8d3a5c82 | 2608 | //\r |
8d3a5c82 | 2609 | //\r |
c6492839 | 2610 | // Step 1:\r |
8d3a5c82 | 2611 | //\r |
8a2d4996 | 2612 | CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r |
c6492839 | 2613 | Status = UpdateVariableStore (\r |
052ad7e1 | 2614 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 2615 | FALSE,\r |
2616 | TRUE,\r | |
8a9e0b72 | 2617 | Fvb,\r |
72399dae | 2618 | mVariableModuleGlobal->NonVolatileLastVariableOffset,\r |
fa0737a8 | 2619 | (UINT32) GetVariableHeaderSize (),\r |
c6492839 | 2620 | (UINT8 *) NextVariable\r |
2621 | );\r | |
2622 | \r | |
2623 | if (EFI_ERROR (Status)) {\r | |
2624 | goto Done;\r | |
2625 | }\r | |
130e2569 | 2626 | \r |
8d3a5c82 | 2627 | //\r |
c6492839 | 2628 | // Step 2:\r |
8d3a5c82 | 2629 | //\r |
130e2569 | 2630 | NextVariable->State = VAR_HEADER_VALID_ONLY;\r |
2631 | Status = UpdateVariableStore (\r | |
2632 | &mVariableModuleGlobal->VariableGlobal,\r | |
2633 | FALSE,\r | |
2634 | TRUE,\r | |
8a9e0b72 | 2635 | Fvb,\r |
8a2d4996 | 2636 | mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r |
2637 | sizeof (UINT8),\r | |
2638 | &NextVariable->State\r | |
130e2569 | 2639 | );\r |
2640 | \r | |
2641 | if (EFI_ERROR (Status)) {\r | |
2642 | goto Done;\r | |
2643 | }\r | |
2644 | //\r | |
2645 | // Step 3:\r | |
2646 | //\r | |
c6492839 | 2647 | Status = UpdateVariableStore (\r |
052ad7e1 | 2648 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 2649 | FALSE,\r |
2650 | TRUE,\r | |
8a9e0b72 | 2651 | Fvb,\r |
fa0737a8 SZ |
2652 | mVariableModuleGlobal->NonVolatileLastVariableOffset + GetVariableHeaderSize (),\r |
2653 | (UINT32) (VarSize - GetVariableHeaderSize ()),\r | |
2654 | (UINT8 *) NextVariable + GetVariableHeaderSize ()\r | |
c6492839 | 2655 | );\r |
2656 | \r | |
2657 | if (EFI_ERROR (Status)) {\r | |
2658 | goto Done;\r | |
2659 | }\r | |
8d3a5c82 | 2660 | //\r |
130e2569 | 2661 | // Step 4:\r |
8d3a5c82 | 2662 | //\r |
c6492839 | 2663 | NextVariable->State = VAR_ADDED;\r |
2664 | Status = UpdateVariableStore (\r | |
052ad7e1 | 2665 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 2666 | FALSE,\r |
2667 | TRUE,\r | |
8a9e0b72 | 2668 | Fvb,\r |
8a2d4996 | 2669 | mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r |
2670 | sizeof (UINT8),\r | |
2671 | &NextVariable->State\r | |
c6492839 | 2672 | );\r |
2673 | \r | |
2674 | if (EFI_ERROR (Status)) {\r | |
2675 | goto Done;\r | |
2676 | }\r | |
8d3a5c82 | 2677 | \r |
72399dae | 2678 | mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r |
8d3a5c82 | 2679 | \r |
2fcdca1d | 2680 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r |
2681 | mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r | |
2682 | } else {\r | |
2683 | mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r | |
4edb1866 SZ |
2684 | if (IsCommonUserVariable) {\r |
2685 | mVariableModuleGlobal->CommonUserVariableTotalSize += HEADER_ALIGN (VarSize);\r | |
2686 | }\r | |
2fcdca1d | 2687 | }\r |
8a2d4996 | 2688 | //\r |
2689 | // update the memory copy of Flash region.\r | |
2690 | //\r | |
2691 | CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);\r | |
c6492839 | 2692 | } else {\r |
2693 | //\r | |
8a2d4996 | 2694 | // Create a volatile variable.\r |
fa0737a8 | 2695 | //\r |
fd51bf70 | 2696 | Volatile = TRUE;\r |
c6492839 | 2697 | \r |
72399dae | 2698 | if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r |
052ad7e1 | 2699 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r |
8d3a5c82 | 2700 | //\r |
7baf3c69 | 2701 | // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r |
8d3a5c82 | 2702 | //\r |
fa0737a8 SZ |
2703 | Status = Reclaim (\r |
2704 | mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,\r | |
2705 | &mVariableModuleGlobal->VolatileLastVariableOffset,\r | |
2706 | TRUE,\r | |
2707 | Variable,\r | |
2708 | NextVariable,\r | |
2709 | HEADER_ALIGN (VarSize)\r | |
2710 | );\r | |
7baf3c69 SZ |
2711 | if (!EFI_ERROR (Status)) {\r |
2712 | //\r | |
2713 | // The new variable has been integrated successfully during reclaiming.\r | |
2714 | //\r | |
2715 | if (Variable->CurrPtr != NULL) {\r | |
2716 | CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r | |
2717 | CacheVariable->InDeletedTransitionPtr = NULL;\r | |
2718 | }\r | |
2719 | UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, FALSE, FALSE);\r | |
23b06935 | 2720 | }\r |
7baf3c69 | 2721 | goto Done;\r |
8d3a5c82 | 2722 | }\r |
8d3a5c82 | 2723 | \r |
c6492839 | 2724 | NextVariable->State = VAR_ADDED;\r |
2725 | Status = UpdateVariableStore (\r | |
052ad7e1 | 2726 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 2727 | TRUE,\r |
2728 | TRUE,\r | |
8a9e0b72 | 2729 | Fvb,\r |
72399dae | 2730 | mVariableModuleGlobal->VolatileLastVariableOffset,\r |
c6492839 | 2731 | (UINT32) VarSize,\r |
2732 | (UINT8 *) NextVariable\r | |
2733 | );\r | |
2734 | \r | |
2735 | if (EFI_ERROR (Status)) {\r | |
2736 | goto Done;\r | |
8d3a5c82 | 2737 | }\r |
c6492839 | 2738 | \r |
72399dae | 2739 | mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r |
c6492839 | 2740 | }\r |
72399dae | 2741 | \r |
c6492839 | 2742 | //\r |
8a2d4996 | 2743 | // Mark the old variable as deleted.\r |
c6492839 | 2744 | //\r |
23b06935 SZ |
2745 | if (!EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r |
2746 | if (Variable->InDeletedTransitionPtr != NULL) {\r | |
2747 | //\r | |
2748 | // Both ADDED and IN_DELETED_TRANSITION old variable are present,\r | |
2749 | // set IN_DELETED_TRANSITION one to DELETED state first.\r | |
2750 | //\r | |
ae658d91 | 2751 | ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r |
9b18845a | 2752 | State = CacheVariable->InDeletedTransitionPtr->State;\r |
23b06935 SZ |
2753 | State &= VAR_DELETED;\r |
2754 | Status = UpdateVariableStore (\r | |
2755 | &mVariableModuleGlobal->VariableGlobal,\r | |
2756 | Variable->Volatile,\r | |
2757 | FALSE,\r | |
2758 | Fvb,\r | |
2759 | (UINTN) &Variable->InDeletedTransitionPtr->State,\r | |
2760 | sizeof (UINT8),\r | |
2761 | &State\r | |
2762 | );\r | |
2763 | if (!EFI_ERROR (Status)) {\r | |
2764 | if (!Variable->Volatile) {\r | |
2765 | CacheVariable->InDeletedTransitionPtr->State = State;\r | |
2766 | }\r | |
2767 | } else {\r | |
2768 | goto Done;\r | |
2769 | }\r | |
2770 | }\r | |
2771 | \r | |
72399dae | 2772 | State = Variable->CurrPtr->State;\r |
c6492839 | 2773 | State &= VAR_DELETED;\r |
2774 | \r | |
2775 | Status = UpdateVariableStore (\r | |
72399dae | 2776 | &mVariableModuleGlobal->VariableGlobal,\r |
2777 | Variable->Volatile,\r | |
2778 | FALSE,\r | |
2779 | Fvb,\r | |
2780 | (UINTN) &Variable->CurrPtr->State,\r | |
2781 | sizeof (UINT8),\r | |
2782 | &State\r | |
2783 | );\r | |
fa0737a8 | 2784 | if (!EFI_ERROR (Status) && !Variable->Volatile) {\r |
8a2d4996 | 2785 | CacheVariable->CurrPtr->State = State;\r |
2786 | }\r | |
72399dae | 2787 | }\r |
2788 | \r | |
2789 | if (!EFI_ERROR (Status)) {\r | |
2790 | UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r | |
335e2681 SZ |
2791 | if (!Volatile) {\r |
2792 | FlushHobVariableToFlash (VariableName, VendorGuid);\r | |
2793 | }\r | |
72399dae | 2794 | }\r |
2795 | \r | |
2796 | Done:\r | |
2797 | return Status;\r | |
2798 | }\r | |
2799 | \r | |
2800 | /**\r | |
2801 | \r | |
2802 | This code finds variable in storage blocks (Volatile or Non-Volatile).\r | |
2803 | \r | |
18a7dbbc SZ |
2804 | Caution: This function may receive untrusted input.\r |
2805 | This function may be invoked in SMM mode, and datasize is external input.\r | |
2806 | This function will do basic validation, before parse the data.\r | |
2807 | \r | |
72399dae | 2808 | @param VariableName Name of Variable to be found.\r |
2809 | @param VendorGuid Variable vendor GUID.\r | |
2810 | @param Attributes Attribute value of the variable found.\r | |
2811 | @param DataSize Size of Data found. If size is less than the\r | |
2812 | data, this value contains the required size.\r | |
dd59d95e SZ |
2813 | @param Data The buffer to return the contents of the variable. May be NULL\r |
2814 | with a zero DataSize in order to determine the size buffer needed.\r | |
fa0737a8 | 2815 | \r |
8a2d4996 | 2816 | @return EFI_INVALID_PARAMETER Invalid parameter.\r |
2817 | @return EFI_SUCCESS Find the specified variable.\r | |
2818 | @return EFI_NOT_FOUND Not found.\r | |
2819 | @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r | |
72399dae | 2820 | \r |
2821 | **/\r | |
2822 | EFI_STATUS\r | |
2823 | EFIAPI\r | |
8a2d4996 | 2824 | VariableServiceGetVariable (\r |
72399dae | 2825 | IN CHAR16 *VariableName,\r |
2826 | IN EFI_GUID *VendorGuid,\r | |
2827 | OUT UINT32 *Attributes OPTIONAL,\r | |
2828 | IN OUT UINTN *DataSize,\r | |
dd59d95e | 2829 | OUT VOID *Data OPTIONAL\r |
72399dae | 2830 | )\r |
2831 | {\r | |
2832 | EFI_STATUS Status;\r | |
2833 | VARIABLE_POINTER_TRACK Variable;\r | |
2834 | UINTN VarDataSize;\r | |
2835 | \r | |
2836 | if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r | |
2837 | return EFI_INVALID_PARAMETER;\r | |
2838 | }\r | |
2839 | \r | |
e19eab61 SZ |
2840 | if (VariableName[0] == 0) {\r |
2841 | return EFI_NOT_FOUND;\r | |
2842 | }\r | |
2843 | \r | |
72399dae | 2844 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
fa0737a8 | 2845 | \r |
9622df63 | 2846 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r |
72399dae | 2847 | if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r |
2848 | goto Done;\r | |
2849 | }\r | |
2850 | \r | |
2851 | //\r | |
2852 | // Get data size\r | |
2853 | //\r | |
2854 | VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r | |
2855 | ASSERT (VarDataSize != 0);\r | |
2856 | \r | |
2857 | if (*DataSize >= VarDataSize) {\r | |
2858 | if (Data == NULL) {\r | |
2859 | Status = EFI_INVALID_PARAMETER;\r | |
2860 | goto Done;\r | |
33a5a666 | 2861 | }\r |
72399dae | 2862 | \r |
2863 | CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r | |
2864 | if (Attributes != NULL) {\r | |
2865 | *Attributes = Variable.CurrPtr->Attributes;\r | |
2866 | }\r | |
2867 | \r | |
2868 | *DataSize = VarDataSize;\r | |
2869 | UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r | |
fa0737a8 | 2870 | \r |
72399dae | 2871 | Status = EFI_SUCCESS;\r |
2872 | goto Done;\r | |
2873 | } else {\r | |
2874 | *DataSize = VarDataSize;\r | |
2875 | Status = EFI_BUFFER_TOO_SMALL;\r | |
2876 | goto Done;\r | |
8d3a5c82 | 2877 | }\r |
2878 | \r | |
72399dae | 2879 | Done:\r |
2880 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r | |
2881 | return Status;\r | |
2882 | }\r | |
2883 | \r | |
72399dae | 2884 | /**\r |
72399dae | 2885 | This code Finds the Next available variable.\r |
2886 | \r | |
18a7dbbc SZ |
2887 | Caution: This function may receive untrusted input.\r |
2888 | This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r | |
2889 | \r | |
fa0737a8 SZ |
2890 | @param[in] VariableName Pointer to variable name.\r |
2891 | @param[in] VendorGuid Variable Vendor Guid.\r | |
2892 | @param[out] VariablePtr Pointer to variable header address.\r | |
72399dae | 2893 | \r |
6f817f9b SZ |
2894 | @retval EFI_SUCCESS The function completed successfully.\r |
2895 | @retval EFI_NOT_FOUND The next variable was not found.\r | |
2896 | @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.\r | |
2897 | @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and\r | |
2898 | GUID of an existing variable.\r | |
72399dae | 2899 | \r |
2900 | **/\r | |
2901 | EFI_STATUS\r | |
2902 | EFIAPI\r | |
fa0737a8 SZ |
2903 | VariableServiceGetNextVariableInternal (\r |
2904 | IN CHAR16 *VariableName,\r | |
2905 | IN EFI_GUID *VendorGuid,\r | |
2906 | OUT VARIABLE_HEADER **VariablePtr\r | |
72399dae | 2907 | )\r |
2908 | {\r | |
0f7aff72 | 2909 | VARIABLE_STORE_TYPE Type;\r |
72399dae | 2910 | VARIABLE_POINTER_TRACK Variable;\r |
0f7aff72 | 2911 | VARIABLE_POINTER_TRACK VariableInHob;\r |
23b06935 | 2912 | VARIABLE_POINTER_TRACK VariablePtrTrack;\r |
72399dae | 2913 | EFI_STATUS Status;\r |
0f7aff72 | 2914 | VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r |
72399dae | 2915 | \r |
9622df63 | 2916 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r |
72399dae | 2917 | if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r |
6f817f9b SZ |
2918 | //\r |
2919 | // For VariableName is an empty string, FindVariable() will try to find and return\r | |
2920 | // the first qualified variable, and if FindVariable() returns error (EFI_NOT_FOUND)\r | |
2921 | // as no any variable is found, still go to return the error (EFI_NOT_FOUND).\r | |
2922 | //\r | |
2923 | if (VariableName[0] != 0) {\r | |
2924 | //\r | |
2925 | // For VariableName is not an empty string, and FindVariable() returns error as\r | |
2926 | // VariableName and VendorGuid are not a name and GUID of an existing variable,\r | |
2927 | // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER.\r | |
2928 | //\r | |
2929 | Status = EFI_INVALID_PARAMETER;\r | |
2930 | }\r | |
72399dae | 2931 | goto Done;\r |
2932 | }\r | |
2933 | \r | |
2934 | if (VariableName[0] != 0) {\r | |
2935 | //\r | |
8a2d4996 | 2936 | // If variable name is not NULL, get next variable.\r |
72399dae | 2937 | //\r |
2938 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
2939 | }\r | |
2940 | \r | |
0f7aff72 RN |
2941 | //\r |
2942 | // 0: Volatile, 1: HOB, 2: Non-Volatile.\r | |
2943 | // The index and attributes mapping must be kept in this order as FindVariable\r | |
2944 | // makes use of this mapping to implement search algorithm.\r | |
2945 | //\r | |
2946 | VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r | |
2947 | VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r | |
2948 | VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r | |
2949 | \r | |
72399dae | 2950 | while (TRUE) {\r |
2951 | //\r | |
0f7aff72 | 2952 | // Switch from Volatile to HOB, to Non-Volatile.\r |
72399dae | 2953 | //\r |
6ebffb67 | 2954 | while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {\r |
0f7aff72 RN |
2955 | //\r |
2956 | // Find current storage index\r | |
2957 | //\r | |
2958 | for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r | |
2959 | if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r | |
2960 | break;\r | |
2961 | }\r | |
2962 | }\r | |
2963 | ASSERT (Type < VariableStoreTypeMax);\r | |
2964 | //\r | |
2965 | // Switch to next storage\r | |
2966 | //\r | |
2967 | for (Type++; Type < VariableStoreTypeMax; Type++) {\r | |
2968 | if (VariableStoreHeader[Type] != NULL) {\r | |
2969 | break;\r | |
2970 | }\r | |
2971 | }\r | |
2972 | //\r | |
fa0737a8 | 2973 | // Capture the case that\r |
0f7aff72 RN |
2974 | // 1. current storage is the last one, or\r |
2975 | // 2. no further storage\r | |
2976 | //\r | |
2977 | if (Type == VariableStoreTypeMax) {\r | |
72399dae | 2978 | Status = EFI_NOT_FOUND;\r |
2979 | goto Done;\r | |
2980 | }\r | |
0f7aff72 RN |
2981 | Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r |
2982 | Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r | |
2983 | Variable.CurrPtr = Variable.StartPtr;\r | |
72399dae | 2984 | }\r |
0f7aff72 | 2985 | \r |
72399dae | 2986 | //\r |
2987 | // Variable is found\r | |
2988 | //\r | |
23b06935 SZ |
2989 | if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r |
2990 | if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r | |
2991 | if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r | |
2992 | //\r | |
2993 | // If it is a IN_DELETED_TRANSITION variable,\r | |
2994 | // and there is also a same ADDED one at the same time,\r | |
2995 | // don't return it.\r | |
2996 | //\r | |
2997 | VariablePtrTrack.StartPtr = Variable.StartPtr;\r | |
2998 | VariablePtrTrack.EndPtr = Variable.EndPtr;\r | |
2999 | Status = FindVariableEx (\r | |
3000 | GetVariableNamePtr (Variable.CurrPtr),\r | |
fa0737a8 | 3001 | GetVendorGuidPtr (Variable.CurrPtr),\r |
23b06935 SZ |
3002 | FALSE,\r |
3003 | &VariablePtrTrack\r | |
3004 | );\r | |
3005 | if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {\r | |
3006 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
3007 | continue;\r | |
3008 | }\r | |
3009 | }\r | |
0f7aff72 RN |
3010 | \r |
3011 | //\r | |
3012 | // Don't return NV variable when HOB overrides it\r | |
3013 | //\r | |
fa0737a8 | 3014 | if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r |
0f7aff72 RN |
3015 | (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r |
3016 | ) {\r | |
3017 | VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r | |
3018 | VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r | |
3019 | Status = FindVariableEx (\r | |
3020 | GetVariableNamePtr (Variable.CurrPtr),\r | |
fa0737a8 | 3021 | GetVendorGuidPtr (Variable.CurrPtr),\r |
9622df63 | 3022 | FALSE,\r |
0f7aff72 RN |
3023 | &VariableInHob\r |
3024 | );\r | |
3025 | if (!EFI_ERROR (Status)) {\r | |
3026 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
3027 | continue;\r | |
3028 | }\r | |
3029 | }\r | |
3030 | \r | |
fa0737a8 SZ |
3031 | *VariablePtr = Variable.CurrPtr;\r |
3032 | Status = EFI_SUCCESS;\r | |
72399dae | 3033 | goto Done;\r |
3034 | }\r | |
3035 | }\r | |
3036 | \r | |
3037 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
3038 | }\r | |
33a5a666 | 3039 | \r |
8d3a5c82 | 3040 | Done:\r |
fa0737a8 SZ |
3041 | return Status;\r |
3042 | }\r | |
3043 | \r | |
3044 | /**\r | |
3045 | \r | |
3046 | This code Finds the Next available variable.\r | |
3047 | \r | |
3048 | Caution: This function may receive untrusted input.\r | |
3049 | This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r | |
3050 | \r | |
6f817f9b SZ |
3051 | @param VariableNameSize The size of the VariableName buffer. The size must be large\r |
3052 | enough to fit input string supplied in VariableName buffer.\r | |
fa0737a8 SZ |
3053 | @param VariableName Pointer to variable name.\r |
3054 | @param VendorGuid Variable Vendor Guid.\r | |
3055 | \r | |
6f817f9b SZ |
3056 | @retval EFI_SUCCESS The function completed successfully.\r |
3057 | @retval EFI_NOT_FOUND The next variable was not found.\r | |
3058 | @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the result.\r | |
3059 | VariableNameSize has been updated with the size needed to complete the request.\r | |
3060 | @retval EFI_INVALID_PARAMETER VariableNameSize is NULL.\r | |
3061 | @retval EFI_INVALID_PARAMETER VariableName is NULL.\r | |
3062 | @retval EFI_INVALID_PARAMETER VendorGuid is NULL.\r | |
3063 | @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and\r | |
3064 | GUID of an existing variable.\r | |
3065 | @retval EFI_INVALID_PARAMETER Null-terminator is not found in the first VariableNameSize bytes of\r | |
3066 | the input VariableName buffer.\r | |
fa0737a8 SZ |
3067 | \r |
3068 | **/\r | |
3069 | EFI_STATUS\r | |
3070 | EFIAPI\r | |
3071 | VariableServiceGetNextVariableName (\r | |
3072 | IN OUT UINTN *VariableNameSize,\r | |
3073 | IN OUT CHAR16 *VariableName,\r | |
3074 | IN OUT EFI_GUID *VendorGuid\r | |
3075 | )\r | |
3076 | {\r | |
3077 | EFI_STATUS Status;\r | |
6f817f9b | 3078 | UINTN MaxLen;\r |
fa0737a8 SZ |
3079 | UINTN VarNameSize;\r |
3080 | VARIABLE_HEADER *VariablePtr;\r | |
3081 | \r | |
3082 | if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r | |
3083 | return EFI_INVALID_PARAMETER;\r | |
3084 | }\r | |
3085 | \r | |
6f817f9b SZ |
3086 | //\r |
3087 | // Calculate the possible maximum length of name string, including the Null terminator.\r | |
3088 | //\r | |
3089 | MaxLen = *VariableNameSize / sizeof (CHAR16);\r | |
3090 | if ((MaxLen == 0) || (StrnLenS (VariableName, MaxLen) == MaxLen)) {\r | |
3091 | //\r | |
3092 | // Null-terminator is not found in the first VariableNameSize bytes of the input VariableName buffer,\r | |
3093 | // follow spec to return EFI_INVALID_PARAMETER.\r | |
3094 | //\r | |
3095 | return EFI_INVALID_PARAMETER;\r | |
3096 | }\r | |
3097 | \r | |
fa0737a8 SZ |
3098 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
3099 | \r | |
3100 | Status = VariableServiceGetNextVariableInternal (VariableName, VendorGuid, &VariablePtr);\r | |
3101 | if (!EFI_ERROR (Status)) {\r | |
3102 | VarNameSize = NameSizeOfVariable (VariablePtr);\r | |
3103 | ASSERT (VarNameSize != 0);\r | |
3104 | if (VarNameSize <= *VariableNameSize) {\r | |
3105 | CopyMem (VariableName, GetVariableNamePtr (VariablePtr), VarNameSize);\r | |
3106 | CopyMem (VendorGuid, GetVendorGuidPtr (VariablePtr), sizeof (EFI_GUID));\r | |
3107 | Status = EFI_SUCCESS;\r | |
3108 | } else {\r | |
3109 | Status = EFI_BUFFER_TOO_SMALL;\r | |
3110 | }\r | |
3111 | \r | |
3112 | *VariableNameSize = VarNameSize;\r | |
3113 | }\r | |
3114 | \r | |
72399dae | 3115 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
3116 | return Status;\r | |
3117 | }\r | |
3118 | \r | |
3119 | /**\r | |
3120 | \r | |
3121 | This code sets variable in storage blocks (Volatile or Non-Volatile).\r | |
3122 | \r | |
18a7dbbc SZ |
3123 | Caution: This function may receive untrusted input.\r |
3124 | This function may be invoked in SMM mode, and datasize and data are external input.\r | |
3125 | This function will do basic validation, before parse the data.\r | |
fa0737a8 SZ |
3126 | This function will parse the authentication carefully to avoid security issues, like\r |
3127 | buffer overflow, integer overflow.\r | |
3128 | This function will check attribute carefully to avoid authentication bypass.\r | |
18a7dbbc | 3129 | \r |
8a2d4996 | 3130 | @param VariableName Name of Variable to be found.\r |
3131 | @param VendorGuid Variable vendor GUID.\r | |
72399dae | 3132 | @param Attributes Attribute value of the variable found\r |
3133 | @param DataSize Size of Data found. If size is less than the\r | |
3134 | data, this value contains the required size.\r | |
8a2d4996 | 3135 | @param Data Data pointer.\r |
72399dae | 3136 | \r |
8a2d4996 | 3137 | @return EFI_INVALID_PARAMETER Invalid parameter.\r |
3138 | @return EFI_SUCCESS Set successfully.\r | |
3139 | @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r | |
3140 | @return EFI_NOT_FOUND Not found.\r | |
3141 | @return EFI_WRITE_PROTECTED Variable is read-only.\r | |
72399dae | 3142 | \r |
3143 | **/\r | |
3144 | EFI_STATUS\r | |
3145 | EFIAPI\r | |
8a2d4996 | 3146 | VariableServiceSetVariable (\r |
72399dae | 3147 | IN CHAR16 *VariableName,\r |
3148 | IN EFI_GUID *VendorGuid,\r | |
3149 | IN UINT32 Attributes,\r | |
3150 | IN UINTN DataSize,\r | |
3151 | IN VOID *Data\r | |
3152 | )\r | |
3153 | {\r | |
3154 | VARIABLE_POINTER_TRACK Variable;\r | |
3155 | EFI_STATUS Status;\r | |
3156 | VARIABLE_HEADER *NextVariable;\r | |
3157 | EFI_PHYSICAL_ADDRESS Point;\r | |
fa0737a8 | 3158 | UINTN PayloadSize;\r |
72399dae | 3159 | \r |
3160 | //\r | |
8a2d4996 | 3161 | // Check input parameters.\r |
72399dae | 3162 | //\r |
3163 | if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r | |
3164 | return EFI_INVALID_PARAMETER;\r | |
fa0737a8 | 3165 | }\r |
48a0e6bf | 3166 | \r |
3167 | if (DataSize != 0 && Data == NULL) {\r | |
3168 | return EFI_INVALID_PARAMETER;\r | |
3169 | }\r | |
3170 | \r | |
8e38f18e | 3171 | //\r |
fa0737a8 | 3172 | // Check for reserverd bit in variable attribute.\r |
8e38f18e | 3173 | //\r |
fa0737a8 | 3174 | if ((Attributes & (~EFI_VARIABLE_ATTRIBUTES_MASK)) != 0) {\r |
8e38f18e | 3175 | return EFI_INVALID_PARAMETER;\r |
3176 | }\r | |
3177 | \r | |
72399dae | 3178 | //\r |
8a2d4996 | 3179 | // Make sure if runtime bit is set, boot service bit is set also.\r |
72399dae | 3180 | //\r |
3181 | if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r | |
3182 | return EFI_INVALID_PARAMETER;\r | |
fa0737a8 SZ |
3183 | } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r |
3184 | if (!mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r | |
3185 | //\r | |
3186 | // Not support authenticated variable write.\r | |
3187 | //\r | |
3188 | return EFI_INVALID_PARAMETER;\r | |
3189 | }\r | |
3190 | } else if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r | |
3191 | if (PcdGet32 (PcdHwErrStorageSize) == 0) {\r | |
3192 | //\r | |
3193 | // Not support harware error record variable variable.\r | |
3194 | //\r | |
3195 | return EFI_INVALID_PARAMETER;\r | |
3196 | }\r | |
3197 | }\r | |
3198 | \r | |
3199 | //\r | |
3200 | // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute\r | |
3201 | // cannot be set both.\r | |
3202 | //\r | |
3203 | if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r | |
3204 | && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {\r | |
3205 | return EFI_INVALID_PARAMETER;\r | |
72399dae | 3206 | }\r |
3207 | \r | |
fa0737a8 SZ |
3208 | if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {\r |
3209 | if (DataSize < AUTHINFO_SIZE) {\r | |
3210 | //\r | |
3211 | // Try to write Authenticated Variable without AuthInfo.\r | |
3212 | //\r | |
3213 | return EFI_SECURITY_VIOLATION;\r | |
3214 | }\r | |
3215 | PayloadSize = DataSize - AUTHINFO_SIZE;\r | |
3216 | } else if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {\r | |
3217 | //\r | |
3218 | // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.\r | |
3219 | //\r | |
3220 | if (DataSize < OFFSET_OF_AUTHINFO2_CERT_DATA ||\r | |
3221 | ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength > DataSize - (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2, AuthInfo)) ||\r | |
3222 | ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength < OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r | |
3223 | return EFI_SECURITY_VIOLATION;\r | |
3224 | }\r | |
3225 | PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r | |
3226 | } else {\r | |
3227 | PayloadSize = DataSize;\r | |
3228 | }\r | |
3229 | \r | |
3230 | if ((UINTN)(~0) - PayloadSize < StrSize(VariableName)){\r | |
3231 | //\r | |
3232 | // Prevent whole variable size overflow\r | |
56251c66 | 3233 | //\r |
56251c66 | 3234 | return EFI_INVALID_PARAMETER;\r |
3235 | }\r | |
3236 | \r | |
72399dae | 3237 | //\r |
3238 | // The size of the VariableName, including the Unicode Null in bytes plus\r | |
188e4e84 | 3239 | // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r |
fa0737a8 | 3240 | // bytes for HwErrRec#### variable.\r |
72399dae | 3241 | //\r |
3242 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
fa0737a8 | 3243 | if (StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize) - GetVariableHeaderSize ()) {\r |
72399dae | 3244 | return EFI_INVALID_PARAMETER;\r |
3245 | }\r | |
72399dae | 3246 | } else {\r |
3247 | //\r | |
3248 | // The size of the VariableName, including the Unicode Null in bytes plus\r | |
fa0737a8 | 3249 | // the DataSize is limited to maximum size of Max(Auth)VariableSize bytes.\r |
72399dae | 3250 | //\r |
fa0737a8 SZ |
3251 | if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r |
3252 | if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ()) {\r | |
3253 | return EFI_INVALID_PARAMETER;\r | |
3254 | }\r | |
3255 | } else {\r | |
3256 | if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize ()) {\r | |
3257 | return EFI_INVALID_PARAMETER;\r | |
3258 | }\r | |
efb01a10 | 3259 | }\r |
6675a21f SZ |
3260 | }\r |
3261 | \r | |
2f6aa774 JY |
3262 | //\r |
3263 | // Special Handling for MOR Lock variable.\r | |
3264 | //\r | |
3265 | Status = SetVariableCheckHandlerMor (VariableName, VendorGuid, Attributes, PayloadSize, (VOID *) ((UINTN) Data + DataSize - PayloadSize));\r | |
3266 | if (Status == EFI_ALREADY_STARTED) {\r | |
3267 | //\r | |
3268 | // EFI_ALREADY_STARTED means the SetVariable() action is handled inside of SetVariableCheckHandlerMor().\r | |
3269 | // Variable driver can just return SUCCESS.\r | |
3270 | //\r | |
3271 | return EFI_SUCCESS;\r | |
3272 | }\r | |
3273 | if (EFI_ERROR (Status)) {\r | |
3274 | return Status;\r | |
3275 | }\r | |
3276 | \r | |
8021f4c7 | 3277 | Status = VarCheckLibSetVariableCheck (VariableName, VendorGuid, Attributes, PayloadSize, (VOID *) ((UINTN) Data + DataSize - PayloadSize), mRequestSource);\r |
fa0737a8 SZ |
3278 | if (EFI_ERROR (Status)) {\r |
3279 | return Status;\r | |
3280 | }\r | |
3281 | \r | |
72399dae | 3282 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
3283 | \r | |
3284 | //\r | |
8a2d4996 | 3285 | // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r |
72399dae | 3286 | //\r |
3287 | if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r | |
8a2d4996 | 3288 | Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r |
72399dae | 3289 | //\r |
8a2d4996 | 3290 | // Parse non-volatile variable data and get last variable offset.\r |
72399dae | 3291 | //\r |
3292 | NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r | |
6ebffb67 | 3293 | while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))) {\r |
72399dae | 3294 | NextVariable = GetNextVariablePtr (NextVariable);\r |
3295 | }\r | |
3296 | mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r | |
3297 | }\r | |
3298 | \r | |
3299 | //\r | |
8a2d4996 | 3300 | // Check whether the input variable is already existed.\r |
72399dae | 3301 | //\r |
9622df63 SZ |
3302 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r |
3303 | if (!EFI_ERROR (Status)) {\r | |
3304 | if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r | |
ff843847 RN |
3305 | Status = EFI_WRITE_PROTECTED;\r |
3306 | goto Done;\r | |
9622df63 | 3307 | }\r |
fa0737a8 | 3308 | if (Attributes != 0 && (Attributes & (~EFI_VARIABLE_APPEND_WRITE)) != Variable.CurrPtr->Attributes) {\r |
6e67fec0 SZ |
3309 | //\r |
3310 | // If a preexisting variable is rewritten with different attributes, SetVariable() shall not\r | |
3311 | // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:\r | |
3312 | // 1. No access attributes specified\r | |
3313 | // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE\r | |
3314 | //\r | |
3315 | Status = EFI_INVALID_PARAMETER;\r | |
4edb1866 | 3316 | DEBUG ((EFI_D_INFO, "[Variable]: Rewritten a preexisting variable(0x%08x) with different attributes(0x%08x) - %g:%s\n", Variable.CurrPtr->Attributes, Attributes, VendorGuid, VariableName));\r |
6e67fec0 SZ |
3317 | goto Done;\r |
3318 | }\r | |
9622df63 | 3319 | }\r |
72399dae | 3320 | \r |
b2bd493e | 3321 | if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate)) {\r |
9bc5dabb | 3322 | //\r |
b2bd493e | 3323 | // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r |
9bc5dabb | 3324 | //\r |
b2bd493e SZ |
3325 | Status = AutoUpdateLangVariable (VariableName, Data, DataSize);\r |
3326 | if (EFI_ERROR (Status)) {\r | |
3327 | //\r | |
3328 | // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.\r | |
3329 | //\r | |
3330 | goto Done;\r | |
3331 | }\r | |
9bc5dabb | 3332 | }\r |
72399dae | 3333 | \r |
fa0737a8 SZ |
3334 | if (mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r |
3335 | Status = AuthVariableLibProcessVariable (VariableName, VendorGuid, Data, DataSize, Attributes);\r | |
3336 | } else {\r | |
3337 | Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, 0, 0, &Variable, NULL);\r | |
3338 | }\r | |
72399dae | 3339 | \r |
ff843847 | 3340 | Done:\r |
fdb7765f | 3341 | InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r |
052ad7e1 | 3342 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
fdb7765f | 3343 | \r |
fa0737a8 SZ |
3344 | if (!AtRuntime ()) {\r |
3345 | if (!EFI_ERROR (Status)) {\r | |
3346 | SecureBootHook (\r | |
3347 | VariableName,\r | |
3348 | VendorGuid\r | |
3349 | );\r | |
3350 | }\r | |
3351 | }\r | |
3352 | \r | |
8d3a5c82 | 3353 | return Status;\r |
3354 | }\r | |
3355 | \r | |
7c80e839 | 3356 | /**\r |
8d3a5c82 | 3357 | \r |
3358 | This code returns information about the EFI variables.\r | |
3359 | \r | |
18a7dbbc SZ |
3360 | Caution: This function may receive untrusted input.\r |
3361 | This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r | |
3362 | \r | |
7c80e839 | 3363 | @param Attributes Attributes bitmask to specify the type of variables\r |
3364 | on which to return information.\r | |
3365 | @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r | |
3366 | for the EFI variables associated with the attributes specified.\r | |
3367 | @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r | |
3368 | for EFI variables associated with the attributes specified.\r | |
3369 | @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r | |
3370 | associated with the attributes specified.\r | |
8d3a5c82 | 3371 | \r |
7c80e839 | 3372 | @return EFI_SUCCESS Query successfully.\r |
8d3a5c82 | 3373 | \r |
7c80e839 | 3374 | **/\r |
052ad7e1 A |
3375 | EFI_STATUS\r |
3376 | EFIAPI\r | |
b2bd493e | 3377 | VariableServiceQueryVariableInfoInternal (\r |
052ad7e1 A |
3378 | IN UINT32 Attributes,\r |
3379 | OUT UINT64 *MaximumVariableStorageSize,\r | |
3380 | OUT UINT64 *RemainingVariableStorageSize,\r | |
3381 | OUT UINT64 *MaximumVariableSize\r | |
3382 | )\r | |
8d3a5c82 | 3383 | {\r |
3384 | VARIABLE_HEADER *Variable;\r | |
3385 | VARIABLE_HEADER *NextVariable;\r | |
3386 | UINT64 VariableSize;\r | |
3387 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
2fcdca1d | 3388 | UINT64 CommonVariableTotalSize;\r |
3389 | UINT64 HwErrVariableTotalSize;\r | |
b2bd493e SZ |
3390 | EFI_STATUS Status;\r |
3391 | VARIABLE_POINTER_TRACK VariablePtrTrack;\r | |
2fcdca1d | 3392 | \r |
3393 | CommonVariableTotalSize = 0;\r | |
3394 | HwErrVariableTotalSize = 0;\r | |
8d3a5c82 | 3395 | \r |
8d3a5c82 | 3396 | if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r |
3397 | //\r | |
3398 | // Query is Volatile related.\r | |
3399 | //\r | |
052ad7e1 | 3400 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r |
8d3a5c82 | 3401 | } else {\r |
3402 | //\r | |
3403 | // Query is Non-Volatile related.\r | |
3404 | //\r | |
8a2d4996 | 3405 | VariableStoreHeader = mNvVariableCache;\r |
8d3a5c82 | 3406 | }\r |
3407 | \r | |
3408 | //\r | |
3409 | // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r | |
3410 | // with the storage size (excluding the storage header size).\r | |
3411 | //\r | |
3412 | *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r | |
c6492839 | 3413 | \r |
3414 | //\r | |
3415 | // Harware error record variable needs larger size.\r | |
3416 | //\r | |
2fcdca1d | 3417 | if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r |
188e4e84 | 3418 | *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r |
fa0737a8 | 3419 | *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - GetVariableHeaderSize ();\r |
2fcdca1d | 3420 | } else {\r |
3421 | if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r | |
4edb1866 SZ |
3422 | if (AtRuntime ()) {\r |
3423 | *MaximumVariableStorageSize = mVariableModuleGlobal->CommonRuntimeVariableSpace;\r | |
3424 | } else {\r | |
3425 | *MaximumVariableStorageSize = mVariableModuleGlobal->CommonVariableSpace;\r | |
3426 | }\r | |
2fcdca1d | 3427 | }\r |
3428 | \r | |
3429 | //\r | |
fa0737a8 | 3430 | // Let *MaximumVariableSize be Max(Auth)VariableSize with the exception of the variable header size.\r |
2fcdca1d | 3431 | //\r |
fa0737a8 SZ |
3432 | if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r |
3433 | *MaximumVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ();\r | |
3434 | } else {\r | |
3435 | *MaximumVariableSize = mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize ();\r | |
3436 | }\r | |
c6492839 | 3437 | }\r |
8d3a5c82 | 3438 | \r |
3439 | //\r | |
3440 | // Point to the starting address of the variables.\r | |
3441 | //\r | |
9cad030b | 3442 | Variable = GetStartPointer (VariableStoreHeader);\r |
8d3a5c82 | 3443 | \r |
3444 | //\r | |
3445 | // Now walk through the related variable store.\r | |
3446 | //\r | |
6ebffb67 | 3447 | while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r |
8d3a5c82 | 3448 | NextVariable = GetNextVariablePtr (Variable);\r |
3449 | VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r | |
3450 | \r | |
8a2d4996 | 3451 | if (AtRuntime ()) {\r |
8d3a5c82 | 3452 | //\r |
8a2d4996 | 3453 | // We don't take the state of the variables in mind\r |
8d3a5c82 | 3454 | // when calculating RemainingVariableStorageSize,\r |
3455 | // since the space occupied by variables not marked with\r | |
3456 | // VAR_ADDED is not allowed to be reclaimed in Runtime.\r | |
3457 | //\r | |
3b425367 | 3458 | if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r |
2fcdca1d | 3459 | HwErrVariableTotalSize += VariableSize;\r |
3460 | } else {\r | |
3461 | CommonVariableTotalSize += VariableSize;\r | |
3462 | }\r | |
8d3a5c82 | 3463 | } else {\r |
3464 | //\r | |
8a2d4996 | 3465 | // Only care about Variables with State VAR_ADDED, because\r |
8d3a5c82 | 3466 | // the space not marked as VAR_ADDED is reclaimable now.\r |
3467 | //\r | |
3468 | if (Variable->State == VAR_ADDED) {\r | |
3b425367 | 3469 | if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r |
2fcdca1d | 3470 | HwErrVariableTotalSize += VariableSize;\r |
3471 | } else {\r | |
3472 | CommonVariableTotalSize += VariableSize;\r | |
3473 | }\r | |
b2bd493e SZ |
3474 | } else if (Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r |
3475 | //\r | |
3476 | // If it is a IN_DELETED_TRANSITION variable,\r | |
3477 | // and there is not also a same ADDED one at the same time,\r | |
3478 | // this IN_DELETED_TRANSITION variable is valid.\r | |
3479 | //\r | |
3480 | VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r | |
3481 | VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r | |
3482 | Status = FindVariableEx (\r | |
3483 | GetVariableNamePtr (Variable),\r | |
fa0737a8 | 3484 | GetVendorGuidPtr (Variable),\r |
b2bd493e SZ |
3485 | FALSE,\r |
3486 | &VariablePtrTrack\r | |
3487 | );\r | |
3488 | if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State != VAR_ADDED) {\r | |
3489 | if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
3490 | HwErrVariableTotalSize += VariableSize;\r | |
3491 | } else {\r | |
3492 | CommonVariableTotalSize += VariableSize;\r | |
3493 | }\r | |
3494 | }\r | |
8d3a5c82 | 3495 | }\r |
3496 | }\r | |
3497 | \r | |
3498 | //\r | |
8a2d4996 | 3499 | // Go to the next one.\r |
8d3a5c82 | 3500 | //\r |
3501 | Variable = NextVariable;\r | |
3502 | }\r | |
3503 | \r | |
2fcdca1d | 3504 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r |
3505 | *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r | |
4edb1866 SZ |
3506 | } else {\r |
3507 | if (*MaximumVariableStorageSize < CommonVariableTotalSize) {\r | |
3508 | *RemainingVariableStorageSize = 0;\r | |
3509 | } else {\r | |
3510 | *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r | |
3511 | }\r | |
2fcdca1d | 3512 | }\r |
3513 | \r | |
fa0737a8 | 3514 | if (*RemainingVariableStorageSize < GetVariableHeaderSize ()) {\r |
c6492839 | 3515 | *MaximumVariableSize = 0;\r |
fa0737a8 SZ |
3516 | } else if ((*RemainingVariableStorageSize - GetVariableHeaderSize ()) < *MaximumVariableSize) {\r |
3517 | *MaximumVariableSize = *RemainingVariableStorageSize - GetVariableHeaderSize ();\r | |
c6492839 | 3518 | }\r |
3519 | \r | |
8d3a5c82 | 3520 | return EFI_SUCCESS;\r |
3521 | }\r | |
3522 | \r | |
b2bd493e SZ |
3523 | /**\r |
3524 | \r | |
3525 | This code returns information about the EFI variables.\r | |
3526 | \r | |
18a7dbbc SZ |
3527 | Caution: This function may receive untrusted input.\r |
3528 | This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r | |
3529 | \r | |
b2bd493e SZ |
3530 | @param Attributes Attributes bitmask to specify the type of variables\r |
3531 | on which to return information.\r | |
3532 | @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r | |
3533 | for the EFI variables associated with the attributes specified.\r | |
3534 | @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r | |
3535 | for EFI variables associated with the attributes specified.\r | |
3536 | @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r | |
3537 | associated with the attributes specified.\r | |
3538 | \r | |
3539 | @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r | |
3540 | @return EFI_SUCCESS Query successfully.\r | |
3541 | @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r | |
3542 | \r | |
3543 | **/\r | |
3544 | EFI_STATUS\r | |
3545 | EFIAPI\r | |
3546 | VariableServiceQueryVariableInfo (\r | |
3547 | IN UINT32 Attributes,\r | |
3548 | OUT UINT64 *MaximumVariableStorageSize,\r | |
3549 | OUT UINT64 *RemainingVariableStorageSize,\r | |
3550 | OUT UINT64 *MaximumVariableSize\r | |
3551 | )\r | |
3552 | {\r | |
3553 | EFI_STATUS Status;\r | |
3554 | \r | |
3555 | if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r | |
3556 | return EFI_INVALID_PARAMETER;\r | |
3557 | }\r | |
3558 | \r | |
fa0737a8 | 3559 | if ((Attributes & EFI_VARIABLE_ATTRIBUTES_MASK) == 0) {\r |
b2bd493e SZ |
3560 | //\r |
3561 | // Make sure the Attributes combination is supported by the platform.\r | |
3562 | //\r | |
3563 | return EFI_UNSUPPORTED;\r | |
3564 | } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r | |
3565 | //\r | |
3566 | // Make sure if runtime bit is set, boot service bit is set also.\r | |
3567 | //\r | |
3568 | return EFI_INVALID_PARAMETER;\r | |
3569 | } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r | |
3570 | //\r | |
3571 | // Make sure RT Attribute is set if we are in Runtime phase.\r | |
3572 | //\r | |
3573 | return EFI_INVALID_PARAMETER;\r | |
3574 | } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
3575 | //\r | |
3576 | // Make sure Hw Attribute is set with NV.\r | |
3577 | //\r | |
3578 | return EFI_INVALID_PARAMETER;\r | |
fa0737a8 SZ |
3579 | } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r |
3580 | if (!mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r | |
3581 | //\r | |
3582 | // Not support authenticated variable write.\r | |
3583 | //\r | |
3584 | return EFI_UNSUPPORTED;\r | |
3585 | }\r | |
3586 | } else if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r | |
3587 | if (PcdGet32 (PcdHwErrStorageSize) == 0) {\r | |
3588 | //\r | |
3589 | // Not support harware error record variable variable.\r | |
3590 | //\r | |
3591 | return EFI_UNSUPPORTED;\r | |
3592 | }\r | |
b2bd493e SZ |
3593 | }\r |
3594 | \r | |
3595 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r | |
3596 | \r | |
3597 | Status = VariableServiceQueryVariableInfoInternal (\r | |
3598 | Attributes,\r | |
3599 | MaximumVariableStorageSize,\r | |
3600 | RemainingVariableStorageSize,\r | |
3601 | MaximumVariableSize\r | |
3602 | );\r | |
3603 | \r | |
3604 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r | |
3605 | return Status;\r | |
3606 | }\r | |
7c80e839 | 3607 | \r |
3608 | /**\r | |
8a2d4996 | 3609 | This function reclaims variable storage if free size is below the threshold.\r |
18a7dbbc SZ |
3610 | \r |
3611 | Caution: This function may be invoked at SMM mode.\r | |
3612 | Care must be taken to make sure not security issue.\r | |
3613 | \r | |
7c80e839 | 3614 | **/\r |
7800593d | 3615 | VOID\r |
7800593d | 3616 | ReclaimForOS(\r |
8a2d4996 | 3617 | VOID\r |
7800593d LG |
3618 | )\r |
3619 | {\r | |
9948c0b0 | 3620 | EFI_STATUS Status;\r |
4edb1866 | 3621 | UINTN RemainingCommonRuntimeVariableSpace;\r |
2fcdca1d | 3622 | UINTN RemainingHwErrVariableSpace;\r |
0fb5e515 SZ |
3623 | STATIC BOOLEAN Reclaimed;\r |
3624 | \r | |
3625 | //\r | |
3626 | // This function will be called only once at EndOfDxe or ReadyToBoot event.\r | |
3627 | //\r | |
3628 | if (Reclaimed) {\r | |
3629 | return;\r | |
3630 | }\r | |
3631 | Reclaimed = TRUE;\r | |
7800593d | 3632 | \r |
4edb1866 | 3633 | Status = EFI_SUCCESS;\r |
7800593d | 3634 | \r |
4edb1866 SZ |
3635 | if (mVariableModuleGlobal->CommonRuntimeVariableSpace < mVariableModuleGlobal->CommonVariableTotalSize) {\r |
3636 | RemainingCommonRuntimeVariableSpace = 0;\r | |
3637 | } else {\r | |
3638 | RemainingCommonRuntimeVariableSpace = mVariableModuleGlobal->CommonRuntimeVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r | |
3639 | }\r | |
2fcdca1d | 3640 | \r |
3641 | RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r | |
fa0737a8 | 3642 | \r |
7800593d | 3643 | //\r |
4edb1866 | 3644 | // Check if the free area is below a threshold.\r |
7800593d | 3645 | //\r |
fa0737a8 SZ |
3646 | if (((RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxVariableSize) ||\r |
3647 | (RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxAuthVariableSize)) ||\r | |
3648 | ((PcdGet32 (PcdHwErrStorageSize) != 0) &&\r | |
9948c0b0 | 3649 | (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r |
7800593d | 3650 | Status = Reclaim (\r |
2fcdca1d | 3651 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r |
3652 | &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r | |
3653 | FALSE,\r | |
335e2681 | 3654 | NULL,\r |
7baf3c69 SZ |
3655 | NULL,\r |
3656 | 0\r | |
2fcdca1d | 3657 | );\r |
7800593d LG |
3658 | ASSERT_EFI_ERROR (Status);\r |
3659 | }\r | |
3660 | }\r | |
3661 | \r | |
fa0737a8 SZ |
3662 | /**\r |
3663 | Get non-volatile maximum variable size.\r | |
3664 | \r | |
3665 | @return Non-volatile maximum variable size.\r | |
3666 | \r | |
3667 | **/\r | |
3668 | UINTN\r | |
3669 | GetNonVolatileMaxVariableSize (\r | |
3670 | VOID\r | |
3671 | )\r | |
3672 | {\r | |
3673 | if (PcdGet32 (PcdHwErrStorageSize) != 0) {\r | |
3674 | return MAX (MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize)),\r | |
3675 | PcdGet32 (PcdMaxHardwareErrorVariableSize));\r | |
3676 | } else {\r | |
3677 | return MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize));\r | |
3678 | }\r | |
3679 | }\r | |
3680 | \r | |
3e02ebb2 SZ |
3681 | /**\r |
3682 | Init non-volatile variable store.\r | |
3683 | \r | |
fa0737a8 SZ |
3684 | @param[out] NvFvHeader Output pointer to non-volatile FV header address.\r |
3685 | \r | |
3e02ebb2 SZ |
3686 | @retval EFI_SUCCESS Function successfully executed.\r |
3687 | @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r | |
3688 | @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.\r | |
3689 | \r | |
3690 | **/\r | |
3691 | EFI_STATUS\r | |
3692 | InitNonVolatileVariableStore (\r | |
fa0737a8 | 3693 | OUT EFI_FIRMWARE_VOLUME_HEADER **NvFvHeader\r |
3e02ebb2 SZ |
3694 | )\r |
3695 | {\r | |
3696 | EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r | |
4edb1866 | 3697 | VARIABLE_HEADER *Variable;\r |
3e02ebb2 SZ |
3698 | VARIABLE_HEADER *NextVariable;\r |
3699 | EFI_PHYSICAL_ADDRESS VariableStoreBase;\r | |
3700 | UINT64 VariableStoreLength;\r | |
3701 | UINTN VariableSize;\r | |
3702 | EFI_HOB_GUID_TYPE *GuidHob;\r | |
3703 | EFI_PHYSICAL_ADDRESS NvStorageBase;\r | |
3704 | UINT8 *NvStorageData;\r | |
3705 | UINT32 NvStorageSize;\r | |
3706 | FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *FtwLastWriteData;\r | |
3707 | UINT32 BackUpOffset;\r | |
3708 | UINT32 BackUpSize;\r | |
4edb1866 SZ |
3709 | UINT32 HwErrStorageSize;\r |
3710 | UINT32 MaxUserNvVariableSpaceSize;\r | |
3711 | UINT32 BoottimeReservedNvVariableSpaceSize;\r | |
14326ed0 SZ |
3712 | EFI_STATUS Status;\r |
3713 | VOID *FtwProtocol;\r | |
3e02ebb2 SZ |
3714 | \r |
3715 | mVariableModuleGlobal->FvbInstance = NULL;\r | |
3716 | \r | |
3e02ebb2 SZ |
3717 | //\r |
3718 | // Allocate runtime memory used for a memory copy of the FLASH region.\r | |
3719 | // Keep the memory and the FLASH in sync as updates occur.\r | |
3720 | //\r | |
3721 | NvStorageSize = PcdGet32 (PcdFlashNvStorageVariableSize);\r | |
3722 | NvStorageData = AllocateRuntimeZeroPool (NvStorageSize);\r | |
3723 | if (NvStorageData == NULL) {\r | |
3724 | return EFI_OUT_OF_RESOURCES;\r | |
3725 | }\r | |
3726 | \r | |
3727 | NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r | |
3728 | if (NvStorageBase == 0) {\r | |
3729 | NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r | |
3730 | }\r | |
3731 | //\r | |
3732 | // Copy NV storage data to the memory buffer.\r | |
3733 | //\r | |
3734 | CopyMem (NvStorageData, (UINT8 *) (UINTN) NvStorageBase, NvStorageSize);\r | |
3735 | \r | |
14326ed0 | 3736 | Status = GetFtwProtocol ((VOID **)&FtwProtocol);\r |
3e02ebb2 | 3737 | //\r |
14326ed0 | 3738 | // If FTW protocol has been installed, no need to check FTW last write data hob.\r |
3e02ebb2 | 3739 | //\r |
14326ed0 SZ |
3740 | if (EFI_ERROR (Status)) {\r |
3741 | //\r | |
3742 | // Check the FTW last write data hob.\r | |
3743 | //\r | |
3744 | GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid);\r | |
3745 | if (GuidHob != NULL) {\r | |
3746 | FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *) GET_GUID_HOB_DATA (GuidHob);\r | |
3747 | if (FtwLastWriteData->TargetAddress == NvStorageBase) {\r | |
3748 | DEBUG ((EFI_D_INFO, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN) FtwLastWriteData->SpareAddress));\r | |
3749 | //\r | |
3750 | // Copy the backed up NV storage data to the memory buffer from spare block.\r | |
3751 | //\r | |
3752 | CopyMem (NvStorageData, (UINT8 *) (UINTN) (FtwLastWriteData->SpareAddress), NvStorageSize);\r | |
3753 | } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) &&\r | |
3754 | (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) {\r | |
3755 | //\r | |
3756 | // Flash NV storage from the Offset is backed up in spare block.\r | |
3757 | //\r | |
3758 | BackUpOffset = (UINT32) (FtwLastWriteData->TargetAddress - NvStorageBase);\r | |
3759 | BackUpSize = NvStorageSize - BackUpOffset;\r | |
3760 | DEBUG ((EFI_D_INFO, "Variable: High partial NV storage from offset: %x is backed up in spare block: 0x%x\n", BackUpOffset, (UINTN) FtwLastWriteData->SpareAddress));\r | |
3761 | //\r | |
3762 | // Copy the partial backed up NV storage data to the memory buffer from spare block.\r | |
3763 | //\r | |
3764 | CopyMem (NvStorageData + BackUpOffset, (UINT8 *) (UINTN) FtwLastWriteData->SpareAddress, BackUpSize);\r | |
3765 | }\r | |
3e02ebb2 SZ |
3766 | }\r |
3767 | }\r | |
3768 | \r | |
3769 | FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) NvStorageData;\r | |
3770 | \r | |
3771 | //\r | |
3772 | // Check if the Firmware Volume is not corrupted\r | |
3773 | //\r | |
3774 | if ((FvHeader->Signature != EFI_FVH_SIGNATURE) || (!CompareGuid (&gEfiSystemNvDataFvGuid, &FvHeader->FileSystemGuid))) {\r | |
3775 | FreePool (NvStorageData);\r | |
3776 | DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r | |
3777 | return EFI_VOLUME_CORRUPTED;\r | |
3778 | }\r | |
3779 | \r | |
16f69227 HW |
3780 | VariableStoreBase = (UINTN) FvHeader + FvHeader->HeaderLength;\r |
3781 | VariableStoreLength = NvStorageSize - FvHeader->HeaderLength;\r | |
3e02ebb2 | 3782 | \r |
9b18845a | 3783 | mNvFvHeaderCache = FvHeader;\r |
3e02ebb2 SZ |
3784 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r |
3785 | mNvVariableCache = (VARIABLE_STORE_HEADER *) (UINTN) VariableStoreBase;\r | |
3786 | if (GetVariableStoreStatus (mNvVariableCache) != EfiValid) {\r | |
3787 | FreePool (NvStorageData);\r | |
9b18845a DL |
3788 | mNvFvHeaderCache = NULL;\r |
3789 | mNvVariableCache = NULL;\r | |
3e02ebb2 SZ |
3790 | DEBUG((EFI_D_ERROR, "Variable Store header is corrupted\n"));\r |
3791 | return EFI_VOLUME_CORRUPTED;\r | |
3792 | }\r | |
3793 | ASSERT(mNvVariableCache->Size == VariableStoreLength);\r | |
3794 | \r | |
4edb1866 SZ |
3795 | ASSERT (sizeof (VARIABLE_STORE_HEADER) <= VariableStoreLength);\r |
3796 | \r | |
fa0737a8 SZ |
3797 | mVariableModuleGlobal->VariableGlobal.AuthFormat = (BOOLEAN)(CompareGuid (&mNvVariableCache->Signature, &gEfiAuthenticatedVariableGuid));\r |
3798 | \r | |
4edb1866 SZ |
3799 | HwErrStorageSize = PcdGet32 (PcdHwErrStorageSize);\r |
3800 | MaxUserNvVariableSpaceSize = PcdGet32 (PcdMaxUserNvVariableSpaceSize);\r | |
3801 | BoottimeReservedNvVariableSpaceSize = PcdGet32 (PcdBoottimeReservedNvVariableSpaceSize);\r | |
3802 | \r | |
3803 | //\r | |
3804 | // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r | |
3805 | // is stored with common variable in the same NV region. So the platform integrator should\r | |
3806 | // ensure that the value of PcdHwErrStorageSize is less than the value of\r | |
fa0737a8 | 3807 | // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r |
4edb1866 SZ |
3808 | //\r |
3809 | ASSERT (HwErrStorageSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r | |
3810 | //\r | |
3811 | // Ensure that the value of PcdMaxUserNvVariableSpaceSize is less than the value of\r | |
fa0737a8 | 3812 | // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r |
4edb1866 SZ |
3813 | //\r |
3814 | ASSERT (MaxUserNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r | |
3815 | //\r | |
3816 | // Ensure that the value of PcdBoottimeReservedNvVariableSpaceSize is less than the value of\r | |
fa0737a8 | 3817 | // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r |
4edb1866 SZ |
3818 | //\r |
3819 | ASSERT (BoottimeReservedNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r | |
3820 | \r | |
3821 | mVariableModuleGlobal->CommonVariableSpace = ((UINTN) VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize);\r | |
3822 | mVariableModuleGlobal->CommonMaxUserVariableSpace = ((MaxUserNvVariableSpaceSize != 0) ? MaxUserNvVariableSpaceSize : mVariableModuleGlobal->CommonVariableSpace);\r | |
3823 | mVariableModuleGlobal->CommonRuntimeVariableSpace = mVariableModuleGlobal->CommonVariableSpace - BoottimeReservedNvVariableSpaceSize;\r | |
3824 | \r | |
3825 | DEBUG ((EFI_D_INFO, "Variable driver common space: 0x%x 0x%x 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonRuntimeVariableSpace));\r | |
3826 | \r | |
3e02ebb2 | 3827 | //\r |
fa0737a8 | 3828 | // The max NV variable size should be < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r |
3e02ebb2 | 3829 | //\r |
fa0737a8 SZ |
3830 | ASSERT (GetNonVolatileMaxVariableSize () < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r |
3831 | \r | |
3832 | mVariableModuleGlobal->MaxVariableSize = PcdGet32 (PcdMaxVariableSize);\r | |
3833 | mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32 (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize) : mVariableModuleGlobal->MaxVariableSize);\r | |
3e02ebb2 SZ |
3834 | \r |
3835 | //\r | |
3836 | // Parse non-volatile variable data and get last variable offset.\r | |
3837 | //\r | |
4edb1866 SZ |
3838 | Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r |
3839 | while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase))) {\r | |
3840 | NextVariable = GetNextVariablePtr (Variable);\r | |
3841 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r | |
3842 | if ((Variable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r | |
3843 | mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r | |
3e02ebb2 | 3844 | } else {\r |
4edb1866 | 3845 | mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r |
3e02ebb2 SZ |
3846 | }\r |
3847 | \r | |
4edb1866 | 3848 | Variable = NextVariable;\r |
3e02ebb2 | 3849 | }\r |
4edb1866 | 3850 | mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) Variable - (UINTN) VariableStoreBase;\r |
3e02ebb2 | 3851 | \r |
fa0737a8 | 3852 | *NvFvHeader = FvHeader;\r |
3e02ebb2 SZ |
3853 | return EFI_SUCCESS;\r |
3854 | }\r | |
3855 | \r | |
335e2681 SZ |
3856 | /**\r |
3857 | Flush the HOB variable to flash.\r | |
3858 | \r | |
3859 | @param[in] VariableName Name of variable has been updated or deleted.\r | |
3860 | @param[in] VendorGuid Guid of variable has been updated or deleted.\r | |
3861 | \r | |
3862 | **/\r | |
3863 | VOID\r | |
3864 | FlushHobVariableToFlash (\r | |
3865 | IN CHAR16 *VariableName,\r | |
3866 | IN EFI_GUID *VendorGuid\r | |
3867 | )\r | |
3868 | {\r | |
3869 | EFI_STATUS Status;\r | |
3870 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
3871 | VARIABLE_HEADER *Variable;\r | |
3872 | VOID *VariableData;\r | |
31349131 | 3873 | VARIABLE_POINTER_TRACK VariablePtrTrack;\r |
335e2681 SZ |
3874 | BOOLEAN ErrorFlag;\r |
3875 | \r | |
3876 | ErrorFlag = FALSE;\r | |
3877 | \r | |
3878 | //\r | |
3879 | // Flush the HOB variable to flash.\r | |
3880 | //\r | |
3881 | if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r | |
3882 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r | |
3883 | //\r | |
3884 | // Set HobVariableBase to 0, it can avoid SetVariable to call back.\r | |
3885 | //\r | |
3886 | mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r | |
3887 | for ( Variable = GetStartPointer (VariableStoreHeader)\r | |
6ebffb67 | 3888 | ; IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))\r |
335e2681 SZ |
3889 | ; Variable = GetNextVariablePtr (Variable)\r |
3890 | ) {\r | |
3891 | if (Variable->State != VAR_ADDED) {\r | |
3892 | //\r | |
3893 | // The HOB variable has been set to DELETED state in local.\r | |
3894 | //\r | |
3895 | continue;\r | |
3896 | }\r | |
3897 | ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r | |
3898 | if (VendorGuid == NULL || VariableName == NULL ||\r | |
fa0737a8 | 3899 | !CompareGuid (VendorGuid, GetVendorGuidPtr (Variable)) ||\r |
335e2681 SZ |
3900 | StrCmp (VariableName, GetVariableNamePtr (Variable)) != 0) {\r |
3901 | VariableData = GetVariableDataPtr (Variable);\r | |
31349131 SZ |
3902 | FindVariable (GetVariableNamePtr (Variable), GetVendorGuidPtr (Variable), &VariablePtrTrack, &mVariableModuleGlobal->VariableGlobal, FALSE);\r |
3903 | Status = UpdateVariable (\r | |
335e2681 | 3904 | GetVariableNamePtr (Variable),\r |
fa0737a8 | 3905 | GetVendorGuidPtr (Variable),\r |
31349131 | 3906 | VariableData,\r |
fa0737a8 | 3907 | DataSizeOfVariable (Variable),\r |
31349131 SZ |
3908 | Variable->Attributes,\r |
3909 | 0,\r | |
3910 | 0,\r | |
3911 | &VariablePtrTrack,\r | |
3912 | NULL\r | |
3913 | );\r | |
fa0737a8 | 3914 | DEBUG ((EFI_D_INFO, "Variable driver flush the HOB variable to flash: %g %s %r\n", GetVendorGuidPtr (Variable), GetVariableNamePtr (Variable), Status));\r |
335e2681 SZ |
3915 | } else {\r |
3916 | //\r | |
31349131 | 3917 | // The updated or deleted variable is matched with this HOB variable.\r |
335e2681 SZ |
3918 | // Don't break here because we will try to set other HOB variables\r |
3919 | // since this variable could be set successfully.\r | |
3920 | //\r | |
3921 | Status = EFI_SUCCESS;\r | |
3922 | }\r | |
3923 | if (!EFI_ERROR (Status)) {\r | |
3924 | //\r | |
3925 | // If set variable successful, or the updated or deleted variable is matched with the HOB variable,\r | |
3926 | // set the HOB variable to DELETED state in local.\r | |
3927 | //\r | |
fa0737a8 | 3928 | DEBUG ((EFI_D_INFO, "Variable driver set the HOB variable to DELETED state in local: %g %s\n", GetVendorGuidPtr (Variable), GetVariableNamePtr (Variable)));\r |
335e2681 SZ |
3929 | Variable->State &= VAR_DELETED;\r |
3930 | } else {\r | |
3931 | ErrorFlag = TRUE;\r | |
3932 | }\r | |
3933 | }\r | |
3934 | if (ErrorFlag) {\r | |
3935 | //\r | |
3936 | // We still have HOB variable(s) not flushed in flash.\r | |
3937 | //\r | |
3938 | mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;\r | |
3939 | } else {\r | |
3940 | //\r | |
3941 | // All HOB variables have been flushed in flash.\r | |
3942 | //\r | |
3943 | DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n"));\r | |
3944 | if (!AtRuntime ()) {\r | |
3945 | FreePool ((VOID *) VariableStoreHeader);\r | |
3946 | }\r | |
3947 | }\r | |
3948 | }\r | |
3949 | \r | |
3950 | }\r | |
8a2d4996 | 3951 | \r |
7c80e839 | 3952 | /**\r |
3e02ebb2 | 3953 | Initializes variable write service after FTW was ready.\r |
7c80e839 | 3954 | \r |
8a2d4996 | 3955 | @retval EFI_SUCCESS Function successfully executed.\r |
3956 | @retval Others Fail to initialize the variable service.\r | |
3957 | \r | |
3958 | **/\r | |
3959 | EFI_STATUS\r | |
3960 | VariableWriteServiceInitialize (\r | |
3961 | VOID\r | |
3962 | )\r | |
3963 | {\r | |
3964 | EFI_STATUS Status;\r | |
8a2d4996 | 3965 | UINTN Index;\r |
3966 | UINT8 Data;\r | |
8a2d4996 | 3967 | EFI_PHYSICAL_ADDRESS VariableStoreBase;\r |
3e02ebb2 | 3968 | EFI_PHYSICAL_ADDRESS NvStorageBase;\r |
fa0737a8 | 3969 | VARIABLE_ENTRY_PROPERTY *VariableEntry;\r |
8a2d4996 | 3970 | \r |
31349131 SZ |
3971 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
3972 | \r | |
3e02ebb2 SZ |
3973 | NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r |
3974 | if (NvStorageBase == 0) {\r | |
3975 | NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r | |
3976 | }\r | |
9b18845a | 3977 | VariableStoreBase = NvStorageBase + (mNvFvHeaderCache->HeaderLength);\r |
3e02ebb2 SZ |
3978 | \r |
3979 | //\r | |
3980 | // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.\r | |
3981 | //\r | |
3982 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r | |
fa0737a8 | 3983 | \r |
8a2d4996 | 3984 | //\r |
3985 | // Check if the free area is really free.\r | |
3986 | //\r | |
9b18845a | 3987 | for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < mNvVariableCache->Size; Index++) {\r |
8a2d4996 | 3988 | Data = ((UINT8 *) mNvVariableCache)[Index];\r |
3989 | if (Data != 0xff) {\r | |
3990 | //\r | |
3991 | // There must be something wrong in variable store, do reclaim operation.\r | |
3992 | //\r | |
3993 | Status = Reclaim (\r | |
3994 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r | |
3995 | &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r | |
3996 | FALSE,\r | |
335e2681 | 3997 | NULL,\r |
7baf3c69 SZ |
3998 | NULL,\r |
3999 | 0\r | |
8a2d4996 | 4000 | );\r |
4001 | if (EFI_ERROR (Status)) {\r | |
31349131 | 4002 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8a2d4996 | 4003 | return Status;\r |
4004 | }\r | |
4005 | break;\r | |
4006 | }\r | |
4007 | }\r | |
4008 | \r | |
335e2681 | 4009 | FlushHobVariableToFlash (NULL, NULL);\r |
0f7aff72 | 4010 | \r |
fa0737a8 | 4011 | Status = EFI_SUCCESS;\r |
8021f4c7 | 4012 | ZeroMem (&mAuthContextOut, sizeof (mAuthContextOut));\r |
fa0737a8 SZ |
4013 | if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r |
4014 | //\r | |
4015 | // Authenticated variable initialize.\r | |
4016 | //\r | |
8021f4c7 SZ |
4017 | mAuthContextIn.StructSize = sizeof (AUTH_VAR_LIB_CONTEXT_IN);\r |
4018 | mAuthContextIn.MaxAuthVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ();\r | |
4019 | Status = AuthVariableLibInitialize (&mAuthContextIn, &mAuthContextOut);\r | |
fa0737a8 SZ |
4020 | if (!EFI_ERROR (Status)) {\r |
4021 | DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable support!\n"));\r | |
4022 | mVariableModuleGlobal->VariableGlobal.AuthSupport = TRUE;\r | |
8021f4c7 SZ |
4023 | if (mAuthContextOut.AuthVarEntry != NULL) {\r |
4024 | for (Index = 0; Index < mAuthContextOut.AuthVarEntryCount; Index++) {\r | |
4025 | VariableEntry = &mAuthContextOut.AuthVarEntry[Index];\r | |
4026 | Status = VarCheckLibVariablePropertySet (\r | |
fa0737a8 SZ |
4027 | VariableEntry->Name,\r |
4028 | VariableEntry->Guid,\r | |
4029 | &VariableEntry->VariableProperty\r | |
4030 | );\r | |
4031 | ASSERT_EFI_ERROR (Status);\r | |
4032 | }\r | |
4033 | }\r | |
4034 | } else if (Status == EFI_UNSUPPORTED) {\r | |
4035 | DEBUG ((EFI_D_INFO, "NOTICE - AuthVariableLibInitialize() returns %r!\n", Status));\r | |
4036 | DEBUG ((EFI_D_INFO, "Variable driver will continue to work without auth variable support!\n"));\r | |
4037 | mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;\r | |
4038 | Status = EFI_SUCCESS;\r | |
4039 | }\r | |
4040 | }\r | |
4041 | \r | |
4042 | if (!EFI_ERROR (Status)) {\r | |
33f615e2 | 4043 | for (Index = 0; Index < ARRAY_SIZE (mVariableEntryProperty); Index++) {\r |
fa0737a8 | 4044 | VariableEntry = &mVariableEntryProperty[Index];\r |
8021f4c7 | 4045 | Status = VarCheckLibVariablePropertySet (VariableEntry->Name, VariableEntry->Guid, &VariableEntry->VariableProperty);\r |
fa0737a8 SZ |
4046 | ASSERT_EFI_ERROR (Status);\r |
4047 | }\r | |
4048 | }\r | |
4049 | \r | |
31349131 | 4050 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
2f6aa774 JY |
4051 | \r |
4052 | //\r | |
4053 | // Initialize MOR Lock variable.\r | |
4054 | //\r | |
4055 | MorLockInit ();\r | |
4056 | \r | |
fa0737a8 | 4057 | return Status;\r |
8a2d4996 | 4058 | }\r |
4059 | \r | |
4060 | \r | |
4061 | /**\r | |
4062 | Initializes variable store area for non-volatile and volatile variable.\r | |
7c80e839 | 4063 | \r |
4064 | @retval EFI_SUCCESS Function successfully executed.\r | |
4065 | @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r | |
4066 | \r | |
4067 | **/\r | |
8d3a5c82 | 4068 | EFI_STATUS\r |
8d3a5c82 | 4069 | VariableCommonInitialize (\r |
8a2d4996 | 4070 | VOID\r |
8d3a5c82 | 4071 | )\r |
8d3a5c82 | 4072 | {\r |
4073 | EFI_STATUS Status;\r | |
8d3a5c82 | 4074 | VARIABLE_STORE_HEADER *VolatileVariableStore;\r |
4075 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
052ad7e1 | 4076 | UINT64 VariableStoreLength;\r |
2fcdca1d | 4077 | UINTN ScratchSize;\r |
0f7aff72 | 4078 | EFI_HOB_GUID_TYPE *GuidHob;\r |
fa0737a8 SZ |
4079 | EFI_GUID *VariableGuid;\r |
4080 | EFI_FIRMWARE_VOLUME_HEADER *NvFvHeader;\r | |
8d3a5c82 | 4081 | \r |
7800593d LG |
4082 | //\r |
4083 | // Allocate runtime memory for variable driver global structure.\r | |
4084 | //\r | |
72399dae | 4085 | mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r |
7800593d LG |
4086 | if (mVariableModuleGlobal == NULL) {\r |
4087 | return EFI_OUT_OF_RESOURCES;\r | |
4088 | }\r | |
8d3a5c82 | 4089 | \r |
8a2d4996 | 4090 | InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r |
8d3a5c82 | 4091 | \r |
fa0737a8 SZ |
4092 | //\r |
4093 | // Init non-volatile variable store.\r | |
4094 | //\r | |
26c2edd5 | 4095 | NvFvHeader = NULL;\r |
fa0737a8 SZ |
4096 | Status = InitNonVolatileVariableStore (&NvFvHeader);\r |
4097 | if (EFI_ERROR (Status)) {\r | |
4098 | FreePool (mVariableModuleGlobal);\r | |
4099 | return Status;\r | |
4100 | }\r | |
4101 | \r | |
4102 | //\r | |
4103 | // mVariableModuleGlobal->VariableGlobal.AuthFormat\r | |
4104 | // has been initialized in InitNonVolatileVariableStore().\r | |
4105 | //\r | |
4106 | if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r | |
4107 | DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable format!\n"));\r | |
4108 | //\r | |
4109 | // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will initialize it.\r | |
4110 | //\r | |
4111 | mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;\r | |
4112 | VariableGuid = &gEfiAuthenticatedVariableGuid;\r | |
4113 | } else {\r | |
4114 | DEBUG ((EFI_D_INFO, "Variable driver will work without auth variable support!\n"));\r | |
4115 | mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;\r | |
4116 | VariableGuid = &gEfiVariableGuid;\r | |
4117 | }\r | |
4118 | \r | |
0f7aff72 RN |
4119 | //\r |
4120 | // Get HOB variable store.\r | |
4121 | //\r | |
fa0737a8 | 4122 | GuidHob = GetFirstGuidHob (VariableGuid);\r |
0f7aff72 | 4123 | if (GuidHob != NULL) {\r |
f68af18e | 4124 | VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r |
16f69227 | 4125 | VariableStoreLength = GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE);\r |
f68af18e | 4126 | if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r |
335e2681 SZ |
4127 | mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);\r |
4128 | if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {\r | |
fa0737a8 | 4129 | FreePool (NvFvHeader);\r |
3e02ebb2 | 4130 | FreePool (mVariableModuleGlobal);\r |
335e2681 SZ |
4131 | return EFI_OUT_OF_RESOURCES;\r |
4132 | }\r | |
f68af18e RN |
4133 | } else {\r |
4134 | DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r | |
4135 | }\r | |
0f7aff72 RN |
4136 | }\r |
4137 | \r | |
8d3a5c82 | 4138 | //\r |
2fcdca1d | 4139 | // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r |
8d3a5c82 | 4140 | //\r |
fa0737a8 SZ |
4141 | ScratchSize = GetNonVolatileMaxVariableSize ();\r |
4142 | mVariableModuleGlobal->ScratchBufferSize = ScratchSize;\r | |
188e4e84 | 4143 | VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r |
8d3a5c82 | 4144 | if (VolatileVariableStore == NULL) {\r |
3e02ebb2 SZ |
4145 | if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r |
4146 | FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r | |
4147 | }\r | |
fa0737a8 | 4148 | FreePool (NvFvHeader);\r |
8d3a5c82 | 4149 | FreePool (mVariableModuleGlobal);\r |
4150 | return EFI_OUT_OF_RESOURCES;\r | |
4151 | }\r | |
4152 | \r | |
188e4e84 | 4153 | SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r |
8d3a5c82 | 4154 | \r |
4155 | //\r | |
8a2d4996 | 4156 | // Initialize Variable Specific Data.\r |
8d3a5c82 | 4157 | //\r |
052ad7e1 | 4158 | mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r |
9cad030b | 4159 | mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r |
8d3a5c82 | 4160 | \r |
fa0737a8 | 4161 | CopyGuid (&VolatileVariableStore->Signature, VariableGuid);\r |
8a2d4996 | 4162 | VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r |
4163 | VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r | |
4164 | VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r | |
4165 | VolatileVariableStore->Reserved = 0;\r | |
4166 | VolatileVariableStore->Reserved1 = 0;\r | |
8d3a5c82 | 4167 | \r |
fa0737a8 | 4168 | return EFI_SUCCESS;\r |
8d3a5c82 | 4169 | }\r |
052ad7e1 | 4170 | \r |
052ad7e1 | 4171 | \r |
aa75dfec | 4172 | /**\r |
8a2d4996 | 4173 | Get the proper fvb handle and/or fvb protocol by the given Flash address.\r |
aa75dfec | 4174 | \r |
fa0737a8 | 4175 | @param[in] Address The Flash address.\r |
8a2d4996 | 4176 | @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r |
4177 | @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r | |
aa75dfec | 4178 | \r |
aa75dfec | 4179 | **/\r |
8a2d4996 | 4180 | EFI_STATUS\r |
4181 | GetFvbInfoByAddress (\r | |
4182 | IN EFI_PHYSICAL_ADDRESS Address,\r | |
4183 | OUT EFI_HANDLE *FvbHandle OPTIONAL,\r | |
4184 | OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r | |
8a9e0b72 | 4185 | )\r |
4186 | {\r | |
8a2d4996 | 4187 | EFI_STATUS Status;\r |
4188 | EFI_HANDLE *HandleBuffer;\r | |
4189 | UINTN HandleCount;\r | |
4190 | UINTN Index;\r | |
4191 | EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r | |
4192 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r | |
8a2d4996 | 4193 | EFI_FVB_ATTRIBUTES_2 Attributes;\r |
1fcbeaea DG |
4194 | UINTN BlockSize;\r |
4195 | UINTN NumberOfBlocks;\r | |
4196 | \r | |
4e1005ec | 4197 | HandleBuffer = NULL;\r |
8a9e0b72 | 4198 | //\r |
8a2d4996 | 4199 | // Get all FVB handles.\r |
8a9e0b72 | 4200 | //\r |
8a2d4996 | 4201 | Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r |
8a9e0b72 | 4202 | if (EFI_ERROR (Status)) {\r |
8a2d4996 | 4203 | return EFI_NOT_FOUND;\r |
8a9e0b72 | 4204 | }\r |
8a2d4996 | 4205 | \r |
8a9e0b72 | 4206 | //\r |
8a2d4996 | 4207 | // Get the FVB to access variable store.\r |
8a9e0b72 | 4208 | //\r |
8a2d4996 | 4209 | Fvb = NULL;\r |
f0480ecf | 4210 | for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r |
8a2d4996 | 4211 | Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r |
8a9e0b72 | 4212 | if (EFI_ERROR (Status)) {\r |
4213 | Status = EFI_NOT_FOUND;\r | |
4214 | break;\r | |
4215 | }\r | |
4216 | \r | |
4217 | //\r | |
4218 | // Ensure this FVB protocol supported Write operation.\r | |
4219 | //\r | |
4220 | Status = Fvb->GetAttributes (Fvb, &Attributes);\r | |
4221 | if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r | |
1fcbeaea | 4222 | continue;\r |
8a9e0b72 | 4223 | }\r |
1fcbeaea | 4224 | \r |
8a9e0b72 | 4225 | //\r |
8a2d4996 | 4226 | // Compare the address and select the right one.\r |
8a9e0b72 | 4227 | //\r |
4228 | Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r | |
4229 | if (EFI_ERROR (Status)) {\r | |
4230 | continue;\r | |
4231 | }\r | |
4232 | \r | |
1fcbeaea DG |
4233 | //\r |
4234 | // Assume one FVB has one type of BlockSize.\r | |
4235 | //\r | |
4236 | Status = Fvb->GetBlockSize (Fvb, 0, &BlockSize, &NumberOfBlocks);\r | |
4237 | if (EFI_ERROR (Status)) {\r | |
4238 | continue;\r | |
4239 | }\r | |
4240 | \r | |
4241 | if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + BlockSize * NumberOfBlocks))) {\r | |
8a2d4996 | 4242 | if (FvbHandle != NULL) {\r |
4243 | *FvbHandle = HandleBuffer[Index];\r | |
4244 | }\r | |
4245 | if (FvbProtocol != NULL) {\r | |
4246 | *FvbProtocol = Fvb;\r | |
4247 | }\r | |
4248 | Status = EFI_SUCCESS;\r | |
8a9e0b72 | 4249 | break;\r |
4250 | }\r | |
4251 | }\r | |
8a9e0b72 | 4252 | FreePool (HandleBuffer);\r |
533020ef | 4253 | \r |
8a2d4996 | 4254 | if (Fvb == NULL) {\r |
4255 | Status = EFI_NOT_FOUND;\r | |
8a9e0b72 | 4256 | }\r |
fa0737a8 SZ |
4257 | \r |
4258 | return Status;\r | |
052ad7e1 A |
4259 | }\r |
4260 | \r |