Blame view
scripts/dtc/treesource.c
6.83 KB
1a59d1b8e treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
a4da2e3ec [POWERPC] Merge d... |
2 3 |
/* * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. |
a4da2e3ec [POWERPC] Merge d... |
4 5 6 7 8 9 10 |
*/ #include "dtc.h" #include "srcpos.h" extern FILE *yyin; extern int yyparse(void); |
cd296721a dtc: import lates... |
11 |
extern YYLTYPE yylloc; |
a4da2e3ec [POWERPC] Merge d... |
12 |
|
6f05afcbb scripts/dtc: Upda... |
13 |
struct dt_info *parser_output; |
476059711 scripts/dtc: Upda... |
14 |
bool treesource_error; |
a4da2e3ec [POWERPC] Merge d... |
15 |
|
6f05afcbb scripts/dtc: Upda... |
16 |
struct dt_info *dt_from_source(const char *fname) |
a4da2e3ec [POWERPC] Merge d... |
17 |
{ |
6f05afcbb scripts/dtc: Upda... |
18 |
parser_output = NULL; |
476059711 scripts/dtc: Upda... |
19 |
treesource_error = false; |
a4da2e3ec [POWERPC] Merge d... |
20 |
|
658f29a51 of/flattree: Upda... |
21 22 |
srcfile_push(fname); yyin = current_srcfile->f; |
cd296721a dtc: import lates... |
23 |
yylloc.file = current_srcfile; |
a4da2e3ec [POWERPC] Merge d... |
24 25 |
if (yyparse() != 0) |
ed95d7450 powerpc: Update i... |
26 27 |
die("Unable to parse input tree "); |
a4da2e3ec [POWERPC] Merge d... |
28 |
|
ed95d7450 powerpc: Update i... |
29 30 31 |
if (treesource_error) die("Syntax error parsing input tree "); |
a4da2e3ec [POWERPC] Merge d... |
32 |
|
6f05afcbb scripts/dtc: Upda... |
33 |
return parser_output; |
a4da2e3ec [POWERPC] Merge d... |
34 35 36 37 38 39 40 41 42 |
} static void write_prefix(FILE *f, int level) { int i; for (i = 0; i < level; i++) fputc('\t', f); } |
476059711 scripts/dtc: Upda... |
43 |
static bool isstring(char c) |
a4da2e3ec [POWERPC] Merge d... |
44 |
{ |
476059711 scripts/dtc: Upda... |
45 |
return (isprint((unsigned char)c) |
a4da2e3ec [POWERPC] Merge d... |
46 47 48 49 |
|| (c == '\0') || strchr("\a\b\t \v\f\r", c)); } |
f858927fd scripts/dtc: Upda... |
50 |
static void write_propval_string(FILE *f, const char *s, size_t len) |
a4da2e3ec [POWERPC] Merge d... |
51 |
{ |
f858927fd scripts/dtc: Upda... |
52 |
const char *end = s + len - 1; |
c2e7075ca scripts/dtc: Upda... |
53 54 55 |
if (!len) return; |
f858927fd scripts/dtc: Upda... |
56 |
assert(*end == '\0'); |
a4da2e3ec [POWERPC] Merge d... |
57 |
|
658f29a51 of/flattree: Upda... |
58 |
fprintf(f, "\""); |
f858927fd scripts/dtc: Upda... |
59 60 |
while (s < end) { char c = *s++; |
a4da2e3ec [POWERPC] Merge d... |
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
switch (c) { case '\a': fprintf(f, "\\a"); break; case '\b': fprintf(f, "\\b"); break; case '\t': fprintf(f, "\\t"); break; case ' ': fprintf(f, "\ "); break; case '\v': fprintf(f, "\\v"); break; case '\f': fprintf(f, "\\f"); break; case '\r': fprintf(f, "\\r"); break; case '\\': fprintf(f, "\\\\"); break; case '\"': fprintf(f, "\\\""); break; case '\0': |
f858927fd scripts/dtc: Upda... |
92 |
fprintf(f, "\\0"); |
a4da2e3ec [POWERPC] Merge d... |
93 94 |
break; default: |
476059711 scripts/dtc: Upda... |
95 |
if (isprint((unsigned char)c)) |
a4da2e3ec [POWERPC] Merge d... |
96 97 |
fprintf(f, "%c", c); else |
f858927fd scripts/dtc: Upda... |
98 |
fprintf(f, "\\x%02"PRIx8, c); |
a4da2e3ec [POWERPC] Merge d... |
99 100 101 |
} } fprintf(f, "\""); |
a4da2e3ec [POWERPC] Merge d... |
102 |
} |
f858927fd scripts/dtc: Upda... |
103 |
static void write_propval_int(FILE *f, const char *p, size_t len, size_t width) |
a4da2e3ec [POWERPC] Merge d... |
104 |
{ |
f858927fd scripts/dtc: Upda... |
105 106 |
const char *end = p + len; assert(len % width == 0); |
a4da2e3ec [POWERPC] Merge d... |
107 |
|
f858927fd scripts/dtc: Upda... |
108 109 110 |
for (; p < end; p += width) { switch (width) { case 1: |
c2e7075ca scripts/dtc: Upda... |
111 |
fprintf(f, "%02"PRIx8, *(const uint8_t*)p); |
f858927fd scripts/dtc: Upda... |
112 113 |
break; case 2: |
3eb619b2f scripts/dtc: Upda... |
114 |
fprintf(f, "0x%02"PRIx16, dtb_ld16(p)); |
f858927fd scripts/dtc: Upda... |
115 116 |
break; case 4: |
3eb619b2f scripts/dtc: Upda... |
117 |
fprintf(f, "0x%02"PRIx32, dtb_ld32(p)); |
f858927fd scripts/dtc: Upda... |
118 119 |
break; case 8: |
3eb619b2f scripts/dtc: Upda... |
120 |
fprintf(f, "0x%02"PRIx64, dtb_ld64(p)); |
a4da2e3ec [POWERPC] Merge d... |
121 |
break; |
f858927fd scripts/dtc: Upda... |
122 |
} |
c2e7075ca scripts/dtc: Upda... |
123 124 |
if (p + width < end) fputc(' ', f); |
a4da2e3ec [POWERPC] Merge d... |
125 |
} |
f858927fd scripts/dtc: Upda... |
126 |
} |
a4da2e3ec [POWERPC] Merge d... |
127 |
|
f858927fd scripts/dtc: Upda... |
128 129 130 |
static bool has_data_type_information(struct marker *m) { return m->type >= TYPE_UINT8; |
a4da2e3ec [POWERPC] Merge d... |
131 |
} |
f858927fd scripts/dtc: Upda... |
132 |
static struct marker *next_type_marker(struct marker *m) |
a4da2e3ec [POWERPC] Merge d... |
133 |
{ |
f858927fd scripts/dtc: Upda... |
134 135 136 137 |
while (m && !has_data_type_information(m)) m = m->next; return m; } |
a4da2e3ec [POWERPC] Merge d... |
138 |
|
f858927fd scripts/dtc: Upda... |
139 140 141 |
size_t type_marker_length(struct marker *m) { struct marker *next = next_type_marker(m->next); |
a4da2e3ec [POWERPC] Merge d... |
142 |
|
f858927fd scripts/dtc: Upda... |
143 144 145 |
if (next) return next->offset - m->offset; return 0; |
a4da2e3ec [POWERPC] Merge d... |
146 |
} |
f858927fd scripts/dtc: Upda... |
147 148 149 150 151 152 153 154 |
static const char *delim_start[] = { [TYPE_UINT8] = "[", [TYPE_UINT16] = "/bits/ 16 <", [TYPE_UINT32] = "<", [TYPE_UINT64] = "/bits/ 64 <", [TYPE_STRING] = "", }; static const char *delim_end[] = { |
c2e7075ca scripts/dtc: Upda... |
155 156 157 158 |
[TYPE_UINT8] = "]", [TYPE_UINT16] = ">", [TYPE_UINT32] = ">", [TYPE_UINT64] = ">", |
f858927fd scripts/dtc: Upda... |
159 160 161 162 |
[TYPE_STRING] = "", }; static enum markertype guess_value_type(struct property *prop) |
a4da2e3ec [POWERPC] Merge d... |
163 164 165 166 167 168 169 |
{ int len = prop->val.len; const char *p = prop->val.val; struct marker *m = prop->val.markers; int nnotstring = 0, nnul = 0; int nnotstringlbl = 0, nnotcelllbl = 0; int i; |
a4da2e3ec [POWERPC] Merge d... |
170 171 172 173 174 175 176 177 178 179 180 181 182 |
for (i = 0; i < len; i++) { if (! isstring(p[i])) nnotstring++; if (p[i] == '\0') nnul++; } for_each_marker_of_type(m, LABEL) { if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0')) nnotstringlbl++; if ((m->offset % sizeof(cell_t)) != 0) nnotcelllbl++; } |
3eb619b2f scripts/dtc: Upda... |
183 |
if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul <= (len-nnul)) |
a4da2e3ec [POWERPC] Merge d... |
184 |
&& (nnotstringlbl == 0)) { |
f858927fd scripts/dtc: Upda... |
185 |
return TYPE_STRING; |
a4da2e3ec [POWERPC] Merge d... |
186 |
} else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) { |
f858927fd scripts/dtc: Upda... |
187 |
return TYPE_UINT32; |
a4da2e3ec [POWERPC] Merge d... |
188 |
} |
f858927fd scripts/dtc: Upda... |
189 190 191 192 193 194 195 196 197 |
return TYPE_UINT8; } static void write_propval(FILE *f, struct property *prop) { size_t len = prop->val.len; struct marker *m = prop->val.markers; struct marker dummy_marker; enum markertype emit_type = TYPE_NONE; |
c2e7075ca scripts/dtc: Upda... |
198 |
char *srcstr; |
f858927fd scripts/dtc: Upda... |
199 200 |
if (len == 0) { |
c2e7075ca scripts/dtc: Upda... |
201 202 203 204 205 206 207 208 209 210 |
fprintf(f, ";"); if (annotate) { srcstr = srcpos_string_first(prop->srcpos, annotate); if (srcstr) { fprintf(f, " /* %s */", srcstr); free(srcstr); } } fprintf(f, " "); |
f858927fd scripts/dtc: Upda... |
211 212 |
return; } |
c2e7075ca scripts/dtc: Upda... |
213 |
fprintf(f, " ="); |
f858927fd scripts/dtc: Upda... |
214 215 216 217 218 219 220 221 222 |
if (!next_type_marker(m)) { /* data type information missing, need to guess */ dummy_marker.type = guess_value_type(prop); dummy_marker.next = prop->val.markers; dummy_marker.offset = 0; dummy_marker.ref = NULL; m = &dummy_marker; } |
f858927fd scripts/dtc: Upda... |
223 |
for_each_marker(m) { |
c2e7075ca scripts/dtc: Upda... |
224 225 |
size_t chunk_len = (m->next ? m->next->offset : len) - m->offset; size_t data_len = type_marker_length(m) ? : len - m->offset; |
f858927fd scripts/dtc: Upda... |
226 |
const char *p = &prop->val.val[m->offset]; |
c2e7075ca scripts/dtc: Upda... |
227 228 229 230 231 232 233 |
if (has_data_type_information(m)) { emit_type = m->type; fprintf(f, " %s", delim_start[emit_type]); } else if (m->type == LABEL) fprintf(f, " %s:", m->ref); else if (m->offset) fputc(' ', f); |
f858927fd scripts/dtc: Upda... |
234 |
|
c2e7075ca scripts/dtc: Upda... |
235 236 |
if (emit_type == TYPE_NONE) { assert(chunk_len == 0); |
f858927fd scripts/dtc: Upda... |
237 |
continue; |
c2e7075ca scripts/dtc: Upda... |
238 |
} |
f858927fd scripts/dtc: Upda... |
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
switch(emit_type) { case TYPE_UINT16: write_propval_int(f, p, chunk_len, 2); break; case TYPE_UINT32: write_propval_int(f, p, chunk_len, 4); break; case TYPE_UINT64: write_propval_int(f, p, chunk_len, 8); break; case TYPE_STRING: write_propval_string(f, p, chunk_len); break; default: write_propval_int(f, p, chunk_len, 1); } |
f858927fd scripts/dtc: Upda... |
256 |
|
c2e7075ca scripts/dtc: Upda... |
257 258 259 260 261 262 |
if (chunk_len == data_len) { size_t pos = m->offset + chunk_len; fprintf(f, pos == len ? "%s" : "%s,", delim_end[emit_type] ? : ""); emit_type = TYPE_NONE; } |
f858927fd scripts/dtc: Upda... |
263 |
} |
c2e7075ca scripts/dtc: Upda... |
264 265 266 267 268 269 270 271 272 273 |
fprintf(f, ";"); if (annotate) { srcstr = srcpos_string_first(prop->srcpos, annotate); if (srcstr) { fprintf(f, " /* %s */", srcstr); free(srcstr); } } fprintf(f, " "); |
a4da2e3ec [POWERPC] Merge d... |
274 275 276 277 278 279 |
} static void write_tree_source_node(FILE *f, struct node *tree, int level) { struct property *prop; struct node *child; |
658f29a51 of/flattree: Upda... |
280 |
struct label *l; |
c2e7075ca scripts/dtc: Upda... |
281 |
char *srcstr; |
a4da2e3ec [POWERPC] Merge d... |
282 283 |
write_prefix(f, level); |
658f29a51 of/flattree: Upda... |
284 285 |
for_each_label(tree->labels, l) fprintf(f, "%s: ", l->label); |
a4da2e3ec [POWERPC] Merge d... |
286 |
if (tree->name && (*tree->name)) |
c2e7075ca scripts/dtc: Upda... |
287 |
fprintf(f, "%s {", tree->name); |
a4da2e3ec [POWERPC] Merge d... |
288 |
else |
c2e7075ca scripts/dtc: Upda... |
289 290 291 292 293 294 295 296 297 298 299 |
fprintf(f, "/ {"); if (annotate) { srcstr = srcpos_string_first(tree->srcpos, annotate); if (srcstr) { fprintf(f, " /* %s */", srcstr); free(srcstr); } } fprintf(f, " "); |
a4da2e3ec [POWERPC] Merge d... |
300 301 302 |
for_each_property(tree, prop) { write_prefix(f, level+1); |
658f29a51 of/flattree: Upda... |
303 304 |
for_each_label(prop->labels, l) fprintf(f, "%s: ", l->label); |
a4da2e3ec [POWERPC] Merge d... |
305 306 307 308 309 310 311 312 313 |
fprintf(f, "%s", prop->name); write_propval(f, prop); } for_each_child(tree, child) { fprintf(f, " "); write_tree_source_node(f, child, level+1); } write_prefix(f, level); |
c2e7075ca scripts/dtc: Upda... |
314 315 316 317 318 319 320 321 322 323 |
fprintf(f, "};"); if (annotate) { srcstr = srcpos_string_last(tree->srcpos, annotate); if (srcstr) { fprintf(f, " /* %s */", srcstr); free(srcstr); } } fprintf(f, " "); |
a4da2e3ec [POWERPC] Merge d... |
324 |
} |
6f05afcbb scripts/dtc: Upda... |
325 |
void dt_to_source(FILE *f, struct dt_info *dti) |
a4da2e3ec [POWERPC] Merge d... |
326 327 328 329 330 331 |
{ struct reserve_info *re; fprintf(f, "/dts-v1/; "); |
6f05afcbb scripts/dtc: Upda... |
332 |
for (re = dti->reservelist; re; re = re->next) { |
658f29a51 of/flattree: Upda... |
333 334 335 336 |
struct label *l; for_each_label(re->labels, l) fprintf(f, "%s: ", l->label); |
a4da2e3ec [POWERPC] Merge d... |
337 338 |
fprintf(f, "/memreserve/\t0x%016llx 0x%016llx; ", |
89d123106 scripts/dtc: Upda... |
339 340 |
(unsigned long long)re->address, (unsigned long long)re->size); |
a4da2e3ec [POWERPC] Merge d... |
341 |
} |
6f05afcbb scripts/dtc: Upda... |
342 |
write_tree_source_node(f, dti->dt, 0); |
a4da2e3ec [POWERPC] Merge d... |
343 |
} |