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