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 | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 58 | #define CHECK_HEADER(fdt) \ |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 59 | { \ |
| 60 | int err; \ |
David Gibson | 4236976 | 2006-12-01 15:07:19 +1100 | [diff] [blame] | 61 | if ((err = _fdt_check_header(fdt)) != 0) \ |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 62 | return err; \ |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 63 | } |
| 64 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 65 | static int offset_streq(const void *fdt, int offset, |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 66 | const char *s, int len) |
| 67 | { |
| 68 | const char *p = fdt_offset_ptr(fdt, offset, len+1); |
| 69 | |
| 70 | if (! p) |
| 71 | /* short match */ |
| 72 | return 0; |
| 73 | |
| 74 | if (memcmp(p, s, len) != 0) |
| 75 | return 0; |
| 76 | |
| 77 | if (p[len] != '\0') |
| 78 | return 0; |
| 79 | |
| 80 | return 1; |
| 81 | } |
| 82 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 83 | char *fdt_string(const void *fdt, int stroffset) |
David Gibson | 3aa4cfd | 2006-11-29 16:34:30 +1100 | [diff] [blame] | 84 | { |
David Gibson | ede25de | 2006-12-01 15:02:10 +1100 | [diff] [blame] | 85 | return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset; |
David Gibson | 3aa4cfd | 2006-11-29 16:34:30 +1100 | [diff] [blame] | 86 | } |
| 87 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 88 | int fdt_subnode_offset_namelen(const void *fdt, int parentoffset, |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 89 | const char *name, int namelen) |
| 90 | { |
| 91 | int level = 0; |
| 92 | uint32_t tag; |
| 93 | int offset, nextoffset; |
| 94 | |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 95 | CHECK_HEADER(fdt); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 96 | |
| 97 | tag = _fdt_next_tag(fdt, parentoffset, &nextoffset); |
| 98 | if (tag != FDT_BEGIN_NODE) |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 99 | return -FDT_ERR_BADOFFSET; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 100 | |
| 101 | do { |
| 102 | offset = nextoffset; |
| 103 | tag = _fdt_next_tag(fdt, offset, &nextoffset); |
| 104 | |
| 105 | switch (tag) { |
| 106 | case FDT_END: |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 107 | return -FDT_ERR_TRUNCATED; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 108 | |
| 109 | case FDT_BEGIN_NODE: |
| 110 | level++; |
| 111 | if (level != 1) |
| 112 | continue; |
| 113 | if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen)) |
| 114 | /* Found it! */ |
| 115 | return offset; |
| 116 | break; |
| 117 | |
| 118 | case FDT_END_NODE: |
| 119 | level--; |
| 120 | break; |
| 121 | |
| 122 | case FDT_PROP: |
| 123 | case FDT_NOP: |
| 124 | break; |
| 125 | |
| 126 | default: |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 127 | return -FDT_ERR_BADSTRUCTURE; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 128 | } |
| 129 | } while (level >= 0); |
| 130 | |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 131 | return -FDT_ERR_NOTFOUND; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 132 | } |
| 133 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 134 | int fdt_subnode_offset(const void *fdt, int parentoffset, |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 135 | const char *name) |
| 136 | { |
| 137 | return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name)); |
| 138 | } |
| 139 | |
David Gibson | 73d6092 | 2006-12-15 15:12:47 +1100 | [diff] [blame] | 140 | int fdt_path_offset(const void *fdt, const char *path) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 141 | { |
| 142 | const char *end = path + strlen(path); |
| 143 | const char *p = path; |
| 144 | int offset = 0; |
| 145 | |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 146 | CHECK_HEADER(fdt); |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 147 | |
| 148 | if (*path != '/') |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 149 | return -FDT_ERR_BADPATH; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 150 | |
| 151 | while (*p) { |
| 152 | const char *q; |
| 153 | |
| 154 | while (*p == '/') |
| 155 | p++; |
| 156 | if (! *p) |
David Gibson | bd2ae2f | 2007-08-29 12:22:50 +1000 | [diff] [blame] | 157 | return offset; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 158 | q = strchr(p, '/'); |
| 159 | if (! q) |
| 160 | q = end; |
| 161 | |
| 162 | offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p); |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 163 | if (offset < 0) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 164 | return offset; |
| 165 | |
| 166 | p = q; |
| 167 | } |
| 168 | |
| 169 | return offset; |
| 170 | } |
| 171 | |
David Gibson | 9d26eab | 2007-08-30 14:54:04 +1000 | [diff] [blame] | 172 | const char *fdt_get_name(const void *fdt, int nodeoffset, int *len) |
| 173 | { |
| 174 | const struct fdt_node_header *nh; |
| 175 | int err; |
| 176 | |
| 177 | if ((err = _fdt_check_header(fdt)) != 0) |
| 178 | goto fail; |
| 179 | |
| 180 | err = -FDT_ERR_BADOFFSET; |
| 181 | nh = fdt_offset_ptr(fdt, nodeoffset, sizeof(*nh)); |
| 182 | if (!nh || (fdt32_to_cpu(nh->tag) != FDT_BEGIN_NODE)) |
| 183 | goto fail; |
| 184 | |
| 185 | if (len) |
| 186 | *len = strlen(nh->name); |
| 187 | |
| 188 | return nh->name; |
| 189 | |
| 190 | fail: |
| 191 | if (len) |
| 192 | *len = err; |
| 193 | return NULL; |
| 194 | } |
| 195 | |
David Gibson | a6c76f9 | 2007-06-13 14:18:10 +1000 | [diff] [blame] | 196 | const struct fdt_property *fdt_get_property(const void *fdt, |
| 197 | int nodeoffset, |
| 198 | const char *name, int *lenp) |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 199 | { |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 200 | int level = 0; |
| 201 | uint32_t tag; |
David Gibson | a6c76f9 | 2007-06-13 14:18:10 +1000 | [diff] [blame] | 202 | const struct fdt_property *prop; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 203 | int namestroff; |
| 204 | int offset, nextoffset; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 205 | int err; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 206 | |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 207 | if ((err = _fdt_check_header(fdt)) != 0) |
| 208 | goto fail; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 209 | |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 210 | err = -FDT_ERR_BADOFFSET; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 211 | if (nodeoffset % FDT_TAGSIZE) |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 212 | goto fail; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 213 | |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 214 | tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset); |
| 215 | if (tag != FDT_BEGIN_NODE) |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 216 | goto fail; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 217 | |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 218 | do { |
| 219 | offset = nextoffset; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 220 | |
| 221 | tag = _fdt_next_tag(fdt, offset, &nextoffset); |
| 222 | switch (tag) { |
| 223 | case FDT_END: |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 224 | err = -FDT_ERR_TRUNCATED; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 225 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 226 | |
| 227 | case FDT_BEGIN_NODE: |
| 228 | level++; |
| 229 | break; |
| 230 | |
| 231 | case FDT_END_NODE: |
| 232 | level--; |
| 233 | break; |
| 234 | |
| 235 | case FDT_PROP: |
| 236 | if (level != 0) |
| 237 | continue; |
| 238 | |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 239 | err = -FDT_ERR_BADSTRUCTURE; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 240 | prop = fdt_offset_ptr_typed(fdt, offset, prop); |
| 241 | if (! prop) |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 242 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 243 | namestroff = fdt32_to_cpu(prop->nameoff); |
| 244 | if (streq(fdt_string(fdt, namestroff), name)) { |
| 245 | /* Found it! */ |
| 246 | int len = fdt32_to_cpu(prop->len); |
| 247 | prop = fdt_offset_ptr(fdt, offset, |
David Gibson | 9825f82 | 2006-12-14 15:29:25 +1100 | [diff] [blame] | 248 | sizeof(*prop)+len); |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 249 | if (! prop) |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 250 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 251 | |
| 252 | if (lenp) |
| 253 | *lenp = len; |
| 254 | |
| 255 | return prop; |
| 256 | } |
| 257 | break; |
| 258 | |
| 259 | case FDT_NOP: |
| 260 | break; |
| 261 | |
| 262 | default: |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 263 | err = -FDT_ERR_BADSTRUCTURE; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 264 | goto fail; |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 265 | } |
| 266 | } while (level >= 0); |
| 267 | |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 268 | err = -FDT_ERR_NOTFOUND; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 269 | fail: |
| 270 | if (lenp) |
David Gibson | 9a9fdf5 | 2006-12-15 15:12:51 +1100 | [diff] [blame] | 271 | *lenp = err; |
David Gibson | a7ee95d | 2006-12-15 15:12:49 +1100 | [diff] [blame] | 272 | return NULL; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 273 | } |
| 274 | |
David Gibson | a6c76f9 | 2007-06-13 14:18:10 +1000 | [diff] [blame] | 275 | const void *fdt_getprop(const void *fdt, int nodeoffset, |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 276 | const char *name, int *lenp) |
| 277 | { |
| 278 | const struct fdt_property *prop; |
David Gibson | 3da0f9a | 2006-11-27 16:21:28 +1100 | [diff] [blame] | 279 | |
David Gibson | 94993f4 | 2006-12-11 16:15:34 +1100 | [diff] [blame] | 280 | prop = fdt_get_property(fdt, nodeoffset, name, 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 | |
| 287 | int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen) |
| 288 | { |
| 289 | uint32_t tag; |
| 290 | int p = 0, overflow = 0; |
| 291 | int offset, nextoffset, namelen; |
| 292 | const char *name; |
| 293 | |
| 294 | CHECK_HEADER(fdt); |
| 295 | |
| 296 | tag = _fdt_next_tag(fdt, 0, &nextoffset); |
| 297 | if (tag != FDT_BEGIN_NODE) |
| 298 | return -FDT_ERR_BADSTRUCTURE; |
| 299 | |
| 300 | if (buflen < 2) |
| 301 | return -FDT_ERR_NOSPACE; |
| 302 | buf[0] = '/'; |
| 303 | p = 1; |
| 304 | |
| 305 | while (offset < nodeoffset) { |
| 306 | offset = nextoffset; |
| 307 | tag = _fdt_next_tag(fdt, offset, &nextoffset); |
| 308 | switch (tag) { |
| 309 | case FDT_END: |
| 310 | return -FDT_ERR_BADOFFSET; |
| 311 | |
| 312 | case FDT_BEGIN_NODE: |
| 313 | name = fdt_get_name(fdt, offset, &namelen); |
| 314 | if (!name) |
| 315 | return namelen; |
| 316 | if (overflow || ((p + namelen + 1) > buflen)) { |
| 317 | overflow++; |
| 318 | break; |
| 319 | } |
| 320 | memcpy(buf + p, name, namelen); |
| 321 | p += namelen; |
| 322 | buf[p++] = '/'; |
| 323 | break; |
| 324 | |
| 325 | case FDT_END_NODE: |
| 326 | if (overflow) { |
| 327 | overflow--; |
| 328 | break; |
| 329 | } |
| 330 | do { |
| 331 | p--; |
| 332 | } while (buf[p-1] != '/'); |
| 333 | break; |
| 334 | |
| 335 | case FDT_PROP: |
| 336 | case FDT_NOP: |
| 337 | break; |
| 338 | |
| 339 | default: |
| 340 | return -FDT_ERR_BADSTRUCTURE; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if (overflow) |
| 345 | return -FDT_ERR_NOSPACE; |
| 346 | |
| 347 | if (p > 1) /* special case so that root path is "/", not "" */ |
| 348 | p--; |
| 349 | buf[p] = '\0'; |
| 350 | return p; |
| 351 | } |
David Gibson | 1248237 | 2007-08-30 14:54:04 +1000 | [diff] [blame^] | 352 | |
| 353 | int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, |
| 354 | int supernodedepth, int *nodedepth) |
| 355 | { |
| 356 | int level = -1; |
| 357 | uint32_t tag; |
| 358 | int offset, nextoffset = 0; |
| 359 | int supernodeoffset = -FDT_ERR_INTERNAL; |
| 360 | |
| 361 | CHECK_HEADER(fdt); |
| 362 | |
| 363 | if (supernodedepth < 0) |
| 364 | return -FDT_ERR_NOTFOUND; |
| 365 | |
| 366 | do { |
| 367 | offset = nextoffset; |
| 368 | tag = _fdt_next_tag(fdt, offset, &nextoffset); |
| 369 | switch (tag) { |
| 370 | case FDT_END: |
| 371 | return -FDT_ERR_BADOFFSET; |
| 372 | |
| 373 | case FDT_BEGIN_NODE: |
| 374 | level++; |
| 375 | if (level == supernodedepth) |
| 376 | supernodeoffset = offset; |
| 377 | break; |
| 378 | |
| 379 | case FDT_END_NODE: |
| 380 | level--; |
| 381 | break; |
| 382 | |
| 383 | case FDT_PROP: |
| 384 | case FDT_NOP: |
| 385 | break; |
| 386 | |
| 387 | default: |
| 388 | return -FDT_ERR_BADSTRUCTURE; |
| 389 | } |
| 390 | } while (offset < nodeoffset); |
| 391 | |
| 392 | if (nodedepth) |
| 393 | *nodedepth = level; |
| 394 | |
| 395 | if (supernodedepth > level) |
| 396 | return -FDT_ERR_NOTFOUND; |
| 397 | return supernodeoffset; |
| 398 | } |
| 399 | |
| 400 | int fdt_node_depth(const void *fdt, int nodeoffset) |
| 401 | { |
| 402 | int nodedepth; |
| 403 | int err; |
| 404 | |
| 405 | err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth); |
| 406 | if (err) |
| 407 | return (err < 0) ? err : -FDT_ERR_INTERNAL; |
| 408 | return nodedepth; |
| 409 | } |
| 410 | |
| 411 | int fdt_parent_offset(const void *fdt, int nodeoffset) |
| 412 | { |
| 413 | int nodedepth = fdt_node_depth(fdt, nodeoffset); |
| 414 | |
| 415 | if (nodedepth < 0) |
| 416 | return nodedepth; |
| 417 | return fdt_supernode_atdepth_offset(fdt, nodeoffset, |
| 418 | nodedepth - 1, NULL); |
| 419 | } |