2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
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.
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.
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
25 extern int yyparse(void);
26 extern YYLTYPE yylloc;
28 struct boot_info *the_boot_info;
29 bool treesource_error;
31 struct boot_info *dt_from_source(const char *fname)
34 treesource_error = false;
37 yyin = current_srcfile->f;
38 yylloc.file = current_srcfile;
41 die("Unable to parse input tree\n");
44 die("Syntax error parsing input tree\n");
49 static void write_prefix(FILE *f, int level)
53 for (i = 0; i < level; i++)
57 static bool isstring(char c)
59 return (isprint((unsigned char)c)
61 || strchr("\a\b\t\n\v\f\r", c));
64 static void write_propval_string(FILE *f, struct data val)
66 const char *str = val.val;
68 struct marker *m = val.markers;
70 assert(str[val.len-1] == '\0');
72 while (m && (m->offset == 0)) {
74 fprintf(f, "%s: ", m->ref);
79 for (i = 0; i < (val.len-1); i++) {
112 while (m && (m->offset <= (i + 1))) {
113 if (m->type == LABEL) {
114 assert(m->offset == (i+1));
115 fprintf(f, "%s: ", m->ref);
122 if (isprint((unsigned char)c))
125 fprintf(f, "\\x%02hhx", c);
130 /* Wrap up any labels at the end of the value */
131 for_each_marker_of_type(m, LABEL) {
132 assert (m->offset == val.len);
133 fprintf(f, " %s:", m->ref);
137 static void write_propval_cells(FILE *f, struct data val)
139 void *propend = val.val + val.len;
140 cell_t *cp = (cell_t *)val.val;
141 struct marker *m = val.markers;
145 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);
153 fprintf(f, "0x%x", fdt32_to_cpu(*cp++));
154 if ((void *)cp >= propend)
159 /* Wrap up any labels at the end of the value */
160 for_each_marker_of_type(m, LABEL) {
161 assert (m->offset == val.len);
162 fprintf(f, " %s:", m->ref);
167 static void write_propval_bytes(FILE *f, struct data val)
169 void *propend = val.val + val.len;
170 const char *bp = val.val;
171 struct marker *m = val.markers;
175 while (m && (m->offset == (bp-val.val))) {
176 if (m->type == LABEL)
177 fprintf(f, "%s: ", m->ref);
181 fprintf(f, "%02hhx", (unsigned char)(*bp++));
182 if ((const void *)bp >= propend)
187 /* Wrap up any labels at the end of the value */
188 for_each_marker_of_type(m, LABEL) {
189 assert (m->offset == val.len);
190 fprintf(f, " %s:", m->ref);
195 static void write_propval(FILE *f, struct property *prop)
197 int len = prop->val.len;
198 const char *p = prop->val.val;
199 struct marker *m = prop->val.markers;
200 int nnotstring = 0, nnul = 0;
201 int nnotstringlbl = 0, nnotcelllbl = 0;
209 for (i = 0; i < len; i++) {
210 if (! isstring(p[i]))
216 for_each_marker_of_type(m, LABEL) {
217 if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
219 if ((m->offset % sizeof(cell_t)) != 0)
224 if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
225 && (nnotstringlbl == 0)) {
226 write_propval_string(f, prop->val);
227 } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
228 write_propval_cells(f, prop->val);
230 write_propval_bytes(f, prop->val);
236 static void write_tree_source_node(FILE *f, struct node *tree, int level)
238 struct property *prop;
242 write_prefix(f, level);
243 for_each_label(tree->labels, l)
244 fprintf(f, "%s: ", l->label);
245 if (tree->name && (*tree->name))
246 fprintf(f, "%s {\n", tree->name);
250 for_each_property(tree, prop) {
251 write_prefix(f, level+1);
252 for_each_label(prop->labels, l)
253 fprintf(f, "%s: ", l->label);
254 fprintf(f, "%s", prop->name);
255 write_propval(f, prop);
257 for_each_child(tree, child) {
259 write_tree_source_node(f, child, level+1);
261 write_prefix(f, level);
266 void dt_to_source(FILE *f, struct boot_info *bi)
268 struct reserve_info *re;
270 fprintf(f, "/dts-v1/;\n\n");
272 for (re = bi->reservelist; re; re = re->next) {
275 for_each_label(re->labels, l)
276 fprintf(f, "%s: ", l->label);
277 fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
278 (unsigned long long)re->re.address,
279 (unsigned long long)re->re.size);
282 write_tree_source_node(f, bi->dt, 0);