blob: 61ed250493774bc11c5868f01ead6330536aeaae [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
David Gibson63dc9c72007-09-18 11:44:04 +10004 *
David Gibsonfc14dad2005-06-08 17:18:34 +10005 * 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.
David Gibson63dc9c72007-09-18 11:44:04 +100014 *
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
David Gibsonfc14dad2005-06-08 17:18:34 +100019 */
20
David Gibson86dbcbd2005-10-19 16:00:31 +100021%locations
22
David Gibsonfc14dad2005-06-08 17:18:34 +100023%{
24#include "dtc.h"
Jon Loeligere45e6fd2007-03-23 15:18:41 -050025#include "srcpos.h"
David Gibsonfc14dad2005-06-08 17:18:34 +100026
Jon Loeliger39488492007-02-16 09:33:54 -060027int yylex(void);
Jon Loeliger39488492007-02-16 09:33:54 -060028cell_t cell_from_string(char *s, unsigned int base);
David Gibsonfc14dad2005-06-08 17:18:34 +100029
David Gibsonf0517db2005-07-15 17:14:24 +100030extern struct boot_info *the_boot_info;
David Gibsonfc14dad2005-06-08 17:18:34 +100031
32%}
33
34%union {
35 cell_t cval;
Jon Loeligeraf0278a2007-02-15 10:59:27 -060036 unsigned int cbase;
David Gibson03a9b9d2005-07-11 16:49:52 +100037 u8 byte;
David Gibsonfc14dad2005-06-08 17:18:34 +100038 char *str;
39 struct data data;
40 struct property *prop;
41 struct property *proplist;
42 struct node *node;
43 struct node *nodelist;
44 int datalen;
45 int hexlen;
David Gibsonf0517db2005-07-15 17:14:24 +100046 u64 addr;
David Gibsonf040d952005-10-24 18:18:38 +100047 struct reserve_info *re;
David Gibsonfc14dad2005-06-08 17:18:34 +100048}
49
David Gibsonf0517db2005-07-15 17:14:24 +100050%token DT_MEMRESERVE
51%token <addr> DT_ADDR
David Gibsonfc14dad2005-06-08 17:18:34 +100052%token <str> DT_PROPNAME
53%token <str> DT_NODENAME
Jon Loeligeraf0278a2007-02-15 10:59:27 -060054%token <cbase> DT_BASE
55%token <str> DT_CELL
David Gibsonfc14dad2005-06-08 17:18:34 +100056%token <byte> DT_BYTE
57%token <data> DT_STRING
David Gibson4102d842005-06-16 14:36:37 +100058%token <str> DT_LABEL
David Gibson81f2e892005-06-16 17:04:00 +100059%token <str> DT_REF
David Gibsonfc14dad2005-06-08 17:18:34 +100060
61%type <data> propdata
David Gibson32da4752007-02-07 16:37:50 +110062%type <data> propdataprefix
David Gibsonf0517db2005-07-15 17:14:24 +100063%type <re> memreserve
David Gibsonf040d952005-10-24 18:18:38 +100064%type <re> memreserves
Jon Loeligeraf0278a2007-02-15 10:59:27 -060065%type <cbase> opt_cell_base
David Gibsonfc14dad2005-06-08 17:18:34 +100066%type <data> celllist
67%type <data> bytestring
68%type <prop> propdef
69%type <proplist> proplist
70
David Gibsonf0517db2005-07-15 17:14:24 +100071%type <node> devicetree
David Gibsonfc14dad2005-06-08 17:18:34 +100072%type <node> nodedef
73%type <node> subnode
74%type <nodelist> subnodes
David Gibson4102d842005-06-16 14:36:37 +100075%type <str> label
76%type <str> nodename
77
David Gibsonfc14dad2005-06-08 17:18:34 +100078%%
79
Jon Loeliger30807ca2007-10-18 09:42:16 -050080sourcefile:
81 memreserves devicetree
82 {
David Gibsonf0517db2005-07-15 17:14:24 +100083 the_boot_info = build_boot_info($1, $2);
84 }
85 ;
86
Jon Loeliger30807ca2007-10-18 09:42:16 -050087memreserves:
Jon Loeliger56412892007-10-23 09:28:54 -050088 /* empty */
Jon Loeliger30807ca2007-10-18 09:42:16 -050089 {
David Gibsonf040d952005-10-24 18:18:38 +100090 $$ = NULL;
David Gibsonf0517db2005-07-15 17:14:24 +100091 }
Jon Loeliger56412892007-10-23 09:28:54 -050092 | memreserve memreserves
93 {
94 $$ = chain_reserve_entry($1, $2);
95 }
David Gibsonf0517db2005-07-15 17:14:24 +100096 ;
97
Jon Loeliger30807ca2007-10-18 09:42:16 -050098memreserve:
99 label DT_MEMRESERVE DT_ADDR DT_ADDR ';'
100 {
Milton Millerd4290332007-07-07 01:18:49 -0500101 $$ = build_reserve_entry($3, $4, $1);
David Gibsonf0517db2005-07-15 17:14:24 +1000102 }
Jon Loeliger30807ca2007-10-18 09:42:16 -0500103 | label DT_MEMRESERVE DT_ADDR '-' DT_ADDR ';'
104 {
Milton Millerd4290332007-07-07 01:18:49 -0500105 $$ = build_reserve_entry($3, $5 - $3 + 1, $1);
David Gibsonf0517db2005-07-15 17:14:24 +1000106 }
107 ;
108
Jon Loeliger30807ca2007-10-18 09:42:16 -0500109devicetree:
110 '/' nodedef
111 {
David Gibsonf0517db2005-07-15 17:14:24 +1000112 $$ = name_node($2, "", NULL);
113 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000114 ;
115
Jon Loeliger30807ca2007-10-18 09:42:16 -0500116nodedef:
117 '{' proplist subnodes '}' ';'
118 {
David Gibsonfc14dad2005-06-08 17:18:34 +1000119 $$ = build_node($2, $3);
120 }
121 ;
122
Jon Loeliger30807ca2007-10-18 09:42:16 -0500123proplist:
Jon Loeliger56412892007-10-23 09:28:54 -0500124 /* empty */
Jon Loeliger30807ca2007-10-18 09:42:16 -0500125 {
David Gibsonfc14dad2005-06-08 17:18:34 +1000126 $$ = NULL;
127 }
Jon Loeliger7b3fb782007-10-22 16:09:56 -0500128 | proplist propdef
Jon Loeliger56412892007-10-23 09:28:54 -0500129 {
Jon Loeliger7b3fb782007-10-22 16:09:56 -0500130 $$ = chain_property($2, $1);
Jon Loeliger56412892007-10-23 09:28:54 -0500131 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000132 ;
133
Jon Loeliger30807ca2007-10-18 09:42:16 -0500134propdef:
135 label DT_PROPNAME '=' propdata ';'
136 {
David Gibson4102d842005-06-16 14:36:37 +1000137 $$ = build_property($2, $4, $1);
David Gibsonfc14dad2005-06-08 17:18:34 +1000138 }
Jon Loeliger30807ca2007-10-18 09:42:16 -0500139 | label DT_PROPNAME ';'
140 {
David Gibson9ad45872005-06-17 14:32:32 +1000141 $$ = build_property($2, empty_data, $1);
David Gibsonfc14dad2005-06-08 17:18:34 +1000142 }
143 ;
144
Jon Loeliger30807ca2007-10-18 09:42:16 -0500145propdata:
146 propdataprefix DT_STRING
147 {
148 $$ = data_merge($1, $2);
David Gibson32da4752007-02-07 16:37:50 +1100149 }
Jon Loeliger30807ca2007-10-18 09:42:16 -0500150 | propdataprefix '<' celllist '>'
151 {
152 $$ = data_merge(data_append_align($1,
153 sizeof(cell_t)), $3);
154 }
155 | propdataprefix '[' bytestring ']'
156 {
157 $$ = data_merge($1, $3);
158 }
159 | propdata DT_LABEL
160 {
161 $$ = data_add_label($1, $2);
162 }
David Gibson32da4752007-02-07 16:37:50 +1100163 ;
164
Jon Loeliger30807ca2007-10-18 09:42:16 -0500165propdataprefix:
Jon Loeliger56412892007-10-23 09:28:54 -0500166 /* empty */
167 {
168 $$ = empty_data;
169 }
170 | propdata ','
Jon Loeliger30807ca2007-10-18 09:42:16 -0500171 {
172 $$ = $1;
173 }
174 | propdataprefix DT_LABEL
175 {
176 $$ = data_add_label($1, $2);
177 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000178 ;
179
Jon Loeligeraf0278a2007-02-15 10:59:27 -0600180opt_cell_base:
181 /* empty */
Jon Loeliger30807ca2007-10-18 09:42:16 -0500182 {
183 $$ = 16;
184 }
Jon Loeligeraf0278a2007-02-15 10:59:27 -0600185 | DT_BASE
186 ;
187
Jon Loeliger30807ca2007-10-18 09:42:16 -0500188celllist:
Jon Loeliger56412892007-10-23 09:28:54 -0500189 /* empty */
190 {
191 $$ = empty_data;
192 }
193 | celllist opt_cell_base DT_CELL
Jon Loeliger30807ca2007-10-18 09:42:16 -0500194 {
Jon Loeligeraf0278a2007-02-15 10:59:27 -0600195 $$ = data_append_cell($1,
Jon Loeliger39488492007-02-16 09:33:54 -0600196 cell_from_string($3, $2));
Jon Loeligeraf0278a2007-02-15 10:59:27 -0600197 }
Jon Loeliger30807ca2007-10-18 09:42:16 -0500198 | celllist DT_REF
199 {
David Gibson81f2e892005-06-16 17:04:00 +1000200 $$ = data_append_cell(data_add_fixup($1, $2), -1);
201 }
Jon Loeliger30807ca2007-10-18 09:42:16 -0500202 | celllist DT_LABEL
203 {
204 $$ = data_add_label($1, $2);
205 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000206 ;
207
Jon Loeliger30807ca2007-10-18 09:42:16 -0500208bytestring:
Jon Loeliger56412892007-10-23 09:28:54 -0500209 /* empty */
210 {
211 $$ = empty_data;
212 }
213 | bytestring DT_BYTE
Jon Loeliger30807ca2007-10-18 09:42:16 -0500214 {
215 $$ = data_append_byte($1, $2);
216 }
217 | bytestring DT_LABEL
218 {
219 $$ = data_add_label($1, $2);
220 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000221 ;
222
Jon Loeliger30807ca2007-10-18 09:42:16 -0500223subnodes:
Jon Loeliger56412892007-10-23 09:28:54 -0500224 /* empty */
Jon Loeliger30807ca2007-10-18 09:42:16 -0500225 {
226 $$ = NULL;
227 }
Jon Loeliger56412892007-10-23 09:28:54 -0500228 | subnode subnodes
229 {
230 $$ = chain_node($1, $2);
231 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000232 ;
233
Jon Loeliger30807ca2007-10-18 09:42:16 -0500234subnode:
235 label nodename nodedef
236 {
237 $$ = name_node($3, $2, $1);
238 }
David Gibson4102d842005-06-16 14:36:37 +1000239 ;
240
Jon Loeliger30807ca2007-10-18 09:42:16 -0500241nodename:
242 DT_NODENAME
243 {
244 $$ = $1;
245 }
246 | DT_PROPNAME
247 {
248 $$ = $1;
249 }
David Gibson4102d842005-06-16 14:36:37 +1000250 ;
251
Jon Loeliger30807ca2007-10-18 09:42:16 -0500252label:
Jon Loeliger56412892007-10-23 09:28:54 -0500253 /* empty */
Jon Loeliger30807ca2007-10-18 09:42:16 -0500254 {
255 $$ = NULL;
256 }
Jon Loeliger56412892007-10-23 09:28:54 -0500257 | DT_LABEL
258 {
259 $$ = $1;
260 }
David Gibsonfc14dad2005-06-08 17:18:34 +1000261 ;
262
263%%
264
265void yyerror (char const *s)
266{
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500267 const char *fname = srcpos_filename_for_num(yylloc.filenum);
268
269 if (strcmp(fname, "-") == 0)
270 fname = "stdin";
271
272 fprintf(stderr, "%s:%d %s\n",
273 fname, yylloc.first_line, s);
David Gibsonfc14dad2005-06-08 17:18:34 +1000274}
Jon Loeliger39488492007-02-16 09:33:54 -0600275
276
277/*
278 * Convert a string representation of a numeric cell
279 * in the given base into a cell.
280 *
Milton Miller85ab5cc2007-07-07 01:18:48 -0500281 * FIXME: should these specification errors be fatal instead?
Jon Loeliger39488492007-02-16 09:33:54 -0600282 */
283
284cell_t cell_from_string(char *s, unsigned int base)
285{
286 cell_t c;
Milton Miller85ab5cc2007-07-07 01:18:48 -0500287 char *e;
Jon Loeliger39488492007-02-16 09:33:54 -0600288
Milton Miller85ab5cc2007-07-07 01:18:48 -0500289 c = strtoul(s, &e, base);
290 if (*e) {
291 fprintf(stderr,
292 "Line %d: Invalid cell value '%s' : "
293 "%c is not a base %d digit; %d assumed\n",
294 yylloc.first_line, s, *e, base, c);
295 }
296
Jon Loeliger39488492007-02-16 09:33:54 -0600297 if (errno == EINVAL || errno == ERANGE) {
298 fprintf(stderr,
299 "Line %d: Invalid cell value '%s'; %d assumed\n",
300 yylloc.first_line, s, c);
Milton Miller85ab5cc2007-07-07 01:18:48 -0500301 errno = 0;
Jon Loeliger39488492007-02-16 09:33:54 -0600302 }
303
304 return c;
305}