]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.c
MdeModulePkg/VariableFlashInfoLib: Add initial library
[mirror_edk2.git] / MdeModulePkg / Library / BaseVariableFlashInfoLib / BaseVariableFlashInfoLib.c
1 /** @file
2 Variable Flash Information Library
3
4 Copyright (c) Microsoft Corporation<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <Uefi.h>
11 #include <Pi/PiMultiPhase.h>
12 #include <Guid/VariableFlashInfo.h>
13 #include <Library/DebugLib.h>
14 #include <Library/HobLib.h>
15 #include <Library/VariableFlashInfoLib.h>
16
17 /**
18 Get the HOB that contains variable flash information.
19
20 @param[out] VariableFlashInfo Pointer to a pointer to set to the variable flash information structure.
21
22 @retval EFI_SUCCESS Variable flash information was found successfully.
23 @retval EFI_INVALID_PARAMETER The VariableFlashInfo pointer given is NULL.
24 @retval EFI_NOT_FOUND Variable flash information could not be found.
25
26 **/
27 STATIC
28 EFI_STATUS
29 GetVariableFlashInfoFromHob (
30 OUT VARIABLE_FLASH_INFO **VariableFlashInfo
31 )
32 {
33 EFI_HOB_GUID_TYPE *GuidHob;
34
35 if (VariableFlashInfo == NULL) {
36 return EFI_INVALID_PARAMETER;
37 }
38
39 GuidHob = GetFirstGuidHob (&gVariableFlashInfoHobGuid);
40 if (GuidHob == NULL) {
41 return EFI_NOT_FOUND;
42 }
43
44 *VariableFlashInfo = GET_GUID_HOB_DATA (GuidHob);
45
46 //
47 // Assert if more than one variable flash information HOB is present.
48 //
49 DEBUG_CODE (
50 if ((GetNextGuidHob (&gVariableFlashInfoHobGuid, GET_NEXT_HOB (GuidHob)) != NULL)) {
51 DEBUG ((DEBUG_ERROR, "ERROR: Found two variable flash information HOBs\n"));
52 ASSERT (FALSE);
53 }
54
55 );
56
57 return EFI_SUCCESS;
58 }
59
60 /**
61 Get the base address and size for the NV storage area used for UEFI variable storage.
62
63 @param[out] BaseAddress The NV storage base address.
64 @param[out] Length The NV storage length in bytes.
65
66 @retval EFI_SUCCESS NV storage information was found successfully.
67 @retval EFI_INVALID_PARAMETER A required pointer parameter is NULL.
68
69 **/
70 EFI_STATUS
71 EFIAPI
72 GetVariableFlashNvStorageInfo (
73 OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
74 OUT UINT64 *Length
75 )
76 {
77 EFI_STATUS Status;
78 VARIABLE_FLASH_INFO *VariableFlashInfo;
79
80 if ((BaseAddress == NULL) || (Length == NULL)) {
81 return EFI_INVALID_PARAMETER;
82 }
83
84 Status = GetVariableFlashInfoFromHob (&VariableFlashInfo);
85 if (!EFI_ERROR (Status)) {
86 *BaseAddress = VariableFlashInfo->NvVariableBaseAddress;
87 *Length = VariableFlashInfo->NvVariableLength;
88 } else {
89 *BaseAddress = (EFI_PHYSICAL_ADDRESS)(PcdGet64 (PcdFlashNvStorageVariableBase64) != 0 ?
90 PcdGet64 (PcdFlashNvStorageVariableBase64) :
91 PcdGet32 (PcdFlashNvStorageVariableBase)
92 );
93 *Length = (UINT64)PcdGet32 (PcdFlashNvStorageVariableSize);
94 }
95
96 return EFI_SUCCESS;
97 }
98
99 /**
100 Get the base address and size for the fault tolerant write (FTW) spare
101 area used for UEFI variable storage.
102
103 @param[out] BaseAddress The FTW spare base address.
104 @param[out] Length The FTW spare length in bytes.
105
106 @retval EFI_SUCCESS FTW spare information was found successfully.
107 @retval EFI_INVALID_PARAMETER A required pointer parameter is NULL.
108 @retval EFI_NOT_FOUND FTW spare information could not be found.
109
110 **/
111 EFI_STATUS
112 EFIAPI
113 GetVariableFlashFtwSpareInfo (
114 OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
115 OUT UINT64 *Length
116 )
117 {
118 EFI_STATUS Status;
119 VARIABLE_FLASH_INFO *VariableFlashInfo;
120
121 if ((BaseAddress == NULL) || (Length == NULL)) {
122 return EFI_INVALID_PARAMETER;
123 }
124
125 Status = GetVariableFlashInfoFromHob (&VariableFlashInfo);
126 if (!EFI_ERROR (Status)) {
127 *BaseAddress = VariableFlashInfo->FtwSpareBaseAddress;
128 *Length = VariableFlashInfo->FtwSpareLength;
129 } else {
130 *BaseAddress = (EFI_PHYSICAL_ADDRESS)(PcdGet64 (PcdFlashNvStorageFtwSpareBase64) != 0 ?
131 PcdGet64 (PcdFlashNvStorageFtwSpareBase64) :
132 PcdGet32 (PcdFlashNvStorageFtwSpareBase)
133 );
134 *Length = (UINT64)PcdGet32 (PcdFlashNvStorageFtwSpareSize);
135 }
136
137 return EFI_SUCCESS;
138 }
139
140 /**
141 Get the base address and size for the fault tolerant write (FTW) working
142 area used for UEFI variable storage.
143
144 @param[out] BaseAddress The FTW working area base address.
145 @param[out] Length The FTW working area length in bytes.
146
147 @retval EFI_SUCCESS FTW working information was found successfully.
148 @retval EFI_INVALID_PARAMETER A required pointer parameter is NULL.
149 @retval EFI_NOT_FOUND FTW working information could not be found.
150
151 **/
152 EFI_STATUS
153 EFIAPI
154 GetVariableFlashFtwWorkingInfo (
155 OUT EFI_PHYSICAL_ADDRESS *BaseAddress,
156 OUT UINT64 *Length
157 )
158 {
159 EFI_STATUS Status;
160 VARIABLE_FLASH_INFO *VariableFlashInfo;
161
162 if ((BaseAddress == NULL) || (Length == NULL)) {
163 return EFI_INVALID_PARAMETER;
164 }
165
166 Status = GetVariableFlashInfoFromHob (&VariableFlashInfo);
167 if (!EFI_ERROR (Status)) {
168 *BaseAddress = VariableFlashInfo->FtwWorkingBaseAddress;
169 *Length = VariableFlashInfo->FtwWorkingLength;
170 } else {
171 *BaseAddress = (EFI_PHYSICAL_ADDRESS)(PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) != 0 ?
172 PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) :
173 PcdGet32 (PcdFlashNvStorageFtwWorkingBase)
174 );
175 *Length = (UINT64)PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
176 }
177
178 return EFI_SUCCESS;
179 }