]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/HiiDatabaseDxe/ConfigRouting.c
Remove some useless EDK_RELEASE_VERSION, EFI_SPECIFICATION_VERSION ,and review VALID_...
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / ConfigRouting.c
1 /** @file
2
3 Copyright (c) 2007 - 2008, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 ConfigRouting.c
15
16 Abstract:
17
18 Implementation for EFI_HII_CONFIG_ROUTING_PROTOCOL.
19
20 Revision History
21
22
23 **/
24
25
26 #include "HiiDatabase.h"
27
28 /**
29 Calculate the number of Unicode characters of the incoming Configuration string,
30 not including NULL terminator.
31
32 This is a internal function.
33
34 @param String String in <MultiConfigRequest> or
35 <MultiConfigResp> format.
36
37 @return The number of Unicode characters.
38
39 **/
40 UINTN
41 CalculateConfigStringLen (
42 IN EFI_STRING String
43 )
44 {
45 UINTN Length;
46
47 //
48 // "GUID=" should be the first element of incoming string.
49 //
50 ASSERT (String != NULL);
51 ASSERT (StrnCmp (String, L"GUID=", StrLen (L"GUID=")) == 0);
52
53 Length = StrLen (L"GUID=");
54 String += Length;
55
56 //
57 // The beginning of next <ConfigRequest>/<ConfigResp> should be "&GUID=".
58 // Will meet '\0' if there is only one <ConfigRequest>/<ConfigResp>.
59 //
60 while (*String != 0 && StrnCmp (String, L"&GUID=", StrLen (L"&GUID=")) != 0) {
61 Length++;
62 String++;
63 }
64
65 return Length;
66 }
67
68
69 /**
70 Convert the hex UNICODE %02x encoding of a UEFI device path to binary
71 from <PathHdr> of <ConfigHdr>.
72
73 This is a internal function.
74
75 @param String UEFI configuration string
76 @param DevicePath binary of a UEFI device path.
77
78 @retval EFI_INVALID_PARAMETER Any incoming parameter is invalid.
79 @retval EFI_OUT_OF_RESOURCES Lake of resources to store neccesary structures.
80 @retval EFI_SUCCESS The device path is retrieved and translated to
81 binary format.
82
83 **/
84 EFI_STATUS
85 GetDevicePath (
86 IN EFI_STRING String,
87 OUT UINT8 **DevicePath
88 )
89 {
90 UINTN Length;
91 EFI_STRING PathHdr;
92 EFI_STRING DevicePathString;
93
94 if (String == NULL || DevicePath == NULL) {
95 return EFI_INVALID_PARAMETER;
96 }
97
98 //
99 // Find the 'PATH=' of <PathHdr> and skip it.
100 //
101 for (; (*String != 0 && StrnCmp (String, L"PATH=", StrLen (L"PATH=")) != 0); String++);
102 if (*String == 0) {
103 return EFI_INVALID_PARAMETER;
104 }
105
106 String += StrLen (L"PATH=");
107 PathHdr = String;
108
109 //
110 // The content between 'PATH=' of <ConfigHdr> and '&' of next element
111 // or '\0' (end of configuration string) is the UNICODE %02x bytes encoding
112 // of UEFI device path.
113 //
114 for (Length = 0; *String != 0 && *String != L'&'; String++, Length++);
115 DevicePathString = (EFI_STRING) AllocateZeroPool ((Length + 1) * sizeof (CHAR16));
116 if (DevicePathString == NULL) {
117 return EFI_OUT_OF_RESOURCES;
118 }
119 StrnCpy (DevicePathString, PathHdr, Length);
120 *(DevicePathString + Length) = 0;
121
122 //
123 // The data in <PathHdr> is encoded as hex UNICODE %02x bytes in the same order
124 // as the device path resides in RAM memory.
125 // Translate the data into binary.
126 //
127 Length /= 2;
128 *DevicePath = (UINT8 *) AllocateZeroPool (Length);
129 if (*DevicePath == NULL) {
130 FreePool (DevicePathString);
131 return EFI_OUT_OF_RESOURCES;
132 }
133
134 HexStringToBufInReverseOrder (*DevicePath, &Length, DevicePathString);
135
136 FreePool (DevicePathString);
137
138 return EFI_SUCCESS;
139
140 }
141
142
143 /**
144 Generate a sub string then output it.
145
146 This is a internal function.
147
148 @param String A constant string which is the prefix of the to be
149 generated string, e.g. GUID=
150 @param BufferLen The length of the Buffer in bytes.
151 @param Buffer Points to a buffer which will be converted to be the
152 content of the generated string.
153 @param Flag If 1, the buffer contains data for the value of GUID or PATH stored in
154 UINT8 *; if 2, the buffer contains unicode string for the value of NAME;
155 if 3, the buffer contains other data.
156 @param SubStr Points to the output string. It's caller's
157 responsibility to free this buffer.
158
159
160 **/
161 VOID
162 GenerateSubStr (
163 IN CONST EFI_STRING String,
164 IN UINTN BufferLen,
165 IN VOID *Buffer,
166 IN UINT8 Flag,
167 OUT EFI_STRING *SubStr
168 )
169 {
170 UINTN Length;
171 EFI_STRING Str;
172 EFI_STATUS Status;
173 EFI_STRING StringHeader;
174
175 ASSERT (String != NULL && SubStr != NULL);
176
177 if (Buffer == NULL) {
178 *SubStr = AllocateCopyPool (StrSize (String), String);
179 ASSERT (*SubStr != NULL);
180 return ;
181 }
182
183 Length = StrLen (String) + BufferLen * 2 + 1 + 1;
184 Str = AllocateZeroPool (Length * sizeof (CHAR16));
185 ASSERT (Str != NULL);
186
187 StrCpy (Str, String);
188 Length = (BufferLen * 2 + 1) * sizeof (CHAR16);
189
190 Status = EFI_SUCCESS;
191 StringHeader = Str + StrLen (String);
192
193 switch (Flag) {
194 case 1:
195 Status = BufInReverseOrderToHexString (StringHeader, (UINT8 *) Buffer, BufferLen);
196 break;
197 case 2:
198 Status = UnicodeToConfigString (StringHeader, &Length, (CHAR16 *) Buffer);
199 break;
200 case 3:
201 Status = BufToHexString (StringHeader, &Length, (UINT8 *) Buffer, BufferLen);
202 //
203 // Convert the uppercase to lowercase since <HexAf> is defined in lowercase format.
204 //
205 ToLower (StringHeader);
206 break;
207 default:
208 break;
209 }
210
211 ASSERT_EFI_ERROR (Status);
212 StrCat (Str, L"&");
213
214 *SubStr = Str;
215 }
216
217
218 /**
219 Retrieve the <ConfigBody> from String then output it.
220
221 This is a internal function.
222
223 @param String A sub string of a configuration string in
224 <MultiConfigAltResp> format.
225 @param ConfigBody Points to the output string. It's caller's
226 responsibility to free this buffer.
227
228 @retval EFI_INVALID_PARAMETER There is no form package in current hii database.
229 @retval EFI_OUT_OF_RESOURCES Not enough memory to finish this operation.
230 @retval EFI_SUCCESS All existing storage is exported.
231
232 **/
233 EFI_STATUS
234 OutputConfigBody (
235 IN EFI_STRING String,
236 OUT EFI_STRING *ConfigBody
237 )
238 {
239 EFI_STRING TmpPtr;
240 EFI_STRING Result;
241 UINTN Length;
242
243 if (String == NULL || ConfigBody == NULL) {
244 return EFI_INVALID_PARAMETER;
245 }
246
247 TmpPtr = StrStr (String, L"GUID=");
248 if (TmpPtr == NULL) {
249 //
250 // It is the last <ConfigResp> of the incoming configuration string.
251 //
252 Result = AllocateCopyPool (StrSize (String), String);
253 if (Result == NULL) {
254 return EFI_OUT_OF_RESOURCES;
255 } else {
256 *ConfigBody = Result;
257 return EFI_SUCCESS;
258 }
259 }
260
261 Length = TmpPtr - String;
262 Result = AllocateCopyPool (Length * sizeof (CHAR16), String);
263 if (Result == NULL) {
264 return EFI_OUT_OF_RESOURCES;
265 }
266
267 *(Result + Length - 1) = 0;
268 *ConfigBody = Result;
269 return EFI_SUCCESS;
270
271 }
272
273 /**
274 Append a string to a multi-string format.
275
276 This is a internal function.
277
278 @param MultiString String in <MultiConfigRequest>,
279 <MultiConfigAltResp>, or <MultiConfigResp>. On
280 input, the buffer length of this string is
281 MAX_STRING_LENGTH. On output, the buffer length
282 might be updated.
283 @param AppendString NULL-terminated Unicode string.
284
285 @retval EFI_INVALID_PARAMETER Any incoming parameter is invalid.
286 @retval EFI_SUCCESS AppendString is append to the end of MultiString
287
288 **/
289 EFI_STATUS
290 AppendToMultiString (
291 IN OUT EFI_STRING *MultiString,
292 IN EFI_STRING AppendString
293 )
294 {
295 UINTN AppendStringSize;
296 UINTN MultiStringSize;
297
298 if (MultiString == NULL || *MultiString == NULL || AppendString == NULL) {
299 return EFI_INVALID_PARAMETER;
300 }
301
302 AppendStringSize = StrSize (AppendString);
303 MultiStringSize = StrSize (*MultiString);
304
305 //
306 // Enlarge the buffer each time when length exceeds MAX_STRING_LENGTH.
307 //
308 if (MultiStringSize + AppendStringSize > MAX_STRING_LENGTH ||
309 MultiStringSize > MAX_STRING_LENGTH) {
310 *MultiString = (EFI_STRING) ReallocatePool (
311 MultiStringSize,
312 MultiStringSize + AppendStringSize,
313 (VOID *) (*MultiString)
314 );
315 }
316
317 //
318 // Append the incoming string
319 //
320 StrCat (*MultiString, AppendString);
321
322 return EFI_SUCCESS;
323 }
324
325
326 /**
327 Get the value of <Number> in <BlockConfig> format, i.e. the value of OFFSET
328 or WIDTH or VALUE.
329 <BlockConfig> ::= 'OFFSET='<Number>&'WIDTH='<Number>&'VALUE'=<Number>
330
331 This is a internal function.
332
333 @param StringPtr String in <BlockConfig> format and points to the
334 first character of <Number>.
335 @param Number The output value. Caller takes the responsibility
336 to free memory.
337 @param Len Length of the <Number>, in characters.
338
339 @retval EFI_OUT_OF_RESOURCES Insufficient resources to store neccessary
340 structures.
341 @retval EFI_SUCCESS Value of <Number> is outputted in Number
342 successfully.
343
344 **/
345 EFI_STATUS
346 GetValueOfNumber (
347 IN EFI_STRING StringPtr,
348 OUT UINT8 **Number,
349 OUT UINTN *Len
350 )
351 {
352 EFI_STRING TmpPtr;
353 UINTN Length;
354 EFI_STRING Str;
355 UINT8 *Buf;
356 EFI_STATUS Status;
357
358 ASSERT (StringPtr != NULL && Number != NULL && Len != NULL);
359 ASSERT (*StringPtr != 0);
360
361 Buf = NULL;
362
363 TmpPtr = StringPtr;
364 while (*StringPtr != 0 && *StringPtr != L'&') {
365 StringPtr++;
366 }
367 *Len = StringPtr - TmpPtr;
368 Length = *Len + 1;
369
370 Str = (EFI_STRING) AllocateZeroPool (Length * sizeof (EFI_STRING));
371 if (Str == NULL) {
372 Status = EFI_OUT_OF_RESOURCES;
373 goto Exit;
374 }
375 CopyMem (Str, TmpPtr, *Len * sizeof (CHAR16));
376 *(Str + *Len) = 0;
377
378 Length = (Length + 1) / 2;
379 Buf = (UINT8 *) AllocateZeroPool (Length);
380 if (Buf == NULL) {
381 Status = EFI_OUT_OF_RESOURCES;
382 goto Exit;
383 }
384
385 Status = HexStringToBuf (Buf, &Length, Str, NULL);
386 if (EFI_ERROR (Status)) {
387 goto Exit;
388 }
389
390 *Number = Buf;
391 Status = EFI_SUCCESS;
392
393 Exit:
394 if (Str != NULL) {
395 FreePool (Str);
396 }
397 return Status;
398 }
399
400
401 /**
402 This function allows a caller to extract the current configuration
403 for one or more named elements from one or more drivers.
404
405 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
406 instance.
407 @param Request A null-terminated Unicode string in
408 <MultiConfigRequest> format.
409 @param Progress On return, points to a character in the Request
410 string. Points to the string's null terminator if
411 request was successful. Points to the most recent
412 & before the first failing name / value pair (or
413 the beginning of the string if the failure is in
414 the first name / value pair) if the request was
415 not successful.
416 @param Results Null-terminated Unicode string in
417 <MultiConfigAltResp> format which has all values
418 filled in for the names in the Request string.
419 String to be allocated by the called function.
420
421 @retval EFI_SUCCESS The Results string is filled with the values
422 corresponding to all requested names.
423 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the parts of the
424 results that must be stored awaiting possible
425 future protocols.
426 @retval EFI_NOT_FOUND Routing data doesn't match any known driver.
427 Progress set to the "G" in "GUID" of the routing
428 header that doesn't match. Note: There is no
429 requirement that all routing data be validated
430 before any configuration extraction.
431 @retval EFI_INVALID_PARAMETER For example, passing in a NULL for the Request
432 parameter would result in this type of error. The
433 Progress parameter is set to NULL.
434 @retval EFI_INVALID_PARAMETER Illegal syntax. Progress set to most recent &
435 before the error or the beginning of the string.
436 @retval EFI_INVALID_PARAMETER Unknown name. Progress points to the & before the
437 name in question.
438
439 **/
440 EFI_STATUS
441 EFIAPI
442 HiiConfigRoutingExtractConfig (
443 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
444 IN CONST EFI_STRING Request,
445 OUT EFI_STRING *Progress,
446 OUT EFI_STRING *Results
447 )
448 {
449 HII_DATABASE_PRIVATE_DATA *Private;
450 EFI_STRING StringPtr;
451 EFI_STRING ConfigRequest;
452 UINTN Length;
453 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
454 EFI_STATUS Status;
455 LIST_ENTRY *Link;
456 HII_DATABASE_RECORD *Database;
457 UINT8 *DevicePathPkg;
458 UINT8 *CurrentDevicePath;
459 EFI_HANDLE DriverHandle;
460 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
461 EFI_STRING AccessProgress;
462 EFI_STRING AccessResults;
463 BOOLEAN FirstElement;
464
465 //
466 // For size reduction, please define PcdSupportFullConfigRoutingProtocol
467 // as FALSE. But this renders the system to not 100% compliant with
468 // UEFI 2.1. Use this with caution.
469 //
470 if (!FeaturePcdGet (PcdSupportFullConfigRoutingProtocol)) {
471 return EFI_UNSUPPORTED;
472 }
473
474 if (This == NULL || Progress == NULL || Results == NULL) {
475 return EFI_INVALID_PARAMETER;
476 }
477
478 if (Request == NULL) {
479 *Progress = NULL;
480 return EFI_INVALID_PARAMETER;
481 }
482
483 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
484 StringPtr = Request;
485 *Progress = StringPtr;
486
487 //
488 // The first element of <MultiConfigRequest> should be
489 // <GuidHdr>, which is in 'GUID='<Guid> syntax.
490 //
491 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
492 return EFI_INVALID_PARAMETER;
493 }
494
495 FirstElement = TRUE;
496
497 //
498 // Allocate a fix length of memory to store Results. Reallocate memory for
499 // Results if this fix length is insufficient.
500 //
501 *Results = (EFI_STRING) AllocateZeroPool (MAX_STRING_LENGTH);
502 if (*Results == NULL) {
503 return EFI_OUT_OF_RESOURCES;
504 }
505
506 while (*StringPtr != 0 && StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) == 0) {
507 //
508 // If parsing error, set Progress to the beginning of the <MultiConfigRequest>
509 // or most recent & before the error.
510 //
511 if (StringPtr == Request) {
512 *Progress = StringPtr;
513 } else {
514 *Progress = StringPtr - 1;
515 }
516
517 //
518 // Process each <ConfigRequest> of <MultiConfigRequest>
519 //
520 Length = CalculateConfigStringLen (StringPtr);
521 ConfigRequest = AllocateCopyPool ((Length + 1) * sizeof (CHAR16), StringPtr);
522 if (ConfigRequest == NULL) {
523 return EFI_OUT_OF_RESOURCES;
524 }
525 *(ConfigRequest + Length) = 0;
526
527 //
528 // Get the UEFI device path
529 //
530 Status = GetDevicePath (ConfigRequest, (UINT8 **) &DevicePath);
531 if (EFI_ERROR (Status)) {
532 FreePool (ConfigRequest);
533 return Status;
534 }
535
536 //
537 // Find driver which matches the routing data.
538 //
539 DriverHandle = NULL;
540 for (Link = Private->DatabaseList.ForwardLink;
541 Link != &Private->DatabaseList;
542 Link = Link->ForwardLink
543 ) {
544 Database = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
545
546 if ((DevicePathPkg = Database->PackageList->DevicePathPkg) != NULL) {
547 CurrentDevicePath = DevicePathPkg + sizeof (EFI_HII_PACKAGE_HEADER);
548 if (CompareMem (
549 DevicePath,
550 CurrentDevicePath,
551 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) CurrentDevicePath)
552 ) == 0) {
553 DriverHandle = Database->DriverHandle;
554 break;
555 }
556 }
557 }
558
559 FreePool (DevicePath);
560
561 if (DriverHandle == NULL) {
562 //
563 // Routing data does not match any known driver.
564 // Set Progress to the 'G' in "GUID" of the routing header.
565 //
566 *Progress = StringPtr;
567 FreePool (ConfigRequest);
568 return EFI_NOT_FOUND;
569 }
570
571 //
572 // Call corresponding ConfigAccess protocol to extract settings
573 //
574 Status = gBS->HandleProtocol (
575 DriverHandle,
576 &gEfiHiiConfigAccessProtocolGuid,
577 (VOID **) &ConfigAccess
578 );
579 ASSERT_EFI_ERROR (Status);
580
581 Status = ConfigAccess->ExtractConfig (
582 ConfigAccess,
583 ConfigRequest,
584 &AccessProgress,
585 &AccessResults
586 );
587 if (EFI_ERROR (Status)) {
588 //
589 // AccessProgress indicates the parsing progress on <ConfigRequest>.
590 // Map it to the progress on <MultiConfigRequest> then return it.
591 //
592 *Progress = StrStr (StringPtr, AccessProgress);
593 FreePool (ConfigRequest);
594 return Status;
595 }
596
597 //
598 // Attach this <ConfigAltResp> to a <MultiConfigAltResp>. There is a '&'
599 // which seperates the first <ConfigAltResp> and the following ones.
600 //
601 ASSERT (*AccessProgress == 0);
602
603 if (!FirstElement) {
604 Status = AppendToMultiString (Results, L"&");
605 ASSERT_EFI_ERROR (Status);
606 }
607
608 Status = AppendToMultiString (Results, AccessResults);
609 ASSERT_EFI_ERROR (Status);
610
611 FirstElement = FALSE;
612
613 FreePool (AccessResults);
614 AccessResults = NULL;
615 FreePool (ConfigRequest);
616 ConfigRequest = NULL;
617
618 //
619 // Go to next <ConfigRequest> (skip '&').
620 //
621 StringPtr += Length;
622 if (*StringPtr == 0) {
623 *Progress = StringPtr;
624 break;
625 }
626
627 StringPtr++;
628
629 }
630
631 return EFI_SUCCESS;
632
633 }
634
635
636 /**
637 This function allows the caller to request the current configuration for the
638 entirety of the current HII database and returns the data in a
639 null-terminated Unicode string.
640
641 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
642 instance.
643 @param Results Null-terminated Unicode string in
644 <MultiConfigAltResp> format which has all values
645 filled in for the names in the Request string.
646 String to be allocated by the called function.
647 De-allocation is up to the caller.
648
649 @retval EFI_SUCCESS The Results string is filled with the values
650 corresponding to all requested names.
651 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the parts of the
652 results that must be stored awaiting possible
653 future protocols.
654 @retval EFI_INVALID_PARAMETER For example, passing in a NULL for the Results
655 parameter would result in this type of error.
656
657 **/
658 EFI_STATUS
659 EFIAPI
660 HiiConfigRoutingExportConfig (
661 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
662 OUT EFI_STRING *Results
663 )
664 {
665 EFI_STATUS Status;
666 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
667 EFI_STRING AccessResults;
668 UINTN Index;
669 EFI_HANDLE *ConfigAccessHandles;
670 UINTN NumberConfigAccessHandles;
671 BOOLEAN FirstElement;
672
673 //
674 // For size reduction, please define PcdSupportFullConfigRoutingProtocol
675 // as FALSE. But this renders the system to not 100% compliant with
676 // UEFI 2.1. Use this with caution.
677 //
678 if (!FeaturePcdGet (PcdSupportFullConfigRoutingProtocol)) {
679 return EFI_UNSUPPORTED;
680 }
681
682 if (This == NULL || Results == NULL) {
683 return EFI_INVALID_PARAMETER;
684 }
685
686 //
687 // Allocate a fix length of memory to store Results. Reallocate memory for
688 // Results if this fix length is insufficient.
689 //
690 *Results = (EFI_STRING) AllocateZeroPool (MAX_STRING_LENGTH);
691 if (*Results == NULL) {
692 return EFI_OUT_OF_RESOURCES;
693 }
694
695 NumberConfigAccessHandles = 0;
696 Status = gBS->LocateHandleBuffer (
697 ByProtocol,
698 &gEfiHiiConfigAccessProtocolGuid,
699 NULL,
700 &NumberConfigAccessHandles,
701 &ConfigAccessHandles
702 );
703 if (EFI_ERROR (Status)) {
704 return Status;
705 }
706
707 FirstElement = TRUE;
708
709 for (Index = 0; Index < NumberConfigAccessHandles; Index++) {
710 Status = gBS->HandleProtocol (
711 ConfigAccessHandles[Index],
712 &gEfiHiiConfigAccessProtocolGuid,
713 (VOID **) &ConfigAccess
714 );
715 if (EFI_ERROR (Status)) {
716 continue;
717 }
718
719 Status = ConfigAccess->ExtractConfig (
720 ConfigAccess,
721 NULL,
722 NULL,
723 &AccessResults
724 );
725 if (!EFI_ERROR (Status)) {
726 //
727 // Attach this <ConfigAltResp> to a <MultiConfigAltResp>. There is a '&'
728 // which seperates the first <ConfigAltResp> and the following ones.
729 //
730 if (!FirstElement) {
731 Status = AppendToMultiString (Results, L"&");
732 ASSERT_EFI_ERROR (Status);
733 }
734
735 Status = AppendToMultiString (Results, AccessResults);
736 ASSERT_EFI_ERROR (Status);
737
738 FirstElement = FALSE;
739
740 FreePool (AccessResults);
741 AccessResults = NULL;
742 }
743 }
744 FreePool (ConfigAccessHandles);
745
746 return EFI_SUCCESS;
747 }
748
749
750 /**
751 This function processes the results of processing forms and routes it to the
752 appropriate handlers or storage.
753
754 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
755 instance.
756 @param Configuration A null-terminated Unicode string in
757 <MulltiConfigResp> format.
758 @param Progress A pointer to a string filled in with the offset of
759 the most recent & before the first failing name /
760 value pair (or the beginning of the string if the
761 failure is in the first name / value pair) or the
762 terminating NULL if all was successful.
763
764 @retval EFI_SUCCESS The results have been distributed or are awaiting
765 distribution.
766 @retval EFI_OUT_OF_RESOURCES Not enough memory to store the parts of the
767 results that must be stored awaiting possible
768 future protocols.
769 @retval EFI_INVALID_PARAMETER Passing in a NULL for the Configuration parameter
770 would result in this type of error.
771 @retval EFI_NOT_FOUND Target for the specified routing data was not
772 found.
773
774 **/
775 EFI_STATUS
776 EFIAPI
777 HiiConfigRoutingRouteConfig (
778 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
779 IN CONST EFI_STRING Configuration,
780 OUT EFI_STRING *Progress
781 )
782 {
783 HII_DATABASE_PRIVATE_DATA *Private;
784 EFI_STRING StringPtr;
785 EFI_STRING ConfigResp;
786 UINTN Length;
787 EFI_STATUS Status;
788 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
789 LIST_ENTRY *Link;
790 HII_DATABASE_RECORD *Database;
791 UINT8 *DevicePathPkg;
792 UINT8 *CurrentDevicePath;
793 EFI_HANDLE DriverHandle;
794 EFI_HII_CONFIG_ACCESS_PROTOCOL *ConfigAccess;
795 EFI_STRING AccessProgress;
796
797 //
798 // For size reduction, please define PcdSupportFullConfigRoutingProtocol
799 // as FALSE. But this renders the system to not 100% compliant with
800 // UEFI 2.1. Use this with caution.
801 //
802 if (!FeaturePcdGet (PcdSupportFullConfigRoutingProtocol)) {
803 return EFI_UNSUPPORTED;
804 }
805
806 if (This == NULL || Progress == NULL) {
807 return EFI_INVALID_PARAMETER;
808 }
809
810 if (Configuration == NULL) {
811 *Progress = NULL;
812 return EFI_INVALID_PARAMETER;
813 }
814
815 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
816 StringPtr = Configuration;
817 *Progress = StringPtr;
818
819 //
820 // The first element of <MultiConfigResp> should be
821 // <GuidHdr>, which is in 'GUID='<Guid> syntax.
822 //
823 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
824 return EFI_INVALID_PARAMETER;
825 }
826
827 while (*StringPtr != 0 && StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) == 0) {
828 //
829 // If parsing error, set Progress to the beginning of the <MultiConfigResp>
830 // or most recent & before the error.
831 //
832 if (StringPtr == Configuration) {
833 *Progress = StringPtr;
834 } else {
835 *Progress = StringPtr - 1;
836 }
837
838 //
839 // Process each <ConfigResp> of <MultiConfigResp>
840 //
841 Length = CalculateConfigStringLen (StringPtr);
842 ConfigResp = AllocateCopyPool ((Length + 1) * sizeof (CHAR16), StringPtr);
843 if (ConfigResp == NULL) {
844 return EFI_OUT_OF_RESOURCES;
845 }
846 //
847 // Append '\0' to the end of ConfigRequest
848 //
849 *(ConfigResp + Length) = 0;
850
851 //
852 // Get the UEFI device path
853 //
854 Status = GetDevicePath (ConfigResp, (UINT8 **) &DevicePath);
855 if (EFI_ERROR (Status)) {
856 FreePool (ConfigResp);
857 return Status;
858 }
859
860 //
861 // Find driver which matches the routing data.
862 //
863 DriverHandle = NULL;
864 for (Link = Private->DatabaseList.ForwardLink;
865 Link != &Private->DatabaseList;
866 Link = Link->ForwardLink
867 ) {
868 Database = CR (Link, HII_DATABASE_RECORD, DatabaseEntry, HII_DATABASE_RECORD_SIGNATURE);
869
870 if ((DevicePathPkg = Database->PackageList->DevicePathPkg) != NULL) {
871 CurrentDevicePath = DevicePathPkg + sizeof (EFI_HII_PACKAGE_HEADER);
872 if (CompareMem (
873 DevicePath,
874 CurrentDevicePath,
875 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) CurrentDevicePath)
876 ) == 0) {
877 DriverHandle = Database->DriverHandle;
878 break;
879 }
880 }
881 }
882
883 FreePool (DevicePath);
884
885 if (DriverHandle == NULL) {
886 //
887 // Routing data does not match any known driver.
888 // Set Progress to the 'G' in "GUID" of the routing header.
889 //
890 *Progress = StringPtr;
891 FreePool (ConfigResp);
892 return EFI_NOT_FOUND;
893 }
894
895 //
896 // Call corresponding ConfigAccess protocol to route settings
897 //
898 Status = gBS->HandleProtocol (
899 DriverHandle,
900 &gEfiHiiConfigAccessProtocolGuid,
901 (VOID **) &ConfigAccess
902 );
903 ASSERT_EFI_ERROR (Status);
904
905 Status = ConfigAccess->RouteConfig (
906 ConfigAccess,
907 ConfigResp,
908 &AccessProgress
909 );
910
911 if (EFI_ERROR (Status)) {
912 //
913 // AccessProgress indicates the parsing progress on <ConfigResp>.
914 // Map it to the progress on <MultiConfigResp> then return it.
915 //
916 *Progress = StrStr (StringPtr, AccessProgress);
917
918 FreePool (ConfigResp);
919 return Status;
920 }
921
922 FreePool (ConfigResp);
923 ConfigResp = NULL;
924
925 //
926 // Go to next <ConfigResp> (skip '&').
927 //
928 StringPtr += Length;
929 if (*StringPtr == 0) {
930 *Progress = StringPtr;
931 break;
932 }
933
934 StringPtr++;
935
936 }
937
938 return EFI_SUCCESS;
939 }
940
941
942 /**
943 This helper function is to be called by drivers to map configuration data
944 stored in byte array ("block") formats such as UEFI Variables into current
945 configuration strings.
946
947 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
948 instance.
949 @param ConfigRequest A null-terminated Unicode string in
950 <ConfigRequest> format.
951 @param Block Array of bytes defining the block's configuration.
952 @param BlockSize Length in bytes of Block.
953 @param Config Filled-in configuration string. String allocated
954 by the function. Returned only if call is
955 successful.
956 @param Progress A pointer to a string filled in with the offset of
957 the most recent & before the first failing
958 name/value pair (or the beginning of the string if
959 the failure is in the first name / value pair) or
960 the terminating NULL if all was successful.
961
962 @retval EFI_SUCCESS The request succeeded. Progress points to the null
963 terminator at the end of the ConfigRequest
964 string.
965 @retval EFI_OUT_OF_RESOURCES Not enough memory to allocate Config. Progress
966 points to the first character of ConfigRequest.
967 @retval EFI_INVALID_PARAMETER Passing in a NULL for the ConfigRequest or
968 Block parameter would result in this type of
969 error. Progress points to the first character of
970 ConfigRequest.
971 @retval EFI_DEVICE_ERROR Block not large enough. Progress undefined.
972 @retval EFI_INVALID_PARAMETER Encountered non <BlockName> formatted string.
973 Block is left updated and Progress points at
974 the "&" preceding the first non-<BlockName>.
975
976 **/
977 EFI_STATUS
978 EFIAPI
979 HiiBlockToConfig (
980 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
981 IN CONST EFI_STRING ConfigRequest,
982 IN CONST UINT8 *Block,
983 IN CONST UINTN BlockSize,
984 OUT EFI_STRING *Config,
985 OUT EFI_STRING *Progress
986 )
987 {
988 HII_DATABASE_PRIVATE_DATA *Private;
989 EFI_STRING StringPtr;
990 UINTN Length;
991 EFI_STATUS Status;
992 EFI_STRING TmpPtr;
993 UINT8 *TmpBuffer;
994 UINTN Offset;
995 UINTN Width;
996 UINT8 *Value;
997 EFI_STRING ValueStr;
998 EFI_STRING ConfigElement;
999
1000 if (This == NULL || Progress == NULL || Config == NULL) {
1001 return EFI_INVALID_PARAMETER;
1002 }
1003
1004 if (Block == NULL || ConfigRequest == NULL) {
1005 *Progress = ConfigRequest;
1006 return EFI_INVALID_PARAMETER;
1007 }
1008
1009
1010 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
1011 ASSERT (Private != NULL);
1012
1013 StringPtr = ConfigRequest;
1014 ValueStr = NULL;
1015 Value = NULL;
1016 ConfigElement = NULL;
1017
1018 //
1019 // Allocate a fix length of memory to store Results. Reallocate memory for
1020 // Results if this fix length is insufficient.
1021 //
1022 *Config = (EFI_STRING) AllocateZeroPool (MAX_STRING_LENGTH);
1023 if (*Config == NULL) {
1024 return EFI_OUT_OF_RESOURCES;
1025 }
1026
1027 //
1028 // Jump <ConfigHdr>
1029 //
1030 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
1031 *Progress = StringPtr;
1032 Status = EFI_INVALID_PARAMETER;
1033 goto Exit;
1034 }
1035 while (*StringPtr != 0 && StrnCmp (StringPtr, L"PATH=", StrLen (L"PATH=")) != 0) {
1036 StringPtr++;
1037 }
1038 if (*StringPtr == 0) {
1039 *Progress = StringPtr;
1040 Status = EFI_INVALID_PARAMETER;
1041 goto Exit;
1042 }
1043
1044 while (*StringPtr != L'&' && *StringPtr != 0) {
1045 StringPtr++;
1046 }
1047 if (*StringPtr == 0) {
1048 *Progress = StringPtr;
1049 Status = EFI_INVALID_PARAMETER;
1050 goto Exit;
1051 }
1052 //
1053 // Skip '&'
1054 //
1055 StringPtr++;
1056
1057 //
1058 // Copy <ConfigHdr> and an additional '&' to <ConfigResp>
1059 //
1060 Length = StringPtr - ConfigRequest;
1061 CopyMem (*Config, ConfigRequest, Length * sizeof (CHAR16));
1062
1063 //
1064 // Parse each <RequestElement> if exists
1065 // Only <BlockName> format is supported by this help function.
1066 // <BlockName> ::= 'OFFSET='<Number>&'WIDTH='<Number>
1067 //
1068 while (*StringPtr != 0 && StrnCmp (StringPtr, L"OFFSET=", StrLen (L"OFFSET=")) == 0) {
1069 //
1070 // Back up the header of one <BlockName>
1071 //
1072 TmpPtr = StringPtr;
1073
1074 StringPtr += StrLen (L"OFFSET=");
1075 //
1076 // Get Offset
1077 //
1078 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
1079 if (Status == EFI_OUT_OF_RESOURCES) {
1080 *Progress = ConfigRequest;
1081 goto Exit;
1082 }
1083 Offset = 0;
1084 CopyMem (
1085 &Offset,
1086 TmpBuffer,
1087 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
1088 );
1089 FreePool (TmpBuffer);
1090
1091 StringPtr += Length;
1092 if (StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
1093 *Progress = StringPtr - Length - StrLen (L"OFFSET=") - 1;
1094 Status = EFI_INVALID_PARAMETER;
1095 goto Exit;
1096 }
1097 StringPtr += StrLen (L"&WIDTH=");
1098
1099 //
1100 // Get Width
1101 //
1102 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
1103 if (Status == EFI_OUT_OF_RESOURCES) {
1104 *Progress = ConfigRequest;
1105 goto Exit;
1106 }
1107 Width = 0;
1108 CopyMem (
1109 &Width,
1110 TmpBuffer,
1111 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
1112 );
1113 FreePool (TmpBuffer);
1114
1115 StringPtr += Length;
1116 if (*StringPtr != 0 && *StringPtr != L'&') {
1117 *Progress = StringPtr - Length - StrLen (L"&WIDTH=");
1118 Status = EFI_INVALID_PARAMETER;
1119 goto Exit;
1120 }
1121
1122 //
1123 // Calculate Value and convert it to hex string.
1124 //
1125 if (Offset + Width > BlockSize) {
1126 *Progress = StringPtr;
1127 Status = EFI_DEVICE_ERROR;
1128 goto Exit;
1129 }
1130
1131 Value = (UINT8 *) AllocateZeroPool (Width);
1132 if (Value == NULL) {
1133 *Progress = ConfigRequest;
1134 Status = EFI_OUT_OF_RESOURCES;
1135 goto Exit;
1136 }
1137
1138 CopyMem (Value, (UINT8 *) Block + Offset, Width);
1139
1140 Length = Width * 2 + 1;
1141 ValueStr = (EFI_STRING) AllocateZeroPool (Length * sizeof (CHAR16));
1142 if (ValueStr == NULL) {
1143 *Progress = ConfigRequest;
1144 Status = EFI_OUT_OF_RESOURCES;
1145 goto Exit;
1146 }
1147
1148 Status = BufToHexString (ValueStr, &Length, Value, Width);
1149 ASSERT_EFI_ERROR (Status);
1150 ToLower (ValueStr);
1151
1152 FreePool (Value);
1153 Value = NULL;
1154
1155 //
1156 // Build a ConfigElement
1157 //
1158 Length += StringPtr - TmpPtr + 1 + StrLen (L"VALUE=");
1159 ConfigElement = (EFI_STRING) AllocateZeroPool (Length * sizeof (CHAR16));
1160 if (ConfigElement == NULL) {
1161 Status = EFI_OUT_OF_RESOURCES;
1162 goto Exit;
1163 }
1164 CopyMem (ConfigElement, TmpPtr, (StringPtr - TmpPtr + 1) * sizeof (CHAR16));
1165 if (*StringPtr == 0) {
1166 *(ConfigElement + (StringPtr - TmpPtr)) = L'&';
1167 }
1168 *(ConfigElement + (StringPtr - TmpPtr) + 1) = 0;
1169 StrCat (ConfigElement, L"VALUE=");
1170 StrCat (ConfigElement, ValueStr);
1171
1172 AppendToMultiString (Config, ConfigElement);
1173
1174 FreePool (ConfigElement);
1175 FreePool (ValueStr);
1176 ConfigElement = NULL;
1177 ValueStr = NULL;
1178
1179 //
1180 // If '\0', parsing is finished. Otherwise skip '&' to continue
1181 //
1182 if (*StringPtr == 0) {
1183 break;
1184 }
1185 AppendToMultiString (Config, L"&");
1186 StringPtr++;
1187
1188 }
1189
1190 if (*StringPtr != 0) {
1191 *Progress = StringPtr - 1;
1192 Status = EFI_INVALID_PARAMETER;
1193 goto Exit;
1194 }
1195
1196 *Progress = StringPtr;
1197 return EFI_SUCCESS;
1198
1199 Exit:
1200 FreePool (*Config);
1201 if (ValueStr != NULL) {
1202 FreePool (ValueStr);
1203 }
1204 if (Value != NULL) {
1205 FreePool (Value);
1206 }
1207 if (ConfigElement != NULL) {
1208 FreePool (ConfigElement);
1209 }
1210
1211 return Status;
1212
1213 }
1214
1215
1216 /**
1217 This helper function is to be called by drivers to map configuration strings
1218 to configurations stored in byte array ("block") formats such as UEFI Variables.
1219
1220 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
1221 instance.
1222 @param ConfigResp A null-terminated Unicode string in <ConfigResp>
1223 format.
1224 @param Block A possibly null array of bytes representing the
1225 current block. Only bytes referenced in the
1226 ConfigResp string in the block are modified. If
1227 this parameter is null or if the *BlockSize
1228 parameter is (on input) shorter than required by
1229 the Configuration string, only the BlockSize
1230 parameter is updated and an appropriate status
1231 (see below) is returned.
1232 @param BlockSize The length of the Block in units of UINT8. On
1233 input, this is the size of the Block. On output,
1234 if successful, contains the index of the last
1235 modified byte in the Block.
1236 @param Progress On return, points to an element of the ConfigResp
1237 string filled in with the offset of the most
1238 recent '&' before the first failing name / value
1239 pair (or the beginning of the string if the
1240 failure is in the first name / value pair) or the
1241 terminating NULL if all was successful.
1242
1243 @retval EFI_SUCCESS The request succeeded. Progress points to the null
1244 terminator at the end of the ConfigResp string.
1245 @retval EFI_OUT_OF_RESOURCES Not enough memory to allocate Config. Progress
1246 points to the first character of ConfigResp.
1247 @retval EFI_INVALID_PARAMETER Passing in a NULL for the ConfigResp or
1248 Block parameter would result in this type of
1249 error. Progress points to the first character of
1250 ConfigResp.
1251 @retval EFI_INVALID_PARAMETER Encountered non <BlockName> formatted name /
1252 value pair. Block is left updated and
1253 Progress points at the '&' preceding the first
1254 non-<BlockName>.
1255
1256 **/
1257 EFI_STATUS
1258 EFIAPI
1259 HiiConfigToBlock (
1260 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
1261 IN CONST EFI_STRING ConfigResp,
1262 IN OUT UINT8 *Block,
1263 IN OUT UINTN *BlockSize,
1264 OUT EFI_STRING *Progress
1265 )
1266 {
1267 HII_DATABASE_PRIVATE_DATA *Private;
1268 EFI_STRING StringPtr;
1269 UINTN Length;
1270 EFI_STATUS Status;
1271 UINT8 *TmpBuffer;
1272 UINTN Offset;
1273 UINTN Width;
1274 UINT8 *Value;
1275 UINTN BufferSize;
1276
1277 if (This == NULL || BlockSize == NULL || Progress == NULL) {
1278 return EFI_INVALID_PARAMETER;
1279 }
1280
1281 if (ConfigResp == NULL || Block == NULL) {
1282 *Progress = ConfigResp;
1283 return EFI_INVALID_PARAMETER;
1284 }
1285
1286 Private = CONFIG_ROUTING_DATABASE_PRIVATE_DATA_FROM_THIS (This);
1287 ASSERT (Private != NULL);
1288
1289 StringPtr = ConfigResp;
1290 BufferSize = *BlockSize;
1291 Value = NULL;
1292
1293 //
1294 // Jump <ConfigHdr>
1295 //
1296 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
1297 *Progress = StringPtr;
1298 Status = EFI_INVALID_PARAMETER;
1299 goto Exit;
1300 }
1301 while (*StringPtr != 0 && StrnCmp (StringPtr, L"PATH=", StrLen (L"PATH=")) != 0) {
1302 StringPtr++;
1303 }
1304 if (*StringPtr == 0) {
1305 *Progress = StringPtr;
1306 Status = EFI_INVALID_PARAMETER;
1307 goto Exit;
1308 }
1309
1310 while (*StringPtr != L'&' && *StringPtr != 0) {
1311 StringPtr++;
1312 }
1313 if (*StringPtr == 0) {
1314 *Progress = StringPtr;
1315 Status = EFI_INVALID_PARAMETER;
1316 goto Exit;
1317 }
1318 //
1319 // Skip '&'
1320 //
1321 StringPtr++;
1322
1323 //
1324 // Parse each <ConfigElement> if exists
1325 // Only <BlockConfig> format is supported by this help function.
1326 // <BlockConfig> ::= 'OFFSET='<Number>&'WIDTH='<Number>&'VALUE='<Number>
1327 //
1328 while (*StringPtr != 0 && StrnCmp (StringPtr, L"OFFSET=", StrLen (L"OFFSET=")) == 0) {
1329 StringPtr += StrLen (L"OFFSET=");
1330 //
1331 // Get Offset
1332 //
1333 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
1334 if (Status == EFI_OUT_OF_RESOURCES) {
1335 *Progress = ConfigResp;
1336 goto Exit;
1337 }
1338 Offset = 0;
1339 CopyMem (
1340 &Offset,
1341 TmpBuffer,
1342 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
1343 );
1344 FreePool (TmpBuffer);
1345
1346 StringPtr += Length;
1347 if (StrnCmp (StringPtr, L"&WIDTH=", StrLen (L"&WIDTH=")) != 0) {
1348 *Progress = StringPtr - Length - StrLen (L"OFFSET=") - 1;
1349 Status = EFI_INVALID_PARAMETER;
1350 goto Exit;
1351 }
1352 StringPtr += StrLen (L"&WIDTH=");
1353
1354 //
1355 // Get Width
1356 //
1357 Status = GetValueOfNumber (StringPtr, &TmpBuffer, &Length);
1358 if (Status == EFI_OUT_OF_RESOURCES) {
1359 *Progress = ConfigResp;
1360 goto Exit;
1361 }
1362 Width = 0;
1363 CopyMem (
1364 &Width,
1365 TmpBuffer,
1366 (((Length + 1) / 2) < sizeof (UINTN)) ? ((Length + 1) / 2) : sizeof (UINTN)
1367 );
1368 FreePool (TmpBuffer);
1369
1370 StringPtr += Length;
1371 if (StrnCmp (StringPtr, L"&VALUE=", StrLen (L"&VALUE=")) != 0) {
1372 *Progress = StringPtr - Length - StrLen (L"&WIDTH=");
1373 Status = EFI_INVALID_PARAMETER;
1374 goto Exit;
1375 }
1376 StringPtr += StrLen (L"&VALUE=");
1377
1378 //
1379 // Get Value
1380 //
1381 Status = GetValueOfNumber (StringPtr, &Value, &Length);
1382 if (Status == EFI_OUT_OF_RESOURCES) {
1383 *Progress = ConfigResp;
1384 goto Exit;
1385 }
1386
1387 StringPtr += Length;
1388 if (*StringPtr != 0 && *StringPtr != L'&') {
1389 *Progress = StringPtr - Length - 7;
1390 Status = EFI_INVALID_PARAMETER;
1391 goto Exit;
1392 }
1393
1394 //
1395 // Update the Block with configuration info
1396 //
1397
1398 if (Offset + Width > BufferSize) {
1399 return EFI_DEVICE_ERROR;
1400 }
1401
1402 CopyMem (Block + Offset, Value, Width);
1403 *BlockSize = Offset + Width - 1;
1404
1405 FreePool (Value);
1406 Value = NULL;
1407
1408 //
1409 // If '\0', parsing is finished. Otherwise skip '&' to continue
1410 //
1411 if (*StringPtr == 0) {
1412 break;
1413 }
1414
1415 StringPtr++;
1416 }
1417
1418 if (*StringPtr != 0) {
1419 *Progress = StringPtr - 1;
1420 Status = EFI_INVALID_PARAMETER;
1421 goto Exit;
1422 }
1423
1424 *Progress = StringPtr;
1425 return EFI_SUCCESS;
1426
1427 Exit:
1428
1429 if (Value != NULL) {
1430 FreePool (Value);
1431 }
1432 return Status;
1433 }
1434
1435
1436 /**
1437 This helper function is to be called by drivers to extract portions of
1438 a larger configuration string.
1439
1440 @param This A pointer to the EFI_HII_CONFIG_ROUTING_PROTOCOL
1441 instance.
1442 @param Configuration A null-terminated Unicode string in
1443 <MultiConfigAltResp> format.
1444 @param Guid A pointer to the GUID value to search for in the
1445 routing portion of the ConfigResp string when
1446 retrieving the requested data. If Guid is NULL,
1447 then all GUID values will be searched for.
1448 @param Name A pointer to the NAME value to search for in the
1449 routing portion of the ConfigResp string when
1450 retrieving the requested data. If Name is NULL,
1451 then all Name values will be searched for.
1452 @param DevicePath A pointer to the PATH value to search for in the
1453 routing portion of the ConfigResp string when
1454 retrieving the requested data. If DevicePath is
1455 NULL, then all DevicePath values will be searched
1456 for.
1457 @param AltCfgId A pointer to the ALTCFG value to search for in the
1458 routing portion of the ConfigResp string when
1459 retrieving the requested data. If this parameter
1460 is NULL, then the current setting will be
1461 retrieved.
1462 @param AltCfgResp A pointer to a buffer which will be allocated by
1463 the function which contains the retrieved string
1464 as requested. This buffer is only allocated if
1465 the call was successful.
1466
1467 @retval EFI_SUCCESS The request succeeded. The requested data was
1468 extracted and placed in the newly allocated
1469 AltCfgResp buffer.
1470 @retval EFI_OUT_OF_RESOURCES Not enough memory to allocate AltCfgResp.
1471 @retval EFI_INVALID_PARAMETER Any parameter is invalid.
1472 @retval EFI_NOT_FOUND Target for the specified routing data was not
1473 found.
1474
1475 **/
1476 EFI_STATUS
1477 EFIAPI
1478 HiiGetAltCfg (
1479 IN CONST EFI_HII_CONFIG_ROUTING_PROTOCOL *This,
1480 IN CONST EFI_STRING Configuration,
1481 IN CONST EFI_GUID *Guid,
1482 IN CONST EFI_STRING Name,
1483 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
1484 IN CONST UINT16 *AltCfgId,
1485 OUT EFI_STRING *AltCfgResp
1486 )
1487 {
1488 EFI_STATUS Status;
1489 EFI_STRING StringPtr;
1490 EFI_STRING HdrStart;
1491 EFI_STRING HdrEnd;
1492 EFI_STRING TmpPtr;
1493 UINTN Length;
1494 EFI_STRING GuidStr;
1495 EFI_STRING NameStr;
1496 EFI_STRING PathStr;
1497 EFI_STRING AltIdStr;
1498 EFI_STRING Result;
1499 BOOLEAN GuidFlag;
1500 BOOLEAN NameFlag;
1501 BOOLEAN PathFlag;
1502
1503 //
1504 // For size reduction, please define PcdSupportFullConfigRoutingProtocol
1505 // as FALSE. But this renders the system to not 100% compliant with
1506 // UEFI 2.1. Use this with caution.
1507 //
1508 if (!FeaturePcdGet (PcdSupportFullConfigRoutingProtocol)) {
1509 return EFI_UNSUPPORTED;
1510 }
1511
1512 HdrStart = NULL;
1513 HdrEnd = NULL;
1514 GuidStr = NULL;
1515 NameStr = NULL;
1516 PathStr = NULL;
1517 AltIdStr = NULL;
1518 Result = NULL;
1519 GuidFlag = FALSE;
1520 NameFlag = FALSE;
1521 PathFlag = FALSE;
1522
1523 if (This == NULL || Configuration == NULL || AltCfgResp == NULL) {
1524 return EFI_INVALID_PARAMETER;
1525 }
1526
1527 StringPtr = Configuration;
1528 if (StrnCmp (StringPtr, L"GUID=", StrLen (L"GUID=")) != 0) {
1529 return EFI_INVALID_PARAMETER;
1530 }
1531
1532 //
1533 // Generate the sub string for later matching.
1534 //
1535 GenerateSubStr (L"GUID=", sizeof (EFI_GUID), (VOID *) Guid, 1, &GuidStr);
1536 GenerateSubStr (
1537 L"PATH=",
1538 GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *) DevicePath),
1539 (VOID *) DevicePath,
1540 1,
1541 &PathStr
1542 );
1543 if (AltCfgId != NULL) {
1544 GenerateSubStr (L"ALTCFG=", sizeof (UINT16), (VOID *) AltCfgId, 3, &AltIdStr);
1545 }
1546 if (Name != NULL) {
1547 GenerateSubStr (L"NAME=", StrLen (Name) * sizeof (CHAR16), (VOID *) Name, 2, &NameStr);
1548 } else {
1549 GenerateSubStr (L"NAME=", 0, NULL, 2, &NameStr);
1550 }
1551
1552 while (*StringPtr != 0) {
1553 //
1554 // Try to match the GUID
1555 //
1556 if (!GuidFlag) {
1557 TmpPtr = StrStr (StringPtr, GuidStr);
1558 if (TmpPtr == NULL) {
1559 Status = EFI_NOT_FOUND;
1560 goto Exit;
1561 }
1562 HdrStart = TmpPtr;
1563
1564 //
1565 // Jump to <NameHdr>
1566 //
1567 if (Guid != NULL) {
1568 StringPtr = TmpPtr + StrLen (GuidStr);
1569 } else {
1570 StringPtr = StrStr (TmpPtr, L"NAME=");
1571 if (StringPtr == NULL) {
1572 Status = EFI_NOT_FOUND;
1573 goto Exit;
1574 }
1575 }
1576 GuidFlag = TRUE;
1577 }
1578
1579 //
1580 // Try to match the NAME
1581 //
1582 if (GuidFlag && !NameFlag) {
1583 if (StrnCmp (StringPtr, NameStr, StrLen (NameStr)) != 0) {
1584 GuidFlag = FALSE;
1585 } else {
1586 //
1587 // Jump to <PathHdr>
1588 //
1589 if (Name != NULL) {
1590 StringPtr += StrLen (NameStr);
1591 } else {
1592 StringPtr = StrStr (StringPtr, L"PATH=");
1593 if (StringPtr == NULL) {
1594 Status = EFI_NOT_FOUND;
1595 goto Exit;
1596 }
1597 }
1598 NameFlag = TRUE;
1599 }
1600 }
1601
1602 //
1603 // Try to match the DevicePath
1604 //
1605 if (GuidFlag && NameFlag && !PathFlag) {
1606 if (StrnCmp (StringPtr, PathStr, StrLen (PathStr)) != 0) {
1607 GuidFlag = FALSE;
1608 NameFlag = FALSE;
1609 } else {
1610 //
1611 // Jump to '&' before <DescHdr> or <ConfigBody>
1612 //
1613 if (DevicePath != NULL) {
1614 StringPtr += StrLen (PathStr);
1615 } else {
1616 StringPtr = StrStr (StringPtr, L"&");
1617 if (StringPtr == NULL) {
1618 Status = EFI_NOT_FOUND;
1619 goto Exit;
1620 }
1621 }
1622 PathFlag = TRUE;
1623 HdrEnd = ++StringPtr;
1624 }
1625 }
1626
1627 //
1628 // Try to match the AltCfgId
1629 //
1630 if (GuidFlag && NameFlag && PathFlag) {
1631 if (AltCfgId == NULL) {
1632 //
1633 // Return Current Setting when AltCfgId is NULL.
1634 //
1635 Status = OutputConfigBody (StringPtr, &Result);
1636 goto Exit;
1637 }
1638 //
1639 // Search the <ConfigAltResp> to get the <AltResp> with AltCfgId.
1640 //
1641 if (StrnCmp (StringPtr, AltIdStr, StrLen (AltIdStr)) != 0) {
1642 GuidFlag = FALSE;
1643 NameFlag = FALSE;
1644 PathFlag = FALSE;
1645 } else {
1646 Status = OutputConfigBody (StringPtr, &Result);
1647 goto Exit;
1648 }
1649 }
1650 }
1651
1652 Status = EFI_NOT_FOUND;
1653
1654 Exit:
1655
1656 if (!EFI_ERROR (Status)) {
1657 //
1658 // Copy the <ConfigHdr> and <ConfigBody>
1659 //
1660 Length = HdrEnd - HdrStart + StrLen (Result);
1661 *AltCfgResp = AllocateZeroPool (Length * sizeof (CHAR16));
1662 if (*AltCfgResp == NULL) {
1663 Status = EFI_OUT_OF_RESOURCES;
1664 } else {
1665 StrnCpy (*AltCfgResp, HdrStart, HdrEnd - HdrStart);
1666 StrCat (*AltCfgResp, Result);
1667 Status = EFI_SUCCESS;
1668 }
1669 }
1670
1671 if (GuidStr != NULL) {
1672 FreePool (GuidStr);
1673 }
1674 if (NameStr != NULL) {
1675 FreePool (NameStr);
1676 }
1677 if (PathStr != NULL) {
1678 FreePool (PathStr);
1679 }
1680 if (AltIdStr != NULL) {
1681 FreePool (AltIdStr);
1682 }
1683 if (Result != NULL) {
1684 FreePool (Result);
1685 }
1686
1687 return Status;
1688
1689 }
1690
1691