blob: f8840839502b94ea3d17bd240c474fc662f0be9f [file] [log] [blame]
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#include "libfdt_env.h"
20
21#include <fdt.h>
22#include <libfdt.h>
23
24#include "libfdt_internal.h"
25
26#define CHECK_HEADER(fdt) \
27 { \
28 int err; \
29 if ((err = _fdt_check_header(fdt)) != 0) \
30 return err; \
31 }
32
33static int offset_streq(const void *fdt, int offset,
34 const char *s, int len)
35{
36 const char *p = fdt_offset_ptr(fdt, offset, len+1);
37
38 if (! p)
39 /* short match */
40 return 0;
41
42 if (memcmp(p, s, len) != 0)
43 return 0;
44
45 if (p[len] != '\0')
46 return 0;
47
48 return 1;
49}
50
Gerald Van Barenca8d8a62007-03-31 12:13:43 -040051/*
52 * Return a pointer to the string at the given string offset.
53 */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040054char *fdt_string(const void *fdt, int stroffset)
55{
56 return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
57}
58
Gerald Van Barenca8d8a62007-03-31 12:13:43 -040059/*
60 * Return the node offset of the node specified by:
61 * parentoffset - starting place (0 to start at the root)
62 * name - name being searched for
63 * namelen - length of the name: typically strlen(name)
64 *
65 * Notes:
66 * If the start node has subnodes, the subnodes are _not_ searched for the
67 * requested name.
68 */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040069int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
70 const char *name, int namelen)
71{
72 int level = 0;
73 uint32_t tag;
74 int offset, nextoffset;
75
76 CHECK_HEADER(fdt);
77
Gerald Van Barenca8d8a62007-03-31 12:13:43 -040078 tag = fdt_next_tag(fdt, parentoffset, &nextoffset, NULL);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040079 if (tag != FDT_BEGIN_NODE)
80 return -FDT_ERR_BADOFFSET;
81
82 do {
83 offset = nextoffset;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -040084 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040085
86 switch (tag) {
87 case FDT_END:
88 return -FDT_ERR_TRUNCATED;
89
90 case FDT_BEGIN_NODE:
91 level++;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -040092 /*
93 * If we are nested down levels, ignore the strings
94 * until we get back to the proper level.
95 */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040096 if (level != 1)
97 continue;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -040098
99 /* Return the offset if this is "our" string. */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400100 if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen))
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400101 return offset;
102 break;
103
104 case FDT_END_NODE:
105 level--;
106 break;
107
108 case FDT_PROP:
109 case FDT_NOP:
110 break;
111
112 default:
113 return -FDT_ERR_BADSTRUCTURE;
114 }
115 } while (level >= 0);
116
117 return -FDT_ERR_NOTFOUND;
118}
119
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400120/*
121 * See fdt_subnode_offset_namelen()
122 */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400123int fdt_subnode_offset(const void *fdt, int parentoffset,
124 const char *name)
125{
126 return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
127}
128
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400129/*
130 * Searches for the node corresponding to the given path and returns the
131 * offset of that node.
132 */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400133int fdt_path_offset(const void *fdt, const char *path)
134{
135 const char *end = path + strlen(path);
136 const char *p = path;
137 int offset = 0;
138
139 CHECK_HEADER(fdt);
140
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400141 /* Paths must be absolute */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400142 if (*path != '/')
143 return -FDT_ERR_BADPATH;
144
145 while (*p) {
146 const char *q;
147
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400148 /* Skip path separator(s) */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400149 while (*p == '/')
150 p++;
151 if (! *p)
152 return -FDT_ERR_BADPATH;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400153
154 /*
155 * Find the next path separator. The characters between
156 * p and q are the next segment of the the path to find.
157 */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400158 q = strchr(p, '/');
159 if (! q)
160 q = end;
161
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400162 /*
163 * Find the offset corresponding to the this path segment.
164 */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400165 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400166
167 /* Oops, error, abort abort abort */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400168 if (offset < 0)
169 return offset;
170
171 p = q;
172 }
173
174 return offset;
175}
176
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400177/*
178 * Given the offset of a node and a name of a property in that node, return
179 * a pointer to the property struct.
180 */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400181struct fdt_property *fdt_get_property(const void *fdt,
182 int nodeoffset,
183 const char *name, int *lenp)
184{
185 int level = 0;
186 uint32_t tag;
187 struct fdt_property *prop;
188 int namestroff;
189 int offset, nextoffset;
190 int err;
191
192 if ((err = _fdt_check_header(fdt)) != 0)
193 goto fail;
194
195 err = -FDT_ERR_BADOFFSET;
196 if (nodeoffset % FDT_TAGSIZE)
197 goto fail;
198
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400199 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400200 if (tag != FDT_BEGIN_NODE)
201 goto fail;
202
203 do {
204 offset = nextoffset;
205
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400206 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400207 switch (tag) {
208 case FDT_END:
209 err = -FDT_ERR_TRUNCATED;
210 goto fail;
211
212 case FDT_BEGIN_NODE:
213 level++;
214 break;
215
216 case FDT_END_NODE:
217 level--;
218 break;
219
220 case FDT_PROP:
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400221 /*
222 * If we are nested down levels, ignore the strings
223 * until we get back to the proper level.
224 */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400225 if (level != 0)
226 continue;
227
228 err = -FDT_ERR_BADSTRUCTURE;
229 prop = fdt_offset_ptr_typed(fdt, offset, prop);
230 if (! prop)
231 goto fail;
232 namestroff = fdt32_to_cpu(prop->nameoff);
233 if (streq(fdt_string(fdt, namestroff), name)) {
234 /* Found it! */
235 int len = fdt32_to_cpu(prop->len);
236 prop = fdt_offset_ptr(fdt, offset,
237 sizeof(*prop)+len);
238 if (! prop)
239 goto fail;
240
241 if (lenp)
242 *lenp = len;
243
244 return prop;
245 }
246 break;
247
248 case FDT_NOP:
249 break;
250
251 default:
252 err = -FDT_ERR_BADSTRUCTURE;
253 goto fail;
254 }
255 } while (level >= 0);
256
257 err = -FDT_ERR_NOTFOUND;
258 fail:
259 if (lenp)
260 *lenp = err;
261 return NULL;
262}
263
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400264/*
265 * Given the offset of a node and a name of a property in that node, return
266 * a pointer to the property data (ONLY).
267 */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400268void *fdt_getprop(const void *fdt, int nodeoffset,
269 const char *name, int *lenp)
270{
271 const struct fdt_property *prop;
272
273 prop = fdt_get_property(fdt, nodeoffset, name, lenp);
274 if (! prop)
275 return NULL;
276
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400277 return (void *)prop->data;
278}
279
280
281uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset, char **namep)
282{
283 const uint32_t *tagp, *lenp;
284 uint32_t tag;
285 const char *p;
286
287 if (offset % FDT_TAGSIZE)
288 return -1;
289
290 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
291 if (! tagp)
292 return FDT_END; /* premature end */
293 tag = fdt32_to_cpu(*tagp);
294 offset += FDT_TAGSIZE;
295
296 switch (tag) {
297 case FDT_BEGIN_NODE:
298 if(namep)
299 *namep = fdt_offset_ptr(fdt, offset, 1);
300
301 /* skip name */
302 do {
303 p = fdt_offset_ptr(fdt, offset++, 1);
304 } while (p && (*p != '\0'));
305 if (! p)
306 return FDT_END;
307 break;
308 case FDT_PROP:
309 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
310 if (! lenp)
311 return FDT_END;
312 /*
313 * Get the property and set the namep to the name.
314 */
315 if(namep) {
316 struct fdt_property *prop;
317
318 prop = fdt_offset_ptr_typed(fdt, offset - FDT_TAGSIZE, prop);
319 if (! prop)
320 return -FDT_ERR_BADSTRUCTURE;
321 *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
322 }
323 /* skip name offset, length and value */
324 offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
325 break;
326 }
327
328 if (nextoffset)
329 *nextoffset = ALIGN(offset, FDT_TAGSIZE);
330
331 return tag;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400332}
Gerald Van Barenca8d8a62007-03-31 12:13:43 -0400333