5d01b0f7 |
1 | /*++\r |
2 | \r |
3 | Copyright (c) 2006, Intel Corporation \r |
4 | All rights reserved. This program and the accompanying materials \r |
5 | are licensed and made available under the terms and conditions of the BSD License \r |
6 | which accompanies this distribution. The full text of the license may be found at \r |
7 | http://opensource.org/licenses/bsd-license.php \r |
8 | \r |
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r |
11 | \r |
12 | Module Name:\r |
13 | IfrVariable.c\r |
14 | \r |
15 | Abstract:\r |
16 | Variable/Map manipulations routines\r |
17 | \r |
18 | --*/\r |
19 | \r |
20 | //\r |
21 | // Include common header file for this module.\r |
22 | //\r |
23 | #include "CommonHeader.h"\r |
24 | \r |
25 | VOID\r |
26 | EfiLibHiiVariablePackGetMap (\r |
27 | IN EFI_HII_VARIABLE_PACK *Pack, \r |
28 | OUT CHAR16 **Name, OPTIONAL\r |
29 | OUT EFI_GUID **Guid, OPTIONAL\r |
30 | OUT UINT16 *Id, OPTIONAL\r |
31 | OUT VOID **Var, OPTIONAL\r |
32 | OUT UINTN *Size OPTIONAL\r |
33 | )\r |
34 | /*++\r |
35 | \r |
36 | Routine Description:\r |
37 | \r |
38 | Extracts a variable form a Pack.\r |
39 | \r |
40 | Arguments:\r |
41 | \r |
42 | Pack - List of variables\r |
43 | Name - Name of the variable/map\r |
44 | Guid - GUID of the variable/map\r |
45 | Var - Pointer to the variable/map\r |
46 | Size - Size of the variable/map in bytes\r |
47 | \r |
48 | Returns: \r |
49 | \r |
50 | VOID\r |
51 | \r |
52 | --*/\r |
53 | {\r |
54 | if (NULL != Name) {\r |
55 | *Name = (VOID *) (Pack + 1);\r |
56 | }\r |
57 | \r |
58 | if (NULL != Guid) { \r |
59 | *Guid = (EFI_GUID *)(UINTN)&Pack->VariableGuid;\r |
60 | }\r |
61 | \r |
62 | \r |
63 | if (NULL != Id) {\r |
64 | *Id = Pack->VariableId;\r |
65 | }\r |
66 | \r |
67 | if (NULL != Var) {\r |
68 | *Var = (VOID *) ((CHAR8 *) (Pack + 1) + Pack->VariableNameLength);\r |
69 | }\r |
70 | \r |
71 | if (NULL != Size) {\r |
72 | *Size = Pack->Header.Length - sizeof (*Pack) - Pack->VariableNameLength;\r |
73 | }\r |
74 | }\r |
75 | \r |
76 | \r |
77 | UINTN\r |
78 | EfiLibHiiVariablePackListGetMapCnt (\r |
79 | IN EFI_HII_VARIABLE_PACK_LIST *List\r |
80 | )\r |
81 | \r |
82 | /*++\r |
83 | \r |
84 | Routine Description:\r |
85 | \r |
86 | Finds a count of the variables/maps in the List.\r |
87 | \r |
88 | Arguments:\r |
89 | \r |
90 | List - List of variables\r |
91 | \r |
92 | Returns: \r |
93 | \r |
94 | UINTN - The number of map count.\r |
95 | \r |
96 | --*/\r |
97 | \r |
98 | {\r |
99 | UINTN Cnt = 0;\r |
100 | while (NULL != List) {\r |
101 | Cnt++;\r |
102 | List = List->NextVariablePack;\r |
103 | }\r |
104 | return Cnt;\r |
105 | }\r |
106 | \r |
107 | \r |
108 | VOID\r |
109 | EfiLibHiiVariablePackListForEachVar (\r |
110 | IN EFI_HII_VARIABLE_PACK_LIST *List,\r |
111 | IN EFI_LIB_HII_VARIABLE_PACK_LIST_CALLBACK *Callback\r |
112 | )\r |
113 | /*++\r |
114 | \r |
115 | Routine Description:\r |
116 | \r |
117 | Will iterate all variable/maps as appearing \r |
118 | in List and for each, it will call the Callback.\r |
119 | \r |
120 | Arguments:\r |
121 | \r |
122 | List - List of variables\r |
123 | Callback - Routine to be called for each iterated variable.\r |
124 | \r |
125 | Returns: \r |
126 | \r |
127 | VOID\r |
128 | \r |
129 | --*/\r |
130 | \r |
131 | {\r |
132 | CHAR16 *MapName;\r |
133 | EFI_GUID *MapGuid;\r |
134 | UINT16 MapId;\r |
135 | VOID *Map;\r |
136 | UINTN MapSize;\r |
137 | \r |
138 | while (NULL != List) {\r |
139 | EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r |
140 | //\r |
141 | // call the callback\r |
142 | //\r |
143 | Callback (MapName, MapGuid, MapId, Map, MapSize); \r |
144 | List = List->NextVariablePack;\r |
145 | }\r |
146 | }\r |
147 | \r |
148 | \r |
149 | EFI_STATUS\r |
150 | EfiLibHiiVariablePackListGetMapByIdx (\r |
151 | IN UINTN Idx, \r |
152 | IN EFI_HII_VARIABLE_PACK_LIST *List, \r |
153 | OUT CHAR16 **Name, OPTIONAL\r |
154 | OUT EFI_GUID **Guid, OPTIONAL\r |
155 | OUT UINT16 *Id, OPTIONAL\r |
156 | OUT VOID **Var,\r |
157 | OUT UINTN *Size\r |
158 | )\r |
159 | \r |
160 | /*++\r |
161 | \r |
162 | Routine Description:\r |
163 | \r |
164 | Finds a variable form List given \r |
165 | the order number as appears in the List.\r |
166 | \r |
167 | Arguments:\r |
168 | \r |
169 | Idx - The index of the variable/map to retrieve\r |
170 | List - List of variables\r |
171 | Name - Name of the variable/map\r |
172 | Guid - GUID of the variable/map\r |
173 | Var - Pointer to the variable/map\r |
174 | Size - Size of the variable/map in bytes\r |
175 | \r |
176 | Returns:\r |
177 | \r |
178 | EFI_SUCCESS - Variable is found, OUT parameters are valid\r |
179 | EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid\r |
180 | \r |
181 | --*/\r |
182 | {\r |
183 | CHAR16 *MapName;\r |
184 | EFI_GUID *MapGuid;\r |
185 | UINT16 MapId;\r |
186 | VOID *Map;\r |
187 | UINTN MapSize;\r |
188 | \r |
189 | while (NULL != List) {\r |
190 | EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r |
191 | if (0 == Idx--) {\r |
192 | *Var = Map;\r |
193 | *Size = MapSize;\r |
194 | \r |
195 | if (NULL != Name) {\r |
196 | *Name = MapName;\r |
197 | }\r |
198 | \r |
199 | if (NULL != Guid) {\r |
200 | *Guid = MapGuid;\r |
201 | }\r |
202 | \r |
203 | if (NULL != Id) {\r |
204 | *Id = MapId;\r |
205 | }\r |
206 | \r |
207 | return EFI_SUCCESS; // Map found\r |
208 | }\r |
209 | List = List->NextVariablePack;\r |
210 | }\r |
211 | //\r |
212 | // If here, the map is not found\r |
213 | //\r |
214 | return EFI_NOT_FOUND; \r |
215 | }\r |
216 | \r |
217 | \r |
218 | EFI_STATUS\r |
219 | EfiLibHiiVariablePackListGetMapById (\r |
220 | IN UINT16 Id, \r |
221 | IN EFI_HII_VARIABLE_PACK_LIST *List,\r |
222 | OUT CHAR16 **Name, OPTIONAL\r |
223 | OUT EFI_GUID **Guid, OPTIONAL\r |
224 | OUT VOID **Var,\r |
225 | OUT UINTN *Size\r |
226 | )\r |
227 | \r |
228 | /*++\r |
229 | \r |
230 | Routine Description:\r |
231 | \r |
232 | Finds a variable form List given the \r |
233 | order number as appears in the List.\r |
234 | \r |
235 | Arguments:\r |
236 | \r |
237 | Id - The ID of the variable/map to retrieve\r |
238 | List - List of variables\r |
239 | Name - Name of the variable/map\r |
240 | Guid - GUID of the variable/map\r |
241 | Var - Pointer to the variable/map\r |
242 | Size - Size of the variable/map in bytes\r |
243 | \r |
244 | Returns:\r |
245 | \r |
246 | EFI_SUCCESS - Variable is found, OUT parameters are valid\r |
247 | EFI_NOT_FOUND - Variable is not found, OUT parameters are not valid\r |
248 | \r |
249 | --*/\r |
250 | \r |
251 | { \r |
252 | CHAR16 *MapName;\r |
253 | EFI_GUID *MapGuid;\r |
254 | UINT16 MapId;\r |
255 | VOID *Map;\r |
256 | UINTN MapSize;\r |
257 | \r |
258 | while (NULL != List) {\r |
259 | EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r |
260 | if (MapId == Id) {\r |
261 | *Var = Map;\r |
262 | *Size = MapSize;\r |
263 | if (NULL != Name) {\r |
264 | *Name = MapName;\r |
265 | }\r |
266 | if (NULL != Guid) {\r |
267 | *Guid = MapGuid;\r |
268 | }\r |
269 | //\r |
270 | // Map found\r |
271 | //\r |
272 | return EFI_SUCCESS; \r |
273 | }\r |
274 | List = List->NextVariablePack;\r |
275 | }\r |
276 | //\r |
277 | // If here, the map is not found\r |
278 | //\r |
279 | return EFI_NOT_FOUND; \r |
280 | }\r |
281 | \r |
282 | \r |
283 | EFI_STATUS\r |
284 | EfiLibHiiVariablePackListGetMap (\r |
285 | IN EFI_HII_VARIABLE_PACK_LIST *List,\r |
286 | IN CHAR16 *Name,\r |
287 | IN EFI_GUID *Guid,\r |
288 | OUT UINT16 *Id,\r |
289 | OUT VOID **Var, \r |
290 | OUT UINTN *Size\r |
291 | )\r |
292 | \r |
293 | /*++\r |
294 | \r |
295 | Routine Description:\r |
296 | \r |
297 | Finds a variable form EFI_HII_VARIABLE_PACK_LIST given name and GUID.\r |
298 | \r |
299 | Arguments:\r |
300 | \r |
301 | List - List of variables\r |
302 | Name - Name of the variable/map to be found\r |
303 | Guid - GUID of the variable/map to be found\r |
304 | Var - Pointer to the variable/map found\r |
305 | Size - Size of the variable/map in bytes found\r |
306 | \r |
307 | Returns:\r |
308 | \r |
309 | EFI_SUCCESS - variable is found, OUT parameters are valid\r |
310 | EFI_NOT_FOUND - variable is not found, OUT parameters are not valid\r |
311 | \r |
312 | --*/\r |
313 | \r |
314 | { \r |
315 | VOID *Map;\r |
316 | UINTN MapSize;\r |
317 | UINT16 MapId;\r |
318 | CHAR16 *MapName;\r |
319 | EFI_GUID *MapGuid;\r |
320 | \r |
321 | while (NULL != List) {\r |
322 | EfiLibHiiVariablePackGetMap (List->VariablePack, &MapName, &MapGuid, &MapId, &Map, &MapSize);\r |
323 | if ((0 == StrCmp (Name, MapName)) && CompareGuid (Guid, MapGuid)) {\r |
324 | *Id = MapId;\r |
325 | *Var = Map;\r |
326 | *Size = MapSize;\r |
327 | return EFI_SUCCESS;\r |
328 | }\r |
329 | List = List->NextVariablePack;\r |
330 | }\r |
331 | //\r |
332 | // If here, the map is not found\r |
333 | //\r |
334 | return EFI_NOT_FOUND;\r |
335 | }\r |
336 | \r |
337 | EFI_STATUS\r |
338 | EfiLibHiiVariableRetrieveFromNv (\r |
339 | IN CHAR16 *Name,\r |
340 | IN EFI_GUID *Guid,\r |
341 | IN UINTN Size,\r |
342 | OUT VOID **Var\r |
343 | )\r |
344 | /*++\r |
345 | \r |
346 | Routine Description:\r |
347 | Finds out if a variable of specific Name/Guid/Size exists in NV. \r |
348 | If it does, it will retrieve it into the Var. \r |
349 | \r |
350 | Arguments:\r |
351 | Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.\r |
352 | Var - Variable will be retrieved into buffer pointed by this pointer.\r |
353 | If pointing to NULL, the buffer will be allocated. Caller is responsible for releasing the buffer.\r |
354 | Returns:\r |
355 | EFI_SUCCESS - The variable of exact Name/Guid/Size parameters was retrieved and written to Var.\r |
356 | EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.\r |
357 | EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.\r |
358 | \r |
359 | --*/\r |
360 | {\r |
361 | EFI_STATUS Status;\r |
362 | UINTN SizeNv;\r |
363 | \r |
364 | //\r |
365 | // Test for existence of the variable.\r |
366 | //\r |
367 | SizeNv = 0;\r |
368 | Status = gRT->GetVariable (Name, Guid, NULL, &SizeNv, NULL);\r |
369 | if (EFI_BUFFER_TOO_SMALL != Status) {\r |
370 | ASSERT (EFI_SUCCESS != Status);\r |
371 | return EFI_NOT_FOUND;\r |
372 | }\r |
373 | if (SizeNv != Size) {\r |
374 | //\r |
375 | // The variable is considered corrupt, as it has different size from expected.\r |
376 | //\r |
377 | return EFI_LOAD_ERROR; \r |
378 | }\r |
379 | \r |
380 | if (NULL == *Var) {\r |
381 | *Var = AllocatePool (Size);\r |
382 | ASSERT (NULL != *Var);\r |
383 | }\r |
384 | SizeNv = Size;\r |
385 | //\r |
386 | // Final read into the Var\r |
387 | //\r |
388 | Status = gRT->GetVariable (Name, Guid, NULL, &SizeNv, *Var); \r |
389 | //\r |
390 | // No tolerance for random failures. Such behavior is undetermined and not validated.\r |
391 | //\r |
392 | ASSERT_EFI_ERROR (Status); \r |
393 | ASSERT (SizeNv == Size);\r |
394 | return EFI_SUCCESS;\r |
395 | }\r |
396 | \r |
397 | \r |
398 | \r |
399 | EFI_STATUS\r |
400 | EfiLibHiiVariableOverrideIfSuffix (\r |
401 | IN CHAR16 *Suffix,\r |
402 | IN CHAR16 *Name,\r |
403 | IN EFI_GUID *Guid,\r |
404 | IN UINTN Size,\r |
405 | OUT VOID *Var\r |
406 | ) \r |
407 | /*++\r |
408 | \r |
409 | Routine Description:\r |
410 | Overrrides the variable with NV data if found.\r |
411 | But it only does it if the Name ends with specified Suffix.\r |
412 | For example, if Suffix="MyOverride" and the Name="XyzSetupMyOverride",\r |
413 | the Suffix matches the end of Name, so the variable will be loaded from NV\r |
414 | provided the variable exists and the GUID and Size matches.\r |
415 | \r |
416 | Arguments:\r |
417 | Suffix - Suffix the Name should end with.\r |
418 | Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.\r |
419 | Var - Variable will be retrieved into this buffer.\r |
420 | Caller is responsible for providing storage of exactly Size size in bytes.\r |
421 | Returns:\r |
422 | EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.\r |
423 | EFI_INVALID_PARAMETER - The name of the variable does not end with <Suffix>.\r |
424 | EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.\r |
425 | EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.\r |
426 | \r |
427 | --*/\r |
428 | {\r |
429 | UINTN StrLength;\r |
430 | UINTN StrLenSuffix;\r |
431 | \r |
432 | StrLength = StrLen (Name);\r |
433 | StrLenSuffix = StrLen (Suffix);\r |
434 | if ((StrLength <= StrLenSuffix) || (0 != StrCmp (Suffix, &Name[StrLength - StrLenSuffix]))) {\r |
435 | //\r |
436 | // Not ending with <Suffix>.\r |
437 | //\r |
438 | return EFI_INVALID_PARAMETER; \r |
439 | }\r |
440 | return EfiLibHiiVariableRetrieveFromNv (Name, Guid, Size, &Var);\r |
441 | }\r |
442 | \r |
443 | EFI_STATUS\r |
444 | EfiLibHiiVariableOverrideBySuffix (\r |
445 | IN CHAR16 *Suffix,\r |
446 | IN CHAR16 *Name,\r |
447 | IN EFI_GUID *Guid,\r |
448 | IN UINTN Size,\r |
449 | OUT VOID *Var\r |
450 | ) \r |
451 | /*++\r |
452 | \r |
453 | Routine Description:\r |
454 | Overrrides the variable with NV data if found.\r |
455 | But it only does it if the NV contains the same variable with Name is appended with Suffix. \r |
456 | For example, if Suffix="MyOverride" and the Name="XyzSetup",\r |
457 | the Suffix will be appended to the end of Name, and the variable with Name="XyzSetupMyOverride"\r |
458 | will be loaded from NV provided the variable exists and the GUID and Size matches.\r |
459 | \r |
460 | Arguments:\r |
461 | Suffix - Suffix the variable will be appended with.\r |
462 | Name, Guid, Size - Parameters of the variable to retrieve. Must match exactly.\r |
463 | Var - Variable will be retrieved into this buffer.\r |
464 | Caller is responsible for providing storage of exactly Size size in bytes.\r |
465 | \r |
466 | Returns:\r |
467 | EFI_SUCCESS - The variable was overriden with NV variable of same Name/Guid/Size.\r |
468 | EFI_NOT_FOUND - The variable of this Name/Guid was not found in the NV.\r |
469 | EFI_LOAD_ERROR - The variable in the NV was of different size, or NV API returned error.\r |
470 | \r |
471 | --*/\r |
472 | {\r |
473 | EFI_STATUS Status;\r |
474 | CHAR16 *NameSuffixed;\r |
475 | UINTN NameLength;\r |
476 | UINTN SuffixLength;\r |
477 | \r |
478 | //\r |
479 | // enough to concatenate both strings.\r |
480 | //\r |
481 | NameLength = StrLen (Name);\r |
482 | SuffixLength = StrLen (Suffix);\r |
483 | NameSuffixed = AllocateZeroPool ((NameLength + SuffixLength + 1) * sizeof (CHAR16)); \r |
484 | \r |
485 | StrCpy (NameSuffixed, Name);\r |
486 | StrCat (NameSuffixed, Suffix);\r |
487 | \r |
488 | Status = EfiLibHiiVariableRetrieveFromNv (NameSuffixed, Guid, Size, &Var);\r |
489 | gBS->FreePool (NameSuffixed);\r |
490 | \r |
491 | return Status;\r |
492 | }\r |
493 | \r |