]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkPkg/Library/FrameworkIfrSupportLib/IfrCommon.c
Replace some CopyMem() for GUID copy with CopyGuid().
[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
446 Status = GetHiiInterface (&Hii);\r
5d01b0f7 447 if (EFI_ERROR (Status)) {\r
448 return Status;\r
449 }\r
450\r
451 //\r
452 // Allocate space for retrieval of IFR data\r
453 //\r
454 RawData = AllocateZeroPool (DataLength);\r
455 if (RawData == NULL) {\r
456 return EFI_OUT_OF_RESOURCES;\r
457 }\r
458\r
459 //\r
460 // Get all the forms associated with this HiiHandle\r
461 //\r
462 Status = Hii->GetForms (Hii, HiiHandle, 0, &DataLength, RawData);\r
5d01b0f7 463 if (EFI_ERROR (Status)) {\r
409f118c 464 FreePool (RawData);\r
5d01b0f7 465\r
466 //\r
467 // Allocate space for retrieval of IFR data\r
468 //\r
469 RawData = AllocateZeroPool (DataLength);\r
470 if (RawData == NULL) {\r
471 return EFI_OUT_OF_RESOURCES;\r
472 }\r
473\r
474 //\r
475 // Get all the forms associated with this HiiHandle\r
476 //\r
477 Status = Hii->GetForms (Hii, HiiHandle, 0, &DataLength, RawData);\r
478 }\r
479\r
480 OldData = RawData;\r
481\r
482 //\r
483 // Point RawData to the beginning of the form data\r
484 //\r
485 RawData = (UINT8 *) ((UINTN) RawData + sizeof (EFI_HII_PACK_HEADER));\r
486\r
dc7b4a5c 487 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
5d01b0f7 488 switch (RawData[Index]) {\r
49a049e6 489 \r
dc7b4a5c 490 case FRAMEWORK_EFI_IFR_FORM_SET_OP:\r
5d01b0f7 491 //\r
492 // Copy the GUID information from this handle\r
493 //\r
409f118c 494 CopyGuid (Guid, &((FRAMEWORK_EFI_IFR_FORM_SET *) &RawData[Index])->Guid);\r
5d01b0f7 495 break;\r
496\r
dc7b4a5c 497 case FRAMEWORK_EFI_IFR_ONE_OF_OP:\r
498 case FRAMEWORK_EFI_IFR_CHECKBOX_OP:\r
499 case FRAMEWORK_EFI_IFR_NUMERIC_OP:\r
500 case FRAMEWORK_EFI_IFR_DATE_OP:\r
501 case FRAMEWORK_EFI_IFR_TIME_OP:\r
502 case FRAMEWORK_EFI_IFR_PASSWORD_OP:\r
503 case FRAMEWORK_EFI_IFR_STRING_OP:\r
5d01b0f7 504 //\r
505 // Remember, multiple op-codes may reference the same item, so let's keep a running\r
506 // marker of what the highest QuestionId that wasn't zero length. This will accurately\r
507 // maintain the Size of the NvStore\r
508 //\r
dc7b4a5c 509 if (((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width != 0) {\r
510 Temp = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId + ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width;\r
5d01b0f7 511 if (SizeOfNvStore < Temp) {\r
dc7b4a5c 512 SizeOfNvStore = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId + ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width;\r
5d01b0f7 513 }\r
514 }\r
515 }\r
516\r
517 Index = RawData[Index + 1] + Index;\r
518 }\r
519\r
520 //\r
521 // Return an error if buffer is too small\r
522 //\r
523 if (SizeOfNvStore > *ImageLength) {\r
409f118c 524 FreePool (OldData);\r
5d01b0f7 525 *ImageLength = (UINT16) SizeOfNvStore;\r
526 return EFI_BUFFER_TOO_SMALL;\r
527 }\r
528\r
529 if (DefaultImage != NULL) {\r
530 ZeroMem (DefaultImage, SizeOfNvStore);\r
531 }\r
532\r
533 //\r
534 // Copy the default image information to the user's buffer\r
535 //\r
dc7b4a5c 536 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
5d01b0f7 537 switch (RawData[Index]) {\r
49a049e6 538 \r
dc7b4a5c 539 case FRAMEWORK_EFI_IFR_ONE_OF_OP:\r
540 CachedStart = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId;\r
5d01b0f7 541 break;\r
542\r
dc7b4a5c 543 case FRAMEWORK_EFI_IFR_ONE_OF_OPTION_OP:\r
544 if (((FRAMEWORK_EFI_IFR_ONE_OF_OPTION *) &RawData[Index])->Flags & FRAMEWORK_EFI_IFR_FLAG_DEFAULT) {\r
545 CopyMem (&DefaultImage[CachedStart], &((FRAMEWORK_EFI_IFR_ONE_OF_OPTION *) &RawData[Index])->Value, 2);\r
5d01b0f7 546 }\r
547 break;\r
548\r
dc7b4a5c 549 case FRAMEWORK_EFI_IFR_CHECKBOX_OP:\r
550 DefaultImage[((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId] = ((FRAMEWORK_EFI_IFR_CHECKBOX *) &RawData[Index])->Flags;\r
5d01b0f7 551 break;\r
552\r
dc7b4a5c 553 case FRAMEWORK_EFI_IFR_NUMERIC_OP:\r
5d01b0f7 554 CopyMem (\r
dc7b4a5c 555 &DefaultImage[((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId],\r
556 &((FRAMEWORK_EFI_IFR_NUMERIC *) &RawData[Index])->Default,\r
5d01b0f7 557 2\r
558 );\r
559 break;\r
560\r
561 }\r
562\r
563 Index = RawData[Index + 1] + Index;\r
564 }\r
565\r
566 *ImageLength = (UINT16) SizeOfNvStore;\r
567\r
568 //\r
569 // Free our temporary repository of form data\r
570 //\r
409f118c 571 FreePool (OldData);\r
5d01b0f7 572\r
573 return EFI_SUCCESS;\r
574}\r
575\r
2ec4d269 576/**\r
577 Finds HII handle for given pack GUID previously registered with the HII.\r
578 \r
49a049e6 579 @param HiiProtocol pointer to pointer to HII protocol interface.\r
580 If NULL, the interface will be found but not returned.\r
581 If it points to NULL, the interface will be found and\r
582 written back to the pointer that is pointed to.\r
583 @param Guid The GUID of the pack that registered with the HII.\r
5d01b0f7 584\r
49a049e6 585 @return Handle to the HII pack previously registered by the memory driver.\r
2ec4d269 586**/\r
409f118c 587FRAMEWORK_EFI_HII_HANDLE\r
588EFIAPI\r
5d01b0f7 589FindHiiHandle (\r
49a049e6 590 IN OUT EFI_HII_PROTOCOL **HiiProtocol, OPTIONAL\r
591 IN EFI_GUID *Guid\r
5d01b0f7 592 )\r
5d01b0f7 593{\r
49a049e6 594 EFI_STATUS Status;\r
595 FRAMEWORK_EFI_HII_HANDLE *HiiHandleBuffer;\r
596 FRAMEWORK_EFI_HII_HANDLE HiiHandle;\r
597 UINT16 HiiHandleBufferLength;\r
598 UINT32 NumberOfHiiHandles;\r
599 EFI_GUID HiiGuid;\r
600 EFI_HII_PROTOCOL *HiiProt;\r
601 UINT32 Index;\r
602 UINT16 Length;\r
5d01b0f7 603\r
604 HiiHandle = 0;\r
605 if ((HiiProtocol != NULL) && (*HiiProtocol != NULL)) {\r
606 //\r
607 // The protocol has been passed in\r
608 //\r
609 HiiProt = *HiiProtocol;\r
610 } else {\r
611 gBS->LocateProtocol (\r
612 &gEfiHiiProtocolGuid,\r
613 NULL,\r
614 (VOID **) &HiiProt\r
615 );\r
616 if (HiiProt == NULL) {\r
617 return HiiHandle;\r
618 }\r
619\r
620 if (HiiProtocol != NULL) {\r
621 //\r
622 // Return back the HII protocol for the caller as promissed\r
623 //\r
624 *HiiProtocol = HiiProt;\r
625 }\r
626 }\r
627 //\r
628 // Allocate buffer\r
629 //\r
630 HiiHandleBufferLength = 10;\r
631 HiiHandleBuffer = AllocatePool (HiiHandleBufferLength);\r
632 ASSERT (HiiHandleBuffer != NULL);\r
633\r
634 //\r
635 // Get the Handles of the packages that were registered with Hii\r
636 //\r
637 Status = HiiProt->FindHandles (\r
638 HiiProt,\r
639 &HiiHandleBufferLength,\r
640 HiiHandleBuffer\r
641 );\r
642\r
643 //\r
644 // Get a bigger bugffer if this one is to small, and try again\r
645 //\r
646 if (Status == EFI_BUFFER_TOO_SMALL) {\r
647\r
409f118c 648 FreePool (HiiHandleBuffer);\r
5d01b0f7 649\r
650 HiiHandleBuffer = AllocatePool (HiiHandleBufferLength);\r
651 ASSERT (HiiHandleBuffer != NULL);\r
652\r
653 Status = HiiProt->FindHandles (\r
654 HiiProt,\r
655 &HiiHandleBufferLength,\r
656 HiiHandleBuffer\r
657 );\r
658 }\r
659\r
660 if (EFI_ERROR (Status)) {\r
661 goto lbl_exit;\r
662 }\r
663\r
dc7b4a5c 664 NumberOfHiiHandles = HiiHandleBufferLength / sizeof (FRAMEWORK_EFI_HII_HANDLE );\r
5d01b0f7 665\r
666 //\r
667 // Iterate Hii handles and look for the one that matches our Guid\r
668 //\r
669 for (Index = 0; Index < NumberOfHiiHandles; Index++) {\r
670\r
671 Length = 0;\r
672 ExtractDataFromHiiHandle (HiiHandleBuffer[Index], &Length, NULL, &HiiGuid);\r
673\r
674 if (CompareGuid (&HiiGuid, Guid)) {\r
5d01b0f7 675 HiiHandle = HiiHandleBuffer[Index];\r
676 break;\r
677 }\r
678 }\r
679\r
680lbl_exit:\r
409f118c 681 FreePool (HiiHandleBuffer);\r
5d01b0f7 682 return HiiHandle;\r
683}\r
684\r
2ec4d269 685/**\r
686 Validate that the data associated with the HiiHandle in NVRAM is within\r
687 the reasonable parameters for that FormSet. Values for strings and passwords\r
688 are not verified due to their not having the equivalent of valid range settings.\r
689\r
49a049e6 690 @param HiiHandle Handle of the HII database entry to query\r
691 @param Results If return Status is EFI_SUCCESS, Results provides valid data\r
2ec4d269 692 TRUE = NVRAM Data is within parameters\r
693 FALSE = NVRAM Data is NOT within parameters\r
49a049e6 694 \r
695 @retval EFI_OUT_OF_RESOURCES No enough buffer to allocate\r
696 @retval EFI_SUCCESS Data successfully validated\r
2ec4d269 697**/\r
5d01b0f7 698EFI_STATUS\r
409f118c 699EFIAPI\r
5d01b0f7 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
409f118c 742 FreePool (RawData);\r
5d01b0f7 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
409f118c 767 CopyGuid (&Guid, &((FRAMEWORK_EFI_IFR_FORM_SET *) &RawData[Index])->Guid);\r
5d01b0f7 768 break;\r
769 }\r
5d01b0f7 770 Index = RawData[Index + 1] + Index;\r
771 }\r
772\r
dc7b4a5c 773 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
5d01b0f7 774 switch (RawData[Index]) {\r
49a049e6 775 \r
dc7b4a5c 776 case FRAMEWORK_EFI_IFR_FORM_SET_OP:\r
5d01b0f7 777 break;\r
778\r
dc7b4a5c 779 case FRAMEWORK_EFI_IFR_ONE_OF_OP:\r
780 case FRAMEWORK_EFI_IFR_CHECKBOX_OP:\r
781 case FRAMEWORK_EFI_IFR_NUMERIC_OP:\r
782 case FRAMEWORK_EFI_IFR_DATE_OP:\r
783 case FRAMEWORK_EFI_IFR_TIME_OP:\r
784 case FRAMEWORK_EFI_IFR_PASSWORD_OP:\r
785 case FRAMEWORK_EFI_IFR_STRING_OP:\r
5d01b0f7 786 //\r
787 // Remember, multiple op-codes may reference the same item, so let's keep a running\r
788 // marker of what the highest QuestionId that wasn't zero length. This will accurately\r
789 // maintain the Size of the NvStore\r
790 //\r
dc7b4a5c 791 if (((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width != 0) {\r
792 Temp = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId + ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width;\r
5d01b0f7 793 if (SizeOfNvStore < Temp) {\r
dc7b4a5c 794 SizeOfNvStore = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId + ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->Width;\r
5d01b0f7 795 }\r
796 }\r
797 }\r
798\r
799 Index = RawData[Index + 1] + Index;\r
800 }\r
801\r
802 //\r
803 // Allocate memory for our File Form Tags\r
804 //\r
805 VariableData = AllocateZeroPool (SizeOfNvStore);\r
806 if (VariableData == NULL) {\r
807 return EFI_OUT_OF_RESOURCES;\r
808 }\r
809\r
810 Status = gRT->GetVariable (\r
811 (CHAR16 *) L"Setup",\r
812 &Guid,\r
813 NULL,\r
814 &SizeOfNvStore,\r
815 (VOID *) VariableData\r
816 );\r
817\r
818 if (EFI_ERROR (Status)) {\r
819\r
820 //\r
821 // If there is a variable that exists already and it is larger than what we calculated the\r
822 // storage needs to be, we must assume the variable size from GetVariable is correct and not\r
823 // allow the truncation of the variable. It is very possible that the user who created the IFR\r
824 // we are cracking is not referring to a variable that was in a previous map, however we cannot\r
825 // allow it's truncation.\r
826 //\r
827 if (Status == EFI_BUFFER_TOO_SMALL) {\r
828 //\r
829 // Free the buffer that was allocated that was too small\r
830 //\r
409f118c 831 FreePool (VariableData);\r
5d01b0f7 832\r
833 VariableData = AllocatePool (SizeOfNvStore);\r
834 if (VariableData == NULL) {\r
835 return EFI_OUT_OF_RESOURCES;\r
836 }\r
837\r
838 Status = gRT->GetVariable (\r
839 (CHAR16 *) L"Setup",\r
840 &Guid,\r
841 NULL,\r
842 &SizeOfNvStore,\r
843 (VOID *) VariableData\r
844 );\r
845 }\r
846 }\r
847\r
848 //\r
849 // Walk through the form and see that the variable data it refers to is ok.\r
850 // This allows for the possibility of stale (obsoleted) data in the variable\r
851 // can be overlooked without causing an error\r
852 //\r
dc7b4a5c 853 for (Index = 0; RawData[Index] != FRAMEWORK_EFI_IFR_END_FORM_SET_OP;) {\r
5d01b0f7 854 switch (RawData[Index]) {\r
49a049e6 855 \r
dc7b4a5c 856 case FRAMEWORK_EFI_IFR_ONE_OF_OP:\r
5d01b0f7 857 //\r
858 // A one_of has no data, its the option that does - cache the storage Id\r
859 //\r
dc7b4a5c 860 CachedStart = ((FRAMEWORK_EFI_IFR_ONE_OF *) &RawData[Index])->QuestionId;\r
5d01b0f7 861 break;\r
862\r
dc7b4a5c 863 case FRAMEWORK_EFI_IFR_ONE_OF_OPTION_OP:\r
5d01b0f7 864 //\r
865 // A one_of_option can be any value\r
866 //\r
dc7b4a5c 867 if (VariableData[CachedStart] == ((FRAMEWORK_EFI_IFR_ONE_OF_OPTION *) &RawData[Index])->Value) {\r
5d01b0f7 868 GotMatch = TRUE;\r
869 }\r
870 break;\r
871\r
dc7b4a5c 872 case FRAMEWORK_EFI_IFR_END_ONE_OF_OP:\r
5d01b0f7 873 //\r
874 // At this point lets make sure that the data value in the NVRAM matches one of the options\r
875 //\r
876 if (!GotMatch) {\r
877 *Results = FALSE;\r
878 return EFI_SUCCESS;\r
879 }\r
880 break;\r
881\r
dc7b4a5c 882 case FRAMEWORK_EFI_IFR_CHECKBOX_OP:\r
5d01b0f7 883 //\r
884 // A checkbox is a boolean, so 0 and 1 are valid\r
885 // Remember, QuestionId corresponds to the offset location of the data in the variable\r
886 //\r
dc7b4a5c 887 if (VariableData[((FRAMEWORK_EFI_IFR_CHECKBOX *) &RawData[Index])->QuestionId] > 1) {\r
5d01b0f7 888 *Results = FALSE;\r
889 return EFI_SUCCESS;\r
890 }\r
891 break;\r
892\r
dc7b4a5c 893 case FRAMEWORK_EFI_IFR_NUMERIC_OP:\r
894 if ((VariableData[((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->QuestionId] < ((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->Minimum) ||\r
895 (VariableData[((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->QuestionId] > ((FRAMEWORK_EFI_IFR_NUMERIC *)&RawData[Index])->Maximum)) {\r
5d01b0f7 896 *Results = FALSE;\r
897 return EFI_SUCCESS;\r
898 }\r
899 break;\r
900\r
901 }\r
902\r
903 Index = RawData[Index + 1] + Index;\r
904 }\r
905\r
906 //\r
907 // Free our temporary repository of form data\r
908 //\r
909 gBS->FreePool (OldData);\r
910 gBS->FreePool (VariableData);\r
911\r
912 return EFI_SUCCESS;\r
913}\r
914\r
dc7b4a5c 915\r