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 | |
| 142 | if (*path != '/') |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 143 | return -FDT_ERR_BADPATH; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 144 | |
| 145 | while (*p) { |
| 146 | const char *q; |
| 147 | |
| 148 | while (*p == '/') |
| 149 | p++; |
| 150 | if (! *p) |
David Gibson | bd2ae2f | 2007-08-29 12:22:50 +1000 | [diff] [blame] | 151 | return offset; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 152 | q = strchr(p, '/'); |
| 153 | if (! q) |
| 154 | q = end; |
| 155 | |
| 156 | offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p); |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 157 | if (offset < 0) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 158 | return offset; |
| 159 | |
| 160 | p = q; |
| 161 | } |
| 162 | |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 163 | return offset; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 164 | } |
| 165 | |
David Gibson | 9d26eab | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 166 | const char *fdt_get_name(const void *fdt, int nodeoffset, int *len) |
| 167 | { |
David Gibson | aa1baab | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 168 | const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset); |
David Gibson | 9d26eab | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 169 | int err; |
| 170 | |
David Gibson | aa1baab | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 171 | if (((err = fdt_check_header(fdt)) != 0) |
| 172 | || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0)) |
| 173 | goto fail; |
David Gibson | 9d26eab | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 174 | |
| 175 | if (len) |
| 176 | *len = strlen(nh->name); |
| 177 | |
| 178 | return nh->name; |
| 179 | |
| 180 | fail: |
| 181 | if (len) |
| 182 | *len = err; |
| 183 | return NULL; |
| 184 | } |
| 185 | |
David Gibson | cb650ae | 2008-08-06 14:50:49 +1000 | [diff] [blame^] | 186 | const struct fdt_property *fdt_get_property_namelen(const void *fdt, |
| 187 | int nodeoffset, |
| 188 | const char *name, |
| 189 | int namelen, int *lenp) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 190 | { |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 191 | uint32_t tag; |
David Gibson | a6c76f9 | 2007-06-13 14:18:10 +1000 | [diff] [blame] | 192 | const struct fdt_property *prop; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 193 | int namestroff; |
| 194 | int offset, nextoffset; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 195 | int err; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 196 | |
David Gibson | aa1baab | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 197 | if (((err = fdt_check_header(fdt)) != 0) |
| 198 | || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0)) |
| 199 | goto fail; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 200 | |
David Gibson | aa1baab | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 201 | nextoffset = err; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 202 | do { |
| 203 | offset = nextoffset; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 204 | |
David Gibson | 3c44c87 | 2007-10-24 11:06:09 +1000 | [diff] [blame] | 205 | tag = fdt_next_tag(fdt, offset, &nextoffset); |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 206 | switch (tag) { |
| 207 | case FDT_END: |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 208 | err = -FDT_ERR_TRUNCATED; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 209 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 210 | |
| 211 | case FDT_BEGIN_NODE: |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 212 | case FDT_END_NODE: |
David Gibson | 592ea58 | 2007-09-04 10:43:03 +1000 | [diff] [blame] | 213 | case FDT_NOP: |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 214 | break; |
| 215 | |
| 216 | case FDT_PROP: |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 217 | err = -FDT_ERR_BADSTRUCTURE; |
David Gibson | 2cf8693 | 2007-11-19 17:26:22 +1100 | [diff] [blame] | 218 | prop = fdt_offset_ptr(fdt, offset, sizeof(*prop)); |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 219 | if (! prop) |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 220 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 221 | namestroff = fdt32_to_cpu(prop->nameoff); |
David Gibson | cb650ae | 2008-08-06 14:50:49 +1000 | [diff] [blame^] | 222 | if (_fdt_string_eq(fdt, namestroff, name, namelen)) { |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 223 | /* Found it! */ |
| 224 | int len = fdt32_to_cpu(prop->len); |
| 225 | prop = fdt_offset_ptr(fdt, offset, |
David Gibson | 9825f82 | 2006-12-14 15:29:25 +1100 | [diff] [blame] | 226 | sizeof(*prop)+len); |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 227 | if (! prop) |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 228 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 229 | |
| 230 | if (lenp) |
| 231 | *lenp = len; |
David Gibson | 63dc9c7 | 2007-09-18 11:44:04 +1000 | [diff] [blame] | 232 | |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 233 | return prop; |
| 234 | } |
| 235 | break; |
| 236 | |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 237 | default: |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 238 | err = -FDT_ERR_BADSTRUCTURE; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 239 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 240 | } |
David Gibson | 592ea58 | 2007-09-04 10:43:03 +1000 | [diff] [blame] | 241 | } while ((tag != FDT_BEGIN_NODE) && (tag != FDT_END_NODE)); |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 242 | |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 243 | err = -FDT_ERR_NOTFOUND; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 244 | fail: |
| 245 | if (lenp) |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 246 | *lenp = err; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 247 | return NULL; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 248 | } |
| 249 | |
David Gibson | cb650ae | 2008-08-06 14:50:49 +1000 | [diff] [blame^] | 250 | const struct fdt_property *fdt_get_property(const void *fdt, |
| 251 | int nodeoffset, |
| 252 | const char *name, int *lenp) |
| 253 | { |
| 254 | return fdt_get_property_namelen(fdt, nodeoffset, name, |
| 255 | strlen(name), lenp); |
| 256 | } |
| 257 | |
| 258 | const void *fdt_getprop_namelen(const void *fdt, int nodeoffset, |
| 259 | const char *name, int namelen, int *lenp) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 260 | { |
| 261 | const struct fdt_property *prop; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 262 | |
David Gibson | cb650ae | 2008-08-06 14:50:49 +1000 | [diff] [blame^] | 263 | prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp); |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 264 | if (! prop) |
| 265 | return NULL; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 266 | |
| 267 | return prop->data; |
| 268 | } |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 269 | |
David Gibson | cb650ae | 2008-08-06 14:50:49 +1000 | [diff] [blame^] | 270 | const void *fdt_getprop(const void *fdt, int nodeoffset, |
| 271 | const char *name, int *lenp) |
| 272 | { |
| 273 | return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp); |
| 274 | } |
| 275 | |
David Gibson | 7346858 | 2007-11-13 09:59:38 +1100 | [diff] [blame] | 276 | uint32_t fdt_get_phandle(const void *fdt, int nodeoffset) |
| 277 | { |
| 278 | const uint32_t *php; |
| 279 | int len; |
| 280 | |
| 281 | php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len); |
| 282 | if (!php || (len != sizeof(*php))) |
| 283 | return 0; |
| 284 | |
| 285 | return fdt32_to_cpu(*php); |
| 286 | } |
| 287 | |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 288 | int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen) |
| 289 | { |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 290 | int pdepth = 0, p = 0; |
| 291 | int offset, depth, namelen; |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 292 | const char *name; |
| 293 | |
David Gibson | b6d80a2 | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 294 | FDT_CHECK_HEADER(fdt); |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 295 | |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 296 | if (buflen < 2) |
| 297 | return -FDT_ERR_NOSPACE; |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 298 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 299 | for (offset = 0, depth = 0; |
| 300 | (offset >= 0) && (offset <= nodeoffset); |
| 301 | offset = fdt_next_node(fdt, offset, &depth)) { |
| 302 | if (pdepth < depth) |
| 303 | continue; /* overflowed buffer */ |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 304 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 305 | while (pdepth > depth) { |
| 306 | do { |
| 307 | p--; |
| 308 | } while (buf[p-1] != '/'); |
| 309 | pdepth--; |
| 310 | } |
| 311 | |
| 312 | name = fdt_get_name(fdt, offset, &namelen); |
| 313 | if (!name) |
| 314 | return namelen; |
| 315 | if ((p + namelen + 1) <= buflen) { |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 316 | memcpy(buf + p, name, namelen); |
| 317 | p += namelen; |
| 318 | buf[p++] = '/'; |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 319 | pdepth++; |
| 320 | } |
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 | if (offset == nodeoffset) { |
| 323 | if (pdepth < (depth + 1)) |
| 324 | return -FDT_ERR_NOSPACE; |
| 325 | |
| 326 | if (p > 1) /* special case so that root path is "/", not "" */ |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 327 | p--; |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 328 | buf[p] = '\0'; |
| 329 | return p; |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 333 | if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) |
| 334 | return -FDT_ERR_BADOFFSET; |
| 335 | else if (offset == -FDT_ERR_BADOFFSET) |
| 336 | return -FDT_ERR_BADSTRUCTURE; |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 337 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 338 | return offset; /* error from fdt_next_node() */ |
David Gibson | 037db26 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 339 | } |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 340 | |
| 341 | int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, |
| 342 | int supernodedepth, int *nodedepth) |
| 343 | { |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 344 | int offset, depth; |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 345 | int supernodeoffset = -FDT_ERR_INTERNAL; |
| 346 | |
David Gibson | b6d80a2 | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 347 | FDT_CHECK_HEADER(fdt); |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 348 | |
| 349 | if (supernodedepth < 0) |
| 350 | return -FDT_ERR_NOTFOUND; |
| 351 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 352 | for (offset = 0, depth = 0; |
| 353 | (offset >= 0) && (offset <= nodeoffset); |
| 354 | offset = fdt_next_node(fdt, offset, &depth)) { |
| 355 | if (depth == supernodedepth) |
| 356 | supernodeoffset = offset; |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 357 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 358 | if (offset == nodeoffset) { |
| 359 | if (nodedepth) |
| 360 | *nodedepth = depth; |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 361 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 362 | if (supernodedepth > depth) |
| 363 | return -FDT_ERR_NOTFOUND; |
| 364 | else |
| 365 | return supernodeoffset; |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 366 | } |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 367 | } |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 368 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 369 | if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) |
| 370 | return -FDT_ERR_BADOFFSET; |
| 371 | else if (offset == -FDT_ERR_BADOFFSET) |
| 372 | return -FDT_ERR_BADSTRUCTURE; |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 373 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 374 | return offset; /* error from fdt_next_node() */ |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | int fdt_node_depth(const void *fdt, int nodeoffset) |
| 378 | { |
| 379 | int nodedepth; |
| 380 | int err; |
| 381 | |
| 382 | err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth); |
| 383 | if (err) |
| 384 | return (err < 0) ? err : -FDT_ERR_INTERNAL; |
| 385 | return nodedepth; |
| 386 | } |
| 387 | |
| 388 | int fdt_parent_offset(const void *fdt, int nodeoffset) |
| 389 | { |
| 390 | int nodedepth = fdt_node_depth(fdt, nodeoffset); |
| 391 | |
| 392 | if (nodedepth < 0) |
| 393 | return nodedepth; |
| 394 | return fdt_supernode_atdepth_offset(fdt, nodeoffset, |
| 395 | nodedepth - 1, NULL); |
| 396 | } |
David Gibson | ae1454b | 2007-09-17 14:28:34 +1000 | [diff] [blame] | 397 | |
| 398 | int fdt_node_offset_by_prop_value(const void *fdt, int startoffset, |
| 399 | const char *propname, |
| 400 | const void *propval, int proplen) |
| 401 | { |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 402 | int offset; |
David Gibson | ae1454b | 2007-09-17 14:28:34 +1000 | [diff] [blame] | 403 | const void *val; |
| 404 | int len; |
| 405 | |
David Gibson | b6d80a2 | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 406 | FDT_CHECK_HEADER(fdt); |
David Gibson | ae1454b | 2007-09-17 14:28:34 +1000 | [diff] [blame] | 407 | |
David Gibson | ae1454b | 2007-09-17 14:28:34 +1000 | [diff] [blame] | 408 | /* FIXME: The algorithm here is pretty horrible: we scan each |
| 409 | * property of a node in fdt_getprop(), then if that didn't |
| 410 | * find what we want, we scan over them again making our way |
| 411 | * to the next node. Still it's the easiest to implement |
| 412 | * approach; performance can come later. */ |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 413 | for (offset = fdt_next_node(fdt, startoffset, NULL); |
| 414 | offset >= 0; |
| 415 | offset = fdt_next_node(fdt, offset, NULL)) { |
| 416 | val = fdt_getprop(fdt, offset, propname, &len); |
| 417 | if (val && (len == proplen) |
| 418 | && (memcmp(val, propval, len) == 0)) |
| 419 | return offset; |
| 420 | } |
David Gibson | ae1454b | 2007-09-17 14:28:34 +1000 | [diff] [blame] | 421 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 422 | return offset; /* error from fdt_next_node() */ |
David Gibson | ae1454b | 2007-09-17 14:28:34 +1000 | [diff] [blame] | 423 | } |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 424 | |
David Gibson | 7346858 | 2007-11-13 09:59:38 +1100 | [diff] [blame] | 425 | int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle) |
| 426 | { |
| 427 | if ((phandle == 0) || (phandle == -1)) |
| 428 | return -FDT_ERR_BADPHANDLE; |
| 429 | phandle = cpu_to_fdt32(phandle); |
| 430 | return fdt_node_offset_by_prop_value(fdt, -1, "linux,phandle", |
| 431 | &phandle, sizeof(phandle)); |
| 432 | } |
| 433 | |
David Gibson | d565361 | 2008-07-29 14:51:22 +1000 | [diff] [blame] | 434 | static int _fdt_stringlist_contains(const char *strlist, int listlen, |
| 435 | const char *str) |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 436 | { |
| 437 | int len = strlen(str); |
David Gibson | 36786db | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 438 | const char *p; |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 439 | |
| 440 | while (listlen >= len) { |
| 441 | if (memcmp(str, strlist, len+1) == 0) |
| 442 | return 1; |
| 443 | p = memchr(strlist, '\0', listlen); |
| 444 | if (!p) |
| 445 | return 0; /* malformed strlist.. */ |
| 446 | listlen -= (p-strlist) + 1; |
| 447 | strlist = p + 1; |
| 448 | } |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | int fdt_node_check_compatible(const void *fdt, int nodeoffset, |
| 453 | const char *compatible) |
| 454 | { |
| 455 | const void *prop; |
| 456 | int len; |
| 457 | |
| 458 | prop = fdt_getprop(fdt, nodeoffset, "compatible", &len); |
| 459 | if (!prop) |
| 460 | return len; |
David Gibson | d565361 | 2008-07-29 14:51:22 +1000 | [diff] [blame] | 461 | if (_fdt_stringlist_contains(prop, len, compatible)) |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 462 | return 0; |
| 463 | else |
| 464 | return 1; |
| 465 | } |
| 466 | |
| 467 | int fdt_node_offset_by_compatible(const void *fdt, int startoffset, |
| 468 | const char *compatible) |
| 469 | { |
David Gibson | 2512a7e | 2008-02-18 18:09:04 +1100 | [diff] [blame] | 470 | int offset, err; |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 471 | |
David Gibson | b6d80a2 | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 472 | FDT_CHECK_HEADER(fdt); |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 473 | |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 474 | /* FIXME: The algorithm here is pretty horrible: we scan each |
| 475 | * property of a node in fdt_node_check_compatible(), then if |
| 476 | * that didn't find what we want, we scan over them again |
| 477 | * making our way to the next node. Still it's the easiest to |
| 478 | * implement approach; performance can come later. */ |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 479 | for (offset = fdt_next_node(fdt, startoffset, NULL); |
| 480 | offset >= 0; |
| 481 | offset = fdt_next_node(fdt, offset, NULL)) { |
| 482 | err = fdt_node_check_compatible(fdt, offset, compatible); |
| 483 | if ((err < 0) && (err != -FDT_ERR_NOTFOUND)) |
| 484 | return err; |
| 485 | else if (err == 0) |
| 486 | return offset; |
| 487 | } |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 488 | |
David Gibson | fc9769a | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 489 | return offset; /* error from fdt_next_node() */ |
David Gibson | 333542f | 2007-10-16 13:58:25 +1000 | [diff] [blame] | 490 | } |