fdt_ro.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. * Return the node offset of the node specified by:
  74. * parentoffset - starting place (0 to start at the root)
  75. * name - name being searched for
  76. * namelen - length of the name: typically strlen(name)
  77. *
  78. * Notes:
  79. * If the start node has subnodes, the subnodes are _not_ searched for the
  80. * requested name.
  81. */
  82. int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
  83. const char *name, int namelen)
  84. {
  85. int level = 0;
  86. uint32_t tag;
  87. int offset, nextoffset;
  88. CHECK_HEADER(fdt);
  89. tag = fdt_next_tag(fdt, parentoffset, &nextoffset, NULL);
  90. if (tag != FDT_BEGIN_NODE)
  91. return -FDT_ERR_BADOFFSET;
  92. do {
  93. offset = nextoffset;
  94. tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
  95. switch (tag) {
  96. case FDT_END:
  97. return -FDT_ERR_TRUNCATED;
  98. case FDT_BEGIN_NODE:
  99. level++;
  100. /*
  101. * If we are nested down levels, ignore the strings
  102. * until we get back to the proper level.
  103. */
  104. if (level != 1)
  105. continue;
  106. /* Return the offset if this is "our" string. */
  107. if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen))
  108. return offset;
  109. break;
  110. case FDT_END_NODE:
  111. level--;
  112. break;
  113. case FDT_PROP:
  114. case FDT_NOP:
  115. break;
  116. default:
  117. return -FDT_ERR_BADSTRUCTURE;
  118. }
  119. } while (level >= 0);
  120. return -FDT_ERR_NOTFOUND;
  121. }
  122. /*
  123. * See fdt_subnode_offset_namelen()
  124. */
  125. int fdt_subnode_offset(const void *fdt, int parentoffset,
  126. const char *name)
  127. {
  128. return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
  129. }
  130. /*
  131. * Searches for the node corresponding to the given path and returns the
  132. * offset of that node.
  133. */
  134. int fdt_find_node_by_path(const void *fdt, const char *path)
  135. {
  136. const char *end = path + strlen(path);
  137. const char *p = path;
  138. int offset = 0;
  139. CHECK_HEADER(fdt);
  140. /* Paths must be absolute */
  141. if (*path != '/')
  142. return -FDT_ERR_BADPATH;
  143. /* Handle the root path: root offset is 0 */
  144. if (strcmp(path, "/") == 0)
  145. return 0;
  146. while (*p) {
  147. const char *q;
  148. /* Skip path separator(s) */
  149. while (*p == '/')
  150. p++;
  151. if (! *p)
  152. return -FDT_ERR_BADPATH;
  153. /*
  154. * Find the next path separator. The characters between
  155. * p and q are the next segment of the the path to find.
  156. */
  157. q = strchr(p, '/');
  158. if (! q)
  159. q = end;
  160. /*
  161. * Find the offset corresponding to the this path segment.
  162. */
  163. offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
  164. /* Oops, error, abort abort abort */
  165. if (offset < 0)
  166. return offset;
  167. p = q;
  168. }
  169. return offset;
  170. }
  171. /*
  172. * Given the offset of a node and a name of a property in that node, return
  173. * a pointer to the property struct.
  174. */
  175. struct fdt_property *fdt_get_property(const void *fdt,
  176. int nodeoffset,
  177. const char *name, int *lenp)
  178. {
  179. int level = 0;
  180. uint32_t tag;
  181. struct fdt_property *prop;
  182. int offset, nextoffset;
  183. int err;
  184. if ((err = fdt_check_header(fdt)) != 0)
  185. goto fail;
  186. err = -FDT_ERR_BADOFFSET;
  187. if (nodeoffset % FDT_TAGSIZE)
  188. goto fail;
  189. tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
  190. if (tag != FDT_BEGIN_NODE)
  191. goto fail;
  192. do {
  193. offset = nextoffset;
  194. tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
  195. switch (tag) {
  196. case FDT_END:
  197. err = -FDT_ERR_TRUNCATED;
  198. goto fail;
  199. case FDT_BEGIN_NODE:
  200. level++;
  201. break;
  202. case FDT_END_NODE:
  203. level--;
  204. break;
  205. case FDT_PROP:
  206. /*
  207. * If we are nested down levels, ignore the strings
  208. * until we get back to the proper level.
  209. */
  210. if (level != 0)
  211. continue;
  212. err = prop_name_eq(fdt, offset, name, &prop, lenp);
  213. if (err > 0)
  214. return prop;
  215. else if (err < 0)
  216. goto fail;
  217. break;
  218. case FDT_NOP:
  219. break;
  220. default:
  221. err = -FDT_ERR_BADSTRUCTURE;
  222. goto fail;
  223. }
  224. } while (level >= 0);
  225. err = -FDT_ERR_NOTFOUND;
  226. fail:
  227. if (lenp)
  228. *lenp = err;
  229. return NULL;
  230. }
  231. /*
  232. * Given the offset of a node and a name of a property in that node, return
  233. * a pointer to the property data (ONLY).
  234. */
  235. void *fdt_getprop(const void *fdt, int nodeoffset,
  236. const char *name, int *lenp)
  237. {
  238. const struct fdt_property *prop;
  239. prop = fdt_get_property(fdt, nodeoffset, name, lenp);
  240. if (! prop)
  241. return NULL;
  242. return (void *)prop->data;
  243. }
  244. uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset, char **namep)
  245. {
  246. const uint32_t *tagp, *lenp;
  247. uint32_t tag;
  248. const char *p;
  249. if (offset % FDT_TAGSIZE)
  250. return -1;
  251. tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
  252. if (! tagp)
  253. return FDT_END; /* premature end */
  254. tag = fdt32_to_cpu(*tagp);
  255. offset += FDT_TAGSIZE;
  256. switch (tag) {
  257. case FDT_BEGIN_NODE:
  258. if(namep)
  259. *namep = fdt_offset_ptr(fdt, offset, 1);
  260. /* skip name */
  261. do {
  262. p = fdt_offset_ptr(fdt, offset++, 1);
  263. } while (p && (*p != '\0'));
  264. if (! p)
  265. return FDT_END;
  266. break;
  267. case FDT_PROP:
  268. lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
  269. if (! lenp)
  270. return FDT_END;
  271. /*
  272. * Get the property and set the namep to the name.
  273. */
  274. if(namep) {
  275. struct fdt_property *prop;
  276. prop = fdt_offset_ptr_typed(fdt, offset - FDT_TAGSIZE, prop);
  277. if (! prop)
  278. return -FDT_ERR_BADSTRUCTURE;
  279. *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
  280. }
  281. /* skip name offset, length and value */
  282. offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
  283. break;
  284. }
  285. if (nextoffset)
  286. *nextoffset = ALIGN(offset, FDT_TAGSIZE);
  287. return tag;
  288. }
  289. /*
  290. * Return the number of used reserve map entries and total slots available.
  291. */
  292. int fdt_num_reservemap(void *fdt, int *used, int *total)
  293. {
  294. struct fdt_reserve_entry *re;
  295. int start;
  296. int end;
  297. int err = fdt_check_header(fdt);
  298. if (err != 0)
  299. return err;
  300. start = fdt_off_mem_rsvmap(fdt);
  301. /*
  302. * Convention is that the reserve map is before the dt_struct,
  303. * but it does not have to be.
  304. */
  305. end = fdt_totalsize(fdt);
  306. if (end > fdt_off_dt_struct(fdt))
  307. end = fdt_off_dt_struct(fdt);
  308. if (end > fdt_off_dt_strings(fdt))
  309. end = fdt_off_dt_strings(fdt);
  310. /*
  311. * Since the reserved area list is zero terminated, you get one fewer.
  312. */
  313. if (total)
  314. *total = ((end - start) / sizeof(struct fdt_reserve_entry)) - 1;
  315. if (used) {
  316. *used = 0;
  317. while (start < end) {
  318. re = (struct fdt_reserve_entry *)(fdt + start);
  319. if (re->size == 0)
  320. return 0; /* zero size terminates the list */
  321. *used += 1;
  322. start += sizeof(struct fdt_reserve_entry);
  323. }
  324. /*
  325. * If we get here, there was no zero size termination.
  326. */
  327. return -FDT_ERR_BADLAYOUT;
  328. }
  329. return 0;
  330. }
  331. /*
  332. * Return the nth reserve map entry.
  333. */
  334. int fdt_get_reservemap(void *fdt, int n, struct fdt_reserve_entry *re)
  335. {
  336. int used;
  337. int total;
  338. int err;
  339. err = fdt_num_reservemap(fdt, &used, &total);
  340. if (err != 0)
  341. return err;
  342. if (n >= total)
  343. return -FDT_ERR_NOSPACE;
  344. if (re) {
  345. *re = *(struct fdt_reserve_entry *)
  346. _fdt_offset_ptr(fdt, n * sizeof(struct fdt_reserve_entry));
  347. }
  348. return 0;
  349. }