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 	unsigned absoffset = offset + fdt_off_dt_struct(fdt);
78 
79 	if ((absoffset < offset)
80 	    || ((absoffset + len) < absoffset)
81 	    || (absoffset + len) > fdt_totalsize(fdt))
82 		return NULL;
83 
84 	if (fdt_version(fdt) >= 0x11)
85 		if (((offset + len) < offset)
86 		    || ((offset + len) > fdt_size_dt_struct(fdt)))
87 			return NULL;
88 
89 	return _fdt_offset_ptr(fdt, offset);
90 }
91 
fdt_next_tag(const void * fdt,int startoffset,int * nextoffset)92 uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
93 {
94 	const fdt32_t *tagp, *lenp;
95 	uint32_t tag;
96 	int offset = startoffset;
97 	const char *p;
98 
99 	*nextoffset = -FDT_ERR_TRUNCATED;
100 	tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
101 	if (!tagp)
102 		return FDT_END; /* premature end */
103 	tag = fdt32_to_cpu(*tagp);
104 	offset += FDT_TAGSIZE;
105 
106 	*nextoffset = -FDT_ERR_BADSTRUCTURE;
107 	switch (tag) {
108 	case FDT_BEGIN_NODE:
109 		/* skip name */
110 		do {
111 			p = fdt_offset_ptr(fdt, offset++, 1);
112 		} while (p && (*p != '\0'));
113 		if (!p)
114 			return FDT_END; /* premature end */
115 		break;
116 
117 	case FDT_PROP:
118 		lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
119 		if (!lenp)
120 			return FDT_END; /* premature end */
121 		/* skip-name offset, length and value */
122 		offset += sizeof(struct fdt_property) - FDT_TAGSIZE
123 			+ fdt32_to_cpu(*lenp);
124 		break;
125 
126 	case FDT_END:
127 	case FDT_END_NODE:
128 	case FDT_NOP:
129 		break;
130 
131 	default:
132 		return FDT_END;
133 	}
134 
135 	if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
136 		return FDT_END; /* premature end */
137 
138 	*nextoffset = FDT_TAGALIGN(offset);
139 	return tag;
140 }
141 
_fdt_check_node_offset(const void * fdt,int offset)142 int _fdt_check_node_offset(const void *fdt, int offset)
143 {
144 	if ((offset < 0) || (offset % FDT_TAGSIZE)
145 	    || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE))
146 		return -FDT_ERR_BADOFFSET;
147 
148 	return offset;
149 }
150 
_fdt_check_prop_offset(const void * fdt,int offset)151 int _fdt_check_prop_offset(const void *fdt, int offset)
152 {
153 	if ((offset < 0) || (offset % FDT_TAGSIZE)
154 	    || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP))
155 		return -FDT_ERR_BADOFFSET;
156 
157 	return offset;
158 }
159 
fdt_next_node(const void * fdt,int offset,int * depth)160 int fdt_next_node(const void *fdt, int offset, int *depth)
161 {
162 	int nextoffset = 0;
163 	uint32_t tag;
164 
165 	if (offset >= 0)
166 		if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0)
167 			return nextoffset;
168 
169 	do {
170 		offset = nextoffset;
171 		tag = fdt_next_tag(fdt, offset, &nextoffset);
172 
173 		switch (tag) {
174 		case FDT_PROP:
175 		case FDT_NOP:
176 			break;
177 
178 		case FDT_BEGIN_NODE:
179 			if (depth)
180 				(*depth)++;
181 			break;
182 
183 		case FDT_END_NODE:
184 			if (depth && ((--(*depth)) < 0))
185 				return nextoffset;
186 			break;
187 
188 		case FDT_END:
189 			if ((nextoffset >= 0)
190 			    || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
191 				return -FDT_ERR_NOTFOUND;
192 			else
193 				return nextoffset;
194 		}
195 	} while (tag != FDT_BEGIN_NODE);
196 
197 	return offset;
198 }
199 
fdt_first_subnode(const void * fdt,int offset)200 int fdt_first_subnode(const void *fdt, int offset)
201 {
202 	int depth = 0;
203 
204 	offset = fdt_next_node(fdt, offset, &depth);
205 	if (offset < 0 || depth != 1)
206 		return -FDT_ERR_NOTFOUND;
207 
208 	return offset;
209 }
210 
fdt_next_subnode(const void * fdt,int offset)211 int fdt_next_subnode(const void *fdt, int offset)
212 {
213 	int depth = 1;
214 
215 	/*
216 	 * With respect to the parent, the depth of the next subnode will be
217 	 * the same as the last.
218 	 */
219 	do {
220 		offset = fdt_next_node(fdt, offset, &depth);
221 		if (offset < 0 || depth < 1)
222 			return -FDT_ERR_NOTFOUND;
223 	} while (depth > 1);
224 
225 	return offset;
226 }
227 
_fdt_find_string(const char * strtab,int tabsize,const char * s)228 const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
229 {
230 	int len = strlen(s) + 1;
231 	const char *last = strtab + tabsize - len;
232 	const char *p;
233 
234 	for (p = strtab; p <= last; p++)
235 		if (memcmp(p, s, len) == 0)
236 			return p;
237 	return NULL;
238 }
239 
fdt_move(const void * fdt,void * buf,int bufsize)240 int fdt_move(const void *fdt, void *buf, int bufsize)
241 {
242 	FDT_CHECK_HEADER(fdt);
243 
244 	if (fdt_totalsize(fdt) > bufsize)
245 		return -FDT_ERR_NOSPACE;
246 
247 	memmove(buf, fdt, fdt_totalsize(fdt));
248 	return 0;
249 }
250