fdt_sw.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. static int fdt_sw_probe_(void *fdt)
  11. {
  12. if (!can_assume(VALID_INPUT)) {
  13. if (fdt_magic(fdt) == FDT_MAGIC)
  14. return -FDT_ERR_BADSTATE;
  15. else if (fdt_magic(fdt) != FDT_SW_MAGIC)
  16. return -FDT_ERR_BADMAGIC;
  17. }
  18. return 0;
  19. }
  20. #define FDT_SW_PROBE(fdt) \
  21. { \
  22. int err; \
  23. if ((err = fdt_sw_probe_(fdt)) != 0) \
  24. return err; \
  25. }
  26. /* 'memrsv' state: Initial state after fdt_create()
  27. *
  28. * Allowed functions:
  29. * fdt_add_reservemap_entry()
  30. * fdt_finish_reservemap() [moves to 'struct' state]
  31. */
  32. static int fdt_sw_probe_memrsv_(void *fdt)
  33. {
  34. int err = fdt_sw_probe_(fdt);
  35. if (err)
  36. return err;
  37. if (!can_assume(VALID_INPUT) && fdt_off_dt_strings(fdt) != 0)
  38. return -FDT_ERR_BADSTATE;
  39. return 0;
  40. }
  41. #define FDT_SW_PROBE_MEMRSV(fdt) \
  42. { \
  43. int err; \
  44. if ((err = fdt_sw_probe_memrsv_(fdt)) != 0) \
  45. return err; \
  46. }
  47. /* 'struct' state: Enter this state after fdt_finish_reservemap()
  48. *
  49. * Allowed functions:
  50. * fdt_begin_node()
  51. * fdt_end_node()
  52. * fdt_property*()
  53. * fdt_finish() [moves to 'complete' state]
  54. */
  55. static int fdt_sw_probe_struct_(void *fdt)
  56. {
  57. int err = fdt_sw_probe_(fdt);
  58. if (err)
  59. return err;
  60. if (!can_assume(VALID_INPUT) &&
  61. fdt_off_dt_strings(fdt) != fdt_totalsize(fdt))
  62. return -FDT_ERR_BADSTATE;
  63. return 0;
  64. }
  65. #define FDT_SW_PROBE_STRUCT(fdt) \
  66. { \
  67. int err; \
  68. if ((err = fdt_sw_probe_struct_(fdt)) != 0) \
  69. return err; \
  70. }
  71. static inline uint32_t sw_flags(void *fdt)
  72. {
  73. /* assert: (fdt_magic(fdt) == FDT_SW_MAGIC) */
  74. return fdt_last_comp_version(fdt);
  75. }
  76. /* 'complete' state: Enter this state after fdt_finish()
  77. *
  78. * Allowed functions: none
  79. */
  80. static void *fdt_grab_space_(void *fdt, size_t len)
  81. {
  82. unsigned int offset = fdt_size_dt_struct(fdt);
  83. unsigned int spaceleft;
  84. spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
  85. - fdt_size_dt_strings(fdt);
  86. if ((offset + len < offset) || (offset + len > spaceleft))
  87. return NULL;
  88. fdt_set_size_dt_struct(fdt, offset + len);
  89. return fdt_offset_ptr_w_(fdt, offset);
  90. }
  91. int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags)
  92. {
  93. const int hdrsize = FDT_ALIGN(sizeof(struct fdt_header),
  94. sizeof(struct fdt_reserve_entry));
  95. void *fdt = buf;
  96. if (bufsize < hdrsize)
  97. return -FDT_ERR_NOSPACE;
  98. if (flags & ~FDT_CREATE_FLAGS_ALL)
  99. return -FDT_ERR_BADFLAGS;
  100. memset(buf, 0, bufsize);
  101. /*
  102. * magic and last_comp_version keep intermediate state during the fdt
  103. * creation process, which is replaced with the proper FDT format by
  104. * fdt_finish().
  105. *
  106. * flags should be accessed with sw_flags().
  107. */
  108. fdt_set_magic(fdt, FDT_SW_MAGIC);
  109. fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
  110. fdt_set_last_comp_version(fdt, flags);
  111. fdt_set_totalsize(fdt, bufsize);
  112. fdt_set_off_mem_rsvmap(fdt, hdrsize);
  113. fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
  114. fdt_set_off_dt_strings(fdt, 0);
  115. return 0;
  116. }
  117. int fdt_create(void *buf, int bufsize)
  118. {
  119. return fdt_create_with_flags(buf, bufsize, 0);
  120. }
  121. int fdt_resize(void *fdt, void *buf, int bufsize)
  122. {
  123. size_t headsize, tailsize;
  124. char *oldtail, *newtail;
  125. FDT_SW_PROBE(fdt);
  126. if (bufsize < 0)
  127. return -FDT_ERR_NOSPACE;
  128. headsize = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
  129. tailsize = fdt_size_dt_strings(fdt);
  130. if (!can_assume(VALID_DTB) &&
  131. headsize + tailsize > fdt_totalsize(fdt))
  132. return -FDT_ERR_INTERNAL;
  133. if ((headsize + tailsize) > (unsigned)bufsize)
  134. return -FDT_ERR_NOSPACE;
  135. oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize;
  136. newtail = (char *)buf + bufsize - tailsize;
  137. /* Two cases to avoid clobbering data if the old and new
  138. * buffers partially overlap */
  139. if (buf <= fdt) {
  140. memmove(buf, fdt, headsize);
  141. memmove(newtail, oldtail, tailsize);
  142. } else {
  143. memmove(newtail, oldtail, tailsize);
  144. memmove(buf, fdt, headsize);
  145. }
  146. fdt_set_totalsize(buf, bufsize);
  147. if (fdt_off_dt_strings(buf))
  148. fdt_set_off_dt_strings(buf, bufsize);
  149. return 0;
  150. }
  151. int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
  152. {
  153. struct fdt_reserve_entry *re;
  154. int offset;
  155. FDT_SW_PROBE_MEMRSV(fdt);
  156. offset = fdt_off_dt_struct(fdt);
  157. if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
  158. return -FDT_ERR_NOSPACE;
  159. re = (struct fdt_reserve_entry *)((char *)fdt + offset);
  160. re->address = cpu_to_fdt64(addr);
  161. re->size = cpu_to_fdt64(size);
  162. fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
  163. return 0;
  164. }
  165. int fdt_finish_reservemap(void *fdt)
  166. {
  167. int err = fdt_add_reservemap_entry(fdt, 0, 0);
  168. if (err)
  169. return err;
  170. fdt_set_off_dt_strings(fdt, fdt_totalsize(fdt));
  171. return 0;
  172. }
  173. int fdt_begin_node(void *fdt, const char *name)
  174. {
  175. struct fdt_node_header *nh;
  176. int namelen;
  177. FDT_SW_PROBE_STRUCT(fdt);
  178. namelen = strlen(name) + 1;
  179. nh = fdt_grab_space_(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
  180. if (! nh)
  181. return -FDT_ERR_NOSPACE;
  182. nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
  183. memcpy(nh->name, name, namelen);
  184. return 0;
  185. }
  186. int fdt_end_node(void *fdt)
  187. {
  188. fdt32_t *en;
  189. FDT_SW_PROBE_STRUCT(fdt);
  190. en = fdt_grab_space_(fdt, FDT_TAGSIZE);
  191. if (! en)
  192. return -FDT_ERR_NOSPACE;
  193. *en = cpu_to_fdt32(FDT_END_NODE);
  194. return 0;
  195. }
  196. static int fdt_add_string_(void *fdt, const char *s)
  197. {
  198. char *strtab = (char *)fdt + fdt_totalsize(fdt);
  199. unsigned int strtabsize = fdt_size_dt_strings(fdt);
  200. unsigned int len = strlen(s) + 1;
  201. unsigned int struct_top, offset;
  202. offset = strtabsize + len;
  203. struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
  204. if (fdt_totalsize(fdt) - offset < struct_top)
  205. return 0; /* no more room :( */
  206. memcpy(strtab - offset, s, len);
  207. fdt_set_size_dt_strings(fdt, strtabsize + len);
  208. return -offset;
  209. }
  210. /* Must only be used to roll back in case of error */
  211. static void fdt_del_last_string_(void *fdt, const char *s)
  212. {
  213. int strtabsize = fdt_size_dt_strings(fdt);
  214. int len = strlen(s) + 1;
  215. fdt_set_size_dt_strings(fdt, strtabsize - len);
  216. }
  217. static int fdt_find_add_string_(void *fdt, const char *s, int *allocated)
  218. {
  219. char *strtab = (char *)fdt + fdt_totalsize(fdt);
  220. int strtabsize = fdt_size_dt_strings(fdt);
  221. const char *p;
  222. *allocated = 0;
  223. p = fdt_find_string_(strtab - strtabsize, strtabsize, s);
  224. if (p)
  225. return p - strtab;
  226. *allocated = 1;
  227. return fdt_add_string_(fdt, s);
  228. }
  229. int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp)
  230. {
  231. struct fdt_property *prop;
  232. int nameoff;
  233. int allocated;
  234. FDT_SW_PROBE_STRUCT(fdt);
  235. /* String de-duplication can be slow, _NO_NAME_DEDUP skips it */
  236. if (sw_flags(fdt) & FDT_CREATE_FLAG_NO_NAME_DEDUP) {
  237. allocated = 1;
  238. nameoff = fdt_add_string_(fdt, name);
  239. } else {
  240. nameoff = fdt_find_add_string_(fdt, name, &allocated);
  241. }
  242. if (nameoff == 0)
  243. return -FDT_ERR_NOSPACE;
  244. prop = fdt_grab_space_(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
  245. if (! prop) {
  246. if (allocated)
  247. fdt_del_last_string_(fdt, name);
  248. return -FDT_ERR_NOSPACE;
  249. }
  250. prop->tag = cpu_to_fdt32(FDT_PROP);
  251. prop->nameoff = cpu_to_fdt32(nameoff);
  252. prop->len = cpu_to_fdt32(len);
  253. *valp = prop->data;
  254. return 0;
  255. }
  256. int fdt_property(void *fdt, const char *name, const void *val, int len)
  257. {
  258. void *ptr;
  259. int ret;
  260. ret = fdt_property_placeholder(fdt, name, len, &ptr);
  261. if (ret)
  262. return ret;
  263. memcpy(ptr, val, len);
  264. return 0;
  265. }
  266. int fdt_finish(void *fdt)
  267. {
  268. char *p = (char *)fdt;
  269. fdt32_t *end;
  270. int oldstroffset, newstroffset;
  271. uint32_t tag;
  272. int offset, nextoffset;
  273. FDT_SW_PROBE_STRUCT(fdt);
  274. /* Add terminator */
  275. end = fdt_grab_space_(fdt, sizeof(*end));
  276. if (! end)
  277. return -FDT_ERR_NOSPACE;
  278. *end = cpu_to_fdt32(FDT_END);
  279. /* Relocate the string table */
  280. oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
  281. newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
  282. memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
  283. fdt_set_off_dt_strings(fdt, newstroffset);
  284. /* Walk the structure, correcting string offsets */
  285. offset = 0;
  286. while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
  287. if (tag == FDT_PROP) {
  288. struct fdt_property *prop =
  289. fdt_offset_ptr_w_(fdt, offset);
  290. int nameoff;
  291. nameoff = fdt32_to_cpu(prop->nameoff);
  292. nameoff += fdt_size_dt_strings(fdt);
  293. prop->nameoff = cpu_to_fdt32(nameoff);
  294. }
  295. offset = nextoffset;
  296. }
  297. if (nextoffset < 0)
  298. return nextoffset;
  299. /* Finally, adjust the header */
  300. fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
  301. /* And fix up fields that were keeping intermediate state. */
  302. fdt_set_last_comp_version(fdt, FDT_LAST_COMPATIBLE_VERSION);
  303. fdt_set_magic(fdt, FDT_MAGIC);
  304. return 0;
  305. }