fdt_ro.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 (strlen(p) == 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. int fdt_path_offset(const void *fdt, const char *path)
  94. {
  95. const char *end = path + strlen(path);
  96. const char *p = path;
  97. int offset = 0;
  98. FDT_CHECK_HEADER(fdt);
  99. /* see if we have an alias */
  100. if (*path != '/') {
  101. const char *q = strchr(path, '/');
  102. if (!q)
  103. q = end;
  104. p = fdt_get_alias_namelen(fdt, p, q - p);
  105. if (!p)
  106. return -FDT_ERR_BADPATH;
  107. offset = fdt_path_offset(fdt, p);
  108. p = q;
  109. }
  110. while (*p) {
  111. const char *q;
  112. while (*p == '/')
  113. p++;
  114. if (! *p)
  115. return offset;
  116. q = strchr(p, '/');
  117. if (! q)
  118. q = end;
  119. offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
  120. if (offset < 0)
  121. return offset;
  122. p = q;
  123. }
  124. return offset;
  125. }
  126. const char *fdt_get_name(const void *fdt, int nodeoffset, int *len)
  127. {
  128. const struct fdt_node_header *nh = _fdt_offset_ptr(fdt, nodeoffset);
  129. int err;
  130. if (((err = fdt_check_header(fdt)) != 0)
  131. || ((err = _fdt_check_node_offset(fdt, nodeoffset)) < 0))
  132. goto fail;
  133. if (len)
  134. *len = strlen(nh->name);
  135. return nh->name;
  136. fail:
  137. if (len)
  138. *len = err;
  139. return NULL;
  140. }
  141. int fdt_first_property_offset(const void *fdt, int nodeoffset)
  142. {
  143. int offset;
  144. if ((offset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
  145. return offset;
  146. return _nextprop(fdt, offset);
  147. }
  148. int fdt_next_property_offset(const void *fdt, int offset)
  149. {
  150. if ((offset = _fdt_check_prop_offset(fdt, offset)) < 0)
  151. return offset;
  152. return _nextprop(fdt, offset);
  153. }
  154. const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
  155. int offset,
  156. int *lenp)
  157. {
  158. int err;
  159. const struct fdt_property *prop;
  160. if ((err = _fdt_check_prop_offset(fdt, offset)) < 0) {
  161. if (lenp)
  162. *lenp = err;
  163. return NULL;
  164. }
  165. prop = _fdt_offset_ptr(fdt, offset);
  166. if (lenp)
  167. *lenp = fdt32_to_cpu(prop->len);
  168. return prop;
  169. }
  170. const struct fdt_property *fdt_get_property_namelen(const void *fdt,
  171. int offset,
  172. const char *name,
  173. int namelen, int *lenp)
  174. {
  175. for (offset = fdt_first_property_offset(fdt, offset);
  176. (offset >= 0);
  177. (offset = fdt_next_property_offset(fdt, offset))) {
  178. const struct fdt_property *prop;
  179. if (!(prop = fdt_get_property_by_offset(fdt, offset, lenp))) {
  180. offset = -FDT_ERR_INTERNAL;
  181. break;
  182. }
  183. if (_fdt_string_eq(fdt, fdt32_to_cpu(prop->nameoff),
  184. name, namelen))
  185. return prop;
  186. }
  187. if (lenp)
  188. *lenp = offset;
  189. return NULL;
  190. }
  191. const struct fdt_property *fdt_get_property(const void *fdt,
  192. int nodeoffset,
  193. const char *name, int *lenp)
  194. {
  195. return fdt_get_property_namelen(fdt, nodeoffset, name,
  196. strlen(name), lenp);
  197. }
  198. const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
  199. const char *name, int namelen, int *lenp)
  200. {
  201. const struct fdt_property *prop;
  202. prop = fdt_get_property_namelen(fdt, nodeoffset, name, namelen, lenp);
  203. if (! prop)
  204. return NULL;
  205. return prop->data;
  206. }
  207. const void *fdt_getprop_by_offset(const void *fdt, int offset,
  208. const char **namep, int *lenp)
  209. {
  210. const struct fdt_property *prop;
  211. prop = fdt_get_property_by_offset(fdt, offset, lenp);
  212. if (!prop)
  213. return NULL;
  214. if (namep)
  215. *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
  216. return prop->data;
  217. }
  218. const void *fdt_getprop(const void *fdt, int nodeoffset,
  219. const char *name, int *lenp)
  220. {
  221. return fdt_getprop_namelen(fdt, nodeoffset, name, strlen(name), lenp);
  222. }
  223. uint32_t fdt_get_phandle(const void *fdt, int nodeoffset)
  224. {
  225. const fdt32_t *php;
  226. int len;
  227. /* FIXME: This is a bit sub-optimal, since we potentially scan
  228. * over all the properties twice. */
  229. php = fdt_getprop(fdt, nodeoffset, "phandle", &len);
  230. if (!php || (len != sizeof(*php))) {
  231. php = fdt_getprop(fdt, nodeoffset, "linux,phandle", &len);
  232. if (!php || (len != sizeof(*php)))
  233. return 0;
  234. }
  235. return fdt32_to_cpu(*php);
  236. }
  237. const char *fdt_get_alias_namelen(const void *fdt,
  238. const char *name, int namelen)
  239. {
  240. int aliasoffset;
  241. aliasoffset = fdt_path_offset(fdt, "/aliases");
  242. if (aliasoffset < 0)
  243. return NULL;
  244. return fdt_getprop_namelen(fdt, aliasoffset, name, namelen, NULL);
  245. }
  246. const char *fdt_get_alias(const void *fdt, const char *name)
  247. {
  248. return fdt_get_alias_namelen(fdt, name, strlen(name));
  249. }
  250. int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen)
  251. {
  252. int pdepth = 0, p = 0;
  253. int offset, depth, namelen;
  254. const char *name;
  255. FDT_CHECK_HEADER(fdt);
  256. if (buflen < 2)
  257. return -FDT_ERR_NOSPACE;
  258. for (offset = 0, depth = 0;
  259. (offset >= 0) && (offset <= nodeoffset);
  260. offset = fdt_next_node(fdt, offset, &depth)) {
  261. while (pdepth > depth) {
  262. do {
  263. p--;
  264. } while (buf[p-1] != '/');
  265. pdepth--;
  266. }
  267. if (pdepth >= depth) {
  268. name = fdt_get_name(fdt, offset, &namelen);
  269. if (!name)
  270. return namelen;
  271. if ((p + namelen + 1) <= buflen) {
  272. memcpy(buf + p, name, namelen);
  273. p += namelen;
  274. buf[p++] = '/';
  275. pdepth++;
  276. }
  277. }
  278. if (offset == nodeoffset) {
  279. if (pdepth < (depth + 1))
  280. return -FDT_ERR_NOSPACE;
  281. if (p > 1) /* special case so that root path is "/", not "" */
  282. p--;
  283. buf[p] = '\0';
  284. return 0;
  285. }
  286. }
  287. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  288. return -FDT_ERR_BADOFFSET;
  289. else if (offset == -FDT_ERR_BADOFFSET)
  290. return -FDT_ERR_BADSTRUCTURE;
  291. return offset; /* error from fdt_next_node() */
  292. }
  293. int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
  294. int supernodedepth, int *nodedepth)
  295. {
  296. int offset, depth;
  297. int supernodeoffset = -FDT_ERR_INTERNAL;
  298. FDT_CHECK_HEADER(fdt);
  299. if (supernodedepth < 0)
  300. return -FDT_ERR_NOTFOUND;
  301. for (offset = 0, depth = 0;
  302. (offset >= 0) && (offset <= nodeoffset);
  303. offset = fdt_next_node(fdt, offset, &depth)) {
  304. if (depth == supernodedepth)
  305. supernodeoffset = offset;
  306. if (offset == nodeoffset) {
  307. if (nodedepth)
  308. *nodedepth = depth;
  309. if (supernodedepth > depth)
  310. return -FDT_ERR_NOTFOUND;
  311. else
  312. return supernodeoffset;
  313. }
  314. }
  315. if ((offset == -FDT_ERR_NOTFOUND) || (offset >= 0))
  316. return -FDT_ERR_BADOFFSET;
  317. else if (offset == -FDT_ERR_BADOFFSET)
  318. return -FDT_ERR_BADSTRUCTURE;
  319. return offset; /* error from fdt_next_node() */
  320. }
  321. int fdt_node_depth(const void *fdt, int nodeoffset)
  322. {
  323. int nodedepth;
  324. int err;
  325. err = fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, &nodedepth);
  326. if (err)
  327. return (err < 0) ? err : -FDT_ERR_INTERNAL;
  328. return nodedepth;
  329. }
  330. int fdt_parent_offset(const void *fdt, int nodeoffset)
  331. {
  332. int nodedepth = fdt_node_depth(fdt, nodeoffset);
  333. if (nodedepth < 0)
  334. return nodedepth;
  335. return fdt_supernode_atdepth_offset(fdt, nodeoffset,
  336. nodedepth - 1, NULL);
  337. }
  338. int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
  339. const char *propname,
  340. const void *propval, int proplen)
  341. {
  342. int offset;
  343. const void *val;
  344. int len;
  345. FDT_CHECK_HEADER(fdt);
  346. /* FIXME: The algorithm here is pretty horrible: we scan each
  347. * property of a node in fdt_getprop(), then if that didn't
  348. * find what we want, we scan over them again making our way
  349. * to the next node. Still it's the easiest to implement
  350. * approach; performance can come later. */
  351. for (offset = fdt_next_node(fdt, startoffset, NULL);
  352. offset >= 0;
  353. offset = fdt_next_node(fdt, offset, NULL)) {
  354. val = fdt_getprop(fdt, offset, propname, &len);
  355. if (val && (len == proplen)
  356. && (memcmp(val, propval, len) == 0))
  357. return offset;
  358. }
  359. return offset; /* error from fdt_next_node() */
  360. }
  361. int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle)
  362. {
  363. int offset;
  364. if ((phandle == 0) || (phandle == -1))
  365. return -FDT_ERR_BADPHANDLE;
  366. FDT_CHECK_HEADER(fdt);
  367. /* FIXME: The algorithm here is pretty horrible: we
  368. * potentially scan each property of a node in
  369. * fdt_get_phandle(), then if that didn't find what
  370. * we want, we scan over them again making our way to the next
  371. * node. Still it's the easiest to implement approach;
  372. * performance can come later. */
  373. for (offset = fdt_next_node(fdt, -1, NULL);
  374. offset >= 0;
  375. offset = fdt_next_node(fdt, offset, NULL)) {
  376. if (fdt_get_phandle(fdt, offset) == phandle)
  377. return offset;
  378. }
  379. return offset; /* error from fdt_next_node() */
  380. }
  381. int fdt_stringlist_contains(const char *strlist, int listlen, const char *str)
  382. {
  383. int len = strlen(str);
  384. const char *p;
  385. while (listlen >= len) {
  386. if (memcmp(str, strlist, len+1) == 0)
  387. return 1;
  388. p = memchr(strlist, '\0', listlen);
  389. if (!p)
  390. return 0; /* malformed strlist.. */
  391. listlen -= (p-strlist) + 1;
  392. strlist = p + 1;
  393. }
  394. return 0;
  395. }
  396. int fdt_node_check_compatible(const void *fdt, int nodeoffset,
  397. const char *compatible)
  398. {
  399. const void *prop;
  400. int len;
  401. prop = fdt_getprop(fdt, nodeoffset, "compatible", &len);
  402. if (!prop)
  403. return len;
  404. if (fdt_stringlist_contains(prop, len, compatible))
  405. return 0;
  406. else
  407. return 1;
  408. }
  409. int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
  410. const char *compatible)
  411. {
  412. int offset, err;
  413. FDT_CHECK_HEADER(fdt);
  414. /* FIXME: The algorithm here is pretty horrible: we scan each
  415. * property of a node in fdt_node_check_compatible(), then if
  416. * that didn't find what we want, we scan over them again
  417. * making our way to the next node. Still it's the easiest to
  418. * implement approach; performance can come later. */
  419. for (offset = fdt_next_node(fdt, startoffset, NULL);
  420. offset >= 0;
  421. offset = fdt_next_node(fdt, offset, NULL)) {
  422. err = fdt_node_check_compatible(fdt, offset, compatible);
  423. if ((err < 0) && (err != -FDT_ERR_NOTFOUND))
  424. return err;
  425. else if (err == 0)
  426. return offset;
  427. }
  428. return offset; /* error from fdt_next_node() */
  429. }