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