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