fdt_ro.c 12 KB

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