blob: 1af36628b75f86cdf99315f609a217292c008a22 [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
David Gibson63dc9c72007-09-18 11:44:04 +10004 *
David Gibsonfc14dad2005-06-08 17:18:34 +10005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
David Gibson63dc9c72007-09-18 11:44:04 +100014 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
David Gibsonfc14dad2005-06-08 17:18:34 +100019 */
20
21#include "dtc.h"
Jon Loeligere45e6fd2007-03-23 15:18:41 -050022#include "srcpos.h"
David Gibsonfc14dad2005-06-08 17:18:34 +100023
David Gibsonfc14dad2005-06-08 17:18:34 +100024extern FILE *yyin;
25extern int yyparse(void);
Horst Kronstorfera6e6c602012-02-07 10:02:53 +010026extern YYLTYPE yylloc;
David Gibsonfc14dad2005-06-08 17:18:34 +100027
David Gibson00fbb862016-05-31 11:58:42 +100028struct dt_info *parser_output;
David Gibson17625372013-10-28 21:06:53 +110029bool treesource_error;
David Gibsonf0517db2005-07-15 17:14:24 +100030
David Gibson00fbb862016-05-31 11:58:42 +100031struct dt_info *dt_from_source(const char *fname)
David Gibsonf0517db2005-07-15 17:14:24 +100032{
David Gibson00fbb862016-05-31 11:58:42 +100033 parser_output = NULL;
David Gibson17625372013-10-28 21:06:53 +110034 treesource_error = false;
David Gibsonf0517db2005-07-15 17:14:24 +100035
David Gibsond68cb362009-12-08 14:24:42 +110036 srcfile_push(fname);
37 yyin = current_srcfile->f;
Horst Kronstorfera6e6c602012-02-07 10:02:53 +010038 yylloc.file = current_srcfile;
Jon Loeligere45e6fd2007-03-23 15:18:41 -050039
David Gibsonfc14dad2005-06-08 17:18:34 +100040 if (yyparse() != 0)
David Gibson35aa1a22008-05-16 13:21:51 +100041 die("Unable to parse input tree\n");
David Gibsonfc14dad2005-06-08 17:18:34 +100042
David Gibson35aa1a22008-05-16 13:21:51 +100043 if (treesource_error)
44 die("Syntax error parsing input tree\n");
45
David Gibson00fbb862016-05-31 11:58:42 +100046 return parser_output;
David Gibsonfc14dad2005-06-08 17:18:34 +100047}
48
49static void write_prefix(FILE *f, int level)
50{
51 int i;
52
53 for (i = 0; i < level; i++)
54 fputc('\t', f);
55}
56
David Gibson17625372013-10-28 21:06:53 +110057static bool isstring(char c)
David Gibson5a98ddd2007-10-17 12:39:10 +100058{
Serge Lamikhov-Center17119ab2013-12-25 15:26:03 +110059 return (isprint((unsigned char)c)
David Gibson5a98ddd2007-10-17 12:39:10 +100060 || (c == '\0')
61 || strchr("\a\b\t\n\v\f\r", c));
62}
63
Grant Likely32b9c612018-06-28 15:37:01 -060064static void write_propval_string(FILE *f, const char *s, size_t len)
David Gibson5a98ddd2007-10-17 12:39:10 +100065{
Grant Likely32b9c612018-06-28 15:37:01 -060066 const char *end = s + len - 1;
Rob Herring522d81d2018-09-26 14:27:08 -050067
68 if (!len)
69 return;
70
Grant Likely32b9c612018-06-28 15:37:01 -060071 assert(*end == '\0');
David Gibson5a98ddd2007-10-17 12:39:10 +100072
David Gibsonc623fe52009-09-09 14:38:30 +100073 fprintf(f, "\"");
Grant Likely32b9c612018-06-28 15:37:01 -060074 while (s < end) {
75 char c = *s++;
David Gibson5a98ddd2007-10-17 12:39:10 +100076 switch (c) {
77 case '\a':
78 fprintf(f, "\\a");
79 break;
80 case '\b':
81 fprintf(f, "\\b");
82 break;
83 case '\t':
84 fprintf(f, "\\t");
85 break;
86 case '\n':
87 fprintf(f, "\\n");
88 break;
89 case '\v':
90 fprintf(f, "\\v");
91 break;
92 case '\f':
93 fprintf(f, "\\f");
94 break;
95 case '\r':
96 fprintf(f, "\\r");
97 break;
98 case '\\':
99 fprintf(f, "\\\\");
100 break;
101 case '\"':
102 fprintf(f, "\\\"");
103 break;
104 case '\0':
Grant Likely32b9c612018-06-28 15:37:01 -0600105 fprintf(f, "\\0");
David Gibson5a98ddd2007-10-17 12:39:10 +1000106 break;
107 default:
Serge Lamikhov-Center17119ab2013-12-25 15:26:03 +1100108 if (isprint((unsigned char)c))
David Gibson5a98ddd2007-10-17 12:39:10 +1000109 fprintf(f, "%c", c);
110 else
Grant Likely32b9c612018-06-28 15:37:01 -0600111 fprintf(f, "\\x%02"PRIx8, c);
David Gibson5a98ddd2007-10-17 12:39:10 +1000112 }
113 }
David Gibsoned01ae42007-11-07 10:21:20 +1100114 fprintf(f, "\"");
David Gibson5a98ddd2007-10-17 12:39:10 +1000115}
116
Grant Likely32b9c612018-06-28 15:37:01 -0600117static void write_propval_int(FILE *f, const char *p, size_t len, size_t width)
David Gibson5a98ddd2007-10-17 12:39:10 +1000118{
Grant Likely32b9c612018-06-28 15:37:01 -0600119 const char *end = p + len;
120 assert(len % width == 0);
David Gibson5a98ddd2007-10-17 12:39:10 +1000121
Grant Likely32b9c612018-06-28 15:37:01 -0600122 for (; p < end; p += width) {
123 switch (width) {
124 case 1:
Rob Herringd448f9a2018-09-14 13:39:06 -0500125 fprintf(f, "%02"PRIx8, *(const uint8_t*)p);
David Gibson5a98ddd2007-10-17 12:39:10 +1000126 break;
Grant Likely32b9c612018-06-28 15:37:01 -0600127 case 2:
Rob Herringd448f9a2018-09-14 13:39:06 -0500128 fprintf(f, "0x%02"PRIx16, fdt16_to_cpu(*(const fdt16_t*)p));
Grant Likely32b9c612018-06-28 15:37:01 -0600129 break;
130 case 4:
Rob Herringd448f9a2018-09-14 13:39:06 -0500131 fprintf(f, "0x%02"PRIx32, fdt32_to_cpu(*(const fdt32_t*)p));
Grant Likely32b9c612018-06-28 15:37:01 -0600132 break;
133 case 8:
Rob Herringd448f9a2018-09-14 13:39:06 -0500134 fprintf(f, "0x%02"PRIx64, fdt64_to_cpu(*(const fdt64_t*)p));
Grant Likely32b9c612018-06-28 15:37:01 -0600135 break;
136 }
Rob Herringd448f9a2018-09-14 13:39:06 -0500137 if (p + width < end)
138 fputc(' ', f);
David Gibson5a98ddd2007-10-17 12:39:10 +1000139 }
David Gibson5a98ddd2007-10-17 12:39:10 +1000140}
141
Greg Kurz9619c862018-08-30 12:01:59 +0200142static bool has_data_type_information(struct marker *m)
143{
144 return m->type >= TYPE_UINT8;
145}
146
Grant Likely32b9c612018-06-28 15:37:01 -0600147static struct marker *next_type_marker(struct marker *m)
David Gibson5a98ddd2007-10-17 12:39:10 +1000148{
Greg Kurz9619c862018-08-30 12:01:59 +0200149 while (m && !has_data_type_information(m))
Grant Likely32b9c612018-06-28 15:37:01 -0600150 m = m->next;
151 return m;
David Gibsoned01ae42007-11-07 10:21:20 +1100152}
153
Rob Herring361b5e72018-09-11 15:41:30 -0500154size_t type_marker_length(struct marker *m)
Grant Likely32b9c612018-06-28 15:37:01 -0600155{
156 struct marker *next = next_type_marker(m->next);
157
158 if (next)
159 return next->offset - m->offset;
160 return 0;
161}
162
163static const char *delim_start[] = {
164 [TYPE_UINT8] = "[",
165 [TYPE_UINT16] = "/bits/ 16 <",
166 [TYPE_UINT32] = "<",
167 [TYPE_UINT64] = "/bits/ 64 <",
168 [TYPE_STRING] = "",
169};
170static const char *delim_end[] = {
Rob Herringd448f9a2018-09-14 13:39:06 -0500171 [TYPE_UINT8] = "]",
172 [TYPE_UINT16] = ">",
173 [TYPE_UINT32] = ">",
174 [TYPE_UINT64] = ">",
Grant Likely32b9c612018-06-28 15:37:01 -0600175 [TYPE_STRING] = "",
176};
177
178static enum markertype guess_value_type(struct property *prop)
David Gibsoned01ae42007-11-07 10:21:20 +1100179{
180 int len = prop->val.len;
David Gibson92cb9a22007-12-04 14:26:15 +1100181 const char *p = prop->val.val;
David Gibsondc941772007-11-22 14:39:23 +1100182 struct marker *m = prop->val.markers;
David Gibsoned01ae42007-11-07 10:21:20 +1100183 int nnotstring = 0, nnul = 0;
David Gibsond3ea6e52007-11-07 10:22:25 +1100184 int nnotstringlbl = 0, nnotcelllbl = 0;
David Gibsoned01ae42007-11-07 10:21:20 +1100185 int i;
186
David Gibsoned01ae42007-11-07 10:21:20 +1100187 for (i = 0; i < len; i++) {
188 if (! isstring(p[i]))
189 nnotstring++;
190 if (p[i] == '\0')
191 nnul++;
192 }
193
David Gibsondc941772007-11-22 14:39:23 +1100194 for_each_marker_of_type(m, LABEL) {
195 if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
David Gibsond3ea6e52007-11-07 10:22:25 +1100196 nnotstringlbl++;
David Gibsondc941772007-11-22 14:39:23 +1100197 if ((m->offset % sizeof(cell_t)) != 0)
David Gibsond3ea6e52007-11-07 10:22:25 +1100198 nnotcelllbl++;
199 }
David Gibsoned01ae42007-11-07 10:21:20 +1100200
David Gibsond3ea6e52007-11-07 10:22:25 +1100201 if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
202 && (nnotstringlbl == 0)) {
Grant Likely32b9c612018-06-28 15:37:01 -0600203 return TYPE_STRING;
David Gibsond3ea6e52007-11-07 10:22:25 +1100204 } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
Grant Likely32b9c612018-06-28 15:37:01 -0600205 return TYPE_UINT32;
David Gibsoned01ae42007-11-07 10:21:20 +1100206 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100207
Grant Likely32b9c612018-06-28 15:37:01 -0600208 return TYPE_UINT8;
209}
210
211static void write_propval(FILE *f, struct property *prop)
212{
213 size_t len = prop->val.len;
214 struct marker *m = prop->val.markers;
215 struct marker dummy_marker;
216 enum markertype emit_type = TYPE_NONE;
Julia Lawall5667e7e2018-11-16 17:30:00 +0100217 char *srcstr;
Grant Likely32b9c612018-06-28 15:37:01 -0600218
219 if (len == 0) {
Julia Lawall5667e7e2018-11-16 17:30:00 +0100220 fprintf(f, ";");
221 if (annotate) {
222 srcstr = srcpos_string_first(prop->srcpos, annotate);
223 if (srcstr) {
224 fprintf(f, " /* %s */", srcstr);
225 free(srcstr);
226 }
227 }
228 fprintf(f, "\n");
Grant Likely32b9c612018-06-28 15:37:01 -0600229 return;
230 }
231
Grant Likely8c59a972018-09-14 14:01:09 +0100232 fprintf(f, " =");
Grant Likely32b9c612018-06-28 15:37:01 -0600233
234 if (!next_type_marker(m)) {
235 /* data type information missing, need to guess */
236 dummy_marker.type = guess_value_type(prop);
237 dummy_marker.next = prop->val.markers;
238 dummy_marker.offset = 0;
239 dummy_marker.ref = NULL;
240 m = &dummy_marker;
241 }
242
Grant Likely32b9c612018-06-28 15:37:01 -0600243 for_each_marker(m) {
Grant Likely8c59a972018-09-14 14:01:09 +0100244 size_t chunk_len = (m->next ? m->next->offset : len) - m->offset;
245 size_t data_len = type_marker_length(m) ? : len - m->offset;
Grant Likely32b9c612018-06-28 15:37:01 -0600246 const char *p = &prop->val.val[m->offset];
247
Grant Likely8c59a972018-09-14 14:01:09 +0100248 if (has_data_type_information(m)) {
249 emit_type = m->type;
250 fprintf(f, " %s", delim_start[emit_type]);
Rob Herringda2b6912018-10-10 10:19:30 -0500251 } else if (m->type == LABEL)
Grant Likely8c59a972018-09-14 14:01:09 +0100252 fprintf(f, " %s:", m->ref);
Rob Herringda2b6912018-10-10 10:19:30 -0500253 else if (m->offset)
254 fputc(' ', f);
Grant Likely32b9c612018-06-28 15:37:01 -0600255
Grant Likely8c59a972018-09-14 14:01:09 +0100256 if (emit_type == TYPE_NONE) {
257 assert(chunk_len == 0);
Grant Likely32b9c612018-06-28 15:37:01 -0600258 continue;
Grant Likely8c59a972018-09-14 14:01:09 +0100259 }
Grant Likely32b9c612018-06-28 15:37:01 -0600260
261 switch(emit_type) {
262 case TYPE_UINT16:
263 write_propval_int(f, p, chunk_len, 2);
264 break;
265 case TYPE_UINT32:
266 write_propval_int(f, p, chunk_len, 4);
267 break;
268 case TYPE_UINT64:
269 write_propval_int(f, p, chunk_len, 8);
270 break;
271 case TYPE_STRING:
272 write_propval_string(f, p, chunk_len);
273 break;
274 default:
275 write_propval_int(f, p, chunk_len, 1);
276 }
Grant Likely32b9c612018-06-28 15:37:01 -0600277
Grant Likely8c59a972018-09-14 14:01:09 +0100278 if (chunk_len == data_len) {
279 size_t pos = m->offset + chunk_len;
280 fprintf(f, pos == len ? "%s" : "%s,",
281 delim_end[emit_type] ? : "");
282 emit_type = TYPE_NONE;
283 }
Grant Likely32b9c612018-06-28 15:37:01 -0600284 }
Julia Lawall5667e7e2018-11-16 17:30:00 +0100285 fprintf(f, ";");
286 if (annotate) {
287 srcstr = srcpos_string_first(prop->srcpos, annotate);
288 if (srcstr) {
289 fprintf(f, " /* %s */", srcstr);
290 free(srcstr);
291 }
292 }
293 fprintf(f, "\n");
David Gibson5a98ddd2007-10-17 12:39:10 +1000294}
David Gibsonf0517db2005-07-15 17:14:24 +1000295
David Gibson230f2532005-08-29 12:48:02 +1000296static void write_tree_source_node(FILE *f, struct node *tree, int level)
David Gibsonfc14dad2005-06-08 17:18:34 +1000297{
298 struct property *prop;
299 struct node *child;
David Gibson05898c62010-02-24 18:22:17 +1100300 struct label *l;
Julia Lawall5667e7e2018-11-16 17:30:00 +0100301 char *srcstr;
David Gibsonfc14dad2005-06-08 17:18:34 +1000302
303 write_prefix(f, level);
David Gibson05898c62010-02-24 18:22:17 +1100304 for_each_label(tree->labels, l)
305 fprintf(f, "%s: ", l->label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000306 if (tree->name && (*tree->name))
Julia Lawall5667e7e2018-11-16 17:30:00 +0100307 fprintf(f, "%s {", tree->name);
David Gibsonfc14dad2005-06-08 17:18:34 +1000308 else
Julia Lawall5667e7e2018-11-16 17:30:00 +0100309 fprintf(f, "/ {");
310
311 if (annotate) {
312 srcstr = srcpos_string_first(tree->srcpos, annotate);
313 if (srcstr) {
314 fprintf(f, " /* %s */", srcstr);
315 free(srcstr);
316 }
317 }
318 fprintf(f, "\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000319
320 for_each_property(tree, prop) {
David Gibson02563ad2007-11-02 16:10:30 +1100321 write_prefix(f, level+1);
David Gibson05898c62010-02-24 18:22:17 +1100322 for_each_label(prop->labels, l)
323 fprintf(f, "%s: ", l->label);
David Gibson02563ad2007-11-02 16:10:30 +1100324 fprintf(f, "%s", prop->name);
David Gibsoned01ae42007-11-07 10:21:20 +1100325 write_propval(f, prop);
David Gibsonfc14dad2005-06-08 17:18:34 +1000326 }
327 for_each_child(tree, child) {
328 fprintf(f, "\n");
David Gibsonf0517db2005-07-15 17:14:24 +1000329 write_tree_source_node(f, child, level+1);
David Gibsonfc14dad2005-06-08 17:18:34 +1000330 }
331 write_prefix(f, level);
Julia Lawall5667e7e2018-11-16 17:30:00 +0100332 fprintf(f, "};");
333 if (annotate) {
334 srcstr = srcpos_string_last(tree->srcpos, annotate);
335 if (srcstr) {
336 fprintf(f, " /* %s */", srcstr);
337 free(srcstr);
338 }
339 }
340 fprintf(f, "\n");
David Gibsonfc14dad2005-06-08 17:18:34 +1000341}
David Gibsonf0517db2005-07-15 17:14:24 +1000342
David Gibson00fbb862016-05-31 11:58:42 +1000343void dt_to_source(FILE *f, struct dt_info *dti)
David Gibsonf0517db2005-07-15 17:14:24 +1000344{
David Gibsonf040d952005-10-24 18:18:38 +1000345 struct reserve_info *re;
David Gibsonf0517db2005-07-15 17:14:24 +1000346
David Gibson91967ac2007-11-07 11:17:37 +1100347 fprintf(f, "/dts-v1/;\n\n");
348
David Gibson00fbb862016-05-31 11:58:42 +1000349 for (re = dti->reservelist; re; re = re->next) {
David Gibson05898c62010-02-24 18:22:17 +1100350 struct label *l;
351
352 for_each_label(re->labels, l)
353 fprintf(f, "%s: ", l->label);
David Gibson91967ac2007-11-07 11:17:37 +1100354 fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
David Gibson49300f22017-03-06 12:04:45 +1100355 (unsigned long long)re->address,
356 (unsigned long long)re->size);
David Gibsonf0517db2005-07-15 17:14:24 +1000357 }
358
David Gibson00fbb862016-05-31 11:58:42 +1000359 write_tree_source_node(f, dti->dt, 0);
David Gibsonf0517db2005-07-15 17:14:24 +1000360}