blob: f2874f1d1465cbccb9384de5a6880208288bd708 [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;
67 assert(*end == '\0');
David Gibsona4da2e32007-12-18 15:06:42 +110068
John Bonesio658f29a2010-11-17 15:28:20 -080069 fprintf(f, "\"");
Rob Herringf8589272018-09-13 08:59:25 -050070 while (s < end) {
71 char c = *s++;
David Gibsona4da2e32007-12-18 15:06:42 +110072 switch (c) {
73 case '\a':
74 fprintf(f, "\\a");
75 break;
76 case '\b':
77 fprintf(f, "\\b");
78 break;
79 case '\t':
80 fprintf(f, "\\t");
81 break;
82 case '\n':
83 fprintf(f, "\\n");
84 break;
85 case '\v':
86 fprintf(f, "\\v");
87 break;
88 case '\f':
89 fprintf(f, "\\f");
90 break;
91 case '\r':
92 fprintf(f, "\\r");
93 break;
94 case '\\':
95 fprintf(f, "\\\\");
96 break;
97 case '\"':
98 fprintf(f, "\\\"");
99 break;
100 case '\0':
Rob Herringf8589272018-09-13 08:59:25 -0500101 fprintf(f, "\\0");
David Gibsona4da2e32007-12-18 15:06:42 +1100102 break;
103 default:
Rob Herring47605972015-04-29 16:00:05 -0500104 if (isprint((unsigned char)c))
David Gibsona4da2e32007-12-18 15:06:42 +1100105 fprintf(f, "%c", c);
106 else
Rob Herringf8589272018-09-13 08:59:25 -0500107 fprintf(f, "\\x%02"PRIx8, c);
David Gibsona4da2e32007-12-18 15:06:42 +1100108 }
109 }
110 fprintf(f, "\"");
David Gibsona4da2e32007-12-18 15:06:42 +1100111}
112
Rob Herringf8589272018-09-13 08:59:25 -0500113static void write_propval_int(FILE *f, const char *p, size_t len, size_t width)
David Gibsona4da2e32007-12-18 15:06:42 +1100114{
Rob Herringf8589272018-09-13 08:59:25 -0500115 const char *end = p + len;
116 assert(len % width == 0);
David Gibsona4da2e32007-12-18 15:06:42 +1100117
Rob Herringf8589272018-09-13 08:59:25 -0500118 for (; p < end; p += width) {
119 switch (width) {
120 case 1:
121 fprintf(f, " %02"PRIx8, *(const uint8_t*)p);
David Gibsona4da2e32007-12-18 15:06:42 +1100122 break;
Rob Herringf8589272018-09-13 08:59:25 -0500123 case 2:
124 fprintf(f, " 0x%02"PRIx16, fdt16_to_cpu(*(const fdt16_t*)p));
125 break;
126 case 4:
127 fprintf(f, " 0x%02"PRIx32, fdt32_to_cpu(*(const fdt32_t*)p));
128 break;
129 case 8:
130 fprintf(f, " 0x%02"PRIx64, fdt64_to_cpu(*(const fdt64_t*)p));
131 break;
132 }
David Gibsona4da2e32007-12-18 15:06:42 +1100133 }
David Gibsona4da2e32007-12-18 15:06:42 +1100134}
135
Rob Herringf8589272018-09-13 08:59:25 -0500136static bool has_data_type_information(struct marker *m)
David Gibsona4da2e32007-12-18 15:06:42 +1100137{
Rob Herringf8589272018-09-13 08:59:25 -0500138 return m->type >= TYPE_UINT8;
David Gibsona4da2e32007-12-18 15:06:42 +1100139}
140
Rob Herringf8589272018-09-13 08:59:25 -0500141static struct marker *next_type_marker(struct marker *m)
142{
143 while (m && !has_data_type_information(m))
144 m = m->next;
145 return m;
146}
147
148size_t type_marker_length(struct marker *m)
149{
150 struct marker *next = next_type_marker(m->next);
151
152 if (next)
153 return next->offset - m->offset;
154 return 0;
155}
156
157static const char *delim_start[] = {
158 [TYPE_UINT8] = "[",
159 [TYPE_UINT16] = "/bits/ 16 <",
160 [TYPE_UINT32] = "<",
161 [TYPE_UINT64] = "/bits/ 64 <",
162 [TYPE_STRING] = "",
163};
164static const char *delim_end[] = {
165 [TYPE_UINT8] = " ]",
166 [TYPE_UINT16] = " >",
167 [TYPE_UINT32] = " >",
168 [TYPE_UINT64] = " >",
169 [TYPE_STRING] = "",
170};
171
172static enum markertype guess_value_type(struct property *prop)
David Gibsona4da2e32007-12-18 15:06:42 +1100173{
174 int len = prop->val.len;
175 const char *p = prop->val.val;
176 struct marker *m = prop->val.markers;
177 int nnotstring = 0, nnul = 0;
178 int nnotstringlbl = 0, nnotcelllbl = 0;
179 int i;
180
David Gibsona4da2e32007-12-18 15:06:42 +1100181 for (i = 0; i < len; i++) {
182 if (! isstring(p[i]))
183 nnotstring++;
184 if (p[i] == '\0')
185 nnul++;
186 }
187
188 for_each_marker_of_type(m, LABEL) {
189 if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
190 nnotstringlbl++;
191 if ((m->offset % sizeof(cell_t)) != 0)
192 nnotcelllbl++;
193 }
194
David Gibsona4da2e32007-12-18 15:06:42 +1100195 if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
196 && (nnotstringlbl == 0)) {
Rob Herringf8589272018-09-13 08:59:25 -0500197 return TYPE_STRING;
David Gibsona4da2e32007-12-18 15:06:42 +1100198 } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
Rob Herringf8589272018-09-13 08:59:25 -0500199 return TYPE_UINT32;
David Gibsona4da2e32007-12-18 15:06:42 +1100200 }
201
Rob Herringf8589272018-09-13 08:59:25 -0500202 return TYPE_UINT8;
203}
204
205static void write_propval(FILE *f, struct property *prop)
206{
207 size_t len = prop->val.len;
208 struct marker *m = prop->val.markers;
209 struct marker dummy_marker;
210 enum markertype emit_type = TYPE_NONE;
211
212 if (len == 0) {
213 fprintf(f, ";\n");
214 return;
215 }
216
217 fprintf(f, " = ");
218
219 if (!next_type_marker(m)) {
220 /* data type information missing, need to guess */
221 dummy_marker.type = guess_value_type(prop);
222 dummy_marker.next = prop->val.markers;
223 dummy_marker.offset = 0;
224 dummy_marker.ref = NULL;
225 m = &dummy_marker;
226 }
227
228 struct marker *m_label = prop->val.markers;
229 for_each_marker(m) {
230 size_t chunk_len;
231 const char *p = &prop->val.val[m->offset];
232
233 if (!has_data_type_information(m))
234 continue;
235
236 chunk_len = type_marker_length(m);
237 if (!chunk_len)
238 chunk_len = len - m->offset;
239
240 if (emit_type != TYPE_NONE)
241 fprintf(f, "%s, ", delim_end[emit_type]);
242 emit_type = m->type;
243
244 for_each_marker_of_type(m_label, LABEL) {
245 if (m_label->offset > m->offset)
246 break;
247 fprintf(f, "%s: ", m_label->ref);
248 }
249
250 fprintf(f, "%s", delim_start[emit_type]);
251
252 if (chunk_len <= 0)
253 continue;
254
255 switch(emit_type) {
256 case TYPE_UINT16:
257 write_propval_int(f, p, chunk_len, 2);
258 break;
259 case TYPE_UINT32:
260 write_propval_int(f, p, chunk_len, 4);
261 break;
262 case TYPE_UINT64:
263 write_propval_int(f, p, chunk_len, 8);
264 break;
265 case TYPE_STRING:
266 write_propval_string(f, p, chunk_len);
267 break;
268 default:
269 write_propval_int(f, p, chunk_len, 1);
270 }
271 }
272
273 /* Wrap up any labels at the end of the value */
274 for_each_marker_of_type(m_label, LABEL) {
275 assert (m_label->offset == len);
276 fprintf(f, " %s:", m_label->ref);
277 }
278
279 fprintf(f, "%s;\n", delim_end[emit_type] ? : "");
David Gibsona4da2e32007-12-18 15:06:42 +1100280}
281
282static void write_tree_source_node(FILE *f, struct node *tree, int level)
283{
284 struct property *prop;
285 struct node *child;
John Bonesio658f29a2010-11-17 15:28:20 -0800286 struct label *l;
David Gibsona4da2e32007-12-18 15:06:42 +1100287
288 write_prefix(f, level);
John Bonesio658f29a2010-11-17 15:28:20 -0800289 for_each_label(tree->labels, l)
290 fprintf(f, "%s: ", l->label);
David Gibsona4da2e32007-12-18 15:06:42 +1100291 if (tree->name && (*tree->name))
292 fprintf(f, "%s {\n", tree->name);
293 else
294 fprintf(f, "/ {\n");
295
296 for_each_property(tree, prop) {
297 write_prefix(f, level+1);
John Bonesio658f29a2010-11-17 15:28:20 -0800298 for_each_label(prop->labels, l)
299 fprintf(f, "%s: ", l->label);
David Gibsona4da2e32007-12-18 15:06:42 +1100300 fprintf(f, "%s", prop->name);
301 write_propval(f, prop);
302 }
303 for_each_child(tree, child) {
304 fprintf(f, "\n");
305 write_tree_source_node(f, child, level+1);
306 }
307 write_prefix(f, level);
308 fprintf(f, "};\n");
309}
310
311
Rob Herring6f05afc2017-01-04 10:45:20 -0600312void dt_to_source(FILE *f, struct dt_info *dti)
David Gibsona4da2e32007-12-18 15:06:42 +1100313{
314 struct reserve_info *re;
315
316 fprintf(f, "/dts-v1/;\n\n");
317
Rob Herring6f05afc2017-01-04 10:45:20 -0600318 for (re = dti->reservelist; re; re = re->next) {
John Bonesio658f29a2010-11-17 15:28:20 -0800319 struct label *l;
320
321 for_each_label(re->labels, l)
322 fprintf(f, "%s: ", l->label);
David Gibsona4da2e32007-12-18 15:06:42 +1100323 fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
Rob Herring89d12312017-03-21 09:01:08 -0500324 (unsigned long long)re->address,
325 (unsigned long long)re->size);
David Gibsona4da2e32007-12-18 15:06:42 +1100326 }
327
Rob Herring6f05afc2017-01-04 10:45:20 -0600328 write_tree_source_node(f, dti->dt, 0);
David Gibsona4da2e32007-12-18 15:06:42 +1100329}