fdt_rw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 ((p < (char *)fdt) || ((end - oldlen + newlen) < (char *)fdt))
  95. return -FDT_ERR_BADOFFSET;
  96. if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt)))
  97. return -FDT_ERR_NOSPACE;
  98. memmove(p + newlen, p + oldlen, end - p - oldlen);
  99. return 0;
  100. }
  101. static int _fdt_splice_mem_rsv(void *fdt, struct fdt_reserve_entry *p,
  102. int oldn, int newn)
  103. {
  104. int delta = (newn - oldn) * sizeof(*p);
  105. int err;
  106. err = _fdt_splice(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
  107. if (err)
  108. return err;
  109. fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
  110. fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
  111. return 0;
  112. }
  113. static int _fdt_splice_struct(void *fdt, void *p,
  114. int oldlen, int newlen)
  115. {
  116. int delta = newlen - oldlen;
  117. int err;
  118. if ((err = _fdt_splice(fdt, p, oldlen, newlen)))
  119. return err;
  120. fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
  121. fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
  122. return 0;
  123. }
  124. static int _fdt_splice_string(void *fdt, int newlen)
  125. {
  126. void *p = (char *)fdt
  127. + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
  128. int err;
  129. if ((err = _fdt_splice(fdt, p, 0, newlen)))
  130. return err;
  131. fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
  132. return 0;
  133. }
  134. static int _fdt_find_add_string(void *fdt, const char *s)
  135. {
  136. char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
  137. const char *p;
  138. char *new;
  139. int len = strlen(s) + 1;
  140. int err;
  141. p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s);
  142. if (p)
  143. /* found it */
  144. return (p - strtab);
  145. new = strtab + fdt_size_dt_strings(fdt);
  146. err = _fdt_splice_string(fdt, len);
  147. if (err)
  148. return err;
  149. memcpy(new, s, len);
  150. return (new - strtab);
  151. }
  152. int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
  153. {
  154. struct fdt_reserve_entry *re;
  155. int err;
  156. FDT_RW_CHECK_HEADER(fdt);
  157. re = _fdt_mem_rsv_w(fdt, fdt_num_mem_rsv(fdt));
  158. err = _fdt_splice_mem_rsv(fdt, re, 0, 1);
  159. if (err)
  160. return err;
  161. re->address = cpu_to_fdt64(address);
  162. re->size = cpu_to_fdt64(size);
  163. return 0;
  164. }
  165. int fdt_del_mem_rsv(void *fdt, int n)
  166. {
  167. struct fdt_reserve_entry *re = _fdt_mem_rsv_w(fdt, n);
  168. int err;
  169. FDT_RW_CHECK_HEADER(fdt);
  170. if (n >= fdt_num_mem_rsv(fdt))
  171. return -FDT_ERR_NOTFOUND;
  172. err = _fdt_splice_mem_rsv(fdt, re, 1, 0);
  173. if (err)
  174. return err;
  175. return 0;
  176. }
  177. static int _fdt_resize_property(void *fdt, int nodeoffset, const char *name,
  178. int len, struct fdt_property **prop)
  179. {
  180. int oldlen;
  181. int err;
  182. *prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
  183. if (! (*prop))
  184. return oldlen;
  185. if ((err = _fdt_splice_struct(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
  186. FDT_TAGALIGN(len))))
  187. return err;
  188. (*prop)->len = cpu_to_fdt32(len);
  189. return 0;
  190. }
  191. static int _fdt_add_property(void *fdt, int nodeoffset, const char *name,
  192. int len, struct fdt_property **prop)
  193. {
  194. int proplen;
  195. int nextoffset;
  196. int namestroff;
  197. int err;
  198. if ((nextoffset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
  199. return nextoffset;
  200. namestroff = _fdt_find_add_string(fdt, name);
  201. if (namestroff < 0)
  202. return namestroff;
  203. *prop = _fdt_offset_ptr_w(fdt, nextoffset);
  204. proplen = sizeof(**prop) + FDT_TAGALIGN(len);
  205. err = _fdt_splice_struct(fdt, *prop, 0, proplen);
  206. if (err)
  207. return err;
  208. (*prop)->tag = cpu_to_fdt32(FDT_PROP);
  209. (*prop)->nameoff = cpu_to_fdt32(namestroff);
  210. (*prop)->len = cpu_to_fdt32(len);
  211. return 0;
  212. }
  213. int fdt_set_name(void *fdt, int nodeoffset, const char *name)
  214. {
  215. char *namep;
  216. int oldlen, newlen;
  217. int err;
  218. FDT_RW_CHECK_HEADER(fdt);
  219. namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
  220. if (!namep)
  221. return oldlen;
  222. newlen = strlen(name);
  223. err = _fdt_splice_struct(fdt, namep, FDT_TAGALIGN(oldlen+1),
  224. FDT_TAGALIGN(newlen+1));
  225. if (err)
  226. return err;
  227. memcpy(namep, name, newlen+1);
  228. return 0;
  229. }
  230. int fdt_setprop(void *fdt, int nodeoffset, const char *name,
  231. const void *val, int len)
  232. {
  233. struct fdt_property *prop;
  234. int err;
  235. FDT_RW_CHECK_HEADER(fdt);
  236. err = _fdt_resize_property(fdt, nodeoffset, name, len, &prop);
  237. if (err == -FDT_ERR_NOTFOUND)
  238. err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
  239. if (err)
  240. return err;
  241. memcpy(prop->data, val, len);
  242. return 0;
  243. }
  244. int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
  245. const void *val, int len)
  246. {
  247. struct fdt_property *prop;
  248. int err, oldlen, newlen;
  249. FDT_RW_CHECK_HEADER(fdt);
  250. prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
  251. if (prop) {
  252. newlen = len + oldlen;
  253. err = _fdt_splice_struct(fdt, prop->data,
  254. FDT_TAGALIGN(oldlen),
  255. FDT_TAGALIGN(newlen));
  256. if (err)
  257. return err;
  258. prop->len = cpu_to_fdt32(newlen);
  259. memcpy(prop->data + oldlen, val, len);
  260. } else {
  261. err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
  262. if (err)
  263. return err;
  264. memcpy(prop->data, val, len);
  265. }
  266. return 0;
  267. }
  268. int fdt_delprop(void *fdt, int nodeoffset, const char *name)
  269. {
  270. struct fdt_property *prop;
  271. int len, proplen;
  272. FDT_RW_CHECK_HEADER(fdt);
  273. prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
  274. if (! prop)
  275. return len;
  276. proplen = sizeof(*prop) + FDT_TAGALIGN(len);
  277. return _fdt_splice_struct(fdt, prop, proplen, 0);
  278. }
  279. int fdt_add_subnode_namelen(void *fdt, int parentoffset,
  280. const char *name, int namelen)
  281. {
  282. struct fdt_node_header *nh;
  283. int offset, nextoffset;
  284. int nodelen;
  285. int err;
  286. uint32_t tag;
  287. fdt32_t *endtag;
  288. FDT_RW_CHECK_HEADER(fdt);
  289. offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
  290. if (offset >= 0)
  291. return -FDT_ERR_EXISTS;
  292. else if (offset != -FDT_ERR_NOTFOUND)
  293. return offset;
  294. /* Try to place the new node after the parent's properties */
  295. fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
  296. do {
  297. offset = nextoffset;
  298. tag = fdt_next_tag(fdt, offset, &nextoffset);
  299. } while ((tag == FDT_PROP) || (tag == FDT_NOP));
  300. nh = _fdt_offset_ptr_w(fdt, offset);
  301. nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
  302. err = _fdt_splice_struct(fdt, nh, 0, nodelen);
  303. if (err)
  304. return err;
  305. nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
  306. memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
  307. memcpy(nh->name, name, namelen);
  308. endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
  309. *endtag = cpu_to_fdt32(FDT_END_NODE);
  310. return offset;
  311. }
  312. int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
  313. {
  314. return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
  315. }
  316. int fdt_del_node(void *fdt, int nodeoffset)
  317. {
  318. int endoffset;
  319. FDT_RW_CHECK_HEADER(fdt);
  320. endoffset = _fdt_node_end_offset(fdt, nodeoffset);
  321. if (endoffset < 0)
  322. return endoffset;
  323. return _fdt_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset),
  324. endoffset - nodeoffset, 0);
  325. }
  326. static void _fdt_packblocks(const char *old, char *new,
  327. int mem_rsv_size, int struct_size)
  328. {
  329. int mem_rsv_off, struct_off, strings_off;
  330. mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
  331. struct_off = mem_rsv_off + mem_rsv_size;
  332. strings_off = struct_off + struct_size;
  333. memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
  334. fdt_set_off_mem_rsvmap(new, mem_rsv_off);
  335. memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
  336. fdt_set_off_dt_struct(new, struct_off);
  337. fdt_set_size_dt_struct(new, struct_size);
  338. memmove(new + strings_off, old + fdt_off_dt_strings(old),
  339. fdt_size_dt_strings(old));
  340. fdt_set_off_dt_strings(new, strings_off);
  341. fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
  342. }
  343. int fdt_open_into(const void *fdt, void *buf, int bufsize)
  344. {
  345. int err;
  346. int mem_rsv_size, struct_size;
  347. int newsize;
  348. const char *fdtstart = fdt;
  349. const char *fdtend = fdtstart + fdt_totalsize(fdt);
  350. char *tmp;
  351. FDT_CHECK_HEADER(fdt);
  352. mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
  353. * sizeof(struct fdt_reserve_entry);
  354. if (fdt_version(fdt) >= 17) {
  355. struct_size = fdt_size_dt_struct(fdt);
  356. } else {
  357. struct_size = 0;
  358. while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
  359. ;
  360. if (struct_size < 0)
  361. return struct_size;
  362. }
  363. if (!_fdt_blocks_misordered(fdt, mem_rsv_size, struct_size)) {
  364. /* no further work necessary */
  365. err = fdt_move(fdt, buf, bufsize);
  366. if (err)
  367. return err;
  368. fdt_set_version(buf, 17);
  369. fdt_set_size_dt_struct(buf, struct_size);
  370. fdt_set_totalsize(buf, bufsize);
  371. return 0;
  372. }
  373. /* Need to reorder */
  374. newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
  375. + struct_size + fdt_size_dt_strings(fdt);
  376. if (bufsize < newsize)
  377. return -FDT_ERR_NOSPACE;
  378. /* First attempt to build converted tree at beginning of buffer */
  379. tmp = buf;
  380. /* But if that overlaps with the old tree... */
  381. if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
  382. /* Try right after the old tree instead */
  383. tmp = (char *)(uintptr_t)fdtend;
  384. if ((tmp + newsize) > ((char *)buf + bufsize))
  385. return -FDT_ERR_NOSPACE;
  386. }
  387. _fdt_packblocks(fdt, tmp, mem_rsv_size, struct_size);
  388. memmove(buf, tmp, newsize);
  389. fdt_set_magic(buf, FDT_MAGIC);
  390. fdt_set_totalsize(buf, bufsize);
  391. fdt_set_version(buf, 17);
  392. fdt_set_last_comp_version(buf, 16);
  393. fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
  394. return 0;
  395. }
  396. int fdt_pack(void *fdt)
  397. {
  398. int mem_rsv_size;
  399. FDT_RW_CHECK_HEADER(fdt);
  400. mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
  401. * sizeof(struct fdt_reserve_entry);
  402. _fdt_packblocks(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt));
  403. fdt_set_totalsize(fdt, _fdt_data_size(fdt));
  404. return 0;
  405. }