]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/VariableAuthenticated/EsalVariableDxeSal/Variable.c
ShellPkg: Set CRC value whenever changing a system table.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / EsalVariableDxeSal / Variable.c
1 /** @file
2 The implementation of Extended SAL variable services.
3
4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Variable.h"
16 #include "AuthService.h"
17
18 //
19 // Don't use module globals after the SetVirtualAddress map is signaled
20 //
21 ESAL_VARIABLE_GLOBAL *mVariableModuleGlobal;
22 CHAR16 *mVariableName[NUM_VAR_NAME] = {
23 L"PlatformLangCodes",
24 L"LangCodes",
25 L"PlatformLang",
26 L"Lang",
27 L"HwErrRec",
28 AUTHVAR_KEYDB_NAME,
29 EFI_SETUP_MODE_NAME,
30 EFI_PLATFORM_KEY_NAME,
31 EFI_KEY_EXCHANGE_KEY_NAME
32 };
33
34 GLOBAL_REMOVE_IF_UNREFERENCED VARIABLE_INFO_ENTRY *gVariableInfo = NULL;
35
36 //
37 // The current Hii implementation accesses this variable a larg # of times on every boot.
38 // Other common variables are only accessed a single time. This is why this cache algorithm
39 // only targets a single variable. Probably to get an performance improvement out of
40 // a Cache you would need a cache that improves the search performance for a variable.
41 //
42 VARIABLE_CACHE_ENTRY mVariableCache[] = {
43 {
44 &gEfiGlobalVariableGuid,
45 L"Lang",
46 0x00000000,
47 0x00,
48 NULL
49 },
50 {
51 &gEfiGlobalVariableGuid,
52 L"PlatformLang",
53 0x00000000,
54 0x00,
55 NULL
56 }
57 };
58
59 /**
60 Acquires lock only at boot time. Simply returns at runtime.
61
62 This is a temperary function which will be removed when
63 EfiAcquireLock() in UefiLib can handle the call in UEFI
64 Runtimer driver in RT phase.
65 It calls EfiAcquireLock() at boot time, and simply returns
66 at runtime.
67
68 @param[in] Lock A pointer to the lock to acquire.
69
70 **/
71 VOID
72 AcquireLockOnlyAtBootTime (
73 IN EFI_LOCK *Lock
74 )
75 {
76 if (!EfiAtRuntime ()) {
77 EfiAcquireLock (Lock);
78 }
79 }
80
81 /**
82 Releases lock only at boot time. Simply returns at runtime.
83
84 This is a temperary function which will be removed when
85 EfiReleaseLock() in UefiLib can handle the call in UEFI
86 Runtimer driver in RT phase.
87 It calls EfiReleaseLock() at boot time, and simply returns
88 at runtime
89
90 @param[in] Lock A pointer to the lock to release.
91
92 **/
93 VOID
94 ReleaseLockOnlyAtBootTime (
95 IN EFI_LOCK *Lock
96 )
97 {
98 if (!EfiAtRuntime ()) {
99 EfiReleaseLock (Lock);
100 }
101 }
102
103 /**
104 Reads/Writes variable storage, volatile or non-volatile.
105
106 This function reads or writes volatile or non-volatile variable stroage.
107 For volatile storage, it performs memory copy.
108 For non-volatile storage, it accesses data on firmware storage. Data
109 area to access can span multiple firmware blocks.
110
111 @param[in] Write TRUE - Write variable store.
112 FALSE - Read variable store.
113 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.
114 @param[in] Volatile TRUE - Variable is volatile.
115 FALSE - Variable is non-volatile.
116 @param[in] Instance Instance of FV Block services.
117 @param[in] StartAddress Start address of data to access.
118 @param[in] DataSize Size of data to access.
119 @param[in, out] Buffer For write, pointer to the buffer from which data is written.
120 For read, pointer to the buffer to hold the data read.
121
122 @retval EFI_SUCCESS Variable store successfully accessed.
123 @retval EFI_INVALID_PARAMETER Data area to access exceeds valid variable storage.
124
125 **/
126 EFI_STATUS
127 AccessVariableStore (
128 IN BOOLEAN Write,
129 IN VARIABLE_GLOBAL *Global,
130 IN BOOLEAN Volatile,
131 IN UINTN Instance,
132 IN EFI_PHYSICAL_ADDRESS StartAddress,
133 IN UINT32 DataSize,
134 IN OUT VOID *Buffer
135 )
136 {
137 EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;
138 UINTN BlockIndex;
139 UINTN LinearOffset;
140 UINTN CurrWriteSize;
141 UINTN CurrWritePtr;
142 UINT8 *CurrBuffer;
143 EFI_LBA LbaNumber;
144 UINTN Size;
145 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
146 VARIABLE_STORE_HEADER *VolatileBase;
147 EFI_PHYSICAL_ADDRESS FvVolHdr;
148 EFI_STATUS Status;
149 VARIABLE_STORE_HEADER *VariableStoreHeader;
150
151 FvVolHdr = 0;
152 FwVolHeader = NULL;
153
154 if (Volatile) {
155 //
156 // If data is volatile, simply calculate the data pointer and copy memory.
157 // Data pointer should point to the actual address where data is to be
158 // accessed.
159 //
160 VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) Global->VolatileVariableBase);
161
162 if ((StartAddress + DataSize) > ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {
163 return EFI_INVALID_PARAMETER;
164 }
165
166 //
167 // For volatile variable, a simple memory copy is enough.
168 //
169 if (Write) {
170 CopyMem ((VOID *) StartAddress, Buffer, DataSize);
171 } else {
172 CopyMem (Buffer, (VOID *) StartAddress, DataSize);
173 }
174
175 return EFI_SUCCESS;
176 }
177
178 //
179 // If data is non-volatile, calculate firmware volume header and data pointer.
180 //
181 Status = (EFI_STATUS) EsalCall (
182 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,
183 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,
184 GetPhysicalAddressFunctionId,
185 Instance,
186 (UINT64) &FvVolHdr,
187 0,
188 0,
189 0,
190 0,
191 0
192 ).Status;
193 ASSERT_EFI_ERROR (Status);
194
195 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);
196 ASSERT (FwVolHeader != NULL);
197 VariableStoreHeader = (VARIABLE_STORE_HEADER *)(FwVolHeader + 1);
198
199 if ((StartAddress + DataSize) > ((EFI_PHYSICAL_ADDRESS) (UINTN) ((CHAR8 *)VariableStoreHeader + VariableStoreHeader->Size))) {
200 return EFI_INVALID_PARAMETER;
201 }
202
203 LinearOffset = (UINTN) FwVolHeader;
204 CurrWritePtr = StartAddress;
205 CurrWriteSize = DataSize;
206 CurrBuffer = Buffer;
207 LbaNumber = 0;
208
209 if (CurrWritePtr < LinearOffset) {
210 return EFI_INVALID_PARAMETER;
211 }
212
213 //
214 // Traverse data blocks of this firmware storage to find the one where CurrWritePtr locates
215 //
216 for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {
217 for (BlockIndex = 0; BlockIndex < PtrBlockMapEntry->NumBlocks; BlockIndex++) {
218 if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {
219 //
220 // Check to see if the data area to access spans multiple blocks.
221 //
222 if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {
223 //
224 // If data area to access is contained in one block, just access and return.
225 //
226 if (Write) {
227 Status = (EFI_STATUS) EsalCall (
228 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,
229 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,
230 WriteFunctionId,
231 Instance,
232 LbaNumber,
233 (CurrWritePtr - LinearOffset),
234 (UINT64) &CurrWriteSize,
235 (UINT64) CurrBuffer,
236 0,
237 0
238 ).Status;
239 } else {
240 Status = (EFI_STATUS) EsalCall (
241 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,
242 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,
243 ReadFunctionId,
244 Instance,
245 LbaNumber,
246 (CurrWritePtr - LinearOffset),
247 (UINT64) &CurrWriteSize,
248 (UINT64) CurrBuffer,
249 0,
250 0
251 ).Status;
252 }
253 return Status;
254 } else {
255 //
256 // If data area to access spans multiple blocks, access this one and adjust for the next one.
257 //
258 Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);
259 if (Write) {
260 Status = (EFI_STATUS) EsalCall (
261 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,
262 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,
263 WriteFunctionId,
264 Instance,
265 LbaNumber,
266 (CurrWritePtr - LinearOffset),
267 (UINT64) &Size,
268 (UINT64) CurrBuffer,
269 0,
270 0
271 ).Status;
272 } else {
273 Status = (EFI_STATUS) EsalCall (
274 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,
275 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,
276 ReadFunctionId,
277 Instance,
278 LbaNumber,
279 (CurrWritePtr - LinearOffset),
280 (UINT64) &Size,
281 (UINT64) CurrBuffer,
282 0,
283 0
284 ).Status;
285 }
286 if (EFI_ERROR (Status)) {
287 return Status;
288 }
289 //
290 // Adjust for the remaining data.
291 //
292 CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;
293 CurrBuffer = CurrBuffer + Size;
294 CurrWriteSize = CurrWriteSize - Size;
295 }
296 }
297
298 LinearOffset += PtrBlockMapEntry->Length;
299 LbaNumber++;
300 }
301 }
302
303 return EFI_SUCCESS;
304 }
305
306 /**
307 Retrieves header of volatile or non-volatile variable stroage.
308
309 @param[in] VarStoreAddress Start address of variable storage.
310 @param[in] Volatile TRUE - Variable storage is volatile.
311 FALSE - Variable storage is non-volatile.
312 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.
313 @param[in] Instance Instance of FV Block services.
314 @param[out] VarStoreHeader Pointer to VARIABLE_STORE_HEADER for output.
315
316 **/
317 VOID
318 GetVarStoreHeader (
319 IN EFI_PHYSICAL_ADDRESS VarStoreAddress,
320 IN BOOLEAN Volatile,
321 IN VARIABLE_GLOBAL *Global,
322 IN UINTN Instance,
323 OUT VARIABLE_STORE_HEADER *VarStoreHeader
324 )
325 {
326 EFI_STATUS Status;
327
328 Status = AccessVariableStore (
329 FALSE,
330 Global,
331 Volatile,
332 Instance,
333 VarStoreAddress,
334 sizeof (VARIABLE_STORE_HEADER),
335 VarStoreHeader
336 );
337 ASSERT_EFI_ERROR (Status);
338 }
339
340 /**
341 Checks variable header.
342
343 This function checks if variable header is valid or not.
344
345 @param[in] VariableAddress Start address of variable header.
346 @param[in] Volatile TRUE - Variable is volatile.
347 FALSE - Variable is non-volatile.
348 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.
349 @param[in] Instance Instance of FV Block services.
350 @param[out] VariableHeader Pointer to VARIABLE_HEADER for output.
351
352 @retval TRUE Variable header is valid.
353 @retval FALSE Variable header is not valid.
354
355 **/
356 BOOLEAN
357 IsValidVariableHeader (
358 IN EFI_PHYSICAL_ADDRESS VariableAddress,
359 IN BOOLEAN Volatile,
360 IN VARIABLE_GLOBAL *Global,
361 IN UINTN Instance,
362 OUT VARIABLE_HEADER *VariableHeader OPTIONAL
363 )
364 {
365 EFI_STATUS Status;
366 VARIABLE_HEADER LocalVariableHeader;
367
368 Status = AccessVariableStore (
369 FALSE,
370 Global,
371 Volatile,
372 Instance,
373 VariableAddress,
374 sizeof (VARIABLE_HEADER),
375 &LocalVariableHeader
376 );
377
378 if (EFI_ERROR (Status) || LocalVariableHeader.StartId != VARIABLE_DATA) {
379 return FALSE;
380 }
381
382 if (VariableHeader != NULL) {
383 CopyMem (VariableHeader, &LocalVariableHeader, sizeof (VARIABLE_HEADER));
384 }
385
386 return TRUE;
387 }
388
389 /**
390 Gets status of variable store.
391
392 This function gets the current status of variable store.
393
394 @param[in] VarStoreHeader Pointer to header of variable store.
395
396 @retval EfiRaw Variable store status is raw.
397 @retval EfiValid Variable store status is valid.
398 @retval EfiInvalid Variable store status is invalid.
399
400 **/
401 VARIABLE_STORE_STATUS
402 GetVariableStoreStatus (
403 IN VARIABLE_STORE_HEADER *VarStoreHeader
404 )
405 {
406
407 if (CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariableGuid) &&
408 VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&
409 VarStoreHeader->State == VARIABLE_STORE_HEALTHY
410 ) {
411
412 return EfiValid;
413 } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&
414 ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&
415 ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&
416 ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&
417 VarStoreHeader->Size == 0xffffffff &&
418 VarStoreHeader->Format == 0xff &&
419 VarStoreHeader->State == 0xff
420 ) {
421
422 return EfiRaw;
423 } else {
424 return EfiInvalid;
425 }
426 }
427
428 /**
429 Gets the size of variable name.
430
431 This function gets the size of variable name.
432 The variable is specified by its variable header.
433 If variable header contains raw data, just return 0.
434
435 @param[in] Variable Pointer to the variable header.
436
437 @return Size of variable name in bytes.
438
439 **/
440 UINTN
441 NameSizeOfVariable (
442 IN VARIABLE_HEADER *Variable
443 )
444 {
445 if (Variable->State == (UINT8) (-1) ||
446 Variable->DataSize == (UINT32) -1 ||
447 Variable->NameSize == (UINT32) -1 ||
448 Variable->Attributes == (UINT32) -1) {
449 return 0;
450 }
451 return (UINTN) Variable->NameSize;
452 }
453
454 /**
455 Gets the size of variable data area.
456
457 This function gets the size of variable data area.
458 The variable is specified by its variable header.
459 If variable header contains raw data, just return 0.
460
461 @param[in] Variable Pointer to the variable header.
462
463 @return Size of variable data area in bytes.
464
465 **/
466 UINTN
467 DataSizeOfVariable (
468 IN VARIABLE_HEADER *Variable
469 )
470 {
471 if (Variable->State == (UINT8) -1 ||
472 Variable->DataSize == (UINT32) -1 ||
473 Variable->NameSize == (UINT32) -1 ||
474 Variable->Attributes == (UINT32) -1) {
475 return 0;
476 }
477 return (UINTN) Variable->DataSize;
478 }
479
480 /**
481 Gets the pointer to variable name.
482
483 This function gets the pointer to variable name.
484 The variable is specified by its variable header.
485
486 @param[in] VariableAddress Start address of variable header.
487 @param[in] Volatile TRUE - Variable is volatile.
488 FALSE - Variable is non-volatile.
489 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.
490 @param[in] Instance Instance of FV Block services.
491 @param[out] VariableName Buffer to hold variable name for output.
492
493 **/
494 VOID
495 GetVariableNamePtr (
496 IN EFI_PHYSICAL_ADDRESS VariableAddress,
497 IN BOOLEAN Volatile,
498 IN VARIABLE_GLOBAL *Global,
499 IN UINTN Instance,
500 OUT CHAR16 *VariableName
501 )
502 {
503 EFI_STATUS Status;
504 EFI_PHYSICAL_ADDRESS Address;
505 VARIABLE_HEADER VariableHeader;
506 BOOLEAN IsValid;
507
508 IsValid = IsValidVariableHeader (VariableAddress, Volatile, Global, Instance, &VariableHeader);
509 ASSERT (IsValid);
510
511 //
512 // Name area follows variable header.
513 //
514 Address = VariableAddress + sizeof (VARIABLE_HEADER);
515
516 Status = AccessVariableStore (
517 FALSE,
518 Global,
519 Volatile,
520 Instance,
521 Address,
522 VariableHeader.NameSize,
523 VariableName
524 );
525 ASSERT_EFI_ERROR (Status);
526 }
527
528 /**
529 Gets the pointer to variable data area.
530
531 This function gets the pointer to variable data area.
532 The variable is specified by its variable header.
533
534 @param[in] VariableAddress Start address of variable header.
535 @param[in] Volatile TRUE - Variable is volatile.
536 FALSE - Variable is non-volatile.
537 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.
538 @param[in] Instance Instance of FV Block services.
539 @param[out] VariableData Buffer to hold variable data for output.
540
541 **/
542 VOID
543 GetVariableDataPtr (
544 IN EFI_PHYSICAL_ADDRESS VariableAddress,
545 IN BOOLEAN Volatile,
546 IN VARIABLE_GLOBAL *Global,
547 IN UINTN Instance,
548 OUT CHAR16 *VariableData
549 )
550 {
551 EFI_STATUS Status;
552 EFI_PHYSICAL_ADDRESS Address;
553 VARIABLE_HEADER VariableHeader;
554 BOOLEAN IsValid;
555
556 IsValid = IsValidVariableHeader (VariableAddress, Volatile, Global, Instance, &VariableHeader);
557 ASSERT (IsValid);
558
559 //
560 // Data area follows variable name.
561 // Be careful about pad size for alignment
562 //
563 Address = VariableAddress + sizeof (VARIABLE_HEADER);
564 Address += NameSizeOfVariable (&VariableHeader);
565 Address += GET_PAD_SIZE (NameSizeOfVariable (&VariableHeader));
566
567 Status = AccessVariableStore (
568 FALSE,
569 Global,
570 Volatile,
571 Instance,
572 Address,
573 VariableHeader.DataSize,
574 VariableData
575 );
576 ASSERT_EFI_ERROR (Status);
577 }
578
579
580 /**
581 Gets the pointer to the next variable header.
582
583 This function gets the pointer to the next variable header.
584 The variable is specified by its variable header.
585
586 @param[in] VariableAddress Start address of variable header.
587 @param[in] Volatile TRUE - Variable is volatile.
588 FALSE - Variable is non-volatile.
589 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.
590 @param[in] Instance Instance of FV Block services.
591
592 @return Pointer to the next variable header.
593 NULL if variable header is invalid.
594
595 **/
596 EFI_PHYSICAL_ADDRESS
597 GetNextVariablePtr (
598 IN EFI_PHYSICAL_ADDRESS VariableAddress,
599 IN BOOLEAN Volatile,
600 IN VARIABLE_GLOBAL *Global,
601 IN UINTN Instance
602 )
603 {
604 EFI_PHYSICAL_ADDRESS Address;
605 VARIABLE_HEADER VariableHeader;
606
607 if (!IsValidVariableHeader (VariableAddress, Volatile, Global, Instance, &VariableHeader)) {
608 return 0x0;
609 }
610
611 //
612 // Header of next variable follows data area of this variable
613 //
614 Address = VariableAddress + sizeof (VARIABLE_HEADER);
615 Address += NameSizeOfVariable (&VariableHeader);
616 Address += GET_PAD_SIZE (NameSizeOfVariable (&VariableHeader));
617 Address += DataSizeOfVariable (&VariableHeader);
618 Address += GET_PAD_SIZE (DataSizeOfVariable (&VariableHeader));
619
620 //
621 // Be careful about pad size for alignment
622 //
623 return HEADER_ALIGN (Address);
624 }
625
626 /**
627 Gets the pointer to the first variable header in given variable store area.
628
629 This function gets the pointer to the first variable header in given variable
630 store area. The variable store area is given by its start address.
631
632 @param[in] VarStoreHeaderAddress Pointer to the header of variable store area.
633
634 @return Pointer to the first variable header.
635
636 **/
637 EFI_PHYSICAL_ADDRESS
638 GetStartPointer (
639 IN EFI_PHYSICAL_ADDRESS VarStoreHeaderAddress
640 )
641 {
642 return HEADER_ALIGN (VarStoreHeaderAddress + sizeof (VARIABLE_STORE_HEADER));
643 }
644
645 /**
646 Gets the pointer to the end of given variable store area.
647
648 This function gets the pointer to the end of given variable store area.
649 The variable store area is given by its start address.
650
651 @param[in] VarStoreHeaderAddress Pointer to the header of variable store area.
652 @param[in] Volatile TRUE - Variable is volatile.
653 FALSE - Variable is non-volatile.
654 @param[in] Global Pointer to VARAIBLE_GLOBAL structure.
655 @param[in] Instance Instance of FV Block services.
656
657 @return Pointer to the end of given variable store area.
658
659 **/
660 EFI_PHYSICAL_ADDRESS
661 GetEndPointer (
662 IN EFI_PHYSICAL_ADDRESS VarStoreHeaderAddress,
663 IN BOOLEAN Volatile,
664 IN VARIABLE_GLOBAL *Global,
665 IN UINTN Instance
666 )
667 {
668 EFI_STATUS Status;
669 VARIABLE_STORE_HEADER VariableStoreHeader;
670
671 Status = AccessVariableStore (
672 FALSE,
673 Global,
674 Volatile,
675 Instance,
676 VarStoreHeaderAddress,
677 sizeof (VARIABLE_STORE_HEADER),
678 &VariableStoreHeader
679 );
680
681 ASSERT_EFI_ERROR (Status);
682 return HEADER_ALIGN (VarStoreHeaderAddress + VariableStoreHeader.Size);
683 }
684
685 /**
686 Updates variable info entry in EFI system table for statistical information.
687
688 Routine used to track statistical information about variable usage.
689 The data is stored in the EFI system table so it can be accessed later.
690 VariableInfo.efi can dump out the table. Only Boot Services variable
691 accesses are tracked by this code. The PcdVariableCollectStatistics
692 build flag controls if this feature is enabled.
693 A read that hits in the cache will have Read and Cache true for
694 the transaction. Data is allocated by this routine, but never
695 freed.
696
697 @param[in] VariableName Name of the Variable to track.
698 @param[in] VendorGuid Guid of the Variable to track.
699 @param[in] Volatile TRUE if volatile FALSE if non-volatile.
700 @param[in] Read TRUE if GetVariable() was called.
701 @param[in] Write TRUE if SetVariable() was called.
702 @param[in] Delete TRUE if deleted via SetVariable().
703 @param[in] Cache TRUE for a cache hit.
704
705 **/
706 VOID
707 UpdateVariableInfo (
708 IN CHAR16 *VariableName,
709 IN EFI_GUID *VendorGuid,
710 IN BOOLEAN Volatile,
711 IN BOOLEAN Read,
712 IN BOOLEAN Write,
713 IN BOOLEAN Delete,
714 IN BOOLEAN Cache
715 )
716 {
717 VARIABLE_INFO_ENTRY *Entry;
718
719 if (FeaturePcdGet (PcdVariableCollectStatistics)) {
720
721 if (EfiAtRuntime ()) {
722 //
723 // Don't collect statistics at runtime
724 //
725 return;
726 }
727
728 if (gVariableInfo == NULL) {
729 //
730 // on the first call allocate a entry and place a pointer to it in
731 // the EFI System Table
732 //
733 gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
734 ASSERT (gVariableInfo != NULL);
735
736 CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);
737 gVariableInfo->Name = AllocatePool (StrSize (VariableName));
738 ASSERT (gVariableInfo->Name != NULL);
739 StrCpy (gVariableInfo->Name, VariableName);
740 gVariableInfo->Volatile = Volatile;
741
742 gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, gVariableInfo);
743 }
744
745
746 for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {
747 if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {
748 if (StrCmp (VariableName, Entry->Name) == 0) {
749 //
750 // Find the entry matching both variable name and vender GUID,
751 // and update counters for all types.
752 //
753 if (Read) {
754 Entry->ReadCount++;
755 }
756 if (Write) {
757 Entry->WriteCount++;
758 }
759 if (Delete) {
760 Entry->DeleteCount++;
761 }
762 if (Cache) {
763 Entry->CacheCount++;
764 }
765
766 return;
767 }
768 }
769
770 if (Entry->Next == NULL) {
771 //
772 // If the entry is not in the table add it.
773 // Next iteration of the loop will fill in the data
774 //
775 Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));
776 ASSERT (Entry->Next != NULL);
777
778 CopyGuid (&Entry->Next->VendorGuid, VendorGuid);
779 Entry->Next->Name = AllocatePool (StrSize (VariableName));
780 ASSERT (Entry->Next->Name != NULL);
781 StrCpy (Entry->Next->Name, VariableName);
782 Entry->Next->Volatile = Volatile;
783 }
784
785 }
786 }
787 }
788
789 /**
790 Updates variable in cache.
791
792 This function searches the variable cache. If the variable to set exists in the cache,
793 it updates the variable in cache. It has the same parameters with UEFI SetVariable()
794 service.
795
796 @param[in] VariableName A Null-terminated Unicode string that is the name of the vendor's
797 variable. Each VariableName is unique for each VendorGuid.
798 @param[in] VendorGuid A unique identifier for the vendor.
799 @param[in] Attributes Attributes bitmask to set for the variable.
800 @param[in] DataSize The size in bytes of the Data buffer. A size of zero causes the
801 variable to be deleted.
802 @param[in] Data The contents for the variable.
803
804 **/
805 VOID
806 UpdateVariableCache (
807 IN CHAR16 *VariableName,
808 IN EFI_GUID *VendorGuid,
809 IN UINT32 Attributes,
810 IN UINTN DataSize,
811 IN VOID *Data
812 )
813 {
814 VARIABLE_CACHE_ENTRY *Entry;
815 UINTN Index;
816
817 if (EfiAtRuntime ()) {
818 //
819 // Don't use the cache at runtime
820 //
821 return;
822 }
823
824 //
825 // Searches cache for the variable to update. If it exists, update it.
826 //
827 for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {
828 if (CompareGuid (VendorGuid, Entry->Guid)) {
829 if (StrCmp (VariableName, Entry->Name) == 0) {
830 Entry->Attributes = Attributes;
831 if (DataSize == 0) {
832 //
833 // If DataSize is 0, delete the variable.
834 //
835 if (Entry->DataSize != 0) {
836 FreePool (Entry->Data);
837 }
838 Entry->DataSize = DataSize;
839 } else if (DataSize == Entry->DataSize) {
840 //
841 // If size of data does not change, simply copy data
842 //
843 CopyMem (Entry->Data, Data, DataSize);
844 } else {
845 //
846 // If size of data changes, allocate pool and copy data.
847 //
848 Entry->Data = AllocatePool (DataSize);
849 Entry->DataSize = DataSize;
850 CopyMem (Entry->Data, Data, DataSize);
851 }
852 }
853 }
854 }
855 }
856
857
858 /**
859 Search the cache to check if the variable is in it.
860
861 This function searches the variable cache. If the variable to find exists, return its data
862 and attributes.
863
864 @param[in] VariableName A Null-terminated Unicode string that is the name of the vendor's
865 variable. Each VariableName is unique for each VendorGuid.
866 @param[in] VendorGuid A unique identifier for the vendor
867 @param[out] Attributes Pointer to the attributes bitmask of the variable for output.
868 @param[in, out] DataSize On input, size of the buffer of Data.
869 On output, size of the variable's data.
870 @param[out] Data Pointer to the data buffer for output.
871
872 @retval EFI_SUCCESS VariableGuid & VariableName data was returned.
873 @retval EFI_NOT_FOUND No matching variable found in cache.
874 @retval EFI_BUFFER_TOO_SMALL *DataSize is smaller than size of the variable's data to return.
875
876 **/
877 EFI_STATUS
878 FindVariableInCache (
879 IN CHAR16 *VariableName,
880 IN EFI_GUID *VendorGuid,
881 OUT UINT32 *Attributes OPTIONAL,
882 IN OUT UINTN *DataSize,
883 OUT VOID *Data
884 )
885 {
886 VARIABLE_CACHE_ENTRY *Entry;
887 UINTN Index;
888
889 if (EfiAtRuntime ()) {
890 //
891 // Don't use the cache at runtime
892 //
893 return EFI_NOT_FOUND;
894 }
895
896 //
897 // Searches cache for the variable
898 //
899 for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {
900 if (CompareGuid (VendorGuid, Entry->Guid)) {
901 if (StrCmp (VariableName, Entry->Name) == 0) {
902 if (Entry->DataSize == 0) {
903 //
904 // Variable has been deleted so return EFI_NOT_FOUND
905 //
906 return EFI_NOT_FOUND;
907 } else if (Entry->DataSize > *DataSize) {
908 //
909 // If buffer is too small, return the size needed and EFI_BUFFER_TOO_SMALL
910 //
911 *DataSize = Entry->DataSize;
912 return EFI_BUFFER_TOO_SMALL;
913 } else {
914 //
915 // If buffer is large enough, return the data
916 //
917 *DataSize = Entry->DataSize;
918 CopyMem (Data, Entry->Data, Entry->DataSize);
919 //
920 // If Attributes is not NULL, return the variable's attribute.
921 //
922 if (Attributes != NULL) {
923 *Attributes = Entry->Attributes;
924 }
925 return EFI_SUCCESS;
926 }
927 }
928 }
929 }
930
931 return EFI_NOT_FOUND;
932 }
933
934 /**
935 Finds variable in volatile and non-volatile storage areas.
936
937 This code finds variable in volatile and non-volatile storage areas.
938 If VariableName is an empty string, then we just return the first
939 qualified variable without comparing VariableName and VendorGuid.
940 Otherwise, VariableName and VendorGuid are compared.
941
942 @param[in] VariableName Name of the variable to be found.
943 @param[in] VendorGuid Vendor GUID to be found.
944 @param[out] PtrTrack VARIABLE_POINTER_TRACK structure for output,
945 including the range searched and the target position.
946 @param[in] Global Pointer to VARIABLE_GLOBAL structure, including
947 base of volatile variable storage area, base of
948 NV variable storage area, and a lock.
949 @param[in] Instance Instance of FV Block services.
950
951 @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while
952 VendorGuid is NULL.
953 @retval EFI_SUCCESS Variable successfully found.
954 @retval EFI_INVALID_PARAMETER Variable not found.
955
956 **/
957 EFI_STATUS
958 FindVariable (
959 IN CHAR16 *VariableName,
960 IN EFI_GUID *VendorGuid,
961 OUT VARIABLE_POINTER_TRACK *PtrTrack,
962 IN VARIABLE_GLOBAL *Global,
963 IN UINTN Instance
964 )
965 {
966 EFI_PHYSICAL_ADDRESS Variable[2];
967 EFI_PHYSICAL_ADDRESS InDeletedVariable;
968 EFI_PHYSICAL_ADDRESS VariableStoreHeader[2];
969 UINTN InDeletedStorageIndex;
970 UINTN Index;
971 CHAR16 LocalVariableName[MAX_NAME_SIZE];
972 BOOLEAN Volatile;
973 VARIABLE_HEADER VariableHeader;
974
975 //
976 // 0: Volatile, 1: Non-Volatile
977 // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName
978 // make use of this mapping to implement search algorithme.
979 //
980 VariableStoreHeader[0] = Global->VolatileVariableBase;
981 VariableStoreHeader[1] = Global->NonVolatileVariableBase;
982
983 //
984 // Start Pointers for the variable.
985 // Actual Data Pointer where data can be written.
986 //
987 Variable[0] = GetStartPointer (VariableStoreHeader[0]);
988 Variable[1] = GetStartPointer (VariableStoreHeader[1]);
989
990 if (VariableName[0] != 0 && VendorGuid == NULL) {
991 return EFI_INVALID_PARAMETER;
992 }
993
994 //
995 // Find the variable by walk through volatile and then non-volatile variable store
996 //
997 InDeletedVariable = 0x0;
998 InDeletedStorageIndex = 0;
999 Volatile = TRUE;
1000 for (Index = 0; Index < 2; Index++) {
1001 if (Index == 1) {
1002 Volatile = FALSE;
1003 }
1004 while (IsValidVariableHeader (Variable[Index], Volatile, Global, Instance, &VariableHeader)) {
1005 if (VariableHeader.State == VAR_ADDED ||
1006 VariableHeader.State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)
1007 ) {
1008 if (!EfiAtRuntime () || ((VariableHeader.Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {
1009 if (VariableName[0] == 0) {
1010 //
1011 // If VariableName is an empty string, then we just find the first qualified variable
1012 // without comparing VariableName and VendorGuid
1013 //
1014 if (VariableHeader.State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
1015 //
1016 // If variable is in delete transition, record it.
1017 //
1018 InDeletedVariable = Variable[Index];
1019 InDeletedStorageIndex = Index;
1020 } else {
1021 //
1022 // If variable is not in delete transition, return it.
1023 //
1024 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Index]);
1025 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index], Volatile, Global, Instance);
1026 PtrTrack->CurrPtr = Variable[Index];
1027 PtrTrack->Volatile = Volatile;
1028
1029 return EFI_SUCCESS;
1030 }
1031 } else {
1032 //
1033 // If VariableName is not an empty string, then VariableName and VendorGuid are compared.
1034 //
1035 if (CompareGuid (VendorGuid, &VariableHeader.VendorGuid)) {
1036 GetVariableNamePtr (
1037 Variable[Index],
1038 Volatile,
1039 Global,
1040 Instance,
1041 LocalVariableName
1042 );
1043
1044 ASSERT (NameSizeOfVariable (&VariableHeader) != 0);
1045 if (CompareMem (VariableName, LocalVariableName, NameSizeOfVariable (&VariableHeader)) == 0) {
1046 if (VariableHeader.State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
1047 //
1048 // If variable is in delete transition, record it.
1049 // We will use if only no VAR_ADDED variable is found.
1050 //
1051 InDeletedVariable = Variable[Index];
1052 InDeletedStorageIndex = Index;
1053 } else {
1054 //
1055 // If variable is not in delete transition, return it.
1056 //
1057 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Index]);
1058 PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index], Volatile, Global, Instance);
1059 PtrTrack->CurrPtr = Variable[Index];
1060 PtrTrack->Volatile = Volatile;
1061
1062 return EFI_SUCCESS;
1063 }
1064 }
1065 }
1066 }
1067 }
1068 }
1069
1070 Variable[Index] = GetNextVariablePtr (
1071 Variable[Index],
1072 Volatile,
1073 Global,
1074 Instance
1075 );
1076 }
1077 if (InDeletedVariable != 0x0) {
1078 //
1079 // If no VAR_ADDED variable is found, and only variable in delete transition, then use this one.
1080 //
1081 PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[InDeletedStorageIndex]);
1082 PtrTrack->EndPtr = GetEndPointer (
1083 VariableStoreHeader[InDeletedStorageIndex],
1084 (BOOLEAN)(InDeletedStorageIndex == 0),
1085 Global,
1086 Instance
1087 );
1088 PtrTrack->CurrPtr = InDeletedVariable;
1089 PtrTrack->Volatile = (BOOLEAN)(InDeletedStorageIndex == 0);
1090 return EFI_SUCCESS;
1091 }
1092 }
1093 PtrTrack->CurrPtr = 0x0;
1094 return EFI_NOT_FOUND;
1095 }
1096
1097 /**
1098 Variable store garbage collection and reclaim operation.
1099
1100 @param[in] VariableBase Base address of variable store area.
1101 @param[out] LastVariableOffset Offset of last variable.
1102 @param[in] IsVolatile The variable store is volatile or not,
1103 if it is non-volatile, need FTW.
1104 @param[in] VirtualMode Current calling mode for this function.
1105 @param[in] Global Context of this Extended SAL Variable Services Class call.
1106 @param[in] UpdatingVariable Pointer to header of the variable that is being updated.
1107
1108 @retval EFI_SUCCESS Variable store successfully reclaimed.
1109 @retval EFI_OUT_OF_RESOURCES Fail to allocate memory buffer to hold all valid variables.
1110
1111 **/
1112 EFI_STATUS
1113 Reclaim (
1114 IN EFI_PHYSICAL_ADDRESS VariableBase,
1115 OUT UINTN *LastVariableOffset,
1116 IN BOOLEAN IsVolatile,
1117 IN BOOLEAN VirtualMode,
1118 IN ESAL_VARIABLE_GLOBAL *Global,
1119 IN EFI_PHYSICAL_ADDRESS UpdatingVariable
1120 )
1121 {
1122 EFI_PHYSICAL_ADDRESS Variable;
1123 EFI_PHYSICAL_ADDRESS AddedVariable;
1124 EFI_PHYSICAL_ADDRESS NextVariable;
1125 EFI_PHYSICAL_ADDRESS NextAddedVariable;
1126 VARIABLE_STORE_HEADER VariableStoreHeader;
1127 VARIABLE_HEADER VariableHeader;
1128 VARIABLE_HEADER AddedVariableHeader;
1129 CHAR16 VariableName[MAX_NAME_SIZE];
1130 CHAR16 AddedVariableName[MAX_NAME_SIZE];
1131 UINT8 *ValidBuffer;
1132 UINTN MaximumBufferSize;
1133 UINTN VariableSize;
1134 UINTN NameSize;
1135 UINT8 *CurrPtr;
1136 BOOLEAN FoundAdded;
1137 EFI_STATUS Status;
1138 VARIABLE_GLOBAL *VariableGlobal;
1139 UINT32 Instance;
1140
1141 VariableGlobal = &Global->VariableGlobal[VirtualMode];
1142 Instance = Global->FvbInstance;
1143
1144 GetVarStoreHeader (VariableBase, IsVolatile, VariableGlobal, Instance, &VariableStoreHeader);
1145 //
1146 // recaluate the total size of Common/HwErr type variables in non-volatile area.
1147 //
1148 if (!IsVolatile) {
1149 Global->CommonVariableTotalSize = 0;
1150 Global->HwErrVariableTotalSize = 0;
1151 }
1152
1153 //
1154 // Calculate the size of buffer needed to gather all valid variables
1155 //
1156 Variable = GetStartPointer (VariableBase);
1157 MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);
1158
1159 while (IsValidVariableHeader (Variable, IsVolatile, VariableGlobal, Instance, &VariableHeader)) {
1160 NextVariable = GetNextVariablePtr (Variable, IsVolatile, VariableGlobal, Instance);
1161 //
1162 // Collect VAR_ADDED variables, and variables in delete transition status.
1163 //
1164 if (VariableHeader.State == VAR_ADDED ||
1165 VariableHeader.State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)
1166 ) {
1167 VariableSize = NextVariable - Variable;
1168 MaximumBufferSize += VariableSize;
1169 }
1170
1171 Variable = NextVariable;
1172 }
1173
1174 //
1175 // Reserve the 1 Bytes with Oxff to identify the
1176 // end of the variable buffer.
1177 //
1178 MaximumBufferSize += 1;
1179 ValidBuffer = AllocatePool (MaximumBufferSize);
1180 if (ValidBuffer == NULL) {
1181 return EFI_OUT_OF_RESOURCES;
1182 }
1183
1184 SetMem (ValidBuffer, MaximumBufferSize, 0xff);
1185
1186 //
1187 // Copy variable store header
1188 //
1189 CopyMem (ValidBuffer, &VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));
1190 CurrPtr = (UINT8 *) GetStartPointer ((EFI_PHYSICAL_ADDRESS) ValidBuffer);
1191
1192 //
1193 // Reinstall all ADDED variables
1194 //
1195 Variable = GetStartPointer (VariableBase);
1196 while (IsValidVariableHeader (Variable, IsVolatile, VariableGlobal, Instance, &VariableHeader)) {
1197 NextVariable = GetNextVariablePtr (Variable, IsVolatile, VariableGlobal, Instance);
1198 if (VariableHeader.State == VAR_ADDED) {
1199 VariableSize = NextVariable - Variable;
1200 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
1201 CurrPtr += VariableSize;
1202 if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
1203 Global->HwErrVariableTotalSize += VariableSize;
1204 } else if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
1205 Global->CommonVariableTotalSize += VariableSize;
1206 }
1207 }
1208 Variable = NextVariable;
1209 }
1210 //
1211 // Reinstall in delete transition variables
1212 //
1213 Variable = GetStartPointer (VariableBase);
1214 while (IsValidVariableHeader (Variable, IsVolatile, VariableGlobal, Instance, &VariableHeader)) {
1215 NextVariable = GetNextVariablePtr (Variable, IsVolatile, VariableGlobal, Instance);
1216 if (VariableHeader.State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {
1217
1218 //
1219 // Buffer has cached all ADDED variable.
1220 // Per IN_DELETED variable, we have to guarantee that
1221 // no ADDED one in previous buffer.
1222 //
1223 FoundAdded = FALSE;
1224 AddedVariable = GetStartPointer ((EFI_PHYSICAL_ADDRESS) ValidBuffer);
1225 while (IsValidVariableHeader (AddedVariable, IsVolatile, VariableGlobal, Instance, &AddedVariableHeader)) {
1226 NextAddedVariable = GetNextVariablePtr (AddedVariable, IsVolatile, VariableGlobal, Instance);
1227 NameSize = NameSizeOfVariable (&AddedVariableHeader);
1228 if (CompareGuid (&AddedVariableHeader.VendorGuid, &VariableHeader.VendorGuid) &&
1229 NameSize == NameSizeOfVariable (&VariableHeader)
1230 ) {
1231 GetVariableNamePtr (Variable, IsVolatile, VariableGlobal, Instance, VariableName);
1232 GetVariableNamePtr (AddedVariable, IsVolatile, VariableGlobal, Instance, AddedVariableName);
1233 if (CompareMem (VariableName, AddedVariableName, NameSize) == 0) {
1234 //
1235 // If ADDED variable with the same name and vender GUID has been reinstalled,
1236 // then discard this IN_DELETED copy.
1237 //
1238 FoundAdded = TRUE;
1239 break;
1240 }
1241 }
1242 AddedVariable = NextAddedVariable;
1243 }
1244 //
1245 // Add IN_DELETE variables that have not been added to buffer
1246 //
1247 if (!FoundAdded) {
1248 VariableSize = NextVariable - Variable;
1249 CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);
1250 if (Variable != UpdatingVariable) {
1251 //
1252 // Make this IN_DELETE instance valid if:
1253 // 1. No valid instance of this variable exists.
1254 // 2. It is not the variable that is going to be updated.
1255 //
1256 ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;
1257 }
1258 CurrPtr += VariableSize;
1259 if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
1260 Global->HwErrVariableTotalSize += VariableSize;
1261 } else if ((!IsVolatile) && ((((VARIABLE_HEADER*)Variable)->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
1262 Global->CommonVariableTotalSize += VariableSize;
1263 }
1264 }
1265 }
1266 Variable = NextVariable;
1267 }
1268
1269 if (IsVolatile) {
1270 //
1271 // If volatile variable store, just copy valid buffer
1272 //
1273 SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader.Size, 0xff);
1274 CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - (UINT8 *) ValidBuffer));
1275 Status = EFI_SUCCESS;
1276 } else {
1277 //
1278 // If non-volatile variable store, perform FTW here.
1279 // Write ValidBuffer to destination specified by VariableBase.
1280 //
1281 Status = FtwVariableSpace (
1282 VariableBase,
1283 ValidBuffer,
1284 (UINTN) (CurrPtr - (UINT8 *) ValidBuffer)
1285 );
1286 }
1287 if (!EFI_ERROR (Status)) {
1288 *LastVariableOffset = (UINTN) (CurrPtr - (UINT8 *) ValidBuffer);
1289 } else {
1290 *LastVariableOffset = 0;
1291 }
1292
1293 FreePool (ValidBuffer);
1294
1295 return Status;
1296 }
1297
1298 /**
1299 Get index from supported language codes according to language string.
1300
1301 This code is used to get corresponding index in supported language codes. It can handle
1302 RFC4646 and ISO639 language tags.
1303 In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.
1304 In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.
1305
1306 For example:
1307 SupportedLang = "engfraengfra"
1308 Lang = "eng"
1309 Iso639Language = TRUE
1310 The return value is "0".
1311 Another example:
1312 SupportedLang = "en;fr;en-US;fr-FR"
1313 Lang = "fr-FR"
1314 Iso639Language = FALSE
1315 The return value is "3".
1316
1317 @param[in] SupportedLang Platform supported language codes.
1318 @param[in] Lang Configured language.
1319 @param[in] Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1320
1321 @return The index of language in the language codes.
1322
1323 **/
1324 UINTN
1325 GetIndexFromSupportedLangCodes(
1326 IN CHAR8 *SupportedLang,
1327 IN CHAR8 *Lang,
1328 IN BOOLEAN Iso639Language
1329 )
1330 {
1331 UINTN Index;
1332 UINTN CompareLength;
1333 UINTN LanguageLength;
1334
1335 if (Iso639Language) {
1336 CompareLength = ISO_639_2_ENTRY_SIZE;
1337 for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {
1338 if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {
1339 //
1340 // Successfully find the index of Lang string in SupportedLang string.
1341 //
1342 Index = Index / CompareLength;
1343 return Index;
1344 }
1345 }
1346 ASSERT (FALSE);
1347 return 0;
1348 } else {
1349 //
1350 // Compare RFC4646 language code
1351 //
1352 Index = 0;
1353 for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);
1354
1355 for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {
1356 //
1357 // Skip ';' characters in SupportedLang
1358 //
1359 for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);
1360 //
1361 // Determine the length of the next language code in SupportedLang
1362 //
1363 for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);
1364
1365 if ((CompareLength == LanguageLength) &&
1366 (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {
1367 //
1368 // Successfully find the index of Lang string in SupportedLang string.
1369 //
1370 return Index;
1371 }
1372 }
1373 ASSERT (FALSE);
1374 return 0;
1375 }
1376 }
1377
1378 /**
1379 Get language string from supported language codes according to index.
1380
1381 This code is used to get corresponding language string in supported language codes. It can handle
1382 RFC4646 and ISO639 language tags.
1383 In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.
1384 In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.
1385
1386 For example:
1387 SupportedLang = "engfraengfra"
1388 Index = "1"
1389 Iso639Language = TRUE
1390 The return value is "fra".
1391 Another example:
1392 SupportedLang = "en;fr;en-US;fr-FR"
1393 Index = "1"
1394 Iso639Language = FALSE
1395 The return value is "fr".
1396
1397 @param[in] SupportedLang Platform supported language codes.
1398 @param[in] Index the index in supported language codes.
1399 @param[in] Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.
1400 @param[in] VirtualMode Current calling mode for this function.
1401 @param[in] Global Context of this Extended SAL Variable Services Class call.
1402
1403 @return The language string in the language codes.
1404
1405 **/
1406 CHAR8 *
1407 GetLangFromSupportedLangCodes (
1408 IN CHAR8 *SupportedLang,
1409 IN UINTN Index,
1410 IN BOOLEAN Iso639Language,
1411 IN BOOLEAN VirtualMode,
1412 IN ESAL_VARIABLE_GLOBAL *Global
1413 )
1414 {
1415 UINTN SubIndex;
1416 UINTN CompareLength;
1417 CHAR8 *Supported;
1418
1419 SubIndex = 0;
1420 Supported = SupportedLang;
1421 if (Iso639Language) {
1422 //
1423 // according to the index of Lang string in SupportedLang string to get the language.
1424 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1425 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1426 //
1427 CompareLength = ISO_639_2_ENTRY_SIZE;
1428 Global->Lang[CompareLength] = '\0';
1429 return CopyMem (Global->Lang, SupportedLang + Index * CompareLength, CompareLength);
1430
1431 } else {
1432 while (TRUE) {
1433 //
1434 // take semicolon as delimitation, sequentially traverse supported language codes.
1435 //
1436 for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {
1437 Supported++;
1438 }
1439 if ((*Supported == '\0') && (SubIndex != Index)) {
1440 //
1441 // Have completed the traverse, but not find corrsponding string.
1442 // This case is not allowed to happen.
1443 //
1444 ASSERT(FALSE);
1445 return NULL;
1446 }
1447 if (SubIndex == Index) {
1448 //
1449 // according to the index of Lang string in SupportedLang string to get the language.
1450 // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.
1451 // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.
1452 //
1453 Global->PlatformLang[VirtualMode][CompareLength] = '\0';
1454 return CopyMem (Global->PlatformLang[VirtualMode], Supported - CompareLength, CompareLength);
1455 }
1456 SubIndex++;
1457
1458 //
1459 // Skip ';' characters in Supported
1460 //
1461 for (; *Supported != '\0' && *Supported == ';'; Supported++);
1462 }
1463 }
1464 }
1465
1466 /**
1467 Returns a pointer to an allocated buffer that contains the best matching language
1468 from a set of supported languages.
1469
1470 This function supports both ISO 639-2 and RFC 4646 language codes, but language
1471 code types may not be mixed in a single call to this function. This function
1472 supports a variable argument list that allows the caller to pass in a prioritized
1473 list of language codes to test against all the language codes in SupportedLanguages.
1474
1475 If SupportedLanguages is NULL, then ASSERT().
1476
1477 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that
1478 contains a set of language codes in the format
1479 specified by Iso639Language.
1480 @param[in] Iso639Language If TRUE, then all language codes are assumed to be
1481 in ISO 639-2 format. If FALSE, then all language
1482 codes are assumed to be in RFC 4646 language format.
1483 @param[in] VirtualMode Current calling mode for this function.
1484 @param[in] ... A variable argument list that contains pointers to
1485 Null-terminated ASCII strings that contain one or more
1486 language codes in the format specified by Iso639Language.
1487 The first language code from each of these language
1488 code lists is used to determine if it is an exact or
1489 close match to any of the language codes in
1490 SupportedLanguages. Close matches only apply to RFC 4646
1491 language codes, and the matching algorithm from RFC 4647
1492 is used to determine if a close match is present. If
1493 an exact or close match is found, then the matching
1494 language code from SupportedLanguages is returned. If
1495 no matches are found, then the next variable argument
1496 parameter is evaluated. The variable argument list
1497 is terminated by a NULL.
1498
1499 @retval NULL The best matching language could not be found in SupportedLanguages.
1500 @retval NULL There are not enough resources available to return the best matching
1501 language.
1502 @retval Other A pointer to a Null-terminated ASCII string that is the best matching
1503 language in SupportedLanguages.
1504
1505 **/
1506 CHAR8 *
1507 VariableGetBestLanguage (
1508 IN CONST CHAR8 *SupportedLanguages,
1509 IN BOOLEAN Iso639Language,
1510 IN BOOLEAN VirtualMode,
1511 ...
1512 )
1513 {
1514 VA_LIST Args;
1515 CHAR8 *Language;
1516 UINTN CompareLength;
1517 UINTN LanguageLength;
1518 CONST CHAR8 *Supported;
1519 CHAR8 *Buffer;
1520
1521 ASSERT (SupportedLanguages != NULL);
1522
1523 VA_START (Args, VirtualMode);
1524 while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {
1525 //
1526 // Default to ISO 639-2 mode
1527 //
1528 CompareLength = 3;
1529 LanguageLength = MIN (3, AsciiStrLen (Language));
1530
1531 //
1532 // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language
1533 //
1534 if (!Iso639Language) {
1535 for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);
1536 }
1537
1538 //
1539 // Trim back the length of Language used until it is empty
1540 //
1541 while (LanguageLength > 0) {
1542 //
1543 // Loop through all language codes in SupportedLanguages
1544 //
1545 for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {
1546 //
1547 // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages
1548 //
1549 if (!Iso639Language) {
1550 //
1551 // Skip ';' characters in Supported
1552 //
1553 for (; *Supported != '\0' && *Supported == ';'; Supported++);
1554 //
1555 // Determine the length of the next language code in Supported
1556 //
1557 for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);
1558 //
1559 // If Language is longer than the Supported, then skip to the next language
1560 //
1561 if (LanguageLength > CompareLength) {
1562 continue;
1563 }
1564 }
1565 //
1566 // See if the first LanguageLength characters in Supported match Language
1567 //
1568 if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {
1569 VA_END (Args);
1570
1571 Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang[VirtualMode];
1572 Buffer[CompareLength] = '\0';
1573 return CopyMem (Buffer, Supported, CompareLength);
1574 }
1575 }
1576
1577 if (Iso639Language) {
1578 //
1579 // If ISO 639 mode, then each language can only be tested once
1580 //
1581 LanguageLength = 0;
1582 } else {
1583 //
1584 // If RFC 4646 mode, then trim Language from the right to the next '-' character
1585 //
1586 for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);
1587 }
1588 }
1589 }
1590 VA_END (Args);
1591
1592 //
1593 // No matches were found
1594 //
1595 return NULL;
1596 }
1597
1598 /**
1599 Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.
1600
1601 When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.
1602 According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,
1603 and are read-only. Therefore, in variable driver, only store the original value for other use.
1604
1605 @param[in] VariableName Name of variable.
1606 @param[in] Data Variable data.
1607 @param[in] DataSize Size of data. 0 means delete.
1608 @param[in] VirtualMode Current calling mode for this function.
1609 @param[in] Global Context of this Extended SAL Variable Services Class call.
1610
1611 **/
1612 VOID
1613 AutoUpdateLangVariable(
1614 IN CHAR16 *VariableName,
1615 IN VOID *Data,
1616 IN UINTN DataSize,
1617 IN BOOLEAN VirtualMode,
1618 IN ESAL_VARIABLE_GLOBAL *Global
1619 )
1620 {
1621 EFI_STATUS Status;
1622 CHAR8 *BestPlatformLang;
1623 CHAR8 *BestLang;
1624 UINTN Index;
1625 UINT32 Attributes;
1626 VARIABLE_POINTER_TRACK Variable;
1627 BOOLEAN SetLanguageCodes;
1628 CHAR16 **PredefinedVariableName;
1629 VARIABLE_GLOBAL *VariableGlobal;
1630 UINT32 Instance;
1631
1632 //
1633 // Don't do updates for delete operation
1634 //
1635 if (DataSize == 0) {
1636 return;
1637 }
1638
1639 SetLanguageCodes = FALSE;
1640 VariableGlobal = &Global->VariableGlobal[VirtualMode];
1641 Instance = Global->FvbInstance;
1642
1643
1644 PredefinedVariableName = &Global->VariableName[VirtualMode][0];
1645 if (StrCmp (VariableName, PredefinedVariableName[VAR_PLATFORM_LANG_CODES]) == 0) {
1646 //
1647 // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.
1648 //
1649 if (EfiAtRuntime ()) {
1650 return;
1651 }
1652
1653 SetLanguageCodes = TRUE;
1654
1655 //
1656 // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only
1657 // Therefore, in variable driver, only store the original value for other use.
1658 //
1659 if (Global->PlatformLangCodes[VirtualMode] != NULL) {
1660 FreePool (Global->PlatformLangCodes[VirtualMode]);
1661 }
1662 Global->PlatformLangCodes[VirtualMode] = AllocateRuntimeCopyPool (DataSize, Data);
1663 ASSERT (mVariableModuleGlobal->PlatformLangCodes[VirtualMode] != NULL);
1664
1665 //
1666 // PlatformLang holds a single language from PlatformLangCodes,
1667 // so the size of PlatformLangCodes is enough for the PlatformLang.
1668 //
1669 if (Global->PlatformLang[VirtualMode] != NULL) {
1670 FreePool (Global->PlatformLang[VirtualMode]);
1671 }
1672 Global->PlatformLang[VirtualMode] = AllocateRuntimePool (DataSize);
1673 ASSERT (Global->PlatformLang[VirtualMode] != NULL);
1674
1675 } else if (StrCmp (VariableName, PredefinedVariableName[VAR_LANG_CODES]) == 0) {
1676 //
1677 // LangCodes is a volatile variable, so it can not be updated at runtime.
1678 //
1679 if (EfiAtRuntime ()) {
1680 return;
1681 }
1682
1683 SetLanguageCodes = TRUE;
1684
1685 //
1686 // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only
1687 // Therefore, in variable driver, only store the original value for other use.
1688 //
1689 if (Global->LangCodes[VirtualMode] != NULL) {
1690 FreePool (Global->LangCodes[VirtualMode]);
1691 }
1692 Global->LangCodes[VirtualMode] = AllocateRuntimeCopyPool (DataSize, Data);
1693 ASSERT (Global->LangCodes[VirtualMode] != NULL);
1694 }
1695
1696 if (SetLanguageCodes
1697 && (Global->PlatformLangCodes[VirtualMode] != NULL)
1698 && (Global->LangCodes[VirtualMode] != NULL)) {
1699 //
1700 // Update Lang if PlatformLang is already set
1701 // Update PlatformLang if Lang is already set
1702 //
1703 Status = FindVariable (PredefinedVariableName[VAR_PLATFORM_LANG], Global->GlobalVariableGuid[VirtualMode], &Variable, VariableGlobal, Instance);
1704 if (!EFI_ERROR (Status)) {
1705 //
1706 // Update Lang
1707 //
1708 VariableName = PredefinedVariableName[VAR_PLATFORM_LANG];
1709 } else {
1710 Status = FindVariable (PredefinedVariableName[VAR_LANG], Global->GlobalVariableGuid[VirtualMode], &Variable, VariableGlobal, Instance);
1711 if (!EFI_ERROR (Status)) {
1712 //
1713 // Update PlatformLang
1714 //
1715 VariableName = PredefinedVariableName[VAR_LANG];
1716 } else {
1717 //
1718 // Neither PlatformLang nor Lang is set, directly return
1719 //
1720 return;
1721 }
1722 }
1723 Data = (VOID *) GetEndPointer (VariableGlobal->VolatileVariableBase, TRUE, VariableGlobal, Instance);
1724 GetVariableDataPtr ((EFI_PHYSICAL_ADDRESS) Variable.CurrPtr, Variable.Volatile, VariableGlobal, Instance, (CHAR16 *) Data);
1725
1726 Status = AccessVariableStore (
1727 FALSE,
1728 VariableGlobal,
1729 Variable.Volatile,
1730 Instance,
1731 (UINTN) &(((VARIABLE_HEADER *)Variable.CurrPtr)->DataSize),
1732 sizeof (DataSize),
1733 &DataSize
1734 );
1735 ASSERT_EFI_ERROR (Status);
1736 }
1737
1738 //
1739 // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.
1740 //
1741 Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;
1742
1743 if (StrCmp (VariableName, PredefinedVariableName[VAR_PLATFORM_LANG]) == 0) {
1744 //
1745 // Update Lang when PlatformLangCodes/LangCodes were set.
1746 //
1747 if ((Global->PlatformLangCodes[VirtualMode] != NULL) && (Global->LangCodes[VirtualMode] != NULL)) {
1748 //
1749 // When setting PlatformLang, firstly get most matched language string from supported language codes.
1750 //
1751 BestPlatformLang = VariableGetBestLanguage (Global->PlatformLangCodes[VirtualMode], FALSE, VirtualMode, Data, NULL);
1752 if (BestPlatformLang != NULL) {
1753 //
1754 // Get the corresponding index in language codes.
1755 //
1756 Index = GetIndexFromSupportedLangCodes (Global->PlatformLangCodes[VirtualMode], BestPlatformLang, FALSE);
1757
1758 //
1759 // Get the corresponding ISO639 language tag according to RFC4646 language tag.
1760 //
1761 BestLang = GetLangFromSupportedLangCodes (Global->LangCodes[VirtualMode], Index, TRUE, VirtualMode, Global);
1762
1763 //
1764 // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.
1765 //
1766 FindVariable (PredefinedVariableName[VAR_LANG], Global->GlobalVariableGuid[VirtualMode], &Variable, VariableGlobal, Instance);
1767
1768 Status = UpdateVariable (
1769 PredefinedVariableName[VAR_LANG],
1770 Global->GlobalVariableGuid[VirtualMode],
1771 BestLang,
1772 ISO_639_2_ENTRY_SIZE + 1,
1773 Attributes,
1774 0,
1775 0,
1776 VirtualMode,
1777 Global,
1778 &Variable
1779 );
1780
1781 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));
1782
1783 ASSERT_EFI_ERROR (Status);
1784 }
1785 }
1786
1787 } else if (StrCmp (VariableName, PredefinedVariableName[VAR_LANG]) == 0) {
1788 //
1789 // Update PlatformLang when PlatformLangCodes/LangCodes were set.
1790 //
1791 if ((Global->PlatformLangCodes[VirtualMode] != NULL) && (Global->LangCodes[VirtualMode] != NULL)) {
1792 //
1793 // When setting Lang, firstly get most matched language string from supported language codes.
1794 //
1795 BestLang = VariableGetBestLanguage (Global->LangCodes[VirtualMode], TRUE, VirtualMode, Data, NULL);
1796 if (BestLang != NULL) {
1797 //
1798 // Get the corresponding index in language codes.
1799 //
1800 Index = GetIndexFromSupportedLangCodes (Global->LangCodes[VirtualMode], BestLang, TRUE);
1801
1802 //
1803 // Get the corresponding RFC4646 language tag according to ISO639 language tag.
1804 //
1805 BestPlatformLang = GetLangFromSupportedLangCodes (Global->PlatformLangCodes[VirtualMode], Index, FALSE, VirtualMode, Global);
1806
1807 //
1808 // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.
1809 //
1810 FindVariable (PredefinedVariableName[VAR_PLATFORM_LANG], Global->GlobalVariableGuid[VirtualMode], &Variable, VariableGlobal, Instance);
1811
1812 Status = UpdateVariable (
1813 PredefinedVariableName[VAR_PLATFORM_LANG],
1814 Global->GlobalVariableGuid[VirtualMode],
1815 BestPlatformLang,
1816 AsciiStrSize (BestPlatformLang),
1817 Attributes,
1818 0,
1819 0,
1820 VirtualMode,
1821 Global,
1822 &Variable
1823 );
1824
1825 DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));
1826 ASSERT_EFI_ERROR (Status);
1827 }
1828 }
1829 }
1830 }
1831
1832 /**
1833 Update the variable region with Variable information. These are the same
1834 arguments as the EFI Variable services.
1835
1836 @param[in] VariableName Name of variable.
1837 @param[in] VendorGuid Guid of variable.
1838 @param[in] Data Variable data.
1839 @param[in] DataSize Size of data. 0 means delete.
1840 @param[in] Attributes Attributes of the variable.
1841 @param[in] KeyIndex Index of associated public key.
1842 @param[in] MonotonicCount Value of associated monotonic count.
1843 @param[in] VirtualMode Current calling mode for this function.
1844 @param[in] Global Context of this Extended SAL Variable Services Class call.
1845 @param[in] Variable The variable information which is used to keep track of variable usage.
1846
1847 @retval EFI_SUCCESS The update operation is success.
1848 @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.
1849
1850 **/
1851 EFI_STATUS
1852 EFIAPI
1853 UpdateVariable (
1854 IN CHAR16 *VariableName,
1855 IN EFI_GUID *VendorGuid,
1856 IN VOID *Data,
1857 IN UINTN DataSize,
1858 IN UINT32 Attributes OPTIONAL,
1859 IN UINT32 KeyIndex OPTIONAL,
1860 IN UINT64 MonotonicCount OPTIONAL,
1861 IN BOOLEAN VirtualMode,
1862 IN ESAL_VARIABLE_GLOBAL *Global,
1863 IN VARIABLE_POINTER_TRACK *Variable
1864 )
1865 {
1866 EFI_STATUS Status;
1867 VARIABLE_HEADER *NextVariable;
1868 UINTN VarNameOffset;
1869 UINTN VarDataOffset;
1870 UINTN VarNameSize;
1871 UINTN VarSize;
1872 BOOLEAN Volatile;
1873 UINT8 State;
1874 VARIABLE_HEADER VariableHeader;
1875 VARIABLE_HEADER *NextVariableHeader;
1876 BOOLEAN Valid;
1877 BOOLEAN Reclaimed;
1878 VARIABLE_STORE_HEADER VariableStoreHeader;
1879 UINTN ScratchSize;
1880 VARIABLE_GLOBAL *VariableGlobal;
1881 UINT32 Instance;
1882
1883 VariableGlobal = &Global->VariableGlobal[VirtualMode];
1884 Instance = Global->FvbInstance;
1885
1886 Reclaimed = FALSE;
1887
1888 if (Variable->CurrPtr != 0) {
1889
1890 Valid = IsValidVariableHeader (Variable->CurrPtr, Variable->Volatile, VariableGlobal, Instance, &VariableHeader);
1891 if (!Valid) {
1892 Status = EFI_NOT_FOUND;
1893 goto Done;
1894 }
1895
1896 //
1897 // Update/Delete existing variable
1898 //
1899 Volatile = Variable->Volatile;
1900
1901 if (EfiAtRuntime ()) {
1902 //
1903 // If EfiAtRuntime and the variable is Volatile and Runtime Access,
1904 // the volatile is ReadOnly, and SetVariable should be aborted and
1905 // return EFI_WRITE_PROTECTED.
1906 //
1907 if (Variable->Volatile) {
1908 Status = EFI_WRITE_PROTECTED;
1909 goto Done;
1910 }
1911 //
1912 // Only variable have NV attribute can be updated/deleted in Runtime
1913 //
1914 if ((VariableHeader.Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
1915 Status = EFI_INVALID_PARAMETER;
1916 goto Done;
1917 }
1918 }
1919 //
1920 // Setting a data variable with no access, or zero DataSize attributes
1921 // specified causes it to be deleted.
1922 //
1923 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
1924 State = VariableHeader.State;
1925 State &= VAR_DELETED;
1926
1927 Status = AccessVariableStore (
1928 TRUE,
1929 VariableGlobal,
1930 Variable->Volatile,
1931 Instance,
1932 (UINTN) &(((VARIABLE_HEADER *)Variable->CurrPtr)->State),
1933 sizeof (UINT8),
1934 &State
1935 );
1936 if (!EFI_ERROR (Status)) {
1937 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, FALSE, TRUE, FALSE);
1938 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);
1939 }
1940 goto Done;
1941 }
1942 //
1943 // Logic comes here to update variable.
1944 // If the variable is marked valid and the same data has been passed in
1945 // then return to the caller immediately.
1946 //
1947 if (DataSizeOfVariable (&VariableHeader) == DataSize) {
1948 NextVariable = (VARIABLE_HEADER *)GetEndPointer (VariableGlobal->VolatileVariableBase, TRUE, VariableGlobal, Instance);
1949 GetVariableDataPtr (Variable->CurrPtr, Variable->Volatile, VariableGlobal, Instance, (CHAR16 *) NextVariable);
1950 if (CompareMem (Data, (VOID *) NextVariable, DataSize) == 0) {
1951 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
1952 Status = EFI_SUCCESS;
1953 goto Done;
1954 }
1955 }
1956 if ((VariableHeader.State == VAR_ADDED) ||
1957 (VariableHeader.State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {
1958 //
1959 // If new data is different from the old one, mark the old one as VAR_IN_DELETED_TRANSITION.
1960 // It will be deleted if new variable is successfully written.
1961 //
1962 State = VariableHeader.State;
1963 State &= VAR_IN_DELETED_TRANSITION;
1964
1965 Status = AccessVariableStore (
1966 TRUE,
1967 VariableGlobal,
1968 Variable->Volatile,
1969 Instance,
1970 (UINTN) &(((VARIABLE_HEADER *)Variable->CurrPtr)->State),
1971 sizeof (UINT8),
1972 &State
1973 );
1974 if (EFI_ERROR (Status)) {
1975 goto Done;
1976 }
1977 }
1978 } else {
1979 //
1980 // Create a new variable
1981 //
1982
1983 //
1984 // Make sure we are trying to create a new variable.
1985 // Setting a data variable with no access, or zero DataSize attributes means to delete it.
1986 //
1987 if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {
1988 Status = EFI_NOT_FOUND;
1989 goto Done;
1990 }
1991
1992 //
1993 // Only variable have NV|RT attribute can be created in Runtime
1994 //
1995 if (EfiAtRuntime () &&
1996 (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {
1997 Status = EFI_INVALID_PARAMETER;
1998 goto Done;
1999 }
2000 }
2001
2002 //
2003 // Function part - create a new variable and copy the data.
2004 // Both update a variable and create a variable will come here.
2005 //
2006 // Tricky part: Use scratch data area at the end of volatile variable store
2007 // as a temporary storage.
2008 //
2009 NextVariable = (VARIABLE_HEADER *)GetEndPointer (VariableGlobal->VolatileVariableBase, TRUE, VariableGlobal, Instance);
2010 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));
2011 NextVariableHeader = (VARIABLE_HEADER *) NextVariable;
2012
2013 SetMem (NextVariableHeader, ScratchSize, 0xff);
2014
2015 NextVariableHeader->StartId = VARIABLE_DATA;
2016 NextVariableHeader->Attributes = Attributes;
2017 NextVariableHeader->PubKeyIndex = KeyIndex;
2018 NextVariableHeader->MonotonicCount = MonotonicCount;
2019 NextVariableHeader->Reserved = 0;
2020 VarNameOffset = sizeof (VARIABLE_HEADER);
2021 VarNameSize = StrSize (VariableName);
2022 CopyMem (
2023 (UINT8 *) ((UINTN)NextVariable + VarNameOffset),
2024 VariableName,
2025 VarNameSize
2026 );
2027 VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);
2028 CopyMem (
2029 (UINT8 *) ((UINTN)NextVariable + VarDataOffset),
2030 Data,
2031 DataSize
2032 );
2033 CopyMem (&NextVariableHeader->VendorGuid, VendorGuid, sizeof (EFI_GUID));
2034 //
2035 // There will be pad bytes after Data, the NextVariable->NameSize and
2036 // NextVariable->DataSize should not include pad size so that variable
2037 // service can get actual size in GetVariable.
2038 //
2039 NextVariableHeader->NameSize = (UINT32)VarNameSize;
2040 NextVariableHeader->DataSize = (UINT32)DataSize;
2041
2042 //
2043 // The actual size of the variable that stores in storage should
2044 // include pad size.
2045 //
2046 VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);
2047 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
2048 //
2049 // Create a nonvolatile variable
2050 //
2051 Volatile = FALSE;
2052
2053 GetVarStoreHeader (VariableGlobal->NonVolatileVariableBase, FALSE, VariableGlobal, Instance, &VariableStoreHeader);
2054 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)
2055 && ((HEADER_ALIGN (VarSize) + Global->HwErrVariableTotalSize) > PcdGet32(PcdHwErrStorageSize)))
2056 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)
2057 && ((HEADER_ALIGN (VarSize) + Global->CommonVariableTotalSize) > VariableStoreHeader.Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize)))) {
2058 if (EfiAtRuntime ()) {
2059 Status = EFI_OUT_OF_RESOURCES;
2060 goto Done;
2061 }
2062 //
2063 // Perform garbage collection & reclaim operation
2064 //
2065 Status = Reclaim (VariableGlobal->NonVolatileVariableBase, &(Global->NonVolatileLastVariableOffset), FALSE, VirtualMode, Global, Variable->CurrPtr);
2066 if (EFI_ERROR (Status)) {
2067 goto Done;
2068 }
2069
2070 Reclaimed = TRUE;
2071 //
2072 // If still no enough space, return out of resources
2073 //
2074 if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0)
2075 && ((HEADER_ALIGN (VarSize) + Global->HwErrVariableTotalSize) > PcdGet32(PcdHwErrStorageSize)))
2076 || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0)
2077 && ((HEADER_ALIGN (VarSize) + Global->CommonVariableTotalSize) > VariableStoreHeader.Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize)))) {
2078 Status = EFI_OUT_OF_RESOURCES;
2079 goto Done;
2080 }
2081 }
2082 //
2083 // Four steps
2084 // 1. Write variable header
2085 // 2. Set variable state to header valid
2086 // 3. Write variable data
2087 // 4. Set variable state to valid
2088 //
2089 //
2090 // Step 1:
2091 //
2092 Status = AccessVariableStore (
2093 TRUE,
2094 VariableGlobal,
2095 FALSE,
2096 Instance,
2097 VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset,
2098 sizeof (VARIABLE_HEADER),
2099 (UINT8 *) NextVariable
2100 );
2101
2102 if (EFI_ERROR (Status)) {
2103 goto Done;
2104 }
2105
2106 //
2107 // Step 2:
2108 //
2109 NextVariableHeader->State = VAR_HEADER_VALID_ONLY;
2110 Status = AccessVariableStore (
2111 TRUE,
2112 VariableGlobal,
2113 FALSE,
2114 Instance,
2115 VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset,
2116 sizeof (VARIABLE_HEADER),
2117 (UINT8 *) NextVariable
2118 );
2119
2120 if (EFI_ERROR (Status)) {
2121 goto Done;
2122 }
2123 //
2124 // Step 3:
2125 //
2126 Status = AccessVariableStore (
2127 TRUE,
2128 VariableGlobal,
2129 FALSE,
2130 Instance,
2131 VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),
2132 (UINT32) VarSize - sizeof (VARIABLE_HEADER),
2133 (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)
2134 );
2135
2136 if (EFI_ERROR (Status)) {
2137 goto Done;
2138 }
2139 //
2140 // Step 4:
2141 //
2142 NextVariableHeader->State = VAR_ADDED;
2143 Status = AccessVariableStore (
2144 TRUE,
2145 VariableGlobal,
2146 FALSE,
2147 Instance,
2148 VariableGlobal->NonVolatileVariableBase + Global->NonVolatileLastVariableOffset,
2149 sizeof (VARIABLE_HEADER),
2150 (UINT8 *) NextVariable
2151 );
2152
2153 if (EFI_ERROR (Status)) {
2154 goto Done;
2155 }
2156
2157 Global->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);
2158
2159 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {
2160 Global->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);
2161 } else {
2162 Global->CommonVariableTotalSize += HEADER_ALIGN (VarSize);
2163 }
2164 } else {
2165 //
2166 // Create a volatile variable
2167 //
2168 Volatile = TRUE;
2169
2170 if ((UINT32) (HEADER_ALIGN(VarSize) + Global->VolatileLastVariableOffset) >
2171 ((VARIABLE_STORE_HEADER *) ((UINTN) (VariableGlobal->VolatileVariableBase)))->Size) {
2172 //
2173 // Perform garbage collection & reclaim operation
2174 //
2175 Status = Reclaim (VariableGlobal->VolatileVariableBase, &Global->VolatileLastVariableOffset, TRUE, VirtualMode, Global, Variable->CurrPtr);
2176 if (EFI_ERROR (Status)) {
2177 goto Done;
2178 }
2179 //
2180 // If still no enough space, return out of resources
2181 //
2182 if ((UINT32) (HEADER_ALIGN (VarSize) + Global->VolatileLastVariableOffset) >
2183 ((VARIABLE_STORE_HEADER *) ((UINTN) (VariableGlobal->VolatileVariableBase)))->Size
2184 ) {
2185 Status = EFI_OUT_OF_RESOURCES;
2186 goto Done;
2187 }
2188 Reclaimed = TRUE;
2189 }
2190
2191 NextVariableHeader->State = VAR_ADDED;
2192 Status = AccessVariableStore (
2193 TRUE,
2194 VariableGlobal,
2195 TRUE,
2196 Instance,
2197 VariableGlobal->VolatileVariableBase + Global->VolatileLastVariableOffset,
2198 (UINT32) VarSize,
2199 (UINT8 *) NextVariable
2200 );
2201
2202 if (EFI_ERROR (Status)) {
2203 goto Done;
2204 }
2205
2206 Global->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);
2207 }
2208 //
2209 // Mark the old variable as deleted
2210 // If storage has just been reclaimed, the old variable marked as VAR_IN_DELETED_TRANSITION
2211 // has already been eliminated, so no need to delete it.
2212 //
2213 if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != 0) {
2214 State = ((VARIABLE_HEADER *)Variable->CurrPtr)->State;
2215 State &= VAR_DELETED;
2216
2217 Status = AccessVariableStore (
2218 TRUE,
2219 VariableGlobal,
2220 Variable->Volatile,
2221 Instance,
2222 (UINTN) &(((VARIABLE_HEADER *)Variable->CurrPtr)->State),
2223 sizeof (UINT8),
2224 &State
2225 );
2226 }
2227
2228 if (!EFI_ERROR (Status)) {
2229 UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);
2230 UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);
2231 }
2232
2233 Done:
2234 return Status;
2235 }
2236
2237 /**
2238 Implements EsalGetVariable function of Extended SAL Variable Services Class.
2239
2240 This function implements EsalGetVariable function of Extended SAL Variable Services Class.
2241 It is equivalent in functionality to the EFI Runtime Service GetVariable().
2242
2243 @param[in] VariableName A Null-terminated Unicode string that is the name of
2244 the vendor's variable.
2245 @param[in] VendorGuid A unique identifier for the vendor.
2246 @param[out] Attributes If not NULL, a pointer to the memory location to return the
2247 attributes bitmask for the variable.
2248 @param[in, out] DataSize Size of Data found. If size is less than the
2249 data, this value contains the required size.
2250 @param[out] Data On input, the size in bytes of the return Data buffer.
2251 On output, the size of data returned in Data.
2252 @param[in] VirtualMode Current calling mode for this function.
2253 @param[in] Global Context of this Extended SAL Variable Services Class call.
2254
2255 @retval EFI_SUCCESS The function completed successfully.
2256 @retval EFI_NOT_FOUND The variable was not found.
2257 @retval EFI_BUFFER_TOO_SMALL DataSize is too small for the result. DataSize has
2258 been updated with the size needed to complete the request.
2259 @retval EFI_INVALID_PARAMETER VariableName is NULL.
2260 @retval EFI_INVALID_PARAMETER VendorGuid is NULL.
2261 @retval EFI_INVALID_PARAMETER DataSize is NULL.
2262 @retval EFI_INVALID_PARAMETER DataSize is not too small and Data is NULL.
2263 @retval EFI_DEVICE_ERROR The variable could not be retrieved due to a hardware error.
2264 @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due to an authentication failure.
2265
2266 **/
2267 EFI_STATUS
2268 EFIAPI
2269 EsalGetVariable (
2270 IN CHAR16 *VariableName,
2271 IN EFI_GUID *VendorGuid,
2272 OUT UINT32 *Attributes OPTIONAL,
2273 IN OUT UINTN *DataSize,
2274 OUT VOID *Data,
2275 IN BOOLEAN VirtualMode,
2276 IN ESAL_VARIABLE_GLOBAL *Global
2277 )
2278 {
2279 VARIABLE_POINTER_TRACK Variable;
2280 UINTN VarDataSize;
2281 EFI_STATUS Status;
2282 VARIABLE_HEADER VariableHeader;
2283 BOOLEAN Valid;
2284 VARIABLE_GLOBAL *VariableGlobal;
2285 UINT32 Instance;
2286
2287 if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {
2288 return EFI_INVALID_PARAMETER;
2289 }
2290
2291 VariableGlobal = &Global->VariableGlobal[VirtualMode];
2292 Instance = Global->FvbInstance;
2293
2294 AcquireLockOnlyAtBootTime(&VariableGlobal->VariableServicesLock);
2295
2296 //
2297 // Check if this variable exists in cache.
2298 //
2299 Status = FindVariableInCache (VariableName, VendorGuid, Attributes, DataSize, Data);
2300 if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)){
2301 //
2302 // If variable exists in cache, just update statistical information for it and finish.
2303 // Here UpdateVariableInfo() has already retrieved data & attributes for output.
2304 //
2305 UpdateVariableInfo (VariableName, VendorGuid, FALSE, TRUE, FALSE, FALSE, TRUE);
2306 goto Done;
2307 }
2308 //
2309 // If variable does not exist in cache, search for it in variable storage area.
2310 //
2311 Status = FindVariable (VariableName, VendorGuid, &Variable, VariableGlobal, Instance);
2312 if (Variable.CurrPtr == 0x0 || EFI_ERROR (Status)) {
2313 //
2314 // If it cannot be found in variable storage area, goto Done.
2315 //
2316 goto Done;
2317 }
2318
2319 Valid = IsValidVariableHeader (Variable.CurrPtr, Variable.Volatile, VariableGlobal, Instance, &VariableHeader);
2320 if (!Valid) {
2321 Status = EFI_NOT_FOUND;
2322 goto Done;
2323 }
2324 //
2325 // If variable exists, but not in cache, get its data and attributes, update
2326 // statistical information, and update cache.
2327 //
2328 VarDataSize = DataSizeOfVariable (&VariableHeader);
2329 ASSERT (VarDataSize != 0);
2330
2331 if (*DataSize >= VarDataSize) {
2332 if (Data == NULL) {
2333 Status = EFI_INVALID_PARAMETER;
2334 goto Done;
2335 }
2336
2337 GetVariableDataPtr (
2338 Variable.CurrPtr,
2339 Variable.Volatile,
2340 VariableGlobal,
2341 Instance,
2342 Data
2343 );
2344 if (Attributes != NULL) {
2345 *Attributes = VariableHeader.Attributes;
2346 }
2347
2348 *DataSize = VarDataSize;
2349 UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);
2350 UpdateVariableCache (VariableName, VendorGuid, VariableHeader.Attributes, VarDataSize, Data);
2351
2352 Status = EFI_SUCCESS;
2353 goto Done;
2354 } else {
2355 //
2356 // If DataSize is too small for the result, return EFI_BUFFER_TOO_SMALL.
2357 //
2358 *DataSize = VarDataSize;
2359 Status = EFI_BUFFER_TOO_SMALL;
2360 goto Done;
2361 }
2362
2363 Done:
2364 ReleaseLockOnlyAtBootTime (&VariableGlobal->VariableServicesLock);
2365 return Status;
2366 }
2367
2368 /**
2369 Implements EsalGetNextVariableName function of Extended SAL Variable Services Class.
2370
2371 This function implements EsalGetNextVariableName function of Extended SAL Variable Services Class.
2372 It is equivalent in functionality to the EFI Runtime Service GetNextVariableName().
2373
2374 @param[in, out] VariableNameSize Size of the variable
2375 @param[in, out] VariableName On input, supplies the last VariableName that was returned by GetNextVariableName().
2376 On output, returns the Null-terminated Unicode string of the current variable.
2377 @param[in, out] VendorGuid On input, supplies the last VendorGuid that was returned by GetNextVariableName().
2378 On output, returns the VendorGuid of the current variable.
2379 @param[in] VirtualMode Current calling mode for this function.
2380 @param[in] Global Context of this Extended SAL Variable Services Class call.
2381
2382 @retval EFI_SUCCESS The function completed successfully.
2383 @retval EFI_NOT_FOUND The next variable was not found.
2384 @retval EFI_BUFFER_TOO_SMALL VariableNameSize is too small for the result.
2385 VariableNameSize has been updated with the size needed to complete the request.
2386 @retval EFI_INVALID_PARAMETER VariableNameSize is NULL.
2387 @retval EFI_INVALID_PARAMETER VariableName is NULL.
2388 @retval EFI_INVALID_PARAMETER VendorGuid is NULL.
2389 @retval EFI_DEVICE_ERROR The variable name could not be retrieved due to a hardware error.
2390
2391 **/
2392 EFI_STATUS
2393 EFIAPI
2394 EsalGetNextVariableName (
2395 IN OUT UINTN *VariableNameSize,
2396 IN OUT CHAR16 *VariableName,
2397 IN OUT EFI_GUID *VendorGuid,
2398 IN BOOLEAN VirtualMode,
2399 IN ESAL_VARIABLE_GLOBAL *Global
2400 )
2401 {
2402 VARIABLE_POINTER_TRACK Variable;
2403 UINTN VarNameSize;
2404 EFI_STATUS Status;
2405 VARIABLE_HEADER VariableHeader;
2406 VARIABLE_GLOBAL *VariableGlobal;
2407 UINT32 Instance;
2408
2409 if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {
2410 return EFI_INVALID_PARAMETER;
2411 }
2412
2413 VariableGlobal = &Global->VariableGlobal[VirtualMode];
2414 Instance = Global->FvbInstance;
2415
2416 AcquireLockOnlyAtBootTime(&VariableGlobal->VariableServicesLock);
2417
2418 Status = FindVariable (VariableName, VendorGuid, &Variable, VariableGlobal, Instance);
2419 //
2420 // If the variable does not exist, goto Done and return.
2421 //
2422 if (Variable.CurrPtr == 0x0 || EFI_ERROR (Status)) {
2423 goto Done;
2424 }
2425
2426 if (VariableName[0] != 0) {
2427 //
2428 // If variable name is not NULL, get next variable
2429 //
2430 Variable.CurrPtr = GetNextVariablePtr (
2431 Variable.CurrPtr,
2432 Variable.Volatile,
2433 VariableGlobal,
2434 Instance
2435 );
2436 }
2437
2438 while (TRUE) {
2439 if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == 0x0) {
2440 //
2441 // If fail to find a variable in current area, reverse the volatile attribute of area to search.
2442 //
2443 Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));
2444 //
2445 // Here we depend on the searching sequence of FindVariable().
2446 // It first searches volatile area, then NV area.
2447 // So if the volatile attribute after switching is non-volatile, it means that we have finished searching volatile area,
2448 // and EFI_NOT_FOUND is returnd.
2449 // Otherwise, it means that we have finished searchig non-volatile area, and we will continue to search volatile area.
2450 //
2451 if (!Variable.Volatile) {
2452 Variable.StartPtr = GetStartPointer (VariableGlobal->NonVolatileVariableBase);
2453 Variable.EndPtr = GetEndPointer (VariableGlobal->NonVolatileVariableBase, FALSE, VariableGlobal, Instance);
2454 } else {
2455 Status = EFI_NOT_FOUND;
2456 goto Done;
2457 }
2458
2459 Variable.CurrPtr = Variable.StartPtr;
2460 if (!IsValidVariableHeader (Variable.CurrPtr, Variable.Volatile, VariableGlobal, Instance, NULL)) {
2461 continue;
2462 }
2463 }
2464 //
2465 // Variable is found
2466 //
2467 if (IsValidVariableHeader (Variable.CurrPtr, Variable.Volatile, VariableGlobal, Instance, &VariableHeader)) {
2468 if ((VariableHeader.State == VAR_ADDED) &&
2469 (!(EfiAtRuntime () && ((VariableHeader.Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)))) {
2470 VarNameSize = NameSizeOfVariable (&VariableHeader);
2471 ASSERT (VarNameSize != 0);
2472
2473 if (VarNameSize <= *VariableNameSize) {
2474 GetVariableNamePtr (
2475 Variable.CurrPtr,
2476 Variable.Volatile,
2477 VariableGlobal,
2478 Instance,
2479 VariableName
2480 );
2481 CopyMem (
2482 VendorGuid,
2483 &VariableHeader.VendorGuid,
2484 sizeof (EFI_GUID)
2485 );
2486 Status = EFI_SUCCESS;
2487 } else {
2488 Status = EFI_BUFFER_TOO_SMALL;
2489 }
2490
2491 *VariableNameSize = VarNameSize;
2492 goto Done;
2493 }
2494 }
2495
2496 Variable.CurrPtr = GetNextVariablePtr (
2497 Variable.CurrPtr,
2498 Variable.Volatile,
2499 VariableGlobal,
2500 Instance
2501 );
2502 }
2503
2504 Done:
2505 ReleaseLockOnlyAtBootTime (&VariableGlobal->VariableServicesLock);
2506 return Status;
2507 }
2508
2509 /**
2510 Implements EsalSetVariable function of Extended SAL Variable Services Class.
2511
2512 This function implements EsalSetVariable function of Extended SAL Variable Services Class.
2513 It is equivalent in functionality to the EFI Runtime Service SetVariable().
2514
2515 @param[in] VariableName A Null-terminated Unicode string that is the name of the vendor's
2516 variable. Each VariableName is unique for each
2517 VendorGuid. VariableName must contain 1 or more
2518 Unicode characters. If VariableName is an empty Unicode
2519 string, then EFI_INVALID_PARAMETER is returned.
2520 @param[in] VendorGuid A unique identifier for the vendor.
2521 @param[in] Attributes Attributes bitmask to set for the variable.
2522 @param[in] DataSize The size in bytes of the Data buffer. A size of zero causes the
2523 variable to be deleted.
2524 @param[in] Data The contents for the variable.
2525 @param[in] VirtualMode Current calling mode for this function.
2526 @param[in] Global Context of this Extended SAL Variable Services Class call.
2527
2528 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
2529 defined by the Attributes.
2530 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied, or the
2531 DataSize exceeds the maximum allowed.
2532 @retval EFI_INVALID_PARAMETER VariableName is an empty Unicode string.
2533 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.
2534 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.
2535 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
2536 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.
2537 @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due to an authentication failure.
2538 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.
2539
2540 **/
2541 EFI_STATUS
2542 EFIAPI
2543 EsalSetVariable (
2544 IN CHAR16 *VariableName,
2545 IN EFI_GUID *VendorGuid,
2546 IN UINT32 Attributes,
2547 IN UINTN DataSize,
2548 IN VOID *Data,
2549 IN BOOLEAN VirtualMode,
2550 IN ESAL_VARIABLE_GLOBAL *Global
2551 )
2552 {
2553 VARIABLE_POINTER_TRACK Variable;
2554 EFI_STATUS Status;
2555 EFI_PHYSICAL_ADDRESS NextVariable;
2556 EFI_PHYSICAL_ADDRESS Point;
2557 VARIABLE_GLOBAL *VariableGlobal;
2558 UINT32 Instance;
2559 UINT32 KeyIndex;
2560 UINT64 MonotonicCount;
2561 UINTN PayloadSize;
2562
2563 //
2564 // Check input parameters
2565 //
2566 if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {
2567 return EFI_INVALID_PARAMETER;
2568 }
2569
2570 if (DataSize != 0 && Data == NULL) {
2571 return EFI_INVALID_PARAMETER;
2572 }
2573
2574 //
2575 // EFI_VARIABLE_RUNTIME_ACCESS bit cannot be set without EFI_VARIABLE_BOOTSERVICE_ACCESS bit.
2576 //
2577 if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
2578 return EFI_INVALID_PARAMETER;
2579 }
2580
2581 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) == EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) {
2582 if (DataSize < AUTHINFO_SIZE) {
2583 //
2584 // Try to write Authencated Variable without AuthInfo
2585 //
2586 return EFI_SECURITY_VIOLATION;
2587 }
2588 PayloadSize = DataSize - AUTHINFO_SIZE;
2589 } else {
2590 PayloadSize = DataSize;
2591 }
2592
2593 VariableGlobal = &Global->VariableGlobal[VirtualMode];
2594 Instance = Global->FvbInstance;
2595
2596 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2597 //
2598 // For variable for hardware error record, the size of the VariableName, including the Unicode Null
2599 // in bytes plus the DataSize is limited to maximum size of PcdGet32(PcdMaxHardwareErrorVariableSize) bytes.
2600 //
2601 if ((PayloadSize > PcdGet32(PcdMaxHardwareErrorVariableSize)) ||
2602 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + PayloadSize > PcdGet32(PcdMaxHardwareErrorVariableSize))) {
2603 return EFI_INVALID_PARAMETER;
2604 }
2605 //
2606 // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"
2607 //
2608 if (StrnCmp (VariableName, \
2609 Global->VariableName[VirtualMode][VAR_HW_ERR_REC], \
2610 StrLen(Global->VariableName[VirtualMode][VAR_HW_ERR_REC])) != 0) {
2611 return EFI_INVALID_PARAMETER;
2612 }
2613 } else {
2614 //
2615 // For variable not for hardware error record, the size of the VariableName, including the
2616 // Unicode Null in bytes plus the DataSize is limited to maximum size of PcdGet32(PcdMaxVariableSize) bytes.
2617 //
2618 if ((PayloadSize > PcdGet32(PcdMaxVariableSize)) ||
2619 (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + PayloadSize > PcdGet32(PcdMaxVariableSize))) {
2620 return EFI_INVALID_PARAMETER;
2621 }
2622 }
2623
2624 AcquireLockOnlyAtBootTime(&VariableGlobal->VariableServicesLock);
2625
2626 //
2627 // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;
2628 //
2629 if (InterlockedIncrement (&Global->ReentrantState) > 1) {
2630 Point = VariableGlobal->NonVolatileVariableBase;;
2631 //
2632 // Parse non-volatile variable data and get last variable offset
2633 //
2634 NextVariable = GetStartPointer (Point);
2635 while (IsValidVariableHeader (NextVariable, FALSE, VariableGlobal, Instance, NULL)) {
2636 NextVariable = GetNextVariablePtr (NextVariable, FALSE, VariableGlobal, Instance);
2637 }
2638 Global->NonVolatileLastVariableOffset = NextVariable - Point;
2639 }
2640
2641 //
2642 // Check whether the input variable exists
2643 //
2644
2645 Status = FindVariable (VariableName, VendorGuid, &Variable, VariableGlobal, Instance);
2646
2647 //
2648 // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang
2649 //
2650 AutoUpdateLangVariable (VariableName, Data, PayloadSize, VirtualMode, Global);
2651
2652 //
2653 // Process PK, KEK, Sigdb seperately
2654 //
2655 if (CompareGuid (VendorGuid, Global->GlobalVariableGuid[VirtualMode]) && (StrCmp (VariableName, Global->VariableName[VirtualMode][VAR_PLATFORM_KEY]) == 0)) {
2656 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, VirtualMode, Global, &Variable, Attributes, TRUE);
2657 } else if (CompareGuid (VendorGuid, Global->GlobalVariableGuid[VirtualMode]) && (StrCmp (VariableName, Global->VariableName[VirtualMode][VAR_KEY_EXCHANGE_KEY]) == 0)) {
2658 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, VirtualMode, Global, &Variable, Attributes, FALSE);
2659 } else if (CompareGuid (VendorGuid, Global->ImageSecurityDatabaseGuid[VirtualMode])) {
2660 Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, VirtualMode, Global, &Variable, Attributes);
2661 } else {
2662 Status = VerifyVariable (Data, DataSize, VirtualMode, Global, &Variable, Attributes, &KeyIndex, &MonotonicCount);
2663 if (!EFI_ERROR(Status)) {
2664 //
2665 // Verification pass
2666 //
2667 if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
2668 //
2669 // Cut the certificate size before set
2670 //
2671 Status = UpdateVariable (
2672 VariableName,
2673 VendorGuid,
2674 (UINT8*)Data + AUTHINFO_SIZE,
2675 DataSize - AUTHINFO_SIZE,
2676 Attributes,
2677 KeyIndex,
2678 MonotonicCount,
2679 VirtualMode,
2680 Global,
2681 &Variable
2682 );
2683 } else {
2684 //
2685 // Update variable as usual
2686 //
2687 Status = UpdateVariable (
2688 VariableName,
2689 VendorGuid,
2690 Data,
2691 DataSize,
2692 Attributes,
2693 0,
2694 0,
2695 VirtualMode,
2696 Global,
2697 &Variable
2698 );
2699 }
2700 }
2701 }
2702
2703 InterlockedDecrement (&Global->ReentrantState);
2704 ReleaseLockOnlyAtBootTime (&VariableGlobal->VariableServicesLock);
2705 return Status;
2706 }
2707
2708 /**
2709 Implements EsalQueryVariableInfo function of Extended SAL Variable Services Class.
2710
2711 This function implements EsalQueryVariableInfo function of Extended SAL Variable Services Class.
2712 It is equivalent in functionality to the EFI Runtime Service QueryVariableInfo().
2713
2714 @param[in] Attributes Attributes bitmask to specify the type of variables
2715 on which to return information.
2716 @param[out] MaximumVariableStorageSize On output the maximum size of the storage space available for
2717 the EFI variables associated with the attributes specified.
2718 @param[out] RemainingVariableStorageSize Returns the remaining size of the storage space available for EFI
2719 variables associated with the attributes specified.
2720 @param[out] MaximumVariableSize Returns the maximum size of an individual EFI variable
2721 associated with the attributes specified.
2722 @param[in] VirtualMode Current calling mode for this function
2723 @param[in] Global Context of this Extended SAL Variable Services Class call
2724
2725 @retval EFI_SUCCESS Valid answer returned.
2726 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.
2727 @retval EFI_UNSUPPORTED The attribute is not supported on this platform, and the
2728 MaximumVariableStorageSize, RemainingVariableStorageSize,
2729 MaximumVariableSize are undefined.
2730 **/
2731 EFI_STATUS
2732 EFIAPI
2733 EsalQueryVariableInfo (
2734 IN UINT32 Attributes,
2735 OUT UINT64 *MaximumVariableStorageSize,
2736 OUT UINT64 *RemainingVariableStorageSize,
2737 OUT UINT64 *MaximumVariableSize,
2738 IN BOOLEAN VirtualMode,
2739 IN ESAL_VARIABLE_GLOBAL *Global
2740 )
2741 {
2742 EFI_PHYSICAL_ADDRESS Variable;
2743 EFI_PHYSICAL_ADDRESS NextVariable;
2744 UINT64 VariableSize;
2745 EFI_PHYSICAL_ADDRESS VariableStoreHeaderAddress;
2746 BOOLEAN Volatile;
2747 VARIABLE_STORE_HEADER VarStoreHeader;
2748 VARIABLE_HEADER VariableHeader;
2749 UINT64 CommonVariableTotalSize;
2750 UINT64 HwErrVariableTotalSize;
2751 VARIABLE_GLOBAL *VariableGlobal;
2752 UINT32 Instance;
2753
2754 CommonVariableTotalSize = 0;
2755 HwErrVariableTotalSize = 0;
2756
2757 if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {
2758 return EFI_INVALID_PARAMETER;
2759 }
2760
2761 if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {
2762 //
2763 // Make sure the Attributes combination is supported by the platform.
2764 //
2765 return EFI_UNSUPPORTED;
2766 } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {
2767 //
2768 // Make sure if runtime bit is set, boot service bit is set also.
2769 //
2770 return EFI_INVALID_PARAMETER;
2771 } else if (EfiAtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {
2772 //
2773 // Make sure RT Attribute is set if we are in Runtime phase.
2774 //
2775 return EFI_INVALID_PARAMETER;
2776 } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2777 //
2778 // Make sure Hw Attribute is set with NV.
2779 //
2780 return EFI_INVALID_PARAMETER;
2781 }
2782
2783 VariableGlobal = &Global->VariableGlobal[VirtualMode];
2784 Instance = Global->FvbInstance;
2785
2786 AcquireLockOnlyAtBootTime(&VariableGlobal->VariableServicesLock);
2787
2788 if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {
2789 //
2790 // Query is Volatile related.
2791 //
2792 Volatile = TRUE;
2793 VariableStoreHeaderAddress = VariableGlobal->VolatileVariableBase;
2794 } else {
2795 //
2796 // Query is Non-Volatile related.
2797 //
2798 Volatile = FALSE;
2799 VariableStoreHeaderAddress = VariableGlobal->NonVolatileVariableBase;
2800 }
2801
2802 //
2803 // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize
2804 // with the storage size (excluding the storage header size).
2805 //
2806 GetVarStoreHeader (VariableStoreHeaderAddress, Volatile, VariableGlobal, Instance, &VarStoreHeader);
2807
2808 *MaximumVariableStorageSize = VarStoreHeader.Size - sizeof (VARIABLE_STORE_HEADER);
2809
2810 // Harware error record variable needs larger size.
2811 //
2812 if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
2813 *MaximumVariableStorageSize = PcdGet32(PcdHwErrStorageSize);
2814 *MaximumVariableSize = PcdGet32(PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);
2815 } else {
2816 if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {
2817 ASSERT (PcdGet32(PcdHwErrStorageSize) < VarStoreHeader.Size);
2818 *MaximumVariableStorageSize = VarStoreHeader.Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize);
2819 }
2820
2821 //
2822 // Let *MaximumVariableSize be PcdGet32(PcdMaxVariableSize) with the exception of the variable header size.
2823 //
2824 *MaximumVariableSize = PcdGet32(PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);
2825 }
2826
2827 //
2828 // Point to the starting address of the variables.
2829 //
2830 Variable = GetStartPointer (VariableStoreHeaderAddress);
2831
2832 //
2833 // Now walk through the related variable store.
2834 //
2835 while (IsValidVariableHeader (Variable, Volatile, VariableGlobal, Instance, &VariableHeader) &&
2836 (Variable < GetEndPointer (VariableStoreHeaderAddress, Volatile, VariableGlobal, Instance))) {
2837 NextVariable = GetNextVariablePtr (Variable, Volatile, VariableGlobal, Instance);
2838 VariableSize = NextVariable - Variable;
2839
2840 if (EfiAtRuntime ()) {
2841 //
2842 // we don't take the state of the variables in mind
2843 // when calculating RemainingVariableStorageSize,
2844 // since the space occupied by variables not marked with
2845 // VAR_ADDED is not allowed to be reclaimed in Runtime.
2846 //
2847 if ((VariableHeader.Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2848 HwErrVariableTotalSize += VariableSize;
2849 } else {
2850 CommonVariableTotalSize += VariableSize;
2851 }
2852 } else {
2853 //
2854 // Only care about Variables with State VAR_ADDED,because
2855 // the space not marked as VAR_ADDED is reclaimable now.
2856 //
2857 if (VariableHeader.State == VAR_ADDED) {
2858 if ((VariableHeader.Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {
2859 HwErrVariableTotalSize += VariableSize;
2860 } else {
2861 CommonVariableTotalSize += VariableSize;
2862 }
2863 }
2864 }
2865
2866 //
2867 // Go to the next one
2868 //
2869 Variable = NextVariable;
2870 }
2871
2872 if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){
2873 *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;
2874 }else {
2875 *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;
2876 }
2877
2878 if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {
2879 *MaximumVariableSize = 0;
2880 } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {
2881 *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);
2882 }
2883
2884 ReleaseLockOnlyAtBootTime (&VariableGlobal->VariableServicesLock);
2885 return EFI_SUCCESS;
2886 }
2887
2888 /**
2889 Notification function of EVT_GROUP_READY_TO_BOOT event group.
2890
2891 This is a notification function registered on EVT_GROUP_READY_TO_BOOT event group.
2892 When the Boot Manager is about to load and execute a boot option, it reclaims variable
2893 storage if free size is below the threshold.
2894
2895 @param[in] Event Event whose notification function is being invoked.
2896 @param[in] Context Pointer to the notification function's context.
2897
2898 **/
2899 VOID
2900 EFIAPI
2901 ReclaimForOS(
2902 IN EFI_EVENT Event,
2903 IN VOID *Context
2904 )
2905 {
2906 UINT32 VarSize;
2907 EFI_STATUS Status;
2908 UINTN CommonVariableSpace;
2909 UINTN RemainingCommonVariableSpace;
2910 UINTN RemainingHwErrVariableSpace;
2911
2912 VarSize = ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase))->Size;
2913 Status = EFI_SUCCESS;
2914 //
2915 //Allowable max size of common variable storage space
2916 //
2917 CommonVariableSpace = VarSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize);
2918
2919 RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;
2920
2921 RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;
2922 //
2923 // If the free area is below a threshold, then performs reclaim operation.
2924 //
2925 if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))
2926 || ((PcdGet32 (PcdHwErrStorageSize) != 0) &&
2927 (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){
2928 Status = Reclaim (
2929 mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase,
2930 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
2931 FALSE,
2932 Physical,
2933 mVariableModuleGlobal,
2934 0x0
2935 );
2936 ASSERT_EFI_ERROR (Status);
2937 }
2938 }
2939
2940 /**
2941 Initializes variable store area for non-volatile and volatile variable.
2942
2943 This function allocates and initializes memory space for global context of ESAL
2944 variable service and variable store area for non-volatile and volatile variable.
2945
2946 @param[in] ImageHandle The Image handle of this driver.
2947 @param[in] SystemTable The pointer of EFI_SYSTEM_TABLE.
2948
2949 @retval EFI_SUCCESS Function successfully executed.
2950 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.
2951
2952 **/
2953 EFI_STATUS
2954 VariableCommonInitialize (
2955 IN EFI_HANDLE ImageHandle,
2956 IN EFI_SYSTEM_TABLE *SystemTable
2957 )
2958 {
2959 EFI_STATUS Status;
2960 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
2961 EFI_PHYSICAL_ADDRESS CurrPtr;
2962 VARIABLE_STORE_HEADER *VolatileVariableStore;
2963 VARIABLE_STORE_HEADER *VariableStoreHeader;
2964 EFI_PHYSICAL_ADDRESS Variable;
2965 EFI_PHYSICAL_ADDRESS NextVariable;
2966 UINTN VariableSize;
2967 UINT32 Instance;
2968 EFI_PHYSICAL_ADDRESS FvVolHdr;
2969 EFI_PHYSICAL_ADDRESS TempVariableStoreHeader;
2970 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
2971 UINT64 BaseAddress;
2972 UINT64 Length;
2973 UINTN Index;
2974 UINT8 Data;
2975 EFI_PHYSICAL_ADDRESS VariableStoreBase;
2976 UINT64 VariableStoreLength;
2977 EFI_EVENT ReadyToBootEvent;
2978 UINTN ScratchSize;
2979
2980 //
2981 // Allocate memory for mVariableModuleGlobal
2982 //
2983 mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (ESAL_VARIABLE_GLOBAL));
2984 if (mVariableModuleGlobal == NULL) {
2985 return EFI_OUT_OF_RESOURCES;
2986 }
2987
2988 mVariableModuleGlobal->GlobalVariableGuid[Physical] = &gEfiGlobalVariableGuid;
2989 CopyMem (
2990 mVariableModuleGlobal->VariableName[Physical],
2991 mVariableName,
2992 sizeof (mVariableName)
2993 );
2994
2995 EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal[Physical].VariableServicesLock, TPL_NOTIFY);
2996
2997 //
2998 // Note that in EdkII variable driver implementation, Hardware Error Record type variable
2999 // is stored with common variable in the same NV region. So the platform integrator should
3000 // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of
3001 // PcdFlashNvStorageVariableSize.
3002 //
3003 ASSERT (PcdGet32(PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));
3004
3005 //
3006 // Allocate memory for volatile variable store
3007 //
3008 ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));
3009 VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);
3010 if (VolatileVariableStore == NULL) {
3011 FreePool (mVariableModuleGlobal);
3012 return EFI_OUT_OF_RESOURCES;
3013 }
3014
3015 SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);
3016
3017 //
3018 // Variable Specific Data
3019 //
3020 mVariableModuleGlobal->VariableGlobal[Physical].VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;
3021 mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer ((EFI_PHYSICAL_ADDRESS) VolatileVariableStore) - (UINTN) VolatileVariableStore;
3022
3023 CopyGuid (&VolatileVariableStore->Signature, &gEfiAuthenticatedVariableGuid);
3024 VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);
3025 VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;
3026 VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;
3027 VolatileVariableStore->Reserved = 0;
3028 VolatileVariableStore->Reserved1 = 0;
3029
3030 //
3031 // Get non volatile varaible store
3032 //
3033 TempVariableStoreHeader = (UINT64) PcdGet32 (PcdFlashNvStorageVariableBase);
3034 VariableStoreBase = TempVariableStoreHeader + \
3035 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);
3036 VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \
3037 (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);
3038 //
3039 // Mark the variable storage region of the FLASH as RUNTIME
3040 //
3041 BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);
3042 Length = VariableStoreLength + (VariableStoreBase - BaseAddress);
3043 Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);
3044
3045 Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);
3046 if (EFI_ERROR (Status)) {
3047 goto Done;
3048 }
3049
3050 Status = gDS->SetMemorySpaceAttributes (
3051 BaseAddress,
3052 Length,
3053 GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME
3054 );
3055 if (EFI_ERROR (Status)) {
3056 goto Done;
3057 }
3058 //
3059 // Get address of non volatile variable store base.
3060 //
3061 mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase = VariableStoreBase;
3062
3063 //
3064 // Check Integrity
3065 //
3066 //
3067 // Find the Correct Instance of the FV Block Service.
3068 //
3069 Instance = 0;
3070 CurrPtr = mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase;
3071
3072 do {
3073 FvVolHdr = 0;
3074 Status = (EFI_STATUS) EsalCall (
3075 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_LO,
3076 EFI_EXTENDED_SAL_FV_BLOCK_SERVICES_PROTOCOL_GUID_HI,
3077 GetPhysicalAddressFunctionId,
3078 Instance,
3079 (UINT64) &FvVolHdr,
3080 0,
3081 0,
3082 0,
3083 0,
3084 0
3085 ).Status;
3086 if (EFI_ERROR (Status)) {
3087 break;
3088 }
3089 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);
3090 ASSERT (FwVolHeader != NULL);
3091 if (CurrPtr >= (EFI_PHYSICAL_ADDRESS) FwVolHeader &&
3092 CurrPtr < ((EFI_PHYSICAL_ADDRESS) FwVolHeader + FwVolHeader->FvLength)) {
3093 mVariableModuleGlobal->FvbInstance = Instance;
3094 break;
3095 }
3096
3097 Instance++;
3098 } while (Status == EFI_SUCCESS);
3099
3100 VariableStoreHeader = (VARIABLE_STORE_HEADER *) CurrPtr;
3101 if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {
3102 if (~VariableStoreHeader->Size == 0) {
3103 Status = AccessVariableStore (
3104 TRUE,
3105 &mVariableModuleGlobal->VariableGlobal[Physical],
3106 FALSE,
3107 mVariableModuleGlobal->FvbInstance,
3108 (UINTN) &VariableStoreHeader->Size,
3109 sizeof (UINT32),
3110 (UINT8 *) &VariableStoreLength
3111 );
3112 //
3113 // As Variables are stored in NV storage, which are slow devices,such as flash.
3114 // Variable operation may skip checking variable program result to improve performance,
3115 // We can assume Variable program is OK through some check point.
3116 // Variable Store Size Setting should be the first Variable write operation,
3117 // We can assume all Read/Write is OK if we can set Variable store size successfully.
3118 // If write fail, we will assert here.
3119 //
3120 ASSERT(VariableStoreHeader->Size == VariableStoreLength);
3121
3122 if (EFI_ERROR (Status)) {
3123 goto Done;
3124 }
3125 }
3126
3127 mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) CurrPtr);
3128 //
3129 // Parse non-volatile variable data and get last variable offset.
3130 //
3131 Variable = GetStartPointer (CurrPtr);
3132 Status = EFI_SUCCESS;
3133
3134 while (IsValidVariableHeader (Variable, FALSE, &(mVariableModuleGlobal->VariableGlobal[Physical]), Instance, NULL)) {
3135 NextVariable = GetNextVariablePtr (
3136 Variable,
3137 FALSE,
3138 &(mVariableModuleGlobal->VariableGlobal[Physical]),
3139 Instance
3140 );
3141 VariableSize = NextVariable - Variable;
3142 if ((((VARIABLE_HEADER *)Variable)->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {
3143 mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;
3144 } else {
3145 mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;
3146 }
3147
3148 Variable = NextVariable;
3149 }
3150
3151 mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) Variable - (UINTN) CurrPtr;
3152
3153 //
3154 // Check if the free area is really free.
3155 //
3156 for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {
3157 Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase)[Index];
3158 if (Data != 0xff) {
3159 //
3160 // There must be something wrong in variable store, do reclaim operation.
3161 //
3162 Status = Reclaim (
3163 mVariableModuleGlobal->VariableGlobal[Physical].NonVolatileVariableBase,
3164 &mVariableModuleGlobal->NonVolatileLastVariableOffset,
3165 FALSE,
3166 Physical,
3167 mVariableModuleGlobal,
3168 0x0
3169 );
3170 if (EFI_ERROR (Status)) {
3171 goto Done;
3172 }
3173 break;
3174 }
3175 }
3176
3177 //
3178 // Register the event handling function to reclaim variable for OS usage.
3179 //
3180 Status = EfiCreateEventReadyToBootEx (
3181 TPL_NOTIFY,
3182 ReclaimForOS,
3183 NULL,
3184 &ReadyToBootEvent
3185 );
3186 } else {
3187 Status = EFI_VOLUME_CORRUPTED;
3188 DEBUG((EFI_D_INFO, "Variable Store header is corrupted\n"));
3189 }
3190
3191 Done:
3192 if (EFI_ERROR (Status)) {
3193 FreePool (mVariableModuleGlobal);
3194 FreePool (VolatileVariableStore);
3195 }
3196
3197 return Status;
3198 }