fdt_rw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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_appendprop(void *fdt, int nodeoffset, const char *name,
  243. const void *val, int len)
  244. {
  245. struct fdt_property *prop;
  246. int err, oldlen, newlen;
  247. FDT_RW_CHECK_HEADER(fdt);
  248. prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
  249. if (prop) {
  250. newlen = len + oldlen;
  251. err = _fdt_splice_struct(fdt, prop->data,
  252. FDT_TAGALIGN(oldlen),
  253. FDT_TAGALIGN(newlen));
  254. if (err)
  255. return err;
  256. prop->len = cpu_to_fdt32(newlen);
  257. memcpy(prop->data + oldlen, val, len);
  258. } else {
  259. err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
  260. if (err)
  261. return err;
  262. memcpy(prop->data, val, len);
  263. }
  264. return 0;
  265. }
  266. int fdt_delprop(void *fdt, int nodeoffset, const char *name)
  267. {
  268. struct fdt_property *prop;
  269. int len, proplen;
  270. FDT_RW_CHECK_HEADER(fdt);
  271. prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
  272. if (! prop)
  273. return len;
  274. proplen = sizeof(*prop) + FDT_TAGALIGN(len);
  275. return _fdt_splice_struct(fdt, prop, proplen, 0);
  276. }
  277. int fdt_add_subnode_namelen(void *fdt, int parentoffset,
  278. const char *name, int namelen)
  279. {
  280. struct fdt_node_header *nh;
  281. int offset, nextoffset;
  282. int nodelen;
  283. int err;
  284. uint32_t tag;
  285. fdt32_t *endtag;
  286. FDT_RW_CHECK_HEADER(fdt);
  287. offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
  288. if (offset >= 0)
  289. return -FDT_ERR_EXISTS;
  290. else if (offset != -FDT_ERR_NOTFOUND)
  291. return offset;
  292. /* Try to place the new node after the parent's properties */
  293. fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
  294. do {
  295. offset = nextoffset;
  296. tag = fdt_next_tag(fdt, offset, &nextoffset);
  297. } while ((tag == FDT_PROP) || (tag == FDT_NOP));
  298. nh = _fdt_offset_ptr_w(fdt, offset);
  299. nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
  300. err = _fdt_splice_struct(fdt, nh, 0, nodelen);
  301. if (err)
  302. return err;
  303. nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
  304. memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
  305. memcpy(nh->name, name, namelen);
  306. endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
  307. *endtag = cpu_to_fdt32(FDT_END_NODE);
  308. return offset;
  309. }
  310. int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
  311. {
  312. return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
  313. }
  314. int fdt_del_node(void *fdt, int nodeoffset)
  315. {
  316. int endoffset;
  317. FDT_RW_CHECK_HEADER(fdt);
  318. endoffset = _fdt_node_end_offset(fdt, nodeoffset);
  319. if (endoffset < 0)
  320. return endoffset;
  321. return _fdt_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset),
  322. endoffset - nodeoffset, 0);
  323. }
  324. static void _fdt_packblocks(const char *old, char *new,
  325. int mem_rsv_size, int struct_size)
  326. {
  327. int mem_rsv_off, struct_off, strings_off;
  328. mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
  329. struct_off = mem_rsv_off + mem_rsv_size;
  330. strings_off = struct_off + struct_size;
  331. memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
  332. fdt_set_off_mem_rsvmap(new, mem_rsv_off);
  333. memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
  334. fdt_set_off_dt_struct(new, struct_off);
  335. fdt_set_size_dt_struct(new, struct_size);
  336. memmove(new + strings_off, old + fdt_off_dt_strings(old),
  337. fdt_size_dt_strings(old));
  338. fdt_set_off_dt_strings(new, strings_off);
  339. fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
  340. }
  341. int fdt_open_into(const void *fdt, void *buf, int bufsize)
  342. {
  343. int err;
  344. int mem_rsv_size, struct_size;
  345. int newsize;
  346. const char *fdtstart = fdt;
  347. const char *fdtend = fdtstart + fdt_totalsize(fdt);
  348. char *tmp;
  349. FDT_CHECK_HEADER(fdt);
  350. mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
  351. * sizeof(struct fdt_reserve_entry);
  352. if (fdt_version(fdt) >= 17) {
  353. struct_size = fdt_size_dt_struct(fdt);
  354. } else {
  355. struct_size = 0;
  356. while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
  357. ;
  358. if (struct_size < 0)
  359. return struct_size;
  360. }
  361. if (!_fdt_blocks_misordered(fdt, mem_rsv_size, struct_size)) {
  362. /* no further work necessary */
  363. err = fdt_move(fdt, buf, bufsize);
  364. if (err)
  365. return err;
  366. fdt_set_version(buf, 17);
  367. fdt_set_size_dt_struct(buf, struct_size);
  368. fdt_set_totalsize(buf, bufsize);
  369. return 0;
  370. }
  371. /* Need to reorder */
  372. newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
  373. + struct_size + fdt_size_dt_strings(fdt);
  374. if (bufsize < newsize)
  375. return -FDT_ERR_NOSPACE;
  376. /* First attempt to build converted tree at beginning of buffer */
  377. tmp = buf;
  378. /* But if that overlaps with the old tree... */
  379. if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
  380. /* Try right after the old tree instead */
  381. tmp = (char *)(uintptr_t)fdtend;
  382. if ((tmp + newsize) > ((char *)buf + bufsize))
  383. return -FDT_ERR_NOSPACE;
  384. }
  385. _fdt_packblocks(fdt, tmp, mem_rsv_size, struct_size);
  386. memmove(buf, tmp, newsize);
  387. fdt_set_magic(buf, FDT_MAGIC);
  388. fdt_set_totalsize(buf, bufsize);
  389. fdt_set_version(buf, 17);
  390. fdt_set_last_comp_version(buf, 16);
  391. fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
  392. return 0;
  393. }
  394. int fdt_pack(void *fdt)
  395. {
  396. int mem_rsv_size;
  397. FDT_RW_CHECK_HEADER(fdt);
  398. mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
  399. * sizeof(struct fdt_reserve_entry);
  400. _fdt_packblocks(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt));
  401. fdt_set_totalsize(fdt, _fdt_data_size(fdt));
  402. return 0;
  403. }