blob: 357b878607ca86a84a0130a8026efca689a0579c [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Rob Herring9130ba82018-02-27 17:40:38 -06002#ifndef DTC_H
3#define DTC_H
David Gibsona4da2e32007-12-18 15:06:42 +11004
5/*
6 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
David Gibsona4da2e32007-12-18 15:06:42 +11007 */
8
9#include <stdio.h>
10#include <string.h>
11#include <stdlib.h>
12#include <stdint.h>
Stephen Warrencd296722012-09-28 21:25:59 +000013#include <stdbool.h>
David Gibsona4da2e32007-12-18 15:06:42 +110014#include <stdarg.h>
15#include <assert.h>
16#include <ctype.h>
17#include <errno.h>
18#include <unistd.h>
Rob Herring4201d052017-10-03 11:37:04 -050019#include <inttypes.h>
David Gibsona4da2e32007-12-18 15:06:42 +110020
David Gibsoned95d742008-08-07 12:24:17 +100021#include <libfdt_env.h>
David Gibsona4da2e32007-12-18 15:06:42 +110022#include <fdt.h>
23
John Bonesio658f29a2010-11-17 15:28:20 -080024#include "util.h"
25
26#ifdef DEBUG
Rob Herring47605972015-04-29 16:00:05 -050027#define debug(...) printf(__VA_ARGS__)
John Bonesio658f29a2010-11-17 15:28:20 -080028#else
Rob Herring47605972015-04-29 16:00:05 -050029#define debug(...)
John Bonesio658f29a2010-11-17 15:28:20 -080030#endif
31
David Gibsona4da2e32007-12-18 15:06:42 +110032#define DEFAULT_FDT_VERSION 17
John Bonesio658f29a2010-11-17 15:28:20 -080033
David Gibsona4da2e32007-12-18 15:06:42 +110034/*
35 * Command line options
36 */
37extern int quiet; /* Level of quietness */
38extern int reservenum; /* Number of memory reservation slots */
39extern int minsize; /* Minimum blob size */
40extern int padsize; /* Additional padding to blob */
Rob Herring6f05afc2017-01-04 10:45:20 -060041extern int alignsize; /* Additional padding to blob accroding to the alignsize */
John Bonesio658f29a2010-11-17 15:28:20 -080042extern int phandle_format; /* Use linux,phandle or phandle properties */
Rob Herring6f05afc2017-01-04 10:45:20 -060043extern int generate_symbols; /* generate symbols for nodes with labels */
44extern int generate_fixups; /* generate fixups */
45extern int auto_label_aliases; /* auto generate labels -> aliases */
Rob Herringc2e70752018-11-28 18:37:35 -060046extern int annotate; /* annotate .dts with input source location */
David Gibsona4da2e32007-12-18 15:06:42 +110047
John Bonesio658f29a2010-11-17 15:28:20 -080048#define PHANDLE_LEGACY 0x1
49#define PHANDLE_EPAPR 0x2
50#define PHANDLE_BOTH 0x3
David Gibsona4da2e32007-12-18 15:06:42 +110051
David Gibsoned95d742008-08-07 12:24:17 +100052typedef uint32_t cell_t;
David Gibsona4da2e32007-12-18 15:06:42 +110053
David Gibsona4da2e32007-12-18 15:06:42 +110054
55#define streq(a, b) (strcmp((a), (b)) == 0)
Rob Herring9130ba82018-02-27 17:40:38 -060056#define strstarts(s, prefix) (strncmp((s), (prefix), strlen(prefix)) == 0)
57#define strprefixeq(a, n, b) (strlen(b) == (n) && (memcmp(a, b, n) == 0))
David Gibsona4da2e32007-12-18 15:06:42 +110058
59#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
David Gibsona4da2e32007-12-18 15:06:42 +110060
61/* Data blobs */
62enum markertype {
Rob Herringf8589272018-09-13 08:59:25 -050063 TYPE_NONE,
David Gibsona4da2e32007-12-18 15:06:42 +110064 REF_PHANDLE,
65 REF_PATH,
66 LABEL,
Rob Herringf8589272018-09-13 08:59:25 -050067 TYPE_UINT8,
68 TYPE_UINT16,
69 TYPE_UINT32,
70 TYPE_UINT64,
71 TYPE_STRING,
David Gibsona4da2e32007-12-18 15:06:42 +110072};
Rob Herringf8589272018-09-13 08:59:25 -050073extern const char *markername(enum markertype markertype);
David Gibsona4da2e32007-12-18 15:06:42 +110074
75struct marker {
76 enum markertype type;
77 int offset;
78 char *ref;
79 struct marker *next;
80};
81
82struct data {
83 int len;
84 char *val;
David Gibsona4da2e32007-12-18 15:06:42 +110085 struct marker *markers;
86};
87
88
Rob Herring47605972015-04-29 16:00:05 -050089#define empty_data ((struct data){ 0 /* all .members = 0 or NULL */ })
David Gibsona4da2e32007-12-18 15:06:42 +110090
91#define for_each_marker(m) \
92 for (; (m); (m) = (m)->next)
93#define for_each_marker_of_type(m, t) \
94 for_each_marker(m) \
95 if ((m)->type == (t))
96
Rob Herringf8589272018-09-13 08:59:25 -050097size_t type_marker_length(struct marker *m);
98
David Gibsona4da2e32007-12-18 15:06:42 +110099void data_free(struct data d);
100
101struct data data_grow_for(struct data d, int xlen);
102
103struct data data_copy_mem(const char *mem, int len);
104struct data data_copy_escape_string(const char *s, int len);
105struct data data_copy_file(FILE *f, size_t len);
106
107struct data data_append_data(struct data d, const void *p, int len);
108struct data data_insert_at_marker(struct data d, struct marker *m,
109 const void *p, int len);
110struct data data_merge(struct data d1, struct data d2);
111struct data data_append_cell(struct data d, cell_t word);
Stephen Warrencd296722012-09-28 21:25:59 +0000112struct data data_append_integer(struct data d, uint64_t word, int bits);
Rob Herring89d12312017-03-21 09:01:08 -0500113struct data data_append_re(struct data d, uint64_t address, uint64_t size);
David Gibsoned95d742008-08-07 12:24:17 +1000114struct data data_append_addr(struct data d, uint64_t addr);
David Gibsona4da2e32007-12-18 15:06:42 +1100115struct data data_append_byte(struct data d, uint8_t byte);
116struct data data_append_zeroes(struct data d, int len);
117struct data data_append_align(struct data d, int align);
118
119struct data data_add_marker(struct data d, enum markertype type, char *ref);
120
Rob Herring47605972015-04-29 16:00:05 -0500121bool data_is_one_string(struct data d);
David Gibsona4da2e32007-12-18 15:06:42 +1100122
123/* DT constraints */
124
125#define MAX_PROPNAME_LEN 31
126#define MAX_NODENAME_LEN 31
127
128/* Live trees */
John Bonesio658f29a2010-11-17 15:28:20 -0800129struct label {
Rob Herring47605972015-04-29 16:00:05 -0500130 bool deleted;
John Bonesio658f29a2010-11-17 15:28:20 -0800131 char *label;
132 struct label *next;
133};
134
Rob Herring89d12312017-03-21 09:01:08 -0500135struct bus_type {
136 const char *name;
137};
138
David Gibsona4da2e32007-12-18 15:06:42 +1100139struct property {
Rob Herring47605972015-04-29 16:00:05 -0500140 bool deleted;
David Gibsona4da2e32007-12-18 15:06:42 +1100141 char *name;
142 struct data val;
143
144 struct property *next;
145
John Bonesio658f29a2010-11-17 15:28:20 -0800146 struct label *labels;
Rob Herringc2e70752018-11-28 18:37:35 -0600147 struct srcpos *srcpos;
David Gibsona4da2e32007-12-18 15:06:42 +1100148};
149
150struct node {
Rob Herring47605972015-04-29 16:00:05 -0500151 bool deleted;
David Gibsona4da2e32007-12-18 15:06:42 +1100152 char *name;
153 struct property *proplist;
154 struct node *children;
155
156 struct node *parent;
157 struct node *next_sibling;
158
159 char *fullpath;
160 int basenamelen;
161
162 cell_t phandle;
163 int addr_cells, size_cells;
164
John Bonesio658f29a2010-11-17 15:28:20 -0800165 struct label *labels;
Rob Herring89d12312017-03-21 09:01:08 -0500166 const struct bus_type *bus;
Rob Herringc2e70752018-11-28 18:37:35 -0600167 struct srcpos *srcpos;
Rob Herring50aafd62018-05-08 13:07:49 -0500168
169 bool omit_if_unused, is_referenced;
David Gibsona4da2e32007-12-18 15:06:42 +1100170};
171
Stephen Warrencd296722012-09-28 21:25:59 +0000172#define for_each_label_withdel(l0, l) \
John Bonesio658f29a2010-11-17 15:28:20 -0800173 for ((l) = (l0); (l); (l) = (l)->next)
174
Stephen Warren205a8eb2012-10-08 16:15:26 -0600175#define for_each_label(l0, l) \
176 for_each_label_withdel(l0, l) \
177 if (!(l)->deleted)
Stephen Warrencd296722012-09-28 21:25:59 +0000178
179#define for_each_property_withdel(n, p) \
David Gibsona4da2e32007-12-18 15:06:42 +1100180 for ((p) = (n)->proplist; (p); (p) = (p)->next)
181
Stephen Warren205a8eb2012-10-08 16:15:26 -0600182#define for_each_property(n, p) \
183 for_each_property_withdel(n, p) \
184 if (!(p)->deleted)
Stephen Warrencd296722012-09-28 21:25:59 +0000185
186#define for_each_child_withdel(n, c) \
David Gibsona4da2e32007-12-18 15:06:42 +1100187 for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
188
Stephen Warren205a8eb2012-10-08 16:15:26 -0600189#define for_each_child(n, c) \
190 for_each_child_withdel(n, c) \
191 if (!(c)->deleted)
192
John Bonesio658f29a2010-11-17 15:28:20 -0800193void add_label(struct label **labels, char *label);
Stephen Warrencd296722012-09-28 21:25:59 +0000194void delete_labels(struct label **labels);
John Bonesio658f29a2010-11-17 15:28:20 -0800195
Rob Herringc2e70752018-11-28 18:37:35 -0600196struct property *build_property(char *name, struct data val,
197 struct srcpos *srcpos);
Stephen Warrencd296722012-09-28 21:25:59 +0000198struct property *build_property_delete(char *name);
David Gibsona4da2e32007-12-18 15:06:42 +1100199struct property *chain_property(struct property *first, struct property *list);
200struct property *reverse_properties(struct property *first);
201
Rob Herringc2e70752018-11-28 18:37:35 -0600202struct node *build_node(struct property *proplist, struct node *children,
203 struct srcpos *srcpos);
204struct node *build_node_delete(struct srcpos *srcpos);
John Bonesio658f29a2010-11-17 15:28:20 -0800205struct node *name_node(struct node *node, char *name);
Rob Herring50aafd62018-05-08 13:07:49 -0500206struct node *omit_node_if_unused(struct node *node);
207struct node *reference_node(struct node *node);
David Gibsona4da2e32007-12-18 15:06:42 +1100208struct node *chain_node(struct node *first, struct node *list);
John Bonesio658f29a2010-11-17 15:28:20 -0800209struct node *merge_nodes(struct node *old_node, struct node *new_node);
Rob Herring9130ba82018-02-27 17:40:38 -0600210struct node *add_orphan_node(struct node *old_node, struct node *new_node, char *ref);
David Gibsona4da2e32007-12-18 15:06:42 +1100211
212void add_property(struct node *node, struct property *prop);
Stephen Warrencd296722012-09-28 21:25:59 +0000213void delete_property_by_name(struct node *node, char *name);
214void delete_property(struct property *prop);
David Gibsona4da2e32007-12-18 15:06:42 +1100215void add_child(struct node *parent, struct node *child);
Stephen Warrencd296722012-09-28 21:25:59 +0000216void delete_node_by_name(struct node *parent, char *name);
217void delete_node(struct node *node);
Rob Herring6f05afc2017-01-04 10:45:20 -0600218void append_to_property(struct node *node,
219 char *name, const void *data, int len);
David Gibsona4da2e32007-12-18 15:06:42 +1100220
221const char *get_unitname(struct node *node);
222struct property *get_property(struct node *node, const char *propname);
223cell_t propval_cell(struct property *prop);
Rob Herring4201d052017-10-03 11:37:04 -0500224cell_t propval_cell_n(struct property *prop, int n);
John Bonesio658f29a2010-11-17 15:28:20 -0800225struct property *get_property_by_label(struct node *tree, const char *label,
226 struct node **node);
227struct marker *get_marker_label(struct node *tree, const char *label,
228 struct node **node, struct property **prop);
David Gibsona4da2e32007-12-18 15:06:42 +1100229struct node *get_subnode(struct node *node, const char *nodename);
230struct node *get_node_by_path(struct node *tree, const char *path);
231struct node *get_node_by_label(struct node *tree, const char *label);
232struct node *get_node_by_phandle(struct node *tree, cell_t phandle);
233struct node *get_node_by_ref(struct node *tree, const char *ref);
234cell_t get_node_phandle(struct node *root, struct node *node);
235
John Bonesio658f29a2010-11-17 15:28:20 -0800236uint32_t guess_boot_cpuid(struct node *tree);
237
David Gibsona4da2e32007-12-18 15:06:42 +1100238/* Boot info (tree plus memreserve information */
239
240struct reserve_info {
Rob Herring89d12312017-03-21 09:01:08 -0500241 uint64_t address, size;
David Gibsona4da2e32007-12-18 15:06:42 +1100242
243 struct reserve_info *next;
244
John Bonesio658f29a2010-11-17 15:28:20 -0800245 struct label *labels;
David Gibsona4da2e32007-12-18 15:06:42 +1100246};
247
John Bonesio658f29a2010-11-17 15:28:20 -0800248struct reserve_info *build_reserve_entry(uint64_t start, uint64_t len);
David Gibsona4da2e32007-12-18 15:06:42 +1100249struct reserve_info *chain_reserve_entry(struct reserve_info *first,
250 struct reserve_info *list);
251struct reserve_info *add_reserve_entry(struct reserve_info *list,
252 struct reserve_info *new);
253
254
Rob Herring6f05afc2017-01-04 10:45:20 -0600255struct dt_info {
256 unsigned int dtsflags;
David Gibsona4da2e32007-12-18 15:06:42 +1100257 struct reserve_info *reservelist;
David Gibsoned95d742008-08-07 12:24:17 +1000258 uint32_t boot_cpuid_phys;
Rob Herring6f05afc2017-01-04 10:45:20 -0600259 struct node *dt; /* the device tree */
Rob Herring89d12312017-03-21 09:01:08 -0500260 const char *outname; /* filename being written to, "-" for stdout */
David Gibsona4da2e32007-12-18 15:06:42 +1100261};
262
Rob Herring6f05afc2017-01-04 10:45:20 -0600263/* DTS version flags definitions */
264#define DTSF_V1 0x0001 /* /dts-v1/ */
265#define DTSF_PLUGIN 0x0002 /* /plugin/ */
266
267struct dt_info *build_dt_info(unsigned int dtsflags,
268 struct reserve_info *reservelist,
269 struct node *tree, uint32_t boot_cpuid_phys);
270void sort_tree(struct dt_info *dti);
271void generate_label_tree(struct dt_info *dti, char *name, bool allocph);
272void generate_fixups_tree(struct dt_info *dti, char *name);
273void generate_local_fixups_tree(struct dt_info *dti, char *name);
David Gibsona4da2e32007-12-18 15:06:42 +1100274
275/* Checks */
276
Rob Herring47605972015-04-29 16:00:05 -0500277void parse_checks_option(bool warn, bool error, const char *arg);
Rob Herring6f05afc2017-01-04 10:45:20 -0600278void process_checks(bool force, struct dt_info *dti);
David Gibsona4da2e32007-12-18 15:06:42 +1100279
280/* Flattened trees */
281
Rob Herring6f05afc2017-01-04 10:45:20 -0600282void dt_to_blob(FILE *f, struct dt_info *dti, int version);
283void dt_to_asm(FILE *f, struct dt_info *dti, int version);
David Gibsona4da2e32007-12-18 15:06:42 +1100284
Rob Herring6f05afc2017-01-04 10:45:20 -0600285struct dt_info *dt_from_blob(const char *fname);
David Gibsona4da2e32007-12-18 15:06:42 +1100286
287/* Tree source */
288
Rob Herring6f05afc2017-01-04 10:45:20 -0600289void dt_to_source(FILE *f, struct dt_info *dti);
290struct dt_info *dt_from_source(const char *f);
David Gibsona4da2e32007-12-18 15:06:42 +1100291
Rob Herringf8589272018-09-13 08:59:25 -0500292/* YAML source */
293
294void dt_to_yaml(FILE *f, struct dt_info *dti);
295
David Gibsona4da2e32007-12-18 15:06:42 +1100296/* FS trees */
297
Rob Herring6f05afc2017-01-04 10:45:20 -0600298struct dt_info *dt_from_fs(const char *dirname);
David Gibsona4da2e32007-12-18 15:06:42 +1100299
Rob Herring9130ba82018-02-27 17:40:38 -0600300#endif /* DTC_H */