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