]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/HiiDatabaseDxe/ConfigRouting.c
Update HiiDataBase to fix parsing Hii package error. Some HiiPackages have no varstor...
[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 - 2008, Intel Corporation
5 All rights reserved. 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 DevicePath binary of a UEFI device path.
65
66 @retval EFI_INVALID_PARAMETER Any incoming parameter is invalid.
67 @retval EFI_OUT_OF_RESOURCES Lake of resources to store neccesary structures.
68 @retval EFI_SUCCESS The device path is retrieved and translated to
69 binary format.
70
71 **/
72 EFI_STATUS
73 GetDevicePath (
74 IN EFI_STRING String,
75 OUT UINT8 **DevicePath
76 )
77 {
78 UINTN Length;
79 EFI_STRING PathHdr;
80 EFI_STRING DevicePathString;
81 UINT8 *DevicePathBuffer;
82 CHAR16 TemStr[2];
83 UINTN Index;
84 UINT8 DigitUint8;
85
86 if (String == NULL || DevicePath == NULL) {
87 return EFI_INVALID_PARAMETER;
88 }
89
90 //
91 // Find the 'PATH=' of <PathHdr> and skip it.
92 //
93 for (; (*String != 0 && StrnCmp (String, L"PATH=", StrLen (L"PATH=")) != 0); String++);
94 if (*String == 0) {
95 return EFI_INVALID_PARAMETER;
96 }
97
98 String += StrLen (L"PATH=");
99 PathHdr = String;
100
101 //
102 // The content between 'PATH=' of <ConfigHdr> and '&' of next element
103 // or '\0' (end of configuration string) is the UNICODE %02x bytes encoding
104 // of UEFI device path.
105 //
106 for (Length = 0; *String != 0 && *String != L'&'; String++, Length++);
107 DevicePathString = (EFI_STRING) AllocateZeroPool ((Length + 1) * sizeof (CHAR16));
108 if (DevicePathString == NULL) {
109 return EFI_OUT_OF_RESOURCES;
110 }
111 StrnCpy (DevicePathString, PathHdr, Length);
112 *(DevicePathString + Length) = 0;
113
114 //
115 // The data in <PathHdr> is encoded as hex UNICODE %02x bytes in the same order
116 // as the device path resides in RAM memory.
117 // Translate the data into binary.
118 //
119 DevicePathBuffer = (UINT8 *) AllocateZeroPool ((Length + 1) / 2);
120 if (DevicePathBuffer == NULL) {
121 FreePool (DevicePathString);
122 return EFI_OUT_OF_RESOURCES;
123 }
124
125 ZeroMem (TemStr, sizeof (TemStr));
126 for (Index = 0; DevicePathString[Index] != L'\0'; Index ++) {
127 TemStr[0] = DevicePathString[Index];
128 DigitUint8 = (UINT8) StrHexToUint64 (TemStr);
129 if ((Index & 1) == 0) {
130 DevicePathBuffer [Index/2] = DigitUint8;
131 } else {
132 DevicePathBuffer [Index/2] = (UINT8) ((DevicePathBuffer [Index/2] << 4) + DigitUint8);
133 }
134 }
135
136 FreePool (DevicePathString);
137
138 *DevicePath = DevicePathBuffer;
139
140 return EFI_SUCCESS;
141
142 }
143
144 /**
145 Converts the unicode character of the string from uppercase to lowercase.
146 This is a internal function.
147
148 @param Str String to be converted
149
150 **/
151 VOID
152 EFIAPI
153 HiiToLower (
154 IN EFI_STRING ConfigString
155 )
156 {
157 EFI_STRING String;
158 BOOLEAN Lower;
159
160 ASSERT (ConfigString != NULL);
161
162 //
163 // Convert all hex digits in range [A-F] in the configuration header to [a-f]
164 //
165 for (String = ConfigString, Lower = FALSE; *String != L'\0'; String++) {
166 if (*String == L'=') {
167 Lower = TRUE;
168 } else if (*String == L'&') {
169 Lower = FALSE;
170 } else if (Lower && *String >= L'A' && *String <= L'F') {
171 *String = (CHAR16) (*String - L'A' + L'a');
172 }
173 }
174
175 return;
176 }
177
178 /**
179 Generate a sub string then output it.
180
181 This is a internal function.
182
183 @param String A constant string which is the prefix of the to be
184 generated string, e.g. GUID=
185
186 @param BufferLen The length of the Buffer in bytes.
187
188 @param Buffer Points to a buffer which will be converted to be the
189 content of the generated string.
190
191 @param Flag If 1, the buffer contains data for the value of GUID or PATH stored in
192 UINT8 *; if 2, the buffer contains unicode string for the value of NAME;
193 if 3, the buffer contains other data.
194
195 @param SubStr Points to the output string. It's caller's
196 responsibility to free this buffer.
197
198
199 **/
200 VOID
201 GenerateSubStr (
202 IN CONST EFI_STRING String,
203 IN UINTN BufferLen,
204 IN VOID *Buffer,
205 IN UINT8 Flag,
206 OUT EFI_STRING *SubStr
207 )
208 {
209 UINTN Length;
210 EFI_STRING Str;
211 EFI_STRING StringHeader;
212 CHAR16 *TemString;
213 CHAR16 *TemName;
214 UINT8 *TemBuffer;
215 UINTN Index;
216
217 ASSERT (String != NULL && SubStr != NULL);
218
219 if (Buffer == NULL) {
220 *SubStr = AllocateCopyPool (StrSize (String), String);
221 ASSERT (*SubStr != NULL);
222 return ;
223 }
224
225 //
226 // Header + Data + '&' + '\0'
227 //
228 Length = StrLen (String) + BufferLen * 2 + 1 + 1;
229 Str = AllocateZeroPool (Length * sizeof (CHAR16));
230 ASSERT (Str != NULL);
231
232 StrCpy (Str, String);
233 Length = (BufferLen * 2 + 1) * sizeof (CHAR16);
234
235 StringHeader = Str + StrLen (String);
236 TemString = (CHAR16 *) StringHeader;
237
238 switch (Flag) {
239 case 1:
240 //
241 // Convert Buffer to Hex String in reverse order
242 //
243 TemBuffer = ((UINT8 *) Buffer);
244 for (Index = 0; Index < BufferLen; Index ++, TemBuffer ++) {
245 TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemBuffer, 2);
246 }
247 break;
248 case 2:
249 //
250 // Check buffer is enough
251 //
252 TemName = (CHAR16 *) Buffer;
253 ASSERT ((BufferLen * 2 + 1) >= (StrLen (TemName) * 4 + 1));
254 //
255 // Convert Unicode String to Config String, e.g. "ABCD" => "0041004200430044"
256 //
257 for (; *TemName != L'\0'; TemName++) {
258 TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemName, 4);
259 }
260 break;
261 case 3:
262 //
263 // Convert Buffer to Hex String
264 //
265 TemBuffer = ((UINT8 *) Buffer) + BufferLen - 1;
266 for (Index = 0; Index < BufferLen; Index ++, TemBuffer --) {
267 TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemBuffer, 2);
268 }
269 break;
270 default:
271 break;
272 }
273
274 //
275 // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.
276 //
277 StrCat (Str, L"&");
278 HiiToLower (Str);
279
280 *SubStr = Str;
281 }
282
283
284 /**
285 Retrieve the <ConfigBody> from String then output it.
286
287 This is a internal function.
288
289 @param String A sub string of a configuration string in
290 <MultiConfigAltResp> format.
291 @param ConfigBody Points to the output string. It's caller's
292 responsibility to free this buffer.
293
294 @retval EFI_INVALID_PARAMETER There is no form package in current hii database.
295 @retval EFI_OUT_OF_RESOURCES Not enough memory to finish this operation.
296 @retval EFI_SUCCESS All existing storage is exported.
297
298 **/
299 EFI_STATUS
300 OutputConfigBody (
301 IN EFI_STRING String,
302 OUT EFI_STRING *ConfigBody
303 )
304 {
305 EFI_STRING TmpPtr;
306 EFI_STRING Result;
307 UINTN Length;
308
309 if (String == NULL || ConfigBody == NULL) {
310 return EFI_INVALID_PARAMETER;
311 }
312
313 //
314 // The setting information should start OFFSET, not ALTCFG.
315 //
316 if (StrnCmp (String, L"&ALTCFG=", StrLen (L"&ALTCFG=")) == 0) {
317 return EFI_INVALID_PARAMETER;
318 }
319
320 TmpPtr = StrStr (String, L"GUID=");
321 if (TmpPtr == NULL) {
322 //
323 // It is the last <ConfigResp> of the incoming configuration string.
324 //
325 Result = AllocateCopyPool (StrSize (String), String);
326 if (Result == NULL) {
327 return EFI_OUT_OF_RESOURCES;
328 } else {
329 *ConfigBody = Result;
330 return EFI_SUCCESS;
331 }
332 }
333
334 Length = TmpPtr - String;
335 Result = AllocateCopyPool (Length * sizeof (CHAR16), String);
336 if (Result == NULL) {
337 return EFI_OUT_OF_RESOURCES;
338 }
339
340 *(Result + Length - 1) = 0;
341 *ConfigBody = Result;
342 return EFI_SUCCESS;
343 }
344
345 /**
346 Append a string to a multi-string format.
347
348 This is a internal function.
349
350 @param MultiString String in <MultiConfigRequest>,
351 <MultiConfigAltResp>, or <MultiConfigResp>. On
352 input, the buffer length of this string is
353 MAX_STRING_LENGTH. On output, the buffer length
354 might be updated.
355 @param AppendString NULL-terminated Unicode string.
356
357 @retval EFI_INVALID_PARAMETER Any incoming parameter is invalid.
358 @retval EFI_SUCCESS AppendString is append to the end of MultiString
359
360 **/
361 EFI_STATUS
362 AppendToMultiString (
363 IN OUT EFI_STRING *MultiString,
364 IN EFI_STRING AppendString
365 )
366 {
367 UINTN AppendStringSize;
368 UINTN MultiStringSize;
369
370 if (MultiString == NULL || *MultiString == NULL || AppendString == NULL) {
371 return EFI_INVALID_PARAMETER;
372 }
373
374 AppendStringSize = StrSize (AppendString);
375 MultiStringSize = StrSize (*MultiString);
376
377 //
378 // Enlarge the buffer each time when length exceeds MAX_STRING_LENGTH.
379 //
380 if (MultiStringSize + AppendStringSize > MAX_STRING_LENGTH ||
381 MultiStringSize > MAX_STRING_LENGTH) {
382 *MultiString = (EFI_STRING) ReallocatePool (
383 MultiStringSize,
384 MultiStringSize + AppendStringSize,
385 (VOID *) (*MultiString)
386 );
387 ASSERT (*MultiString != NULL);
388 }
389 //
390 // Append the incoming string
391 //
392 StrCat (*MultiString, AppendString);
393
394 return EFI_SUCCESS;
395 }
396
397
398 /**
399 Get the value of <Number> in <BlockConfig> format, i.e. the value of OFFSET
400 or WIDTH or VALUE.
401 <BlockConfig> ::= 'OFFSET='<Number>&'WIDTH='<Number>&'VALUE'=<Number>
402
403 This is a internal function.
404
405 @param StringPtr String in <BlockConfig> format and points to the
406 first character of <Number>.
407 @param Number The output value. Caller takes the responsibility
408 to free memory.
409 @param Len Length of the <Number>, in characters.
410
411 @retval EFI_OUT_OF_RESOURCES Insufficient resources to store neccessary
412 structures.
413 @retval EFI_SUCCESS Value of <Number> is outputted in Number
414 successfully.
415
416 **/
417 EFI_STATUS
418 GetValueOfNumber (
419 IN EFI_STRING StringPtr,
420 OUT UINT8 **Number,
421 OUT UINTN *Len
422 )
423 {
424 EFI_STRING TmpPtr;
425 UINTN Length;
426 EFI_STRING Str;
427 UINT8 *Buf;
428 EFI_STATUS Status;
429 UINT8 DigitUint8;
430 UINTN Index;
431 CHAR16 TemStr[2];
432
433 ASSERT (StringPtr != NULL && Number != NULL && Len != NULL);
434 ASSERT (*StringPtr != L'\0');
435
436 Buf = NULL;
437
438 TmpPtr = StringPtr;
439 while (*StringPtr != L'\0' && *StringPtr != L'&') {
440 StringPtr++;
441 }
442 *Len = StringPtr - TmpPtr;
443 Length = *Len + 1;
444
445 Str = (EFI_STRING) AllocateZeroPool (Length * sizeof (CHAR16));
446 if (Str == NULL) {
447 Status = EFI_OUT_OF_RESOURCES;
448 goto Exit;
449 }
450 CopyMem (Str, TmpPtr, *Len * sizeof (CHAR16));
451 *(Str + *Len) = L'\0';
452
453 Length = (Length + 1) / 2;
454 Buf = (UINT8 *) AllocateZeroPool (Length);
455 if (Buf == NULL) {
456 Status = EFI_OUT_OF_RESOURCES;
457 goto Exit;
458 }
459
460 Length = *Len;
461 ZeroMem (TemStr, sizeof (TemStr));
462 for (Index = 0; Index < Length; Index ++) {
463 TemStr[0] = Str[Length - Index - 1];
464 DigitUint8 = (UINT8) StrHexToUint64 (TemStr);
465 if ((Index & 1) == 0) {
466 Buf [Index/2] = DigitUint8;
467 } else {
468 Buf [Index/2] = (UINT8) ((DigitUint8 << 4) + Buf [Index/2]);
469 }
470 }
471
472 *Number = Buf;
473 Status = EFI_SUCCESS;
474
475 Exit:
476 if (Str != NULL) {
477 FreePool (Str);
478 }
479
480 return Status;
481 }
482
483 /**
484 This function merges DefaultAltCfgResp string into AltCfgResp string for
485 the missing AltCfgId in AltCfgResq.
486
487 @param AltCfgResp Pointer to a null-terminated Unicode string in
488 <ConfigAltResp> format. The default value string
489 will be merged into it.
490 @param DefaultAltCfgResp Pointer to a null-terminated Unicode string in
491 <MultiConfigAltResp> format. The default value
492 string may contain more than one ConfigAltResp
493 string for the different varstore buffer.
494
495 @retval EFI_SUCCESS The merged string returns.
496 @retval EFI_INVALID_PARAMETER *AltCfgResp is to NULL.
497 **/
498 EFI_STATUS
499 EFIAPI
500 MergeDefaultString (
501 IN OUT EFI_STRING *AltCfgResp,
502 IN EFI_STRING DefaultAltCfgResp
503 )
504 {
505 EFI_STRING StringPtrDefault;
506 EFI_STRING StringPtrEnd;
507 CHAR16 TempChar;
508 EFI_STRING StringPtr;
509 EFI_STRING AltConfigHdr;
510 UINTN HeaderLength;
511 UINTN SizeAltCfgResp;
512
513 if (*AltCfgResp == NULL) {
514 return EFI_INVALID_PARAMETER;
515 }
516
517 //
518 // Get the requestr ConfigHdr
519 //
520 SizeAltCfgResp = 0;
521 StringPtr = *AltCfgResp;
522
523 //
524 // Find <ConfigHdr> GUID=...&NAME=...&PATH=...
525 //
526 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
527 return EFI_INVALID_PARAMETER;
528 }
529 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&NAME=", StrLen (L"&NAME=")) != 0) {
530 StringPtr++;
531 }
532 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&PATH=", StrLen (L"&PATH=")) != 0) {
533 StringPtr++;
534 }
535 if (*StringPtr == L'\0') {
536 return EFI_INVALID_PARAMETER;
537 }
538 StringPtr += StrLen (L"&PATH=");
539 while (*StringPtr != L'\0' && *StringPtr != L'&') {
540 StringPtr ++;
541 }
542 HeaderLength = StringPtr - *AltCfgResp;
543
544 //
545 // Construct AltConfigHdr string "&<ConfigHdr>&ALTCFG=XXXX\0"
546 // |1| StrLen (ConfigHdr) | 8 | 4 | 1 |
547 //
548 AltConfigHdr = AllocateZeroPool ((1 + HeaderLength + 8 + 4 + 1) * sizeof (CHAR16));
549 if (AltConfigHdr == NULL) {
550 return EFI_OUT_OF_RESOURCES;
551 }
552 StrCpy (AltConfigHdr, L"&");
553 StrnCat (AltConfigHdr, *AltCfgResp, HeaderLength);
554 StrCat (AltConfigHdr, L"&ALTCFG=");
555 HeaderLength = StrLen (AltConfigHdr);
556
557 StringPtrDefault = StrStr (DefaultAltCfgResp, AltConfigHdr);
558 while (StringPtrDefault != NULL) {
559 //
560 // Get AltCfg Name
561 //
562 StrnCat (AltConfigHdr, StringPtrDefault + HeaderLength, 4);
563 StringPtr = StrStr (*AltCfgResp, AltConfigHdr);
564
565 //
566 // Append the found default value string to the input AltCfgResp
567 //
568 if (StringPtr == NULL) {
569 StringPtrEnd = StrStr (StringPtrDefault + 1, L"&GUID");
570 SizeAltCfgResp = StrSize (*AltCfgResp);
571 if (StringPtrEnd == NULL) {
572 //
573 // No more default string is found.
574 //
575 *AltCfgResp = (EFI_STRING) ReallocatePool (
576 SizeAltCfgResp,
577 SizeAltCfgResp + StrSize (StringPtrDefault),
578 (VOID *) (*AltCfgResp)
579 );
580 StrCat (*AltCfgResp, StringPtrDefault);
581 break;
582 } else {
583 TempChar = *StringPtrEnd;
584 *StringPtrEnd = L'\0';
585 *AltCfgResp = (EFI_STRING) ReallocatePool (
586 SizeAltCfgResp,
587 SizeAltCfgResp + StrSize (StringPtrDefault),
588 (VOID *) (*AltCfgResp)
589 );
590 StrCat (*AltCfgResp, StringPtrDefault);
591 *StringPtrEnd = TempChar;
592 }
593 }
594
595 //
596 // Find next AltCfg String
597 //
598 *(AltConfigHdr + HeaderLength) = L'\0';
599 StringPtrDefault = StrStr (StringPtrDefault + 1, AltConfigHdr);
600 }
601
602 return EFI_SUCCESS;
603 }
604
605 /**
606 This function finds the matched DefaultName for the input DefaultId
607
608 @param DefaultIdArray Array stores the map table between DefaultId and DefaultName.
609 @param VarDefaultId Default Id
610 @param VarDefaultName Default Name string ID for the input default ID.
611
612 @retval EFI_SUCCESS The mapped default name string ID is found.
613 @retval EFI_NOT_FOUND The mapped default name string ID is not found.
614 **/
615 EFI_STATUS
616 FindDefaultName (
617 IN IFR_DEFAULT_DATA *DefaultIdArray,
618 IN UINT16 VarDefaultId,
619 OUT EFI_STRING_ID *VarDefaultName
620 )
621 {
622 LIST_ENTRY *Link;
623 IFR_DEFAULT_DATA *DefaultData;
624
625 for (Link = DefaultIdArray->Entry.ForwardLink; Link != &DefaultIdArray->Entry; Link = Link->ForwardLink) {
626 DefaultData = BASE_CR (Link, IFR_DEFAULT_DATA, Entry);
627 if (DefaultData->DefaultId == VarDefaultId) {
628 *VarDefaultName = DefaultData->DefaultName;
629 return EFI_SUCCESS;
630 }
631 }
632
633 return EFI_NOT_FOUND;
634 }
635
636 /**
637 This function inserts new DefaultValueData into the BlockData DefaultValue array.
638
639 @param BlockData The BlockData is updated to add new default value.
640 @param DefaultValueData The DefaultValue is added.
641
642 **/
643 VOID
644 InsertDefaultValue (
645 IN IFR_BLOCK_DATA *BlockData,
646 IN IFR_DEFAULT_DATA *DefaultValueData
647 )
648 {
649 LIST_ENTRY *Link;
650 IFR_DEFAULT_DATA *DefaultValueArray;
651
652 for (Link = BlockData->DefaultValueEntry.ForwardLink; Link != &BlockData->DefaultValueEntry; Link = Link->ForwardLink) {
653 DefaultValueArray = BASE_CR (Link, IFR_DEFAULT_DATA, Entry);
654 if (DefaultValueArray->DefaultId == DefaultValueData->DefaultId) {
655 //
656 // Update the default value array in BlockData.
657 //
658 DefaultValueArray->Value = DefaultValueData->Value;
659 FreePool (DefaultValueData);
660 return;
661 } else if (DefaultValueArray->DefaultId > DefaultValueData->DefaultId) {
662 //
663 // Insert new default value data in the front of this default value array.
664 //
665 InsertTailList (Link, &DefaultValueData->Entry);
666 return;
667 }
668 }
669
670 //
671 // Insert new default value data in tail.
672 //
673 InsertTailList (Link, &DefaultValueData->Entry);
674 return;
675 }
676
677 /**
678 This function inserts new BlockData into the block link
679
680 @param BlockLink The list entry points to block array.
681 @param BlockData The point to BlockData is added.
682
683 **/
684 VOID
685 InsertBlockData (
686 IN LIST_ENTRY *BlockLink,
687 IN IFR_BLOCK_DATA **BlockData
688 )
689 {
690 LIST_ENTRY *Link;
691 IFR_BLOCK_DATA *BlockArray;
692 IFR_BLOCK_DATA *BlockSingleData;
693
694 BlockSingleData = *BlockData;
695
696 //
697 // Insert block data in its Offset and Width order.
698 //
699 for (Link = BlockLink->ForwardLink; Link != BlockLink; Link = Link->ForwardLink) {
700 BlockArray = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
701 if (BlockArray->Offset == BlockSingleData->Offset) {
702 if (BlockArray->Width > BlockSingleData->Width) {
703 //
704 // Insert this block data in the front of block array
705 //
706 InsertTailList (Link, &BlockSingleData->Entry);
707 return;
708 }
709
710 if (BlockArray->Width == BlockSingleData->Width) {
711 //
712 // The same block array has been added.
713 //
714 FreePool (BlockSingleData);
715 *BlockData = BlockArray;
716 return;
717 }
718 } else if (BlockArray->Offset > BlockSingleData->Offset) {
719 //
720 // Insert new block data in the front of block array
721 //
722 InsertTailList (Link, &BlockSingleData->Entry);
723 return;
724 }
725 }
726
727 //
728 // Add new block data into the tail.
729 //
730 InsertTailList (Link, &BlockSingleData->Entry);
731 return;
732 }
733
734 /**
735 This function checks VarOffset and VarWidth is in the block range.
736
737 @param BlockArray The block array is to be checked.
738 @param VarOffset Offset of var to the structure
739 @param VarWidth Width of var.
740
741 @retval TRUE This Var is in the block range.
742 @retval FALSE This Var is not in the block range.
743 **/
744 BOOLEAN
745 BlockArrayCheck (
746 IN IFR_BLOCK_DATA *RequestBlockArray,
747 IN UINT16 VarOffset,
748 IN UINT16 VarWidth
749 )
750 {
751 LIST_ENTRY *Link;
752 IFR_BLOCK_DATA *BlockData;
753
754 //
755 // No Request Block array, all vars are got.
756 //
757 if (RequestBlockArray == NULL) {
758 return TRUE;
759 }
760
761 //
762 // Check the input var is in the request block range.
763 //
764 for (Link = RequestBlockArray->Entry.ForwardLink; Link != &RequestBlockArray->Entry; Link = Link->ForwardLink) {
765 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
766 if ((VarOffset >= BlockData->Offset) && ((VarOffset + VarWidth) <= (BlockData->Offset + BlockData->Width))) {
767 return TRUE;
768 }
769 }
770
771 return FALSE;
772 }
773
774 /**
775 This function parses Form Package to get the block array and the default
776 value array according to the request ConfigHdr.
777
778 @param Package Pointer to the form package data.
779 @param PackageLength Length of the pacakge.
780 @param ConfigHdr Request string ConfigHdr. If it is NULL,
781 the first found varstore will be as ConfigHdr.
782 @param RequestBlockArray The block array is retrieved from the request string.
783 @param VarStorageData VarStorage structure contains the got block and default value.
784 @param PIfrDefaultIdArray Point to the got default id and default name array.
785
786 @retval EFI_SUCCESS The block array and the default value array are got.
787 @retval EFI_INVALID_PARAMETER The varstore defintion in the differnt form pacakges
788 are conflicted.
789 @retval EFI_OUT_OF_RESOURCES No enough memory.
790 **/
791 EFI_STATUS
792 EFIAPI
793 ParseIfrData (
794 IN UINT8 *Package,
795 IN UINT32 PackageLenth,
796 IN EFI_STRING ConfigHdr,
797 IN IFR_BLOCK_DATA *RequestBlockArray,
798 IN OUT IFR_VARSTORAGE_DATA *VarStorageData,
799 OUT IFR_DEFAULT_DATA **PIfrDefaultIdArray
800 )
801 {
802 EFI_STATUS Status;
803 UINTN IfrOffset;
804 EFI_IFR_VARSTORE *IfrVarStore;
805 EFI_IFR_OP_HEADER *IfrOpHdr;
806 EFI_IFR_ONE_OF *IfrOneOf;
807 EFI_IFR_ONE_OF_OPTION *IfrOneOfOption;
808 EFI_IFR_DEFAULT *IfrDefault;
809 EFI_IFR_ORDERED_LIST *IfrOrderedList;
810 EFI_IFR_CHECKBOX *IfrCheckBox;
811 EFI_IFR_PASSWORD *IfrPassword;
812 EFI_IFR_STRING *IfrString;
813 IFR_DEFAULT_DATA *DefaultIdArray;
814 IFR_DEFAULT_DATA *DefaultData;
815 IFR_BLOCK_DATA *BlockData;
816 CHAR16 *VarStoreName;
817 UINT16 VarOffset;
818 UINT16 VarWidth;
819 EFI_STRING_ID VarDefaultName;
820 UINT16 VarDefaultId;
821 EFI_STRING GuidStr;
822 EFI_STRING NameStr;
823 EFI_STRING TempStr;
824 UINTN LengthString;
825
826 //
827 // Initialize DefaultIdArray to store the map between DeaultId and DefaultName
828 //
829 LengthString = 0;
830 Status = EFI_SUCCESS;
831 GuidStr = NULL;
832 NameStr = NULL;
833 TempStr = NULL;
834 BlockData = NULL;
835 DefaultData = NULL;
836 DefaultIdArray = (IFR_DEFAULT_DATA *) AllocateZeroPool (sizeof (IFR_DEFAULT_DATA));
837 if (DefaultIdArray == NULL) {
838 return EFI_OUT_OF_RESOURCES;
839 }
840 InitializeListHead (&DefaultIdArray->Entry);
841
842 //
843 // Go through the form package to parse OpCode one by one.
844 //
845 IfrOffset = sizeof (EFI_HII_PACKAGE_HEADER);
846 while (IfrOffset < PackageLenth) {
847 IfrOpHdr = (EFI_IFR_OP_HEADER *) (Package + IfrOffset);
848
849 switch (IfrOpHdr->OpCode) {
850 case EFI_IFR_VARSTORE_OP:
851 //
852 // VarStore is found. Don't need to search any more.
853 //
854 if (VarStorageData->Size != 0) {
855 break;
856 }
857
858 //
859 // Get the requied varstore information
860 // Add varstore by Guid and Name in ConfigHdr
861 // Make sure Offset is in varstore size and varstoreid
862 //
863 IfrVarStore = (EFI_IFR_VARSTORE *) IfrOpHdr;
864 VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrVarStore->Name) * sizeof (CHAR16));
865 if (VarStoreName == NULL) {
866 Status = EFI_OUT_OF_RESOURCES;
867 goto Done;
868 }
869 AsciiStrToUnicodeStr ((CHAR8 *) IfrVarStore->Name, VarStoreName);
870
871 GenerateSubStr (L"GUID=", sizeof (EFI_GUID), (VOID *) &IfrVarStore->Guid, 1, &GuidStr);
872 GenerateSubStr (L"NAME=", StrLen (VarStoreName) * sizeof (CHAR16), (VOID *) VarStoreName, 2, &NameStr);
873 LengthString = StrLen (GuidStr);
874 LengthString = LengthString + StrLen (NameStr) + 1;
875 TempStr = AllocateZeroPool (LengthString * sizeof (CHAR16));
876 if (TempStr == NULL) {
877 FreePool (GuidStr);
878 FreePool (NameStr);
879 Status = EFI_OUT_OF_RESOURCES;
880 goto Done;
881 }
882 StrCpy (TempStr, GuidStr);
883 StrCat (TempStr, NameStr);
884 if (ConfigHdr == NULL || StrnCmp (ConfigHdr, TempStr, StrLen (TempStr)) == 0) {
885 //
886 // Find the matched VarStore
887 //
888 CopyGuid (&VarStorageData->Guid, (EFI_GUID *) (VOID *) &IfrVarStore->Guid);
889 VarStorageData->VarStoreId = IfrVarStore->VarStoreId;
890 VarStorageData->Size = IfrVarStore->Size;
891 VarStorageData->Name = VarStoreName;
892 } else {
893 //
894 // No found, free the allocated memory
895 //
896 FreePool (VarStoreName);
897 }
898 //
899 // Free alllocated temp string.
900 //
901 FreePool (TempStr);
902 FreePool (GuidStr);
903 FreePool (NameStr);
904 break;
905
906 case EFI_IFR_DEFAULTSTORE_OP:
907 //
908 // Add new the map between default id and default name.
909 //
910 DefaultData = (IFR_DEFAULT_DATA *) AllocateZeroPool (sizeof (IFR_DEFAULT_DATA));
911 if (DefaultData == NULL) {
912 Status = EFI_OUT_OF_RESOURCES;
913 goto Done;
914 }
915 DefaultData->DefaultId = ((EFI_IFR_DEFAULTSTORE *) IfrOpHdr)->DefaultId;
916 DefaultData->DefaultName = ((EFI_IFR_DEFAULTSTORE *) IfrOpHdr)->DefaultName;
917 InsertTailList (&DefaultIdArray->Entry, &DefaultData->Entry);
918 DefaultData = NULL;
919 break;
920
921 case EFI_IFR_FORM_OP:
922 //
923 // No matched varstore is found and directly return.
924 //
925 if (VarStorageData->Size == 0) {
926 Status = EFI_SUCCESS;
927 goto Done;
928 }
929 break;
930
931 case EFI_IFR_ONE_OF_OP:
932 case EFI_IFR_NUMERIC_OP:
933 //
934 // Numeric and OneOf has the same opcode structure.
935 //
936
937 //
938 // Check whether this question is for the requested varstore.
939 //
940 IfrOneOf = (EFI_IFR_ONE_OF *) IfrOpHdr;
941 if (IfrOneOf->Question.VarStoreId != VarStorageData->VarStoreId) {
942 break;
943 }
944
945 //
946 // Get Offset/Width by Question header and OneOf Flags
947 //
948 VarOffset = IfrOneOf->Question.VarStoreInfo.VarOffset;
949 VarWidth = (UINT16) (1 << (IfrOneOf->Flags & EFI_IFR_NUMERIC_SIZE));
950 //
951 // Check whether this question is in requested block array.
952 //
953 if (!BlockArrayCheck (RequestBlockArray, VarOffset, VarWidth)) {
954 //
955 // This question is not in the requested string. Skip it.
956 //
957 break;
958 }
959
960 //
961 // Check this var question is in the var storage
962 //
963 if ((VarOffset + VarWidth) > VarStorageData->Size) {
964 Status = EFI_INVALID_PARAMETER;
965 goto Done;
966 }
967
968 //
969 // Set Block Data
970 //
971 BlockData = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
972 if (BlockData == NULL) {
973 Status = EFI_OUT_OF_RESOURCES;
974 goto Done;
975 }
976 BlockData->Offset = VarOffset;
977 BlockData->Width = VarWidth;
978 BlockData->QuestionId = IfrOneOf->Question.QuestionId;
979 BlockData->OpCode = IfrOpHdr->OpCode;
980 BlockData->Scope = IfrOpHdr->Scope;
981 InitializeListHead (&BlockData->DefaultValueEntry);
982 //
983 // Add Block Data into VarStorageData BlockEntry
984 //
985 InsertBlockData (&VarStorageData->BlockEntry, &BlockData);
986 break;
987
988 case EFI_IFR_ORDERED_LIST_OP:
989 //
990 // offset by question header
991 // width by EFI_IFR_ORDERED_LIST MaxContainers * OneofOption Type
992 // no default value and default id, how to define its default value?
993 //
994
995 //
996 // Check whether this question is for the requested varstore.
997 //
998 IfrOrderedList = (EFI_IFR_ORDERED_LIST *) IfrOpHdr;
999 if (IfrOrderedList->Question.VarStoreId != VarStorageData->VarStoreId) {
1000 break;
1001 }
1002
1003 //
1004 // Get Offset/Width by Question header and OneOf Flags
1005 //
1006 VarOffset = IfrOrderedList->Question.VarStoreInfo.VarOffset;
1007 VarWidth = IfrOrderedList->MaxContainers;
1008
1009 //
1010 // Check whether this question is in requested block array.
1011 //
1012 if (!BlockArrayCheck (RequestBlockArray, VarOffset, VarWidth)) {
1013 //
1014 // This question is not in the requested string. Skip it.
1015 //
1016 break;
1017 }
1018
1019 //
1020 // Check this var question is in the var storage
1021 //
1022 if ((VarOffset + VarWidth) > VarStorageData->Size) {
1023 Status = EFI_INVALID_PARAMETER;
1024 goto Done;
1025 }
1026
1027 //
1028 // Set Block Data
1029 //
1030 BlockData = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
1031 if (BlockData == NULL) {
1032 Status = EFI_OUT_OF_RESOURCES;
1033 goto Done;
1034 }
1035 BlockData->Offset = VarOffset;
1036 BlockData->Width = VarWidth;
1037 BlockData->QuestionId = IfrOrderedList->Question.QuestionId;
1038 BlockData->OpCode = IfrOpHdr->OpCode;
1039 BlockData->Scope = IfrOpHdr->Scope;
1040 InitializeListHead (&BlockData->DefaultValueEntry);
1041
1042 //
1043 // Add Block Data into VarStorageData BlockEntry
1044 //
1045 InsertBlockData (&VarStorageData->BlockEntry, &BlockData);
1046 break;
1047
1048 case EFI_IFR_CHECKBOX_OP:
1049 //
1050 // EFI_IFR_DEFAULT_OP
1051 // offset by question header
1052 // width is 1 sizeof (BOOLEAN)
1053 // default id by CheckBox Flags if CheckBox flags (Default or Mau) is set, the default value is 1 to be set.
1054 // value by DefaultOption
1055 // default id by DeaultOption DefaultId can override CheckBox Flags and Default value.
1056 //
1057
1058 //
1059 // Check whether this question is for the requested varstore.
1060 //
1061 IfrCheckBox = (EFI_IFR_CHECKBOX *) IfrOpHdr;
1062 if (IfrCheckBox->Question.VarStoreId != VarStorageData->VarStoreId) {
1063 break;
1064 }
1065
1066 //
1067 // Get Offset/Width by Question header and OneOf Flags
1068 //
1069 VarOffset = IfrCheckBox->Question.VarStoreInfo.VarOffset;
1070 VarWidth = sizeof (BOOLEAN);
1071
1072 //
1073 // Check whether this question is in requested block array.
1074 //
1075 if (!BlockArrayCheck (RequestBlockArray, VarOffset, VarWidth)) {
1076 //
1077 // This question is not in the requested string. Skip it.
1078 //
1079 break;
1080 }
1081
1082 //
1083 // Check this var question is in the var storage
1084 //
1085 if ((VarOffset + VarWidth) > VarStorageData->Size) {
1086 Status = EFI_INVALID_PARAMETER;
1087 goto Done;
1088 }
1089
1090 //
1091 // Set Block Data
1092 //
1093 BlockData = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
1094 if (BlockData == NULL) {
1095 Status = EFI_OUT_OF_RESOURCES;
1096 goto Done;
1097 }
1098 BlockData->Offset = VarOffset;
1099 BlockData->Width = VarWidth;
1100 BlockData->QuestionId = IfrCheckBox->Question.QuestionId;
1101 BlockData->OpCode = IfrOpHdr->OpCode;
1102 BlockData->Scope = IfrOpHdr->Scope;
1103 InitializeListHead (&BlockData->DefaultValueEntry);
1104 //
1105 // Add Block Data into VarStorageData BlockEntry
1106 //
1107 InsertBlockData (&VarStorageData->BlockEntry, &BlockData);
1108
1109 //
1110 // Add default value by CheckBox Flags
1111 //
1112 if ((IfrCheckBox->Flags & EFI_IFR_CHECKBOX_DEFAULT) == EFI_IFR_CHECKBOX_DEFAULT) {
1113 //
1114 // Set standard ID to Manufacture ID and Get DefaultName String ID
1115 //
1116 VarDefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
1117 Status = FindDefaultName (DefaultIdArray, VarDefaultId, &VarDefaultName);
1118 if (EFI_ERROR (Status)) {
1119 goto Done;
1120 }
1121 //
1122 // Prepare new DefaultValue
1123 //
1124 DefaultData = (IFR_DEFAULT_DATA *) AllocateZeroPool (sizeof (IFR_DEFAULT_DATA));
1125 if (DefaultData == NULL) {
1126 Status = EFI_OUT_OF_RESOURCES;
1127 goto Done;
1128 }
1129 DefaultData->DefaultId = VarDefaultId;
1130 DefaultData->DefaultName = VarDefaultName;
1131 DefaultData->Value = 1;
1132 //
1133 // Add DefaultValue into current BlockData
1134 //
1135 InsertDefaultValue (BlockData, DefaultData);
1136 }
1137
1138 if ((IfrCheckBox->Flags & EFI_IFR_CHECKBOX_DEFAULT_MFG) == EFI_IFR_CHECKBOX_DEFAULT_MFG) {
1139 //
1140 // Set standard ID to Manufacture ID and Get DefaultName String ID
1141 //
1142 VarDefaultId = EFI_HII_DEFAULT_CLASS_MANUFACTURING;
1143 Status = FindDefaultName (DefaultIdArray, VarDefaultId, &VarDefaultName);
1144 if (EFI_ERROR (Status)) {
1145 goto Done;
1146 }
1147 //
1148 // Prepare new DefaultValue
1149 //
1150 DefaultData = (IFR_DEFAULT_DATA *) AllocateZeroPool (sizeof (IFR_DEFAULT_DATA));
1151 if (DefaultData == NULL) {
1152 Status = EFI_OUT_OF_RESOURCES;
1153 goto Done;
1154 }
1155 DefaultData->DefaultId = VarDefaultId;
1156 DefaultData->DefaultName = VarDefaultName;
1157 DefaultData->Value = 1;
1158 //
1159 // Add DefaultValue into current BlockData
1160 //
1161 InsertDefaultValue (BlockData, DefaultData);
1162 }
1163 break;
1164
1165 case EFI_IFR_STRING_OP:
1166 //
1167 // offset by question header
1168 // width MaxSize * sizeof (CHAR16)
1169 // no default value, only block array
1170 //
1171
1172 //
1173 // Check whether this question is for the requested varstore.
1174 //
1175 IfrString = (EFI_IFR_STRING *) IfrOpHdr;
1176 if (IfrString->Question.VarStoreId != VarStorageData->VarStoreId) {
1177 break;
1178 }
1179
1180 //
1181 // Get Offset/Width by Question header and OneOf Flags
1182 //
1183 VarOffset = IfrString->Question.VarStoreInfo.VarOffset;
1184 VarWidth = (UINT16) (IfrString->MaxSize * sizeof (UINT16));
1185
1186 //
1187 // Check whether this question is in requested block array.
1188 //
1189 if (!BlockArrayCheck (RequestBlockArray, VarOffset, VarWidth)) {
1190 //
1191 // This question is not in the requested string. Skip it.
1192 //
1193 break;
1194 }
1195
1196 //
1197 // Check this var question is in the var storage
1198 //
1199 if ((VarOffset + VarWidth) > VarStorageData->Size) {
1200 Status = EFI_INVALID_PARAMETER;
1201 goto Done;
1202 }
1203
1204 //
1205 // Set Block Data
1206 //
1207 BlockData = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
1208 if (BlockData == NULL) {
1209 Status = EFI_OUT_OF_RESOURCES;
1210 goto Done;
1211 }
1212 BlockData->Offset = VarOffset;
1213 BlockData->Width = VarWidth;
1214 BlockData->QuestionId = IfrString->Question.QuestionId;
1215 BlockData->OpCode = IfrOpHdr->OpCode;
1216 InitializeListHead (&BlockData->DefaultValueEntry);
1217
1218 //
1219 // Add Block Data into VarStorageData BlockEntry
1220 //
1221 InsertBlockData (&VarStorageData->BlockEntry, &BlockData);
1222
1223 //
1224 // No default value for string.
1225 //
1226 BlockData = NULL;
1227 break;
1228
1229 case EFI_IFR_PASSWORD_OP:
1230 //
1231 // offset by question header
1232 // width MaxSize * sizeof (CHAR16)
1233 // no default value, only block array
1234 //
1235
1236 //
1237 // Check whether this question is for the requested varstore.
1238 //
1239 IfrPassword = (EFI_IFR_PASSWORD *) IfrOpHdr;
1240 if (IfrPassword->Question.VarStoreId != VarStorageData->VarStoreId) {
1241 break;
1242 }
1243
1244 //
1245 // Get Offset/Width by Question header and OneOf Flags
1246 //
1247 VarOffset = IfrPassword->Question.VarStoreInfo.VarOffset;
1248 VarWidth = (UINT16) (IfrPassword->MaxSize * sizeof (UINT16));
1249
1250 //
1251 // Check whether this question is in requested block array.
1252 //
1253 if (!BlockArrayCheck (RequestBlockArray, VarOffset, VarWidth)) {
1254 //
1255 // This question is not in the requested string. Skip it.
1256 //
1257 break;
1258 }
1259
1260 //
1261 // Check this var question is in the var storage
1262 //
1263 if ((VarOffset + VarWidth) > VarStorageData->Size) {
1264 Status = EFI_INVALID_PARAMETER;
1265 goto Done;
1266 }
1267
1268 //
1269 // Set Block Data
1270 //
1271 BlockData = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
1272 if (BlockData == NULL) {
1273 Status = EFI_OUT_OF_RESOURCES;
1274 goto Done;
1275 }
1276 BlockData->Offset = VarOffset;
1277 BlockData->Width = VarWidth;
1278 BlockData->QuestionId = IfrPassword->Question.QuestionId;
1279 BlockData->OpCode = IfrOpHdr->OpCode;
1280 InitializeListHead (&BlockData->DefaultValueEntry);
1281
1282 //
1283 // Add Block Data into VarStorageData BlockEntry
1284 //
1285 InsertBlockData (&VarStorageData->BlockEntry, &BlockData);
1286
1287 //
1288 // No default value for string.
1289 //
1290 BlockData = NULL;
1291 break;
1292
1293 case EFI_IFR_ONE_OF_OPTION_OP:
1294 //
1295 // No matched block data is ignored.
1296 //
1297 if (BlockData == NULL || BlockData->Scope == 0) {
1298 break;
1299 }
1300
1301 IfrOneOfOption = (EFI_IFR_ONE_OF_OPTION *) IfrOpHdr;
1302 if (BlockData->OpCode == EFI_IFR_ORDERED_LIST_OP) {
1303 //
1304 // Get ordered list option data type.
1305 //
1306 if (IfrOneOfOption->Type == EFI_IFR_TYPE_NUM_SIZE_8 || IfrOneOfOption->Type == EFI_IFR_TYPE_BOOLEAN) {
1307 VarWidth = 1;
1308 } else if (IfrOneOfOption->Type == EFI_IFR_TYPE_NUM_SIZE_16) {
1309 VarWidth = 2;
1310 } else if (IfrOneOfOption->Type == EFI_IFR_TYPE_NUM_SIZE_32) {
1311 VarWidth = 4;
1312 } else if (IfrOneOfOption->Type == EFI_IFR_TYPE_NUM_SIZE_64) {
1313 VarWidth = 8;
1314 } else {
1315 //
1316 // Invalid ordered list option data type.
1317 //
1318 Status = EFI_INVALID_PARAMETER;
1319 goto Done;
1320 }
1321 //
1322 // Calculate Ordered list QuestionId width.
1323 //
1324 BlockData->Width = (UINT16) (BlockData->Width * VarWidth);
1325 BlockData = NULL;
1326 break;
1327 }
1328
1329 if ((IfrOneOfOption->Flags & EFI_IFR_OPTION_DEFAULT) == EFI_IFR_OPTION_DEFAULT) {
1330 //
1331 // Set standard ID to Manufacture ID and Get DefaultName String ID
1332 //
1333 VarDefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
1334 Status = FindDefaultName (DefaultIdArray, VarDefaultId, &VarDefaultName);
1335 if (EFI_ERROR (Status)) {
1336 goto Done;
1337 }
1338 //
1339 // Prepare new DefaultValue
1340 //
1341 DefaultData = (IFR_DEFAULT_DATA *) AllocateZeroPool (sizeof (IFR_DEFAULT_DATA));
1342 if (DefaultData == NULL) {
1343 Status = EFI_OUT_OF_RESOURCES;
1344 goto Done;
1345 }
1346 DefaultData->DefaultId = VarDefaultId;
1347 DefaultData->DefaultName = VarDefaultName;
1348 DefaultData->Value = IfrOneOfOption->Value.u64;
1349 //
1350 // Add DefaultValue into current BlockData
1351 //
1352 InsertDefaultValue (BlockData, DefaultData);
1353 }
1354
1355 if ((IfrOneOfOption->Flags & EFI_IFR_OPTION_DEFAULT_MFG) == EFI_IFR_OPTION_DEFAULT_MFG) {
1356 //
1357 // Set default ID to Manufacture ID and Get DefaultName String ID
1358 //
1359 VarDefaultId = EFI_HII_DEFAULT_CLASS_MANUFACTURING;
1360 Status = FindDefaultName (DefaultIdArray, VarDefaultId, &VarDefaultName);
1361 if (EFI_ERROR (Status)) {
1362 goto Done;
1363 }
1364 //
1365 // Prepare new DefaultValue
1366 //
1367 DefaultData = (IFR_DEFAULT_DATA *) AllocateZeroPool (sizeof (IFR_DEFAULT_DATA));
1368 if (DefaultData == NULL) {
1369 Status = EFI_OUT_OF_RESOURCES;
1370 goto Done;
1371 }
1372 DefaultData->DefaultId = VarDefaultId;
1373 DefaultData->DefaultName = VarDefaultName;
1374 DefaultData->Value = IfrOneOfOption->Value.u64;
1375 //
1376 // Add DefaultValue into current BlockData
1377 //
1378 InsertDefaultValue (BlockData, DefaultData);
1379 }
1380 break;
1381
1382 case EFI_IFR_DEFAULT_OP:
1383 //
1384 // Update Current BlockData to the default value.
1385 //
1386 if (BlockData == NULL || BlockData->Scope == 0) {
1387 //
1388 // No matched block data is ignored.
1389 //
1390 break;
1391 }
1392
1393 if (BlockData->OpCode == EFI_IFR_ORDERED_LIST_OP) {
1394 //
1395 // OrderedList Opcode is no default value.
1396 //
1397 break;
1398 }
1399 //
1400 // Get the DefaultId and DefaultName String ID
1401 //
1402 IfrDefault = (EFI_IFR_DEFAULT *) IfrOpHdr;
1403 VarDefaultId = IfrDefault->DefaultId;
1404 Status = FindDefaultName (DefaultIdArray, VarDefaultId, &VarDefaultName);
1405 if (EFI_ERROR (Status)) {
1406 goto Done;
1407 }
1408 //
1409 // Prepare new DefaultValue
1410 //
1411 DefaultData = (IFR_DEFAULT_DATA *) AllocateZeroPool (sizeof (IFR_DEFAULT_DATA));
1412 if (DefaultData == NULL) {
1413 Status = EFI_OUT_OF_RESOURCES;
1414 goto Done;
1415 }
1416 DefaultData->DefaultId = VarDefaultId;
1417 DefaultData->DefaultName = VarDefaultName;
1418 DefaultData->Value = IfrDefault->Value.u64;
1419 //
1420 // Add DefaultValue into current BlockData
1421 //
1422 InsertDefaultValue (BlockData, DefaultData);
1423 break;
1424 case EFI_IFR_END_OP:
1425 //
1426 // End Opcode is for Var.
1427 //
1428 if (BlockData != NULL && BlockData->Scope > 0) {
1429 BlockData->Scope--;
1430 }
1431 break;
1432 default:
1433 if (BlockData != NULL && BlockData->Scope > 0) {
1434 BlockData->Scope = (UINT8) (BlockData->Scope + IfrOpHdr->Scope);
1435 }
1436 break;
1437 }
1438
1439 IfrOffset += IfrOpHdr->Length;
1440 }
1441
1442 Done:
1443 //
1444 // Set the defualt ID array.
1445 //
1446 *PIfrDefaultIdArray = DefaultIdArray;
1447
1448 return Status;
1449 }
1450
1451 /**
1452 This function gets the full request string and full default value string by
1453 parsing IFR data in HII form packages.
1454
1455 When Request points to NULL string, the request string and default value string
1456 for each varstore in form package will return.
1457
1458 @param HiiHandle Hii Handle which Hii Packages are registered.
1459 @param DevicePath Device Path which Hii Config Access Protocol is registered.
1460 @param Request Pointer to a null-terminated Unicode string in
1461 <ConfigRequest> format. When it doesn't contain
1462 any RequestElement, it will be updated to return
1463 the full RequestElement retrieved from IFR data.
1464 If it points to NULL, the request string for the first
1465 varstore in form package will be merged into a
1466 <MultiConfigRequest> format string and return.
1467 @param AltCfgResp Pointer to a null-terminated Unicode string in
1468 <ConfigAltResp> format. When the pointer is to NULL,
1469 the full default value string retrieved from IFR data
1470 will return. When the pinter is to a string, the
1471 full default value string retrieved from IFR data
1472 will be merged into the input string and return.
1473 When Request points to NULL, the default value string
1474 for each varstore in form package will be merged into
1475 a <MultiConfigAltResp> format string and return.
1476 @retval EFI_SUCCESS The Results string is set to the full request string.
1477 And AltCfgResp contains all default value string.
1478 @retval EFI_OUT_OF_RESOURCES Not enough memory for the return string.
1479 @retval EFI_NOT_FOUND The varstore (Guid and Name) in Request string
1480 can't be found in Form package.
1481 @retval EFI_NOT_FOUND HiiPackage can't be got on the input HiiHandle.
1482 @retval EFI_INVALID_PARAMETER *Request points to NULL.
1483
1484 **/
1485 EFI_STATUS
1486 EFIAPI
1487 GetFullStringFromHiiFormPackages (
1488 IN EFI_HII_HANDLE HiiHandle,
1489 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
1490 IN OUT EFI_STRING *Request,
1491 IN OUT EFI_STRING *AltCfgResp
1492 )
1493 {
1494 EFI_STATUS Status;
1495 EFI_HII_PACKAGE_LIST_HEADER *HiiPackageList;
1496 UINT32 PackageListLength;
1497 UINTN BufferSize;
1498 IFR_BLOCK_DATA *RequestBlockArray;
1499 IFR_BLOCK_DATA *BlockData;
1500 IFR_BLOCK_DATA *NextBlockData;
1501 IFR_DEFAULT_DATA *DefaultValueData;
1502 IFR_DEFAULT_DATA *DefaultId;
1503 IFR_DEFAULT_DATA *DefaultIdArray;
1504 EFI_HII_PACKAGE_HEADER PacakgeHeader;
1505 UINT32 PackageOffset;
1506 IFR_VARSTORAGE_DATA *VarStorageData;
1507 EFI_STRING DefaultAltCfgResp;
1508 EFI_STRING FullConfigRequest;
1509 EFI_STRING ConfigHdr;
1510 EFI_STRING GuidStr;
1511 EFI_STRING NameStr;
1512 EFI_STRING PathStr;
1513 EFI_STRING StringPtr;
1514 UINTN Length;
1515 UINT8 *TmpBuffer;
1516 UINT16 Offset;
1517 UINT16 Width;
1518 LIST_ENTRY *Link;
1519 LIST_ENTRY *LinkData;
1520 LIST_ENTRY *LinkDefault;
1521
1522 //
1523 // Initialize the local variables.
1524 //
1525 RequestBlockArray = NULL;
1526 VarStorageData = NULL;
1527 DefaultAltCfgResp = NULL;
1528 FullConfigRequest = NULL;
1529 ConfigHdr = NULL;
1530 DefaultIdArray = NULL;
1531 GuidStr = NULL;
1532 NameStr = NULL;
1533 PathStr = NULL;
1534
1535 //
1536 // 1. Get HiiPackage by HiiHandle
1537 //
1538 BufferSize = 0;
1539 HiiPackageList = NULL;
1540 Status = HiiExportPackageLists (&mPrivate.HiiDatabase, HiiHandle, &BufferSize, HiiPackageList);
1541
1542 //
1543 // The return status should always be EFI_BUFFER_TOO_SMALL as input buffer's size is 0.
1544 //
1545 if (Status != EFI_BUFFER_TOO_SMALL) {
1546 return Status;
1547 }
1548
1549 HiiPackageList = AllocatePool (BufferSize);
1550 if (HiiPackageList == NULL) {
1551 return EFI_OUT_OF_RESOURCES;
1552 }
1553
1554 //
1555 // Get PackageList on HiiHandle
1556 //
1557 Status = HiiExportPackageLists (&mPrivate.HiiDatabase, HiiHandle, &BufferSize, HiiPackageList);
1558 if (EFI_ERROR (Status)) {
1559 goto Done;
1560 }
1561
1562 //
1563 // 2. Parse FormPackage to get BlockArray and DefaultId Array for the request BlockArray.
1564 // 1) Request is NULL.
1565 // 2) Request is not NULL. And it doesn't contain any BlockArray.
1566 // 3) Request is not NULL. And it containts BlockArray.
1567 //
1568
1569 //
1570 // Initialize VarStorageData to store the var store Block and Default value information.
1571 //
1572 VarStorageData = (IFR_VARSTORAGE_DATA *) AllocateZeroPool (sizeof (IFR_VARSTORAGE_DATA));
1573 if (VarStorageData == NULL) {
1574 Status = EFI_OUT_OF_RESOURCES;
1575 goto Done;
1576 }
1577
1578 InitializeListHead (&VarStorageData->Entry);
1579 InitializeListHead (&VarStorageData->BlockEntry);
1580
1581 //
1582 // Gte the request block array by Request String
1583 //
1584 StringPtr = NULL;
1585 if (*Request != NULL) {
1586 StringPtr = StrStr (*Request, L"&OFFSET=");
1587 }
1588 if (StringPtr != NULL) {
1589 //
1590 // Init RequestBlockArray
1591 //
1592 RequestBlockArray = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
1593 if (RequestBlockArray == NULL) {
1594 Status = EFI_OUT_OF_RESOURCES;
1595 goto Done;
1596 }
1597 InitializeListHead (&RequestBlockArray->Entry);
1598
1599 //
1600 // Get the request Block array from the request string
1601 // Offset and Width
1602 //
1603
1604 //
1605 // Parse each <RequestElement> if exists
1606 // Only <BlockName> format is supported by this help function.
1607 // <BlockName> ::= &'OFFSET='<Number>&'WIDTH='<Number>
1608 //
1609 while (*StringPtr != 0 && StrnCmp (StringPtr, L"&OFFSET=", StrLen (L"&OFFSET=")) == 0) {
1610 //
1611 // Skip the OFFSET string
1612 //
1613 StringPtr += StrLen (L"&OFFSET=");
1614 //
1615 // Get Offset
1616 //
1617 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
1618 if (EFI_ERROR (Status)) {
1619 goto Done;
1620 }
1621 Offset = 0;
1622 CopyMem (
1623 &Offset,
1624 TmpBuffer,
1625 (((Length + 1) / 2) < sizeof (UINT16)) ? ((Length + 1) / 2) : sizeof (UINT16)
1626 );
1627 FreePool (TmpBuffer);
1628
1629 StringPtr += Length;
1630 if (StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
1631 Status = EFI_INVALID_PARAMETER;
1632 goto Done;
1633 }
1634 StringPtr += StrLen (L"&WIDTH=");
1635
1636 //
1637 // Get Width
1638 //
1639 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
1640 if (EFI_ERROR (Status)) {
1641 goto Done;
1642 }
1643 Width = 0;
1644 CopyMem (
1645 &Width,
1646 TmpBuffer,
1647 (((Length + 1) / 2) < sizeof (UINT16)) ? ((Length + 1) / 2) : sizeof (UINT16)
1648 );
1649 FreePool (TmpBuffer);
1650
1651 StringPtr += Length;
1652 if (*StringPtr != 0 && *StringPtr != L'&') {
1653 Status = EFI_INVALID_PARAMETER;
1654 goto Done;
1655 }
1656
1657 //
1658 // Set Block Data
1659 //
1660 BlockData = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
1661 if (BlockData == NULL) {
1662 Status = EFI_OUT_OF_RESOURCES;
1663 goto Done;
1664 }
1665 BlockData->Offset = Offset;
1666 BlockData->Width = Width;
1667 InsertBlockData (&RequestBlockArray->Entry, &BlockData);
1668
1669 //
1670 // If '\0', parsing is finished.
1671 //
1672 if (*StringPtr == 0) {
1673 break;
1674 }
1675 }
1676
1677 //
1678 // Merge the requested block data.
1679 //
1680 Link = RequestBlockArray->Entry.ForwardLink;
1681 while ((Link != &RequestBlockArray->Entry) && (Link->ForwardLink != &RequestBlockArray->Entry)) {
1682 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
1683 NextBlockData = BASE_CR (Link->ForwardLink, IFR_BLOCK_DATA, Entry);
1684 if ((NextBlockData->Offset >= BlockData->Offset) && (NextBlockData->Offset <= (BlockData->Offset + BlockData->Width))) {
1685 if ((NextBlockData->Offset + NextBlockData->Width) > (BlockData->Offset + BlockData->Width)) {
1686 BlockData->Width = (UINT16) (NextBlockData->Offset + NextBlockData->Width - BlockData->Offset);
1687 }
1688 RemoveEntryList (Link->ForwardLink);
1689 FreePool (NextBlockData);
1690 continue;
1691 }
1692 Link = Link->ForwardLink;
1693 }
1694 }
1695
1696 //
1697 // Get the form package
1698 //
1699 PackageOffset = sizeof (EFI_HII_PACKAGE_LIST_HEADER);
1700 PackageListLength = ReadUnaligned32 (&HiiPackageList->PackageLength);
1701 while (PackageOffset < PackageListLength) {
1702 CopyMem (&PacakgeHeader, (UINT8 *) HiiPackageList + PackageOffset, sizeof (PacakgeHeader));
1703
1704 if (PacakgeHeader.Type == EFI_HII_PACKAGE_FORMS) {
1705 //
1706 // Reset VarStorageData
1707 //
1708 VarStorageData->Size = 0;
1709 VarStorageData->VarStoreId = 0;
1710 if (VarStorageData->Name != NULL) {
1711 FreePool (VarStorageData->Name);
1712 VarStorageData->Name = NULL;
1713 }
1714
1715 //
1716 // Parse the opcode in form package
1717 //
1718 Status = ParseIfrData ((UINT8 *) HiiPackageList + PackageOffset, PacakgeHeader.Length, *Request, RequestBlockArray, VarStorageData, &DefaultIdArray);
1719 if (EFI_ERROR (Status)) {
1720 goto Done;
1721 }
1722
1723 //
1724 // Only one form is in a pacakge list.
1725 //
1726 break;
1727 }
1728
1729 PackageOffset += PacakgeHeader.Length;
1730 }
1731
1732 //
1733 // No requested varstore in IFR data and directly return
1734 //
1735 if (VarStorageData->Size == 0) {
1736 goto Done;
1737 }
1738
1739 //
1740 // 3. Construct Request Element (Block Name) for 2.1 and 2.2 case.
1741 //
1742
1743 //
1744 // Construct <ConfigHdr> : "GUID=...&NAME=...&PATH=..." by VarStorageData Guid, Name and DriverHandle
1745 //
1746 GenerateSubStr (L"GUID=", sizeof (EFI_GUID), (VOID *) &VarStorageData->Guid, 1, &GuidStr);
1747 GenerateSubStr (L"NAME=", StrLen (VarStorageData->Name) * sizeof (CHAR16), (VOID *) VarStorageData->Name, 2, &NameStr);
1748 GenerateSubStr (
1749 L"PATH=",
1750 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) DevicePath),
1751 (VOID *) DevicePath,
1752 1,
1753 &PathStr
1754 );
1755 Length = StrLen (GuidStr);
1756 Length = Length + StrLen (NameStr);
1757 Length = Length + StrLen (PathStr) + 1;
1758 ConfigHdr = AllocateZeroPool (Length * sizeof (CHAR16));
1759 if (ConfigHdr == NULL) {
1760 Status = EFI_OUT_OF_RESOURCES;
1761 goto Done;
1762 }
1763 StrCpy (ConfigHdr, GuidStr);
1764 StrCat (ConfigHdr, NameStr);
1765 StrCat (ConfigHdr, PathStr);
1766
1767 //
1768 // Remove the last character L'&'
1769 //
1770 *(ConfigHdr + StrLen (ConfigHdr) - 1) = L'\0';
1771
1772 if (RequestBlockArray == NULL) {
1773 //
1774 // Append VarStorageData BlockEntry into *Request string
1775 // Now support only one varstore in a form package.
1776 //
1777
1778 //
1779 // Go through all VarStorageData Entry and get BlockEntry for each one for the multiple varstore in a single form package
1780 // Then construct them all to return MultiRequest string : ConfigHdr BlockConfig
1781 //
1782
1783 //
1784 // Compute the length of the entire request starting with <ConfigHdr> and a
1785 // Null-terminator
1786 //
1787 Length = StrLen (ConfigHdr) + 1;
1788
1789 for (Link = VarStorageData->BlockEntry.ForwardLink; Link != &VarStorageData->BlockEntry; Link = Link->ForwardLink) {
1790 //
1791 // Add <BlockName> length for each Offset/Width pair
1792 //
1793 // <BlockName> ::= &OFFSET=1234&WIDTH=1234
1794 // | 8 | 4 | 7 | 4 |
1795 //
1796 Length = Length + (8 + 4 + 7 + 4);
1797 }
1798
1799 //
1800 // Allocate buffer for the entire <ConfigRequest>
1801 //
1802 FullConfigRequest = AllocateZeroPool (Length * sizeof (CHAR16));
1803 if (FullConfigRequest == NULL) {
1804 goto Done;
1805 }
1806 StringPtr = FullConfigRequest;
1807
1808 //
1809 // Start with <ConfigHdr>
1810 //
1811 StrCpy (StringPtr, ConfigHdr);
1812 StringPtr += StrLen (StringPtr);
1813
1814 //
1815 // Loop through all the Offset/Width pairs and append them to ConfigRequest
1816 //
1817 for (Link = VarStorageData->BlockEntry.ForwardLink; Link != &VarStorageData->BlockEntry; Link = Link->ForwardLink) {
1818 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
1819 //
1820 // Append &OFFSET=XXXX&WIDTH=YYYY\0
1821 //
1822 UnicodeSPrint (
1823 StringPtr,
1824 (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16),
1825 L"&OFFSET=%04X&WIDTH=%04X",
1826 BlockData->Offset,
1827 BlockData->Width
1828 );
1829 StringPtr += StrLen (StringPtr);
1830 }
1831 //
1832 // Set to the got full request string.
1833 //
1834 HiiToLower (FullConfigRequest);
1835 if (*Request != NULL) {
1836 FreePool (*Request);
1837 }
1838 *Request = FullConfigRequest;
1839 }
1840
1841 //
1842 // 4. Construct Default Value string in AltResp according to request element.
1843 // Go through all VarStorageData Entry and get the DefaultId array for each one
1844 // Then construct them all to : ConfigHdr AltConfigHdr ConfigBody AltConfigHdr ConfigBody
1845 //
1846
1847 //
1848 // Add length for <ConfigHdr> + '\0'
1849 //
1850 Length = StrLen (ConfigHdr) + 1;
1851
1852 for (Link = DefaultIdArray->Entry.ForwardLink; Link != &DefaultIdArray->Entry; Link = Link->ForwardLink) {
1853 DefaultId = BASE_CR (Link, IFR_DEFAULT_DATA, Entry);
1854 //
1855 // Add length for "&<ConfigHdr>&ALTCFG=XXXX"
1856 // |1| StrLen (ConfigHdr) | 8 | 4 |
1857 //
1858 Length += (1 + StrLen (ConfigHdr) + 8 + 4);
1859
1860 for (LinkData = VarStorageData->BlockEntry.ForwardLink; LinkData != &VarStorageData->BlockEntry; LinkData = LinkData->ForwardLink) {
1861 BlockData = BASE_CR (LinkData, IFR_BLOCK_DATA, Entry);
1862 for (LinkDefault = BlockData->DefaultValueEntry.ForwardLink; LinkDefault != &BlockData->DefaultValueEntry; LinkDefault = LinkDefault->ForwardLink) {
1863 DefaultValueData = BASE_CR (LinkDefault, IFR_DEFAULT_DATA, Entry);
1864 if (DefaultValueData->DefaultId == DefaultId->DefaultId) {
1865 //
1866 // Add length for "&OFFSET=XXXX&WIDTH=YYYY&VALUE=zzzzzzzzzzzz"
1867 // | 8 | 4 | 7 | 4 | 7 | Width * 2 |
1868 //
1869 Length += (8 + 4 + 7 + 4 + 7 + BlockData->Width * 2);
1870 }
1871 }
1872 }
1873 }
1874
1875 //
1876 // Allocate buffer for the entire <DefaultAltCfgResp>
1877 //
1878 DefaultAltCfgResp = AllocateZeroPool (Length * sizeof (CHAR16));
1879 if (DefaultAltCfgResp == NULL) {
1880 goto Done;
1881 }
1882 StringPtr = DefaultAltCfgResp;
1883
1884 //
1885 // Start with <ConfigHdr>
1886 //
1887 StrCpy (StringPtr, ConfigHdr);
1888 StringPtr += StrLen (StringPtr);
1889
1890 for (Link = DefaultIdArray->Entry.ForwardLink; Link != &DefaultIdArray->Entry; Link = Link->ForwardLink) {
1891 DefaultId = BASE_CR (Link, IFR_DEFAULT_DATA, Entry);
1892 //
1893 // Add <AltConfigHdr> of the form "&<ConfigHdr>&ALTCFG=XXXX\0"
1894 // |1| StrLen (ConfigHdr) | 8 | 4 |
1895 //
1896 UnicodeSPrint (
1897 StringPtr,
1898 (1 + StrLen (ConfigHdr) + 8 + 4 + 1) * sizeof (CHAR16),
1899 L"&%s&ALTCFG=%04X",
1900 ConfigHdr,
1901 DefaultId->DefaultName
1902 );
1903 StringPtr += StrLen (StringPtr);
1904
1905 for (LinkData = VarStorageData->BlockEntry.ForwardLink; LinkData != &VarStorageData->BlockEntry; LinkData = LinkData->ForwardLink) {
1906 BlockData = BASE_CR (LinkData, IFR_BLOCK_DATA, Entry);
1907 for (LinkDefault = BlockData->DefaultValueEntry.ForwardLink; LinkDefault != &BlockData->DefaultValueEntry; LinkDefault = LinkDefault->ForwardLink) {
1908 DefaultValueData = BASE_CR (LinkDefault, IFR_DEFAULT_DATA, Entry);
1909 if (DefaultValueData->DefaultId == DefaultId->DefaultId) {
1910 //
1911 // Add <BlockConfig>
1912 // <BlockConfig> ::= 'OFFSET='<Number>&'WIDTH='<Number>&'VALUE'=<Number>
1913 //
1914 UnicodeSPrint (
1915 StringPtr,
1916 (8 + 4 + 7 + 4 + 7 + 1) * sizeof (CHAR16),
1917 L"&OFFSET=%04X&WIDTH=%04X&VALUE=",
1918 BlockData->Offset,
1919 BlockData->Width
1920 );
1921 StringPtr += StrLen (StringPtr);
1922
1923 //
1924 // Convert Value to a hex string in "%x" format
1925 // NOTE: This is in the opposite byte that GUID and PATH use
1926 //
1927 Width = BlockData->Width;
1928 TmpBuffer = (UINT8 *) &(DefaultValueData->Value);
1929 for (; Width > 0; Width--) {
1930 StringPtr += UnicodeValueToString (StringPtr, PREFIX_ZERO | RADIX_HEX, TmpBuffer[Width - 1], 2);
1931 }
1932 }
1933 }
1934 }
1935 }
1936 HiiToLower (DefaultAltCfgResp);
1937
1938 //
1939 // 5. Merge string into the input AltCfgResp if the iput *AltCfgResp is not NULL.
1940 //
1941 if (*AltCfgResp != NULL) {
1942 Status = MergeDefaultString (AltCfgResp, DefaultAltCfgResp);
1943 FreePool (DefaultAltCfgResp);
1944 } else {
1945 *AltCfgResp = DefaultAltCfgResp;
1946 }
1947
1948 Done:
1949 if (RequestBlockArray != NULL) {
1950 //
1951 // Free Link Array RequestBlockArray
1952 //
1953 while (!IsListEmpty (&RequestBlockArray->Entry)) {
1954 BlockData = BASE_CR (RequestBlockArray->Entry.ForwardLink, IFR_BLOCK_DATA, Entry);
1955 RemoveEntryList (&BlockData->Entry);
1956 FreePool (BlockData);
1957 }
1958
1959 FreePool (RequestBlockArray);
1960 }
1961
1962 if (VarStorageData != NULL) {
1963 //
1964 // Free link array VarStorageData
1965 //
1966 while (!IsListEmpty (&VarStorageData->BlockEntry)) {
1967 BlockData = BASE_CR (VarStorageData->BlockEntry.ForwardLink, IFR_BLOCK_DATA, Entry);
1968 RemoveEntryList (&BlockData->Entry);
1969 //
1970 // Free default value link array
1971 //
1972 while (!IsListEmpty (&BlockData->DefaultValueEntry)) {
1973 DefaultValueData = BASE_CR (BlockData->DefaultValueEntry.ForwardLink, IFR_DEFAULT_DATA, Entry);
1974 RemoveEntryList (&DefaultValueData->Entry);
1975 FreePool (DefaultValueData);
1976 }
1977 FreePool (BlockData);
1978 }
1979 FreePool (VarStorageData);
1980 }
1981
1982 if (DefaultIdArray != NULL) {
1983 //
1984 // Free DefaultId Array
1985 //
1986 while (!IsListEmpty (&DefaultIdArray->Entry)) {
1987 DefaultId = BASE_CR (DefaultIdArray->Entry.ForwardLink, IFR_DEFAULT_DATA, Entry);
1988 RemoveEntryList (&DefaultId->Entry);
1989 FreePool (DefaultId);
1990 }
1991 FreePool (DefaultIdArray);
1992 }
1993
1994 //
1995 // Free the allocated string
1996 //
1997 if (GuidStr != NULL) {
1998 FreePool (GuidStr);
1999 }
2000 if (NameStr != NULL) {
2001 FreePool (NameStr);
2002 }
2003 if (PathStr != NULL) {
2004 FreePool (PathStr);
2005 }
2006 if (ConfigHdr != NULL) {
2007 FreePool (ConfigHdr);
2008 }
2009
2010 //
2011 // Free Pacakge data
2012 //
2013 if (HiiPackageList != NULL) {
2014 FreePool (HiiPackageList);
2015 }
2016
2017 return Status;
2018 }
2019
2020 /**
2021 This function allows a caller to extract the current configuration
2022 for one or more named elements from one or more drivers.
2023
2024 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
2025 instance.
2026 @param Request A null-terminated Unicode string in
2027 <MultiConfigRequest> format.
2028 @param Progress On return, points to a character in the Request
2029 string. Points to the string's null terminator if
2030 request was successful. Points to the most recent
2031 & before the first failing name / value pair (or
2032 the beginning of the string if the failure is in
2033 the first name / value pair) if the request was
2034 not successful.
2035 @param Results Null-terminated Unicode string in
2036 <MultiConfigAltResp> format which has all values
2037 filled in for the names in the Request string.
2038 String to be allocated by the called function.
2039
2040 @retval EFI_SUCCESS The Results string is filled with the values
2041 corresponding to all requested names.
2042 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the parts of the
2043 results that must be stored awaiting possible
2044 future protocols.
2045 @retval EFI_NOT_FOUND Routing data doesn't match any known driver.
2046 Progress set to the "G" in "GUID" of the routing
2047 header that doesn't match. Note: There is no
2048 requirement that all routing data be validated
2049 before any configuration extraction.
2050 @retval EFI_INVALID_PARAMETER For example, passing in a NULL for the Request
2051 parameter would result in this type of error. The
2052 Progress parameter is set to NULL.
2053 @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set to most recent &
2054 before the error or the beginning of the string.
2055 @retval EFI_INVALID_PARAMETER Unknown name. Progress points to the & before the
2056 name in question.
2057
2058 **/
2059 EFI_STATUS
2060 EFIAPI
2061 HiiConfigRoutingExtractConfig (
2062 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
2063 IN CONST EFI_STRING Request,
2064 OUT EFI_STRING *Progress,
2065 OUT EFI_STRING *Results
2066 )
2067 {
2068 HII_DATABASE_PRIVATE_DATA *Private;
2069 EFI_STRING StringPtr;
2070 EFI_STRING ConfigRequest;
2071 UINTN Length;
2072 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
2073 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
2074 EFI_STATUS Status;
2075 LIST_ENTRY *Link;
2076 HII_DATABASE_RECORD *Database;
2077 UINT8 *DevicePathPkg;
2078 UINT8 *CurrentDevicePath;
2079 EFI_HANDLE DriverHandle;
2080 EFI_HII_HANDLE HiiHandle;
2081 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
2082 EFI_STRING AccessProgress;
2083 EFI_STRING AccessResults;
2084 EFI_STRING DefaultResults;
2085 BOOLEAN FirstElement;
2086 UINTN DevicePathLength;
2087
2088 if (This == NULL || Progress == NULL || Results == NULL) {
2089 return EFI_INVALID_PARAMETER;
2090 }
2091
2092 if (Request == NULL) {
2093 *Progress = NULL;
2094 return EFI_INVALID_PARAMETER;
2095 }
2096
2097 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
2098 StringPtr = Request;
2099 *Progress = StringPtr;
2100 DefaultResults = NULL;
2101 ConfigRequest = NULL;
2102 Status = EFI_SUCCESS;
2103 AccessResults = NULL;
2104 DevicePath = NULL;
2105
2106 //
2107 // The first element of <MultiConfigRequest> should be
2108 // <GuidHdr>, which is in 'GUID='<Guid> syntax.
2109 //
2110 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
2111 return EFI_INVALID_PARAMETER;
2112 }
2113
2114 FirstElement = TRUE;
2115
2116 //
2117 // Allocate a fix length of memory to store Results. Reallocate memory for
2118 // Results if this fix length is insufficient.
2119 //
2120 *Results = (EFI_STRING) AllocateZeroPool (MAX_STRING_LENGTH);
2121 if (*Results == NULL) {
2122 return EFI_OUT_OF_RESOURCES;
2123 }
2124
2125 while (*StringPtr != 0 && StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) == 0) {
2126 //
2127 // If parsing error, set Progress to the beginning of the <MultiConfigRequest>
2128 // or most recent & before the error.
2129 //
2130 if (StringPtr == Request) {
2131 *Progress = StringPtr;
2132 } else {
2133 *Progress = StringPtr - 1;
2134 }
2135
2136 //
2137 // Process each <ConfigRequest> of <MultiConfigRequest>
2138 //
2139 Length = CalculateConfigStringLen (StringPtr);
2140 ConfigRequest = AllocateCopyPool ((Length + 1) * sizeof (CHAR16), StringPtr);
2141 if (ConfigRequest == NULL) {
2142 Status = EFI_OUT_OF_RESOURCES;
2143 goto Done;
2144 }
2145 *(ConfigRequest + Length) = 0;
2146
2147 //
2148 // Get the UEFI device path
2149 //
2150 Status = GetDevicePath (ConfigRequest, (UINT8 **) &DevicePath);
2151 if (EFI_ERROR (Status)) {
2152 goto Done;
2153 }
2154
2155 //
2156 // Find driver which matches the routing data.
2157 //
2158 DriverHandle = NULL;
2159 HiiHandle = NULL;
2160 DevicePathLength = GetDevicePathSize (DevicePath);
2161 for (Link = Private->DatabaseList.ForwardLink;
2162 Link != &Private->DatabaseList;
2163 Link = Link->ForwardLink
2164 ) {
2165 Database = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
2166
2167 if ((DevicePathPkg = Database->PackageList->DevicePathPkg) != NULL) {
2168 CurrentDevicePath = DevicePathPkg + sizeof (EFI_HII_PACKAGE_HEADER);
2169 if ((DevicePathLength == GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) CurrentDevicePath)) &&
2170 (CompareMem (
2171 DevicePath,
2172 CurrentDevicePath,
2173 DevicePathLength
2174 ) == 0)) {
2175 DriverHandle = Database->DriverHandle;
2176 HiiHandle = Database->Handle;
2177 break;
2178 }
2179 }
2180 }
2181
2182 //
2183 // Try to find driver handle by device path.
2184 //
2185 if (DriverHandle == NULL) {
2186 TempDevicePath = DevicePath;
2187 Status = gBS->LocateDevicePath (
2188 &gEfiDevicePathProtocolGuid,
2189 &TempDevicePath,
2190 &DriverHandle
2191 );
2192 if (EFI_ERROR (Status) || (DriverHandle == NULL)) {
2193 //
2194 // Routing data does not match any known driver.
2195 // Set Progress to the 'G' in "GUID" of the routing header.
2196 //
2197 *Progress = StringPtr;
2198 Status = EFI_NOT_FOUND;
2199 goto Done;
2200 }
2201 }
2202
2203 //
2204 // Check whether ConfigRequest contains request string OFFSET/WIDTH
2205 //
2206 if ((HiiHandle != NULL) && (StrStr (ConfigRequest, L"&OFFSET=") == NULL)) {
2207 //
2208 // Get the full request string from IFR when HiiPackage is registered to HiiHandle
2209 //
2210 Status = GetFullStringFromHiiFormPackages (HiiHandle, DevicePath, &ConfigRequest, &DefaultResults);
2211 if (EFI_ERROR (Status)) {
2212 goto Done;
2213 }
2214 //
2215 // Not any request block is found.
2216 //
2217 if (StrStr (ConfigRequest, L"&OFFSET=") == NULL) {
2218 AccessResults = AllocateCopyPool (StrSize (ConfigRequest), ConfigRequest);
2219 goto NextConfigString;
2220 }
2221 }
2222
2223 //
2224 // Call corresponding ConfigAccess protocol to extract settings
2225 //
2226 Status = gBS->HandleProtocol (
2227 DriverHandle,
2228 &gEfiHiiConfigAccessProtocolGuid,
2229 (VOID **) &ConfigAccess
2230 );
2231 ASSERT_EFI_ERROR (Status);
2232
2233 Status = ConfigAccess->ExtractConfig (
2234 ConfigAccess,
2235 ConfigRequest,
2236 &AccessProgress,
2237 &AccessResults
2238 );
2239 if (EFI_ERROR (Status)) {
2240 //
2241 // AccessProgress indicates the parsing progress on <ConfigRequest>.
2242 // Map it to the progress on <MultiConfigRequest> then return it.
2243 //
2244 *Progress = StrStr (StringPtr, AccessProgress);
2245 goto Done;
2246 }
2247
2248 //
2249 // Attach this <ConfigAltResp> to a <MultiConfigAltResp>. There is a '&'
2250 // which seperates the first <ConfigAltResp> and the following ones.
2251 //
2252 ASSERT (*AccessProgress == 0);
2253
2254 //
2255 // Update AccessResults by getting default setting from IFR when HiiPackage is registered to HiiHandle
2256 //
2257 if (HiiHandle != NULL) {
2258 if (DefaultResults == NULL) {
2259 Status = GetFullStringFromHiiFormPackages (HiiHandle, DevicePath, &ConfigRequest, &AccessResults);
2260 } else {
2261 Status = MergeDefaultString (&AccessResults, DefaultResults);
2262 }
2263 }
2264 FreePool (DevicePath);
2265 DevicePath = NULL;
2266
2267 if (EFI_ERROR (Status)) {
2268 goto Done;
2269 }
2270
2271 //
2272 // Free the allocated memory.
2273 //
2274 if (DefaultResults != NULL) {
2275 FreePool (DefaultResults);
2276 DefaultResults = NULL;
2277 }
2278
2279 NextConfigString:
2280 if (!FirstElement) {
2281 Status = AppendToMultiString (Results, L"&");
2282 ASSERT_EFI_ERROR (Status);
2283 }
2284
2285 Status = AppendToMultiString (Results, AccessResults);
2286 ASSERT_EFI_ERROR (Status);
2287
2288 FirstElement = FALSE;
2289
2290 FreePool (AccessResults);
2291 AccessResults = NULL;
2292 FreePool (ConfigRequest);
2293 ConfigRequest = NULL;
2294
2295 //
2296 // Go to next <ConfigRequest> (skip '&').
2297 //
2298 StringPtr += Length;
2299 if (*StringPtr == 0) {
2300 *Progress = StringPtr;
2301 break;
2302 }
2303
2304 StringPtr++;
2305 }
2306
2307 Done:
2308 if (EFI_ERROR (Status)) {
2309 FreePool (*Results);
2310 *Results = NULL;
2311 }
2312
2313 if (ConfigRequest != NULL) {
2314 FreePool (ConfigRequest);
2315 }
2316
2317 if (AccessResults != NULL) {
2318 FreePool (AccessResults);
2319 }
2320
2321 if (DefaultResults != NULL) {
2322 FreePool (DefaultResults);
2323 }
2324
2325 if (DevicePath != NULL) {
2326 FreePool (DevicePath);
2327 }
2328
2329 return Status;
2330 }
2331
2332
2333 /**
2334 This function allows the caller to request the current configuration for the
2335 entirety of the current HII database and returns the data in a
2336 null-terminated Unicode string.
2337
2338 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
2339 instance.
2340 @param Results Null-terminated Unicode string in
2341 <MultiConfigAltResp> format which has all values
2342 filled in for the names in the Request string.
2343 String to be allocated by the called function.
2344 De-allocation is up to the caller.
2345
2346 @retval EFI_SUCCESS The Results string is filled with the values
2347 corresponding to all requested names.
2348 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the parts of the
2349 results that must be stored awaiting possible
2350 future protocols.
2351 @retval EFI_INVALID_PARAMETER For example, passing in a NULL for the Results
2352 parameter would result in this type of error.
2353
2354 **/
2355 EFI_STATUS
2356 EFIAPI
2357 HiiConfigRoutingExportConfig (
2358 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
2359 OUT EFI_STRING *Results
2360 )
2361 {
2362 EFI_STATUS Status;
2363 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
2364 EFI_STRING AccessResults;
2365 EFI_STRING Progress;
2366 EFI_STRING ConfigRequest;
2367 UINTN Index;
2368 EFI_HANDLE *ConfigAccessHandles;
2369 UINTN NumberConfigAccessHandles;
2370 BOOLEAN FirstElement;
2371 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
2372 EFI_HII_HANDLE HiiHandle;
2373 EFI_STRING DefaultResults;
2374 HII_DATABASE_PRIVATE_DATA *Private;
2375 LIST_ENTRY *Link;
2376 HII_DATABASE_RECORD *Database;
2377 UINT8 *DevicePathPkg;
2378 UINT8 *CurrentDevicePath;
2379 UINTN DevicePathLength;
2380
2381 if (This == NULL || Results == NULL) {
2382 return EFI_INVALID_PARAMETER;
2383 }
2384
2385 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
2386
2387 //
2388 // Allocate a fix length of memory to store Results. Reallocate memory for
2389 // Results if this fix length is insufficient.
2390 //
2391 *Results = (EFI_STRING) AllocateZeroPool (MAX_STRING_LENGTH);
2392 if (*Results == NULL) {
2393 return EFI_OUT_OF_RESOURCES;
2394 }
2395
2396 NumberConfigAccessHandles = 0;
2397 Status = gBS->LocateHandleBuffer (
2398 ByProtocol,
2399 &gEfiHiiConfigAccessProtocolGuid,
2400 NULL,
2401 &NumberConfigAccessHandles,
2402 &ConfigAccessHandles
2403 );
2404 if (EFI_ERROR (Status)) {
2405 return Status;
2406 }
2407
2408 FirstElement = TRUE;
2409
2410 for (Index = 0; Index < NumberConfigAccessHandles; Index++) {
2411 Status = gBS->HandleProtocol (
2412 ConfigAccessHandles[Index],
2413 &gEfiHiiConfigAccessProtocolGuid,
2414 (VOID **) &ConfigAccess
2415 );
2416 if (EFI_ERROR (Status)) {
2417 continue;
2418 }
2419
2420 //
2421 // Get DevicePath and HiiHandle for this ConfigAccess driver handle
2422 //
2423 Progress = NULL;
2424 HiiHandle = NULL;
2425 ConfigRequest = NULL;
2426 DefaultResults = NULL;
2427 DevicePath = DevicePathFromHandle (ConfigAccessHandles[Index]);
2428 DevicePathLength = GetDevicePathSize (DevicePath);
2429 if (DevicePath != NULL) {
2430 for (Link = Private->DatabaseList.ForwardLink;
2431 Link != &Private->DatabaseList;
2432 Link = Link->ForwardLink
2433 ) {
2434 Database = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
2435 if ((DevicePathPkg = Database->PackageList->DevicePathPkg) != NULL) {
2436 CurrentDevicePath = DevicePathPkg + sizeof (EFI_HII_PACKAGE_HEADER);
2437 if ((DevicePathLength == GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) CurrentDevicePath)) &&
2438 (CompareMem (
2439 DevicePath,
2440 CurrentDevicePath,
2441 DevicePathLength
2442 ) == 0)) {
2443 HiiHandle = Database->Handle;
2444 break;
2445 }
2446 }
2447 }
2448 }
2449
2450 //
2451 // Update AccessResults by getting default setting from IFR when HiiPackage is registered to HiiHandle
2452 //
2453 if (HiiHandle != NULL && DevicePath != NULL) {
2454 Status = GetFullStringFromHiiFormPackages (HiiHandle, DevicePath, &ConfigRequest, &DefaultResults);
2455 }
2456 //
2457 // Can't parse IFR data to get the request string and default string.
2458 //
2459 if (EFI_ERROR (Status)) {
2460 ConfigRequest = NULL;
2461 DefaultResults = NULL;
2462 }
2463
2464 Status = ConfigAccess->ExtractConfig (
2465 ConfigAccess,
2466 ConfigRequest,
2467 &Progress,
2468 &AccessResults
2469 );
2470 if (!EFI_ERROR (Status)) {
2471 //
2472 // Merge the default sting from IFR code into the got setting from driver.
2473 //
2474 if (DefaultResults != NULL) {
2475 MergeDefaultString (&AccessResults, DefaultResults);
2476 FreePool (DefaultResults);
2477 }
2478
2479 //
2480 // Attach this <ConfigAltResp> to a <MultiConfigAltResp>. There is a '&'
2481 // which seperates the first <ConfigAltResp> and the following ones.
2482 //
2483 if (!FirstElement) {
2484 Status = AppendToMultiString (Results, L"&");
2485 ASSERT_EFI_ERROR (Status);
2486 }
2487
2488 Status = AppendToMultiString (Results, AccessResults);
2489 ASSERT_EFI_ERROR (Status);
2490
2491 FirstElement = FALSE;
2492
2493 FreePool (AccessResults);
2494 AccessResults = NULL;
2495 }
2496 }
2497 FreePool (ConfigAccessHandles);
2498
2499 return EFI_SUCCESS;
2500 }
2501
2502
2503 /**
2504 This function processes the results of processing forms and routes it to the
2505 appropriate handlers or storage.
2506
2507 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
2508 instance.
2509 @param Configuration A null-terminated Unicode string in
2510 <MulltiConfigResp> format.
2511 @param Progress A pointer to a string filled in with the offset of
2512 the most recent & before the first failing name /
2513 value pair (or the beginning of the string if the
2514 failure is in the first name / value pair) or the
2515 terminating NULL if all was successful.
2516
2517 @retval EFI_SUCCESS The results have been distributed or are awaiting
2518 distribution.
2519 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the parts of the
2520 results that must be stored awaiting possible
2521 future protocols.
2522 @retval EFI_INVALID_PARAMETER Passing in a NULL for the Configuration parameter
2523 would result in this type of error.
2524 @retval EFI_NOT_FOUND Target for the specified routing data was not
2525 found.
2526
2527 **/
2528 EFI_STATUS
2529 EFIAPI
2530 HiiConfigRoutingRouteConfig (
2531 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
2532 IN CONST EFI_STRING Configuration,
2533 OUT EFI_STRING *Progress
2534 )
2535 {
2536 HII_DATABASE_PRIVATE_DATA *Private;
2537 EFI_STRING StringPtr;
2538 EFI_STRING ConfigResp;
2539 UINTN Length;
2540 EFI_STATUS Status;
2541 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
2542 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
2543 LIST_ENTRY *Link;
2544 HII_DATABASE_RECORD *Database;
2545 UINT8 *DevicePathPkg;
2546 UINT8 *CurrentDevicePath;
2547 EFI_HANDLE DriverHandle;
2548 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
2549 EFI_STRING AccessProgress;
2550 UINTN DevicePathLength;
2551
2552 if (This == NULL || Progress == NULL) {
2553 return EFI_INVALID_PARAMETER;
2554 }
2555
2556 if (Configuration == NULL) {
2557 *Progress = NULL;
2558 return EFI_INVALID_PARAMETER;
2559 }
2560
2561 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
2562 StringPtr = Configuration;
2563 *Progress = StringPtr;
2564
2565 //
2566 // The first element of <MultiConfigResp> should be
2567 // <GuidHdr>, which is in 'GUID='<Guid> syntax.
2568 //
2569 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
2570 return EFI_INVALID_PARAMETER;
2571 }
2572
2573 while (*StringPtr != 0 && StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) == 0) {
2574 //
2575 // If parsing error, set Progress to the beginning of the <MultiConfigResp>
2576 // or most recent & before the error.
2577 //
2578 if (StringPtr == Configuration) {
2579 *Progress = StringPtr;
2580 } else {
2581 *Progress = StringPtr - 1;
2582 }
2583
2584 //
2585 // Process each <ConfigResp> of <MultiConfigResp>
2586 //
2587 Length = CalculateConfigStringLen (StringPtr);
2588 ConfigResp = AllocateCopyPool ((Length + 1) * sizeof (CHAR16), StringPtr);
2589 if (ConfigResp == NULL) {
2590 return EFI_OUT_OF_RESOURCES;
2591 }
2592 //
2593 // Append '\0' to the end of ConfigRequest
2594 //
2595 *(ConfigResp + Length) = 0;
2596
2597 //
2598 // Get the UEFI device path
2599 //
2600 Status = GetDevicePath (ConfigResp, (UINT8 **) &DevicePath);
2601 if (EFI_ERROR (Status)) {
2602 FreePool (ConfigResp);
2603 return Status;
2604 }
2605
2606 //
2607 // Find driver which matches the routing data.
2608 //
2609 DriverHandle = NULL;
2610 DevicePathLength = GetDevicePathSize (DevicePath);
2611 for (Link = Private->DatabaseList.ForwardLink;
2612 Link != &Private->DatabaseList;
2613 Link = Link->ForwardLink
2614 ) {
2615 Database = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
2616
2617 if ((DevicePathPkg = Database->PackageList->DevicePathPkg) != NULL) {
2618 CurrentDevicePath = DevicePathPkg + sizeof (EFI_HII_PACKAGE_HEADER);
2619 if ((DevicePathLength == GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) CurrentDevicePath)) &&
2620 (CompareMem (
2621 DevicePath,
2622 CurrentDevicePath,
2623 DevicePathLength
2624 ) == 0)) {
2625 DriverHandle = Database->DriverHandle;
2626 break;
2627 }
2628 }
2629 }
2630
2631 //
2632 // Try to find driver handle by device path.
2633 //
2634 if (DriverHandle == NULL) {
2635 TempDevicePath = DevicePath;
2636 Status = gBS->LocateDevicePath (
2637 &gEfiDevicePathProtocolGuid,
2638 &TempDevicePath,
2639 &DriverHandle
2640 );
2641 if (EFI_ERROR (Status) || (DriverHandle == NULL)) {
2642 //
2643 // Routing data does not match any known driver.
2644 // Set Progress to the 'G' in "GUID" of the routing header.
2645 //
2646 FreePool (DevicePath);
2647 *Progress = StringPtr;
2648 FreePool (ConfigResp);
2649 return EFI_NOT_FOUND;
2650 }
2651 }
2652
2653 FreePool (DevicePath);
2654
2655 //
2656 // Call corresponding ConfigAccess protocol to route settings
2657 //
2658 Status = gBS->HandleProtocol (
2659 DriverHandle,
2660 &gEfiHiiConfigAccessProtocolGuid,
2661 (VOID **) &ConfigAccess
2662 );
2663 ASSERT_EFI_ERROR (Status);
2664
2665 Status = ConfigAccess->RouteConfig (
2666 ConfigAccess,
2667 ConfigResp,
2668 &AccessProgress
2669 );
2670
2671 if (EFI_ERROR (Status)) {
2672 //
2673 // AccessProgress indicates the parsing progress on <ConfigResp>.
2674 // Map it to the progress on <MultiConfigResp> then return it.
2675 //
2676 *Progress = StrStr (StringPtr, AccessProgress);
2677
2678 FreePool (ConfigResp);
2679 return Status;
2680 }
2681
2682 FreePool (ConfigResp);
2683 ConfigResp = NULL;
2684
2685 //
2686 // Go to next <ConfigResp> (skip '&').
2687 //
2688 StringPtr += Length;
2689 if (*StringPtr == 0) {
2690 *Progress = StringPtr;
2691 break;
2692 }
2693
2694 StringPtr++;
2695
2696 }
2697
2698 return EFI_SUCCESS;
2699 }
2700
2701
2702 /**
2703 This helper function is to be called by drivers to map configuration data
2704 stored in byte array ("block") formats such as UEFI Variables into current
2705 configuration strings.
2706
2707 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
2708 instance.
2709 @param ConfigRequest A null-terminated Unicode string in
2710 <ConfigRequest> format.
2711 @param Block Array of bytes defining the block's configuration.
2712 @param BlockSize Length in bytes of Block.
2713 @param Config Filled-in configuration string. String allocated
2714 by the function. Returned only if call is
2715 successful. It is <ConfigResp> string format.
2716 @param Progress A pointer to a string filled in with the offset of
2717 the most recent & before the first failing
2718 name/value pair (or the beginning of the string if
2719 the failure is in the first name / value pair) or
2720 the terminating NULL if all was successful.
2721
2722 @retval EFI_SUCCESS The request succeeded. Progress points to the null
2723 terminator at the end of the ConfigRequest
2724 string.
2725 @retval EFI_OUT_OF_RESOURCES Not enough memory to allocate Config. Progress
2726 points to the first character of ConfigRequest.
2727 @retval EFI_INVALID_PARAMETER Passing in a NULL for the ConfigRequest or
2728 Block parameter would result in this type of
2729 error. Progress points to the first character of
2730 ConfigRequest.
2731 @retval EFI_DEVICE_ERROR Block not large enough. Progress undefined.
2732 @retval EFI_INVALID_PARAMETER Encountered non <BlockName> formatted string.
2733 Block is left updated and Progress points at
2734 the "&" preceding the first non-<BlockName>.
2735
2736 **/
2737 EFI_STATUS
2738 EFIAPI
2739 HiiBlockToConfig (
2740 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
2741 IN CONST EFI_STRING ConfigRequest,
2742 IN CONST UINT8 *Block,
2743 IN CONST UINTN BlockSize,
2744 OUT EFI_STRING *Config,
2745 OUT EFI_STRING *Progress
2746 )
2747 {
2748 HII_DATABASE_PRIVATE_DATA *Private;
2749 EFI_STRING StringPtr;
2750 UINTN Length;
2751 EFI_STATUS Status;
2752 EFI_STRING TmpPtr;
2753 UINT8 *TmpBuffer;
2754 UINTN Offset;
2755 UINTN Width;
2756 UINT8 *Value;
2757 EFI_STRING ValueStr;
2758 EFI_STRING ConfigElement;
2759 UINTN Index;
2760 UINT8 *TemBuffer;
2761 CHAR16 *TemString;
2762
2763 if (This == NULL || Progress == NULL || Config == NULL) {
2764 return EFI_INVALID_PARAMETER;
2765 }
2766
2767 if (Block == NULL || ConfigRequest == NULL) {
2768 *Progress = ConfigRequest;
2769 return EFI_INVALID_PARAMETER;
2770 }
2771
2772
2773 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
2774 ASSERT (Private != NULL);
2775
2776 StringPtr = ConfigRequest;
2777 ValueStr = NULL;
2778 Value = NULL;
2779 ConfigElement = NULL;
2780
2781 //
2782 // Allocate a fix length of memory to store Results. Reallocate memory for
2783 // Results if this fix length is insufficient.
2784 //
2785 *Config = (EFI_STRING) AllocateZeroPool (MAX_STRING_LENGTH);
2786 if (*Config == NULL) {
2787 return EFI_OUT_OF_RESOURCES;
2788 }
2789
2790 //
2791 // Jump <ConfigHdr>
2792 //
2793 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
2794 *Progress = StringPtr;
2795 Status = EFI_INVALID_PARAMETER;
2796 goto Exit;
2797 }
2798 while (*StringPtr != 0 && StrnCmp (StringPtr, L"PATH=", StrLen (L"PATH=")) != 0) {
2799 StringPtr++;
2800 }
2801 if (*StringPtr == 0) {
2802 *Progress = StringPtr - 1;
2803 Status = EFI_INVALID_PARAMETER;
2804 goto Exit;
2805 }
2806
2807 while (*StringPtr != L'&' && *StringPtr != 0) {
2808 StringPtr++;
2809 }
2810 if (*StringPtr == 0) {
2811 *Progress = StringPtr - 1;
2812 Status = EFI_INVALID_PARAMETER;
2813 goto Exit;
2814 }
2815 //
2816 // Skip '&'
2817 //
2818 StringPtr++;
2819
2820 //
2821 // Copy <ConfigHdr> and an additional '&' to <ConfigResp>
2822 //
2823 Length = StringPtr - ConfigRequest;
2824 CopyMem (*Config, ConfigRequest, Length * sizeof (CHAR16));
2825
2826 //
2827 // Parse each <RequestElement> if exists
2828 // Only <BlockName> format is supported by this help function.
2829 // <BlockName> ::= 'OFFSET='<Number>&'WIDTH='<Number>
2830 //
2831 while (*StringPtr != 0 && StrnCmp (StringPtr, L"OFFSET=", StrLen (L"OFFSET=")) == 0) {
2832 //
2833 // Back up the header of one <BlockName>
2834 //
2835 TmpPtr = StringPtr;
2836
2837 StringPtr += StrLen (L"OFFSET=");
2838 //
2839 // Get Offset
2840 //
2841 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
2842 if (Status == EFI_OUT_OF_RESOURCES) {
2843 *Progress = ConfigRequest;
2844 goto Exit;
2845 }
2846 Offset = 0;
2847 CopyMem (
2848 &Offset,
2849 TmpBuffer,
2850 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
2851 );
2852 FreePool (TmpBuffer);
2853
2854 StringPtr += Length;
2855 if (StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
2856 *Progress = StringPtr - Length - StrLen (L"OFFSET=") - 1;
2857 Status = EFI_INVALID_PARAMETER;
2858 goto Exit;
2859 }
2860 StringPtr += StrLen (L"&WIDTH=");
2861
2862 //
2863 // Get Width
2864 //
2865 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
2866 if (Status == EFI_OUT_OF_RESOURCES) {
2867 *Progress = ConfigRequest;
2868 goto Exit;
2869 }
2870 Width = 0;
2871 CopyMem (
2872 &Width,
2873 TmpBuffer,
2874 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
2875 );
2876 FreePool (TmpBuffer);
2877
2878 StringPtr += Length;
2879 if (*StringPtr != 0 && *StringPtr != L'&') {
2880 *Progress = StringPtr - Length - StrLen (L"&WIDTH=");
2881 Status = EFI_INVALID_PARAMETER;
2882 goto Exit;
2883 }
2884
2885 //
2886 // Calculate Value and convert it to hex string.
2887 //
2888 if (Offset + Width > BlockSize) {
2889 *Progress = StringPtr;
2890 Status = EFI_DEVICE_ERROR;
2891 goto Exit;
2892 }
2893
2894 Value = (UINT8 *) AllocateZeroPool (Width);
2895 if (Value == NULL) {
2896 *Progress = ConfigRequest;
2897 Status = EFI_OUT_OF_RESOURCES;
2898 goto Exit;
2899 }
2900
2901 CopyMem (Value, (UINT8 *) Block + Offset, Width);
2902
2903 Length = Width * 2 + 1;
2904 ValueStr = (EFI_STRING) AllocateZeroPool (Length * sizeof (CHAR16));
2905 if (ValueStr == NULL) {
2906 *Progress = ConfigRequest;
2907 Status = EFI_OUT_OF_RESOURCES;
2908 goto Exit;
2909 }
2910
2911 TemString = ValueStr;
2912 TemBuffer = Value + Width - 1;
2913 for (Index = 0; Index < Width; Index ++, TemBuffer --) {
2914 TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemBuffer, 2);
2915 }
2916
2917 FreePool (Value);
2918 Value = NULL;
2919
2920 //
2921 // Build a ConfigElement
2922 //
2923 Length += StringPtr - TmpPtr + 1 + StrLen (L"VALUE=");
2924 ConfigElement = (EFI_STRING) AllocateZeroPool (Length * sizeof (CHAR16));
2925 if (ConfigElement == NULL) {
2926 Status = EFI_OUT_OF_RESOURCES;
2927 goto Exit;
2928 }
2929 CopyMem (ConfigElement, TmpPtr, (StringPtr - TmpPtr + 1) * sizeof (CHAR16));
2930 if (*StringPtr == 0) {
2931 *(ConfigElement + (StringPtr - TmpPtr)) = L'&';
2932 }
2933 *(ConfigElement + (StringPtr - TmpPtr) + 1) = 0;
2934 StrCat (ConfigElement, L"VALUE=");
2935 StrCat (ConfigElement, ValueStr);
2936
2937 AppendToMultiString (Config, ConfigElement);
2938
2939 FreePool (ConfigElement);
2940 FreePool (ValueStr);
2941 ConfigElement = NULL;
2942 ValueStr = NULL;
2943
2944 //
2945 // If '\0', parsing is finished. Otherwise skip '&' to continue
2946 //
2947 if (*StringPtr == 0) {
2948 break;
2949 }
2950 AppendToMultiString (Config, L"&");
2951 StringPtr++;
2952
2953 }
2954
2955 if (*StringPtr != 0) {
2956 *Progress = StringPtr - 1;
2957 Status = EFI_INVALID_PARAMETER;
2958 goto Exit;
2959 }
2960
2961 HiiToLower (*Config);
2962 *Progress = StringPtr;
2963 return EFI_SUCCESS;
2964
2965 Exit:
2966 if (*Config != NULL) {
2967 FreePool (*Config);
2968 *Config = NULL;
2969 }
2970 if (ValueStr != NULL) {
2971 FreePool (ValueStr);
2972 }
2973 if (Value != NULL) {
2974 FreePool (Value);
2975 }
2976 if (ConfigElement != NULL) {
2977 FreePool (ConfigElement);
2978 }
2979
2980 return Status;
2981
2982 }
2983
2984
2985 /**
2986 This helper function is to be called by drivers to map configuration strings
2987 to configurations stored in byte array ("block") formats such as UEFI Variables.
2988
2989 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
2990 instance.
2991 @param ConfigResp A null-terminated Unicode string in <ConfigResp>
2992 format. It can be ConfigAltResp format string.
2993 @param Block A possibly null array of bytes representing the
2994 current block. Only bytes referenced in the
2995 ConfigResp string in the block are modified. If
2996 this parameter is null or if the *BlockSize
2997 parameter is (on input) shorter than required by
2998 the Configuration string, only the BlockSize
2999 parameter is updated and an appropriate status
3000 (see below) is returned.
3001 @param BlockSize The length of the Block in units of UINT8. On
3002 input, this is the size of the Block. On output,
3003 if successful, contains the index of the last
3004 modified byte in the Block.
3005 @param Progress On return, points to an element of the ConfigResp
3006 string filled in with the offset of the most
3007 recent '&' before the first failing name / value
3008 pair (or the beginning of the string if the
3009 failure is in the first name / value pair) or the
3010 terminating NULL if all was successful.
3011
3012 @retval EFI_SUCCESS The request succeeded. Progress points to the null
3013 terminator at the end of the ConfigResp string.
3014 @retval EFI_OUT_OF_RESOURCES Not enough memory to allocate Config. Progress
3015 points to the first character of ConfigResp.
3016 @retval EFI_INVALID_PARAMETER Passing in a NULL for the ConfigResp or
3017 Block parameter would result in this type of
3018 error. Progress points to the first character of
3019 ConfigResp.
3020 @retval EFI_INVALID_PARAMETER Encountered non <BlockName> formatted name /
3021 value pair. Block is left updated and
3022 Progress points at the '&' preceding the first
3023 non-<BlockName>.
3024
3025 **/
3026 EFI_STATUS
3027 EFIAPI
3028 HiiConfigToBlock (
3029 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
3030 IN CONST EFI_STRING ConfigResp,
3031 IN OUT UINT8 *Block,
3032 IN OUT UINTN *BlockSize,
3033 OUT EFI_STRING *Progress
3034 )
3035 {
3036 HII_DATABASE_PRIVATE_DATA *Private;
3037 EFI_STRING StringPtr;
3038 UINTN Length;
3039 EFI_STATUS Status;
3040 UINT8 *TmpBuffer;
3041 UINTN Offset;
3042 UINTN Width;
3043 UINT8 *Value;
3044 UINTN BufferSize;
3045
3046 if (This == NULL || BlockSize == NULL || Progress == NULL) {
3047 return EFI_INVALID_PARAMETER;
3048 }
3049
3050 if (ConfigResp == NULL || Block == NULL) {
3051 *Progress = ConfigResp;
3052 return EFI_INVALID_PARAMETER;
3053 }
3054
3055 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
3056 ASSERT (Private != NULL);
3057
3058 StringPtr = ConfigResp;
3059 BufferSize = *BlockSize;
3060 Value = NULL;
3061
3062 //
3063 // Jump <ConfigHdr>
3064 //
3065 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
3066 *Progress = StringPtr;
3067 Status = EFI_INVALID_PARAMETER;
3068 goto Exit;
3069 }
3070 while (*StringPtr != 0 && StrnCmp (StringPtr, L"PATH=", StrLen (L"PATH=")) != 0) {
3071 StringPtr++;
3072 }
3073 if (*StringPtr == 0) {
3074 *Progress = StringPtr;
3075 Status = EFI_INVALID_PARAMETER;
3076 goto Exit;
3077 }
3078
3079 while (*StringPtr != L'&' && *StringPtr != 0) {
3080 StringPtr++;
3081 }
3082 if (*StringPtr == 0) {
3083 *Progress = StringPtr;
3084 Status = EFI_INVALID_PARAMETER;
3085 goto Exit;
3086 }
3087 //
3088 // Skip '&'
3089 //
3090 StringPtr++;
3091
3092 //
3093 // Parse each <ConfigElement> if exists
3094 // Only <BlockConfig> format is supported by this help function.
3095 // <BlockConfig> ::= 'OFFSET='<Number>&'WIDTH='<Number>&'VALUE='<Number>
3096 //
3097 while (*StringPtr != 0 && StrnCmp (StringPtr, L"OFFSET=", StrLen (L"OFFSET=")) == 0) {
3098 StringPtr += StrLen (L"OFFSET=");
3099 //
3100 // Get Offset
3101 //
3102 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
3103 if (EFI_ERROR (Status)) {
3104 *Progress = ConfigResp;
3105 goto Exit;
3106 }
3107 Offset = 0;
3108 CopyMem (
3109 &Offset,
3110 TmpBuffer,
3111 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
3112 );
3113 FreePool (TmpBuffer);
3114
3115 StringPtr += Length;
3116 if (StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
3117 *Progress = StringPtr - Length - StrLen (L"OFFSET=") - 1;
3118 Status = EFI_INVALID_PARAMETER;
3119 goto Exit;
3120 }
3121 StringPtr += StrLen (L"&WIDTH=");
3122
3123 //
3124 // Get Width
3125 //
3126 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
3127 if (Status == EFI_OUT_OF_RESOURCES) {
3128 *Progress = ConfigResp;
3129 goto Exit;
3130 }
3131 Width = 0;
3132 CopyMem (
3133 &Width,
3134 TmpBuffer,
3135 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
3136 );
3137 FreePool (TmpBuffer);
3138
3139 StringPtr += Length;
3140 if (StrnCmp (StringPtr, L"&VALUE=", StrLen (L"&VALUE=")) != 0) {
3141 *Progress = StringPtr - Length - StrLen (L"&WIDTH=");
3142 Status = EFI_INVALID_PARAMETER;
3143 goto Exit;
3144 }
3145 StringPtr += StrLen (L"&VALUE=");
3146
3147 //
3148 // Get Value
3149 //
3150 Status = GetValueOfNumber (StringPtr, &Value, &Length);
3151 if (EFI_ERROR (Status)) {
3152 *Progress = ConfigResp;
3153 goto Exit;
3154 }
3155
3156 StringPtr += Length;
3157 if (*StringPtr != 0 && *StringPtr != L'&') {
3158 *Progress = StringPtr - Length - 7;
3159 Status = EFI_INVALID_PARAMETER;
3160 goto Exit;
3161 }
3162
3163 //
3164 // Update the Block with configuration info
3165 //
3166
3167 if (Offset + Width > BufferSize) {
3168 return EFI_DEVICE_ERROR;
3169 }
3170
3171 CopyMem (Block + Offset, Value, Width);
3172 *BlockSize = Offset + Width - 1;
3173
3174 FreePool (Value);
3175 Value = NULL;
3176
3177 //
3178 // If '\0', parsing is finished. Otherwise skip '&' to continue
3179 //
3180 if (*StringPtr == 0) {
3181 break;
3182 }
3183
3184 StringPtr++;
3185 }
3186
3187 //
3188 // The input string is ConfigAltResp format.
3189 //
3190 if ((*StringPtr != 0) && (StrnCmp (StringPtr, L"&GUID=", StrLen (L"&GUID=")) != 0)) {
3191 *Progress = StringPtr - 1;
3192 Status = EFI_INVALID_PARAMETER;
3193 goto Exit;
3194 }
3195
3196 *Progress = StringPtr;
3197 return EFI_SUCCESS;
3198
3199 Exit:
3200
3201 if (Value != NULL) {
3202 FreePool (Value);
3203 }
3204 return Status;
3205 }
3206
3207
3208 /**
3209 This helper function is to be called by drivers to extract portions of
3210 a larger configuration string.
3211
3212 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
3213 instance.
3214 @param Configuration A null-terminated Unicode string in
3215 <MultiConfigAltResp> format. It is <ConfigAltResp> format.
3216 @param Guid A pointer to the GUID value to search for in the
3217 routing portion of the ConfigResp string when
3218 retrieving the requested data. If Guid is NULL,
3219 then all GUID values will be searched for.
3220 @param Name A pointer to the NAME value to search for in the
3221 routing portion of the ConfigResp string when
3222 retrieving the requested data. If Name is NULL,
3223 then all Name values will be searched for.
3224 @param DevicePath A pointer to the PATH value to search for in the
3225 routing portion of the ConfigResp string when
3226 retrieving the requested data. If DevicePath is
3227 NULL, then all DevicePath values will be searched
3228 for.
3229 @param AltCfgId A pointer to the ALTCFG value to search for in the
3230 routing portion of the ConfigResp string when
3231 retrieving the requested data. If this parameter
3232 is NULL, then the current setting will be
3233 retrieved.
3234 @param AltCfgResp A pointer to a buffer which will be allocated by
3235 the function which contains the retrieved string
3236 as requested. This buffer is only allocated if
3237 the call was successful. It is <ConfigResp> format.
3238
3239 @retval EFI_SUCCESS The request succeeded. The requested data was
3240 extracted and placed in the newly allocated
3241 AltCfgResp buffer.
3242 @retval EFI_OUT_OF_RESOURCES Not enough memory to allocate AltCfgResp.
3243 @retval EFI_INVALID_PARAMETER Any parameter is invalid.
3244 @retval EFI_NOT_FOUND Target for the specified routing data was not
3245 found.
3246
3247 **/
3248 EFI_STATUS
3249 EFIAPI
3250 HiiGetAltCfg (
3251 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
3252 IN CONST EFI_STRING Configuration,
3253 IN CONST EFI_GUID *Guid,
3254 IN CONST EFI_STRING Name,
3255 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
3256 IN CONST UINT16 *AltCfgId,
3257 OUT EFI_STRING *AltCfgResp
3258 )
3259 {
3260 EFI_STATUS Status;
3261 EFI_STRING StringPtr;
3262 EFI_STRING HdrStart;
3263 EFI_STRING HdrEnd;
3264 EFI_STRING TmpPtr;
3265 UINTN Length;
3266 EFI_STRING GuidStr;
3267 EFI_STRING NameStr;
3268 EFI_STRING PathStr;
3269 EFI_STRING AltIdStr;
3270 EFI_STRING Result;
3271 BOOLEAN GuidFlag;
3272 BOOLEAN NameFlag;
3273 BOOLEAN PathFlag;
3274
3275 HdrStart = NULL;
3276 HdrEnd = NULL;
3277 GuidStr = NULL;
3278 NameStr = NULL;
3279 PathStr = NULL;
3280 AltIdStr = NULL;
3281 Result = NULL;
3282 GuidFlag = FALSE;
3283 NameFlag = FALSE;
3284 PathFlag = FALSE;
3285
3286 if (This == NULL || Configuration == NULL || AltCfgResp == NULL) {
3287 return EFI_INVALID_PARAMETER;
3288 }
3289
3290 StringPtr = Configuration;
3291 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
3292 return EFI_INVALID_PARAMETER;
3293 }
3294
3295 //
3296 // Generate the sub string for later matching.
3297 //
3298 GenerateSubStr (L"GUID=", sizeof (EFI_GUID), (VOID *) Guid, 1, &GuidStr);
3299 GenerateSubStr (
3300 L"PATH=",
3301 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) DevicePath),
3302 (VOID *) DevicePath,
3303 1,
3304 &PathStr
3305 );
3306 if (AltCfgId != NULL) {
3307 GenerateSubStr (L"ALTCFG=", sizeof (UINT16), (VOID *) AltCfgId, 3, &AltIdStr);
3308 }
3309 if (Name != NULL) {
3310 GenerateSubStr (L"NAME=", StrLen (Name) * sizeof (CHAR16), (VOID *) Name, 2, &NameStr);
3311 } else {
3312 GenerateSubStr (L"NAME=", 0, NULL, 2, &NameStr);
3313 }
3314
3315 while (*StringPtr != 0) {
3316 //
3317 // Try to match the GUID
3318 //
3319 if (!GuidFlag) {
3320 TmpPtr = StrStr (StringPtr, GuidStr);
3321 if (TmpPtr == NULL) {
3322 Status = EFI_NOT_FOUND;
3323 goto Exit;
3324 }
3325 HdrStart = TmpPtr;
3326
3327 //
3328 // Jump to <NameHdr>
3329 //
3330 if (Guid != NULL) {
3331 StringPtr = TmpPtr + StrLen (GuidStr);
3332 } else {
3333 StringPtr = StrStr (TmpPtr, L"NAME=");
3334 if (StringPtr == NULL) {
3335 Status = EFI_NOT_FOUND;
3336 goto Exit;
3337 }
3338 }
3339 GuidFlag = TRUE;
3340 }
3341
3342 //
3343 // Try to match the NAME
3344 //
3345 if (GuidFlag && !NameFlag) {
3346 if (StrnCmp (StringPtr, NameStr, StrLen (NameStr)) != 0) {
3347 GuidFlag = FALSE;
3348 } else {
3349 //
3350 // Jump to <PathHdr>
3351 //
3352 if (Name != NULL) {
3353 StringPtr += StrLen (NameStr);
3354 } else {
3355 StringPtr = StrStr (StringPtr, L"PATH=");
3356 if (StringPtr == NULL) {
3357 Status = EFI_NOT_FOUND;
3358 goto Exit;
3359 }
3360 }
3361 NameFlag = TRUE;
3362 }
3363 }
3364
3365 //
3366 // Try to match the DevicePath
3367 //
3368 if (GuidFlag && NameFlag && !PathFlag) {
3369 if (StrnCmp (StringPtr, PathStr, StrLen (PathStr)) != 0) {
3370 GuidFlag = FALSE;
3371 NameFlag = FALSE;
3372 } else {
3373 //
3374 // Jump to '&' before <DescHdr> or <ConfigBody>
3375 //
3376 if (DevicePath != NULL) {
3377 StringPtr += StrLen (PathStr);
3378 } else {
3379 StringPtr = StrStr (StringPtr, L"&");
3380 if (StringPtr == NULL) {
3381 Status = EFI_NOT_FOUND;
3382 goto Exit;
3383 }
3384 StringPtr ++;
3385 }
3386 PathFlag = TRUE;
3387 HdrEnd = StringPtr;
3388 }
3389 }
3390
3391 //
3392 // Try to match the AltCfgId
3393 //
3394 if (GuidFlag && NameFlag && PathFlag) {
3395 if (AltCfgId == NULL) {
3396 //
3397 // Return Current Setting when AltCfgId is NULL.
3398 //
3399 Status = OutputConfigBody (StringPtr, &Result);
3400 goto Exit;
3401 }
3402 //
3403 // Search the <ConfigAltResp> to get the <AltResp> with AltCfgId.
3404 //
3405 if (StrnCmp (StringPtr, AltIdStr, StrLen (AltIdStr)) != 0) {
3406 GuidFlag = FALSE;
3407 NameFlag = FALSE;
3408 PathFlag = FALSE;
3409 } else {
3410 //
3411 // Skip AltIdStr and &
3412 //
3413 StringPtr = StringPtr + StrLen (AltIdStr);
3414 Status = OutputConfigBody (StringPtr, &Result);
3415 goto Exit;
3416 }
3417 }
3418 }
3419
3420 Status = EFI_NOT_FOUND;
3421
3422 Exit:
3423 *AltCfgResp = NULL;
3424 if (!EFI_ERROR (Status) && (Result != NULL)) {
3425 //
3426 // Copy the <ConfigHdr> and <ConfigBody>
3427 //
3428 Length = HdrEnd - HdrStart + StrLen (Result) + 1;
3429 *AltCfgResp = AllocateZeroPool (Length * sizeof (CHAR16));
3430 if (*AltCfgResp == NULL) {
3431 Status = EFI_OUT_OF_RESOURCES;
3432 } else {
3433 StrnCpy (*AltCfgResp, HdrStart, HdrEnd - HdrStart);
3434 StrCat (*AltCfgResp, Result);
3435 Status = EFI_SUCCESS;
3436 }
3437 }
3438
3439 if (GuidStr != NULL) {
3440 FreePool (GuidStr);
3441 }
3442 if (NameStr != NULL) {
3443 FreePool (NameStr);
3444 }
3445 if (PathStr != NULL) {
3446 FreePool (PathStr);
3447 }
3448 if (AltIdStr != NULL) {
3449 FreePool (AltIdStr);
3450 }
3451 if (Result != NULL) {
3452 FreePool (Result);
3453 }
3454
3455 return Status;
3456
3457 }
3458
3459