blob: ca3f5003427cb5e280eb6ec4343547f7f37bd428 [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 */
David Gibsona4da2e32007-12-18 15:06:42 +110020%{
David Gibsoned95d742008-08-07 12:24:17 +100021#include <stdio.h>
Rob Herring6f05afc2017-01-04 10:45:20 -060022#include <inttypes.h>
David Gibsoned95d742008-08-07 12:24:17 +100023
David Gibsona4da2e32007-12-18 15:06:42 +110024#include "dtc.h"
25#include "srcpos.h"
26
David Gibsoned95d742008-08-07 12:24:17 +100027extern int yylex(void);
John Bonesio658f29a2010-11-17 15:28:20 -080028extern void yyerror(char const *s);
Rob Herring47605972015-04-29 16:00:05 -050029#define ERROR(loc, ...) \
30 do { \
31 srcpos_error((loc), "Error", __VA_ARGS__); \
32 treesource_error = true; \
33 } while (0)
David Gibsona4da2e32007-12-18 15:06:42 +110034
Rob Herring6f05afc2017-01-04 10:45:20 -060035extern struct dt_info *parser_output;
Rob Herring47605972015-04-29 16:00:05 -050036extern bool treesource_error;
David Gibsona4da2e32007-12-18 15:06:42 +110037%}
38
39%union {
40 char *propnodename;
David Gibsona4da2e32007-12-18 15:06:42 +110041 char *labelref;
David Gibsoned95d742008-08-07 12:24:17 +100042 uint8_t byte;
David Gibsona4da2e32007-12-18 15:06:42 +110043 struct data data;
44
Stephen Warrencd296722012-09-28 21:25:59 +000045 struct {
46 struct data data;
47 int bits;
48 } array;
49
David Gibsona4da2e32007-12-18 15:06:42 +110050 struct property *prop;
51 struct property *proplist;
52 struct node *node;
53 struct node *nodelist;
54 struct reserve_info *re;
Stephen Warrencd296722012-09-28 21:25:59 +000055 uint64_t integer;
Rob Herring6f05afc2017-01-04 10:45:20 -060056 unsigned int flags;
David Gibsona4da2e32007-12-18 15:06:42 +110057}
58
59%token DT_V1
Rob Herring6f05afc2017-01-04 10:45:20 -060060%token DT_PLUGIN
David Gibsona4da2e32007-12-18 15:06:42 +110061%token DT_MEMRESERVE
Stephen Warrencd296722012-09-28 21:25:59 +000062%token DT_LSHIFT DT_RSHIFT DT_LE DT_GE DT_EQ DT_NE DT_AND DT_OR
63%token DT_BITS
64%token DT_DEL_PROP
65%token DT_DEL_NODE
David Gibsona4da2e32007-12-18 15:06:42 +110066%token <propnodename> DT_PROPNODENAME
Rob Herring47605972015-04-29 16:00:05 -050067%token <integer> DT_LITERAL
68%token <integer> DT_CHAR_LITERAL
David Gibsona4da2e32007-12-18 15:06:42 +110069%token <byte> DT_BYTE
70%token <data> DT_STRING
71%token <labelref> DT_LABEL
72%token <labelref> DT_REF
David Gibsoned95d742008-08-07 12:24:17 +100073%token DT_INCBIN
David Gibsona4da2e32007-12-18 15:06:42 +110074
75%type <data> propdata
76%type <data> propdataprefix
Rob Herring6f05afc2017-01-04 10:45:20 -060077%type <flags> header
78%type <flags> headers
David Gibsona4da2e32007-12-18 15:06:42 +110079%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:
Rob Herring6f05afc2017-01-04 10:45:20 -0600109 headers memreserves devicetree
David Gibsona4da2e32007-12-18 15:06:42 +1100110 {
Rob Herring6f05afc2017-01-04 10:45:20 -0600111 parser_output = build_dt_info($1, $2, $3,
112 guess_boot_cpuid($3));
113 }
114 ;
115
116header:
117 DT_V1 ';'
118 {
119 $$ = DTSF_V1;
120 }
121 | DT_V1 ';' DT_PLUGIN ';'
122 {
123 $$ = DTSF_V1 | DTSF_PLUGIN;
124 }
125 ;
126
127headers:
128 header
129 | header headers
130 {
131 if ($2 != $1)
132 ERROR(&@2, "Header flags don't match earlier ones");
133 $$ = $1;
David Gibsona4da2e32007-12-18 15:06:42 +1100134 }
135 ;
136
137memreserves:
138 /* empty */
139 {
140 $$ = NULL;
141 }
142 | memreserve memreserves
143 {
144 $$ = chain_reserve_entry($1, $2);
145 }
146 ;
147
148memreserve:
Stephen Warrencd296722012-09-28 21:25:59 +0000149 DT_MEMRESERVE integer_prim integer_prim ';'
David Gibsona4da2e32007-12-18 15:06:42 +1100150 {
John Bonesio658f29a2010-11-17 15:28:20 -0800151 $$ = build_reserve_entry($2, $3);
David Gibsona4da2e32007-12-18 15:06:42 +1100152 }
John Bonesio658f29a2010-11-17 15:28:20 -0800153 | DT_LABEL memreserve
David Gibsona4da2e32007-12-18 15:06:42 +1100154 {
John Bonesio658f29a2010-11-17 15:28:20 -0800155 add_label(&$2->labels, $1);
156 $$ = $2;
David Gibsona4da2e32007-12-18 15:06:42 +1100157 }
158 ;
159
David Gibsona4da2e32007-12-18 15:06:42 +1100160devicetree:
161 '/' nodedef
162 {
John Bonesio658f29a2010-11-17 15:28:20 -0800163 $$ = name_node($2, "");
164 }
165 | devicetree '/' nodedef
166 {
167 $$ = merge_nodes($1, $3);
168 }
Rob Herring47605972015-04-29 16:00:05 -0500169
170 | devicetree DT_LABEL DT_REF nodedef
171 {
172 struct node *target = get_node_by_ref($1, $3);
173
Rob Herring89d12312017-03-21 09:01:08 -0500174 if (target) {
175 add_label(&target->labels, $2);
Rob Herring47605972015-04-29 16:00:05 -0500176 merge_nodes(target, $4);
Rob Herring89d12312017-03-21 09:01:08 -0500177 } else
Rob Herring47605972015-04-29 16:00:05 -0500178 ERROR(&@3, "Label or path %s not found", $3);
179 $$ = $1;
180 }
John Bonesio658f29a2010-11-17 15:28:20 -0800181 | devicetree DT_REF nodedef
182 {
183 struct node *target = get_node_by_ref($1, $2);
184
185 if (target)
186 merge_nodes(target, $3);
187 else
Rob Herring47605972015-04-29 16:00:05 -0500188 ERROR(&@2, "Label or path %s not found", $2);
John Bonesio658f29a2010-11-17 15:28:20 -0800189 $$ = $1;
David Gibsona4da2e32007-12-18 15:06:42 +1100190 }
Stephen Warrencd296722012-09-28 21:25:59 +0000191 | devicetree DT_DEL_NODE DT_REF ';'
192 {
193 struct node *target = get_node_by_ref($1, $3);
194
Rob Herring47605972015-04-29 16:00:05 -0500195 if (target)
Stephen Warrencd296722012-09-28 21:25:59 +0000196 delete_node(target);
Rob Herring47605972015-04-29 16:00:05 -0500197 else
198 ERROR(&@3, "Label or path %s not found", $3);
199
Stephen Warrencd296722012-09-28 21:25:59 +0000200
201 $$ = $1;
202 }
David Gibsona4da2e32007-12-18 15:06:42 +1100203 ;
204
205nodedef:
206 '{' proplist subnodes '}' ';'
207 {
208 $$ = build_node($2, $3);
209 }
210 ;
211
212proplist:
213 /* empty */
214 {
215 $$ = NULL;
216 }
217 | proplist propdef
218 {
219 $$ = chain_property($2, $1);
220 }
221 ;
222
223propdef:
John Bonesio658f29a2010-11-17 15:28:20 -0800224 DT_PROPNODENAME '=' propdata ';'
David Gibsona4da2e32007-12-18 15:06:42 +1100225 {
John Bonesio658f29a2010-11-17 15:28:20 -0800226 $$ = build_property($1, $3);
David Gibsona4da2e32007-12-18 15:06:42 +1100227 }
John Bonesio658f29a2010-11-17 15:28:20 -0800228 | DT_PROPNODENAME ';'
David Gibsona4da2e32007-12-18 15:06:42 +1100229 {
John Bonesio658f29a2010-11-17 15:28:20 -0800230 $$ = build_property($1, empty_data);
231 }
Stephen Warrencd296722012-09-28 21:25:59 +0000232 | DT_DEL_PROP DT_PROPNODENAME ';'
233 {
234 $$ = build_property_delete($2);
235 }
John Bonesio658f29a2010-11-17 15:28:20 -0800236 | DT_LABEL propdef
237 {
238 add_label(&$2->labels, $1);
239 $$ = $2;
David Gibsona4da2e32007-12-18 15:06:42 +1100240 }
241 ;
242
243propdata:
244 propdataprefix DT_STRING
245 {
246 $$ = data_merge($1, $2);
247 }
Stephen Warrencd296722012-09-28 21:25:59 +0000248 | propdataprefix arrayprefix '>'
David Gibsona4da2e32007-12-18 15:06:42 +1100249 {
Stephen Warrencd296722012-09-28 21:25:59 +0000250 $$ = data_merge($1, $2.data);
David Gibsona4da2e32007-12-18 15:06:42 +1100251 }
252 | propdataprefix '[' bytestring ']'
253 {
254 $$ = data_merge($1, $3);
255 }
256 | propdataprefix DT_REF
257 {
258 $$ = data_add_marker($1, REF_PATH, $2);
259 }
Stephen Warrencd296722012-09-28 21:25:59 +0000260 | propdataprefix DT_INCBIN '(' DT_STRING ',' integer_prim ',' integer_prim ')'
David Gibsoned95d742008-08-07 12:24:17 +1000261 {
John Bonesio658f29a2010-11-17 15:28:20 -0800262 FILE *f = srcfile_relative_open($4.val, NULL);
263 struct data d;
David Gibsoned95d742008-08-07 12:24:17 +1000264
265 if ($6 != 0)
John Bonesio658f29a2010-11-17 15:28:20 -0800266 if (fseek(f, $6, SEEK_SET) != 0)
Rob Herring47605972015-04-29 16:00:05 -0500267 die("Couldn't seek to offset %llu in \"%s\": %s",
268 (unsigned long long)$6, $4.val,
269 strerror(errno));
David Gibsoned95d742008-08-07 12:24:17 +1000270
John Bonesio658f29a2010-11-17 15:28:20 -0800271 d = data_copy_file(f, $8);
David Gibsoned95d742008-08-07 12:24:17 +1000272
273 $$ = data_merge($1, d);
John Bonesio658f29a2010-11-17 15:28:20 -0800274 fclose(f);
David Gibsoned95d742008-08-07 12:24:17 +1000275 }
276 | propdataprefix DT_INCBIN '(' DT_STRING ')'
277 {
John Bonesio658f29a2010-11-17 15:28:20 -0800278 FILE *f = srcfile_relative_open($4.val, NULL);
David Gibsoned95d742008-08-07 12:24:17 +1000279 struct data d = empty_data;
280
John Bonesio658f29a2010-11-17 15:28:20 -0800281 d = data_copy_file(f, -1);
David Gibsoned95d742008-08-07 12:24:17 +1000282
283 $$ = data_merge($1, d);
John Bonesio658f29a2010-11-17 15:28:20 -0800284 fclose(f);
David Gibsoned95d742008-08-07 12:24:17 +1000285 }
David Gibsona4da2e32007-12-18 15:06:42 +1100286 | propdata DT_LABEL
287 {
288 $$ = data_add_marker($1, LABEL, $2);
289 }
290 ;
291
292propdataprefix:
293 /* empty */
294 {
295 $$ = empty_data;
296 }
297 | propdata ','
298 {
299 $$ = $1;
300 }
301 | propdataprefix DT_LABEL
302 {
303 $$ = data_add_marker($1, LABEL, $2);
304 }
305 ;
306
Stephen Warrencd296722012-09-28 21:25:59 +0000307arrayprefix:
308 DT_BITS DT_LITERAL '<'
David Gibsona4da2e32007-12-18 15:06:42 +1100309 {
Rob Herring47605972015-04-29 16:00:05 -0500310 unsigned long long bits;
Stephen Warrencd296722012-09-28 21:25:59 +0000311
Rob Herring47605972015-04-29 16:00:05 -0500312 bits = $2;
313
314 if ((bits != 8) && (bits != 16) &&
315 (bits != 32) && (bits != 64)) {
316 ERROR(&@2, "Array elements must be"
317 " 8, 16, 32 or 64-bits");
318 bits = 32;
Stephen Warrencd296722012-09-28 21:25:59 +0000319 }
Rob Herring47605972015-04-29 16:00:05 -0500320
321 $$.data = empty_data;
322 $$.bits = bits;
David Gibsona4da2e32007-12-18 15:06:42 +1100323 }
Stephen Warrencd296722012-09-28 21:25:59 +0000324 | '<'
David Gibsona4da2e32007-12-18 15:06:42 +1100325 {
Stephen Warrencd296722012-09-28 21:25:59 +0000326 $$.data = empty_data;
327 $$.bits = 32;
David Gibsona4da2e32007-12-18 15:06:42 +1100328 }
Stephen Warrencd296722012-09-28 21:25:59 +0000329 | arrayprefix integer_prim
David Gibsona4da2e32007-12-18 15:06:42 +1100330 {
Stephen Warrencd296722012-09-28 21:25:59 +0000331 if ($1.bits < 64) {
332 uint64_t mask = (1ULL << $1.bits) - 1;
333 /*
334 * Bits above mask must either be all zero
335 * (positive within range of mask) or all one
336 * (negative and sign-extended). The second
337 * condition is true if when we set all bits
338 * within the mask to one (i.e. | in the
339 * mask), all bits are one.
340 */
341 if (($2 > mask) && (($2 | mask) != -1ULL))
Rob Herring47605972015-04-29 16:00:05 -0500342 ERROR(&@2, "Value out of range for"
343 " %d-bit array element", $1.bits);
Stephen Warrencd296722012-09-28 21:25:59 +0000344 }
345
346 $$.data = data_append_integer($1.data, $2, $1.bits);
David Gibsona4da2e32007-12-18 15:06:42 +1100347 }
Stephen Warrencd296722012-09-28 21:25:59 +0000348 | arrayprefix DT_REF
David Gibsona4da2e32007-12-18 15:06:42 +1100349 {
Stephen Warrencd296722012-09-28 21:25:59 +0000350 uint64_t val = ~0ULL >> (64 - $1.bits);
351
352 if ($1.bits == 32)
353 $1.data = data_add_marker($1.data,
354 REF_PHANDLE,
355 $2);
356 else
Rob Herring47605972015-04-29 16:00:05 -0500357 ERROR(&@2, "References are only allowed in "
Stephen Warrencd296722012-09-28 21:25:59 +0000358 "arrays with 32-bit elements.");
359
360 $$.data = data_append_integer($1.data, val, $1.bits);
361 }
362 | arrayprefix DT_LABEL
363 {
364 $$.data = data_add_marker($1.data, LABEL, $2);
David Gibsona4da2e32007-12-18 15:06:42 +1100365 }
366 ;
367
Stephen Warrencd296722012-09-28 21:25:59 +0000368integer_prim:
David Gibsona4da2e32007-12-18 15:06:42 +1100369 DT_LITERAL
Stephen Warrencd296722012-09-28 21:25:59 +0000370 | DT_CHAR_LITERAL
Stephen Warrencd296722012-09-28 21:25:59 +0000371 | '(' integer_expr ')'
372 {
373 $$ = $2;
374 }
375 ;
376
377integer_expr:
378 integer_trinary
379 ;
380
381integer_trinary:
382 integer_or
383 | integer_or '?' integer_expr ':' integer_trinary { $$ = $1 ? $3 : $5; }
384 ;
385
386integer_or:
387 integer_and
388 | integer_or DT_OR integer_and { $$ = $1 || $3; }
389 ;
390
391integer_and:
392 integer_bitor
393 | integer_and DT_AND integer_bitor { $$ = $1 && $3; }
394 ;
395
396integer_bitor:
397 integer_bitxor
398 | integer_bitor '|' integer_bitxor { $$ = $1 | $3; }
399 ;
400
401integer_bitxor:
402 integer_bitand
403 | integer_bitxor '^' integer_bitand { $$ = $1 ^ $3; }
404 ;
405
406integer_bitand:
407 integer_eq
408 | integer_bitand '&' integer_eq { $$ = $1 & $3; }
409 ;
410
411integer_eq:
412 integer_rela
413 | integer_eq DT_EQ integer_rela { $$ = $1 == $3; }
414 | integer_eq DT_NE integer_rela { $$ = $1 != $3; }
415 ;
416
417integer_rela:
418 integer_shift
419 | integer_rela '<' integer_shift { $$ = $1 < $3; }
420 | integer_rela '>' integer_shift { $$ = $1 > $3; }
421 | integer_rela DT_LE integer_shift { $$ = $1 <= $3; }
422 | integer_rela DT_GE integer_shift { $$ = $1 >= $3; }
423 ;
424
425integer_shift:
426 integer_shift DT_LSHIFT integer_add { $$ = $1 << $3; }
427 | integer_shift DT_RSHIFT integer_add { $$ = $1 >> $3; }
428 | integer_add
429 ;
430
431integer_add:
432 integer_add '+' integer_mul { $$ = $1 + $3; }
433 | integer_add '-' integer_mul { $$ = $1 - $3; }
434 | integer_mul
435 ;
436
437integer_mul:
438 integer_mul '*' integer_unary { $$ = $1 * $3; }
Rob Herring91feabc2016-01-26 09:04:11 -0600439 | integer_mul '/' integer_unary
440 {
441 if ($3 != 0) {
442 $$ = $1 / $3;
443 } else {
444 ERROR(&@$, "Division by zero");
445 $$ = 0;
446 }
447 }
448 | integer_mul '%' integer_unary
449 {
450 if ($3 != 0) {
451 $$ = $1 % $3;
452 } else {
453 ERROR(&@$, "Division by zero");
454 $$ = 0;
455 }
456 }
Stephen Warrencd296722012-09-28 21:25:59 +0000457 | integer_unary
458 ;
459
460integer_unary:
461 integer_prim
462 | '-' integer_unary { $$ = -$2; }
463 | '~' integer_unary { $$ = ~$2; }
464 | '!' integer_unary { $$ = !$2; }
David Gibsona4da2e32007-12-18 15:06:42 +1100465 ;
466
467bytestring:
468 /* empty */
469 {
470 $$ = empty_data;
471 }
472 | bytestring DT_BYTE
473 {
474 $$ = data_append_byte($1, $2);
475 }
476 | bytestring DT_LABEL
477 {
478 $$ = data_add_marker($1, LABEL, $2);
479 }
480 ;
481
482subnodes:
483 /* empty */
484 {
485 $$ = NULL;
486 }
John Bonesio658f29a2010-11-17 15:28:20 -0800487 | subnode subnodes
David Gibsona4da2e32007-12-18 15:06:42 +1100488 {
489 $$ = chain_node($1, $2);
490 }
491 | subnode propdef
492 {
Rob Herring47605972015-04-29 16:00:05 -0500493 ERROR(&@2, "Properties must precede subnodes");
David Gibsona4da2e32007-12-18 15:06:42 +1100494 YYERROR;
495 }
496 ;
497
498subnode:
John Bonesio658f29a2010-11-17 15:28:20 -0800499 DT_PROPNODENAME nodedef
David Gibsona4da2e32007-12-18 15:06:42 +1100500 {
John Bonesio658f29a2010-11-17 15:28:20 -0800501 $$ = name_node($2, $1);
David Gibsona4da2e32007-12-18 15:06:42 +1100502 }
Stephen Warrencd296722012-09-28 21:25:59 +0000503 | DT_DEL_NODE DT_PROPNODENAME ';'
504 {
505 $$ = name_node(build_node_delete(), $2);
506 }
John Bonesio658f29a2010-11-17 15:28:20 -0800507 | DT_LABEL subnode
David Gibsona4da2e32007-12-18 15:06:42 +1100508 {
John Bonesio658f29a2010-11-17 15:28:20 -0800509 add_label(&$2->labels, $1);
510 $$ = $2;
David Gibsona4da2e32007-12-18 15:06:42 +1100511 }
512 ;
513
514%%
515
Rob Herring47605972015-04-29 16:00:05 -0500516void yyerror(char const *s)
David Gibsona4da2e32007-12-18 15:06:42 +1100517{
Rob Herring47605972015-04-29 16:00:05 -0500518 ERROR(&yylloc, "%s", s);
Stephen Warrencd296722012-09-28 21:25:59 +0000519}