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