]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/EmuRuntimeDxe/EmuVariable.c
add error handling for input parameter.
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / EmuRuntimeDxe / EmuVariable.c
CommitLineData
504214c4 1/** @file\r
8d3a5c82 2\r
504214c4
LG
3 Emulation Variable services operate on the runtime volatile memory.\r
4 The nonvolatile variable space doesn't exist.\r
5\r
6Copyright (c) 2006 - 2008, Intel Corporation\r
8d3a5c82 7All rights reserved. This program and the accompanying materials\r
8are licensed and made available under the terms and conditions of the BSD License\r
9which accompanies this distribution. The full text of the license may be found at\r
10http://opensource.org/licenses/bsd-license.php\r
11\r
12THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
504214c4 15**/\r
8d3a5c82 16\r
17#include "Variable.h"\r
18\r
1794e506 19///\r
20/// Don't use module globals after the SetVirtualAddress map is signaled\r
21///\r
8d3a5c82 22ESAL_VARIABLE_GLOBAL *mVariableModuleGlobal;\r
23\r
e4ddc008 24VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r
1794e506 25\r
72399dae 26///\r
27/// The size of a 3 character ISO639 language code.\r
28///\r
29#define ISO_639_2_ENTRY_SIZE 3\r
30\r
31/**\r
32 Update the variable region with Variable information. These are the same \r
33 arguments as the EFI Variable services.\r
34\r
35 @param[in] VariableName Name of variable\r
36\r
37 @param[in] VendorGuid Guid of variable\r
38\r
39 @param[in] Data Variable data\r
40\r
41 @param[in] DataSize Size of data. 0 means delete\r
42\r
43 @param[in] Attributes Attribues of the variable\r
44\r
45 @param[in] Variable The variable information which is used to keep track of variable usage.\r
46\r
47 @retval EFI_SUCCESS The update operation is success.\r
48\r
49 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
50\r
51**/\r
52EFI_STATUS\r
53EFIAPI\r
54UpdateVariable (\r
55 IN CHAR16 *VariableName,\r
56 IN EFI_GUID *VendorGuid,\r
57 IN VOID *Data,\r
58 IN UINTN DataSize,\r
59 IN UINT32 Attributes OPTIONAL,\r
60 IN VARIABLE_POINTER_TRACK *Variable\r
61 );\r
62\r
63/**\r
64 Finds variable in storage blocks of volatile and non-volatile storage areas.\r
65\r
66 This code finds variable in storage blocks of volatile and non-volatile storage areas.\r
67 If VariableName is an empty string, then we just return the first\r
68 qualified variable without comparing VariableName and VendorGuid.\r
69 Otherwise, VariableName and VendorGuid are compared.\r
70\r
71 @param VariableName Name of the variable to be found.\r
72 @param VendorGuid Vendor GUID to be found.\r
73 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,\r
74 including the range searched and the target position.\r
75 @param Global Pointer to VARIABLE_GLOBAL structure, including\r
76 base of volatile variable storage area, base of\r
77 NV variable storage area, and a lock.\r
78\r
79 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while\r
80 VendorGuid is NULL.\r
81 @retval EFI_SUCCESS Variable successfully found.\r
82 @retval EFI_NOT_FOUND Variable not found.\r
83\r
84**/\r
85EFI_STATUS\r
86FindVariable (\r
87 IN CHAR16 *VariableName,\r
88 IN EFI_GUID *VendorGuid,\r
89 OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
90 IN VARIABLE_GLOBAL *Global\r
91 );\r
92\r
1794e506 93/**\r
94 Acquires lock only at boot time. Simply returns at runtime.\r
95\r
96 This is a temperary function which will be removed when\r
97 EfiAcquireLock() in UefiLib can handle the call in UEFI\r
98 Runtimer driver in RT phase.\r
99 It calls EfiAcquireLock() at boot time, and simply returns\r
100 at runtime\r
101\r
102 @param Lock A pointer to the lock to acquire\r
103\r
104**/\r
8d3a5c82 105VOID\r
106AcquireLockOnlyAtBootTime (\r
107 IN EFI_LOCK *Lock\r
108 )\r
109{\r
110 if (!EfiAtRuntime ()) {\r
111 EfiAcquireLock (Lock);\r
112 }\r
113}\r
114\r
1794e506 115/**\r
116 Releases lock only at boot time. Simply returns at runtime.\r
117\r
118 This is a temperary function which will be removed when\r
119 EfiReleaseLock() in UefiLib can handle the call in UEFI\r
120 Runtimer driver in RT phase.\r
121 It calls EfiReleaseLock() at boot time, and simply returns\r
122 at runtime\r
123\r
124 @param Lock A pointer to the lock to release\r
125\r
126**/\r
8d3a5c82 127VOID\r
128ReleaseLockOnlyAtBootTime (\r
129 IN EFI_LOCK *Lock\r
130 )\r
131{\r
132 if (!EfiAtRuntime ()) {\r
133 EfiReleaseLock (Lock);\r
134 }\r
135}\r
136\r
1794e506 137/**\r
138 Gets pointer to the variable data.\r
8d3a5c82 139\r
1794e506 140 This function gets the pointer to the variable data according\r
141 to the input pointer to the variable header.\r
8d3a5c82 142\r
1794e506 143 @param Variable Pointer to the variable header.\r
8d3a5c82 144\r
1794e506 145 @return Pointer to variable data\r
8d3a5c82 146\r
1794e506 147**/\r
148UINT8 *\r
149GetVariableDataPtr (\r
150 IN VARIABLE_HEADER *Variable\r
151 )\r
8d3a5c82 152{\r
153 if (Variable->StartId != VARIABLE_DATA) {\r
154 return NULL;\r
155 }\r
156 //\r
157 // Be careful about pad size for alignment\r
158 //\r
159 return (UINT8 *) ((UINTN) GET_VARIABLE_NAME_PTR (Variable) + Variable->NameSize + GET_PAD_SIZE (Variable->NameSize));\r
160}\r
161\r
1794e506 162/**\r
02328a57 163 Gets pointer to header of the next potential variable.\r
8d3a5c82 164\r
02328a57 165 This function gets the pointer to the next potential variable header\r
166 according to the input point to the variable header. The return value\r
167 is not a valid variable if the input variable was the last variable\r
168 in the variabl store.\r
8d3a5c82 169\r
1794e506 170 @param Variable Pointer to header of the next variable\r
8d3a5c82 171\r
1794e506 172 @return Pointer to next variable header.\r
02328a57 173 @retval NULL Input was not a valid variable header.\r
8d3a5c82 174\r
1794e506 175**/\r
176VARIABLE_HEADER *\r
02328a57 177GetNextPotentialVariablePtr (\r
1794e506 178 IN VARIABLE_HEADER *Variable\r
179 )\r
8d3a5c82 180{\r
181 VARIABLE_HEADER *VarHeader;\r
182\r
183 if (Variable->StartId != VARIABLE_DATA) {\r
184 return NULL;\r
185 }\r
186 //\r
187 // Be careful about pad size for alignment\r
188 //\r
189 VarHeader = (VARIABLE_HEADER *) (GetVariableDataPtr (Variable) + Variable->DataSize + GET_PAD_SIZE (Variable->DataSize));\r
190\r
02328a57 191 return VarHeader;\r
192}\r
193\r
194/**\r
195 Gets pointer to header of the next variable.\r
196\r
197 This function gets the pointer to the next variable header according\r
198 to the input point to the variable header.\r
199\r
200 @param Variable Pointer to header of the next variable\r
201\r
202 @return Pointer to next variable header.\r
203\r
204**/\r
205VARIABLE_HEADER *\r
206GetNextVariablePtr (\r
207 IN VARIABLE_HEADER *Variable\r
208 )\r
209{\r
210 VARIABLE_HEADER *VarHeader;\r
211\r
212 VarHeader = GetNextPotentialVariablePtr (Variable);\r
213\r
214 if ((VarHeader == NULL) || (VarHeader->StartId != VARIABLE_DATA)) {\r
8d3a5c82 215 return NULL;\r
216 }\r
217\r
218 return VarHeader;\r
219}\r
220\r
02328a57 221/**\r
222 Updates LastVariableOffset variable for the given variable store.\r
223\r
224 LastVariableOffset points to the offset to use for the next variable\r
225 when updating the variable store.\r
226\r
227 @param[in] VariableStore Pointer to the start of the variable store\r
228 @param[out] LastVariableOffset Offset to put the next new variable in\r
229\r
230**/\r
231VOID\r
232InitializeLocationForLastVariableOffset (\r
233 IN VARIABLE_STORE_HEADER *VariableStore,\r
234 OUT UINTN *LastVariableOffset\r
235 )\r
236{\r
237 VARIABLE_HEADER *VarHeader;\r
238\r
239 *LastVariableOffset = sizeof (VARIABLE_STORE_HEADER);\r
240 VarHeader = (VARIABLE_HEADER*) ((UINT8*)VariableStore + *LastVariableOffset);\r
241 while (VarHeader->StartId == VARIABLE_DATA) {\r
242 VarHeader = GetNextPotentialVariablePtr (VarHeader);\r
243\r
244 if (VarHeader != NULL) {\r
245 *LastVariableOffset = (UINTN) VarHeader - (UINTN) VariableStore;\r
246 } else {\r
247 return;\r
248 }\r
249 }\r
250}\r
251\r
1794e506 252/**\r
253 Gets pointer to the end of the variable storage area.\r
8d3a5c82 254\r
1794e506 255 This function gets pointer to the end of the variable storage\r
256 area, according to the input variable store header.\r
8d3a5c82 257\r
1794e506 258 @param VolHeader Pointer to the variale store header\r
8d3a5c82 259\r
1794e506 260 @return Pointer to the end of the variable storage area.\r
8d3a5c82 261\r
1794e506 262**/\r
263VARIABLE_HEADER *\r
264GetEndPointer (\r
265 IN VARIABLE_STORE_HEADER *VolHeader\r
266 )\r
8d3a5c82 267{\r
268 //\r
269 // The end of variable store\r
270 //\r
271 return (VARIABLE_HEADER *) ((UINTN) VolHeader + VolHeader->Size);\r
272}\r
273\r
e4ddc008 274/**\r
275 Routine used to track statistical information about variable usage. \r
276 The data is stored in the EFI system table so it can be accessed later.\r
277 VariableInfo.efi can dump out the table. Only Boot Services variable \r
278 accesses are tracked by this code. The PcdVariableCollectStatistics\r
279 build flag controls if this feature is enabled. \r
280\r
281 A read that hits in the cache will have Read and Cache true for \r
282 the transaction. Data is allocated by this routine, but never\r
283 freed.\r
284\r
285 @param[in] VariableName Name of the Variable to track\r
286 @param[in] VendorGuid Guid of the Variable to track\r
287 @param[in] Volatile TRUE if volatile FALSE if non-volatile\r
288 @param[in] Read TRUE if GetVariable() was called\r
289 @param[in] Write TRUE if SetVariable() was called\r
290 @param[in] Delete TRUE if deleted via SetVariable()\r
291 @param[in] Cache TRUE for a cache hit.\r
292\r
293**/\r
294VOID\r
295UpdateVariableInfo (\r
296 IN CHAR16 *VariableName,\r
297 IN EFI_GUID *VendorGuid,\r
298 IN BOOLEAN Volatile,\r
299 IN BOOLEAN Read,\r
300 IN BOOLEAN Write,\r
301 IN BOOLEAN Delete,\r
302 IN BOOLEAN Cache\r
303 )\r
304{\r
305 VARIABLE_INFO_ENTRY *Entry;\r
306\r
307 if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r
308\r
309 if (EfiAtRuntime ()) {\r
310 // Don't collect statistics at runtime\r
311 return;\r
312 }\r
313\r
314 if (gVariableInfo == NULL) {\r
315 //\r
316 // on the first call allocate a entry and place a pointer to it in\r
317 // the EFI System Table\r
318 //\r
319 gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
320 ASSERT (gVariableInfo != NULL);\r
321\r
322 CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r
bcd7070d 323 gVariableInfo->Name = AllocatePool (StrSize (VariableName));\r
e4ddc008 324 ASSERT (gVariableInfo->Name != NULL);\r
325 StrCpy (gVariableInfo->Name, VariableName);\r
326 gVariableInfo->Volatile = Volatile;\r
327\r
328 gBS->InstallConfigurationTable (&gEfiVariableGuid, gVariableInfo);\r
329 }\r
330\r
331 \r
332 for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r
333 if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r
334 if (StrCmp (VariableName, Entry->Name) == 0) {\r
335 if (Read) {\r
336 Entry->ReadCount++;\r
337 }\r
338 if (Write) {\r
339 Entry->WriteCount++;\r
340 }\r
341 if (Delete) {\r
342 Entry->DeleteCount++;\r
343 }\r
344 if (Cache) {\r
345 Entry->CacheCount++;\r
346 }\r
347\r
348 return;\r
349 }\r
350 }\r
351\r
352 if (Entry->Next == NULL) {\r
353 //\r
354 // If the entry is not in the table add it.\r
355 // Next iteration of the loop will fill in the data\r
356 //\r
357 Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r
358 ASSERT (Entry->Next != NULL);\r
359\r
360 CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r
bcd7070d 361 Entry->Next->Name = AllocatePool (StrSize (VariableName));\r
e4ddc008 362 ASSERT (Entry->Next->Name != NULL);\r
363 StrCpy (Entry->Next->Name, VariableName);\r
364 Entry->Next->Volatile = Volatile;\r
365 }\r
366\r
367 }\r
368 }\r
369}\r
370\r
72399dae 371/**\r
372 Get index from supported language codes according to language string.\r
373\r
374 This code is used to get corresponding index in supported language codes. It can handle\r
0254efc0 375 RFC4646 and ISO639 language tags.\r
72399dae 376 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.\r
0254efc0 377 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.\r
72399dae 378\r
379 For example:\r
380 SupportedLang = "engfraengfra"\r
381 Lang = "eng"\r
382 Iso639Language = TRUE\r
383 The return value is "0".\r
384 Another example:\r
385 SupportedLang = "en;fr;en-US;fr-FR"\r
386 Lang = "fr-FR"\r
387 Iso639Language = FALSE\r
388 The return value is "3".\r
389\r
390 @param SupportedLang Platform supported language codes.\r
391 @param Lang Configured language.\r
0254efc0 392 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
72399dae 393\r
394 @retval the index of language in the language codes.\r
395\r
396**/\r
397UINTN\r
398EFIAPI\r
399GetIndexFromSupportedLangCodes(\r
400 IN CHAR8 *SupportedLang,\r
401 IN CHAR8 *Lang,\r
402 IN BOOLEAN Iso639Language\r
403 ) \r
404{\r
405 UINTN Index;\r
406 UINT32 CompareLength;\r
407 CHAR8 *Supported;\r
408\r
409 Index = 0;\r
410 Supported = SupportedLang;\r
411 if (Iso639Language) {\r
412 CompareLength = 3;\r
413 for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r
414 if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r
415 //\r
416 // Successfully find the index of Lang string in SupportedLang string.\r
417 //\r
418 Index = Index / CompareLength;\r
419 return Index;\r
420 }\r
421 }\r
422 ASSERT (FALSE);\r
423 return 0;\r
424 } else {\r
425 //\r
0254efc0 426 // Compare RFC4646 language code\r
72399dae 427 //\r
428 while (*Supported != '\0') {\r
429 //\r
430 // take semicolon as delimitation, sequentially traverse supported language codes.\r
431 //\r
432 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r
433 Supported++;\r
434 }\r
435 if (AsciiStrnCmp (Lang, Supported - CompareLength, CompareLength) == 0) {\r
436 //\r
437 // Successfully find the index of Lang string in SupportedLang string.\r
438 //\r
439 return Index;\r
440 }\r
441 Index++;\r
442 }\r
443 ASSERT (FALSE);\r
444 return 0;\r
445 }\r
446}\r
447\r
448/**\r
449 Get language string from supported language codes according to index.\r
450\r
451 This code is used to get corresponding language string in supported language codes. It can handle\r
0254efc0 452 RFC4646 and ISO639 language tags.\r
72399dae 453 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.\r
0254efc0 454 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.\r
72399dae 455\r
456 For example:\r
457 SupportedLang = "engfraengfra"\r
458 Index = "1"\r
459 Iso639Language = TRUE\r
460 The return value is "fra".\r
461 Another example:\r
462 SupportedLang = "en;fr;en-US;fr-FR"\r
463 Index = "1"\r
464 Iso639Language = FALSE\r
465 The return value is "fr".\r
466\r
467 @param SupportedLang Platform supported language codes.\r
468 @param Index the index in supported language codes.\r
0254efc0 469 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r
72399dae 470\r
471 @retval the language string in the language codes.\r
472\r
473**/\r
474CHAR8 *\r
475EFIAPI\r
476GetLangFromSupportedLangCodes (\r
477 IN CHAR8 *SupportedLang,\r
478 IN UINTN Index,\r
479 IN BOOLEAN Iso639Language\r
480)\r
481{\r
482 UINTN SubIndex;\r
483 UINT32 CompareLength;\r
484 CHAR8 *Supported;\r
485\r
486 SubIndex = 0;\r
487 Supported = SupportedLang;\r
488 if (Iso639Language) {\r
489 //\r
490 // according to the index of Lang string in SupportedLang string to get the language.\r
491 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
492 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
493 //\r
494 CompareLength = 3;\r
495 SetMem (mVariableModuleGlobal->Lang, sizeof(mVariableModuleGlobal->Lang), 0);\r
496 return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);\r
497 \r
498 } else {\r
499 while (TRUE) {\r
500 //\r
501 // take semicolon as delimitation, sequentially traverse supported language codes.\r
502 //\r
503 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r
504 Supported++;\r
505 }\r
506 if ((*Supported == '\0') && (SubIndex != Index)) {\r
507 //\r
508 // Have completed the traverse, but not find corrsponding string.\r
509 // This case is not allowed to happen.\r
510 //\r
511 ASSERT(FALSE);\r
512 return NULL;\r
513 }\r
514 if (SubIndex == Index) {\r
515 //\r
516 // according to the index of Lang string in SupportedLang string to get the language.\r
517 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r
518 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r
519 //\r
520 SetMem (mVariableModuleGlobal->PlatformLang, sizeof (mVariableModuleGlobal->PlatformLang), 0);\r
521 return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);\r
522 }\r
523 SubIndex++; \r
524 }\r
525 }\r
526}\r
527\r
528/**\r
529 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r
530\r
531 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r
532\r
533 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r
534 and are read-only. Therefore, in variable driver, only store the original value for other use.\r
535\r
536 @param[in] VariableName Name of variable\r
537\r
538 @param[in] Data Variable data\r
539\r
540 @param[in] DataSize Size of data. 0 means delete\r
541\r
542 @retval EFI_SUCCESS auto update operation is successful.\r
543\r
544**/\r
545EFI_STATUS\r
546EFIAPI\r
547AutoUpdateLangVariable(\r
548 IN CHAR16 *VariableName,\r
549 IN VOID *Data,\r
550 IN UINTN DataSize\r
551 )\r
552{\r
553 EFI_STATUS Status;\r
554 CHAR8 *BestPlatformLang;\r
555 CHAR8 *BestLang;\r
556 UINTN Index;\r
557 UINT32 Attributes;\r
558 VARIABLE_POINTER_TRACK Variable;\r
559\r
560 //\r
561 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r
562 //\r
563 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r
564\r
565 if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {\r
566 //\r
567 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r
568 // Therefore, in variable driver, only store the original value for other use.\r
569 //\r
570 AsciiStrnCpy (mVariableModuleGlobal->PlatformLangCodes, Data, DataSize);\r
571 } else if (StrCmp (VariableName, L"LangCodes") == 0) {\r
572 //\r
573 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r
574 // Therefore, in variable driver, only store the original value for other use.\r
575 //\r
576 AsciiStrnCpy (mVariableModuleGlobal->LangCodes, Data, DataSize);\r
577 } else if (StrCmp (VariableName, L"PlatformLang") == 0) {\r
578 ASSERT (AsciiStrLen (mVariableModuleGlobal->PlatformLangCodes) != 0);\r
579\r
580 //\r
581 // When setting PlatformLang, firstly get most matched language string from supported language codes.\r
582 //\r
bd397350 583 BestPlatformLang = GetBestLanguage(mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r
72399dae 584\r
585 //\r
586 // Get the corresponding index in language codes.\r
587 //\r
588 Index = GetIndexFromSupportedLangCodes(mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r
589\r
590 //\r
0254efc0 591 // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r
72399dae 592 //\r
593 BestLang = GetLangFromSupportedLangCodes(mVariableModuleGlobal->LangCodes, Index, TRUE);\r
594\r
595 //\r
596 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r
597 //\r
598 FindVariable(L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);\r
599\r
600 Status = UpdateVariable(L"Lang", &gEfiGlobalVariableGuid, \r
601 BestLang, ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);\r
602\r
603 DEBUG((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));\r
604\r
605 ASSERT_EFI_ERROR(Status);\r
606\r
607 } else if (StrCmp (VariableName, L"Lang") == 0) {\r
608 ASSERT (AsciiStrLen (mVariableModuleGlobal->LangCodes) != 0);\r
609\r
610 //\r
611 // When setting Lang, firstly get most matched language string from supported language codes.\r
612 //\r
bd397350 613 BestLang = GetBestLanguage(mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r
72399dae 614\r
615 //\r
616 // Get the corresponding index in language codes.\r
617 //\r
618 Index = GetIndexFromSupportedLangCodes(mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r
619\r
620 //\r
0254efc0 621 // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r
72399dae 622 //\r
623 BestPlatformLang = GetLangFromSupportedLangCodes(mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r
624\r
625 //\r
626 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r
627 //\r
628 FindVariable(L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);\r
629\r
630 Status = UpdateVariable(L"PlatformLang", &gEfiGlobalVariableGuid, \r
a1373617 631 BestPlatformLang, AsciiStrSize (BestPlatformLang), Attributes, &Variable);\r
72399dae 632\r
633 DEBUG((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));\r
634 ASSERT_EFI_ERROR(Status);\r
635 }\r
636 return EFI_SUCCESS;\r
637}\r
638\r
639/**\r
640 Update the variable region with Variable information. These are the same \r
641 arguments as the EFI Variable services.\r
642\r
643 @param[in] VariableName Name of variable\r
644\r
645 @param[in] VendorGuid Guid of variable\r
646\r
647 @param[in] Data Variable data\r
648\r
649 @param[in] DataSize Size of data. 0 means delete\r
650\r
651 @param[in] Attributes Attribues of the variable\r
652\r
653 @param[in] Variable The variable information which is used to keep track of variable usage.\r
654\r
655 @retval EFI_SUCCESS The update operation is success.\r
656\r
657 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r
658\r
659**/\r
660EFI_STATUS\r
661EFIAPI\r
662UpdateVariable (\r
663 IN CHAR16 *VariableName,\r
664 IN EFI_GUID *VendorGuid,\r
665 IN VOID *Data,\r
666 IN UINTN DataSize,\r
667 IN UINT32 Attributes OPTIONAL,\r
668 IN VARIABLE_POINTER_TRACK *Variable\r
669 )\r
670{\r
671 EFI_STATUS Status;\r
672 VARIABLE_HEADER *NextVariable;\r
673 UINTN VarNameSize;\r
674 UINTN VarNameOffset;\r
675 UINTN VarDataOffset;\r
676 UINTN VarSize;\r
677 VARIABLE_GLOBAL *Global;\r
1d5b25b7 678 UINTN NonVolatileVarableStoreSize;\r
72399dae 679\r
680 Global = &mVariableModuleGlobal->VariableGlobal[Physical];\r
681\r
682 if (Variable->CurrPtr != NULL) {\r
683 //\r
684 // Update/Delete existing variable\r
685 //\r
686\r
687 if (EfiAtRuntime ()) { \r
688 //\r
689 // If EfiAtRuntime and the variable is Volatile and Runtime Access, \r
690 // the volatile is ReadOnly, and SetVariable should be aborted and \r
691 // return EFI_WRITE_PROTECTED.\r
692 //\r
693 if (Variable->Volatile) {\r
694 Status = EFI_WRITE_PROTECTED;\r
695 goto Done;\r
696 }\r
697 //\r
698 // Only variable have NV attribute can be updated/deleted in Runtime\r
699 //\r
700 if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
701 Status = EFI_INVALID_PARAMETER;\r
702 goto Done;\r
703 }\r
704 }\r
705\r
706 //\r
707 // Setting a data variable with no access, or zero DataSize attributes\r
708 // specified causes it to be deleted.\r
709 //\r
710 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
711 Variable->CurrPtr->State &= VAR_DELETED;\r
712 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r
713 Status = EFI_SUCCESS;\r
714 goto Done;\r
715 }\r
716\r
717 //\r
718 // If the variable is marked valid and the same data has been passed in\r
719 // then return to the caller immediately.\r
720 //\r
721 if (Variable->CurrPtr->DataSize == DataSize &&\r
722 CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0\r
723 ) {\r
724 Status = EFI_SUCCESS;\r
725 goto Done;\r
726 } else if (Variable->CurrPtr->State == VAR_ADDED) {\r
727 //\r
728 // Mark the old variable as in delete transition\r
729 //\r
730 Variable->CurrPtr->State &= VAR_IN_DELETED_TRANSITION;\r
731 }\r
732 \r
733 } else {\r
734 //\r
735 // No found existing variable, Create a new variable\r
736 // \r
737 \r
738 //\r
739 // Make sure we are trying to create a new variable.\r
740 // Setting a data variable with no access, or zero DataSize attributes means to delete it. \r
741 //\r
742 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r
743 Status = EFI_NOT_FOUND;\r
744 goto Done;\r
745 }\r
746 \r
747 //\r
748 // Only variable have NV|RT attribute can be created in Runtime\r
749 //\r
750 if (EfiAtRuntime () &&\r
751 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r
752 Status = EFI_INVALID_PARAMETER;\r
753 goto Done;\r
754 } \r
755 }\r
756 \r
757 //\r
758 // Function part - create a new variable and copy the data.\r
759 // Both update a variable and create a variable will come here.\r
760 //\r
761 \r
762 VarNameOffset = sizeof (VARIABLE_HEADER);\r
763 VarNameSize = StrSize (VariableName);\r
764 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r
765 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r
766\r
767 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
768 NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(Global->NonVolatileVariableBase))->Size;\r
769 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r
770 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > FixedPcdGet32(PcdHwErrStorageSize)))\r
771 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r
772 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - FixedPcdGet32(PcdHwErrStorageSize)))) {\r
773 Status = EFI_OUT_OF_RESOURCES;\r
774 goto Done;\r
775 }\r
776\r
777 NextVariable = (VARIABLE_HEADER *) (UINT8 *) (mVariableModuleGlobal->NonVolatileLastVariableOffset\r
778 + (UINTN) Global->NonVolatileVariableBase);\r
779 mVariableModuleGlobal->NonVolatileLastVariableOffset += VarSize;\r
780\r
781 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r
782 mVariableModuleGlobal->HwErrVariableTotalSize += VarSize;\r
783 } else {\r
784 mVariableModuleGlobal->CommonVariableTotalSize += VarSize;\r
785 }\r
786 } else {\r
787 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r
788 ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->VolatileVariableBase)))->Size\r
789 ) {\r
790 Status = EFI_OUT_OF_RESOURCES;\r
791 goto Done;\r
792 }\r
793\r
794 NextVariable = (VARIABLE_HEADER *) (UINT8 *) (mVariableModuleGlobal->VolatileLastVariableOffset\r
795 + (UINTN) Global->VolatileVariableBase);\r
796 mVariableModuleGlobal->VolatileLastVariableOffset += VarSize;\r
797 }\r
798\r
799 NextVariable->StartId = VARIABLE_DATA;\r
800 NextVariable->Attributes = Attributes;\r
801 NextVariable->State = VAR_ADDED;\r
802 NextVariable->Reserved = 0;\r
803\r
804 //\r
805 // There will be pad bytes after Data, the NextVariable->NameSize and\r
806 // NextVariable->NameSize should not include pad size so that variable\r
807 // service can get actual size in GetVariable\r
808 //\r
809 NextVariable->NameSize = (UINT32)VarNameSize;\r
810 NextVariable->DataSize = (UINT32)DataSize;\r
811\r
812 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r
813 CopyMem (\r
814 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r
815 VariableName,\r
816 VarNameSize\r
817 );\r
818 CopyMem (\r
819 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r
820 Data,\r
821 DataSize\r
822 );\r
823\r
824 //\r
825 // Mark the old variable as deleted\r
826 //\r
1d5b25b7 827 if (Variable->CurrPtr != NULL) {\r
828 Variable->CurrPtr->State &= VAR_DELETED;\r
829 }\r
72399dae 830\r
831 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r
832\r
833 Status = EFI_SUCCESS;\r
834\r
835Done:\r
836 return Status;\r
837}\r
838\r
1794e506 839/**\r
840 Finds variable in storage blocks of volatile and non-volatile storage areas.\r
841\r
842 This code finds variable in storage blocks of volatile and non-volatile storage areas.\r
843 If VariableName is an empty string, then we just return the first\r
844 qualified variable without comparing VariableName and VendorGuid.\r
845 Otherwise, VariableName and VendorGuid are compared.\r
846\r
847 @param VariableName Name of the variable to be found.\r
848 @param VendorGuid Vendor GUID to be found.\r
849 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,\r
850 including the range searched and the target position.\r
851 @param Global Pointer to VARIABLE_GLOBAL structure, including\r
852 base of volatile variable storage area, base of\r
853 NV variable storage area, and a lock.\r
854\r
855 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while\r
856 VendorGuid is NULL.\r
857 @retval EFI_SUCCESS Variable successfully found.\r
858 @retval EFI_NOT_FOUND Variable not found.\r
859\r
860**/\r
8d3a5c82 861EFI_STATUS\r
8d3a5c82 862FindVariable (\r
863 IN CHAR16 *VariableName,\r
864 IN EFI_GUID *VendorGuid,\r
865 OUT VARIABLE_POINTER_TRACK *PtrTrack,\r
866 IN VARIABLE_GLOBAL *Global\r
867 )\r
8d3a5c82 868{\r
869 VARIABLE_HEADER *Variable[2];\r
870 VARIABLE_STORE_HEADER *VariableStoreHeader[2];\r
871 UINTN Index;\r
872\r
8d3a5c82 873 //\r
874 // 0: Non-Volatile, 1: Volatile\r
875 //\r
876 VariableStoreHeader[0] = (VARIABLE_STORE_HEADER *) ((UINTN) Global->NonVolatileVariableBase);\r
877 VariableStoreHeader[1] = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);\r
878\r
879 //\r
880 // Start Pointers for the variable.\r
881 // Actual Data Pointer where data can be written.\r
882 //\r
883 Variable[0] = (VARIABLE_HEADER *) (VariableStoreHeader[0] + 1);\r
884 Variable[1] = (VARIABLE_HEADER *) (VariableStoreHeader[1] + 1);\r
885\r
886 if (VariableName[0] != 0 && VendorGuid == NULL) {\r
887 return EFI_INVALID_PARAMETER;\r
888 }\r
889 //\r
890 // Find the variable by walk through non-volatile and volatile variable store\r
891 //\r
892 for (Index = 0; Index < 2; Index++) {\r
893 PtrTrack->StartPtr = (VARIABLE_HEADER *) (VariableStoreHeader[Index] + 1);\r
894 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);\r
895\r
fba0ee1f 896 while ((Variable[Index] < GetEndPointer (VariableStoreHeader[Index])) && (Variable[Index] != NULL)) {\r
8d3a5c82 897 if (Variable[Index]->StartId == VARIABLE_DATA && Variable[Index]->State == VAR_ADDED) {\r
2374b973 898 if (!(EfiAtRuntime () && ((Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0))) {\r
8d3a5c82 899 if (VariableName[0] == 0) {\r
900 PtrTrack->CurrPtr = Variable[Index];\r
901 PtrTrack->Volatile = (BOOLEAN) Index;\r
902 return EFI_SUCCESS;\r
903 } else {\r
904 if (CompareGuid (VendorGuid, &Variable[Index]->VendorGuid)) {\r
c24b392c 905 if (CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable[Index]), Variable[Index]->NameSize) == 0) {\r
8d3a5c82 906 PtrTrack->CurrPtr = Variable[Index];\r
907 PtrTrack->Volatile = (BOOLEAN) Index;\r
908 return EFI_SUCCESS;\r
909 }\r
910 }\r
911 }\r
912 }\r
913 }\r
914\r
915 Variable[Index] = GetNextVariablePtr (Variable[Index]);\r
916 }\r
917 }\r
918 PtrTrack->CurrPtr = NULL;\r
919 return EFI_NOT_FOUND;\r
920}\r
921\r
1794e506 922/**\r
923 This code finds variable in storage blocks (Volatile or Non-Volatile).\r
924 \r
925 @param VariableName A Null-terminated Unicode string that is the name of\r
926 the vendor's variable.\r
927 @param VendorGuid A unique identifier for the vendor.\r
928 @param Attributes If not NULL, a pointer to the memory location to return the \r
929 attributes bitmask for the variable.\r
930 @param DataSize Size of Data found. If size is less than the\r
931 data, this value contains the required size.\r
932 @param Data On input, the size in bytes of the return Data buffer. \r
933 On output, the size of data returned in Data.\r
934 @param Global Pointer to VARIABLE_GLOBAL structure\r
1794e506 935\r
936 @retval EFI_SUCCESS The function completed successfully. \r
937 @retval EFI_NOT_FOUND The variable was not found.\r
938 @retval EFI_BUFFER_TOO_SMALL DataSize is too small for the result. DataSize has \r
939 been updated with the size needed to complete the request.\r
940 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid or DataSize is NULL.\r
941\r
942**/\r
8d3a5c82 943EFI_STATUS\r
944EFIAPI\r
bd682470 945EmuGetVariable (\r
8d3a5c82 946 IN CHAR16 *VariableName,\r
72399dae 947 IN EFI_GUID *VendorGuid,\r
8d3a5c82 948 OUT UINT32 *Attributes OPTIONAL,\r
949 IN OUT UINTN *DataSize,\r
950 OUT VOID *Data,\r
72399dae 951 IN VARIABLE_GLOBAL *Global\r
8d3a5c82 952 )\r
8d3a5c82 953{\r
954 VARIABLE_POINTER_TRACK Variable;\r
955 UINTN VarDataSize;\r
956 EFI_STATUS Status;\r
957\r
958 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r
959 return EFI_INVALID_PARAMETER;\r
960 }\r
51967a53 961\r
962 AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
963\r
8d3a5c82 964 //\r
965 // Find existing variable\r
966 //\r
967 Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
968\r
969 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
970 goto Done;\r
971 }\r
972 //\r
973 // Get data size\r
974 //\r
975 VarDataSize = Variable.CurrPtr->DataSize;\r
976 if (*DataSize >= VarDataSize) {\r
977 if (Data == NULL) {\r
978 Status = EFI_INVALID_PARAMETER;\r
979 goto Done;\r
980 }\r
981\r
982 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r
983 if (Attributes != NULL) {\r
984 *Attributes = Variable.CurrPtr->Attributes;\r
985 }\r
986\r
987 *DataSize = VarDataSize;\r
e4ddc008 988 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r
8d3a5c82 989 Status = EFI_SUCCESS;\r
990 goto Done;\r
991 } else {\r
992 *DataSize = VarDataSize;\r
993 Status = EFI_BUFFER_TOO_SMALL;\r
994 goto Done;\r
995 }\r
996\r
997Done:\r
998 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
999 return Status;\r
1000}\r
1001\r
1794e506 1002/**\r
1003\r
1004 This code Finds the Next available variable.\r
1005\r
1006 @param VariableNameSize Size of the variable.\r
1007 @param VariableName On input, supplies the last VariableName that was returned by GetNextVariableName().\r
1008 On output, returns the Null-terminated Unicode string of the current variable.\r
1009 @param VendorGuid On input, supplies the last VendorGuid that was returned by GetNextVariableName().\r
1010 On output, returns the VendorGuid of the current variable. \r
1011 @param Global Pointer to VARIABLE_GLOBAL structure.\r
1794e506 1012\r
1013 @retval EFI_SUCCESS The function completed successfully. \r
1014 @retval EFI_NOT_FOUND The next variable was not found.\r
1015 @retval EFI_BUFFER_TOO_SMALL VariableNameSize is too small for the result. \r
1016 VariableNameSize has been updated with the size needed to complete the request.\r
1017 @retval EFI_INVALID_PARAMETER VariableNameSize or VariableName or VendorGuid is NULL.\r
1018\r
1019**/\r
8d3a5c82 1020EFI_STATUS\r
1021EFIAPI\r
bd682470 1022EmuGetNextVariableName (\r
8d3a5c82 1023 IN OUT UINTN *VariableNameSize,\r
1024 IN OUT CHAR16 *VariableName,\r
1025 IN OUT EFI_GUID *VendorGuid,\r
72399dae 1026 IN VARIABLE_GLOBAL *Global\r
8d3a5c82 1027 )\r
8d3a5c82 1028{\r
1029 VARIABLE_POINTER_TRACK Variable;\r
1030 UINTN VarNameSize;\r
1031 EFI_STATUS Status;\r
1032\r
1033 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
1034 return EFI_INVALID_PARAMETER;\r
1035 }\r
1036\r
51967a53 1037 AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
1038\r
8d3a5c82 1039 Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
1040\r
1041 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
1042 goto Done;\r
1043 }\r
1044\r
1045 while (TRUE) {\r
1046 if (VariableName[0] != 0) {\r
1047 //\r
1048 // If variable name is not NULL, get next variable\r
1049 //\r
1050 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r
1051 }\r
1052 //\r
1053 // If both volatile and non-volatile variable store are parsed,\r
1054 // return not found\r
1055 //\r
1056 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {\r
1057 Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));\r
1058 if (Variable.Volatile) {\r
1059 Variable.StartPtr = (VARIABLE_HEADER *) ((UINTN) (Global->VolatileVariableBase + sizeof (VARIABLE_STORE_HEADER)));\r
1060 Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase));\r
1061 } else {\r
1062 Status = EFI_NOT_FOUND;\r
1063 goto Done;\r
1064 }\r
1065\r
1066 Variable.CurrPtr = Variable.StartPtr;\r
1067 if (Variable.CurrPtr->StartId != VARIABLE_DATA) {\r
1068 continue;\r
1069 }\r
1070 }\r
1071 //\r
1072 // Variable is found\r
1073 //\r
1074 if (Variable.CurrPtr->StartId == VARIABLE_DATA && Variable.CurrPtr->State == VAR_ADDED) {\r
ba5e88f9 1075 if (!(EfiAtRuntime () && ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0))) {\r
8d3a5c82 1076 VarNameSize = Variable.CurrPtr->NameSize;\r
1077 if (VarNameSize <= *VariableNameSize) {\r
1078 CopyMem (\r
1079 VariableName,\r
1080 GET_VARIABLE_NAME_PTR (Variable.CurrPtr),\r
1081 VarNameSize\r
1082 );\r
1083 CopyMem (\r
1084 VendorGuid,\r
1085 &Variable.CurrPtr->VendorGuid,\r
1086 sizeof (EFI_GUID)\r
1087 );\r
1088 Status = EFI_SUCCESS;\r
1089 } else {\r
1090 Status = EFI_BUFFER_TOO_SMALL;\r
1091 }\r
1092\r
1093 *VariableNameSize = VarNameSize;\r
1094 goto Done;\r
1095 }\r
1096 }\r
1097 }\r
1098\r
1099Done:\r
1100 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
1101 return Status;\r
1102\r
1103}\r
1104\r
1794e506 1105/**\r
1106\r
1107 This code sets variable in storage blocks (Volatile or Non-Volatile).\r
1108\r
1109 @param VariableName A Null-terminated Unicode string that is the name of the vendor's\r
1110 variable. Each VariableName is unique for each \r
1111 VendorGuid. VariableName must contain 1 or more \r
1112 Unicode characters. If VariableName is an empty Unicode \r
1113 string, then EFI_INVALID_PARAMETER is returned.\r
1114 @param VendorGuid A unique identifier for the vendor\r
1115 @param Attributes Attributes bitmask to set for the variable\r
1116 @param DataSize The size in bytes of the Data buffer. A size of zero causes the\r
1117 variable to be deleted.\r
1118 @param Data The contents for the variable\r
1119 @param Global Pointer to VARIABLE_GLOBAL structure\r
1120 @param VolatileOffset The offset of last volatile variable\r
1121 @param NonVolatileOffset The offset of last non-volatile variable\r
1794e506 1122\r
1123 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as \r
1124 defined by the Attributes.\r
1125 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied, or the \r
1126 DataSize exceeds the maximum allowed, or VariableName is an empty \r
1127 Unicode string, or VendorGuid is NULL.\r
1128 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.\r
1129 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.\r
1130 @retval EFI_WRITE_PROTECTED The variable in question is read-only or cannot be deleted.\r
1131 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.\r
1132\r
1133**/\r
8d3a5c82 1134EFI_STATUS\r
1135EFIAPI\r
bd682470 1136EmuSetVariable (\r
8d3a5c82 1137 IN CHAR16 *VariableName,\r
1138 IN EFI_GUID *VendorGuid,\r
1139 IN UINT32 Attributes,\r
1140 IN UINTN DataSize,\r
1141 IN VOID *Data,\r
1142 IN VARIABLE_GLOBAL *Global,\r
1143 IN UINTN *VolatileOffset,\r
72399dae 1144 IN UINTN *NonVolatileOffset\r
8d3a5c82 1145 )\r
8d3a5c82 1146{\r
1147 VARIABLE_POINTER_TRACK Variable;\r
1148 EFI_STATUS Status;\r
8d3a5c82 1149\r
c6492839 1150 //\r
1151 // Check input parameters\r
1152 //\r
8d3a5c82 1153 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r
1154 return EFI_INVALID_PARAMETER;\r
c6492839 1155 } \r
48a0e6bf 1156\r
1157 if (DataSize != 0 && Data == NULL) {\r
1158 return EFI_INVALID_PARAMETER;\r
1159 }\r
1160\r
c6492839 1161 //\r
1162 // Make sure if runtime bit is set, boot service bit is set also\r
1163 //\r
1164 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
1165 return EFI_INVALID_PARAMETER;\r
8d3a5c82 1166 }\r
c6492839 1167 //\r
1168 // The size of the VariableName, including the Unicode Null in bytes plus\r
e5618791
LG
1169 // the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxHardwareErrorVariableSize)\r
1170 // bytes for HwErrRec, and FixedPcdGet32(PcdMaxVariableSize) bytes for the others.\r
c6492839 1171 //\r
1172 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
e5618791
LG
1173 if ((DataSize > FixedPcdGet32(PcdMaxHardwareErrorVariableSize)) || \r
1174 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > FixedPcdGet32(PcdMaxHardwareErrorVariableSize))) {\r
c6492839 1175 return EFI_INVALID_PARAMETER;\r
0c420e26 1176 }\r
1177 //\r
1178 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"\r
1179 //\r
1180 if (StrnCmp(VariableName, L"HwErrRec", StrLen(L"HwErrRec")) != 0) {\r
1181 return EFI_INVALID_PARAMETER;\r
1182 }\r
c6492839 1183 } else {\r
1184 //\r
1185 // The size of the VariableName, including the Unicode Null in bytes plus\r
e5618791 1186 // the DataSize is limited to maximum size of FixedPcdGet32(PcdMaxVariableSize) bytes.\r
c6492839 1187 //\r
e5618791
LG
1188 if ((DataSize > FixedPcdGet32(PcdMaxVariableSize)) ||\r
1189 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > FixedPcdGet32(PcdMaxVariableSize))) {\r
c6492839 1190 return EFI_INVALID_PARAMETER;\r
1191 } \r
0c420e26 1192 }\r
1193\r
51967a53 1194 AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
1195\r
c6492839 1196 //\r
1197 // Check whether the input variable is already existed\r
1198 //\r
1199 \r
8d3a5c82 1200 Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
1201\r
c6492839 1202 //\r
72399dae 1203 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang\r
c6492839 1204 //\r
72399dae 1205 AutoUpdateLangVariable (VariableName, Data, DataSize);\r
c6492839 1206\r
72399dae 1207 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);\r
c6492839 1208\r
8d3a5c82 1209 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
1210 return Status;\r
1211}\r
1212\r
1794e506 1213/**\r
1214\r
1215 This code returns information about the EFI variables.\r
1216\r
1217 @param Attributes Attributes bitmask to specify the type of variables\r
1218 on which to return information.\r
1219 @param MaximumVariableStorageSize On output the maximum size of the storage space available for \r
1220 the EFI variables associated with the attributes specified. \r
1221 @param RemainingVariableStorageSize Returns the remaining size of the storage space available for EFI \r
1222 variables associated with the attributes specified.\r
1223 @param MaximumVariableSize Returns the maximum size of an individual EFI variable \r
1224 associated with the attributes specified.\r
1225 @param Global Pointer to VARIABLE_GLOBAL structure.\r
1794e506 1226\r
1227 @retval EFI_SUCCESS Valid answer returned.\r
1228 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied\r
1229 @retval EFI_UNSUPPORTED The attribute is not supported on this platform, and the \r
1230 MaximumVariableStorageSize, RemainingVariableStorageSize, \r
1231 MaximumVariableSize are undefined.\r
1232\r
1233**/\r
8d3a5c82 1234EFI_STATUS\r
1235EFIAPI\r
bd682470 1236EmuQueryVariableInfo (\r
8d3a5c82 1237 IN UINT32 Attributes,\r
1238 OUT UINT64 *MaximumVariableStorageSize,\r
1239 OUT UINT64 *RemainingVariableStorageSize,\r
1240 OUT UINT64 *MaximumVariableSize,\r
72399dae 1241 IN VARIABLE_GLOBAL *Global\r
8d3a5c82 1242 )\r
8d3a5c82 1243{\r
1244 VARIABLE_HEADER *Variable;\r
1245 VARIABLE_HEADER *NextVariable;\r
1246 UINT64 VariableSize;\r
1247 VARIABLE_STORE_HEADER *VariableStoreHeader;\r
72399dae 1248 UINT64 CommonVariableTotalSize;\r
1249 UINT64 HwErrVariableTotalSize;\r
1250\r
1251 CommonVariableTotalSize = 0;\r
1252 HwErrVariableTotalSize = 0;\r
8d3a5c82 1253\r
c6492839 1254 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r
8d3a5c82 1255 return EFI_INVALID_PARAMETER;\r
1256 }\r
c6492839 1257 \r
1258 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r
8d3a5c82 1259 //\r
1260 // Make sure the Attributes combination is supported by the platform.\r
1261 //\r
c6492839 1262 return EFI_UNSUPPORTED; \r
8d3a5c82 1263 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r
1264 //\r
1265 // Make sure if runtime bit is set, boot service bit is set also.\r
1266 //\r
1267 return EFI_INVALID_PARAMETER;\r
1794e506 1268 } else if (EfiAtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r
8d3a5c82 1269 //\r
1270 // Make sure RT Attribute is set if we are in Runtime phase.\r
1271 //\r
1272 return EFI_INVALID_PARAMETER;\r
72399dae 1273 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1274 //\r
1275 // Make sure Hw Attribute is set with NV.\r
1276 //\r
1277 return EFI_INVALID_PARAMETER;\r
8d3a5c82 1278 }\r
1279\r
1280 AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
1281\r
1282 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r
1283 //\r
1284 // Query is Volatile related.\r
1285 //\r
1286 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);\r
1287 } else {\r
1288 //\r
1289 // Query is Non-Volatile related.\r
1290 //\r
1291 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) Global->NonVolatileVariableBase);\r
1292 }\r
1293\r
1294 //\r
1295 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r
1296 // with the storage size (excluding the storage header size)\r
1297 //\r
1298 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r
c6492839 1299\r
1300 //\r
1301 // Harware error record variable needs larger size.\r
1302 //\r
72399dae 1303 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r
1304 *MaximumVariableStorageSize = FixedPcdGet32(PcdHwErrStorageSize);\r
e5618791 1305 *MaximumVariableSize = FixedPcdGet32(PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r
72399dae 1306 } else {\r
1307 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r
1308 ASSERT (FixedPcdGet32(PcdHwErrStorageSize) < VariableStoreHeader->Size);\r
1309 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - FixedPcdGet32(PcdHwErrStorageSize);\r
1310 }\r
1311\r
1312 //\r
1313 // Let *MaximumVariableSize be FixedPcdGet32(PcdMaxVariableSize) with the exception of the variable header size.\r
1314 //\r
1315 *MaximumVariableSize = FixedPcdGet32(PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r
c6492839 1316 }\r
8d3a5c82 1317\r
1318 //\r
1319 // Point to the starting address of the variables.\r
1320 //\r
1321 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r
1322\r
1323 //\r
1324 // Now walk through the related variable store.\r
1325 //\r
1326 while (Variable < GetEndPointer (VariableStoreHeader)) {\r
72399dae 1327 NextVariable = GetNextVariablePtr(Variable);\r
1328 if (NextVariable == NULL) {\r
8d3a5c82 1329 break;\r
1330 }\r
8d3a5c82 1331 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r
1332\r
72399dae 1333 if ((NextVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r
1334 HwErrVariableTotalSize += VariableSize;\r
1335 } else {\r
1336 CommonVariableTotalSize += VariableSize;\r
8d3a5c82 1337 }\r
1338\r
1339 //\r
1340 // Go to the next one.\r
1341 //\r
1342 Variable = NextVariable;\r
1343 }\r
1344\r
72399dae 1345 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r
1346 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r
1347 } else {\r
1348 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r
1349 }\r
1350\r
c6492839 1351 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r
1352 *MaximumVariableSize = 0;\r
1353 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r
1354 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r
1355 }\r
1356 \r
8d3a5c82 1357 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);\r
1358 return EFI_SUCCESS;\r
1359}\r
1360\r
1794e506 1361/**\r
1362 Initializes variable store area.\r
1363\r
1364 This function allocates memory space for variable store area and initializes its attributes.\r
1365\r
1366 @param VariableBase Base of the variable store area created\r
1367 @param LastVariableOffset Size of VARIABLE_STORE_HEADER\r
1368\r
1369**/\r
8d3a5c82 1370EFI_STATUS\r
8d3a5c82 1371InitializeVariableStore (\r
02328a57 1372 IN BOOLEAN VolatileStore\r
8d3a5c82 1373 )\r
8d3a5c82 1374{\r
1375 VARIABLE_STORE_HEADER *VariableStore;\r
02328a57 1376 BOOLEAN FullyInitializeStore;\r
1377 EFI_PHYSICAL_ADDRESS *VariableBase;\r
1378 UINTN *LastVariableOffset;\r
1379\r
1380 FullyInitializeStore = TRUE;\r
1381\r
1382 if (VolatileStore) {\r
1383 VariableBase = &mVariableModuleGlobal->VariableGlobal[Physical].VolatileVariableBase;\r
1384 LastVariableOffset = &mVariableModuleGlobal->VolatileLastVariableOffset;\r
1385 } else {\r
1386 VariableBase = &mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase;\r
1387 LastVariableOffset = &mVariableModuleGlobal->NonVolatileLastVariableOffset;\r
1388 }\r
8d3a5c82 1389\r
48cd992a 1390 //\r
1391 // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r
1392 // is stored with common variable in the same NV region. So the platform integrator should\r
1393 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of \r
93c288f2 1394 // PcdVariableStoreSize.\r
48cd992a 1395 //\r
93c288f2 1396 ASSERT (FixedPcdGet32(PcdHwErrStorageSize) <= FixedPcdGet32(PcdVariableStoreSize));\r
48cd992a 1397\r
8d3a5c82 1398 //\r
02328a57 1399 // Allocate memory for variable store.\r
8d3a5c82 1400 //\r
02328a57 1401 if (VolatileStore || (PcdGet64 (PcdEmuVariableNvStoreReserved) == 0)) {\r
1402 VariableStore = (VARIABLE_STORE_HEADER *) AllocateRuntimePool (\r
1403 FixedPcdGet32(PcdVariableStoreSize)\r
1404 );\r
1405 } else {\r
1406 //\r
1407 // A memory location has been reserved for the NV variable store. Certain\r
1408 // platforms may be able to preserve a memory range across system resets,\r
1409 // thereby providing better NV variable emulation.\r
1410 //\r
1411 VariableStore =\r
1412 (VARIABLE_STORE_HEADER *)(VOID*)(UINTN)\r
1413 PcdGet64 (PcdEmuVariableNvStoreReserved);\r
1414 if (\r
1415 (VariableStore->Size == FixedPcdGet32(PcdVariableStoreSize)) &&\r
1416 (VariableStore->Format == VARIABLE_STORE_FORMATTED) &&\r
1417 (VariableStore->State == VARIABLE_STORE_HEALTHY)\r
1418 ) {\r
1419 DEBUG((\r
1420 EFI_D_INFO,\r
1421 "Variable Store reserved at %p appears to be valid\n",\r
1422 VariableStore\r
1423 ));\r
1424 FullyInitializeStore = FALSE;\r
1425 }\r
1426 }\r
1427\r
8d3a5c82 1428 if (NULL == VariableStore) {\r
1429 return EFI_OUT_OF_RESOURCES;\r
1430 }\r
1431\r
02328a57 1432 if (FullyInitializeStore) {\r
1433 SetMem (VariableStore, FixedPcdGet32(PcdVariableStoreSize), 0xff);\r
1434 }\r
8d3a5c82 1435\r
1436 //\r
1437 // Variable Specific Data\r
1438 //\r
1439 *VariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStore;\r
02328a57 1440 InitializeLocationForLastVariableOffset (VariableStore, LastVariableOffset);\r
8d3a5c82 1441\r
3709c4cd 1442 CopyGuid (&VariableStore->Signature, &gEfiVariableGuid);\r
e5618791 1443 VariableStore->Size = FixedPcdGet32(PcdVariableStoreSize);\r
8d3a5c82 1444 VariableStore->Format = VARIABLE_STORE_FORMATTED;\r
1445 VariableStore->State = VARIABLE_STORE_HEALTHY;\r
1446 VariableStore->Reserved = 0;\r
1447 VariableStore->Reserved1 = 0;\r
1448\r
1449 return EFI_SUCCESS;\r
1450}\r
1451\r
1794e506 1452/**\r
1453 Initializes variable store area for non-volatile and volatile variable.\r
1454\r
1455 This function allocates and initializes memory space for global context of ESAL\r
1456 variable service and variable store area for non-volatile and volatile variable.\r
1457\r
1458 @param ImageHandle The Image handle of this driver.\r
1459 @param SystemTable The pointer of EFI_SYSTEM_TABLE.\r
1460\r
1461 @retval EFI_SUCCESS Function successfully executed.\r
1462 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r
1463\r
1464**/\r
8d3a5c82 1465EFI_STATUS\r
1466EFIAPI\r
1467VariableCommonInitialize (\r
1468 IN EFI_HANDLE ImageHandle,\r
1469 IN EFI_SYSTEM_TABLE *SystemTable\r
1470 )\r
8d3a5c82 1471{\r
1472 EFI_STATUS Status;\r
1473\r
1474 //\r
1475 // Allocate memory for mVariableModuleGlobal\r
1476 //\r
72399dae 1477 mVariableModuleGlobal = (ESAL_VARIABLE_GLOBAL *) AllocateRuntimeZeroPool (\r
8d3a5c82 1478 sizeof (ESAL_VARIABLE_GLOBAL)\r
1479 );\r
1480 if (NULL == mVariableModuleGlobal) {\r
1481 return EFI_OUT_OF_RESOURCES;\r
1482 }\r
1483\r
1484 EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal[Physical].VariableServicesLock, TPL_NOTIFY);\r
1485\r
1486 //\r
1487 // Intialize volatile variable store\r
1488 //\r
02328a57 1489 Status = InitializeVariableStore (TRUE);\r
8d3a5c82 1490 if (EFI_ERROR (Status)) {\r
5bb820af 1491 FreePool(mVariableModuleGlobal);\r
8d3a5c82 1492 return Status;\r
1493 }\r
1494 //\r
1495 // Intialize non volatile variable store\r
1496 //\r
02328a57 1497 Status = InitializeVariableStore (FALSE);\r
8d3a5c82 1498\r
1499 return Status;\r
1500}\r