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