xref: /optee_os/core/lib/libfdt/fdt.c (revision 5a913ee74d3c71af2a2860ce8a4e7aeab2916f9b)
1 // SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0+)
2 /*
3  * libfdt - Flat Device Tree manipulation
4  * Copyright (C) 2006 David Gibson, IBM Corporation.
5  *
6  * libfdt is dual licensed: you can use it either under the terms of
7  * the GPL, or the BSD license, at your option.
8  *
9  *  a) This library is free software; you can redistribute it and/or
10  *     modify it under the terms of the GNU General Public License as
11  *     published by the Free Software Foundation; either version 2 of the
12  *     License, or (at your option) any later version.
13  *
14  *     This library is distributed in the hope that it will be useful,
15  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *     GNU General Public License for more details.
18  *
19  *     You should have received a copy of the GNU General Public
20  *     License along with this library; if not, write to the Free
21  *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
22  *     MA 02110-1301 USA
23  *
24  * Alternatively,
25  *
26  *  b) Redistribution and use in source and binary forms, with or
27  *     without modification, are permitted provided that the following
28  *     conditions are met:
29  *
30  *     1. Redistributions of source code must retain the above
31  *        copyright notice, this list of conditions and the following
32  *        disclaimer.
33  *     2. Redistributions in binary form must reproduce the above
34  *        copyright notice, this list of conditions and the following
35  *        disclaimer in the documentation and/or other materials
36  *        provided with the distribution.
37  *
38  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
39  *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
40  *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
41  *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
43  *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
49  *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
50  *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51  */
52 #include "libfdt_env.h"
53 
54 #include <fdt.h>
55 #include <libfdt.h>
56 
57 #include "libfdt_internal.h"
58 
59 int fdt_check_header(const void *fdt)
60 {
61 	if (fdt_magic(fdt) == FDT_MAGIC) {
62 		/* Complete tree */
63 		if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
64 			return -FDT_ERR_BADVERSION;
65 		if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
66 			return -FDT_ERR_BADVERSION;
67 	} else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
68 		/* Unfinished sequential-write blob */
69 		if (fdt_size_dt_struct(fdt) == 0)
70 			return -FDT_ERR_BADSTATE;
71 	} else {
72 		return -FDT_ERR_BADMAGIC;
73 	}
74 
75 	return 0;
76 }
77 
78 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
79 {
80 	unsigned absoffset = offset + fdt_off_dt_struct(fdt);
81 
82 	if ((absoffset < offset)
83 	    || ((absoffset + len) < absoffset)
84 	    || (absoffset + len) > fdt_totalsize(fdt))
85 		return NULL;
86 
87 	if (fdt_version(fdt) >= 0x11)
88 		if (((offset + len) < offset)
89 		    || ((offset + len) > fdt_size_dt_struct(fdt)))
90 			return NULL;
91 
92 	return fdt_offset_ptr_(fdt, offset);
93 }
94 
95 uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
96 {
97 	const fdt32_t *tagp, *lenp;
98 	uint32_t tag;
99 	int offset = startoffset;
100 	const char *p;
101 
102 	*nextoffset = -FDT_ERR_TRUNCATED;
103 	tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
104 	if (!tagp)
105 		return FDT_END; /* premature end */
106 	tag = fdt32_to_cpu(*tagp);
107 	offset += FDT_TAGSIZE;
108 
109 	*nextoffset = -FDT_ERR_BADSTRUCTURE;
110 	switch (tag) {
111 	case FDT_BEGIN_NODE:
112 		/* skip name */
113 		do {
114 			p = fdt_offset_ptr(fdt, offset++, 1);
115 		} while (p && (*p != '\0'));
116 		if (!p)
117 			return FDT_END; /* premature end */
118 		break;
119 
120 	case FDT_PROP:
121 		lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
122 		if (!lenp)
123 			return FDT_END; /* premature end */
124 		/* skip-name offset, length and value */
125 		offset += sizeof(struct fdt_property) - FDT_TAGSIZE
126 			+ fdt32_to_cpu(*lenp);
127 		if (fdt_version(fdt) < 0x10 && fdt32_to_cpu(*lenp) >= 8 &&
128 		    ((offset - fdt32_to_cpu(*lenp)) % 8) != 0)
129 			offset += 4;
130 		break;
131 
132 	case FDT_END:
133 	case FDT_END_NODE:
134 	case FDT_NOP:
135 		break;
136 
137 	default:
138 		return FDT_END;
139 	}
140 
141 	if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
142 		return FDT_END; /* premature end */
143 
144 	*nextoffset = FDT_TAGALIGN(offset);
145 	return tag;
146 }
147 
148 int fdt_check_node_offset_(const void *fdt, int offset)
149 {
150 	if ((offset < 0) || (offset % FDT_TAGSIZE)
151 	    || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE))
152 		return -FDT_ERR_BADOFFSET;
153 
154 	return offset;
155 }
156 
157 int fdt_check_prop_offset_(const void *fdt, int offset)
158 {
159 	if ((offset < 0) || (offset % FDT_TAGSIZE)
160 	    || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP))
161 		return -FDT_ERR_BADOFFSET;
162 
163 	return offset;
164 }
165 
166 int fdt_next_node(const void *fdt, int offset, int *depth)
167 {
168 	int nextoffset = 0;
169 	uint32_t tag;
170 
171 	if (offset >= 0)
172 		if ((nextoffset = fdt_check_node_offset_(fdt, offset)) < 0)
173 			return nextoffset;
174 
175 	do {
176 		offset = nextoffset;
177 		tag = fdt_next_tag(fdt, offset, &nextoffset);
178 
179 		switch (tag) {
180 		case FDT_PROP:
181 		case FDT_NOP:
182 			break;
183 
184 		case FDT_BEGIN_NODE:
185 			if (depth)
186 				(*depth)++;
187 			break;
188 
189 		case FDT_END_NODE:
190 			if (depth && ((--(*depth)) < 0))
191 				return nextoffset;
192 			break;
193 
194 		case FDT_END:
195 			if ((nextoffset >= 0)
196 			    || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
197 				return -FDT_ERR_NOTFOUND;
198 			else
199 				return nextoffset;
200 		}
201 	} while (tag != FDT_BEGIN_NODE);
202 
203 	return offset;
204 }
205 
206 int fdt_first_subnode(const void *fdt, int offset)
207 {
208 	int depth = 0;
209 
210 	offset = fdt_next_node(fdt, offset, &depth);
211 	if (offset < 0 || depth != 1)
212 		return -FDT_ERR_NOTFOUND;
213 
214 	return offset;
215 }
216 
217 int fdt_next_subnode(const void *fdt, int offset)
218 {
219 	int depth = 1;
220 
221 	/*
222 	 * With respect to the parent, the depth of the next subnode will be
223 	 * the same as the last.
224 	 */
225 	do {
226 		offset = fdt_next_node(fdt, offset, &depth);
227 		if (offset < 0 || depth < 1)
228 			return -FDT_ERR_NOTFOUND;
229 	} while (depth > 1);
230 
231 	return offset;
232 }
233 
234 const char *fdt_find_string_(const char *strtab, int tabsize, const char *s)
235 {
236 	int len = strlen(s) + 1;
237 	const char *last = strtab + tabsize - len;
238 	const char *p;
239 
240 	for (p = strtab; p <= last; p++)
241 		if (memcmp(p, s, len) == 0)
242 			return p;
243 	return NULL;
244 }
245 
246 int fdt_move(const void *fdt, void *buf, int bufsize)
247 {
248 	FDT_CHECK_HEADER(fdt);
249 
250 	if (fdt_totalsize(fdt) > bufsize)
251 		return -FDT_ERR_NOSPACE;
252 
253 	memmove(buf, fdt, fdt_totalsize(fdt));
254 	return 0;
255 }
256