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 - 2010, Intel Corporation. All rights reserved.<BR>
6 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
, PasswordMaxSize
) != 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 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
;
383 BOOLEAN AllocatedRequest
;
385 if (Progress
== NULL
|| Results
== NULL
) {
386 return EFI_INVALID_PARAMETER
;
389 // Initialize the local variables.
391 ConfigRequestHdr
= NULL
;
392 ConfigRequest
= NULL
;
395 AllocatedRequest
= FALSE
;
397 PrivateData
= DRIVER_SAMPLE_PRIVATE_FROM_THIS (This
);
398 HiiConfigRouting
= PrivateData
->HiiConfigRouting
;
401 // Get Buffer Storage data from EFI variable.
402 // Try to get the current setting from variable.
404 BufferSize
= sizeof (DRIVER_SAMPLE_CONFIGURATION
);
405 Status
= gRT
->GetVariable (
410 &PrivateData
->Configuration
412 if (EFI_ERROR (Status
)) {
413 return EFI_NOT_FOUND
;
416 if (Request
== NULL
) {
418 // Request is set to NULL, construct full request string.
422 // Allocate and fill a buffer large enough to hold the <ConfigHdr> template
423 // followed by "&OFFSET=0&WIDTH=WWWWWWWWWWWWWWWW" followed by a Null-terminator
425 ConfigRequestHdr
= HiiConstructConfigHdr (&mFormSetGuid
, VariableName
, PrivateData
->DriverHandle
[0]);
426 Size
= (StrLen (ConfigRequestHdr
) + 32 + 1) * sizeof (CHAR16
);
427 ConfigRequest
= AllocateZeroPool (Size
);
428 ASSERT (ConfigRequest
!= NULL
);
429 AllocatedRequest
= TRUE
;
430 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", ConfigRequestHdr
, (UINT64
)BufferSize
);
431 FreePool (ConfigRequestHdr
);
434 // Check routing data in <ConfigHdr>.
435 // Note: if only one Storage is used, then this checking could be skipped.
437 if (!HiiIsConfigHdrMatch (Request
, &mFormSetGuid
, NULL
)) {
438 return EFI_NOT_FOUND
;
441 // Set Request to the unified request string.
443 ConfigRequest
= Request
;
445 // Check whether Request includes Request Element.
447 if (StrStr (Request
, L
"OFFSET") == NULL
) {
449 // Check Request Element does exist in Reques String
451 StrPointer
= StrStr (Request
, L
"PATH");
452 if (StrPointer
== NULL
) {
453 return EFI_INVALID_PARAMETER
;
455 if (StrStr (StrPointer
, L
"&") == NULL
) {
456 Size
= (StrLen (Request
) + 32 + 1) * sizeof (CHAR16
);
457 ConfigRequest
= AllocateZeroPool (Size
);
458 ASSERT (ConfigRequest
!= NULL
);
459 AllocatedRequest
= TRUE
;
460 UnicodeSPrint (ConfigRequest
, Size
, L
"%s&OFFSET=0&WIDTH=%016LX", Request
, (UINT64
)BufferSize
);
466 // Check if requesting Name/Value storage
468 if (StrStr (ConfigRequest
, L
"OFFSET") == NULL
) {
470 // Update Name/Value storage Names
472 Status
= LoadNameValueNames (PrivateData
);
473 if (EFI_ERROR (Status
)) {
478 // Allocate memory for <ConfigResp>, e.g. Name0=0x11, Name1=0x1234, Name2="ABCD"
479 // <Request> ::=<ConfigHdr>&Name0&Name1&Name2
480 // <ConfigResp>::=<ConfigHdr>&Name0=11&Name1=1234&Name2=0041004200430044
482 BufferSize
= (StrLen (ConfigRequest
) +
483 1 + sizeof (PrivateData
->Configuration
.NameValueVar0
) * 2 +
484 1 + sizeof (PrivateData
->Configuration
.NameValueVar1
) * 2 +
485 1 + sizeof (PrivateData
->Configuration
.NameValueVar2
) * 2 + 1) * sizeof (CHAR16
);
486 *Results
= AllocateZeroPool (BufferSize
);
487 ASSERT (*Results
!= NULL
);
488 StrCpy (*Results
, ConfigRequest
);
492 // Append value of NameValueVar0, type is UINT8
494 if ((Value
= StrStr (*Results
, PrivateData
->NameValueName
[0])) != NULL
) {
495 Value
+= StrLen (PrivateData
->NameValueName
[0]);
496 ValueStrLen
= ((sizeof (PrivateData
->Configuration
.NameValueVar0
) * 2) + 1);
497 CopyMem (Value
+ ValueStrLen
, Value
, StrSize (Value
));
499 BackupChar
= Value
[ValueStrLen
];
501 Value
+= UnicodeValueToString (
503 PREFIX_ZERO
| RADIX_HEX
,
504 PrivateData
->Configuration
.NameValueVar0
,
505 sizeof (PrivateData
->Configuration
.NameValueVar0
) * 2
511 // Append value of NameValueVar1, type is UINT16
513 if ((Value
= StrStr (*Results
, PrivateData
->NameValueName
[1])) != NULL
) {
514 Value
+= StrLen (PrivateData
->NameValueName
[1]);
515 ValueStrLen
= ((sizeof (PrivateData
->Configuration
.NameValueVar1
) * 2) + 1);
516 CopyMem (Value
+ ValueStrLen
, Value
, StrSize (Value
));
518 BackupChar
= Value
[ValueStrLen
];
520 Value
+= UnicodeValueToString (
522 PREFIX_ZERO
| RADIX_HEX
,
523 PrivateData
->Configuration
.NameValueVar1
,
524 sizeof (PrivateData
->Configuration
.NameValueVar1
) * 2
530 // Append value of NameValueVar2, type is CHAR16 *
532 if ((Value
= StrStr (*Results
, PrivateData
->NameValueName
[2])) != NULL
) {
533 Value
+= StrLen (PrivateData
->NameValueName
[2]);
534 ValueStrLen
= StrLen (PrivateData
->Configuration
.NameValueVar2
) * 4 + 1;
535 CopyMem (Value
+ ValueStrLen
, Value
, StrSize (Value
));
539 // Convert Unicode String to Config String, e.g. "ABCD" => "0041004200430044"
541 StrPointer
= (CHAR16
*) PrivateData
->Configuration
.NameValueVar2
;
542 for (; *StrPointer
!= L
'\0'; StrPointer
++) {
543 Value
+= UnicodeValueToString (Value
, PREFIX_ZERO
| RADIX_HEX
, *StrPointer
, 4);
547 Status
= EFI_SUCCESS
;
550 // Convert buffer data to <ConfigResp> by helper function BlockToConfig()
552 Status
= HiiConfigRouting
->BlockToConfig (
555 (UINT8
*) &PrivateData
->Configuration
,
563 // Free the allocated config request string.
565 if (AllocatedRequest
) {
566 FreePool (ConfigRequest
);
569 // Set Progress string to the original request string.
571 if (Request
== NULL
) {
573 } else if (StrStr (Request
, L
"OFFSET") == NULL
) {
574 *Progress
= Request
+ StrLen (Request
);
582 This function processes the results of changes in configuration.
584 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
585 @param Configuration A null-terminated Unicode string in <ConfigResp>
587 @param Progress A pointer to a string filled in with the offset of
588 the most recent '&' before the first failing
589 name/value pair (or the beginning of the string if
590 the failure is in the first name/value pair) or
591 the terminating NULL if all was successful.
593 @retval EFI_SUCCESS The Results is processed successfully.
594 @retval EFI_INVALID_PARAMETER Configuration is NULL.
595 @retval EFI_NOT_FOUND Routing data doesn't match any storage in this
602 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
603 IN CONST EFI_STRING Configuration
,
604 OUT EFI_STRING
*Progress
609 DRIVER_SAMPLE_PRIVATE_DATA
*PrivateData
;
610 EFI_HII_CONFIG_ROUTING_PROTOCOL
*HiiConfigRouting
;
619 if (Configuration
== NULL
|| Progress
== NULL
) {
620 return EFI_INVALID_PARAMETER
;
623 PrivateData
= DRIVER_SAMPLE_PRIVATE_FROM_THIS (This
);
624 HiiConfigRouting
= PrivateData
->HiiConfigRouting
;
625 *Progress
= Configuration
;
628 // Check routing data in <ConfigHdr>.
629 // Note: if only one Storage is used, then this checking could be skipped.
631 if (!HiiIsConfigHdrMatch (Configuration
, &mFormSetGuid
, NULL
)) {
632 return EFI_NOT_FOUND
;
636 // Get Buffer Storage data from EFI variable
638 BufferSize
= sizeof (DRIVER_SAMPLE_CONFIGURATION
);
639 Status
= gRT
->GetVariable (
644 &PrivateData
->Configuration
646 if (EFI_ERROR (Status
)) {
651 // Check if configuring Name/Value storage
653 if (StrStr (Configuration
, L
"OFFSET") == NULL
) {
655 // Update Name/Value storage Names
657 Status
= LoadNameValueNames (PrivateData
);
658 if (EFI_ERROR (Status
)) {
663 // Convert value for NameValueVar0
665 if ((Value
= StrStr (Configuration
, PrivateData
->NameValueName
[0])) != NULL
) {
669 Value
+= StrLen (PrivateData
->NameValueName
[0]);
674 StrPtr
= StrStr (Value
, L
"&");
675 if (StrPtr
== NULL
) {
676 StrPtr
= Value
+ StrLen (Value
);
679 // Convert Value to Buffer data
681 DataBuffer
= (UINT8
*) &PrivateData
->Configuration
.NameValueVar0
;
682 ZeroMem (TemStr
, sizeof (TemStr
));
683 for (Index
= 0, StrPtr
--; StrPtr
>= Value
; StrPtr
--, Index
++) {
685 DigitUint8
= (UINT8
) StrHexToUint64 (TemStr
);
686 if ((Index
& 1) == 0) {
687 DataBuffer
[Index
/2] = DigitUint8
;
689 DataBuffer
[Index
/2] = (UINT8
) ((UINT8
) (DigitUint8
<< 4) + DataBuffer
[Index
/2]);
695 // Convert value for NameValueVar1
697 if ((Value
= StrStr (Configuration
, PrivateData
->NameValueName
[1])) != NULL
) {
701 Value
+= StrLen (PrivateData
->NameValueName
[1]);
706 StrPtr
= StrStr (Value
, L
"&");
707 if (StrPtr
== NULL
) {
708 StrPtr
= Value
+ StrLen (Value
);
711 // Convert Value to Buffer data
713 DataBuffer
= (UINT8
*) &PrivateData
->Configuration
.NameValueVar1
;
714 ZeroMem (TemStr
, sizeof (TemStr
));
715 for (Index
= 0, StrPtr
--; StrPtr
>= Value
; StrPtr
--, Index
++) {
717 DigitUint8
= (UINT8
) StrHexToUint64 (TemStr
);
718 if ((Index
& 1) == 0) {
719 DataBuffer
[Index
/2] = DigitUint8
;
721 DataBuffer
[Index
/2] = (UINT8
) ((UINT8
) (DigitUint8
<< 4) + DataBuffer
[Index
/2]);
727 // Convert value for NameValueVar2
729 if ((Value
= StrStr (Configuration
, PrivateData
->NameValueName
[2])) != NULL
) {
733 Value
+= StrLen (PrivateData
->NameValueName
[2]);
738 StrPtr
= StrStr (Value
, L
"&");
739 if (StrPtr
== NULL
) {
740 StrPtr
= Value
+ StrLen (Value
);
743 // Convert Config String to Unicode String, e.g "0041004200430044" => "ABCD"
745 StrBuffer
= (CHAR16
*) PrivateData
->Configuration
.NameValueVar2
;
746 ZeroMem (TemStr
, sizeof (TemStr
));
747 while (Value
< StrPtr
) {
748 StrnCpy (TemStr
, Value
, 4);
749 *(StrBuffer
++) = (CHAR16
) StrHexToUint64 (TemStr
);
756 // Store Buffer Storage back to EFI variable
758 Status
= gRT
->SetVariable(
761 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
762 sizeof (DRIVER_SAMPLE_CONFIGURATION
),
763 &PrivateData
->Configuration
770 // Convert <ConfigResp> to buffer data by helper function ConfigToBlock()
772 BufferSize
= sizeof (DRIVER_SAMPLE_CONFIGURATION
);
773 Status
= HiiConfigRouting
->ConfigToBlock (
776 (UINT8
*) &PrivateData
->Configuration
,
780 if (EFI_ERROR (Status
)) {
785 // Store Buffer Storage back to EFI variable
787 Status
= gRT
->SetVariable(
790 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
791 sizeof (DRIVER_SAMPLE_CONFIGURATION
),
792 &PrivateData
->Configuration
800 This function processes the results of changes in configuration.
802 @param This Points to the EFI_HII_CONFIG_ACCESS_PROTOCOL.
803 @param Action Specifies the type of action taken by the browser.
804 @param QuestionId A unique value which is sent to the original
805 exporting driver so that it can identify the type
807 @param Type The type of value for the question.
808 @param Value A pointer to the data being sent to the original
810 @param ActionRequest On return, points to the action requested by the
813 @retval EFI_SUCCESS The callback successfully handled the action.
814 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the
815 variable and its data.
816 @retval EFI_DEVICE_ERROR The variable could not be saved.
817 @retval EFI_UNSUPPORTED The specified Action is not supported by the
824 IN CONST EFI_HII_CONFIG_ACCESS_PROTOCOL
*This
,
825 IN EFI_BROWSER_ACTION Action
,
826 IN EFI_QUESTION_ID QuestionId
,
828 IN EFI_IFR_TYPE_VALUE
*Value
,
829 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
832 DRIVER_SAMPLE_PRIVATE_DATA
*PrivateData
;
835 VOID
*StartOpCodeHandle
;
836 VOID
*OptionsOpCodeHandle
;
837 EFI_IFR_GUID_LABEL
*StartLabel
;
838 VOID
*EndOpCodeHandle
;
839 EFI_IFR_GUID_LABEL
*EndLabel
;
841 DRIVER_SAMPLE_CONFIGURATION
*Configuration
;
844 if (Action
== EFI_BROWSER_ACTION_FORM_OPEN
) {
845 if (QuestionId
== 0x1234) {
847 // Sample CallBack for UEFI FORM_OPEN action:
848 // Add Save action into Form 3 when Form 1 is opened.
849 // This will be done only in FORM_OPEN CallBack of question with ID 0x1234 from Form 1.
851 PrivateData
= DRIVER_SAMPLE_PRIVATE_FROM_THIS (This
);
854 // Initialize the container for dynamic opcodes
856 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
857 ASSERT (StartOpCodeHandle
!= NULL
);
860 // Create Hii Extend Label OpCode as the start opcode
862 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (StartOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
863 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
864 StartLabel
->Number
= LABEL_UPDATE2
;
866 HiiCreateActionOpCode (
867 StartOpCodeHandle
, // Container for dynamic created opcodes
868 0x1238, // Question ID
869 STRING_TOKEN(STR_SAVE_TEXT
), // Prompt text
870 STRING_TOKEN(STR_SAVE_TEXT
), // Help text
871 EFI_IFR_FLAG_CALLBACK
, // Question flag
872 0 // Action String ID
876 PrivateData
->HiiHandle
[0], // HII handle
877 &mFormSetGuid
, // Formset GUID
879 StartOpCodeHandle
, // Label for where to insert opcodes
883 HiiFreeOpCodeHandle (StartOpCodeHandle
);
888 if (Action
== EFI_BROWSER_ACTION_FORM_CLOSE
) {
889 if (QuestionId
== 0x5678) {
891 // Sample CallBack for UEFI FORM_CLOSE action:
892 // Show up a pop-up to specify Form 3 will be closed when exit Form 3.
896 EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
,
899 L
"You are going to leave third Form!",
900 L
"Press ESC or ENTER to continue ...",
904 } while ((Key
.ScanCode
!= SCAN_ESC
) && (Key
.UnicodeChar
!= CHAR_CARRIAGE_RETURN
));
909 if ((Value
== NULL
) || (ActionRequest
== NULL
)) {
910 return EFI_INVALID_PARAMETER
;
913 if ((Type
== EFI_IFR_TYPE_STRING
) && (Value
->string
== 0)) {
914 return EFI_INVALID_PARAMETER
;
917 Status
= EFI_SUCCESS
;
918 PrivateData
= DRIVER_SAMPLE_PRIVATE_FROM_THIS (This
);
920 switch (QuestionId
) {
923 // Initialize the container for dynamic opcodes
925 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
926 ASSERT (StartOpCodeHandle
!= NULL
);
928 EndOpCodeHandle
= HiiAllocateOpCodeHandle ();
929 ASSERT (EndOpCodeHandle
!= NULL
);
932 // Create Hii Extend Label OpCode as the start opcode
934 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (StartOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
935 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
936 StartLabel
->Number
= LABEL_UPDATE1
;
939 // Create Hii Extend Label OpCode as the end opcode
941 EndLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (EndOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
942 EndLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
943 EndLabel
->Number
= LABEL_END
;
945 HiiCreateActionOpCode (
946 StartOpCodeHandle
, // Container for dynamic created opcodes
947 0x1237, // Question ID
948 STRING_TOKEN(STR_EXIT_TEXT
), // Prompt text
949 STRING_TOKEN(STR_EXIT_TEXT
), // Help text
950 EFI_IFR_FLAG_CALLBACK
, // Question flag
951 0 // Action String ID
955 // Create Option OpCode
957 OptionsOpCodeHandle
= HiiAllocateOpCodeHandle ();
958 ASSERT (OptionsOpCodeHandle
!= NULL
);
960 HiiCreateOneOfOptionOpCode (
962 STRING_TOKEN (STR_BOOT_OPTION1
),
964 EFI_IFR_NUMERIC_SIZE_1
,
968 HiiCreateOneOfOptionOpCode (
970 STRING_TOKEN (STR_BOOT_OPTION2
),
972 EFI_IFR_NUMERIC_SIZE_1
,
977 // Prepare initial value for the dynamic created oneof Question
979 PrivateData
->Configuration
.DynamicOneof
= 2;
980 Status
= gRT
->SetVariable(
983 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
984 sizeof (DRIVER_SAMPLE_CONFIGURATION
),
985 &PrivateData
->Configuration
989 // Set initial vlaue of dynamic created oneof Question in Form Browser
991 Configuration
= AllocateZeroPool (sizeof (DRIVER_SAMPLE_CONFIGURATION
));
992 ASSERT (Configuration
!= NULL
);
993 if (HiiGetBrowserData (&mFormSetGuid
, VariableName
, sizeof (DRIVER_SAMPLE_CONFIGURATION
), (UINT8
*) Configuration
)) {
994 Configuration
->DynamicOneof
= 2;
997 // Update uncommitted data of Browser
1002 sizeof (DRIVER_SAMPLE_CONFIGURATION
),
1003 (UINT8
*) Configuration
,
1007 FreePool (Configuration
);
1009 HiiCreateOneOfOpCode (
1010 StartOpCodeHandle
, // Container for dynamic created opcodes
1011 0x8001, // Question ID (or call it "key")
1012 CONFIGURATION_VARSTORE_ID
, // VarStore ID
1013 (UINT16
) DYNAMIC_ONE_OF_VAR_OFFSET
, // Offset in Buffer Storage
1014 STRING_TOKEN (STR_ONE_OF_PROMPT
), // Question prompt text
1015 STRING_TOKEN (STR_ONE_OF_HELP
), // Question help text
1016 EFI_IFR_FLAG_CALLBACK
, // Question flag
1017 EFI_IFR_NUMERIC_SIZE_1
, // Data type of Question Value
1018 OptionsOpCodeHandle
, // Option Opcode list
1019 NULL
// Default Opcode is NULl
1022 HiiCreateOrderedListOpCode (
1023 StartOpCodeHandle
, // Container for dynamic created opcodes
1024 0x8002, // Question ID
1025 CONFIGURATION_VARSTORE_ID
, // VarStore ID
1026 (UINT16
) DYNAMIC_ORDERED_LIST_VAR_OFFSET
, // Offset in Buffer Storage
1027 STRING_TOKEN (STR_BOOT_OPTIONS
), // Question prompt text
1028 STRING_TOKEN (STR_BOOT_OPTIONS
), // Question help text
1029 EFI_IFR_FLAG_RESET_REQUIRED
, // Question flag
1030 0, // Ordered list flag, e.g. EFI_IFR_UNIQUE_SET
1031 EFI_IFR_NUMERIC_SIZE_1
, // Data type of Question value
1032 5, // Maximum container
1033 OptionsOpCodeHandle
, // Option Opcode list
1034 NULL
// Default Opcode is NULl
1037 HiiCreateTextOpCode (
1039 STRING_TOKEN(STR_TEXT_SAMPLE_HELP
),
1040 STRING_TOKEN(STR_TEXT_SAMPLE_HELP
),
1041 STRING_TOKEN(STR_TEXT_SAMPLE_STRING
)
1044 HiiCreateDateOpCode (
1049 STRING_TOKEN(STR_DATE_SAMPLE_HELP
),
1050 STRING_TOKEN(STR_DATE_SAMPLE_HELP
),
1052 QF_DATE_STORAGE_TIME
,
1056 HiiCreateTimeOpCode (
1061 STRING_TOKEN(STR_TIME_SAMPLE_HELP
),
1062 STRING_TOKEN(STR_TIME_SAMPLE_HELP
),
1064 QF_TIME_STORAGE_TIME
,
1068 HiiCreateGotoOpCode (
1069 StartOpCodeHandle
, // Container for dynamic created opcodes
1070 1, // Target Form ID
1071 STRING_TOKEN (STR_GOTO_FORM1
), // Prompt text
1072 STRING_TOKEN (STR_GOTO_HELP
), // Help text
1074 0x8003 // Question ID
1078 PrivateData
->HiiHandle
[0], // HII handle
1079 &mFormSetGuid
, // Formset GUID
1081 StartOpCodeHandle
, // Label for where to insert opcodes
1082 EndOpCodeHandle
// Replace data
1085 HiiFreeOpCodeHandle (StartOpCodeHandle
);
1086 HiiFreeOpCodeHandle (OptionsOpCodeHandle
);
1087 HiiFreeOpCodeHandle (EndOpCodeHandle
);
1092 // We will reach here once the Question is refreshed
1096 // Initialize the container for dynamic opcodes
1098 StartOpCodeHandle
= HiiAllocateOpCodeHandle ();
1099 ASSERT (StartOpCodeHandle
!= NULL
);
1102 // Create Hii Extend Label OpCode as the start opcode
1104 StartLabel
= (EFI_IFR_GUID_LABEL
*) HiiCreateGuidOpCode (StartOpCodeHandle
, &gEfiIfrTianoGuid
, NULL
, sizeof (EFI_IFR_GUID_LABEL
));
1105 StartLabel
->ExtendOpCode
= EFI_IFR_EXTEND_OP_LABEL
;
1106 StartLabel
->Number
= LABEL_UPDATE2
;
1108 HiiCreateActionOpCode (
1109 StartOpCodeHandle
, // Container for dynamic created opcodes
1110 0x1237, // Question ID
1111 STRING_TOKEN(STR_EXIT_TEXT
), // Prompt text
1112 STRING_TOKEN(STR_EXIT_TEXT
), // Help text
1113 EFI_IFR_FLAG_CALLBACK
, // Question flag
1114 0 // Action String ID
1118 PrivateData
->HiiHandle
[0], // HII handle
1119 &mFormSetGuid
, // Formset GUID
1121 StartOpCodeHandle
, // Label for where to insert opcodes
1125 HiiFreeOpCodeHandle (StartOpCodeHandle
);
1128 // Refresh the Question value
1130 PrivateData
->Configuration
.DynamicRefresh
++;
1131 Status
= gRT
->SetVariable(
1134 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1135 sizeof (DRIVER_SAMPLE_CONFIGURATION
),
1136 &PrivateData
->Configuration
1140 // Change an EFI Variable storage (MyEfiVar) asynchronous, this will cause
1141 // the first statement in Form 3 be suppressed
1145 Status
= gRT
->SetVariable(
1148 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1156 // User press "Exit now", request Browser to exit
1158 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_EXIT
;
1163 // User press "Save now", request Browser to save the uncommitted data.
1165 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_SUBMIT
;
1170 // When try to set a new password, user will be chanlleged with old password.
1171 // The Callback is responsible for validating old password input by user,
1172 // If Callback return EFI_SUCCESS, it indicates validation pass.
1174 switch (PrivateData
->PasswordState
) {
1175 case BROWSER_STATE_VALIDATE_PASSWORD
:
1176 Status
= ValidatePassword (PrivateData
, Value
->string
);
1177 if (Status
== EFI_SUCCESS
) {
1178 PrivateData
->PasswordState
= BROWSER_STATE_SET_PASSWORD
;
1182 case BROWSER_STATE_SET_PASSWORD
:
1183 Status
= SetPassword (PrivateData
, Value
->string
);
1184 PrivateData
->PasswordState
= BROWSER_STATE_VALIDATE_PASSWORD
;
1195 // EfiVarstore question takes sample action (print value as debug information)
1196 // after read/write question.
1199 Status
= gRT
->GetVariable(
1206 ASSERT_EFI_ERROR (Status
);
1207 DEBUG ((DEBUG_INFO
, "EfiVarstore question: Tall value is %d with value width %d\n", MyVar
, MyVarSize
));
1216 Main entry for this driver.
1218 @param ImageHandle Image handle this driver.
1219 @param SystemTable Pointer to SystemTable.
1221 @retval EFI_SUCESS This function always complete successfully.
1227 IN EFI_HANDLE ImageHandle
,
1228 IN EFI_SYSTEM_TABLE
*SystemTable
1232 EFI_HII_HANDLE HiiHandle
[2];
1233 EFI_SCREEN_DESCRIPTOR Screen
;
1234 EFI_HII_DATABASE_PROTOCOL
*HiiDatabase
;
1235 EFI_HII_STRING_PROTOCOL
*HiiString
;
1236 EFI_FORM_BROWSER2_PROTOCOL
*FormBrowser2
;
1237 EFI_HII_CONFIG_ROUTING_PROTOCOL
*HiiConfigRouting
;
1240 DRIVER_SAMPLE_CONFIGURATION
*Configuration
;
1242 EFI_STRING ConfigRequestHdr
;
1245 // Initialize the local variables.
1247 ConfigRequestHdr
= NULL
;
1249 // Initialize screen dimensions for SendForm().
1250 // Remove 3 characters from top and bottom
1252 ZeroMem (&Screen
, sizeof (EFI_SCREEN_DESCRIPTOR
));
1253 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Screen
.RightColumn
, &Screen
.BottomRow
);
1256 Screen
.BottomRow
= Screen
.BottomRow
- 3;
1259 // Initialize driver private data
1261 PrivateData
= AllocateZeroPool (sizeof (DRIVER_SAMPLE_PRIVATE_DATA
));
1262 if (PrivateData
== NULL
) {
1263 return EFI_OUT_OF_RESOURCES
;
1266 PrivateData
->Signature
= DRIVER_SAMPLE_PRIVATE_SIGNATURE
;
1268 PrivateData
->ConfigAccess
.ExtractConfig
= ExtractConfig
;
1269 PrivateData
->ConfigAccess
.RouteConfig
= RouteConfig
;
1270 PrivateData
->ConfigAccess
.Callback
= DriverCallback
;
1271 PrivateData
->PasswordState
= BROWSER_STATE_VALIDATE_PASSWORD
;
1274 // Locate Hii Database protocol
1276 Status
= gBS
->LocateProtocol (&gEfiHiiDatabaseProtocolGuid
, NULL
, (VOID
**) &HiiDatabase
);
1277 if (EFI_ERROR (Status
)) {
1280 PrivateData
->HiiDatabase
= HiiDatabase
;
1283 // Locate HiiString protocol
1285 Status
= gBS
->LocateProtocol (&gEfiHiiStringProtocolGuid
, NULL
, (VOID
**) &HiiString
);
1286 if (EFI_ERROR (Status
)) {
1289 PrivateData
->HiiString
= HiiString
;
1292 // Locate Formbrowser2 protocol
1294 Status
= gBS
->LocateProtocol (&gEfiFormBrowser2ProtocolGuid
, NULL
, (VOID
**) &FormBrowser2
);
1295 if (EFI_ERROR (Status
)) {
1298 PrivateData
->FormBrowser2
= FormBrowser2
;
1301 // Locate ConfigRouting protocol
1303 Status
= gBS
->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid
, NULL
, (VOID
**) &HiiConfigRouting
);
1304 if (EFI_ERROR (Status
)) {
1307 PrivateData
->HiiConfigRouting
= HiiConfigRouting
;
1309 Status
= gBS
->InstallMultipleProtocolInterfaces (
1311 &gEfiDevicePathProtocolGuid
,
1312 &mHiiVendorDevicePath0
,
1313 &gEfiHiiConfigAccessProtocolGuid
,
1314 &PrivateData
->ConfigAccess
,
1317 ASSERT_EFI_ERROR (Status
);
1319 PrivateData
->DriverHandle
[0] = DriverHandle
[0];
1322 // Publish our HII data
1324 HiiHandle
[0] = HiiAddPackages (
1327 DriverSampleStrings
,
1331 if (HiiHandle
[0] == NULL
) {
1332 return EFI_OUT_OF_RESOURCES
;
1335 PrivateData
->HiiHandle
[0] = HiiHandle
[0];
1338 // Publish another Fromset
1340 Status
= gBS
->InstallMultipleProtocolInterfaces (
1342 &gEfiDevicePathProtocolGuid
,
1343 &mHiiVendorDevicePath1
,
1346 ASSERT_EFI_ERROR (Status
);
1348 PrivateData
->DriverHandle
[1] = DriverHandle
[1];
1350 HiiHandle
[1] = HiiAddPackages (
1353 DriverSampleStrings
,
1357 if (HiiHandle
[1] == NULL
) {
1358 DriverSampleUnload (ImageHandle
);
1359 return EFI_OUT_OF_RESOURCES
;
1362 PrivateData
->HiiHandle
[1] = HiiHandle
[1];
1365 // Very simple example of how one would update a string that is already
1366 // in the HII database
1368 NewString
= L
"700 Mhz";
1370 if (HiiSetString (HiiHandle
[0], STRING_TOKEN (STR_CPU_STRING2
), NewString
, NULL
) == 0) {
1371 DriverSampleUnload (ImageHandle
);
1372 return EFI_OUT_OF_RESOURCES
;
1375 HiiSetString (HiiHandle
[0], 0, NewString
, NULL
);
1378 // Initialize Name/Value name String ID
1380 PrivateData
->NameStringId
[0] = STR_NAME_VALUE_VAR_NAME0
;
1381 PrivateData
->NameStringId
[1] = STR_NAME_VALUE_VAR_NAME1
;
1382 PrivateData
->NameStringId
[2] = STR_NAME_VALUE_VAR_NAME2
;
1385 // Initialize configuration data
1387 Configuration
= &PrivateData
->Configuration
;
1388 ZeroMem (Configuration
, sizeof (DRIVER_SAMPLE_CONFIGURATION
));
1391 // Try to read NV config EFI variable first
1393 ConfigRequestHdr
= HiiConstructConfigHdr (&mFormSetGuid
, VariableName
, DriverHandle
[0]);
1394 ASSERT (ConfigRequestHdr
!= NULL
);
1396 BufferSize
= sizeof (DRIVER_SAMPLE_CONFIGURATION
);
1397 Status
= gRT
->GetVariable (VariableName
, &mFormSetGuid
, NULL
, &BufferSize
, Configuration
);
1398 if (EFI_ERROR (Status
)) {
1400 // Store zero data Buffer Storage to EFI variable
1402 Status
= gRT
->SetVariable(
1405 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
,
1406 sizeof (DRIVER_SAMPLE_CONFIGURATION
),
1409 ASSERT (Status
== EFI_SUCCESS
);
1411 // EFI variable for NV config doesn't exit, we should build this variable
1412 // based on default values stored in IFR
1414 ActionFlag
= HiiSetToDefaults (ConfigRequestHdr
, EFI_HII_DEFAULT_CLASS_STANDARD
);
1415 ASSERT (ActionFlag
);
1418 // EFI variable does exist and Validate Current Setting
1420 ActionFlag
= HiiValidateSettings (ConfigRequestHdr
);
1421 ASSERT (ActionFlag
);
1424 FreePool (ConfigRequestHdr
);
1428 // In default, this driver is built into Flash device image,
1429 // the following code doesn't run.
1433 // Example of how to display only the item we sent to HII
1434 // When this driver is not built into Flash device image,
1435 // it need to call SendForm to show front page by itself.
1437 if (DISPLAY_ONLY_MY_ITEM
<= 1) {
1439 // Have the browser pull out our copy of the data, and only display our data
1441 Status
= FormBrowser2
->SendForm (
1443 &(HiiHandle
[DISPLAY_ONLY_MY_ITEM
]),
1451 HiiRemovePackages (HiiHandle
[0]);
1453 HiiRemovePackages (HiiHandle
[1]);
1460 Unloads the application and its installed protocol.
1462 @param[in] ImageHandle Handle that identifies the image to be unloaded.
1464 @retval EFI_SUCCESS The image has been unloaded.
1468 DriverSampleUnload (
1469 IN EFI_HANDLE ImageHandle
1474 ASSERT (PrivateData
!= NULL
);
1476 if (DriverHandle
[0] != NULL
) {
1477 gBS
->UninstallMultipleProtocolInterfaces (
1479 &gEfiDevicePathProtocolGuid
,
1480 &mHiiVendorDevicePath0
,
1481 &gEfiHiiConfigAccessProtocolGuid
,
1482 &PrivateData
->ConfigAccess
,
1485 DriverHandle
[0] = NULL
;
1488 if (DriverHandle
[1] != NULL
) {
1489 gBS
->UninstallMultipleProtocolInterfaces (
1491 &gEfiDevicePathProtocolGuid
,
1492 &mHiiVendorDevicePath1
,
1495 DriverHandle
[1] = NULL
;
1498 if (PrivateData
->HiiHandle
[0] != NULL
) {
1499 HiiRemovePackages (PrivateData
->HiiHandle
[0]);
1502 if (PrivateData
->HiiHandle
[1] != NULL
) {
1503 HiiRemovePackages (PrivateData
->HiiHandle
[1]);
1506 for (Index
= 0; Index
< NAME_VALUE_NAME_NUMBER
; Index
++) {
1507 if (PrivateData
->NameValueName
[Index
] != NULL
) {
1508 FreePool (PrivateData
->NameValueName
[Index
]);
1511 FreePool (PrivateData
);