MdeModulePkg: Add match2 opcode support in SetupBrowserDxe and sample code in DriverS...
[mirror_edk2.git] / MdeModulePkg / Universal / SetupBrowserDxe / Expression.c
1 /** @file
2 Utility functions for expression evaluation.
3
4 Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Setup.h"
16
17 //
18 // Global stack used to evaluate boolean expresions
19 //
20 EFI_HII_VALUE *mOpCodeScopeStack = NULL;
21 EFI_HII_VALUE *mOpCodeScopeStackEnd = NULL;
22 EFI_HII_VALUE *mOpCodeScopeStackPointer = NULL;
23
24 EFI_HII_VALUE *mExpressionEvaluationStack = NULL;
25 EFI_HII_VALUE *mExpressionEvaluationStackEnd = NULL;
26 EFI_HII_VALUE *mExpressionEvaluationStackPointer = NULL;
27 UINTN mExpressionEvaluationStackOffset = 0;
28
29 EFI_HII_VALUE *mCurrentExpressionStack = NULL;
30 EFI_HII_VALUE *mCurrentExpressionEnd = NULL;
31 EFI_HII_VALUE *mCurrentExpressionPointer = NULL;
32
33 EFI_HII_VALUE *mMapExpressionListStack = NULL;
34 EFI_HII_VALUE *mMapExpressionListEnd = NULL;
35 EFI_HII_VALUE *mMapExpressionListPointer = NULL;
36
37 FORM_EXPRESSION **mFormExpressionStack = NULL;
38 FORM_EXPRESSION **mFormExpressionEnd = NULL;
39 FORM_EXPRESSION **mFormExpressionPointer = NULL;
40
41 FORM_EXPRESSION **mStatementExpressionStack = NULL;
42 FORM_EXPRESSION **mStatementExpressionEnd = NULL;
43 FORM_EXPRESSION **mStatementExpressionPointer = NULL;
44
45 FORM_EXPRESSION **mOptionExpressionStack = NULL;
46 FORM_EXPRESSION **mOptionExpressionEnd = NULL;
47 FORM_EXPRESSION **mOptionExpressionPointer = NULL;
48
49
50 //
51 // Unicode collation protocol interface
52 //
53 EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollation = NULL;
54 EFI_USER_MANAGER_PROTOCOL *mUserManager = NULL;
55
56 /**
57 Grow size of the stack.
58
59 This is an internal function.
60
61 @param Stack On input: old stack; On output: new stack
62 @param StackPtr On input: old stack pointer; On output: new stack
63 pointer
64 @param StackEnd On input: old stack end; On output: new stack end
65
66 @retval EFI_SUCCESS Grow stack success.
67 @retval EFI_OUT_OF_RESOURCES No enough memory for stack space.
68
69 **/
70 EFI_STATUS
71 GrowStack (
72 IN OUT EFI_HII_VALUE **Stack,
73 IN OUT EFI_HII_VALUE **StackPtr,
74 IN OUT EFI_HII_VALUE **StackEnd
75 )
76 {
77 UINTN Size;
78 EFI_HII_VALUE *NewStack;
79
80 Size = EXPRESSION_STACK_SIZE_INCREMENT;
81 if (*StackPtr != NULL) {
82 Size = Size + (*StackEnd - *Stack);
83 }
84
85 NewStack = AllocatePool (Size * sizeof (EFI_HII_VALUE));
86 if (NewStack == NULL) {
87 return EFI_OUT_OF_RESOURCES;
88 }
89
90 if (*StackPtr != NULL) {
91 //
92 // Copy from Old Stack to the New Stack
93 //
94 CopyMem (
95 NewStack,
96 *Stack,
97 (*StackEnd - *Stack) * sizeof (EFI_HII_VALUE)
98 );
99
100 //
101 // Free The Old Stack
102 //
103 FreePool (*Stack);
104 }
105
106 //
107 // Make the Stack pointer point to the old data in the new stack
108 //
109 *StackPtr = NewStack + (*StackPtr - *Stack);
110 *Stack = NewStack;
111 *StackEnd = NewStack + Size;
112
113 return EFI_SUCCESS;
114 }
115
116
117 /**
118 Push an element onto the Boolean Stack.
119
120 @param Stack On input: old stack; On output: new stack
121 @param StackPtr On input: old stack pointer; On output: new stack
122 pointer
123 @param StackEnd On input: old stack end; On output: new stack end
124 @param Data Data to push.
125
126 @retval EFI_SUCCESS Push stack success.
127
128 **/
129 EFI_STATUS
130 PushStack (
131 IN OUT EFI_HII_VALUE **Stack,
132 IN OUT EFI_HII_VALUE **StackPtr,
133 IN OUT EFI_HII_VALUE **StackEnd,
134 IN EFI_HII_VALUE *Data
135 )
136 {
137 EFI_STATUS Status;
138
139 //
140 // Check for a stack overflow condition
141 //
142 if (*StackPtr >= *StackEnd) {
143 //
144 // Grow the stack
145 //
146 Status = GrowStack (Stack, StackPtr, StackEnd);
147 if (EFI_ERROR (Status)) {
148 return Status;
149 }
150 }
151
152 //
153 // Push the item onto the stack
154 //
155 CopyMem (*StackPtr, Data, sizeof (EFI_HII_VALUE));
156 if (Data->Type == EFI_IFR_TYPE_BUFFER) {
157 (*StackPtr)->Buffer = AllocateCopyPool(Data->BufferLen, Data->Buffer);
158 ASSERT ((*StackPtr)->Buffer != NULL);
159 }
160
161 *StackPtr = *StackPtr + 1;
162
163 return EFI_SUCCESS;
164 }
165
166
167 /**
168 Pop an element from the stack.
169
170 @param Stack On input: old stack
171 @param StackPtr On input: old stack pointer; On output: new stack pointer
172 @param Data Data to pop.
173
174 @retval EFI_SUCCESS The value was popped onto the stack.
175 @retval EFI_ACCESS_DENIED The pop operation underflowed the stack
176
177 **/
178 EFI_STATUS
179 PopStack (
180 IN EFI_HII_VALUE *Stack,
181 IN OUT EFI_HII_VALUE **StackPtr,
182 OUT EFI_HII_VALUE *Data
183 )
184 {
185 //
186 // Check for a stack underflow condition
187 //
188 if (*StackPtr == Stack) {
189 return EFI_ACCESS_DENIED;
190 }
191
192 //
193 // Pop the item off the stack
194 //
195 *StackPtr = *StackPtr - 1;
196 CopyMem (Data, *StackPtr, sizeof (EFI_HII_VALUE));
197 return EFI_SUCCESS;
198 }
199
200
201 /**
202 Reset stack pointer to begin of the stack.
203
204 **/
205 VOID
206 ResetCurrentExpressionStack (
207 VOID
208 )
209 {
210 mCurrentExpressionPointer = mCurrentExpressionStack;
211 mFormExpressionPointer = mFormExpressionStack;
212 mStatementExpressionPointer = mStatementExpressionStack;
213 mOptionExpressionPointer = mOptionExpressionStack;
214 }
215
216
217 /**
218 Push current expression onto the Stack
219
220 @param Pointer Pointer to current expression.
221
222 @retval EFI_SUCCESS The value was pushed onto the stack.
223 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
224
225 **/
226 EFI_STATUS
227 PushCurrentExpression (
228 IN VOID *Pointer
229 )
230 {
231 EFI_HII_VALUE Data;
232
233 Data.Type = EFI_IFR_TYPE_NUM_SIZE_64;
234 Data.Value.u64 = (UINT64) (UINTN) Pointer;
235
236 return PushStack (
237 &mCurrentExpressionStack,
238 &mCurrentExpressionPointer,
239 &mCurrentExpressionEnd,
240 &Data
241 );
242 }
243
244
245 /**
246 Pop current expression from the Stack
247
248 @param Pointer Pointer to current expression to be pop.
249
250 @retval EFI_SUCCESS The value was pushed onto the stack.
251 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
252
253 **/
254 EFI_STATUS
255 PopCurrentExpression (
256 OUT VOID **Pointer
257 )
258 {
259 EFI_STATUS Status;
260 EFI_HII_VALUE Data;
261
262 Status = PopStack (
263 mCurrentExpressionStack,
264 &mCurrentExpressionPointer,
265 &Data
266 );
267
268 *Pointer = (VOID *) (UINTN) Data.Value.u64;
269
270 return Status;
271 }
272
273 /**
274 Reset stack pointer to begin of the stack.
275
276 **/
277 VOID
278 ResetMapExpressionListStack (
279 VOID
280 )
281 {
282 mMapExpressionListPointer = mMapExpressionListStack;
283 }
284
285
286 /**
287 Grow size of the stack.
288
289 This is an internal function.
290
291 @param Stack On input: old stack; On output: new stack
292 @param StackPtr On input: old stack pointer; On output: new stack
293 pointer
294 @param StackEnd On input: old stack end; On output: new stack end
295 @param MemberSize The stack member size.
296
297 @retval EFI_SUCCESS Grow stack success.
298 @retval EFI_OUT_OF_RESOURCES No enough memory for stack space.
299
300 **/
301 EFI_STATUS
302 GrowConditionalStack (
303 IN OUT FORM_EXPRESSION ***Stack,
304 IN OUT FORM_EXPRESSION ***StackPtr,
305 IN OUT FORM_EXPRESSION ***StackEnd,
306 IN UINTN MemberSize
307 )
308 {
309 UINTN Size;
310 FORM_EXPRESSION **NewStack;
311
312 Size = EXPRESSION_STACK_SIZE_INCREMENT;
313 if (*StackPtr != NULL) {
314 Size = Size + (*StackEnd - *Stack);
315 }
316
317 NewStack = AllocatePool (Size * MemberSize);
318 if (NewStack == NULL) {
319 return EFI_OUT_OF_RESOURCES;
320 }
321
322 if (*StackPtr != NULL) {
323 //
324 // Copy from Old Stack to the New Stack
325 //
326 CopyMem (
327 NewStack,
328 *Stack,
329 (*StackEnd - *Stack) * MemberSize
330 );
331
332 //
333 // Free The Old Stack
334 //
335 FreePool (*Stack);
336 }
337
338 //
339 // Make the Stack pointer point to the old data in the new stack
340 //
341 *StackPtr = NewStack + (*StackPtr - *Stack);
342 *Stack = NewStack;
343 *StackEnd = NewStack + Size;
344
345 return EFI_SUCCESS;
346 }
347
348 /**
349 Push an element onto the Stack.
350
351 @param Stack On input: old stack; On output: new stack
352 @param StackPtr On input: old stack pointer; On output: new stack
353 pointer
354 @param StackEnd On input: old stack end; On output: new stack end
355 @param Data Data to push.
356
357 @retval EFI_SUCCESS Push stack success.
358
359 **/
360 EFI_STATUS
361 PushConditionalStack (
362 IN OUT FORM_EXPRESSION ***Stack,
363 IN OUT FORM_EXPRESSION ***StackPtr,
364 IN OUT FORM_EXPRESSION ***StackEnd,
365 IN FORM_EXPRESSION **Data
366 )
367 {
368 EFI_STATUS Status;
369
370 //
371 // Check for a stack overflow condition
372 //
373 if (*StackPtr >= *StackEnd) {
374 //
375 // Grow the stack
376 //
377 Status = GrowConditionalStack (Stack, StackPtr, StackEnd, sizeof (FORM_EXPRESSION *));
378 if (EFI_ERROR (Status)) {
379 return Status;
380 }
381 }
382
383 //
384 // Push the item onto the stack
385 //
386 CopyMem (*StackPtr, Data, sizeof (FORM_EXPRESSION *));
387 *StackPtr = *StackPtr + 1;
388
389 return EFI_SUCCESS;
390
391 }
392
393 /**
394 Pop an element from the stack.
395
396 @param Stack On input: old stack
397 @param StackPtr On input: old stack pointer; On output: new stack pointer
398 @param Data Data to pop.
399
400 @retval EFI_SUCCESS The value was popped onto the stack.
401 @retval EFI_ACCESS_DENIED The pop operation underflowed the stack
402
403 **/
404 EFI_STATUS
405 PopConditionalStack (
406 IN FORM_EXPRESSION **Stack,
407 IN OUT FORM_EXPRESSION ***StackPtr,
408 OUT FORM_EXPRESSION **Data
409 )
410 {
411 //
412 // Check for a stack underflow condition
413 //
414 if (*StackPtr == Stack) {
415 return EFI_ACCESS_DENIED;
416 }
417
418 //
419 // Pop the item off the stack
420 //
421 *StackPtr = *StackPtr - 1;
422 CopyMem (Data, *StackPtr, sizeof (FORM_EXPRESSION *));
423 return EFI_SUCCESS;
424
425 }
426
427 /**
428 Get the expression list count.
429
430 @param Level Which type this expression belong to. Form,
431 statement or option?
432
433 @retval >=0 The expression count
434 @retval -1 Input parameter error.
435
436 **/
437 INTN
438 GetConditionalExpressionCount (
439 IN EXPRESS_LEVEL Level
440 )
441 {
442 switch (Level) {
443 case ExpressForm:
444 return mFormExpressionPointer - mFormExpressionStack;
445 case ExpressStatement:
446 return mStatementExpressionPointer - mStatementExpressionStack;
447 case ExpressOption:
448 return mOptionExpressionPointer - mOptionExpressionStack;
449 default:
450 ASSERT (FALSE);
451 return -1;
452 }
453 }
454
455 /**
456 Get the expression Buffer pointer.
457
458 @param Level Which type this expression belong to. Form,
459 statement or option?
460
461 @retval The start pointer of the expression buffer or NULL.
462
463 **/
464 FORM_EXPRESSION **
465 GetConditionalExpressionList (
466 IN EXPRESS_LEVEL Level
467 )
468 {
469 switch (Level) {
470 case ExpressForm:
471 return mFormExpressionStack;
472 case ExpressStatement:
473 return mStatementExpressionStack;
474 case ExpressOption:
475 return mOptionExpressionStack;
476 default:
477 ASSERT (FALSE);
478 return NULL;
479 }
480 }
481
482
483 /**
484 Push the expression options onto the Stack.
485
486 @param Pointer Pointer to the current expression.
487 @param Level Which type this expression belong to. Form,
488 statement or option?
489
490 @retval EFI_SUCCESS The value was pushed onto the stack.
491 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
492
493 **/
494 EFI_STATUS
495 PushConditionalExpression (
496 IN FORM_EXPRESSION *Pointer,
497 IN EXPRESS_LEVEL Level
498 )
499 {
500 switch (Level) {
501 case ExpressForm:
502 return PushConditionalStack (
503 &mFormExpressionStack,
504 &mFormExpressionPointer,
505 &mFormExpressionEnd,
506 &Pointer
507 );
508 case ExpressStatement:
509 return PushConditionalStack (
510 &mStatementExpressionStack,
511 &mStatementExpressionPointer,
512 &mStatementExpressionEnd,
513 &Pointer
514 );
515 case ExpressOption:
516 return PushConditionalStack (
517 &mOptionExpressionStack,
518 &mOptionExpressionPointer,
519 &mOptionExpressionEnd,
520 &Pointer
521 );
522 default:
523 ASSERT (FALSE);
524 return EFI_INVALID_PARAMETER;
525 }
526 }
527
528 /**
529 Pop the expression options from the Stack
530
531 @param Level Which type this expression belong to. Form,
532 statement or option?
533
534 @retval EFI_SUCCESS The value was pushed onto the stack.
535 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
536
537 **/
538 EFI_STATUS
539 PopConditionalExpression (
540 IN EXPRESS_LEVEL Level
541 )
542 {
543 FORM_EXPRESSION *Pointer;
544
545 switch (Level) {
546 case ExpressForm:
547 return PopConditionalStack (
548 mFormExpressionStack,
549 &mFormExpressionPointer,
550 &Pointer
551 );
552
553 case ExpressStatement:
554 return PopConditionalStack (
555 mStatementExpressionStack,
556 &mStatementExpressionPointer,
557 &Pointer
558 );
559
560 case ExpressOption:
561 return PopConditionalStack (
562 mOptionExpressionStack,
563 &mOptionExpressionPointer,
564 &Pointer
565 );
566
567 default:
568 ASSERT (FALSE);
569 return EFI_INVALID_PARAMETER;
570 }
571 }
572
573
574 /**
575 Push the list of map expression onto the Stack
576
577 @param Pointer Pointer to the list of map expression to be pushed.
578
579 @retval EFI_SUCCESS The value was pushed onto the stack.
580 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
581
582 **/
583 EFI_STATUS
584 PushMapExpressionList (
585 IN VOID *Pointer
586 )
587 {
588 EFI_HII_VALUE Data;
589
590 Data.Type = EFI_IFR_TYPE_NUM_SIZE_64;
591 Data.Value.u64 = (UINT64) (UINTN) Pointer;
592
593 return PushStack (
594 &mMapExpressionListStack,
595 &mMapExpressionListPointer,
596 &mMapExpressionListEnd,
597 &Data
598 );
599 }
600
601
602 /**
603 Pop the list of map expression from the Stack
604
605 @param Pointer Pointer to the list of map expression to be pop.
606
607 @retval EFI_SUCCESS The value was pushed onto the stack.
608 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the stack.
609
610 **/
611 EFI_STATUS
612 PopMapExpressionList (
613 OUT VOID **Pointer
614 )
615 {
616 EFI_STATUS Status;
617 EFI_HII_VALUE Data;
618
619 Status = PopStack (
620 mMapExpressionListStack,
621 &mMapExpressionListPointer,
622 &Data
623 );
624
625 *Pointer = (VOID *) (UINTN) Data.Value.u64;
626
627 return Status;
628 }
629
630 /**
631 Reset stack pointer to begin of the stack.
632
633 **/
634 VOID
635 ResetScopeStack (
636 VOID
637 )
638 {
639 mOpCodeScopeStackPointer = mOpCodeScopeStack;
640 }
641
642
643 /**
644 Push an Operand onto the Stack
645
646 @param Operand Operand to push.
647
648 @retval EFI_SUCCESS The value was pushed onto the stack.
649 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the
650 stack.
651
652 **/
653 EFI_STATUS
654 PushScope (
655 IN UINT8 Operand
656 )
657 {
658 EFI_HII_VALUE Data;
659
660 Data.Type = EFI_IFR_TYPE_NUM_SIZE_8;
661 Data.Value.u8 = Operand;
662
663 return PushStack (
664 &mOpCodeScopeStack,
665 &mOpCodeScopeStackPointer,
666 &mOpCodeScopeStackEnd,
667 &Data
668 );
669 }
670
671
672 /**
673 Pop an Operand from the Stack
674
675 @param Operand Operand to pop.
676
677 @retval EFI_SUCCESS The value was pushed onto the stack.
678 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the
679 stack.
680
681 **/
682 EFI_STATUS
683 PopScope (
684 OUT UINT8 *Operand
685 )
686 {
687 EFI_STATUS Status;
688 EFI_HII_VALUE Data;
689
690 Status = PopStack (
691 mOpCodeScopeStack,
692 &mOpCodeScopeStackPointer,
693 &Data
694 );
695
696 *Operand = Data.Value.u8;
697
698 return Status;
699 }
700
701
702 /**
703 Push an Expression value onto the Stack
704
705 @param Value Expression value to push.
706
707 @retval EFI_SUCCESS The value was pushed onto the stack.
708 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the
709 stack.
710
711 **/
712 EFI_STATUS
713 PushExpression (
714 IN EFI_HII_VALUE *Value
715 )
716 {
717 return PushStack (
718 &mExpressionEvaluationStack,
719 &mExpressionEvaluationStackPointer,
720 &mExpressionEvaluationStackEnd,
721 Value
722 );
723 }
724
725
726 /**
727 Pop an Expression value from the stack.
728
729 @param Value Expression value to pop.
730
731 @retval EFI_SUCCESS The value was popped onto the stack.
732 @retval EFI_ACCESS_DENIED The pop operation underflowed the stack
733
734 **/
735 EFI_STATUS
736 PopExpression (
737 OUT EFI_HII_VALUE *Value
738 )
739 {
740 return PopStack (
741 mExpressionEvaluationStack + mExpressionEvaluationStackOffset,
742 &mExpressionEvaluationStackPointer,
743 Value
744 );
745 }
746
747 /**
748 Get current stack offset from stack start.
749
750 @return Stack offset to stack start.
751 **/
752 UINTN
753 SaveExpressionEvaluationStackOffset (
754 )
755 {
756 UINTN TempStackOffset;
757 TempStackOffset = mExpressionEvaluationStackOffset;
758 mExpressionEvaluationStackOffset = mExpressionEvaluationStackPointer - mExpressionEvaluationStack;
759 return TempStackOffset;
760 }
761
762 /**
763 Restore stack offset based on input stack offset
764
765 @param StackOffset Offset to stack start.
766
767 **/
768 VOID
769 RestoreExpressionEvaluationStackOffset (
770 UINTN StackOffset
771 )
772 {
773 mExpressionEvaluationStackOffset = StackOffset;
774 }
775
776 /**
777 Get Form given its FormId.
778
779 @param FormSet The formset which contains this form.
780 @param FormId Id of this form.
781
782 @retval Pointer The form.
783 @retval NULL Specified Form is not found in the formset.
784
785 **/
786 FORM_BROWSER_FORM *
787 IdToForm (
788 IN FORM_BROWSER_FORMSET *FormSet,
789 IN UINT16 FormId
790 )
791 {
792 LIST_ENTRY *Link;
793 FORM_BROWSER_FORM *Form;
794
795 Link = GetFirstNode (&FormSet->FormListHead);
796 while (!IsNull (&FormSet->FormListHead, Link)) {
797 Form = FORM_BROWSER_FORM_FROM_LINK (Link);
798
799 if (Form->FormId == FormId) {
800 return Form;
801 }
802
803 Link = GetNextNode (&FormSet->FormListHead, Link);
804 }
805
806 return NULL;
807 }
808
809
810 /**
811 Search a Question in Form scope using its QuestionId.
812
813 @param Form The form which contains this Question.
814 @param QuestionId Id of this Question.
815
816 @retval Pointer The Question.
817 @retval NULL Specified Question not found in the form.
818
819 **/
820 FORM_BROWSER_STATEMENT *
821 IdToQuestion2 (
822 IN FORM_BROWSER_FORM *Form,
823 IN UINT16 QuestionId
824 )
825 {
826 LIST_ENTRY *Link;
827 FORM_BROWSER_STATEMENT *Question;
828
829 if (QuestionId == 0 || Form == NULL) {
830 //
831 // The value of zero is reserved
832 //
833 return NULL;
834 }
835
836 Link = GetFirstNode (&Form->StatementListHead);
837 while (!IsNull (&Form->StatementListHead, Link)) {
838 Question = FORM_BROWSER_STATEMENT_FROM_LINK (Link);
839
840 if (Question->QuestionId == QuestionId) {
841 return Question;
842 }
843
844 Link = GetNextNode (&Form->StatementListHead, Link);
845 }
846
847 return NULL;
848 }
849
850
851 /**
852 Search a Question in Formset scope using its QuestionId.
853
854 @param FormSet The formset which contains this form.
855 @param Form The form which contains this Question.
856 @param QuestionId Id of this Question.
857
858 @retval Pointer The Question.
859 @retval NULL Specified Question not found in the form.
860
861 **/
862 FORM_BROWSER_STATEMENT *
863 IdToQuestion (
864 IN FORM_BROWSER_FORMSET *FormSet,
865 IN FORM_BROWSER_FORM *Form,
866 IN UINT16 QuestionId
867 )
868 {
869 LIST_ENTRY *Link;
870 FORM_BROWSER_STATEMENT *Question;
871
872 //
873 // Search in the form scope first
874 //
875 Question = IdToQuestion2 (Form, QuestionId);
876 if (Question != NULL) {
877 return Question;
878 }
879
880 //
881 // Search in the formset scope
882 //
883 Link = GetFirstNode (&FormSet->FormListHead);
884 while (!IsNull (&FormSet->FormListHead, Link)) {
885 Form = FORM_BROWSER_FORM_FROM_LINK (Link);
886
887 Question = IdToQuestion2 (Form, QuestionId);
888 if (Question != NULL) {
889 //
890 // EFI variable storage may be updated by Callback() asynchronous,
891 // to keep synchronous, always reload the Question Value.
892 //
893 if (Question->Storage->Type == EFI_HII_VARSTORE_EFI_VARIABLE) {
894 GetQuestionValue (FormSet, Form, Question, GetSetValueWithHiiDriver);
895 }
896
897 return Question;
898 }
899
900 Link = GetNextNode (&FormSet->FormListHead, Link);
901 }
902
903 return NULL;
904 }
905
906
907 /**
908 Get Expression given its RuleId.
909
910 @param Form The form which contains this Expression.
911 @param RuleId Id of this Expression.
912
913 @retval Pointer The Expression.
914 @retval NULL Specified Expression not found in the form.
915
916 **/
917 FORM_EXPRESSION *
918 RuleIdToExpression (
919 IN FORM_BROWSER_FORM *Form,
920 IN UINT8 RuleId
921 )
922 {
923 LIST_ENTRY *Link;
924 FORM_EXPRESSION *Expression;
925
926 Link = GetFirstNode (&Form->ExpressionListHead);
927 while (!IsNull (&Form->ExpressionListHead, Link)) {
928 Expression = FORM_EXPRESSION_FROM_LINK (Link);
929
930 if (Expression->Type == EFI_HII_EXPRESSION_RULE && Expression->RuleId == RuleId) {
931 return Expression;
932 }
933
934 Link = GetNextNode (&Form->ExpressionListHead, Link);
935 }
936
937 return NULL;
938 }
939
940
941 /**
942 Locate the Unicode Collation Protocol interface for later use.
943
944 @retval EFI_SUCCESS Protocol interface initialize success.
945 @retval Other Protocol interface initialize failed.
946
947 **/
948 EFI_STATUS
949 InitializeUnicodeCollationProtocol (
950 VOID
951 )
952 {
953 EFI_STATUS Status;
954
955 if (mUnicodeCollation != NULL) {
956 return EFI_SUCCESS;
957 }
958
959 //
960 // BUGBUG: Proper impelmentation is to locate all Unicode Collation Protocol
961 // instances first and then select one which support English language.
962 // Current implementation just pick the first instance.
963 //
964 Status = gBS->LocateProtocol (
965 &gEfiUnicodeCollation2ProtocolGuid,
966 NULL,
967 (VOID **) &mUnicodeCollation
968 );
969 return Status;
970 }
971
972 /**
973 Convert the input Unicode character to upper.
974
975 @param String Th Unicode character to be converted.
976
977 **/
978 VOID
979 IfrStrToUpper (
980 IN CHAR16 *String
981 )
982 {
983 while (*String != 0) {
984 if ((*String >= 'a') && (*String <= 'z')) {
985 *String = (UINT16) ((*String) & ((UINT16) ~0x20));
986 }
987 String++;
988 }
989 }
990
991 /**
992 Check whether this value type can be transfer to EFI_IFR_TYPE_BUFFER type.
993
994 EFI_IFR_TYPE_REF, EFI_IFR_TYPE_DATE and EFI_IFR_TYPE_TIME are converted to
995 EFI_IFR_TYPE_BUFFER when do the value compare.
996
997 @param Value Expression value to compare on.
998
999 @retval TRUE This value type can be transter to EFI_IFR_TYPE_BUFFER type.
1000 @retval FALSE This value type can't be transter to EFI_IFR_TYPE_BUFFER type.
1001
1002 **/
1003 BOOLEAN
1004 IsTypeInBuffer (
1005 IN EFI_HII_VALUE *Value
1006 )
1007 {
1008 switch (Value->Type) {
1009 case EFI_IFR_TYPE_BUFFER:
1010 case EFI_IFR_TYPE_DATE:
1011 case EFI_IFR_TYPE_TIME:
1012 case EFI_IFR_TYPE_REF:
1013 return TRUE;
1014
1015 default:
1016 return FALSE;
1017 }
1018 }
1019
1020 /**
1021 Check whether this value type can be transfer to EFI_IFR_TYPE_UINT64
1022
1023 @param Value Expression value to compare on.
1024
1025 @retval TRUE This value type can be transter to EFI_IFR_TYPE_BUFFER type.
1026 @retval FALSE This value type can't be transter to EFI_IFR_TYPE_BUFFER type.
1027
1028 **/
1029 BOOLEAN
1030 IsTypeInUINT64 (
1031 IN EFI_HII_VALUE *Value
1032 )
1033 {
1034 switch (Value->Type) {
1035 case EFI_IFR_TYPE_NUM_SIZE_8:
1036 case EFI_IFR_TYPE_NUM_SIZE_16:
1037 case EFI_IFR_TYPE_NUM_SIZE_32:
1038 case EFI_IFR_TYPE_NUM_SIZE_64:
1039 case EFI_IFR_TYPE_BOOLEAN:
1040 return TRUE;
1041
1042 default:
1043 return FALSE;
1044 }
1045 }
1046
1047 /**
1048 Return the buffer length for this value.
1049
1050 EFI_IFR_TYPE_REF, EFI_IFR_TYPE_DATE and EFI_IFR_TYPE_TIME are converted to
1051 EFI_IFR_TYPE_BUFFER when do the value compare.
1052
1053 @param Value Expression value to compare on.
1054
1055 @retval BufLen Return the buffer length.
1056
1057 **/
1058 UINT16
1059 GetLengthForValue (
1060 IN EFI_HII_VALUE *Value
1061 )
1062 {
1063 switch (Value->Type) {
1064 case EFI_IFR_TYPE_BUFFER:
1065 return Value->BufferLen;
1066
1067 case EFI_IFR_TYPE_DATE:
1068 return (UINT16) sizeof (EFI_HII_DATE);
1069
1070 case EFI_IFR_TYPE_TIME:
1071 return (UINT16) sizeof (EFI_HII_TIME);
1072
1073 case EFI_IFR_TYPE_REF:
1074 return (UINT16) sizeof (EFI_HII_REF);
1075
1076 default:
1077 return 0;
1078 }
1079 }
1080
1081 /**
1082 Return the buffer pointer for this value.
1083
1084 EFI_IFR_TYPE_REF, EFI_IFR_TYPE_DATE and EFI_IFR_TYPE_TIME are converted to
1085 EFI_IFR_TYPE_BUFFER when do the value compare.
1086
1087 @param Value Expression value to compare on.
1088
1089 @retval Buf Return the buffer pointer.
1090
1091 **/
1092 UINT8 *
1093 GetBufferForValue (
1094 IN EFI_HII_VALUE *Value
1095 )
1096 {
1097 switch (Value->Type) {
1098 case EFI_IFR_TYPE_BUFFER:
1099 return Value->Buffer;
1100
1101 case EFI_IFR_TYPE_DATE:
1102 return (UINT8 *) (&Value->Value.date);
1103
1104 case EFI_IFR_TYPE_TIME:
1105 return (UINT8 *) (&Value->Value.time);
1106
1107 case EFI_IFR_TYPE_REF:
1108 return (UINT8 *) (&Value->Value.ref);
1109
1110 default:
1111 return NULL;
1112 }
1113 }
1114
1115 /**
1116 Evaluate opcode EFI_IFR_TO_STRING.
1117
1118 @param FormSet Formset which contains this opcode.
1119 @param Format String format in EFI_IFR_TO_STRING.
1120 @param Result Evaluation result for this opcode.
1121
1122 @retval EFI_SUCCESS Opcode evaluation success.
1123 @retval Other Opcode evaluation failed.
1124
1125 **/
1126 EFI_STATUS
1127 IfrToString (
1128 IN FORM_BROWSER_FORMSET *FormSet,
1129 IN UINT8 Format,
1130 OUT EFI_HII_VALUE *Result
1131 )
1132 {
1133 EFI_STATUS Status;
1134 EFI_HII_VALUE Value;
1135 CHAR16 *String;
1136 CHAR16 *PrintFormat;
1137 CHAR16 Buffer[MAXIMUM_VALUE_CHARACTERS];
1138 UINT8 *TmpBuf;
1139 UINT8 *SrcBuf;
1140 UINTN SrcLen;
1141 UINTN BufferSize;
1142
1143 Status = PopExpression (&Value);
1144 if (EFI_ERROR (Status)) {
1145 return Status;
1146 }
1147
1148 switch (Value.Type) {
1149 case EFI_IFR_TYPE_NUM_SIZE_8:
1150 case EFI_IFR_TYPE_NUM_SIZE_16:
1151 case EFI_IFR_TYPE_NUM_SIZE_32:
1152 case EFI_IFR_TYPE_NUM_SIZE_64:
1153 BufferSize = MAXIMUM_VALUE_CHARACTERS * sizeof (CHAR16);
1154 switch (Format) {
1155 case EFI_IFR_STRING_UNSIGNED_DEC:
1156 case EFI_IFR_STRING_SIGNED_DEC:
1157 PrintFormat = L"%ld";
1158 break;
1159
1160 case EFI_IFR_STRING_LOWERCASE_HEX:
1161 PrintFormat = L"%lx";
1162 break;
1163
1164 case EFI_IFR_STRING_UPPERCASE_HEX:
1165 PrintFormat = L"%lX";
1166 break;
1167
1168 default:
1169 Result->Type = EFI_IFR_TYPE_UNDEFINED;
1170 return EFI_SUCCESS;
1171 }
1172 UnicodeSPrint (Buffer, BufferSize, PrintFormat, Value.Value.u64);
1173 String = Buffer;
1174 break;
1175
1176 case EFI_IFR_TYPE_STRING:
1177 CopyMem (Result, &Value, sizeof (EFI_HII_VALUE));
1178 return EFI_SUCCESS;
1179
1180 case EFI_IFR_TYPE_BOOLEAN:
1181 String = (Value.Value.b) ? L"True" : L"False";
1182 break;
1183
1184 case EFI_IFR_TYPE_BUFFER:
1185 case EFI_IFR_TYPE_DATE:
1186 case EFI_IFR_TYPE_TIME:
1187 case EFI_IFR_TYPE_REF:
1188 //
1189 // + 3 is base on the unicode format, the length may be odd number,
1190 // so need 1 byte to align, also need 2 bytes for L'\0'.
1191 //
1192 if (Value.Type == EFI_IFR_TYPE_BUFFER) {
1193 SrcLen = Value.BufferLen;
1194 SrcBuf = Value.Buffer;
1195 } else {
1196 SrcBuf = GetBufferForValue(&Value);
1197 SrcLen = GetLengthForValue(&Value);
1198 }
1199
1200 TmpBuf = AllocateZeroPool (SrcLen + 3);
1201 ASSERT (TmpBuf != NULL);
1202 if (Format == EFI_IFR_STRING_ASCII) {
1203 CopyMem (TmpBuf, SrcBuf, SrcLen);
1204 PrintFormat = L"%a";
1205 } else {
1206 // Format == EFI_IFR_STRING_UNICODE
1207 CopyMem (TmpBuf, SrcBuf, SrcLen * sizeof (CHAR16));
1208 PrintFormat = L"%s";
1209 }
1210 UnicodeSPrint (Buffer, sizeof (Buffer), PrintFormat, TmpBuf);
1211 String = Buffer;
1212 FreePool (TmpBuf);
1213 if (Value.Type == EFI_IFR_TYPE_BUFFER) {
1214 FreePool (Value.Buffer);
1215 }
1216 break;
1217
1218 default:
1219 Result->Type = EFI_IFR_TYPE_UNDEFINED;
1220 return EFI_SUCCESS;
1221 }
1222
1223 Result->Type = EFI_IFR_TYPE_STRING;
1224 Result->Value.string = NewString (String, FormSet->HiiHandle);
1225 return EFI_SUCCESS;
1226 }
1227
1228
1229 /**
1230 Evaluate opcode EFI_IFR_TO_UINT.
1231
1232 @param FormSet Formset which contains this opcode.
1233 @param Result Evaluation result for this opcode.
1234
1235 @retval EFI_SUCCESS Opcode evaluation success.
1236 @retval Other Opcode evaluation failed.
1237
1238 **/
1239 EFI_STATUS
1240 IfrToUint (
1241 IN FORM_BROWSER_FORMSET *FormSet,
1242 OUT EFI_HII_VALUE *Result
1243 )
1244 {
1245 EFI_STATUS Status;
1246 EFI_HII_VALUE Value;
1247 CHAR16 *String;
1248 CHAR16 *StringPtr;
1249
1250 Status = PopExpression (&Value);
1251 if (EFI_ERROR (Status)) {
1252 return Status;
1253 }
1254
1255 if (Value.Type >= EFI_IFR_TYPE_OTHER && !IsTypeInBuffer(&Value)) {
1256 Result->Type = EFI_IFR_TYPE_UNDEFINED;
1257 return EFI_SUCCESS;
1258 }
1259
1260 Status = EFI_SUCCESS;
1261 if (Value.Type == EFI_IFR_TYPE_STRING) {
1262 String = GetToken (Value.Value.string, FormSet->HiiHandle);
1263 if (String == NULL) {
1264 return EFI_NOT_FOUND;
1265 }
1266
1267 IfrStrToUpper (String);
1268 StringPtr = StrStr (String, L"0X");
1269 if (StringPtr != NULL) {
1270 //
1271 // Hex string
1272 //
1273 Result->Value.u64 = StrHexToUint64 (String);
1274 } else {
1275 //
1276 // decimal string
1277 //
1278 Result->Value.u64 = StrDecimalToUint64 (String);
1279 }
1280 FreePool (String);
1281 } else if (IsTypeInBuffer(&Value)) {
1282 if (GetLengthForValue (&Value) > 8) {
1283 if (Value.Type == EFI_IFR_TYPE_BUFFER) {
1284 FreePool (Value.Buffer);
1285 }
1286 Result->Type = EFI_IFR_TYPE_UNDEFINED;
1287 return EFI_SUCCESS;
1288 }
1289 Result->Value.u64 = *(UINT64*) GetBufferForValue (&Value);
1290 if (Value.Type == EFI_IFR_TYPE_BUFFER) {
1291 FreePool (Value.Buffer);
1292 }
1293 } else {
1294 CopyMem (Result, &Value, sizeof (EFI_HII_VALUE));
1295 }
1296
1297 Result->Type = EFI_IFR_TYPE_NUM_SIZE_64;
1298 return Status;
1299 }
1300
1301
1302 /**
1303 Evaluate opcode EFI_IFR_CATENATE.
1304
1305 @param FormSet Formset which contains this opcode.
1306 @param Result Evaluation result for this opcode.
1307
1308 @retval EFI_SUCCESS Opcode evaluation success.
1309 @retval Other Opcode evaluation failed.
1310
1311 **/
1312 EFI_STATUS
1313 IfrCatenate (
1314 IN FORM_BROWSER_FORMSET *FormSet,
1315 OUT EFI_HII_VALUE *Result
1316 )
1317 {
1318 EFI_STATUS Status;
1319 EFI_HII_VALUE Value[2];
1320 CHAR16 *String[2];
1321 UINTN Index;
1322 CHAR16 *StringPtr;
1323 UINTN Size;
1324 UINT16 Length0;
1325 UINT16 Length1;
1326 UINT8 *TmpBuf;
1327
1328 //
1329 // String[0] - The second string
1330 // String[1] - The first string
1331 //
1332 String[0] = NULL;
1333 String[1] = NULL;
1334 StringPtr = NULL;
1335 Status = EFI_SUCCESS;
1336 ZeroMem (Value, sizeof (Value));
1337
1338 Status = PopExpression (&Value[0]);
1339 if (EFI_ERROR (Status)) {
1340 goto Done;
1341 }
1342
1343 Status = PopExpression (&Value[1]);
1344 if (EFI_ERROR (Status)) {
1345 goto Done;
1346 }
1347
1348 for (Index = 0; Index < 2; Index++) {
1349 if (Value[Index].Type != EFI_IFR_TYPE_STRING && !IsTypeInBuffer(&Value[Index])) {
1350 Result->Type = EFI_IFR_TYPE_UNDEFINED;
1351 Status = EFI_SUCCESS;
1352 goto Done;
1353 }
1354
1355 if (Value[Index].Type == EFI_IFR_TYPE_STRING) {
1356 String[Index] = GetToken (Value[Index].Value.string, FormSet->HiiHandle);
1357 if (String[Index] == NULL) {
1358 Status = EFI_NOT_FOUND;
1359 goto Done;
1360 }
1361 }
1362 }
1363
1364 if (Value[0].Type == EFI_IFR_TYPE_STRING) {
1365 Size = StrSize (String[0]);
1366 StringPtr= AllocatePool (StrSize (String[1]) + Size);
1367 ASSERT (StringPtr != NULL);
1368 StrCpy (StringPtr, String[1]);
1369 StrCat (StringPtr, String[0]);
1370
1371 Result->Type = EFI_IFR_TYPE_STRING;
1372 Result->Value.string = NewString (StringPtr, FormSet->HiiHandle);
1373 } else {
1374 Result->Type = EFI_IFR_TYPE_BUFFER;
1375 Length0 = GetLengthForValue(&Value[0]);
1376 Length1 = GetLengthForValue(&Value[1]);
1377 Result->BufferLen = (UINT16) (Length0 + Length1);
1378
1379 Result->Buffer = AllocateZeroPool (Result->BufferLen);
1380 ASSERT (Result->Buffer != NULL);
1381
1382 TmpBuf = GetBufferForValue(&Value[0]);
1383 ASSERT (TmpBuf != NULL);
1384 CopyMem (Result->Buffer, TmpBuf, Length0);
1385 TmpBuf = GetBufferForValue(&Value[1]);
1386 ASSERT (TmpBuf != NULL);
1387 CopyMem (&Result->Buffer[Length0], TmpBuf, Length1);
1388 }
1389 Done:
1390 if (Value[0].Buffer != NULL) {
1391 FreePool (Value[0].Buffer);
1392 }
1393 if (Value[1].Buffer != NULL) {
1394 FreePool (Value[1].Buffer);
1395 }
1396 if (String[0] != NULL) {
1397 FreePool (String[0]);
1398 }
1399 if (String[1] != NULL) {
1400 FreePool (String[1]);
1401 }
1402 if (StringPtr != NULL) {
1403 FreePool (StringPtr);
1404 }
1405
1406 return Status;
1407 }
1408
1409
1410 /**
1411 Evaluate opcode EFI_IFR_MATCH.
1412
1413 @param FormSet Formset which contains this opcode.
1414 @param Result Evaluation result for this opcode.
1415
1416 @retval EFI_SUCCESS Opcode evaluation success.
1417 @retval Other Opcode evaluation failed.
1418
1419 **/
1420 EFI_STATUS
1421 IfrMatch (
1422 IN FORM_BROWSER_FORMSET *FormSet,
1423 OUT EFI_HII_VALUE *Result
1424 )
1425 {
1426 EFI_STATUS Status;
1427 EFI_HII_VALUE Value[2];
1428 CHAR16 *String[2];
1429 UINTN Index;
1430
1431 //
1432 // String[0] - The string to search
1433 // String[1] - pattern
1434 //
1435 String[0] = NULL;
1436 String[1] = NULL;
1437 Status = EFI_SUCCESS;
1438 ZeroMem (Value, sizeof (Value));
1439
1440 Status = PopExpression (&Value[0]);
1441 if (EFI_ERROR (Status)) {
1442 goto Done;
1443 }
1444
1445 Status = PopExpression (&Value[1]);
1446 if (EFI_ERROR (Status)) {
1447 goto Done;
1448 }
1449
1450 for (Index = 0; Index < 2; Index++) {
1451 if (Value[Index].Type != EFI_IFR_TYPE_STRING) {
1452 Result->Type = EFI_IFR_TYPE_UNDEFINED;
1453 Status = EFI_SUCCESS;
1454 goto Done;
1455 }
1456
1457 String[Index] = GetToken (Value[Index].Value.string, FormSet->HiiHandle);
1458 if (String [Index] == NULL) {
1459 Status = EFI_NOT_FOUND;
1460 goto Done;
1461 }
1462 }
1463
1464 Result->Type = EFI_IFR_TYPE_BOOLEAN;
1465 Result->Value.b = mUnicodeCollation->MetaiMatch (mUnicodeCollation, String[0], String[1]);
1466
1467 Done:
1468 if (String[0] != NULL) {
1469 FreePool (String[0]);
1470 }
1471 if (String[1] != NULL) {
1472 FreePool (String[1]);
1473 }
1474
1475 return Status;
1476 }
1477
1478 /**
1479 Evaluate opcode EFI_IFR_MATCH2.
1480
1481 @param FormSet Formset which contains this opcode.
1482 @param SyntaxType Syntax type for match2.
1483 @param Result Evaluation result for this opcode.
1484
1485 @retval EFI_SUCCESS Opcode evaluation success.
1486 @retval Other Opcode evaluation failed.
1487
1488 **/
1489 EFI_STATUS
1490 IfrMatch2 (
1491 IN FORM_BROWSER_FORMSET *FormSet,
1492 IN EFI_GUID *SyntaxType,
1493 OUT EFI_HII_VALUE *Result
1494 )
1495 {
1496 EFI_STATUS Status;
1497 EFI_HII_VALUE Value[2];
1498 CHAR16 *String[2];
1499 UINTN Index;
1500 UINTN GuidIndex;
1501 EFI_HANDLE *HandleBuffer;
1502 UINTN BufferSize;
1503 EFI_REGULAR_EXPRESSION_PROTOCOL *RegularExpressionProtocol;
1504 UINTN RegExSyntaxTypeListSize;
1505 EFI_REGEX_SYNTAX_TYPE *RegExSyntaxTypeList;
1506 UINTN CapturesCount;
1507
1508 //
1509 // String[0] - The string to search
1510 // String[1] - pattern
1511 //
1512 String[0] = NULL;
1513 String[1] = NULL;
1514 HandleBuffer = NULL;
1515 RegExSyntaxTypeList = NULL;
1516 Status = EFI_SUCCESS;
1517 ZeroMem (Value, sizeof (Value));
1518
1519 Status = PopExpression (&Value[0]);
1520 if (EFI_ERROR (Status)) {
1521 goto Done;
1522 }
1523
1524 Status = PopExpression (&Value[1]);
1525 if (EFI_ERROR (Status)) {
1526 goto Done;
1527 }
1528
1529 for (Index = 0; Index < 2; Index++) {
1530 if (Value[Index].Type != EFI_IFR_TYPE_STRING) {
1531 Result->Type = EFI_IFR_TYPE_UNDEFINED;
1532 Status = EFI_SUCCESS;
1533 goto Done;
1534 }
1535
1536 String[Index] = GetToken (Value[Index].Value.string, FormSet->HiiHandle);
1537 if (String [Index] == NULL) {
1538 Status = EFI_NOT_FOUND;
1539 goto Done;
1540 }
1541 }
1542
1543 BufferSize = 0;
1544 HandleBuffer = NULL;
1545 Status = gBS->LocateHandle(
1546 ByProtocol,
1547 &gEfiRegularExpressionProtocolGuid,
1548 NULL,
1549 &BufferSize,
1550 HandleBuffer);
1551 if (Status == EFI_BUFFER_TOO_SMALL) {
1552 HandleBuffer = AllocateZeroPool(BufferSize);
1553 if (HandleBuffer == NULL) {
1554 Status = EFI_OUT_OF_RESOURCES;
1555 goto Done;
1556 }
1557 Status = gBS->LocateHandle(
1558 ByProtocol,
1559 &gEfiRegularExpressionProtocolGuid,
1560 NULL,
1561 &BufferSize,
1562 HandleBuffer);
1563
1564 } else if (EFI_ERROR (Status)) {
1565 Value->Type = EFI_IFR_TYPE_UNDEFINED;
1566 Status = EFI_SUCCESS;
1567 goto Done;
1568 }
1569
1570 for ( Index = 0; Index < BufferSize / sizeof(EFI_HANDLE); Index ++) {
1571 Status = gBS->HandleProtocol (
1572 HandleBuffer[Index],
1573 &gEfiRegularExpressionProtocolGuid,
1574 (VOID**)&RegularExpressionProtocol
1575 );
1576 if (EFI_ERROR (Status)) {
1577 goto Done;
1578 }
1579
1580 RegExSyntaxTypeListSize = 0;
1581 RegExSyntaxTypeList = NULL;
1582
1583 Status = RegularExpressionProtocol->GetInfo (
1584 RegularExpressionProtocol,
1585 &RegExSyntaxTypeListSize,
1586 RegExSyntaxTypeList
1587 );
1588 if (Status == EFI_BUFFER_TOO_SMALL) {
1589 RegExSyntaxTypeList = AllocateZeroPool(RegExSyntaxTypeListSize);
1590 if (RegExSyntaxTypeList == NULL) {
1591 Status = EFI_OUT_OF_RESOURCES;
1592 goto Done;
1593 }
1594 Status = RegularExpressionProtocol->GetInfo (
1595 RegularExpressionProtocol,
1596 &RegExSyntaxTypeListSize,
1597 RegExSyntaxTypeList
1598 );
1599 } else if (EFI_ERROR (Status)) {
1600 goto Done;
1601 }
1602
1603 for (GuidIndex = 0; GuidIndex < RegExSyntaxTypeListSize / sizeof(EFI_GUID); GuidIndex++) {
1604 if (CompareGuid (&RegExSyntaxTypeList[GuidIndex], SyntaxType)) {
1605 //
1606 // Find the match type, return the value.
1607 //
1608 Result->Type = EFI_IFR_TYPE_BOOLEAN;
1609 Status = RegularExpressionProtocol->MatchString (
1610 RegularExpressionProtocol,
1611 String[0],
1612 String[1],
1613 SyntaxType,
1614 &Result->Value.b,
1615 NULL,
1616 &CapturesCount
1617 );
1618 goto Done;
1619 }
1620 }
1621
1622 if (RegExSyntaxTypeList != NULL) {
1623 FreePool (RegExSyntaxTypeList);
1624 }
1625 }
1626
1627 //
1628 // Type specified by SyntaxType is not supported
1629 // in any of the EFI_REGULAR_EXPRESSION_PROTOCOL instances.
1630 //
1631 Value->Type = EFI_IFR_TYPE_UNDEFINED;
1632 Status = EFI_SUCCESS;
1633
1634 Done:
1635 if (String[0] != NULL) {
1636 FreePool (String[0]);
1637 }
1638 if (String[1] != NULL) {
1639 FreePool (String[1]);
1640 }
1641 if (RegExSyntaxTypeList != NULL) {
1642 FreePool (RegExSyntaxTypeList);
1643 }
1644 if (HandleBuffer != NULL) {
1645 FreePool (HandleBuffer);
1646 }
1647 return Status;
1648 }
1649
1650 /**
1651 Evaluate opcode EFI_IFR_FIND.
1652
1653 @param FormSet Formset which contains this opcode.
1654 @param Format Case sensitive or insensitive.
1655 @param Result Evaluation result for this opcode.
1656
1657 @retval EFI_SUCCESS Opcode evaluation success.
1658 @retval Other Opcode evaluation failed.
1659
1660 **/
1661 EFI_STATUS
1662 IfrFind (
1663 IN FORM_BROWSER_FORMSET *FormSet,
1664 IN UINT8 Format,
1665 OUT EFI_HII_VALUE *Result
1666 )
1667 {
1668 EFI_STATUS Status;
1669 EFI_HII_VALUE Value[3];
1670 CHAR16 *String[2];
1671 UINTN Base;
1672 CHAR16 *StringPtr;
1673 UINTN Index;
1674
1675 ZeroMem (Value, sizeof (Value));
1676
1677 if (Format > EFI_IFR_FF_CASE_INSENSITIVE) {
1678 return EFI_INVALID_PARAMETER;
1679 }
1680
1681 Status = PopExpression (&Value[0]);
1682 if (EFI_ERROR (Status)) {
1683 return Status;
1684 }
1685
1686 Status = PopExpression (&Value[1]);
1687 if (EFI_ERROR (Status)) {
1688 return Status;
1689 }
1690
1691 Status = PopExpression (&Value[2]);
1692 if (EFI_ERROR (Status)) {
1693 return Status;
1694 }
1695
1696 if (Value[0].Type > EFI_IFR_TYPE_NUM_SIZE_64) {
1697 Result->Type = EFI_IFR_TYPE_UNDEFINED;
1698 return EFI_SUCCESS;
1699 }
1700 Base = (UINTN) Value[0].Value.u64;
1701
1702 //
1703 // String[0] - sub-string
1704 // String[1] - The string to search
1705 //
1706 String[0] = NULL;
1707 String[1] = NULL;
1708 for (Index = 0; Index < 2; Index++) {
1709 if (Value[Index + 1].Type != EFI_IFR_TYPE_STRING) {
1710 Result->Type = EFI_IFR_TYPE_UNDEFINED;
1711 Status = EFI_SUCCESS;
1712 goto Done;
1713 }
1714
1715 String[Index] = GetToken (Value[Index + 1].Value.string, FormSet->HiiHandle);
1716 if (String[Index] == NULL) {
1717 Status = EFI_NOT_FOUND;
1718 goto Done;
1719 }
1720
1721 if (Format == EFI_IFR_FF_CASE_INSENSITIVE) {
1722 //
1723 // Case insensitive, convert both string to upper case
1724 //
1725 IfrStrToUpper (String[Index]);
1726 }
1727 }
1728
1729 Result->Type = EFI_IFR_TYPE_NUM_SIZE_64;
1730 if (Base >= StrLen (String[1])) {
1731 Result->Value.u64 = 0xFFFFFFFFFFFFFFFFULL;
1732 } else {
1733 StringPtr = StrStr (String[1] + Base, String[0]);
1734 Result->Value.u64 = (StringPtr == NULL) ? 0xFFFFFFFFFFFFFFFFULL : (StringPtr - String[1]);
1735 }
1736
1737 Done:
1738 if (String[0] != NULL) {
1739 FreePool (String[0]);
1740 }
1741 if (String[1] != NULL) {
1742 FreePool (String[1]);
1743 }
1744
1745 return Status;
1746 }
1747
1748
1749 /**
1750 Evaluate opcode EFI_IFR_MID.
1751
1752 @param FormSet Formset which contains this opcode.
1753 @param Result Evaluation result for this opcode.
1754
1755 @retval EFI_SUCCESS Opcode evaluation success.
1756 @retval Other Opcode evaluation failed.
1757
1758 **/
1759 EFI_STATUS
1760 IfrMid (
1761 IN FORM_BROWSER_FORMSET *FormSet,
1762 OUT EFI_HII_VALUE *Result
1763 )
1764 {
1765 EFI_STATUS Status;
1766 EFI_HII_VALUE Value[3];
1767 CHAR16 *String;
1768 UINTN Base;
1769 UINTN Length;
1770 CHAR16 *SubString;
1771 UINT16 BufferLen;
1772 UINT8 *Buffer;
1773
1774 ZeroMem (Value, sizeof (Value));
1775
1776 Status = PopExpression (&Value[0]);
1777 if (EFI_ERROR (Status)) {
1778 return Status;
1779 }
1780
1781 Status = PopExpression (&Value[1]);
1782 if (EFI_ERROR (Status)) {
1783 return Status;
1784 }
1785
1786 Status = PopExpression (&Value[2]);
1787 if (EFI_ERROR (Status)) {
1788 return Status;
1789 }
1790
1791 if (Value[0].Type > EFI_IFR_TYPE_NUM_SIZE_64) {
1792 Result->Type = EFI_IFR_TYPE_UNDEFINED;
1793 return EFI_SUCCESS;
1794 }
1795 Length = (UINTN) Value[0].Value.u64;
1796
1797 if (Value[1].Type > EFI_IFR_TYPE_NUM_SIZE_64) {
1798 Result->Type = EFI_IFR_TYPE_UNDEFINED;
1799 return EFI_SUCCESS;
1800 }
1801 Base = (UINTN) Value[1].Value.u64;
1802
1803 if (Value[2].Type != EFI_IFR_TYPE_STRING && !IsTypeInBuffer(&Value[2])) {
1804 Result->Type = EFI_IFR_TYPE_UNDEFINED;
1805 return EFI_SUCCESS;
1806 }
1807 if (Value[2].Type == EFI_IFR_TYPE_STRING) {
1808 String = GetToken (Value[2].Value.string, FormSet->HiiHandle);
1809 if (String == NULL) {
1810 return EFI_NOT_FOUND;
1811 }
1812
1813 if (Length == 0 || Base >= StrLen (String)) {
1814 SubString = gEmptyString;
1815 } else {
1816 SubString = String + Base;
1817 if ((Base + Length) < StrLen (String)) {
1818 SubString[Length] = L'\0';
1819 }
1820 }
1821
1822 Result->Type = EFI_IFR_TYPE_STRING;
1823 Result->Value.string = NewString (SubString, FormSet->HiiHandle);
1824
1825 FreePool (String);
1826 } else {
1827 BufferLen = GetLengthForValue (&Value[2]);
1828 Buffer = GetBufferForValue (&Value[2]);
1829
1830 Result->Type = EFI_IFR_TYPE_BUFFER;
1831 if (Length == 0 || Base >= BufferLen) {
1832 Result->BufferLen = 0;
1833 Result->Buffer = NULL;
1834 } else {
1835 Result->BufferLen = (UINT16)((BufferLen - Base) < Length ? (BufferLen - Base) : Length);
1836 Result->Buffer = AllocateZeroPool (Result->BufferLen);
1837 ASSERT (Result->Buffer != NULL);
1838 CopyMem (Result->Buffer, &Buffer[Base], Result->BufferLen);
1839 }
1840
1841 if (Value[2].Type == EFI_IFR_TYPE_BUFFER) {
1842 FreePool (Value[2].Buffer);
1843 }
1844 }
1845
1846 return Status;
1847 }
1848
1849
1850 /**
1851 Evaluate opcode EFI_IFR_TOKEN.
1852
1853 @param FormSet Formset which contains this opcode.
1854 @param Result Evaluation result for this opcode.
1855
1856 @retval EFI_SUCCESS Opcode evaluation success.
1857 @retval Other Opcode evaluation failed.
1858
1859 **/
1860 EFI_STATUS
1861 IfrToken (
1862 IN FORM_BROWSER_FORMSET *FormSet,
1863 OUT EFI_HII_VALUE *Result
1864 )
1865 {
1866 EFI_STATUS Status;
1867 EFI_HII_VALUE Value[3];
1868 CHAR16 *String[2];
1869 UINTN Count;
1870 CHAR16 *Delimiter;
1871 CHAR16 *SubString;
1872 CHAR16 *StringPtr;
1873 UINTN Index;
1874
1875 ZeroMem (Value, sizeof (Value));
1876
1877 Status = PopExpression (&Value[0]);
1878 if (EFI_ERROR (Status)) {
1879 return Status;
1880 }
1881
1882 Status = PopExpression (&Value[1]);
1883 if (EFI_ERROR (Status)) {
1884 return Status;
1885 }
1886
1887 Status = PopExpression (&Value[2]);
1888 if (EFI_ERROR (Status)) {
1889 return Status;
1890 }
1891
1892 if (Value[0].Type > EFI_IFR_TYPE_NUM_SIZE_64) {
1893 Result->Type = EFI_IFR_TYPE_UNDEFINED;
1894 return EFI_SUCCESS;
1895 }
1896 Count = (UINTN) Value[0].Value.u64;
1897
1898 //
1899 // String[0] - Delimiter
1900 // String[1] - The string to search
1901 //
1902 String[0] = NULL;
1903 String[1] = NULL;
1904 for (Index = 0; Index < 2; Index++) {
1905 if (Value[Index + 1].Type != EFI_IFR_TYPE_STRING) {
1906 Result->Type = EFI_IFR_TYPE_UNDEFINED;
1907 Status = EFI_SUCCESS;
1908 goto Done;
1909 }
1910
1911 String[Index] = GetToken (Value[Index + 1].Value.string, FormSet->HiiHandle);
1912 if (String[Index] == NULL) {
1913 Status = EFI_NOT_FOUND;
1914 goto Done;
1915 }
1916 }
1917
1918 Delimiter = String[0];
1919 SubString = String[1];
1920 while (Count > 0) {
1921 SubString = StrStr (SubString, Delimiter);
1922 if (SubString != NULL) {
1923 //
1924 // Skip over the delimiter
1925 //
1926 SubString = SubString + StrLen (Delimiter);
1927 } else {
1928 break;
1929 }
1930 Count--;
1931 }
1932
1933 if (SubString == NULL) {
1934 //
1935 // nth delimited sub-string not found, push an empty string
1936 //
1937 SubString = gEmptyString;
1938 } else {
1939 //
1940 // Put a NULL terminator for nth delimited sub-string
1941 //
1942 StringPtr = StrStr (SubString, Delimiter);
1943 if (StringPtr != NULL) {
1944 *StringPtr = L'\0';
1945 }
1946 }
1947
1948 Result->Type = EFI_IFR_TYPE_STRING;
1949 Result->Value.string = NewString (SubString, FormSet->HiiHandle);
1950
1951 Done:
1952 if (String[0] != NULL) {
1953 FreePool (String[0]);
1954 }
1955 if (String[1] != NULL) {
1956 FreePool (String[1]);
1957 }
1958
1959 return Status;
1960 }
1961
1962
1963 /**
1964 Evaluate opcode EFI_IFR_SPAN.
1965
1966 @param FormSet Formset which contains this opcode.
1967 @param Flags FIRST_MATCHING or FIRST_NON_MATCHING.
1968 @param Result Evaluation result for this opcode.
1969
1970 @retval EFI_SUCCESS Opcode evaluation success.
1971 @retval Other Opcode evaluation failed.
1972
1973 **/
1974 EFI_STATUS
1975 IfrSpan (
1976 IN FORM_BROWSER_FORMSET *FormSet,
1977 IN UINT8 Flags,
1978 OUT EFI_HII_VALUE *Result
1979 )
1980 {
1981 EFI_STATUS Status;
1982 EFI_HII_VALUE Value[3];
1983 CHAR16 *String[2];
1984 CHAR16 *Charset;
1985 UINTN Base;
1986 UINTN Index;
1987 CHAR16 *StringPtr;
1988 BOOLEAN Found;
1989
1990 ZeroMem (Value, sizeof (Value));
1991
1992 Status = PopExpression (&Value[0]);
1993 if (EFI_ERROR (Status)) {
1994 return Status;
1995 }
1996
1997 Status = PopExpression (&Value[1]);
1998 if (EFI_ERROR (Status)) {
1999 return Status;
2000 }
2001
2002 Status = PopExpression (&Value[2]);
2003 if (EFI_ERROR (Status)) {
2004 return Status;
2005 }
2006
2007 if (Value[0].Type > EFI_IFR_TYPE_NUM_SIZE_64) {
2008 Result->Type = EFI_IFR_TYPE_UNDEFINED;
2009 return EFI_SUCCESS;
2010 }
2011 Base = (UINTN) Value[0].Value.u64;
2012
2013 //
2014 // String[0] - Charset
2015 // String[1] - The string to search
2016 //
2017 String[0] = NULL;
2018 String[1] = NULL;
2019 for (Index = 0; Index < 2; Index++) {
2020 if (Value[Index + 1].Type != EFI_IFR_TYPE_STRING) {
2021 Result->Type = EFI_IFR_TYPE_UNDEFINED;
2022 Status = EFI_SUCCESS;
2023 goto Done;
2024 }
2025
2026 String[Index] = GetToken (Value[Index + 1].Value.string, FormSet->HiiHandle);
2027 if (String [Index] == NULL) {
2028 Status = EFI_NOT_FOUND;
2029 goto Done;
2030 }
2031 }
2032
2033 if (Base >= StrLen (String[1])) {
2034 Result->Type = EFI_IFR_TYPE_UNDEFINED;
2035 Status = EFI_SUCCESS;
2036 goto Done;
2037 }
2038
2039 Found = FALSE;
2040 StringPtr = String[1] + Base;
2041 Charset = String[0];
2042 while (*StringPtr != 0 && !Found) {
2043 Index = 0;
2044 while (Charset[Index] != 0) {
2045 if (*StringPtr >= Charset[Index] && *StringPtr <= Charset[Index + 1]) {
2046 if (Flags == EFI_IFR_FLAGS_FIRST_MATCHING) {
2047 Found = TRUE;
2048 break;
2049 }
2050 } else {
2051 if (Flags == EFI_IFR_FLAGS_FIRST_NON_MATCHING) {
2052 Found = TRUE;
2053 break;
2054 }
2055 }
2056 //
2057 // Skip characters pair representing low-end of a range and high-end of a range
2058 //
2059 Index += 2;
2060 }
2061
2062 if (!Found) {
2063 StringPtr++;
2064 }
2065 }
2066
2067 Result->Type = EFI_IFR_TYPE_NUM_SIZE_64;
2068 Result->Value.u64 = StringPtr - String[1];
2069
2070 Done:
2071 if (String[0] != NULL) {
2072 FreePool (String[0]);
2073 }
2074 if (String[1] != NULL) {
2075 FreePool (String[1]);
2076 }
2077
2078 return Status;
2079 }
2080
2081
2082 /**
2083 Zero extend integer/boolean/date/time to UINT64 for comparing.
2084
2085 @param Value HII Value to be converted.
2086
2087 **/
2088 VOID
2089 ExtendValueToU64 (
2090 IN EFI_HII_VALUE *Value
2091 )
2092 {
2093 UINT64 Temp;
2094
2095 Temp = 0;
2096 switch (Value->Type) {
2097 case EFI_IFR_TYPE_NUM_SIZE_8:
2098 Temp = Value->Value.u8;
2099 break;
2100
2101 case EFI_IFR_TYPE_NUM_SIZE_16:
2102 Temp = Value->Value.u16;
2103 break;
2104
2105 case EFI_IFR_TYPE_NUM_SIZE_32:
2106 Temp = Value->Value.u32;
2107 break;
2108
2109 case EFI_IFR_TYPE_BOOLEAN:
2110 Temp = Value->Value.b;
2111 break;
2112
2113 case EFI_IFR_TYPE_TIME:
2114 Temp = Value->Value.u32 & 0xffffff;
2115 break;
2116
2117 case EFI_IFR_TYPE_DATE:
2118 Temp = Value->Value.u32;
2119 break;
2120
2121 default:
2122 return;
2123 }
2124
2125 Value->Value.u64 = Temp;
2126 }
2127
2128 /**
2129 Get UINT64 type value.
2130
2131 @param Value Input Hii value.
2132
2133 @retval UINT64 Return the UINT64 type value.
2134
2135 **/
2136 UINT64
2137 HiiValueToUINT64 (
2138 IN EFI_HII_VALUE *Value
2139 )
2140 {
2141 UINT64 RetVal;
2142
2143 RetVal = 0;
2144
2145 switch (Value->Type) {
2146 case EFI_IFR_TYPE_NUM_SIZE_8:
2147 RetVal = Value->Value.u8;
2148 break;
2149
2150 case EFI_IFR_TYPE_NUM_SIZE_16:
2151 RetVal = Value->Value.u16;
2152 break;
2153
2154 case EFI_IFR_TYPE_NUM_SIZE_32:
2155 RetVal = Value->Value.u32;
2156 break;
2157
2158 case EFI_IFR_TYPE_BOOLEAN:
2159 RetVal = Value->Value.b;
2160 break;
2161
2162 case EFI_IFR_TYPE_DATE:
2163 RetVal = *(UINT64*) &Value->Value.date;
2164 break;
2165
2166 case EFI_IFR_TYPE_TIME:
2167 RetVal = (*(UINT64*) &Value->Value.time) & 0xffffff;
2168 break;
2169
2170 default:
2171 RetVal = Value->Value.u64;
2172 break;
2173 }
2174
2175 return RetVal;
2176 }
2177
2178 /**
2179 Compare two Hii value.
2180
2181 @param Value1 Expression value to compare on left-hand.
2182 @param Value2 Expression value to compare on right-hand.
2183 @param Result Return value after compare.
2184 retval 0 Two operators equal.
2185 return Positive value if Value1 is greater than Value2.
2186 retval Negative value if Value1 is less than Value2.
2187 @param HiiHandle Only required for string compare.
2188
2189 @retval other Could not perform compare on two values.
2190 @retval EFI_SUCCESS Compare the value success.
2191
2192 **/
2193 EFI_STATUS
2194 CompareHiiValue (
2195 IN EFI_HII_VALUE *Value1,
2196 IN EFI_HII_VALUE *Value2,
2197 OUT INTN *Result,
2198 IN EFI_HII_HANDLE HiiHandle OPTIONAL
2199 )
2200 {
2201 INT64 Temp64;
2202 CHAR16 *Str1;
2203 CHAR16 *Str2;
2204 UINTN Len;
2205 UINT8 *Buf1;
2206 UINT16 Buf1Len;
2207 UINT8 *Buf2;
2208 UINT16 Buf2Len;
2209
2210 if (Value1->Type == EFI_IFR_TYPE_STRING && Value2->Type == EFI_IFR_TYPE_STRING) {
2211 if (Value1->Value.string == 0 || Value2->Value.string == 0) {
2212 //
2213 // StringId 0 is reserved
2214 //
2215 return EFI_INVALID_PARAMETER;
2216 }
2217
2218 if (Value1->Value.string == Value2->Value.string) {
2219 *Result = 0;
2220 return EFI_SUCCESS;
2221 }
2222
2223 Str1 = GetToken (Value1->Value.string, HiiHandle);
2224 if (Str1 == NULL) {
2225 //
2226 // String not found
2227 //
2228 return EFI_NOT_FOUND;
2229 }
2230
2231 Str2 = GetToken (Value2->Value.string, HiiHandle);
2232 if (Str2 == NULL) {
2233 FreePool (Str1);
2234 return EFI_NOT_FOUND;
2235 }
2236
2237 *Result = StrCmp (Str1, Str2);
2238
2239 FreePool (Str1);
2240 FreePool (Str2);
2241
2242 return EFI_SUCCESS;
2243 }
2244
2245 //
2246 // Take types(date, time, ref, buffer) as buffer
2247 //
2248 if (IsTypeInBuffer(Value1) && IsTypeInBuffer(Value2)) {
2249 Buf1 = GetBufferForValue(Value1);
2250 Buf1Len = GetLengthForValue(Value1);
2251 Buf2 = GetBufferForValue(Value2);
2252 Buf2Len = GetLengthForValue(Value2);
2253
2254 Len = Buf1Len > Buf2Len ? Buf2Len : Buf1Len;
2255 *Result = CompareMem (Buf1, Buf2, Len);
2256 if ((*Result == 0) && (Buf1Len != Buf2Len)) {
2257 //
2258 // In this case, means base on samll number buffer, the data is same
2259 // So which value has more data, which value is bigger.
2260 //
2261 *Result = Buf1Len > Buf2Len ? 1 : -1;
2262 }
2263 return EFI_SUCCESS;
2264 }
2265
2266 //
2267 // Take types(integer, boolean) as integer
2268 //
2269 if (IsTypeInUINT64(Value1) && IsTypeInUINT64(Value2)) {
2270 Temp64 = HiiValueToUINT64(Value1) - HiiValueToUINT64(Value2);
2271 if (Temp64 > 0) {
2272 *Result = 1;
2273 } else if (Temp64 < 0) {
2274 *Result = -1;
2275 } else {
2276 *Result = 0;
2277 }
2278
2279 return EFI_SUCCESS;
2280 }
2281
2282 return EFI_UNSUPPORTED;
2283 }
2284
2285 /**
2286 Check if current user has the privilege specified by the permissions GUID.
2287
2288 @param[in] Guid A GUID specifying setup access permissions.
2289
2290 @retval TRUE Current user has the privilege.
2291 @retval FALSE Current user does not have the privilege.
2292 **/
2293 BOOLEAN
2294 CheckUserPrivilege (
2295 IN EFI_GUID *Guid
2296 )
2297 {
2298 EFI_STATUS Status;
2299 EFI_USER_PROFILE_HANDLE UserProfileHandle;
2300 EFI_USER_INFO_HANDLE UserInfoHandle;
2301 EFI_USER_INFO *UserInfo;
2302 EFI_GUID *UserPermissionsGuid;
2303 UINTN UserInfoSize;
2304 UINTN AccessControlDataSize;
2305 EFI_USER_INFO_ACCESS_CONTROL *AccessControl;
2306 UINTN RemainSize;
2307
2308 if (mUserManager == NULL) {
2309 Status = gBS->LocateProtocol (
2310 &gEfiUserManagerProtocolGuid,
2311 NULL,
2312 (VOID **) &mUserManager
2313 );
2314 if (EFI_ERROR (Status)) {
2315 ///
2316 /// If the system does not support user management, then it is assumed that
2317 /// all users have admin privilege and evaluation of each EFI_IFR_SECURITY
2318 /// op-code is always TRUE.
2319 ///
2320 return TRUE;
2321 }
2322 }
2323
2324 Status = mUserManager->Current (mUserManager, &UserProfileHandle);
2325 ASSERT_EFI_ERROR (Status);
2326
2327 ///
2328 /// Enumerate all user information of the current user profile
2329 /// to look for any EFI_USER_INFO_ACCESS_SETUP record.
2330 ///
2331
2332 for (UserInfoHandle = NULL;;) {
2333 Status = mUserManager->GetNextInfo (mUserManager, UserProfileHandle, &UserInfoHandle);
2334 if (EFI_ERROR (Status)) {
2335 break;
2336 }
2337
2338 UserInfoSize = 0;
2339 Status = mUserManager->GetInfo (mUserManager, UserProfileHandle, UserInfoHandle, NULL, &UserInfoSize);
2340 if (Status != EFI_BUFFER_TOO_SMALL) {
2341 continue;
2342 }
2343
2344 UserInfo = (EFI_USER_INFO *) AllocatePool (UserInfoSize);
2345 if (UserInfo == NULL) {
2346 break;
2347 }
2348
2349 Status = mUserManager->GetInfo (mUserManager, UserProfileHandle, UserInfoHandle, UserInfo, &UserInfoSize);
2350 if (EFI_ERROR (Status) ||
2351 UserInfo->InfoType != EFI_USER_INFO_ACCESS_POLICY_RECORD ||
2352 UserInfo->InfoSize <= sizeof (EFI_USER_INFO)) {
2353 FreePool (UserInfo);
2354 continue;
2355 }
2356
2357 RemainSize = UserInfo->InfoSize - sizeof (EFI_USER_INFO);
2358 AccessControl = (EFI_USER_INFO_ACCESS_CONTROL *)(UserInfo + 1);
2359 while (RemainSize >= sizeof (EFI_USER_INFO_ACCESS_CONTROL)) {
2360 if (RemainSize < AccessControl->Size || AccessControl->Size < sizeof (EFI_USER_INFO_ACCESS_CONTROL)) {
2361 break;
2362 }
2363 if (AccessControl->Type == EFI_USER_INFO_ACCESS_SETUP) {
2364 ///
2365 /// Check if current user has the privilege specified by the permissions GUID.
2366 ///
2367
2368 UserPermissionsGuid = (EFI_GUID *)(AccessControl + 1);
2369 AccessControlDataSize = AccessControl->Size - sizeof (EFI_USER_INFO_ACCESS_CONTROL);
2370 while (AccessControlDataSize >= sizeof (EFI_GUID)) {
2371 if (CompareGuid (Guid, UserPermissionsGuid)) {
2372 FreePool (UserInfo);
2373 return TRUE;
2374 }
2375 UserPermissionsGuid++;
2376 AccessControlDataSize -= sizeof (EFI_GUID);
2377 }
2378 }
2379 RemainSize -= AccessControl->Size;
2380 AccessControl = (EFI_USER_INFO_ACCESS_CONTROL *)((UINT8 *)AccessControl + AccessControl->Size);
2381 }
2382
2383 FreePool (UserInfo);
2384 }
2385 return FALSE;
2386 }
2387
2388 /**
2389 Get question value from the predefined formset.
2390
2391 @param DevicePath The driver's device path which produece the formset data.
2392 @param InputHiiHandle The hii handle associate with the formset data.
2393 @param FormSetGuid The formset guid which include the question.
2394 @param QuestionId The question id which need to get value from.
2395 @param Value The return data about question's value.
2396
2397 @retval TRUE Get the question value success.
2398 @retval FALSE Get the question value failed.
2399 **/
2400 BOOLEAN
2401 GetQuestionValueFromForm (
2402 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
2403 IN EFI_HII_HANDLE InputHiiHandle,
2404 IN EFI_GUID *FormSetGuid,
2405 IN EFI_QUESTION_ID QuestionId,
2406 OUT EFI_HII_VALUE *Value
2407 )
2408 {
2409 EFI_STATUS Status;
2410 EFI_HII_HANDLE HiiHandle;
2411 FORM_BROWSER_STATEMENT *Question;
2412 FORM_BROWSER_FORMSET *FormSet;
2413 FORM_BROWSER_FORM *Form;
2414 BOOLEAN GetTheVal;
2415 LIST_ENTRY *Link;
2416
2417 //
2418 // The input parameter DevicePath or InputHiiHandle must have one valid input.
2419 //
2420 ASSERT ((DevicePath != NULL && InputHiiHandle == NULL) ||
2421 (DevicePath == NULL && InputHiiHandle != NULL) );
2422
2423 GetTheVal = TRUE;
2424 HiiHandle = NULL;
2425 Question = NULL;
2426 Form = NULL;
2427
2428 //
2429 // Get HiiHandle.
2430 //
2431 if (DevicePath != NULL) {
2432 HiiHandle = DevicePathToHiiHandle (DevicePath, FormSetGuid);
2433 if (HiiHandle == NULL) {
2434 return FALSE;
2435 }
2436 } else {
2437 HiiHandle = InputHiiHandle;
2438 }
2439 ASSERT (HiiHandle != NULL);
2440
2441 //
2442 // Get the formset data include this question.
2443 //
2444 FormSet = AllocateZeroPool (sizeof (FORM_BROWSER_FORMSET));
2445 ASSERT (FormSet != NULL);
2446 Status = InitializeFormSet(HiiHandle, FormSetGuid, FormSet);
2447 if (EFI_ERROR (Status)) {
2448 GetTheVal = FALSE;
2449 goto Done;
2450 }
2451
2452 //
2453 // Base on the Question Id to get the question info.
2454 //
2455 Question = IdToQuestion(FormSet, NULL, QuestionId);
2456 if (Question == NULL) {
2457 GetTheVal = FALSE;
2458 goto Done;
2459 }
2460
2461 //
2462 // Search form in the formset scope
2463 //
2464 Link = GetFirstNode (&FormSet->FormListHead);
2465 while (!IsNull (&FormSet->FormListHead, Link)) {
2466 Form = FORM_BROWSER_FORM_FROM_LINK (Link);
2467
2468 Question = IdToQuestion2 (Form, QuestionId);
2469 if (Question != NULL) {
2470 break;
2471 }
2472
2473 Link = GetNextNode (&FormSet->FormListHead, Link);
2474 Form = NULL;
2475 }
2476 ASSERT (Form != NULL);
2477
2478 //
2479 // Get the question value.
2480 //
2481 Status = GetQuestionValue(FormSet, Form, Question, GetSetValueWithHiiDriver);
2482 if (EFI_ERROR (Status)) {
2483 GetTheVal = FALSE;
2484 goto Done;
2485 }
2486
2487 CopyMem (Value, &Question->HiiValue, sizeof (EFI_HII_VALUE));
2488
2489 Done:
2490 //
2491 // Clean the formset structure and restore the global parameter.
2492 //
2493 if (FormSet != NULL) {
2494 DestroyFormSet (FormSet);
2495 }
2496
2497 return GetTheVal;
2498 }
2499
2500 /**
2501 Evaluate the result of a HII expression.
2502
2503 If Expression is NULL, then ASSERT.
2504
2505 @param FormSet FormSet associated with this expression.
2506 @param Form Form associated with this expression.
2507 @param Expression Expression to be evaluated.
2508
2509 @retval EFI_SUCCESS The expression evaluated successfuly
2510 @retval EFI_NOT_FOUND The Question which referenced by a QuestionId
2511 could not be found.
2512 @retval EFI_OUT_OF_RESOURCES There is not enough system memory to grow the
2513 stack.
2514 @retval EFI_ACCESS_DENIED The pop operation underflowed the stack
2515 @retval EFI_INVALID_PARAMETER Syntax error with the Expression
2516
2517 **/
2518 EFI_STATUS
2519 EvaluateExpression (
2520 IN FORM_BROWSER_FORMSET *FormSet,
2521 IN FORM_BROWSER_FORM *Form,
2522 IN OUT FORM_EXPRESSION *Expression
2523 )
2524 {
2525 EFI_STATUS Status;
2526 LIST_ENTRY *Link;
2527 EXPRESSION_OPCODE *OpCode;
2528 FORM_BROWSER_STATEMENT *Question;
2529 FORM_BROWSER_STATEMENT *Question2;
2530 UINT16 Index;
2531 EFI_HII_VALUE Data1;
2532 EFI_HII_VALUE Data2;
2533 EFI_HII_VALUE Data3;
2534 FORM_EXPRESSION *RuleExpression;
2535 EFI_HII_VALUE *Value;
2536 INTN Result;
2537 CHAR16 *StrPtr;
2538 CHAR16 *NameValue;
2539 UINT32 TempValue;
2540 LIST_ENTRY *SubExpressionLink;
2541 FORM_EXPRESSION *SubExpression;
2542 UINTN StackOffset;
2543 UINTN TempLength;
2544 CHAR16 TempStr[5];
2545 UINT8 DigitUint8;
2546 UINT8 *TempBuffer;
2547 EFI_TIME EfiTime;
2548 EFI_HII_VALUE QuestionVal;
2549 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
2550
2551 StrPtr = NULL;
2552
2553 //
2554 // Save current stack offset.
2555 //
2556 StackOffset = SaveExpressionEvaluationStackOffset ();
2557
2558 ASSERT (Expression != NULL);
2559 Expression->Result.Type = EFI_IFR_TYPE_OTHER;
2560
2561 Link = GetFirstNode (&Expression->OpCodeListHead);
2562 while (!IsNull (&Expression->OpCodeListHead, Link)) {
2563 OpCode = EXPRESSION_OPCODE_FROM_LINK (Link);
2564
2565 Link = GetNextNode (&Expression->OpCodeListHead, Link);
2566
2567 ZeroMem (&Data1, sizeof (EFI_HII_VALUE));
2568 ZeroMem (&Data2, sizeof (EFI_HII_VALUE));
2569 ZeroMem (&Data3, sizeof (EFI_HII_VALUE));
2570
2571 Value = &Data3;
2572 Value->Type = EFI_IFR_TYPE_BOOLEAN;
2573 Status = EFI_SUCCESS;
2574
2575 switch (OpCode->Operand) {
2576 //
2577 // Built-in functions
2578 //
2579 case EFI_IFR_EQ_ID_VAL_OP:
2580 Question = IdToQuestion (FormSet, Form, OpCode->QuestionId);
2581 if (Question == NULL) {
2582 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2583 break;
2584 }
2585
2586 Status = CompareHiiValue (&Question->HiiValue, &OpCode->Value, &Result, NULL);
2587 if (Status == EFI_UNSUPPORTED) {
2588 Status = EFI_SUCCESS;
2589 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2590 break;
2591 }
2592
2593 if (EFI_ERROR (Status)) {
2594 goto Done;
2595 }
2596 Value->Value.b = (BOOLEAN) ((Result == 0) ? TRUE : FALSE);
2597 break;
2598
2599 case EFI_IFR_EQ_ID_ID_OP:
2600 Question = IdToQuestion (FormSet, Form, OpCode->QuestionId);
2601 if (Question == NULL) {
2602 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2603 break;
2604 }
2605
2606 Question2 = IdToQuestion (FormSet, Form, OpCode->QuestionId2);
2607 if (Question2 == NULL) {
2608 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2609 break;
2610 }
2611
2612 Status = CompareHiiValue (&Question->HiiValue, &Question2->HiiValue, &Result, FormSet->HiiHandle);
2613 if (Status == EFI_UNSUPPORTED) {
2614 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2615 Status = EFI_SUCCESS;
2616 break;
2617 }
2618 if (EFI_ERROR (Status)) {
2619 goto Done;
2620 }
2621 Value->Value.b = (BOOLEAN) ((Result == 0) ? TRUE : FALSE);
2622 break;
2623
2624 case EFI_IFR_EQ_ID_VAL_LIST_OP:
2625 Question = IdToQuestion (FormSet, Form, OpCode->QuestionId);
2626 if (Question == NULL) {
2627 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2628 break;
2629 }
2630
2631 Value->Value.b = FALSE;
2632 for (Index =0; Index < OpCode->ListLength; Index++) {
2633 if (Question->HiiValue.Value.u16 == OpCode->ValueList[Index]) {
2634 Value->Value.b = TRUE;
2635 break;
2636 }
2637 }
2638 break;
2639
2640 case EFI_IFR_DUP_OP:
2641 Status = PopExpression (Value);
2642 if (EFI_ERROR (Status)) {
2643 goto Done;
2644 }
2645
2646 Status = PushExpression (Value);
2647 break;
2648
2649 case EFI_IFR_QUESTION_REF1_OP:
2650 case EFI_IFR_THIS_OP:
2651 Question = IdToQuestion (FormSet, Form, OpCode->QuestionId);
2652 if (Question == NULL) {
2653 Status = EFI_NOT_FOUND;
2654 goto Done;
2655 }
2656
2657 Value = &Question->HiiValue;
2658 break;
2659
2660 case EFI_IFR_SECURITY_OP:
2661 Value->Value.b = CheckUserPrivilege (&OpCode->Guid);
2662 break;
2663
2664 case EFI_IFR_GET_OP:
2665 //
2666 // Get Value from VarStore buffer, EFI VarStore, Name/Value VarStore.
2667 //
2668 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2669 Value->Value.u8 = 0;
2670 if (OpCode->VarStorage != NULL) {
2671 switch (OpCode->VarStorage->Type) {
2672 case EFI_HII_VARSTORE_BUFFER:
2673 case EFI_HII_VARSTORE_EFI_VARIABLE_BUFFER:
2674 //
2675 // Get value from Edit Buffer
2676 //
2677 Value->Type = OpCode->ValueType;
2678 CopyMem (&Value->Value, OpCode->VarStorage->EditBuffer + OpCode->VarStoreInfo.VarOffset, OpCode->ValueWidth);
2679 break;
2680 case EFI_HII_VARSTORE_NAME_VALUE:
2681 if (OpCode->ValueType != EFI_IFR_TYPE_STRING) {
2682 //
2683 // Get value from string except for STRING value.
2684 //
2685 Status = GetValueByName (OpCode->VarStorage, OpCode->ValueName, &StrPtr, GetSetValueWithEditBuffer);
2686 if (!EFI_ERROR (Status)) {
2687 ASSERT (StrPtr != NULL);
2688 TempLength = StrLen (StrPtr);
2689 if (OpCode->ValueWidth >= ((TempLength + 1) / 2)) {
2690 Value->Type = OpCode->ValueType;
2691 TempBuffer = (UINT8 *) &Value->Value;
2692 ZeroMem (TempStr, sizeof (TempStr));
2693 for (Index = 0; Index < TempLength; Index ++) {
2694 TempStr[0] = StrPtr[TempLength - Index - 1];
2695 DigitUint8 = (UINT8) StrHexToUint64 (TempStr);
2696 if ((Index & 1) == 0) {
2697 TempBuffer [Index/2] = DigitUint8;
2698 } else {
2699 TempBuffer [Index/2] = (UINT8) ((DigitUint8 << 4) + TempBuffer [Index/2]);
2700 }
2701 }
2702 }
2703 }
2704 }
2705 break;
2706 case EFI_HII_VARSTORE_EFI_VARIABLE:
2707 //
2708 // Get value from variable.
2709 //
2710 TempLength = OpCode->ValueWidth;
2711 Value->Type = OpCode->ValueType;
2712 Status = gRT->GetVariable (
2713 OpCode->ValueName,
2714 &OpCode->VarStorage->Guid,
2715 NULL,
2716 &TempLength,
2717 &Value->Value
2718 );
2719 if (EFI_ERROR (Status)) {
2720 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2721 Value->Value.u8 = 0;
2722 }
2723 break;
2724 default:
2725 //
2726 // Not recognize storage.
2727 //
2728 Status = EFI_UNSUPPORTED;
2729 goto Done;
2730 }
2731 } else {
2732 //
2733 // For Time/Date Data
2734 //
2735 if (OpCode->ValueType != EFI_IFR_TYPE_DATE && OpCode->ValueType != EFI_IFR_TYPE_TIME) {
2736 //
2737 // Only support Data/Time data when storage doesn't exist.
2738 //
2739 Status = EFI_UNSUPPORTED;
2740 goto Done;
2741 }
2742 Status = gRT->GetTime (&EfiTime, NULL);
2743 if (!EFI_ERROR (Status)) {
2744 if (OpCode->ValueType == EFI_IFR_TYPE_DATE) {
2745 switch (OpCode->VarStoreInfo.VarOffset) {
2746 case 0x00:
2747 Value->Type = EFI_IFR_TYPE_NUM_SIZE_16;
2748 Value->Value.u16 = EfiTime.Year;
2749 break;
2750 case 0x02:
2751 Value->Type = EFI_IFR_TYPE_NUM_SIZE_8;
2752 Value->Value.u8 = EfiTime.Month;
2753 break;
2754 case 0x03:
2755 Value->Type = EFI_IFR_TYPE_NUM_SIZE_8;
2756 Value->Value.u8 = EfiTime.Day;
2757 break;
2758 default:
2759 //
2760 // Invalid Date field.
2761 //
2762 Status = EFI_INVALID_PARAMETER;
2763 goto Done;
2764 }
2765 } else {
2766 switch (OpCode->VarStoreInfo.VarOffset) {
2767 case 0x00:
2768 Value->Type = EFI_IFR_TYPE_NUM_SIZE_8;
2769 Value->Value.u8 = EfiTime.Hour;
2770 break;
2771 case 0x01:
2772 Value->Type = EFI_IFR_TYPE_NUM_SIZE_8;
2773 Value->Value.u8 = EfiTime.Minute;
2774 break;
2775 case 0x02:
2776 Value->Type = EFI_IFR_TYPE_NUM_SIZE_8;
2777 Value->Value.u8 = EfiTime.Second;
2778 break;
2779 default:
2780 //
2781 // Invalid Time field.
2782 //
2783 Status = EFI_INVALID_PARAMETER;
2784 goto Done;
2785 }
2786 }
2787 }
2788 }
2789
2790 break;
2791
2792 case EFI_IFR_QUESTION_REF3_OP:
2793 //
2794 // EFI_IFR_QUESTION_REF3
2795 // Pop an expression from the expression stack
2796 //
2797 Status = PopExpression (Value);
2798 if (EFI_ERROR (Status)) {
2799 goto Done;
2800 }
2801
2802 //
2803 // Validate the expression value
2804 //
2805 if ((Value->Type > EFI_IFR_TYPE_NUM_SIZE_64) || (Value->Value.u64 > 0xffff)) {
2806 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2807 break;
2808 }
2809
2810 if (OpCode->DevicePath != 0) {
2811 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2812
2813 StrPtr = GetToken (OpCode->DevicePath, FormSet->HiiHandle);
2814 if (StrPtr != NULL && mPathFromText != NULL) {
2815 DevicePath = mPathFromText->ConvertTextToDevicePath(StrPtr);
2816 if (DevicePath != NULL && GetQuestionValueFromForm(DevicePath, NULL, &OpCode->Guid, Value->Value.u16, &QuestionVal)) {
2817 Value = &QuestionVal;
2818 }
2819 if (DevicePath != NULL) {
2820 FreePool (DevicePath);
2821 }
2822 }
2823
2824 if (StrPtr != NULL) {
2825 FreePool (StrPtr);
2826 }
2827 } else if (CompareGuid (&OpCode->Guid, &gZeroGuid) != 0) {
2828 if (!GetQuestionValueFromForm(NULL, FormSet->HiiHandle, &OpCode->Guid, Value->Value.u16, &QuestionVal)){
2829 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2830 break;
2831 }
2832 Value = &QuestionVal;
2833 } else {
2834 Question = IdToQuestion (FormSet, Form, Value->Value.u16);
2835 if (Question == NULL) {
2836 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2837 break;
2838 }
2839
2840 //
2841 // push the questions' value on to the expression stack
2842 //
2843 Value = &Question->HiiValue;
2844 }
2845 break;
2846
2847 case EFI_IFR_RULE_REF_OP:
2848 //
2849 // Find expression for this rule
2850 //
2851 RuleExpression = RuleIdToExpression (Form, OpCode->RuleId);
2852 if (RuleExpression == NULL) {
2853 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2854 break;
2855 }
2856
2857 //
2858 // Evaluate this rule expression
2859 //
2860 Status = EvaluateExpression (FormSet, Form, RuleExpression);
2861 if (EFI_ERROR (Status) || RuleExpression->Result.Type == EFI_IFR_TYPE_UNDEFINED) {
2862 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2863 break;
2864 }
2865
2866 Value = &RuleExpression->Result;
2867 break;
2868
2869 case EFI_IFR_STRING_REF1_OP:
2870 Value->Type = EFI_IFR_TYPE_STRING;
2871 Value->Value.string = OpCode->Value.Value.string;
2872 break;
2873
2874 //
2875 // Constant
2876 //
2877 case EFI_IFR_TRUE_OP:
2878 case EFI_IFR_FALSE_OP:
2879 case EFI_IFR_ONE_OP:
2880 case EFI_IFR_ONES_OP:
2881 case EFI_IFR_UINT8_OP:
2882 case EFI_IFR_UINT16_OP:
2883 case EFI_IFR_UINT32_OP:
2884 case EFI_IFR_UINT64_OP:
2885 case EFI_IFR_UNDEFINED_OP:
2886 case EFI_IFR_VERSION_OP:
2887 case EFI_IFR_ZERO_OP:
2888 Value = &OpCode->Value;
2889 break;
2890
2891 //
2892 // unary-op
2893 //
2894 case EFI_IFR_LENGTH_OP:
2895 Status = PopExpression (Value);
2896 if (EFI_ERROR (Status)) {
2897 goto Done;
2898 }
2899 if (Value->Type != EFI_IFR_TYPE_STRING && !IsTypeInBuffer (Value)) {
2900 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2901 break;
2902 }
2903
2904 if (Value->Type == EFI_IFR_TYPE_STRING) {
2905 StrPtr = GetToken (Value->Value.string, FormSet->HiiHandle);
2906 if (StrPtr == NULL) {
2907 Status = EFI_INVALID_PARAMETER;
2908 goto Done;
2909 }
2910
2911 Value->Type = EFI_IFR_TYPE_NUM_SIZE_64;
2912 Value->Value.u64 = StrLen (StrPtr);
2913 FreePool (StrPtr);
2914 } else {
2915 Value->Type = EFI_IFR_TYPE_NUM_SIZE_64;
2916 Value->Value.u64 = GetLengthForValue(Value);
2917 FreePool (Value->Buffer);
2918 }
2919 break;
2920
2921 case EFI_IFR_NOT_OP:
2922 Status = PopExpression (Value);
2923 if (EFI_ERROR (Status)) {
2924 goto Done;
2925 }
2926 if (Value->Type != EFI_IFR_TYPE_BOOLEAN) {
2927 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2928 break;
2929 }
2930 Value->Value.b = (BOOLEAN) (!Value->Value.b);
2931 break;
2932
2933 case EFI_IFR_QUESTION_REF2_OP:
2934 //
2935 // Pop an expression from the expression stack
2936 //
2937 Status = PopExpression (Value);
2938 if (EFI_ERROR (Status)) {
2939 goto Done;
2940 }
2941
2942 //
2943 // Validate the expression value
2944 //
2945 if ((Value->Type > EFI_IFR_TYPE_NUM_SIZE_64) || (Value->Value.u64 > 0xffff)) {
2946 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2947 break;
2948 }
2949
2950 Question = IdToQuestion (FormSet, Form, Value->Value.u16);
2951 if (Question == NULL) {
2952 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2953 break;
2954 }
2955
2956 Value = &Question->HiiValue;
2957 break;
2958
2959 case EFI_IFR_STRING_REF2_OP:
2960 //
2961 // Pop an expression from the expression stack
2962 //
2963 Status = PopExpression (Value);
2964 if (EFI_ERROR (Status)) {
2965 goto Done;
2966 }
2967
2968 //
2969 // Validate the expression value
2970 //
2971 if ((Value->Type > EFI_IFR_TYPE_NUM_SIZE_64) || (Value->Value.u64 > 0xffff)) {
2972 Value->Type = EFI_IFR_TYPE_UNDEFINED;
2973 break;
2974 }
2975
2976 Value->Type = EFI_IFR_TYPE_STRING;
2977 StrPtr = GetToken (Value->Value.u16, FormSet->HiiHandle);
2978 if (StrPtr == NULL) {
2979 //
2980 // If String not exit, push an empty string
2981 //
2982 Value->Value.string = NewString (gEmptyString, FormSet->HiiHandle);
2983 } else {
2984 Index = (UINT16) Value->Value.u64;
2985 Value->Value.string = Index;
2986 FreePool (StrPtr);
2987 }
2988 break;
2989
2990 case EFI_IFR_TO_BOOLEAN_OP:
2991 //
2992 // Pop an expression from the expression stack
2993 //
2994 Status = PopExpression (Value);
2995 if (EFI_ERROR (Status)) {
2996 goto Done;
2997 }
2998
2999 //
3000 // Convert an expression to a Boolean
3001 //
3002 if (Value->Type <= EFI_IFR_TYPE_DATE) {
3003 //
3004 // When converting from an unsigned integer, zero will be converted to
3005 // FALSE and any other value will be converted to TRUE.
3006 //
3007 Value->Value.b = (BOOLEAN) (HiiValueToUINT64(Value) != 0);
3008
3009 Value->Type = EFI_IFR_TYPE_BOOLEAN;
3010 } else if (Value->Type == EFI_IFR_TYPE_STRING) {
3011 //
3012 // When converting from a string, if case-insensitive compare
3013 // with "true" is True, then push True. If a case-insensitive compare
3014 // with "false" is True, then push False. Otherwise, push Undefined.
3015 //
3016 StrPtr = GetToken (Value->Value.string, FormSet->HiiHandle);
3017 if (StrPtr == NULL) {
3018 Status = EFI_INVALID_PARAMETER;
3019 goto Done;
3020 }
3021
3022 IfrStrToUpper (StrPtr);
3023 if (StrCmp (StrPtr, L"TRUE") == 0){
3024 Value->Value.b = TRUE;
3025 Value->Type = EFI_IFR_TYPE_BOOLEAN;
3026 } else if (StrCmp (StrPtr, L"FALSE") == 0) {
3027 Value->Value.b = FALSE;
3028 Value->Type = EFI_IFR_TYPE_BOOLEAN;
3029 } else {
3030 Value->Type = EFI_IFR_TYPE_UNDEFINED;
3031 }
3032 FreePool (StrPtr);
3033 } else if (Value->Type == EFI_IFR_TYPE_BUFFER) {
3034 //
3035 // When converting from a buffer, if the buffer is all zeroes,
3036 // then push False. Otherwise push True.
3037 //
3038 for (Index =0; Index < Value->BufferLen; Index ++) {
3039 if (Value->Buffer[Index] != 0) {
3040 break;
3041 }
3042 }
3043
3044 if (Index >= Value->BufferLen) {
3045 Value->Value.b = FALSE;
3046 } else {
3047 Value->Value.b = TRUE;
3048 }
3049 Value->Type = EFI_IFR_TYPE_BOOLEAN;
3050 FreePool (Value->Buffer);
3051 }
3052 break;
3053
3054 case EFI_IFR_TO_STRING_OP:
3055 Status = IfrToString (FormSet, OpCode->Format, Value);
3056 break;
3057
3058 case EFI_IFR_TO_UINT_OP:
3059 Status = IfrToUint (FormSet, Value);
3060 break;
3061
3062 case EFI_IFR_TO_LOWER_OP:
3063 case EFI_IFR_TO_UPPER_OP:
3064 Status = InitializeUnicodeCollationProtocol ();
3065 if (EFI_ERROR (Status)) {
3066 goto Done;
3067 }
3068
3069 Status = PopExpression (Value);
3070 if (EFI_ERROR (Status)) {
3071 goto Done;
3072 }
3073
3074 if (Value->Type != EFI_IFR_TYPE_STRING) {
3075 Value->Type = EFI_IFR_TYPE_UNDEFINED;
3076 break;
3077 }
3078
3079 StrPtr = GetToken (Value->Value.string, FormSet->HiiHandle);
3080 if (StrPtr == NULL) {
3081 Status = EFI_NOT_FOUND;
3082 goto Done;
3083 }
3084
3085 if (OpCode->Operand == EFI_IFR_TO_LOWER_OP) {
3086 mUnicodeCollation->StrLwr (mUnicodeCollation, StrPtr);
3087 } else {
3088 mUnicodeCollation->StrUpr (mUnicodeCollation, StrPtr);
3089 }
3090 Value->Value.string = NewString (StrPtr, FormSet->HiiHandle);
3091 FreePool (StrPtr);
3092 break;
3093
3094 case EFI_IFR_BITWISE_NOT_OP:
3095 //
3096 // Pop an expression from the expression stack
3097 //
3098 Status = PopExpression (Value);
3099 if (EFI_ERROR (Status)) {
3100 goto Done;
3101 }
3102 if (Value->Type > EFI_IFR_TYPE_DATE) {
3103 Value->Type = EFI_IFR_TYPE_UNDEFINED;
3104 break;
3105 }
3106
3107 Value->Type = EFI_IFR_TYPE_NUM_SIZE_64;
3108 Value->Value.u64 = ~ HiiValueToUINT64(Value);
3109 break;
3110
3111 case EFI_IFR_SET_OP:
3112 //
3113 // Pop an expression from the expression stack
3114 //
3115 Status = PopExpression (Value);
3116 if (EFI_ERROR (Status)) {
3117 goto Done;
3118 }
3119 Data1.Type = EFI_IFR_TYPE_BOOLEAN;
3120 Data1.Value.b = FALSE;
3121 //
3122 // Set value to var storage buffer
3123 //
3124 if (OpCode->VarStorage != NULL) {
3125 switch (OpCode->VarStorage->Type) {
3126 case EFI_HII_VARSTORE_BUFFER:
3127 case EFI_HII_VARSTORE_EFI_VARIABLE_BUFFER:
3128 CopyMem (OpCode->VarStorage->EditBuffer + OpCode->VarStoreInfo.VarOffset, &Value->Value, OpCode->ValueWidth);
3129 Data1.Value.b = TRUE;
3130 break;
3131 case EFI_HII_VARSTORE_NAME_VALUE:
3132 if (OpCode->ValueType != EFI_IFR_TYPE_STRING) {
3133 NameValue = AllocateZeroPool ((OpCode->ValueWidth * 2 + 1) * sizeof (CHAR16));
3134 ASSERT (Value != NULL);
3135 //
3136 // Convert Buffer to Hex String
3137 //
3138 TempBuffer = (UINT8 *) &Value->Value + OpCode->ValueWidth - 1;
3139 StrPtr = NameValue;
3140 for (Index = 0; Index < OpCode->ValueWidth; Index ++, TempBuffer --) {
3141 StrPtr += UnicodeValueToString (StrPtr, PREFIX_ZERO | RADIX_HEX, *TempBuffer, 2);
3142 }
3143 Status = SetValueByName (OpCode->VarStorage, OpCode->ValueName, NameValue, GetSetValueWithEditBuffer, NULL);
3144 FreePool (NameValue);
3145 if (!EFI_ERROR (Status)) {
3146 Data1.Value.b = TRUE;
3147 }
3148 }
3149 break;
3150 case EFI_HII_VARSTORE_EFI_VARIABLE:
3151 Status = gRT->SetVariable (
3152 OpCode->ValueName,
3153 &OpCode->VarStorage->Guid,
3154 OpCode->VarStorage->Attributes,
3155 OpCode->ValueWidth,
3156 &Value->Value
3157 );
3158 if (!EFI_ERROR (Status)) {
3159 Data1.Value.b = TRUE;
3160 }
3161 break;
3162 default:
3163 //
3164 // Not recognize storage.
3165 //
3166 Status = EFI_UNSUPPORTED;
3167 goto Done;
3168 break;
3169 }
3170 } else {
3171 //
3172 // For Time/Date Data
3173 //
3174 if (OpCode->ValueType != EFI_IFR_TYPE_DATE && OpCode->ValueType != EFI_IFR_TYPE_TIME) {
3175 //
3176 // Only support Data/Time data when storage doesn't exist.
3177 //
3178 Status = EFI_UNSUPPORTED;
3179 goto Done;
3180 }
3181 Status = gRT->GetTime (&EfiTime, NULL);
3182 if (!EFI_ERROR (Status)) {
3183 if (OpCode->ValueType == EFI_IFR_TYPE_DATE) {
3184 switch (OpCode->VarStoreInfo.VarOffset) {
3185 case 0x00:
3186 EfiTime.Year = Value->Value.u16;
3187 break;
3188 case 0x02:
3189 EfiTime.Month = Value->Value.u8;
3190 break;
3191 case 0x03:
3192 EfiTime.Day = Value->Value.u8;
3193 break;
3194 default:
3195 //
3196 // Invalid Date field.
3197 //
3198 Status = EFI_INVALID_PARAMETER;