0001
0002
0003
0004
0005
0006
0007
0008 #include <stdlib.h>
0009 #include <yaml.h>
0010 #include "dtc.h"
0011 #include "srcpos.h"
0012
0013 char *yaml_error_name[] = {
0014 [YAML_NO_ERROR] = "no error",
0015 [YAML_MEMORY_ERROR] = "memory error",
0016 [YAML_READER_ERROR] = "reader error",
0017 [YAML_SCANNER_ERROR] = "scanner error",
0018 [YAML_PARSER_ERROR] = "parser error",
0019 [YAML_COMPOSER_ERROR] = "composer error",
0020 [YAML_WRITER_ERROR] = "writer error",
0021 [YAML_EMITTER_ERROR] = "emitter error",
0022 };
0023
0024 #define yaml_emitter_emit_or_die(emitter, event) ( \
0025 { \
0026 if (!yaml_emitter_emit(emitter, event)) \
0027 die("yaml '%s': %s in %s, line %i\n", \
0028 yaml_error_name[(emitter)->error], \
0029 (emitter)->problem, __func__, __LINE__); \
0030 })
0031
0032 static void yaml_propval_int(yaml_emitter_t *emitter, struct marker *markers,
0033 char *data, unsigned int seq_offset, unsigned int len, int width)
0034 {
0035 yaml_event_t event;
0036 void *tag;
0037 unsigned int off;
0038
0039 switch(width) {
0040 case 1: tag = "!u8"; break;
0041 case 2: tag = "!u16"; break;
0042 case 4: tag = "!u32"; break;
0043 case 8: tag = "!u64"; break;
0044 default:
0045 die("Invalid width %i", width);
0046 }
0047 assert(len % width == 0);
0048
0049 yaml_sequence_start_event_initialize(&event, NULL,
0050 (yaml_char_t *)tag, width == 4, YAML_FLOW_SEQUENCE_STYLE);
0051 yaml_emitter_emit_or_die(emitter, &event);
0052
0053 for (off = 0; off < len; off += width) {
0054 char buf[32];
0055 struct marker *m;
0056 bool is_phandle = false;
0057
0058 switch(width) {
0059 case 1:
0060 sprintf(buf, "0x%"PRIx8, *(uint8_t*)(data + off));
0061 break;
0062 case 2:
0063 sprintf(buf, "0x%"PRIx16, dtb_ld16(data + off));
0064 break;
0065 case 4:
0066 sprintf(buf, "0x%"PRIx32, dtb_ld32(data + off));
0067 m = markers;
0068 is_phandle = false;
0069 for_each_marker_of_type(m, REF_PHANDLE) {
0070 if (m->offset == (seq_offset + off)) {
0071 is_phandle = true;
0072 break;
0073 }
0074 }
0075 break;
0076 case 8:
0077 sprintf(buf, "0x%"PRIx64, dtb_ld64(data + off));
0078 break;
0079 }
0080
0081 if (is_phandle)
0082 yaml_scalar_event_initialize(&event, NULL,
0083 (yaml_char_t*)"!phandle", (yaml_char_t *)buf,
0084 strlen(buf), 0, 0, YAML_PLAIN_SCALAR_STYLE);
0085 else
0086 yaml_scalar_event_initialize(&event, NULL,
0087 (yaml_char_t*)YAML_INT_TAG, (yaml_char_t *)buf,
0088 strlen(buf), 1, 1, YAML_PLAIN_SCALAR_STYLE);
0089 yaml_emitter_emit_or_die(emitter, &event);
0090 }
0091
0092 yaml_sequence_end_event_initialize(&event);
0093 yaml_emitter_emit_or_die(emitter, &event);
0094 }
0095
0096 static void yaml_propval_string(yaml_emitter_t *emitter, char *str, int len)
0097 {
0098 yaml_event_t event;
0099 int i;
0100
0101 assert(str[len-1] == '\0');
0102
0103
0104 for (i = 0; i < len; i++)
0105 assert(isascii(str[i]));
0106
0107 yaml_scalar_event_initialize(&event, NULL,
0108 (yaml_char_t *)YAML_STR_TAG, (yaml_char_t*)str,
0109 len-1, 0, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE);
0110 yaml_emitter_emit_or_die(emitter, &event);
0111 }
0112
0113 static void yaml_propval(yaml_emitter_t *emitter, struct property *prop)
0114 {
0115 yaml_event_t event;
0116 unsigned int len = prop->val.len;
0117 struct marker *m = prop->val.markers;
0118 struct marker *markers = prop->val.markers;
0119
0120
0121 yaml_scalar_event_initialize(&event, NULL,
0122 (yaml_char_t *)YAML_STR_TAG, (yaml_char_t*)prop->name,
0123 strlen(prop->name), 1, 1, YAML_PLAIN_SCALAR_STYLE);
0124 yaml_emitter_emit_or_die(emitter, &event);
0125
0126
0127 if (len == 0) {
0128 yaml_scalar_event_initialize(&event, NULL,
0129 (yaml_char_t *)YAML_BOOL_TAG,
0130 (yaml_char_t*)"true",
0131 strlen("true"), 1, 0, YAML_PLAIN_SCALAR_STYLE);
0132 yaml_emitter_emit_or_die(emitter, &event);
0133 return;
0134 }
0135
0136 if (!m)
0137 die("No markers present in property '%s' value\n", prop->name);
0138
0139 yaml_sequence_start_event_initialize(&event, NULL,
0140 (yaml_char_t *)YAML_SEQ_TAG, 1, YAML_FLOW_SEQUENCE_STYLE);
0141 yaml_emitter_emit_or_die(emitter, &event);
0142
0143 for_each_marker(m) {
0144 int chunk_len;
0145 char *data = &prop->val.val[m->offset];
0146
0147 if (m->type < TYPE_UINT8)
0148 continue;
0149
0150 chunk_len = type_marker_length(m) ? : len;
0151 assert(chunk_len > 0);
0152 len -= chunk_len;
0153
0154 switch(m->type) {
0155 case TYPE_UINT16:
0156 yaml_propval_int(emitter, markers, data, m->offset, chunk_len, 2);
0157 break;
0158 case TYPE_UINT32:
0159 yaml_propval_int(emitter, markers, data, m->offset, chunk_len, 4);
0160 break;
0161 case TYPE_UINT64:
0162 yaml_propval_int(emitter, markers, data, m->offset, chunk_len, 8);
0163 break;
0164 case TYPE_STRING:
0165 yaml_propval_string(emitter, data, chunk_len);
0166 break;
0167 default:
0168 yaml_propval_int(emitter, markers, data, m->offset, chunk_len, 1);
0169 break;
0170 }
0171 }
0172
0173 yaml_sequence_end_event_initialize(&event);
0174 yaml_emitter_emit_or_die(emitter, &event);
0175 }
0176
0177
0178 static void yaml_tree(struct node *tree, yaml_emitter_t *emitter)
0179 {
0180 struct property *prop;
0181 struct node *child;
0182 yaml_event_t event;
0183
0184 if (tree->deleted)
0185 return;
0186
0187 yaml_mapping_start_event_initialize(&event, NULL,
0188 (yaml_char_t *)YAML_MAP_TAG, 1, YAML_ANY_MAPPING_STYLE);
0189 yaml_emitter_emit_or_die(emitter, &event);
0190
0191 for_each_property(tree, prop)
0192 yaml_propval(emitter, prop);
0193
0194
0195 for_each_child(tree, child) {
0196 yaml_scalar_event_initialize(&event, NULL,
0197 (yaml_char_t *)YAML_STR_TAG, (yaml_char_t*)child->name,
0198 strlen(child->name), 1, 0, YAML_PLAIN_SCALAR_STYLE);
0199 yaml_emitter_emit_or_die(emitter, &event);
0200 yaml_tree(child, emitter);
0201 }
0202
0203 yaml_mapping_end_event_initialize(&event);
0204 yaml_emitter_emit_or_die(emitter, &event);
0205 }
0206
0207 void dt_to_yaml(FILE *f, struct dt_info *dti)
0208 {
0209 yaml_emitter_t emitter;
0210 yaml_event_t event;
0211
0212 yaml_emitter_initialize(&emitter);
0213 yaml_emitter_set_output_file(&emitter, f);
0214 yaml_stream_start_event_initialize(&event, YAML_UTF8_ENCODING);
0215 yaml_emitter_emit_or_die(&emitter, &event);
0216
0217 yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
0218 yaml_emitter_emit_or_die(&emitter, &event);
0219
0220 yaml_sequence_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_SEQ_TAG, 1, YAML_ANY_SEQUENCE_STYLE);
0221 yaml_emitter_emit_or_die(&emitter, &event);
0222
0223 yaml_tree(dti->dt, &emitter);
0224
0225 yaml_sequence_end_event_initialize(&event);
0226 yaml_emitter_emit_or_die(&emitter, &event);
0227
0228 yaml_document_end_event_initialize(&event, 0);
0229 yaml_emitter_emit_or_die(&emitter, &event);
0230
0231 yaml_stream_end_event_initialize(&event);
0232 yaml_emitter_emit_or_die(&emitter, &event);
0233
0234 yaml_emitter_delete(&emitter);
0235 }