]>
Commit | Line | Data |
---|---|---|
9071550e | 1 | /*++\r |
2 | \r | |
741a1e58 ED |
3 | Caution: This file is used for Duet platform only, do not use them in real platform.\r |
4 | All variable code, variable metadata, and variable data used by Duet platform are on \r | |
5 | disk. They can be changed by user. BIOS is not able to protoect those.\r | |
6 | Duet trusts all meta data from disk. If variable code, variable metadata and variable\r | |
7 | data is modified in inproper way, the behavior is undefined.\r | |
8 | \r | |
e286e118 | 9 | Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r |
b1f700a8 | 10 | This program and the accompanying materials\r |
9071550e | 11 | are licensed and made available under the terms and conditions of the BSD License\r |
12 | which accompanies this distribution. The full text of the license may be found at\r | |
13 | http://opensource.org/licenses/bsd-license.php\r | |
14 | \r | |
15 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
16 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
17 | \r | |
18 | Module Name:\r | |
19 | \r | |
20 | FSVariable.c\r | |
21 | \r | |
22 | Abstract:\r | |
23 | \r | |
24 | Provide support functions for variable services.\r | |
25 | \r | |
26 | --*/\r | |
27 | \r | |
28 | #include "FSVariable.h"\r | |
29 | \r | |
30 | VARIABLE_STORE_HEADER mStoreHeaderTemplate = {\r | |
439c34f3 | 31 | VARIABLE_STORE_SIGNATURE,\r |
9071550e | 32 | VOLATILE_VARIABLE_STORE_SIZE,\r |
33 | VARIABLE_STORE_FORMATTED,\r | |
34 | VARIABLE_STORE_HEALTHY,\r | |
35 | 0,\r | |
36 | 0\r | |
37 | };\r | |
38 | \r | |
39 | //\r | |
40 | // Don't use module globals after the SetVirtualAddress map is signaled\r | |
41 | //\r | |
42 | VARIABLE_GLOBAL *mGlobal;\r | |
43 | \r | |
a24b4043 | 44 | /**\r |
45 | Update the variable region with Variable information. These are the same \r | |
46 | arguments as the EFI Variable services.\r | |
47 | \r | |
48 | @param[in] VariableName Name of variable\r | |
49 | \r | |
50 | @param[in] VendorGuid Guid of variable\r | |
51 | \r | |
52 | @param[in] Data Variable data\r | |
53 | \r | |
54 | @param[in] DataSize Size of data. 0 means delete\r | |
55 | \r | |
b29a823d | 56 | @param[in] Attributes Attribues of the variable\r |
a24b4043 | 57 | \r |
58 | @param[in] Variable The variable information which is used to keep track of variable usage.\r | |
59 | \r | |
60 | @retval EFI_SUCCESS The update operation is success.\r | |
61 | \r | |
62 | @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r | |
63 | \r | |
64 | **/\r | |
65 | EFI_STATUS\r | |
66 | EFIAPI\r | |
67 | UpdateVariable (\r | |
68 | IN CHAR16 *VariableName,\r | |
69 | IN EFI_GUID *VendorGuid,\r | |
70 | IN VOID *Data,\r | |
71 | IN UINTN DataSize,\r | |
72 | IN UINT32 Attributes OPTIONAL,\r | |
73 | IN VARIABLE_POINTER_TRACK *Variable\r | |
74 | );\r | |
75 | \r | |
9071550e | 76 | VOID\r |
77 | EFIAPI\r | |
e56dd2ce | 78 | OnVirtualAddressChangeFsv (\r |
9071550e | 79 | IN EFI_EVENT Event,\r |
80 | IN VOID *Context\r | |
81 | );\r | |
82 | \r | |
9071550e | 83 | VOID\r |
84 | EFIAPI\r | |
85 | OnSimpleFileSystemInstall (\r | |
86 | IN EFI_EVENT Event,\r | |
87 | IN VOID *Context\r | |
88 | );\r | |
89 | \r | |
9071550e | 90 | BOOLEAN\r |
91 | IsValidVariableHeader (\r | |
92 | IN VARIABLE_HEADER *Variable\r | |
93 | )\r | |
94 | /*++\r | |
95 | \r | |
96 | Routine Description:\r | |
97 | \r | |
98 | This code checks if variable header is valid or not.\r | |
99 | \r | |
100 | Arguments:\r | |
101 | Variable Pointer to the Variable Header.\r | |
102 | \r | |
103 | Returns:\r | |
104 | TRUE Variable header is valid.\r | |
105 | FALSE Variable header is not valid.\r | |
106 | \r | |
107 | --*/\r | |
108 | {\r | |
a24b4043 | 109 | if (Variable == NULL || Variable->StartId != VARIABLE_DATA) {\r |
9071550e | 110 | return FALSE;\r |
111 | }\r | |
112 | \r | |
113 | return TRUE;\r | |
114 | }\r | |
115 | \r | |
9071550e | 116 | VARIABLE_STORE_STATUS\r |
117 | GetVariableStoreStatus (\r | |
118 | IN VARIABLE_STORE_HEADER *VarStoreHeader\r | |
119 | )\r | |
120 | /*++\r | |
121 | \r | |
122 | Routine Description:\r | |
123 | \r | |
124 | This code gets the current status of Variable Store.\r | |
125 | \r | |
126 | Arguments:\r | |
127 | \r | |
128 | VarStoreHeader Pointer to the Variable Store Header.\r | |
129 | \r | |
130 | Returns:\r | |
131 | \r | |
132 | EfiRaw Variable store status is raw\r | |
133 | EfiValid Variable store status is valid\r | |
134 | EfiInvalid Variable store status is invalid\r | |
135 | \r | |
136 | --*/\r | |
137 | {\r | |
3709c4cd | 138 | if (CompareGuid (&VarStoreHeader->Signature, &mStoreHeaderTemplate.Signature) &&\r |
9071550e | 139 | (VarStoreHeader->Format == mStoreHeaderTemplate.Format) &&\r |
140 | (VarStoreHeader->State == mStoreHeaderTemplate.State)\r | |
141 | ) {\r | |
142 | return EfiValid;\r | |
3709c4cd | 143 | } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] == VAR_DEFAULT_VALUE_32 &&\r |
144 | ((UINT32 *)(&VarStoreHeader->Signature))[1] == VAR_DEFAULT_VALUE_32 &&\r | |
145 | ((UINT32 *)(&VarStoreHeader->Signature))[2] == VAR_DEFAULT_VALUE_32 &&\r | |
146 | ((UINT32 *)(&VarStoreHeader->Signature))[3] == VAR_DEFAULT_VALUE_32 &&\r | |
147 | VarStoreHeader->Size == VAR_DEFAULT_VALUE_32 &&\r | |
148 | VarStoreHeader->Format == VAR_DEFAULT_VALUE &&\r | |
149 | VarStoreHeader->State == VAR_DEFAULT_VALUE\r | |
9071550e | 150 | ) {\r |
151 | \r | |
152 | return EfiRaw;\r | |
153 | } else {\r | |
154 | return EfiInvalid;\r | |
155 | }\r | |
156 | }\r | |
157 | \r | |
9071550e | 158 | UINT8 *\r |
159 | GetVariableDataPtr (\r | |
160 | IN VARIABLE_HEADER *Variable\r | |
161 | )\r | |
162 | /*++\r | |
163 | \r | |
164 | Routine Description:\r | |
165 | \r | |
166 | This code gets the pointer to the variable data.\r | |
167 | \r | |
168 | Arguments:\r | |
169 | \r | |
170 | Variable Pointer to the Variable Header.\r | |
171 | \r | |
172 | Returns:\r | |
173 | \r | |
174 | UINT8* Pointer to Variable Data\r | |
175 | \r | |
176 | --*/\r | |
177 | {\r | |
178 | //\r | |
179 | // Be careful about pad size for alignment\r | |
180 | //\r | |
181 | return (UINT8 *) ((UINTN) GET_VARIABLE_NAME_PTR (Variable) + Variable->NameSize + GET_PAD_SIZE (Variable->NameSize));\r | |
182 | }\r | |
183 | \r | |
9071550e | 184 | VARIABLE_HEADER *\r |
185 | GetNextVariablePtr (\r | |
186 | IN VARIABLE_HEADER *Variable\r | |
187 | )\r | |
188 | /*++\r | |
189 | \r | |
190 | Routine Description:\r | |
191 | \r | |
192 | This code gets the pointer to the next variable header.\r | |
193 | \r | |
194 | Arguments:\r | |
195 | \r | |
196 | Variable Pointer to the Variable Header.\r | |
197 | \r | |
198 | Returns:\r | |
199 | \r | |
200 | VARIABLE_HEADER* Pointer to next variable header.\r | |
201 | \r | |
202 | --*/\r | |
203 | {\r | |
204 | if (!IsValidVariableHeader (Variable)) {\r | |
205 | return NULL;\r | |
206 | }\r | |
207 | //\r | |
208 | // Be careful about pad size for alignment\r | |
209 | //\r | |
210 | return (VARIABLE_HEADER *) ((UINTN) GetVariableDataPtr (Variable) + Variable->DataSize + GET_PAD_SIZE (Variable->DataSize));\r | |
211 | }\r | |
212 | \r | |
9071550e | 213 | VARIABLE_HEADER *\r |
214 | GetEndPointer (\r | |
215 | IN VARIABLE_STORE_HEADER *VarStoreHeader\r | |
216 | )\r | |
217 | /*++\r | |
218 | \r | |
219 | Routine Description:\r | |
220 | \r | |
221 | This code gets the pointer to the last variable memory pointer byte\r | |
222 | \r | |
223 | Arguments:\r | |
224 | \r | |
225 | VarStoreHeader Pointer to the Variable Store Header.\r | |
226 | \r | |
227 | Returns:\r | |
228 | \r | |
229 | VARIABLE_HEADER* Pointer to last unavailable Variable Header\r | |
230 | \r | |
231 | --*/\r | |
232 | {\r | |
233 | //\r | |
234 | // The end of variable store\r | |
235 | //\r | |
236 | return (VARIABLE_HEADER *) ((UINTN) VarStoreHeader + VarStoreHeader->Size);\r | |
237 | }\r | |
238 | \r | |
239 | BOOLEAN\r | |
240 | ExistNewerVariable (\r | |
241 | IN VARIABLE_HEADER *Variable\r | |
242 | )\r | |
243 | /*++\r | |
244 | \r | |
245 | Routine Description:\r | |
246 | \r | |
247 | Check if exist newer variable when doing reclaim\r | |
248 | \r | |
249 | Arguments:\r | |
250 | \r | |
251 | Variable Pointer to start position\r | |
252 | \r | |
253 | Returns:\r | |
254 | \r | |
255 | TRUE - Exists another variable, which is newer than the current one\r | |
256 | FALSE - Doesn't exist another vairable which is newer than the current one\r | |
257 | \r | |
258 | --*/\r | |
259 | {\r | |
260 | VARIABLE_HEADER *NextVariable;\r | |
261 | CHAR16 *VariableName;\r | |
262 | EFI_GUID *VendorGuid;\r | |
263 | \r | |
264 | VendorGuid = &Variable->VendorGuid;\r | |
265 | VariableName = GET_VARIABLE_NAME_PTR(Variable);\r | |
266 | \r | |
267 | NextVariable = GetNextVariablePtr (Variable);\r | |
268 | while (IsValidVariableHeader (NextVariable)) {\r | |
269 | if ((NextVariable->State == VAR_ADDED) || (NextVariable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r | |
270 | //\r | |
271 | // If match Guid and Name\r | |
272 | //\r | |
273 | if (CompareGuid (VendorGuid, &NextVariable->VendorGuid)) {\r | |
274 | if (CompareMem (VariableName, GET_VARIABLE_NAME_PTR (NextVariable), StrSize (VariableName)) == 0) {\r | |
275 | return TRUE;\r | |
276 | }\r | |
277 | }\r | |
278 | }\r | |
279 | NextVariable = GetNextVariablePtr (NextVariable);\r | |
280 | }\r | |
281 | return FALSE;\r | |
282 | }\r | |
283 | \r | |
9071550e | 284 | EFI_STATUS\r |
285 | Reclaim (\r | |
286 | IN VARIABLE_STORAGE_TYPE StorageType,\r | |
287 | IN VARIABLE_HEADER *CurrentVariable OPTIONAL\r | |
288 | )\r | |
289 | /*++\r | |
290 | \r | |
291 | Routine Description:\r | |
292 | \r | |
293 | Variable store garbage collection and reclaim operation\r | |
294 | \r | |
295 | Arguments:\r | |
296 | \r | |
297 | IsVolatile The variable store is volatile or not,\r | |
298 | if it is non-volatile, need FTW\r | |
299 | CurrentVairable If it is not NULL, it means not to process\r | |
300 | current variable for Reclaim.\r | |
301 | \r | |
302 | Returns:\r | |
303 | \r | |
304 | EFI STATUS\r | |
305 | \r | |
306 | --*/\r | |
307 | {\r | |
308 | VARIABLE_HEADER *Variable;\r | |
309 | VARIABLE_HEADER *NextVariable;\r | |
310 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
311 | UINT8 *ValidBuffer;\r | |
312 | UINTN ValidBufferSize;\r | |
313 | UINTN VariableSize;\r | |
314 | UINT8 *CurrPtr;\r | |
315 | EFI_STATUS Status;\r | |
316 | \r | |
317 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType];\r | |
318 | \r | |
319 | //\r | |
320 | // Start Pointers for the variable.\r | |
321 | //\r | |
322 | Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r | |
323 | \r | |
a24b4043 | 324 | //\r |
325 | // recaluate the total size of Common/HwErr type variables in non-volatile area.\r | |
326 | //\r | |
327 | if (!StorageType) {\r | |
328 | mGlobal->CommonVariableTotalSize = 0;\r | |
329 | mGlobal->HwErrVariableTotalSize = 0;\r | |
330 | }\r | |
9071550e | 331 | //\r |
332 | // To make the reclaim, here we just allocate a memory that equal to the original memory\r | |
333 | //\r | |
334 | ValidBufferSize = sizeof (VARIABLE_STORE_HEADER) + VariableStoreHeader->Size;\r | |
335 | \r | |
336 | Status = gBS->AllocatePool (\r | |
337 | EfiBootServicesData,\r | |
338 | ValidBufferSize,\r | |
7c04a679 | 339 | (VOID**) &ValidBuffer\r |
9071550e | 340 | );\r |
341 | if (EFI_ERROR (Status)) {\r | |
342 | return Status;\r | |
343 | }\r | |
344 | \r | |
345 | CurrPtr = ValidBuffer;\r | |
346 | \r | |
347 | //\r | |
348 | // Copy variable store header\r | |
349 | //\r | |
350 | CopyMem (CurrPtr, VariableStoreHeader, sizeof (VARIABLE_STORE_HEADER));\r | |
351 | CurrPtr += sizeof (VARIABLE_STORE_HEADER);\r | |
352 | \r | |
353 | //\r | |
354 | // Start Pointers for the variable.\r | |
355 | //\r | |
356 | Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r | |
357 | \r | |
358 | \r | |
359 | ValidBufferSize = sizeof (VARIABLE_STORE_HEADER);\r | |
360 | while (IsValidVariableHeader (Variable)) {\r | |
361 | NextVariable = GetNextVariablePtr (Variable);\r | |
362 | //\r | |
363 | // State VAR_ADDED or VAR_IN_DELETED_TRANSITION are to kept,\r | |
364 | // The CurrentVariable, is also saved, as SetVariable may fail due to lack of space\r | |
365 | //\r | |
366 | if (Variable->State == VAR_ADDED) {\r | |
367 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r | |
368 | CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r | |
369 | ValidBufferSize += VariableSize;\r | |
370 | CurrPtr += VariableSize;\r | |
a24b4043 | 371 | if ((!StorageType) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r |
372 | mGlobal->HwErrVariableTotalSize += VariableSize;\r | |
373 | } else if ((!StorageType) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r | |
374 | mGlobal->CommonVariableTotalSize += VariableSize;\r | |
375 | }\r | |
9071550e | 376 | } else if (Variable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)) {\r |
377 | //\r | |
378 | // As variables that with the same guid and name may exist in NV due to power failure during SetVariable,\r | |
379 | // we will only save the latest valid one\r | |
380 | //\r | |
381 | if (!ExistNewerVariable(Variable)) {\r | |
382 | VariableSize = (UINTN) NextVariable - (UINTN) Variable;\r | |
383 | CopyMem (CurrPtr, (UINT8 *) Variable, VariableSize);\r | |
384 | //\r | |
385 | // If CurrentVariable == Variable, mark as VAR_IN_DELETED_TRANSITION\r | |
386 | //\r | |
387 | if (Variable != CurrentVariable){\r | |
388 | ((VARIABLE_HEADER *)CurrPtr)->State = VAR_ADDED;\r | |
389 | }\r | |
390 | CurrPtr += VariableSize;\r | |
391 | ValidBufferSize += VariableSize;\r | |
a24b4043 | 392 | if ((!StorageType) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r |
393 | mGlobal->HwErrVariableTotalSize += VariableSize;\r | |
394 | } else if ((!StorageType) && ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r | |
395 | mGlobal->CommonVariableTotalSize += VariableSize;\r | |
396 | }\r | |
9071550e | 397 | }\r |
398 | }\r | |
399 | Variable = NextVariable;\r | |
400 | }\r | |
401 | \r | |
a24b4043 | 402 | mGlobal->LastVariableOffset[StorageType] = ValidBufferSize;\r |
403 | \r | |
9071550e | 404 | //\r |
405 | // TODO: cannot restore to original state, basic FTW needed\r | |
406 | //\r | |
407 | Status = mGlobal->VariableStore[StorageType]->Erase (\r | |
408 | mGlobal->VariableStore[StorageType]\r | |
409 | );\r | |
410 | Status = mGlobal->VariableStore[StorageType]->Write (\r | |
411 | mGlobal->VariableStore[StorageType],\r | |
412 | 0,\r | |
413 | ValidBufferSize,\r | |
414 | ValidBuffer\r | |
415 | );\r | |
416 | \r | |
a24b4043 | 417 | if (EFI_ERROR (Status)) {\r |
418 | //\r | |
419 | // If error, then reset the last variable offset to zero.\r | |
420 | //\r | |
421 | mGlobal->LastVariableOffset[StorageType] = 0;\r | |
422 | };\r | |
9071550e | 423 | \r |
9071550e | 424 | gBS->FreePool (ValidBuffer);\r |
425 | \r | |
426 | return Status;\r | |
427 | }\r | |
428 | \r | |
9071550e | 429 | EFI_STATUS\r |
430 | FindVariable (\r | |
431 | IN CHAR16 *VariableName,\r | |
432 | IN EFI_GUID *VendorGuid,\r | |
433 | OUT VARIABLE_POINTER_TRACK *PtrTrack\r | |
434 | )\r | |
435 | /*++\r | |
436 | \r | |
437 | Routine Description:\r | |
438 | \r | |
439 | This code finds variable in storage blocks (Volatile or Non-Volatile)\r | |
440 | \r | |
441 | Arguments:\r | |
442 | \r | |
443 | VariableName Name of the variable to be found\r | |
444 | VendorGuid Vendor GUID to be found.\r | |
445 | PtrTrack Variable Track Pointer structure that contains\r | |
446 | Variable Information.\r | |
447 | Contains the pointer of Variable header.\r | |
448 | \r | |
449 | Returns:\r | |
450 | \r | |
451 | EFI_INVALID_PARAMETER - Invalid parameter\r | |
452 | EFI_SUCCESS - Find the specified variable\r | |
453 | EFI_NOT_FOUND - Not found\r | |
454 | \r | |
455 | --*/\r | |
456 | {\r | |
457 | VARIABLE_HEADER *Variable;\r | |
458 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
459 | UINTN Index;\r | |
460 | VARIABLE_HEADER *InDeleteVariable;\r | |
461 | UINTN InDeleteIndex;\r | |
462 | VARIABLE_HEADER *InDeleteStartPtr;\r | |
463 | VARIABLE_HEADER *InDeleteEndPtr;\r | |
464 | \r | |
465 | if (VariableName[0] != 0 && VendorGuid == NULL) {\r | |
466 | return EFI_INVALID_PARAMETER;\r | |
467 | }\r | |
468 | \r | |
469 | InDeleteVariable = NULL;\r | |
470 | InDeleteIndex = (UINTN)-1;\r | |
471 | InDeleteStartPtr = NULL;\r | |
472 | InDeleteEndPtr = NULL;\r | |
473 | \r | |
474 | for (Index = 0; Index < MaxType; Index ++) {\r | |
475 | //\r | |
476 | // 0: Non-Volatile, 1: Volatile\r | |
477 | //\r | |
478 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Index];\r | |
479 | \r | |
480 | //\r | |
481 | // Start Pointers for the variable.\r | |
482 | // Actual Data Pointer where data can be written.\r | |
483 | //\r | |
484 | Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r | |
485 | \r | |
486 | //\r | |
487 | // Find the variable by walk through non-volatile and volatile variable store\r | |
488 | //\r | |
489 | PtrTrack->StartPtr = Variable;\r | |
490 | PtrTrack->EndPtr = GetEndPointer (VariableStoreHeader);\r | |
491 | \r | |
a24b4043 | 492 | while ((Variable < PtrTrack->EndPtr) && IsValidVariableHeader (Variable)) {\r |
9071550e | 493 | if (Variable->State == VAR_ADDED) {\r |
494 | if (!EfiAtRuntime () || (Variable->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r | |
495 | if (VariableName[0] == 0) {\r | |
496 | PtrTrack->CurrPtr = Variable;\r | |
497 | PtrTrack->Type = (VARIABLE_STORAGE_TYPE) Index;\r | |
498 | return EFI_SUCCESS;\r | |
499 | } else {\r | |
500 | if (CompareGuid (VendorGuid, &Variable->VendorGuid)) {\r | |
501 | if (!CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable), StrSize (VariableName))) {\r | |
502 | PtrTrack->CurrPtr = Variable;\r | |
503 | PtrTrack->Type = (VARIABLE_STORAGE_TYPE) Index;\r | |
504 | return EFI_SUCCESS;\r | |
505 | }\r | |
506 | }\r | |
507 | }\r | |
508 | }\r | |
509 | } else if (Variable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)) {\r | |
510 | //\r | |
511 | // VAR_IN_DELETED_TRANSITION should also be checked.\r | |
512 | //\r | |
513 | if (!EfiAtRuntime () || (Variable->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r | |
514 | if (VariableName[0] == 0) {\r | |
515 | InDeleteVariable = Variable;\r | |
516 | InDeleteIndex = Index;\r | |
517 | InDeleteStartPtr = PtrTrack->StartPtr;\r | |
518 | InDeleteEndPtr = PtrTrack->EndPtr;\r | |
519 | } else {\r | |
520 | if (CompareGuid (VendorGuid, &Variable->VendorGuid)) {\r | |
521 | if (!CompareMem (VariableName, GET_VARIABLE_NAME_PTR (Variable), StrSize (VariableName))) {\r | |
522 | InDeleteVariable = Variable;\r | |
523 | InDeleteIndex = Index;\r | |
524 | InDeleteStartPtr = PtrTrack->StartPtr;\r | |
525 | InDeleteEndPtr = PtrTrack->EndPtr;\r | |
526 | }\r | |
527 | }\r | |
528 | }\r | |
529 | }\r | |
530 | }\r | |
531 | \r | |
532 | Variable = GetNextVariablePtr (Variable);\r | |
533 | }\r | |
534 | //\r | |
535 | // While (...)\r | |
536 | //\r | |
537 | }\r | |
538 | //\r | |
a24b4043 | 539 | // for (...)\r |
540 | //\r | |
541 | \r | |
542 | //\r | |
543 | // if VAR_IN_DELETED_TRANSITION found, and VAR_ADDED not found,\r | |
544 | // we return it.\r | |
545 | //\r | |
546 | if (InDeleteVariable != NULL) {\r | |
547 | PtrTrack->CurrPtr = InDeleteVariable;\r | |
548 | PtrTrack->Type = (VARIABLE_STORAGE_TYPE) InDeleteIndex;\r | |
549 | PtrTrack->StartPtr = InDeleteStartPtr;\r | |
550 | PtrTrack->EndPtr = InDeleteEndPtr;\r | |
551 | return EFI_SUCCESS;\r | |
552 | }\r | |
553 | \r | |
554 | PtrTrack->CurrPtr = NULL;\r | |
555 | return EFI_NOT_FOUND;\r | |
556 | }\r | |
557 | \r | |
558 | /**\r | |
559 | Get index from supported language codes according to language string.\r | |
560 | \r | |
561 | This code is used to get corresponding index in supported language codes. It can handle\r | |
607e91f5 | 562 | RFC4646 and ISO639 language tags.\r |
a24b4043 | 563 | In ISO639 language tags, take 3-characters as a delimitation to find matched string and calculate the index.\r |
607e91f5 | 564 | In RFC4646 language tags, take semicolon as a delimitation to find matched string and calculate the index.\r |
a24b4043 | 565 | \r |
566 | For example:\r | |
567 | SupportedLang = "engfraengfra"\r | |
568 | Lang = "eng"\r | |
569 | Iso639Language = TRUE\r | |
570 | The return value is "0".\r | |
571 | Another example:\r | |
572 | SupportedLang = "en;fr;en-US;fr-FR"\r | |
573 | Lang = "fr-FR"\r | |
574 | Iso639Language = FALSE\r | |
575 | The return value is "3".\r | |
576 | \r | |
577 | @param SupportedLang Platform supported language codes.\r | |
578 | @param Lang Configured language.\r | |
607e91f5 | 579 | @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r |
a24b4043 | 580 | \r |
581 | @retval the index of language in the language codes.\r | |
582 | \r | |
583 | **/\r | |
584 | UINTN\r | |
a24b4043 | 585 | GetIndexFromSupportedLangCodes(\r |
586 | IN CHAR8 *SupportedLang,\r | |
587 | IN CHAR8 *Lang,\r | |
588 | IN BOOLEAN Iso639Language\r | |
589 | ) \r | |
590 | {\r | |
591 | UINTN Index;\r | |
d7f79118 RN |
592 | UINTN CompareLength;\r |
593 | UINTN LanguageLength;\r | |
a24b4043 | 594 | \r |
a24b4043 | 595 | if (Iso639Language) {\r |
d7f79118 | 596 | CompareLength = ISO_639_2_ENTRY_SIZE;\r |
a24b4043 | 597 | for (Index = 0; Index < AsciiStrLen (SupportedLang); Index += CompareLength) {\r |
598 | if (AsciiStrnCmp (Lang, SupportedLang + Index, CompareLength) == 0) {\r | |
599 | //\r | |
600 | // Successfully find the index of Lang string in SupportedLang string.\r | |
601 | //\r | |
602 | Index = Index / CompareLength;\r | |
603 | return Index;\r | |
604 | }\r | |
605 | }\r | |
606 | ASSERT (FALSE);\r | |
607 | return 0;\r | |
608 | } else {\r | |
609 | //\r | |
607e91f5 | 610 | // Compare RFC4646 language code\r |
a24b4043 | 611 | //\r |
d7f79118 RN |
612 | Index = 0;\r |
613 | for (LanguageLength = 0; Lang[LanguageLength] != '\0'; LanguageLength++);\r | |
614 | \r | |
615 | for (Index = 0; *SupportedLang != '\0'; Index++, SupportedLang += CompareLength) {\r | |
a24b4043 | 616 | //\r |
d7f79118 | 617 | // Skip ';' characters in SupportedLang\r |
a24b4043 | 618 | //\r |
d7f79118 RN |
619 | for (; *SupportedLang != '\0' && *SupportedLang == ';'; SupportedLang++);\r |
620 | //\r | |
621 | // Determine the length of the next language code in SupportedLang\r | |
622 | //\r | |
623 | for (CompareLength = 0; SupportedLang[CompareLength] != '\0' && SupportedLang[CompareLength] != ';'; CompareLength++);\r | |
624 | \r | |
625 | if ((CompareLength == LanguageLength) && \r | |
626 | (AsciiStrnCmp (Lang, SupportedLang, CompareLength) == 0)) {\r | |
a24b4043 | 627 | //\r |
628 | // Successfully find the index of Lang string in SupportedLang string.\r | |
629 | //\r | |
630 | return Index;\r | |
631 | }\r | |
a24b4043 | 632 | }\r |
633 | ASSERT (FALSE);\r | |
634 | return 0;\r | |
635 | }\r | |
636 | }\r | |
637 | \r | |
638 | /**\r | |
639 | Get language string from supported language codes according to index.\r | |
640 | \r | |
641 | This code is used to get corresponding language string in supported language codes. It can handle\r | |
607e91f5 | 642 | RFC4646 and ISO639 language tags.\r |
a24b4043 | 643 | In ISO639 language tags, take 3-characters as a delimitation. Find language string according to the index.\r |
607e91f5 | 644 | In RFC4646 language tags, take semicolon as a delimitation. Find language string according to the index.\r |
a24b4043 | 645 | \r |
646 | For example:\r | |
647 | SupportedLang = "engfraengfra"\r | |
648 | Index = "1"\r | |
649 | Iso639Language = TRUE\r | |
650 | The return value is "fra".\r | |
651 | Another example:\r | |
652 | SupportedLang = "en;fr;en-US;fr-FR"\r | |
653 | Index = "1"\r | |
654 | Iso639Language = FALSE\r | |
655 | The return value is "fr".\r | |
656 | \r | |
657 | @param SupportedLang Platform supported language codes.\r | |
658 | @param Index the index in supported language codes.\r | |
607e91f5 | 659 | @param Iso639Language A bool value to signify if the handler is operated on ISO639 or RFC4646.\r |
a24b4043 | 660 | \r |
661 | @retval the language string in the language codes.\r | |
662 | \r | |
663 | **/\r | |
664 | CHAR8 *\r | |
a24b4043 | 665 | GetLangFromSupportedLangCodes (\r |
666 | IN CHAR8 *SupportedLang,\r | |
667 | IN UINTN Index,\r | |
668 | IN BOOLEAN Iso639Language\r | |
669 | )\r | |
670 | {\r | |
671 | UINTN SubIndex;\r | |
d7f79118 | 672 | UINTN CompareLength;\r |
a24b4043 | 673 | CHAR8 *Supported;\r |
674 | \r | |
675 | SubIndex = 0;\r | |
676 | Supported = SupportedLang;\r | |
677 | if (Iso639Language) {\r | |
678 | //\r | |
679 | // according to the index of Lang string in SupportedLang string to get the language.\r | |
680 | // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r | |
681 | // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r | |
682 | //\r | |
d7f79118 RN |
683 | CompareLength = ISO_639_2_ENTRY_SIZE;\r |
684 | mGlobal->Lang[CompareLength] = '\0';\r | |
a24b4043 | 685 | return CopyMem (mGlobal->Lang, SupportedLang + Index * CompareLength, CompareLength);\r |
d7f79118 | 686 | \r |
a24b4043 | 687 | } else {\r |
688 | while (TRUE) {\r | |
689 | //\r | |
690 | // take semicolon as delimitation, sequentially traverse supported language codes.\r | |
691 | //\r | |
692 | for (CompareLength = 0; *Supported != ';' && *Supported != '\0'; CompareLength++) {\r | |
693 | Supported++;\r | |
694 | }\r | |
695 | if ((*Supported == '\0') && (SubIndex != Index)) {\r | |
696 | //\r | |
697 | // Have completed the traverse, but not find corrsponding string.\r | |
698 | // This case is not allowed to happen.\r | |
699 | //\r | |
700 | ASSERT(FALSE);\r | |
701 | return NULL;\r | |
702 | }\r | |
703 | if (SubIndex == Index) {\r | |
704 | //\r | |
705 | // according to the index of Lang string in SupportedLang string to get the language.\r | |
706 | // As this code will be invoked in RUNTIME, therefore there is not memory allocate/free operation.\r | |
707 | // In driver entry, it pre-allocates a runtime attribute memory to accommodate this string.\r | |
708 | //\r | |
d7f79118 | 709 | mGlobal->PlatformLang[CompareLength] = '\0';\r |
a24b4043 | 710 | return CopyMem (mGlobal->PlatformLang, Supported - CompareLength, CompareLength);\r |
711 | }\r | |
712 | SubIndex++;\r | |
462979af RN |
713 | \r |
714 | //\r | |
715 | // Skip ';' characters in Supported\r | |
716 | //\r | |
717 | for (; *Supported != '\0' && *Supported == ';'; Supported++);\r | |
a24b4043 | 718 | }\r |
719 | }\r | |
720 | }\r | |
721 | \r | |
d7f79118 RN |
722 | /**\r |
723 | Returns a pointer to an allocated buffer that contains the best matching language \r | |
724 | from a set of supported languages. \r | |
725 | \r | |
726 | This function supports both ISO 639-2 and RFC 4646 language codes, but language \r | |
727 | code types may not be mixed in a single call to this function. This function\r | |
728 | supports a variable argument list that allows the caller to pass in a prioritized\r | |
729 | list of language codes to test against all the language codes in SupportedLanguages.\r | |
730 | \r | |
731 | If SupportedLanguages is NULL, then ASSERT().\r | |
732 | \r | |
733 | @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string that\r | |
734 | contains a set of language codes in the format \r | |
735 | specified by Iso639Language.\r | |
736 | @param[in] Iso639Language If TRUE, then all language codes are assumed to be\r | |
737 | in ISO 639-2 format. If FALSE, then all language\r | |
738 | codes are assumed to be in RFC 4646 language format\r | |
739 | @param[in] ... A variable argument list that contains pointers to \r | |
740 | Null-terminated ASCII strings that contain one or more\r | |
741 | language codes in the format specified by Iso639Language.\r | |
742 | The first language code from each of these language\r | |
743 | code lists is used to determine if it is an exact or\r | |
744 | close match to any of the language codes in \r | |
745 | SupportedLanguages. Close matches only apply to RFC 4646\r | |
746 | language codes, and the matching algorithm from RFC 4647\r | |
747 | is used to determine if a close match is present. If \r | |
748 | an exact or close match is found, then the matching\r | |
749 | language code from SupportedLanguages is returned. If\r | |
750 | no matches are found, then the next variable argument\r | |
751 | parameter is evaluated. The variable argument list \r | |
752 | is terminated by a NULL.\r | |
753 | \r | |
754 | @retval NULL The best matching language could not be found in SupportedLanguages.\r | |
755 | @retval NULL There are not enough resources available to return the best matching \r | |
756 | language.\r | |
757 | @retval Other A pointer to a Null-terminated ASCII string that is the best matching \r | |
758 | language in SupportedLanguages.\r | |
759 | \r | |
760 | **/\r | |
761 | CHAR8 *\r | |
e1adae60 | 762 | EFIAPI\r |
d7f79118 RN |
763 | VariableGetBestLanguage (\r |
764 | IN CONST CHAR8 *SupportedLanguages, \r | |
765 | IN BOOLEAN Iso639Language,\r | |
766 | ...\r | |
767 | )\r | |
768 | {\r | |
769 | VA_LIST Args;\r | |
770 | CHAR8 *Language;\r | |
771 | UINTN CompareLength;\r | |
772 | UINTN LanguageLength;\r | |
773 | CONST CHAR8 *Supported;\r | |
774 | CHAR8 *Buffer;\r | |
775 | \r | |
776 | ASSERT (SupportedLanguages != NULL);\r | |
777 | \r | |
778 | VA_START (Args, Iso639Language);\r | |
779 | while ((Language = VA_ARG (Args, CHAR8 *)) != NULL) {\r | |
780 | //\r | |
781 | // Default to ISO 639-2 mode\r | |
782 | //\r | |
783 | CompareLength = 3;\r | |
784 | LanguageLength = MIN (3, AsciiStrLen (Language));\r | |
785 | \r | |
786 | //\r | |
787 | // If in RFC 4646 mode, then determine the length of the first RFC 4646 language code in Language\r | |
788 | //\r | |
789 | if (!Iso639Language) {\r | |
790 | for (LanguageLength = 0; Language[LanguageLength] != 0 && Language[LanguageLength] != ';'; LanguageLength++);\r | |
791 | }\r | |
792 | \r | |
793 | //\r | |
794 | // Trim back the length of Language used until it is empty\r | |
795 | //\r | |
796 | while (LanguageLength > 0) {\r | |
797 | //\r | |
798 | // Loop through all language codes in SupportedLanguages\r | |
799 | //\r | |
800 | for (Supported = SupportedLanguages; *Supported != '\0'; Supported += CompareLength) {\r | |
801 | //\r | |
802 | // In RFC 4646 mode, then Loop through all language codes in SupportedLanguages\r | |
803 | //\r | |
804 | if (!Iso639Language) {\r | |
805 | //\r | |
806 | // Skip ';' characters in Supported\r | |
807 | //\r | |
808 | for (; *Supported != '\0' && *Supported == ';'; Supported++);\r | |
809 | //\r | |
810 | // Determine the length of the next language code in Supported\r | |
811 | //\r | |
812 | for (CompareLength = 0; Supported[CompareLength] != 0 && Supported[CompareLength] != ';'; CompareLength++);\r | |
813 | //\r | |
814 | // If Language is longer than the Supported, then skip to the next language\r | |
815 | //\r | |
816 | if (LanguageLength > CompareLength) {\r | |
817 | continue;\r | |
818 | }\r | |
819 | }\r | |
820 | //\r | |
821 | // See if the first LanguageLength characters in Supported match Language\r | |
822 | //\r | |
823 | if (AsciiStrnCmp (Supported, Language, LanguageLength) == 0) {\r | |
824 | VA_END (Args);\r | |
825 | \r | |
826 | Buffer = Iso639Language ? mGlobal->Lang : mGlobal->PlatformLang;\r | |
827 | Buffer[CompareLength] = '\0';\r | |
828 | return CopyMem (Buffer, Supported, CompareLength);\r | |
829 | }\r | |
830 | }\r | |
831 | \r | |
832 | if (Iso639Language) {\r | |
833 | //\r | |
834 | // If ISO 639 mode, then each language can only be tested once\r | |
835 | //\r | |
836 | LanguageLength = 0;\r | |
837 | } else {\r | |
838 | //\r | |
839 | // If RFC 4646 mode, then trim Language from the right to the next '-' character \r | |
840 | //\r | |
841 | for (LanguageLength--; LanguageLength > 0 && Language[LanguageLength] != '-'; LanguageLength--);\r | |
842 | }\r | |
843 | }\r | |
844 | }\r | |
845 | VA_END (Args);\r | |
846 | \r | |
847 | //\r | |
848 | // No matches were found \r | |
849 | //\r | |
850 | return NULL;\r | |
851 | }\r | |
852 | \r | |
a24b4043 | 853 | /**\r |
854 | Hook the operations in PlatformLangCodes, LangCodes, PlatformLang and Lang.\r | |
855 | \r | |
856 | When setting Lang/LangCodes, simultaneously update PlatformLang/PlatformLangCodes.\r | |
857 | \r | |
858 | According to UEFI spec, PlatformLangCodes/LangCodes are only set once in firmware initialization,\r | |
859 | and are read-only. Therefore, in variable driver, only store the original value for other use.\r | |
860 | \r | |
861 | @param[in] VariableName Name of variable\r | |
862 | \r | |
863 | @param[in] Data Variable data\r | |
864 | \r | |
865 | @param[in] DataSize Size of data. 0 means delete\r | |
866 | \r | |
a24b4043 | 867 | **/\r |
d7f79118 | 868 | VOID\r |
a24b4043 | 869 | AutoUpdateLangVariable(\r |
870 | IN CHAR16 *VariableName,\r | |
871 | IN VOID *Data,\r | |
872 | IN UINTN DataSize\r | |
873 | )\r | |
874 | {\r | |
d7f79118 RN |
875 | EFI_STATUS Status;\r |
876 | CHAR8 *BestPlatformLang;\r | |
877 | CHAR8 *BestLang;\r | |
878 | UINTN Index;\r | |
879 | UINT32 Attributes;\r | |
a24b4043 | 880 | VARIABLE_POINTER_TRACK Variable;\r |
d7f79118 | 881 | BOOLEAN SetLanguageCodes;\r |
a24b4043 | 882 | \r |
883 | //\r | |
d7f79118 | 884 | // Don't do updates for delete operation\r |
a24b4043 | 885 | //\r |
d7f79118 RN |
886 | if (DataSize == 0) {\r |
887 | return;\r | |
888 | }\r | |
889 | \r | |
890 | SetLanguageCodes = FALSE;\r | |
a24b4043 | 891 | \r |
892 | if (StrCmp (VariableName, L"PlatformLangCodes") == 0) {\r | |
d7f79118 RN |
893 | //\r |
894 | // PlatformLangCodes is a volatile variable, so it can not be updated at runtime.\r | |
895 | //\r | |
896 | if (EfiAtRuntime ()) {\r | |
897 | return;\r | |
898 | }\r | |
899 | \r | |
900 | SetLanguageCodes = TRUE;\r | |
901 | \r | |
a24b4043 | 902 | //\r |
903 | // According to UEFI spec, PlatformLangCodes is only set once in firmware initialization, and is read-only\r | |
904 | // Therefore, in variable driver, only store the original value for other use.\r | |
905 | //\r | |
d7f79118 RN |
906 | if (mGlobal->PlatformLangCodes != NULL) {\r |
907 | FreePool (mGlobal->PlatformLangCodes);\r | |
908 | }\r | |
909 | mGlobal->PlatformLangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r | |
910 | ASSERT (mGlobal->PlatformLangCodes != NULL);\r | |
911 | \r | |
a24b4043 | 912 | //\r |
d7f79118 RN |
913 | // PlatformLang holds a single language from PlatformLangCodes, \r |
914 | // so the size of PlatformLangCodes is enough for the PlatformLang.\r | |
a24b4043 | 915 | //\r |
d7f79118 RN |
916 | if (mGlobal->PlatformLang != NULL) {\r |
917 | FreePool (mGlobal->PlatformLang);\r | |
918 | }\r | |
919 | mGlobal->PlatformLang = AllocateRuntimePool (DataSize);\r | |
920 | ASSERT (mGlobal->PlatformLang != NULL);\r | |
a24b4043 | 921 | \r |
d7f79118 | 922 | } else if (StrCmp (VariableName, L"LangCodes") == 0) {\r |
a24b4043 | 923 | //\r |
d7f79118 | 924 | // LangCodes is a volatile variable, so it can not be updated at runtime.\r |
a24b4043 | 925 | //\r |
d7f79118 RN |
926 | if (EfiAtRuntime ()) {\r |
927 | return;\r | |
928 | }\r | |
929 | \r | |
930 | SetLanguageCodes = TRUE;\r | |
a24b4043 | 931 | \r |
932 | //\r | |
d7f79118 RN |
933 | // According to UEFI spec, LangCodes is only set once in firmware initialization, and is read-only\r |
934 | // Therefore, in variable driver, only store the original value for other use.\r | |
a24b4043 | 935 | //\r |
d7f79118 RN |
936 | if (mGlobal->LangCodes != NULL) {\r |
937 | FreePool (mGlobal->LangCodes);\r | |
938 | }\r | |
939 | mGlobal->LangCodes = AllocateRuntimeCopyPool (DataSize, Data);\r | |
940 | ASSERT (mGlobal->LangCodes != NULL);\r | |
941 | }\r | |
a24b4043 | 942 | \r |
d7f79118 RN |
943 | if (SetLanguageCodes \r |
944 | && (mGlobal->PlatformLangCodes != NULL)\r | |
945 | && (mGlobal->LangCodes != NULL)) {\r | |
a24b4043 | 946 | //\r |
d7f79118 RN |
947 | // Update Lang if PlatformLang is already set\r |
948 | // Update PlatformLang if Lang is already set\r | |
a24b4043 | 949 | //\r |
d7f79118 RN |
950 | Status = FindVariable (L"PlatformLang", &gEfiGlobalVariableGuid, &Variable);\r |
951 | if (!EFI_ERROR (Status)) {\r | |
952 | //\r | |
953 | // Update Lang\r | |
954 | //\r | |
955 | VariableName = L"PlatformLang";\r | |
956 | Data = GetVariableDataPtr (Variable.CurrPtr);\r | |
957 | DataSize = Variable.CurrPtr->DataSize;\r | |
958 | } else {\r | |
959 | Status = FindVariable (L"Lang", &gEfiGlobalVariableGuid, &Variable);\r | |
960 | if (!EFI_ERROR (Status)) {\r | |
961 | //\r | |
962 | // Update PlatformLang\r | |
963 | //\r | |
964 | VariableName = L"Lang";\r | |
965 | Data = GetVariableDataPtr (Variable.CurrPtr);\r | |
966 | DataSize = Variable.CurrPtr->DataSize;\r | |
967 | } else {\r | |
968 | //\r | |
969 | // Neither PlatformLang nor Lang is set, directly return\r | |
970 | //\r | |
971 | return;\r | |
972 | }\r | |
973 | }\r | |
974 | }\r | |
975 | \r | |
976 | //\r | |
977 | // According to UEFI spec, "Lang" and "PlatformLang" is NV|BS|RT attributions.\r | |
978 | //\r | |
979 | Attributes = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS;\r | |
a24b4043 | 980 | \r |
d7f79118 | 981 | if (StrCmp (VariableName, L"PlatformLang") == 0) {\r |
a24b4043 | 982 | //\r |
d7f79118 | 983 | // Update Lang when PlatformLangCodes/LangCodes were set.\r |
a24b4043 | 984 | //\r |
d7f79118 RN |
985 | if ((mGlobal->PlatformLangCodes != NULL) && (mGlobal->LangCodes != NULL)) {\r |
986 | //\r | |
987 | // When setting PlatformLang, firstly get most matched language string from supported language codes.\r | |
988 | //\r | |
989 | BestPlatformLang = VariableGetBestLanguage (mGlobal->PlatformLangCodes, FALSE, Data, NULL);\r | |
990 | if (BestPlatformLang != NULL) {\r | |
991 | //\r | |
992 | // Get the corresponding index in language codes.\r | |
993 | //\r | |
994 | Index = GetIndexFromSupportedLangCodes (mGlobal->PlatformLangCodes, BestPlatformLang, FALSE);\r | |
a24b4043 | 995 | \r |
d7f79118 RN |
996 | //\r |
997 | // Get the corresponding ISO639 language tag according to RFC4646 language tag.\r | |
998 | //\r | |
999 | BestLang = GetLangFromSupportedLangCodes (mGlobal->LangCodes, Index, TRUE);\r | |
a24b4043 | 1000 | \r |
d7f79118 RN |
1001 | //\r |
1002 | // Successfully convert PlatformLang to Lang, and set the BestLang value into Lang variable simultaneously.\r | |
1003 | //\r | |
1004 | FindVariable(L"Lang", &gEfiGlobalVariableGuid, &Variable);\r | |
a24b4043 | 1005 | \r |
d7f79118 | 1006 | Status = UpdateVariable (L"Lang", &gEfiGlobalVariableGuid, BestLang, ISO_639_2_ENTRY_SIZE + 1, Attributes, &Variable);\r |
a24b4043 | 1007 | \r |
d7f79118 | 1008 | DEBUG ((EFI_D_INFO, "Variable Driver Auto Update PlatformLang, PlatformLang:%a, Lang:%a\n", BestPlatformLang, BestLang));\r |
a24b4043 | 1009 | \r |
d7f79118 RN |
1010 | ASSERT_EFI_ERROR(Status);\r |
1011 | }\r | |
1012 | }\r | |
a24b4043 | 1013 | \r |
d7f79118 | 1014 | } else if (StrCmp (VariableName, L"Lang") == 0) {\r |
a24b4043 | 1015 | //\r |
d7f79118 | 1016 | // Update PlatformLang when PlatformLangCodes/LangCodes were set.\r |
a24b4043 | 1017 | //\r |
d7f79118 RN |
1018 | if ((mGlobal->PlatformLangCodes != NULL) && (mGlobal->LangCodes != NULL)) {\r |
1019 | //\r | |
1020 | // When setting Lang, firstly get most matched language string from supported language codes.\r | |
1021 | //\r | |
1022 | BestLang = VariableGetBestLanguage (mGlobal->LangCodes, TRUE, Data, NULL);\r | |
1023 | if (BestLang != NULL) {\r | |
1024 | //\r | |
1025 | // Get the corresponding index in language codes.\r | |
1026 | //\r | |
1027 | Index = GetIndexFromSupportedLangCodes (mGlobal->LangCodes, BestLang, TRUE);\r | |
a24b4043 | 1028 | \r |
d7f79118 RN |
1029 | //\r |
1030 | // Get the corresponding RFC4646 language tag according to ISO639 language tag.\r | |
1031 | //\r | |
1032 | BestPlatformLang = GetLangFromSupportedLangCodes (mGlobal->PlatformLangCodes, Index, FALSE);\r | |
1033 | \r | |
1034 | //\r | |
1035 | // Successfully convert Lang to PlatformLang, and set the BestPlatformLang value into PlatformLang variable simultaneously.\r | |
1036 | //\r | |
1037 | FindVariable(L"PlatformLang", &gEfiGlobalVariableGuid, &Variable);\r | |
a24b4043 | 1038 | \r |
d7f79118 RN |
1039 | Status = UpdateVariable (L"PlatformLang", &gEfiGlobalVariableGuid, BestPlatformLang, \r |
1040 | AsciiStrSize (BestPlatformLang), Attributes, &Variable);\r | |
a24b4043 | 1041 | \r |
d7f79118 RN |
1042 | DEBUG ((EFI_D_INFO, "Variable Driver Auto Update Lang, Lang:%a, PlatformLang:%a\n", BestLang, BestPlatformLang));\r |
1043 | ASSERT_EFI_ERROR (Status);\r | |
1044 | }\r | |
1045 | }\r | |
a24b4043 | 1046 | }\r |
a24b4043 | 1047 | }\r |
1048 | \r | |
1049 | /**\r | |
1050 | Update the variable region with Variable information. These are the same \r | |
1051 | arguments as the EFI Variable services.\r | |
1052 | \r | |
1053 | @param[in] VariableName Name of variable\r | |
1054 | \r | |
1055 | @param[in] VendorGuid Guid of variable\r | |
1056 | \r | |
1057 | @param[in] Data Variable data\r | |
1058 | \r | |
1059 | @param[in] DataSize Size of data. 0 means delete\r | |
1060 | \r | |
b29a823d | 1061 | @param[in] Attributes Attribues of the variable\r |
a24b4043 | 1062 | \r |
1063 | @param[in] Variable The variable information which is used to keep track of variable usage.\r | |
1064 | \r | |
1065 | @retval EFI_SUCCESS The update operation is success.\r | |
1066 | \r | |
1067 | @retval EFI_OUT_OF_RESOURCES Variable region is full, can not write other data into this region.\r | |
1068 | \r | |
1069 | **/\r | |
1070 | EFI_STATUS\r | |
1071 | EFIAPI\r | |
1072 | UpdateVariable (\r | |
1073 | IN CHAR16 *VariableName,\r | |
1074 | IN EFI_GUID *VendorGuid,\r | |
1075 | IN VOID *Data,\r | |
1076 | IN UINTN DataSize,\r | |
1077 | IN UINT32 Attributes OPTIONAL,\r | |
1078 | IN VARIABLE_POINTER_TRACK *Variable\r | |
1079 | )\r | |
1080 | {\r | |
1081 | EFI_STATUS Status;\r | |
1082 | VARIABLE_HEADER *NextVariable;\r | |
1083 | UINTN VarNameOffset;\r | |
1084 | UINTN VarDataOffset;\r | |
1085 | UINTN VarNameSize;\r | |
1086 | UINTN VarSize;\r | |
1087 | UINT8 State;\r | |
1088 | BOOLEAN Reclaimed;\r | |
1089 | VARIABLE_STORAGE_TYPE StorageType;\r | |
1090 | \r | |
1091 | Reclaimed = FALSE;\r | |
1092 | \r | |
1093 | if (Variable->CurrPtr != NULL) { \r | |
1094 | //\r | |
1095 | // Update/Delete existing variable\r | |
1096 | //\r | |
1097 | \r | |
1098 | if (EfiAtRuntime ()) { \r | |
1099 | //\r | |
1100 | // If EfiAtRuntime and the variable is Volatile and Runtime Access, \r | |
1101 | // the volatile is ReadOnly, and SetVariable should be aborted and \r | |
1102 | // return EFI_WRITE_PROTECTED.\r | |
1103 | //\r | |
1104 | if (Variable->Type == Volatile) {\r | |
1105 | return EFI_WRITE_PROTECTED;\r | |
1106 | }\r | |
1107 | //\r | |
1108 | // Only variable have NV attribute can be updated/deleted in Runtime\r | |
1109 | //\r | |
1110 | if (!(Variable->CurrPtr->Attributes & EFI_VARIABLE_NON_VOLATILE)) {\r | |
1111 | return EFI_INVALID_PARAMETER; \r | |
1112 | }\r | |
1113 | }\r | |
1114 | \r | |
1115 | //\r | |
1116 | // Setting a data variable with no access, or zero DataSize attributes\r | |
1117 | // specified causes it to be deleted.\r | |
1118 | //\r | |
1119 | if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r | |
1120 | //\r | |
1121 | // Found this variable in storage\r | |
1122 | //\r | |
1123 | State = Variable->CurrPtr->State;\r | |
1124 | State &= VAR_DELETED;\r | |
1125 | \r | |
1126 | Status = mGlobal->VariableStore[Variable->Type]->Write (\r | |
1127 | mGlobal->VariableStore[Variable->Type],\r | |
1128 | VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr),\r | |
1129 | sizeof (Variable->CurrPtr->State),\r | |
1130 | &State\r | |
1131 | );\r | |
1132 | //\r | |
1133 | // NOTE: Write operation at least can write data to memory cache\r | |
1134 | // Discard file writing failure here.\r | |
1135 | //\r | |
1136 | return EFI_SUCCESS;\r | |
1137 | }\r | |
1138 | \r | |
1139 | //\r | |
1140 | // Found this variable in storage\r | |
1141 | // If the variable is marked valid and the same data has been passed in\r | |
1142 | // then return to the caller immediately.\r | |
1143 | //\r | |
1144 | if ((Variable->CurrPtr->DataSize == DataSize) &&\r | |
1145 | (CompareMem (Data, GetVariableDataPtr (Variable->CurrPtr), DataSize) == 0)\r | |
1146 | ) {\r | |
1147 | return EFI_SUCCESS;\r | |
1148 | } else if ((Variable->CurrPtr->State == VAR_ADDED) ||\r | |
1149 | (Variable->CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r | |
1150 | //\r | |
1151 | // Mark the old variable as in delete transition\r | |
1152 | //\r | |
1153 | State = Variable->CurrPtr->State;\r | |
1154 | State &= VAR_IN_DELETED_TRANSITION;\r | |
1155 | \r | |
1156 | Status = mGlobal->VariableStore[Variable->Type]->Write (\r | |
1157 | mGlobal->VariableStore[Variable->Type],\r | |
1158 | VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr),\r | |
1159 | sizeof (Variable->CurrPtr->State),\r | |
1160 | &State\r | |
1161 | );\r | |
1162 | //\r | |
1163 | // NOTE: Write operation at least can write data to memory cache\r | |
1164 | // Discard file writing failure here.\r | |
1165 | //\r | |
1166 | }\r | |
1167 | } else {\r | |
1168 | //\r | |
1169 | // Create a new variable\r | |
1170 | // \r | |
1171 | \r | |
1172 | //\r | |
1173 | // Make sure we are trying to create a new variable.\r | |
1174 | // Setting a data variable with no access, or zero DataSize attributes means to delete it. \r | |
1175 | //\r | |
1176 | if (DataSize == 0 || (Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == 0) {\r | |
1177 | return EFI_NOT_FOUND;\r | |
1178 | } \r | |
1179 | //\r | |
1180 | // Only variable have NV|RT attribute can be created in Runtime\r | |
1181 | //\r | |
1182 | if (EfiAtRuntime () &&\r | |
1183 | (!(Attributes & EFI_VARIABLE_RUNTIME_ACCESS) || !(Attributes & EFI_VARIABLE_NON_VOLATILE))) {\r | |
1184 | return EFI_INVALID_PARAMETER;\r | |
1185 | } \r | |
1186 | \r | |
1187 | } \r | |
1188 | \r | |
1189 | //\r | |
1190 | // Function part - create a new variable and copy the data.\r | |
1191 | // Both update a variable and create a variable will come here. \r | |
1192 | // We can firstly write all the data in memory, then write them to file\r | |
1193 | // This can reduce the times of write operation\r | |
1194 | //\r | |
1195 | \r | |
1196 | NextVariable = (VARIABLE_HEADER *) mGlobal->Scratch;\r | |
1197 | \r | |
1198 | NextVariable->StartId = VARIABLE_DATA;\r | |
1199 | NextVariable->Attributes = Attributes;\r | |
1200 | NextVariable->State = VAR_ADDED;\r | |
1201 | NextVariable->Reserved = 0;\r | |
1202 | VarNameOffset = sizeof (VARIABLE_HEADER);\r | |
1203 | VarNameSize = StrSize (VariableName);\r | |
1204 | CopyMem (\r | |
1205 | (UINT8 *) ((UINTN) NextVariable + VarNameOffset),\r | |
1206 | VariableName,\r | |
1207 | VarNameSize\r | |
1208 | );\r | |
1209 | VarDataOffset = VarNameOffset + VarNameSize + GET_PAD_SIZE (VarNameSize);\r | |
1210 | CopyMem (\r | |
1211 | (UINT8 *) ((UINTN) NextVariable + VarDataOffset),\r | |
1212 | Data,\r | |
1213 | DataSize\r | |
1214 | );\r | |
1215 | CopyMem (&NextVariable->VendorGuid, VendorGuid, sizeof (EFI_GUID));\r | |
1216 | //\r | |
1217 | // There will be pad bytes after Data, the NextVariable->NameSize and\r | |
1218 | // NextVariable->DataSize should not include pad size so that variable\r | |
1219 | // service can get actual size in GetVariable\r | |
1220 | //\r | |
1221 | NextVariable->NameSize = (UINT32)VarNameSize;\r | |
1222 | NextVariable->DataSize = (UINT32)DataSize;\r | |
1223 | \r | |
1224 | //\r | |
1225 | // The actual size of the variable that stores in storage should\r | |
1226 | // include pad size.\r | |
1227 | // VarDataOffset: offset from begin of current variable header\r | |
1228 | //\r | |
1229 | VarSize = VarDataOffset + DataSize + GET_PAD_SIZE (DataSize);\r | |
1230 | \r | |
1231 | StorageType = (Attributes & EFI_VARIABLE_NON_VOLATILE) ? NonVolatile : Volatile;\r | |
1232 | \r | |
1233 | if ((UINT32) (VarSize + mGlobal->LastVariableOffset[StorageType]) >\r | |
1234 | ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType])->Size\r | |
1235 | ) {\r | |
1236 | if ((StorageType == NonVolatile) && EfiAtRuntime ()) {\r | |
1237 | return EFI_OUT_OF_RESOURCES;\r | |
1238 | }\r | |
1239 | //\r | |
1240 | // Perform garbage collection & reclaim operation\r | |
1241 | //\r | |
1242 | Status = Reclaim (StorageType, Variable->CurrPtr);\r | |
1243 | if (EFI_ERROR (Status)) {\r | |
1244 | //\r | |
1245 | // Reclaim error\r | |
1246 | // we cannot restore to original state, fetal error, report to user\r | |
1247 | //\r | |
1248 | DEBUG ((EFI_D_ERROR, "FSVariable: Recalim error (fetal error) - %r\n", Status));\r | |
1249 | return Status;\r | |
1250 | }\r | |
1251 | //\r | |
1252 | // If still no enough space, return out of resources\r | |
1253 | //\r | |
1254 | if ((UINT32) (VarSize + mGlobal->LastVariableOffset[StorageType]) >\r | |
1255 | ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[StorageType])->Size\r | |
1256 | ) {\r | |
1257 | return EFI_OUT_OF_RESOURCES;\r | |
1258 | }\r | |
1259 | \r | |
1260 | Reclaimed = TRUE;\r | |
1261 | }\r | |
1262 | Status = mGlobal->VariableStore[StorageType]->Write (\r | |
1263 | mGlobal->VariableStore[StorageType],\r | |
1264 | mGlobal->LastVariableOffset[StorageType],\r | |
1265 | VarSize,\r | |
1266 | NextVariable\r | |
1267 | );\r | |
1268 | //\r | |
1269 | // NOTE: Write operation at least can write data to memory cache\r | |
1270 | // Discard file writing failure here.\r | |
9071550e | 1271 | //\r |
a24b4043 | 1272 | mGlobal->LastVariableOffset[StorageType] += VarSize;\r |
1273 | \r | |
1274 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {\r | |
1275 | mGlobal->HwErrVariableTotalSize += VarSize;\r | |
1276 | } else {\r | |
1277 | mGlobal->CommonVariableTotalSize += VarSize;\r | |
1278 | }\r | |
9071550e | 1279 | \r |
1280 | //\r | |
a24b4043 | 1281 | // Mark the old variable as deleted\r |
9071550e | 1282 | //\r |
a24b4043 | 1283 | if (!Reclaimed && !EFI_ERROR (Status) && Variable->CurrPtr != NULL) {\r |
1284 | State = Variable->CurrPtr->State;\r | |
1285 | State &= VAR_DELETED;\r | |
9071550e | 1286 | \r |
a24b4043 | 1287 | Status = mGlobal->VariableStore[StorageType]->Write (\r |
1288 | mGlobal->VariableStore[StorageType],\r | |
1289 | VARIABLE_MEMBER_OFFSET (State, (UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr),\r | |
1290 | sizeof (Variable->CurrPtr->State),\r | |
1291 | &State\r | |
1292 | );\r | |
1293 | //\r | |
1294 | // NOTE: Write operation at least can write data to memory cache\r | |
1295 | // Discard file writing failure here.\r | |
1296 | //\r | |
1297 | }\r | |
1298 | return EFI_SUCCESS;\r | |
9071550e | 1299 | }\r |
1300 | \r | |
1301 | EFI_STATUS\r | |
1302 | EFIAPI\r | |
3ffa0f1f | 1303 | DuetGetVariable (\r |
9071550e | 1304 | IN CHAR16 *VariableName,\r |
1305 | IN EFI_GUID *VendorGuid,\r | |
1306 | OUT UINT32 *Attributes OPTIONAL,\r | |
1307 | IN OUT UINTN *DataSize,\r | |
e286e118 | 1308 | OUT VOID *Data OPTIONAL\r |
9071550e | 1309 | )\r |
1310 | /*++\r | |
1311 | \r | |
1312 | Routine Description:\r | |
1313 | \r | |
1314 | This code finds variable in storage blocks (Volatile or Non-Volatile)\r | |
1315 | \r | |
1316 | Arguments:\r | |
1317 | \r | |
1318 | VariableName Name of Variable to be found\r | |
1319 | VendorGuid Variable vendor GUID\r | |
1320 | Attributes OPTIONAL Attribute value of the variable found\r | |
1321 | DataSize Size of Data found. If size is less than the\r | |
1322 | data, this value contains the required size.\r | |
e286e118 SZ |
1323 | Data The buffer to return the contents of the variable. May be NULL\r |
1324 | with a zero DataSize in order to determine the size buffer needed.\r | |
9071550e | 1325 | \r |
1326 | Returns:\r | |
1327 | \r | |
1328 | EFI STATUS\r | |
1329 | \r | |
1330 | --*/\r | |
1331 | {\r | |
1332 | VARIABLE_POINTER_TRACK Variable;\r | |
1333 | UINTN VarDataSize;\r | |
1334 | EFI_STATUS Status;\r | |
1335 | \r | |
1336 | if (VariableName == NULL || VendorGuid == NULL || DataSize == NULL) {\r | |
1337 | return EFI_INVALID_PARAMETER;\r | |
1338 | }\r | |
1339 | \r | |
053d95e7 SZ |
1340 | if (VariableName[0] == 0) {\r |
1341 | return EFI_NOT_FOUND;\r | |
1342 | }\r | |
1343 | \r | |
9071550e | 1344 | //\r |
1345 | // Find existing variable\r | |
1346 | //\r | |
1347 | Status = FindVariable (VariableName, VendorGuid, &Variable);\r | |
1348 | \r | |
1349 | if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r | |
1350 | return Status;\r | |
1351 | }\r | |
1352 | //\r | |
1353 | // Get data size\r | |
1354 | //\r | |
1355 | VarDataSize = Variable.CurrPtr->DataSize;\r | |
1356 | if (*DataSize >= VarDataSize) {\r | |
1357 | if (Data == NULL) {\r | |
1358 | return EFI_INVALID_PARAMETER;\r | |
1359 | }\r | |
1360 | CopyMem (Data, GetVariableDataPtr (Variable.CurrPtr), VarDataSize);\r | |
1361 | \r | |
1362 | if (Attributes != NULL) {\r | |
1363 | *Attributes = Variable.CurrPtr->Attributes;\r | |
1364 | }\r | |
1365 | \r | |
1366 | *DataSize = VarDataSize;\r | |
1367 | \r | |
1368 | return EFI_SUCCESS;\r | |
1369 | } else {\r | |
1370 | *DataSize = VarDataSize;\r | |
1371 | return EFI_BUFFER_TOO_SMALL;\r | |
1372 | }\r | |
1373 | }\r | |
1374 | \r | |
1375 | EFI_STATUS\r | |
1376 | EFIAPI\r | |
1377 | GetNextVariableName (\r | |
1378 | IN OUT UINTN *VariableNameSize,\r | |
1379 | IN OUT CHAR16 *VariableName,\r | |
1380 | IN OUT EFI_GUID *VendorGuid\r | |
1381 | )\r | |
1382 | /*++\r | |
1383 | \r | |
1384 | Routine Description:\r | |
1385 | \r | |
1386 | This code Finds the Next available variable\r | |
1387 | \r | |
1388 | Arguments:\r | |
1389 | \r | |
1390 | VariableNameSize Size of the variable\r | |
1391 | VariableName Pointer to variable name\r | |
1392 | VendorGuid Variable Vendor Guid\r | |
1393 | \r | |
1394 | Returns:\r | |
1395 | \r | |
1396 | EFI STATUS\r | |
1397 | \r | |
1398 | --*/\r | |
1399 | {\r | |
1400 | VARIABLE_POINTER_TRACK Variable;\r | |
1401 | UINTN VarNameSize;\r | |
1402 | EFI_STATUS Status;\r | |
1403 | \r | |
1404 | if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r | |
1405 | return EFI_INVALID_PARAMETER;\r | |
1406 | }\r | |
1407 | \r | |
1408 | Status = FindVariable (VariableName, VendorGuid, &Variable);\r | |
1409 | \r | |
1410 | if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r | |
1411 | return Status;\r | |
1412 | }\r | |
1413 | \r | |
1414 | if (VariableName[0] != 0) {\r | |
1415 | //\r | |
1416 | // If variable name is not NULL, get next variable\r | |
1417 | //\r | |
1418 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
1419 | }\r | |
1420 | \r | |
1421 | while (TRUE) {\r | |
1422 | //\r | |
1423 | // The order we find variable is: 1). NonVolatile; 2). Volatile\r | |
1424 | // If both volatile and non-volatile variable store are parsed,\r | |
1425 | // return not found\r | |
1426 | //\r | |
1427 | if (Variable.CurrPtr >= Variable.EndPtr || Variable.CurrPtr == NULL) {\r | |
1428 | if (Variable.Type == Volatile) {\r | |
1429 | //\r | |
1430 | // Since we met the end of Volatile storage, we have parsed all the stores.\r | |
1431 | //\r | |
1432 | return EFI_NOT_FOUND;\r | |
1433 | }\r | |
1434 | \r | |
1435 | //\r | |
1436 | // End of NonVolatile, continue to parse Volatile\r | |
1437 | //\r | |
1438 | Variable.Type = Volatile;\r | |
1439 | Variable.StartPtr = (VARIABLE_HEADER *) ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Volatile] + 1);\r | |
1440 | Variable.EndPtr = (VARIABLE_HEADER *) GetEndPointer ((VARIABLE_STORE_HEADER *) mGlobal->VariableBase[Volatile]);\r | |
1441 | \r | |
1442 | Variable.CurrPtr = Variable.StartPtr;\r | |
1443 | if (!IsValidVariableHeader (Variable.CurrPtr)) {\r | |
1444 | continue;\r | |
1445 | }\r | |
1446 | }\r | |
1447 | //\r | |
1448 | // Variable is found\r | |
1449 | //\r | |
1450 | if (IsValidVariableHeader (Variable.CurrPtr) &&\r | |
1451 | ((Variable.CurrPtr->State == VAR_ADDED) ||\r | |
1452 | (Variable.CurrPtr->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION)))) {\r | |
1453 | if (!EfiAtRuntime () || (Variable.CurrPtr->Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r | |
1454 | VarNameSize = Variable.CurrPtr->NameSize;\r | |
1455 | if (VarNameSize <= *VariableNameSize) {\r | |
1456 | CopyMem (\r | |
1457 | VariableName,\r | |
1458 | GET_VARIABLE_NAME_PTR (Variable.CurrPtr),\r | |
1459 | VarNameSize\r | |
1460 | );\r | |
1461 | CopyMem (\r | |
1462 | VendorGuid,\r | |
1463 | &Variable.CurrPtr->VendorGuid,\r | |
1464 | sizeof (EFI_GUID)\r | |
1465 | );\r | |
1466 | Status = EFI_SUCCESS;\r | |
1467 | } else {\r | |
1468 | Status = EFI_BUFFER_TOO_SMALL;\r | |
1469 | }\r | |
1470 | \r | |
1471 | *VariableNameSize = VarNameSize;\r | |
1472 | return Status;\r | |
1473 | }\r | |
1474 | }\r | |
1475 | \r | |
1476 | Variable.CurrPtr = GetNextVariablePtr (Variable.CurrPtr);\r | |
1477 | }\r | |
9071550e | 1478 | }\r |
1479 | \r | |
1480 | EFI_STATUS\r | |
1481 | EFIAPI\r | |
1482 | SetVariable (\r | |
1483 | IN CHAR16 *VariableName,\r | |
1484 | IN EFI_GUID *VendorGuid,\r | |
1485 | IN UINT32 Attributes,\r | |
1486 | IN UINTN DataSize,\r | |
1487 | IN VOID *Data\r | |
1488 | )\r | |
1489 | /*++\r | |
1490 | \r | |
1491 | Routine Description:\r | |
1492 | \r | |
1493 | This code sets variable in storage blocks (Volatile or Non-Volatile)\r | |
1494 | \r | |
1495 | Arguments:\r | |
1496 | \r | |
1497 | VariableName Name of Variable to be found\r | |
1498 | VendorGuid Variable vendor GUID\r | |
1499 | Attributes Attribute value of the variable found\r | |
1500 | DataSize Size of Data found. If size is less than the\r | |
1501 | data, this value contains the required size.\r | |
1502 | Data Data pointer\r | |
1503 | \r | |
1504 | Returns:\r | |
1505 | \r | |
1506 | EFI_INVALID_PARAMETER - Invalid parameter\r | |
1507 | EFI_SUCCESS - Set successfully\r | |
1508 | EFI_OUT_OF_RESOURCES - Resource not enough to set variable\r | |
1509 | EFI_NOT_FOUND - Not found\r | |
1510 | EFI_DEVICE_ERROR - Variable can not be saved due to hardware failure\r | |
1511 | EFI_WRITE_PROTECTED - Variable is read-only\r | |
1512 | \r | |
1513 | --*/\r | |
1514 | {\r | |
1515 | VARIABLE_POINTER_TRACK Variable;\r | |
1516 | EFI_STATUS Status;\r | |
a24b4043 | 1517 | \r |
9071550e | 1518 | //\r |
1519 | // Check input parameters\r | |
1520 | // \r | |
9071550e | 1521 | if (VariableName == NULL || VariableName[0] == 0 || VendorGuid == NULL) {\r |
1522 | return EFI_INVALID_PARAMETER;\r | |
1523 | }\r | |
1524 | \r | |
81c5255c | 1525 | if (DataSize != 0 && Data == NULL) {\r |
1526 | return EFI_INVALID_PARAMETER;\r | |
1527 | }\r | |
1528 | \r | |
ff197263 | 1529 | //\r |
1530 | // Not support authenticated variable write yet.\r | |
1531 | //\r | |
1532 | if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r | |
1533 | return EFI_INVALID_PARAMETER;\r | |
1534 | }\r | |
1535 | \r | |
9071550e | 1536 | //\r |
1537 | // Make sure if runtime bit is set, boot service bit is set also\r | |
1538 | //\r | |
1539 | if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r | |
1540 | return EFI_INVALID_PARAMETER;\r | |
1541 | }\r | |
1542 | \r | |
9071550e | 1543 | //\r |
1544 | // The size of the VariableName, including the Unicode Null in bytes plus\r | |
d4577dce | 1545 | // the DataSize is limited to maximum size of PcdGet32 (PcdMaxHardwareErrorVariableSize)\r |
1546 | // bytes for HwErrRec, and PcdGet32 (PcdMaxVariableSize) bytes for the others.\r | |
9071550e | 1547 | //\r |
1548 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r | |
d4577dce | 1549 | if ((DataSize > PcdGet32(PcdMaxHardwareErrorVariableSize)) || \r |
1550 | (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32(PcdMaxHardwareErrorVariableSize))) {\r | |
9071550e | 1551 | return EFI_INVALID_PARAMETER;\r |
a24b4043 | 1552 | }\r |
1553 | //\r | |
1554 | // According to UEFI spec, HARDWARE_ERROR_RECORD variable name convention should be L"HwErrRecXXXX"\r | |
1555 | //\r | |
1556 | if (StrnCmp(VariableName, L"HwErrRec", StrLen(L"HwErrRec")) != 0) {\r | |
1557 | return EFI_INVALID_PARAMETER;\r | |
1558 | }\r | |
9071550e | 1559 | } else {\r |
d4577dce | 1560 | if ((DataSize > PcdGet32(PcdMaxVariableSize)) ||\r |
1561 | (sizeof (VARIABLE_HEADER) + StrSize (VariableName) + DataSize > PcdGet32(PcdMaxVariableSize))) {\r | |
9071550e | 1562 | return EFI_INVALID_PARAMETER;\r |
1563 | } \r | |
1564 | } \r | |
e5653d94 | 1565 | \r |
9071550e | 1566 | //\r |
1567 | // Check whether the input variable is already existed\r | |
1568 | //\r | |
9071550e | 1569 | Status = FindVariable (VariableName, VendorGuid, &Variable);\r |
1570 | \r | |
9071550e | 1571 | //\r |
a24b4043 | 1572 | // Hook the operation of setting PlatformLangCodes/PlatformLang and LangCodes/Lang\r |
9071550e | 1573 | //\r |
a24b4043 | 1574 | AutoUpdateLangVariable (VariableName, Data, DataSize);\r |
9071550e | 1575 | \r |
a24b4043 | 1576 | Status = UpdateVariable (VariableName, VendorGuid, Data, DataSize, Attributes, &Variable);\r |
9071550e | 1577 | \r |
a24b4043 | 1578 | return Status;\r |
9071550e | 1579 | }\r |
1580 | \r | |
9071550e | 1581 | EFI_STATUS\r |
1582 | EFIAPI\r | |
1583 | QueryVariableInfo (\r | |
1584 | IN UINT32 Attributes,\r | |
1585 | OUT UINT64 *MaximumVariableStorageSize,\r | |
1586 | OUT UINT64 *RemainingVariableStorageSize,\r | |
1587 | OUT UINT64 *MaximumVariableSize\r | |
1588 | )\r | |
1589 | /*++\r | |
1590 | \r | |
1591 | Routine Description:\r | |
1592 | \r | |
1593 | This code returns information about the EFI variables.\r | |
1594 | \r | |
1595 | Arguments:\r | |
1596 | \r | |
1597 | Attributes Attributes bitmask to specify the type of variables\r | |
1598 | on which to return information.\r | |
1599 | MaximumVariableStorageSize Pointer to the maximum size of the storage space available\r | |
1600 | for the EFI variables associated with the attributes specified.\r | |
1601 | RemainingVariableStorageSize Pointer to the remaining size of the storage space available\r | |
1602 | for the EFI variables associated with the attributes specified.\r | |
1603 | MaximumVariableSize Pointer to the maximum size of the individual EFI variables\r | |
1604 | associated with the attributes specified.\r | |
1605 | \r | |
1606 | Returns:\r | |
1607 | \r | |
1608 | EFI STATUS\r | |
1609 | EFI_INVALID_PARAMETER - An invalid combination of attribute bits was supplied.\r | |
1610 | EFI_SUCCESS - Query successfully.\r | |
1611 | EFI_UNSUPPORTED - The attribute is not supported on this platform.\r | |
1612 | \r | |
1613 | --*/\r | |
1614 | {\r | |
1615 | VARIABLE_HEADER *Variable;\r | |
1616 | VARIABLE_HEADER *NextVariable;\r | |
1617 | UINT64 VariableSize;\r | |
1618 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
a24b4043 | 1619 | UINT64 CommonVariableTotalSize;\r |
1620 | UINT64 HwErrVariableTotalSize;\r | |
1621 | \r | |
1622 | CommonVariableTotalSize = 0;\r | |
1623 | HwErrVariableTotalSize = 0;\r | |
9071550e | 1624 | \r |
1625 | if(MaximumVariableStorageSize == NULL || RemainingVariableStorageSize == NULL || MaximumVariableSize == NULL || Attributes == 0) {\r | |
1626 | return EFI_INVALID_PARAMETER;\r | |
1627 | }\r | |
1628 | \r | |
9071550e | 1629 | if((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == 0) {\r |
1630 | //\r | |
1631 | // Make sure the Attributes combination is supported by the platform.\r | |
1632 | //\r | |
1633 | return EFI_UNSUPPORTED; \r | |
ff197263 | 1634 | } else if ((Attributes & (EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)) == EFI_VARIABLE_RUNTIME_ACCESS) {\r |
9071550e | 1635 | //\r |
1636 | // Make sure if runtime bit is set, boot service bit is set also.\r | |
1637 | //\r | |
1638 | return EFI_INVALID_PARAMETER;\r | |
1639 | } else if (EfiAtRuntime () && !(Attributes & EFI_VARIABLE_RUNTIME_ACCESS)) {\r | |
1640 | //\r | |
1641 | // Make sure RT Attribute is set if we are in Runtime phase.\r | |
1642 | //\r | |
1643 | return EFI_INVALID_PARAMETER;\r | |
a24b4043 | 1644 | } else if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r |
1645 | //\r | |
1646 | // Make sure Hw Attribute is set with NV.\r | |
1647 | //\r | |
1648 | return EFI_INVALID_PARAMETER;\r | |
ff197263 | 1649 | } else if ((Attributes & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {\r |
1650 | //\r | |
1651 | // Not support authentiated variable write yet.\r | |
1652 | //\r | |
1653 | return EFI_UNSUPPORTED;\r | |
a24b4043 | 1654 | }\r |
9071550e | 1655 | \r |
1656 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) mGlobal->VariableBase[\r | |
1657 | (Attributes & EFI_VARIABLE_NON_VOLATILE) ? NonVolatile : Volatile\r | |
1658 | ];\r | |
1659 | //\r | |
1660 | // Now let's fill *MaximumVariableStorageSize *RemainingVariableStorageSize\r | |
1661 | // with the storage size (excluding the storage header size).\r | |
1662 | //\r | |
1663 | *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER);\r | |
9071550e | 1664 | \r |
9071550e | 1665 | //\r |
1666 | // Harware error record variable needs larger size.\r | |
1667 | //\r | |
a24b4043 | 1668 | if ((Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r |
d4577dce | 1669 | *MaximumVariableStorageSize = PcdGet32(PcdHwErrStorageSize);\r |
1670 | *MaximumVariableSize = PcdGet32(PcdMaxHardwareErrorVariableSize) - sizeof (VARIABLE_HEADER);\r | |
a24b4043 | 1671 | } else {\r |
1672 | if ((Attributes & EFI_VARIABLE_NON_VOLATILE) != 0) {\r | |
d4577dce | 1673 | ASSERT (PcdGet32(PcdHwErrStorageSize) < VariableStoreHeader->Size);\r |
1674 | *MaximumVariableStorageSize = VariableStoreHeader->Size - sizeof (VARIABLE_STORE_HEADER) - PcdGet32(PcdHwErrStorageSize);\r | |
a24b4043 | 1675 | }\r |
1676 | \r | |
1677 | //\r | |
d4577dce | 1678 | // Let *MaximumVariableSize be PcdGet32(PcdMaxVariableSize) with the exception of the variable header size.\r |
a24b4043 | 1679 | //\r |
d4577dce | 1680 | *MaximumVariableSize = PcdGet32(PcdMaxVariableSize) - sizeof (VARIABLE_HEADER);\r |
9071550e | 1681 | }\r |
9071550e | 1682 | \r |
1683 | //\r | |
1684 | // Point to the starting address of the variables.\r | |
1685 | //\r | |
1686 | Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r | |
1687 | \r | |
1688 | //\r | |
1689 | // Now walk through the related variable store.\r | |
1690 | //\r | |
a24b4043 | 1691 | while ((Variable < GetEndPointer (VariableStoreHeader)) && IsValidVariableHeader (Variable)) {\r |
9071550e | 1692 | NextVariable = GetNextVariablePtr (Variable);\r |
1693 | VariableSize = (UINT64) (UINTN) NextVariable - (UINT64) (UINTN) Variable;\r | |
1694 | \r | |
1695 | if (EfiAtRuntime ()) {\r | |
1696 | //\r | |
1697 | // we don't take the state of the variables in mind\r | |
1698 | // when calculating RemainingVariableStorageSize,\r | |
1699 | // since the space occupied by variables not marked with\r | |
1700 | // VAR_ADDED is not allowed to be reclaimed in Runtime.\r | |
1701 | //\r | |
452f0207 | 1702 | if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r |
a24b4043 | 1703 | HwErrVariableTotalSize += VariableSize;\r |
1704 | } else {\r | |
1705 | CommonVariableTotalSize += VariableSize;\r | |
1706 | }\r | |
9071550e | 1707 | } else {\r |
1708 | //\r | |
1709 | // Only care about Variables with State VAR_ADDED,because\r | |
1710 | // the space not marked as VAR_ADDED is reclaimable now.\r | |
1711 | //\r | |
a24b4043 | 1712 | if ((Variable->State == VAR_ADDED) || (Variable->State == (VAR_ADDED & VAR_IN_DELETED_TRANSITION))) {\r |
452f0207 | 1713 | if ((Variable->Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD) {\r |
a24b4043 | 1714 | HwErrVariableTotalSize += VariableSize;\r |
1715 | } else {\r | |
1716 | CommonVariableTotalSize += VariableSize;\r | |
1717 | }\r | |
9071550e | 1718 | }\r |
1719 | }\r | |
1720 | \r | |
1721 | //\r | |
1722 | // Go to the next one\r | |
1723 | //\r | |
1724 | Variable = NextVariable;\r | |
1725 | }\r | |
1726 | \r | |
a24b4043 | 1727 | if ((Attributes & EFI_VARIABLE_HARDWARE_ERROR_RECORD) == EFI_VARIABLE_HARDWARE_ERROR_RECORD){\r |
1728 | *RemainingVariableStorageSize = *MaximumVariableStorageSize - HwErrVariableTotalSize;\r | |
1729 | } else {\r | |
1730 | *RemainingVariableStorageSize = *MaximumVariableStorageSize - CommonVariableTotalSize;\r | |
9071550e | 1731 | }\r |
1732 | \r | |
1733 | return EFI_SUCCESS;\r | |
1734 | }\r | |
9071550e | 1735 | \r |
1736 | EFI_STATUS\r | |
1737 | EFIAPI\r | |
1738 | VariableServiceInitialize (\r | |
1739 | IN EFI_HANDLE ImageHandle,\r | |
1740 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
1741 | )\r | |
1742 | /*++\r | |
1743 | \r | |
1744 | Routine Description:\r | |
1745 | This function does initialization for variable services\r | |
1746 | \r | |
1747 | Arguments:\r | |
1748 | \r | |
1749 | ImageHandle - The firmware allocated handle for the EFI image.\r | |
1750 | SystemTable - A pointer to the EFI System Table.\r | |
1751 | \r | |
1752 | Returns:\r | |
1753 | \r | |
1754 | Status code.\r | |
1755 | \r | |
1756 | EFI_NOT_FOUND - Variable store area not found.\r | |
1757 | EFI_SUCCESS - Variable services successfully initialized.\r | |
1758 | \r | |
1759 | --*/\r | |
1760 | {\r | |
1761 | EFI_STATUS Status;\r | |
1762 | EFI_HANDLE NewHandle;\r | |
1763 | VS_DEV *Dev;\r | |
eb16e240 | 1764 | EFI_PEI_HOB_POINTERS GuidHob;\r |
a24b4043 | 1765 | VARIABLE_HEADER *Variable;\r |
9071550e | 1766 | VARIABLE_HEADER *NextVariable;\r |
1767 | VARIABLE_STORE_HEADER *VariableStoreHeader;\r | |
1768 | EFI_FLASH_MAP_FS_ENTRY_DATA *FlashMapEntryData;\r | |
1769 | EFI_FLASH_SUBAREA_ENTRY VariableStoreEntry;\r | |
9071550e | 1770 | UINT64 BaseAddress;\r |
1771 | UINT64 Length;\r | |
1772 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r | |
1773 | \r | |
1774 | Status = gBS->AllocatePool (\r | |
1775 | EfiRuntimeServicesData,\r | |
1776 | (UINTN) sizeof (VARIABLE_GLOBAL),\r | |
7c04a679 | 1777 | (VOID**) &mGlobal\r |
9071550e | 1778 | );\r |
1779 | if (EFI_ERROR (Status)) {\r | |
1780 | return Status;\r | |
1781 | }\r | |
1782 | \r | |
a24b4043 | 1783 | ZeroMem (mGlobal, (UINTN) sizeof (VARIABLE_GLOBAL));\r |
1784 | \r | |
eb16e240 | 1785 | GuidHob.Raw = GetHobList ();\r |
1786 | FlashMapEntryData = NULL;\r | |
1787 | while ((GuidHob.Raw = GetNextGuidHob (&gEfiFlashMapHobGuid, GuidHob.Raw)) != NULL) {\r | |
1788 | FlashMapEntryData = (EFI_FLASH_MAP_FS_ENTRY_DATA *) GET_GUID_HOB_DATA (GuidHob.Guid);\r | |
9071550e | 1789 | if (FlashMapEntryData->AreaType == EFI_FLASH_AREA_EFI_VARIABLES) {\r |
1790 | break;\r | |
1791 | }\r | |
eb16e240 | 1792 | GuidHob.Raw = GET_NEXT_HOB (GuidHob); \r |
9071550e | 1793 | }\r |
1794 | \r | |
eb16e240 | 1795 | if (FlashMapEntryData == NULL) {\r |
1796 | DEBUG ((EFI_D_ERROR, "FSVariable: Could not find flash area for variable!\n"));\r | |
9071550e | 1797 | Status = EFI_NOT_FOUND;\r |
1798 | return Status;\r | |
1799 | }\r | |
be768885 | 1800 | \r |
394bbc59 | 1801 | CopyMem(\r |
1802 | (VOID*)&VariableStoreEntry,\r | |
1803 | (VOID*)&FlashMapEntryData->Entries[0],\r | |
1804 | sizeof(EFI_FLASH_SUBAREA_ENTRY)\r | |
1805 | );\r | |
9071550e | 1806 | \r |
1807 | //\r | |
1808 | // Mark the variable storage region of the FLASH as RUNTIME\r | |
1809 | //\r | |
1810 | BaseAddress = VariableStoreEntry.Base & (~EFI_PAGE_MASK);\r | |
1811 | Length = VariableStoreEntry.Length + (VariableStoreEntry.Base - BaseAddress);\r | |
1812 | Length = (Length + EFI_PAGE_SIZE - 1) & (~EFI_PAGE_MASK);\r | |
1813 | Status = gDS->GetMemorySpaceDescriptor (BaseAddress, &GcdDescriptor);\r | |
1814 | if (EFI_ERROR (Status)) {\r | |
1815 | Status = EFI_UNSUPPORTED;\r | |
1816 | return Status;\r | |
1817 | }\r | |
1818 | Status = gDS->SetMemorySpaceAttributes (\r | |
1819 | BaseAddress,\r | |
1820 | Length,\r | |
1821 | GcdDescriptor.Attributes | EFI_MEMORY_RUNTIME\r | |
1822 | );\r | |
1823 | if (EFI_ERROR (Status)) {\r | |
1824 | Status = EFI_UNSUPPORTED;\r | |
1825 | return Status;\r | |
1826 | }\r | |
7c04a679 | 1827 | \r |
9071550e | 1828 | Status = FileStorageConstructor (\r |
1829 | &mGlobal->VariableStore[NonVolatile], \r | |
1830 | &mGlobal->GoVirtualChildEvent[NonVolatile],\r | |
1831 | VariableStoreEntry.Base,\r | |
1832 | (UINT32) VariableStoreEntry.Length,\r | |
1833 | FlashMapEntryData->VolumeId,\r | |
1834 | FlashMapEntryData->FilePath\r | |
1835 | );\r | |
1836 | ASSERT_EFI_ERROR (Status);\r | |
1837 | \r | |
1838 | //\r | |
1839 | // Volatile Storage\r | |
1840 | //\r | |
1841 | Status = MemStorageConstructor (\r | |
1842 | &mGlobal->VariableStore[Volatile],\r | |
1843 | &mGlobal->GoVirtualChildEvent[Volatile],\r | |
1844 | VOLATILE_VARIABLE_STORE_SIZE\r | |
1845 | );\r | |
1846 | ASSERT_EFI_ERROR (Status);\r | |
1847 | \r | |
1848 | //\r | |
1849 | // Scratch\r | |
1850 | //\r | |
1851 | Status = gBS->AllocatePool (\r | |
1852 | EfiRuntimeServicesData,\r | |
1853 | VARIABLE_SCRATCH_SIZE,\r | |
1854 | &mGlobal->Scratch\r | |
1855 | );\r | |
1856 | ASSERT_EFI_ERROR (Status);\r | |
1857 | \r | |
1858 | //\r | |
1859 | // 1. NV Storage\r | |
1860 | //\r | |
1861 | Dev = DEV_FROM_THIS (mGlobal->VariableStore[NonVolatile]);\r | |
1862 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) VAR_DATA_PTR (Dev);\r | |
1863 | if (GetVariableStoreStatus (VariableStoreHeader) == EfiValid) {\r | |
1864 | if (~VariableStoreHeader->Size == 0) {\r | |
1865 | VariableStoreHeader->Size = (UINT32) VariableStoreEntry.Length;\r | |
1866 | }\r | |
1867 | }\r | |
1868 | //\r | |
1869 | // Calculate LastVariableOffset\r | |
1870 | //\r | |
a24b4043 | 1871 | Variable = (VARIABLE_HEADER *) (VariableStoreHeader + 1);\r |
1872 | while (IsValidVariableHeader (Variable)) {\r | |
1873 | UINTN VariableSize = 0;\r | |
1874 | NextVariable = GetNextVariablePtr (Variable);\r | |
1875 | VariableSize = NextVariable - Variable;\r | |
1876 | if ((NextVariable->Attributes & (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) == (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_HARDWARE_ERROR_RECORD)) {\r | |
1877 | mGlobal->HwErrVariableTotalSize += HEADER_ALIGN (VariableSize);\r | |
1878 | } else {\r | |
1879 | mGlobal->CommonVariableTotalSize += HEADER_ALIGN (VariableSize);\r | |
1880 | }\r | |
1881 | Variable = NextVariable;\r | |
9071550e | 1882 | }\r |
a24b4043 | 1883 | \r |
1884 | mGlobal->LastVariableOffset[NonVolatile] = (UINTN) Variable - (UINTN) VariableStoreHeader;\r | |
1885 | mGlobal->VariableBase[NonVolatile] = VariableStoreHeader;\r | |
9071550e | 1886 | \r |
1887 | //\r | |
1888 | // Reclaim if remaining space is too small\r | |
1889 | //\r | |
1890 | if ((VariableStoreHeader->Size - mGlobal->LastVariableOffset[NonVolatile]) < VARIABLE_RECLAIM_THRESHOLD) {\r | |
1891 | Status = Reclaim (NonVolatile, NULL);\r | |
1892 | if (EFI_ERROR (Status)) {\r | |
1893 | //\r | |
1894 | // Reclaim error\r | |
1895 | // we cannot restore to original state\r | |
1896 | //\r | |
a24b4043 | 1897 | DEBUG ((EFI_D_ERROR, "FSVariable: Reclaim error (fatal error) - %r\n", Status));\r |
9071550e | 1898 | ASSERT_EFI_ERROR (Status);\r |
1899 | }\r | |
1900 | }\r | |
1901 | \r | |
1902 | //\r | |
1903 | // 2. Volatile Storage\r | |
1904 | //\r | |
1905 | Dev = DEV_FROM_THIS (mGlobal->VariableStore[Volatile]);\r | |
1906 | VariableStoreHeader = (VARIABLE_STORE_HEADER *) VAR_DATA_PTR (Dev);\r | |
1907 | mGlobal->VariableBase[Volatile] = VAR_DATA_PTR (Dev); \r | |
1908 | mGlobal->LastVariableOffset[Volatile] = sizeof (VARIABLE_STORE_HEADER);\r | |
1909 | //\r | |
1910 | // init store_header & body in memory.\r | |
1911 | //\r | |
1912 | mGlobal->VariableStore[Volatile]->Erase (mGlobal->VariableStore[Volatile]);\r | |
1913 | mGlobal->VariableStore[Volatile]->Write (\r | |
1914 | mGlobal->VariableStore[Volatile],\r | |
1915 | 0,\r | |
1916 | sizeof (VARIABLE_STORE_HEADER),\r | |
1917 | &mStoreHeaderTemplate\r | |
1918 | );\r | |
1919 | \r | |
1920 | \r | |
3ffa0f1f | 1921 | SystemTable->RuntimeServices->GetVariable = DuetGetVariable;\r |
9071550e | 1922 | SystemTable->RuntimeServices->GetNextVariableName = GetNextVariableName;\r |
1923 | SystemTable->RuntimeServices->SetVariable = SetVariable;\r | |
1924 | \r | |
9071550e | 1925 | SystemTable->RuntimeServices->QueryVariableInfo = QueryVariableInfo;\r |
9071550e | 1926 | \r |
1927 | //\r | |
1928 | // Now install the Variable Runtime Architectural Protocol on a new handle\r | |
1929 | //\r | |
1930 | NewHandle = NULL;\r | |
1931 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
1932 | &NewHandle,\r | |
1933 | &gEfiVariableArchProtocolGuid,\r | |
1934 | NULL,\r | |
1935 | &gEfiVariableWriteArchProtocolGuid,\r | |
1936 | NULL,\r | |
1937 | NULL\r | |
1938 | );\r | |
1939 | ASSERT_EFI_ERROR (Status);\r | |
1940 | \r | |
1941 | return Status;\r | |
9071550e | 1942 | }\r |
1943 | \r | |
1944 | \r | |
1945 | \r | |
9071550e | 1946 | VOID\r |
1947 | EFIAPI\r | |
e56dd2ce | 1948 | OnVirtualAddressChangeFsv (\r |
9071550e | 1949 | IN EFI_EVENT Event,\r |
1950 | IN VOID *Context\r | |
1951 | )\r | |
1952 | {\r | |
1953 | UINTN Index;\r | |
1954 | \r | |
1955 | for (Index = 0; Index < MaxType; Index++) {\r | |
1956 | mGlobal->GoVirtualChildEvent[Index] (Event, mGlobal->VariableStore[Index]);\r | |
7c04a679 | 1957 | EfiConvertPointer (0, (VOID**) &mGlobal->VariableStore[Index]);\r |
9071550e | 1958 | EfiConvertPointer (0, &mGlobal->VariableBase[Index]);\r |
1959 | }\r | |
d7f79118 RN |
1960 | EfiConvertPointer (0, (VOID **) &mGlobal->PlatformLangCodes);\r |
1961 | EfiConvertPointer (0, (VOID **) &mGlobal->LangCodes);\r | |
1962 | EfiConvertPointer (0, (VOID **) &mGlobal->PlatformLang);\r | |
9071550e | 1963 | EfiConvertPointer (0, &mGlobal->Scratch);\r |
7c04a679 | 1964 | EfiConvertPointer (0, (VOID**) &mGlobal);\r |
9071550e | 1965 | }\r |