3 This library class defines a set of interfaces to customize Ui module
5 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available under
7 the terms and conditions of the BSD License that accompanies this distribution.
8 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 <Guid/MdeModuleHii.h>
18 #include <Guid/GlobalVariable.h>
20 #include <Protocol/HiiConfigAccess.h>
21 #include <Protocol/HiiString.h>
23 #include <Library/HiiLib.h>
24 #include <Library/DebugLib.h>
25 #include <Library/UefiLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/PcdLib.h>
28 #include <Library/MemoryAllocationLib.h>
29 #include <Library/UefiRuntimeServicesTableLib.h>
30 #include <Library/UefiHiiServicesLib.h>
31 #include <Library/DevicePathLib.h>
32 #include <Library/UefiBootServicesTableLib.h>
33 #include "FrontPageCustomizedUiSupport.h"
36 // This is the VFR compiler generated header file which defines the
37 // string identifiers.
39 #define PRINTABLE_LANGUAGE_NAME_STRING_ID 0x0001
41 #define UI_HII_DRIVER_LIST_SIZE 0x8
43 #define FRONT_PAGE_KEY_CONTINUE 0x1000
44 #define FRONT_PAGE_KEY_RESET 0x1001
45 #define FRONT_PAGE_KEY_LANGUAGE 0x1002
48 EFI_STRING_ID PromptId
;
50 EFI_STRING_ID DevicePathId
;
52 BOOLEAN EmptyLineAfter
;
53 } UI_HII_DRIVER_INSTANCE
;
55 CHAR8
*gLanguageString
;
56 EFI_STRING_ID
*gLanguageToken
;
57 UI_HII_DRIVER_INSTANCE
*gHiiDriverList
;
58 extern EFI_HII_HANDLE gStringPackHandle
;
59 UINT8 gCurrentLanguageIndex
;
63 Get next language from language code list (with separator ';').
65 If LangCode is NULL, then ASSERT.
66 If Lang is NULL, then ASSERT.
68 @param LangCode On input: point to first language in the list. On
69 output: point to next language in the list, or
70 NULL if no more language in the list.
71 @param Lang The first language in the list.
76 IN OUT CHAR8
**LangCode
,
83 ASSERT (LangCode
!= NULL
);
84 ASSERT (*LangCode
!= NULL
);
85 ASSERT (Lang
!= NULL
);
88 StringPtr
= *LangCode
;
89 while (StringPtr
[Index
] != 0 && StringPtr
[Index
] != ';') {
93 CopyMem (Lang
, StringPtr
, Index
);
96 if (StringPtr
[Index
] == ';') {
99 *LangCode
= StringPtr
+ Index
;
103 This function processes the language changes in configuration.
105 @param Value A pointer to the data being sent to the original exporting driver.
108 @retval TRUE The callback successfully handled the action.
109 @retval FALSE The callback not supported in this handler.
113 LanguageChangeHandler (
114 IN EFI_IFR_TYPE_VALUE
*Value
123 // Allocate working buffer for RFC 4646 language in supported LanguageString.
125 Lang
= AllocatePool (AsciiStrSize (gLanguageString
));
126 ASSERT (Lang
!= NULL
);
129 LangCode
= gLanguageString
;
130 while (*LangCode
!= 0) {
131 GetNextLanguage (&LangCode
, Lang
);
133 if (Index
== Value
->u8
) {
134 gCurrentLanguageIndex
= Value
->u8
;
141 if (Index
== Value
->u8
) {
142 Status
= gRT
->SetVariable (
144 &gEfiGlobalVariableGuid
,
145 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
149 if (EFI_ERROR (Status
)) {
151 return EFI_DEVICE_ERROR
;
162 This function processes the results of changes in configuration.
165 @param HiiHandle Points to the hii handle for this formset.
166 @param Action Specifies the type of action taken by the browser.
167 @param QuestionId A unique value which is sent to the original exporting driver
168 so that it can identify the type of data to expect.
169 @param Type The type of value for the question.
170 @param Value A pointer to the data being sent to the original exporting driver.
171 @param ActionRequest On return, points to the action requested by the callback function.
172 @param Status Return the handle status.
174 @retval TRUE The callback successfully handled the action.
175 @retval FALSE The callback not supported in this handler.
179 UiSupportLibCallbackHandler (
180 IN EFI_HII_HANDLE HiiHandle
,
181 IN EFI_BROWSER_ACTION Action
,
182 IN EFI_QUESTION_ID QuestionId
,
184 IN EFI_IFR_TYPE_VALUE
*Value
,
185 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
,
186 OUT EFI_STATUS
*Status
189 if (QuestionId
!= FRONT_PAGE_KEY_CONTINUE
&&
190 QuestionId
!= FRONT_PAGE_KEY_RESET
&&
191 QuestionId
!= FRONT_PAGE_KEY_LANGUAGE
) {
195 if (Action
== EFI_BROWSER_ACTION_RETRIEVE
) {
196 if (QuestionId
== FRONT_PAGE_KEY_LANGUAGE
) {
197 Value
->u8
= gCurrentLanguageIndex
;
198 *Status
= EFI_SUCCESS
;
200 *Status
= EFI_UNSUPPORTED
;
205 if (Action
!= EFI_BROWSER_ACTION_CHANGED
) {
207 // Do nothing for other UEFI Action. Only do call back when data is changed.
209 *Status
= EFI_UNSUPPORTED
;
213 if (Action
== EFI_BROWSER_ACTION_CHANGED
) {
214 if ((Value
== NULL
) || (ActionRequest
== NULL
)) {
215 *Status
= EFI_INVALID_PARAMETER
;
219 *Status
= EFI_SUCCESS
;
220 switch (QuestionId
) {
221 case FRONT_PAGE_KEY_CONTINUE
:
223 // This is the continue - clear the screen and return an error to get out of FrontPage loop
225 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_EXIT
;
228 case FRONT_PAGE_KEY_LANGUAGE
:
229 *Status
= LanguageChangeHandler(Value
);
232 case FRONT_PAGE_KEY_RESET
:
236 gRT
->ResetSystem (EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
237 *Status
= EFI_UNSUPPORTED
;
248 Create Select language menu in the front page with oneof opcode.
250 @param[in] HiiHandle The hii handle for the Uiapp driver.
251 @param[in] StartOpCodeHandle The opcode handle to save the new opcode.
255 UiCreateLanguageMenu (
256 IN EFI_HII_HANDLE HiiHandle
,
257 IN VOID
*StartOpCodeHandle
265 CHAR16
*StringBuffer
;
266 VOID
*OptionsOpCodeHandle
;
269 EFI_HII_STRING_PROTOCOL
*HiiString
;
275 // Init OpCode Handle and Allocate space for creation of UpdateData Buffer
277 OptionsOpCodeHandle
= HiiAllocateOpCodeHandle ();
278 ASSERT (OptionsOpCodeHandle
!= NULL
);
280 GetEfiGlobalVariable2 (L
"PlatformLang", (VOID
**)&CurrentLang
, NULL
);
283 // Get Support language list from variable.
285 GetEfiGlobalVariable2 (L
"PlatformLangCodes", (VOID
**)&gLanguageString
, NULL
);
286 if (gLanguageString
== NULL
) {
287 gLanguageString
= AllocateCopyPool (
288 AsciiStrSize ((CHAR8
*) PcdGetPtr (PcdUefiVariableDefaultPlatformLangCodes
)),
289 (CHAR8
*) PcdGetPtr (PcdUefiVariableDefaultPlatformLangCodes
)
291 ASSERT (gLanguageString
!= NULL
);
294 if (gLanguageToken
== NULL
) {
296 // Count the language list number.
298 LangCode
= gLanguageString
;
299 Lang
= AllocatePool (AsciiStrSize (gLanguageString
));
300 ASSERT (Lang
!= NULL
);
303 while (*LangCode
!= 0) {
304 GetNextLanguage (&LangCode
, Lang
);
309 // Allocate extra 1 as the end tag.
311 gLanguageToken
= AllocateZeroPool ((OptionCount
+ 1) * sizeof (EFI_STRING_ID
));
312 ASSERT (gLanguageToken
!= NULL
);
314 Status
= gBS
->LocateProtocol (&gEfiHiiStringProtocolGuid
, NULL
, (VOID
**) &HiiString
);
315 ASSERT_EFI_ERROR (Status
);
317 LangCode
= gLanguageString
;
319 while (*LangCode
!= 0) {
320 GetNextLanguage (&LangCode
, Lang
);
323 Status
= HiiString
->GetString (HiiString
, Lang
, HiiHandle
, PRINTABLE_LANGUAGE_NAME_STRING_ID
, StringBuffer
, &StringSize
, NULL
);
324 if (Status
== EFI_BUFFER_TOO_SMALL
) {
325 StringBuffer
= AllocateZeroPool (StringSize
);
326 ASSERT (StringBuffer
!= NULL
);
327 Status
= HiiString
->GetString (HiiString
, Lang
, HiiHandle
, PRINTABLE_LANGUAGE_NAME_STRING_ID
, StringBuffer
, &StringSize
, NULL
);
328 ASSERT_EFI_ERROR (Status
);
331 if (EFI_ERROR (Status
)) {
332 LangSize
= AsciiStrSize (Lang
);
333 StringBuffer
= AllocatePool (LangSize
* sizeof (CHAR16
));
334 ASSERT (StringBuffer
!= NULL
);
335 AsciiStrToUnicodeStrS (Lang
, StringBuffer
, LangSize
);
338 ASSERT (StringBuffer
!= NULL
);
339 gLanguageToken
[OptionCount
] = HiiSetString (HiiHandle
, 0, StringBuffer
, NULL
);
340 FreePool (StringBuffer
);
346 ASSERT (gLanguageToken
!= NULL
);
347 LangCode
= gLanguageString
;
350 Lang
= AllocatePool (AsciiStrSize (gLanguageString
));
351 ASSERT (Lang
!= NULL
);
353 while (*LangCode
!= 0) {
354 GetNextLanguage (&LangCode
, Lang
);
356 if (CurrentLang
!= NULL
&& AsciiStrCmp (Lang
, CurrentLang
) == 0) {
357 HiiCreateOneOfOptionOpCode (
359 gLanguageToken
[OptionCount
],
360 EFI_IFR_OPTION_DEFAULT
,
361 EFI_IFR_NUMERIC_SIZE_1
,
364 gCurrentLanguageIndex
= (UINT8
) OptionCount
;
366 HiiCreateOneOfOptionOpCode (
368 gLanguageToken
[OptionCount
],
370 EFI_IFR_NUMERIC_SIZE_1
,
378 if (CurrentLang
!= NULL
) {
379 FreePool (CurrentLang
);
383 HiiCreateOneOfOpCode (
385 FRONT_PAGE_KEY_LANGUAGE
,
388 STRING_TOKEN (STR_LANGUAGE_SELECT
),
389 STRING_TOKEN (STR_LANGUAGE_SELECT_HELP
),
390 EFI_IFR_FLAG_CALLBACK
,
391 EFI_IFR_NUMERIC_SIZE_1
,
398 Create continue menu in the front page.
400 @param[in] HiiHandle The hii handle for the Uiapp driver.
401 @param[in] StartOpCodeHandle The opcode handle to save the new opcode.
405 UiCreateContinueMenu (
406 IN EFI_HII_HANDLE HiiHandle
,
407 IN VOID
*StartOpCodeHandle
410 HiiCreateActionOpCode (
412 FRONT_PAGE_KEY_CONTINUE
,
413 STRING_TOKEN (STR_CONTINUE_PROMPT
),
414 STRING_TOKEN (STR_CONTINUE_PROMPT
),
415 EFI_IFR_FLAG_CALLBACK
,
421 Create empty line menu in the front page.
423 @param HiiHandle The hii handle for the Uiapp driver.
424 @param StartOpCodeHandle The opcode handle to save the new opcode.
429 IN EFI_HII_HANDLE HiiHandle
,
430 IN VOID
*StartOpCodeHandle
433 HiiCreateSubTitleOpCode (StartOpCodeHandle
, STRING_TOKEN (STR_NULL_STRING
), 0, 0, 0);
437 Create Reset menu in the front page.
439 @param[in] HiiHandle The hii handle for the Uiapp driver.
440 @param[in] StartOpCodeHandle The opcode handle to save the new opcode.
445 IN EFI_HII_HANDLE HiiHandle
,
446 IN VOID
*StartOpCodeHandle
449 HiiCreateActionOpCode (
451 FRONT_PAGE_KEY_RESET
,
452 STRING_TOKEN (STR_RESET_STRING
),
453 STRING_TOKEN (STR_RESET_STRING
),
454 EFI_IFR_FLAG_CALLBACK
,
460 Extract device path for given HII handle and class guid.
462 @param Handle The HII handle.
464 @retval NULL Fail to get the device path string.
465 @return PathString Get the device path string.
469 ExtractDevicePathFromHiiHandle (
470 IN EFI_HII_HANDLE Handle
474 EFI_HANDLE DriverHandle
;
476 ASSERT (Handle
!= NULL
);
478 if (Handle
== NULL
) {
482 Status
= gHiiDatabase
->GetPackageListHandle (gHiiDatabase
, Handle
, &DriverHandle
);
483 if (EFI_ERROR (Status
)) {
487 return ConvertDevicePathToText(DevicePathFromHandle (DriverHandle
), FALSE
, FALSE
);
491 Check whether this driver need to be shown in the front page.
493 @param HiiHandle The hii handle for the driver.
494 @param Guid The special guid for the driver which is the target.
495 @param PromptId Return the prompt string id.
496 @param HelpId Return the help string id.
497 @param FormsetGuid Return the formset guid info.
499 @retval EFI_SUCCESS Search the driver success
504 IN EFI_HII_HANDLE HiiHandle
,
506 OUT EFI_STRING_ID
*PromptId
,
507 OUT EFI_STRING_ID
*HelpId
,
508 OUT VOID
*FormsetGuid
514 EFI_IFR_FORM_SET
*Buffer
;
520 Status
= HiiGetFormSetFromHiiHandle(HiiHandle
, &Buffer
,&BufferSize
);
521 if (EFI_ERROR (Status
)) {
527 Ptr
= (UINT8
*) Buffer
;
528 while(TempSize
< BufferSize
) {
529 TempSize
+= ((EFI_IFR_OP_HEADER
*) Ptr
)->Length
;
531 if (((EFI_IFR_OP_HEADER
*) Ptr
)->Length
<= OFFSET_OF (EFI_IFR_FORM_SET
, Flags
)){
532 Ptr
+= ((EFI_IFR_OP_HEADER
*) Ptr
)->Length
;
536 ClassGuidNum
= (UINT8
) (((EFI_IFR_FORM_SET
*)Ptr
)->Flags
& 0x3);
537 ClassGuid
= (EFI_GUID
*) (VOID
*)(Ptr
+ sizeof (EFI_IFR_FORM_SET
));
538 while (ClassGuidNum
-- > 0) {
539 if (!CompareGuid (Guid
, ClassGuid
)){
544 *PromptId
= ((EFI_IFR_FORM_SET
*)Ptr
)->FormSetTitle
;
545 *HelpId
= ((EFI_IFR_FORM_SET
*)Ptr
)->Help
;
546 CopyMem (FormsetGuid
, &((EFI_IFR_FORM_SET
*) Ptr
)->Guid
, sizeof (EFI_GUID
));
557 Search the drivers in the system which need to show in the front page
558 and insert the menu to the front page.
560 @param HiiHandle The hii handle for the Uiapp driver.
561 @param ClassGuid The class guid for the driver which is the target.
562 @param SpecialHandlerFn The pointer to the specail handler function, if any.
563 @param StartOpCodeHandle The opcode handle to save the new opcode.
565 @retval EFI_SUCCESS Search the driver success
569 UiListThirdPartyDrivers (
570 IN EFI_HII_HANDLE HiiHandle
,
571 IN EFI_GUID
*ClassGuid
,
572 IN DRIVER_SPECIAL_HANDLER SpecialHandlerFn
,
573 IN VOID
*StartOpCodeHandle
579 EFI_STRING_ID TokenHelp
;
580 EFI_HII_HANDLE
*HiiHandles
;
581 CHAR16
*DevicePathStr
;
584 UI_HII_DRIVER_INSTANCE
*DriverListPtr
;
586 BOOLEAN EmptyLineAfter
;
588 if (gHiiDriverList
!= NULL
) {
589 FreePool (gHiiDriverList
);
592 HiiHandles
= HiiGetHiiHandles (NULL
);
593 ASSERT (HiiHandles
!= NULL
);
595 gHiiDriverList
= AllocateZeroPool (UI_HII_DRIVER_LIST_SIZE
* sizeof (UI_HII_DRIVER_INSTANCE
));
596 ASSERT (gHiiDriverList
!= NULL
);
597 DriverListPtr
= gHiiDriverList
;
598 CurrentSize
= UI_HII_DRIVER_LIST_SIZE
;
600 for (Index
= 0, Count
= 0; HiiHandles
[Index
] != NULL
; Index
++) {
601 if (!RequiredDriver (HiiHandles
[Index
], ClassGuid
, &Token
, &TokenHelp
, &gHiiDriverList
[Count
].FormSetGuid
)) {
605 String
= HiiGetString (HiiHandles
[Index
], Token
, NULL
);
606 if (String
== NULL
) {
607 String
= HiiGetString (gStringPackHandle
, STRING_TOKEN (STR_MISSING_STRING
), NULL
);
608 ASSERT (String
!= NULL
);
609 } else if (SpecialHandlerFn
!= NULL
) {
611 // Check whether need to rename the driver name.
613 EmptyLineAfter
= FALSE
;
614 if (SpecialHandlerFn (String
, &NewName
, &EmptyLineAfter
)) {
617 DriverListPtr
[Count
].EmptyLineAfter
= EmptyLineAfter
;
620 DriverListPtr
[Count
].PromptId
= HiiSetString (HiiHandle
, 0, String
, NULL
);
623 String
= HiiGetString (HiiHandles
[Index
], TokenHelp
, NULL
);
624 if (String
== NULL
) {
625 String
= HiiGetString (gStringPackHandle
, STRING_TOKEN (STR_MISSING_STRING
), NULL
);
626 ASSERT (String
!= NULL
);
628 DriverListPtr
[Count
].HelpId
= HiiSetString (HiiHandle
, 0, String
, NULL
);
631 DevicePathStr
= ExtractDevicePathFromHiiHandle(HiiHandles
[Index
]);
632 if (DevicePathStr
!= NULL
){
633 DriverListPtr
[Count
].DevicePathId
= HiiSetString (HiiHandle
, 0, DevicePathStr
, NULL
);
634 FreePool (DevicePathStr
);
636 DriverListPtr
[Count
].DevicePathId
= 0;
640 if (Count
>= CurrentSize
) {
641 DriverListPtr
= AllocateCopyPool ((Count
+ UI_HII_DRIVER_LIST_SIZE
) * sizeof (UI_HII_DRIVER_INSTANCE
), gHiiDriverList
);
642 ASSERT (DriverListPtr
!= NULL
);
643 FreePool (gHiiDriverList
);
644 gHiiDriverList
= DriverListPtr
;
645 CurrentSize
+= UI_HII_DRIVER_LIST_SIZE
;
649 FreePool (HiiHandles
);
652 while (gHiiDriverList
[Index
].PromptId
!= 0) {
653 HiiCreateGotoExOpCode (
656 gHiiDriverList
[Index
].PromptId
,
657 gHiiDriverList
[Index
].HelpId
,
661 &gHiiDriverList
[Index
].FormSetGuid
,
662 gHiiDriverList
[Index
].DevicePathId
665 if (gHiiDriverList
[Index
].EmptyLineAfter
) {
666 UiCreateEmptyLine (HiiHandle
, StartOpCodeHandle
);