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