-
Notifications
You must be signed in to change notification settings - Fork 0
/
dt.h
414 lines (345 loc) · 11.3 KB
/
dt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* dt.h - DT header for methods
*
* YAML to DTB generator
*
* (C) Copyright Pantelis Antoniou <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* (1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* (3)The name of the author may not be used to
* endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef DT_H
#define DT_H
#include "config.h"
#include <stdint.h>
#include <sys/time.h>
#ifdef __linux__
#include <linux/limits.h>
#else
#include <limits.h>
#endif
#include <yaml.h>
#include "list.h"
#include "libfdt_env.h"
#include "fdt.h"
#include "tree.h"
#include "dtsparser.h"
/* should be enough */
#define YAMLDL_PROP_SEQ_TAG_DEPTH_MAX 128
struct dt_yaml_mark {
yaml_mark_t start;
yaml_mark_t end;
};
static inline bool dt_mark_is_unset(const struct dt_yaml_mark *m)
{
return m->start.index == 0 && m->end.index == 0;
}
struct dt_node {
struct node n;
struct dt_yaml_mark m;
};
#define to_dt_node(_n) container_of(_n, struct dt_node, n)
#define to_node(_np) (&(_np)->n)
struct dt_property {
struct property p;
struct dt_yaml_mark m;
};
#define to_dt_property(_p) container_of(_p, struct dt_property, p)
#define to_property(_prop) (&(_prop)->p)
struct dt_ref {
struct ref r;
struct dt_yaml_mark m;
const char *tag; /* tag after implicit resolve */
unsigned long long val;
union {
struct {
bool is_int : 1;
bool is_str : 1;
bool is_bool : 1;
bool is_null : 1;
bool is_resolved : 1;
bool is_builtin_tag : 1;
bool is_hex : 1;
bool is_unsigned : 1;
};
unsigned int is_flags;
};
struct node *npref; /* r_anchor, r_path */
const char *use_label;
void *alloc_data;
void *binary; /* to avoid costly conversions */
size_t binary_size;
};
#define to_dt_ref(_r) container_of(_r, struct dt_ref, r)
#define to_ref(_ref) (&(_ref)->r)
struct dt_label {
struct label l;
struct dt_yaml_mark m;
};
#define to_dt_label(_l) container_of(_l, struct dt_label, l)
#define to_label(_label) (&(_label)->l)
struct yaml_dt_config {
char * const *input_file;
int input_file_count;
const char *output_file;
bool debug;
int color;
bool save_temps;
bool force;
char * const *search_path;
bool warnings_are_errors;
/* for dtb & yaml */
bool object;
/* for dtb */
bool sort;
bool compatible;
bool symbols;
bool auto_alias;
bool force_boot_cpuid;
unsigned int quiet;
unsigned int reserve;
unsigned int space;
unsigned int align;
unsigned int pad;
unsigned int out_version;
unsigned int boot_cpuid;
const char *depname;
const char *schema;
const char *schema_save;
const char *codegen;
const char *input_format;
const char *output_format;
const char *phandle_format;
};
struct yaml_dt_input {
struct list_head node;
char *name;
void *content;
size_t size;
size_t pos;
struct yaml_dt_input *parent;
struct list_head includes;
bool dts; /* set to true when DTS source */
};
struct yaml_dt_span {
struct list_head node;
const struct yaml_dt_input *in;
size_t start_pos;
size_t end_pos;
struct dt_yaml_mark m;
};
struct yaml_dt_state;
struct yaml_dt_config;
struct yaml_dt_emitter_ops {
int (*setup)(struct yaml_dt_state *dt);
void (*cleanup)(struct yaml_dt_state *dt);
int (*emit)(struct yaml_dt_state *dt);
};
struct yaml_dt_emitter {
struct list_head node;
const char *name;
const char * const *suffixes;
const struct tree_ops *tops;
const struct yaml_dt_emitter_ops *eops;
};
struct yaml_dt_checker_ops {
int (*setup)(struct yaml_dt_state *dt);
void (*cleanup)(struct yaml_dt_state *dt);
int (*check)(struct yaml_dt_state *dt);
};
struct yaml_dt_checker {
struct list_head node;
const char *name;
const struct yaml_dt_checker_ops *cops;
};
struct yaml_dt_state {
struct yaml_dt_state *parent;
struct list_head node;
char *name;
struct list_head children;
struct yaml_dt_config cfg;
char *input_compiler_tag;
char *output_compiler_tag;
FILE *output;
int curr_input_file;
size_t curr_input_pos;
size_t curr_input_line;
size_t curr_input_column;
size_t last_input_pos;
size_t last_input_line;
size_t last_input_column;
struct yaml_dt_input *curr_in;
struct list_head inputs;
struct yaml_dt_span *curr_span;
struct dt_yaml_mark curr_span_mark;
struct list_head spans;
char **alloc_argv;
/* yaml parser state */
bool last_was_marker;
yaml_parser_t parser;
yaml_event_t *current_event;
struct dt_yaml_mark current_mark;
struct dt_yaml_mark last_map_mark;
struct dt_yaml_mark last_alias_mark;
struct node *current_np;
bool current_np_isref;
struct property *current_prop;
bool current_prop_existed;
char *map_key;
int depth;
int prop_seq_depth;
char *prop_seq_tag[YAMLDL_PROP_SEQ_TAG_DEPTH_MAX];
bool current_np_ref;
int bare_seq;
int bare_map;
bool stream_ended;
/* emitter data */
bool error_on_failed_get;
bool error_flag;
/* tree build state (initialized by the emitter) */
struct tree tree;
const struct yaml_dt_emitter *emitter;
void *emitter_state;
void *emitter_cfg;
const struct yaml_dt_checker *checker;
void *checker_state;
void *checker_cfg;
/* dts parser */
struct dts_state ds;
bool dts_initialized;
FILE *dep_output;
};
#define to_dt(_t) container_of(_t, struct yaml_dt_state, tree)
#define to_tree(_dt) (&(_dt)->tree)
static inline bool dt_get_error_flag(struct yaml_dt_state *dt)
{
return dt->error_flag;
}
static inline void dt_set_error_flag(struct yaml_dt_state *dt, bool error)
{
dt->error_flag = error;
}
static inline bool dt_get_error_on_failed_get(struct yaml_dt_state *dt)
{
return dt->error_on_failed_get;
}
static inline void dt_set_error_on_failed_get(struct yaml_dt_state *dt, bool error)
{
dt->error_on_failed_get = error;
}
void dt_debug(struct yaml_dt_state *dt, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 0)));
void dt_info(struct yaml_dt_state *dt, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 0)));
void dt_error(struct yaml_dt_state *dt, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 0)));
void dt_warning(struct yaml_dt_state *dt, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 0)));
void dt_exit_failure(struct yaml_dt_state *dt)
__attribute__ ((noreturn));
void dt_fatal(struct yaml_dt_state *dt, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 0)))
__attribute__ ((noreturn));
void dt_print_at(struct yaml_dt_state *dt,
const struct dt_yaml_mark *m,
const char *type, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 4, 0)));
void dt_error_at(struct yaml_dt_state *dt,
const struct dt_yaml_mark *m,
const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 3, 0)));
void dt_warning_at(struct yaml_dt_state *dt,
const struct dt_yaml_mark *m,
const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 3, 0)));
/* common tree hooks (called by emitters) */
struct ref *yaml_dt_ref_alloc(struct tree *t, enum ref_type type,
const void *data, int len, const char *xtag,
int size);
void yaml_dt_ref_free(struct tree *t, struct ref *ref);
struct property *yaml_dt_prop_alloc(struct tree *t, const char *name,
int size);
void yaml_dt_prop_free(struct tree *t, struct property *prop);
struct label *yaml_dt_label_alloc(struct tree *t, const char *name,
int size);
void yaml_dt_label_free(struct tree *t, struct label *l);
struct node *yaml_dt_node_alloc(struct tree *t, const char *name,
const char *label, int size);
void yaml_dt_node_free(struct tree *t, struct node *np);
void yaml_dt_tree_debugf(struct tree *t, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 0)));
void yaml_dt_tree_msg_at_node(struct tree *t, struct node *np,
const char *type, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 4, 0)));
void yaml_dt_tree_msg_at_property(struct tree *t, struct property *prop,
const char *type, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 4, 0)));
void yaml_dt_tree_msg_at_ref(struct tree *t, struct ref *ref,
const char *type, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 4, 0)));
void yaml_dt_tree_msg_at_label(struct tree *t, struct label *l,
const char *type, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 4, 0)));
int dt_setup(struct yaml_dt_state *dt, struct yaml_dt_config *cfg,
struct yaml_dt_emitter *emitter, struct yaml_dt_checker *checker);
int dt_parse(struct yaml_dt_state *dt);
int dt_emitter_emit(struct yaml_dt_state *dt);
int dt_checker_check(struct yaml_dt_state *dt);
void dt_cleanup(struct yaml_dt_state *dt, bool abnormal);
struct yaml_dt_state *dt_parse_single(struct yaml_dt_state *dt,
const char *input, const char *output, const char *name);
int dt_resolve_ref(struct yaml_dt_state *dt, struct ref *ref);
struct node *dt_get_node(struct yaml_dt_state *dt,
struct node *parent, const char *name,
int index);
struct property *dt_get_property(struct yaml_dt_state *dt,
struct node *np, const char *name,
int index);
struct ref *dt_get_ref(struct yaml_dt_state *dt,
struct property *prop, int index);
int dt_get_rcount(struct yaml_dt_state *dt, struct node *np,
const char *name, int pindex);
const char *dt_ref_string(struct yaml_dt_state *dt, struct ref *ref);
const char *dt_get_string(struct yaml_dt_state *dt, struct node *np,
const char *name, int pindex, int rindex);
unsigned long long dt_ref_int(struct yaml_dt_state *dt, struct ref *ref,
int *error);
unsigned long long dt_get_int(struct yaml_dt_state *dt, struct node *np,
const char *name, int pindex, int rindex, int *error);
int dt_ref_bool(struct yaml_dt_state *dt, struct ref *ref);
int dt_get_bool(struct yaml_dt_state *dt, struct node *np,
const char *name, int pindex, int rindex);
const void *dt_ref_binary(struct yaml_dt_state *dt, struct ref *ref,
size_t *binary_size);
const void *dt_get_binary(struct yaml_dt_state *dt, struct node *np,
const char *name, int pindex, int rindex,
size_t *binary_size);
struct node *dt_ref_noderef(struct yaml_dt_state *dt, struct ref *ref);
struct node *dt_get_noderef(struct yaml_dt_state *dt, struct node *np,
const char *name, int pindex, int rindex);
#endif