blob: 75a1ac56d23b7bbac6398134d278d319dc24f2e6 [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
65typedef uint16_t u16;
66typedef uint32_t u32;
67typedef uint64_t u64;
68typedef u32 cell_t;
69
70#define cpu_to_be16(x) htons(x)
71#define be16_to_cpu(x) ntohs(x)
72
73#define cpu_to_be32(x) htonl(x)
74#define be32_to_cpu(x) ntohl(x)
75
76
77
78#define streq(a, b) (strcmp((a), (b)) == 0)
79#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
80#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
81
82/* Data blobs */
83struct data {
84 int len;
85 char *val;
86 int asize;
87};
88
89#define empty_data ((struct data){.len = 0, .val = NULL, .asize = 0})
90
91void data_free(struct data d);
92
93struct data data_grow_for(struct data d, int xlen);
94
95struct data data_copy_mem(char *mem, int len);
96struct data data_copy_escape_string(char *s, int len);
97struct data data_copy_file(FILE *f, size_t len);
98
99struct data data_append_data(struct data d, void *p, int len);
100struct data data_append_cell(struct data d, cell_t word);
101struct data data_append_byte(struct data d, uint8_t byte);
102struct data data_append_zeroes(struct data d, int len);
103struct data data_append_align(struct data d, int align);
104
105int data_is_one_string(struct data d);
106
107/* DT constraints */
108
109#define MAX_PROPNAME_LEN 31
110#define MAX_NODENAME_LEN 31
111
112/* Live trees */
113struct property {
114 char *name;
115 struct data val;
116
117 struct property *next;
118};
119
120struct node {
121 char *name;
122 struct property *proplist;
123 struct node *children;
124
125 struct node *parent;
126 struct node *next_sibling;
127
128 char *fullpath;
129 int basenamelen;
130
131 cell_t phandle;
132 int addr_cells, size_cells;
133};
134
135#define for_each_property(n, p) \
136 for ((p) = (n)->proplist; (p); (p) = (p)->next)
137
138#define for_each_child(n, c) \
139 for ((c) = (n)->children; (c); (c) = (c)->next_sibling)
140
141struct property *build_property(char *name, struct data val);
142struct property *build_empty_property(char *name);
143struct property *chain_property(struct property *first, struct property *list);
144
145struct node *build_node(struct property *proplist, struct node *children);
146struct node *name_node(struct node *node, char *name);
147struct node *chain_node(struct node *first, struct node *list);
148
149void add_property(struct node *node, struct property *prop);
150void add_child(struct node *parent, struct node *child);
151
152int check_device_tree(struct node *dt);
153
154/* Flattened trees */
155
156enum flat_dt_format {
157 FFMT_BIN,
158 FFMT_ASM,
159};
160
161void write_dt_blob(FILE *f, struct node *tree, int version, int reservenum);
162void write_dt_asm(FILE *f, struct node *tree, int version, int reservenum);
163
164struct node *dt_from_blob(FILE *f);
165
166/* Tree source */
167
168void write_tree_source(FILE *f, struct node *tree, int level);
169
170struct node *dt_from_source(FILE *f);
171
172/* FS trees */
173
174struct node *dt_from_fs(char *dirname);
175
176/* misc */
177
178char *join_path(char *path, char *name);
179void fill_fullpaths(struct node *tree, char *prefix);
180
181#endif /* _DTC_H */