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