treesource.c 5.8 KB

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