19fffb55fSDavid Gibson /* 29fffb55fSDavid Gibson * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. 39fffb55fSDavid Gibson * 49fffb55fSDavid Gibson * 59fffb55fSDavid Gibson * This program is free software; you can redistribute it and/or 69fffb55fSDavid Gibson * modify it under the terms of the GNU General Public License as 79fffb55fSDavid Gibson * published by the Free Software Foundation; either version 2 of the 89fffb55fSDavid Gibson * License, or (at your option) any later version. 99fffb55fSDavid Gibson * 109fffb55fSDavid Gibson * This program is distributed in the hope that it will be useful, 119fffb55fSDavid Gibson * but WITHOUT ANY WARRANTY; without even the implied warranty of 129fffb55fSDavid Gibson * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 139fffb55fSDavid Gibson * General Public License for more details. 149fffb55fSDavid Gibson * 159fffb55fSDavid Gibson * You should have received a copy of the GNU General Public License 169fffb55fSDavid Gibson * along with this program; if not, write to the Free Software 179fffb55fSDavid Gibson * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 189fffb55fSDavid Gibson * USA 199fffb55fSDavid Gibson */ 209fffb55fSDavid Gibson 219fffb55fSDavid Gibson #include "dtc.h" 229fffb55fSDavid Gibson #include "srcpos.h" 239fffb55fSDavid Gibson 249fffb55fSDavid Gibson extern FILE *yyin; 259fffb55fSDavid Gibson extern int yyparse(void); 269fffb55fSDavid Gibson 279fffb55fSDavid Gibson struct boot_info *the_boot_info; 289fffb55fSDavid Gibson int treesource_error; 299fffb55fSDavid Gibson 309fffb55fSDavid Gibson struct boot_info *dt_from_source(const char *fname) 319fffb55fSDavid Gibson { 329fffb55fSDavid Gibson the_boot_info = NULL; 339fffb55fSDavid Gibson treesource_error = 0; 349fffb55fSDavid Gibson 35*658f29a5SJohn Bonesio srcfile_push(fname); 36*658f29a5SJohn Bonesio yyin = current_srcfile->f; 379fffb55fSDavid Gibson 389fffb55fSDavid Gibson if (yyparse() != 0) 399fffb55fSDavid Gibson die("Unable to parse input tree\n"); 409fffb55fSDavid Gibson 419fffb55fSDavid Gibson if (treesource_error) 429fffb55fSDavid Gibson die("Syntax error parsing input tree\n"); 439fffb55fSDavid Gibson 449fffb55fSDavid Gibson return the_boot_info; 459fffb55fSDavid Gibson } 469fffb55fSDavid Gibson 479fffb55fSDavid Gibson static void write_prefix(FILE *f, int level) 489fffb55fSDavid Gibson { 499fffb55fSDavid Gibson int i; 509fffb55fSDavid Gibson 519fffb55fSDavid Gibson for (i = 0; i < level; i++) 529fffb55fSDavid Gibson fputc('\t', f); 539fffb55fSDavid Gibson } 549fffb55fSDavid Gibson 555ccd9915SJosh Triplett static int isstring(char c) 569fffb55fSDavid Gibson { 579fffb55fSDavid Gibson return (isprint(c) 589fffb55fSDavid Gibson || (c == '\0') 599fffb55fSDavid Gibson || strchr("\a\b\t\n\v\f\r", c)); 609fffb55fSDavid Gibson } 619fffb55fSDavid Gibson 629fffb55fSDavid Gibson static void write_propval_string(FILE *f, struct data val) 639fffb55fSDavid Gibson { 649fffb55fSDavid Gibson const char *str = val.val; 659fffb55fSDavid Gibson int i; 669fffb55fSDavid Gibson struct marker *m = val.markers; 679fffb55fSDavid Gibson 689fffb55fSDavid Gibson assert(str[val.len-1] == '\0'); 699fffb55fSDavid Gibson 70*658f29a5SJohn Bonesio while (m && (m->offset == 0)) { 71*658f29a5SJohn Bonesio if (m->type == LABEL) 729fffb55fSDavid Gibson fprintf(f, "%s: ", m->ref); 739fffb55fSDavid Gibson m = m->next; 749fffb55fSDavid Gibson } 759fffb55fSDavid Gibson fprintf(f, "\""); 76*658f29a5SJohn Bonesio 77*658f29a5SJohn Bonesio for (i = 0; i < (val.len-1); i++) { 78*658f29a5SJohn Bonesio char c = str[i]; 799fffb55fSDavid Gibson 809fffb55fSDavid Gibson switch (c) { 819fffb55fSDavid Gibson case '\a': 829fffb55fSDavid Gibson fprintf(f, "\\a"); 839fffb55fSDavid Gibson break; 849fffb55fSDavid Gibson case '\b': 859fffb55fSDavid Gibson fprintf(f, "\\b"); 869fffb55fSDavid Gibson break; 879fffb55fSDavid Gibson case '\t': 889fffb55fSDavid Gibson fprintf(f, "\\t"); 899fffb55fSDavid Gibson break; 909fffb55fSDavid Gibson case '\n': 919fffb55fSDavid Gibson fprintf(f, "\\n"); 929fffb55fSDavid Gibson break; 939fffb55fSDavid Gibson case '\v': 949fffb55fSDavid Gibson fprintf(f, "\\v"); 959fffb55fSDavid Gibson break; 969fffb55fSDavid Gibson case '\f': 979fffb55fSDavid Gibson fprintf(f, "\\f"); 989fffb55fSDavid Gibson break; 999fffb55fSDavid Gibson case '\r': 1009fffb55fSDavid Gibson fprintf(f, "\\r"); 1019fffb55fSDavid Gibson break; 1029fffb55fSDavid Gibson case '\\': 1039fffb55fSDavid Gibson fprintf(f, "\\\\"); 1049fffb55fSDavid Gibson break; 1059fffb55fSDavid Gibson case '\"': 1069fffb55fSDavid Gibson fprintf(f, "\\\""); 1079fffb55fSDavid Gibson break; 1089fffb55fSDavid Gibson case '\0': 1099fffb55fSDavid Gibson fprintf(f, "\", "); 110*658f29a5SJohn Bonesio while (m && (m->offset < i)) { 111*658f29a5SJohn Bonesio if (m->type == LABEL) { 112*658f29a5SJohn Bonesio assert(m->offset == (i+1)); 113*658f29a5SJohn Bonesio fprintf(f, "%s: ", m->ref); 114*658f29a5SJohn Bonesio } 115*658f29a5SJohn Bonesio m = m->next; 116*658f29a5SJohn Bonesio } 117*658f29a5SJohn Bonesio fprintf(f, "\""); 1189fffb55fSDavid Gibson break; 1199fffb55fSDavid Gibson default: 1209fffb55fSDavid Gibson if (isprint(c)) 1219fffb55fSDavid Gibson fprintf(f, "%c", c); 1229fffb55fSDavid Gibson else 1239fffb55fSDavid Gibson fprintf(f, "\\x%02hhx", c); 1249fffb55fSDavid Gibson } 1259fffb55fSDavid Gibson } 1269fffb55fSDavid Gibson fprintf(f, "\""); 1279fffb55fSDavid Gibson 1289fffb55fSDavid Gibson /* Wrap up any labels at the end of the value */ 1299fffb55fSDavid Gibson for_each_marker_of_type(m, LABEL) { 1309fffb55fSDavid Gibson assert (m->offset == val.len); 1319fffb55fSDavid Gibson fprintf(f, " %s:", m->ref); 1329fffb55fSDavid Gibson } 1339fffb55fSDavid Gibson } 1349fffb55fSDavid Gibson 1359fffb55fSDavid Gibson static void write_propval_cells(FILE *f, struct data val) 1369fffb55fSDavid Gibson { 1379fffb55fSDavid Gibson void *propend = val.val + val.len; 1389fffb55fSDavid Gibson cell_t *cp = (cell_t *)val.val; 1399fffb55fSDavid Gibson struct marker *m = val.markers; 1409fffb55fSDavid Gibson 1419fffb55fSDavid Gibson fprintf(f, "<"); 1429fffb55fSDavid Gibson for (;;) { 1439fffb55fSDavid Gibson while (m && (m->offset <= ((char *)cp - val.val))) { 1449fffb55fSDavid Gibson if (m->type == LABEL) { 1459fffb55fSDavid Gibson assert(m->offset == ((char *)cp - val.val)); 1469fffb55fSDavid Gibson fprintf(f, "%s: ", m->ref); 1479fffb55fSDavid Gibson } 1489fffb55fSDavid Gibson m = m->next; 1499fffb55fSDavid Gibson } 1509fffb55fSDavid Gibson 1519fffb55fSDavid Gibson fprintf(f, "0x%x", fdt32_to_cpu(*cp++)); 1529fffb55fSDavid Gibson if ((void *)cp >= propend) 1539fffb55fSDavid Gibson break; 1549fffb55fSDavid Gibson fprintf(f, " "); 1559fffb55fSDavid Gibson } 1569fffb55fSDavid Gibson 1579fffb55fSDavid Gibson /* Wrap up any labels at the end of the value */ 1589fffb55fSDavid Gibson for_each_marker_of_type(m, LABEL) { 1599fffb55fSDavid Gibson assert (m->offset == val.len); 1609fffb55fSDavid Gibson fprintf(f, " %s:", m->ref); 1619fffb55fSDavid Gibson } 1629fffb55fSDavid Gibson fprintf(f, ">"); 1639fffb55fSDavid Gibson } 1649fffb55fSDavid Gibson 1659fffb55fSDavid Gibson static void write_propval_bytes(FILE *f, struct data val) 1669fffb55fSDavid Gibson { 1679fffb55fSDavid Gibson void *propend = val.val + val.len; 1689fffb55fSDavid Gibson const char *bp = val.val; 1699fffb55fSDavid Gibson struct marker *m = val.markers; 1709fffb55fSDavid Gibson 1719fffb55fSDavid Gibson fprintf(f, "["); 1729fffb55fSDavid Gibson for (;;) { 1739fffb55fSDavid Gibson while (m && (m->offset == (bp-val.val))) { 1749fffb55fSDavid Gibson if (m->type == LABEL) 1759fffb55fSDavid Gibson fprintf(f, "%s: ", m->ref); 1769fffb55fSDavid Gibson m = m->next; 1779fffb55fSDavid Gibson } 1789fffb55fSDavid Gibson 1799fffb55fSDavid Gibson fprintf(f, "%02hhx", *bp++); 1809fffb55fSDavid Gibson if ((const void *)bp >= propend) 1819fffb55fSDavid Gibson break; 1829fffb55fSDavid Gibson fprintf(f, " "); 1839fffb55fSDavid Gibson } 1849fffb55fSDavid Gibson 1859fffb55fSDavid Gibson /* Wrap up any labels at the end of the value */ 1869fffb55fSDavid Gibson for_each_marker_of_type(m, LABEL) { 1879fffb55fSDavid Gibson assert (m->offset == val.len); 1889fffb55fSDavid Gibson fprintf(f, " %s:", m->ref); 1899fffb55fSDavid Gibson } 1909fffb55fSDavid Gibson fprintf(f, "]"); 1919fffb55fSDavid Gibson } 1929fffb55fSDavid Gibson 1939fffb55fSDavid Gibson static void write_propval(FILE *f, struct property *prop) 1949fffb55fSDavid Gibson { 1959fffb55fSDavid Gibson int len = prop->val.len; 1969fffb55fSDavid Gibson const char *p = prop->val.val; 1979fffb55fSDavid Gibson struct marker *m = prop->val.markers; 1989fffb55fSDavid Gibson int nnotstring = 0, nnul = 0; 1999fffb55fSDavid Gibson int nnotstringlbl = 0, nnotcelllbl = 0; 2009fffb55fSDavid Gibson int i; 2019fffb55fSDavid Gibson 2029fffb55fSDavid Gibson if (len == 0) { 2039fffb55fSDavid Gibson fprintf(f, ";\n"); 2049fffb55fSDavid Gibson return; 2059fffb55fSDavid Gibson } 2069fffb55fSDavid Gibson 2079fffb55fSDavid Gibson for (i = 0; i < len; i++) { 2089fffb55fSDavid Gibson if (! isstring(p[i])) 2099fffb55fSDavid Gibson nnotstring++; 2109fffb55fSDavid Gibson if (p[i] == '\0') 2119fffb55fSDavid Gibson nnul++; 2129fffb55fSDavid Gibson } 2139fffb55fSDavid Gibson 2149fffb55fSDavid Gibson for_each_marker_of_type(m, LABEL) { 2159fffb55fSDavid Gibson if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0')) 2169fffb55fSDavid Gibson nnotstringlbl++; 2179fffb55fSDavid Gibson if ((m->offset % sizeof(cell_t)) != 0) 2189fffb55fSDavid Gibson nnotcelllbl++; 2199fffb55fSDavid Gibson } 2209fffb55fSDavid Gibson 2219fffb55fSDavid Gibson fprintf(f, " = "); 2229fffb55fSDavid Gibson if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul)) 2239fffb55fSDavid Gibson && (nnotstringlbl == 0)) { 2249fffb55fSDavid Gibson write_propval_string(f, prop->val); 2259fffb55fSDavid Gibson } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) { 2269fffb55fSDavid Gibson write_propval_cells(f, prop->val); 2279fffb55fSDavid Gibson } else { 2289fffb55fSDavid Gibson write_propval_bytes(f, prop->val); 2299fffb55fSDavid Gibson } 2309fffb55fSDavid Gibson 2319fffb55fSDavid Gibson fprintf(f, ";\n"); 2329fffb55fSDavid Gibson } 2339fffb55fSDavid Gibson 2349fffb55fSDavid Gibson static void write_tree_source_node(FILE *f, struct node *tree, int level) 2359fffb55fSDavid Gibson { 2369fffb55fSDavid Gibson struct property *prop; 2379fffb55fSDavid Gibson struct node *child; 238*658f29a5SJohn Bonesio struct label *l; 2399fffb55fSDavid Gibson 2409fffb55fSDavid Gibson write_prefix(f, level); 241*658f29a5SJohn Bonesio for_each_label(tree->labels, l) 242*658f29a5SJohn Bonesio fprintf(f, "%s: ", l->label); 2439fffb55fSDavid Gibson if (tree->name && (*tree->name)) 2449fffb55fSDavid Gibson fprintf(f, "%s {\n", tree->name); 2459fffb55fSDavid Gibson else 2469fffb55fSDavid Gibson fprintf(f, "/ {\n"); 2479fffb55fSDavid Gibson 2489fffb55fSDavid Gibson for_each_property(tree, prop) { 2499fffb55fSDavid Gibson write_prefix(f, level+1); 250*658f29a5SJohn Bonesio for_each_label(prop->labels, l) 251*658f29a5SJohn Bonesio fprintf(f, "%s: ", l->label); 2529fffb55fSDavid Gibson fprintf(f, "%s", prop->name); 2539fffb55fSDavid Gibson write_propval(f, prop); 2549fffb55fSDavid Gibson } 2559fffb55fSDavid Gibson for_each_child(tree, child) { 2569fffb55fSDavid Gibson fprintf(f, "\n"); 2579fffb55fSDavid Gibson write_tree_source_node(f, child, level+1); 2589fffb55fSDavid Gibson } 2599fffb55fSDavid Gibson write_prefix(f, level); 2609fffb55fSDavid Gibson fprintf(f, "};\n"); 2619fffb55fSDavid Gibson } 2629fffb55fSDavid Gibson 2639fffb55fSDavid Gibson 2649fffb55fSDavid Gibson void dt_to_source(FILE *f, struct boot_info *bi) 2659fffb55fSDavid Gibson { 2669fffb55fSDavid Gibson struct reserve_info *re; 2679fffb55fSDavid Gibson 2689fffb55fSDavid Gibson fprintf(f, "/dts-v1/;\n\n"); 2699fffb55fSDavid Gibson 2709fffb55fSDavid Gibson for (re = bi->reservelist; re; re = re->next) { 271*658f29a5SJohn Bonesio struct label *l; 272*658f29a5SJohn Bonesio 273*658f29a5SJohn Bonesio for_each_label(re->labels, l) 274*658f29a5SJohn Bonesio fprintf(f, "%s: ", l->label); 2759fffb55fSDavid Gibson fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n", 2769fffb55fSDavid Gibson (unsigned long long)re->re.address, 2779fffb55fSDavid Gibson (unsigned long long)re->re.size); 2789fffb55fSDavid Gibson } 2799fffb55fSDavid Gibson 2809fffb55fSDavid Gibson write_tree_source_node(f, bi->dt, 0); 2819fffb55fSDavid Gibson } 2829fffb55fSDavid Gibson 283