xref: /DragonStub/apps/lib/libfdt/fdt.c (revision 3e6106c4d60a23aae3c0740979c5e6fb728b63c3)
1 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
2 /*
3  * libfdt - Flat Device Tree manipulation
4  * Copyright (C) 2006 David Gibson, IBM Corporation.
5  */
6 #include "libfdt_env.h"
7 
8 #include <fdt.h>
9 #include <libfdt.h>
10 #include <lib.h>
11 #include <dragonstub/dragonstub.h>
12 #include "libfdt_internal.h"
13 
14 /*
15  * Minimal sanity check for a read-only tree. fdt_ro_probe_() checks
16  * that the given buffer contains what appears to be a flattened
17  * device tree with sane information in its header.
18  */
fdt_ro_probe_(const void * fdt)19 int32_t fdt_ro_probe_(const void *fdt)
20 {
21 	uint32_t totalsize = fdt_totalsize(fdt);
22 
23 	if (can_assume(VALID_DTB))
24 		return totalsize;
25 
26 	/* The device tree must be at an 8-byte aligned address */
27 	if ((uintptr_t)fdt & 7)
28 		return -FDT_ERR_ALIGNMENT;
29 
30 	if (fdt_magic(fdt) == FDT_MAGIC) {
31 		/* Complete tree */
32 		if (!can_assume(LATEST)) {
33 			if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
34 				return -FDT_ERR_BADVERSION;
35 			if (fdt_last_comp_version(fdt) >
36 					FDT_LAST_SUPPORTED_VERSION)
37 				return -FDT_ERR_BADVERSION;
38 		}
39 	} else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
40 		/* Unfinished sequential-write blob */
41 		if (!can_assume(VALID_INPUT) && fdt_size_dt_struct(fdt) == 0)
42 			return -FDT_ERR_BADSTATE;
43 	} else {
44 		return -FDT_ERR_BADMAGIC;
45 	}
46 
47 	if (totalsize < INT32_MAX)
48 		return totalsize;
49 	else
50 		return -FDT_ERR_TRUNCATED;
51 }
52 
check_off_(uint32_t hdrsize,uint32_t totalsize,uint32_t off)53 static int check_off_(uint32_t hdrsize, uint32_t totalsize, uint32_t off)
54 {
55 	return (off >= hdrsize) && (off <= totalsize);
56 }
57 
check_block_(uint32_t hdrsize,uint32_t totalsize,uint32_t base,uint32_t size)58 static int check_block_(uint32_t hdrsize, uint32_t totalsize,
59 			uint32_t base, uint32_t size)
60 {
61 	if (!check_off_(hdrsize, totalsize, base))
62 		return 0; /* block start out of bounds */
63 	if ((base + size) < base)
64 		return 0; /* overflow */
65 	if (!check_off_(hdrsize, totalsize, base + size))
66 		return 0; /* block end out of bounds */
67 	return 1;
68 }
69 
fdt_header_size_(uint32_t version)70 size_t fdt_header_size_(uint32_t version)
71 {
72 	if (version <= 1)
73 		return FDT_V1_SIZE;
74 	else if (version <= 2)
75 		return FDT_V2_SIZE;
76 	else if (version <= 3)
77 		return FDT_V3_SIZE;
78 	else if (version <= 16)
79 		return FDT_V16_SIZE;
80 	else
81 		return FDT_V17_SIZE;
82 }
83 
fdt_header_size(const void * fdt)84 size_t fdt_header_size(const void *fdt)
85 {
86 	return can_assume(LATEST) ? FDT_V17_SIZE :
87 		fdt_header_size_(fdt_version(fdt));
88 }
89 
fdt_check_header(const void * fdt)90 int fdt_check_header(const void *fdt)
91 {
92 	size_t hdrsize;
93 
94 	/* The device tree must be at an 8-byte aligned address */
95 	if ((uintptr_t)fdt & 7)
96 		return -FDT_ERR_ALIGNMENT;
97 
98 	if (fdt_magic(fdt) != FDT_MAGIC)
99 		return -FDT_ERR_BADMAGIC;
100 	if (!can_assume(LATEST)) {
101 		if ((fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
102 		    || (fdt_last_comp_version(fdt) >
103 			FDT_LAST_SUPPORTED_VERSION))
104 			return -FDT_ERR_BADVERSION;
105 		if (fdt_version(fdt) < fdt_last_comp_version(fdt))
106 			return -FDT_ERR_BADVERSION;
107 	}
108 	hdrsize = fdt_header_size(fdt);
109 	if (!can_assume(VALID_DTB)) {
110 		if ((fdt_totalsize(fdt) < hdrsize)
111 		    || (fdt_totalsize(fdt) > INT_MAX))
112 			return -FDT_ERR_TRUNCATED;
113 
114 		/* Bounds check memrsv block */
115 		if (!check_off_(hdrsize, fdt_totalsize(fdt),
116 				fdt_off_mem_rsvmap(fdt)))
117 			return -FDT_ERR_TRUNCATED;
118 
119 		/* Bounds check structure block */
120 		if (!can_assume(LATEST) && fdt_version(fdt) < 17) {
121 			if (!check_off_(hdrsize, fdt_totalsize(fdt),
122 					fdt_off_dt_struct(fdt)))
123 				return -FDT_ERR_TRUNCATED;
124 		} else {
125 			if (!check_block_(hdrsize, fdt_totalsize(fdt),
126 					  fdt_off_dt_struct(fdt),
127 					  fdt_size_dt_struct(fdt)))
128 				return -FDT_ERR_TRUNCATED;
129 		}
130 
131 		/* Bounds check strings block */
132 		if (!check_block_(hdrsize, fdt_totalsize(fdt),
133 				  fdt_off_dt_strings(fdt),
134 				  fdt_size_dt_strings(fdt)))
135 			return -FDT_ERR_TRUNCATED;
136 	}
137 
138 	return 0;
139 }
140 
fdt_offset_ptr(const void * fdt,int offset,unsigned int len)141 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
142 {
143 	unsigned int uoffset = offset;
144 	unsigned int absoffset = offset + fdt_off_dt_struct(fdt);
145 
146 	if (offset < 0)
147 		return NULL;
148 
149 	if (!can_assume(VALID_INPUT))
150 		if ((absoffset < uoffset)
151 		    || ((absoffset + len) < absoffset)
152 		    || (absoffset + len) > fdt_totalsize(fdt))
153 			return NULL;
154 
155 	if (can_assume(LATEST) || fdt_version(fdt) >= 0x11)
156 		if (((uoffset + len) < uoffset)
157 		    || ((offset + len) > fdt_size_dt_struct(fdt)))
158 			return NULL;
159 
160 	return fdt_offset_ptr_(fdt, offset);
161 }
162 
fdt_next_tag(const void * fdt,int startoffset,int * nextoffset)163 uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
164 {
165 	const fdt32_t *tagp, *lenp;
166 	uint32_t tag, len, sum;
167 	int offset = startoffset;
168 	const char *p;
169 
170 	*nextoffset = -FDT_ERR_TRUNCATED;
171 	tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
172 	if (!can_assume(VALID_DTB) && !tagp)
173 		return FDT_END; /* premature end */
174 	tag = fdt32_to_cpu(*tagp);
175 	offset += FDT_TAGSIZE;
176 
177 	*nextoffset = -FDT_ERR_BADSTRUCTURE;
178 	switch (tag) {
179 	case FDT_BEGIN_NODE:
180 		/* skip name */
181 		do {
182 			p = fdt_offset_ptr(fdt, offset++, 1);
183 		} while (p && (*p != '\0'));
184 		if (!can_assume(VALID_DTB) && !p)
185 			return FDT_END; /* premature end */
186 		break;
187 
188 	case FDT_PROP:
189 		lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
190 		if (!can_assume(VALID_DTB) && !lenp)
191 			return FDT_END; /* premature end */
192 
193 		len = fdt32_to_cpu(*lenp);
194 		sum = len + offset;
195 		if (!can_assume(VALID_DTB) &&
196 		    (INT_MAX <= sum || sum < (uint32_t) offset))
197 			return FDT_END; /* premature end */
198 
199 		/* skip-name offset, length and value */
200 		offset += sizeof(struct fdt_property) - FDT_TAGSIZE + len;
201 
202 		if (!can_assume(LATEST) &&
203 		    fdt_version(fdt) < 0x10 && len >= 8 &&
204 		    ((offset - len) % 8) != 0)
205 			offset += 4;
206 		break;
207 
208 	case FDT_END:
209 	case FDT_END_NODE:
210 	case FDT_NOP:
211 		break;
212 
213 	default:
214 		return FDT_END;
215 	}
216 
217 	if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
218 		return FDT_END; /* premature end */
219 
220 	*nextoffset = FDT_TAGALIGN(offset);
221 	return tag;
222 }
223 
fdt_check_node_offset_(const void * fdt,int offset)224 int fdt_check_node_offset_(const void *fdt, int offset)
225 {
226 	if (!can_assume(VALID_INPUT)
227 	    && ((offset < 0) || (offset % FDT_TAGSIZE)))
228 		return -FDT_ERR_BADOFFSET;
229 
230 	if (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE)
231 		return -FDT_ERR_BADOFFSET;
232 
233 	return offset;
234 }
235 
fdt_check_prop_offset_(const void * fdt,int offset)236 int fdt_check_prop_offset_(const void *fdt, int offset)
237 {
238 	if (!can_assume(VALID_INPUT)
239 	    && ((offset < 0) || (offset % FDT_TAGSIZE)))
240 		return -FDT_ERR_BADOFFSET;
241 
242 	if (fdt_next_tag(fdt, offset, &offset) != FDT_PROP)
243 		return -FDT_ERR_BADOFFSET;
244 
245 	return offset;
246 }
247 
fdt_next_node(const void * fdt,int offset,int * depth)248 int fdt_next_node(const void *fdt, int offset, int *depth)
249 {
250 	int nextoffset = 0;
251 	uint32_t tag;
252 
253 	if (offset >= 0)
254 		if ((nextoffset = fdt_check_node_offset_(fdt, offset)) < 0)
255 			return nextoffset;
256 
257 	do {
258 		offset = nextoffset;
259 		tag = fdt_next_tag(fdt, offset, &nextoffset);
260 
261 		switch (tag) {
262 		case FDT_PROP:
263 		case FDT_NOP:
264 			break;
265 
266 		case FDT_BEGIN_NODE:
267 			if (depth)
268 				(*depth)++;
269 			break;
270 
271 		case FDT_END_NODE:
272 			if (depth && ((--(*depth)) < 0))
273 				return nextoffset;
274 			break;
275 
276 		case FDT_END:
277 			if ((nextoffset >= 0)
278 			    || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
279 				return -FDT_ERR_NOTFOUND;
280 			else
281 				return nextoffset;
282 		}
283 	} while (tag != FDT_BEGIN_NODE);
284 
285 	return offset;
286 }
287 
fdt_first_subnode(const void * fdt,int offset)288 int fdt_first_subnode(const void *fdt, int offset)
289 {
290 	int depth = 0;
291 
292 	offset = fdt_next_node(fdt, offset, &depth);
293 	if (offset < 0 || depth != 1)
294 		return -FDT_ERR_NOTFOUND;
295 
296 	return offset;
297 }
298 
fdt_next_subnode(const void * fdt,int offset)299 int fdt_next_subnode(const void *fdt, int offset)
300 {
301 	int depth = 1;
302 
303 	/*
304 	 * With respect to the parent, the depth of the next subnode will be
305 	 * the same as the last.
306 	 */
307 	do {
308 		offset = fdt_next_node(fdt, offset, &depth);
309 		if (offset < 0 || depth < 1)
310 			return -FDT_ERR_NOTFOUND;
311 	} while (depth > 1);
312 
313 	return offset;
314 }
315 
fdt_find_string_(const char * strtab,int tabsize,const char * s)316 const char *fdt_find_string_(const char *strtab, int tabsize, const char *s)
317 {
318 	int len = strlen(s) + 1;
319 	const char *last = strtab + tabsize - len;
320 	const char *p;
321 
322 	for (p = strtab; p <= last; p++)
323 		if (memcmp(p, s, len) == 0)
324 			return p;
325 	return NULL;
326 }
327 
fdt_move(const void * fdt,void * buf,int bufsize)328 int fdt_move(const void *fdt, void *buf, int bufsize)
329 {
330 	if (!can_assume(VALID_INPUT) && bufsize < 0)
331 		return -FDT_ERR_NOSPACE;
332 
333 	FDT_RO_PROBE(fdt);
334 
335 	if (fdt_totalsize(fdt) > (unsigned int)bufsize)
336 		return -FDT_ERR_NOSPACE;
337 
338 	memmove(buf, fdt, fdt_totalsize(fdt));
339 	return 0;
340 }
341