11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29fffb55fSDavid Gibson /*
39fffb55fSDavid Gibson * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
49fffb55fSDavid Gibson */
59fffb55fSDavid Gibson
69fffb55fSDavid Gibson #include "dtc.h"
79fffb55fSDavid Gibson #include "srcpos.h"
89fffb55fSDavid Gibson
99fffb55fSDavid Gibson extern FILE *yyin;
109fffb55fSDavid Gibson extern int yyparse(void);
11cd296721SStephen Warren extern YYLTYPE yylloc;
129fffb55fSDavid Gibson
136f05afcbSRob Herring struct dt_info *parser_output;
1447605971SRob Herring bool treesource_error;
159fffb55fSDavid Gibson
dt_from_source(const char * fname)166f05afcbSRob Herring struct dt_info *dt_from_source(const char *fname)
179fffb55fSDavid Gibson {
186f05afcbSRob Herring parser_output = NULL;
1947605971SRob Herring treesource_error = false;
209fffb55fSDavid Gibson
21658f29a5SJohn Bonesio srcfile_push(fname);
22658f29a5SJohn Bonesio yyin = current_srcfile->f;
23cd296721SStephen Warren yylloc.file = current_srcfile;
249fffb55fSDavid Gibson
259fffb55fSDavid Gibson if (yyparse() != 0)
269fffb55fSDavid Gibson die("Unable to parse input tree\n");
279fffb55fSDavid Gibson
289fffb55fSDavid Gibson if (treesource_error)
299fffb55fSDavid Gibson die("Syntax error parsing input tree\n");
309fffb55fSDavid Gibson
316f05afcbSRob Herring return parser_output;
329fffb55fSDavid Gibson }
339fffb55fSDavid Gibson
write_prefix(FILE * f,int level)349fffb55fSDavid Gibson static void write_prefix(FILE *f, int level)
359fffb55fSDavid Gibson {
369fffb55fSDavid Gibson int i;
379fffb55fSDavid Gibson
389fffb55fSDavid Gibson for (i = 0; i < level; i++)
399fffb55fSDavid Gibson fputc('\t', f);
409fffb55fSDavid Gibson }
419fffb55fSDavid Gibson
isstring(char c)4247605971SRob Herring static bool isstring(char c)
439fffb55fSDavid Gibson {
4447605971SRob Herring return (isprint((unsigned char)c)
459fffb55fSDavid Gibson || (c == '\0')
469fffb55fSDavid Gibson || strchr("\a\b\t\n\v\f\r", c));
479fffb55fSDavid Gibson }
489fffb55fSDavid Gibson
write_propval_string(FILE * f,const char * s,size_t len)49f858927fSRob Herring static void write_propval_string(FILE *f, const char *s, size_t len)
509fffb55fSDavid Gibson {
51f858927fSRob Herring const char *end = s + len - 1;
52c2e7075cSRob Herring
53c2e7075cSRob Herring if (!len)
54c2e7075cSRob Herring return;
55c2e7075cSRob Herring
56f858927fSRob Herring assert(*end == '\0');
579fffb55fSDavid Gibson
589fffb55fSDavid Gibson fprintf(f, "\"");
59f858927fSRob Herring while (s < end) {
60f858927fSRob Herring char c = *s++;
619fffb55fSDavid Gibson switch (c) {
629fffb55fSDavid Gibson case '\a':
639fffb55fSDavid Gibson fprintf(f, "\\a");
649fffb55fSDavid Gibson break;
659fffb55fSDavid Gibson case '\b':
669fffb55fSDavid Gibson fprintf(f, "\\b");
679fffb55fSDavid Gibson break;
689fffb55fSDavid Gibson case '\t':
699fffb55fSDavid Gibson fprintf(f, "\\t");
709fffb55fSDavid Gibson break;
719fffb55fSDavid Gibson case '\n':
729fffb55fSDavid Gibson fprintf(f, "\\n");
739fffb55fSDavid Gibson break;
749fffb55fSDavid Gibson case '\v':
759fffb55fSDavid Gibson fprintf(f, "\\v");
769fffb55fSDavid Gibson break;
779fffb55fSDavid Gibson case '\f':
789fffb55fSDavid Gibson fprintf(f, "\\f");
799fffb55fSDavid Gibson break;
809fffb55fSDavid Gibson case '\r':
819fffb55fSDavid Gibson fprintf(f, "\\r");
829fffb55fSDavid Gibson break;
839fffb55fSDavid Gibson case '\\':
849fffb55fSDavid Gibson fprintf(f, "\\\\");
859fffb55fSDavid Gibson break;
869fffb55fSDavid Gibson case '\"':
879fffb55fSDavid Gibson fprintf(f, "\\\"");
889fffb55fSDavid Gibson break;
899fffb55fSDavid Gibson case '\0':
90f858927fSRob Herring fprintf(f, "\\0");
919fffb55fSDavid Gibson break;
929fffb55fSDavid Gibson default:
9347605971SRob Herring if (isprint((unsigned char)c))
949fffb55fSDavid Gibson fprintf(f, "%c", c);
959fffb55fSDavid Gibson else
96f858927fSRob Herring fprintf(f, "\\x%02"PRIx8, c);
979fffb55fSDavid Gibson }
989fffb55fSDavid Gibson }
999fffb55fSDavid Gibson fprintf(f, "\"");
1009fffb55fSDavid Gibson }
1019fffb55fSDavid Gibson
write_propval_int(FILE * f,const char * p,size_t len,size_t width)102f858927fSRob Herring static void write_propval_int(FILE *f, const char *p, size_t len, size_t width)
1039fffb55fSDavid Gibson {
104f858927fSRob Herring const char *end = p + len;
105f858927fSRob Herring assert(len % width == 0);
1069fffb55fSDavid Gibson
107f858927fSRob Herring for (; p < end; p += width) {
108f858927fSRob Herring switch (width) {
109f858927fSRob Herring case 1:
110f858927fSRob Herring fprintf(f, "%02"PRIx8, *(const uint8_t*)p);
1119fffb55fSDavid Gibson break;
112f858927fSRob Herring case 2:
1133eb619b2SRob Herring fprintf(f, "0x%02"PRIx16, dtb_ld16(p));
114f858927fSRob Herring break;
115f858927fSRob Herring case 4:
1163eb619b2SRob Herring fprintf(f, "0x%02"PRIx32, dtb_ld32(p));
117f858927fSRob Herring break;
118f858927fSRob Herring case 8:
1193eb619b2SRob Herring fprintf(f, "0x%02"PRIx64, dtb_ld64(p));
120f858927fSRob Herring break;
121f858927fSRob Herring }
122c2e7075cSRob Herring if (p + width < end)
123c2e7075cSRob Herring fputc(' ', f);
124f858927fSRob Herring }
1259fffb55fSDavid Gibson }
1269fffb55fSDavid Gibson
127f858927fSRob Herring static const char *delim_start[] = {
128f858927fSRob Herring [TYPE_UINT8] = "[",
129f858927fSRob Herring [TYPE_UINT16] = "/bits/ 16 <",
130f858927fSRob Herring [TYPE_UINT32] = "<",
131f858927fSRob Herring [TYPE_UINT64] = "/bits/ 64 <",
132f858927fSRob Herring [TYPE_STRING] = "",
133f858927fSRob Herring };
134f858927fSRob Herring static const char *delim_end[] = {
135f858927fSRob Herring [TYPE_UINT8] = "]",
136f858927fSRob Herring [TYPE_UINT16] = ">",
137f858927fSRob Herring [TYPE_UINT32] = ">",
138f858927fSRob Herring [TYPE_UINT64] = ">",
139f858927fSRob Herring [TYPE_STRING] = "",
140f858927fSRob Herring };
1419fffb55fSDavid Gibson
guess_value_type(struct property * prop)142f858927fSRob Herring static enum markertype guess_value_type(struct property *prop)
1439fffb55fSDavid Gibson {
1449fffb55fSDavid Gibson int len = prop->val.len;
1459fffb55fSDavid Gibson const char *p = prop->val.val;
1469fffb55fSDavid Gibson struct marker *m = prop->val.markers;
1479fffb55fSDavid Gibson int nnotstring = 0, nnul = 0;
1489fffb55fSDavid Gibson int nnotstringlbl = 0, nnotcelllbl = 0;
1499fffb55fSDavid Gibson int i;
1509fffb55fSDavid Gibson
1519fffb55fSDavid Gibson for (i = 0; i < len; i++) {
1529fffb55fSDavid Gibson if (! isstring(p[i]))
1539fffb55fSDavid Gibson nnotstring++;
1549fffb55fSDavid Gibson if (p[i] == '\0')
1559fffb55fSDavid Gibson nnul++;
1569fffb55fSDavid Gibson }
1579fffb55fSDavid Gibson
1589fffb55fSDavid Gibson for_each_marker_of_type(m, LABEL) {
1599fffb55fSDavid Gibson if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
1609fffb55fSDavid Gibson nnotstringlbl++;
1619fffb55fSDavid Gibson if ((m->offset % sizeof(cell_t)) != 0)
1629fffb55fSDavid Gibson nnotcelllbl++;
1639fffb55fSDavid Gibson }
1649fffb55fSDavid Gibson
1653eb619b2SRob Herring if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul <= (len-nnul))
1669fffb55fSDavid Gibson && (nnotstringlbl == 0)) {
167f858927fSRob Herring return TYPE_STRING;
1689fffb55fSDavid Gibson } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
169f858927fSRob Herring return TYPE_UINT32;
1709fffb55fSDavid Gibson }
1719fffb55fSDavid Gibson
172f858927fSRob Herring return TYPE_UINT8;
173f858927fSRob Herring }
174f858927fSRob Herring
write_propval(FILE * f,struct property * prop)175f858927fSRob Herring static void write_propval(FILE *f, struct property *prop)
176f858927fSRob Herring {
177f858927fSRob Herring size_t len = prop->val.len;
178f858927fSRob Herring struct marker *m = prop->val.markers;
179f858927fSRob Herring struct marker dummy_marker;
180f858927fSRob Herring enum markertype emit_type = TYPE_NONE;
181c2e7075cSRob Herring char *srcstr;
182f858927fSRob Herring
183f858927fSRob Herring if (len == 0) {
184c2e7075cSRob Herring fprintf(f, ";");
185c2e7075cSRob Herring if (annotate) {
186c2e7075cSRob Herring srcstr = srcpos_string_first(prop->srcpos, annotate);
187c2e7075cSRob Herring if (srcstr) {
188c2e7075cSRob Herring fprintf(f, " /* %s */", srcstr);
189c2e7075cSRob Herring free(srcstr);
190c2e7075cSRob Herring }
191c2e7075cSRob Herring }
192c2e7075cSRob Herring fprintf(f, "\n");
193f858927fSRob Herring return;
194f858927fSRob Herring }
195f858927fSRob Herring
196f858927fSRob Herring fprintf(f, " =");
197f858927fSRob Herring
198f858927fSRob Herring if (!next_type_marker(m)) {
199f858927fSRob Herring /* data type information missing, need to guess */
200f858927fSRob Herring dummy_marker.type = guess_value_type(prop);
201f858927fSRob Herring dummy_marker.next = prop->val.markers;
202f858927fSRob Herring dummy_marker.offset = 0;
203f858927fSRob Herring dummy_marker.ref = NULL;
204f858927fSRob Herring m = &dummy_marker;
205f858927fSRob Herring }
206f858927fSRob Herring
207f858927fSRob Herring for_each_marker(m) {
208c2e7075cSRob Herring size_t chunk_len = (m->next ? m->next->offset : len) - m->offset;
209c2e7075cSRob Herring size_t data_len = type_marker_length(m) ? : len - m->offset;
210f858927fSRob Herring const char *p = &prop->val.val[m->offset];
211*a77725a9SRob Herring struct marker *m_phandle;
212f858927fSRob Herring
213*a77725a9SRob Herring if (is_type_marker(m->type)) {
214f858927fSRob Herring emit_type = m->type;
215f858927fSRob Herring fprintf(f, " %s", delim_start[emit_type]);
216c2e7075cSRob Herring } else if (m->type == LABEL)
217c2e7075cSRob Herring fprintf(f, " %s:", m->ref);
218f858927fSRob Herring
219*a77725a9SRob Herring if (emit_type == TYPE_NONE || chunk_len == 0)
220f858927fSRob Herring continue;
221f858927fSRob Herring
222f858927fSRob Herring switch(emit_type) {
223f858927fSRob Herring case TYPE_UINT16:
224f858927fSRob Herring write_propval_int(f, p, chunk_len, 2);
225f858927fSRob Herring break;
226f858927fSRob Herring case TYPE_UINT32:
227*a77725a9SRob Herring m_phandle = prop->val.markers;
228*a77725a9SRob Herring for_each_marker_of_type(m_phandle, REF_PHANDLE)
229*a77725a9SRob Herring if (m->offset == m_phandle->offset)
230*a77725a9SRob Herring break;
231*a77725a9SRob Herring
232*a77725a9SRob Herring if (m_phandle) {
233*a77725a9SRob Herring if (m_phandle->ref[0] == '/')
234*a77725a9SRob Herring fprintf(f, "&{%s}", m_phandle->ref);
235*a77725a9SRob Herring else
236*a77725a9SRob Herring fprintf(f, "&%s", m_phandle->ref);
237*a77725a9SRob Herring if (chunk_len > 4) {
238*a77725a9SRob Herring fputc(' ', f);
239*a77725a9SRob Herring write_propval_int(f, p + 4, chunk_len - 4, 4);
240*a77725a9SRob Herring }
241*a77725a9SRob Herring } else {
242f858927fSRob Herring write_propval_int(f, p, chunk_len, 4);
243*a77725a9SRob Herring }
244f858927fSRob Herring break;
245f858927fSRob Herring case TYPE_UINT64:
246f858927fSRob Herring write_propval_int(f, p, chunk_len, 8);
247f858927fSRob Herring break;
248f858927fSRob Herring case TYPE_STRING:
249f858927fSRob Herring write_propval_string(f, p, chunk_len);
250f858927fSRob Herring break;
251f858927fSRob Herring default:
252f858927fSRob Herring write_propval_int(f, p, chunk_len, 1);
253f858927fSRob Herring }
254f858927fSRob Herring
255c2e7075cSRob Herring if (chunk_len == data_len) {
256c2e7075cSRob Herring size_t pos = m->offset + chunk_len;
257c2e7075cSRob Herring fprintf(f, pos == len ? "%s" : "%s,",
258c2e7075cSRob Herring delim_end[emit_type] ? : "");
259c2e7075cSRob Herring emit_type = TYPE_NONE;
260f858927fSRob Herring }
261c2e7075cSRob Herring }
262c2e7075cSRob Herring fprintf(f, ";");
263c2e7075cSRob Herring if (annotate) {
264c2e7075cSRob Herring srcstr = srcpos_string_first(prop->srcpos, annotate);
265c2e7075cSRob Herring if (srcstr) {
266c2e7075cSRob Herring fprintf(f, " /* %s */", srcstr);
267c2e7075cSRob Herring free(srcstr);
268c2e7075cSRob Herring }
269c2e7075cSRob Herring }
270c2e7075cSRob Herring fprintf(f, "\n");
2719fffb55fSDavid Gibson }
2729fffb55fSDavid Gibson
write_tree_source_node(FILE * f,struct node * tree,int level)2739fffb55fSDavid Gibson static void write_tree_source_node(FILE *f, struct node *tree, int level)
2749fffb55fSDavid Gibson {
2759fffb55fSDavid Gibson struct property *prop;
2769fffb55fSDavid Gibson struct node *child;
277658f29a5SJohn Bonesio struct label *l;
278c2e7075cSRob Herring char *srcstr;
2799fffb55fSDavid Gibson
2809fffb55fSDavid Gibson write_prefix(f, level);
281658f29a5SJohn Bonesio for_each_label(tree->labels, l)
282658f29a5SJohn Bonesio fprintf(f, "%s: ", l->label);
2839fffb55fSDavid Gibson if (tree->name && (*tree->name))
284c2e7075cSRob Herring fprintf(f, "%s {", tree->name);
2859fffb55fSDavid Gibson else
286c2e7075cSRob Herring fprintf(f, "/ {");
287c2e7075cSRob Herring
288c2e7075cSRob Herring if (annotate) {
289c2e7075cSRob Herring srcstr = srcpos_string_first(tree->srcpos, annotate);
290c2e7075cSRob Herring if (srcstr) {
291c2e7075cSRob Herring fprintf(f, " /* %s */", srcstr);
292c2e7075cSRob Herring free(srcstr);
293c2e7075cSRob Herring }
294c2e7075cSRob Herring }
295c2e7075cSRob Herring fprintf(f, "\n");
2969fffb55fSDavid Gibson
2979fffb55fSDavid Gibson for_each_property(tree, prop) {
2989fffb55fSDavid Gibson write_prefix(f, level+1);
299658f29a5SJohn Bonesio for_each_label(prop->labels, l)
300658f29a5SJohn Bonesio fprintf(f, "%s: ", l->label);
3019fffb55fSDavid Gibson fprintf(f, "%s", prop->name);
3029fffb55fSDavid Gibson write_propval(f, prop);
3039fffb55fSDavid Gibson }
3049fffb55fSDavid Gibson for_each_child(tree, child) {
3059fffb55fSDavid Gibson fprintf(f, "\n");
3069fffb55fSDavid Gibson write_tree_source_node(f, child, level+1);
3079fffb55fSDavid Gibson }
3089fffb55fSDavid Gibson write_prefix(f, level);
309c2e7075cSRob Herring fprintf(f, "};");
310c2e7075cSRob Herring if (annotate) {
311c2e7075cSRob Herring srcstr = srcpos_string_last(tree->srcpos, annotate);
312c2e7075cSRob Herring if (srcstr) {
313c2e7075cSRob Herring fprintf(f, " /* %s */", srcstr);
314c2e7075cSRob Herring free(srcstr);
3159fffb55fSDavid Gibson }
316c2e7075cSRob Herring }
317c2e7075cSRob Herring fprintf(f, "\n");
318c2e7075cSRob Herring }
3199fffb55fSDavid Gibson
dt_to_source(FILE * f,struct dt_info * dti)3206f05afcbSRob Herring void dt_to_source(FILE *f, struct dt_info *dti)
3219fffb55fSDavid Gibson {
3229fffb55fSDavid Gibson struct reserve_info *re;
3239fffb55fSDavid Gibson
3249fffb55fSDavid Gibson fprintf(f, "/dts-v1/;\n\n");
3259fffb55fSDavid Gibson
3266f05afcbSRob Herring for (re = dti->reservelist; re; re = re->next) {
327658f29a5SJohn Bonesio struct label *l;
328658f29a5SJohn Bonesio
329658f29a5SJohn Bonesio for_each_label(re->labels, l)
330658f29a5SJohn Bonesio fprintf(f, "%s: ", l->label);
3319fffb55fSDavid Gibson fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
33289d12310SRob Herring (unsigned long long)re->address,
33389d12310SRob Herring (unsigned long long)re->size);
3349fffb55fSDavid Gibson }
3359fffb55fSDavid Gibson
3366f05afcbSRob Herring write_tree_source_node(f, dti->dt, 0);
3379fffb55fSDavid Gibson }
338