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