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