David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 1 | /* |
| 2 | * libfdt - Flat Device Tree manipulation |
| 3 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
| 4 | * |
David Gibson | 9481605 | 2007-06-13 16:30:48 +1000 | [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. |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 7 | * |
David Gibson | 9481605 | 2007-06-13 16:30:48 +1000 | [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. |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 12 | * |
David Gibson | 9481605 | 2007-06-13 16:30:48 +1000 | [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. |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 50 | */ |
| 51 | #include "libfdt_env.h" |
| 52 | |
| 53 | #include <fdt.h> |
| 54 | #include <libfdt.h> |
| 55 | |
| 56 | #include "libfdt_internal.h" |
| 57 | |
David Gibson | b6d80a2 | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 58 | static int _fdt_nodename_eq(const void *fdt, int offset, |
| 59 | const char *s, int len) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 60 | { |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 61 | const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 62 | |
| 63 | if (! p) |
| 64 | /* short match */ |
| 65 | return 0; |
| 66 | |
| 67 | if (memcmp(p, s, len) != 0) |
| 68 | return 0; |
| 69 | |
David Gibson | d2a9da0 | 2007-09-28 15:51:04 +1000 | [diff] [blame] | 70 | if (p[len] == '\0') |
| 71 | return 1; |
| 72 | else if (!memchr(s, '@', len) && (p[len] == '@')) |
| 73 | return 1; |
| 74 | else |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 75 | return 0; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 76 | } |
| 77 | |
David Gibson | 11d5302 | 2007-10-18 12:10:42 +1000 | [diff] [blame] | 78 | const char *fdt_string(const void *fdt, int stroffset) |
David Gibson | 3aa4cfd | 2006-11-29 16:34:30 +1100 | [diff] [blame] | 79 | { |
David Gibson | 1409097 | 2008-07-07 10:14:15 +1000 | [diff] [blame] | 80 | return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset; |
David Gibson | 3aa4cfd | 2006-11-29 16:34:30 +1100 | [diff] [blame] | 81 | } |
| 82 | |
David Gibson | cb650ae | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 83 | static int _fdt_string_eq(const void *fdt, int stroffset, |
| 84 | const char *s, int len) |
| 85 | { |
| 86 | const char *p = fdt_string(fdt, stroffset); |
| 87 | |
| 88 | return (strlen(p) == len) && (memcmp(p, s, len) == 0); |
| 89 | } |
| 90 | |
David Gibson | fd1bf3a | 2007-10-10 17:12:12 +1000 | [diff] [blame] | 91 | int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size) |
| 92 | { |
David Gibson | b6d80a2 | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 93 | FDT_CHECK_HEADER(fdt); |
David Gibson | fd1bf3a | 2007-10-10 17:12:12 +1000 | [diff] [blame] | 94 | *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address); |
| 95 | *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size); |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | int fdt_num_mem_rsv(const void *fdt) |
| 100 | { |
| 101 | int i = 0; |
| 102 | |
| 103 | while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0) |
| 104 | i++; |
| 105 | return i; |
| 106 | } |
| 107 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 108 | int fdt_subnode_offset_namelen(const void *fdt, int offset, |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 109 | const char *name, int namelen) |
| 110 | { |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 111 | int depth; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 112 | |
David Gibson | b6d80a2 | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 113 | FDT_CHECK_HEADER(fdt); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 114 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 115 | for (depth = 0; |
| 116 | offset >= 0; |
| 117 | offset = fdt_next_node(fdt, offset, &depth)) { |
| 118 | if (depth < 0) |
| 119 | return -FDT_ERR_NOTFOUND; |
| 120 | else if ((depth == 1) |
David Gibson | b6d80a2 | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 121 | && _fdt_nodename_eq(fdt, offset, name, namelen)) |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 122 | return offset; |
| 123 | } |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 124 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 125 | return offset; /* error */ |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 126 | } |
| 127 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 128 | int fdt_subnode_offset(const void *fdt, int parentoffset, |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 129 | const char *name) |
| 130 | { |
| 131 | return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name)); |
| 132 | } |
| 133 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 134 | int fdt_path_offset(const void *fdt, const char *path) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 135 | { |
| 136 | const char *end = path + strlen(path); |
| 137 | const char *p = path; |
| 138 | int offset = 0; |
| 139 | |
David Gibson | b6d80a2 | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 140 | FDT_CHECK_HEADER(fdt); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 141 | |
Kumar Gala | 02cc835 | 2008-08-14 08:28:19 -0500 | [diff] [blame^] | 142 | /* see if we have an alias */ |
| 143 | if (*path != '/') { |
| 144 | const char *q; |
| 145 | int aliasoffset = fdt_path_offset(fdt, "/aliases"); |
| 146 | |
| 147 | if (aliasoffset < 0) |
| 148 | return -FDT_ERR_BADPATH; |
| 149 | |
| 150 | q = strchr(path, '/'); |
| 151 | if (!q) |
| 152 | q = end; |
| 153 | |
| 154 | p = fdt_getprop_namelen(fdt, aliasoffset, path, q - p, NULL); |
| 155 | if (!p) |
| 156 | return -FDT_ERR_BADPATH; |
| 157 | offset = fdt_path_offset(fdt, p); |
| 158 | |
| 159 | p = q; |
| 160 | } |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 161 | |
| 162 | while (*p) { |
| 163 | const char *q; |
| 164 | |
| 165 | while (*p == '/') |
| 166 | p++; |
| 167 | if (! *p) |
David Gibson | bd2ae2f | 2007-08-29 12:22:50 +1000 | [diff] [blame] | 168 | return offset; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 169 | q = strchr(p, '/'); |
| 170 | if (! q) |
| 171 | q = end; |
| 172 | |
| 173 | offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p); |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 174 | if (offset < 0) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 175 | return offset; |
| 176 | |
| 177 | p = q; |
| 178 | } |
| 179 | |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 180 | return offset; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 181 | } |
| 182 | |
David Gibson | 9d26eab | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 183 | const char *fdt_get_name(const void *fdt, int nodeoffset, int *len) |
| 184 | { |
David Gibson | aa1baab | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 185 | const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset); |
David Gibson | 9d26eab | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 186 | int err; |
| 187 | |
David Gibson | aa1baab | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 188 | if (((err = fdt_check_header(fdt)) != 0) |
| 189 | || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0)) |
| 190 | goto fail; |
David Gibson | 9d26eab | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 191 | |
| 192 | if (len) |
| 193 | *len = strlen(nh->name); |
| 194 | |
| 195 | return nh->name; |
| 196 | |
| 197 | fail: |
| 198 | if (len) |
| 199 | *len = err; |
| 200 | return NULL; |
| 201 | } |
| 202 | |
David Gibson | cb650ae | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 203 | const struct fdt_property *fdt_get_property_namelen(const void *fdt, |
| 204 | int nodeoffset, |
| 205 | const char *name, |
| 206 | int namelen, int *lenp) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 207 | { |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 208 | uint32_t tag; |
David Gibson | a6c76f9 | 2007-06-13 14:18:10 +1000 | [diff] [blame] | 209 | const struct fdt_property *prop; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 210 | int namestroff; |
| 211 | int offset, nextoffset; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 212 | int err; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 213 | |
David Gibson | aa1baab | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 214 | if (((err = fdt_check_header(fdt)) != 0) |
| 215 | || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0)) |
| 216 | goto fail; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 217 | |
David Gibson | aa1baab | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 218 | nextoffset = err; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 219 | do { |
| 220 | offset = nextoffset; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 221 | |
David Gibson | 3c44c87 | 2007-10-24 11:06:09 +1000 | [diff] [blame] | 222 | tag = fdt_next_tag(fdt, offset, &nextoffset); |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 223 | switch (tag) { |
| 224 | case FDT_END: |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 225 | err = -FDT_ERR_TRUNCATED; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 226 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 227 | |
| 228 | case FDT_BEGIN_NODE: |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 229 | case FDT_END_NODE: |
David Gibson | 592ea58 | 2007-09-04 10:43:03 +1000 | [diff] [blame] | 230 | case FDT_NOP: |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 231 | break; |
| 232 | |
| 233 | case FDT_PROP: |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 234 | err = -FDT_ERR_BADSTRUCTURE; |
David Gibson | 2cf8693 | 2007-11-19 17:26:22 +1100 | [diff] [blame] | 235 | prop = fdt_offset_ptr(fdt, offset, sizeof(*prop)); |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 236 | if (! prop) |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 237 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 238 | namestroff = fdt32_to_cpu(prop->nameoff); |
David Gibson | cb650ae | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 239 | if (_fdt_string_eq(fdt, namestroff, name, namelen)) { |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 240 | /* Found it! */ |
| 241 | int len = fdt32_to_cpu(prop->len); |
| 242 | prop = fdt_offset_ptr(fdt, offset, |
David Gibson | 9825f82 | 2006-12-14 15:29:25 +1100 | [diff] [blame] | 243 | sizeof(*prop)+len); |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 244 | if (! prop) |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 245 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 246 | |
| 247 | if (lenp) |
| 248 | *lenp = len; |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 249 | |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 250 | return prop; |
| 251 | } |
| 252 | break; |
| 253 | |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 254 | default: |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 255 | err = -FDT_ERR_BADSTRUCTURE; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 256 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 257 | } |
David Gibson | 592ea58 | 2007-09-04 10:43:03 +1000 | [diff] [blame] | 258 | } while ((tag != FDT_BEGIN_NODE) && (tag != FDT_END_NODE)); |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 259 | |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 260 | err = -FDT_ERR_NOTFOUND; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 261 | fail: |
| 262 | if (lenp) |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 263 | *lenp = err; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 264 | return NULL; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 265 | } |
| 266 | |
David Gibson | cb650ae | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 267 | const struct fdt_property *fdt_get_property(const void *fdt, |
| 268 | int nodeoffset, |
| 269 | const char *name, int *lenp) |
| 270 | { |
| 271 | return fdt_get_property_namelen(fdt, nodeoffset, name, |
| 272 | strlen(name), lenp); |
| 273 | } |
| 274 | |
| 275 | const void *fdt_getprop_namelen(const void *fdt, int nodeoffset, |
| 276 | const char *name, int namelen, int *lenp) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 277 | { |
| 278 | const struct fdt_property *prop; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 279 | |
David Gibson | cb650ae | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 280 | prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp); |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 281 | if (! prop) |
| 282 | return NULL; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 283 | |
| 284 | return prop->data; |
| 285 | } |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 286 | |
David Gibson | cb650ae | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 287 | const void *fdt_getprop(const void *fdt, int nodeoffset, |
| 288 | const char *name, int *lenp) |
| 289 | { |
| 290 | return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp); |
| 291 | } |
| 292 | |
David Gibson | 7346858 | 2007-11-13 09:59:38 +1100 | [diff] [blame] | 293 | uint32_t fdt_get_phandle(const void *fdt, int nodeoffset) |
| 294 | { |
| 295 | const uint32_t *php; |
| 296 | int len; |
| 297 | |
| 298 | php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len); |
| 299 | if (!php || (len != sizeof(*php))) |
| 300 | return 0; |
| 301 | |
| 302 | return fdt32_to_cpu(*php); |
| 303 | } |
| 304 | |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 305 | int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen) |
| 306 | { |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 307 | int pdepth = 0, p = 0; |
| 308 | int offset, depth, namelen; |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 309 | const char *name; |
| 310 | |
David Gibson | b6d80a2 | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 311 | FDT_CHECK_HEADER(fdt); |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 312 | |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 313 | if (buflen < 2) |
| 314 | return -FDT_ERR_NOSPACE; |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 315 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 316 | for (offset = 0, depth = 0; |
| 317 | (offset >= 0) && (offset <= nodeoffset); |
| 318 | offset = fdt_next_node(fdt, offset, &depth)) { |
| 319 | if (pdepth < depth) |
| 320 | continue; /* overflowed buffer */ |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 321 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 322 | while (pdepth > depth) { |
| 323 | do { |
| 324 | p--; |
| 325 | } while (buf[p-1] != '/'); |
| 326 | pdepth--; |
| 327 | } |
| 328 | |
| 329 | name = fdt_get_name(fdt, offset, &namelen); |
| 330 | if (!name) |
| 331 | return namelen; |
| 332 | if ((p + namelen + 1) <= buflen) { |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 333 | memcpy(buf + p, name, namelen); |
| 334 | p += namelen; |
| 335 | buf[p++] = '/'; |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 336 | pdepth++; |
| 337 | } |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 338 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 339 | if (offset == nodeoffset) { |
| 340 | if (pdepth < (depth + 1)) |
| 341 | return -FDT_ERR_NOSPACE; |
| 342 | |
| 343 | if (p > 1) /* special case so that root path is "/", not "" */ |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 344 | p--; |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 345 | buf[p] = '\0'; |
| 346 | return p; |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
David Gibson | fc9769a | 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; |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 354 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 355 | return offset; /* error from fdt_next_node() */ |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 356 | } |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 357 | |
| 358 | int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, |
| 359 | int supernodedepth, int *nodedepth) |
| 360 | { |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 361 | int offset, depth; |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 362 | int supernodeoffset = -FDT_ERR_INTERNAL; |
| 363 | |
David Gibson | b6d80a2 | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 364 | FDT_CHECK_HEADER(fdt); |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 365 | |
| 366 | if (supernodedepth < 0) |
| 367 | return -FDT_ERR_NOTFOUND; |
| 368 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 369 | for (offset = 0, depth = 0; |
| 370 | (offset >= 0) && (offset <= nodeoffset); |
| 371 | offset = fdt_next_node(fdt, offset, &depth)) { |
| 372 | if (depth == supernodedepth) |
| 373 | supernodeoffset = offset; |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 374 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 375 | if (offset == nodeoffset) { |
| 376 | if (nodedepth) |
| 377 | *nodedepth = depth; |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 378 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 379 | if (supernodedepth > depth) |
| 380 | return -FDT_ERR_NOTFOUND; |
| 381 | else |
| 382 | return supernodeoffset; |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 383 | } |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 384 | } |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 385 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 386 | if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) |
| 387 | return -FDT_ERR_BADOFFSET; |
| 388 | else if (offset == -FDT_ERR_BADOFFSET) |
| 389 | return -FDT_ERR_BADSTRUCTURE; |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 390 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 391 | return offset; /* error from fdt_next_node() */ |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | int fdt_node_depth(const void *fdt, int nodeoffset) |
| 395 | { |
| 396 | int nodedepth; |
| 397 | int err; |
| 398 | |
| 399 | err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth); |
| 400 | if (err) |
| 401 | return (err < 0) ? err : -FDT_ERR_INTERNAL; |
| 402 | return nodedepth; |
| 403 | } |
| 404 | |
| 405 | int fdt_parent_offset(const void *fdt, int nodeoffset) |
| 406 | { |
| 407 | int nodedepth = fdt_node_depth(fdt, nodeoffset); |
| 408 | |
| 409 | if (nodedepth < 0) |
| 410 | return nodedepth; |
| 411 | return fdt_supernode_atdepth_offset(fdt, nodeoffset, |
| 412 | nodedepth - 1, NULL); |
| 413 | } |
David Gibson | ae1454b | 2007-09-17 14:28:34 +1000 | [diff] [blame] | 414 | |
| 415 | int fdt_node_offset_by_prop_value(const void *fdt, int startoffset, |
| 416 | const char *propname, |
| 417 | const void *propval, int proplen) |
| 418 | { |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 419 | int offset; |
David Gibson | ae1454b | 2007-09-17 14:28:34 +1000 | [diff] [blame] | 420 | const void *val; |
| 421 | int len; |
| 422 | |
David Gibson | b6d80a2 | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 423 | FDT_CHECK_HEADER(fdt); |
David Gibson | ae1454b | 2007-09-17 14:28:34 +1000 | [diff] [blame] | 424 | |
David Gibson | ae1454b | 2007-09-17 14:28:34 +1000 | [diff] [blame] | 425 | /* FIXME: The algorithm here is pretty horrible: we scan each |
| 426 | * property of a node in fdt_getprop(), then if that didn't |
| 427 | * find what we want, we scan over them again making our way |
| 428 | * to the next node. Still it's the easiest to implement |
| 429 | * approach; performance can come later. */ |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 430 | for (offset = fdt_next_node(fdt, startoffset, NULL); |
| 431 | offset >= 0; |
| 432 | offset = fdt_next_node(fdt, offset, NULL)) { |
| 433 | val = fdt_getprop(fdt, offset, propname, &len); |
| 434 | if (val && (len == proplen) |
| 435 | && (memcmp(val, propval, len) == 0)) |
| 436 | return offset; |
| 437 | } |
David Gibson | ae1454b | 2007-09-17 14:28:34 +1000 | [diff] [blame] | 438 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 439 | return offset; /* error from fdt_next_node() */ |
David Gibson | ae1454b | 2007-09-17 14:28:34 +1000 | [diff] [blame] | 440 | } |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 441 | |
David Gibson | 7346858 | 2007-11-13 09:59:38 +1100 | [diff] [blame] | 442 | int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle) |
| 443 | { |
| 444 | if ((phandle == 0) || (phandle == -1)) |
| 445 | return -FDT_ERR_BADPHANDLE; |
| 446 | phandle = cpu_to_fdt32(phandle); |
| 447 | return fdt_node_offset_by_prop_value(fdt, -1, "linux,phandle", |
| 448 | &phandle, sizeof(phandle)); |
| 449 | } |
| 450 | |
David Gibson | d565361 | 2008-07-29 14:51:22 +1000 | [diff] [blame] | 451 | static int _fdt_stringlist_contains(const char *strlist, int listlen, |
| 452 | const char *str) |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 453 | { |
| 454 | int len = strlen(str); |
David Gibson | 36786db | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 455 | const char *p; |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 456 | |
| 457 | while (listlen >= len) { |
| 458 | if (memcmp(str, strlist, len+1) == 0) |
| 459 | return 1; |
| 460 | p = memchr(strlist, '\0', listlen); |
| 461 | if (!p) |
| 462 | return 0; /* malformed strlist.. */ |
| 463 | listlen -= (p-strlist) + 1; |
| 464 | strlist = p + 1; |
| 465 | } |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | int fdt_node_check_compatible(const void *fdt, int nodeoffset, |
| 470 | const char *compatible) |
| 471 | { |
| 472 | const void *prop; |
| 473 | int len; |
| 474 | |
| 475 | prop = fdt_getprop(fdt, nodeoffset, "compatible", &len); |
| 476 | if (!prop) |
| 477 | return len; |
David Gibson | d565361 | 2008-07-29 14:51:22 +1000 | [diff] [blame] | 478 | if (_fdt_stringlist_contains(prop, len, compatible)) |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 479 | return 0; |
| 480 | else |
| 481 | return 1; |
| 482 | } |
| 483 | |
| 484 | int fdt_node_offset_by_compatible(const void *fdt, int startoffset, |
| 485 | const char *compatible) |
| 486 | { |
David Gibson | 2512a7e | 2008-02-18 18:09:04 +1100 | [diff] [blame] | 487 | int offset, err; |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 488 | |
David Gibson | b6d80a2 | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 489 | FDT_CHECK_HEADER(fdt); |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 490 | |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 491 | /* FIXME: The algorithm here is pretty horrible: we scan each |
| 492 | * property of a node in fdt_node_check_compatible(), then if |
| 493 | * that didn't find what we want, we scan over them again |
| 494 | * making our way to the next node. Still it's the easiest to |
| 495 | * implement approach; performance can come later. */ |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 496 | for (offset = fdt_next_node(fdt, startoffset, NULL); |
| 497 | offset >= 0; |
| 498 | offset = fdt_next_node(fdt, offset, NULL)) { |
| 499 | err = fdt_node_check_compatible(fdt, offset, compatible); |
| 500 | if ((err < 0) && (err != -FDT_ERR_NOTFOUND)) |
| 501 | return err; |
| 502 | else if (err == 0) |
| 503 | return offset; |
| 504 | } |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 505 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 506 | return offset; /* error from fdt_next_node() */ |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 507 | } |