]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Application/UiApp/FrontPageCustomizedUiSupport.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9 #include <Uefi.h>
10
11 #include <Guid/MdeModuleHii.h>
12 #include <Guid/GlobalVariable.h>
13
14 #include <Protocol/HiiConfigAccess.h>
15 #include <Protocol/HiiString.h>
16
17 #include <Library/HiiLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/UefiLib.h>
20 #include <Library/BaseMemoryLib.h>
21 #include <Library/PcdLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/UefiRuntimeServicesTableLib.h>
24 #include <Library/UefiHiiServicesLib.h>
25 #include <Library/DevicePathLib.h>
26 #include <Library/UefiBootServicesTableLib.h>
27 #include "FrontPageCustomizedUiSupport.h"
28
29 //
30 // This is the VFR compiler generated header file which defines the
31 // string identifiers.
32 //
33 #define PRINTABLE_LANGUAGE_NAME_STRING_ID 0x0001
34
35 #define UI_HII_DRIVER_LIST_SIZE 0x8
36
37 #define FRONT_PAGE_KEY_CONTINUE 0x1000
38 #define FRONT_PAGE_KEY_RESET 0x1001
39 #define FRONT_PAGE_KEY_LANGUAGE 0x1002
40 #define FRONT_PAGE_KEY_DRIVER 0x2000
41
42 typedef struct {
43 EFI_STRING_ID PromptId;
44 EFI_STRING_ID HelpId;
45 EFI_STRING_ID DevicePathId;
46 EFI_GUID FormSetGuid;
47 BOOLEAN EmptyLineAfter;
48 } UI_HII_DRIVER_INSTANCE;
49
50 CHAR8 *gLanguageString;
51 EFI_STRING_ID *gLanguageToken;
52 UI_HII_DRIVER_INSTANCE *gHiiDriverList;
53 extern EFI_HII_HANDLE gStringPackHandle;
54 UINT8 gCurrentLanguageIndex;
55
56 /**
57 Get next language from language code list (with separator ';').
58
59 If LangCode is NULL, then ASSERT.
60 If Lang is NULL, then ASSERT.
61
62 @param LangCode On input: point to first language in the list. On
63 output: point to next language in the list, or
64 NULL if no more language in the list.
65 @param Lang The first language in the list.
66
67 **/
68 VOID
69 GetNextLanguage (
70 IN OUT CHAR8 **LangCode,
71 OUT CHAR8 *Lang
72 )
73 {
74 UINTN Index;
75 CHAR8 *StringPtr;
76
77 ASSERT (LangCode != NULL);
78 ASSERT (*LangCode != NULL);
79 ASSERT (Lang != NULL);
80
81 Index = 0;
82 StringPtr = *LangCode;
83 while (StringPtr[Index] != 0 && StringPtr[Index] != ';') {
84 Index++;
85 }
86
87 CopyMem (Lang, StringPtr, Index);
88 Lang[Index] = 0;
89
90 if (StringPtr[Index] == ';') {
91 Index++;
92 }
93
94 *LangCode = StringPtr + Index;
95 }
96
97 /**
98 This function processes the language changes in configuration.
99
100 @param Value A pointer to the data being sent to the original exporting driver.
101
102
103 @retval TRUE The callback successfully handled the action.
104 @retval FALSE The callback not supported in this handler.
105
106 **/
107 EFI_STATUS
108 LanguageChangeHandler (
109 IN EFI_IFR_TYPE_VALUE *Value
110 )
111 {
112 CHAR8 *LangCode;
113 CHAR8 *Lang;
114 UINTN Index;
115 EFI_STATUS Status;
116
117 //
118 // Allocate working buffer for RFC 4646 language in supported LanguageString.
119 //
120 Lang = AllocatePool (AsciiStrSize (gLanguageString));
121 ASSERT (Lang != NULL);
122
123 Index = 0;
124 LangCode = gLanguageString;
125 while (*LangCode != 0) {
126 GetNextLanguage (&LangCode, Lang);
127
128 if (Index == Value->u8) {
129 gCurrentLanguageIndex = Value->u8;
130 break;
131 }
132
133 Index++;
134 }
135
136 if (Index == Value->u8) {
137 Status = gRT->SetVariable (
138 L"PlatformLang",
139 &gEfiGlobalVariableGuid,
140 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
141 AsciiStrSize (Lang),
142 Lang
143 );
144 if (EFI_ERROR (Status)) {
145 FreePool (Lang);
146 return EFI_DEVICE_ERROR;
147 }
148 } else {
149 ASSERT (FALSE);
150 }
151
152 FreePool (Lang);
153
154 return EFI_SUCCESS;
155 }
156
157 /**
158 This function processes the results of changes in configuration.
159
160
161 @param HiiHandle Points to the hii handle for this formset.
162 @param Action Specifies the type of action taken by the browser.
163 @param QuestionId A unique value which is sent to the original exporting driver
164 so that it can identify the type of data to expect.
165 @param Type The type of value for the question.
166 @param Value A pointer to the data being sent to the original exporting driver.
167 @param ActionRequest On return, points to the action requested by the callback function.
168 @param Status Return the handle status.
169
170 @retval TRUE The callback successfully handled the action.
171 @retval FALSE The callback not supported in this handler.
172
173 **/
174 BOOLEAN
175 UiSupportLibCallbackHandler (
176 IN EFI_HII_HANDLE HiiHandle,
177 IN EFI_BROWSER_ACTION Action,
178 IN EFI_QUESTION_ID QuestionId,
179 IN UINT8 Type,
180 IN EFI_IFR_TYPE_VALUE *Value,
181 OUT EFI_BROWSER_ACTION_REQUEST *ActionRequest,
182 OUT EFI_STATUS *Status
183 )
184 {
185 if ((QuestionId != FRONT_PAGE_KEY_CONTINUE) &&
186 (QuestionId != FRONT_PAGE_KEY_RESET) &&
187 (QuestionId != FRONT_PAGE_KEY_LANGUAGE))
188 {
189 return FALSE;
190 }
191
192 if (Action == EFI_BROWSER_ACTION_RETRIEVE) {
193 if (QuestionId == FRONT_PAGE_KEY_LANGUAGE) {
194 Value->u8 = gCurrentLanguageIndex;
195 *Status = EFI_SUCCESS;
196 } else {
197 *Status = EFI_UNSUPPORTED;
198 }
199
200 return TRUE;
201 }
202
203 if (Action != EFI_BROWSER_ACTION_CHANGED) {
204 //
205 // Do nothing for other UEFI Action. Only do call back when data is changed.
206 //
207 *Status = EFI_UNSUPPORTED;
208 return TRUE;
209 }
210
211 if (Action == EFI_BROWSER_ACTION_CHANGED) {
212 if ((Value == NULL) || (ActionRequest == NULL)) {
213 *Status = EFI_INVALID_PARAMETER;
214 return TRUE;
215 }
216
217 *Status = EFI_SUCCESS;
218 switch (QuestionId) {
219 case FRONT_PAGE_KEY_CONTINUE:
220 //
221 // This is the continue - clear the screen and return an error to get out of FrontPage loop
222 //
223 *ActionRequest = EFI_BROWSER_ACTION_REQUEST_EXIT;
224 break;
225
226 case FRONT_PAGE_KEY_LANGUAGE:
227 *Status = LanguageChangeHandler (Value);
228 break;
229
230 case FRONT_PAGE_KEY_RESET:
231 //
232 // Reset
233 //
234 gRT->ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);
235 *Status = EFI_UNSUPPORTED;
236
237 default:
238 break;
239 }
240 }
241
242 return TRUE;
243 }
244
245 /**
246 Create Select language menu in the front page with oneof opcode.
247
248 @param[in] HiiHandle The hii handle for the Uiapp driver.
249 @param[in] StartOpCodeHandle The opcode handle to save the new opcode.
250
251 **/
252 VOID
253 UiCreateLanguageMenu (
254 IN EFI_HII_HANDLE HiiHandle,
255 IN VOID *StartOpCodeHandle
256 )
257 {
258 CHAR8 *LangCode;
259 CHAR8 *Lang;
260 UINTN LangSize;
261 CHAR8 *CurrentLang;
262 UINTN OptionCount;
263 CHAR16 *StringBuffer;
264 VOID *OptionsOpCodeHandle;
265 UINTN StringSize;
266 EFI_STATUS Status;
267 EFI_HII_STRING_PROTOCOL *HiiString;
268
269 Lang = NULL;
270 StringBuffer = NULL;
271
272 //
273 // Init OpCode Handle and Allocate space for creation of UpdateData Buffer
274 //
275 OptionsOpCodeHandle = HiiAllocateOpCodeHandle ();
276 ASSERT (OptionsOpCodeHandle != NULL);
277
278 GetEfiGlobalVariable2 (L"PlatformLang", (VOID **)&CurrentLang, NULL);
279
280 //
281 // Get Support language list from variable.
282 //
283 GetEfiGlobalVariable2 (L"PlatformLangCodes", (VOID **)&gLanguageString, NULL);
284 if (gLanguageString == NULL) {
285 gLanguageString = AllocateCopyPool (
286 AsciiStrSize ((CHAR8 *)PcdGetPtr (PcdUefiVariableDefaultPlatformLangCodes)),
287 (CHAR8 *)PcdGetPtr (PcdUefiVariableDefaultPlatformLangCodes)
288 );
289 ASSERT (gLanguageString != NULL);
290 }
291
292 if (gLanguageToken == NULL) {
293 //
294 // Count the language list number.
295 //
296 LangCode = gLanguageString;
297 Lang = AllocatePool (AsciiStrSize (gLanguageString));
298 ASSERT (Lang != NULL);
299
300 OptionCount = 0;
301 while (*LangCode != 0) {
302 GetNextLanguage (&LangCode, Lang);
303 OptionCount++;
304 }
305
306 //
307 // Allocate extra 1 as the end tag.
308 //
309 gLanguageToken = AllocateZeroPool ((OptionCount + 1) * sizeof (EFI_STRING_ID));
310 ASSERT (gLanguageToken != NULL);
311
312 Status = gBS->LocateProtocol (&gEfiHiiStringProtocolGuid, NULL, (VOID **)&HiiString);
313 ASSERT_EFI_ERROR (Status);
314
315 LangCode = gLanguageString;
316 OptionCount = 0;
317 while (*LangCode != 0) {
318 GetNextLanguage (&LangCode, Lang);
319
320 StringSize = 0;
321 Status = HiiString->GetString (HiiString, Lang, HiiHandle, PRINTABLE_LANGUAGE_NAME_STRING_ID, StringBuffer, &StringSize, NULL);
322 if (Status == EFI_BUFFER_TOO_SMALL) {
323 StringBuffer = AllocateZeroPool (StringSize);
324 ASSERT (StringBuffer != NULL);
325 Status = HiiString->GetString (HiiString, Lang, HiiHandle, PRINTABLE_LANGUAGE_NAME_STRING_ID, StringBuffer, &StringSize, NULL);
326 ASSERT_EFI_ERROR (Status);
327 }
328
329 if (EFI_ERROR (Status)) {
330 LangSize = AsciiStrSize (Lang);
331 StringBuffer = AllocatePool (LangSize * sizeof (CHAR16));
332 ASSERT (StringBuffer != NULL);
333 AsciiStrToUnicodeStrS (Lang, StringBuffer, LangSize);
334 }
335
336 ASSERT (StringBuffer != NULL);
337 gLanguageToken[OptionCount] = HiiSetString (HiiHandle, 0, StringBuffer, NULL);
338 FreePool (StringBuffer);
339
340 OptionCount++;
341 }
342 }
343
344 ASSERT (gLanguageToken != NULL);
345 LangCode = gLanguageString;
346 OptionCount = 0;
347 if (Lang == NULL) {
348 Lang = AllocatePool (AsciiStrSize (gLanguageString));
349 ASSERT (Lang != NULL);
350 }
351
352 while (*LangCode != 0) {
353 GetNextLanguage (&LangCode, Lang);
354
355 if ((CurrentLang != NULL) && (AsciiStrCmp (Lang, CurrentLang) == 0)) {
356 HiiCreateOneOfOptionOpCode (
357 OptionsOpCodeHandle,
358 gLanguageToken[OptionCount],
359 EFI_IFR_OPTION_DEFAULT,
360 EFI_IFR_NUMERIC_SIZE_1,
361 (UINT8)OptionCount
362 );
363 gCurrentLanguageIndex = (UINT8)OptionCount;
364 } else {
365 HiiCreateOneOfOptionOpCode (
366 OptionsOpCodeHandle,
367 gLanguageToken[OptionCount],
368 0,
369 EFI_IFR_NUMERIC_SIZE_1,
370 (UINT8)OptionCount
371 );
372 }
373
374 OptionCount++;
375 }
376
377 if (CurrentLang != NULL) {
378 FreePool (CurrentLang);
379 }
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 special 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
621 DriverListPtr[Count].PromptId = HiiSetString (HiiHandle, 0, String, NULL);
622 FreePool (String);
623
624 String = HiiGetString (HiiHandles[Index], TokenHelp, NULL);
625 if (String == NULL) {
626 String = HiiGetString (gStringPackHandle, STRING_TOKEN (STR_MISSING_STRING), NULL);
627 ASSERT (String != NULL);
628 }
629
630 DriverListPtr[Count].HelpId = HiiSetString (HiiHandle, 0, String, NULL);
631 FreePool (String);
632
633 DevicePathStr = ExtractDevicePathFromHiiHandle (HiiHandles[Index]);
634 if (DevicePathStr != NULL) {
635 DriverListPtr[Count].DevicePathId = HiiSetString (HiiHandle, 0, DevicePathStr, NULL);
636 FreePool (DevicePathStr);
637 } else {
638 DriverListPtr[Count].DevicePathId = 0;
639 }
640
641 Count++;
642 if (Count >= CurrentSize) {
643 DriverListPtr = ReallocatePool (
644 CurrentSize * sizeof (UI_HII_DRIVER_INSTANCE),
645 (Count + UI_HII_DRIVER_LIST_SIZE)
646 * sizeof (UI_HII_DRIVER_INSTANCE),
647 gHiiDriverList
648 );
649 ASSERT (DriverListPtr != NULL);
650 gHiiDriverList = DriverListPtr;
651 CurrentSize += UI_HII_DRIVER_LIST_SIZE;
652 }
653 }
654
655 FreePool (HiiHandles);
656
657 Index = 0;
658 while (gHiiDriverList[Index].PromptId != 0) {
659 HiiCreateGotoExOpCode (
660 StartOpCodeHandle,
661 0,
662 gHiiDriverList[Index].PromptId,
663 gHiiDriverList[Index].HelpId,
664 0,
665 (EFI_QUESTION_ID)(Index + FRONT_PAGE_KEY_DRIVER),
666 0,
667 &gHiiDriverList[Index].FormSetGuid,
668 gHiiDriverList[Index].DevicePathId
669 );
670
671 if (gHiiDriverList[Index].EmptyLineAfter) {
672 UiCreateEmptyLine (HiiHandle, StartOpCodeHandle);
673 }
674
675 Index++;
676 }
677
678 return EFI_SUCCESS;
679 }