blob: be17ad69f4278bbe82ef96be467fb89acb86ba83 [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 *
7 *
8 * 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.
17 *
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
22 */
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
39static inline void die(char * str, ...)
40{
41 va_list ap;
42
43 va_start(ap, str);
44 fprintf(stderr, "FATAL ERROR: ");
45 vfprintf(stderr, str, ap);
46 exit(1);
47}
48
49static inline void *xmalloc(size_t len)
50{
51 void *new = malloc(len);
52
53 if (! new)
54 die("malloc() failed\n");
55
56 return new;
57}
58
59static inline void *xrealloc(void *p, size_t len)
60{
61 void *new = realloc(p, len);
62
63 if (! new)
64 die("realloc() failed (len=%d)\n", len);
65
66 return new;
67}
68
David Gibson03a9b9d2005-07-11 16:49:52 +100069typedef uint8_t u8;
David Gibsonfc14dad2005-06-08 17:18:34 +100070typedef uint16_t u16;
71typedef uint32_t u32;
72typedef uint64_t u64;
73typedef u32 cell_t;
74
75#define cpu_to_be16(x) htons(x)
76#define be16_to_cpu(x) ntohs(x)
77
78#define cpu_to_be32(x) htonl(x)
79#define be32_to_cpu(x) ntohl(x)
80
David Gibsonf0517db2005-07-15 17:14:24 +100081#if __BYTE_ORDER == __BIG_ENDIAN
82#define cpu_to_be64(x) (x)
83#define be64_to_cpu(x) (x)
84#else
85#define cpu_to_be64(x) bswap_64(x)
86#define be64_to_cpu(x) bswap_64(x)
87#endif
David Gibsonfc14dad2005-06-08 17:18:34 +100088
89#define streq(a, b) (strcmp((a), (b)) == 0)
David Gibson81f2e892005-06-16 17:04:00 +100090#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
91
David Gibsonfc14dad2005-06-08 17:18:34 +100092#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
93#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
94
95/* Data blobs */
David Gibson81f2e892005-06-16 17:04:00 +100096struct fixup {
97 int offset;
98 char *ref;
99 struct fixup *next;
100};
101
David Gibsonfc14dad2005-06-08 17:18:34 +1000102struct data {
103 int len;
104 char *val;
105 int asize;
David Gibson81f2e892005-06-16 17:04:00 +1000106 struct fixup *refs;
David Gibsonfc14dad2005-06-08 17:18:34 +1000107};
108
David Gibson81f2e892005-06-16 17:04:00 +1000109#define empty_data \
110 ((struct data){.len = 0, .val = NULL, .asize = 0, .refs = NULL})
David Gibsonfc14dad2005-06-08 17:18:34 +1000111
David Gibson81f2e892005-06-16 17:04:00 +1000112void fixup_free(struct fixup *f);
David Gibsonfc14dad2005-06-08 17:18:34 +1000113void data_free(struct data d);
114
115struct data data_grow_for(struct data d, int xlen);
116
117struct data data_copy_mem(char *mem, int len);
118struct data data_copy_escape_string(char *s, int len);
119struct data data_copy_file(FILE *f, size_t len);
120
121struct data data_append_data(struct data d, void *p, int len);
122struct data data_append_cell(struct data d, cell_t word);
David Gibsonf040d952005-10-24 18:18:38 +1000123struct data data_append_re(struct data d, struct reserve_entry *re);
David Gibsonf0517db2005-07-15 17:14:24 +1000124struct data data_append_addr(struct data d, u64 addr);
David Gibsonfc14dad2005-06-08 17:18:34 +1000125struct data data_append_byte(struct data d, uint8_t byte);
126struct data data_append_zeroes(struct data d, int len);
127struct data data_append_align(struct data d, int align);
128
David Gibson81f2e892005-06-16 17:04:00 +1000129struct data data_add_fixup(struct data d, char *ref);
130
David Gibsonfc14dad2005-06-08 17:18:34 +1000131int data_is_one_string(struct data d);
132
133/* DT constraints */
134
135#define MAX_PROPNAME_LEN 31
136#define MAX_NODENAME_LEN 31
137
138/* Live trees */
139struct property {
140 char *name;
141 struct data val;
142
143 struct property *next;
David Gibson4102d842005-06-16 14:36:37 +1000144
145 char *label;
David Gibsonfc14dad2005-06-08 17:18:34 +1000146};
147
148struct node {
149 char *name;
150 struct property *proplist;
151 struct node *children;
152
153 struct node *parent;
154 struct node *next_sibling;
155
156 char *fullpath;
157 int basenamelen;
158
159 cell_t phandle;
160 int addr_cells, size_cells;
David Gibson4102d842005-06-16 14:36:37 +1000161
162 char *label;
David Gibsonfc14dad2005-06-08 17:18:34 +1000163};
164
165#define for_each_property(n, p) \
166 for ((p) = (n)->proplist; (p); (p) = (p)->next)
167
168#define for_each_child(n, c) \
169 for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
170
David Gibson4102d842005-06-16 14:36:37 +1000171struct property *build_property(char *name, struct data val, char *label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000172struct property *chain_property(struct property *first, struct property *list);
173
174struct node *build_node(struct property *proplist, struct node *children);
David Gibson4102d842005-06-16 14:36:37 +1000175struct node *name_node(struct node *node, char *name, char *label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000176struct node *chain_node(struct node *first, struct node *list);
177
178void add_property(struct node *node, struct property *prop);
179void add_child(struct node *parent, struct node *child);
180
181int check_device_tree(struct node *dt);
182
David Gibsonf0517db2005-07-15 17:14:24 +1000183/* Boot info (tree plus memreserve information */
184
David Gibsonf040d952005-10-24 18:18:38 +1000185struct reserve_info {
186 struct reserve_entry re;
187
188 struct reserve_info *next;
189
190 char *label;
191};
192
193struct reserve_info *build_reserve_entry(u64 start, u64 len, char *label);
194struct reserve_info *chain_reserve_entry(struct reserve_info *first,
195 struct reserve_info *list);
196struct reserve_info *add_reserve_entry(struct reserve_info *list,
197 struct reserve_info *new);
198
199
David Gibsonf0517db2005-07-15 17:14:24 +1000200struct boot_info {
David Gibsonf040d952005-10-24 18:18:38 +1000201 struct reserve_info *reservelist;
202/* struct data mem_reserve_data; /\* mem reserve from header *\/ */
David Gibsonf0517db2005-07-15 17:14:24 +1000203 struct node *dt; /* the device tree */
204};
205
David Gibsonf040d952005-10-24 18:18:38 +1000206struct boot_info *build_boot_info(struct reserve_info *reservelist,
David Gibsonf0517db2005-07-15 17:14:24 +1000207 struct node *tree);
208
David Gibsonfc14dad2005-06-08 17:18:34 +1000209/* Flattened trees */
210
David Gibson712e52e2005-10-26 16:56:26 +1000211void dt_to_blob(FILE *f, struct boot_info *bi, int version);
212void dt_to_asm(FILE *f, struct boot_info *bi, int version);
David Gibsonfc14dad2005-06-08 17:18:34 +1000213
David Gibsonf0517db2005-07-15 17:14:24 +1000214struct boot_info *dt_from_blob(FILE *f);
David Gibsonfc14dad2005-06-08 17:18:34 +1000215
216/* Tree source */
217
David Gibson712e52e2005-10-26 16:56:26 +1000218void dt_to_source(FILE *f, struct boot_info *bi);
David Gibsonf0517db2005-07-15 17:14:24 +1000219struct boot_info *dt_from_source(FILE *f);
David Gibsonfc14dad2005-06-08 17:18:34 +1000220
221/* FS trees */
222
David Gibsonf0517db2005-07-15 17:14:24 +1000223struct boot_info *dt_from_fs(char *dirname);
David Gibsonfc14dad2005-06-08 17:18:34 +1000224
225/* misc */
226
227char *join_path(char *path, char *name);
228void fill_fullpaths(struct node *tree, char *prefix);
229
230#endif /* _DTC_H */