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