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