Blame view
scripts/dtc/dtc-parser.y
5.83 KB
a4da2e3ec
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/* * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. * * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ |
a4da2e3ec
|
20 |
%{ |
ed95d7450
|
21 |
#include <stdio.h> |
a4da2e3ec
|
22 23 |
#include "dtc.h" #include "srcpos.h" |
658f29a51
|
24 |
YYLTYPE yylloc; |
ed95d7450
|
25 |
extern int yylex(void); |
658f29a51
|
26 27 |
extern void print_error(char const *fmt, ...); extern void yyerror(char const *s); |
a4da2e3ec
|
28 29 |
extern struct boot_info *the_boot_info; |
ed95d7450
|
30 |
extern int treesource_error; |
a4da2e3ec
|
31 |
|
ed95d7450
|
32 |
static unsigned long long eval_literal(const char *s, int base, int bits); |
a4da2e3ec
|
33 34 35 36 37 38 39 |
%} %union { char *propnodename; char *literal; char *labelref; unsigned int cbase; |
ed95d7450
|
40 |
uint8_t byte; |
a4da2e3ec
|
41 |
struct data data; |
ed95d7450
|
42 |
uint64_t addr; |
a4da2e3ec
|
43 44 45 46 47 48 49 50 51 52 53 54 |
cell_t cell; struct property *prop; struct property *proplist; struct node *node; struct node *nodelist; struct reserve_info *re; } %token DT_V1 %token DT_MEMRESERVE %token <propnodename> DT_PROPNODENAME %token <literal> DT_LITERAL |
a4da2e3ec
|
55 56 57 58 59 |
%token <cbase> DT_BASE %token <byte> DT_BYTE %token <data> DT_STRING %token <labelref> DT_LABEL %token <labelref> DT_REF |
ed95d7450
|
60 |
%token DT_INCBIN |
a4da2e3ec
|
61 62 63 64 65 |
%type <data> propdata %type <data> propdataprefix %type <re> memreserve %type <re> memreserves |
a4da2e3ec
|
66 67 |
%type <addr> addr %type <data> celllist |
a4da2e3ec
|
68 69 70 71 72 73 74 75 76 |
%type <cell> cellval %type <data> bytestring %type <prop> propdef %type <proplist> proplist %type <node> devicetree %type <node> nodedef %type <node> subnode %type <nodelist> subnodes |
a4da2e3ec
|
77 78 79 80 81 82 |
%% sourcefile: DT_V1 ';' memreserves devicetree { |
658f29a51
|
83 84 |
the_boot_info = build_boot_info($3, $4, guess_boot_cpuid($4)); |
a4da2e3ec
|
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
} ; memreserves: /* empty */ { $$ = NULL; } | memreserve memreserves { $$ = chain_reserve_entry($1, $2); } ; memreserve: |
658f29a51
|
100 |
DT_MEMRESERVE addr addr ';' |
a4da2e3ec
|
101 |
{ |
658f29a51
|
102 |
$$ = build_reserve_entry($2, $3); |
a4da2e3ec
|
103 |
} |
658f29a51
|
104 |
| DT_LABEL memreserve |
a4da2e3ec
|
105 |
{ |
658f29a51
|
106 107 |
add_label(&$2->labels, $1); $$ = $2; |
a4da2e3ec
|
108 109 110 111 112 113 114 115 |
} ; addr: DT_LITERAL { $$ = eval_literal($1, 0, 64); } |
a4da2e3ec
|
116 117 118 119 120 |
; devicetree: '/' nodedef { |
658f29a51
|
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
$$ = name_node($2, ""); } | devicetree '/' nodedef { $$ = merge_nodes($1, $3); } | devicetree DT_REF nodedef { struct node *target = get_node_by_ref($1, $2); if (target) merge_nodes(target, $3); else print_error("label or path, '%s', not found", $2); $$ = $1; |
a4da2e3ec
|
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
} ; nodedef: '{' proplist subnodes '}' ';' { $$ = build_node($2, $3); } ; proplist: /* empty */ { $$ = NULL; } | proplist propdef { $$ = chain_property($2, $1); } ; propdef: |
658f29a51
|
158 159 160 161 162 |
DT_PROPNODENAME '=' propdata ';' { $$ = build_property($1, $3); } | DT_PROPNODENAME ';' |
a4da2e3ec
|
163 |
{ |
658f29a51
|
164 |
$$ = build_property($1, empty_data); |
a4da2e3ec
|
165 |
} |
658f29a51
|
166 |
| DT_LABEL propdef |
a4da2e3ec
|
167 |
{ |
658f29a51
|
168 169 |
add_label(&$2->labels, $1); $$ = $2; |
a4da2e3ec
|
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
} ; propdata: propdataprefix DT_STRING { $$ = data_merge($1, $2); } | propdataprefix '<' celllist '>' { $$ = data_merge($1, $3); } | propdataprefix '[' bytestring ']' { $$ = data_merge($1, $3); } | propdataprefix DT_REF { $$ = data_add_marker($1, REF_PATH, $2); } |
ed95d7450
|
190 191 |
| propdataprefix DT_INCBIN '(' DT_STRING ',' addr ',' addr ')' { |
658f29a51
|
192 193 |
FILE *f = srcfile_relative_open($4.val, NULL); struct data d; |
ed95d7450
|
194 195 |
if ($6 != 0) |
658f29a51
|
196 197 198 199 200 |
if (fseek(f, $6, SEEK_SET) != 0) print_error("Couldn't seek to offset %llu in \"%s\": %s", (unsigned long long)$6, $4.val, strerror(errno)); |
ed95d7450
|
201 |
|
658f29a51
|
202 |
d = data_copy_file(f, $8); |
ed95d7450
|
203 204 |
$$ = data_merge($1, d); |
658f29a51
|
205 |
fclose(f); |
ed95d7450
|
206 207 208 |
} | propdataprefix DT_INCBIN '(' DT_STRING ')' { |
658f29a51
|
209 |
FILE *f = srcfile_relative_open($4.val, NULL); |
ed95d7450
|
210 |
struct data d = empty_data; |
658f29a51
|
211 |
d = data_copy_file(f, -1); |
ed95d7450
|
212 213 |
$$ = data_merge($1, d); |
658f29a51
|
214 |
fclose(f); |
ed95d7450
|
215 |
} |
a4da2e3ec
|
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
| propdata DT_LABEL { $$ = data_add_marker($1, LABEL, $2); } ; propdataprefix: /* empty */ { $$ = empty_data; } | propdata ',' { $$ = $1; } | propdataprefix DT_LABEL { $$ = data_add_marker($1, LABEL, $2); } ; celllist: /* empty */ { $$ = empty_data; } | celllist cellval { $$ = data_append_cell($1, $2); } | celllist DT_REF { $$ = data_append_cell(data_add_marker($1, REF_PHANDLE, $2), -1); } | celllist DT_LABEL { $$ = data_add_marker($1, LABEL, $2); } ; |
a4da2e3ec
|
256 257 258 259 260 |
cellval: DT_LITERAL { $$ = eval_literal($1, 0, 32); } |
a4da2e3ec
|
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
; bytestring: /* empty */ { $$ = empty_data; } | bytestring DT_BYTE { $$ = data_append_byte($1, $2); } | bytestring DT_LABEL { $$ = data_add_marker($1, LABEL, $2); } ; subnodes: /* empty */ { $$ = NULL; } |
658f29a51
|
283 |
| subnode subnodes |
a4da2e3ec
|
284 285 286 287 288 |
{ $$ = chain_node($1, $2); } | subnode propdef { |
658f29a51
|
289 |
print_error("syntax error: properties must precede subnodes"); |
a4da2e3ec
|
290 291 292 293 294 |
YYERROR; } ; subnode: |
658f29a51
|
295 |
DT_PROPNODENAME nodedef |
a4da2e3ec
|
296 |
{ |
658f29a51
|
297 |
$$ = name_node($2, $1); |
a4da2e3ec
|
298 |
} |
658f29a51
|
299 |
| DT_LABEL subnode |
a4da2e3ec
|
300 |
{ |
658f29a51
|
301 302 |
add_label(&$2->labels, $1); $$ = $2; |
a4da2e3ec
|
303 304 305 306 |
} ; %% |
658f29a51
|
307 |
void print_error(char const *fmt, ...) |
a4da2e3ec
|
308 |
{ |
ed95d7450
|
309 |
va_list va; |
a4da2e3ec
|
310 |
|
658f29a51
|
311 312 313 |
va_start(va, fmt); srcpos_verror(&yylloc, fmt, va); va_end(va); |
ed95d7450
|
314 315 |
treesource_error = 1; |
ed95d7450
|
316 |
} |
658f29a51
|
317 318 |
void yyerror(char const *s) { print_error("%s", s); |
a4da2e3ec
|
319 |
} |
ed95d7450
|
320 |
static unsigned long long eval_literal(const char *s, int base, int bits) |
a4da2e3ec
|
321 322 323 324 325 326 327 |
{ unsigned long long val; char *e; errno = 0; val = strtoull(s, &e, base); if (*e) |
658f29a51
|
328 |
print_error("bad characters in literal"); |
a4da2e3ec
|
329 330 |
else if ((errno == ERANGE) || ((bits < 64) && (val >= (1ULL << bits)))) |
658f29a51
|
331 |
print_error("literal out of range"); |
a4da2e3ec
|
332 |
else if (errno != 0) |
658f29a51
|
333 |
print_error("bad literal"); |
a4da2e3ec
|
334 335 |
return val; } |