Commit | Line | Data |
---|---|---|
052ad7e1 | 1 | /** @file\r |
504214c4 LG |
2 | \r |
3 | Implement all four UEFI Runtime Variable services for the nonvolatile\r | |
4 | and volatile storage space and install variable architecture protocol.\r | |
052ad7e1 | 5 | \r |
504214c4 LG |
6 | Copyright (c) 2006 - 2008, Intel Corporation \r |
7 | All rights reserved. This program and the accompanying materials \r | |
8 | are licensed and made available under the terms and conditions of the BSD License \r | |
9 | which accompanies this distribution. The full text of the license may be found at \r | |
10 | http://opensource.org/licenses/bsd-license.php \r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
8d3a5c82 | 14 | \r |
052ad7e1 | 15 | **/\r |
8d3a5c82 | 16 | \r |
17 | \r | |
18 | #include "Variable.h"\r | |
33a5a666 | 19 | \r |
7800593d | 20 | VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal;\r |
052ad7e1 A |
21 | EFI_EVENT mVirtualAddressChangeEvent = NULL;\r |
22 | EFI_HANDLE mHandle = NULL;\r | |
8d3a5c82 | 23 | \r |
33a5a666 | 24 | \r |
8d3a5c82 | 25 | //\r |
26 | // This is a temperary function which will be removed\r | |
27 | // when EfiAcquireLock in UefiLib can handle the\r | |
28 | // the call in UEFI Runtimer driver in RT phase.\r | |
29 | //\r | |
8d3a5c82 | 30 | VOID\r |
31 | AcquireLockOnlyAtBootTime (\r | |
32 | IN EFI_LOCK *Lock\r | |
33 | )\r | |
34 | {\r | |
35 | if (!EfiAtRuntime ()) {\r | |
36 | EfiAcquireLock (Lock);\r | |
37 | }\r | |
38 | }\r | |
39 | \r | |
40 | //\r | |
41 | // This is a temperary function which will be removed\r | |
42 | // when EfiAcquireLock in UefiLib can handle the\r | |
43 | // the call in UEFI Runtimer driver in RT phase.\r | |
44 | //\r | |
8d3a5c82 | 45 | VOID\r |
46 | ReleaseLockOnlyAtBootTime (\r | |
47 | IN EFI_LOCK *Lock\r | |
48 | )\r | |
49 | {\r | |
50 | if (!EfiAtRuntime ()) {\r | |
51 | EfiReleaseLock (Lock);\r | |
52 | }\r | |
53 | }\r | |
54 | \r | |
33a5a666 A |
55 | \r |
56 | GLOBAL_REMOVE_IF_UNREFERENCED VARIABLE_INFO_ENTRY *gVariableInfo = NULL;\r | |
57 | \r | |
58 | \r | |
052ad7e1 A |
59 | /**\r |
60 | Routine used to track statistical information about variable usage. \r | |
61 | The data is stored in the EFI system table so it can be accessed later.\r | |
62 | VariableInfo.efi can dump out the table. Only Boot Services variable \r | |
63 | accesses are tracked by this code. The PcdVariableCollectStatistics\r | |
64 | build flag controls if this feature is enabled. \r | |
65 | \r | |
66 | A read that hits in the cache will have Read and Cache true for \r | |
67 | the transaction. Data is allocated by this routine, but never\r | |
68 | freed.\r | |
69 | \r | |
70 | @param[in] VariableName Name of the Variable to track\r | |
71 | @param[in] VendorGuid Guid of the Variable to track\r | |
72 | @param[in] Volatile TRUE if volatile FALSE if non-volatile\r | |
73 | @param[in] Read TRUE if GetVariable() was called\r | |
74 | @param[in] Write TRUE if SetVariable() was called\r | |
75 | @param[in] Delete TRUE if deleted via SetVariable()\r | |
76 | @param[in] Cache TRUE for a cache hit.\r | |
77 | \r | |
78 | **/\r | |
33a5a666 A |
79 | VOID\r |
80 | UpdateVariableInfo (\r | |
81 | IN CHAR16 *VariableName,\r | |
82 | IN EFI_GUID *VendorGuid,\r | |
83 | IN BOOLEAN Volatile,\r | |
84 | IN BOOLEAN Read,\r | |
85 | IN BOOLEAN Write,\r | |
86 | IN BOOLEAN Delete,\r | |
87 | IN BOOLEAN Cache\r | |
88 | )\r | |
89 | {\r | |
90 | VARIABLE_INFO_ENTRY *Entry;\r | |
91 | \r | |
92 | if (FeaturePcdGet (PcdVariableCollectStatistics)) {\r | |
93 | \r | |
94 | if (EfiAtRuntime ()) {\r | |
95 | // Don't collect statistics at runtime\r | |
96 | return;\r | |
97 | }\r | |
98 | \r | |
99 | if (gVariableInfo == NULL) {\r | |
052ad7e1 A |
100 | //\r |
101 | // on the first call allocate a entry and place a pointer to it in\r | |
102 | // the EFI System Table\r | |
103 | //\r | |
33a5a666 | 104 | gVariableInfo = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r |
052ad7e1 A |
105 | ASSERT (gVariableInfo != NULL);\r |
106 | \r | |
33a5a666 A |
107 | CopyGuid (&gVariableInfo->VendorGuid, VendorGuid);\r |
108 | gVariableInfo->Name = AllocatePool (StrLen (VariableName));\r | |
109 | StrCpy (gVariableInfo->Name, VariableName);\r | |
110 | gVariableInfo->Volatile = Volatile;\r | |
111 | \r | |
112 | gBS->InstallConfigurationTable (&gEfiVariableInfoGuid, gVariableInfo);\r | |
113 | }\r | |
114 | \r | |
115 | \r | |
116 | for (Entry = gVariableInfo; Entry != NULL; Entry = Entry->Next) {\r | |
117 | if (CompareGuid (VendorGuid, &Entry->VendorGuid)) {\r | |
118 | if (StrCmp (VariableName, Entry->Name) == 0) {\r | |
119 | if (Read) {\r | |
120 | Entry->ReadCount++;\r | |
121 | }\r | |
122 | if (Write) {\r | |
123 | Entry->WriteCount++;\r | |
124 | }\r | |
125 | if (Delete) {\r | |
126 | Entry->DeleteCount++;\r | |
127 | }\r | |
128 | if (Cache) {\r | |
129 | Entry->CacheCount++;\r | |
130 | }\r | |
131 | \r | |
132 | return;\r | |
133 | }\r | |
134 | }\r | |
135 | \r | |
136 | if (Entry->Next == NULL) {\r | |
052ad7e1 A |
137 | //\r |
138 | // If the entry is not in the table add it.\r | |
139 | // Next iteration of the loop will fill in the data\r | |
140 | //\r | |
33a5a666 | 141 | Entry->Next = AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY));\r |
052ad7e1 | 142 | ASSERT (Entry->Next != NULL);\r |
33a5a666 A |
143 | \r |
144 | CopyGuid (&Entry->Next->VendorGuid, VendorGuid);\r | |
145 | Entry->Next->Name = AllocatePool (StrLen (VariableName));\r | |
146 | StrCpy (Entry->Next->Name, VariableName);\r | |
147 | Entry->Next->Volatile = Volatile;\r | |
148 | }\r | |
149 | \r | |
150 | }\r | |
151 | }\r | |
152 | }\r | |
153 | \r | |
154 | \r | |
8d3a5c82 | 155 | BOOLEAN\r |
8d3a5c82 | 156 | IsValidVariableHeader (\r |
157 | IN VARIABLE_HEADER *Variable\r | |
158 | )\r | |
159 | /*++\r | |
160 | \r | |
161 | Routine Description:\r | |
162 | \r | |
163 | This code checks if variable header is valid or not.\r | |
164 | \r | |
165 | Arguments:\r | |
166 | Variable Pointer to the Variable Header.\r | |
167 | \r | |
168 | Returns:\r | |
169 | TRUE Variable header is valid.\r | |
170 | FALSE Variable header is not valid.\r | |
171 | \r | |
172 | --*/\r | |
173 | {\r | |
fdb7765f | 174 | if (Variable == NULL || Variable->StartId != VARIABLE_DATA) {\r |
8d3a5c82 | 175 | return FALSE;\r |
176 | }\r | |
177 | \r | |
178 | return TRUE;\r | |
179 | }\r | |
180 | \r | |
052ad7e1 | 181 | \r |
8d3a5c82 | 182 | EFI_STATUS\r |
8d3a5c82 | 183 | UpdateVariableStore (\r |
184 | IN VARIABLE_GLOBAL *Global,\r | |
185 | IN BOOLEAN Volatile,\r | |
186 | IN BOOLEAN SetByIndex,\r | |
187 | IN UINTN Instance,\r | |
188 | IN UINTN DataPtrIndex,\r | |
189 | IN UINT32 DataSize,\r | |
190 | IN UINT8 *Buffer\r | |
191 | )\r | |
192 | /*++\r | |
193 | \r | |
194 | Routine Description:\r | |
195 | \r | |
196 | This function writes data to the FWH at the correct LBA even if the LBAs\r | |
197 | are fragmented.\r | |
198 | \r | |
199 | Arguments:\r | |
200 | \r | |
c6492839 | 201 | Global - Pointer to VARAIBLE_GLOBAL structure\r |
202 | Volatile - If the Variable is Volatile or Non-Volatile\r | |
203 | SetByIndex - TRUE: Target pointer is given as index\r | |
204 | FALSE: Target pointer is absolute\r | |
205 | Instance - Instance of FV Block services\r | |
206 | DataPtrIndex - Pointer to the Data from the end of VARIABLE_STORE_HEADER\r | |
207 | structure\r | |
208 | DataSize - Size of data to be written.\r | |
209 | Buffer - Pointer to the buffer from which data is written\r | |
8d3a5c82 | 210 | \r |
211 | Returns:\r | |
212 | \r | |
c6492839 | 213 | EFI_INVALID_PARAMETER - Parameters not valid\r |
214 | EFI_SUCCESS - Variable store successfully updated\r | |
8d3a5c82 | 215 | \r |
216 | --*/\r | |
217 | {\r | |
218 | EFI_FV_BLOCK_MAP_ENTRY *PtrBlockMapEntry;\r | |
219 | UINTN BlockIndex2;\r | |
220 | UINTN LinearOffset;\r | |
221 | UINTN CurrWriteSize;\r | |
222 | UINTN CurrWritePtr;\r | |
223 | UINT8 *CurrBuffer;\r | |
224 | EFI_LBA LbaNumber;\r | |
225 | UINTN Size;\r | |
226 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
227 | VARIABLE_STORE_HEADER *VolatileBase;\r | |
228 | EFI_PHYSICAL_ADDRESS FvVolHdr;\r | |
229 | EFI_PHYSICAL_ADDRESS DataPtr;\r | |
230 | EFI_STATUS Status;\r | |
231 | \r | |
232 | FwVolHeader = NULL;\r | |
233 | DataPtr = DataPtrIndex;\r | |
234 | \r | |
235 | //\r | |
236 | // Check if the Data is Volatile\r | |
237 | //\r | |
238 | if (!Volatile) {\r | |
239 | EfiFvbGetPhysicalAddress (Instance, &FvVolHdr);\r | |
240 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r | |
241 | //\r | |
242 | // Data Pointer should point to the actual Address where data is to be\r | |
243 | // written\r | |
244 | //\r | |
245 | if (SetByIndex) {\r | |
052ad7e1 | 246 | DataPtr += mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;\r |
8d3a5c82 | 247 | }\r |
248 | \r | |
249 | if ((DataPtr + DataSize) >= ((EFI_PHYSICAL_ADDRESS) (UINTN) ((UINT8 *) FwVolHeader + FwVolHeader->FvLength))) {\r | |
250 | return EFI_INVALID_PARAMETER;\r | |
251 | }\r | |
252 | } else {\r | |
253 | //\r | |
254 | // Data Pointer should point to the actual Address where data is to be\r | |
255 | // written\r | |
256 | //\r | |
052ad7e1 | 257 | VolatileBase = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r |
8d3a5c82 | 258 | if (SetByIndex) {\r |
052ad7e1 | 259 | DataPtr += mVariableModuleGlobal->VariableGlobal.VolatileVariableBase;\r |
8d3a5c82 | 260 | }\r |
261 | \r | |
262 | if ((DataPtr + DataSize) >= ((UINTN) ((UINT8 *) VolatileBase + VolatileBase->Size))) {\r | |
263 | return EFI_INVALID_PARAMETER;\r | |
264 | }\r | |
c6492839 | 265 | \r |
266 | //\r | |
267 | // If Volatile Variable just do a simple mem copy.\r | |
268 | // \r | |
269 | CopyMem ((UINT8 *)(UINTN)DataPtr, Buffer, DataSize);\r | |
8d3a5c82 | 270 | return EFI_SUCCESS;\r |
271 | }\r | |
c6492839 | 272 | \r |
8d3a5c82 | 273 | //\r |
274 | // If we are here we are dealing with Non-Volatile Variables\r | |
275 | //\r | |
276 | LinearOffset = (UINTN) FwVolHeader;\r | |
277 | CurrWritePtr = (UINTN) DataPtr;\r | |
278 | CurrWriteSize = DataSize;\r | |
279 | CurrBuffer = Buffer;\r | |
280 | LbaNumber = 0;\r | |
281 | \r | |
282 | if (CurrWritePtr < LinearOffset) {\r | |
283 | return EFI_INVALID_PARAMETER;\r | |
284 | }\r | |
285 | \r | |
286 | for (PtrBlockMapEntry = FwVolHeader->BlockMap; PtrBlockMapEntry->NumBlocks != 0; PtrBlockMapEntry++) {\r | |
287 | for (BlockIndex2 = 0; BlockIndex2 < PtrBlockMapEntry->NumBlocks; BlockIndex2++) {\r | |
288 | //\r | |
289 | // Check to see if the Variable Writes are spanning through multiple\r | |
290 | // blocks.\r | |
291 | //\r | |
292 | if ((CurrWritePtr >= LinearOffset) && (CurrWritePtr < LinearOffset + PtrBlockMapEntry->Length)) {\r | |
293 | if ((CurrWritePtr + CurrWriteSize) <= (LinearOffset + PtrBlockMapEntry->Length)) {\r | |
294 | Status = EfiFvbWriteBlock (\r | |
295 | Instance,\r | |
296 | LbaNumber,\r | |
297 | (UINTN) (CurrWritePtr - LinearOffset),\r | |
298 | &CurrWriteSize,\r | |
299 | CurrBuffer\r | |
300 | );\r | |
8d3a5c82 | 301 | return Status;\r |
8d3a5c82 | 302 | } else {\r |
303 | Size = (UINT32) (LinearOffset + PtrBlockMapEntry->Length - CurrWritePtr);\r | |
304 | Status = EfiFvbWriteBlock (\r | |
305 | Instance,\r | |
306 | LbaNumber,\r | |
307 | (UINTN) (CurrWritePtr - LinearOffset),\r | |
308 | &Size,\r | |
309 | CurrBuffer\r | |
310 | );\r | |
311 | if (EFI_ERROR (Status)) {\r | |
312 | return Status;\r | |
313 | }\r | |
314 | \r | |
315 | CurrWritePtr = LinearOffset + PtrBlockMapEntry->Length;\r | |
316 | CurrBuffer = CurrBuffer + Size;\r | |
317 | CurrWriteSize = CurrWriteSize - Size;\r | |
318 | }\r | |
319 | }\r | |
320 | \r | |
321 | LinearOffset += PtrBlockMapEntry->Length;\r | |
322 | LbaNumber++;\r | |
323 | }\r | |
324 | }\r | |
325 | \r | |
326 | return EFI_SUCCESS;\r | |
327 | }\r | |
328 | \r | |
052ad7e1 | 329 | \r |
8d3a5c82 | 330 | VARIABLE_STORE_STATUS\r |
8d3a5c82 | 331 | GetVariableStoreStatus (\r |
332 | IN VARIABLE_STORE_HEADER *VarStoreHeader\r | |
333 | )\r | |
334 | /*++\r | |
335 | \r | |
336 | Routine Description:\r | |
337 | \r | |
338 | This code gets the current status of Variable Store.\r | |
339 | \r | |
340 | Arguments:\r | |
341 | \r | |
342 | VarStoreHeader Pointer to the Variable Store Header.\r | |
343 | \r | |
344 | Returns:\r | |
345 | \r | |
346 | EfiRaw Variable store status is raw\r | |
347 | EfiValid Variable store status is valid\r | |
348 | EfiInvalid Variable store status is invalid\r | |
349 | \r | |
350 | --*/\r | |
351 | {\r | |
352 | if (VarStoreHeader->Signature == VARIABLE_STORE_SIGNATURE &&\r | |
353 | VarStoreHeader->Format == VARIABLE_STORE_FORMATTED &&\r | |
354 | VarStoreHeader->State == VARIABLE_STORE_HEALTHY\r | |
355 | ) {\r | |
356 | \r | |
357 | return EfiValid;\r | |
358 | } else if (VarStoreHeader->Signature == 0xffffffff &&\r | |
359 | VarStoreHeader->Size == 0xffffffff &&\r | |
360 | VarStoreHeader->Format == 0xff &&\r | |
361 | VarStoreHeader->State == 0xff\r | |
362 | ) {\r | |
363 | \r | |
364 | return EfiRaw;\r | |
365 | } else {\r | |
366 | return EfiInvalid;\r | |
367 | }\r | |
368 | }\r | |
369 | \r | |
130e2569 | 370 | \r |
371 | UINTN\r | |
372 | NameSizeOfVariable (\r | |
373 | IN VARIABLE_HEADER *Variable\r | |
374 | )\r | |
375 | /*++\r | |
376 | \r | |
377 | Routine Description:\r | |
378 | \r | |
379 | This code gets the size of name of variable.\r | |
380 | \r | |
381 | Arguments:\r | |
382 | \r | |
383 | Variable Pointer to the Variable Header.\r | |
384 | \r | |
385 | Returns:\r | |
386 | \r | |
387 | UINTN Size of variable in bytes\r | |
388 | \r | |
389 | --*/\r | |
390 | {\r | |
391 | if (Variable->State == (UINT8) (-1) ||\r | |
392 | Variable->DataSize == (UINT32) -1 ||\r | |
393 | Variable->NameSize == (UINT32) -1 ||\r | |
394 | Variable->Attributes == (UINT32) -1) {\r | |
395 | return 0;\r | |
396 | }\r | |
397 | return (UINTN) Variable->NameSize;\r | |
398 | }\r | |
399 | \r | |
400 | UINTN\r | |
401 | DataSizeOfVariable (\r | |
402 | IN VARIABLE_HEADER *Variable\r | |
403 | )\r | |
404 | /*++\r | |
405 | \r | |
406 | Routine Description:\r | |
407 | \r | |
408 | This code gets the size of name of variable.\r | |
409 | \r | |
410 | Arguments:\r | |
411 | \r | |
412 | Variable Pointer to the Variable Header.\r | |
413 | \r | |
414 | Returns:\r | |
415 | \r | |
416 | UINTN Size of variable in bytes\r | |
417 | \r | |
418 | --*/\r | |
419 | {\r | |
420 | if (Variable->State == (UINT8) -1 ||\r | |
421 | Variable->DataSize == (UINT32) -1 ||\r | |
422 | Variable->NameSize == (UINT32) -1 ||\r | |
423 | Variable->Attributes == (UINT32) -1) {\r | |
424 | return 0;\r | |
425 | }\r | |
426 | return (UINTN) Variable->DataSize;\r | |
427 | }\r | |
428 | \r | |
429 | CHAR16 *\r | |
430 | GetVariableNamePtr (\r | |
431 | IN VARIABLE_HEADER *Variable\r | |
432 | )\r | |
433 | /*++\r | |
434 | \r | |
435 | Routine Description:\r | |
436 | \r | |
437 | This code gets the pointer to the variable name.\r | |
438 | \r | |
439 | Arguments:\r | |
440 | \r | |
441 | Variable Pointer to the Variable Header.\r | |
442 | \r | |
443 | Returns:\r | |
444 | \r | |
445 | CHAR16* Pointer to Variable Name\r | |
446 | \r | |
447 | --*/\r | |
448 | {\r | |
449 | \r | |
450 | return (CHAR16 *) (Variable + 1);\r | |
451 | }\r | |
452 | \r | |
8d3a5c82 | 453 | UINT8 *\r |
8d3a5c82 | 454 | GetVariableDataPtr (\r |
455 | IN VARIABLE_HEADER *Variable\r | |
456 | )\r | |
457 | /*++\r | |
458 | \r | |
459 | Routine Description:\r | |
460 | \r | |
461 | This code gets the pointer to the variable data.\r | |
462 | \r | |
463 | Arguments:\r | |
464 | \r | |
465 | Variable Pointer to the Variable Header.\r | |
466 | \r | |
467 | Returns:\r | |
468 | \r | |
469 | UINT8* Pointer to Variable Data\r | |
470 | \r | |
471 | --*/\r | |
472 | {\r | |
130e2569 | 473 | UINTN Value;\r |
474 | \r | |
8d3a5c82 | 475 | //\r |
476 | // Be careful about pad size for alignment\r | |
477 | //\r | |
130e2569 | 478 | Value = (UINTN) GetVariableNamePtr (Variable);\r |
479 | Value += NameSizeOfVariable (Variable);\r | |
480 | Value += GET_PAD_SIZE (NameSizeOfVariable (Variable));\r | |
481 | \r | |
482 | return (UINT8 *) Value;\r | |
8d3a5c82 | 483 | }\r |
484 | \r | |
052ad7e1 | 485 | \r |
8d3a5c82 | 486 | VARIABLE_HEADER *\r |
8d3a5c82 | 487 | GetNextVariablePtr (\r |
488 | IN VARIABLE_HEADER *Variable\r | |
489 | )\r | |
490 | /*++\r | |
491 | \r | |
492 | Routine Description:\r | |
493 | \r | |
494 | This code gets the pointer to the next variable header.\r | |
495 | \r | |
496 | Arguments:\r | |
497 | \r | |
498 | Variable Pointer to the Variable Header.\r | |
499 | \r | |
500 | Returns:\r | |
501 | \r | |
502 | VARIABLE_HEADER* Pointer to next variable header.\r | |
503 | \r | |
504 | --*/\r | |
505 | {\r | |
130e2569 | 506 | UINTN Value;\r |
507 | \r | |
8d3a5c82 | 508 | if (!IsValidVariableHeader (Variable)) {\r |
509 | return NULL;\r | |
510 | }\r | |
130e2569 | 511 | \r |
512 | Value = (UINTN) GetVariableDataPtr (Variable);\r | |
513 | Value += DataSizeOfVariable (Variable);\r | |
514 | Value += GET_PAD_SIZE (DataSizeOfVariable (Variable));\r | |
515 | \r | |
8d3a5c82 | 516 | //\r |
517 | // Be careful about pad size for alignment\r | |
518 | //\r | |
130e2569 | 519 | return (VARIABLE_HEADER *) HEADER_ALIGN (Value);\r |
8d3a5c82 | 520 | }\r |
521 | \r | |
9cad030b | 522 | VARIABLE_HEADER *\r |
523 | GetStartPointer (\r | |
524 | IN VARIABLE_STORE_HEADER *VarStoreHeader\r | |
525 | )\r | |
526 | /*++\r | |
527 | \r | |
528 | Routine Description:\r | |
529 | \r | |
530 | This code gets the pointer to the first variable memory pointer byte\r | |
531 | \r | |
532 | Arguments:\r | |
533 | \r | |
534 | VarStoreHeader Pointer to the Variable Store Header.\r | |
535 | \r | |
536 | Returns:\r | |
537 | \r | |
538 | VARIABLE_HEADER* Pointer to last unavailable Variable Header\r | |
539 | \r | |
540 | --*/\r | |
541 | {\r | |
542 | //\r | |
543 | // The end of variable store\r | |
544 | //\r | |
545 | return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1);\r | |
546 | }\r | |
052ad7e1 | 547 | \r |
8d3a5c82 | 548 | VARIABLE_HEADER *\r |
8d3a5c82 | 549 | GetEndPointer (\r |
550 | IN VARIABLE_STORE_HEADER *VarStoreHeader\r | |
551 | )\r | |
552 | /*++\r | |
553 | \r | |
554 | Routine Description:\r | |
555 | \r | |
556 | This code gets the pointer to the last variable memory pointer byte\r | |
557 | \r | |
558 | Arguments:\r | |
559 | \r | |
560 | VarStoreHeader Pointer to the Variable Store Header.\r | |
561 | \r | |
562 | Returns:\r | |
563 | \r | |
564 | VARIABLE_HEADER* Pointer to last unavailable Variable Header\r | |
565 | \r | |
566 | --*/\r | |
567 | {\r | |
568 | //\r | |
569 | // The end of variable store\r | |
570 | //\r | |
9cad030b | 571 | return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r |
8d3a5c82 | 572 | }\r |
573 | \r | |
052ad7e1 | 574 | \r |
8d3a5c82 | 575 | EFI_STATUS\r |
8d3a5c82 | 576 | Reclaim (\r |
577 | IN EFI_PHYSICAL_ADDRESS VariableBase,\r | |
578 | OUT UINTN *LastVariableOffset,\r | |
814bae52 | 579 | IN BOOLEAN IsVolatile,\r |
580 | IN VARIABLE_HEADER *UpdatingVariable\r | |
8d3a5c82 | 581 | )\r |
582 | /*++\r | |
583 | \r | |
584 | Routine Description:\r | |
585 | \r | |
586 | Variable store garbage collection and reclaim operation\r | |
587 | \r | |
588 | Arguments:\r | |
589 | \r | |
590 | VariableBase Base address of variable store\r | |
591 | LastVariableOffset Offset of last variable\r | |
592 | IsVolatile The variable store is volatile or not,\r | |
593 | if it is non-volatile, need FTW\r | |
594 | \r | |
595 | Returns:\r | |
596 | \r | |
597 | EFI STATUS\r | |
598 | \r | |
599 | --*/\r | |
600 | {\r | |
601 | VARIABLE_HEADER *Variable;\r | |
814bae52 | 602 | VARIABLE_HEADER *AddedVariable;\r |
8d3a5c82 | 603 | VARIABLE_HEADER *NextVariable;\r |
814bae52 | 604 | VARIABLE_HEADER *NextAddedVariable;\r |
8d3a5c82 | 605 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r |
606 | UINT8 *ValidBuffer;\r | |
814bae52 | 607 | UINTN MaximumBufferSize;\r |
8d3a5c82 | 608 | UINTN VariableSize;\r |
814bae52 | 609 | UINTN NameSize;\r |
8d3a5c82 | 610 | UINT8 *CurrPtr;\r |
814bae52 | 611 | VOID *Point0;\r |
612 | VOID *Point1;\r | |
613 | BOOLEAN FoundAdded;\r | |
8d3a5c82 | 614 | EFI_STATUS Status;\r |
615 | \r | |
616 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) VariableBase);\r | |
617 | \r | |
618 | //\r | |
619 | // Start Pointers for the variable.\r | |
620 | //\r | |
814bae52 | 621 | Variable = GetStartPointer (VariableStoreHeader);\r |
622 | MaximumBufferSize = sizeof (VARIABLE_STORE_HEADER);\r | |
8d3a5c82 | 623 | \r |
624 | while (IsValidVariableHeader (Variable)) {\r | |
625 | NextVariable = GetNextVariablePtr (Variable);\r | |
814bae52 | 626 | if (Variable->State == VAR_ADDED || \r |
627 | Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r | |
628 | ) {\r | |
8d3a5c82 | 629 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r |
814bae52 | 630 | MaximumBufferSize += VariableSize;\r |
8d3a5c82 | 631 | }\r |
632 | \r | |
633 | Variable = NextVariable;\r | |
634 | }\r | |
635 | \r | |
814bae52 | 636 | //\r |
637 | // Reserve the 1 Bytes with Oxff to identify the \r | |
638 | // end of the variable buffer. \r | |
639 | // \r | |
640 | MaximumBufferSize += 1;\r | |
641 | ValidBuffer = AllocatePool (MaximumBufferSize);\r | |
8d3a5c82 | 642 | if (ValidBuffer == NULL) {\r |
643 | return EFI_OUT_OF_RESOURCES;\r | |
644 | }\r | |
645 | \r | |
814bae52 | 646 | SetMem (ValidBuffer, MaximumBufferSize, 0xff);\r |
8d3a5c82 | 647 | \r |
648 | //\r | |
649 | // Copy variable store header\r | |
650 | //\r | |
814bae52 | 651 | CopyMem (ValidBuffer, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r |
652 | CurrPtr = (UINT8 *) GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r | |
8d3a5c82 | 653 | \r |
654 | //\r | |
655 | // Start Pointers for the variable.\r | |
656 | //\r | |
8d3a5c82 | 657 | \r |
814bae52 | 658 | //\r |
659 | // Reinstall all ADDED variables\r | |
660 | // \r | |
661 | Variable = GetStartPointer (VariableStoreHeader);\r | |
8d3a5c82 | 662 | while (IsValidVariableHeader (Variable)) {\r |
663 | NextVariable = GetNextVariablePtr (Variable);\r | |
664 | if (Variable->State == VAR_ADDED) {\r | |
665 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r | |
666 | CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r | |
667 | CurrPtr += VariableSize;\r | |
668 | }\r | |
669 | \r | |
670 | Variable = NextVariable;\r | |
671 | }\r | |
814bae52 | 672 | //\r |
673 | // Reinstall all in delete transition variables\r | |
674 | // \r | |
675 | Variable = GetStartPointer (VariableStoreHeader);\r | |
676 | while (IsValidVariableHeader (Variable)) {\r | |
677 | NextVariable = GetNextVariablePtr (Variable);\r | |
678 | if (Variable->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r | |
679 | \r | |
680 | //\r | |
681 | // Buffer has cached all ADDED variable. \r | |
682 | // Per IN_DELETED variable, we have to guarantee that\r | |
683 | // no ADDED one in previous buffer. \r | |
684 | // \r | |
685 | \r | |
686 | FoundAdded = FALSE;\r | |
687 | AddedVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) ValidBuffer);\r | |
688 | while (IsValidVariableHeader (AddedVariable)) {\r | |
689 | NextAddedVariable = GetNextVariablePtr (AddedVariable);\r | |
690 | NameSize = NameSizeOfVariable (AddedVariable);\r | |
691 | if (CompareGuid (&AddedVariable->VendorGuid, &Variable->VendorGuid) &&\r | |
692 | NameSize == NameSizeOfVariable (Variable)\r | |
693 | ) {\r | |
694 | Point0 = (VOID *) GetVariableNamePtr (AddedVariable);\r | |
695 | Point1 = (VOID *) GetVariableNamePtr (Variable);\r | |
696 | if (!CompareMem (\r | |
697 | Point0,\r | |
698 | Point1,\r | |
699 | NameSizeOfVariable (AddedVariable)\r | |
700 | )\r | |
701 | ) {\r | |
702 | FoundAdded = TRUE;\r | |
703 | break;\r | |
704 | }\r | |
705 | }\r | |
706 | AddedVariable = NextAddedVariable;\r | |
707 | }\r | |
708 | if (!FoundAdded) {\r | |
709 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r | |
710 | CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r | |
711 | if (Variable != UpdatingVariable) {\r | |
712 | ((VARIABLE_HEADER *) CurrPtr)->State = VAR_ADDED;\r | |
713 | }\r | |
714 | CurrPtr += VariableSize;\r | |
715 | }\r | |
716 | }\r | |
717 | \r | |
718 | Variable = NextVariable;\r | |
719 | }\r | |
8d3a5c82 | 720 | \r |
721 | if (IsVolatile) {\r | |
722 | //\r | |
723 | // If volatile variable store, just copy valid buffer\r | |
724 | //\r | |
725 | SetMem ((UINT8 *) (UINTN) VariableBase, VariableStoreHeader->Size, 0xff);\r | |
814bae52 | 726 | CopyMem ((UINT8 *) (UINTN) VariableBase, ValidBuffer, (UINTN) (CurrPtr - (UINT8 *) ValidBuffer));\r |
8d3a5c82 | 727 | Status = EFI_SUCCESS;\r |
728 | } else {\r | |
729 | //\r | |
730 | // If non-volatile variable store, perform FTW here.\r | |
731 | //\r | |
732 | Status = FtwVariableSpace (\r | |
733 | VariableBase,\r | |
734 | ValidBuffer,\r | |
814bae52 | 735 | (UINTN) (CurrPtr - (UINT8 *) ValidBuffer)\r |
8d3a5c82 | 736 | );\r |
8d3a5c82 | 737 | }\r |
814bae52 | 738 | if (!EFI_ERROR (Status)) {\r |
739 | *LastVariableOffset = (UINTN) (CurrPtr - (UINT8 *) ValidBuffer);\r | |
740 | } else {\r | |
8d3a5c82 | 741 | *LastVariableOffset = 0;\r |
742 | }\r | |
743 | \r | |
814bae52 | 744 | FreePool (ValidBuffer);\r |
745 | \r | |
8d3a5c82 | 746 | return Status;\r |
747 | }\r | |
748 | \r | |
33a5a666 A |
749 | \r |
750 | //\r | |
751 | // The current Hii implementation accesses this variable a larg # of times on every boot.\r | |
752 | // Other common variables are only accessed a single time. This is why this cache algorithm\r | |
753 | // only targets a single variable. Probably to get an performance improvement out of\r | |
754 | // a Cache you would need a cache that improves the search performance for a variable.\r | |
755 | //\r | |
756 | VARIABLE_CACHE_ENTRY mVariableCache[] = {\r | |
757 | {\r | |
758 | &gEfiGlobalVariableGuid,\r | |
759 | L"Lang",\r | |
760 | 0x00000000,\r | |
761 | 0x00,\r | |
762 | NULL\r | |
763 | }\r | |
764 | };\r | |
765 | \r | |
052ad7e1 A |
766 | \r |
767 | /**\r | |
768 | Update the Cache with Variable information. These are the same \r | |
769 | arguments as the EFI Variable services.\r | |
770 | \r | |
771 | @param[in] VariableName Name of variable\r | |
772 | @param[in] VendorGuid Guid of variable\r | |
773 | @param[in] Attribute Attribue of the variable\r | |
774 | @param[in] DataSize Size of data. 0 means delete\r | |
775 | @param[in] Data Variable data\r | |
776 | \r | |
777 | **/\r | |
33a5a666 A |
778 | VOID\r |
779 | UpdateVariableCache (\r | |
780 | IN CHAR16 *VariableName,\r | |
781 | IN EFI_GUID *VendorGuid,\r | |
052ad7e1 A |
782 | IN UINT32 Attributes,\r |
783 | IN UINTN DataSize,\r | |
784 | IN VOID *Data\r | |
33a5a666 A |
785 | )\r |
786 | {\r | |
787 | VARIABLE_CACHE_ENTRY *Entry;\r | |
788 | UINTN Index;\r | |
789 | \r | |
790 | if (EfiAtRuntime ()) {\r | |
791 | // Don't use the cache at runtime\r | |
792 | return;\r | |
793 | }\r | |
794 | \r | |
795 | for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {\r | |
796 | if (CompareGuid (VendorGuid, Entry->Guid)) {\r | |
797 | if (StrCmp (VariableName, Entry->Name) == 0) { \r | |
798 | Entry->Attributes = Attributes;\r | |
799 | if (DataSize == 0) {\r | |
800 | // Delete Case\r | |
801 | if (Entry->DataSize != 0) {\r | |
802 | FreePool (Entry->Data);\r | |
803 | }\r | |
804 | Entry->DataSize = DataSize;\r | |
805 | } else if (DataSize == Entry->DataSize) {\r | |
806 | CopyMem (Entry->Data, Data, DataSize);\r | |
807 | } else {\r | |
808 | Entry->Data = AllocatePool (DataSize);\r | |
809 | Entry->DataSize = DataSize;\r | |
810 | CopyMem (Entry->Data, Data, DataSize);\r | |
811 | }\r | |
812 | }\r | |
813 | }\r | |
814 | }\r | |
815 | }\r | |
816 | \r | |
817 | \r | |
052ad7e1 A |
818 | /**\r |
819 | Search the cache to see if the variable is in the cache.\r | |
820 | \r | |
821 | @param[in] VariableName Name of variable\r | |
822 | @param[in] VendorGuid Guid of variable\r | |
823 | @param[in] Attribute Attribue returned \r | |
824 | @param[in] DataSize Size of data returned\r | |
825 | @param[in] Data Variable data returned\r | |
826 | \r | |
827 | @retval EFI_SUCCESS VariableGuid & VariableName data was returned.\r | |
828 | @retval other Not found.\r | |
829 | \r | |
830 | **/\r | |
33a5a666 A |
831 | EFI_STATUS\r |
832 | FindVariableInCache (\r | |
833 | IN CHAR16 *VariableName,\r | |
834 | IN EFI_GUID *VendorGuid,\r | |
835 | OUT UINT32 *Attributes OPTIONAL,\r | |
836 | IN OUT UINTN *DataSize,\r | |
837 | OUT VOID *Data\r | |
838 | )\r | |
839 | {\r | |
840 | VARIABLE_CACHE_ENTRY *Entry;\r | |
841 | UINTN Index;\r | |
842 | \r | |
843 | if (EfiAtRuntime ()) {\r | |
844 | // Don't use the cache at runtime\r | |
845 | return EFI_NOT_FOUND;\r | |
846 | }\r | |
847 | \r | |
848 | for (Index = 0, Entry = mVariableCache; Index < sizeof (mVariableCache)/sizeof (VARIABLE_CACHE_ENTRY); Index++, Entry++) {\r | |
849 | if (CompareGuid (VendorGuid, Entry->Guid)) {\r | |
850 | if (StrCmp (VariableName, Entry->Name) == 0) {\r | |
851 | if (Entry->DataSize == 0) {\r | |
052ad7e1 | 852 | // Variable was deleted so return not found\r |
33a5a666 | 853 | return EFI_NOT_FOUND;\r |
aa09397b | 854 | } else if (Entry->DataSize > *DataSize) {\r |
052ad7e1 | 855 | // If the buffer is too small return correct size\r |
33a5a666 A |
856 | *DataSize = Entry->DataSize;\r |
857 | return EFI_BUFFER_TOO_SMALL;\r | |
858 | } else {\r | |
aa09397b | 859 | *DataSize = Entry->DataSize;\r |
052ad7e1 | 860 | // Return the data\r |
33a5a666 A |
861 | CopyMem (Data, Entry->Data, Entry->DataSize);\r |
862 | if (Attributes != NULL) {\r | |
863 | *Attributes = Entry->Attributes;\r | |
864 | }\r | |
865 | return EFI_SUCCESS;\r | |
866 | }\r | |
867 | }\r | |
868 | }\r | |
869 | }\r | |
870 | \r | |
871 | return EFI_NOT_FOUND;\r | |
872 | }\r | |
873 | \r | |
874 | \r | |
8d3a5c82 | 875 | EFI_STATUS\r |
8d3a5c82 | 876 | FindVariable (\r |
877 | IN CHAR16 *VariableName,\r | |
878 | IN EFI_GUID *VendorGuid,\r | |
879 | OUT VARIABLE_POINTER_TRACK *PtrTrack,\r | |
880 | IN VARIABLE_GLOBAL *Global\r | |
881 | )\r | |
882 | /*++\r | |
883 | \r | |
884 | Routine Description:\r | |
885 | \r | |
886 | This code finds variable in storage blocks (Volatile or Non-Volatile)\r | |
887 | \r | |
888 | Arguments:\r | |
889 | \r | |
890 | VariableName Name of the variable to be found\r | |
891 | VendorGuid Vendor GUID to be found.\r | |
892 | PtrTrack Variable Track Pointer structure that contains\r | |
893 | Variable Information.\r | |
894 | Contains the pointer of Variable header.\r | |
895 | Global VARIABLE_GLOBAL pointer\r | |
896 | \r | |
897 | Returns:\r | |
898 | \r | |
899 | EFI STATUS\r | |
900 | \r | |
901 | --*/\r | |
902 | {\r | |
814bae52 | 903 | VARIABLE_HEADER *Variable[2];\r |
904 | VARIABLE_HEADER *InDeletedVariable;\r | |
905 | VARIABLE_STORE_HEADER *VariableStoreHeader[2];\r | |
906 | UINTN InDeletedStorageIndex;\r | |
907 | UINTN Index;\r | |
908 | VOID *Point;\r | |
8d3a5c82 | 909 | \r |
8d3a5c82 | 910 | //\r |
33a5a666 | 911 | // 0: Volatile, 1: Non-Volatile\r |
36873a61 | 912 | // The index and attributes mapping must be kept in this order as RuntimeServiceGetNextVariableName\r |
913 | // make use of this mapping to implement search algorithme.\r | |
8d3a5c82 | 914 | //\r |
052ad7e1 A |
915 | VariableStoreHeader[0] = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r |
916 | VariableStoreHeader[1] = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r | |
8d3a5c82 | 917 | \r |
918 | //\r | |
919 | // Start Pointers for the variable.\r | |
920 | // Actual Data Pointer where data can be written.\r | |
921 | //\r | |
9cad030b | 922 | Variable[0] = GetStartPointer (VariableStoreHeader[0]);\r |
923 | Variable[1] = GetStartPointer (VariableStoreHeader[1]);\r | |
8d3a5c82 | 924 | \r |
925 | if (VariableName[0] != 0 && VendorGuid == NULL) {\r | |
926 | return EFI_INVALID_PARAMETER;\r | |
927 | }\r | |
814bae52 | 928 | \r |
8d3a5c82 | 929 | //\r |
33a5a666 | 930 | // Find the variable by walk through volatile and then non-volatile variable store\r |
8d3a5c82 | 931 | //\r |
814bae52 | 932 | InDeletedVariable = NULL;\r |
933 | InDeletedStorageIndex = 0;\r | |
8d3a5c82 | 934 | for (Index = 0; Index < 2; Index++) {\r |
8d3a5c82 | 935 | while (IsValidVariableHeader (Variable[Index]) && (Variable[Index] <= GetEndPointer (VariableStoreHeader[Index]))) {\r |
814bae52 | 936 | if (Variable[Index]->State == VAR_ADDED || \r |
937 | Variable[Index]->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)\r | |
938 | ) {\r | |
c6492839 | 939 | if (!EfiAtRuntime () || (Variable[Index]->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r |
8d3a5c82 | 940 | if (VariableName[0] == 0) {\r |
814bae52 | 941 | if (Variable[Index]->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r |
942 | InDeletedVariable = Variable[Index];\r | |
943 | InDeletedStorageIndex = Index;\r | |
944 | } else {\r | |
945 | PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Index]);\r | |
946 | PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);\r | |
947 | PtrTrack->CurrPtr = Variable[Index];\r | |
948 | PtrTrack->Volatile = (BOOLEAN)(Index == 0);\r | |
949 | \r | |
950 | return EFI_SUCCESS;\r | |
951 | }\r | |
8d3a5c82 | 952 | } else {\r |
953 | if (CompareGuid (VendorGuid, &Variable[Index]->VendorGuid)) {\r | |
130e2569 | 954 | Point = (VOID *) GetVariableNamePtr (Variable[Index]);\r |
955 | \r | |
956 | ASSERT (NameSizeOfVariable (Variable[Index]) != 0);\r | |
957 | if (!CompareMem (VariableName, Point, NameSizeOfVariable (Variable[Index]))) {\r | |
814bae52 | 958 | if (Variable[Index]->State == (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) {\r |
959 | InDeletedVariable = Variable[Index];\r | |
960 | InDeletedStorageIndex = Index;\r | |
961 | } else {\r | |
962 | PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[Index]);\r | |
963 | PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[Index]);\r | |
964 | PtrTrack->CurrPtr = Variable[Index];\r | |
965 | PtrTrack->Volatile = (BOOLEAN)(Index == 0);\r | |
966 | \r | |
967 | return EFI_SUCCESS;\r | |
968 | }\r | |
8d3a5c82 | 969 | }\r |
970 | }\r | |
971 | }\r | |
972 | }\r | |
973 | }\r | |
974 | \r | |
975 | Variable[Index] = GetNextVariablePtr (Variable[Index]);\r | |
976 | }\r | |
814bae52 | 977 | if (InDeletedVariable != NULL) {\r |
978 | PtrTrack->StartPtr = GetStartPointer (VariableStoreHeader[InDeletedStorageIndex]);\r | |
979 | PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader[InDeletedStorageIndex]);\r | |
980 | PtrTrack->CurrPtr = InDeletedVariable;\r | |
981 | PtrTrack->Volatile = (BOOLEAN)(InDeletedStorageIndex == 0);\r | |
982 | return EFI_SUCCESS;\r | |
983 | }\r | |
8d3a5c82 | 984 | }\r |
8d3a5c82 | 985 | PtrTrack->CurrPtr = NULL;\r |
986 | return EFI_NOT_FOUND;\r | |
987 | }\r | |
988 | \r | |
33a5a666 | 989 | \r |
052ad7e1 | 990 | \r |
8d3a5c82 | 991 | /*++\r |
992 | \r | |
993 | Routine Description:\r | |
994 | \r | |
995 | This code finds variable in storage blocks (Volatile or Non-Volatile)\r | |
996 | \r | |
997 | Arguments:\r | |
998 | \r | |
c6492839 | 999 | VariableName Name of Variable to be found\r |
1000 | VendorGuid Variable vendor GUID\r | |
1001 | Attributes OPTIONAL Attribute value of the variable found\r | |
1002 | DataSize Size of Data found. If size is less than the\r | |
1003 | data, this value contains the required size.\r | |
1004 | Data Data pointer\r | |
1005 | Global Pointer to VARIABLE_GLOBAL structure\r | |
1006 | Instance Instance of the Firmware Volume.\r | |
8d3a5c82 | 1007 | \r |
1008 | Returns:\r | |
1009 | \r | |
c6492839 | 1010 | EFI_INVALID_PARAMETER - Invalid parameter\r |
1011 | EFI_SUCCESS - Find the specified variable\r | |
1012 | EFI_NOT_FOUND - Not found\r | |
1013 | EFI_BUFFER_TO_SMALL - DataSize is too small for the result\r | |
1014 | \r | |
8d3a5c82 | 1015 | \r |
1016 | --*/\r | |
052ad7e1 A |
1017 | EFI_STATUS\r |
1018 | EFIAPI\r | |
1019 | RuntimeServiceGetVariable (\r | |
1020 | IN CHAR16 *VariableName,\r | |
1021 | IN EFI_GUID *VendorGuid,\r | |
1022 | OUT UINT32 *Attributes OPTIONAL,\r | |
1023 | IN OUT UINTN *DataSize,\r | |
1024 | OUT VOID *Data\r | |
1025 | )\r | |
8d3a5c82 | 1026 | {\r |
052ad7e1 | 1027 | EFI_STATUS Status;\r |
8d3a5c82 | 1028 | VARIABLE_POINTER_TRACK Variable;\r |
1029 | UINTN VarDataSize;\r | |
8d3a5c82 | 1030 | \r |
1031 | if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r | |
1032 | return EFI_INVALID_PARAMETER;\r | |
1033 | }\r | |
33a5a666 | 1034 | \r |
fdb7765f | 1035 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
1036 | \r | |
8d3a5c82 | 1037 | //\r |
1038 | // Find existing variable\r | |
1039 | //\r | |
33a5a666 A |
1040 | Status = FindVariableInCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r |
1041 | if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)){\r | |
1042 | // Hit in the Cache\r | |
1043 | UpdateVariableInfo (VariableName, VendorGuid, FALSE, TRUE, FALSE, FALSE, TRUE);\r | |
fdb7765f | 1044 | goto Done;\r |
33a5a666 A |
1045 | }\r |
1046 | \r | |
052ad7e1 | 1047 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r |
8d3a5c82 | 1048 | if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r |
1049 | goto Done;\r | |
1050 | }\r | |
33a5a666 | 1051 | \r |
8d3a5c82 | 1052 | //\r |
1053 | // Get data size\r | |
1054 | //\r | |
130e2569 | 1055 | VarDataSize = DataSizeOfVariable (Variable.CurrPtr);\r |
fdb7765f | 1056 | ASSERT (VarDataSize != 0);\r |
1057 | \r | |
8d3a5c82 | 1058 | if (*DataSize >= VarDataSize) {\r |
1059 | if (Data == NULL) {\r | |
1060 | Status = EFI_INVALID_PARAMETER;\r | |
1061 | goto Done;\r | |
1062 | }\r | |
1063 | \r | |
1064 | CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r | |
1065 | if (Attributes != NULL) {\r | |
1066 | *Attributes = Variable.CurrPtr->Attributes;\r | |
1067 | }\r | |
1068 | \r | |
1069 | *DataSize = VarDataSize;\r | |
33a5a666 A |
1070 | UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE, FALSE, FALSE, FALSE);\r |
1071 | UpdateVariableCache (VariableName, VendorGuid, Variable.CurrPtr->Attributes, VarDataSize, Data);\r | |
1072 | \r | |
8d3a5c82 | 1073 | Status = EFI_SUCCESS;\r |
1074 | goto Done;\r | |
1075 | } else {\r | |
1076 | *DataSize = VarDataSize;\r | |
1077 | Status = EFI_BUFFER_TOO_SMALL;\r | |
1078 | goto Done;\r | |
1079 | }\r | |
1080 | \r | |
1081 | Done:\r | |
052ad7e1 | 1082 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8d3a5c82 | 1083 | return Status;\r |
1084 | }\r | |
1085 | \r | |
052ad7e1 A |
1086 | \r |
1087 | \r | |
8d3a5c82 | 1088 | /*++\r |
1089 | \r | |
1090 | Routine Description:\r | |
1091 | \r | |
1092 | This code Finds the Next available variable\r | |
1093 | \r | |
1094 | Arguments:\r | |
1095 | \r | |
1096 | VariableNameSize Size of the variable\r | |
1097 | VariableName Pointer to variable name\r | |
1098 | VendorGuid Variable Vendor Guid\r | |
1099 | Global VARIABLE_GLOBAL structure pointer.\r | |
1100 | Instance FV instance\r | |
1101 | \r | |
1102 | Returns:\r | |
1103 | \r | |
1104 | EFI STATUS\r | |
1105 | \r | |
1106 | --*/\r | |
052ad7e1 A |
1107 | EFI_STATUS\r |
1108 | EFIAPI\r | |
1109 | RuntimeServiceGetNextVariableName (\r | |
1110 | IN OUT UINTN *VariableNameSize,\r | |
1111 | IN OUT CHAR16 *VariableName,\r | |
1112 | IN OUT EFI_GUID *VendorGuid\r | |
1113 | )\r | |
8d3a5c82 | 1114 | {\r |
1115 | VARIABLE_POINTER_TRACK Variable;\r | |
1116 | UINTN VarNameSize;\r | |
1117 | EFI_STATUS Status;\r | |
1118 | \r | |
1119 | if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r | |
1120 | return EFI_INVALID_PARAMETER;\r | |
1121 | }\r | |
1122 | \r | |
fdb7765f | 1123 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
1124 | \r | |
052ad7e1 | 1125 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r |
8d3a5c82 | 1126 | if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r |
1127 | goto Done;\r | |
1128 | }\r | |
1129 | \r | |
1130 | if (VariableName[0] != 0) {\r | |
1131 | //\r | |
1132 | // If variable name is not NULL, get next variable\r | |
1133 | //\r | |
1134 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
1135 | }\r | |
1136 | \r | |
1137 | while (TRUE) {\r | |
1138 | //\r | |
1139 | // If both volatile and non-volatile variable store are parsed,\r | |
1140 | // return not found\r | |
1141 | //\r | |
1142 | if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {\r | |
1143 | Variable.Volatile = (BOOLEAN) (Variable.Volatile ^ ((BOOLEAN) 0x1));\r | |
36873a61 | 1144 | if (!Variable.Volatile) {\r |
9cad030b | 1145 | Variable.StartPtr = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r |
1146 | Variable.EndPtr = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase));\r | |
8d3a5c82 | 1147 | } else {\r |
1148 | Status = EFI_NOT_FOUND;\r | |
1149 | goto Done;\r | |
1150 | }\r | |
1151 | \r | |
1152 | Variable.CurrPtr = Variable.StartPtr;\r | |
1153 | if (!IsValidVariableHeader (Variable.CurrPtr)) {\r | |
1154 | continue;\r | |
1155 | }\r | |
1156 | }\r | |
1157 | //\r | |
1158 | // Variable is found\r | |
1159 | //\r | |
1160 | if (IsValidVariableHeader (Variable.CurrPtr) && Variable.CurrPtr->State == VAR_ADDED) {\r | |
1161 | if (!(EfiAtRuntime () && !(Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS))) {\r | |
130e2569 | 1162 | VarNameSize = NameSizeOfVariable (Variable.CurrPtr);\r |
fdb7765f | 1163 | ASSERT (VarNameSize != 0);\r |
1164 | \r | |
8d3a5c82 | 1165 | if (VarNameSize <= *VariableNameSize) {\r |
1166 | CopyMem (\r | |
1167 | VariableName,\r | |
130e2569 | 1168 | GetVariableNamePtr (Variable.CurrPtr),\r |
8d3a5c82 | 1169 | VarNameSize\r |
1170 | );\r | |
1171 | CopyMem (\r | |
1172 | VendorGuid,\r | |
1173 | &Variable.CurrPtr->VendorGuid,\r | |
1174 | sizeof (EFI_GUID)\r | |
1175 | );\r | |
1176 | Status = EFI_SUCCESS;\r | |
1177 | } else {\r | |
1178 | Status = EFI_BUFFER_TOO_SMALL;\r | |
1179 | }\r | |
1180 | \r | |
1181 | *VariableNameSize = VarNameSize;\r | |
1182 | goto Done;\r | |
1183 | }\r | |
1184 | }\r | |
1185 | \r | |
1186 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
1187 | }\r | |
1188 | \r | |
1189 | Done:\r | |
052ad7e1 | 1190 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8d3a5c82 | 1191 | return Status;\r |
1192 | }\r | |
1193 | \r | |
052ad7e1 | 1194 | \r |
8d3a5c82 | 1195 | /*++\r |
1196 | \r | |
1197 | Routine Description:\r | |
1198 | \r | |
1199 | This code sets variable in storage blocks (Volatile or Non-Volatile)\r | |
1200 | \r | |
1201 | Arguments:\r | |
1202 | \r | |
1203 | VariableName Name of Variable to be found\r | |
1204 | VendorGuid Variable vendor GUID\r | |
1205 | Attributes Attribute value of the variable found\r | |
1206 | DataSize Size of Data found. If size is less than the\r | |
1207 | data, this value contains the required size.\r | |
1208 | Data Data pointer\r | |
1209 | Global Pointer to VARIABLE_GLOBAL structure\r | |
1210 | VolatileOffset The offset of last volatile variable\r | |
1211 | NonVolatileOffset The offset of last non-volatile variable\r | |
1212 | Instance Instance of the Firmware Volume.\r | |
1213 | \r | |
1214 | Returns:\r | |
1215 | \r | |
8d3a5c82 | 1216 | EFI_INVALID_PARAMETER - Invalid parameter\r |
1217 | EFI_SUCCESS - Set successfully\r | |
1218 | EFI_OUT_OF_RESOURCES - Resource not enough to set variable\r | |
1219 | EFI_NOT_FOUND - Not found\r | |
c6492839 | 1220 | EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure\r |
1221 | EFI_WRITE_PROTECTED - Variable is read-only\r | |
8d3a5c82 | 1222 | \r |
1223 | --*/\r | |
052ad7e1 A |
1224 | EFI_STATUS\r |
1225 | EFIAPI\r | |
1226 | RuntimeServiceSetVariable (\r | |
1227 | IN CHAR16 *VariableName,\r | |
1228 | IN EFI_GUID *VendorGuid,\r | |
1229 | IN UINT32 Attributes,\r | |
1230 | IN UINTN DataSize,\r | |
1231 | IN VOID *Data\r | |
1232 | )\r | |
8d3a5c82 | 1233 | {\r |
1234 | VARIABLE_POINTER_TRACK Variable;\r | |
1235 | EFI_STATUS Status;\r | |
1236 | VARIABLE_HEADER *NextVariable;\r | |
1237 | UINTN VarNameSize;\r | |
1238 | UINTN VarNameOffset;\r | |
1239 | UINTN VarDataOffset;\r | |
1240 | UINTN VarSize;\r | |
1241 | UINT8 State;\r | |
1242 | BOOLEAN Reclaimed;\r | |
052ad7e1 A |
1243 | UINTN *VolatileOffset;\r |
1244 | UINTN *NonVolatileOffset;\r | |
1245 | UINT32 Instance;\r | |
fd51bf70 | 1246 | BOOLEAN Volatile;\r |
fdb7765f | 1247 | EFI_PHYSICAL_ADDRESS Point;\r |
8d3a5c82 | 1248 | \r |
c6492839 | 1249 | //\r |
1250 | // Check input parameters\r | |
1251 | //\r | |
8d3a5c82 | 1252 | if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r |
1253 | return EFI_INVALID_PARAMETER;\r | |
c6492839 | 1254 | } \r |
1255 | //\r | |
1256 | // Make sure if runtime bit is set, boot service bit is set also\r | |
1257 | //\r | |
1258 | if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r | |
1259 | return EFI_INVALID_PARAMETER;\r | |
8d3a5c82 | 1260 | }\r |
fdb7765f | 1261 | \r |
c6492839 | 1262 | //\r |
1263 | // The size of the VariableName, including the Unicode Null in bytes plus\r | |
1264 | // the DataSize is limited to maximum size of MAX_HARDWARE_ERROR_VARIABLE_SIZE (32K)\r | |
1265 | // bytes for HwErrRec, and MAX_VARIABLE_SIZE (1024) bytes for the others.\r | |
1266 | //\r | |
1267 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
1268 | if ((DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE) || \r | |
1269 | (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_HARDWARE_ERROR_VARIABLE_SIZE)) {\r | |
1270 | return EFI_INVALID_PARAMETER;\r | |
1271 | } \r | |
1272 | } else {\r | |
1273 | //\r | |
1274 | // The size of the VariableName, including the Unicode Null in bytes plus\r | |
1275 | // the DataSize is limited to maximum size of MAX_VARIABLE_SIZE (1024) bytes.\r | |
1276 | //\r | |
1277 | if ((DataSize > MAX_VARIABLE_SIZE) ||\r | |
1278 | (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > MAX_VARIABLE_SIZE)) {\r | |
1279 | return EFI_INVALID_PARAMETER;\r | |
1280 | } \r | |
1281 | } \r | |
fdb7765f | 1282 | \r |
1283 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r | |
1284 | \r | |
1285 | Reclaimed = FALSE;\r | |
1286 | Instance = mVariableModuleGlobal->FvbInstance;\r | |
1287 | VolatileOffset = &mVariableModuleGlobal->VolatileLastVariableOffset;\r | |
1288 | \r | |
1289 | //\r | |
1290 | // Consider reentrant in MCA/INIT/NMI. It needs be reupdated;\r | |
1291 | //\r | |
1292 | if (1 < InterlockedIncrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState)) {\r | |
fdb7765f | 1293 | Point = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase;;\r |
1294 | //\r | |
1295 | // Parse non-volatile variable data and get last variable offset\r | |
1296 | //\r | |
9cad030b | 1297 | NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) (UINTN) Point);\r |
fdb7765f | 1298 | while (IsValidVariableHeader (NextVariable)) {\r |
1299 | NextVariable = GetNextVariablePtr (NextVariable);\r | |
1300 | }\r | |
1301 | mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) Point;\r | |
1302 | }\r | |
1303 | \r | |
1304 | NonVolatileOffset = &mVariableModuleGlobal->NonVolatileLastVariableOffset;\r | |
1305 | \r | |
1306 | \r | |
c6492839 | 1307 | //\r |
1308 | // Check whether the input variable is already existed\r | |
1309 | //\r | |
1310 | \r | |
052ad7e1 | 1311 | Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal);\r |
c6492839 | 1312 | if (Status == EFI_SUCCESS && Variable.CurrPtr != NULL) {\r |
8d3a5c82 | 1313 | //\r |
c6492839 | 1314 | // Update/Delete existing variable\r |
8d3a5c82 | 1315 | //\r |
fd51bf70 | 1316 | Volatile = Variable.Volatile;\r |
c6492839 | 1317 | \r |
1318 | if (EfiAtRuntime ()) { \r | |
1319 | //\r | |
1320 | // If EfiAtRuntime and the variable is Volatile and Runtime Access, \r | |
1321 | // the volatile is ReadOnly, and SetVariable should be aborted and \r | |
1322 | // return EFI_WRITE_PROTECTED.\r | |
1323 | //\r | |
1324 | if (Variable.Volatile) {\r | |
1325 | Status = EFI_WRITE_PROTECTED;\r | |
1326 | goto Done;\r | |
1327 | }\r | |
1328 | //\r | |
1329 | // Only variable have NV attribute can be updated/deleted in Runtime\r | |
1330 | //\r | |
1331 | if (!(Variable.CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r | |
1332 | Status = EFI_INVALID_PARAMETER;\r | |
1333 | goto Done; \r | |
1334 | }\r | |
1335 | }\r | |
8d3a5c82 | 1336 | //\r |
c6492839 | 1337 | // Setting a data variable with no access, or zero DataSize attributes\r |
1338 | // specified causes it to be deleted.\r | |
8d3a5c82 | 1339 | //\r |
c6492839 | 1340 | if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) { \r |
1341 | State = Variable.CurrPtr->State;\r | |
1342 | State &= VAR_DELETED;\r | |
1343 | \r | |
1344 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1345 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1346 | Variable.Volatile,\r |
1347 | FALSE,\r | |
1348 | Instance,\r | |
1349 | (UINTN) &Variable.CurrPtr->State,\r | |
1350 | sizeof (UINT8),\r | |
1351 | &State\r | |
1352 | ); \r | |
33a5a666 | 1353 | if (!EFI_ERROR (Status)) {\r |
fd51bf70 | 1354 | UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, FALSE, TRUE, FALSE);\r |
33a5a666 A |
1355 | UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r |
1356 | }\r | |
c6492839 | 1357 | goto Done; \r |
1358 | }\r | |
8d3a5c82 | 1359 | //\r |
c6492839 | 1360 | // If the variable is marked valid and the same data has been passed in\r |
1361 | // then return to the caller immediately.\r | |
8d3a5c82 | 1362 | //\r |
130e2569 | 1363 | if (DataSizeOfVariable (Variable.CurrPtr) == DataSize &&\r |
c6492839 | 1364 | (CompareMem (Data, GetVariableDataPtr (Variable.CurrPtr), DataSize) == 0)) {\r |
33a5a666 | 1365 | \r |
fd51bf70 | 1366 | UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r |
c6492839 | 1367 | Status = EFI_SUCCESS;\r |
1368 | goto Done;\r | |
1369 | } else if ((Variable.CurrPtr->State == VAR_ADDED) ||\r | |
1370 | (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r | |
814bae52 | 1371 | \r |
c6492839 | 1372 | //\r |
1373 | // Mark the old variable as in delete transition\r | |
1374 | //\r | |
1375 | State = Variable.CurrPtr->State;\r | |
1376 | State &= VAR_IN_DELETED_TRANSITION;\r | |
1377 | \r | |
1378 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1379 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1380 | Variable.Volatile,\r |
1381 | FALSE,\r | |
1382 | Instance,\r | |
1383 | (UINTN) &Variable.CurrPtr->State,\r | |
1384 | sizeof (UINT8),\r | |
1385 | &State\r | |
1386 | ); \r | |
1387 | if (EFI_ERROR (Status)) {\r | |
1388 | goto Done; \r | |
33a5a666 | 1389 | } \r |
c6492839 | 1390 | } \r |
1391 | } else if (Status == EFI_NOT_FOUND) {\r | |
8d3a5c82 | 1392 | //\r |
c6492839 | 1393 | // Create a new variable\r |
1394 | // \r | |
1395 | \r | |
8d3a5c82 | 1396 | //\r |
c6492839 | 1397 | // Make sure we are trying to create a new variable.\r |
1398 | // Setting a data variable with no access, or zero DataSize attributes means to delete it. \r | |
8d3a5c82 | 1399 | //\r |
c6492839 | 1400 | if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r |
1401 | Status = EFI_NOT_FOUND;\r | |
1402 | goto Done;\r | |
1403 | }\r | |
1404 | \r | |
8d3a5c82 | 1405 | //\r |
c6492839 | 1406 | // Only variable have NV|RT attribute can be created in Runtime\r |
1407 | //\r | |
1408 | if (EfiAtRuntime () &&\r | |
1409 | (!(Attributes & EFI_VARIABLE_RUNTIME_ACCESS) || !(Attributes & EFI_VARIABLE_NON_VOLATILE))) {\r | |
1410 | Status = EFI_INVALID_PARAMETER;\r | |
1411 | goto Done;\r | |
1412 | } \r | |
1413 | } else {\r | |
8d3a5c82 | 1414 | //\r |
c6492839 | 1415 | // Status should be EFI_INVALID_PARAMETER here according to return status of FindVariable().\r |
8d3a5c82 | 1416 | //\r |
c6492839 | 1417 | ASSERT (Status == EFI_INVALID_PARAMETER);\r |
8d3a5c82 | 1418 | goto Done;\r |
c6492839 | 1419 | }\r |
1420 | \r | |
1421 | //\r | |
1422 | // Function part - create a new variable and copy the data.\r | |
1423 | // Both update a variable and create a variable will come here.\r | |
1424 | //\r | |
1425 | // Tricky part: Use scratch data area at the end of volatile variable store\r | |
1426 | // as a temporary storage.\r | |
1427 | //\r | |
052ad7e1 | 1428 | NextVariable = GetEndPointer ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase));\r |
c6492839 | 1429 | \r |
1430 | SetMem (NextVariable, SCRATCH_SIZE, 0xff);\r | |
1431 | \r | |
1432 | NextVariable->StartId = VARIABLE_DATA;\r | |
1433 | NextVariable->Attributes = Attributes;\r | |
1434 | //\r | |
1435 | // NextVariable->State = VAR_ADDED;\r | |
1436 | //\r | |
1437 | NextVariable->Reserved = 0;\r | |
1438 | VarNameOffset = sizeof (VARIABLE_HEADER);\r | |
1439 | VarNameSize = StrSize (VariableName);\r | |
1440 | CopyMem (\r | |
1441 | (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r | |
1442 | VariableName,\r | |
1443 | VarNameSize\r | |
1444 | );\r | |
1445 | VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r | |
1446 | CopyMem (\r | |
1447 | (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r | |
1448 | Data,\r | |
1449 | DataSize\r | |
1450 | );\r | |
1451 | CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r | |
1452 | //\r | |
1453 | // There will be pad bytes after Data, the NextVariable->NameSize and\r | |
1454 | // NextVariable->DataSize should not include pad size so that variable\r | |
1455 | // service can get actual size in GetVariable\r | |
1456 | //\r | |
1457 | NextVariable->NameSize = (UINT32)VarNameSize;\r | |
1458 | NextVariable->DataSize = (UINT32)DataSize;\r | |
1459 | \r | |
1460 | //\r | |
1461 | // The actual size of the variable that stores in storage should\r | |
1462 | // include pad size.\r | |
1463 | //\r | |
1464 | VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r | |
1465 | if (Attributes & EFI_VARIABLE_NON_VOLATILE) {\r | |
8d3a5c82 | 1466 | //\r |
c6492839 | 1467 | // Create a nonvolatile variable\r |
8d3a5c82 | 1468 | //\r |
fd51bf70 | 1469 | Volatile = FALSE;\r |
c6492839 | 1470 | \r |
1471 | if ((UINT32) (VarSize +*NonVolatileOffset) >\r | |
052ad7e1 | 1472 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size\r |
c6492839 | 1473 | ) {\r |
1474 | if (EfiAtRuntime ()) {\r | |
1475 | Status = EFI_OUT_OF_RESOURCES;\r | |
1476 | goto Done;\r | |
1477 | }\r | |
1478 | //\r | |
1479 | // Perform garbage collection & reclaim operation\r | |
1480 | //\r | |
814bae52 | 1481 | Status = Reclaim (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, NonVolatileOffset, FALSE, Variable.CurrPtr);\r |
8d3a5c82 | 1482 | if (EFI_ERROR (Status)) {\r |
1483 | goto Done;\r | |
1484 | }\r | |
8d3a5c82 | 1485 | //\r |
c6492839 | 1486 | // If still no enough space, return out of resources\r |
8d3a5c82 | 1487 | //\r |
c6492839 | 1488 | if ((UINT32) (VarSize +*NonVolatileOffset) >\r |
052ad7e1 | 1489 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)))->Size\r |
8d3a5c82 | 1490 | ) {\r |
c6492839 | 1491 | Status = EFI_OUT_OF_RESOURCES;\r |
8d3a5c82 | 1492 | goto Done;\r |
8d3a5c82 | 1493 | }\r |
c6492839 | 1494 | \r |
1495 | Reclaimed = TRUE;\r | |
8d3a5c82 | 1496 | }\r |
1497 | //\r | |
c6492839 | 1498 | // Three steps\r |
1499 | // 1. Write variable header\r | |
130e2569 | 1500 | // 2. Set variable state to header valid \r |
1501 | // 3. Write variable data\r | |
1502 | // 4. Set variable state to valid\r | |
8d3a5c82 | 1503 | //\r |
8d3a5c82 | 1504 | //\r |
c6492839 | 1505 | // Step 1:\r |
8d3a5c82 | 1506 | //\r |
c6492839 | 1507 | Status = UpdateVariableStore (\r |
052ad7e1 | 1508 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1509 | FALSE,\r |
1510 | TRUE,\r | |
1511 | Instance,\r | |
1512 | *NonVolatileOffset,\r | |
1513 | sizeof (VARIABLE_HEADER),\r | |
1514 | (UINT8 *) NextVariable\r | |
1515 | );\r | |
1516 | \r | |
1517 | if (EFI_ERROR (Status)) {\r | |
1518 | goto Done;\r | |
1519 | }\r | |
130e2569 | 1520 | \r |
8d3a5c82 | 1521 | //\r |
c6492839 | 1522 | // Step 2:\r |
8d3a5c82 | 1523 | //\r |
130e2569 | 1524 | NextVariable->State = VAR_HEADER_VALID_ONLY;\r |
1525 | Status = UpdateVariableStore (\r | |
1526 | &mVariableModuleGlobal->VariableGlobal,\r | |
1527 | FALSE,\r | |
1528 | TRUE,\r | |
1529 | Instance,\r | |
1530 | *NonVolatileOffset,\r | |
1531 | sizeof (VARIABLE_HEADER),\r | |
1532 | (UINT8 *) NextVariable\r | |
1533 | );\r | |
1534 | \r | |
1535 | if (EFI_ERROR (Status)) {\r | |
1536 | goto Done;\r | |
1537 | }\r | |
1538 | //\r | |
1539 | // Step 3:\r | |
1540 | //\r | |
c6492839 | 1541 | Status = UpdateVariableStore (\r |
052ad7e1 | 1542 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1543 | FALSE,\r |
1544 | TRUE,\r | |
1545 | Instance,\r | |
1546 | *NonVolatileOffset + sizeof (VARIABLE_HEADER),\r | |
1547 | (UINT32) VarSize - sizeof (VARIABLE_HEADER),\r | |
1548 | (UINT8 *) NextVariable + sizeof (VARIABLE_HEADER)\r | |
1549 | );\r | |
1550 | \r | |
1551 | if (EFI_ERROR (Status)) {\r | |
1552 | goto Done;\r | |
1553 | }\r | |
8d3a5c82 | 1554 | //\r |
130e2569 | 1555 | // Step 4:\r |
8d3a5c82 | 1556 | //\r |
c6492839 | 1557 | NextVariable->State = VAR_ADDED;\r |
1558 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1559 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1560 | FALSE,\r |
1561 | TRUE,\r | |
1562 | Instance,\r | |
1563 | *NonVolatileOffset,\r | |
1564 | sizeof (VARIABLE_HEADER),\r | |
1565 | (UINT8 *) NextVariable\r | |
1566 | );\r | |
1567 | \r | |
1568 | if (EFI_ERROR (Status)) {\r | |
1569 | goto Done;\r | |
1570 | }\r | |
8d3a5c82 | 1571 | \r |
9cad030b | 1572 | *NonVolatileOffset = HEADER_ALIGN (*NonVolatileOffset + VarSize);\r |
8d3a5c82 | 1573 | \r |
c6492839 | 1574 | } else {\r |
1575 | //\r | |
1576 | // Create a volatile variable\r | |
1577 | // \r | |
fd51bf70 | 1578 | Volatile = TRUE;\r |
c6492839 | 1579 | \r |
1580 | if ((UINT32) (VarSize +*VolatileOffset) >\r | |
052ad7e1 | 1581 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size) {\r |
8d3a5c82 | 1582 | //\r |
c6492839 | 1583 | // Perform garbage collection & reclaim operation\r |
8d3a5c82 | 1584 | //\r |
814bae52 | 1585 | Status = Reclaim (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase, VolatileOffset, TRUE, Variable.CurrPtr);\r |
8d3a5c82 | 1586 | if (EFI_ERROR (Status)) {\r |
1587 | goto Done;\r | |
1588 | }\r | |
1589 | //\r | |
c6492839 | 1590 | // If still no enough space, return out of resources\r |
8d3a5c82 | 1591 | //\r |
8d3a5c82 | 1592 | if ((UINT32) (VarSize +*VolatileOffset) >\r |
052ad7e1 | 1593 | ((VARIABLE_STORE_HEADER *) ((UINTN) (mVariableModuleGlobal->VariableGlobal.VolatileVariableBase)))->Size\r |
8d3a5c82 | 1594 | ) {\r |
c6492839 | 1595 | Status = EFI_OUT_OF_RESOURCES;\r |
8d3a5c82 | 1596 | goto Done;\r |
1597 | }\r | |
c6492839 | 1598 | \r |
1599 | Reclaimed = TRUE;\r | |
8d3a5c82 | 1600 | }\r |
8d3a5c82 | 1601 | \r |
c6492839 | 1602 | NextVariable->State = VAR_ADDED;\r |
1603 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1604 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1605 | TRUE,\r |
1606 | TRUE,\r | |
1607 | Instance,\r | |
1608 | *VolatileOffset,\r | |
1609 | (UINT32) VarSize,\r | |
1610 | (UINT8 *) NextVariable\r | |
1611 | );\r | |
1612 | \r | |
1613 | if (EFI_ERROR (Status)) {\r | |
1614 | goto Done;\r | |
8d3a5c82 | 1615 | }\r |
c6492839 | 1616 | \r |
9cad030b | 1617 | *VolatileOffset = HEADER_ALIGN (*VolatileOffset + VarSize);\r |
c6492839 | 1618 | }\r |
1619 | //\r | |
1620 | // Mark the old variable as deleted\r | |
1621 | //\r | |
1622 | if (!Reclaimed && !EFI_ERROR (Status) && Variable.CurrPtr != NULL) {\r | |
1623 | State = Variable.CurrPtr->State;\r | |
1624 | State &= VAR_DELETED;\r | |
1625 | \r | |
1626 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1627 | &mVariableModuleGlobal->VariableGlobal,\r |
c6492839 | 1628 | Variable.Volatile,\r |
1629 | FALSE,\r | |
1630 | Instance,\r | |
1631 | (UINTN) &Variable.CurrPtr->State,\r | |
1632 | sizeof (UINT8),\r | |
1633 | &State\r | |
1634 | );\r | |
33a5a666 A |
1635 | \r |
1636 | if (!EFI_ERROR (Status)) {\r | |
fd51bf70 | 1637 | UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r |
33a5a666 A |
1638 | UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r |
1639 | }\r | |
c6492839 | 1640 | goto Done; \r |
8d3a5c82 | 1641 | }\r |
1642 | \r | |
1643 | Status = EFI_SUCCESS;\r | |
fd51bf70 | 1644 | UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, FALSE, FALSE);\r |
33a5a666 A |
1645 | UpdateVariableCache (VariableName, VendorGuid, Attributes, DataSize, Data);\r |
1646 | \r | |
8d3a5c82 | 1647 | Done:\r |
fdb7765f | 1648 | InterlockedDecrement (&mVariableModuleGlobal->VariableGlobal.ReentrantState);\r |
052ad7e1 | 1649 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
fdb7765f | 1650 | \r |
8d3a5c82 | 1651 | return Status;\r |
1652 | }\r | |
1653 | \r | |
052ad7e1 | 1654 | \r |
8d3a5c82 | 1655 | /*++\r |
1656 | \r | |
1657 | Routine Description:\r | |
1658 | \r | |
1659 | This code returns information about the EFI variables.\r | |
1660 | \r | |
1661 | Arguments:\r | |
1662 | \r | |
1663 | Attributes Attributes bitmask to specify the type of variables\r | |
1664 | on which to return information.\r | |
1665 | MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r | |
1666 | for the EFI variables associated with the attributes specified.\r | |
1667 | RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r | |
c6492839 | 1668 | for EFI variables associated with the attributes specified.\r |
1669 | MaximumVariableSize Pointer to the maximum size of an individual EFI variables\r | |
8d3a5c82 | 1670 | associated with the attributes specified.\r |
1671 | Global Pointer to VARIABLE_GLOBAL structure.\r | |
1672 | Instance Instance of the Firmware Volume.\r | |
1673 | \r | |
1674 | Returns:\r | |
1675 | \r | |
1676 | EFI STATUS\r | |
1677 | EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.\r | |
1678 | EFI_SUCCESS - Query successfully.\r | |
1679 | EFI_UNSUPPORTED - The attribute is not supported on this platform.\r | |
1680 | \r | |
1681 | --*/\r | |
052ad7e1 A |
1682 | EFI_STATUS\r |
1683 | EFIAPI\r | |
1684 | RuntimeServiceQueryVariableInfo (\r | |
1685 | IN UINT32 Attributes,\r | |
1686 | OUT UINT64 *MaximumVariableStorageSize,\r | |
1687 | OUT UINT64 *RemainingVariableStorageSize,\r | |
1688 | OUT UINT64 *MaximumVariableSize\r | |
1689 | )\r | |
8d3a5c82 | 1690 | {\r |
1691 | VARIABLE_HEADER *Variable;\r | |
1692 | VARIABLE_HEADER *NextVariable;\r | |
1693 | UINT64 VariableSize;\r | |
1694 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
1695 | \r | |
c6492839 | 1696 | if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r |
8d3a5c82 | 1697 | return EFI_INVALID_PARAMETER;\r |
1698 | }\r | |
c6492839 | 1699 | \r |
1700 | if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r | |
8d3a5c82 | 1701 | //\r |
1702 | // Make sure the Attributes combination is supported by the platform.\r | |
1703 | //\r | |
c6492839 | 1704 | return EFI_UNSUPPORTED; \r |
8d3a5c82 | 1705 | } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r |
1706 | //\r | |
1707 | // Make sure if runtime bit is set, boot service bit is set also.\r | |
1708 | //\r | |
1709 | return EFI_INVALID_PARAMETER;\r | |
1710 | } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r | |
1711 | //\r | |
1712 | // Make sure RT Attribute is set if we are in Runtime phase.\r | |
1713 | //\r | |
1714 | return EFI_INVALID_PARAMETER;\r | |
1715 | }\r | |
1716 | \r | |
052ad7e1 | 1717 | AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8d3a5c82 | 1718 | \r |
1719 | if((Attributes & EFI_VARIABLE_NON_VOLATILE) == 0) {\r | |
1720 | //\r | |
1721 | // Query is Volatile related.\r | |
1722 | //\r | |
052ad7e1 | 1723 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase);\r |
8d3a5c82 | 1724 | } else {\r |
1725 | //\r | |
1726 | // Query is Non-Volatile related.\r | |
1727 | //\r | |
052ad7e1 | 1728 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r |
8d3a5c82 | 1729 | }\r |
1730 | \r | |
1731 | //\r | |
1732 | // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r | |
1733 | // with the storage size (excluding the storage header size).\r | |
1734 | //\r | |
1735 | *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r | |
1736 | *RemainingVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r | |
1737 | \r | |
1738 | //\r | |
c6492839 | 1739 | // Let *MaximumVariableSize be MAX_VARIABLE_SIZE with the exception of the variable header size.\r |
8d3a5c82 | 1740 | //\r |
c6492839 | 1741 | *MaximumVariableSize = MAX_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r |
1742 | \r | |
1743 | //\r | |
1744 | // Harware error record variable needs larger size.\r | |
1745 | //\r | |
1746 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
1747 | *MaximumVariableSize = MAX_HARDWARE_ERROR_VARIABLE_SIZE - sizeof (VARIABLE_HEADER);\r | |
1748 | }\r | |
8d3a5c82 | 1749 | \r |
1750 | //\r | |
1751 | // Point to the starting address of the variables.\r | |
1752 | //\r | |
9cad030b | 1753 | Variable = GetStartPointer (VariableStoreHeader);\r |
8d3a5c82 | 1754 | \r |
1755 | //\r | |
1756 | // Now walk through the related variable store.\r | |
1757 | //\r | |
1758 | while (IsValidVariableHeader (Variable) && (Variable < GetEndPointer (VariableStoreHeader))) {\r | |
1759 | NextVariable = GetNextVariablePtr (Variable);\r | |
1760 | VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r | |
1761 | \r | |
1762 | if (EfiAtRuntime ()) {\r | |
1763 | //\r | |
1764 | // we don't take the state of the variables in mind\r | |
1765 | // when calculating RemainingVariableStorageSize,\r | |
1766 | // since the space occupied by variables not marked with\r | |
1767 | // VAR_ADDED is not allowed to be reclaimed in Runtime.\r | |
1768 | //\r | |
1769 | *RemainingVariableStorageSize -= VariableSize;\r | |
1770 | } else {\r | |
1771 | //\r | |
1772 | // Only care about Variables with State VAR_ADDED,because\r | |
1773 | // the space not marked as VAR_ADDED is reclaimable now.\r | |
1774 | //\r | |
1775 | if (Variable->State == VAR_ADDED) {\r | |
1776 | *RemainingVariableStorageSize -= VariableSize;\r | |
1777 | }\r | |
1778 | }\r | |
1779 | \r | |
1780 | //\r | |
1781 | // Go to the next one\r | |
1782 | //\r | |
1783 | Variable = NextVariable;\r | |
1784 | }\r | |
1785 | \r | |
c6492839 | 1786 | if (*RemainingVariableStorageSize < sizeof (VARIABLE_HEADER)) {\r |
1787 | *MaximumVariableSize = 0;\r | |
1788 | } else if ((*RemainingVariableStorageSize - sizeof (VARIABLE_HEADER)) < *MaximumVariableSize) {\r | |
1789 | *MaximumVariableSize = *RemainingVariableStorageSize - sizeof (VARIABLE_HEADER);\r | |
1790 | }\r | |
1791 | \r | |
052ad7e1 | 1792 | ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r |
8d3a5c82 | 1793 | return EFI_SUCCESS;\r |
1794 | }\r | |
1795 | \r | |
7800593d LG |
1796 | VOID\r |
1797 | EFIAPI\r | |
1798 | ReclaimForOS(\r | |
1799 | EFI_EVENT Event,\r | |
1800 | VOID *Context\r | |
1801 | )\r | |
1802 | {\r | |
1803 | UINT32 VarSize;\r | |
1804 | EFI_STATUS Status;\r | |
1805 | \r | |
1806 | VarSize = ((VARIABLE_STORE_HEADER *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase))->Size;\r | |
1807 | Status = EFI_SUCCESS; \r | |
1808 | \r | |
1809 | //\r | |
1810 | // Check if the free area is blow a threshold\r | |
1811 | //\r | |
1812 | if ((VarSize - mVariableModuleGlobal->NonVolatileLastVariableOffset) < VARIABLE_RECLAIM_THRESHOLD) {\r | |
1813 | Status = Reclaim (\r | |
1814 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r | |
1815 | &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r | |
814bae52 | 1816 | FALSE,\r |
1817 | NULL\r | |
7800593d LG |
1818 | );\r |
1819 | ASSERT_EFI_ERROR (Status);\r | |
1820 | }\r | |
1821 | }\r | |
1822 | \r | |
8d3a5c82 | 1823 | EFI_STATUS\r |
8d3a5c82 | 1824 | VariableCommonInitialize (\r |
1825 | IN EFI_HANDLE ImageHandle,\r | |
1826 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
1827 | )\r | |
1828 | /*++\r | |
1829 | \r | |
1830 | Routine Description:\r | |
1831 | This function does common initialization for variable services\r | |
1832 | \r | |
1833 | Arguments:\r | |
1834 | \r | |
1835 | ImageHandle - The firmware allocated handle for the EFI image.\r | |
1836 | SystemTable - A pointer to the EFI System Table.\r | |
1837 | \r | |
1838 | Returns:\r | |
1839 | \r | |
1840 | Status code.\r | |
1841 | \r | |
1842 | EFI_NOT_FOUND - Variable store area not found.\r | |
1843 | EFI_UNSUPPORTED - Currently only one non-volatile variable store is supported.\r | |
1844 | EFI_SUCCESS - Variable services successfully initialized.\r | |
1845 | \r | |
1846 | --*/\r | |
1847 | {\r | |
1848 | EFI_STATUS Status;\r | |
1849 | EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader;\r | |
1850 | CHAR8 *CurrPtr;\r | |
1851 | VARIABLE_STORE_HEADER *VolatileVariableStore;\r | |
1852 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
1853 | VARIABLE_HEADER *NextVariable;\r | |
1854 | UINT32 Instance;\r | |
1855 | EFI_PHYSICAL_ADDRESS FvVolHdr;\r | |
8d3a5c82 | 1856 | UINT64 TempVariableStoreHeader;\r |
8d3a5c82 | 1857 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r |
8d3a5c82 | 1858 | UINT64 BaseAddress;\r |
1859 | UINT64 Length;\r | |
1860 | UINTN Index;\r | |
1861 | UINT8 Data;\r | |
052ad7e1 A |
1862 | UINT64 VariableStoreBase;\r |
1863 | UINT64 VariableStoreLength;\r | |
7800593d | 1864 | EFI_EVENT ReadyToBootEvent;\r |
8d3a5c82 | 1865 | \r |
7800593d LG |
1866 | Status = EFI_SUCCESS;\r |
1867 | //\r | |
1868 | // Allocate runtime memory for variable driver global structure.\r | |
1869 | //\r | |
1870 | mVariableModuleGlobal = AllocateRuntimePool (sizeof (VARIABLE_MODULE_GLOBAL));\r | |
1871 | if (mVariableModuleGlobal == NULL) {\r | |
1872 | return EFI_OUT_OF_RESOURCES;\r | |
1873 | }\r | |
8d3a5c82 | 1874 | \r |
052ad7e1 | 1875 | EfiInitializeLock(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock, TPL_NOTIFY);\r |
fdb7765f | 1876 | mVariableModuleGlobal->VariableGlobal.ReentrantState = 0;\r |
8d3a5c82 | 1877 | \r |
1878 | //\r | |
1879 | // Allocate memory for volatile variable store\r | |
1880 | //\r | |
1881 | VolatileVariableStore = AllocateRuntimePool (VARIABLE_STORE_SIZE + SCRATCH_SIZE);\r | |
1882 | if (VolatileVariableStore == NULL) {\r | |
1883 | FreePool (mVariableModuleGlobal);\r | |
1884 | return EFI_OUT_OF_RESOURCES;\r | |
1885 | }\r | |
1886 | \r | |
1887 | SetMem (VolatileVariableStore, VARIABLE_STORE_SIZE + SCRATCH_SIZE, 0xff);\r | |
1888 | \r | |
1889 | //\r | |
1890 | // Variable Specific Data\r | |
1891 | //\r | |
052ad7e1 | 1892 | mVariableModuleGlobal->VariableGlobal.VolatileVariableBase = (EFI_PHYSICAL_ADDRESS) (UINTN) VolatileVariableStore;\r |
9cad030b | 1893 | mVariableModuleGlobal->VolatileLastVariableOffset = (UINTN) GetStartPointer (VolatileVariableStore) - (UINTN) VolatileVariableStore;\r |
8d3a5c82 | 1894 | \r |
1895 | VolatileVariableStore->Signature = VARIABLE_STORE_SIGNATURE;\r | |
1896 | VolatileVariableStore->Size = VARIABLE_STORE_SIZE;\r | |
1897 | VolatileVariableStore->Format = VARIABLE_STORE_FORMATTED;\r | |
1898 | VolatileVariableStore->State = VARIABLE_STORE_HEALTHY;\r | |
1899 | VolatileVariableStore->Reserved = 0;\r | |
1900 | VolatileVariableStore->Reserved1 = 0;\r | |
1901 | \r | |
1902 | //\r | |
1903 | // Get non volatile varaible store\r | |
1904 | //\r | |
1905 | \r | |
1906 | TempVariableStoreHeader = (UINT64) PcdGet32 (PcdFlashNvStorageVariableBase);\r | |
052ad7e1 | 1907 | VariableStoreBase = TempVariableStoreHeader + \\r |
8d3a5c82 | 1908 | (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r |
052ad7e1 | 1909 | VariableStoreLength = (UINT64) PcdGet32 (PcdFlashNvStorageVariableSize) - \\r |
8d3a5c82 | 1910 | (((EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) (TempVariableStoreHeader)) -> HeaderLength);\r |
1911 | //\r | |
1912 | // Mark the variable storage region of the FLASH as RUNTIME\r | |
1913 | //\r | |
052ad7e1 A |
1914 | BaseAddress = VariableStoreBase & (~EFI_PAGE_MASK);\r |
1915 | Length = VariableStoreLength + (VariableStoreBase - BaseAddress);\r | |
8d3a5c82 | 1916 | Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r |
1917 | \r | |
1918 | Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r | |
1919 | if (EFI_ERROR (Status)) {\r | |
7800593d | 1920 | goto Done;\r |
8d3a5c82 | 1921 | }\r |
1922 | \r | |
1923 | Status = gDS->SetMemorySpaceAttributes (\r | |
1924 | BaseAddress,\r | |
1925 | Length,\r | |
1926 | GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r | |
1927 | );\r | |
1928 | if (EFI_ERROR (Status)) {\r | |
7800593d | 1929 | goto Done;\r |
8d3a5c82 | 1930 | }\r |
1931 | //\r | |
1932 | // Get address of non volatile variable store base\r | |
1933 | //\r | |
052ad7e1 | 1934 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = VariableStoreBase;\r |
8d3a5c82 | 1935 | \r |
1936 | //\r | |
1937 | // Check Integrity\r | |
1938 | //\r | |
1939 | //\r | |
1940 | // Find the Correct Instance of the FV Block Service.\r | |
1941 | //\r | |
1942 | Instance = 0;\r | |
052ad7e1 | 1943 | CurrPtr = (CHAR8 *) ((UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase);\r |
8d3a5c82 | 1944 | while (EfiFvbGetPhysicalAddress (Instance, &FvVolHdr) == EFI_SUCCESS) {\r |
1945 | FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINTN) FvVolHdr);\r | |
1946 | if (CurrPtr >= (CHAR8 *) FwVolHeader && CurrPtr < (((CHAR8 *) FwVolHeader) + FwVolHeader->FvLength)) {\r | |
1947 | mVariableModuleGlobal->FvbInstance = Instance;\r | |
1948 | break;\r | |
1949 | }\r | |
1950 | \r | |
1951 | Instance++;\r | |
1952 | }\r | |
1953 | \r | |
1954 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) CurrPtr;\r | |
1955 | if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r | |
1956 | if (~VariableStoreHeader->Size == 0) {\r | |
1957 | Status = UpdateVariableStore (\r | |
052ad7e1 | 1958 | &mVariableModuleGlobal->VariableGlobal,\r |
8d3a5c82 | 1959 | FALSE,\r |
1960 | FALSE,\r | |
1961 | mVariableModuleGlobal->FvbInstance,\r | |
1962 | (UINTN) &VariableStoreHeader->Size,\r | |
1963 | sizeof (UINT32),\r | |
052ad7e1 | 1964 | (UINT8 *) &VariableStoreLength\r |
8d3a5c82 | 1965 | );\r |
1966 | //\r | |
1967 | // As Variables are stored in NV storage, which are slow devices,such as flash.\r | |
1968 | // Variable operation may skip checking variable program result to improve performance,\r | |
1969 | // We can assume Variable program is OK through some check point.\r | |
1970 | // Variable Store Size Setting should be the first Variable write operation,\r | |
1971 | // We can assume all Read/Write is OK if we can set Variable store size successfully.\r | |
1972 | // If write fail, we will assert here\r | |
1973 | //\r | |
052ad7e1 | 1974 | ASSERT(VariableStoreHeader->Size == VariableStoreLength);\r |
8d3a5c82 | 1975 | \r |
1976 | if (EFI_ERROR (Status)) {\r | |
7800593d | 1977 | goto Done;\r |
8d3a5c82 | 1978 | }\r |
1979 | }\r | |
1980 | \r | |
052ad7e1 | 1981 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase = (EFI_PHYSICAL_ADDRESS) ((UINTN) CurrPtr);\r |
8d3a5c82 | 1982 | //\r |
1983 | // Parse non-volatile variable data and get last variable offset\r | |
1984 | //\r | |
9cad030b | 1985 | NextVariable = GetStartPointer ((VARIABLE_STORE_HEADER *) CurrPtr);\r |
8d3a5c82 | 1986 | Status = EFI_SUCCESS;\r |
1987 | \r | |
1988 | while (IsValidVariableHeader (NextVariable)) {\r | |
1989 | NextVariable = GetNextVariablePtr (NextVariable);\r | |
1990 | }\r | |
1991 | \r | |
1992 | mVariableModuleGlobal->NonVolatileLastVariableOffset = (UINTN) NextVariable - (UINTN) CurrPtr;\r | |
1993 | \r | |
8d3a5c82 | 1994 | //\r |
1995 | // Check if the free area is really free.\r | |
1996 | //\r | |
1997 | for (Index = mVariableModuleGlobal->NonVolatileLastVariableOffset; Index < VariableStoreHeader->Size; Index++) {\r | |
052ad7e1 | 1998 | Data = ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase)[Index];\r |
8d3a5c82 | 1999 | if (Data != 0xff) {\r |
2000 | //\r | |
2001 | // There must be something wrong in variable store, do reclaim operation.\r | |
2002 | //\r | |
2003 | Status = Reclaim (\r | |
052ad7e1 | 2004 | mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase,\r |
8d3a5c82 | 2005 | &mVariableModuleGlobal->NonVolatileLastVariableOffset,\r |
814bae52 | 2006 | FALSE,\r |
2007 | NULL\r | |
8d3a5c82 | 2008 | );\r |
7800593d LG |
2009 | \r |
2010 | if (EFI_ERROR (Status)) {\r | |
2011 | goto Done;\r | |
2012 | }\r | |
2013 | \r | |
8d3a5c82 | 2014 | break;\r |
2015 | }\r | |
2016 | }\r | |
7800593d LG |
2017 | \r |
2018 | //\r | |
2019 | // Register the event handling function to reclaim variable for OS usage.\r | |
2020 | //\r | |
2021 | Status = EfiCreateEventReadyToBootEx (\r | |
2022 | TPL_NOTIFY, \r | |
2023 | ReclaimForOS, \r | |
2024 | NULL, \r | |
2025 | &ReadyToBootEvent\r | |
2026 | );\r | |
8d3a5c82 | 2027 | }\r |
2028 | \r | |
7800593d | 2029 | Done:\r |
8d3a5c82 | 2030 | if (EFI_ERROR (Status)) {\r |
2031 | FreePool (mVariableModuleGlobal);\r | |
2032 | FreePool (VolatileVariableStore);\r | |
2033 | }\r | |
2034 | \r | |
2035 | return Status;\r | |
2036 | }\r | |
052ad7e1 | 2037 | \r |
052ad7e1 A |
2038 | VOID\r |
2039 | EFIAPI\r | |
2040 | VariableClassAddressChangeEvent (\r | |
2041 | IN EFI_EVENT Event,\r | |
2042 | IN VOID *Context\r | |
2043 | )\r | |
2044 | {\r | |
2045 | EfiConvertPointer (\r | |
2046 | 0x0,\r | |
2047 | (VOID **) &mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase\r | |
2048 | );\r | |
2049 | EfiConvertPointer (\r | |
2050 | 0x0,\r | |
2051 | (VOID **) &mVariableModuleGlobal->VariableGlobal.VolatileVariableBase\r | |
2052 | );\r | |
2053 | EfiConvertPointer (0x0, (VOID **) &mVariableModuleGlobal);\r | |
2054 | }\r | |
2055 | \r | |
2056 | \r | |
2057 | /**\r | |
2058 | Variable Driver main entry point. The Variable driver places the 4 EFI\r | |
2059 | runtime services in the EFI System Table and installs arch protocols \r | |
2060 | for variable read and write services being availible. \r | |
2061 | \r | |
2062 | @param[in] ImageHandle The firmware allocated handle for the EFI image. \r | |
2063 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
2064 | \r | |
2065 | @retval EFI_SUCCESS The entry point is executed successfully.\r | |
2066 | @retval other Some error occurs when executing this entry point.\r | |
2067 | \r | |
2068 | **/\r | |
2069 | EFI_STATUS\r | |
2070 | EFIAPI\r | |
2071 | VariableServiceInitialize (\r | |
2072 | IN EFI_HANDLE ImageHandle,\r | |
2073 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
2074 | )\r | |
2075 | {\r | |
2076 | EFI_STATUS Status;\r | |
2077 | \r | |
2078 | Status = VariableCommonInitialize (ImageHandle, SystemTable);\r | |
2079 | ASSERT_EFI_ERROR (Status);\r | |
2080 | \r | |
2081 | SystemTable->RuntimeServices->GetVariable = RuntimeServiceGetVariable;\r | |
2082 | SystemTable->RuntimeServices->GetNextVariableName = RuntimeServiceGetNextVariableName;\r | |
2083 | SystemTable->RuntimeServices->SetVariable = RuntimeServiceSetVariable;\r | |
2084 | SystemTable->RuntimeServices->QueryVariableInfo = RuntimeServiceQueryVariableInfo;\r | |
2085 | \r | |
2086 | //\r | |
2087 | // Now install the Variable Runtime Architectural Protocol on a new handle\r | |
2088 | //\r | |
2089 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
2090 | &mHandle,\r | |
2091 | &gEfiVariableArchProtocolGuid, NULL,\r | |
2092 | &gEfiVariableWriteArchProtocolGuid, NULL,\r | |
2093 | NULL\r | |
2094 | );\r | |
2095 | ASSERT_EFI_ERROR (Status);\r | |
2096 | \r | |
2097 | Status = gBS->CreateEvent (\r | |
2098 | EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,\r | |
2099 | TPL_NOTIFY,\r | |
2100 | VariableClassAddressChangeEvent,\r | |
2101 | NULL,\r | |
2102 | &mVirtualAddressChangeEvent\r | |
2103 | );\r | |
2104 | ASSERT_EFI_ERROR (Status);\r | |
2105 | \r | |
2106 | return EFI_SUCCESS;\r | |
2107 | }\r | |
2108 | \r |