]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/HiiDatabaseDxe/ConfigRouting.c
1081e759211657c7ba408b6995ca0a2938540eda
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / ConfigRouting.c
1 /** @file
2 Implementation of interfaces function for EFI_HII_CONFIG_ROUTING_PROTOCOL.
3
4 Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15
16 #include "HiiDatabase.h"
17 extern HII_DATABASE_PRIVATE_DATA mPrivate;
18
19 /**
20 Calculate the number of Unicode characters of the incoming Configuration string,
21 not including NULL terminator.
22
23 This is a internal function.
24
25 @param String String in <MultiConfigRequest> or
26 <MultiConfigResp> format.
27
28 @return The number of Unicode characters.
29
30 **/
31 UINTN
32 CalculateConfigStringLen (
33 IN EFI_STRING String
34 )
35 {
36 EFI_STRING TmpPtr;
37
38 //
39 // "GUID=" should be the first element of incoming string.
40 //
41 ASSERT (String != NULL);
42 ASSERT (StrnCmp (String, L"GUID=", StrLen (L"GUID=")) == 0);
43
44 //
45 // The beginning of next <ConfigRequest>/<ConfigResp> should be "&GUID=".
46 // Will meet '\0' if there is only one <ConfigRequest>/<ConfigResp>.
47 //
48 TmpPtr = StrStr (String, L"&GUID=");
49 if (TmpPtr == NULL) {
50 return StrLen (String);
51 }
52
53 return (TmpPtr - String);
54 }
55
56
57 /**
58 Convert the hex UNICODE %02x encoding of a UEFI device path to binary
59 from <PathHdr> of <ConfigHdr>.
60
61 This is a internal function.
62
63 @param String UEFI configuration string
64 @param DevicePathData Binary of a UEFI device path.
65
66 @retval EFI_NOT_FOUND The device path is not invalid.
67 @retval EFI_INVALID_PARAMETER Any incoming parameter is invalid.
68 @retval EFI_OUT_OF_RESOURCES Lake of resources to store neccesary structures.
69 @retval EFI_SUCCESS The device path is retrieved and translated to
70 binary format.
71
72 **/
73 EFI_STATUS
74 GetDevicePath (
75 IN EFI_STRING String,
76 OUT UINT8 **DevicePathData
77 )
78 {
79 UINTN Length;
80 EFI_STRING PathHdr;
81 UINT8 *DevicePathBuffer;
82 CHAR16 TemStr[2];
83 UINTN Index;
84 UINT8 DigitUint8;
85 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
86
87
88 if (String == NULL || DevicePathData == NULL) {
89 return EFI_INVALID_PARAMETER;
90 }
91
92 //
93 // Find the 'PATH=' of <PathHdr> and skip it.
94 //
95 for (; (*String != 0 && StrnCmp (String, L"PATH=", StrLen (L"PATH=")) != 0); String++);
96 if (*String == 0) {
97 return EFI_INVALID_PARAMETER;
98 }
99 //
100 // Check whether path data does exist.
101 //
102 String += StrLen (L"PATH=");
103 if (*String == 0) {
104 return EFI_INVALID_PARAMETER;
105 }
106 PathHdr = String;
107
108 //
109 // The content between 'PATH=' of <ConfigHdr> and '&' of next element
110 // or '\0' (end of configuration string) is the UNICODE %02x bytes encoding
111 // of UEFI device path.
112 //
113 for (Length = 0; *String != 0 && *String != L'&'; String++, Length++);
114 //
115 // Check DevicePath Length
116 //
117 if (((Length + 1) / 2) < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
118 return EFI_NOT_FOUND;
119 }
120
121 //
122 // The data in <PathHdr> is encoded as hex UNICODE %02x bytes in the same order
123 // as the device path resides in RAM memory.
124 // Translate the data into binary.
125 //
126 DevicePathBuffer = (UINT8 *) AllocateZeroPool ((Length + 1) / 2);
127 if (DevicePathBuffer == NULL) {
128 return EFI_OUT_OF_RESOURCES;
129 }
130
131 //
132 // Convert DevicePath
133 //
134 ZeroMem (TemStr, sizeof (TemStr));
135 for (Index = 0; Index < Length; Index ++) {
136 TemStr[0] = PathHdr[Index];
137 DigitUint8 = (UINT8) StrHexToUint64 (TemStr);
138 if ((Index & 1) == 0) {
139 DevicePathBuffer [Index/2] = DigitUint8;
140 } else {
141 DevicePathBuffer [Index/2] = (UINT8) ((DevicePathBuffer [Index/2] << 4) + DigitUint8);
142 }
143 }
144
145 //
146 // Validate DevicePath
147 //
148 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) DevicePathBuffer;
149 while (!IsDevicePathEnd (DevicePath)) {
150 if ((DevicePath->Type == 0) || (DevicePath->SubType == 0) || (DevicePathNodeLength (DevicePath) < sizeof (EFI_DEVICE_PATH_PROTOCOL))) {
151 //
152 // Invalid device path
153 //
154 FreePool (DevicePathBuffer);
155 return EFI_NOT_FOUND;
156 }
157 DevicePath = NextDevicePathNode (DevicePath);
158 }
159
160 //
161 // return the device path
162 //
163 *DevicePathData = DevicePathBuffer;
164 return EFI_SUCCESS;
165 }
166
167 /**
168 Converts the unicode character of the string from uppercase to lowercase.
169 This is a internal function.
170
171 @param ConfigString String to be converted
172
173 **/
174 VOID
175 EFIAPI
176 HiiToLower (
177 IN EFI_STRING ConfigString
178 )
179 {
180 EFI_STRING String;
181 BOOLEAN Lower;
182
183 ASSERT (ConfigString != NULL);
184
185 //
186 // Convert all hex digits in range [A-F] in the configuration header to [a-f]
187 //
188 for (String = ConfigString, Lower = FALSE; *String != L'\0'; String++) {
189 if (*String == L'=') {
190 Lower = TRUE;
191 } else if (*String == L'&') {
192 Lower = FALSE;
193 } else if (Lower && *String >= L'A' && *String <= L'F') {
194 *String = (CHAR16) (*String - L'A' + L'a');
195 }
196 }
197
198 return;
199 }
200
201 /**
202 Generate a sub string then output it.
203
204 This is a internal function.
205
206 @param String A constant string which is the prefix of the to be
207 generated string, e.g. GUID=
208
209 @param BufferLen The length of the Buffer in bytes.
210
211 @param Buffer Points to a buffer which will be converted to be the
212 content of the generated string.
213
214 @param Flag If 1, the buffer contains data for the value of GUID or PATH stored in
215 UINT8 *; if 2, the buffer contains unicode string for the value of NAME;
216 if 3, the buffer contains other data.
217
218 @param SubStr Points to the output string. It's caller's
219 responsibility to free this buffer.
220
221
222 **/
223 VOID
224 GenerateSubStr (
225 IN CONST EFI_STRING String,
226 IN UINTN BufferLen,
227 IN VOID *Buffer,
228 IN UINT8 Flag,
229 OUT EFI_STRING *SubStr
230 )
231 {
232 UINTN Length;
233 EFI_STRING Str;
234 EFI_STRING StringHeader;
235 CHAR16 *TemString;
236 CHAR16 *TemName;
237 UINT8 *TemBuffer;
238 UINTN Index;
239
240 ASSERT (String != NULL && SubStr != NULL);
241
242 if (Buffer == NULL) {
243 *SubStr = AllocateCopyPool (StrSize (String), String);
244 ASSERT (*SubStr != NULL);
245 return;
246 }
247
248 //
249 // Header + Data + '&' + '\0'
250 //
251 Length = StrLen (String) + BufferLen * 2 + 1 + 1;
252 Str = AllocateZeroPool (Length * sizeof (CHAR16));
253 ASSERT (Str != NULL);
254
255 StrCpyS (Str, Length, String);
256
257 StringHeader = Str + StrLen (String);
258 TemString = (CHAR16 *) StringHeader;
259
260 switch (Flag) {
261 case 1:
262 //
263 // Convert Buffer to Hex String in reverse order
264 //
265 TemBuffer = ((UINT8 *) Buffer);
266 for (Index = 0; Index < BufferLen; Index ++, TemBuffer ++) {
267 TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemBuffer, 2);
268 }
269 break;
270 case 2:
271 //
272 // Check buffer is enough
273 //
274 TemName = (CHAR16 *) Buffer;
275 ASSERT ((BufferLen * 2 + 1) >= (StrLen (TemName) * 4 + 1));
276 //
277 // Convert Unicode String to Config String, e.g. "ABCD" => "0041004200430044"
278 //
279 for (; *TemName != L'\0'; TemName++) {
280 TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemName, 4);
281 }
282 break;
283 case 3:
284 //
285 // Convert Buffer to Hex String
286 //
287 TemBuffer = ((UINT8 *) Buffer) + BufferLen - 1;
288 for (Index = 0; Index < BufferLen; Index ++, TemBuffer --) {
289 TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemBuffer, 2);
290 }
291 break;
292 default:
293 break;
294 }
295
296 //
297 // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.
298 //
299 StrCatS (Str, Length, L"&");
300 HiiToLower (Str);
301
302 *SubStr = Str;
303 }
304
305
306 /**
307 Retrieve the <ConfigBody> from String then output it.
308
309 This is a internal function.
310
311 @param String A sub string of a configuration string in
312 <MultiConfigAltResp> format.
313 @param ConfigBody Points to the output string. It's caller's
314 responsibility to free this buffer.
315
316 @retval EFI_INVALID_PARAMETER There is no form package in current hii database.
317 @retval EFI_OUT_OF_RESOURCES Not enough memory to finish this operation.
318 @retval EFI_SUCCESS All existing storage is exported.
319
320 **/
321 EFI_STATUS
322 OutputConfigBody (
323 IN EFI_STRING String,
324 OUT EFI_STRING *ConfigBody
325 )
326 {
327 EFI_STRING TmpPtr;
328 EFI_STRING Result;
329 UINTN Length;
330
331 if (String == NULL || ConfigBody == NULL) {
332 return EFI_INVALID_PARAMETER;
333 }
334
335 //
336 // The setting information should start OFFSET, not ALTCFG.
337 //
338 if (StrnCmp (String, L"&ALTCFG=", StrLen (L"&ALTCFG=")) == 0) {
339 return EFI_INVALID_PARAMETER;
340 }
341
342 TmpPtr = StrStr (String, L"GUID=");
343 if (TmpPtr == NULL) {
344 //
345 // It is the last <ConfigResp> of the incoming configuration string.
346 //
347 Result = AllocateCopyPool (StrSize (String), String);
348 if (Result == NULL) {
349 return EFI_OUT_OF_RESOURCES;
350 } else {
351 *ConfigBody = Result;
352 return EFI_SUCCESS;
353 }
354 }
355
356 Length = TmpPtr - String;
357 if (Length == 0) {
358 return EFI_NOT_FOUND;
359 }
360 Result = AllocateCopyPool (Length * sizeof (CHAR16), String);
361 if (Result == NULL) {
362 return EFI_OUT_OF_RESOURCES;
363 }
364
365 *(Result + Length - 1) = 0;
366 *ConfigBody = Result;
367 return EFI_SUCCESS;
368 }
369
370 /**
371 Append a string to a multi-string format.
372
373 This is a internal function.
374
375 @param MultiString String in <MultiConfigRequest>,
376 <MultiConfigAltResp>, or <MultiConfigResp>. On
377 input, the buffer length of this string is
378 MAX_STRING_LENGTH. On output, the buffer length
379 might be updated.
380 @param AppendString NULL-terminated Unicode string.
381
382 @retval EFI_INVALID_PARAMETER Any incoming parameter is invalid.
383 @retval EFI_SUCCESS AppendString is append to the end of MultiString
384
385 **/
386 EFI_STATUS
387 AppendToMultiString (
388 IN OUT EFI_STRING *MultiString,
389 IN EFI_STRING AppendString
390 )
391 {
392 UINTN AppendStringSize;
393 UINTN MultiStringSize;
394 UINTN MaxLen;
395
396 if (MultiString == NULL || *MultiString == NULL || AppendString == NULL) {
397 return EFI_INVALID_PARAMETER;
398 }
399
400 AppendStringSize = StrSize (AppendString);
401 MultiStringSize = StrSize (*MultiString);
402 MaxLen = MAX_STRING_LENGTH / sizeof (CHAR16);
403
404 //
405 // Enlarge the buffer each time when length exceeds MAX_STRING_LENGTH.
406 //
407 if (MultiStringSize + AppendStringSize > MAX_STRING_LENGTH ||
408 MultiStringSize > MAX_STRING_LENGTH) {
409 *MultiString = (EFI_STRING) ReallocatePool (
410 MultiStringSize,
411 MultiStringSize + AppendStringSize,
412 (VOID *) (*MultiString)
413 );
414 MaxLen = (MultiStringSize + AppendStringSize) / sizeof (CHAR16);
415 ASSERT (*MultiString != NULL);
416 }
417 //
418 // Append the incoming string
419 //
420 StrCatS (*MultiString, MaxLen, AppendString);
421
422 return EFI_SUCCESS;
423 }
424
425
426 /**
427 Get the value of <Number> in <BlockConfig> format, i.e. the value of OFFSET
428 or WIDTH or VALUE.
429 <BlockConfig> ::= 'OFFSET='<Number>&'WIDTH='<Number>&'VALUE'=<Number>
430
431 This is a internal function.
432
433 @param StringPtr String in <BlockConfig> format and points to the
434 first character of <Number>.
435 @param Number The output value. Caller takes the responsibility
436 to free memory.
437 @param Len Length of the <Number>, in characters.
438
439 @retval EFI_OUT_OF_RESOURCES Insufficient resources to store neccessary
440 structures.
441 @retval EFI_SUCCESS Value of <Number> is outputted in Number
442 successfully.
443
444 **/
445 EFI_STATUS
446 GetValueOfNumber (
447 IN EFI_STRING StringPtr,
448 OUT UINT8 **Number,
449 OUT UINTN *Len
450 )
451 {
452 EFI_STRING TmpPtr;
453 UINTN Length;
454 EFI_STRING Str;
455 UINT8 *Buf;
456 EFI_STATUS Status;
457 UINT8 DigitUint8;
458 UINTN Index;
459 CHAR16 TemStr[2];
460
461 if (StringPtr == NULL || *StringPtr == L'\0' || Number == NULL || Len == NULL) {
462 return EFI_INVALID_PARAMETER;
463 }
464
465 Buf = NULL;
466
467 TmpPtr = StringPtr;
468 while (*StringPtr != L'\0' && *StringPtr != L'&') {
469 StringPtr++;
470 }
471 *Len = StringPtr - TmpPtr;
472 Length = *Len + 1;
473
474 Str = (EFI_STRING) AllocateZeroPool (Length * sizeof (CHAR16));
475 if (Str == NULL) {
476 Status = EFI_OUT_OF_RESOURCES;
477 goto Exit;
478 }
479 CopyMem (Str, TmpPtr, *Len * sizeof (CHAR16));
480 *(Str + *Len) = L'\0';
481
482 Length = (Length + 1) / 2;
483 Buf = (UINT8 *) AllocateZeroPool (Length);
484 if (Buf == NULL) {
485 Status = EFI_OUT_OF_RESOURCES;
486 goto Exit;
487 }
488
489 Length = *Len;
490 ZeroMem (TemStr, sizeof (TemStr));
491 for (Index = 0; Index < Length; Index ++) {
492 TemStr[0] = Str[Length - Index - 1];
493 DigitUint8 = (UINT8) StrHexToUint64 (TemStr);
494 if ((Index & 1) == 0) {
495 Buf [Index/2] = DigitUint8;
496 } else {
497 Buf [Index/2] = (UINT8) ((DigitUint8 << 4) + Buf [Index/2]);
498 }
499 }
500
501 *Number = Buf;
502 Status = EFI_SUCCESS;
503
504 Exit:
505 if (Str != NULL) {
506 FreePool (Str);
507 }
508
509 return Status;
510 }
511
512 /**
513 This function merges DefaultAltCfgResp string into AltCfgResp string for
514 the missing AltCfgId in AltCfgResq.
515
516 @param AltCfgResp Pointer to a null-terminated Unicode string in
517 <ConfigAltResp> format. The default value string
518 will be merged into it.
519 @param DefaultAltCfgResp Pointer to a null-terminated Unicode string in
520 <MultiConfigAltResp> format. The default value
521 string may contain more than one ConfigAltResp
522 string for the different varstore buffer.
523
524 @retval EFI_SUCCESS The merged string returns.
525 @retval EFI_INVALID_PARAMETER *AltCfgResp is to NULL.
526 **/
527 EFI_STATUS
528 EFIAPI
529 MergeDefaultString (
530 IN OUT EFI_STRING *AltCfgResp,
531 IN EFI_STRING DefaultAltCfgResp
532 )
533 {
534 EFI_STRING StringPtrDefault;
535 EFI_STRING StringPtrEnd;
536 CHAR16 TempChar;
537 EFI_STRING StringPtr;
538 EFI_STRING AltConfigHdr;
539 UINTN HeaderLength;
540 UINTN SizeAltCfgResp;
541 UINTN MaxLen;
542 UINTN TotalSize;
543
544 if (*AltCfgResp == NULL) {
545 return EFI_INVALID_PARAMETER;
546 }
547
548 //
549 // Get the requestr ConfigHdr
550 //
551 SizeAltCfgResp = 0;
552 StringPtr = *AltCfgResp;
553
554 //
555 // Find <ConfigHdr> GUID=...&NAME=...&PATH=...
556 //
557 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
558 return EFI_INVALID_PARAMETER;
559 }
560 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&NAME=", StrLen (L"&NAME=")) != 0) {
561 StringPtr++;
562 }
563 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&PATH=", StrLen (L"&PATH=")) != 0) {
564 StringPtr++;
565 }
566 if (*StringPtr == L'\0') {
567 return EFI_INVALID_PARAMETER;
568 }
569 StringPtr += StrLen (L"&PATH=");
570 while (*StringPtr != L'\0' && *StringPtr != L'&') {
571 StringPtr ++;
572 }
573 HeaderLength = StringPtr - *AltCfgResp;
574
575 //
576 // Construct AltConfigHdr string "&<ConfigHdr>&ALTCFG=XXXX\0"
577 // |1| StrLen (ConfigHdr) | 8 | 4 | 1 |
578 //
579 MaxLen = 1 + HeaderLength + 8 + 4 + 1;
580 AltConfigHdr = AllocateZeroPool (MaxLen * sizeof (CHAR16));
581 if (AltConfigHdr == NULL) {
582 return EFI_OUT_OF_RESOURCES;
583 }
584 StrCpyS (AltConfigHdr, MaxLen, L"&");
585 StrnCatS (AltConfigHdr, MaxLen, *AltCfgResp, HeaderLength);
586 StrCatS (AltConfigHdr, MaxLen, L"&ALTCFG=");
587 HeaderLength = StrLen (AltConfigHdr);
588
589 StringPtrDefault = StrStr (DefaultAltCfgResp, AltConfigHdr);
590 while (StringPtrDefault != NULL) {
591 //
592 // Get AltCfg Name
593 //
594 StrnCatS (AltConfigHdr, MaxLen, StringPtrDefault + HeaderLength, 4);
595 StringPtr = StrStr (*AltCfgResp, AltConfigHdr);
596
597 //
598 // Append the found default value string to the input AltCfgResp
599 //
600 if (StringPtr == NULL) {
601 StringPtrEnd = StrStr (StringPtrDefault + 1, L"&GUID");
602 SizeAltCfgResp = StrSize (*AltCfgResp);
603 TotalSize = SizeAltCfgResp + StrSize (StringPtrDefault);
604 if (StringPtrEnd == NULL) {
605 //
606 // No more default string is found.
607 //
608 *AltCfgResp = (EFI_STRING) ReallocatePool (
609 SizeAltCfgResp,
610 TotalSize,
611 (VOID *) (*AltCfgResp)
612 );
613 if (*AltCfgResp == NULL) {
614 FreePool (AltConfigHdr);
615 return EFI_OUT_OF_RESOURCES;
616 }
617 StrCatS (*AltCfgResp, TotalSize / sizeof (CHAR16), StringPtrDefault);
618 break;
619 } else {
620 TempChar = *StringPtrEnd;
621 *StringPtrEnd = L'\0';
622 *AltCfgResp = (EFI_STRING) ReallocatePool (
623 SizeAltCfgResp,
624 TotalSize,
625 (VOID *) (*AltCfgResp)
626 );
627 if (*AltCfgResp == NULL) {
628 FreePool (AltConfigHdr);
629 return EFI_OUT_OF_RESOURCES;
630 }
631 StrCatS (*AltCfgResp, TotalSize / sizeof (CHAR16), StringPtrDefault);
632 *StringPtrEnd = TempChar;
633 }
634 }
635
636 //
637 // Find next AltCfg String
638 //
639 *(AltConfigHdr + HeaderLength) = L'\0';
640 StringPtrDefault = StrStr (StringPtrDefault + 1, AltConfigHdr);
641 }
642
643 FreePool (AltConfigHdr);
644 return EFI_SUCCESS;
645 }
646
647 /**
648 This function inserts new DefaultValueData into the BlockData DefaultValue array.
649
650 @param BlockData The BlockData is updated to add new default value.
651 @param DefaultValueData The DefaultValue is added.
652
653 **/
654 VOID
655 InsertDefaultValue (
656 IN IFR_BLOCK_DATA *BlockData,
657 IN IFR_DEFAULT_DATA *DefaultValueData
658 )
659 {
660 LIST_ENTRY *Link;
661 IFR_DEFAULT_DATA *DefaultValueArray;
662 LIST_ENTRY *DefaultLink;
663
664 DefaultLink = &BlockData->DefaultValueEntry;
665
666 for (Link = DefaultLink->ForwardLink; Link != DefaultLink; Link = Link->ForwardLink) {
667 DefaultValueArray = BASE_CR (Link, IFR_DEFAULT_DATA, Entry);
668 if (DefaultValueArray->DefaultId == DefaultValueData->DefaultId) {
669 //
670 // DEFAULT_VALUE_FROM_OPCODE has high priority, DEFAULT_VALUE_FROM_DEFAULT has low priority.
671 //
672 if (DefaultValueData->Type > DefaultValueArray->Type) {
673 //
674 // Update the default value array in BlockData.
675 //
676 CopyMem (&DefaultValueArray->Value, &DefaultValueData->Value, sizeof (EFI_IFR_TYPE_VALUE));
677 DefaultValueArray->Type = DefaultValueData->Type;
678 DefaultValueArray->Cleaned = DefaultValueData->Cleaned;
679 }
680 return;
681 }
682 }
683
684 //
685 // Insert new default value data in tail.
686 //
687 DefaultValueArray = AllocateZeroPool (sizeof (IFR_DEFAULT_DATA));
688 ASSERT (DefaultValueArray != NULL);
689 CopyMem (DefaultValueArray, DefaultValueData, sizeof (IFR_DEFAULT_DATA));
690 InsertTailList (Link, &DefaultValueArray->Entry);
691 }
692
693 /**
694 This function inserts new BlockData into the block link
695
696 @param BlockLink The list entry points to block array.
697 @param BlockData The point to BlockData is added.
698
699 **/
700 VOID
701 InsertBlockData (
702 IN LIST_ENTRY *BlockLink,
703 IN IFR_BLOCK_DATA **BlockData
704 )
705 {
706 LIST_ENTRY *Link;
707 IFR_BLOCK_DATA *BlockArray;
708 IFR_BLOCK_DATA *BlockSingleData;
709
710 BlockSingleData = *BlockData;
711
712 if (BlockSingleData->Name != NULL) {
713 InsertTailList (BlockLink, &BlockSingleData->Entry);
714 return;
715 }
716
717 //
718 // Insert block data in its Offset and Width order.
719 //
720 for (Link = BlockLink->ForwardLink; Link != BlockLink; Link = Link->ForwardLink) {
721 BlockArray = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
722 if (BlockArray->Offset == BlockSingleData->Offset) {
723 if (BlockArray->Width > BlockSingleData->Width) {
724 //
725 // Insert this block data in the front of block array
726 //
727 InsertTailList (Link, &BlockSingleData->Entry);
728 return;
729 }
730
731 if (BlockArray->Width == BlockSingleData->Width) {
732 //
733 // The same block array has been added.
734 //
735 if (BlockSingleData != BlockArray) {
736 FreePool (BlockSingleData);
737 *BlockData = BlockArray;
738 }
739 return;
740 }
741 } else if (BlockArray->Offset > BlockSingleData->Offset) {
742 //
743 // Insert new block data in the front of block array
744 //
745 InsertTailList (Link, &BlockSingleData->Entry);
746 return;
747 }
748 }
749
750 //
751 // Add new block data into the tail.
752 //
753 InsertTailList (Link, &BlockSingleData->Entry);
754 }
755
756 /**
757 Retrieves a pointer to the a Null-terminated ASCII string containing the list
758 of languages that an HII handle in the HII Database supports. The returned
759 string is allocated using AllocatePool(). The caller is responsible for freeing
760 the returned string using FreePool(). The format of the returned string follows
761 the language format assumed the HII Database.
762
763 If HiiHandle is NULL, then ASSERT().
764
765 @param[in] HiiHandle A handle that was previously registered in the HII Database.
766
767 @retval NULL HiiHandle is not registered in the HII database
768 @retval NULL There are not enough resources available to retrieve the suported
769 languages.
770 @retval NULL The list of suported languages could not be retrieved.
771 @retval Other A pointer to the Null-terminated ASCII string of supported languages.
772
773 **/
774 CHAR8 *
775 GetSupportedLanguages (
776 IN EFI_HII_HANDLE HiiHandle
777 )
778 {
779 EFI_STATUS Status;
780 UINTN LanguageSize;
781 CHAR8 TempSupportedLanguages;
782 CHAR8 *SupportedLanguages;
783
784 ASSERT (HiiHandle != NULL);
785
786 //
787 // Retrieve the size required for the supported languages buffer.
788 //
789 LanguageSize = 0;
790 Status = mPrivate.HiiString.GetLanguages (&mPrivate.HiiString, HiiHandle, &TempSupportedLanguages, &LanguageSize);
791
792 //
793 // If GetLanguages() returns EFI_SUCCESS for a zero size,
794 // then there are no supported languages registered for HiiHandle. If GetLanguages()
795 // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present
796 // in the HII Database
797 //
798 if (Status != EFI_BUFFER_TOO_SMALL) {
799 //
800 // Return NULL if the size can not be retrieved, or if HiiHandle is not in the HII Database
801 //
802 return NULL;
803 }
804
805 //
806 // Allocate the supported languages buffer.
807 //
808 SupportedLanguages = AllocateZeroPool (LanguageSize);
809 if (SupportedLanguages == NULL) {
810 //
811 // Return NULL if allocation fails.
812 //
813 return NULL;
814 }
815
816 //
817 // Retrieve the supported languages string
818 //
819 Status = mPrivate.HiiString.GetLanguages (&mPrivate.HiiString, HiiHandle, SupportedLanguages, &LanguageSize);
820 if (EFI_ERROR (Status)) {
821 //
822 // Free the buffer and return NULL if the supported languages can not be retrieved.
823 //
824 FreePool (SupportedLanguages);
825 return NULL;
826 }
827
828 //
829 // Return the Null-terminated ASCII string of supported languages
830 //
831 return SupportedLanguages;
832 }
833
834 /**
835 Retrieves a string from a string package.
836
837 If HiiHandle is NULL, then ASSERT().
838 If StringId is 0, then ASSET.
839
840 @param[in] HiiHandle A handle that was previously registered in the HII Database.
841 @param[in] StringId The identifier of the string to retrieved from the string
842 package associated with HiiHandle.
843
844 @retval NULL The string specified by StringId is not present in the string package.
845 @retval Other The string was returned.
846
847 **/
848 EFI_STRING
849 InternalGetString (
850 IN EFI_HII_HANDLE HiiHandle,
851 IN EFI_STRING_ID StringId
852 )
853 {
854 EFI_STATUS Status;
855 UINTN StringSize;
856 CHAR16 TempString;
857 EFI_STRING String;
858 CHAR8 *SupportedLanguages;
859 CHAR8 *PlatformLanguage;
860 CHAR8 *BestLanguage;
861 CHAR8 *Language;
862
863 ASSERT (HiiHandle != NULL);
864 ASSERT (StringId != 0);
865
866 //
867 // Initialize all allocated buffers to NULL
868 //
869 SupportedLanguages = NULL;
870 PlatformLanguage = NULL;
871 BestLanguage = NULL;
872 String = NULL;
873 Language = "";
874
875 //
876 // Get the languages that the package specified by HiiHandle supports
877 //
878 SupportedLanguages = GetSupportedLanguages (HiiHandle);
879 if (SupportedLanguages == NULL) {
880 goto Error;
881 }
882
883 //
884 // Get the current platform language setting
885 //
886 GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatformLanguage, NULL);
887
888 //
889 // Get the best matching language from SupportedLanguages
890 //
891 BestLanguage = GetBestLanguage (
892 SupportedLanguages,
893 FALSE, // RFC 4646 mode
894 Language, // Highest priority
895 PlatformLanguage != NULL ? PlatformLanguage : "", // Next highest priority
896 SupportedLanguages, // Lowest priority
897 NULL
898 );
899 if (BestLanguage == NULL) {
900 goto Error;
901 }
902
903 //
904 // Retrieve the size of the string in the string package for the BestLanguage
905 //
906 StringSize = 0;
907 Status = mPrivate.HiiString.GetString (
908 &mPrivate.HiiString,
909 BestLanguage,
910 HiiHandle,
911 StringId,
912 &TempString,
913 &StringSize,
914 NULL
915 );
916 //
917 // If GetString() returns EFI_SUCCESS for a zero size,
918 // then there are no supported languages registered for HiiHandle. If GetString()
919 // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present
920 // in the HII Database
921 //
922 if (Status != EFI_BUFFER_TOO_SMALL) {
923 goto Error;
924 }
925
926 //
927 // Allocate a buffer for the return string
928 //
929 String = AllocateZeroPool (StringSize);
930 if (String == NULL) {
931 goto Error;
932 }
933
934 //
935 // Retrieve the string from the string package
936 //
937 Status = mPrivate.HiiString.GetString (
938 &mPrivate.HiiString,
939 BestLanguage,
940 HiiHandle,
941 StringId,
942 String,
943 &StringSize,
944 NULL
945 );
946 if (EFI_ERROR (Status)) {
947 //
948 // Free the buffer and return NULL if the supported languages can not be retrieved.
949 //
950 FreePool (String);
951 String = NULL;
952 }
953
954 Error:
955 //
956 // Free allocated buffers
957 //
958 if (SupportedLanguages != NULL) {
959 FreePool (SupportedLanguages);
960 }
961 if (PlatformLanguage != NULL) {
962 FreePool (PlatformLanguage);
963 }
964 if (BestLanguage != NULL) {
965 FreePool (BestLanguage);
966 }
967
968 //
969 // Return the Null-terminated Unicode string
970 //
971 return String;
972 }
973
974 /**
975 This function checks VarOffset and VarWidth is in the block range.
976
977 @param RequestBlockArray The block array is to be checked.
978 @param VarOffset Offset of var to the structure
979 @param VarWidth Width of var.
980 @param IsNameValueType Whether this varstore is name/value varstore or not.
981 @param HiiHandle Hii handle for this hii package.
982
983 @retval TRUE This Var is in the block range.
984 @retval FALSE This Var is not in the block range.
985 **/
986 BOOLEAN
987 BlockArrayCheck (
988 IN IFR_BLOCK_DATA *RequestBlockArray,
989 IN UINT16 VarOffset,
990 IN UINT16 VarWidth,
991 IN BOOLEAN IsNameValueType,
992 IN EFI_HII_HANDLE HiiHandle
993 )
994 {
995 LIST_ENTRY *Link;
996 IFR_BLOCK_DATA *BlockData;
997 EFI_STRING Name;
998
999 //
1000 // No Request Block array, all vars are got.
1001 //
1002 if (RequestBlockArray == NULL) {
1003 return TRUE;
1004 }
1005
1006 //
1007 // Check the input var is in the request block range.
1008 //
1009 for (Link = RequestBlockArray->Entry.ForwardLink; Link != &RequestBlockArray->Entry; Link = Link->ForwardLink) {
1010 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
1011
1012 if (IsNameValueType) {
1013 Name = InternalGetString (HiiHandle, VarOffset);
1014 ASSERT (Name != NULL);
1015
1016 if (StrnCmp (BlockData->Name, Name, StrLen (Name)) == 0) {
1017 FreePool (Name);
1018 return TRUE;
1019 }
1020 FreePool (Name);
1021 } else {
1022 if ((VarOffset >= BlockData->Offset) && ((VarOffset + VarWidth) <= (BlockData->Offset + BlockData->Width))) {
1023 return TRUE;
1024 }
1025 }
1026 }
1027
1028 return FALSE;
1029 }
1030
1031 /**
1032 Get form package data from data base.
1033
1034 @param DataBaseRecord The DataBaseRecord instance contains the found Hii handle and package.
1035 @param HiiFormPackage The buffer saves the package data.
1036 @param PackageSize The buffer size of the package data.
1037
1038 **/
1039 EFI_STATUS
1040 GetFormPackageData (
1041 IN HII_DATABASE_RECORD *DataBaseRecord,
1042 IN OUT UINT8 **HiiFormPackage,
1043 OUT UINTN *PackageSize
1044 )
1045 {
1046 EFI_STATUS Status;
1047 UINTN Size;
1048 UINTN ResultSize;
1049
1050 if (DataBaseRecord == NULL || HiiFormPackage == NULL || PackageSize == NULL) {
1051 return EFI_INVALID_PARAMETER;
1052 }
1053
1054 Size = 0;
1055 ResultSize = 0;
1056 //
1057 // 0. Get Hii Form Package by HiiHandle
1058 //
1059 Status = ExportFormPackages (
1060 &mPrivate,
1061 DataBaseRecord->Handle,
1062 DataBaseRecord->PackageList,
1063 0,
1064 Size,
1065 HiiFormPackage,
1066 &ResultSize
1067 );
1068 if (EFI_ERROR (Status)) {
1069 return Status;
1070 }
1071
1072 (*HiiFormPackage) = AllocatePool (ResultSize);
1073 if (*HiiFormPackage == NULL) {
1074 Status = EFI_OUT_OF_RESOURCES;
1075 return Status;
1076 }
1077
1078 //
1079 // Get HiiFormPackage by HiiHandle
1080 //
1081 Size = ResultSize;
1082 ResultSize = 0;
1083 Status = ExportFormPackages (
1084 &mPrivate,
1085 DataBaseRecord->Handle,
1086 DataBaseRecord->PackageList,
1087 0,
1088 Size,
1089 *HiiFormPackage,
1090 &ResultSize
1091 );
1092 if (EFI_ERROR (Status)) {
1093 FreePool (*HiiFormPackage);
1094 }
1095
1096 *PackageSize = Size;
1097
1098 return Status;
1099 }
1100
1101
1102 /**
1103 This function parses Form Package to get the efi varstore info according to the request ConfigHdr.
1104
1105 @param DataBaseRecord The DataBaseRecord instance contains the found Hii handle and package.
1106 @param ConfigHdr Request string ConfigHdr. If it is NULL,
1107 the first found varstore will be as ConfigHdr.
1108 @param IsEfiVarstore Whether the request storage type is efi varstore type.
1109 @param EfiVarStore The efi varstore info which will return.
1110 **/
1111 EFI_STATUS
1112 GetVarStoreType (
1113 IN HII_DATABASE_RECORD *DataBaseRecord,
1114 IN EFI_STRING ConfigHdr,
1115 OUT BOOLEAN *IsEfiVarstore,
1116 OUT EFI_IFR_VARSTORE_EFI **EfiVarStore
1117 )
1118 {
1119 EFI_STATUS Status;
1120 UINTN IfrOffset;
1121 UINTN PackageOffset;
1122 EFI_IFR_OP_HEADER *IfrOpHdr;
1123 CHAR16 *VarStoreName;
1124 EFI_STRING GuidStr;
1125 EFI_STRING NameStr;
1126 EFI_STRING TempStr;
1127 UINTN LengthString;
1128 UINT8 *HiiFormPackage;
1129 UINTN PackageSize;
1130 EFI_IFR_VARSTORE_EFI *IfrEfiVarStore;
1131 EFI_HII_PACKAGE_HEADER *PackageHeader;
1132
1133 HiiFormPackage = NULL;
1134 LengthString = 0;
1135 Status = EFI_SUCCESS;
1136 GuidStr = NULL;
1137 NameStr = NULL;
1138 TempStr = NULL;
1139 *IsEfiVarstore = FALSE;
1140
1141 Status = GetFormPackageData(DataBaseRecord, &HiiFormPackage, &PackageSize);
1142 if (EFI_ERROR (Status)) {
1143 return Status;
1144 }
1145
1146 IfrOffset = sizeof (EFI_HII_PACKAGE_HEADER);
1147 PackageOffset = IfrOffset;
1148 PackageHeader = (EFI_HII_PACKAGE_HEADER *) HiiFormPackage;
1149
1150 while (IfrOffset < PackageSize) {
1151 //
1152 // More than one form packages exist.
1153 //
1154 if (PackageOffset >= PackageHeader->Length) {
1155 //
1156 // Process the new form package.
1157 //
1158 PackageOffset = sizeof (EFI_HII_PACKAGE_HEADER);
1159 IfrOffset += PackageOffset;
1160 PackageHeader = (EFI_HII_PACKAGE_HEADER *) (HiiFormPackage + IfrOffset);
1161 }
1162
1163 IfrOpHdr = (EFI_IFR_OP_HEADER *) (HiiFormPackage + IfrOffset);
1164 IfrOffset += IfrOpHdr->Length;
1165 PackageOffset += IfrOpHdr->Length;
1166
1167 if (IfrOpHdr->OpCode == EFI_IFR_VARSTORE_EFI_OP ) {
1168 IfrEfiVarStore = (EFI_IFR_VARSTORE_EFI *) IfrOpHdr;
1169 //
1170 // If the length is small than the structure, this is from old efi
1171 // varstore definition. Old efi varstore get config directly from
1172 // GetVariable function.
1173 //
1174 if (IfrOpHdr->Length < sizeof (EFI_IFR_VARSTORE_EFI)) {
1175 continue;
1176 }
1177
1178 VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrEfiVarStore->Name) * sizeof (CHAR16));
1179 if (VarStoreName == NULL) {
1180 Status = EFI_OUT_OF_RESOURCES;
1181 goto Done;
1182 }
1183 AsciiStrToUnicodeStr ((CHAR8 *) IfrEfiVarStore->Name, VarStoreName);
1184
1185 GenerateSubStr (L"GUID=", sizeof (EFI_GUID), (VOID *) &IfrEfiVarStore->Guid, 1, &GuidStr);
1186 GenerateSubStr (L"NAME=", StrLen (VarStoreName) * sizeof (CHAR16), (VOID *) VarStoreName, 2, &NameStr);
1187 LengthString = StrLen (GuidStr);
1188 LengthString = LengthString + StrLen (NameStr) + 1;
1189 TempStr = AllocateZeroPool (LengthString * sizeof (CHAR16));
1190 if (TempStr == NULL) {
1191 FreePool (GuidStr);
1192 FreePool (NameStr);
1193 FreePool (VarStoreName);
1194 Status = EFI_OUT_OF_RESOURCES;
1195 goto Done;
1196 }
1197 StrCpyS (TempStr, LengthString, GuidStr);
1198 StrCatS (TempStr, LengthString, NameStr);
1199 if (ConfigHdr == NULL || StrnCmp (ConfigHdr, TempStr, StrLen (TempStr)) == 0) {
1200 *EfiVarStore = (EFI_IFR_VARSTORE_EFI *) AllocateZeroPool (IfrOpHdr->Length);
1201 if (*EfiVarStore == NULL) {
1202 FreePool (VarStoreName);
1203 FreePool (GuidStr);
1204 FreePool (NameStr);
1205 FreePool (TempStr);
1206 Status = EFI_OUT_OF_RESOURCES;
1207 goto Done;
1208 }
1209 *IsEfiVarstore = TRUE;
1210 CopyMem (*EfiVarStore, IfrEfiVarStore, IfrOpHdr->Length);
1211 }
1212
1213 //
1214 // Free alllocated temp string.
1215 //
1216 FreePool (VarStoreName);
1217 FreePool (GuidStr);
1218 FreePool (NameStr);
1219 FreePool (TempStr);
1220
1221 //
1222 // Already found the varstore, break;
1223 //
1224 if (*IsEfiVarstore) {
1225 break;
1226 }
1227 }
1228 }
1229 Done:
1230 if (HiiFormPackage != NULL) {
1231 FreePool (HiiFormPackage);
1232 }
1233
1234 return Status;
1235 }
1236
1237 /**
1238 Check whether the ConfigRequest string has the request elements.
1239 For EFI_HII_VARSTORE_BUFFER type, the request has "&OFFSET=****&WIDTH=****..." format.
1240 For EFI_HII_VARSTORE_NAME_VALUE type, the request has "&NAME1**&NAME2..." format.
1241
1242 @param ConfigRequest The input config request string.
1243
1244 @retval TRUE The input include config request elements.
1245 @retval FALSE The input string not includes.
1246
1247 **/
1248 BOOLEAN
1249 GetElementsFromRequest (
1250 IN EFI_STRING ConfigRequest
1251 )
1252 {
1253 EFI_STRING TmpRequest;
1254
1255 TmpRequest = StrStr (ConfigRequest, L"PATH=");
1256 ASSERT (TmpRequest != NULL);
1257
1258 if ((StrStr (TmpRequest, L"&OFFSET=") != NULL) || (StrStr (TmpRequest, L"&") != NULL)) {
1259 return TRUE;
1260 }
1261
1262 return FALSE;
1263 }
1264
1265 /**
1266 Check whether the this varstore is the request varstore.
1267
1268 @param VarstoreGuid Varstore guid.
1269 @param Name Varstore name.
1270 @param ConfigHdr Current configRequest info.
1271
1272 @retval TRUE This varstore is the requst one.
1273 @retval FALSE This varstore is not the requst one.
1274
1275 **/
1276 BOOLEAN
1277 IsThisVarstore (
1278 IN EFI_GUID *VarstoreGuid,
1279 IN CHAR16 *Name,
1280 IN CHAR16 *ConfigHdr
1281 )
1282 {
1283 EFI_STRING GuidStr;
1284 EFI_STRING NameStr;
1285 EFI_STRING TempStr;
1286 UINTN LengthString;
1287 BOOLEAN RetVal;
1288
1289 RetVal = FALSE;
1290 GuidStr = NULL;
1291 TempStr = NULL;
1292
1293 //
1294 // If ConfigHdr has name field and varstore not has name, return FALSE.
1295 //
1296 if (Name == NULL && ConfigHdr != NULL && StrStr (ConfigHdr, L"NAME=&") == NULL) {
1297 return FALSE;
1298 }
1299
1300 GenerateSubStr (L"GUID=", sizeof (EFI_GUID), (VOID *)VarstoreGuid, 1, &GuidStr);
1301 if (Name != NULL) {
1302 GenerateSubStr (L"NAME=", StrLen (Name) * sizeof (CHAR16), (VOID *) Name, 2, &NameStr);
1303 } else {
1304 GenerateSubStr (L"NAME=", 0, NULL, 2, &NameStr);
1305 }
1306 LengthString = StrLen (GuidStr);
1307 LengthString = LengthString + StrLen (NameStr) + 1;
1308 TempStr = AllocateZeroPool (LengthString * sizeof (CHAR16));
1309 if (TempStr == NULL) {
1310 goto Done;
1311 }
1312
1313 StrCpyS (TempStr, LengthString, GuidStr);
1314 StrCatS (TempStr, LengthString, NameStr);
1315
1316 if (ConfigHdr == NULL || StrnCmp (ConfigHdr, TempStr, StrLen (TempStr)) == 0) {
1317 RetVal = TRUE;
1318 }
1319
1320 Done:
1321 if (GuidStr != NULL) {
1322 FreePool (GuidStr);
1323 }
1324
1325 if (NameStr != NULL) {
1326 FreePool (NameStr);
1327 }
1328
1329 if (TempStr != NULL) {
1330 FreePool (TempStr);
1331 }
1332
1333 return RetVal;
1334 }
1335
1336 /**
1337 This function parses Form Package to get the efi varstore info according to the request ConfigHdr.
1338
1339 @param DataBaseRecord The DataBaseRecord instance contains the found Hii handle and package.
1340 @param ConfigHdr Request string ConfigHdr. If it is NULL,
1341 the first found varstore will be as ConfigHdr.
1342 @retval TRUE This hii package is the reqeust one.
1343 @retval FALSE This hii package is not the reqeust one.
1344 **/
1345 BOOLEAN
1346 IsThisPackageList (
1347 IN HII_DATABASE_RECORD *DataBaseRecord,
1348 IN EFI_STRING ConfigHdr
1349 )
1350 {
1351 EFI_STATUS Status;
1352 UINTN IfrOffset;
1353 UINTN PackageOffset;
1354 EFI_IFR_OP_HEADER *IfrOpHdr;
1355 CHAR16 *VarStoreName;
1356 UINT8 *HiiFormPackage;
1357 UINTN PackageSize;
1358 EFI_IFR_VARSTORE_EFI *IfrEfiVarStore;
1359 EFI_HII_PACKAGE_HEADER *PackageHeader;
1360 EFI_IFR_VARSTORE *IfrVarStore;
1361 EFI_IFR_VARSTORE_NAME_VALUE *IfrNameValueVarStore;
1362 BOOLEAN FindVarstore;
1363
1364 HiiFormPackage = NULL;
1365 VarStoreName = NULL;
1366 Status = EFI_SUCCESS;
1367 FindVarstore = FALSE;
1368
1369 Status = GetFormPackageData(DataBaseRecord, &HiiFormPackage, &PackageSize);
1370 if (EFI_ERROR (Status)) {
1371 return FALSE;
1372 }
1373
1374 IfrOffset = sizeof (EFI_HII_PACKAGE_HEADER);
1375 PackageOffset = IfrOffset;
1376 PackageHeader = (EFI_HII_PACKAGE_HEADER *) HiiFormPackage;
1377
1378 while (IfrOffset < PackageSize) {
1379 //
1380 // More than one form packages exist.
1381 //
1382 if (PackageOffset >= PackageHeader->Length) {
1383 //
1384 // Process the new form package.
1385 //
1386 PackageOffset = sizeof (EFI_HII_PACKAGE_HEADER);
1387 IfrOffset += PackageOffset;
1388 PackageHeader = (EFI_HII_PACKAGE_HEADER *) (HiiFormPackage + IfrOffset);
1389 }
1390
1391 IfrOpHdr = (EFI_IFR_OP_HEADER *) (HiiFormPackage + IfrOffset);
1392 IfrOffset += IfrOpHdr->Length;
1393 PackageOffset += IfrOpHdr->Length;
1394
1395 switch (IfrOpHdr->OpCode) {
1396
1397 case EFI_IFR_VARSTORE_OP:
1398 IfrVarStore = (EFI_IFR_VARSTORE *) IfrOpHdr;
1399
1400 VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrVarStore->Name) * sizeof (CHAR16));
1401 if (VarStoreName == NULL) {
1402 goto Done;
1403 }
1404 AsciiStrToUnicodeStr ((CHAR8 *)IfrVarStore->Name, VarStoreName);
1405
1406 if (IsThisVarstore((VOID *)&IfrVarStore->Guid, VarStoreName, ConfigHdr)) {
1407 FindVarstore = TRUE;
1408 goto Done;
1409 }
1410 break;
1411
1412 case EFI_IFR_VARSTORE_EFI_OP:
1413 IfrEfiVarStore = (EFI_IFR_VARSTORE_EFI *) IfrOpHdr;
1414 VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrEfiVarStore->Name) * sizeof (CHAR16));
1415 if (VarStoreName == NULL) {
1416 goto Done;
1417 }
1418 AsciiStrToUnicodeStr ((CHAR8 *)IfrEfiVarStore->Name, VarStoreName);
1419
1420 if (IsThisVarstore (&IfrEfiVarStore->Guid, VarStoreName, ConfigHdr)) {
1421 FindVarstore = TRUE;
1422 goto Done;
1423 }
1424 break;
1425
1426 case EFI_IFR_VARSTORE_NAME_VALUE_OP:
1427 IfrNameValueVarStore = (EFI_IFR_VARSTORE_NAME_VALUE *) IfrOpHdr;
1428
1429 if (IsThisVarstore (&IfrNameValueVarStore->Guid, NULL, ConfigHdr)) {
1430 FindVarstore = TRUE;
1431 goto Done;
1432 }
1433 break;
1434
1435 case EFI_IFR_FORM_OP:
1436 case EFI_IFR_FORM_MAP_OP:
1437 //
1438 // No matched varstore is found and directly return.
1439 //
1440 goto Done;
1441
1442 default:
1443 break;
1444 }
1445 }
1446 Done:
1447 if (HiiFormPackage != NULL) {
1448 FreePool (HiiFormPackage);
1449 }
1450
1451 if (VarStoreName != NULL) {
1452 FreePool (VarStoreName);
1453 }
1454
1455 return FindVarstore;
1456 }
1457
1458 /**
1459 Check whether the this op code is required.
1460
1461 @param RequestBlockArray The array includes all the request info or NULL.
1462 @param HiiHandle The hii handle for this form package.
1463 @param VarStorageData The varstore data strucure.
1464 @param IfrOpHdr Ifr opcode header for this opcode.
1465 @param VarWidth The buffer width for this opcode.
1466 @param ReturnData The data block added for this opcode.
1467
1468 @retval EFI_SUCCESS This opcode is required.
1469 @retval Others This opcode is not required or error occur.
1470
1471 **/
1472 EFI_STATUS
1473 IsThisOpcodeRequired (
1474 IN IFR_BLOCK_DATA *RequestBlockArray,
1475 IN EFI_HII_HANDLE HiiHandle,
1476 IN OUT IFR_VARSTORAGE_DATA *VarStorageData,
1477 IN EFI_IFR_OP_HEADER *IfrOpHdr,
1478 IN UINT16 VarWidth,
1479 OUT IFR_BLOCK_DATA **ReturnData
1480 )
1481 {
1482 IFR_BLOCK_DATA *BlockData;
1483 UINT16 VarOffset;
1484 EFI_STRING_ID NameId;
1485 EFI_IFR_QUESTION_HEADER *IfrQuestionHdr;
1486
1487 NameId = 0;
1488 VarOffset = 0;
1489 IfrQuestionHdr = (EFI_IFR_QUESTION_HEADER *)((CHAR8 *) IfrOpHdr + sizeof (EFI_IFR_OP_HEADER));
1490
1491 if (VarStorageData->Type == EFI_HII_VARSTORE_NAME_VALUE) {
1492 NameId = IfrQuestionHdr->VarStoreInfo.VarName;
1493
1494 //
1495 // Check whether this question is in requested block array.
1496 //
1497 if (!BlockArrayCheck (RequestBlockArray, NameId, 0, TRUE, HiiHandle)) {
1498 //
1499 // This question is not in the requested string. Skip it.
1500 //
1501 return EFI_SUCCESS;
1502 }
1503 } else {
1504 VarOffset = IfrQuestionHdr->VarStoreInfo.VarOffset;
1505
1506 //
1507 // Check whether this question is in requested block array.
1508 //
1509 if (!BlockArrayCheck (RequestBlockArray, VarOffset, VarWidth, FALSE, HiiHandle)) {
1510 //
1511 // This question is not in the requested string. Skip it.
1512 //
1513 return EFI_SUCCESS;
1514 }
1515
1516 //
1517 // Check this var question is in the var storage
1518 //
1519 if (((VarOffset + VarWidth) > VarStorageData->Size)) {
1520 return EFI_INVALID_PARAMETER;
1521 }
1522 }
1523
1524 BlockData = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
1525 if (BlockData == NULL) {
1526 return EFI_OUT_OF_RESOURCES;
1527 }
1528
1529 if (VarStorageData->Type == EFI_HII_VARSTORE_NAME_VALUE) {
1530 BlockData->Name = InternalGetString(HiiHandle, NameId);
1531 } else {
1532 BlockData->Offset = VarOffset;
1533 }
1534
1535 BlockData->Width = VarWidth;
1536 BlockData->QuestionId = IfrQuestionHdr->QuestionId;
1537 BlockData->OpCode = IfrOpHdr->OpCode;
1538 BlockData->Scope = IfrOpHdr->Scope;
1539 InitializeListHead (&BlockData->DefaultValueEntry);
1540 //
1541 // Add Block Data into VarStorageData BlockEntry
1542 //
1543 InsertBlockData (&VarStorageData->BlockEntry, &BlockData);
1544 *ReturnData = BlockData;
1545
1546 return EFI_SUCCESS;
1547 }
1548
1549 /**
1550 This function parses Form Package to get the block array and the default
1551 value array according to the request ConfigHdr.
1552
1553 @param HiiHandle Hii Handle for this hii package.
1554 @param Package Pointer to the form package data.
1555 @param PackageLength Length of the pacakge.
1556 @param ConfigHdr Request string ConfigHdr. If it is NULL,
1557 the first found varstore will be as ConfigHdr.
1558 @param RequestBlockArray The block array is retrieved from the request string.
1559 @param VarStorageData VarStorage structure contains the got block and default value.
1560 @param DefaultIdArray Point to the got default id and default name array.
1561
1562 @retval EFI_SUCCESS The block array and the default value array are got.
1563 @retval EFI_INVALID_PARAMETER The varstore defintion in the differnt form pacakges
1564 are conflicted.
1565 @retval EFI_OUT_OF_RESOURCES No enough memory.
1566 **/
1567 EFI_STATUS
1568 EFIAPI
1569 ParseIfrData (
1570 IN EFI_HII_HANDLE HiiHandle,
1571 IN UINT8 *Package,
1572 IN UINT32 PackageLength,
1573 IN EFI_STRING ConfigHdr,
1574 IN IFR_BLOCK_DATA *RequestBlockArray,
1575 IN OUT IFR_VARSTORAGE_DATA *VarStorageData,
1576 OUT IFR_DEFAULT_DATA *DefaultIdArray
1577 )
1578 {
1579 EFI_STATUS Status;
1580 UINTN IfrOffset;
1581 UINTN PackageOffset;
1582 EFI_IFR_VARSTORE *IfrVarStore;
1583 EFI_IFR_VARSTORE_EFI *IfrEfiVarStore;
1584 EFI_IFR_OP_HEADER *IfrOpHdr;
1585 EFI_IFR_ONE_OF *IfrOneOf;
1586 EFI_IFR_REF4 *IfrRef;
1587 EFI_IFR_ONE_OF_OPTION *IfrOneOfOption;
1588 EFI_IFR_DEFAULT *IfrDefault;
1589 EFI_IFR_ORDERED_LIST *IfrOrderedList;
1590 EFI_IFR_CHECKBOX *IfrCheckBox;
1591 EFI_IFR_PASSWORD *IfrPassword;
1592 EFI_IFR_STRING *IfrString;
1593 EFI_IFR_DATE *IfrDate;
1594 EFI_IFR_TIME *IfrTime;
1595 IFR_DEFAULT_DATA DefaultData;
1596 IFR_DEFAULT_DATA *DefaultDataPtr;
1597 IFR_BLOCK_DATA *BlockData;
1598 CHAR16 *VarStoreName;
1599 UINT16 VarWidth;
1600 UINT16 VarDefaultId;
1601 BOOLEAN FirstOneOfOption;
1602 LIST_ENTRY *LinkData;
1603 LIST_ENTRY *LinkDefault;
1604 EFI_IFR_VARSTORE_NAME_VALUE *IfrNameValueVarStore;
1605 EFI_HII_PACKAGE_HEADER *PackageHeader;
1606 EFI_VARSTORE_ID VarStoreId;
1607
1608 Status = EFI_SUCCESS;
1609 BlockData = NULL;
1610 DefaultDataPtr = NULL;
1611 FirstOneOfOption = FALSE;
1612 VarStoreId = 0;
1613 ZeroMem (&DefaultData, sizeof (IFR_DEFAULT_DATA));
1614
1615 //
1616 // Go through the form package to parse OpCode one by one.
1617 //
1618 PackageOffset = sizeof (EFI_HII_PACKAGE_HEADER);
1619 PackageHeader = (EFI_HII_PACKAGE_HEADER *) Package;
1620 IfrOffset = PackageOffset;
1621 while (IfrOffset < PackageLength) {
1622
1623 //
1624 // More than one form package found.
1625 //
1626 if (PackageOffset >= PackageHeader->Length) {
1627 //
1628 // Already found varstore for this request, break;
1629 //
1630 if (VarStoreId != 0) {
1631 VarStoreId = 0;
1632 }
1633
1634 //
1635 // Get next package header info.
1636 //
1637 IfrOffset += sizeof (EFI_HII_PACKAGE_HEADER);
1638 PackageOffset = sizeof (EFI_HII_PACKAGE_HEADER);
1639 PackageHeader = (EFI_HII_PACKAGE_HEADER *) (Package + IfrOffset);
1640 }
1641
1642 IfrOpHdr = (EFI_IFR_OP_HEADER *) (Package + IfrOffset);
1643 switch (IfrOpHdr->OpCode) {
1644 case EFI_IFR_VARSTORE_OP:
1645 //
1646 // VarStore is found. Don't need to search any more.
1647 //
1648 if (VarStoreId != 0) {
1649 break;
1650 }
1651
1652 IfrVarStore = (EFI_IFR_VARSTORE *) IfrOpHdr;
1653
1654 VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrVarStore->Name) * sizeof (CHAR16));
1655 if (VarStoreName == NULL) {
1656 Status = EFI_OUT_OF_RESOURCES;
1657 goto Done;
1658 }
1659 AsciiStrToUnicodeStr ((CHAR8 *)IfrVarStore->Name, VarStoreName);
1660
1661 if (IsThisVarstore((VOID *)&IfrVarStore->Guid, VarStoreName, ConfigHdr)) {
1662 //
1663 // Find the matched VarStore
1664 //
1665 CopyGuid (&VarStorageData->Guid, (EFI_GUID *) (VOID *) &IfrVarStore->Guid);
1666 VarStorageData->Size = IfrVarStore->Size;
1667 VarStorageData->Name = VarStoreName;
1668 VarStorageData->Type = EFI_HII_VARSTORE_BUFFER;
1669 VarStoreId = IfrVarStore->VarStoreId;
1670 }
1671 break;
1672
1673 case EFI_IFR_VARSTORE_EFI_OP:
1674 //
1675 // VarStore is found. Don't need to search any more.
1676 //
1677 if (VarStoreId != 0) {
1678 break;
1679 }
1680
1681 IfrEfiVarStore = (EFI_IFR_VARSTORE_EFI *) IfrOpHdr;
1682
1683 //
1684 // If the length is small than the structure, this is from old efi
1685 // varstore definition. Old efi varstore get config directly from
1686 // GetVariable function.
1687 //
1688 if (IfrOpHdr->Length < sizeof (EFI_IFR_VARSTORE_EFI)) {
1689 break;
1690 }
1691
1692 VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrEfiVarStore->Name) * sizeof (CHAR16));
1693 if (VarStoreName == NULL) {
1694 Status = EFI_OUT_OF_RESOURCES;
1695 goto Done;
1696 }
1697 AsciiStrToUnicodeStr ((CHAR8 *)IfrEfiVarStore->Name, VarStoreName);
1698
1699 if (IsThisVarstore (&IfrEfiVarStore->Guid, VarStoreName, ConfigHdr)) {
1700 //
1701 // Find the matched VarStore
1702 //
1703 CopyGuid (&VarStorageData->Guid, (EFI_GUID *) (VOID *) &IfrEfiVarStore->Guid);
1704 VarStorageData->Size = IfrEfiVarStore->Size;
1705 VarStorageData->Name = VarStoreName;
1706 VarStorageData->Type = EFI_HII_VARSTORE_EFI_VARIABLE_BUFFER;
1707 VarStoreId = IfrEfiVarStore->VarStoreId;
1708 }
1709 break;
1710
1711 case EFI_IFR_VARSTORE_NAME_VALUE_OP:
1712 //
1713 // VarStore is found. Don't need to search any more.
1714 //
1715 if (VarStoreId != 0) {
1716 break;
1717 }
1718
1719 IfrNameValueVarStore = (EFI_IFR_VARSTORE_NAME_VALUE *) IfrOpHdr;
1720
1721 if (IsThisVarstore (&IfrNameValueVarStore->Guid, NULL, ConfigHdr)) {
1722 //
1723 // Find the matched VarStore
1724 //
1725 CopyGuid (&VarStorageData->Guid, (EFI_GUID *) (VOID *) &IfrNameValueVarStore->Guid);
1726 VarStorageData->Type = EFI_HII_VARSTORE_NAME_VALUE;
1727 VarStoreId = IfrNameValueVarStore->VarStoreId;
1728 }
1729 break;
1730
1731 case EFI_IFR_DEFAULTSTORE_OP:
1732 //
1733 // Add new the map between default id and default name.
1734 //
1735 DefaultDataPtr = (IFR_DEFAULT_DATA *) AllocateZeroPool (sizeof (IFR_DEFAULT_DATA));
1736 if (DefaultDataPtr == NULL) {
1737 Status = EFI_OUT_OF_RESOURCES;
1738 goto Done;
1739 }
1740 DefaultDataPtr->DefaultId = ((EFI_IFR_DEFAULTSTORE *) IfrOpHdr)->DefaultId;
1741 InsertTailList (&DefaultIdArray->Entry, &DefaultDataPtr->Entry);
1742 DefaultDataPtr = NULL;
1743 break;
1744
1745 case EFI_IFR_FORM_OP:
1746 case EFI_IFR_FORM_MAP_OP:
1747 //
1748 // No matched varstore is found and directly return.
1749 //
1750 if ( VarStoreId == 0) {
1751 Status = EFI_SUCCESS;
1752 goto Done;
1753 }
1754 break;
1755
1756 case EFI_IFR_REF_OP:
1757 //
1758 // Ref question is not in IFR Form. This IFR form is not valid.
1759 //
1760 if ( VarStoreId == 0) {
1761 Status = EFI_INVALID_PARAMETER;
1762 goto Done;
1763 }
1764 //
1765 // Check whether this question is for the requested varstore.
1766 //
1767 IfrRef = (EFI_IFR_REF4 *) IfrOpHdr;
1768 if (IfrRef->Question.VarStoreId != VarStoreId) {
1769 break;
1770 }
1771 VarWidth = (UINT16) (sizeof (EFI_HII_REF));
1772
1773 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
1774 if (EFI_ERROR (Status)) {
1775 goto Done;
1776 }
1777 break;
1778
1779 case EFI_IFR_ONE_OF_OP:
1780 case EFI_IFR_NUMERIC_OP:
1781 //
1782 // Numeric and OneOf has the same opcode structure.
1783 //
1784
1785 //
1786 // Numeric and OneOf question is not in IFR Form. This IFR form is not valid.
1787 //
1788 if (VarStoreId == 0) {
1789 Status = EFI_INVALID_PARAMETER;
1790 goto Done;
1791 }
1792 //
1793 // Check whether this question is for the requested varstore.
1794 //
1795 IfrOneOf = (EFI_IFR_ONE_OF *) IfrOpHdr;
1796 if (IfrOneOf->Question.VarStoreId != VarStoreId) {
1797 break;
1798 }
1799 VarWidth = (UINT16) (1 << (IfrOneOf->Flags & EFI_IFR_NUMERIC_SIZE));
1800
1801 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
1802 if (EFI_ERROR (Status)) {
1803 goto Done;
1804 }
1805
1806 if (BlockData == NULL) {
1807 //
1808 // BlockData == NULL means this opcode is not in the requst array.
1809 //
1810 break;
1811 }
1812
1813 if (IfrOpHdr->OpCode == EFI_IFR_ONE_OF_OP) {
1814 //
1815 // Set this flag to TRUE for the first oneof option.
1816 //
1817 FirstOneOfOption = TRUE;
1818 } else if (IfrOpHdr->OpCode == EFI_IFR_NUMERIC_OP) {
1819 //
1820 // Numeric minimum value will be used as default value when no default is specified.
1821 //
1822 DefaultData.Type = DefaultValueFromDefault;
1823 switch (IfrOneOf->Flags & EFI_IFR_NUMERIC_SIZE) {
1824 case EFI_IFR_NUMERIC_SIZE_1:
1825 DefaultData.Value.u8 = IfrOneOf->data.u8.MinValue;
1826 break;
1827
1828 case EFI_IFR_NUMERIC_SIZE_2:
1829 CopyMem (&DefaultData.Value.u16, &IfrOneOf->data.u16.MinValue, sizeof (UINT16));
1830 break;
1831
1832 case EFI_IFR_NUMERIC_SIZE_4:
1833 CopyMem (&DefaultData.Value.u32, &IfrOneOf->data.u32.MinValue, sizeof (UINT32));
1834 break;
1835
1836 case EFI_IFR_NUMERIC_SIZE_8:
1837 CopyMem (&DefaultData.Value.u64, &IfrOneOf->data.u64.MinValue, sizeof (UINT64));
1838 break;
1839
1840 default:
1841 Status = EFI_INVALID_PARAMETER;
1842 goto Done;
1843 }
1844 //
1845 // Set default value base on the DefaultId list get from IFR data.
1846 //
1847 for (LinkData = DefaultIdArray->Entry.ForwardLink; LinkData != &DefaultIdArray->Entry; LinkData = LinkData->ForwardLink) {
1848 DefaultDataPtr = BASE_CR (LinkData, IFR_DEFAULT_DATA, Entry);
1849 DefaultData.DefaultId = DefaultDataPtr->DefaultId;
1850 InsertDefaultValue (BlockData, &DefaultData);
1851 }
1852 }
1853 break;
1854
1855 case EFI_IFR_ORDERED_LIST_OP:
1856 //
1857 // offset by question header
1858 // width by EFI_IFR_ORDERED_LIST MaxContainers * OneofOption Type
1859 // no default value and default id, how to define its default value?
1860 //
1861
1862 //
1863 // OrderedList question is not in IFR Form. This IFR form is not valid.
1864 //
1865 if (VarStoreId == 0) {
1866 Status = EFI_INVALID_PARAMETER;
1867 goto Done;
1868 }
1869 //
1870 // Check whether this question is for the requested varstore.
1871 //
1872 IfrOrderedList = (EFI_IFR_ORDERED_LIST *) IfrOpHdr;
1873 if (IfrOrderedList->Question.VarStoreId != VarStoreId) {
1874 BlockData = NULL;
1875 break;
1876 }
1877 VarWidth = IfrOrderedList->MaxContainers;
1878 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
1879 if (EFI_ERROR (Status)) {
1880 goto Done;
1881 }
1882 break;
1883
1884 case EFI_IFR_CHECKBOX_OP:
1885 //
1886 // EFI_IFR_DEFAULT_OP
1887 // offset by question header
1888 // width is 1 sizeof (BOOLEAN)
1889 // default id by CheckBox Flags if CheckBox flags (Default or Mau) is set, the default value is 1 to be set.
1890 // value by DefaultOption
1891 // default id by DeaultOption DefaultId can override CheckBox Flags and Default value.
1892 //
1893
1894 //
1895 // CheckBox question is not in IFR Form. This IFR form is not valid.
1896 //
1897 if (VarStoreId == 0) {
1898 Status = EFI_INVALID_PARAMETER;
1899 goto Done;
1900 }
1901 //
1902 // Check whether this question is for the requested varstore.
1903 //
1904 IfrCheckBox = (EFI_IFR_CHECKBOX *) IfrOpHdr;
1905 if (IfrCheckBox->Question.VarStoreId != VarStoreId) {
1906 break;
1907 }
1908 VarWidth = (UINT16) sizeof (BOOLEAN);
1909 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
1910 if (EFI_ERROR (Status)) {
1911 goto Done;
1912 }
1913
1914 if (BlockData == NULL) {
1915 //
1916 // BlockData == NULL means this opcode is not in the requst array.
1917 //
1918 break;
1919 }
1920
1921 //
1922 // Add default value for standard ID by CheckBox Flag
1923 //
1924 VarDefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
1925 //
1926 // Prepare new DefaultValue
1927 //
1928 DefaultData.DefaultId = VarDefaultId;
1929 if ((IfrCheckBox->Flags & EFI_IFR_CHECKBOX_DEFAULT) == EFI_IFR_CHECKBOX_DEFAULT) {
1930 //
1931 // When flag is set, defautl value is TRUE.
1932 //
1933 DefaultData.Type = DefaultValueFromFlag;
1934 DefaultData.Value.b = TRUE;
1935 } else {
1936 //
1937 // When flag is not set, defautl value is FASLE.
1938 //
1939 DefaultData.Type = DefaultValueFromDefault;
1940 DefaultData.Value.b = FALSE;
1941 }
1942 //
1943 // Add DefaultValue into current BlockData
1944 //
1945 InsertDefaultValue (BlockData, &DefaultData);
1946
1947 //
1948 // Add default value for Manufacture ID by CheckBox Flag
1949 //
1950 VarDefaultId = EFI_HII_DEFAULT_CLASS_MANUFACTURING;
1951 //
1952 // Prepare new DefaultValue
1953 //
1954 DefaultData.DefaultId = VarDefaultId;
1955 if ((IfrCheckBox->Flags & EFI_IFR_CHECKBOX_DEFAULT_MFG) == EFI_IFR_CHECKBOX_DEFAULT_MFG) {
1956 //
1957 // When flag is set, defautl value is TRUE.
1958 //
1959 DefaultData.Type = DefaultValueFromFlag;
1960 DefaultData.Value.b = TRUE;
1961 } else {
1962 //
1963 // When flag is not set, defautl value is FASLE.
1964 //
1965 DefaultData.Type = DefaultValueFromDefault;
1966 DefaultData.Value.b = FALSE;
1967 }
1968 //
1969 // Add DefaultValue into current BlockData
1970 //
1971 InsertDefaultValue (BlockData, &DefaultData);
1972 break;
1973
1974 case EFI_IFR_DATE_OP:
1975 //
1976 // offset by question header
1977 // width MaxSize * sizeof (CHAR16)
1978 // no default value, only block array
1979 //
1980
1981 //
1982 // Date question is not in IFR Form. This IFR form is not valid.
1983 //
1984 if (VarStoreId == 0) {
1985 Status = EFI_INVALID_PARAMETER;
1986 goto Done;
1987 }
1988 //
1989 // Check whether this question is for the requested varstore.
1990 //
1991 IfrDate = (EFI_IFR_DATE *) IfrOpHdr;
1992 if (IfrDate->Question.VarStoreId != VarStoreId) {
1993 break;
1994 }
1995
1996 VarWidth = (UINT16) sizeof (EFI_HII_DATE);
1997 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
1998 if (EFI_ERROR (Status)) {
1999 goto Done;
2000 }
2001 break;
2002
2003 case EFI_IFR_TIME_OP:
2004 //
2005 // offset by question header
2006 // width MaxSize * sizeof (CHAR16)
2007 // no default value, only block array
2008 //
2009
2010 //
2011 // Time question is not in IFR Form. This IFR form is not valid.
2012 //
2013 if (VarStoreId == 0) {
2014 Status = EFI_INVALID_PARAMETER;
2015 goto Done;
2016 }
2017 //
2018 // Check whether this question is for the requested varstore.
2019 //
2020 IfrTime = (EFI_IFR_TIME *) IfrOpHdr;
2021 if (IfrTime->Question.VarStoreId != VarStoreId) {
2022 break;
2023 }
2024
2025 VarWidth = (UINT16) sizeof (EFI_HII_TIME);
2026 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
2027 if (EFI_ERROR (Status)) {
2028 goto Done;
2029 }
2030 break;
2031
2032 case EFI_IFR_STRING_OP:
2033 //
2034 // offset by question header
2035 // width MaxSize * sizeof (CHAR16)
2036 // no default value, only block array
2037 //
2038
2039 //
2040 // String question is not in IFR Form. This IFR form is not valid.
2041 //
2042 if (VarStoreId == 0) {
2043 Status = EFI_INVALID_PARAMETER;
2044 goto Done;
2045 }
2046 //
2047 // Check whether this question is for the requested varstore.
2048 //
2049 IfrString = (EFI_IFR_STRING *) IfrOpHdr;
2050 if (IfrString->Question.VarStoreId != VarStoreId) {
2051 break;
2052 }
2053
2054 VarWidth = (UINT16) (IfrString->MaxSize * sizeof (UINT16));
2055 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
2056 if (EFI_ERROR (Status)) {
2057 goto Done;
2058 }
2059 break;
2060
2061 case EFI_IFR_PASSWORD_OP:
2062 //
2063 // offset by question header
2064 // width MaxSize * sizeof (CHAR16)
2065 // no default value, only block array
2066 //
2067
2068 //
2069 // Password question is not in IFR Form. This IFR form is not valid.
2070 //
2071 if (VarStoreId == 0) {
2072 Status = EFI_INVALID_PARAMETER;
2073 goto Done;
2074 }
2075 //
2076 // Check whether this question is for the requested varstore.
2077 //
2078 IfrPassword = (EFI_IFR_PASSWORD *) IfrOpHdr;
2079 if (IfrPassword->Question.VarStoreId != VarStoreId) {
2080 break;
2081 }
2082
2083 VarWidth = (UINT16) (IfrPassword->MaxSize * sizeof (UINT16));
2084 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
2085 if (EFI_ERROR (Status)) {
2086 goto Done;
2087 }
2088
2089 //
2090 // No default value for string.
2091 //
2092 BlockData = NULL;
2093 break;
2094
2095 case EFI_IFR_ONE_OF_OPTION_OP:
2096 //
2097 // No matched block data is ignored.
2098 //
2099 if (BlockData == NULL || BlockData->Scope == 0) {
2100 break;
2101 }
2102
2103 IfrOneOfOption = (EFI_IFR_ONE_OF_OPTION *) IfrOpHdr;
2104 if (BlockData->OpCode == EFI_IFR_ORDERED_LIST_OP) {
2105 //
2106 // Get ordered list option data type.
2107 //
2108 if (IfrOneOfOption->Type == EFI_IFR_TYPE_NUM_SIZE_8 || IfrOneOfOption->Type == EFI_IFR_TYPE_BOOLEAN) {
2109 VarWidth = 1;
2110 } else if (IfrOneOfOption->Type == EFI_IFR_TYPE_NUM_SIZE_16) {
2111 VarWidth = 2;
2112 } else if (IfrOneOfOption->Type == EFI_IFR_TYPE_NUM_SIZE_32) {
2113 VarWidth = 4;
2114 } else if (IfrOneOfOption->Type == EFI_IFR_TYPE_NUM_SIZE_64) {
2115 VarWidth = 8;
2116 } else {
2117 //
2118 // Invalid ordered list option data type.
2119 //
2120 Status = EFI_INVALID_PARAMETER;
2121 if (BlockData->Name != NULL) {
2122 FreePool (BlockData->Name);
2123 }
2124 FreePool (BlockData);
2125 goto Done;
2126 }
2127
2128 //
2129 // Calculate Ordered list QuestionId width.
2130 //
2131 BlockData->Width = (UINT16) (BlockData->Width * VarWidth);
2132 //
2133 // Check whether this question is in requested block array.
2134 //
2135 if (!BlockArrayCheck (RequestBlockArray, BlockData->Offset, BlockData->Width, (BOOLEAN)(BlockData->Name != NULL), HiiHandle)) {
2136 //
2137 // This question is not in the requested string. Skip it.
2138 //
2139 if (BlockData->Name != NULL) {
2140 FreePool (BlockData->Name);
2141 }
2142 FreePool (BlockData);
2143 BlockData = NULL;
2144 break;
2145 }
2146 //
2147 // Check this var question is in the var storage
2148 //
2149 if ((BlockData->Name == NULL) && ((BlockData->Offset + BlockData->Width) > VarStorageData->Size)) {
2150 Status = EFI_INVALID_PARAMETER;
2151 if (BlockData->Name != NULL) {
2152 FreePool (BlockData->Name);
2153 }
2154 FreePool (BlockData);
2155 goto Done;
2156 }
2157 //
2158 // Add Block Data into VarStorageData BlockEntry
2159 //
2160 InsertBlockData (&VarStorageData->BlockEntry, &BlockData);
2161 //
2162 // No default data for OrderedList.
2163 //
2164 BlockData = NULL;
2165 break;
2166 }
2167
2168 //
2169 // 1. Set default value for OneOf option when flag field has default attribute.
2170 //
2171 if (((IfrOneOfOption->Flags & EFI_IFR_OPTION_DEFAULT) == EFI_IFR_OPTION_DEFAULT) ||
2172 ((IfrOneOfOption->Flags & EFI_IFR_OPTION_DEFAULT_MFG) == EFI_IFR_OPTION_DEFAULT_MFG)) {
2173 //
2174 // This flag is used to specify whether this option is the first. Set it to FALSE for the following options.
2175 // The first oneof option value will be used as default value when no default value is specified.
2176 //
2177 FirstOneOfOption = FALSE;
2178
2179 // Prepare new DefaultValue
2180 //
2181 DefaultData.Type = DefaultValueFromFlag;
2182 CopyMem (&DefaultData.Value, &IfrOneOfOption->Value, IfrOneOfOption->Header.Length - OFFSET_OF (EFI_IFR_ONE_OF_OPTION, Value));
2183 if ((IfrOneOfOption->Flags & EFI_IFR_OPTION_DEFAULT) == EFI_IFR_OPTION_DEFAULT) {
2184 DefaultData.DefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
2185 InsertDefaultValue (BlockData, &DefaultData);
2186 }
2187 if ((IfrOneOfOption->Flags & EFI_IFR_OPTION_DEFAULT_MFG) == EFI_IFR_OPTION_DEFAULT_MFG) {
2188 DefaultData.DefaultId = EFI_HII_DEFAULT_CLASS_MANUFACTURING;
2189 InsertDefaultValue (BlockData, &DefaultData);
2190 }
2191 }
2192
2193 //
2194 // 2. Set as the default value when this is the first option.
2195 // The first oneof option value will be used as default value when no default value is specified.
2196 //
2197 if (FirstOneOfOption) {
2198 // This flag is used to specify whether this option is the first. Set it to FALSE for the following options.
2199 FirstOneOfOption = FALSE;
2200
2201 //
2202 // Prepare new DefaultValue
2203 //
2204 DefaultData.Type = DefaultValueFromDefault;
2205 CopyMem (&DefaultData.Value, &IfrOneOfOption->Value, IfrOneOfOption->Header.Length - OFFSET_OF (EFI_IFR_ONE_OF_OPTION, Value));
2206 for (LinkData = DefaultIdArray->Entry.ForwardLink; LinkData != &DefaultIdArray->Entry; LinkData = LinkData->ForwardLink) {
2207 DefaultDataPtr = BASE_CR (LinkData, IFR_DEFAULT_DATA, Entry);
2208 DefaultData.DefaultId = DefaultDataPtr->DefaultId;
2209 InsertDefaultValue (BlockData, &DefaultData);
2210 }
2211 }
2212 break;
2213
2214 case EFI_IFR_DEFAULT_OP:
2215 //
2216 // Update Current BlockData to the default value.
2217 //
2218 if (BlockData == NULL || BlockData->Scope == 0) {
2219 //
2220 // No matched block data is ignored.
2221 //
2222 break;
2223 }
2224
2225 if (BlockData->OpCode == EFI_IFR_ORDERED_LIST_OP) {
2226 //
2227 // OrderedList Opcode is no default value.
2228 //
2229 break;
2230 }
2231 //
2232 // Get the DefaultId
2233 //
2234 IfrDefault = (EFI_IFR_DEFAULT *) IfrOpHdr;
2235 VarDefaultId = IfrDefault->DefaultId;
2236 //
2237 // Prepare new DefaultValue
2238 //
2239 DefaultData.Type = DefaultValueFromOpcode;
2240 DefaultData.DefaultId = VarDefaultId;
2241 CopyMem (&DefaultData.Value, &IfrDefault->Value, IfrDefault->Header.Length - OFFSET_OF (EFI_IFR_DEFAULT, Value));
2242
2243 // If the value field is expression, set the cleaned flag.
2244 if (IfrDefault->Type == EFI_IFR_TYPE_OTHER) {
2245 DefaultData.Cleaned = TRUE;
2246 }
2247 //
2248 // Add DefaultValue into current BlockData
2249 //
2250 InsertDefaultValue (BlockData, &DefaultData);
2251
2252 //
2253 // After insert the default value, reset the cleaned value for next
2254 // time used. If not set here, need to set the value before everytime
2255 // use it.
2256 //
2257 DefaultData.Cleaned = FALSE;
2258 break;
2259
2260 case EFI_IFR_END_OP:
2261 //
2262 // End Opcode is for Var question.
2263 //
2264 if (BlockData != NULL) {
2265 if (BlockData->Scope > 0) {
2266 BlockData->Scope--;
2267 }
2268 if (BlockData->Scope == 0) {
2269 BlockData = NULL;
2270 }
2271 }
2272
2273 break;
2274
2275 default:
2276 if (BlockData != NULL) {
2277 if (BlockData->Scope > 0) {
2278 BlockData->Scope = (UINT8) (BlockData->Scope + IfrOpHdr->Scope);
2279 }
2280
2281 if (BlockData->Scope == 0) {
2282 BlockData = NULL;
2283 }
2284 }
2285 break;
2286 }
2287
2288 IfrOffset += IfrOpHdr->Length;
2289 PackageOffset += IfrOpHdr->Length;
2290 }
2291
2292 Done:
2293 for (LinkData = VarStorageData->BlockEntry.ForwardLink; LinkData != &VarStorageData->BlockEntry; LinkData = LinkData->ForwardLink) {
2294 BlockData = BASE_CR (LinkData, IFR_BLOCK_DATA, Entry);
2295 for (LinkDefault = BlockData->DefaultValueEntry.ForwardLink; LinkDefault != &BlockData->DefaultValueEntry; ) {
2296 DefaultDataPtr = BASE_CR (LinkDefault, IFR_DEFAULT_DATA, Entry);
2297 LinkDefault = LinkDefault->ForwardLink;
2298 if (DefaultDataPtr->Cleaned == TRUE) {
2299 RemoveEntryList (&DefaultDataPtr->Entry);
2300 FreePool (DefaultDataPtr);
2301 }
2302 }
2303 }
2304
2305 return Status;
2306 }
2307
2308 /**
2309 parse the configrequest string, get the elements.
2310
2311 @param ConfigRequest The input configrequest string.
2312 @param Progress Return the progress data.
2313
2314 @retval Block data pointer.
2315 **/
2316 IFR_BLOCK_DATA *
2317 GetBlockElement (
2318 IN EFI_STRING ConfigRequest,
2319 OUT EFI_STRING *Progress
2320 )
2321 {
2322 EFI_STRING StringPtr;
2323 IFR_BLOCK_DATA *BlockData;
2324 IFR_BLOCK_DATA *RequestBlockArray;
2325 EFI_STATUS Status;
2326 UINT8 *TmpBuffer;
2327 UINT16 Offset;
2328 UINT16 Width;
2329 LIST_ENTRY *Link;
2330 IFR_BLOCK_DATA *NextBlockData;
2331 UINTN Length;
2332
2333 TmpBuffer = NULL;
2334
2335 //
2336 // Init RequestBlockArray
2337 //
2338 RequestBlockArray = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
2339 if (RequestBlockArray == NULL) {
2340 goto Done;
2341 }
2342 InitializeListHead (&RequestBlockArray->Entry);
2343
2344 //
2345 // Get the request Block array from the request string
2346 // Offset and Width
2347 //
2348
2349 //
2350 // Parse each <RequestElement> if exists
2351 // Only <BlockName> format is supported by this help function.
2352 // <BlockName> ::= &'OFFSET='<Number>&'WIDTH='<Number>
2353 //
2354 StringPtr = ConfigRequest;
2355 while (*StringPtr != 0 && StrnCmp (StringPtr, L"&OFFSET=", StrLen (L"&OFFSET=")) == 0) {
2356 //
2357 // Skip the OFFSET string
2358 //
2359 *Progress = StringPtr;
2360 StringPtr += StrLen (L"&OFFSET=");
2361 //
2362 // Get Offset
2363 //
2364 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
2365 if (EFI_ERROR (Status)) {
2366 goto Done;
2367 }
2368 Offset = 0;
2369 CopyMem (
2370 &Offset,
2371 TmpBuffer,
2372 (((Length + 1) / 2) < sizeof (UINT16)) ? ((Length + 1) / 2) : sizeof (UINT16)
2373 );
2374 FreePool (TmpBuffer);
2375
2376 StringPtr += Length;
2377 if (StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
2378 goto Done;
2379 }
2380 StringPtr += StrLen (L"&WIDTH=");
2381
2382 //
2383 // Get Width
2384 //
2385 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
2386 if (EFI_ERROR (Status)) {
2387 goto Done;
2388 }
2389 Width = 0;
2390 CopyMem (
2391 &Width,
2392 TmpBuffer,
2393 (((Length + 1) / 2) < sizeof (UINT16)) ? ((Length + 1) / 2) : sizeof (UINT16)
2394 );
2395 FreePool (TmpBuffer);
2396
2397 StringPtr += Length;
2398 if (*StringPtr != 0 && *StringPtr != L'&') {
2399 goto Done;
2400 }
2401
2402 //
2403 // Set Block Data
2404 //
2405 BlockData = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
2406 if (BlockData == NULL) {
2407 goto Done;
2408 }
2409 BlockData->Offset = Offset;
2410 BlockData->Width = Width;
2411 InsertBlockData (&RequestBlockArray->Entry, &BlockData);
2412
2413 //
2414 // Skip &VALUE string if &VALUE does exists.
2415 //
2416 if (StrnCmp (StringPtr, L"&VALUE=", StrLen (L"&VALUE=")) == 0) {
2417 StringPtr += StrLen (L"&VALUE=");
2418
2419 //
2420 // Get Value
2421 //
2422 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
2423 if (EFI_ERROR (Status)) {
2424 goto Done;
2425 }
2426
2427 StringPtr += Length;
2428 if (*StringPtr != 0 && *StringPtr != L'&') {
2429 goto Done;
2430 }
2431 }
2432 //
2433 // If '\0', parsing is finished.
2434 //
2435 if (*StringPtr == 0) {
2436 break;
2437 }
2438 }
2439
2440 //
2441 // Merge the requested block data.
2442 //
2443 Link = RequestBlockArray->Entry.ForwardLink;
2444 while ((Link != &RequestBlockArray->Entry) && (Link->ForwardLink != &RequestBlockArray->Entry)) {
2445 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
2446 NextBlockData = BASE_CR (Link->ForwardLink, IFR_BLOCK_DATA, Entry);
2447 if ((NextBlockData->Offset >= BlockData->Offset) && (NextBlockData->Offset <= (BlockData->Offset + BlockData->Width))) {
2448 if ((NextBlockData->Offset + NextBlockData->Width) > (BlockData->Offset + BlockData->Width)) {
2449 BlockData->Width = (UINT16) (NextBlockData->Offset + NextBlockData->Width - BlockData->Offset);
2450 }
2451 RemoveEntryList (Link->ForwardLink);
2452 FreePool (NextBlockData);
2453 continue;
2454 }
2455 Link = Link->ForwardLink;
2456 }
2457
2458 return RequestBlockArray;
2459
2460 Done:
2461 if (RequestBlockArray != NULL) {
2462 //
2463 // Free Link Array RequestBlockArray
2464 //
2465 while (!IsListEmpty (&RequestBlockArray->Entry)) {
2466 BlockData = BASE_CR (RequestBlockArray->Entry.ForwardLink, IFR_BLOCK_DATA, Entry);
2467 RemoveEntryList (&BlockData->Entry);
2468 FreePool (BlockData);
2469 }
2470
2471 FreePool (RequestBlockArray);
2472 }
2473
2474 return NULL;
2475 }
2476
2477 /**
2478 parse the configrequest string, get the elements.
2479
2480 @param ConfigRequest The input config request string.
2481 @param Progress Return the progress data.
2482
2483 @retval return data block array.
2484 **/
2485 IFR_BLOCK_DATA *
2486 GetNameElement (
2487 IN EFI_STRING ConfigRequest,
2488 OUT EFI_STRING *Progress
2489 )
2490 {
2491 EFI_STRING StringPtr;
2492 EFI_STRING NextTag;
2493 IFR_BLOCK_DATA *BlockData;
2494 IFR_BLOCK_DATA *RequestBlockArray;
2495 BOOLEAN HasValue;
2496
2497 StringPtr = ConfigRequest;
2498
2499 //
2500 // Init RequestBlockArray
2501 //
2502 RequestBlockArray = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
2503 if (RequestBlockArray == NULL) {
2504 goto Done;
2505 }
2506 InitializeListHead (&RequestBlockArray->Entry);
2507
2508 //
2509 // Get the request Block array from the request string
2510 //
2511
2512 //
2513 // Parse each <RequestElement> if exists
2514 // Only <BlockName> format is supported by this help function.
2515 // <BlockName> ::= &'Name***=***
2516 //
2517 while (StringPtr != NULL && *StringPtr == L'&') {
2518
2519 *Progress = StringPtr;
2520 //
2521 // Skip the L"&" string
2522 //
2523 StringPtr += 1;
2524
2525 HasValue = FALSE;
2526 if ((NextTag = StrStr (StringPtr, L"=")) != NULL) {
2527 *NextTag = L'\0';
2528 HasValue = TRUE;
2529 } else if ((NextTag = StrStr (StringPtr, L"&")) != NULL) {
2530 *NextTag = L'\0';
2531 }
2532
2533 //
2534 // Set Block Data
2535 //
2536 BlockData = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
2537 if (BlockData == NULL) {
2538 goto Done;
2539 }
2540
2541 //
2542 // Get Name
2543 //
2544 BlockData->Name = AllocateCopyPool(StrSize (StringPtr), StringPtr);
2545 InsertBlockData (&RequestBlockArray->Entry, &BlockData);
2546
2547 if (HasValue) {
2548 //
2549 // If has value, skip the value.
2550 //
2551 StringPtr = NextTag + 1;
2552 *NextTag = L'=';
2553 StringPtr = StrStr (StringPtr, L"&");
2554 } else if (NextTag != NULL) {
2555 //
2556 // restore the '&' text.
2557 //
2558 StringPtr = NextTag;
2559 *NextTag = L'&';
2560 }
2561 }
2562
2563 return RequestBlockArray;
2564
2565 Done:
2566 if (RequestBlockArray != NULL) {
2567 //
2568 // Free Link Array RequestBlockArray
2569 //
2570 while (!IsListEmpty (&RequestBlockArray->Entry)) {
2571 BlockData = BASE_CR (RequestBlockArray->Entry.ForwardLink, IFR_BLOCK_DATA, Entry);
2572 RemoveEntryList (&BlockData->Entry);
2573 if (BlockData->Name != NULL) {
2574 FreePool (BlockData->Name);
2575 }
2576 FreePool (BlockData);
2577 }
2578
2579 FreePool (RequestBlockArray);
2580 }
2581
2582 return NULL;
2583 }
2584
2585 /**
2586 Generate ConfigRequest string base on the varstore info.
2587
2588 @param ConfigHdr The config header for this varstore.
2589 @param VarStorageData The varstore info.
2590 @param Status Return Status.
2591 @param ConfigRequest The ConfigRequest info may be return.
2592
2593 @retval TRUE Need to continue
2594 @retval Others NO need to continue or error occur.
2595 **/
2596 BOOLEAN
2597 GenerateConfigRequest (
2598 IN CHAR16 *ConfigHdr,
2599 IN IFR_VARSTORAGE_DATA *VarStorageData,
2600 OUT EFI_STATUS *Status,
2601 IN OUT EFI_STRING *ConfigRequest
2602 )
2603 {
2604 BOOLEAN DataExist;
2605 UINTN Length;
2606 LIST_ENTRY *Link;
2607 CHAR16 *FullConfigRequest;
2608 CHAR16 *StringPtr;
2609 IFR_BLOCK_DATA *BlockData;
2610
2611 //
2612 // Append VarStorageData BlockEntry into *Request string
2613 // Now support only one varstore in a form package.
2614 //
2615
2616 //
2617 // Go through all VarStorageData Entry and get BlockEntry for each one for the multiple varstore in a single form package
2618 // Then construct them all to return MultiRequest string : ConfigHdr BlockConfig
2619 //
2620
2621 //
2622 // Compute the length of the entire request starting with <ConfigHdr> and a
2623 // Null-terminator
2624 //
2625 DataExist = FALSE;
2626 Length = StrLen (ConfigHdr) + 1;
2627
2628 for (Link = VarStorageData->BlockEntry.ForwardLink; Link != &VarStorageData->BlockEntry; Link = Link->ForwardLink) {
2629 DataExist = TRUE;
2630 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
2631 if (VarStorageData->Type == EFI_HII_VARSTORE_NAME_VALUE) {
2632 //
2633 // Add <BlockName> length for each Name
2634 //
2635 // <BlockName> ::= &Name1&Name2&...
2636 // |1| StrLen(Name1)
2637 //
2638 Length = Length + (1 + StrLen (BlockData->Name));
2639 } else {
2640 //
2641 // Add <BlockName> length for each Offset/Width pair
2642 //
2643 // <BlockName> ::= &OFFSET=1234&WIDTH=1234
2644 // | 8 | 4 | 7 | 4 |
2645 //
2646 Length = Length + (8 + 4 + 7 + 4);
2647 }
2648 }
2649 //
2650 // No any request block data is found. The request string can't be constructed.
2651 //
2652 if (!DataExist) {
2653 *Status = EFI_SUCCESS;
2654 return FALSE;
2655 }
2656
2657 //
2658 // Allocate buffer for the entire <ConfigRequest>
2659 //
2660 FullConfigRequest = AllocateZeroPool (Length * sizeof (CHAR16));
2661 if (FullConfigRequest == NULL) {
2662 *Status = EFI_OUT_OF_RESOURCES;
2663 return FALSE;
2664 }
2665 StringPtr = FullConfigRequest;
2666
2667 //
2668 // Start with <ConfigHdr>
2669 //
2670 StrCpyS (StringPtr, Length, ConfigHdr);
2671 StringPtr += StrLen (StringPtr);
2672
2673 //
2674 // Loop through all the Offset/Width pairs and append them to ConfigRequest
2675 //
2676 for (Link = VarStorageData->BlockEntry.ForwardLink; Link != &VarStorageData->BlockEntry; Link = Link->ForwardLink) {
2677 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
2678 if (VarStorageData->Type == EFI_HII_VARSTORE_NAME_VALUE) {
2679 //
2680 // Append &Name1\0
2681 //
2682 UnicodeSPrint (
2683 StringPtr,
2684 (1 + StrLen (BlockData->Name) + 1) * sizeof (CHAR16),
2685 L"&%s",
2686 BlockData->Name
2687 );
2688 } else {
2689 //
2690 // Append &OFFSET=XXXX&WIDTH=YYYY\0
2691 //
2692 UnicodeSPrint (
2693 StringPtr,
2694 (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16),
2695 L"&OFFSET=%04X&WIDTH=%04X",
2696 BlockData->Offset,
2697 BlockData->Width
2698 );
2699 }
2700 StringPtr += StrLen (StringPtr);
2701 }
2702 //
2703 // Set to the got full request string.
2704 //
2705 HiiToLower (FullConfigRequest);
2706
2707 if (*ConfigRequest != NULL) {
2708 FreePool (*ConfigRequest);
2709 }
2710 *ConfigRequest = FullConfigRequest;
2711
2712 return TRUE;
2713 }
2714
2715 /**
2716 Generate ConfigRequest Header base on the varstore info.
2717
2718 @param VarStorageData The varstore info.
2719 @param DevicePath Device path for this varstore.
2720 @param ConfigHdr The config header for this varstore.
2721
2722 @retval EFI_SUCCESS Generate the header success.
2723 @retval EFI_OUT_OF_RESOURCES Allocate buffer fail.
2724 **/
2725 EFI_STATUS
2726 GenerateHdr (
2727 IN IFR_VARSTORAGE_DATA *VarStorageData,
2728 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
2729 OUT EFI_STRING *ConfigHdr
2730 )
2731 {
2732 EFI_STRING GuidStr;
2733 EFI_STRING NameStr;
2734 EFI_STRING PathStr;
2735 UINTN Length;
2736 EFI_STATUS Status;
2737
2738 Status = EFI_SUCCESS;
2739 NameStr = NULL;
2740 GuidStr = NULL;
2741 PathStr = NULL;
2742
2743 //
2744 // Construct <ConfigHdr> : "GUID=...&NAME=...&PATH=..." by VarStorageData Guid, Name and DriverHandle
2745 //
2746 GenerateSubStr (L"GUID=", sizeof (EFI_GUID), (VOID *) &VarStorageData->Guid, 1, &GuidStr);
2747 if (VarStorageData->Name != NULL) {
2748 GenerateSubStr (L"NAME=", StrLen (VarStorageData->Name) * sizeof (CHAR16), (VOID *) VarStorageData->Name, 2, &NameStr);
2749 } else {
2750 GenerateSubStr (L"NAME=", 0, NULL, 2, &NameStr);
2751 }
2752 GenerateSubStr (
2753 L"PATH=",
2754 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) DevicePath),
2755 (VOID *) DevicePath,
2756 1,
2757 &PathStr
2758 );
2759 Length = StrLen (GuidStr) + StrLen (NameStr) + StrLen (PathStr) + 1;
2760 if (VarStorageData->Name == NULL) {
2761 Length += 1;
2762 }
2763
2764 *ConfigHdr = AllocateZeroPool (Length * sizeof (CHAR16));
2765 if (*ConfigHdr == NULL) {
2766 Status = EFI_OUT_OF_RESOURCES;
2767 goto Done;
2768 }
2769 StrCpyS (*ConfigHdr, Length, GuidStr);
2770 StrCatS (*ConfigHdr, Length, NameStr);
2771 if (VarStorageData->Name == NULL) {
2772 StrCatS (*ConfigHdr, Length, L"&");
2773 }
2774 StrCatS (*ConfigHdr, Length, PathStr);
2775
2776 //
2777 // Remove the last character L'&'
2778 //
2779 *(*ConfigHdr + StrLen (*ConfigHdr) - 1) = L'\0';
2780
2781 Done:
2782 if (GuidStr != NULL) {
2783 FreePool (GuidStr);
2784 }
2785
2786 if (NameStr != NULL) {
2787 FreePool (NameStr);
2788 }
2789
2790 if (PathStr != NULL) {
2791 FreePool (PathStr);
2792 }
2793
2794 return Status;
2795 }
2796
2797 /**
2798 Get Data buffer size based on data type.
2799
2800 @param ValueType The input data type.
2801
2802 @retval The data buffer size for the input type.
2803 **/
2804 UINT16
2805 GetStorageWidth (
2806 IN UINT8 ValueType
2807 )
2808 {
2809 UINT16 StorageWidth;
2810
2811 switch (ValueType) {
2812 case EFI_IFR_NUMERIC_SIZE_1:
2813 case EFI_IFR_TYPE_BOOLEAN:
2814 StorageWidth = (UINT16) sizeof (UINT8);
2815 break;
2816
2817 case EFI_IFR_NUMERIC_SIZE_2:
2818 StorageWidth = (UINT16) sizeof (UINT16);
2819 break;
2820
2821 case EFI_IFR_NUMERIC_SIZE_4:
2822 StorageWidth = (UINT16) sizeof (UINT32);
2823 break;
2824
2825 case EFI_IFR_NUMERIC_SIZE_8:
2826 StorageWidth = (UINT16) sizeof (UINT64);
2827 break;
2828
2829 case EFI_IFR_TYPE_TIME:
2830 StorageWidth = (UINT16) sizeof (EFI_IFR_TIME);
2831 break;
2832
2833 case EFI_IFR_TYPE_DATE:
2834 StorageWidth = (UINT16) sizeof (EFI_IFR_DATE);
2835 break;
2836
2837 default:
2838 StorageWidth = 0;
2839 break;
2840 }
2841
2842 return StorageWidth;
2843 }
2844
2845 /**
2846 Generate ConfigAltResp string base on the varstore info.
2847
2848 @param HiiHandle Hii Handle for this hii package.
2849 @param ConfigHdr The config header for this varstore.
2850 @param VarStorageData The varstore info.
2851 @param DefaultIdArray The Default id array.
2852 @param DefaultAltCfgResp The DefaultAltCfgResp info may be return.
2853
2854 @retval TRUE Need to continue
2855 @retval Others NO need to continue or error occur.
2856 **/
2857 EFI_STATUS
2858 GenerateAltConfigResp (
2859 IN EFI_HII_HANDLE HiiHandle,
2860 IN CHAR16 *ConfigHdr,
2861 IN IFR_VARSTORAGE_DATA *VarStorageData,
2862 IN IFR_DEFAULT_DATA *DefaultIdArray,
2863 IN OUT EFI_STRING *DefaultAltCfgResp
2864 )
2865 {
2866 BOOLEAN DataExist;
2867 UINTN Length;
2868 LIST_ENTRY *Link;
2869 LIST_ENTRY *LinkData;
2870 LIST_ENTRY *LinkDefault;
2871 LIST_ENTRY *ListEntry;
2872 CHAR16 *StringPtr;
2873 IFR_BLOCK_DATA *BlockData;
2874 IFR_DEFAULT_DATA *DefaultId;
2875 IFR_DEFAULT_DATA *DefaultValueData;
2876 UINTN Width;
2877 UINT8 *TmpBuffer;
2878 CHAR16 *DefaultString;
2879
2880 BlockData = NULL;
2881 DataExist = FALSE;
2882 DefaultString = NULL;
2883 //
2884 // Add length for <ConfigHdr> + '\0'
2885 //
2886 Length = StrLen (ConfigHdr) + 1;
2887
2888 for (Link = DefaultIdArray->Entry.ForwardLink; Link != &DefaultIdArray->Entry; Link = Link->ForwardLink) {
2889 DefaultId = BASE_CR (Link, IFR_DEFAULT_DATA, Entry);
2890 //
2891 // Add length for "&<ConfigHdr>&ALTCFG=XXXX"
2892 // |1| StrLen (ConfigHdr) | 8 | 4 |
2893 //
2894 Length += (1 + StrLen (ConfigHdr) + 8 + 4);
2895
2896 for (LinkData = VarStorageData->BlockEntry.ForwardLink; LinkData != &VarStorageData->BlockEntry; LinkData = LinkData->ForwardLink) {
2897 BlockData = BASE_CR (LinkData, IFR_BLOCK_DATA, Entry);
2898 ListEntry = &BlockData->DefaultValueEntry;
2899 for (LinkDefault = ListEntry->ForwardLink; LinkDefault != ListEntry; LinkDefault = LinkDefault->ForwardLink) {
2900 DefaultValueData = BASE_CR (LinkDefault, IFR_DEFAULT_DATA, Entry);
2901 if (DefaultValueData->DefaultId != DefaultId->DefaultId) {
2902 continue;
2903 }
2904 if (VarStorageData->Type == EFI_HII_VARSTORE_NAME_VALUE) {
2905 //
2906 // Add length for "&Name1=zzzzzzzzzzzz"
2907 // |1|Name|1|Value|
2908 //
2909 Length += (1 + StrLen (BlockData->Name) + 1 + BlockData->Width * 2);
2910 } else {
2911 //
2912 // Add length for "&OFFSET=XXXX&WIDTH=YYYY&VALUE=zzzzzzzzzzzz"
2913 // | 8 | 4 | 7 | 4 | 7 | Width * 2 |
2914 //
2915 Length += (8 + 4 + 7 + 4 + 7 + BlockData->Width * 2);
2916 }
2917 DataExist = TRUE;
2918 }
2919 }
2920 }
2921
2922 //
2923 // No default value is found. The default string doesn't exist.
2924 //
2925 if (!DataExist) {
2926 return EFI_SUCCESS;
2927 }
2928
2929 //
2930 // Allocate buffer for the entire <DefaultAltCfgResp>
2931 //
2932 *DefaultAltCfgResp = AllocateZeroPool (Length * sizeof (CHAR16));
2933 if (*DefaultAltCfgResp == NULL) {
2934 return EFI_OUT_OF_RESOURCES;
2935 }
2936 StringPtr = *DefaultAltCfgResp;
2937
2938 //
2939 // Start with <ConfigHdr>
2940 //
2941 StrCpyS (StringPtr, Length, ConfigHdr);
2942 StringPtr += StrLen (StringPtr);
2943
2944 for (Link = DefaultIdArray->Entry.ForwardLink; Link != &DefaultIdArray->Entry; Link = Link->ForwardLink) {
2945 DefaultId = BASE_CR (Link, IFR_DEFAULT_DATA, Entry);
2946 //
2947 // Add <AltConfigHdr> of the form "&<ConfigHdr>&ALTCFG=XXXX\0"
2948 // |1| StrLen (ConfigHdr) | 8 | 4 |
2949 //
2950 UnicodeSPrint (
2951 StringPtr,
2952 (1 + StrLen (ConfigHdr) + 8 + 4 + 1) * sizeof (CHAR16),
2953 L"&%s&ALTCFG=%04X",
2954 ConfigHdr,
2955 DefaultId->DefaultId
2956 );
2957 StringPtr += StrLen (StringPtr);
2958
2959 for (LinkData = VarStorageData->BlockEntry.ForwardLink; LinkData != &VarStorageData->BlockEntry; LinkData = LinkData->ForwardLink) {
2960 BlockData = BASE_CR (LinkData, IFR_BLOCK_DATA, Entry);
2961 ListEntry = &BlockData->DefaultValueEntry;
2962 for (LinkDefault = ListEntry->ForwardLink; LinkDefault != ListEntry; LinkDefault = LinkDefault->ForwardLink) {
2963 DefaultValueData = BASE_CR (LinkDefault, IFR_DEFAULT_DATA, Entry);
2964 if (DefaultValueData->DefaultId != DefaultId->DefaultId) {
2965 continue;
2966 }
2967 if (VarStorageData->Type == EFI_HII_VARSTORE_NAME_VALUE) {
2968 UnicodeSPrint (
2969 StringPtr,
2970 (1 + StrLen (ConfigHdr) + 1) * sizeof (CHAR16),
2971 L"&%s=",
2972 BlockData->Name
2973 );
2974 StringPtr += StrLen (StringPtr);
2975 } else {
2976 //
2977 // Add <BlockConfig>
2978 // <BlockConfig> ::= 'OFFSET='<Number>&'WIDTH='<Number>&'VALUE'=<Number>
2979 //
2980 UnicodeSPrint (
2981 StringPtr,
2982 (8 + 4 + 7 + 4 + 7 + 1) * sizeof (CHAR16),
2983 L"&OFFSET=%04X&WIDTH=%04X&VALUE=",
2984 BlockData->Offset,
2985 BlockData->Width
2986 );
2987 StringPtr += StrLen (StringPtr);
2988 }
2989 Width = BlockData->Width;
2990 //
2991 // Convert Value to a hex string in "%x" format
2992 // NOTE: This is in the opposite byte that GUID and PATH use
2993 //
2994 if (BlockData->OpCode == EFI_IFR_STRING_OP){
2995 DefaultString = InternalGetString(HiiHandle, DefaultValueData->Value.string);
2996 TmpBuffer = (UINT8 *) DefaultString;
2997 } else {
2998 TmpBuffer = (UINT8 *) &(DefaultValueData->Value);
2999 }
3000 for (; Width > 0 && (TmpBuffer != NULL); Width--) {
3001 StringPtr += UnicodeValueToString (StringPtr, PREFIX_ZERO | RADIX_HEX, TmpBuffer[Width - 1], 2);
3002 }
3003 if (DefaultString != NULL){
3004 FreePool(DefaultString);
3005 DefaultString = NULL;
3006 }
3007 }
3008 }
3009 }
3010
3011 HiiToLower (*DefaultAltCfgResp);
3012
3013 return EFI_SUCCESS;
3014 }
3015
3016 /**
3017 This function gets the full request string and full default value string by
3018 parsing IFR data in HII form packages.
3019
3020 When Request points to NULL string, the request string and default value string
3021 for each varstore in form package will return.
3022
3023 @param DataBaseRecord The DataBaseRecord instance contains the found Hii handle and package.
3024 @param DevicePath Device Path which Hii Config Access Protocol is registered.
3025 @param Request Pointer to a null-terminated Unicode string in
3026 <ConfigRequest> format. When it doesn't contain
3027 any RequestElement, it will be updated to return
3028 the full RequestElement retrieved from IFR data.
3029 If it points to NULL, the request string for the first
3030 varstore in form package will be merged into a
3031 <MultiConfigRequest> format string and return.
3032 @param AltCfgResp Pointer to a null-terminated Unicode string in
3033 <ConfigAltResp> format. When the pointer is to NULL,
3034 the full default value string retrieved from IFR data
3035 will return. When the pinter is to a string, the
3036 full default value string retrieved from IFR data
3037 will be merged into the input string and return.
3038 When Request points to NULL, the default value string
3039 for each varstore in form package will be merged into
3040 a <MultiConfigAltResp> format string and return.
3041 @param PointerProgress Optional parameter, it can be be NULL.
3042 When it is not NULL, if Request is NULL, it returns NULL.
3043 On return, points to a character in the Request
3044 string. Points to the string's null terminator if
3045 request was successful. Points to the most recent
3046 & before the first failing name / value pair (or
3047 the beginning of the string if the failure is in
3048 the first name / value pair) if the request was
3049 not successful.
3050 @retval EFI_SUCCESS The Results string is set to the full request string.
3051 And AltCfgResp contains all default value string.
3052 @retval EFI_OUT_OF_RESOURCES Not enough memory for the return string.
3053 @retval EFI_NOT_FOUND The varstore (Guid and Name) in Request string
3054 can't be found in Form package.
3055 @retval EFI_NOT_FOUND HiiPackage can't be got on the input HiiHandle.
3056 @retval EFI_INVALID_PARAMETER Request points to NULL.
3057
3058 **/
3059 EFI_STATUS
3060 EFIAPI
3061 GetFullStringFromHiiFormPackages (
3062 IN HII_DATABASE_RECORD *DataBaseRecord,
3063 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
3064 IN OUT EFI_STRING *Request,
3065 IN OUT EFI_STRING *AltCfgResp,
3066 OUT EFI_STRING *PointerProgress OPTIONAL
3067 )
3068 {
3069 EFI_STATUS Status;
3070 UINT8 *HiiFormPackage;
3071 UINTN PackageSize;
3072 IFR_BLOCK_DATA *RequestBlockArray;
3073 IFR_BLOCK_DATA *BlockData;
3074 IFR_DEFAULT_DATA *DefaultValueData;
3075 IFR_DEFAULT_DATA *DefaultId;
3076 IFR_DEFAULT_DATA *DefaultIdArray;
3077 IFR_VARSTORAGE_DATA *VarStorageData;
3078 EFI_STRING DefaultAltCfgResp;
3079 EFI_STRING ConfigHdr;
3080 EFI_STRING StringPtr;
3081 EFI_STRING Progress;
3082
3083 if (DataBaseRecord == NULL || DevicePath == NULL || Request == NULL || AltCfgResp == NULL) {
3084 return EFI_INVALID_PARAMETER;
3085 }
3086
3087 //
3088 // Initialize the local variables.
3089 //
3090 RequestBlockArray = NULL;
3091 DefaultIdArray = NULL;
3092 VarStorageData = NULL;
3093 DefaultAltCfgResp = NULL;
3094 ConfigHdr = NULL;
3095 HiiFormPackage = NULL;
3096 PackageSize = 0;
3097 Progress = *Request;
3098
3099 Status = GetFormPackageData (DataBaseRecord, &HiiFormPackage, &PackageSize);
3100 if (EFI_ERROR (Status)) {
3101 goto Done;
3102 }
3103
3104 //
3105 // 1. Get the request block array by Request String when Request string containts the block array.
3106 //
3107 StringPtr = NULL;
3108 if (*Request != NULL) {
3109 StringPtr = *Request;
3110 //
3111 // Jump <ConfigHdr>
3112 //
3113 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
3114 Status = EFI_INVALID_PARAMETER;
3115 goto Done;
3116 }
3117 StringPtr += StrLen (L"GUID=");
3118 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&NAME=", StrLen (L"&NAME=")) != 0) {
3119 StringPtr++;
3120 }
3121 if (*StringPtr == L'\0') {
3122 Status = EFI_INVALID_PARAMETER;
3123 goto Done;
3124 }
3125 StringPtr += StrLen (L"&NAME=");
3126 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&PATH=", StrLen (L"&PATH=")) != 0) {
3127 StringPtr++;
3128 }
3129 if (*StringPtr == L'\0') {
3130 Status = EFI_INVALID_PARAMETER;
3131 goto Done;
3132 }
3133 StringPtr += StrLen (L"&PATH=");
3134 while (*StringPtr != L'\0' && *StringPtr != L'&') {
3135 StringPtr ++;
3136 }
3137
3138 if (*StringPtr == L'\0') {
3139 //
3140 // No request block is found.
3141 //
3142 StringPtr = NULL;
3143 }
3144 }
3145
3146 //
3147 // If StringPtr != NULL, get the request elements.
3148 //
3149 if (StringPtr != NULL) {
3150 if (StrStr (StringPtr, L"&OFFSET=") != NULL) {
3151 RequestBlockArray = GetBlockElement(StringPtr, &Progress);
3152 } else {
3153 RequestBlockArray = GetNameElement(StringPtr, &Progress);
3154 }
3155
3156 if (RequestBlockArray == NULL) {
3157 Status = EFI_INVALID_PARAMETER;
3158 goto Done;
3159 }
3160 }
3161
3162 //
3163 // Initialize DefaultIdArray to store the map between DeaultId and DefaultName
3164 //
3165 DefaultIdArray = (IFR_DEFAULT_DATA *) AllocateZeroPool (sizeof (IFR_DEFAULT_DATA));
3166 if (DefaultIdArray == NULL) {
3167 Status = EFI_OUT_OF_RESOURCES;
3168 goto Done;
3169 }
3170 InitializeListHead (&DefaultIdArray->Entry);
3171
3172 //
3173 // Initialize VarStorageData to store the var store Block and Default value information.
3174 //
3175 VarStorageData = (IFR_VARSTORAGE_DATA *) AllocateZeroPool (sizeof (IFR_VARSTORAGE_DATA));
3176 if (VarStorageData == NULL) {
3177 Status = EFI_OUT_OF_RESOURCES;
3178 goto Done;
3179 }
3180 InitializeListHead (&VarStorageData->Entry);
3181 InitializeListHead (&VarStorageData->BlockEntry);
3182
3183 //
3184 // 2. Parse FormPackage to get BlockArray and DefaultId Array for the request BlockArray.
3185 //
3186
3187 //
3188 // Parse the opcode in form pacakge to get the default setting.
3189 //
3190 Status = ParseIfrData (DataBaseRecord->Handle,
3191 HiiFormPackage,
3192 (UINT32) PackageSize,
3193 *Request,
3194 RequestBlockArray,
3195 VarStorageData,
3196 DefaultIdArray);
3197 if (EFI_ERROR (Status)) {
3198 goto Done;
3199 }
3200
3201 //
3202 // No requested varstore in IFR data and directly return
3203 //
3204 if (VarStorageData->Type == 0 && VarStorageData->Name == NULL) {
3205 Status = EFI_SUCCESS;
3206 goto Done;
3207 }
3208
3209 //
3210 // 3. Construct Request Element (Block Name) for 2.1 and 2.2 case.
3211 //
3212 Status = GenerateHdr (VarStorageData, DevicePath, &ConfigHdr);
3213 if (EFI_ERROR (Status)) {
3214 goto Done;
3215 }
3216
3217 if (RequestBlockArray == NULL) {
3218 if (!GenerateConfigRequest(ConfigHdr, VarStorageData, &Status, Request)) {
3219 goto Done;
3220 }
3221 }
3222
3223 //
3224 // 4. Construct Default Value string in AltResp according to request element.
3225 // Go through all VarStorageData Entry and get the DefaultId array for each one
3226 // Then construct them all to : ConfigHdr AltConfigHdr ConfigBody AltConfigHdr ConfigBody
3227 //
3228 Status = GenerateAltConfigResp (DataBaseRecord->Handle,ConfigHdr, VarStorageData, DefaultIdArray, &DefaultAltCfgResp);
3229 if (EFI_ERROR (Status)) {
3230 goto Done;
3231 }
3232
3233 //
3234 // 5. Merge string into the input AltCfgResp if the iput *AltCfgResp is not NULL.
3235 //
3236 if (*AltCfgResp != NULL && DefaultAltCfgResp != NULL) {
3237 Status = MergeDefaultString (AltCfgResp, DefaultAltCfgResp);
3238 FreePool (DefaultAltCfgResp);
3239 } else if (*AltCfgResp == NULL) {
3240 *AltCfgResp = DefaultAltCfgResp;
3241 }
3242
3243 Done:
3244 if (RequestBlockArray != NULL) {
3245 //
3246 // Free Link Array RequestBlockArray
3247 //
3248 while (!IsListEmpty (&RequestBlockArray->Entry)) {
3249 BlockData = BASE_CR (RequestBlockArray->Entry.ForwardLink, IFR_BLOCK_DATA, Entry);
3250 RemoveEntryList (&BlockData->Entry);
3251 if (BlockData->Name != NULL) {
3252 FreePool (BlockData->Name);
3253 }
3254 FreePool (BlockData);
3255 }
3256
3257 FreePool (RequestBlockArray);
3258 }
3259
3260 if (VarStorageData != NULL) {
3261 //
3262 // Free link array VarStorageData
3263 //
3264 while (!IsListEmpty (&VarStorageData->BlockEntry)) {
3265 BlockData = BASE_CR (VarStorageData->BlockEntry.ForwardLink, IFR_BLOCK_DATA, Entry);
3266 RemoveEntryList (&BlockData->Entry);
3267 if (BlockData->Name != NULL) {
3268 FreePool (BlockData->Name);
3269 }
3270 //
3271 // Free default value link array
3272 //
3273 while (!IsListEmpty (&BlockData->DefaultValueEntry)) {
3274 DefaultValueData = BASE_CR (BlockData->DefaultValueEntry.ForwardLink, IFR_DEFAULT_DATA, Entry);
3275 RemoveEntryList (&DefaultValueData->Entry);
3276 FreePool (DefaultValueData);
3277 }
3278 FreePool (BlockData);
3279 }
3280 FreePool (VarStorageData);
3281 }
3282
3283 if (DefaultIdArray != NULL) {
3284 //
3285 // Free DefaultId Array
3286 //
3287 while (!IsListEmpty (&DefaultIdArray->Entry)) {
3288 DefaultId = BASE_CR (DefaultIdArray->Entry.ForwardLink, IFR_DEFAULT_DATA, Entry);
3289 RemoveEntryList (&DefaultId->Entry);
3290 FreePool (DefaultId);
3291 }
3292 FreePool (DefaultIdArray);
3293 }
3294
3295 //
3296 // Free the allocated string
3297 //
3298 if (ConfigHdr != NULL) {
3299 FreePool (ConfigHdr);
3300 }
3301
3302 //
3303 // Free Pacakge data
3304 //
3305 if (HiiFormPackage != NULL) {
3306 FreePool (HiiFormPackage);
3307 }
3308
3309 if (PointerProgress != NULL) {
3310 if (*Request == NULL) {
3311 *PointerProgress = NULL;
3312 } else if (EFI_ERROR (Status)) {
3313 *PointerProgress = *Request;
3314 } else {
3315 *PointerProgress = *Request + StrLen (*Request);
3316 }
3317 }
3318
3319 return Status;
3320 }
3321
3322 /**
3323 This function gets the full request resp string by
3324 parsing IFR data in HII form packages.
3325
3326 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
3327 instance.
3328 @param EfiVarStoreInfo The efi varstore info which is save in the EFI
3329 varstore data structure.
3330 @param Request Pointer to a null-terminated Unicode string in
3331 <ConfigRequest> format.
3332 @param RequestResp Pointer to a null-terminated Unicode string in
3333 <ConfigResp> format.
3334 @param AccessProgress On return, points to a character in the Request
3335 string. Points to the string's null terminator if
3336 request was successful. Points to the most recent
3337 & before the first failing name / value pair (or
3338 the beginning of the string if the failure is in
3339 the first name / value pair) if the request was
3340 not successful.
3341
3342 @retval EFI_SUCCESS The Results string is set to the full request string.
3343 And AltCfgResp contains all default value string.
3344 @retval EFI_OUT_OF_RESOURCES Not enough memory for the return string.
3345 @retval EFI_INVALID_PARAMETER Request points to NULL.
3346
3347 **/
3348 EFI_STATUS
3349 GetConfigRespFromEfiVarStore (
3350 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
3351 IN EFI_IFR_VARSTORE_EFI *EfiVarStoreInfo,
3352 IN EFI_STRING Request,
3353 OUT EFI_STRING *RequestResp,
3354 OUT EFI_STRING *AccessProgress
3355 )
3356 {
3357 EFI_STATUS Status;
3358 EFI_STRING VarStoreName;
3359 UINT8 *VarStore;
3360 UINTN BufferSize;
3361
3362 Status = EFI_SUCCESS;
3363 BufferSize = 0;
3364 VarStore = NULL;
3365 VarStoreName = NULL;
3366 *AccessProgress = Request;
3367
3368 VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)EfiVarStoreInfo->Name) * sizeof (CHAR16));
3369 if (VarStoreName == NULL) {
3370 Status = EFI_OUT_OF_RESOURCES;
3371 goto Done;
3372 }
3373 AsciiStrToUnicodeStr ((CHAR8 *) EfiVarStoreInfo->Name, VarStoreName);
3374
3375
3376 Status = gRT->GetVariable (VarStoreName, &EfiVarStoreInfo->Guid, NULL, &BufferSize, NULL);
3377 if (Status != EFI_BUFFER_TOO_SMALL) {
3378 goto Done;
3379 }
3380
3381 VarStore = AllocateZeroPool (BufferSize);
3382 ASSERT (VarStore != NULL);
3383 Status = gRT->GetVariable (VarStoreName, &EfiVarStoreInfo->Guid, NULL, &BufferSize, VarStore);
3384 if (EFI_ERROR (Status)) {
3385 goto Done;
3386 }
3387
3388 Status = HiiBlockToConfig(This, Request, VarStore, BufferSize, RequestResp, AccessProgress);
3389 if (EFI_ERROR (Status)) {
3390 goto Done;
3391 }
3392
3393 Done:
3394 if (VarStoreName != NULL) {
3395 FreePool (VarStoreName);
3396 }
3397
3398 if (VarStore != NULL) {
3399 FreePool (VarStore);
3400 }
3401
3402 return Status;
3403 }
3404
3405
3406 /**
3407 This function route the full request resp string for efi varstore.
3408
3409 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
3410 instance.
3411 @param EfiVarStoreInfo The efi varstore info which is save in the EFI
3412 varstore data structure.
3413 @param RequestResp Pointer to a null-terminated Unicode string in
3414 <ConfigResp> format.
3415 @param Result Pointer to a null-terminated Unicode string in
3416 <ConfigResp> format.
3417
3418 @retval EFI_SUCCESS The Results string is set to the full request string.
3419 And AltCfgResp contains all default value string.
3420 @retval EFI_OUT_OF_RESOURCES Not enough memory for the return string.
3421 @retval EFI_INVALID_PARAMETER Request points to NULL.
3422
3423 **/
3424 EFI_STATUS
3425 RouteConfigRespForEfiVarStore (
3426 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
3427 IN EFI_IFR_VARSTORE_EFI *EfiVarStoreInfo,
3428 IN EFI_STRING RequestResp,
3429 OUT EFI_STRING *Result
3430 )
3431 {
3432 EFI_STATUS Status;
3433 EFI_STRING VarStoreName;
3434 UINT8 *VarStore;
3435 UINTN BufferSize;
3436 UINTN BlockSize;
3437
3438 Status = EFI_SUCCESS;
3439 BufferSize = 0;
3440 VarStore = NULL;
3441 VarStoreName = NULL;
3442
3443 VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)EfiVarStoreInfo->Name) * sizeof (CHAR16));
3444 if (VarStoreName == NULL) {
3445 Status = EFI_OUT_OF_RESOURCES;
3446 goto Done;
3447 }
3448 AsciiStrToUnicodeStr ((CHAR8 *) EfiVarStoreInfo->Name, VarStoreName);
3449
3450 Status = gRT->GetVariable (VarStoreName, &EfiVarStoreInfo->Guid, NULL, &BufferSize, NULL);
3451 if (Status != EFI_BUFFER_TOO_SMALL) {
3452 goto Done;
3453 }
3454
3455 BlockSize = BufferSize;
3456 VarStore = AllocateZeroPool (BufferSize);
3457 ASSERT (VarStore != NULL);
3458 Status = gRT->GetVariable (VarStoreName, &EfiVarStoreInfo->Guid, NULL, &BufferSize, VarStore);
3459 if (EFI_ERROR (Status)) {
3460 goto Done;
3461 }
3462
3463 Status = HiiConfigToBlock(This, RequestResp, VarStore, &BlockSize, Result);
3464 if (EFI_ERROR (Status)) {
3465 goto Done;
3466 }
3467
3468 Status = gRT->SetVariable (VarStoreName, &EfiVarStoreInfo->Guid, EfiVarStoreInfo->Attributes, BufferSize, VarStore);
3469 if (EFI_ERROR (Status)) {
3470 goto Done;
3471 }
3472
3473 Done:
3474 if (VarStoreName != NULL) {
3475 FreePool (VarStoreName);
3476 }
3477
3478 if (VarStore != NULL) {
3479 FreePool (VarStore);
3480 }
3481
3482 return Status;
3483 }
3484
3485 /**
3486 Validate the config request elements.
3487
3488 @param ConfigElements A null-terminated Unicode string in <ConfigRequest> format,
3489 without configHdr field.
3490
3491 @retval CHAR16 * THE first Name/value pair not correct.
3492 @retval NULL Success parse the name/value pair
3493 **/
3494 CHAR16 *
3495 OffsetWidthValidate (
3496 CHAR16 *ConfigElements
3497 )
3498 {
3499 CHAR16 *StringPtr;
3500 CHAR16 *RetVal;
3501
3502 StringPtr = ConfigElements;
3503
3504 while (1) {
3505 RetVal = StringPtr;
3506 if (StrnCmp (StringPtr, L"&OFFSET=", StrLen (L"&OFFSET=")) != 0) {
3507 return RetVal;
3508 }
3509
3510 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
3511 StringPtr++;
3512 }
3513 if (*StringPtr == L'\0') {
3514 return RetVal;
3515 }
3516
3517 StringPtr += StrLen (L"&WIDTH=");
3518 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&OFFSET=", StrLen (L"&OFFSET=")) != 0) {
3519 StringPtr ++;
3520 }
3521
3522 if (*StringPtr == L'\0') {
3523 return NULL;
3524 }
3525 }
3526 }
3527
3528 /**
3529 Validate the config request elements.
3530
3531 @param ConfigElements A null-terminated Unicode string in <ConfigRequest> format,
3532 without configHdr field.
3533
3534 @retval CHAR16 * THE first Name/value pair not correct.
3535 @retval NULL Success parse the name/value pair
3536
3537 **/
3538 CHAR16 *
3539 NameValueValidate (
3540 CHAR16 *ConfigElements
3541 )
3542 {
3543 CHAR16 *StringPtr;
3544 CHAR16 *RetVal;
3545
3546 StringPtr = ConfigElements;
3547
3548 while (1) {
3549 RetVal = StringPtr;
3550 if (*StringPtr != L'&') {
3551 return RetVal;
3552 }
3553 StringPtr += 1;
3554
3555 StringPtr = StrStr (StringPtr, L"&");
3556
3557 if (StringPtr == NULL) {
3558 return NULL;
3559 }
3560 }
3561 }
3562
3563 /**
3564 Validate the config request string.
3565
3566 @param ConfigRequest A null-terminated Unicode string in <ConfigRequest> format.
3567
3568 @retval CHAR16 * THE first element not correct.
3569 @retval NULL Success parse the name/value pair
3570
3571 **/
3572 CHAR16 *
3573 ConfigRequestValidate (
3574 CHAR16 *ConfigRequest
3575 )
3576 {
3577 BOOLEAN HasNameField;
3578 CHAR16 *StringPtr;
3579
3580 HasNameField = TRUE;
3581 StringPtr = ConfigRequest;
3582
3583 //
3584 // Check <ConfigHdr>
3585 //
3586 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
3587 return ConfigRequest;
3588 }
3589 StringPtr += StrLen (L"GUID=");
3590 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&NAME=", StrLen (L"&NAME=")) != 0) {
3591 StringPtr++;
3592 }
3593 if (*StringPtr == L'\0') {
3594 return ConfigRequest;
3595 }
3596 StringPtr += StrLen (L"&NAME=");
3597 if (*StringPtr == L'&') {
3598 HasNameField = FALSE;
3599 }
3600 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&PATH=", StrLen (L"&PATH=")) != 0) {
3601 StringPtr++;
3602 }
3603 if (*StringPtr == L'\0') {
3604 return ConfigRequest;
3605 }
3606 StringPtr += StrLen (L"&PATH=");
3607 while (*StringPtr != L'\0' && *StringPtr != L'&') {
3608 StringPtr ++;
3609 }
3610
3611 if (*StringPtr == L'\0') {
3612 return NULL;
3613 }
3614
3615 if (HasNameField) {
3616 //
3617 // Should be Buffer varstore, config request should be "OFFSET/Width" pairs.
3618 //
3619 return OffsetWidthValidate(StringPtr);
3620 } else {
3621 //
3622 // Should be Name/Value varstore, config request should be "&name1&name2..." pairs.
3623 //
3624 return NameValueValidate(StringPtr);
3625 }
3626 }
3627
3628 /**
3629 This function allows a caller to extract the current configuration
3630 for one or more named elements from one or more drivers.
3631
3632 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
3633 instance.
3634 @param Request A null-terminated Unicode string in
3635 <MultiConfigRequest> format.
3636 @param Progress On return, points to a character in the Request
3637 string. Points to the string's null terminator if
3638 request was successful. Points to the most recent
3639 & before the first failing name / value pair (or
3640 the beginning of the string if the failure is in
3641 the first name / value pair) if the request was
3642 not successful.
3643 @param Results Null-terminated Unicode string in
3644 <MultiConfigAltResp> format which has all values
3645 filled in for the names in the Request string.
3646 String to be allocated by the called function.
3647
3648 @retval EFI_SUCCESS The Results string is filled with the values
3649 corresponding to all requested names.
3650 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the parts of the
3651 results that must be stored awaiting possible
3652 future protocols.
3653 @retval EFI_NOT_FOUND Routing data doesn't match any known driver.
3654 Progress set to the "G" in "GUID" of the routing
3655 header that doesn't match. Note: There is no
3656 requirement that all routing data be validated
3657 before any configuration extraction.
3658 @retval EFI_INVALID_PARAMETER For example, passing in a NULL for the Request
3659 parameter would result in this type of error. The
3660 Progress parameter is set to NULL.
3661 @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set to most recent &
3662 before the error or the beginning of the string.
3663 @retval EFI_INVALID_PARAMETER The ExtractConfig function of the underlying HII
3664 Configuration Access Protocol returned
3665 EFI_INVALID_PARAMETER. Progress set to most recent
3666 & before the error or the beginning of the string.
3667
3668 **/
3669 EFI_STATUS
3670 EFIAPI
3671 HiiConfigRoutingExtractConfig (
3672 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
3673 IN CONST EFI_STRING Request,
3674 OUT EFI_STRING *Progress,
3675 OUT EFI_STRING *Results
3676 )
3677 {
3678 HII_DATABASE_PRIVATE_DATA *Private;
3679 EFI_STRING StringPtr;
3680 EFI_STRING ConfigRequest;
3681 UINTN Length;
3682 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
3683 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
3684 EFI_STATUS Status;
3685 LIST_ENTRY *Link;
3686 HII_DATABASE_RECORD *Database;
3687 UINT8 *DevicePathPkg;
3688 UINT8 *CurrentDevicePath;
3689 EFI_HANDLE DriverHandle;
3690 EFI_HII_HANDLE HiiHandle;
3691 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
3692 EFI_STRING AccessProgress;
3693 EFI_STRING AccessResults;
3694 EFI_STRING DefaultResults;
3695 BOOLEAN FirstElement;
3696 BOOLEAN IfrDataParsedFlag;
3697 BOOLEAN IsEfiVarStore;
3698 EFI_IFR_VARSTORE_EFI *EfiVarStoreInfo;
3699 EFI_STRING ErrorPtr;
3700 UINTN DevicePathSize;
3701
3702 if (This == NULL || Progress == NULL || Results == NULL) {
3703 return EFI_INVALID_PARAMETER;
3704 }
3705
3706 if (Request == NULL) {
3707 *Progress = NULL;
3708 return EFI_INVALID_PARAMETER;
3709 }
3710
3711 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
3712 StringPtr = Request;
3713 *Progress = StringPtr;
3714 DefaultResults = NULL;
3715 ConfigRequest = NULL;
3716 Status = EFI_SUCCESS;
3717 AccessResults = NULL;
3718 AccessProgress = NULL;
3719 DevicePath = NULL;
3720 IfrDataParsedFlag = FALSE;
3721 IsEfiVarStore = FALSE;
3722 EfiVarStoreInfo = NULL;
3723
3724 //
3725 // The first element of <MultiConfigRequest> should be
3726 // <GuidHdr>, which is in 'GUID='<Guid> syntax.
3727 //
3728 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
3729 return EFI_INVALID_PARAMETER;
3730 }
3731
3732 FirstElement = TRUE;
3733
3734 //
3735 // Allocate a fix length of memory to store Results. Reallocate memory for
3736 // Results if this fix length is insufficient.
3737 //
3738 *Results = (EFI_STRING) AllocateZeroPool (MAX_STRING_LENGTH);
3739 if (*Results == NULL) {
3740 return EFI_OUT_OF_RESOURCES;
3741 }
3742
3743 while (*StringPtr != 0 && StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) == 0) {
3744 //
3745 // If parsing error, set Progress to the beginning of the <MultiConfigRequest>
3746 // or most recent & before the error.
3747 //
3748 if (StringPtr == Request) {
3749 *Progress = StringPtr;
3750 } else {
3751 *Progress = StringPtr - 1;
3752 }
3753
3754 //
3755 // Process each <ConfigRequest> of <MultiConfigRequest>
3756 //
3757 Length = CalculateConfigStringLen (StringPtr);
3758 ConfigRequest = AllocateCopyPool ((Length + 1) * sizeof (CHAR16), StringPtr);
3759 if (ConfigRequest == NULL) {
3760 Status = EFI_OUT_OF_RESOURCES;
3761 goto Done;
3762 }
3763 *(ConfigRequest + Length) = 0;
3764
3765 //
3766 // Get the UEFI device path
3767 //
3768 Status = GetDevicePath (ConfigRequest, (UINT8 **) &DevicePath);
3769 if (EFI_ERROR (Status)) {
3770 goto Done;
3771 }
3772
3773 //
3774 // Find driver which matches the routing data.
3775 //
3776 DriverHandle = NULL;
3777 HiiHandle = NULL;
3778 Database = NULL;
3779 for (Link = Private->DatabaseList.ForwardLink;
3780 Link != &Private->DatabaseList;
3781 Link = Link->ForwardLink
3782 ) {
3783 Database = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
3784 if ((DevicePathPkg = Database->PackageList->DevicePathPkg) != NULL) {
3785 CurrentDevicePath = DevicePathPkg + sizeof (EFI_HII_PACKAGE_HEADER);
3786 DevicePathSize = GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) CurrentDevicePath);
3787 if ((CompareMem (DevicePath,CurrentDevicePath,DevicePathSize) == 0) && IsThisPackageList(Database, ConfigRequest)) {
3788 DriverHandle = Database->DriverHandle;
3789 HiiHandle = Database->Handle;
3790 break;
3791 }
3792 }
3793 }
3794
3795 //
3796 // Try to find driver handle by device path.
3797 //
3798 if (DriverHandle == NULL) {
3799 TempDevicePath = DevicePath;
3800 Status = gBS->LocateDevicePath (
3801 &gEfiDevicePathProtocolGuid,
3802 &TempDevicePath,
3803 &DriverHandle
3804 );
3805 if (EFI_ERROR (Status) || (DriverHandle == NULL)) {
3806 //
3807 // Routing data does not match any known driver.
3808 // Set Progress to the 'G' in "GUID" of the routing header.
3809 //
3810 *Progress = StringPtr;
3811 Status = EFI_NOT_FOUND;
3812 goto Done;
3813 }
3814 }
3815
3816 //
3817 // Validate ConfigRequest String.
3818 //
3819 ErrorPtr = ConfigRequestValidate(ConfigRequest);
3820 if (ErrorPtr != NULL) {
3821 *Progress = StrStr (StringPtr, ErrorPtr);
3822 Status = EFI_INVALID_PARAMETER;
3823 goto Done;
3824 }
3825
3826 //
3827 // Check whether ConfigRequest contains request string.
3828 //
3829 IfrDataParsedFlag = FALSE;
3830 if ((HiiHandle != NULL) && !GetElementsFromRequest(ConfigRequest)) {
3831 //
3832 // Get the full request string from IFR when HiiPackage is registered to HiiHandle
3833 //
3834 IfrDataParsedFlag = TRUE;
3835 Status = GetFullStringFromHiiFormPackages (Database, DevicePath, &ConfigRequest, &DefaultResults, &AccessProgress);
3836 if (EFI_ERROR (Status)) {
3837 //
3838 // AccessProgress indicates the parsing progress on <ConfigRequest>.
3839 // Map it to the progress on <MultiConfigRequest> then return it.
3840 //
3841 ASSERT (AccessProgress != NULL);
3842 *Progress = StrStr (StringPtr, AccessProgress);
3843 goto Done;
3844 }
3845 //
3846 // Not any request block is found.
3847 //
3848 if (!GetElementsFromRequest(ConfigRequest)) {
3849 AccessResults = AllocateCopyPool (StrSize (ConfigRequest), ConfigRequest);
3850 goto NextConfigString;
3851 }
3852 }
3853
3854 //
3855 // Check whether this ConfigRequest is search from Efi varstore type storage.
3856 //
3857 Status = GetVarStoreType(Database, ConfigRequest, &IsEfiVarStore, &EfiVarStoreInfo);
3858 if (EFI_ERROR (Status)) {
3859 goto Done;
3860 }
3861
3862 if (IsEfiVarStore) {
3863 //
3864 // Call the GetVariable function to extract settings.
3865 //
3866 Status = GetConfigRespFromEfiVarStore(This, EfiVarStoreInfo, ConfigRequest, &AccessResults, &AccessProgress);
3867 FreePool (EfiVarStoreInfo);
3868 } else {
3869 //
3870 // Call corresponding ConfigAccess protocol to extract settings
3871 //
3872 Status = gBS->HandleProtocol (
3873 DriverHandle,
3874 &gEfiHiiConfigAccessProtocolGuid,
3875 (VOID **) &ConfigAccess
3876 );
3877 ASSERT_EFI_ERROR (Status);
3878
3879 Status = ConfigAccess->ExtractConfig (
3880 ConfigAccess,
3881 ConfigRequest,
3882 &AccessProgress,
3883 &AccessResults
3884 );
3885 }
3886 if (EFI_ERROR (Status)) {
3887 //
3888 // AccessProgress indicates the parsing progress on <ConfigRequest>.
3889 // Map it to the progress on <MultiConfigRequest> then return it.
3890 //
3891 *Progress = StrStr (StringPtr, AccessProgress);
3892 goto Done;
3893 }
3894
3895 //
3896 // Attach this <ConfigAltResp> to a <MultiConfigAltResp>. There is a '&'
3897 // which seperates the first <ConfigAltResp> and the following ones.
3898 //
3899 ASSERT (*AccessProgress == 0);
3900
3901 //
3902 // Update AccessResults by getting default setting from IFR when HiiPackage is registered to HiiHandle
3903 //
3904 if (!IfrDataParsedFlag && HiiHandle != NULL) {
3905 Status = GetFullStringFromHiiFormPackages (Database, DevicePath, &ConfigRequest, &DefaultResults, NULL);
3906 ASSERT_EFI_ERROR (Status);
3907 }
3908
3909 FreePool (DevicePath);
3910 DevicePath = NULL;
3911
3912 if (DefaultResults != NULL) {
3913 Status = MergeDefaultString (&AccessResults, DefaultResults);
3914 ASSERT_EFI_ERROR (Status);
3915 FreePool (DefaultResults);
3916 DefaultResults = NULL;
3917 }
3918
3919 NextConfigString:
3920 if (!FirstElement) {
3921 Status = AppendToMultiString (Results, L"&");
3922 ASSERT_EFI_ERROR (Status);
3923 }
3924
3925 Status = AppendToMultiString (Results, AccessResults);
3926 ASSERT_EFI_ERROR (Status);
3927
3928 FirstElement = FALSE;
3929
3930 FreePool (AccessResults);
3931 AccessResults = NULL;
3932 FreePool (ConfigRequest);
3933 ConfigRequest = NULL;
3934
3935 //
3936 // Go to next <ConfigRequest> (skip '&').
3937 //
3938 StringPtr += Length;
3939 if (*StringPtr == 0) {
3940 *Progress = StringPtr;
3941 break;
3942 }
3943
3944 StringPtr++;
3945 }
3946
3947 Done:
3948 if (EFI_ERROR (Status)) {
3949 FreePool (*Results);
3950 *Results = NULL;
3951 }
3952
3953 if (ConfigRequest != NULL) {
3954 FreePool (ConfigRequest);
3955 }
3956
3957 if (AccessResults != NULL) {
3958 FreePool (AccessResults);
3959 }
3960
3961 if (DefaultResults != NULL) {
3962 FreePool (DefaultResults);
3963 }
3964
3965 if (DevicePath != NULL) {
3966 FreePool (DevicePath);
3967 }
3968
3969 return Status;
3970 }
3971
3972
3973 /**
3974 This function allows the caller to request the current configuration for the
3975 entirety of the current HII database and returns the data in a
3976 null-terminated Unicode string.
3977
3978 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
3979 instance.
3980 @param Results Null-terminated Unicode string in
3981 <MultiConfigAltResp> format which has all values
3982 filled in for the entirety of the current HII
3983 database. String to be allocated by the called
3984 function. De-allocation is up to the caller.
3985
3986 @retval EFI_SUCCESS The Results string is filled with the values
3987 corresponding to all requested names.
3988 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the parts of the
3989 results that must be stored awaiting possible
3990 future protocols.
3991 @retval EFI_INVALID_PARAMETER For example, passing in a NULL for the Results
3992 parameter would result in this type of error.
3993
3994 **/
3995 EFI_STATUS
3996 EFIAPI
3997 HiiConfigRoutingExportConfig (
3998 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
3999 OUT EFI_STRING *Results
4000 )
4001 {
4002 EFI_STATUS Status;
4003 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
4004 EFI_STRING AccessResults;
4005 EFI_STRING Progress;
4006 EFI_STRING StringPtr;
4007 EFI_STRING ConfigRequest;
4008 UINTN Index;
4009 EFI_HANDLE *ConfigAccessHandles;
4010 UINTN NumberConfigAccessHandles;
4011 BOOLEAN FirstElement;
4012 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
4013 EFI_HII_HANDLE HiiHandle;
4014 EFI_STRING DefaultResults;
4015 HII_DATABASE_PRIVATE_DATA *Private;
4016 LIST_ENTRY *Link;
4017 HII_DATABASE_RECORD *Database;
4018 UINT8 *DevicePathPkg;
4019 UINT8 *CurrentDevicePath;
4020 BOOLEAN IfrDataParsedFlag;
4021
4022 if (This == NULL || Results == NULL) {
4023 return EFI_INVALID_PARAMETER;
4024 }
4025
4026 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
4027
4028 //
4029 // Allocate a fix length of memory to store Results. Reallocate memory for
4030 // Results if this fix length is insufficient.
4031 //
4032 *Results = (EFI_STRING) AllocateZeroPool (MAX_STRING_LENGTH);
4033 if (*Results == NULL) {
4034 return EFI_OUT_OF_RESOURCES;
4035 }
4036
4037 NumberConfigAccessHandles = 0;
4038 Status = gBS->LocateHandleBuffer (
4039 ByProtocol,
4040 &gEfiHiiConfigAccessProtocolGuid,
4041 NULL,
4042 &NumberConfigAccessHandles,
4043 &ConfigAccessHandles
4044 );
4045 if (EFI_ERROR (Status)) {
4046 return Status;
4047 }
4048
4049 FirstElement = TRUE;
4050
4051 for (Index = 0; Index < NumberConfigAccessHandles; Index++) {
4052 Status = gBS->HandleProtocol (
4053 ConfigAccessHandles[Index],
4054 &gEfiHiiConfigAccessProtocolGuid,
4055 (VOID **) &ConfigAccess
4056 );
4057 if (EFI_ERROR (Status)) {
4058 continue;
4059 }
4060
4061 //
4062 // Get DevicePath and HiiHandle for this ConfigAccess driver handle
4063 //
4064 IfrDataParsedFlag = FALSE;
4065 Progress = NULL;
4066 HiiHandle = NULL;
4067 DefaultResults = NULL;
4068 Database = NULL;
4069 ConfigRequest = NULL;
4070 DevicePath = DevicePathFromHandle (ConfigAccessHandles[Index]);
4071 if (DevicePath != NULL) {
4072 for (Link = Private->DatabaseList.ForwardLink;
4073 Link != &Private->DatabaseList;
4074 Link = Link->ForwardLink
4075 ) {
4076 Database = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
4077 if ((DevicePathPkg = Database->PackageList->DevicePathPkg) != NULL) {
4078 CurrentDevicePath = DevicePathPkg + sizeof (EFI_HII_PACKAGE_HEADER);
4079 if (CompareMem (
4080 DevicePath,
4081 CurrentDevicePath,
4082 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) CurrentDevicePath)
4083 ) == 0) {
4084 HiiHandle = Database->Handle;
4085 break;
4086 }
4087 }
4088 }
4089 }
4090
4091 Status = ConfigAccess->ExtractConfig (
4092 ConfigAccess,
4093 NULL,
4094 &Progress,
4095 &AccessResults
4096 );
4097 if (EFI_ERROR (Status)) {
4098 //
4099 // Update AccessResults by getting default setting from IFR when HiiPackage is registered to HiiHandle
4100 //
4101 if (HiiHandle != NULL && DevicePath != NULL) {
4102 IfrDataParsedFlag = TRUE;
4103 Status = GetFullStringFromHiiFormPackages (Database, DevicePath, &ConfigRequest, &DefaultResults, NULL);
4104 //
4105 // Get the full request string to get the Current setting again.
4106 //
4107 if (!EFI_ERROR (Status) && ConfigRequest != NULL) {
4108 Status = ConfigAccess->ExtractConfig (
4109 ConfigAccess,
4110 ConfigRequest,
4111 &Progress,
4112 &AccessResults
4113 );
4114 FreePool (ConfigRequest);
4115 } else {
4116 Status = EFI_NOT_FOUND;
4117 }
4118 }
4119 }
4120
4121 if (!EFI_ERROR (Status)) {
4122 //
4123 // Update AccessResults by getting default setting from IFR when HiiPackage is registered to HiiHandle
4124 //
4125 if (!IfrDataParsedFlag && HiiHandle != NULL && DevicePath != NULL) {
4126 StringPtr = StrStr (AccessResults, L"&GUID=");
4127 if (StringPtr != NULL) {
4128 *StringPtr = 0;
4129 }
4130 if (GetElementsFromRequest (AccessResults)) {
4131 Status = GetFullStringFromHiiFormPackages (Database, DevicePath, &AccessResults, &DefaultResults, NULL);
4132 ASSERT_EFI_ERROR (Status);
4133 }
4134 if (StringPtr != NULL) {
4135 *StringPtr = L'&';
4136 }
4137 }
4138 //
4139 // Merge the default sting from IFR code into the got setting from driver.
4140 //
4141 if (DefaultResults != NULL) {
4142 Status = MergeDefaultString (&AccessResults, DefaultResults);
4143 ASSERT_EFI_ERROR (Status);
4144 FreePool (DefaultResults);
4145 DefaultResults = NULL;
4146 }
4147
4148 //
4149 // Attach this <ConfigAltResp> to a <MultiConfigAltResp>. There is a '&'
4150 // which seperates the first <ConfigAltResp> and the following ones.
4151 //
4152 if (!FirstElement) {
4153 Status = AppendToMultiString (Results, L"&");
4154 ASSERT_EFI_ERROR (Status);
4155 }
4156
4157 Status = AppendToMultiString (Results, AccessResults);
4158 ASSERT_EFI_ERROR (Status);
4159
4160 FirstElement = FALSE;
4161
4162 FreePool (AccessResults);
4163 AccessResults = NULL;
4164 }
4165 }
4166 FreePool (ConfigAccessHandles);
4167
4168 return EFI_SUCCESS;
4169 }
4170
4171
4172 /**
4173 This function processes the results of processing forms and routes it to the
4174 appropriate handlers or storage.
4175
4176 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
4177 instance.
4178 @param Configuration A null-terminated Unicode string in
4179 <MulltiConfigResp> format.
4180 @param Progress A pointer to a string filled in with the offset of
4181 the most recent & before the first failing name /
4182 value pair (or the beginning of the string if the
4183 failure is in the first name / value pair) or the
4184 terminating NULL if all was successful.
4185
4186 @retval EFI_SUCCESS The results have been distributed or are awaiting
4187 distribution.
4188 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the parts of the
4189 results that must be stored awaiting possible
4190 future protocols.
4191 @retval EFI_INVALID_PARAMETER Passing in a NULL for the Configuration parameter
4192 would result in this type of error.
4193 @retval EFI_NOT_FOUND Target for the specified routing data was not
4194 found.
4195
4196 **/
4197 EFI_STATUS
4198 EFIAPI
4199 HiiConfigRoutingRouteConfig (
4200 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
4201 IN CONST EFI_STRING Configuration,
4202 OUT EFI_STRING *Progress
4203 )
4204 {
4205 HII_DATABASE_PRIVATE_DATA *Private;
4206 EFI_STRING StringPtr;
4207 EFI_STRING ConfigResp;
4208 UINTN Length;
4209 EFI_STATUS Status;
4210 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
4211 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
4212 LIST_ENTRY *Link;
4213 HII_DATABASE_RECORD *Database;
4214 UINT8 *DevicePathPkg;
4215 UINT8 *CurrentDevicePath;
4216 EFI_HANDLE DriverHandle;
4217 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
4218 EFI_STRING AccessProgress;
4219 EFI_IFR_VARSTORE_EFI *EfiVarStoreInfo;
4220 BOOLEAN IsEfiVarstore;
4221 UINTN DevicePathSize;
4222
4223 if (This == NULL || Progress == NULL) {
4224 return EFI_INVALID_PARAMETER;
4225 }
4226
4227 if (Configuration == NULL) {
4228 *Progress = NULL;
4229 return EFI_INVALID_PARAMETER;
4230 }
4231
4232 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
4233 StringPtr = Configuration;
4234 *Progress = StringPtr;
4235 Database = NULL;
4236 AccessProgress = NULL;
4237 EfiVarStoreInfo= NULL;
4238 IsEfiVarstore = FALSE;
4239
4240 //
4241 // The first element of <MultiConfigResp> should be
4242 // <GuidHdr>, which is in 'GUID='<Guid> syntax.
4243 //
4244 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
4245 return EFI_INVALID_PARAMETER;
4246 }
4247
4248 while (*StringPtr != 0 && StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) == 0) {
4249 //
4250 // If parsing error, set Progress to the beginning of the <MultiConfigResp>
4251 // or most recent & before the error.
4252 //
4253 if (StringPtr == Configuration) {
4254 *Progress = StringPtr;
4255 } else {
4256 *Progress = StringPtr - 1;
4257 }
4258
4259 //
4260 // Process each <ConfigResp> of <MultiConfigResp>
4261 //
4262 Length = CalculateConfigStringLen (StringPtr);
4263 ConfigResp = AllocateCopyPool ((Length + 1) * sizeof (CHAR16), StringPtr);
4264 if (ConfigResp == NULL) {
4265 return EFI_OUT_OF_RESOURCES;
4266 }
4267 //
4268 // Append '\0' to the end of ConfigRequest
4269 //
4270 *(ConfigResp + Length) = 0;
4271
4272 //
4273 // Get the UEFI device path
4274 //
4275 Status = GetDevicePath (ConfigResp, (UINT8 **) &DevicePath);
4276 if (EFI_ERROR (Status)) {
4277 FreePool (ConfigResp);
4278 return Status;
4279 }
4280
4281 //
4282 // Find driver which matches the routing data.
4283 //
4284 DriverHandle = NULL;
4285 for (Link = Private->DatabaseList.ForwardLink;
4286 Link != &Private->DatabaseList;
4287 Link = Link->ForwardLink
4288 ) {
4289 Database = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
4290
4291 if ((DevicePathPkg = Database->PackageList->DevicePathPkg) != NULL) {
4292 CurrentDevicePath = DevicePathPkg + sizeof (EFI_HII_PACKAGE_HEADER);
4293 DevicePathSize = GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) CurrentDevicePath);
4294 if ((CompareMem (DevicePath,CurrentDevicePath,DevicePathSize) == 0) && IsThisPackageList(Database, ConfigResp)) {
4295 DriverHandle = Database->DriverHandle;
4296 break;
4297 }
4298 }
4299 }
4300
4301 //
4302 // Try to find driver handle by device path.
4303 //
4304 if (DriverHandle == NULL) {
4305 TempDevicePath = DevicePath;
4306 Status = gBS->LocateDevicePath (
4307 &gEfiDevicePathProtocolGuid,
4308 &TempDevicePath,
4309 &DriverHandle
4310 );
4311 if (EFI_ERROR (Status) || (DriverHandle == NULL)) {
4312 //
4313 // Routing data does not match any known driver.
4314 // Set Progress to the 'G' in "GUID" of the routing header.
4315 //
4316 FreePool (DevicePath);
4317 *Progress = StringPtr;
4318 FreePool (ConfigResp);
4319 return EFI_NOT_FOUND;
4320 }
4321 }
4322
4323 FreePool (DevicePath);
4324
4325 //
4326 // Check whether this ConfigRequest is search from Efi varstore type storage.
4327 //
4328 Status = GetVarStoreType(Database, ConfigResp, &IsEfiVarstore, &EfiVarStoreInfo);
4329 if (EFI_ERROR (Status)) {
4330 return Status;
4331 }
4332
4333 if (IsEfiVarstore) {
4334 //
4335 // Call the SetVariable function to route settings.
4336 //
4337 Status = RouteConfigRespForEfiVarStore(This, EfiVarStoreInfo, ConfigResp, &AccessProgress);
4338 FreePool (EfiVarStoreInfo);
4339 } else {
4340 //
4341 // Call corresponding ConfigAccess protocol to route settings
4342 //
4343 Status = gBS->HandleProtocol (
4344 DriverHandle,
4345 &gEfiHiiConfigAccessProtocolGuid,
4346 (VOID **) &ConfigAccess
4347 );
4348 ASSERT_EFI_ERROR (Status);
4349
4350 Status = ConfigAccess->RouteConfig (
4351 ConfigAccess,
4352 ConfigResp,
4353 &AccessProgress
4354 );
4355 }
4356 if (EFI_ERROR (Status)) {
4357 ASSERT (AccessProgress != NULL);
4358 //
4359 // AccessProgress indicates the parsing progress on <ConfigResp>.
4360 // Map it to the progress on <MultiConfigResp> then return it.
4361 //
4362 *Progress = StrStr (StringPtr, AccessProgress);
4363
4364 FreePool (ConfigResp);
4365 return Status;
4366 }
4367
4368 FreePool (ConfigResp);
4369 ConfigResp = NULL;
4370
4371 //
4372 // Go to next <ConfigResp> (skip '&').
4373 //
4374 StringPtr += Length;
4375 if (*StringPtr == 0) {
4376 *Progress = StringPtr;
4377 break;
4378 }
4379
4380 StringPtr++;
4381
4382 }
4383
4384 return EFI_SUCCESS;
4385 }
4386
4387
4388 /**
4389 This helper function is to be called by drivers to map configuration data
4390 stored in byte array ("block") formats such as UEFI Variables into current
4391 configuration strings.
4392
4393 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
4394 instance.
4395 @param ConfigRequest A null-terminated Unicode string in
4396 <ConfigRequest> format.
4397 @param Block Array of bytes defining the block's configuration.
4398 @param BlockSize Length in bytes of Block.
4399 @param Config Filled-in configuration string. String allocated
4400 by the function. Returned only if call is
4401 successful. It is <ConfigResp> string format.
4402 @param Progress A pointer to a string filled in with the offset of
4403 the most recent & before the first failing
4404 name/value pair (or the beginning of the string if
4405 the failure is in the first name / value pair) or
4406 the terminating NULL if all was successful.
4407
4408 @retval EFI_SUCCESS The request succeeded. Progress points to the null
4409 terminator at the end of the ConfigRequest
4410 string.
4411 @retval EFI_OUT_OF_RESOURCES Not enough memory to allocate Config. Progress
4412 points to the first character of ConfigRequest.
4413 @retval EFI_INVALID_PARAMETER Passing in a NULL for the ConfigRequest or
4414 Block parameter would result in this type of
4415 error. Progress points to the first character of
4416 ConfigRequest.
4417 @retval EFI_DEVICE_ERROR Block not large enough. Progress undefined.
4418 @retval EFI_INVALID_PARAMETER Encountered non <BlockName> formatted string.
4419 Block is left updated and Progress points at
4420 the "&" preceding the first non-<BlockName>.
4421
4422 **/
4423 EFI_STATUS
4424 EFIAPI
4425 HiiBlockToConfig (
4426 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
4427 IN CONST EFI_STRING ConfigRequest,
4428 IN CONST UINT8 *Block,
4429 IN CONST UINTN BlockSize,
4430 OUT EFI_STRING *Config,
4431 OUT EFI_STRING *Progress
4432 )
4433 {
4434 HII_DATABASE_PRIVATE_DATA *Private;
4435 EFI_STRING StringPtr;
4436 UINTN Length;
4437 EFI_STATUS Status;
4438 EFI_STRING TmpPtr;
4439 UINT8 *TmpBuffer;
4440 UINTN Offset;
4441 UINTN Width;
4442 UINT8 *Value;
4443 EFI_STRING ValueStr;
4444 EFI_STRING ConfigElement;
4445 UINTN Index;
4446 UINT8 *TemBuffer;
4447 CHAR16 *TemString;
4448 CHAR16 TemChar;
4449
4450 TmpBuffer = NULL;
4451
4452 if (This == NULL || Progress == NULL || Config == NULL) {
4453 return EFI_INVALID_PARAMETER;
4454 }
4455
4456 if (Block == NULL || ConfigRequest == NULL) {
4457 *Progress = ConfigRequest;
4458 return EFI_INVALID_PARAMETER;
4459 }
4460
4461
4462 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
4463 ASSERT (Private != NULL);
4464
4465 StringPtr = ConfigRequest;
4466 ValueStr = NULL;
4467 Value = NULL;
4468 ConfigElement = NULL;
4469
4470 //
4471 // Allocate a fix length of memory to store Results. Reallocate memory for
4472 // Results if this fix length is insufficient.
4473 //
4474 *Config = (EFI_STRING) AllocateZeroPool (MAX_STRING_LENGTH);
4475 if (*Config == NULL) {
4476 return EFI_OUT_OF_RESOURCES;
4477 }
4478
4479 //
4480 // Jump <ConfigHdr>
4481 //
4482 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
4483 *Progress = StringPtr;
4484 Status = EFI_INVALID_PARAMETER;
4485 goto Exit;
4486 }
4487 while (*StringPtr != 0 && StrnCmp (StringPtr, L"PATH=", StrLen (L"PATH=")) != 0) {
4488 StringPtr++;
4489 }
4490 if (*StringPtr == 0) {
4491 *Progress = StringPtr - 1;
4492 Status = EFI_INVALID_PARAMETER;
4493 goto Exit;
4494 }
4495
4496 while (*StringPtr != L'&' && *StringPtr != 0) {
4497 StringPtr++;
4498 }
4499 if (*StringPtr == 0) {
4500 *Progress = StringPtr;
4501
4502 AppendToMultiString(Config, ConfigRequest);
4503 HiiToLower (*Config);
4504
4505 return EFI_SUCCESS;
4506 }
4507 //
4508 // Skip '&'
4509 //
4510 StringPtr++;
4511
4512 //
4513 // Copy <ConfigHdr> and an additional '&' to <ConfigResp>
4514 //
4515 TemChar = *StringPtr;
4516 *StringPtr = '\0';
4517 AppendToMultiString(Config, ConfigRequest);
4518 *StringPtr = TemChar;
4519
4520 //
4521 // Parse each <RequestElement> if exists
4522 // Only <BlockName> format is supported by this help function.
4523 // <BlockName> ::= 'OFFSET='<Number>&'WIDTH='<Number>
4524 //
4525 while (*StringPtr != 0 && StrnCmp (StringPtr, L"OFFSET=", StrLen (L"OFFSET=")) == 0) {
4526 //
4527 // Back up the header of one <BlockName>
4528 //
4529 TmpPtr = StringPtr;
4530
4531 StringPtr += StrLen (L"OFFSET=");
4532 //
4533 // Get Offset
4534 //
4535 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
4536 if (EFI_ERROR (Status)) {
4537 *Progress = TmpPtr - 1;
4538 goto Exit;
4539 }
4540 Offset = 0;
4541 CopyMem (
4542 &Offset,
4543 TmpBuffer,
4544 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
4545 );
4546 FreePool (TmpBuffer);
4547
4548 StringPtr += Length;
4549 if (StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
4550 *Progress = TmpPtr - 1;
4551 Status = EFI_INVALID_PARAMETER;
4552 goto Exit;
4553 }
4554 StringPtr += StrLen (L"&WIDTH=");
4555
4556 //
4557 // Get Width
4558 //
4559 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
4560 if (EFI_ERROR (Status)) {
4561 *Progress = TmpPtr - 1;
4562 goto Exit;
4563 }
4564 Width = 0;
4565 CopyMem (
4566 &Width,
4567 TmpBuffer,
4568 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
4569 );
4570 FreePool (TmpBuffer);
4571
4572 StringPtr += Length;
4573 if (*StringPtr != 0 && *StringPtr != L'&') {
4574 *Progress = TmpPtr - 1;
4575 Status = EFI_INVALID_PARAMETER;
4576 goto Exit;
4577 }
4578
4579 //
4580 // Calculate Value and convert it to hex string.
4581 //
4582 if (Offset + Width > BlockSize) {
4583 *Progress = StringPtr;
4584 Status = EFI_DEVICE_ERROR;
4585 goto Exit;
4586 }
4587
4588 Value = (UINT8 *) AllocateZeroPool (Width);
4589 if (Value == NULL) {
4590 *Progress = ConfigRequest;
4591 Status = EFI_OUT_OF_RESOURCES;
4592 goto Exit;
4593 }
4594
4595 CopyMem (Value, (UINT8 *) Block + Offset, Width);
4596
4597 Length = Width * 2 + 1;
4598 ValueStr = (EFI_STRING) AllocateZeroPool (Length * sizeof (CHAR16));
4599 if (ValueStr == NULL) {
4600 *Progress = ConfigRequest;
4601 Status = EFI_OUT_OF_RESOURCES;
4602 goto Exit;
4603 }
4604
4605 TemString = ValueStr;
4606 TemBuffer = Value + Width - 1;
4607 for (Index = 0; Index < Width; Index ++, TemBuffer --) {
4608 TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemBuffer, 2);
4609 }
4610
4611 FreePool (Value);
4612 Value = NULL;
4613
4614 //
4615 // Build a ConfigElement
4616 //
4617 Length += StringPtr - TmpPtr + 1 + StrLen (L"VALUE=");
4618 ConfigElement = (EFI_STRING) AllocateZeroPool (Length * sizeof (CHAR16));
4619 if (ConfigElement == NULL) {
4620 Status = EFI_OUT_OF_RESOURCES;
4621 goto Exit;
4622 }
4623 CopyMem (ConfigElement, TmpPtr, (StringPtr - TmpPtr + 1) * sizeof (CHAR16));
4624 if (*StringPtr == 0) {
4625 *(ConfigElement + (StringPtr - TmpPtr)) = L'&';
4626 }
4627 *(ConfigElement + (StringPtr - TmpPtr) + 1) = 0;
4628 StrCatS (ConfigElement, Length, L"VALUE=");
4629 StrCatS (ConfigElement, Length, ValueStr);
4630
4631 AppendToMultiString (Config, ConfigElement);
4632
4633 FreePool (ConfigElement);
4634 FreePool (ValueStr);
4635 ConfigElement = NULL;
4636 ValueStr = NULL;
4637
4638 //
4639 // If '\0', parsing is finished. Otherwise skip '&' to continue
4640 //
4641 if (*StringPtr == 0) {
4642 break;
4643 }
4644 AppendToMultiString (Config, L"&");
4645 StringPtr++;
4646
4647 }
4648
4649 if (*StringPtr != 0) {
4650 *Progress = StringPtr - 1;
4651 Status = EFI_INVALID_PARAMETER;
4652 goto Exit;
4653 }
4654
4655 HiiToLower (*Config);
4656 *Progress = StringPtr;
4657 return EFI_SUCCESS;
4658
4659 Exit:
4660 if (*Config != NULL) {
4661 FreePool (*Config);
4662 *Config = NULL;
4663 }
4664 if (ValueStr != NULL) {
4665 FreePool (ValueStr);
4666 }
4667 if (Value != NULL) {
4668 FreePool (Value);
4669 }
4670 if (ConfigElement != NULL) {
4671 FreePool (ConfigElement);
4672 }
4673
4674 return Status;
4675
4676 }
4677
4678
4679 /**
4680 This helper function is to be called by drivers to map configuration strings
4681 to configurations stored in byte array ("block") formats such as UEFI Variables.
4682
4683 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
4684 instance.
4685 @param ConfigResp A null-terminated Unicode string in <ConfigResp>
4686 format.
4687 @param Block A possibly null array of bytes representing the
4688 current block. Only bytes referenced in the
4689 ConfigResp string in the block are modified. If
4690 this parameter is null or if the *BlockSize
4691 parameter is (on input) shorter than required by
4692 the Configuration string, only the BlockSize
4693 parameter is updated and an appropriate status
4694 (see below) is returned.
4695 @param BlockSize The length of the Block in units of UINT8. On
4696 input, this is the size of the Block. On output,
4697 if successful, contains the largest index of the
4698 modified byte in the Block, or the required buffer
4699 size if the Block is not large enough.
4700 @param Progress On return, points to an element of the ConfigResp
4701 string filled in with the offset of the most
4702 recent '&' before the first failing name / value
4703 pair (or the beginning of the string if the
4704 failure is in the first name / value pair) or the
4705 terminating NULL if all was successful.
4706
4707 @retval EFI_SUCCESS The request succeeded. Progress points to the null
4708 terminator at the end of the ConfigResp string.
4709 @retval EFI_OUT_OF_RESOURCES Not enough memory to allocate Config. Progress
4710 points to the first character of ConfigResp.
4711 @retval EFI_INVALID_PARAMETER Passing in a NULL for the ConfigResp or
4712 Block parameter would result in this type of
4713 error. Progress points to the first character of
4714 ConfigResp.
4715 @retval EFI_INVALID_PARAMETER Encountered non <BlockName> formatted name /
4716 value pair. Block is left updated and
4717 Progress points at the '&' preceding the first
4718 non-<BlockName>.
4719 @retval EFI_BUFFER_TOO_SMALL Block not large enough. Progress undefined.
4720 BlockSize is updated with the required buffer size.
4721 @retval EFI_NOT_FOUND Target for the specified routing data was not found.
4722 Progress points to the "G" in "GUID" of the errant
4723 routing data.
4724
4725 **/
4726 EFI_STATUS
4727 EFIAPI
4728 HiiConfigToBlock (
4729 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
4730 IN CONST EFI_STRING ConfigResp,
4731 IN OUT UINT8 *Block,
4732 IN OUT UINTN *BlockSize,
4733 OUT EFI_STRING *Progress
4734 )
4735 {
4736 HII_DATABASE_PRIVATE_DATA *Private;
4737 EFI_STRING StringPtr;
4738 EFI_STRING TmpPtr;
4739 UINTN Length;
4740 EFI_STATUS Status;
4741 UINT8 *TmpBuffer;
4742 UINTN Offset;
4743 UINTN Width;
4744 UINT8 *Value;
4745 UINTN BufferSize;
4746 UINTN MaxBlockSize;
4747
4748 TmpBuffer = NULL;
4749
4750 if (This == NULL || BlockSize == NULL || Progress == NULL) {
4751 return EFI_INVALID_PARAMETER;
4752 }
4753
4754 *Progress = ConfigResp;
4755 if (ConfigResp == NULL) {
4756 return EFI_INVALID_PARAMETER;
4757 }
4758
4759 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
4760 ASSERT (Private != NULL);
4761
4762 StringPtr = ConfigResp;
4763 BufferSize = *BlockSize;
4764 Value = NULL;
4765 MaxBlockSize = 0;
4766
4767 //
4768 // Jump <ConfigHdr>
4769 //
4770 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
4771 *Progress = StringPtr;
4772 Status = EFI_INVALID_PARAMETER;
4773 goto Exit;
4774 }
4775 while (*StringPtr != 0 && StrnCmp (StringPtr, L"PATH=", StrLen (L"PATH=")) != 0) {
4776 StringPtr++;
4777 }
4778 if (*StringPtr == 0) {
4779 *Progress = StringPtr;
4780 Status = EFI_INVALID_PARAMETER;
4781 goto Exit;
4782 }
4783
4784 while (*StringPtr != L'&' && *StringPtr != 0) {
4785 StringPtr++;
4786 }
4787 if (*StringPtr == 0) {
4788 *Progress = StringPtr;
4789 Status = EFI_INVALID_PARAMETER;
4790 goto Exit;
4791 }
4792
4793 //
4794 // Parse each <ConfigElement> if exists
4795 // Only '&'<BlockConfig> format is supported by this help function.
4796 // <BlockConfig> ::= 'OFFSET='<Number>&'WIDTH='<Number>&'VALUE='<Number>
4797 //
4798 while (*StringPtr != 0 && StrnCmp (StringPtr, L"&OFFSET=", StrLen (L"&OFFSET=")) == 0) {
4799 TmpPtr = StringPtr;
4800 StringPtr += StrLen (L"&OFFSET=");
4801 //
4802 // Get Offset
4803 //
4804 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
4805 if (EFI_ERROR (Status)) {
4806 *Progress = TmpPtr;
4807 goto Exit;
4808 }
4809 Offset = 0;
4810 CopyMem (
4811 &Offset,
4812 TmpBuffer,
4813 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
4814 );
4815 FreePool (TmpBuffer);
4816
4817 StringPtr += Length;
4818 if (StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
4819 *Progress = TmpPtr;
4820 Status = EFI_INVALID_PARAMETER;
4821 goto Exit;
4822 }
4823 StringPtr += StrLen (L"&WIDTH=");
4824
4825 //
4826 // Get Width
4827 //
4828 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
4829 if (EFI_ERROR (Status)) {
4830 *Progress = TmpPtr;
4831 goto Exit;
4832 }
4833 Width = 0;
4834 CopyMem (
4835 &Width,
4836 TmpBuffer,
4837 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
4838 );
4839 FreePool (TmpBuffer);
4840
4841 StringPtr += Length;
4842 if (StrnCmp (StringPtr, L"&VALUE=", StrLen (L"&VALUE=")) != 0) {
4843 *Progress = TmpPtr;
4844 Status = EFI_INVALID_PARAMETER;
4845 goto Exit;
4846 }
4847 StringPtr += StrLen (L"&VALUE=");
4848
4849 //
4850 // Get Value
4851 //
4852 Status = GetValueOfNumber (StringPtr, &Value, &Length);
4853 if (EFI_ERROR (Status)) {
4854 *Progress = TmpPtr;
4855 goto Exit;
4856 }
4857
4858 StringPtr += Length;
4859 if (*StringPtr != 0 && *StringPtr != L'&') {
4860 *Progress = TmpPtr;
4861 Status = EFI_INVALID_PARAMETER;
4862 goto Exit;
4863 }
4864
4865 //
4866 // Update the Block with configuration info
4867 //
4868 if ((Block != NULL) && (Offset + Width <= BufferSize)) {
4869 CopyMem (Block + Offset, Value, Width);
4870 }
4871 if (Offset + Width > MaxBlockSize) {
4872 MaxBlockSize = Offset + Width;
4873 }
4874
4875 FreePool (Value);
4876 Value = NULL;
4877
4878 //
4879 // If '\0', parsing is finished.
4880 //
4881 if (*StringPtr == 0) {
4882 break;
4883 }
4884 }
4885
4886 //
4887 // The input string is not ConfigResp format, return error.
4888 //
4889 if (*StringPtr != 0) {
4890 *Progress = StringPtr;
4891 Status = EFI_INVALID_PARAMETER;
4892 goto Exit;
4893 }
4894
4895 *Progress = StringPtr + StrLen (StringPtr);
4896 *BlockSize = MaxBlockSize - 1;
4897
4898 if (MaxBlockSize > BufferSize) {
4899 *BlockSize = MaxBlockSize;
4900 if (Block != NULL) {
4901 return EFI_BUFFER_TOO_SMALL;
4902 }
4903 }
4904
4905 if (Block == NULL) {
4906 *Progress = ConfigResp;
4907 return EFI_INVALID_PARAMETER;
4908 }
4909
4910 return EFI_SUCCESS;
4911
4912 Exit:
4913
4914 if (Value != NULL) {
4915 FreePool (Value);
4916 }
4917 return Status;
4918 }
4919
4920
4921 /**
4922 This helper function is to be called by drivers to extract portions of
4923 a larger configuration string.
4924
4925 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
4926 instance.
4927 @param Configuration A null-terminated Unicode string in
4928 <MultiConfigAltResp> format.
4929 @param Guid A pointer to the GUID value to search for in the
4930 routing portion of the ConfigResp string when
4931 retrieving the requested data. If Guid is NULL,
4932 then all GUID values will be searched for.
4933 @param Name A pointer to the NAME value to search for in the
4934 routing portion of the ConfigResp string when
4935 retrieving the requested data. If Name is NULL,
4936 then all Name values will be searched for.
4937 @param DevicePath A pointer to the PATH value to search for in the
4938 routing portion of the ConfigResp string when
4939 retrieving the requested data. If DevicePath is
4940 NULL, then all DevicePath values will be searched
4941 for.
4942 @param AltCfgId A pointer to the ALTCFG value to search for in the
4943 routing portion of the ConfigResp string when
4944 retrieving the requested data. If this parameter
4945 is NULL, then the current setting will be
4946 retrieved.
4947 @param AltCfgResp A pointer to a buffer which will be allocated by
4948 the function which contains the retrieved string
4949 as requested. This buffer is only allocated if
4950 the call was successful. It is <ConfigResp> format.
4951
4952 @retval EFI_SUCCESS The request succeeded. The requested data was
4953 extracted and placed in the newly allocated
4954 AltCfgResp buffer.
4955 @retval EFI_OUT_OF_RESOURCES Not enough memory to allocate AltCfgResp.
4956 @retval EFI_INVALID_PARAMETER Any parameter is invalid.
4957 @retval EFI_NOT_FOUND Target for the specified routing data was not
4958 found.
4959
4960 **/
4961 EFI_STATUS
4962 EFIAPI
4963 HiiGetAltCfg (
4964 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
4965 IN CONST EFI_STRING Configuration,
4966 IN CONST EFI_GUID *Guid,
4967 IN CONST EFI_STRING Name,
4968 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
4969 IN CONST UINT16 *AltCfgId,
4970 OUT EFI_STRING *AltCfgResp
4971 )
4972 {
4973 EFI_STATUS Status;
4974 EFI_STRING StringPtr;
4975 EFI_STRING HdrStart;
4976 EFI_STRING HdrEnd;
4977 EFI_STRING TmpPtr;
4978 UINTN Length;
4979 EFI_STRING GuidStr;
4980 EFI_STRING NameStr;
4981 EFI_STRING PathStr;
4982 EFI_STRING AltIdStr;
4983 EFI_STRING Result;
4984 BOOLEAN GuidFlag;
4985 BOOLEAN NameFlag;
4986 BOOLEAN PathFlag;
4987
4988 HdrStart = NULL;
4989 HdrEnd = NULL;
4990 GuidStr = NULL;
4991 NameStr = NULL;
4992 PathStr = NULL;
4993 AltIdStr = NULL;
4994 Result = NULL;
4995 GuidFlag = FALSE;
4996 NameFlag = FALSE;
4997 PathFlag = FALSE;
4998
4999 if (This == NULL || Configuration == NULL || AltCfgResp == NULL) {
5000 return EFI_INVALID_PARAMETER;
5001 }
5002
5003 StringPtr = Configuration;
5004 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
5005 return EFI_INVALID_PARAMETER;
5006 }
5007
5008 //
5009 // Generate the sub string for later matching.
5010 //
5011 GenerateSubStr (L"GUID=", sizeof (EFI_GUID), (VOID *) Guid, 1, &GuidStr);
5012 GenerateSubStr (
5013 L"PATH=",
5014 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) DevicePath),
5015 (VOID *) DevicePath,
5016 1,
5017 &PathStr
5018 );
5019 if (AltCfgId != NULL) {
5020 GenerateSubStr (L"ALTCFG=", sizeof (UINT16), (VOID *) AltCfgId, 3, &AltIdStr);
5021 }
5022 if (Name != NULL) {
5023 GenerateSubStr (L"NAME=", StrLen (Name) * sizeof (CHAR16), (VOID *) Name, 2, &NameStr);
5024 } else {
5025 GenerateSubStr (L"NAME=", 0, NULL, 2, &NameStr);
5026 }
5027
5028 while (*StringPtr != 0) {
5029 //
5030 // Try to match the GUID
5031 //
5032 if (!GuidFlag) {
5033 TmpPtr = StrStr (StringPtr, GuidStr);
5034 if (TmpPtr == NULL) {
5035 Status = EFI_NOT_FOUND;
5036 goto Exit;
5037 }
5038 HdrStart = TmpPtr;
5039
5040 //
5041 // Jump to <NameHdr>
5042 //
5043 if (Guid != NULL) {
5044 StringPtr = TmpPtr + StrLen (GuidStr);
5045 } else {
5046 StringPtr = StrStr (TmpPtr, L"NAME=");
5047 if (StringPtr == NULL) {
5048 Status = EFI_NOT_FOUND;
5049 goto Exit;
5050 }
5051 }
5052 GuidFlag = TRUE;
5053 }
5054
5055 //
5056 // Try to match the NAME
5057 //
5058 if (GuidFlag && !NameFlag) {
5059 if (StrnCmp (StringPtr, NameStr, StrLen (NameStr)) != 0) {
5060 GuidFlag = FALSE;
5061 } else {
5062 //
5063 // Jump to <PathHdr>
5064 //
5065 if (Name != NULL) {
5066 StringPtr += StrLen (NameStr);
5067 } else {
5068 StringPtr = StrStr (StringPtr, L"PATH=");
5069 if (StringPtr == NULL) {
5070 Status = EFI_NOT_FOUND;
5071 goto Exit;
5072 }
5073 }
5074 NameFlag = TRUE;
5075 }
5076 }
5077
5078 //
5079 // Try to match the DevicePath
5080 //
5081 if (GuidFlag && NameFlag && !PathFlag) {
5082 if (StrnCmp (StringPtr, PathStr, StrLen (PathStr)) != 0) {
5083 GuidFlag = FALSE;
5084 NameFlag = FALSE;
5085 } else {
5086 //
5087 // Jump to '&' before <DescHdr> or <ConfigBody>
5088 //
5089 if (DevicePath != NULL) {
5090 StringPtr += StrLen (PathStr);
5091 } else {
5092 StringPtr = StrStr (StringPtr, L"&");
5093 if (StringPtr == NULL) {
5094 Status = EFI_NOT_FOUND;
5095 goto Exit;
5096 }
5097 StringPtr ++;
5098 }
5099 PathFlag = TRUE;
5100 HdrEnd = StringPtr;
5101 }
5102 }
5103
5104 //
5105 // Try to match the AltCfgId
5106 //
5107 if (GuidFlag && NameFlag && PathFlag) {
5108 if (AltCfgId == NULL) {
5109 //
5110 // Return Current Setting when AltCfgId is NULL.
5111 //
5112 Status = OutputConfigBody (StringPtr, &Result);
5113 goto Exit;
5114 }
5115 //
5116 // Search the <ConfigAltResp> to get the <AltResp> with AltCfgId.
5117 //
5118 if (StrnCmp (StringPtr, AltIdStr, StrLen (AltIdStr)) != 0) {
5119 GuidFlag = FALSE;
5120 NameFlag = FALSE;
5121 PathFlag = FALSE;
5122 } else {
5123 //
5124 // Skip AltIdStr and &
5125 //
5126 StringPtr = StringPtr + StrLen (AltIdStr);
5127 Status = OutputConfigBody (StringPtr, &Result);
5128 goto Exit;
5129 }
5130 }
5131 }
5132
5133 Status = EFI_NOT_FOUND;
5134
5135 Exit:
5136 *AltCfgResp = NULL;
5137 if (!EFI_ERROR (Status) && (Result != NULL)) {
5138 //
5139 // Copy the <ConfigHdr> and <ConfigBody>
5140 //
5141 Length = HdrEnd - HdrStart + StrLen (Result) + 1;
5142 *AltCfgResp = AllocateZeroPool (Length * sizeof (CHAR16));
5143 if (*AltCfgResp == NULL) {
5144 Status = EFI_OUT_OF_RESOURCES;
5145 } else {
5146 StrnCpyS (*AltCfgResp, Length, HdrStart, HdrEnd - HdrStart);
5147 StrCatS (*AltCfgResp, Length, Result);
5148 Status = EFI_SUCCESS;
5149 }
5150 }
5151
5152 if (GuidStr != NULL) {
5153 FreePool (GuidStr);
5154 }
5155 if (NameStr != NULL) {
5156 FreePool (NameStr);
5157 }
5158 if (PathStr != NULL) {
5159 FreePool (PathStr);
5160 }
5161 if (AltIdStr != NULL) {
5162 FreePool (AltIdStr);
5163 }
5164 if (Result != NULL) {
5165 FreePool (Result);
5166 }
5167
5168 return Status;
5169
5170 }
5171
5172