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