treesource.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 void add_string_markers(struct property *prop)
  124. {
  125. int l, len = prop->val.len;
  126. const char *p = prop->val.val;
  127. for (l = strlen(p) + 1; l < len; l += strlen(p + l) + 1) {
  128. struct marker *m, **nextp;
  129. m = xmalloc(sizeof(*m));
  130. m->offset = l;
  131. m->type = TYPE_STRING;
  132. m->ref = NULL;
  133. m->next = NULL;
  134. /* Find the end of the markerlist */
  135. nextp = &prop->val.markers;
  136. while (*nextp)
  137. nextp = &((*nextp)->next);
  138. *nextp = m;
  139. }
  140. }
  141. static enum markertype guess_value_type(struct property *prop)
  142. {
  143. int len = prop->val.len;
  144. const char *p = prop->val.val;
  145. struct marker *m = prop->val.markers;
  146. int nnotstring = 0, nnul = 0;
  147. int nnotstringlbl = 0, nnotcelllbl = 0;
  148. int i;
  149. for (i = 0; i < len; i++) {
  150. if (! isstring(p[i]))
  151. nnotstring++;
  152. if (p[i] == '\0')
  153. nnul++;
  154. }
  155. for_each_marker_of_type(m, LABEL) {
  156. if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
  157. nnotstringlbl++;
  158. if ((m->offset % sizeof(cell_t)) != 0)
  159. nnotcelllbl++;
  160. }
  161. if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul <= (len-nnul))
  162. && (nnotstringlbl == 0)) {
  163. if (nnul > 1)
  164. add_string_markers(prop);
  165. return TYPE_STRING;
  166. } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
  167. return TYPE_UINT32;
  168. }
  169. return TYPE_UINT8;
  170. }
  171. static void write_propval(FILE *f, struct property *prop)
  172. {
  173. size_t len = prop->val.len;
  174. struct marker *m = prop->val.markers;
  175. struct marker dummy_marker;
  176. enum markertype emit_type = TYPE_NONE;
  177. char *srcstr;
  178. if (len == 0) {
  179. fprintf(f, ";");
  180. if (annotate) {
  181. srcstr = srcpos_string_first(prop->srcpos, annotate);
  182. if (srcstr) {
  183. fprintf(f, " /* %s */", srcstr);
  184. free(srcstr);
  185. }
  186. }
  187. fprintf(f, "\n");
  188. return;
  189. }
  190. fprintf(f, " =");
  191. if (!next_type_marker(m)) {
  192. /* data type information missing, need to guess */
  193. dummy_marker.type = guess_value_type(prop);
  194. dummy_marker.next = prop->val.markers;
  195. dummy_marker.offset = 0;
  196. dummy_marker.ref = NULL;
  197. m = &dummy_marker;
  198. }
  199. for_each_marker(m) {
  200. size_t chunk_len = (m->next ? m->next->offset : len) - m->offset;
  201. size_t data_len = type_marker_length(m) ? : len - m->offset;
  202. const char *p = &prop->val.val[m->offset];
  203. struct marker *m_phandle;
  204. if (is_type_marker(m->type)) {
  205. emit_type = m->type;
  206. fprintf(f, " %s", delim_start[emit_type]);
  207. } else if (m->type == LABEL)
  208. fprintf(f, " %s:", m->ref);
  209. if (emit_type == TYPE_NONE || chunk_len == 0)
  210. continue;
  211. switch(emit_type) {
  212. case TYPE_UINT16:
  213. write_propval_int(f, p, chunk_len, 2);
  214. break;
  215. case TYPE_UINT32:
  216. m_phandle = prop->val.markers;
  217. for_each_marker_of_type(m_phandle, REF_PHANDLE)
  218. if (m->offset == m_phandle->offset)
  219. break;
  220. if (m_phandle) {
  221. if (m_phandle->ref[0] == '/')
  222. fprintf(f, "&{%s}", m_phandle->ref);
  223. else
  224. fprintf(f, "&%s", m_phandle->ref);
  225. if (chunk_len > 4) {
  226. fputc(' ', f);
  227. write_propval_int(f, p + 4, chunk_len - 4, 4);
  228. }
  229. } else {
  230. write_propval_int(f, p, chunk_len, 4);
  231. }
  232. if (data_len > chunk_len)
  233. fputc(' ', f);
  234. break;
  235. case TYPE_UINT64:
  236. write_propval_int(f, p, chunk_len, 8);
  237. break;
  238. case TYPE_STRING:
  239. write_propval_string(f, p, chunk_len);
  240. break;
  241. default:
  242. write_propval_int(f, p, chunk_len, 1);
  243. }
  244. if (chunk_len == data_len) {
  245. size_t pos = m->offset + chunk_len;
  246. fprintf(f, pos == len ? "%s" : "%s,",
  247. delim_end[emit_type] ? : "");
  248. emit_type = TYPE_NONE;
  249. }
  250. }
  251. fprintf(f, ";");
  252. if (annotate) {
  253. srcstr = srcpos_string_first(prop->srcpos, annotate);
  254. if (srcstr) {
  255. fprintf(f, " /* %s */", srcstr);
  256. free(srcstr);
  257. }
  258. }
  259. fprintf(f, "\n");
  260. }
  261. static void write_tree_source_node(FILE *f, struct node *tree, int level)
  262. {
  263. struct property *prop;
  264. struct node *child;
  265. struct label *l;
  266. char *srcstr;
  267. write_prefix(f, level);
  268. for_each_label(tree->labels, l)
  269. fprintf(f, "%s: ", l->label);
  270. if (tree->name && (*tree->name))
  271. fprintf(f, "%s {", tree->name);
  272. else
  273. fprintf(f, "/ {");
  274. if (annotate) {
  275. srcstr = srcpos_string_first(tree->srcpos, annotate);
  276. if (srcstr) {
  277. fprintf(f, " /* %s */", srcstr);
  278. free(srcstr);
  279. }
  280. }
  281. fprintf(f, "\n");
  282. for_each_property(tree, prop) {
  283. write_prefix(f, level+1);
  284. for_each_label(prop->labels, l)
  285. fprintf(f, "%s: ", l->label);
  286. fprintf(f, "%s", prop->name);
  287. write_propval(f, prop);
  288. }
  289. for_each_child(tree, child) {
  290. fprintf(f, "\n");
  291. write_tree_source_node(f, child, level+1);
  292. }
  293. write_prefix(f, level);
  294. fprintf(f, "};");
  295. if (annotate) {
  296. srcstr = srcpos_string_last(tree->srcpos, annotate);
  297. if (srcstr) {
  298. fprintf(f, " /* %s */", srcstr);
  299. free(srcstr);
  300. }
  301. }
  302. fprintf(f, "\n");
  303. }
  304. void dt_to_source(FILE *f, struct dt_info *dti)
  305. {
  306. struct reserve_info *re;
  307. fprintf(f, "/dts-v1/;\n\n");
  308. for (re = dti->reservelist; re; re = re->next) {
  309. struct label *l;
  310. for_each_label(re->labels, l)
  311. fprintf(f, "%s: ", l->label);
  312. fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
  313. (unsigned long long)re->address,
  314. (unsigned long long)re->size);
  315. }
  316. write_tree_source_node(f, dti->dt, 0);
  317. }