]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/HiiDatabaseDxe/ConfigKeywordHandler.c
MdeModulePkg: Validate the return value before use it.
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / ConfigKeywordHandler.c
1 /** @file
2 Implementation of interfaces function for EFI_CONFIG_KEYWORD_HANDLER_PROTOCOL.
3
4 Copyright (c) 2015, 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
18 extern HII_DATABASE_PRIVATE_DATA mPrivate;
19
20 /**
21 Convert the hex UNICODE %02x encoding of a UEFI device path to binary
22 from <PathHdr> of <MultiKeywordRequest>.
23
24 This is a internal function.
25
26 @param String MultiKeywordRequest string.
27 @param DevicePathData Binary of a UEFI device path.
28 @param NextString string follow the possible PathHdr string.
29
30 @retval EFI_INVALID_PARAMETER The device path is not valid or the incoming parameter is invalid.
31 @retval EFI_OUT_OF_RESOURCES Lake of resources to store neccesary structures.
32 @retval EFI_SUCCESS The device path is retrieved and translated to binary format.
33 The Input string not include PathHdr section.
34
35 **/
36 EFI_STATUS
37 ExtractDevicePath (
38 IN EFI_STRING String,
39 OUT UINT8 **DevicePathData,
40 OUT EFI_STRING *NextString
41 )
42 {
43 UINTN Length;
44 EFI_STRING PathHdr;
45 UINT8 *DevicePathBuffer;
46 CHAR16 TemStr[2];
47 UINTN Index;
48 UINT8 DigitUint8;
49 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
50
51 ASSERT (NextString != NULL && DevicePathData != NULL);
52
53 //
54 // KeywordRequest == NULL case.
55 //
56 if (String == NULL) {
57 *DevicePathData = NULL;
58 *NextString = NULL;
59 return EFI_SUCCESS;
60 }
61
62 //
63 // Skip '&' if exist.
64 //
65 if (*String == L'&') {
66 String ++;
67 }
68
69 //
70 // Find the 'PATH=' of <PathHdr>.
71 //
72 if (StrnCmp (String, L"PATH=", StrLen (L"PATH=")) != 0) {
73 if (StrnCmp (String, L"KEYWORD=", StrLen (L"KEYWORD=")) != 0) {
74 return EFI_INVALID_PARAMETER;
75 } else {
76 //
77 // Not include PathHdr, return success and DevicePath = NULL.
78 //
79 *DevicePathData = NULL;
80 *NextString = String;
81 return EFI_SUCCESS;
82 }
83 }
84
85 //
86 // Check whether path data does exist.
87 //
88 String += StrLen (L"PATH=");
89 if (*String == 0) {
90 return EFI_INVALID_PARAMETER;
91 }
92 PathHdr = String;
93
94 //
95 // The content between 'PATH=' of <ConfigHdr> and '&' of next element
96 // or '\0' (end of configuration string) is the UNICODE %02x bytes encoding
97 // of UEFI device path.
98 //
99 for (Length = 0; *String != 0 && *String != L'&'; String++, Length++);
100
101 //
102 // Save the return next keyword string value.
103 //
104 *NextString = String;
105
106 //
107 // Check DevicePath Length
108 //
109 if (((Length + 1) / 2) < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
110 return EFI_INVALID_PARAMETER;
111 }
112
113 //
114 // The data in <PathHdr> is encoded as hex UNICODE %02x bytes in the same order
115 // as the device path resides in RAM memory.
116 // Translate the data into binary.
117 //
118 DevicePathBuffer = (UINT8 *) AllocateZeroPool ((Length + 1) / 2);
119 if (DevicePathBuffer == NULL) {
120 return EFI_OUT_OF_RESOURCES;
121 }
122
123 //
124 // Convert DevicePath
125 //
126 ZeroMem (TemStr, sizeof (TemStr));
127 for (Index = 0; Index < Length; Index ++) {
128 TemStr[0] = PathHdr[Index];
129 DigitUint8 = (UINT8) StrHexToUint64 (TemStr);
130 if ((Index & 1) == 0) {
131 DevicePathBuffer [Index/2] = DigitUint8;
132 } else {
133 DevicePathBuffer [Index/2] = (UINT8) ((DevicePathBuffer [Index/2] << 4) + DigitUint8);
134 }
135 }
136
137 //
138 // Validate DevicePath
139 //
140 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) DevicePathBuffer;
141 while (!IsDevicePathEnd (DevicePath)) {
142 if ((DevicePath->Type == 0) || (DevicePath->SubType == 0) || (DevicePathNodeLength (DevicePath) < sizeof (EFI_DEVICE_PATH_PROTOCOL))) {
143 //
144 // Invalid device path
145 //
146 FreePool (DevicePathBuffer);
147 return EFI_INVALID_PARAMETER;
148 }
149 DevicePath = NextDevicePathNode (DevicePath);
150 }
151
152 //
153 // return the device path
154 //
155 *DevicePathData = DevicePathBuffer;
156
157 return EFI_SUCCESS;
158 }
159
160 /**
161 Get NameSpace from the input NameSpaceId string.
162
163 This is a internal function.
164
165 @param String <NameSpaceId> format string.
166 @param NameSpace Return the name space string.
167 @param NextString Return the next string follow namespace.
168
169 @retval EFI_SUCCESS Get the namespace string success.
170 @retval EFI_INVALID_PARAMETER The NameSpaceId string not follow spec definition.
171
172 **/
173 EFI_STATUS
174 ExtractNameSpace (
175 IN EFI_STRING String,
176 OUT CHAR8 **NameSpace,
177 OUT EFI_STRING *NextString
178 )
179 {
180 CHAR16 *TmpPtr;
181
182 ASSERT (NameSpace != NULL);
183
184 TmpPtr = NULL;
185
186 //
187 // Input NameSpaceId == NULL
188 //
189 if (String == NULL) {
190 *NameSpace = NULL;
191 if (NextString != NULL) {
192 *NextString = NULL;
193 }
194 return EFI_SUCCESS;
195 }
196
197 //
198 // Skip '&' if exist.
199 //
200 if (*String == L'&') {
201 String++;
202 }
203
204 if (StrnCmp (String, L"NAMESPACE=", StrLen (L"NAMESPACE=")) != 0) {
205 return EFI_INVALID_PARAMETER;
206 }
207 String += StrLen (L"NAMESPACE=");
208
209 TmpPtr = StrStr (String, L"&");
210 if (TmpPtr != NULL) {
211 *TmpPtr = 0;
212 }
213 if (NextString != NULL) {
214 *NextString = String + StrLen (String);
215 }
216
217 //
218 // Input NameSpace is unicode string. The language in String package is ascii string.
219 // Here will convert the unicode string to ascii and save it.
220 //
221 *NameSpace = AllocatePool (StrLen (String) + 1);
222 if (*NameSpace == NULL) {
223 return EFI_OUT_OF_RESOURCES;
224 }
225 UnicodeStrToAsciiStr (String, *NameSpace);
226
227 if (TmpPtr != NULL) {
228 *TmpPtr = L'&';
229 }
230
231 return EFI_SUCCESS;
232 }
233
234 /**
235 Get Keyword from the input KeywordRequest string.
236
237 This is a internal function.
238
239 @param String KeywordRequestformat string.
240 @param Keyword return the extract keyword string.
241 @param NextString return the next string follow this keyword sectin.
242
243 @retval EFI_SUCCESS Success to get the keyword string.
244 @retval EFI_INVALID_PARAMETER Parsr the input string return error.
245
246 **/
247 EFI_STATUS
248 ExtractKeyword (
249 IN EFI_STRING String,
250 OUT EFI_STRING *Keyword,
251 OUT EFI_STRING *NextString
252 )
253 {
254 EFI_STRING TmpPtr;
255
256 ASSERT ((Keyword != NULL) && (NextString != NULL));
257
258 TmpPtr = NULL;
259
260 //
261 // KeywordRequest == NULL case.
262 //
263 if (String == NULL) {
264 *Keyword = NULL;
265 *NextString = NULL;
266 return EFI_SUCCESS;
267 }
268
269 //
270 // Skip '&' if exist.
271 //
272 if (*String == L'&') {
273 String++;
274 }
275
276 if (StrnCmp (String, L"KEYWORD=", StrLen (L"KEYWORD=")) != 0) {
277 return EFI_INVALID_PARAMETER;
278 }
279
280 String += StrLen (L"KEYWORD=");
281
282 TmpPtr = StrStr (String, L"&");
283 if (TmpPtr != NULL) {
284 *TmpPtr = 0;
285 }
286 *NextString = String + StrLen (String);
287
288 *Keyword = AllocateCopyPool (StrSize (String), String);
289 if (*Keyword == NULL) {
290 return EFI_OUT_OF_RESOURCES;
291 }
292
293 if (TmpPtr != NULL) {
294 *TmpPtr = L'&';
295 }
296
297 return EFI_SUCCESS;
298 }
299
300 /**
301 Get value from the input KeywordRequest string.
302
303 This is a internal function.
304
305 @param String KeywordRequestformat string.
306 @param Value return the extract value string.
307 @param NextString return the next string follow this keyword sectin.
308
309 @retval EFI_SUCCESS Success to get the keyword string.
310 @retval EFI_INVALID_PARAMETER Parsr the input string return error.
311
312 **/
313 EFI_STATUS
314 ExtractValue (
315 IN EFI_STRING String,
316 OUT EFI_STRING *Value,
317 OUT EFI_STRING *NextString
318 )
319 {
320 EFI_STRING TmpPtr;
321
322 ASSERT ((Value != NULL) && (NextString != NULL) && (String != NULL));
323
324 //
325 // Skip '&' if exist.
326 //
327 if (*String == L'&') {
328 String++;
329 }
330
331 if (StrnCmp (String, L"VALUE=", StrLen (L"VALUE=")) != 0) {
332 return EFI_INVALID_PARAMETER;
333 }
334
335 String += StrLen (L"VALUE=");
336
337 TmpPtr = StrStr (String, L"&");
338 if (TmpPtr != NULL) {
339 *TmpPtr = 0;
340 }
341 *NextString = String + StrLen (String);
342
343 *Value = AllocateCopyPool (StrSize (String), String);
344 if (*Value == NULL) {
345 return EFI_OUT_OF_RESOURCES;
346 }
347
348 if (TmpPtr != NULL) {
349 *TmpPtr = L'&';
350 }
351
352 return EFI_SUCCESS;
353 }
354
355 /**
356 Get filter from the input KeywordRequest string.
357
358 This is a internal function.
359
360 @param String KeywordRequestformat string.
361 @param FilterFlags return the filter condition.
362 @param NextString return the next string follow this keyword sectin.
363
364 @retval EFI_SUCCESS Success to get the keyword string.
365 @retval EFI_INVALID_PARAMETER Parsr the input string return error.
366
367 **/
368 BOOLEAN
369 ExtractFilter (
370 IN EFI_STRING String,
371 OUT UINT8 *FilterFlags,
372 OUT EFI_STRING *NextString
373 )
374 {
375 CHAR16 *PathPtr;
376 CHAR16 *KeywordPtr;
377 BOOLEAN RetVal;
378
379 ASSERT ((FilterFlags != NULL) && (NextString != NULL));
380
381 //
382 // String end, no filter section.
383 //
384 if (String == NULL) {
385 *NextString = NULL;
386 return FALSE;
387 }
388
389 *FilterFlags = 0;
390 RetVal = TRUE;
391
392 //
393 // Skip '&' if exist.
394 //
395 if (*String == L'&') {
396 String++;
397 }
398
399 if (StrnCmp (String, L"ReadOnly", StrLen (L"ReadOnly")) == 0) {
400 //
401 // Find ReadOnly filter.
402 //
403 *FilterFlags |= EFI_KEYWORD_FILTER_READONY;
404 String += StrLen (L"ReadOnly");
405 } else if (StrnCmp (String, L"ReadWrite", StrLen (L"ReadWrite")) == 0) {
406 //
407 // Find ReadWrite filter.
408 //
409 *FilterFlags |= EFI_KEYWORD_FILTER_REAWRITE;
410 String += StrLen (L"ReadWrite");
411 } else if (StrnCmp (String, L"Buffer", StrLen (L"Buffer")) == 0) {
412 //
413 // Find Buffer Filter.
414 //
415 *FilterFlags |= EFI_KEYWORD_FILTER_BUFFER;
416 String += StrLen (L"Buffer");
417 } else if (StrnCmp (String, L"Numeric", StrLen (L"Numeric")) == 0) {
418 //
419 // Find Numeric Filter
420 //
421 String += StrLen (L"Numeric");
422 if (*String != L':') {
423 *FilterFlags |= EFI_KEYWORD_FILTER_NUMERIC;
424 } else {
425 String++;
426 switch (*String) {
427 case L'1':
428 *FilterFlags |= EFI_KEYWORD_FILTER_NUMERIC_1;
429 break;
430 case L'2':
431 *FilterFlags |= EFI_KEYWORD_FILTER_NUMERIC_2;
432 break;
433 case L'4':
434 *FilterFlags |= EFI_KEYWORD_FILTER_NUMERIC_4;
435 break;
436 case L'8':
437 *FilterFlags |= EFI_KEYWORD_FILTER_NUMERIC_8;
438 break;
439 default:
440 ASSERT (FALSE);
441 break;
442 }
443 String++;
444 }
445 } else {
446 //
447 // Check whether other filter item defined by Platform.
448 //
449 if ((StrnCmp (String, L"&PATH", StrLen (L"&PATH")) == 0) ||
450 (StrnCmp (String, L"&KEYWORD", StrLen (L"&KEYWORD")) == 0)) {
451 //
452 // New KeywordRequest start, no platform defined filter.
453 //
454 } else {
455 //
456 // Platform defined filter rule.
457 // Just skip platform defined filter rule, return success.
458 //
459 PathPtr = StrStr(String, L"&PATH");
460 KeywordPtr = StrStr(String, L"&KEYWORD");
461 if (PathPtr != NULL && KeywordPtr != NULL) {
462 //
463 // If both sections exist, return the first follow string.
464 //
465 String = KeywordPtr > PathPtr ? PathPtr : KeywordPtr;
466 } else if (PathPtr != NULL) {
467 //
468 // Should not exist PathPtr != NULL && KeywordPtr == NULL case.
469 //
470 ASSERT (FALSE);
471 } else if (KeywordPtr != NULL) {
472 //
473 // Just to the next keyword section.
474 //
475 String = KeywordPtr;
476 } else {
477 //
478 // Only has paltform defined filter section, just skip it.
479 //
480 String += StrLen (String);
481 }
482 }
483 RetVal = FALSE;
484 }
485
486 *NextString = String;
487
488 return RetVal;
489 }
490
491 /**
492 Extract Readonly flag from opcode.
493
494 This is a internal function.
495
496 @param OpCodeData Input opcode for this question.
497
498 @retval TRUE This question is readonly.
499 @retval FALSE This question is not readonly.
500
501 **/
502 BOOLEAN
503 ExtractReadOnlyFromOpCode (
504 IN UINT8 *OpCodeData
505 )
506 {
507 EFI_IFR_QUESTION_HEADER *QuestionHdr;
508
509 ASSERT (OpCodeData != NULL);
510
511 QuestionHdr = (EFI_IFR_QUESTION_HEADER *) (OpCodeData + sizeof (EFI_IFR_OP_HEADER));
512
513 return (QuestionHdr->Flags & EFI_IFR_FLAG_READ_ONLY) != 0;
514 }
515
516 /**
517 Create a circuit to check the filter section.
518
519 This is a internal function.
520
521 @param OpCodeData The questin binary ifr data.
522 @param KeywordRequest KeywordRequestformat string.
523 @param NextString return the next string follow this keyword sectin.
524 @param ReadOnly Return whether this question is read only.
525
526 @retval KEYWORD_HANDLER_NO_ERROR Success validate.
527 @retval KEYWORD_HANDLER_INCOMPATIBLE_VALUE_DETECTED Validate fail.
528
529 **/
530 UINT32
531 ValidateFilter (
532 IN UINT8 *OpCodeData,
533 IN CHAR16 *KeywordRequest,
534 OUT CHAR16 **NextString,
535 OUT BOOLEAN *ReadOnly
536 )
537 {
538 CHAR16 *NextFilter;
539 CHAR16 *StringPtr;
540 UINT8 FilterFlags;
541 EFI_IFR_QUESTION_HEADER *QuestionHdr;
542 EFI_IFR_OP_HEADER *OpCodeHdr;
543 UINT8 Flags;
544 UINT32 RetVal;
545
546 RetVal = KEYWORD_HANDLER_NO_ERROR;
547 StringPtr = KeywordRequest;
548
549 OpCodeHdr = (EFI_IFR_OP_HEADER *) OpCodeData;
550 QuestionHdr = (EFI_IFR_QUESTION_HEADER *) (OpCodeData + sizeof (EFI_IFR_OP_HEADER));
551 if (OpCodeHdr->OpCode == EFI_IFR_ONE_OF_OP || OpCodeHdr->OpCode == EFI_IFR_NUMERIC_OP) {
552 Flags = *(OpCodeData + sizeof (EFI_IFR_OP_HEADER) + sizeof (EFI_IFR_QUESTION_HEADER));
553 } else {
554 Flags = 0;
555 }
556
557 //
558 // Get ReadOnly flag from Question.
559 //
560 *ReadOnly = ExtractReadOnlyFromOpCode(OpCodeData);
561
562 while (ExtractFilter (StringPtr, &FilterFlags, &NextFilter)) {
563 switch (FilterFlags) {
564 case EFI_KEYWORD_FILTER_READONY:
565 if ((QuestionHdr->Flags & EFI_IFR_FLAG_READ_ONLY) == 0) {
566 RetVal = KEYWORD_HANDLER_INCOMPATIBLE_VALUE_DETECTED;
567 goto Done;
568 }
569 break;
570
571 case EFI_KEYWORD_FILTER_REAWRITE:
572 if ((QuestionHdr->Flags & EFI_IFR_FLAG_READ_ONLY) != 0) {
573 RetVal = KEYWORD_HANDLER_INCOMPATIBLE_VALUE_DETECTED;
574 goto Done;
575 }
576 break;
577
578 case EFI_KEYWORD_FILTER_BUFFER:
579 //
580 // Only these three opcode use numeric value type.
581 //
582 if (OpCodeHdr->OpCode == EFI_IFR_ONE_OF_OP || OpCodeHdr->OpCode == EFI_IFR_NUMERIC_OP || OpCodeHdr->OpCode == EFI_IFR_CHECKBOX_OP) {
583 RetVal = KEYWORD_HANDLER_INCOMPATIBLE_VALUE_DETECTED;
584 goto Done;
585 }
586 break;
587
588 case EFI_KEYWORD_FILTER_NUMERIC:
589 if (OpCodeHdr->OpCode != EFI_IFR_ONE_OF_OP && OpCodeHdr->OpCode != EFI_IFR_NUMERIC_OP && OpCodeHdr->OpCode != EFI_IFR_CHECKBOX_OP) {
590 RetVal = KEYWORD_HANDLER_INCOMPATIBLE_VALUE_DETECTED;
591 goto Done;
592 }
593 break;
594
595 case EFI_KEYWORD_FILTER_NUMERIC_1:
596 case EFI_KEYWORD_FILTER_NUMERIC_2:
597 case EFI_KEYWORD_FILTER_NUMERIC_4:
598 case EFI_KEYWORD_FILTER_NUMERIC_8:
599 if (OpCodeHdr->OpCode != EFI_IFR_ONE_OF_OP && OpCodeHdr->OpCode != EFI_IFR_NUMERIC_OP && OpCodeHdr->OpCode != EFI_IFR_CHECKBOX_OP) {
600 RetVal = KEYWORD_HANDLER_INCOMPATIBLE_VALUE_DETECTED;
601 goto Done;
602 }
603
604 //
605 // For numeric and oneof, it has flags field to specify the detail numeric type.
606 //
607 if (OpCodeHdr->OpCode == EFI_IFR_ONE_OF_OP || OpCodeHdr->OpCode == EFI_IFR_NUMERIC_OP) {
608 switch (Flags & EFI_IFR_NUMERIC_SIZE) {
609 case EFI_IFR_NUMERIC_SIZE_1:
610 if (FilterFlags != EFI_KEYWORD_FILTER_NUMERIC_1) {
611 RetVal = KEYWORD_HANDLER_INCOMPATIBLE_VALUE_DETECTED;
612 goto Done;
613 }
614 break;
615
616 case EFI_IFR_NUMERIC_SIZE_2:
617 if (FilterFlags != EFI_KEYWORD_FILTER_NUMERIC_2) {
618 RetVal = KEYWORD_HANDLER_INCOMPATIBLE_VALUE_DETECTED;
619 goto Done;
620 }
621 break;
622
623 case EFI_IFR_NUMERIC_SIZE_4:
624 if (FilterFlags != EFI_KEYWORD_FILTER_NUMERIC_4) {
625 RetVal = KEYWORD_HANDLER_INCOMPATIBLE_VALUE_DETECTED;
626 goto Done;
627 }
628 break;
629
630 case EFI_IFR_NUMERIC_SIZE_8:
631 if (FilterFlags != EFI_KEYWORD_FILTER_NUMERIC_8) {
632 RetVal = KEYWORD_HANDLER_INCOMPATIBLE_VALUE_DETECTED;
633 goto Done;
634 }
635 break;
636
637 default:
638 ASSERT (FALSE);
639 break;
640 }
641 }
642 break;
643
644 default:
645 ASSERT (FALSE);
646 break;
647 }
648
649 //
650 // Jump to the next filter.
651 //
652 StringPtr = NextFilter;
653 }
654
655 Done:
656 //
657 // The current filter which is processing.
658 //
659 *NextString = StringPtr;
660
661 return RetVal;
662 }
663
664 /**
665 Get HII_DATABASE_RECORD from the input device path info.
666
667 This is a internal function.
668
669 @param DevicePath UEFI device path protocol.
670
671 @retval Internal data base record.
672
673 **/
674 HII_DATABASE_RECORD *
675 GetRecordFromDevicePath (
676 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath
677 )
678 {
679 LIST_ENTRY *Link;
680 UINT8 *DevicePathPkg;
681 UINT8 *CurrentDevicePath;
682 UINTN DevicePathSize;
683 HII_DATABASE_RECORD *TempDatabase;
684
685 ASSERT (DevicePath != NULL);
686
687 for (Link = mPrivate.DatabaseList.ForwardLink; Link != &mPrivate.DatabaseList; Link = Link->ForwardLink) {
688 TempDatabase = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
689 DevicePathPkg = TempDatabase->PackageList->DevicePathPkg;
690 if (DevicePathPkg != NULL) {
691 CurrentDevicePath = DevicePathPkg + sizeof (EFI_HII_PACKAGE_HEADER);
692 DevicePathSize = GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) CurrentDevicePath);
693 if ((CompareMem (DevicePath, CurrentDevicePath, DevicePathSize) == 0)) {
694 return TempDatabase;
695 }
696 }
697 }
698
699 return NULL;
700 }
701
702 /**
703 Calculate the size of StringSrc and output it. Also copy string text from src
704 to dest.
705
706 This is a internal function.
707
708 @param StringSrc Points to current null-terminated string.
709 @param BufferSize Length of the buffer.
710 @param StringDest Buffer to store the string text.
711
712 @retval EFI_SUCCESS The string text was outputed successfully.
713 @retval EFI_OUT_OF_RESOURCES Out of resource.
714
715 **/
716 EFI_STATUS
717 GetUnicodeStringTextAndSize (
718 IN UINT8 *StringSrc,
719 OUT UINTN *BufferSize,
720 OUT EFI_STRING *StringDest
721 )
722 {
723 UINTN StringSize;
724 UINT8 *StringPtr;
725
726 ASSERT (StringSrc != NULL && BufferSize != NULL && StringDest != NULL);
727
728 StringSize = sizeof (CHAR16);
729 StringPtr = StringSrc;
730 while (ReadUnaligned16 ((UINT16 *) StringPtr) != 0) {
731 StringSize += sizeof (CHAR16);
732 StringPtr += sizeof (CHAR16);
733 }
734
735 *StringDest = AllocatePool (StringSize);
736 if (*StringDest == NULL) {
737 return EFI_OUT_OF_RESOURCES;
738 }
739
740 CopyMem (*StringDest, StringSrc, StringSize);
741
742 *BufferSize = StringSize;
743 return EFI_SUCCESS;
744 }
745
746 /**
747 Find the string id for the input keyword.
748
749 @param StringPackage Hii string package instance.
750 @param KeywordValue Input keyword value.
751 @param StringId The string's id, which is unique within PackageList.
752
753
754 @retval EFI_SUCCESS The string text and font is retrieved
755 successfully.
756 @retval EFI_NOT_FOUND The specified text or font info can not be found
757 out.
758 @retval EFI_OUT_OF_RESOURCES The system is out of resources to accomplish the
759 task.
760 **/
761 EFI_STATUS
762 GetStringIdFromString (
763 IN HII_STRING_PACKAGE_INSTANCE *StringPackage,
764 IN CHAR16 *KeywordValue,
765 OUT EFI_STRING_ID *StringId
766 )
767 {
768 UINT8 *BlockHdr;
769 EFI_STRING_ID CurrentStringId;
770 UINTN BlockSize;
771 UINTN Index;
772 UINT8 *StringTextPtr;
773 UINTN Offset;
774 UINT16 StringCount;
775 UINT16 SkipCount;
776 UINT8 Length8;
777 EFI_HII_SIBT_EXT2_BLOCK Ext2;
778 UINT32 Length32;
779 UINTN StringSize;
780 CHAR16 *String;
781 CHAR8 *AsciiKeywordValue;
782 EFI_STATUS Status;
783
784 ASSERT (StringPackage != NULL && KeywordValue != NULL && StringId != NULL);
785 ASSERT (StringPackage->Signature == HII_STRING_PACKAGE_SIGNATURE);
786
787 CurrentStringId = 1;
788 Status = EFI_SUCCESS;
789 String = NULL;
790 BlockHdr = StringPackage->StringBlock;
791 BlockSize = 0;
792 Offset = 0;
793
794 //
795 // Make a ascii keyword value for later use.
796 //
797 AsciiKeywordValue = AllocatePool (StrLen (KeywordValue) + 1);
798 if (AsciiKeywordValue == NULL) {
799 return EFI_OUT_OF_RESOURCES;
800 }
801 UnicodeStrToAsciiStr(KeywordValue, AsciiKeywordValue);
802
803 while (*BlockHdr != EFI_HII_SIBT_END) {
804 switch (*BlockHdr) {
805 case EFI_HII_SIBT_STRING_SCSU:
806 Offset = sizeof (EFI_HII_STRING_BLOCK);
807 StringTextPtr = BlockHdr + Offset;
808 BlockSize += Offset + AsciiStrSize ((CHAR8 *) StringTextPtr);
809 if (AsciiStrCmp(AsciiKeywordValue, (CHAR8 *) StringTextPtr) == 0) {
810 *StringId = CurrentStringId;
811 goto Done;
812 }
813 CurrentStringId++;
814 break;
815
816 case EFI_HII_SIBT_STRING_SCSU_FONT:
817 Offset = sizeof (EFI_HII_SIBT_STRING_SCSU_FONT_BLOCK) - sizeof (UINT8);
818 StringTextPtr = BlockHdr + Offset;
819 if (AsciiStrCmp(AsciiKeywordValue, (CHAR8 *) StringTextPtr) == 0) {
820 *StringId = CurrentStringId;
821 goto Done;
822 }
823 BlockSize += Offset + AsciiStrSize ((CHAR8 *) StringTextPtr);
824 CurrentStringId++;
825 break;
826
827 case EFI_HII_SIBT_STRINGS_SCSU:
828 CopyMem (&StringCount, BlockHdr + sizeof (EFI_HII_STRING_BLOCK), sizeof (UINT16));
829 StringTextPtr = (UINT8*)((UINTN)BlockHdr + sizeof (EFI_HII_SIBT_STRINGS_SCSU_BLOCK) - sizeof (UINT8));
830 BlockSize += StringTextPtr - BlockHdr;
831
832 for (Index = 0; Index < StringCount; Index++) {
833 BlockSize += AsciiStrSize ((CHAR8 *) StringTextPtr);
834 if (AsciiStrCmp(AsciiKeywordValue, (CHAR8 *) StringTextPtr) == 0) {
835 *StringId = CurrentStringId;
836 goto Done;
837 }
838 StringTextPtr = StringTextPtr + AsciiStrSize ((CHAR8 *) StringTextPtr);
839 CurrentStringId++;
840 }
841 break;
842
843 case EFI_HII_SIBT_STRINGS_SCSU_FONT:
844 CopyMem (
845 &StringCount,
846 (UINT8*)((UINTN)BlockHdr + sizeof (EFI_HII_STRING_BLOCK) + sizeof (UINT8)),
847 sizeof (UINT16)
848 );
849 StringTextPtr = (UINT8*)((UINTN)BlockHdr + sizeof (EFI_HII_SIBT_STRINGS_SCSU_FONT_BLOCK) - sizeof (UINT8));
850 BlockSize += StringTextPtr - BlockHdr;
851
852 for (Index = 0; Index < StringCount; Index++) {
853 BlockSize += AsciiStrSize ((CHAR8 *) StringTextPtr);
854 if (AsciiStrCmp(AsciiKeywordValue, (CHAR8 *) StringTextPtr) == 0) {
855 *StringId = CurrentStringId;
856 goto Done;
857 }
858 StringTextPtr = StringTextPtr + AsciiStrSize ((CHAR8 *) StringTextPtr);
859 CurrentStringId++;
860 }
861 break;
862
863 case EFI_HII_SIBT_STRING_UCS2:
864 Offset = sizeof (EFI_HII_STRING_BLOCK);
865 StringTextPtr = BlockHdr + Offset;
866 //
867 // Use StringSize to store the size of the specified string, including the NULL
868 // terminator.
869 //
870 Status = GetUnicodeStringTextAndSize (StringTextPtr, &StringSize, &String);
871 if (EFI_ERROR (Status)) {
872 goto Done;
873 }
874
875 if (StrCmp(KeywordValue, String) == 0) {
876 *StringId = CurrentStringId;
877 goto Done;
878 }
879 BlockSize += Offset + StringSize;
880 CurrentStringId++;
881 break;
882
883 case EFI_HII_SIBT_STRING_UCS2_FONT:
884 Offset = sizeof (EFI_HII_SIBT_STRING_UCS2_FONT_BLOCK) - sizeof (CHAR16);
885 StringTextPtr = BlockHdr + Offset;
886 //
887 // Use StringSize to store the size of the specified string, including the NULL
888 // terminator.
889 //
890 Status = GetUnicodeStringTextAndSize (StringTextPtr, &StringSize, &String);
891 if (EFI_ERROR (Status)) {
892 goto Done;
893 }
894
895 if (StrCmp(KeywordValue, String) == 0) {
896 *StringId = CurrentStringId;
897 goto Done;
898 }
899 BlockSize += Offset + StringSize;
900 CurrentStringId++;
901 break;
902
903 case EFI_HII_SIBT_STRINGS_UCS2:
904 Offset = sizeof (EFI_HII_SIBT_STRINGS_UCS2_BLOCK) - sizeof (CHAR16);
905 StringTextPtr = BlockHdr + Offset;
906 BlockSize += Offset;
907 CopyMem (&StringCount, BlockHdr + sizeof (EFI_HII_STRING_BLOCK), sizeof (UINT16));
908 for (Index = 0; Index < StringCount; Index++) {
909 Status = GetUnicodeStringTextAndSize (StringTextPtr, &StringSize, &String);
910 if (EFI_ERROR (Status)) {
911 goto Done;
912 }
913
914 BlockSize += StringSize;
915 if (StrCmp(KeywordValue, String) == 0) {
916 *StringId = CurrentStringId;
917 goto Done;
918 }
919 StringTextPtr = StringTextPtr + StringSize;
920 CurrentStringId++;
921 }
922 break;
923
924 case EFI_HII_SIBT_STRINGS_UCS2_FONT:
925 Offset = sizeof (EFI_HII_SIBT_STRINGS_UCS2_FONT_BLOCK) - sizeof (CHAR16);
926 StringTextPtr = BlockHdr + Offset;
927 BlockSize += Offset;
928 CopyMem (
929 &StringCount,
930 (UINT8*)((UINTN)BlockHdr + sizeof (EFI_HII_STRING_BLOCK) + sizeof (UINT8)),
931 sizeof (UINT16)
932 );
933 for (Index = 0; Index < StringCount; Index++) {
934 Status = GetUnicodeStringTextAndSize (StringTextPtr, &StringSize, &String);
935 if (EFI_ERROR (Status)) {
936 goto Done;
937 }
938
939 BlockSize += StringSize;
940 if (StrCmp(KeywordValue, String) == 0) {
941 *StringId = CurrentStringId;
942 goto Done;
943 }
944 StringTextPtr = StringTextPtr + StringSize;
945 CurrentStringId++;
946 }
947 break;
948
949 case EFI_HII_SIBT_DUPLICATE:
950 BlockSize += sizeof (EFI_HII_SIBT_DUPLICATE_BLOCK);
951 CurrentStringId++;
952 break;
953
954 case EFI_HII_SIBT_SKIP1:
955 SkipCount = (UINT16) (*(UINT8*)((UINTN)BlockHdr + sizeof (EFI_HII_STRING_BLOCK)));
956 CurrentStringId = (UINT16) (CurrentStringId + SkipCount);
957 BlockSize += sizeof (EFI_HII_SIBT_SKIP1_BLOCK);
958 break;
959
960 case EFI_HII_SIBT_SKIP2:
961 CopyMem (&SkipCount, BlockHdr + sizeof (EFI_HII_STRING_BLOCK), sizeof (UINT16));
962 CurrentStringId = (UINT16) (CurrentStringId + SkipCount);
963 BlockSize += sizeof (EFI_HII_SIBT_SKIP2_BLOCK);
964 break;
965
966 case EFI_HII_SIBT_EXT1:
967 CopyMem (
968 &Length8,
969 (UINT8*)((UINTN)BlockHdr + sizeof (EFI_HII_STRING_BLOCK) + sizeof (UINT8)),
970 sizeof (UINT8)
971 );
972 BlockSize += Length8;
973 break;
974
975 case EFI_HII_SIBT_EXT2:
976 CopyMem (&Ext2, BlockHdr, sizeof (EFI_HII_SIBT_EXT2_BLOCK));
977 BlockSize += Ext2.Length;
978 break;
979
980 case EFI_HII_SIBT_EXT4:
981 CopyMem (
982 &Length32,
983 (UINT8*)((UINTN)BlockHdr + sizeof (EFI_HII_STRING_BLOCK) + sizeof (UINT8)),
984 sizeof (UINT32)
985 );
986
987 BlockSize += Length32;
988 break;
989
990 default:
991 break;
992 }
993
994 if (String != NULL) {
995 FreePool (String);
996 String = NULL;
997 }
998
999 BlockHdr = StringPackage->StringBlock + BlockSize;
1000 }
1001
1002 Status = EFI_NOT_FOUND;
1003
1004 Done:
1005 if (AsciiKeywordValue != NULL) {
1006 FreePool (AsciiKeywordValue);
1007 }
1008 if (String != NULL) {
1009 FreePool (String);
1010 }
1011 return Status;
1012 }
1013
1014 /**
1015 Find the next valid string id for the input string id.
1016
1017 @param StringPackage Hii string package instance.
1018 @param StringId The current string id which is already got.
1019 1 means just begin to get the string id.
1020 @param KeywordValue Return the string for the next string id.
1021
1022
1023 @retval EFI_STRING_ID Not 0 means a valid stringid found.
1024 0 means not found a valid string id.
1025 **/
1026 EFI_STRING_ID
1027 GetNextStringId (
1028 IN HII_STRING_PACKAGE_INSTANCE *StringPackage,
1029 IN EFI_STRING_ID StringId,
1030 OUT EFI_STRING *KeywordValue
1031 )
1032 {
1033 UINT8 *BlockHdr;
1034 EFI_STRING_ID CurrentStringId;
1035 UINTN BlockSize;
1036 UINTN Index;
1037 UINT8 *StringTextPtr;
1038 UINTN Offset;
1039 UINT16 StringCount;
1040 UINT16 SkipCount;
1041 UINT8 Length8;
1042 EFI_HII_SIBT_EXT2_BLOCK Ext2;
1043 UINT32 Length32;
1044 BOOLEAN FindString;
1045 UINTN StringSize;
1046 CHAR16 *String;
1047
1048 ASSERT (StringPackage != NULL);
1049 ASSERT (StringPackage->Signature == HII_STRING_PACKAGE_SIGNATURE);
1050
1051 CurrentStringId = 1;
1052 FindString = FALSE;
1053 String = NULL;
1054
1055 //
1056 // Parse the string blocks to get the string text and font.
1057 //
1058 BlockHdr = StringPackage->StringBlock;
1059 BlockSize = 0;
1060 Offset = 0;
1061 while (*BlockHdr != EFI_HII_SIBT_END) {
1062 switch (*BlockHdr) {
1063 case EFI_HII_SIBT_STRING_SCSU:
1064 Offset = sizeof (EFI_HII_STRING_BLOCK);
1065 StringTextPtr = BlockHdr + Offset;
1066
1067 if (FindString) {
1068 *KeywordValue = AllocatePool (AsciiStrSize ((CHAR8 *) StringTextPtr) * sizeof (CHAR16));
1069 if (*KeywordValue == NULL) {
1070 return 0;
1071 }
1072 AsciiStrToUnicodeStr ((CHAR8 *) StringTextPtr, *KeywordValue);
1073 return CurrentStringId;
1074 } else if (CurrentStringId == StringId) {
1075 FindString = TRUE;
1076 }
1077
1078 BlockSize += Offset + AsciiStrSize ((CHAR8 *) StringTextPtr);
1079 CurrentStringId++;
1080 break;
1081
1082 case EFI_HII_SIBT_STRING_SCSU_FONT:
1083 Offset = sizeof (EFI_HII_SIBT_STRING_SCSU_FONT_BLOCK) - sizeof (UINT8);
1084 StringTextPtr = BlockHdr + Offset;
1085
1086 if (FindString) {
1087 *KeywordValue = AllocatePool (AsciiStrSize ((CHAR8 *) StringTextPtr) * sizeof (CHAR16));
1088 if (*KeywordValue == NULL) {
1089 return 0;
1090 }
1091 AsciiStrToUnicodeStr ((CHAR8 *) StringTextPtr, *KeywordValue);
1092 return CurrentStringId;
1093 } else if (CurrentStringId == StringId) {
1094 FindString = TRUE;
1095 }
1096
1097 BlockSize += Offset + AsciiStrSize ((CHAR8 *) StringTextPtr);
1098 CurrentStringId++;
1099 break;
1100
1101 case EFI_HII_SIBT_STRINGS_SCSU:
1102 CopyMem (&StringCount, BlockHdr + sizeof (EFI_HII_STRING_BLOCK), sizeof (UINT16));
1103 StringTextPtr = (UINT8*)((UINTN)BlockHdr + sizeof (EFI_HII_SIBT_STRINGS_SCSU_BLOCK) - sizeof (UINT8));
1104 BlockSize += StringTextPtr - BlockHdr;
1105
1106 for (Index = 0; Index < StringCount; Index++) {
1107 if (FindString) {
1108 *KeywordValue = AllocatePool (AsciiStrSize ((CHAR8 *) StringTextPtr) * sizeof (CHAR16));
1109 if (*KeywordValue == NULL) {
1110 return 0;
1111 }
1112 AsciiStrToUnicodeStr ((CHAR8 *) StringTextPtr, *KeywordValue);
1113 return CurrentStringId;
1114 } else if (CurrentStringId == StringId) {
1115 FindString = TRUE;
1116 }
1117
1118 BlockSize += AsciiStrSize ((CHAR8 *) StringTextPtr);
1119 StringTextPtr = StringTextPtr + AsciiStrSize ((CHAR8 *) StringTextPtr);
1120 CurrentStringId++;
1121 }
1122 break;
1123
1124 case EFI_HII_SIBT_STRINGS_SCSU_FONT:
1125 CopyMem (
1126 &StringCount,
1127 (UINT8*)((UINTN)BlockHdr + sizeof (EFI_HII_STRING_BLOCK) + sizeof (UINT8)),
1128 sizeof (UINT16)
1129 );
1130 StringTextPtr = (UINT8*)((UINTN)BlockHdr + sizeof (EFI_HII_SIBT_STRINGS_SCSU_FONT_BLOCK) - sizeof (UINT8));
1131 BlockSize += StringTextPtr - BlockHdr;
1132
1133 for (Index = 0; Index < StringCount; Index++) {
1134 if (FindString) {
1135 *KeywordValue = AllocatePool (AsciiStrSize ((CHAR8 *) StringTextPtr) * sizeof (CHAR16));
1136 if (*KeywordValue == NULL) {
1137 return 0;
1138 }
1139 AsciiStrToUnicodeStr ((CHAR8 *) StringTextPtr, *KeywordValue);
1140 return CurrentStringId;
1141 } else if (CurrentStringId == StringId) {
1142 FindString = TRUE;
1143 }
1144
1145 BlockSize += AsciiStrSize ((CHAR8 *) StringTextPtr);
1146 StringTextPtr = StringTextPtr + AsciiStrSize ((CHAR8 *) StringTextPtr);
1147 CurrentStringId++;
1148 }
1149 break;
1150
1151 case EFI_HII_SIBT_STRING_UCS2:
1152 Offset = sizeof (EFI_HII_STRING_BLOCK);
1153 StringTextPtr = BlockHdr + Offset;
1154 //
1155 // Use StringSize to store the size of the specified string, including the NULL
1156 // terminator.
1157 //
1158 GetUnicodeStringTextAndSize (StringTextPtr, &StringSize, &String);
1159 if (FindString && (String != NULL) && (*String != L'\0')) {
1160 //
1161 // String protocol use this type for the string id which has value for other package.
1162 // It will allocate an empty string block for this string id. so here we also check
1163 // *String != L'\0' to prohibit this case.
1164 //
1165 *KeywordValue = String;
1166 return CurrentStringId;
1167 } else if (CurrentStringId == StringId) {
1168 FindString = TRUE;
1169 }
1170
1171 BlockSize += Offset + StringSize;
1172 CurrentStringId++;
1173 break;
1174
1175 case EFI_HII_SIBT_STRING_UCS2_FONT:
1176 Offset = sizeof (EFI_HII_SIBT_STRING_UCS2_FONT_BLOCK) - sizeof (CHAR16);
1177 StringTextPtr = BlockHdr + Offset;
1178 //
1179 // Use StringSize to store the size of the specified string, including the NULL
1180 // terminator.
1181 //
1182 GetUnicodeStringTextAndSize (StringTextPtr, &StringSize, &String);
1183 if (FindString) {
1184 *KeywordValue = String;
1185 return CurrentStringId;
1186 } else if (CurrentStringId == StringId) {
1187 FindString = TRUE;
1188 }
1189
1190 BlockSize += Offset + StringSize;
1191 CurrentStringId++;
1192 break;
1193
1194 case EFI_HII_SIBT_STRINGS_UCS2:
1195 Offset = sizeof (EFI_HII_SIBT_STRINGS_UCS2_BLOCK) - sizeof (CHAR16);
1196 StringTextPtr = BlockHdr + Offset;
1197 BlockSize += Offset;
1198 CopyMem (&StringCount, BlockHdr + sizeof (EFI_HII_STRING_BLOCK), sizeof (UINT16));
1199 for (Index = 0; Index < StringCount; Index++) {
1200 GetUnicodeStringTextAndSize (StringTextPtr, &StringSize, &String);
1201
1202 if (FindString) {
1203 *KeywordValue = String;
1204 return CurrentStringId;
1205 } else if (CurrentStringId == StringId) {
1206 FindString = TRUE;
1207 }
1208
1209 BlockSize += StringSize;
1210 StringTextPtr = StringTextPtr + StringSize;
1211 CurrentStringId++;
1212 }
1213 break;
1214
1215 case EFI_HII_SIBT_STRINGS_UCS2_FONT:
1216 Offset = sizeof (EFI_HII_SIBT_STRINGS_UCS2_FONT_BLOCK) - sizeof (CHAR16);
1217 StringTextPtr = BlockHdr + Offset;
1218 BlockSize += Offset;
1219 CopyMem (
1220 &StringCount,
1221 (UINT8*)((UINTN)BlockHdr + sizeof (EFI_HII_STRING_BLOCK) + sizeof (UINT8)),
1222 sizeof (UINT16)
1223 );
1224 for (Index = 0; Index < StringCount; Index++) {
1225 GetUnicodeStringTextAndSize (StringTextPtr, &StringSize, &String);
1226 if (FindString) {
1227 *KeywordValue = String;
1228 return CurrentStringId;
1229 } else if (CurrentStringId == StringId) {
1230 FindString = TRUE;
1231 }
1232
1233 BlockSize += StringSize;
1234 StringTextPtr = StringTextPtr + StringSize;
1235 CurrentStringId++;
1236 }
1237 break;
1238
1239 case EFI_HII_SIBT_DUPLICATE:
1240 BlockSize += sizeof (EFI_HII_SIBT_DUPLICATE_BLOCK);
1241 CurrentStringId++;
1242 break;
1243
1244 case EFI_HII_SIBT_SKIP1:
1245 SkipCount = (UINT16) (*(UINT8*)((UINTN)BlockHdr + sizeof (EFI_HII_STRING_BLOCK)));
1246 CurrentStringId = (UINT16) (CurrentStringId + SkipCount);
1247 BlockSize += sizeof (EFI_HII_SIBT_SKIP1_BLOCK);
1248 break;
1249
1250 case EFI_HII_SIBT_SKIP2:
1251 CopyMem (&SkipCount, BlockHdr + sizeof (EFI_HII_STRING_BLOCK), sizeof (UINT16));
1252 CurrentStringId = (UINT16) (CurrentStringId + SkipCount);
1253 BlockSize += sizeof (EFI_HII_SIBT_SKIP2_BLOCK);
1254 break;
1255
1256 case EFI_HII_SIBT_EXT1:
1257 CopyMem (
1258 &Length8,
1259 (UINT8*)((UINTN)BlockHdr + sizeof (EFI_HII_STRING_BLOCK) + sizeof (UINT8)),
1260 sizeof (UINT8)
1261 );
1262 BlockSize += Length8;
1263 break;
1264
1265 case EFI_HII_SIBT_EXT2:
1266 CopyMem (&Ext2, BlockHdr, sizeof (EFI_HII_SIBT_EXT2_BLOCK));
1267 BlockSize += Ext2.Length;
1268 break;
1269
1270 case EFI_HII_SIBT_EXT4:
1271 CopyMem (
1272 &Length32,
1273 (UINT8*)((UINTN)BlockHdr + sizeof (EFI_HII_STRING_BLOCK) + sizeof (UINT8)),
1274 sizeof (UINT32)
1275 );
1276
1277 BlockSize += Length32;
1278 break;
1279
1280 default:
1281 break;
1282 }
1283
1284 if (String != NULL) {
1285 FreePool (String);
1286 String = NULL;
1287 }
1288
1289 BlockHdr = StringPackage->StringBlock + BlockSize;
1290 }
1291
1292 return 0;
1293 }
1294
1295 /**
1296 Get string package from the input NameSpace string.
1297
1298 This is a internal function.
1299
1300 @param DatabaseRecord HII_DATABASE_RECORD format string.
1301 @param NameSpace NameSpace format string.
1302 @param KeywordValue Keyword value.
1303 @param StringId String Id for this keyword.
1304
1305 @retval KEYWORD_HANDLER_NO_ERROR Get String id succes.
1306 @retval KEYWORD_HANDLER_KEYWORD_NOT_FOUND Not found the string id in the string package.
1307 @retval KEYWORD_HANDLER_NAMESPACE_ID_NOT_FOUND Not found the string package for this namespace.
1308 @retval KEYWORD_HANDLER_UNDEFINED_PROCESSING_ERROR Out of resource error.
1309
1310 **/
1311 UINT32
1312 GetStringIdFromRecord (
1313 IN HII_DATABASE_RECORD *DatabaseRecord,
1314 IN CHAR8 **NameSpace,
1315 IN CHAR16 *KeywordValue,
1316 OUT EFI_STRING_ID *StringId
1317 )
1318 {
1319 LIST_ENTRY *Link;
1320 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;
1321 HII_STRING_PACKAGE_INSTANCE *StringPackage;
1322 EFI_STATUS Status;
1323 CHAR8 *Name;
1324 UINT32 RetVal;
1325
1326 ASSERT (DatabaseRecord != NULL && NameSpace != NULL && KeywordValue != NULL);
1327
1328 PackageListNode = DatabaseRecord->PackageList;
1329 RetVal = KEYWORD_HANDLER_NAMESPACE_ID_NOT_FOUND;
1330
1331 if (*NameSpace != NULL) {
1332 Name = *NameSpace;
1333 } else {
1334 Name = UEFI_CONFIG_LANG;
1335 }
1336
1337 for (Link = PackageListNode->StringPkgHdr.ForwardLink; Link != &PackageListNode->StringPkgHdr; Link = Link->ForwardLink) {
1338 StringPackage = CR (Link, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE);
1339
1340 if (AsciiStrnCmp(Name, StringPackage->StringPkgHdr->Language, AsciiStrLen (Name)) == 0) {
1341 Status = GetStringIdFromString (StringPackage, KeywordValue, StringId);
1342 if (EFI_ERROR (Status)) {
1343 RetVal = KEYWORD_HANDLER_KEYWORD_NOT_FOUND;
1344 continue;
1345 } else {
1346 if (*NameSpace == NULL) {
1347 *NameSpace = AllocateCopyPool (AsciiStrSize (StringPackage->StringPkgHdr->Language), StringPackage->StringPkgHdr->Language);
1348 if (*NameSpace == NULL) {
1349 return KEYWORD_HANDLER_UNDEFINED_PROCESSING_ERROR;
1350 }
1351 }
1352 return KEYWORD_HANDLER_NO_ERROR;
1353 }
1354 }
1355 }
1356
1357 return RetVal;
1358 }
1359
1360 /**
1361 Tell whether this Operand is an Statement OpCode.
1362
1363 @param Operand Operand of an IFR OpCode.
1364
1365 @retval TRUE This is an Statement OpCode.
1366 @retval FALSE Not an Statement OpCode.
1367
1368 **/
1369 BOOLEAN
1370 IsStatementOpCode (
1371 IN UINT8 Operand
1372 )
1373 {
1374 if ((Operand == EFI_IFR_SUBTITLE_OP) ||
1375 (Operand == EFI_IFR_TEXT_OP) ||
1376 (Operand == EFI_IFR_RESET_BUTTON_OP) ||
1377 (Operand == EFI_IFR_REF_OP) ||
1378 (Operand == EFI_IFR_ACTION_OP) ||
1379 (Operand == EFI_IFR_NUMERIC_OP) ||
1380 (Operand == EFI_IFR_ORDERED_LIST_OP) ||
1381 (Operand == EFI_IFR_CHECKBOX_OP) ||
1382 (Operand == EFI_IFR_STRING_OP) ||
1383 (Operand == EFI_IFR_PASSWORD_OP) ||
1384 (Operand == EFI_IFR_DATE_OP) ||
1385 (Operand == EFI_IFR_TIME_OP) ||
1386 (Operand == EFI_IFR_GUID_OP) ||
1387 (Operand == EFI_IFR_ONE_OF_OP)) {
1388 return TRUE;
1389 }
1390
1391 return FALSE;
1392 }
1393
1394 /**
1395 Tell whether this Operand is an Statement OpCode.
1396
1397 @param Operand Operand of an IFR OpCode.
1398
1399 @retval TRUE This is an Statement OpCode.
1400 @retval FALSE Not an Statement OpCode.
1401
1402 **/
1403 BOOLEAN
1404 IsStorageOpCode (
1405 IN UINT8 Operand
1406 )
1407 {
1408 if ((Operand == EFI_IFR_VARSTORE_OP) ||
1409 (Operand == EFI_IFR_VARSTORE_NAME_VALUE_OP) ||
1410 (Operand == EFI_IFR_VARSTORE_EFI_OP)) {
1411 return TRUE;
1412 }
1413
1414 return FALSE;
1415 }
1416
1417 /**
1418 Base on the prompt string id to find the question.
1419
1420 @param FormPackage The input form package.
1421 @param KeywordStrId The input prompt string id for one question.
1422
1423 @retval the opcode for the question.
1424
1425 **/
1426 UINT8 *
1427 FindQuestionFromStringId (
1428 IN HII_IFR_PACKAGE_INSTANCE *FormPackage,
1429 IN EFI_STRING_ID KeywordStrId
1430 )
1431 {
1432 UINT8 *OpCodeData;
1433 UINT32 Offset;
1434 EFI_IFR_STATEMENT_HEADER *StatementHeader;
1435 EFI_IFR_OP_HEADER *OpCodeHeader;
1436 UINT32 FormDataLen;
1437
1438 ASSERT (FormPackage != NULL);
1439
1440 FormDataLen = FormPackage->FormPkgHdr.Length - sizeof (EFI_HII_PACKAGE_HEADER);
1441 Offset = 0;
1442 while (Offset < FormDataLen) {
1443 OpCodeData = FormPackage->IfrData + Offset;
1444 OpCodeHeader = (EFI_IFR_OP_HEADER *) OpCodeData;
1445
1446 if (IsStatementOpCode(OpCodeHeader->OpCode)) {
1447 StatementHeader = (EFI_IFR_STATEMENT_HEADER *) (OpCodeData + sizeof (EFI_IFR_OP_HEADER));
1448 if (StatementHeader->Prompt == KeywordStrId) {
1449 return OpCodeData;
1450 }
1451 }
1452
1453 Offset += OpCodeHeader->Length;
1454 }
1455
1456 return NULL;
1457 }
1458
1459 /**
1460 Base on the varstore id to find the storage info.
1461
1462 @param FormPackage The input form package.
1463 @param VarStoreId The input storage id.
1464
1465 @retval the opcode for the storage.
1466
1467 **/
1468 UINT8 *
1469 FindStorageFromVarId (
1470 IN HII_IFR_PACKAGE_INSTANCE *FormPackage,
1471 IN EFI_VARSTORE_ID VarStoreId
1472 )
1473 {
1474 UINT8 *OpCodeData;
1475 UINT32 Offset;
1476 EFI_IFR_OP_HEADER *OpCodeHeader;
1477 UINT32 FormDataLen;
1478
1479 ASSERT (FormPackage != NULL);
1480
1481 FormDataLen = FormPackage->FormPkgHdr.Length - sizeof (EFI_HII_PACKAGE_HEADER);
1482 Offset = 0;
1483 while (Offset < FormDataLen) {
1484 OpCodeData = FormPackage->IfrData + Offset;
1485 OpCodeHeader = (EFI_IFR_OP_HEADER *) OpCodeData;
1486
1487 if (IsStorageOpCode(OpCodeHeader->OpCode)) {
1488 switch (OpCodeHeader->OpCode) {
1489 case EFI_IFR_VARSTORE_OP:
1490 if (VarStoreId == ((EFI_IFR_VARSTORE *) OpCodeData)->VarStoreId) {
1491 return OpCodeData;
1492 }
1493 break;
1494
1495 case EFI_IFR_VARSTORE_NAME_VALUE_OP:
1496 if (VarStoreId == ((EFI_IFR_VARSTORE_NAME_VALUE *) OpCodeData)->VarStoreId) {
1497 return OpCodeData;
1498 }
1499 break;
1500
1501 case EFI_IFR_VARSTORE_EFI_OP:
1502 if (VarStoreId == ((EFI_IFR_VARSTORE_EFI *) OpCodeData)->VarStoreId) {
1503 return OpCodeData;
1504 }
1505 break;
1506
1507 default:
1508 break;
1509 }
1510 }
1511
1512 Offset += OpCodeHeader->Length;
1513 }
1514
1515 return NULL;
1516 }
1517
1518 /**
1519 Get width info for one question.
1520
1521 @param OpCodeData The input opcode for one question.
1522
1523 @retval the width info for one question.
1524
1525 **/
1526 UINT16
1527 GetWidth (
1528 IN UINT8 *OpCodeData
1529 )
1530 {
1531 UINT8 *NextOpCodeData;
1532
1533 ASSERT (OpCodeData != NULL);
1534
1535 switch (((EFI_IFR_OP_HEADER *) OpCodeData)->OpCode) {
1536 case EFI_IFR_REF_OP:
1537 return (UINT16) sizeof (EFI_HII_REF);
1538
1539 case EFI_IFR_ONE_OF_OP:
1540 case EFI_IFR_NUMERIC_OP:
1541 switch (((EFI_IFR_ONE_OF *) OpCodeData)->Flags & EFI_IFR_NUMERIC_SIZE) {
1542 case EFI_IFR_NUMERIC_SIZE_1:
1543 return (UINT16) sizeof (UINT8);
1544
1545 case EFI_IFR_NUMERIC_SIZE_2:
1546 return (UINT16) sizeof (UINT16);
1547
1548 case EFI_IFR_NUMERIC_SIZE_4:
1549 return (UINT16) sizeof (UINT32);
1550
1551 case EFI_IFR_NUMERIC_SIZE_8:
1552 return (UINT16) sizeof (UINT64);
1553
1554 default:
1555 ASSERT (FALSE);
1556 return 0;
1557 }
1558
1559 case EFI_IFR_ORDERED_LIST_OP:
1560 NextOpCodeData = OpCodeData + ((EFI_IFR_ORDERED_LIST *) OpCodeData)->Header.Length;
1561 //
1562 // OneOfOption must follow the orderedlist opcode.
1563 //
1564 ASSERT (((EFI_IFR_OP_HEADER *) NextOpCodeData)->OpCode == EFI_IFR_ONE_OF_OPTION_OP);
1565 switch (((EFI_IFR_ONE_OF_OPTION *) NextOpCodeData)->Type) {
1566 case EFI_IFR_TYPE_NUM_SIZE_8:
1567 return (UINT16) sizeof (UINT8) * ((EFI_IFR_ORDERED_LIST *) OpCodeData)->MaxContainers;
1568
1569 case EFI_IFR_TYPE_NUM_SIZE_16:
1570 return (UINT16) sizeof (UINT16) * ((EFI_IFR_ORDERED_LIST *) OpCodeData)->MaxContainers ;
1571
1572 case EFI_IFR_TYPE_NUM_SIZE_32:
1573 return (UINT16) sizeof (UINT32) * ((EFI_IFR_ORDERED_LIST *) OpCodeData)->MaxContainers;
1574
1575 case EFI_IFR_TYPE_NUM_SIZE_64:
1576 return (UINT16) sizeof (UINT64) * ((EFI_IFR_ORDERED_LIST *) OpCodeData)->MaxContainers;
1577 break;
1578
1579 default:
1580 ASSERT (FALSE);
1581 return 0;
1582 }
1583
1584 case EFI_IFR_CHECKBOX_OP:
1585 return (UINT16) sizeof (BOOLEAN);
1586
1587 case EFI_IFR_PASSWORD_OP:
1588 case EFI_IFR_STRING_OP:
1589 return (UINT16)((UINTN) ((EFI_IFR_STRING *) OpCodeData)->MaxSize * sizeof (CHAR16));
1590
1591 case EFI_IFR_DATE_OP:
1592 return (UINT16) sizeof (EFI_HII_DATE);
1593
1594 case EFI_IFR_TIME_OP:
1595 return (UINT16) sizeof (EFI_HII_TIME);
1596
1597 default:
1598 ASSERT (FALSE);
1599 return 0;
1600 }
1601 }
1602
1603 /**
1604 Converts all hex dtring characters in range ['A'..'F'] to ['a'..'f'] for
1605 hex digits that appear between a '=' and a '&' in a config string.
1606
1607 If ConfigString is NULL, then ASSERT().
1608
1609 @param[in] ConfigString Pointer to a Null-terminated Unicode string.
1610
1611 @return Pointer to the Null-terminated Unicode result string.
1612
1613 **/
1614 EFI_STRING
1615 EFIAPI
1616 InternalLowerConfigString (
1617 IN EFI_STRING ConfigString
1618 )
1619 {
1620 EFI_STRING String;
1621 BOOLEAN Lower;
1622
1623 ASSERT (ConfigString != NULL);
1624
1625 //
1626 // Convert all hex digits in range [A-F] in the configuration header to [a-f]
1627 //
1628 for (String = ConfigString, Lower = FALSE; *String != L'\0'; String++) {
1629 if (*String == L'=') {
1630 Lower = TRUE;
1631 } else if (*String == L'&') {
1632 Lower = FALSE;
1633 } else if (Lower && *String >= L'A' && *String <= L'F') {
1634 *String = (CHAR16) (*String - L'A' + L'a');
1635 }
1636 }
1637
1638 return ConfigString;
1639 }
1640
1641 /**
1642 Allocates and returns a Null-terminated Unicode <ConfigHdr> string.
1643
1644 The format of a <ConfigHdr> is as follows:
1645
1646 GUID=<HexCh>32&NAME=<Char>NameLength&PATH=<HexChar>DevicePathSize<Null>
1647
1648 @param[in] OpCodeData The opcode for the storage.
1649 @param[in] DriverHandle The driver handle which supports a Device Path Protocol
1650 that is the routing information PATH. Each byte of
1651 the Device Path associated with DriverHandle is converted
1652 to a 2 Unicode character hexidecimal string.
1653
1654 @retval NULL DriverHandle does not support the Device Path Protocol.
1655 @retval Other A pointer to the Null-terminate Unicode <ConfigHdr> string
1656
1657 **/
1658 EFI_STRING
1659 ConstructConfigHdr (
1660 IN UINT8 *OpCodeData,
1661 IN EFI_HANDLE DriverHandle
1662 )
1663 {
1664 UINTN NameLength;
1665 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
1666 UINTN DevicePathSize;
1667 CHAR16 *String;
1668 CHAR16 *ReturnString;
1669 UINTN Index;
1670 UINT8 *Buffer;
1671 CHAR16 *Name;
1672 CHAR8 *AsciiName;
1673 EFI_GUID *Guid;
1674
1675 ASSERT (OpCodeData != NULL);
1676
1677 switch (((EFI_IFR_OP_HEADER *)OpCodeData)->OpCode) {
1678 case EFI_IFR_VARSTORE_OP:
1679 Guid = (EFI_GUID *)(UINTN *)&((EFI_IFR_VARSTORE *) OpCodeData)->Guid;
1680 AsciiName = (CHAR8 *) ((EFI_IFR_VARSTORE *) OpCodeData)->Name;
1681 break;
1682
1683 case EFI_IFR_VARSTORE_NAME_VALUE_OP:
1684 Guid = (EFI_GUID *)(UINTN *)&((EFI_IFR_VARSTORE_NAME_VALUE *) OpCodeData)->Guid;
1685 AsciiName = NULL;
1686 break;
1687
1688 case EFI_IFR_VARSTORE_EFI_OP:
1689 Guid = (EFI_GUID *)(UINTN *)&((EFI_IFR_VARSTORE_EFI *) OpCodeData)->Guid;
1690 AsciiName = (CHAR8 *) ((EFI_IFR_VARSTORE_EFI *) OpCodeData)->Name;
1691 break;
1692
1693 default:
1694 ASSERT (FALSE);
1695 Guid = NULL;
1696 AsciiName = NULL;
1697 break;
1698 }
1699
1700 if (AsciiName != NULL) {
1701 Name = AllocateZeroPool (AsciiStrSize (AsciiName) * 2);
1702 ASSERT (Name != NULL);
1703 AsciiStrToUnicodeStr(AsciiName, Name);
1704 } else {
1705 Name = NULL;
1706 }
1707
1708 //
1709 // Compute the length of Name in Unicode characters.
1710 // If Name is NULL, then the length is 0.
1711 //
1712 NameLength = 0;
1713 if (Name != NULL) {
1714 NameLength = StrLen (Name);
1715 }
1716
1717 DevicePath = NULL;
1718 DevicePathSize = 0;
1719 //
1720 // Retrieve DevicePath Protocol associated with DriverHandle
1721 //
1722 if (DriverHandle != NULL) {
1723 DevicePath = DevicePathFromHandle (DriverHandle);
1724 if (DevicePath == NULL) {
1725 return NULL;
1726 }
1727 //
1728 // Compute the size of the device path in bytes
1729 //
1730 DevicePathSize = GetDevicePathSize (DevicePath);
1731 }
1732
1733 //
1734 // GUID=<HexCh>32&NAME=<Char>NameLength&PATH=<HexChar>DevicePathSize <Null>
1735 // | 5 | sizeof (EFI_GUID) * 2 | 6 | NameStrLen*4 | 6 | DevicePathSize * 2 | 1 |
1736 //
1737 String = AllocateZeroPool ((5 + sizeof (EFI_GUID) * 2 + 6 + NameLength * 4 + 6 + DevicePathSize * 2 + 1) * sizeof (CHAR16));
1738 if (String == NULL) {
1739 return NULL;
1740 }
1741
1742 //
1743 // Start with L"GUID="
1744 //
1745 ReturnString = StrCpy (String, L"GUID=");
1746 String += StrLen (String);
1747
1748 if (Guid != NULL) {
1749 //
1750 // Append Guid converted to <HexCh>32
1751 //
1752 for (Index = 0, Buffer = (UINT8 *)Guid; Index < sizeof (EFI_GUID); Index++) {
1753 String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, *(Buffer++), 2);
1754 }
1755 }
1756
1757 //
1758 // Append L"&NAME="
1759 //
1760 StrCpy (String, L"&NAME=");
1761 String += StrLen (String);
1762
1763 if (Name != NULL) {
1764 //
1765 // Append Name converted to <Char>NameLength
1766 //
1767 for (; *Name != L'\0'; Name++) {
1768 String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, *Name, 4);
1769 }
1770 }
1771
1772 //
1773 // Append L"&PATH="
1774 //
1775 StrCpy (String, L"&PATH=");
1776 String += StrLen (String);
1777
1778 //
1779 // Append the device path associated with DriverHandle converted to <HexChar>DevicePathSize
1780 //
1781 for (Index = 0, Buffer = (UINT8 *)DevicePath; Index < DevicePathSize; Index++) {
1782 String += UnicodeValueToString (String, PREFIX_ZERO | RADIX_HEX, *(Buffer++), 2);
1783 }
1784
1785 //
1786 // Null terminate the Unicode string
1787 //
1788 *String = L'\0';
1789
1790 //
1791 // Convert all hex digits in range [A-F] in the configuration header to [a-f]
1792 //
1793 return InternalLowerConfigString (ReturnString);
1794 }
1795
1796 /**
1797 Generate the Config request element for one question.
1798
1799 @param Name The name info for one question.
1800 @param Offset The offset info for one question.
1801 @param Width The width info for one question.
1802
1803 @return Pointer to the Null-terminated Unicode request element string.
1804
1805 **/
1806 EFI_STRING
1807 ConstructRequestElement (
1808 IN CHAR16 *Name,
1809 IN UINT16 Offset,
1810 IN UINT16 Width
1811 )
1812 {
1813 CHAR16 *StringPtr;
1814 UINTN Length;
1815
1816 if (Name != NULL) {
1817 //
1818 // Add <BlockName> length for each Name
1819 //
1820 // <BlockName> ::= Name + \0
1821 // StrLen(Name) | 1
1822 //
1823 Length = StrLen (Name) + 1;
1824 } else {
1825 //
1826 // Add <BlockName> length for each Offset/Width pair
1827 //
1828 // <BlockName> ::= OFFSET=1234&WIDTH=1234 + \0
1829 // | 7 | 4 | 7 | 4 | 1
1830 //
1831 Length = (7 + 4 + 7 + 4 + 1);
1832 }
1833
1834 //
1835 // Allocate buffer for the entire <ConfigRequest>
1836 //
1837 StringPtr = AllocateZeroPool (Length * sizeof (CHAR16));
1838 ASSERT (StringPtr != NULL);
1839
1840 if (Name != NULL) {
1841 //
1842 // Append Name\0
1843 //
1844 UnicodeSPrint (
1845 StringPtr,
1846 (StrLen (Name) + 1) * sizeof (CHAR16),
1847 L"%s",
1848 Name
1849 );
1850 } else {
1851 //
1852 // Append OFFSET=XXXX&WIDTH=YYYY\0
1853 //
1854 UnicodeSPrint (
1855 StringPtr,
1856 (7 + 4 + 7 + 4 + 1) * sizeof (CHAR16),
1857 L"OFFSET=%04X&WIDTH=%04X",
1858 Offset,
1859 Width
1860 );
1861 }
1862
1863 return StringPtr;
1864 }
1865
1866 /**
1867 Get string value for question's name field.
1868
1869 @param DatabaseRecord HII_DATABASE_RECORD format string.
1870 @param NameId The string id for the name field.
1871
1872 @retval Name string.
1873
1874 **/
1875 CHAR16 *
1876 GetNameFromId (
1877 IN HII_DATABASE_RECORD *DatabaseRecord,
1878 IN EFI_STRING_ID NameId
1879 )
1880 {
1881 CHAR16 *Name;
1882 CHAR8 *PlatformLanguage;
1883 CHAR8 *SupportedLanguages;
1884 CHAR8 *BestLanguage;
1885 UINTN StringSize;
1886 CHAR16 TempString;
1887 EFI_STATUS Status;
1888
1889 Name = NULL;
1890 BestLanguage = NULL;
1891 PlatformLanguage = NULL;
1892 SupportedLanguages = NULL;
1893
1894 GetEfiGlobalVariable2 (L"PlatformLang", (VOID**)&PlatformLanguage, NULL);
1895 SupportedLanguages = GetSupportedLanguages(DatabaseRecord->Handle);
1896
1897 //
1898 // Get the best matching language from SupportedLanguages
1899 //
1900 BestLanguage = GetBestLanguage (
1901 SupportedLanguages,
1902 FALSE, // RFC 4646 mode
1903 PlatformLanguage != NULL ? PlatformLanguage : "", // Highest priority
1904 SupportedLanguages, // Lowest priority
1905 NULL
1906 );
1907 if (BestLanguage == NULL) {
1908 BestLanguage = AllocateCopyPool (AsciiStrLen ("en-US"), "en-US");
1909 ASSERT (BestLanguage != NULL);
1910 }
1911
1912 StringSize = 0;
1913 Status = mPrivate.HiiString.GetString (
1914 &mPrivate.HiiString,
1915 BestLanguage,
1916 DatabaseRecord->Handle,
1917 NameId,
1918 &TempString,
1919 &StringSize,
1920 NULL
1921 );
1922 if (Status != EFI_BUFFER_TOO_SMALL) {
1923 goto Done;
1924 }
1925
1926 Name = AllocateZeroPool (StringSize);
1927 if (Name == NULL) {
1928 goto Done;
1929 }
1930
1931 Status = mPrivate.HiiString.GetString (
1932 &mPrivate.HiiString,
1933 BestLanguage,
1934 DatabaseRecord->Handle,
1935 NameId,
1936 Name,
1937 &StringSize,
1938 NULL
1939 );
1940
1941 if (EFI_ERROR (Status)) {
1942 FreePool (Name);
1943 Name = NULL;
1944 goto Done;
1945 }
1946
1947 Done:
1948 if (SupportedLanguages != NULL) {
1949 FreePool(SupportedLanguages);
1950 }
1951 if (BestLanguage != NULL) {
1952 FreePool (BestLanguage);
1953 }
1954 if (PlatformLanguage != NULL) {
1955 FreePool (PlatformLanguage);
1956 }
1957
1958 return Name;
1959 }
1960
1961 /**
1962 Base on the input parameter to generate the ConfigRequest string.
1963
1964 This is a internal function.
1965
1966 @param DatabaseRecord HII_DATABASE_RECORD format string.
1967 @param KeywordStrId Keyword string id.
1968 @param OpCodeData The IFR data for this question.
1969 @param ConfigRequest Return the generate ConfigRequest string.
1970
1971 @retval EFI_SUCCESS Generate ConfigResp string success.
1972 @retval EFI_OUT_OF_RESOURCES System out of memory resource error.
1973 @retval EFI_NOT_FOUND Not found the question which use this string id
1974 as the prompt string id.
1975 **/
1976 EFI_STATUS
1977 ExtractConfigRequest (
1978 IN HII_DATABASE_RECORD *DatabaseRecord,
1979 IN EFI_STRING_ID KeywordStrId,
1980 OUT UINT8 **OpCodeData,
1981 OUT EFI_STRING *ConfigRequest
1982 )
1983 {
1984 LIST_ENTRY *Link;
1985 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;
1986 HII_IFR_PACKAGE_INSTANCE *FormPackage;
1987 EFI_IFR_QUESTION_HEADER *Header;
1988 UINT8 *Storage;
1989 UINT8 *OpCode;
1990 CHAR16 *Name;
1991 UINT16 Offset;
1992 UINT16 Width;
1993 CHAR16 *ConfigHdr;
1994 CHAR16 *RequestElement;
1995 UINTN Length;
1996 CHAR16 *StringPtr;
1997
1998 ASSERT (DatabaseRecord != NULL && OpCodeData != NULL && ConfigRequest != NULL);
1999
2000 OpCode = NULL;
2001 Name = NULL;
2002 Width = 0;
2003 Offset = 0;
2004
2005 PackageListNode = DatabaseRecord->PackageList;
2006
2007 //
2008 // Search the languages in the specified packagelist.
2009 //
2010 for (Link = PackageListNode->FormPkgHdr.ForwardLink; Link != &PackageListNode->FormPkgHdr; Link = Link->ForwardLink) {
2011 FormPackage = CR (Link, HII_IFR_PACKAGE_INSTANCE, IfrEntry, HII_IFR_PACKAGE_SIGNATURE);
2012
2013 OpCode = FindQuestionFromStringId (FormPackage, KeywordStrId);
2014 if (OpCode != NULL) {
2015 *OpCodeData = OpCode;
2016 Header = (EFI_IFR_QUESTION_HEADER *) (OpCode + sizeof (EFI_IFR_OP_HEADER));
2017 //
2018 // Header->VarStoreId == 0 means no storage for this question.
2019 //
2020 ASSERT (Header->VarStoreId != 0);
2021 DEBUG ((EFI_D_INFO, "Varstore Id: 0x%x\n", Header->VarStoreId));
2022
2023 Storage = FindStorageFromVarId (FormPackage, Header->VarStoreId);
2024 ASSERT (Storage != NULL);
2025
2026 if (((EFI_IFR_OP_HEADER *) Storage)->OpCode == EFI_IFR_VARSTORE_NAME_VALUE_OP) {
2027 Name = GetNameFromId (DatabaseRecord, Header->VarStoreInfo.VarName);
2028 } else {
2029 Offset = Header->VarStoreInfo.VarOffset;
2030 Width = GetWidth (OpCode);
2031 }
2032 RequestElement = ConstructRequestElement(Name, Offset, Width);
2033 ConfigHdr = ConstructConfigHdr(Storage, DatabaseRecord->DriverHandle);
2034 ASSERT (ConfigHdr != NULL);
2035
2036 Length = (StrLen (ConfigHdr) + 1 + StrLen(RequestElement) + 1) * sizeof (CHAR16);
2037 *ConfigRequest = AllocatePool (Length);
2038 if (*ConfigRequest == NULL) {
2039 FreePool (ConfigHdr);
2040 FreePool (RequestElement);
2041 return EFI_OUT_OF_RESOURCES;
2042 }
2043 StringPtr = *ConfigRequest;
2044
2045 StrCpy (StringPtr, ConfigHdr);
2046 StringPtr += StrLen (StringPtr);
2047
2048 *StringPtr = L'&';
2049 StringPtr++;
2050
2051 StrCpy (StringPtr, RequestElement);
2052 StringPtr += StrLen (StringPtr);
2053 *StringPtr = L'\0';
2054
2055 FreePool (ConfigHdr);
2056 FreePool (RequestElement);
2057
2058 return EFI_SUCCESS;
2059 }
2060 }
2061
2062 return EFI_NOT_FOUND;
2063 }
2064
2065 /**
2066 Base on the input parameter to generate the ConfigResp string.
2067
2068 This is a internal function.
2069
2070 @param DatabaseRecord HII_DATABASE_RECORD format string.
2071 @param KeywordStrId Keyword string id.
2072 @param ValueElement The value for the question which use keyword string id
2073 as the prompt string id.
2074 @param OpCodeData The IFR data for this question.
2075 @param ConfigResp Return the generate ConfigResp string.
2076
2077 @retval EFI_SUCCESS Generate ConfigResp string success.
2078 @retval EFI_OUT_OF_RESOURCES System out of memory resource error.
2079 @retval EFI_NOT_FOUND Not found the question which use this string id
2080 as the prompt string id.
2081 **/
2082 EFI_STATUS
2083 ExtractConfigResp (
2084 IN HII_DATABASE_RECORD *DatabaseRecord,
2085 IN EFI_STRING_ID KeywordStrId,
2086 IN EFI_STRING ValueElement,
2087 OUT UINT8 **OpCodeData,
2088 OUT EFI_STRING *ConfigResp
2089 )
2090 {
2091 LIST_ENTRY *Link;
2092 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;
2093 HII_IFR_PACKAGE_INSTANCE *FormPackage;
2094 EFI_IFR_QUESTION_HEADER *Header;
2095 UINT8 *Storage;
2096 UINT8 *OpCode;
2097 CHAR16 *Name;
2098 UINT16 Offset;
2099 UINT16 Width;
2100 CHAR16 *ConfigHdr;
2101 CHAR16 *RequestElement;
2102 UINTN Length;
2103 CHAR16 *StringPtr;
2104
2105 ASSERT ((DatabaseRecord != NULL) && (OpCodeData != NULL) && (ConfigResp != NULL) && (ValueElement != NULL));
2106
2107 OpCode = NULL;
2108 Name = NULL;
2109 Width = 0;
2110 Offset = 0;
2111
2112 PackageListNode = DatabaseRecord->PackageList;
2113
2114 //
2115 // Search the languages in the specified packagelist.
2116 //
2117 for (Link = PackageListNode->FormPkgHdr.ForwardLink; Link != &PackageListNode->FormPkgHdr; Link = Link->ForwardLink) {
2118 FormPackage = CR (Link, HII_IFR_PACKAGE_INSTANCE, IfrEntry, HII_IFR_PACKAGE_SIGNATURE);
2119
2120 OpCode = FindQuestionFromStringId (FormPackage, KeywordStrId);
2121 if (OpCode != NULL) {
2122 *OpCodeData = OpCode;
2123 Header = (EFI_IFR_QUESTION_HEADER *) (OpCode + sizeof (EFI_IFR_OP_HEADER));
2124 //
2125 // Header->VarStoreId == 0 means no storage for this question.
2126 //
2127 ASSERT (Header->VarStoreId != 0);
2128 DEBUG ((EFI_D_INFO, "Varstore Id: 0x%x\n", Header->VarStoreId));
2129
2130 Storage = FindStorageFromVarId (FormPackage, Header->VarStoreId);
2131 ASSERT (Storage != NULL);
2132
2133 if (((EFI_IFR_OP_HEADER *) Storage)->OpCode == EFI_IFR_VARSTORE_NAME_VALUE_OP) {
2134 Name = GetNameFromId (DatabaseRecord, Header->VarStoreInfo.VarName);
2135 } else {
2136 Offset = Header->VarStoreInfo.VarOffset;
2137 Width = GetWidth (OpCode);
2138 }
2139 RequestElement = ConstructRequestElement(Name, Offset, Width);
2140
2141 ConfigHdr = ConstructConfigHdr(Storage, DatabaseRecord->DriverHandle);
2142 ASSERT (ConfigHdr != NULL);
2143
2144 Length = (StrLen (ConfigHdr) + 1 + StrLen(RequestElement) + 1 + StrLen (L"VALUE=") + StrLen(ValueElement) + 1) * sizeof (CHAR16);
2145 *ConfigResp = AllocatePool (Length);
2146 if (*ConfigResp == NULL) {
2147 FreePool (ConfigHdr);
2148 FreePool (RequestElement);
2149 return EFI_OUT_OF_RESOURCES;
2150 }
2151 StringPtr = *ConfigResp;
2152
2153 StrCpy (StringPtr, ConfigHdr);
2154 StringPtr += StrLen (StringPtr);
2155
2156 *StringPtr = L'&';
2157 StringPtr++;
2158
2159 StrCpy (StringPtr, RequestElement);
2160 StringPtr += StrLen (StringPtr);
2161
2162 *StringPtr = L'&';
2163 StringPtr++;
2164
2165 StrCpy (StringPtr, L"VALUE=");
2166 StringPtr += StrLen (StringPtr);
2167
2168 StrCpy (StringPtr, ValueElement);
2169 StringPtr += StrLen (StringPtr);
2170 *StringPtr = L'\0';
2171
2172 FreePool (ConfigHdr);
2173 FreePool (RequestElement);
2174
2175 return EFI_SUCCESS;
2176 }
2177 }
2178
2179 return EFI_NOT_FOUND;
2180 }
2181
2182 /**
2183 Get the Value section from the Hii driver.
2184
2185 This is a internal function.
2186
2187 @param ConfigRequest The input ConfigRequest string.
2188 @param ValueElement The respond Value section from the hii driver.
2189
2190 @retval Misc value The error status return from ExtractConfig function.
2191 @retval EFI_OUT_OF_RESOURCES The memory can't be allocated
2192 @retval EFI_SUCCESS Get the value section success.
2193
2194 **/
2195 EFI_STATUS
2196 ExtractValueFromDriver (
2197 IN CHAR16 *ConfigRequest,
2198 OUT CHAR16 **ValueElement
2199 )
2200 {
2201 EFI_STATUS Status;
2202 EFI_STRING Result;
2203 EFI_STRING Progress;
2204 CHAR16 *StringPtr;
2205 CHAR16 *StringEnd;
2206
2207 ASSERT ((ConfigRequest != NULL) && (ValueElement != NULL));
2208
2209 Status = mPrivate.ConfigRouting.ExtractConfig (
2210 &mPrivate.ConfigRouting,
2211 (EFI_STRING) ConfigRequest,
2212 &Progress,
2213 &Result
2214 );
2215 if (EFI_ERROR (Status)) {
2216 return Status;
2217 }
2218
2219 //
2220 // Find Value Section and return it.
2221 //
2222 StringPtr = StrStr (Result, L"&VALUE=");
2223 ASSERT (StringPtr != NULL);
2224 StringEnd = StrStr (StringPtr + 1, L"&");
2225 if (StringEnd != NULL) {
2226 *StringEnd = L'\0';
2227 }
2228
2229 *ValueElement = AllocateCopyPool (StrSize (StringPtr), StringPtr);
2230 if (*ValueElement == NULL) {
2231 return EFI_OUT_OF_RESOURCES;
2232 }
2233
2234 if (StringEnd != NULL) {
2235 *StringEnd = L'&';
2236 }
2237 FreePool (Result);
2238
2239 return EFI_SUCCESS;
2240 }
2241
2242 /**
2243 Get EFI_STRING_ID info from the input device path, namespace and keyword.
2244
2245 This is a internal function.
2246
2247 @param DevicePath Input device path info.
2248 @param NameSpace NameSpace format string.
2249 @param KeywordData Keyword used to get string id.
2250 @param ProgressErr Return extra error type.
2251 @param KeywordStringId Return EFI_STRING_ID.
2252 @param DataBaseRecord DataBase record data for this driver.
2253
2254 @retval EFI_INVALID_PARAMETER Can't find the database record base on the input device path or namespace.
2255 @retval EFI_NOT_FOUND Can't find the EFI_STRING_ID for the keyword.
2256 @retval EFI_SUCCESS Find the EFI_STRING_ID.
2257
2258 **/
2259 EFI_STATUS
2260 GetStringIdFromDatabase (
2261 IN EFI_DEVICE_PATH_PROTOCOL **DevicePath,
2262 IN CHAR8 **NameSpace,
2263 IN CHAR16 *KeywordData,
2264 OUT UINT32 *ProgressErr,
2265 OUT EFI_STRING_ID *KeywordStringId,
2266 OUT HII_DATABASE_RECORD **DataBaseRecord
2267 )
2268 {
2269 HII_DATABASE_RECORD *Record;
2270 LIST_ENTRY *Link;
2271 BOOLEAN FindNameSpace;
2272 EFI_DEVICE_PATH_PROTOCOL *DestDevicePath;
2273 UINT8 *DevicePathPkg;
2274 UINTN DevicePathSize;
2275
2276 ASSERT ((NameSpace != NULL) && (KeywordData != NULL) && (ProgressErr != NULL) && (KeywordStringId != NULL) && (DataBaseRecord != NULL));
2277
2278 FindNameSpace = FALSE;
2279
2280 if (*DevicePath != NULL) {
2281 //
2282 // Get DataBaseRecord from device path protocol.
2283 //
2284 Record = GetRecordFromDevicePath(*DevicePath);
2285 if (Record == NULL) {
2286 //
2287 // Can't find the DatabaseRecord base on the input device path info.
2288 // NEED TO CONFIRM the return ProgressErr.
2289 //
2290 *ProgressErr = KEYWORD_HANDLER_MALFORMED_STRING;
2291 return EFI_INVALID_PARAMETER;
2292 }
2293
2294 //
2295 // Get string id from the record.
2296 //
2297 *ProgressErr = GetStringIdFromRecord (Record, NameSpace, KeywordData, KeywordStringId);
2298 switch (*ProgressErr) {
2299 case KEYWORD_HANDLER_NO_ERROR:
2300 *DataBaseRecord = Record;
2301 return EFI_SUCCESS;
2302
2303 case KEYWORD_HANDLER_NAMESPACE_ID_NOT_FOUND:
2304 return EFI_INVALID_PARAMETER;
2305
2306 default:
2307 ASSERT (*ProgressErr == KEYWORD_HANDLER_KEYWORD_NOT_FOUND);
2308 return EFI_NOT_FOUND;
2309 }
2310 } else {
2311 //
2312 // Find driver which matches the routing data.
2313 //
2314 for (Link = mPrivate.DatabaseList.ForwardLink; Link != &mPrivate.DatabaseList; Link = Link->ForwardLink) {
2315 Record = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
2316
2317 *ProgressErr = GetStringIdFromRecord (Record, NameSpace, KeywordData, KeywordStringId);
2318 if (*ProgressErr == KEYWORD_HANDLER_NO_ERROR) {
2319 *DataBaseRecord = Record;
2320
2321 if ((DevicePathPkg = Record->PackageList->DevicePathPkg) != NULL) {
2322 DestDevicePath = (EFI_DEVICE_PATH_PROTOCOL *) (DevicePathPkg + sizeof (EFI_HII_PACKAGE_HEADER));
2323 DevicePathSize = GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) DestDevicePath);
2324 *DevicePath = AllocateCopyPool (DevicePathSize, DestDevicePath);
2325 if (*DevicePath == NULL) {
2326 return EFI_OUT_OF_RESOURCES;
2327 }
2328 } else {
2329 //
2330 // Need to verify this ASSERT.
2331 //
2332 ASSERT (FALSE);
2333 }
2334
2335 return EFI_SUCCESS;
2336 } else if (*ProgressErr == KEYWORD_HANDLER_UNDEFINED_PROCESSING_ERROR) {
2337 return EFI_OUT_OF_RESOURCES;
2338 } else if (*ProgressErr == KEYWORD_HANDLER_KEYWORD_NOT_FOUND) {
2339 FindNameSpace = TRUE;
2340 }
2341 }
2342
2343 //
2344 // When PathHdr not input, if ever find the namespace, will return KEYWORD_HANDLER_KEYWORD_NOT_FOUND.
2345 // This is a bit more progress than KEYWORD_HANDLER_NAMESPACE_ID_NOT_FOUND.
2346 //
2347 if (FindNameSpace) {
2348 return EFI_NOT_FOUND;
2349 } else {
2350 return EFI_INVALID_PARAMETER;
2351 }
2352 }
2353 }
2354
2355 /**
2356 Genereate the KeywordResp String.
2357
2358 <KeywordResp> ::= <NameSpaceId><PathHdr>'&'<Keyword>'&VALUE='<Number>['&READONLY']
2359
2360 @param NameSpace NameSpace format string.
2361 @param DevicePath Input device path info.
2362 @param KeywordData Keyword used to get string id.
2363 @param ValueStr The value section for the keyword.
2364 @param ReadOnly Whether this value is readonly.
2365 @param KeywordResp Return the point to the KeywordResp string.
2366
2367 @retval EFI_OUT_OF_RESOURCES The memory can't be allocated.
2368 @retval EFI_SUCCESS Generate the KeywordResp string.
2369
2370 **/
2371 EFI_STATUS
2372 GenerateKeywordResp (
2373 IN CHAR8 *NameSpace,
2374 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
2375 IN EFI_STRING KeywordData,
2376 IN EFI_STRING ValueStr,
2377 IN BOOLEAN ReadOnly,
2378 OUT EFI_STRING *KeywordResp
2379 )
2380 {
2381 UINTN RespStrLen;
2382 CHAR16 *RespStr;
2383 CHAR16 *PathHdr;
2384 CHAR16 *UnicodeNameSpace;
2385
2386 ASSERT ((NameSpace != NULL) && (DevicePath != NULL) && (KeywordData != NULL) && (ValueStr != NULL) && (KeywordResp != NULL));
2387
2388 //
2389 // 1. Calculate the string length.
2390 //
2391 //
2392 // 1.1 NameSpaceId size.
2393 // 'NAMESPACE='<String>
2394 //
2395 RespStrLen = 10 + AsciiStrLen (NameSpace);
2396 UnicodeNameSpace = AllocatePool ((AsciiStrLen (NameSpace) + 1) * sizeof (CHAR16));
2397 if (UnicodeNameSpace == NULL) {
2398 return EFI_OUT_OF_RESOURCES;
2399 }
2400 AsciiStrToUnicodeStr(NameSpace, UnicodeNameSpace);
2401
2402 //
2403 // 1.2 PathHdr size.
2404 // PATH=<UEFI binary Device Path represented as hex number>'&'
2405 // Attention: The output include the '&' at the end.
2406 //
2407 GenerateSubStr (
2408 L"&PATH=",
2409 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) DevicePath),
2410 (VOID *) DevicePath,
2411 1,
2412 &PathHdr
2413 );
2414 RespStrLen += StrLen (PathHdr);
2415
2416 //
2417 // 1.3 Keyword setion.
2418 // 'KEYWORD='<String>[':'<DecCh>(1/4)]
2419 //
2420 RespStrLen += 8 + StrLen (KeywordData);
2421
2422 //
2423 // 1.4 Value section.
2424 // ValueStr = '&VALUE='<Number>
2425 //
2426 RespStrLen += StrLen (ValueStr);
2427
2428 //
2429 // 1.5 ReadOnly Section.
2430 // '&READONLY'
2431 //
2432 if (ReadOnly) {
2433 RespStrLen += 9;
2434 }
2435
2436 //
2437 // 2. Allocate the buffer and create the KeywordResp string.
2438 //
2439 *KeywordResp = AllocatePool ((RespStrLen + 1) * sizeof (CHAR16));
2440 if (*KeywordResp == NULL) {
2441 if (UnicodeNameSpace != NULL) {
2442 FreePool (UnicodeNameSpace);
2443 }
2444
2445 return EFI_OUT_OF_RESOURCES;
2446 }
2447 RespStr = *KeywordResp;
2448
2449 //
2450 // 2.1 Copy NameSpaceId section.
2451 //
2452 StrCpy (RespStr, L"NAMESPACE=");
2453 RespStr += StrLen (RespStr);
2454 StrCpy (RespStr, UnicodeNameSpace);
2455 RespStr += StrLen (RespStr);
2456
2457 //
2458 // 2.2 Copy PathHdr section.
2459 //
2460 StrCpy (RespStr, PathHdr);
2461 RespStr += StrLen (RespStr);
2462
2463 //
2464 // 2.3 Copy Keyword section.
2465 //
2466 StrCpy (RespStr, L"KEYWORD=");
2467 RespStr += StrLen (RespStr);
2468 StrCpy (RespStr, KeywordData);
2469 RespStr += StrLen (RespStr);
2470
2471 //
2472 // 2.4 Copy the Value section.
2473 //
2474 StrCpy (RespStr, ValueStr);
2475 RespStr += StrLen (RespStr);
2476
2477 //
2478 // 2.5 Copy ReadOnly section if exist.
2479 //
2480 if (ReadOnly) {
2481 StrCpy (RespStr, L"&READONLY");
2482 RespStr += StrLen (RespStr);
2483 }
2484
2485 //
2486 // 2.6 Add the end.
2487 //
2488 *RespStr = L'\0';
2489
2490 if (UnicodeNameSpace != NULL) {
2491 FreePool (UnicodeNameSpace);
2492 }
2493 if (PathHdr != NULL) {
2494 FreePool (PathHdr);
2495 }
2496
2497 return EFI_SUCCESS;
2498 }
2499
2500 /**
2501 Merge the KeywordResp String to MultiKeywordResp string.
2502
2503 This is a internal function.
2504
2505 @param MultiKeywordResp The existed multikeywordresp string.
2506 @param KeywordResp The input keywordResp string.
2507
2508 @retval EFI_OUT_OF_RESOURCES The memory can't be allocated.
2509 @retval EFI_SUCCESS Generate the MultiKeywordResp string.
2510
2511 **/
2512 EFI_STATUS
2513 MergeToMultiKeywordResp (
2514 IN OUT EFI_STRING *MultiKeywordResp,
2515 IN EFI_STRING *KeywordResp
2516 )
2517 {
2518 UINTN MultiKeywordRespLen;
2519 EFI_STRING StringPtr;
2520
2521 if (*MultiKeywordResp == NULL) {
2522 *MultiKeywordResp = *KeywordResp;
2523 *KeywordResp = NULL;
2524 return EFI_SUCCESS;
2525 }
2526
2527 MultiKeywordRespLen = (StrLen (*MultiKeywordResp) + 1 + StrLen (*KeywordResp) + 1) * sizeof (CHAR16);
2528
2529 StringPtr = AllocateCopyPool (MultiKeywordRespLen, *MultiKeywordResp);
2530 if (StringPtr == NULL) {
2531 return EFI_OUT_OF_RESOURCES;
2532 }
2533
2534 FreePool (*MultiKeywordResp);
2535 *MultiKeywordResp = StringPtr;
2536
2537 StringPtr += StrLen (StringPtr);
2538
2539 *StringPtr = L'&';
2540 StringPtr++;
2541
2542 StrCpy (StringPtr, *KeywordResp);
2543
2544 return EFI_SUCCESS;
2545 }
2546
2547 /**
2548 Enumerate all keyword in the system.
2549
2550 If error occur when parse one keyword, just skip it and parse the next one.
2551
2552 This is a internal function.
2553
2554 @param NameSpace The namespace used to search the string.
2555 @param MultiResp Return the MultiKeywordResp string for the system.
2556
2557 @retval EFI_OUT_OF_RESOURCES The memory can't be allocated.
2558 @retval EFI_SUCCESS Generate the MultiKeywordResp string.
2559 @retval EFI_NOT_FOUND No keyword found.
2560
2561 **/
2562 EFI_STATUS
2563 EnumerateAllKeywords (
2564 IN CHAR8 *NameSpace,
2565 OUT EFI_STRING *MultiResp
2566 )
2567 {
2568 LIST_ENTRY *Link;
2569 LIST_ENTRY *StringLink;
2570 UINT8 *DevicePathPkg;
2571 UINT8 *DevicePath;
2572 HII_DATABASE_RECORD *DataBaseRecord;
2573 HII_DATABASE_PACKAGE_LIST_INSTANCE *PackageListNode;
2574 HII_STRING_PACKAGE_INSTANCE *StringPackage;
2575 CHAR8 *LocalNameSpace;
2576 EFI_STRING_ID NextStringId;
2577 EFI_STATUS Status;
2578 UINT8 *OpCode;
2579 CHAR16 *ConfigRequest;
2580 CHAR16 *ValueElement;
2581 CHAR16 *KeywordResp;
2582 CHAR16 *MultiKeywordResp;
2583 CHAR16 *KeywordData;
2584 BOOLEAN ReadOnly;
2585
2586 DataBaseRecord = NULL;
2587 Status = EFI_SUCCESS;
2588 MultiKeywordResp = NULL;
2589 DevicePath = NULL;
2590 LocalNameSpace = NULL;
2591 ConfigRequest = NULL;
2592 ValueElement = NULL;
2593 KeywordResp = NULL;
2594
2595 if (NameSpace == NULL) {
2596 NameSpace = UEFI_CONFIG_LANG;
2597 }
2598
2599 //
2600 // Find driver which matches the routing data.
2601 //
2602 for (Link = mPrivate.DatabaseList.ForwardLink; Link != &mPrivate.DatabaseList; Link = Link->ForwardLink) {
2603 DataBaseRecord = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
2604 if ((DevicePathPkg = DataBaseRecord->PackageList->DevicePathPkg) != NULL) {
2605 DevicePath = DevicePathPkg + sizeof (EFI_HII_PACKAGE_HEADER);
2606 }
2607 PackageListNode = DataBaseRecord->PackageList;
2608
2609 for (StringLink = PackageListNode->StringPkgHdr.ForwardLink; StringLink != &PackageListNode->StringPkgHdr; StringLink = StringLink->ForwardLink) {
2610 StringPackage = CR (StringLink, HII_STRING_PACKAGE_INSTANCE, StringEntry, HII_STRING_PACKAGE_SIGNATURE);
2611
2612 //
2613 // Check whether has keyword string package.
2614 //
2615 if (AsciiStrnCmp(NameSpace, StringPackage->StringPkgHdr->Language, AsciiStrLen (NameSpace)) == 0) {
2616 //
2617 // Keep the NameSpace string.
2618 //
2619 LocalNameSpace = AllocateCopyPool (AsciiStrSize (StringPackage->StringPkgHdr->Language), StringPackage->StringPkgHdr->Language);
2620 if (LocalNameSpace == NULL) {
2621 return EFI_OUT_OF_RESOURCES;
2622 }
2623
2624 //
2625 // 1 means just begin the enumerate the valid string ids.
2626 // StringId == 1 is always used to save the language for this string package.
2627 // Any valid string start from 2. so here initial it to 1.
2628 //
2629 NextStringId = 1;
2630
2631 //
2632 // Enumerate all valid stringid in the package.
2633 //
2634 while ((NextStringId = GetNextStringId (StringPackage, NextStringId, &KeywordData)) != 0) {
2635 //
2636 // 3.3 Construct the ConfigRequest string.
2637 //
2638 Status = ExtractConfigRequest (DataBaseRecord, NextStringId, &OpCode, &ConfigRequest);
2639 if (EFI_ERROR (Status)) {
2640 //
2641 // If can't generate ConfigRequest for this question, skip it and start the next.
2642 //
2643 goto Error;
2644 }
2645
2646 //
2647 // 3.4 Extract Value for the input keyword.
2648 //
2649 Status = ExtractValueFromDriver(ConfigRequest, &ValueElement);
2650 if (EFI_ERROR (Status)) {
2651 if (Status != EFI_OUT_OF_RESOURCES) {
2652 //
2653 // If can't generate ConfigRequest for this question, skip it and start the next.
2654 //
2655 goto Error;
2656 }
2657 //
2658 // If EFI_OUT_OF_RESOURCES error occur, no need to continue.
2659 //
2660 goto Done;
2661 }
2662
2663 //
2664 // Extract readonly flag from opcode.
2665 //
2666 ReadOnly = ExtractReadOnlyFromOpCode(OpCode);
2667
2668 //
2669 // 5. Generate KeywordResp string.
2670 //
2671 ASSERT (DevicePath != NULL);
2672 Status = GenerateKeywordResp(LocalNameSpace, (EFI_DEVICE_PATH_PROTOCOL *)DevicePath, KeywordData, ValueElement, ReadOnly, &KeywordResp);
2673 if (Status != EFI_SUCCESS) {
2674 //
2675 // If EFI_OUT_OF_RESOURCES error occur, no need to continue.
2676 //
2677 goto Done;
2678 }
2679
2680 //
2681 // 6. Merge to the MultiKeywordResp string.
2682 //
2683 Status = MergeToMultiKeywordResp(&MultiKeywordResp, &KeywordResp);
2684 if (EFI_ERROR (Status)) {
2685 goto Done;
2686 }
2687 Error:
2688 //
2689 // Clean the temp buffer to later use again.
2690 //
2691 if (ConfigRequest != NULL) {
2692 FreePool (ConfigRequest);
2693 ConfigRequest = NULL;
2694 }
2695 if (ValueElement != NULL) {
2696 FreePool (ValueElement);
2697 ValueElement = NULL;
2698 }
2699 if (KeywordResp != NULL) {
2700 FreePool (KeywordResp);
2701 KeywordResp = NULL;
2702 }
2703 }
2704
2705 if (LocalNameSpace != NULL) {
2706 FreePool (LocalNameSpace);
2707 LocalNameSpace = NULL;
2708 }
2709 }
2710 }
2711 }
2712
2713 //
2714 // return the already get MultiKeywordString even error occured.
2715 //
2716 if (MultiKeywordResp == NULL) {
2717 Status = EFI_NOT_FOUND;
2718 } else {
2719 Status = EFI_SUCCESS;
2720 }
2721 *MultiResp = MultiKeywordResp;
2722
2723 Done:
2724 if (LocalNameSpace != NULL) {
2725 FreePool (LocalNameSpace);
2726 }
2727 if (ConfigRequest != NULL) {
2728 FreePool (ConfigRequest);
2729 }
2730 if (ValueElement != NULL) {
2731 FreePool (ValueElement);
2732 }
2733
2734 return Status;
2735 }
2736
2737 /**
2738
2739 This function accepts a <MultiKeywordResp> formatted string, finds the associated
2740 keyword owners, creates a <MultiConfigResp> string from it and forwards it to the
2741 EFI_HII_ROUTING_PROTOCOL.RouteConfig function.
2742
2743 If there is an issue in resolving the contents of the KeywordString, then the
2744 function returns an error and also sets the Progress and ProgressErr with the
2745 appropriate information about where the issue occurred and additional data about
2746 the nature of the issue.
2747
2748 In the case when KeywordString containing multiple keywords, when an EFI_NOT_FOUND
2749 error is generated during processing the second or later keyword element, the system
2750 storage associated with earlier keywords is not modified. All elements of the
2751 KeywordString must successfully pass all tests for format and access prior to making
2752 any modifications to storage.
2753
2754 In the case when EFI_DEVICE_ERROR is returned from the processing of a KeywordString
2755 containing multiple keywords, the state of storage associated with earlier keywords
2756 is undefined.
2757
2758
2759 @param This Pointer to the EFI_KEYWORD_HANDLER _PROTOCOL instance.
2760
2761 @param KeywordString A null-terminated string in <MultiKeywordResp> format.
2762
2763 @param Progress On return, points to a character in the KeywordString.
2764 Points to the string's NULL terminator if the request
2765 was successful. Points to the most recent '&' before
2766 the first failing string element if the request was
2767 not successful.
2768
2769 @param ProgressErr If during the processing of the KeywordString there was
2770 a failure, this parameter gives additional information
2771 about the possible source of the problem. The various
2772 errors are defined in "Related Definitions" below.
2773
2774
2775 @retval EFI_SUCCESS The specified action was completed successfully.
2776
2777 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:
2778 1. KeywordString is NULL.
2779 2. Parsing of the KeywordString resulted in an
2780 error. See Progress and ProgressErr for more data.
2781
2782 @retval EFI_NOT_FOUND An element of the KeywordString was not found.
2783 See ProgressErr for more data.
2784
2785 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
2786 See ProgressErr for more data.
2787
2788 @retval EFI_ACCESS_DENIED The action violated system policy. See ProgressErr
2789 for more data.
2790
2791 @retval EFI_DEVICE_ERROR An unexpected system error occurred. See ProgressErr
2792 for more data.
2793
2794 **/
2795 EFI_STATUS
2796 EFIAPI
2797 EfiConfigKeywordHandlerSetData (
2798 IN EFI_CONFIG_KEYWORD_HANDLER_PROTOCOL *This,
2799 IN CONST EFI_STRING KeywordString,
2800 OUT EFI_STRING *Progress,
2801 OUT UINT32 *ProgressErr
2802 )
2803 {
2804 CHAR8 *NameSpace;
2805 EFI_STATUS Status;
2806 CHAR16 *StringPtr;
2807 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
2808 CHAR16 *NextStringPtr;
2809 CHAR16 *KeywordData;
2810 EFI_STRING_ID KeywordStringId;
2811 UINT32 RetVal;
2812 HII_DATABASE_RECORD *DataBaseRecord;
2813 UINT8 *OpCode;
2814 CHAR16 *ConfigResp;
2815 CHAR16 *MultiConfigResp;
2816 CHAR16 *ValueElement;
2817 BOOLEAN ReadOnly;
2818 EFI_STRING InternalProgress;
2819
2820 if (This == NULL || Progress == NULL || ProgressErr == NULL || KeywordString == NULL) {
2821 return EFI_INVALID_PARAMETER;
2822 }
2823
2824 *Progress = KeywordString;
2825 *ProgressErr = KEYWORD_HANDLER_UNDEFINED_PROCESSING_ERROR;
2826 Status = EFI_SUCCESS;
2827 StringPtr = KeywordString;
2828 MultiConfigResp = NULL;
2829 NameSpace = NULL;
2830 DevicePath = NULL;
2831 KeywordData = NULL;
2832 ValueElement = NULL;
2833 ConfigResp = NULL;
2834 KeywordStringId = 0;
2835
2836 while ((StringPtr != NULL) && (*StringPtr != L'\0')) {
2837 //
2838 // 1. Get NameSpace from NameSpaceId keyword.
2839 //
2840 Status = ExtractNameSpace (StringPtr, &NameSpace, &NextStringPtr);
2841 if (EFI_ERROR (Status)) {
2842 *ProgressErr = KEYWORD_HANDLER_MALFORMED_STRING;
2843 return Status;
2844 }
2845 StringPtr = NextStringPtr;
2846
2847 //
2848 // 2. Get possible Device Path info from KeywordString.
2849 //
2850 Status = ExtractDevicePath (StringPtr, (UINT8 **)&DevicePath, &NextStringPtr);
2851 if (EFI_ERROR (Status)) {
2852 *ProgressErr = KEYWORD_HANDLER_MALFORMED_STRING;
2853 goto Done;
2854 }
2855 StringPtr = NextStringPtr;
2856
2857 //
2858 // 3. Extract keyword from the KeywordRequest string.
2859 //
2860 Status = ExtractKeyword(StringPtr, &KeywordData, &NextStringPtr);
2861 if (EFI_ERROR (Status)) {
2862 //
2863 // Can't find Keyword base on the input device path info.
2864 //
2865 *ProgressErr = KEYWORD_HANDLER_MALFORMED_STRING;
2866 Status = EFI_INVALID_PARAMETER;
2867 goto Done;
2868 }
2869 StringPtr = NextStringPtr;
2870
2871 //
2872 // 4. Extract Value from the KeywordRequest string.
2873 //
2874 Status = ExtractValue (StringPtr, &ValueElement, &NextStringPtr);
2875 if (EFI_ERROR (Status)) {
2876 //
2877 // Can't find Value base on the input device path info.
2878 //
2879 *ProgressErr = KEYWORD_HANDLER_MALFORMED_STRING;
2880 Status = EFI_INVALID_PARAMETER;
2881 goto Done;
2882 }
2883 StringPtr = NextStringPtr;
2884
2885 //
2886 // 5. Find ReadOnly filter.
2887 //
2888 if ((StringPtr != NULL) && StrnCmp (StringPtr, L"&ReadOnly", StrLen (L"&ReadOnly")) == 0) {
2889 ReadOnly = TRUE;
2890 StringPtr += StrLen (L"&ReadOnly");
2891 } else {
2892 ReadOnly = FALSE;
2893 }
2894
2895 //
2896 // 6. Get EFI_STRING_ID for the input keyword.
2897 //
2898 Status = GetStringIdFromDatabase (&DevicePath, &NameSpace, KeywordData, &RetVal, &KeywordStringId, &DataBaseRecord);
2899 if (EFI_ERROR (Status)) {
2900 *ProgressErr = RetVal;
2901 goto Done;
2902 }
2903
2904 //
2905 // 7. Construct the ConfigRequest string.
2906 //
2907 Status = ExtractConfigResp (DataBaseRecord, KeywordStringId, ValueElement, &OpCode, &ConfigResp);
2908 if (EFI_ERROR (Status)) {
2909 goto Done;
2910 }
2911
2912 //
2913 // 8. Check the readonly flag.
2914 //
2915 if (ExtractReadOnlyFromOpCode (OpCode) != ReadOnly) {
2916 *ProgressErr = KEYWORD_HANDLER_INCOMPATIBLE_VALUE_DETECTED;
2917 Status = EFI_INVALID_PARAMETER;
2918 goto Done;
2919 }
2920
2921 //
2922 // 9. Merge to the MultiKeywordResp string.
2923 //
2924 Status = MergeToMultiKeywordResp(&MultiConfigResp, &ConfigResp);
2925 if (EFI_ERROR (Status)) {
2926 goto Done;
2927 }
2928
2929 //
2930 // 10. Clean the temp buffer point.
2931 //
2932 FreePool (NameSpace);
2933 FreePool (DevicePath);
2934 FreePool (KeywordData);
2935 FreePool (ValueElement);
2936 NameSpace = NULL;
2937 DevicePath = NULL;
2938 KeywordData = NULL;
2939 ValueElement = NULL;
2940 if (ConfigResp != NULL) {
2941 FreePool (ConfigResp);
2942 ConfigResp = NULL;
2943 }
2944 }
2945
2946 //
2947 // 11. Set value to driver.
2948 //
2949 Status = mPrivate.ConfigRouting.RouteConfig(
2950 &mPrivate.ConfigRouting,
2951 (EFI_STRING) MultiConfigResp,
2952 &InternalProgress
2953 );
2954 if (EFI_ERROR (Status)) {
2955 Status = EFI_DEVICE_ERROR;
2956 goto Done;
2957 }
2958
2959 *ProgressErr = KEYWORD_HANDLER_NO_ERROR;
2960
2961 Done:
2962 if (NameSpace != NULL) {
2963 FreePool (NameSpace);
2964 }
2965 if (DevicePath != NULL) {
2966 FreePool (DevicePath);
2967 }
2968 if (KeywordData != NULL) {
2969 FreePool (KeywordData);
2970 }
2971 if (ValueElement != NULL) {
2972 FreePool (ValueElement);
2973 }
2974 if (ConfigResp != NULL) {
2975 FreePool (ConfigResp);
2976 }
2977 if (MultiConfigResp != NULL && MultiConfigResp != ConfigResp) {
2978 FreePool (MultiConfigResp);
2979 }
2980 *Progress = StringPtr;
2981 return Status;
2982 }
2983
2984 /**
2985
2986 This function accepts a <MultiKeywordRequest> formatted string, finds the underlying
2987 keyword owners, creates a <MultiConfigRequest> string from it and forwards it to the
2988 EFI_HII_ROUTING_PROTOCOL.ExtractConfig function.
2989
2990 If there is an issue in resolving the contents of the KeywordString, then the function
2991 returns an EFI_INVALID_PARAMETER and also set the Progress and ProgressErr with the
2992 appropriate information about where the issue occurred and additional data about the
2993 nature of the issue.
2994
2995 In the case when KeywordString is NULL, or contains multiple keywords, or when
2996 EFI_NOT_FOUND is generated while processing the keyword elements, the Results string
2997 contains values returned for all keywords processed prior to the keyword generating the
2998 error but no values for the keyword with error or any following keywords.
2999
3000
3001 @param This Pointer to the EFI_KEYWORD_HANDLER _PROTOCOL instance.
3002
3003 @param NameSpaceId A null-terminated string containing the platform configuration
3004 language to search through in the system. If a NULL is passed
3005 in, then it is assumed that any platform configuration language
3006 with the prefix of "x-UEFI-" are searched.
3007
3008 @param KeywordString A null-terminated string in <MultiKeywordRequest> format. If a
3009 NULL is passed in the KeywordString field, all of the known
3010 keywords in the system for the NameSpaceId specified are
3011 returned in the Results field.
3012
3013 @param Progress On return, points to a character in the KeywordString. Points
3014 to the string's NULL terminator if the request was successful.
3015 Points to the most recent '&' before the first failing string
3016 element if the request was not successful.
3017
3018 @param ProgressErr If during the processing of the KeywordString there was a
3019 failure, this parameter gives additional information about the
3020 possible source of the problem. See the definitions in SetData()
3021 for valid value definitions.
3022
3023 @param Results A null-terminated string in <MultiKeywordResp> format is returned
3024 which has all the values filled in for the keywords in the
3025 KeywordString. This is a callee-allocated field, and must be freed
3026 by the caller after being used.
3027
3028 @retval EFI_SUCCESS The specified action was completed successfully.
3029
3030 @retval EFI_INVALID_PARAMETER One or more of the following are TRUE:
3031 1.Progress, ProgressErr, or Resuts is NULL.
3032 2.Parsing of the KeywordString resulted in an error. See
3033 Progress and ProgressErr for more data.
3034
3035
3036 @retval EFI_NOT_FOUND An element of the KeywordString was not found. See
3037 ProgressErr for more data.
3038
3039 @retval EFI_NOT_FOUND The NamespaceId specified was not found. See ProgressErr
3040 for more data.
3041
3042 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated. See
3043 ProgressErr for more data.
3044
3045 @retval EFI_ACCESS_DENIED The action violated system policy. See ProgressErr for
3046 more data.
3047
3048 @retval EFI_DEVICE_ERROR An unexpected system error occurred. See ProgressErr
3049 for more data.
3050
3051 **/
3052 EFI_STATUS
3053 EFIAPI
3054 EfiConfigKeywordHandlerGetData (
3055 IN EFI_CONFIG_KEYWORD_HANDLER_PROTOCOL *This,
3056 IN CONST EFI_STRING NameSpaceId, OPTIONAL
3057 IN CONST EFI_STRING KeywordString, OPTIONAL
3058 OUT EFI_STRING *Progress,
3059 OUT UINT32 *ProgressErr,
3060 OUT EFI_STRING *Results
3061 )
3062 {
3063 CHAR8 *NameSpace;
3064 EFI_STATUS Status;
3065 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
3066 HII_DATABASE_RECORD *DataBaseRecord;
3067 CHAR16 *StringPtr;
3068 CHAR16 *NextStringPtr;
3069 CHAR16 *KeywordData;
3070 EFI_STRING_ID KeywordStringId;
3071 UINT8 *OpCode;
3072 CHAR16 *ConfigRequest;
3073 CHAR16 *ValueElement;
3074 UINT32 RetVal;
3075 BOOLEAN ReadOnly;
3076 CHAR16 *KeywordResp;
3077 CHAR16 *MultiKeywordResp;
3078
3079 if (This == NULL || Progress == NULL || ProgressErr == NULL || Results == NULL) {
3080 return EFI_INVALID_PARAMETER;
3081 }
3082
3083 *ProgressErr = KEYWORD_HANDLER_UNDEFINED_PROCESSING_ERROR;
3084 Status = EFI_SUCCESS;
3085 DevicePath = NULL;
3086 NameSpace = NULL;
3087 KeywordData = NULL;
3088 ConfigRequest= NULL;
3089 StringPtr = KeywordString;
3090 ReadOnly = FALSE;
3091 MultiKeywordResp = NULL;
3092 KeywordStringId = 0;
3093
3094 //
3095 // 1. Get NameSpace from NameSpaceId keyword.
3096 //
3097 Status = ExtractNameSpace (NameSpaceId, &NameSpace, NULL);
3098 if (EFI_ERROR (Status)) {
3099 *ProgressErr = KEYWORD_HANDLER_NAMESPACE_ID_NOT_FOUND;
3100 return Status;
3101 }
3102
3103 if (KeywordString != NULL) {
3104 StringPtr = KeywordString;
3105
3106 while (*StringPtr != L'\0') {
3107 //
3108 // 2. Get possible Device Path info from KeywordString.
3109 //
3110 Status = ExtractDevicePath (StringPtr, (UINT8 **)&DevicePath, &NextStringPtr);
3111 if (EFI_ERROR (Status)) {
3112 *ProgressErr = KEYWORD_HANDLER_MALFORMED_STRING;
3113 goto Done;
3114 }
3115 StringPtr = NextStringPtr;
3116
3117
3118 //
3119 // 3. Process Keyword section from the input keywordRequest string.
3120 //
3121 // 3.1 Extract keyword from the KeywordRequest string.
3122 //
3123 Status = ExtractKeyword(StringPtr, &KeywordData, &NextStringPtr);
3124 if (EFI_ERROR (Status)) {
3125 //
3126 // Can't find Keyword base on the input device path info.
3127 //
3128 *ProgressErr = KEYWORD_HANDLER_MALFORMED_STRING;
3129 Status = EFI_INVALID_PARAMETER;
3130 goto Done;
3131 }
3132
3133 //
3134 // 3.2 Get EFI_STRING_ID for the input keyword.
3135 //
3136 Status = GetStringIdFromDatabase (&DevicePath, &NameSpace, KeywordData, &RetVal, &KeywordStringId, &DataBaseRecord);
3137 if (EFI_ERROR (Status)) {
3138 *ProgressErr = RetVal;
3139 goto Done;
3140 }
3141
3142 //
3143 // 3.3 Construct the ConfigRequest string.
3144 //
3145 Status = ExtractConfigRequest (DataBaseRecord, KeywordStringId, &OpCode, &ConfigRequest);
3146 if (EFI_ERROR (Status)) {
3147 goto Done;
3148 }
3149
3150 //
3151 // 3.4 Extract Value for the input keyword.
3152 //
3153 Status = ExtractValueFromDriver(ConfigRequest, &ValueElement);
3154 if (EFI_ERROR (Status)) {
3155 if (Status != EFI_OUT_OF_RESOURCES) {
3156 Status = EFI_DEVICE_ERROR;
3157 }
3158 goto Done;
3159 }
3160 StringPtr = NextStringPtr;
3161
3162 //
3163 // 4. Process the possible filter section.
3164 //
3165 RetVal = ValidateFilter (OpCode, StringPtr, &NextStringPtr, &ReadOnly);
3166 if (RetVal != KEYWORD_HANDLER_NO_ERROR) {
3167 *ProgressErr = RetVal;
3168 Status = EFI_INVALID_PARAMETER;
3169 goto Done;
3170 }
3171 StringPtr = NextStringPtr;
3172
3173
3174 //
3175 // 5. Generate KeywordResp string.
3176 //
3177 Status = GenerateKeywordResp(NameSpace, DevicePath, KeywordData, ValueElement, ReadOnly, &KeywordResp);
3178 if (Status != EFI_SUCCESS) {
3179 goto Done;
3180 }
3181
3182 //
3183 // 6. Merge to the MultiKeywordResp string.
3184 //
3185 Status = MergeToMultiKeywordResp(&MultiKeywordResp, &KeywordResp);
3186 if (EFI_ERROR (Status)) {
3187 goto Done;
3188 }
3189
3190 //
3191 // 7. Update return value.
3192 //
3193 *Results = MultiKeywordResp;
3194
3195 //
3196 // 8. Clean the temp buffer.
3197 //
3198 FreePool (DevicePath);
3199 FreePool (KeywordData);
3200 FreePool (ValueElement);
3201 FreePool (ConfigRequest);
3202 DevicePath = NULL;
3203 KeywordData = NULL;
3204 ValueElement = NULL;
3205 ConfigRequest = NULL;
3206 if (KeywordResp != NULL) {
3207 FreePool (KeywordResp);
3208 KeywordResp = NULL;
3209 }
3210 }
3211 } else {
3212 //
3213 // Enumerate all keyword in the system.
3214 //
3215 Status = EnumerateAllKeywords(NameSpace, &MultiKeywordResp);
3216 if (EFI_ERROR (Status)) {
3217 goto Done;
3218 }
3219 *Results = MultiKeywordResp;
3220 }
3221
3222 *ProgressErr = KEYWORD_HANDLER_NO_ERROR;
3223
3224 Done:
3225 if (NameSpace != NULL) {
3226 FreePool (NameSpace);
3227 }
3228 if (DevicePath != NULL) {
3229 FreePool (DevicePath);
3230 }
3231 if (KeywordData != NULL) {
3232 FreePool (KeywordData);
3233 }
3234 *Progress = StringPtr;
3235 return Status;
3236 }