fdt.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
  2. /*
  3. * libfdt - Flat Device Tree manipulation
  4. * Copyright (C) 2006 David Gibson, IBM Corporation.
  5. */
  6. #include "libfdt_env.h"
  7. #include <fdt.h>
  8. #include <libfdt.h>
  9. #include "libfdt_internal.h"
  10. /*
  11. * Minimal sanity check for a read-only tree. fdt_ro_probe_() checks
  12. * that the given buffer contains what appears to be a flattened
  13. * device tree with sane information in its header.
  14. */
  15. int32_t fdt_ro_probe_(const void *fdt)
  16. {
  17. uint32_t totalsize = fdt_totalsize(fdt);
  18. if (can_assume(VALID_DTB))
  19. return totalsize;
  20. /* The device tree must be at an 8-byte aligned address */
  21. if ((uintptr_t)fdt & 7)
  22. return -FDT_ERR_ALIGNMENT;
  23. if (fdt_magic(fdt) == FDT_MAGIC) {
  24. /* Complete tree */
  25. if (!can_assume(LATEST)) {
  26. if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
  27. return -FDT_ERR_BADVERSION;
  28. if (fdt_last_comp_version(fdt) >
  29. FDT_LAST_SUPPORTED_VERSION)
  30. return -FDT_ERR_BADVERSION;
  31. }
  32. } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
  33. /* Unfinished sequential-write blob */
  34. if (!can_assume(VALID_INPUT) && fdt_size_dt_struct(fdt) == 0)
  35. return -FDT_ERR_BADSTATE;
  36. } else {
  37. return -FDT_ERR_BADMAGIC;
  38. }
  39. if (totalsize < INT32_MAX)
  40. return totalsize;
  41. else
  42. return -FDT_ERR_TRUNCATED;
  43. }
  44. static int check_off_(uint32_t hdrsize, uint32_t totalsize, uint32_t off)
  45. {
  46. return (off >= hdrsize) && (off <= totalsize);
  47. }
  48. static int check_block_(uint32_t hdrsize, uint32_t totalsize,
  49. uint32_t base, uint32_t size)
  50. {
  51. if (!check_off_(hdrsize, totalsize, base))
  52. return 0; /* block start out of bounds */
  53. if ((base + size) < base)
  54. return 0; /* overflow */
  55. if (!check_off_(hdrsize, totalsize, base + size))
  56. return 0; /* block end out of bounds */
  57. return 1;
  58. }
  59. size_t fdt_header_size_(uint32_t version)
  60. {
  61. if (version <= 1)
  62. return FDT_V1_SIZE;
  63. else if (version <= 2)
  64. return FDT_V2_SIZE;
  65. else if (version <= 3)
  66. return FDT_V3_SIZE;
  67. else if (version <= 16)
  68. return FDT_V16_SIZE;
  69. else
  70. return FDT_V17_SIZE;
  71. }
  72. size_t fdt_header_size(const void *fdt)
  73. {
  74. return can_assume(LATEST) ? FDT_V17_SIZE :
  75. fdt_header_size_(fdt_version(fdt));
  76. }
  77. int fdt_check_header(const void *fdt)
  78. {
  79. size_t hdrsize;
  80. /* The device tree must be at an 8-byte aligned address */
  81. if ((uintptr_t)fdt & 7)
  82. return -FDT_ERR_ALIGNMENT;
  83. if (fdt_magic(fdt) != FDT_MAGIC)
  84. return -FDT_ERR_BADMAGIC;
  85. if (!can_assume(LATEST)) {
  86. if ((fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
  87. || (fdt_last_comp_version(fdt) >
  88. FDT_LAST_SUPPORTED_VERSION))
  89. return -FDT_ERR_BADVERSION;
  90. if (fdt_version(fdt) < fdt_last_comp_version(fdt))
  91. return -FDT_ERR_BADVERSION;
  92. }
  93. hdrsize = fdt_header_size(fdt);
  94. if (!can_assume(VALID_DTB)) {
  95. if ((fdt_totalsize(fdt) < hdrsize)
  96. || (fdt_totalsize(fdt) > INT_MAX))
  97. return -FDT_ERR_TRUNCATED;
  98. /* Bounds check memrsv block */
  99. if (!check_off_(hdrsize, fdt_totalsize(fdt),
  100. fdt_off_mem_rsvmap(fdt)))
  101. return -FDT_ERR_TRUNCATED;
  102. /* Bounds check structure block */
  103. if (!can_assume(LATEST) && fdt_version(fdt) < 17) {
  104. if (!check_off_(hdrsize, fdt_totalsize(fdt),
  105. fdt_off_dt_struct(fdt)))
  106. return -FDT_ERR_TRUNCATED;
  107. } else {
  108. if (!check_block_(hdrsize, fdt_totalsize(fdt),
  109. fdt_off_dt_struct(fdt),
  110. fdt_size_dt_struct(fdt)))
  111. return -FDT_ERR_TRUNCATED;
  112. }
  113. /* Bounds check strings block */
  114. if (!check_block_(hdrsize, fdt_totalsize(fdt),
  115. fdt_off_dt_strings(fdt),
  116. fdt_size_dt_strings(fdt)))
  117. return -FDT_ERR_TRUNCATED;
  118. }
  119. return 0;
  120. }
  121. const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
  122. {
  123. unsigned int uoffset = offset;
  124. unsigned int absoffset = offset + fdt_off_dt_struct(fdt);
  125. if (offset < 0)
  126. return NULL;
  127. if (!can_assume(VALID_INPUT))
  128. if ((absoffset < uoffset)
  129. || ((absoffset + len) < absoffset)
  130. || (absoffset + len) > fdt_totalsize(fdt))
  131. return NULL;
  132. if (can_assume(LATEST) || fdt_version(fdt) >= 0x11)
  133. if (((uoffset + len) < uoffset)
  134. || ((offset + len) > fdt_size_dt_struct(fdt)))
  135. return NULL;
  136. return fdt_offset_ptr_(fdt, offset);
  137. }
  138. uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
  139. {
  140. const fdt32_t *tagp, *lenp;
  141. uint32_t tag, len, sum;
  142. int offset = startoffset;
  143. const char *p;
  144. *nextoffset = -FDT_ERR_TRUNCATED;
  145. tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
  146. if (!can_assume(VALID_DTB) && !tagp)
  147. return FDT_END; /* premature end */
  148. tag = fdt32_to_cpu(*tagp);
  149. offset += FDT_TAGSIZE;
  150. *nextoffset = -FDT_ERR_BADSTRUCTURE;
  151. switch (tag) {
  152. case FDT_BEGIN_NODE:
  153. /* skip name */
  154. do {
  155. p = fdt_offset_ptr(fdt, offset++, 1);
  156. } while (p && (*p != '\0'));
  157. if (!can_assume(VALID_DTB) && !p)
  158. return FDT_END; /* premature end */
  159. break;
  160. case FDT_PROP:
  161. lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
  162. if (!can_assume(VALID_DTB) && !lenp)
  163. return FDT_END; /* premature end */
  164. len = fdt32_to_cpu(*lenp);
  165. sum = len + offset;
  166. if (!can_assume(VALID_DTB) &&
  167. (INT_MAX <= sum || sum < (uint32_t) offset))
  168. return FDT_END; /* premature end */
  169. /* skip-name offset, length and value */
  170. offset += sizeof(struct fdt_property) - FDT_TAGSIZE + len;
  171. if (!can_assume(LATEST) &&
  172. fdt_version(fdt) < 0x10 && len >= 8 &&
  173. ((offset - len) % 8) != 0)
  174. offset += 4;
  175. break;
  176. case FDT_END:
  177. case FDT_END_NODE:
  178. case FDT_NOP:
  179. break;
  180. default:
  181. return FDT_END;
  182. }
  183. if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
  184. return FDT_END; /* premature end */
  185. *nextoffset = FDT_TAGALIGN(offset);
  186. return tag;
  187. }
  188. int fdt_check_node_offset_(const void *fdt, int offset)
  189. {
  190. if (!can_assume(VALID_INPUT)
  191. && ((offset < 0) || (offset % FDT_TAGSIZE)))
  192. return -FDT_ERR_BADOFFSET;
  193. if (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE)
  194. return -FDT_ERR_BADOFFSET;
  195. return offset;
  196. }
  197. int fdt_check_prop_offset_(const void *fdt, int offset)
  198. {
  199. if (!can_assume(VALID_INPUT)
  200. && ((offset < 0) || (offset % FDT_TAGSIZE)))
  201. return -FDT_ERR_BADOFFSET;
  202. if (fdt_next_tag(fdt, offset, &offset) != FDT_PROP)
  203. return -FDT_ERR_BADOFFSET;
  204. return offset;
  205. }
  206. int fdt_next_node(const void *fdt, int offset, int *depth)
  207. {
  208. int nextoffset = 0;
  209. uint32_t tag;
  210. if (offset >= 0)
  211. if ((nextoffset = fdt_check_node_offset_(fdt, offset)) < 0)
  212. return nextoffset;
  213. do {
  214. offset = nextoffset;
  215. tag = fdt_next_tag(fdt, offset, &nextoffset);
  216. switch (tag) {
  217. case FDT_PROP:
  218. case FDT_NOP:
  219. break;
  220. case FDT_BEGIN_NODE:
  221. if (depth)
  222. (*depth)++;
  223. break;
  224. case FDT_END_NODE:
  225. if (depth && ((--(*depth)) < 0))
  226. return nextoffset;
  227. break;
  228. case FDT_END:
  229. if ((nextoffset >= 0)
  230. || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
  231. return -FDT_ERR_NOTFOUND;
  232. else
  233. return nextoffset;
  234. }
  235. } while (tag != FDT_BEGIN_NODE);
  236. return offset;
  237. }
  238. int fdt_first_subnode(const void *fdt, int offset)
  239. {
  240. int depth = 0;
  241. offset = fdt_next_node(fdt, offset, &depth);
  242. if (offset < 0 || depth != 1)
  243. return -FDT_ERR_NOTFOUND;
  244. return offset;
  245. }
  246. int fdt_next_subnode(const void *fdt, int offset)
  247. {
  248. int depth = 1;
  249. /*
  250. * With respect to the parent, the depth of the next subnode will be
  251. * the same as the last.
  252. */
  253. do {
  254. offset = fdt_next_node(fdt, offset, &depth);
  255. if (offset < 0 || depth < 1)
  256. return -FDT_ERR_NOTFOUND;
  257. } while (depth > 1);
  258. return offset;
  259. }
  260. const char *fdt_find_string_(const char *strtab, int tabsize, const char *s)
  261. {
  262. int len = strlen(s) + 1;
  263. const char *last = strtab + tabsize - len;
  264. const char *p;
  265. for (p = strtab; p <= last; p++)
  266. if (memcmp(p, s, len) == 0)
  267. return p;
  268. return NULL;
  269. }
  270. int fdt_move(const void *fdt, void *buf, int bufsize)
  271. {
  272. if (!can_assume(VALID_INPUT) && bufsize < 0)
  273. return -FDT_ERR_NOSPACE;
  274. FDT_RO_PROBE(fdt);
  275. if (fdt_totalsize(fdt) > (unsigned int)bufsize)
  276. return -FDT_ERR_NOSPACE;
  277. memmove(buf, fdt, fdt_totalsize(fdt));
  278. return 0;
  279. }