Simon Glass | 1ccaa05 | 2017-05-18 20:08:54 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Originally from Linux v4.9 |
| 3 | * Paul Mackerras August 1996. |
| 4 | * Copyright (C) 1996-2005 Paul Mackerras. |
| 5 | * |
| 6 | * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. |
| 7 | * {engebret|bergner}@us.ibm.com |
| 8 | * |
| 9 | * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net |
| 10 | * |
| 11 | * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and |
| 12 | * Grant Likely. |
| 13 | * |
| 14 | * Modified for U-Boot |
| 15 | * Copyright (c) 2017 Google, Inc |
| 16 | * |
| 17 | * This file follows drivers/of/base.c with functions in the same order as the |
| 18 | * Linux version. |
| 19 | * |
| 20 | * SPDX-License-Identifier: GPL-2.0+ |
| 21 | */ |
| 22 | |
| 23 | #include <common.h> |
| 24 | #include <libfdt.h> |
| 25 | #include <dm/of_access.h> |
| 26 | #include <linux/ctype.h> |
| 27 | #include <linux/err.h> |
| 28 | #include <linux/ioport.h> |
| 29 | |
| 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
| 32 | /* list of struct alias_prop aliases */ |
| 33 | LIST_HEAD(aliases_lookup); |
| 34 | |
| 35 | /* "/aliaes" node */ |
| 36 | static struct device_node *of_aliases; |
| 37 | |
| 38 | /* "/chosen" node */ |
| 39 | static struct device_node *of_chosen; |
| 40 | |
| 41 | /* node pointed to by the stdout-path alias */ |
| 42 | static struct device_node *of_stdout; |
| 43 | |
| 44 | /* pointer to options given after the alias (separated by :) or NULL if none */ |
| 45 | static const char *of_stdout_options; |
| 46 | |
| 47 | /** |
| 48 | * struct alias_prop - Alias property in 'aliases' node |
| 49 | * |
| 50 | * The structure represents one alias property of 'aliases' node as |
| 51 | * an entry in aliases_lookup list. |
| 52 | * |
| 53 | * @link: List node to link the structure in aliases_lookup list |
| 54 | * @alias: Alias property name |
| 55 | * @np: Pointer to device_node that the alias stands for |
| 56 | * @id: Index value from end of alias name |
| 57 | * @stem: Alias string without the index |
| 58 | */ |
| 59 | struct alias_prop { |
| 60 | struct list_head link; |
| 61 | const char *alias; |
| 62 | struct device_node *np; |
| 63 | int id; |
| 64 | char stem[0]; |
| 65 | }; |
| 66 | |
| 67 | int of_n_addr_cells(const struct device_node *np) |
| 68 | { |
| 69 | const __be32 *ip; |
| 70 | |
| 71 | do { |
| 72 | if (np->parent) |
| 73 | np = np->parent; |
| 74 | ip = of_get_property(np, "#address-cells", NULL); |
| 75 | if (ip) |
| 76 | return be32_to_cpup(ip); |
| 77 | } while (np->parent); |
| 78 | |
| 79 | /* No #address-cells property for the root node */ |
| 80 | return OF_ROOT_NODE_ADDR_CELLS_DEFAULT; |
| 81 | } |
| 82 | |
| 83 | int of_n_size_cells(const struct device_node *np) |
| 84 | { |
| 85 | const __be32 *ip; |
| 86 | |
| 87 | do { |
| 88 | if (np->parent) |
| 89 | np = np->parent; |
| 90 | ip = of_get_property(np, "#size-cells", NULL); |
| 91 | if (ip) |
| 92 | return be32_to_cpup(ip); |
| 93 | } while (np->parent); |
| 94 | |
| 95 | /* No #size-cells property for the root node */ |
| 96 | return OF_ROOT_NODE_SIZE_CELLS_DEFAULT; |
| 97 | } |
| 98 | |
| 99 | struct property *of_find_property(const struct device_node *np, |
| 100 | const char *name, int *lenp) |
| 101 | { |
| 102 | struct property *pp; |
| 103 | |
| 104 | if (!np) |
| 105 | return NULL; |
| 106 | |
| 107 | for (pp = np->properties; pp; pp = pp->next) { |
| 108 | if (strcmp(pp->name, name) == 0) { |
| 109 | if (lenp) |
| 110 | *lenp = pp->length; |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | if (!pp && lenp) |
| 115 | *lenp = -FDT_ERR_NOTFOUND; |
| 116 | |
| 117 | return pp; |
| 118 | } |
| 119 | |
| 120 | struct device_node *of_find_all_nodes(struct device_node *prev) |
| 121 | { |
| 122 | struct device_node *np; |
| 123 | |
| 124 | if (!prev) { |
| 125 | np = gd->of_root; |
| 126 | } else if (prev->child) { |
| 127 | np = prev->child; |
| 128 | } else { |
| 129 | /* |
| 130 | * Walk back up looking for a sibling, or the end of the |
| 131 | * structure |
| 132 | */ |
| 133 | np = prev; |
| 134 | while (np->parent && !np->sibling) |
| 135 | np = np->parent; |
| 136 | np = np->sibling; /* Might be null at the end of the tree */ |
| 137 | } |
| 138 | |
| 139 | return np; |
| 140 | } |
| 141 | |
| 142 | const void *of_get_property(const struct device_node *np, const char *name, |
| 143 | int *lenp) |
| 144 | { |
| 145 | struct property *pp = of_find_property(np, name, lenp); |
| 146 | |
| 147 | return pp ? pp->value : NULL; |
| 148 | } |
| 149 | |
| 150 | static const char *of_prop_next_string(struct property *prop, const char *cur) |
| 151 | { |
| 152 | const void *curv = cur; |
| 153 | |
| 154 | if (!prop) |
| 155 | return NULL; |
| 156 | |
| 157 | if (!cur) |
| 158 | return prop->value; |
| 159 | |
| 160 | curv += strlen(cur) + 1; |
| 161 | if (curv >= prop->value + prop->length) |
| 162 | return NULL; |
| 163 | |
| 164 | return curv; |
| 165 | } |
| 166 | |
| 167 | int of_device_is_compatible(const struct device_node *device, |
| 168 | const char *compat, const char *type, |
| 169 | const char *name) |
| 170 | { |
| 171 | struct property *prop; |
| 172 | const char *cp; |
| 173 | int index = 0, score = 0; |
| 174 | |
| 175 | /* Compatible match has highest priority */ |
| 176 | if (compat && compat[0]) { |
| 177 | prop = of_find_property(device, "compatible", NULL); |
| 178 | for (cp = of_prop_next_string(prop, NULL); cp; |
| 179 | cp = of_prop_next_string(prop, cp), index++) { |
| 180 | if (of_compat_cmp(cp, compat, strlen(compat)) == 0) { |
| 181 | score = INT_MAX/2 - (index << 2); |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | if (!score) |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | /* Matching type is better than matching name */ |
| 190 | if (type && type[0]) { |
| 191 | if (!device->type || of_node_cmp(type, device->type)) |
| 192 | return 0; |
| 193 | score += 2; |
| 194 | } |
| 195 | |
| 196 | /* Matching name is a bit better than not */ |
| 197 | if (name && name[0]) { |
| 198 | if (!device->name || of_node_cmp(name, device->name)) |
| 199 | return 0; |
| 200 | score++; |
| 201 | } |
| 202 | |
| 203 | return score; |
| 204 | } |
| 205 | |
| 206 | bool of_device_is_available(const struct device_node *device) |
| 207 | { |
| 208 | const char *status; |
| 209 | int statlen; |
| 210 | |
| 211 | if (!device) |
| 212 | return false; |
| 213 | |
| 214 | status = of_get_property(device, "status", &statlen); |
| 215 | if (status == NULL) |
| 216 | return true; |
| 217 | |
| 218 | if (statlen > 0) { |
| 219 | if (!strcmp(status, "okay")) |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | struct device_node *of_get_parent(const struct device_node *node) |
| 227 | { |
| 228 | const struct device_node *np; |
| 229 | |
| 230 | if (!node) |
| 231 | return NULL; |
| 232 | |
| 233 | np = of_node_get(node->parent); |
| 234 | |
| 235 | return (struct device_node *)np; |
| 236 | } |
| 237 | |
| 238 | static struct device_node *__of_get_next_child(const struct device_node *node, |
| 239 | struct device_node *prev) |
| 240 | { |
| 241 | struct device_node *next; |
| 242 | |
| 243 | if (!node) |
| 244 | return NULL; |
| 245 | |
| 246 | next = prev ? prev->sibling : node->child; |
Simon Glass | 3694ac5 | 2017-06-07 10:28:45 -0600 | [diff] [blame] | 247 | /* |
| 248 | * coverity[dead_error_line : FALSE] |
| 249 | * Dead code here since our current implementation of of_node_get() |
| 250 | * always returns NULL (Coverity CID 163245). But we leave it as is |
| 251 | * since we may want to implement get/put later. |
| 252 | */ |
Simon Glass | 1ccaa05 | 2017-05-18 20:08:54 -0600 | [diff] [blame] | 253 | for (; next; next = next->sibling) |
| 254 | if (of_node_get(next)) |
| 255 | break; |
| 256 | of_node_put(prev); |
| 257 | return next; |
| 258 | } |
| 259 | |
| 260 | #define __for_each_child_of_node(parent, child) \ |
| 261 | for (child = __of_get_next_child(parent, NULL); child != NULL; \ |
| 262 | child = __of_get_next_child(parent, child)) |
| 263 | |
| 264 | static struct device_node *__of_find_node_by_path(struct device_node *parent, |
| 265 | const char *path) |
| 266 | { |
| 267 | struct device_node *child; |
| 268 | int len; |
| 269 | |
| 270 | len = strcspn(path, "/:"); |
| 271 | if (!len) |
| 272 | return NULL; |
| 273 | |
| 274 | __for_each_child_of_node(parent, child) { |
| 275 | const char *name = strrchr(child->full_name, '/'); |
| 276 | |
| 277 | name++; |
| 278 | if (strncmp(path, name, len) == 0 && (strlen(name) == len)) |
| 279 | return child; |
| 280 | } |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | #define for_each_property_of_node(dn, pp) \ |
| 285 | for (pp = dn->properties; pp != NULL; pp = pp->next) |
| 286 | |
| 287 | struct device_node *of_find_node_opts_by_path(const char *path, |
| 288 | const char **opts) |
| 289 | { |
| 290 | struct device_node *np = NULL; |
| 291 | struct property *pp; |
| 292 | const char *separator = strchr(path, ':'); |
| 293 | |
| 294 | if (opts) |
| 295 | *opts = separator ? separator + 1 : NULL; |
| 296 | |
| 297 | if (strcmp(path, "/") == 0) |
| 298 | return of_node_get(gd->of_root); |
| 299 | |
| 300 | /* The path could begin with an alias */ |
| 301 | if (*path != '/') { |
| 302 | int len; |
| 303 | const char *p = separator; |
| 304 | |
| 305 | if (!p) |
| 306 | p = strchrnul(path, '/'); |
| 307 | len = p - path; |
| 308 | |
| 309 | /* of_aliases must not be NULL */ |
| 310 | if (!of_aliases) |
| 311 | return NULL; |
| 312 | |
| 313 | for_each_property_of_node(of_aliases, pp) { |
| 314 | if (strlen(pp->name) == len && !strncmp(pp->name, path, |
| 315 | len)) { |
| 316 | np = of_find_node_by_path(pp->value); |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | if (!np) |
| 321 | return NULL; |
| 322 | path = p; |
| 323 | } |
| 324 | |
| 325 | /* Step down the tree matching path components */ |
| 326 | if (!np) |
| 327 | np = of_node_get(gd->of_root); |
| 328 | while (np && *path == '/') { |
| 329 | struct device_node *tmp = np; |
| 330 | |
| 331 | path++; /* Increment past '/' delimiter */ |
| 332 | np = __of_find_node_by_path(np, path); |
| 333 | of_node_put(tmp); |
| 334 | path = strchrnul(path, '/'); |
| 335 | if (separator && separator < path) |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | return np; |
| 340 | } |
| 341 | |
| 342 | struct device_node *of_find_compatible_node(struct device_node *from, |
| 343 | const char *type, const char *compatible) |
| 344 | { |
| 345 | struct device_node *np; |
| 346 | |
| 347 | for_each_of_allnodes_from(from, np) |
| 348 | if (of_device_is_compatible(np, compatible, type, NULL) && |
| 349 | of_node_get(np)) |
| 350 | break; |
| 351 | of_node_put(from); |
| 352 | |
| 353 | return np; |
| 354 | } |
| 355 | |
| 356 | struct device_node *of_find_node_by_phandle(phandle handle) |
| 357 | { |
| 358 | struct device_node *np; |
| 359 | |
| 360 | if (!handle) |
| 361 | return NULL; |
| 362 | |
| 363 | for_each_of_allnodes(np) |
| 364 | if (np->phandle == handle) |
| 365 | break; |
| 366 | (void)of_node_get(np); |
| 367 | |
| 368 | return np; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * of_find_property_value_of_size() - find property of given size |
| 373 | * |
| 374 | * Search for a property in a device node and validate the requested size. |
| 375 | * |
| 376 | * @np: device node from which the property value is to be read. |
| 377 | * @propname: name of the property to be searched. |
| 378 | * @len: requested length of property value |
| 379 | * |
| 380 | * @return the property value on success, -EINVAL if the property does not |
| 381 | * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the |
| 382 | * property data isn't large enough. |
| 383 | */ |
| 384 | static void *of_find_property_value_of_size(const struct device_node *np, |
| 385 | const char *propname, u32 len) |
| 386 | { |
| 387 | struct property *prop = of_find_property(np, propname, NULL); |
| 388 | |
| 389 | if (!prop) |
| 390 | return ERR_PTR(-EINVAL); |
| 391 | if (!prop->value) |
| 392 | return ERR_PTR(-ENODATA); |
| 393 | if (len > prop->length) |
| 394 | return ERR_PTR(-EOVERFLOW); |
| 395 | |
| 396 | return prop->value; |
| 397 | } |
| 398 | |
| 399 | int of_read_u32(const struct device_node *np, const char *propname, u32 *outp) |
| 400 | { |
| 401 | const __be32 *val; |
| 402 | |
| 403 | debug("%s: %s: ", __func__, propname); |
| 404 | if (!np) |
| 405 | return -EINVAL; |
| 406 | val = of_find_property_value_of_size(np, propname, sizeof(*outp)); |
| 407 | if (IS_ERR(val)) { |
| 408 | debug("(not found)\n"); |
| 409 | return PTR_ERR(val); |
| 410 | } |
| 411 | |
| 412 | *outp = be32_to_cpup(val); |
| 413 | debug("%#x (%d)\n", *outp, *outp); |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | int of_read_u32_array(const struct device_node *np, const char *propname, |
| 419 | u32 *out_values, size_t sz) |
| 420 | { |
| 421 | const __be32 *val; |
| 422 | |
| 423 | debug("%s: %s: ", __func__, propname); |
| 424 | val = of_find_property_value_of_size(np, propname, |
| 425 | sz * sizeof(*out_values)); |
| 426 | |
| 427 | if (IS_ERR(val)) |
| 428 | return PTR_ERR(val); |
| 429 | |
| 430 | debug("size %zd\n", sz); |
| 431 | while (sz--) |
| 432 | *out_values++ = be32_to_cpup(val++); |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | int of_property_match_string(const struct device_node *np, const char *propname, |
| 438 | const char *string) |
| 439 | { |
| 440 | const struct property *prop = of_find_property(np, propname, NULL); |
| 441 | size_t l; |
| 442 | int i; |
| 443 | const char *p, *end; |
| 444 | |
| 445 | if (!prop) |
| 446 | return -EINVAL; |
| 447 | if (!prop->value) |
| 448 | return -ENODATA; |
| 449 | |
| 450 | p = prop->value; |
| 451 | end = p + prop->length; |
| 452 | |
| 453 | for (i = 0; p < end; i++, p += l) { |
| 454 | l = strnlen(p, end - p) + 1; |
| 455 | if (p + l > end) |
| 456 | return -EILSEQ; |
| 457 | debug("comparing %s with %s\n", string, p); |
| 458 | if (strcmp(string, p) == 0) |
| 459 | return i; /* Found it; return index */ |
| 460 | } |
| 461 | return -ENODATA; |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * of_property_read_string_helper() - Utility helper for parsing string properties |
| 466 | * @np: device node from which the property value is to be read. |
| 467 | * @propname: name of the property to be searched. |
| 468 | * @out_strs: output array of string pointers. |
| 469 | * @sz: number of array elements to read. |
| 470 | * @skip: Number of strings to skip over at beginning of list. |
| 471 | * |
| 472 | * Don't call this function directly. It is a utility helper for the |
| 473 | * of_property_read_string*() family of functions. |
| 474 | */ |
| 475 | int of_property_read_string_helper(const struct device_node *np, |
| 476 | const char *propname, const char **out_strs, |
| 477 | size_t sz, int skip) |
| 478 | { |
| 479 | const struct property *prop = of_find_property(np, propname, NULL); |
| 480 | int l = 0, i = 0; |
| 481 | const char *p, *end; |
| 482 | |
| 483 | if (!prop) |
| 484 | return -EINVAL; |
| 485 | if (!prop->value) |
| 486 | return -ENODATA; |
| 487 | p = prop->value; |
| 488 | end = p + prop->length; |
| 489 | |
| 490 | for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l) { |
| 491 | l = strnlen(p, end - p) + 1; |
| 492 | if (p + l > end) |
| 493 | return -EILSEQ; |
| 494 | if (out_strs && i >= skip) |
| 495 | *out_strs++ = p; |
| 496 | } |
| 497 | i -= skip; |
| 498 | return i <= 0 ? -ENODATA : i; |
| 499 | } |
| 500 | |
| 501 | static int __of_parse_phandle_with_args(const struct device_node *np, |
| 502 | const char *list_name, |
| 503 | const char *cells_name, |
| 504 | int cell_count, int index, |
| 505 | struct of_phandle_args *out_args) |
| 506 | { |
| 507 | const __be32 *list, *list_end; |
| 508 | int rc = 0, cur_index = 0; |
| 509 | uint32_t count = 0; |
| 510 | struct device_node *node = NULL; |
| 511 | phandle phandle; |
| 512 | int size; |
| 513 | |
| 514 | /* Retrieve the phandle list property */ |
| 515 | list = of_get_property(np, list_name, &size); |
| 516 | if (!list) |
| 517 | return -ENOENT; |
| 518 | list_end = list + size / sizeof(*list); |
| 519 | |
| 520 | /* Loop over the phandles until all the requested entry is found */ |
| 521 | while (list < list_end) { |
| 522 | rc = -EINVAL; |
| 523 | count = 0; |
| 524 | |
| 525 | /* |
| 526 | * If phandle is 0, then it is an empty entry with no |
| 527 | * arguments. Skip forward to the next entry. |
| 528 | */ |
| 529 | phandle = be32_to_cpup(list++); |
| 530 | if (phandle) { |
| 531 | /* |
| 532 | * Find the provider node and parse the #*-cells |
| 533 | * property to determine the argument length. |
| 534 | * |
| 535 | * This is not needed if the cell count is hard-coded |
| 536 | * (i.e. cells_name not set, but cell_count is set), |
| 537 | * except when we're going to return the found node |
| 538 | * below. |
| 539 | */ |
| 540 | if (cells_name || cur_index == index) { |
| 541 | node = of_find_node_by_phandle(phandle); |
| 542 | if (!node) { |
| 543 | debug("%s: could not find phandle\n", |
| 544 | np->full_name); |
| 545 | goto err; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | if (cells_name) { |
| 550 | if (of_read_u32(node, cells_name, &count)) { |
| 551 | debug("%s: could not get %s for %s\n", |
| 552 | np->full_name, cells_name, |
| 553 | node->full_name); |
| 554 | goto err; |
| 555 | } |
| 556 | } else { |
| 557 | count = cell_count; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Make sure that the arguments actually fit in the |
| 562 | * remaining property data length |
| 563 | */ |
| 564 | if (list + count > list_end) { |
| 565 | debug("%s: arguments longer than property\n", |
| 566 | np->full_name); |
| 567 | goto err; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * All of the error cases above bail out of the loop, so at |
| 573 | * this point, the parsing is successful. If the requested |
| 574 | * index matches, then fill the out_args structure and return, |
| 575 | * or return -ENOENT for an empty entry. |
| 576 | */ |
| 577 | rc = -ENOENT; |
| 578 | if (cur_index == index) { |
| 579 | if (!phandle) |
| 580 | goto err; |
| 581 | |
| 582 | if (out_args) { |
| 583 | int i; |
| 584 | if (WARN_ON(count > OF_MAX_PHANDLE_ARGS)) |
| 585 | count = OF_MAX_PHANDLE_ARGS; |
| 586 | out_args->np = node; |
| 587 | out_args->args_count = count; |
| 588 | for (i = 0; i < count; i++) |
| 589 | out_args->args[i] = |
| 590 | be32_to_cpup(list++); |
| 591 | } else { |
| 592 | of_node_put(node); |
| 593 | } |
| 594 | |
| 595 | /* Found it! return success */ |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | of_node_put(node); |
| 600 | node = NULL; |
| 601 | list += count; |
| 602 | cur_index++; |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * Unlock node before returning result; will be one of: |
| 607 | * -ENOENT : index is for empty phandle |
| 608 | * -EINVAL : parsing error on data |
| 609 | * [1..n] : Number of phandle (count mode; when index = -1) |
| 610 | */ |
| 611 | rc = index < 0 ? cur_index : -ENOENT; |
| 612 | err: |
| 613 | if (node) |
| 614 | of_node_put(node); |
| 615 | return rc; |
| 616 | } |
| 617 | |
| 618 | struct device_node *of_parse_phandle(const struct device_node *np, |
| 619 | const char *phandle_name, int index) |
| 620 | { |
| 621 | struct of_phandle_args args; |
| 622 | |
| 623 | if (index < 0) |
| 624 | return NULL; |
| 625 | |
| 626 | if (__of_parse_phandle_with_args(np, phandle_name, NULL, 0, index, |
| 627 | &args)) |
| 628 | return NULL; |
| 629 | |
| 630 | return args.np; |
| 631 | } |
| 632 | |
| 633 | int of_parse_phandle_with_args(const struct device_node *np, |
| 634 | const char *list_name, const char *cells_name, |
| 635 | int index, struct of_phandle_args *out_args) |
| 636 | { |
| 637 | if (index < 0) |
| 638 | return -EINVAL; |
| 639 | |
| 640 | return __of_parse_phandle_with_args(np, list_name, cells_name, 0, |
| 641 | index, out_args); |
| 642 | } |
| 643 | |
| 644 | static void of_alias_add(struct alias_prop *ap, struct device_node *np, |
| 645 | int id, const char *stem, int stem_len) |
| 646 | { |
| 647 | ap->np = np; |
| 648 | ap->id = id; |
| 649 | strncpy(ap->stem, stem, stem_len); |
| 650 | ap->stem[stem_len] = 0; |
| 651 | list_add_tail(&ap->link, &aliases_lookup); |
| 652 | debug("adding DT alias:%s: stem=%s id=%i node=%s\n", |
| 653 | ap->alias, ap->stem, ap->id, of_node_full_name(np)); |
| 654 | } |
| 655 | |
| 656 | int of_alias_scan(void) |
| 657 | { |
| 658 | struct property *pp; |
| 659 | |
| 660 | of_aliases = of_find_node_by_path("/aliases"); |
| 661 | of_chosen = of_find_node_by_path("/chosen"); |
| 662 | if (of_chosen == NULL) |
| 663 | of_chosen = of_find_node_by_path("/chosen@0"); |
| 664 | |
| 665 | if (of_chosen) { |
| 666 | const char *name; |
| 667 | |
| 668 | name = of_get_property(of_chosen, "stdout-path", NULL); |
| 669 | if (name) |
| 670 | of_stdout = of_find_node_opts_by_path(name, |
| 671 | &of_stdout_options); |
| 672 | } |
| 673 | |
| 674 | if (!of_aliases) |
| 675 | return 0; |
| 676 | |
| 677 | for_each_property_of_node(of_aliases, pp) { |
| 678 | const char *start = pp->name; |
| 679 | const char *end = start + strlen(start); |
| 680 | struct device_node *np; |
| 681 | struct alias_prop *ap; |
| 682 | ulong id; |
| 683 | int len; |
| 684 | |
| 685 | /* Skip those we do not want to proceed */ |
| 686 | if (!strcmp(pp->name, "name") || |
| 687 | !strcmp(pp->name, "phandle") || |
| 688 | !strcmp(pp->name, "linux,phandle")) |
| 689 | continue; |
| 690 | |
| 691 | np = of_find_node_by_path(pp->value); |
| 692 | if (!np) |
| 693 | continue; |
| 694 | |
| 695 | /* |
| 696 | * walk the alias backwards to extract the id and work out |
| 697 | * the 'stem' string |
| 698 | */ |
| 699 | while (isdigit(*(end-1)) && end > start) |
| 700 | end--; |
| 701 | len = end - start; |
| 702 | |
| 703 | if (strict_strtoul(end, 10, &id) < 0) |
| 704 | continue; |
| 705 | |
| 706 | /* Allocate an alias_prop with enough space for the stem */ |
| 707 | ap = malloc(sizeof(*ap) + len + 1); |
| 708 | if (!ap) |
| 709 | return -ENOMEM; |
| 710 | memset(ap, 0, sizeof(*ap) + len + 1); |
| 711 | ap->alias = start; |
| 712 | of_alias_add(ap, np, id, start, len); |
| 713 | } |
| 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | int of_alias_get_id(const struct device_node *np, const char *stem) |
| 719 | { |
| 720 | struct alias_prop *app; |
| 721 | int id = -ENODEV; |
| 722 | |
| 723 | mutex_lock(&of_mutex); |
| 724 | list_for_each_entry(app, &aliases_lookup, link) { |
| 725 | if (strcmp(app->stem, stem) != 0) |
| 726 | continue; |
| 727 | |
| 728 | if (np == app->np) { |
| 729 | id = app->id; |
| 730 | break; |
| 731 | } |
| 732 | } |
| 733 | mutex_unlock(&of_mutex); |
| 734 | |
| 735 | return id; |
| 736 | } |
| 737 | |
| 738 | struct device_node *of_get_stdout(void) |
| 739 | { |
| 740 | return of_stdout; |
| 741 | } |