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