blob: 33eeba55fb4dd6482193902e784dbb1a62368f3e [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 Gibsonf0517db2005-07-15 17:14:24 +100028struct boot_info *the_boot_info;
Scott Woodad4f54a2008-01-03 17:43:33 -060029int treesource_error;
David Gibsonf0517db2005-07-15 17:14:24 +100030
Jon Loeligere45e6fd2007-03-23 15:18:41 -050031struct boot_info *dt_from_source(const char *fname)
David Gibsonf0517db2005-07-15 17:14:24 +100032{
33 the_boot_info = NULL;
Scott Woodad4f54a2008-01-03 17:43:33 -060034 treesource_error = 0;
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 Gibsonf0517db2005-07-15 17:14:24 +100046 return the_boot_info;
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 Gibson01a2d8a2008-08-04 15:30:13 +100057static int isstring(char c)
David Gibson5a98ddd2007-10-17 12:39:10 +100058{
59 return (isprint(c)
60 || (c == '\0')
61 || strchr("\a\b\t\n\v\f\r", c));
62}
63
David Gibson5a98ddd2007-10-17 12:39:10 +100064static void write_propval_string(FILE *f, struct data val)
65{
David Gibson92cb9a22007-12-04 14:26:15 +110066 const char *str = val.val;
David Gibson5a98ddd2007-10-17 12:39:10 +100067 int i;
David Gibsondc941772007-11-22 14:39:23 +110068 struct marker *m = val.markers;
David Gibson5a98ddd2007-10-17 12:39:10 +100069
70 assert(str[val.len-1] == '\0');
71
David Gibsonc623fe52009-09-09 14:38:30 +100072 while (m && (m->offset == 0)) {
73 if (m->type == LABEL)
74 fprintf(f, "%s: ", m->ref);
75 m = m->next;
76 }
77 fprintf(f, "\"");
78
David Gibson5a98ddd2007-10-17 12:39:10 +100079 for (i = 0; i < (val.len-1); i++) {
80 char c = str[i];
81
82 switch (c) {
83 case '\a':
84 fprintf(f, "\\a");
85 break;
86 case '\b':
87 fprintf(f, "\\b");
88 break;
89 case '\t':
90 fprintf(f, "\\t");
91 break;
92 case '\n':
93 fprintf(f, "\\n");
94 break;
95 case '\v':
96 fprintf(f, "\\v");
97 break;
98 case '\f':
99 fprintf(f, "\\f");
100 break;
101 case '\r':
102 fprintf(f, "\\r");
103 break;
104 case '\\':
105 fprintf(f, "\\\\");
106 break;
107 case '\"':
108 fprintf(f, "\\\"");
109 break;
110 case '\0':
David Gibsond3ea6e52007-11-07 10:22:25 +1100111 fprintf(f, "\", ");
David Gibsonc623fe52009-09-09 14:38:30 +1000112 while (m && (m->offset < i)) {
113 if (m->type == LABEL) {
114 assert(m->offset == (i+1));
115 fprintf(f, "%s: ", m->ref);
116 }
117 m = m->next;
118 }
119 fprintf(f, "\"");
David Gibson5a98ddd2007-10-17 12:39:10 +1000120 break;
121 default:
122 if (isprint(c))
123 fprintf(f, "%c", c);
124 else
125 fprintf(f, "\\x%02hhx", c);
126 }
127 }
David Gibsoned01ae42007-11-07 10:21:20 +1100128 fprintf(f, "\"");
David Gibsond3ea6e52007-11-07 10:22:25 +1100129
130 /* Wrap up any labels at the end of the value */
David Gibsondc941772007-11-22 14:39:23 +1100131 for_each_marker_of_type(m, LABEL) {
132 assert (m->offset == val.len);
133 fprintf(f, " %s:", m->ref);
David Gibsond3ea6e52007-11-07 10:22:25 +1100134 }
David Gibson5a98ddd2007-10-17 12:39:10 +1000135}
136
137static void write_propval_cells(FILE *f, struct data val)
138{
139 void *propend = val.val + val.len;
140 cell_t *cp = (cell_t *)val.val;
David Gibsondc941772007-11-22 14:39:23 +1100141 struct marker *m = val.markers;
David Gibson5a98ddd2007-10-17 12:39:10 +1000142
David Gibsoned01ae42007-11-07 10:21:20 +1100143 fprintf(f, "<");
David Gibson5a98ddd2007-10-17 12:39:10 +1000144 for (;;) {
David Gibsondc941772007-11-22 14:39:23 +1100145 while (m && (m->offset <= ((char *)cp - val.val))) {
146 if (m->type == LABEL) {
147 assert(m->offset == ((char *)cp - val.val));
148 fprintf(f, "%s: ", m->ref);
149 }
150 m = m->next;
David Gibsond3ea6e52007-11-07 10:22:25 +1100151 }
152
David Gibsonc8c374b2008-06-25 14:27:53 +1000153 fprintf(f, "0x%x", fdt32_to_cpu(*cp++));
David Gibson5a98ddd2007-10-17 12:39:10 +1000154 if ((void *)cp >= propend)
155 break;
156 fprintf(f, " ");
157 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100158
159 /* Wrap up any labels at the end of the value */
David Gibsondc941772007-11-22 14:39:23 +1100160 for_each_marker_of_type(m, LABEL) {
161 assert (m->offset == val.len);
162 fprintf(f, " %s:", m->ref);
David Gibsond3ea6e52007-11-07 10:22:25 +1100163 }
David Gibsoned01ae42007-11-07 10:21:20 +1100164 fprintf(f, ">");
David Gibson5a98ddd2007-10-17 12:39:10 +1000165}
166
167static void write_propval_bytes(FILE *f, struct data val)
168{
169 void *propend = val.val + val.len;
David Gibson92cb9a22007-12-04 14:26:15 +1100170 const char *bp = val.val;
David Gibsondc941772007-11-22 14:39:23 +1100171 struct marker *m = val.markers;
David Gibson5a98ddd2007-10-17 12:39:10 +1000172
David Gibsoned01ae42007-11-07 10:21:20 +1100173 fprintf(f, "[");
David Gibson5a98ddd2007-10-17 12:39:10 +1000174 for (;;) {
David Gibsondc941772007-11-22 14:39:23 +1100175 while (m && (m->offset == (bp-val.val))) {
176 if (m->type == LABEL)
177 fprintf(f, "%s: ", m->ref);
178 m = m->next;
David Gibsond3ea6e52007-11-07 10:22:25 +1100179 }
180
David Gibson5a98ddd2007-10-17 12:39:10 +1000181 fprintf(f, "%02hhx", *bp++);
David Gibson14090972008-07-07 10:14:15 +1000182 if ((const void *)bp >= propend)
David Gibson5a98ddd2007-10-17 12:39:10 +1000183 break;
184 fprintf(f, " ");
185 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100186
187 /* Wrap up any labels at the end of the value */
David Gibsondc941772007-11-22 14:39:23 +1100188 for_each_marker_of_type(m, LABEL) {
189 assert (m->offset == val.len);
190 fprintf(f, " %s:", m->ref);
David Gibsond3ea6e52007-11-07 10:22:25 +1100191 }
David Gibsoned01ae42007-11-07 10:21:20 +1100192 fprintf(f, "]");
193}
194
195static void write_propval(FILE *f, struct property *prop)
196{
197 int len = prop->val.len;
David Gibson92cb9a22007-12-04 14:26:15 +1100198 const char *p = prop->val.val;
David Gibsondc941772007-11-22 14:39:23 +1100199 struct marker *m = prop->val.markers;
David Gibsoned01ae42007-11-07 10:21:20 +1100200 int nnotstring = 0, nnul = 0;
David Gibsond3ea6e52007-11-07 10:22:25 +1100201 int nnotstringlbl = 0, nnotcelllbl = 0;
David Gibsoned01ae42007-11-07 10:21:20 +1100202 int i;
203
204 if (len == 0) {
205 fprintf(f, ";\n");
206 return;
207 }
208
209 for (i = 0; i < len; i++) {
210 if (! isstring(p[i]))
211 nnotstring++;
212 if (p[i] == '\0')
213 nnul++;
214 }
215
David Gibsondc941772007-11-22 14:39:23 +1100216 for_each_marker_of_type(m, LABEL) {
217 if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
David Gibsond3ea6e52007-11-07 10:22:25 +1100218 nnotstringlbl++;
David Gibsondc941772007-11-22 14:39:23 +1100219 if ((m->offset % sizeof(cell_t)) != 0)
David Gibsond3ea6e52007-11-07 10:22:25 +1100220 nnotcelllbl++;
221 }
David Gibsoned01ae42007-11-07 10:21:20 +1100222
David Gibsond3ea6e52007-11-07 10:22:25 +1100223 fprintf(f, " = ");
224 if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
225 && (nnotstringlbl == 0)) {
David Gibsoned01ae42007-11-07 10:21:20 +1100226 write_propval_string(f, prop->val);
David Gibsond3ea6e52007-11-07 10:22:25 +1100227 } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
David Gibsoned01ae42007-11-07 10:21:20 +1100228 write_propval_cells(f, prop->val);
229 } else {
230 write_propval_bytes(f, prop->val);
231 }
David Gibsond3ea6e52007-11-07 10:22:25 +1100232
David Gibsoned01ae42007-11-07 10:21:20 +1100233 fprintf(f, ";\n");
David Gibson5a98ddd2007-10-17 12:39:10 +1000234}
David Gibsonf0517db2005-07-15 17:14:24 +1000235
David Gibson230f2532005-08-29 12:48:02 +1000236static void write_tree_source_node(FILE *f, struct node *tree, int level)
David Gibsonfc14dad2005-06-08 17:18:34 +1000237{
238 struct property *prop;
239 struct node *child;
David Gibson05898c62010-02-24 18:22:17 +1100240 struct label *l;
David Gibsonfc14dad2005-06-08 17:18:34 +1000241
242 write_prefix(f, level);
David Gibson05898c62010-02-24 18:22:17 +1100243 for_each_label(tree->labels, l)
244 fprintf(f, "%s: ", l->label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000245 if (tree->name && (*tree->name))
246 fprintf(f, "%s {\n", tree->name);
247 else
248 fprintf(f, "/ {\n");
249
250 for_each_property(tree, prop) {
David Gibson02563ad2007-11-02 16:10:30 +1100251 write_prefix(f, level+1);
David Gibson05898c62010-02-24 18:22:17 +1100252 for_each_label(prop->labels, l)
253 fprintf(f, "%s: ", l->label);
David Gibson02563ad2007-11-02 16:10:30 +1100254 fprintf(f, "%s", prop->name);
David Gibsoned01ae42007-11-07 10:21:20 +1100255 write_propval(f, prop);
David Gibsonfc14dad2005-06-08 17:18:34 +1000256 }
257 for_each_child(tree, child) {
258 fprintf(f, "\n");
David Gibsonf0517db2005-07-15 17:14:24 +1000259 write_tree_source_node(f, child, level+1);
David Gibsonfc14dad2005-06-08 17:18:34 +1000260 }
261 write_prefix(f, level);
262 fprintf(f, "};\n");
263}
David Gibsonf0517db2005-07-15 17:14:24 +1000264
265
David Gibson712e52e2005-10-26 16:56:26 +1000266void dt_to_source(FILE *f, struct boot_info *bi)
David Gibsonf0517db2005-07-15 17:14:24 +1000267{
David Gibsonf040d952005-10-24 18:18:38 +1000268 struct reserve_info *re;
David Gibsonf0517db2005-07-15 17:14:24 +1000269
David Gibson91967ac2007-11-07 11:17:37 +1100270 fprintf(f, "/dts-v1/;\n\n");
271
David Gibsonf040d952005-10-24 18:18:38 +1000272 for (re = bi->reservelist; re; re = re->next) {
David Gibson05898c62010-02-24 18:22:17 +1100273 struct label *l;
274
275 for_each_label(re->labels, l)
276 fprintf(f, "%s: ", l->label);
David Gibson91967ac2007-11-07 11:17:37 +1100277 fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
David Gibsonf040d952005-10-24 18:18:38 +1000278 (unsigned long long)re->re.address,
David Gibson91967ac2007-11-07 11:17:37 +1100279 (unsigned long long)re->re.size);
David Gibsonf0517db2005-07-15 17:14:24 +1000280 }
281
282 write_tree_source_node(f, bi->dt, 0);
283}
284