of_access.c 19 KB

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