Tom Rini | 4549e78 | 2018-05-06 18:27:01 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 2 | /* |
| 3 | * libfdt - Flat Device Tree manipulation |
| 4 | * Copyright (C) 2006 David Gibson, IBM Corporation. |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 5 | */ |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 6 | #include <linux/libfdt_env.h> |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 7 | |
Bartlomiej Sieka | 8cf3080 | 2008-02-29 16:00:24 +0100 | [diff] [blame] | 8 | #ifndef USE_HOSTCC |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 9 | #include <fdt.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 10 | #include <linux/libfdt.h> |
Bartlomiej Sieka | 8cf3080 | 2008-02-29 16:00:24 +0100 | [diff] [blame] | 11 | #else |
| 12 | #include "fdt_host.h" |
| 13 | #endif |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 14 | |
| 15 | #include "libfdt_internal.h" |
| 16 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 17 | static int _fdt_nodename_eq(const void *fdt, int offset, |
| 18 | const char *s, int len) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 19 | { |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 20 | 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] | 21 | |
Robert P. J. Day | 6feed2a | 2016-05-23 05:40:55 -0400 | [diff] [blame] | 22 | if (!p) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 23 | /* short match */ |
| 24 | return 0; |
| 25 | |
| 26 | if (memcmp(p, s, len) != 0) |
| 27 | return 0; |
| 28 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 29 | if (p[len] == '\0') |
| 30 | return 1; |
| 31 | else if (!memchr(s, '@', len) && (p[len] == '@')) |
| 32 | return 1; |
| 33 | else |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 34 | return 0; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 37 | const char *fdt_string(const void *fdt, int stroffset) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 38 | { |
David Gibson | c668302 | 2008-07-07 10:14:15 +1000 | [diff] [blame] | 39 | return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 40 | } |
| 41 | |
David Gibson | 0219399 | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 42 | static int _fdt_string_eq(const void *fdt, int stroffset, |
| 43 | const char *s, int len) |
| 44 | { |
| 45 | const char *p = fdt_string(fdt, stroffset); |
| 46 | |
Jon Nalley | af67b25 | 2014-02-26 11:32:21 -0500 | [diff] [blame] | 47 | return (strnlen(p, len + 1) == len) && (memcmp(p, s, len) == 0); |
David Gibson | 0219399 | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 48 | } |
| 49 | |
Maxime Ripard | 57c7809 | 2016-07-05 10:26:39 +0200 | [diff] [blame] | 50 | uint32_t fdt_get_max_phandle(const void *fdt) |
| 51 | { |
| 52 | uint32_t max_phandle = 0; |
| 53 | int offset; |
| 54 | |
| 55 | for (offset = fdt_next_node(fdt, -1, NULL);; |
| 56 | offset = fdt_next_node(fdt, offset, NULL)) { |
| 57 | uint32_t phandle; |
| 58 | |
| 59 | if (offset == -FDT_ERR_NOTFOUND) |
| 60 | return max_phandle; |
| 61 | |
| 62 | if (offset < 0) |
Simon Glass | 9c07b98 | 2016-10-02 17:59:27 -0600 | [diff] [blame] | 63 | return (uint32_t)-1; |
Maxime Ripard | 57c7809 | 2016-07-05 10:26:39 +0200 | [diff] [blame] | 64 | |
| 65 | phandle = fdt_get_phandle(fdt, offset); |
| 66 | if (phandle == (uint32_t)-1) |
Simon Glass | 9c07b98 | 2016-10-02 17:59:27 -0600 | [diff] [blame] | 67 | continue; |
Maxime Ripard | 57c7809 | 2016-07-05 10:26:39 +0200 | [diff] [blame] | 68 | |
| 69 | if (phandle > max_phandle) |
| 70 | max_phandle = phandle; |
| 71 | } |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
Thierry Reding | ea1df3e | 2019-03-21 19:09:58 +0100 | [diff] [blame] | 76 | int fdt_generate_phandle(const void *fdt, uint32_t *phandle) |
| 77 | { |
| 78 | uint32_t max = 0; |
| 79 | int offset = -1; |
| 80 | |
| 81 | while (true) { |
| 82 | uint32_t value; |
| 83 | |
| 84 | offset = fdt_next_node(fdt, offset, NULL); |
| 85 | if (offset < 0) { |
| 86 | if (offset == -FDT_ERR_NOTFOUND) |
| 87 | break; |
| 88 | |
| 89 | return offset; |
| 90 | } |
| 91 | |
| 92 | value = fdt_get_phandle(fdt, offset); |
| 93 | |
| 94 | if (value > max) |
| 95 | max = value; |
| 96 | } |
| 97 | |
| 98 | if (max == FDT_MAX_PHANDLE) |
| 99 | return -FDT_ERR_NOPHANDLES; |
| 100 | |
| 101 | if (phandle) |
| 102 | *phandle = max + 1; |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 107 | int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size) |
| 108 | { |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 109 | FDT_CHECK_HEADER(fdt); |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 110 | *address = fdt64_to_cpu(fdt_mem_rsv_(fdt, n)->address); |
| 111 | *size = fdt64_to_cpu(fdt_mem_rsv_(fdt, n)->size); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | int fdt_num_mem_rsv(const void *fdt) |
| 116 | { |
| 117 | int i = 0; |
| 118 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 119 | while (fdt64_to_cpu(fdt_mem_rsv_(fdt, i)->size) != 0) |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 120 | i++; |
| 121 | return i; |
| 122 | } |
| 123 | |
David Gibson | d1c6314 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 124 | static int _nextprop(const void *fdt, int offset) |
| 125 | { |
| 126 | uint32_t tag; |
| 127 | int nextoffset; |
| 128 | |
| 129 | do { |
| 130 | tag = fdt_next_tag(fdt, offset, &nextoffset); |
| 131 | |
| 132 | switch (tag) { |
| 133 | case FDT_END: |
| 134 | if (nextoffset >= 0) |
| 135 | return -FDT_ERR_BADSTRUCTURE; |
| 136 | else |
| 137 | return nextoffset; |
| 138 | |
| 139 | case FDT_PROP: |
| 140 | return offset; |
| 141 | } |
| 142 | offset = nextoffset; |
| 143 | } while (tag == FDT_NOP); |
| 144 | |
| 145 | return -FDT_ERR_NOTFOUND; |
| 146 | } |
| 147 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 148 | int fdt_subnode_offset_namelen(const void *fdt, int offset, |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 149 | const char *name, int namelen) |
| 150 | { |
David Gibson | 2c0b843 | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 151 | int depth; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 152 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 153 | FDT_CHECK_HEADER(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 154 | |
David Gibson | 2c0b843 | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 155 | for (depth = 0; |
| 156 | (offset >= 0) && (depth >= 0); |
| 157 | offset = fdt_next_node(fdt, offset, &depth)) |
| 158 | if ((depth == 1) |
| 159 | && _fdt_nodename_eq(fdt, offset, name, namelen)) |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 160 | return offset; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 161 | |
David Gibson | 2c0b843 | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 162 | if (depth < 0) |
David Gibson | 4bc7dee | 2008-10-29 23:27:45 -0500 | [diff] [blame] | 163 | return -FDT_ERR_NOTFOUND; |
David Gibson | 2c0b843 | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 164 | return offset; /* error */ |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | int fdt_subnode_offset(const void *fdt, int parentoffset, |
| 168 | const char *name) |
| 169 | { |
| 170 | return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name)); |
| 171 | } |
| 172 | |
Hans de Goede | 77d7fff | 2015-04-20 11:13:37 +0200 | [diff] [blame] | 173 | /* |
Maxime Ripard | 6f5f92c | 2016-07-05 10:26:40 +0200 | [diff] [blame] | 174 | * Find the next of path separator, note we need to search for both '/' and ':' |
Robert P. J. Day | 6feed2a | 2016-05-23 05:40:55 -0400 | [diff] [blame] | 175 | * and then take the first one so that we do the right thing for e.g. |
Hans de Goede | 77d7fff | 2015-04-20 11:13:37 +0200 | [diff] [blame] | 176 | * "foo/bar:option" and "bar:option/otheroption", both of which happen, so |
| 177 | * first searching for either ':' or '/' does not work. |
| 178 | */ |
Maxime Ripard | 8e96857 | 2016-07-05 10:26:41 +0200 | [diff] [blame] | 179 | static const char *fdt_path_next_separator(const char *path, int len) |
Hans de Goede | 77d7fff | 2015-04-20 11:13:37 +0200 | [diff] [blame] | 180 | { |
Maxime Ripard | 8e96857 | 2016-07-05 10:26:41 +0200 | [diff] [blame] | 181 | const void *sep1 = memchr(path, '/', len); |
| 182 | const void *sep2 = memchr(path, ':', len); |
Hans de Goede | 77d7fff | 2015-04-20 11:13:37 +0200 | [diff] [blame] | 183 | |
| 184 | if (sep1 && sep2) |
| 185 | return (sep1 < sep2) ? sep1 : sep2; |
| 186 | else if (sep1) |
| 187 | return sep1; |
| 188 | else |
| 189 | return sep2; |
| 190 | } |
| 191 | |
Maxime Ripard | 8e96857 | 2016-07-05 10:26:41 +0200 | [diff] [blame] | 192 | int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 193 | { |
Maxime Ripard | 8e96857 | 2016-07-05 10:26:41 +0200 | [diff] [blame] | 194 | const char *end = path + namelen; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 195 | const char *p = path; |
| 196 | int offset = 0; |
| 197 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 198 | FDT_CHECK_HEADER(fdt); |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 199 | |
Kumar Gala | feeca3f | 2008-08-14 08:28:19 -0500 | [diff] [blame] | 200 | /* see if we have an alias */ |
| 201 | if (*path != '/') { |
Maxime Ripard | 8e96857 | 2016-07-05 10:26:41 +0200 | [diff] [blame] | 202 | const char *q = fdt_path_next_separator(path, namelen); |
Kumar Gala | feeca3f | 2008-08-14 08:28:19 -0500 | [diff] [blame] | 203 | |
Kumar Gala | feeca3f | 2008-08-14 08:28:19 -0500 | [diff] [blame] | 204 | if (!q) |
| 205 | q = end; |
| 206 | |
David Gibson | 9a6cf73 | 2008-08-20 16:55:14 +1000 | [diff] [blame] | 207 | p = fdt_get_alias_namelen(fdt, p, q - p); |
Kumar Gala | feeca3f | 2008-08-14 08:28:19 -0500 | [diff] [blame] | 208 | if (!p) |
| 209 | return -FDT_ERR_BADPATH; |
| 210 | offset = fdt_path_offset(fdt, p); |
| 211 | |
| 212 | p = q; |
| 213 | } |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 214 | |
Maxime Ripard | 8e96857 | 2016-07-05 10:26:41 +0200 | [diff] [blame] | 215 | while (*p && (p < end)) { |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 216 | const char *q; |
| 217 | |
| 218 | while (*p == '/') |
| 219 | p++; |
Maxime Ripard | 8e96857 | 2016-07-05 10:26:41 +0200 | [diff] [blame] | 220 | |
Hans de Goede | 77d7fff | 2015-04-20 11:13:37 +0200 | [diff] [blame] | 221 | if (*p == '\0' || *p == ':') |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 222 | return offset; |
Maxime Ripard | 8e96857 | 2016-07-05 10:26:41 +0200 | [diff] [blame] | 223 | |
| 224 | q = fdt_path_next_separator(p, end - p); |
Robert P. J. Day | 6feed2a | 2016-05-23 05:40:55 -0400 | [diff] [blame] | 225 | if (!q) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 226 | q = end; |
| 227 | |
| 228 | offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p); |
| 229 | if (offset < 0) |
| 230 | return offset; |
| 231 | |
| 232 | p = q; |
| 233 | } |
| 234 | |
Gerald Van Baren | aea03c4 | 2007-03-31 14:30:53 -0400 | [diff] [blame] | 235 | return offset; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 236 | } |
| 237 | |
Simon Glass | 42b7600 | 2016-10-02 17:59:30 -0600 | [diff] [blame] | 238 | int fdt_path_offset(const void *fdt, const char *path) |
| 239 | { |
| 240 | return fdt_path_offset_namelen(fdt, path, strlen(path)); |
| 241 | } |
| 242 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 243 | 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] | 244 | { |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 245 | const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 246 | int err; |
| 247 | |
David Gibson | 2f08bfa | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 248 | if (((err = fdt_check_header(fdt)) != 0) |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 249 | || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0)) |
David Gibson | 2f08bfa | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 250 | goto fail; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 251 | |
| 252 | if (len) |
| 253 | *len = strlen(nh->name); |
| 254 | |
| 255 | return nh->name; |
| 256 | |
| 257 | fail: |
| 258 | if (len) |
| 259 | *len = err; |
| 260 | return NULL; |
| 261 | } |
| 262 | |
David Gibson | d1c6314 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 263 | int fdt_first_property_offset(const void *fdt, int nodeoffset) |
| 264 | { |
| 265 | int offset; |
| 266 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 267 | if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0) |
David Gibson | d1c6314 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 268 | return offset; |
| 269 | |
| 270 | return _nextprop(fdt, offset); |
| 271 | } |
| 272 | |
| 273 | int fdt_next_property_offset(const void *fdt, int offset) |
| 274 | { |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 275 | if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0) |
David Gibson | d1c6314 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 276 | return offset; |
| 277 | |
| 278 | return _nextprop(fdt, offset); |
| 279 | } |
| 280 | |
| 281 | const struct fdt_property *fdt_get_property_by_offset(const void *fdt, |
| 282 | int offset, |
| 283 | int *lenp) |
| 284 | { |
| 285 | int err; |
| 286 | const struct fdt_property *prop; |
| 287 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 288 | if ((err = fdt_check_prop_offset_(fdt, offset)) < 0) { |
David Gibson | d1c6314 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 289 | if (lenp) |
| 290 | *lenp = err; |
| 291 | return NULL; |
| 292 | } |
| 293 | |
Rob Herring | db405d1 | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 294 | prop = fdt_offset_ptr_(fdt, offset); |
David Gibson | d1c6314 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 295 | |
| 296 | if (lenp) |
| 297 | *lenp = fdt32_to_cpu(prop->len); |
| 298 | |
| 299 | return prop; |
| 300 | } |
| 301 | |
David Gibson | 0219399 | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 302 | const struct fdt_property *fdt_get_property_namelen(const void *fdt, |
David Gibson | d1c6314 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 303 | int offset, |
David Gibson | 0219399 | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 304 | const char *name, |
| 305 | int namelen, int *lenp) |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 306 | { |
David Gibson | d1c6314 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 307 | for (offset = fdt_first_property_offset(fdt, offset); |
| 308 | (offset >= 0); |
| 309 | (offset = fdt_next_property_offset(fdt, offset))) { |
| 310 | const struct fdt_property *prop; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 311 | |
David Gibson | d1c6314 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 312 | if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) { |
| 313 | offset = -FDT_ERR_INTERNAL; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 314 | break; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 315 | } |
David Gibson | d1c6314 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 316 | if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff), |
| 317 | name, namelen)) |
| 318 | return prop; |
| 319 | } |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 320 | |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 321 | if (lenp) |
David Gibson | d1c6314 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 322 | *lenp = offset; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 323 | return NULL; |
| 324 | } |
| 325 | |
David Gibson | 0219399 | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 326 | const struct fdt_property *fdt_get_property(const void *fdt, |
| 327 | int nodeoffset, |
| 328 | const char *name, int *lenp) |
| 329 | { |
| 330 | return fdt_get_property_namelen(fdt, nodeoffset, name, |
| 331 | strlen(name), lenp); |
| 332 | } |
| 333 | |
| 334 | const void *fdt_getprop_namelen(const void *fdt, int nodeoffset, |
| 335 | const char *name, int namelen, int *lenp) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 336 | { |
| 337 | const struct fdt_property *prop; |
| 338 | |
David Gibson | 0219399 | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 339 | prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp); |
Robert P. J. Day | 6feed2a | 2016-05-23 05:40:55 -0400 | [diff] [blame] | 340 | if (!prop) |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 341 | return NULL; |
| 342 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 343 | return prop->data; |
Gerald Van Baren | 3574817 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 344 | } |
Gerald Van Baren | 3af0d58 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 345 | |
David Gibson | d1c6314 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 346 | const void *fdt_getprop_by_offset(const void *fdt, int offset, |
| 347 | const char **namep, int *lenp) |
| 348 | { |
| 349 | const struct fdt_property *prop; |
| 350 | |
| 351 | prop = fdt_get_property_by_offset(fdt, offset, lenp); |
| 352 | if (!prop) |
| 353 | return NULL; |
| 354 | if (namep) |
| 355 | *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff)); |
| 356 | return prop->data; |
| 357 | } |
| 358 | |
David Gibson | 0219399 | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 359 | const void *fdt_getprop(const void *fdt, int nodeoffset, |
| 360 | const char *name, int *lenp) |
| 361 | { |
| 362 | return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp); |
| 363 | } |
| 364 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 365 | uint32_t fdt_get_phandle(const void *fdt, int nodeoffset) |
Gerald Van Baren | 3af0d58 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 366 | { |
Kim Phillips | b2ba62a | 2013-01-16 13:59:50 +0000 | [diff] [blame] | 367 | const fdt32_t *php; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 368 | int len; |
| 369 | |
David Gibson | 05a22ba | 2009-11-26 15:37:13 +1100 | [diff] [blame] | 370 | /* FIXME: This is a bit sub-optimal, since we potentially scan |
| 371 | * over all the properties twice. */ |
| 372 | php = fdt_getprop(fdt, nodeoffset, "phandle", &len); |
| 373 | if (!php || (len != sizeof(*php))) { |
| 374 | php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len); |
| 375 | if (!php || (len != sizeof(*php))) |
| 376 | return 0; |
| 377 | } |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 378 | |
| 379 | return fdt32_to_cpu(*php); |
| 380 | } |
| 381 | |
David Gibson | 9a6cf73 | 2008-08-20 16:55:14 +1000 | [diff] [blame] | 382 | const char *fdt_get_alias_namelen(const void *fdt, |
| 383 | const char *name, int namelen) |
| 384 | { |
| 385 | int aliasoffset; |
| 386 | |
| 387 | aliasoffset = fdt_path_offset(fdt, "/aliases"); |
| 388 | if (aliasoffset < 0) |
| 389 | return NULL; |
| 390 | |
| 391 | return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL); |
| 392 | } |
| 393 | |
| 394 | const char *fdt_get_alias(const void *fdt, const char *name) |
| 395 | { |
| 396 | return fdt_get_alias_namelen(fdt, name, strlen(name)); |
| 397 | } |
| 398 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 399 | int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen) |
| 400 | { |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 401 | int pdepth = 0, p = 0; |
| 402 | int offset, depth, namelen; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 403 | const char *name; |
Gerald Van Baren | 3af0d58 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 404 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 405 | FDT_CHECK_HEADER(fdt); |
Gerald Van Baren | 3af0d58 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 406 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 407 | if (buflen < 2) |
Gerald Van Baren | 3f9f08c | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 408 | return -FDT_ERR_NOSPACE; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 409 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 410 | for (offset = 0, depth = 0; |
| 411 | (offset >= 0) && (offset <= nodeoffset); |
| 412 | offset = fdt_next_node(fdt, offset, &depth)) { |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 413 | while (pdepth > depth) { |
| 414 | do { |
| 415 | p--; |
| 416 | } while (buf[p-1] != '/'); |
| 417 | pdepth--; |
| 418 | } |
| 419 | |
David Gibson | bbdbc7c | 2008-08-29 14:19:13 +1000 | [diff] [blame] | 420 | if (pdepth >= depth) { |
| 421 | name = fdt_get_name(fdt, offset, &namelen); |
| 422 | if (!name) |
| 423 | return namelen; |
| 424 | if ((p + namelen + 1) <= buflen) { |
| 425 | memcpy(buf + p, name, namelen); |
| 426 | p += namelen; |
| 427 | buf[p++] = '/'; |
| 428 | pdepth++; |
| 429 | } |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 430 | } |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 431 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 432 | if (offset == nodeoffset) { |
| 433 | if (pdepth < (depth + 1)) |
| 434 | return -FDT_ERR_NOSPACE; |
| 435 | |
| 436 | if (p > 1) /* special case so that root path is "/", not "" */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 437 | p--; |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 438 | buf[p] = '\0'; |
David Gibson | bbdbc7c | 2008-08-29 14:19:13 +1000 | [diff] [blame] | 439 | return 0; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 443 | if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) |
| 444 | return -FDT_ERR_BADOFFSET; |
| 445 | else if (offset == -FDT_ERR_BADOFFSET) |
| 446 | return -FDT_ERR_BADSTRUCTURE; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 447 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 448 | return offset; /* error from fdt_next_node() */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, |
| 452 | int supernodedepth, int *nodedepth) |
| 453 | { |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 454 | int offset, depth; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 455 | int supernodeoffset = -FDT_ERR_INTERNAL; |
| 456 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 457 | FDT_CHECK_HEADER(fdt); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 458 | |
| 459 | if (supernodedepth < 0) |
| 460 | return -FDT_ERR_NOTFOUND; |
| 461 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 462 | for (offset = 0, depth = 0; |
| 463 | (offset >= 0) && (offset <= nodeoffset); |
| 464 | offset = fdt_next_node(fdt, offset, &depth)) { |
| 465 | if (depth == supernodedepth) |
| 466 | supernodeoffset = offset; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 467 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 468 | if (offset == nodeoffset) { |
| 469 | if (nodedepth) |
| 470 | *nodedepth = depth; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 471 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 472 | if (supernodedepth > depth) |
| 473 | return -FDT_ERR_NOTFOUND; |
| 474 | else |
| 475 | return supernodeoffset; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 476 | } |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 477 | } |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 478 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 479 | if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) |
| 480 | return -FDT_ERR_BADOFFSET; |
| 481 | else if (offset == -FDT_ERR_BADOFFSET) |
| 482 | return -FDT_ERR_BADSTRUCTURE; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 483 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 484 | return offset; /* error from fdt_next_node() */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | int fdt_node_depth(const void *fdt, int nodeoffset) |
| 488 | { |
| 489 | int nodedepth; |
| 490 | int err; |
| 491 | |
| 492 | err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth); |
| 493 | if (err) |
| 494 | return (err < 0) ? err : -FDT_ERR_INTERNAL; |
| 495 | return nodedepth; |
| 496 | } |
| 497 | |
| 498 | int fdt_parent_offset(const void *fdt, int nodeoffset) |
| 499 | { |
| 500 | int nodedepth = fdt_node_depth(fdt, nodeoffset); |
| 501 | |
| 502 | if (nodedepth < 0) |
| 503 | return nodedepth; |
| 504 | return fdt_supernode_atdepth_offset(fdt, nodeoffset, |
| 505 | nodedepth - 1, NULL); |
| 506 | } |
| 507 | |
| 508 | int fdt_node_offset_by_prop_value(const void *fdt, int startoffset, |
| 509 | const char *propname, |
| 510 | const void *propval, int proplen) |
| 511 | { |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 512 | int offset; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 513 | const void *val; |
| 514 | int len; |
| 515 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 516 | FDT_CHECK_HEADER(fdt); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 517 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 518 | /* FIXME: The algorithm here is pretty horrible: we scan each |
| 519 | * property of a node in fdt_getprop(), then if that didn't |
| 520 | * find what we want, we scan over them again making our way |
| 521 | * to the next node. Still it's the easiest to implement |
| 522 | * approach; performance can come later. */ |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 523 | for (offset = fdt_next_node(fdt, startoffset, NULL); |
| 524 | offset >= 0; |
| 525 | offset = fdt_next_node(fdt, offset, NULL)) { |
| 526 | val = fdt_getprop(fdt, offset, propname, &len); |
| 527 | if (val && (len == proplen) |
| 528 | && (memcmp(val, propval, len) == 0)) |
| 529 | return offset; |
| 530 | } |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 531 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 532 | return offset; /* error from fdt_next_node() */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle) |
| 536 | { |
David Gibson | 05a22ba | 2009-11-26 15:37:13 +1100 | [diff] [blame] | 537 | int offset; |
| 538 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 539 | if ((phandle == 0) || (phandle == -1)) |
| 540 | return -FDT_ERR_BADPHANDLE; |
David Gibson | 05a22ba | 2009-11-26 15:37:13 +1100 | [diff] [blame] | 541 | |
| 542 | FDT_CHECK_HEADER(fdt); |
| 543 | |
| 544 | /* FIXME: The algorithm here is pretty horrible: we |
| 545 | * potentially scan each property of a node in |
| 546 | * fdt_get_phandle(), then if that didn't find what |
| 547 | * we want, we scan over them again making our way to the next |
| 548 | * node. Still it's the easiest to implement approach; |
| 549 | * performance can come later. */ |
| 550 | for (offset = fdt_next_node(fdt, -1, NULL); |
| 551 | offset >= 0; |
| 552 | offset = fdt_next_node(fdt, offset, NULL)) { |
| 553 | if (fdt_get_phandle(fdt, offset) == phandle) |
| 554 | return offset; |
| 555 | } |
| 556 | |
| 557 | return offset; /* error from fdt_next_node() */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 558 | } |
| 559 | |
Simon Glass | e853b32 | 2013-01-21 12:59:18 -0800 | [diff] [blame] | 560 | int fdt_stringlist_contains(const char *strlist, int listlen, const char *str) |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 561 | { |
| 562 | int len = strlen(str); |
David Gibson | ef4e8ce | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 563 | const char *p; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 564 | |
| 565 | while (listlen >= len) { |
| 566 | if (memcmp(str, strlist, len+1) == 0) |
| 567 | return 1; |
| 568 | p = memchr(strlist, '\0', listlen); |
| 569 | if (!p) |
| 570 | return 0; /* malformed strlist.. */ |
| 571 | listlen -= (p-strlist) + 1; |
| 572 | strlist = p + 1; |
Gerald Van Baren | 3f9f08c | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 573 | } |
| 574 | return 0; |
| 575 | } |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 576 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 577 | int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property) |
Thierry Reding | bc4147a | 2014-08-26 17:33:50 +0200 | [diff] [blame] | 578 | { |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 579 | const char *list, *end; |
| 580 | int length, count = 0; |
Thierry Reding | bc4147a | 2014-08-26 17:33:50 +0200 | [diff] [blame] | 581 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 582 | list = fdt_getprop(fdt, nodeoffset, property, &length); |
Thierry Reding | bc4147a | 2014-08-26 17:33:50 +0200 | [diff] [blame] | 583 | if (!list) |
Masahiro Yamada | 7c9786d | 2016-10-17 20:22:33 +0900 | [diff] [blame] | 584 | return length; |
Thierry Reding | bc4147a | 2014-08-26 17:33:50 +0200 | [diff] [blame] | 585 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 586 | end = list + length; |
Thierry Reding | bc4147a | 2014-08-26 17:33:50 +0200 | [diff] [blame] | 587 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 588 | while (list < end) { |
| 589 | length = strnlen(list, end - list) + 1; |
| 590 | |
| 591 | /* Abort if the last string isn't properly NUL-terminated. */ |
| 592 | if (list + length > end) |
| 593 | return -FDT_ERR_BADVALUE; |
| 594 | |
| 595 | list += length; |
Thierry Reding | bc4147a | 2014-08-26 17:33:50 +0200 | [diff] [blame] | 596 | count++; |
| 597 | } |
| 598 | |
| 599 | return count; |
| 600 | } |
| 601 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 602 | int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property, |
| 603 | const char *string) |
Thierry Reding | fc503c1 | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 604 | { |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 605 | int length, len, idx = 0; |
Thierry Reding | fc503c1 | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 606 | const char *list, *end; |
Thierry Reding | fc503c1 | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 607 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 608 | list = fdt_getprop(fdt, nodeoffset, property, &length); |
Thierry Reding | fc503c1 | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 609 | if (!list) |
Masahiro Yamada | 01ae56c | 2016-10-17 20:22:34 +0900 | [diff] [blame] | 610 | return length; |
Thierry Reding | fc503c1 | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 611 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 612 | len = strlen(string) + 1; |
| 613 | end = list + length; |
Thierry Reding | fc503c1 | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 614 | |
| 615 | while (list < end) { |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 616 | length = strnlen(list, end - list) + 1; |
Thierry Reding | fc503c1 | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 617 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 618 | /* Abort if the last string isn't properly NUL-terminated. */ |
| 619 | if (list + length > end) |
| 620 | return -FDT_ERR_BADVALUE; |
Thierry Reding | fc503c1 | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 621 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 622 | if (length == len && memcmp(list, string, length) == 0) |
| 623 | return idx; |
| 624 | |
| 625 | list += length; |
| 626 | idx++; |
Thierry Reding | fc503c1 | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | return -FDT_ERR_NOTFOUND; |
| 630 | } |
| 631 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 632 | const char *fdt_stringlist_get(const void *fdt, int nodeoffset, |
| 633 | const char *property, int idx, |
| 634 | int *lenp) |
Thierry Reding | 5094eb4 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 635 | { |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 636 | const char *list, *end; |
| 637 | int length; |
Thierry Reding | 5094eb4 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 638 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 639 | list = fdt_getprop(fdt, nodeoffset, property, &length); |
| 640 | if (!list) { |
| 641 | if (lenp) |
| 642 | *lenp = length; |
Thierry Reding | 5094eb4 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 643 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 644 | return NULL; |
| 645 | } |
Thierry Reding | 5094eb4 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 646 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 647 | end = list + length; |
| 648 | |
| 649 | while (list < end) { |
| 650 | length = strnlen(list, end - list) + 1; |
| 651 | |
| 652 | /* Abort if the last string isn't properly NUL-terminated. */ |
| 653 | if (list + length > end) { |
| 654 | if (lenp) |
| 655 | *lenp = -FDT_ERR_BADVALUE; |
| 656 | |
| 657 | return NULL; |
Thierry Reding | 5094eb4 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 658 | } |
| 659 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 660 | if (idx == 0) { |
| 661 | if (lenp) |
| 662 | *lenp = length - 1; |
| 663 | |
| 664 | return list; |
| 665 | } |
| 666 | |
| 667 | list += length; |
| 668 | idx--; |
Thierry Reding | 5094eb4 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 669 | } |
| 670 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 671 | if (lenp) |
| 672 | *lenp = -FDT_ERR_NOTFOUND; |
Thierry Reding | 5094eb4 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 673 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 674 | return NULL; |
Thierry Reding | 5094eb4 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 677 | int fdt_node_check_compatible(const void *fdt, int nodeoffset, |
| 678 | const char *compatible) |
| 679 | { |
| 680 | const void *prop; |
| 681 | int len; |
| 682 | |
| 683 | prop = fdt_getprop(fdt, nodeoffset, "compatible", &len); |
| 684 | if (!prop) |
| 685 | return len; |
Simon Glass | 9c07b98 | 2016-10-02 17:59:27 -0600 | [diff] [blame] | 686 | |
| 687 | return !fdt_stringlist_contains(prop, len, compatible); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | int fdt_node_offset_by_compatible(const void *fdt, int startoffset, |
| 691 | const char *compatible) |
| 692 | { |
David Gibson | 11abe45 | 2008-02-18 18:09:04 +1100 | [diff] [blame] | 693 | int offset, err; |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 694 | |
David Gibson | fc7758e | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 695 | FDT_CHECK_HEADER(fdt); |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 696 | |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 697 | /* FIXME: The algorithm here is pretty horrible: we scan each |
| 698 | * property of a node in fdt_node_check_compatible(), then if |
| 699 | * that didn't find what we want, we scan over them again |
| 700 | * making our way to the next node. Still it's the easiest to |
| 701 | * implement approach; performance can come later. */ |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 702 | for (offset = fdt_next_node(fdt, startoffset, NULL); |
| 703 | offset >= 0; |
| 704 | offset = fdt_next_node(fdt, offset, NULL)) { |
| 705 | err = fdt_node_check_compatible(fdt, offset, compatible); |
| 706 | if ((err < 0) && (err != -FDT_ERR_NOTFOUND)) |
| 707 | return err; |
| 708 | else if (err == 0) |
| 709 | return offset; |
| 710 | } |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 711 | |
David Gibson | ae0b590 | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 712 | return offset; /* error from fdt_next_node() */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 713 | } |