]> git.proxmox.com Git - mirror_edk2.git/blob - SignedCapsulePkg/Library/IniParsingLib/IniParsingLib.c
SignedCapsulePkg/IniParsingLib: Use AsciiStrToGuid in BaseLib
[mirror_edk2.git] / SignedCapsulePkg / Library / IniParsingLib / IniParsingLib.c
1 /** @file
2 This library parses the INI configuration file.
3
4 The INI file format is:
5 ================
6 [SectionName]
7 EntryName=EntryValue
8 ================
9
10 Where:
11 1) SectionName is an ASCII string. The valid format is [A-Za-z0-9_]+
12 2) EntryName is an ASCII string. The valid format is [A-Za-z0-9_]+
13 3) EntryValue can be:
14 3.1) an ASCII String. The valid format is [A-Za-z0-9_]+
15 3.2) a GUID. The valid format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, where x is [A-Fa-f0-9]
16 3.3) a decimal value. The valid format is [0-9]+
17 3.4) a heximal value. The valid format is 0x[A-Fa-f0-9]+
18 4) '#' or ';' can be used as comment at anywhere.
19 5) TAB(0x20) or SPACE(0x9) can be used as separator.
20 6) LF(\n, 0xA) or CR(\r, 0xD) can be used as line break.
21
22 Caution: This module requires additional review when modified.
23 This driver will have external input - INI data file.
24
25 OpenIniFile(), PreProcessDataFile(), ProfileGetSection(), ProfileGetEntry()
26 will receive untrusted input and do basic validation.
27
28 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
29
30 This program and the accompanying materials
31 are licensed and made available under the terms and conditions
32 of the BSD License which accompanies this distribution. The
33 full text of the license may be found at
34 http://opensource.org/licenses/bsd-license.php
35
36 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
37 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
38
39 **/
40
41 #include <Uefi.h>
42 #include <Library/BaseLib.h>
43 #include <Library/BaseMemoryLib.h>
44 #include <Library/DebugLib.h>
45 #include <Library/MemoryAllocationLib.h>
46
47 #define IS_HYPHEN(a) ((a) == '-')
48 #define IS_NULL(a) ((a) == '\0')
49
50 // This is default allocation. Reallocation will happen if it is not enough.
51 #define MAX_LINE_LENGTH 512
52
53 typedef struct _INI_SECTION_ITEM SECTION_ITEM;
54 struct _INI_SECTION_ITEM {
55 CHAR8 *PtrSection;
56 UINTN SecNameLen;
57 CHAR8 *PtrEntry;
58 CHAR8 *PtrValue;
59 SECTION_ITEM *PtrNext;
60 };
61
62 typedef struct _INI_COMMENT_LINE COMMENT_LINE;
63 struct _INI_COMMENT_LINE {
64 CHAR8 *PtrComment;
65 COMMENT_LINE *PtrNext;
66 };
67
68 typedef struct {
69 SECTION_ITEM *SectionHead;
70 COMMENT_LINE *CommentHead;
71 } INI_PARSING_LIB_CONTEXT;
72
73 /**
74 Return if the digital char is valid.
75
76 @param[in] DigitalChar The digital char to be checked.
77 @param[in] IncludeHex If it include HEX char.
78
79 @retval TRUE The digital char is valid.
80 @retval FALSE The digital char is invalid.
81 **/
82 BOOLEAN
83 IsValidDigitalChar (
84 IN CHAR8 DigitalChar,
85 IN BOOLEAN IncludeHex
86 )
87 {
88 if (DigitalChar >= '0' && DigitalChar <= '9') {
89 return TRUE;
90 }
91 if (IncludeHex) {
92 if (DigitalChar >= 'a' && DigitalChar <= 'f') {
93 return TRUE;
94 }
95 if (DigitalChar >= 'A' && DigitalChar <= 'F') {
96 return TRUE;
97 }
98 }
99 return FALSE;
100 }
101
102 /**
103 Return if the name char is valid.
104
105 @param[in] NameChar The name char to be checked.
106
107 @retval TRUE The name char is valid.
108 @retval FALSE The name char is invalid.
109 **/
110 BOOLEAN
111 IsValidNameChar (
112 IN CHAR8 NameChar
113 )
114 {
115 if (NameChar >= 'a' && NameChar <= 'z') {
116 return TRUE;
117 }
118 if (NameChar >= 'A' && NameChar <= 'Z') {
119 return TRUE;
120 }
121 if (NameChar >= '0' && NameChar <= '9') {
122 return TRUE;
123 }
124 if (NameChar == '_') {
125 return TRUE;
126 }
127 return FALSE;
128 }
129
130 /**
131 Return if the digital string is valid.
132
133 @param[in] Digital The digital to be checked.
134 @param[in] Length The length of digital string in bytes.
135 @param[in] IncludeHex If it include HEX char.
136
137 @retval TRUE The digital string is valid.
138 @retval FALSE The digital string is invalid.
139 **/
140 BOOLEAN
141 IsValidDigital (
142 IN CHAR8 *Digital,
143 IN UINTN Length,
144 IN BOOLEAN IncludeHex
145 )
146 {
147 UINTN Index;
148 for (Index = 0; Index < Length; Index++) {
149 if (!IsValidDigitalChar(Digital[Index], IncludeHex)) {
150 return FALSE;
151 }
152 }
153 return TRUE;
154 }
155
156 /**
157 Return if the decimal string is valid.
158
159 @param[in] Decimal The decimal string to be checked.
160 @param[in] Length The length of decimal string in bytes.
161
162 @retval TRUE The decimal string is valid.
163 @retval FALSE The decimal string is invalid.
164 **/
165 BOOLEAN
166 IsValidDecimalString (
167 IN CHAR8 *Decimal,
168 IN UINTN Length
169 )
170 {
171 return IsValidDigital(Decimal, Length, FALSE);
172 }
173
174 /**
175 Return if the heximal string is valid.
176
177 @param[in] Hex The heximal string to be checked.
178 @param[in] Length The length of heximal string in bytes.
179
180 @retval TRUE The heximal string is valid.
181 @retval FALSE The heximal string is invalid.
182 **/
183 BOOLEAN
184 IsValidHexString (
185 IN CHAR8 *Hex,
186 IN UINTN Length
187 )
188 {
189 if (Length <= 2) {
190 return FALSE;
191 }
192 if (Hex[0] != '0') {
193 return FALSE;
194 }
195 if (Hex[1] != 'x' && Hex[1] != 'X') {
196 return FALSE;
197 }
198 return IsValidDigital(&Hex[2], Length - 2, TRUE);
199 }
200
201 /**
202 Return if the name string is valid.
203
204 @param[in] Name The name to be checked.
205 @param[in] Length The length of name string in bytes.
206
207 @retval TRUE The name string is valid.
208 @retval FALSE The name string is invalid.
209 **/
210 BOOLEAN
211 IsValidName (
212 IN CHAR8 *Name,
213 IN UINTN Length
214 )
215 {
216 UINTN Index;
217 for (Index = 0; Index < Length; Index++) {
218 if (!IsValidNameChar(Name[Index])) {
219 return FALSE;
220 }
221 }
222 return TRUE;
223 }
224
225 /**
226 Return if the value string is valid GUID.
227
228 @param[in] Value The value to be checked.
229 @param[in] Length The length of value string in bytes.
230
231 @retval TRUE The value string is valid GUID.
232 @retval FALSE The value string is invalid GUID.
233 **/
234 BOOLEAN
235 IsValidGuid (
236 IN CHAR8 *Value,
237 IN UINTN Length
238 )
239 {
240 if (Length != sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") - 1) {
241 return FALSE;
242 }
243 if (!IS_HYPHEN(Value[8])) {
244 return FALSE;
245 }
246 if (!IS_HYPHEN(Value[13])) {
247 return FALSE;
248 }
249 if (!IS_HYPHEN(Value[18])) {
250 return FALSE;
251 }
252 if (!IS_HYPHEN(Value[23])) {
253 return FALSE;
254 }
255 if (!IsValidDigital(&Value[0], 8, TRUE)) {
256 return FALSE;
257 }
258 if (!IsValidDigital(&Value[9], 4, TRUE)) {
259 return FALSE;
260 }
261 if (!IsValidDigital(&Value[14], 4, TRUE)) {
262 return FALSE;
263 }
264 if (!IsValidDigital(&Value[19], 4, TRUE)) {
265 return FALSE;
266 }
267 if (!IsValidDigital(&Value[24], 12, TRUE)) {
268 return FALSE;
269 }
270 return TRUE;
271 }
272
273 /**
274 Return if the value string is valid.
275
276 @param[in] Value The value to be checked.
277 @param[in] Length The length of value string in bytes.
278
279 @retval TRUE The name string is valid.
280 @retval FALSE The name string is invalid.
281 **/
282 BOOLEAN
283 IsValidValue (
284 IN CHAR8 *Value,
285 IN UINTN Length
286 )
287 {
288 if (IsValidName(Value, Length) || IsValidGuid(Value, Length)) {
289 return TRUE;
290 }
291 return FALSE;
292 }
293
294 /**
295 Dump an INI config file context.
296
297 @param[in] Context INI Config file context.
298 **/
299 VOID
300 DumpIniSection (
301 IN VOID *Context
302 )
303 {
304 INI_PARSING_LIB_CONTEXT *IniContext;
305 SECTION_ITEM *PtrSection;
306 SECTION_ITEM *Section;
307
308 if (Context == NULL) {
309 return;
310 }
311
312 IniContext = Context;
313 Section = IniContext->SectionHead;
314
315 while (Section != NULL) {
316 PtrSection = Section;
317 Section = Section->PtrNext;
318 if (PtrSection->PtrSection != NULL) {
319 DEBUG((DEBUG_VERBOSE, "Section - %a\n", PtrSection->PtrSection));
320 }
321 if (PtrSection->PtrEntry != NULL) {
322 DEBUG ((DEBUG_VERBOSE, " Entry - %a\n", PtrSection->PtrEntry));
323 }
324 if (PtrSection->PtrValue != NULL) {
325 DEBUG((DEBUG_VERBOSE, " Value - %a\n", PtrSection->PtrValue));
326 }
327 }
328 }
329
330 /**
331 Copy one line data from buffer data to the line buffer.
332
333 @param[in] Buffer Buffer data.
334 @param[in] BufferSize Buffer Size.
335 @param[in, out] LineBuffer Line buffer to store the found line data.
336 @param[in, out] LineSize On input, size of the input line buffer.
337 On output, size of the actual line buffer.
338
339 @retval EFI_BUFFER_TOO_SMALL The size of input line buffer is not enough.
340 @retval EFI_SUCCESS Copy line data into the line buffer.
341
342 **/
343 EFI_STATUS
344 ProfileGetLine (
345 IN UINT8 *Buffer,
346 IN UINTN BufferSize,
347 IN OUT UINT8 *LineBuffer,
348 IN OUT UINTN *LineSize
349 )
350 {
351 UINTN Length;
352 UINT8 *PtrBuf;
353 UINTN PtrEnd;
354
355 PtrBuf = Buffer;
356 PtrEnd = (UINTN)Buffer + BufferSize;
357
358 //
359 // 0x0D indicates a line break. Otherwise there is no line break
360 //
361 while ((UINTN)PtrBuf < PtrEnd) {
362 if (*PtrBuf == 0x0D || *PtrBuf == 0x0A) {
363 break;
364 }
365 PtrBuf++;
366 }
367
368 if ((UINTN)PtrBuf >= (PtrEnd - 1)) {
369 //
370 // The buffer ends without any line break
371 // or it is the last character of the buffer
372 //
373 Length = BufferSize;
374 } else if (*(PtrBuf + 1) == 0x0A) {
375 //
376 // Further check if a 0x0A follows. If yes, count 0xA
377 //
378 Length = (UINTN) PtrBuf - (UINTN) Buffer + 2;
379 } else {
380 Length = (UINTN) PtrBuf - (UINTN) Buffer + 1;
381 }
382
383 if (Length > (*LineSize)) {
384 *LineSize = Length;
385 return EFI_BUFFER_TOO_SMALL;
386 }
387
388 SetMem (LineBuffer, *LineSize, 0x0);
389 *LineSize = Length;
390 CopyMem (LineBuffer, Buffer, Length);
391
392 return EFI_SUCCESS;
393 }
394
395 /**
396 Trim Buffer by removing all CR, LF, TAB, and SPACE chars in its head and tail.
397
398 @param[in, out] Buffer On input, buffer data to be trimed.
399 On output, the trimmed buffer.
400 @param[in, out] BufferSize On input, size of original buffer data.
401 On output, size of the trimmed buffer.
402
403 **/
404 VOID
405 ProfileTrim (
406 IN OUT UINT8 *Buffer,
407 IN OUT UINTN *BufferSize
408 )
409 {
410 UINTN Length;
411 UINT8 *PtrBuf;
412 UINT8 *PtrEnd;
413
414 if (*BufferSize == 0) {
415 return;
416 }
417
418 //
419 // Trim the tail first, include CR, LF, TAB, and SPACE.
420 //
421 Length = *BufferSize;
422 PtrBuf = (UINT8 *) ((UINTN) Buffer + Length - 1);
423 while (PtrBuf >= Buffer) {
424 if ((*PtrBuf != 0x0D) && (*PtrBuf != 0x0A )
425 && (*PtrBuf != 0x20) && (*PtrBuf != 0x09)) {
426 break;
427 }
428 PtrBuf --;
429 }
430
431 //
432 // all spaces, a blank line, return directly;
433 //
434 if (PtrBuf < Buffer) {
435 *BufferSize = 0;
436 return;
437 }
438
439 Length = (UINTN)PtrBuf - (UINTN)Buffer + 1;
440 PtrEnd = PtrBuf;
441 PtrBuf = Buffer;
442
443 //
444 // Now skip the heading CR, LF, TAB and SPACE
445 //
446 while (PtrBuf <= PtrEnd) {
447 if ((*PtrBuf != 0x0D) && (*PtrBuf != 0x0A )
448 && (*PtrBuf != 0x20) && (*PtrBuf != 0x09)) {
449 break;
450 }
451 PtrBuf++;
452 }
453
454 //
455 // If no heading CR, LF, TAB or SPACE, directly return
456 //
457 if (PtrBuf == Buffer) {
458 *BufferSize = Length;
459 return;
460 }
461
462 *BufferSize = (UINTN)PtrEnd - (UINTN)PtrBuf + 1;
463
464 //
465 // The first Buffer..PtrBuf characters are CR, LF, TAB or SPACE.
466 // Now move out all these characters.
467 //
468 while (PtrBuf <= PtrEnd) {
469 *Buffer = *PtrBuf;
470 Buffer++;
471 PtrBuf++;
472 }
473
474 return;
475 }
476
477 /**
478 Insert new comment item into comment head.
479
480 @param[in] Buffer Comment buffer to be added.
481 @param[in] BufferSize Size of comment buffer.
482 @param[in, out] CommentHead Comment Item head entry.
483
484 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
485 @retval EFI_SUCCESS New comment item is inserted.
486
487 **/
488 EFI_STATUS
489 ProfileGetComments (
490 IN UINT8 *Buffer,
491 IN UINTN BufferSize,
492 IN OUT COMMENT_LINE **CommentHead
493 )
494 {
495 COMMENT_LINE *CommentItem;
496
497 CommentItem = NULL;
498 CommentItem = AllocatePool (sizeof (COMMENT_LINE));
499 if (CommentItem == NULL) {
500 return EFI_OUT_OF_RESOURCES;
501 }
502
503 CommentItem->PtrNext = *CommentHead;
504 *CommentHead = CommentItem;
505
506 //
507 // Add a trailing '\0'
508 //
509 CommentItem->PtrComment = AllocatePool (BufferSize + 1);
510 if (CommentItem->PtrComment == NULL) {
511 FreePool (CommentItem);
512 return EFI_OUT_OF_RESOURCES;
513 }
514 CopyMem (CommentItem->PtrComment, Buffer, BufferSize);
515 *(CommentItem->PtrComment + BufferSize) = '\0';
516
517 return EFI_SUCCESS;
518 }
519
520 /**
521 Add new section item into Section head.
522
523 @param[in] Buffer Section item data buffer.
524 @param[in] BufferSize Size of section item.
525 @param[in, out] SectionHead Section item head entry.
526
527 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
528 @retval EFI_SUCCESS Section item is NULL or Section item is added.
529
530 **/
531 EFI_STATUS
532 ProfileGetSection (
533 IN UINT8 *Buffer,
534 IN UINTN BufferSize,
535 IN OUT SECTION_ITEM **SectionHead
536 )
537 {
538 SECTION_ITEM *SectionItem;
539 UINTN Length;
540 UINT8 *PtrBuf;
541 UINT8 *PtrEnd;
542
543 ASSERT(BufferSize >= 1);
544 //
545 // The first character of Buffer is '[', now we want for ']'
546 //
547 PtrEnd = (UINT8 *)((UINTN)Buffer + BufferSize - 1);
548 PtrBuf = (UINT8 *)((UINTN)Buffer + 1);
549 while (PtrBuf <= PtrEnd) {
550 if (*PtrBuf == ']') {
551 break;
552 }
553 PtrBuf ++;
554 }
555 if (PtrBuf > PtrEnd) {
556 //
557 // Not found. Invalid line
558 //
559 return EFI_NOT_FOUND;
560 }
561 if (PtrBuf <= Buffer + 1) {
562 // Empty name
563 return EFI_NOT_FOUND;
564 }
565
566 //
567 // excluding the heading '[' and tailing ']'
568 //
569 Length = PtrBuf - Buffer - 1;
570 ProfileTrim (
571 Buffer + 1,
572 &Length
573 );
574
575 //
576 // Invalid line if the section name is null
577 //
578 if (Length == 0) {
579 return EFI_NOT_FOUND;
580 }
581
582 if (!IsValidName((CHAR8 *)Buffer + 1, Length)) {
583 return EFI_INVALID_PARAMETER;
584 }
585
586 SectionItem = AllocatePool (sizeof (SECTION_ITEM));
587 if (SectionItem == NULL) {
588 return EFI_OUT_OF_RESOURCES;
589 }
590
591 SectionItem->PtrSection = NULL;
592 SectionItem->SecNameLen = Length;
593 SectionItem->PtrEntry = NULL;
594 SectionItem->PtrValue = NULL;
595 SectionItem->PtrNext = *SectionHead;
596 *SectionHead = SectionItem;
597
598 //
599 // Add a trailing '\0'
600 //
601 SectionItem->PtrSection = AllocatePool (Length + 1);
602 if (SectionItem->PtrSection == NULL) {
603 return EFI_OUT_OF_RESOURCES;
604 }
605
606 //
607 // excluding the heading '['
608 //
609 CopyMem (SectionItem->PtrSection, Buffer + 1, Length);
610 *(SectionItem->PtrSection + Length) = '\0';
611
612 return EFI_SUCCESS;
613 }
614
615 /**
616 Add new section entry and entry value into Section head.
617
618 @param[in] Buffer Section entry data buffer.
619 @param[in] BufferSize Size of section entry.
620 @param[in, out] SectionHead Section item head entry.
621
622 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
623 @retval EFI_SUCCESS Section entry is added.
624 @retval EFI_NOT_FOUND Section entry is not found.
625 @retval EFI_INVALID_PARAMETER Section entry is invalid.
626
627 **/
628 EFI_STATUS
629 ProfileGetEntry (
630 IN UINT8 *Buffer,
631 IN UINTN BufferSize,
632 IN OUT SECTION_ITEM **SectionHead
633 )
634 {
635 EFI_STATUS Status;
636 SECTION_ITEM *SectionItem;
637 SECTION_ITEM *PtrSection;
638 UINTN Length;
639 UINT8 *PtrBuf;
640 UINT8 *PtrEnd;
641
642 Status = EFI_SUCCESS;
643 PtrBuf = Buffer;
644 PtrEnd = (UINT8 *) ((UINTN)Buffer + BufferSize - 1);
645
646 //
647 // First search for '='
648 //
649 while (PtrBuf <= PtrEnd) {
650 if (*PtrBuf == '=') {
651 break;
652 }
653 PtrBuf++;
654 }
655 if (PtrBuf > PtrEnd) {
656 //
657 // Not found. Invalid line
658 //
659 return EFI_NOT_FOUND;
660 }
661 if (PtrBuf <= Buffer) {
662 // Empty name
663 return EFI_NOT_FOUND;
664 }
665
666 //
667 // excluding the tailing '='
668 //
669 Length = PtrBuf - Buffer;
670 ProfileTrim (
671 Buffer,
672 &Length
673 );
674
675 //
676 // Invalid line if the entry name is null
677 //
678 if (Length == 0) {
679 return EFI_NOT_FOUND;
680 }
681
682 if (!IsValidName((CHAR8 *)Buffer, Length)) {
683 return EFI_INVALID_PARAMETER;
684 }
685
686 //
687 // Omit this line if no section header has been found before
688 //
689 if (*SectionHead == NULL) {
690 return Status;
691 }
692 PtrSection = *SectionHead;
693
694 SectionItem = AllocatePool (sizeof (SECTION_ITEM));
695 if (SectionItem == NULL) {
696 return EFI_OUT_OF_RESOURCES;
697 }
698
699 SectionItem->PtrSection = NULL;
700 SectionItem->PtrEntry = NULL;
701 SectionItem->PtrValue = NULL;
702 SectionItem->SecNameLen = PtrSection->SecNameLen;
703 SectionItem->PtrNext = *SectionHead;
704 *SectionHead = SectionItem;
705
706 //
707 // SectionName, add a trailing '\0'
708 //
709 SectionItem->PtrSection = AllocatePool (PtrSection->SecNameLen + 1);
710 if (SectionItem->PtrSection == NULL) {
711 return EFI_OUT_OF_RESOURCES;
712 }
713 CopyMem (SectionItem->PtrSection, PtrSection->PtrSection, PtrSection->SecNameLen + 1);
714
715 //
716 // EntryName, add a trailing '\0'
717 //
718 SectionItem->PtrEntry = AllocatePool (Length + 1);
719 if (SectionItem->PtrEntry == NULL) {
720 FreePool(SectionItem->PtrSection);
721 return EFI_OUT_OF_RESOURCES;
722 }
723 CopyMem (SectionItem->PtrEntry, Buffer, Length);
724 *(SectionItem->PtrEntry + Length) = '\0';
725
726 //
727 // Next search for '#' or ';'
728 //
729 PtrBuf = PtrBuf + 1;
730 Buffer = PtrBuf;
731 while (PtrBuf <= PtrEnd) {
732 if (*PtrBuf == '#' || *PtrBuf == ';') {
733 break;
734 }
735 PtrBuf++;
736 }
737 if (PtrBuf <= Buffer) {
738 // Empty name
739 FreePool(SectionItem->PtrEntry);
740 FreePool(SectionItem->PtrSection);
741 return EFI_NOT_FOUND;
742 }
743 Length = PtrBuf - Buffer;
744 ProfileTrim (
745 Buffer,
746 &Length
747 );
748
749 //
750 // Invalid line if the entry value is null
751 //
752 if (Length == 0) {
753 FreePool(SectionItem->PtrEntry);
754 FreePool(SectionItem->PtrSection);
755 return EFI_NOT_FOUND;
756 }
757
758 if (!IsValidValue((CHAR8 *)Buffer, Length)) {
759 FreePool(SectionItem->PtrEntry);
760 FreePool(SectionItem->PtrSection);
761 return EFI_INVALID_PARAMETER;
762 }
763
764 //
765 // EntryValue, add a trailing '\0'
766 //
767 SectionItem->PtrValue = AllocatePool (Length + 1);
768 if (SectionItem->PtrValue == NULL) {
769 FreePool(SectionItem->PtrEntry);
770 FreePool(SectionItem->PtrSection);
771 return EFI_OUT_OF_RESOURCES;
772 }
773 CopyMem (SectionItem->PtrValue, Buffer, Length);
774 *(SectionItem->PtrValue + Length) = '\0';
775
776 return EFI_SUCCESS;
777 }
778
779 /**
780 Free all comment entry and section entry.
781
782 @param[in] Section Section entry list.
783 @param[in] Comment Comment entry list.
784
785 **/
786 VOID
787 FreeAllList (
788 IN SECTION_ITEM *Section,
789 IN COMMENT_LINE *Comment
790 )
791 {
792 SECTION_ITEM *PtrSection;
793 COMMENT_LINE *PtrComment;
794
795 while (Section != NULL) {
796 PtrSection = Section;
797 Section = Section->PtrNext;
798 if (PtrSection->PtrEntry != NULL) {
799 FreePool (PtrSection->PtrEntry);
800 }
801 if (PtrSection->PtrSection != NULL) {
802 FreePool (PtrSection->PtrSection);
803 }
804 if (PtrSection->PtrValue != NULL) {
805 FreePool (PtrSection->PtrValue);
806 }
807 FreePool (PtrSection);
808 }
809
810 while (Comment != NULL) {
811 PtrComment = Comment;
812 Comment = Comment->PtrNext;
813 if (PtrComment->PtrComment != NULL) {
814 FreePool (PtrComment->PtrComment);
815 }
816 FreePool (PtrComment);
817 }
818
819 return;
820 }
821
822 /**
823 Get section entry value.
824
825 @param[in] Section Section entry list.
826 @param[in] SectionName Section name.
827 @param[in] EntryName Section entry name.
828 @param[out] EntryValue Point to the got entry value.
829
830 @retval EFI_NOT_FOUND Section is not found.
831 @retval EFI_SUCCESS Section entry value is got.
832
833 **/
834 EFI_STATUS
835 UpdateGetProfileString (
836 IN SECTION_ITEM *Section,
837 IN CHAR8 *SectionName,
838 IN CHAR8 *EntryName,
839 OUT CHAR8 **EntryValue
840 )
841 {
842 *EntryValue = NULL;
843
844 while (Section != NULL) {
845 if (AsciiStrCmp ((CONST CHAR8 *) Section->PtrSection, (CONST CHAR8 *) SectionName) == 0) {
846 if (Section->PtrEntry != NULL) {
847 if (AsciiStrCmp ((CONST CHAR8 *) Section->PtrEntry, (CONST CHAR8 *) EntryName) == 0) {
848 break;
849 }
850 }
851 }
852 Section = Section->PtrNext;
853 }
854
855 if (Section == NULL) {
856 return EFI_NOT_FOUND;
857 }
858
859 *EntryValue = Section->PtrValue;
860
861 return EFI_SUCCESS;
862 }
863
864 /**
865 Pre process config data buffer into Section entry list and Comment entry list.
866
867 @param[in] DataBuffer Config raw file buffer.
868 @param[in] BufferSize Size of raw buffer.
869 @param[in, out] SectionHead Pointer to the section entry list.
870 @param[in, out] CommentHead Pointer to the comment entry list.
871
872 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
873 @retval EFI_SUCCESS Config data buffer is preprocessed.
874 @retval EFI_NOT_FOUND Config data buffer is invalid, because Section or Entry is not found.
875 @retval EFI_INVALID_PARAMETER Config data buffer is invalid, because Section or Entry is invalid.
876
877 **/
878 EFI_STATUS
879 PreProcessDataFile (
880 IN UINT8 *DataBuffer,
881 IN UINTN BufferSize,
882 IN OUT SECTION_ITEM **SectionHead,
883 IN OUT COMMENT_LINE **CommentHead
884 )
885 {
886 EFI_STATUS Status;
887 CHAR8 *Source;
888 CHAR8 *CurrentPtr;
889 CHAR8 *BufferEnd;
890 CHAR8 *PtrLine;
891 UINTN LineLength;
892 UINTN SourceLength;
893 UINTN MaxLineLength;
894
895 *SectionHead = NULL;
896 *CommentHead = NULL;
897 BufferEnd = (CHAR8 *) ( (UINTN) DataBuffer + BufferSize);
898 CurrentPtr = (CHAR8 *) DataBuffer;
899 MaxLineLength = MAX_LINE_LENGTH;
900 Status = EFI_SUCCESS;
901
902 PtrLine = AllocatePool (MaxLineLength);
903 if (PtrLine == NULL) {
904 return EFI_OUT_OF_RESOURCES;
905 }
906
907 while (CurrentPtr < BufferEnd) {
908 Source = CurrentPtr;
909 SourceLength = (UINTN)BufferEnd - (UINTN)CurrentPtr;
910 LineLength = MaxLineLength;
911 //
912 // With the assumption that line length is less than 512
913 // characters. Otherwise BUFFER_TOO_SMALL will be returned.
914 //
915 Status = ProfileGetLine (
916 (UINT8 *) Source,
917 SourceLength,
918 (UINT8 *) PtrLine,
919 &LineLength
920 );
921 if (EFI_ERROR (Status)) {
922 if (Status == EFI_BUFFER_TOO_SMALL) {
923 //
924 // If buffer too small, re-allocate the buffer according
925 // to the returned LineLength and try again.
926 //
927 FreePool (PtrLine);
928 PtrLine = NULL;
929 PtrLine = AllocatePool (LineLength);
930 if (PtrLine == NULL) {
931 Status = EFI_OUT_OF_RESOURCES;
932 break;
933 }
934 SourceLength = LineLength;
935 Status = ProfileGetLine (
936 (UINT8 *) Source,
937 SourceLength,
938 (UINT8 *) PtrLine,
939 &LineLength
940 );
941 if (EFI_ERROR (Status)) {
942 break;
943 }
944 MaxLineLength = LineLength;
945 } else {
946 break;
947 }
948 }
949 CurrentPtr = (CHAR8 *) ( (UINTN) CurrentPtr + LineLength);
950
951 //
952 // Line got. Trim the line before processing it.
953 //
954 ProfileTrim (
955 (UINT8 *) PtrLine,
956 &LineLength
957 );
958
959 //
960 // Blank line
961 //
962 if (LineLength == 0) {
963 continue;
964 }
965
966 if (PtrLine[0] == '#' || PtrLine[0] == ';') {
967 Status = ProfileGetComments (
968 (UINT8 *) PtrLine,
969 LineLength,
970 CommentHead
971 );
972 } else if (PtrLine[0] == '[') {
973 Status = ProfileGetSection (
974 (UINT8 *) PtrLine,
975 LineLength,
976 SectionHead
977 );
978 } else {
979 Status = ProfileGetEntry (
980 (UINT8 *) PtrLine,
981 LineLength,
982 SectionHead
983 );
984 }
985
986 if (EFI_ERROR (Status)) {
987 break;
988 }
989 }
990
991 //
992 // Free buffer
993 //
994 FreePool (PtrLine);
995
996 return Status;
997 }
998
999 /**
1000 Open an INI config file and return a context.
1001
1002 @param[in] DataBuffer Config raw file buffer.
1003 @param[in] BufferSize Size of raw buffer.
1004
1005 @return Config data buffer is opened and context is returned.
1006 @retval NULL No enough memory is allocated.
1007 @retval NULL Config data buffer is invalid.
1008 **/
1009 VOID *
1010 EFIAPI
1011 OpenIniFile (
1012 IN UINT8 *DataBuffer,
1013 IN UINTN BufferSize
1014 )
1015 {
1016 EFI_STATUS Status;
1017 INI_PARSING_LIB_CONTEXT *IniContext;
1018
1019 if (DataBuffer == NULL || BufferSize == 0) {
1020 return NULL;
1021 }
1022
1023 IniContext = AllocateZeroPool(sizeof(INI_PARSING_LIB_CONTEXT));
1024 if (IniContext == NULL) {
1025 return NULL;
1026 }
1027
1028 //
1029 // First process the data buffer and get all sections and entries
1030 //
1031 Status = PreProcessDataFile (
1032 DataBuffer,
1033 BufferSize,
1034 &IniContext->SectionHead,
1035 &IniContext->CommentHead
1036 );
1037 if (EFI_ERROR(Status)) {
1038 FreePool(IniContext);
1039 return NULL;
1040 }
1041 DEBUG_CODE_BEGIN ();
1042 DumpIniSection(IniContext);
1043 DEBUG_CODE_END ();
1044 return IniContext;
1045 }
1046
1047 /**
1048 Get section entry string value.
1049
1050 @param[in] Context INI Config file context.
1051 @param[in] SectionName Section name.
1052 @param[in] EntryName Section entry name.
1053 @param[out] EntryValue Point to the got entry string value.
1054
1055 @retval EFI_SUCCESS Section entry string value is got.
1056 @retval EFI_NOT_FOUND Section is not found.
1057 **/
1058 EFI_STATUS
1059 EFIAPI
1060 GetStringFromDataFile(
1061 IN VOID *Context,
1062 IN CHAR8 *SectionName,
1063 IN CHAR8 *EntryName,
1064 OUT CHAR8 **EntryValue
1065 )
1066 {
1067 INI_PARSING_LIB_CONTEXT *IniContext;
1068 EFI_STATUS Status;
1069
1070 if (Context == NULL || SectionName == NULL || EntryName == NULL || EntryValue == NULL) {
1071 return EFI_INVALID_PARAMETER;
1072 }
1073
1074 IniContext = Context;
1075
1076 *EntryValue = NULL;
1077 Status = UpdateGetProfileString (
1078 IniContext->SectionHead,
1079 SectionName,
1080 EntryName,
1081 EntryValue
1082 );
1083 return Status;
1084 }
1085
1086 /**
1087 Get section entry GUID value.
1088
1089 @param[in] Context INI Config file context.
1090 @param[in] SectionName Section name.
1091 @param[in] EntryName Section entry name.
1092 @param[out] Guid Point to the got GUID value.
1093
1094 @retval EFI_SUCCESS Section entry GUID value is got.
1095 @retval EFI_NOT_FOUND Section is not found.
1096 **/
1097 EFI_STATUS
1098 EFIAPI
1099 GetGuidFromDataFile (
1100 IN VOID *Context,
1101 IN CHAR8 *SectionName,
1102 IN CHAR8 *EntryName,
1103 OUT EFI_GUID *Guid
1104 )
1105 {
1106 CHAR8 *Value;
1107 EFI_STATUS Status;
1108 RETURN_STATUS RStatus;
1109
1110 if (Context == NULL || SectionName == NULL || EntryName == NULL || Guid == NULL) {
1111 return EFI_INVALID_PARAMETER;
1112 }
1113
1114 Status = GetStringFromDataFile(
1115 Context,
1116 SectionName,
1117 EntryName,
1118 &Value
1119 );
1120 if (EFI_ERROR(Status)) {
1121 return EFI_NOT_FOUND;
1122 }
1123 ASSERT (Value != NULL);
1124 RStatus = AsciiStrToGuid (Value, Guid);
1125 if (RETURN_ERROR (RStatus) || (Value[GUID_STRING_LENGTH] != '\0')) {
1126 return EFI_NOT_FOUND;
1127 }
1128 return EFI_SUCCESS;
1129 }
1130
1131 /**
1132 Get section entry decimal UINTN value.
1133
1134 @param[in] Context INI Config file context.
1135 @param[in] SectionName Section name.
1136 @param[in] EntryName Section entry name.
1137 @param[out] Data Point to the got decimal UINTN value.
1138
1139 @retval EFI_SUCCESS Section entry decimal UINTN value is got.
1140 @retval EFI_NOT_FOUND Section is not found.
1141 **/
1142 EFI_STATUS
1143 EFIAPI
1144 GetDecimalUintnFromDataFile (
1145 IN VOID *Context,
1146 IN CHAR8 *SectionName,
1147 IN CHAR8 *EntryName,
1148 OUT UINTN *Data
1149 )
1150 {
1151 CHAR8 *Value;
1152 EFI_STATUS Status;
1153
1154 if (Context == NULL || SectionName == NULL || EntryName == NULL || Data == NULL) {
1155 return EFI_INVALID_PARAMETER;
1156 }
1157
1158 Status = GetStringFromDataFile(
1159 Context,
1160 SectionName,
1161 EntryName,
1162 &Value
1163 );
1164 if (EFI_ERROR(Status)) {
1165 return EFI_NOT_FOUND;
1166 }
1167 ASSERT (Value != NULL);
1168 if (!IsValidDecimalString(Value, AsciiStrLen(Value))) {
1169 return EFI_NOT_FOUND;
1170 }
1171 *Data = AsciiStrDecimalToUintn(Value);
1172 return EFI_SUCCESS;
1173 }
1174
1175 /**
1176 Get section entry heximal UINTN value.
1177
1178 @param[in] Context INI Config file context.
1179 @param[in] SectionName Section name.
1180 @param[in] EntryName Section entry name.
1181 @param[out] Data Point to the got heximal UINTN value.
1182
1183 @retval EFI_SUCCESS Section entry heximal UINTN value is got.
1184 @retval EFI_NOT_FOUND Section is not found.
1185 **/
1186 EFI_STATUS
1187 EFIAPI
1188 GetHexUintnFromDataFile (
1189 IN VOID *Context,
1190 IN CHAR8 *SectionName,
1191 IN CHAR8 *EntryName,
1192 OUT UINTN *Data
1193 )
1194 {
1195 CHAR8 *Value;
1196 EFI_STATUS Status;
1197
1198 if (Context == NULL || SectionName == NULL || EntryName == NULL || Data == NULL) {
1199 return EFI_INVALID_PARAMETER;
1200 }
1201
1202 Status = GetStringFromDataFile(
1203 Context,
1204 SectionName,
1205 EntryName,
1206 &Value
1207 );
1208 if (EFI_ERROR(Status)) {
1209 return EFI_NOT_FOUND;
1210 }
1211 ASSERT (Value != NULL);
1212 if (!IsValidHexString(Value, AsciiStrLen(Value))) {
1213 return EFI_NOT_FOUND;
1214 }
1215 *Data = AsciiStrHexToUintn(Value);
1216 return EFI_SUCCESS;
1217 }
1218
1219 /**
1220 Get section entry heximal UINT64 value.
1221
1222 @param[in] Context INI Config file context.
1223 @param[in] SectionName Section name.
1224 @param[in] EntryName Section entry name.
1225 @param[out] Data Point to the got heximal UINT64 value.
1226
1227 @retval EFI_SUCCESS Section entry heximal UINT64 value is got.
1228 @retval EFI_NOT_FOUND Section is not found.
1229 **/
1230 EFI_STATUS
1231 EFIAPI
1232 GetHexUint64FromDataFile (
1233 IN VOID *Context,
1234 IN CHAR8 *SectionName,
1235 IN CHAR8 *EntryName,
1236 OUT UINT64 *Data
1237 )
1238 {
1239 CHAR8 *Value;
1240 EFI_STATUS Status;
1241
1242 if (Context == NULL || SectionName == NULL || EntryName == NULL || Data == NULL) {
1243 return EFI_INVALID_PARAMETER;
1244 }
1245
1246 Status = GetStringFromDataFile(
1247 Context,
1248 SectionName,
1249 EntryName,
1250 &Value
1251 );
1252 if (EFI_ERROR(Status)) {
1253 return EFI_NOT_FOUND;
1254 }
1255 ASSERT (Value != NULL);
1256 if (!IsValidHexString(Value, AsciiStrLen(Value))) {
1257 return EFI_NOT_FOUND;
1258 }
1259 *Data = AsciiStrHexToUint64(Value);
1260 return EFI_SUCCESS;
1261 }
1262
1263 /**
1264 Close an INI config file and free the context.
1265
1266 @param[in] Context INI Config file context.
1267 **/
1268 VOID
1269 EFIAPI
1270 CloseIniFile (
1271 IN VOID *Context
1272 )
1273 {
1274 INI_PARSING_LIB_CONTEXT *IniContext;
1275
1276 if (Context == NULL) {
1277 return ;
1278 }
1279
1280 IniContext = Context;
1281 FreeAllList(IniContext->SectionHead, IniContext->CommentHead);
1282
1283 return;
1284 }