fdt_ro.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * libfdt - Flat Device Tree manipulation
  3. * Copyright (C) 2006 David Gibson, IBM Corporation.
  4. * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
  5. */
  6. #include "libfdt_env.h"
  7. #ifndef USE_HOSTCC
  8. #include <fdt.h>
  9. #include <libfdt.h>
  10. #else
  11. #include "fdt_host.h"
  12. #endif
  13. #include "libfdt_internal.h"
  14. static int _fdt_nodename_eq(const void *fdt, int offset,
  15. const char *s, int len)
  16. {
  17. const char *p = fdt_offset_ptr(fdt, offset + FDT_TAGSIZE, len+1);
  18. if (! p)
  19. /* short match */
  20. return 0;
  21. if (memcmp(p, s, len) != 0)
  22. return 0;
  23. if (p[len] == '\0')
  24. return 1;
  25. else if (!memchr(s, '@', len) && (p[len] == '@'))
  26. return 1;
  27. else
  28. return 0;
  29. }
  30. const char *fdt_string(const void *fdt, int stroffset)
  31. {
  32. return (const char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
  33. }
  34. static int _fdt_string_eq(const void *fdt, int stroffset,
  35. const char *s, int len)
  36. {
  37. const char *p = fdt_string(fdt, stroffset);
  38. return (strnlen(p, len + 1) == len) && (memcmp(p, s, len) == 0);
  39. }
  40. int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)
  41. {
  42. FDT_CHECK_HEADER(fdt);
  43. *address = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->address);
  44. *size = fdt64_to_cpu(_fdt_mem_rsv(fdt, n)->size);
  45. return 0;
  46. }
  47. int fdt_num_mem_rsv(const void *fdt)
  48. {
  49. int i = 0;
  50. while (fdt64_to_cpu(_fdt_mem_rsv(fdt, i)->size) != 0)
  51. i++;
  52. return i;
  53. }
  54. static int _nextprop(const void *fdt, int offset)
  55. {
  56. uint32_t tag;
  57. int nextoffset;
  58. do {
  59. tag = fdt_next_tag(fdt, offset, &nextoffset);
  60. switch (tag) {
  61. case FDT_END:
  62. if (nextoffset >= 0)
  63. return -FDT_ERR_BADSTRUCTURE;
  64. else
  65. return nextoffset;
  66. case FDT_PROP:
  67. return offset;
  68. }
  69. offset = nextoffset;
  70. } while (tag == FDT_NOP);
  71. return -FDT_ERR_NOTFOUND;
  72. }
  73. int fdt_subnode_offset_namelen(const void *fdt, int offset,
  74. const char *name, int namelen)
  75. {
  76. int depth;
  77. FDT_CHECK_HEADER(fdt);
  78. for (depth = 0;
  79. (offset >= 0) && (depth >= 0);
  80. offset = fdt_next_node(fdt, offset, &depth))
  81. if ((depth == 1)
  82. && _fdt_nodename_eq(fdt, offset, name, namelen))
  83. return offset;
  84. if (depth < 0)
  85. return -FDT_ERR_NOTFOUND;
  86. return offset; /* error */
  87. }
  88. int fdt_subnode_offset(const void *fdt, int parentoffset,
  89. const char *name)
  90. {
  91. return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
  92. }
  93. /*
  94. * Find the next of path seperator, note we need to search for both '/' and ':'
  95. * and then take the first one so that we do the rigth thing for e.g.
  96. * "foo/bar:option" and "bar:option/otheroption", both of which happen, so
  97. * first searching for either ':' or '/' does not work.
  98. */
  99. static const char *fdt_path_next_seperator(const char *path)
  100. {
  101. const char *sep1 = strchr(path, '/');
  102. const char *sep2 = strchr(path, ':');
  103. if (sep1 && sep2)
  104. return (sep1 < sep2) ? sep1 : sep2;
  105. else if (sep1)
  106. return sep1;
  107. else
  108. return sep2;
  109. }
  110. int fdt_path_offset(const void *fdt, const char *path)
  111. {
  112. const char *end = path + strlen(path);
  113. const char *p = path;
  114. int offset = 0;
  115. FDT_CHECK_HEADER(fdt);
  116. /* see if we have an alias */
  117. if (*path != '/') {
  118. const char *q = fdt_path_next_seperator(path);
  119. if (!q)
  120. q = end;
  121. p = fdt_get_alias_namelen(fdt, p, q - p);
  122. if (!p)
  123. return -FDT_ERR_BADPATH;
  124. offset = fdt_path_offset(fdt, p);
  125. p = q;
  126. }
  127. while (*p) {
  128. const char *q;
  129. while (*p == '/')
  130. p++;
  131. if (*p == '\0' || *p == ':')
  132. return offset;
  133. q = fdt_path_next_seperator(p);
  134. if (! q)
  135. q = end;
  136. offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
  137. if (offset < 0)
  138. return offset;
  139. p = q;
  140. }
  141. return offset;
  142. }
  143. const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
  144. {
  145. const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
  146. int err;
  147. if (((err = fdt_check_header(fdt)) != 0)
  148. || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
  149. goto fail;
  150. if (len)
  151. *len = strlen(nh->name);
  152. return nh->name;
  153. fail:
  154. if (len)
  155. *len = err;
  156. return NULL;
  157. }
  158. int fdt_first_property_offset(const void *fdt, int nodeoffset)
  159. {
  160. int offset;
  161. if ((offset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
  162. return offset;
  163. return _nextprop(fdt, offset);
  164. }
  165. int fdt_next_property_offset(const void *fdt, int offset)
  166. {
  167. if ((offset = _fdt_check_prop_offset(fdt, offset)) < 0)
  168. return offset;
  169. return _nextprop(fdt, offset);
  170. }
  171. const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
  172. int offset,
  173. int *lenp)
  174. {
  175. int err;
  176. const struct fdt_property *prop;
  177. if ((err = _fdt_check_prop_offset(fdt, offset)) < 0) {
  178. if (lenp)
  179. *lenp = err;
  180. return NULL;
  181. }
  182. prop = _fdt_offset_ptr(fdt, offset);
  183. if (lenp)
  184. *lenp = fdt32_to_cpu(prop->len);
  185. return prop;
  186. }
  187. const struct fdt_property *fdt_get_property_namelen(const void *fdt,
  188. int offset,
  189. const char *name,
  190. int namelen, int *lenp)
  191. {
  192. for (offset = fdt_first_property_offset(fdt, offset);
  193. (offset >= 0);
  194. (offset = fdt_next_property_offset(fdt, offset))) {
  195. const struct fdt_property *prop;
  196. if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) {
  197. offset = -FDT_ERR_INTERNAL;
  198. break;
  199. }
  200. if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
  201. name, namelen))
  202. return prop;
  203. }
  204. if (lenp)
  205. *lenp = offset;
  206. return NULL;
  207. }
  208. const struct fdt_property *fdt_get_property(const void *fdt,
  209. int nodeoffset,
  210. const char *name, int *lenp)
  211. {
  212. return fdt_get_property_namelen(fdt, nodeoffset, name,
  213. strlen(name), lenp);
  214. }
  215. const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
  216. const char *name, int namelen, int *lenp)
  217. {
  218. const struct fdt_property *prop;
  219. prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
  220. if (! prop)
  221. return NULL;
  222. return prop->data;
  223. }
  224. const void *fdt_getprop_by_offset(const void *fdt, int offset,
  225. const char **namep, int *lenp)
  226. {
  227. const struct fdt_property *prop;
  228. prop = fdt_get_property_by_offset(fdt, offset, lenp);
  229. if (!prop)
  230. return NULL;
  231. if (namep)
  232. *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
  233. return prop->data;
  234. }
  235. const void *fdt_getprop(const void *fdt, int nodeoffset,
  236. const char *name, int *lenp)
  237. {
  238. return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
  239. }
  240. uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
  241. {
  242. const fdt32_t *php;
  243. int len;
  244. /* FIXME: This is a bit sub-optimal, since we potentially scan
  245. * over all the properties twice. */
  246. php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
  247. if (!php || (len != sizeof(*php))) {
  248. php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
  249. if (!php || (len != sizeof(*php)))
  250. return 0;
  251. }
  252. return fdt32_to_cpu(*php);
  253. }
  254. const char *fdt_get_alias_namelen(const void *fdt,
  255. const char *name, int namelen)
  256. {
  257. int aliasoffset;
  258. aliasoffset = fdt_path_offset(fdt, "/aliases");
  259. if (aliasoffset < 0)
  260. return NULL;
  261. return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
  262. }
  263. const char *fdt_get_alias(const void *fdt, const char *name)
  264. {
  265. return fdt_get_alias_namelen(fdt, name, strlen(name));
  266. }
  267. int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
  268. {
  269. int pdepth = 0, p = 0;
  270. int offset, depth, namelen;
  271. const char *name;
  272. FDT_CHECK_HEADER(fdt);
  273. if (buflen < 2)
  274. return -FDT_ERR_NOSPACE;
  275. for (offset = 0, depth = 0;
  276. (offset >= 0) && (offset <= nodeoffset);
  277. offset = fdt_next_node(fdt, offset, &depth)) {
  278. while (pdepth > depth) {
  279. do {
  280. p--;
  281. } while (buf[p-1] != '/');
  282. pdepth--;
  283. }
  284. if (pdepth >= depth) {
  285. name = fdt_get_name(fdt, offset, &namelen);
  286. if (!name)
  287. return namelen;
  288. if ((p + namelen + 1) <= buflen) {
  289. memcpy(buf + p, name, namelen);
  290. p += namelen;
  291. buf[p++] = '/';
  292. pdepth++;
  293. }
  294. }
  295. if (offset == nodeoffset) {
  296. if (pdepth < (depth + 1))
  297. return -FDT_ERR_NOSPACE;
  298. if (p > 1) /* special case so that root path is "/", not "" */
  299. p--;
  300. buf[p] = '\0';
  301. return 0;
  302. }
  303. }
  304. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  305. return -FDT_ERR_BADOFFSET;
  306. else if (offset == -FDT_ERR_BADOFFSET)
  307. return -FDT_ERR_BADSTRUCTURE;
  308. return offset; /* error from fdt_next_node() */
  309. }
  310. int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
  311. int supernodedepth, int *nodedepth)
  312. {
  313. int offset, depth;
  314. int supernodeoffset = -FDT_ERR_INTERNAL;
  315. FDT_CHECK_HEADER(fdt);
  316. if (supernodedepth < 0)
  317. return -FDT_ERR_NOTFOUND;
  318. for (offset = 0, depth = 0;
  319. (offset >= 0) && (offset <= nodeoffset);
  320. offset = fdt_next_node(fdt, offset, &depth)) {
  321. if (depth == supernodedepth)
  322. supernodeoffset = offset;
  323. if (offset == nodeoffset) {
  324. if (nodedepth)
  325. *nodedepth = depth;
  326. if (supernodedepth > depth)
  327. return -FDT_ERR_NOTFOUND;
  328. else
  329. return supernodeoffset;
  330. }
  331. }
  332. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  333. return -FDT_ERR_BADOFFSET;
  334. else if (offset == -FDT_ERR_BADOFFSET)
  335. return -FDT_ERR_BADSTRUCTURE;
  336. return offset; /* error from fdt_next_node() */
  337. }
  338. int fdt_node_depth(const void *fdt, int nodeoffset)
  339. {
  340. int nodedepth;
  341. int err;
  342. err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
  343. if (err)
  344. return (err < 0) ? err : -FDT_ERR_INTERNAL;
  345. return nodedepth;
  346. }
  347. int fdt_parent_offset(const void *fdt, int nodeoffset)
  348. {
  349. int nodedepth = fdt_node_depth(fdt, nodeoffset);
  350. if (nodedepth < 0)
  351. return nodedepth;
  352. return fdt_supernode_atdepth_offset(fdt, nodeoffset,
  353. nodedepth - 1, NULL);
  354. }
  355. int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
  356. const char *propname,
  357. const void *propval, int proplen)
  358. {
  359. int offset;
  360. const void *val;
  361. int len;
  362. FDT_CHECK_HEADER(fdt);
  363. /* FIXME: The algorithm here is pretty horrible: we scan each
  364. * property of a node in fdt_getprop(), then if that didn't
  365. * find what we want, we scan over them again making our way
  366. * to the next node. Still it's the easiest to implement
  367. * approach; performance can come later. */
  368. for (offset = fdt_next_node(fdt, startoffset, NULL);
  369. offset >= 0;
  370. offset = fdt_next_node(fdt, offset, NULL)) {
  371. val = fdt_getprop(fdt, offset, propname, &len);
  372. if (val && (len == proplen)
  373. && (memcmp(val, propval, len) == 0))
  374. return offset;
  375. }
  376. return offset; /* error from fdt_next_node() */
  377. }
  378. int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
  379. {
  380. int offset;
  381. if ((phandle == 0) || (phandle == -1))
  382. return -FDT_ERR_BADPHANDLE;
  383. FDT_CHECK_HEADER(fdt);
  384. /* FIXME: The algorithm here is pretty horrible: we
  385. * potentially scan each property of a node in
  386. * fdt_get_phandle(), then if that didn't find what
  387. * we want, we scan over them again making our way to the next
  388. * node. Still it's the easiest to implement approach;
  389. * performance can come later. */
  390. for (offset = fdt_next_node(fdt, -1, NULL);
  391. offset >= 0;
  392. offset = fdt_next_node(fdt, offset, NULL)) {
  393. if (fdt_get_phandle(fdt, offset) == phandle)
  394. return offset;
  395. }
  396. return offset; /* error from fdt_next_node() */
  397. }
  398. int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
  399. {
  400. int len = strlen(str);
  401. const char *p;
  402. while (listlen >= len) {
  403. if (memcmp(str, strlist, len+1) == 0)
  404. return 1;
  405. p = memchr(strlist, '\0', listlen);
  406. if (!p)
  407. return 0; /* malformed strlist.. */
  408. listlen -= (p-strlist) + 1;
  409. strlist = p + 1;
  410. }
  411. return 0;
  412. }
  413. int fdt_count_strings(const void *fdt, int node, const char *property)
  414. {
  415. int length, i, count = 0;
  416. const char *list;
  417. list = fdt_getprop(fdt, node, property, &length);
  418. if (!list)
  419. return length;
  420. for (i = 0; i < length; i++) {
  421. int len = strlen(list);
  422. list += len + 1;
  423. i += len;
  424. count++;
  425. }
  426. return count;
  427. }
  428. int fdt_find_string(const void *fdt, int node, const char *property,
  429. const char *string)
  430. {
  431. const char *list, *end;
  432. int len, index = 0;
  433. list = fdt_getprop(fdt, node, property, &len);
  434. if (!list)
  435. return len;
  436. end = list + len;
  437. len = strlen(string);
  438. while (list < end) {
  439. int l = strlen(list);
  440. if (l == len && memcmp(list, string, len) == 0)
  441. return index;
  442. list += l + 1;
  443. index++;
  444. }
  445. return -FDT_ERR_NOTFOUND;
  446. }
  447. int fdt_get_string_index(const void *fdt, int node, const char *property,
  448. int index, const char **output)
  449. {
  450. const char *list;
  451. int length, i;
  452. list = fdt_getprop(fdt, node, property, &length);
  453. for (i = 0; i < length; i++) {
  454. int len = strlen(list);
  455. if (index == 0) {
  456. *output = list;
  457. return 0;
  458. }
  459. list += len + 1;
  460. i += len;
  461. index--;
  462. }
  463. return -FDT_ERR_NOTFOUND;
  464. }
  465. int fdt_get_string(const void *fdt, int node, const char *property,
  466. const char **output)
  467. {
  468. return fdt_get_string_index(fdt, node, property, 0, output);
  469. }
  470. int fdt_node_check_compatible(const void *fdt, int nodeoffset,
  471. const char *compatible)
  472. {
  473. const void *prop;
  474. int len;
  475. prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
  476. if (!prop)
  477. return len;
  478. if (fdt_stringlist_contains(prop, len, compatible))
  479. return 0;
  480. else
  481. return 1;
  482. }
  483. int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
  484. const char *compatible)
  485. {
  486. int offset, err;
  487. FDT_CHECK_HEADER(fdt);
  488. /* FIXME: The algorithm here is pretty horrible: we scan each
  489. * property of a node in fdt_node_check_compatible(), then if
  490. * that didn't find what we want, we scan over them again
  491. * making our way to the next node. Still it's the easiest to
  492. * implement approach; performance can come later. */
  493. for (offset = fdt_next_node(fdt, startoffset, NULL);
  494. offset >= 0;
  495. offset = fdt_next_node(fdt, offset, NULL)) {
  496. err = fdt_node_check_compatible(fdt, offset, compatible);
  497. if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
  498. return err;
  499. else if (err == 0)
  500. return offset;
  501. }
  502. return offset; /* error from fdt_next_node() */
  503. }