1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * (C) Copyright Linaro, Ltd. 2018
4 * (C) Copyright Arm Holdings. 2017
5 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
13 char *yaml_error_name
[] = {
14 [YAML_NO_ERROR
] = "no error",
15 [YAML_MEMORY_ERROR
] = "memory error",
16 [YAML_READER_ERROR
] = "reader error",
17 [YAML_SCANNER_ERROR
] = "scanner error",
18 [YAML_PARSER_ERROR
] = "parser error",
19 [YAML_COMPOSER_ERROR
] = "composer error",
20 [YAML_WRITER_ERROR
] = "writer error",
21 [YAML_EMITTER_ERROR
] = "emitter error",
24 #define yaml_emitter_emit_or_die(emitter, event) ( \
26 if (!yaml_emitter_emit(emitter, event)) \
27 die("yaml '%s': %s in %s, line %i\n", \
28 yaml_error_name[(emitter)->error], \
29 (emitter)->problem, __func__, __LINE__); \
32 static void yaml_propval_int(yaml_emitter_t
*emitter
, struct marker
*markers
, char *data
, int len
, int width
)
36 int off
, start_offset
= markers
->offset
;
39 case 1: tag
= "!u8"; break;
40 case 2: tag
= "!u16"; break;
41 case 4: tag
= "!u32"; break;
42 case 8: tag
= "!u64"; break;
44 die("Invalid width %i", width
);
46 assert(len
% width
== 0);
48 yaml_sequence_start_event_initialize(&event
, NULL
,
49 (yaml_char_t
*)tag
, width
== 4, YAML_FLOW_SEQUENCE_STYLE
);
50 yaml_emitter_emit_or_die(emitter
, &event
);
52 for (off
= 0; off
< len
; off
+= width
) {
55 bool is_phandle
= false;
59 sprintf(buf
, "0x%"PRIx8
, *(uint8_t*)(data
+ off
));
62 sprintf(buf
, "0x%"PRIx16
, fdt16_to_cpu(*(fdt16_t
*)(data
+ off
)));
65 sprintf(buf
, "0x%"PRIx32
, fdt32_to_cpu(*(fdt32_t
*)(data
+ off
)));
68 for_each_marker_of_type(m
, REF_PHANDLE
) {
69 if (m
->offset
== (start_offset
+ off
)) {
76 sprintf(buf
, "0x%"PRIx64
, fdt64_to_cpu(*(fdt64_t
*)(data
+ off
)));
81 yaml_scalar_event_initialize(&event
, NULL
,
82 (yaml_char_t
*)"!phandle", (yaml_char_t
*)buf
,
83 strlen(buf
), 0, 0, YAML_PLAIN_SCALAR_STYLE
);
85 yaml_scalar_event_initialize(&event
, NULL
,
86 (yaml_char_t
*)YAML_INT_TAG
, (yaml_char_t
*)buf
,
87 strlen(buf
), 1, 1, YAML_PLAIN_SCALAR_STYLE
);
88 yaml_emitter_emit_or_die(emitter
, &event
);
91 yaml_sequence_end_event_initialize(&event
);
92 yaml_emitter_emit_or_die(emitter
, &event
);
95 static void yaml_propval_string(yaml_emitter_t
*emitter
, char *str
, int len
)
100 assert(str
[len
-1] == '\0');
102 /* Make sure the entire string is in the lower 7-bit ascii range */
103 for (i
= 0; i
< len
; i
++)
104 assert(isascii(str
[i
]));
106 yaml_scalar_event_initialize(&event
, NULL
,
107 (yaml_char_t
*)YAML_STR_TAG
, (yaml_char_t
*)str
,
108 len
-1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE
);
109 yaml_emitter_emit_or_die(emitter
, &event
);
112 static void yaml_propval(yaml_emitter_t
*emitter
, struct property
*prop
)
115 int len
= prop
->val
.len
;
116 struct marker
*m
= prop
->val
.markers
;
118 /* Emit the property name */
119 yaml_scalar_event_initialize(&event
, NULL
,
120 (yaml_char_t
*)YAML_STR_TAG
, (yaml_char_t
*)prop
->name
,
121 strlen(prop
->name
), 1, 1, YAML_PLAIN_SCALAR_STYLE
);
122 yaml_emitter_emit_or_die(emitter
, &event
);
124 /* Boolean properties are easiest to deal with. Length is zero, so just emit 'true' */
126 yaml_scalar_event_initialize(&event
, NULL
,
127 (yaml_char_t
*)YAML_BOOL_TAG
,
128 (yaml_char_t
*)"true",
129 strlen("true"), 1, 0, YAML_PLAIN_SCALAR_STYLE
);
130 yaml_emitter_emit_or_die(emitter
, &event
);
135 die("No markers present in property '%s' value\n", prop
->name
);
137 yaml_sequence_start_event_initialize(&event
, NULL
,
138 (yaml_char_t
*)YAML_SEQ_TAG
, 1, YAML_FLOW_SEQUENCE_STYLE
);
139 yaml_emitter_emit_or_die(emitter
, &event
);
143 char *data
= &prop
->val
.val
[m
->offset
];
145 if (m
->type
< TYPE_UINT8
)
148 chunk_len
= type_marker_length(m
) ? : len
;
149 assert(chunk_len
> 0);
154 yaml_propval_int(emitter
, m
, data
, chunk_len
, 2);
157 yaml_propval_int(emitter
, m
, data
, chunk_len
, 4);
160 yaml_propval_int(emitter
, m
, data
, chunk_len
, 8);
163 yaml_propval_string(emitter
, data
, chunk_len
);
166 yaml_propval_int(emitter
, m
, data
, chunk_len
, 1);
171 yaml_sequence_end_event_initialize(&event
);
172 yaml_emitter_emit_or_die(emitter
, &event
);
176 static void yaml_tree(struct node
*tree
, yaml_emitter_t
*emitter
)
178 struct property
*prop
;
185 yaml_mapping_start_event_initialize(&event
, NULL
,
186 (yaml_char_t
*)YAML_MAP_TAG
, 1, YAML_ANY_MAPPING_STYLE
);
187 yaml_emitter_emit_or_die(emitter
, &event
);
189 for_each_property(tree
, prop
)
190 yaml_propval(emitter
, prop
);
192 /* Loop over all the children, emitting them into the map */
193 for_each_child(tree
, child
) {
194 yaml_scalar_event_initialize(&event
, NULL
,
195 (yaml_char_t
*)YAML_STR_TAG
, (yaml_char_t
*)child
->name
,
196 strlen(child
->name
), 1, 0, YAML_PLAIN_SCALAR_STYLE
);
197 yaml_emitter_emit_or_die(emitter
, &event
);
198 yaml_tree(child
, emitter
);
201 yaml_mapping_end_event_initialize(&event
);
202 yaml_emitter_emit_or_die(emitter
, &event
);
205 void dt_to_yaml(FILE *f
, struct dt_info
*dti
)
207 yaml_emitter_t emitter
;
210 yaml_emitter_initialize(&emitter
);
211 yaml_emitter_set_output_file(&emitter
, f
);
212 yaml_stream_start_event_initialize(&event
, YAML_UTF8_ENCODING
);
213 yaml_emitter_emit_or_die(&emitter
, &event
);
215 yaml_document_start_event_initialize(&event
, NULL
, NULL
, NULL
, 0);
216 yaml_emitter_emit_or_die(&emitter
, &event
);
218 yaml_sequence_start_event_initialize(&event
, NULL
, (yaml_char_t
*)YAML_SEQ_TAG
, 1, YAML_ANY_SEQUENCE_STYLE
);
219 yaml_emitter_emit_or_die(&emitter
, &event
);
221 yaml_tree(dti
->dt
, &emitter
);
223 yaml_sequence_end_event_initialize(&event
);
224 yaml_emitter_emit_or_die(&emitter
, &event
);
226 yaml_document_end_event_initialize(&event
, 0);
227 yaml_emitter_emit_or_die(&emitter
, &event
);
229 yaml_stream_end_event_initialize(&event
);
230 yaml_emitter_emit_or_die(&emitter
, &event
);
232 yaml_emitter_delete(&emitter
);