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"
18 STATIC CONST EFI_FORM_BROWSER2_PROTOCOL
*mFormBrowser2
= NULL
;
19 STATIC CONST EFI_HII_CONFIG_ROUTING_PROTOCOL
*mHiiConfigRouting
= NULL
;
22 This function locate FormBrowser2 protocols for later usage.
24 @return Status the status to locate protocol.
27 LocateFormBrowser2Protocols (
33 // Locate protocols for later usage
35 if (mFormBrowser2
== NULL
) {
36 Status
= gBS
->LocateProtocol (&gEfiFormBrowser2ProtocolGuid
, NULL
, (VOID
**) &mFormBrowser2
);
37 if (EFI_ERROR (Status
)) {
42 if (mHiiConfigRouting
== NULL
) {
43 Status
= gBS
->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid
, NULL
, (VOID
**) &mHiiConfigRouting
);
44 if (EFI_ERROR (Status
)) {
55 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT16 mFakeConfigHdr
[] = L
"GUID=00000000000000000000000000000000&NAME=0000&PATH=0";
58 Draw a dialog and return the selected key.
60 @param NumberOfLines The number of lines for the dialog box
61 @param KeyValue The EFI_KEY value returned if HotKey is TRUE..
62 @param String Pointer to the first string in the list
63 @param ... A series of (quantity == NumberOfLines - 1) text
64 strings which will be used to construct the dialog
67 @retval EFI_SUCCESS Displayed dialog and received user interaction
68 @retval EFI_INVALID_PARAMETER One of the parameters was invalid.
69 @retval EFI_OUT_OF_RESOURCES There is no enough available memory space.
75 IN UINTN NumberOfLines
,
76 OUT EFI_INPUT_KEY
*KeyValue
,
89 UINTN DimensionsWidth
;
90 UINTN DimensionsHeight
;
99 EFI_EVENT WaitList
[2];
100 UINTN CurrentAttribute
;
101 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL
*ConOut
;
104 String
= VA_ARG (Marker
, CHAR16
*);
106 if ((KeyValue
== NULL
) || (String
== NULL
)) {
107 return EFI_INVALID_PARAMETER
;
115 ConOut
= gST
->ConOut
;
116 ConOut
->QueryMode (ConOut
, ConOut
->Mode
->Mode
, &RightColumn
, &BottomRow
);
118 DimensionsWidth
= RightColumn
- LeftColumn
;
119 DimensionsHeight
= BottomRow
- TopRow
;
121 CurrentAttribute
= ConOut
->Mode
->Attribute
;
123 LineBuffer
= AllocateZeroPool (DimensionsWidth
* sizeof (CHAR16
));
124 if (LineBuffer
== NULL
) {
125 return EFI_OUT_OF_RESOURCES
;
129 // Determine the largest string in the dialog box
130 // Notice we are starting with 1 since String is the first string
132 StringArray
= AllocateZeroPool (NumberOfLines
* sizeof (CHAR16
*));
133 if (StringArray
== NULL
) {
134 FreePool (LineBuffer
);
135 return EFI_OUT_OF_RESOURCES
;
137 LargestString
= StrLen (String
);
138 StringArray
[0] = String
;
140 for (Index
= 1; Index
< NumberOfLines
; Index
++) {
141 StackString
= VA_ARG (Marker
, CHAR16
*);
143 if (StackString
== NULL
) {
144 FreePool (LineBuffer
);
145 FreePool (StringArray
);
146 return EFI_INVALID_PARAMETER
;
149 StringArray
[Index
] = StackString
;
150 StringLen
= StrLen (StackString
);
151 if (StringLen
> LargestString
) {
152 LargestString
= StringLen
;
156 if ((LargestString
+ 2) > DimensionsWidth
) {
157 LargestString
= DimensionsWidth
- 2;
161 // Subtract the PopUp width from total Columns, allow for one space extra on
162 // each end plus a border.
164 Start
= (DimensionsWidth
- LargestString
- 2) / 2 + LeftColumn
+ 1;
166 Top
= ((DimensionsHeight
- NumberOfLines
- 2) / 2) + TopRow
- 1;
171 ConOut
->EnableCursor (ConOut
, FALSE
);
172 ConOut
->SetAttribute (ConOut
, EFI_LIGHTGRAY
| EFI_BACKGROUND_BLUE
);
174 StringPtr
= &LineBuffer
[0];
175 *StringPtr
++ = BOXDRAW_DOWN_RIGHT
;
176 for (Index
= 0; Index
< LargestString
; Index
++) {
177 *StringPtr
++ = BOXDRAW_HORIZONTAL
;
179 *StringPtr
++ = BOXDRAW_DOWN_LEFT
;
182 ConOut
->SetCursorPosition (ConOut
, Start
, Top
);
183 ConOut
->OutputString (ConOut
, LineBuffer
);
185 for (Index
= 0; Index
< NumberOfLines
; Index
++) {
186 StringPtr
= &LineBuffer
[0];
187 *StringPtr
++ = BOXDRAW_VERTICAL
;
189 for (Count
= 0; Count
< LargestString
; Count
++) {
190 StringPtr
[Count
] = L
' ';
193 StringLen
= StrLen (StringArray
[Index
]);
194 if (StringLen
> LargestString
) {
195 StringLen
= LargestString
;
198 StringPtr
+ ((LargestString
- StringLen
) / 2),
200 StringLen
* sizeof (CHAR16
)
202 StringPtr
+= LargestString
;
204 *StringPtr
++ = BOXDRAW_VERTICAL
;
207 ConOut
->SetCursorPosition (ConOut
, Start
, Top
+ 1 + Index
);
208 ConOut
->OutputString (ConOut
, LineBuffer
);
211 StringPtr
= &LineBuffer
[0];
212 *StringPtr
++ = BOXDRAW_UP_RIGHT
;
213 for (Index
= 0; Index
< LargestString
; Index
++) {
214 *StringPtr
++ = BOXDRAW_HORIZONTAL
;
216 *StringPtr
++ = BOXDRAW_UP_LEFT
;
219 ConOut
->SetCursorPosition (ConOut
, Start
, Top
+ NumberOfLines
+ 1);
220 ConOut
->OutputString (ConOut
, LineBuffer
);
223 Status
= gBS
->CreateEvent (EVT_TIMER
, 0, NULL
, NULL
, &TimerEvent
);
226 // Set a timer event of 1 second expiration
235 // Wait for the keystroke event or the timer
237 WaitList
[0] = gST
->ConIn
->WaitForKey
;
238 WaitList
[1] = TimerEvent
;
239 Status
= gBS
->WaitForEvent (2, WaitList
, &Index
);
242 // Check for the timer expiration
244 if (!EFI_ERROR (Status
) && Index
== 1) {
245 Status
= EFI_TIMEOUT
;
248 gBS
->CloseEvent (TimerEvent
);
249 } while (Status
== EFI_TIMEOUT
);
251 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
252 CopyMem (KeyValue
, &Key
, sizeof (EFI_INPUT_KEY
));
254 ConOut
->SetAttribute (ConOut
, CurrentAttribute
);
255 ConOut
->EnableCursor (ConOut
, TRUE
);
257 FreePool (LineBuffer
);
258 FreePool (StringArray
);
265 Draw a dialog and return the selected key.
267 @param NumberOfLines The number of lines for the dialog box
268 @param KeyValue The EFI_KEY value returned if HotKey is TRUE..
269 @param String Pointer to the first string in the list
270 @param ... A series of (quantity == NumberOfLines - 1) text
271 strings which will be used to construct the dialog
274 @retval EFI_SUCCESS Displayed dialog and received user interaction
275 @retval EFI_INVALID_PARAMETER One of the parameters was invalid.
281 IN UINTN NumberOfLines
,
282 OUT EFI_INPUT_KEY
*KeyValue
,
290 VA_START (Marker
, KeyValue
);
292 Status
= IfrLibCreatePopUp2 (NumberOfLines
, KeyValue
, Marker
);
300 Swap bytes in the buffer. This is a internal function.
302 @param Buffer Binary buffer.
303 @param BufferSize Size of the buffer in bytes.
310 IN OUT UINT8
*Buffer
,
318 SwapCount
= BufferSize
/ 2;
319 for (Index
= 0; Index
< SwapCount
; Index
++) {
320 Temp
= Buffer
[Index
];
321 Buffer
[Index
] = Buffer
[BufferSize
- 1 - Index
];
322 Buffer
[BufferSize
- 1 - Index
] = Temp
;
327 Converts the unicode character of the string from uppercase to lowercase.
328 This is a internal function.
330 @param Str String to be converted
341 for (Ptr
= Str
; *Ptr
!= L
'\0'; Ptr
++) {
342 if (*Ptr
>= L
'A' && *Ptr
<= L
'Z') {
343 *Ptr
= (CHAR16
) (*Ptr
- L
'A' + L
'a');
350 Converts binary buffer to Unicode string in reversed byte order from BufToHexString().
352 @param Str String for output
353 @param Buffer Binary buffer.
354 @param BufferSize Size of the buffer in bytes.
356 @retval EFI_SUCCESS The function completed successfully.
357 @retval EFI_OUT_OF_RESOURCES There is no enough available memory space.
372 NewBuffer
= AllocateCopyPool (BufferSize
, Buffer
);
373 if (NewBuffer
== NULL
) {
374 return EFI_OUT_OF_RESOURCES
;
376 SwapBuffer (NewBuffer
, BufferSize
);
378 StrBufferLen
= BufferSize
* sizeof (CHAR16
) + 1;
379 Status
= BufToHexString (Str
, &StrBufferLen
, NewBuffer
, BufferSize
);
381 FreePool (NewBuffer
);
383 // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.
392 Converts Hex String to binary buffer in reversed byte order from HexStringToBuf().
394 @param Buffer Pointer to buffer that receives the data.
395 @param BufferSize Length in bytes of the buffer to hold converted
396 data. If routine return with EFI_SUCCESS,
397 containing length of converted data. If routine
398 return with EFI_BUFFER_TOO_SMALL, containg length
400 @param Str String to be converted from.
402 @retval EFI_SUCCESS The function completed successfully.
408 IN OUT UINT8
*Buffer
,
409 IN OUT UINTN
*BufferSize
,
414 UINTN ConvertedStrLen
;
417 Status
= HexStringToBuf (Buffer
, BufferSize
, Str
, &ConvertedStrLen
);
418 if (!EFI_ERROR (Status
)) {
419 SwapBuffer (Buffer
, ConvertedStrLen
);
426 Convert binary representation Config string (e.g. "0041004200430044") to the
427 original string (e.g. "ABCD"). Config string appears in <ConfigHdr> (i.e.
428 "&NAME=<string>"), or Name/Value pair in <ConfigBody> (i.e. "label=<string>").
430 @param UnicodeString Original Unicode string.
431 @param StrBufferLen On input: Length in bytes of buffer to hold the Unicode string.
432 Includes tailing '\0' character.
434 If return EFI_SUCCESS, containing length of Unicode string buffer.
435 If return EFI_BUFFER_TOO_SMALL, containg length of string buffer desired.
436 @param ConfigString Binary representation of Unicode String, <string> := (<HexCh>4)+
438 @retval EFI_SUCCESS Routine success.
439 @retval EFI_BUFFER_TOO_SMALL The string buffer is too small.
444 ConfigStringToUnicode (
445 IN OUT CHAR16
*UnicodeString
,
446 IN OUT UINTN
*StrBufferLen
,
447 IN CHAR16
*ConfigString
455 Len
= StrLen (ConfigString
) / 4;
456 BufferSize
= (Len
+ 1) * sizeof (CHAR16
);
458 if (*StrBufferLen
< BufferSize
) {
459 *StrBufferLen
= BufferSize
;
460 return EFI_BUFFER_TOO_SMALL
;
463 *StrBufferLen
= BufferSize
;
465 for (Index
= 0; Index
< Len
; Index
++) {
466 BackupChar
= ConfigString
[4];
467 ConfigString
[4] = L
'\0';
469 HexStringToBuf ((UINT8
*) UnicodeString
, &BufferSize
, ConfigString
, NULL
);
471 ConfigString
[4] = BackupChar
;
478 // Add tailing '\0' character
480 *UnicodeString
= L
'\0';
486 Convert Unicode string to binary representation Config string, e.g.
487 "ABCD" => "0041004200430044". Config string appears in <ConfigHdr> (i.e.
488 "&NAME=<string>"), or Name/Value pair in <ConfigBody> (i.e. "label=<string>").
490 @param ConfigString Binary representation of Unicode String, <string> := (<HexCh>4)+
491 @param StrBufferLen On input: Length in bytes of buffer to hold the Unicode string.
492 Includes tailing '\0' character.
494 If return EFI_SUCCESS, containing length of Unicode string buffer.
495 If return EFI_BUFFER_TOO_SMALL, containg length of string buffer desired.
496 @param UnicodeString Original Unicode string.
498 @retval EFI_SUCCESS Routine success.
499 @retval EFI_BUFFER_TOO_SMALL The string buffer is too small.
504 UnicodeToConfigString (
505 IN OUT CHAR16
*ConfigString
,
506 IN OUT UINTN
*StrBufferLen
,
507 IN CHAR16
*UnicodeString
515 Len
= StrLen (UnicodeString
);
516 BufferSize
= (Len
* 4 + 1) * sizeof (CHAR16
);
518 if (*StrBufferLen
< BufferSize
) {
519 *StrBufferLen
= BufferSize
;
520 return EFI_BUFFER_TOO_SMALL
;
523 *StrBufferLen
= BufferSize
;
524 String
= ConfigString
;
526 for (Index
= 0; Index
< Len
; Index
++) {
527 BufToHexString (ConfigString
, &BufferSize
, (UINT8
*) UnicodeString
, 2);
534 // Add tailing '\0' character
536 *ConfigString
= L
'\0';
539 // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.
546 Construct <ConfigHdr> using routing information GUID/NAME/PATH.
548 @param ConfigHdr Pointer to the ConfigHdr string.
549 @param StrBufferLen On input: Length in bytes of buffer to hold the
550 ConfigHdr string. Includes tailing '\0' character.
551 On output: If return EFI_SUCCESS, containing
552 length of ConfigHdr string buffer. If return
553 EFI_BUFFER_TOO_SMALL, containg length of string
555 @param Guid Routing information: GUID.
556 @param Name Routing information: NAME.
557 @param DriverHandle Driver handle which contains the routing
560 @retval EFI_SUCCESS Routine success.
561 @retval EFI_BUFFER_TOO_SMALL The ConfigHdr string buffer is too small.
567 IN OUT CHAR16
*ConfigHdr
,
568 IN OUT UINTN
*StrBufferLen
,
570 IN CHAR16
*Name
, OPTIONAL
571 IN EFI_HANDLE
*DriverHandle
576 UINTN DevicePathSize
;
579 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
583 // There will be no "NAME" in <ConfigHdr> for Name/Value storage
588 // For buffer storage
590 NameStrLen
= StrLen (Name
);
594 // Retrieve DevicePath Protocol associated with this HiiPackageList
596 Status
= gBS
->HandleProtocol (
598 &gEfiDevicePathProtocolGuid
,
599 (VOID
**) &DevicePath
601 if (EFI_ERROR (Status
)) {
605 DevicePathSize
= GetDevicePathSize (DevicePath
);
608 // GUID=<HexCh>32&NAME=<Char>NameStrLen&PATH=<HexChar>DevicePathStrLen <NULL>
609 // | 5 | 32 | 6 | NameStrLen*4 | 6 | DevicePathStrLen | 1 |
611 BufferSize
= (5 + 32 + 6 + NameStrLen
* 4 + 6 + DevicePathSize
* 2 + 1) * sizeof (CHAR16
);
612 if (*StrBufferLen
< BufferSize
) {
613 *StrBufferLen
= BufferSize
;
614 return EFI_BUFFER_TOO_SMALL
;
617 *StrBufferLen
= BufferSize
;
621 StrCpy (StrPtr
, L
"GUID=");
623 BufferToHexString (StrPtr
, (UINT8
*) Guid
, sizeof (EFI_GUID
));
627 // Convert name string, e.g. name "ABCD" => "&NAME=0041004200430044"
629 StrCpy (StrPtr
, L
"&NAME=");
632 BufferSize
= (NameStrLen
* 4 + 1) * sizeof (CHAR16
);
633 UnicodeToConfigString (StrPtr
, &BufferSize
, Name
);
634 StrPtr
+= (NameStrLen
* 4);
637 StrCpy (StrPtr
, L
"&PATH=");
639 BufferToHexString (StrPtr
, (UINT8
*) DevicePath
, DevicePathSize
);
646 Search BlockName "&OFFSET=Offset&WIDTH=Width" in a string.
648 @param String The string to be searched in.
649 @param Offset Offset in BlockName.
650 @param Width Width in BlockName.
652 @retval TRUE Block name found.
653 @retval FALSE Block name not found.
659 IN OUT CHAR16
*String
,
667 UINTN ConvertedStrLen
;
669 while ((String
= StrStr (String
, L
"&OFFSET=")) != NULL
) {
676 BufferSize
= sizeof (UINTN
);
677 Status
= HexStringToBuf ((UINT8
*) &Data
, &BufferSize
, String
, &ConvertedStrLen
);
678 if (EFI_ERROR (Status
)) {
681 String
= String
+ ConvertedStrLen
;
683 if (Data
!= Offset
) {
687 if (StrnCmp (String
, L
"&WIDTH=", 7) != 0) {
693 BufferSize
= sizeof (UINTN
);
694 Status
= HexStringToBuf ((UINT8
*) &Data
, &BufferSize
, String
, &ConvertedStrLen
);
695 if (EFI_ERROR (Status
)) {
702 String
= String
+ ConvertedStrLen
;
710 This routine is invoked by ConfigAccess.Callback() to retrived uncommitted data from Form Browser.
712 @param VariableGuid An optional field to indicate the target variable
714 @param VariableName An optional field to indicate the target
715 human-readable variable name.
716 @param BufferSize On input: Length in bytes of buffer to hold
717 retrived data. On output: If return
718 EFI_BUFFER_TOO_SMALL, containg length of buffer
720 @param Buffer Buffer to hold retrived data.
722 @retval EFI_SUCCESS Routine success.
723 @retval EFI_BUFFER_TOO_SMALL The intput buffer is too small.
724 @retval EFI_OUT_OF_RESOURCES There is no enough available memory space.
730 EFI_GUID
*VariableGuid
, OPTIONAL
731 CHAR16
*VariableName
, OPTIONAL
737 CONST CHAR16
*ConfigHdr
;
745 // Locate protocols for use
747 Status
= LocateFormBrowser2Protocols ();
748 if (EFI_ERROR (Status
)) {
753 // Retrive formset storage data from Form Browser
755 ConfigHdr
= mFakeConfigHdr
;
756 HeaderLen
= StrLen (ConfigHdr
);
759 // First try allocate 0x4000 buffer for the formet storage data.
762 ConfigResp
= AllocateZeroPool (BufferLen
+ HeaderLen
);
763 if (ConfigResp
== NULL
) {
767 StringPtr
= ConfigResp
+ HeaderLen
;
771 Status
= mFormBrowser2
->BrowserCallback (
779 if (Status
== EFI_BUFFER_TOO_SMALL
) {
780 if (ConfigResp
!= NULL
) {
781 FreePool (ConfigResp
);
784 ConfigResp
= AllocateZeroPool (BufferLen
+ HeaderLen
);
785 if (ConfigResp
== NULL
) {
786 return EFI_OUT_OF_RESOURCES
;
789 StringPtr
= ConfigResp
+ HeaderLen
;
793 Status
= mFormBrowser2
->BrowserCallback (
802 if (EFI_ERROR (Status
)) {
803 FreePool (ConfigResp
);
806 CopyMem (ConfigResp
, ConfigHdr
, HeaderLen
* sizeof (UINT16
));
809 // Convert <ConfigResp> to buffer data
811 Status
= mHiiConfigRouting
->ConfigToBlock (
818 FreePool (ConfigResp
);
825 This routine is invoked by ConfigAccess.Callback() to update uncommitted data of Form Browser.
827 @param VariableGuid An optional field to indicate the target variable
829 @param VariableName An optional field to indicate the target
830 human-readable variable name.
831 @param BufferSize Length in bytes of buffer to hold retrived data.
832 @param Buffer Buffer to hold retrived data.
833 @param RequestElement An optional field to specify which part of the
834 buffer data will be send back to Browser. If NULL,
835 the whole buffer of data will be committed to
836 Browser. <RequestElement> ::=
837 &OFFSET=<Number>&WIDTH=<Number>*
839 @retval EFI_SUCCESS Routine success.
840 @retval EFI_OUT_OF_RESOURCES There is no enough available memory space.
841 @retval Other Updating Browser uncommitted data failed.
847 EFI_GUID
*VariableGuid
, OPTIONAL
848 CHAR16
*VariableName
, OPTIONAL
851 CHAR16
*RequestElement OPTIONAL
855 CONST CHAR16
*ConfigHdr
;
861 CHAR16 BlockName
[33];
862 CHAR16
*ConfigRequest
;
866 // Locate protocols for use
868 Status
= LocateFormBrowser2Protocols ();
869 if (EFI_ERROR (Status
)) {
874 // Prepare <ConfigRequest>
876 ConfigHdr
= mFakeConfigHdr
;
877 HeaderLen
= StrLen (ConfigHdr
);
879 if (RequestElement
== NULL
) {
881 // RequestElement not specified, use "&OFFSET=0&WIDTH=<BufferSize>" as <BlockName>
883 BlockName
[0] = L
'\0';
884 StrCpy (BlockName
, L
"&OFFSET=0&WIDTH=");
887 // String lenghth of L"&OFFSET=0&WIDTH=" is 16
889 StringPtr
= BlockName
+ 16;
890 BufferLen
= sizeof (BlockName
) - (16 * sizeof (CHAR16
));
891 BufToHexString (StringPtr
, &BufferLen
, (UINT8
*) &BufferSize
, sizeof (UINTN
));
895 Request
= RequestElement
;
898 BufferLen
= HeaderLen
* sizeof (CHAR16
) + StrSize (Request
);
899 ConfigRequest
= AllocateZeroPool (BufferLen
);
900 if (ConfigRequest
== NULL
) {
901 return EFI_OUT_OF_RESOURCES
;
904 CopyMem (ConfigRequest
, ConfigHdr
, HeaderLen
* sizeof (CHAR16
));
905 StringPtr
= ConfigRequest
+ HeaderLen
;
906 StrCpy (StringPtr
, Request
);
909 // Convert buffer to <ConfigResp>
911 Status
= mHiiConfigRouting
->BlockToConfig (
919 if (EFI_ERROR (Status
)) {
920 FreePool (ConfigRequest
);
925 // Skip <ConfigHdr> and '&'
927 StringPtr
= ConfigResp
+ HeaderLen
+ 1;
930 // Change uncommitted data in Browser
932 Status
= mFormBrowser2
->BrowserCallback (
940 FreePool (ConfigRequest
);