dtb_access.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "libfdt.h"
  7. #include "dtb_node.h"
  8. /* "/aliaes" node */
  9. static struct dtb_node *fdt_aliases;
  10. /**
  11. * of_find_property_value_of_size() - find property of given size
  12. *
  13. * Search for a property in a device node and validate the requested size.
  14. *
  15. * @np: device node from which the property value is to be read.
  16. * @propname: name of the property to be searched.
  17. * @len: requested length of property value
  18. *
  19. * @return the property value on success, -EINVAL if the property does not
  20. * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the
  21. * property data isn't large enough.
  22. */
  23. static void *dtb_node_find_property_value_of_size(const struct dtb_node *dn,
  24. const char *propname, uint32_t len)
  25. {
  26. struct dtb_property *prop = dtb_node_get_dtb_node_property(dn, propname, NULL);
  27. if (!prop)
  28. return ERR_PTR(-EINVAL);
  29. if (!prop->value)
  30. return ERR_PTR(-ENODATA);
  31. if (len > prop->size)
  32. return ERR_PTR(-EOVERFLOW);
  33. return prop->value;
  34. }
  35. int dtb_node_read_u32(const struct dtb_node *dn, const char *propname, uint32_t *outp)
  36. {
  37. const uint32_t *val;
  38. debug("%s: %s: \n", __func__, propname);
  39. if (!dn)
  40. return -EINVAL;
  41. val = dtb_node_find_property_value_of_size(dn, propname, sizeof(*outp));
  42. if (IS_ERR(val))
  43. {
  44. debug("(not found)\n");
  45. return PTR_ERR(val);
  46. }
  47. *outp = fdt32_to_cpu(*val);
  48. debug("%#x (%d)\n", *outp, *outp);
  49. return 0;
  50. }
  51. uint32_t dtb_node_read_u32_default(const struct dtb_node *node, const char *propname, uint32_t def)
  52. {
  53. dtb_node_read_u32(node, propname, &def);
  54. return def;
  55. }
  56. int dtb_node_read_u32_array(const struct dtb_node *dn, const char *propname,
  57. uint32_t *out_values, size_t sz)
  58. {
  59. const uint32_t *val;
  60. debug("%s: %s: ", __func__, propname);
  61. val = dtb_node_find_property_value_of_size(dn, propname,
  62. sz * sizeof(*out_values));
  63. if (IS_ERR(val))
  64. return PTR_ERR(val);
  65. debug("size %zd, val:%d\n", sz, *val);
  66. while (sz--)
  67. *out_values++ = fdt32_to_cpu(*val++);
  68. return 0;
  69. }
  70. uint32_t dtb_node_read_u32_index_default(const struct dtb_node *node, const char *propname, int index,
  71. uint32_t def)
  72. {
  73. RT_ASSERT(dtb_node_valid(node));
  74. dtb_node_read_u32_index(node, propname, index, &def);
  75. return def;
  76. }
  77. int dtb_node_read_s32_default(const struct dtb_node *node, const char *propname, int32_t def)
  78. {
  79. RT_ASSERT(dtb_node_valid(node));
  80. dtb_node_read_u32(node, propname, (uint32_t *)&def);
  81. return def;
  82. }
  83. int dtb_node_read_u32_index(const struct dtb_node *dn, const char *propname,
  84. int index, uint32_t *outp)
  85. {
  86. const uint32_t *val;
  87. debug("%s: %s: ", __func__, propname);
  88. if (!dn)
  89. return -EINVAL;
  90. val = dtb_node_find_property_value_of_size(dn, propname,
  91. sizeof(*outp) * (index + 1));
  92. if (IS_ERR(val))
  93. {
  94. debug("(not found)\n");
  95. return PTR_ERR(val);
  96. }
  97. *outp = fdt32_to_cpu(val[index]);
  98. debug("%#x (%d)\n", *outp, *outp);
  99. return 0;
  100. }
  101. int dtb_node_read_u64(const struct dtb_node *dn, const char *propname, uint64_t *outp)
  102. {
  103. const uint64_t *val;
  104. debug("%s: %s: ", __func__, propname);
  105. if (!dn)
  106. return -EINVAL;
  107. val = dtb_node_find_property_value_of_size(dn, propname, sizeof(*outp));
  108. if (IS_ERR(val))
  109. {
  110. debug("(not found)\n");
  111. return PTR_ERR(val);
  112. }
  113. *outp = fdt64_to_cpu(*val);
  114. debug("%#llx (%lld)\n", (unsigned long long)*outp,
  115. (unsigned long long)*outp);
  116. return 0;
  117. }
  118. uint64_t dtb_node_read_u64_default(const struct dtb_node *node, const char *propname, uint64_t def)
  119. {
  120. RT_ASSERT(dtb_node_valid(node));
  121. dtb_node_read_u64(node, propname, &def);
  122. return def;
  123. }
  124. int dtb_node_n_addr_cells(const struct dtb_node *dn)
  125. {
  126. const uint32_t *ip;
  127. do
  128. {
  129. if (dn->parent)
  130. dn = dn->parent;
  131. ip = dtb_node_get_dtb_node_property_value(dn, "#address-cells", NULL);
  132. if (ip)
  133. return fdt32_to_cpu(*ip);
  134. } while (dn->parent);
  135. /* No #address-cells property for the root node */
  136. return DEV_ROOT_NODE_ADDR_CELLS_DEFAULT;
  137. }
  138. int dtb_node_n_size_cells(const struct dtb_node *dn)
  139. {
  140. const uint32_t *ip;
  141. do
  142. {
  143. if (dn->parent)
  144. dn = dn->parent;
  145. ip = dtb_node_get_dtb_node_property_value(dn, "#size-cells", NULL);
  146. if (ip)
  147. return fdt32_to_cpu(*ip);
  148. } while (dn->parent);
  149. /* No #size-cells property for the root node */
  150. return DEV_ROOT_NODE_SIZE_CELLS_DEFAULT;
  151. }
  152. int dtb_node_simple_addr_cells(const struct dtb_node *dn)
  153. {
  154. const uint32_t *ip;
  155. ip = dtb_node_get_dtb_node_property_value(dn, "#address-cells", NULL);
  156. if (ip)
  157. return fdt32_to_cpu(*ip);
  158. /* Return a default of 2 to match fdt_address_cells()*/
  159. return 2;
  160. }
  161. int dtb_node_simple_size_cells(const struct dtb_node *dn)
  162. {
  163. const uint32_t *ip;
  164. ip = dtb_node_get_dtb_node_property_value(dn, "#size-cells", NULL);
  165. if (ip)
  166. return fdt32_to_cpu(*ip);
  167. /* Return a default of 2 to match fdt_size_cells()*/
  168. return 2;
  169. }
  170. struct dtb_property *dtb_node_get_dtb_node_property(const struct dtb_node *dtb_node, const char *property_name, int *property_size)
  171. {
  172. struct dtb_property *dtb_property = NULL;
  173. if (dtb_node != NULL && property_name != NULL)
  174. {
  175. dtb_property = dtb_node->properties;
  176. while (dtb_property != NULL)
  177. {
  178. if (!strcmp(dtb_property->name, property_name))
  179. {
  180. if (property_size != NULL)
  181. {
  182. *property_size = dtb_property->size;
  183. }
  184. return dtb_property;
  185. }
  186. dtb_property = dtb_property->next;
  187. }
  188. }
  189. return dtb_property;
  190. }
  191. #define for_each_property_of_node(dn, pp) \
  192. for (pp = dn->properties; pp != NULL; pp = pp->next)
  193. struct dtb_node *dtb_node_find_node_opts_by_path(const char *path,
  194. const char **opts)
  195. {
  196. struct dtb_node *np = NULL;
  197. struct dtb_property *pp;
  198. const char *separator = strchr(path, ':');
  199. if (opts)
  200. *opts = separator ? separator + 1 : NULL;
  201. if (strcmp(path, "/") == 0)
  202. return dtb_node_get(get_dtb_node_head());
  203. /* The path could begin with an alias */
  204. if (*path != '/')
  205. {
  206. int len;
  207. const char *p = separator;
  208. if (!p)
  209. p = strchrnul(path, '/');
  210. len = p - path;
  211. /* of_aliases must not be NULL */
  212. if (!fdt_aliases)
  213. return NULL;
  214. for_each_property_of_node(fdt_aliases, pp)
  215. {
  216. if (strlen(pp->name) == len && !strncmp(pp->name, path,
  217. len))
  218. {
  219. np = dtb_node_find_node_by_path(pp->value);
  220. break;
  221. }
  222. }
  223. if (!np)
  224. return NULL;
  225. path = p;
  226. }
  227. /* Step down the tree matching path components */
  228. if (!np)
  229. np = dtb_node_get(get_dtb_node_head());
  230. while (np && *path == '/')
  231. {
  232. struct dtb_node *tmp = np;
  233. path++; /* Increment past '/' delimiter */
  234. np = dtb_node_get_dtb_node_by_path(np, path);
  235. dtb_node_put(tmp);
  236. path = strchrnul(path, '/');
  237. if (separator && separator < path)
  238. break;
  239. }
  240. return np;
  241. }
  242. struct dtb_node *dtb_node_find_compatible_node(struct dtb_node *from, const char *compatible)
  243. {
  244. struct dtb_node *dn;
  245. for_each_of_allnodes_from(from, dn)
  246. {
  247. if (dtb_node_get_dtb_node_compatible_match(dn, compatible) &&
  248. dtb_node_get(dn))
  249. break;
  250. }
  251. dtb_node_put(from);
  252. return dn;
  253. }
  254. void *dtb_node_get_dtb_node_property_value(const struct dtb_node *dtb_node, const char *property_name, int *property_size)
  255. {
  256. struct dtb_property *dtb_property = dtb_node_get_dtb_node_property(dtb_node, property_name, NULL);
  257. if (!dtb_property || !dtb_property->value || !dtb_property->size)
  258. {
  259. return NULL;
  260. }
  261. if (property_size != NULL)
  262. {
  263. *property_size = dtb_property->size;
  264. }
  265. return dtb_property->value;
  266. }
  267. const struct dtb_node *dtb_node_find_node_by_prop_value(struct dtb_node *from,
  268. const char *propname,
  269. const void *propval, int proplen)
  270. {
  271. struct dtb_node *np;
  272. void *value;
  273. for_each_of_allnodes_from(from, np)
  274. {
  275. value = dtb_node_get_dtb_node_property_value(np, propname, &proplen);
  276. if (!memcmp(value, propval, proplen) && dtb_node_get(np))
  277. break;
  278. }
  279. dtb_node_put(from);
  280. return np;
  281. }
  282. struct dtb_node *dtb_node_find_all_nodes(const struct dtb_node *prev)
  283. {
  284. const struct dtb_node *dn;
  285. if (!prev)
  286. {
  287. dn = get_dtb_node_head();
  288. }
  289. else if (prev->child)
  290. {
  291. dn = prev->child;
  292. }
  293. else
  294. {
  295. /*
  296. * Walk back up looking for a sibling, or the end of the
  297. * structure
  298. */
  299. dn = prev;
  300. while (dn->parent && !dn->sibling)
  301. dn = dn->parent;
  302. dn = dn->sibling; /* Might be null at the end of the tree */
  303. }
  304. return (struct dtb_node *)dn;
  305. }
  306. rt_bool_t dtb_node_device_is_available(const struct dtb_node *device)
  307. {
  308. const char *status;
  309. int statlen;
  310. if (!device)
  311. return RT_FALSE;
  312. status = dtb_node_get_dtb_node_property_value(device, "status", &statlen);
  313. if (status == NULL)
  314. return RT_TRUE;
  315. if (statlen > 0)
  316. {
  317. if (!strcmp(status, "okay"))
  318. return RT_TRUE;
  319. }
  320. return RT_FALSE;
  321. }
  322. struct dtb_node *dtb_node_get_parent(const struct dtb_node *node)
  323. {
  324. const struct dtb_node *dn;
  325. if (!node)
  326. return NULL;
  327. dn = dtb_node_get(node->parent);
  328. return (struct dtb_node *)dn;
  329. }
  330. struct dtb_node *dtb_node_find_node_by_phandle(phandle handle)
  331. {
  332. struct dtb_node *dn;
  333. if (!handle)
  334. return NULL;
  335. for_each_of_allnodes(dn) if (dn->handle == handle) break;
  336. (void)dtb_node_get(dn);
  337. return dn;
  338. }
  339. int dtb_node_property_match_string(const struct dtb_node *dn, const char *propname,
  340. const char *string)
  341. {
  342. const struct dtb_property *prop = dtb_node_get_dtb_node_property(dn, propname, NULL);
  343. size_t l;
  344. int i;
  345. const char *p, *end;
  346. if (!prop)
  347. return -EINVAL;
  348. if (!prop->value)
  349. return -ENODATA;
  350. p = prop->value;
  351. end = p + prop->size;
  352. for (i = 0; p < end; i++, p += l)
  353. {
  354. l = strnlen(p, end - p) + 1;
  355. if (p + l > end)
  356. return -EILSEQ;
  357. debug("comparing %s with %s\n", string, p);
  358. if (strcmp(string, p) == 0)
  359. return i; /* Found it; return index */
  360. }
  361. return -ENODATA;
  362. }
  363. /**
  364. * of_property_read_string_helper() - Utility helper for parsing string properties
  365. * @np: device node from which the property value is to be read.
  366. * @propname: name of the property to be searched.
  367. * @out_strs: output array of string pointers.
  368. * @sz: number of array elements to read.
  369. * @skip: Number of strings to skip over at beginning of list.
  370. *
  371. * Don't call this function directly. It is a utility helper for the
  372. * of_property_read_string*() family of functions.
  373. */
  374. int dtb_node_property_read_string_helper(const struct dtb_node *dn,
  375. const char *propname, const char **out_strs,
  376. size_t sz, int skip)
  377. {
  378. const struct dtb_property *prop = dtb_node_get_dtb_node_property(dn, propname, NULL);
  379. int l = 0, i = 0;
  380. const char *p, *end;
  381. if (!prop)
  382. return -EINVAL;
  383. if (!prop->value)
  384. return -ENODATA;
  385. p = prop->value;
  386. end = p + prop->size;
  387. for (i = 0; p < end && (!out_strs || i < skip + sz); i++, p += l)
  388. {
  389. l = strnlen(p, end - p) + 1;
  390. if (p + l > end)
  391. return -EILSEQ;
  392. if (out_strs && i >= skip)
  393. *out_strs++ = p;
  394. }
  395. i -= skip;
  396. return i <= 0 ? -ENODATA : i;
  397. }
  398. static int __dtb_node_parse_phandle_with_args(const struct dtb_node *dn,
  399. const char *list_name,
  400. const char *cells_name,
  401. int cell_count, int index,
  402. struct fdt_phandle_args *out_args)
  403. {
  404. const uint32_t *list, *list_end;
  405. int rc = 0, cur_index = 0;
  406. uint32_t count = 0;
  407. struct dtb_node *node = NULL;
  408. phandle phandle;
  409. int size;
  410. /* Retrieve the phandle list property */
  411. list = dtb_node_get_dtb_node_property_value(dn, list_name, &size);
  412. if (!list)
  413. return -ENOENT;
  414. list_end = list + size / sizeof(*list);
  415. /* Loop over the phandles until all the requested entry is found */
  416. while (list < list_end)
  417. {
  418. rc = -EINVAL;
  419. count = 0;
  420. /*
  421. * If phandle is 0, then it is an empty entry with no
  422. * arguments. Skip forward to the next entry.
  423. */
  424. phandle = fdt32_to_cpu(*(list++));
  425. if (phandle)
  426. {
  427. /*
  428. * Find the provider node and parse the #*-cells
  429. * property to determine the argument length.
  430. *
  431. * This is not needed if the cell count is hard-coded
  432. * (i.e. cells_name not set, but cell_count is set),
  433. * except when we're going to return the found node
  434. * below.
  435. */
  436. if (cells_name || cur_index == index)
  437. {
  438. node = dtb_node_find_node_by_phandle(phandle);
  439. if (!node)
  440. {
  441. debug("%s: could not find phandle\n",
  442. dn->path);
  443. goto err;
  444. }
  445. }
  446. if (cells_name)
  447. {
  448. if (dtb_node_read_u32(node, cells_name, &count))
  449. {
  450. debug("%s: could not get %s for %s\n",
  451. dn->path, cells_name,
  452. node->path);
  453. goto err;
  454. }
  455. }
  456. else
  457. {
  458. count = cell_count;
  459. }
  460. /*
  461. * Make sure that the arguments actually fit in the
  462. * remaining property data length
  463. */
  464. if (list + count > list_end)
  465. {
  466. debug("%s: arguments longer than property\n",
  467. dn->path);
  468. goto err;
  469. }
  470. }
  471. /*
  472. * All of the error cases above bail out of the loop, so at
  473. * this point, the parsing is successful. If the requested
  474. * index matches, then fill the out_args structure and return,
  475. * or return -ENOENT for an empty entry.
  476. */
  477. rc = -ENOENT;
  478. if (cur_index == index)
  479. {
  480. if (!phandle)
  481. goto err;
  482. if (out_args)
  483. {
  484. int i;
  485. if (count > FDT_MAX_PHANDLE_ARGS)
  486. count = FDT_MAX_PHANDLE_ARGS;
  487. out_args->np = node;
  488. out_args->args_count = count;
  489. for (i = 0; i < count; i++)
  490. out_args->args[i] =
  491. fdt32_to_cpu(*(list++));
  492. }
  493. else
  494. {
  495. dtb_node_put(node);
  496. }
  497. /* Found it! return success */
  498. return 0;
  499. }
  500. dtb_node_put(node);
  501. node = NULL;
  502. list += count;
  503. cur_index++;
  504. }
  505. /*
  506. * Unlock node before returning result; will be one of:
  507. * -ENOENT : index is for empty phandle
  508. * -EINVAL : parsing error on data
  509. * [1..n] : Number of phandle (count mode; when index = -1)
  510. */
  511. rc = index < 0 ? cur_index : -ENOENT;
  512. err:
  513. if (node)
  514. dtb_node_put(node);
  515. return rc;
  516. }
  517. struct dtb_node *dtb_node_parse_phandle(const struct dtb_node *dn,
  518. const char *phandle_name, int index)
  519. {
  520. struct fdt_phandle_args args;
  521. if (index < 0)
  522. return NULL;
  523. if (__dtb_node_parse_phandle_with_args(dn, phandle_name, NULL, 0, index,
  524. &args))
  525. return NULL;
  526. return args.np;
  527. }
  528. int dtb_node_parse_phandle_with_args(const struct dtb_node *dn,
  529. const char *list_name, const char *cells_name,
  530. int index, struct fdt_phandle_args *out_args)
  531. {
  532. if (index < 0)
  533. return -EINVAL;
  534. return __dtb_node_parse_phandle_with_args(dn, list_name, cells_name, 0,
  535. index, out_args);
  536. }
  537. int dtb_node_count_phandle_with_args(const struct dtb_node *dn,
  538. const char *list_name, const char *cells_name)
  539. {
  540. return __dtb_node_parse_phandle_with_args(dn, list_name, cells_name, 0,
  541. -1, NULL);
  542. }