]>
Commit | Line | Data |
---|---|---|
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 | |
c5ad1f86 | 6 | Copyright (c) 2002 - 2015, Intel Corporation. All rights reserved.<BR>\r |
b2824a8e | 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 | |
34 | EFI_STATUS\r | |
35 | ProfileGetLine (\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 | |
95 | VOID\r | |
96 | ProfileTrim (\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 | |
179 | EFI_STATUS\r | |
180 | ProfileGetComments (\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 | |
222 | EFI_STATUS\r | |
223 | ProfileGetSection (\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 | |
308 | EFI_STATUS\r | |
309 | ProfileGetEntry (\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 | |
436 | VOID\r | |
437 | FreeAllList (\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 | |
484 | EFI_STATUS\r | |
485 | UpdateGetProfileString (\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 | |
522 | UINTN\r | |
523 | UpdateAtoi (\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 | |
577 | UINTN\r | |
578 | UpdateValueToString (\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 | |
632 | VOID\r | |
633 | UpdateStrCatNumber (\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 | |
662 | EFI_STATUS\r | |
663 | UpdateStringToGuid (\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 | |
c5ad1f86 | 677 | Buffer = AllocateCopyPool (StrLen + 1, Str);\r |
b2824a8e | 678 | if (Buffer == NULL) {\r |
679 | return EFI_OUT_OF_RESOURCES;\r | |
680 | }\r | |
b2824a8e | 681 | \r |
682 | //\r | |
683 | // Data1\r | |
684 | //\r | |
685 | PtrBuffer = Buffer;\r | |
686 | PtrPosition = PtrBuffer;\r | |
687 | while (*PtrBuffer != '\0') {\r | |
688 | if (*PtrBuffer == '-') {\r | |
689 | break;\r | |
690 | }\r | |
691 | PtrBuffer++;\r | |
692 | }\r | |
693 | if (*PtrBuffer == '\0') {\r | |
694 | FreePool (Buffer);\r | |
695 | return EFI_NOT_FOUND;\r | |
696 | }\r | |
697 | \r | |
698 | *PtrBuffer = '\0';\r | |
699 | Data = AsciiStrHexToUintn ((CONST CHAR8 *) PtrPosition);\r | |
700 | Guid->Data1 = (UINT32)Data;\r | |
701 | \r | |
702 | //\r | |
703 | // Data2\r | |
704 | //\r | |
705 | PtrBuffer++;\r | |
706 | PtrPosition = PtrBuffer;\r | |
707 | while (*PtrBuffer != '\0') {\r | |
708 | if (*PtrBuffer == '-') {\r | |
709 | break;\r | |
710 | }\r | |
711 | PtrBuffer++;\r | |
712 | }\r | |
713 | if (*PtrBuffer == '\0') {\r | |
714 | FreePool (Buffer);\r | |
715 | return EFI_NOT_FOUND;\r | |
716 | }\r | |
717 | *PtrBuffer = '\0';\r | |
718 | Data = AsciiStrHexToUintn ((CONST CHAR8 *) PtrPosition);\r | |
719 | Guid->Data2 = (UINT16)Data;\r | |
720 | \r | |
721 | //\r | |
722 | // Data3\r | |
723 | //\r | |
724 | PtrBuffer++;\r | |
725 | PtrPosition = PtrBuffer;\r | |
726 | while (*PtrBuffer != '\0') {\r | |
727 | if (*PtrBuffer == '-') {\r | |
728 | break;\r | |
729 | }\r | |
730 | PtrBuffer++;\r | |
731 | }\r | |
732 | if (*PtrBuffer == '\0') {\r | |
733 | FreePool (Buffer);\r | |
734 | return EFI_NOT_FOUND;\r | |
735 | }\r | |
736 | *PtrBuffer = '\0';\r | |
737 | Data = AsciiStrHexToUintn ((CONST CHAR8 *) PtrPosition);\r | |
738 | Guid->Data3 = (UINT16)Data;\r | |
739 | \r | |
740 | //\r | |
741 | // Data4[0..1]\r | |
742 | //\r | |
743 | for ( Index = 0 ; Index < 2 ; Index++) {\r | |
744 | PtrBuffer++;\r | |
745 | if ((*PtrBuffer == '\0') || ( *(PtrBuffer + 1) == '\0')) {\r | |
746 | FreePool (Buffer);\r | |
747 | return EFI_NOT_FOUND;\r | |
748 | }\r | |
749 | Digits[0] = *PtrBuffer;\r | |
750 | PtrBuffer++;\r | |
751 | Digits[1] = *PtrBuffer;\r | |
752 | Digits[2] = '\0';\r | |
753 | Data = AsciiStrHexToUintn ((CONST CHAR8 *) Digits);\r | |
754 | Guid->Data4[Index] = (UINT8)Data;\r | |
755 | }\r | |
756 | \r | |
757 | //\r | |
758 | // skip the '-'\r | |
759 | //\r | |
760 | PtrBuffer++;\r | |
761 | if ((*PtrBuffer != '-' ) || ( *PtrBuffer == '\0')) {\r | |
762 | return EFI_NOT_FOUND;\r | |
763 | }\r | |
764 | \r | |
765 | //\r | |
766 | // Data4[2..7]\r | |
767 | //\r | |
768 | for ( ; Index < 8; Index++) {\r | |
769 | PtrBuffer++;\r | |
770 | if ((*PtrBuffer == '\0') || ( *(PtrBuffer + 1) == '\0')) {\r | |
771 | FreePool (Buffer);\r | |
772 | return EFI_NOT_FOUND;\r | |
773 | }\r | |
774 | Digits[0] = *PtrBuffer;\r | |
775 | PtrBuffer++;\r | |
776 | Digits[1] = *PtrBuffer;\r | |
777 | Digits[2] = '\0';\r | |
778 | Data = AsciiStrHexToUintn ((CONST CHAR8 *) Digits);\r | |
779 | Guid->Data4[Index] = (UINT8)Data;\r | |
780 | }\r | |
781 | \r | |
782 | FreePool (Buffer);\r | |
783 | \r | |
784 | return EFI_SUCCESS;\r | |
785 | }\r | |
786 | \r | |
787 | /**\r | |
788 | Pre process config data buffer into Section entry list and Comment entry list.\r | |
789 | \r | |
790 | @param DataBuffer Config raw file buffer.\r | |
791 | @param BufferSize Size of raw buffer.\r | |
792 | @param SectionHead Pointer to the section entry list.\r | |
793 | @param CommentHead Pointer to the comment entry list.\r | |
794 | \r | |
795 | @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.\r | |
796 | @retval EFI_SUCCESS Config data buffer is preprocessed.\r | |
797 | \r | |
798 | **/\r | |
799 | EFI_STATUS\r | |
800 | PreProcessDataFile (\r | |
801 | IN UINT8 *DataBuffer,\r | |
802 | IN UINTN BufferSize,\r | |
803 | IN OUT SECTION_ITEM **SectionHead,\r | |
804 | IN OUT COMMENT_LINE **CommentHead\r | |
805 | )\r | |
806 | {\r | |
807 | EFI_STATUS Status;\r | |
808 | CHAR8 *Source;\r | |
809 | CHAR8 *CurrentPtr;\r | |
810 | CHAR8 *BufferEnd;\r | |
811 | CHAR8 *PtrLine;\r | |
812 | UINTN LineLength;\r | |
813 | UINTN SourceLength;\r | |
814 | UINTN MaxLineLength;\r | |
815 | \r | |
816 | *SectionHead = NULL;\r | |
817 | *CommentHead = NULL;\r | |
818 | BufferEnd = (CHAR8 *) ( (UINTN) DataBuffer + BufferSize);\r | |
819 | CurrentPtr = (CHAR8 *) DataBuffer;\r | |
820 | MaxLineLength = MAX_LINE_LENGTH;\r | |
821 | Status = EFI_SUCCESS;\r | |
822 | \r | |
823 | PtrLine = AllocatePool (MaxLineLength);\r | |
824 | if (PtrLine == NULL) {\r | |
825 | return EFI_OUT_OF_RESOURCES;\r | |
826 | }\r | |
827 | \r | |
828 | while (CurrentPtr < BufferEnd) {\r | |
829 | Source = CurrentPtr;\r | |
830 | SourceLength = (UINTN)BufferEnd - (UINTN)CurrentPtr;\r | |
831 | LineLength = MaxLineLength;\r | |
832 | //\r | |
833 | // With the assumption that line length is less than 512\r | |
834 | // characters. Otherwise BUFFER_TOO_SMALL will be returned.\r | |
835 | //\r | |
836 | Status = ProfileGetLine (\r | |
837 | (UINT8 *) Source,\r | |
838 | SourceLength,\r | |
839 | (UINT8 *) PtrLine,\r | |
840 | &LineLength\r | |
841 | );\r | |
842 | if (EFI_ERROR (Status)) {\r | |
843 | if (Status == EFI_BUFFER_TOO_SMALL) {\r | |
844 | //\r | |
845 | // If buffer too small, re-allocate the buffer according\r | |
846 | // to the returned LineLength and try again.\r | |
847 | //\r | |
848 | FreePool (PtrLine);\r | |
849 | PtrLine = NULL;\r | |
850 | PtrLine = AllocatePool (LineLength);\r | |
851 | if (PtrLine == NULL) {\r | |
852 | Status = EFI_OUT_OF_RESOURCES;\r | |
853 | break;\r | |
854 | }\r | |
855 | SourceLength = LineLength;\r | |
856 | Status = ProfileGetLine (\r | |
857 | (UINT8 *) Source,\r | |
858 | SourceLength,\r | |
859 | (UINT8 *) PtrLine,\r | |
860 | &LineLength\r | |
861 | );\r | |
862 | if (EFI_ERROR (Status)) {\r | |
863 | break;\r | |
864 | }\r | |
865 | MaxLineLength = LineLength;\r | |
866 | } else {\r | |
867 | break;\r | |
868 | }\r | |
869 | }\r | |
870 | CurrentPtr = (CHAR8 *) ( (UINTN) CurrentPtr + LineLength);\r | |
871 | \r | |
872 | //\r | |
873 | // Line got. Trim the line before processing it.\r | |
874 | //\r | |
875 | ProfileTrim (\r | |
876 | (UINT8 *) PtrLine,\r | |
877 | &LineLength\r | |
878 | );\r | |
879 | \r | |
880 | //\r | |
881 | // Blank line\r | |
882 | //\r | |
883 | if (LineLength == 0) {\r | |
884 | continue;\r | |
885 | }\r | |
886 | \r | |
887 | if (PtrLine[0] == '#') {\r | |
888 | Status = ProfileGetComments (\r | |
889 | (UINT8 *) PtrLine,\r | |
890 | LineLength,\r | |
891 | CommentHead\r | |
892 | );\r | |
893 | } else if (PtrLine[0] == '[') {\r | |
894 | Status = ProfileGetSection (\r | |
895 | (UINT8 *) PtrLine,\r | |
896 | LineLength,\r | |
897 | SectionHead\r | |
898 | );\r | |
899 | } else {\r | |
900 | Status = ProfileGetEntry (\r | |
901 | (UINT8 *) PtrLine,\r | |
902 | LineLength,\r | |
903 | SectionHead\r | |
904 | );\r | |
905 | }\r | |
906 | \r | |
907 | if (EFI_ERROR (Status)) {\r | |
908 | break;\r | |
909 | }\r | |
910 | }\r | |
911 | \r | |
912 | //\r | |
913 | // Free buffer\r | |
914 | //\r | |
915 | FreePool (PtrLine);\r | |
916 | \r | |
917 | return Status;\r | |
918 | }\r | |
919 | \r | |
920 | /**\r | |
921 | Parse Config data file to get the updated data array.\r | |
922 | \r | |
923 | @param DataBuffer Config raw file buffer.\r | |
924 | @param BufferSize Size of raw buffer.\r | |
925 | @param NumOfUpdates Pointer to the number of update data.\r | |
926 | @param UpdateArray Pointer to the config of update data.\r | |
927 | \r | |
928 | @retval EFI_NOT_FOUND No config data is found.\r | |
929 | @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.\r | |
930 | @retval EFI_SUCCESS Parse the config file successfully.\r | |
931 | \r | |
932 | **/\r | |
933 | EFI_STATUS\r | |
934 | ParseUpdateDataFile (\r | |
935 | IN UINT8 *DataBuffer,\r | |
936 | IN UINTN BufferSize,\r | |
937 | IN OUT UINTN *NumOfUpdates,\r | |
938 | IN OUT UPDATE_CONFIG_DATA **UpdateArray\r | |
939 | )\r | |
940 | {\r | |
941 | EFI_STATUS Status;\r | |
942 | CHAR8 *Value;\r | |
943 | CHAR8 *SectionName;\r | |
944 | CHAR8 Entry[MAX_LINE_LENGTH];\r | |
945 | SECTION_ITEM *SectionHead;\r | |
946 | COMMENT_LINE *CommentHead;\r | |
947 | UINTN Num;\r | |
948 | UINTN Index;\r | |
949 | EFI_GUID FileGuid;\r | |
950 | \r | |
951 | SectionHead = NULL;\r | |
952 | CommentHead = NULL;\r | |
953 | \r | |
954 | //\r | |
955 | // First process the data buffer and get all sections and entries\r | |
956 | //\r | |
957 | Status = PreProcessDataFile (\r | |
958 | DataBuffer,\r | |
959 | BufferSize,\r | |
960 | &SectionHead,\r | |
961 | &CommentHead\r | |
962 | );\r | |
963 | if (EFI_ERROR (Status)) {\r | |
964 | FreeAllList (SectionHead, CommentHead);\r | |
965 | return Status;\r | |
966 | }\r | |
967 | \r | |
968 | //\r | |
969 | // Now get NumOfUpdate\r | |
970 | //\r | |
971 | Value = NULL;\r | |
972 | Status = UpdateGetProfileString (\r | |
973 | SectionHead,\r | |
974 | (UINT8 *) "Head",\r | |
975 | (UINT8 *) "NumOfUpdate",\r | |
976 | (UINT8 **) &Value\r | |
977 | );\r | |
978 | if (Value == NULL) {\r | |
979 | FreeAllList (SectionHead, CommentHead);\r | |
980 | return EFI_NOT_FOUND;\r | |
981 | }\r | |
982 | Num = UpdateAtoi((UINT8 *) Value);\r | |
983 | if (Num <= 0) {\r | |
984 | FreeAllList (SectionHead, CommentHead);\r | |
985 | return EFI_NOT_FOUND;\r | |
986 | }\r | |
987 | \r | |
988 | *NumOfUpdates = Num;\r | |
989 | *UpdateArray = AllocatePool ((sizeof (UPDATE_CONFIG_DATA) * Num));\r | |
990 | if (*UpdateArray == NULL) {\r | |
991 | FreeAllList (SectionHead, CommentHead);\r | |
992 | return EFI_OUT_OF_RESOURCES;\r | |
993 | }\r | |
994 | \r | |
995 | for ( Index = 0 ; Index < *NumOfUpdates ; Index++) {\r | |
996 | //\r | |
997 | // Get the section name of each update\r | |
998 | //\r | |
c5ad1f86 | 999 | AsciiStrCpyS (Entry, MAX_LINE_LENGTH, "Update");\r |
b2824a8e | 1000 | UpdateStrCatNumber ((UINT8 *) Entry, Index);\r |
1001 | Value = NULL;\r | |
1002 | Status = UpdateGetProfileString (\r | |
1003 | SectionHead,\r | |
1004 | (UINT8 *) "Head",\r | |
1005 | (UINT8 *) Entry,\r | |
1006 | (UINT8 **) &Value\r | |
1007 | );\r | |
1008 | if (Value == NULL) {\r | |
1009 | FreeAllList (SectionHead, CommentHead);\r | |
1010 | return EFI_NOT_FOUND;\r | |
1011 | }\r | |
1012 | \r | |
1013 | //\r | |
1014 | // The section name of this update has been found.\r | |
1015 | // Now looks for all the config data of this update\r | |
1016 | //\r | |
1017 | SectionName = Value;\r | |
1018 | \r | |
1019 | //\r | |
1020 | // UpdateType\r | |
1021 | //\r | |
1022 | Value = NULL;\r | |
1023 | Status = UpdateGetProfileString (\r | |
1024 | SectionHead,\r | |
1025 | (UINT8 *) SectionName,\r | |
1026 | (UINT8 *) "UpdateType",\r | |
1027 | (UINT8 **) &Value\r | |
1028 | );\r | |
1029 | if (Value == NULL) {\r | |
1030 | FreeAllList (SectionHead, CommentHead);\r | |
1031 | return EFI_NOT_FOUND;\r | |
1032 | }\r | |
1033 | \r | |
1034 | Num = UpdateAtoi((UINT8 *) Value);\r | |
1035 | if (( Num >= (UINTN) UpdateOperationMaximum)) {\r | |
1036 | FreeAllList (SectionHead, CommentHead);\r | |
1037 | return Status;\r | |
1038 | }\r | |
1039 | (*UpdateArray)[Index].Index = Index;\r | |
1040 | (*UpdateArray)[Index].UpdateType = (UPDATE_OPERATION_TYPE) Num;\r | |
1041 | \r | |
1042 | //\r | |
1043 | // FvBaseAddress\r | |
1044 | //\r | |
1045 | Value = NULL;\r | |
1046 | Status = UpdateGetProfileString (\r | |
1047 | SectionHead,\r | |
1048 | (UINT8 *) SectionName,\r | |
1049 | (UINT8 *) "FvBaseAddress",\r | |
1050 | (UINT8 **) &Value\r | |
1051 | );\r | |
1052 | if (Value == NULL) {\r | |
1053 | FreeAllList (SectionHead, CommentHead);\r | |
1054 | return EFI_NOT_FOUND;\r | |
1055 | }\r | |
1056 | \r | |
1057 | Num = AsciiStrHexToUintn ((CONST CHAR8 *) Value);\r | |
1058 | (*UpdateArray)[Index].BaseAddress = (EFI_PHYSICAL_ADDRESS) Num;\r | |
1059 | \r | |
1060 | //\r | |
1061 | // FileBuid\r | |
1062 | //\r | |
1063 | Value = NULL;\r | |
1064 | Status = UpdateGetProfileString (\r | |
1065 | SectionHead,\r | |
1066 | (UINT8 *) SectionName,\r | |
1067 | (UINT8 *) "FileGuid",\r | |
1068 | (UINT8 **) &Value\r | |
1069 | );\r | |
1070 | if (Value == NULL) {\r | |
1071 | FreeAllList (SectionHead, CommentHead);\r | |
1072 | return EFI_NOT_FOUND;\r | |
1073 | }\r | |
1074 | \r | |
1075 | Status = UpdateStringToGuid ((UINT8 *) Value, &FileGuid);\r | |
1076 | if (EFI_ERROR (Status)) {\r | |
1077 | FreeAllList (SectionHead, CommentHead);\r | |
1078 | return Status;\r | |
1079 | }\r | |
1080 | CopyMem (&((*UpdateArray)[Index].FileGuid), &FileGuid, sizeof(EFI_GUID));\r | |
1081 | \r | |
1082 | //\r | |
1083 | // FaultTolerant\r | |
1084 | // Default value is FALSE\r | |
1085 | //\r | |
1086 | Value = NULL;\r | |
1087 | (*UpdateArray)[Index].FaultTolerant = FALSE;\r | |
1088 | Status = UpdateGetProfileString (\r | |
1089 | SectionHead,\r | |
1090 | (UINT8 *) SectionName,\r | |
1091 | (UINT8 *) "FaultTolerant",\r | |
1092 | (UINT8 **) &Value\r | |
1093 | );\r | |
1094 | if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) {\r | |
1095 | FreeAllList (SectionHead, CommentHead);\r | |
1096 | return Status;\r | |
1097 | } else if (Value != NULL) {\r | |
1098 | if (AsciiStriCmp ((CONST CHAR8 *) Value, (CONST CHAR8 *) "TRUE") == 0) {\r | |
1099 | (*UpdateArray)[Index].FaultTolerant = TRUE;\r | |
1100 | } else if (AsciiStriCmp ((CONST CHAR8 *) Value, (CONST CHAR8 *) "FALSE") == 0) {\r | |
1101 | (*UpdateArray)[Index].FaultTolerant = FALSE;\r | |
1102 | }\r | |
1103 | }\r | |
1104 | \r | |
1105 | if ((*UpdateArray)[Index].UpdateType == UpdateFvRange) {\r | |
1106 | //\r | |
1107 | // Length\r | |
1108 | //\r | |
1109 | Value = NULL;\r | |
1110 | Status = UpdateGetProfileString (\r | |
1111 | SectionHead,\r | |
1112 | (UINT8 *) SectionName,\r | |
1113 | (UINT8 *) "Length",\r | |
1114 | (UINT8 **) &Value\r | |
1115 | );\r | |
1116 | if (Value == NULL) {\r | |
1117 | FreeAllList (SectionHead, CommentHead);\r | |
1118 | return EFI_NOT_FOUND;\r | |
1119 | }\r | |
1120 | \r | |
1121 | Num = AsciiStrHexToUintn ((CONST CHAR8 *) Value);\r | |
1122 | (*UpdateArray)[Index].Length = (UINTN) Num;\r | |
1123 | }\r | |
1124 | }\r | |
1125 | \r | |
1126 | //\r | |
1127 | // Now all configuration data got. Free those temporary buffers\r | |
1128 | //\r | |
1129 | FreeAllList (SectionHead, CommentHead);\r | |
1130 | \r | |
1131 | return EFI_SUCCESS;\r | |
1132 | }\r | |
1133 | \r |