2 This is an example of how a driver might export data to the HII protocol to be
3 later utilized by the Setup Protocol
5 Copyright (c) 2004 - 2009, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "DriverSample.h"
19 #define DISPLAY_ONLY_MY_ITEM 0x0002
21 EFI_GUID mFormSetGuid
= FORMSET_GUID
;
22 EFI_GUID mInventoryGuid
= INVENTORY_GUID
;
24 CHAR16 VariableName
[] = L
"MyIfrNVData";
25 EFI_HANDLE DriverHandle
[2] = {NULL
, NULL
};
26 DRIVER_SAMPLE_PRIVATE_DATA
*PrivateData
= NULL
;
28 HII_VENDOR_DEVICE_PATH mHiiVendorDevicePath0
= {
34 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
)),
35 (UINT8
) ((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
39 // {C153B68D-EBFC-488e-B110-662867745B87}
41 { 0xc153b68d, 0xebfc, 0x488e, { 0xb1, 0x10, 0x66, 0x28, 0x67, 0x74, 0x5b, 0x87 } }
45 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
47 (UINT8
) (END_DEVICE_PATH_LENGTH
),
48 (UINT8
) ((END_DEVICE_PATH_LENGTH
) >> 8)
53 HII_VENDOR_DEVICE_PATH mHiiVendorDevicePath1
= {
59 (UINT8
) (sizeof (VENDOR_DEVICE_PATH
)),
60 (UINT8
) ((sizeof (VENDOR_DEVICE_PATH
)) >> 8)
64 // {06F37F07-0C48-40e9-8436-0A08A0BB76B0}
66 { 0x6f37f07, 0xc48, 0x40e9, { 0x84, 0x36, 0xa, 0x8, 0xa0, 0xbb, 0x76, 0xb0 } }
70 END_ENTIRE_DEVICE_PATH_SUBTYPE
,
72 (UINT8
) (END_DEVICE_PATH_LENGTH
),
73 (UINT8
) ((END_DEVICE_PATH_LENGTH
) >> 8)
79 Encode the password using a simple algorithm.
81 @param Password The string to be encoded.
82 @param MaxSize The size of the string.
97 Buffer
= AllocateZeroPool (MaxSize
);
98 ASSERT (Buffer
!= NULL
);
100 for (Index
= 0; Key
[Index
] != 0; Index
++) {
101 for (Loop
= 0; Loop
< (UINT8
) (MaxSize
/ 2); Loop
++) {
102 Buffer
[Loop
] = (CHAR16
) (Password
[Loop
] ^ Key
[Index
]);
106 CopyMem (Password
, Buffer
, MaxSize
);
113 Validate the user's password.
115 @param PrivateData This driver's private context data.
116 @param StringId The user's input.
118 @retval EFI_SUCCESS The user's input matches the password.
119 @retval EFI_NOT_READY The user's input does not match the password.
123 IN DRIVER_SAMPLE_PRIVATE_DATA
*PrivateData
,
124 IN EFI_STRING_ID StringId
130 UINTN PasswordMaxSize
;
132 CHAR16
*EncodedPassword
;
136 // Get encoded password first
138 BufferSize
= sizeof (DRIVER_SAMPLE_CONFIGURATION
);
139 Status
= gRT
->GetVariable (
144 &PrivateData
->Configuration
146 if (EFI_ERROR (Status
)) {
148 // Old password not exist, prompt for new password
154 PasswordMaxSize
= sizeof (PrivateData
->Configuration
.WhatIsThePassword2
);
156 // Check whether we have any old password set
158 for (Index
= 0; Index
< PasswordMaxSize
/ sizeof (UINT16
); Index
++) {
159 if (PrivateData
->Configuration
.WhatIsThePassword2
[Index
] != 0) {
166 // Old password not exist, return EFI_SUCCESS to prompt for new password
172 // Get user input password
174 Password
= HiiGetString (PrivateData
->HiiHandle
[0], StringId
, NULL
);
175 if (Password
== NULL
) {
176 return EFI_NOT_READY
;
178 if (StrSize (Password
) > PasswordMaxSize
) {
180 return EFI_NOT_READY
;
184 // Validate old password
186 EncodedPassword
= AllocateZeroPool (PasswordMaxSize
);
187 ASSERT (EncodedPassword
!= NULL
);
188 StrnCpy (EncodedPassword
, Password
, StrLen (Password
));
189 EncodePassword (EncodedPassword
, StrLen (EncodedPassword
) * sizeof (CHAR16
));
190 if (CompareMem (EncodedPassword
, PrivateData
->Configuration
.WhatIsThePassword2
, StrLen (EncodedPassword
) * sizeof (CHAR16
)) != 0) {
192 // Old password mismatch, return EFI_NOT_READY to prompt for error message
194 Status
= EFI_NOT_READY
;
196 Status
= EFI_SUCCESS
;
200 FreePool (EncodedPassword
);
206 Encode the password using a simple algorithm.
208 @param PrivateData This driver's private context data.
209 @param StringId The password from User.
211 @retval EFI_SUCESS The operation is successful.
212 @return Other value if gRT->SetVariable () fails.
217 IN DRIVER_SAMPLE_PRIVATE_DATA
*PrivateData
,
218 IN EFI_STRING_ID StringId
223 CHAR16
*TempPassword
;
225 DRIVER_SAMPLE_CONFIGURATION
*Configuration
;
229 // Get Buffer Storage data from EFI variable
231 BufferSize
= sizeof (DRIVER_SAMPLE_CONFIGURATION
);
232 Status
= gRT
->GetVariable (
237 &PrivateData
->Configuration
239 if (EFI_ERROR (Status
)) {
244 // Get user input password
246 Password
= &PrivateData
->Configuration
.WhatIsThePassword2
[0];
247 PasswordSize
= sizeof (PrivateData
->Configuration
.WhatIsThePassword2
);
248 ZeroMem (Password
, PasswordSize
);
250 TempPassword
= HiiGetString (PrivateData
->HiiHandle
[0], StringId
, NULL
);
251 if (TempPassword
== NULL
) {
252 return EFI_NOT_READY
;
254 if (StrSize (TempPassword
) > PasswordSize
) {
255 FreePool (TempPassword
);
256 return EFI_NOT_READY
;
258 StrnCpy (Password
, TempPassword
, StrLen (TempPassword
));
259 FreePool (TempPassword
);
262 // Retrive uncommitted data from Browser
264 Configuration
= AllocateZeroPool (sizeof (DRIVER_SAMPLE_CONFIGURATION
));
265 ASSERT (Configuration
!= NULL
);
266 if (HiiGetBrowserData (&mFormSetGuid
, VariableName
, sizeof (DRIVER_SAMPLE_CONFIGURATION
), (UINT8
*) Configuration
)) {
268 // Update password's clear text in the screen
270 CopyMem (Configuration
->PasswordClearText
, Password
, StrSize (Password
));
273 // Update uncommitted data of Browser
278 sizeof (DRIVER_SAMPLE_CONFIGURATION
),
279 (UINT8
*) Configuration
,
285 // Free Configuration Buffer
287 FreePool (Configuration
);
293 EncodePassword (Password
, StrLen (Password
) * 2);
294 Status
= gRT
->SetVariable(
297 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
298 sizeof (DRIVER_SAMPLE_CONFIGURATION
),
299 &PrivateData
->Configuration
305 Update names of Name/Value storage to current language.
307 @param PrivateData Points to the driver private data.
309 @retval EFI_SUCCESS All names are successfully updated.
310 @retval EFI_NOT_FOUND Failed to get Name from HII database.
315 IN DRIVER_SAMPLE_PRIVATE_DATA
*PrivateData
321 // Get Name/Value name string of current language
323 for (Index
= 0; Index
< NAME_VALUE_NAME_NUMBER
; Index
++) {
324 PrivateData
->NameValueName
[Index
] = HiiGetString (
325 PrivateData
->HiiHandle
[0],
326 PrivateData
->NameStringId
[Index
],
329 if (PrivateData
->NameValueName
[Index
] == NULL
) {
330 return EFI_NOT_FOUND
;
338 This function allows a caller to extract the current configuration for one
339 or more named elements from the target driver.
341 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
342 @param Request A null-terminated Unicode string in
343 <ConfigRequest> format.
344 @param Progress On return, points to a character in the Request
345 string. Points to the string's null terminator if
346 request was successful. Points to the most recent
347 '&' before the first failing name/value pair (or
348 the beginning of the string if the failure is in
349 the first name/value pair) if the request was not
351 @param Results A null-terminated Unicode string in
352 <ConfigAltResp> format which has all values filled
353 in for the names in the Request string. String to
354 be allocated by the called function.
356 @retval EFI_SUCCESS The Results is filled with the requested values.
357 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the results.
358 @retval EFI_INVALID_PARAMETER Request is NULL, illegal syntax, or unknown name.
359 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
366 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
367 IN CONST EFI_STRING Request
,
368 OUT EFI_STRING
*Progress
,
369 OUT EFI_STRING
*Results
374 DRIVER_SAMPLE_PRIVATE_DATA
*PrivateData
;
375 EFI_HII_CONFIG_ROUTING_PROTOCOL
*HiiConfigRouting
;
376 EFI_STRING ConfigRequest
;
377 EFI_STRING ConfigRequestHdr
;
385 if (Progress
== NULL
|| Results
== NULL
|| Request
== NULL
) {
386 return EFI_INVALID_PARAMETER
;
389 // Initialize the local variables.
391 ConfigRequestHdr
= NULL
;
392 ConfigRequest
= NULL
;
396 PrivateData
= DRIVER_SAMPLE_PRIVATE_FROM_THIS (This
);
397 HiiConfigRouting
= PrivateData
->HiiConfigRouting
;
400 // Get Buffer Storage data from EFI variable.
401 // Try to get the current setting from variable.
403 BufferSize
= sizeof (DRIVER_SAMPLE_CONFIGURATION
);
404 Status
= gRT
->GetVariable (
409 &PrivateData
->Configuration
411 if (EFI_ERROR (Status
)) {
412 return EFI_NOT_FOUND
;
415 if (Request
== NULL
) {
417 // Request is set to NULL, construct full request string.
421 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
422 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
424 ConfigRequestHdr
= HiiConstructConfigHdr (&mFormSetGuid
, VariableName
, PrivateData
->DriverHandle
[0]);
425 Size
= (StrLen (ConfigRequest
) + 32 + 1) * sizeof (CHAR16
);
426 ConfigRequest
= AllocateZeroPool (Size
);
427 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
428 FreePool (ConfigRequestHdr
);
431 // Check routing data in <ConfigHdr>.
432 // Note: if only one Storage is used, then this checking could be skipped.
434 if (!HiiIsConfigHdrMatch (Request
, &mFormSetGuid
, NULL
)) {
435 return EFI_NOT_FOUND
;
437 ConfigRequest
= Request
;
440 // Check if requesting Name/Value storage
442 if (StrStr (Request
, L
"OFFSET") == NULL
) {
444 // Update Name/Value storage Names
446 Status
= LoadNameValueNames (PrivateData
);
447 if (EFI_ERROR (Status
)) {
452 // Allocate memory for <ConfigResp>, e.g. Name0=0x11, Name1=0x1234, Name2="ABCD"
453 // <Request> ::=<ConfigHdr>&Name0&Name1&Name2
454 // <ConfigResp>::=<ConfigHdr>&Name0=11&Name1=1234&Name2=0041004200430044
456 BufferSize
= (StrLen (Request
) +
457 1 + sizeof (PrivateData
->Configuration
.NameValueVar0
) * 2 +
458 1 + sizeof (PrivateData
->Configuration
.NameValueVar1
) * 2 +
459 1 + sizeof (PrivateData
->Configuration
.NameValueVar2
) * 2 + 1) * sizeof (CHAR16
);
460 *Results
= AllocateZeroPool (BufferSize
);
461 ASSERT (*Results
!= NULL
);
462 StrCpy (*Results
, Request
);
466 // Append value of NameValueVar0, type is UINT8
468 if ((Value
= StrStr (*Results
, PrivateData
->NameValueName
[0])) != NULL
) {
469 Value
+= StrLen (PrivateData
->NameValueName
[0]);
470 ValueStrLen
= ((sizeof (PrivateData
->Configuration
.NameValueVar0
) * 2) + 1);
471 CopyMem (Value
+ ValueStrLen
, Value
, StrSize (Value
));
473 BackupChar
= Value
[ValueStrLen
];
475 Value
+= UnicodeValueToString (
477 PREFIX_ZERO
| RADIX_HEX
,
478 PrivateData
->Configuration
.NameValueVar0
,
479 sizeof (PrivateData
->Configuration
.NameValueVar0
) * 2
485 // Append value of NameValueVar1, type is UINT16
487 if ((Value
= StrStr (*Results
, PrivateData
->NameValueName
[1])) != NULL
) {
488 Value
+= StrLen (PrivateData
->NameValueName
[1]);
489 ValueStrLen
= ((sizeof (PrivateData
->Configuration
.NameValueVar1
) * 2) + 1);
490 CopyMem (Value
+ ValueStrLen
, Value
, StrSize (Value
));
492 BackupChar
= Value
[ValueStrLen
];
494 Value
+= UnicodeValueToString (
496 PREFIX_ZERO
| RADIX_HEX
,
497 PrivateData
->Configuration
.NameValueVar1
,
498 sizeof (PrivateData
->Configuration
.NameValueVar1
) * 2
504 // Append value of NameValueVar2, type is CHAR16 *
506 if ((Value
= StrStr (*Results
, PrivateData
->NameValueName
[2])) != NULL
) {
507 Value
+= StrLen (PrivateData
->NameValueName
[2]);
508 ValueStrLen
= StrLen (PrivateData
->Configuration
.NameValueVar2
) * 4 + 1;
509 CopyMem (Value
+ ValueStrLen
, Value
, StrSize (Value
));
513 // Convert Unicode String to Config String, e.g. "ABCD" => "0041004200430044"
515 StrPointer
= (CHAR16
*) PrivateData
->Configuration
.NameValueVar2
;
516 for (; *StrPointer
!= L
'\0'; StrPointer
++) {
517 Value
+= UnicodeValueToString (Value
, PREFIX_ZERO
| RADIX_HEX
, *StrPointer
, 4);
521 Progress
= (EFI_STRING
*) Request
+ StrLen (Request
);
527 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
529 Status
= HiiConfigRouting
->BlockToConfig (
532 (UINT8
*) &PrivateData
->Configuration
,
538 if (Request
== NULL
) {
539 FreePool (ConfigRequest
);
548 This function processes the results of changes in configuration.
550 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
551 @param Configuration A null-terminated Unicode string in <ConfigResp>
553 @param Progress A pointer to a string filled in with the offset of
554 the most recent '&' before the first failing
555 name/value pair (or the beginning of the string if
556 the failure is in the first name/value pair) or
557 the terminating NULL if all was successful.
559 @retval EFI_SUCCESS The Results is processed successfully.
560 @retval EFI_INVALID_PARAMETER Configuration is NULL.
561 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
568 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
569 IN CONST EFI_STRING Configuration
,
570 OUT EFI_STRING
*Progress
575 DRIVER_SAMPLE_PRIVATE_DATA
*PrivateData
;
576 EFI_HII_CONFIG_ROUTING_PROTOCOL
*HiiConfigRouting
;
585 if (Configuration
== NULL
|| Progress
== NULL
) {
586 return EFI_INVALID_PARAMETER
;
589 PrivateData
= DRIVER_SAMPLE_PRIVATE_FROM_THIS (This
);
590 HiiConfigRouting
= PrivateData
->HiiConfigRouting
;
591 *Progress
= Configuration
;
594 // Check routing data in <ConfigHdr>.
595 // Note: if only one Storage is used, then this checking could be skipped.
597 if (!HiiIsConfigHdrMatch (Configuration
, &mFormSetGuid
, NULL
)) {
598 return EFI_NOT_FOUND
;
602 // Get Buffer Storage data from EFI variable
604 BufferSize
= sizeof (DRIVER_SAMPLE_CONFIGURATION
);
605 Status
= gRT
->GetVariable (
610 &PrivateData
->Configuration
612 if (EFI_ERROR (Status
)) {
617 // Check if configuring Name/Value storage
619 if (StrStr (Configuration
, L
"OFFSET") == NULL
) {
621 // Update Name/Value storage Names
623 Status
= LoadNameValueNames (PrivateData
);
624 if (EFI_ERROR (Status
)) {
629 // Convert value for NameValueVar0
631 if ((Value
= StrStr (Configuration
, PrivateData
->NameValueName
[0])) != NULL
) {
635 Value
+= StrLen (PrivateData
->NameValueName
[0]);
640 StrPtr
= StrStr (Value
, L
"&");
641 if (StrPtr
== NULL
) {
642 StrPtr
= Value
+ StrLen (Value
);
645 // Convert Value to Buffer data
647 DataBuffer
= (UINT8
*) &PrivateData
->Configuration
.NameValueVar0
;
648 ZeroMem (TemStr
, sizeof (TemStr
));
649 for (Index
= 0, StrPtr
--; StrPtr
>= Value
; StrPtr
--, Index
++) {
651 DigitUint8
= (UINT8
) StrHexToUint64 (TemStr
);
652 if ((Index
& 1) == 0) {
653 DataBuffer
[Index
/2] = DigitUint8
;
655 DataBuffer
[Index
/2] = (UINT8
) ((UINT8
) (DigitUint8
<< 4) + DataBuffer
[Index
/2]);
661 // Convert value for NameValueVar1
663 if ((Value
= StrStr (Configuration
, PrivateData
->NameValueName
[1])) != NULL
) {
667 Value
+= StrLen (PrivateData
->NameValueName
[1]);
672 StrPtr
= StrStr (Value
, L
"&");
673 if (StrPtr
== NULL
) {
674 StrPtr
= Value
+ StrLen (Value
);
677 // Convert Value to Buffer data
679 DataBuffer
= (UINT8
*) &PrivateData
->Configuration
.NameValueVar1
;
680 ZeroMem (TemStr
, sizeof (TemStr
));
681 for (Index
= 0, StrPtr
--; StrPtr
>= Value
; StrPtr
--, Index
++) {
683 DigitUint8
= (UINT8
) StrHexToUint64 (TemStr
);
684 if ((Index
& 1) == 0) {
685 DataBuffer
[Index
/2] = DigitUint8
;
687 DataBuffer
[Index
/2] = (UINT8
) ((UINT8
) (DigitUint8
<< 4) + DataBuffer
[Index
/2]);
693 // Convert value for NameValueVar2
695 if ((Value
= StrStr (Configuration
, PrivateData
->NameValueName
[2])) != NULL
) {
699 Value
+= StrLen (PrivateData
->NameValueName
[2]);
704 StrPtr
= StrStr (Value
, L
"&");
705 if (StrPtr
== NULL
) {
706 StrPtr
= Value
+ StrLen (Value
);
709 // Convert Config String to Unicode String, e.g "0041004200430044" => "ABCD"
711 StrBuffer
= (CHAR16
*) PrivateData
->Configuration
.NameValueVar2
;
712 ZeroMem (TemStr
, sizeof (TemStr
));
713 while (Value
< StrPtr
) {
714 StrnCpy (TemStr
, Value
, 4);
715 *(StrBuffer
++) = (CHAR16
) StrHexToUint64 (TemStr
);
722 // Store Buffer Storage back to EFI variable
724 Status
= gRT
->SetVariable(
727 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
728 sizeof (DRIVER_SAMPLE_CONFIGURATION
),
729 &PrivateData
->Configuration
736 // Convert <ConfigResp> to buffer data by helper function ConfigToBlock()
738 BufferSize
= sizeof (DRIVER_SAMPLE_CONFIGURATION
);
739 Status
= HiiConfigRouting
->ConfigToBlock (
742 (UINT8
*) &PrivateData
->Configuration
,
746 if (EFI_ERROR (Status
)) {
751 // Store Buffer Storage back to EFI variable
753 Status
= gRT
->SetVariable(
756 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
757 sizeof (DRIVER_SAMPLE_CONFIGURATION
),
758 &PrivateData
->Configuration
766 This function processes the results of changes in configuration.
768 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
769 @param Action Specifies the type of action taken by the browser.
770 @param QuestionId A unique value which is sent to the original
771 exporting driver so that it can identify the type
773 @param Type The type of value for the question.
774 @param Value A pointer to the data being sent to the original
776 @param ActionRequest On return, points to the action requested by the
779 @retval EFI_SUCCESS The callback successfully handled the action.
780 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
781 variable and its data.
782 @retval EFI_DEVICE_ERROR The variable could not be saved.
783 @retval EFI_UNSUPPORTED The specified Action is not supported by the
790 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
791 IN EFI_BROWSER_ACTION Action
,
792 IN EFI_QUESTION_ID QuestionId
,
794 IN EFI_IFR_TYPE_VALUE
*Value
,
795 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
798 DRIVER_SAMPLE_PRIVATE_DATA
*PrivateData
;
801 VOID
*StartOpCodeHandle
;
802 VOID
*OptionsOpCodeHandle
;
803 EFI_IFR_GUID_LABEL
*StartLabel
;
804 VOID
*EndOpCodeHandle
;
805 EFI_IFR_GUID_LABEL
*EndLabel
;
807 DRIVER_SAMPLE_CONFIGURATION
*Configuration
;
810 if (Action
== EFI_BROWSER_ACTION_FORM_OPEN
) {
812 // On FORM_OPEN event, update the form on-the-fly
814 PrivateData
= DRIVER_SAMPLE_PRIVATE_FROM_THIS (This
);
817 // Initialize the container for dynamic opcodes
819 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
820 ASSERT (StartOpCodeHandle
!= NULL
);
823 // Create Hii Extend Label OpCode as the start opcode
825 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (StartOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
826 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
827 StartLabel
->Number
= LABEL_UPDATE2
;
829 HiiCreateActionOpCode (
830 StartOpCodeHandle
, // Container for dynamic created opcodes
831 0x1238, // Question ID
832 STRING_TOKEN(STR_SAVE_TEXT
), // Prompt text
833 STRING_TOKEN(STR_SAVE_TEXT
), // Help text
834 EFI_IFR_FLAG_CALLBACK
, // Question flag
835 0 // Action String ID
839 PrivateData
->HiiHandle
[0], // HII handle
840 &mFormSetGuid
, // Formset GUID
842 StartOpCodeHandle
, // Label for where to insert opcodes
846 HiiFreeOpCodeHandle (StartOpCodeHandle
);
850 if (Action
== EFI_BROWSER_ACTION_FORM_CLOSE
) {
852 // On FORM_CLOSE event, show up a pop-up
856 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
859 L
"You are going to leave the Form!",
860 L
"Press ESC or ENTER to continue ...",
864 } while ((Key
.ScanCode
!= SCAN_ESC
) && (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
));
869 if ((Value
== NULL
) || (ActionRequest
== NULL
)) {
870 return EFI_INVALID_PARAMETER
;
873 if ((Type
== EFI_IFR_TYPE_STRING
) && (Value
->string
== 0)) {
874 return EFI_INVALID_PARAMETER
;
877 Status
= EFI_SUCCESS
;
878 PrivateData
= DRIVER_SAMPLE_PRIVATE_FROM_THIS (This
);
880 switch (QuestionId
) {
883 // Initialize the container for dynamic opcodes
885 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
886 ASSERT (StartOpCodeHandle
!= NULL
);
888 EndOpCodeHandle
= HiiAllocateOpCodeHandle ();
889 ASSERT (EndOpCodeHandle
!= NULL
);
892 // Create Hii Extend Label OpCode as the start opcode
894 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (StartOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
895 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
896 StartLabel
->Number
= LABEL_UPDATE1
;
899 // Create Hii Extend Label OpCode as the end opcode
901 EndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (EndOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
902 EndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
903 EndLabel
->Number
= LABEL_END
;
905 HiiCreateActionOpCode (
906 StartOpCodeHandle
, // Container for dynamic created opcodes
907 0x1237, // Question ID
908 STRING_TOKEN(STR_EXIT_TEXT
), // Prompt text
909 STRING_TOKEN(STR_EXIT_TEXT
), // Help text
910 EFI_IFR_FLAG_CALLBACK
, // Question flag
911 0 // Action String ID
915 // Create Option OpCode
917 OptionsOpCodeHandle
= HiiAllocateOpCodeHandle ();
918 ASSERT (OptionsOpCodeHandle
!= NULL
);
920 HiiCreateOneOfOptionOpCode (
922 STRING_TOKEN (STR_BOOT_OPTION1
),
924 EFI_IFR_NUMERIC_SIZE_1
,
928 HiiCreateOneOfOptionOpCode (
930 STRING_TOKEN (STR_BOOT_OPTION2
),
932 EFI_IFR_NUMERIC_SIZE_1
,
937 // Prepare initial value for the dynamic created oneof Question
939 PrivateData
->Configuration
.DynamicOneof
= 2;
940 Status
= gRT
->SetVariable(
943 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
944 sizeof (DRIVER_SAMPLE_CONFIGURATION
),
945 &PrivateData
->Configuration
949 // Set initial vlaue of dynamic created oneof Question in Form Browser
951 Configuration
= AllocateZeroPool (sizeof (DRIVER_SAMPLE_CONFIGURATION
));
952 ASSERT (Configuration
!= NULL
);
953 if (HiiGetBrowserData (&mFormSetGuid
, VariableName
, sizeof (DRIVER_SAMPLE_CONFIGURATION
), (UINT8
*) Configuration
)) {
954 Configuration
->DynamicOneof
= 2;
957 // Update uncommitted data of Browser
962 sizeof (DRIVER_SAMPLE_CONFIGURATION
),
963 (UINT8
*) Configuration
,
967 FreePool (Configuration
);
969 HiiCreateOneOfOpCode (
970 StartOpCodeHandle
, // Container for dynamic created opcodes
971 0x8001, // Question ID (or call it "key")
972 CONFIGURATION_VARSTORE_ID
, // VarStore ID
973 (UINT16
) DYNAMIC_ONE_OF_VAR_OFFSET
, // Offset in Buffer Storage
974 STRING_TOKEN (STR_ONE_OF_PROMPT
), // Question prompt text
975 STRING_TOKEN (STR_ONE_OF_HELP
), // Question help text
976 EFI_IFR_FLAG_CALLBACK
, // Question flag
977 EFI_IFR_NUMERIC_SIZE_1
, // Data type of Question Value
978 OptionsOpCodeHandle
, // Option Opcode list
979 NULL
// Default Opcode is NULl
982 HiiCreateOrderedListOpCode (
983 StartOpCodeHandle
, // Container for dynamic created opcodes
984 0x8002, // Question ID
985 CONFIGURATION_VARSTORE_ID
, // VarStore ID
986 (UINT16
) DYNAMIC_ORDERED_LIST_VAR_OFFSET
, // Offset in Buffer Storage
987 STRING_TOKEN (STR_BOOT_OPTIONS
), // Question prompt text
988 STRING_TOKEN (STR_BOOT_OPTIONS
), // Question help text
989 EFI_IFR_FLAG_RESET_REQUIRED
, // Question flag
990 0, // Ordered list flag, e.g. EFI_IFR_UNIQUE_SET
991 EFI_IFR_NUMERIC_SIZE_1
, // Data type of Question value
992 5, // Maximum container
993 OptionsOpCodeHandle
, // Option Opcode list
994 NULL
// Default Opcode is NULl
997 HiiCreateGotoOpCode (
998 StartOpCodeHandle
, // Container for dynamic created opcodes
1000 STRING_TOKEN (STR_GOTO_FORM1
), // Prompt text
1001 STRING_TOKEN (STR_GOTO_HELP
), // Help text
1003 0x8003 // Question ID
1007 PrivateData
->HiiHandle
[0], // HII handle
1008 &mFormSetGuid
, // Formset GUID
1010 StartOpCodeHandle
, // Label for where to insert opcodes
1011 EndOpCodeHandle
// Replace data
1014 HiiFreeOpCodeHandle (StartOpCodeHandle
);
1015 HiiFreeOpCodeHandle (OptionsOpCodeHandle
);
1016 HiiFreeOpCodeHandle (EndOpCodeHandle
);
1021 // We will reach here once the Question is refreshed
1025 // Initialize the container for dynamic opcodes
1027 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
1028 ASSERT (StartOpCodeHandle
!= NULL
);
1031 // Create Hii Extend Label OpCode as the start opcode
1033 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (StartOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
1034 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1035 StartLabel
->Number
= LABEL_UPDATE2
;
1037 HiiCreateActionOpCode (
1038 StartOpCodeHandle
, // Container for dynamic created opcodes
1039 0x1237, // Question ID
1040 STRING_TOKEN(STR_EXIT_TEXT
), // Prompt text
1041 STRING_TOKEN(STR_EXIT_TEXT
), // Help text
1042 EFI_IFR_FLAG_CALLBACK
, // Question flag
1043 0 // Action String ID
1047 PrivateData
->HiiHandle
[0], // HII handle
1048 &mFormSetGuid
, // Formset GUID
1050 StartOpCodeHandle
, // Label for where to insert opcodes
1054 HiiFreeOpCodeHandle (StartOpCodeHandle
);
1057 // Refresh the Question value
1059 PrivateData
->Configuration
.DynamicRefresh
++;
1060 Status
= gRT
->SetVariable(
1063 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1064 sizeof (DRIVER_SAMPLE_CONFIGURATION
),
1065 &PrivateData
->Configuration
1069 // Change an EFI Variable storage (MyEfiVar) asynchronous, this will cause
1070 // the first statement in Form 3 be suppressed
1074 Status
= gRT
->SetVariable(
1077 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1085 // User press "Exit now", request Browser to exit
1087 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_EXIT
;
1092 // User press "Save now", request Browser to save the uncommitted data.
1094 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_SUBMIT
;
1099 // When try to set a new password, user will be chanlleged with old password.
1100 // The Callback is responsible for validating old password input by user,
1101 // If Callback return EFI_SUCCESS, it indicates validation pass.
1103 switch (PrivateData
->PasswordState
) {
1104 case BROWSER_STATE_VALIDATE_PASSWORD
:
1105 Status
= ValidatePassword (PrivateData
, Value
->string
);
1106 if (Status
== EFI_SUCCESS
) {
1107 PrivateData
->PasswordState
= BROWSER_STATE_SET_PASSWORD
;
1111 case BROWSER_STATE_SET_PASSWORD
:
1112 Status
= SetPassword (PrivateData
, Value
->string
);
1113 PrivateData
->PasswordState
= BROWSER_STATE_VALIDATE_PASSWORD
;
1124 // EfiVarstore question takes sample action (print value as debug information)
1125 // after read/write question.
1128 Status
= gRT
->GetVariable(
1135 ASSERT_EFI_ERROR (Status
);
1136 DEBUG ((DEBUG_INFO
, "EfiVarstore question: Tall value is %d with value width %d\n", MyVar
, MyVarSize
));
1145 Main entry for this driver.
1147 @param ImageHandle Image handle this driver.
1148 @param SystemTable Pointer to SystemTable.
1150 @retval EFI_SUCESS This function always complete successfully.
1156 IN EFI_HANDLE ImageHandle
,
1157 IN EFI_SYSTEM_TABLE
*SystemTable
1161 EFI_HII_HANDLE HiiHandle
[2];
1162 EFI_SCREEN_DESCRIPTOR Screen
;
1163 EFI_HII_DATABASE_PROTOCOL
*HiiDatabase
;
1164 EFI_HII_STRING_PROTOCOL
*HiiString
;
1165 EFI_FORM_BROWSER2_PROTOCOL
*FormBrowser2
;
1166 EFI_HII_CONFIG_ROUTING_PROTOCOL
*HiiConfigRouting
;
1169 DRIVER_SAMPLE_CONFIGURATION
*Configuration
;
1171 EFI_STRING ConfigRequestHdr
;
1174 // Initialize the local variables.
1176 ConfigRequestHdr
= NULL
;
1178 // Initialize screen dimensions for SendForm().
1179 // Remove 3 characters from top and bottom
1181 ZeroMem (&Screen
, sizeof (EFI_SCREEN_DESCRIPTOR
));
1182 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Screen
.RightColumn
, &Screen
.BottomRow
);
1185 Screen
.BottomRow
= Screen
.BottomRow
- 3;
1188 // Initialize driver private data
1190 PrivateData
= AllocateZeroPool (sizeof (DRIVER_SAMPLE_PRIVATE_DATA
));
1191 if (PrivateData
== NULL
) {
1192 return EFI_OUT_OF_RESOURCES
;
1195 PrivateData
->Signature
= DRIVER_SAMPLE_PRIVATE_SIGNATURE
;
1197 PrivateData
->ConfigAccess
.ExtractConfig
= ExtractConfig
;
1198 PrivateData
->ConfigAccess
.RouteConfig
= RouteConfig
;
1199 PrivateData
->ConfigAccess
.Callback
= DriverCallback
;
1200 PrivateData
->PasswordState
= BROWSER_STATE_VALIDATE_PASSWORD
;
1203 // Locate Hii Database protocol
1205 Status
= gBS
->LocateProtocol (&gEfiHiiDatabaseProtocolGuid
, NULL
, (VOID
**) &HiiDatabase
);
1206 if (EFI_ERROR (Status
)) {
1209 PrivateData
->HiiDatabase
= HiiDatabase
;
1212 // Locate HiiString protocol
1214 Status
= gBS
->LocateProtocol (&gEfiHiiStringProtocolGuid
, NULL
, (VOID
**) &HiiString
);
1215 if (EFI_ERROR (Status
)) {
1218 PrivateData
->HiiString
= HiiString
;
1221 // Locate Formbrowser2 protocol
1223 Status
= gBS
->LocateProtocol (&gEfiFormBrowser2ProtocolGuid
, NULL
, (VOID
**) &FormBrowser2
);
1224 if (EFI_ERROR (Status
)) {
1227 PrivateData
->FormBrowser2
= FormBrowser2
;
1230 // Locate ConfigRouting protocol
1232 Status
= gBS
->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid
, NULL
, (VOID
**) &HiiConfigRouting
);
1233 if (EFI_ERROR (Status
)) {
1236 PrivateData
->HiiConfigRouting
= HiiConfigRouting
;
1238 Status
= gBS
->InstallMultipleProtocolInterfaces (
1240 &gEfiDevicePathProtocolGuid
,
1241 &mHiiVendorDevicePath0
,
1242 &gEfiHiiConfigAccessProtocolGuid
,
1243 &PrivateData
->ConfigAccess
,
1246 ASSERT_EFI_ERROR (Status
);
1248 PrivateData
->DriverHandle
[0] = DriverHandle
[0];
1251 // Publish our HII data
1253 HiiHandle
[0] = HiiAddPackages (
1256 DriverSampleStrings
,
1260 if (HiiHandle
[0] == NULL
) {
1261 return EFI_OUT_OF_RESOURCES
;
1264 PrivateData
->HiiHandle
[0] = HiiHandle
[0];
1267 // Publish another Fromset
1269 Status
= gBS
->InstallMultipleProtocolInterfaces (
1271 &gEfiDevicePathProtocolGuid
,
1272 &mHiiVendorDevicePath1
,
1275 ASSERT_EFI_ERROR (Status
);
1277 PrivateData
->DriverHandle
[1] = DriverHandle
[1];
1279 HiiHandle
[1] = HiiAddPackages (
1282 DriverSampleStrings
,
1286 if (HiiHandle
[1] == NULL
) {
1287 DriverSampleUnload (ImageHandle
);
1288 return EFI_OUT_OF_RESOURCES
;
1291 PrivateData
->HiiHandle
[1] = HiiHandle
[1];
1294 // Very simple example of how one would update a string that is already
1295 // in the HII database
1297 NewString
= L
"700 Mhz";
1299 if (HiiSetString (HiiHandle
[0], STRING_TOKEN (STR_CPU_STRING2
), NewString
, NULL
) == 0) {
1300 DriverSampleUnload (ImageHandle
);
1301 return EFI_OUT_OF_RESOURCES
;
1304 HiiSetString (HiiHandle
[0], 0, NewString
, NULL
);
1307 // Initialize Name/Value name String ID
1309 PrivateData
->NameStringId
[0] = STR_NAME_VALUE_VAR_NAME0
;
1310 PrivateData
->NameStringId
[1] = STR_NAME_VALUE_VAR_NAME1
;
1311 PrivateData
->NameStringId
[2] = STR_NAME_VALUE_VAR_NAME2
;
1314 // Initialize configuration data
1316 Configuration
= &PrivateData
->Configuration
;
1317 ZeroMem (Configuration
, sizeof (DRIVER_SAMPLE_CONFIGURATION
));
1320 // Try to read NV config EFI variable first
1322 ConfigRequestHdr
= HiiConstructConfigHdr (&mFormSetGuid
, VariableName
, DriverHandle
[0]);
1323 ASSERT (ConfigRequestHdr
!= NULL
);
1325 BufferSize
= sizeof (DRIVER_SAMPLE_CONFIGURATION
);
1326 Status
= gRT
->GetVariable (VariableName
, &mFormSetGuid
, NULL
, &BufferSize
, Configuration
);
1327 if (EFI_ERROR (Status
)) {
1329 // Store zero data Buffer Storage to EFI variable
1331 Status
= gRT
->SetVariable(
1334 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1335 sizeof (DRIVER_SAMPLE_CONFIGURATION
),
1338 ASSERT (Status
== EFI_SUCCESS
);
1340 // EFI variable for NV config doesn't exit, we should build this variable
1341 // based on default values stored in IFR
1343 ActionFlag
= HiiSetToDefaults (ConfigRequestHdr
, EFI_HII_DEFAULT_CLASS_STANDARD
);
1344 ASSERT (ActionFlag
);
1347 // EFI variable does exist and Validate Current Setting
1349 ActionFlag
= HiiValidateSettings (ConfigRequestHdr
);
1350 ASSERT (ActionFlag
);
1353 FreePool (ConfigRequestHdr
);
1357 // In default, this driver is built into Flash device image,
1358 // the following code doesn't run.
1362 // Example of how to display only the item we sent to HII
1363 // When this driver is not built into Flash device image,
1364 // it need to call SendForm to show front page by itself.
1366 if (DISPLAY_ONLY_MY_ITEM
<= 1) {
1368 // Have the browser pull out our copy of the data, and only display our data
1370 Status
= FormBrowser2
->SendForm (
1372 &(HiiHandle
[DISPLAY_ONLY_MY_ITEM
]),
1380 HiiRemovePackages (HiiHandle
[0]);
1382 HiiRemovePackages (HiiHandle
[1]);
1389 Unloads the application and its installed protocol.
1391 @param[in] ImageHandle Handle that identifies the image to be unloaded.
1393 @retval EFI_SUCCESS The image has been unloaded.
1397 DriverSampleUnload (
1398 IN EFI_HANDLE ImageHandle
1402 if (DriverHandle
[0] != NULL
) {
1403 gBS
->UninstallMultipleProtocolInterfaces (
1405 &gEfiDevicePathProtocolGuid
,
1406 &mHiiVendorDevicePath0
,
1407 &gEfiHiiConfigAccessProtocolGuid
,
1408 &PrivateData
->ConfigAccess
,
1411 DriverHandle
[0] = NULL
;
1414 if (DriverHandle
[1] != NULL
) {
1415 gBS
->UninstallMultipleProtocolInterfaces (
1417 &gEfiDevicePathProtocolGuid
,
1418 &mHiiVendorDevicePath1
,
1421 DriverHandle
[1] = NULL
;
1424 if (PrivateData
->HiiHandle
[0] != NULL
) {
1425 HiiRemovePackages (PrivateData
->HiiHandle
[0]);
1428 if (PrivateData
->HiiHandle
[1] != NULL
) {
1429 HiiRemovePackages (PrivateData
->HiiHandle
[1]);
1432 if (PrivateData
!= NULL
) {
1433 for (Index
= 0; Index
< NAME_VALUE_NAME_NUMBER
; Index
++) {
1434 if (PrivateData
->NameValueName
[Index
] != NULL
) {
1435 FreePool (PrivateData
->NameValueName
[Index
]);
1438 FreePool (PrivateData
);