fdt_sw.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_sw_check_header(void *fdt)
  56. {
  57. if (fdt_magic(fdt) != FDT_SW_MAGIC)
  58. return -FDT_ERR_BADMAGIC;
  59. /* FIXME: should check more details about the header state */
  60. return 0;
  61. }
  62. #define FDT_SW_CHECK_HEADER(fdt) \
  63. { \
  64. int err; \
  65. if ((err = _fdt_sw_check_header(fdt)) != 0) \
  66. return err; \
  67. }
  68. static void *_fdt_grab_space(void *fdt, size_t len)
  69. {
  70. int offset = fdt_size_dt_struct(fdt);
  71. int spaceleft;
  72. spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
  73. - fdt_size_dt_strings(fdt);
  74. if ((offset + len < offset) || (offset + len > spaceleft))
  75. return NULL;
  76. fdt_set_size_dt_struct(fdt, offset + len);
  77. return _fdt_offset_ptr_w(fdt, offset);
  78. }
  79. int fdt_create(void *buf, int bufsize)
  80. {
  81. void *fdt = buf;
  82. if (bufsize < sizeof(struct fdt_header))
  83. return -FDT_ERR_NOSPACE;
  84. memset(buf, 0, bufsize);
  85. fdt_set_magic(fdt, FDT_SW_MAGIC);
  86. fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
  87. fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
  88. fdt_set_totalsize(fdt, bufsize);
  89. fdt_set_off_mem_rsvmap(fdt, FDT_ALIGN(sizeof(struct fdt_header),
  90. sizeof(struct fdt_reserve_entry)));
  91. fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
  92. fdt_set_off_dt_strings(fdt, bufsize);
  93. return 0;
  94. }
  95. int fdt_resize(void *fdt, void *buf, int bufsize)
  96. {
  97. size_t headsize, tailsize;
  98. char *oldtail, *newtail;
  99. FDT_SW_CHECK_HEADER(fdt);
  100. headsize = fdt_off_dt_struct(fdt);
  101. tailsize = fdt_size_dt_strings(fdt);
  102. if ((headsize + tailsize) > bufsize)
  103. return -FDT_ERR_NOSPACE;
  104. oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize;
  105. newtail = (char *)buf + bufsize - tailsize;
  106. /* Two cases to avoid clobbering data if the old and new
  107. * buffers partially overlap */
  108. if (buf <= fdt) {
  109. memmove(buf, fdt, headsize);
  110. memmove(newtail, oldtail, tailsize);
  111. } else {
  112. memmove(newtail, oldtail, tailsize);
  113. memmove(buf, fdt, headsize);
  114. }
  115. fdt_set_off_dt_strings(buf, bufsize);
  116. fdt_set_totalsize(buf, bufsize);
  117. return 0;
  118. }
  119. int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
  120. {
  121. struct fdt_reserve_entry *re;
  122. int offset;
  123. FDT_SW_CHECK_HEADER(fdt);
  124. if (fdt_size_dt_struct(fdt))
  125. return -FDT_ERR_BADSTATE;
  126. offset = fdt_off_dt_struct(fdt);
  127. if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
  128. return -FDT_ERR_NOSPACE;
  129. re = (struct fdt_reserve_entry *)((char *)fdt + offset);
  130. re->address = cpu_to_fdt64(addr);
  131. re->size = cpu_to_fdt64(size);
  132. fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
  133. return 0;
  134. }
  135. int fdt_finish_reservemap(void *fdt)
  136. {
  137. return fdt_add_reservemap_entry(fdt, 0, 0);
  138. }
  139. int fdt_begin_node(void *fdt, const char *name)
  140. {
  141. struct fdt_node_header *nh;
  142. int namelen = strlen(name) + 1;
  143. FDT_SW_CHECK_HEADER(fdt);
  144. nh = _fdt_grab_space(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
  145. if (! nh)
  146. return -FDT_ERR_NOSPACE;
  147. nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
  148. memcpy(nh->name, name, namelen);
  149. return 0;
  150. }
  151. int fdt_end_node(void *fdt)
  152. {
  153. fdt32_t *en;
  154. FDT_SW_CHECK_HEADER(fdt);
  155. en = _fdt_grab_space(fdt, FDT_TAGSIZE);
  156. if (! en)
  157. return -FDT_ERR_NOSPACE;
  158. *en = cpu_to_fdt32(FDT_END_NODE);
  159. return 0;
  160. }
  161. static int _fdt_find_add_string(void *fdt, const char *s)
  162. {
  163. char *strtab = (char *)fdt + fdt_totalsize(fdt);
  164. const char *p;
  165. int strtabsize = fdt_size_dt_strings(fdt);
  166. int len = strlen(s) + 1;
  167. int struct_top, offset;
  168. p = _fdt_find_string(strtab - strtabsize, strtabsize, s);
  169. if (p)
  170. return p - strtab;
  171. /* Add it */
  172. offset = -strtabsize - len;
  173. struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
  174. if (fdt_totalsize(fdt) + offset < struct_top)
  175. return 0; /* no more room :( */
  176. memcpy(strtab + offset, s, len);
  177. fdt_set_size_dt_strings(fdt, strtabsize + len);
  178. return offset;
  179. }
  180. int fdt_property(void *fdt, const char *name, const void *val, int len)
  181. {
  182. struct fdt_property *prop;
  183. int nameoff;
  184. FDT_SW_CHECK_HEADER(fdt);
  185. nameoff = _fdt_find_add_string(fdt, name);
  186. if (nameoff == 0)
  187. return -FDT_ERR_NOSPACE;
  188. prop = _fdt_grab_space(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
  189. if (! prop)
  190. return -FDT_ERR_NOSPACE;
  191. prop->tag = cpu_to_fdt32(FDT_PROP);
  192. prop->nameoff = cpu_to_fdt32(nameoff);
  193. prop->len = cpu_to_fdt32(len);
  194. memcpy(prop->data, val, len);
  195. return 0;
  196. }
  197. int fdt_finish(void *fdt)
  198. {
  199. char *p = (char *)fdt;
  200. fdt32_t *end;
  201. int oldstroffset, newstroffset;
  202. uint32_t tag;
  203. int offset, nextoffset;
  204. FDT_SW_CHECK_HEADER(fdt);
  205. /* Add terminator */
  206. end = _fdt_grab_space(fdt, sizeof(*end));
  207. if (! end)
  208. return -FDT_ERR_NOSPACE;
  209. *end = cpu_to_fdt32(FDT_END);
  210. /* Relocate the string table */
  211. oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
  212. newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
  213. memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
  214. fdt_set_off_dt_strings(fdt, newstroffset);
  215. /* Walk the structure, correcting string offsets */
  216. offset = 0;
  217. while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
  218. if (tag == FDT_PROP) {
  219. struct fdt_property *prop =
  220. _fdt_offset_ptr_w(fdt, offset);
  221. int nameoff;
  222. nameoff = fdt32_to_cpu(prop->nameoff);
  223. nameoff += fdt_size_dt_strings(fdt);
  224. prop->nameoff = cpu_to_fdt32(nameoff);
  225. }
  226. offset = nextoffset;
  227. }
  228. if (nextoffset < 0)
  229. return nextoffset;
  230. /* Finally, adjust the header */
  231. fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
  232. fdt_set_magic(fdt, FDT_MAGIC);
  233. return 0;
  234. }