1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
10 extern int yyparse(void);
11 extern YYLTYPE yylloc
;
13 struct dt_info
*parser_output
;
14 bool treesource_error
;
16 struct dt_info
*dt_from_source(const char *fname
)
19 treesource_error
= false;
22 yyin
= current_srcfile
->f
;
23 yylloc
.file
= current_srcfile
;
26 die("Unable to parse input tree\n");
29 die("Syntax error parsing input tree\n");
34 static void write_prefix(FILE *f
, int level
)
38 for (i
= 0; i
< level
; i
++)
42 static bool isstring(char c
)
44 return (isprint((unsigned char)c
)
46 || strchr("\a\b\t\n\v\f\r", c
));
49 static void write_propval_string(FILE *f
, const char *s
, size_t len
)
51 const char *end
= s
+ len
- 1;
93 if (isprint((unsigned char)c
))
96 fprintf(f
, "\\x%02"PRIx8
, c
);
102 static void write_propval_int(FILE *f
, const char *p
, size_t len
, size_t width
)
104 const char *end
= p
+ len
;
105 assert(len
% width
== 0);
107 for (; p
< end
; p
+= width
) {
110 fprintf(f
, "%02"PRIx8
, *(const uint8_t*)p
);
113 fprintf(f
, "0x%02"PRIx16
, dtb_ld16(p
));
116 fprintf(f
, "0x%02"PRIx32
, dtb_ld32(p
));
119 fprintf(f
, "0x%02"PRIx64
, dtb_ld64(p
));
127 static const char *delim_start
[] = {
129 [TYPE_UINT16
] = "/bits/ 16 <",
131 [TYPE_UINT64
] = "/bits/ 64 <",
134 static const char *delim_end
[] = {
142 static void add_string_markers(struct property
*prop
)
144 int l
, len
= prop
->val
.len
;
145 const char *p
= prop
->val
.val
;
147 for (l
= strlen(p
) + 1; l
< len
; l
+= strlen(p
+ l
) + 1) {
148 struct marker
*m
, **nextp
;
150 m
= xmalloc(sizeof(*m
));
152 m
->type
= TYPE_STRING
;
156 /* Find the end of the markerlist */
157 nextp
= &prop
->val
.markers
;
159 nextp
= &((*nextp
)->next
);
164 static enum markertype
guess_value_type(struct property
*prop
)
166 int len
= prop
->val
.len
;
167 const char *p
= prop
->val
.val
;
168 struct marker
*m
= prop
->val
.markers
;
169 int nnotstring
= 0, nnul
= 0;
170 int nnotstringlbl
= 0, nnotcelllbl
= 0;
173 for (i
= 0; i
< len
; i
++) {
174 if (! isstring(p
[i
]))
180 for_each_marker_of_type(m
, LABEL
) {
181 if ((m
->offset
> 0) && (prop
->val
.val
[m
->offset
- 1] != '\0'))
183 if ((m
->offset
% sizeof(cell_t
)) != 0)
187 if ((p
[len
-1] == '\0') && (nnotstring
== 0) && (nnul
<= (len
-nnul
))
188 && (nnotstringlbl
== 0)) {
190 add_string_markers(prop
);
192 } else if (((len
% sizeof(cell_t
)) == 0) && (nnotcelllbl
== 0)) {
199 static void write_propval(FILE *f
, struct property
*prop
)
201 size_t len
= prop
->val
.len
;
202 struct marker
*m
= prop
->val
.markers
;
203 struct marker dummy_marker
;
204 enum markertype emit_type
= TYPE_NONE
;
210 srcstr
= srcpos_string_first(prop
->srcpos
, annotate
);
212 fprintf(f
, " /* %s */", srcstr
);
222 if (!next_type_marker(m
)) {
223 /* data type information missing, need to guess */
224 dummy_marker
.type
= guess_value_type(prop
);
225 dummy_marker
.next
= prop
->val
.markers
;
226 dummy_marker
.offset
= 0;
227 dummy_marker
.ref
= NULL
;
232 size_t chunk_len
= (m
->next
? m
->next
->offset
: len
) - m
->offset
;
233 size_t data_len
= type_marker_length(m
) ? : len
- m
->offset
;
234 const char *p
= &prop
->val
.val
[m
->offset
];
235 struct marker
*m_phandle
;
237 if (is_type_marker(m
->type
)) {
239 fprintf(f
, " %s", delim_start
[emit_type
]);
240 } else if (m
->type
== LABEL
)
241 fprintf(f
, " %s:", m
->ref
);
243 if (emit_type
== TYPE_NONE
|| chunk_len
== 0)
248 write_propval_int(f
, p
, chunk_len
, 2);
251 m_phandle
= prop
->val
.markers
;
252 for_each_marker_of_type(m_phandle
, REF_PHANDLE
)
253 if (m
->offset
== m_phandle
->offset
)
257 if (m_phandle
->ref
[0] == '/')
258 fprintf(f
, "&{%s}", m_phandle
->ref
);
260 fprintf(f
, "&%s", m_phandle
->ref
);
263 write_propval_int(f
, p
+ 4, chunk_len
- 4, 4);
266 write_propval_int(f
, p
, chunk_len
, 4);
268 if (data_len
> chunk_len
)
272 write_propval_int(f
, p
, chunk_len
, 8);
275 write_propval_string(f
, p
, chunk_len
);
278 write_propval_int(f
, p
, chunk_len
, 1);
281 if (chunk_len
== data_len
) {
282 size_t pos
= m
->offset
+ chunk_len
;
283 fprintf(f
, pos
== len
? "%s" : "%s,",
284 delim_end
[emit_type
] ? : "");
285 emit_type
= TYPE_NONE
;
290 srcstr
= srcpos_string_first(prop
->srcpos
, annotate
);
292 fprintf(f
, " /* %s */", srcstr
);
299 static void write_tree_source_node(FILE *f
, struct node
*tree
, int level
)
301 struct property
*prop
;
306 write_prefix(f
, level
);
307 for_each_label(tree
->labels
, l
)
308 fprintf(f
, "%s: ", l
->label
);
309 if (tree
->name
&& (*tree
->name
))
310 fprintf(f
, "%s {", tree
->name
);
315 srcstr
= srcpos_string_first(tree
->srcpos
, annotate
);
317 fprintf(f
, " /* %s */", srcstr
);
323 for_each_property(tree
, prop
) {
324 write_prefix(f
, level
+1);
325 for_each_label(prop
->labels
, l
)
326 fprintf(f
, "%s: ", l
->label
);
327 fprintf(f
, "%s", prop
->name
);
328 write_propval(f
, prop
);
330 for_each_child(tree
, child
) {
332 write_tree_source_node(f
, child
, level
+1);
334 write_prefix(f
, level
);
337 srcstr
= srcpos_string_last(tree
->srcpos
, annotate
);
339 fprintf(f
, " /* %s */", srcstr
);
346 void dt_to_source(FILE *f
, struct dt_info
*dti
)
348 struct reserve_info
*re
;
350 fprintf(f
, "/dts-v1/;\n\n");
352 for (re
= dti
->reservelist
; re
; re
= re
->next
) {
355 for_each_label(re
->labels
, l
)
356 fprintf(f
, "%s: ", l
->label
);
357 fprintf(f
, "/memreserve/\t0x%016llx 0x%016llx;\n",
358 (unsigned long long)re
->address
,
359 (unsigned long long)re
->size
);
362 write_tree_source_node(f
, dti
->dt
, 0);