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