]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | \r | |
3 | The common variable operation routines shared by DXE_RINTIME variable \r | |
4 | module and DXE_SMM variable module.\r | |
5 | \r | |
6 | Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r | |
7 | This program and the accompanying materials \r | |
8 | are licensed and made available under the terms and conditions of the BSD License \r | |
9 | which accompanies this distribution. The full text of the license may be found at \r | |
10 | http://opensource.org/licenses/bsd-license.php \r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | #include "Variable.h"\r | |
18 | \r | |
19 | VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;\r | |
20 | \r | |
21 | ///\r | |
22 | /// Define a memory cache that improves the search performance for a variable.\r | |
23 | ///\r | |
24 | VARIABLE_STORE_HEADER *mNvVariableCache = NULL;\r | |
25 | \r | |
26 | ///\r | |
27 | /// The memory entry used for variable statistics data.\r | |
28 | ///\r | |
29 | VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r | |
30 | \r | |
31 | \r | |
32 | /**\r | |
33 | Routine used to track statistical information about variable usage. \r | |
34 | The data is stored in the EFI system table so it can be accessed later.\r | |
35 | VariableInfo.efi can dump out the table. Only Boot Services variable \r | |
36 | accesses are tracked by this code. The PcdVariableCollectStatistics\r | |
37 | build flag controls if this feature is enabled. \r | |
38 | \r | |
39 | A read that hits in the cache will have Read and Cache true for \r | |
40 | the transaction. Data is allocated by this routine, but never\r | |
41 | freed.\r | |
42 | \r | |
43 | @param[in] VariableName Name of the Variable to track.\r | |
44 | @param[in] VendorGuid Guid of the Variable to track.\r | |
45 | @param[in] Volatile TRUE if volatile FALSE if non-volatile.\r | |
46 | @param[in] Read TRUE if GetVariable() was called.\r | |
47 | @param[in] Write TRUE if SetVariable() was called.\r | |
48 | @param[in] Delete TRUE if deleted via SetVariable().\r | |
49 | @param[in] Cache TRUE for a cache hit.\r | |
50 | \r | |
51 | **/\r | |
52 | VOID\r | |
53 | UpdateVariableInfo (\r | |
54 | IN CHAR16 *VariableName,\r | |
55 | IN EFI_GUID *VendorGuid,\r | |
56 | IN BOOLEAN Volatile,\r | |
57 | IN BOOLEAN Read,\r | |
58 | IN BOOLEAN Write,\r | |
59 | IN BOOLEAN Delete,\r | |
60 | IN BOOLEAN Cache\r | |
61 | )\r | |
62 | {\r | |
63 | VARIABLE_INFO_ENTRY *Entry;\r | |
64 | \r | |
65 | if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r | |
66 | \r | |
67 | if (AtRuntime ()) {\r | |
68 | // Don't collect statistics at runtime.\r | |
69 | return;\r | |
70 | }\r | |
71 | \r | |
72 | if (gVariableInfo == NULL) {\r | |
73 | //\r | |
74 | // On the first call allocate a entry and place a pointer to it in\r | |
75 | // the EFI System Table.\r | |
76 | //\r | |
77 | gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r | |
78 | ASSERT (gVariableInfo != NULL);\r | |
79 | \r | |
80 | CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r | |
81 | gVariableInfo->Name = AllocatePool (StrSize (VariableName));\r | |
82 | ASSERT (gVariableInfo->Name != NULL);\r | |
83 | StrCpy (gVariableInfo->Name, VariableName);\r | |
84 | gVariableInfo->Volatile = Volatile;\r | |
85 | }\r | |
86 | \r | |
87 | \r | |
88 | for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r | |
89 | if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r | |
90 | if (StrCmp (VariableName, Entry->Name) == 0) {\r | |
91 | if (Read) {\r | |
92 | Entry->ReadCount++;\r | |
93 | }\r | |
94 | if (Write) {\r | |
95 | Entry->WriteCount++;\r | |
96 | }\r | |
97 | if (Delete) {\r | |
98 | Entry->DeleteCount++;\r | |
99 | }\r | |
100 | if (Cache) {\r | |
101 | Entry->CacheCount++;\r | |
102 | }\r | |
103 | \r | |
104 | return;\r | |
105 | }\r | |
106 | }\r | |
107 | \r | |
108 | if (Entry->Next == NULL) {\r | |
109 | //\r | |
110 | // If the entry is not in the table add it.\r | |
111 | // Next iteration of the loop will fill in the data.\r | |
112 | //\r | |
113 | Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r | |
114 | ASSERT (Entry->Next != NULL);\r | |
115 | \r | |
116 | CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r | |
117 | Entry->Next->Name = AllocatePool (StrSize (VariableName));\r | |
118 | ASSERT (Entry->Next->Name != NULL);\r | |
119 | StrCpy (Entry->Next->Name, VariableName);\r | |
120 | Entry->Next->Volatile = Volatile;\r | |
121 | }\r | |
122 | \r | |
123 | }\r | |
124 | }\r | |
125 | }\r | |
126 | \r | |
127 | \r | |
128 | /**\r | |
129 | \r | |
130 | This code checks if variable header is valid or not.\r | |
131 | \r | |
132 | @param Variable Pointer to the Variable Header.\r | |
133 | \r | |
134 | @retval TRUE Variable header is valid.\r | |
135 | @retval FALSE Variable header is not valid.\r | |
136 | \r | |
137 | **/\r | |
138 | BOOLEAN\r | |
139 | IsValidVariableHeader (\r | |
140 | IN VARIABLE_HEADER *Variable\r | |
141 | )\r | |
142 | {\r | |
143 | if (Variable == NULL || Variable->StartId != VARIABLE_DATA) {\r | |
144 | return FALSE;\r | |
145 | }\r | |
146 | \r | |
147 | return TRUE;\r | |
148 | }\r | |
149 | \r | |
150 | \r | |
151 | /**\r | |
152 | \r | |
153 | This function writes data to the FWH at the correct LBA even if the LBAs\r | |
154 | are fragmented.\r | |
155 | \r | |
156 | @param Global Pointer to VARAIBLE_GLOBAL structure.\r | |
157 | @param Volatile Point out the Variable is Volatile or Non-Volatile.\r | |
158 | @param SetByIndex TRUE if target pointer is given as index.\r | |
159 | FALSE if target pointer is absolute.\r | |
160 | @param Fvb Pointer to the writable FVB protocol.\r | |
161 | @param DataPtrIndex Pointer to the Data from the end of VARIABLE_STORE_HEADER\r | |
162 | structure.\r | |
163 | @param DataSize Size of data to be written.\r | |
164 | @param Buffer Pointer to the buffer from which data is written.\r | |
165 | \r | |
166 | @retval EFI_INVALID_PARAMETER Parameters not valid.\r | |
167 | @retval EFI_SUCCESS Variable store successfully updated.\r | |
168 | \r | |
169 | **/\r | |
170 | EFI_STATUS\r | |
171 | UpdateVariableStore (\r | |
172 | IN VARIABLE_GLOBAL *Global,\r | |
173 | IN BOOLEAN Volatile,\r | |
174 | IN BOOLEAN SetByIndex,\r | |
175 | IN EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb,\r | |
176 | IN UINTN DataPtrIndex,\r | |
177 | IN UINT32 DataSize,\r | |
178 | IN UINT8 *Buffer\r | |
179 | )\r | |
180 | {\r | |
181 | EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r | |
182 | UINTN BlockIndex2;\r | |
183 | UINTN LinearOffset;\r | |
184 | UINTN CurrWriteSize;\r | |
185 | UINTN CurrWritePtr;\r | |
186 | UINT8 *CurrBuffer;\r | |
187 | EFI_LBA LbaNumber;\r | |
188 | UINTN Size;\r | |
189 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
190 | VARIABLE_STORE_HEADER *VolatileBase;\r | |
191 | EFI_PHYSICAL_ADDRESS FvVolHdr;\r | |
192 | EFI_PHYSICAL_ADDRESS DataPtr;\r | |
193 | EFI_STATUS Status;\r | |
194 | \r | |
195 | FwVolHeader = NULL;\r | |
196 | DataPtr = DataPtrIndex;\r | |
197 | \r | |
198 | //\r | |
199 | // Check if the Data is Volatile.\r | |
200 | //\r | |
201 | if (!Volatile) {\r | |
202 | Status = Fvb->GetPhysicalAddress(Fvb, &FvVolHdr);\r | |
203 | ASSERT_EFI_ERROR (Status);\r | |
204 | \r | |
205 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r | |
206 | //\r | |
207 | // Data Pointer should point to the actual Address where data is to be\r | |
208 | // written.\r | |
209 | //\r | |
210 | if (SetByIndex) {\r | |
211 | DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r | |
212 | }\r | |
213 | \r | |
214 | if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {\r | |
215 | return EFI_INVALID_PARAMETER;\r | |
216 | }\r | |
217 | } else {\r | |
218 | //\r | |
219 | // Data Pointer should point to the actual Address where data is to be\r | |
220 | // written.\r | |
221 | //\r | |
222 | VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r | |
223 | if (SetByIndex) {\r | |
224 | DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r | |
225 | }\r | |
226 | \r | |
227 | if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r | |
228 | return EFI_INVALID_PARAMETER;\r | |
229 | }\r | |
230 | \r | |
231 | //\r | |
232 | // If Volatile Variable just do a simple mem copy.\r | |
233 | // \r | |
234 | CopyMem ((UINT8 *)(UINTN)DataPtr, Buffer, DataSize);\r | |
235 | return EFI_SUCCESS;\r | |
236 | }\r | |
237 | \r | |
238 | //\r | |
239 | // If we are here we are dealing with Non-Volatile Variables.\r | |
240 | //\r | |
241 | LinearOffset = (UINTN) FwVolHeader;\r | |
242 | CurrWritePtr = (UINTN) DataPtr;\r | |
243 | CurrWriteSize = DataSize;\r | |
244 | CurrBuffer = Buffer;\r | |
245 | LbaNumber = 0;\r | |
246 | \r | |
247 | if (CurrWritePtr < LinearOffset) {\r | |
248 | return EFI_INVALID_PARAMETER;\r | |
249 | }\r | |
250 | \r | |
251 | for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r | |
252 | for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {\r | |
253 | //\r | |
254 | // Check to see if the Variable Writes are spanning through multiple\r | |
255 | // blocks.\r | |
256 | //\r | |
257 | if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {\r | |
258 | if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {\r | |
259 | Status = Fvb->Write (\r | |
260 | Fvb,\r | |
261 | LbaNumber,\r | |
262 | (UINTN) (CurrWritePtr - LinearOffset),\r | |
263 | &CurrWriteSize,\r | |
264 | CurrBuffer\r | |
265 | );\r | |
266 | return Status;\r | |
267 | } else {\r | |
268 | Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r | |
269 | Status = Fvb->Write (\r | |
270 | Fvb,\r | |
271 | LbaNumber,\r | |
272 | (UINTN) (CurrWritePtr - LinearOffset),\r | |
273 | &Size,\r | |
274 | CurrBuffer\r | |
275 | );\r | |
276 | if (EFI_ERROR (Status)) {\r | |
277 | return Status;\r | |
278 | }\r | |
279 | \r | |
280 | CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;\r | |
281 | CurrBuffer = CurrBuffer + Size;\r | |
282 | CurrWriteSize = CurrWriteSize - Size;\r | |
283 | }\r | |
284 | }\r | |
285 | \r | |
286 | LinearOffset += PtrBlockMapEntry->Length;\r | |
287 | LbaNumber++;\r | |
288 | }\r | |
289 | }\r | |
290 | \r | |
291 | return EFI_SUCCESS;\r | |
292 | }\r | |
293 | \r | |
294 | \r | |
295 | /**\r | |
296 | \r | |
297 | This code gets the current status of Variable Store.\r | |
298 | \r | |
299 | @param VarStoreHeader Pointer to the Variable Store Header.\r | |
300 | \r | |
301 | @retval EfiRaw Variable store status is raw.\r | |
302 | @retval EfiValid Variable store status is valid.\r | |
303 | @retval EfiInvalid Variable store status is invalid.\r | |
304 | \r | |
305 | **/\r | |
306 | VARIABLE_STORE_STATUS\r | |
307 | GetVariableStoreStatus (\r | |
308 | IN VARIABLE_STORE_HEADER *VarStoreHeader\r | |
309 | )\r | |
310 | {\r | |
311 | if (CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid) &&\r | |
312 | VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r | |
313 | VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r | |
314 | ) {\r | |
315 | \r | |
316 | return EfiValid;\r | |
317 | } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == 0xffffffff &&\r | |
318 | ((UINT32 *)(&VarStoreHeader->Signature))[1] == 0xffffffff &&\r | |
319 | ((UINT32 *)(&VarStoreHeader->Signature))[2] == 0xffffffff &&\r | |
320 | ((UINT32 *)(&VarStoreHeader->Signature))[3] == 0xffffffff &&\r | |
321 | VarStoreHeader->Size == 0xffffffff &&\r | |
322 | VarStoreHeader->Format == 0xff &&\r | |
323 | VarStoreHeader->State == 0xff\r | |
324 | ) {\r | |
325 | \r | |
326 | return EfiRaw;\r | |
327 | } else {\r | |
328 | return EfiInvalid;\r | |
329 | }\r | |
330 | }\r | |
331 | \r | |
332 | \r | |
333 | /**\r | |
334 | \r | |
335 | This code gets the size of name of variable.\r | |
336 | \r | |
337 | @param Variable Pointer to the Variable Header.\r | |
338 | \r | |
339 | @return UINTN Size of variable in bytes.\r | |
340 | \r | |
341 | **/\r | |
342 | UINTN\r | |
343 | NameSizeOfVariable (\r | |
344 | IN VARIABLE_HEADER *Variable\r | |
345 | )\r | |
346 | {\r | |
347 | if (Variable->State == (UINT8) (-1) ||\r | |
348 | Variable->DataSize == (UINT32) (-1) ||\r | |
349 | Variable->NameSize == (UINT32) (-1) ||\r | |
350 | Variable->Attributes == (UINT32) (-1)) {\r | |
351 | return 0;\r | |
352 | }\r | |
353 | return (UINTN) Variable->NameSize;\r | |
354 | }\r | |
355 | \r | |
356 | /**\r | |
357 | \r | |
358 | This code gets the size of variable data.\r | |
359 | \r | |
360 | @param Variable Pointer to the Variable Header.\r | |
361 | \r | |
362 | @return Size of variable in bytes.\r | |
363 | \r | |
364 | **/\r | |
365 | UINTN\r | |
366 | DataSizeOfVariable (\r | |
367 | IN VARIABLE_HEADER *Variable\r | |
368 | )\r | |
369 | {\r | |
370 | if (Variable->State == (UINT8) (-1) ||\r | |
371 | Variable->DataSize == (UINT32) (-1) ||\r | |
372 | Variable->NameSize == (UINT32) (-1) ||\r | |
373 | Variable->Attributes == (UINT32) (-1)) {\r | |
374 | return 0;\r | |
375 | }\r | |
376 | return (UINTN) Variable->DataSize;\r | |
377 | }\r | |
378 | \r | |
379 | /**\r | |
380 | \r | |
381 | This code gets the pointer to the variable name.\r | |
382 | \r | |
383 | @param Variable Pointer to the Variable Header.\r | |
384 | \r | |
385 | @return Pointer to Variable Name which is Unicode encoding.\r | |
386 | \r | |
387 | **/\r | |
388 | CHAR16 *\r | |
389 | GetVariableNamePtr (\r | |
390 | IN VARIABLE_HEADER *Variable\r | |
391 | )\r | |
392 | {\r | |
393 | \r | |
394 | return (CHAR16 *) (Variable + 1);\r | |
395 | }\r | |
396 | \r | |
397 | /**\r | |
398 | \r | |
399 | This code gets the pointer to the variable data.\r | |
400 | \r | |
401 | @param Variable Pointer to the Variable Header.\r | |
402 | \r | |
403 | @return Pointer to Variable Data.\r | |
404 | \r | |
405 | **/\r | |
406 | UINT8 *\r | |
407 | GetVariableDataPtr (\r | |
408 | IN VARIABLE_HEADER *Variable\r | |
409 | )\r | |
410 | {\r | |
411 | UINTN Value;\r | |
412 | \r | |
413 | //\r | |
414 | // Be careful about pad size for alignment.\r | |
415 | //\r | |
416 | Value = (UINTN) GetVariableNamePtr (Variable);\r | |
417 | Value += NameSizeOfVariable (Variable);\r | |
418 | Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r | |
419 | \r | |
420 | return (UINT8 *) Value;\r | |
421 | }\r | |
422 | \r | |
423 | \r | |
424 | /**\r | |
425 | \r | |
426 | This code gets the pointer to the next variable header.\r | |
427 | \r | |
428 | @param Variable Pointer to the Variable Header.\r | |
429 | \r | |
430 | @return Pointer to next variable header.\r | |
431 | \r | |
432 | **/\r | |
433 | VARIABLE_HEADER *\r | |
434 | GetNextVariablePtr (\r | |
435 | IN VARIABLE_HEADER *Variable\r | |
436 | )\r | |
437 | {\r | |
438 | UINTN Value;\r | |
439 | \r | |
440 | if (!IsValidVariableHeader (Variable)) {\r | |
441 | return NULL;\r | |
442 | }\r | |
443 | \r | |
444 | Value = (UINTN) GetVariableDataPtr (Variable);\r | |
445 | Value += DataSizeOfVariable (Variable);\r | |
446 | Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));\r | |
447 | \r | |
448 | //\r | |
449 | // Be careful about pad size for alignment.\r | |
450 | //\r | |
451 | return (VARIABLE_HEADER *) HEADER_ALIGN (Value);\r | |
452 | }\r | |
453 | \r | |
454 | /**\r | |
455 | \r | |
456 | Gets the pointer to the first variable header in given variable store area.\r | |
457 | \r | |
458 | @param VarStoreHeader Pointer to the Variable Store Header.\r | |
459 | \r | |
460 | @return Pointer to the first variable header.\r | |
461 | \r | |
462 | **/\r | |
463 | VARIABLE_HEADER *\r | |
464 | GetStartPointer (\r | |
465 | IN VARIABLE_STORE_HEADER *VarStoreHeader\r | |
466 | )\r | |
467 | {\r | |
468 | //\r | |
469 | // The end of variable store.\r | |
470 | //\r | |
471 | return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);\r | |
472 | }\r | |
473 | \r | |
474 | /**\r | |
475 | \r | |
476 | Gets the pointer to the end of the variable storage area.\r | |
477 | \r | |
478 | This function gets pointer to the end of the variable storage\r | |
479 | area, according to the input variable store header.\r | |
480 | \r | |
481 | @param VarStoreHeader Pointer to the Variable Store Header.\r | |
482 | \r | |
483 | @return Pointer to the end of the variable storage area. \r | |
484 | \r | |
485 | **/\r | |
486 | VARIABLE_HEADER *\r | |
487 | GetEndPointer (\r | |
488 | IN VARIABLE_STORE_HEADER *VarStoreHeader\r | |
489 | )\r | |
490 | {\r | |
491 | //\r | |
492 | // The end of variable store\r | |
493 | //\r | |
494 | return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r | |
495 | }\r | |
496 | \r | |
497 | \r | |
498 | /**\r | |
499 | \r | |
500 | Variable store garbage collection and reclaim operation.\r | |
501 | \r | |
502 | @param VariableBase Base address of variable store.\r | |
503 | @param LastVariableOffset Offset of last variable.\r | |
504 | @param IsVolatile The variable store is volatile or not;\r | |
505 | if it is non-volatile, need FTW.\r | |
506 | @param UpdatingVariable Pointer to updating variable.\r | |
507 | \r | |
508 | @return EFI_OUT_OF_RESOURCES\r | |
509 | @return EFI_SUCCESS\r | |
510 | @return Others\r | |
511 | \r | |
512 | **/\r | |
513 | EFI_STATUS\r | |
514 | Reclaim (\r | |
515 | IN EFI_PHYSICAL_ADDRESS VariableBase,\r | |
516 | OUT UINTN *LastVariableOffset,\r | |
517 | IN BOOLEAN IsVolatile,\r | |
518 | IN VARIABLE_HEADER *UpdatingVariable\r | |
519 | )\r | |
520 | {\r | |
521 | VARIABLE_HEADER *Variable;\r | |
522 | VARIABLE_HEADER *AddedVariable;\r | |
523 | VARIABLE_HEADER *NextVariable;\r | |
524 | VARIABLE_HEADER *NextAddedVariable;\r | |
525 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
526 | UINT8 *ValidBuffer;\r | |
527 | UINTN MaximumBufferSize;\r | |
528 | UINTN VariableSize;\r | |
529 | UINTN VariableNameSize;\r | |
530 | UINTN UpdatingVariableNameSize;\r | |
531 | UINTN NameSize;\r | |
532 | UINT8 *CurrPtr;\r | |
533 | VOID *Point0;\r | |
534 | VOID *Point1;\r | |
535 | BOOLEAN FoundAdded;\r | |
536 | EFI_STATUS Status;\r | |
537 | CHAR16 *VariableNamePtr;\r | |
538 | CHAR16 *UpdatingVariableNamePtr;\r | |
539 | \r | |
540 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);\r | |
541 | //\r | |
542 | // Recalculate the total size of Common/HwErr type variables in non-volatile area.\r | |
543 | //\r | |
544 | if (!IsVolatile) {\r | |
545 | mVariableModuleGlobal->CommonVariableTotalSize = 0;\r | |
546 | mVariableModuleGlobal->HwErrVariableTotalSize = 0;\r | |
547 | }\r | |
548 | \r | |
549 | //\r | |
550 | // Start Pointers for the variable.\r | |
551 | //\r | |
552 | Variable = GetStartPointer (VariableStoreHeader);\r | |
553 | MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);\r | |
554 | \r | |
555 | while (IsValidVariableHeader (Variable)) {\r | |
556 | NextVariable = GetNextVariablePtr (Variable);\r | |
557 | if (Variable->State == VAR_ADDED || \r | |
558 | Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r | |
559 | ) {\r | |
560 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r | |
561 | MaximumBufferSize += VariableSize;\r | |
562 | }\r | |
563 | \r | |
564 | Variable = NextVariable;\r | |
565 | }\r | |
566 | \r | |
567 | //\r | |
568 | // Reserve the 1 Bytes with Oxff to identify the \r | |
569 | // end of the variable buffer. \r | |
570 | // \r | |
571 | MaximumBufferSize += 1;\r | |
572 | ValidBuffer = AllocatePool (MaximumBufferSize);\r | |
573 | if (ValidBuffer == NULL) {\r | |
574 | return EFI_OUT_OF_RESOURCES;\r | |
575 | }\r | |
576 | \r | |
577 | SetMem (ValidBuffer, MaximumBufferSize, 0xff);\r | |
578 | \r | |
579 | //\r | |
580 | // Copy variable store header.\r | |
581 | //\r | |
582 | CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r | |
583 | CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r | |
584 | \r | |
585 | //\r | |
586 | // Reinstall all ADDED variables as long as they are not identical to Updating Variable.\r | |
587 | // \r | |
588 | Variable = GetStartPointer (VariableStoreHeader);\r | |
589 | while (IsValidVariableHeader (Variable)) {\r | |
590 | NextVariable = GetNextVariablePtr (Variable);\r | |
591 | if (Variable->State == VAR_ADDED) {\r | |
592 | if (UpdatingVariable != NULL) {\r | |
593 | if (UpdatingVariable == Variable) {\r | |
594 | Variable = NextVariable;\r | |
595 | continue;\r | |
596 | }\r | |
597 | \r | |
598 | VariableNameSize = NameSizeOfVariable(Variable);\r | |
599 | UpdatingVariableNameSize = NameSizeOfVariable(UpdatingVariable);\r | |
600 | \r | |
601 | VariableNamePtr = GetVariableNamePtr (Variable);\r | |
602 | UpdatingVariableNamePtr = GetVariableNamePtr (UpdatingVariable);\r | |
603 | if (CompareGuid (&Variable->VendorGuid, &UpdatingVariable->VendorGuid) &&\r | |
604 | VariableNameSize == UpdatingVariableNameSize &&\r | |
605 | CompareMem (VariableNamePtr, UpdatingVariableNamePtr, VariableNameSize) == 0 ) {\r | |
606 | Variable = NextVariable;\r | |
607 | continue;\r | |
608 | }\r | |
609 | }\r | |
610 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r | |
611 | CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r | |
612 | CurrPtr += VariableSize;\r | |
613 | if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r | |
614 | mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r | |
615 | } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r | |
616 | mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r | |
617 | }\r | |
618 | }\r | |
619 | Variable = NextVariable;\r | |
620 | }\r | |
621 | \r | |
622 | //\r | |
623 | // Reinstall the variable being updated if it is not NULL.\r | |
624 | //\r | |
625 | if (UpdatingVariable != NULL) {\r | |
626 | VariableSize = (UINTN)(GetNextVariablePtr (UpdatingVariable)) - (UINTN)UpdatingVariable;\r | |
627 | CopyMem (CurrPtr, (UINT8 *) UpdatingVariable, VariableSize);\r | |
628 | CurrPtr += VariableSize;\r | |
629 | if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r | |
630 | mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r | |
631 | } else if ((!IsVolatile) && ((UpdatingVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r | |
632 | mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r | |
633 | }\r | |
634 | }\r | |
635 | \r | |
636 | //\r | |
637 | // Reinstall all in delete transition variables.\r | |
638 | // \r | |
639 | Variable = GetStartPointer (VariableStoreHeader);\r | |
640 | while (IsValidVariableHeader (Variable)) {\r | |
641 | NextVariable = GetNextVariablePtr (Variable);\r | |
642 | if (Variable != UpdatingVariable && Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r | |
643 | \r | |
644 | //\r | |
645 | // Buffer has cached all ADDED variable. \r | |
646 | // Per IN_DELETED variable, we have to guarantee that\r | |
647 | // no ADDED one in previous buffer. \r | |
648 | // \r | |
649 | \r | |
650 | FoundAdded = FALSE;\r | |
651 | AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r | |
652 | while (IsValidVariableHeader (AddedVariable)) {\r | |
653 | NextAddedVariable = GetNextVariablePtr (AddedVariable);\r | |
654 | NameSize = NameSizeOfVariable (AddedVariable);\r | |
655 | if (CompareGuid (&AddedVariable->VendorGuid, &Variable->VendorGuid) &&\r | |
656 | NameSize == NameSizeOfVariable (Variable)\r | |
657 | ) {\r | |
658 | Point0 = (VOID *) GetVariableNamePtr (AddedVariable);\r | |
659 | Point1 = (VOID *) GetVariableNamePtr (Variable);\r | |
660 | if (CompareMem (Point0, Point1, NameSizeOfVariable (AddedVariable)) == 0) {\r | |
661 | FoundAdded = TRUE;\r | |
662 | break;\r | |
663 | }\r | |
664 | }\r | |
665 | AddedVariable = NextAddedVariable;\r | |
666 | }\r | |
667 | if (!FoundAdded) {\r | |
668 | //\r | |
669 | // Promote VAR_IN_DELETED_TRANSITION to VAR_ADDED.\r | |
670 | //\r | |
671 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r | |
672 | CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r | |
673 | ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r | |
674 | CurrPtr += VariableSize;\r | |
675 | if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r | |
676 | mVariableModuleGlobal->HwErrVariableTotalSize += VariableSize;\r | |
677 | } else if ((!IsVolatile) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r | |
678 | mVariableModuleGlobal->CommonVariableTotalSize += VariableSize;\r | |
679 | }\r | |
680 | }\r | |
681 | }\r | |
682 | \r | |
683 | Variable = NextVariable;\r | |
684 | }\r | |
685 | \r | |
686 | if (IsVolatile) {\r | |
687 | //\r | |
688 | // If volatile variable store, just copy valid buffer.\r | |
689 | //\r | |
690 | SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);\r | |
691 | CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - (UINT8 *) ValidBuffer));\r | |
692 | Status = EFI_SUCCESS;\r | |
693 | } else {\r | |
694 | //\r | |
695 | // If non-volatile variable store, perform FTW here.\r | |
696 | //\r | |
697 | Status = FtwVariableSpace (\r | |
698 | VariableBase,\r | |
699 | ValidBuffer,\r | |
700 | (UINTN) (CurrPtr - (UINT8 *) ValidBuffer)\r | |
701 | );\r | |
702 | CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableBase, VariableStoreHeader->Size);\r | |
703 | }\r | |
704 | if (!EFI_ERROR (Status)) {\r | |
705 | *LastVariableOffset = (UINTN) (CurrPtr - (UINT8 *) ValidBuffer);\r | |
706 | } else {\r | |
707 | *LastVariableOffset = 0;\r | |
708 | }\r | |
709 | \r | |
710 | FreePool (ValidBuffer);\r | |
711 | \r | |
712 | return Status;\r | |
713 | }\r | |
714 | \r | |
715 | \r | |
716 | /**\r | |
717 | Finds variable in storage blocks of volatile and non-volatile storage areas.\r | |
718 | \r | |
719 | This code finds variable in storage blocks of volatile and non-volatile storage areas.\r | |
720 | If VariableName is an empty string, then we just return the first\r | |
721 | qualified variable without comparing VariableName and VendorGuid.\r | |
722 | Otherwise, VariableName and VendorGuid are compared.\r | |
723 | \r | |
724 | @param VariableName Name of the variable to be found.\r | |
725 | @param VendorGuid Vendor GUID to be found.\r | |
726 | @param PtrTrack VARIABLE_POINTER_TRACK structure for output,\r | |
727 | including the range searched and the target position.\r | |
728 | @param Global Pointer to VARIABLE_GLOBAL structure, including\r | |
729 | base of volatile variable storage area, base of\r | |
730 | NV variable storage area, and a lock.\r | |
731 | \r | |
732 | @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while\r | |
733 | VendorGuid is NULL.\r | |
734 | @retval EFI_SUCCESS Variable successfully found.\r | |
735 | @retval EFI_NOT_FOUND Variable not found\r | |
736 | \r | |
737 | **/\r | |
738 | EFI_STATUS\r | |
739 | FindVariable (\r | |
740 | IN CHAR16 *VariableName,\r | |
741 | IN EFI_GUID *VendorGuid,\r | |
742 | OUT VARIABLE_POINTER_TRACK *PtrTrack,\r | |
743 | IN VARIABLE_GLOBAL *Global\r | |
744 | )\r | |
745 | {\r | |
746 | VARIABLE_HEADER *Variable[2];\r | |
747 | VARIABLE_HEADER *InDeletedVariable;\r | |
748 | VARIABLE_STORE_HEADER *VariableStoreHeader[2];\r | |
749 | UINTN InDeletedStorageIndex;\r | |
750 | UINTN Index;\r | |
751 | VOID *Point;\r | |
752 | \r | |
753 | //\r | |
754 | // 0: Volatile, 1: Non-Volatile.\r | |
755 | // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName\r | |
756 | // make use of this mapping to implement search algorithm.\r | |
757 | //\r | |
758 | VariableStoreHeader[0] = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r | |
759 | VariableStoreHeader[1] = mNvVariableCache;\r | |
760 | \r | |
761 | //\r | |
762 | // Start Pointers for the variable.\r | |
763 | // Actual Data Pointer where data can be written.\r | |
764 | //\r | |
765 | Variable[0] = GetStartPointer (VariableStoreHeader[0]);\r | |
766 | Variable[1] = GetStartPointer (VariableStoreHeader[1]);\r | |
767 | \r | |
768 | if (VariableName[0] != 0 && VendorGuid == NULL) {\r | |
769 | return EFI_INVALID_PARAMETER;\r | |
770 | }\r | |
771 | \r | |
772 | //\r | |
773 | // Find the variable by walk through volatile and then non-volatile variable store.\r | |
774 | //\r | |
775 | InDeletedVariable = NULL;\r | |
776 | InDeletedStorageIndex = 0;\r | |
777 | for (Index = 0; Index < 2; Index++) {\r | |
778 | while ((Variable[Index] < GetEndPointer (VariableStoreHeader[Index])) && IsValidVariableHeader (Variable[Index])) {\r | |
779 | if (Variable[Index]->State == VAR_ADDED || \r | |
780 | Variable[Index]->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r | |
781 | ) {\r | |
782 | if (!AtRuntime () || ((Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) != 0)) {\r | |
783 | if (VariableName[0] == 0) {\r | |
784 | if (Variable[Index]->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r | |
785 | InDeletedVariable = Variable[Index];\r | |
786 | InDeletedStorageIndex = Index;\r | |
787 | } else {\r | |
788 | PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Index]);\r | |
789 | PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);\r | |
790 | PtrTrack->CurrPtr = Variable[Index];\r | |
791 | PtrTrack->Volatile = (BOOLEAN)(Index == 0);\r | |
792 | \r | |
793 | return EFI_SUCCESS;\r | |
794 | }\r | |
795 | } else {\r | |
796 | if (CompareGuid (VendorGuid, &Variable[Index]->VendorGuid)) {\r | |
797 | Point = (VOID *) GetVariableNamePtr (Variable[Index]);\r | |
798 | \r | |
799 | ASSERT (NameSizeOfVariable (Variable[Index]) != 0);\r | |
800 | if (CompareMem (VariableName, Point, NameSizeOfVariable (Variable[Index])) == 0) {\r | |
801 | if (Variable[Index]->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r | |
802 | InDeletedVariable = Variable[Index];\r | |
803 | InDeletedStorageIndex = Index;\r | |
804 | } else {\r | |
805 | PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Index]);\r | |
806 | PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);\r | |
807 | PtrTrack->CurrPtr = Variable[Index];\r | |
808 | PtrTrack->Volatile = (BOOLEAN)(Index == 0);\r | |
809 | \r | |
810 | return EFI_SUCCESS;\r | |
811 | }\r | |
812 | }\r | |
813 | }\r | |
814 | }\r | |
815 | }\r | |
816 | }\r | |
817 | \r | |
818 | Variable[Index] = GetNextVariablePtr (Variable[Index]);\r | |
819 | }\r | |
820 | if (InDeletedVariable != NULL) {\r | |
821 | PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[InDeletedStorageIndex]);\r | |
822 | PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[InDeletedStorageIndex]);\r | |
823 | PtrTrack->CurrPtr = InDeletedVariable;\r | |
824 | PtrTrack->Volatile = (BOOLEAN)(InDeletedStorageIndex == 0);\r | |
825 | return EFI_SUCCESS;\r | |
826 | }\r | |
827 | }\r | |
828 | PtrTrack->CurrPtr = NULL;\r | |
829 | return EFI_NOT_FOUND;\r | |
830 | }\r | |
831 | \r | |
832 | /**\r | |
833 | Get index from supported language codes according to language string.\r | |
834 | \r | |
835 | This code is used to get corresponding index in supported language codes. It can handle\r | |
836 | RFC4646 and ISO639 language tags.\r | |
837 | In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.\r | |
838 | In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.\r | |
839 | \r | |
840 | For example:\r | |
841 | SupportedLang = "engfraengfra"\r | |
842 | Lang = "eng"\r | |
843 | Iso639Language = TRUE\r | |
844 | The return value is "0".\r | |
845 | Another example:\r | |
846 | SupportedLang = "en;fr;en-US;fr-FR"\r | |
847 | Lang = "fr-FR"\r | |
848 | Iso639Language = FALSE\r | |
849 | The return value is "3".\r | |
850 | \r | |
851 | @param SupportedLang Platform supported language codes.\r | |
852 | @param Lang Configured language.\r | |
853 | @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r | |
854 | \r | |
855 | @retval The index of language in the language codes.\r | |
856 | \r | |
857 | **/\r | |
858 | UINTN\r | |
859 | GetIndexFromSupportedLangCodes(\r | |
860 | IN CHAR8 *SupportedLang,\r | |
861 | IN CHAR8 *Lang,\r | |
862 | IN BOOLEAN Iso639Language\r | |
863 | ) \r | |
864 | {\r | |
865 | UINTN Index;\r | |
866 | UINTN CompareLength;\r | |
867 | UINTN LanguageLength;\r | |
868 | \r | |
869 | if (Iso639Language) {\r | |
870 | CompareLength = ISO_639_2_ENTRY_SIZE;\r | |
871 | for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r | |
872 | if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r | |
873 | //\r | |
874 | // Successfully find the index of Lang string in SupportedLang string.\r | |
875 | //\r | |
876 | Index = Index / CompareLength;\r | |
877 | return Index;\r | |
878 | }\r | |
879 | }\r | |
880 | ASSERT (FALSE);\r | |
881 | return 0;\r | |
882 | } else {\r | |
883 | //\r | |
884 | // Compare RFC4646 language code\r | |
885 | //\r | |
886 | Index = 0;\r | |
887 | for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);\r | |
888 | \r | |
889 | for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {\r | |
890 | //\r | |
891 | // Skip ';' characters in SupportedLang\r | |
892 | //\r | |
893 | for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);\r | |
894 | //\r | |
895 | // Determine the length of the next language code in SupportedLang\r | |
896 | //\r | |
897 | for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);\r | |
898 | \r | |
899 | if ((CompareLength == LanguageLength) && \r | |
900 | (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {\r | |
901 | //\r | |
902 | // Successfully find the index of Lang string in SupportedLang string.\r | |
903 | //\r | |
904 | return Index;\r | |
905 | }\r | |
906 | }\r | |
907 | ASSERT (FALSE);\r | |
908 | return 0;\r | |
909 | }\r | |
910 | }\r | |
911 | \r | |
912 | /**\r | |
913 | Get language string from supported language codes according to index.\r | |
914 | \r | |
915 | This code is used to get corresponding language strings in supported language codes. It can handle\r | |
916 | RFC4646 and ISO639 language tags.\r | |
917 | In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.\r | |
918 | In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.\r | |
919 | \r | |
920 | For example:\r | |
921 | SupportedLang = "engfraengfra"\r | |
922 | Index = "1"\r | |
923 | Iso639Language = TRUE\r | |
924 | The return value is "fra".\r | |
925 | Another example:\r | |
926 | SupportedLang = "en;fr;en-US;fr-FR"\r | |
927 | Index = "1"\r | |
928 | Iso639Language = FALSE\r | |
929 | The return value is "fr".\r | |
930 | \r | |
931 | @param SupportedLang Platform supported language codes.\r | |
932 | @param Index The index in supported language codes.\r | |
933 | @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r | |
934 | \r | |
935 | @retval The language string in the language codes.\r | |
936 | \r | |
937 | **/\r | |
938 | CHAR8 *\r | |
939 | GetLangFromSupportedLangCodes (\r | |
940 | IN CHAR8 *SupportedLang,\r | |
941 | IN UINTN Index,\r | |
942 | IN BOOLEAN Iso639Language\r | |
943 | )\r | |
944 | {\r | |
945 | UINTN SubIndex;\r | |
946 | UINTN CompareLength;\r | |
947 | CHAR8 *Supported;\r | |
948 | \r | |
949 | SubIndex = 0;\r | |
950 | Supported = SupportedLang;\r | |
951 | if (Iso639Language) {\r | |
952 | //\r | |
953 | // According to the index of Lang string in SupportedLang string to get the language.\r | |
954 | // This code will be invoked in RUNTIME, therefore there is not a memory allocate/free operation.\r | |
955 | // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r | |
956 | //\r | |
957 | CompareLength = ISO_639_2_ENTRY_SIZE;\r | |
958 | mVariableModuleGlobal->Lang[CompareLength] = '\0';\r | |
959 | return CopyMem (mVariableModuleGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);\r | |
960 | \r | |
961 | } else {\r | |
962 | while (TRUE) {\r | |
963 | //\r | |
964 | // Take semicolon as delimitation, sequentially traverse supported language codes.\r | |
965 | //\r | |
966 | for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r | |
967 | Supported++;\r | |
968 | }\r | |
969 | if ((*Supported == '\0') && (SubIndex != Index)) {\r | |
970 | //\r | |
971 | // Have completed the traverse, but not find corrsponding string.\r | |
972 | // This case is not allowed to happen.\r | |
973 | //\r | |
974 | ASSERT(FALSE);\r | |
975 | return NULL;\r | |
976 | }\r | |
977 | if (SubIndex == Index) {\r | |
978 | //\r | |
979 | // According to the index of Lang string in SupportedLang string to get the language.\r | |
980 | // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r | |
981 | // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r | |
982 | //\r | |
983 | mVariableModuleGlobal->PlatformLang[CompareLength] = '\0';\r | |
984 | return CopyMem (mVariableModuleGlobal->PlatformLang, Supported - CompareLength, CompareLength);\r | |
985 | }\r | |
986 | SubIndex++;\r | |
987 | \r | |
988 | //\r | |
989 | // Skip ';' characters in Supported\r | |
990 | //\r | |
991 | for (; *Supported != '\0' && *Supported == ';'; Supported++);\r | |
992 | }\r | |
993 | }\r | |
994 | }\r | |
995 | \r | |
996 | /**\r | |
997 | Returns a pointer to an allocated buffer that contains the best matching language \r | |
998 | from a set of supported languages. \r | |
999 | \r | |
1000 | This function supports both ISO 639-2 and RFC 4646 language codes, but language \r | |
1001 | code types may not be mixed in a single call to this function. This function\r | |
1002 | supports a variable argument list that allows the caller to pass in a prioritized\r | |
1003 | list of language codes to test against all the language codes in SupportedLanguages.\r | |
1004 | \r | |
1005 | If SupportedLanguages is NULL, then ASSERT().\r | |
1006 | \r | |
1007 | @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r | |
1008 | contains a set of language codes in the format \r | |
1009 | specified by Iso639Language.\r | |
1010 | @param[in] Iso639Language If TRUE, then all language codes are assumed to be\r | |
1011 | in ISO 639-2 format. If FALSE, then all language\r | |
1012 | codes are assumed to be in RFC 4646 language format\r | |
1013 | @param[in] ... A variable argument list that contains pointers to \r | |
1014 | Null-terminated ASCII strings that contain one or more\r | |
1015 | language codes in the format specified by Iso639Language.\r | |
1016 | The first language code from each of these language\r | |
1017 | code lists is used to determine if it is an exact or\r | |
1018 | close match to any of the language codes in \r | |
1019 | SupportedLanguages. Close matches only apply to RFC 4646\r | |
1020 | language codes, and the matching algorithm from RFC 4647\r | |
1021 | is used to determine if a close match is present. If \r | |
1022 | an exact or close match is found, then the matching\r | |
1023 | language code from SupportedLanguages is returned. If\r | |
1024 | no matches are found, then the next variable argument\r | |
1025 | parameter is evaluated. The variable argument list \r | |
1026 | is terminated by a NULL.\r | |
1027 | \r | |
1028 | @retval NULL The best matching language could not be found in SupportedLanguages.\r | |
1029 | @retval NULL There are not enough resources available to return the best matching \r | |
1030 | language.\r | |
1031 | @retval Other A pointer to a Null-terminated ASCII string that is the best matching \r | |
1032 | language in SupportedLanguages.\r | |
1033 | \r | |
1034 | **/\r | |
1035 | CHAR8 *\r | |
1036 | EFIAPI\r | |
1037 | VariableGetBestLanguage (\r | |
1038 | IN CONST CHAR8 *SupportedLanguages, \r | |
1039 | IN BOOLEAN Iso639Language,\r | |
1040 | ...\r | |
1041 | )\r | |
1042 | {\r | |
1043 | VA_LIST Args;\r | |
1044 | CHAR8 *Language;\r | |
1045 | UINTN CompareLength;\r | |
1046 | UINTN LanguageLength;\r | |
1047 | CONST CHAR8 *Supported;\r | |
1048 | CHAR8 *Buffer;\r | |
1049 | \r | |
1050 | ASSERT (SupportedLanguages != NULL);\r | |
1051 | \r | |
1052 | VA_START (Args, Iso639Language);\r | |
1053 | while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r | |
1054 | //\r | |
1055 | // Default to ISO 639-2 mode\r | |
1056 | //\r | |
1057 | CompareLength = 3;\r | |
1058 | LanguageLength = MIN (3, AsciiStrLen (Language));\r | |
1059 | \r | |
1060 | //\r | |
1061 | // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r | |
1062 | //\r | |
1063 | if (!Iso639Language) {\r | |
1064 | for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r | |
1065 | }\r | |
1066 | \r | |
1067 | //\r | |
1068 | // Trim back the length of Language used until it is empty\r | |
1069 | //\r | |
1070 | while (LanguageLength > 0) {\r | |
1071 | //\r | |
1072 | // Loop through all language codes in SupportedLanguages\r | |
1073 | //\r | |
1074 | for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r | |
1075 | //\r | |
1076 | // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r | |
1077 | //\r | |
1078 | if (!Iso639Language) {\r | |
1079 | //\r | |
1080 | // Skip ';' characters in Supported\r | |
1081 | //\r | |
1082 | for (; *Supported != '\0' && *Supported == ';'; Supported++);\r | |
1083 | //\r | |
1084 | // Determine the length of the next language code in Supported\r | |
1085 | //\r | |
1086 | for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r | |
1087 | //\r | |
1088 | // If Language is longer than the Supported, then skip to the next language\r | |
1089 | //\r | |
1090 | if (LanguageLength > CompareLength) {\r | |
1091 | continue;\r | |
1092 | }\r | |
1093 | }\r | |
1094 | //\r | |
1095 | // See if the first LanguageLength characters in Supported match Language\r | |
1096 | //\r | |
1097 | if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r | |
1098 | VA_END (Args);\r | |
1099 | \r | |
1100 | Buffer = Iso639Language ? mVariableModuleGlobal->Lang : mVariableModuleGlobal->PlatformLang;\r | |
1101 | Buffer[CompareLength] = '\0';\r | |
1102 | return CopyMem (Buffer, Supported, CompareLength);\r | |
1103 | }\r | |
1104 | }\r | |
1105 | \r | |
1106 | if (Iso639Language) {\r | |
1107 | //\r | |
1108 | // If ISO 639 mode, then each language can only be tested once\r | |
1109 | //\r | |
1110 | LanguageLength = 0;\r | |
1111 | } else {\r | |
1112 | //\r | |
1113 | // If RFC 4646 mode, then trim Language from the right to the next '-' character \r | |
1114 | //\r | |
1115 | for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r | |
1116 | }\r | |
1117 | }\r | |
1118 | }\r | |
1119 | VA_END (Args);\r | |
1120 | \r | |
1121 | //\r | |
1122 | // No matches were found \r | |
1123 | //\r | |
1124 | return NULL;\r | |
1125 | }\r | |
1126 | \r | |
1127 | /**\r | |
1128 | Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r | |
1129 | \r | |
1130 | When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r | |
1131 | \r | |
1132 | According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r | |
1133 | and are read-only. Therefore, in variable driver, only store the original value for other use.\r | |
1134 | \r | |
1135 | @param[in] VariableName Name of variable.\r | |
1136 | \r | |
1137 | @param[in] Data Variable data.\r | |
1138 | \r | |
1139 | @param[in] DataSize Size of data. 0 means delete.\r | |
1140 | \r | |
1141 | **/\r | |
1142 | VOID\r | |
1143 | AutoUpdateLangVariable(\r | |
1144 | IN CHAR16 *VariableName,\r | |
1145 | IN VOID *Data,\r | |
1146 | IN UINTN DataSize\r | |
1147 | )\r | |
1148 | {\r | |
1149 | EFI_STATUS Status;\r | |
1150 | CHAR8 *BestPlatformLang;\r | |
1151 | CHAR8 *BestLang;\r | |
1152 | UINTN Index;\r | |
1153 | UINT32 Attributes;\r | |
1154 | VARIABLE_POINTER_TRACK Variable;\r | |
1155 | BOOLEAN SetLanguageCodes;\r | |
1156 | \r | |
1157 | //\r | |
1158 | // Don't do updates for delete operation\r | |
1159 | //\r | |
1160 | if (DataSize == 0) {\r | |
1161 | return;\r | |
1162 | }\r | |
1163 | \r | |
1164 | SetLanguageCodes = FALSE;\r | |
1165 | \r | |
1166 | if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {\r | |
1167 | //\r | |
1168 | // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r | |
1169 | //\r | |
1170 | if (AtRuntime ()) {\r | |
1171 | return;\r | |
1172 | }\r | |
1173 | \r | |
1174 | SetLanguageCodes = TRUE;\r | |
1175 | \r | |
1176 | //\r | |
1177 | // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r | |
1178 | // Therefore, in variable driver, only store the original value for other use.\r | |
1179 | //\r | |
1180 | if (mVariableModuleGlobal->PlatformLangCodes != NULL) {\r | |
1181 | FreePool (mVariableModuleGlobal->PlatformLangCodes);\r | |
1182 | }\r | |
1183 | mVariableModuleGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r | |
1184 | ASSERT (mVariableModuleGlobal->PlatformLangCodes != NULL);\r | |
1185 | \r | |
1186 | //\r | |
1187 | // PlatformLang holds a single language from PlatformLangCodes, \r | |
1188 | // so the size of PlatformLangCodes is enough for the PlatformLang.\r | |
1189 | //\r | |
1190 | if (mVariableModuleGlobal->PlatformLang != NULL) {\r | |
1191 | FreePool (mVariableModuleGlobal->PlatformLang);\r | |
1192 | }\r | |
1193 | mVariableModuleGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r | |
1194 | ASSERT (mVariableModuleGlobal->PlatformLang != NULL);\r | |
1195 | \r | |
1196 | } else if (StrCmp (VariableName, L"LangCodes") == 0) {\r | |
1197 | //\r | |
1198 | // LangCodes is a volatile variable, so it can not be updated at runtime.\r | |
1199 | //\r | |
1200 | if (AtRuntime ()) {\r | |
1201 | return;\r | |
1202 | }\r | |
1203 | \r | |
1204 | SetLanguageCodes = TRUE;\r | |
1205 | \r | |
1206 | //\r | |
1207 | // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r | |
1208 | // Therefore, in variable driver, only store the original value for other use.\r | |
1209 | //\r | |
1210 | if (mVariableModuleGlobal->LangCodes != NULL) {\r | |
1211 | FreePool (mVariableModuleGlobal->LangCodes);\r | |
1212 | }\r | |
1213 | mVariableModuleGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r | |
1214 | ASSERT (mVariableModuleGlobal->LangCodes != NULL);\r | |
1215 | }\r | |
1216 | \r | |
1217 | if (SetLanguageCodes \r | |
1218 | && (mVariableModuleGlobal->PlatformLangCodes != NULL)\r | |
1219 | && (mVariableModuleGlobal->LangCodes != NULL)) {\r | |
1220 | //\r | |
1221 | // Update Lang if PlatformLang is already set\r | |
1222 | // Update PlatformLang if Lang is already set\r | |
1223 | //\r | |
1224 | Status = FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *) mVariableModuleGlobal);\r | |
1225 | if (!EFI_ERROR (Status)) {\r | |
1226 | //\r | |
1227 | // Update Lang\r | |
1228 | //\r | |
1229 | VariableName = L"PlatformLang";\r | |
1230 | Data = GetVariableDataPtr (Variable.CurrPtr);\r | |
1231 | DataSize = Variable.CurrPtr->DataSize;\r | |
1232 | } else {\r | |
1233 | Status = FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *) mVariableModuleGlobal);\r | |
1234 | if (!EFI_ERROR (Status)) {\r | |
1235 | //\r | |
1236 | // Update PlatformLang\r | |
1237 | //\r | |
1238 | VariableName = L"Lang";\r | |
1239 | Data = GetVariableDataPtr (Variable.CurrPtr);\r | |
1240 | DataSize = Variable.CurrPtr->DataSize;\r | |
1241 | } else {\r | |
1242 | //\r | |
1243 | // Neither PlatformLang nor Lang is set, directly return\r | |
1244 | //\r | |
1245 | return;\r | |
1246 | }\r | |
1247 | }\r | |
1248 | }\r | |
1249 | \r | |
1250 | //\r | |
1251 | // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r | |
1252 | //\r | |
1253 | Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r | |
1254 | \r | |
1255 | if (StrCmp (VariableName, L"PlatformLang") == 0) {\r | |
1256 | //\r | |
1257 | // Update Lang when PlatformLangCodes/LangCodes were set.\r | |
1258 | //\r | |
1259 | if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r | |
1260 | //\r | |
1261 | // When setting PlatformLang, firstly get most matched language string from supported language codes.\r | |
1262 | //\r | |
1263 | BestPlatformLang = VariableGetBestLanguage (mVariableModuleGlobal->PlatformLangCodes, FALSE, Data, NULL);\r | |
1264 | if (BestPlatformLang != NULL) {\r | |
1265 | //\r | |
1266 | // Get the corresponding index in language codes.\r | |
1267 | //\r | |
1268 | Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r | |
1269 | \r | |
1270 | //\r | |
1271 | // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r | |
1272 | //\r | |
1273 | BestLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, Index, TRUE);\r | |
1274 | \r | |
1275 | //\r | |
1276 | // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r | |
1277 | //\r | |
1278 | FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);\r | |
1279 | \r | |
1280 | Status = UpdateVariable (L"Lang", &gEfiGlobalVariableGuid, BestLang,\r | |
1281 | ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);\r | |
1282 | \r | |
1283 | DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));\r | |
1284 | \r | |
1285 | ASSERT_EFI_ERROR(Status);\r | |
1286 | }\r | |
1287 | }\r | |
1288 | \r | |
1289 | } else if (StrCmp (VariableName, L"Lang") == 0) {\r | |
1290 | //\r | |
1291 | // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r | |
1292 | //\r | |
1293 | if ((mVariableModuleGlobal->PlatformLangCodes != NULL) && (mVariableModuleGlobal->LangCodes != NULL)) {\r | |
1294 | //\r | |
1295 | // When setting Lang, firstly get most matched language string from supported language codes.\r | |
1296 | //\r | |
1297 | BestLang = VariableGetBestLanguage (mVariableModuleGlobal->LangCodes, TRUE, Data, NULL);\r | |
1298 | if (BestLang != NULL) {\r | |
1299 | //\r | |
1300 | // Get the corresponding index in language codes.\r | |
1301 | //\r | |
1302 | Index = GetIndexFromSupportedLangCodes (mVariableModuleGlobal->LangCodes, BestLang, TRUE);\r | |
1303 | \r | |
1304 | //\r | |
1305 | // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r | |
1306 | //\r | |
1307 | BestPlatformLang = GetLangFromSupportedLangCodes (mVariableModuleGlobal->PlatformLangCodes, Index, FALSE);\r | |
1308 | \r | |
1309 | //\r | |
1310 | // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r | |
1311 | //\r | |
1312 | FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable, (VARIABLE_GLOBAL *)mVariableModuleGlobal);\r | |
1313 | \r | |
1314 | Status = UpdateVariable (L"PlatformLang", &gEfiGlobalVariableGuid, BestPlatformLang, \r | |
1315 | AsciiStrSize (BestPlatformLang), Attributes, &Variable);\r | |
1316 | \r | |
1317 | DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));\r | |
1318 | ASSERT_EFI_ERROR (Status);\r | |
1319 | }\r | |
1320 | }\r | |
1321 | }\r | |
1322 | }\r | |
1323 | \r | |
1324 | /**\r | |
1325 | Update the variable region with Variable information. These are the same \r | |
1326 | arguments as the EFI Variable services.\r | |
1327 | \r | |
1328 | @param[in] VariableName Name of variable.\r | |
1329 | @param[in] VendorGuid Guid of variable.\r | |
1330 | @param[in] Data Variable data.\r | |
1331 | @param[in] DataSize Size of data. 0 means delete.\r | |
1332 | @param[in] Attributes Attribues of the variable.\r | |
1333 | @param[in] CacheVariable The variable information which is used to keep track of variable usage.\r | |
1334 | \r | |
1335 | @retval EFI_SUCCESS The update operation is success.\r | |
1336 | @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r | |
1337 | \r | |
1338 | **/\r | |
1339 | EFI_STATUS\r | |
1340 | UpdateVariable (\r | |
1341 | IN CHAR16 *VariableName,\r | |
1342 | IN EFI_GUID *VendorGuid,\r | |
1343 | IN VOID *Data,\r | |
1344 | IN UINTN DataSize,\r | |
1345 | IN UINT32 Attributes OPTIONAL,\r | |
1346 | IN VARIABLE_POINTER_TRACK *CacheVariable\r | |
1347 | )\r | |
1348 | {\r | |
1349 | EFI_STATUS Status;\r | |
1350 | VARIABLE_HEADER *NextVariable;\r | |
1351 | UINTN ScratchSize;\r | |
1352 | UINTN NonVolatileVarableStoreSize;\r | |
1353 | UINTN VarNameOffset;\r | |
1354 | UINTN VarDataOffset;\r | |
1355 | UINTN VarNameSize;\r | |
1356 | UINTN VarSize;\r | |
1357 | BOOLEAN Volatile;\r | |
1358 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r | |
1359 | UINT8 State;\r | |
1360 | BOOLEAN Reclaimed;\r | |
1361 | VARIABLE_POINTER_TRACK *Variable;\r | |
1362 | VARIABLE_POINTER_TRACK NvVariable;\r | |
1363 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
1364 | UINTN CacheOffset;\r | |
1365 | \r | |
1366 | if ((mVariableModuleGlobal->FvbInstance == NULL) && ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0)) {\r | |
1367 | //\r | |
1368 | // The FVB protocol is not ready. Trying to update NV variable prior to the installation\r | |
1369 | // of EFI_VARIABLE_WRITE_ARCH_PROTOCOL.\r | |
1370 | //\r | |
1371 | return EFI_NOT_AVAILABLE_YET; \r | |
1372 | }\r | |
1373 | \r | |
1374 | if ((CacheVariable->CurrPtr == NULL) || CacheVariable->Volatile) {\r | |
1375 | Variable = CacheVariable;\r | |
1376 | } else {\r | |
1377 | //\r | |
1378 | // Update/Delete existing NV variable.\r | |
1379 | // CacheVariable points to the variable in the memory copy of Flash area\r | |
1380 | // Now let Variable points to the same variable in Flash area.\r | |
1381 | //\r | |
1382 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r | |
1383 | Variable = &NvVariable; \r | |
1384 | Variable->StartPtr = GetStartPointer (VariableStoreHeader);\r | |
1385 | Variable->EndPtr = GetEndPointer (VariableStoreHeader);\r | |
1386 | Variable->CurrPtr = (VARIABLE_HEADER *)((UINTN)Variable->StartPtr + ((UINTN)CacheVariable->CurrPtr - (UINTN)CacheVariable->StartPtr));\r | |
1387 | Variable->Volatile = FALSE;\r | |
1388 | } \r | |
1389 | \r | |
1390 | Fvb = mVariableModuleGlobal->FvbInstance;\r | |
1391 | Reclaimed = FALSE;\r | |
1392 | \r | |
1393 | if (Variable->CurrPtr != NULL) {\r | |
1394 | //\r | |
1395 | // Update/Delete existing variable.\r | |
1396 | //\r | |
1397 | if (AtRuntime ()) { \r | |
1398 | //\r | |
1399 | // If AtRuntime and the variable is Volatile and Runtime Access, \r | |
1400 | // the volatile is ReadOnly, and SetVariable should be aborted and \r | |
1401 | // return EFI_WRITE_PROTECTED.\r | |
1402 | //\r | |
1403 | if (Variable->Volatile) {\r | |
1404 | Status = EFI_WRITE_PROTECTED;\r | |
1405 | goto Done;\r | |
1406 | }\r | |
1407 | //\r | |
1408 | // Only variable that have NV attributes can be updated/deleted in Runtime.\r | |
1409 | //\r | |
1410 | if ((Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r | |
1411 | Status = EFI_INVALID_PARAMETER;\r | |
1412 | goto Done; \r | |
1413 | }\r | |
1414 | }\r | |
1415 | \r | |
1416 | //\r | |
1417 | // Setting a data variable with no access, or zero DataSize attributes\r | |
1418 | // causes it to be deleted.\r | |
1419 | //\r | |
1420 | if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) { \r | |
1421 | State = Variable->CurrPtr->State;\r | |
1422 | State &= VAR_DELETED;\r | |
1423 | \r | |
1424 | Status = UpdateVariableStore (\r | |
1425 | &mVariableModuleGlobal->VariableGlobal,\r | |
1426 | Variable->Volatile,\r | |
1427 | FALSE,\r | |
1428 | Fvb,\r | |
1429 | (UINTN) &Variable->CurrPtr->State,\r | |
1430 | sizeof (UINT8),\r | |
1431 | &State\r | |
1432 | ); \r | |
1433 | if (!EFI_ERROR (Status)) {\r | |
1434 | UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, FALSE, TRUE, FALSE);\r | |
1435 | if (!Variable->Volatile) {\r | |
1436 | CacheVariable->CurrPtr->State = State;\r | |
1437 | }\r | |
1438 | }\r | |
1439 | goto Done; \r | |
1440 | }\r | |
1441 | //\r | |
1442 | // If the variable is marked valid, and the same data has been passed in,\r | |
1443 | // then return to the caller immediately.\r | |
1444 | //\r | |
1445 | if (DataSizeOfVariable (Variable->CurrPtr) == DataSize &&\r | |
1446 | (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)) {\r | |
1447 | \r | |
1448 | UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FALSE, TRUE, FALSE, FALSE);\r | |
1449 | Status = EFI_SUCCESS;\r | |
1450 | goto Done;\r | |
1451 | } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r | |
1452 | (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r | |
1453 | \r | |
1454 | //\r | |
1455 | // Mark the old variable as in delete transition.\r | |
1456 | //\r | |
1457 | State = Variable->CurrPtr->State;\r | |
1458 | State &= VAR_IN_DELETED_TRANSITION;\r | |
1459 | \r | |
1460 | Status = UpdateVariableStore (\r | |
1461 | &mVariableModuleGlobal->VariableGlobal,\r | |
1462 | Variable->Volatile,\r | |
1463 | FALSE,\r | |
1464 | Fvb,\r | |
1465 | (UINTN) &Variable->CurrPtr->State,\r | |
1466 | sizeof (UINT8),\r | |
1467 | &State\r | |
1468 | ); \r | |
1469 | if (EFI_ERROR (Status)) {\r | |
1470 | goto Done; \r | |
1471 | } \r | |
1472 | if (!Variable->Volatile) {\r | |
1473 | CacheVariable->CurrPtr->State = State;\r | |
1474 | }\r | |
1475 | } \r | |
1476 | } else {\r | |
1477 | //\r | |
1478 | // Not found existing variable. Create a new variable.\r | |
1479 | // \r | |
1480 | \r | |
1481 | //\r | |
1482 | // Make sure we are trying to create a new variable.\r | |
1483 | // Setting a data variable with zero DataSize or no access attributes means to delete it. \r | |
1484 | //\r | |
1485 | if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r | |
1486 | Status = EFI_NOT_FOUND;\r | |
1487 | goto Done;\r | |
1488 | }\r | |
1489 | \r | |
1490 | //\r | |
1491 | // Only variable have NV|RT attribute can be created in Runtime.\r | |
1492 | //\r | |
1493 | if (AtRuntime () &&\r | |
1494 | (((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0) || ((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0))) {\r | |
1495 | Status = EFI_INVALID_PARAMETER;\r | |
1496 | goto Done;\r | |
1497 | } \r | |
1498 | }\r | |
1499 | \r | |
1500 | //\r | |
1501 | // Function part - create a new variable and copy the data.\r | |
1502 | // Both update a variable and create a variable will come here.\r | |
1503 | \r | |
1504 | //\r | |
1505 | // Tricky part: Use scratch data area at the end of volatile variable store\r | |
1506 | // as a temporary storage.\r | |
1507 | //\r | |
1508 | NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r | |
1509 | ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r | |
1510 | \r | |
1511 | SetMem (NextVariable, ScratchSize, 0xff);\r | |
1512 | \r | |
1513 | NextVariable->StartId = VARIABLE_DATA;\r | |
1514 | NextVariable->Attributes = Attributes;\r | |
1515 | //\r | |
1516 | // NextVariable->State = VAR_ADDED;\r | |
1517 | //\r | |
1518 | NextVariable->Reserved = 0;\r | |
1519 | VarNameOffset = sizeof (VARIABLE_HEADER);\r | |
1520 | VarNameSize = StrSize (VariableName);\r | |
1521 | CopyMem (\r | |
1522 | (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r | |
1523 | VariableName,\r | |
1524 | VarNameSize\r | |
1525 | );\r | |
1526 | VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r | |
1527 | CopyMem (\r | |
1528 | (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r | |
1529 | Data,\r | |
1530 | DataSize\r | |
1531 | );\r | |
1532 | CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r | |
1533 | //\r | |
1534 | // There will be pad bytes after Data, the NextVariable->NameSize and\r | |
1535 | // NextVariable->DataSize should not include pad size so that variable\r | |
1536 | // service can get actual size in GetVariable.\r | |
1537 | //\r | |
1538 | NextVariable->NameSize = (UINT32)VarNameSize;\r | |
1539 | NextVariable->DataSize = (UINT32)DataSize;\r | |
1540 | \r | |
1541 | //\r | |
1542 | // The actual size of the variable that stores in storage should\r | |
1543 | // include pad size.\r | |
1544 | //\r | |
1545 | VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r | |
1546 | if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r | |
1547 | //\r | |
1548 | // Create a nonvolatile variable.\r | |
1549 | //\r | |
1550 | Volatile = FALSE;\r | |
1551 | NonVolatileVarableStoreSize = ((VARIABLE_STORE_HEADER *)(UINTN)(mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;\r | |
1552 | if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r | |
1553 | && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r | |
1554 | || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r | |
1555 | && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r | |
1556 | if (AtRuntime ()) {\r | |
1557 | Status = EFI_OUT_OF_RESOURCES;\r | |
1558 | goto Done;\r | |
1559 | }\r | |
1560 | //\r | |
1561 | // Perform garbage collection & reclaim operation.\r | |
1562 | //\r | |
1563 | Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, \r | |
1564 | &mVariableModuleGlobal->NonVolatileLastVariableOffset, FALSE, Variable->CurrPtr);\r | |
1565 | if (EFI_ERROR (Status)) {\r | |
1566 | goto Done;\r | |
1567 | }\r | |
1568 | //\r | |
1569 | // If still no enough space, return out of resources.\r | |
1570 | //\r | |
1571 | if ((((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) \r | |
1572 | && ((VarSize + mVariableModuleGlobal->HwErrVariableTotalSize) > PcdGet32 (PcdHwErrStorageSize)))\r | |
1573 | || (((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == 0) \r | |
1574 | && ((VarSize + mVariableModuleGlobal->CommonVariableTotalSize) > NonVolatileVarableStoreSize - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize)))) {\r | |
1575 | Status = EFI_OUT_OF_RESOURCES;\r | |
1576 | goto Done;\r | |
1577 | }\r | |
1578 | Reclaimed = TRUE;\r | |
1579 | }\r | |
1580 | //\r | |
1581 | // Four steps\r | |
1582 | // 1. Write variable header\r | |
1583 | // 2. Set variable state to header valid \r | |
1584 | // 3. Write variable data\r | |
1585 | // 4. Set variable state to valid\r | |
1586 | //\r | |
1587 | //\r | |
1588 | // Step 1:\r | |
1589 | //\r | |
1590 | CacheOffset = mVariableModuleGlobal->NonVolatileLastVariableOffset;\r | |
1591 | Status = UpdateVariableStore (\r | |
1592 | &mVariableModuleGlobal->VariableGlobal,\r | |
1593 | FALSE,\r | |
1594 | TRUE,\r | |
1595 | Fvb,\r | |
1596 | mVariableModuleGlobal->NonVolatileLastVariableOffset,\r | |
1597 | sizeof (VARIABLE_HEADER),\r | |
1598 | (UINT8 *) NextVariable\r | |
1599 | );\r | |
1600 | \r | |
1601 | if (EFI_ERROR (Status)) {\r | |
1602 | goto Done;\r | |
1603 | }\r | |
1604 | \r | |
1605 | //\r | |
1606 | // Step 2:\r | |
1607 | //\r | |
1608 | NextVariable->State = VAR_HEADER_VALID_ONLY;\r | |
1609 | Status = UpdateVariableStore (\r | |
1610 | &mVariableModuleGlobal->VariableGlobal,\r | |
1611 | FALSE,\r | |
1612 | TRUE,\r | |
1613 | Fvb,\r | |
1614 | mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r | |
1615 | sizeof (UINT8),\r | |
1616 | &NextVariable->State\r | |
1617 | );\r | |
1618 | \r | |
1619 | if (EFI_ERROR (Status)) {\r | |
1620 | goto Done;\r | |
1621 | }\r | |
1622 | //\r | |
1623 | // Step 3:\r | |
1624 | //\r | |
1625 | Status = UpdateVariableStore (\r | |
1626 | &mVariableModuleGlobal->VariableGlobal,\r | |
1627 | FALSE,\r | |
1628 | TRUE,\r | |
1629 | Fvb,\r | |
1630 | mVariableModuleGlobal->NonVolatileLastVariableOffset + sizeof (VARIABLE_HEADER),\r | |
1631 | (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r | |
1632 | (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r | |
1633 | );\r | |
1634 | \r | |
1635 | if (EFI_ERROR (Status)) {\r | |
1636 | goto Done;\r | |
1637 | }\r | |
1638 | //\r | |
1639 | // Step 4:\r | |
1640 | //\r | |
1641 | NextVariable->State = VAR_ADDED;\r | |
1642 | Status = UpdateVariableStore (\r | |
1643 | &mVariableModuleGlobal->VariableGlobal,\r | |
1644 | FALSE,\r | |
1645 | TRUE,\r | |
1646 | Fvb,\r | |
1647 | mVariableModuleGlobal->NonVolatileLastVariableOffset + OFFSET_OF (VARIABLE_HEADER, State),\r | |
1648 | sizeof (UINT8),\r | |
1649 | &NextVariable->State\r | |
1650 | );\r | |
1651 | \r | |
1652 | if (EFI_ERROR (Status)) {\r | |
1653 | goto Done;\r | |
1654 | }\r | |
1655 | \r | |
1656 | mVariableModuleGlobal->NonVolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r | |
1657 | \r | |
1658 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r | |
1659 | mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VarSize);\r | |
1660 | } else {\r | |
1661 | mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VarSize);\r | |
1662 | }\r | |
1663 | //\r | |
1664 | // update the memory copy of Flash region.\r | |
1665 | //\r | |
1666 | CopyMem ((UINT8 *)mNvVariableCache + CacheOffset, (UINT8 *)NextVariable, VarSize);\r | |
1667 | } else {\r | |
1668 | //\r | |
1669 | // Create a volatile variable.\r | |
1670 | // \r | |
1671 | Volatile = TRUE;\r | |
1672 | \r | |
1673 | if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r | |
1674 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r | |
1675 | //\r | |
1676 | // Perform garbage collection & reclaim operation.\r | |
1677 | //\r | |
1678 | Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, \r | |
1679 | &mVariableModuleGlobal->VolatileLastVariableOffset, TRUE, Variable->CurrPtr);\r | |
1680 | if (EFI_ERROR (Status)) {\r | |
1681 | goto Done;\r | |
1682 | }\r | |
1683 | //\r | |
1684 | // If still no enough space, return out of resources.\r | |
1685 | //\r | |
1686 | if ((UINT32) (VarSize + mVariableModuleGlobal->VolatileLastVariableOffset) >\r | |
1687 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size\r | |
1688 | ) {\r | |
1689 | Status = EFI_OUT_OF_RESOURCES;\r | |
1690 | goto Done;\r | |
1691 | }\r | |
1692 | Reclaimed = TRUE;\r | |
1693 | }\r | |
1694 | \r | |
1695 | NextVariable->State = VAR_ADDED;\r | |
1696 | Status = UpdateVariableStore (\r | |
1697 | &mVariableModuleGlobal->VariableGlobal,\r | |
1698 | TRUE,\r | |
1699 | TRUE,\r | |
1700 | Fvb,\r | |
1701 | mVariableModuleGlobal->VolatileLastVariableOffset,\r | |
1702 | (UINT32) VarSize,\r | |
1703 | (UINT8 *) NextVariable\r | |
1704 | );\r | |
1705 | \r | |
1706 | if (EFI_ERROR (Status)) {\r | |
1707 | goto Done;\r | |
1708 | }\r | |
1709 | \r | |
1710 | mVariableModuleGlobal->VolatileLastVariableOffset += HEADER_ALIGN (VarSize);\r | |
1711 | }\r | |
1712 | \r | |
1713 | //\r | |
1714 | // Mark the old variable as deleted.\r | |
1715 | //\r | |
1716 | if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r | |
1717 | State = Variable->CurrPtr->State;\r | |
1718 | State &= VAR_DELETED;\r | |
1719 | \r | |
1720 | Status = UpdateVariableStore (\r | |
1721 | &mVariableModuleGlobal->VariableGlobal,\r | |
1722 | Variable->Volatile,\r | |
1723 | FALSE,\r | |
1724 | Fvb,\r | |
1725 | (UINTN) &Variable->CurrPtr->State,\r | |
1726 | sizeof (UINT8),\r | |
1727 | &State\r | |
1728 | );\r | |
1729 | if (!EFI_ERROR (Status) && !Variable->Volatile) { \r | |
1730 | CacheVariable->CurrPtr->State = State;\r | |
1731 | }\r | |
1732 | }\r | |
1733 | \r | |
1734 | if (!EFI_ERROR (Status)) {\r | |
1735 | UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r | |
1736 | }\r | |
1737 | \r | |
1738 | Done:\r | |
1739 | return Status;\r | |
1740 | }\r | |
1741 | \r | |
1742 | /**\r | |
1743 | \r | |
1744 | This code finds variable in storage blocks (Volatile or Non-Volatile).\r | |
1745 | \r | |
1746 | @param VariableName Name of Variable to be found.\r | |
1747 | @param VendorGuid Variable vendor GUID.\r | |
1748 | @param Attributes Attribute value of the variable found.\r | |
1749 | @param DataSize Size of Data found. If size is less than the\r | |
1750 | data, this value contains the required size.\r | |
1751 | @param Data Data pointer.\r | |
1752 | \r | |
1753 | @return EFI_INVALID_PARAMETER Invalid parameter.\r | |
1754 | @return EFI_SUCCESS Find the specified variable.\r | |
1755 | @return EFI_NOT_FOUND Not found.\r | |
1756 | @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r | |
1757 | \r | |
1758 | **/\r | |
1759 | EFI_STATUS\r | |
1760 | EFIAPI\r | |
1761 | VariableServiceGetVariable (\r | |
1762 | IN CHAR16 *VariableName,\r | |
1763 | IN EFI_GUID *VendorGuid,\r | |
1764 | OUT UINT32 *Attributes OPTIONAL,\r | |
1765 | IN OUT UINTN *DataSize,\r | |
1766 | OUT VOID *Data\r | |
1767 | )\r | |
1768 | {\r | |
1769 | EFI_STATUS Status;\r | |
1770 | VARIABLE_POINTER_TRACK Variable;\r | |
1771 | UINTN VarDataSize;\r | |
1772 | \r | |
1773 | if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r | |
1774 | return EFI_INVALID_PARAMETER;\r | |
1775 | }\r | |
1776 | \r | |
1777 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r | |
1778 | \r | |
1779 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r | |
1780 | if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r | |
1781 | goto Done;\r | |
1782 | }\r | |
1783 | \r | |
1784 | //\r | |
1785 | // Get data size\r | |
1786 | //\r | |
1787 | VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r | |
1788 | ASSERT (VarDataSize != 0);\r | |
1789 | \r | |
1790 | if (*DataSize >= VarDataSize) {\r | |
1791 | if (Data == NULL) {\r | |
1792 | Status = EFI_INVALID_PARAMETER;\r | |
1793 | goto Done;\r | |
1794 | }\r | |
1795 | \r | |
1796 | CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r | |
1797 | if (Attributes != NULL) {\r | |
1798 | *Attributes = Variable.CurrPtr->Attributes;\r | |
1799 | }\r | |
1800 | \r | |
1801 | *DataSize = VarDataSize;\r | |
1802 | UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r | |
1803 | \r | |
1804 | Status = EFI_SUCCESS;\r | |
1805 | goto Done;\r | |
1806 | } else {\r | |
1807 | *DataSize = VarDataSize;\r | |
1808 | Status = EFI_BUFFER_TOO_SMALL;\r | |
1809 | goto Done;\r | |
1810 | }\r | |
1811 | \r | |
1812 | Done:\r | |
1813 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r | |
1814 | return Status;\r | |
1815 | }\r | |
1816 | \r | |
1817 | \r | |
1818 | \r | |
1819 | /**\r | |
1820 | \r | |
1821 | This code Finds the Next available variable.\r | |
1822 | \r | |
1823 | @param VariableNameSize Size of the variable name.\r | |
1824 | @param VariableName Pointer to variable name.\r | |
1825 | @param VendorGuid Variable Vendor Guid.\r | |
1826 | \r | |
1827 | @return EFI_INVALID_PARAMETER Invalid parameter.\r | |
1828 | @return EFI_SUCCESS Find the specified variable.\r | |
1829 | @return EFI_NOT_FOUND Not found.\r | |
1830 | @return EFI_BUFFER_TO_SMALL DataSize is too small for the result.\r | |
1831 | \r | |
1832 | **/\r | |
1833 | EFI_STATUS\r | |
1834 | EFIAPI\r | |
1835 | VariableServiceGetNextVariableName (\r | |
1836 | IN OUT UINTN *VariableNameSize,\r | |
1837 | IN OUT CHAR16 *VariableName,\r | |
1838 | IN OUT EFI_GUID *VendorGuid\r | |
1839 | )\r | |
1840 | {\r | |
1841 | VARIABLE_POINTER_TRACK Variable;\r | |
1842 | UINTN VarNameSize;\r | |
1843 | EFI_STATUS Status;\r | |
1844 | \r | |
1845 | if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r | |
1846 | return EFI_INVALID_PARAMETER;\r | |
1847 | }\r | |
1848 | \r | |
1849 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r | |
1850 | \r | |
1851 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r | |
1852 | if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r | |
1853 | goto Done;\r | |
1854 | }\r | |
1855 | \r | |
1856 | if (VariableName[0] != 0) {\r | |
1857 | //\r | |
1858 | // If variable name is not NULL, get next variable.\r | |
1859 | //\r | |
1860 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
1861 | }\r | |
1862 | \r | |
1863 | while (TRUE) {\r | |
1864 | //\r | |
1865 | // If both volatile and non-volatile variable store are parsed,\r | |
1866 | // return not found.\r | |
1867 | //\r | |
1868 | if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {\r | |
1869 | Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));\r | |
1870 | if (!Variable.Volatile) {\r | |
1871 | Variable.StartPtr = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r | |
1872 | Variable.EndPtr = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase));\r | |
1873 | } else {\r | |
1874 | Status = EFI_NOT_FOUND;\r | |
1875 | goto Done;\r | |
1876 | }\r | |
1877 | \r | |
1878 | Variable.CurrPtr = Variable.StartPtr;\r | |
1879 | if (!IsValidVariableHeader (Variable.CurrPtr)) {\r | |
1880 | continue;\r | |
1881 | }\r | |
1882 | }\r | |
1883 | //\r | |
1884 | // Variable is found\r | |
1885 | //\r | |
1886 | if (IsValidVariableHeader (Variable.CurrPtr) && Variable.CurrPtr->State == VAR_ADDED) {\r | |
1887 | if ((AtRuntime () && ((Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) == 0) {\r | |
1888 | VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r | |
1889 | ASSERT (VarNameSize != 0);\r | |
1890 | \r | |
1891 | if (VarNameSize <= *VariableNameSize) {\r | |
1892 | CopyMem (\r | |
1893 | VariableName,\r | |
1894 | GetVariableNamePtr (Variable.CurrPtr),\r | |
1895 | VarNameSize\r | |
1896 | );\r | |
1897 | CopyMem (\r | |
1898 | VendorGuid,\r | |
1899 | &Variable.CurrPtr->VendorGuid,\r | |
1900 | sizeof (EFI_GUID)\r | |
1901 | );\r | |
1902 | Status = EFI_SUCCESS;\r | |
1903 | } else {\r | |
1904 | Status = EFI_BUFFER_TOO_SMALL;\r | |
1905 | }\r | |
1906 | \r | |
1907 | *VariableNameSize = VarNameSize;\r | |
1908 | goto Done;\r | |
1909 | }\r | |
1910 | }\r | |
1911 | \r | |
1912 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
1913 | }\r | |
1914 | \r | |
1915 | Done:\r | |
1916 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r | |
1917 | return Status;\r | |
1918 | }\r | |
1919 | \r | |
1920 | /**\r | |
1921 | \r | |
1922 | This code sets variable in storage blocks (Volatile or Non-Volatile).\r | |
1923 | \r | |
1924 | @param VariableName Name of Variable to be found.\r | |
1925 | @param VendorGuid Variable vendor GUID.\r | |
1926 | @param Attributes Attribute value of the variable found\r | |
1927 | @param DataSize Size of Data found. If size is less than the\r | |
1928 | data, this value contains the required size.\r | |
1929 | @param Data Data pointer.\r | |
1930 | \r | |
1931 | @return EFI_INVALID_PARAMETER Invalid parameter.\r | |
1932 | @return EFI_SUCCESS Set successfully.\r | |
1933 | @return EFI_OUT_OF_RESOURCES Resource not enough to set variable.\r | |
1934 | @return EFI_NOT_FOUND Not found.\r | |
1935 | @return EFI_WRITE_PROTECTED Variable is read-only.\r | |
1936 | \r | |
1937 | **/\r | |
1938 | EFI_STATUS\r | |
1939 | EFIAPI\r | |
1940 | VariableServiceSetVariable (\r | |
1941 | IN CHAR16 *VariableName,\r | |
1942 | IN EFI_GUID *VendorGuid,\r | |
1943 | IN UINT32 Attributes,\r | |
1944 | IN UINTN DataSize,\r | |
1945 | IN VOID *Data\r | |
1946 | )\r | |
1947 | {\r | |
1948 | VARIABLE_POINTER_TRACK Variable;\r | |
1949 | EFI_STATUS Status;\r | |
1950 | VARIABLE_HEADER *NextVariable;\r | |
1951 | EFI_PHYSICAL_ADDRESS Point;\r | |
1952 | \r | |
1953 | //\r | |
1954 | // Check input parameters.\r | |
1955 | //\r | |
1956 | if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r | |
1957 | return EFI_INVALID_PARAMETER;\r | |
1958 | } \r | |
1959 | \r | |
1960 | if (DataSize != 0 && Data == NULL) {\r | |
1961 | return EFI_INVALID_PARAMETER;\r | |
1962 | }\r | |
1963 | \r | |
1964 | //\r | |
1965 | // Not support authenticated variable write yet.\r | |
1966 | //\r | |
1967 | if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r | |
1968 | return EFI_INVALID_PARAMETER;\r | |
1969 | }\r | |
1970 | \r | |
1971 | //\r | |
1972 | // Make sure if runtime bit is set, boot service bit is set also.\r | |
1973 | //\r | |
1974 | if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r | |
1975 | return EFI_INVALID_PARAMETER;\r | |
1976 | }\r | |
1977 | \r | |
1978 | //\r | |
1979 | // The size of the VariableName, including the Unicode Null in bytes plus\r | |
1980 | // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r | |
1981 | // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r | |
1982 | //\r | |
1983 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
1984 | if ((DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize)) ||\r | |
1985 | (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxHardwareErrorVariableSize))) {\r | |
1986 | return EFI_INVALID_PARAMETER;\r | |
1987 | }\r | |
1988 | //\r | |
1989 | // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX".\r | |
1990 | //\r | |
1991 | if (StrnCmp(VariableName, L"HwErrRec", StrLen(L"HwErrRec")) != 0) {\r | |
1992 | return EFI_INVALID_PARAMETER;\r | |
1993 | }\r | |
1994 | } else {\r | |
1995 | //\r | |
1996 | // The size of the VariableName, including the Unicode Null in bytes plus\r | |
1997 | // the DataSize is limited to maximum size of PcdGet32 (PcdMaxVariableSize) bytes.\r | |
1998 | //\r | |
1999 | if ((DataSize > PcdGet32 (PcdMaxVariableSize)) ||\r | |
2000 | (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32 (PcdMaxVariableSize))) {\r | |
2001 | return EFI_INVALID_PARAMETER;\r | |
2002 | } \r | |
2003 | } \r | |
2004 | \r | |
2005 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r | |
2006 | \r | |
2007 | //\r | |
2008 | // Consider reentrant in MCA/INIT/NMI. It needs be reupdated.\r | |
2009 | //\r | |
2010 | if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r | |
2011 | Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r | |
2012 | //\r | |
2013 | // Parse non-volatile variable data and get last variable offset.\r | |
2014 | //\r | |
2015 | NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r | |
2016 | while ((NextVariable < GetEndPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point)) \r | |
2017 | && IsValidVariableHeader (NextVariable)) {\r | |
2018 | NextVariable = GetNextVariablePtr (NextVariable);\r | |
2019 | }\r | |
2020 | mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r | |
2021 | }\r | |
2022 | \r | |
2023 | //\r | |
2024 | // Check whether the input variable is already existed.\r | |
2025 | //\r | |
2026 | FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r | |
2027 | \r | |
2028 | //\r | |
2029 | // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang.\r | |
2030 | //\r | |
2031 | AutoUpdateLangVariable (VariableName, Data, DataSize);\r | |
2032 | \r | |
2033 | Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);\r | |
2034 | \r | |
2035 | InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r | |
2036 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r | |
2037 | \r | |
2038 | return Status;\r | |
2039 | }\r | |
2040 | \r | |
2041 | /**\r | |
2042 | \r | |
2043 | This code returns information about the EFI variables.\r | |
2044 | \r | |
2045 | @param Attributes Attributes bitmask to specify the type of variables\r | |
2046 | on which to return information.\r | |
2047 | @param MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r | |
2048 | for the EFI variables associated with the attributes specified.\r | |
2049 | @param RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r | |
2050 | for EFI variables associated with the attributes specified.\r | |
2051 | @param MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r | |
2052 | associated with the attributes specified.\r | |
2053 | \r | |
2054 | @return EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied.\r | |
2055 | @return EFI_SUCCESS Query successfully.\r | |
2056 | @return EFI_UNSUPPORTED The attribute is not supported on this platform.\r | |
2057 | \r | |
2058 | **/\r | |
2059 | EFI_STATUS\r | |
2060 | EFIAPI\r | |
2061 | VariableServiceQueryVariableInfo (\r | |
2062 | IN UINT32 Attributes,\r | |
2063 | OUT UINT64 *MaximumVariableStorageSize,\r | |
2064 | OUT UINT64 *RemainingVariableStorageSize,\r | |
2065 | OUT UINT64 *MaximumVariableSize\r | |
2066 | )\r | |
2067 | {\r | |
2068 | VARIABLE_HEADER *Variable;\r | |
2069 | VARIABLE_HEADER *NextVariable;\r | |
2070 | UINT64 VariableSize;\r | |
2071 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
2072 | UINT64 CommonVariableTotalSize;\r | |
2073 | UINT64 HwErrVariableTotalSize;\r | |
2074 | \r | |
2075 | CommonVariableTotalSize = 0;\r | |
2076 | HwErrVariableTotalSize = 0;\r | |
2077 | \r | |
2078 | if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r | |
2079 | return EFI_INVALID_PARAMETER;\r | |
2080 | }\r | |
2081 | \r | |
2082 | if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r | |
2083 | //\r | |
2084 | // Make sure the Attributes combination is supported by the platform.\r | |
2085 | //\r | |
2086 | return EFI_UNSUPPORTED; \r | |
2087 | } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r | |
2088 | //\r | |
2089 | // Make sure if runtime bit is set, boot service bit is set also.\r | |
2090 | //\r | |
2091 | return EFI_INVALID_PARAMETER;\r | |
2092 | } else if (AtRuntime () && ((Attributes & EFI_VARIABLE_RUNTIME_ACCESS) == 0)) {\r | |
2093 | //\r | |
2094 | // Make sure RT Attribute is set if we are in Runtime phase.\r | |
2095 | //\r | |
2096 | return EFI_INVALID_PARAMETER;\r | |
2097 | } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
2098 | //\r | |
2099 | // Make sure Hw Attribute is set with NV.\r | |
2100 | //\r | |
2101 | return EFI_INVALID_PARAMETER;\r | |
2102 | } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r | |
2103 | //\r | |
2104 | // Not support authentiated variable write yet.\r | |
2105 | //\r | |
2106 | return EFI_UNSUPPORTED;\r | |
2107 | }\r | |
2108 | \r | |
2109 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r | |
2110 | \r | |
2111 | if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r | |
2112 | //\r | |
2113 | // Query is Volatile related.\r | |
2114 | //\r | |
2115 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r | |
2116 | } else {\r | |
2117 | //\r | |
2118 | // Query is Non-Volatile related.\r | |
2119 | //\r | |
2120 | VariableStoreHeader = mNvVariableCache;\r | |
2121 | }\r | |
2122 | \r | |
2123 | //\r | |
2124 | // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r | |
2125 | // with the storage size (excluding the storage header size).\r | |
2126 | //\r | |
2127 | *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r | |
2128 | \r | |
2129 | //\r | |
2130 | // Harware error record variable needs larger size.\r | |
2131 | //\r | |
2132 | if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r | |
2133 | *MaximumVariableStorageSize = PcdGet32 (PcdHwErrStorageSize);\r | |
2134 | *MaximumVariableSize = PcdGet32 (PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r | |
2135 | } else {\r | |
2136 | if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r | |
2137 | ASSERT (PcdGet32 (PcdHwErrStorageSize) < VariableStoreHeader->Size);\r | |
2138 | *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32 (PcdHwErrStorageSize);\r | |
2139 | }\r | |
2140 | \r | |
2141 | //\r | |
2142 | // Let *MaximumVariableSize be PcdGet32 (PcdMaxVariableSize) with the exception of the variable header size.\r | |
2143 | //\r | |
2144 | *MaximumVariableSize = PcdGet32 (PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r | |
2145 | }\r | |
2146 | \r | |
2147 | //\r | |
2148 | // Point to the starting address of the variables.\r | |
2149 | //\r | |
2150 | Variable = GetStartPointer (VariableStoreHeader);\r | |
2151 | \r | |
2152 | //\r | |
2153 | // Now walk through the related variable store.\r | |
2154 | //\r | |
2155 | while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {\r | |
2156 | NextVariable = GetNextVariablePtr (Variable);\r | |
2157 | VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r | |
2158 | \r | |
2159 | if (AtRuntime ()) {\r | |
2160 | //\r | |
2161 | // We don't take the state of the variables in mind\r | |
2162 | // when calculating RemainingVariableStorageSize,\r | |
2163 | // since the space occupied by variables not marked with\r | |
2164 | // VAR_ADDED is not allowed to be reclaimed in Runtime.\r | |
2165 | //\r | |
2166 | if ((NextVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
2167 | HwErrVariableTotalSize += VariableSize;\r | |
2168 | } else {\r | |
2169 | CommonVariableTotalSize += VariableSize;\r | |
2170 | }\r | |
2171 | } else {\r | |
2172 | //\r | |
2173 | // Only care about Variables with State VAR_ADDED, because\r | |
2174 | // the space not marked as VAR_ADDED is reclaimable now.\r | |
2175 | //\r | |
2176 | if (Variable->State == VAR_ADDED) {\r | |
2177 | if ((NextVariable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
2178 | HwErrVariableTotalSize += VariableSize;\r | |
2179 | } else {\r | |
2180 | CommonVariableTotalSize += VariableSize;\r | |
2181 | }\r | |
2182 | }\r | |
2183 | }\r | |
2184 | \r | |
2185 | //\r | |
2186 | // Go to the next one.\r | |
2187 | //\r | |
2188 | Variable = NextVariable;\r | |
2189 | }\r | |
2190 | \r | |
2191 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r | |
2192 | *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r | |
2193 | }else {\r | |
2194 | *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r | |
2195 | }\r | |
2196 | \r | |
2197 | if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r | |
2198 | *MaximumVariableSize = 0;\r | |
2199 | } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r | |
2200 | *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r | |
2201 | }\r | |
2202 | \r | |
2203 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r | |
2204 | return EFI_SUCCESS;\r | |
2205 | }\r | |
2206 | \r | |
2207 | \r | |
2208 | /**\r | |
2209 | This function reclaims variable storage if free size is below the threshold.\r | |
2210 | \r | |
2211 | **/\r | |
2212 | VOID\r | |
2213 | ReclaimForOS(\r | |
2214 | VOID\r | |
2215 | )\r | |
2216 | {\r | |
2217 | EFI_STATUS Status;\r | |
2218 | UINTN CommonVariableSpace;\r | |
2219 | UINTN RemainingCommonVariableSpace;\r | |
2220 | UINTN RemainingHwErrVariableSpace;\r | |
2221 | \r | |
2222 | Status = EFI_SUCCESS; \r | |
2223 | \r | |
2224 | CommonVariableSpace = ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize); //Allowable max size of common variable storage space\r | |
2225 | \r | |
2226 | RemainingCommonVariableSpace = CommonVariableSpace - mVariableModuleGlobal->CommonVariableTotalSize;\r | |
2227 | \r | |
2228 | RemainingHwErrVariableSpace = PcdGet32 (PcdHwErrStorageSize) - mVariableModuleGlobal->HwErrVariableTotalSize;\r | |
2229 | //\r | |
2230 | // Check if the free area is blow a threshold.\r | |
2231 | //\r | |
2232 | if ((RemainingCommonVariableSpace < PcdGet32 (PcdMaxVariableSize))\r | |
2233 | || ((PcdGet32 (PcdHwErrStorageSize) != 0) && \r | |
2234 | (RemainingHwErrVariableSpace < PcdGet32 (PcdMaxHardwareErrorVariableSize)))){\r | |
2235 | Status = Reclaim (\r | |
2236 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r | |
2237 | &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r | |
2238 | FALSE,\r | |
2239 | NULL\r | |
2240 | );\r | |
2241 | ASSERT_EFI_ERROR (Status);\r | |
2242 | }\r | |
2243 | }\r | |
2244 | \r | |
2245 | \r | |
2246 | /**\r | |
2247 | Initializes variable write service after FVB was ready.\r | |
2248 | \r | |
2249 | @retval EFI_SUCCESS Function successfully executed.\r | |
2250 | @retval Others Fail to initialize the variable service.\r | |
2251 | \r | |
2252 | **/\r | |
2253 | EFI_STATUS\r | |
2254 | VariableWriteServiceInitialize (\r | |
2255 | VOID\r | |
2256 | )\r | |
2257 | {\r | |
2258 | EFI_STATUS Status;\r | |
2259 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
2260 | UINTN Index;\r | |
2261 | UINT8 Data;\r | |
2262 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r | |
2263 | EFI_PHYSICAL_ADDRESS BaseAddress;\r | |
2264 | UINT64 Length;\r | |
2265 | EFI_PHYSICAL_ADDRESS VariableStoreBase;\r | |
2266 | UINT64 VariableStoreLength;\r | |
2267 | \r | |
2268 | VariableStoreBase = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r | |
2269 | VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r | |
2270 | VariableStoreLength = VariableStoreHeader->Size;\r | |
2271 | \r | |
2272 | //\r | |
2273 | // Check if the free area is really free.\r | |
2274 | //\r | |
2275 | for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreLength; Index++) {\r | |
2276 | Data = ((UINT8 *) mNvVariableCache)[Index];\r | |
2277 | if (Data != 0xff) {\r | |
2278 | //\r | |
2279 | // There must be something wrong in variable store, do reclaim operation.\r | |
2280 | //\r | |
2281 | Status = Reclaim (\r | |
2282 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r | |
2283 | &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r | |
2284 | FALSE,\r | |
2285 | NULL\r | |
2286 | );\r | |
2287 | if (EFI_ERROR (Status)) {\r | |
2288 | return Status;\r | |
2289 | }\r | |
2290 | break;\r | |
2291 | }\r | |
2292 | }\r | |
2293 | \r | |
2294 | //\r | |
2295 | // Mark the variable storage region of the FLASH as RUNTIME.\r | |
2296 | //\r | |
2297 | BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r | |
2298 | Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r | |
2299 | Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r | |
2300 | \r | |
2301 | Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r | |
2302 | if (EFI_ERROR (Status)) {\r | |
2303 | DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r | |
2304 | } else {\r | |
2305 | Status = gDS->SetMemorySpaceAttributes (\r | |
2306 | BaseAddress,\r | |
2307 | Length,\r | |
2308 | GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r | |
2309 | );\r | |
2310 | if (EFI_ERROR (Status)) {\r | |
2311 | DEBUG ((DEBUG_WARN, "Variable driver failed to add EFI_MEMORY_RUNTIME attribute to Flash.\n"));\r | |
2312 | }\r | |
2313 | }\r | |
2314 | return EFI_SUCCESS;\r | |
2315 | }\r | |
2316 | \r | |
2317 | \r | |
2318 | /**\r | |
2319 | Initializes variable store area for non-volatile and volatile variable.\r | |
2320 | \r | |
2321 | @retval EFI_SUCCESS Function successfully executed.\r | |
2322 | @retval EFI_OUT_OF_RESOURCES Fail to allocate enough memory resource.\r | |
2323 | \r | |
2324 | **/\r | |
2325 | EFI_STATUS\r | |
2326 | VariableCommonInitialize (\r | |
2327 | VOID\r | |
2328 | )\r | |
2329 | {\r | |
2330 | EFI_STATUS Status;\r | |
2331 | VARIABLE_STORE_HEADER *VolatileVariableStore;\r | |
2332 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
2333 | VARIABLE_HEADER *NextVariable;\r | |
2334 | EFI_PHYSICAL_ADDRESS TempVariableStoreHeader;\r | |
2335 | EFI_PHYSICAL_ADDRESS VariableStoreBase;\r | |
2336 | UINT64 VariableStoreLength;\r | |
2337 | UINTN ScratchSize;\r | |
2338 | UINTN VariableSize;\r | |
2339 | \r | |
2340 | //\r | |
2341 | // Allocate runtime memory for variable driver global structure.\r | |
2342 | //\r | |
2343 | mVariableModuleGlobal = AllocateRuntimeZeroPool (sizeof (VARIABLE_MODULE_GLOBAL));\r | |
2344 | if (mVariableModuleGlobal == NULL) {\r | |
2345 | return EFI_OUT_OF_RESOURCES;\r | |
2346 | }\r | |
2347 | \r | |
2348 | InitializeLock (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r | |
2349 | \r | |
2350 | //\r | |
2351 | // Note that in EdkII variable driver implementation, Hardware Error Record type variable\r | |
2352 | // is stored with common variable in the same NV region. So the platform integrator should\r | |
2353 | // ensure that the value of PcdHwErrStorageSize is less than or equal to the value of \r | |
2354 | // PcdFlashNvStorageVariableSize.\r | |
2355 | //\r | |
2356 | ASSERT (PcdGet32 (PcdHwErrStorageSize) <= PcdGet32 (PcdFlashNvStorageVariableSize));\r | |
2357 | \r | |
2358 | //\r | |
2359 | // Allocate memory for volatile variable store, note that there is a scratch space to store scratch data.\r | |
2360 | //\r | |
2361 | ScratchSize = MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxHardwareErrorVariableSize));\r | |
2362 | VolatileVariableStore = AllocateRuntimePool (PcdGet32 (PcdVariableStoreSize) + ScratchSize);\r | |
2363 | if (VolatileVariableStore == NULL) {\r | |
2364 | FreePool (mVariableModuleGlobal);\r | |
2365 | return EFI_OUT_OF_RESOURCES;\r | |
2366 | }\r | |
2367 | \r | |
2368 | SetMem (VolatileVariableStore, PcdGet32 (PcdVariableStoreSize) + ScratchSize, 0xff);\r | |
2369 | \r | |
2370 | //\r | |
2371 | // Initialize Variable Specific Data.\r | |
2372 | //\r | |
2373 | mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r | |
2374 | mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r | |
2375 | mVariableModuleGlobal->FvbInstance = NULL;\r | |
2376 | \r | |
2377 | CopyGuid (&VolatileVariableStore->Signature, &gEfiVariableGuid);\r | |
2378 | VolatileVariableStore->Size = PcdGet32 (PcdVariableStoreSize);\r | |
2379 | VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r | |
2380 | VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r | |
2381 | VolatileVariableStore->Reserved = 0;\r | |
2382 | VolatileVariableStore->Reserved1 = 0;\r | |
2383 | \r | |
2384 | //\r | |
2385 | // Get non-volatile varaible store.\r | |
2386 | //\r | |
2387 | \r | |
2388 | TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);\r | |
2389 | if (TempVariableStoreHeader == 0) {\r | |
2390 | TempVariableStoreHeader = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);\r | |
2391 | }\r | |
2392 | VariableStoreBase = TempVariableStoreHeader + \\r | |
2393 | (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r | |
2394 | VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r | |
2395 | (((EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)(TempVariableStoreHeader)) -> HeaderLength);\r | |
2396 | \r | |
2397 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r | |
2398 | VariableStoreHeader = (VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase;\r | |
2399 | if (GetVariableStoreStatus (VariableStoreHeader) != EfiValid) {\r | |
2400 | Status = EFI_VOLUME_CORRUPTED;\r | |
2401 | DEBUG((EFI_D_INFO, "Variable Store header is corrupted\n"));\r | |
2402 | goto Done;\r | |
2403 | } \r | |
2404 | ASSERT(VariableStoreHeader->Size == VariableStoreLength);\r | |
2405 | \r | |
2406 | //\r | |
2407 | // Parse non-volatile variable data and get last variable offset.\r | |
2408 | //\r | |
2409 | NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *)(UINTN)VariableStoreBase);\r | |
2410 | while (IsValidVariableHeader (NextVariable)) {\r | |
2411 | VariableSize = NextVariable->NameSize + NextVariable->DataSize + sizeof (VARIABLE_HEADER);\r | |
2412 | if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r | |
2413 | mVariableModuleGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r | |
2414 | } else {\r | |
2415 | mVariableModuleGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);\r | |
2416 | }\r | |
2417 | \r | |
2418 | NextVariable = GetNextVariablePtr (NextVariable);\r | |
2419 | }\r | |
2420 | \r | |
2421 | mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) VariableStoreBase;\r | |
2422 | \r | |
2423 | //\r | |
2424 | // Allocate runtime memory used for a memory copy of the FLASH region.\r | |
2425 | // Keep the memory and the FLASH in sync as updates occur\r | |
2426 | //\r | |
2427 | mNvVariableCache = AllocateRuntimeZeroPool ((UINTN)VariableStoreLength);\r | |
2428 | if (mNvVariableCache == NULL) {\r | |
2429 | Status = EFI_OUT_OF_RESOURCES;\r | |
2430 | goto Done;\r | |
2431 | }\r | |
2432 | CopyMem (mNvVariableCache, (CHAR8 *)(UINTN)VariableStoreBase, (UINTN)VariableStoreLength);\r | |
2433 | Status = EFI_SUCCESS;\r | |
2434 | \r | |
2435 | Done:\r | |
2436 | if (EFI_ERROR (Status)) {\r | |
2437 | FreePool (mVariableModuleGlobal);\r | |
2438 | FreePool (VolatileVariableStore);\r | |
2439 | }\r | |
2440 | \r | |
2441 | return Status;\r | |
2442 | }\r | |
2443 | \r | |
2444 | \r | |
2445 | /**\r | |
2446 | Get the proper fvb handle and/or fvb protocol by the given Flash address.\r | |
2447 | \r | |
2448 | @param[in] Address The Flash address.\r | |
2449 | @param[out] FvbHandle In output, if it is not NULL, it points to the proper FVB handle.\r | |
2450 | @param[out] FvbProtocol In output, if it is not NULL, it points to the proper FVB protocol.\r | |
2451 | \r | |
2452 | **/\r | |
2453 | EFI_STATUS\r | |
2454 | GetFvbInfoByAddress (\r | |
2455 | IN EFI_PHYSICAL_ADDRESS Address,\r | |
2456 | OUT EFI_HANDLE *FvbHandle OPTIONAL,\r | |
2457 | OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvbProtocol OPTIONAL\r | |
2458 | )\r | |
2459 | {\r | |
2460 | EFI_STATUS Status;\r | |
2461 | EFI_HANDLE *HandleBuffer;\r | |
2462 | UINTN HandleCount;\r | |
2463 | UINTN Index;\r | |
2464 | EFI_PHYSICAL_ADDRESS FvbBaseAddress;\r | |
2465 | EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL *Fvb;\r | |
2466 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
2467 | EFI_FVB_ATTRIBUTES_2 Attributes;\r | |
2468 | \r | |
2469 | //\r | |
2470 | // Get all FVB handles.\r | |
2471 | //\r | |
2472 | Status = GetFvbCountAndBuffer (&HandleCount, &HandleBuffer);\r | |
2473 | if (EFI_ERROR (Status)) {\r | |
2474 | return EFI_NOT_FOUND;\r | |
2475 | }\r | |
2476 | \r | |
2477 | //\r | |
2478 | // Get the FVB to access variable store.\r | |
2479 | //\r | |
2480 | Fvb = NULL;\r | |
2481 | for (Index = 0; Index < HandleCount; Index += 1, Status = EFI_NOT_FOUND, Fvb = NULL) {\r | |
2482 | Status = GetFvbByHandle (HandleBuffer[Index], &Fvb);\r | |
2483 | if (EFI_ERROR (Status)) {\r | |
2484 | Status = EFI_NOT_FOUND;\r | |
2485 | break;\r | |
2486 | }\r | |
2487 | \r | |
2488 | //\r | |
2489 | // Ensure this FVB protocol supported Write operation.\r | |
2490 | //\r | |
2491 | Status = Fvb->GetAttributes (Fvb, &Attributes);\r | |
2492 | if (EFI_ERROR (Status) || ((Attributes & EFI_FVB2_WRITE_STATUS) == 0)) {\r | |
2493 | continue; \r | |
2494 | }\r | |
2495 | \r | |
2496 | //\r | |
2497 | // Compare the address and select the right one.\r | |
2498 | //\r | |
2499 | Status = Fvb->GetPhysicalAddress (Fvb, &FvbBaseAddress);\r | |
2500 | if (EFI_ERROR (Status)) {\r | |
2501 | continue;\r | |
2502 | }\r | |
2503 | \r | |
2504 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvbBaseAddress);\r | |
2505 | if ((Address >= FvbBaseAddress) && (Address < (FvbBaseAddress + FwVolHeader->FvLength))) {\r | |
2506 | if (FvbHandle != NULL) {\r | |
2507 | *FvbHandle = HandleBuffer[Index];\r | |
2508 | }\r | |
2509 | if (FvbProtocol != NULL) {\r | |
2510 | *FvbProtocol = Fvb;\r | |
2511 | }\r | |
2512 | Status = EFI_SUCCESS;\r | |
2513 | break;\r | |
2514 | }\r | |
2515 | }\r | |
2516 | FreePool (HandleBuffer);\r | |
2517 | \r | |
2518 | if (Fvb == NULL) {\r | |
2519 | Status = EFI_NOT_FOUND;\r | |
2520 | }\r | |
2521 | \r | |
2522 | return Status; \r | |
2523 | }\r | |
2524 | \r |