fdt_rw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * libfdt - Flat Device Tree manipulation
  3. * Copyright (C) 2006 David Gibson, IBM Corporation.
  4. *
  5. * libfdt is dual licensed: you can use it either under the terms of
  6. * the GPL, or the BSD license, at your option.
  7. *
  8. * a) This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this library; if not, write to the Free
  20. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  21. * MA 02110-1301 USA
  22. *
  23. * Alternatively,
  24. *
  25. * b) Redistribution and use in source and binary forms, with or
  26. * without modification, are permitted provided that the following
  27. * conditions are met:
  28. *
  29. * 1. Redistributions of source code must retain the above
  30. * copyright notice, this list of conditions and the following
  31. * disclaimer.
  32. * 2. Redistributions in binary form must reproduce the above
  33. * copyright notice, this list of conditions and the following
  34. * disclaimer in the documentation and/or other materials
  35. * provided with the distribution.
  36. *
  37. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  38. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  39. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  40. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  42. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  47. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  48. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  49. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  50. */
  51. #include "libfdt_env.h"
  52. #include <fdt.h>
  53. #include <libfdt.h>
  54. #include "libfdt_internal.h"
  55. static int _fdt_blocks_misordered(const void *fdt,
  56. int mem_rsv_size, int struct_size)
  57. {
  58. return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8))
  59. || (fdt_off_dt_struct(fdt) <
  60. (fdt_off_mem_rsvmap(fdt) + mem_rsv_size))
  61. || (fdt_off_dt_strings(fdt) <
  62. (fdt_off_dt_struct(fdt) + struct_size))
  63. || (fdt_totalsize(fdt) <
  64. (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
  65. }
  66. static int _fdt_rw_check_header(void *fdt)
  67. {
  68. FDT_CHECK_HEADER(fdt);
  69. if (fdt_version(fdt) < 17)
  70. return -FDT_ERR_BADVERSION;
  71. if (_fdt_blocks_misordered(fdt, sizeof(struct fdt_reserve_entry),
  72. fdt_size_dt_struct(fdt)))
  73. return -FDT_ERR_BADLAYOUT;
  74. if (fdt_version(fdt) > 17)
  75. fdt_set_version(fdt, 17);
  76. return 0;
  77. }
  78. #define FDT_RW_CHECK_HEADER(fdt) \
  79. { \
  80. int err; \
  81. if ((err = _fdt_rw_check_header(fdt)) != 0) \
  82. return err; \
  83. }
  84. static inline int _fdt_data_size(void *fdt)
  85. {
  86. return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
  87. }
  88. static int _fdt_splice(void *fdt, void *splicepoint, int oldlen, int newlen)
  89. {
  90. char *p = splicepoint;
  91. char *end = (char *)fdt + _fdt_data_size(fdt);
  92. if (((p + oldlen) < p) || ((p + oldlen) > end))
  93. return -FDT_ERR_BADOFFSET;
  94. if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt)))
  95. return -FDT_ERR_NOSPACE;
  96. memmove(p + newlen, p + oldlen, end - p - oldlen);
  97. return 0;
  98. }
  99. static int _fdt_splice_mem_rsv(void *fdt, struct fdt_reserve_entry *p,
  100. int oldn, int newn)
  101. {
  102. int delta = (newn - oldn) * sizeof(*p);
  103. int err;
  104. err = _fdt_splice(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
  105. if (err)
  106. return err;
  107. fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
  108. fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
  109. return 0;
  110. }
  111. static int _fdt_splice_struct(void *fdt, void *p,
  112. int oldlen, int newlen)
  113. {
  114. int delta = newlen - oldlen;
  115. int err;
  116. if ((err = _fdt_splice(fdt, p, oldlen, newlen)))
  117. return err;
  118. fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
  119. fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
  120. return 0;
  121. }
  122. static int _fdt_splice_string(void *fdt, int newlen)
  123. {
  124. void *p = (char *)fdt
  125. + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
  126. int err;
  127. if ((err = _fdt_splice(fdt, p, 0, newlen)))
  128. return err;
  129. fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
  130. return 0;
  131. }
  132. static int _fdt_find_add_string(void *fdt, const char *s)
  133. {
  134. char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
  135. const char *p;
  136. char *new;
  137. int len = strlen(s) + 1;
  138. int err;
  139. p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s);
  140. if (p)
  141. /* found it */
  142. return (p - strtab);
  143. new = strtab + fdt_size_dt_strings(fdt);
  144. err = _fdt_splice_string(fdt, len);
  145. if (err)
  146. return err;
  147. memcpy(new, s, len);
  148. return (new - strtab);
  149. }
  150. int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
  151. {
  152. struct fdt_reserve_entry *re;
  153. int err;
  154. FDT_RW_CHECK_HEADER(fdt);
  155. re = _fdt_mem_rsv_w(fdt, fdt_num_mem_rsv(fdt));
  156. err = _fdt_splice_mem_rsv(fdt, re, 0, 1);
  157. if (err)
  158. return err;
  159. re->address = cpu_to_fdt64(address);
  160. re->size = cpu_to_fdt64(size);
  161. return 0;
  162. }
  163. int fdt_del_mem_rsv(void *fdt, int n)
  164. {
  165. struct fdt_reserve_entry *re = _fdt_mem_rsv_w(fdt, n);
  166. int err;
  167. FDT_RW_CHECK_HEADER(fdt);
  168. if (n >= fdt_num_mem_rsv(fdt))
  169. return -FDT_ERR_NOTFOUND;
  170. err = _fdt_splice_mem_rsv(fdt, re, 1, 0);
  171. if (err)
  172. return err;
  173. return 0;
  174. }
  175. static int _fdt_resize_property(void *fdt, int nodeoffset, const char *name,
  176. int len, struct fdt_property **prop)
  177. {
  178. int oldlen;
  179. int err;
  180. *prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
  181. if (! (*prop))
  182. return oldlen;
  183. if ((err = _fdt_splice_struct(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
  184. FDT_TAGALIGN(len))))
  185. return err;
  186. (*prop)->len = cpu_to_fdt32(len);
  187. return 0;
  188. }
  189. static int _fdt_add_property(void *fdt, int nodeoffset, const char *name,
  190. int len, struct fdt_property **prop)
  191. {
  192. int proplen;
  193. int nextoffset;
  194. int namestroff;
  195. int err;
  196. if ((nextoffset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
  197. return nextoffset;
  198. namestroff = _fdt_find_add_string(fdt, name);
  199. if (namestroff < 0)
  200. return namestroff;
  201. *prop = _fdt_offset_ptr_w(fdt, nextoffset);
  202. proplen = sizeof(**prop) + FDT_TAGALIGN(len);
  203. err = _fdt_splice_struct(fdt, *prop, 0, proplen);
  204. if (err)
  205. return err;
  206. (*prop)->tag = cpu_to_fdt32(FDT_PROP);
  207. (*prop)->nameoff = cpu_to_fdt32(namestroff);
  208. (*prop)->len = cpu_to_fdt32(len);
  209. return 0;
  210. }
  211. int fdt_set_name(void *fdt, int nodeoffset, const char *name)
  212. {
  213. char *namep;
  214. int oldlen, newlen;
  215. int err;
  216. FDT_RW_CHECK_HEADER(fdt);
  217. namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
  218. if (!namep)
  219. return oldlen;
  220. newlen = strlen(name);
  221. err = _fdt_splice_struct(fdt, namep, FDT_TAGALIGN(oldlen+1),
  222. FDT_TAGALIGN(newlen+1));
  223. if (err)
  224. return err;
  225. memcpy(namep, name, newlen+1);
  226. return 0;
  227. }
  228. int fdt_setprop(void *fdt, int nodeoffset, const char *name,
  229. const void *val, int len)
  230. {
  231. struct fdt_property *prop;
  232. int err;
  233. FDT_RW_CHECK_HEADER(fdt);
  234. err = _fdt_resize_property(fdt, nodeoffset, name, len, &prop);
  235. if (err == -FDT_ERR_NOTFOUND)
  236. err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
  237. if (err)
  238. return err;
  239. memcpy(prop->data, val, len);
  240. return 0;
  241. }
  242. int fdt_delprop(void *fdt, int nodeoffset, const char *name)
  243. {
  244. struct fdt_property *prop;
  245. int len, proplen;
  246. FDT_RW_CHECK_HEADER(fdt);
  247. prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
  248. if (! prop)
  249. return len;
  250. proplen = sizeof(*prop) + FDT_TAGALIGN(len);
  251. return _fdt_splice_struct(fdt, prop, proplen, 0);
  252. }
  253. int fdt_add_subnode_namelen(void *fdt, int parentoffset,
  254. const char *name, int namelen)
  255. {
  256. struct fdt_node_header *nh;
  257. int offset, nextoffset;
  258. int nodelen;
  259. int err;
  260. uint32_t tag;
  261. uint32_t *endtag;
  262. FDT_RW_CHECK_HEADER(fdt);
  263. offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
  264. if (offset >= 0)
  265. return -FDT_ERR_EXISTS;
  266. else if (offset != -FDT_ERR_NOTFOUND)
  267. return offset;
  268. /* Try to place the new node after the parent's properties */
  269. fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
  270. do {
  271. offset = nextoffset;
  272. tag = fdt_next_tag(fdt, offset, &nextoffset);
  273. } while ((tag == FDT_PROP) || (tag == FDT_NOP));
  274. nh = _fdt_offset_ptr_w(fdt, offset);
  275. nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
  276. err = _fdt_splice_struct(fdt, nh, 0, nodelen);
  277. if (err)
  278. return err;
  279. nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
  280. memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
  281. memcpy(nh->name, name, namelen);
  282. endtag = (uint32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
  283. *endtag = cpu_to_fdt32(FDT_END_NODE);
  284. return offset;
  285. }
  286. int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
  287. {
  288. return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
  289. }
  290. int fdt_del_node(void *fdt, int nodeoffset)
  291. {
  292. int endoffset;
  293. FDT_RW_CHECK_HEADER(fdt);
  294. endoffset = _fdt_node_end_offset(fdt, nodeoffset);
  295. if (endoffset < 0)
  296. return endoffset;
  297. return _fdt_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset),
  298. endoffset - nodeoffset, 0);
  299. }
  300. static void _fdt_packblocks(const char *old, char *new,
  301. int mem_rsv_size, int struct_size)
  302. {
  303. int mem_rsv_off, struct_off, strings_off;
  304. mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
  305. struct_off = mem_rsv_off + mem_rsv_size;
  306. strings_off = struct_off + struct_size;
  307. memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
  308. fdt_set_off_mem_rsvmap(new, mem_rsv_off);
  309. memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
  310. fdt_set_off_dt_struct(new, struct_off);
  311. fdt_set_size_dt_struct(new, struct_size);
  312. memmove(new + strings_off, old + fdt_off_dt_strings(old),
  313. fdt_size_dt_strings(old));
  314. fdt_set_off_dt_strings(new, strings_off);
  315. fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
  316. }
  317. int fdt_open_into(const void *fdt, void *buf, int bufsize)
  318. {
  319. int err;
  320. int mem_rsv_size, struct_size;
  321. int newsize;
  322. const char *fdtstart = fdt;
  323. const char *fdtend = fdtstart + fdt_totalsize(fdt);
  324. char *tmp;
  325. FDT_CHECK_HEADER(fdt);
  326. mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
  327. * sizeof(struct fdt_reserve_entry);
  328. if (fdt_version(fdt) >= 17) {
  329. struct_size = fdt_size_dt_struct(fdt);
  330. } else {
  331. struct_size = 0;
  332. while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
  333. ;
  334. }
  335. if (!_fdt_blocks_misordered(fdt, mem_rsv_size, struct_size)) {
  336. /* no further work necessary */
  337. err = fdt_move(fdt, buf, bufsize);
  338. if (err)
  339. return err;
  340. fdt_set_version(buf, 17);
  341. fdt_set_size_dt_struct(buf, struct_size);
  342. fdt_set_totalsize(buf, bufsize);
  343. return 0;
  344. }
  345. /* Need to reorder */
  346. newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
  347. + struct_size + fdt_size_dt_strings(fdt);
  348. if (bufsize < newsize)
  349. return -FDT_ERR_NOSPACE;
  350. /* First attempt to build converted tree at beginning of buffer */
  351. tmp = buf;
  352. /* But if that overlaps with the old tree... */
  353. if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
  354. /* Try right after the old tree instead */
  355. tmp = (char *)(uintptr_t)fdtend;
  356. if ((tmp + newsize) > ((char *)buf + bufsize))
  357. return -FDT_ERR_NOSPACE;
  358. }
  359. _fdt_packblocks(fdt, tmp, mem_rsv_size, struct_size);
  360. memmove(buf, tmp, newsize);
  361. fdt_set_magic(buf, FDT_MAGIC);
  362. fdt_set_totalsize(buf, bufsize);
  363. fdt_set_version(buf, 17);
  364. fdt_set_last_comp_version(buf, 16);
  365. fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
  366. return 0;
  367. }
  368. int fdt_pack(void *fdt)
  369. {
  370. int mem_rsv_size;
  371. FDT_RW_CHECK_HEADER(fdt);
  372. mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
  373. * sizeof(struct fdt_reserve_entry);
  374. _fdt_packblocks(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt));
  375. fdt_set_totalsize(fdt, _fdt_data_size(fdt));
  376. return 0;
  377. }