blob: 213e5534d4bfe45354416163185e91d87cfb3411 [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
37#include "flat_dt.h"
David Gibsonfc14dad2005-06-08 17:18:34 +100038
Jerry Van Barencd1da872007-03-18 16:49:24 -040039/*
Jerry Van Baren4384b232007-04-04 22:04:33 -040040 * Command line options
Jerry Van Barencd1da872007-03-18 16:49:24 -040041 */
Jerry Van Baren4384b232007-04-04 22:04:33 -040042extern int quiet; /* Level of quietness */
43extern int reservenum; /* Number of memory reservation slots */
44extern int minsize; /* Minimum blob size */
Jerry Van Barencd1da872007-03-18 16:49:24 -040045
David Gibsonbf944972007-08-31 16:21:23 +100046static inline void __attribute__((noreturn)) die(char * str, ...)
David Gibsonfc14dad2005-06-08 17:18:34 +100047{
48 va_list ap;
49
50 va_start(ap, str);
51 fprintf(stderr, "FATAL ERROR: ");
52 vfprintf(stderr, str, ap);
53 exit(1);
54}
55
56static inline void *xmalloc(size_t len)
57{
58 void *new = malloc(len);
59
60 if (! new)
61 die("malloc() failed\n");
62
63 return new;
64}
65
66static inline void *xrealloc(void *p, size_t len)
67{
68 void *new = realloc(p, len);
69
70 if (! new)
71 die("realloc() failed (len=%d)\n", len);
72
73 return new;
74}
75
David Gibson03a9b9d2005-07-11 16:49:52 +100076typedef uint8_t u8;
David Gibsonfc14dad2005-06-08 17:18:34 +100077typedef uint16_t u16;
78typedef uint32_t u32;
79typedef uint64_t u64;
80typedef u32 cell_t;
81
82#define cpu_to_be16(x) htons(x)
83#define be16_to_cpu(x) ntohs(x)
84
85#define cpu_to_be32(x) htonl(x)
86#define be32_to_cpu(x) ntohl(x)
87
David Gibsonf0517db2005-07-15 17:14:24 +100088#if __BYTE_ORDER == __BIG_ENDIAN
89#define cpu_to_be64(x) (x)
90#define be64_to_cpu(x) (x)
91#else
92#define cpu_to_be64(x) bswap_64(x)
93#define be64_to_cpu(x) bswap_64(x)
94#endif
David Gibsonfc14dad2005-06-08 17:18:34 +100095
96#define streq(a, b) (strcmp((a), (b)) == 0)
David Gibson81f2e892005-06-16 17:04:00 +100097#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
98
David Gibsonfc14dad2005-06-08 17:18:34 +100099#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
100#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
101
102/* Data blobs */
David Gibson81f2e892005-06-16 17:04:00 +1000103struct fixup {
104 int offset;
105 char *ref;
106 struct fixup *next;
107};
108
David Gibsonfc14dad2005-06-08 17:18:34 +1000109struct data {
110 int len;
111 char *val;
112 int asize;
David Gibson81f2e892005-06-16 17:04:00 +1000113 struct fixup *refs;
Milton Miller6a99b132007-07-07 01:18:51 -0500114 struct fixup *labels;
David Gibsonfc14dad2005-06-08 17:18:34 +1000115};
116
Milton Miller6a99b132007-07-07 01:18:51 -0500117#define empty_data ((struct data){ /* all .members = 0 or NULL */ })
David Gibsonfc14dad2005-06-08 17:18:34 +1000118
David Gibson81f2e892005-06-16 17:04:00 +1000119void fixup_free(struct fixup *f);
David Gibsonfc14dad2005-06-08 17:18:34 +1000120void data_free(struct data d);
121
122struct data data_grow_for(struct data d, int xlen);
123
124struct data data_copy_mem(char *mem, int len);
125struct data data_copy_escape_string(char *s, int len);
126struct data data_copy_file(FILE *f, size_t len);
127
128struct data data_append_data(struct data d, void *p, int len);
David Gibson32da4752007-02-07 16:37:50 +1100129struct data data_merge(struct data d1, struct data d2);
David Gibsonfc14dad2005-06-08 17:18:34 +1000130struct data data_append_cell(struct data d, cell_t word);
David Gibsonf040d952005-10-24 18:18:38 +1000131struct data data_append_re(struct data d, struct reserve_entry *re);
David Gibsonf0517db2005-07-15 17:14:24 +1000132struct data data_append_addr(struct data d, u64 addr);
David Gibsonfc14dad2005-06-08 17:18:34 +1000133struct data data_append_byte(struct data d, uint8_t byte);
134struct data data_append_zeroes(struct data d, int len);
135struct data data_append_align(struct data d, int align);
136
David Gibson81f2e892005-06-16 17:04:00 +1000137struct data data_add_fixup(struct data d, char *ref);
Milton Miller6a99b132007-07-07 01:18:51 -0500138struct data data_add_label(struct data d, char *label);
David Gibson81f2e892005-06-16 17:04:00 +1000139
David Gibsonfc14dad2005-06-08 17:18:34 +1000140int data_is_one_string(struct data d);
141
142/* DT constraints */
143
144#define MAX_PROPNAME_LEN 31
145#define MAX_NODENAME_LEN 31
146
147/* Live trees */
148struct property {
149 char *name;
150 struct data val;
151
152 struct property *next;
David Gibson4102d842005-06-16 14:36:37 +1000153
154 char *label;
David Gibsonfc14dad2005-06-08 17:18:34 +1000155};
156
157struct node {
158 char *name;
159 struct property *proplist;
160 struct node *children;
161
162 struct node *parent;
163 struct node *next_sibling;
164
165 char *fullpath;
166 int basenamelen;
167
168 cell_t phandle;
169 int addr_cells, size_cells;
David Gibson4102d842005-06-16 14:36:37 +1000170
171 char *label;
David Gibsonfc14dad2005-06-08 17:18:34 +1000172};
173
174#define for_each_property(n, p) \
175 for ((p) = (n)->proplist; (p); (p) = (p)->next)
176
177#define for_each_child(n, c) \
178 for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
179
David Gibson4102d842005-06-16 14:36:37 +1000180struct property *build_property(char *name, struct data val, char *label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000181struct property *chain_property(struct property *first, struct property *list);
182
183struct node *build_node(struct property *proplist, struct node *children);
David Gibson4102d842005-06-16 14:36:37 +1000184struct node *name_node(struct node *node, char *name, char *label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000185struct node *chain_node(struct node *first, struct node *list);
186
187void add_property(struct node *node, struct property *prop);
188void add_child(struct node *parent, struct node *child);
189
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000190int check_device_tree(struct node *dt, int outversion, int boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000191
David Gibsonf0517db2005-07-15 17:14:24 +1000192/* Boot info (tree plus memreserve information */
193
David Gibsonf040d952005-10-24 18:18:38 +1000194struct reserve_info {
195 struct reserve_entry re;
196
197 struct reserve_info *next;
198
199 char *label;
200};
201
202struct reserve_info *build_reserve_entry(u64 start, u64 len, char *label);
203struct reserve_info *chain_reserve_entry(struct reserve_info *first,
204 struct reserve_info *list);
205struct reserve_info *add_reserve_entry(struct reserve_info *list,
206 struct reserve_info *new);
207
208
David Gibsonf0517db2005-07-15 17:14:24 +1000209struct boot_info {
David Gibsonf040d952005-10-24 18:18:38 +1000210 struct reserve_info *reservelist;
David Gibsonf0517db2005-07-15 17:14:24 +1000211 struct node *dt; /* the device tree */
212};
213
David Gibsonf040d952005-10-24 18:18:38 +1000214struct boot_info *build_boot_info(struct reserve_info *reservelist,
David Gibsonf0517db2005-07-15 17:14:24 +1000215 struct node *tree);
216
David Gibsonfc14dad2005-06-08 17:18:34 +1000217/* Flattened trees */
218
Michael Neuling38e8f8f2006-05-31 08:31:51 +1000219void dt_to_blob(FILE *f, struct boot_info *bi, int version,
220 int boot_cpuid_phys);
221void dt_to_asm(FILE *f, struct boot_info *bi, int version,
222 int boot_cpuid_phys);
David Gibsonfc14dad2005-06-08 17:18:34 +1000223
David Gibsonf0517db2005-07-15 17:14:24 +1000224struct boot_info *dt_from_blob(FILE *f);
David Gibsonfc14dad2005-06-08 17:18:34 +1000225
226/* Tree source */
227
David Gibson712e52e2005-10-26 16:56:26 +1000228void dt_to_source(FILE *f, struct boot_info *bi);
Jon Loeligere45e6fd2007-03-23 15:18:41 -0500229struct boot_info *dt_from_source(const char *f);
David Gibsonfc14dad2005-06-08 17:18:34 +1000230
231/* FS trees */
232
David Gibsonf0517db2005-07-15 17:14:24 +1000233struct boot_info *dt_from_fs(char *dirname);
David Gibsonfc14dad2005-06-08 17:18:34 +1000234
235/* misc */
236
237char *join_path(char *path, char *name);
238void fill_fullpaths(struct node *tree, char *prefix);
239
240#endif /* _DTC_H */