blob: f412460f94d7478ad3fb9a81951f5c6a5e60c4ad [file] [log] [blame]
David Gibsona4da2e32007-12-18 15:06:42 +11001/*
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
David Gibsona4da2e32007-12-18 15:06:42 +110021%{
David Gibsoned95d742008-08-07 12:24:17 +100022#include <stdio.h>
23
David Gibsona4da2e32007-12-18 15:06:42 +110024#include "dtc.h"
25#include "srcpos.h"
26
John Bonesio658f29a2010-11-17 15:28:20 -080027YYLTYPE yylloc;
28
David Gibsoned95d742008-08-07 12:24:17 +100029extern int yylex(void);
John Bonesio658f29a2010-11-17 15:28:20 -080030extern void print_error(char const *fmt, ...);
31extern void yyerror(char const *s);
David Gibsona4da2e32007-12-18 15:06:42 +110032
33extern struct boot_info *the_boot_info;
David Gibsoned95d742008-08-07 12:24:17 +100034extern int treesource_error;
David Gibsona4da2e32007-12-18 15:06:42 +110035
David Gibsoned95d742008-08-07 12:24:17 +100036static unsigned long long eval_literal(const char *s, int base, int bits);
Stephen Warrencd296722012-09-28 21:25:59 +000037static unsigned char eval_char_literal(const char *s);
David Gibsona4da2e32007-12-18 15:06:42 +110038%}
39
40%union {
41 char *propnodename;
42 char *literal;
43 char *labelref;
44 unsigned int cbase;
David Gibsoned95d742008-08-07 12:24:17 +100045 uint8_t byte;
David Gibsona4da2e32007-12-18 15:06:42 +110046 struct data data;
47
Stephen Warrencd296722012-09-28 21:25:59 +000048 struct {
49 struct data data;
50 int bits;
51 } array;
52
David Gibsona4da2e32007-12-18 15:06:42 +110053 struct property *prop;
54 struct property *proplist;
55 struct node *node;
56 struct node *nodelist;
57 struct reserve_info *re;
Stephen Warrencd296722012-09-28 21:25:59 +000058 uint64_t integer;
David Gibsona4da2e32007-12-18 15:06:42 +110059}
60
61%token DT_V1
62%token DT_MEMRESERVE
Stephen Warrencd296722012-09-28 21:25:59 +000063%token DT_LSHIFT DT_RSHIFT DT_LE DT_GE DT_EQ DT_NE DT_AND DT_OR
64%token DT_BITS
65%token DT_DEL_PROP
66%token DT_DEL_NODE
David Gibsona4da2e32007-12-18 15:06:42 +110067%token <propnodename> DT_PROPNODENAME
68%token <literal> DT_LITERAL
Stephen Warrencd296722012-09-28 21:25:59 +000069%token <literal> DT_CHAR_LITERAL
David Gibsona4da2e32007-12-18 15:06:42 +110070%token <cbase> DT_BASE
71%token <byte> DT_BYTE
72%token <data> DT_STRING
73%token <labelref> DT_LABEL
74%token <labelref> DT_REF
David Gibsoned95d742008-08-07 12:24:17 +100075%token DT_INCBIN
David Gibsona4da2e32007-12-18 15:06:42 +110076
77%type <data> propdata
78%type <data> propdataprefix
79%type <re> memreserve
80%type <re> memreserves
Stephen Warrencd296722012-09-28 21:25:59 +000081%type <array> arrayprefix
David Gibsona4da2e32007-12-18 15:06:42 +110082%type <data> bytestring
83%type <prop> propdef
84%type <proplist> proplist
85
86%type <node> devicetree
87%type <node> nodedef
88%type <node> subnode
89%type <nodelist> subnodes
David Gibsona4da2e32007-12-18 15:06:42 +110090
Stephen Warrencd296722012-09-28 21:25:59 +000091%type <integer> integer_prim
92%type <integer> integer_unary
93%type <integer> integer_mul
94%type <integer> integer_add
95%type <integer> integer_shift
96%type <integer> integer_rela
97%type <integer> integer_eq
98%type <integer> integer_bitand
99%type <integer> integer_bitxor
100%type <integer> integer_bitor
101%type <integer> integer_and
102%type <integer> integer_or
103%type <integer> integer_trinary
104%type <integer> integer_expr
105
David Gibsona4da2e32007-12-18 15:06:42 +1100106%%
107
108sourcefile:
109 DT_V1 ';' memreserves devicetree
110 {
John Bonesio658f29a2010-11-17 15:28:20 -0800111 the_boot_info = build_boot_info($3, $4,
112 guess_boot_cpuid($4));
David Gibsona4da2e32007-12-18 15:06:42 +1100113 }
114 ;
115
116memreserves:
117 /* empty */
118 {
119 $$ = NULL;
120 }
121 | memreserve memreserves
122 {
123 $$ = chain_reserve_entry($1, $2);
124 }
125 ;
126
127memreserve:
Stephen Warrencd296722012-09-28 21:25:59 +0000128 DT_MEMRESERVE integer_prim integer_prim ';'
David Gibsona4da2e32007-12-18 15:06:42 +1100129 {
John Bonesio658f29a2010-11-17 15:28:20 -0800130 $$ = build_reserve_entry($2, $3);
David Gibsona4da2e32007-12-18 15:06:42 +1100131 }
John Bonesio658f29a2010-11-17 15:28:20 -0800132 | DT_LABEL memreserve
David Gibsona4da2e32007-12-18 15:06:42 +1100133 {
John Bonesio658f29a2010-11-17 15:28:20 -0800134 add_label(&$2->labels, $1);
135 $$ = $2;
David Gibsona4da2e32007-12-18 15:06:42 +1100136 }
137 ;
138
David Gibsona4da2e32007-12-18 15:06:42 +1100139devicetree:
140 '/' nodedef
141 {
John Bonesio658f29a2010-11-17 15:28:20 -0800142 $$ = name_node($2, "");
143 }
144 | devicetree '/' nodedef
145 {
146 $$ = merge_nodes($1, $3);
147 }
148 | devicetree DT_REF nodedef
149 {
150 struct node *target = get_node_by_ref($1, $2);
151
152 if (target)
153 merge_nodes(target, $3);
154 else
155 print_error("label or path, '%s', not found", $2);
156 $$ = $1;
David Gibsona4da2e32007-12-18 15:06:42 +1100157 }
Stephen Warrencd296722012-09-28 21:25:59 +0000158 | devicetree DT_DEL_NODE DT_REF ';'
159 {
160 struct node *target = get_node_by_ref($1, $3);
161
162 if (!target)
163 print_error("label or path, '%s', not found", $3);
164 else
165 delete_node(target);
166
167 $$ = $1;
168 }
David Gibsona4da2e32007-12-18 15:06:42 +1100169 ;
170
171nodedef:
172 '{' proplist subnodes '}' ';'
173 {
174 $$ = build_node($2, $3);
175 }
176 ;
177
178proplist:
179 /* empty */
180 {
181 $$ = NULL;
182 }
183 | proplist propdef
184 {
185 $$ = chain_property($2, $1);
186 }
187 ;
188
189propdef:
John Bonesio658f29a2010-11-17 15:28:20 -0800190 DT_PROPNODENAME '=' propdata ';'
David Gibsona4da2e32007-12-18 15:06:42 +1100191 {
John Bonesio658f29a2010-11-17 15:28:20 -0800192 $$ = build_property($1, $3);
David Gibsona4da2e32007-12-18 15:06:42 +1100193 }
John Bonesio658f29a2010-11-17 15:28:20 -0800194 | DT_PROPNODENAME ';'
David Gibsona4da2e32007-12-18 15:06:42 +1100195 {
John Bonesio658f29a2010-11-17 15:28:20 -0800196 $$ = build_property($1, empty_data);
197 }
Stephen Warrencd296722012-09-28 21:25:59 +0000198 | DT_DEL_PROP DT_PROPNODENAME ';'
199 {
200 $$ = build_property_delete($2);
201 }
John Bonesio658f29a2010-11-17 15:28:20 -0800202 | DT_LABEL propdef
203 {
204 add_label(&$2->labels, $1);
205 $$ = $2;
David Gibsona4da2e32007-12-18 15:06:42 +1100206 }
207 ;
208
209propdata:
210 propdataprefix DT_STRING
211 {
212 $$ = data_merge($1, $2);
213 }
Stephen Warrencd296722012-09-28 21:25:59 +0000214 | propdataprefix arrayprefix '>'
David Gibsona4da2e32007-12-18 15:06:42 +1100215 {
Stephen Warrencd296722012-09-28 21:25:59 +0000216 $$ = data_merge($1, $2.data);
David Gibsona4da2e32007-12-18 15:06:42 +1100217 }
218 | propdataprefix '[' bytestring ']'
219 {
220 $$ = data_merge($1, $3);
221 }
222 | propdataprefix DT_REF
223 {
224 $$ = data_add_marker($1, REF_PATH, $2);
225 }
Stephen Warrencd296722012-09-28 21:25:59 +0000226 | propdataprefix DT_INCBIN '(' DT_STRING ',' integer_prim ',' integer_prim ')'
David Gibsoned95d742008-08-07 12:24:17 +1000227 {
John Bonesio658f29a2010-11-17 15:28:20 -0800228 FILE *f = srcfile_relative_open($4.val, NULL);
229 struct data d;
David Gibsoned95d742008-08-07 12:24:17 +1000230
231 if ($6 != 0)
John Bonesio658f29a2010-11-17 15:28:20 -0800232 if (fseek(f, $6, SEEK_SET) != 0)
233 print_error("Couldn't seek to offset %llu in \"%s\": %s",
234 (unsigned long long)$6,
235 $4.val,
236 strerror(errno));
David Gibsoned95d742008-08-07 12:24:17 +1000237
John Bonesio658f29a2010-11-17 15:28:20 -0800238 d = data_copy_file(f, $8);
David Gibsoned95d742008-08-07 12:24:17 +1000239
240 $$ = data_merge($1, d);
John Bonesio658f29a2010-11-17 15:28:20 -0800241 fclose(f);
David Gibsoned95d742008-08-07 12:24:17 +1000242 }
243 | propdataprefix DT_INCBIN '(' DT_STRING ')'
244 {
John Bonesio658f29a2010-11-17 15:28:20 -0800245 FILE *f = srcfile_relative_open($4.val, NULL);
David Gibsoned95d742008-08-07 12:24:17 +1000246 struct data d = empty_data;
247
John Bonesio658f29a2010-11-17 15:28:20 -0800248 d = data_copy_file(f, -1);
David Gibsoned95d742008-08-07 12:24:17 +1000249
250 $$ = data_merge($1, d);
John Bonesio658f29a2010-11-17 15:28:20 -0800251 fclose(f);
David Gibsoned95d742008-08-07 12:24:17 +1000252 }
David Gibsona4da2e32007-12-18 15:06:42 +1100253 | propdata DT_LABEL
254 {
255 $$ = data_add_marker($1, LABEL, $2);
256 }
257 ;
258
259propdataprefix:
260 /* empty */
261 {
262 $$ = empty_data;
263 }
264 | propdata ','
265 {
266 $$ = $1;
267 }
268 | propdataprefix DT_LABEL
269 {
270 $$ = data_add_marker($1, LABEL, $2);
271 }
272 ;
273
Stephen Warrencd296722012-09-28 21:25:59 +0000274arrayprefix:
275 DT_BITS DT_LITERAL '<'
David Gibsona4da2e32007-12-18 15:06:42 +1100276 {
Stephen Warrencd296722012-09-28 21:25:59 +0000277 $$.data = empty_data;
278 $$.bits = eval_literal($2, 0, 7);
279
280 if (($$.bits != 8) &&
281 ($$.bits != 16) &&
282 ($$.bits != 32) &&
283 ($$.bits != 64))
284 {
285 print_error("Only 8, 16, 32 and 64-bit elements"
286 " are currently supported");
287 $$.bits = 32;
288 }
David Gibsona4da2e32007-12-18 15:06:42 +1100289 }
Stephen Warrencd296722012-09-28 21:25:59 +0000290 | '<'
David Gibsona4da2e32007-12-18 15:06:42 +1100291 {
Stephen Warrencd296722012-09-28 21:25:59 +0000292 $$.data = empty_data;
293 $$.bits = 32;
David Gibsona4da2e32007-12-18 15:06:42 +1100294 }
Stephen Warrencd296722012-09-28 21:25:59 +0000295 | arrayprefix integer_prim
David Gibsona4da2e32007-12-18 15:06:42 +1100296 {
Stephen Warrencd296722012-09-28 21:25:59 +0000297 if ($1.bits < 64) {
298 uint64_t mask = (1ULL << $1.bits) - 1;
299 /*
300 * Bits above mask must either be all zero
301 * (positive within range of mask) or all one
302 * (negative and sign-extended). The second
303 * condition is true if when we set all bits
304 * within the mask to one (i.e. | in the
305 * mask), all bits are one.
306 */
307 if (($2 > mask) && (($2 | mask) != -1ULL))
308 print_error(
309 "integer value out of range "
310 "%016lx (%d bits)", $1.bits);
311 }
312
313 $$.data = data_append_integer($1.data, $2, $1.bits);
David Gibsona4da2e32007-12-18 15:06:42 +1100314 }
Stephen Warrencd296722012-09-28 21:25:59 +0000315 | arrayprefix DT_REF
David Gibsona4da2e32007-12-18 15:06:42 +1100316 {
Stephen Warrencd296722012-09-28 21:25:59 +0000317 uint64_t val = ~0ULL >> (64 - $1.bits);
318
319 if ($1.bits == 32)
320 $1.data = data_add_marker($1.data,
321 REF_PHANDLE,
322 $2);
323 else
324 print_error("References are only allowed in "
325 "arrays with 32-bit elements.");
326
327 $$.data = data_append_integer($1.data, val, $1.bits);
328 }
329 | arrayprefix DT_LABEL
330 {
331 $$.data = data_add_marker($1.data, LABEL, $2);
David Gibsona4da2e32007-12-18 15:06:42 +1100332 }
333 ;
334
Stephen Warrencd296722012-09-28 21:25:59 +0000335integer_prim:
David Gibsona4da2e32007-12-18 15:06:42 +1100336 DT_LITERAL
337 {
Stephen Warrencd296722012-09-28 21:25:59 +0000338 $$ = eval_literal($1, 0, 64);
David Gibsona4da2e32007-12-18 15:06:42 +1100339 }
Stephen Warrencd296722012-09-28 21:25:59 +0000340 | DT_CHAR_LITERAL
341 {
342 $$ = eval_char_literal($1);
343 }
344 | '(' integer_expr ')'
345 {
346 $$ = $2;
347 }
348 ;
349
350integer_expr:
351 integer_trinary
352 ;
353
354integer_trinary:
355 integer_or
356 | integer_or '?' integer_expr ':' integer_trinary { $$ = $1 ? $3 : $5; }
357 ;
358
359integer_or:
360 integer_and
361 | integer_or DT_OR integer_and { $$ = $1 || $3; }
362 ;
363
364integer_and:
365 integer_bitor
366 | integer_and DT_AND integer_bitor { $$ = $1 && $3; }
367 ;
368
369integer_bitor:
370 integer_bitxor
371 | integer_bitor '|' integer_bitxor { $$ = $1 | $3; }
372 ;
373
374integer_bitxor:
375 integer_bitand
376 | integer_bitxor '^' integer_bitand { $$ = $1 ^ $3; }
377 ;
378
379integer_bitand:
380 integer_eq
381 | integer_bitand '&' integer_eq { $$ = $1 & $3; }
382 ;
383
384integer_eq:
385 integer_rela
386 | integer_eq DT_EQ integer_rela { $$ = $1 == $3; }
387 | integer_eq DT_NE integer_rela { $$ = $1 != $3; }
388 ;
389
390integer_rela:
391 integer_shift
392 | integer_rela '<' integer_shift { $$ = $1 < $3; }
393 | integer_rela '>' integer_shift { $$ = $1 > $3; }
394 | integer_rela DT_LE integer_shift { $$ = $1 <= $3; }
395 | integer_rela DT_GE integer_shift { $$ = $1 >= $3; }
396 ;
397
398integer_shift:
399 integer_shift DT_LSHIFT integer_add { $$ = $1 << $3; }
400 | integer_shift DT_RSHIFT integer_add { $$ = $1 >> $3; }
401 | integer_add
402 ;
403
404integer_add:
405 integer_add '+' integer_mul { $$ = $1 + $3; }
406 | integer_add '-' integer_mul { $$ = $1 - $3; }
407 | integer_mul
408 ;
409
410integer_mul:
411 integer_mul '*' integer_unary { $$ = $1 * $3; }
412 | integer_mul '/' integer_unary { $$ = $1 / $3; }
413 | integer_mul '%' integer_unary { $$ = $1 % $3; }
414 | integer_unary
415 ;
416
417integer_unary:
418 integer_prim
419 | '-' integer_unary { $$ = -$2; }
420 | '~' integer_unary { $$ = ~$2; }
421 | '!' integer_unary { $$ = !$2; }
David Gibsona4da2e32007-12-18 15:06:42 +1100422 ;
423
424bytestring:
425 /* empty */
426 {
427 $$ = empty_data;
428 }
429 | bytestring DT_BYTE
430 {
431 $$ = data_append_byte($1, $2);
432 }
433 | bytestring DT_LABEL
434 {
435 $$ = data_add_marker($1, LABEL, $2);
436 }
437 ;
438
439subnodes:
440 /* empty */
441 {
442 $$ = NULL;
443 }
John Bonesio658f29a2010-11-17 15:28:20 -0800444 | subnode subnodes
David Gibsona4da2e32007-12-18 15:06:42 +1100445 {
446 $$ = chain_node($1, $2);
447 }
448 | subnode propdef
449 {
John Bonesio658f29a2010-11-17 15:28:20 -0800450 print_error("syntax error: properties must precede subnodes");
David Gibsona4da2e32007-12-18 15:06:42 +1100451 YYERROR;
452 }
453 ;
454
455subnode:
John Bonesio658f29a2010-11-17 15:28:20 -0800456 DT_PROPNODENAME nodedef
David Gibsona4da2e32007-12-18 15:06:42 +1100457 {
John Bonesio658f29a2010-11-17 15:28:20 -0800458 $$ = name_node($2, $1);
David Gibsona4da2e32007-12-18 15:06:42 +1100459 }
Stephen Warrencd296722012-09-28 21:25:59 +0000460 | DT_DEL_NODE DT_PROPNODENAME ';'
461 {
462 $$ = name_node(build_node_delete(), $2);
463 }
John Bonesio658f29a2010-11-17 15:28:20 -0800464 | DT_LABEL subnode
David Gibsona4da2e32007-12-18 15:06:42 +1100465 {
John Bonesio658f29a2010-11-17 15:28:20 -0800466 add_label(&$2->labels, $1);
467 $$ = $2;
David Gibsona4da2e32007-12-18 15:06:42 +1100468 }
469 ;
470
471%%
472
John Bonesio658f29a2010-11-17 15:28:20 -0800473void print_error(char const *fmt, ...)
David Gibsona4da2e32007-12-18 15:06:42 +1100474{
David Gibsoned95d742008-08-07 12:24:17 +1000475 va_list va;
David Gibsona4da2e32007-12-18 15:06:42 +1100476
John Bonesio658f29a2010-11-17 15:28:20 -0800477 va_start(va, fmt);
478 srcpos_verror(&yylloc, fmt, va);
479 va_end(va);
David Gibsoned95d742008-08-07 12:24:17 +1000480
481 treesource_error = 1;
David Gibsona4da2e32007-12-18 15:06:42 +1100482}
483
John Bonesio658f29a2010-11-17 15:28:20 -0800484void yyerror(char const *s) {
485 print_error("%s", s);
David Gibsoned95d742008-08-07 12:24:17 +1000486}
487
488static unsigned long long eval_literal(const char *s, int base, int bits)
David Gibsona4da2e32007-12-18 15:06:42 +1100489{
490 unsigned long long val;
491 char *e;
492
493 errno = 0;
494 val = strtoull(s, &e, base);
Stephen Warrencd296722012-09-28 21:25:59 +0000495 if (*e) {
496 size_t uls = strspn(e, "UL");
497 if (e[uls])
498 print_error("bad characters in literal");
499 }
500 if ((errno == ERANGE)
David Gibsona4da2e32007-12-18 15:06:42 +1100501 || ((bits < 64) && (val >= (1ULL << bits))))
John Bonesio658f29a2010-11-17 15:28:20 -0800502 print_error("literal out of range");
David Gibsona4da2e32007-12-18 15:06:42 +1100503 else if (errno != 0)
John Bonesio658f29a2010-11-17 15:28:20 -0800504 print_error("bad literal");
David Gibsona4da2e32007-12-18 15:06:42 +1100505 return val;
506}
Stephen Warrencd296722012-09-28 21:25:59 +0000507
508static unsigned char eval_char_literal(const char *s)
509{
510 int i = 1;
511 char c = s[0];
512
513 if (c == '\0')
514 {
515 print_error("empty character literal");
516 return 0;
517 }
518
519 /*
520 * If the first character in the character literal is a \ then process
521 * the remaining characters as an escape encoding. If the first
522 * character is neither an escape or a terminator it should be the only
523 * character in the literal and will be returned.
524 */
525 if (c == '\\')
526 c = get_escape_char(s, &i);
527
528 if (s[i] != '\0')
529 print_error("malformed character literal");
530
531 return c;
532}