Tom Rini | 8b0c8a1 | 2018-05-06 18:27:01 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause |
Gerald Van Baren | 9e61ed8 | 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 | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 5 | */ |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 6 | #include <linux/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> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 10 | #include <linux/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 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 17 | static int fdt_nodename_eq_(const void *fdt, int offset, |
David Gibson | 9457d9d | 2008-07-09 14:10:24 +1000 | [diff] [blame] | 18 | const char *s, int len) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 19 | { |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 20 | int olen; |
| 21 | const char *p = fdt_get_name(fdt, offset, &olen); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 22 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 23 | if (!p || (fdt_chk_extra() && olen < len)) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 24 | /* short match */ |
| 25 | return 0; |
| 26 | |
| 27 | if (memcmp(p, s, len) != 0) |
| 28 | return 0; |
| 29 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 30 | if (p[len] == '\0') |
| 31 | return 1; |
| 32 | else if (!memchr(s, '@', len) && (p[len] == '@')) |
| 33 | return 1; |
| 34 | else |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 35 | return 0; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 36 | } |
| 37 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 38 | const char *fdt_get_string(const void *fdt, int stroffset, int *lenp) |
Gerald Van Baren | 18c75c6 | 2007-05-17 23:54:36 -0400 | [diff] [blame] | 39 | { |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 40 | int32_t totalsize; |
| 41 | uint32_t absoffset; |
| 42 | size_t len; |
| 43 | int err; |
| 44 | const char *s, *n; |
Gerald Van Baren | 18c75c6 | 2007-05-17 23:54:36 -0400 | [diff] [blame] | 45 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 46 | if (!fdt_chk_extra()) { |
| 47 | s = (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset; |
David Gibson | c5b130e | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 48 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 49 | if (lenp) |
| 50 | *lenp = strlen(s); |
| 51 | return s; |
| 52 | } |
| 53 | totalsize = fdt_ro_probe_(fdt); |
| 54 | err = totalsize; |
| 55 | if (totalsize < 0) |
| 56 | goto fail; |
David Gibson | c5b130e | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 57 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 58 | err = -FDT_ERR_BADOFFSET; |
| 59 | absoffset = stroffset + fdt_off_dt_strings(fdt); |
| 60 | if (absoffset >= totalsize) |
| 61 | goto fail; |
| 62 | len = totalsize - absoffset; |
Maxime Ripard | 2125ca5 | 2016-07-05 10:26:39 +0200 | [diff] [blame] | 63 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 64 | if (fdt_magic(fdt) == FDT_MAGIC) { |
| 65 | if (stroffset < 0) |
| 66 | goto fail; |
| 67 | if (!fdt_chk_version() || fdt_version(fdt) >= 17) { |
| 68 | if (stroffset >= fdt_size_dt_strings(fdt)) |
| 69 | goto fail; |
| 70 | if ((fdt_size_dt_strings(fdt) - stroffset) < len) |
| 71 | len = fdt_size_dt_strings(fdt) - stroffset; |
| 72 | } |
| 73 | } else if (fdt_magic(fdt) == FDT_SW_MAGIC) { |
| 74 | if ((stroffset >= 0) |
| 75 | || (stroffset < -fdt_size_dt_strings(fdt))) |
| 76 | goto fail; |
| 77 | if ((-stroffset) < len) |
| 78 | len = -stroffset; |
| 79 | } else { |
| 80 | err = -FDT_ERR_INTERNAL; |
| 81 | goto fail; |
| 82 | } |
Maxime Ripard | 2125ca5 | 2016-07-05 10:26:39 +0200 | [diff] [blame] | 83 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 84 | s = (const char *)fdt + absoffset; |
| 85 | n = memchr(s, '\0', len); |
| 86 | if (!n) { |
| 87 | /* missing terminating NULL */ |
| 88 | err = -FDT_ERR_TRUNCATED; |
| 89 | goto fail; |
| 90 | } |
Maxime Ripard | 2125ca5 | 2016-07-05 10:26:39 +0200 | [diff] [blame] | 91 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 92 | if (lenp) |
| 93 | *lenp = n - s; |
| 94 | return s; |
Maxime Ripard | 2125ca5 | 2016-07-05 10:26:39 +0200 | [diff] [blame] | 95 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 96 | fail: |
| 97 | if (lenp) |
| 98 | *lenp = err; |
| 99 | return NULL; |
| 100 | } |
Maxime Ripard | 2125ca5 | 2016-07-05 10:26:39 +0200 | [diff] [blame] | 101 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 102 | const char *fdt_string(const void *fdt, int stroffset) |
| 103 | { |
| 104 | return fdt_get_string(fdt, stroffset, NULL); |
| 105 | } |
Maxime Ripard | 2125ca5 | 2016-07-05 10:26:39 +0200 | [diff] [blame] | 106 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 107 | static int fdt_string_eq_(const void *fdt, int stroffset, |
| 108 | const char *s, int len) |
| 109 | { |
| 110 | int slen; |
| 111 | const char *p = fdt_get_string(fdt, stroffset, &slen); |
| 112 | |
| 113 | return p && (slen == len) && (memcmp(p, s, len) == 0); |
Maxime Ripard | 2125ca5 | 2016-07-05 10:26:39 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 116 | int fdt_find_max_phandle(const void *fdt, uint32_t *phandle) |
Thierry Reding | 0431be0 | 2019-03-21 19:09:58 +0100 | [diff] [blame] | 117 | { |
| 118 | uint32_t max = 0; |
| 119 | int offset = -1; |
| 120 | |
| 121 | while (true) { |
| 122 | uint32_t value; |
| 123 | |
| 124 | offset = fdt_next_node(fdt, offset, NULL); |
| 125 | if (offset < 0) { |
| 126 | if (offset == -FDT_ERR_NOTFOUND) |
| 127 | break; |
| 128 | |
| 129 | return offset; |
| 130 | } |
| 131 | |
| 132 | value = fdt_get_phandle(fdt, offset); |
| 133 | |
| 134 | if (value > max) |
| 135 | max = value; |
| 136 | } |
| 137 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 138 | if (phandle) |
| 139 | *phandle = max; |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | int fdt_generate_phandle(const void *fdt, uint32_t *phandle) |
| 145 | { |
| 146 | uint32_t max; |
| 147 | int err; |
| 148 | |
| 149 | err = fdt_find_max_phandle(fdt, &max); |
| 150 | if (err < 0) |
| 151 | return err; |
| 152 | |
Thierry Reding | 0431be0 | 2019-03-21 19:09:58 +0100 | [diff] [blame] | 153 | if (max == FDT_MAX_PHANDLE) |
| 154 | return -FDT_ERR_NOPHANDLES; |
| 155 | |
| 156 | if (phandle) |
| 157 | *phandle = max + 1; |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 162 | static const struct fdt_reserve_entry *fdt_mem_rsv(const void *fdt, int n) |
| 163 | { |
| 164 | int offset = n * sizeof(struct fdt_reserve_entry); |
| 165 | int absoffset = fdt_off_mem_rsvmap(fdt) + offset; |
| 166 | |
| 167 | if (fdt_chk_extra()) { |
| 168 | if (absoffset < fdt_off_mem_rsvmap(fdt)) |
| 169 | return NULL; |
| 170 | if (absoffset > fdt_totalsize(fdt) - |
| 171 | sizeof(struct fdt_reserve_entry)) |
| 172 | return NULL; |
| 173 | } |
| 174 | return fdt_mem_rsv_(fdt, n); |
| 175 | } |
| 176 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 177 | int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size) |
| 178 | { |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 179 | const struct fdt_reserve_entry *re; |
| 180 | |
| 181 | FDT_RO_PROBE(fdt); |
| 182 | re = fdt_mem_rsv(fdt, n); |
| 183 | if (fdt_chk_extra() && !re) |
| 184 | return -FDT_ERR_BADOFFSET; |
| 185 | |
| 186 | *address = fdt64_ld(&re->address); |
| 187 | *size = fdt64_ld(&re->size); |
Gerald Van Baren | 18c75c6 | 2007-05-17 23:54:36 -0400 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 191 | int fdt_num_mem_rsv(const void *fdt) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 192 | { |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 193 | int i; |
| 194 | const struct fdt_reserve_entry *re; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 195 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 196 | for (i = 0; (re = fdt_mem_rsv(fdt, i)) != NULL; i++) { |
| 197 | if (fdt64_ld(&re->size) == 0) |
| 198 | return i; |
| 199 | } |
| 200 | return -FDT_ERR_TRUNCATED; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 201 | } |
| 202 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 203 | static int nextprop_(const void *fdt, int offset) |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 204 | { |
| 205 | uint32_t tag; |
| 206 | int nextoffset; |
| 207 | |
| 208 | do { |
| 209 | tag = fdt_next_tag(fdt, offset, &nextoffset); |
| 210 | |
| 211 | switch (tag) { |
| 212 | case FDT_END: |
| 213 | if (nextoffset >= 0) |
| 214 | return -FDT_ERR_BADSTRUCTURE; |
| 215 | else |
| 216 | return nextoffset; |
| 217 | |
| 218 | case FDT_PROP: |
| 219 | return offset; |
| 220 | } |
| 221 | offset = nextoffset; |
| 222 | } while (tag == FDT_NOP); |
| 223 | |
| 224 | return -FDT_ERR_NOTFOUND; |
| 225 | } |
| 226 | |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 227 | int fdt_subnode_offset_namelen(const void *fdt, int offset, |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 228 | const char *name, int namelen) |
| 229 | { |
David Gibson | 954914a | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 230 | int depth; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 231 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 232 | FDT_RO_PROBE(fdt); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 233 | |
David Gibson | 954914a | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 234 | for (depth = 0; |
| 235 | (offset >= 0) && (depth >= 0); |
| 236 | offset = fdt_next_node(fdt, offset, &depth)) |
| 237 | if ((depth == 1) |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 238 | && fdt_nodename_eq_(fdt, offset, name, namelen)) |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 239 | return offset; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 240 | |
David Gibson | 954914a | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 241 | if (depth < 0) |
David Gibson | 1c0320e | 2008-10-29 23:27:45 -0500 | [diff] [blame] | 242 | return -FDT_ERR_NOTFOUND; |
David Gibson | 954914a | 2009-02-06 14:01:56 +1100 | [diff] [blame] | 243 | return offset; /* error */ |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | int fdt_subnode_offset(const void *fdt, int parentoffset, |
| 247 | const char *name) |
| 248 | { |
| 249 | return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name)); |
| 250 | } |
| 251 | |
Maxime Ripard | 87390e8 | 2016-07-05 10:26:41 +0200 | [diff] [blame] | 252 | int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 253 | { |
Maxime Ripard | 87390e8 | 2016-07-05 10:26:41 +0200 | [diff] [blame] | 254 | const char *end = path + namelen; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 255 | const char *p = path; |
| 256 | int offset = 0; |
| 257 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 258 | FDT_RO_PROBE(fdt); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 259 | |
Kumar Gala | eb45e03 | 2008-08-14 08:28:19 -0500 | [diff] [blame] | 260 | /* see if we have an alias */ |
| 261 | if (*path != '/') { |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 262 | const char *q = memchr(path, '/', end - p); |
Kumar Gala | eb45e03 | 2008-08-14 08:28:19 -0500 | [diff] [blame] | 263 | |
Kumar Gala | eb45e03 | 2008-08-14 08:28:19 -0500 | [diff] [blame] | 264 | if (!q) |
| 265 | q = end; |
| 266 | |
David Gibson | d6656ea | 2008-08-20 16:55:14 +1000 | [diff] [blame] | 267 | p = fdt_get_alias_namelen(fdt, p, q - p); |
Kumar Gala | eb45e03 | 2008-08-14 08:28:19 -0500 | [diff] [blame] | 268 | if (!p) |
| 269 | return -FDT_ERR_BADPATH; |
| 270 | offset = fdt_path_offset(fdt, p); |
| 271 | |
| 272 | p = q; |
| 273 | } |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 274 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 275 | while (p < end) { |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 276 | const char *q; |
| 277 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 278 | while (*p == '/') { |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 279 | p++; |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 280 | if (p == end) |
| 281 | return offset; |
| 282 | } |
| 283 | q = memchr(p, '/', end - p); |
| 284 | if (! q) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 285 | q = end; |
| 286 | |
| 287 | offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p); |
| 288 | if (offset < 0) |
| 289 | return offset; |
| 290 | |
| 291 | p = q; |
| 292 | } |
| 293 | |
Gerald Van Baren | 3248089 | 2007-03-31 14:30:53 -0400 | [diff] [blame] | 294 | return offset; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 295 | } |
| 296 | |
Simon Glass | a2235e4 | 2016-10-02 17:59:30 -0600 | [diff] [blame] | 297 | int fdt_path_offset(const void *fdt, const char *path) |
| 298 | { |
| 299 | return fdt_path_offset_namelen(fdt, path, strlen(path)); |
| 300 | } |
| 301 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 302 | 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] | 303 | { |
Rob Herring | cd5f70d | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 304 | const struct fdt_node_header *nh = fdt_offset_ptr_(fdt, nodeoffset); |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 305 | const char *nameptr; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 306 | int err; |
| 307 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 308 | if (fdt_chk_extra() && |
| 309 | (((err = fdt_ro_probe_(fdt)) < 0) |
| 310 | || ((err = fdt_check_node_offset_(fdt, nodeoffset)) < 0))) |
| 311 | goto fail; |
| 312 | |
| 313 | nameptr = nh->name; |
| 314 | |
| 315 | if (fdt_chk_version() && fdt_version(fdt) < 0x10) { |
| 316 | /* |
| 317 | * For old FDT versions, match the naming conventions of V16: |
| 318 | * give only the leaf name (after all /). The actual tree |
| 319 | * contents are loosely checked. |
| 320 | */ |
| 321 | const char *leaf; |
| 322 | leaf = strrchr(nameptr, '/'); |
| 323 | if (leaf == NULL) { |
| 324 | err = -FDT_ERR_BADSTRUCTURE; |
David Gibson | 148a49b | 2008-05-20 17:19:11 +1000 | [diff] [blame] | 325 | goto fail; |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 326 | } |
| 327 | nameptr = leaf+1; |
| 328 | } |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 329 | |
| 330 | if (len) |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 331 | *len = strlen(nameptr); |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 332 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 333 | return nameptr; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 334 | |
| 335 | fail: |
| 336 | if (len) |
| 337 | *len = err; |
| 338 | return NULL; |
| 339 | } |
| 340 | |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 341 | int fdt_first_property_offset(const void *fdt, int nodeoffset) |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 342 | { |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 343 | int offset; |
| 344 | |
Rob Herring | cd5f70d | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 345 | if ((offset = fdt_check_node_offset_(fdt, nodeoffset)) < 0) |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 346 | return offset; |
| 347 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 348 | return nextprop_(fdt, offset); |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | int fdt_next_property_offset(const void *fdt, int offset) |
| 352 | { |
Rob Herring | cd5f70d | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 353 | if ((offset = fdt_check_prop_offset_(fdt, offset)) < 0) |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 354 | return offset; |
| 355 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 356 | return nextprop_(fdt, offset); |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 357 | } |
| 358 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 359 | static const struct fdt_property *fdt_get_property_by_offset_(const void *fdt, |
| 360 | int offset, |
| 361 | int *lenp) |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 362 | { |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 363 | int err; |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 364 | const struct fdt_property *prop; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 365 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 366 | if (fdt_chk_basic() && (err = fdt_check_prop_offset_(fdt, offset)) < 0) { |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 367 | if (lenp) |
| 368 | *lenp = err; |
| 369 | return NULL; |
| 370 | } |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 371 | |
Rob Herring | cd5f70d | 2018-05-19 14:13:53 +0200 | [diff] [blame] | 372 | prop = fdt_offset_ptr_(fdt, offset); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 373 | |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 374 | if (lenp) |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 375 | *lenp = fdt32_ld(&prop->len); |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 376 | |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 377 | return prop; |
| 378 | } |
| 379 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 380 | const struct fdt_property *fdt_get_property_by_offset(const void *fdt, |
| 381 | int offset, |
| 382 | int *lenp) |
| 383 | { |
| 384 | /* Prior to version 16, properties may need realignment |
| 385 | * and this API does not work. fdt_getprop_*() will, however. */ |
| 386 | |
| 387 | if (fdt_chk_version() && fdt_version(fdt) < 0x10) { |
| 388 | if (lenp) |
| 389 | *lenp = -FDT_ERR_BADVERSION; |
| 390 | return NULL; |
| 391 | } |
| 392 | |
| 393 | return fdt_get_property_by_offset_(fdt, offset, lenp); |
| 394 | } |
| 395 | |
| 396 | static const struct fdt_property *fdt_get_property_namelen_(const void *fdt, |
| 397 | int offset, |
| 398 | const char *name, |
| 399 | int namelen, |
| 400 | int *lenp, |
| 401 | int *poffset) |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 402 | { |
| 403 | for (offset = fdt_first_property_offset(fdt, offset); |
| 404 | (offset >= 0); |
| 405 | (offset = fdt_next_property_offset(fdt, offset))) { |
| 406 | const struct fdt_property *prop; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 407 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 408 | prop = fdt_get_property_by_offset_(fdt, offset, lenp); |
| 409 | if (fdt_chk_extra() && !prop) { |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 410 | offset = -FDT_ERR_INTERNAL; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 411 | break; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 412 | } |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 413 | if (fdt_string_eq_(fdt, fdt32_ld(&prop->nameoff), |
| 414 | name, namelen)) { |
| 415 | if (poffset) |
| 416 | *poffset = offset; |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 417 | return prop; |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 418 | } |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 419 | } |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 420 | |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 421 | if (lenp) |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 422 | *lenp = offset; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 423 | return NULL; |
| 424 | } |
| 425 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 426 | |
| 427 | const struct fdt_property *fdt_get_property_namelen(const void *fdt, |
| 428 | int offset, |
| 429 | const char *name, |
| 430 | int namelen, int *lenp) |
| 431 | { |
| 432 | /* Prior to version 16, properties may need realignment |
| 433 | * and this API does not work. fdt_getprop_*() will, however. */ |
| 434 | if (fdt_chk_version() && fdt_version(fdt) < 0x10) { |
| 435 | if (lenp) |
| 436 | *lenp = -FDT_ERR_BADVERSION; |
| 437 | return NULL; |
| 438 | } |
| 439 | |
| 440 | return fdt_get_property_namelen_(fdt, offset, name, namelen, lenp, |
| 441 | NULL); |
| 442 | } |
| 443 | |
| 444 | |
David Gibson | c5b130e | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 445 | const struct fdt_property *fdt_get_property(const void *fdt, |
| 446 | int nodeoffset, |
| 447 | const char *name, int *lenp) |
| 448 | { |
| 449 | return fdt_get_property_namelen(fdt, nodeoffset, name, |
| 450 | strlen(name), lenp); |
| 451 | } |
| 452 | |
| 453 | const void *fdt_getprop_namelen(const void *fdt, int nodeoffset, |
| 454 | const char *name, int namelen, int *lenp) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 455 | { |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 456 | int poffset; |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 457 | const struct fdt_property *prop; |
| 458 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 459 | prop = fdt_get_property_namelen_(fdt, nodeoffset, name, namelen, lenp, |
| 460 | &poffset); |
Robert P. J. Day | 55019c5 | 2016-05-23 05:40:55 -0400 | [diff] [blame] | 461 | if (!prop) |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 462 | return NULL; |
| 463 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 464 | /* Handle realignment */ |
| 465 | if (fdt_chk_version() && fdt_version(fdt) < 0x10 && |
| 466 | (poffset + sizeof(*prop)) % 8 && fdt32_ld(&prop->len) >= 8) |
| 467 | return prop->data + 4; |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 468 | return prop->data; |
| 469 | } |
| 470 | |
| 471 | const void *fdt_getprop_by_offset(const void *fdt, int offset, |
| 472 | const char **namep, int *lenp) |
| 473 | { |
| 474 | const struct fdt_property *prop; |
| 475 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 476 | prop = fdt_get_property_by_offset_(fdt, offset, lenp); |
David Gibson | 7b64da1 | 2010-03-09 17:39:14 +1100 | [diff] [blame] | 477 | if (!prop) |
| 478 | return NULL; |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 479 | if (namep) { |
| 480 | const char *name; |
| 481 | int namelen; |
| 482 | |
| 483 | if (fdt_chk_extra()) { |
| 484 | name = fdt_get_string(fdt, fdt32_ld(&prop->nameoff), |
| 485 | &namelen); |
| 486 | if (!name) { |
| 487 | if (lenp) |
| 488 | *lenp = namelen; |
| 489 | return NULL; |
| 490 | } |
| 491 | *namep = name; |
| 492 | } else { |
| 493 | *namep = fdt_string(fdt, fdt32_ld(&prop->nameoff)); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | /* Handle realignment */ |
| 498 | if (fdt_chk_version() && fdt_version(fdt) < 0x10 && |
| 499 | (offset + sizeof(*prop)) % 8 && fdt32_ld(&prop->len) >= 8) |
| 500 | return prop->data + 4; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 501 | return prop->data; |
Gerald Van Baren | ca8d8a6 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 502 | } |
| 503 | |
David Gibson | c5b130e | 2008-08-06 14:50:49 +1000 | [diff] [blame] | 504 | const void *fdt_getprop(const void *fdt, int nodeoffset, |
| 505 | const char *name, int *lenp) |
| 506 | { |
| 507 | return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp); |
| 508 | } |
| 509 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 510 | uint32_t fdt_get_phandle(const void *fdt, int nodeoffset) |
| 511 | { |
Kim Phillips | 38c3815 | 2013-01-16 13:59:50 +0000 | [diff] [blame] | 512 | const fdt32_t *php; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 513 | int len; |
| 514 | |
David Gibson | 3e31de9 | 2009-11-26 15:37:13 +1100 | [diff] [blame] | 515 | /* FIXME: This is a bit sub-optimal, since we potentially scan |
| 516 | * over all the properties twice. */ |
| 517 | php = fdt_getprop(fdt, nodeoffset, "phandle", &len); |
| 518 | if (!php || (len != sizeof(*php))) { |
| 519 | php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len); |
| 520 | if (!php || (len != sizeof(*php))) |
| 521 | return 0; |
| 522 | } |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 523 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 524 | return fdt32_ld(php); |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 525 | } |
Gerald Van Baren | ca8d8a6 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 526 | |
David Gibson | d6656ea | 2008-08-20 16:55:14 +1000 | [diff] [blame] | 527 | const char *fdt_get_alias_namelen(const void *fdt, |
| 528 | const char *name, int namelen) |
| 529 | { |
| 530 | int aliasoffset; |
| 531 | |
| 532 | aliasoffset = fdt_path_offset(fdt, "/aliases"); |
| 533 | if (aliasoffset < 0) |
| 534 | return NULL; |
| 535 | |
| 536 | return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL); |
| 537 | } |
| 538 | |
| 539 | const char *fdt_get_alias(const void *fdt, const char *name) |
| 540 | { |
| 541 | return fdt_get_alias_namelen(fdt, name, strlen(name)); |
| 542 | } |
| 543 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 544 | 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] | 545 | { |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 546 | int pdepth = 0, p = 0; |
| 547 | int offset, depth, namelen; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 548 | const char *name; |
Gerald Van Baren | ca8d8a6 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 549 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 550 | FDT_RO_PROBE(fdt); |
Gerald Van Baren | ca8d8a6 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 551 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 552 | if (buflen < 2) |
| 553 | return -FDT_ERR_NOSPACE; |
Gerald Van Baren | ca8d8a6 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 554 | |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 555 | for (offset = 0, depth = 0; |
| 556 | (offset >= 0) && (offset <= nodeoffset); |
| 557 | offset = fdt_next_node(fdt, offset, &depth)) { |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 558 | while (pdepth > depth) { |
| 559 | do { |
| 560 | p--; |
| 561 | } while (buf[p-1] != '/'); |
| 562 | pdepth--; |
| 563 | } |
| 564 | |
David Gibson | 8d2810d | 2008-08-29 14:19:13 +1000 | [diff] [blame] | 565 | if (pdepth >= depth) { |
| 566 | name = fdt_get_name(fdt, offset, &namelen); |
| 567 | if (!name) |
| 568 | return namelen; |
| 569 | if ((p + namelen + 1) <= buflen) { |
| 570 | memcpy(buf + p, name, namelen); |
| 571 | p += namelen; |
| 572 | buf[p++] = '/'; |
| 573 | pdepth++; |
| 574 | } |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 575 | } |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 576 | |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 577 | if (offset == nodeoffset) { |
| 578 | if (pdepth < (depth + 1)) |
| 579 | return -FDT_ERR_NOSPACE; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 580 | |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 581 | if (p > 1) /* special case so that root path is "/", not "" */ |
| 582 | p--; |
| 583 | buf[p] = '\0'; |
David Gibson | 8d2810d | 2008-08-29 14:19:13 +1000 | [diff] [blame] | 584 | return 0; |
Gerald Van Baren | ca8d8a6 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 585 | } |
Gerald Van Baren | ca8d8a6 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 586 | } |
| 587 | |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 588 | if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) |
| 589 | return -FDT_ERR_BADOFFSET; |
| 590 | else if (offset == -FDT_ERR_BADOFFSET) |
| 591 | return -FDT_ERR_BADSTRUCTURE; |
Gerald Van Baren | ca8d8a6 | 2007-03-31 12:13:43 -0400 | [diff] [blame] | 592 | |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 593 | return offset; /* error from fdt_next_node() */ |
Gerald Van Baren | 9e61ed8 | 2007-03-31 12:00:56 -0400 | [diff] [blame] | 594 | } |
Gerald Van Baren | c077a13 | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 595 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 596 | int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset, |
| 597 | int supernodedepth, int *nodedepth) |
Gerald Van Baren | c077a13 | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 598 | { |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 599 | int offset, depth; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 600 | int supernodeoffset = -FDT_ERR_INTERNAL; |
Gerald Van Baren | c077a13 | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 601 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 602 | FDT_RO_PROBE(fdt); |
Gerald Van Baren | c077a13 | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 603 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 604 | if (supernodedepth < 0) |
| 605 | return -FDT_ERR_NOTFOUND; |
Gerald Van Baren | c077a13 | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 606 | |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 607 | for (offset = 0, depth = 0; |
| 608 | (offset >= 0) && (offset <= nodeoffset); |
| 609 | offset = fdt_next_node(fdt, offset, &depth)) { |
| 610 | if (depth == supernodedepth) |
| 611 | supernodeoffset = offset; |
Gerald Van Baren | c077a13 | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 612 | |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 613 | if (offset == nodeoffset) { |
| 614 | if (nodedepth) |
| 615 | *nodedepth = depth; |
Gerald Van Baren | c077a13 | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 616 | |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 617 | if (supernodedepth > depth) |
| 618 | return -FDT_ERR_NOTFOUND; |
| 619 | else |
| 620 | return supernodeoffset; |
Gerald Van Baren | c077a13 | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 621 | } |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 622 | } |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 623 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 624 | if (fdt_chk_extra()) { |
| 625 | if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0)) |
| 626 | return -FDT_ERR_BADOFFSET; |
| 627 | else if (offset == -FDT_ERR_BADOFFSET) |
| 628 | return -FDT_ERR_BADSTRUCTURE; |
| 629 | } |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 630 | |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 631 | return offset; /* error from fdt_next_node() */ |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | int fdt_node_depth(const void *fdt, int nodeoffset) |
| 635 | { |
| 636 | int nodedepth; |
| 637 | int err; |
| 638 | |
| 639 | err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth); |
| 640 | if (err) |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 641 | return (!fdt_chk_extra() || err < 0) ? err : -FDT_ERR_INTERNAL; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 642 | return nodedepth; |
| 643 | } |
| 644 | |
| 645 | int fdt_parent_offset(const void *fdt, int nodeoffset) |
| 646 | { |
| 647 | int nodedepth = fdt_node_depth(fdt, nodeoffset); |
| 648 | |
| 649 | if (nodedepth < 0) |
| 650 | return nodedepth; |
| 651 | return fdt_supernode_atdepth_offset(fdt, nodeoffset, |
| 652 | nodedepth - 1, NULL); |
| 653 | } |
| 654 | |
| 655 | int fdt_node_offset_by_prop_value(const void *fdt, int startoffset, |
| 656 | const char *propname, |
| 657 | const void *propval, int proplen) |
| 658 | { |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 659 | int offset; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 660 | const void *val; |
| 661 | int len; |
| 662 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 663 | FDT_RO_PROBE(fdt); |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 664 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 665 | /* FIXME: The algorithm here is pretty horrible: we scan each |
| 666 | * property of a node in fdt_getprop(), then if that didn't |
| 667 | * find what we want, we scan over them again making our way |
| 668 | * to the next node. Still it's the easiest to implement |
| 669 | * approach; performance can come later. */ |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 670 | for (offset = fdt_next_node(fdt, startoffset, NULL); |
| 671 | offset >= 0; |
| 672 | offset = fdt_next_node(fdt, offset, NULL)) { |
| 673 | val = fdt_getprop(fdt, offset, propname, &len); |
| 674 | if (val && (len == proplen) |
| 675 | && (memcmp(val, propval, len) == 0)) |
| 676 | return offset; |
| 677 | } |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 678 | |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 679 | return offset; /* error from fdt_next_node() */ |
Gerald Van Baren | c077a13 | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 680 | } |
| 681 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 682 | 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] | 683 | { |
David Gibson | 3e31de9 | 2009-11-26 15:37:13 +1100 | [diff] [blame] | 684 | int offset; |
| 685 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 686 | if ((phandle == 0) || (phandle == -1)) |
| 687 | return -FDT_ERR_BADPHANDLE; |
David Gibson | 3e31de9 | 2009-11-26 15:37:13 +1100 | [diff] [blame] | 688 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 689 | FDT_RO_PROBE(fdt); |
David Gibson | 3e31de9 | 2009-11-26 15:37:13 +1100 | [diff] [blame] | 690 | |
| 691 | /* FIXME: The algorithm here is pretty horrible: we |
| 692 | * potentially scan each property of a node in |
| 693 | * fdt_get_phandle(), then if that didn't find what |
| 694 | * we want, we scan over them again making our way to the next |
| 695 | * node. Still it's the easiest to implement approach; |
| 696 | * performance can come later. */ |
| 697 | for (offset = fdt_next_node(fdt, -1, NULL); |
| 698 | offset >= 0; |
| 699 | offset = fdt_next_node(fdt, offset, NULL)) { |
| 700 | if (fdt_get_phandle(fdt, offset) == phandle) |
| 701 | return offset; |
| 702 | } |
| 703 | |
| 704 | return offset; /* error from fdt_next_node() */ |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 705 | } |
Gerald Van Baren | c077a13 | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 706 | |
Simon Glass | 73b4756 | 2013-01-21 12:59:18 -0800 | [diff] [blame] | 707 | int fdt_stringlist_contains(const char *strlist, int listlen, const char *str) |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 708 | { |
| 709 | int len = strlen(str); |
David Gibson | b4141b8 | 2008-07-07 10:10:48 +1000 | [diff] [blame] | 710 | const char *p; |
Gerald Van Baren | c077a13 | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 711 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 712 | while (listlen >= len) { |
| 713 | if (memcmp(str, strlist, len+1) == 0) |
| 714 | return 1; |
| 715 | p = memchr(strlist, '\0', listlen); |
| 716 | if (!p) |
| 717 | return 0; /* malformed strlist.. */ |
| 718 | listlen -= (p-strlist) + 1; |
| 719 | strlist = p + 1; |
Gerald Van Baren | c077a13 | 2007-04-14 22:46:41 -0400 | [diff] [blame] | 720 | } |
| 721 | return 0; |
| 722 | } |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 723 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 724 | int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property) |
Thierry Reding | 92ba09e | 2014-08-26 17:33:50 +0200 | [diff] [blame] | 725 | { |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 726 | const char *list, *end; |
| 727 | int length, count = 0; |
Thierry Reding | 92ba09e | 2014-08-26 17:33:50 +0200 | [diff] [blame] | 728 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 729 | list = fdt_getprop(fdt, nodeoffset, property, &length); |
Thierry Reding | 92ba09e | 2014-08-26 17:33:50 +0200 | [diff] [blame] | 730 | if (!list) |
Masahiro Yamada | 0a37826 | 2016-10-17 20:22:33 +0900 | [diff] [blame] | 731 | return length; |
Thierry Reding | 92ba09e | 2014-08-26 17:33:50 +0200 | [diff] [blame] | 732 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 733 | end = list + length; |
| 734 | |
| 735 | while (list < end) { |
| 736 | length = strnlen(list, end - list) + 1; |
Thierry Reding | 92ba09e | 2014-08-26 17:33:50 +0200 | [diff] [blame] | 737 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 738 | /* Abort if the last string isn't properly NUL-terminated. */ |
| 739 | if (list + length > end) |
| 740 | return -FDT_ERR_BADVALUE; |
| 741 | |
| 742 | list += length; |
Thierry Reding | 92ba09e | 2014-08-26 17:33:50 +0200 | [diff] [blame] | 743 | count++; |
| 744 | } |
| 745 | |
| 746 | return count; |
| 747 | } |
| 748 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 749 | int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property, |
| 750 | const char *string) |
Thierry Reding | a24f1ac | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 751 | { |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 752 | int length, len, idx = 0; |
Thierry Reding | a24f1ac | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 753 | const char *list, *end; |
Thierry Reding | a24f1ac | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 754 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 755 | list = fdt_getprop(fdt, nodeoffset, property, &length); |
Thierry Reding | a24f1ac | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 756 | if (!list) |
Masahiro Yamada | 48fd0f1 | 2016-10-17 20:22:34 +0900 | [diff] [blame] | 757 | return length; |
Thierry Reding | a24f1ac | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 758 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 759 | len = strlen(string) + 1; |
| 760 | end = list + length; |
Thierry Reding | a24f1ac | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 761 | |
| 762 | while (list < end) { |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 763 | length = strnlen(list, end - list) + 1; |
Thierry Reding | a24f1ac | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 764 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 765 | /* Abort if the last string isn't properly NUL-terminated. */ |
| 766 | if (list + length > end) |
| 767 | return -FDT_ERR_BADVALUE; |
Thierry Reding | a24f1ac | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 768 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 769 | if (length == len && memcmp(list, string, length) == 0) |
| 770 | return idx; |
| 771 | |
| 772 | list += length; |
| 773 | idx++; |
Thierry Reding | a24f1ac | 2014-08-26 17:33:51 +0200 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | return -FDT_ERR_NOTFOUND; |
| 777 | } |
| 778 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 779 | const char *fdt_stringlist_get(const void *fdt, int nodeoffset, |
| 780 | const char *property, int idx, |
| 781 | int *lenp) |
Thierry Reding | 5504b80 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 782 | { |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 783 | const char *list, *end; |
| 784 | int length; |
| 785 | |
| 786 | list = fdt_getprop(fdt, nodeoffset, property, &length); |
| 787 | if (!list) { |
| 788 | if (lenp) |
| 789 | *lenp = length; |
Thierry Reding | 5504b80 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 790 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 791 | return NULL; |
| 792 | } |
Thierry Reding | 5504b80 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 793 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 794 | end = list + length; |
Thierry Reding | 5504b80 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 795 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 796 | while (list < end) { |
| 797 | length = strnlen(list, end - list) + 1; |
| 798 | |
| 799 | /* Abort if the last string isn't properly NUL-terminated. */ |
| 800 | if (list + length > end) { |
| 801 | if (lenp) |
| 802 | *lenp = -FDT_ERR_BADVALUE; |
| 803 | |
| 804 | return NULL; |
| 805 | } |
| 806 | |
| 807 | if (idx == 0) { |
| 808 | if (lenp) |
| 809 | *lenp = length - 1; |
| 810 | |
| 811 | return list; |
Thierry Reding | 5504b80 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 812 | } |
| 813 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 814 | list += length; |
| 815 | idx--; |
Thierry Reding | 5504b80 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 816 | } |
| 817 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 818 | if (lenp) |
| 819 | *lenp = -FDT_ERR_NOTFOUND; |
Thierry Reding | 5504b80 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 820 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 821 | return NULL; |
Thierry Reding | 5504b80 | 2014-08-26 17:33:52 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 824 | int fdt_node_check_compatible(const void *fdt, int nodeoffset, |
| 825 | const char *compatible) |
| 826 | { |
| 827 | const void *prop; |
| 828 | int len; |
| 829 | |
| 830 | prop = fdt_getprop(fdt, nodeoffset, "compatible", &len); |
| 831 | if (!prop) |
| 832 | return len; |
Simon Glass | dfdcd6f | 2016-10-02 17:59:27 -0600 | [diff] [blame] | 833 | |
| 834 | return !fdt_stringlist_contains(prop, len, compatible); |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | int fdt_node_offset_by_compatible(const void *fdt, int startoffset, |
| 838 | const char *compatible) |
| 839 | { |
David Gibson | e8f3f7f | 2008-02-18 18:09:04 +1100 | [diff] [blame] | 840 | int offset, err; |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 841 | |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 842 | FDT_RO_PROBE(fdt); |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 843 | |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 844 | /* FIXME: The algorithm here is pretty horrible: we scan each |
| 845 | * property of a node in fdt_node_check_compatible(), then if |
| 846 | * that didn't find what we want, we scan over them again |
| 847 | * making our way to the next node. Still it's the easiest to |
| 848 | * implement approach; performance can come later. */ |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 849 | for (offset = fdt_next_node(fdt, startoffset, NULL); |
| 850 | offset >= 0; |
| 851 | offset = fdt_next_node(fdt, offset, NULL)) { |
| 852 | err = fdt_node_check_compatible(fdt, offset, compatible); |
| 853 | if ((err < 0) && (err != -FDT_ERR_NOTFOUND)) |
| 854 | return err; |
| 855 | else if (err == 0) |
| 856 | return offset; |
| 857 | } |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 858 | |
David Gibson | d9d793c | 2008-02-12 11:58:31 +1100 | [diff] [blame] | 859 | return offset; /* error from fdt_next_node() */ |
Kumar Gala | c8ab705 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 860 | } |
Simon Glass | a5e1a58 | 2019-10-27 09:47:42 -0600 | [diff] [blame] | 861 | |
| 862 | #if !defined(CHECK_LEVEL) || CHECK_LEVEL > 0 |
| 863 | int fdt_check_full(const void *fdt, size_t bufsize) |
| 864 | { |
| 865 | int err; |
| 866 | int num_memrsv; |
| 867 | int offset, nextoffset = 0; |
| 868 | uint32_t tag; |
| 869 | unsigned depth = 0; |
| 870 | const void *prop; |
| 871 | const char *propname; |
| 872 | |
| 873 | if (bufsize < FDT_V1_SIZE) |
| 874 | return -FDT_ERR_TRUNCATED; |
| 875 | err = fdt_check_header(fdt); |
| 876 | if (err != 0) |
| 877 | return err; |
| 878 | if (bufsize < fdt_totalsize(fdt)) |
| 879 | return -FDT_ERR_TRUNCATED; |
| 880 | |
| 881 | num_memrsv = fdt_num_mem_rsv(fdt); |
| 882 | if (num_memrsv < 0) |
| 883 | return num_memrsv; |
| 884 | |
| 885 | while (1) { |
| 886 | offset = nextoffset; |
| 887 | tag = fdt_next_tag(fdt, offset, &nextoffset); |
| 888 | |
| 889 | if (nextoffset < 0) |
| 890 | return nextoffset; |
| 891 | |
| 892 | switch (tag) { |
| 893 | case FDT_NOP: |
| 894 | break; |
| 895 | |
| 896 | case FDT_END: |
| 897 | if (depth != 0) |
| 898 | return -FDT_ERR_BADSTRUCTURE; |
| 899 | return 0; |
| 900 | |
| 901 | case FDT_BEGIN_NODE: |
| 902 | depth++; |
| 903 | if (depth > INT_MAX) |
| 904 | return -FDT_ERR_BADSTRUCTURE; |
| 905 | break; |
| 906 | |
| 907 | case FDT_END_NODE: |
| 908 | if (depth == 0) |
| 909 | return -FDT_ERR_BADSTRUCTURE; |
| 910 | depth--; |
| 911 | break; |
| 912 | |
| 913 | case FDT_PROP: |
| 914 | prop = fdt_getprop_by_offset(fdt, offset, &propname, |
| 915 | &err); |
| 916 | if (!prop) |
| 917 | return err; |
| 918 | break; |
| 919 | |
| 920 | default: |
| 921 | return -FDT_ERR_INTERNAL; |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | #endif |