]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Variable/EmuRuntimeDxe/EmuVariable.c
Remove unnecessary use of FixedPcdxxx() functions and [FixedPcd] INF sections. These...
[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
7 All rights reserved. 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 EFIAPI
399 GetIndexFromSupportedLangCodes(
400 IN CHAR8 *SupportedLang,
401 IN CHAR8 *Lang,
402 IN BOOLEAN Iso639Language
403 )
404 {
405 UINTN Index;
406 UINT32 CompareLength;
407 CHAR8 *Supported;
408
409 Index = 0;
410 Supported = SupportedLang;
411 if (Iso639Language) {
412 CompareLength = 3;
413 for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {
414 if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {
415 //
416 // Successfully find the index of Lang string in SupportedLang string.
417 //
418 Index = Index / CompareLength;
419 return Index;
420 }
421 }
422 ASSERT (FALSE);
423 return 0;
424 } else {
425 //
426 // Compare RFC4646 language code
427 //
428 while (*Supported != '\0') {
429 //
430 // take semicolon as delimitation, sequentially traverse supported language codes.
431 //
432 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {
433 Supported++;
434 }
435 if (AsciiStrnCmp (Lang, Supported - CompareLength, CompareLength) == 0) {
436 //
437 // Successfully find the index of Lang string in SupportedLang string.
438 //
439 return Index;
440 }
441 Index++;
442 }
443 ASSERT (FALSE);
444 return 0;
445 }
446 }
447
448 /**
449 Get language string from supported language codes according to index.
450
451 This code is used to get corresponding language string in supported language codes. It can handle
452 RFC4646 and ISO639 language tags.
453 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
454 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
455
456 For example:
457 SupportedLang = "engfraengfra"
458 Index = "1"
459 Iso639Language = TRUE
460 The return value is "fra".
461 Another example:
462 SupportedLang = "en;fr;en-US;fr-FR"
463 Index = "1"
464 Iso639Language = FALSE
465 The return value is "fr".
466
467 @param SupportedLang Platform supported language codes.
468 @param Index the index in supported language codes.
469 @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
470
471 @retval the language string in the language codes.
472
473 **/
474 CHAR8 *
475 EFIAPI
476 GetLangFromSupportedLangCodes (
477 IN CHAR8 *SupportedLang,
478 IN UINTN Index,
479 IN BOOLEAN Iso639Language
480 )
481 {
482 UINTN SubIndex;
483 UINT32 CompareLength;
484 CHAR8 *Supported;
485
486 SubIndex = 0;
487 Supported = SupportedLang;
488 if (Iso639Language) {
489 //
490 // according to the index of Lang string in SupportedLang string to get the language.
491 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
492 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
493 //
494 CompareLength = 3;
495 SetMem (mVariableModuleGlobal->Lang, sizeof(mVariableModuleGlobal->Lang), 0);
496 return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);
497
498 } else {
499 while (TRUE) {
500 //
501 // take semicolon as delimitation, sequentially traverse supported language codes.
502 //
503 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {
504 Supported++;
505 }
506 if ((*Supported == '\0') && (SubIndex != Index)) {
507 //
508 // Have completed the traverse, but not find corrsponding string.
509 // This case is not allowed to happen.
510 //
511 ASSERT(FALSE);
512 return NULL;
513 }
514 if (SubIndex == Index) {
515 //
516 // according to the index of Lang string in SupportedLang string to get the language.
517 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
518 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
519 //
520 SetMem (mVariableModuleGlobal->PlatformLang, sizeof (mVariableModuleGlobal->PlatformLang), 0);
521 return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);
522 }
523 SubIndex++;
524 }
525 }
526 }
527
528 /**
529 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
530
531 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
532
533 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
534 and are read-only. Therefore, in variable driver, only store the original value for other use.
535
536 @param[in] VariableName Name of variable
537
538 @param[in] Data Variable data
539
540 @param[in] DataSize Size of data. 0 means delete
541
542 @retval EFI_SUCCESS auto update operation is successful.
543
544 **/
545 EFI_STATUS
546 EFIAPI
547 AutoUpdateLangVariable(
548 IN CHAR16 *VariableName,
549 IN VOID *Data,
550 IN UINTN DataSize
551 )
552 {
553 EFI_STATUS Status;
554 CHAR8 *BestPlatformLang;
555 CHAR8 *BestLang;
556 UINTN Index;
557 UINT32 Attributes;
558 VARIABLE_POINTER_TRACK Variable;
559
560 //
561 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
562 //
563 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
564
565 if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {
566 //
567 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
568 // Therefore, in variable driver, only store the original value for other use.
569 //
570 AsciiStrnCpy (mVariableModuleGlobal->PlatformLangCodes, Data, DataSize);
571 } else if (StrCmp (VariableName, L"LangCodes") == 0) {
572 //
573 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
574 // Therefore, in variable driver, only store the original value for other use.
575 //
576 AsciiStrnCpy (mVariableModuleGlobal->LangCodes, Data, DataSize);
577 } else if ((StrCmp (VariableName, L"PlatformLang") == 0) && (DataSize != 0)) {
578 ASSERT (AsciiStrLen (mVariableModuleGlobal->PlatformLangCodes) != 0);
579
580 //
581 // When setting PlatformLang, firstly get most matched language string from supported language codes.
582 //
583 BestPlatformLang = GetBestLanguage(mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);
584
585 //
586 // Get the corresponding index in language codes.
587 //
588 Index = GetIndexFromSupportedLangCodes(mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);
589
590 //
591 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
592 //
593 BestLang = GetLangFromSupportedLangCodes(mVariableModuleGlobal->LangCodes, Index, TRUE);
594
595 //
596 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
597 //
598 FindVariable(L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);
599
600 Status = UpdateVariable(L"Lang", &gEfiGlobalVariableGuid,
601 BestLang, ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);
602
603 DEBUG((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));
604
605 ASSERT_EFI_ERROR(Status);
606
607 } else if ((StrCmp (VariableName, L"Lang") == 0) && (DataSize != 0)) {
608 ASSERT (AsciiStrLen (mVariableModuleGlobal->LangCodes) != 0);
609
610 //
611 // When setting Lang, firstly get most matched language string from supported language codes.
612 //
613 BestLang = GetBestLanguage(mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);
614
615 //
616 // Get the corresponding index in language codes.
617 //
618 Index = GetIndexFromSupportedLangCodes(mVariableModuleGlobal->LangCodes, BestLang, TRUE);
619
620 //
621 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
622 //
623 BestPlatformLang = GetLangFromSupportedLangCodes(mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);
624
625 //
626 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
627 //
628 FindVariable(L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);
629
630 Status = UpdateVariable(L"PlatformLang", &gEfiGlobalVariableGuid,
631 BestPlatformLang, AsciiStrSize (BestPlatformLang), Attributes, &Variable);
632
633 DEBUG((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));
634 ASSERT_EFI_ERROR(Status);
635 }
636 return EFI_SUCCESS;
637 }
638
639 /**
640 Update the variable region with Variable information. These are the same
641 arguments as the EFI Variable services.
642
643 @param[in] VariableName Name of variable
644
645 @param[in] VendorGuid Guid of variable
646
647 @param[in] Data Variable data
648
649 @param[in] DataSize Size of data. 0 means delete
650
651 @param[in] Attributes Attribues of the variable
652
653 @param[in] Variable The variable information which is used to keep track of variable usage.
654
655 @retval EFI_SUCCESS The update operation is success.
656
657 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
658
659 **/
660 EFI_STATUS
661 EFIAPI
662 UpdateVariable (
663 IN CHAR16 *VariableName,
664 IN EFI_GUID *VendorGuid,
665 IN VOID *Data,
666 IN UINTN DataSize,
667 IN UINT32 Attributes OPTIONAL,
668 IN VARIABLE_POINTER_TRACK *Variable
669 )
670 {
671 EFI_STATUS Status;
672 VARIABLE_HEADER *NextVariable;
673 UINTN VarNameSize;
674 UINTN VarNameOffset;
675 UINTN VarDataOffset;
676 UINTN VarSize;
677 VARIABLE_GLOBAL *Global;
678 UINTN NonVolatileVarableStoreSize;
679
680 Global = &mVariableModuleGlobal->VariableGlobal[Physical];
681
682 if (Variable->CurrPtr != NULL) {
683 //
684 // Update/Delete existing variable
685 //
686
687 if (EfiAtRuntime ()) {
688 //
689 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
690 // the volatile is ReadOnly, and SetVariable should be aborted and
691 // return EFI_WRITE_PROTECTED.
692 //
693 if (Variable->Volatile) {
694 Status = EFI_WRITE_PROTECTED;
695 goto Done;
696 }
697 //
698 // Only variable have NV attribute can be updated/deleted in Runtime
699 //
700 if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
701 Status = EFI_INVALID_PARAMETER;
702 goto Done;
703 }
704 }
705
706 //
707 // Setting a data variable with no access, or zero DataSize attributes
708 // specified causes it to be deleted.
709 //
710 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
711 Variable->CurrPtr->State &= VAR_DELETED;
712 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);
713 Status = EFI_SUCCESS;
714 goto Done;
715 }
716
717 //
718 // If the variable is marked valid and the same data has been passed in
719 // then return to the caller immediately.
720 //
721 if (Variable->CurrPtr->DataSize == DataSize &&
722 CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0
723 ) {
724 Status = EFI_SUCCESS;
725 goto Done;
726 } else if (Variable->CurrPtr->State == VAR_ADDED) {
727 //
728 // Mark the old variable as in delete transition
729 //
730 Variable->CurrPtr->State &= VAR_IN_DELETED_TRANSITION;
731 }
732
733 } else {
734 //
735 // No found existing variable, Create a new variable
736 //
737
738 //
739 // Make sure we are trying to create a new variable.
740 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
741 //
742 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
743 Status = EFI_NOT_FOUND;
744 goto Done;
745 }
746
747 //
748 // Only variable have NV|RT attribute can be created in Runtime
749 //
750 if (EfiAtRuntime () &&
751 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {
752 Status = EFI_INVALID_PARAMETER;
753 goto Done;
754 }
755 }
756
757 //
758 // Function part - create a new variable and copy the data.
759 // Both update a variable and create a variable will come here.
760 //
761
762 VarNameOffset = sizeof (VARIABLE_HEADER);
763 VarNameSize = StrSize (VariableName);
764 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);
765 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);
766
767 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
768 NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(Global->NonVolatileVariableBase))->Size;
769 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)
770 && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))
771 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)
772 && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {
773 Status = EFI_OUT_OF_RESOURCES;
774 goto Done;
775 }
776
777 NextVariable = (VARIABLE_HEADER *) (UINT8 *) (mVariableModuleGlobal->NonVolatileLastVariableOffset
778 + (UINTN) Global->NonVolatileVariableBase);
779 mVariableModuleGlobal->NonVolatileLastVariableOffset += VarSize;
780
781 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {
782 mVariableModuleGlobal->HwErrVariableTotalSize += VarSize;
783 } else {
784 mVariableModuleGlobal->CommonVariableTotalSize += VarSize;
785 }
786 } else {
787 if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >
788 ((VARIABLE_STORE_HEADER *) ((UINTN) (Global->VolatileVariableBase)))->Size
789 ) {
790 Status = EFI_OUT_OF_RESOURCES;
791 goto Done;
792 }
793
794 NextVariable = (VARIABLE_HEADER *) (UINT8 *) (mVariableModuleGlobal->VolatileLastVariableOffset
795 + (UINTN) Global->VolatileVariableBase);
796 mVariableModuleGlobal->VolatileLastVariableOffset += VarSize;
797 }
798
799 NextVariable->StartId = VARIABLE_DATA;
800 NextVariable->Attributes = Attributes;
801 NextVariable->State = VAR_ADDED;
802 NextVariable->Reserved = 0;
803
804 //
805 // There will be pad bytes after Data, the NextVariable->NameSize and
806 // NextVariable->NameSize should not include pad size so that variable
807 // service can get actual size in GetVariable
808 //
809 NextVariable->NameSize = (UINT32)VarNameSize;
810 NextVariable->DataSize = (UINT32)DataSize;
811
812 CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));
813 CopyMem (
814 (UINT8 *) ((UINTN) NextVariable + VarNameOffset),
815 VariableName,
816 VarNameSize
817 );
818 CopyMem (
819 (UINT8 *) ((UINTN) NextVariable + VarDataOffset),
820 Data,
821 DataSize
822 );
823
824 //
825 // Mark the old variable as deleted
826 //
827 if (Variable->CurrPtr != NULL) {
828 Variable->CurrPtr->State &= VAR_DELETED;
829 }
830
831 UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);
832
833 Status = EFI_SUCCESS;
834
835 Done:
836 return Status;
837 }
838
839 /**
840 Finds variable in storage blocks of volatile and non-volatile storage areas.
841
842 This code finds variable in storage blocks of volatile and non-volatile storage areas.
843 If VariableName is an empty string, then we just return the first
844 qualified variable without comparing VariableName and VendorGuid.
845 Otherwise, VariableName and VendorGuid are compared.
846
847 @param VariableName Name of the variable to be found.
848 @param VendorGuid Vendor GUID to be found.
849 @param PtrTrack VARIABLE_POINTER_TRACK structure for output,
850 including the range searched and the target position.
851 @param Global Pointer to VARIABLE_GLOBAL structure, including
852 base of volatile variable storage area, base of
853 NV variable storage area, and a lock.
854
855 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
856 VendorGuid is NULL.
857 @retval EFI_SUCCESS Variable successfully found.
858 @retval EFI_NOT_FOUND Variable not found.
859
860 **/
861 EFI_STATUS
862 FindVariable (
863 IN CHAR16 *VariableName,
864 IN EFI_GUID *VendorGuid,
865 OUT VARIABLE_POINTER_TRACK *PtrTrack,
866 IN VARIABLE_GLOBAL *Global
867 )
868 {
869 VARIABLE_HEADER *Variable[2];
870 VARIABLE_STORE_HEADER *VariableStoreHeader[2];
871 UINTN Index;
872
873 //
874 // 0: Non-Volatile, 1: Volatile
875 //
876 VariableStoreHeader[0] = (VARIABLE_STORE_HEADER *) ((UINTN) Global->NonVolatileVariableBase);
877 VariableStoreHeader[1] = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);
878
879 //
880 // Start Pointers for the variable.
881 // Actual Data Pointer where data can be written.
882 //
883 Variable[0] = (VARIABLE_HEADER *) (VariableStoreHeader[0] + 1);
884 Variable[1] = (VARIABLE_HEADER *) (VariableStoreHeader[1] + 1);
885
886 if (VariableName[0] != 0 && VendorGuid == NULL) {
887 return EFI_INVALID_PARAMETER;
888 }
889 //
890 // Find the variable by walk through non-volatile and volatile variable store
891 //
892 for (Index = 0; Index < 2; Index++) {
893 PtrTrack->StartPtr = (VARIABLE_HEADER *) (VariableStoreHeader[Index] + 1);
894 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);
895
896 while ((Variable[Index] < GetEndPointer (VariableStoreHeader[Index])) && (Variable[Index] != NULL)) {
897 if (Variable[Index]->StartId == VARIABLE_DATA && Variable[Index]->State == VAR_ADDED) {
898 if (!(EfiAtRuntime () && ((Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0))) {
899 if (VariableName[0] == 0) {
900 PtrTrack->CurrPtr = Variable[Index];
901 PtrTrack->Volatile = (BOOLEAN) Index;
902 return EFI_SUCCESS;
903 } else {
904 if (CompareGuid (VendorGuid, &Variable[Index]->VendorGuid)) {
905 if (CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable[Index]), Variable[Index]->NameSize) == 0) {
906 PtrTrack->CurrPtr = Variable[Index];
907 PtrTrack->Volatile = (BOOLEAN) Index;
908 return EFI_SUCCESS;
909 }
910 }
911 }
912 }
913 }
914
915 Variable[Index] = GetNextVariablePtr (Variable[Index]);
916 }
917 }
918 PtrTrack->CurrPtr = NULL;
919 return EFI_NOT_FOUND;
920 }
921
922 /**
923 This code finds variable in storage blocks (Volatile or Non-Volatile).
924
925 @param VariableName A Null-terminated Unicode string that is the name of
926 the vendor's variable.
927 @param VendorGuid A unique identifier for the vendor.
928 @param Attributes If not NULL, a pointer to the memory location to return the
929 attributes bitmask for the variable.
930 @param DataSize Size of Data found. If size is less than the
931 data, this value contains the required size.
932 @param Data On input, the size in bytes of the return Data buffer.
933 On output, the size of data returned in Data.
934 @param Global Pointer to VARIABLE_GLOBAL structure
935
936 @retval EFI_SUCCESS The function completed successfully.
937 @retval EFI_NOT_FOUND The variable was not found.
938 @retval EFI_BUFFER_TOO_SMALL DataSize is too small for the result. DataSize has
939 been updated with the size needed to complete the request.
940 @retval EFI_INVALID_PARAMETER VariableName or VendorGuid or DataSize is NULL.
941
942 **/
943 EFI_STATUS
944 EFIAPI
945 EmuGetVariable (
946 IN CHAR16 *VariableName,
947 IN EFI_GUID *VendorGuid,
948 OUT UINT32 *Attributes OPTIONAL,
949 IN OUT UINTN *DataSize,
950 OUT VOID *Data,
951 IN VARIABLE_GLOBAL *Global
952 )
953 {
954 VARIABLE_POINTER_TRACK Variable;
955 UINTN VarDataSize;
956 EFI_STATUS Status;
957
958 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
959 return EFI_INVALID_PARAMETER;
960 }
961
962 AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);
963
964 //
965 // Find existing variable
966 //
967 Status = FindVariable (VariableName, VendorGuid, &Variable, Global);
968
969 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
970 goto Done;
971 }
972 //
973 // Get data size
974 //
975 VarDataSize = Variable.CurrPtr->DataSize;
976 if (*DataSize >= VarDataSize) {
977 if (Data == NULL) {
978 Status = EFI_INVALID_PARAMETER;
979 goto Done;
980 }
981
982 CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);
983 if (Attributes != NULL) {
984 *Attributes = Variable.CurrPtr->Attributes;
985 }
986
987 *DataSize = VarDataSize;
988 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);
989 Status = EFI_SUCCESS;
990 goto Done;
991 } else {
992 *DataSize = VarDataSize;
993 Status = EFI_BUFFER_TOO_SMALL;
994 goto Done;
995 }
996
997 Done:
998 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);
999 return Status;
1000 }
1001
1002 /**
1003
1004 This code Finds the Next available variable.
1005
1006 @param VariableNameSize Size of the variable.
1007 @param VariableName On input, supplies the last VariableName that was returned by GetNextVariableName().
1008 On output, returns the Null-terminated Unicode string of the current variable.
1009 @param VendorGuid On input, supplies the last VendorGuid that was returned by GetNextVariableName().
1010 On output, returns the VendorGuid of the current variable.
1011 @param Global Pointer to VARIABLE_GLOBAL structure.
1012
1013 @retval EFI_SUCCESS The function completed successfully.
1014 @retval EFI_NOT_FOUND The next variable was not found.
1015 @retval EFI_BUFFER_TOO_SMALL VariableNameSize is too small for the result.
1016 VariableNameSize has been updated with the size needed to complete the request.
1017 @retval EFI_INVALID_PARAMETER VariableNameSize or VariableName or VendorGuid is NULL.
1018
1019 **/
1020 EFI_STATUS
1021 EFIAPI
1022 EmuGetNextVariableName (
1023 IN OUT UINTN *VariableNameSize,
1024 IN OUT CHAR16 *VariableName,
1025 IN OUT EFI_GUID *VendorGuid,
1026 IN VARIABLE_GLOBAL *Global
1027 )
1028 {
1029 VARIABLE_POINTER_TRACK Variable;
1030 UINTN VarNameSize;
1031 EFI_STATUS Status;
1032
1033 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
1034 return EFI_INVALID_PARAMETER;
1035 }
1036
1037 AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);
1038
1039 Status = FindVariable (VariableName, VendorGuid, &Variable, Global);
1040
1041 if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {
1042 goto Done;
1043 }
1044
1045 while (TRUE) {
1046 if (VariableName[0] != 0) {
1047 //
1048 // If variable name is not NULL, get next variable
1049 //
1050 Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);
1051 }
1052 //
1053 // If both volatile and non-volatile variable store are parsed,
1054 // return not found
1055 //
1056 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {
1057 Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));
1058 if (Variable.Volatile) {
1059 Variable.StartPtr = (VARIABLE_HEADER *) ((UINTN) (Global->VolatileVariableBase + sizeof (VARIABLE_STORE_HEADER)));
1060 Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase));
1061 } else {
1062 Status = EFI_NOT_FOUND;
1063 goto Done;
1064 }
1065
1066 Variable.CurrPtr = Variable.StartPtr;
1067 if (Variable.CurrPtr->StartId != VARIABLE_DATA) {
1068 continue;
1069 }
1070 }
1071 //
1072 // Variable is found
1073 //
1074 if (Variable.CurrPtr->StartId == VARIABLE_DATA && Variable.CurrPtr->State == VAR_ADDED) {
1075 if (!(EfiAtRuntime () && ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0))) {
1076 VarNameSize = Variable.CurrPtr->NameSize;
1077 if (VarNameSize <= *VariableNameSize) {
1078 CopyMem (
1079 VariableName,
1080 GET_VARIABLE_NAME_PTR (Variable.CurrPtr),
1081 VarNameSize
1082 );
1083 CopyMem (
1084 VendorGuid,
1085 &Variable.CurrPtr->VendorGuid,
1086 sizeof (EFI_GUID)
1087 );
1088 Status = EFI_SUCCESS;
1089 } else {
1090 Status = EFI_BUFFER_TOO_SMALL;
1091 }
1092
1093 *VariableNameSize = VarNameSize;
1094 goto Done;
1095 }
1096 }
1097 }
1098
1099 Done:
1100 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);
1101 return Status;
1102
1103 }
1104
1105 /**
1106
1107 This code sets variable in storage blocks (Volatile or Non-Volatile).
1108
1109 @param VariableName A Null-terminated Unicode string that is the name of the vendor's
1110 variable. Each VariableName is unique for each
1111 VendorGuid. VariableName must contain 1 or more
1112 Unicode characters. If VariableName is an empty Unicode
1113 string, then EFI_INVALID_PARAMETER is returned.
1114 @param VendorGuid A unique identifier for the vendor
1115 @param Attributes Attributes bitmask to set for the variable
1116 @param DataSize The size in bytes of the Data buffer. A size of zero causes the
1117 variable to be deleted.
1118 @param Data The contents for the variable
1119 @param Global Pointer to VARIABLE_GLOBAL structure
1120 @param VolatileOffset The offset of last volatile variable
1121 @param NonVolatileOffset The offset of last non-volatile variable
1122
1123 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
1124 defined by the Attributes.
1125 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied, or the
1126 DataSize exceeds the maximum allowed, or VariableName is an empty
1127 Unicode string, or VendorGuid is NULL.
1128 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
1129 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.
1130 @retval EFI_WRITE_PROTECTED The variable in question is read-only or cannot be deleted.
1131 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
1132
1133 **/
1134 EFI_STATUS
1135 EFIAPI
1136 EmuSetVariable (
1137 IN CHAR16 *VariableName,
1138 IN EFI_GUID *VendorGuid,
1139 IN UINT32 Attributes,
1140 IN UINTN DataSize,
1141 IN VOID *Data,
1142 IN VARIABLE_GLOBAL *Global,
1143 IN UINTN *VolatileOffset,
1144 IN UINTN *NonVolatileOffset
1145 )
1146 {
1147 VARIABLE_POINTER_TRACK Variable;
1148 EFI_STATUS Status;
1149
1150 //
1151 // Check input parameters
1152 //
1153 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
1154 return EFI_INVALID_PARAMETER;
1155 }
1156
1157 if (DataSize != 0 && Data == NULL) {
1158 return EFI_INVALID_PARAMETER;
1159 }
1160
1161 //
1162 // Make sure if runtime bit is set, boot service bit is set also
1163 //
1164 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
1165 return EFI_INVALID_PARAMETER;
1166 }
1167 //
1168 // The size of the VariableName, including the Unicode Null in bytes plus
1169 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)
1170 // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.
1171 //
1172 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
1173 if ((DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize)) ||
1174 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize))) {
1175 return EFI_INVALID_PARAMETER;
1176 }
1177 //
1178 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"
1179 //
1180 if (StrnCmp(VariableName, L"HwErrRec", StrLen(L"HwErrRec")) != 0) {
1181 return EFI_INVALID_PARAMETER;
1182 }
1183 } else {
1184 //
1185 // The size of the VariableName, including the Unicode Null in bytes plus
1186 // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.
1187 //
1188 if ((DataSize > PcdGet32 (PcdMaxVariableSize)) ||
1189 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxVariableSize))) {
1190 return EFI_INVALID_PARAMETER;
1191 }
1192 }
1193
1194 AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);
1195
1196 //
1197 // Check whether the input variable is already existed
1198 //
1199
1200 Status = FindVariable (VariableName, VendorGuid, &Variable, Global);
1201
1202 //
1203 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang
1204 //
1205 AutoUpdateLangVariable (VariableName, Data, DataSize);
1206
1207 Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);
1208
1209 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);
1210 return Status;
1211 }
1212
1213 /**
1214
1215 This code returns information about the EFI variables.
1216
1217 @param Attributes Attributes bitmask to specify the type of variables
1218 on which to return information.
1219 @param MaximumVariableStorageSize On output the maximum size of the storage space available for
1220 the EFI variables associated with the attributes specified.
1221 @param RemainingVariableStorageSize Returns the remaining size of the storage space available for EFI
1222 variables associated with the attributes specified.
1223 @param MaximumVariableSize Returns the maximum size of an individual EFI variable
1224 associated with the attributes specified.
1225 @param Global Pointer to VARIABLE_GLOBAL structure.
1226
1227 @retval EFI_SUCCESS Valid answer returned.
1228 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied
1229 @retval EFI_UNSUPPORTED The attribute is not supported on this platform, and the
1230 MaximumVariableStorageSize, RemainingVariableStorageSize,
1231 MaximumVariableSize are undefined.
1232
1233 **/
1234 EFI_STATUS
1235 EFIAPI
1236 EmuQueryVariableInfo (
1237 IN UINT32 Attributes,
1238 OUT UINT64 *MaximumVariableStorageSize,
1239 OUT UINT64 *RemainingVariableStorageSize,
1240 OUT UINT64 *MaximumVariableSize,
1241 IN VARIABLE_GLOBAL *Global
1242 )
1243 {
1244 VARIABLE_HEADER *Variable;
1245 VARIABLE_HEADER *NextVariable;
1246 UINT64 VariableSize;
1247 VARIABLE_STORE_HEADER *VariableStoreHeader;
1248 UINT64 CommonVariableTotalSize;
1249 UINT64 HwErrVariableTotalSize;
1250
1251 CommonVariableTotalSize = 0;
1252 HwErrVariableTotalSize = 0;
1253
1254 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {
1255 return EFI_INVALID_PARAMETER;
1256 }
1257
1258 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {
1259 //
1260 // Make sure the Attributes combination is supported by the platform.
1261 //
1262 return EFI_UNSUPPORTED;
1263 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
1264 //
1265 // Make sure if runtime bit is set, boot service bit is set also.
1266 //
1267 return EFI_INVALID_PARAMETER;
1268 } else if (EfiAtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {
1269 //
1270 // Make sure RT Attribute is set if we are in Runtime phase.
1271 //
1272 return EFI_INVALID_PARAMETER;
1273 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
1274 //
1275 // Make sure Hw Attribute is set with NV.
1276 //
1277 return EFI_INVALID_PARAMETER;
1278 }
1279
1280 AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);
1281
1282 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
1283 //
1284 // Query is Volatile related.
1285 //
1286 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);
1287 } else {
1288 //
1289 // Query is Non-Volatile related.
1290 //
1291 VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) Global->NonVolatileVariableBase);
1292 }
1293
1294 //
1295 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
1296 // with the storage size (excluding the storage header size)
1297 //
1298 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);
1299
1300 //
1301 // Harware error record variable needs larger size.
1302 //
1303 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
1304 *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);
1305 *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);
1306 } else {
1307 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
1308 ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);
1309 *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);
1310 }
1311
1312 //
1313 // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.
1314 //
1315 *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);
1316 }
1317
1318 //
1319 // Point to the starting address of the variables.
1320 //
1321 Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);
1322
1323 //
1324 // Now walk through the related variable store.
1325 //
1326 while (Variable < GetEndPointer (VariableStoreHeader)) {
1327 NextVariable = GetNextVariablePtr(Variable);
1328 if (NextVariable == NULL) {
1329 break;
1330 }
1331 VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;
1332
1333 if ((NextVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
1334 HwErrVariableTotalSize += VariableSize;
1335 } else {
1336 CommonVariableTotalSize += VariableSize;
1337 }
1338
1339 //
1340 // Go to the next one.
1341 //
1342 Variable = NextVariable;
1343 }
1344
1345 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){
1346 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;
1347 } else {
1348 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;
1349 }
1350
1351 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {
1352 *MaximumVariableSize = 0;
1353 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {
1354 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);
1355 }
1356
1357 ReleaseLockOnlyAtBootTime (&Global->VariableServicesLock);
1358 return EFI_SUCCESS;
1359 }
1360
1361 /**
1362 Initializes variable store area.
1363
1364 This function allocates memory space for variable store area and initializes its attributes.
1365
1366 @param VolatileStore Indicates if the variable store is volatile.
1367
1368 **/
1369 EFI_STATUS
1370 InitializeVariableStore (
1371 IN BOOLEAN VolatileStore
1372 )
1373 {
1374 VARIABLE_STORE_HEADER *VariableStore;
1375 BOOLEAN FullyInitializeStore;
1376 EFI_PHYSICAL_ADDRESS *VariableBase;
1377 UINTN *LastVariableOffset;
1378
1379 FullyInitializeStore = TRUE;
1380
1381 if (VolatileStore) {
1382 VariableBase = &mVariableModuleGlobal->VariableGlobal[Physical].VolatileVariableBase;
1383 LastVariableOffset = &mVariableModuleGlobal->VolatileLastVariableOffset;
1384 } else {
1385 VariableBase = &mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase;
1386 LastVariableOffset = &mVariableModuleGlobal->NonVolatileLastVariableOffset;
1387 }
1388
1389 //
1390 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
1391 // is stored with common variable in the same NV region. So the platform integrator should
1392 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
1393 // PcdVariableStoreSize.
1394 //
1395 ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdVariableStoreSize));
1396
1397 //
1398 // Allocate memory for variable store.
1399 //
1400 if (VolatileStore || (PcdGet64 (PcdEmuVariableNvStoreReserved) == 0)) {
1401 VariableStore = (VARIABLE_STORE_HEADER *) AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize));
1402 } else {
1403 //
1404 // A memory location has been reserved for the NV variable store. Certain
1405 // platforms may be able to preserve a memory range across system resets,
1406 // thereby providing better NV variable emulation.
1407 //
1408 VariableStore =
1409 (VARIABLE_STORE_HEADER *)(VOID*)(UINTN)
1410 PcdGet64 (PcdEmuVariableNvStoreReserved);
1411 if (
1412 (VariableStore->Size == PcdGet32 (PcdVariableStoreSize)) &&
1413 (VariableStore->Format == VARIABLE_STORE_FORMATTED) &&
1414 (VariableStore->State == VARIABLE_STORE_HEALTHY)
1415 ) {
1416 DEBUG((
1417 EFI_D_INFO,
1418 "Variable Store reserved at %p appears to be valid\n",
1419 VariableStore
1420 ));
1421 FullyInitializeStore = FALSE;
1422 }
1423 }
1424
1425 if (NULL == VariableStore) {
1426 return EFI_OUT_OF_RESOURCES;
1427 }
1428
1429 if (FullyInitializeStore) {
1430 SetMem (VariableStore, PcdGet32 (PcdVariableStoreSize), 0xff);
1431 }
1432
1433 //
1434 // Variable Specific Data
1435 //
1436 *VariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VariableStore;
1437 InitializeLocationForLastVariableOffset (VariableStore, LastVariableOffset);
1438
1439 CopyGuid (&VariableStore->Signature, &gEfiVariableGuid);
1440 VariableStore->Size = PcdGet32 (PcdVariableStoreSize);
1441 VariableStore->Format = VARIABLE_STORE_FORMATTED;
1442 VariableStore->State = VARIABLE_STORE_HEALTHY;
1443 VariableStore->Reserved = 0;
1444 VariableStore->Reserved1 = 0;
1445
1446 return EFI_SUCCESS;
1447 }
1448
1449 /**
1450 Initializes variable store area for non-volatile and volatile variable.
1451
1452 This function allocates and initializes memory space for global context of ESAL
1453 variable service and variable store area for non-volatile and volatile variable.
1454
1455 @param ImageHandle The Image handle of this driver.
1456 @param SystemTable The pointer of EFI_SYSTEM_TABLE.
1457
1458 @retval EFI_SUCCESS Function successfully executed.
1459 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
1460
1461 **/
1462 EFI_STATUS
1463 EFIAPI
1464 VariableCommonInitialize (
1465 IN EFI_HANDLE ImageHandle,
1466 IN EFI_SYSTEM_TABLE *SystemTable
1467 )
1468 {
1469 EFI_STATUS Status;
1470
1471 //
1472 // Allocate memory for mVariableModuleGlobal
1473 //
1474 mVariableModuleGlobal = (ESAL_VARIABLE_GLOBAL *) AllocateRuntimeZeroPool (
1475 sizeof (ESAL_VARIABLE_GLOBAL)
1476 );
1477 if (NULL == mVariableModuleGlobal) {
1478 return EFI_OUT_OF_RESOURCES;
1479 }
1480
1481 EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal[Physical].VariableServicesLock, TPL_NOTIFY);
1482
1483 //
1484 // Intialize volatile variable store
1485 //
1486 Status = InitializeVariableStore (TRUE);
1487 if (EFI_ERROR (Status)) {
1488 FreePool(mVariableModuleGlobal);
1489 return Status;
1490 }
1491 //
1492 // Intialize non volatile variable store
1493 //
1494 Status = InitializeVariableStore (FALSE);
1495
1496 return Status;
1497 }