dtc-parser.y 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. %{
  21. #include <stdio.h>
  22. #include "dtc.h"
  23. #include "srcpos.h"
  24. YYLTYPE yylloc;
  25. extern int yylex(void);
  26. extern void print_error(char const *fmt, ...);
  27. extern void yyerror(char const *s);
  28. extern struct boot_info *the_boot_info;
  29. extern int treesource_error;
  30. static unsigned long long eval_literal(const char *s, int base, int bits);
  31. %}
  32. %union {
  33. char *propnodename;
  34. char *literal;
  35. char *labelref;
  36. unsigned int cbase;
  37. uint8_t byte;
  38. struct data data;
  39. uint64_t addr;
  40. cell_t cell;
  41. struct property *prop;
  42. struct property *proplist;
  43. struct node *node;
  44. struct node *nodelist;
  45. struct reserve_info *re;
  46. }
  47. %token DT_V1
  48. %token DT_MEMRESERVE
  49. %token <propnodename> DT_PROPNODENAME
  50. %token <literal> DT_LITERAL
  51. %token <cbase> DT_BASE
  52. %token <byte> DT_BYTE
  53. %token <data> DT_STRING
  54. %token <labelref> DT_LABEL
  55. %token <labelref> DT_REF
  56. %token DT_INCBIN
  57. %type <data> propdata
  58. %type <data> propdataprefix
  59. %type <re> memreserve
  60. %type <re> memreserves
  61. %type <addr> addr
  62. %type <data> celllist
  63. %type <cell> cellval
  64. %type <data> bytestring
  65. %type <prop> propdef
  66. %type <proplist> proplist
  67. %type <node> devicetree
  68. %type <node> nodedef
  69. %type <node> subnode
  70. %type <nodelist> subnodes
  71. %%
  72. sourcefile:
  73. DT_V1 ';' memreserves devicetree
  74. {
  75. the_boot_info = build_boot_info($3, $4,
  76. guess_boot_cpuid($4));
  77. }
  78. ;
  79. memreserves:
  80. /* empty */
  81. {
  82. $$ = NULL;
  83. }
  84. | memreserve memreserves
  85. {
  86. $$ = chain_reserve_entry($1, $2);
  87. }
  88. ;
  89. memreserve:
  90. DT_MEMRESERVE addr addr ';'
  91. {
  92. $$ = build_reserve_entry($2, $3);
  93. }
  94. | DT_LABEL memreserve
  95. {
  96. add_label(&$2->labels, $1);
  97. $$ = $2;
  98. }
  99. ;
  100. addr:
  101. DT_LITERAL
  102. {
  103. $$ = eval_literal($1, 0, 64);
  104. }
  105. ;
  106. devicetree:
  107. '/' nodedef
  108. {
  109. $$ = name_node($2, "");
  110. }
  111. | devicetree '/' nodedef
  112. {
  113. $$ = merge_nodes($1, $3);
  114. }
  115. | devicetree DT_REF nodedef
  116. {
  117. struct node *target = get_node_by_ref($1, $2);
  118. if (target)
  119. merge_nodes(target, $3);
  120. else
  121. print_error("label or path, '%s', not found", $2);
  122. $$ = $1;
  123. }
  124. ;
  125. nodedef:
  126. '{' proplist subnodes '}' ';'
  127. {
  128. $$ = build_node($2, $3);
  129. }
  130. ;
  131. proplist:
  132. /* empty */
  133. {
  134. $$ = NULL;
  135. }
  136. | proplist propdef
  137. {
  138. $$ = chain_property($2, $1);
  139. }
  140. ;
  141. propdef:
  142. DT_PROPNODENAME '=' propdata ';'
  143. {
  144. $$ = build_property($1, $3);
  145. }
  146. | DT_PROPNODENAME ';'
  147. {
  148. $$ = build_property($1, empty_data);
  149. }
  150. | DT_LABEL propdef
  151. {
  152. add_label(&$2->labels, $1);
  153. $$ = $2;
  154. }
  155. ;
  156. propdata:
  157. propdataprefix DT_STRING
  158. {
  159. $$ = data_merge($1, $2);
  160. }
  161. | propdataprefix '<' celllist '>'
  162. {
  163. $$ = data_merge($1, $3);
  164. }
  165. | propdataprefix '[' bytestring ']'
  166. {
  167. $$ = data_merge($1, $3);
  168. }
  169. | propdataprefix DT_REF
  170. {
  171. $$ = data_add_marker($1, REF_PATH, $2);
  172. }
  173. | propdataprefix DT_INCBIN '(' DT_STRING ',' addr ',' addr ')'
  174. {
  175. FILE *f = srcfile_relative_open($4.val, NULL);
  176. struct data d;
  177. if ($6 != 0)
  178. if (fseek(f, $6, SEEK_SET) != 0)
  179. print_error("Couldn't seek to offset %llu in \"%s\": %s",
  180. (unsigned long long)$6,
  181. $4.val,
  182. strerror(errno));
  183. d = data_copy_file(f, $8);
  184. $$ = data_merge($1, d);
  185. fclose(f);
  186. }
  187. | propdataprefix DT_INCBIN '(' DT_STRING ')'
  188. {
  189. FILE *f = srcfile_relative_open($4.val, NULL);
  190. struct data d = empty_data;
  191. d = data_copy_file(f, -1);
  192. $$ = data_merge($1, d);
  193. fclose(f);
  194. }
  195. | propdata DT_LABEL
  196. {
  197. $$ = data_add_marker($1, LABEL, $2);
  198. }
  199. ;
  200. propdataprefix:
  201. /* empty */
  202. {
  203. $$ = empty_data;
  204. }
  205. | propdata ','
  206. {
  207. $$ = $1;
  208. }
  209. | propdataprefix DT_LABEL
  210. {
  211. $$ = data_add_marker($1, LABEL, $2);
  212. }
  213. ;
  214. celllist:
  215. /* empty */
  216. {
  217. $$ = empty_data;
  218. }
  219. | celllist cellval
  220. {
  221. $$ = data_append_cell($1, $2);
  222. }
  223. | celllist DT_REF
  224. {
  225. $$ = data_append_cell(data_add_marker($1, REF_PHANDLE,
  226. $2), -1);
  227. }
  228. | celllist DT_LABEL
  229. {
  230. $$ = data_add_marker($1, LABEL, $2);
  231. }
  232. ;
  233. cellval:
  234. DT_LITERAL
  235. {
  236. $$ = eval_literal($1, 0, 32);
  237. }
  238. ;
  239. bytestring:
  240. /* empty */
  241. {
  242. $$ = empty_data;
  243. }
  244. | bytestring DT_BYTE
  245. {
  246. $$ = data_append_byte($1, $2);
  247. }
  248. | bytestring DT_LABEL
  249. {
  250. $$ = data_add_marker($1, LABEL, $2);
  251. }
  252. ;
  253. subnodes:
  254. /* empty */
  255. {
  256. $$ = NULL;
  257. }
  258. | subnode subnodes
  259. {
  260. $$ = chain_node($1, $2);
  261. }
  262. | subnode propdef
  263. {
  264. print_error("syntax error: properties must precede subnodes");
  265. YYERROR;
  266. }
  267. ;
  268. subnode:
  269. DT_PROPNODENAME nodedef
  270. {
  271. $$ = name_node($2, $1);
  272. }
  273. | DT_LABEL subnode
  274. {
  275. add_label(&$2->labels, $1);
  276. $$ = $2;
  277. }
  278. ;
  279. %%
  280. void print_error(char const *fmt, ...)
  281. {
  282. va_list va;
  283. va_start(va, fmt);
  284. srcpos_verror(&yylloc, fmt, va);
  285. va_end(va);
  286. treesource_error = 1;
  287. }
  288. void yyerror(char const *s) {
  289. print_error("%s", s);
  290. }
  291. static unsigned long long eval_literal(const char *s, int base, int bits)
  292. {
  293. unsigned long long val;
  294. char *e;
  295. errno = 0;
  296. val = strtoull(s, &e, base);
  297. if (*e)
  298. print_error("bad characters in literal");
  299. else if ((errno == ERANGE)
  300. || ((bits < 64) && (val >= (1ULL << bits))))
  301. print_error("literal out of range");
  302. else if (errno != 0)
  303. print_error("bad literal");
  304. return val;
  305. }