fdt_ro.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * libfdt - Flat Device Tree manipulation
  3. * Copyright (C) 2006 David Gibson, IBM Corporation.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation; either version 2.1 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "config.h"
  20. #if CONFIG_OF_LIBFDT
  21. #include "libfdt_env.h"
  22. #include <fdt.h>
  23. #include <libfdt.h>
  24. #include "libfdt_internal.h"
  25. #define CHECK_HEADER(fdt) { \
  26. int err; \
  27. if ((err = fdt_check_header(fdt)) != 0) \
  28. return err; \
  29. }
  30. static int offset_streq(const void *fdt, int offset,
  31. const char *s, int len)
  32. {
  33. const char *p = fdt_offset_ptr(fdt, offset, len+1);
  34. if (! p)
  35. /* short match */
  36. return 0;
  37. if (memcmp(p, s, len) != 0)
  38. return 0;
  39. if (p[len] != '\0')
  40. return 0;
  41. return 1;
  42. }
  43. /*
  44. * Checks if the property name matches.
  45. */
  46. static int prop_name_eq(const void *fdt, int offset, const char *name,
  47. struct fdt_property **prop, int *lenp)
  48. {
  49. int namestroff, len;
  50. *prop = fdt_offset_ptr_typed(fdt, offset, *prop);
  51. if (! *prop)
  52. return -FDT_ERR_BADSTRUCTURE;
  53. namestroff = fdt32_to_cpu((*prop)->nameoff);
  54. if (streq(fdt_string(fdt, namestroff), name)) {
  55. len = fdt32_to_cpu((*prop)->len);
  56. *prop = fdt_offset_ptr(fdt, offset,
  57. sizeof(**prop) + len);
  58. if (*prop) {
  59. if (lenp)
  60. *lenp = len;
  61. return 1;
  62. } else
  63. return -FDT_ERR_BADSTRUCTURE;
  64. }
  65. return 0;
  66. }
  67. /*
  68. * Return a pointer to the string at the given string offset.
  69. */
  70. char *fdt_string(const void *fdt, int stroffset)
  71. {
  72. return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
  73. }
  74. /*
  75. * Check if the specified node is compatible by comparing the tokens
  76. * in its "compatible" property with the specified string:
  77. *
  78. * nodeoffset - starting place of the node
  79. * compat - the string to match to one of the tokens in the
  80. * "compatible" list.
  81. */
  82. int fdt_node_is_compatible(const void *fdt, int nodeoffset,
  83. const char *compat)
  84. {
  85. const char* cp;
  86. int cplen, len;
  87. cp = fdt_getprop(fdt, nodeoffset, "compatible", &cplen);
  88. if (cp == NULL)
  89. return 0;
  90. while (cplen > 0) {
  91. if (strncmp(cp, compat, strlen(compat)) == 0)
  92. return 1;
  93. len = strlen(cp) + 1;
  94. cp += len;
  95. cplen -= len;
  96. }
  97. return 0;
  98. }
  99. /*
  100. * Find a node by its device type property. On success, the offset of that
  101. * node is returned or an error code otherwise:
  102. *
  103. * nodeoffset - the node to start searching from or 0, the node you pass
  104. * will not be searched, only the next one will; typically,
  105. * you pass 0 to start the search and then what the previous
  106. * call returned.
  107. * type - the device type string to match against.
  108. */
  109. int fdt_find_node_by_type(const void *fdt, int nodeoffset, const char *type)
  110. {
  111. int offset, nextoffset;
  112. struct fdt_property *prop;
  113. uint32_t tag;
  114. int len, ret;
  115. CHECK_HEADER(fdt);
  116. tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
  117. if (tag != FDT_BEGIN_NODE)
  118. return -FDT_ERR_BADOFFSET;
  119. if (nodeoffset)
  120. nodeoffset = 0; /* start searching with next node */
  121. while (1) {
  122. offset = nextoffset;
  123. tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
  124. switch (tag) {
  125. case FDT_BEGIN_NODE:
  126. nodeoffset = offset;
  127. break;
  128. case FDT_PROP:
  129. if (nodeoffset == 0)
  130. break;
  131. ret = prop_name_eq(fdt, offset, "device_type",
  132. &prop, &len);
  133. if (ret < 0)
  134. return ret;
  135. else if (ret > 0 &&
  136. strncmp(prop->data, type, len - 1) == 0)
  137. return nodeoffset;
  138. break;
  139. case FDT_END_NODE:
  140. case FDT_NOP:
  141. break;
  142. case FDT_END:
  143. return -FDT_ERR_NOTFOUND;
  144. default:
  145. return -FDT_ERR_BADSTRUCTURE;
  146. }
  147. }
  148. }
  149. /*
  150. * Find a node based on its device type and one of the tokens in its its
  151. * "compatible" property. On success, the offset of that node is returned
  152. * or an error code otherwise:
  153. *
  154. * nodeoffset - the node to start searching from or 0, the node you pass
  155. * will not be searched, only the next one will; typically,
  156. * you pass 0 to start the search and then what the previous
  157. * call returned.
  158. * type - the device type string to match against.
  159. * compat - the string to match to one of the tokens in the
  160. * "compatible" list.
  161. */
  162. int fdt_find_compatible_node(const void *fdt, int nodeoffset,
  163. const char *type, const char *compat)
  164. {
  165. int offset;
  166. offset = fdt_find_node_by_type(fdt, nodeoffset, type);
  167. if (offset < 0 || fdt_node_is_compatible(fdt, offset, compat))
  168. return offset;
  169. return -FDT_ERR_NOTFOUND;
  170. }
  171. /*
  172. * Return the node offset of the node specified by:
  173. * parentoffset - starting place (0 to start at the root)
  174. * name - name being searched for
  175. * namelen - length of the name: typically strlen(name)
  176. *
  177. * Notes:
  178. * If the start node has subnodes, the subnodes are _not_ searched for the
  179. * requested name.
  180. */
  181. int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
  182. const char *name, int namelen)
  183. {
  184. int level = 0;
  185. uint32_t tag;
  186. int offset, nextoffset;
  187. CHECK_HEADER(fdt);
  188. tag = fdt_next_tag(fdt, parentoffset, &nextoffset, NULL);
  189. if (tag != FDT_BEGIN_NODE)
  190. return -FDT_ERR_BADOFFSET;
  191. do {
  192. offset = nextoffset;
  193. tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
  194. switch (tag) {
  195. case FDT_END:
  196. return -FDT_ERR_TRUNCATED;
  197. case FDT_BEGIN_NODE:
  198. level++;
  199. /*
  200. * If we are nested down levels, ignore the strings
  201. * until we get back to the proper level.
  202. */
  203. if (level != 1)
  204. continue;
  205. /* Return the offset if this is "our" string. */
  206. if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen))
  207. return offset;
  208. break;
  209. case FDT_END_NODE:
  210. level--;
  211. break;
  212. case FDT_PROP:
  213. case FDT_NOP:
  214. break;
  215. default:
  216. return -FDT_ERR_BADSTRUCTURE;
  217. }
  218. } while (level >= 0);
  219. return -FDT_ERR_NOTFOUND;
  220. }
  221. /*
  222. * See fdt_subnode_offset_namelen()
  223. */
  224. int fdt_subnode_offset(const void *fdt, int parentoffset,
  225. const char *name)
  226. {
  227. return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
  228. }
  229. /*
  230. * Searches for the node corresponding to the given path and returns the
  231. * offset of that node.
  232. */
  233. int fdt_find_node_by_path(const void *fdt, const char *path)
  234. {
  235. const char *end = path + strlen(path);
  236. const char *p = path;
  237. int offset = 0;
  238. CHECK_HEADER(fdt);
  239. /* Paths must be absolute */
  240. if (*path != '/')
  241. return -FDT_ERR_BADPATH;
  242. while (*p) {
  243. const char *q;
  244. /* Skip path separator(s) */
  245. while (*p == '/')
  246. p++;
  247. if (! *p)
  248. return -FDT_ERR_BADPATH;
  249. /*
  250. * Find the next path separator. The characters between
  251. * p and q are the next segment of the the path to find.
  252. */
  253. q = strchr(p, '/');
  254. if (! q)
  255. q = end;
  256. /*
  257. * Find the offset corresponding to the this path segment.
  258. */
  259. offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
  260. /* Oops, error, abort abort abort */
  261. if (offset < 0)
  262. return offset;
  263. p = q;
  264. }
  265. return offset;
  266. }
  267. /*
  268. * Given the offset of a node and a name of a property in that node, return
  269. * a pointer to the property struct.
  270. */
  271. struct fdt_property *fdt_get_property(const void *fdt,
  272. int nodeoffset,
  273. const char *name, int *lenp)
  274. {
  275. int level = 0;
  276. uint32_t tag;
  277. struct fdt_property *prop;
  278. int offset, nextoffset;
  279. int err;
  280. if ((err = fdt_check_header(fdt)) != 0)
  281. goto fail;
  282. err = -FDT_ERR_BADOFFSET;
  283. if (nodeoffset % FDT_TAGSIZE)
  284. goto fail;
  285. tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
  286. if (tag != FDT_BEGIN_NODE)
  287. goto fail;
  288. do {
  289. offset = nextoffset;
  290. tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
  291. switch (tag) {
  292. case FDT_END:
  293. err = -FDT_ERR_TRUNCATED;
  294. goto fail;
  295. case FDT_BEGIN_NODE:
  296. level++;
  297. break;
  298. case FDT_END_NODE:
  299. level--;
  300. break;
  301. case FDT_PROP:
  302. /*
  303. * If we are nested down levels, ignore the strings
  304. * until we get back to the proper level.
  305. */
  306. if (level != 0)
  307. continue;
  308. err = prop_name_eq(fdt, offset, name, &prop, lenp);
  309. if (err > 0)
  310. return prop;
  311. else if (err < 0)
  312. goto fail;
  313. break;
  314. case FDT_NOP:
  315. break;
  316. default:
  317. err = -FDT_ERR_BADSTRUCTURE;
  318. goto fail;
  319. }
  320. } while (level >= 0);
  321. err = -FDT_ERR_NOTFOUND;
  322. fail:
  323. if (lenp)
  324. *lenp = err;
  325. return NULL;
  326. }
  327. /*
  328. * Given the offset of a node and a name of a property in that node, return
  329. * a pointer to the property data (ONLY).
  330. */
  331. void *fdt_getprop(const void *fdt, int nodeoffset,
  332. const char *name, int *lenp)
  333. {
  334. const struct fdt_property *prop;
  335. prop = fdt_get_property(fdt, nodeoffset, name, lenp);
  336. if (! prop)
  337. return NULL;
  338. return (void *)prop->data;
  339. }
  340. uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset, char **namep)
  341. {
  342. const uint32_t *tagp, *lenp;
  343. uint32_t tag;
  344. const char *p;
  345. if (offset % FDT_TAGSIZE)
  346. return -1;
  347. tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
  348. if (! tagp)
  349. return FDT_END; /* premature end */
  350. tag = fdt32_to_cpu(*tagp);
  351. offset += FDT_TAGSIZE;
  352. switch (tag) {
  353. case FDT_BEGIN_NODE:
  354. if(namep)
  355. *namep = fdt_offset_ptr(fdt, offset, 1);
  356. /* skip name */
  357. do {
  358. p = fdt_offset_ptr(fdt, offset++, 1);
  359. } while (p && (*p != '\0'));
  360. if (! p)
  361. return FDT_END;
  362. break;
  363. case FDT_PROP:
  364. lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
  365. if (! lenp)
  366. return FDT_END;
  367. /*
  368. * Get the property and set the namep to the name.
  369. */
  370. if(namep) {
  371. struct fdt_property *prop;
  372. prop = fdt_offset_ptr_typed(fdt, offset - FDT_TAGSIZE, prop);
  373. if (! prop)
  374. return -FDT_ERR_BADSTRUCTURE;
  375. *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
  376. }
  377. /* skip name offset, length and value */
  378. offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
  379. break;
  380. }
  381. if (nextoffset)
  382. *nextoffset = ALIGN(offset, FDT_TAGSIZE);
  383. return tag;
  384. }
  385. /*
  386. * Return the number of used reserve map entries and total slots available.
  387. */
  388. int fdt_num_reservemap(void *fdt, int *used, int *total)
  389. {
  390. struct fdt_reserve_entry *re;
  391. int start;
  392. int end;
  393. int err = fdt_check_header(fdt);
  394. if (err != 0)
  395. return err;
  396. start = fdt_off_mem_rsvmap(fdt);
  397. /*
  398. * Convention is that the reserve map is before the dt_struct,
  399. * but it does not have to be.
  400. */
  401. end = fdt_totalsize(fdt);
  402. if (end > fdt_off_dt_struct(fdt))
  403. end = fdt_off_dt_struct(fdt);
  404. if (end > fdt_off_dt_strings(fdt))
  405. end = fdt_off_dt_strings(fdt);
  406. /*
  407. * Since the reserved area list is zero terminated, you get one fewer.
  408. */
  409. if (total)
  410. *total = ((end - start) / sizeof(struct fdt_reserve_entry)) - 1;
  411. if (used) {
  412. *used = 0;
  413. while (start < end) {
  414. re = (struct fdt_reserve_entry *)(fdt + start);
  415. if (re->size == 0)
  416. return 0; /* zero size terminates the list */
  417. *used += 1;
  418. start += sizeof(struct fdt_reserve_entry);
  419. }
  420. /*
  421. * If we get here, there was no zero size termination.
  422. */
  423. return -FDT_ERR_BADLAYOUT;
  424. }
  425. return 0;
  426. }
  427. /*
  428. * Return the nth reserve map entry.
  429. */
  430. int fdt_get_reservemap(void *fdt, int n, struct fdt_reserve_entry *re)
  431. {
  432. int used;
  433. int total;
  434. int err;
  435. err = fdt_num_reservemap(fdt, &used, &total);
  436. if (err != 0)
  437. return err;
  438. if (n >= total)
  439. return -FDT_ERR_NOSPACE;
  440. if (re) {
  441. *re = *(struct fdt_reserve_entry *)
  442. _fdt_offset_ptr(fdt, n * sizeof(struct fdt_reserve_entry));
  443. }
  444. return 0;
  445. }
  446. #endif /* CONFIG_OF_LIBFDT */