blob: 93c3e60d406205edfd19810fa1a09946bcf6ebfc [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>
34
35static inline void die(char * str, ...)
36{
37 va_list ap;
38
39 va_start(ap, str);
40 fprintf(stderr, "FATAL ERROR: ");
41 vfprintf(stderr, str, ap);
42 exit(1);
43}
44
45static inline void *xmalloc(size_t len)
46{
47 void *new = malloc(len);
48
49 if (! new)
50 die("malloc() failed\n");
51
52 return new;
53}
54
55static inline void *xrealloc(void *p, size_t len)
56{
57 void *new = realloc(p, len);
58
59 if (! new)
60 die("realloc() failed (len=%d)\n", len);
61
62 return new;
63}
64
David Gibson03a9b9d2005-07-11 16:49:52 +100065typedef uint8_t u8;
David Gibsonfc14dad2005-06-08 17:18:34 +100066typedef uint16_t u16;
67typedef uint32_t u32;
68typedef uint64_t u64;
69typedef u32 cell_t;
70
71#define cpu_to_be16(x) htons(x)
72#define be16_to_cpu(x) ntohs(x)
73
74#define cpu_to_be32(x) htonl(x)
75#define be32_to_cpu(x) ntohl(x)
76
77
78
79#define streq(a, b) (strcmp((a), (b)) == 0)
David Gibson81f2e892005-06-16 17:04:00 +100080#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
81
David Gibsonfc14dad2005-06-08 17:18:34 +100082#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
83#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
84
85/* Data blobs */
David Gibson81f2e892005-06-16 17:04:00 +100086struct fixup {
87 int offset;
88 char *ref;
89 struct fixup *next;
90};
91
David Gibsonfc14dad2005-06-08 17:18:34 +100092struct data {
93 int len;
94 char *val;
95 int asize;
David Gibson81f2e892005-06-16 17:04:00 +100096 struct fixup *refs;
David Gibsonfc14dad2005-06-08 17:18:34 +100097};
98
David Gibson81f2e892005-06-16 17:04:00 +100099#define empty_data \
100 ((struct data){.len = 0, .val = NULL, .asize = 0, .refs = NULL})
David Gibsonfc14dad2005-06-08 17:18:34 +1000101
David Gibson81f2e892005-06-16 17:04:00 +1000102void fixup_free(struct fixup *f);
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
107struct data data_copy_mem(char *mem, int len);
108struct data data_copy_escape_string(char *s, int len);
109struct data data_copy_file(FILE *f, size_t len);
110
111struct data data_append_data(struct data d, void *p, int len);
112struct data data_append_cell(struct data d, cell_t word);
113struct data data_append_byte(struct data d, uint8_t byte);
114struct data data_append_zeroes(struct data d, int len);
115struct data data_append_align(struct data d, int align);
116
David Gibson81f2e892005-06-16 17:04:00 +1000117struct data data_add_fixup(struct data d, char *ref);
118
David Gibsonfc14dad2005-06-08 17:18:34 +1000119int data_is_one_string(struct data d);
120
121/* DT constraints */
122
123#define MAX_PROPNAME_LEN 31
124#define MAX_NODENAME_LEN 31
125
126/* Live trees */
127struct property {
128 char *name;
129 struct data val;
130
131 struct property *next;
David Gibson4102d842005-06-16 14:36:37 +1000132
133 char *label;
David Gibsonfc14dad2005-06-08 17:18:34 +1000134};
135
136struct node {
137 char *name;
138 struct property *proplist;
139 struct node *children;
140
141 struct node *parent;
142 struct node *next_sibling;
143
144 char *fullpath;
145 int basenamelen;
146
147 cell_t phandle;
148 int addr_cells, size_cells;
David Gibson4102d842005-06-16 14:36:37 +1000149
150 char *label;
David Gibsonfc14dad2005-06-08 17:18:34 +1000151};
152
153#define for_each_property(n, p) \
154 for ((p) = (n)->proplist; (p); (p) = (p)->next)
155
156#define for_each_child(n, c) \
157 for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
158
David Gibson4102d842005-06-16 14:36:37 +1000159struct property *build_property(char *name, struct data val, char *label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000160struct property *chain_property(struct property *first, struct property *list);
161
162struct node *build_node(struct property *proplist, struct node *children);
David Gibson4102d842005-06-16 14:36:37 +1000163struct node *name_node(struct node *node, char *name, char *label);
David Gibsonfc14dad2005-06-08 17:18:34 +1000164struct node *chain_node(struct node *first, struct node *list);
165
166void add_property(struct node *node, struct property *prop);
167void add_child(struct node *parent, struct node *child);
168
169int check_device_tree(struct node *dt);
170
171/* Flattened trees */
172
173enum flat_dt_format {
174 FFMT_BIN,
175 FFMT_ASM,
176};
177
178void write_dt_blob(FILE *f, struct node *tree, int version, int reservenum);
179void write_dt_asm(FILE *f, struct node *tree, int version, int reservenum);
180
181struct node *dt_from_blob(FILE *f);
182
183/* Tree source */
184
185void write_tree_source(FILE *f, struct node *tree, int level);
186
187struct node *dt_from_source(FILE *f);
188
189/* FS trees */
190
191struct node *dt_from_fs(char *dirname);
192
193/* misc */
194
195char *join_path(char *path, char *name);
196void fill_fullpaths(struct node *tree, char *prefix);
197
198#endif /* _DTC_H */