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