blob: 5a897e36562d67107dba44fa91274253b34152dc [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>
22
David Gibsona4da2e32007-12-18 15:06:42 +110023#include "dtc.h"
24#include "srcpos.h"
25
David Gibsoned95d742008-08-07 12:24:17 +100026extern int yylex(void);
John Bonesio658f29a2010-11-17 15:28:20 -080027extern void yyerror(char const *s);
Rob Herring47605972015-04-29 16:00:05 -050028#define ERROR(loc, ...) \
29 do { \
30 srcpos_error((loc), "Error", __VA_ARGS__); \
31 treesource_error = true; \
32 } while (0)
David Gibsona4da2e32007-12-18 15:06:42 +110033
34extern struct boot_info *the_boot_info;
Rob Herring47605972015-04-29 16:00:05 -050035extern bool treesource_error;
David Gibsona4da2e32007-12-18 15:06:42 +110036%}
37
38%union {
39 char *propnodename;
David Gibsona4da2e32007-12-18 15:06:42 +110040 char *labelref;
David Gibsoned95d742008-08-07 12:24:17 +100041 uint8_t byte;
David Gibsona4da2e32007-12-18 15:06:42 +110042 struct data data;
43
Stephen Warrencd296722012-09-28 21:25:59 +000044 struct {
45 struct data data;
46 int bits;
47 } array;
48
David Gibsona4da2e32007-12-18 15:06:42 +110049 struct property *prop;
50 struct property *proplist;
51 struct node *node;
52 struct node *nodelist;
53 struct reserve_info *re;
Stephen Warrencd296722012-09-28 21:25:59 +000054 uint64_t integer;
David Gibsona4da2e32007-12-18 15:06:42 +110055}
56
57%token DT_V1
58%token DT_MEMRESERVE
Stephen Warrencd296722012-09-28 21:25:59 +000059%token DT_LSHIFT DT_RSHIFT DT_LE DT_GE DT_EQ DT_NE DT_AND DT_OR
60%token DT_BITS
61%token DT_DEL_PROP
62%token DT_DEL_NODE
David Gibsona4da2e32007-12-18 15:06:42 +110063%token <propnodename> DT_PROPNODENAME
Rob Herring47605972015-04-29 16:00:05 -050064%token <integer> DT_LITERAL
65%token <integer> DT_CHAR_LITERAL
David Gibsona4da2e32007-12-18 15:06:42 +110066%token <byte> DT_BYTE
67%token <data> DT_STRING
68%token <labelref> DT_LABEL
69%token <labelref> DT_REF
David Gibsoned95d742008-08-07 12:24:17 +100070%token DT_INCBIN
David Gibsona4da2e32007-12-18 15:06:42 +110071
72%type <data> propdata
73%type <data> propdataprefix
74%type <re> memreserve
75%type <re> memreserves
Stephen Warrencd296722012-09-28 21:25:59 +000076%type <array> arrayprefix
David Gibsona4da2e32007-12-18 15:06:42 +110077%type <data> bytestring
78%type <prop> propdef
79%type <proplist> proplist
80
81%type <node> devicetree
82%type <node> nodedef
83%type <node> subnode
84%type <nodelist> subnodes
David Gibsona4da2e32007-12-18 15:06:42 +110085
Stephen Warrencd296722012-09-28 21:25:59 +000086%type <integer> integer_prim
87%type <integer> integer_unary
88%type <integer> integer_mul
89%type <integer> integer_add
90%type <integer> integer_shift
91%type <integer> integer_rela
92%type <integer> integer_eq
93%type <integer> integer_bitand
94%type <integer> integer_bitxor
95%type <integer> integer_bitor
96%type <integer> integer_and
97%type <integer> integer_or
98%type <integer> integer_trinary
99%type <integer> integer_expr
100
David Gibsona4da2e32007-12-18 15:06:42 +1100101%%
102
103sourcefile:
104 DT_V1 ';' memreserves devicetree
105 {
John Bonesio658f29a2010-11-17 15:28:20 -0800106 the_boot_info = build_boot_info($3, $4,
107 guess_boot_cpuid($4));
David Gibsona4da2e32007-12-18 15:06:42 +1100108 }
109 ;
110
111memreserves:
112 /* empty */
113 {
114 $$ = NULL;
115 }
116 | memreserve memreserves
117 {
118 $$ = chain_reserve_entry($1, $2);
119 }
120 ;
121
122memreserve:
Stephen Warrencd296722012-09-28 21:25:59 +0000123 DT_MEMRESERVE integer_prim integer_prim ';'
David Gibsona4da2e32007-12-18 15:06:42 +1100124 {
John Bonesio658f29a2010-11-17 15:28:20 -0800125 $$ = build_reserve_entry($2, $3);
David Gibsona4da2e32007-12-18 15:06:42 +1100126 }
John Bonesio658f29a2010-11-17 15:28:20 -0800127 | DT_LABEL memreserve
David Gibsona4da2e32007-12-18 15:06:42 +1100128 {
John Bonesio658f29a2010-11-17 15:28:20 -0800129 add_label(&$2->labels, $1);
130 $$ = $2;
David Gibsona4da2e32007-12-18 15:06:42 +1100131 }
132 ;
133
David Gibsona4da2e32007-12-18 15:06:42 +1100134devicetree:
135 '/' nodedef
136 {
John Bonesio658f29a2010-11-17 15:28:20 -0800137 $$ = name_node($2, "");
138 }
139 | devicetree '/' nodedef
140 {
141 $$ = merge_nodes($1, $3);
142 }
Rob Herring47605972015-04-29 16:00:05 -0500143
144 | devicetree DT_LABEL DT_REF nodedef
145 {
146 struct node *target = get_node_by_ref($1, $3);
147
148 add_label(&target->labels, $2);
149 if (target)
150 merge_nodes(target, $4);
151 else
152 ERROR(&@3, "Label or path %s not found", $3);
153 $$ = $1;
154 }
John Bonesio658f29a2010-11-17 15:28:20 -0800155 | devicetree DT_REF nodedef
156 {
157 struct node *target = get_node_by_ref($1, $2);
158
159 if (target)
160 merge_nodes(target, $3);
161 else
Rob Herring47605972015-04-29 16:00:05 -0500162 ERROR(&@2, "Label or path %s not found", $2);
John Bonesio658f29a2010-11-17 15:28:20 -0800163 $$ = $1;
David Gibsona4da2e32007-12-18 15:06:42 +1100164 }
Stephen Warrencd296722012-09-28 21:25:59 +0000165 | devicetree DT_DEL_NODE DT_REF ';'
166 {
167 struct node *target = get_node_by_ref($1, $3);
168
Rob Herring47605972015-04-29 16:00:05 -0500169 if (target)
Stephen Warrencd296722012-09-28 21:25:59 +0000170 delete_node(target);
Rob Herring47605972015-04-29 16:00:05 -0500171 else
172 ERROR(&@3, "Label or path %s not found", $3);
173
Stephen Warrencd296722012-09-28 21:25:59 +0000174
175 $$ = $1;
176 }
David Gibsona4da2e32007-12-18 15:06:42 +1100177 ;
178
179nodedef:
180 '{' proplist subnodes '}' ';'
181 {
182 $$ = build_node($2, $3);
183 }
184 ;
185
186proplist:
187 /* empty */
188 {
189 $$ = NULL;
190 }
191 | proplist propdef
192 {
193 $$ = chain_property($2, $1);
194 }
195 ;
196
197propdef:
John Bonesio658f29a2010-11-17 15:28:20 -0800198 DT_PROPNODENAME '=' propdata ';'
David Gibsona4da2e32007-12-18 15:06:42 +1100199 {
John Bonesio658f29a2010-11-17 15:28:20 -0800200 $$ = build_property($1, $3);
David Gibsona4da2e32007-12-18 15:06:42 +1100201 }
John Bonesio658f29a2010-11-17 15:28:20 -0800202 | DT_PROPNODENAME ';'
David Gibsona4da2e32007-12-18 15:06:42 +1100203 {
John Bonesio658f29a2010-11-17 15:28:20 -0800204 $$ = build_property($1, empty_data);
205 }
Stephen Warrencd296722012-09-28 21:25:59 +0000206 | DT_DEL_PROP DT_PROPNODENAME ';'
207 {
208 $$ = build_property_delete($2);
209 }
John Bonesio658f29a2010-11-17 15:28:20 -0800210 | DT_LABEL propdef
211 {
212 add_label(&$2->labels, $1);
213 $$ = $2;
David Gibsona4da2e32007-12-18 15:06:42 +1100214 }
215 ;
216
217propdata:
218 propdataprefix DT_STRING
219 {
220 $$ = data_merge($1, $2);
221 }
Stephen Warrencd296722012-09-28 21:25:59 +0000222 | propdataprefix arrayprefix '>'
David Gibsona4da2e32007-12-18 15:06:42 +1100223 {
Stephen Warrencd296722012-09-28 21:25:59 +0000224 $$ = data_merge($1, $2.data);
David Gibsona4da2e32007-12-18 15:06:42 +1100225 }
226 | propdataprefix '[' bytestring ']'
227 {
228 $$ = data_merge($1, $3);
229 }
230 | propdataprefix DT_REF
231 {
232 $$ = data_add_marker($1, REF_PATH, $2);
233 }
Stephen Warrencd296722012-09-28 21:25:59 +0000234 | propdataprefix DT_INCBIN '(' DT_STRING ',' integer_prim ',' integer_prim ')'
David Gibsoned95d742008-08-07 12:24:17 +1000235 {
John Bonesio658f29a2010-11-17 15:28:20 -0800236 FILE *f = srcfile_relative_open($4.val, NULL);
237 struct data d;
David Gibsoned95d742008-08-07 12:24:17 +1000238
239 if ($6 != 0)
John Bonesio658f29a2010-11-17 15:28:20 -0800240 if (fseek(f, $6, SEEK_SET) != 0)
Rob Herring47605972015-04-29 16:00:05 -0500241 die("Couldn't seek to offset %llu in \"%s\": %s",
242 (unsigned long long)$6, $4.val,
243 strerror(errno));
David Gibsoned95d742008-08-07 12:24:17 +1000244
John Bonesio658f29a2010-11-17 15:28:20 -0800245 d = data_copy_file(f, $8);
David Gibsoned95d742008-08-07 12:24:17 +1000246
247 $$ = data_merge($1, d);
John Bonesio658f29a2010-11-17 15:28:20 -0800248 fclose(f);
David Gibsoned95d742008-08-07 12:24:17 +1000249 }
250 | propdataprefix DT_INCBIN '(' DT_STRING ')'
251 {
John Bonesio658f29a2010-11-17 15:28:20 -0800252 FILE *f = srcfile_relative_open($4.val, NULL);
David Gibsoned95d742008-08-07 12:24:17 +1000253 struct data d = empty_data;
254
John Bonesio658f29a2010-11-17 15:28:20 -0800255 d = data_copy_file(f, -1);
David Gibsoned95d742008-08-07 12:24:17 +1000256
257 $$ = data_merge($1, d);
John Bonesio658f29a2010-11-17 15:28:20 -0800258 fclose(f);
David Gibsoned95d742008-08-07 12:24:17 +1000259 }
David Gibsona4da2e32007-12-18 15:06:42 +1100260 | propdata DT_LABEL
261 {
262 $$ = data_add_marker($1, LABEL, $2);
263 }
264 ;
265
266propdataprefix:
267 /* empty */
268 {
269 $$ = empty_data;
270 }
271 | propdata ','
272 {
273 $$ = $1;
274 }
275 | propdataprefix DT_LABEL
276 {
277 $$ = data_add_marker($1, LABEL, $2);
278 }
279 ;
280
Stephen Warrencd296722012-09-28 21:25:59 +0000281arrayprefix:
282 DT_BITS DT_LITERAL '<'
David Gibsona4da2e32007-12-18 15:06:42 +1100283 {
Rob Herring47605972015-04-29 16:00:05 -0500284 unsigned long long bits;
Stephen Warrencd296722012-09-28 21:25:59 +0000285
Rob Herring47605972015-04-29 16:00:05 -0500286 bits = $2;
287
288 if ((bits != 8) && (bits != 16) &&
289 (bits != 32) && (bits != 64)) {
290 ERROR(&@2, "Array elements must be"
291 " 8, 16, 32 or 64-bits");
292 bits = 32;
Stephen Warrencd296722012-09-28 21:25:59 +0000293 }
Rob Herring47605972015-04-29 16:00:05 -0500294
295 $$.data = empty_data;
296 $$.bits = bits;
David Gibsona4da2e32007-12-18 15:06:42 +1100297 }
Stephen Warrencd296722012-09-28 21:25:59 +0000298 | '<'
David Gibsona4da2e32007-12-18 15:06:42 +1100299 {
Stephen Warrencd296722012-09-28 21:25:59 +0000300 $$.data = empty_data;
301 $$.bits = 32;
David Gibsona4da2e32007-12-18 15:06:42 +1100302 }
Stephen Warrencd296722012-09-28 21:25:59 +0000303 | arrayprefix integer_prim
David Gibsona4da2e32007-12-18 15:06:42 +1100304 {
Stephen Warrencd296722012-09-28 21:25:59 +0000305 if ($1.bits < 64) {
306 uint64_t mask = (1ULL << $1.bits) - 1;
307 /*
308 * Bits above mask must either be all zero
309 * (positive within range of mask) or all one
310 * (negative and sign-extended). The second
311 * condition is true if when we set all bits
312 * within the mask to one (i.e. | in the
313 * mask), all bits are one.
314 */
315 if (($2 > mask) && (($2 | mask) != -1ULL))
Rob Herring47605972015-04-29 16:00:05 -0500316 ERROR(&@2, "Value out of range for"
317 " %d-bit array element", $1.bits);
Stephen Warrencd296722012-09-28 21:25:59 +0000318 }
319
320 $$.data = data_append_integer($1.data, $2, $1.bits);
David Gibsona4da2e32007-12-18 15:06:42 +1100321 }
Stephen Warrencd296722012-09-28 21:25:59 +0000322 | arrayprefix DT_REF
David Gibsona4da2e32007-12-18 15:06:42 +1100323 {
Stephen Warrencd296722012-09-28 21:25:59 +0000324 uint64_t val = ~0ULL >> (64 - $1.bits);
325
326 if ($1.bits == 32)
327 $1.data = data_add_marker($1.data,
328 REF_PHANDLE,
329 $2);
330 else
Rob Herring47605972015-04-29 16:00:05 -0500331 ERROR(&@2, "References are only allowed in "
Stephen Warrencd296722012-09-28 21:25:59 +0000332 "arrays with 32-bit elements.");
333
334 $$.data = data_append_integer($1.data, val, $1.bits);
335 }
336 | arrayprefix DT_LABEL
337 {
338 $$.data = data_add_marker($1.data, LABEL, $2);
David Gibsona4da2e32007-12-18 15:06:42 +1100339 }
340 ;
341
Stephen Warrencd296722012-09-28 21:25:59 +0000342integer_prim:
David Gibsona4da2e32007-12-18 15:06:42 +1100343 DT_LITERAL
Stephen Warrencd296722012-09-28 21:25:59 +0000344 | DT_CHAR_LITERAL
Stephen Warrencd296722012-09-28 21:25:59 +0000345 | '(' integer_expr ')'
346 {
347 $$ = $2;
348 }
349 ;
350
351integer_expr:
352 integer_trinary
353 ;
354
355integer_trinary:
356 integer_or
357 | integer_or '?' integer_expr ':' integer_trinary { $$ = $1 ? $3 : $5; }
358 ;
359
360integer_or:
361 integer_and
362 | integer_or DT_OR integer_and { $$ = $1 || $3; }
363 ;
364
365integer_and:
366 integer_bitor
367 | integer_and DT_AND integer_bitor { $$ = $1 && $3; }
368 ;
369
370integer_bitor:
371 integer_bitxor
372 | integer_bitor '|' integer_bitxor { $$ = $1 | $3; }
373 ;
374
375integer_bitxor:
376 integer_bitand
377 | integer_bitxor '^' integer_bitand { $$ = $1 ^ $3; }
378 ;
379
380integer_bitand:
381 integer_eq
382 | integer_bitand '&' integer_eq { $$ = $1 & $3; }
383 ;
384
385integer_eq:
386 integer_rela
387 | integer_eq DT_EQ integer_rela { $$ = $1 == $3; }
388 | integer_eq DT_NE integer_rela { $$ = $1 != $3; }
389 ;
390
391integer_rela:
392 integer_shift
393 | integer_rela '<' integer_shift { $$ = $1 < $3; }
394 | integer_rela '>' integer_shift { $$ = $1 > $3; }
395 | integer_rela DT_LE integer_shift { $$ = $1 <= $3; }
396 | integer_rela DT_GE integer_shift { $$ = $1 >= $3; }
397 ;
398
399integer_shift:
400 integer_shift DT_LSHIFT integer_add { $$ = $1 << $3; }
401 | integer_shift DT_RSHIFT integer_add { $$ = $1 >> $3; }
402 | integer_add
403 ;
404
405integer_add:
406 integer_add '+' integer_mul { $$ = $1 + $3; }
407 | integer_add '-' integer_mul { $$ = $1 - $3; }
408 | integer_mul
409 ;
410
411integer_mul:
412 integer_mul '*' integer_unary { $$ = $1 * $3; }
413 | integer_mul '/' integer_unary { $$ = $1 / $3; }
414 | integer_mul '%' integer_unary { $$ = $1 % $3; }
415 | integer_unary
416 ;
417
418integer_unary:
419 integer_prim
420 | '-' integer_unary { $$ = -$2; }
421 | '~' integer_unary { $$ = ~$2; }
422 | '!' integer_unary { $$ = !$2; }
David Gibsona4da2e32007-12-18 15:06:42 +1100423 ;
424
425bytestring:
426 /* empty */
427 {
428 $$ = empty_data;
429 }
430 | bytestring DT_BYTE
431 {
432 $$ = data_append_byte($1, $2);
433 }
434 | bytestring DT_LABEL
435 {
436 $$ = data_add_marker($1, LABEL, $2);
437 }
438 ;
439
440subnodes:
441 /* empty */
442 {
443 $$ = NULL;
444 }
John Bonesio658f29a2010-11-17 15:28:20 -0800445 | subnode subnodes
David Gibsona4da2e32007-12-18 15:06:42 +1100446 {
447 $$ = chain_node($1, $2);
448 }
449 | subnode propdef
450 {
Rob Herring47605972015-04-29 16:00:05 -0500451 ERROR(&@2, "Properties must precede subnodes");
David Gibsona4da2e32007-12-18 15:06:42 +1100452 YYERROR;
453 }
454 ;
455
456subnode:
John Bonesio658f29a2010-11-17 15:28:20 -0800457 DT_PROPNODENAME nodedef
David Gibsona4da2e32007-12-18 15:06:42 +1100458 {
John Bonesio658f29a2010-11-17 15:28:20 -0800459 $$ = name_node($2, $1);
David Gibsona4da2e32007-12-18 15:06:42 +1100460 }
Stephen Warrencd296722012-09-28 21:25:59 +0000461 | DT_DEL_NODE DT_PROPNODENAME ';'
462 {
463 $$ = name_node(build_node_delete(), $2);
464 }
John Bonesio658f29a2010-11-17 15:28:20 -0800465 | DT_LABEL subnode
David Gibsona4da2e32007-12-18 15:06:42 +1100466 {
John Bonesio658f29a2010-11-17 15:28:20 -0800467 add_label(&$2->labels, $1);
468 $$ = $2;
David Gibsona4da2e32007-12-18 15:06:42 +1100469 }
470 ;
471
472%%
473
Rob Herring47605972015-04-29 16:00:05 -0500474void yyerror(char const *s)
David Gibsona4da2e32007-12-18 15:06:42 +1100475{
Rob Herring47605972015-04-29 16:00:05 -0500476 ERROR(&yylloc, "%s", s);
Stephen Warrencd296722012-09-28 21:25:59 +0000477}