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);
27 struct boot_info
*the_boot_info
;
30 struct boot_info
*dt_from_source(const char *fname
)
36 yyin
= current_srcfile
->f
;
39 die("Unable to parse input tree\n");
42 die("Syntax error parsing input tree\n");
47 static void write_prefix(FILE *f
, int level
)
51 for (i
= 0; i
< level
; i
++)
55 static int isstring(char c
)
59 || strchr("\a\b\t\n\v\f\r", c
));
62 static void write_propval_string(FILE *f
, struct data val
)
64 const char *str
= val
.val
;
66 struct marker
*m
= val
.markers
;
68 assert(str
[val
.len
-1] == '\0');
70 while (m
&& (m
->offset
== 0)) {
72 fprintf(f
, "%s: ", m
->ref
);
77 for (i
= 0; i
< (val
.len
-1); i
++) {
110 while (m
&& (m
->offset
< i
)) {
111 if (m
->type
== LABEL
) {
112 assert(m
->offset
== (i
+1));
113 fprintf(f
, "%s: ", m
->ref
);
123 fprintf(f
, "\\x%02hhx", c
);
128 /* Wrap up any labels at the end of the value */
129 for_each_marker_of_type(m
, LABEL
) {
130 assert (m
->offset
== val
.len
);
131 fprintf(f
, " %s:", m
->ref
);
135 static void write_propval_cells(FILE *f
, struct data val
)
137 void *propend
= val
.val
+ val
.len
;
138 cell_t
*cp
= (cell_t
*)val
.val
;
139 struct marker
*m
= val
.markers
;
143 while (m
&& (m
->offset
<= ((char *)cp
- val
.val
))) {
144 if (m
->type
== LABEL
) {
145 assert(m
->offset
== ((char *)cp
- val
.val
));
146 fprintf(f
, "%s: ", m
->ref
);
151 fprintf(f
, "0x%x", fdt32_to_cpu(*cp
++));
152 if ((void *)cp
>= propend
)
157 /* Wrap up any labels at the end of the value */
158 for_each_marker_of_type(m
, LABEL
) {
159 assert (m
->offset
== val
.len
);
160 fprintf(f
, " %s:", m
->ref
);
165 static void write_propval_bytes(FILE *f
, struct data val
)
167 void *propend
= val
.val
+ val
.len
;
168 const char *bp
= val
.val
;
169 struct marker
*m
= val
.markers
;
173 while (m
&& (m
->offset
== (bp
-val
.val
))) {
174 if (m
->type
== LABEL
)
175 fprintf(f
, "%s: ", m
->ref
);
179 fprintf(f
, "%02hhx", *bp
++);
180 if ((const void *)bp
>= propend
)
185 /* Wrap up any labels at the end of the value */
186 for_each_marker_of_type(m
, LABEL
) {
187 assert (m
->offset
== val
.len
);
188 fprintf(f
, " %s:", m
->ref
);
193 static void write_propval(FILE *f
, struct property
*prop
)
195 int len
= prop
->val
.len
;
196 const char *p
= prop
->val
.val
;
197 struct marker
*m
= prop
->val
.markers
;
198 int nnotstring
= 0, nnul
= 0;
199 int nnotstringlbl
= 0, nnotcelllbl
= 0;
207 for (i
= 0; i
< len
; i
++) {
208 if (! isstring(p
[i
]))
214 for_each_marker_of_type(m
, LABEL
) {
215 if ((m
->offset
> 0) && (prop
->val
.val
[m
->offset
- 1] != '\0'))
217 if ((m
->offset
% sizeof(cell_t
)) != 0)
222 if ((p
[len
-1] == '\0') && (nnotstring
== 0) && (nnul
< (len
-nnul
))
223 && (nnotstringlbl
== 0)) {
224 write_propval_string(f
, prop
->val
);
225 } else if (((len
% sizeof(cell_t
)) == 0) && (nnotcelllbl
== 0)) {
226 write_propval_cells(f
, prop
->val
);
228 write_propval_bytes(f
, prop
->val
);
234 static void write_tree_source_node(FILE *f
, struct node
*tree
, int level
)
236 struct property
*prop
;
240 write_prefix(f
, level
);
241 for_each_label(tree
->labels
, l
)
242 fprintf(f
, "%s: ", l
->label
);
243 if (tree
->name
&& (*tree
->name
))
244 fprintf(f
, "%s {\n", tree
->name
);
248 for_each_property(tree
, prop
) {
249 write_prefix(f
, level
+1);
250 for_each_label(prop
->labels
, l
)
251 fprintf(f
, "%s: ", l
->label
);
252 fprintf(f
, "%s", prop
->name
);
253 write_propval(f
, prop
);
255 for_each_child(tree
, child
) {
257 write_tree_source_node(f
, child
, level
+1);
259 write_prefix(f
, level
);
264 void dt_to_source(FILE *f
, struct boot_info
*bi
)
266 struct reserve_info
*re
;
268 fprintf(f
, "/dts-v1/;\n\n");
270 for (re
= bi
->reservelist
; re
; re
= re
->next
) {
273 for_each_label(re
->labels
, l
)
274 fprintf(f
, "%s: ", l
->label
);
275 fprintf(f
, "/memreserve/\t0x%016llx 0x%016llx;\n",
276 (unsigned long long)re
->re
.address
,
277 (unsigned long long)re
->re
.size
);
280 write_tree_source_node(f
, bi
->dt
, 0);