blob: 1af36628b75f86cdf99315f609a217292c008a22 [file] [log] [blame]
David Gibsona4da2e32007-12-18 15:06:42 +11001/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
4 *
5 * 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.
14 *
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
19 */
20
21#include "dtc.h"
22#include "srcpos.h"
23
24extern FILE *yyin;
25extern int yyparse(void);
Stephen Warrencd296722012-09-28 21:25:59 +000026extern YYLTYPE yylloc;
David Gibsona4da2e32007-12-18 15:06:42 +110027
Rob Herring6f05afc2017-01-04 10:45:20 -060028struct dt_info *parser_output;
Rob Herring47605972015-04-29 16:00:05 -050029bool treesource_error;
David Gibsona4da2e32007-12-18 15:06:42 +110030
Rob Herring6f05afc2017-01-04 10:45:20 -060031struct dt_info *dt_from_source(const char *fname)
David Gibsona4da2e32007-12-18 15:06:42 +110032{
Rob Herring6f05afc2017-01-04 10:45:20 -060033 parser_output = NULL;
Rob Herring47605972015-04-29 16:00:05 -050034 treesource_error = false;
David Gibsona4da2e32007-12-18 15:06:42 +110035
John Bonesio658f29a2010-11-17 15:28:20 -080036 srcfile_push(fname);
37 yyin = current_srcfile->f;
Stephen Warrencd296722012-09-28 21:25:59 +000038 yylloc.file = current_srcfile;
David Gibsona4da2e32007-12-18 15:06:42 +110039
40 if (yyparse() != 0)
David Gibsoned95d742008-08-07 12:24:17 +100041 die("Unable to parse input tree\n");
David Gibsona4da2e32007-12-18 15:06:42 +110042
David Gibsoned95d742008-08-07 12:24:17 +100043 if (treesource_error)
44 die("Syntax error parsing input tree\n");
David Gibsona4da2e32007-12-18 15:06:42 +110045
Rob Herring6f05afc2017-01-04 10:45:20 -060046 return parser_output;
David Gibsona4da2e32007-12-18 15:06:42 +110047}
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
Rob Herring47605972015-04-29 16:00:05 -050057static bool isstring(char c)
David Gibsona4da2e32007-12-18 15:06:42 +110058{
Rob Herring47605972015-04-29 16:00:05 -050059 return (isprint((unsigned char)c)
David Gibsona4da2e32007-12-18 15:06:42 +110060 || (c == '\0')
61 || strchr("\a\b\t\n\v\f\r", c));
62}
63
Rob Herringf8589272018-09-13 08:59:25 -050064static void write_propval_string(FILE *f, const char *s, size_t len)
David Gibsona4da2e32007-12-18 15:06:42 +110065{
Rob Herringf8589272018-09-13 08:59:25 -050066 const char *end = s + len - 1;
Rob Herringc2e70752018-11-28 18:37:35 -060067
68 if (!len)
69 return;
70
Rob Herringf8589272018-09-13 08:59:25 -050071 assert(*end == '\0');
David Gibsona4da2e32007-12-18 15:06:42 +110072
John Bonesio658f29a2010-11-17 15:28:20 -080073 fprintf(f, "\"");
Rob Herringf8589272018-09-13 08:59:25 -050074 while (s < end) {
75 char c = *s++;
David Gibsona4da2e32007-12-18 15:06:42 +110076 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':
Rob Herringf8589272018-09-13 08:59:25 -0500105 fprintf(f, "\\0");
David Gibsona4da2e32007-12-18 15:06:42 +1100106 break;
107 default:
Rob Herring47605972015-04-29 16:00:05 -0500108 if (isprint((unsigned char)c))
David Gibsona4da2e32007-12-18 15:06:42 +1100109 fprintf(f, "%c", c);
110 else
Rob Herringf8589272018-09-13 08:59:25 -0500111 fprintf(f, "\\x%02"PRIx8, c);
David Gibsona4da2e32007-12-18 15:06:42 +1100112 }
113 }
114 fprintf(f, "\"");
David Gibsona4da2e32007-12-18 15:06:42 +1100115}
116
Rob Herringf8589272018-09-13 08:59:25 -0500117static void write_propval_int(FILE *f, const char *p, size_t len, size_t width)
David Gibsona4da2e32007-12-18 15:06:42 +1100118{
Rob Herringf8589272018-09-13 08:59:25 -0500119 const char *end = p + len;
120 assert(len % width == 0);
David Gibsona4da2e32007-12-18 15:06:42 +1100121
Rob Herringf8589272018-09-13 08:59:25 -0500122 for (; p < end; p += width) {
123 switch (width) {
124 case 1:
Rob Herringc2e70752018-11-28 18:37:35 -0600125 fprintf(f, "%02"PRIx8, *(const uint8_t*)p);
David Gibsona4da2e32007-12-18 15:06:42 +1100126 break;
Rob Herringf8589272018-09-13 08:59:25 -0500127 case 2:
Rob Herringc2e70752018-11-28 18:37:35 -0600128 fprintf(f, "0x%02"PRIx16, fdt16_to_cpu(*(const fdt16_t*)p));
Rob Herringf8589272018-09-13 08:59:25 -0500129 break;
130 case 4:
Rob Herringc2e70752018-11-28 18:37:35 -0600131 fprintf(f, "0x%02"PRIx32, fdt32_to_cpu(*(const fdt32_t*)p));
Rob Herringf8589272018-09-13 08:59:25 -0500132 break;
133 case 8:
Rob Herringc2e70752018-11-28 18:37:35 -0600134 fprintf(f, "0x%02"PRIx64, fdt64_to_cpu(*(const fdt64_t*)p));
Rob Herringf8589272018-09-13 08:59:25 -0500135 break;
136 }
Rob Herringc2e70752018-11-28 18:37:35 -0600137 if (p + width < end)
138 fputc(' ', f);
David Gibsona4da2e32007-12-18 15:06:42 +1100139 }
David Gibsona4da2e32007-12-18 15:06:42 +1100140}
141
Rob Herringf8589272018-09-13 08:59:25 -0500142static bool has_data_type_information(struct marker *m)
David Gibsona4da2e32007-12-18 15:06:42 +1100143{
Rob Herringf8589272018-09-13 08:59:25 -0500144 return m->type >= TYPE_UINT8;
David Gibsona4da2e32007-12-18 15:06:42 +1100145}
146
Rob Herringf8589272018-09-13 08:59:25 -0500147static struct marker *next_type_marker(struct marker *m)
148{
149 while (m && !has_data_type_information(m))
150 m = m->next;
151 return m;
152}
153
154size_t type_marker_length(struct marker *m)
155{
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 Herringc2e70752018-11-28 18:37:35 -0600171 [TYPE_UINT8] = "]",
172 [TYPE_UINT16] = ">",
173 [TYPE_UINT32] = ">",
174 [TYPE_UINT64] = ">",
Rob Herringf8589272018-09-13 08:59:25 -0500175 [TYPE_STRING] = "",
176};
177
178static enum markertype guess_value_type(struct property *prop)
David Gibsona4da2e32007-12-18 15:06:42 +1100179{
180 int len = prop->val.len;
181 const char *p = prop->val.val;
182 struct marker *m = prop->val.markers;
183 int nnotstring = 0, nnul = 0;
184 int nnotstringlbl = 0, nnotcelllbl = 0;
185 int i;
186
David Gibsona4da2e32007-12-18 15:06:42 +1100187 for (i = 0; i < len; i++) {
188 if (! isstring(p[i]))
189 nnotstring++;
190 if (p[i] == '\0')
191 nnul++;
192 }
193
194 for_each_marker_of_type(m, LABEL) {
195 if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
196 nnotstringlbl++;
197 if ((m->offset % sizeof(cell_t)) != 0)
198 nnotcelllbl++;
199 }
200
David Gibsona4da2e32007-12-18 15:06:42 +1100201 if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
202 && (nnotstringlbl == 0)) {
Rob Herringf8589272018-09-13 08:59:25 -0500203 return TYPE_STRING;
David Gibsona4da2e32007-12-18 15:06:42 +1100204 } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
Rob Herringf8589272018-09-13 08:59:25 -0500205 return TYPE_UINT32;
David Gibsona4da2e32007-12-18 15:06:42 +1100206 }
207
Rob Herringf8589272018-09-13 08:59:25 -0500208 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;
Rob Herringc2e70752018-11-28 18:37:35 -0600217 char *srcstr;
Rob Herringf8589272018-09-13 08:59:25 -0500218
219 if (len == 0) {
Rob Herringc2e70752018-11-28 18:37:35 -0600220 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");
Rob Herringf8589272018-09-13 08:59:25 -0500229 return;
230 }
231
Rob Herringc2e70752018-11-28 18:37:35 -0600232 fprintf(f, " =");
Rob Herringf8589272018-09-13 08:59:25 -0500233
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
Rob Herringf8589272018-09-13 08:59:25 -0500243 for_each_marker(m) {
Rob Herringc2e70752018-11-28 18:37:35 -0600244 size_t chunk_len = (m->next ? m->next->offset : len) - m->offset;
245 size_t data_len = type_marker_length(m) ? : len - m->offset;
Rob Herringf8589272018-09-13 08:59:25 -0500246 const char *p = &prop->val.val[m->offset];
247
Rob Herringc2e70752018-11-28 18:37:35 -0600248 if (has_data_type_information(m)) {
249 emit_type = m->type;
250 fprintf(f, " %s", delim_start[emit_type]);
251 } else if (m->type == LABEL)
252 fprintf(f, " %s:", m->ref);
253 else if (m->offset)
254 fputc(' ', f);
255
256 if (emit_type == TYPE_NONE) {
257 assert(chunk_len == 0);
Rob Herringf8589272018-09-13 08:59:25 -0500258 continue;
Rob Herringf8589272018-09-13 08:59:25 -0500259 }
260
Rob Herringf8589272018-09-13 08:59:25 -0500261 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 }
Rob Herringf8589272018-09-13 08:59:25 -0500277
Rob Herringc2e70752018-11-28 18:37:35 -0600278 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 }
Rob Herringf8589272018-09-13 08:59:25 -0500284 }
Rob Herringc2e70752018-11-28 18:37:35 -0600285 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 Gibsona4da2e32007-12-18 15:06:42 +1100294}
295
296static void write_tree_source_node(FILE *f, struct node *tree, int level)
297{
298 struct property *prop;
299 struct node *child;
John Bonesio658f29a2010-11-17 15:28:20 -0800300 struct label *l;
Rob Herringc2e70752018-11-28 18:37:35 -0600301 char *srcstr;
David Gibsona4da2e32007-12-18 15:06:42 +1100302
303 write_prefix(f, level);
John Bonesio658f29a2010-11-17 15:28:20 -0800304 for_each_label(tree->labels, l)
305 fprintf(f, "%s: ", l->label);
David Gibsona4da2e32007-12-18 15:06:42 +1100306 if (tree->name && (*tree->name))
Rob Herringc2e70752018-11-28 18:37:35 -0600307 fprintf(f, "%s {", tree->name);
David Gibsona4da2e32007-12-18 15:06:42 +1100308 else
Rob Herringc2e70752018-11-28 18:37:35 -0600309 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 Gibsona4da2e32007-12-18 15:06:42 +1100319
320 for_each_property(tree, prop) {
321 write_prefix(f, level+1);
John Bonesio658f29a2010-11-17 15:28:20 -0800322 for_each_label(prop->labels, l)
323 fprintf(f, "%s: ", l->label);
David Gibsona4da2e32007-12-18 15:06:42 +1100324 fprintf(f, "%s", prop->name);
325 write_propval(f, prop);
326 }
327 for_each_child(tree, child) {
328 fprintf(f, "\n");
329 write_tree_source_node(f, child, level+1);
330 }
331 write_prefix(f, level);
Rob Herringc2e70752018-11-28 18:37:35 -0600332 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 Gibsona4da2e32007-12-18 15:06:42 +1100341}
342
Rob Herring6f05afc2017-01-04 10:45:20 -0600343void dt_to_source(FILE *f, struct dt_info *dti)
David Gibsona4da2e32007-12-18 15:06:42 +1100344{
345 struct reserve_info *re;
346
347 fprintf(f, "/dts-v1/;\n\n");
348
Rob Herring6f05afc2017-01-04 10:45:20 -0600349 for (re = dti->reservelist; re; re = re->next) {
John Bonesio658f29a2010-11-17 15:28:20 -0800350 struct label *l;
351
352 for_each_label(re->labels, l)
353 fprintf(f, "%s: ", l->label);
David Gibsona4da2e32007-12-18 15:06:42 +1100354 fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
Rob Herring89d12312017-03-21 09:01:08 -0500355 (unsigned long long)re->address,
356 (unsigned long long)re->size);
David Gibsona4da2e32007-12-18 15:06:42 +1100357 }
358
Rob Herring6f05afc2017-01-04 10:45:20 -0600359 write_tree_source_node(f, dti->dt, 0);
David Gibsona4da2e32007-12-18 15:06:42 +1100360}