TianoCore EDK2 master
Loading...
Searching...
No Matches
FvbService.c
Go to the documentation of this file.
1
9#include "FvbService.h"
10
11//
12// Global variable for this FVB driver which contains
13// the private data of all firmware volume block instances
14//
15FWB_GLOBAL mFvbModuleGlobal;
16
17FV_MEMMAP_DEVICE_PATH mFvMemmapDevicePathTemplate = {
18 {
19 {
22 {
23 (UINT8)(sizeof (MEMMAP_DEVICE_PATH)),
24 (UINT8)(sizeof (MEMMAP_DEVICE_PATH) >> 8)
25 }
26 },
30 },
31 {
32 END_DEVICE_PATH_TYPE,
33 END_ENTIRE_DEVICE_PATH_SUBTYPE,
34 {
35 END_DEVICE_PATH_LENGTH,
36 0
37 }
38 }
39};
40
41FV_PIWG_DEVICE_PATH mFvPIWGDevicePathTemplate = {
42 {
43 {
44 MEDIA_DEVICE_PATH,
46 {
47 (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH)),
48 (UINT8)(sizeof (MEDIA_FW_VOL_DEVICE_PATH) >> 8)
49 }
50 },
51 { 0 }
52 },
53 {
54 END_DEVICE_PATH_TYPE,
55 END_ENTIRE_DEVICE_PATH_SUBTYPE,
56 {
57 END_DEVICE_PATH_LENGTH,
58 0
59 }
60 }
61};
62
63EFI_FW_VOL_BLOCK_DEVICE mFvbDeviceTemplate = {
64 FVB_DEVICE_SIGNATURE,
65 NULL,
66 0, // Instance
67 {
75 NULL
76 } // FwVolBlockInstance
77};
78
92 IN UINTN Instance
93 )
94{
95 EFI_FW_VOL_INSTANCE *FwhRecord;
96
97 if ( Instance >= mFvbModuleGlobal.NumFv ) {
98 ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);
99 return NULL;
100 }
101
102 //
103 // Find the right instance of the FVB private data
104 //
105 FwhRecord = mFvbModuleGlobal.FvInstance;
106 while ( Instance > 0 ) {
107 FwhRecord = (EFI_FW_VOL_INSTANCE *)((UINTN)((UINT8 *)FwhRecord) +
108 FwhRecord->VolumeHeader.HeaderLength +
109 (sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER)));
110 Instance--;
111 }
112
113 return FwhRecord;
114}
115
124STATIC
127 IN UINTN Instance
128 )
129{
130 EFI_FW_VOL_INSTANCE *FwInstance;
131
132 FwInstance = GetFvbInstance (Instance);
133 ASSERT (FwInstance != NULL);
134
135 if (FwInstance == NULL) {
136 return 0;
137 }
138
139 return FwInstance->VolumeHeader.Attributes;
140}
141
160STATIC
163 IN UINTN Instance,
164 IN EFI_LBA Lba,
165 OUT UINTN *LbaAddress,
166 OUT UINTN *LbaLength,
167 OUT UINTN *NumOfBlocks
168 )
169{
170 UINT32 NumBlocks;
171 UINT32 BlockLength;
172 UINTN Offset;
173 EFI_LBA StartLba;
174 EFI_LBA NextLba;
175 EFI_FW_VOL_INSTANCE *FwhInstance;
176 EFI_FV_BLOCK_MAP_ENTRY *BlockMap;
177
178 //
179 // Find the right instance of the FVB private data
180 //
181 FwhInstance = GetFvbInstance (Instance);
182 if (FwhInstance == NULL) {
183 return EFI_INVALID_PARAMETER;
184 }
185
186 StartLba = 0;
187 Offset = 0;
188 BlockMap = &FwhInstance->VolumeHeader.BlockMap[0];
189 ASSERT (BlockMap != NULL);
190
191 //
192 // Parse the blockmap of the FV to find which map entry the Lba belongs to
193 //
194 while (TRUE) {
195 if ( BlockMap != NULL) {
196 NumBlocks = BlockMap->NumBlocks;
197 BlockLength = BlockMap->Length;
198 }
199
200 if ((NumBlocks == 0) || (BlockLength == 0)) {
201 return EFI_INVALID_PARAMETER;
202 }
203
204 NextLba = StartLba + NumBlocks;
205
206 //
207 // The map entry found
208 //
209 if ((Lba >= StartLba) && (Lba < NextLba)) {
210 Offset = Offset + (UINTN)MultU64x32 ((Lba - StartLba), BlockLength);
211 if (LbaAddress != NULL) {
212 *LbaAddress = FwhInstance->FvBase + Offset;
213 }
214
215 if (LbaLength != NULL) {
216 *LbaLength = BlockLength;
217 }
218
219 if (NumOfBlocks != NULL) {
220 *NumOfBlocks = (UINTN)(NextLba - Lba);
221 }
222
223 return EFI_SUCCESS;
224 }
225
226 StartLba = NextLba;
227 Offset = Offset + NumBlocks * BlockLength;
228 BlockMap++;
229 }
230}
231
256STATIC
259 IN UINTN Instance,
260 IN EFI_LBA Lba,
261 IN UINTN BlockOffset,
262 IN OUT UINTN *NumBytes,
263 IN UINT8 *Buffer
264 )
265{
266 EFI_FVB_ATTRIBUTES_2 Attributes;
267 UINTN LbaAddress;
268 UINTN LbaLength;
269 EFI_STATUS Status;
271
272 if ((NumBytes == NULL) || (Buffer == NULL)) {
273 return (EFI_INVALID_PARAMETER);
274 }
275
276 if (*NumBytes == 0) {
277 return (EFI_INVALID_PARAMETER);
278 }
279
280 Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL);
281 if (EFI_ERROR (Status)) {
282 return Status;
283 }
284
285 Attributes = FvbGetVolumeAttributes (Instance);
286
287 if ((Attributes & EFI_FVB2_READ_STATUS) == 0) {
288 return (EFI_ACCESS_DENIED);
289 }
290
291 if (BlockOffset > LbaLength) {
292 return (EFI_INVALID_PARAMETER);
293 }
294
295 if (LbaLength < (*NumBytes + BlockOffset)) {
296 *NumBytes = (UINT32)(LbaLength - BlockOffset);
297 Status = EFI_BAD_BUFFER_SIZE;
298 }
299
300 ReadStatus = LibFvbFlashDeviceRead (LbaAddress + BlockOffset, NumBytes, Buffer);
301 if (EFI_ERROR (ReadStatus)) {
302 return ReadStatus;
303 }
304
305 return Status;
306}
307
331 IN UINTN Instance,
332 IN EFI_LBA Lba,
333 IN UINTN BlockOffset,
334 IN OUT UINTN *NumBytes,
335 IN UINT8 *Buffer
336 )
337{
338 EFI_FVB_ATTRIBUTES_2 Attributes;
339 UINTN LbaAddress;
340 UINTN LbaLength;
341 EFI_STATUS Status;
342
343 if ((NumBytes == NULL) || (Buffer == NULL)) {
344 return (EFI_INVALID_PARAMETER);
345 }
346
347 if (*NumBytes == 0) {
348 return (EFI_INVALID_PARAMETER);
349 }
350
351 Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL);
352 if (EFI_ERROR (Status)) {
353 return Status;
354 }
355
356 //
357 // Check if the FV is write enabled
358 //
359 Attributes = FvbGetVolumeAttributes (Instance);
360 if ((Attributes & EFI_FVB2_WRITE_STATUS) == 0) {
361 return EFI_ACCESS_DENIED;
362 }
363
364 //
365 // Perform boundary checks and adjust NumBytes
366 //
367 if (BlockOffset > LbaLength) {
368 return EFI_INVALID_PARAMETER;
369 }
370
371 if ( LbaLength < (*NumBytes + BlockOffset)) {
372 DEBUG ((
373 DEBUG_ERROR,
374 "FvWriteBlock: Reducing Numbytes from 0x%x to 0x%x\n",
375 *NumBytes,
376 (UINT32)(LbaLength - BlockOffset)
377 ));
378 *NumBytes = (UINT32)(LbaLength - BlockOffset);
379 return EFI_BAD_BUFFER_SIZE;
380 }
381
382 LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, FALSE);
383 Status = LibFvbFlashDeviceWrite (LbaAddress + BlockOffset, NumBytes, Buffer);
384
385 LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, TRUE);
386 WriteBackInvalidateDataCacheRange ((VOID *)(LbaAddress + BlockOffset), *NumBytes);
387 return Status;
388}
389
406 IN UINTN Instance,
407 IN EFI_LBA Lba
408 )
409{
410 EFI_FVB_ATTRIBUTES_2 Attributes;
411 UINTN LbaAddress;
412 UINTN LbaLength;
413 EFI_STATUS Status;
414
415 //
416 // Check if the FV is write enabled
417 //
418 Attributes = FvbGetVolumeAttributes (Instance);
419
420 if ((Attributes & EFI_FVB2_WRITE_STATUS) == 0) {
421 return (EFI_ACCESS_DENIED);
422 }
423
424 //
425 // Get the starting address of the block for erase.
426 //
427 Status = FvbGetLbaAddress (Instance, Lba, &LbaAddress, &LbaLength, NULL);
428 if (EFI_ERROR (Status)) {
429 return Status;
430 }
431
432 LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, FALSE);
433
434 Status = LibFvbFlashDeviceBlockErase (LbaAddress, LbaLength);
435
436 LibFvbFlashDeviceBlockLock (LbaAddress, LbaLength, TRUE);
437
438 WriteBackInvalidateDataCacheRange ((VOID *)LbaAddress, LbaLength);
439
440 return Status;
441}
442
461STATIC
464 IN UINTN Instance,
465 IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
466 )
467{
468 EFI_FW_VOL_INSTANCE *FwhInstance;
469 EFI_FVB_ATTRIBUTES_2 OldAttributes;
470 EFI_FVB_ATTRIBUTES_2 *AttribPtr;
471 EFI_FVB_ATTRIBUTES_2 UnchangedAttributes;
472 UINT32 Capabilities;
473 UINT32 OldStatus;
474 UINT32 NewStatus;
475
476 //
477 // Find the right instance of the FVB private data
478 //
479 FwhInstance = GetFvbInstance (Instance);
480 if (FwhInstance == NULL) {
481 return EFI_INVALID_PARAMETER;
482 }
483
484 AttribPtr = (EFI_FVB_ATTRIBUTES_2 *)&(FwhInstance->VolumeHeader.Attributes);
485 ASSERT (AttribPtr != NULL);
486 if ( AttribPtr == NULL) {
487 return EFI_INVALID_PARAMETER;
488 }
489
490 OldAttributes = *AttribPtr;
491 Capabilities = OldAttributes & EFI_FVB2_CAPABILITIES;
492 OldStatus = OldAttributes & EFI_FVB2_STATUS;
493 NewStatus = *Attributes & EFI_FVB2_STATUS;
494
495 UnchangedAttributes = EFI_FVB2_READ_DISABLED_CAP | \
496 EFI_FVB2_READ_ENABLED_CAP | \
497 EFI_FVB2_WRITE_DISABLED_CAP | \
498 EFI_FVB2_WRITE_ENABLED_CAP | \
499 EFI_FVB2_LOCK_CAP | \
500 EFI_FVB2_STICKY_WRITE | \
501 EFI_FVB2_MEMORY_MAPPED | \
502 EFI_FVB2_ERASE_POLARITY | \
503 EFI_FVB2_READ_LOCK_CAP | \
504 EFI_FVB2_WRITE_LOCK_CAP | \
505 EFI_FVB2_ALIGNMENT;
506
507 //
508 // Some attributes of FV is read only can *not* be set
509 //
510 if ((OldAttributes & UnchangedAttributes) ^ (*Attributes & UnchangedAttributes)) {
511 return EFI_INVALID_PARAMETER;
512 }
513
514 //
515 // If firmware volume is locked, no status bit can be updated
516 //
517 if ((OldAttributes & EFI_FVB2_LOCK_STATUS) != 0) {
518 if ((OldStatus ^ NewStatus) != 0) {
519 return EFI_ACCESS_DENIED;
520 }
521 }
522
523 //
524 // Test read disable
525 //
526 if ((Capabilities & EFI_FVB2_READ_DISABLED_CAP) == 0) {
527 if ((NewStatus & EFI_FVB2_READ_STATUS) == 0) {
528 return EFI_INVALID_PARAMETER;
529 }
530 }
531
532 //
533 // Test read enable
534 //
535 if ((Capabilities & EFI_FVB2_READ_ENABLED_CAP) == 0) {
536 if ((NewStatus & EFI_FVB2_READ_STATUS) != 0) {
537 return EFI_INVALID_PARAMETER;
538 }
539 }
540
541 //
542 // Test write disable
543 //
544 if ((Capabilities & EFI_FVB2_WRITE_DISABLED_CAP) == 0) {
545 if ((NewStatus & EFI_FVB2_WRITE_STATUS) == 0) {
546 return EFI_INVALID_PARAMETER;
547 }
548 }
549
550 //
551 // Test write enable
552 //
553 if ((Capabilities & EFI_FVB2_WRITE_ENABLED_CAP) == 0) {
554 if ((NewStatus & EFI_FVB2_WRITE_STATUS) != 0) {
555 return EFI_INVALID_PARAMETER;
556 }
557 }
558
559 //
560 // Test lock
561 //
562 if ((Capabilities & EFI_FVB2_LOCK_CAP) == 0) {
563 if ((NewStatus & EFI_FVB2_LOCK_STATUS) != 0) {
564 return EFI_INVALID_PARAMETER;
565 }
566 }
567
568 *AttribPtr = (*AttribPtr) & (0xFFFFFFFF & (~EFI_FVB2_STATUS));
569 *AttribPtr = (*AttribPtr) | NewStatus;
570 *Attributes = *AttribPtr;
571
572 return EFI_SUCCESS;
573}
574
586EFIAPI
590 )
591{
592 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
593 EFI_FW_VOL_INSTANCE *FwhInstance;
594
595 FvbDevice = FVB_DEVICE_FROM_THIS (This);
596 FwhInstance = GetFvbInstance (FvbDevice->Instance);
597 if (FwhInstance == NULL) {
598 return EFI_INVALID_PARAMETER;
599 }
600
601 *Address = FwhInstance->FvBase;
602 return EFI_SUCCESS;
603}
604
621EFIAPI
624 IN EFI_LBA Lba,
625 OUT UINTN *BlockSize,
626 OUT UINTN *NumOfBlocks
627 )
628{
629 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
630
631 FvbDevice = FVB_DEVICE_FROM_THIS (This);
632 return FvbGetLbaAddress (FvbDevice->Instance, Lba, NULL, BlockSize, NumOfBlocks);
633}
634
645EFIAPI
648 OUT EFI_FVB_ATTRIBUTES_2 *Attributes
649 )
650{
651 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
652
653 FvbDevice = FVB_DEVICE_FROM_THIS (This);
654 *Attributes = FvbGetVolumeAttributes (FvbDevice->Instance);
655
656 return EFI_SUCCESS;
657}
658
669EFIAPI
672 IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes
673 )
674{
675 EFI_STATUS Status;
676 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
677
678 FvbDevice = FVB_DEVICE_FROM_THIS (This);
679 Status = FvbSetVolumeAttributes (FvbDevice->Instance, Attributes);
680 return Status;
681}
682
703EFIAPI
706 ...
707 )
708{
709 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
710 EFI_FW_VOL_INSTANCE *FwhInstance;
711 UINTN NumOfBlocks;
712 VA_LIST args;
713 EFI_LBA StartingLba;
714 UINTN NumOfLba;
715 EFI_STATUS Status;
716
717 FvbDevice = FVB_DEVICE_FROM_THIS (This);
718 FwhInstance = GetFvbInstance (FvbDevice->Instance);
719 if (FwhInstance == NULL) {
720 return EFI_OUT_OF_RESOURCES;
721 }
722
723 NumOfBlocks = FwhInstance->NumOfBlocks;
724 VA_START (args, This);
725
726 do {
727 StartingLba = VA_ARG (args, EFI_LBA);
728 if ( StartingLba == EFI_LBA_LIST_TERMINATOR ) {
729 break;
730 }
731
732 NumOfLba = VA_ARG (args, UINT32);
733
734 //
735 // Check input parameters
736 //
737 if (NumOfLba == 0) {
738 VA_END (args);
739 return EFI_INVALID_PARAMETER;
740 }
741
742 if ((StartingLba + NumOfLba) > NumOfBlocks ) {
743 return EFI_INVALID_PARAMETER;
744 }
745 } while (1);
746
747 VA_END (args);
748
749 VA_START (args, This);
750 do {
751 StartingLba = VA_ARG (args, EFI_LBA);
752 if (StartingLba == EFI_LBA_LIST_TERMINATOR) {
753 break;
754 }
755
756 NumOfLba = VA_ARG (args, UINT32);
757
758 while ( NumOfLba > 0 ) {
759 Status = FvbEraseBlock (FvbDevice->Instance, StartingLba);
760 if ( EFI_ERROR (Status)) {
761 VA_END (args);
762 return Status;
763 }
764
765 StartingLba++;
766 NumOfLba--;
767 }
768 } while (1);
769
770 VA_END (args);
771
772 return EFI_SUCCESS;
773}
774
801EFIAPI
804 IN EFI_LBA Lba,
805 IN UINTN Offset,
806 IN OUT UINTN *NumBytes,
807 IN UINT8 *Buffer
808 )
809{
810 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
811 EFI_STATUS Status;
812
813 FvbDevice = FVB_DEVICE_FROM_THIS (This);
814 Status = FvbWriteBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer);
815 DEBUG ((
816 DEBUG_VERBOSE,
817 "FvbWrite: Lba: 0x%lx Offset: 0x%x NumBytes: 0x%x, Buffer: 0x%x Status:%r\n",
818 Lba,
819 Offset,
820 *NumBytes,
821 Buffer,
822 Status
823 ));
824
825 return Status;
826}
827
857EFIAPI
860 IN EFI_LBA Lba,
861 IN UINTN Offset,
862 IN OUT UINTN *NumBytes,
863 OUT UINT8 *Buffer
864 )
865{
866 EFI_FW_VOL_BLOCK_DEVICE *FvbDevice;
867 EFI_STATUS Status;
868
869 FvbDevice = FVB_DEVICE_FROM_THIS (This);
870 Status = FvbReadBlock (FvbDevice->Instance, Lba, Offset, NumBytes, Buffer);
871 DEBUG ((
872 DEBUG_VERBOSE,
873 "FvbRead: Lba: 0x%lx Offset: 0x%x NumBytes: 0x%x, Buffer: 0x%x, Status:%r\n",
874 Lba,
875 Offset,
876 *NumBytes,
877 Buffer,
878 Status
879 ));
880
881 return Status;
882}
883
893BOOLEAN
896 )
897{
898 UINT16 Sum;
899 EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;
900
901 FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)FvBase;
902 if (FvBase == PcdGet32 (PcdFlashNvStorageVariableBase)) {
903 if (CompareMem (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid, sizeof (EFI_GUID)) != 0 ) {
904 DEBUG ((DEBUG_INFO, " --FileSystemGuid not match: %g\n", &FwVolHeader->FileSystemGuid));
905 return FALSE;
906 }
907 } else {
908 if (CompareMem (&FwVolHeader->FileSystemGuid, &gEfiFirmwareFileSystem2Guid, sizeof (EFI_GUID)) != 0 ) {
909 DEBUG ((DEBUG_INFO, " --not expected guid.\n"));
910 return FALSE;
911 }
912 }
913
914 if ((FwVolHeader->Revision != EFI_FVH_REVISION) ||
915 (FwVolHeader->Signature != EFI_FVH_SIGNATURE) ||
916 (FwVolHeader->FvLength == ((UINTN)-1)) ||
917 ((FwVolHeader->HeaderLength & 0x01) != 0))
918 {
919 DEBUG ((DEBUG_INFO, " -- >Revision = 0x%x, Signature = 0x%x\n", FwVolHeader->Revision, FwVolHeader->Signature));
920 DEBUG ((DEBUG_INFO, " -- >FvLength = 0x%lx, HeaderLength = 0x%x\n", FwVolHeader->FvLength, FwVolHeader->HeaderLength));
921 return FALSE;
922 }
923
924 Sum = CalculateSum16 ((UINT16 *)FwVolHeader, FwVolHeader->HeaderLength);
925 if (Sum != 0) {
926 DEBUG ((DEBUG_INFO, "error: checksum: 0x%04X (expect 0x0)\n", Sum));
927 return FALSE;
928 }
929
930 return TRUE;
931}
932
946 OUT VOID **VarData,
947 OUT UINTN *VarSize
948 )
949{
950 EFI_STATUS Status;
951 VOID *ImageData;
952 UINTN ImageSize;
954 VARIABLE_STORE_HEADER *VariableStore;
956 UINTN VariableSize;
957 UINTN VarEndAddr;
958
959 if ((VarData == NULL) || (VarSize == NULL)) {
960 return EFI_INVALID_PARAMETER;
961 }
962
963 Status = GetSectionFromAnyFv (PcdGetPtr (PcdNvsDataFile), EFI_SECTION_RAW, 0, &ImageData, &ImageSize);
964 if (EFI_ERROR (Status)) {
965 return Status;
966 }
967
968 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)ImageData;
969 VariableStore = (VARIABLE_STORE_HEADER *)((UINT8 *)ImageData + FvHeader->HeaderLength);
970 VarEndAddr = (UINTN)VariableStore + VariableStore->Size;
971 Variable = (AUTHENTICATED_VARIABLE_HEADER *)HEADER_ALIGN (VariableStore + 1);
972 *VarData = (VOID *)Variable;
973 while (((UINTN)Variable < VarEndAddr)) {
974 if (Variable->StartId != VARIABLE_DATA) {
975 break;
976 }
977
978 VariableSize = sizeof (AUTHENTICATED_VARIABLE_HEADER) + Variable->DataSize + Variable->NameSize;
979 Variable = (AUTHENTICATED_VARIABLE_HEADER *)HEADER_ALIGN ((UINTN)Variable + VariableSize);
980 }
981
982 *VarSize = (UINTN)Variable - HEADER_ALIGN (VariableStore + 1);
983
984 return EFI_SUCCESS;
985}
986
997 VOID
998 )
999{
1000 EFI_FW_VOL_INSTANCE *FwVolInstance;
1002 EFI_FV_BLOCK_MAP_ENTRY *BlockMap;
1003 EFI_PHYSICAL_ADDRESS BaseAddress;
1004 UINTN WriteAddr;
1005 EFI_STATUS Status;
1006 UINTN BufferSize;
1007 UINTN Length;
1008 VARIABLE_STORE_HEADER VariableStore;
1009 VOID *VarData;
1010
1012 BaseAddress = PcdGet32 (PcdFlashNvStorageVariableBase);
1013 FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)BaseAddress;
1014
1015 //
1016 // Check FV header and variable store header
1017 //
1018 if (!IsFvHeaderValid (BaseAddress)) {
1019 //
1020 // Write back a healthy FV header
1021 //
1022 DEBUG ((DEBUG_ERROR, "Fvb: Writing back a healthy FV header: 0x%lx\n", BaseAddress));
1023 FvHeader = GetFvHeaderTemplate ();
1024 LibFvbFlashDeviceBlockLock ((UINTN)BaseAddress, FvHeader->BlockMap->Length, FALSE);
1025
1026 Status = LibFvbFlashDeviceBlockErase ((UINTN)BaseAddress, FvHeader->BlockMap->Length);
1027 ASSERT_EFI_ERROR (Status);
1028
1029 Length = FvHeader->HeaderLength;
1030 WriteAddr = (UINTN)BaseAddress;
1031 Status = LibFvbFlashDeviceWrite (WriteAddr, &Length, (UINT8 *)FvHeader);
1032 WriteAddr += Length;
1033 ASSERT_EFI_ERROR (Status);
1034
1035 //
1036 // Write back variable store header
1037 //
1038 VariableStore.Size = PcdGet32 (PcdFlashNvStorageVariableSize) - FvHeader->HeaderLength;
1039 VariableStore.Format = VARIABLE_STORE_FORMATTED;
1040 VariableStore.State = VARIABLE_STORE_HEALTHY;
1041 CopyGuid (&VariableStore.Signature, &gEfiAuthenticatedVariableGuid);
1042 BufferSize = sizeof (VARIABLE_STORE_HEADER);
1043 Status = LibFvbFlashDeviceWrite (WriteAddr, &BufferSize, (UINT8 *)&VariableStore);
1044 WriteAddr += BufferSize;
1045 ASSERT_EFI_ERROR (Status);
1046
1047 //
1048 // Write initial variable data if found
1049 //
1050 Status = GetInitialVariableData (&VarData, &Length);
1051 if (!EFI_ERROR (Status)) {
1052 Status = LibFvbFlashDeviceWrite (WriteAddr, &Length, (UINT8 *)VarData);
1053 ASSERT_EFI_ERROR (Status);
1054 }
1055
1056 LibFvbFlashDeviceBlockLock ((UINTN)BaseAddress, FvHeader->BlockMap->Length, TRUE);
1057 WriteBackInvalidateDataCacheRange ((VOID *)(UINTN)BaseAddress, FvHeader->BlockMap->Length);
1058 }
1059
1060 //
1061 // Create a new FW volume instance for NVS variable
1062 //
1063 BufferSize = FvHeader->HeaderLength + sizeof (EFI_FW_VOL_INSTANCE) - sizeof (EFI_FIRMWARE_VOLUME_HEADER);
1064 FwVolInstance = (EFI_FW_VOL_INSTANCE *)AllocateRuntimeZeroPool (BufferSize);
1065 if (FwVolInstance == NULL) {
1066 return EFI_OUT_OF_RESOURCES;
1067 }
1068
1069 FwVolInstance->FvBase = (UINTN)BaseAddress;
1070 CopyMem (&FwVolInstance->VolumeHeader, FvHeader, FvHeader->HeaderLength);
1071
1072 //
1073 // Process the block map for each FV. Assume it has same block size.
1074 //
1075 FwVolInstance->NumOfBlocks = 0;
1076 FvHeader = &FwVolInstance->VolumeHeader;
1077 for (BlockMap = FvHeader->BlockMap; BlockMap->NumBlocks != 0; BlockMap++) {
1078 FwVolInstance->NumOfBlocks += BlockMap->NumBlocks;
1079 }
1080
1081 //
1082 // Add a FVB Protocol Instance
1083 //
1084 Status = InstallFvbProtocol (FwVolInstance, mFvbModuleGlobal.NumFv);
1085 mFvbModuleGlobal.NumFv++;
1086 mFvbModuleGlobal.FvInstance = FwVolInstance;
1087
1088 return Status;
1089}
UINT64 UINTN
VOID *EFIAPI WriteBackInvalidateDataCacheRange(IN VOID *Address, IN UINTN Length)
UINT16 EFIAPI CalculateSum16(IN CONST UINT16 *Buffer, IN UINTN Length)
Definition: CheckSum.c:107
UINT64 EFIAPI MultU64x32(IN UINT64 Multiplicand, IN UINT32 Multiplier)
Definition: MultU64x32.c:27
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
GUID *EFIAPI CopyGuid(OUT GUID *DestinationGuid, IN CONST GUID *SourceGuid)
Definition: MemLibGuid.c:39
#define HARDWARE_DEVICE_PATH
Definition: DevicePath.h:68
#define MEDIA_PIWG_FW_VOL_DP
Definition: DevicePath.h:1146
#define HW_MEMMAP_DP
Definition: DevicePath.h:109
EFI_STATUS EFIAPI GetSectionFromAnyFv(IN CONST EFI_GUID *NameGuid, IN EFI_SECTION_TYPE SectionType, IN UINTN SectionInstance, OUT VOID **Buffer, OUT UINTN *Size)
VOID *EFIAPI AllocateRuntimeZeroPool(IN UINTN AllocationSize)
#define EFI_LBA_LIST_TERMINATOR
EFI_STATUS EFIAPI LibFvbFlashDeviceRead(IN UINTN PAddress, IN OUT UINTN *NumBytes, OUT UINT8 *Buffer)
EFI_STATUS EFIAPI LibFvbFlashDeviceBlockLock(IN UINTN PAddress, IN UINTN LbaLength, IN BOOLEAN Lock)
EFI_STATUS EFIAPI LibFvbFlashDeviceBlockErase(IN UINTN PAddress, IN UINTN LbaLength)
EFI_STATUS EFIAPI LibFvbFlashDeviceWrite(IN UINTN PAddress, IN OUT UINTN *NumBytes, IN UINT8 *Buffer)
EFI_STATUS EFIAPI FvbProtocolRead(IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This, IN EFI_LBA Lba, IN UINTN Offset, IN OUT UINTN *NumBytes, OUT UINT8 *Buffer)
Definition: FvbService.c:858
EFI_STATUS EFIAPI FvbProtocolSetAttributes(IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This, IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes)
Definition: FvbService.c:670
EFI_STATUS FvbInitialize(VOID)
Definition: FvbService.c:996
BOOLEAN IsFvHeaderValid(IN EFI_PHYSICAL_ADDRESS FvBase)
Definition: FvbService.c:894
EFI_FW_VOL_INSTANCE * GetFvbInstance(IN UINTN Instance)
Definition: FvbService.c:91
STATIC EFI_STATUS FvbGetLbaAddress(IN UINTN Instance, IN EFI_LBA Lba, OUT UINTN *LbaAddress, OUT UINTN *LbaLength, OUT UINTN *NumOfBlocks)
Definition: FvbService.c:162
STATIC EFI_STATUS FvbSetVolumeAttributes(IN UINTN Instance, IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes)
Definition: FvbService.c:463
EFI_STATUS EFIAPI FvbProtocolEraseBlocks(IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This,...)
Definition: FvbService.c:704
EFI_STATUS FvbWriteBlock(IN UINTN Instance, IN EFI_LBA Lba, IN UINTN BlockOffset, IN OUT UINTN *NumBytes, IN UINT8 *Buffer)
Definition: FvbService.c:330
EFI_STATUS EFIAPI FvbProtocolGetPhysicalAddress(IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This, OUT EFI_PHYSICAL_ADDRESS *Address)
Definition: FvbService.c:587
EFI_STATUS EFIAPI FvbProtocolWrite(IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This, IN EFI_LBA Lba, IN UINTN Offset, IN OUT UINTN *NumBytes, IN UINT8 *Buffer)
Definition: FvbService.c:802
EFI_STATUS EFIAPI FvbProtocolGetBlockSize(IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This, IN EFI_LBA Lba, OUT UINTN *BlockSize, OUT UINTN *NumOfBlocks)
Definition: FvbService.c:622
EFI_STATUS FvbEraseBlock(IN UINTN Instance, IN EFI_LBA Lba)
Definition: FvbService.c:405
STATIC EFI_FVB_ATTRIBUTES_2 FvbGetVolumeAttributes(IN UINTN Instance)
Definition: FvbService.c:126
STATIC EFI_STATUS FvbReadBlock(IN UINTN Instance, IN EFI_LBA Lba, IN UINTN BlockOffset, IN OUT UINTN *NumBytes, IN UINT8 *Buffer)
Definition: FvbService.c:258
EFI_STATUS GetInitialVariableData(OUT VOID **VarData, OUT UINTN *VarSize)
Definition: FvbService.c:945
EFI_STATUS EFIAPI FvbProtocolGetAttributes(IN CONST EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *This, OUT EFI_FVB_ATTRIBUTES_2 *Attributes)
Definition: FvbService.c:646
EFI_STATUS InstallFvbProtocol(IN EFI_FW_VOL_INSTANCE *FwhInstance, IN UINTN InstanceNum)
Definition: FvbServiceSmm.c:28
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define VA_ARG(Marker, TYPE)
Definition: Base.h:679
#define VA_START(Marker, Parameter)
Definition: Base.h:661
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
CHAR8 * VA_LIST
Definition: Base.h:643
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define VA_END(Marker)
Definition: Base.h:691
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define PcdGet32(TokenName)
Definition: PcdLib.h:362
#define PcdGetPtr(TokenName)
Definition: PcdLib.h:388
UINT32 EFI_FVB_ATTRIBUTES_2
#define EFI_FVH_REVISION
EFI_STATUS EFIAPI ReadStatus(IN CONST EFI_SPI_NOR_FLASH_PROTOCOL *This, IN UINT32 LengthInBytes, OUT UINT8 *FlashStatus)
Definition: SpiNorFlash.c:707
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
UINT64 EFI_LBA
Definition: UefiBaseType.h:45
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
@ EfiMemoryMappedIO
EFI_STATUS InitVariableStore(VOID)
Definition: FvbInfo.c:63
EFI_FIRMWARE_VOLUME_HEADER * GetFvHeaderTemplate(VOID)
Definition: FvbInfo.c:136
#define VARIABLE_DATA
#define VARIABLE_STORE_FORMATTED
EFI_FV_BLOCK_MAP_ENTRY BlockMap[1]
EFI_FVB_ATTRIBUTES_2 Attributes
Definition: Base.h:213