treesource.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. struct boot_info *the_boot_info;
  25. int treesource_error;
  26. struct boot_info *dt_from_source(const char *fname)
  27. {
  28. the_boot_info = NULL;
  29. treesource_error = 0;
  30. srcfile_push(fname);
  31. yyin = current_srcfile->f;
  32. if (yyparse() != 0)
  33. die("Unable to parse input tree\n");
  34. if (treesource_error)
  35. die("Syntax error parsing input tree\n");
  36. return the_boot_info;
  37. }
  38. static void write_prefix(FILE *f, int level)
  39. {
  40. int i;
  41. for (i = 0; i < level; i++)
  42. fputc('\t', f);
  43. }
  44. static int isstring(char c)
  45. {
  46. return (isprint(c)
  47. || (c == '\0')
  48. || strchr("\a\b\t\n\v\f\r", c));
  49. }
  50. static void write_propval_string(FILE *f, struct data val)
  51. {
  52. const char *str = val.val;
  53. int i;
  54. struct marker *m = val.markers;
  55. assert(str[val.len-1] == '\0');
  56. while (m && (m->offset == 0)) {
  57. if (m->type == LABEL)
  58. fprintf(f, "%s: ", m->ref);
  59. m = m->next;
  60. }
  61. fprintf(f, "\"");
  62. for (i = 0; i < (val.len-1); i++) {
  63. char c = str[i];
  64. switch (c) {
  65. case '\a':
  66. fprintf(f, "\\a");
  67. break;
  68. case '\b':
  69. fprintf(f, "\\b");
  70. break;
  71. case '\t':
  72. fprintf(f, "\\t");
  73. break;
  74. case '\n':
  75. fprintf(f, "\\n");
  76. break;
  77. case '\v':
  78. fprintf(f, "\\v");
  79. break;
  80. case '\f':
  81. fprintf(f, "\\f");
  82. break;
  83. case '\r':
  84. fprintf(f, "\\r");
  85. break;
  86. case '\\':
  87. fprintf(f, "\\\\");
  88. break;
  89. case '\"':
  90. fprintf(f, "\\\"");
  91. break;
  92. case '\0':
  93. fprintf(f, "\", ");
  94. while (m && (m->offset < i)) {
  95. if (m->type == LABEL) {
  96. assert(m->offset == (i+1));
  97. fprintf(f, "%s: ", m->ref);
  98. }
  99. m = m->next;
  100. }
  101. fprintf(f, "\"");
  102. break;
  103. default:
  104. if (isprint(c))
  105. fprintf(f, "%c", c);
  106. else
  107. fprintf(f, "\\x%02hhx", c);
  108. }
  109. }
  110. fprintf(f, "\"");
  111. /* Wrap up any labels at the end of the value */
  112. for_each_marker_of_type(m, LABEL) {
  113. assert (m->offset == val.len);
  114. fprintf(f, " %s:", m->ref);
  115. }
  116. }
  117. static void write_propval_cells(FILE *f, struct data val)
  118. {
  119. void *propend = val.val + val.len;
  120. cell_t *cp = (cell_t *)val.val;
  121. struct marker *m = val.markers;
  122. fprintf(f, "<");
  123. for (;;) {
  124. while (m && (m->offset <= ((char *)cp - val.val))) {
  125. if (m->type == LABEL) {
  126. assert(m->offset == ((char *)cp - val.val));
  127. fprintf(f, "%s: ", m->ref);
  128. }
  129. m = m->next;
  130. }
  131. fprintf(f, "0x%x", fdt32_to_cpu(*cp++));
  132. if ((void *)cp >= propend)
  133. break;
  134. fprintf(f, " ");
  135. }
  136. /* Wrap up any labels at the end of the value */
  137. for_each_marker_of_type(m, LABEL) {
  138. assert (m->offset == val.len);
  139. fprintf(f, " %s:", m->ref);
  140. }
  141. fprintf(f, ">");
  142. }
  143. static void write_propval_bytes(FILE *f, struct data val)
  144. {
  145. void *propend = val.val + val.len;
  146. const char *bp = val.val;
  147. struct marker *m = val.markers;
  148. fprintf(f, "[");
  149. for (;;) {
  150. while (m && (m->offset == (bp-val.val))) {
  151. if (m->type == LABEL)
  152. fprintf(f, "%s: ", m->ref);
  153. m = m->next;
  154. }
  155. fprintf(f, "%02hhx", *bp++);
  156. if ((const void *)bp >= propend)
  157. break;
  158. fprintf(f, " ");
  159. }
  160. /* Wrap up any labels at the end of the value */
  161. for_each_marker_of_type(m, LABEL) {
  162. assert (m->offset == val.len);
  163. fprintf(f, " %s:", m->ref);
  164. }
  165. fprintf(f, "]");
  166. }
  167. static void write_propval(FILE *f, struct property *prop)
  168. {
  169. int len = prop->val.len;
  170. const char *p = prop->val.val;
  171. struct marker *m = prop->val.markers;
  172. int nnotstring = 0, nnul = 0;
  173. int nnotstringlbl = 0, nnotcelllbl = 0;
  174. int i;
  175. if (len == 0) {
  176. fprintf(f, ";\n");
  177. return;
  178. }
  179. for (i = 0; i < len; i++) {
  180. if (! isstring(p[i]))
  181. nnotstring++;
  182. if (p[i] == '\0')
  183. nnul++;
  184. }
  185. for_each_marker_of_type(m, LABEL) {
  186. if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
  187. nnotstringlbl++;
  188. if ((m->offset % sizeof(cell_t)) != 0)
  189. nnotcelllbl++;
  190. }
  191. fprintf(f, " = ");
  192. if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
  193. && (nnotstringlbl == 0)) {
  194. write_propval_string(f, prop->val);
  195. } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
  196. write_propval_cells(f, prop->val);
  197. } else {
  198. write_propval_bytes(f, prop->val);
  199. }
  200. fprintf(f, ";\n");
  201. }
  202. static void write_tree_source_node(FILE *f, struct node *tree, int level)
  203. {
  204. struct property *prop;
  205. struct node *child;
  206. struct label *l;
  207. write_prefix(f, level);
  208. for_each_label(tree->labels, l)
  209. fprintf(f, "%s: ", l->label);
  210. if (tree->name && (*tree->name))
  211. fprintf(f, "%s {\n", tree->name);
  212. else
  213. fprintf(f, "/ {\n");
  214. for_each_property(tree, prop) {
  215. write_prefix(f, level+1);
  216. for_each_label(prop->labels, l)
  217. fprintf(f, "%s: ", l->label);
  218. fprintf(f, "%s", prop->name);
  219. write_propval(f, prop);
  220. }
  221. for_each_child(tree, child) {
  222. fprintf(f, "\n");
  223. write_tree_source_node(f, child, level+1);
  224. }
  225. write_prefix(f, level);
  226. fprintf(f, "};\n");
  227. }
  228. void dt_to_source(FILE *f, struct boot_info *bi)
  229. {
  230. struct reserve_info *re;
  231. fprintf(f, "/dts-v1/;\n\n");
  232. for (re = bi->reservelist; re; re = re->next) {
  233. struct label *l;
  234. for_each_label(re->labels, l)
  235. fprintf(f, "%s: ", l->label);
  236. fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
  237. (unsigned long long)re->re.address,
  238. (unsigned long long)re->re.size);
  239. }
  240. write_tree_source_node(f, bi->dt, 0);
  241. }