]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/HiiDatabaseDxe/ConfigRouting.c
MdeModulePkg DxeCore: Call PeCoffExtraActionLib member after Constructor
[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 - 2016, 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 StrCpyS (Str, Length, String);
256
257 StringHeader = Str + StrLen (String);
258 TemString = (CHAR16 *) StringHeader;
259
260 switch (Flag) {
261 case 1:
262 //
263 // Convert Buffer to Hex String in reverse order
264 //
265 TemBuffer = ((UINT8 *) Buffer);
266 for (Index = 0; Index < BufferLen; Index ++, TemBuffer ++) {
267 TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemBuffer, 2);
268 }
269 break;
270 case 2:
271 //
272 // Check buffer is enough
273 //
274 TemName = (CHAR16 *) Buffer;
275 ASSERT ((BufferLen * 2 + 1) >= (StrLen (TemName) * 4 + 1));
276 //
277 // Convert Unicode String to Config String, e.g. "ABCD" => "0041004200430044"
278 //
279 for (; *TemName != L'\0'; TemName++) {
280 TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemName, 4);
281 }
282 break;
283 case 3:
284 //
285 // Convert Buffer to Hex String
286 //
287 TemBuffer = ((UINT8 *) Buffer) + BufferLen - 1;
288 for (Index = 0; Index < BufferLen; Index ++, TemBuffer --) {
289 TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemBuffer, 2);
290 }
291 break;
292 default:
293 break;
294 }
295
296 //
297 // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.
298 //
299 StrCatS (Str, Length, L"&");
300 HiiToLower (Str);
301
302 *SubStr = Str;
303 }
304
305
306 /**
307 Retrieve the <ConfigBody> from String then output it.
308
309 This is a internal function.
310
311 @param String A sub string of a configuration string in
312 <MultiConfigAltResp> format.
313 @param ConfigBody Points to the output string. It's caller's
314 responsibility to free this buffer.
315
316 @retval EFI_INVALID_PARAMETER There is no form package in current hii database.
317 @retval EFI_OUT_OF_RESOURCES Not enough memory to finish this operation.
318 @retval EFI_SUCCESS All existing storage is exported.
319
320 **/
321 EFI_STATUS
322 OutputConfigBody (
323 IN EFI_STRING String,
324 OUT EFI_STRING *ConfigBody
325 )
326 {
327 EFI_STRING TmpPtr;
328 EFI_STRING Result;
329 UINTN Length;
330
331 if (String == NULL || ConfigBody == NULL) {
332 return EFI_INVALID_PARAMETER;
333 }
334
335 //
336 // The setting information should start OFFSET, not ALTCFG.
337 //
338 if (StrnCmp (String, L"&ALTCFG=", StrLen (L"&ALTCFG=")) == 0) {
339 return EFI_INVALID_PARAMETER;
340 }
341
342 TmpPtr = StrStr (String, L"GUID=");
343 if (TmpPtr == NULL) {
344 //
345 // It is the last <ConfigResp> of the incoming configuration string.
346 //
347 Result = AllocateCopyPool (StrSize (String), String);
348 if (Result == NULL) {
349 return EFI_OUT_OF_RESOURCES;
350 } else {
351 *ConfigBody = Result;
352 return EFI_SUCCESS;
353 }
354 }
355
356 Length = TmpPtr - String;
357 if (Length == 0) {
358 return EFI_NOT_FOUND;
359 }
360 Result = AllocateCopyPool (Length * sizeof (CHAR16), String);
361 if (Result == NULL) {
362 return EFI_OUT_OF_RESOURCES;
363 }
364
365 *(Result + Length - 1) = 0;
366 *ConfigBody = Result;
367 return EFI_SUCCESS;
368 }
369
370 /**
371 Append a string to a multi-string format.
372
373 This is a internal function.
374
375 @param MultiString String in <MultiConfigRequest>,
376 <MultiConfigAltResp>, or <MultiConfigResp>. On
377 input, the buffer length of this string is
378 MAX_STRING_LENGTH. On output, the buffer length
379 might be updated.
380 @param AppendString NULL-terminated Unicode string.
381
382 @retval EFI_INVALID_PARAMETER Any incoming parameter is invalid.
383 @retval EFI_SUCCESS AppendString is append to the end of MultiString
384
385 **/
386 EFI_STATUS
387 AppendToMultiString (
388 IN OUT EFI_STRING *MultiString,
389 IN EFI_STRING AppendString
390 )
391 {
392 UINTN AppendStringSize;
393 UINTN MultiStringSize;
394 UINTN MaxLen;
395
396 if (MultiString == NULL || *MultiString == NULL || AppendString == NULL) {
397 return EFI_INVALID_PARAMETER;
398 }
399
400 AppendStringSize = StrSize (AppendString);
401 MultiStringSize = StrSize (*MultiString);
402 MaxLen = MAX_STRING_LENGTH / sizeof (CHAR16);
403
404 //
405 // Enlarge the buffer each time when length exceeds MAX_STRING_LENGTH.
406 //
407 if (MultiStringSize + AppendStringSize > MAX_STRING_LENGTH ||
408 MultiStringSize > MAX_STRING_LENGTH) {
409 *MultiString = (EFI_STRING) ReallocatePool (
410 MultiStringSize,
411 MultiStringSize + AppendStringSize,
412 (VOID *) (*MultiString)
413 );
414 MaxLen = (MultiStringSize + AppendStringSize) / sizeof (CHAR16);
415 ASSERT (*MultiString != NULL);
416 }
417 //
418 // Append the incoming string
419 //
420 StrCatS (*MultiString, MaxLen, AppendString);
421
422 return EFI_SUCCESS;
423 }
424
425
426 /**
427 Get the value of <Number> in <BlockConfig> format, i.e. the value of OFFSET
428 or WIDTH or VALUE.
429 <BlockConfig> ::= 'OFFSET='<Number>&'WIDTH='<Number>&'VALUE'=<Number>
430
431 This is a internal function.
432
433 @param StringPtr String in <BlockConfig> format and points to the
434 first character of <Number>.
435 @param Number The output value. Caller takes the responsibility
436 to free memory.
437 @param Len Length of the <Number>, in characters.
438
439 @retval EFI_OUT_OF_RESOURCES Insufficient resources to store neccessary
440 structures.
441 @retval EFI_SUCCESS Value of <Number> is outputted in Number
442 successfully.
443
444 **/
445 EFI_STATUS
446 GetValueOfNumber (
447 IN EFI_STRING StringPtr,
448 OUT UINT8 **Number,
449 OUT UINTN *Len
450 )
451 {
452 EFI_STRING TmpPtr;
453 UINTN Length;
454 EFI_STRING Str;
455 UINT8 *Buf;
456 EFI_STATUS Status;
457 UINT8 DigitUint8;
458 UINTN Index;
459 CHAR16 TemStr[2];
460
461 if (StringPtr == NULL || *StringPtr == L'\0' || Number == NULL || Len == NULL) {
462 return EFI_INVALID_PARAMETER;
463 }
464
465 Buf = NULL;
466
467 TmpPtr = StringPtr;
468 while (*StringPtr != L'\0' && *StringPtr != L'&') {
469 StringPtr++;
470 }
471 *Len = StringPtr - TmpPtr;
472 Length = *Len + 1;
473
474 Str = (EFI_STRING) AllocateZeroPool (Length * sizeof (CHAR16));
475 if (Str == NULL) {
476 Status = EFI_OUT_OF_RESOURCES;
477 goto Exit;
478 }
479 CopyMem (Str, TmpPtr, *Len * sizeof (CHAR16));
480 *(Str + *Len) = L'\0';
481
482 Length = (Length + 1) / 2;
483 Buf = (UINT8 *) AllocateZeroPool (Length);
484 if (Buf == NULL) {
485 Status = EFI_OUT_OF_RESOURCES;
486 goto Exit;
487 }
488
489 Length = *Len;
490 ZeroMem (TemStr, sizeof (TemStr));
491 for (Index = 0; Index < Length; Index ++) {
492 TemStr[0] = Str[Length - Index - 1];
493 DigitUint8 = (UINT8) StrHexToUint64 (TemStr);
494 if ((Index & 1) == 0) {
495 Buf [Index/2] = DigitUint8;
496 } else {
497 Buf [Index/2] = (UINT8) ((DigitUint8 << 4) + Buf [Index/2]);
498 }
499 }
500
501 *Number = Buf;
502 Status = EFI_SUCCESS;
503
504 Exit:
505 if (Str != NULL) {
506 FreePool (Str);
507 }
508
509 return Status;
510 }
511
512 /**
513 To find the BlockName in the string with same value.
514
515 @param String Pointer to a Null-terminated Unicode string.
516 @param BlockName Pointer to a Null-terminated Unicode string to search for.
517 @param Buffer Pointer to the value correspond to the BlockName.
518 @param Found The Block whether has been found.
519 @param BufferLen The length of the buffer.
520
521 @retval EFI_OUT_OF_RESOURCES Insufficient resources to store neccessary structures.
522 @retval EFI_SUCCESS The function finishes successfully.
523
524 **/
525 EFI_STATUS
526 FindSameBlockElement(
527 IN EFI_STRING String,
528 IN EFI_STRING BlockName,
529 IN UINT8 *Buffer,
530 OUT BOOLEAN *Found,
531 IN UINTN BufferLen
532 )
533 {
534 EFI_STRING BlockPtr;
535 UINTN Length;
536 UINT8 *TempBuffer;
537 EFI_STATUS Status;
538
539 TempBuffer = NULL;
540 *Found = FALSE;
541 BlockPtr = StrStr (String, BlockName);
542
543 while (BlockPtr != NULL) {
544 BlockPtr += StrLen (BlockName);
545 Status = GetValueOfNumber (BlockPtr, &TempBuffer, &Length);
546 if (EFI_ERROR (Status)) {
547 return Status;
548 }
549 ASSERT (TempBuffer != NULL);
550 if ((BufferLen == Length) && (0 == CompareMem (Buffer, TempBuffer, Length))) {
551 *Found = TRUE;
552 return EFI_SUCCESS;
553 } else {
554 FreePool (TempBuffer);
555 TempBuffer = NULL;
556 BlockPtr = StrStr (BlockPtr + 1, BlockName);
557 }
558 }
559 return EFI_SUCCESS;
560 }
561
562 /**
563 Compare the <AltResp> in ConfigAltResp and DefaultAltCfgResp, if the <AltResp>
564 in DefaultAltCfgResp but not in ConfigAltResp,add it to the ConfigAltResp.
565
566 @param DefaultAltCfgResp Pointer to a null-terminated Unicode string in
567 <MultiConfigAltResp> format. The default value
568 string may contain more than one ConfigAltResp
569 string for the different varstore buffer.
570 @param ConfigAltResp Pointer to a null-terminated Unicode string in
571 <ConfigAltResp> format.
572 @param AltConfigHdr Pointer to a Unicode string in <AltConfigHdr> format.
573 @param ConfigAltRespChanged Whether the ConfigAltResp has been changed.
574
575 @retval EFI_OUT_OF_RESOURCES Insufficient resources to store neccessary structures.
576 @retval EFI_SUCCESS The function finishes successfully.
577
578 **/
579 EFI_STATUS
580 CompareBlockElementDefault (
581 IN EFI_STRING DefaultAltCfgResp,
582 IN OUT EFI_STRING *ConfigAltResp,
583 IN EFI_STRING AltConfigHdr,
584 IN OUT BOOLEAN *ConfigAltRespChanged
585 )
586 {
587 EFI_STATUS Status;
588 EFI_STRING BlockPtr;
589 EFI_STRING BlockPtrStart;
590 EFI_STRING StringPtr;
591 EFI_STRING AppendString;
592 EFI_STRING AltConfigHdrPtr;
593 UINT8 *TempBuffer;
594 UINTN OffsetLength;
595 UINTN AppendSize;
596 UINTN TotalSize;
597 BOOLEAN FoundOffset;
598
599 AppendString = NULL;
600 TempBuffer = NULL;
601 //
602 // Make BlockPtr point to the first <BlockConfig> with AltConfigHdr in DefaultAltCfgResp.
603 //
604 AltConfigHdrPtr = StrStr (DefaultAltCfgResp, AltConfigHdr);
605 ASSERT (AltConfigHdrPtr != NULL);
606 BlockPtr = StrStr (AltConfigHdrPtr, L"&OFFSET=");
607 //
608 // Make StringPtr point to the AltConfigHdr in ConfigAltResp.
609 //
610 StringPtr = StrStr (*ConfigAltResp, AltConfigHdr);
611 ASSERT (StringPtr != NULL);
612
613 while (BlockPtr != NULL) {
614 //
615 // Find the "&OFFSET=<Number>" block and get the value of the Number with AltConfigHdr in DefaultAltCfgResp.
616 //
617 BlockPtrStart = BlockPtr;
618 BlockPtr += StrLen (L"&OFFSET=");
619 Status = GetValueOfNumber (BlockPtr, &TempBuffer, &OffsetLength);
620 if (EFI_ERROR (Status)) {
621 Status = EFI_OUT_OF_RESOURCES;
622 goto Exit;
623 }
624 //
625 // To find the same "&OFFSET=<Number>" block in ConfigAltResp.
626 //
627 Status = FindSameBlockElement (StringPtr, L"&OFFSET=", TempBuffer, &FoundOffset, OffsetLength);
628 if (TempBuffer != NULL) {
629 FreePool (TempBuffer);
630 TempBuffer = NULL;
631 }
632 if (EFI_ERROR (Status)) {
633 Status = EFI_OUT_OF_RESOURCES;
634 goto Exit;
635 }
636 if (!FoundOffset) {
637 //
638 // Don't find the same "&OFFSET=<Number>" block in ConfigAltResp.
639 // Calculate the size of <BlockConfig>.
640 // <BlockConfig>::='OFFSET='<Number>'&WIDTH='<Number>'&VALUE='<Number>.
641 //
642 BlockPtr = StrStr (BlockPtr + 1, L"&OFFSET=");
643 if (BlockPtr != NULL) {
644 AppendSize = (BlockPtr - BlockPtrStart) * sizeof (CHAR16);
645 } else {
646 AppendSize = StrSize (BlockPtrStart);
647 }
648 //
649 // Copy the <BlockConfig> to AppendString.
650 //
651 if (AppendString == NULL) {
652 AppendString = (EFI_STRING) AllocateZeroPool (AppendSize + sizeof (CHAR16));
653 StrnCatS (AppendString, AppendSize / sizeof (CHAR16) + 1, BlockPtrStart, AppendSize / sizeof (CHAR16));
654 } else {
655 TotalSize = StrSize (AppendString) + AppendSize + sizeof (CHAR16);
656 AppendString = (EFI_STRING) ReallocatePool (
657 StrSize (AppendString),
658 TotalSize,
659 AppendString
660 );
661 if (AppendString == NULL) {
662 Status = EFI_OUT_OF_RESOURCES;
663 goto Exit;
664 }
665 StrnCatS (AppendString, TotalSize / sizeof (CHAR16), BlockPtrStart, AppendSize / sizeof (CHAR16));
666 }
667 } else {
668 //
669 // To find next "&OFFSET=<Number>" block with AltConfigHdr in DefaultAltCfgResp.
670 //
671 BlockPtr = StrStr (BlockPtr + 1, L"&OFFSET=");
672 }
673 }
674
675 if (AppendString != NULL) {
676 //
677 // Reallocate ConfigAltResp to copy the AppendString.
678 //
679 TotalSize = StrSize (*ConfigAltResp) + StrSize (AppendString) + sizeof (CHAR16);
680 *ConfigAltResp = (EFI_STRING) ReallocatePool (
681 StrSize (*ConfigAltResp),
682 TotalSize,
683 *ConfigAltResp
684 );
685 if (*ConfigAltResp == NULL) {
686 Status = EFI_OUT_OF_RESOURCES;
687 goto Exit;
688 }
689 StrCatS (*ConfigAltResp, TotalSize / sizeof (CHAR16), AppendString);
690 *ConfigAltRespChanged = TRUE;
691 }
692
693 Status = EFI_SUCCESS;
694
695 Exit:
696 if (AppendString != NULL) {
697 FreePool (AppendString);
698 }
699
700 return Status;
701 }
702
703 /**
704 Compare the <AltResp> in ConfigAltResp and DefaultAltCfgResp, if the <AltResp>
705 in DefaultAltCfgResp but not in ConfigAltResp,add it to the ConfigAltResp.
706
707 @param DefaultAltCfgResp Pointer to a null-terminated Unicode string in
708 <MultiConfigAltResp> format. The default value
709 string may contain more than one ConfigAltResp
710 string for the different varstore buffer.
711 @param ConfigAltResp Pointer to a null-terminated Unicode string in
712 <ConfigAltResp> format.
713 @param AltConfigHdr Pointer to a Unicode string in <AltConfigHdr> format.
714 @param ConfigAltRespChanged Whether the ConfigAltResp has been changed.
715
716 @retval EFI_OUT_OF_RESOURCES Insufficient resources to store neccessary structures.
717 @retval EFI_SUCCESS The function finishes successfully.
718
719 **/
720 EFI_STATUS
721 CompareNameElementDefault (
722 IN EFI_STRING DefaultAltCfgResp,
723 IN OUT EFI_STRING *ConfigAltResp,
724 IN EFI_STRING AltConfigHdr,
725 IN OUT BOOLEAN *ConfigAltRespChanged
726 )
727 {
728 EFI_STATUS Status;
729 EFI_STRING NvConfigPtr;
730 EFI_STRING NvConfigStart;
731 EFI_STRING NvConfigValuePtr;
732 EFI_STRING StringPtr;
733 EFI_STRING NvConfigExist;
734 EFI_STRING AppendString;
735 CHAR16 TempChar;
736 UINTN AppendSize;
737 UINTN TotalSize;
738
739 AppendString = NULL;
740 NvConfigExist = NULL;
741 //
742 // Make NvConfigPtr point to the first <NvConfig> with AltConfigHdr in DefaultAltCfgResp.
743 //
744 NvConfigPtr = StrStr (DefaultAltCfgResp, AltConfigHdr);
745 ASSERT (NvConfigPtr != NULL);
746 NvConfigPtr = StrStr (NvConfigPtr + StrLen(AltConfigHdr),L"&");
747 //
748 // Make StringPtr point to the first <NvConfig> with AltConfigHdr in ConfigAltResp.
749 //
750 StringPtr = StrStr (*ConfigAltResp, AltConfigHdr);
751 ASSERT (StringPtr != NULL);
752 StringPtr = StrStr (StringPtr + StrLen (AltConfigHdr), L"&");
753 ASSERT (StringPtr != NULL);
754
755 while (NvConfigPtr != NULL) {
756 //
757 // <NvConfig> ::= <Label>'='<String> | <Label>'='<Number>.
758 // Get the <Label> with AltConfigHdr in DefaultAltCfgResp.
759 //
760 NvConfigStart = NvConfigPtr;
761 NvConfigValuePtr = StrStr (NvConfigPtr + 1, L"=");
762 ASSERT (NvConfigValuePtr != NULL);
763 TempChar = *NvConfigValuePtr;
764 *NvConfigValuePtr = L'\0';
765 //
766 // Get the <Label> with AltConfigHdr in ConfigAltResp.
767 //
768 NvConfigExist = StrStr (StringPtr, NvConfigPtr);
769 if (NvConfigExist == NULL) {
770 //
771 // Don't find same <Label> in ConfigAltResp.
772 // Calculate the size of <NvConfig>.
773 //
774 *NvConfigValuePtr = TempChar;
775 NvConfigPtr = StrStr (NvConfigPtr + 1, L"&");
776 if (NvConfigPtr != NULL) {
777 AppendSize = (NvConfigPtr - NvConfigStart) * sizeof (CHAR16);
778 } else {
779 AppendSize = StrSize (NvConfigStart);
780 }
781 //
782 // Copy the <NvConfig> to AppendString.
783 //
784 if (AppendString == NULL) {
785 AppendString = (EFI_STRING) AllocateZeroPool (AppendSize + sizeof (CHAR16));
786 StrnCatS (AppendString, AppendSize / sizeof (CHAR16) + 1, NvConfigStart, AppendSize / sizeof (CHAR16));
787 } else {
788 TotalSize = StrSize (AppendString) + AppendSize + sizeof (CHAR16);
789 AppendString = (EFI_STRING) ReallocatePool (
790 StrSize (AppendString),
791 TotalSize,
792 AppendString
793 );
794 if (AppendString == NULL) {
795 Status = EFI_OUT_OF_RESOURCES;
796 goto Exit;
797 }
798 StrnCatS (AppendString, TotalSize / sizeof (CHAR16), NvConfigStart, AppendSize / sizeof (CHAR16));
799 }
800 } else {
801 //
802 // To find next <Label> in DefaultAltCfgResp.
803 //
804 *NvConfigValuePtr = TempChar;
805 NvConfigPtr = StrStr (NvConfigPtr + 1, L"&");
806 }
807 }
808 if (AppendString != NULL) {
809 //
810 // Reallocate ConfigAltResp to copy the AppendString.
811 //
812 TotalSize = StrSize (*ConfigAltResp) + StrSize (AppendString) + sizeof (CHAR16);
813 *ConfigAltResp = (EFI_STRING) ReallocatePool (
814 StrSize (*ConfigAltResp),
815 StrSize (*ConfigAltResp) + StrSize (AppendString) + sizeof (CHAR16),
816 *ConfigAltResp
817 );
818 if (*ConfigAltResp == NULL) {
819 Status = EFI_OUT_OF_RESOURCES;
820 goto Exit;
821 }
822 StrCatS (*ConfigAltResp, TotalSize / sizeof (CHAR16), AppendString);
823 *ConfigAltRespChanged = TRUE;
824 }
825 Status = EFI_SUCCESS;
826
827 Exit:
828 if (AppendString != NULL) {
829 FreePool (AppendString);
830 }
831 return Status;
832 }
833
834 /**
835 Compare the <AltResp> in AltCfgResp and DefaultAltCfgResp, if the <AltResp>
836 in DefaultAltCfgResp but not in AltCfgResp,add it to the AltCfgResp.
837
838 @param AltCfgResp Pointer to a null-terminated Unicode string in
839 <ConfigAltResp> format.
840 @param DefaultAltCfgResp Pointer to a null-terminated Unicode string in
841 <MultiConfigAltResp> format. The default value
842 string may contain more than one ConfigAltResp
843 string for the different varstore buffer.
844 @param AltConfigHdr Pointer to a Unicode string in <AltConfigHdr> format.
845
846 @retval EFI_OUT_OF_RESOURCES Insufficient resources to store neccessary
847 structures.
848 @retval EFI_SUCCESS The function finishes successfully.
849
850 **/
851 EFI_STATUS
852 CompareAndMergeDefaultString (
853 IN OUT EFI_STRING *AltCfgResp,
854 IN EFI_STRING DefaultAltCfgResp,
855 IN EFI_STRING AltConfigHdr
856 )
857 {
858 EFI_STATUS Status;
859 EFI_STRING AltCfgRespBackup;
860 EFI_STRING AltConfigHdrPtr;
861 EFI_STRING AltConfigHdrPtrNext;
862 EFI_STRING ConfigAltResp;
863 EFI_STRING StringPtr;
864 EFI_STRING StringPtrNext;
865 EFI_STRING BlockPtr;
866 UINTN ReallocateSize;
867 CHAR16 TempChar;
868 CHAR16 TempCharA;
869 BOOLEAN ConfigAltRespChanged;
870
871 Status = EFI_OUT_OF_RESOURCES;
872 BlockPtr = NULL;
873 AltConfigHdrPtrNext = NULL;
874 StringPtrNext = NULL;
875 ConfigAltResp = NULL;
876 AltCfgRespBackup = NULL;
877 ConfigAltRespChanged = FALSE;
878
879 //
880 //To find the <AltResp> with AltConfigHdr in DefaultAltCfgResp, ignore other <AltResp> which follow it.
881 //
882 AltConfigHdrPtr = StrStr (DefaultAltCfgResp, AltConfigHdr);
883 ASSERT (AltConfigHdrPtr != NULL);
884 AltConfigHdrPtrNext = StrStr (AltConfigHdrPtr + 1, L"&GUID");
885 if (AltConfigHdrPtrNext != NULL) {
886 TempChar = *AltConfigHdrPtrNext;
887 *AltConfigHdrPtrNext = L'\0';
888 }
889 //
890 // To find the <AltResp> with AltConfigHdr in AltCfgResp, ignore other <AltResp> which follow it.
891 //
892 StringPtr = StrStr (*AltCfgResp, AltConfigHdr);
893 StringPtrNext = StrStr (StringPtr + 1, L"&GUID");
894 if (StringPtrNext != NULL) {
895 TempCharA = *StringPtrNext;
896 *StringPtrNext = L'\0';
897 }
898 //
899 // Copy the content of <ConfigAltResp> which contain current AltConfigHdr in AltCfgResp.
900 //
901 ConfigAltResp = AllocateCopyPool (StrSize (*AltCfgResp), *AltCfgResp);
902 if (ConfigAltResp == NULL) {
903 goto Exit;
904 }
905 //
906 // To find the <ConfigBody> with AltConfigHdr in DefaultAltCfgResp.
907 //
908 BlockPtr = StrStr (AltConfigHdrPtr, L"&OFFSET=");
909 if (BlockPtr != NULL) {
910 //
911 // <BlockConfig>::='OFFSET='<Number>'&WIDTH='<Number>'&VALUE='<Number> style.
912 // Call function CompareBlockElementDefault to compare the <BlockConfig> in DefaultAltCfgResp and ConfigAltResp.
913 // The ConfigAltResp which may contain the new <BlockConfig> get from DefaultAltCfgResp.
914 //
915 Status = CompareBlockElementDefault (DefaultAltCfgResp, &ConfigAltResp, AltConfigHdr, &ConfigAltRespChanged);
916 if (EFI_ERROR(Status)) {
917 goto Exit;
918 }
919 } else {
920 //
921 // <NvConfig> ::= <Label>'='<String> | <Label>'='<Number> style.
922 // Call function CompareNameElementDefault to compare the <NvConfig> in DefaultAltCfgResp and ConfigAltResp.
923 // The ConfigAltResp which may contain the new <NvConfig> get from DefaultAltCfgResp.
924 //
925 Status = CompareNameElementDefault (DefaultAltCfgResp, &ConfigAltResp, AltConfigHdr, &ConfigAltRespChanged);
926 if (EFI_ERROR(Status)) {
927 goto Exit;
928 }
929 }
930 //
931 // Restore the AltCfgResp.
932 //
933 if (StringPtrNext != NULL) {
934 *StringPtrNext = TempCharA;
935 }
936
937 //
938 // If the ConfigAltResp has no change,no need to update the content in AltCfgResp.
939 //
940 if (ConfigAltRespChanged == FALSE) {
941 Status = EFI_SUCCESS;
942 goto Exit;
943 }
944 //
945 // ConfigAltResp has been changed, need to update the content in AltCfgResp.
946 //
947 if (StringPtrNext != NULL) {
948 ReallocateSize = StrSize (ConfigAltResp) + StrSize (StringPtrNext) + sizeof (CHAR16);
949 } else {
950 ReallocateSize = StrSize (ConfigAltResp) + sizeof (CHAR16);
951 }
952
953 AltCfgRespBackup = (EFI_STRING) AllocateZeroPool (ReallocateSize);
954 if (AltCfgRespBackup == NULL) {
955 goto Exit;
956 }
957
958 StrCatS (AltCfgRespBackup, ReallocateSize / sizeof (CHAR16), ConfigAltResp);
959 if (StringPtrNext != NULL) {
960 StrCatS (AltCfgRespBackup, ReallocateSize / sizeof (CHAR16), StringPtrNext);
961 }
962
963 FreePool (*AltCfgResp);
964 *AltCfgResp = AltCfgRespBackup;
965
966 Status = EFI_SUCCESS;
967
968 Exit:
969 if (ConfigAltResp != NULL) {
970 FreePool(ConfigAltResp);
971 }
972 //
973 // Restore the DefaultAltCfgResp.
974 //
975 if ( AltConfigHdrPtrNext != NULL) {
976 *AltConfigHdrPtrNext = TempChar;
977 AltConfigHdrPtrNext = NULL;
978 }
979
980 return Status;
981 }
982
983 /**
984 This function merges DefaultAltCfgResp string into AltCfgResp string for
985 the missing AltCfgId in AltCfgResq.
986
987 @param AltCfgResp Pointer to a null-terminated Unicode string in
988 <ConfigAltResp> format. The default value string
989 will be merged into it.
990 @param DefaultAltCfgResp Pointer to a null-terminated Unicode string in
991 <MultiConfigAltResp> format. The default value
992 string may contain more than one ConfigAltResp
993 string for the different varstore buffer.
994
995 @retval EFI_SUCCESS The merged string returns.
996 @retval EFI_INVALID_PARAMETER *AltCfgResp is to NULL.
997 **/
998 EFI_STATUS
999 EFIAPI
1000 MergeDefaultString (
1001 IN OUT EFI_STRING *AltCfgResp,
1002 IN EFI_STRING DefaultAltCfgResp
1003 )
1004 {
1005 EFI_STRING StringPtrDefault;
1006 EFI_STRING StringPtrEnd;
1007 CHAR16 TempChar;
1008 EFI_STRING StringPtr;
1009 EFI_STRING AltConfigHdr;
1010 UINTN HeaderLength;
1011 UINTN SizeAltCfgResp;
1012 UINTN MaxLen;
1013 UINTN TotalSize;
1014
1015 if (*AltCfgResp == NULL) {
1016 return EFI_INVALID_PARAMETER;
1017 }
1018
1019 //
1020 // Get the requestr ConfigHdr
1021 //
1022 SizeAltCfgResp = 0;
1023 StringPtr = *AltCfgResp;
1024
1025 //
1026 // Find <ConfigHdr> GUID=...&NAME=...&PATH=...
1027 //
1028 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
1029 return EFI_INVALID_PARAMETER;
1030 }
1031 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&NAME=", StrLen (L"&NAME=")) != 0) {
1032 StringPtr++;
1033 }
1034 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&PATH=", StrLen (L"&PATH=")) != 0) {
1035 StringPtr++;
1036 }
1037 if (*StringPtr == L'\0') {
1038 return EFI_INVALID_PARAMETER;
1039 }
1040 StringPtr += StrLen (L"&PATH=");
1041 while (*StringPtr != L'\0' && *StringPtr != L'&') {
1042 StringPtr ++;
1043 }
1044 HeaderLength = StringPtr - *AltCfgResp;
1045
1046 //
1047 // Construct AltConfigHdr string "&<ConfigHdr>&ALTCFG=XXXX\0"
1048 // |1| StrLen (ConfigHdr) | 8 | 4 | 1 |
1049 //
1050 MaxLen = 1 + HeaderLength + 8 + 4 + 1;
1051 AltConfigHdr = AllocateZeroPool (MaxLen * sizeof (CHAR16));
1052 if (AltConfigHdr == NULL) {
1053 return EFI_OUT_OF_RESOURCES;
1054 }
1055 StrCpyS (AltConfigHdr, MaxLen, L"&");
1056 StrnCatS (AltConfigHdr, MaxLen, *AltCfgResp, HeaderLength);
1057 StrCatS (AltConfigHdr, MaxLen, L"&ALTCFG=");
1058 HeaderLength = StrLen (AltConfigHdr);
1059
1060 StringPtrDefault = StrStr (DefaultAltCfgResp, AltConfigHdr);
1061 while (StringPtrDefault != NULL) {
1062 //
1063 // Get AltCfg Name
1064 //
1065 StrnCatS (AltConfigHdr, MaxLen, StringPtrDefault + HeaderLength, 4);
1066 StringPtr = StrStr (*AltCfgResp, AltConfigHdr);
1067
1068 //
1069 // Append the found default value string to the input AltCfgResp
1070 //
1071 if (StringPtr == NULL) {
1072 StringPtrEnd = StrStr (StringPtrDefault + 1, L"&GUID");
1073 SizeAltCfgResp = StrSize (*AltCfgResp);
1074 if (StringPtrEnd == NULL) {
1075 //
1076 // No more default string is found.
1077 //
1078 TotalSize = SizeAltCfgResp + StrSize (StringPtrDefault);
1079 *AltCfgResp = (EFI_STRING) ReallocatePool (
1080 SizeAltCfgResp,
1081 TotalSize,
1082 (VOID *) (*AltCfgResp)
1083 );
1084 if (*AltCfgResp == NULL) {
1085 FreePool (AltConfigHdr);
1086 return EFI_OUT_OF_RESOURCES;
1087 }
1088 StrCatS (*AltCfgResp, TotalSize / sizeof (CHAR16), StringPtrDefault);
1089 break;
1090 } else {
1091 TempChar = *StringPtrEnd;
1092 *StringPtrEnd = L'\0';
1093 TotalSize = SizeAltCfgResp + StrSize (StringPtrDefault);
1094 *AltCfgResp = (EFI_STRING) ReallocatePool (
1095 SizeAltCfgResp,
1096 TotalSize,
1097 (VOID *) (*AltCfgResp)
1098 );
1099 if (*AltCfgResp == NULL) {
1100 FreePool (AltConfigHdr);
1101 return EFI_OUT_OF_RESOURCES;
1102 }
1103 StrCatS (*AltCfgResp, TotalSize / sizeof (CHAR16), StringPtrDefault);
1104 *StringPtrEnd = TempChar;
1105 }
1106 } else {
1107 //
1108 // The AltCfgResp contains <AltCfgResp>.
1109 // If the <ConfigElement> in <AltCfgResp> in the DefaultAltCfgResp but not in the
1110 // related <AltCfgResp> in AltCfgResp, merge it to AltCfgResp. else no need to merge.
1111 //
1112 CompareAndMergeDefaultString (AltCfgResp, DefaultAltCfgResp, AltConfigHdr);
1113 }
1114
1115 //
1116 // Find next AltCfg String
1117 //
1118 *(AltConfigHdr + HeaderLength) = L'\0';
1119 StringPtrDefault = StrStr (StringPtrDefault + 1, AltConfigHdr);
1120 }
1121
1122 FreePool (AltConfigHdr);
1123 return EFI_SUCCESS;
1124 }
1125
1126 /**
1127 This function inserts new DefaultValueData into the BlockData DefaultValue array.
1128
1129 @param BlockData The BlockData is updated to add new default value.
1130 @param DefaultValueData The DefaultValue is added.
1131
1132 **/
1133 VOID
1134 InsertDefaultValue (
1135 IN IFR_BLOCK_DATA *BlockData,
1136 IN IFR_DEFAULT_DATA *DefaultValueData
1137 )
1138 {
1139 LIST_ENTRY *Link;
1140 IFR_DEFAULT_DATA *DefaultValueArray;
1141 LIST_ENTRY *DefaultLink;
1142
1143 DefaultLink = &BlockData->DefaultValueEntry;
1144
1145 for (Link = DefaultLink->ForwardLink; Link != DefaultLink; Link = Link->ForwardLink) {
1146 DefaultValueArray = BASE_CR (Link, IFR_DEFAULT_DATA, Entry);
1147 if (DefaultValueArray->DefaultId == DefaultValueData->DefaultId) {
1148 //
1149 // DEFAULT_VALUE_FROM_OPCODE has high priority, DEFAULT_VALUE_FROM_DEFAULT has low priority.
1150 //
1151 if (DefaultValueData->Type > DefaultValueArray->Type) {
1152 //
1153 // Update the default value array in BlockData.
1154 //
1155 CopyMem (&DefaultValueArray->Value, &DefaultValueData->Value, sizeof (EFI_IFR_TYPE_VALUE));
1156 DefaultValueArray->Type = DefaultValueData->Type;
1157 DefaultValueArray->Cleaned = DefaultValueData->Cleaned;
1158 }
1159 return;
1160 }
1161 }
1162
1163 //
1164 // Insert new default value data in tail.
1165 //
1166 DefaultValueArray = AllocateZeroPool (sizeof (IFR_DEFAULT_DATA));
1167 ASSERT (DefaultValueArray != NULL);
1168 CopyMem (DefaultValueArray, DefaultValueData, sizeof (IFR_DEFAULT_DATA));
1169 InsertTailList (Link, &DefaultValueArray->Entry);
1170 }
1171
1172 /**
1173 This function inserts new BlockData into the block link
1174
1175 @param BlockLink The list entry points to block array.
1176 @param BlockData The point to BlockData is added.
1177
1178 **/
1179 VOID
1180 InsertBlockData (
1181 IN LIST_ENTRY *BlockLink,
1182 IN IFR_BLOCK_DATA **BlockData
1183 )
1184 {
1185 LIST_ENTRY *Link;
1186 IFR_BLOCK_DATA *BlockArray;
1187 IFR_BLOCK_DATA *BlockSingleData;
1188
1189 BlockSingleData = *BlockData;
1190
1191 if (BlockSingleData->Name != NULL) {
1192 InsertTailList (BlockLink, &BlockSingleData->Entry);
1193 return;
1194 }
1195
1196 //
1197 // Insert block data in its Offset and Width order.
1198 //
1199 for (Link = BlockLink->ForwardLink; Link != BlockLink; Link = Link->ForwardLink) {
1200 BlockArray = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
1201 if (BlockArray->Offset == BlockSingleData->Offset) {
1202 if (BlockArray->Width > BlockSingleData->Width) {
1203 //
1204 // Insert this block data in the front of block array
1205 //
1206 InsertTailList (Link, &BlockSingleData->Entry);
1207 return;
1208 }
1209
1210 if (BlockArray->Width == BlockSingleData->Width) {
1211 //
1212 // The same block array has been added.
1213 //
1214 if (BlockSingleData != BlockArray) {
1215 FreePool (BlockSingleData);
1216 *BlockData = BlockArray;
1217 }
1218 return;
1219 }
1220 } else if (BlockArray->Offset > BlockSingleData->Offset) {
1221 //
1222 // Insert new block data in the front of block array
1223 //
1224 InsertTailList (Link, &BlockSingleData->Entry);
1225 return;
1226 }
1227 }
1228
1229 //
1230 // Add new block data into the tail.
1231 //
1232 InsertTailList (Link, &BlockSingleData->Entry);
1233 }
1234
1235 /**
1236 Retrieves a pointer to the a Null-terminated ASCII string containing the list
1237 of languages that an HII handle in the HII Database supports. The returned
1238 string is allocated using AllocatePool(). The caller is responsible for freeing
1239 the returned string using FreePool(). The format of the returned string follows
1240 the language format assumed the HII Database.
1241
1242 If HiiHandle is NULL, then ASSERT().
1243
1244 @param[in] HiiHandle A handle that was previously registered in the HII Database.
1245
1246 @retval NULL HiiHandle is not registered in the HII database
1247 @retval NULL There are not enough resources available to retrieve the suported
1248 languages.
1249 @retval NULL The list of suported languages could not be retrieved.
1250 @retval Other A pointer to the Null-terminated ASCII string of supported languages.
1251
1252 **/
1253 CHAR8 *
1254 GetSupportedLanguages (
1255 IN EFI_HII_HANDLE HiiHandle
1256 )
1257 {
1258 EFI_STATUS Status;
1259 UINTN LanguageSize;
1260 CHAR8 TempSupportedLanguages;
1261 CHAR8 *SupportedLanguages;
1262
1263 ASSERT (HiiHandle != NULL);
1264
1265 //
1266 // Retrieve the size required for the supported languages buffer.
1267 //
1268 LanguageSize = 0;
1269 Status = mPrivate.HiiString.GetLanguages (&mPrivate.HiiString, HiiHandle, &TempSupportedLanguages, &LanguageSize);
1270
1271 //
1272 // If GetLanguages() returns EFI_SUCCESS for a zero size,
1273 // then there are no supported languages registered for HiiHandle. If GetLanguages()
1274 // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present
1275 // in the HII Database
1276 //
1277 if (Status != EFI_BUFFER_TOO_SMALL) {
1278 //
1279 // Return NULL if the size can not be retrieved, or if HiiHandle is not in the HII Database
1280 //
1281 return NULL;
1282 }
1283
1284 //
1285 // Allocate the supported languages buffer.
1286 //
1287 SupportedLanguages = AllocateZeroPool (LanguageSize);
1288 if (SupportedLanguages == NULL) {
1289 //
1290 // Return NULL if allocation fails.
1291 //
1292 return NULL;
1293 }
1294
1295 //
1296 // Retrieve the supported languages string
1297 //
1298 Status = mPrivate.HiiString.GetLanguages (&mPrivate.HiiString, HiiHandle, SupportedLanguages, &LanguageSize);
1299 if (EFI_ERROR (Status)) {
1300 //
1301 // Free the buffer and return NULL if the supported languages can not be retrieved.
1302 //
1303 FreePool (SupportedLanguages);
1304 return NULL;
1305 }
1306
1307 //
1308 // Return the Null-terminated ASCII string of supported languages
1309 //
1310 return SupportedLanguages;
1311 }
1312
1313 /**
1314 Retrieves a string from a string package.
1315
1316 If HiiHandle is NULL, then ASSERT().
1317 If StringId is 0, then ASSET.
1318
1319 @param[in] HiiHandle A handle that was previously registered in the HII Database.
1320 @param[in] StringId The identifier of the string to retrieved from the string
1321 package associated with HiiHandle.
1322
1323 @retval NULL The string specified by StringId is not present in the string package.
1324 @retval Other The string was returned.
1325
1326 **/
1327 EFI_STRING
1328 InternalGetString (
1329 IN EFI_HII_HANDLE HiiHandle,
1330 IN EFI_STRING_ID StringId
1331 )
1332 {
1333 EFI_STATUS Status;
1334 UINTN StringSize;
1335 CHAR16 TempString;
1336 EFI_STRING String;
1337 CHAR8 *SupportedLanguages;
1338 CHAR8 *PlatformLanguage;
1339 CHAR8 *BestLanguage;
1340 CHAR8 *Language;
1341
1342 ASSERT (HiiHandle != NULL);
1343 ASSERT (StringId != 0);
1344
1345 //
1346 // Initialize all allocated buffers to NULL
1347 //
1348 SupportedLanguages = NULL;
1349 PlatformLanguage = NULL;
1350 BestLanguage = NULL;
1351 String = NULL;
1352 Language = "";
1353
1354 //
1355 // Get the languages that the package specified by HiiHandle supports
1356 //
1357 SupportedLanguages = GetSupportedLanguages (HiiHandle);
1358 if (SupportedLanguages == NULL) {
1359 goto Error;
1360 }
1361
1362 //
1363 // Get the current platform language setting
1364 //
1365 GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatformLanguage, NULL);
1366
1367 //
1368 // Get the best matching language from SupportedLanguages
1369 //
1370 BestLanguage = GetBestLanguage (
1371 SupportedLanguages,
1372 FALSE, // RFC 4646 mode
1373 Language, // Highest priority
1374 PlatformLanguage != NULL ? PlatformLanguage : "", // Next highest priority
1375 SupportedLanguages, // Lowest priority
1376 NULL
1377 );
1378 if (BestLanguage == NULL) {
1379 goto Error;
1380 }
1381
1382 //
1383 // Retrieve the size of the string in the string package for the BestLanguage
1384 //
1385 StringSize = 0;
1386 Status = mPrivate.HiiString.GetString (
1387 &mPrivate.HiiString,
1388 BestLanguage,
1389 HiiHandle,
1390 StringId,
1391 &TempString,
1392 &StringSize,
1393 NULL
1394 );
1395 //
1396 // If GetString() returns EFI_SUCCESS for a zero size,
1397 // then there are no supported languages registered for HiiHandle. If GetString()
1398 // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present
1399 // in the HII Database
1400 //
1401 if (Status != EFI_BUFFER_TOO_SMALL) {
1402 goto Error;
1403 }
1404
1405 //
1406 // Allocate a buffer for the return string
1407 //
1408 String = AllocateZeroPool (StringSize);
1409 if (String == NULL) {
1410 goto Error;
1411 }
1412
1413 //
1414 // Retrieve the string from the string package
1415 //
1416 Status = mPrivate.HiiString.GetString (
1417 &mPrivate.HiiString,
1418 BestLanguage,
1419 HiiHandle,
1420 StringId,
1421 String,
1422 &StringSize,
1423 NULL
1424 );
1425 if (EFI_ERROR (Status)) {
1426 //
1427 // Free the buffer and return NULL if the supported languages can not be retrieved.
1428 //
1429 FreePool (String);
1430 String = NULL;
1431 }
1432
1433 Error:
1434 //
1435 // Free allocated buffers
1436 //
1437 if (SupportedLanguages != NULL) {
1438 FreePool (SupportedLanguages);
1439 }
1440 if (PlatformLanguage != NULL) {
1441 FreePool (PlatformLanguage);
1442 }
1443 if (BestLanguage != NULL) {
1444 FreePool (BestLanguage);
1445 }
1446
1447 //
1448 // Return the Null-terminated Unicode string
1449 //
1450 return String;
1451 }
1452
1453 /**
1454 This function checks VarOffset and VarWidth is in the block range.
1455
1456 @param RequestBlockArray The block array is to be checked.
1457 @param VarOffset Offset of var to the structure
1458 @param VarWidth Width of var.
1459 @param IsNameValueType Whether this varstore is name/value varstore or not.
1460 @param HiiHandle Hii handle for this hii package.
1461
1462 @retval TRUE This Var is in the block range.
1463 @retval FALSE This Var is not in the block range.
1464 **/
1465 BOOLEAN
1466 BlockArrayCheck (
1467 IN IFR_BLOCK_DATA *RequestBlockArray,
1468 IN UINT16 VarOffset,
1469 IN UINT16 VarWidth,
1470 IN BOOLEAN IsNameValueType,
1471 IN EFI_HII_HANDLE HiiHandle
1472 )
1473 {
1474 LIST_ENTRY *Link;
1475 IFR_BLOCK_DATA *BlockData;
1476 EFI_STRING Name;
1477
1478 //
1479 // No Request Block array, all vars are got.
1480 //
1481 if (RequestBlockArray == NULL) {
1482 return TRUE;
1483 }
1484
1485 //
1486 // Check the input var is in the request block range.
1487 //
1488 for (Link = RequestBlockArray->Entry.ForwardLink; Link != &RequestBlockArray->Entry; Link = Link->ForwardLink) {
1489 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
1490
1491 if (IsNameValueType) {
1492 Name = InternalGetString (HiiHandle, VarOffset);
1493 ASSERT (Name != NULL);
1494
1495 if (StrnCmp (BlockData->Name, Name, StrLen (Name)) == 0) {
1496 FreePool (Name);
1497 return TRUE;
1498 }
1499 FreePool (Name);
1500 } else {
1501 if ((VarOffset >= BlockData->Offset) && ((VarOffset + VarWidth) <= (BlockData->Offset + BlockData->Width))) {
1502 return TRUE;
1503 }
1504 }
1505 }
1506
1507 return FALSE;
1508 }
1509
1510 /**
1511 Get form package data from data base.
1512
1513 @param DataBaseRecord The DataBaseRecord instance contains the found Hii handle and package.
1514 @param HiiFormPackage The buffer saves the package data.
1515 @param PackageSize The buffer size of the package data.
1516
1517 **/
1518 EFI_STATUS
1519 GetFormPackageData (
1520 IN HII_DATABASE_RECORD *DataBaseRecord,
1521 IN OUT UINT8 **HiiFormPackage,
1522 OUT UINTN *PackageSize
1523 )
1524 {
1525 EFI_STATUS Status;
1526 UINTN Size;
1527 UINTN ResultSize;
1528
1529 if (DataBaseRecord == NULL || HiiFormPackage == NULL || PackageSize == NULL) {
1530 return EFI_INVALID_PARAMETER;
1531 }
1532
1533 Size = 0;
1534 ResultSize = 0;
1535 //
1536 // 0. Get Hii Form Package by HiiHandle
1537 //
1538 Status = ExportFormPackages (
1539 &mPrivate,
1540 DataBaseRecord->Handle,
1541 DataBaseRecord->PackageList,
1542 0,
1543 Size,
1544 HiiFormPackage,
1545 &ResultSize
1546 );
1547 if (EFI_ERROR (Status)) {
1548 return Status;
1549 }
1550
1551 (*HiiFormPackage) = AllocatePool (ResultSize);
1552 if (*HiiFormPackage == NULL) {
1553 Status = EFI_OUT_OF_RESOURCES;
1554 return Status;
1555 }
1556
1557 //
1558 // Get HiiFormPackage by HiiHandle
1559 //
1560 Size = ResultSize;
1561 ResultSize = 0;
1562 Status = ExportFormPackages (
1563 &mPrivate,
1564 DataBaseRecord->Handle,
1565 DataBaseRecord->PackageList,
1566 0,
1567 Size,
1568 *HiiFormPackage,
1569 &ResultSize
1570 );
1571 if (EFI_ERROR (Status)) {
1572 FreePool (*HiiFormPackage);
1573 }
1574
1575 *PackageSize = Size;
1576
1577 return Status;
1578 }
1579
1580
1581 /**
1582 This function parses Form Package to get the efi varstore info according to the request ConfigHdr.
1583
1584 @param DataBaseRecord The DataBaseRecord instance contains the found Hii handle and package.
1585 @param ConfigHdr Request string ConfigHdr. If it is NULL,
1586 the first found varstore will be as ConfigHdr.
1587 @param IsEfiVarstore Whether the request storage type is efi varstore type.
1588 @param EfiVarStore The efi varstore info which will return.
1589 **/
1590 EFI_STATUS
1591 GetVarStoreType (
1592 IN HII_DATABASE_RECORD *DataBaseRecord,
1593 IN EFI_STRING ConfigHdr,
1594 OUT BOOLEAN *IsEfiVarstore,
1595 OUT EFI_IFR_VARSTORE_EFI **EfiVarStore
1596 )
1597 {
1598 EFI_STATUS Status;
1599 UINTN IfrOffset;
1600 UINTN PackageOffset;
1601 EFI_IFR_OP_HEADER *IfrOpHdr;
1602 CHAR16 *VarStoreName;
1603 EFI_STRING GuidStr;
1604 EFI_STRING NameStr;
1605 EFI_STRING TempStr;
1606 UINTN LengthString;
1607 UINT8 *HiiFormPackage;
1608 UINTN PackageSize;
1609 EFI_IFR_VARSTORE_EFI *IfrEfiVarStore;
1610 EFI_HII_PACKAGE_HEADER *PackageHeader;
1611
1612 HiiFormPackage = NULL;
1613 LengthString = 0;
1614 Status = EFI_SUCCESS;
1615 GuidStr = NULL;
1616 NameStr = NULL;
1617 TempStr = NULL;
1618 *IsEfiVarstore = FALSE;
1619
1620 Status = GetFormPackageData(DataBaseRecord, &HiiFormPackage, &PackageSize);
1621 if (EFI_ERROR (Status)) {
1622 return Status;
1623 }
1624
1625 IfrOffset = sizeof (EFI_HII_PACKAGE_HEADER);
1626 PackageOffset = IfrOffset;
1627 PackageHeader = (EFI_HII_PACKAGE_HEADER *) HiiFormPackage;
1628
1629 while (IfrOffset < PackageSize) {
1630 //
1631 // More than one form packages exist.
1632 //
1633 if (PackageOffset >= PackageHeader->Length) {
1634 //
1635 // Process the new form package.
1636 //
1637 PackageOffset = sizeof (EFI_HII_PACKAGE_HEADER);
1638 IfrOffset += PackageOffset;
1639 PackageHeader = (EFI_HII_PACKAGE_HEADER *) (HiiFormPackage + IfrOffset);
1640 }
1641
1642 IfrOpHdr = (EFI_IFR_OP_HEADER *) (HiiFormPackage + IfrOffset);
1643 IfrOffset += IfrOpHdr->Length;
1644 PackageOffset += IfrOpHdr->Length;
1645
1646 if (IfrOpHdr->OpCode == EFI_IFR_VARSTORE_EFI_OP ) {
1647 IfrEfiVarStore = (EFI_IFR_VARSTORE_EFI *) IfrOpHdr;
1648 //
1649 // If the length is small than the structure, this is from old efi
1650 // varstore definition. Old efi varstore get config directly from
1651 // GetVariable function.
1652 //
1653 if (IfrOpHdr->Length < sizeof (EFI_IFR_VARSTORE_EFI)) {
1654 continue;
1655 }
1656
1657 VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrEfiVarStore->Name) * sizeof (CHAR16));
1658 if (VarStoreName == NULL) {
1659 Status = EFI_OUT_OF_RESOURCES;
1660 goto Done;
1661 }
1662 AsciiStrToUnicodeStr ((CHAR8 *) IfrEfiVarStore->Name, VarStoreName);
1663
1664 GenerateSubStr (L"GUID=", sizeof (EFI_GUID), (VOID *) &IfrEfiVarStore->Guid, 1, &GuidStr);
1665 GenerateSubStr (L"NAME=", StrLen (VarStoreName) * sizeof (CHAR16), (VOID *) VarStoreName, 2, &NameStr);
1666 LengthString = StrLen (GuidStr);
1667 LengthString = LengthString + StrLen (NameStr) + 1;
1668 TempStr = AllocateZeroPool (LengthString * sizeof (CHAR16));
1669 if (TempStr == NULL) {
1670 FreePool (GuidStr);
1671 FreePool (NameStr);
1672 FreePool (VarStoreName);
1673 Status = EFI_OUT_OF_RESOURCES;
1674 goto Done;
1675 }
1676 StrCpyS (TempStr, LengthString, GuidStr);
1677 StrCatS (TempStr, LengthString, NameStr);
1678 if (ConfigHdr == NULL || StrnCmp (ConfigHdr, TempStr, StrLen (TempStr)) == 0) {
1679 *EfiVarStore = (EFI_IFR_VARSTORE_EFI *) AllocateZeroPool (IfrOpHdr->Length);
1680 if (*EfiVarStore == NULL) {
1681 FreePool (VarStoreName);
1682 FreePool (GuidStr);
1683 FreePool (NameStr);
1684 FreePool (TempStr);
1685 Status = EFI_OUT_OF_RESOURCES;
1686 goto Done;
1687 }
1688 *IsEfiVarstore = TRUE;
1689 CopyMem (*EfiVarStore, IfrEfiVarStore, IfrOpHdr->Length);
1690 }
1691
1692 //
1693 // Free alllocated temp string.
1694 //
1695 FreePool (VarStoreName);
1696 FreePool (GuidStr);
1697 FreePool (NameStr);
1698 FreePool (TempStr);
1699
1700 //
1701 // Already found the varstore, break;
1702 //
1703 if (*IsEfiVarstore) {
1704 break;
1705 }
1706 }
1707 }
1708 Done:
1709 if (HiiFormPackage != NULL) {
1710 FreePool (HiiFormPackage);
1711 }
1712
1713 return Status;
1714 }
1715
1716 /**
1717 Check whether the ConfigRequest string has the request elements.
1718 For EFI_HII_VARSTORE_BUFFER type, the request has "&OFFSET=****&WIDTH=****..." format.
1719 For EFI_HII_VARSTORE_NAME_VALUE type, the request has "&NAME1**&NAME2..." format.
1720
1721 @param ConfigRequest The input config request string.
1722
1723 @retval TRUE The input include config request elements.
1724 @retval FALSE The input string not includes.
1725
1726 **/
1727 BOOLEAN
1728 GetElementsFromRequest (
1729 IN EFI_STRING ConfigRequest
1730 )
1731 {
1732 EFI_STRING TmpRequest;
1733
1734 TmpRequest = StrStr (ConfigRequest, L"PATH=");
1735 ASSERT (TmpRequest != NULL);
1736
1737 if ((StrStr (TmpRequest, L"&OFFSET=") != NULL) || (StrStr (TmpRequest, L"&") != NULL)) {
1738 return TRUE;
1739 }
1740
1741 return FALSE;
1742 }
1743
1744 /**
1745 Check whether the this varstore is the request varstore.
1746
1747 @param VarstoreGuid Varstore guid.
1748 @param Name Varstore name.
1749 @param ConfigHdr Current configRequest info.
1750
1751 @retval TRUE This varstore is the requst one.
1752 @retval FALSE This varstore is not the requst one.
1753
1754 **/
1755 BOOLEAN
1756 IsThisVarstore (
1757 IN EFI_GUID *VarstoreGuid,
1758 IN CHAR16 *Name,
1759 IN CHAR16 *ConfigHdr
1760 )
1761 {
1762 EFI_STRING GuidStr;
1763 EFI_STRING NameStr;
1764 EFI_STRING TempStr;
1765 UINTN LengthString;
1766 BOOLEAN RetVal;
1767
1768 RetVal = FALSE;
1769 GuidStr = NULL;
1770 TempStr = NULL;
1771
1772 //
1773 // If ConfigHdr has name field and varstore not has name, return FALSE.
1774 //
1775 if (Name == NULL && ConfigHdr != NULL && StrStr (ConfigHdr, L"NAME=&") == NULL) {
1776 return FALSE;
1777 }
1778
1779 GenerateSubStr (L"GUID=", sizeof (EFI_GUID), (VOID *)VarstoreGuid, 1, &GuidStr);
1780 if (Name != NULL) {
1781 GenerateSubStr (L"NAME=", StrLen (Name) * sizeof (CHAR16), (VOID *) Name, 2, &NameStr);
1782 } else {
1783 GenerateSubStr (L"NAME=", 0, NULL, 2, &NameStr);
1784 }
1785 LengthString = StrLen (GuidStr);
1786 LengthString = LengthString + StrLen (NameStr) + 1;
1787 TempStr = AllocateZeroPool (LengthString * sizeof (CHAR16));
1788 if (TempStr == NULL) {
1789 goto Done;
1790 }
1791
1792 StrCpyS (TempStr, LengthString, GuidStr);
1793 StrCatS (TempStr, LengthString, NameStr);
1794
1795 if (ConfigHdr == NULL || StrnCmp (ConfigHdr, TempStr, StrLen (TempStr)) == 0) {
1796 RetVal = TRUE;
1797 }
1798
1799 Done:
1800 if (GuidStr != NULL) {
1801 FreePool (GuidStr);
1802 }
1803
1804 if (NameStr != NULL) {
1805 FreePool (NameStr);
1806 }
1807
1808 if (TempStr != NULL) {
1809 FreePool (TempStr);
1810 }
1811
1812 return RetVal;
1813 }
1814
1815 /**
1816 This function parses Form Package to get the efi varstore info according to the request ConfigHdr.
1817
1818 @param DataBaseRecord The DataBaseRecord instance contains the found Hii handle and package.
1819 @param ConfigHdr Request string ConfigHdr. If it is NULL,
1820 the first found varstore will be as ConfigHdr.
1821 @retval TRUE This hii package is the reqeust one.
1822 @retval FALSE This hii package is not the reqeust one.
1823 **/
1824 BOOLEAN
1825 IsThisPackageList (
1826 IN HII_DATABASE_RECORD *DataBaseRecord,
1827 IN EFI_STRING ConfigHdr
1828 )
1829 {
1830 EFI_STATUS Status;
1831 UINTN IfrOffset;
1832 UINTN PackageOffset;
1833 EFI_IFR_OP_HEADER *IfrOpHdr;
1834 CHAR16 *VarStoreName;
1835 UINT8 *HiiFormPackage;
1836 UINTN PackageSize;
1837 EFI_IFR_VARSTORE_EFI *IfrEfiVarStore;
1838 EFI_HII_PACKAGE_HEADER *PackageHeader;
1839 EFI_IFR_VARSTORE *IfrVarStore;
1840 EFI_IFR_VARSTORE_NAME_VALUE *IfrNameValueVarStore;
1841 BOOLEAN FindVarstore;
1842
1843 HiiFormPackage = NULL;
1844 VarStoreName = NULL;
1845 Status = EFI_SUCCESS;
1846 FindVarstore = FALSE;
1847
1848 Status = GetFormPackageData(DataBaseRecord, &HiiFormPackage, &PackageSize);
1849 if (EFI_ERROR (Status)) {
1850 return FALSE;
1851 }
1852
1853 IfrOffset = sizeof (EFI_HII_PACKAGE_HEADER);
1854 PackageOffset = IfrOffset;
1855 PackageHeader = (EFI_HII_PACKAGE_HEADER *) HiiFormPackage;
1856
1857 while (IfrOffset < PackageSize) {
1858 //
1859 // More than one form packages exist.
1860 //
1861 if (PackageOffset >= PackageHeader->Length) {
1862 //
1863 // Process the new form package.
1864 //
1865 PackageOffset = sizeof (EFI_HII_PACKAGE_HEADER);
1866 IfrOffset += PackageOffset;
1867 PackageHeader = (EFI_HII_PACKAGE_HEADER *) (HiiFormPackage + IfrOffset);
1868 }
1869
1870 IfrOpHdr = (EFI_IFR_OP_HEADER *) (HiiFormPackage + IfrOffset);
1871 IfrOffset += IfrOpHdr->Length;
1872 PackageOffset += IfrOpHdr->Length;
1873
1874 switch (IfrOpHdr->OpCode) {
1875
1876 case EFI_IFR_VARSTORE_OP:
1877 IfrVarStore = (EFI_IFR_VARSTORE *) IfrOpHdr;
1878
1879 VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrVarStore->Name) * sizeof (CHAR16));
1880 if (VarStoreName == NULL) {
1881 goto Done;
1882 }
1883 AsciiStrToUnicodeStr ((CHAR8 *)IfrVarStore->Name, VarStoreName);
1884
1885 if (IsThisVarstore((VOID *)&IfrVarStore->Guid, VarStoreName, ConfigHdr)) {
1886 FindVarstore = TRUE;
1887 goto Done;
1888 }
1889 break;
1890
1891 case EFI_IFR_VARSTORE_EFI_OP:
1892 IfrEfiVarStore = (EFI_IFR_VARSTORE_EFI *) IfrOpHdr;
1893 VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrEfiVarStore->Name) * sizeof (CHAR16));
1894 if (VarStoreName == NULL) {
1895 goto Done;
1896 }
1897 AsciiStrToUnicodeStr ((CHAR8 *)IfrEfiVarStore->Name, VarStoreName);
1898
1899 if (IsThisVarstore (&IfrEfiVarStore->Guid, VarStoreName, ConfigHdr)) {
1900 FindVarstore = TRUE;
1901 goto Done;
1902 }
1903 break;
1904
1905 case EFI_IFR_VARSTORE_NAME_VALUE_OP:
1906 IfrNameValueVarStore = (EFI_IFR_VARSTORE_NAME_VALUE *) IfrOpHdr;
1907
1908 if (IsThisVarstore (&IfrNameValueVarStore->Guid, NULL, ConfigHdr)) {
1909 FindVarstore = TRUE;
1910 goto Done;
1911 }
1912 break;
1913
1914 case EFI_IFR_FORM_OP:
1915 case EFI_IFR_FORM_MAP_OP:
1916 //
1917 // No matched varstore is found and directly return.
1918 //
1919 goto Done;
1920
1921 default:
1922 break;
1923 }
1924 }
1925 Done:
1926 if (HiiFormPackage != NULL) {
1927 FreePool (HiiFormPackage);
1928 }
1929
1930 if (VarStoreName != NULL) {
1931 FreePool (VarStoreName);
1932 }
1933
1934 return FindVarstore;
1935 }
1936
1937 /**
1938 Check whether the this op code is required.
1939
1940 @param RequestBlockArray The array includes all the request info or NULL.
1941 @param HiiHandle The hii handle for this form package.
1942 @param VarStorageData The varstore data strucure.
1943 @param IfrOpHdr Ifr opcode header for this opcode.
1944 @param VarWidth The buffer width for this opcode.
1945 @param ReturnData The data block added for this opcode.
1946
1947 @retval EFI_SUCCESS This opcode is required.
1948 @retval EFI_NOT_FOUND This opcode is not required.
1949 @retval Others Contain some error.
1950
1951 **/
1952 EFI_STATUS
1953 IsThisOpcodeRequired (
1954 IN IFR_BLOCK_DATA *RequestBlockArray,
1955 IN EFI_HII_HANDLE HiiHandle,
1956 IN OUT IFR_VARSTORAGE_DATA *VarStorageData,
1957 IN EFI_IFR_OP_HEADER *IfrOpHdr,
1958 IN UINT16 VarWidth,
1959 OUT IFR_BLOCK_DATA **ReturnData
1960 )
1961 {
1962 IFR_BLOCK_DATA *BlockData;
1963 UINT16 VarOffset;
1964 EFI_STRING_ID NameId;
1965 EFI_IFR_QUESTION_HEADER *IfrQuestionHdr;
1966
1967 NameId = 0;
1968 VarOffset = 0;
1969 IfrQuestionHdr = (EFI_IFR_QUESTION_HEADER *)((CHAR8 *) IfrOpHdr + sizeof (EFI_IFR_OP_HEADER));
1970
1971 if (VarStorageData->Type == EFI_HII_VARSTORE_NAME_VALUE) {
1972 NameId = IfrQuestionHdr->VarStoreInfo.VarName;
1973
1974 //
1975 // Check whether this question is in requested block array.
1976 //
1977 if (!BlockArrayCheck (RequestBlockArray, NameId, 0, TRUE, HiiHandle)) {
1978 //
1979 // This question is not in the requested string. Skip it.
1980 //
1981 return EFI_NOT_FOUND;
1982 }
1983 } else {
1984 VarOffset = IfrQuestionHdr->VarStoreInfo.VarOffset;
1985
1986 //
1987 // Check whether this question is in requested block array.
1988 //
1989 if (!BlockArrayCheck (RequestBlockArray, VarOffset, VarWidth, FALSE, HiiHandle)) {
1990 //
1991 // This question is not in the requested string. Skip it.
1992 //
1993 return EFI_NOT_FOUND;
1994 }
1995
1996 //
1997 // Check this var question is in the var storage
1998 //
1999 if (((VarOffset + VarWidth) > VarStorageData->Size)) {
2000 return EFI_INVALID_PARAMETER;
2001 }
2002 }
2003
2004 BlockData = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
2005 if (BlockData == NULL) {
2006 return EFI_OUT_OF_RESOURCES;
2007 }
2008
2009 if (VarStorageData->Type == EFI_HII_VARSTORE_NAME_VALUE) {
2010 BlockData->Name = InternalGetString(HiiHandle, NameId);
2011 } else {
2012 BlockData->Offset = VarOffset;
2013 }
2014
2015 BlockData->Width = VarWidth;
2016 BlockData->QuestionId = IfrQuestionHdr->QuestionId;
2017 BlockData->OpCode = IfrOpHdr->OpCode;
2018 BlockData->Scope = IfrOpHdr->Scope;
2019 InitializeListHead (&BlockData->DefaultValueEntry);
2020 //
2021 // Add Block Data into VarStorageData BlockEntry
2022 //
2023 InsertBlockData (&VarStorageData->BlockEntry, &BlockData);
2024 *ReturnData = BlockData;
2025
2026 return EFI_SUCCESS;
2027 }
2028
2029 /**
2030 This function parses Form Package to get the block array and the default
2031 value array according to the request ConfigHdr.
2032
2033 @param HiiHandle Hii Handle for this hii package.
2034 @param Package Pointer to the form package data.
2035 @param PackageLength Length of the pacakge.
2036 @param ConfigHdr Request string ConfigHdr. If it is NULL,
2037 the first found varstore will be as ConfigHdr.
2038 @param RequestBlockArray The block array is retrieved from the request string.
2039 @param VarStorageData VarStorage structure contains the got block and default value.
2040 @param DefaultIdArray Point to the got default id and default name array.
2041
2042 @retval EFI_SUCCESS The block array and the default value array are got.
2043 @retval EFI_INVALID_PARAMETER The varstore defintion in the differnt form pacakges
2044 are conflicted.
2045 @retval EFI_OUT_OF_RESOURCES No enough memory.
2046 **/
2047 EFI_STATUS
2048 EFIAPI
2049 ParseIfrData (
2050 IN EFI_HII_HANDLE HiiHandle,
2051 IN UINT8 *Package,
2052 IN UINT32 PackageLength,
2053 IN EFI_STRING ConfigHdr,
2054 IN IFR_BLOCK_DATA *RequestBlockArray,
2055 IN OUT IFR_VARSTORAGE_DATA *VarStorageData,
2056 OUT IFR_DEFAULT_DATA *DefaultIdArray
2057 )
2058 {
2059 EFI_STATUS Status;
2060 UINTN IfrOffset;
2061 UINTN PackageOffset;
2062 EFI_IFR_VARSTORE *IfrVarStore;
2063 EFI_IFR_VARSTORE_EFI *IfrEfiVarStore;
2064 EFI_IFR_OP_HEADER *IfrOpHdr;
2065 EFI_IFR_ONE_OF *IfrOneOf;
2066 EFI_IFR_REF4 *IfrRef;
2067 EFI_IFR_ONE_OF_OPTION *IfrOneOfOption;
2068 EFI_IFR_DEFAULT *IfrDefault;
2069 EFI_IFR_ORDERED_LIST *IfrOrderedList;
2070 EFI_IFR_CHECKBOX *IfrCheckBox;
2071 EFI_IFR_PASSWORD *IfrPassword;
2072 EFI_IFR_STRING *IfrString;
2073 EFI_IFR_DATE *IfrDate;
2074 EFI_IFR_TIME *IfrTime;
2075 IFR_DEFAULT_DATA DefaultData;
2076 IFR_DEFAULT_DATA *DefaultDataPtr;
2077 IFR_BLOCK_DATA *BlockData;
2078 CHAR16 *VarStoreName;
2079 UINT16 VarWidth;
2080 UINT16 VarDefaultId;
2081 BOOLEAN FirstOneOfOption;
2082 BOOLEAN FirstOrderedList;
2083 LIST_ENTRY *LinkData;
2084 LIST_ENTRY *LinkDefault;
2085 EFI_IFR_VARSTORE_NAME_VALUE *IfrNameValueVarStore;
2086 EFI_HII_PACKAGE_HEADER *PackageHeader;
2087 EFI_VARSTORE_ID VarStoreId;
2088
2089 Status = EFI_SUCCESS;
2090 BlockData = NULL;
2091 DefaultDataPtr = NULL;
2092 FirstOneOfOption = FALSE;
2093 VarStoreId = 0;
2094 FirstOrderedList = FALSE;
2095 ZeroMem (&DefaultData, sizeof (IFR_DEFAULT_DATA));
2096
2097 //
2098 // Go through the form package to parse OpCode one by one.
2099 //
2100 PackageOffset = sizeof (EFI_HII_PACKAGE_HEADER);
2101 PackageHeader = (EFI_HII_PACKAGE_HEADER *) Package;
2102 IfrOffset = PackageOffset;
2103 while (IfrOffset < PackageLength) {
2104
2105 //
2106 // More than one form package found.
2107 //
2108 if (PackageOffset >= PackageHeader->Length) {
2109 //
2110 // Already found varstore for this request, break;
2111 //
2112 if (VarStoreId != 0) {
2113 VarStoreId = 0;
2114 }
2115
2116 //
2117 // Get next package header info.
2118 //
2119 IfrOffset += sizeof (EFI_HII_PACKAGE_HEADER);
2120 PackageOffset = sizeof (EFI_HII_PACKAGE_HEADER);
2121 PackageHeader = (EFI_HII_PACKAGE_HEADER *) (Package + IfrOffset);
2122 }
2123
2124 IfrOpHdr = (EFI_IFR_OP_HEADER *) (Package + IfrOffset);
2125 switch (IfrOpHdr->OpCode) {
2126 case EFI_IFR_VARSTORE_OP:
2127 //
2128 // VarStore is found. Don't need to search any more.
2129 //
2130 if (VarStoreId != 0) {
2131 break;
2132 }
2133
2134 IfrVarStore = (EFI_IFR_VARSTORE *) IfrOpHdr;
2135
2136 VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrVarStore->Name) * sizeof (CHAR16));
2137 if (VarStoreName == NULL) {
2138 Status = EFI_OUT_OF_RESOURCES;
2139 goto Done;
2140 }
2141 AsciiStrToUnicodeStr ((CHAR8 *)IfrVarStore->Name, VarStoreName);
2142
2143 if (IsThisVarstore((VOID *)&IfrVarStore->Guid, VarStoreName, ConfigHdr)) {
2144 //
2145 // Find the matched VarStore
2146 //
2147 CopyGuid (&VarStorageData->Guid, (EFI_GUID *) (VOID *) &IfrVarStore->Guid);
2148 VarStorageData->Size = IfrVarStore->Size;
2149 VarStorageData->Name = VarStoreName;
2150 VarStorageData->Type = EFI_HII_VARSTORE_BUFFER;
2151 VarStoreId = IfrVarStore->VarStoreId;
2152 }
2153 break;
2154
2155 case EFI_IFR_VARSTORE_EFI_OP:
2156 //
2157 // VarStore is found. Don't need to search any more.
2158 //
2159 if (VarStoreId != 0) {
2160 break;
2161 }
2162
2163 IfrEfiVarStore = (EFI_IFR_VARSTORE_EFI *) IfrOpHdr;
2164
2165 //
2166 // If the length is small than the structure, this is from old efi
2167 // varstore definition. Old efi varstore get config directly from
2168 // GetVariable function.
2169 //
2170 if (IfrOpHdr->Length < sizeof (EFI_IFR_VARSTORE_EFI)) {
2171 break;
2172 }
2173
2174 VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)IfrEfiVarStore->Name) * sizeof (CHAR16));
2175 if (VarStoreName == NULL) {
2176 Status = EFI_OUT_OF_RESOURCES;
2177 goto Done;
2178 }
2179 AsciiStrToUnicodeStr ((CHAR8 *)IfrEfiVarStore->Name, VarStoreName);
2180
2181 if (IsThisVarstore (&IfrEfiVarStore->Guid, VarStoreName, ConfigHdr)) {
2182 //
2183 // Find the matched VarStore
2184 //
2185 CopyGuid (&VarStorageData->Guid, (EFI_GUID *) (VOID *) &IfrEfiVarStore->Guid);
2186 VarStorageData->Size = IfrEfiVarStore->Size;
2187 VarStorageData->Name = VarStoreName;
2188 VarStorageData->Type = EFI_HII_VARSTORE_EFI_VARIABLE_BUFFER;
2189 VarStoreId = IfrEfiVarStore->VarStoreId;
2190 }
2191 break;
2192
2193 case EFI_IFR_VARSTORE_NAME_VALUE_OP:
2194 //
2195 // VarStore is found. Don't need to search any more.
2196 //
2197 if (VarStoreId != 0) {
2198 break;
2199 }
2200
2201 IfrNameValueVarStore = (EFI_IFR_VARSTORE_NAME_VALUE *) IfrOpHdr;
2202
2203 if (IsThisVarstore (&IfrNameValueVarStore->Guid, NULL, ConfigHdr)) {
2204 //
2205 // Find the matched VarStore
2206 //
2207 CopyGuid (&VarStorageData->Guid, (EFI_GUID *) (VOID *) &IfrNameValueVarStore->Guid);
2208 VarStorageData->Type = EFI_HII_VARSTORE_NAME_VALUE;
2209 VarStoreId = IfrNameValueVarStore->VarStoreId;
2210 }
2211 break;
2212
2213 case EFI_IFR_DEFAULTSTORE_OP:
2214 //
2215 // Add new the map between default id and default name.
2216 //
2217 DefaultDataPtr = (IFR_DEFAULT_DATA *) AllocateZeroPool (sizeof (IFR_DEFAULT_DATA));
2218 if (DefaultDataPtr == NULL) {
2219 Status = EFI_OUT_OF_RESOURCES;
2220 goto Done;
2221 }
2222 DefaultDataPtr->DefaultId = ((EFI_IFR_DEFAULTSTORE *) IfrOpHdr)->DefaultId;
2223 InsertTailList (&DefaultIdArray->Entry, &DefaultDataPtr->Entry);
2224 DefaultDataPtr = NULL;
2225 break;
2226
2227 case EFI_IFR_FORM_OP:
2228 case EFI_IFR_FORM_MAP_OP:
2229 //
2230 // No matched varstore is found and directly return.
2231 //
2232 if ( VarStoreId == 0) {
2233 Status = EFI_SUCCESS;
2234 goto Done;
2235 }
2236 break;
2237
2238 case EFI_IFR_REF_OP:
2239 //
2240 // Ref question is not in IFR Form. This IFR form is not valid.
2241 //
2242 if ( VarStoreId == 0) {
2243 Status = EFI_INVALID_PARAMETER;
2244 goto Done;
2245 }
2246 //
2247 // Check whether this question is for the requested varstore.
2248 //
2249 IfrRef = (EFI_IFR_REF4 *) IfrOpHdr;
2250 if (IfrRef->Question.VarStoreId != VarStoreId) {
2251 break;
2252 }
2253 VarWidth = (UINT16) (sizeof (EFI_HII_REF));
2254
2255 //
2256 // The BlockData may allocate by other opcode,need to clean.
2257 //
2258 if (BlockData != NULL){
2259 BlockData = NULL;
2260 }
2261
2262 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
2263 if (EFI_ERROR (Status)) {
2264 if (Status == EFI_NOT_FOUND){
2265 //
2266 //The opcode is not required,exit and parse other opcode.
2267 //
2268 break;
2269 }
2270 goto Done;
2271 }
2272 break;
2273
2274 case EFI_IFR_ONE_OF_OP:
2275 case EFI_IFR_NUMERIC_OP:
2276 //
2277 // Numeric and OneOf has the same opcode structure.
2278 //
2279
2280 //
2281 // Numeric and OneOf question is not in IFR Form. This IFR form is not valid.
2282 //
2283 if (VarStoreId == 0) {
2284 Status = EFI_INVALID_PARAMETER;
2285 goto Done;
2286 }
2287 //
2288 // Check whether this question is for the requested varstore.
2289 //
2290 IfrOneOf = (EFI_IFR_ONE_OF *) IfrOpHdr;
2291 if (IfrOneOf->Question.VarStoreId != VarStoreId) {
2292 break;
2293 }
2294 VarWidth = (UINT16) (1 << (IfrOneOf->Flags & EFI_IFR_NUMERIC_SIZE));
2295
2296 //
2297 // The BlockData may allocate by other opcode,need to clean.
2298 //
2299 if (BlockData != NULL){
2300 BlockData = NULL;
2301 }
2302
2303 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
2304 if (EFI_ERROR (Status)) {
2305 if (Status == EFI_NOT_FOUND){
2306 //
2307 //The opcode is not required,exit and parse other opcode.
2308 //
2309 break;
2310 }
2311 goto Done;
2312 }
2313
2314 //
2315 //when go to there,BlockData can't be NULLL.
2316 //
2317 ASSERT (BlockData != NULL);
2318
2319 if (IfrOpHdr->OpCode == EFI_IFR_ONE_OF_OP) {
2320 //
2321 // Set this flag to TRUE for the first oneof option.
2322 //
2323 FirstOneOfOption = TRUE;
2324 } else if (IfrOpHdr->OpCode == EFI_IFR_NUMERIC_OP) {
2325 //
2326 // Numeric minimum value will be used as default value when no default is specified.
2327 //
2328 DefaultData.Type = DefaultValueFromDefault;
2329 switch (IfrOneOf->Flags & EFI_IFR_NUMERIC_SIZE) {
2330 case EFI_IFR_NUMERIC_SIZE_1:
2331 DefaultData.Value.u8 = IfrOneOf->data.u8.MinValue;
2332 break;
2333
2334 case EFI_IFR_NUMERIC_SIZE_2:
2335 CopyMem (&DefaultData.Value.u16, &IfrOneOf->data.u16.MinValue, sizeof (UINT16));
2336 break;
2337
2338 case EFI_IFR_NUMERIC_SIZE_4:
2339 CopyMem (&DefaultData.Value.u32, &IfrOneOf->data.u32.MinValue, sizeof (UINT32));
2340 break;
2341
2342 case EFI_IFR_NUMERIC_SIZE_8:
2343 CopyMem (&DefaultData.Value.u64, &IfrOneOf->data.u64.MinValue, sizeof (UINT64));
2344 break;
2345
2346 default:
2347 Status = EFI_INVALID_PARAMETER;
2348 goto Done;
2349 }
2350 //
2351 // Set default value base on the DefaultId list get from IFR data.
2352 //
2353 for (LinkData = DefaultIdArray->Entry.ForwardLink; LinkData != &DefaultIdArray->Entry; LinkData = LinkData->ForwardLink) {
2354 DefaultDataPtr = BASE_CR (LinkData, IFR_DEFAULT_DATA, Entry);
2355 DefaultData.DefaultId = DefaultDataPtr->DefaultId;
2356 InsertDefaultValue (BlockData, &DefaultData);
2357 }
2358 }
2359 break;
2360
2361 case EFI_IFR_ORDERED_LIST_OP:
2362 //
2363 // offset by question header
2364 // width by EFI_IFR_ORDERED_LIST MaxContainers * OneofOption Type
2365 //
2366
2367 FirstOrderedList = TRUE;
2368 //
2369 // OrderedList question is not in IFR Form. This IFR form is not valid.
2370 //
2371 if (VarStoreId == 0) {
2372 Status = EFI_INVALID_PARAMETER;
2373 goto Done;
2374 }
2375 //
2376 // Check whether this question is for the requested varstore.
2377 //
2378 IfrOrderedList = (EFI_IFR_ORDERED_LIST *) IfrOpHdr;
2379 if (IfrOrderedList->Question.VarStoreId != VarStoreId) {
2380 BlockData = NULL;
2381 break;
2382 }
2383 VarWidth = IfrOrderedList->MaxContainers;
2384
2385 //
2386 // The BlockData may allocate by other opcode,need to clean.
2387 //
2388 if (BlockData != NULL){
2389 BlockData = NULL;
2390 }
2391
2392 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
2393 if (EFI_ERROR (Status)) {
2394 if (Status == EFI_NOT_FOUND){
2395 //
2396 //The opcode is not required,exit and parse other opcode.
2397 //
2398 break;
2399 }
2400 goto Done;
2401 }
2402 break;
2403
2404 case EFI_IFR_CHECKBOX_OP:
2405 //
2406 // EFI_IFR_DEFAULT_OP
2407 // offset by question header
2408 // width is 1 sizeof (BOOLEAN)
2409 // default id by CheckBox Flags if CheckBox flags (Default or Mau) is set, the default value is 1 to be set.
2410 // value by DefaultOption
2411 // default id by DeaultOption DefaultId can override CheckBox Flags and Default value.
2412 //
2413
2414 //
2415 // CheckBox question is not in IFR Form. This IFR form is not valid.
2416 //
2417 if (VarStoreId == 0) {
2418 Status = EFI_INVALID_PARAMETER;
2419 goto Done;
2420 }
2421 //
2422 // Check whether this question is for the requested varstore.
2423 //
2424 IfrCheckBox = (EFI_IFR_CHECKBOX *) IfrOpHdr;
2425 if (IfrCheckBox->Question.VarStoreId != VarStoreId) {
2426 break;
2427 }
2428 VarWidth = (UINT16) sizeof (BOOLEAN);
2429
2430 //
2431 // The BlockData may allocate by other opcode,need to clean.
2432 //
2433 if (BlockData != NULL){
2434 BlockData = NULL;
2435 }
2436
2437 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
2438 if (EFI_ERROR (Status)) {
2439 if (Status == EFI_NOT_FOUND){
2440 //
2441 //The opcode is not required,exit and parse other opcode.
2442 //
2443 break;
2444 }
2445 goto Done;
2446 }
2447
2448 //
2449 //when go to there,BlockData can't be NULLL.
2450 //
2451 ASSERT (BlockData != NULL);
2452
2453 //
2454 // Add default value for standard ID by CheckBox Flag
2455 //
2456 VarDefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
2457 //
2458 // Prepare new DefaultValue
2459 //
2460 DefaultData.DefaultId = VarDefaultId;
2461 if ((IfrCheckBox->Flags & EFI_IFR_CHECKBOX_DEFAULT) == EFI_IFR_CHECKBOX_DEFAULT) {
2462 //
2463 // When flag is set, defautl value is TRUE.
2464 //
2465 DefaultData.Type = DefaultValueFromFlag;
2466 DefaultData.Value.b = TRUE;
2467 } else {
2468 //
2469 // When flag is not set, defautl value is FASLE.
2470 //
2471 DefaultData.Type = DefaultValueFromDefault;
2472 DefaultData.Value.b = FALSE;
2473 }
2474 //
2475 // Add DefaultValue into current BlockData
2476 //
2477 InsertDefaultValue (BlockData, &DefaultData);
2478
2479 //
2480 // Add default value for Manufacture ID by CheckBox Flag
2481 //
2482 VarDefaultId = EFI_HII_DEFAULT_CLASS_MANUFACTURING;
2483 //
2484 // Prepare new DefaultValue
2485 //
2486 DefaultData.DefaultId = VarDefaultId;
2487 if ((IfrCheckBox->Flags & EFI_IFR_CHECKBOX_DEFAULT_MFG) == EFI_IFR_CHECKBOX_DEFAULT_MFG) {
2488 //
2489 // When flag is set, defautl value is TRUE.
2490 //
2491 DefaultData.Type = DefaultValueFromFlag;
2492 DefaultData.Value.b = TRUE;
2493 } else {
2494 //
2495 // When flag is not set, defautl value is FASLE.
2496 //
2497 DefaultData.Type = DefaultValueFromDefault;
2498 DefaultData.Value.b = FALSE;
2499 }
2500 //
2501 // Add DefaultValue into current BlockData
2502 //
2503 InsertDefaultValue (BlockData, &DefaultData);
2504 break;
2505
2506 case EFI_IFR_DATE_OP:
2507 //
2508 // offset by question header
2509 // width MaxSize * sizeof (CHAR16)
2510 // no default value, only block array
2511 //
2512
2513 //
2514 // Date question is not in IFR Form. This IFR form is not valid.
2515 //
2516 if (VarStoreId == 0) {
2517 Status = EFI_INVALID_PARAMETER;
2518 goto Done;
2519 }
2520 //
2521 // Check whether this question is for the requested varstore.
2522 //
2523 IfrDate = (EFI_IFR_DATE *) IfrOpHdr;
2524 if (IfrDate->Question.VarStoreId != VarStoreId) {
2525 break;
2526 }
2527
2528 //
2529 // The BlockData may allocate by other opcode,need to clean.
2530 //
2531 if (BlockData != NULL){
2532 BlockData = NULL;
2533 }
2534
2535 VarWidth = (UINT16) sizeof (EFI_HII_DATE);
2536 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
2537 if (EFI_ERROR (Status)) {
2538 if (Status == EFI_NOT_FOUND){
2539 //
2540 //The opcode is not required,exit and parse other opcode.
2541 //
2542 break;
2543 }
2544 goto Done;
2545 }
2546 break;
2547
2548 case EFI_IFR_TIME_OP:
2549 //
2550 // offset by question header
2551 // width MaxSize * sizeof (CHAR16)
2552 // no default value, only block array
2553 //
2554
2555 //
2556 // Time question is not in IFR Form. This IFR form is not valid.
2557 //
2558 if (VarStoreId == 0) {
2559 Status = EFI_INVALID_PARAMETER;
2560 goto Done;
2561 }
2562 //
2563 // Check whether this question is for the requested varstore.
2564 //
2565 IfrTime = (EFI_IFR_TIME *) IfrOpHdr;
2566 if (IfrTime->Question.VarStoreId != VarStoreId) {
2567 break;
2568 }
2569
2570 //
2571 // The BlockData may allocate by other opcode,need to clean.
2572 //
2573 if (BlockData != NULL){
2574 BlockData = NULL;
2575 }
2576
2577 VarWidth = (UINT16) sizeof (EFI_HII_TIME);
2578 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
2579 if (EFI_ERROR (Status)) {
2580 if (Status == EFI_NOT_FOUND){
2581 //
2582 //The opcode is not required,exit and parse other opcode.
2583 //
2584 break;
2585 }
2586 goto Done;
2587 }
2588 break;
2589
2590 case EFI_IFR_STRING_OP:
2591 //
2592 // offset by question header
2593 // width MaxSize * sizeof (CHAR16)
2594 // no default value, only block array
2595 //
2596
2597 //
2598 // String question is not in IFR Form. This IFR form is not valid.
2599 //
2600 if (VarStoreId == 0) {
2601 Status = EFI_INVALID_PARAMETER;
2602 goto Done;
2603 }
2604 //
2605 // Check whether this question is for the requested varstore.
2606 //
2607 IfrString = (EFI_IFR_STRING *) IfrOpHdr;
2608 if (IfrString->Question.VarStoreId != VarStoreId) {
2609 break;
2610 }
2611
2612 //
2613 // The BlockData may allocate by other opcode,need to clean.
2614 //
2615 if (BlockData != NULL){
2616 BlockData = NULL;
2617 }
2618
2619 VarWidth = (UINT16) (IfrString->MaxSize * sizeof (UINT16));
2620 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
2621 if (EFI_ERROR (Status)) {
2622 if (Status == EFI_NOT_FOUND){
2623 //
2624 //The opcode is not required,exit and parse other opcode.
2625 //
2626 break;
2627 }
2628 goto Done;
2629 }
2630 break;
2631
2632 case EFI_IFR_PASSWORD_OP:
2633 //
2634 // offset by question header
2635 // width MaxSize * sizeof (CHAR16)
2636 // no default value, only block array
2637 //
2638
2639 //
2640 // Password question is not in IFR Form. This IFR form is not valid.
2641 //
2642 if (VarStoreId == 0) {
2643 Status = EFI_INVALID_PARAMETER;
2644 goto Done;
2645 }
2646 //
2647 // Check whether this question is for the requested varstore.
2648 //
2649 IfrPassword = (EFI_IFR_PASSWORD *) IfrOpHdr;
2650 if (IfrPassword->Question.VarStoreId != VarStoreId) {
2651 break;
2652 }
2653
2654 //
2655 // The BlockData may allocate by other opcode,need to clean.
2656 //
2657 if (BlockData != NULL){
2658 BlockData = NULL;
2659 }
2660
2661 VarWidth = (UINT16) (IfrPassword->MaxSize * sizeof (UINT16));
2662 Status = IsThisOpcodeRequired(RequestBlockArray, HiiHandle, VarStorageData, IfrOpHdr, VarWidth, &BlockData);
2663 if (EFI_ERROR (Status)) {
2664 if (Status == EFI_NOT_FOUND){
2665 //
2666 //The opcode is not required,exit and parse other opcode.
2667 //
2668 break;
2669 }
2670 goto Done;
2671 }
2672
2673 //
2674 // No default value for string.
2675 //
2676 BlockData = NULL;
2677 break;
2678
2679 case EFI_IFR_ONE_OF_OPTION_OP:
2680 //
2681 // No matched block data is ignored.
2682 //
2683 if (BlockData == NULL || BlockData->Scope == 0) {
2684 break;
2685 }
2686
2687 IfrOneOfOption = (EFI_IFR_ONE_OF_OPTION *) IfrOpHdr;
2688 if (BlockData->OpCode == EFI_IFR_ORDERED_LIST_OP) {
2689
2690 if (!FirstOrderedList){
2691 break;
2692 }
2693 //
2694 // Get ordered list option data type.
2695 //
2696 if (IfrOneOfOption->Type == EFI_IFR_TYPE_NUM_SIZE_8 || IfrOneOfOption->Type == EFI_IFR_TYPE_BOOLEAN) {
2697 VarWidth = 1;
2698 } else if (IfrOneOfOption->Type == EFI_IFR_TYPE_NUM_SIZE_16) {
2699 VarWidth = 2;
2700 } else if (IfrOneOfOption->Type == EFI_IFR_TYPE_NUM_SIZE_32) {
2701 VarWidth = 4;
2702 } else if (IfrOneOfOption->Type == EFI_IFR_TYPE_NUM_SIZE_64) {
2703 VarWidth = 8;
2704 } else {
2705 //
2706 // Invalid ordered list option data type.
2707 //
2708 Status = EFI_INVALID_PARAMETER;
2709 if (BlockData->Name != NULL) {
2710 FreePool (BlockData->Name);
2711 }
2712 FreePool (BlockData);
2713 goto Done;
2714 }
2715
2716 //
2717 // Calculate Ordered list QuestionId width.
2718 //
2719 BlockData->Width = (UINT16) (BlockData->Width * VarWidth);
2720 //
2721 // Check whether this question is in requested block array.
2722 //
2723 if (!BlockArrayCheck (RequestBlockArray, BlockData->Offset, BlockData->Width, (BOOLEAN)(BlockData->Name != NULL), HiiHandle)) {
2724 //
2725 // This question is not in the requested string. Skip it.
2726 //
2727 if (BlockData->Name != NULL) {
2728 FreePool (BlockData->Name);
2729 }
2730 FreePool (BlockData);
2731 BlockData = NULL;
2732 break;
2733 }
2734 //
2735 // Check this var question is in the var storage
2736 //
2737 if ((BlockData->Name == NULL) && ((BlockData->Offset + BlockData->Width) > VarStorageData->Size)) {
2738 Status = EFI_INVALID_PARAMETER;
2739 if (BlockData->Name != NULL) {
2740 FreePool (BlockData->Name);
2741 }
2742 FreePool (BlockData);
2743 goto Done;
2744 }
2745 //
2746 // Add Block Data into VarStorageData BlockEntry
2747 //
2748 InsertBlockData (&VarStorageData->BlockEntry, &BlockData);
2749
2750 FirstOrderedList = FALSE;
2751
2752 break;
2753 }
2754
2755 //
2756 // 1. Set default value for OneOf option when flag field has default attribute.
2757 //
2758 if (((IfrOneOfOption->Flags & EFI_IFR_OPTION_DEFAULT) == EFI_IFR_OPTION_DEFAULT) ||
2759 ((IfrOneOfOption->Flags & EFI_IFR_OPTION_DEFAULT_MFG) == EFI_IFR_OPTION_DEFAULT_MFG)) {
2760 //
2761 // This flag is used to specify whether this option is the first. Set it to FALSE for the following options.
2762 // The first oneof option value will be used as default value when no default value is specified.
2763 //
2764 FirstOneOfOption = FALSE;
2765
2766 // Prepare new DefaultValue
2767 //
2768 DefaultData.Type = DefaultValueFromFlag;
2769 CopyMem (&DefaultData.Value, &IfrOneOfOption->Value, IfrOneOfOption->Header.Length - OFFSET_OF (EFI_IFR_ONE_OF_OPTION, Value));
2770 if ((IfrOneOfOption->Flags & EFI_IFR_OPTION_DEFAULT) == EFI_IFR_OPTION_DEFAULT) {
2771 DefaultData.DefaultId = EFI_HII_DEFAULT_CLASS_STANDARD;
2772 InsertDefaultValue (BlockData, &DefaultData);
2773 }
2774 if ((IfrOneOfOption->Flags & EFI_IFR_OPTION_DEFAULT_MFG) == EFI_IFR_OPTION_DEFAULT_MFG) {
2775 DefaultData.DefaultId = EFI_HII_DEFAULT_CLASS_MANUFACTURING;
2776 InsertDefaultValue (BlockData, &DefaultData);
2777 }
2778 }
2779
2780 //
2781 // 2. Set as the default value when this is the first option.
2782 // The first oneof option value will be used as default value when no default value is specified.
2783 //
2784 if (FirstOneOfOption) {
2785 // This flag is used to specify whether this option is the first. Set it to FALSE for the following options.
2786 FirstOneOfOption = FALSE;
2787
2788 //
2789 // Prepare new DefaultValue
2790 //
2791 DefaultData.Type = DefaultValueFromDefault;
2792 CopyMem (&DefaultData.Value, &IfrOneOfOption->Value, IfrOneOfOption->Header.Length - OFFSET_OF (EFI_IFR_ONE_OF_OPTION, Value));
2793 for (LinkData = DefaultIdArray->Entry.ForwardLink; LinkData != &DefaultIdArray->Entry; LinkData = LinkData->ForwardLink) {
2794 DefaultDataPtr = BASE_CR (LinkData, IFR_DEFAULT_DATA, Entry);
2795 DefaultData.DefaultId = DefaultDataPtr->DefaultId;
2796 InsertDefaultValue (BlockData, &DefaultData);
2797 }
2798 }
2799 break;
2800
2801 case EFI_IFR_DEFAULT_OP:
2802 //
2803 // Update Current BlockData to the default value.
2804 //
2805 if (BlockData == NULL || BlockData->Scope == 0) {
2806 //
2807 // No matched block data is ignored.
2808 //
2809 break;
2810 }
2811
2812 //
2813 // Get the DefaultId
2814 //
2815 IfrDefault = (EFI_IFR_DEFAULT *) IfrOpHdr;
2816 VarDefaultId = IfrDefault->DefaultId;
2817 //
2818 // Prepare new DefaultValue
2819 //
2820 DefaultData.Type = DefaultValueFromOpcode;
2821 DefaultData.DefaultId = VarDefaultId;
2822 CopyMem (&DefaultData.Value, &IfrDefault->Value, IfrDefault->Header.Length - OFFSET_OF (EFI_IFR_DEFAULT, Value));
2823
2824 // If the value field is expression, set the cleaned flag.
2825 if (IfrDefault->Type == EFI_IFR_TYPE_OTHER) {
2826 DefaultData.Cleaned = TRUE;
2827 }
2828 //
2829 // Add DefaultValue into current BlockData
2830 //
2831 InsertDefaultValue (BlockData, &DefaultData);
2832
2833 //
2834 // After insert the default value, reset the cleaned value for next
2835 // time used. If not set here, need to set the value before everytime
2836 // use it.
2837 //
2838 DefaultData.Cleaned = FALSE;
2839 break;
2840
2841 case EFI_IFR_END_OP:
2842 //
2843 // End Opcode is for Var question.
2844 //
2845 if (BlockData != NULL) {
2846 if (BlockData->Scope > 0) {
2847 BlockData->Scope--;
2848 }
2849 if (BlockData->Scope == 0) {
2850 BlockData = NULL;
2851 }
2852 }
2853
2854 break;
2855
2856 default:
2857 if (BlockData != NULL) {
2858 if (BlockData->Scope > 0) {
2859 BlockData->Scope = (UINT8) (BlockData->Scope + IfrOpHdr->Scope);
2860 }
2861
2862 if (BlockData->Scope == 0) {
2863 BlockData = NULL;
2864 }
2865 }
2866 break;
2867 }
2868
2869 IfrOffset += IfrOpHdr->Length;
2870 PackageOffset += IfrOpHdr->Length;
2871 }
2872
2873 //
2874 //if Status == EFI_NOT_FOUND, just means the opcode is not required,not contain any error,
2875 //so set the Status to EFI_SUCCESS.
2876 //
2877 if (Status == EFI_NOT_FOUND){
2878 Status = EFI_SUCCESS;
2879 }
2880
2881 Done:
2882 for (LinkData = VarStorageData->BlockEntry.ForwardLink; LinkData != &VarStorageData->BlockEntry; LinkData = LinkData->ForwardLink) {
2883 BlockData = BASE_CR (LinkData, IFR_BLOCK_DATA, Entry);
2884 for (LinkDefault = BlockData->DefaultValueEntry.ForwardLink; LinkDefault != &BlockData->DefaultValueEntry; ) {
2885 DefaultDataPtr = BASE_CR (LinkDefault, IFR_DEFAULT_DATA, Entry);
2886 LinkDefault = LinkDefault->ForwardLink;
2887 if (DefaultDataPtr->Cleaned == TRUE) {
2888 RemoveEntryList (&DefaultDataPtr->Entry);
2889 FreePool (DefaultDataPtr);
2890 }
2891 }
2892 }
2893
2894 return Status;
2895 }
2896
2897 /**
2898 parse the configrequest string, get the elements.
2899
2900 @param ConfigRequest The input configrequest string.
2901 @param Progress Return the progress data.
2902
2903 @retval Block data pointer.
2904 **/
2905 IFR_BLOCK_DATA *
2906 GetBlockElement (
2907 IN EFI_STRING ConfigRequest,
2908 OUT EFI_STRING *Progress
2909 )
2910 {
2911 EFI_STRING StringPtr;
2912 IFR_BLOCK_DATA *BlockData;
2913 IFR_BLOCK_DATA *RequestBlockArray;
2914 EFI_STATUS Status;
2915 UINT8 *TmpBuffer;
2916 UINT16 Offset;
2917 UINT16 Width;
2918 LIST_ENTRY *Link;
2919 IFR_BLOCK_DATA *NextBlockData;
2920 UINTN Length;
2921
2922 TmpBuffer = NULL;
2923
2924 //
2925 // Init RequestBlockArray
2926 //
2927 RequestBlockArray = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
2928 if (RequestBlockArray == NULL) {
2929 goto Done;
2930 }
2931 InitializeListHead (&RequestBlockArray->Entry);
2932
2933 //
2934 // Get the request Block array from the request string
2935 // Offset and Width
2936 //
2937
2938 //
2939 // Parse each <RequestElement> if exists
2940 // Only <BlockName> format is supported by this help function.
2941 // <BlockName> ::= &'OFFSET='<Number>&'WIDTH='<Number>
2942 //
2943 StringPtr = ConfigRequest;
2944 while (*StringPtr != 0 && StrnCmp (StringPtr, L"&OFFSET=", StrLen (L"&OFFSET=")) == 0) {
2945 //
2946 // Skip the OFFSET string
2947 //
2948 *Progress = StringPtr;
2949 StringPtr += StrLen (L"&OFFSET=");
2950 //
2951 // Get Offset
2952 //
2953 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
2954 if (EFI_ERROR (Status)) {
2955 goto Done;
2956 }
2957 Offset = 0;
2958 CopyMem (
2959 &Offset,
2960 TmpBuffer,
2961 (((Length + 1) / 2) < sizeof (UINT16)) ? ((Length + 1) / 2) : sizeof (UINT16)
2962 );
2963 FreePool (TmpBuffer);
2964
2965 StringPtr += Length;
2966 if (StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
2967 goto Done;
2968 }
2969 StringPtr += StrLen (L"&WIDTH=");
2970
2971 //
2972 // Get Width
2973 //
2974 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
2975 if (EFI_ERROR (Status)) {
2976 goto Done;
2977 }
2978 Width = 0;
2979 CopyMem (
2980 &Width,
2981 TmpBuffer,
2982 (((Length + 1) / 2) < sizeof (UINT16)) ? ((Length + 1) / 2) : sizeof (UINT16)
2983 );
2984 FreePool (TmpBuffer);
2985
2986 StringPtr += Length;
2987 if (*StringPtr != 0 && *StringPtr != L'&') {
2988 goto Done;
2989 }
2990
2991 //
2992 // Set Block Data
2993 //
2994 BlockData = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
2995 if (BlockData == NULL) {
2996 goto Done;
2997 }
2998 BlockData->Offset = Offset;
2999 BlockData->Width = Width;
3000 InsertBlockData (&RequestBlockArray->Entry, &BlockData);
3001
3002 //
3003 // Skip &VALUE string if &VALUE does exists.
3004 //
3005 if (StrnCmp (StringPtr, L"&VALUE=", StrLen (L"&VALUE=")) == 0) {
3006 StringPtr += StrLen (L"&VALUE=");
3007
3008 //
3009 // Get Value
3010 //
3011 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
3012 if (EFI_ERROR (Status)) {
3013 goto Done;
3014 }
3015
3016 StringPtr += Length;
3017 if (*StringPtr != 0 && *StringPtr != L'&') {
3018 goto Done;
3019 }
3020 }
3021 //
3022 // If '\0', parsing is finished.
3023 //
3024 if (*StringPtr == 0) {
3025 break;
3026 }
3027 }
3028
3029 //
3030 // Merge the requested block data.
3031 //
3032 Link = RequestBlockArray->Entry.ForwardLink;
3033 while ((Link != &RequestBlockArray->Entry) && (Link->ForwardLink != &RequestBlockArray->Entry)) {
3034 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
3035 NextBlockData = BASE_CR (Link->ForwardLink, IFR_BLOCK_DATA, Entry);
3036 if ((NextBlockData->Offset >= BlockData->Offset) && (NextBlockData->Offset <= (BlockData->Offset + BlockData->Width))) {
3037 if ((NextBlockData->Offset + NextBlockData->Width) > (BlockData->Offset + BlockData->Width)) {
3038 BlockData->Width = (UINT16) (NextBlockData->Offset + NextBlockData->Width - BlockData->Offset);
3039 }
3040 RemoveEntryList (Link->ForwardLink);
3041 FreePool (NextBlockData);
3042 continue;
3043 }
3044 Link = Link->ForwardLink;
3045 }
3046
3047 return RequestBlockArray;
3048
3049 Done:
3050 if (RequestBlockArray != NULL) {
3051 //
3052 // Free Link Array RequestBlockArray
3053 //
3054 while (!IsListEmpty (&RequestBlockArray->Entry)) {
3055 BlockData = BASE_CR (RequestBlockArray->Entry.ForwardLink, IFR_BLOCK_DATA, Entry);
3056 RemoveEntryList (&BlockData->Entry);
3057 FreePool (BlockData);
3058 }
3059
3060 FreePool (RequestBlockArray);
3061 }
3062
3063 return NULL;
3064 }
3065
3066 /**
3067 parse the configrequest string, get the elements.
3068
3069 @param ConfigRequest The input config request string.
3070 @param Progress Return the progress data.
3071
3072 @retval return data block array.
3073 **/
3074 IFR_BLOCK_DATA *
3075 GetNameElement (
3076 IN EFI_STRING ConfigRequest,
3077 OUT EFI_STRING *Progress
3078 )
3079 {
3080 EFI_STRING StringPtr;
3081 EFI_STRING NextTag;
3082 IFR_BLOCK_DATA *BlockData;
3083 IFR_BLOCK_DATA *RequestBlockArray;
3084 BOOLEAN HasValue;
3085
3086 StringPtr = ConfigRequest;
3087
3088 //
3089 // Init RequestBlockArray
3090 //
3091 RequestBlockArray = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
3092 if (RequestBlockArray == NULL) {
3093 goto Done;
3094 }
3095 InitializeListHead (&RequestBlockArray->Entry);
3096
3097 //
3098 // Get the request Block array from the request string
3099 //
3100
3101 //
3102 // Parse each <RequestElement> if exists
3103 // Only <BlockName> format is supported by this help function.
3104 // <BlockName> ::= &'Name***=***
3105 //
3106 while (StringPtr != NULL && *StringPtr == L'&') {
3107
3108 *Progress = StringPtr;
3109 //
3110 // Skip the L"&" string
3111 //
3112 StringPtr += 1;
3113
3114 HasValue = FALSE;
3115 if ((NextTag = StrStr (StringPtr, L"=")) != NULL) {
3116 *NextTag = L'\0';
3117 HasValue = TRUE;
3118 } else if ((NextTag = StrStr (StringPtr, L"&")) != NULL) {
3119 *NextTag = L'\0';
3120 }
3121
3122 //
3123 // Set Block Data
3124 //
3125 BlockData = (IFR_BLOCK_DATA *) AllocateZeroPool (sizeof (IFR_BLOCK_DATA));
3126 if (BlockData == NULL) {
3127 goto Done;
3128 }
3129
3130 //
3131 // Get Name
3132 //
3133 BlockData->Name = AllocateCopyPool(StrSize (StringPtr), StringPtr);
3134 InsertBlockData (&RequestBlockArray->Entry, &BlockData);
3135
3136 if (HasValue) {
3137 //
3138 // If has value, skip the value.
3139 //
3140 StringPtr = NextTag + 1;
3141 *NextTag = L'=';
3142 StringPtr = StrStr (StringPtr, L"&");
3143 } else if (NextTag != NULL) {
3144 //
3145 // restore the '&' text.
3146 //
3147 StringPtr = NextTag;
3148 *NextTag = L'&';
3149 }
3150 }
3151
3152 return RequestBlockArray;
3153
3154 Done:
3155 if (RequestBlockArray != NULL) {
3156 //
3157 // Free Link Array RequestBlockArray
3158 //
3159 while (!IsListEmpty (&RequestBlockArray->Entry)) {
3160 BlockData = BASE_CR (RequestBlockArray->Entry.ForwardLink, IFR_BLOCK_DATA, Entry);
3161 RemoveEntryList (&BlockData->Entry);
3162 if (BlockData->Name != NULL) {
3163 FreePool (BlockData->Name);
3164 }
3165 FreePool (BlockData);
3166 }
3167
3168 FreePool (RequestBlockArray);
3169 }
3170
3171 return NULL;
3172 }
3173
3174 /**
3175 Generate ConfigRequest string base on the varstore info.
3176
3177 @param ConfigHdr The config header for this varstore.
3178 @param VarStorageData The varstore info.
3179 @param Status Return Status.
3180 @param ConfigRequest The ConfigRequest info may be return.
3181
3182 @retval TRUE Need to continue
3183 @retval Others NO need to continue or error occur.
3184 **/
3185 BOOLEAN
3186 GenerateConfigRequest (
3187 IN CHAR16 *ConfigHdr,
3188 IN IFR_VARSTORAGE_DATA *VarStorageData,
3189 OUT EFI_STATUS *Status,
3190 IN OUT EFI_STRING *ConfigRequest
3191 )
3192 {
3193 BOOLEAN DataExist;
3194 UINTN Length;
3195 LIST_ENTRY *Link;
3196 CHAR16 *FullConfigRequest;
3197 CHAR16 *StringPtr;
3198 IFR_BLOCK_DATA *BlockData;
3199
3200 //
3201 // Append VarStorageData BlockEntry into *Request string
3202 // Now support only one varstore in a form package.
3203 //
3204
3205 //
3206 // Go through all VarStorageData Entry and get BlockEntry for each one for the multiple varstore in a single form package
3207 // Then construct them all to return MultiRequest string : ConfigHdr BlockConfig
3208 //
3209
3210 //
3211 // Compute the length of the entire request starting with <ConfigHdr> and a
3212 // Null-terminator
3213 //
3214 DataExist = FALSE;
3215 Length = StrLen (ConfigHdr) + 1;
3216
3217 for (Link = VarStorageData->BlockEntry.ForwardLink; Link != &VarStorageData->BlockEntry; Link = Link->ForwardLink) {
3218 DataExist = TRUE;
3219 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
3220 if (VarStorageData->Type == EFI_HII_VARSTORE_NAME_VALUE) {
3221 //
3222 // Add <BlockName> length for each Name
3223 //
3224 // <BlockName> ::= &Name1&Name2&...
3225 // |1| StrLen(Name1)
3226 //
3227 Length = Length + (1 + StrLen (BlockData->Name));
3228 } else {
3229 //
3230 // Add <BlockName> length for each Offset/Width pair
3231 //
3232 // <BlockName> ::= &OFFSET=1234&WIDTH=1234
3233 // | 8 | 4 | 7 | 4 |
3234 //
3235 Length = Length + (8 + 4 + 7 + 4);
3236 }
3237 }
3238 //
3239 // No any request block data is found. The request string can't be constructed.
3240 //
3241 if (!DataExist) {
3242 *Status = EFI_SUCCESS;
3243 return FALSE;
3244 }
3245
3246 //
3247 // Allocate buffer for the entire <ConfigRequest>
3248 //
3249 FullConfigRequest = AllocateZeroPool (Length * sizeof (CHAR16));
3250 if (FullConfigRequest == NULL) {
3251 *Status = EFI_OUT_OF_RESOURCES;
3252 return FALSE;
3253 }
3254 StringPtr = FullConfigRequest;
3255
3256 //
3257 // Start with <ConfigHdr>
3258 //
3259 StrCpyS (StringPtr, Length, ConfigHdr);
3260 StringPtr += StrLen (StringPtr);
3261
3262 //
3263 // Loop through all the Offset/Width pairs and append them to ConfigRequest
3264 //
3265 for (Link = VarStorageData->BlockEntry.ForwardLink; Link != &VarStorageData->BlockEntry; Link = Link->ForwardLink) {
3266 BlockData = BASE_CR (Link, IFR_BLOCK_DATA, Entry);
3267 if (VarStorageData->Type == EFI_HII_VARSTORE_NAME_VALUE) {
3268 //
3269 // Append &Name1\0
3270 //
3271 UnicodeSPrint (
3272 StringPtr,
3273 (1 + StrLen (BlockData->Name) + 1) * sizeof (CHAR16),
3274 L"&%s",
3275 BlockData->Name
3276 );
3277 } else {
3278 //
3279 // Append &OFFSET=XXXX&WIDTH=YYYY\0
3280 //
3281 UnicodeSPrint (
3282 StringPtr,
3283 (8 + 4 + 7 + 4 + 1) * sizeof (CHAR16),
3284 L"&OFFSET=%04X&WIDTH=%04X",
3285 BlockData->Offset,
3286 BlockData->Width
3287 );
3288 }
3289 StringPtr += StrLen (StringPtr);
3290 }
3291 //
3292 // Set to the got full request string.
3293 //
3294 HiiToLower (FullConfigRequest);
3295
3296 if (*ConfigRequest != NULL) {
3297 FreePool (*ConfigRequest);
3298 }
3299 *ConfigRequest = FullConfigRequest;
3300
3301 return TRUE;
3302 }
3303
3304 /**
3305 Generate ConfigRequest Header base on the varstore info.
3306
3307 @param VarStorageData The varstore info.
3308 @param DevicePath Device path for this varstore.
3309 @param ConfigHdr The config header for this varstore.
3310
3311 @retval EFI_SUCCESS Generate the header success.
3312 @retval EFI_OUT_OF_RESOURCES Allocate buffer fail.
3313 **/
3314 EFI_STATUS
3315 GenerateHdr (
3316 IN IFR_VARSTORAGE_DATA *VarStorageData,
3317 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
3318 OUT EFI_STRING *ConfigHdr
3319 )
3320 {
3321 EFI_STRING GuidStr;
3322 EFI_STRING NameStr;
3323 EFI_STRING PathStr;
3324 UINTN Length;
3325 EFI_STATUS Status;
3326
3327 Status = EFI_SUCCESS;
3328 NameStr = NULL;
3329 GuidStr = NULL;
3330 PathStr = NULL;
3331
3332 //
3333 // Construct <ConfigHdr> : "GUID=...&NAME=...&PATH=..." by VarStorageData Guid, Name and DriverHandle
3334 //
3335 GenerateSubStr (L"GUID=", sizeof (EFI_GUID), (VOID *) &VarStorageData->Guid, 1, &GuidStr);
3336 if (VarStorageData->Name != NULL) {
3337 GenerateSubStr (L"NAME=", StrLen (VarStorageData->Name) * sizeof (CHAR16), (VOID *) VarStorageData->Name, 2, &NameStr);
3338 } else {
3339 GenerateSubStr (L"NAME=", 0, NULL, 2, &NameStr);
3340 }
3341 GenerateSubStr (
3342 L"PATH=",
3343 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) DevicePath),
3344 (VOID *) DevicePath,
3345 1,
3346 &PathStr
3347 );
3348 Length = StrLen (GuidStr) + StrLen (NameStr) + StrLen (PathStr) + 1;
3349 if (VarStorageData->Name == NULL) {
3350 Length += 1;
3351 }
3352
3353 *ConfigHdr = AllocateZeroPool (Length * sizeof (CHAR16));
3354 if (*ConfigHdr == NULL) {
3355 Status = EFI_OUT_OF_RESOURCES;
3356 goto Done;
3357 }
3358 StrCpyS (*ConfigHdr, Length, GuidStr);
3359 StrCatS (*ConfigHdr, Length, NameStr);
3360 if (VarStorageData->Name == NULL) {
3361 StrCatS (*ConfigHdr, Length, L"&");
3362 }
3363 StrCatS (*ConfigHdr, Length, PathStr);
3364
3365 //
3366 // Remove the last character L'&'
3367 //
3368 *(*ConfigHdr + StrLen (*ConfigHdr) - 1) = L'\0';
3369
3370 Done:
3371 if (GuidStr != NULL) {
3372 FreePool (GuidStr);
3373 }
3374
3375 if (NameStr != NULL) {
3376 FreePool (NameStr);
3377 }
3378
3379 if (PathStr != NULL) {
3380 FreePool (PathStr);
3381 }
3382
3383 return Status;
3384 }
3385
3386 /**
3387 Get Data buffer size based on data type.
3388
3389 @param ValueType The input data type.
3390
3391 @retval The data buffer size for the input type.
3392 **/
3393 UINT16
3394 GetStorageWidth (
3395 IN UINT8 ValueType
3396 )
3397 {
3398 UINT16 StorageWidth;
3399
3400 switch (ValueType) {
3401 case EFI_IFR_NUMERIC_SIZE_1:
3402 case EFI_IFR_TYPE_BOOLEAN:
3403 StorageWidth = (UINT16) sizeof (UINT8);
3404 break;
3405
3406 case EFI_IFR_NUMERIC_SIZE_2:
3407 StorageWidth = (UINT16) sizeof (UINT16);
3408 break;
3409
3410 case EFI_IFR_NUMERIC_SIZE_4:
3411 StorageWidth = (UINT16) sizeof (UINT32);
3412 break;
3413
3414 case EFI_IFR_NUMERIC_SIZE_8:
3415 StorageWidth = (UINT16) sizeof (UINT64);
3416 break;
3417
3418 case EFI_IFR_TYPE_TIME:
3419 StorageWidth = (UINT16) sizeof (EFI_IFR_TIME);
3420 break;
3421
3422 case EFI_IFR_TYPE_DATE:
3423 StorageWidth = (UINT16) sizeof (EFI_IFR_DATE);
3424 break;
3425
3426 default:
3427 StorageWidth = 0;
3428 break;
3429 }
3430
3431 return StorageWidth;
3432 }
3433
3434 /**
3435 Generate ConfigAltResp string base on the varstore info.
3436
3437 @param HiiHandle Hii Handle for this hii package.
3438 @param ConfigHdr The config header for this varstore.
3439 @param VarStorageData The varstore info.
3440 @param DefaultIdArray The Default id array.
3441 @param DefaultAltCfgResp The DefaultAltCfgResp info may be return.
3442
3443 @retval TRUE Need to continue
3444 @retval Others NO need to continue or error occur.
3445 **/
3446 EFI_STATUS
3447 GenerateAltConfigResp (
3448 IN EFI_HII_HANDLE HiiHandle,
3449 IN CHAR16 *ConfigHdr,
3450 IN IFR_VARSTORAGE_DATA *VarStorageData,
3451 IN IFR_DEFAULT_DATA *DefaultIdArray,
3452 IN OUT EFI_STRING *DefaultAltCfgResp
3453 )
3454 {
3455 BOOLEAN DataExist;
3456 UINTN Length;
3457 LIST_ENTRY *Link;
3458 LIST_ENTRY *LinkData;
3459 LIST_ENTRY *LinkDefault;
3460 LIST_ENTRY *ListEntry;
3461 CHAR16 *StringPtr;
3462 IFR_BLOCK_DATA *BlockData;
3463 IFR_DEFAULT_DATA *DefaultId;
3464 IFR_DEFAULT_DATA *DefaultValueData;
3465 UINTN Width;
3466 UINT8 *TmpBuffer;
3467 CHAR16 *DefaultString;
3468
3469 BlockData = NULL;
3470 DataExist = FALSE;
3471 DefaultString = NULL;
3472 //
3473 // Add length for <ConfigHdr> + '\0'
3474 //
3475 Length = StrLen (ConfigHdr) + 1;
3476
3477 for (Link = DefaultIdArray->Entry.ForwardLink; Link != &DefaultIdArray->Entry; Link = Link->ForwardLink) {
3478 DefaultId = BASE_CR (Link, IFR_DEFAULT_DATA, Entry);
3479 //
3480 // Add length for "&<ConfigHdr>&ALTCFG=XXXX"
3481 // |1| StrLen (ConfigHdr) | 8 | 4 |
3482 //
3483 Length += (1 + StrLen (ConfigHdr) + 8 + 4);
3484
3485 for (LinkData = VarStorageData->BlockEntry.ForwardLink; LinkData != &VarStorageData->BlockEntry; LinkData = LinkData->ForwardLink) {
3486 BlockData = BASE_CR (LinkData, IFR_BLOCK_DATA, Entry);
3487 ListEntry = &BlockData->DefaultValueEntry;
3488 for (LinkDefault = ListEntry->ForwardLink; LinkDefault != ListEntry; LinkDefault = LinkDefault->ForwardLink) {
3489 DefaultValueData = BASE_CR (LinkDefault, IFR_DEFAULT_DATA, Entry);
3490 if (DefaultValueData->DefaultId != DefaultId->DefaultId) {
3491 continue;
3492 }
3493 if (VarStorageData->Type == EFI_HII_VARSTORE_NAME_VALUE) {
3494 //
3495 // Add length for "&Name1=zzzzzzzzzzzz"
3496 // |1|Name|1|Value|
3497 //
3498 Length += (1 + StrLen (BlockData->Name) + 1 + BlockData->Width * 2);
3499 } else {
3500 //
3501 // Add length for "&OFFSET=XXXX&WIDTH=YYYY&VALUE=zzzzzzzzzzzz"
3502 // | 8 | 4 | 7 | 4 | 7 | Width * 2 |
3503 //
3504 Length += (8 + 4 + 7 + 4 + 7 + BlockData->Width * 2);
3505 }
3506 DataExist = TRUE;
3507 }
3508 }
3509 }
3510
3511 //
3512 // No default value is found. The default string doesn't exist.
3513 //
3514 if (!DataExist) {
3515 return EFI_SUCCESS;
3516 }
3517
3518 //
3519 // Allocate buffer for the entire <DefaultAltCfgResp>
3520 //
3521 *DefaultAltCfgResp = AllocateZeroPool (Length * sizeof (CHAR16));
3522 if (*DefaultAltCfgResp == NULL) {
3523 return EFI_OUT_OF_RESOURCES;
3524 }
3525 StringPtr = *DefaultAltCfgResp;
3526
3527 //
3528 // Start with <ConfigHdr>
3529 //
3530 StrCpyS (StringPtr, Length, ConfigHdr);
3531 StringPtr += StrLen (StringPtr);
3532
3533 for (Link = DefaultIdArray->Entry.ForwardLink; Link != &DefaultIdArray->Entry; Link = Link->ForwardLink) {
3534 DefaultId = BASE_CR (Link, IFR_DEFAULT_DATA, Entry);
3535 //
3536 // Add <AltConfigHdr> of the form "&<ConfigHdr>&ALTCFG=XXXX\0"
3537 // |1| StrLen (ConfigHdr) | 8 | 4 |
3538 //
3539 UnicodeSPrint (
3540 StringPtr,
3541 (1 + StrLen (ConfigHdr) + 8 + 4 + 1) * sizeof (CHAR16),
3542 L"&%s&ALTCFG=%04X",
3543 ConfigHdr,
3544 DefaultId->DefaultId
3545 );
3546 StringPtr += StrLen (StringPtr);
3547
3548 for (LinkData = VarStorageData->BlockEntry.ForwardLink; LinkData != &VarStorageData->BlockEntry; LinkData = LinkData->ForwardLink) {
3549 BlockData = BASE_CR (LinkData, IFR_BLOCK_DATA, Entry);
3550 ListEntry = &BlockData->DefaultValueEntry;
3551 for (LinkDefault = ListEntry->ForwardLink; LinkDefault != ListEntry; LinkDefault = LinkDefault->ForwardLink) {
3552 DefaultValueData = BASE_CR (LinkDefault, IFR_DEFAULT_DATA, Entry);
3553 if (DefaultValueData->DefaultId != DefaultId->DefaultId) {
3554 continue;
3555 }
3556 if (VarStorageData->Type == EFI_HII_VARSTORE_NAME_VALUE) {
3557 UnicodeSPrint (
3558 StringPtr,
3559 (1 + StrLen (ConfigHdr) + 1) * sizeof (CHAR16),
3560 L"&%s=",
3561 BlockData->Name
3562 );
3563 StringPtr += StrLen (StringPtr);
3564 } else {
3565 //
3566 // Add <BlockConfig>
3567 // <BlockConfig> ::= 'OFFSET='<Number>&'WIDTH='<Number>&'VALUE'=<Number>
3568 //
3569 UnicodeSPrint (
3570 StringPtr,
3571 (8 + 4 + 7 + 4 + 7 + 1) * sizeof (CHAR16),
3572 L"&OFFSET=%04X&WIDTH=%04X&VALUE=",
3573 BlockData->Offset,
3574 BlockData->Width
3575 );
3576 StringPtr += StrLen (StringPtr);
3577 }
3578 Width = BlockData->Width;
3579 //
3580 // Convert Value to a hex string in "%x" format
3581 // NOTE: This is in the opposite byte that GUID and PATH use
3582 //
3583 if (BlockData->OpCode == EFI_IFR_STRING_OP){
3584 DefaultString = InternalGetString(HiiHandle, DefaultValueData->Value.string);
3585 TmpBuffer = (UINT8 *) DefaultString;
3586 } else {
3587 TmpBuffer = (UINT8 *) &(DefaultValueData->Value);
3588 }
3589 for (; Width > 0 && (TmpBuffer != NULL); Width--) {
3590 StringPtr += UnicodeValueToString (StringPtr, PREFIX_ZERO | RADIX_HEX, TmpBuffer[Width - 1], 2);
3591 }
3592 if (DefaultString != NULL){
3593 FreePool(DefaultString);
3594 DefaultString = NULL;
3595 }
3596 }
3597 }
3598 }
3599
3600 HiiToLower (*DefaultAltCfgResp);
3601
3602 return EFI_SUCCESS;
3603 }
3604
3605 /**
3606 This function gets the full request string and full default value string by
3607 parsing IFR data in HII form packages.
3608
3609 When Request points to NULL string, the request string and default value string
3610 for each varstore in form package will return.
3611
3612 @param DataBaseRecord The DataBaseRecord instance contains the found Hii handle and package.
3613 @param DevicePath Device Path which Hii Config Access Protocol is registered.
3614 @param Request Pointer to a null-terminated Unicode string in
3615 <ConfigRequest> format. When it doesn't contain
3616 any RequestElement, it will be updated to return
3617 the full RequestElement retrieved from IFR data.
3618 If it points to NULL, the request string for the first
3619 varstore in form package will be merged into a
3620 <MultiConfigRequest> format string and return.
3621 @param AltCfgResp Pointer to a null-terminated Unicode string in
3622 <ConfigAltResp> format. When the pointer is to NULL,
3623 the full default value string retrieved from IFR data
3624 will return. When the pinter is to a string, the
3625 full default value string retrieved from IFR data
3626 will be merged into the input string and return.
3627 When Request points to NULL, the default value string
3628 for each varstore in form package will be merged into
3629 a <MultiConfigAltResp> format string and return.
3630 @param PointerProgress Optional parameter, it can be be NULL.
3631 When it is not NULL, if Request is NULL, it returns NULL.
3632 On return, points to a character in the Request
3633 string. Points to the string's null terminator if
3634 request was successful. Points to the most recent
3635 & before the first failing name / value pair (or
3636 the beginning of the string if the failure is in
3637 the first name / value pair) if the request was
3638 not successful.
3639 @retval EFI_SUCCESS The Results string is set to the full request string.
3640 And AltCfgResp contains all default value string.
3641 @retval EFI_OUT_OF_RESOURCES Not enough memory for the return string.
3642 @retval EFI_NOT_FOUND The varstore (Guid and Name) in Request string
3643 can't be found in Form package.
3644 @retval EFI_NOT_FOUND HiiPackage can't be got on the input HiiHandle.
3645 @retval EFI_INVALID_PARAMETER Request points to NULL.
3646
3647 **/
3648 EFI_STATUS
3649 EFIAPI
3650 GetFullStringFromHiiFormPackages (
3651 IN HII_DATABASE_RECORD *DataBaseRecord,
3652 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
3653 IN OUT EFI_STRING *Request,
3654 IN OUT EFI_STRING *AltCfgResp,
3655 OUT EFI_STRING *PointerProgress OPTIONAL
3656 )
3657 {
3658 EFI_STATUS Status;
3659 UINT8 *HiiFormPackage;
3660 UINTN PackageSize;
3661 IFR_BLOCK_DATA *RequestBlockArray;
3662 IFR_BLOCK_DATA *BlockData;
3663 IFR_DEFAULT_DATA *DefaultValueData;
3664 IFR_DEFAULT_DATA *DefaultId;
3665 IFR_DEFAULT_DATA *DefaultIdArray;
3666 IFR_VARSTORAGE_DATA *VarStorageData;
3667 EFI_STRING DefaultAltCfgResp;
3668 EFI_STRING ConfigHdr;
3669 EFI_STRING StringPtr;
3670 EFI_STRING Progress;
3671
3672 if (DataBaseRecord == NULL || DevicePath == NULL || Request == NULL || AltCfgResp == NULL) {
3673 return EFI_INVALID_PARAMETER;
3674 }
3675
3676 //
3677 // Initialize the local variables.
3678 //
3679 RequestBlockArray = NULL;
3680 DefaultIdArray = NULL;
3681 VarStorageData = NULL;
3682 DefaultAltCfgResp = NULL;
3683 ConfigHdr = NULL;
3684 HiiFormPackage = NULL;
3685 PackageSize = 0;
3686 Progress = *Request;
3687
3688 Status = GetFormPackageData (DataBaseRecord, &HiiFormPackage, &PackageSize);
3689 if (EFI_ERROR (Status)) {
3690 goto Done;
3691 }
3692
3693 //
3694 // 1. Get the request block array by Request String when Request string containts the block array.
3695 //
3696 StringPtr = NULL;
3697 if (*Request != NULL) {
3698 StringPtr = *Request;
3699 //
3700 // Jump <ConfigHdr>
3701 //
3702 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
3703 Status = EFI_INVALID_PARAMETER;
3704 goto Done;
3705 }
3706 StringPtr += StrLen (L"GUID=");
3707 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&NAME=", StrLen (L"&NAME=")) != 0) {
3708 StringPtr++;
3709 }
3710 if (*StringPtr == L'\0') {
3711 Status = EFI_INVALID_PARAMETER;
3712 goto Done;
3713 }
3714 StringPtr += StrLen (L"&NAME=");
3715 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&PATH=", StrLen (L"&PATH=")) != 0) {
3716 StringPtr++;
3717 }
3718 if (*StringPtr == L'\0') {
3719 Status = EFI_INVALID_PARAMETER;
3720 goto Done;
3721 }
3722 StringPtr += StrLen (L"&PATH=");
3723 while (*StringPtr != L'\0' && *StringPtr != L'&') {
3724 StringPtr ++;
3725 }
3726
3727 if (*StringPtr == L'\0') {
3728 //
3729 // No request block is found.
3730 //
3731 StringPtr = NULL;
3732 }
3733 }
3734
3735 //
3736 // If StringPtr != NULL, get the request elements.
3737 //
3738 if (StringPtr != NULL) {
3739 if (StrStr (StringPtr, L"&OFFSET=") != NULL) {
3740 RequestBlockArray = GetBlockElement(StringPtr, &Progress);
3741 } else {
3742 RequestBlockArray = GetNameElement(StringPtr, &Progress);
3743 }
3744
3745 if (RequestBlockArray == NULL) {
3746 Status = EFI_INVALID_PARAMETER;
3747 goto Done;
3748 }
3749 }
3750
3751 //
3752 // Initialize DefaultIdArray to store the map between DeaultId and DefaultName
3753 //
3754 DefaultIdArray = (IFR_DEFAULT_DATA *) AllocateZeroPool (sizeof (IFR_DEFAULT_DATA));
3755 if (DefaultIdArray == NULL) {
3756 Status = EFI_OUT_OF_RESOURCES;
3757 goto Done;
3758 }
3759 InitializeListHead (&DefaultIdArray->Entry);
3760
3761 //
3762 // Initialize VarStorageData to store the var store Block and Default value information.
3763 //
3764 VarStorageData = (IFR_VARSTORAGE_DATA *) AllocateZeroPool (sizeof (IFR_VARSTORAGE_DATA));
3765 if (VarStorageData == NULL) {
3766 Status = EFI_OUT_OF_RESOURCES;
3767 goto Done;
3768 }
3769 InitializeListHead (&VarStorageData->Entry);
3770 InitializeListHead (&VarStorageData->BlockEntry);
3771
3772 //
3773 // 2. Parse FormPackage to get BlockArray and DefaultId Array for the request BlockArray.
3774 //
3775
3776 //
3777 // Parse the opcode in form pacakge to get the default setting.
3778 //
3779 Status = ParseIfrData (DataBaseRecord->Handle,
3780 HiiFormPackage,
3781 (UINT32) PackageSize,
3782 *Request,
3783 RequestBlockArray,
3784 VarStorageData,
3785 DefaultIdArray);
3786 if (EFI_ERROR (Status)) {
3787 goto Done;
3788 }
3789
3790 //
3791 // No requested varstore in IFR data and directly return
3792 //
3793 if (VarStorageData->Type == 0 && VarStorageData->Name == NULL) {
3794 Status = EFI_SUCCESS;
3795 goto Done;
3796 }
3797
3798 //
3799 // 3. Construct Request Element (Block Name) for 2.1 and 2.2 case.
3800 //
3801 Status = GenerateHdr (VarStorageData, DevicePath, &ConfigHdr);
3802 if (EFI_ERROR (Status)) {
3803 goto Done;
3804 }
3805
3806 if (RequestBlockArray == NULL) {
3807 if (!GenerateConfigRequest(ConfigHdr, VarStorageData, &Status, Request)) {
3808 goto Done;
3809 }
3810 }
3811
3812 //
3813 // 4. Construct Default Value string in AltResp according to request element.
3814 // Go through all VarStorageData Entry and get the DefaultId array for each one
3815 // Then construct them all to : ConfigHdr AltConfigHdr ConfigBody AltConfigHdr ConfigBody
3816 //
3817 Status = GenerateAltConfigResp (DataBaseRecord->Handle,ConfigHdr, VarStorageData, DefaultIdArray, &DefaultAltCfgResp);
3818 if (EFI_ERROR (Status)) {
3819 goto Done;
3820 }
3821
3822 //
3823 // 5. Merge string into the input AltCfgResp if the iput *AltCfgResp is not NULL.
3824 //
3825 if (*AltCfgResp != NULL && DefaultAltCfgResp != NULL) {
3826 Status = MergeDefaultString (AltCfgResp, DefaultAltCfgResp);
3827 FreePool (DefaultAltCfgResp);
3828 } else if (*AltCfgResp == NULL) {
3829 *AltCfgResp = DefaultAltCfgResp;
3830 }
3831
3832 Done:
3833 if (RequestBlockArray != NULL) {
3834 //
3835 // Free Link Array RequestBlockArray
3836 //
3837 while (!IsListEmpty (&RequestBlockArray->Entry)) {
3838 BlockData = BASE_CR (RequestBlockArray->Entry.ForwardLink, IFR_BLOCK_DATA, Entry);
3839 RemoveEntryList (&BlockData->Entry);
3840 if (BlockData->Name != NULL) {
3841 FreePool (BlockData->Name);
3842 }
3843 FreePool (BlockData);
3844 }
3845
3846 FreePool (RequestBlockArray);
3847 }
3848
3849 if (VarStorageData != NULL) {
3850 //
3851 // Free link array VarStorageData
3852 //
3853 while (!IsListEmpty (&VarStorageData->BlockEntry)) {
3854 BlockData = BASE_CR (VarStorageData->BlockEntry.ForwardLink, IFR_BLOCK_DATA, Entry);
3855 RemoveEntryList (&BlockData->Entry);
3856 if (BlockData->Name != NULL) {
3857 FreePool (BlockData->Name);
3858 }
3859 //
3860 // Free default value link array
3861 //
3862 while (!IsListEmpty (&BlockData->DefaultValueEntry)) {
3863 DefaultValueData = BASE_CR (BlockData->DefaultValueEntry.ForwardLink, IFR_DEFAULT_DATA, Entry);
3864 RemoveEntryList (&DefaultValueData->Entry);
3865 FreePool (DefaultValueData);
3866 }
3867 FreePool (BlockData);
3868 }
3869 FreePool (VarStorageData);
3870 }
3871
3872 if (DefaultIdArray != NULL) {
3873 //
3874 // Free DefaultId Array
3875 //
3876 while (!IsListEmpty (&DefaultIdArray->Entry)) {
3877 DefaultId = BASE_CR (DefaultIdArray->Entry.ForwardLink, IFR_DEFAULT_DATA, Entry);
3878 RemoveEntryList (&DefaultId->Entry);
3879 FreePool (DefaultId);
3880 }
3881 FreePool (DefaultIdArray);
3882 }
3883
3884 //
3885 // Free the allocated string
3886 //
3887 if (ConfigHdr != NULL) {
3888 FreePool (ConfigHdr);
3889 }
3890
3891 //
3892 // Free Pacakge data
3893 //
3894 if (HiiFormPackage != NULL) {
3895 FreePool (HiiFormPackage);
3896 }
3897
3898 if (PointerProgress != NULL) {
3899 if (*Request == NULL) {
3900 *PointerProgress = NULL;
3901 } else if (EFI_ERROR (Status)) {
3902 *PointerProgress = *Request;
3903 } else {
3904 *PointerProgress = *Request + StrLen (*Request);
3905 }
3906 }
3907
3908 return Status;
3909 }
3910
3911 /**
3912 This function gets the full request resp string by
3913 parsing IFR data in HII form packages.
3914
3915 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
3916 instance.
3917 @param EfiVarStoreInfo The efi varstore info which is save in the EFI
3918 varstore data structure.
3919 @param Request Pointer to a null-terminated Unicode string in
3920 <ConfigRequest> format.
3921 @param RequestResp Pointer to a null-terminated Unicode string in
3922 <ConfigResp> format.
3923 @param AccessProgress On return, points to a character in the Request
3924 string. Points to the string's null terminator if
3925 request was successful. Points to the most recent
3926 & before the first failing name / value pair (or
3927 the beginning of the string if the failure is in
3928 the first name / value pair) if the request was
3929 not successful.
3930
3931 @retval EFI_SUCCESS The Results string is set to the full request string.
3932 And AltCfgResp contains all default value string.
3933 @retval EFI_OUT_OF_RESOURCES Not enough memory for the return string.
3934 @retval EFI_INVALID_PARAMETER Request points to NULL.
3935
3936 **/
3937 EFI_STATUS
3938 GetConfigRespFromEfiVarStore (
3939 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
3940 IN EFI_IFR_VARSTORE_EFI *EfiVarStoreInfo,
3941 IN EFI_STRING Request,
3942 OUT EFI_STRING *RequestResp,
3943 OUT EFI_STRING *AccessProgress
3944 )
3945 {
3946 EFI_STATUS Status;
3947 EFI_STRING VarStoreName;
3948 UINT8 *VarStore;
3949 UINTN BufferSize;
3950
3951 Status = EFI_SUCCESS;
3952 BufferSize = 0;
3953 VarStore = NULL;
3954 VarStoreName = NULL;
3955 *AccessProgress = Request;
3956
3957 VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)EfiVarStoreInfo->Name) * sizeof (CHAR16));
3958 if (VarStoreName == NULL) {
3959 Status = EFI_OUT_OF_RESOURCES;
3960 goto Done;
3961 }
3962 AsciiStrToUnicodeStr ((CHAR8 *) EfiVarStoreInfo->Name, VarStoreName);
3963
3964
3965 Status = gRT->GetVariable (VarStoreName, &EfiVarStoreInfo->Guid, NULL, &BufferSize, NULL);
3966 if (Status != EFI_BUFFER_TOO_SMALL) {
3967 goto Done;
3968 }
3969
3970 VarStore = AllocateZeroPool (BufferSize);
3971 ASSERT (VarStore != NULL);
3972 Status = gRT->GetVariable (VarStoreName, &EfiVarStoreInfo->Guid, NULL, &BufferSize, VarStore);
3973 if (EFI_ERROR (Status)) {
3974 goto Done;
3975 }
3976
3977 Status = HiiBlockToConfig(This, Request, VarStore, BufferSize, RequestResp, AccessProgress);
3978 if (EFI_ERROR (Status)) {
3979 goto Done;
3980 }
3981
3982 Done:
3983 if (VarStoreName != NULL) {
3984 FreePool (VarStoreName);
3985 }
3986
3987 if (VarStore != NULL) {
3988 FreePool (VarStore);
3989 }
3990
3991 return Status;
3992 }
3993
3994
3995 /**
3996 This function route the full request resp string for efi varstore.
3997
3998 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
3999 instance.
4000 @param EfiVarStoreInfo The efi varstore info which is save in the EFI
4001 varstore data structure.
4002 @param RequestResp Pointer to a null-terminated Unicode string in
4003 <ConfigResp> format.
4004 @param Result Pointer to a null-terminated Unicode string in
4005 <ConfigResp> format.
4006
4007 @retval EFI_SUCCESS The Results string is set to the full request string.
4008 And AltCfgResp contains all default value string.
4009 @retval EFI_OUT_OF_RESOURCES Not enough memory for the return string.
4010 @retval EFI_INVALID_PARAMETER Request points to NULL.
4011
4012 **/
4013 EFI_STATUS
4014 RouteConfigRespForEfiVarStore (
4015 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
4016 IN EFI_IFR_VARSTORE_EFI *EfiVarStoreInfo,
4017 IN EFI_STRING RequestResp,
4018 OUT EFI_STRING *Result
4019 )
4020 {
4021 EFI_STATUS Status;
4022 EFI_STRING VarStoreName;
4023 UINT8 *VarStore;
4024 UINTN BufferSize;
4025 UINTN BlockSize;
4026
4027 Status = EFI_SUCCESS;
4028 BufferSize = 0;
4029 VarStore = NULL;
4030 VarStoreName = NULL;
4031
4032 VarStoreName = AllocateZeroPool (AsciiStrSize ((CHAR8 *)EfiVarStoreInfo->Name) * sizeof (CHAR16));
4033 if (VarStoreName == NULL) {
4034 Status = EFI_OUT_OF_RESOURCES;
4035 goto Done;
4036 }
4037 AsciiStrToUnicodeStr ((CHAR8 *) EfiVarStoreInfo->Name, VarStoreName);
4038
4039 Status = gRT->GetVariable (VarStoreName, &EfiVarStoreInfo->Guid, NULL, &BufferSize, NULL);
4040 if (Status != EFI_BUFFER_TOO_SMALL) {
4041 goto Done;
4042 }
4043
4044 BlockSize = BufferSize;
4045 VarStore = AllocateZeroPool (BufferSize);
4046 ASSERT (VarStore != NULL);
4047 Status = gRT->GetVariable (VarStoreName, &EfiVarStoreInfo->Guid, NULL, &BufferSize, VarStore);
4048 if (EFI_ERROR (Status)) {
4049 goto Done;
4050 }
4051
4052 Status = HiiConfigToBlock(This, RequestResp, VarStore, &BlockSize, Result);
4053 if (EFI_ERROR (Status)) {
4054 goto Done;
4055 }
4056
4057 Status = gRT->SetVariable (VarStoreName, &EfiVarStoreInfo->Guid, EfiVarStoreInfo->Attributes, BufferSize, VarStore);
4058 if (EFI_ERROR (Status)) {
4059 goto Done;
4060 }
4061
4062 Done:
4063 if (VarStoreName != NULL) {
4064 FreePool (VarStoreName);
4065 }
4066
4067 if (VarStore != NULL) {
4068 FreePool (VarStore);
4069 }
4070
4071 return Status;
4072 }
4073
4074 /**
4075 Validate the config request elements.
4076
4077 @param ConfigElements A null-terminated Unicode string in <ConfigRequest> format,
4078 without configHdr field.
4079
4080 @retval CHAR16 * THE first Name/value pair not correct.
4081 @retval NULL Success parse the name/value pair
4082 **/
4083 CHAR16 *
4084 OffsetWidthValidate (
4085 CHAR16 *ConfigElements
4086 )
4087 {
4088 CHAR16 *StringPtr;
4089 CHAR16 *RetVal;
4090
4091 StringPtr = ConfigElements;
4092
4093 while (1) {
4094 RetVal = StringPtr;
4095 if (StrnCmp (StringPtr, L"&OFFSET=", StrLen (L"&OFFSET=")) != 0) {
4096 return RetVal;
4097 }
4098
4099 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
4100 StringPtr++;
4101 }
4102 if (*StringPtr == L'\0') {
4103 return RetVal;
4104 }
4105
4106 StringPtr += StrLen (L"&WIDTH=");
4107 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&OFFSET=", StrLen (L"&OFFSET=")) != 0) {
4108 StringPtr ++;
4109 }
4110
4111 if (*StringPtr == L'\0') {
4112 return NULL;
4113 }
4114 }
4115 }
4116
4117 /**
4118 Validate the config request elements.
4119
4120 @param ConfigElements A null-terminated Unicode string in <ConfigRequest> format,
4121 without configHdr field.
4122
4123 @retval CHAR16 * THE first Name/value pair not correct.
4124 @retval NULL Success parse the name/value pair
4125
4126 **/
4127 CHAR16 *
4128 NameValueValidate (
4129 CHAR16 *ConfigElements
4130 )
4131 {
4132 CHAR16 *StringPtr;
4133 CHAR16 *RetVal;
4134
4135 StringPtr = ConfigElements;
4136
4137 while (1) {
4138 RetVal = StringPtr;
4139 if (*StringPtr != L'&') {
4140 return RetVal;
4141 }
4142 StringPtr += 1;
4143
4144 StringPtr = StrStr (StringPtr, L"&");
4145
4146 if (StringPtr == NULL) {
4147 return NULL;
4148 }
4149 }
4150 }
4151
4152 /**
4153 Validate the config request string.
4154
4155 @param ConfigRequest A null-terminated Unicode string in <ConfigRequest> format.
4156
4157 @retval CHAR16 * THE first element not correct.
4158 @retval NULL Success parse the name/value pair
4159
4160 **/
4161 CHAR16 *
4162 ConfigRequestValidate (
4163 CHAR16 *ConfigRequest
4164 )
4165 {
4166 BOOLEAN HasNameField;
4167 CHAR16 *StringPtr;
4168
4169 HasNameField = TRUE;
4170 StringPtr = ConfigRequest;
4171
4172 //
4173 // Check <ConfigHdr>
4174 //
4175 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
4176 return ConfigRequest;
4177 }
4178 StringPtr += StrLen (L"GUID=");
4179 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&NAME=", StrLen (L"&NAME=")) != 0) {
4180 StringPtr++;
4181 }
4182 if (*StringPtr == L'\0') {
4183 return ConfigRequest;
4184 }
4185 StringPtr += StrLen (L"&NAME=");
4186 if (*StringPtr == L'&') {
4187 HasNameField = FALSE;
4188 }
4189 while (*StringPtr != L'\0' && StrnCmp (StringPtr, L"&PATH=", StrLen (L"&PATH=")) != 0) {
4190 StringPtr++;
4191 }
4192 if (*StringPtr == L'\0') {
4193 return ConfigRequest;
4194 }
4195 StringPtr += StrLen (L"&PATH=");
4196 while (*StringPtr != L'\0' && *StringPtr != L'&') {
4197 StringPtr ++;
4198 }
4199
4200 if (*StringPtr == L'\0') {
4201 return NULL;
4202 }
4203
4204 if (HasNameField) {
4205 //
4206 // Should be Buffer varstore, config request should be "OFFSET/Width" pairs.
4207 //
4208 return OffsetWidthValidate(StringPtr);
4209 } else {
4210 //
4211 // Should be Name/Value varstore, config request should be "&name1&name2..." pairs.
4212 //
4213 return NameValueValidate(StringPtr);
4214 }
4215 }
4216
4217 /**
4218 This function allows a caller to extract the current configuration
4219 for one or more named elements from one or more drivers.
4220
4221 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
4222 instance.
4223 @param Request A null-terminated Unicode string in
4224 <MultiConfigRequest> format.
4225 @param Progress On return, points to a character in the Request
4226 string. Points to the string's null terminator if
4227 request was successful. Points to the most recent
4228 & before the first failing name / value pair (or
4229 the beginning of the string if the failure is in
4230 the first name / value pair) if the request was
4231 not successful.
4232 @param Results Null-terminated Unicode string in
4233 <MultiConfigAltResp> format which has all values
4234 filled in for the names in the Request string.
4235 String to be allocated by the called function.
4236
4237 @retval EFI_SUCCESS The Results string is filled with the values
4238 corresponding to all requested names.
4239 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the parts of the
4240 results that must be stored awaiting possible
4241 future protocols.
4242 @retval EFI_NOT_FOUND Routing data doesn't match any known driver.
4243 Progress set to the "G" in "GUID" of the routing
4244 header that doesn't match. Note: There is no
4245 requirement that all routing data be validated
4246 before any configuration extraction.
4247 @retval EFI_INVALID_PARAMETER For example, passing in a NULL for the Request
4248 parameter would result in this type of error. The
4249 Progress parameter is set to NULL.
4250 @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set to most recent &
4251 before the error or the beginning of the string.
4252 @retval EFI_INVALID_PARAMETER The ExtractConfig function of the underlying HII
4253 Configuration Access Protocol returned
4254 EFI_INVALID_PARAMETER. Progress set to most recent
4255 & before the error or the beginning of the string.
4256
4257 **/
4258 EFI_STATUS
4259 EFIAPI
4260 HiiConfigRoutingExtractConfig (
4261 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
4262 IN CONST EFI_STRING Request,
4263 OUT EFI_STRING *Progress,
4264 OUT EFI_STRING *Results
4265 )
4266 {
4267 HII_DATABASE_PRIVATE_DATA *Private;
4268 EFI_STRING StringPtr;
4269 EFI_STRING ConfigRequest;
4270 UINTN Length;
4271 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
4272 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
4273 EFI_STATUS Status;
4274 LIST_ENTRY *Link;
4275 HII_DATABASE_RECORD *Database;
4276 UINT8 *DevicePathPkg;
4277 UINT8 *CurrentDevicePath;
4278 EFI_HANDLE DriverHandle;
4279 EFI_HII_HANDLE HiiHandle;
4280 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
4281 EFI_STRING AccessProgress;
4282 EFI_STRING AccessResults;
4283 EFI_STRING AccessProgressBackup;
4284 EFI_STRING AccessResultsBackup;
4285 EFI_STRING DefaultResults;
4286 BOOLEAN FirstElement;
4287 BOOLEAN IfrDataParsedFlag;
4288 BOOLEAN IsEfiVarStore;
4289 EFI_IFR_VARSTORE_EFI *EfiVarStoreInfo;
4290 EFI_STRING ErrorPtr;
4291 UINTN DevicePathSize;
4292 UINTN ConigStringSize;
4293 UINTN ConigStringSizeNewsize;
4294 EFI_STRING ConfigStringPtr;
4295
4296 if (This == NULL || Progress == NULL || Results == NULL) {
4297 return EFI_INVALID_PARAMETER;
4298 }
4299
4300 if (Request == NULL) {
4301 *Progress = NULL;
4302 return EFI_INVALID_PARAMETER;
4303 }
4304
4305 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
4306 StringPtr = Request;
4307 *Progress = StringPtr;
4308 DefaultResults = NULL;
4309 ConfigRequest = NULL;
4310 Status = EFI_SUCCESS;
4311 AccessResults = NULL;
4312 AccessProgress = NULL;
4313 AccessResultsBackup = NULL;
4314 AccessProgressBackup = NULL;
4315 DevicePath = NULL;
4316 IfrDataParsedFlag = FALSE;
4317 IsEfiVarStore = FALSE;
4318 EfiVarStoreInfo = NULL;
4319
4320 //
4321 // The first element of <MultiConfigRequest> should be
4322 // <GuidHdr>, which is in 'GUID='<Guid> syntax.
4323 //
4324 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
4325 return EFI_INVALID_PARAMETER;
4326 }
4327
4328 FirstElement = TRUE;
4329
4330 //
4331 // Allocate a fix length of memory to store Results. Reallocate memory for
4332 // Results if this fix length is insufficient.
4333 //
4334 *Results = (EFI_STRING) AllocateZeroPool (MAX_STRING_LENGTH);
4335 if (*Results == NULL) {
4336 return EFI_OUT_OF_RESOURCES;
4337 }
4338
4339 while (*StringPtr != 0 && StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) == 0) {
4340 //
4341 // If parsing error, set Progress to the beginning of the <MultiConfigRequest>
4342 // or most recent & before the error.
4343 //
4344 if (StringPtr == Request) {
4345 *Progress = StringPtr;
4346 } else {
4347 *Progress = StringPtr - 1;
4348 }
4349
4350 //
4351 // Process each <ConfigRequest> of <MultiConfigRequest>
4352 //
4353 Length = CalculateConfigStringLen (StringPtr);
4354 ConfigRequest = AllocateCopyPool ((Length + 1) * sizeof (CHAR16), StringPtr);
4355 if (ConfigRequest == NULL) {
4356 Status = EFI_OUT_OF_RESOURCES;
4357 goto Done;
4358 }
4359 *(ConfigRequest + Length) = 0;
4360
4361 //
4362 // Get the UEFI device path
4363 //
4364 Status = GetDevicePath (ConfigRequest, (UINT8 **) &DevicePath);
4365 if (EFI_ERROR (Status)) {
4366 goto Done;
4367 }
4368
4369 //
4370 // Find driver which matches the routing data.
4371 //
4372 DriverHandle = NULL;
4373 HiiHandle = NULL;
4374 Database = NULL;
4375 for (Link = Private->DatabaseList.ForwardLink;
4376 Link != &Private->DatabaseList;
4377 Link = Link->ForwardLink
4378 ) {
4379 Database = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
4380 if ((DevicePathPkg = Database->PackageList->DevicePathPkg) != NULL) {
4381 CurrentDevicePath = DevicePathPkg + sizeof (EFI_HII_PACKAGE_HEADER);
4382 DevicePathSize = GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) CurrentDevicePath);
4383 if ((CompareMem (DevicePath,CurrentDevicePath,DevicePathSize) == 0) && IsThisPackageList(Database, ConfigRequest)) {
4384 DriverHandle = Database->DriverHandle;
4385 HiiHandle = Database->Handle;
4386 break;
4387 }
4388 }
4389 }
4390
4391 //
4392 // Try to find driver handle by device path.
4393 //
4394 if (DriverHandle == NULL) {
4395 TempDevicePath = DevicePath;
4396 Status = gBS->LocateDevicePath (
4397 &gEfiDevicePathProtocolGuid,
4398 &TempDevicePath,
4399 &DriverHandle
4400 );
4401 if (EFI_ERROR (Status) || (DriverHandle == NULL)) {
4402 //
4403 // Routing data does not match any known driver.
4404 // Set Progress to the 'G' in "GUID" of the routing header.
4405 //
4406 *Progress = StringPtr;
4407 Status = EFI_NOT_FOUND;
4408 goto Done;
4409 }
4410 }
4411
4412 //
4413 // Validate ConfigRequest String.
4414 //
4415 ErrorPtr = ConfigRequestValidate(ConfigRequest);
4416 if (ErrorPtr != NULL) {
4417 *Progress = StrStr (StringPtr, ErrorPtr);
4418 Status = EFI_INVALID_PARAMETER;
4419 goto Done;
4420 }
4421
4422 //
4423 // Check whether ConfigRequest contains request string.
4424 //
4425 IfrDataParsedFlag = FALSE;
4426 if ((HiiHandle != NULL) && !GetElementsFromRequest(ConfigRequest)) {
4427 //
4428 // Get the full request string from IFR when HiiPackage is registered to HiiHandle
4429 //
4430 IfrDataParsedFlag = TRUE;
4431 Status = GetFullStringFromHiiFormPackages (Database, DevicePath, &ConfigRequest, &DefaultResults, &AccessProgress);
4432 if (EFI_ERROR (Status)) {
4433 //
4434 // AccessProgress indicates the parsing progress on <ConfigRequest>.
4435 // Map it to the progress on <MultiConfigRequest> then return it.
4436 //
4437 ASSERT (AccessProgress != NULL);
4438 *Progress = StrStr (StringPtr, AccessProgress);
4439 goto Done;
4440 }
4441 //
4442 // Not any request block is found.
4443 //
4444 if (!GetElementsFromRequest(ConfigRequest)) {
4445 AccessResults = AllocateCopyPool (StrSize (ConfigRequest), ConfigRequest);
4446 goto NextConfigString;
4447 }
4448 }
4449
4450 //
4451 // Check whether this ConfigRequest is search from Efi varstore type storage.
4452 //
4453 Status = GetVarStoreType(Database, ConfigRequest, &IsEfiVarStore, &EfiVarStoreInfo);
4454 if (EFI_ERROR (Status)) {
4455 goto Done;
4456 }
4457
4458 if (IsEfiVarStore) {
4459 //
4460 // Call the GetVariable function to extract settings.
4461 //
4462 Status = GetConfigRespFromEfiVarStore(This, EfiVarStoreInfo, ConfigRequest, &AccessResults, &AccessProgress);
4463 FreePool (EfiVarStoreInfo);
4464 if (EFI_ERROR (Status)) {
4465 //
4466 // AccessProgress indicates the parsing progress on <ConfigRequest>.
4467 // Map it to the progress on <MultiConfigRequest> then return it.
4468 //
4469 *Progress = StrStr (StringPtr, AccessProgress);
4470 goto Done;
4471 }
4472
4473 //
4474 // For EfiVarstore, call corresponding ConfigAccess protocol to get the AltCfgResp from driver.
4475 //
4476 Status = gBS->HandleProtocol (
4477 DriverHandle,
4478 &gEfiHiiConfigAccessProtocolGuid,
4479 (VOID **) &ConfigAccess
4480 );
4481 if (EFI_ERROR (Status)) {
4482 //
4483 // The driver has EfiVarStore, may not install ConfigAccess protocol.
4484 // So ignore the error status in this case.
4485 //
4486 Status = EFI_SUCCESS;
4487 } else {
4488 Status = ConfigAccess->ExtractConfig (
4489 ConfigAccess,
4490 ConfigRequest,
4491 &AccessProgressBackup,
4492 &AccessResultsBackup
4493 );
4494 if (!EFI_ERROR(Status)) {
4495 //
4496 //Merge the AltCfgResp in AccessResultsBackup to AccessResults
4497 //
4498 if ((AccessResultsBackup != NULL) && (StrStr (AccessResultsBackup, L"&ALTCFG=") != NULL)) {
4499 ConigStringSize = StrSize (AccessResults);
4500 ConfigStringPtr = StrStr (AccessResultsBackup, L"&GUID=");
4501 ConigStringSizeNewsize = StrSize (ConfigStringPtr) + ConigStringSize + sizeof (CHAR16);
4502 AccessResults = (EFI_STRING) ReallocatePool (
4503 ConigStringSize,
4504 ConigStringSizeNewsize,
4505 AccessResults);
4506 StrCatS (AccessResults, ConigStringSizeNewsize / sizeof (CHAR16), ConfigStringPtr);
4507 }
4508 } else {
4509 //
4510 // In the ExtractConfig function of some driver may not support EfiVarStore,
4511 // may return error status, just ignore the error status in this case.
4512 //
4513 Status = EFI_SUCCESS;
4514 }
4515 if (AccessResultsBackup != NULL) {
4516 FreePool (AccessResultsBackup);
4517 AccessResultsBackup = NULL;
4518 }
4519 }
4520 } else {
4521 //
4522 // Call corresponding ConfigAccess protocol to extract settings
4523 //
4524 Status = gBS->HandleProtocol (
4525 DriverHandle,
4526 &gEfiHiiConfigAccessProtocolGuid,
4527 (VOID **) &ConfigAccess
4528 );
4529 if (EFI_ERROR (Status)) {
4530 goto Done;
4531 }
4532
4533 Status = ConfigAccess->ExtractConfig (
4534 ConfigAccess,
4535 ConfigRequest,
4536 &AccessProgress,
4537 &AccessResults
4538 );
4539 }
4540 if (EFI_ERROR (Status)) {
4541 //
4542 // AccessProgress indicates the parsing progress on <ConfigRequest>.
4543 // Map it to the progress on <MultiConfigRequest> then return it.
4544 //
4545 *Progress = StrStr (StringPtr, AccessProgress);
4546 goto Done;
4547 }
4548
4549 //
4550 // Attach this <ConfigAltResp> to a <MultiConfigAltResp>. There is a '&'
4551 // which seperates the first <ConfigAltResp> and the following ones.
4552 //
4553 ASSERT (*AccessProgress == 0);
4554
4555 //
4556 // Update AccessResults by getting default setting from IFR when HiiPackage is registered to HiiHandle
4557 //
4558 if (!IfrDataParsedFlag && HiiHandle != NULL) {
4559 Status = GetFullStringFromHiiFormPackages (Database, DevicePath, &ConfigRequest, &DefaultResults, NULL);
4560 ASSERT_EFI_ERROR (Status);
4561 }
4562
4563 FreePool (DevicePath);
4564 DevicePath = NULL;
4565
4566 if (DefaultResults != NULL) {
4567 Status = MergeDefaultString (&AccessResults, DefaultResults);
4568 ASSERT_EFI_ERROR (Status);
4569 FreePool (DefaultResults);
4570 DefaultResults = NULL;
4571 }
4572
4573 NextConfigString:
4574 if (!FirstElement) {
4575 Status = AppendToMultiString (Results, L"&");
4576 ASSERT_EFI_ERROR (Status);
4577 }
4578
4579 Status = AppendToMultiString (Results, AccessResults);
4580 ASSERT_EFI_ERROR (Status);
4581
4582 FirstElement = FALSE;
4583
4584 FreePool (AccessResults);
4585 AccessResults = NULL;
4586 FreePool (ConfigRequest);
4587 ConfigRequest = NULL;
4588
4589 //
4590 // Go to next <ConfigRequest> (skip '&').
4591 //
4592 StringPtr += Length;
4593 if (*StringPtr == 0) {
4594 *Progress = StringPtr;
4595 break;
4596 }
4597
4598 StringPtr++;
4599 }
4600
4601 Done:
4602 if (EFI_ERROR (Status)) {
4603 FreePool (*Results);
4604 *Results = NULL;
4605 }
4606
4607 if (ConfigRequest != NULL) {
4608 FreePool (ConfigRequest);
4609 }
4610
4611 if (AccessResults != NULL) {
4612 FreePool (AccessResults);
4613 }
4614
4615 if (DefaultResults != NULL) {
4616 FreePool (DefaultResults);
4617 }
4618
4619 if (DevicePath != NULL) {
4620 FreePool (DevicePath);
4621 }
4622
4623 return Status;
4624 }
4625
4626
4627 /**
4628 This function allows the caller to request the current configuration for the
4629 entirety of the current HII database and returns the data in a
4630 null-terminated Unicode string.
4631
4632 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
4633 instance.
4634 @param Results Null-terminated Unicode string in
4635 <MultiConfigAltResp> format which has all values
4636 filled in for the entirety of the current HII
4637 database. String to be allocated by the called
4638 function. De-allocation is up to the caller.
4639
4640 @retval EFI_SUCCESS The Results string is filled with the values
4641 corresponding to all requested names.
4642 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the parts of the
4643 results that must be stored awaiting possible
4644 future protocols.
4645 @retval EFI_INVALID_PARAMETER For example, passing in a NULL for the Results
4646 parameter would result in this type of error.
4647
4648 **/
4649 EFI_STATUS
4650 EFIAPI
4651 HiiConfigRoutingExportConfig (
4652 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
4653 OUT EFI_STRING *Results
4654 )
4655 {
4656 EFI_STATUS Status;
4657 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
4658 EFI_STRING AccessResults;
4659 EFI_STRING Progress;
4660 EFI_STRING StringPtr;
4661 EFI_STRING ConfigRequest;
4662 UINTN Index;
4663 EFI_HANDLE *ConfigAccessHandles;
4664 UINTN NumberConfigAccessHandles;
4665 BOOLEAN FirstElement;
4666 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
4667 EFI_HII_HANDLE HiiHandle;
4668 EFI_STRING DefaultResults;
4669 HII_DATABASE_PRIVATE_DATA *Private;
4670 LIST_ENTRY *Link;
4671 HII_DATABASE_RECORD *Database;
4672 UINT8 *DevicePathPkg;
4673 UINT8 *CurrentDevicePath;
4674 BOOLEAN IfrDataParsedFlag;
4675
4676 if (This == NULL || Results == NULL) {
4677 return EFI_INVALID_PARAMETER;
4678 }
4679
4680 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
4681
4682 //
4683 // Allocate a fix length of memory to store Results. Reallocate memory for
4684 // Results if this fix length is insufficient.
4685 //
4686 *Results = (EFI_STRING) AllocateZeroPool (MAX_STRING_LENGTH);
4687 if (*Results == NULL) {
4688 return EFI_OUT_OF_RESOURCES;
4689 }
4690
4691 NumberConfigAccessHandles = 0;
4692 Status = gBS->LocateHandleBuffer (
4693 ByProtocol,
4694 &gEfiHiiConfigAccessProtocolGuid,
4695 NULL,
4696 &NumberConfigAccessHandles,
4697 &ConfigAccessHandles
4698 );
4699 if (EFI_ERROR (Status)) {
4700 return Status;
4701 }
4702
4703 FirstElement = TRUE;
4704
4705 for (Index = 0; Index < NumberConfigAccessHandles; Index++) {
4706 Status = gBS->HandleProtocol (
4707 ConfigAccessHandles[Index],
4708 &gEfiHiiConfigAccessProtocolGuid,
4709 (VOID **) &ConfigAccess
4710 );
4711 if (EFI_ERROR (Status)) {
4712 continue;
4713 }
4714
4715 //
4716 // Get DevicePath and HiiHandle for this ConfigAccess driver handle
4717 //
4718 IfrDataParsedFlag = FALSE;
4719 Progress = NULL;
4720 HiiHandle = NULL;
4721 DefaultResults = NULL;
4722 Database = NULL;
4723 ConfigRequest = NULL;
4724 DevicePath = DevicePathFromHandle (ConfigAccessHandles[Index]);
4725 if (DevicePath != NULL) {
4726 for (Link = Private->DatabaseList.ForwardLink;
4727 Link != &Private->DatabaseList;
4728 Link = Link->ForwardLink
4729 ) {
4730 Database = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
4731 if ((DevicePathPkg = Database->PackageList->DevicePathPkg) != NULL) {
4732 CurrentDevicePath = DevicePathPkg + sizeof (EFI_HII_PACKAGE_HEADER);
4733 if (CompareMem (
4734 DevicePath,
4735 CurrentDevicePath,
4736 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) CurrentDevicePath)
4737 ) == 0) {
4738 HiiHandle = Database->Handle;
4739 break;
4740 }
4741 }
4742 }
4743 }
4744
4745 Status = ConfigAccess->ExtractConfig (
4746 ConfigAccess,
4747 NULL,
4748 &Progress,
4749 &AccessResults
4750 );
4751 if (EFI_ERROR (Status)) {
4752 //
4753 // Update AccessResults by getting default setting from IFR when HiiPackage is registered to HiiHandle
4754 //
4755 if (HiiHandle != NULL && DevicePath != NULL) {
4756 IfrDataParsedFlag = TRUE;
4757 Status = GetFullStringFromHiiFormPackages (Database, DevicePath, &ConfigRequest, &DefaultResults, NULL);
4758 //
4759 // Get the full request string to get the Current setting again.
4760 //
4761 if (!EFI_ERROR (Status) && ConfigRequest != NULL) {
4762 Status = ConfigAccess->ExtractConfig (
4763 ConfigAccess,
4764 ConfigRequest,
4765 &Progress,
4766 &AccessResults
4767 );
4768 FreePool (ConfigRequest);
4769 } else {
4770 Status = EFI_NOT_FOUND;
4771 }
4772 }
4773 }
4774
4775 if (!EFI_ERROR (Status)) {
4776 //
4777 // Update AccessResults by getting default setting from IFR when HiiPackage is registered to HiiHandle
4778 //
4779 if (!IfrDataParsedFlag && HiiHandle != NULL && DevicePath != NULL) {
4780 StringPtr = StrStr (AccessResults, L"&GUID=");
4781 if (StringPtr != NULL) {
4782 *StringPtr = 0;
4783 }
4784 if (GetElementsFromRequest (AccessResults)) {
4785 Status = GetFullStringFromHiiFormPackages (Database, DevicePath, &AccessResults, &DefaultResults, NULL);
4786 ASSERT_EFI_ERROR (Status);
4787 }
4788 if (StringPtr != NULL) {
4789 *StringPtr = L'&';
4790 }
4791 }
4792 //
4793 // Merge the default sting from IFR code into the got setting from driver.
4794 //
4795 if (DefaultResults != NULL) {
4796 Status = MergeDefaultString (&AccessResults, DefaultResults);
4797 ASSERT_EFI_ERROR (Status);
4798 FreePool (DefaultResults);
4799 DefaultResults = NULL;
4800 }
4801
4802 //
4803 // Attach this <ConfigAltResp> to a <MultiConfigAltResp>. There is a '&'
4804 // which seperates the first <ConfigAltResp> and the following ones.
4805 //
4806 if (!FirstElement) {
4807 Status = AppendToMultiString (Results, L"&");
4808 ASSERT_EFI_ERROR (Status);
4809 }
4810
4811 Status = AppendToMultiString (Results, AccessResults);
4812 ASSERT_EFI_ERROR (Status);
4813
4814 FirstElement = FALSE;
4815
4816 FreePool (AccessResults);
4817 AccessResults = NULL;
4818 }
4819 }
4820 FreePool (ConfigAccessHandles);
4821
4822 return EFI_SUCCESS;
4823 }
4824
4825
4826 /**
4827 This function processes the results of processing forms and routes it to the
4828 appropriate handlers or storage.
4829
4830 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
4831 instance.
4832 @param Configuration A null-terminated Unicode string in
4833 <MulltiConfigResp> format.
4834 @param Progress A pointer to a string filled in with the offset of
4835 the most recent & before the first failing name /
4836 value pair (or the beginning of the string if the
4837 failure is in the first name / value pair) or the
4838 terminating NULL if all was successful.
4839
4840 @retval EFI_SUCCESS The results have been distributed or are awaiting
4841 distribution.
4842 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the parts of the
4843 results that must be stored awaiting possible
4844 future protocols.
4845 @retval EFI_INVALID_PARAMETER Passing in a NULL for the Configuration parameter
4846 would result in this type of error.
4847 @retval EFI_NOT_FOUND Target for the specified routing data was not
4848 found.
4849
4850 **/
4851 EFI_STATUS
4852 EFIAPI
4853 HiiConfigRoutingRouteConfig (
4854 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
4855 IN CONST EFI_STRING Configuration,
4856 OUT EFI_STRING *Progress
4857 )
4858 {
4859 HII_DATABASE_PRIVATE_DATA *Private;
4860 EFI_STRING StringPtr;
4861 EFI_STRING ConfigResp;
4862 UINTN Length;
4863 EFI_STATUS Status;
4864 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
4865 EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;
4866 LIST_ENTRY *Link;
4867 HII_DATABASE_RECORD *Database;
4868 UINT8 *DevicePathPkg;
4869 UINT8 *CurrentDevicePath;
4870 EFI_HANDLE DriverHandle;
4871 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
4872 EFI_STRING AccessProgress;
4873 EFI_IFR_VARSTORE_EFI *EfiVarStoreInfo;
4874 BOOLEAN IsEfiVarstore;
4875 UINTN DevicePathSize;
4876
4877 if (This == NULL || Progress == NULL) {
4878 return EFI_INVALID_PARAMETER;
4879 }
4880
4881 if (Configuration == NULL) {
4882 *Progress = NULL;
4883 return EFI_INVALID_PARAMETER;
4884 }
4885
4886 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
4887 StringPtr = Configuration;
4888 *Progress = StringPtr;
4889 Database = NULL;
4890 AccessProgress = NULL;
4891 EfiVarStoreInfo= NULL;
4892 IsEfiVarstore = FALSE;
4893
4894 //
4895 // The first element of <MultiConfigResp> should be
4896 // <GuidHdr>, which is in 'GUID='<Guid> syntax.
4897 //
4898 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
4899 return EFI_INVALID_PARAMETER;
4900 }
4901
4902 while (*StringPtr != 0 && StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) == 0) {
4903 //
4904 // If parsing error, set Progress to the beginning of the <MultiConfigResp>
4905 // or most recent & before the error.
4906 //
4907 if (StringPtr == Configuration) {
4908 *Progress = StringPtr;
4909 } else {
4910 *Progress = StringPtr - 1;
4911 }
4912
4913 //
4914 // Process each <ConfigResp> of <MultiConfigResp>
4915 //
4916 Length = CalculateConfigStringLen (StringPtr);
4917 ConfigResp = AllocateCopyPool ((Length + 1) * sizeof (CHAR16), StringPtr);
4918 if (ConfigResp == NULL) {
4919 return EFI_OUT_OF_RESOURCES;
4920 }
4921 //
4922 // Append '\0' to the end of ConfigRequest
4923 //
4924 *(ConfigResp + Length) = 0;
4925
4926 //
4927 // Get the UEFI device path
4928 //
4929 Status = GetDevicePath (ConfigResp, (UINT8 **) &DevicePath);
4930 if (EFI_ERROR (Status)) {
4931 FreePool (ConfigResp);
4932 return Status;
4933 }
4934
4935 //
4936 // Find driver which matches the routing data.
4937 //
4938 DriverHandle = NULL;
4939 for (Link = Private->DatabaseList.ForwardLink;
4940 Link != &Private->DatabaseList;
4941 Link = Link->ForwardLink
4942 ) {
4943 Database = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
4944
4945 if ((DevicePathPkg = Database->PackageList->DevicePathPkg) != NULL) {
4946 CurrentDevicePath = DevicePathPkg + sizeof (EFI_HII_PACKAGE_HEADER);
4947 DevicePathSize = GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) CurrentDevicePath);
4948 if ((CompareMem (DevicePath,CurrentDevicePath,DevicePathSize) == 0) && IsThisPackageList(Database, ConfigResp)) {
4949 DriverHandle = Database->DriverHandle;
4950 break;
4951 }
4952 }
4953 }
4954
4955 //
4956 // Try to find driver handle by device path.
4957 //
4958 if (DriverHandle == NULL) {
4959 TempDevicePath = DevicePath;
4960 Status = gBS->LocateDevicePath (
4961 &gEfiDevicePathProtocolGuid,
4962 &TempDevicePath,
4963 &DriverHandle
4964 );
4965 if (EFI_ERROR (Status) || (DriverHandle == NULL)) {
4966 //
4967 // Routing data does not match any known driver.
4968 // Set Progress to the 'G' in "GUID" of the routing header.
4969 //
4970 FreePool (DevicePath);
4971 *Progress = StringPtr;
4972 FreePool (ConfigResp);
4973 return EFI_NOT_FOUND;
4974 }
4975 }
4976
4977 FreePool (DevicePath);
4978
4979 //
4980 // Check whether this ConfigRequest is search from Efi varstore type storage.
4981 //
4982 Status = GetVarStoreType(Database, ConfigResp, &IsEfiVarstore, &EfiVarStoreInfo);
4983 if (EFI_ERROR (Status)) {
4984 return Status;
4985 }
4986
4987 if (IsEfiVarstore) {
4988 //
4989 // Call the SetVariable function to route settings.
4990 //
4991 Status = RouteConfigRespForEfiVarStore(This, EfiVarStoreInfo, ConfigResp, &AccessProgress);
4992 FreePool (EfiVarStoreInfo);
4993 } else {
4994 //
4995 // Call corresponding ConfigAccess protocol to route settings
4996 //
4997 Status = gBS->HandleProtocol (
4998 DriverHandle,
4999 &gEfiHiiConfigAccessProtocolGuid,
5000 (VOID **) &ConfigAccess
5001 );
5002 if (EFI_ERROR (Status)) {
5003 *Progress = StringPtr;
5004 FreePool (ConfigResp);
5005 return EFI_NOT_FOUND;
5006 }
5007
5008 Status = ConfigAccess->RouteConfig (
5009 ConfigAccess,
5010 ConfigResp,
5011 &AccessProgress
5012 );
5013 }
5014 if (EFI_ERROR (Status)) {
5015 ASSERT (AccessProgress != NULL);
5016 //
5017 // AccessProgress indicates the parsing progress on <ConfigResp>.
5018 // Map it to the progress on <MultiConfigResp> then return it.
5019 //
5020 *Progress = StrStr (StringPtr, AccessProgress);
5021
5022 FreePool (ConfigResp);
5023 return Status;
5024 }
5025
5026 FreePool (ConfigResp);
5027 ConfigResp = NULL;
5028
5029 //
5030 // Go to next <ConfigResp> (skip '&').
5031 //
5032 StringPtr += Length;
5033 if (*StringPtr == 0) {
5034 *Progress = StringPtr;
5035 break;
5036 }
5037
5038 StringPtr++;
5039
5040 }
5041
5042 return EFI_SUCCESS;
5043 }
5044
5045
5046 /**
5047 This helper function is to be called by drivers to map configuration data
5048 stored in byte array ("block") formats such as UEFI Variables into current
5049 configuration strings.
5050
5051 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
5052 instance.
5053 @param ConfigRequest A null-terminated Unicode string in
5054 <ConfigRequest> format.
5055 @param Block Array of bytes defining the block's configuration.
5056 @param BlockSize Length in bytes of Block.
5057 @param Config Filled-in configuration string. String allocated
5058 by the function. Returned only if call is
5059 successful. It is <ConfigResp> string format.
5060 @param Progress A pointer to a string filled in with the offset of
5061 the most recent & before the first failing
5062 name/value pair (or the beginning of the string if
5063 the failure is in the first name / value pair) or
5064 the terminating NULL if all was successful.
5065
5066 @retval EFI_SUCCESS The request succeeded. Progress points to the null
5067 terminator at the end of the ConfigRequest
5068 string.
5069 @retval EFI_OUT_OF_RESOURCES Not enough memory to allocate Config. Progress
5070 points to the first character of ConfigRequest.
5071 @retval EFI_INVALID_PARAMETER Passing in a NULL for the ConfigRequest or
5072 Block parameter would result in this type of
5073 error. Progress points to the first character of
5074 ConfigRequest.
5075 @retval EFI_DEVICE_ERROR Block not large enough. Progress undefined.
5076 @retval EFI_INVALID_PARAMETER Encountered non <BlockName> formatted string.
5077 Block is left updated and Progress points at
5078 the "&" preceding the first non-<BlockName>.
5079
5080 **/
5081 EFI_STATUS
5082 EFIAPI
5083 HiiBlockToConfig (
5084 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
5085 IN CONST EFI_STRING ConfigRequest,
5086 IN CONST UINT8 *Block,
5087 IN CONST UINTN BlockSize,
5088 OUT EFI_STRING *Config,
5089 OUT EFI_STRING *Progress
5090 )
5091 {
5092 HII_DATABASE_PRIVATE_DATA *Private;
5093 EFI_STRING StringPtr;
5094 UINTN Length;
5095 EFI_STATUS Status;
5096 EFI_STRING TmpPtr;
5097 UINT8 *TmpBuffer;
5098 UINTN Offset;
5099 UINTN Width;
5100 UINT8 *Value;
5101 EFI_STRING ValueStr;
5102 EFI_STRING ConfigElement;
5103 UINTN Index;
5104 UINT8 *TemBuffer;
5105 CHAR16 *TemString;
5106 CHAR16 TemChar;
5107
5108 TmpBuffer = NULL;
5109
5110 if (This == NULL || Progress == NULL || Config == NULL) {
5111 return EFI_INVALID_PARAMETER;
5112 }
5113
5114 if (Block == NULL || ConfigRequest == NULL) {
5115 *Progress = ConfigRequest;
5116 return EFI_INVALID_PARAMETER;
5117 }
5118
5119
5120 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
5121 ASSERT (Private != NULL);
5122
5123 StringPtr = ConfigRequest;
5124 ValueStr = NULL;
5125 Value = NULL;
5126 ConfigElement = NULL;
5127
5128 //
5129 // Allocate a fix length of memory to store Results. Reallocate memory for
5130 // Results if this fix length is insufficient.
5131 //
5132 *Config = (EFI_STRING) AllocateZeroPool (MAX_STRING_LENGTH);
5133 if (*Config == NULL) {
5134 return EFI_OUT_OF_RESOURCES;
5135 }
5136
5137 //
5138 // Jump <ConfigHdr>
5139 //
5140 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
5141 *Progress = StringPtr;
5142 Status = EFI_INVALID_PARAMETER;
5143 goto Exit;
5144 }
5145 while (*StringPtr != 0 && StrnCmp (StringPtr, L"PATH=", StrLen (L"PATH=")) != 0) {
5146 StringPtr++;
5147 }
5148 if (*StringPtr == 0) {
5149 *Progress = StringPtr - 1;
5150 Status = EFI_INVALID_PARAMETER;
5151 goto Exit;
5152 }
5153
5154 while (*StringPtr != L'&' && *StringPtr != 0) {
5155 StringPtr++;
5156 }
5157 if (*StringPtr == 0) {
5158 *Progress = StringPtr;
5159
5160 AppendToMultiString(Config, ConfigRequest);
5161 HiiToLower (*Config);
5162
5163 return EFI_SUCCESS;
5164 }
5165 //
5166 // Skip '&'
5167 //
5168 StringPtr++;
5169
5170 //
5171 // Copy <ConfigHdr> and an additional '&' to <ConfigResp>
5172 //
5173 TemChar = *StringPtr;
5174 *StringPtr = '\0';
5175 AppendToMultiString(Config, ConfigRequest);
5176 *StringPtr = TemChar;
5177
5178 //
5179 // Parse each <RequestElement> if exists
5180 // Only <BlockName> format is supported by this help function.
5181 // <BlockName> ::= 'OFFSET='<Number>&'WIDTH='<Number>
5182 //
5183 while (*StringPtr != 0 && StrnCmp (StringPtr, L"OFFSET=", StrLen (L"OFFSET=")) == 0) {
5184 //
5185 // Back up the header of one <BlockName>
5186 //
5187 TmpPtr = StringPtr;
5188
5189 StringPtr += StrLen (L"OFFSET=");
5190 //
5191 // Get Offset
5192 //
5193 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
5194 if (EFI_ERROR (Status)) {
5195 *Progress = TmpPtr - 1;
5196 goto Exit;
5197 }
5198 Offset = 0;
5199 CopyMem (
5200 &Offset,
5201 TmpBuffer,
5202 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
5203 );
5204 FreePool (TmpBuffer);
5205
5206 StringPtr += Length;
5207 if (StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
5208 *Progress = TmpPtr - 1;
5209 Status = EFI_INVALID_PARAMETER;
5210 goto Exit;
5211 }
5212 StringPtr += StrLen (L"&WIDTH=");
5213
5214 //
5215 // Get Width
5216 //
5217 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
5218 if (EFI_ERROR (Status)) {
5219 *Progress = TmpPtr - 1;
5220 goto Exit;
5221 }
5222 Width = 0;
5223 CopyMem (
5224 &Width,
5225 TmpBuffer,
5226 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
5227 );
5228 FreePool (TmpBuffer);
5229
5230 StringPtr += Length;
5231 if (*StringPtr != 0 && *StringPtr != L'&') {
5232 *Progress = TmpPtr - 1;
5233 Status = EFI_INVALID_PARAMETER;
5234 goto Exit;
5235 }
5236
5237 //
5238 // Calculate Value and convert it to hex string.
5239 //
5240 if (Offset + Width > BlockSize) {
5241 *Progress = StringPtr;
5242 Status = EFI_DEVICE_ERROR;
5243 goto Exit;
5244 }
5245
5246 Value = (UINT8 *) AllocateZeroPool (Width);
5247 if (Value == NULL) {
5248 *Progress = ConfigRequest;
5249 Status = EFI_OUT_OF_RESOURCES;
5250 goto Exit;
5251 }
5252
5253 CopyMem (Value, (UINT8 *) Block + Offset, Width);
5254
5255 Length = Width * 2 + 1;
5256 ValueStr = (EFI_STRING) AllocateZeroPool (Length * sizeof (CHAR16));
5257 if (ValueStr == NULL) {
5258 *Progress = ConfigRequest;
5259 Status = EFI_OUT_OF_RESOURCES;
5260 goto Exit;
5261 }
5262
5263 TemString = ValueStr;
5264 TemBuffer = Value + Width - 1;
5265 for (Index = 0; Index < Width; Index ++, TemBuffer --) {
5266 TemString += UnicodeValueToString (TemString, PREFIX_ZERO | RADIX_HEX, *TemBuffer, 2);
5267 }
5268
5269 FreePool (Value);
5270 Value = NULL;
5271
5272 //
5273 // Build a ConfigElement
5274 //
5275 Length += StringPtr - TmpPtr + 1 + StrLen (L"VALUE=");
5276 ConfigElement = (EFI_STRING) AllocateZeroPool (Length * sizeof (CHAR16));
5277 if (ConfigElement == NULL) {
5278 Status = EFI_OUT_OF_RESOURCES;
5279 goto Exit;
5280 }
5281 CopyMem (ConfigElement, TmpPtr, (StringPtr - TmpPtr + 1) * sizeof (CHAR16));
5282 if (*StringPtr == 0) {
5283 *(ConfigElement + (StringPtr - TmpPtr)) = L'&';
5284 }
5285 *(ConfigElement + (StringPtr - TmpPtr) + 1) = 0;
5286 StrCatS (ConfigElement, Length, L"VALUE=");
5287 StrCatS (ConfigElement, Length, ValueStr);
5288
5289 AppendToMultiString (Config, ConfigElement);
5290
5291 FreePool (ConfigElement);
5292 FreePool (ValueStr);
5293 ConfigElement = NULL;
5294 ValueStr = NULL;
5295
5296 //
5297 // If '\0', parsing is finished. Otherwise skip '&' to continue
5298 //
5299 if (*StringPtr == 0) {
5300 break;
5301 }
5302 AppendToMultiString (Config, L"&");
5303 StringPtr++;
5304
5305 }
5306
5307 if (*StringPtr != 0) {
5308 *Progress = StringPtr - 1;
5309 Status = EFI_INVALID_PARAMETER;
5310 goto Exit;
5311 }
5312
5313 HiiToLower (*Config);
5314 *Progress = StringPtr;
5315 return EFI_SUCCESS;
5316
5317 Exit:
5318 if (*Config != NULL) {
5319 FreePool (*Config);
5320 *Config = NULL;
5321 }
5322 if (ValueStr != NULL) {
5323 FreePool (ValueStr);
5324 }
5325 if (Value != NULL) {
5326 FreePool (Value);
5327 }
5328 if (ConfigElement != NULL) {
5329 FreePool (ConfigElement);
5330 }
5331
5332 return Status;
5333
5334 }
5335
5336
5337 /**
5338 This helper function is to be called by drivers to map configuration strings
5339 to configurations stored in byte array ("block") formats such as UEFI Variables.
5340
5341 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
5342 instance.
5343 @param ConfigResp A null-terminated Unicode string in <ConfigResp>
5344 format.
5345 @param Block A possibly null array of bytes representing the
5346 current block. Only bytes referenced in the
5347 ConfigResp string in the block are modified. If
5348 this parameter is null or if the *BlockSize
5349 parameter is (on input) shorter than required by
5350 the Configuration string, only the BlockSize
5351 parameter is updated and an appropriate status
5352 (see below) is returned.
5353 @param BlockSize The length of the Block in units of UINT8. On
5354 input, this is the size of the Block. On output,
5355 if successful, contains the largest index of the
5356 modified byte in the Block, or the required buffer
5357 size if the Block is not large enough.
5358 @param Progress On return, points to an element of the ConfigResp
5359 string filled in with the offset of the most
5360 recent '&' before the first failing name / value
5361 pair (or the beginning of the string if the
5362 failure is in the first name / value pair) or the
5363 terminating NULL if all was successful.
5364
5365 @retval EFI_SUCCESS The request succeeded. Progress points to the null
5366 terminator at the end of the ConfigResp string.
5367 @retval EFI_OUT_OF_RESOURCES Not enough memory to allocate Config. Progress
5368 points to the first character of ConfigResp.
5369 @retval EFI_INVALID_PARAMETER Passing in a NULL for the ConfigResp or
5370 Block parameter would result in this type of
5371 error. Progress points to the first character of
5372 ConfigResp.
5373 @retval EFI_INVALID_PARAMETER Encountered non <BlockName> formatted name /
5374 value pair. Block is left updated and
5375 Progress points at the '&' preceding the first
5376 non-<BlockName>.
5377 @retval EFI_BUFFER_TOO_SMALL Block not large enough. Progress undefined.
5378 BlockSize is updated with the required buffer size.
5379 @retval EFI_NOT_FOUND Target for the specified routing data was not found.
5380 Progress points to the "G" in "GUID" of the errant
5381 routing data.
5382
5383 **/
5384 EFI_STATUS
5385 EFIAPI
5386 HiiConfigToBlock (
5387 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
5388 IN CONST EFI_STRING ConfigResp,
5389 IN OUT UINT8 *Block,
5390 IN OUT UINTN *BlockSize,
5391 OUT EFI_STRING *Progress
5392 )
5393 {
5394 HII_DATABASE_PRIVATE_DATA *Private;
5395 EFI_STRING StringPtr;
5396 EFI_STRING TmpPtr;
5397 UINTN Length;
5398 EFI_STATUS Status;
5399 UINT8 *TmpBuffer;
5400 UINTN Offset;
5401 UINTN Width;
5402 UINT8 *Value;
5403 UINTN BufferSize;
5404 UINTN MaxBlockSize;
5405
5406 TmpBuffer = NULL;
5407
5408 if (This == NULL || BlockSize == NULL || Progress == NULL) {
5409 return EFI_INVALID_PARAMETER;
5410 }
5411
5412 *Progress = ConfigResp;
5413 if (ConfigResp == NULL) {
5414 return EFI_INVALID_PARAMETER;
5415 }
5416
5417 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
5418 ASSERT (Private != NULL);
5419
5420 StringPtr = ConfigResp;
5421 BufferSize = *BlockSize;
5422 Value = NULL;
5423 MaxBlockSize = 0;
5424
5425 //
5426 // Jump <ConfigHdr>
5427 //
5428 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
5429 *Progress = StringPtr;
5430 Status = EFI_INVALID_PARAMETER;
5431 goto Exit;
5432 }
5433 while (*StringPtr != 0 && StrnCmp (StringPtr, L"PATH=", StrLen (L"PATH=")) != 0) {
5434 StringPtr++;
5435 }
5436 if (*StringPtr == 0) {
5437 *Progress = StringPtr;
5438 Status = EFI_INVALID_PARAMETER;
5439 goto Exit;
5440 }
5441
5442 while (*StringPtr != L'&' && *StringPtr != 0) {
5443 StringPtr++;
5444 }
5445 if (*StringPtr == 0) {
5446 *Progress = StringPtr;
5447 Status = EFI_INVALID_PARAMETER;
5448 goto Exit;
5449 }
5450
5451 //
5452 // Parse each <ConfigElement> if exists
5453 // Only '&'<BlockConfig> format is supported by this help function.
5454 // <BlockConfig> ::= 'OFFSET='<Number>&'WIDTH='<Number>&'VALUE='<Number>
5455 //
5456 while (*StringPtr != 0 && StrnCmp (StringPtr, L"&OFFSET=", StrLen (L"&OFFSET=")) == 0) {
5457 TmpPtr = StringPtr;
5458 StringPtr += StrLen (L"&OFFSET=");
5459 //
5460 // Get Offset
5461 //
5462 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
5463 if (EFI_ERROR (Status)) {
5464 *Progress = TmpPtr;
5465 goto Exit;
5466 }
5467 Offset = 0;
5468 CopyMem (
5469 &Offset,
5470 TmpBuffer,
5471 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
5472 );
5473 FreePool (TmpBuffer);
5474
5475 StringPtr += Length;
5476 if (StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
5477 *Progress = TmpPtr;
5478 Status = EFI_INVALID_PARAMETER;
5479 goto Exit;
5480 }
5481 StringPtr += StrLen (L"&WIDTH=");
5482
5483 //
5484 // Get Width
5485 //
5486 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
5487 if (EFI_ERROR (Status)) {
5488 *Progress = TmpPtr;
5489 goto Exit;
5490 }
5491 Width = 0;
5492 CopyMem (
5493 &Width,
5494 TmpBuffer,
5495 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
5496 );
5497 FreePool (TmpBuffer);
5498
5499 StringPtr += Length;
5500 if (StrnCmp (StringPtr, L"&VALUE=", StrLen (L"&VALUE=")) != 0) {
5501 *Progress = TmpPtr;
5502 Status = EFI_INVALID_PARAMETER;
5503 goto Exit;
5504 }
5505 StringPtr += StrLen (L"&VALUE=");
5506
5507 //
5508 // Get Value
5509 //
5510 Status = GetValueOfNumber (StringPtr, &Value, &Length);
5511 if (EFI_ERROR (Status)) {
5512 *Progress = TmpPtr;
5513 goto Exit;
5514 }
5515
5516 StringPtr += Length;
5517 if (*StringPtr != 0 && *StringPtr != L'&') {
5518 *Progress = TmpPtr;
5519 Status = EFI_INVALID_PARAMETER;
5520 goto Exit;
5521 }
5522
5523 //
5524 // Update the Block with configuration info
5525 //
5526 if ((Block != NULL) && (Offset + Width <= BufferSize)) {
5527 CopyMem (Block + Offset, Value, Width);
5528 }
5529 if (Offset + Width > MaxBlockSize) {
5530 MaxBlockSize = Offset + Width;
5531 }
5532
5533 FreePool (Value);
5534 Value = NULL;
5535
5536 //
5537 // If '\0', parsing is finished.
5538 //
5539 if (*StringPtr == 0) {
5540 break;
5541 }
5542 }
5543
5544 //
5545 // The input string is not ConfigResp format, return error.
5546 //
5547 if (*StringPtr != 0) {
5548 *Progress = StringPtr;
5549 Status = EFI_INVALID_PARAMETER;
5550 goto Exit;
5551 }
5552
5553 *Progress = StringPtr + StrLen (StringPtr);
5554 *BlockSize = MaxBlockSize - 1;
5555
5556 if (MaxBlockSize > BufferSize) {
5557 *BlockSize = MaxBlockSize;
5558 if (Block != NULL) {
5559 return EFI_BUFFER_TOO_SMALL;
5560 }
5561 }
5562
5563 if (Block == NULL) {
5564 *Progress = ConfigResp;
5565 return EFI_INVALID_PARAMETER;
5566 }
5567
5568 return EFI_SUCCESS;
5569
5570 Exit:
5571
5572 if (Value != NULL) {
5573 FreePool (Value);
5574 }
5575 return Status;
5576 }
5577
5578
5579 /**
5580 This helper function is to be called by drivers to extract portions of
5581 a larger configuration string.
5582
5583 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
5584 instance.
5585 @param Configuration A null-terminated Unicode string in
5586 <MultiConfigAltResp> format.
5587 @param Guid A pointer to the GUID value to search for in the
5588 routing portion of the ConfigResp string when
5589 retrieving the requested data. If Guid is NULL,
5590 then all GUID values will be searched for.
5591 @param Name A pointer to the NAME value to search for in the
5592 routing portion of the ConfigResp string when
5593 retrieving the requested data. If Name is NULL,
5594 then all Name values will be searched for.
5595 @param DevicePath A pointer to the PATH value to search for in the
5596 routing portion of the ConfigResp string when
5597 retrieving the requested data. If DevicePath is
5598 NULL, then all DevicePath values will be searched
5599 for.
5600 @param AltCfgId A pointer to the ALTCFG value to search for in the
5601 routing portion of the ConfigResp string when
5602 retrieving the requested data. If this parameter
5603 is NULL, then the current setting will be
5604 retrieved.
5605 @param AltCfgResp A pointer to a buffer which will be allocated by
5606 the function which contains the retrieved string
5607 as requested. This buffer is only allocated if
5608 the call was successful. It is <ConfigResp> format.
5609
5610 @retval EFI_SUCCESS The request succeeded. The requested data was
5611 extracted and placed in the newly allocated
5612 AltCfgResp buffer.
5613 @retval EFI_OUT_OF_RESOURCES Not enough memory to allocate AltCfgResp.
5614 @retval EFI_INVALID_PARAMETER Any parameter is invalid.
5615 @retval EFI_NOT_FOUND Target for the specified routing data was not
5616 found.
5617
5618 **/
5619 EFI_STATUS
5620 EFIAPI
5621 HiiGetAltCfg (
5622 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
5623 IN CONST EFI_STRING Configuration,
5624 IN CONST EFI_GUID *Guid,
5625 IN CONST EFI_STRING Name,
5626 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
5627 IN CONST UINT16 *AltCfgId,
5628 OUT EFI_STRING *AltCfgResp
5629 )
5630 {
5631 EFI_STATUS Status;
5632 EFI_STRING StringPtr;
5633 EFI_STRING HdrStart;
5634 EFI_STRING HdrEnd;
5635 EFI_STRING TmpPtr;
5636 UINTN Length;
5637 EFI_STRING GuidStr;
5638 EFI_STRING NameStr;
5639 EFI_STRING PathStr;
5640 EFI_STRING AltIdStr;
5641 EFI_STRING Result;
5642 BOOLEAN GuidFlag;
5643 BOOLEAN NameFlag;
5644 BOOLEAN PathFlag;
5645
5646 HdrStart = NULL;
5647 HdrEnd = NULL;
5648 GuidStr = NULL;
5649 NameStr = NULL;
5650 PathStr = NULL;
5651 AltIdStr = NULL;
5652 Result = NULL;
5653 GuidFlag = FALSE;
5654 NameFlag = FALSE;
5655 PathFlag = FALSE;
5656
5657 if (This == NULL || Configuration == NULL || AltCfgResp == NULL) {
5658 return EFI_INVALID_PARAMETER;
5659 }
5660
5661 StringPtr = Configuration;
5662 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
5663 return EFI_INVALID_PARAMETER;
5664 }
5665
5666 //
5667 // Generate the sub string for later matching.
5668 //
5669 GenerateSubStr (L"GUID=", sizeof (EFI_GUID), (VOID *) Guid, 1, &GuidStr);
5670 GenerateSubStr (
5671 L"PATH=",
5672 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) DevicePath),
5673 (VOID *) DevicePath,
5674 1,
5675 &PathStr
5676 );
5677 if (AltCfgId != NULL) {
5678 GenerateSubStr (L"ALTCFG=", sizeof (UINT16), (VOID *) AltCfgId, 3, &AltIdStr);
5679 }
5680 if (Name != NULL) {
5681 GenerateSubStr (L"NAME=", StrLen (Name) * sizeof (CHAR16), (VOID *) Name, 2, &NameStr);
5682 } else {
5683 GenerateSubStr (L"NAME=", 0, NULL, 2, &NameStr);
5684 }
5685
5686 while (*StringPtr != 0) {
5687 //
5688 // Try to match the GUID
5689 //
5690 if (!GuidFlag) {
5691 TmpPtr = StrStr (StringPtr, GuidStr);
5692 if (TmpPtr == NULL) {
5693 Status = EFI_NOT_FOUND;
5694 goto Exit;
5695 }
5696 HdrStart = TmpPtr;
5697
5698 //
5699 // Jump to <NameHdr>
5700 //
5701 if (Guid != NULL) {
5702 StringPtr = TmpPtr + StrLen (GuidStr);
5703 } else {
5704 StringPtr = StrStr (TmpPtr, L"NAME=");
5705 if (StringPtr == NULL) {
5706 Status = EFI_NOT_FOUND;
5707 goto Exit;
5708 }
5709 }
5710 GuidFlag = TRUE;
5711 }
5712
5713 //
5714 // Try to match the NAME
5715 //
5716 if (GuidFlag && !NameFlag) {
5717 if (StrnCmp (StringPtr, NameStr, StrLen (NameStr)) != 0) {
5718 GuidFlag = FALSE;
5719 } else {
5720 //
5721 // Jump to <PathHdr>
5722 //
5723 if (Name != NULL) {
5724 StringPtr += StrLen (NameStr);
5725 } else {
5726 StringPtr = StrStr (StringPtr, L"PATH=");
5727 if (StringPtr == NULL) {
5728 Status = EFI_NOT_FOUND;
5729 goto Exit;
5730 }
5731 }
5732 NameFlag = TRUE;
5733 }
5734 }
5735
5736 //
5737 // Try to match the DevicePath
5738 //
5739 if (GuidFlag && NameFlag && !PathFlag) {
5740 if (StrnCmp (StringPtr, PathStr, StrLen (PathStr)) != 0) {
5741 GuidFlag = FALSE;
5742 NameFlag = FALSE;
5743 } else {
5744 //
5745 // Jump to '&' before <DescHdr> or <ConfigBody>
5746 //
5747 if (DevicePath != NULL) {
5748 StringPtr += StrLen (PathStr);
5749 } else {
5750 StringPtr = StrStr (StringPtr, L"&");
5751 if (StringPtr == NULL) {
5752 Status = EFI_NOT_FOUND;
5753 goto Exit;
5754 }
5755 StringPtr ++;
5756 }
5757 PathFlag = TRUE;
5758 HdrEnd = StringPtr;
5759 }
5760 }
5761
5762 //
5763 // Try to match the AltCfgId
5764 //
5765 if (GuidFlag && NameFlag && PathFlag) {
5766 if (AltCfgId == NULL) {
5767 //
5768 // Return Current Setting when AltCfgId is NULL.
5769 //
5770 Status = OutputConfigBody (StringPtr, &Result);
5771 goto Exit;
5772 }
5773 //
5774 // Search the <ConfigAltResp> to get the <AltResp> with AltCfgId.
5775 //
5776 if (StrnCmp (StringPtr, AltIdStr, StrLen (AltIdStr)) != 0) {
5777 GuidFlag = FALSE;
5778 NameFlag = FALSE;
5779 PathFlag = FALSE;
5780 } else {
5781 //
5782 // Skip AltIdStr and &
5783 //
5784 StringPtr = StringPtr + StrLen (AltIdStr);
5785 Status = OutputConfigBody (StringPtr, &Result);
5786 goto Exit;
5787 }
5788 }
5789 }
5790
5791 Status = EFI_NOT_FOUND;
5792
5793 Exit:
5794 *AltCfgResp = NULL;
5795 if (!EFI_ERROR (Status) && (Result != NULL)) {
5796 //
5797 // Copy the <ConfigHdr> and <ConfigBody>
5798 //
5799 Length = HdrEnd - HdrStart + StrLen (Result) + 1;
5800 *AltCfgResp = AllocateZeroPool (Length * sizeof (CHAR16));
5801 if (*AltCfgResp == NULL) {
5802 Status = EFI_OUT_OF_RESOURCES;
5803 } else {
5804 StrnCpyS (*AltCfgResp, Length, HdrStart, HdrEnd - HdrStart);
5805 StrCatS (*AltCfgResp, Length, Result);
5806 Status = EFI_SUCCESS;
5807 }
5808 }
5809
5810 if (GuidStr != NULL) {
5811 FreePool (GuidStr);
5812 }
5813 if (NameStr != NULL) {
5814 FreePool (NameStr);
5815 }
5816 if (PathStr != NULL) {
5817 FreePool (PathStr);
5818 }
5819 if (AltIdStr != NULL) {
5820 FreePool (AltIdStr);
5821 }
5822 if (Result != NULL) {
5823 FreePool (Result);
5824 }
5825
5826 return Status;
5827
5828 }
5829
5830