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