]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkPkg/Library/FrameworkIfrSupportLib/IfrCommon.c
Fixed two K8 report issue
[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
409f118c 29EFIAPI\r
5d01b0f7 30GetCurrentLanguage (\r
49a049e6 31 OUT CHAR16 *Lang\r
5d01b0f7 32 )\r
5d01b0f7 33{\r
49a049e6 34 EFI_STATUS Status;\r
35 UINTN Size;\r
36 UINTN Index;\r
37 CHAR8 Language[4];\r
5d01b0f7 38\r
39 //\r
40 // Getting the system language and placing it into our Global Data\r
41 //\r
42 Size = sizeof (Language);\r
5d01b0f7 43 Status = gRT->GetVariable (\r
44 (CHAR16 *) L"Lang",\r
45 &gEfiGlobalVariableGuid,\r
46 NULL,\r
47 &Size,\r
48 Language\r
49 );\r
5d01b0f7 50 if (EFI_ERROR (Status)) {\r
51 AsciiStrCpy (Language, "eng");\r
52 }\r
53\r
54 for (Index = 0; Index < 3; Index++) {\r
55 //\r
56 // Bitwise AND ascii value with 0xDF yields an uppercase value.\r
57 // Sign extend into a unicode value\r
58 //\r
59 Lang[Index] = (CHAR16) (Language[Index] & 0xDF);\r
60 }\r
61\r
62 //\r
63 // Null-terminate the value\r
64 //\r
65 Lang[3] = (CHAR16) 0;\r
66\r
67 return Status;\r
68}\r
69\r
2ec4d269 70/**\r
49a049e6 71 Add a string to the incoming buffer and return the token and offset data.\r
2ec4d269 72 \r
49a049e6 73 @param StringBuffer The incoming buffer\r
74 @param Language Currrent language\r
75 @param String The string to be added\r
76 @param StringToken The index where the string placed \r
2ec4d269 77 \r
78 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate\r
79 @retval EFI_SUCCESS String successfully added to the incoming buffer\r
80**/\r
5d01b0f7 81EFI_STATUS\r
409f118c 82EFIAPI\r
5d01b0f7 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
409f118c 149 PackDestination[Index] = (UINT16)(PackDestination[Index - 1] +\r
150 StrSize((CHAR16 *)((CHAR8 *)(StringPack) + PackSource[Index - 1])));\r
5d01b0f7 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
409f118c 277 FreePool (NewBuffer);\r
5d01b0f7 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
409f118c 291EFIAPI\r
5d01b0f7 292AddOpCode (\r
49a049e6 293 IN VOID *FormBuffer,\r
294 IN OUT VOID *OpCodeData\r
5d01b0f7 295 )\r
5d01b0f7 296{\r
49a049e6 297 EFI_HII_PACK_HEADER *NewBuffer;\r
298 UINT8 *Source;\r
299 UINT8 *Destination;\r
5d01b0f7 300\r
301 //\r
302 // Pre-allocate a buffer sufficient for us to work on.\r
303 // We will use it as a destination scratch pad to build data on\r
304 // and when complete shift the data back to the original buffer\r
305 //\r
306 NewBuffer = AllocateZeroPool (DEFAULT_FORM_BUFFER_SIZE);\r
307 if (NewBuffer == NULL) {\r
308 return EFI_OUT_OF_RESOURCES;\r
309 }\r
310\r
5d01b0f7 311 //\r
312 // Copy the IFR Package header to the new buffer\r
313 //\r
409f118c 314 Source = (UINT8 *) FormBuffer;\r
315 Destination = (UINT8 *) NewBuffer;\r
5d01b0f7 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
409f118c 329 // If the opcode is an end_form_set we better be creating and endform\r
5d01b0f7 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
409f118c 354 \r
355 Destination = Destination + (UINTN) ((FRAMEWORK_EFI_IFR_OP_HEADER *) OpCodeData)->Length;\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
409f118c 364 // Copy end-formset data to new buffer\r
5d01b0f7 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
dc7b4a5c 368 CopyMem (Destination, Source, ((FRAMEWORK_EFI_IFR_OP_HEADER *) Source)->Length);\r
5d01b0f7 369\r
370 //\r
371 // Zero out the original buffer and copy the updated data in the new buffer to the old buffer\r
372 //\r
373 ZeroMem (FormBuffer, DEFAULT_FORM_BUFFER_SIZE);\r
374 CopyMem (FormBuffer, NewBuffer, DEFAULT_FORM_BUFFER_SIZE);\r
375\r
376 //\r
377 // Free the newly created buffer since we don't need it anymore\r
378 //\r
409f118c 379 FreePool (NewBuffer);\r
5d01b0f7 380 return EFI_SUCCESS;\r
381}\r
382\r
2ec4d269 383/**\r
49a049e6 384 Get the HII protocol interface.\r
2ec4d269 385 \r
49a049e6 386 @param Hii HII protocol interface\r
2ec4d269 387 \r
49a049e6 388 @return the statue of locating HII protocol\r
2ec4d269 389**/\r
5d01b0f7 390EFI_STATUS\r
409f118c 391EFIAPI\r
5d01b0f7 392GetHiiInterface (\r
49a049e6 393 OUT EFI_HII_PROTOCOL **Hii\r
5d01b0f7 394 )\r
5d01b0f7 395{\r
49a049e6 396 EFI_STATUS Status;\r
5d01b0f7 397\r
398 //\r
399 // There should only be one HII protocol\r
400 //\r
401 Status = gBS->LocateProtocol (\r
402 &gEfiHiiProtocolGuid,\r
403 NULL,\r
404 (VOID **) Hii\r
405 );\r
406\r
407 return Status;;\r
408}\r
409\r
2ec4d269 410/**\r
49a049e6 411 Extract information pertaining to the HiiHandle.\r
2ec4d269 412 \r
49a049e6 413 @param HiiHandle Hii handle\r
414 @param ImageLength For input, length of DefaultImage;\r
415 For output, length of actually required\r
416 @param DefaultImage Image buffer prepared by caller\r
417 @param Guid Guid information about the form \r
2ec4d269 418 \r
49a049e6 419 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate\r
420 @retval EFI_BUFFER_TOO_SMALL DefualtImage has no enough ImageLength\r
421 @retval EFI_SUCCESS Successfully extract data from Hii database.\r
2ec4d269 422**/\r
5d01b0f7 423EFI_STATUS\r
409f118c 424EFIAPI\r
5d01b0f7 425ExtractDataFromHiiHandle (\r
49a049e6 426 IN FRAMEWORK_EFI_HII_HANDLE HiiHandle,\r
427 IN OUT UINT16 *ImageLength,\r
428 OUT UINT8 *DefaultImage,\r
429 OUT EFI_GUID *Guid\r
5d01b0f7 430 )\r
5d01b0f7 431{\r
49a049e6 432 EFI_STATUS Status;\r
433 EFI_HII_PROTOCOL *Hii;\r
434 UINTN DataLength;\r
435 UINT8 *RawData;\r
436 UINT8 *OldData;\r
437 UINTN Index;\r
438 UINTN Temp;\r
439 UINTN SizeOfNvStore;\r
440 UINTN CachedStart;\r
5d01b0f7 441\r
442 DataLength = DEFAULT_FORM_BUFFER_SIZE;\r
443 SizeOfNvStore = 0;\r
444 CachedStart = 0;\r
445\r
5547062d 446 if (DefaultImage == NULL || Guid == NULL) {\r
447 return EFI_INVALID_PARAMETER;\r
448 }\r
449 \r
5d01b0f7 450 Status = GetHiiInterface (&Hii);\r
5d01b0f7 451 if (EFI_ERROR (Status)) {\r
452 return Status;\r
453 }\r
454\r
455 //\r
456 // Allocate space for retrieval of IFR data\r
457 //\r
458 RawData = AllocateZeroPool (DataLength);\r
459 if (RawData == NULL) {\r
460 return EFI_OUT_OF_RESOURCES;\r
461 }\r
462\r
463 //\r
464 // Get all the forms associated with this HiiHandle\r
465 //\r
466 Status = Hii->GetForms (Hii, HiiHandle, 0, &DataLength, RawData);\r
5d01b0f7 467 if (EFI_ERROR (Status)) {\r
409f118c 468 FreePool (RawData);\r
5d01b0f7 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
49a049e6 493 \r
dc7b4a5c 494 case FRAMEWORK_EFI_IFR_FORM_SET_OP:\r
5d01b0f7 495 //\r
496 // Copy the GUID information from this handle\r
497 //\r
77980854 498 CopyGuid (Guid, (GUID *)(VOID *)&((FRAMEWORK_EFI_IFR_FORM_SET *) &RawData[Index])->Guid);\r
5d01b0f7 499 break;\r
500\r
dc7b4a5c 501 case FRAMEWORK_EFI_IFR_ONE_OF_OP:\r
502 case FRAMEWORK_EFI_IFR_CHECKBOX_OP:\r
503 case FRAMEWORK_EFI_IFR_NUMERIC_OP:\r
504 case FRAMEWORK_EFI_IFR_DATE_OP:\r
505 case FRAMEWORK_EFI_IFR_TIME_OP:\r
506 case FRAMEWORK_EFI_IFR_PASSWORD_OP:\r
507 case FRAMEWORK_EFI_IFR_STRING_OP:\r
5d01b0f7 508 //\r
509 // Remember, multiple op-codes may reference the same item, so let's keep a running\r
510 // marker of what the highest QuestionId that wasn't zero length. This will accurately\r
511 // maintain the Size of the NvStore\r
512 //\r
dc7b4a5c 513 if (((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width != 0) {\r
514 Temp = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId + ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width;\r
5d01b0f7 515 if (SizeOfNvStore < Temp) {\r
dc7b4a5c 516 SizeOfNvStore = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId + ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width;\r
5d01b0f7 517 }\r
518 }\r
519 }\r
520\r
521 Index = RawData[Index + 1] + Index;\r
522 }\r
523\r
524 //\r
525 // Return an error if buffer is too small\r
526 //\r
527 if (SizeOfNvStore > *ImageLength) {\r
409f118c 528 FreePool (OldData);\r
5d01b0f7 529 *ImageLength = (UINT16) SizeOfNvStore;\r
530 return EFI_BUFFER_TOO_SMALL;\r
531 }\r
532\r
533 if (DefaultImage != NULL) {\r
534 ZeroMem (DefaultImage, SizeOfNvStore);\r
535 }\r
536\r
537 //\r
538 // Copy the default image information to the user's buffer\r
539 //\r
dc7b4a5c 540 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
5d01b0f7 541 switch (RawData[Index]) {\r
49a049e6 542 \r
dc7b4a5c 543 case FRAMEWORK_EFI_IFR_ONE_OF_OP:\r
544 CachedStart = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId;\r
5d01b0f7 545 break;\r
546\r
dc7b4a5c 547 case FRAMEWORK_EFI_IFR_ONE_OF_OPTION_OP:\r
548 if (((FRAMEWORK_EFI_IFR_ONE_OF_OPTION *) &RawData[Index])->Flags & FRAMEWORK_EFI_IFR_FLAG_DEFAULT) {\r
549 CopyMem (&DefaultImage[CachedStart], &((FRAMEWORK_EFI_IFR_ONE_OF_OPTION *) &RawData[Index])->Value, 2);\r
5d01b0f7 550 }\r
551 break;\r
552\r
dc7b4a5c 553 case FRAMEWORK_EFI_IFR_CHECKBOX_OP:\r
554 DefaultImage[((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId] = ((FRAMEWORK_EFI_IFR_CHECKBOX *) &RawData[Index])->Flags;\r
5d01b0f7 555 break;\r
556\r
dc7b4a5c 557 case FRAMEWORK_EFI_IFR_NUMERIC_OP:\r
5d01b0f7 558 CopyMem (\r
dc7b4a5c 559 &DefaultImage[((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId],\r
560 &((FRAMEWORK_EFI_IFR_NUMERIC *) &RawData[Index])->Default,\r
5d01b0f7 561 2\r
562 );\r
563 break;\r
564\r
565 }\r
566\r
567 Index = RawData[Index + 1] + Index;\r
568 }\r
569\r
570 *ImageLength = (UINT16) SizeOfNvStore;\r
571\r
572 //\r
573 // Free our temporary repository of form data\r
574 //\r
409f118c 575 FreePool (OldData);\r
5d01b0f7 576\r
577 return EFI_SUCCESS;\r
578}\r
579\r
2ec4d269 580/**\r
581 Finds HII handle for given pack GUID previously registered with the HII.\r
582 \r
49a049e6 583 @param HiiProtocol pointer to pointer to HII protocol interface.\r
584 If NULL, the interface will be found but not returned.\r
585 If it points to NULL, the interface will be found and\r
586 written back to the pointer that is pointed to.\r
587 @param Guid The GUID of the pack that registered with the HII.\r
5d01b0f7 588\r
49a049e6 589 @return Handle to the HII pack previously registered by the memory driver.\r
2ec4d269 590**/\r
409f118c 591FRAMEWORK_EFI_HII_HANDLE\r
592EFIAPI\r
5d01b0f7 593FindHiiHandle (\r
49a049e6 594 IN OUT EFI_HII_PROTOCOL **HiiProtocol, OPTIONAL\r
595 IN EFI_GUID *Guid\r
5d01b0f7 596 )\r
5d01b0f7 597{\r
49a049e6 598 EFI_STATUS Status;\r
599 FRAMEWORK_EFI_HII_HANDLE *HiiHandleBuffer;\r
600 FRAMEWORK_EFI_HII_HANDLE HiiHandle;\r
601 UINT16 HiiHandleBufferLength;\r
602 UINT32 NumberOfHiiHandles;\r
603 EFI_GUID HiiGuid;\r
604 EFI_HII_PROTOCOL *HiiProt;\r
605 UINT32 Index;\r
606 UINT16 Length;\r
5d01b0f7 607\r
608 HiiHandle = 0;\r
609 if ((HiiProtocol != NULL) && (*HiiProtocol != NULL)) {\r
610 //\r
611 // The protocol has been passed in\r
612 //\r
613 HiiProt = *HiiProtocol;\r
614 } else {\r
615 gBS->LocateProtocol (\r
616 &gEfiHiiProtocolGuid,\r
617 NULL,\r
618 (VOID **) &HiiProt\r
619 );\r
620 if (HiiProt == NULL) {\r
621 return HiiHandle;\r
622 }\r
623\r
624 if (HiiProtocol != NULL) {\r
625 //\r
626 // Return back the HII protocol for the caller as promissed\r
627 //\r
628 *HiiProtocol = HiiProt;\r
629 }\r
630 }\r
631 //\r
632 // Allocate buffer\r
633 //\r
634 HiiHandleBufferLength = 10;\r
635 HiiHandleBuffer = AllocatePool (HiiHandleBufferLength);\r
636 ASSERT (HiiHandleBuffer != NULL);\r
637\r
638 //\r
639 // Get the Handles of the packages that were registered with Hii\r
640 //\r
641 Status = HiiProt->FindHandles (\r
642 HiiProt,\r
643 &HiiHandleBufferLength,\r
644 HiiHandleBuffer\r
645 );\r
646\r
647 //\r
648 // Get a bigger bugffer if this one is to small, and try again\r
649 //\r
650 if (Status == EFI_BUFFER_TOO_SMALL) {\r
651\r
409f118c 652 FreePool (HiiHandleBuffer);\r
5d01b0f7 653\r
654 HiiHandleBuffer = AllocatePool (HiiHandleBufferLength);\r
655 ASSERT (HiiHandleBuffer != NULL);\r
656\r
657 Status = HiiProt->FindHandles (\r
658 HiiProt,\r
659 &HiiHandleBufferLength,\r
660 HiiHandleBuffer\r
661 );\r
662 }\r
663\r
664 if (EFI_ERROR (Status)) {\r
665 goto lbl_exit;\r
666 }\r
667\r
dc7b4a5c 668 NumberOfHiiHandles = HiiHandleBufferLength / sizeof (FRAMEWORK_EFI_HII_HANDLE );\r
5d01b0f7 669\r
670 //\r
671 // Iterate Hii handles and look for the one that matches our Guid\r
672 //\r
673 for (Index = 0; Index < NumberOfHiiHandles; Index++) {\r
674\r
675 Length = 0;\r
676 ExtractDataFromHiiHandle (HiiHandleBuffer[Index], &Length, NULL, &HiiGuid);\r
677\r
678 if (CompareGuid (&HiiGuid, Guid)) {\r
5d01b0f7 679 HiiHandle = HiiHandleBuffer[Index];\r
680 break;\r
681 }\r
682 }\r
683\r
684lbl_exit:\r
409f118c 685 FreePool (HiiHandleBuffer);\r
5d01b0f7 686 return HiiHandle;\r
687}\r
688\r
2ec4d269 689/**\r
690 Validate that the data associated with the HiiHandle in NVRAM is within\r
691 the reasonable parameters for that FormSet. Values for strings and passwords\r
692 are not verified due to their not having the equivalent of valid range settings.\r
693\r
49a049e6 694 @param HiiHandle Handle of the HII database entry to query\r
695 @param Results If return Status is EFI_SUCCESS, Results provides valid data\r
2ec4d269 696 TRUE = NVRAM Data is within parameters\r
697 FALSE = NVRAM Data is NOT within parameters\r
49a049e6 698 \r
699 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate\r
700 @retval EFI_SUCCESS Data successfully validated\r
2ec4d269 701**/\r
5d01b0f7 702EFI_STATUS\r
409f118c 703EFIAPI\r
5d01b0f7 704ValidateDataFromHiiHandle (\r
49a049e6 705 IN FRAMEWORK_EFI_HII_HANDLE HiiHandle,\r
706 OUT BOOLEAN *Results\r
5d01b0f7 707 )\r
5d01b0f7 708{\r
49a049e6 709 EFI_STATUS Status;\r
710 EFI_HII_PROTOCOL *Hii;\r
711 EFI_GUID Guid;\r
712 UINT8 *RawData;\r
713 UINT8 *OldData;\r
714 UINTN RawDataLength;\r
715 UINT8 *VariableData;\r
716 UINTN Index;\r
717 UINTN Temp;\r
718 UINTN SizeOfNvStore;\r
719 UINTN CachedStart;\r
720 BOOLEAN GotMatch;\r
5d01b0f7 721\r
722 RawDataLength = DEFAULT_FORM_BUFFER_SIZE;\r
723 SizeOfNvStore = 0;\r
724 CachedStart = 0;\r
725 GotMatch = FALSE;\r
726 *Results = TRUE;\r
727\r
728 Status = GetHiiInterface (&Hii);\r
5d01b0f7 729 if (EFI_ERROR (Status)) {\r
730 return Status;\r
731 }\r
732\r
733 //\r
734 // Allocate space for retrieval of IFR data\r
735 //\r
736 RawData = AllocateZeroPool (RawDataLength);\r
737 if (RawData == NULL) {\r
738 return EFI_OUT_OF_RESOURCES;\r
739 }\r
740\r
741 //\r
742 // Get all the forms associated with this HiiHandle\r
743 //\r
744 Status = Hii->GetForms (Hii, HiiHandle, 0, &RawDataLength, RawData);\r
5d01b0f7 745 if (EFI_ERROR (Status)) {\r
409f118c 746 FreePool (RawData);\r
5d01b0f7 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
77980854 771 CopyGuid (&Guid, (GUID *)(VOID *)&((FRAMEWORK_EFI_IFR_FORM_SET *) &RawData[Index])->Guid);\r
5d01b0f7 772 break;\r
773 }\r
5d01b0f7 774 Index = RawData[Index + 1] + Index;\r
775 }\r
776\r
dc7b4a5c 777 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
5d01b0f7 778 switch (RawData[Index]) {\r
49a049e6 779 \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
1b74e1c7 811 FreePool (OldData);\r
5d01b0f7 812 return EFI_OUT_OF_RESOURCES;\r
813 }\r
814\r
815 Status = gRT->GetVariable (\r
816 (CHAR16 *) L"Setup",\r
817 &Guid,\r
818 NULL,\r
819 &SizeOfNvStore,\r
820 (VOID *) VariableData\r
821 );\r
822\r
823 if (EFI_ERROR (Status)) {\r
824\r
825 //\r
826 // If there is a variable that exists already and it is larger than what we calculated the\r
827 // storage needs to be, we must assume the variable size from GetVariable is correct and not\r
828 // allow the truncation of the variable. It is very possible that the user who created the IFR\r
829 // we are cracking is not referring to a variable that was in a previous map, however we cannot\r
830 // allow it's truncation.\r
831 //\r
832 if (Status == EFI_BUFFER_TOO_SMALL) {\r
833 //\r
834 // Free the buffer that was allocated that was too small\r
835 //\r
409f118c 836 FreePool (VariableData);\r
5d01b0f7 837\r
838 VariableData = AllocatePool (SizeOfNvStore);\r
839 if (VariableData == NULL) {\r
1b74e1c7 840 FreePool (OldData);\r
5d01b0f7 841 return EFI_OUT_OF_RESOURCES;\r
842 }\r
843\r
844 Status = gRT->GetVariable (\r
845 (CHAR16 *) L"Setup",\r
846 &Guid,\r
847 NULL,\r
848 &SizeOfNvStore,\r
849 (VOID *) VariableData\r
850 );\r
851 }\r
852 }\r
853\r
854 //\r
855 // Walk through the form and see that the variable data it refers to is ok.\r
856 // This allows for the possibility of stale (obsoleted) data in the variable\r
857 // can be overlooked without causing an error\r
858 //\r
dc7b4a5c 859 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
5d01b0f7 860 switch (RawData[Index]) {\r
49a049e6 861 \r
dc7b4a5c 862 case FRAMEWORK_EFI_IFR_ONE_OF_OP:\r
5d01b0f7 863 //\r
864 // A one_of has no data, its the option that does - cache the storage Id\r
865 //\r
dc7b4a5c 866 CachedStart = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId;\r
5d01b0f7 867 break;\r
868\r
dc7b4a5c 869 case FRAMEWORK_EFI_IFR_ONE_OF_OPTION_OP:\r
5d01b0f7 870 //\r
871 // A one_of_option can be any value\r
872 //\r
dc7b4a5c 873 if (VariableData[CachedStart] == ((FRAMEWORK_EFI_IFR_ONE_OF_OPTION *) &RawData[Index])->Value) {\r
5d01b0f7 874 GotMatch = TRUE;\r
875 }\r
876 break;\r
877\r
dc7b4a5c 878 case FRAMEWORK_EFI_IFR_END_ONE_OF_OP:\r
5d01b0f7 879 //\r
880 // At this point lets make sure that the data value in the NVRAM matches one of the options\r
881 //\r
882 if (!GotMatch) {\r
883 *Results = FALSE;\r
1b74e1c7 884 goto EXIT;\r
5d01b0f7 885 }\r
886 break;\r
887\r
dc7b4a5c 888 case FRAMEWORK_EFI_IFR_CHECKBOX_OP:\r
5d01b0f7 889 //\r
890 // A checkbox is a boolean, so 0 and 1 are valid\r
891 // Remember, QuestionId corresponds to the offset location of the data in the variable\r
892 //\r
dc7b4a5c 893 if (VariableData[((FRAMEWORK_EFI_IFR_CHECKBOX *) &RawData[Index])->QuestionId] > 1) {\r
5d01b0f7 894 *Results = FALSE;\r
1b74e1c7 895 goto EXIT;\r
5d01b0f7 896 }\r
897 break;\r
898\r
dc7b4a5c 899 case FRAMEWORK_EFI_IFR_NUMERIC_OP:\r
900 if ((VariableData[((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->QuestionId] < ((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->Minimum) ||\r
901 (VariableData[((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->QuestionId] > ((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->Maximum)) {\r
5d01b0f7 902 *Results = FALSE;\r
1b74e1c7 903 goto EXIT;\r
5d01b0f7 904 }\r
905 break;\r
906\r
907 }\r
908\r
909 Index = RawData[Index + 1] + Index;\r
910 }\r
911\r
1b74e1c7 912EXIT: \r
5d01b0f7 913 //\r
914 // Free our temporary repository of form data\r
915 //\r
1b74e1c7 916 FreePool (OldData);\r
917 FreePool (VariableData);\r
5d01b0f7 918\r
919 return EFI_SUCCESS;\r
920}\r
921\r
dc7b4a5c 922\r