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 EFI_HII_HANDLE gHiiHandle
;
62 Get next language from language code list (with separator ';').
64 If LangCode is NULL, then ASSERT.
65 If Lang is NULL, then ASSERT.
67 @param LangCode On input: point to first language in the list. On
68 output: point to next language in the list, or
69 NULL if no more language in the list.
70 @param Lang The first language in the list.
75 IN OUT CHAR8
**LangCode
,
82 ASSERT (LangCode
!= NULL
);
83 ASSERT (*LangCode
!= NULL
);
84 ASSERT (Lang
!= NULL
);
87 StringPtr
= *LangCode
;
88 while (StringPtr
[Index
] != 0 && StringPtr
[Index
] != ';') {
92 CopyMem (Lang
, StringPtr
, Index
);
95 if (StringPtr
[Index
] == ';') {
98 *LangCode
= StringPtr
+ Index
;
102 This function processes the language changes in configuration.
104 @param Value A pointer to the data being sent to the original exporting driver.
107 @retval TRUE The callback successfully handled the action.
108 @retval FALSE The callback not supported in this handler.
112 LanguageChangeHandler (
113 IN EFI_IFR_TYPE_VALUE
*Value
122 // Allocate working buffer for RFC 4646 language in supported LanguageString.
124 Lang
= AllocatePool (AsciiStrSize (gLanguageString
));
125 ASSERT (Lang
!= NULL
);
128 LangCode
= gLanguageString
;
129 while (*LangCode
!= 0) {
130 GetNextLanguage (&LangCode
, Lang
);
132 if (Index
== Value
->u8
) {
139 if (Index
== Value
->u8
) {
140 Status
= gRT
->SetVariable (
142 &gEfiGlobalVariableGuid
,
143 EFI_VARIABLE_NON_VOLATILE
| EFI_VARIABLE_BOOTSERVICE_ACCESS
| EFI_VARIABLE_RUNTIME_ACCESS
,
147 if (EFI_ERROR (Status
)) {
149 return EFI_DEVICE_ERROR
;
160 This function processes the results of changes in configuration.
163 @param HiiHandle Points to the hii handle for this formset.
164 @param Action Specifies the type of action taken by the browser.
165 @param QuestionId A unique value which is sent to the original exporting driver
166 so that it can identify the type of data to expect.
167 @param Type The type of value for the question.
168 @param Value A pointer to the data being sent to the original exporting driver.
169 @param ActionRequest On return, points to the action requested by the callback function.
170 @param Status Return the handle status.
172 @retval TRUE The callback successfully handled the action.
173 @retval FALSE The callback not supported in this handler.
177 UiSupportLibCallbackHandler (
178 IN EFI_HII_HANDLE HiiHandle
,
179 IN EFI_BROWSER_ACTION Action
,
180 IN EFI_QUESTION_ID QuestionId
,
182 IN EFI_IFR_TYPE_VALUE
*Value
,
183 OUT EFI_BROWSER_ACTION_REQUEST
*ActionRequest
,
184 OUT EFI_STATUS
*Status
187 if (QuestionId
!= FRONT_PAGE_KEY_CONTINUE
&&
188 QuestionId
!= FRONT_PAGE_KEY_RESET
&&
189 QuestionId
!= FRONT_PAGE_KEY_LANGUAGE
) {
193 if (Action
!= EFI_BROWSER_ACTION_CHANGED
) {
195 // Do nothing for other UEFI Action. Only do call back when data is changed.
197 *Status
= EFI_UNSUPPORTED
;
200 if (Action
== EFI_BROWSER_ACTION_CHANGED
) {
201 if ((Value
== NULL
) || (ActionRequest
== NULL
)) {
202 *Status
= EFI_INVALID_PARAMETER
;
205 *Status
= EFI_SUCCESS
;
206 switch (QuestionId
) {
207 case FRONT_PAGE_KEY_CONTINUE
:
209 // This is the continue - clear the screen and return an error to get out of FrontPage loop
211 *ActionRequest
= EFI_BROWSER_ACTION_REQUEST_EXIT
;
214 case FRONT_PAGE_KEY_LANGUAGE
:
215 *Status
= LanguageChangeHandler(Value
);
218 case FRONT_PAGE_KEY_RESET
:
222 gRT
->ResetSystem (EfiResetCold
, EFI_SUCCESS
, 0, NULL
);
223 *Status
= EFI_UNSUPPORTED
;
234 Create Select language menu in the front page with oneof opcode.
236 @param[in] HiiHandle The hii handle for the Uiapp driver.
237 @param[in] QuestionId Question ID
238 @param[in] StartOpCodeHandle The opcode handle to save the new opcode.
240 @retval EFI_SUCCESS Search the driver success
244 UiCreateLanguageMenu (
245 IN EFI_HII_HANDLE HiiHandle
,
246 IN VOID
*StartOpCodeHandle
253 CHAR16
*StringBuffer
;
254 VOID
*OptionsOpCodeHandle
;
257 EFI_HII_STRING_PROTOCOL
*HiiString
;
263 // Init OpCode Handle and Allocate space for creation of UpdateData Buffer
265 OptionsOpCodeHandle
= HiiAllocateOpCodeHandle ();
266 ASSERT (OptionsOpCodeHandle
!= NULL
);
268 GetEfiGlobalVariable2 (L
"PlatformLang", (VOID
**)&CurrentLang
, NULL
);
271 // Get Support language list from variable.
273 GetEfiGlobalVariable2 (L
"PlatformLangCodes", (VOID
**)&gLanguageString
, NULL
);
274 if (gLanguageString
== NULL
) {
275 gLanguageString
= AllocateCopyPool (
276 AsciiStrSize ((CHAR8
*) PcdGetPtr (PcdUefiVariableDefaultPlatformLangCodes
)),
277 (CHAR8
*) PcdGetPtr (PcdUefiVariableDefaultPlatformLangCodes
)
279 ASSERT (gLanguageString
!= NULL
);
282 if (gLanguageToken
== NULL
) {
284 // Count the language list number.
286 LangCode
= gLanguageString
;
287 Lang
= AllocatePool (AsciiStrSize (gLanguageString
));
288 ASSERT (Lang
!= NULL
);
291 while (*LangCode
!= 0) {
292 GetNextLanguage (&LangCode
, Lang
);
297 // Allocate extra 1 as the end tag.
299 gLanguageToken
= AllocateZeroPool ((OptionCount
+ 1) * sizeof (EFI_STRING_ID
));
300 ASSERT (gLanguageToken
!= NULL
);
302 Status
= gBS
->LocateProtocol (&gEfiHiiStringProtocolGuid
, NULL
, (VOID
**) &HiiString
);
303 ASSERT_EFI_ERROR (Status
);
305 LangCode
= gLanguageString
;
307 while (*LangCode
!= 0) {
308 GetNextLanguage (&LangCode
, Lang
);
311 Status
= HiiString
->GetString (HiiString
, Lang
, HiiHandle
, PRINTABLE_LANGUAGE_NAME_STRING_ID
, StringBuffer
, &StringSize
, NULL
);
312 if (Status
== EFI_BUFFER_TOO_SMALL
) {
313 StringBuffer
= AllocateZeroPool (StringSize
);
314 ASSERT (StringBuffer
!= NULL
);
315 Status
= HiiString
->GetString (HiiString
, Lang
, HiiHandle
, PRINTABLE_LANGUAGE_NAME_STRING_ID
, StringBuffer
, &StringSize
, NULL
);
316 ASSERT_EFI_ERROR (Status
);
319 if (EFI_ERROR (Status
)) {
320 StringBuffer
= AllocatePool (AsciiStrSize (Lang
) * sizeof (CHAR16
));
321 ASSERT (StringBuffer
!= NULL
);
322 AsciiStrToUnicodeStr (Lang
, StringBuffer
);
325 ASSERT (StringBuffer
!= NULL
);
326 gLanguageToken
[OptionCount
] = HiiSetString (HiiHandle
, 0, StringBuffer
, NULL
);
327 FreePool (StringBuffer
);
333 ASSERT (gLanguageToken
!= NULL
);
334 LangCode
= gLanguageString
;
337 Lang
= AllocatePool (AsciiStrSize (gLanguageString
));
338 ASSERT (Lang
!= NULL
);
340 while (*LangCode
!= 0) {
341 GetNextLanguage (&LangCode
, Lang
);
343 if (CurrentLang
!= NULL
&& AsciiStrCmp (Lang
, CurrentLang
) == 0) {
344 HiiCreateOneOfOptionOpCode (
346 gLanguageToken
[OptionCount
],
347 EFI_IFR_OPTION_DEFAULT
,
348 EFI_IFR_NUMERIC_SIZE_1
,
352 HiiCreateOneOfOptionOpCode (
354 gLanguageToken
[OptionCount
],
356 EFI_IFR_NUMERIC_SIZE_1
,
364 if (CurrentLang
!= NULL
) {
365 FreePool (CurrentLang
);
369 HiiCreateOneOfOpCode (
371 FRONT_PAGE_KEY_LANGUAGE
,
374 STRING_TOKEN (STR_LANGUAGE_SELECT
),
375 STRING_TOKEN (STR_LANGUAGE_SELECT_HELP
),
376 EFI_IFR_FLAG_CALLBACK
,
377 EFI_IFR_NUMERIC_SIZE_1
,
384 Create continue menu in the front page.
386 @param[in] HiiHandle The hii handle for the Uiapp driver.
387 @param[in] StartOpCodeHandle The opcode handle to save the new opcode.
391 UiCreateContinueMenu (
392 IN EFI_HII_HANDLE HiiHandle
,
393 IN VOID
*StartOpCodeHandle
396 HiiCreateActionOpCode (
398 FRONT_PAGE_KEY_CONTINUE
,
399 STRING_TOKEN (STR_CONTINUE_PROMPT
),
400 STRING_TOKEN (STR_CONTINUE_PROMPT
),
401 EFI_IFR_FLAG_CALLBACK
,
407 Create empty line menu in the front page.
409 @param HiiHandle The hii handle for the Uiapp driver.
410 @param StartOpCodeHandle The opcode handle to save the new opcode.
415 IN EFI_HII_HANDLE HiiHandle
,
416 IN VOID
*StartOpCodeHandle
419 HiiCreateSubTitleOpCode (StartOpCodeHandle
, STRING_TOKEN (STR_NULL_STRING
), 0, 0, 0);
423 Create Reset menu in the front page.
425 @param[in] HiiHandle The hii handle for the Uiapp driver.
426 @param[in] StartOpCodeHandle The opcode handle to save the new opcode.
431 IN EFI_HII_HANDLE HiiHandle
,
432 IN VOID
*StartOpCodeHandle
435 HiiCreateActionOpCode (
437 FRONT_PAGE_KEY_RESET
,
438 STRING_TOKEN (STR_RESET_STRING
),
439 STRING_TOKEN (STR_RESET_STRING
),
440 EFI_IFR_FLAG_CALLBACK
,
446 Extract device path for given HII handle and class guid.
448 @param Handle The HII handle.
450 @retval NULL Fail to get the device path string.
451 @return PathString Get the device path string.
455 ExtractDevicePathFromHiiHandle (
456 IN EFI_HII_HANDLE Handle
460 EFI_HANDLE DriverHandle
;
462 ASSERT (Handle
!= NULL
);
464 if (Handle
== NULL
) {
468 Status
= gHiiDatabase
->GetPackageListHandle (gHiiDatabase
, Handle
, &DriverHandle
);
469 if (EFI_ERROR (Status
)) {
473 return ConvertDevicePathToText(DevicePathFromHandle (DriverHandle
), FALSE
, FALSE
);
477 Check whether this driver need to be shown in the front page.
479 @param HiiHandle The hii handle for the driver.
480 @param Guid The special guid for the driver which is the target.
481 @param PromptId Return the prompt string id.
482 @param HelpId Return the help string id.
483 @param FormsetGuid Return the formset guid info.
485 @retval EFI_SUCCESS Search the driver success
490 IN EFI_HII_HANDLE HiiHandle
,
492 OUT EFI_STRING_ID
*PromptId
,
493 OUT EFI_STRING_ID
*HelpId
,
494 OUT VOID
*FormsetGuid
500 EFI_IFR_FORM_SET
*Buffer
;
506 Status
= HiiGetFormSetFromHiiHandle(HiiHandle
, &Buffer
,&BufferSize
);
507 if (EFI_ERROR (Status
)) {
513 Ptr
= (UINT8
*) Buffer
;
514 while(TempSize
< BufferSize
) {
515 TempSize
+= ((EFI_IFR_OP_HEADER
*) Ptr
)->Length
;
517 if (((EFI_IFR_OP_HEADER
*) Ptr
)->Length
<= OFFSET_OF (EFI_IFR_FORM_SET
, Flags
)){
518 Ptr
+= ((EFI_IFR_OP_HEADER
*) Ptr
)->Length
;
522 ClassGuidNum
= (UINT8
) (((EFI_IFR_FORM_SET
*)Ptr
)->Flags
& 0x3);
523 ClassGuid
= (EFI_GUID
*) (VOID
*)(Ptr
+ sizeof (EFI_IFR_FORM_SET
));
524 while (ClassGuidNum
-- > 0) {
525 if (!CompareGuid (Guid
, ClassGuid
)){
530 *PromptId
= ((EFI_IFR_FORM_SET
*)Ptr
)->FormSetTitle
;
531 *HelpId
= ((EFI_IFR_FORM_SET
*)Ptr
)->Help
;
532 CopyMem (FormsetGuid
, &((EFI_IFR_FORM_SET
*) Ptr
)->Guid
, sizeof (EFI_GUID
));
543 Search the drivers in the system which need to show in the front page
544 and insert the menu to the front page.
546 @param HiiHandle The hii handle for the Uiapp driver.
547 @param ClassGuid The class guid for the driver which is the target.
548 @param StartOpCodeHandle The opcode handle to save the new opcode.
549 @param SpecialHandler The pointer to the specail handler function, if any.
551 @retval EFI_SUCCESS Search the driver success
555 UiListThirdPartyDrivers (
556 IN EFI_HII_HANDLE HiiHandle
,
557 IN EFI_GUID
*ClassGuid
,
558 IN DRIVER_SPECIAL_HANDLER SpecialHandlerFn
,
559 IN VOID
*StartOpCodeHandle
565 EFI_STRING_ID TokenHelp
;
566 EFI_HII_HANDLE
*HiiHandles
;
567 CHAR16
*DevicePathStr
;
570 UI_HII_DRIVER_INSTANCE
*DriverListPtr
;
572 BOOLEAN EmptyLineAfter
;
574 if (gHiiDriverList
!= NULL
) {
575 FreePool (gHiiDriverList
);
578 HiiHandles
= HiiGetHiiHandles (NULL
);
579 ASSERT (HiiHandles
!= NULL
);
581 gHiiDriverList
= AllocateZeroPool (UI_HII_DRIVER_LIST_SIZE
* sizeof (UI_HII_DRIVER_INSTANCE
));
582 ASSERT (gHiiDriverList
!= NULL
);
583 DriverListPtr
= gHiiDriverList
;
584 CurrentSize
= UI_HII_DRIVER_LIST_SIZE
;
586 for (Index
= 0, Count
= 0; HiiHandles
[Index
] != NULL
; Index
++) {
587 if (!RequiredDriver (HiiHandles
[Index
], ClassGuid
, &Token
, &TokenHelp
, &gHiiDriverList
[Count
].FormSetGuid
)) {
591 String
= HiiGetString (HiiHandles
[Index
], Token
, NULL
);
592 if (String
== NULL
) {
593 String
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_MISSING_STRING
), NULL
);
594 ASSERT (String
!= NULL
);
595 } else if (SpecialHandlerFn
!= NULL
) {
597 // Check whether need to rename the driver name.
599 EmptyLineAfter
= FALSE
;
600 if (SpecialHandlerFn (String
, &NewName
, &EmptyLineAfter
)) {
603 DriverListPtr
[Count
].EmptyLineAfter
= EmptyLineAfter
;
606 DriverListPtr
[Count
].PromptId
= HiiSetString (HiiHandle
, 0, String
, NULL
);
609 String
= HiiGetString (HiiHandles
[Index
], TokenHelp
, NULL
);
610 if (String
== NULL
) {
611 String
= HiiGetString (gHiiHandle
, STRING_TOKEN (STR_MISSING_STRING
), NULL
);
612 ASSERT (String
!= NULL
);
614 DriverListPtr
[Count
].HelpId
= HiiSetString (HiiHandle
, 0, String
, NULL
);
617 DevicePathStr
= ExtractDevicePathFromHiiHandle(HiiHandles
[Index
]);
618 if (DevicePathStr
!= NULL
){
619 DriverListPtr
[Count
].DevicePathId
= HiiSetString (HiiHandle
, 0, DevicePathStr
, NULL
);
620 FreePool (DevicePathStr
);
622 DriverListPtr
[Count
].DevicePathId
= 0;
626 if (Count
>= CurrentSize
) {
627 DriverListPtr
= AllocateCopyPool ((Count
+ UI_HII_DRIVER_LIST_SIZE
) * sizeof (UI_HII_DRIVER_INSTANCE
), gHiiDriverList
);
628 ASSERT (DriverListPtr
!= NULL
);
629 FreePool (gHiiDriverList
);
630 gHiiDriverList
= DriverListPtr
;
631 CurrentSize
+= UI_HII_DRIVER_LIST_SIZE
;
635 FreePool (HiiHandles
);
638 while (gHiiDriverList
[Index
].PromptId
!= 0) {
639 HiiCreateGotoExOpCode (
642 gHiiDriverList
[Index
].PromptId
,
643 gHiiDriverList
[Index
].HelpId
,
647 &gHiiDriverList
[Index
].FormSetGuid
,
648 gHiiDriverList
[Index
].DevicePathId
651 if (gHiiDriverList
[Index
].EmptyLineAfter
) {
652 UiCreateEmptyLine (HiiHandle
, StartOpCodeHandle
);