Debian Codegraph @

util/nvidia/cbootimage/src/parse.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2012-2014, NVIDIA CORPORATION. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * See file CREDITS for list of people who contributed to this
17 * project.
18 */
19
20 /*
21 * parse.c - Parsing support for the cbootimage tool
22 */
23
24 #include <ctype.h>
25 #include "parse.h"
26 #include "cbootimage.h"
27 #include "data_layout.h"
28 #include "crypto.h"
29 #include "set.h"
30
31 /*
32 * Function prototypes
33 *
34 * ParseXXX() parses XXX in the input
35 * SetXXX() sets state based on the parsing results but does not perform
36 * any parsing of its own
37 * A ParseXXX() function may call other parse functions and set functions.
38 * A SetXXX() function may not call any parsing functions.
39 */
40
41 static int
42 set_array(build_image_context *context,
43 uint32_t index,
44 parse_token token,
45 uint32_t value);
46 static char *parse_u32(char *str, uint32_t *val);
47 static char *parse_u8(char *str, uint32_t *val);
48 static char *parse_chipuid(char *str, uint8_t *val);
49 static char *parse_filename(char *str, char *name, int chars_remaining);
50 static char *parse_enum(build_image_context *context,
51 char *str,
52 enum_item *table,
53 uint32_t *val);
54 static char
55 *parse_field_name(char *rest, field_item *field_table, field_item **field);
56 static char
57 *parse_field_value(build_image_context *context,
58 char *rest,
59 field_item *field,
60 uint32_t *value);
61 static int
62 parse_array(build_image_context *context, parse_token token, char *rest);
63 static int
64 parse_bootloader(build_image_context *context, parse_token token, char *rest);
65 static int
66 parse_mts_image(build_image_context *context, parse_token token, char *rest);
67 static int
68 parse_rsa_param(build_image_context *context, parse_token token, char *rest);
69 static int
70 parse_value_u32(build_image_context *context, parse_token token, char *rest);
71 static int
72 parse_value_chipuid(build_image_context *context,
73 parse_token token,
74 char *rest);
75 static int
76 parse_bct_file(build_image_context *context, parse_token token, char *rest);
77 static char
78 *parse_end_state(char *str, char *uname, int chars_remaining);
79 static int
80 parse_dev_param(build_image_context *context, parse_token token, char *rest);
81 static int
82 parse_sdram_param(build_image_context *context, parse_token token, char *rest);
83 static int
84 parse_sign_bl(build_image_context *context, parse_token token, char *rest);
85
86 static int process_statement(build_image_context *context,
87 char *str,
88 uint8_t simple_parse);
89
90 static parse_item parse_simple_items[] =
91 {
92 { "Bctfile=", token_bct_file, parse_bct_file },
93 { "BootLoader=", token_bootloader, parse_bootloader },
94 { "Redundancy=", token_redundancy, parse_value_u32 },
95 { "Bctcopy=", token_bct_copy, parse_value_u32 },
96 { "MtsPreboot=", token_mts_preboot, parse_mts_image},
97 { "Mts=", token_mts, parse_mts_image},
98 { "Version=", token_version, parse_value_u32 },
99 { "PreBctPadBlocks=", token_pre_bct_pad_blocks, parse_value_u32 },
100 { NULL, 0, NULL } /* Must be last */
101 };
102
103 static parse_item s_top_level_items[] = {
104 { "Bctfile=", token_bct_file, parse_bct_file },
105 { "Attribute=", token_attribute, parse_value_u32 },
106 { "Attribute[", token_attribute, parse_array },
107 { "PageSize=", token_page_size, parse_value_u32 },
108 { "BlockSize=", token_block_size, parse_value_u32 },
109 { "PartitionSize=", token_partition_size, parse_value_u32 },
110 { "DevType[", token_dev_type, parse_array },
111 { "DeviceParam[", token_dev_param, parse_dev_param },
112 { "SDRAM[", token_sdram, parse_sdram_param },
113 { "BootLoader=", token_bootloader, parse_bootloader },
114 { "Redundancy=", token_redundancy, parse_value_u32 },
115 { "Bctcopy=", token_bct_copy, parse_value_u32 },
116 { "MtsPreboot=", token_mts_preboot, parse_mts_image},
117 { "Mts=", token_mts, parse_mts_image},
118 { "Version=", token_version, parse_value_u32 },
119 { "OdmData=", token_odm_data, parse_value_u32 },
120 { "ChipUid=", token_unique_chip_id, parse_value_chipuid },
121 { "JtagCtrl=", token_secure_jtag_control, parse_value_u32 },
122 { "DebugCtrl=", token_secure_debug_control, parse_value_u32 },
123 { "RsaKeyModulusFile=", token_rsa_key_modulus, parse_rsa_param },
124 { "RsaPssSigBlFile=", token_rsa_pss_sig_bl, parse_rsa_param },
125 { "RsaPssSigBctFile=", token_rsa_pss_sig_bct, parse_rsa_param },
126 { "RehashBl", token_sign_bl, parse_sign_bl },
127 { NULL, 0, NULL } /* Must be last */
128 };
129
130 /* Macro to simplify parser code a bit. */
131 #define PARSE_COMMA(x) if (*rest != ',') return (x); rest++
132
133 /*
134 * Parse the given string and find the u32 dec/hex number.
135 *
136 * @param str String to parse
137 * @param val Returns value that was parsed
138 * @return the remainder of the string after the number was parsed
139 */
140 static char *
141 parse_u32(char *str, uint32_t *val)
142 {
143 uint32_t value = 0;
144 uint32_t digit;
145
146 while (*str == '0')
147 str++;
148
149 if (tolower(*str) == 'x') {
150 str++;
151 while (isxdigit(*str)) {
152 value *= 16;
153 digit = tolower(*str);
154 value += digit <= '9' ? digit - '0' : digit - 'a' + 10;
155 str++;
156 }
157 } else {
158 while (*str >= '0' && *str <= '9') {
159 value = value*10 + (*str - '0');
160 str++;
161 }
162 }
163 *val = value;
164 return str;
165 }
166
167 /*
168 * Parse the given string and find the u8 dec/hex number.
169 *
170 * @param str String to parse
171 * @param val Returns value that was parsed
172 * @return the remainder of the string after the number was parsed
173 */
174 static char *
175 parse_u8(char *str, uint32_t *val)
176 {
177 char *retval;
178
179 retval = parse_u32(str, val);
180
181 if (*val > 0xff) {
182 printf("Warning: Parsed 8-bit value that exceeded 8-bits.\n");
183 printf(" Parsed value = %d. Remaining text = %s\n",
184 *val, retval);
185 }
186
187 return retval;
188 }
189
190 /*
191 * Parse the given string and transfer to chip uid.
192 *
193 * @param str String to parse
194 * @param chipuid Returns chip uid that was parsed
195 * @return the remainder of the string after the number was parsed
196 */
197 static char *
198 parse_chipuid(char *str, uint8_t *chipuid)
199 {
200 int byte_index = 0;
201 int paddings = 0;
202 char byte_str[3];
203
204 if (*str++ != '0')
205 return NULL;
206
207 if (*str++ != 'x')
208 return NULL;
209
210 paddings = strlen(str) % 2;
211 byte_index = strlen(str) / 2 + paddings;
212
213 if (byte_index > 16)
214 return NULL;
215
216 memset(chipuid, 0, 16);
217
218 while (*str != '\0' && byte_index > 0) {
219 char *endptr;
220
221 strncpy(byte_str, str, 2 - paddings);
222 byte_str[2 - paddings] = '\0';
223 str += 2 - paddings;
224
225 chipuid[byte_index - 1] = strtoul(byte_str, &endptr, 16);
226 if (*endptr)
227 return NULL;
228
229 byte_index--;
230 paddings = 0;
231 }
232
233 return str;
234 }
235
236 /*
237 * Parse the given string and find the file name then
238 * return the rest of the string.
239 *
240 * @param str String to parse
241 * @param name Returns the filename that was parsed
242 * @param chars_remaining The maximum length of filename
243 * @return the remainder of the string after the name was parsed
244 */
245 static char *
246 parse_filename(char *str, char *name, int chars_remaining)
247 {
248 /*
249 * Check if the filename buffer is out of space, preserving one
250 * character to null terminate the string.
251 */
252 while (*str && (isalnum(*str) || strchr("\\/~_-+:.@", *str))) {
253
254 chars_remaining--;
255
256 if (chars_remaining < 1)
257 return NULL;
258 *name++ = *str++;
259 }
260
261 /* Null terminate the filename. */
262 *name = '\0';
263
264 return str;
265 }
266
267 /*
268 * Parse the given string and find the match field name listed
269 * in field table.
270 *
271 * @param rest String to parse
272 * @param field_table The field table to parse
273 * @param field Returns the field item that was parsed
274 * @return NULL or the remainder of the string after the field item was parsed
275 */
276 static char
277 *parse_field_name(char *rest, field_item *field_table, field_item **field)
278 {
279 uint32_t i;
280 uint32_t field_name_len = 0;
281
282 assert(field_table != NULL);
283 assert(rest != NULL);
284 assert(field != NULL);
285
286 while (rest[field_name_len] != '=')
287 field_name_len++;
288
289 /* Parse the field name. */
290 for (i = 0; field_table[i].name != NULL; i++) {
291 if ((strlen(field_table[i].name) == field_name_len) &&
292 !strncmp(field_table[i].name,
293 rest,
294 field_name_len)) {
295
296 *field = &(field_table[i]);
297 rest = rest + field_name_len;
298 return rest;
299 }
300 }
301
302 /* Field wasn't found or a parse error occurred. */
303 return NULL;
304 }
305
306 /*
307 * Parse the value based on the field table
308 *
309 * @param context The main context pointer
310 * @param rest String to parse
311 * @param field Field item to parse
312 * @param value Returns the value that was parsed
313 * @return the remainder of the string after the value was parsed
314 */
315 static char
316 *parse_field_value(build_image_context *context,
317 char *rest,
318 field_item *field,
319 uint32_t *value)
320 {
321 assert(rest != NULL);
322 assert(field != NULL);
323 assert((field->type != field_type_enum)
324 || (field->enum_table != NULL));
325
326 switch (field->type) {
327 case field_type_enum:
328 rest = parse_enum(context, rest, field->enum_table, value);
329 break;
330
331 case field_type_u32:
332 rest = parse_u32(rest, value);
333 break;
334
335 case field_type_u8:
336 rest = parse_u8(rest, value);
337 break;
338
339 default:
340 printf("Unexpected field type %d at line %d\n",
341 field->type, __LINE__);
342 rest = NULL;
343 break;
344 }
345
346 return rest;
347 }
348
349 /*
350 * Parse the given string and find the match enum item listed
351 * in table.
352 *
353 * @param context The main context pointer
354 * @param str String to parse
355 * @param table Enum item table to parse
356 * @param value Returns the value that was parsed
357 * @return the remainder of the string after the item was parsed
358 */
359 static char *
360 parse_enum(build_image_context *context,
361 char *str,
362 enum_item *table,
363 uint32_t *val)
364 {
365 int i;
366 char *rest;
367
368 for (i = 0; table[i].name != NULL; i++) {
369 if (!strncmp(table[i].name, str,
370 strlen(table[i].name))) {
371 *val = table[i].value;
372 rest = str + strlen(table[i].name);
373 return rest;
374 }
375 }
376 return parse_u32(str, val);
377
378 }
379
380 /*
381 * Parse the given string and find the bootloader file name, load address and
382 * entry point information then call set_bootloader function.
383 *
384 * @param context The main context pointer
385 * @param token The parse token value
386 * @param rest String to parse
387 * @return 0 and 1 for success and failure
388 */
389 static int parse_bootloader(build_image_context *context,
390 parse_token token,
391 char *rest)
392 {
393 char filename[MAX_BUFFER];
394 char e_state[MAX_STR_LEN];
395 uint32_t load_addr;
396 uint32_t entry_point;
397
398 assert(context != NULL);
399 assert(rest != NULL);
400
401 if (context->generate_bct != 0)
402 return 0;
403 /* Parse the file name. */
404 rest = parse_filename(rest, filename, MAX_BUFFER);
405 if (rest == NULL)
406 return 1;
407
408 PARSE_COMMA(1);
409
410 /* Parse the load address. */
411 rest = parse_u32(rest, &load_addr);
412 if (rest == NULL)
413 return 1;
414
415 PARSE_COMMA(1);
416
417 /* Parse the entry point. */
418 rest = parse_u32(rest, &entry_point);
419 if (rest == NULL)
420 return 1;
421
422 PARSE_COMMA(1);
423
424 /* Parse the end state. */
425 rest = parse_end_state(rest, e_state, MAX_STR_LEN);
426 if (rest == NULL)
427 return 1;
428 if (strncmp(e_state, "Complete", strlen("Complete")))
429 return 1;
430
431 /* Parsing has finished - set the bootloader */
432 return set_bootloader(context, filename, load_addr, entry_point);
433 }
434
435 /*
436 * Parse the given string and find the MTS file name, load address and
437 * entry point information then call set_mts_image function.
438 *
439 * @param context The main context pointer
440 * @param token The parse token value
441 * @param rest String to parse
442 * @return 0 and 1 for success and failure
443 */
444 static int parse_mts_image(build_image_context *context,
445 parse_token token,
446 char *rest)
447 {
448 char filename[MAX_BUFFER];
449 char e_state[MAX_STR_LEN];
450 uint32_t load_addr;
451 uint32_t entry_point;
452
453 assert(context != NULL);
454 assert(rest != NULL);
455
456 if (context->generate_bct != 0)
457 return 0;
458 /* Parse the file name. */
459 rest = parse_filename(rest, filename, MAX_BUFFER);
460 if (rest == NULL)
461 return 1;
462
463 PARSE_COMMA(1);
464
465 /* Parse the load address. */
466 rest = parse_u32(rest, &load_addr);
467 if (rest == NULL)
468 return 1;
469
470 PARSE_COMMA(1);
471
472 /* Parse the entry point. */
473 rest = parse_u32(rest, &entry_point);
474 if (rest == NULL)
475 return 1;
476
477 PARSE_COMMA(1);
478
479 /* Parse the end state. */
480 rest = parse_end_state(rest, e_state, MAX_STR_LEN);
481 if (rest == NULL)
482 return 1;
483 if (strncmp(e_state, "Complete", strlen("Complete")))
484 return 1;
485
486 /* Parsing has finished - set the bootloader */
487 return set_mts_image(context, filename, load_addr, entry_point);
488 }
489
490 /*
491 * Parse the given rsa modulus/key/signature file name
492 * then call set_rsa_settings to set proper rsa field.
493 *
494 * @param context The main context pointer
495 * @param token The parse token value
496 * @param rest String to parse
497 * @return 0 and 1 for success and failure
498 */
499 static int parse_rsa_param(build_image_context *context,
500 parse_token token,
501 char *rest)
502 {
503 char filename[MAX_BUFFER];
504
505 assert(context != NULL);
506 assert(rest != NULL);
507
508 if (context->generate_bct != 0)
509 return 0;
510
511 /* Parse the file name. */
512 rest = parse_filename(rest, filename, MAX_BUFFER);
513 if (rest == NULL)
514 return 1;
515
516 /* Parsing has finished - set the bootloader */
517 return set_rsa_param(context, token, filename);
518 }
519
520 /*
521 * Parse the given string and find the array items in config file.
522 *
523 * @param context The main context pointer
524 * @param token The parse token value
525 * @param rest String to parse
526 * @return 0 and 1 for success and failure
527 */
528 static int
529 parse_array(build_image_context *context, parse_token token, char *rest)
530 {
531 uint32_t index;
532 uint32_t value;
533
534 assert(context != NULL);
535 assert(rest != NULL);
536
537 /* Parse the index. */
538 rest = parse_u32(rest, &index);
539 if (rest == NULL)
540 return 1;
541
542 /* Parse the closing bracket. */
543 if (*rest != ']')
544 return 1;
545 rest++;
546
547 /* Parse the equals sign.*/
548 if (*rest != '=')
549 return 1;
550 rest++;
551
552 /* Parse the value based on the field table. */
553 switch (token) {
554 case token_attribute:
555 rest = parse_u32(rest, &value);
556 break;
557 case token_dev_type:
558 rest = parse_enum(context,
559 rest,
560 g_soc_config->devtype_table,
561 &value);
562 break;
563
564 default:
565 /* Unknown token */
566 return 1;
567 }
568
569 if (rest == NULL)
570 return 1;
571
572 /* Store the result. */
573 return set_array(context, index, token, value);
574 }
575
576 /*
577 * Call hw interface to set the value for array item in bct such as device
578 * type and bootloader attribute.
579 *
580 * @param context The main context pointer
581 * @param index The index for array
582 * @param token The parse token value
583 * @param value The value to set
584 * @return 0 and -ENODATA for success and failure
585 */
586
587 static int
588 set_array(build_image_context *context,
589 uint32_t index,
590 parse_token token,
591 uint32_t value)
592 {
593 int err = 0;
594
595 assert(context != NULL);
596
597 switch (token) {
598 case token_attribute:
599 err = g_soc_config->setbl_param(index,
600 token_bl_attribute,
601 &value,
602 context->bct);
603 break;
604 case token_dev_type:
605 err = g_soc_config->set_dev_param(context,
606 index,
607 token_dev_type,
608 value);
609 break;
610 default:
611 break;
612 }
613 return err;
614 }
615
616 /*
617 * General handler for setting uint32_t values in config files.
618 *
619 * @param context The main context pointer
620 * @param token The parse token value
621 * @param rest String to parse
622 * @return 0 and 1 for success and failure
623 */
624 static int parse_value_u32(build_image_context *context,
625 parse_token token,
626 char *rest)
627 {
628 uint32_t value;
629
630 assert(context != NULL);
631 assert(rest != NULL);
632
633 rest = parse_u32(rest, &value);
634 if (rest == NULL)
635 return 1;
636
637 return context_set_value(context, token, &value);
638 }
639
640 /*
641 * General handler for setting chip uid in config files.
642 *
643 * @param context The main context pointer
644 * @param token The parse token value
645 * @param rest String to parse
646 * @return 0 and 1 for success and failure
647 */
648 static int parse_value_chipuid(build_image_context *context,
649 parse_token token,
650 char *rest)
651 {
652 uint8_t value[16];
653
654 assert(context != NULL);
655 assert(rest != NULL);
656
657 rest = parse_chipuid(rest, value);
658 if (rest == NULL)
659 return 1;
660
661 return context_set_value(context, token, value);
662 }
663
664 /*
665 * Parse the given string and find the bct file name.
666 *
667 * @param context The main context pointer
668 * @param token The parse token value
669 * @param rest String to parse
670 * @return 0 and 1 for success and failure
671 */
672 static int
673 parse_bct_file(build_image_context *context, parse_token token, char *rest)
674 {
675 char filename[MAX_BUFFER];
676
677 assert(context != NULL);
678 assert(rest != NULL);
679
680 /* Parse the file name. */
681 rest = parse_filename(rest, filename, MAX_BUFFER);
682 if (rest == NULL)
683 return 1;
684
685 /* Parsing has finished - set the bctfile */
686 context->bct_filename = filename;
687 /* Read the bct file to buffer */
688 if (read_bct_file(context))
689 return 1;
690
691 update_context(context);
692 return 0;
693 }
694
695 static int
696 parse_sign_bl(build_image_context *context, parse_token token, char *rest)
697 {
698 return resign_bl(context);
699 }
700
701 static char *
702 parse_end_state(char *str, char *uname, int chars_remaining)
703 {
704 while (isalpha(*str)) {
705
706 *uname++ = *str++;
707 if (--chars_remaining < 0)
708 return NULL;
709 }
710 *uname = '\0';
711 return str;
712 }
713
714 /*
715 * Parse the given string and find device parameter listed in device table
716 * and value for this device parameter. If match, call the corresponding
717 * function in the table to set device parameter.
718 *
719 * @param context The main context pointer
720 * @param token The parse token value
721 * @param rest String to parse
722 * @return 0 and 1 for success and failure
723 */
724 static int
725 parse_dev_param(build_image_context *context, parse_token token, char *rest)
726 {
727 uint32_t i;
728 uint32_t value;
729 field_item *field;
730 uint32_t index;
731 parse_subfield_item *device_item = NULL;
732
733 assert(context != NULL);
734 assert(rest != NULL);
735
736 /* Parse the index. */
737 rest = parse_u32(rest, &index);
738 if (rest == NULL)
739 return 1;
740
741 /* Parse the closing bracket. */
742 if (*rest != ']')
743 return 1;
744 rest++;
745
746 /* Parse the following '.' */
747 if (*rest != '.')
748 return 1;
749 rest++;
750
751 /* Parse the device name. */
752 for (i = 0; g_soc_config->device_type_table[i].prefix != NULL; i++) {
753 if (!strncmp(g_soc_config->device_type_table[i].prefix,
754 rest, strlen(g_soc_config->device_type_table[i].prefix))) {
755
756 device_item = &(g_soc_config->device_type_table[i]);
757 rest = rest + strlen(g_soc_config->device_type_table[i].prefix);
758
759 /* Parse the field name. */
760 rest = parse_field_name(rest,
761 g_soc_config->device_type_table[i].field_table,
762 &field);
763 if (rest == NULL)
764 return 1;
765
766 /* Parse the equals sign.*/
767 if (*rest != '=')
768 return 1;
769 rest++;
770
771 /* Parse the value based on the field table. */
772 rest = parse_field_value(context, rest, field, &value);
773 if (rest == NULL)
774 return 1;
775 return device_item->process(context,
776 index, field->token, value);
777 }
778 }
779 return 1;
780 }
781
782 /*
783 * Parse the given string and find sdram parameter and value in config
784 * file. If match, call the corresponding function set the sdram parameter.
785 *
786 * @param context The main context pointer
787 * @param token The parse token value
788 * @param rest String to parse
789 * @return 0 and 1 for success and failure
790 */
791 static int
792 parse_sdram_param(build_image_context *context, parse_token token, char *rest)
793 {
794 uint32_t value;
795 field_item *field;
796 uint32_t index;
797
798 assert(context != NULL);
799 assert(rest != NULL);
800
801 /* Parse the index. */
802 rest = parse_u32(rest, &index);
803 if (rest == NULL)
804 return 1;
805
806 /* Parse the closing bracket. */
807 if (*rest != ']')
808 return 1;
809 rest++;
810
811 /* Parse the following '.' */
812 if (*rest != '.')
813 return 1;
814 rest++;
815
816 /* Parse the field name. */
817 rest = parse_field_name(rest, g_soc_config->sdram_field_table, &field);
818
819 if (rest == NULL)
820 return 1;
821
822 /* Parse the equals sign.*/
823 if (*rest != '=')
824 return 1;
825 rest++;
826
827 /* Parse the value based on the field table. */
828 rest = parse_field_value(context, rest, field, &value);
829 if (rest == NULL)
830 return 1;
831
832 /* Store the result. */
833 return g_soc_config->set_sdram_param(context,
834 index,
835 field->token,
836 value);
837 }
838
839 /*
840 * Compare the given string with item listed in table.
841 * Execute the proper process function if match.
842 *
843 * @param context The main context pointer
844 * @param str String to parse
845 * @param simple_parse Simple parse flag
846 * @return 0 and 1 for success and failure
847 */
848 static int
849 process_statement(build_image_context *context,
850 char *str,
851 uint8_t simple_parse)
852 {
853 int i;
854 char *rest;
855 parse_item *cfg_parse_item;
856
857 if (simple_parse == 0)
858 cfg_parse_item = s_top_level_items;
859 else
860 cfg_parse_item = parse_simple_items;
861
862 for (i = 0; cfg_parse_item[i].prefix != NULL; i++) {
863 if (!strncmp(cfg_parse_item[i].prefix, str,
864 strlen(cfg_parse_item[i].prefix))) {
865 rest = str + strlen(cfg_parse_item[i].prefix);
866
867 return cfg_parse_item[i].process(context,
868 cfg_parse_item[i].token,
869 rest);
870 }
871 }
872
873 /* If this point was reached, there was a processing error. */
874 return 1;
875 }
876
877 /*
878 * The main function parse the config file.
879 *
880 * @param context The main context pointer
881 * @param simple_parse Simple parse flag
882 */
883 void process_config_file(build_image_context *context, uint8_t simple_parse)
884 {
885 char buffer[MAX_BUFFER];
886 int space = 0;
887 int current;
888 uint8_t c_eol_comment_start = 0; /* True after first slash */
889 uint8_t comment = 0;
890 uint8_t string = 0;
891 uint8_t equal_encounter = 0;
892
893 assert(context != NULL);
894 assert(context->config_file != NULL);
895
896 while ((current = fgetc(context->config_file)) != EOF) {
897 if (space >= (MAX_BUFFER-1)) {
898 /* if we exceeded the max buffer size, it is likely
899 due to a missing semi-colon at the end of a line */
900 printf("Config file parsing error!");
901 exit(1);
902 }
903
904 /* Handle failure to complete "//" comment token.
905 Insert the '/' into the busffer and proceed with
906 processing the current character. */
907 if (c_eol_comment_start && current != '/') {
908 c_eol_comment_start = 0;
909 buffer[space++] = '/';
910 }
911
912 switch (current) {
913 case '\"': /* " indicates start or end of a string */
914 if (!comment) {
915 string ^= 1;
916 buffer[space++] = current;
917 }
918 break;
919 case ';':
920 if (!string && !comment) {
921 buffer[space++] = '\0';
922
923 if (process_statement(context,
924 buffer,
925 simple_parse))
926 goto error;
927 space = 0;
928 equal_encounter = 0;
929 } else if (string)
930 buffer[space++] = current;
931 break;
932
933 case '/':
934 if (!string && !comment) {
935 if (c_eol_comment_start) {
936 /* EOL comment started. */
937 comment = 1;
938 c_eol_comment_start = 0;
939 } else {
940 /* Potential start of eol comment. */
941 c_eol_comment_start = 1;
942 }
943 } else if (!comment)
944 buffer[space++] = current;
945 break;
946
947 /* ignore whitespace. uses fallthrough */
948 case '\n':
949 case '\r': /* carriage returns end comments */
950 string = 0;
951 comment = 0;
952 c_eol_comment_start = 0;
953 case ' ':
954 case '\t':
955 if (string)
956 buffer[space++] = current;
957 break;
958
959 case '#':
960 if (!string)
961 comment = 1;
962 else
963 buffer[space++] = current;
964 break;
965
966 default:
967 if (!comment) {
968 buffer[space++] = current;
969 if (current == '=') {
970 if (!equal_encounter)
971 equal_encounter = 1;
972 else
973 goto error;
974 }
975 }
976 break;
977 }
978 }
979
980 return;
981
982 error:
983 printf("Error parsing: %s\n", buffer);
984 exit(1);
985 }
986
987 int bct_get_unsupported(parse_token id)
988 {
989 return -ENODATA;
990 }