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