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