xref: /openbmc/u-boot/scripts/dtc/treesource.c (revision d6fc90ced42e9fb77185cedfe45d0a2c7ac78508)
1c0e032e0STom Rini /*
2c0e032e0STom Rini  * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
3c0e032e0STom Rini  *
4c0e032e0STom Rini  *
5c0e032e0STom Rini  * This program is free software; you can redistribute it and/or
6c0e032e0STom Rini  * modify it under the terms of the GNU General Public License as
7c0e032e0STom Rini  * published by the Free Software Foundation; either version 2 of the
8c0e032e0STom Rini  * License, or (at your option) any later version.
9c0e032e0STom Rini  *
10c0e032e0STom Rini  *  This program is distributed in the hope that it will be useful,
11c0e032e0STom Rini  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12c0e032e0STom Rini  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13c0e032e0STom Rini  *  General Public License for more details.
14c0e032e0STom Rini  *
15c0e032e0STom Rini  *  You should have received a copy of the GNU General Public License
16c0e032e0STom Rini  *  along with this program; if not, write to the Free Software
17c0e032e0STom Rini  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18c0e032e0STom Rini  *                                                                   USA
19c0e032e0STom Rini  */
20c0e032e0STom Rini 
21c0e032e0STom Rini #include "dtc.h"
22c0e032e0STom Rini #include "srcpos.h"
23c0e032e0STom Rini 
24c0e032e0STom Rini extern FILE *yyin;
25c0e032e0STom Rini extern int yyparse(void);
26c0e032e0STom Rini extern YYLTYPE yylloc;
27c0e032e0STom Rini 
28c0e032e0STom Rini struct dt_info *parser_output;
29c0e032e0STom Rini bool treesource_error;
30c0e032e0STom Rini 
dt_from_source(const char * fname)31c0e032e0STom Rini struct dt_info *dt_from_source(const char *fname)
32c0e032e0STom Rini {
33c0e032e0STom Rini 	parser_output = NULL;
34c0e032e0STom Rini 	treesource_error = false;
35c0e032e0STom Rini 
36c0e032e0STom Rini 	srcfile_push(fname);
37c0e032e0STom Rini 	yyin = current_srcfile->f;
38c0e032e0STom Rini 	yylloc.file = current_srcfile;
39c0e032e0STom Rini 
40c0e032e0STom Rini 	if (yyparse() != 0)
41c0e032e0STom Rini 		die("Unable to parse input tree\n");
42c0e032e0STom Rini 
43c0e032e0STom Rini 	if (treesource_error)
44c0e032e0STom Rini 		die("Syntax error parsing input tree\n");
45c0e032e0STom Rini 
46c0e032e0STom Rini 	return parser_output;
47c0e032e0STom Rini }
48c0e032e0STom Rini 
write_prefix(FILE * f,int level)49c0e032e0STom Rini static void write_prefix(FILE *f, int level)
50c0e032e0STom Rini {
51c0e032e0STom Rini 	int i;
52c0e032e0STom Rini 
53c0e032e0STom Rini 	for (i = 0; i < level; i++)
54c0e032e0STom Rini 		fputc('\t', f);
55c0e032e0STom Rini }
56c0e032e0STom Rini 
isstring(char c)57c0e032e0STom Rini static bool isstring(char c)
58c0e032e0STom Rini {
59c0e032e0STom Rini 	return (isprint((unsigned char)c)
60c0e032e0STom Rini 		|| (c == '\0')
61c0e032e0STom Rini 		|| strchr("\a\b\t\n\v\f\r", c));
62c0e032e0STom Rini }
63c0e032e0STom Rini 
write_propval_string(FILE * f,struct data val)64c0e032e0STom Rini static void write_propval_string(FILE *f, struct data val)
65c0e032e0STom Rini {
66c0e032e0STom Rini 	const char *str = val.val;
67c0e032e0STom Rini 	int i;
68c0e032e0STom Rini 	struct marker *m = val.markers;
69c0e032e0STom Rini 
70c0e032e0STom Rini 	assert(str[val.len-1] == '\0');
71c0e032e0STom Rini 
72c0e032e0STom Rini 	while (m && (m->offset == 0)) {
73c0e032e0STom Rini 		if (m->type == LABEL)
74c0e032e0STom Rini 			fprintf(f, "%s: ", m->ref);
75c0e032e0STom Rini 		m = m->next;
76c0e032e0STom Rini 	}
77c0e032e0STom Rini 	fprintf(f, "\"");
78c0e032e0STom Rini 
79c0e032e0STom Rini 	for (i = 0; i < (val.len-1); i++) {
80c0e032e0STom Rini 		char c = str[i];
81c0e032e0STom Rini 
82c0e032e0STom Rini 		switch (c) {
83c0e032e0STom Rini 		case '\a':
84c0e032e0STom Rini 			fprintf(f, "\\a");
85c0e032e0STom Rini 			break;
86c0e032e0STom Rini 		case '\b':
87c0e032e0STom Rini 			fprintf(f, "\\b");
88c0e032e0STom Rini 			break;
89c0e032e0STom Rini 		case '\t':
90c0e032e0STom Rini 			fprintf(f, "\\t");
91c0e032e0STom Rini 			break;
92c0e032e0STom Rini 		case '\n':
93c0e032e0STom Rini 			fprintf(f, "\\n");
94c0e032e0STom Rini 			break;
95c0e032e0STom Rini 		case '\v':
96c0e032e0STom Rini 			fprintf(f, "\\v");
97c0e032e0STom Rini 			break;
98c0e032e0STom Rini 		case '\f':
99c0e032e0STom Rini 			fprintf(f, "\\f");
100c0e032e0STom Rini 			break;
101c0e032e0STom Rini 		case '\r':
102c0e032e0STom Rini 			fprintf(f, "\\r");
103c0e032e0STom Rini 			break;
104c0e032e0STom Rini 		case '\\':
105c0e032e0STom Rini 			fprintf(f, "\\\\");
106c0e032e0STom Rini 			break;
107c0e032e0STom Rini 		case '\"':
108c0e032e0STom Rini 			fprintf(f, "\\\"");
109c0e032e0STom Rini 			break;
110c0e032e0STom Rini 		case '\0':
111c0e032e0STom Rini 			fprintf(f, "\", ");
112c0e032e0STom Rini 			while (m && (m->offset <= (i + 1))) {
113c0e032e0STom Rini 				if (m->type == LABEL) {
114c0e032e0STom Rini 					assert(m->offset == (i+1));
115c0e032e0STom Rini 					fprintf(f, "%s: ", m->ref);
116c0e032e0STom Rini 				}
117c0e032e0STom Rini 				m = m->next;
118c0e032e0STom Rini 			}
119c0e032e0STom Rini 			fprintf(f, "\"");
120c0e032e0STom Rini 			break;
121c0e032e0STom Rini 		default:
122c0e032e0STom Rini 			if (isprint((unsigned char)c))
123c0e032e0STom Rini 				fprintf(f, "%c", c);
124c0e032e0STom Rini 			else
125c0e032e0STom Rini 				fprintf(f, "\\x%02hhx", c);
126c0e032e0STom Rini 		}
127c0e032e0STom Rini 	}
128c0e032e0STom Rini 	fprintf(f, "\"");
129c0e032e0STom Rini 
130c0e032e0STom Rini 	/* Wrap up any labels at the end of the value */
131c0e032e0STom Rini 	for_each_marker_of_type(m, LABEL) {
132c0e032e0STom Rini 		assert (m->offset == val.len);
133c0e032e0STom Rini 		fprintf(f, " %s:", m->ref);
134c0e032e0STom Rini 	}
135c0e032e0STom Rini }
136c0e032e0STom Rini 
write_propval_cells(FILE * f,struct data val)137c0e032e0STom Rini static void write_propval_cells(FILE *f, struct data val)
138c0e032e0STom Rini {
139c0e032e0STom Rini 	void *propend = val.val + val.len;
140*d6fc90ceSTom Rini 	fdt32_t *cp = (fdt32_t *)val.val;
141c0e032e0STom Rini 	struct marker *m = val.markers;
142c0e032e0STom Rini 
143c0e032e0STom Rini 	fprintf(f, "<");
144c0e032e0STom Rini 	for (;;) {
145c0e032e0STom Rini 		while (m && (m->offset <= ((char *)cp - val.val))) {
146c0e032e0STom Rini 			if (m->type == LABEL) {
147c0e032e0STom Rini 				assert(m->offset == ((char *)cp - val.val));
148c0e032e0STom Rini 				fprintf(f, "%s: ", m->ref);
149c0e032e0STom Rini 			}
150c0e032e0STom Rini 			m = m->next;
151c0e032e0STom Rini 		}
152c0e032e0STom Rini 
153c0e032e0STom Rini 		fprintf(f, "0x%x", fdt32_to_cpu(*cp++));
154c0e032e0STom Rini 		if ((void *)cp >= propend)
155c0e032e0STom Rini 			break;
156c0e032e0STom Rini 		fprintf(f, " ");
157c0e032e0STom Rini 	}
158c0e032e0STom Rini 
159c0e032e0STom Rini 	/* Wrap up any labels at the end of the value */
160c0e032e0STom Rini 	for_each_marker_of_type(m, LABEL) {
161c0e032e0STom Rini 		assert (m->offset == val.len);
162c0e032e0STom Rini 		fprintf(f, " %s:", m->ref);
163c0e032e0STom Rini 	}
164c0e032e0STom Rini 	fprintf(f, ">");
165c0e032e0STom Rini }
166c0e032e0STom Rini 
write_propval_bytes(FILE * f,struct data val)167c0e032e0STom Rini static void write_propval_bytes(FILE *f, struct data val)
168c0e032e0STom Rini {
169c0e032e0STom Rini 	void *propend = val.val + val.len;
170c0e032e0STom Rini 	const char *bp = val.val;
171c0e032e0STom Rini 	struct marker *m = val.markers;
172c0e032e0STom Rini 
173c0e032e0STom Rini 	fprintf(f, "[");
174c0e032e0STom Rini 	for (;;) {
175c0e032e0STom Rini 		while (m && (m->offset == (bp-val.val))) {
176c0e032e0STom Rini 			if (m->type == LABEL)
177c0e032e0STom Rini 				fprintf(f, "%s: ", m->ref);
178c0e032e0STom Rini 			m = m->next;
179c0e032e0STom Rini 		}
180c0e032e0STom Rini 
181c0e032e0STom Rini 		fprintf(f, "%02hhx", (unsigned char)(*bp++));
182c0e032e0STom Rini 		if ((const void *)bp >= propend)
183c0e032e0STom Rini 			break;
184c0e032e0STom Rini 		fprintf(f, " ");
185c0e032e0STom Rini 	}
186c0e032e0STom Rini 
187c0e032e0STom Rini 	/* Wrap up any labels at the end of the value */
188c0e032e0STom Rini 	for_each_marker_of_type(m, LABEL) {
189c0e032e0STom Rini 		assert (m->offset == val.len);
190c0e032e0STom Rini 		fprintf(f, " %s:", m->ref);
191c0e032e0STom Rini 	}
192c0e032e0STom Rini 	fprintf(f, "]");
193c0e032e0STom Rini }
194c0e032e0STom Rini 
write_propval(FILE * f,struct property * prop)195c0e032e0STom Rini static void write_propval(FILE *f, struct property *prop)
196c0e032e0STom Rini {
197c0e032e0STom Rini 	int len = prop->val.len;
198c0e032e0STom Rini 	const char *p = prop->val.val;
199c0e032e0STom Rini 	struct marker *m = prop->val.markers;
200c0e032e0STom Rini 	int nnotstring = 0, nnul = 0;
201c0e032e0STom Rini 	int nnotstringlbl = 0, nnotcelllbl = 0;
202c0e032e0STom Rini 	int i;
203c0e032e0STom Rini 
204c0e032e0STom Rini 	if (len == 0) {
205c0e032e0STom Rini 		fprintf(f, ";\n");
206c0e032e0STom Rini 		return;
207c0e032e0STom Rini 	}
208c0e032e0STom Rini 
209c0e032e0STom Rini 	for (i = 0; i < len; i++) {
210c0e032e0STom Rini 		if (! isstring(p[i]))
211c0e032e0STom Rini 			nnotstring++;
212c0e032e0STom Rini 		if (p[i] == '\0')
213c0e032e0STom Rini 			nnul++;
214c0e032e0STom Rini 	}
215c0e032e0STom Rini 
216c0e032e0STom Rini 	for_each_marker_of_type(m, LABEL) {
217c0e032e0STom Rini 		if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
218c0e032e0STom Rini 			nnotstringlbl++;
219c0e032e0STom Rini 		if ((m->offset % sizeof(cell_t)) != 0)
220c0e032e0STom Rini 			nnotcelllbl++;
221c0e032e0STom Rini 	}
222c0e032e0STom Rini 
223c0e032e0STom Rini 	fprintf(f, " = ");
224c0e032e0STom Rini 	if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
225c0e032e0STom Rini 	    && (nnotstringlbl == 0)) {
226c0e032e0STom Rini 		write_propval_string(f, prop->val);
227c0e032e0STom Rini 	} else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
228c0e032e0STom Rini 		write_propval_cells(f, prop->val);
229c0e032e0STom Rini 	} else {
230c0e032e0STom Rini 		write_propval_bytes(f, prop->val);
231c0e032e0STom Rini 	}
232c0e032e0STom Rini 
233c0e032e0STom Rini 	fprintf(f, ";\n");
234c0e032e0STom Rini }
235c0e032e0STom Rini 
write_tree_source_node(FILE * f,struct node * tree,int level)236c0e032e0STom Rini static void write_tree_source_node(FILE *f, struct node *tree, int level)
237c0e032e0STom Rini {
238c0e032e0STom Rini 	struct property *prop;
239c0e032e0STom Rini 	struct node *child;
240c0e032e0STom Rini 	struct label *l;
241c0e032e0STom Rini 
242c0e032e0STom Rini 	write_prefix(f, level);
243c0e032e0STom Rini 	for_each_label(tree->labels, l)
244c0e032e0STom Rini 		fprintf(f, "%s: ", l->label);
245c0e032e0STom Rini 	if (tree->name && (*tree->name))
246c0e032e0STom Rini 		fprintf(f, "%s {\n", tree->name);
247c0e032e0STom Rini 	else
248c0e032e0STom Rini 		fprintf(f, "/ {\n");
249c0e032e0STom Rini 
250c0e032e0STom Rini 	for_each_property(tree, prop) {
251c0e032e0STom Rini 		write_prefix(f, level+1);
252c0e032e0STom Rini 		for_each_label(prop->labels, l)
253c0e032e0STom Rini 			fprintf(f, "%s: ", l->label);
254c0e032e0STom Rini 		fprintf(f, "%s", prop->name);
255c0e032e0STom Rini 		write_propval(f, prop);
256c0e032e0STom Rini 	}
257c0e032e0STom Rini 	for_each_child(tree, child) {
258c0e032e0STom Rini 		fprintf(f, "\n");
259c0e032e0STom Rini 		write_tree_source_node(f, child, level+1);
260c0e032e0STom Rini 	}
261c0e032e0STom Rini 	write_prefix(f, level);
262c0e032e0STom Rini 	fprintf(f, "};\n");
263c0e032e0STom Rini }
264c0e032e0STom Rini 
265c0e032e0STom Rini 
dt_to_source(FILE * f,struct dt_info * dti)266c0e032e0STom Rini void dt_to_source(FILE *f, struct dt_info *dti)
267c0e032e0STom Rini {
268c0e032e0STom Rini 	struct reserve_info *re;
269c0e032e0STom Rini 
270c0e032e0STom Rini 	fprintf(f, "/dts-v1/;\n\n");
271c0e032e0STom Rini 
272c0e032e0STom Rini 	for (re = dti->reservelist; re; re = re->next) {
273c0e032e0STom Rini 		struct label *l;
274c0e032e0STom Rini 
275c0e032e0STom Rini 		for_each_label(re->labels, l)
276c0e032e0STom Rini 			fprintf(f, "%s: ", l->label);
277c0e032e0STom Rini 		fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
278*d6fc90ceSTom Rini 			(unsigned long long)re->address,
279*d6fc90ceSTom Rini 			(unsigned long long)re->size);
280c0e032e0STom Rini 	}
281c0e032e0STom Rini 
282c0e032e0STom Rini 	write_tree_source_node(f, dti->dt, 0);
283c0e032e0STom Rini }
284c0e032e0STom Rini 
285