xref: /openbmc/u-boot/scripts/dtc/dtc.h (revision db405d1980e0e3bfdd629399d1dfe457f1f32646)
1*db405d19SRob Herring #ifndef DTC_H
2*db405d19SRob Herring #define DTC_H
3c0e032e0STom Rini 
4c0e032e0STom Rini /*
5c0e032e0STom Rini  * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
6c0e032e0STom Rini  *
7c0e032e0STom Rini  *
8c0e032e0STom Rini  * This program is free software; you can redistribute it and/or
9c0e032e0STom Rini  * modify it under the terms of the GNU General Public License as
10c0e032e0STom Rini  * published by the Free Software Foundation; either version 2 of the
11c0e032e0STom Rini  * License, or (at your option) any later version.
12c0e032e0STom Rini  *
13c0e032e0STom Rini  *  This program is distributed in the hope that it will be useful,
14c0e032e0STom Rini  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15c0e032e0STom Rini  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16c0e032e0STom Rini  *  General Public License for more details.
17c0e032e0STom Rini  *
18c0e032e0STom Rini  *  You should have received a copy of the GNU General Public License
19c0e032e0STom Rini  *  along with this program; if not, write to the Free Software
20c0e032e0STom Rini  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
21c0e032e0STom Rini  *                                                                   USA
22c0e032e0STom Rini  */
23c0e032e0STom Rini 
24c0e032e0STom Rini #include <stdio.h>
25c0e032e0STom Rini #include <string.h>
26c0e032e0STom Rini #include <stdlib.h>
27c0e032e0STom Rini #include <stdint.h>
28c0e032e0STom Rini #include <stdbool.h>
29c0e032e0STom Rini #include <stdarg.h>
30c0e032e0STom Rini #include <assert.h>
31c0e032e0STom Rini #include <ctype.h>
32c0e032e0STom Rini #include <errno.h>
33c0e032e0STom Rini #include <unistd.h>
342d4c2259STom Rini #include <inttypes.h>
35c0e032e0STom Rini 
36c0e032e0STom Rini #include <libfdt_env.h>
37c0e032e0STom Rini #include <fdt.h>
38c0e032e0STom Rini 
39c0e032e0STom Rini #include "util.h"
40c0e032e0STom Rini 
41c0e032e0STom Rini #ifdef DEBUG
42c0e032e0STom Rini #define debug(...)	printf(__VA_ARGS__)
43c0e032e0STom Rini #else
44c0e032e0STom Rini #define debug(...)
45c0e032e0STom Rini #endif
46c0e032e0STom Rini 
47c0e032e0STom Rini #define DEFAULT_FDT_VERSION	17
48c0e032e0STom Rini 
49c0e032e0STom Rini /*
50c0e032e0STom Rini  * Command line options
51c0e032e0STom Rini  */
52c0e032e0STom Rini extern int quiet;		/* Level of quietness */
53c0e032e0STom Rini extern int reservenum;		/* Number of memory reservation slots */
54c0e032e0STom Rini extern int minsize;		/* Minimum blob size */
55c0e032e0STom Rini extern int padsize;		/* Additional padding to blob */
56c0e032e0STom Rini extern int alignsize;		/* Additional padding to blob accroding to the alignsize */
57c0e032e0STom Rini extern int phandle_format;	/* Use linux,phandle or phandle properties */
58c0e032e0STom Rini extern int generate_symbols;	/* generate symbols for nodes with labels */
59c0e032e0STom Rini extern int generate_fixups;	/* generate fixups */
60c0e032e0STom Rini extern int auto_label_aliases;	/* auto generate labels -> aliases */
61c0e032e0STom Rini 
62c0e032e0STom Rini #define PHANDLE_LEGACY	0x1
63c0e032e0STom Rini #define PHANDLE_EPAPR	0x2
64c0e032e0STom Rini #define PHANDLE_BOTH	0x3
65c0e032e0STom Rini 
66c0e032e0STom Rini typedef uint32_t cell_t;
67c0e032e0STom Rini 
68c0e032e0STom Rini 
69c0e032e0STom Rini #define streq(a, b)	(strcmp((a), (b)) == 0)
70*db405d19SRob Herring #define strstarts(s, prefix)	(strncmp((s), (prefix), strlen(prefix)) == 0)
71*db405d19SRob Herring #define strprefixeq(a, n, b)	(strlen(b) == (n) && (memcmp(a, b, n) == 0))
72c0e032e0STom Rini 
73c0e032e0STom Rini #define ALIGN(x, a)	(((x) + (a) - 1) & ~((a) - 1))
74c0e032e0STom Rini 
75c0e032e0STom Rini /* Data blobs */
76c0e032e0STom Rini enum markertype {
77c0e032e0STom Rini 	REF_PHANDLE,
78c0e032e0STom Rini 	REF_PATH,
79c0e032e0STom Rini 	LABEL,
80c0e032e0STom Rini };
81c0e032e0STom Rini 
82c0e032e0STom Rini struct  marker {
83c0e032e0STom Rini 	enum markertype type;
84c0e032e0STom Rini 	int offset;
85c0e032e0STom Rini 	char *ref;
86c0e032e0STom Rini 	struct marker *next;
87c0e032e0STom Rini };
88c0e032e0STom Rini 
89c0e032e0STom Rini struct data {
90c0e032e0STom Rini 	int len;
91c0e032e0STom Rini 	char *val;
92c0e032e0STom Rini 	struct marker *markers;
93c0e032e0STom Rini };
94c0e032e0STom Rini 
95c0e032e0STom Rini 
96c0e032e0STom Rini #define empty_data ((struct data){ 0 /* all .members = 0 or NULL */ })
97c0e032e0STom Rini 
98c0e032e0STom Rini #define for_each_marker(m) \
99c0e032e0STom Rini 	for (; (m); (m) = (m)->next)
100c0e032e0STom Rini #define for_each_marker_of_type(m, t) \
101c0e032e0STom Rini 	for_each_marker(m) \
102c0e032e0STom Rini 		if ((m)->type == (t))
103c0e032e0STom Rini 
104c0e032e0STom Rini void data_free(struct data d);
105c0e032e0STom Rini 
106c0e032e0STom Rini struct data data_grow_for(struct data d, int xlen);
107c0e032e0STom Rini 
108c0e032e0STom Rini struct data data_copy_mem(const char *mem, int len);
109c0e032e0STom Rini struct data data_copy_escape_string(const char *s, int len);
110c0e032e0STom Rini struct data data_copy_file(FILE *f, size_t len);
111c0e032e0STom Rini 
112c0e032e0STom Rini struct data data_append_data(struct data d, const void *p, int len);
113c0e032e0STom Rini struct data data_insert_at_marker(struct data d, struct marker *m,
114c0e032e0STom Rini 				  const void *p, int len);
115c0e032e0STom Rini struct data data_merge(struct data d1, struct data d2);
116c0e032e0STom Rini struct data data_append_cell(struct data d, cell_t word);
117c0e032e0STom Rini struct data data_append_integer(struct data d, uint64_t word, int bits);
118d6fc90ceSTom Rini struct data data_append_re(struct data d, uint64_t address, uint64_t size);
119c0e032e0STom Rini struct data data_append_addr(struct data d, uint64_t addr);
120c0e032e0STom Rini struct data data_append_byte(struct data d, uint8_t byte);
121c0e032e0STom Rini struct data data_append_zeroes(struct data d, int len);
122c0e032e0STom Rini struct data data_append_align(struct data d, int align);
123c0e032e0STom Rini 
124c0e032e0STom Rini struct data data_add_marker(struct data d, enum markertype type, char *ref);
125c0e032e0STom Rini 
126c0e032e0STom Rini bool data_is_one_string(struct data d);
127c0e032e0STom Rini 
128c0e032e0STom Rini /* DT constraints */
129c0e032e0STom Rini 
130c0e032e0STom Rini #define MAX_PROPNAME_LEN	31
131c0e032e0STom Rini #define MAX_NODENAME_LEN	31
132c0e032e0STom Rini 
133c0e032e0STom Rini /* Live trees */
134c0e032e0STom Rini struct label {
135c0e032e0STom Rini 	bool deleted;
136c0e032e0STom Rini 	char *label;
137c0e032e0STom Rini 	struct label *next;
138c0e032e0STom Rini };
139c0e032e0STom Rini 
1402d4c2259STom Rini struct bus_type {
1412d4c2259STom Rini 	const char *name;
1422d4c2259STom Rini };
1432d4c2259STom Rini 
144c0e032e0STom Rini struct property {
145c0e032e0STom Rini 	bool deleted;
146c0e032e0STom Rini 	char *name;
147c0e032e0STom Rini 	struct data val;
148c0e032e0STom Rini 
149c0e032e0STom Rini 	struct property *next;
150c0e032e0STom Rini 
151c0e032e0STom Rini 	struct label *labels;
152c0e032e0STom Rini };
153c0e032e0STom Rini 
154c0e032e0STom Rini struct node {
155c0e032e0STom Rini 	bool deleted;
156c0e032e0STom Rini 	char *name;
157c0e032e0STom Rini 	struct property *proplist;
158c0e032e0STom Rini 	struct node *children;
159c0e032e0STom Rini 
160c0e032e0STom Rini 	struct node *parent;
161c0e032e0STom Rini 	struct node *next_sibling;
162c0e032e0STom Rini 
163c0e032e0STom Rini 	char *fullpath;
164c0e032e0STom Rini 	int basenamelen;
165c0e032e0STom Rini 
166c0e032e0STom Rini 	cell_t phandle;
167c0e032e0STom Rini 	int addr_cells, size_cells;
168c0e032e0STom Rini 
169c0e032e0STom Rini 	struct label *labels;
1702d4c2259STom Rini 	const struct bus_type *bus;
171c0e032e0STom Rini };
172c0e032e0STom Rini 
173c0e032e0STom Rini #define for_each_label_withdel(l0, l) \
174c0e032e0STom Rini 	for ((l) = (l0); (l); (l) = (l)->next)
175c0e032e0STom Rini 
176c0e032e0STom Rini #define for_each_label(l0, l) \
177c0e032e0STom Rini 	for_each_label_withdel(l0, l) \
178c0e032e0STom Rini 		if (!(l)->deleted)
179c0e032e0STom Rini 
180c0e032e0STom Rini #define for_each_property_withdel(n, p) \
181c0e032e0STom Rini 	for ((p) = (n)->proplist; (p); (p) = (p)->next)
182c0e032e0STom Rini 
183c0e032e0STom Rini #define for_each_property(n, p) \
184c0e032e0STom Rini 	for_each_property_withdel(n, p) \
185c0e032e0STom Rini 		if (!(p)->deleted)
186c0e032e0STom Rini 
187c0e032e0STom Rini #define for_each_child_withdel(n, c) \
188c0e032e0STom Rini 	for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
189c0e032e0STom Rini 
190c0e032e0STom Rini #define for_each_child(n, c) \
191c0e032e0STom Rini 	for_each_child_withdel(n, c) \
192c0e032e0STom Rini 		if (!(c)->deleted)
193c0e032e0STom Rini 
194c0e032e0STom Rini void add_label(struct label **labels, char *label);
195c0e032e0STom Rini void delete_labels(struct label **labels);
196c0e032e0STom Rini 
197c0e032e0STom Rini struct property *build_property(char *name, struct data val);
198c0e032e0STom Rini struct property *build_property_delete(char *name);
199c0e032e0STom Rini struct property *chain_property(struct property *first, struct property *list);
200c0e032e0STom Rini struct property *reverse_properties(struct property *first);
201c0e032e0STom Rini 
202c0e032e0STom Rini struct node *build_node(struct property *proplist, struct node *children);
203c0e032e0STom Rini struct node *build_node_delete(void);
204c0e032e0STom Rini struct node *name_node(struct node *node, char *name);
205c0e032e0STom Rini struct node *chain_node(struct node *first, struct node *list);
206c0e032e0STom Rini struct node *merge_nodes(struct node *old_node, struct node *new_node);
207*db405d19SRob Herring struct node *add_orphan_node(struct node *old_node, struct node *new_node, char *ref);
208c0e032e0STom Rini 
209c0e032e0STom Rini void add_property(struct node *node, struct property *prop);
210c0e032e0STom Rini void delete_property_by_name(struct node *node, char *name);
211c0e032e0STom Rini void delete_property(struct property *prop);
212c0e032e0STom Rini void add_child(struct node *parent, struct node *child);
213c0e032e0STom Rini void delete_node_by_name(struct node *parent, char *name);
214c0e032e0STom Rini void delete_node(struct node *node);
215c0e032e0STom Rini void append_to_property(struct node *node,
216c0e032e0STom Rini 			char *name, const void *data, int len);
217c0e032e0STom Rini 
218c0e032e0STom Rini const char *get_unitname(struct node *node);
219c0e032e0STom Rini struct property *get_property(struct node *node, const char *propname);
220c0e032e0STom Rini cell_t propval_cell(struct property *prop);
221999a78d5SMasahiro Yamada cell_t propval_cell_n(struct property *prop, int n);
222c0e032e0STom Rini struct property *get_property_by_label(struct node *tree, const char *label,
223c0e032e0STom Rini 				       struct node **node);
224c0e032e0STom Rini struct marker *get_marker_label(struct node *tree, const char *label,
225c0e032e0STom Rini 				struct node **node, struct property **prop);
226c0e032e0STom Rini struct node *get_subnode(struct node *node, const char *nodename);
227c0e032e0STom Rini struct node *get_node_by_path(struct node *tree, const char *path);
228c0e032e0STom Rini struct node *get_node_by_label(struct node *tree, const char *label);
229c0e032e0STom Rini struct node *get_node_by_phandle(struct node *tree, cell_t phandle);
230c0e032e0STom Rini struct node *get_node_by_ref(struct node *tree, const char *ref);
231c0e032e0STom Rini cell_t get_node_phandle(struct node *root, struct node *node);
232c0e032e0STom Rini 
233c0e032e0STom Rini uint32_t guess_boot_cpuid(struct node *tree);
234c0e032e0STom Rini 
235c0e032e0STom Rini /* Boot info (tree plus memreserve information */
236c0e032e0STom Rini 
237c0e032e0STom Rini struct reserve_info {
238d6fc90ceSTom Rini 	uint64_t address, size;
239c0e032e0STom Rini 
240c0e032e0STom Rini 	struct reserve_info *next;
241c0e032e0STom Rini 
242c0e032e0STom Rini 	struct label *labels;
243c0e032e0STom Rini };
244c0e032e0STom Rini 
245c0e032e0STom Rini struct reserve_info *build_reserve_entry(uint64_t start, uint64_t len);
246c0e032e0STom Rini struct reserve_info *chain_reserve_entry(struct reserve_info *first,
247c0e032e0STom Rini 					 struct reserve_info *list);
248c0e032e0STom Rini struct reserve_info *add_reserve_entry(struct reserve_info *list,
249c0e032e0STom Rini 				       struct reserve_info *new);
250c0e032e0STom Rini 
251c0e032e0STom Rini 
252c0e032e0STom Rini struct dt_info {
253c0e032e0STom Rini 	unsigned int dtsflags;
254c0e032e0STom Rini 	struct reserve_info *reservelist;
255c0e032e0STom Rini 	uint32_t boot_cpuid_phys;
256c0e032e0STom Rini 	struct node *dt;		/* the device tree */
257c0e032e0STom Rini 	const char *outname;		/* filename being written to, "-" for stdout */
258c0e032e0STom Rini };
259c0e032e0STom Rini 
260c0e032e0STom Rini /* DTS version flags definitions */
261c0e032e0STom Rini #define DTSF_V1		0x0001	/* /dts-v1/ */
262c0e032e0STom Rini #define DTSF_PLUGIN	0x0002	/* /plugin/ */
263c0e032e0STom Rini 
264c0e032e0STom Rini struct dt_info *build_dt_info(unsigned int dtsflags,
265c0e032e0STom Rini 			      struct reserve_info *reservelist,
266c0e032e0STom Rini 			      struct node *tree, uint32_t boot_cpuid_phys);
267c0e032e0STom Rini void sort_tree(struct dt_info *dti);
268c0e032e0STom Rini void generate_label_tree(struct dt_info *dti, char *name, bool allocph);
269c0e032e0STom Rini void generate_fixups_tree(struct dt_info *dti, char *name);
270c0e032e0STom Rini void generate_local_fixups_tree(struct dt_info *dti, char *name);
271c0e032e0STom Rini 
272c0e032e0STom Rini /* Checks */
273c0e032e0STom Rini 
274c0e032e0STom Rini void parse_checks_option(bool warn, bool error, const char *arg);
275c0e032e0STom Rini void process_checks(bool force, struct dt_info *dti);
276c0e032e0STom Rini 
277c0e032e0STom Rini /* Flattened trees */
278c0e032e0STom Rini 
279c0e032e0STom Rini void dt_to_blob(FILE *f, struct dt_info *dti, int version);
280c0e032e0STom Rini void dt_to_asm(FILE *f, struct dt_info *dti, int version);
281c0e032e0STom Rini 
282c0e032e0STom Rini struct dt_info *dt_from_blob(const char *fname);
283c0e032e0STom Rini 
284c0e032e0STom Rini /* Tree source */
285c0e032e0STom Rini 
286c0e032e0STom Rini void dt_to_source(FILE *f, struct dt_info *dti);
287c0e032e0STom Rini struct dt_info *dt_from_source(const char *f);
288c0e032e0STom Rini 
289c0e032e0STom Rini /* FS trees */
290c0e032e0STom Rini 
291c0e032e0STom Rini struct dt_info *dt_from_fs(const char *dirname);
292c0e032e0STom Rini 
293*db405d19SRob Herring #endif /* DTC_H */
294