fdt_ro.c 8.3 KB

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