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.
362 BufInReverseOrderToHexString (
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.
403 @retval RETURN_BUFFER_TOO_SMALL The input BufferSize is too small to hold the output. BufferSize
404 will be updated to the size required for the converstion.
409 HexStringToBufInReverseOrder (
410 IN OUT UINT8
*Buffer
,
411 IN OUT UINTN
*BufferSize
,
416 UINTN ConvertedStrLen
;
419 Status
= HexStringToBuf (Buffer
, BufferSize
, Str
, &ConvertedStrLen
);
420 if (!EFI_ERROR (Status
)) {
421 SwapBuffer (Buffer
, ConvertedStrLen
);
428 Convert binary representation Config string (e.g. "0041004200430044") to the
429 original string (e.g. "ABCD"). Config string appears in <ConfigHdr> (i.e.
430 "&NAME=<string>"), or Name/Value pair in <ConfigBody> (i.e. "label=<string>").
432 @param UnicodeString Original Unicode string.
433 @param StrBufferLen On input: Length in bytes of buffer to hold the Unicode string.
434 Includes tailing '\0' character.
436 If return EFI_SUCCESS, containing length of Unicode string buffer.
437 If return EFI_BUFFER_TOO_SMALL, containg length of string buffer desired.
438 @param ConfigString Binary representation of Unicode String, <string> := (<HexCh>4)+
440 @retval EFI_SUCCESS Operation completes successfully.
441 @retval EFI_BUFFER_TOO_SMALL The string buffer is too small.
446 ConfigStringToUnicode (
447 IN OUT CHAR16
*UnicodeString
,
448 IN OUT UINTN
*StrBufferLen
,
449 IN CHAR16
*ConfigString
457 Len
= StrLen (ConfigString
) / 4;
458 BufferSize
= (Len
+ 1) * sizeof (CHAR16
);
460 if (*StrBufferLen
< BufferSize
) {
461 *StrBufferLen
= BufferSize
;
462 return EFI_BUFFER_TOO_SMALL
;
465 *StrBufferLen
= BufferSize
;
467 for (Index
= 0; Index
< Len
; Index
++) {
468 BackupChar
= ConfigString
[4];
469 ConfigString
[4] = L
'\0';
471 HexStringToBuf ((UINT8
*) UnicodeString
, &BufferSize
, ConfigString
, NULL
);
473 ConfigString
[4] = BackupChar
;
480 // Add tailing '\0' character
482 *UnicodeString
= L
'\0';
488 Convert Unicode string to binary representation Config string, e.g.
489 "ABCD" => "0041004200430044". Config string appears in <ConfigHdr> (i.e.
490 "&NAME=<string>"), or Name/Value pair in <ConfigBody> (i.e. "label=<string>").
492 @param ConfigString Binary representation of Unicode String, <string> := (<HexCh>4)+
493 @param StrBufferLen On input: Length in bytes of buffer to hold the Unicode string.
494 Includes tailing '\0' character.
496 If return EFI_SUCCESS, containing length of Unicode string buffer.
497 If return EFI_BUFFER_TOO_SMALL, containg length of string buffer desired.
498 @param UnicodeString Original Unicode string.
500 @retval EFI_SUCCESS Operation completes successfully.
501 @retval EFI_BUFFER_TOO_SMALL The string buffer is too small.
506 UnicodeToConfigString (
507 IN OUT CHAR16
*ConfigString
,
508 IN OUT UINTN
*StrBufferLen
,
509 IN CHAR16
*UnicodeString
517 Len
= StrLen (UnicodeString
);
518 BufferSize
= (Len
* 4 + 1) * sizeof (CHAR16
);
520 if (*StrBufferLen
< BufferSize
) {
521 *StrBufferLen
= BufferSize
;
522 return EFI_BUFFER_TOO_SMALL
;
525 *StrBufferLen
= BufferSize
;
526 String
= ConfigString
;
528 for (Index
= 0; Index
< Len
; Index
++) {
529 BufToHexString (ConfigString
, &BufferSize
, (UINT8
*) UnicodeString
, 2);
536 // Add tailing '\0' character
538 *ConfigString
= L
'\0';
541 // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.
548 Construct <ConfigHdr> using routing information GUID/NAME/PATH.
550 @param ConfigHdr Pointer to the ConfigHdr string.
551 @param StrBufferLen On input: Length in bytes of buffer to hold the
552 ConfigHdr string. Includes tailing '\0' character.
553 On output: If return EFI_SUCCESS, containing
554 length of ConfigHdr string buffer. If return
555 EFI_BUFFER_TOO_SMALL, containg length of string
557 @param Guid Routing information: GUID.
558 @param Name Routing information: NAME.
559 @param DriverHandle Driver handle which contains the routing
562 @retval EFI_SUCCESS Operation completes successfully.
563 @retval EFI_BUFFER_TOO_SMALL The ConfigHdr string buffer is too small.
569 IN OUT CHAR16
*ConfigHdr
,
570 IN OUT UINTN
*StrBufferLen
,
571 IN CONST EFI_GUID
*Guid
,
572 IN CHAR16
*Name
, OPTIONAL
573 IN EFI_HANDLE
*DriverHandle
578 UINTN DevicePathSize
;
581 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
585 // There will be no "NAME" in <ConfigHdr> for Name/Value storage
590 // For buffer storage
592 NameStrLen
= StrLen (Name
);
596 // Retrieve DevicePath Protocol associated with this HiiPackageList
598 Status
= gBS
->HandleProtocol (
600 &gEfiDevicePathProtocolGuid
,
601 (VOID
**) &DevicePath
603 if (EFI_ERROR (Status
)) {
607 DevicePathSize
= GetDevicePathSize (DevicePath
);
610 // GUID=<HexCh>32&NAME=<Char>NameStrLen&PATH=<HexChar>DevicePathStrLen <NULL>
611 // | 5 | 32 | 6 | NameStrLen*4 | 6 | DevicePathStrLen | 1 |
613 BufferSize
= (5 + 32 + 6 + NameStrLen
* 4 + 6 + DevicePathSize
* 2 + 1) * sizeof (CHAR16
);
614 if (*StrBufferLen
< BufferSize
) {
615 *StrBufferLen
= BufferSize
;
616 return EFI_BUFFER_TOO_SMALL
;
619 *StrBufferLen
= BufferSize
;
623 StrCpy (StrPtr
, L
"GUID=");
625 BufInReverseOrderToHexString (StrPtr
, (UINT8
*) Guid
, sizeof (EFI_GUID
));
629 // Convert name string, e.g. name "ABCD" => "&NAME=0041004200430044"
631 StrCpy (StrPtr
, L
"&NAME=");
634 BufferSize
= (NameStrLen
* 4 + 1) * sizeof (CHAR16
);
635 UnicodeToConfigString (StrPtr
, &BufferSize
, Name
);
636 StrPtr
+= (NameStrLen
* 4);
639 StrCpy (StrPtr
, L
"&PATH=");
641 BufInReverseOrderToHexString (StrPtr
, (UINT8
*) DevicePath
, DevicePathSize
);
648 Search BlockName "&OFFSET=Offset&WIDTH=Width" in a string.
650 @param String The string to be searched in.
651 @param Offset Offset in BlockName.
652 @param Width Width in BlockName.
654 @retval TRUE Block name found.
655 @retval FALSE Block name not found.
661 IN OUT CHAR16
*String
,
669 UINTN ConvertedStrLen
;
671 while ((String
= StrStr (String
, L
"&OFFSET=")) != NULL
) {
678 BufferSize
= sizeof (UINTN
);
679 Status
= HexStringToBuf ((UINT8
*) &Data
, &BufferSize
, String
, &ConvertedStrLen
);
680 if (EFI_ERROR (Status
)) {
683 String
= String
+ ConvertedStrLen
;
685 if (Data
!= Offset
) {
689 if (StrnCmp (String
, L
"&WIDTH=", 7) != 0) {
695 BufferSize
= sizeof (UINTN
);
696 Status
= HexStringToBuf ((UINT8
*) &Data
, &BufferSize
, String
, &ConvertedStrLen
);
697 if (EFI_ERROR (Status
)) {
704 String
= String
+ ConvertedStrLen
;
712 This routine is invoked by ConfigAccess.Callback() to retrived uncommitted data from Form Browser.
714 @param VariableGuid An optional field to indicate the target variable
716 @param VariableName An optional field to indicate the target
717 human-readable variable name.
718 @param BufferSize On input: Length in bytes of buffer to hold
719 retrived data. On output: If return
720 EFI_BUFFER_TOO_SMALL, containg length of buffer
722 @param Buffer Buffer to hold retrived data.
724 @retval EFI_SUCCESS Operation completes successfully.
725 @retval EFI_BUFFER_TOO_SMALL The intput buffer is too small.
726 @retval EFI_OUT_OF_RESOURCES There is no enough available memory space.
732 IN CONST EFI_GUID
*VariableGuid
, OPTIONAL
733 IN CONST CHAR16
*VariableName
, OPTIONAL
734 IN OUT UINTN
*BufferSize
,
739 CONST CHAR16
*ConfigHdr
;
747 // Locate protocols for use
749 Status
= LocateFormBrowser2Protocols ();
750 if (EFI_ERROR (Status
)) {
755 // Retrive formset storage data from Form Browser
757 ConfigHdr
= mFakeConfigHdr
;
758 HeaderLen
= StrLen (ConfigHdr
);
761 // First try allocate 0x4000 buffer for the formet storage data.
764 ConfigResp
= AllocateZeroPool (BufferLen
+ HeaderLen
);
765 if (ConfigResp
== NULL
) {
769 StringPtr
= ConfigResp
+ HeaderLen
;
773 Status
= mFormBrowser2
->BrowserCallback (
781 if (Status
== EFI_BUFFER_TOO_SMALL
) {
782 if (ConfigResp
!= NULL
) {
783 FreePool (ConfigResp
);
786 ConfigResp
= AllocateZeroPool (BufferLen
+ HeaderLen
);
787 if (ConfigResp
== NULL
) {
788 return EFI_OUT_OF_RESOURCES
;
791 StringPtr
= ConfigResp
+ HeaderLen
;
795 Status
= mFormBrowser2
->BrowserCallback (
804 if (EFI_ERROR (Status
)) {
805 FreePool (ConfigResp
);
808 CopyMem (ConfigResp
, ConfigHdr
, HeaderLen
* sizeof (UINT16
));
811 // Convert <ConfigResp> to buffer data
813 Status
= mHiiConfigRouting
->ConfigToBlock (
820 FreePool (ConfigResp
);
827 This routine is invoked by ConfigAccess.Callback() to update uncommitted data of Form Browser.
829 @param VariableGuid An optional field to indicate the target variable
831 @param VariableName An optional field to indicate the target
832 human-readable variable name.
833 @param BufferSize Length in bytes of buffer to hold retrived data.
834 @param Buffer Buffer to hold retrived data.
835 @param RequestElement An optional field to specify which part of the
836 buffer data will be send back to Browser. If NULL,
837 the whole buffer of data will be committed to
838 Browser. <RequestElement> ::=
839 &OFFSET=<Number>&WIDTH=<Number>*
841 @retval EFI_SUCCESS Operation completes successfully.
842 @retval EFI_OUT_OF_RESOURCES There is no enough available memory space.
843 @retval Other Updating Browser uncommitted data failed.
849 IN CONST EFI_GUID
*VariableGuid
, OPTIONAL
850 IN CONST CHAR16
*VariableName
, OPTIONAL
852 IN CONST UINT8
*Buffer
,
853 IN CONST CHAR16
*RequestElement OPTIONAL
857 CONST CHAR16
*ConfigHdr
;
863 CHAR16 BlockName
[33];
864 CHAR16
*ConfigRequest
;
865 CONST CHAR16
*Request
;
868 // Locate protocols for use
870 Status
= LocateFormBrowser2Protocols ();
871 if (EFI_ERROR (Status
)) {
876 // Prepare <ConfigRequest>
878 ConfigHdr
= mFakeConfigHdr
;
879 HeaderLen
= StrLen (ConfigHdr
);
881 if (RequestElement
== NULL
) {
883 // RequestElement not specified, use "&OFFSET=0&WIDTH=<BufferSize>" as <BlockName>
885 BlockName
[0] = L
'\0';
886 StrCpy (BlockName
, L
"&OFFSET=0&WIDTH=");
889 // String lenghth of L"&OFFSET=0&WIDTH=" is 16
891 StringPtr
= BlockName
+ 16;
892 BufferLen
= sizeof (BlockName
) - (16 * sizeof (CHAR16
));
893 BufToHexString (StringPtr
, &BufferLen
, (UINT8
*) &BufferSize
, sizeof (UINTN
));
897 Request
= RequestElement
;
900 BufferLen
= HeaderLen
* sizeof (CHAR16
) + StrSize (Request
);
901 ConfigRequest
= AllocateZeroPool (BufferLen
);
902 if (ConfigRequest
== NULL
) {
903 return EFI_OUT_OF_RESOURCES
;
906 CopyMem (ConfigRequest
, ConfigHdr
, HeaderLen
* sizeof (CHAR16
));
907 StringPtr
= ConfigRequest
+ HeaderLen
;
908 StrCpy (StringPtr
, Request
);
911 // Convert buffer to <ConfigResp>
913 Status
= mHiiConfigRouting
->BlockToConfig (
921 if (EFI_ERROR (Status
)) {
922 FreePool (ConfigRequest
);
927 // Skip <ConfigHdr> and '&'
929 StringPtr
= ConfigResp
+ HeaderLen
+ 1;
932 // Change uncommitted data in Browser
934 Status
= mFormBrowser2
->BrowserCallback (
942 FreePool (ConfigRequest
);