]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkPkg/Library/FrameworkIfrSupportLib/IfrCommon.c
Patch to remove STATIC modifier. This is on longer recommended by EFI Framework codin...
[mirror_edk2.git] / IntelFrameworkPkg / Library / FrameworkIfrSupportLib / IfrCommon.c
CommitLineData
cf7e50f8 1/** @file\r
65c2940a 2 Common Library Routines to assist in IFR creation on-the-fly\r
3 \r
5d01b0f7 4Copyright (c) 2006, Intel Corporation\r
5All rights reserved. This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php \r
9 \r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
cf7e50f8 13**/\r
5d01b0f7 14\r
82096802 15#include "IfrSupportLibInternal.h"\r
5d01b0f7 16\r
2ec4d269 17/**\r
18 Determine what is the current language setting\r
19 The setting is stored in language variable in flash. This routine\r
20 will get setting by accesssing that variable. If failed to access\r
21 language variable, then use default setting that 'eng' as current\r
22 language setting.\r
23 \r
24 @param Lang Pointer of system language\r
25 \r
26 @return whether sucess to get setting from variable\r
27**/\r
5d01b0f7 28EFI_STATUS\r
29GetCurrentLanguage (\r
30 OUT CHAR16 *Lang\r
31 )\r
5d01b0f7 32{\r
33 EFI_STATUS Status;\r
34 UINTN Size;\r
35 UINTN Index;\r
36 CHAR8 Language[4];\r
37\r
38 //\r
39 // Getting the system language and placing it into our Global Data\r
40 //\r
41 Size = sizeof (Language);\r
42\r
43 Status = gRT->GetVariable (\r
44 (CHAR16 *) L"Lang",\r
45 &gEfiGlobalVariableGuid,\r
46 NULL,\r
47 &Size,\r
48 Language\r
49 );\r
50\r
51 if (EFI_ERROR (Status)) {\r
52 AsciiStrCpy (Language, "eng");\r
53 }\r
54\r
55 for (Index = 0; Index < 3; Index++) {\r
56 //\r
57 // Bitwise AND ascii value with 0xDF yields an uppercase value.\r
58 // Sign extend into a unicode value\r
59 //\r
60 Lang[Index] = (CHAR16) (Language[Index] & 0xDF);\r
61 }\r
62\r
63 //\r
64 // Null-terminate the value\r
65 //\r
66 Lang[3] = (CHAR16) 0;\r
67\r
68 return Status;\r
69}\r
70\r
2ec4d269 71/**\r
72 Add a string to the incoming buffer and return the token and offset data\r
73 \r
74 @param StringBuffer The incoming buffer\r
75 @param Language Currrent language\r
76 @param String The string to be added\r
77 @param StringToken The index where the string placed \r
78 \r
79 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate\r
80 @retval EFI_SUCCESS String successfully added to the incoming buffer\r
81**/\r
5d01b0f7 82EFI_STATUS\r
83AddString (\r
84 IN VOID *StringBuffer,\r
85 IN CHAR16 *Language,\r
86 IN CHAR16 *String,\r
87 IN OUT STRING_REF *StringToken\r
88 )\r
5d01b0f7 89{\r
90 EFI_HII_STRING_PACK *StringPack;\r
91 EFI_HII_STRING_PACK *StringPackBuffer;\r
92 VOID *NewBuffer;\r
93 RELOFST *PackSource;\r
94 RELOFST *PackDestination;\r
95 UINT8 *Source;\r
96 UINT8 *Destination;\r
97 UINTN Index;\r
98 BOOLEAN Finished;\r
99 UINTN SizeofLanguage;\r
100 UINTN SizeofString;\r
101\r
102 StringPack = (EFI_HII_STRING_PACK *) StringBuffer;\r
103 Finished = FALSE;\r
104\r
105 //\r
106 // Pre-allocate a buffer sufficient for us to work on.\r
107 // We will use it as a destination scratch pad to build data on\r
108 // and when complete shift the data back to the original buffer\r
109 //\r
110 NewBuffer = AllocateZeroPool (DEFAULT_STRING_BUFFER_SIZE);\r
111 if (NewBuffer == NULL) {\r
112 return EFI_OUT_OF_RESOURCES;\r
113 }\r
114\r
115 StringPackBuffer = (EFI_HII_STRING_PACK *) NewBuffer;\r
116\r
117 //\r
118 // StringPack is terminated with a length 0 entry\r
119 //\r
120 for (; StringPack->Header.Length != 0;) {\r
121 //\r
122 // If this stringpack's language is same as CurrentLanguage, use it\r
123 //\r
124 if (CompareMem ((VOID *) ((CHAR8 *) (StringPack) + StringPack->LanguageNameString), Language, 3) == 0) {\r
125 //\r
126 // We have some data in this string pack, copy the string package up to the string data\r
127 //\r
128 CopyMem (&StringPackBuffer->Header, &StringPack->Header, sizeof (StringPack));\r
129\r
130 //\r
131 // These are references in the structure to tokens, need to increase them by the space occupied by an additional StringPointer\r
132 //\r
133 StringPackBuffer->LanguageNameString = (UINT16) (StringPackBuffer->LanguageNameString + (UINT16) sizeof (RELOFST));\r
134 StringPackBuffer->PrintableLanguageName = (UINT16) (StringPackBuffer->PrintableLanguageName + (UINT16) sizeof (RELOFST));\r
135\r
136 PackSource = (RELOFST *) (StringPack + 1);\r
137 PackDestination = (RELOFST *) (StringPackBuffer + 1);\r
138 for (Index = 0; PackSource[Index] != 0x0000; Index++) {\r
139 //\r
140 // Copy the stringpointers from old to new buffer\r
141 // remember that we are adding a string, so the string offsets will all go up by sizeof (RELOFST)\r
142 //\r
143 PackDestination[Index] = (UINT16) (PackDestination[Index] + sizeof (RELOFST));\r
144 }\r
145\r
146 //\r
147 // Add a new stringpointer in the new buffer since we are adding a string. Null terminate it\r
148 //\r
149 PackDestination[Index] = (UINT16)(PackDestination[Index-1] +\r
150 StrSize((CHAR16 *)((CHAR8 *)(StringPack) + PackSource[Index-1])));\r
151 PackDestination[Index + 1] = (UINT16) 0;\r
152\r
153 //\r
154 // Index is the token value for the new string\r
155 //\r
156 *StringToken = (UINT16) Index;\r
157\r
158 //\r
159 // Source now points to the beginning of the old buffer strings\r
160 // Destination now points to the beginning of the new buffer strings\r
161 //\r
162 Source = (UINT8 *) &PackSource[Index + 1];\r
163 Destination = (UINT8 *) &PackDestination[Index + 2];\r
164\r
165 //\r
166 // This should copy all the strings from the old buffer to the new buffer\r
167 //\r
168 for (; Index != 0; Index--) {\r
169 //\r
170 // Copy Source string to destination buffer\r
171 //\r
172 StrCpy ((CHAR16 *) Destination, (CHAR16 *) Source);\r
173\r
174 //\r
175 // Adjust the source/destination to the next string location\r
176 //\r
177 Destination = Destination + StrSize ((CHAR16 *) Source);\r
178 Source = Source + StrSize ((CHAR16 *) Source);\r
179 }\r
180\r
181 //\r
182 // This copies the new string to the destination buffer\r
183 //\r
184 StrCpy ((CHAR16 *) Destination, (CHAR16 *) String);\r
185\r
186 //\r
187 // Adjust the size of the changed string pack by adding the size of the new string\r
188 // along with the size of the additional offset entry for the new string\r
189 //\r
190 StringPackBuffer->Header.Length = (UINT32) ((UINTN) StringPackBuffer->Header.Length + StrSize (String) + sizeof (RELOFST));\r
191\r
192 //\r
193 // Advance the buffers to point to the next spots.\r
194 //\r
195 StringPackBuffer = (EFI_HII_STRING_PACK *) ((CHAR8 *) (StringPackBuffer) + StringPackBuffer->Header.Length);\r
196 StringPack = (EFI_HII_STRING_PACK *) ((CHAR8 *) (StringPack) + StringPack->Header.Length);\r
197 Finished = TRUE;\r
198 continue;\r
199 }\r
200 //\r
201 // This isn't the language of the stringpack we were asked to add a string to\r
202 // so we need to copy it to the new buffer.\r
203 //\r
204 CopyMem (&StringPackBuffer->Header, &StringPack->Header, StringPack->Header.Length);\r
205\r
206 //\r
207 // Advance the buffers to point to the next spots.\r
208 //\r
209 StringPackBuffer = (EFI_HII_STRING_PACK *) ((CHAR8 *) (StringPackBuffer) + StringPack->Header.Length);\r
210 StringPack = (EFI_HII_STRING_PACK *) ((CHAR8 *) (StringPack) + StringPack->Header.Length);\r
211 }\r
212\r
213 //\r
214 // If we didn't copy the new data to a stringpack yet\r
215 //\r
216 if (!Finished) {\r
217 PackDestination = (RELOFST *) (StringPackBuffer + 1);\r
218 //\r
219 // Pointing to a new string pack location\r
220 //\r
221 SizeofLanguage = StrSize (Language);\r
222 SizeofString = StrSize (String);\r
223 StringPackBuffer->Header.Length = (UINT32)\r
224 (\r
225 sizeof (EFI_HII_STRING_PACK) -\r
226 sizeof (EFI_STRING) +\r
227 sizeof (RELOFST) +\r
228 sizeof (RELOFST) +\r
229 SizeofLanguage +\r
230 SizeofString\r
231 );\r
232 StringPackBuffer->Header.Type = EFI_HII_STRING;\r
233 StringPackBuffer->LanguageNameString = (UINT16) ((UINTN) &PackDestination[3] - (UINTN) StringPackBuffer);\r
234 StringPackBuffer->PrintableLanguageName = (UINT16) ((UINTN) &PackDestination[3] - (UINTN) StringPackBuffer);\r
235 StringPackBuffer->Attributes = 0;\r
236 PackDestination[0] = (UINT16) ((UINTN) &PackDestination[3] - (UINTN) StringPackBuffer);\r
237 PackDestination[1] = (UINT16) (PackDestination[0] + StrSize (Language));\r
238 PackDestination[2] = (UINT16) 0;\r
239\r
240 //\r
241 // The first string location will be set to destination. The minimum number of strings\r
242 // associated with a stringpack will always be token 0 stored as the languagename (e.g. ENG, SPA, etc)\r
243 // and token 1 as the new string being added and and null entry for the stringpointers\r
244 //\r
245 Destination = (UINT8 *) &PackDestination[3];\r
246\r
247 //\r
248 // Copy the language name string to the new buffer\r
249 //\r
250 StrCpy ((CHAR16 *) Destination, Language);\r
251\r
252 //\r
253 // Advance the destination to the new empty spot\r
254 //\r
255 Destination = Destination + StrSize (Language);\r
256\r
257 //\r
258 // Copy the string to the new buffer\r
259 //\r
260 StrCpy ((CHAR16 *) Destination, String);\r
261\r
262 //\r
263 // Since we are starting with a new string pack - we know the new string is token 1\r
264 //\r
265 *StringToken = (UINT16) 1;\r
266 }\r
267\r
268 //\r
269 // Zero out the original buffer and copy the updated data in the new buffer to the old buffer\r
270 //\r
271 ZeroMem (StringBuffer, DEFAULT_STRING_BUFFER_SIZE);\r
272 CopyMem (StringBuffer, NewBuffer, DEFAULT_STRING_BUFFER_SIZE);\r
273\r
274 //\r
275 // Free the newly created buffer since we don't need it anymore\r
276 //\r
277 gBS->FreePool (NewBuffer);\r
278 return EFI_SUCCESS;\r
279}\r
280\r
2ec4d269 281/**\r
282 Add op-code data to the FormBuffer\r
283 \r
284 @param FormBuffer Form buffer to be inserted to\r
285 @param OpCodeData Op-code data to be inserted \r
286 \r
287 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate\r
288 @retval EFI_SUCCESS Op-code data successfully inserted \r
289**/\r
5d01b0f7 290EFI_STATUS\r
291AddOpCode (\r
292 IN VOID *FormBuffer,\r
293 IN OUT VOID *OpCodeData\r
294 )\r
5d01b0f7 295{\r
296 EFI_HII_PACK_HEADER *NewBuffer;\r
297 UINT8 *Source;\r
298 UINT8 *Destination;\r
299\r
300 //\r
301 // Pre-allocate a buffer sufficient for us to work on.\r
302 // We will use it as a destination scratch pad to build data on\r
303 // and when complete shift the data back to the original buffer\r
304 //\r
305 NewBuffer = AllocateZeroPool (DEFAULT_FORM_BUFFER_SIZE);\r
306 if (NewBuffer == NULL) {\r
307 return EFI_OUT_OF_RESOURCES;\r
308 }\r
309\r
310 Source = (UINT8 *) FormBuffer;\r
311 Destination = (UINT8 *) NewBuffer;\r
312\r
313 //\r
314 // Copy the IFR Package header to the new buffer\r
315 //\r
316 CopyMem (Destination, Source, sizeof (EFI_HII_PACK_HEADER));\r
317\r
318 //\r
319 // Advance Source and Destination to next op-code\r
320 //\r
321 Source = Source + sizeof (EFI_HII_PACK_HEADER);\r
322 Destination = Destination + sizeof (EFI_HII_PACK_HEADER);\r
323\r
324 //\r
325 // Copy data to the new buffer until we run into the end_form\r
326 //\r
dc7b4a5c 327 for (; ((FRAMEWORK_EFI_IFR_OP_HEADER *) Source)->OpCode != FRAMEWORK_EFI_IFR_END_FORM_OP;) {\r
5d01b0f7 328 //\r
329 // If the this opcode is an end_form_set we better be creating and endform\r
330 // Nonetheless, we will add data before the end_form_set. This also provides\r
331 // for interesting behavior in the code we will run, but has no bad side-effects\r
332 // since we will possibly do a 0 byte copy in this particular end-case.\r
333 //\r
dc7b4a5c 334 if (((FRAMEWORK_EFI_IFR_OP_HEADER *) Source)->OpCode == FRAMEWORK_EFI_IFR_END_FORM_SET_OP) {\r
5d01b0f7 335 break;\r
336 }\r
337\r
338 //\r
339 // Copy data to new buffer\r
340 //\r
dc7b4a5c 341 CopyMem (Destination, Source, ((FRAMEWORK_EFI_IFR_OP_HEADER *) Source)->Length);\r
5d01b0f7 342\r
343 //\r
344 // Adjust Source/Destination to next op-code location\r
345 //\r
dc7b4a5c 346 Destination = Destination + (UINTN) ((FRAMEWORK_EFI_IFR_OP_HEADER *) Source)->Length;\r
347 Source = Source + (UINTN) ((FRAMEWORK_EFI_IFR_OP_HEADER *) Source)->Length;\r
5d01b0f7 348 }\r
349\r
350 //\r
351 // Prior to the end_form is where we insert the new op-code data\r
352 //\r
dc7b4a5c 353 CopyMem (Destination, OpCodeData, ((FRAMEWORK_EFI_IFR_OP_HEADER *) OpCodeData)->Length);\r
354 Destination = Destination + (UINTN) ((FRAMEWORK_EFI_IFR_OP_HEADER *) OpCodeData)->Length;\r
5d01b0f7 355\r
dc7b4a5c 356 NewBuffer->Length = (UINT32) (NewBuffer->Length + (UINT32) (((FRAMEWORK_EFI_IFR_OP_HEADER *) OpCodeData)->Length));\r
5d01b0f7 357\r
358 //\r
359 // Copy end-form data to new buffer\r
360 //\r
dc7b4a5c 361 CopyMem (Destination, Source, ((FRAMEWORK_EFI_IFR_OP_HEADER *) Source)->Length);\r
5d01b0f7 362\r
363 //\r
364 // Adjust Source/Destination to next op-code location\r
365 //\r
dc7b4a5c 366 Destination = Destination + (UINTN) ((FRAMEWORK_EFI_IFR_OP_HEADER *) Source)->Length;\r
367 Source = Source + (UINTN) ((FRAMEWORK_EFI_IFR_OP_HEADER *) Source)->Length;\r
5d01b0f7 368\r
369 //\r
370 // Copy end-formset data to new buffer\r
371 //\r
dc7b4a5c 372 CopyMem (Destination, Source, ((FRAMEWORK_EFI_IFR_OP_HEADER *) Source)->Length);\r
5d01b0f7 373\r
374 //\r
375 // Zero out the original buffer and copy the updated data in the new buffer to the old buffer\r
376 //\r
377 ZeroMem (FormBuffer, DEFAULT_FORM_BUFFER_SIZE);\r
378 CopyMem (FormBuffer, NewBuffer, DEFAULT_FORM_BUFFER_SIZE);\r
379\r
380 //\r
381 // Free the newly created buffer since we don't need it anymore\r
382 //\r
383 gBS->FreePool (NewBuffer);\r
384 return EFI_SUCCESS;\r
385}\r
386\r
2ec4d269 387/**\r
388 Get the HII protocol interface\r
389 \r
390 @param Hii HII protocol interface\r
391 \r
392 @return the statue of locating HII protocol\r
393**/\r
5d01b0f7 394EFI_STATUS\r
395GetHiiInterface (\r
396 OUT EFI_HII_PROTOCOL **Hii\r
397 )\r
5d01b0f7 398{\r
399 EFI_STATUS Status;\r
400\r
401 //\r
402 // There should only be one HII protocol\r
403 //\r
404 Status = gBS->LocateProtocol (\r
405 &gEfiHiiProtocolGuid,\r
406 NULL,\r
407 (VOID **) Hii\r
408 );\r
409\r
410 return Status;;\r
411}\r
412\r
2ec4d269 413/**\r
414 Extract information pertaining to the HiiHandle\r
415 \r
416 @param HiiHandle Hii handle\r
417 @param ImageLength For input, length of DefaultImage;\r
418 For output, length of actually required\r
419 @param DefaultImage Image buffer prepared by caller\r
420 @param Guid Guid information about the form \r
421 \r
422 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate\r
423 @retval EFI_BUFFER_TOO_SMALL DefualtImage has no enough ImageLength\r
424 @retval EFI_SUCCESS Successfully extract data from Hii database.\r
425**/\r
5d01b0f7 426EFI_STATUS\r
427ExtractDataFromHiiHandle (\r
dc7b4a5c 428 IN FRAMEWORK_EFI_HII_HANDLE HiiHandle,\r
5d01b0f7 429 IN OUT UINT16 *ImageLength,\r
430 OUT UINT8 *DefaultImage,\r
431 OUT EFI_GUID *Guid\r
432 )\r
5d01b0f7 433{\r
434 EFI_STATUS Status;\r
435 EFI_HII_PROTOCOL *Hii;\r
436 UINTN DataLength;\r
437 UINT8 *RawData;\r
438 UINT8 *OldData;\r
439 UINTN Index;\r
440 UINTN Temp;\r
441 UINTN SizeOfNvStore;\r
442 UINTN CachedStart;\r
443\r
444 DataLength = DEFAULT_FORM_BUFFER_SIZE;\r
445 SizeOfNvStore = 0;\r
446 CachedStart = 0;\r
447\r
448 Status = GetHiiInterface (&Hii);\r
449\r
450 if (EFI_ERROR (Status)) {\r
451 return Status;\r
452 }\r
453\r
454 //\r
455 // Allocate space for retrieval of IFR data\r
456 //\r
457 RawData = AllocateZeroPool (DataLength);\r
458 if (RawData == NULL) {\r
459 return EFI_OUT_OF_RESOURCES;\r
460 }\r
461\r
462 //\r
463 // Get all the forms associated with this HiiHandle\r
464 //\r
465 Status = Hii->GetForms (Hii, HiiHandle, 0, &DataLength, RawData);\r
466\r
467 if (EFI_ERROR (Status)) {\r
468 gBS->FreePool (RawData);\r
469\r
470 //\r
471 // Allocate space for retrieval of IFR data\r
472 //\r
473 RawData = AllocateZeroPool (DataLength);\r
474 if (RawData == NULL) {\r
475 return EFI_OUT_OF_RESOURCES;\r
476 }\r
477\r
478 //\r
479 // Get all the forms associated with this HiiHandle\r
480 //\r
481 Status = Hii->GetForms (Hii, HiiHandle, 0, &DataLength, RawData);\r
482 }\r
483\r
484 OldData = RawData;\r
485\r
486 //\r
487 // Point RawData to the beginning of the form data\r
488 //\r
489 RawData = (UINT8 *) ((UINTN) RawData + sizeof (EFI_HII_PACK_HEADER));\r
490\r
dc7b4a5c 491 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
5d01b0f7 492 switch (RawData[Index]) {\r
dc7b4a5c 493 case FRAMEWORK_EFI_IFR_FORM_SET_OP:\r
5d01b0f7 494 //\r
495 // Copy the GUID information from this handle\r
496 //\r
dc7b4a5c 497 CopyMem (Guid, &((FRAMEWORK_EFI_IFR_FORM_SET *) &RawData[Index])->Guid, sizeof (EFI_GUID));\r
5d01b0f7 498 break;\r
499\r
dc7b4a5c 500 case FRAMEWORK_EFI_IFR_ONE_OF_OP:\r
501 case FRAMEWORK_EFI_IFR_CHECKBOX_OP:\r
502 case FRAMEWORK_EFI_IFR_NUMERIC_OP:\r
503 case FRAMEWORK_EFI_IFR_DATE_OP:\r
504 case FRAMEWORK_EFI_IFR_TIME_OP:\r
505 case FRAMEWORK_EFI_IFR_PASSWORD_OP:\r
506 case FRAMEWORK_EFI_IFR_STRING_OP:\r
5d01b0f7 507 //\r
508 // Remember, multiple op-codes may reference the same item, so let's keep a running\r
509 // marker of what the highest QuestionId that wasn't zero length. This will accurately\r
510 // maintain the Size of the NvStore\r
511 //\r
dc7b4a5c 512 if (((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width != 0) {\r
513 Temp = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId + ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width;\r
5d01b0f7 514 if (SizeOfNvStore < Temp) {\r
dc7b4a5c 515 SizeOfNvStore = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId + ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width;\r
5d01b0f7 516 }\r
517 }\r
518 }\r
519\r
520 Index = RawData[Index + 1] + Index;\r
521 }\r
522\r
523 //\r
524 // Return an error if buffer is too small\r
525 //\r
526 if (SizeOfNvStore > *ImageLength) {\r
527 gBS->FreePool (OldData);\r
528 *ImageLength = (UINT16) SizeOfNvStore;\r
529 return EFI_BUFFER_TOO_SMALL;\r
530 }\r
531\r
532 if (DefaultImage != NULL) {\r
533 ZeroMem (DefaultImage, SizeOfNvStore);\r
534 }\r
535\r
536 //\r
537 // Copy the default image information to the user's buffer\r
538 //\r
dc7b4a5c 539 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
5d01b0f7 540 switch (RawData[Index]) {\r
dc7b4a5c 541 case FRAMEWORK_EFI_IFR_ONE_OF_OP:\r
542 CachedStart = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId;\r
5d01b0f7 543 break;\r
544\r
dc7b4a5c 545 case FRAMEWORK_EFI_IFR_ONE_OF_OPTION_OP:\r
546 if (((FRAMEWORK_EFI_IFR_ONE_OF_OPTION *) &RawData[Index])->Flags & FRAMEWORK_EFI_IFR_FLAG_DEFAULT) {\r
547 CopyMem (&DefaultImage[CachedStart], &((FRAMEWORK_EFI_IFR_ONE_OF_OPTION *) &RawData[Index])->Value, 2);\r
5d01b0f7 548 }\r
549 break;\r
550\r
dc7b4a5c 551 case FRAMEWORK_EFI_IFR_CHECKBOX_OP:\r
552 DefaultImage[((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId] = ((FRAMEWORK_EFI_IFR_CHECKBOX *) &RawData[Index])->Flags;\r
5d01b0f7 553 break;\r
554\r
dc7b4a5c 555 case FRAMEWORK_EFI_IFR_NUMERIC_OP:\r
5d01b0f7 556 CopyMem (\r
dc7b4a5c 557 &DefaultImage[((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId],\r
558 &((FRAMEWORK_EFI_IFR_NUMERIC *) &RawData[Index])->Default,\r
5d01b0f7 559 2\r
560 );\r
561 break;\r
562\r
563 }\r
564\r
565 Index = RawData[Index + 1] + Index;\r
566 }\r
567\r
568 *ImageLength = (UINT16) SizeOfNvStore;\r
569\r
570 //\r
571 // Free our temporary repository of form data\r
572 //\r
573 gBS->FreePool (OldData);\r
574\r
575 return EFI_SUCCESS;\r
576}\r
577\r
2ec4d269 578/**\r
579 Finds HII handle for given pack GUID previously registered with the HII.\r
580 \r
581 @param HiiProtocol pointer to pointer to HII protocol interface.\r
582 If NULL, the interface will be found but not returned.\r
583 If it points to NULL, the interface will be found and\r
584 written back to the pointer that is pointed to.\r
585 @param Guid The GUID of the pack that registered with the HII.\r
5d01b0f7 586\r
2ec4d269 587 @return Handle to the HII pack previously registered by the memory driver.\r
588**/\r
dc7b4a5c 589FRAMEWORK_EFI_HII_HANDLE \r
5d01b0f7 590FindHiiHandle (\r
591 IN OUT EFI_HII_PROTOCOL **HiiProtocol, OPTIONAL\r
592 IN EFI_GUID *Guid\r
593 )\r
5d01b0f7 594{\r
595 EFI_STATUS Status;\r
596\r
dc7b4a5c 597 FRAMEWORK_EFI_HII_HANDLE *HiiHandleBuffer;\r
598 FRAMEWORK_EFI_HII_HANDLE HiiHandle;\r
5d01b0f7 599 UINT16 HiiHandleBufferLength;\r
600 UINT32 NumberOfHiiHandles;\r
601 EFI_GUID HiiGuid;\r
602 EFI_HII_PROTOCOL *HiiProt;\r
603 UINT32 Index;\r
604 UINT16 Length;\r
605\r
606 HiiHandle = 0;\r
607 if ((HiiProtocol != NULL) && (*HiiProtocol != NULL)) {\r
608 //\r
609 // The protocol has been passed in\r
610 //\r
611 HiiProt = *HiiProtocol;\r
612 } else {\r
613 gBS->LocateProtocol (\r
614 &gEfiHiiProtocolGuid,\r
615 NULL,\r
616 (VOID **) &HiiProt\r
617 );\r
618 if (HiiProt == NULL) {\r
619 return HiiHandle;\r
620 }\r
621\r
622 if (HiiProtocol != NULL) {\r
623 //\r
624 // Return back the HII protocol for the caller as promissed\r
625 //\r
626 *HiiProtocol = HiiProt;\r
627 }\r
628 }\r
629 //\r
630 // Allocate buffer\r
631 //\r
632 HiiHandleBufferLength = 10;\r
633 HiiHandleBuffer = AllocatePool (HiiHandleBufferLength);\r
634 ASSERT (HiiHandleBuffer != NULL);\r
635\r
636 //\r
637 // Get the Handles of the packages that were registered with Hii\r
638 //\r
639 Status = HiiProt->FindHandles (\r
640 HiiProt,\r
641 &HiiHandleBufferLength,\r
642 HiiHandleBuffer\r
643 );\r
644\r
645 //\r
646 // Get a bigger bugffer if this one is to small, and try again\r
647 //\r
648 if (Status == EFI_BUFFER_TOO_SMALL) {\r
649\r
650 gBS->FreePool (HiiHandleBuffer);\r
651\r
652 HiiHandleBuffer = AllocatePool (HiiHandleBufferLength);\r
653 ASSERT (HiiHandleBuffer != NULL);\r
654\r
655 Status = HiiProt->FindHandles (\r
656 HiiProt,\r
657 &HiiHandleBufferLength,\r
658 HiiHandleBuffer\r
659 );\r
660 }\r
661\r
662 if (EFI_ERROR (Status)) {\r
663 goto lbl_exit;\r
664 }\r
665\r
dc7b4a5c 666 NumberOfHiiHandles = HiiHandleBufferLength / sizeof (FRAMEWORK_EFI_HII_HANDLE );\r
5d01b0f7 667\r
668 //\r
669 // Iterate Hii handles and look for the one that matches our Guid\r
670 //\r
671 for (Index = 0; Index < NumberOfHiiHandles; Index++) {\r
672\r
673 Length = 0;\r
674 ExtractDataFromHiiHandle (HiiHandleBuffer[Index], &Length, NULL, &HiiGuid);\r
675\r
676 if (CompareGuid (&HiiGuid, Guid)) {\r
677\r
678 HiiHandle = HiiHandleBuffer[Index];\r
679 break;\r
680 }\r
681 }\r
682\r
683lbl_exit:\r
684 gBS->FreePool (HiiHandleBuffer);\r
685 return HiiHandle;\r
686}\r
687\r
2ec4d269 688/**\r
689 Validate that the data associated with the HiiHandle in NVRAM is within\r
690 the reasonable parameters for that FormSet. Values for strings and passwords\r
691 are not verified due to their not having the equivalent of valid range settings.\r
692\r
693 @param HiiHandle Handle of the HII database entry to query\r
5d01b0f7 694\r
2ec4d269 695 @param Results If return Status is EFI_SUCCESS, Results provides valid data\r
696 TRUE = NVRAM Data is within parameters\r
697 FALSE = NVRAM Data is NOT within parameters\r
698 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate\r
699 @retval EFI_SUCCESS Data successfully validated\r
700**/\r
5d01b0f7 701EFI_STATUS\r
702ValidateDataFromHiiHandle (\r
dc7b4a5c 703 IN FRAMEWORK_EFI_HII_HANDLE HiiHandle,\r
5d01b0f7 704 OUT BOOLEAN *Results\r
705 )\r
5d01b0f7 706{\r
707 EFI_STATUS Status;\r
708 EFI_HII_PROTOCOL *Hii;\r
709 EFI_GUID Guid;\r
710 UINT8 *RawData;\r
711 UINT8 *OldData;\r
712 UINTN RawDataLength;\r
713 UINT8 *VariableData;\r
714 UINTN Index;\r
715 UINTN Temp;\r
716 UINTN SizeOfNvStore;\r
717 UINTN CachedStart;\r
718 BOOLEAN GotMatch;\r
719\r
720 RawDataLength = DEFAULT_FORM_BUFFER_SIZE;\r
721 SizeOfNvStore = 0;\r
722 CachedStart = 0;\r
723 GotMatch = FALSE;\r
724 *Results = TRUE;\r
725\r
726 Status = GetHiiInterface (&Hii);\r
727\r
728 if (EFI_ERROR (Status)) {\r
729 return Status;\r
730 }\r
731\r
732 //\r
733 // Allocate space for retrieval of IFR data\r
734 //\r
735 RawData = AllocateZeroPool (RawDataLength);\r
736 if (RawData == NULL) {\r
737 return EFI_OUT_OF_RESOURCES;\r
738 }\r
739\r
740 //\r
741 // Get all the forms associated with this HiiHandle\r
742 //\r
743 Status = Hii->GetForms (Hii, HiiHandle, 0, &RawDataLength, RawData);\r
744\r
745 if (EFI_ERROR (Status)) {\r
746 gBS->FreePool (RawData);\r
747\r
748 //\r
749 // Allocate space for retrieval of IFR data\r
750 //\r
751 RawData = AllocateZeroPool (RawDataLength);\r
752 if (RawData == NULL) {\r
753 return EFI_OUT_OF_RESOURCES;\r
754 }\r
755\r
756 //\r
757 // Get all the forms associated with this HiiHandle\r
758 //\r
759 Status = Hii->GetForms (Hii, HiiHandle, 0, &RawDataLength, RawData);\r
760 }\r
761\r
762 OldData = RawData;\r
763\r
764 //\r
765 // Point RawData to the beginning of the form data\r
766 //\r
767 RawData = (UINT8 *) ((UINTN) RawData + sizeof (EFI_HII_PACK_HEADER));\r
768\r
dc7b4a5c 769 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
770 if (RawData[Index] == FRAMEWORK_EFI_IFR_FORM_SET_OP) {\r
771 CopyMem (&Guid, &((FRAMEWORK_EFI_IFR_FORM_SET *) &RawData[Index])->Guid, sizeof (EFI_GUID));\r
5d01b0f7 772 break;\r
773 }\r
774\r
775 Index = RawData[Index + 1] + Index;\r
776 }\r
777\r
dc7b4a5c 778 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
5d01b0f7 779 switch (RawData[Index]) {\r
dc7b4a5c 780 case FRAMEWORK_EFI_IFR_FORM_SET_OP:\r
5d01b0f7 781 break;\r
782\r
dc7b4a5c 783 case FRAMEWORK_EFI_IFR_ONE_OF_OP:\r
784 case FRAMEWORK_EFI_IFR_CHECKBOX_OP:\r
785 case FRAMEWORK_EFI_IFR_NUMERIC_OP:\r
786 case FRAMEWORK_EFI_IFR_DATE_OP:\r
787 case FRAMEWORK_EFI_IFR_TIME_OP:\r
788 case FRAMEWORK_EFI_IFR_PASSWORD_OP:\r
789 case FRAMEWORK_EFI_IFR_STRING_OP:\r
5d01b0f7 790 //\r
791 // Remember, multiple op-codes may reference the same item, so let's keep a running\r
792 // marker of what the highest QuestionId that wasn't zero length. This will accurately\r
793 // maintain the Size of the NvStore\r
794 //\r
dc7b4a5c 795 if (((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width != 0) {\r
796 Temp = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId + ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width;\r
5d01b0f7 797 if (SizeOfNvStore < Temp) {\r
dc7b4a5c 798 SizeOfNvStore = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId + ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width;\r
5d01b0f7 799 }\r
800 }\r
801 }\r
802\r
803 Index = RawData[Index + 1] + Index;\r
804 }\r
805\r
806 //\r
807 // Allocate memory for our File Form Tags\r
808 //\r
809 VariableData = AllocateZeroPool (SizeOfNvStore);\r
810 if (VariableData == NULL) {\r
811 return EFI_OUT_OF_RESOURCES;\r
812 }\r
813\r
814 Status = gRT->GetVariable (\r
815 (CHAR16 *) L"Setup",\r
816 &Guid,\r
817 NULL,\r
818 &SizeOfNvStore,\r
819 (VOID *) VariableData\r
820 );\r
821\r
822 if (EFI_ERROR (Status)) {\r
823\r
824 //\r
825 // If there is a variable that exists already and it is larger than what we calculated the\r
826 // storage needs to be, we must assume the variable size from GetVariable is correct and not\r
827 // allow the truncation of the variable. It is very possible that the user who created the IFR\r
828 // we are cracking is not referring to a variable that was in a previous map, however we cannot\r
829 // allow it's truncation.\r
830 //\r
831 if (Status == EFI_BUFFER_TOO_SMALL) {\r
832 //\r
833 // Free the buffer that was allocated that was too small\r
834 //\r
835 gBS->FreePool (VariableData);\r
836\r
837 VariableData = AllocatePool (SizeOfNvStore);\r
838 if (VariableData == NULL) {\r
839 return EFI_OUT_OF_RESOURCES;\r
840 }\r
841\r
842 Status = gRT->GetVariable (\r
843 (CHAR16 *) L"Setup",\r
844 &Guid,\r
845 NULL,\r
846 &SizeOfNvStore,\r
847 (VOID *) VariableData\r
848 );\r
849 }\r
850 }\r
851\r
852 //\r
853 // Walk through the form and see that the variable data it refers to is ok.\r
854 // This allows for the possibility of stale (obsoleted) data in the variable\r
855 // can be overlooked without causing an error\r
856 //\r
dc7b4a5c 857 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
5d01b0f7 858 switch (RawData[Index]) {\r
dc7b4a5c 859 case FRAMEWORK_EFI_IFR_ONE_OF_OP:\r
5d01b0f7 860 //\r
861 // A one_of has no data, its the option that does - cache the storage Id\r
862 //\r
dc7b4a5c 863 CachedStart = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId;\r
5d01b0f7 864 break;\r
865\r
dc7b4a5c 866 case FRAMEWORK_EFI_IFR_ONE_OF_OPTION_OP:\r
5d01b0f7 867 //\r
868 // A one_of_option can be any value\r
869 //\r
dc7b4a5c 870 if (VariableData[CachedStart] == ((FRAMEWORK_EFI_IFR_ONE_OF_OPTION *) &RawData[Index])->Value) {\r
5d01b0f7 871 GotMatch = TRUE;\r
872 }\r
873 break;\r
874\r
dc7b4a5c 875 case FRAMEWORK_EFI_IFR_END_ONE_OF_OP:\r
5d01b0f7 876 //\r
877 // At this point lets make sure that the data value in the NVRAM matches one of the options\r
878 //\r
879 if (!GotMatch) {\r
880 *Results = FALSE;\r
881 return EFI_SUCCESS;\r
882 }\r
883 break;\r
884\r
dc7b4a5c 885 case FRAMEWORK_EFI_IFR_CHECKBOX_OP:\r
5d01b0f7 886 //\r
887 // A checkbox is a boolean, so 0 and 1 are valid\r
888 // Remember, QuestionId corresponds to the offset location of the data in the variable\r
889 //\r
dc7b4a5c 890 if (VariableData[((FRAMEWORK_EFI_IFR_CHECKBOX *) &RawData[Index])->QuestionId] > 1) {\r
5d01b0f7 891 *Results = FALSE;\r
892 return EFI_SUCCESS;\r
893 }\r
894 break;\r
895\r
dc7b4a5c 896 case FRAMEWORK_EFI_IFR_NUMERIC_OP:\r
897 if ((VariableData[((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->QuestionId] < ((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->Minimum) ||\r
898 (VariableData[((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->QuestionId] > ((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->Maximum)) {\r
5d01b0f7 899 *Results = FALSE;\r
900 return EFI_SUCCESS;\r
901 }\r
902 break;\r
903\r
904 }\r
905\r
906 Index = RawData[Index + 1] + Index;\r
907 }\r
908\r
909 //\r
910 // Free our temporary repository of form data\r
911 //\r
912 gBS->FreePool (OldData);\r
913 gBS->FreePool (VariableData);\r
914\r
915 return EFI_SUCCESS;\r
916}\r
917\r
dc7b4a5c 918\r