]> git.proxmox.com Git - mirror_edk2.git/blame - IntelFrameworkModulePkg/Universal/FirmwareVolume/UpdateDriverDxe/ParseUpdateProfile.c
IntelFrameworkModulePkg DeviceMngr: Use safe string functions
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / FirmwareVolume / UpdateDriverDxe / ParseUpdateProfile.c
CommitLineData
b2824a8e 1/** @file\r
2 Source file for the component update driver. It parse the update\r
3 configuration file and pass the information to the update driver\r
4 so that the driver can perform updates accordingly.\r
5\r
6 Copyright (c) 2002 - 2010, Intel Corporation. All rights reserved.<BR>\r
7\r
8 This program and the accompanying materials\r
9 are licensed and made available under the terms and conditions\r
10 of the BSD License which accompanies this distribution. The\r
11 full text of the license may be found at\r
12 http://opensource.org/licenses/bsd-license.php\r
13\r
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
16\r
17**/\r
18\r
19#include "UpdateDriver.h"\r
20\r
21/**\r
22 Copy one line data from buffer data to the line buffer.\r
23\r
24 @param Buffer Buffer data.\r
25 @param BufferSize Buffer Size.\r
26 @param LineBuffer Line buffer to store the found line data.\r
27 @param LineSize On input, size of the input line buffer.\r
28 On output, size of the actual line buffer.\r
29\r
30 @retval EFI_BUFFER_TOO_SMALL The size of input line buffer is not enough.\r
31 @retval EFI_SUCCESS Copy line data into the line buffer.\r
32\r
33**/\r
34EFI_STATUS\r
35ProfileGetLine (\r
36 IN UINT8 *Buffer,\r
37 IN UINTN BufferSize,\r
38 IN OUT UINT8 *LineBuffer,\r
39 IN OUT UINTN *LineSize\r
40 )\r
41{\r
42 UINTN Length;\r
43 UINT8 *PtrBuf;\r
44 UINTN PtrEnd;\r
45\r
46 PtrBuf = Buffer;\r
47 PtrEnd = (UINTN)Buffer + BufferSize;\r
48\r
49 //\r
50 // 0x0D indicates a line break. Otherwise there is no line break\r
51 //\r
52 while ((UINTN)PtrBuf < PtrEnd) {\r
53 if (*PtrBuf == 0x0D) {\r
54 break;\r
55 }\r
56 PtrBuf++;\r
57 }\r
58\r
59 if ((UINTN)PtrBuf >= (PtrEnd - 1)) {\r
60 //\r
61 // The buffer ends without any line break\r
62 // or it is the last character of the buffer\r
63 //\r
64 Length = BufferSize;\r
65 } else if (*(PtrBuf + 1) == 0x0A) {\r
66 //\r
67 // Further check if a 0x0A follows. If yes, count 0xA\r
68 //\r
69 Length = (UINTN) PtrBuf - (UINTN) Buffer + 2;\r
70 } else {\r
71 Length = (UINTN) PtrBuf - (UINTN) Buffer + 1;\r
72 }\r
73\r
74 if (Length > (*LineSize)) {\r
75 *LineSize = Length;\r
76 return EFI_BUFFER_TOO_SMALL;\r
77 }\r
78\r
79 SetMem (LineBuffer, *LineSize, 0x0);\r
80 *LineSize = Length;\r
81 CopyMem (LineBuffer, Buffer, Length);\r
82\r
83 return EFI_SUCCESS;\r
84}\r
85\r
86/**\r
87 Trim Buffer by removing all CR, LF, TAB, and SPACE chars in its head and tail.\r
88\r
89 @param Buffer On input, buffer data to be trimed.\r
90 On output, the trimmed buffer.\r
91 @param BufferSize On input, size of original buffer data.\r
92 On output, size of the trimmed buffer.\r
93\r
94**/\r
95VOID\r
96ProfileTrim (\r
97 IN OUT UINT8 *Buffer,\r
98 IN OUT UINTN *BufferSize\r
99 )\r
100{\r
101 UINTN Length;\r
102 UINT8 *PtrBuf;\r
103 UINT8 *PtrEnd;\r
104\r
105 if (*BufferSize == 0) {\r
106 return;\r
107 }\r
108\r
109 //\r
110 // Trim the tail first, include CR, LF, TAB, and SPACE.\r
111 //\r
112 Length = *BufferSize;\r
113 PtrBuf = (UINT8 *) ((UINTN) Buffer + Length - 1);\r
114 while (PtrBuf >= Buffer) {\r
115 if ((*PtrBuf != 0x0D) && (*PtrBuf != 0x0A )\r
116 && (*PtrBuf != 0x20) && (*PtrBuf != 0x09)) {\r
117 break;\r
118 }\r
119 PtrBuf --;\r
120 }\r
121\r
122 //\r
123 // all spaces, a blank line, return directly;\r
124 //\r
125 if (PtrBuf < Buffer) {\r
126 *BufferSize = 0;\r
127 return;\r
128 }\r
129\r
130 Length = (UINTN)PtrBuf - (UINTN)Buffer + 1;\r
131 PtrEnd = PtrBuf;\r
132 PtrBuf = Buffer;\r
133\r
134 //\r
135 // Now skip the heading CR, LF, TAB and SPACE\r
136 //\r
137 while (PtrBuf <= PtrEnd) {\r
138 if ((*PtrBuf != 0x0D) && (*PtrBuf != 0x0A )\r
139 && (*PtrBuf != 0x20) && (*PtrBuf != 0x09)) {\r
140 break;\r
141 }\r
142 PtrBuf++;\r
143 }\r
144\r
145 //\r
146 // If no heading CR, LF, TAB or SPACE, directly return\r
147 //\r
148 if (PtrBuf == Buffer) {\r
149 *BufferSize = Length;\r
150 return;\r
151 }\r
152\r
153 *BufferSize = (UINTN)PtrEnd - (UINTN)PtrBuf + 1;\r
154\r
155 //\r
156 // The first Buffer..PtrBuf characters are CR, LF, TAB or SPACE.\r
157 // Now move out all these characters.\r
158 //\r
159 while (PtrBuf <= PtrEnd) {\r
160 *Buffer = *PtrBuf;\r
161 Buffer++;\r
162 PtrBuf++;\r
163 }\r
164\r
165 return;\r
166}\r
167\r
168/**\r
169 Insert new comment item into comment head.\r
170\r
171 @param Buffer Comment buffer to be added.\r
172 @param BufferSize Size of comment buffer.\r
173 @param CommentHead Comment Item head entry.\r
174\r
175 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.\r
176 @retval EFI_SUCCESS New comment item is inserted.\r
177\r
178**/\r
179EFI_STATUS\r
180ProfileGetComments (\r
181 IN UINT8 *Buffer,\r
182 IN UINTN BufferSize,\r
183 IN OUT COMMENT_LINE **CommentHead\r
184 )\r
185{\r
186 COMMENT_LINE *CommentItem;\r
187\r
188 CommentItem = NULL;\r
189 CommentItem = AllocatePool (sizeof (COMMENT_LINE));\r
190 if (CommentItem == NULL) {\r
191 return EFI_OUT_OF_RESOURCES;\r
192 }\r
193\r
194 CommentItem->ptrNext = *CommentHead;\r
195 *CommentHead = CommentItem;\r
196\r
197 //\r
198 // Add a trailing '\0'\r
199 //\r
200 CommentItem->ptrComment = AllocatePool (BufferSize + 1);\r
201 if (CommentItem->ptrComment == NULL) {\r
202 FreePool (CommentItem);\r
203 return EFI_OUT_OF_RESOURCES;\r
204 }\r
205 CopyMem (CommentItem->ptrComment, Buffer, BufferSize);\r
206 *(CommentItem->ptrComment + BufferSize) = '\0';\r
207\r
208 return EFI_SUCCESS;\r
209}\r
210\r
211/**\r
212 Add new section item into Section head.\r
213\r
214 @param Buffer Section item data buffer.\r
215 @param BufferSize Size of section item.\r
216 @param SectionHead Section item head entry.\r
217\r
218 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.\r
219 @retval EFI_SUCCESS Section item is NULL or Section item is added.\r
220\r
221**/\r
222EFI_STATUS\r
223ProfileGetSection (\r
224 IN UINT8 *Buffer,\r
225 IN UINTN BufferSize,\r
226 IN OUT SECTION_ITEM **SectionHead\r
227 )\r
228{\r
229 EFI_STATUS Status;\r
230 SECTION_ITEM *SectionItem;\r
231 UINTN Length;\r
232 UINT8 *PtrBuf;\r
233\r
234 Status = EFI_SUCCESS;\r
235 //\r
236 // The first character of Buffer is '[', now we want for ']'\r
237 //\r
238 PtrBuf = (UINT8 *)((UINTN)Buffer + BufferSize - 1);\r
239 while (PtrBuf > Buffer) {\r
240 if (*PtrBuf == ']') {\r
241 break;\r
242 }\r
243 PtrBuf --;\r
244 }\r
245 if (PtrBuf <= Buffer) {\r
246 //\r
247 // Not found. Omit this line\r
248 //\r
249 return Status;\r
250 }\r
251\r
252 //\r
253 // excluding the heading '[' and tailing ']'\r
254 //\r
255 Length = PtrBuf - Buffer - 1;\r
256 ProfileTrim (\r
257 Buffer + 1,\r
258 &Length\r
259 );\r
260\r
261 //\r
262 // omit this line if the section name is null\r
263 //\r
264 if (Length == 0) {\r
265 return Status;\r
266 }\r
267\r
268 SectionItem = AllocatePool (sizeof (SECTION_ITEM));\r
269 if (SectionItem == NULL) {\r
270 return EFI_OUT_OF_RESOURCES;\r
271 }\r
272\r
273 SectionItem->ptrSection = NULL;\r
274 SectionItem->SecNameLen = Length;\r
275 SectionItem->ptrEntry = NULL;\r
276 SectionItem->ptrValue = NULL;\r
277 SectionItem->ptrNext = *SectionHead;\r
278 *SectionHead = SectionItem;\r
279\r
280 //\r
281 // Add a trailing '\0'\r
282 //\r
283 SectionItem->ptrSection = AllocatePool (Length + 1);\r
284 if (SectionItem->ptrSection == NULL) {\r
285 return EFI_OUT_OF_RESOURCES;\r
286 }\r
287\r
288 //\r
289 // excluding the heading '['\r
290 //\r
291 CopyMem (SectionItem->ptrSection, Buffer + 1, Length);\r
292 *(SectionItem->ptrSection + Length) = '\0';\r
293\r
294 return EFI_SUCCESS;\r
295}\r
296\r
297/**\r
298 Add new section entry and entry value into Section head.\r
299\r
300 @param Buffer Section entry data buffer.\r
301 @param BufferSize Size of section entry.\r
302 @param SectionHead Section item head entry.\r
303\r
304 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.\r
305 @retval EFI_SUCCESS Section entry is NULL or Section entry is added.\r
306\r
307**/\r
308EFI_STATUS\r
309ProfileGetEntry (\r
310 IN UINT8 *Buffer,\r
311 IN UINTN BufferSize,\r
312 IN OUT SECTION_ITEM **SectionHead\r
313 )\r
314{\r
315 EFI_STATUS Status;\r
316 SECTION_ITEM *SectionItem;\r
317 SECTION_ITEM *PtrSection;\r
318 UINTN Length;\r
319 UINT8 *PtrBuf;\r
320 UINT8 *PtrEnd;\r
321\r
322 Status = EFI_SUCCESS;\r
323 PtrBuf = Buffer;\r
324 PtrEnd = (UINT8 *) ((UINTN)Buffer + BufferSize - 1);\r
325\r
326 //\r
327 // First search for '='\r
328 //\r
329 while (PtrBuf <= PtrEnd) {\r
330 if (*PtrBuf == '=') {\r
331 break;\r
332 }\r
333 PtrBuf++;\r
334 }\r
335 if (PtrBuf > PtrEnd) {\r
336 //\r
337 // Not found. Omit this line\r
338 //\r
339 return Status;\r
340 }\r
341\r
342 //\r
343 // excluding the tailing '='\r
344 //\r
345 Length = PtrBuf - Buffer;\r
346 ProfileTrim (\r
347 Buffer,\r
348 &Length\r
349 );\r
350\r
351 //\r
352 // Omit this line if the entry name is null\r
353 //\r
354 if (Length == 0) {\r
355 return Status;\r
356 }\r
357\r
358 //\r
359 // Omit this line if no section header has been found before\r
360 //\r
361 if (*SectionHead == NULL) {\r
362 return Status;\r
363 }\r
364 PtrSection = *SectionHead;\r
365\r
366 SectionItem = AllocatePool (sizeof (SECTION_ITEM));\r
367 if (SectionItem == NULL) {\r
368 return EFI_OUT_OF_RESOURCES;\r
369 }\r
370\r
371 SectionItem->ptrSection = NULL;\r
372 SectionItem->ptrEntry = NULL;\r
373 SectionItem->ptrValue = NULL;\r
374 SectionItem->SecNameLen = PtrSection->SecNameLen;\r
375 SectionItem->ptrNext = *SectionHead;\r
376 *SectionHead = SectionItem;\r
377\r
378 //\r
379 // SectionName, add a trailing '\0'\r
380 //\r
381 SectionItem->ptrSection = AllocatePool (PtrSection->SecNameLen + 1);\r
382 if (SectionItem->ptrSection == NULL) {\r
383 return EFI_OUT_OF_RESOURCES;\r
384 }\r
385 CopyMem (SectionItem->ptrSection, PtrSection->ptrSection, PtrSection->SecNameLen + 1);\r
386\r
387 //\r
388 // EntryName, add a trailing '\0'\r
389 //\r
390 SectionItem->ptrEntry = AllocatePool (Length + 1);\r
391 if (SectionItem->ptrEntry == NULL) {\r
392 return EFI_OUT_OF_RESOURCES;\r
393 }\r
394 CopyMem (SectionItem->ptrEntry, Buffer, Length);\r
395 *(SectionItem->ptrEntry + Length) = '\0';\r
396\r
397 //\r
398 // Next search for '#'\r
399 //\r
400 PtrBuf = PtrBuf + 1;\r
401 Buffer = PtrBuf;\r
402 while (PtrBuf <= PtrEnd) {\r
403 if (*PtrBuf == '#') {\r
404 break;\r
405 }\r
406 PtrBuf++;\r
407 }\r
408 Length = PtrBuf - Buffer;\r
409 ProfileTrim (\r
410 Buffer,\r
411 &Length\r
412 );\r
413\r
414 if (Length > 0) {\r
415 //\r
416 // EntryValue, add a trailing '\0'\r
417 //\r
418 SectionItem->ptrValue = AllocatePool (Length + 1);\r
419 if (SectionItem->ptrValue == NULL) {\r
420 return EFI_OUT_OF_RESOURCES;\r
421 }\r
422 CopyMem (SectionItem->ptrValue, Buffer, Length);\r
423 *(SectionItem->ptrValue + Length) = '\0';\r
424 }\r
425\r
426 return EFI_SUCCESS;\r
427}\r
428\r
429/**\r
430 Free all comment entry and section entry.\r
431\r
432 @param Section Section entry list.\r
433 @param Comment Comment entry list.\r
434\r
435**/\r
436VOID\r
437FreeAllList (\r
438 IN SECTION_ITEM *Section,\r
439 IN COMMENT_LINE *Comment\r
440 )\r
441{\r
442 SECTION_ITEM *PtrSection;\r
443 COMMENT_LINE *PtrComment;\r
444\r
445 while (Section != NULL) {\r
446 PtrSection = Section;\r
447 Section = Section->ptrNext;\r
448 if (PtrSection->ptrEntry != NULL) {\r
449 FreePool (PtrSection->ptrEntry);\r
450 }\r
451 if (PtrSection->ptrSection != NULL) {\r
452 FreePool (PtrSection->ptrSection);\r
453 }\r
454 if (PtrSection->ptrValue != NULL) {\r
455 FreePool (PtrSection->ptrValue);\r
456 }\r
457 FreePool (PtrSection);\r
458 }\r
459\r
460 while (Comment != NULL) {\r
461 PtrComment = Comment;\r
462 Comment = Comment->ptrNext;\r
463 if (PtrComment->ptrComment != NULL) {\r
464 FreePool (PtrComment->ptrComment);\r
465 }\r
466 FreePool (PtrComment);\r
467 }\r
468\r
469 return;\r
470}\r
471\r
472/**\r
473 Get section entry value.\r
474\r
475 @param Section Section entry list.\r
476 @param SectionName Section name.\r
477 @param EntryName Section entry name.\r
478 @param EntryValue Point to the got entry value.\r
479\r
480 @retval EFI_NOT_FOUND Section is not found.\r
481 @retval EFI_SUCCESS Section entry value is got.\r
482\r
483**/\r
484EFI_STATUS\r
485UpdateGetProfileString (\r
486 IN SECTION_ITEM *Section,\r
487 IN UINT8 *SectionName,\r
488 IN UINT8 *EntryName,\r
489 OUT UINT8 **EntryValue\r
490 )\r
491{\r
492 *EntryValue = NULL;\r
493\r
494 while (Section != NULL) {\r
495 if (AsciiStrCmp ((CONST CHAR8 *) Section->ptrSection, (CONST CHAR8 *) SectionName) == 0) {\r
496 if (Section->ptrEntry != NULL) {\r
497 if (AsciiStrCmp ((CONST CHAR8 *) Section->ptrEntry, (CONST CHAR8 *) EntryName) == 0) {\r
498 break;\r
499 }\r
500 }\r
501 }\r
502 Section = Section->ptrNext;\r
503 }\r
504\r
505 if (Section == NULL) {\r
506 return EFI_NOT_FOUND;\r
507 }\r
508\r
509 *EntryValue = (UINT8 *) Section->ptrValue;\r
510\r
511 return EFI_SUCCESS;\r
512}\r
513\r
514/**\r
515 Convert the dec or hex ascii string to value.\r
516\r
517 @param Str ascii string to be converted.\r
518\r
519 @return the converted value.\r
520\r
521**/\r
522UINTN\r
523UpdateAtoi (\r
524 IN UINT8 *Str\r
525 )\r
526{\r
527 UINTN Number;\r
528\r
529 Number = 0;\r
530\r
531 //\r
532 // Skip preceeding while spaces\r
533 //\r
534 while (*Str != '\0') {\r
535 if (*Str != 0x20) {\r
536 break;\r
537 }\r
538 Str++;\r
539 }\r
540\r
541 if (*Str == '\0') {\r
542 return Number;\r
543 }\r
544\r
545 //\r
546 // Find whether the string is prefixed by 0x.\r
547 // That is, it should be xtoi or atoi.\r
548 //\r
549 if (*Str == '0') {\r
550 if ((*(Str+1) == 'x' ) || ( *(Str+1) == 'X')) {\r
551 return AsciiStrHexToUintn ((CONST CHAR8 *) Str);\r
552 }\r
553 }\r
554\r
555 while (*Str != '\0') {\r
556 if ((*Str >= '0') && (*Str <= '9')) {\r
557 Number = Number * 10 + *Str - '0';\r
558 } else {\r
559 break;\r
560 }\r
561 Str++;\r
562 }\r
563\r
564 return Number;\r
565}\r
566\r
567/**\r
568 Converts a decimal value to a Null-terminated ascii string.\r
569\r
570 @param Buffer Pointer to the output buffer for the produced Null-terminated\r
571 ASCII string.\r
572 @param Value The 64-bit sgned value to convert to a string.\r
573\r
574 @return The number of ASCII characters in Buffer not including the Null-terminator.\r
575\r
576**/\r
577UINTN\r
578UpdateValueToString (\r
579 IN OUT UINT8 *Buffer,\r
580 IN INT64 Value\r
581 )\r
582{\r
583 UINT8 TempBuffer[30];\r
584 UINT8 *TempStr;\r
585 UINT8 *BufferPtr;\r
586 UINTN Count;\r
587 UINT32 Remainder;\r
588\r
589 TempStr = TempBuffer;\r
590 BufferPtr = Buffer;\r
591 Count = 0;\r
592\r
593 if (Value < 0) {\r
594 *BufferPtr = '-';\r
595 BufferPtr++;\r
596 Value = -Value;\r
597 Count++;\r
598 }\r
599\r
600 do {\r
601 Value = (INT64) DivU64x32Remainder ((UINT64)Value, 10, &Remainder);\r
602 //\r
603 // The first item of TempStr is not occupied. It's kind of flag\r
604 //\r
605 TempStr++;\r
606 Count++;\r
607 *TempStr = (UINT8) ((UINT8)Remainder + '0');\r
608 } while (Value != 0);\r
609\r
610 //\r
611 // Reverse temp string into Buffer.\r
612 //\r
613 while (TempStr != TempBuffer) {\r
614 *BufferPtr = *TempStr;\r
615 BufferPtr++;\r
616 TempStr --;\r
617 }\r
618\r
619 *BufferPtr = 0;\r
620\r
621 return Count;\r
622}\r
623\r
624/**\r
625 Convert the input value to a ascii string, \r
626 and concatenates this string to the input string.\r
627\r
628 @param Str Pointer to a Null-terminated ASCII string.\r
629 @param Number The unsgned value to convert to a string.\r
630\r
631**/\r
632VOID\r
633UpdateStrCatNumber (\r
634 IN OUT UINT8 *Str,\r
635 IN UINTN Number\r
636 )\r
637{\r
638 UINTN Count;\r
639\r
640 while (*Str != '\0') {\r
641 Str++;\r
642 }\r
643\r
644 Count = UpdateValueToString (Str, (INT64)Number);\r
645\r
646 *(Str + Count) = '\0';\r
647\r
648 return;\r
649}\r
650\r
651/**\r
652 Convert the input ascii string into GUID value.\r
653\r
654 @param Str Ascii GUID string to be converted.\r
655 @param Guid Pointer to the converted GUID value.\r
656\r
657 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.\r
658 @retval EFI_NOT_FOUND The input ascii string is not a valid GUID format string.\r
659 @retval EFI_SUCCESS GUID value is got.\r
660\r
661**/\r
662EFI_STATUS\r
663UpdateStringToGuid (\r
664 IN UINT8 *Str,\r
665 IN OUT EFI_GUID *Guid\r
666 )\r
667{\r
668 UINT8 *PtrBuffer;\r
669 UINT8 *PtrPosition;\r
670 UINT8 *Buffer;\r
671 UINTN Data;\r
672 UINTN StrLen;\r
673 UINTN Index;\r
674 UINT8 Digits[3];\r
675\r
676 StrLen = AsciiStrLen ((CONST CHAR8 *) Str);\r
677 Buffer = AllocatePool (StrLen + 1);\r
678 if (Buffer == NULL) {\r
679 return EFI_OUT_OF_RESOURCES;\r
680 }\r
681 AsciiStrCpy ((CHAR8 *)Buffer, (CHAR8 *)Str);\r
682\r
683 //\r
684 // Data1\r
685 //\r
686 PtrBuffer = Buffer;\r
687 PtrPosition = PtrBuffer;\r
688 while (*PtrBuffer != '\0') {\r
689 if (*PtrBuffer == '-') {\r
690 break;\r
691 }\r
692 PtrBuffer++;\r
693 }\r
694 if (*PtrBuffer == '\0') {\r
695 FreePool (Buffer);\r
696 return EFI_NOT_FOUND;\r
697 }\r
698\r
699 *PtrBuffer = '\0';\r
700 Data = AsciiStrHexToUintn ((CONST CHAR8 *) PtrPosition);\r
701 Guid->Data1 = (UINT32)Data;\r
702\r
703 //\r
704 // Data2\r
705 //\r
706 PtrBuffer++;\r
707 PtrPosition = PtrBuffer;\r
708 while (*PtrBuffer != '\0') {\r
709 if (*PtrBuffer == '-') {\r
710 break;\r
711 }\r
712 PtrBuffer++;\r
713 }\r
714 if (*PtrBuffer == '\0') {\r
715 FreePool (Buffer);\r
716 return EFI_NOT_FOUND;\r
717 }\r
718 *PtrBuffer = '\0';\r
719 Data = AsciiStrHexToUintn ((CONST CHAR8 *) PtrPosition);\r
720 Guid->Data2 = (UINT16)Data;\r
721\r
722 //\r
723 // Data3\r
724 //\r
725 PtrBuffer++;\r
726 PtrPosition = PtrBuffer;\r
727 while (*PtrBuffer != '\0') {\r
728 if (*PtrBuffer == '-') {\r
729 break;\r
730 }\r
731 PtrBuffer++;\r
732 }\r
733 if (*PtrBuffer == '\0') {\r
734 FreePool (Buffer);\r
735 return EFI_NOT_FOUND;\r
736 }\r
737 *PtrBuffer = '\0';\r
738 Data = AsciiStrHexToUintn ((CONST CHAR8 *) PtrPosition);\r
739 Guid->Data3 = (UINT16)Data;\r
740\r
741 //\r
742 // Data4[0..1]\r
743 //\r
744 for ( Index = 0 ; Index < 2 ; Index++) {\r
745 PtrBuffer++;\r
746 if ((*PtrBuffer == '\0') || ( *(PtrBuffer + 1) == '\0')) {\r
747 FreePool (Buffer);\r
748 return EFI_NOT_FOUND;\r
749 }\r
750 Digits[0] = *PtrBuffer;\r
751 PtrBuffer++;\r
752 Digits[1] = *PtrBuffer;\r
753 Digits[2] = '\0';\r
754 Data = AsciiStrHexToUintn ((CONST CHAR8 *) Digits);\r
755 Guid->Data4[Index] = (UINT8)Data;\r
756 }\r
757\r
758 //\r
759 // skip the '-'\r
760 //\r
761 PtrBuffer++;\r
762 if ((*PtrBuffer != '-' ) || ( *PtrBuffer == '\0')) {\r
763 return EFI_NOT_FOUND;\r
764 }\r
765\r
766 //\r
767 // Data4[2..7]\r
768 //\r
769 for ( ; Index < 8; Index++) {\r
770 PtrBuffer++;\r
771 if ((*PtrBuffer == '\0') || ( *(PtrBuffer + 1) == '\0')) {\r
772 FreePool (Buffer);\r
773 return EFI_NOT_FOUND;\r
774 }\r
775 Digits[0] = *PtrBuffer;\r
776 PtrBuffer++;\r
777 Digits[1] = *PtrBuffer;\r
778 Digits[2] = '\0';\r
779 Data = AsciiStrHexToUintn ((CONST CHAR8 *) Digits);\r
780 Guid->Data4[Index] = (UINT8)Data;\r
781 }\r
782\r
783 FreePool (Buffer);\r
784\r
785 return EFI_SUCCESS;\r
786}\r
787\r
788/**\r
789 Pre process config data buffer into Section entry list and Comment entry list.\r
790 \r
791 @param DataBuffer Config raw file buffer.\r
792 @param BufferSize Size of raw buffer.\r
793 @param SectionHead Pointer to the section entry list.\r
794 @param CommentHead Pointer to the comment entry list.\r
795\r
796 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.\r
797 @retval EFI_SUCCESS Config data buffer is preprocessed.\r
798\r
799**/\r
800EFI_STATUS\r
801PreProcessDataFile (\r
802 IN UINT8 *DataBuffer,\r
803 IN UINTN BufferSize,\r
804 IN OUT SECTION_ITEM **SectionHead,\r
805 IN OUT COMMENT_LINE **CommentHead\r
806 )\r
807{\r
808 EFI_STATUS Status;\r
809 CHAR8 *Source;\r
810 CHAR8 *CurrentPtr;\r
811 CHAR8 *BufferEnd;\r
812 CHAR8 *PtrLine;\r
813 UINTN LineLength;\r
814 UINTN SourceLength;\r
815 UINTN MaxLineLength;\r
816\r
817 *SectionHead = NULL;\r
818 *CommentHead = NULL;\r
819 BufferEnd = (CHAR8 *) ( (UINTN) DataBuffer + BufferSize);\r
820 CurrentPtr = (CHAR8 *) DataBuffer;\r
821 MaxLineLength = MAX_LINE_LENGTH;\r
822 Status = EFI_SUCCESS;\r
823\r
824 PtrLine = AllocatePool (MaxLineLength);\r
825 if (PtrLine == NULL) {\r
826 return EFI_OUT_OF_RESOURCES;\r
827 }\r
828\r
829 while (CurrentPtr < BufferEnd) {\r
830 Source = CurrentPtr;\r
831 SourceLength = (UINTN)BufferEnd - (UINTN)CurrentPtr;\r
832 LineLength = MaxLineLength;\r
833 //\r
834 // With the assumption that line length is less than 512\r
835 // characters. Otherwise BUFFER_TOO_SMALL will be returned.\r
836 //\r
837 Status = ProfileGetLine (\r
838 (UINT8 *) Source,\r
839 SourceLength,\r
840 (UINT8 *) PtrLine,\r
841 &LineLength\r
842 );\r
843 if (EFI_ERROR (Status)) {\r
844 if (Status == EFI_BUFFER_TOO_SMALL) {\r
845 //\r
846 // If buffer too small, re-allocate the buffer according\r
847 // to the returned LineLength and try again.\r
848 //\r
849 FreePool (PtrLine);\r
850 PtrLine = NULL;\r
851 PtrLine = AllocatePool (LineLength);\r
852 if (PtrLine == NULL) {\r
853 Status = EFI_OUT_OF_RESOURCES;\r
854 break;\r
855 }\r
856 SourceLength = LineLength;\r
857 Status = ProfileGetLine (\r
858 (UINT8 *) Source,\r
859 SourceLength,\r
860 (UINT8 *) PtrLine,\r
861 &LineLength\r
862 );\r
863 if (EFI_ERROR (Status)) {\r
864 break;\r
865 }\r
866 MaxLineLength = LineLength;\r
867 } else {\r
868 break;\r
869 }\r
870 }\r
871 CurrentPtr = (CHAR8 *) ( (UINTN) CurrentPtr + LineLength);\r
872\r
873 //\r
874 // Line got. Trim the line before processing it.\r
875 //\r
876 ProfileTrim (\r
877 (UINT8 *) PtrLine,\r
878 &LineLength\r
879 );\r
880\r
881 //\r
882 // Blank line\r
883 //\r
884 if (LineLength == 0) {\r
885 continue;\r
886 }\r
887\r
888 if (PtrLine[0] == '#') {\r
889 Status = ProfileGetComments (\r
890 (UINT8 *) PtrLine,\r
891 LineLength,\r
892 CommentHead\r
893 );\r
894 } else if (PtrLine[0] == '[') {\r
895 Status = ProfileGetSection (\r
896 (UINT8 *) PtrLine,\r
897 LineLength,\r
898 SectionHead\r
899 );\r
900 } else {\r
901 Status = ProfileGetEntry (\r
902 (UINT8 *) PtrLine,\r
903 LineLength,\r
904 SectionHead\r
905 );\r
906 }\r
907\r
908 if (EFI_ERROR (Status)) {\r
909 break;\r
910 }\r
911 }\r
912\r
913 //\r
914 // Free buffer\r
915 //\r
916 FreePool (PtrLine);\r
917\r
918 return Status;\r
919}\r
920\r
921/**\r
922 Parse Config data file to get the updated data array.\r
923\r
924 @param DataBuffer Config raw file buffer.\r
925 @param BufferSize Size of raw buffer.\r
926 @param NumOfUpdates Pointer to the number of update data.\r
927 @param UpdateArray Pointer to the config of update data.\r
928\r
929 @retval EFI_NOT_FOUND No config data is found.\r
930 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.\r
931 @retval EFI_SUCCESS Parse the config file successfully.\r
932\r
933**/\r
934EFI_STATUS\r
935ParseUpdateDataFile (\r
936 IN UINT8 *DataBuffer,\r
937 IN UINTN BufferSize,\r
938 IN OUT UINTN *NumOfUpdates,\r
939 IN OUT UPDATE_CONFIG_DATA **UpdateArray\r
940 )\r
941{\r
942 EFI_STATUS Status;\r
943 CHAR8 *Value;\r
944 CHAR8 *SectionName;\r
945 CHAR8 Entry[MAX_LINE_LENGTH];\r
946 SECTION_ITEM *SectionHead;\r
947 COMMENT_LINE *CommentHead;\r
948 UINTN Num;\r
949 UINTN Index;\r
950 EFI_GUID FileGuid;\r
951\r
952 SectionHead = NULL;\r
953 CommentHead = NULL;\r
954\r
955 //\r
956 // First process the data buffer and get all sections and entries\r
957 //\r
958 Status = PreProcessDataFile (\r
959 DataBuffer,\r
960 BufferSize,\r
961 &SectionHead,\r
962 &CommentHead\r
963 );\r
964 if (EFI_ERROR (Status)) {\r
965 FreeAllList (SectionHead, CommentHead);\r
966 return Status;\r
967 }\r
968\r
969 //\r
970 // Now get NumOfUpdate\r
971 //\r
972 Value = NULL;\r
973 Status = UpdateGetProfileString (\r
974 SectionHead,\r
975 (UINT8 *) "Head",\r
976 (UINT8 *) "NumOfUpdate",\r
977 (UINT8 **) &Value\r
978 );\r
979 if (Value == NULL) {\r
980 FreeAllList (SectionHead, CommentHead);\r
981 return EFI_NOT_FOUND;\r
982 }\r
983 Num = UpdateAtoi((UINT8 *) Value);\r
984 if (Num <= 0) {\r
985 FreeAllList (SectionHead, CommentHead);\r
986 return EFI_NOT_FOUND;\r
987 }\r
988\r
989 *NumOfUpdates = Num;\r
990 *UpdateArray = AllocatePool ((sizeof (UPDATE_CONFIG_DATA) * Num));\r
991 if (*UpdateArray == NULL) {\r
992 FreeAllList (SectionHead, CommentHead);\r
993 return EFI_OUT_OF_RESOURCES;\r
994 }\r
995\r
996 for ( Index = 0 ; Index < *NumOfUpdates ; Index++) {\r
997 //\r
998 // Get the section name of each update\r
999 //\r
1000 AsciiStrCpy (Entry, "Update");\r
1001 UpdateStrCatNumber ((UINT8 *) Entry, Index);\r
1002 Value = NULL;\r
1003 Status = UpdateGetProfileString (\r
1004 SectionHead,\r
1005 (UINT8 *) "Head",\r
1006 (UINT8 *) Entry,\r
1007 (UINT8 **) &Value\r
1008 );\r
1009 if (Value == NULL) {\r
1010 FreeAllList (SectionHead, CommentHead);\r
1011 return EFI_NOT_FOUND;\r
1012 }\r
1013\r
1014 //\r
1015 // The section name of this update has been found.\r
1016 // Now looks for all the config data of this update\r
1017 //\r
1018 SectionName = Value;\r
1019\r
1020 //\r
1021 // UpdateType\r
1022 //\r
1023 Value = NULL;\r
1024 Status = UpdateGetProfileString (\r
1025 SectionHead,\r
1026 (UINT8 *) SectionName,\r
1027 (UINT8 *) "UpdateType",\r
1028 (UINT8 **) &Value\r
1029 );\r
1030 if (Value == NULL) {\r
1031 FreeAllList (SectionHead, CommentHead);\r
1032 return EFI_NOT_FOUND;\r
1033 }\r
1034\r
1035 Num = UpdateAtoi((UINT8 *) Value);\r
1036 if (( Num >= (UINTN) UpdateOperationMaximum)) {\r
1037 FreeAllList (SectionHead, CommentHead);\r
1038 return Status;\r
1039 }\r
1040 (*UpdateArray)[Index].Index = Index;\r
1041 (*UpdateArray)[Index].UpdateType = (UPDATE_OPERATION_TYPE) Num;\r
1042\r
1043 //\r
1044 // FvBaseAddress\r
1045 //\r
1046 Value = NULL;\r
1047 Status = UpdateGetProfileString (\r
1048 SectionHead,\r
1049 (UINT8 *) SectionName,\r
1050 (UINT8 *) "FvBaseAddress",\r
1051 (UINT8 **) &Value\r
1052 );\r
1053 if (Value == NULL) {\r
1054 FreeAllList (SectionHead, CommentHead);\r
1055 return EFI_NOT_FOUND;\r
1056 }\r
1057\r
1058 Num = AsciiStrHexToUintn ((CONST CHAR8 *) Value);\r
1059 (*UpdateArray)[Index].BaseAddress = (EFI_PHYSICAL_ADDRESS) Num;\r
1060\r
1061 //\r
1062 // FileBuid\r
1063 //\r
1064 Value = NULL;\r
1065 Status = UpdateGetProfileString (\r
1066 SectionHead,\r
1067 (UINT8 *) SectionName,\r
1068 (UINT8 *) "FileGuid",\r
1069 (UINT8 **) &Value\r
1070 );\r
1071 if (Value == NULL) {\r
1072 FreeAllList (SectionHead, CommentHead);\r
1073 return EFI_NOT_FOUND;\r
1074 }\r
1075\r
1076 Status = UpdateStringToGuid ((UINT8 *) Value, &FileGuid);\r
1077 if (EFI_ERROR (Status)) {\r
1078 FreeAllList (SectionHead, CommentHead);\r
1079 return Status;\r
1080 }\r
1081 CopyMem (&((*UpdateArray)[Index].FileGuid), &FileGuid, sizeof(EFI_GUID));\r
1082\r
1083 //\r
1084 // FaultTolerant\r
1085 // Default value is FALSE\r
1086 //\r
1087 Value = NULL;\r
1088 (*UpdateArray)[Index].FaultTolerant = FALSE;\r
1089 Status = UpdateGetProfileString (\r
1090 SectionHead,\r
1091 (UINT8 *) SectionName,\r
1092 (UINT8 *) "FaultTolerant",\r
1093 (UINT8 **) &Value\r
1094 );\r
1095 if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) {\r
1096 FreeAllList (SectionHead, CommentHead);\r
1097 return Status;\r
1098 } else if (Value != NULL) {\r
1099 if (AsciiStriCmp ((CONST CHAR8 *) Value, (CONST CHAR8 *) "TRUE") == 0) {\r
1100 (*UpdateArray)[Index].FaultTolerant = TRUE;\r
1101 } else if (AsciiStriCmp ((CONST CHAR8 *) Value, (CONST CHAR8 *) "FALSE") == 0) {\r
1102 (*UpdateArray)[Index].FaultTolerant = FALSE;\r
1103 }\r
1104 }\r
1105\r
1106 if ((*UpdateArray)[Index].UpdateType == UpdateFvRange) {\r
1107 //\r
1108 // Length\r
1109 //\r
1110 Value = NULL;\r
1111 Status = UpdateGetProfileString (\r
1112 SectionHead,\r
1113 (UINT8 *) SectionName,\r
1114 (UINT8 *) "Length",\r
1115 (UINT8 **) &Value\r
1116 );\r
1117 if (Value == NULL) {\r
1118 FreeAllList (SectionHead, CommentHead);\r
1119 return EFI_NOT_FOUND;\r
1120 }\r
1121\r
1122 Num = AsciiStrHexToUintn ((CONST CHAR8 *) Value);\r
1123 (*UpdateArray)[Index].Length = (UINTN) Num;\r
1124 }\r
1125 }\r
1126\r
1127 //\r
1128 // Now all configuration data got. Free those temporary buffers\r
1129 //\r
1130 FreeAllList (SectionHead, CommentHead);\r
1131\r
1132 return EFI_SUCCESS;\r
1133}\r
1134\r