Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 1 | /* |
| 2 | * libfdt - Flat Device Tree manipulation |
| 3 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
| 4 | * |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 5 | * libfdt is dual licensed: you can use it either under the terms of |
| 6 | * the GPL, or the BSD license, at your option. |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 7 | * |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 8 | * a) This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of the |
| 11 | * License, or (at your option) any later version. |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 12 | * |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public |
| 19 | * License along with this library; if not, write to the Free |
| 20 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
| 21 | * MA 02110-1301 USA |
| 22 | * |
| 23 | * Alternatively, |
| 24 | * |
| 25 | * b) Redistribution and use in source and binary forms, with or |
| 26 | * without modification, are permitted provided that the following |
| 27 | * conditions are met: |
| 28 | * |
| 29 | * 1. Redistributions of source code must retain the above |
| 30 | * copyright notice, this list of conditions and the following |
| 31 | * disclaimer. |
| 32 | * 2. Redistributions in binary form must reproduce the above |
| 33 | * copyright notice, this list of conditions and the following |
| 34 | * disclaimer in the documentation and/or other materials |
| 35 | * provided with the distribution. |
| 36 | * |
| 37 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 38 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 39 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 40 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 41 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 42 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 44 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 45 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 47 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 48 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 49 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 50 | */ |
| 51 | #include "libfdt_env.h" |
| 52 | |
Bartlomiej Sieka | 8cf3080 | 2008-02-29 16:00:24 +0100 | [diff] [blame] | 53 | #ifndef USE_HOSTCC |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 54 | #include <fdt.h> |
| 55 | #include <libfdt.h> |
Bartlomiej Sieka | 8cf3080 | 2008-02-29 16:00:24 +0100 | [diff] [blame] | 56 | #else |
| 57 | #include "fdt_host.h" |
| 58 | #endif |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 59 | |
| 60 | #include "libfdt_internal.h" |
| 61 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame^] | 62 | static int _fdt_nodename_eq(const void *fdt, int offset, |
| 63 | const char *s, int len) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 64 | { |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 65 | const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 66 | |
| 67 | if (! p) |
| 68 | /* short match */ |
| 69 | return 0; |
| 70 | |
| 71 | if (memcmp(p, s, len) != 0) |
| 72 | return 0; |
| 73 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 74 | if (p[len] == '\0') |
| 75 | return 1; |
| 76 | else if (!memchr(s, '@', len) && (p[len] == '@')) |
| 77 | return 1; |
| 78 | else |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 79 | return 0; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 82 | const char *fdt_string(const void *fdt, int stroffset) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 83 | { |
David Gibson | c668302 | 2008-07-07 10:14:15 +1000 | [diff] [blame] | 84 | return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 85 | } |
| 86 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 87 | int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size) |
| 88 | { |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame^] | 89 | FDT_CHECK_HEADER(fdt); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 90 | *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address); |
| 91 | *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | int fdt_num_mem_rsv(const void *fdt) |
| 96 | { |
| 97 | int i = 0; |
| 98 | |
| 99 | while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0) |
| 100 | i++; |
| 101 | return i; |
| 102 | } |
| 103 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 104 | int fdt_subnode_offset_namelen(const void *fdt, int offset, |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 105 | const char *name, int namelen) |
| 106 | { |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 107 | int depth; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 108 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame^] | 109 | FDT_CHECK_HEADER(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 110 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 111 | for (depth = 0; |
| 112 | offset >= 0; |
| 113 | offset = fdt_next_node(fdt, offset, &depth)) { |
| 114 | if (depth < 0) |
| 115 | return -FDT_ERR_NOTFOUND; |
| 116 | else if ((depth == 1) |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame^] | 117 | && _fdt_nodename_eq(fdt, offset, name, namelen)) |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 118 | return offset; |
| 119 | } |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 120 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 121 | return offset; /* error */ |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | int fdt_subnode_offset(const void *fdt, int parentoffset, |
| 125 | const char *name) |
| 126 | { |
| 127 | return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name)); |
| 128 | } |
| 129 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 130 | int fdt_path_offset(const void *fdt, const char *path) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 131 | { |
| 132 | const char *end = path + strlen(path); |
| 133 | const char *p = path; |
| 134 | int offset = 0; |
| 135 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame^] | 136 | FDT_CHECK_HEADER(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 137 | |
| 138 | if (*path != '/') |
| 139 | return -FDT_ERR_BADPATH; |
| 140 | |
| 141 | while (*p) { |
| 142 | const char *q; |
| 143 | |
| 144 | while (*p == '/') |
| 145 | p++; |
| 146 | if (! *p) |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 147 | return offset; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 148 | q = strchr(p, '/'); |
| 149 | if (! q) |
| 150 | q = end; |
| 151 | |
| 152 | offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p); |
| 153 | if (offset < 0) |
| 154 | return offset; |
| 155 | |
| 156 | p = q; |
| 157 | } |
| 158 | |
Gerald Van Baren | aea03c4 | 2007-03-31 14:30:53 -0400 | [diff] [blame] | 159 | return offset; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 162 | const char *fdt_get_name(const void *fdt, int nodeoffset, int *len) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 163 | { |
David Gibson | 2f08bfa | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 164 | const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 165 | int err; |
| 166 | |
David Gibson | 2f08bfa | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 167 | if (((err = fdt_check_header(fdt)) != 0) |
| 168 | || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0)) |
| 169 | goto fail; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 170 | |
| 171 | if (len) |
| 172 | *len = strlen(nh->name); |
| 173 | |
| 174 | return nh->name; |
| 175 | |
| 176 | fail: |
| 177 | if (len) |
| 178 | *len = err; |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | const struct fdt_property *fdt_get_property(const void *fdt, |
| 183 | int nodeoffset, |
| 184 | const char *name, int *lenp) |
| 185 | { |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 186 | uint32_t tag; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 187 | const struct fdt_property *prop; |
| 188 | int namestroff; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 189 | int offset, nextoffset; |
| 190 | int err; |
| 191 | |
David Gibson | 2f08bfa | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 192 | if (((err = fdt_check_header(fdt)) != 0) |
| 193 | || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0)) |
| 194 | goto fail; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 195 | |
David Gibson | 2f08bfa | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 196 | nextoffset = err; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 197 | do { |
| 198 | offset = nextoffset; |
| 199 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 200 | tag = fdt_next_tag(fdt, offset, &nextoffset); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 201 | switch (tag) { |
| 202 | case FDT_END: |
| 203 | err = -FDT_ERR_TRUNCATED; |
| 204 | goto fail; |
| 205 | |
| 206 | case FDT_BEGIN_NODE: |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 207 | case FDT_END_NODE: |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 208 | case FDT_NOP: |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 209 | break; |
| 210 | |
| 211 | case FDT_PROP: |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 212 | err = -FDT_ERR_BADSTRUCTURE; |
| 213 | prop = fdt_offset_ptr(fdt, offset, sizeof(*prop)); |
| 214 | if (! prop) |
Gerald Van Baren | 9675ee7 | 2007-05-17 23:54:36 -0400 | [diff] [blame] | 215 | goto fail; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 216 | namestroff = fdt32_to_cpu(prop->nameoff); |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame^] | 217 | if (strcmp(fdt_string(fdt, namestroff), name) == 0) { |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 218 | /* Found it! */ |
| 219 | int len = fdt32_to_cpu(prop->len); |
| 220 | prop = fdt_offset_ptr(fdt, offset, |
| 221 | sizeof(*prop)+len); |
| 222 | if (! prop) |
| 223 | goto fail; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 224 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 225 | if (lenp) |
| 226 | *lenp = len; |
| 227 | |
| 228 | return prop; |
| 229 | } |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 230 | break; |
| 231 | |
| 232 | default: |
| 233 | err = -FDT_ERR_BADSTRUCTURE; |
| 234 | goto fail; |
| 235 | } |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 236 | } while ((tag != FDT_BEGIN_NODE) && (tag != FDT_END_NODE)); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 237 | |
| 238 | err = -FDT_ERR_NOTFOUND; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 239 | fail: |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 240 | if (lenp) |
| 241 | *lenp = err; |
| 242 | return NULL; |
| 243 | } |
| 244 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 245 | const void *fdt_getprop(const void *fdt, int nodeoffset, |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 246 | const char *name, int *lenp) |
| 247 | { |
| 248 | const struct fdt_property *prop; |
| 249 | |
| 250 | prop = fdt_get_property(fdt, nodeoffset, name, lenp); |
| 251 | if (! prop) |
| 252 | return NULL; |
| 253 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 254 | return prop->data; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 255 | } |
Gerald Van Baren | 3af0d58 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 256 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 257 | uint32_t fdt_get_phandle(const void *fdt, int nodeoffset) |
Gerald Van Baren | 3af0d58 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 258 | { |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 259 | const uint32_t *php; |
| 260 | int len; |
| 261 | |
| 262 | php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len); |
| 263 | if (!php || (len != sizeof(*php))) |
| 264 | return 0; |
| 265 | |
| 266 | return fdt32_to_cpu(*php); |
| 267 | } |
| 268 | |
| 269 | int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen) |
| 270 | { |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 271 | int pdepth = 0, p = 0; |
| 272 | int offset, depth, namelen; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 273 | const char *name; |
Gerald Van Baren | 3af0d58 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 274 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame^] | 275 | FDT_CHECK_HEADER(fdt); |
Gerald Van Baren | 3af0d58 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 276 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 277 | if (buflen < 2) |
Gerald Van Baren | 3f9f08c | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 278 | return -FDT_ERR_NOSPACE; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 279 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 280 | for (offset = 0, depth = 0; |
| 281 | (offset >= 0) && (offset <= nodeoffset); |
| 282 | offset = fdt_next_node(fdt, offset, &depth)) { |
| 283 | if (pdepth < depth) |
| 284 | continue; /* overflowed buffer */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 285 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 286 | while (pdepth > depth) { |
| 287 | do { |
| 288 | p--; |
| 289 | } while (buf[p-1] != '/'); |
| 290 | pdepth--; |
| 291 | } |
| 292 | |
| 293 | name = fdt_get_name(fdt, offset, &namelen); |
| 294 | if (!name) |
| 295 | return namelen; |
| 296 | if ((p + namelen + 1) <= buflen) { |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 297 | memcpy(buf + p, name, namelen); |
| 298 | p += namelen; |
| 299 | buf[p++] = '/'; |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 300 | pdepth++; |
| 301 | } |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 302 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 303 | if (offset == nodeoffset) { |
| 304 | if (pdepth < (depth + 1)) |
| 305 | return -FDT_ERR_NOSPACE; |
| 306 | |
| 307 | if (p > 1) /* special case so that root path is "/", not "" */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 308 | p--; |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 309 | buf[p] = '\0'; |
| 310 | return p; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 314 | if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) |
| 315 | return -FDT_ERR_BADOFFSET; |
| 316 | else if (offset == -FDT_ERR_BADOFFSET) |
| 317 | return -FDT_ERR_BADSTRUCTURE; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 318 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 319 | return offset; /* error from fdt_next_node() */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, |
| 323 | int supernodedepth, int *nodedepth) |
| 324 | { |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 325 | int offset, depth; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 326 | int supernodeoffset = -FDT_ERR_INTERNAL; |
| 327 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame^] | 328 | FDT_CHECK_HEADER(fdt); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 329 | |
| 330 | if (supernodedepth < 0) |
| 331 | return -FDT_ERR_NOTFOUND; |
| 332 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 333 | for (offset = 0, depth = 0; |
| 334 | (offset >= 0) && (offset <= nodeoffset); |
| 335 | offset = fdt_next_node(fdt, offset, &depth)) { |
| 336 | if (depth == supernodedepth) |
| 337 | supernodeoffset = offset; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 338 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 339 | if (offset == nodeoffset) { |
| 340 | if (nodedepth) |
| 341 | *nodedepth = depth; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 342 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 343 | if (supernodedepth > depth) |
| 344 | return -FDT_ERR_NOTFOUND; |
| 345 | else |
| 346 | return supernodeoffset; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 347 | } |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 348 | } |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 349 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 350 | if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) |
| 351 | return -FDT_ERR_BADOFFSET; |
| 352 | else if (offset == -FDT_ERR_BADOFFSET) |
| 353 | return -FDT_ERR_BADSTRUCTURE; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 354 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 355 | return offset; /* error from fdt_next_node() */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | int fdt_node_depth(const void *fdt, int nodeoffset) |
| 359 | { |
| 360 | int nodedepth; |
| 361 | int err; |
| 362 | |
| 363 | err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth); |
| 364 | if (err) |
| 365 | return (err < 0) ? err : -FDT_ERR_INTERNAL; |
| 366 | return nodedepth; |
| 367 | } |
| 368 | |
| 369 | int fdt_parent_offset(const void *fdt, int nodeoffset) |
| 370 | { |
| 371 | int nodedepth = fdt_node_depth(fdt, nodeoffset); |
| 372 | |
| 373 | if (nodedepth < 0) |
| 374 | return nodedepth; |
| 375 | return fdt_supernode_atdepth_offset(fdt, nodeoffset, |
| 376 | nodedepth - 1, NULL); |
| 377 | } |
| 378 | |
| 379 | int fdt_node_offset_by_prop_value(const void *fdt, int startoffset, |
| 380 | const char *propname, |
| 381 | const void *propval, int proplen) |
| 382 | { |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 383 | int offset; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 384 | const void *val; |
| 385 | int len; |
| 386 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame^] | 387 | FDT_CHECK_HEADER(fdt); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 388 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 389 | /* FIXME: The algorithm here is pretty horrible: we scan each |
| 390 | * property of a node in fdt_getprop(), then if that didn't |
| 391 | * find what we want, we scan over them again making our way |
| 392 | * to the next node. Still it's the easiest to implement |
| 393 | * approach; performance can come later. */ |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 394 | for (offset = fdt_next_node(fdt, startoffset, NULL); |
| 395 | offset >= 0; |
| 396 | offset = fdt_next_node(fdt, offset, NULL)) { |
| 397 | val = fdt_getprop(fdt, offset, propname, &len); |
| 398 | if (val && (len == proplen) |
| 399 | && (memcmp(val, propval, len) == 0)) |
| 400 | return offset; |
| 401 | } |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 402 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 403 | return offset; /* error from fdt_next_node() */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle) |
| 407 | { |
| 408 | if ((phandle == 0) || (phandle == -1)) |
| 409 | return -FDT_ERR_BADPHANDLE; |
| 410 | phandle = cpu_to_fdt32(phandle); |
| 411 | return fdt_node_offset_by_prop_value(fdt, -1, "linux,phandle", |
| 412 | &phandle, sizeof(phandle)); |
| 413 | } |
| 414 | |
David Gibson | ef4e8ce | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 415 | int _stringlist_contains(const char *strlist, int listlen, const char *str) |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 416 | { |
| 417 | int len = strlen(str); |
David Gibson | ef4e8ce | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 418 | const char *p; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 419 | |
| 420 | while (listlen >= len) { |
| 421 | if (memcmp(str, strlist, len+1) == 0) |
| 422 | return 1; |
| 423 | p = memchr(strlist, '\0', listlen); |
| 424 | if (!p) |
| 425 | return 0; /* malformed strlist.. */ |
| 426 | listlen -= (p-strlist) + 1; |
| 427 | strlist = p + 1; |
Gerald Van Baren | 3f9f08c | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 428 | } |
| 429 | return 0; |
| 430 | } |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 431 | |
| 432 | int fdt_node_check_compatible(const void *fdt, int nodeoffset, |
| 433 | const char *compatible) |
| 434 | { |
| 435 | const void *prop; |
| 436 | int len; |
| 437 | |
| 438 | prop = fdt_getprop(fdt, nodeoffset, "compatible", &len); |
| 439 | if (!prop) |
| 440 | return len; |
| 441 | if (_stringlist_contains(prop, len, compatible)) |
| 442 | return 0; |
| 443 | else |
| 444 | return 1; |
| 445 | } |
| 446 | |
| 447 | int fdt_node_offset_by_compatible(const void *fdt, int startoffset, |
| 448 | const char *compatible) |
| 449 | { |
David Gibson | 11abe45 | 2008-02-18 18:09:04 +1100 | [diff] [blame] | 450 | int offset, err; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 451 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame^] | 452 | FDT_CHECK_HEADER(fdt); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 453 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 454 | /* FIXME: The algorithm here is pretty horrible: we scan each |
| 455 | * property of a node in fdt_node_check_compatible(), then if |
| 456 | * that didn't find what we want, we scan over them again |
| 457 | * making our way to the next node. Still it's the easiest to |
| 458 | * implement approach; performance can come later. */ |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 459 | for (offset = fdt_next_node(fdt, startoffset, NULL); |
| 460 | offset >= 0; |
| 461 | offset = fdt_next_node(fdt, offset, NULL)) { |
| 462 | err = fdt_node_check_compatible(fdt, offset, compatible); |
| 463 | if ((err < 0) && (err != -FDT_ERR_NOTFOUND)) |
| 464 | return err; |
| 465 | else if (err == 0) |
| 466 | return offset; |
| 467 | } |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 468 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 469 | return offset; /* error from fdt_next_node() */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 470 | } |