treesource.c 6.8 KB

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