fdt.c revision 261215
1204431Sraj/*
2204431Sraj * libfdt - Flat Device Tree manipulation
3204431Sraj * Copyright (C) 2006 David Gibson, IBM Corporation.
4204431Sraj *
5204431Sraj * libfdt is dual licensed: you can use it either under the terms of
6204431Sraj * the GPL, or the BSD license, at your option.
7204431Sraj *
8204431Sraj *  a) This library is free software; you can redistribute it and/or
9204431Sraj *     modify it under the terms of the GNU General Public License as
10204431Sraj *     published by the Free Software Foundation; either version 2 of the
11204431Sraj *     License, or (at your option) any later version.
12204431Sraj *
13204431Sraj *     This library is distributed in the hope that it will be useful,
14204431Sraj *     but WITHOUT ANY WARRANTY; without even the implied warranty of
15204431Sraj *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16204431Sraj *     GNU General Public License for more details.
17204431Sraj *
18204431Sraj *     You should have received a copy of the GNU General Public
19204431Sraj *     License along with this library; if not, write to the Free
20204431Sraj *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21204431Sraj *     MA 02110-1301 USA
22204431Sraj *
23204431Sraj * Alternatively,
24204431Sraj *
25204431Sraj *  b) Redistribution and use in source and binary forms, with or
26204431Sraj *     without modification, are permitted provided that the following
27204431Sraj *     conditions are met:
28204431Sraj *
29204431Sraj *     1. Redistributions of source code must retain the above
30204431Sraj *        copyright notice, this list of conditions and the following
31204431Sraj *        disclaimer.
32204431Sraj *     2. Redistributions in binary form must reproduce the above
33204431Sraj *        copyright notice, this list of conditions and the following
34204431Sraj *        disclaimer in the documentation and/or other materials
35204431Sraj *        provided with the distribution.
36204431Sraj *
37204431Sraj *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38204431Sraj *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39204431Sraj *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40204431Sraj *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41204431Sraj *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
42204431Sraj *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43204431Sraj *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44204431Sraj *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45204431Sraj *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46204431Sraj *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47204431Sraj *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48204431Sraj *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49204431Sraj *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50204431Sraj */
51204431Sraj#include "libfdt_env.h"
52204431Sraj
53204431Sraj#include <fdt.h>
54204431Sraj#include <libfdt.h>
55204431Sraj
56204431Sraj#include "libfdt_internal.h"
57204431Sraj
58204431Srajint fdt_check_header(const void *fdt)
59204431Sraj{
60204431Sraj	if (fdt_magic(fdt) == FDT_MAGIC) {
61204431Sraj		/* Complete tree */
62204431Sraj		if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
63204431Sraj			return -FDT_ERR_BADVERSION;
64204431Sraj		if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
65204431Sraj			return -FDT_ERR_BADVERSION;
66204431Sraj	} else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
67204431Sraj		/* Unfinished sequential-write blob */
68204431Sraj		if (fdt_size_dt_struct(fdt) == 0)
69204431Sraj			return -FDT_ERR_BADSTATE;
70204431Sraj	} else {
71204431Sraj		return -FDT_ERR_BADMAGIC;
72204431Sraj	}
73204431Sraj
74204431Sraj	return 0;
75204431Sraj}
76204431Sraj
77204433Srajconst void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
78204431Sraj{
79204431Sraj	const char *p;
80204431Sraj
81204431Sraj	if (fdt_version(fdt) >= 0x11)
82204431Sraj		if (((offset + len) < offset)
83204431Sraj		    || ((offset + len) > fdt_size_dt_struct(fdt)))
84204431Sraj			return NULL;
85204431Sraj
86204431Sraj	p = _fdt_offset_ptr(fdt, offset);
87204431Sraj
88204431Sraj	if (p + len < p)
89204431Sraj		return NULL;
90204431Sraj	return p;
91204431Sraj}
92204431Sraj
93204433Srajuint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
94204431Sraj{
95261215Simp	const fdt32_t *tagp, *lenp;
96204431Sraj	uint32_t tag;
97204433Sraj	int offset = startoffset;
98204431Sraj	const char *p;
99204431Sraj
100204433Sraj	*nextoffset = -FDT_ERR_TRUNCATED;
101204431Sraj	tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
102204433Sraj	if (!tagp)
103204431Sraj		return FDT_END; /* premature end */
104204431Sraj	tag = fdt32_to_cpu(*tagp);
105204431Sraj	offset += FDT_TAGSIZE;
106204431Sraj
107204433Sraj	*nextoffset = -FDT_ERR_BADSTRUCTURE;
108204431Sraj	switch (tag) {
109204431Sraj	case FDT_BEGIN_NODE:
110204431Sraj		/* skip name */
111204431Sraj		do {
112204431Sraj			p = fdt_offset_ptr(fdt, offset++, 1);
113204431Sraj		} while (p && (*p != '\0'));
114204433Sraj		if (!p)
115204433Sraj			return FDT_END; /* premature end */
116204431Sraj		break;
117204433Sraj
118204431Sraj	case FDT_PROP:
119204431Sraj		lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
120204433Sraj		if (!lenp)
121204433Sraj			return FDT_END; /* premature end */
122204433Sraj		/* skip-name offset, length and value */
123204433Sraj		offset += sizeof(struct fdt_property) - FDT_TAGSIZE
124204433Sraj			+ fdt32_to_cpu(*lenp);
125204431Sraj		break;
126204433Sraj
127204433Sraj	case FDT_END:
128204433Sraj	case FDT_END_NODE:
129204433Sraj	case FDT_NOP:
130204433Sraj		break;
131204433Sraj
132204433Sraj	default:
133204433Sraj		return FDT_END;
134204431Sraj	}
135204431Sraj
136204433Sraj	if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
137204433Sraj		return FDT_END; /* premature end */
138204431Sraj
139204433Sraj	*nextoffset = FDT_TAGALIGN(offset);
140204431Sraj	return tag;
141204431Sraj}
142204431Sraj
143204431Srajint _fdt_check_node_offset(const void *fdt, int offset)
144204431Sraj{
145204431Sraj	if ((offset < 0) || (offset % FDT_TAGSIZE)
146204431Sraj	    || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE))
147204431Sraj		return -FDT_ERR_BADOFFSET;
148204431Sraj
149204431Sraj	return offset;
150204431Sraj}
151204431Sraj
152238742Simpint _fdt_check_prop_offset(const void *fdt, int offset)
153238742Simp{
154238742Simp	if ((offset < 0) || (offset % FDT_TAGSIZE)
155238742Simp	    || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP))
156238742Simp		return -FDT_ERR_BADOFFSET;
157238742Simp
158238742Simp	return offset;
159238742Simp}
160238742Simp
161204431Srajint fdt_next_node(const void *fdt, int offset, int *depth)
162204431Sraj{
163204431Sraj	int nextoffset = 0;
164204431Sraj	uint32_t tag;
165204431Sraj
166204431Sraj	if (offset >= 0)
167204431Sraj		if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0)
168204431Sraj			return nextoffset;
169204431Sraj
170204431Sraj	do {
171204431Sraj		offset = nextoffset;
172204431Sraj		tag = fdt_next_tag(fdt, offset, &nextoffset);
173204431Sraj
174204431Sraj		switch (tag) {
175204431Sraj		case FDT_PROP:
176204431Sraj		case FDT_NOP:
177204431Sraj			break;
178204431Sraj
179204431Sraj		case FDT_BEGIN_NODE:
180204431Sraj			if (depth)
181204431Sraj				(*depth)++;
182204431Sraj			break;
183204431Sraj
184204431Sraj		case FDT_END_NODE:
185204433Sraj			if (depth && ((--(*depth)) < 0))
186204433Sraj				return nextoffset;
187204431Sraj			break;
188204431Sraj
189204431Sraj		case FDT_END:
190204433Sraj			if ((nextoffset >= 0)
191204433Sraj			    || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
192204433Sraj				return -FDT_ERR_NOTFOUND;
193204433Sraj			else
194204433Sraj				return nextoffset;
195204431Sraj		}
196204431Sraj	} while (tag != FDT_BEGIN_NODE);
197204431Sraj
198204431Sraj	return offset;
199204431Sraj}
200204431Sraj
201261215Simpint fdt_first_subnode(const void *fdt, int offset)
202261215Simp{
203261215Simp	int depth = 0;
204261215Simp
205261215Simp	offset = fdt_next_node(fdt, offset, &depth);
206261215Simp	if (offset < 0 || depth != 1)
207261215Simp		return -FDT_ERR_NOTFOUND;
208261215Simp
209261215Simp	return offset;
210261215Simp}
211261215Simp
212261215Simpint fdt_next_subnode(const void *fdt, int offset)
213261215Simp{
214261215Simp	int depth = 1;
215261215Simp
216261215Simp	/*
217261215Simp	 * With respect to the parent, the depth of the next subnode will be
218261215Simp	 * the same as the last.
219261215Simp	 */
220261215Simp	do {
221261215Simp		offset = fdt_next_node(fdt, offset, &depth);
222261215Simp		if (offset < 0 || depth < 1)
223261215Simp			return -FDT_ERR_NOTFOUND;
224261215Simp	} while (depth > 1);
225261215Simp
226261215Simp	return offset;
227261215Simp}
228261215Simp
229204431Srajconst char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
230204431Sraj{
231204431Sraj	int len = strlen(s) + 1;
232204431Sraj	const char *last = strtab + tabsize - len;
233204431Sraj	const char *p;
234204431Sraj
235204431Sraj	for (p = strtab; p <= last; p++)
236204431Sraj		if (memcmp(p, s, len) == 0)
237204431Sraj			return p;
238204431Sraj	return NULL;
239204431Sraj}
240204431Sraj
241204431Srajint fdt_move(const void *fdt, void *buf, int bufsize)
242204431Sraj{
243204431Sraj	FDT_CHECK_HEADER(fdt);
244204431Sraj
245204431Sraj	if (fdt_totalsize(fdt) > bufsize)
246204431Sraj		return -FDT_ERR_NOSPACE;
247204431Sraj
248204431Sraj	memmove(buf, fdt, fdt_totalsize(fdt));
249204431Sraj	return 0;
250204431Sraj}
251