| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 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 } |
| 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 } |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
#define PARSE_COMMA(x) if (*rest != ',') return (x); rest++ |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 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 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 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 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 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 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
static char * |
| 246 |
parse_filename(char *str, char *name, int chars_remaining) |
| 247 |
{ |
| 248 |
|
| 249 |
|
| 250 |
|
| 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 |
|
| 262 |
*name = '\0'; |
| 263 |
|
| 264 |
return str; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 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 |
|
| 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 |
|
| 303 |
return NULL; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 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 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 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 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 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 |
|
| 404 |
rest = parse_filename(rest, filename, MAX_BUFFER); |
| 405 |
if (rest == NULL) |
| 406 |
return 1; |
| 407 |
|
| 408 |
PARSE_COMMA(1); |
| 409 |
|
| 410 |
|
| 411 |
rest = parse_u32(rest, &load_addr); |
| 412 |
if (rest == NULL) |
| 413 |
return 1; |
| 414 |
|
| 415 |
PARSE_COMMA(1); |
| 416 |
|
| 417 |
|
| 418 |
rest = parse_u32(rest, &entry_point); |
| 419 |
if (rest == NULL) |
| 420 |
return 1; |
| 421 |
|
| 422 |
PARSE_COMMA(1); |
| 423 |
|
| 424 |
|
| 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 |
|
| 432 |
return set_bootloader(context, filename, load_addr, entry_point); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 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 |
|
| 459 |
rest = parse_filename(rest, filename, MAX_BUFFER); |
| 460 |
if (rest == NULL) |
| 461 |
return 1; |
| 462 |
|
| 463 |
PARSE_COMMA(1); |
| 464 |
|
| 465 |
|
| 466 |
rest = parse_u32(rest, &load_addr); |
| 467 |
if (rest == NULL) |
| 468 |
return 1; |
| 469 |
|
| 470 |
PARSE_COMMA(1); |
| 471 |
|
| 472 |
|
| 473 |
rest = parse_u32(rest, &entry_point); |
| 474 |
if (rest == NULL) |
| 475 |
return 1; |
| 476 |
|
| 477 |
PARSE_COMMA(1); |
| 478 |
|
| 479 |
|
| 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 |
|
| 487 |
return set_mts_image(context, filename, load_addr, entry_point); |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 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 |
|
| 512 |
rest = parse_filename(rest, filename, MAX_BUFFER); |
| 513 |
if (rest == NULL) |
| 514 |
return 1; |
| 515 |
|
| 516 |
|
| 517 |
return set_rsa_param(context, token, filename); |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 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 |
|
| 538 |
rest = parse_u32(rest, &index); |
| 539 |
if (rest == NULL) |
| 540 |
return 1; |
| 541 |
|
| 542 |
|
| 543 |
if (*rest != ']') |
| 544 |
return 1; |
| 545 |
rest++; |
| 546 |
|
| 547 |
|
| 548 |
if (*rest != '=') |
| 549 |
return 1; |
| 550 |
rest++; |
| 551 |
|
| 552 |
|
| 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 |
|
| 566 |
return 1; |
| 567 |
} |
| 568 |
|
| 569 |
if (rest == NULL) |
| 570 |
return 1; |
| 571 |
|
| 572 |
|
| 573 |
return set_array(context, index, token, value); |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 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 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 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 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 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 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 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 |
|
| 681 |
rest = parse_filename(rest, filename, MAX_BUFFER); |
| 682 |
if (rest == NULL) |
| 683 |
return 1; |
| 684 |
|
| 685 |
|
| 686 |
context->bct_filename = filename; |
| 687 |
|
| 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 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 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 |
|
| 737 |
rest = parse_u32(rest, &index); |
| 738 |
if (rest == NULL) |
| 739 |
return 1; |
| 740 |
|
| 741 |
|
| 742 |
if (*rest != ']') |
| 743 |
return 1; |
| 744 |
rest++; |
| 745 |
|
| 746 |
|
| 747 |
if (*rest != '.') |
| 748 |
return 1; |
| 749 |
rest++; |
| 750 |
|
| 751 |
|
| 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 |
|
| 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 |
|
| 767 |
if (*rest != '=') |
| 768 |
return 1; |
| 769 |
rest++; |
| 770 |
|
| 771 |
|
| 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 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 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 |
|
| 802 |
rest = parse_u32(rest, &index); |
| 803 |
if (rest == NULL) |
| 804 |
return 1; |
| 805 |
|
| 806 |
|
| 807 |
if (*rest != ']') |
| 808 |
return 1; |
| 809 |
rest++; |
| 810 |
|
| 811 |
|
| 812 |
if (*rest != '.') |
| 813 |
return 1; |
| 814 |
rest++; |
| 815 |
|
| 816 |
|
| 817 |
rest = parse_field_name(rest, g_soc_config->sdram_field_table, &field); |
| 818 |
|
| 819 |
if (rest == NULL) |
| 820 |
return 1; |
| 821 |
|
| 822 |
|
| 823 |
if (*rest != '=') |
| 824 |
return 1; |
| 825 |
rest++; |
| 826 |
|
| 827 |
|
| 828 |
rest = parse_field_value(context, rest, field, &value); |
| 829 |
if (rest == NULL) |
| 830 |
return 1; |
| 831 |
|
| 832 |
|
| 833 |
return g_soc_config->set_sdram_param(context, |
| 834 |
index, |
| 835 |
field->token, |
| 836 |
value); |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 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 |
|
| 874 |
return 1; |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 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; |
| 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 |
|
| 899 |
|
| 900 |
printf("Config file parsing error!"); |
| 901 |
exit(1); |
| 902 |
} |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
if (c_eol_comment_start && current != '/') { |
| 908 |
c_eol_comment_start = 0; |
| 909 |
buffer[space++] = '/'; |
| 910 |
} |
| 911 |
|
| 912 |
switch (current) { |
| 913 |
case '\"': |
| 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 |
|
| 937 |
comment = 1; |
| 938 |
c_eol_comment_start = 0; |
| 939 |
} else { |
| 940 |
|
| 941 |
c_eol_comment_start = 1; |
| 942 |
} |
| 943 |
} else if (!comment) |
| 944 |
buffer[space++] = current; |
| 945 |
break; |
| 946 |
|
| 947 |
|
| 948 |
case '\n': |
| 949 |
case '\r': |
| 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 |
} |