dtc.h revision 261215
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
41204433Sraj#define debug(fmt,args...)	printf(fmt, ##args)
42204433Sraj#else
43204433Sraj#define debug(fmt,args...)
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 */
56204433Srajextern int phandle_format;	/* Use linux,phandle or phandle properties */
57204431Sraj
58204433Sraj#define PHANDLE_LEGACY	0x1
59204433Sraj#define PHANDLE_EPAPR	0x2
60204433Sraj#define PHANDLE_BOTH	0x3
61204431Sraj
62204431Srajtypedef uint32_t cell_t;
63204431Sraj
64204431Sraj
65204431Sraj#define streq(a, b)	(strcmp((a), (b)) == 0)
66204431Sraj#define strneq(a, b, n)	(strncmp((a), (b), (n)) == 0)
67204431Sraj
68204431Sraj#define ALIGN(x, a)	(((x) + (a) - 1) & ~((a) - 1))
69204431Sraj
70204431Sraj/* Data blobs */
71204431Srajenum markertype {
72204431Sraj	REF_PHANDLE,
73204431Sraj	REF_PATH,
74204431Sraj	LABEL,
75204431Sraj};
76204431Sraj
77204431Srajstruct  marker {
78204431Sraj	enum markertype type;
79204431Sraj	int offset;
80204431Sraj	char *ref;
81204431Sraj	struct marker *next;
82204431Sraj};
83204431Sraj
84204431Srajstruct data {
85204431Sraj	int len;
86204431Sraj	char *val;
87204431Sraj	struct marker *markers;
88204431Sraj};
89204431Sraj
90204431Sraj
91204431Sraj#define empty_data ((struct data){ /* all .members = 0 or NULL */ })
92204431Sraj
93204431Sraj#define for_each_marker(m) \
94204431Sraj	for (; (m); (m) = (m)->next)
95204431Sraj#define for_each_marker_of_type(m, t) \
96204431Sraj	for_each_marker(m) \
97204431Sraj		if ((m)->type == (t))
98204431Sraj
99204431Srajvoid data_free(struct data d);
100204431Sraj
101204431Srajstruct data data_grow_for(struct data d, int xlen);
102204431Sraj
103204431Srajstruct data data_copy_mem(const char *mem, int len);
104204431Srajstruct data data_copy_escape_string(const char *s, int len);
105204431Srajstruct data data_copy_file(FILE *f, size_t len);
106204431Sraj
107204431Srajstruct data data_append_data(struct data d, const void *p, int len);
108204431Srajstruct data data_insert_at_marker(struct data d, struct marker *m,
109204431Sraj				  const void *p, int len);
110204431Srajstruct data data_merge(struct data d1, struct data d2);
111204431Srajstruct data data_append_cell(struct data d, cell_t word);
112238742Simpstruct data data_append_integer(struct data d, uint64_t word, int bits);
113204431Srajstruct data data_append_re(struct data d, const struct fdt_reserve_entry *re);
114204431Srajstruct data data_append_addr(struct data d, uint64_t addr);
115204431Srajstruct data data_append_byte(struct data d, uint8_t byte);
116204431Srajstruct data data_append_zeroes(struct data d, int len);
117204431Srajstruct data data_append_align(struct data d, int align);
118204431Sraj
119204431Srajstruct data data_add_marker(struct data d, enum markertype type, char *ref);
120204431Sraj
121261215Simpbool data_is_one_string(struct data d);
122204431Sraj
123204431Sraj/* DT constraints */
124204431Sraj
125204431Sraj#define MAX_PROPNAME_LEN	31
126204431Sraj#define MAX_NODENAME_LEN	31
127204431Sraj
128204431Sraj/* Live trees */
129238742Simpstruct label {
130261215Simp	bool deleted;
131238742Simp	char *label;
132238742Simp	struct label *next;
133238742Simp};
134238742Simp
135204431Srajstruct property {
136261215Simp	bool deleted;
137204431Sraj	char *name;
138204431Sraj	struct data val;
139204431Sraj
140204431Sraj	struct property *next;
141204431Sraj
142238742Simp	struct label *labels;
143204431Sraj};
144204431Sraj
145204431Srajstruct node {
146261215Simp	bool deleted;
147204431Sraj	char *name;
148204431Sraj	struct property *proplist;
149204431Sraj	struct node *children;
150204431Sraj
151204431Sraj	struct node *parent;
152204431Sraj	struct node *next_sibling;
153204431Sraj
154204431Sraj	char *fullpath;
155204431Sraj	int basenamelen;
156204431Sraj
157204431Sraj	cell_t phandle;
158204431Sraj	int addr_cells, size_cells;
159204431Sraj
160238742Simp	struct label *labels;
161204431Sraj};
162204431Sraj
163261215Simp#define for_each_label_withdel(l0, l) \
164238742Simp	for ((l) = (l0); (l); (l) = (l)->next)
165238742Simp
166261215Simp#define for_each_label(l0, l) \
167261215Simp	for_each_label_withdel(l0, l) \
168261215Simp		if (!(l)->deleted)
169261215Simp
170261215Simp#define for_each_property_withdel(n, p) \
171204431Sraj	for ((p) = (n)->proplist; (p); (p) = (p)->next)
172204431Sraj
173261215Simp#define for_each_property(n, p) \
174261215Simp	for_each_property_withdel(n, p) \
175261215Simp		if (!(p)->deleted)
176261215Simp
177261215Simp#define for_each_child_withdel(n, c) \
178204431Sraj	for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
179204431Sraj
180261215Simp#define for_each_child(n, c) \
181261215Simp	for_each_child_withdel(n, c) \
182261215Simp		if (!(c)->deleted)
183261215Simp
184238742Simpvoid add_label(struct label **labels, char *label);
185261215Simpvoid delete_labels(struct label **labels);
186238742Simp
187238742Simpstruct property *build_property(char *name, struct data val);
188261215Simpstruct property *build_property_delete(char *name);
189204431Srajstruct property *chain_property(struct property *first, struct property *list);
190204431Srajstruct property *reverse_properties(struct property *first);
191204431Sraj
192204431Srajstruct node *build_node(struct property *proplist, struct node *children);
193261215Simpstruct node *build_node_delete(void);
194238742Simpstruct node *name_node(struct node *node, char *name);
195204431Srajstruct node *chain_node(struct node *first, struct node *list);
196238742Simpstruct node *merge_nodes(struct node *old_node, struct node *new_node);
197204431Sraj
198204431Srajvoid add_property(struct node *node, struct property *prop);
199261215Simpvoid delete_property_by_name(struct node *node, char *name);
200261215Simpvoid delete_property(struct property *prop);
201204431Srajvoid add_child(struct node *parent, struct node *child);
202261215Simpvoid delete_node_by_name(struct node *parent, char *name);
203261215Simpvoid delete_node(struct node *node);
204204431Sraj
205204431Srajconst char *get_unitname(struct node *node);
206204431Srajstruct property *get_property(struct node *node, const char *propname);
207204431Srajcell_t propval_cell(struct property *prop);
208238742Simpstruct property *get_property_by_label(struct node *tree, const char *label,
209238742Simp				       struct node **node);
210238742Simpstruct marker *get_marker_label(struct node *tree, const char *label,
211238742Simp				struct node **node, struct property **prop);
212204431Srajstruct node *get_subnode(struct node *node, const char *nodename);
213204431Srajstruct node *get_node_by_path(struct node *tree, const char *path);
214204431Srajstruct node *get_node_by_label(struct node *tree, const char *label);
215204431Srajstruct node *get_node_by_phandle(struct node *tree, cell_t phandle);
216204431Srajstruct node *get_node_by_ref(struct node *tree, const char *ref);
217204431Srajcell_t get_node_phandle(struct node *root, struct node *node);
218204431Sraj
219238742Simpuint32_t guess_boot_cpuid(struct node *tree);
220238742Simp
221204431Sraj/* Boot info (tree plus memreserve information */
222204431Sraj
223204431Srajstruct reserve_info {
224204431Sraj	struct fdt_reserve_entry re;
225204431Sraj
226204431Sraj	struct reserve_info *next;
227204431Sraj
228238742Simp	struct label *labels;
229204431Sraj};
230204431Sraj
231238742Simpstruct reserve_info *build_reserve_entry(uint64_t start, uint64_t len);
232204431Srajstruct reserve_info *chain_reserve_entry(struct reserve_info *first,
233204431Sraj					 struct reserve_info *list);
234204431Srajstruct reserve_info *add_reserve_entry(struct reserve_info *list,
235204431Sraj				       struct reserve_info *new);
236204431Sraj
237204431Sraj
238204431Srajstruct boot_info {
239204431Sraj	struct reserve_info *reservelist;
240204431Sraj	struct node *dt;		/* the device tree */
241204431Sraj	uint32_t boot_cpuid_phys;
242204431Sraj};
243204431Sraj
244204431Srajstruct boot_info *build_boot_info(struct reserve_info *reservelist,
245204431Sraj				  struct node *tree, uint32_t boot_cpuid_phys);
246238742Simpvoid sort_tree(struct boot_info *bi);
247204431Sraj
248204431Sraj/* Checks */
249204431Sraj
250238742Simpvoid parse_checks_option(bool warn, bool error, const char *optarg);
251261215Simpvoid process_checks(bool force, struct boot_info *bi);
252204431Sraj
253204431Sraj/* Flattened trees */
254204431Sraj
255204431Srajvoid dt_to_blob(FILE *f, struct boot_info *bi, int version);
256204431Srajvoid dt_to_asm(FILE *f, struct boot_info *bi, int version);
257204431Sraj
258204431Srajstruct boot_info *dt_from_blob(const char *fname);
259204431Sraj
260204431Sraj/* Tree source */
261204431Sraj
262204431Srajvoid dt_to_source(FILE *f, struct boot_info *bi);
263204431Srajstruct boot_info *dt_from_source(const char *f);
264204431Sraj
265204431Sraj/* FS trees */
266204431Sraj
267204431Srajstruct boot_info *dt_from_fs(const char *dirname);
268204431Sraj
269204431Sraj#endif /* _DTC_H */
270