2 Utility functions which helps in opcode creation, HII configuration string manipulations,
3 pop up window creations, setup browser persistence data set and get.
5 Copyright (c) 2007- 2008, Intel Corporation
6 All rights reserved. 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.
16 #include "UefiIfrLibraryInternal.h"
21 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT16 mFakeConfigHdr
[] = L
"GUID=00000000000000000000000000000000&NAME=0000&PATH=0";
24 Draw a dialog and return the selected key.
26 @param NumberOfLines The number of lines for the dialog box
27 @param KeyValue The EFI_KEY value returned if HotKey is TRUE..
28 @param String Pointer to the first string in the list
29 @param ... A series of (quantity == NumberOfLines - 1) text
30 strings which will be used to construct the dialog
33 @retval EFI_SUCCESS Displayed dialog and received user interaction
34 @retval EFI_INVALID_PARAMETER One of the parameters was invalid.
40 IN UINTN NumberOfLines
,
41 OUT EFI_INPUT_KEY
*KeyValue
,
55 UINTN DimensionsWidth
;
56 UINTN DimensionsHeight
;
66 EFI_EVENT WaitList
[2];
67 UINTN CurrentAttribute
;
68 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*ConOut
;
70 if ((KeyValue
== NULL
) || (String
== NULL
)) {
71 return EFI_INVALID_PARAMETER
;
80 ConOut
->QueryMode (ConOut
, ConOut
->Mode
->Mode
, &RightColumn
, &BottomRow
);
82 DimensionsWidth
= RightColumn
- LeftColumn
;
83 DimensionsHeight
= BottomRow
- TopRow
;
85 CurrentAttribute
= ConOut
->Mode
->Attribute
;
87 LineBuffer
= AllocateZeroPool (DimensionsWidth
* sizeof (CHAR16
));
88 ASSERT (LineBuffer
!= NULL
);
91 // Determine the largest string in the dialog box
92 // Notice we are starting with 1 since String is the first string
94 StringArray
= AllocateZeroPool (NumberOfLines
* sizeof (CHAR16
*));
95 LargestString
= StrLen (String
);
96 StringArray
[0] = String
;
98 VA_START (Marker
, String
);
99 for (Index
= 1; Index
< NumberOfLines
; Index
++) {
100 StackString
= VA_ARG (Marker
, CHAR16
*);
102 if (StackString
== NULL
) {
103 return EFI_INVALID_PARAMETER
;
106 StringArray
[Index
] = StackString
;
107 StringLen
= StrLen (StackString
);
108 if (StringLen
> LargestString
) {
109 LargestString
= StringLen
;
114 if ((LargestString
+ 2) > DimensionsWidth
) {
115 LargestString
= DimensionsWidth
- 2;
119 // Subtract the PopUp width from total Columns, allow for one space extra on
120 // each end plus a border.
122 Start
= (DimensionsWidth
- LargestString
- 2) / 2 + LeftColumn
+ 1;
124 Top
= ((DimensionsHeight
- NumberOfLines
- 2) / 2) + TopRow
- 1;
129 ConOut
->EnableCursor (ConOut
, FALSE
);
130 ConOut
->SetAttribute (ConOut
, EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
);
132 StringPtr
= &LineBuffer
[0];
133 *StringPtr
++ = BOXDRAW_DOWN_RIGHT
;
134 for (Index
= 0; Index
< LargestString
; Index
++) {
135 *StringPtr
++ = BOXDRAW_HORIZONTAL
;
137 *StringPtr
++ = BOXDRAW_DOWN_LEFT
;
140 ConOut
->SetCursorPosition (ConOut
, Start
, Top
);
141 ConOut
->OutputString (ConOut
, LineBuffer
);
143 for (Index
= 0; Index
< NumberOfLines
; Index
++) {
144 StringPtr
= &LineBuffer
[0];
145 *StringPtr
++ = BOXDRAW_VERTICAL
;
147 for (Count
= 0; Count
< LargestString
; Count
++) {
148 StringPtr
[Count
] = L
' ';
151 StringLen
= StrLen (StringArray
[Index
]);
152 if (StringLen
> LargestString
) {
153 StringLen
= LargestString
;
156 StringPtr
+ ((LargestString
- StringLen
) / 2),
158 StringLen
* sizeof (CHAR16
)
160 StringPtr
+= LargestString
;
162 *StringPtr
++ = BOXDRAW_VERTICAL
;
165 ConOut
->SetCursorPosition (ConOut
, Start
, Top
+ 1 + Index
);
166 ConOut
->OutputString (ConOut
, LineBuffer
);
169 StringPtr
= &LineBuffer
[0];
170 *StringPtr
++ = BOXDRAW_UP_RIGHT
;
171 for (Index
= 0; Index
< LargestString
; Index
++) {
172 *StringPtr
++ = BOXDRAW_HORIZONTAL
;
174 *StringPtr
++ = BOXDRAW_UP_LEFT
;
177 ConOut
->SetCursorPosition (ConOut
, Start
, Top
+ NumberOfLines
+ 1);
178 ConOut
->OutputString (ConOut
, LineBuffer
);
181 Status
= gBS
->CreateEvent (EVT_TIMER
, 0, NULL
, NULL
, &TimerEvent
);
184 // Set a timer event of 1 second expiration
193 // Wait for the keystroke event or the timer
195 WaitList
[0] = gST
->ConIn
->WaitForKey
;
196 WaitList
[1] = TimerEvent
;
197 Status
= gBS
->WaitForEvent (2, WaitList
, &Index
);
200 // Check for the timer expiration
202 if (!EFI_ERROR (Status
) && Index
== 1) {
203 Status
= EFI_TIMEOUT
;
206 gBS
->CloseEvent (TimerEvent
);
207 } while (Status
== EFI_TIMEOUT
);
209 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
210 CopyMem (KeyValue
, &Key
, sizeof (EFI_INPUT_KEY
));
212 ConOut
->SetAttribute (ConOut
, CurrentAttribute
);
213 ConOut
->EnableCursor (ConOut
, TRUE
);
220 Swap bytes in the buffer. This is a internal function.
222 @param Buffer Binary buffer.
223 @param BufferSize Size of the buffer in bytes.
230 IN OUT UINT8
*Buffer
,
238 SwapCount
= BufferSize
/ 2;
239 for (Index
= 0; Index
< SwapCount
; Index
++) {
240 Temp
= Buffer
[Index
];
241 Buffer
[Index
] = Buffer
[BufferSize
- 1 - Index
];
242 Buffer
[BufferSize
- 1 - Index
] = Temp
;
247 Converts the unicode character of the string from uppercase to lowercase.
248 This is a internal function.
250 @param Str String to be converted
261 for (Ptr
= Str
; *Ptr
!= L
'\0'; Ptr
++) {
262 if (*Ptr
>= L
'A' && *Ptr
<= L
'Z') {
263 *Ptr
= (CHAR16
) (*Ptr
- L
'A' + L
'a');
270 Converts binary buffer to Unicode string in reversed byte order from BufToHexString().
272 @param Str String for output
273 @param Buffer Binary buffer.
274 @param BufferSize Size of the buffer in bytes.
276 @retval EFI_SUCCESS The function completed successfully.
291 NewBuffer
= AllocateCopyPool (BufferSize
, Buffer
);
292 SwapBuffer (NewBuffer
, BufferSize
);
294 StrBufferLen
= BufferSize
* sizeof (CHAR16
) + 1;
295 Status
= BufToHexString (Str
, &StrBufferLen
, NewBuffer
, BufferSize
);
297 FreePool (NewBuffer
);
299 // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.
308 Converts Hex String to binary buffer in reversed byte order from HexStringToBuf().
310 @param Buffer Pointer to buffer that receives the data.
311 @param BufferSize Length in bytes of the buffer to hold converted
312 data. If routine return with EFI_SUCCESS,
313 containing length of converted data. If routine
314 return with EFI_BUFFER_TOO_SMALL, containg length
316 @param Str String to be converted from.
318 @retval EFI_SUCCESS The function completed successfully.
324 IN OUT UINT8
*Buffer
,
325 IN OUT UINTN
*BufferSize
,
330 UINTN ConvertedStrLen
;
333 Status
= HexStringToBuf (Buffer
, BufferSize
, Str
, &ConvertedStrLen
);
334 if (!EFI_ERROR (Status
)) {
335 SwapBuffer (Buffer
, ConvertedStrLen
);
342 Convert binary representation Config string (e.g. "0041004200430044") to the
343 original string (e.g. "ABCD"). Config string appears in <ConfigHdr> (i.e.
344 "&NAME=<string>"), or Name/Value pair in <ConfigBody> (i.e. "label=<string>").
346 @param UnicodeString Original Unicode string.
347 @param StrBufferLen On input: Length in bytes of buffer to hold the Unicode string.
348 Includes tailing '\0' character.
350 If return EFI_SUCCESS, containing length of Unicode string buffer.
351 If return EFI_BUFFER_TOO_SMALL, containg length of string buffer desired.
352 @param ConfigString Binary representation of Unicode String, <string> := (<HexCh>4)+
354 @retval EFI_SUCCESS Routine success.
355 @retval EFI_BUFFER_TOO_SMALL The string buffer is too small.
360 ConfigStringToUnicode (
361 IN OUT CHAR16
*UnicodeString
,
362 IN OUT UINTN
*StrBufferLen
,
363 IN CHAR16
*ConfigString
371 Len
= StrLen (ConfigString
) / 4;
372 BufferSize
= (Len
+ 1) * sizeof (CHAR16
);
374 if (*StrBufferLen
< BufferSize
) {
375 *StrBufferLen
= BufferSize
;
376 return EFI_BUFFER_TOO_SMALL
;
379 *StrBufferLen
= BufferSize
;
381 for (Index
= 0; Index
< Len
; Index
++) {
382 BackupChar
= ConfigString
[4];
383 ConfigString
[4] = L
'\0';
385 HexStringToBuf ((UINT8
*) UnicodeString
, &BufferSize
, ConfigString
, NULL
);
387 ConfigString
[4] = BackupChar
;
394 // Add tailing '\0' character
396 *UnicodeString
= L
'\0';
402 Convert Unicode string to binary representation Config string, e.g.
403 "ABCD" => "0041004200430044". Config string appears in <ConfigHdr> (i.e.
404 "&NAME=<string>"), or Name/Value pair in <ConfigBody> (i.e. "label=<string>").
406 @param ConfigString Binary representation of Unicode String, <string> := (<HexCh>4)+
407 @param StrBufferLen On input: Length in bytes of buffer to hold the Unicode string.
408 Includes tailing '\0' character.
410 If return EFI_SUCCESS, containing length of Unicode string buffer.
411 If return EFI_BUFFER_TOO_SMALL, containg length of string buffer desired.
412 @param UnicodeString Original Unicode string.
414 @retval EFI_SUCCESS Routine success.
415 @retval EFI_BUFFER_TOO_SMALL The string buffer is too small.
420 UnicodeToConfigString (
421 IN OUT CHAR16
*ConfigString
,
422 IN OUT UINTN
*StrBufferLen
,
423 IN CHAR16
*UnicodeString
431 Len
= StrLen (UnicodeString
);
432 BufferSize
= (Len
* 4 + 1) * sizeof (CHAR16
);
434 if (*StrBufferLen
< BufferSize
) {
435 *StrBufferLen
= BufferSize
;
436 return EFI_BUFFER_TOO_SMALL
;
439 *StrBufferLen
= BufferSize
;
440 String
= ConfigString
;
442 for (Index
= 0; Index
< Len
; Index
++) {
443 BufToHexString (ConfigString
, &BufferSize
, (UINT8
*) UnicodeString
, 2);
450 // Add tailing '\0' character
452 *ConfigString
= L
'\0';
455 // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.
462 Construct <ConfigHdr> using routing information GUID/NAME/PATH.
464 @param ConfigHdr Pointer to the ConfigHdr string.
465 @param StrBufferLen On input: Length in bytes of buffer to hold the
466 ConfigHdr string. Includes tailing '\0' character.
467 On output: If return EFI_SUCCESS, containing
468 length of ConfigHdr string buffer. If return
469 EFI_BUFFER_TOO_SMALL, containg length of string
471 @param Guid Routing information: GUID.
472 @param Name Routing information: NAME.
473 @param DriverHandle Driver handle which contains the routing
476 @retval EFI_SUCCESS Routine success.
477 @retval EFI_BUFFER_TOO_SMALL The ConfigHdr string buffer is too small.
483 IN OUT CHAR16
*ConfigHdr
,
484 IN OUT UINTN
*StrBufferLen
,
486 IN CHAR16
*Name
, OPTIONAL
487 IN EFI_HANDLE
*DriverHandle
492 UINTN DevicePathSize
;
495 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
499 // There will be no "NAME" in <ConfigHdr> for Name/Value storage
504 // For buffer storage
506 NameStrLen
= StrLen (Name
);
510 // Retrieve DevicePath Protocol associated with this HiiPackageList
512 Status
= gBS
->HandleProtocol (
514 &gEfiDevicePathProtocolGuid
,
515 (VOID
**) &DevicePath
517 if (EFI_ERROR (Status
)) {
521 DevicePathSize
= GetDevicePathSize (DevicePath
);
524 // GUID=<HexCh>32&NAME=<Char>NameStrLen&PATH=<HexChar>DevicePathStrLen <NULL>
525 // | 5 | 32 | 6 | NameStrLen*4 | 6 | DevicePathStrLen | 1 |
527 BufferSize
= (5 + 32 + 6 + NameStrLen
* 4 + 6 + DevicePathSize
* 2 + 1) * sizeof (CHAR16
);
528 if (*StrBufferLen
< BufferSize
) {
529 *StrBufferLen
= BufferSize
;
530 return EFI_BUFFER_TOO_SMALL
;
533 *StrBufferLen
= BufferSize
;
537 StrCpy (StrPtr
, L
"GUID=");
539 BufferToHexString (StrPtr
, (UINT8
*) Guid
, sizeof (EFI_GUID
));
543 // Convert name string, e.g. name "ABCD" => "&NAME=0041004200430044"
545 StrCpy (StrPtr
, L
"&NAME=");
548 BufferSize
= (NameStrLen
* 4 + 1) * sizeof (CHAR16
);
549 UnicodeToConfigString (StrPtr
, &BufferSize
, Name
);
550 StrPtr
+= (NameStrLen
* 4);
553 StrCpy (StrPtr
, L
"&PATH=");
555 BufferToHexString (StrPtr
, (UINT8
*) DevicePath
, DevicePathSize
);
562 Search BlockName "&OFFSET=Offset&WIDTH=Width" in a string.
564 @param String The string to be searched in.
565 @param Offset Offset in BlockName.
566 @param Width Width in BlockName.
568 @retval TRUE Block name found.
569 @retval FALSE Block name not found.
575 IN OUT CHAR16
*String
,
583 UINTN ConvertedStrLen
;
585 while ((String
= StrStr (String
, L
"&OFFSET=")) != NULL
) {
592 BufferSize
= sizeof (UINTN
);
593 Status
= HexStringToBuf ((UINT8
*) &Data
, &BufferSize
, String
, &ConvertedStrLen
);
594 if (EFI_ERROR (Status
)) {
597 String
= String
+ ConvertedStrLen
;
599 if (Data
!= Offset
) {
603 if (StrnCmp (String
, L
"&WIDTH=", 7) != 0) {
609 BufferSize
= sizeof (UINTN
);
610 Status
= HexStringToBuf ((UINT8
*) &Data
, &BufferSize
, String
, &ConvertedStrLen
);
611 if (EFI_ERROR (Status
)) {
618 String
= String
+ ConvertedStrLen
;
626 This routine is invoked by ConfigAccess.Callback() to retrived uncommitted data from Form Browser.
628 @param VariableGuid An optional field to indicate the target variable
630 @param VariableName An optional field to indicate the target
631 human-readable variable name.
632 @param BufferSize On input: Length in bytes of buffer to hold
633 retrived data. On output: If return
634 EFI_BUFFER_TOO_SMALL, containg length of buffer
636 @param Buffer Buffer to hold retrived data.
638 @retval EFI_SUCCESS Routine success.
639 @retval EFI_BUFFER_TOO_SMALL The intput buffer is too small.
645 EFI_GUID
*VariableGuid
, OPTIONAL
646 CHAR16
*VariableName
, OPTIONAL
652 CONST CHAR16
*ConfigHdr
;
658 EFI_FORM_BROWSER2_PROTOCOL
*FormBrowser2
;
659 EFI_HII_CONFIG_ROUTING_PROTOCOL
*HiiConfigRouting
;
662 // Locate protocols for use
664 Status
= gBS
->LocateProtocol (&gEfiFormBrowser2ProtocolGuid
, NULL
, (VOID
**) &FormBrowser2
);
665 if (EFI_ERROR (Status
)) {
669 Status
= gBS
->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid
, NULL
, (VOID
**) &HiiConfigRouting
);
670 if (EFI_ERROR (Status
)) {
675 // Retrive formset storage data from Form Browser
677 ConfigHdr
= mFakeConfigHdr
;
678 HeaderLen
= StrLen (ConfigHdr
);
681 ConfigResp
= AllocateZeroPool (BufferLen
+ HeaderLen
);
683 StringPtr
= ConfigResp
+ HeaderLen
;
687 Status
= FormBrowser2
->BrowserCallback (
695 if (Status
== EFI_BUFFER_TOO_SMALL
) {
696 FreePool (ConfigResp
);
697 ConfigResp
= AllocateZeroPool (BufferLen
+ HeaderLen
);
699 StringPtr
= ConfigResp
+ HeaderLen
;
703 Status
= FormBrowser2
->BrowserCallback (
712 if (EFI_ERROR (Status
)) {
713 FreePool (ConfigResp
);
716 CopyMem (ConfigResp
, ConfigHdr
, HeaderLen
* sizeof (UINT16
));
719 // Convert <ConfigResp> to buffer data
721 Status
= HiiConfigRouting
->ConfigToBlock (
728 FreePool (ConfigResp
);
735 This routine is invoked by ConfigAccess.Callback() to update uncommitted data of Form Browser.
737 @param VariableGuid An optional field to indicate the target variable
739 @param VariableName An optional field to indicate the target
740 human-readable variable name.
741 @param BufferSize Length in bytes of buffer to hold retrived data.
742 @param Buffer Buffer to hold retrived data.
743 @param RequestElement An optional field to specify which part of the
744 buffer data will be send back to Browser. If NULL,
745 the whole buffer of data will be committed to
746 Browser. <RequestElement> ::=
747 &OFFSET=<Number>&WIDTH=<Number>*
749 @retval EFI_SUCCESS Routine success.
750 @retval Other Updating Browser uncommitted data failed.
756 EFI_GUID
*VariableGuid
, OPTIONAL
757 CHAR16
*VariableName
, OPTIONAL
760 CHAR16
*RequestElement OPTIONAL
764 CONST CHAR16
*ConfigHdr
;
770 EFI_FORM_BROWSER2_PROTOCOL
*FormBrowser2
;
771 EFI_HII_CONFIG_ROUTING_PROTOCOL
*HiiConfigRouting
;
772 CHAR16 BlockName
[33];
773 CHAR16
*ConfigRequest
;
777 // Locate protocols for use
779 Status
= gBS
->LocateProtocol (&gEfiFormBrowser2ProtocolGuid
, NULL
, (VOID
**) &FormBrowser2
);
780 if (EFI_ERROR (Status
)) {
784 Status
= gBS
->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid
, NULL
, (VOID
**) &HiiConfigRouting
);
785 if (EFI_ERROR (Status
)) {
790 // Prepare <ConfigRequest>
792 ConfigHdr
= mFakeConfigHdr
;
793 HeaderLen
= StrLen (ConfigHdr
);
795 if (RequestElement
== NULL
) {
797 // RequestElement not specified, use "&OFFSET=0&WIDTH=<BufferSize>" as <BlockName>
799 BlockName
[0] = L
'\0';
800 StrCpy (BlockName
, L
"&OFFSET=0&WIDTH=");
803 // String lenghth of L"&OFFSET=0&WIDTH=" is 16
805 StringPtr
= BlockName
+ 16;
806 BufferLen
= sizeof (BlockName
) - (16 * sizeof (CHAR16
));
807 BufToHexString (StringPtr
, &BufferLen
, (UINT8
*) &BufferSize
, sizeof (UINTN
));
811 Request
= RequestElement
;
814 BufferLen
= HeaderLen
* sizeof (CHAR16
) + StrSize (Request
);
815 ConfigRequest
= AllocateZeroPool (BufferLen
);
817 CopyMem (ConfigRequest
, ConfigHdr
, HeaderLen
* sizeof (CHAR16
));
818 StringPtr
= ConfigRequest
+ HeaderLen
;
819 StrCpy (StringPtr
, Request
);
822 // Convert buffer to <ConfigResp>
824 Status
= HiiConfigRouting
->BlockToConfig (
832 if (EFI_ERROR (Status
)) {
833 FreePool (ConfigResp
);
838 // Skip <ConfigHdr> and '&'
840 StringPtr
= ConfigResp
+ HeaderLen
+ 1;
843 // Change uncommitted data in Browser
845 Status
= FormBrowser2
->BrowserCallback (
853 FreePool (ConfigResp
);