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 | |
8a2d4996 | 684 | // The end 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 SZ |
2464 | CopyMem (&AuthVariable->TimeStamp, TimeStamp, sizeof (EFI_TIME));\r |
2465 | }\r | |
2466 | }\r | |
2467 | }\r | |
2468 | }\r | |
2469 | }\r | |
2470 | \r | |
2471 | //\r | |
2472 | // The EFI_VARIABLE_APPEND_WRITE attribute will never be set in the returned\r | |
2473 | // Attributes bitmask parameter of a GetVariable() call.\r | |
2474 | //\r | |
2475 | NextVariable->Attributes = Attributes & (~EFI_VARIABLE_APPEND_WRITE);\r | |
2476 | \r | |
2477 | VarNameOffset = GetVariableHeaderSize ();\r | |
8a2d4996 | 2478 | VarNameSize = StrSize (VariableName);\r |
c6492839 | 2479 | CopyMem (\r |
2480 | (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r | |
2481 | VariableName,\r | |
2482 | VarNameSize\r | |
2483 | );\r | |
2484 | VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r | |
fa0737a8 SZ |
2485 | \r |
2486 | //\r | |
2487 | // If DataReady is TRUE, it means the variable data has been saved into\r | |
2488 | // NextVariable during EFI_VARIABLE_APPEND_WRITE operation preparation.\r | |
2489 | //\r | |
2490 | if (!DataReady) {\r | |
2491 | CopyMem (\r | |
2492 | (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r | |
2493 | Data,\r | |
2494 | DataSize\r | |
2495 | );\r | |
2496 | }\r | |
2497 | \r | |
2498 | CopyMem (GetVendorGuidPtr (NextVariable), VendorGuid, sizeof (EFI_GUID));\r | |
c6492839 | 2499 | //\r |
2500 | // There will be pad bytes after Data, the NextVariable->NameSize and\r | |
2501 | // NextVariable->DataSize should not include pad size so that variable\r | |
8a2d4996 | 2502 | // service can get actual size in GetVariable.\r |
c6492839 | 2503 | //\r |
fa0737a8 SZ |
2504 | SetNameSizeOfVariable (NextVariable, VarNameSize);\r |
2505 | SetDataSizeOfVariable (NextVariable, DataSize);\r | |
c6492839 | 2506 | \r |
2507 | //\r | |
2508 | // The actual size of the variable that stores in storage should\r | |
2509 | // include pad size.\r | |
2510 | //\r | |
2511 | VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r | |
45f6c85b | 2512 | if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r |
8d3a5c82 | 2513 | //\r |
8a2d4996 | 2514 | // Create a nonvolatile variable.\r |
8d3a5c82 | 2515 | //\r |
fd51bf70 | 2516 | Volatile = FALSE;\r |
4edb1866 SZ |
2517 | \r |
2518 | IsCommonVariable = FALSE;\r | |
2519 | IsCommonUserVariable = FALSE;\r | |
2520 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) {\r | |
2521 | IsCommonVariable = TRUE;\r | |
2522 | IsCommonUserVariable = IsUserVariable (NextVariable);\r | |
2523 | }\r | |
2524 | if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)\r | |
188e4e84 | 2525 | && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r |
4edb1866 SZ |
2526 | || (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace))\r |
2527 | || (IsCommonVariable && AtRuntime () && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace))\r | |
2528 | || (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace))) {\r | |
8a2d4996 | 2529 | if (AtRuntime ()) {\r |
4edb1866 SZ |
2530 | if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r |
2531 | RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r | |
2532 | }\r | |
2533 | if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonRuntimeVariableSpace)) {\r | |
2534 | RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r | |
2535 | }\r | |
c6492839 | 2536 | Status = EFI_OUT_OF_RESOURCES;\r |
2537 | goto Done;\r | |
2538 | }\r | |
2539 | //\r | |
7baf3c69 | 2540 | // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r |
c6492839 | 2541 | //\r |
fa0737a8 SZ |
2542 | Status = Reclaim (\r |
2543 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r | |
2544 | &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r | |
2545 | FALSE,\r | |
2546 | Variable,\r | |
2547 | NextVariable,\r | |
2548 | HEADER_ALIGN (VarSize)\r | |
2549 | );\r | |
7baf3c69 SZ |
2550 | if (!EFI_ERROR (Status)) {\r |
2551 | //\r | |
2552 | // The new variable has been integrated successfully during reclaiming.\r | |
2553 | //\r | |
2554 | if (Variable->CurrPtr != NULL) {\r | |
2555 | CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r | |
2556 | CacheVariable->InDeletedTransitionPtr = NULL;\r | |
2557 | }\r | |
2558 | UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, FALSE, FALSE);\r | |
2559 | FlushHobVariableToFlash (VariableName, VendorGuid);\r | |
4edb1866 SZ |
2560 | } else {\r |
2561 | if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->CommonUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpace)) {\r | |
2562 | RecordVarErrorFlag (VAR_ERROR_FLAG_USER_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r | |
2563 | }\r | |
2564 | if (IsCommonVariable && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > mVariableModuleGlobal->CommonVariableSpace)) {\r | |
2565 | RecordVarErrorFlag (VAR_ERROR_FLAG_SYSTEM_ERROR, VariableName, VendorGuid, Attributes, VarSize);\r | |
2566 | }\r | |
23b06935 | 2567 | }\r |
7baf3c69 | 2568 | goto Done;\r |
8d3a5c82 | 2569 | }\r |
2570 | //\r | |
8a2d4996 | 2571 | // Four steps\r |
c6492839 | 2572 | // 1. Write variable header\r |
fa0737a8 | 2573 | // 2. Set variable state to header valid\r |
130e2569 | 2574 | // 3. Write variable data\r |
2575 | // 4. Set variable state to valid\r | |
8d3a5c82 | 2576 | //\r |
8d3a5c82 | 2577 | //\r |
c6492839 | 2578 | // Step 1:\r |
8d3a5c82 | 2579 | //\r |
8a2d4996 | 2580 | CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r |
c6492839 | 2581 | Status = UpdateVariableStore (\r |
052ad7e1 | 2582 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 2583 | FALSE,\r |
2584 | TRUE,\r | |
8a9e0b72 | 2585 | Fvb,\r |
72399dae | 2586 | mVariableModuleGlobal->NonVolatileLastVariableOffset,\r |
fa0737a8 | 2587 | (UINT32) GetVariableHeaderSize (),\r |
c6492839 | 2588 | (UINT8 *) NextVariable\r |
2589 | );\r | |
2590 | \r | |
2591 | if (EFI_ERROR (Status)) {\r | |
2592 | goto Done;\r | |
2593 | }\r | |
130e2569 | 2594 | \r |
8d3a5c82 | 2595 | //\r |
c6492839 | 2596 | // Step 2:\r |
8d3a5c82 | 2597 | //\r |
130e2569 | 2598 | NextVariable->State = VAR_HEADER_VALID_ONLY;\r |
2599 | Status = UpdateVariableStore (\r | |
2600 | &mVariableModuleGlobal->VariableGlobal,\r | |
2601 | FALSE,\r | |
2602 | TRUE,\r | |
8a9e0b72 | 2603 | Fvb,\r |
8a2d4996 | 2604 | mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r |
2605 | sizeof (UINT8),\r | |
2606 | &NextVariable->State\r | |
130e2569 | 2607 | );\r |
2608 | \r | |
2609 | if (EFI_ERROR (Status)) {\r | |
2610 | goto Done;\r | |
2611 | }\r | |
2612 | //\r | |
2613 | // Step 3:\r | |
2614 | //\r | |
c6492839 | 2615 | Status = UpdateVariableStore (\r |
052ad7e1 | 2616 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 2617 | FALSE,\r |
2618 | TRUE,\r | |
8a9e0b72 | 2619 | Fvb,\r |
fa0737a8 SZ |
2620 | mVariableModuleGlobal->NonVolatileLastVariableOffset + GetVariableHeaderSize (),\r |
2621 | (UINT32) (VarSize - GetVariableHeaderSize ()),\r | |
2622 | (UINT8 *) NextVariable + GetVariableHeaderSize ()\r | |
c6492839 | 2623 | );\r |
2624 | \r | |
2625 | if (EFI_ERROR (Status)) {\r | |
2626 | goto Done;\r | |
2627 | }\r | |
8d3a5c82 | 2628 | //\r |
130e2569 | 2629 | // Step 4:\r |
8d3a5c82 | 2630 | //\r |
c6492839 | 2631 | NextVariable->State = VAR_ADDED;\r |
2632 | Status = UpdateVariableStore (\r | |
052ad7e1 | 2633 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 2634 | FALSE,\r |
2635 | TRUE,\r | |
8a9e0b72 | 2636 | Fvb,\r |
8a2d4996 | 2637 | mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r |
2638 | sizeof (UINT8),\r | |
2639 | &NextVariable->State\r | |
c6492839 | 2640 | );\r |
2641 | \r | |
2642 | if (EFI_ERROR (Status)) {\r | |
2643 | goto Done;\r | |
2644 | }\r | |
8d3a5c82 | 2645 | \r |
72399dae | 2646 | mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r |
8d3a5c82 | 2647 | \r |
2fcdca1d | 2648 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r |
2649 | mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r | |
2650 | } else {\r | |
2651 | mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r | |
4edb1866 SZ |
2652 | if (IsCommonUserVariable) {\r |
2653 | mVariableModuleGlobal->CommonUserVariableTotalSize += HEADER_ALIGN (VarSize);\r | |
2654 | }\r | |
2fcdca1d | 2655 | }\r |
8a2d4996 | 2656 | //\r |
2657 | // update the memory copy of Flash region.\r | |
2658 | //\r | |
2659 | CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);\r | |
c6492839 | 2660 | } else {\r |
2661 | //\r | |
8a2d4996 | 2662 | // Create a volatile variable.\r |
fa0737a8 | 2663 | //\r |
fd51bf70 | 2664 | Volatile = TRUE;\r |
c6492839 | 2665 | \r |
72399dae | 2666 | if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r |
052ad7e1 | 2667 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r |
8d3a5c82 | 2668 | //\r |
7baf3c69 | 2669 | // Perform garbage collection & reclaim operation, and integrate the new variable at the same time.\r |
8d3a5c82 | 2670 | //\r |
fa0737a8 SZ |
2671 | Status = Reclaim (\r |
2672 | mVariableModuleGlobal->VariableGlobal.VolatileVariableBase,\r | |
2673 | &mVariableModuleGlobal->VolatileLastVariableOffset,\r | |
2674 | TRUE,\r | |
2675 | Variable,\r | |
2676 | NextVariable,\r | |
2677 | HEADER_ALIGN (VarSize)\r | |
2678 | );\r | |
7baf3c69 SZ |
2679 | if (!EFI_ERROR (Status)) {\r |
2680 | //\r | |
2681 | // The new variable has been integrated successfully during reclaiming.\r | |
2682 | //\r | |
2683 | if (Variable->CurrPtr != NULL) {\r | |
2684 | CacheVariable->CurrPtr = (VARIABLE_HEADER *)((UINTN) CacheVariable->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr));\r | |
2685 | CacheVariable->InDeletedTransitionPtr = NULL;\r | |
2686 | }\r | |
2687 | UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, FALSE, FALSE);\r | |
23b06935 | 2688 | }\r |
7baf3c69 | 2689 | goto Done;\r |
8d3a5c82 | 2690 | }\r |
8d3a5c82 | 2691 | \r |
c6492839 | 2692 | NextVariable->State = VAR_ADDED;\r |
2693 | Status = UpdateVariableStore (\r | |
052ad7e1 | 2694 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 2695 | TRUE,\r |
2696 | TRUE,\r | |
8a9e0b72 | 2697 | Fvb,\r |
72399dae | 2698 | mVariableModuleGlobal->VolatileLastVariableOffset,\r |
c6492839 | 2699 | (UINT32) VarSize,\r |
2700 | (UINT8 *) NextVariable\r | |
2701 | );\r | |
2702 | \r | |
2703 | if (EFI_ERROR (Status)) {\r | |
2704 | goto Done;\r | |
8d3a5c82 | 2705 | }\r |
c6492839 | 2706 | \r |
72399dae | 2707 | mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r |
c6492839 | 2708 | }\r |
72399dae | 2709 | \r |
c6492839 | 2710 | //\r |
8a2d4996 | 2711 | // Mark the old variable as deleted.\r |
c6492839 | 2712 | //\r |
23b06935 SZ |
2713 | if (!EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r |
2714 | if (Variable->InDeletedTransitionPtr != NULL) {\r | |
2715 | //\r | |
2716 | // Both ADDED and IN_DELETED_TRANSITION old variable are present,\r | |
2717 | // set IN_DELETED_TRANSITION one to DELETED state first.\r | |
2718 | //\r | |
ae658d91 | 2719 | ASSERT (CacheVariable->InDeletedTransitionPtr != NULL);\r |
9b18845a | 2720 | State = CacheVariable->InDeletedTransitionPtr->State;\r |
23b06935 SZ |
2721 | State &= VAR_DELETED;\r |
2722 | Status = UpdateVariableStore (\r | |
2723 | &mVariableModuleGlobal->VariableGlobal,\r | |
2724 | Variable->Volatile,\r | |
2725 | FALSE,\r | |
2726 | Fvb,\r | |
2727 | (UINTN) &Variable->InDeletedTransitionPtr->State,\r | |
2728 | sizeof (UINT8),\r | |
2729 | &State\r | |
2730 | );\r | |
2731 | if (!EFI_ERROR (Status)) {\r | |
2732 | if (!Variable->Volatile) {\r | |
2733 | CacheVariable->InDeletedTransitionPtr->State = State;\r | |
2734 | }\r | |
2735 | } else {\r | |
2736 | goto Done;\r | |
2737 | }\r | |
2738 | }\r | |
2739 | \r | |
72399dae | 2740 | State = Variable->CurrPtr->State;\r |
c6492839 | 2741 | State &= VAR_DELETED;\r |
2742 | \r | |
2743 | Status = UpdateVariableStore (\r | |
72399dae | 2744 | &mVariableModuleGlobal->VariableGlobal,\r |
2745 | Variable->Volatile,\r | |
2746 | FALSE,\r | |
2747 | Fvb,\r | |
2748 | (UINTN) &Variable->CurrPtr->State,\r | |
2749 | sizeof (UINT8),\r | |
2750 | &State\r | |
2751 | );\r | |
fa0737a8 | 2752 | if (!EFI_ERROR (Status) && !Variable->Volatile) {\r |
8a2d4996 | 2753 | CacheVariable->CurrPtr->State = State;\r |
2754 | }\r | |
72399dae | 2755 | }\r |
2756 | \r | |
2757 | if (!EFI_ERROR (Status)) {\r | |
2758 | UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r | |
335e2681 SZ |
2759 | if (!Volatile) {\r |
2760 | FlushHobVariableToFlash (VariableName, VendorGuid);\r | |
2761 | }\r | |
72399dae | 2762 | }\r |
2763 | \r | |
2764 | Done:\r | |
2765 | return Status;\r | |
2766 | }\r | |
2767 | \r | |
2768 | /**\r | |
2769 | \r | |
2770 | This code finds variable in storage blocks (Volatile or Non-Volatile).\r | |
2771 | \r | |
18a7dbbc SZ |
2772 | Caution: This function may receive untrusted input.\r |
2773 | This function may be invoked in SMM mode, and datasize is external input.\r | |
2774 | This function will do basic validation, before parse the data.\r | |
2775 | \r | |
72399dae | 2776 | @param VariableName Name of Variable to be found.\r |
2777 | @param VendorGuid Variable vendor GUID.\r | |
2778 | @param Attributes Attribute value of the variable found.\r | |
2779 | @param DataSize Size of Data found. If size is less than the\r | |
2780 | data, this value contains the required size.\r | |
dd59d95e SZ |
2781 | @param Data The buffer to return the contents of the variable. May be NULL\r |
2782 | with a zero DataSize in order to determine the size buffer needed.\r | |
fa0737a8 | 2783 | \r |
8a2d4996 | 2784 | @return EFI_INVALID_PARAMETER Invalid parameter.\r |
2785 | @return EFI_SUCCESS Find the specified variable.\r | |
2786 | @return EFI_NOT_FOUND Not found.\r | |
2787 | @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r | |
72399dae | 2788 | \r |
2789 | **/\r | |
2790 | EFI_STATUS\r | |
2791 | EFIAPI\r | |
8a2d4996 | 2792 | VariableServiceGetVariable (\r |
72399dae | 2793 | IN CHAR16 *VariableName,\r |
2794 | IN EFI_GUID *VendorGuid,\r | |
2795 | OUT UINT32 *Attributes OPTIONAL,\r | |
2796 | IN OUT UINTN *DataSize,\r | |
dd59d95e | 2797 | OUT VOID *Data OPTIONAL\r |
72399dae | 2798 | )\r |
2799 | {\r | |
2800 | EFI_STATUS Status;\r | |
2801 | VARIABLE_POINTER_TRACK Variable;\r | |
2802 | UINTN VarDataSize;\r | |
2803 | \r | |
2804 | if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r | |
2805 | return EFI_INVALID_PARAMETER;\r | |
2806 | }\r | |
2807 | \r | |
e19eab61 SZ |
2808 | if (VariableName[0] == 0) {\r |
2809 | return EFI_NOT_FOUND;\r | |
2810 | }\r | |
2811 | \r | |
72399dae | 2812 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
fa0737a8 | 2813 | \r |
9622df63 | 2814 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r |
72399dae | 2815 | if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r |
2816 | goto Done;\r | |
2817 | }\r | |
2818 | \r | |
2819 | //\r | |
2820 | // Get data size\r | |
2821 | //\r | |
2822 | VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r | |
2823 | ASSERT (VarDataSize != 0);\r | |
2824 | \r | |
2825 | if (*DataSize >= VarDataSize) {\r | |
2826 | if (Data == NULL) {\r | |
2827 | Status = EFI_INVALID_PARAMETER;\r | |
2828 | goto Done;\r | |
33a5a666 | 2829 | }\r |
72399dae | 2830 | \r |
2831 | CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r | |
2832 | if (Attributes != NULL) {\r | |
2833 | *Attributes = Variable.CurrPtr->Attributes;\r | |
2834 | }\r | |
2835 | \r | |
2836 | *DataSize = VarDataSize;\r | |
2837 | UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r | |
fa0737a8 | 2838 | \r |
72399dae | 2839 | Status = EFI_SUCCESS;\r |
2840 | goto Done;\r | |
2841 | } else {\r | |
2842 | *DataSize = VarDataSize;\r | |
2843 | Status = EFI_BUFFER_TOO_SMALL;\r | |
2844 | goto Done;\r | |
8d3a5c82 | 2845 | }\r |
2846 | \r | |
72399dae | 2847 | Done:\r |
2848 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r | |
2849 | return Status;\r | |
2850 | }\r | |
2851 | \r | |
72399dae | 2852 | /**\r |
72399dae | 2853 | This code Finds the Next available variable.\r |
2854 | \r | |
18a7dbbc SZ |
2855 | Caution: This function may receive untrusted input.\r |
2856 | This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r | |
2857 | \r | |
fa0737a8 SZ |
2858 | @param[in] VariableName Pointer to variable name.\r |
2859 | @param[in] VendorGuid Variable Vendor Guid.\r | |
2860 | @param[out] VariablePtr Pointer to variable header address.\r | |
72399dae | 2861 | \r |
6f817f9b SZ |
2862 | @retval EFI_SUCCESS The function completed successfully.\r |
2863 | @retval EFI_NOT_FOUND The next variable was not found.\r | |
2864 | @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.\r | |
2865 | @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and\r | |
2866 | GUID of an existing variable.\r | |
72399dae | 2867 | \r |
2868 | **/\r | |
2869 | EFI_STATUS\r | |
2870 | EFIAPI\r | |
fa0737a8 SZ |
2871 | VariableServiceGetNextVariableInternal (\r |
2872 | IN CHAR16 *VariableName,\r | |
2873 | IN EFI_GUID *VendorGuid,\r | |
2874 | OUT VARIABLE_HEADER **VariablePtr\r | |
72399dae | 2875 | )\r |
2876 | {\r | |
0f7aff72 | 2877 | VARIABLE_STORE_TYPE Type;\r |
72399dae | 2878 | VARIABLE_POINTER_TRACK Variable;\r |
0f7aff72 | 2879 | VARIABLE_POINTER_TRACK VariableInHob;\r |
23b06935 | 2880 | VARIABLE_POINTER_TRACK VariablePtrTrack;\r |
72399dae | 2881 | EFI_STATUS Status;\r |
0f7aff72 | 2882 | VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax];\r |
72399dae | 2883 | \r |
9622df63 | 2884 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r |
72399dae | 2885 | if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r |
6f817f9b SZ |
2886 | //\r |
2887 | // For VariableName is an empty string, FindVariable() will try to find and return\r | |
2888 | // the first qualified variable, and if FindVariable() returns error (EFI_NOT_FOUND)\r | |
2889 | // as no any variable is found, still go to return the error (EFI_NOT_FOUND).\r | |
2890 | //\r | |
2891 | if (VariableName[0] != 0) {\r | |
2892 | //\r | |
2893 | // For VariableName is not an empty string, and FindVariable() returns error as\r | |
2894 | // VariableName and VendorGuid are not a name and GUID of an existing variable,\r | |
2895 | // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER.\r | |
2896 | //\r | |
2897 | Status = EFI_INVALID_PARAMETER;\r | |
2898 | }\r | |
72399dae | 2899 | goto Done;\r |
2900 | }\r | |
2901 | \r | |
2902 | if (VariableName[0] != 0) {\r | |
2903 | //\r | |
8a2d4996 | 2904 | // If variable name is not NULL, get next variable.\r |
72399dae | 2905 | //\r |
2906 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
2907 | }\r | |
2908 | \r | |
0f7aff72 RN |
2909 | //\r |
2910 | // 0: Volatile, 1: HOB, 2: Non-Volatile.\r | |
2911 | // The index and attributes mapping must be kept in this order as FindVariable\r | |
2912 | // makes use of this mapping to implement search algorithm.\r | |
2913 | //\r | |
2914 | VariableStoreHeader[VariableStoreTypeVolatile] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r | |
2915 | VariableStoreHeader[VariableStoreTypeHob] = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r | |
2916 | VariableStoreHeader[VariableStoreTypeNv] = mNvVariableCache;\r | |
2917 | \r | |
72399dae | 2918 | while (TRUE) {\r |
2919 | //\r | |
0f7aff72 | 2920 | // Switch from Volatile to HOB, to Non-Volatile.\r |
72399dae | 2921 | //\r |
6ebffb67 | 2922 | while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) {\r |
0f7aff72 RN |
2923 | //\r |
2924 | // Find current storage index\r | |
2925 | //\r | |
2926 | for (Type = (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; Type++) {\r | |
2927 | if ((VariableStoreHeader[Type] != NULL) && (Variable.StartPtr == GetStartPointer (VariableStoreHeader[Type]))) {\r | |
2928 | break;\r | |
2929 | }\r | |
2930 | }\r | |
2931 | ASSERT (Type < VariableStoreTypeMax);\r | |
2932 | //\r | |
2933 | // Switch to next storage\r | |
2934 | //\r | |
2935 | for (Type++; Type < VariableStoreTypeMax; Type++) {\r | |
2936 | if (VariableStoreHeader[Type] != NULL) {\r | |
2937 | break;\r | |
2938 | }\r | |
2939 | }\r | |
2940 | //\r | |
fa0737a8 | 2941 | // Capture the case that\r |
0f7aff72 RN |
2942 | // 1. current storage is the last one, or\r |
2943 | // 2. no further storage\r | |
2944 | //\r | |
2945 | if (Type == VariableStoreTypeMax) {\r | |
72399dae | 2946 | Status = EFI_NOT_FOUND;\r |
2947 | goto Done;\r | |
2948 | }\r | |
0f7aff72 RN |
2949 | Variable.StartPtr = GetStartPointer (VariableStoreHeader[Type]);\r |
2950 | Variable.EndPtr = GetEndPointer (VariableStoreHeader[Type]);\r | |
2951 | Variable.CurrPtr = Variable.StartPtr;\r | |
72399dae | 2952 | }\r |
0f7aff72 | 2953 | \r |
72399dae | 2954 | //\r |
2955 | // Variable is found\r | |
2956 | //\r | |
23b06935 SZ |
2957 | if (Variable.CurrPtr->State == VAR_ADDED || Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r |
2958 | if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r | |
2959 | if (Variable.CurrPtr->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r | |
2960 | //\r | |
2961 | // If it is a IN_DELETED_TRANSITION variable,\r | |
2962 | // and there is also a same ADDED one at the same time,\r | |
2963 | // don't return it.\r | |
2964 | //\r | |
2965 | VariablePtrTrack.StartPtr = Variable.StartPtr;\r | |
2966 | VariablePtrTrack.EndPtr = Variable.EndPtr;\r | |
2967 | Status = FindVariableEx (\r | |
2968 | GetVariableNamePtr (Variable.CurrPtr),\r | |
fa0737a8 | 2969 | GetVendorGuidPtr (Variable.CurrPtr),\r |
23b06935 SZ |
2970 | FALSE,\r |
2971 | &VariablePtrTrack\r | |
2972 | );\r | |
2973 | if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State == VAR_ADDED) {\r | |
2974 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
2975 | continue;\r | |
2976 | }\r | |
2977 | }\r | |
0f7aff72 RN |
2978 | \r |
2979 | //\r | |
2980 | // Don't return NV variable when HOB overrides it\r | |
2981 | //\r | |
fa0737a8 | 2982 | if ((VariableStoreHeader[VariableStoreTypeHob] != NULL) && (VariableStoreHeader[VariableStoreTypeNv] != NULL) &&\r |
0f7aff72 RN |
2983 | (Variable.StartPtr == GetStartPointer (VariableStoreHeader[VariableStoreTypeNv]))\r |
2984 | ) {\r | |
2985 | VariableInHob.StartPtr = GetStartPointer (VariableStoreHeader[VariableStoreTypeHob]);\r | |
2986 | VariableInHob.EndPtr = GetEndPointer (VariableStoreHeader[VariableStoreTypeHob]);\r | |
2987 | Status = FindVariableEx (\r | |
2988 | GetVariableNamePtr (Variable.CurrPtr),\r | |
fa0737a8 | 2989 | GetVendorGuidPtr (Variable.CurrPtr),\r |
9622df63 | 2990 | FALSE,\r |
0f7aff72 RN |
2991 | &VariableInHob\r |
2992 | );\r | |
2993 | if (!EFI_ERROR (Status)) {\r | |
2994 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
2995 | continue;\r | |
2996 | }\r | |
2997 | }\r | |
2998 | \r | |
fa0737a8 SZ |
2999 | *VariablePtr = Variable.CurrPtr;\r |
3000 | Status = EFI_SUCCESS;\r | |
72399dae | 3001 | goto Done;\r |
3002 | }\r | |
3003 | }\r | |
3004 | \r | |
3005 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
3006 | }\r | |
33a5a666 | 3007 | \r |
8d3a5c82 | 3008 | Done:\r |
fa0737a8 SZ |
3009 | return Status;\r |
3010 | }\r | |
3011 | \r | |
3012 | /**\r | |
3013 | \r | |
3014 | This code Finds the Next available variable.\r | |
3015 | \r | |
3016 | Caution: This function may receive untrusted input.\r | |
3017 | This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r | |
3018 | \r | |
6f817f9b SZ |
3019 | @param VariableNameSize The size of the VariableName buffer. The size must be large\r |
3020 | enough to fit input string supplied in VariableName buffer.\r | |
fa0737a8 SZ |
3021 | @param VariableName Pointer to variable name.\r |
3022 | @param VendorGuid Variable Vendor Guid.\r | |
3023 | \r | |
6f817f9b SZ |
3024 | @retval EFI_SUCCESS The function completed successfully.\r |
3025 | @retval EFI_NOT_FOUND The next variable was not found.\r | |
3026 | @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the result.\r | |
3027 | VariableNameSize has been updated with the size needed to complete the request.\r | |
3028 | @retval EFI_INVALID_PARAMETER VariableNameSize is NULL.\r | |
3029 | @retval EFI_INVALID_PARAMETER VariableName is NULL.\r | |
3030 | @retval EFI_INVALID_PARAMETER VendorGuid is NULL.\r | |
3031 | @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and\r | |
3032 | GUID of an existing variable.\r | |
3033 | @retval EFI_INVALID_PARAMETER Null-terminator is not found in the first VariableNameSize bytes of\r | |
3034 | the input VariableName buffer.\r | |
fa0737a8 SZ |
3035 | \r |
3036 | **/\r | |
3037 | EFI_STATUS\r | |
3038 | EFIAPI\r | |
3039 | VariableServiceGetNextVariableName (\r | |
3040 | IN OUT UINTN *VariableNameSize,\r | |
3041 | IN OUT CHAR16 *VariableName,\r | |
3042 | IN OUT EFI_GUID *VendorGuid\r | |
3043 | )\r | |
3044 | {\r | |
3045 | EFI_STATUS Status;\r | |
6f817f9b | 3046 | UINTN MaxLen;\r |
fa0737a8 SZ |
3047 | UINTN VarNameSize;\r |
3048 | VARIABLE_HEADER *VariablePtr;\r | |
3049 | \r | |
3050 | if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r | |
3051 | return EFI_INVALID_PARAMETER;\r | |
3052 | }\r | |
3053 | \r | |
6f817f9b SZ |
3054 | //\r |
3055 | // Calculate the possible maximum length of name string, including the Null terminator.\r | |
3056 | //\r | |
3057 | MaxLen = *VariableNameSize / sizeof (CHAR16);\r | |
3058 | if ((MaxLen == 0) || (StrnLenS (VariableName, MaxLen) == MaxLen)) {\r | |
3059 | //\r | |
3060 | // Null-terminator is not found in the first VariableNameSize bytes of the input VariableName buffer,\r | |
3061 | // follow spec to return EFI_INVALID_PARAMETER.\r | |
3062 | //\r | |
3063 | return EFI_INVALID_PARAMETER;\r | |
3064 | }\r | |
3065 | \r | |
fa0737a8 SZ |
3066 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
3067 | \r | |
3068 | Status = VariableServiceGetNextVariableInternal (VariableName, VendorGuid, &VariablePtr);\r | |
3069 | if (!EFI_ERROR (Status)) {\r | |
3070 | VarNameSize = NameSizeOfVariable (VariablePtr);\r | |
3071 | ASSERT (VarNameSize != 0);\r | |
3072 | if (VarNameSize <= *VariableNameSize) {\r | |
3073 | CopyMem (VariableName, GetVariableNamePtr (VariablePtr), VarNameSize);\r | |
3074 | CopyMem (VendorGuid, GetVendorGuidPtr (VariablePtr), sizeof (EFI_GUID));\r | |
3075 | Status = EFI_SUCCESS;\r | |
3076 | } else {\r | |
3077 | Status = EFI_BUFFER_TOO_SMALL;\r | |
3078 | }\r | |
3079 | \r | |
3080 | *VariableNameSize = VarNameSize;\r | |
3081 | }\r | |
3082 | \r | |
72399dae | 3083 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
3084 | return Status;\r | |
3085 | }\r | |
3086 | \r | |
3087 | /**\r | |
3088 | \r | |
3089 | This code sets variable in storage blocks (Volatile or Non-Volatile).\r | |
3090 | \r | |
18a7dbbc SZ |
3091 | Caution: This function may receive untrusted input.\r |
3092 | This function may be invoked in SMM mode, and datasize and data are external input.\r | |
3093 | This function will do basic validation, before parse the data.\r | |
fa0737a8 SZ |
3094 | This function will parse the authentication carefully to avoid security issues, like\r |
3095 | buffer overflow, integer overflow.\r | |
3096 | This function will check attribute carefully to avoid authentication bypass.\r | |
18a7dbbc | 3097 | \r |
8a2d4996 | 3098 | @param VariableName Name of Variable to be found.\r |
3099 | @param VendorGuid Variable vendor GUID.\r | |
72399dae | 3100 | @param Attributes Attribute value of the variable found\r |
3101 | @param DataSize Size of Data found. If size is less than the\r | |
3102 | data, this value contains the required size.\r | |
8a2d4996 | 3103 | @param Data Data pointer.\r |
72399dae | 3104 | \r |
8a2d4996 | 3105 | @return EFI_INVALID_PARAMETER Invalid parameter.\r |
3106 | @return EFI_SUCCESS Set successfully.\r | |
3107 | @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r | |
3108 | @return EFI_NOT_FOUND Not found.\r | |
3109 | @return EFI_WRITE_PROTECTED Variable is read-only.\r | |
72399dae | 3110 | \r |
3111 | **/\r | |
3112 | EFI_STATUS\r | |
3113 | EFIAPI\r | |
8a2d4996 | 3114 | VariableServiceSetVariable (\r |
72399dae | 3115 | IN CHAR16 *VariableName,\r |
3116 | IN EFI_GUID *VendorGuid,\r | |
3117 | IN UINT32 Attributes,\r | |
3118 | IN UINTN DataSize,\r | |
3119 | IN VOID *Data\r | |
3120 | )\r | |
3121 | {\r | |
3122 | VARIABLE_POINTER_TRACK Variable;\r | |
3123 | EFI_STATUS Status;\r | |
3124 | VARIABLE_HEADER *NextVariable;\r | |
3125 | EFI_PHYSICAL_ADDRESS Point;\r | |
fa0737a8 | 3126 | UINTN PayloadSize;\r |
72399dae | 3127 | \r |
3128 | //\r | |
8a2d4996 | 3129 | // Check input parameters.\r |
72399dae | 3130 | //\r |
3131 | if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r | |
3132 | return EFI_INVALID_PARAMETER;\r | |
fa0737a8 | 3133 | }\r |
48a0e6bf | 3134 | \r |
3135 | if (DataSize != 0 && Data == NULL) {\r | |
3136 | return EFI_INVALID_PARAMETER;\r | |
3137 | }\r | |
3138 | \r | |
8e38f18e | 3139 | //\r |
fa0737a8 | 3140 | // Check for reserverd bit in variable attribute.\r |
4073f85d ZC |
3141 | // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS is deprecated but we still allow\r |
3142 | // the delete operation of common authenticated variable at user physical presence.\r | |
3143 | // So leave EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS attribute check to AuthVariableLib\r | |
8e38f18e | 3144 | //\r |
4073f85d | 3145 | if ((Attributes & (~(EFI_VARIABLE_ATTRIBUTES_MASK | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS))) != 0) {\r |
8e38f18e | 3146 | return EFI_INVALID_PARAMETER;\r |
3147 | }\r | |
3148 | \r | |
72399dae | 3149 | //\r |
8a2d4996 | 3150 | // Make sure if runtime bit is set, boot service bit is set also.\r |
72399dae | 3151 | //\r |
3152 | if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r | |
67943427 | 3153 | if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r |
3154 | return EFI_UNSUPPORTED;\r | |
3155 | } else {\r | |
3156 | return EFI_INVALID_PARAMETER;\r | |
3157 | }\r | |
fa0737a8 SZ |
3158 | } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r |
3159 | if (!mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r | |
3160 | //\r | |
3161 | // Not support authenticated variable write.\r | |
3162 | //\r | |
3163 | return EFI_INVALID_PARAMETER;\r | |
3164 | }\r | |
3165 | } else if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r | |
3166 | if (PcdGet32 (PcdHwErrStorageSize) == 0) {\r | |
3167 | //\r | |
3168 | // Not support harware error record variable variable.\r | |
3169 | //\r | |
3170 | return EFI_INVALID_PARAMETER;\r | |
3171 | }\r | |
3172 | }\r | |
3173 | \r | |
3174 | //\r | |
3175 | // EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS and EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS attribute\r | |
3176 | // cannot be set both.\r | |
3177 | //\r | |
3178 | if (((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS)\r | |
3179 | && ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS)) {\r | |
67943427 | 3180 | return EFI_UNSUPPORTED;\r |
72399dae | 3181 | }\r |
3182 | \r | |
fa0737a8 | 3183 | if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {\r |
67943427 | 3184 | //\r |
3185 | // If DataSize == AUTHINFO_SIZE and then PayloadSize is 0.\r | |
3186 | // Maybe it's the delete operation of common authenticated variable at user physical presence.\r | |
3187 | //\r | |
3188 | if (DataSize != AUTHINFO_SIZE) {\r | |
3189 | return EFI_UNSUPPORTED;\r | |
fa0737a8 SZ |
3190 | }\r |
3191 | PayloadSize = DataSize - AUTHINFO_SIZE;\r | |
3192 | } else if ((Attributes & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) {\r | |
3193 | //\r | |
3194 | // Sanity check for EFI_VARIABLE_AUTHENTICATION_2 descriptor.\r | |
3195 | //\r | |
3196 | if (DataSize < OFFSET_OF_AUTHINFO2_CERT_DATA ||\r | |
3197 | ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength > DataSize - (OFFSET_OF (EFI_VARIABLE_AUTHENTICATION_2, AuthInfo)) ||\r | |
3198 | ((EFI_VARIABLE_AUTHENTICATION_2 *) Data)->AuthInfo.Hdr.dwLength < OFFSET_OF (WIN_CERTIFICATE_UEFI_GUID, CertData)) {\r | |
3199 | return EFI_SECURITY_VIOLATION;\r | |
3200 | }\r | |
3201 | PayloadSize = DataSize - AUTHINFO2_SIZE (Data);\r | |
3202 | } else {\r | |
3203 | PayloadSize = DataSize;\r | |
3204 | }\r | |
3205 | \r | |
3206 | if ((UINTN)(~0) - PayloadSize < StrSize(VariableName)){\r | |
3207 | //\r | |
3208 | // Prevent whole variable size overflow\r | |
56251c66 | 3209 | //\r |
56251c66 | 3210 | return EFI_INVALID_PARAMETER;\r |
3211 | }\r | |
3212 | \r | |
72399dae | 3213 | //\r |
3214 | // The size of the VariableName, including the Unicode Null in bytes plus\r | |
188e4e84 | 3215 | // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r |
fa0737a8 | 3216 | // bytes for HwErrRec#### variable.\r |
72399dae | 3217 | //\r |
3218 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
fa0737a8 | 3219 | if (StrSize (VariableName) + PayloadSize > PcdGet32 (PcdMaxHardwareErrorVariableSize) - GetVariableHeaderSize ()) {\r |
72399dae | 3220 | return EFI_INVALID_PARAMETER;\r |
3221 | }\r | |
72399dae | 3222 | } else {\r |
3223 | //\r | |
3224 | // The size of the VariableName, including the Unicode Null in bytes plus\r | |
9b4a2032 | 3225 | // the DataSize is limited to maximum size of Max(Auth|Volatile)VariableSize bytes.\r |
72399dae | 3226 | //\r |
fa0737a8 SZ |
3227 | if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r |
3228 | if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ()) {\r | |
3229 | return EFI_INVALID_PARAMETER;\r | |
3230 | }\r | |
9b4a2032 | 3231 | } else if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r |
fa0737a8 SZ |
3232 | if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize ()) {\r |
3233 | return EFI_INVALID_PARAMETER;\r | |
3234 | }\r | |
9b4a2032 LE |
3235 | } else {\r |
3236 | if (StrSize (VariableName) + PayloadSize > mVariableModuleGlobal->MaxVolatileVariableSize - GetVariableHeaderSize ()) {\r | |
3237 | return EFI_INVALID_PARAMETER;\r | |
3238 | }\r | |
efb01a10 | 3239 | }\r |
6675a21f SZ |
3240 | }\r |
3241 | \r | |
2f6aa774 JY |
3242 | //\r |
3243 | // Special Handling for MOR Lock variable.\r | |
3244 | //\r | |
3245 | Status = SetVariableCheckHandlerMor (VariableName, VendorGuid, Attributes, PayloadSize, (VOID *) ((UINTN) Data + DataSize - PayloadSize));\r | |
3246 | if (Status == EFI_ALREADY_STARTED) {\r | |
3247 | //\r | |
3248 | // EFI_ALREADY_STARTED means the SetVariable() action is handled inside of SetVariableCheckHandlerMor().\r | |
3249 | // Variable driver can just return SUCCESS.\r | |
3250 | //\r | |
3251 | return EFI_SUCCESS;\r | |
3252 | }\r | |
3253 | if (EFI_ERROR (Status)) {\r | |
3254 | return Status;\r | |
3255 | }\r | |
3256 | \r | |
8021f4c7 | 3257 | Status = VarCheckLibSetVariableCheck (VariableName, VendorGuid, Attributes, PayloadSize, (VOID *) ((UINTN) Data + DataSize - PayloadSize), mRequestSource);\r |
fa0737a8 SZ |
3258 | if (EFI_ERROR (Status)) {\r |
3259 | return Status;\r | |
3260 | }\r | |
3261 | \r | |
72399dae | 3262 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
3263 | \r | |
3264 | //\r | |
8a2d4996 | 3265 | // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r |
72399dae | 3266 | //\r |
3267 | if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r | |
8a2d4996 | 3268 | Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r |
72399dae | 3269 | //\r |
8a2d4996 | 3270 | // Parse non-volatile variable data and get last variable offset.\r |
72399dae | 3271 | //\r |
3272 | NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r | |
6ebffb67 | 3273 | while (IsValidVariableHeader (NextVariable, GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point))) {\r |
72399dae | 3274 | NextVariable = GetNextVariablePtr (NextVariable);\r |
3275 | }\r | |
3276 | mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r | |
3277 | }\r | |
3278 | \r | |
3279 | //\r | |
8a2d4996 | 3280 | // Check whether the input variable is already existed.\r |
72399dae | 3281 | //\r |
9622df63 SZ |
3282 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, TRUE);\r |
3283 | if (!EFI_ERROR (Status)) {\r | |
3284 | if (((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) && AtRuntime ()) {\r | |
ff843847 RN |
3285 | Status = EFI_WRITE_PROTECTED;\r |
3286 | goto Done;\r | |
9622df63 | 3287 | }\r |
fa0737a8 | 3288 | if (Attributes != 0 && (Attributes & (~EFI_VARIABLE_APPEND_WRITE)) != Variable.CurrPtr->Attributes) {\r |
6e67fec0 SZ |
3289 | //\r |
3290 | // If a preexisting variable is rewritten with different attributes, SetVariable() shall not\r | |
3291 | // modify the variable and shall return EFI_INVALID_PARAMETER. Two exceptions to this rule:\r | |
3292 | // 1. No access attributes specified\r | |
3293 | // 2. The only attribute differing is EFI_VARIABLE_APPEND_WRITE\r | |
3294 | //\r | |
3295 | Status = EFI_INVALID_PARAMETER;\r | |
4edb1866 | 3296 | 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 |
3297 | goto Done;\r |
3298 | }\r | |
9622df63 | 3299 | }\r |
72399dae | 3300 | \r |
b2bd493e | 3301 | if (!FeaturePcdGet (PcdUefiVariableDefaultLangDeprecate)) {\r |
9bc5dabb | 3302 | //\r |
b2bd493e | 3303 | // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r |
9bc5dabb | 3304 | //\r |
b2bd493e SZ |
3305 | Status = AutoUpdateLangVariable (VariableName, Data, DataSize);\r |
3306 | if (EFI_ERROR (Status)) {\r | |
3307 | //\r | |
3308 | // The auto update operation failed, directly return to avoid inconsistency between PlatformLang and Lang.\r | |
3309 | //\r | |
3310 | goto Done;\r | |
3311 | }\r | |
9bc5dabb | 3312 | }\r |
72399dae | 3313 | \r |
fa0737a8 SZ |
3314 | if (mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r |
3315 | Status = AuthVariableLibProcessVariable (VariableName, VendorGuid, Data, DataSize, Attributes);\r | |
3316 | } else {\r | |
3317 | Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, 0, 0, &Variable, NULL);\r | |
3318 | }\r | |
72399dae | 3319 | \r |
ff843847 | 3320 | Done:\r |
fdb7765f | 3321 | InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r |
052ad7e1 | 3322 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
fdb7765f | 3323 | \r |
fa0737a8 SZ |
3324 | if (!AtRuntime ()) {\r |
3325 | if (!EFI_ERROR (Status)) {\r | |
3326 | SecureBootHook (\r | |
3327 | VariableName,\r | |
3328 | VendorGuid\r | |
3329 | );\r | |
3330 | }\r | |
3331 | }\r | |
3332 | \r | |
8d3a5c82 | 3333 | return Status;\r |
3334 | }\r | |
3335 | \r | |
7c80e839 | 3336 | /**\r |
8d3a5c82 | 3337 | \r |
3338 | This code returns information about the EFI variables.\r | |
3339 | \r | |
18a7dbbc SZ |
3340 | Caution: This function may receive untrusted input.\r |
3341 | This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r | |
3342 | \r | |
7c80e839 | 3343 | @param Attributes Attributes bitmask to specify the type of variables\r |
3344 | on which to return information.\r | |
3345 | @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r | |
3346 | for the EFI variables associated with the attributes specified.\r | |
3347 | @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r | |
3348 | for EFI variables associated with the attributes specified.\r | |
3349 | @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r | |
3350 | associated with the attributes specified.\r | |
8d3a5c82 | 3351 | \r |
7c80e839 | 3352 | @return EFI_SUCCESS Query successfully.\r |
8d3a5c82 | 3353 | \r |
7c80e839 | 3354 | **/\r |
052ad7e1 A |
3355 | EFI_STATUS\r |
3356 | EFIAPI\r | |
b2bd493e | 3357 | VariableServiceQueryVariableInfoInternal (\r |
052ad7e1 A |
3358 | IN UINT32 Attributes,\r |
3359 | OUT UINT64 *MaximumVariableStorageSize,\r | |
3360 | OUT UINT64 *RemainingVariableStorageSize,\r | |
3361 | OUT UINT64 *MaximumVariableSize\r | |
3362 | )\r | |
8d3a5c82 | 3363 | {\r |
3364 | VARIABLE_HEADER *Variable;\r | |
3365 | VARIABLE_HEADER *NextVariable;\r | |
3366 | UINT64 VariableSize;\r | |
3367 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
2fcdca1d | 3368 | UINT64 CommonVariableTotalSize;\r |
3369 | UINT64 HwErrVariableTotalSize;\r | |
b2bd493e SZ |
3370 | EFI_STATUS Status;\r |
3371 | VARIABLE_POINTER_TRACK VariablePtrTrack;\r | |
2fcdca1d | 3372 | \r |
3373 | CommonVariableTotalSize = 0;\r | |
3374 | HwErrVariableTotalSize = 0;\r | |
8d3a5c82 | 3375 | \r |
8d3a5c82 | 3376 | if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r |
3377 | //\r | |
3378 | // Query is Volatile related.\r | |
3379 | //\r | |
052ad7e1 | 3380 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r |
8d3a5c82 | 3381 | } else {\r |
3382 | //\r | |
3383 | // Query is Non-Volatile related.\r | |
3384 | //\r | |
8a2d4996 | 3385 | VariableStoreHeader = mNvVariableCache;\r |
8d3a5c82 | 3386 | }\r |
3387 | \r | |
3388 | //\r | |
3389 | // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r | |
3390 | // with the storage size (excluding the storage header size).\r | |
3391 | //\r | |
3392 | *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r | |
c6492839 | 3393 | \r |
3394 | //\r | |
3395 | // Harware error record variable needs larger size.\r | |
3396 | //\r | |
2fcdca1d | 3397 | if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r |
188e4e84 | 3398 | *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r |
fa0737a8 | 3399 | *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - GetVariableHeaderSize ();\r |
2fcdca1d | 3400 | } else {\r |
3401 | if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r | |
4edb1866 SZ |
3402 | if (AtRuntime ()) {\r |
3403 | *MaximumVariableStorageSize = mVariableModuleGlobal->CommonRuntimeVariableSpace;\r | |
3404 | } else {\r | |
3405 | *MaximumVariableStorageSize = mVariableModuleGlobal->CommonVariableSpace;\r | |
3406 | }\r | |
2fcdca1d | 3407 | }\r |
3408 | \r | |
3409 | //\r | |
9b4a2032 | 3410 | // Let *MaximumVariableSize be Max(Auth|Volatile)VariableSize with the exception of the variable header size.\r |
2fcdca1d | 3411 | //\r |
fa0737a8 SZ |
3412 | if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r |
3413 | *MaximumVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ();\r | |
9b4a2032 | 3414 | } else if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r |
fa0737a8 | 3415 | *MaximumVariableSize = mVariableModuleGlobal->MaxVariableSize - GetVariableHeaderSize ();\r |
9b4a2032 LE |
3416 | } else {\r |
3417 | *MaximumVariableSize = mVariableModuleGlobal->MaxVolatileVariableSize - GetVariableHeaderSize ();\r | |
fa0737a8 | 3418 | }\r |
c6492839 | 3419 | }\r |
8d3a5c82 | 3420 | \r |
3421 | //\r | |
3422 | // Point to the starting address of the variables.\r | |
3423 | //\r | |
9cad030b | 3424 | Variable = GetStartPointer (VariableStoreHeader);\r |
8d3a5c82 | 3425 | \r |
3426 | //\r | |
3427 | // Now walk through the related variable store.\r | |
3428 | //\r | |
6ebffb67 | 3429 | while (IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))) {\r |
8d3a5c82 | 3430 | NextVariable = GetNextVariablePtr (Variable);\r |
3431 | VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r | |
3432 | \r | |
8a2d4996 | 3433 | if (AtRuntime ()) {\r |
8d3a5c82 | 3434 | //\r |
8a2d4996 | 3435 | // We don't take the state of the variables in mind\r |
8d3a5c82 | 3436 | // when calculating RemainingVariableStorageSize,\r |
3437 | // since the space occupied by variables not marked with\r | |
3438 | // VAR_ADDED is not allowed to be reclaimed in Runtime.\r | |
3439 | //\r | |
3b425367 | 3440 | if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r |
2fcdca1d | 3441 | HwErrVariableTotalSize += VariableSize;\r |
3442 | } else {\r | |
3443 | CommonVariableTotalSize += VariableSize;\r | |
3444 | }\r | |
8d3a5c82 | 3445 | } else {\r |
3446 | //\r | |
8a2d4996 | 3447 | // Only care about Variables with State VAR_ADDED, because\r |
8d3a5c82 | 3448 | // the space not marked as VAR_ADDED is reclaimable now.\r |
3449 | //\r | |
3450 | if (Variable->State == VAR_ADDED) {\r | |
3b425367 | 3451 | if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r |
2fcdca1d | 3452 | HwErrVariableTotalSize += VariableSize;\r |
3453 | } else {\r | |
3454 | CommonVariableTotalSize += VariableSize;\r | |
3455 | }\r | |
b2bd493e SZ |
3456 | } else if (Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r |
3457 | //\r | |
3458 | // If it is a IN_DELETED_TRANSITION variable,\r | |
3459 | // and there is not also a same ADDED one at the same time,\r | |
3460 | // this IN_DELETED_TRANSITION variable is valid.\r | |
3461 | //\r | |
3462 | VariablePtrTrack.StartPtr = GetStartPointer (VariableStoreHeader);\r | |
3463 | VariablePtrTrack.EndPtr = GetEndPointer (VariableStoreHeader);\r | |
3464 | Status = FindVariableEx (\r | |
3465 | GetVariableNamePtr (Variable),\r | |
fa0737a8 | 3466 | GetVendorGuidPtr (Variable),\r |
b2bd493e SZ |
3467 | FALSE,\r |
3468 | &VariablePtrTrack\r | |
3469 | );\r | |
3470 | if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State != VAR_ADDED) {\r | |
3471 | if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
3472 | HwErrVariableTotalSize += VariableSize;\r | |
3473 | } else {\r | |
3474 | CommonVariableTotalSize += VariableSize;\r | |
3475 | }\r | |
3476 | }\r | |
8d3a5c82 | 3477 | }\r |
3478 | }\r | |
3479 | \r | |
3480 | //\r | |
8a2d4996 | 3481 | // Go to the next one.\r |
8d3a5c82 | 3482 | //\r |
3483 | Variable = NextVariable;\r | |
3484 | }\r | |
3485 | \r | |
2fcdca1d | 3486 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r |
3487 | *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r | |
4edb1866 SZ |
3488 | } else {\r |
3489 | if (*MaximumVariableStorageSize < CommonVariableTotalSize) {\r | |
3490 | *RemainingVariableStorageSize = 0;\r | |
3491 | } else {\r | |
3492 | *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r | |
3493 | }\r | |
2fcdca1d | 3494 | }\r |
3495 | \r | |
fa0737a8 | 3496 | if (*RemainingVariableStorageSize < GetVariableHeaderSize ()) {\r |
c6492839 | 3497 | *MaximumVariableSize = 0;\r |
fa0737a8 SZ |
3498 | } else if ((*RemainingVariableStorageSize - GetVariableHeaderSize ()) < *MaximumVariableSize) {\r |
3499 | *MaximumVariableSize = *RemainingVariableStorageSize - GetVariableHeaderSize ();\r | |
c6492839 | 3500 | }\r |
3501 | \r | |
8d3a5c82 | 3502 | return EFI_SUCCESS;\r |
3503 | }\r | |
3504 | \r | |
b2bd493e SZ |
3505 | /**\r |
3506 | \r | |
3507 | This code returns information about the EFI variables.\r | |
3508 | \r | |
18a7dbbc SZ |
3509 | Caution: This function may receive untrusted input.\r |
3510 | This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r | |
3511 | \r | |
b2bd493e SZ |
3512 | @param Attributes Attributes bitmask to specify the type of variables\r |
3513 | on which to return information.\r | |
3514 | @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r | |
3515 | for the EFI variables associated with the attributes specified.\r | |
3516 | @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r | |
3517 | for EFI variables associated with the attributes specified.\r | |
3518 | @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r | |
3519 | associated with the attributes specified.\r | |
3520 | \r | |
3521 | @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r | |
3522 | @return EFI_SUCCESS Query successfully.\r | |
3523 | @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r | |
3524 | \r | |
3525 | **/\r | |
3526 | EFI_STATUS\r | |
3527 | EFIAPI\r | |
3528 | VariableServiceQueryVariableInfo (\r | |
3529 | IN UINT32 Attributes,\r | |
3530 | OUT UINT64 *MaximumVariableStorageSize,\r | |
3531 | OUT UINT64 *RemainingVariableStorageSize,\r | |
3532 | OUT UINT64 *MaximumVariableSize\r | |
3533 | )\r | |
3534 | {\r | |
3535 | EFI_STATUS Status;\r | |
3536 | \r | |
3537 | if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r | |
3538 | return EFI_INVALID_PARAMETER;\r | |
3539 | }\r | |
3540 | \r | |
67943427 | 3541 | if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r |
3542 | //\r | |
3543 | // Deprecated attribute, make this check as highest priority.\r | |
3544 | //\r | |
3545 | return EFI_UNSUPPORTED;\r | |
3546 | }\r | |
3547 | \r | |
fa0737a8 | 3548 | if ((Attributes & EFI_VARIABLE_ATTRIBUTES_MASK) == 0) {\r |
b2bd493e SZ |
3549 | //\r |
3550 | // Make sure the Attributes combination is supported by the platform.\r | |
3551 | //\r | |
3552 | return EFI_UNSUPPORTED;\r | |
3553 | } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r | |
3554 | //\r | |
3555 | // Make sure if runtime bit is set, boot service bit is set also.\r | |
3556 | //\r | |
3557 | return EFI_INVALID_PARAMETER;\r | |
3558 | } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r | |
3559 | //\r | |
3560 | // Make sure RT Attribute is set if we are in Runtime phase.\r | |
3561 | //\r | |
3562 | return EFI_INVALID_PARAMETER;\r | |
3563 | } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
3564 | //\r | |
3565 | // Make sure Hw Attribute is set with NV.\r | |
3566 | //\r | |
3567 | return EFI_INVALID_PARAMETER;\r | |
fa0737a8 SZ |
3568 | } else if ((Attributes & VARIABLE_ATTRIBUTE_AT_AW) != 0) {\r |
3569 | if (!mVariableModuleGlobal->VariableGlobal.AuthSupport) {\r | |
3570 | //\r | |
3571 | // Not support authenticated variable write.\r | |
3572 | //\r | |
3573 | return EFI_UNSUPPORTED;\r | |
3574 | }\r | |
3575 | } else if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r | |
3576 | if (PcdGet32 (PcdHwErrStorageSize) == 0) {\r | |
3577 | //\r | |
3578 | // Not support harware error record variable variable.\r | |
3579 | //\r | |
3580 | return EFI_UNSUPPORTED;\r | |
3581 | }\r | |
b2bd493e SZ |
3582 | }\r |
3583 | \r | |
3584 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r | |
3585 | \r | |
3586 | Status = VariableServiceQueryVariableInfoInternal (\r | |
3587 | Attributes,\r | |
3588 | MaximumVariableStorageSize,\r | |
3589 | RemainingVariableStorageSize,\r | |
3590 | MaximumVariableSize\r | |
3591 | );\r | |
3592 | \r | |
3593 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r | |
3594 | return Status;\r | |
3595 | }\r | |
7c80e839 | 3596 | \r |
3597 | /**\r | |
8a2d4996 | 3598 | This function reclaims variable storage if free size is below the threshold.\r |
18a7dbbc SZ |
3599 | \r |
3600 | Caution: This function may be invoked at SMM mode.\r | |
3601 | Care must be taken to make sure not security issue.\r | |
3602 | \r | |
7c80e839 | 3603 | **/\r |
7800593d | 3604 | VOID\r |
7800593d | 3605 | ReclaimForOS(\r |
8a2d4996 | 3606 | VOID\r |
7800593d LG |
3607 | )\r |
3608 | {\r | |
9948c0b0 | 3609 | EFI_STATUS Status;\r |
4edb1866 | 3610 | UINTN RemainingCommonRuntimeVariableSpace;\r |
2fcdca1d | 3611 | UINTN RemainingHwErrVariableSpace;\r |
0fb5e515 SZ |
3612 | STATIC BOOLEAN Reclaimed;\r |
3613 | \r | |
3614 | //\r | |
3615 | // This function will be called only once at EndOfDxe or ReadyToBoot event.\r | |
3616 | //\r | |
3617 | if (Reclaimed) {\r | |
3618 | return;\r | |
3619 | }\r | |
3620 | Reclaimed = TRUE;\r | |
7800593d | 3621 | \r |
4edb1866 | 3622 | Status = EFI_SUCCESS;\r |
7800593d | 3623 | \r |
4edb1866 SZ |
3624 | if (mVariableModuleGlobal->CommonRuntimeVariableSpace < mVariableModuleGlobal->CommonVariableTotalSize) {\r |
3625 | RemainingCommonRuntimeVariableSpace = 0;\r | |
3626 | } else {\r | |
3627 | RemainingCommonRuntimeVariableSpace = mVariableModuleGlobal->CommonRuntimeVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r | |
3628 | }\r | |
2fcdca1d | 3629 | \r |
3630 | RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r | |
fa0737a8 | 3631 | \r |
7800593d | 3632 | //\r |
4edb1866 | 3633 | // Check if the free area is below a threshold.\r |
7800593d | 3634 | //\r |
fa0737a8 SZ |
3635 | if (((RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxVariableSize) ||\r |
3636 | (RemainingCommonRuntimeVariableSpace < mVariableModuleGlobal->MaxAuthVariableSize)) ||\r | |
3637 | ((PcdGet32 (PcdHwErrStorageSize) != 0) &&\r | |
9948c0b0 | 3638 | (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r |
7800593d | 3639 | Status = Reclaim (\r |
2fcdca1d | 3640 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r |
3641 | &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r | |
3642 | FALSE,\r | |
335e2681 | 3643 | NULL,\r |
7baf3c69 SZ |
3644 | NULL,\r |
3645 | 0\r | |
2fcdca1d | 3646 | );\r |
7800593d LG |
3647 | ASSERT_EFI_ERROR (Status);\r |
3648 | }\r | |
3649 | }\r | |
3650 | \r | |
fa0737a8 SZ |
3651 | /**\r |
3652 | Get non-volatile maximum variable size.\r | |
3653 | \r | |
3654 | @return Non-volatile maximum variable size.\r | |
3655 | \r | |
3656 | **/\r | |
3657 | UINTN\r | |
3658 | GetNonVolatileMaxVariableSize (\r | |
3659 | VOID\r | |
3660 | )\r | |
3661 | {\r | |
3662 | if (PcdGet32 (PcdHwErrStorageSize) != 0) {\r | |
3663 | return MAX (MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize)),\r | |
3664 | PcdGet32 (PcdMaxHardwareErrorVariableSize));\r | |
3665 | } else {\r | |
3666 | return MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariableSize));\r | |
3667 | }\r | |
3668 | }\r | |
3669 | \r | |
9b4a2032 LE |
3670 | /**\r |
3671 | Get maximum variable size, covering both non-volatile and volatile variables.\r | |
3672 | \r | |
3673 | @return Maximum variable size.\r | |
3674 | \r | |
3675 | **/\r | |
3676 | UINTN\r | |
3677 | GetMaxVariableSize (\r | |
3678 | VOID\r | |
3679 | )\r | |
3680 | {\r | |
3681 | UINTN MaxVariableSize;\r | |
3682 | \r | |
3683 | MaxVariableSize = GetNonVolatileMaxVariableSize();\r | |
3684 | //\r | |
3685 | // The condition below fails implicitly if PcdMaxVolatileVariableSize equals\r | |
3686 | // the default zero value.\r | |
3687 | //\r | |
3688 | if (MaxVariableSize < PcdGet32 (PcdMaxVolatileVariableSize)) {\r | |
3689 | MaxVariableSize = PcdGet32 (PcdMaxVolatileVariableSize);\r | |
3690 | }\r | |
3691 | return MaxVariableSize;\r | |
3692 | }\r | |
3693 | \r | |
3e02ebb2 SZ |
3694 | /**\r |
3695 | Init non-volatile variable store.\r | |
3696 | \r | |
fa0737a8 SZ |
3697 | @param[out] NvFvHeader Output pointer to non-volatile FV header address.\r |
3698 | \r | |
3e02ebb2 SZ |
3699 | @retval EFI_SUCCESS Function successfully executed.\r |
3700 | @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r | |
3701 | @retval EFI_VOLUME_CORRUPTED Variable Store or Firmware Volume for Variable Store is corrupted.\r | |
3702 | \r | |
3703 | **/\r | |
3704 | EFI_STATUS\r | |
3705 | InitNonVolatileVariableStore (\r | |
fa0737a8 | 3706 | OUT EFI_FIRMWARE_VOLUME_HEADER **NvFvHeader\r |
3e02ebb2 SZ |
3707 | )\r |
3708 | {\r | |
3709 | EFI_FIRMWARE_VOLUME_HEADER *FvHeader;\r | |
4edb1866 | 3710 | VARIABLE_HEADER *Variable;\r |
3e02ebb2 SZ |
3711 | VARIABLE_HEADER *NextVariable;\r |
3712 | EFI_PHYSICAL_ADDRESS VariableStoreBase;\r | |
3713 | UINT64 VariableStoreLength;\r | |
3714 | UINTN VariableSize;\r | |
3715 | EFI_HOB_GUID_TYPE *GuidHob;\r | |
3716 | EFI_PHYSICAL_ADDRESS NvStorageBase;\r | |
3717 | UINT8 *NvStorageData;\r | |
3718 | UINT32 NvStorageSize;\r | |
3719 | FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *FtwLastWriteData;\r | |
3720 | UINT32 BackUpOffset;\r | |
3721 | UINT32 BackUpSize;\r | |
4edb1866 SZ |
3722 | UINT32 HwErrStorageSize;\r |
3723 | UINT32 MaxUserNvVariableSpaceSize;\r | |
3724 | UINT32 BoottimeReservedNvVariableSpaceSize;\r | |
14326ed0 SZ |
3725 | EFI_STATUS Status;\r |
3726 | VOID *FtwProtocol;\r | |
3e02ebb2 SZ |
3727 | \r |
3728 | mVariableModuleGlobal->FvbInstance = NULL;\r | |
3729 | \r | |
3e02ebb2 SZ |
3730 | //\r |
3731 | // Allocate runtime memory used for a memory copy of the FLASH region.\r | |
3732 | // Keep the memory and the FLASH in sync as updates occur.\r | |
3733 | //\r | |
3734 | NvStorageSize = PcdGet32 (PcdFlashNvStorageVariableSize);\r | |
3735 | NvStorageData = AllocateRuntimeZeroPool (NvStorageSize);\r | |
3736 | if (NvStorageData == NULL) {\r | |
3737 | return EFI_OUT_OF_RESOURCES;\r | |
3738 | }\r | |
3739 | \r | |
3740 | NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r | |
3741 | if (NvStorageBase == 0) {\r | |
3742 | NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r | |
3743 | }\r | |
3744 | //\r | |
3745 | // Copy NV storage data to the memory buffer.\r | |
3746 | //\r | |
3747 | CopyMem (NvStorageData, (UINT8 *) (UINTN) NvStorageBase, NvStorageSize);\r | |
3748 | \r | |
14326ed0 | 3749 | Status = GetFtwProtocol ((VOID **)&FtwProtocol);\r |
3e02ebb2 | 3750 | //\r |
14326ed0 | 3751 | // If FTW protocol has been installed, no need to check FTW last write data hob.\r |
3e02ebb2 | 3752 | //\r |
14326ed0 SZ |
3753 | if (EFI_ERROR (Status)) {\r |
3754 | //\r | |
3755 | // Check the FTW last write data hob.\r | |
3756 | //\r | |
3757 | GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid);\r | |
3758 | if (GuidHob != NULL) {\r | |
3759 | FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *) GET_GUID_HOB_DATA (GuidHob);\r | |
3760 | if (FtwLastWriteData->TargetAddress == NvStorageBase) {\r | |
3761 | DEBUG ((EFI_D_INFO, "Variable: NV storage is backed up in spare block: 0x%x\n", (UINTN) FtwLastWriteData->SpareAddress));\r | |
3762 | //\r | |
3763 | // Copy the backed up NV storage data to the memory buffer from spare block.\r | |
3764 | //\r | |
3765 | CopyMem (NvStorageData, (UINT8 *) (UINTN) (FtwLastWriteData->SpareAddress), NvStorageSize);\r | |
3766 | } else if ((FtwLastWriteData->TargetAddress > NvStorageBase) &&\r | |
3767 | (FtwLastWriteData->TargetAddress < (NvStorageBase + NvStorageSize))) {\r | |
3768 | //\r | |
3769 | // Flash NV storage from the Offset is backed up in spare block.\r | |
3770 | //\r | |
3771 | BackUpOffset = (UINT32) (FtwLastWriteData->TargetAddress - NvStorageBase);\r | |
3772 | BackUpSize = NvStorageSize - BackUpOffset;\r | |
3773 | 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 | |
3774 | //\r | |
3775 | // Copy the partial backed up NV storage data to the memory buffer from spare block.\r | |
3776 | //\r | |
3777 | CopyMem (NvStorageData + BackUpOffset, (UINT8 *) (UINTN) FtwLastWriteData->SpareAddress, BackUpSize);\r | |
3778 | }\r | |
3e02ebb2 SZ |
3779 | }\r |
3780 | }\r | |
3781 | \r | |
3782 | FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *) NvStorageData;\r | |
3783 | \r | |
3784 | //\r | |
3785 | // Check if the Firmware Volume is not corrupted\r | |
3786 | //\r | |
3787 | if ((FvHeader->Signature != EFI_FVH_SIGNATURE) || (!CompareGuid (&gEfiSystemNvDataFvGuid, &FvHeader->FileSystemGuid))) {\r | |
3788 | FreePool (NvStorageData);\r | |
3789 | DEBUG ((EFI_D_ERROR, "Firmware Volume for Variable Store is corrupted\n"));\r | |
3790 | return EFI_VOLUME_CORRUPTED;\r | |
3791 | }\r | |
3792 | \r | |
16f69227 HW |
3793 | VariableStoreBase = (UINTN) FvHeader + FvHeader->HeaderLength;\r |
3794 | VariableStoreLength = NvStorageSize - FvHeader->HeaderLength;\r | |
3e02ebb2 | 3795 | \r |
9b18845a | 3796 | mNvFvHeaderCache = FvHeader;\r |
3e02ebb2 SZ |
3797 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r |
3798 | mNvVariableCache = (VARIABLE_STORE_HEADER *) (UINTN) VariableStoreBase;\r | |
3799 | if (GetVariableStoreStatus (mNvVariableCache) != EfiValid) {\r | |
3800 | FreePool (NvStorageData);\r | |
9b18845a DL |
3801 | mNvFvHeaderCache = NULL;\r |
3802 | mNvVariableCache = NULL;\r | |
3e02ebb2 SZ |
3803 | DEBUG((EFI_D_ERROR, "Variable Store header is corrupted\n"));\r |
3804 | return EFI_VOLUME_CORRUPTED;\r | |
3805 | }\r | |
3806 | ASSERT(mNvVariableCache->Size == VariableStoreLength);\r | |
3807 | \r | |
4edb1866 SZ |
3808 | ASSERT (sizeof (VARIABLE_STORE_HEADER) <= VariableStoreLength);\r |
3809 | \r | |
fa0737a8 SZ |
3810 | mVariableModuleGlobal->VariableGlobal.AuthFormat = (BOOLEAN)(CompareGuid (&mNvVariableCache->Signature, &gEfiAuthenticatedVariableGuid));\r |
3811 | \r | |
4edb1866 SZ |
3812 | HwErrStorageSize = PcdGet32 (PcdHwErrStorageSize);\r |
3813 | MaxUserNvVariableSpaceSize = PcdGet32 (PcdMaxUserNvVariableSpaceSize);\r | |
3814 | BoottimeReservedNvVariableSpaceSize = PcdGet32 (PcdBoottimeReservedNvVariableSpaceSize);\r | |
3815 | \r | |
3816 | //\r | |
3817 | // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r | |
3818 | // is stored with common variable in the same NV region. So the platform integrator should\r | |
3819 | // ensure that the value of PcdHwErrStorageSize is less than the value of\r | |
fa0737a8 | 3820 | // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r |
4edb1866 SZ |
3821 | //\r |
3822 | ASSERT (HwErrStorageSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r | |
3823 | //\r | |
3824 | // Ensure that the value of PcdMaxUserNvVariableSpaceSize is less than the value of\r | |
fa0737a8 | 3825 | // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r |
4edb1866 SZ |
3826 | //\r |
3827 | ASSERT (MaxUserNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r | |
3828 | //\r | |
3829 | // Ensure that the value of PcdBoottimeReservedNvVariableSpaceSize is less than the value of\r | |
fa0737a8 | 3830 | // (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)) - PcdGet32 (PcdHwErrStorageSize).\r |
4edb1866 SZ |
3831 | //\r |
3832 | ASSERT (BoottimeReservedNvVariableSpaceSize < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize));\r | |
3833 | \r | |
3834 | mVariableModuleGlobal->CommonVariableSpace = ((UINTN) VariableStoreLength - sizeof (VARIABLE_STORE_HEADER) - HwErrStorageSize);\r | |
3835 | mVariableModuleGlobal->CommonMaxUserVariableSpace = ((MaxUserNvVariableSpaceSize != 0) ? MaxUserNvVariableSpaceSize : mVariableModuleGlobal->CommonVariableSpace);\r | |
3836 | mVariableModuleGlobal->CommonRuntimeVariableSpace = mVariableModuleGlobal->CommonVariableSpace - BoottimeReservedNvVariableSpaceSize;\r | |
3837 | \r | |
3838 | DEBUG ((EFI_D_INFO, "Variable driver common space: 0x%x 0x%x 0x%x\n", mVariableModuleGlobal->CommonVariableSpace, mVariableModuleGlobal->CommonMaxUserVariableSpace, mVariableModuleGlobal->CommonRuntimeVariableSpace));\r | |
3839 | \r | |
3e02ebb2 | 3840 | //\r |
fa0737a8 | 3841 | // The max NV variable size should be < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)).\r |
3e02ebb2 | 3842 | //\r |
fa0737a8 SZ |
3843 | ASSERT (GetNonVolatileMaxVariableSize () < (VariableStoreLength - sizeof (VARIABLE_STORE_HEADER)));\r |
3844 | \r | |
3845 | mVariableModuleGlobal->MaxVariableSize = PcdGet32 (PcdMaxVariableSize);\r | |
3846 | mVariableModuleGlobal->MaxAuthVariableSize = ((PcdGet32 (PcdMaxAuthVariableSize) != 0) ? PcdGet32 (PcdMaxAuthVariableSize) : mVariableModuleGlobal->MaxVariableSize);\r | |
3e02ebb2 SZ |
3847 | \r |
3848 | //\r | |
3849 | // Parse non-volatile variable data and get last variable offset.\r | |
3850 | //\r | |
4edb1866 SZ |
3851 | Variable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r |
3852 | while (IsValidVariableHeader (Variable, GetEndPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase))) {\r | |
3853 | NextVariable = GetNextVariablePtr (Variable);\r | |
3854 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r | |
3855 | if ((Variable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r | |
3856 | mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r | |
3e02ebb2 | 3857 | } else {\r |
4edb1866 | 3858 | mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r |
3e02ebb2 SZ |
3859 | }\r |
3860 | \r | |
4edb1866 | 3861 | Variable = NextVariable;\r |
3e02ebb2 | 3862 | }\r |
4edb1866 | 3863 | mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) Variable - (UINTN) VariableStoreBase;\r |
3e02ebb2 | 3864 | \r |
fa0737a8 | 3865 | *NvFvHeader = FvHeader;\r |
3e02ebb2 SZ |
3866 | return EFI_SUCCESS;\r |
3867 | }\r | |
3868 | \r | |
335e2681 SZ |
3869 | /**\r |
3870 | Flush the HOB variable to flash.\r | |
3871 | \r | |
3872 | @param[in] VariableName Name of variable has been updated or deleted.\r | |
3873 | @param[in] VendorGuid Guid of variable has been updated or deleted.\r | |
3874 | \r | |
3875 | **/\r | |
3876 | VOID\r | |
3877 | FlushHobVariableToFlash (\r | |
3878 | IN CHAR16 *VariableName,\r | |
3879 | IN EFI_GUID *VendorGuid\r | |
3880 | )\r | |
3881 | {\r | |
3882 | EFI_STATUS Status;\r | |
3883 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
3884 | VARIABLE_HEADER *Variable;\r | |
3885 | VOID *VariableData;\r | |
31349131 | 3886 | VARIABLE_POINTER_TRACK VariablePtrTrack;\r |
335e2681 SZ |
3887 | BOOLEAN ErrorFlag;\r |
3888 | \r | |
3889 | ErrorFlag = FALSE;\r | |
3890 | \r | |
3891 | //\r | |
3892 | // Flush the HOB variable to flash.\r | |
3893 | //\r | |
3894 | if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r | |
3895 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase;\r | |
3896 | //\r | |
3897 | // Set HobVariableBase to 0, it can avoid SetVariable to call back.\r | |
3898 | //\r | |
3899 | mVariableModuleGlobal->VariableGlobal.HobVariableBase = 0;\r | |
3900 | for ( Variable = GetStartPointer (VariableStoreHeader)\r | |
6ebffb67 | 3901 | ; IsValidVariableHeader (Variable, GetEndPointer (VariableStoreHeader))\r |
335e2681 SZ |
3902 | ; Variable = GetNextVariablePtr (Variable)\r |
3903 | ) {\r | |
3904 | if (Variable->State != VAR_ADDED) {\r | |
3905 | //\r | |
3906 | // The HOB variable has been set to DELETED state in local.\r | |
3907 | //\r | |
3908 | continue;\r | |
3909 | }\r | |
3910 | ASSERT ((Variable->Attributes & EFI_VARIABLE_NON_VOLATILE) != 0);\r | |
3911 | if (VendorGuid == NULL || VariableName == NULL ||\r | |
fa0737a8 | 3912 | !CompareGuid (VendorGuid, GetVendorGuidPtr (Variable)) ||\r |
335e2681 SZ |
3913 | StrCmp (VariableName, GetVariableNamePtr (Variable)) != 0) {\r |
3914 | VariableData = GetVariableDataPtr (Variable);\r | |
31349131 SZ |
3915 | FindVariable (GetVariableNamePtr (Variable), GetVendorGuidPtr (Variable), &VariablePtrTrack, &mVariableModuleGlobal->VariableGlobal, FALSE);\r |
3916 | Status = UpdateVariable (\r | |
335e2681 | 3917 | GetVariableNamePtr (Variable),\r |
fa0737a8 | 3918 | GetVendorGuidPtr (Variable),\r |
31349131 | 3919 | VariableData,\r |
fa0737a8 | 3920 | DataSizeOfVariable (Variable),\r |
31349131 SZ |
3921 | Variable->Attributes,\r |
3922 | 0,\r | |
3923 | 0,\r | |
3924 | &VariablePtrTrack,\r | |
3925 | NULL\r | |
3926 | );\r | |
fa0737a8 | 3927 | DEBUG ((EFI_D_INFO, "Variable driver flush the HOB variable to flash: %g %s %r\n", GetVendorGuidPtr (Variable), GetVariableNamePtr (Variable), Status));\r |
335e2681 SZ |
3928 | } else {\r |
3929 | //\r | |
31349131 | 3930 | // The updated or deleted variable is matched with this HOB variable.\r |
335e2681 SZ |
3931 | // Don't break here because we will try to set other HOB variables\r |
3932 | // since this variable could be set successfully.\r | |
3933 | //\r | |
3934 | Status = EFI_SUCCESS;\r | |
3935 | }\r | |
3936 | if (!EFI_ERROR (Status)) {\r | |
3937 | //\r | |
3938 | // If set variable successful, or the updated or deleted variable is matched with the HOB variable,\r | |
3939 | // set the HOB variable to DELETED state in local.\r | |
3940 | //\r | |
fa0737a8 | 3941 | 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 |
3942 | Variable->State &= VAR_DELETED;\r |
3943 | } else {\r | |
3944 | ErrorFlag = TRUE;\r | |
3945 | }\r | |
3946 | }\r | |
3947 | if (ErrorFlag) {\r | |
3948 | //\r | |
3949 | // We still have HOB variable(s) not flushed in flash.\r | |
3950 | //\r | |
3951 | mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStoreHeader;\r | |
3952 | } else {\r | |
3953 | //\r | |
3954 | // All HOB variables have been flushed in flash.\r | |
3955 | //\r | |
3956 | DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been flushed in flash.\n"));\r | |
3957 | if (!AtRuntime ()) {\r | |
3958 | FreePool ((VOID *) VariableStoreHeader);\r | |
3959 | }\r | |
3960 | }\r | |
3961 | }\r | |
3962 | \r | |
3963 | }\r | |
8a2d4996 | 3964 | \r |
7c80e839 | 3965 | /**\r |
3e02ebb2 | 3966 | Initializes variable write service after FTW was ready.\r |
7c80e839 | 3967 | \r |
8a2d4996 | 3968 | @retval EFI_SUCCESS Function successfully executed.\r |
3969 | @retval Others Fail to initialize the variable service.\r | |
3970 | \r | |
3971 | **/\r | |
3972 | EFI_STATUS\r | |
3973 | VariableWriteServiceInitialize (\r | |
3974 | VOID\r | |
3975 | )\r | |
3976 | {\r | |
3977 | EFI_STATUS Status;\r | |
8a2d4996 | 3978 | UINTN Index;\r |
3979 | UINT8 Data;\r | |
8a2d4996 | 3980 | EFI_PHYSICAL_ADDRESS VariableStoreBase;\r |
3e02ebb2 | 3981 | EFI_PHYSICAL_ADDRESS NvStorageBase;\r |
fa0737a8 | 3982 | VARIABLE_ENTRY_PROPERTY *VariableEntry;\r |
8a2d4996 | 3983 | \r |
31349131 SZ |
3984 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
3985 | \r | |
3e02ebb2 SZ |
3986 | NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r |
3987 | if (NvStorageBase == 0) {\r | |
3988 | NvStorageBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r | |
3989 | }\r | |
9b18845a | 3990 | VariableStoreBase = NvStorageBase + (mNvFvHeaderCache->HeaderLength);\r |
3e02ebb2 SZ |
3991 | \r |
3992 | //\r | |
3993 | // Let NonVolatileVariableBase point to flash variable store base directly after FTW ready.\r | |
3994 | //\r | |
3995 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r | |
fa0737a8 | 3996 | \r |
8a2d4996 | 3997 | //\r |
3998 | // Check if the free area is really free.\r | |
3999 | //\r | |
9b18845a | 4000 | for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < mNvVariableCache->Size; Index++) {\r |
8a2d4996 | 4001 | Data = ((UINT8 *) mNvVariableCache)[Index];\r |
4002 | if (Data != 0xff) {\r | |
4003 | //\r | |
4004 | // There must be something wrong in variable store, do reclaim operation.\r | |
4005 | //\r | |
4006 | Status = Reclaim (\r | |
4007 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r | |
4008 | &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r | |
4009 | FALSE,\r | |
335e2681 | 4010 | NULL,\r |
7baf3c69 SZ |
4011 | NULL,\r |
4012 | 0\r | |
8a2d4996 | 4013 | );\r |
4014 | if (EFI_ERROR (Status)) {\r | |
31349131 | 4015 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8a2d4996 | 4016 | return Status;\r |
4017 | }\r | |
4018 | break;\r | |
4019 | }\r | |
4020 | }\r | |
4021 | \r | |
335e2681 | 4022 | FlushHobVariableToFlash (NULL, NULL);\r |
0f7aff72 | 4023 | \r |
fa0737a8 | 4024 | Status = EFI_SUCCESS;\r |
8021f4c7 | 4025 | ZeroMem (&mAuthContextOut, sizeof (mAuthContextOut));\r |
fa0737a8 SZ |
4026 | if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r |
4027 | //\r | |
4028 | // Authenticated variable initialize.\r | |
4029 | //\r | |
8021f4c7 SZ |
4030 | mAuthContextIn.StructSize = sizeof (AUTH_VAR_LIB_CONTEXT_IN);\r |
4031 | mAuthContextIn.MaxAuthVariableSize = mVariableModuleGlobal->MaxAuthVariableSize - GetVariableHeaderSize ();\r | |
4032 | Status = AuthVariableLibInitialize (&mAuthContextIn, &mAuthContextOut);\r | |
fa0737a8 SZ |
4033 | if (!EFI_ERROR (Status)) {\r |
4034 | DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable support!\n"));\r | |
4035 | mVariableModuleGlobal->VariableGlobal.AuthSupport = TRUE;\r | |
8021f4c7 SZ |
4036 | if (mAuthContextOut.AuthVarEntry != NULL) {\r |
4037 | for (Index = 0; Index < mAuthContextOut.AuthVarEntryCount; Index++) {\r | |
4038 | VariableEntry = &mAuthContextOut.AuthVarEntry[Index];\r | |
4039 | Status = VarCheckLibVariablePropertySet (\r | |
fa0737a8 SZ |
4040 | VariableEntry->Name,\r |
4041 | VariableEntry->Guid,\r | |
4042 | &VariableEntry->VariableProperty\r | |
4043 | );\r | |
4044 | ASSERT_EFI_ERROR (Status);\r | |
4045 | }\r | |
4046 | }\r | |
4047 | } else if (Status == EFI_UNSUPPORTED) {\r | |
4048 | DEBUG ((EFI_D_INFO, "NOTICE - AuthVariableLibInitialize() returns %r!\n", Status));\r | |
4049 | DEBUG ((EFI_D_INFO, "Variable driver will continue to work without auth variable support!\n"));\r | |
4050 | mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;\r | |
4051 | Status = EFI_SUCCESS;\r | |
4052 | }\r | |
4053 | }\r | |
4054 | \r | |
4055 | if (!EFI_ERROR (Status)) {\r | |
33f615e2 | 4056 | for (Index = 0; Index < ARRAY_SIZE (mVariableEntryProperty); Index++) {\r |
fa0737a8 | 4057 | VariableEntry = &mVariableEntryProperty[Index];\r |
8021f4c7 | 4058 | Status = VarCheckLibVariablePropertySet (VariableEntry->Name, VariableEntry->Guid, &VariableEntry->VariableProperty);\r |
fa0737a8 SZ |
4059 | ASSERT_EFI_ERROR (Status);\r |
4060 | }\r | |
4061 | }\r | |
4062 | \r | |
31349131 | 4063 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
2f6aa774 JY |
4064 | \r |
4065 | //\r | |
4066 | // Initialize MOR Lock variable.\r | |
4067 | //\r | |
4068 | MorLockInit ();\r | |
4069 | \r | |
fa0737a8 | 4070 | return Status;\r |
8a2d4996 | 4071 | }\r |
4072 | \r | |
a37044ef LG |
4073 | /**\r |
4074 | Convert normal variable storage to the allocated auth variable storage.\r | |
4075 | \r | |
4076 | @param[in] NormalVarStorage Pointer to the normal variable storage header\r | |
4077 | \r | |
4078 | @retval the allocated auth variable storage\r | |
4079 | **/\r | |
4080 | VOID *\r | |
4081 | ConvertNormalVarStorageToAuthVarStorage (\r | |
4082 | VARIABLE_STORE_HEADER *NormalVarStorage\r | |
4083 | )\r | |
4084 | {\r | |
4085 | VARIABLE_HEADER *StartPtr;\r | |
4086 | UINT8 *NextPtr;\r | |
4087 | VARIABLE_HEADER *EndPtr;\r | |
4088 | UINTN AuthVarStroageSize;\r | |
4089 | AUTHENTICATED_VARIABLE_HEADER *AuthStartPtr;\r | |
4090 | VARIABLE_STORE_HEADER *AuthVarStorage;\r | |
4091 | \r | |
4092 | AuthVarStroageSize = sizeof (VARIABLE_STORE_HEADER);\r | |
4093 | //\r | |
4094 | // Set AuthFormat as FALSE for normal variable storage\r | |
4095 | //\r | |
4096 | mVariableModuleGlobal->VariableGlobal.AuthFormat = FALSE;\r | |
4097 | \r | |
4098 | //\r | |
4099 | // Calculate Auth Variable Storage Size\r | |
4100 | //\r | |
4101 | StartPtr = GetStartPointer (NormalVarStorage);\r | |
4102 | EndPtr = GetEndPointer (NormalVarStorage);\r | |
4103 | while (StartPtr < EndPtr) {\r | |
4104 | if (StartPtr->State == VAR_ADDED) {\r | |
4105 | AuthVarStroageSize = HEADER_ALIGN (AuthVarStroageSize);\r | |
4106 | AuthVarStroageSize += sizeof (AUTHENTICATED_VARIABLE_HEADER);\r | |
4107 | AuthVarStroageSize += StartPtr->NameSize + GET_PAD_SIZE (StartPtr->NameSize);\r | |
4108 | AuthVarStroageSize += StartPtr->DataSize + GET_PAD_SIZE (StartPtr->DataSize);\r | |
4109 | }\r | |
4110 | StartPtr = GetNextVariablePtr (StartPtr);\r | |
4111 | }\r | |
4112 | \r | |
4113 | //\r | |
4114 | // Allocate Runtime memory for Auth Variable Storage\r | |
4115 | //\r | |
4116 | AuthVarStorage = AllocateRuntimeZeroPool (AuthVarStroageSize);\r | |
4117 | ASSERT (AuthVarStorage != NULL);\r | |
4118 | if (AuthVarStorage == NULL) {\r | |
4119 | return NULL;\r | |
4120 | }\r | |
4121 | \r | |
4122 | //\r | |
4123 | // Copy Variable from Normal storage to Auth storage\r | |
4124 | //\r | |
4125 | StartPtr = GetStartPointer (NormalVarStorage);\r | |
4126 | EndPtr = GetEndPointer (NormalVarStorage);\r | |
4127 | AuthStartPtr = (AUTHENTICATED_VARIABLE_HEADER *) GetStartPointer (AuthVarStorage);\r | |
4128 | while (StartPtr < EndPtr) {\r | |
4129 | if (StartPtr->State == VAR_ADDED) {\r | |
4130 | AuthStartPtr = (AUTHENTICATED_VARIABLE_HEADER *) HEADER_ALIGN (AuthStartPtr);\r | |
4131 | //\r | |
4132 | // Copy Variable Header\r | |
4133 | //\r | |
4134 | AuthStartPtr->StartId = StartPtr->StartId;\r | |
4135 | AuthStartPtr->State = StartPtr->State;\r | |
4136 | AuthStartPtr->Attributes = StartPtr->Attributes;\r | |
4137 | AuthStartPtr->NameSize = StartPtr->NameSize;\r | |
4138 | AuthStartPtr->DataSize = StartPtr->DataSize;\r | |
4139 | CopyGuid (&AuthStartPtr->VendorGuid, &StartPtr->VendorGuid);\r | |
4140 | //\r | |
4141 | // Copy Variable Name\r | |
4142 | //\r | |
4143 | NextPtr = (UINT8 *) (AuthStartPtr + 1);\r | |
4144 | CopyMem (NextPtr, GetVariableNamePtr (StartPtr), AuthStartPtr->NameSize);\r | |
4145 | //\r | |
4146 | // Copy Variable Data\r | |
4147 | //\r | |
4148 | NextPtr = NextPtr + AuthStartPtr->NameSize + GET_PAD_SIZE (AuthStartPtr->NameSize);\r | |
4149 | CopyMem (NextPtr, GetVariableDataPtr (StartPtr), AuthStartPtr->DataSize);\r | |
4150 | //\r | |
4151 | // Go to next variable\r | |
4152 | //\r | |
4153 | AuthStartPtr = (AUTHENTICATED_VARIABLE_HEADER *) (NextPtr + AuthStartPtr->DataSize + GET_PAD_SIZE (AuthStartPtr->DataSize));\r | |
4154 | }\r | |
4155 | StartPtr = GetNextVariablePtr (StartPtr);\r | |
4156 | }\r | |
4157 | //\r | |
4158 | // Update Auth Storage Header\r | |
4159 | //\r | |
4160 | AuthVarStorage->Format = NormalVarStorage->Format;\r | |
4161 | AuthVarStorage->State = NormalVarStorage->State;\r | |
1b79547a | 4162 | AuthVarStorage->Size = (UINT32)((UINTN)AuthStartPtr - (UINTN)AuthVarStorage);\r |
a37044ef LG |
4163 | CopyGuid (&AuthVarStorage->Signature, &gEfiAuthenticatedVariableGuid);\r |
4164 | ASSERT (AuthVarStorage->Size <= AuthVarStroageSize);\r | |
8a2d4996 | 4165 | \r |
a37044ef LG |
4166 | //\r |
4167 | // Restore AuthFormat\r | |
4168 | //\r | |
4169 | mVariableModuleGlobal->VariableGlobal.AuthFormat = TRUE;\r | |
4170 | return AuthVarStorage;\r | |
4171 | }\r | |
09808bd3 SZ |
4172 | \r |
4173 | /**\r | |
4174 | Get HOB variable store.\r | |
4175 | \r | |
4176 | @param[out] VariableGuid NV variable store signature.\r | |
4177 | \r | |
4178 | @retval EFI_SUCCESS Function successfully executed.\r | |
4179 | @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r | |
4180 | \r | |
4181 | **/\r | |
4182 | EFI_STATUS\r | |
4183 | GetHobVariableStore (\r | |
4184 | IN EFI_GUID *VariableGuid\r | |
4185 | )\r | |
4186 | {\r | |
4187 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
4188 | UINT64 VariableStoreLength;\r | |
4189 | EFI_HOB_GUID_TYPE *GuidHob;\r | |
4190 | BOOLEAN NeedConvertNormalToAuth;\r | |
4191 | \r | |
4192 | //\r | |
4193 | // Combinations supported:\r | |
4194 | // 1. Normal NV variable store +\r | |
4195 | // Normal HOB variable store\r | |
4196 | // 2. Auth NV variable store +\r | |
4197 | // Auth HOB variable store\r | |
4198 | // 3. Auth NV variable store +\r | |
4199 | // Normal HOB variable store (code will convert it to Auth Format)\r | |
4200 | //\r | |
4201 | NeedConvertNormalToAuth = FALSE;\r | |
4202 | GuidHob = GetFirstGuidHob (VariableGuid);\r | |
4203 | if (GuidHob == NULL && VariableGuid == &gEfiAuthenticatedVariableGuid) {\r | |
4204 | //\r | |
4205 | // Try getting it from normal variable HOB\r | |
4206 | //\r | |
4207 | GuidHob = GetFirstGuidHob (&gEfiVariableGuid);\r | |
4208 | NeedConvertNormalToAuth = TRUE;\r | |
4209 | }\r | |
4210 | if (GuidHob != NULL) {\r | |
4211 | VariableStoreHeader = GET_GUID_HOB_DATA (GuidHob);\r | |
4212 | VariableStoreLength = GuidHob->Header.HobLength - sizeof (EFI_HOB_GUID_TYPE);\r | |
4213 | if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r | |
4214 | if (!NeedConvertNormalToAuth) {\r | |
4215 | mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) AllocateRuntimeCopyPool ((UINTN) VariableStoreLength, (VOID *) VariableStoreHeader);\r | |
4216 | } else {\r | |
4217 | mVariableModuleGlobal->VariableGlobal.HobVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) ConvertNormalVarStorageToAuthVarStorage ((VOID *) VariableStoreHeader);\r | |
4218 | }\r | |
4219 | if (mVariableModuleGlobal->VariableGlobal.HobVariableBase == 0) {\r | |
4220 | return EFI_OUT_OF_RESOURCES;\r | |
4221 | }\r | |
4222 | } else {\r | |
4223 | DEBUG ((EFI_D_ERROR, "HOB Variable Store header is corrupted!\n"));\r | |
4224 | }\r | |
4225 | }\r | |
4226 | \r | |
4227 | return EFI_SUCCESS;\r | |
4228 | }\r | |
4229 | \r | |
8a2d4996 | 4230 | /**\r |
4231 | Initializes variable store area for non-volatile and volatile variable.\r | |
7c80e839 | 4232 | \r |
4233 | @retval EFI_SUCCESS Function successfully executed.\r | |
4234 | @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r | |
4235 | \r | |
4236 | **/\r | |
8d3a5c82 | 4237 | EFI_STATUS\r |
8d3a5c82 | 4238 | VariableCommonInitialize (\r |
8a2d4996 | 4239 | VOID\r |
8d3a5c82 | 4240 | )\r |
8d3a5c82 | 4241 | {\r |
4242 | EFI_STATUS Status;\r | |
8d3a5c82 | 4243 | VARIABLE_STORE_HEADER *VolatileVariableStore;\r |
2fcdca1d | 4244 | UINTN ScratchSize;\r |
fa0737a8 SZ |
4245 | EFI_GUID *VariableGuid;\r |
4246 | EFI_FIRMWARE_VOLUME_HEADER *NvFvHeader;\r | |
8d3a5c82 | 4247 | \r |
7800593d LG |
4248 | //\r |
4249 | // Allocate runtime memory for variable driver global structure.\r | |
4250 | //\r | |
72399dae | 4251 | mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r |
7800593d LG |
4252 | if (mVariableModuleGlobal == NULL) {\r |
4253 | return EFI_OUT_OF_RESOURCES;\r | |
4254 | }\r | |
8d3a5c82 | 4255 | \r |
8a2d4996 | 4256 | InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r |
8d3a5c82 | 4257 | \r |
fa0737a8 SZ |
4258 | //\r |
4259 | // Init non-volatile variable store.\r | |
4260 | //\r | |
26c2edd5 | 4261 | NvFvHeader = NULL;\r |
fa0737a8 SZ |
4262 | Status = InitNonVolatileVariableStore (&NvFvHeader);\r |
4263 | if (EFI_ERROR (Status)) {\r | |
4264 | FreePool (mVariableModuleGlobal);\r | |
4265 | return Status;\r | |
4266 | }\r | |
4267 | \r | |
4268 | //\r | |
4269 | // mVariableModuleGlobal->VariableGlobal.AuthFormat\r | |
4270 | // has been initialized in InitNonVolatileVariableStore().\r | |
4271 | //\r | |
4272 | if (mVariableModuleGlobal->VariableGlobal.AuthFormat) {\r | |
4273 | DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable format!\n"));\r | |
4274 | //\r | |
4275 | // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() will initialize it.\r | |
4276 | //\r | |
4277 | mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;\r | |
4278 | VariableGuid = &gEfiAuthenticatedVariableGuid;\r | |
4279 | } else {\r | |
4280 | DEBUG ((EFI_D_INFO, "Variable driver will work without auth variable support!\n"));\r | |
4281 | mVariableModuleGlobal->VariableGlobal.AuthSupport = FALSE;\r | |
4282 | VariableGuid = &gEfiVariableGuid;\r | |
4283 | }\r | |
4284 | \r | |
0f7aff72 RN |
4285 | //\r |
4286 | // Get HOB variable store.\r | |
4287 | //\r | |
09808bd3 SZ |
4288 | Status = GetHobVariableStore (VariableGuid);\r |
4289 | if (EFI_ERROR (Status)) {\r | |
4290 | FreePool (NvFvHeader);\r | |
4291 | FreePool (mVariableModuleGlobal);\r | |
4292 | return Status;\r | |
0f7aff72 RN |
4293 | }\r |
4294 | \r | |
9b4a2032 LE |
4295 | mVariableModuleGlobal->MaxVolatileVariableSize = ((PcdGet32 (PcdMaxVolatileVariableSize) != 0) ?\r |
4296 | PcdGet32 (PcdMaxVolatileVariableSize) :\r | |
4297 | mVariableModuleGlobal->MaxVariableSize\r | |
4298 | );\r | |
8d3a5c82 | 4299 | //\r |
2fcdca1d | 4300 | // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r |
8d3a5c82 | 4301 | //\r |
9b4a2032 | 4302 | ScratchSize = GetMaxVariableSize ();\r |
fa0737a8 | 4303 | mVariableModuleGlobal->ScratchBufferSize = ScratchSize;\r |
188e4e84 | 4304 | VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r |
8d3a5c82 | 4305 | if (VolatileVariableStore == NULL) {\r |
3e02ebb2 SZ |
4306 | if (mVariableModuleGlobal->VariableGlobal.HobVariableBase != 0) {\r |
4307 | FreePool ((VOID *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase);\r | |
4308 | }\r | |
fa0737a8 | 4309 | FreePool (NvFvHeader);\r |
8d3a5c82 | 4310 | FreePool (mVariableModuleGlobal);\r |
4311 | return EFI_OUT_OF_RESOURCES;\r | |
4312 | }\r | |
4313 | \r | |
188e4e84 | 4314 | SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r |
8d3a5c82 | 4315 | \r |
4316 | //\r | |
8a2d4996 | 4317 | // Initialize Variable Specific Data.\r |
8d3a5c82 | 4318 | //\r |
052ad7e1 | 4319 | mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r |
9cad030b | 4320 | mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r |
8d3a5c82 | 4321 | \r |
fa0737a8 | 4322 | CopyGuid (&VolatileVariableStore->Signature, VariableGuid);\r |
8a2d4996 | 4323 | VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r |
4324 | VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r | |
4325 | VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r | |
4326 | VolatileVariableStore->Reserved = 0;\r | |
4327 | VolatileVariableStore->Reserved1 = 0;\r | |
8d3a5c82 | 4328 | \r |
fa0737a8 | 4329 | return EFI_SUCCESS;\r |
8d3a5c82 | 4330 | }\r |
052ad7e1 | 4331 | \r |
052ad7e1 | 4332 | \r |
aa75dfec | 4333 | /**\r |
8a2d4996 | 4334 | Get the proper fvb handle and/or fvb protocol by the given Flash address.\r |
aa75dfec | 4335 | \r |
fa0737a8 | 4336 | @param[in] Address The Flash address.\r |
8a2d4996 | 4337 | @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r |
4338 | @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r | |
aa75dfec | 4339 | \r |
aa75dfec | 4340 | **/\r |
8a2d4996 | 4341 | EFI_STATUS\r |
4342 | GetFvbInfoByAddress (\r | |
4343 | IN EFI_PHYSICAL_ADDRESS Address,\r | |
4344 | OUT EFI_HANDLE *FvbHandle OPTIONAL,\r | |
4345 | OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r | |
8a9e0b72 | 4346 | )\r |
4347 | {\r | |
8a2d4996 | 4348 | EFI_STATUS Status;\r |
4349 | EFI_HANDLE *HandleBuffer;\r | |
4350 | UINTN HandleCount;\r | |
4351 | UINTN Index;\r | |
4352 | EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r | |
4353 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r | |
8a2d4996 | 4354 | EFI_FVB_ATTRIBUTES_2 Attributes;\r |
1fcbeaea DG |
4355 | UINTN BlockSize;\r |
4356 | UINTN NumberOfBlocks;\r | |
4357 | \r | |
4e1005ec | 4358 | HandleBuffer = NULL;\r |
8a9e0b72 | 4359 | //\r |
8a2d4996 | 4360 | // Get all FVB handles.\r |
8a9e0b72 | 4361 | //\r |
8a2d4996 | 4362 | Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r |
8a9e0b72 | 4363 | if (EFI_ERROR (Status)) {\r |
8a2d4996 | 4364 | return EFI_NOT_FOUND;\r |
8a9e0b72 | 4365 | }\r |
8a2d4996 | 4366 | \r |
8a9e0b72 | 4367 | //\r |
8a2d4996 | 4368 | // Get the FVB to access variable store.\r |
8a9e0b72 | 4369 | //\r |
8a2d4996 | 4370 | Fvb = NULL;\r |
f0480ecf | 4371 | for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r |
8a2d4996 | 4372 | Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r |
8a9e0b72 | 4373 | if (EFI_ERROR (Status)) {\r |
4374 | Status = EFI_NOT_FOUND;\r | |
4375 | break;\r | |
4376 | }\r | |
4377 | \r | |
4378 | //\r | |
4379 | // Ensure this FVB protocol supported Write operation.\r | |
4380 | //\r | |
4381 | Status = Fvb->GetAttributes (Fvb, &Attributes);\r | |
4382 | if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r | |
1fcbeaea | 4383 | continue;\r |
8a9e0b72 | 4384 | }\r |
1fcbeaea | 4385 | \r |
8a9e0b72 | 4386 | //\r |
8a2d4996 | 4387 | // Compare the address and select the right one.\r |
8a9e0b72 | 4388 | //\r |
4389 | Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r | |
4390 | if (EFI_ERROR (Status)) {\r | |
4391 | continue;\r | |
4392 | }\r | |
4393 | \r | |
1fcbeaea DG |
4394 | //\r |
4395 | // Assume one FVB has one type of BlockSize.\r | |
4396 | //\r | |
4397 | Status = Fvb->GetBlockSize (Fvb, 0, &BlockSize, &NumberOfBlocks);\r | |
4398 | if (EFI_ERROR (Status)) {\r | |
4399 | continue;\r | |
4400 | }\r | |
4401 | \r | |
4402 | if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + BlockSize * NumberOfBlocks))) {\r | |
8a2d4996 | 4403 | if (FvbHandle != NULL) {\r |
4404 | *FvbHandle = HandleBuffer[Index];\r | |
4405 | }\r | |
4406 | if (FvbProtocol != NULL) {\r | |
4407 | *FvbProtocol = Fvb;\r | |
4408 | }\r | |
4409 | Status = EFI_SUCCESS;\r | |
8a9e0b72 | 4410 | break;\r |
4411 | }\r | |
4412 | }\r | |
8a9e0b72 | 4413 | FreePool (HandleBuffer);\r |
533020ef | 4414 | \r |
8a2d4996 | 4415 | if (Fvb == NULL) {\r |
4416 | Status = EFI_NOT_FOUND;\r | |
8a9e0b72 | 4417 | }\r |
fa0737a8 SZ |
4418 | \r |
4419 | return Status;\r | |
052ad7e1 A |
4420 | }\r |
4421 | \r |