]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkPkg/Library/FrameworkIfrSupportLib/IfrCommon.c
correct some coding style issues.
[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
49a049e6 24 @param Lang Pointer of system language\r
2ec4d269 25 \r
49a049e6 26 @return whether sucess to get setting from variable\r
2ec4d269 27**/\r
5d01b0f7 28EFI_STATUS\r
29GetCurrentLanguage (\r
49a049e6 30 OUT CHAR16 *Lang\r
5d01b0f7 31 )\r
5d01b0f7 32{\r
49a049e6 33 EFI_STATUS Status;\r
34 UINTN Size;\r
35 UINTN Index;\r
36 CHAR8 Language[4];\r
5d01b0f7 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
49a049e6 72 Add a string to the incoming buffer and return the token and offset data.\r
2ec4d269 73 \r
49a049e6 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
2ec4d269 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
49a049e6 84 IN VOID *StringBuffer,\r
85 IN CHAR16 *Language,\r
86 IN CHAR16 *String,\r
87 IN OUT STRING_REF *StringToken\r
5d01b0f7 88 )\r
5d01b0f7 89{\r
49a049e6 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
5d01b0f7 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
49a049e6 282 Add op-code data to the FormBuffer.\r
2ec4d269 283 \r
49a049e6 284 @param FormBuffer Form buffer to be inserted to\r
285 @param OpCodeData Op-code data to be inserted \r
2ec4d269 286 \r
49a049e6 287 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate\r
288 @retval EFI_SUCCESS Op-code data successfully inserted \r
2ec4d269 289**/\r
5d01b0f7 290EFI_STATUS\r
291AddOpCode (\r
49a049e6 292 IN VOID *FormBuffer,\r
293 IN OUT VOID *OpCodeData\r
5d01b0f7 294 )\r
5d01b0f7 295{\r
49a049e6 296 EFI_HII_PACK_HEADER *NewBuffer;\r
297 UINT8 *Source;\r
298 UINT8 *Destination;\r
5d01b0f7 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
49a049e6 388 Get the HII protocol interface.\r
2ec4d269 389 \r
49a049e6 390 @param Hii HII protocol interface\r
2ec4d269 391 \r
49a049e6 392 @return the statue of locating HII protocol\r
2ec4d269 393**/\r
5d01b0f7 394EFI_STATUS\r
395GetHiiInterface (\r
49a049e6 396 OUT EFI_HII_PROTOCOL **Hii\r
5d01b0f7 397 )\r
5d01b0f7 398{\r
49a049e6 399 EFI_STATUS Status;\r
5d01b0f7 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
49a049e6 414 Extract information pertaining to the HiiHandle.\r
2ec4d269 415 \r
49a049e6 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
2ec4d269 421 \r
49a049e6 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
2ec4d269 425**/\r
5d01b0f7 426EFI_STATUS\r
427ExtractDataFromHiiHandle (\r
49a049e6 428 IN FRAMEWORK_EFI_HII_HANDLE HiiHandle,\r
429 IN OUT UINT16 *ImageLength,\r
430 OUT UINT8 *DefaultImage,\r
431 OUT EFI_GUID *Guid\r
5d01b0f7 432 )\r
5d01b0f7 433{\r
49a049e6 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
5d01b0f7 443\r
444 DataLength = DEFAULT_FORM_BUFFER_SIZE;\r
445 SizeOfNvStore = 0;\r
446 CachedStart = 0;\r
447\r
448 Status = GetHiiInterface (&Hii);\r
5d01b0f7 449 if (EFI_ERROR (Status)) {\r
450 return Status;\r
451 }\r
452\r
453 //\r
454 // Allocate space for retrieval of IFR data\r
455 //\r
456 RawData = AllocateZeroPool (DataLength);\r
457 if (RawData == NULL) {\r
458 return EFI_OUT_OF_RESOURCES;\r
459 }\r
460\r
461 //\r
462 // Get all the forms associated with this HiiHandle\r
463 //\r
464 Status = Hii->GetForms (Hii, HiiHandle, 0, &DataLength, RawData);\r
5d01b0f7 465 if (EFI_ERROR (Status)) {\r
466 gBS->FreePool (RawData);\r
467\r
468 //\r
469 // Allocate space for retrieval of IFR data\r
470 //\r
471 RawData = AllocateZeroPool (DataLength);\r
472 if (RawData == NULL) {\r
473 return EFI_OUT_OF_RESOURCES;\r
474 }\r
475\r
476 //\r
477 // Get all the forms associated with this HiiHandle\r
478 //\r
479 Status = Hii->GetForms (Hii, HiiHandle, 0, &DataLength, RawData);\r
480 }\r
481\r
482 OldData = RawData;\r
483\r
484 //\r
485 // Point RawData to the beginning of the form data\r
486 //\r
487 RawData = (UINT8 *) ((UINTN) RawData + sizeof (EFI_HII_PACK_HEADER));\r
488\r
dc7b4a5c 489 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
5d01b0f7 490 switch (RawData[Index]) {\r
49a049e6 491 \r
dc7b4a5c 492 case FRAMEWORK_EFI_IFR_FORM_SET_OP:\r
5d01b0f7 493 //\r
494 // Copy the GUID information from this handle\r
495 //\r
dc7b4a5c 496 CopyMem (Guid, &((FRAMEWORK_EFI_IFR_FORM_SET *) &RawData[Index])->Guid, sizeof (EFI_GUID));\r
5d01b0f7 497 break;\r
498\r
dc7b4a5c 499 case FRAMEWORK_EFI_IFR_ONE_OF_OP:\r
500 case FRAMEWORK_EFI_IFR_CHECKBOX_OP:\r
501 case FRAMEWORK_EFI_IFR_NUMERIC_OP:\r
502 case FRAMEWORK_EFI_IFR_DATE_OP:\r
503 case FRAMEWORK_EFI_IFR_TIME_OP:\r
504 case FRAMEWORK_EFI_IFR_PASSWORD_OP:\r
505 case FRAMEWORK_EFI_IFR_STRING_OP:\r
5d01b0f7 506 //\r
507 // Remember, multiple op-codes may reference the same item, so let's keep a running\r
508 // marker of what the highest QuestionId that wasn't zero length. This will accurately\r
509 // maintain the Size of the NvStore\r
510 //\r
dc7b4a5c 511 if (((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width != 0) {\r
512 Temp = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId + ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width;\r
5d01b0f7 513 if (SizeOfNvStore < Temp) {\r
dc7b4a5c 514 SizeOfNvStore = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId + ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width;\r
5d01b0f7 515 }\r
516 }\r
517 }\r
518\r
519 Index = RawData[Index + 1] + Index;\r
520 }\r
521\r
522 //\r
523 // Return an error if buffer is too small\r
524 //\r
525 if (SizeOfNvStore > *ImageLength) {\r
526 gBS->FreePool (OldData);\r
527 *ImageLength = (UINT16) SizeOfNvStore;\r
528 return EFI_BUFFER_TOO_SMALL;\r
529 }\r
530\r
531 if (DefaultImage != NULL) {\r
532 ZeroMem (DefaultImage, SizeOfNvStore);\r
533 }\r
534\r
535 //\r
536 // Copy the default image information to the user's buffer\r
537 //\r
dc7b4a5c 538 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
5d01b0f7 539 switch (RawData[Index]) {\r
49a049e6 540 \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
49a049e6 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
49a049e6 587 @return Handle to the HII pack previously registered by the memory driver.\r
2ec4d269 588**/\r
dc7b4a5c 589FRAMEWORK_EFI_HII_HANDLE \r
5d01b0f7 590FindHiiHandle (\r
49a049e6 591 IN OUT EFI_HII_PROTOCOL **HiiProtocol, OPTIONAL\r
592 IN EFI_GUID *Guid\r
5d01b0f7 593 )\r
5d01b0f7 594{\r
49a049e6 595 EFI_STATUS Status;\r
596 FRAMEWORK_EFI_HII_HANDLE *HiiHandleBuffer;\r
597 FRAMEWORK_EFI_HII_HANDLE HiiHandle;\r
598 UINT16 HiiHandleBufferLength;\r
599 UINT32 NumberOfHiiHandles;\r
600 EFI_GUID HiiGuid;\r
601 EFI_HII_PROTOCOL *HiiProt;\r
602 UINT32 Index;\r
603 UINT16 Length;\r
5d01b0f7 604\r
605 HiiHandle = 0;\r
606 if ((HiiProtocol != NULL) && (*HiiProtocol != NULL)) {\r
607 //\r
608 // The protocol has been passed in\r
609 //\r
610 HiiProt = *HiiProtocol;\r
611 } else {\r
612 gBS->LocateProtocol (\r
613 &gEfiHiiProtocolGuid,\r
614 NULL,\r
615 (VOID **) &HiiProt\r
616 );\r
617 if (HiiProt == NULL) {\r
618 return HiiHandle;\r
619 }\r
620\r
621 if (HiiProtocol != NULL) {\r
622 //\r
623 // Return back the HII protocol for the caller as promissed\r
624 //\r
625 *HiiProtocol = HiiProt;\r
626 }\r
627 }\r
628 //\r
629 // Allocate buffer\r
630 //\r
631 HiiHandleBufferLength = 10;\r
632 HiiHandleBuffer = AllocatePool (HiiHandleBufferLength);\r
633 ASSERT (HiiHandleBuffer != NULL);\r
634\r
635 //\r
636 // Get the Handles of the packages that were registered with Hii\r
637 //\r
638 Status = HiiProt->FindHandles (\r
639 HiiProt,\r
640 &HiiHandleBufferLength,\r
641 HiiHandleBuffer\r
642 );\r
643\r
644 //\r
645 // Get a bigger bugffer if this one is to small, and try again\r
646 //\r
647 if (Status == EFI_BUFFER_TOO_SMALL) {\r
648\r
649 gBS->FreePool (HiiHandleBuffer);\r
650\r
651 HiiHandleBuffer = AllocatePool (HiiHandleBufferLength);\r
652 ASSERT (HiiHandleBuffer != NULL);\r
653\r
654 Status = HiiProt->FindHandles (\r
655 HiiProt,\r
656 &HiiHandleBufferLength,\r
657 HiiHandleBuffer\r
658 );\r
659 }\r
660\r
661 if (EFI_ERROR (Status)) {\r
662 goto lbl_exit;\r
663 }\r
664\r
dc7b4a5c 665 NumberOfHiiHandles = HiiHandleBufferLength / sizeof (FRAMEWORK_EFI_HII_HANDLE );\r
5d01b0f7 666\r
667 //\r
668 // Iterate Hii handles and look for the one that matches our Guid\r
669 //\r
670 for (Index = 0; Index < NumberOfHiiHandles; Index++) {\r
671\r
672 Length = 0;\r
673 ExtractDataFromHiiHandle (HiiHandleBuffer[Index], &Length, NULL, &HiiGuid);\r
674\r
675 if (CompareGuid (&HiiGuid, Guid)) {\r
5d01b0f7 676 HiiHandle = HiiHandleBuffer[Index];\r
677 break;\r
678 }\r
679 }\r
680\r
681lbl_exit:\r
682 gBS->FreePool (HiiHandleBuffer);\r
683 return HiiHandle;\r
684}\r
685\r
2ec4d269 686/**\r
687 Validate that the data associated with the HiiHandle in NVRAM is within\r
688 the reasonable parameters for that FormSet. Values for strings and passwords\r
689 are not verified due to their not having the equivalent of valid range settings.\r
690\r
49a049e6 691 @param HiiHandle Handle of the HII database entry to query\r
692 @param Results If return Status is EFI_SUCCESS, Results provides valid data\r
2ec4d269 693 TRUE = NVRAM Data is within parameters\r
694 FALSE = NVRAM Data is NOT within parameters\r
49a049e6 695 \r
696 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate\r
697 @retval EFI_SUCCESS Data successfully validated\r
2ec4d269 698**/\r
5d01b0f7 699EFI_STATUS\r
700ValidateDataFromHiiHandle (\r
49a049e6 701 IN FRAMEWORK_EFI_HII_HANDLE HiiHandle,\r
702 OUT BOOLEAN *Results\r
5d01b0f7 703 )\r
5d01b0f7 704{\r
49a049e6 705 EFI_STATUS Status;\r
706 EFI_HII_PROTOCOL *Hii;\r
707 EFI_GUID Guid;\r
708 UINT8 *RawData;\r
709 UINT8 *OldData;\r
710 UINTN RawDataLength;\r
711 UINT8 *VariableData;\r
712 UINTN Index;\r
713 UINTN Temp;\r
714 UINTN SizeOfNvStore;\r
715 UINTN CachedStart;\r
716 BOOLEAN GotMatch;\r
5d01b0f7 717\r
718 RawDataLength = DEFAULT_FORM_BUFFER_SIZE;\r
719 SizeOfNvStore = 0;\r
720 CachedStart = 0;\r
721 GotMatch = FALSE;\r
722 *Results = TRUE;\r
723\r
724 Status = GetHiiInterface (&Hii);\r
5d01b0f7 725 if (EFI_ERROR (Status)) {\r
726 return Status;\r
727 }\r
728\r
729 //\r
730 // Allocate space for retrieval of IFR data\r
731 //\r
732 RawData = AllocateZeroPool (RawDataLength);\r
733 if (RawData == NULL) {\r
734 return EFI_OUT_OF_RESOURCES;\r
735 }\r
736\r
737 //\r
738 // Get all the forms associated with this HiiHandle\r
739 //\r
740 Status = Hii->GetForms (Hii, HiiHandle, 0, &RawDataLength, RawData);\r
5d01b0f7 741 if (EFI_ERROR (Status)) {\r
742 gBS->FreePool (RawData);\r
743\r
744 //\r
745 // Allocate space for retrieval of IFR data\r
746 //\r
747 RawData = AllocateZeroPool (RawDataLength);\r
748 if (RawData == NULL) {\r
749 return EFI_OUT_OF_RESOURCES;\r
750 }\r
751\r
752 //\r
753 // Get all the forms associated with this HiiHandle\r
754 //\r
755 Status = Hii->GetForms (Hii, HiiHandle, 0, &RawDataLength, RawData);\r
756 }\r
757\r
758 OldData = RawData;\r
759\r
760 //\r
761 // Point RawData to the beginning of the form data\r
762 //\r
763 RawData = (UINT8 *) ((UINTN) RawData + sizeof (EFI_HII_PACK_HEADER));\r
764\r
dc7b4a5c 765 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
766 if (RawData[Index] == FRAMEWORK_EFI_IFR_FORM_SET_OP) {\r
767 CopyMem (&Guid, &((FRAMEWORK_EFI_IFR_FORM_SET *) &RawData[Index])->Guid, sizeof (EFI_GUID));\r
5d01b0f7 768 break;\r
769 }\r
770\r
771 Index = RawData[Index + 1] + Index;\r
772 }\r
773\r
dc7b4a5c 774 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
5d01b0f7 775 switch (RawData[Index]) {\r
49a049e6 776 \r
dc7b4a5c 777 case FRAMEWORK_EFI_IFR_FORM_SET_OP:\r
5d01b0f7 778 break;\r
779\r
dc7b4a5c 780 case FRAMEWORK_EFI_IFR_ONE_OF_OP:\r
781 case FRAMEWORK_EFI_IFR_CHECKBOX_OP:\r
782 case FRAMEWORK_EFI_IFR_NUMERIC_OP:\r
783 case FRAMEWORK_EFI_IFR_DATE_OP:\r
784 case FRAMEWORK_EFI_IFR_TIME_OP:\r
785 case FRAMEWORK_EFI_IFR_PASSWORD_OP:\r
786 case FRAMEWORK_EFI_IFR_STRING_OP:\r
5d01b0f7 787 //\r
788 // Remember, multiple op-codes may reference the same item, so let's keep a running\r
789 // marker of what the highest QuestionId that wasn't zero length. This will accurately\r
790 // maintain the Size of the NvStore\r
791 //\r
dc7b4a5c 792 if (((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width != 0) {\r
793 Temp = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId + ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width;\r
5d01b0f7 794 if (SizeOfNvStore < Temp) {\r
dc7b4a5c 795 SizeOfNvStore = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId + ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width;\r
5d01b0f7 796 }\r
797 }\r
798 }\r
799\r
800 Index = RawData[Index + 1] + Index;\r
801 }\r
802\r
803 //\r
804 // Allocate memory for our File Form Tags\r
805 //\r
806 VariableData = AllocateZeroPool (SizeOfNvStore);\r
807 if (VariableData == NULL) {\r
808 return EFI_OUT_OF_RESOURCES;\r
809 }\r
810\r
811 Status = gRT->GetVariable (\r
812 (CHAR16 *) L"Setup",\r
813 &Guid,\r
814 NULL,\r
815 &SizeOfNvStore,\r
816 (VOID *) VariableData\r
817 );\r
818\r
819 if (EFI_ERROR (Status)) {\r
820\r
821 //\r
822 // If there is a variable that exists already and it is larger than what we calculated the\r
823 // storage needs to be, we must assume the variable size from GetVariable is correct and not\r
824 // allow the truncation of the variable. It is very possible that the user who created the IFR\r
825 // we are cracking is not referring to a variable that was in a previous map, however we cannot\r
826 // allow it's truncation.\r
827 //\r
828 if (Status == EFI_BUFFER_TOO_SMALL) {\r
829 //\r
830 // Free the buffer that was allocated that was too small\r
831 //\r
832 gBS->FreePool (VariableData);\r
833\r
834 VariableData = AllocatePool (SizeOfNvStore);\r
835 if (VariableData == NULL) {\r
836 return EFI_OUT_OF_RESOURCES;\r
837 }\r
838\r
839 Status = gRT->GetVariable (\r
840 (CHAR16 *) L"Setup",\r
841 &Guid,\r
842 NULL,\r
843 &SizeOfNvStore,\r
844 (VOID *) VariableData\r
845 );\r
846 }\r
847 }\r
848\r
849 //\r
850 // Walk through the form and see that the variable data it refers to is ok.\r
851 // This allows for the possibility of stale (obsoleted) data in the variable\r
852 // can be overlooked without causing an error\r
853 //\r
dc7b4a5c 854 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
5d01b0f7 855 switch (RawData[Index]) {\r
49a049e6 856 \r
dc7b4a5c 857 case FRAMEWORK_EFI_IFR_ONE_OF_OP:\r
5d01b0f7 858 //\r
859 // A one_of has no data, its the option that does - cache the storage Id\r
860 //\r
dc7b4a5c 861 CachedStart = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId;\r
5d01b0f7 862 break;\r
863\r
dc7b4a5c 864 case FRAMEWORK_EFI_IFR_ONE_OF_OPTION_OP:\r
5d01b0f7 865 //\r
866 // A one_of_option can be any value\r
867 //\r
dc7b4a5c 868 if (VariableData[CachedStart] == ((FRAMEWORK_EFI_IFR_ONE_OF_OPTION *) &RawData[Index])->Value) {\r
5d01b0f7 869 GotMatch = TRUE;\r
870 }\r
871 break;\r
872\r
dc7b4a5c 873 case FRAMEWORK_EFI_IFR_END_ONE_OF_OP:\r
5d01b0f7 874 //\r
875 // At this point lets make sure that the data value in the NVRAM matches one of the options\r
876 //\r
877 if (!GotMatch) {\r
878 *Results = FALSE;\r
879 return EFI_SUCCESS;\r
880 }\r
881 break;\r
882\r
dc7b4a5c 883 case FRAMEWORK_EFI_IFR_CHECKBOX_OP:\r
5d01b0f7 884 //\r
885 // A checkbox is a boolean, so 0 and 1 are valid\r
886 // Remember, QuestionId corresponds to the offset location of the data in the variable\r
887 //\r
dc7b4a5c 888 if (VariableData[((FRAMEWORK_EFI_IFR_CHECKBOX *) &RawData[Index])->QuestionId] > 1) {\r
5d01b0f7 889 *Results = FALSE;\r
890 return EFI_SUCCESS;\r
891 }\r
892 break;\r
893\r
dc7b4a5c 894 case FRAMEWORK_EFI_IFR_NUMERIC_OP:\r
895 if ((VariableData[((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->QuestionId] < ((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->Minimum) ||\r
896 (VariableData[((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->QuestionId] > ((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->Maximum)) {\r
5d01b0f7 897 *Results = FALSE;\r
898 return EFI_SUCCESS;\r
899 }\r
900 break;\r
901\r
902 }\r
903\r
904 Index = RawData[Index + 1] + Index;\r
905 }\r
906\r
907 //\r
908 // Free our temporary repository of form data\r
909 //\r
910 gBS->FreePool (OldData);\r
911 gBS->FreePool (VariableData);\r
912\r
913 return EFI_SUCCESS;\r
914}\r
915\r
dc7b4a5c 916\r