blob: ad21e0d5070d415b101ca57008205cf56b7442cf [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>
28#include <stdarg.h>
29#include <assert.h>
30#include <ctype.h>
31#include <errno.h>
32#include <unistd.h>
33#include <netinet/in.h>
David Gibsonf0517db2005-07-15 17:14:24 +100034#include <endian.h>
35#include <byteswap.h>
36
David Gibsonfb7c7ac2007-09-26 13:11:05 +100037#include <fdt.h>
David Gibsonfc14dad2005-06-08 17:18:34 +100038
David Gibsonfb7c7ac2007-09-26 13:11:05 +100039#define DEFAULT_FDT_VERSION 17
Jerry Van Barencd1da872007-03-18 16:49:24 -040040/*
Jerry Van Baren4384b232007-04-04 22:04:33 -040041 * Command line options
Jerry Van Barencd1da872007-03-18 16:49:24 -040042 */
Jerry Van Baren4384b232007-04-04 22:04:33 -040043extern int quiet; /* Level of quietness */
44extern int reservenum; /* Number of memory reservation slots */
45extern int minsize; /* Minimum blob size */
Jerry Van Barencd1da872007-03-18 16:49:24 -040046
David Gibsonbf944972007-08-31 16:21:23 +100047static inline void __attribute__((noreturn)) die(char * str, ...)
David Gibsonfc14dad2005-06-08 17:18:34 +100048{
49 va_list ap;
50
51 va_start(ap, str);
52 fprintf(stderr, "FATAL ERROR: ");
53 vfprintf(stderr, str, ap);
54 exit(1);
55}
56
57static inline void *xmalloc(size_t len)
58{
59 void *new = malloc(len);
60
61 if (! new)
62 die("malloc() failed\n");
63
64 return new;
65}
66
67static inline void *xrealloc(void *p, size_t len)
68{
69 void *new = realloc(p, len);
70
71 if (! new)
72 die("realloc() failed (len=%d)\n", len);
73
74 return new;
75}
76
David Gibson03a9b9d2005-07-11 16:49:52 +100077typedef uint8_t u8;
David Gibsonfc14dad2005-06-08 17:18:34 +100078typedef uint16_t u16;
79typedef uint32_t u32;
80typedef uint64_t u64;
81typedef u32 cell_t;
82
83#define cpu_to_be16(x) htons(x)
84#define be16_to_cpu(x) ntohs(x)
85
86#define cpu_to_be32(x) htonl(x)
87#define be32_to_cpu(x) ntohl(x)
88
David Gibsonf0517db2005-07-15 17:14:24 +100089#if __BYTE_ORDER == __BIG_ENDIAN
90#define cpu_to_be64(x) (x)
91#define be64_to_cpu(x) (x)
92#else
93#define cpu_to_be64(x) bswap_64(x)
94#define be64_to_cpu(x) bswap_64(x)
95#endif
David Gibsonfc14dad2005-06-08 17:18:34 +100096
97#define streq(a, b) (strcmp((a), (b)) == 0)
David Gibson81f2e892005-06-16 17:04:00 +100098#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
99
David Gibsonfc14dad2005-06-08 17:18:34 +1000100#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
101#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
102
103/* Data blobs */
David Gibsondc941772007-11-22 14:39:23 +1100104enum markertype {
105 REF_PHANDLE,
106 LABEL,
107};
108
109struct marker {
110 enum markertype type;
David Gibson81f2e892005-06-16 17:04:00 +1000111 int offset;
112 char *ref;
David Gibsondc941772007-11-22 14:39:23 +1100113 struct marker *next;
David Gibson81f2e892005-06-16 17:04:00 +1000114};
115
David Gibsonfc14dad2005-06-08 17:18:34 +1000116struct data {
117 int len;
118 char *val;
119 int asize;
David Gibsondc941772007-11-22 14:39:23 +1100120 struct marker *markers;
David Gibsonfc14dad2005-06-08 17:18:34 +1000121};
122
David Gibsondc941772007-11-22 14:39:23 +1100123
Milton Miller6a99b132007-07-07 01:18:51 -0500124#define empty_data ((struct data){ /* all .members = 0 or NULL */ })
David Gibsonfc14dad2005-06-08 17:18:34 +1000125
David Gibsondc941772007-11-22 14:39:23 +1100126#define for_each_marker(m) \
127 for (; (m); (m) = (m)->next)
128#define for_each_marker_of_type(m, t) \
129 for_each_marker(m) \
130 if ((m)->type == (t))
131
David Gibsonfc14dad2005-06-08 17:18:34 +1000132void data_free(struct data d);
133
134struct data data_grow_for(struct data d, int xlen);
135
136struct data data_copy_mem(char *mem, int len);
137struct data data_copy_escape_string(char *s, int len);
138struct data data_copy_file(FILE *f, size_t len);
139
140struct data data_append_data(struct data d, void *p, int len);
David Gibson32da4752007-02-07 16:37:50 +1100141struct data data_merge(struct data d1, struct data d2);
David Gibsonfc14dad2005-06-08 17:18:34 +1000142struct data data_append_cell(struct data d, cell_t word);
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000143struct data data_append_re(struct data d, struct fdt_reserve_entry *re);
David Gibsonf0517db2005-07-15 17:14:24 +1000144struct data data_append_addr(struct data d, u64 addr);
David Gibsonfc14dad2005-06-08 17:18:34 +1000145struct data data_append_byte(struct data d, uint8_t byte);
146struct data data_append_zeroes(struct data d, int len);
147struct data data_append_align(struct data d, int align);
148
David Gibsondc941772007-11-22 14:39:23 +1100149struct data data_add_marker(struct data d, enum markertype type, char *ref);
David Gibson81f2e892005-06-16 17:04:00 +1000150
David Gibsonfc14dad2005-06-08 17:18:34 +1000151int data_is_one_string(struct data d);
152
153/* DT constraints */
154
155#define MAX_PROPNAME_LEN 31
156#define MAX_NODENAME_LEN 31
157
158/* Live trees */
159struct property {
160 char *name;
161 struct data val;
162
163 struct property *next;
David Gibson4102d842005-06-16 14:36:37 +1000164
165 char *label;
David Gibsonfc14dad2005-06-08 17:18:34 +1000166};
167
168struct node {
169 char *name;
170 struct property *proplist;
171 struct node *children;
172
173 struct node *parent;
174 struct node *next_sibling;
175
176 char *fullpath;
177 int basenamelen;
178
179 cell_t phandle;
180 int addr_cells, size_cells;
David Gibson4102d842005-06-16 14:36:37 +1000181
182 char *label;
David Gibsonfc14dad2005-06-08 17:18:34 +1000183};
184
185#define for_each_property(n, p) \
186 for ((p) = (n)->proplist; (p); (p) = (p)->next)
187
188#define for_each_child(n, c) \
189 for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
190
David Gibson4102d842005-06-16 14:36:37 +1000191struct property *build_property(char *name, struct data val, char *label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000192struct property *chain_property(struct property *first, struct property *list);
Jon Loeliger7b3fb782007-10-22 16:09:56 -0500193struct property *reverse_properties(struct property *first);
David Gibsonfc14dad2005-06-08 17:18:34 +1000194
195struct node *build_node(struct property *proplist, struct node *children);
David Gibson4102d842005-06-16 14:36:37 +1000196struct node *name_node(struct node *node, char *name, char *label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000197struct node *chain_node(struct node *first, struct node *list);
198
199void add_property(struct node *node, struct property *prop);
200void add_child(struct node *parent, struct node *child);
201
David Gibson2f1ccc32007-11-01 16:49:26 +1100202char *get_unitname(struct node *node);
203struct property *get_property(struct node *node, char *propname);
204cell_t propval_cell(struct property *prop);
205struct node *get_subnode(struct node *node, char *nodename);
David Gibsonb16a2bd2007-11-22 14:38:07 +1100206struct node *get_node_by_path(struct node *tree, char *path);
207struct node *get_node_by_label(struct node *tree, const char *label);
David Gibson2f1ccc32007-11-01 16:49:26 +1100208struct node *get_node_by_phandle(struct node *tree, cell_t phandle);
David Gibsonb16a2bd2007-11-22 14:38:07 +1100209struct node *get_node_by_ref(struct node *tree, char *ref);
210cell_t get_node_phandle(struct node *root, struct node *node);
David Gibsonfc14dad2005-06-08 17:18:34 +1000211
David Gibsonf0517db2005-07-15 17:14:24 +1000212/* Boot info (tree plus memreserve information */
213
David Gibsonf040d952005-10-24 18:18:38 +1000214struct reserve_info {
David Gibsonfb7c7ac2007-09-26 13:11:05 +1000215 struct fdt_reserve_entry re;
David Gibsonf040d952005-10-24 18:18:38 +1000216
217 struct reserve_info *next;
218
219 char *label;
220};
221
222struct reserve_info *build_reserve_entry(u64 start, u64 len, char *label);
223struct reserve_info *chain_reserve_entry(struct reserve_info *first,
224 struct reserve_info *list);
225struct reserve_info *add_reserve_entry(struct reserve_info *list,
226 struct reserve_info *new);
227
228
David Gibsonf0517db2005-07-15 17:14:24 +1000229struct boot_info {
David Gibsonf040d952005-10-24 18:18:38 +1000230 struct reserve_info *reservelist;
David Gibsonf0517db2005-07-15 17:14:24 +1000231 struct node *dt; /* the device tree */
232};
233
David Gibsonf040d952005-10-24 18:18:38 +1000234struct boot_info *build_boot_info(struct reserve_info *reservelist,
David Gibsonf0517db2005-07-15 17:14:24 +1000235 struct node *tree);
236
David Gibson2f1ccc32007-11-01 16:49:26 +1100237/* Checks */
238
David Gibsonb16a2bd2007-11-22 14:38:07 +1100239void process_checks(int force, struct node *dt);
David Gibson2f1ccc32007-11-01 16:49:26 +1100240int check_semantics(struct node *dt, int outversion, int boot_cpuid_phys);
241
David Gibsonfc14dad2005-06-08 17:18:34 +1000242/* Flattened trees */
243
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000244void dt_to_blob(FILE *f, struct boot_info *bi, int version,
245 int boot_cpuid_phys);
246void dt_to_asm(FILE *f, struct boot_info *bi, int version,
247 int boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000248
David Gibsonf0517db2005-07-15 17:14:24 +1000249struct boot_info *dt_from_blob(FILE *f);
David Gibsonfc14dad2005-06-08 17:18:34 +1000250
251/* Tree source */
252
David Gibson712e52e2005-10-26 16:56:26 +1000253void dt_to_source(FILE *f, struct boot_info *bi);
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500254struct boot_info *dt_from_source(const char *f);
David Gibsonfc14dad2005-06-08 17:18:34 +1000255
256/* FS trees */
257
David Gibsonf0517db2005-07-15 17:14:24 +1000258struct boot_info *dt_from_fs(char *dirname);
David Gibsonfc14dad2005-06-08 17:18:34 +1000259
260/* misc */
261
262char *join_path(char *path, char *name);
263void fill_fullpaths(struct node *tree, char *prefix);
264
265#endif /* _DTC_H */