treesource.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
  3. *
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  18. * USA
  19. */
  20. #include "dtc.h"
  21. #include "srcpos.h"
  22. extern FILE *yyin;
  23. extern int yyparse(void);
  24. extern YYLTYPE yylloc;
  25. struct boot_info *the_boot_info;
  26. bool treesource_error;
  27. struct boot_info *dt_from_source(const char *fname)
  28. {
  29. the_boot_info = NULL;
  30. treesource_error = false;
  31. srcfile_push(fname);
  32. yyin = current_srcfile->f;
  33. yylloc.file = current_srcfile;
  34. if (yyparse() != 0)
  35. die("Unable to parse input tree\n");
  36. if (treesource_error)
  37. die("Syntax error parsing input tree\n");
  38. return the_boot_info;
  39. }
  40. static void write_prefix(FILE *f, int level)
  41. {
  42. int i;
  43. for (i = 0; i < level; i++)
  44. fputc('\t', f);
  45. }
  46. static bool isstring(char c)
  47. {
  48. return (isprint((unsigned char)c)
  49. || (c == '\0')
  50. || strchr("\a\b\t\n\v\f\r", c));
  51. }
  52. static void write_propval_string(FILE *f, struct data val)
  53. {
  54. const char *str = val.val;
  55. int i;
  56. struct marker *m = val.markers;
  57. assert(str[val.len-1] == '\0');
  58. while (m && (m->offset == 0)) {
  59. if (m->type == LABEL)
  60. fprintf(f, "%s: ", m->ref);
  61. m = m->next;
  62. }
  63. fprintf(f, "\"");
  64. for (i = 0; i < (val.len-1); i++) {
  65. char c = str[i];
  66. switch (c) {
  67. case '\a':
  68. fprintf(f, "\\a");
  69. break;
  70. case '\b':
  71. fprintf(f, "\\b");
  72. break;
  73. case '\t':
  74. fprintf(f, "\\t");
  75. break;
  76. case '\n':
  77. fprintf(f, "\\n");
  78. break;
  79. case '\v':
  80. fprintf(f, "\\v");
  81. break;
  82. case '\f':
  83. fprintf(f, "\\f");
  84. break;
  85. case '\r':
  86. fprintf(f, "\\r");
  87. break;
  88. case '\\':
  89. fprintf(f, "\\\\");
  90. break;
  91. case '\"':
  92. fprintf(f, "\\\"");
  93. break;
  94. case '\0':
  95. fprintf(f, "\", ");
  96. while (m && (m->offset <= (i + 1))) {
  97. if (m->type == LABEL) {
  98. assert(m->offset == (i+1));
  99. fprintf(f, "%s: ", m->ref);
  100. }
  101. m = m->next;
  102. }
  103. fprintf(f, "\"");
  104. break;
  105. default:
  106. if (isprint((unsigned char)c))
  107. fprintf(f, "%c", c);
  108. else
  109. fprintf(f, "\\x%02hhx", c);
  110. }
  111. }
  112. fprintf(f, "\"");
  113. /* Wrap up any labels at the end of the value */
  114. for_each_marker_of_type(m, LABEL) {
  115. assert (m->offset == val.len);
  116. fprintf(f, " %s:", m->ref);
  117. }
  118. }
  119. static void write_propval_cells(FILE *f, struct data val)
  120. {
  121. void *propend = val.val + val.len;
  122. cell_t *cp = (cell_t *)val.val;
  123. struct marker *m = val.markers;
  124. fprintf(f, "<");
  125. for (;;) {
  126. while (m && (m->offset <= ((char *)cp - val.val))) {
  127. if (m->type == LABEL) {
  128. assert(m->offset == ((char *)cp - val.val));
  129. fprintf(f, "%s: ", m->ref);
  130. }
  131. m = m->next;
  132. }
  133. fprintf(f, "0x%x", fdt32_to_cpu(*cp++));
  134. if ((void *)cp >= propend)
  135. break;
  136. fprintf(f, " ");
  137. }
  138. /* Wrap up any labels at the end of the value */
  139. for_each_marker_of_type(m, LABEL) {
  140. assert (m->offset == val.len);
  141. fprintf(f, " %s:", m->ref);
  142. }
  143. fprintf(f, ">");
  144. }
  145. static void write_propval_bytes(FILE *f, struct data val)
  146. {
  147. void *propend = val.val + val.len;
  148. const char *bp = val.val;
  149. struct marker *m = val.markers;
  150. fprintf(f, "[");
  151. for (;;) {
  152. while (m && (m->offset == (bp-val.val))) {
  153. if (m->type == LABEL)
  154. fprintf(f, "%s: ", m->ref);
  155. m = m->next;
  156. }
  157. fprintf(f, "%02hhx", (unsigned char)(*bp++));
  158. if ((const void *)bp >= propend)
  159. break;
  160. fprintf(f, " ");
  161. }
  162. /* Wrap up any labels at the end of the value */
  163. for_each_marker_of_type(m, LABEL) {
  164. assert (m->offset == val.len);
  165. fprintf(f, " %s:", m->ref);
  166. }
  167. fprintf(f, "]");
  168. }
  169. static void write_propval(FILE *f, struct property *prop)
  170. {
  171. int len = prop->val.len;
  172. const char *p = prop->val.val;
  173. struct marker *m = prop->val.markers;
  174. int nnotstring = 0, nnul = 0;
  175. int nnotstringlbl = 0, nnotcelllbl = 0;
  176. int i;
  177. if (len == 0) {
  178. fprintf(f, ";\n");
  179. return;
  180. }
  181. for (i = 0; i < len; i++) {
  182. if (! isstring(p[i]))
  183. nnotstring++;
  184. if (p[i] == '\0')
  185. nnul++;
  186. }
  187. for_each_marker_of_type(m, LABEL) {
  188. if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
  189. nnotstringlbl++;
  190. if ((m->offset % sizeof(cell_t)) != 0)
  191. nnotcelllbl++;
  192. }
  193. fprintf(f, " = ");
  194. if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
  195. && (nnotstringlbl == 0)) {
  196. write_propval_string(f, prop->val);
  197. } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
  198. write_propval_cells(f, prop->val);
  199. } else {
  200. write_propval_bytes(f, prop->val);
  201. }
  202. fprintf(f, ";\n");
  203. }
  204. static void write_tree_source_node(FILE *f, struct node *tree, int level)
  205. {
  206. struct property *prop;
  207. struct node *child;
  208. struct label *l;
  209. write_prefix(f, level);
  210. for_each_label(tree->labels, l)
  211. fprintf(f, "%s: ", l->label);
  212. if (tree->name && (*tree->name))
  213. fprintf(f, "%s {\n", tree->name);
  214. else
  215. fprintf(f, "/ {\n");
  216. for_each_property(tree, prop) {
  217. write_prefix(f, level+1);
  218. for_each_label(prop->labels, l)
  219. fprintf(f, "%s: ", l->label);
  220. fprintf(f, "%s", prop->name);
  221. write_propval(f, prop);
  222. }
  223. for_each_child(tree, child) {
  224. fprintf(f, "\n");
  225. write_tree_source_node(f, child, level+1);
  226. }
  227. write_prefix(f, level);
  228. fprintf(f, "};\n");
  229. }
  230. void dt_to_source(FILE *f, struct boot_info *bi)
  231. {
  232. struct reserve_info *re;
  233. fprintf(f, "/dts-v1/;\n\n");
  234. for (re = bi->reservelist; re; re = re->next) {
  235. struct label *l;
  236. for_each_label(re->labels, l)
  237. fprintf(f, "%s: ", l->label);
  238. fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
  239. (unsigned long long)re->re.address,
  240. (unsigned long long)re->re.size);
  241. }
  242. write_tree_source_node(f, bi->dt, 0);
  243. }