1204431Sraj#ifndef _DTC_H
2204431Sraj#define _DTC_H
3204431Sraj
4204431Sraj/*
5204431Sraj * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
6204431Sraj *
7204431Sraj *
8204431Sraj * This program is free software; you can redistribute it and/or
9204431Sraj * modify it under the terms of the GNU General Public License as
10204431Sraj * published by the Free Software Foundation; either version 2 of the
11204431Sraj * License, or (at your option) any later version.
12204431Sraj *
13204431Sraj *  This program is distributed in the hope that it will be useful,
14204431Sraj *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15204431Sraj *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16204431Sraj *  General Public License for more details.
17204431Sraj *
18204431Sraj *  You should have received a copy of the GNU General Public License
19204431Sraj *  along with this program; if not, write to the Free Software
20204431Sraj *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
21204431Sraj *                                                                   USA
22204431Sraj */
23204431Sraj
24204431Sraj#include <stdio.h>
25204431Sraj#include <string.h>
26204431Sraj#include <stdlib.h>
27204431Sraj#include <stdint.h>
28238742Simp#include <stdbool.h>
29204431Sraj#include <stdarg.h>
30204431Sraj#include <assert.h>
31204431Sraj#include <ctype.h>
32204431Sraj#include <errno.h>
33204431Sraj#include <unistd.h>
34204431Sraj
35204431Sraj#include <libfdt_env.h>
36204431Sraj#include <fdt.h>
37204431Sraj
38204433Sraj#include "util.h"
39204433Sraj
40204433Sraj#ifdef DEBUG
41318102Sgonzo#define debug(...)	printf(__VA_ARGS__)
42204433Sraj#else
43318102Sgonzo#define debug(...)
44204433Sraj#endif
45204433Sraj
46204433Sraj
47204431Sraj#define DEFAULT_FDT_VERSION	17
48204433Sraj
49204431Sraj/*
50204431Sraj * Command line options
51204431Sraj */
52204431Srajextern int quiet;		/* Level of quietness */
53204431Srajextern int reservenum;		/* Number of memory reservation slots */
54204431Srajextern int minsize;		/* Minimum blob size */
55204431Srajextern int padsize;		/* Additional padding to blob */
56318102Sgonzoextern int alignsize;		/* Additional padding to blob accroding to the alignsize */
57204433Srajextern int phandle_format;	/* Use linux,phandle or phandle properties */
58318102Sgonzoextern int generate_symbols;	/* generate symbols for nodes with labels */
59318102Sgonzoextern int generate_fixups;	/* generate fixups */
60318102Sgonzoextern int auto_label_aliases;	/* auto generate labels -> aliases */
61204431Sraj
62204433Sraj#define PHANDLE_LEGACY	0x1
63204433Sraj#define PHANDLE_EPAPR	0x2
64204433Sraj#define PHANDLE_BOTH	0x3
65204431Sraj
66204431Srajtypedef uint32_t cell_t;
67204431Sraj
68204431Sraj
69204431Sraj#define streq(a, b)	(strcmp((a), (b)) == 0)
70204431Sraj#define strneq(a, b, n)	(strncmp((a), (b), (n)) == 0)
71204431Sraj
72204431Sraj#define ALIGN(x, a)	(((x) + (a) - 1) & ~((a) - 1))
73204431Sraj
74204431Sraj/* Data blobs */
75204431Srajenum markertype {
76204431Sraj	REF_PHANDLE,
77204431Sraj	REF_PATH,
78204431Sraj	LABEL,
79204431Sraj};
80204431Sraj
81204431Srajstruct  marker {
82204431Sraj	enum markertype type;
83204431Sraj	int offset;
84204431Sraj	char *ref;
85204431Sraj	struct marker *next;
86204431Sraj};
87204431Sraj
88204431Srajstruct data {
89204431Sraj	int len;
90204431Sraj	char *val;
91204431Sraj	struct marker *markers;
92204431Sraj};
93204431Sraj
94204431Sraj
95318102Sgonzo#define empty_data ((struct data){ 0 /* all .members = 0 or NULL */ })
96204431Sraj
97204431Sraj#define for_each_marker(m) \
98204431Sraj	for (; (m); (m) = (m)->next)
99204431Sraj#define for_each_marker_of_type(m, t) \
100204431Sraj	for_each_marker(m) \
101204431Sraj		if ((m)->type == (t))
102204431Sraj
103204431Srajvoid data_free(struct data d);
104204431Sraj
105204431Srajstruct data data_grow_for(struct data d, int xlen);
106204431Sraj
107204431Srajstruct data data_copy_mem(const char *mem, int len);
108204431Srajstruct data data_copy_escape_string(const char *s, int len);
109204431Srajstruct data data_copy_file(FILE *f, size_t len);
110204431Sraj
111204431Srajstruct data data_append_data(struct data d, const void *p, int len);
112204431Srajstruct data data_insert_at_marker(struct data d, struct marker *m,
113204431Sraj				  const void *p, int len);
114204431Srajstruct data data_merge(struct data d1, struct data d2);
115204431Srajstruct data data_append_cell(struct data d, cell_t word);
116238742Simpstruct data data_append_integer(struct data d, uint64_t word, int bits);
117204431Srajstruct data data_append_re(struct data d, const struct fdt_reserve_entry *re);
118204431Srajstruct data data_append_addr(struct data d, uint64_t addr);
119204431Srajstruct data data_append_byte(struct data d, uint8_t byte);
120204431Srajstruct data data_append_zeroes(struct data d, int len);
121204431Srajstruct data data_append_align(struct data d, int align);
122204431Sraj
123204431Srajstruct data data_add_marker(struct data d, enum markertype type, char *ref);
124204431Sraj
125261215Simpbool data_is_one_string(struct data d);
126204431Sraj
127204431Sraj/* DT constraints */
128204431Sraj
129204431Sraj#define MAX_PROPNAME_LEN	31
130204431Sraj#define MAX_NODENAME_LEN	31
131204431Sraj
132204431Sraj/* Live trees */
133238742Simpstruct label {
134261215Simp	bool deleted;
135238742Simp	char *label;
136238742Simp	struct label *next;
137238742Simp};
138238742Simp
139204431Srajstruct property {
140261215Simp	bool deleted;
141204431Sraj	char *name;
142204431Sraj	struct data val;
143204431Sraj
144204431Sraj	struct property *next;
145204431Sraj
146238742Simp	struct label *labels;
147204431Sraj};
148204431Sraj
149204431Srajstruct node {
150261215Simp	bool deleted;
151204431Sraj	char *name;
152204431Sraj	struct property *proplist;
153204431Sraj	struct node *children;
154204431Sraj
155204431Sraj	struct node *parent;
156204431Sraj	struct node *next_sibling;
157204431Sraj
158204431Sraj	char *fullpath;
159204431Sraj	int basenamelen;
160204431Sraj
161204431Sraj	cell_t phandle;
162204431Sraj	int addr_cells, size_cells;
163204431Sraj
164238742Simp	struct label *labels;
165204431Sraj};
166204431Sraj
167261215Simp#define for_each_label_withdel(l0, l) \
168238742Simp	for ((l) = (l0); (l); (l) = (l)->next)
169238742Simp
170261215Simp#define for_each_label(l0, l) \
171261215Simp	for_each_label_withdel(l0, l) \
172261215Simp		if (!(l)->deleted)
173261215Simp
174261215Simp#define for_each_property_withdel(n, p) \
175204431Sraj	for ((p) = (n)->proplist; (p); (p) = (p)->next)
176204431Sraj
177261215Simp#define for_each_property(n, p) \
178261215Simp	for_each_property_withdel(n, p) \
179261215Simp		if (!(p)->deleted)
180261215Simp
181261215Simp#define for_each_child_withdel(n, c) \
182204431Sraj	for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
183204431Sraj
184261215Simp#define for_each_child(n, c) \
185261215Simp	for_each_child_withdel(n, c) \
186261215Simp		if (!(c)->deleted)
187261215Simp
188238742Simpvoid add_label(struct label **labels, char *label);
189261215Simpvoid delete_labels(struct label **labels);
190238742Simp
191238742Simpstruct property *build_property(char *name, struct data val);
192261215Simpstruct property *build_property_delete(char *name);
193204431Srajstruct property *chain_property(struct property *first, struct property *list);
194204431Srajstruct property *reverse_properties(struct property *first);
195204431Sraj
196204431Srajstruct node *build_node(struct property *proplist, struct node *children);
197261215Simpstruct node *build_node_delete(void);
198238742Simpstruct node *name_node(struct node *node, char *name);
199204431Srajstruct node *chain_node(struct node *first, struct node *list);
200238742Simpstruct node *merge_nodes(struct node *old_node, struct node *new_node);
201204431Sraj
202204431Srajvoid add_property(struct node *node, struct property *prop);
203261215Simpvoid delete_property_by_name(struct node *node, char *name);
204261215Simpvoid delete_property(struct property *prop);
205204431Srajvoid add_child(struct node *parent, struct node *child);
206261215Simpvoid delete_node_by_name(struct node *parent, char *name);
207261215Simpvoid delete_node(struct node *node);
208318102Sgonzovoid append_to_property(struct node *node,
209318102Sgonzo			char *name, const void *data, int len);
210204431Sraj
211204431Srajconst char *get_unitname(struct node *node);
212204431Srajstruct property *get_property(struct node *node, const char *propname);
213204431Srajcell_t propval_cell(struct property *prop);
214238742Simpstruct property *get_property_by_label(struct node *tree, const char *label,
215238742Simp				       struct node **node);
216238742Simpstruct marker *get_marker_label(struct node *tree, const char *label,
217238742Simp				struct node **node, struct property **prop);
218204431Srajstruct node *get_subnode(struct node *node, const char *nodename);
219204431Srajstruct node *get_node_by_path(struct node *tree, const char *path);
220204431Srajstruct node *get_node_by_label(struct node *tree, const char *label);
221204431Srajstruct node *get_node_by_phandle(struct node *tree, cell_t phandle);
222204431Srajstruct node *get_node_by_ref(struct node *tree, const char *ref);
223204431Srajcell_t get_node_phandle(struct node *root, struct node *node);
224204431Sraj
225238742Simpuint32_t guess_boot_cpuid(struct node *tree);
226238742Simp
227204431Sraj/* Boot info (tree plus memreserve information */
228204431Sraj
229204431Srajstruct reserve_info {
230204431Sraj	struct fdt_reserve_entry re;
231204431Sraj
232204431Sraj	struct reserve_info *next;
233204431Sraj
234238742Simp	struct label *labels;
235204431Sraj};
236204431Sraj
237238742Simpstruct reserve_info *build_reserve_entry(uint64_t start, uint64_t len);
238204431Srajstruct reserve_info *chain_reserve_entry(struct reserve_info *first,
239204431Sraj					 struct reserve_info *list);
240204431Srajstruct reserve_info *add_reserve_entry(struct reserve_info *list,
241204431Sraj				       struct reserve_info *new);
242204431Sraj
243204431Sraj
244318102Sgonzostruct dt_info {
245318102Sgonzo	unsigned int dtsflags;
246204431Sraj	struct reserve_info *reservelist;
247318102Sgonzo	uint32_t boot_cpuid_phys;
248204431Sraj	struct node *dt;		/* the device tree */
249318102Sgonzo	const char *outname;		/* filename being written to, "-" for stdout */
250204431Sraj};
251204431Sraj
252318102Sgonzo/* DTS version flags definitions */
253318102Sgonzo#define DTSF_V1		0x0001	/* /dts-v1/ */
254318102Sgonzo#define DTSF_PLUGIN	0x0002	/* /plugin/ */
255204431Sraj
256318102Sgonzostruct dt_info *build_dt_info(unsigned int dtsflags,
257318102Sgonzo			      struct reserve_info *reservelist,
258318102Sgonzo			      struct node *tree, uint32_t boot_cpuid_phys);
259318102Sgonzovoid sort_tree(struct dt_info *dti);
260318102Sgonzovoid generate_label_tree(struct dt_info *dti, char *name, bool allocph);
261318102Sgonzovoid generate_fixups_tree(struct dt_info *dti, char *name);
262318102Sgonzovoid generate_local_fixups_tree(struct dt_info *dti, char *name);
263318102Sgonzo
264204431Sraj/* Checks */
265204431Sraj
266318102Sgonzovoid parse_checks_option(bool warn, bool error, const char *arg);
267318102Sgonzovoid process_checks(bool force, struct dt_info *dti);
268204431Sraj
269204431Sraj/* Flattened trees */
270204431Sraj
271318102Sgonzovoid dt_to_blob(FILE *f, struct dt_info *dti, int version);
272318102Sgonzovoid dt_to_asm(FILE *f, struct dt_info *dti, int version);
273204431Sraj
274318102Sgonzostruct dt_info *dt_from_blob(const char *fname);
275204431Sraj
276204431Sraj/* Tree source */
277204431Sraj
278318102Sgonzovoid dt_to_source(FILE *f, struct dt_info *dti);
279318102Sgonzostruct dt_info *dt_from_source(const char *f);
280204431Sraj
281204431Sraj/* FS trees */
282204431Sraj
283318102Sgonzostruct dt_info *dt_from_fs(const char *dirname);
284204431Sraj
285204431Sraj#endif /* _DTC_H */
286