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