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