treesource.c 5.7 KB

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