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