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