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