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, see <http://www.gnu.org/licenses/>.
20  *
21  * Alternatively,
22  *
23  *  b) Redistribution and use in source and binary forms, with or
24  *     without modification, are permitted provided that the following
25  *     conditions are met:
26  *
27  *     1. Redistributions of source code must retain the above
28  *        copyright notice, this list of conditions and the following
29  *        disclaimer.
30  *     2. Redistributions in binary form must reproduce the above
31  *        copyright notice, this list of conditions and the following
32  *        disclaimer in the documentation and/or other materials
33  *        provided with the distribution.
34  *
35  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
36  *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
37  *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
38  *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
40  *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
46  *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
47  *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49 #include "libfdt_env.h"
50 
51 #include <fdt.h>
52 #include <libfdt.h>
53 
54 #include "libfdt_internal.h"
55 
fdt_check_header(const void * fdt)56 int fdt_check_header(const void *fdt)
57 {
58 	if (fdt_magic(fdt) == FDT_MAGIC) {
59 		/* Complete tree */
60 		if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
61 			return -FDT_ERR_BADVERSION;
62 		if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
63 			return -FDT_ERR_BADVERSION;
64 	} else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
65 		/* Unfinished sequential-write blob */
66 		if (fdt_size_dt_struct(fdt) == 0)
67 			return -FDT_ERR_BADSTATE;
68 	} else {
69 		return -FDT_ERR_BADMAGIC;
70 	}
71 
72 	return 0;
73 }
74 
fdt_offset_ptr(const void * fdt,int offset,unsigned int len)75 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
76 {
77 	const char *p;
78 
79 	if (fdt_version(fdt) >= 0x11)
80 		if (((offset + len) < offset)
81 		    || ((offset + len) > fdt_size_dt_struct(fdt)))
82 			return NULL;
83 
84 	p = _fdt_offset_ptr(fdt, offset);
85 
86 	if (p + len < p)
87 		return NULL;
88 	return p;
89 }
90 
fdt_next_tag(const void * fdt,int startoffset,int * nextoffset)91 uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
92 {
93 	const fdt32_t *tagp, *lenp;
94 	uint32_t tag;
95 	int offset = startoffset;
96 	const char *p;
97 
98 	*nextoffset = -FDT_ERR_TRUNCATED;
99 	tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
100 	if (!tagp)
101 		return FDT_END; /* premature end */
102 	tag = fdt32_to_cpu(*tagp);
103 	offset += FDT_TAGSIZE;
104 
105 	*nextoffset = -FDT_ERR_BADSTRUCTURE;
106 	switch (tag) {
107 	case FDT_BEGIN_NODE:
108 		/* skip name */
109 		do {
110 			p = fdt_offset_ptr(fdt, offset++, 1);
111 		} while (p && (*p != '\0'));
112 		if (!p)
113 			return FDT_END; /* premature end */
114 		break;
115 
116 	case FDT_PROP:
117 		lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
118 		if (!lenp)
119 			return FDT_END; /* premature end */
120 		/* skip-name offset, length and value */
121 		offset += sizeof(struct fdt_property) - FDT_TAGSIZE
122 			+ fdt32_to_cpu(*lenp);
123 		break;
124 
125 	case FDT_END:
126 	case FDT_END_NODE:
127 	case FDT_NOP:
128 		break;
129 
130 	default:
131 		return FDT_END;
132 	}
133 
134 	if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
135 		return FDT_END; /* premature end */
136 
137 	*nextoffset = FDT_TAGALIGN(offset);
138 	return tag;
139 }
140 
_fdt_check_node_offset(const void * fdt,int offset)141 int _fdt_check_node_offset(const void *fdt, int offset)
142 {
143 	if ((offset < 0) || (offset % FDT_TAGSIZE)
144 	    || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE))
145 		return -FDT_ERR_BADOFFSET;
146 
147 	return offset;
148 }
149 
_fdt_check_prop_offset(const void * fdt,int offset)150 int _fdt_check_prop_offset(const void *fdt, int offset)
151 {
152 	if ((offset < 0) || (offset % FDT_TAGSIZE)
153 	    || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP))
154 		return -FDT_ERR_BADOFFSET;
155 
156 	return offset;
157 }
158 
fdt_next_node(const void * fdt,int offset,int * depth)159 int fdt_next_node(const void *fdt, int offset, int *depth)
160 {
161 	int nextoffset = 0;
162 	uint32_t tag;
163 
164 	if (offset >= 0)
165 		if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0)
166 			return nextoffset;
167 
168 	do {
169 		offset = nextoffset;
170 		tag = fdt_next_tag(fdt, offset, &nextoffset);
171 
172 		switch (tag) {
173 		case FDT_PROP:
174 		case FDT_NOP:
175 			break;
176 
177 		case FDT_BEGIN_NODE:
178 			if (depth)
179 				(*depth)++;
180 			break;
181 
182 		case FDT_END_NODE:
183 			if (depth && ((--(*depth)) < 0))
184 				return nextoffset;
185 			break;
186 
187 		case FDT_END:
188 			if ((nextoffset >= 0)
189 			    || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
190 				return -FDT_ERR_NOTFOUND;
191 			else
192 				return nextoffset;
193 		}
194 	} while (tag != FDT_BEGIN_NODE);
195 
196 	return offset;
197 }
198 
fdt_first_subnode(const void * fdt,int offset)199 int fdt_first_subnode(const void *fdt, int offset)
200 {
201 	int depth = 0;
202 
203 	offset = fdt_next_node(fdt, offset, &depth);
204 	if (offset < 0 || depth != 1)
205 		return -FDT_ERR_NOTFOUND;
206 
207 	return offset;
208 }
209 
fdt_next_subnode(const void * fdt,int offset)210 int fdt_next_subnode(const void *fdt, int offset)
211 {
212 	int depth = 1;
213 
214 	/*
215 	 * With respect to the parent, the depth of the next subnode will be
216 	 * the same as the last.
217 	 */
218 	do {
219 		offset = fdt_next_node(fdt, offset, &depth);
220 		if (offset < 0 || depth < 1)
221 			return -FDT_ERR_NOTFOUND;
222 	} while (depth > 1);
223 
224 	return offset;
225 }
226 
_fdt_find_string(const char * strtab,int tabsize,const char * s)227 const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
228 {
229 	int len = strlen(s) + 1;
230 	const char *last = strtab + tabsize - len;
231 	const char *p;
232 
233 	for (p = strtab; p <= last; p++)
234 		if (memcmp(p, s, len) == 0)
235 			return p;
236 	return NULL;
237 }
238 
fdt_move(const void * fdt,void * buf,int bufsize)239 int fdt_move(const void *fdt, void *buf, int bufsize)
240 {
241 	FDT_CHECK_HEADER(fdt);
242 
243 	if (fdt_totalsize(fdt) > bufsize)
244 		return -FDT_ERR_NOSPACE;
245 
246 	memmove(buf, fdt, fdt_totalsize(fdt));
247 	return 0;
248 }
249