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_blocks_misordered(const void *fdt,
59			      int mem_rsv_size, int struct_size)
60{
61	return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8))
62		|| (fdt_off_dt_struct(fdt) <
63		    (fdt_off_mem_rsvmap(fdt) + mem_rsv_size))
64		|| (fdt_off_dt_strings(fdt) <
65		    (fdt_off_dt_struct(fdt) + struct_size))
66		|| (fdt_totalsize(fdt) <
67		    (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
68}
69
70static int _fdt_rw_check_header(void *fdt)
71{
72	FDT_CHECK_HEADER(fdt);
73
74	if (fdt_version(fdt) < 17)
75		return -FDT_ERR_BADVERSION;
76	if (_fdt_blocks_misordered(fdt, sizeof(struct fdt_reserve_entry),
77				   fdt_size_dt_struct(fdt)))
78		return -FDT_ERR_BADLAYOUT;
79	if (fdt_version(fdt) > 17)
80		fdt_set_version(fdt, 17);
81
82	return 0;
83}
84
85#define FDT_RW_CHECK_HEADER(fdt) \
86	{ \
87		int __err; \
88		if ((__err = _fdt_rw_check_header(fdt)) != 0) \
89			return __err; \
90	}
91
92static inline int _fdt_data_size(void *fdt)
93{
94	return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
95}
96
97static int _fdt_splice(void *fdt, void *splicepoint, int oldlen, int newlen)
98{
99	char *p = splicepoint;
100	char *end = (char *)fdt + _fdt_data_size(fdt);
101
102	if (((p + oldlen) < p) || ((p + oldlen) > end))
103		return -FDT_ERR_BADOFFSET;
104	if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt)))
105		return -FDT_ERR_NOSPACE;
106	memmove(p + newlen, p + oldlen, end - p - oldlen);
107	return 0;
108}
109
110static int _fdt_splice_mem_rsv(void *fdt, struct fdt_reserve_entry *p,
111			       int oldn, int newn)
112{
113	int delta = (newn - oldn) * sizeof(*p);
114	int err;
115	err = _fdt_splice(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
116	if (err)
117		return err;
118	fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
119	fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
120	return 0;
121}
122
123static int _fdt_splice_struct(void *fdt, void *p,
124			      int oldlen, int newlen)
125{
126	int delta = newlen - oldlen;
127	int err;
128
129	if ((err = _fdt_splice(fdt, p, oldlen, newlen)))
130		return err;
131
132	fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
133	fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
134	return 0;
135}
136
137static int _fdt_splice_string(void *fdt, int newlen)
138{
139	void *p = (char *)fdt
140		+ fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
141	int err;
142
143	if ((err = _fdt_splice(fdt, p, 0, newlen)))
144		return err;
145
146	fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
147	return 0;
148}
149
150static int _fdt_find_add_string(void *fdt, const char *s)
151{
152	char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
153	const char *p;
154	char *new;
155	int len = strlen(s) + 1;
156	int err;
157
158	p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s);
159	if (p)
160		/* found it */
161		return (p - strtab);
162
163	new = strtab + fdt_size_dt_strings(fdt);
164	err = _fdt_splice_string(fdt, len);
165	if (err)
166		return err;
167
168	memcpy(new, s, len);
169	return (new - strtab);
170}
171
172int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
173{
174	struct fdt_reserve_entry *re;
175	int err;
176
177	FDT_RW_CHECK_HEADER(fdt);
178
179	re = _fdt_mem_rsv_w(fdt, fdt_num_mem_rsv(fdt));
180	err = _fdt_splice_mem_rsv(fdt, re, 0, 1);
181	if (err)
182		return err;
183
184	re->address = cpu_to_fdt64(address);
185	re->size = cpu_to_fdt64(size);
186	return 0;
187}
188
189int fdt_del_mem_rsv(void *fdt, int n)
190{
191	struct fdt_reserve_entry *re = _fdt_mem_rsv_w(fdt, n);
192	int err;
193
194	FDT_RW_CHECK_HEADER(fdt);
195
196	if (n >= fdt_num_mem_rsv(fdt))
197		return -FDT_ERR_NOTFOUND;
198
199	err = _fdt_splice_mem_rsv(fdt, re, 1, 0);
200	if (err)
201		return err;
202	return 0;
203}
204
205static int _fdt_resize_property(void *fdt, int nodeoffset, const char *name,
206				int len, struct fdt_property **prop)
207{
208	int oldlen;
209	int err;
210
211	*prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
212	if (! (*prop))
213		return oldlen;
214
215	if ((err = _fdt_splice_struct(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
216				      FDT_TAGALIGN(len))))
217		return err;
218
219	(*prop)->len = cpu_to_fdt32(len);
220	return 0;
221}
222
223static int _fdt_add_property(void *fdt, int nodeoffset, const char *name,
224			     int len, struct fdt_property **prop)
225{
226	int proplen;
227	int nextoffset;
228	int namestroff;
229	int err;
230
231	if ((nextoffset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
232		return nextoffset;
233
234	namestroff = _fdt_find_add_string(fdt, name);
235	if (namestroff < 0)
236		return namestroff;
237
238	*prop = _fdt_offset_ptr_w(fdt, nextoffset);
239	proplen = sizeof(**prop) + FDT_TAGALIGN(len);
240
241	err = _fdt_splice_struct(fdt, *prop, 0, proplen);
242	if (err)
243		return err;
244
245	(*prop)->tag = cpu_to_fdt32(FDT_PROP);
246	(*prop)->nameoff = cpu_to_fdt32(namestroff);
247	(*prop)->len = cpu_to_fdt32(len);
248	return 0;
249}
250
251int fdt_set_name(void *fdt, int nodeoffset, const char *name)
252{
253	char *namep;
254	int oldlen, newlen;
255	int err;
256
257	FDT_RW_CHECK_HEADER(fdt);
258
259	namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
260	if (!namep)
261		return oldlen;
262
263	newlen = strlen(name);
264
265	err = _fdt_splice_struct(fdt, namep, FDT_TAGALIGN(oldlen+1),
266				 FDT_TAGALIGN(newlen+1));
267	if (err)
268		return err;
269
270	memcpy(namep, name, newlen+1);
271	return 0;
272}
273
274int fdt_setprop(void *fdt, int nodeoffset, const char *name,
275		const void *val, int len)
276{
277	struct fdt_property *prop;
278	int err;
279
280	FDT_RW_CHECK_HEADER(fdt);
281
282	err = _fdt_resize_property(fdt, nodeoffset, name, len, &prop);
283	if (err == -FDT_ERR_NOTFOUND)
284		err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
285	if (err)
286		return err;
287
288	memcpy(prop->data, val, len);
289	return 0;
290}
291
292int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
293		   const void *val, int len)
294{
295	struct fdt_property *prop;
296	int err, oldlen, newlen;
297
298	FDT_RW_CHECK_HEADER(fdt);
299
300	prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
301	if (prop) {
302		newlen = len + oldlen;
303		err = _fdt_splice_struct(fdt, prop->data,
304					 FDT_TAGALIGN(oldlen),
305					 FDT_TAGALIGN(newlen));
306		if (err)
307			return err;
308		prop->len = cpu_to_fdt32(newlen);
309		memcpy(prop->data + oldlen, val, len);
310	} else {
311		err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
312		if (err)
313			return err;
314		memcpy(prop->data, val, len);
315	}
316	return 0;
317}
318
319int fdt_delprop(void *fdt, int nodeoffset, const char *name)
320{
321	struct fdt_property *prop;
322	int len, proplen;
323
324	FDT_RW_CHECK_HEADER(fdt);
325
326	prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
327	if (! prop)
328		return len;
329
330	proplen = sizeof(*prop) + FDT_TAGALIGN(len);
331	return _fdt_splice_struct(fdt, prop, proplen, 0);
332}
333
334int fdt_add_subnode_namelen(void *fdt, int parentoffset,
335			    const char *name, int namelen)
336{
337	struct fdt_node_header *nh;
338	int offset, nextoffset;
339	int nodelen;
340	int err;
341	uint32_t tag;
342	fdt32_t *endtag;
343
344	FDT_RW_CHECK_HEADER(fdt);
345
346	offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
347	if (offset >= 0)
348		return -FDT_ERR_EXISTS;
349	else if (offset != -FDT_ERR_NOTFOUND)
350		return offset;
351
352	/* Try to place the new node after the parent's properties */
353	fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
354	do {
355		offset = nextoffset;
356		tag = fdt_next_tag(fdt, offset, &nextoffset);
357	} while ((tag == FDT_PROP) || (tag == FDT_NOP));
358
359	nh = _fdt_offset_ptr_w(fdt, offset);
360	nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
361
362	err = _fdt_splice_struct(fdt, nh, 0, nodelen);
363	if (err)
364		return err;
365
366	nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
367	memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
368	memcpy(nh->name, name, namelen);
369	endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
370	*endtag = cpu_to_fdt32(FDT_END_NODE);
371
372	return offset;
373}
374
375int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
376{
377	return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
378}
379
380int fdt_del_node(void *fdt, int nodeoffset)
381{
382	int endoffset;
383
384	FDT_RW_CHECK_HEADER(fdt);
385
386	endoffset = _fdt_node_end_offset(fdt, nodeoffset);
387	if (endoffset < 0)
388		return endoffset;
389
390	return _fdt_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset),
391				  endoffset - nodeoffset, 0);
392}
393
394static void _fdt_packblocks(const char *old, char *new,
395			    int mem_rsv_size, int struct_size)
396{
397	int mem_rsv_off, struct_off, strings_off;
398
399	mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
400	struct_off = mem_rsv_off + mem_rsv_size;
401	strings_off = struct_off + struct_size;
402
403	memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
404	fdt_set_off_mem_rsvmap(new, mem_rsv_off);
405
406	memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
407	fdt_set_off_dt_struct(new, struct_off);
408	fdt_set_size_dt_struct(new, struct_size);
409
410	memmove(new + strings_off, old + fdt_off_dt_strings(old),
411		fdt_size_dt_strings(old));
412	fdt_set_off_dt_strings(new, strings_off);
413	fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
414}
415
416int fdt_open_into(const void *fdt, void *buf, int bufsize)
417{
418	int err;
419	int mem_rsv_size, struct_size;
420	int newsize;
421	const char *fdtstart = fdt;
422	const char *fdtend = fdtstart + fdt_totalsize(fdt);
423	char *tmp;
424
425	FDT_CHECK_HEADER(fdt);
426
427	mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
428		* sizeof(struct fdt_reserve_entry);
429
430	if (fdt_version(fdt) >= 17) {
431		struct_size = fdt_size_dt_struct(fdt);
432	} else {
433		struct_size = 0;
434		while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
435			;
436		if (struct_size < 0)
437			return struct_size;
438	}
439
440	if (!_fdt_blocks_misordered(fdt, mem_rsv_size, struct_size)) {
441		/* no further work necessary */
442		err = fdt_move(fdt, buf, bufsize);
443		if (err)
444			return err;
445		fdt_set_version(buf, 17);
446		fdt_set_size_dt_struct(buf, struct_size);
447		fdt_set_totalsize(buf, bufsize);
448		return 0;
449	}
450
451	/* Need to reorder */
452	newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
453		+ struct_size + fdt_size_dt_strings(fdt);
454
455	if (bufsize < newsize)
456		return -FDT_ERR_NOSPACE;
457
458	/* First attempt to build converted tree at beginning of buffer */
459	tmp = buf;
460	/* But if that overlaps with the old tree... */
461	if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
462		/* Try right after the old tree instead */
463		tmp = (char *)(uintptr_t)fdtend;
464		if ((tmp + newsize) > ((char *)buf + bufsize))
465			return -FDT_ERR_NOSPACE;
466	}
467
468	_fdt_packblocks(fdt, tmp, mem_rsv_size, struct_size);
469	memmove(buf, tmp, newsize);
470
471	fdt_set_magic(buf, FDT_MAGIC);
472	fdt_set_totalsize(buf, bufsize);
473	fdt_set_version(buf, 17);
474	fdt_set_last_comp_version(buf, 16);
475	fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
476
477	return 0;
478}
479
480int fdt_pack(void *fdt)
481{
482	int mem_rsv_size;
483
484	FDT_RW_CHECK_HEADER(fdt);
485
486	mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
487		* sizeof(struct fdt_reserve_entry);
488	_fdt_packblocks(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt));
489	fdt_set_totalsize(fdt, _fdt_data_size(fdt));
490
491	return 0;
492}
493