blob: 07e3ddb73552b2802cc72ddf79902f5335ed8c4d [file] [log] [blame]
David Gibsonfc14dad2005-06-08 17:18:34 +10001#ifndef _DTC_H
2#define _DTC_H
3
4/*
5 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
6 *
David Gibson63dc9c72007-09-18 11:44:04 +10007 *
David Gibsonfc14dad2005-06-08 17:18:34 +10008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
David Gibson63dc9c72007-09-18 11:44:04 +100017 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
David Gibsonfc14dad2005-06-08 17:18:34 +100022 */
23
24#include <stdio.h>
25#include <string.h>
26#include <stdlib.h>
27#include <stdint.h>
David Gibson511dedd2012-07-08 23:25:21 +100028#include <stdbool.h>
David Gibsonfc14dad2005-06-08 17:18:34 +100029#include <stdarg.h>
30#include <assert.h>
31#include <ctype.h>
32#include <errno.h>
33#include <unistd.h>
David Gibson13ce6e12017-06-08 14:35:16 +100034#include <inttypes.h>
David Gibsonf0517db2005-07-15 17:14:24 +100035
David Gibsonc8c374b2008-06-25 14:27:53 +100036#include <libfdt_env.h>
David Gibsonfb7c7ac2007-09-26 13:11:05 +100037#include <fdt.h>
David Gibsonfc14dad2005-06-08 17:18:34 +100038
Jon Loeliger879e4d22008-10-03 11:12:33 -050039#include "util.h"
40
Jon Loeliger2ebe88d2008-09-05 14:25:44 -050041#ifdef DEBUG
Andrei Errapartf9e91a42014-06-19 21:07:48 +100042#define debug(...) printf(__VA_ARGS__)
Jon Loeliger2ebe88d2008-09-05 14:25:44 -050043#else
Andrei Errapartf9e91a42014-06-19 21:07:48 +100044#define debug(...)
Jon Loeliger2ebe88d2008-09-05 14:25:44 -050045#endif
46
David Gibsonfb7c7ac2007-09-26 13:11:05 +100047#define DEFAULT_FDT_VERSION 17
Jon Loeliger879e4d22008-10-03 11:12:33 -050048
Jerry Van Barencd1da872007-03-18 16:49:24 -040049/*
Jerry Van Baren4384b232007-04-04 22:04:33 -040050 * Command line options
Jerry Van Barencd1da872007-03-18 16:49:24 -040051 */
Jerry Van Baren4384b232007-04-04 22:04:33 -040052extern int quiet; /* Level of quietness */
53extern int reservenum; /* Number of memory reservation slots */
54extern int minsize; /* Minimum blob size */
Kumar Gala2b7dc8d2007-11-28 10:21:12 -060055extern int padsize; /* Additional padding to blob */
Tim Wang874f4052016-07-18 15:56:53 +080056extern int alignsize; /* Additional padding to blob accroding to the alignsize */
David Gibsond75b33a2009-11-26 15:37:13 +110057extern int phandle_format; /* Use linux,phandle or phandle properties */
Pantelis Antoniou20f29d82016-12-07 14:48:18 +020058extern int generate_symbols; /* generate symbols for nodes with labels */
59extern int generate_fixups; /* generate fixups */
60extern int auto_label_aliases; /* auto generate labels -> aliases */
David Gibsond75b33a2009-11-26 15:37:13 +110061
62#define PHANDLE_LEGACY 0x1
63#define PHANDLE_EPAPR 0x2
64#define PHANDLE_BOTH 0x3
Jerry Van Barencd1da872007-03-18 16:49:24 -040065
David Gibson53359012008-06-25 13:53:07 +100066typedef uint32_t cell_t;
David Gibsonfc14dad2005-06-08 17:18:34 +100067
David Gibsonfc14dad2005-06-08 17:18:34 +100068
69#define streq(a, b) (strcmp((a), (b)) == 0)
David Gibson81f2e892005-06-16 17:04:00 +100070#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
71
David Gibsonfc14dad2005-06-08 17:18:34 +100072#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
David Gibsonfc14dad2005-06-08 17:18:34 +100073
74/* Data blobs */
David Gibsondc941772007-11-22 14:39:23 +110075enum markertype {
76 REF_PHANDLE,
David Gibsonefbbef82007-12-05 10:43:50 +110077 REF_PATH,
David Gibsondc941772007-11-22 14:39:23 +110078 LABEL,
79};
80
81struct marker {
82 enum markertype type;
David Gibson81f2e892005-06-16 17:04:00 +100083 int offset;
84 char *ref;
David Gibsondc941772007-11-22 14:39:23 +110085 struct marker *next;
David Gibson81f2e892005-06-16 17:04:00 +100086};
87
David Gibsonfc14dad2005-06-08 17:18:34 +100088struct data {
89 int len;
90 char *val;
David Gibsondc941772007-11-22 14:39:23 +110091 struct marker *markers;
David Gibsonfc14dad2005-06-08 17:18:34 +100092};
93
David Gibsondc941772007-11-22 14:39:23 +110094
Andrei Errapartf9e91a42014-06-19 21:07:48 +100095#define empty_data ((struct data){ 0 /* all .members = 0 or NULL */ })
David Gibsonfc14dad2005-06-08 17:18:34 +100096
David Gibsondc941772007-11-22 14:39:23 +110097#define for_each_marker(m) \
98 for (; (m); (m) = (m)->next)
99#define for_each_marker_of_type(m, t) \
100 for_each_marker(m) \
101 if ((m)->type == (t))
102
David Gibsonfc14dad2005-06-08 17:18:34 +1000103void data_free(struct data d);
104
105struct data data_grow_for(struct data d, int xlen);
106
David Gibson92cb9a22007-12-04 14:26:15 +1100107struct data data_copy_mem(const char *mem, int len);
108struct data data_copy_escape_string(const char *s, int len);
David Gibsonfc14dad2005-06-08 17:18:34 +1000109struct data data_copy_file(FILE *f, size_t len);
110
David Gibson92cb9a22007-12-04 14:26:15 +1100111struct data data_append_data(struct data d, const void *p, int len);
David Gibsonefbbef82007-12-05 10:43:50 +1100112struct data data_insert_at_marker(struct data d, struct marker *m,
113 const void *p, int len);
David Gibson32da4752007-02-07 16:37:50 +1100114struct data data_merge(struct data d1, struct data d2);
David Gibsonfc14dad2005-06-08 17:18:34 +1000115struct data data_append_cell(struct data d, cell_t word);
Anton Staafa4b515c2011-10-11 10:22:28 -0700116struct data data_append_integer(struct data d, uint64_t word, int bits);
David Gibson49300f22017-03-06 12:04:45 +1100117struct data data_append_re(struct data d, uint64_t address, uint64_t size);
David Gibson53359012008-06-25 13:53:07 +1000118struct data data_append_addr(struct data d, uint64_t addr);
David Gibsonfc14dad2005-06-08 17:18:34 +1000119struct data data_append_byte(struct data d, uint8_t byte);
120struct data data_append_zeroes(struct data d, int len);
121struct data data_append_align(struct data d, int align);
122
David Gibsondc941772007-11-22 14:39:23 +1100123struct data data_add_marker(struct data d, enum markertype type, char *ref);
David Gibson81f2e892005-06-16 17:04:00 +1000124
David Gibson17625372013-10-28 21:06:53 +1100125bool data_is_one_string(struct data d);
David Gibsonfc14dad2005-06-08 17:18:34 +1000126
127/* DT constraints */
128
129#define MAX_PROPNAME_LEN 31
130#define MAX_NODENAME_LEN 31
131
132/* Live trees */
David Gibson05898c62010-02-24 18:22:17 +1100133struct label {
David Gibson17625372013-10-28 21:06:53 +1100134 bool deleted;
David Gibson05898c62010-02-24 18:22:17 +1100135 char *label;
136 struct label *next;
137};
138
Rob Herring33c39852017-03-20 09:44:16 -0500139struct bus_type {
140 const char *name;
141};
142
David Gibsonfc14dad2005-06-08 17:18:34 +1000143struct property {
David Gibson17625372013-10-28 21:06:53 +1100144 bool deleted;
David Gibsonfc14dad2005-06-08 17:18:34 +1000145 char *name;
146 struct data val;
147
148 struct property *next;
David Gibson4102d842005-06-16 14:36:37 +1000149
David Gibson05898c62010-02-24 18:22:17 +1100150 struct label *labels;
David Gibsonfc14dad2005-06-08 17:18:34 +1000151};
152
153struct node {
David Gibson17625372013-10-28 21:06:53 +1100154 bool deleted;
David Gibsonfc14dad2005-06-08 17:18:34 +1000155 char *name;
156 struct property *proplist;
157 struct node *children;
158
159 struct node *parent;
160 struct node *next_sibling;
161
162 char *fullpath;
163 int basenamelen;
164
165 cell_t phandle;
166 int addr_cells, size_cells;
David Gibson4102d842005-06-16 14:36:37 +1000167
David Gibson05898c62010-02-24 18:22:17 +1100168 struct label *labels;
Rob Herring33c39852017-03-20 09:44:16 -0500169 const struct bus_type *bus;
David Gibsonfc14dad2005-06-08 17:18:34 +1000170};
171
Stephen Warren45013d82012-08-07 22:50:15 -0600172#define for_each_label_withdel(l0, l) \
David Gibson05898c62010-02-24 18:22:17 +1100173 for ((l) = (l0); (l); (l) = (l)->next)
174
Stephen Warren1762ab42012-10-05 09:57:41 -0600175#define for_each_label(l0, l) \
176 for_each_label_withdel(l0, l) \
177 if (!(l)->deleted)
Stephen Warren45013d82012-08-07 22:50:15 -0600178
179#define for_each_property_withdel(n, p) \
David Gibsonfc14dad2005-06-08 17:18:34 +1000180 for ((p) = (n)->proplist; (p); (p) = (p)->next)
181
Stephen Warren1762ab42012-10-05 09:57:41 -0600182#define for_each_property(n, p) \
183 for_each_property_withdel(n, p) \
184 if (!(p)->deleted)
Stephen Warren45013d82012-08-07 22:50:15 -0600185
186#define for_each_child_withdel(n, c) \
David Gibsonfc14dad2005-06-08 17:18:34 +1000187 for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
188
Stephen Warren1762ab42012-10-05 09:57:41 -0600189#define for_each_child(n, c) \
190 for_each_child_withdel(n, c) \
191 if (!(c)->deleted)
192
David Gibson05898c62010-02-24 18:22:17 +1100193void add_label(struct label **labels, char *label);
Stephen Warren45013d82012-08-07 22:50:15 -0600194void delete_labels(struct label **labels);
David Gibson05898c62010-02-24 18:22:17 +1100195
196struct property *build_property(char *name, struct data val);
Stephen Warren45013d82012-08-07 22:50:15 -0600197struct property *build_property_delete(char *name);
David Gibsonfc14dad2005-06-08 17:18:34 +1000198struct property *chain_property(struct property *first, struct property *list);
Jon Loeliger7b3fb782007-10-22 16:09:56 -0500199struct property *reverse_properties(struct property *first);
David Gibsonfc14dad2005-06-08 17:18:34 +1000200
201struct node *build_node(struct property *proplist, struct node *children);
Stephen Warren45013d82012-08-07 22:50:15 -0600202struct node *build_node_delete(void);
David Gibson05898c62010-02-24 18:22:17 +1100203struct node *name_node(struct node *node, char *name);
David Gibsonfc14dad2005-06-08 17:18:34 +1000204struct node *chain_node(struct node *first, struct node *list);
Grant Likely83da1b22010-02-25 09:58:29 -0700205struct node *merge_nodes(struct node *old_node, struct node *new_node);
Pantelis Antoniouec291732016-12-07 12:48:22 +0800206void add_orphan_node(struct node *old_node, struct node *new_node, char *ref);
David Gibsonfc14dad2005-06-08 17:18:34 +1000207
208void add_property(struct node *node, struct property *prop);
Stephen Warren45013d82012-08-07 22:50:15 -0600209void delete_property_by_name(struct node *node, char *name);
210void delete_property(struct property *prop);
David Gibsonfc14dad2005-06-08 17:18:34 +1000211void add_child(struct node *parent, struct node *child);
Stephen Warren45013d82012-08-07 22:50:15 -0600212void delete_node_by_name(struct node *parent, char *name);
213void delete_node(struct node *node);
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200214void append_to_property(struct node *node,
215 char *name, const void *data, int len);
David Gibsonfc14dad2005-06-08 17:18:34 +1000216
David Gibson92cb9a22007-12-04 14:26:15 +1100217const char *get_unitname(struct node *node);
218struct property *get_property(struct node *node, const char *propname);
David Gibson2f1ccc32007-11-01 16:49:26 +1100219cell_t propval_cell(struct property *prop);
David Gibson329055d2010-02-23 19:56:41 +1100220struct property *get_property_by_label(struct node *tree, const char *label,
221 struct node **node);
222struct marker *get_marker_label(struct node *tree, const char *label,
223 struct node **node, struct property **prop);
David Gibson92cb9a22007-12-04 14:26:15 +1100224struct node *get_subnode(struct node *node, const char *nodename);
225struct node *get_node_by_path(struct node *tree, const char *path);
David Gibsonb16a2bd2007-11-22 14:38:07 +1100226struct node *get_node_by_label(struct node *tree, const char *label);
David Gibson2f1ccc32007-11-01 16:49:26 +1100227struct node *get_node_by_phandle(struct node *tree, cell_t phandle);
David Gibson92cb9a22007-12-04 14:26:15 +1100228struct node *get_node_by_ref(struct node *tree, const char *ref);
David Gibsonb16a2bd2007-11-22 14:38:07 +1100229cell_t get_node_phandle(struct node *root, struct node *node);
David Gibsonfc14dad2005-06-08 17:18:34 +1000230
David Gibson15ad6d82010-02-19 15:50:50 +1100231uint32_t guess_boot_cpuid(struct node *tree);
232
David Gibsonf0517db2005-07-15 17:14:24 +1000233/* Boot info (tree plus memreserve information */
234
David Gibsonf040d952005-10-24 18:18:38 +1000235struct reserve_info {
David Gibson49300f22017-03-06 12:04:45 +1100236 uint64_t address, size;
David Gibsonf040d952005-10-24 18:18:38 +1000237
238 struct reserve_info *next;
239
David Gibson05898c62010-02-24 18:22:17 +1100240 struct label *labels;
David Gibsonf040d952005-10-24 18:18:38 +1000241};
242
David Gibson05898c62010-02-24 18:22:17 +1100243struct reserve_info *build_reserve_entry(uint64_t start, uint64_t len);
David Gibsonf040d952005-10-24 18:18:38 +1000244struct reserve_info *chain_reserve_entry(struct reserve_info *first,
245 struct reserve_info *list);
246struct reserve_info *add_reserve_entry(struct reserve_info *list,
247 struct reserve_info *new);
248
249
David Gibson00fbb862016-05-31 11:58:42 +1000250struct dt_info {
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200251 unsigned int dtsflags;
David Gibsonf040d952005-10-24 18:18:38 +1000252 struct reserve_info *reservelist;
David Gibson53359012008-06-25 13:53:07 +1000253 uint32_t boot_cpuid_phys;
David Gibson00fbb862016-05-31 11:58:42 +1000254 struct node *dt; /* the device tree */
Ian Campbellacd1b532017-02-03 08:29:39 +0000255 const char *outname; /* filename being written to, "-" for stdout */
David Gibsonf0517db2005-07-15 17:14:24 +1000256};
257
Pantelis Antoniou20f29d82016-12-07 14:48:18 +0200258/* DTS version flags definitions */
259#define DTSF_V1 0x0001 /* /dts-v1/ */
260#define DTSF_PLUGIN 0x0002 /* /plugin/ */
261
David Gibson00fbb862016-05-31 11:58:42 +1000262struct dt_info *build_dt_info(unsigned int dtsflags,
263 struct reserve_info *reservelist,
264 struct node *tree, uint32_t boot_cpuid_phys);
265void sort_tree(struct dt_info *dti);
266void generate_label_tree(struct dt_info *dti, char *name, bool allocph);
267void generate_fixups_tree(struct dt_info *dti, char *name);
268void generate_local_fixups_tree(struct dt_info *dti, char *name);
David Gibsonf0517db2005-07-15 17:14:24 +1000269
David Gibson2f1ccc32007-11-01 16:49:26 +1100270/* Checks */
271
Florian Fainelli24cb3d02014-02-01 16:41:59 +1100272void parse_checks_option(bool warn, bool error, const char *arg);
David Gibson00fbb862016-05-31 11:58:42 +1000273void process_checks(bool force, struct dt_info *dti);
David Gibson2f1ccc32007-11-01 16:49:26 +1100274
David Gibsonfc14dad2005-06-08 17:18:34 +1000275/* Flattened trees */
276
David Gibson00fbb862016-05-31 11:58:42 +1000277void dt_to_blob(FILE *f, struct dt_info *dti, int version);
278void dt_to_asm(FILE *f, struct dt_info *dti, int version);
David Gibsonfc14dad2005-06-08 17:18:34 +1000279
David Gibson00fbb862016-05-31 11:58:42 +1000280struct dt_info *dt_from_blob(const char *fname);
David Gibsonfc14dad2005-06-08 17:18:34 +1000281
282/* Tree source */
283
David Gibson00fbb862016-05-31 11:58:42 +1000284void dt_to_source(FILE *f, struct dt_info *dti);
285struct dt_info *dt_from_source(const char *f);
David Gibsonfc14dad2005-06-08 17:18:34 +1000286
287/* FS trees */
288
David Gibson00fbb862016-05-31 11:58:42 +1000289struct dt_info *dt_from_fs(const char *dirname);
David Gibsonfc14dad2005-06-08 17:18:34 +1000290
David Gibsonfc14dad2005-06-08 17:18:34 +1000291#endif /* _DTC_H */