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