]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c
MdeModulePkg: Replace UnicodeStrToAsciiStr/AsciiStrToUnicodeStr
[mirror_edk2.git] / MdeModulePkg / Application / UiApp / FrontPageCustomizedUiSupport.c
1 /** @file
2
3 This library class defines a set of interfaces to customize Ui module
4
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.
10
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.
13
14 **/
15 #include <Uefi.h>
16
17 #include <Guid/MdeModuleHii.h>
18 #include <Guid/GlobalVariable.h>
19
20 #include <Protocol/HiiConfigAccess.h>
21 #include <Protocol/HiiString.h>
22
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"
34
35 //
36 // This is the VFR compiler generated header file which defines the
37 // string identifiers.
38 //
39 #define PRINTABLE_LANGUAGE_NAME_STRING_ID 0x0001
40
41 #define UI_HII_DRIVER_LIST_SIZE 0x8
42
43 #define FRONT_PAGE_KEY_CONTINUE 0x1000
44 #define FRONT_PAGE_KEY_RESET 0x1001
45 #define FRONT_PAGE_KEY_LANGUAGE 0x1002
46
47 typedef struct {
48 EFI_STRING_ID PromptId;
49 EFI_STRING_ID HelpId;
50 EFI_STRING_ID DevicePathId;
51 EFI_GUID FormSetGuid;
52 BOOLEAN EmptyLineAfter;
53 } UI_HII_DRIVER_INSTANCE;
54
55 CHAR8 *gLanguageString;
56 EFI_STRING_ID *gLanguageToken;
57 UI_HII_DRIVER_INSTANCE *gHiiDriverList;
58 extern EFI_HII_HANDLE gStringPackHandle;
59 UINT8 gCurrentLanguageIndex;
60
61
62 /**
63 Get next language from language code list (with separator ';').
64
65 If LangCode is NULL, then ASSERT.
66 If Lang is NULL, then ASSERT.
67
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.
72
73 **/
74 VOID
75 GetNextLanguage (
76 IN OUT CHAR8 **LangCode,
77 OUT CHAR8 *Lang
78 )
79 {
80 UINTN Index;
81 CHAR8 *StringPtr;
82
83 ASSERT (LangCode != NULL);
84 ASSERT (*LangCode != NULL);
85 ASSERT (Lang != NULL);
86
87 Index = 0;
88 StringPtr = *LangCode;
89 while (StringPtr[Index] != 0 && StringPtr[Index] != ';') {
90 Index++;
91 }
92
93 CopyMem (Lang, StringPtr, Index);
94 Lang[Index] = 0;
95
96 if (StringPtr[Index] == ';') {
97 Index++;
98 }
99 *LangCode = StringPtr + Index;
100 }
101
102 /**
103 This function processes the language changes in configuration.
104
105 @param Value A pointer to the data being sent to the original exporting driver.
106
107
108 @retval TRUE The callback successfully handled the action.
109 @retval FALSE The callback not supported in this handler.
110
111 **/
112 EFI_STATUS
113 LanguageChangeHandler (
114 IN EFI_IFR_TYPE_VALUE *Value
115 )
116 {
117 CHAR8 *LangCode;
118 CHAR8 *Lang;
119 UINTN Index;
120 EFI_STATUS Status;
121
122 //
123 // Allocate working buffer for RFC 4646 language in supported LanguageString.
124 //
125 Lang = AllocatePool (AsciiStrSize (gLanguageString));
126 ASSERT (Lang != NULL);
127
128 Index = 0;
129 LangCode = gLanguageString;
130 while (*LangCode != 0) {
131 GetNextLanguage (&LangCode, Lang);
132
133 if (Index == Value->u8) {
134 gCurrentLanguageIndex = Value->u8;
135 break;
136 }
137
138 Index++;
139 }
140
141 if (Index == Value->u8) {
142 Status = gRT->SetVariable (
143 L"PlatformLang",
144 &gEfiGlobalVariableGuid,
145 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
146 AsciiStrSize (Lang),
147 Lang
148 );
149 if (EFI_ERROR (Status)) {
150 FreePool (Lang);
151 return EFI_DEVICE_ERROR;
152 }
153 } else {
154 ASSERT (FALSE);
155 }
156 FreePool (Lang);
157
158 return EFI_SUCCESS;
159 }
160
161 /**
162 This function processes the results of changes in configuration.
163
164
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.
173
174 @retval TRUE The callback successfully handled the action.
175 @retval FALSE The callback not supported in this handler.
176
177 **/
178 BOOLEAN
179 UiSupportLibCallbackHandler (
180 IN EFI_HII_HANDLE HiiHandle,
181 IN EFI_BROWSER_ACTION Action,
182 IN EFI_QUESTION_ID QuestionId,
183 IN UINT8 Type,
184 IN EFI_IFR_TYPE_VALUE *Value,
185 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest,
186 OUT EFI_STATUS *Status
187 )
188 {
189 if (QuestionId != FRONT_PAGE_KEY_CONTINUE &&
190 QuestionId != FRONT_PAGE_KEY_RESET &&
191 QuestionId != FRONT_PAGE_KEY_LANGUAGE) {
192 return FALSE;
193 }
194
195 if (Action == EFI_BROWSER_ACTION_RETRIEVE) {
196 if (QuestionId == FRONT_PAGE_KEY_LANGUAGE) {
197 Value->u8 = gCurrentLanguageIndex;
198 *Status = EFI_SUCCESS;
199 } else {
200 *Status = EFI_UNSUPPORTED;
201 }
202 return TRUE;
203 }
204
205 if (Action != EFI_BROWSER_ACTION_CHANGED) {
206 //
207 // Do nothing for other UEFI Action. Only do call back when data is changed.
208 //
209 *Status = EFI_UNSUPPORTED;
210 return TRUE;
211 }
212
213 if (Action == EFI_BROWSER_ACTION_CHANGED) {
214 if ((Value == NULL) || (ActionRequest == NULL)) {
215 *Status = EFI_INVALID_PARAMETER;
216 return TRUE;
217 }
218
219 *Status = EFI_SUCCESS;
220 switch (QuestionId) {
221 case FRONT_PAGE_KEY_CONTINUE:
222 //
223 // This is the continue - clear the screen and return an error to get out of FrontPage loop
224 //
225 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;
226 break;
227
228 case FRONT_PAGE_KEY_LANGUAGE:
229 *Status = LanguageChangeHandler(Value);
230 break;
231
232 case FRONT_PAGE_KEY_RESET:
233 //
234 // Reset
235 //
236 gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);
237 *Status = EFI_UNSUPPORTED;
238
239 default:
240 break;
241 }
242 }
243
244 return TRUE;
245 }
246
247 /**
248 Create Select language menu in the front page with oneof opcode.
249
250 @param[in] HiiHandle The hii handle for the Uiapp driver.
251 @param[in] StartOpCodeHandle The opcode handle to save the new opcode.
252
253 **/
254 VOID
255 UiCreateLanguageMenu (
256 IN EFI_HII_HANDLE HiiHandle,
257 IN VOID *StartOpCodeHandle
258 )
259 {
260 CHAR8 *LangCode;
261 CHAR8 *Lang;
262 UINTN LangSize;
263 CHAR8 *CurrentLang;
264 UINTN OptionCount;
265 CHAR16 *StringBuffer;
266 VOID *OptionsOpCodeHandle;
267 UINTN StringSize;
268 EFI_STATUS Status;
269 EFI_HII_STRING_PROTOCOL *HiiString;
270
271 Lang = NULL;
272 StringBuffer = NULL;
273
274 //
275 // Init OpCode Handle and Allocate space for creation of UpdateData Buffer
276 //
277 OptionsOpCodeHandle = HiiAllocateOpCodeHandle ();
278 ASSERT (OptionsOpCodeHandle != NULL);
279
280 GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&CurrentLang, NULL);
281
282 //
283 // Get Support language list from variable.
284 //
285 GetEfiGlobalVariable2 (L"PlatformLangCodes", (VOID**)&gLanguageString, NULL);
286 if (gLanguageString == NULL) {
287 gLanguageString = AllocateCopyPool (
288 AsciiStrSize ((CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLangCodes)),
289 (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLangCodes)
290 );
291 ASSERT (gLanguageString != NULL);
292 }
293
294 if (gLanguageToken == NULL) {
295 //
296 // Count the language list number.
297 //
298 LangCode = gLanguageString;
299 Lang = AllocatePool (AsciiStrSize (gLanguageString));
300 ASSERT (Lang != NULL);
301
302 OptionCount = 0;
303 while (*LangCode != 0) {
304 GetNextLanguage (&LangCode, Lang);
305 OptionCount ++;
306 }
307
308 //
309 // Allocate extra 1 as the end tag.
310 //
311 gLanguageToken = AllocateZeroPool ((OptionCount + 1) * sizeof (EFI_STRING_ID));
312 ASSERT (gLanguageToken != NULL);
313
314 Status = gBS->LocateProtocol (&gEfiHiiStringProtocolGuid, NULL, (VOID **) &HiiString);
315 ASSERT_EFI_ERROR (Status);
316
317 LangCode = gLanguageString;
318 OptionCount = 0;
319 while (*LangCode != 0) {
320 GetNextLanguage (&LangCode, Lang);
321
322 StringSize = 0;
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);
329 }
330
331 if (EFI_ERROR (Status)) {
332 LangSize = AsciiStrSize (Lang);
333 StringBuffer = AllocatePool (LangSize * sizeof (CHAR16));
334 ASSERT (StringBuffer != NULL);
335 AsciiStrToUnicodeStrS (Lang, StringBuffer, LangSize);
336 }
337
338 ASSERT (StringBuffer != NULL);
339 gLanguageToken[OptionCount] = HiiSetString (HiiHandle, 0, StringBuffer, NULL);
340 FreePool (StringBuffer);
341
342 OptionCount++;
343 }
344 }
345
346 ASSERT (gLanguageToken != NULL);
347 LangCode = gLanguageString;
348 OptionCount = 0;
349 if (Lang == NULL) {
350 Lang = AllocatePool (AsciiStrSize (gLanguageString));
351 ASSERT (Lang != NULL);
352 }
353 while (*LangCode != 0) {
354 GetNextLanguage (&LangCode, Lang);
355
356 if (CurrentLang != NULL && AsciiStrCmp (Lang, CurrentLang) == 0) {
357 HiiCreateOneOfOptionOpCode (
358 OptionsOpCodeHandle,
359 gLanguageToken[OptionCount],
360 EFI_IFR_OPTION_DEFAULT,
361 EFI_IFR_NUMERIC_SIZE_1,
362 (UINT8) OptionCount
363 );
364 gCurrentLanguageIndex = (UINT8) OptionCount;
365 } else {
366 HiiCreateOneOfOptionOpCode (
367 OptionsOpCodeHandle,
368 gLanguageToken[OptionCount],
369 0,
370 EFI_IFR_NUMERIC_SIZE_1,
371 (UINT8) OptionCount
372 );
373 }
374
375 OptionCount++;
376 }
377
378 if (CurrentLang != NULL) {
379 FreePool (CurrentLang);
380 }
381 FreePool (Lang);
382
383 HiiCreateOneOfOpCode (
384 StartOpCodeHandle,
385 FRONT_PAGE_KEY_LANGUAGE,
386 0,
387 0,
388 STRING_TOKEN (STR_LANGUAGE_SELECT),
389 STRING_TOKEN (STR_LANGUAGE_SELECT_HELP),
390 EFI_IFR_FLAG_CALLBACK,
391 EFI_IFR_NUMERIC_SIZE_1,
392 OptionsOpCodeHandle,
393 NULL
394 );
395 }
396
397 /**
398 Create continue menu in the front page.
399
400 @param[in] HiiHandle The hii handle for the Uiapp driver.
401 @param[in] StartOpCodeHandle The opcode handle to save the new opcode.
402
403 **/
404 VOID
405 UiCreateContinueMenu (
406 IN EFI_HII_HANDLE HiiHandle,
407 IN VOID *StartOpCodeHandle
408 )
409 {
410 HiiCreateActionOpCode (
411 StartOpCodeHandle,
412 FRONT_PAGE_KEY_CONTINUE,
413 STRING_TOKEN (STR_CONTINUE_PROMPT),
414 STRING_TOKEN (STR_CONTINUE_PROMPT),
415 EFI_IFR_FLAG_CALLBACK,
416 0
417 );
418 }
419
420 /**
421 Create empty line menu in the front page.
422
423 @param HiiHandle The hii handle for the Uiapp driver.
424 @param StartOpCodeHandle The opcode handle to save the new opcode.
425
426 **/
427 VOID
428 UiCreateEmptyLine (
429 IN EFI_HII_HANDLE HiiHandle,
430 IN VOID *StartOpCodeHandle
431 )
432 {
433 HiiCreateSubTitleOpCode (StartOpCodeHandle, STRING_TOKEN (STR_NULL_STRING), 0, 0, 0);
434 }
435
436 /**
437 Create Reset menu in the front page.
438
439 @param[in] HiiHandle The hii handle for the Uiapp driver.
440 @param[in] StartOpCodeHandle The opcode handle to save the new opcode.
441
442 **/
443 VOID
444 UiCreateResetMenu (
445 IN EFI_HII_HANDLE HiiHandle,
446 IN VOID *StartOpCodeHandle
447 )
448 {
449 HiiCreateActionOpCode (
450 StartOpCodeHandle,
451 FRONT_PAGE_KEY_RESET,
452 STRING_TOKEN (STR_RESET_STRING),
453 STRING_TOKEN (STR_RESET_STRING),
454 EFI_IFR_FLAG_CALLBACK,
455 0
456 );
457 }
458
459 /**
460 Extract device path for given HII handle and class guid.
461
462 @param Handle The HII handle.
463
464 @retval NULL Fail to get the device path string.
465 @return PathString Get the device path string.
466
467 **/
468 CHAR16 *
469 ExtractDevicePathFromHiiHandle (
470 IN EFI_HII_HANDLE Handle
471 )
472 {
473 EFI_STATUS Status;
474 EFI_HANDLE DriverHandle;
475
476 ASSERT (Handle != NULL);
477
478 if (Handle == NULL) {
479 return NULL;
480 }
481
482 Status = gHiiDatabase->GetPackageListHandle (gHiiDatabase, Handle, &DriverHandle);
483 if (EFI_ERROR (Status)) {
484 return NULL;
485 }
486
487 return ConvertDevicePathToText(DevicePathFromHandle (DriverHandle), FALSE, FALSE);
488 }
489
490 /**
491 Check whether this driver need to be shown in the front page.
492
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.
498
499 @retval EFI_SUCCESS Search the driver success
500
501 **/
502 BOOLEAN
503 RequiredDriver (
504 IN EFI_HII_HANDLE HiiHandle,
505 IN EFI_GUID *Guid,
506 OUT EFI_STRING_ID *PromptId,
507 OUT EFI_STRING_ID *HelpId,
508 OUT VOID *FormsetGuid
509 )
510 {
511 EFI_STATUS Status;
512 UINT8 ClassGuidNum;
513 EFI_GUID *ClassGuid;
514 EFI_IFR_FORM_SET *Buffer;
515 UINTN BufferSize;
516 UINT8 *Ptr;
517 UINTN TempSize;
518 BOOLEAN RetVal;
519
520 Status = HiiGetFormSetFromHiiHandle(HiiHandle, &Buffer,&BufferSize);
521 if (EFI_ERROR (Status)) {
522 return FALSE;
523 }
524
525 RetVal = FALSE;
526 TempSize = 0;
527 Ptr = (UINT8 *) Buffer;
528 while(TempSize < BufferSize) {
529 TempSize += ((EFI_IFR_OP_HEADER *) Ptr)->Length;
530
531 if (((EFI_IFR_OP_HEADER *) Ptr)->Length <= OFFSET_OF (EFI_IFR_FORM_SET, Flags)){
532 Ptr += ((EFI_IFR_OP_HEADER *) Ptr)->Length;
533 continue;
534 }
535
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)){
540 ClassGuid ++;
541 continue;
542 }
543
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));
547 RetVal = TRUE;
548 }
549 }
550
551 FreePool (Buffer);
552
553 return RetVal;
554 }
555
556 /**
557 Search the drivers in the system which need to show in the front page
558 and insert the menu to the front page.
559
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.
564
565 @retval EFI_SUCCESS Search the driver success
566
567 **/
568 EFI_STATUS
569 UiListThirdPartyDrivers (
570 IN EFI_HII_HANDLE HiiHandle,
571 IN EFI_GUID *ClassGuid,
572 IN DRIVER_SPECIAL_HANDLER SpecialHandlerFn,
573 IN VOID *StartOpCodeHandle
574 )
575 {
576 UINTN Index;
577 EFI_STRING String;
578 EFI_STRING_ID Token;
579 EFI_STRING_ID TokenHelp;
580 EFI_HII_HANDLE *HiiHandles;
581 CHAR16 *DevicePathStr;
582 UINTN Count;
583 UINTN CurrentSize;
584 UI_HII_DRIVER_INSTANCE *DriverListPtr;
585 EFI_STRING NewName;
586 BOOLEAN EmptyLineAfter;
587
588 if (gHiiDriverList != NULL) {
589 FreePool (gHiiDriverList);
590 }
591
592 HiiHandles = HiiGetHiiHandles (NULL);
593 ASSERT (HiiHandles != NULL);
594
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;
599
600 for (Index = 0, Count = 0; HiiHandles[Index] != NULL; Index++) {
601 if (!RequiredDriver (HiiHandles[Index], ClassGuid, &Token, &TokenHelp, &gHiiDriverList[Count].FormSetGuid)) {
602 continue;
603 }
604
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) {
610 //
611 // Check whether need to rename the driver name.
612 //
613 EmptyLineAfter = FALSE;
614 if (SpecialHandlerFn (String, &NewName, &EmptyLineAfter)) {
615 FreePool (String);
616 String = NewName;
617 DriverListPtr[Count].EmptyLineAfter = EmptyLineAfter;
618 }
619 }
620 DriverListPtr[Count].PromptId = HiiSetString (HiiHandle, 0, String, NULL);
621 FreePool (String);
622
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);
627 }
628 DriverListPtr[Count].HelpId = HiiSetString (HiiHandle, 0, String, NULL);
629 FreePool (String);
630
631 DevicePathStr = ExtractDevicePathFromHiiHandle(HiiHandles[Index]);
632 if (DevicePathStr != NULL){
633 DriverListPtr[Count].DevicePathId = HiiSetString (HiiHandle, 0, DevicePathStr, NULL);
634 FreePool (DevicePathStr);
635 } else {
636 DriverListPtr[Count].DevicePathId = 0;
637 }
638
639 Count++;
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;
646 }
647 }
648
649 FreePool (HiiHandles);
650
651 Index = 0;
652 while (gHiiDriverList[Index].PromptId != 0) {
653 HiiCreateGotoExOpCode (
654 StartOpCodeHandle,
655 0,
656 gHiiDriverList[Index].PromptId,
657 gHiiDriverList[Index].HelpId,
658 0,
659 0,
660 0,
661 &gHiiDriverList[Index].FormSetGuid,
662 gHiiDriverList[Index].DevicePathId
663 );
664
665 if (gHiiDriverList[Index].EmptyLineAfter) {
666 UiCreateEmptyLine (HiiHandle, StartOpCodeHandle);
667 }
668
669 Index ++;
670 }
671
672 return EFI_SUCCESS;
673 }