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