]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/UefiIfrSupportLib/UefiIfrForm.c
1) StrGather (Build.exe) in compatible mode add a Framework Package Header to replace...
[mirror_edk2.git] / MdeModulePkg / Library / UefiIfrSupportLib / UefiIfrForm.c
1 /** @file
2 Utility functions which helps in opcode creation, HII configuration string manipulations,
3 pop up window creations, setup browser persistence data set and get.
4
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
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
16 #include "UefiIfrLibraryInternal.h"
17
18 CONST EFI_FORM_BROWSER2_PROTOCOL *mFormBrowser2 = NULL;
19 CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *mIfrSupportLibHiiConfigRouting = NULL;
20
21 /**
22 This function locate FormBrowser2 protocols for later usage.
23
24 @return Status the status to locate protocol.
25 **/
26 EFI_STATUS
27 LocateFormBrowser2Protocols (
28 VOID
29 )
30 {
31 EFI_STATUS Status;
32 //
33 // Locate protocols for later usage
34 //
35 if (mFormBrowser2 == NULL) {
36 Status = gBS->LocateProtocol (&gEfiFormBrowser2ProtocolGuid, NULL, (VOID **) &mFormBrowser2);
37 if (EFI_ERROR (Status)) {
38 return Status;
39 }
40 }
41
42 if (mIfrSupportLibHiiConfigRouting == NULL) {
43 Status = gBS->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid, NULL, (VOID **) &mIfrSupportLibHiiConfigRouting);
44 if (EFI_ERROR (Status)) {
45 return Status;
46 }
47 }
48
49 return EFI_SUCCESS;
50 }
51
52 //
53 // Fake <ConfigHdr>
54 //
55 GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT16 mFakeConfigHdr[] = L"GUID=00000000000000000000000000000000&NAME=0000&PATH=0";
56
57 /**
58 Draw a dialog and return the selected key.
59
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 The first String to be displayed in the Pop-Up.
63 @param Marker A series of (quantity == NumberOfLines - 1) text
64 strings which will be used to construct the dialog
65 box
66
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.
70
71 **/
72 EFI_STATUS
73 EFIAPI
74 IfrLibCreatePopUp2 (
75 IN UINTN NumberOfLines,
76 OUT EFI_INPUT_KEY *KeyValue,
77 IN CHAR16 *String,
78 IN VA_LIST Marker
79 )
80 {
81 UINTN Index;
82 UINTN Count;
83 UINTN Start;
84 UINTN Top;
85 CHAR16 *StringPtr;
86 UINTN LeftColumn;
87 UINTN RightColumn;
88 UINTN TopRow;
89 UINTN BottomRow;
90 UINTN DimensionsWidth;
91 UINTN DimensionsHeight;
92 EFI_INPUT_KEY Key;
93 UINTN LargestString;
94 CHAR16 *StackString;
95 EFI_STATUS Status;
96 UINTN StringLen;
97 CHAR16 *LineBuffer;
98 CHAR16 **StringArray;
99 EFI_EVENT TimerEvent;
100 EFI_EVENT WaitList[2];
101 UINTN CurrentAttribute;
102 EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ConOut;
103
104 if ((KeyValue == NULL) || (String == NULL)) {
105 return EFI_INVALID_PARAMETER;
106 }
107
108 TopRow = 0;
109 BottomRow = 0;
110 LeftColumn = 0;
111 RightColumn = 0;
112
113 ConOut = gST->ConOut;
114 ConOut->QueryMode (ConOut, ConOut->Mode->Mode, &RightColumn, &BottomRow);
115
116 DimensionsWidth = RightColumn - LeftColumn;
117 DimensionsHeight = BottomRow - TopRow;
118
119 CurrentAttribute = ConOut->Mode->Attribute;
120
121 LineBuffer = AllocateZeroPool (DimensionsWidth * sizeof (CHAR16));
122 if (LineBuffer == NULL) {
123 return EFI_OUT_OF_RESOURCES;
124 }
125
126 //
127 // Determine the largest string in the dialog box
128 // Notice we are starting with 1 since String is the first string
129 //
130 StringArray = AllocateZeroPool (NumberOfLines * sizeof (CHAR16 *));
131 if (StringArray == NULL) {
132 FreePool (LineBuffer);
133 return EFI_OUT_OF_RESOURCES;
134 }
135 LargestString = StrLen (String);
136 StringArray[0] = String;
137
138 for (Index = 1; Index < NumberOfLines; Index++) {
139 StackString = VA_ARG (Marker, CHAR16 *);
140
141 if (StackString == NULL) {
142 FreePool (LineBuffer);
143 FreePool (StringArray);
144 return EFI_INVALID_PARAMETER;
145 }
146
147 StringArray[Index] = StackString;
148 StringLen = StrLen (StackString);
149 if (StringLen > LargestString) {
150 LargestString = StringLen;
151 }
152 }
153
154 if ((LargestString + 2) > DimensionsWidth) {
155 LargestString = DimensionsWidth - 2;
156 }
157
158 //
159 // Subtract the PopUp width from total Columns, allow for one space extra on
160 // each end plus a border.
161 //
162 Start = (DimensionsWidth - LargestString - 2) / 2 + LeftColumn + 1;
163
164 Top = ((DimensionsHeight - NumberOfLines - 2) / 2) + TopRow - 1;
165
166 //
167 // Disable cursor
168 //
169 ConOut->EnableCursor (ConOut, FALSE);
170 ConOut->SetAttribute (ConOut, EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE);
171
172 StringPtr = &LineBuffer[0];
173 *StringPtr++ = BOXDRAW_DOWN_RIGHT;
174 for (Index = 0; Index < LargestString; Index++) {
175 *StringPtr++ = BOXDRAW_HORIZONTAL;
176 }
177 *StringPtr++ = BOXDRAW_DOWN_LEFT;
178 *StringPtr = L'\0';
179
180 ConOut->SetCursorPosition (ConOut, Start, Top);
181 ConOut->OutputString (ConOut, LineBuffer);
182
183 for (Index = 0; Index < NumberOfLines; Index++) {
184 StringPtr = &LineBuffer[0];
185 *StringPtr++ = BOXDRAW_VERTICAL;
186
187 for (Count = 0; Count < LargestString; Count++) {
188 StringPtr[Count] = L' ';
189 }
190
191 StringLen = StrLen (StringArray[Index]);
192 if (StringLen > LargestString) {
193 StringLen = LargestString;
194 }
195 CopyMem (
196 StringPtr + ((LargestString - StringLen) / 2),
197 StringArray[Index],
198 StringLen * sizeof (CHAR16)
199 );
200 StringPtr += LargestString;
201
202 *StringPtr++ = BOXDRAW_VERTICAL;
203 *StringPtr = L'\0';
204
205 ConOut->SetCursorPosition (ConOut, Start, Top + 1 + Index);
206 ConOut->OutputString (ConOut, LineBuffer);
207 }
208
209 StringPtr = &LineBuffer[0];
210 *StringPtr++ = BOXDRAW_UP_RIGHT;
211 for (Index = 0; Index < LargestString; Index++) {
212 *StringPtr++ = BOXDRAW_HORIZONTAL;
213 }
214 *StringPtr++ = BOXDRAW_UP_LEFT;
215 *StringPtr = L'\0';
216
217 ConOut->SetCursorPosition (ConOut, Start, Top + NumberOfLines + 1);
218 ConOut->OutputString (ConOut, LineBuffer);
219
220 do {
221 Status = gBS->CreateEvent (EVT_TIMER, 0, NULL, NULL, &TimerEvent);
222
223 //
224 // Set a timer event of 1 second expiration
225 //
226 gBS->SetTimer (
227 TimerEvent,
228 TimerRelative,
229 10000000
230 );
231
232 //
233 // Wait for the keystroke event or the timer
234 //
235 WaitList[0] = gST->ConIn->WaitForKey;
236 WaitList[1] = TimerEvent;
237 Status = gBS->WaitForEvent (2, WaitList, &Index);
238
239 //
240 // Check for the timer expiration
241 //
242 if (!EFI_ERROR (Status) && Index == 1) {
243 Status = EFI_TIMEOUT;
244 }
245
246 gBS->CloseEvent (TimerEvent);
247 } while (Status == EFI_TIMEOUT);
248
249 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
250 CopyMem (KeyValue, &Key, sizeof (EFI_INPUT_KEY));
251
252 ConOut->SetAttribute (ConOut, CurrentAttribute);
253 ConOut->EnableCursor (ConOut, TRUE);
254
255 FreePool (LineBuffer);
256 FreePool (StringArray);
257
258 return Status;
259 }
260
261
262 /**
263 Draw a dialog and return the selected key.
264
265 @param NumberOfLines The number of lines for the dialog box
266 @param KeyValue The EFI_KEY value returned if HotKey is TRUE..
267 @param String Pointer to the first string in the list
268 @param ... A series of (quantity == NumberOfLines - 1) text
269 strings which will be used to construct the dialog
270 box
271
272 @retval EFI_SUCCESS Displayed dialog and received user interaction
273 @retval EFI_INVALID_PARAMETER One of the parameters was invalid.
274
275 **/
276 EFI_STATUS
277 EFIAPI
278 IfrLibCreatePopUp (
279 IN UINTN NumberOfLines,
280 OUT EFI_INPUT_KEY *KeyValue,
281 IN CHAR16 *String,
282 ...
283 )
284 {
285 EFI_STATUS Status;
286 VA_LIST Marker;
287
288 VA_START (Marker, String);
289
290 Status = IfrLibCreatePopUp2 (NumberOfLines, KeyValue, String, Marker);
291
292 VA_END (Marker);
293
294 return Status;
295 }
296
297 /**
298 Extract block name from the array generated by VFR compiler. The name of
299 this array is "Vfr + <StorageName> + BlockName", e.g. "VfrMyIfrNVDataBlockName".
300 Format of this array is:
301 Array length | 4-bytes
302 Offset | 2-bytes
303 Width | 2-bytes
304 Offset | 2-bytes
305 Width | 2-bytes
306 ... ...
307
308 @param Buffer Array generated by VFR compiler.
309 @param BlockName The returned <BlockName>
310
311 @retval EFI_OUT_OF_RESOURCES Run out of memory resource.
312 @retval EFI_INVALID_PARAMETER Buffer is NULL or BlockName is NULL.
313 @retval EFI_SUCCESS Operation successful.
314
315 **/
316 EFI_STATUS
317 ExtractBlockName (
318 IN UINT8 *Buffer,
319 OUT CHAR16 **BlockName
320 )
321
322 {
323 UINTN Index;
324 UINT32 Length;
325 UINT32 BlockNameNumber;
326 UINTN HexStringBufferLen;
327 CHAR16 *StringPtr;
328
329 if ((Buffer == NULL) || (BlockName == NULL)) {
330 return EFI_INVALID_PARAMETER;
331 }
332
333 //
334 // Calculate number of Offset/Width pair
335 //
336 CopyMem (&Length, Buffer, sizeof (UINT32));
337 BlockNameNumber = (Length - sizeof (UINT32)) / (sizeof (UINT16) * 2);
338
339 //
340 // <BlockName> ::= &OFFSET=1234&WIDTH=1234
341 // | 8 | 4 | 7 | 4 |
342 //
343 StringPtr = AllocateZeroPool ((BlockNameNumber * (8 + 4 + 7 + 4) + 1) * sizeof (CHAR16));
344 *BlockName = StringPtr;
345 if (StringPtr == NULL) {
346 return EFI_OUT_OF_RESOURCES;
347 }
348
349 Buffer += sizeof (UINT32);
350 for (Index = 0; Index < BlockNameNumber; Index++) {
351 StrCpy (StringPtr, L"&OFFSET=");
352 StringPtr += 8;
353
354 HexStringBufferLen = 5;
355 BufToHexString (StringPtr, &HexStringBufferLen, Buffer, sizeof (UINT16));
356 Buffer += sizeof (UINT16);
357 StringPtr += 4;
358
359 StrCpy (StringPtr, L"&WIDTH=");
360 StringPtr += 7;
361
362 HexStringBufferLen = 5;
363 BufToHexString (StringPtr, &HexStringBufferLen, Buffer, sizeof (UINT16));
364 Buffer += sizeof (UINT16);
365 StringPtr += 4;
366 }
367
368 return EFI_SUCCESS;
369 }
370
371 /**
372
373 Extract block config from the array generated by VFR compiler. The name of
374 this array is "Vfr + <StorageName> + Default<HexCh>4", e.g. "VfrMyIfrNVDataDefault0000".
375
376 @param Buffer - Array generated by VFR compiler.
377 @param BlockConfig - The returned <BlockConfig>
378
379 @retval EFI_OUT_OF_RESOURCES - Run out of memory resource.
380 @retval EFI_INVALID_PARAMETER - Buffer is NULL or BlockConfig is NULL.
381 @retval EFI_SUCCESS - Operation successful.
382
383 **/
384 EFI_STATUS
385 ExtractBlockConfig (
386 IN UINT8 *Buffer,
387 OUT CHAR16 **BlockConfig
388 )
389 {
390 UINT32 Length;
391 UINT16 Width;
392 UINTN HexStringBufferLen;
393 CHAR16 *StringPtr;
394 UINT8 *BufferEnd;
395 CHAR16 *StringEnd;
396 EFI_STATUS Status;
397
398 if ((Buffer == NULL) || (BlockConfig == NULL)) {
399 return EFI_INVALID_PARAMETER;
400 }
401
402 //
403 // Calculate length of AltResp string
404 // Format of Default value array is:
405 // Array length | 4-bytes
406 // Offset | 2-bytes
407 // Width | 2-bytes
408 // Value | Variable length
409 // Offset | 2-bytes
410 // Width | 2-bytes
411 // Value | Variable length
412 // ... ...
413 // When value is 1 byte in length, overhead of AltResp string will be maximum,
414 // BlockConfig ::= <&OFFSET=1234&WIDTH=1234&VALUE=12>+
415 // | 8 | 4 | 7 | 4 | 7 |2|
416 // so the maximum length of BlockConfig could be calculated as:
417 // (ArrayLength / 5) * (8 + 4 + 7 + 4 + 7 + 2) = ArrayLength * 6.4 < ArrayLength * 7
418 //
419 CopyMem (&Length, Buffer, sizeof (UINT32));
420 BufferEnd = Buffer + Length;
421 StringPtr = AllocatePool (Length * 7 * sizeof (CHAR16));
422 *BlockConfig = StringPtr;
423 if (StringPtr == NULL) {
424 return EFI_OUT_OF_RESOURCES;
425 }
426 StringEnd = StringPtr + (Length * 7);
427
428 Buffer += sizeof (UINT32);
429 while (Buffer < BufferEnd) {
430 StrCpy (StringPtr, L"&OFFSET=");
431 StringPtr += 8;
432
433 HexStringBufferLen = 5;
434 BufToHexString (StringPtr, &HexStringBufferLen, Buffer, sizeof (UINT16));
435 Buffer += sizeof (UINT16);
436 StringPtr += 4;
437
438 StrCpy (StringPtr, L"&WIDTH=");
439 StringPtr += 7;
440
441 HexStringBufferLen = 5;
442 BufToHexString (StringPtr, &HexStringBufferLen, Buffer, sizeof (UINT16));
443 CopyMem (&Width, Buffer, sizeof (UINT16));
444 Buffer += sizeof (UINT16);
445 StringPtr += 4;
446
447 StrCpy (StringPtr, L"&VALUE=");
448 StringPtr += 7;
449
450 HexStringBufferLen = StringEnd - StringPtr;
451 Status = BufToHexString (StringPtr, &HexStringBufferLen, Buffer, Width);
452 if (EFI_ERROR (Status)) {
453 return Status;
454 }
455 Buffer += Width;
456 StringPtr += (Width * 2);
457 }
458
459 return EFI_SUCCESS;
460 }
461
462 /**
463 Construct <ConfigAltResp> for a buffer storage.
464
465 @param ConfigRequest The Config request string. If set to NULL, all the
466 configurable elements will be extracted from BlockNameArray.
467 @param ConfigAltResp The returned <ConfigAltResp>.
468 @param Progress On return, points to a character in the Request.
469 @param Guid GUID of the buffer storage.
470 @param Name Name of the buffer storage.
471 @param DriverHandle The DriverHandle which is used to invoke HiiDatabase
472 protocol interface NewPackageList().
473 @param BufferStorage Content of the buffer storage.
474 @param BufferStorageSize Length in bytes of the buffer storage.
475 @param BlockNameArray Array generated by VFR compiler.
476 @param NumberAltCfg Number of Default value array generated by VFR compiler.
477 The sequential input parameters will be number of
478 AltCfgId and DefaultValueArray pairs. When set to 0,
479 there will be no <AltResp>.
480
481 retval EFI_OUT_OF_RESOURCES Run out of memory resource.
482 retval EFI_INVALID_PARAMETER ConfigAltResp is NULL.
483 retval EFI_SUCCESS Operation successful.
484
485 **/
486 EFI_STATUS
487 ConstructConfigAltResp (
488 IN EFI_STRING ConfigRequest, OPTIONAL
489 OUT EFI_STRING *Progress,
490 OUT EFI_STRING *ConfigAltResp,
491 IN EFI_GUID *Guid,
492 IN CHAR16 *Name,
493 IN EFI_HANDLE *DriverHandle,
494 IN VOID *BufferStorage,
495 IN UINTN BufferStorageSize,
496 IN VOID *BlockNameArray, OPTIONAL
497 IN UINTN NumberAltCfg,
498 ...
499 //IN UINT16 AltCfgId,
500 //IN VOID *DefaultValueArray,
501 )
502 {
503 EFI_STATUS Status;
504 CHAR16 *ConfigHdr;
505 CHAR16 *BlockName;
506 CHAR16 *DescHdr;
507 CHAR16 *StringPtr;
508 CHAR16 **AltCfg;
509 UINT16 AltCfgId;
510 VOID *DefaultValueArray;
511 UINTN StrBufferLen;
512 EFI_STRING ConfigResp;
513 EFI_STRING TempStr;
514 VA_LIST Args;
515 UINTN AltRespLen;
516 UINTN Index;
517 BOOLEAN NeedFreeConfigRequest;
518 EFI_HII_CONFIG_ROUTING_PROTOCOL *HiiConfigRouting;
519 UINTN Len;
520
521 if (ConfigAltResp == NULL) {
522 return EFI_INVALID_PARAMETER;
523 }
524
525 //
526 // Construct <ConfigHdr> : "GUID=...&NAME=...&PATH=..."
527 //
528 ConfigHdr = NULL;
529 StrBufferLen = 0;
530 Status = ConstructConfigHdr (
531 ConfigHdr,
532 &StrBufferLen,
533 Guid,
534 Name,
535 DriverHandle
536 );
537 if (Status == EFI_BUFFER_TOO_SMALL) {
538 ConfigHdr = AllocateZeroPool (StrBufferLen);
539 Status = ConstructConfigHdr (
540 ConfigHdr,
541 &StrBufferLen,
542 Guid,
543 Name,
544 DriverHandle
545 );
546 }
547
548 if (EFI_ERROR (Status)) {
549 return Status;
550 }
551
552 //
553 // Construct <ConfigResp>
554 //
555 NeedFreeConfigRequest = FALSE;
556 if (ConfigRequest == NULL) {
557 //
558 // If ConfigRequest is set to NULL, export all configurable elements in BlockNameArray
559 //
560 Status = ExtractBlockName (BlockNameArray, &BlockName);
561 if (EFI_ERROR (Status)) {
562 return Status;
563 }
564
565 Len = StrSize (ConfigHdr);
566 ConfigRequest = AllocateZeroPool (Len + StrSize (BlockName) - sizeof (CHAR16));
567 StrCpy (ConfigRequest, ConfigHdr);
568 StrCat (ConfigRequest, BlockName);
569 NeedFreeConfigRequest = TRUE;
570 }
571
572 Status = gBS->LocateProtocol (&gEfiHiiConfigRoutingProtocolGuid, NULL, (VOID **) &HiiConfigRouting);
573 if (EFI_ERROR (Status)) {
574 return Status;
575 }
576
577 Status = HiiConfigRouting->BlockToConfig (
578 HiiConfigRouting,
579 ConfigRequest,
580 BufferStorage,
581 BufferStorageSize,
582 &ConfigResp,
583 (Progress == NULL) ? &TempStr : Progress
584 );
585 if (EFI_ERROR (Status)) {
586 return Status;
587 }
588
589 //
590 // Construct <AltResp>
591 //
592 DescHdr = AllocateZeroPool (NumberAltCfg * 16 * sizeof (CHAR16));
593 StringPtr = DescHdr;
594 AltCfg = AllocateZeroPool (NumberAltCfg * sizeof (CHAR16 *));
595 AltRespLen = 0;
596 VA_START (Args, NumberAltCfg);
597 for (Index = 0; Index < NumberAltCfg; Index++) {
598 AltCfgId = (UINT16) VA_ARG (Args, UINT16);
599 DefaultValueArray = (UINT8 *) VA_ARG (Args, VOID *);
600
601 //
602 // '&' <ConfigHdr>
603 //
604 AltRespLen += (StrLen (ConfigHdr) + 1);
605
606 StringPtr = DescHdr + Index * 16;
607 StrCpy (StringPtr, L"&ALTCFG=");
608 AltRespLen += (8 + sizeof (UINT16) * 2);
609
610 StrBufferLen = 5;
611 BufToHexString (StringPtr + 8, &StrBufferLen, (UINT8 *) &AltCfgId, sizeof (UINT16));
612 Status = ExtractBlockConfig (DefaultValueArray, &AltCfg[Index]);
613 if (EFI_ERROR (Status)) {
614 return Status;
615 }
616 AltRespLen += StrLen (AltCfg[Index]);
617 }
618 VA_END (Args);
619
620 //
621 // Generate the final <ConfigAltResp>
622 //
623 StrBufferLen = (StrLen ((CHAR16 *) ConfigResp) + AltRespLen + 1) * sizeof (CHAR16);
624 TempStr = AllocateZeroPool (StrBufferLen);
625 *ConfigAltResp = TempStr;
626 if (TempStr == NULL) {
627 return EFI_OUT_OF_RESOURCES;
628 }
629
630 //
631 // <ConfigAltResp> ::= <ConfigResp> ['&' <AltResp>]*
632 //
633 StrCpy (TempStr, ConfigResp);
634 for (Index = 0; Index < NumberAltCfg; Index++) {
635 StrCat (TempStr, L"&");
636 StrCat (TempStr, ConfigHdr);
637 StrCat (TempStr, DescHdr + Index * 16);
638 StrCat (TempStr, AltCfg[Index]);
639
640 gBS->FreePool (AltCfg[Index]);
641 }
642
643 if (NeedFreeConfigRequest) {
644 gBS->FreePool (ConfigRequest);
645 }
646 gBS->FreePool (ConfigHdr);
647 gBS->FreePool (ConfigResp);
648 gBS->FreePool (DescHdr);
649 gBS->FreePool (AltCfg);
650
651 return EFI_SUCCESS;
652 }
653
654 /**
655 Swap bytes in the buffer. This is a internal function.
656
657 @param Buffer Binary buffer.
658 @param BufferSize Size of the buffer in bytes.
659
660 @return None.
661
662 **/
663 VOID
664 SwapBuffer (
665 IN OUT UINT8 *Buffer,
666 IN UINTN BufferSize
667 )
668 {
669 UINTN Index;
670 UINT8 Temp;
671 UINTN SwapCount;
672
673 SwapCount = BufferSize / 2;
674 for (Index = 0; Index < SwapCount; Index++) {
675 Temp = Buffer[Index];
676 Buffer[Index] = Buffer[BufferSize - 1 - Index];
677 Buffer[BufferSize - 1 - Index] = Temp;
678 }
679 }
680
681 /**
682 Converts the unicode character of the string from uppercase to lowercase.
683 This is a internal function.
684
685 @param Str String to be converted
686
687 **/
688 VOID
689 EFIAPI
690 ToLower (
691 IN OUT CHAR16 *Str
692 )
693 {
694 CHAR16 *Ptr;
695
696 for (Ptr = Str; *Ptr != L'\0'; Ptr++) {
697 if (*Ptr >= L'A' && *Ptr <= L'Z') {
698 *Ptr = (CHAR16) (*Ptr - L'A' + L'a');
699 }
700 }
701 }
702
703
704 /**
705 Converts binary buffer to Unicode string in reversed byte order from BufToHexString().
706
707 @param Str String for output
708 @param Buffer Binary buffer.
709 @param BufferSize Size of the buffer in bytes.
710
711 @retval EFI_SUCCESS The function completed successfully.
712 @retval EFI_OUT_OF_RESOURCES There is no enough available memory space.
713
714 **/
715 EFI_STATUS
716 EFIAPI
717 BufInReverseOrderToHexString (
718 IN OUT CHAR16 *Str,
719 IN UINT8 *Buffer,
720 IN UINTN BufferSize
721 )
722 {
723 EFI_STATUS Status;
724 UINT8 *NewBuffer;
725 UINTN StrBufferLen;
726
727 NewBuffer = AllocateCopyPool (BufferSize, Buffer);
728 if (NewBuffer == NULL) {
729 return EFI_OUT_OF_RESOURCES;
730 }
731 SwapBuffer (NewBuffer, BufferSize);
732
733 StrBufferLen = BufferSize * sizeof (CHAR16) + 1;
734 Status = BufToHexString (Str, &StrBufferLen, NewBuffer, BufferSize);
735
736 FreePool (NewBuffer);
737 //
738 // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.
739 //
740 ToLower (Str);
741
742 return Status;
743 }
744
745
746 /**
747 Converts Hex String to binary buffer in reversed byte order from HexStringToBuf().
748
749 @param Buffer Pointer to buffer that receives the data.
750 @param BufferSize Length in bytes of the buffer to hold converted
751 data. If routine return with EFI_SUCCESS,
752 containing length of converted data. If routine
753 return with EFI_BUFFER_TOO_SMALL, containg length
754 of buffer desired.
755 @param Str String to be converted from.
756
757 @retval EFI_SUCCESS The function completed successfully.
758 @retval RETURN_BUFFER_TOO_SMALL The input BufferSize is too small to hold the output. BufferSize
759 will be updated to the size required for the converstion.
760
761 **/
762 EFI_STATUS
763 EFIAPI
764 HexStringToBufInReverseOrder (
765 IN OUT UINT8 *Buffer,
766 IN OUT UINTN *BufferSize,
767 IN CHAR16 *Str
768 )
769 {
770 EFI_STATUS Status;
771 UINTN ConvertedStrLen;
772
773 ConvertedStrLen = 0;
774 Status = HexStringToBuf (Buffer, BufferSize, Str, &ConvertedStrLen);
775 if (!EFI_ERROR (Status)) {
776 SwapBuffer (Buffer, (ConvertedStrLen + 1) / 2);
777 }
778
779 return Status;
780 }
781
782 /**
783 Convert binary representation Config string (e.g. "0041004200430044") to the
784 original string (e.g. "ABCD"). Config string appears in <ConfigHdr> (i.e.
785 "&NAME=<string>"), or Name/Value pair in <ConfigBody> (i.e. "label=<string>").
786
787 @param UnicodeString Original Unicode string.
788 @param StrBufferLen On input: Length in bytes of buffer to hold the Unicode string.
789 Includes tailing '\0' character.
790 On output:
791 If return EFI_SUCCESS, containing length of Unicode string buffer.
792 If return EFI_BUFFER_TOO_SMALL, containg length of string buffer desired.
793 @param ConfigString Binary representation of Unicode String, <string> := (<HexCh>4)+
794
795 @retval EFI_SUCCESS Operation completes successfully.
796 @retval EFI_BUFFER_TOO_SMALL The string buffer is too small.
797
798 **/
799 EFI_STATUS
800 EFIAPI
801 ConfigStringToUnicode (
802 IN OUT CHAR16 *UnicodeString,
803 IN OUT UINTN *StrBufferLen,
804 IN CHAR16 *ConfigString
805 )
806 {
807 UINTN Index;
808 UINTN Len;
809 UINTN BufferSize;
810 CHAR16 BackupChar;
811
812 Len = StrLen (ConfigString) / 4;
813 BufferSize = (Len + 1) * sizeof (CHAR16);
814
815 if (*StrBufferLen < BufferSize) {
816 *StrBufferLen = BufferSize;
817 return EFI_BUFFER_TOO_SMALL;
818 }
819
820 *StrBufferLen = BufferSize;
821
822 for (Index = 0; Index < Len; Index++) {
823 BackupChar = ConfigString[4];
824 ConfigString[4] = L'\0';
825
826 HexStringToBuf ((UINT8 *) UnicodeString, &BufferSize, ConfigString, NULL);
827
828 ConfigString[4] = BackupChar;
829
830 ConfigString += 4;
831 UnicodeString += 1;
832 }
833
834 //
835 // Add tailing '\0' character
836 //
837 *UnicodeString = L'\0';
838
839 return EFI_SUCCESS;
840 }
841
842 /**
843 Convert Unicode string to binary representation Config string, e.g.
844 "ABCD" => "0041004200430044". Config string appears in <ConfigHdr> (i.e.
845 "&NAME=<string>"), or Name/Value pair in <ConfigBody> (i.e. "label=<string>").
846
847 @param ConfigString Binary representation of Unicode String, <string> := (<HexCh>4)+
848 @param StrBufferLen On input: Length in bytes of buffer to hold the Unicode string.
849 Includes tailing '\0' character.
850 On output:
851 If return EFI_SUCCESS, containing length of Unicode string buffer.
852 If return EFI_BUFFER_TOO_SMALL, containg length of string buffer desired.
853 @param UnicodeString Original Unicode string.
854
855 @retval EFI_SUCCESS Operation completes successfully.
856 @retval EFI_BUFFER_TOO_SMALL The string buffer is too small.
857
858 **/
859 EFI_STATUS
860 EFIAPI
861 UnicodeToConfigString (
862 IN OUT CHAR16 *ConfigString,
863 IN OUT UINTN *StrBufferLen,
864 IN CHAR16 *UnicodeString
865 )
866 {
867 UINTN Index;
868 UINTN Len;
869 UINTN BufferSize;
870 CHAR16 *String;
871
872 Len = StrLen (UnicodeString);
873 BufferSize = (Len * 4 + 1) * sizeof (CHAR16);
874
875 if (*StrBufferLen < BufferSize) {
876 *StrBufferLen = BufferSize;
877 return EFI_BUFFER_TOO_SMALL;
878 }
879
880 *StrBufferLen = BufferSize;
881 String = ConfigString;
882
883 for (Index = 0; Index < Len; Index++) {
884 BufToHexString (ConfigString, &BufferSize, (UINT8 *) UnicodeString, 2);
885
886 ConfigString += 4;
887 UnicodeString += 1;
888 }
889
890 //
891 // Add tailing '\0' character
892 //
893 *ConfigString = L'\0';
894
895 //
896 // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.
897 //
898 ToLower (String);
899 return EFI_SUCCESS;
900 }
901
902 /**
903 Construct <ConfigHdr> using routing information GUID/NAME/PATH.
904
905 @param ConfigHdr Pointer to the ConfigHdr string.
906 @param StrBufferLen On input: Length in bytes of buffer to hold the
907 ConfigHdr string. Includes tailing '\0' character.
908 On output: If return EFI_SUCCESS, containing
909 length of ConfigHdr string buffer. If return
910 EFI_BUFFER_TOO_SMALL, containg length of string
911 buffer desired.
912 @param Guid Routing information: GUID.
913 @param Name Routing information: NAME.
914 @param DriverHandle Driver handle which contains the routing
915 information: PATH.
916
917 @retval EFI_SUCCESS Operation completes successfully.
918 @retval EFI_BUFFER_TOO_SMALL The ConfigHdr string buffer is too small.
919
920 **/
921 EFI_STATUS
922 EFIAPI
923 ConstructConfigHdr (
924 IN OUT CHAR16 *ConfigHdr,
925 IN OUT UINTN *StrBufferLen,
926 IN CONST EFI_GUID *Guid,
927 IN CHAR16 *Name, OPTIONAL
928 IN EFI_HANDLE *DriverHandle
929 )
930 {
931 EFI_STATUS Status;
932 UINTN NameStrLen;
933 UINTN DevicePathSize;
934 UINTN BufferSize;
935 CHAR16 *StrPtr;
936 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
937
938 if (Name == NULL) {
939 //
940 // There will be no "NAME" in <ConfigHdr> for Name/Value storage
941 //
942 NameStrLen = 0;
943 } else {
944 //
945 // For buffer storage
946 //
947 NameStrLen = StrLen (Name);
948 }
949
950 //
951 // Retrieve DevicePath Protocol associated with this HiiPackageList
952 //
953 Status = gBS->HandleProtocol (
954 DriverHandle,
955 &gEfiDevicePathProtocolGuid,
956 (VOID **) &DevicePath
957 );
958 if (EFI_ERROR (Status)) {
959 return Status;
960 }
961
962 DevicePathSize = GetDevicePathSize (DevicePath);
963
964 //
965 // GUID=<HexCh>32&NAME=<Char>NameStrLen&PATH=<HexChar>DevicePathStrLen <NULL>
966 // | 5 | 32 | 6 | NameStrLen*4 | 6 | DevicePathStrLen | 1 |
967 //
968 BufferSize = (5 + 32 + 6 + NameStrLen * 4 + 6 + DevicePathSize * 2 + 1) * sizeof (CHAR16);
969 if (*StrBufferLen < BufferSize) {
970 *StrBufferLen = BufferSize;
971 return EFI_BUFFER_TOO_SMALL;
972 }
973
974 *StrBufferLen = BufferSize;
975
976 StrPtr = ConfigHdr;
977
978 StrCpy (StrPtr, L"GUID=");
979 StrPtr += 5;
980 BufInReverseOrderToHexString (StrPtr, (UINT8 *) Guid, sizeof (EFI_GUID));
981 StrPtr += 32;
982
983 //
984 // Convert name string, e.g. name "ABCD" => "&NAME=0041004200430044"
985 //
986 StrCpy (StrPtr, L"&NAME=");
987 StrPtr += 6;
988 if (Name != NULL) {
989 BufferSize = (NameStrLen * 4 + 1) * sizeof (CHAR16);
990 UnicodeToConfigString (StrPtr, &BufferSize, Name);
991 StrPtr += (NameStrLen * 4);
992 }
993
994 StrCpy (StrPtr, L"&PATH=");
995 StrPtr += 6;
996 BufInReverseOrderToHexString (StrPtr, (UINT8 *) DevicePath, DevicePathSize);
997
998 return EFI_SUCCESS;
999 }
1000
1001 /**
1002 Determines if the Routing data (Guid and Name) is correct in <ConfigHdr>.
1003
1004 @param ConfigString Either <ConfigRequest> or <ConfigResp>.
1005 @param StorageGuid GUID of the storage.
1006 @param StorageName Name of the stoarge.
1007
1008 @retval TRUE Routing information is correct in ConfigString.
1009 @retval FALSE Routing information is incorrect in ConfigString.
1010
1011 **/
1012 BOOLEAN
1013 IsConfigHdrMatch (
1014 IN EFI_STRING ConfigString,
1015 IN EFI_GUID *StorageGuid, OPTIONAL
1016 IN CHAR16 *StorageName OPTIONAL
1017 )
1018 {
1019 EFI_STATUS Status;
1020 BOOLEAN Match;
1021 EFI_GUID Guid;
1022 CHAR16 *Name;
1023 CHAR16 *StrPtr;
1024 UINTN BufferSize;
1025
1026 //
1027 // <ConfigHdr> ::=
1028 // GUID=<HexCh>32&NAME=<Char>NameStrLen&PATH=<HexChar>DevicePathStrLen <NULL>
1029 // | 5 | 32 | 6 | NameStrLen*4 | 6 | DevicePathStrLen | 1 |
1030 //
1031 if (StrLen (ConfigString) <= (5 + 32 + 6)) {
1032 return FALSE;
1033 }
1034
1035 //
1036 // Compare GUID
1037 //
1038 if (StorageGuid != NULL) {
1039
1040 StrPtr = ConfigString + 5 + 32;
1041 if (*StrPtr != L'&') {
1042 return FALSE;
1043 }
1044 *StrPtr = L'\0';
1045
1046 BufferSize = sizeof (EFI_GUID);
1047 Status = HexStringToBufInReverseOrder (
1048 (UINT8 *) &Guid,
1049 &BufferSize,
1050 ConfigString + 5
1051 );
1052 *StrPtr = L'&';
1053
1054 if (EFI_ERROR (Status)) {
1055 return FALSE;
1056 }
1057
1058 if (!CompareGuid (&Guid, StorageGuid)) {
1059 return FALSE;
1060 }
1061 }
1062
1063 //
1064 // Compare Name
1065 //
1066 Match = TRUE;
1067 if (StorageName != NULL) {
1068 StrPtr = ConfigString + 5 + 32 + 6;
1069 while (*StrPtr != L'\0' && *StrPtr != L'&') {
1070 StrPtr++;
1071 }
1072 if (*StrPtr != L'&') {
1073 return FALSE;
1074 }
1075
1076 *StrPtr = L'\0';
1077 BufferSize = (((UINTN) StrPtr) - ((UINTN) &ConfigString[5 + 32 + 6])) / 4 + sizeof (CHAR16);
1078 Name = AllocatePool (BufferSize);
1079 ASSERT (Name != NULL);
1080 Status = ConfigStringToUnicode (
1081 Name,
1082 &BufferSize,
1083 ConfigString + 5 + 32 + 6
1084 );
1085 *StrPtr = L'&';
1086
1087 if (EFI_ERROR (Status) || (StrCmp (Name, StorageName) != 0)) {
1088 Match = FALSE;
1089 }
1090 gBS->FreePool (Name);
1091 }
1092
1093 return Match;
1094 }
1095
1096 /**
1097 Search BlockName "&OFFSET=Offset&WIDTH=Width" in a string.
1098
1099 @param String The string to be searched in.
1100 @param Offset Offset in BlockName.
1101 @param Width Width in BlockName.
1102
1103 @retval TRUE Block name found.
1104 @retval FALSE Block name not found.
1105
1106 **/
1107 BOOLEAN
1108 EFIAPI
1109 FindBlockName (
1110 IN OUT CHAR16 *String,
1111 IN UINTN Offset,
1112 IN UINTN Width
1113 )
1114 {
1115 EFI_STATUS Status;
1116 UINTN Data;
1117 UINTN BufferSize;
1118 UINTN ConvertedStrLen;
1119
1120 while ((String = StrStr (String, L"&OFFSET=")) != NULL) {
1121 //
1122 // Skip '&OFFSET='
1123 //
1124 String = String + 8;
1125
1126 Data = 0;
1127 BufferSize = sizeof (UINTN);
1128 Status = HexStringToBuf ((UINT8 *) &Data, &BufferSize, String, &ConvertedStrLen);
1129 if (EFI_ERROR (Status)) {
1130 return FALSE;
1131 }
1132 String = String + ConvertedStrLen;
1133
1134 if (Data != Offset) {
1135 continue;
1136 }
1137
1138 if (StrnCmp (String, L"&WIDTH=", 7) != 0) {
1139 return FALSE;
1140 }
1141 String = String + 7;
1142
1143 Data = 0;
1144 BufferSize = sizeof (UINTN);
1145 Status = HexStringToBuf ((UINT8 *) &Data, &BufferSize, String, &ConvertedStrLen);
1146 if (EFI_ERROR (Status)) {
1147 return FALSE;
1148 }
1149 if (Data == Width) {
1150 return TRUE;
1151 }
1152
1153 String = String + ConvertedStrLen;
1154 }
1155
1156 return FALSE;
1157 }
1158
1159
1160 /**
1161 This routine is invoked by ConfigAccess.Callback() to retrived uncommitted data from Form Browser.
1162
1163 @param VariableGuid An optional field to indicate the target variable
1164 GUID name to use.
1165 @param VariableName An optional field to indicate the target
1166 human-readable variable name.
1167 @param BufferSize On input: Length in bytes of buffer to hold
1168 retrived data. On output: If return
1169 EFI_BUFFER_TOO_SMALL, containg length of buffer
1170 desired.
1171 @param Buffer Buffer to hold retrived data.
1172
1173 @retval EFI_SUCCESS Operation completes successfully.
1174 @retval EFI_BUFFER_TOO_SMALL The intput buffer is too small.
1175 @retval EFI_OUT_OF_RESOURCES There is no enough available memory space.
1176
1177 **/
1178 EFI_STATUS
1179 EFIAPI
1180 GetBrowserData (
1181 IN CONST EFI_GUID *VariableGuid, OPTIONAL
1182 IN CONST CHAR16 *VariableName, OPTIONAL
1183 IN OUT UINTN *BufferSize,
1184 IN OUT UINT8 *Buffer
1185 )
1186 {
1187 EFI_STATUS Status;
1188 CONST CHAR16 *ConfigHdr;
1189 CHAR16 *ConfigResp;
1190 CHAR16 *StringPtr;
1191 UINTN HeaderLen;
1192 UINTN BufferLen;
1193 CHAR16 *Progress;
1194
1195 //
1196 // Locate protocols for use
1197 //
1198 Status = LocateFormBrowser2Protocols ();
1199 if (EFI_ERROR (Status)) {
1200 return Status;
1201 }
1202
1203 //
1204 // Retrive formset storage data from Form Browser
1205 //
1206 ConfigHdr = mFakeConfigHdr;
1207 HeaderLen = StrLen (ConfigHdr);
1208
1209 //
1210 // First try allocate 0x4000 buffer for the formet storage data.
1211 //
1212 BufferLen = 0x4000;
1213 ConfigResp = AllocateZeroPool (BufferLen + HeaderLen);
1214 if (ConfigResp == NULL) {
1215 BufferLen = 0;
1216 }
1217
1218 StringPtr = ConfigResp + HeaderLen;
1219 *StringPtr = L'&';
1220 StringPtr++;
1221
1222 Status = mFormBrowser2->BrowserCallback (
1223 mFormBrowser2,
1224 &BufferLen,
1225 StringPtr,
1226 TRUE,
1227 VariableGuid,
1228 VariableName
1229 );
1230 if (Status == EFI_BUFFER_TOO_SMALL) {
1231 if (ConfigResp != NULL) {
1232 FreePool (ConfigResp);
1233 }
1234
1235 ConfigResp = AllocateZeroPool (BufferLen + HeaderLen);
1236 if (ConfigResp == NULL) {
1237 return EFI_OUT_OF_RESOURCES;
1238 }
1239
1240 StringPtr = ConfigResp + HeaderLen;
1241 *StringPtr = L'&';
1242 StringPtr++;
1243
1244 Status = mFormBrowser2->BrowserCallback (
1245 mFormBrowser2,
1246 &BufferLen,
1247 StringPtr,
1248 TRUE,
1249 VariableGuid,
1250 VariableName
1251 );
1252 }
1253 if (EFI_ERROR (Status)) {
1254 FreePool (ConfigResp);
1255 return Status;
1256 }
1257 CopyMem (ConfigResp, ConfigHdr, HeaderLen * sizeof (UINT16));
1258
1259 //
1260 // Convert <ConfigResp> to buffer data
1261 //
1262 Status = mIfrSupportLibHiiConfigRouting->ConfigToBlock (
1263 mIfrSupportLibHiiConfigRouting,
1264 ConfigResp,
1265 Buffer,
1266 BufferSize,
1267 &Progress
1268 );
1269 FreePool (ConfigResp);
1270
1271 return Status;
1272 }
1273
1274
1275 /**
1276 This routine is invoked by ConfigAccess.Callback() to update uncommitted data of Form Browser.
1277
1278 @param VariableGuid An optional field to indicate the target variable
1279 GUID name to use.
1280 @param VariableName An optional field to indicate the target
1281 human-readable variable name.
1282 @param BufferSize Length in bytes of buffer to hold retrived data.
1283 @param Buffer Buffer to hold retrived data.
1284 @param RequestElement An optional field to specify which part of the
1285 buffer data will be send back to Browser. If NULL,
1286 the whole buffer of data will be committed to
1287 Browser. <RequestElement> ::=
1288 &OFFSET=<Number>&WIDTH=<Number>*
1289
1290 @retval EFI_SUCCESS Operation completes successfully.
1291 @retval EFI_OUT_OF_RESOURCES There is no enough available memory space.
1292 @retval Other Updating Browser uncommitted data failed.
1293
1294 **/
1295 EFI_STATUS
1296 EFIAPI
1297 SetBrowserData (
1298 IN CONST EFI_GUID *VariableGuid, OPTIONAL
1299 IN CONST CHAR16 *VariableName, OPTIONAL
1300 IN UINTN BufferSize,
1301 IN CONST UINT8 *Buffer,
1302 IN CONST CHAR16 *RequestElement OPTIONAL
1303 )
1304 {
1305 EFI_STATUS Status;
1306 CONST CHAR16 *ConfigHdr;
1307 CHAR16 *ConfigResp;
1308 CHAR16 *StringPtr;
1309 UINTN HeaderLen;
1310 UINTN BufferLen;
1311 CHAR16 *Progress;
1312 CHAR16 BlockName[33];
1313 CHAR16 *ConfigRequest;
1314 CONST CHAR16 *Request;
1315
1316 //
1317 // Locate protocols for use
1318 //
1319 Status = LocateFormBrowser2Protocols ();
1320 if (EFI_ERROR (Status)) {
1321 return Status;
1322 }
1323
1324 //
1325 // Prepare <ConfigRequest>
1326 //
1327 ConfigHdr = mFakeConfigHdr;
1328 HeaderLen = StrLen (ConfigHdr);
1329
1330 if (RequestElement == NULL) {
1331 //
1332 // RequestElement not specified, use "&OFFSET=0&WIDTH=<BufferSize>" as <BlockName>
1333 //
1334 BlockName[0] = L'\0';
1335 StrCpy (BlockName, L"&OFFSET=0&WIDTH=");
1336
1337 //
1338 // String lenghth of L"&OFFSET=0&WIDTH=" is 16
1339 //
1340 StringPtr = BlockName + 16;
1341 BufferLen = sizeof (BlockName) - (16 * sizeof (CHAR16));
1342 BufToHexString (StringPtr, &BufferLen, (UINT8 *) &BufferSize, sizeof (UINTN));
1343
1344 Request = BlockName;
1345 } else {
1346 Request = RequestElement;
1347 }
1348
1349 BufferLen = HeaderLen * sizeof (CHAR16) + StrSize (Request);
1350 ConfigRequest = AllocateZeroPool (BufferLen);
1351 if (ConfigRequest == NULL) {
1352 return EFI_OUT_OF_RESOURCES;
1353 }
1354
1355 CopyMem (ConfigRequest, ConfigHdr, HeaderLen * sizeof (CHAR16));
1356 StringPtr = ConfigRequest + HeaderLen;
1357 StrCpy (StringPtr, Request);
1358
1359 //
1360 // Convert buffer to <ConfigResp>
1361 //
1362 Status = mIfrSupportLibHiiConfigRouting->BlockToConfig (
1363 mIfrSupportLibHiiConfigRouting,
1364 ConfigRequest,
1365 Buffer,
1366 BufferSize,
1367 &ConfigResp,
1368 &Progress
1369 );
1370 if (EFI_ERROR (Status)) {
1371 FreePool (ConfigRequest);
1372 return Status;
1373 }
1374
1375 //
1376 // Skip <ConfigHdr> and '&'
1377 //
1378 StringPtr = ConfigResp + HeaderLen + 1;
1379
1380 //
1381 // Change uncommitted data in Browser
1382 //
1383 Status = mFormBrowser2->BrowserCallback (
1384 mFormBrowser2,
1385 &BufferSize,
1386 StringPtr,
1387 FALSE,
1388 VariableGuid,
1389 VariableName
1390 );
1391 FreePool (ConfigRequest);
1392 return Status;
1393 }