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