]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Universal/UserInterface/SetupBrowser/Dxe/ProcessOptions.c
Fix the TPL issue
[mirror_edk2.git] / EdkModulePkg / Universal / UserInterface / SetupBrowser / Dxe / ProcessOptions.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 ProcessOptions.c
15
16 Abstract:
17
18 Implementation for handling the User Interface option processing.
19
20 Revision History
21
22 --*/
23
24 #include "Setup.h"
25 #include "Ui.h"
26
27 EFI_STATUS
28 ExtractRequestedNvMap (
29 IN EFI_FILE_FORM_TAGS *FileFormTags,
30 IN UINT16 VariableId,
31 OUT EFI_VARIABLE_DEFINITION **VariableDefinition
32 )
33 {
34 *VariableDefinition = FileFormTags->VariableDefinitions;
35
36 //
37 // Extract the data from the NV variable - consumer will free the buffer.
38 //
39 for (; *VariableDefinition != NULL; *VariableDefinition = (*VariableDefinition)->Next) {
40 //
41 // If there is a variable with this ID return with EFI_SUCCESS
42 //
43 if (!CompareMem (&(*VariableDefinition)->VariableId, &VariableId, sizeof (UINT16))) {
44 return EFI_SUCCESS;
45 }
46 }
47
48 return EFI_NOT_FOUND;
49 }
50
51 EFI_STATUS
52 ExtractNvValue (
53 IN EFI_FILE_FORM_TAGS *FileFormTags,
54 IN UINT16 VariableId,
55 IN UINT16 VariableSize,
56 IN UINT16 OffsetValue,
57 OUT VOID **Buffer
58 )
59 {
60 EFI_STATUS Status;
61 EFI_VARIABLE_DEFINITION *VariableDefinition;
62
63 Status = ExtractRequestedNvMap (FileFormTags, VariableId, &VariableDefinition);
64
65 if (!EFI_ERROR (Status)) {
66 //
67 // Allocate sufficient space for the data and copy it into the outgoing buffer
68 //
69 if (VariableSize != 0) {
70 *Buffer = AllocateZeroPool (VariableSize);
71 ASSERT (*Buffer != NULL);
72 CopyMem (*Buffer, &VariableDefinition->NvRamMap[OffsetValue], VariableSize);
73 }
74 return EFI_SUCCESS;
75 }
76
77 return Status;
78 }
79
80 STATIC
81 VOID
82 AdjustNvMap (
83 IN EFI_FILE_FORM_TAGS *FileFormTags,
84 IN UI_MENU_OPTION *MenuOption
85 )
86 {
87 CHAR8 *NvRamMap;
88 UINTN SizeRequired;
89 UINTN Index;
90 UINTN CachedStart;
91 EFI_VARIABLE_DEFINITION *VariableDefinition;
92
93 CachedStart = 0;
94
95 SizeRequired = MenuOption->ThisTag->StorageStart + MenuOption->ThisTag->StorageWidth;
96
97 ExtractRequestedNvMap (FileFormTags, MenuOption->Tags->VariableNumber, &VariableDefinition);
98
99 //
100 // We arrived here because the current NvRamMap is too small for the new op-code to store things and
101 // we need to adjust the buffer to support this.
102 //
103 NvRamMap = AllocateZeroPool (SizeRequired + 1);
104 ASSERT (NvRamMap != NULL);
105
106 //
107 // Copy current NvRamMap to the new NvRamMap
108 //
109 CopyMem (NvRamMap, VariableDefinition->NvRamMap, VariableDefinition->VariableFakeSize);
110
111 //
112 // Remember, the only time we come here is because we are in the NVPlus section of the NvRamMap
113 //
114 for (Index = MenuOption->TagIndex;
115 (MenuOption->Tags[Index].Operand != EFI_IFR_END_FORM_OP) && (MenuOption->Tags[Index].Operand != EFI_IFR_END_ONE_OF_OP);
116 Index++
117 ) {
118
119 switch (MenuOption->Tags[Index].Operand) {
120 case EFI_IFR_ORDERED_LIST_OP:
121 case EFI_IFR_ONE_OF_OP:
122 CachedStart = MenuOption->Tags[Index].StorageStart;
123 break;
124
125 case EFI_IFR_ONE_OF_OPTION_OP:
126 if (MenuOption->Tags[Index].Flags & EFI_IFR_FLAG_DEFAULT) {
127 CopyMem (&NvRamMap[CachedStart], &MenuOption->Tags[Index].Value, 2);
128 }
129 break;
130
131 case EFI_IFR_CHECKBOX_OP:
132 CopyMem (&NvRamMap[MenuOption->Tags[Index].StorageStart], &MenuOption->Tags[Index].Flags, 1);
133 break;
134
135 case EFI_IFR_NUMERIC_OP:
136 case EFI_IFR_DATE_OP:
137 case EFI_IFR_TIME_OP:
138 case EFI_IFR_STRING_OP:
139 case EFI_IFR_PASSWORD_OP:
140 CopyMem (
141 &NvRamMap[MenuOption->Tags[Index].StorageStart],
142 &MenuOption->Tags[Index].Value,
143 MenuOption->Tags[Index].StorageWidth
144 );
145 break;
146
147 }
148 }
149
150 gBS->FreePool (VariableDefinition->NvRamMap);
151 VariableDefinition->NvRamMap = NvRamMap;
152 VariableDefinition->VariableFakeSize = (UINT16) SizeRequired;
153 }
154
155 EFI_STATUS
156 ProcessOptions (
157 IN UI_MENU_OPTION *MenuOption,
158 IN BOOLEAN Selected,
159 IN EFI_FILE_FORM_TAGS *FileFormTagsHead,
160 IN EFI_IFR_DATA_ARRAY *PageData,
161 OUT CHAR16 **OptionString
162 )
163 {
164 EFI_STATUS Status;
165 CHAR16 *StringPtr;
166 UINTN Index;
167 UINTN CachedIndex;
168 EFI_FILE_FORM_TAGS *FileFormTags;
169 EFI_TAG *Tag;
170 CHAR16 FormattedNumber[6];
171 UINT16 Number;
172 UINT16 Value;
173 UINT16 *ValueArray;
174 UINT16 *NvRamMap;
175 CHAR8 *TmpNvRamMap;
176 UINTN Default;
177 UINTN StringCount;
178 CHAR16 Character[2];
179 UINTN Count;
180 EFI_TIME Time;
181 EFI_FORM_CALLBACK_PROTOCOL *FormCallback;
182 STRING_REF PopUp;
183 CHAR16 NullCharacter;
184 EFI_INPUT_KEY Key;
185 EFI_VARIABLE_DEFINITION *VariableDefinition;
186 BOOLEAN OrderedList;
187 BOOLEAN Initialized;
188 UINT16 KeyValue;
189 BOOLEAN Skip;
190
191 FileFormTags = FileFormTagsHead;
192
193 for (Index = 0; Index < MenuOption->IfrNumber; Index++) {
194 FileFormTags = FileFormTags->NextFile;
195 }
196
197 OrderedList = FALSE;
198 Initialized = FALSE;
199 ValueArray = NULL;
200 VariableDefinition = NULL;
201 Skip = FALSE;
202
203 ZeroMem (&Time, sizeof (EFI_TIME));
204
205 StringPtr = (CHAR16 *) L"\0";
206 Tag = MenuOption->ThisTag;
207 ExtractRequestedNvMap (FileFormTags, Tag->VariableNumber, &VariableDefinition);
208
209 if (Tag->StorageStart > VariableDefinition->VariableSize) {
210 NvRamMap = (UINT16 *) &VariableDefinition->FakeNvRamMap[Tag->StorageStart];
211 } else {
212 NvRamMap = (UINT16 *) &VariableDefinition->NvRamMap[Tag->StorageStart];
213 }
214
215 StringCount = 0;
216 Character[1] = 0;
217 Count = 0;
218 Default = 0;
219 NullCharacter = CHAR_NULL;
220 FormCallback = NULL;
221
222 if (MenuOption->ThisTag->Operand == EFI_IFR_ORDERED_LIST_OP) {
223 OrderedList = TRUE;
224 if (((UINT8 *) NvRamMap)[0] != 0x00) {
225 Initialized = TRUE;
226 }
227 }
228
229 ZeroMem (FormattedNumber, 12);
230
231 Status = gBS->HandleProtocol (
232 (VOID *) (UINTN) FileFormTags->FormTags.Tags[0].CallbackHandle,
233 &gEfiFormCallbackProtocolGuid,
234 (VOID **) &FormCallback
235 );
236
237 if (*OptionString != NULL) {
238 gBS->FreePool (*OptionString);
239 *OptionString = NULL;
240 }
241
242 switch (Tag->Operand) {
243
244 case EFI_IFR_ORDERED_LIST_OP:
245 case EFI_IFR_ONE_OF_OP:
246 //
247 // If the op-code we are looking at is larger than the latest created NvMap - we likely encountered a dynamically
248 // created entry which has an expanded NvMap requirement. We won't save this information - but we need to adjust
249 // the NvMap so that we can properly display the information
250 //
251 if ((UINTN) (Tag->StorageStart + Tag->StorageWidth) > VariableDefinition->VariableFakeSize) {
252 AdjustNvMap (FileFormTags, MenuOption);
253 NvRamMap = (UINT16 *) &VariableDefinition->NvRamMap[Tag->StorageStart];
254 }
255
256 CachedIndex = MenuOption->TagIndex;
257
258 //
259 // search for EFI_IFR_ONE_OF_OPTION_OP until you hit the EFI_IFR_END_ONE_OF_OP,
260 // each of the .Text in the options are going to be what gets displayed. Break each into 26 char chunks
261 // when hit right/left arrow allows for selection - then repopulate Tag[TagIndex] with the choice
262 //
263 for (Index = MenuOption->TagIndex; MenuOption->Tags[Index].Operand != EFI_IFR_END_ONE_OF_OP; Index++) {
264 //
265 // We found an option - which assumedly has a string. We will eventually have to support
266 // wrapping of strings. For now, let's pretend they don't wrap and code that up.
267 //
268 // Count how many strings there are
269 //
270 if (MenuOption->Tags[Index].Operand == EFI_IFR_ONE_OF_OPTION_OP) {
271 //
272 // If one of the options for the one-of has an interactive flag, back-define the oneof to have one too
273 //
274 if (MenuOption->Tags[Index].Flags & EFI_IFR_FLAG_INTERACTIVE) {
275 MenuOption->Tags[CachedIndex].Flags = (UINT8) (MenuOption->Tags[CachedIndex].Flags | EFI_IFR_FLAG_INTERACTIVE);
276 }
277
278 StringCount++;
279 }
280 }
281 //
282 // We now know how many strings we will have, so we can allocate the
283 // space required for the array or strings.
284 //
285 *OptionString = AllocateZeroPool (StringCount * (gOptionBlockWidth + 1) * 2 * gScreenDimensions.BottomRow);
286 ASSERT (*OptionString);
287
288 //
289 // Add left delimeter to string
290 //
291 *OptionString[0] = LEFT_ONEOF_DELIMITER;
292
293 //
294 // Retrieve the current OneOf value
295 //
296 if (Selected) {
297 //
298 // Auto selection from list
299 //
300 Value = 0;
301 //
302 // Copy current setting to the seed Value
303 //
304 if (Tag->Operand == EFI_IFR_ORDERED_LIST_OP) {
305 ValueArray = AllocateZeroPool (MenuOption->ThisTag->StorageWidth);
306 ASSERT (ValueArray != NULL);
307 CopyMem (ValueArray, NvRamMap, MenuOption->ThisTag->StorageWidth);
308 } else {
309 CopyMem (&Value, NvRamMap, MenuOption->ThisTag->StorageWidth);
310 CopyMem (gPreviousValue, NvRamMap, MenuOption->ThisTag->StorageWidth);
311 }
312
313 Number = Value;
314 if (Tag->Operand == EFI_IFR_ORDERED_LIST_OP) {
315 Status = GetSelectionInputPopUp (MenuOption, Tag, MenuOption->ThisTag->StorageWidth, ValueArray, &KeyValue);
316 } else {
317 Status = GetSelectionInputPopUp (MenuOption, Tag, 1, &Value, &KeyValue);
318 }
319
320 if (!EFI_ERROR (Status)) {
321 if (Tag->Operand == EFI_IFR_ORDERED_LIST_OP) {
322 CopyMem (NvRamMap, ValueArray, MenuOption->ThisTag->StorageWidth);
323 gBS->FreePool (ValueArray);
324 } else {
325 //
326 // Since the value can be one byte long or two bytes long, do a CopyMem based on StorageWidth
327 //
328 CopyMem (NvRamMap, &Value, Tag->StorageWidth);
329 MenuOption->ThisTag->Key = KeyValue;
330 }
331 //
332 // If a late check is required save off the information. This is used when consistency checks
333 // are required, but certain values might be bound by an impossible consistency check such as
334 // if two questions are bound by consistency checks and each only has two possible choices, there
335 // would be no way for a user to switch the values. Thus we require late checking.
336 //
337 if (Tag->Flags & EFI_IFR_FLAG_LATE_CHECK) {
338 CopyMem (&Tag->OldValue, &Value, Tag->StorageWidth);
339 } else {
340 //
341 // In theory, passing the value and the Id are sufficient to determine what needs
342 // to be done. The Id is the key to look for the entry needed in the Inconsistency
343 // database. That will yields operand and ID data - and since the ID's correspond
344 // to the NV storage, we can determine the values for other IDs there.
345 //
346 if (ValueIsNotValid (TRUE, 0, Tag, FileFormTags, &PopUp)) {
347 if (PopUp == 0x0000) {
348 //
349 // Restore Old Value
350 //
351 if (!Tag->Suppress && !Tag->GrayOut) {
352 CopyMem (NvRamMap, &Number, MenuOption->ThisTag->StorageWidth);
353 }
354 break;
355 }
356
357 StringPtr = GetToken (PopUp, MenuOption->Handle);
358
359 CreatePopUp (GetStringWidth (StringPtr) / 2, 3, &NullCharacter, StringPtr, &NullCharacter);
360
361 do {
362 Status = WaitForKeyStroke (&Key);
363
364 switch (Key.UnicodeChar) {
365
366 case CHAR_CARRIAGE_RETURN:
367 //
368 // Since the value can be one byte long or two bytes long, do a CopyMem based on StorageWidth
369 //
370 CopyMem (NvRamMap, &Number, MenuOption->ThisTag->StorageWidth);
371 gBS->FreePool (StringPtr);
372 break;
373
374 default:
375 break;
376 }
377 } while (Key.UnicodeChar != CHAR_CARRIAGE_RETURN);
378 }
379 }
380
381 UpdateStatusBar (NV_UPDATE_REQUIRED, Tag->Flags, TRUE);
382 } else {
383 if (Tag->Operand == EFI_IFR_ORDERED_LIST_OP) {
384 gBS->FreePool (ValueArray);
385 }
386
387 return EFI_SUCCESS;
388 }
389 } else {
390 for (Index = MenuOption->TagIndex; MenuOption->Tags[Index].Operand != EFI_IFR_END_ONE_OF_OP; Index++) {
391 //
392 // We found an option - which assumedly has a string. We will eventually have to support
393 // wrapping of strings. For now, let's pretend they don't wrap and code that up.
394 //
395 if (MenuOption->Tags[Index].Operand == EFI_IFR_ONE_OF_OPTION_OP) {
396 if (OrderedList) {
397 if (!Initialized) {
398 //
399 // If the first entry is invalid, then the "default" settings are based on what is reflected
400 // in the order of the op-codes
401 //
402 ((UINT8 *) NvRamMap)[Index - MenuOption->TagIndex - 1] = (UINT8) MenuOption->Tags[Index].Value;
403 }
404 //
405 // Only display 3 lines of stuff at most
406 //
407 if ((Index - MenuOption->TagIndex) > ORDERED_LIST_SIZE) {
408 break;
409 }
410
411 if (((Index - MenuOption->TagIndex) != 1) && !Skip) {
412 Character[0] = LEFT_ONEOF_DELIMITER;
413 NewStrCat (OptionString[0], Character);
414 }
415
416 MenuOption->ThisTag->NumberOfLines = (UINT16) (Index - MenuOption->TagIndex);
417 if (!Initialized) {
418 StringPtr = GetToken (MenuOption->Tags[Index].Text, MenuOption->Handle);
419 } else {
420 for (Value = (UINT16) (MenuOption->TagIndex + 1);
421 MenuOption->Tags[Value].Operand != EFI_IFR_END_ONE_OF_OP;
422 Value++
423 ) {
424 if (MenuOption->Tags[Value].Value == ((UINT8 *) NvRamMap)[Index - MenuOption->TagIndex - 1]) {
425 StringPtr = GetToken (MenuOption->Tags[Value].Text, MenuOption->Handle);
426 break;
427 }
428 }
429
430 if (MenuOption->Tags[Value].Operand == EFI_IFR_END_ONE_OF_OP) {
431 Skip = TRUE;
432 continue;
433 }
434 }
435
436 Skip = FALSE;
437 NewStrCat (OptionString[0], StringPtr);
438 Character[0] = RIGHT_ONEOF_DELIMITER;
439 NewStrCat (OptionString[0], Character);
440 Character[0] = CHAR_CARRIAGE_RETURN;
441 NewStrCat (OptionString[0], Character);
442
443 //
444 // Remove Buffer allocated for StringPtr after it has been used.
445 //
446 gBS->FreePool (StringPtr);
447 } else {
448 //
449 // The option value is the same as what is stored in NV store. Print this.
450 //
451 if (!CompareMem (&(MenuOption->Tags[Index].Value), NvRamMap, MenuOption->ThisTag->StorageWidth)) {
452 StringPtr = GetToken (MenuOption->Tags[Index].Text, MenuOption->Handle);
453 NewStrCat (OptionString[0], StringPtr);
454 Character[0] = RIGHT_ONEOF_DELIMITER;
455 NewStrCat (OptionString[0], Character);
456 //
457 // Remove Buffer allocated for StringPtr after it has been used.
458 //
459 gBS->FreePool (StringPtr);
460 Default = 0;
461 break;
462 }
463
464 if ((MenuOption->Tags[Index].Flags & EFI_IFR_FLAG_DEFAULT) == 1) {
465 Default = MenuOption->Tags[Index].Text;
466 Value = MenuOption->Tags[Index].Value;
467 };
468 }
469 }
470 }
471 //
472 // We didn't find a value that matched a setting in the NVRAM Map - display default - set default
473 //
474 if (Default != 0) {
475 //
476 // Since the value can be one byte long or two bytes long, do a CopyMem based on StorageWidth
477 //
478 CopyMem (NvRamMap, &Value, MenuOption->ThisTag->StorageWidth);
479
480 StringPtr = GetToken ((UINT16) Default, MenuOption->Handle);
481 NewStrCat (OptionString[0], StringPtr);
482 Character[0] = RIGHT_ONEOF_DELIMITER;
483 NewStrCat (OptionString[0], Character);
484 //
485 // Remove Buffer allocated for StringPtr after it has been used.
486 //
487 gBS->FreePool (StringPtr);
488 }
489 }
490 break;
491
492 case EFI_IFR_CHECKBOX_OP:
493 //
494 // If the op-code we are looking at is larger than the latest created NvMap - we likely encountered a dynamically
495 // created entry which has an expanded NvMap requirement. We won't save this information - but we need to adjust
496 // the NvMap so that we can properly display the information
497 //
498 if ((UINTN) (Tag->StorageStart + Tag->StorageWidth) > VariableDefinition->VariableFakeSize) {
499 AdjustNvMap (FileFormTags, MenuOption);
500 NvRamMap = (UINT16 *) &VariableDefinition->NvRamMap[Tag->StorageStart];
501 }
502
503 Default = Tag->Flags & 1;
504 //
505 // If hit spacebar, set or unset Tag[TagIndex].Flags based on it's previous value - BOOLEAN
506 //
507 *OptionString = AllocateZeroPool ((gOptionBlockWidth + 1) * 2 * gScreenDimensions.BottomRow);
508 ASSERT (*OptionString);
509
510 //
511 // Since Checkboxes are BOOLEAN values, bit 0 of the Flags bit defines the default option, therefore, if
512 // the default option (only one option for checkboxes) is on, then the default value is on. Tag.Default is not
513 // an active field for Checkboxes.
514 //
515 StrnCpy (OptionString[0], (CHAR16 *) LEFT_CHECKBOX_DELIMITER, 1);
516
517 //
518 // Since this is a BOOLEAN operation, flip bit 0 upon selection
519 //
520 if (Selected) {
521 Tag->Value = (UINT16) (Tag->Value ^ 1);
522 *(UINT8 *) NvRamMap = (UINT8) (Tag->Value & 1);
523 UpdateStatusBar (NV_UPDATE_REQUIRED, Tag->Flags, TRUE);
524 }
525
526 if ((*(UINT8 *) NvRamMap & 1) == 0x01) {
527 NewStrCat (OptionString[0], (CHAR16 *) CHECK_ON);
528 //
529 // If someone reset default variables - we may need to reload from our NvMapping....
530 //
531 Tag->Value = *(UINT8 *) NvRamMap;
532 } else {
533 //
534 // If someone reset default variables - we may need to reload from our NvMapping....
535 //
536 NewStrCat (OptionString[0], (CHAR16 *) CHECK_OFF);
537 Tag->Value = *(UINT8 *) NvRamMap;
538 }
539
540 NewStrCat (OptionString[0], (CHAR16 *) RIGHT_CHECKBOX_DELIMITER);
541 NewStrCat (OptionString[0], StringPtr);
542 break;
543
544 case EFI_IFR_NUMERIC_OP:
545 //
546 // If the op-code we are looking at is larger than the latest created NvMap - we likely encountered a dynamically
547 // created entry which has an expanded NvMap requirement. We won't save this information - but we need to adjust
548 // the NvMap so that we can properly display the information
549 //
550 if ((UINTN) (Tag->StorageStart + Tag->StorageWidth) > VariableDefinition->VariableFakeSize) {
551 AdjustNvMap (FileFormTags, MenuOption);
552 NvRamMap = (UINT16 *) &VariableDefinition->NvRamMap[Tag->StorageStart];
553 }
554
555 *OptionString = AllocateZeroPool ((gOptionBlockWidth + 1) * 2 * gScreenDimensions.BottomRow);
556 ASSERT (*OptionString);
557
558 //
559 // Add left delimeter to string
560 //
561 *OptionString[0] = LEFT_NUMERIC_DELIMITER;
562
563 //
564 // Retrieve the current numeric value
565 //
566 if (Selected) {
567 //
568 // Go ask for input
569 //
570 if (Tag->Step == 0) {
571 //
572 // Manual Input
573 //
574 Status = GetNumericInput (MenuOption, FileFormTagsHead, TRUE, Tag, REGULAR_NUMERIC, &Number);
575 if (!EFI_ERROR (Status)) {
576 CopyMem (gPreviousValue, NvRamMap, MenuOption->ThisTag->StorageWidth);
577 UpdateStatusBar (NV_UPDATE_REQUIRED, Tag->Flags, TRUE);
578
579 //
580 // Since the value can be one byte long or two bytes long, do a CopyMem based on StorageWidth
581 //
582 CopyMem (NvRamMap, &Number, MenuOption->ThisTag->StorageWidth);
583 } else {
584 return EFI_SUCCESS;
585 }
586 } else {
587 //
588 // Auto selection from list
589 //
590 if ((((Tag->StorageWidth == 1) && (UINT8) (*NvRamMap) > Tag->Maximum) || ((UINT8) (*NvRamMap) < Tag->Minimum)) ||
591 (((Tag->StorageWidth == 2) && *NvRamMap > Tag->Maximum) || (*NvRamMap < Tag->Minimum))
592 ) {
593 //
594 // Seed Number with valid value if currently invalid
595 //
596 Number = Tag->Default;
597 } else {
598 if (Tag->StorageWidth == 1) {
599 Number = (UINT8) (*NvRamMap);
600 } else {
601 Number = *NvRamMap;
602 }
603 }
604
605 Status = GetNumericInput (MenuOption, FileFormTagsHead, FALSE, Tag, REGULAR_NUMERIC, &Number);
606 if (!EFI_ERROR (Status)) {
607 CopyMem (gPreviousValue, NvRamMap, MenuOption->ThisTag->StorageWidth);
608 UpdateStatusBar (NV_UPDATE_REQUIRED, Tag->Flags, TRUE);
609
610 //
611 // Since the value can be one byte long or two bytes long, do a CopyMem based on StorageWidth
612 //
613 CopyMem (NvRamMap, &Number, MenuOption->ThisTag->StorageWidth);
614 } else {
615 return EFI_SUCCESS;
616 }
617 }
618 } else {
619 if (((Tag->StorageWidth == 1) && (UINT8) (*NvRamMap) <= Tag->Maximum && (UINT8) (*NvRamMap) >= Tag->Minimum) ||
620 ((Tag->StorageWidth == 2) && *NvRamMap <= Tag->Maximum && *NvRamMap >= Tag->Minimum)
621 ) {
622 if (Tag->StorageWidth == 1) {
623 Number = (UINT8) (*NvRamMap);
624 } else {
625 Number = *NvRamMap;
626 }
627 UnicodeValueToString (
628 FormattedNumber,
629 FALSE,
630 (UINTN) Number,
631 (sizeof (FormattedNumber) / sizeof (FormattedNumber[0]))
632 );
633 Number = (UINT16) GetStringWidth (FormattedNumber);
634 StrnCpy (OptionString[0] + 1, FormattedNumber, Number);
635 } else {
636 //
637 // If *NvRamMap isn't within parameters, set it to within parameters
638 //
639 //
640 // Since the value can be one byte long or two bytes long, do a CopyMem based on StorageWidth
641 //
642 CopyMem (NvRamMap, &Tag->Default, MenuOption->ThisTag->StorageWidth);
643 Number = Tag->Default;
644
645 UnicodeValueToString (
646 FormattedNumber,
647 FALSE,
648 (UINTN) Number,
649 (sizeof (FormattedNumber) / sizeof (FormattedNumber[0]))
650 );
651 Number = (UINT16) GetStringWidth (FormattedNumber);
652 StrnCpy (OptionString[0] + 1, FormattedNumber, Number);
653 }
654
655 *(OptionString[0] + Number / 2) = RIGHT_NUMERIC_DELIMITER;
656 NewStrCat (OptionString[0] + (Number / 2) + 1, StringPtr);
657 }
658 break;
659
660 case EFI_IFR_DATE_OP:
661 //
662 // If the op-code we are looking at is larger than the latest created NvMap - we likely encountered a dynamically
663 // created entry which has an expanded NvMap requirement. We won't save this information - but we need to adjust
664 // the NvMap so that we can properly display the information
665 //
666 if ((UINTN) (Tag->StorageStart + Tag->StorageWidth) > VariableDefinition->VariableFakeSize) {
667 AdjustNvMap (FileFormTags, MenuOption);
668 NvRamMap = (UINT16 *) &VariableDefinition->NvRamMap[Tag->StorageStart];
669 }
670
671 Status = gRT->GetTime (&Time, NULL);
672 if (EFI_ERROR (Status)) {
673 return EFI_SUCCESS;
674 }
675 //
676 // This for loop advances Index till it points immediately after a date entry. We can then
677 // subtract MenuOption->TagIndex from Index and find out relative to the start of the Date
678 // structure which field we were in. For instance, if TagIndex was 52, and we advanced Index
679 // to 53 and found it to no longer point to a date operand, we were pointing to the last of 3
680 // date operands.
681 //
682 //
683 // This has BUGBUG potential....fix this - if someone wants to ask two DATE questions in a row.....code
684 // against such silliness.
685 //
686 // Also, we want to internationalize the order of the date information. We need to code for it as well.
687 //
688 for (Index = MenuOption->TagIndex; MenuOption->Tags[Index].Operand == EFI_IFR_DATE_OP; Index++)
689 ;
690
691 //
692 // Count 0 = We entered on the first Date operand
693 // Count 1 = We entered on the second Date operand
694 // Count 2 = We entered on the third Date operand
695 //
696 Count = 3 - (Index - MenuOption->TagIndex);
697 if (Count > 2) {
698 return EFI_SUCCESS;
699 }
700 //
701 // This is similar to numerics, except for the following:
702 // We will under normal circumstances get 3 consecutive calls
703 // to process this opcodes data.
704 //
705 *OptionString = AllocateZeroPool ((gOptionBlockWidth + 1) * 2 * gScreenDimensions.BottomRow);
706 ASSERT (*OptionString);
707
708 switch (Count) {
709 case 0:
710 if (Selected) {
711 Number = (UINT16) Time.Month;
712
713 if (Tag->Step == 0) {
714 MenuOption->OptCol++;
715 Status = GetNumericInput (MenuOption, FileFormTagsHead, TRUE, Tag, DATE_NUMERIC, &Number);
716 } else {
717 //
718 // Seed value with current setting
719 //
720 Tag->Value = (UINT16) Time.Month;
721 Status = GetNumericInput (MenuOption, FileFormTagsHead, FALSE, Tag, DATE_NUMERIC, &Number);
722 }
723
724 if (!EFI_ERROR (Status)) {
725 Time.Month = (UINT8) Number;
726 gRT->SetTime (&Time);
727 }
728 }
729
730 VariableDefinition->FakeNvRamMap[Tag->Id] = Time.Month;
731 *OptionString[0] = LEFT_NUMERIC_DELIMITER;
732
733 UnicodeValueToString (
734 FormattedNumber,
735 FALSE,
736 (UINTN) Time.Month,
737 (sizeof (FormattedNumber) / sizeof (FormattedNumber[0]))
738 );
739 Number = (UINT16) GetStringWidth (FormattedNumber);
740
741 if (Number == 4) {
742 FormattedNumber[2] = FormattedNumber[1];
743 FormattedNumber[1] = FormattedNumber[0];
744 FormattedNumber[0] = L'0';
745 Number = 6;
746 }
747
748 StrnCpy (OptionString[0] + 1, FormattedNumber, Number);
749 *(OptionString[0] + Number / 2) = DATE_SEPARATOR;
750 StrCat (OptionString[0] + (Number / 2) + 1, StringPtr);
751 break;
752
753 case 1:
754 if (Selected) {
755 Number = (UINT16) Time.Day;
756
757 if (Tag->Step == 0) {
758 Status = GetNumericInput (MenuOption, FileFormTagsHead, TRUE, Tag, DATE_NUMERIC, &Number);
759 } else {
760 //
761 // Seed value with current setting
762 //
763 Tag->Value = (UINT16) Time.Day;
764 Status = GetNumericInput (MenuOption, FileFormTagsHead, FALSE, Tag, DATE_NUMERIC, &Number);
765 }
766
767 if (!EFI_ERROR (Status)) {
768 Time.Day = (UINT8) Number;
769 gRT->SetTime (&Time);
770 }
771 }
772
773 VariableDefinition->FakeNvRamMap[Tag->Id] = Time.Day;
774 SetUnicodeMem (OptionString[0], 4, L' ');
775
776 UnicodeValueToString (
777 FormattedNumber,
778 FALSE,
779 (UINTN) Time.Day,
780 (sizeof (FormattedNumber) / sizeof (FormattedNumber[0]))
781 );
782 Number = (UINT16) GetStringWidth (FormattedNumber);
783 if (Number == 4) {
784 FormattedNumber[2] = FormattedNumber[1];
785 FormattedNumber[1] = FormattedNumber[0];
786 FormattedNumber[0] = L'0';
787 Number = 6;
788 }
789
790 StrnCpy (OptionString[0] + 4, FormattedNumber, Number);
791 *(OptionString[0] + Number / 2 + 3) = DATE_SEPARATOR;
792 StrCat (OptionString[0] + (Number / 2) + 4, StringPtr);
793 break;
794
795 case 2:
796 if (Selected) {
797 Number = (UINT16) Time.Year;
798
799 if (Tag->Step == 0) {
800 Status = GetNumericInput (MenuOption, FileFormTagsHead, TRUE, Tag, DATE_NUMERIC, &Number);
801 } else {
802 //
803 // Seed value with current setting
804 //
805 Status = GetNumericInput (MenuOption, FileFormTagsHead, FALSE, Tag, DATE_NUMERIC, &Number);
806 }
807
808 if (!EFI_ERROR (Status)) {
809 Time.Year = (UINT16) Number;
810 gRT->SetTime (&Time);
811 }
812 }
813
814 Tag->Value = (UINT16) Time.Year;
815 VariableDefinition->FakeNvRamMap[Tag->Id] = (UINT8) Tag->Value;
816 VariableDefinition->FakeNvRamMap[Tag->Id + 1] = (UINT8) (Tag->Value >> 8);
817 SetUnicodeMem (OptionString[0], 7, L' ');
818 UnicodeValueToString (
819 FormattedNumber,
820 FALSE,
821 (UINTN) Time.Year,
822 (sizeof (FormattedNumber) / sizeof (FormattedNumber[0]))
823 );
824 Number = (UINT16) GetStringWidth (FormattedNumber);
825 StrnCpy (OptionString[0] + 7, FormattedNumber, Number);
826 *(OptionString[0] + Number / 2 + 6) = RIGHT_NUMERIC_DELIMITER;
827 StrCat (OptionString[0] + (Number / 2) + 7, StringPtr);
828 break;
829 }
830
831 break;
832
833 //
834 // BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG
835 // We need to add code to support the NVRam storage version of Date - this is the 1% case where someone
836 // might want to set an alarm and actually preserve the data in NVRam so a driver can pick up the instruction
837 // BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG
838 //
839 case EFI_IFR_TIME_OP:
840 //
841 // If the op-code we are looking at is larger than the latest created NvMap - we likely encountered a dynamically
842 // created entry which has an expanded NvMap requirement. We won't save this information - but we need to adjust
843 // the NvMap so that we can properly display the information
844 //
845 if ((UINTN) (Tag->StorageStart + Tag->StorageWidth) > VariableDefinition->VariableFakeSize) {
846 AdjustNvMap (FileFormTags, MenuOption);
847 NvRamMap = (UINT16 *) &VariableDefinition->NvRamMap[Tag->StorageStart];
848 }
849
850 Status = gRT->GetTime (&Time, NULL);
851 if (EFI_ERROR (Status)) {
852 return EFI_SUCCESS;
853 }
854 //
855 // This is similar to numerics, except for the following:
856 // We will under normal circumstances get 3 consecutive calls
857 // to process this opcodes data.
858 //
859 *OptionString = AllocateZeroPool ((gOptionBlockWidth + 1) * 2 * gScreenDimensions.BottomRow);
860 ASSERT (*OptionString);
861
862 //
863 // This for loop advances Index till it points immediately after a date entry. We can then
864 // subtract MenuOption->TagIndex from Index and find out relative to the start of the Date
865 // structure which field we were in. For instance, if TagIndex was 52, and we advanced Index
866 // to 53 and found it to no longer point to a date operand, we were pointing to the last of 3
867 // date operands.
868 //
869 for (Index = MenuOption->TagIndex; MenuOption->Tags[Index].Operand == EFI_IFR_TIME_OP; Index++)
870 ;
871 //
872 // Count 0 = We entered on the first Date operand
873 // Count 1 = We entered on the second Date operand
874 // Count 2 = We entered on the third Date operand
875 //
876 Count = 3 - (Index - MenuOption->TagIndex);
877 if (Count > 2) {
878 return EFI_SUCCESS;
879 }
880
881 switch (Count) {
882 case 0:
883 Number = Time.Hour;
884 break;
885
886 case 1:
887 Number = Time.Minute;
888 break;
889
890 case 2:
891 Number = Time.Second;
892 }
893 //
894 // Retrieve the current numeric value
895 //
896 if (Selected) {
897 //
898 // Go ask for input
899 //
900 if (Tag->Step == 0) {
901 //
902 // Manual Input
903 //
904 Status = GetNumericInput (MenuOption, FileFormTagsHead, TRUE, Tag, TIME_NUMERIC, &Number);
905 if (!EFI_ERROR (Status)) {
906 *NvRamMap = Number;
907 Time.Nanosecond = 0;
908 gRT->SetTime (&Time);
909 } else {
910 return EFI_SUCCESS;
911 }
912 } else {
913 //
914 // Auto selection from list
915 //
916 Status = GetNumericInput (MenuOption, FileFormTagsHead, FALSE, Tag, TIME_NUMERIC, &Number);
917 if (!EFI_ERROR (Status)) {
918 *NvRamMap = Number;
919 } else {
920 return EFI_SUCCESS;
921 }
922 }
923
924 switch (Count) {
925 case 0:
926 Time.Hour = (UINT8) Number;
927 break;
928
929 case 1:
930 Time.Minute = (UINT8) Number;
931 break;
932
933 case 2:
934 Time.Second = (UINT8) Number;
935 }
936
937 Time.Nanosecond = 0;
938 gRT->SetTime (&Time);
939 } else {
940 switch (Count) {
941 case 0:
942 *OptionString[0] = LEFT_NUMERIC_DELIMITER;
943 UnicodeValueToString (
944 FormattedNumber,
945 FALSE,
946 (UINTN) Time.Hour,
947 (sizeof (FormattedNumber) / sizeof (FormattedNumber[0]))
948 );
949 Number = (UINT16) GetStringWidth (FormattedNumber);
950 if (Number == 4) {
951 FormattedNumber[2] = FormattedNumber[1];
952 FormattedNumber[1] = FormattedNumber[0];
953 FormattedNumber[0] = L'0';
954 Number = 6;
955 }
956
957 StrnCpy (OptionString[0] + 1, FormattedNumber, Number);
958 *(OptionString[0] + Number / 2) = TIME_SEPARATOR;
959 StrCat (OptionString[0] + (Number / 2) + 1, StringPtr);
960 break;
961
962 case 1:
963 SetUnicodeMem (OptionString[0], 4, L' ');
964 UnicodeValueToString (
965 FormattedNumber,
966 FALSE,
967 (UINTN) Time.Minute,
968 (sizeof (FormattedNumber) / sizeof (FormattedNumber[0]))
969 );
970 Number = (UINT16) GetStringWidth (FormattedNumber);
971 if (Number == 4) {
972 FormattedNumber[2] = FormattedNumber[1];
973 FormattedNumber[1] = FormattedNumber[0];
974 FormattedNumber[0] = L'0';
975 Number = 6;
976 }
977
978 StrnCpy (OptionString[0] + 4, FormattedNumber, Number);
979 *(OptionString[0] + Number / 2 + 3) = TIME_SEPARATOR;
980 StrCat (OptionString[0] + (Number / 2) + 4, StringPtr);
981 break;
982
983 case 2:
984 SetUnicodeMem (OptionString[0], 7, L' ');
985 UnicodeValueToString (
986 FormattedNumber,
987 FALSE,
988 (UINTN) Time.Second,
989 (sizeof (FormattedNumber) / sizeof (FormattedNumber[0]))
990 );
991 Number = (UINT16) GetStringWidth (FormattedNumber);
992 if (Number == 4) {
993 FormattedNumber[2] = FormattedNumber[1];
994 FormattedNumber[1] = FormattedNumber[0];
995 FormattedNumber[0] = L'0';
996 Number = 6;
997 }
998
999 StrnCpy (OptionString[0] + 7, FormattedNumber, Number);
1000 *(OptionString[0] + Number / 2 + 6) = RIGHT_NUMERIC_DELIMITER;
1001 StrCat (OptionString[0] + (Number / 2) + 7, StringPtr);
1002 break;
1003 }
1004 //
1005 // BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG
1006 // We need to add code to support the NVRam storage version of Date - this is the 1% case where someone
1007 // might want to set an alarm and actually preserve the data in NVRam so a driver can pick up the instruction
1008 // BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG BUGBUG
1009 //
1010 }
1011 break;
1012
1013 case EFI_IFR_STRING_OP:
1014 //
1015 // If the op-code we are looking at is larger than the latest created NvMap - we likely encountered a dynamically
1016 // created entry which has an expanded NvMap requirement. We won't save this information - but we need to adjust
1017 // the NvMap so that we can properly display the information
1018 //
1019 if ((UINTN) (Tag->StorageStart + Tag->StorageWidth) > VariableDefinition->VariableFakeSize) {
1020 AdjustNvMap (FileFormTags, MenuOption);
1021 NvRamMap = (UINT16 *) &VariableDefinition->NvRamMap[Tag->StorageStart];
1022 }
1023
1024 *OptionString = AllocateZeroPool ((gOptionBlockWidth + 1) * 2 * gScreenDimensions.BottomRow);
1025 ASSERT (*OptionString);
1026
1027 if (Selected) {
1028 StringPtr = AllocateZeroPool (Tag->Maximum);
1029 ASSERT (StringPtr);
1030
1031 Status = ReadString (MenuOption, StringPtr);
1032
1033 if (!EFI_ERROR (Status)) {
1034 CopyMem (gPreviousValue, NvRamMap, MenuOption->ThisTag->StorageWidth);
1035 CopyMem (&VariableDefinition->NvRamMap[Tag->StorageStart], StringPtr, Tag->StorageWidth);
1036
1037 UpdateStatusBar (NV_UPDATE_REQUIRED, Tag->Flags, TRUE);
1038 }
1039
1040 gBS->FreePool (StringPtr);
1041 return Status;
1042 } else {
1043 for (Index = 0; Index < gOptionBlockWidth; Index++) {
1044 if (VariableDefinition->NvRamMap[Tag->StorageStart + (Index * 2)] != 0x0000) {
1045 CopyMem (OptionString[0] + Index, &VariableDefinition->NvRamMap[Tag->StorageStart + (Index * 2)], 2);
1046 } else {
1047 if (Index == 0) {
1048 *(OptionString[0] + Index) = '_';
1049 *(OptionString[0] + 1 + Index) = 0;
1050 }
1051 break;
1052 }
1053 }
1054
1055 return Status;
1056 }
1057
1058 case EFI_IFR_PASSWORD_OP:
1059 //
1060 // If the op-code we are looking at is larger than the latest created NvMap - we likely encountered a dynamically
1061 // created entry which has an expanded NvMap requirement. We won't save this information - but we need to adjust
1062 // the NvMap so that we can properly display the information
1063 //
1064 if ((UINTN) (Tag->StorageStart + Tag->StorageWidth) > VariableDefinition->VariableFakeSize) {
1065 AdjustNvMap (FileFormTags, MenuOption);
1066 NvRamMap = (UINT16 *) &VariableDefinition->NvRamMap[Tag->StorageStart];
1067 }
1068
1069 if (Selected) {
1070 StringPtr = AllocateZeroPool (Tag->Maximum);
1071 ASSERT (StringPtr);
1072
1073 //
1074 // If interactive, read the password and do the appropriate callbacks in that routine.
1075 // Since interactive passwords assume to handle the password data in a separate variable
1076 // storage, we don't need to do more than what is below for password callbacks
1077 //
1078 if (Tag->Flags & EFI_IFR_FLAG_INTERACTIVE) {
1079 MenuOption->Tags[0].CallbackHandle = FileFormTags->FormTags.Tags[0].CallbackHandle;
1080 Status = ReadPassword (MenuOption, TRUE, Tag, PageData, FALSE, FileFormTags, StringPtr);
1081 ZeroMem (StringPtr, Tag->Maximum);
1082
1083 if (EFI_ERROR (Status)) {
1084 if (Status == EFI_NOT_READY) {
1085 gBS->FreePool (StringPtr);
1086 return EFI_SUCCESS;
1087 }
1088 }
1089
1090 Status = ReadPassword (MenuOption, TRUE, Tag, PageData, TRUE, FileFormTags, StringPtr);
1091 gBS->FreePool (StringPtr);
1092 return EFI_SUCCESS;
1093 }
1094
1095 for (Index = 0; Index < Tag->Maximum; Index++) {
1096 if (VariableDefinition->NvRamMap[Tag->StorageStart + Index] != 0x00) {
1097 //
1098 // There is something there! Prompt for password
1099 //
1100 Status = ReadPassword (MenuOption, TRUE, Tag, PageData, FALSE, FileFormTags, StringPtr);
1101 if (EFI_ERROR (Status)) {
1102 gBS->FreePool (StringPtr);
1103 return EFI_SUCCESS;
1104 }
1105
1106 if (Tag->Encoding == 1) {
1107 EncodePassword (StringPtr, (UINT8) Tag->Maximum);
1108 Status = CompareMem (StringPtr, &VariableDefinition->NvRamMap[Tag->StorageStart], Tag->Maximum);
1109 } else {
1110 Status = CompareMem (StringPtr, &VariableDefinition->NvRamMap[Tag->StorageStart], Tag->Maximum);
1111 }
1112
1113 if (Status != 0) {
1114 gBS->FreePool (StringPtr);
1115 return EFI_SUCCESS;
1116 } else {
1117 break;
1118 }
1119 }
1120 }
1121 //
1122 // Clean the string
1123 //
1124 ZeroMem (StringPtr, Tag->Maximum);
1125
1126 //
1127 // No password set! Go ahead and prompt the user for a password.
1128 //
1129 Status = ReadPassword (MenuOption, FALSE, Tag, PageData, FALSE, FileFormTags, StringPtr);
1130
1131 if (EFI_ERROR (Status)) {
1132 //
1133 // User couldn't figure out how to type two identical passwords
1134 //
1135 gBS->FreePool (StringPtr);
1136 return EFI_SUCCESS;
1137 }
1138 //
1139 // Very simple example of how one MIGHT do password encoding
1140 //
1141 if (Tag->Encoding == 1) {
1142 EncodePassword (StringPtr, (UINT8) Tag->Maximum);
1143 }
1144
1145 TmpNvRamMap = AllocatePool (VariableDefinition->VariableSize);
1146 ASSERT (TmpNvRamMap != NULL);
1147
1148 Count = VariableDefinition->VariableSize;
1149
1150 if ((FormCallback != NULL) && (FormCallback->NvRead != NULL)) {
1151 Status = FormCallback->NvRead (
1152 FormCallback,
1153 VariableDefinition->VariableName,
1154 &VariableDefinition->Guid,
1155 NULL,
1156 &Count,
1157 (VOID *) TmpNvRamMap
1158 );
1159 } else {
1160 Status = gRT->GetVariable (
1161 VariableDefinition->VariableName,
1162 &VariableDefinition->Guid,
1163 NULL,
1164 &Count,
1165 (VOID *) TmpNvRamMap
1166 );
1167 }
1168
1169 CopyMem (&VariableDefinition->NvRamMap[Tag->StorageStart], StringPtr, Tag->StorageWidth);
1170 CopyMem (&TmpNvRamMap[Tag->StorageStart], StringPtr, Tag->StorageWidth);
1171
1172 if ((FormCallback != NULL) && (FormCallback->NvWrite != NULL)) {
1173 Status = FormCallback->NvWrite (
1174 FormCallback,
1175 VariableDefinition->VariableName,
1176 &VariableDefinition->Guid,
1177 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
1178 VariableDefinition->VariableSize,
1179 (VOID *) TmpNvRamMap,
1180 &gResetRequired
1181 );
1182 } else {
1183 Status = gRT->SetVariable (
1184 VariableDefinition->VariableName,
1185 &VariableDefinition->Guid,
1186 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
1187 VariableDefinition->VariableSize,
1188 (VOID *) TmpNvRamMap
1189 );
1190 }
1191
1192 gBS->FreePool (TmpNvRamMap);
1193 gBS->FreePool (StringPtr);
1194 break;
1195 }
1196
1197 default:
1198 break;
1199 }
1200
1201 return EFI_SUCCESS;
1202 }
1203
1204 VOID
1205 ProcessHelpString (
1206 IN CHAR16 *StringPtr,
1207 OUT CHAR16 **FormattedString,
1208 IN UINTN RowCount
1209 )
1210 {
1211 UINTN CurrIndex;
1212 UINTN PrevIndex;
1213 UINTN SearchIndex;
1214 UINTN PrevSearchIndex;
1215 UINTN StringCount;
1216 UINTN PageCount;
1217
1218 StringCount = 0;
1219 PrevIndex = 0;
1220 CurrIndex = gHelpBlockWidth - 1;
1221
1222 if (*FormattedString != NULL) {
1223 gBS->FreePool (*FormattedString);
1224 *FormattedString = NULL;
1225 }
1226
1227 for (; CurrIndex > PrevIndex; CurrIndex--) {
1228 //
1229 // In the case where the string ended and a new one is immediately after it
1230 // we need to check for the null-terminator and reset the CurrIndex
1231 //
1232 SearchIndex = CurrIndex;
1233 PrevSearchIndex = PrevIndex;
1234
1235 for (; SearchIndex > PrevSearchIndex; PrevSearchIndex++) {
1236 if ((StringPtr[PrevSearchIndex] == CHAR_NULL) || (StringPtr[PrevSearchIndex] == CHAR_LINEFEED)) {
1237 CurrIndex = PrevSearchIndex;
1238 break;
1239 }
1240
1241 if (StringPtr[PrevSearchIndex] == CHAR_CARRIAGE_RETURN) {
1242 if (StringPtr[PrevSearchIndex + 1] == CHAR_LINEFEED) {
1243 //
1244 // Found a "\n",advance to the next new line.
1245 //
1246 CurrIndex = PrevSearchIndex + 1;
1247 break;
1248 } else {
1249 //
1250 // Found a "\r",return to the start of the current line.
1251 //
1252 PrevIndex = PrevSearchIndex + 1;
1253 CurrIndex = PrevSearchIndex + gHelpBlockWidth;
1254 continue;
1255 }
1256 }
1257 }
1258
1259 //
1260 // End of the string, thus stop counting.
1261 //
1262 if (StringPtr[CurrIndex] == CHAR_NULL) {
1263 StringCount++;
1264 break;
1265 }
1266 //
1267 // The premise is that for every HELP_BLOCK_WIDTH we rewind
1268 // until we find the first space. That is the delimiter for
1269 // the string, and we will then advance our CurrIndex another
1270 // HELP_BLOCK_WIDTH and continue the process breaking the larger
1271 // string into chunks that fit within the HELP_BLOCK_WIDTH requirements.
1272 //
1273 if (StringPtr[CurrIndex] == CHAR_SPACE) {
1274 //
1275 // How many strings have been found?
1276 //
1277 StringCount++;
1278 PrevIndex = CurrIndex + 1;
1279 CurrIndex = CurrIndex + gHelpBlockWidth;
1280 }
1281 //
1282 // Found a Linefeed, advance to the next line.
1283 //
1284 if (StringPtr[CurrIndex] == CHAR_LINEFEED) {
1285 StringCount++;
1286 PrevIndex = CurrIndex + 1;
1287 CurrIndex = CurrIndex + gHelpBlockWidth;
1288 }
1289 }
1290 //
1291 // endfor
1292 //
1293 // Round the value up one (doesn't hurt)
1294 //
1295 StringCount++;
1296
1297 //
1298 // Determine the number of pages this help string occupies
1299 //
1300 PageCount = StringCount / RowCount;
1301 if (StringCount % RowCount > 0) {
1302 PageCount++;
1303 }
1304 //
1305 // Convert the PageCount into lines so we can allocate the correct buffer size
1306 //
1307 StringCount = PageCount * RowCount;
1308
1309 //
1310 // We now know how many strings we will have, so we can allocate the
1311 // space required for the array or strings.
1312 //
1313 *FormattedString = AllocateZeroPool ((StringCount) * (gHelpBlockWidth + 1) * 2);
1314 ASSERT (*FormattedString);
1315
1316 StringCount = 0;
1317 PrevIndex = 0;
1318 CurrIndex = gHelpBlockWidth - 1;
1319
1320 for (; CurrIndex > PrevIndex; CurrIndex--) {
1321 //
1322 // In the case where the string ended and a new one is immediately after it
1323 // we need to check for the null-terminator and reset the CurrIndex
1324 //
1325 SearchIndex = CurrIndex;
1326 PrevSearchIndex = PrevIndex;
1327
1328 for (; SearchIndex > PrevSearchIndex; PrevSearchIndex++) {
1329 if ((StringPtr[PrevSearchIndex] == CHAR_NULL) || (StringPtr[PrevSearchIndex] == CHAR_LINEFEED)) {
1330 CurrIndex = PrevSearchIndex;
1331 break;
1332 }
1333
1334 if (StringPtr[PrevSearchIndex] == CHAR_CARRIAGE_RETURN) {
1335 if (StringPtr[PrevSearchIndex + 1] == CHAR_LINEFEED) {
1336 //
1337 // Found a "\n",advance to the next new line.
1338 //
1339 CurrIndex = PrevSearchIndex + 1;
1340 break;
1341 } else {
1342 //
1343 // Found a "\r",return to the start of the current line.
1344 //
1345 PrevIndex = PrevSearchIndex + 1;
1346 CurrIndex = PrevSearchIndex + gHelpBlockWidth;
1347 continue;
1348 }
1349 }
1350 }
1351
1352 //
1353 // End of the string, thus stop counting.
1354 //
1355 if (StringPtr[CurrIndex] == CHAR_NULL) {
1356 //
1357 // Copy the fragment to the FormattedString buffer
1358 //
1359 StrnCpy ((FormattedString[0] + StringCount * gHelpBlockWidth), &StringPtr[PrevIndex], CurrIndex - PrevIndex);
1360 StringCount++;
1361 break;
1362 }
1363 //
1364 // The premise is that for every HELP_BLOCK_WIDTH we rewind
1365 // until we find the first space. That is the delimiter for
1366 // the string, and we will then advance our CurrIndex another
1367 // HELP_BLOCK_WIDTH and continue the process breaking the larger
1368 // string into chunks that fit within the HELP_BLOCK_WIDTH requirements.
1369 //
1370 if (StringPtr[CurrIndex] == CHAR_SPACE) {
1371 //
1372 // Copy the fragment to the FormattedString buffer
1373 //
1374 StrnCpy ((FormattedString[0] + StringCount * gHelpBlockWidth), &StringPtr[PrevIndex], CurrIndex - PrevIndex);
1375 StringCount++;
1376 PrevIndex = CurrIndex + 1;
1377 CurrIndex = CurrIndex + gHelpBlockWidth;
1378 }
1379 //
1380 // Found a LineFeed, advance to the next line.
1381 //
1382 if (StringPtr[CurrIndex] == CHAR_LINEFEED) {
1383 StringPtr[CurrIndex] = CHAR_SPACE;
1384 //
1385 // "\n" is represented as CHAR_CARRIAGE_RETURN + CHAR_LINEFEED,check this.
1386 //
1387 if (StringPtr[CurrIndex - 1] == CHAR_CARRIAGE_RETURN) {
1388 StringPtr[CurrIndex - 1] = CHAR_SPACE;
1389 }
1390
1391 StrnCpy ((FormattedString[0] + StringCount * gHelpBlockWidth), &StringPtr[PrevIndex], CurrIndex - PrevIndex);
1392 StringCount++;
1393 PrevIndex = CurrIndex + 1;
1394 CurrIndex = CurrIndex + gHelpBlockWidth;
1395 }
1396 }
1397 //
1398 // endfor
1399 //
1400 return ;
1401 }
1402
1403 VOID
1404 IfrToFormTag (
1405 IN UINT8 OpCode,
1406 IN EFI_TAG *TargetTag,
1407 IN VOID *FormData,
1408 EFI_VARIABLE_DEFINITION *VariableDefinitionsHead
1409 )
1410 {
1411 UINT16 TempValue;
1412 CHAR16 *VariableName;
1413 CHAR8 *AsciiString;
1414 EFI_VARIABLE_DEFINITION *VariableDefinitions;
1415 EFI_VARIABLE_DEFINITION *PreviousVariableDefinitions;
1416 STATIC UINT16 VariableSize;
1417 EFI_GUID Guid;
1418 STATIC UINT16 CurrentVariable;
1419 STATIC UINT16 CurrentVariable2;
1420 UINTN Index;
1421
1422 switch (OpCode) {
1423 case EFI_IFR_FORM_OP:
1424 CopyMem (&TargetTag->Id, &((EFI_IFR_FORM *) FormData)->FormId, sizeof (UINT16));
1425 CopyMem (&TargetTag->Text, &((EFI_IFR_FORM *) FormData)->FormTitle, sizeof (UINT16));
1426 TargetTag->VariableNumber = CurrentVariable;
1427 if (VariableDefinitionsHead != NULL) {
1428 VariableName = AllocateZeroPool (12);
1429 ASSERT (VariableName != NULL);
1430 CopyMem (VariableName, L"Setup", 12);
1431 VariableDefinitionsHead->VariableName = VariableName;
1432 VariableDefinitionsHead->VariableSize = VariableSize;
1433 CopyMem (&VariableDefinitionsHead->Guid, &Guid, sizeof (EFI_GUID));
1434 }
1435 break;
1436
1437 case EFI_IFR_SUBTITLE_OP:
1438 TargetTag->NumberOfLines = 1;
1439 CopyMem (&TargetTag->Text, &((EFI_IFR_SUBTITLE *) FormData)->SubTitle, sizeof (UINT16));
1440 TargetTag->VariableNumber = CurrentVariable;
1441 break;
1442
1443 case EFI_IFR_TEXT_OP:
1444 TargetTag->NumberOfLines = 1;
1445 CopyMem (&TargetTag->Text, &((EFI_IFR_TEXT *) FormData)->Text, sizeof (UINT16));
1446 CopyMem (&TargetTag->Help, &((EFI_IFR_TEXT *) FormData)->Help, sizeof (UINT16));
1447 TargetTag->VariableNumber = CurrentVariable;
1448
1449 //
1450 // To optimize the encoding size, certain opcodes have optional fields such as those
1451 // inside the if() statement. If the encoded length is the complete size, then we
1452 // know we have valid data encoded that we want to integrate
1453 //
1454 if (((EFI_IFR_TEXT *) FormData)->Header.Length == sizeof (EFI_IFR_TEXT)) {
1455 //
1456 // Text has no help associated with it, but in case there is a second entry due to
1457 // dynamic/interactive flags being active, bring this data over.
1458 //
1459 CopyMem (&TargetTag->TextTwo, &((EFI_IFR_TEXT *) FormData)->TextTwo, sizeof (UINT16));
1460 TargetTag->Flags = ((EFI_IFR_TEXT *) FormData)->Flags;
1461 CopyMem (&TargetTag->Key, &((EFI_IFR_TEXT *) FormData)->Key, sizeof (UINT16));
1462 }
1463 break;
1464
1465 case EFI_IFR_ONE_OF_OPTION_OP:
1466 CopyMem (&TargetTag->Text, &((EFI_IFR_ONE_OF_OPTION *) FormData)->Option, sizeof (UINT16));
1467 CopyMem (&TargetTag->Value, &((EFI_IFR_ONE_OF_OPTION *) FormData)->Value, sizeof (UINT16));
1468 TargetTag->Flags = ((EFI_IFR_ONE_OF_OPTION *) FormData)->Flags;
1469 CopyMem (&TargetTag->Key, &((EFI_IFR_ONE_OF_OPTION *) FormData)->Key, sizeof (UINT16));
1470 TargetTag->VariableNumber = CurrentVariable;
1471 break;
1472
1473 case EFI_IFR_CHECKBOX_OP:
1474 TargetTag->Flags = ((EFI_IFR_CHECKBOX *) FormData)->Flags;
1475 TargetTag->ResetRequired = (BOOLEAN) (TargetTag->Flags & EFI_IFR_FLAG_RESET_REQUIRED);
1476 CopyMem (&TargetTag->Key, &((EFI_IFR_CHECKBOX *) FormData)->Key, sizeof (UINT16));
1477 TargetTag->VariableNumber = CurrentVariable;
1478 break;
1479
1480 case EFI_IFR_NUMERIC_OP:
1481 TargetTag->Flags = ((EFI_IFR_NUMERIC *) FormData)->Flags;
1482 CopyMem (&TargetTag->Key, &((EFI_IFR_NUMERIC *) FormData)->Key, sizeof (UINT16));
1483 TargetTag->VariableNumber = CurrentVariable;
1484 break;
1485
1486 case EFI_IFR_STRING_OP:
1487 TempValue = 0;
1488 CopyMem (&TempValue, &((EFI_IFR_STRING *) FormData)->MinSize, sizeof (UINT8));
1489 TempValue = (UINT16) (TempValue * 2);
1490 CopyMem (&TargetTag->Minimum, &TempValue, sizeof (UINT16));
1491
1492 CopyMem (&TempValue, &((EFI_IFR_STRING *) FormData)->MaxSize, sizeof (UINT8));
1493 TempValue = (UINT16) (TempValue * 2);
1494 CopyMem (&TargetTag->Maximum, &TempValue, sizeof (UINT16));
1495 CopyMem (&TargetTag->StorageWidth, &TempValue, sizeof (UINT16));
1496 TargetTag->Flags = (UINT8) (((EFI_IFR_STRING *) FormData)->Flags);
1497 TargetTag->ResetRequired = (BOOLEAN) (TargetTag->Flags & EFI_IFR_FLAG_RESET_REQUIRED);
1498 CopyMem (&TargetTag->Key, &((EFI_IFR_STRING *) FormData)->Key, sizeof (UINT16));
1499 TargetTag->VariableNumber = CurrentVariable;
1500 break;
1501
1502 case EFI_IFR_PASSWORD_OP:
1503 TempValue = 0;
1504 CopyMem (&TempValue, &((EFI_IFR_PASSWORD *) FormData)->MinSize, sizeof (UINT8));
1505 TempValue = (UINT16) (TempValue * 2);
1506 CopyMem (&TargetTag->Minimum, &TempValue, sizeof (UINT16));
1507
1508 CopyMem (&TempValue, &((EFI_IFR_PASSWORD *) FormData)->MaxSize, sizeof (UINT8));
1509 TempValue = (UINT16) (TempValue * 2);
1510 CopyMem (&TargetTag->Maximum, &TempValue, sizeof (UINT16));
1511 CopyMem (&TargetTag->StorageWidth, &TempValue, sizeof (UINT16));
1512 TargetTag->Flags = ((EFI_IFR_PASSWORD *) FormData)->Flags;
1513 TargetTag->ResetRequired = (BOOLEAN) (TargetTag->Flags & EFI_IFR_FLAG_RESET_REQUIRED);
1514 CopyMem (&TargetTag->Key, &((EFI_IFR_PASSWORD *) FormData)->Key, sizeof (UINT16));
1515 CopyMem (&TargetTag->Encoding, &((EFI_IFR_PASSWORD *) FormData)->Encoding, sizeof (UINT16));
1516 TargetTag->VariableNumber = CurrentVariable;
1517 break;
1518
1519 case EFI_IFR_VARSTORE_OP:
1520 //
1521 // It should NEVER be NULL
1522 //
1523 if (VariableDefinitionsHead == NULL) {
1524 break;
1525 }
1526
1527 VariableDefinitions = VariableDefinitionsHead;
1528
1529 //
1530 // Advance VariableDefinitions to the last entry
1531 //
1532 for (; VariableDefinitions != NULL; VariableDefinitions = VariableDefinitions->Next) {
1533 PreviousVariableDefinitions = VariableDefinitions;
1534 //
1535 // If there is a variable with this GUID and ID already, we need to bail out
1536 //
1537 if (!CompareMem (&VariableDefinitions->Guid, &((EFI_IFR_VARSTORE *) FormData)->Guid, sizeof (EFI_GUID)) &&
1538 !CompareMem (&VariableDefinitions->VariableId, &((EFI_IFR_VARSTORE *) FormData)->VarId, sizeof (UINT16))
1539 ) {
1540 return ;
1541 }
1542
1543 if (VariableDefinitions->Next == NULL) {
1544 break;
1545 }
1546 }
1547 //
1548 // If the last entry has a variable in it already, allocate a new entry and use it
1549 //
1550 if (VariableDefinitions->VariableName != NULL) {
1551 VariableDefinitions->Next = AllocateZeroPool (sizeof (EFI_VARIABLE_DEFINITION));
1552 ASSERT (VariableDefinitions->Next != NULL);
1553 PreviousVariableDefinitions = VariableDefinitions;
1554 VariableDefinitions = VariableDefinitions->Next;
1555 VariableDefinitions->Previous = PreviousVariableDefinitions;
1556 }
1557 //
1558 // Copy the Variable data to our linked list
1559 //
1560 CopyMem (&VariableDefinitions->VariableId, &((EFI_IFR_VARSTORE *) FormData)->VarId, sizeof (UINT16));
1561 CopyMem (&VariableDefinitions->VariableSize, &((EFI_IFR_VARSTORE *) FormData)->Size, sizeof (UINT16));
1562 CopyMem (&VariableDefinitions->Guid, &((EFI_IFR_VARSTORE *) FormData)->Guid, sizeof (EFI_GUID));
1563
1564 //
1565 // The ASCII String which is immediately past the EFI_IFR_VARSTORE is inferred by the structure definition
1566 // due to it being variable sized. There are rules preventing it from being > 40 characters long and should
1567 // be enforced by the compiler.
1568 //
1569 AsciiString = (CHAR8 *) (&((EFI_IFR_VARSTORE *) FormData)->Size);
1570 AsciiString = AsciiString + 2;
1571 VariableDefinitions->VariableName = AllocateZeroPool ((AsciiStrLen (AsciiString) + 1) * 2);
1572 ASSERT (VariableDefinitions->VariableName != NULL);
1573 for (Index = 0; AsciiString[Index] != 0; Index++) {
1574 VariableDefinitions->VariableName[Index] = (CHAR16) AsciiString[Index];
1575 }
1576
1577 VariableDefinitions->VariableName[Index] = 0;
1578
1579 //
1580 // Propogate the tag information for this op-code
1581 //
1582 CopyMem (&TargetTag->VariableNumber, &((EFI_IFR_VARSTORE *) FormData)->VarId, sizeof (UINT16));
1583 CopyMem (&TargetTag->GuidValue, &((EFI_IFR_VARSTORE *) FormData)->Guid, sizeof (EFI_GUID));
1584 CopyMem (&TargetTag->StorageWidth, &((EFI_IFR_VARSTORE *) FormData)->Size, sizeof (UINT16));
1585 CopyMem (&TargetTag->Maximum, &((EFI_IFR_VARSTORE *) FormData)->Size, sizeof (UINT16));
1586 break;
1587
1588 case EFI_IFR_VARSTORE_SELECT_OP:
1589 CopyMem (&TargetTag->VariableNumber, &((EFI_IFR_VARSTORE_SELECT *) FormData)->VarId, sizeof (UINT16));
1590 CopyMem (&CurrentVariable, &((EFI_IFR_VARSTORE_SELECT *) FormData)->VarId, sizeof (UINT16));
1591 CurrentVariable2 = CurrentVariable;
1592 break;
1593
1594 case EFI_IFR_VARSTORE_SELECT_PAIR_OP:
1595 CopyMem (&TargetTag->VariableNumber, &((EFI_IFR_VARSTORE_SELECT_PAIR *) FormData)->VarId, sizeof (UINT16));
1596 CopyMem (
1597 &TargetTag->VariableNumber2,
1598 &((EFI_IFR_VARSTORE_SELECT_PAIR *) FormData)->SecondaryVarId,
1599 sizeof (UINT16)
1600 );
1601 CopyMem (&CurrentVariable, &((EFI_IFR_VARSTORE_SELECT_PAIR *) FormData)->VarId, sizeof (UINT16));
1602 CopyMem (&CurrentVariable2, &((EFI_IFR_VARSTORE_SELECT_PAIR *) FormData)->SecondaryVarId, sizeof (UINT16));
1603 break;
1604
1605 case EFI_IFR_REF_OP:
1606 TargetTag->NumberOfLines = 1;
1607 CopyMem (&TargetTag->Id, &((EFI_IFR_REF *) FormData)->FormId, sizeof (UINT16));
1608 CopyMem (&TargetTag->Key, &((EFI_IFR_REF *) FormData)->Key, sizeof (UINT16));
1609 CopyMem (&TargetTag->Text, &((EFI_IFR_REF *) FormData)->Prompt, sizeof (UINT16));
1610 CopyMem (&TargetTag->Help, &((EFI_IFR_REF *) FormData)->Help, sizeof (UINT16));
1611 TargetTag->Flags = ((EFI_IFR_REF *) FormData)->Flags;
1612 TargetTag->VariableNumber = CurrentVariable;
1613 break;
1614
1615 case EFI_IFR_EQ_ID_VAL_OP:
1616 CopyMem (&TargetTag->Value, &((EFI_IFR_EQ_ID_VAL *) FormData)->Value, sizeof (UINT16));
1617 CopyMem (&TargetTag->Id, &((EFI_IFR_EQ_ID_VAL *) FormData)->QuestionId, sizeof (UINT16));
1618 TargetTag->StorageWidth = ((EFI_IFR_EQ_ID_VAL *) FormData)->Width;
1619 TargetTag->VariableNumber = CurrentVariable;
1620 break;
1621
1622 case EFI_IFR_EQ_VAR_VAL_OP:
1623 CopyMem (&TargetTag->Value, &((EFI_IFR_EQ_VAR_VAL *) FormData)->Value, sizeof (UINT16));
1624 CopyMem (&TargetTag->Id, &((EFI_IFR_EQ_VAR_VAL *) FormData)->VariableId, sizeof (UINT16));
1625 TargetTag->VariableNumber = CurrentVariable;
1626 break;
1627
1628 case EFI_IFR_EQ_ID_ID_OP:
1629 CopyMem (&TargetTag->Id, &((EFI_IFR_EQ_ID_ID *) FormData)->QuestionId1, sizeof (UINT16));
1630 CopyMem (&TargetTag->Id2, &((EFI_IFR_EQ_ID_ID *) FormData)->QuestionId2, sizeof (UINT16));
1631 TargetTag->StorageWidth = ((EFI_IFR_EQ_ID_ID *) FormData)->Width;
1632 TargetTag->VariableNumber = CurrentVariable;
1633 TargetTag->VariableNumber = CurrentVariable2;
1634 break;
1635
1636 case EFI_IFR_EQ_ID_LIST_OP:
1637 CopyMem (&TargetTag->Id, &((EFI_IFR_EQ_ID_LIST *) FormData)->QuestionId, sizeof (UINT16));
1638 CopyMem (&TargetTag->Id2, &((EFI_IFR_EQ_ID_LIST *) FormData)->ListLength, sizeof (UINT16));
1639 TargetTag->StorageWidth = ((EFI_IFR_EQ_ID_LIST *) FormData)->Width;
1640
1641 TargetTag->IntList = AllocateZeroPool (TargetTag->Id2 * sizeof (UINT16));
1642 ASSERT (TargetTag->IntList);
1643
1644 for (TempValue = 0; TempValue < TargetTag->Id2; TempValue++) {
1645 CopyMem (
1646 &TargetTag->IntList[TempValue],
1647 &((EFI_IFR_EQ_ID_LIST *) FormData)->ValueList[TempValue],
1648 sizeof (UINT16)
1649 );
1650 }
1651
1652 TargetTag->VariableNumber = CurrentVariable;
1653 break;
1654
1655 case EFI_IFR_FORM_SET_OP:
1656 CopyMem (&VariableSize, &((EFI_IFR_FORM_SET *) FormData)->NvDataSize, sizeof (UINT16));
1657 CopyMem (&Guid, &((EFI_IFR_FORM_SET *) FormData)->Guid, sizeof (EFI_GUID));
1658 //
1659 // If there is a size specified in the formste, we will establish a "default" variable
1660 //
1661 if (VariableDefinitionsHead != NULL) {
1662 VariableName = AllocateZeroPool (12);
1663 ASSERT (VariableName != NULL);
1664 CopyMem (VariableName, L"Setup", 12);
1665 VariableDefinitionsHead->VariableName = VariableName;
1666 VariableDefinitionsHead->VariableSize = VariableSize;
1667 CopyMem (&VariableDefinitionsHead->Guid, &Guid, sizeof (EFI_GUID));
1668 }
1669 break;
1670
1671 case EFI_IFR_END_FORM_SET_OP:
1672 CurrentVariable = 0;
1673 CurrentVariable2 = 0;
1674 break;
1675 }
1676
1677 return ;
1678 }