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{
95204431Sraj	const uint32_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
152204431Srajint fdt_next_node(const void *fdt, int offset, int *depth)
153204431Sraj{
154204431Sraj	int nextoffset = 0;
155204431Sraj	uint32_t tag;
156204431Sraj
157204431Sraj	if (offset >= 0)
158204431Sraj		if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0)
159204431Sraj			return nextoffset;
160204431Sraj
161204431Sraj	do {
162204431Sraj		offset = nextoffset;
163204431Sraj		tag = fdt_next_tag(fdt, offset, &nextoffset);
164204431Sraj
165204431Sraj		switch (tag) {
166204431Sraj		case FDT_PROP:
167204431Sraj		case FDT_NOP:
168204431Sraj			break;
169204431Sraj
170204431Sraj		case FDT_BEGIN_NODE:
171204431Sraj			if (depth)
172204431Sraj				(*depth)++;
173204431Sraj			break;
174204431Sraj
175204431Sraj		case FDT_END_NODE:
176204433Sraj			if (depth && ((--(*depth)) < 0))
177204433Sraj				return nextoffset;
178204431Sraj			break;
179204431Sraj
180204431Sraj		case FDT_END:
181204433Sraj			if ((nextoffset >= 0)
182204433Sraj			    || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
183204433Sraj				return -FDT_ERR_NOTFOUND;
184204433Sraj			else
185204433Sraj				return nextoffset;
186204431Sraj		}
187204431Sraj	} while (tag != FDT_BEGIN_NODE);
188204431Sraj
189204431Sraj	return offset;
190204431Sraj}
191204431Sraj
192204431Srajconst char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
193204431Sraj{
194204431Sraj	int len = strlen(s) + 1;
195204431Sraj	const char *last = strtab + tabsize - len;
196204431Sraj	const char *p;
197204431Sraj
198204431Sraj	for (p = strtab; p <= last; p++)
199204431Sraj		if (memcmp(p, s, len) == 0)
200204431Sraj			return p;
201204431Sraj	return NULL;
202204431Sraj}
203204431Sraj
204204431Srajint fdt_move(const void *fdt, void *buf, int bufsize)
205204431Sraj{
206204431Sraj	FDT_CHECK_HEADER(fdt);
207204431Sraj
208204431Sraj	if (fdt_totalsize(fdt) > bufsize)
209204431Sraj		return -FDT_ERR_NOSPACE;
210204431Sraj
211204431Sraj	memmove(buf, fdt, fdt_totalsize(fdt));
212204431Sraj	return 0;
213204431Sraj}
214