• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/scripts/dtc/libfdt/
1/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
4 *
5 * libfdt is dual licensed: you can use it either under the terms of
6 * the GPL, or the BSD license, at your option.
7 *
8 *  a) This library 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 library 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
16 *     GNU General Public License for more details.
17 *
18 *     You should have received a copy of the GNU General Public
19 *     License along with this library; if not, write to the Free
20 *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 *     MA 02110-1301 USA
22 *
23 * Alternatively,
24 *
25 *  b) Redistribution and use in source and binary forms, with or
26 *     without modification, are permitted provided that the following
27 *     conditions are met:
28 *
29 *     1. Redistributions of source code must retain the above
30 *        copyright notice, this list of conditions and the following
31 *        disclaimer.
32 *     2. Redistributions in binary form must reproduce the above
33 *        copyright notice, this list of conditions and the following
34 *        disclaimer in the documentation and/or other materials
35 *        provided with the distribution.
36 *
37 *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38 *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39 *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40 *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
42 *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48 *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49 *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 */
51#include "libfdt_env.h"
52
53#include <fdt.h>
54#include <libfdt.h>
55
56#include "libfdt_internal.h"
57
58static int _fdt_sw_check_header(void *fdt)
59{
60	if (fdt_magic(fdt) != FDT_SW_MAGIC)
61		return -FDT_ERR_BADMAGIC;
62	return 0;
63}
64
65#define FDT_SW_CHECK_HEADER(fdt) \
66	{ \
67		int err; \
68		if ((err = _fdt_sw_check_header(fdt)) != 0) \
69			return err; \
70	}
71
72static void *_fdt_grab_space(void *fdt, int len)
73{
74	int offset = fdt_size_dt_struct(fdt);
75	int spaceleft;
76
77	spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
78		- fdt_size_dt_strings(fdt);
79
80	if ((offset + len < offset) || (offset + len > spaceleft))
81		return NULL;
82
83	fdt_set_size_dt_struct(fdt, offset + len);
84	return fdt_offset_ptr_w(fdt, offset, len);
85}
86
87int fdt_create(void *buf, int bufsize)
88{
89	void *fdt = buf;
90
91	if (bufsize < sizeof(struct fdt_header))
92		return -FDT_ERR_NOSPACE;
93
94	memset(buf, 0, bufsize);
95
96	fdt_set_magic(fdt, FDT_SW_MAGIC);
97	fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
98	fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
99	fdt_set_totalsize(fdt,  bufsize);
100
101	fdt_set_off_mem_rsvmap(fdt, FDT_ALIGN(sizeof(struct fdt_header),
102					      sizeof(struct fdt_reserve_entry)));
103	fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
104	fdt_set_off_dt_strings(fdt, bufsize);
105
106	return 0;
107}
108
109int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
110{
111	struct fdt_reserve_entry *re;
112	int offset;
113
114	FDT_SW_CHECK_HEADER(fdt);
115
116	if (fdt_size_dt_struct(fdt))
117		return -FDT_ERR_BADSTATE;
118
119	offset = fdt_off_dt_struct(fdt);
120	if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
121		return -FDT_ERR_NOSPACE;
122
123	re = (struct fdt_reserve_entry *)((char *)fdt + offset);
124	re->address = cpu_to_fdt64(addr);
125	re->size = cpu_to_fdt64(size);
126
127	fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
128
129	return 0;
130}
131
132int fdt_finish_reservemap(void *fdt)
133{
134	return fdt_add_reservemap_entry(fdt, 0, 0);
135}
136
137int fdt_begin_node(void *fdt, const char *name)
138{
139	struct fdt_node_header *nh;
140	int namelen = strlen(name) + 1;
141
142	FDT_SW_CHECK_HEADER(fdt);
143
144	nh = _fdt_grab_space(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
145	if (! nh)
146		return -FDT_ERR_NOSPACE;
147
148	nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
149	memcpy(nh->name, name, namelen);
150	return 0;
151}
152
153int fdt_end_node(void *fdt)
154{
155	uint32_t *en;
156
157	FDT_SW_CHECK_HEADER(fdt);
158
159	en = _fdt_grab_space(fdt, FDT_TAGSIZE);
160	if (! en)
161		return -FDT_ERR_NOSPACE;
162
163	*en = cpu_to_fdt32(FDT_END_NODE);
164	return 0;
165}
166
167static int _fdt_find_add_string(void *fdt, const char *s)
168{
169	char *strtab = (char *)fdt + fdt_totalsize(fdt);
170	const char *p;
171	int strtabsize = fdt_size_dt_strings(fdt);
172	int len = strlen(s) + 1;
173	int struct_top, offset;
174
175	p = _fdt_find_string(strtab - strtabsize, strtabsize, s);
176	if (p)
177		return p - strtab;
178
179	/* Add it */
180	offset = -strtabsize - len;
181	struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
182	if (fdt_totalsize(fdt) + offset < struct_top)
183		return 0; /* no more room :( */
184
185	memcpy(strtab + offset, s, len);
186	fdt_set_size_dt_strings(fdt, strtabsize + len);
187	return offset;
188}
189
190int fdt_property(void *fdt, const char *name, const void *val, int len)
191{
192	struct fdt_property *prop;
193	int nameoff;
194
195	FDT_SW_CHECK_HEADER(fdt);
196
197	nameoff = _fdt_find_add_string(fdt, name);
198	if (nameoff == 0)
199		return -FDT_ERR_NOSPACE;
200
201	prop = _fdt_grab_space(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
202	if (! prop)
203		return -FDT_ERR_NOSPACE;
204
205	prop->tag = cpu_to_fdt32(FDT_PROP);
206	prop->nameoff = cpu_to_fdt32(nameoff);
207	prop->len = cpu_to_fdt32(len);
208	memcpy(prop->data, val, len);
209	return 0;
210}
211
212int fdt_finish(void *fdt)
213{
214	char *p = (char *)fdt;
215	uint32_t *end;
216	int oldstroffset, newstroffset;
217	uint32_t tag;
218	int offset, nextoffset;
219
220	FDT_SW_CHECK_HEADER(fdt);
221
222	/* Add terminator */
223	end = _fdt_grab_space(fdt, sizeof(*end));
224	if (! end)
225		return -FDT_ERR_NOSPACE;
226	*end = cpu_to_fdt32(FDT_END);
227
228	/* Relocate the string table */
229	oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
230	newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
231	memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
232	fdt_set_off_dt_strings(fdt, newstroffset);
233
234	/* Walk the structure, correcting string offsets */
235	offset = 0;
236	while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
237		if (tag == FDT_PROP) {
238			struct fdt_property *prop =
239				fdt_offset_ptr_w(fdt, offset, sizeof(*prop));
240			int nameoff;
241
242			if (! prop)
243				return -FDT_ERR_BADSTRUCTURE;
244
245			nameoff = fdt32_to_cpu(prop->nameoff);
246			nameoff += fdt_size_dt_strings(fdt);
247			prop->nameoff = cpu_to_fdt32(nameoff);
248		}
249		offset = nextoffset;
250	}
251
252	/* Finally, adjust the header */
253	fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
254	fdt_set_magic(fdt, FDT_MAGIC);
255	return 0;
256}
257