fdt_rw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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_blocks_misordered_(const void *fdt,
  11. int mem_rsv_size, int struct_size)
  12. {
  13. return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8))
  14. || (fdt_off_dt_struct(fdt) <
  15. (fdt_off_mem_rsvmap(fdt) + mem_rsv_size))
  16. || (fdt_off_dt_strings(fdt) <
  17. (fdt_off_dt_struct(fdt) + struct_size))
  18. || (fdt_totalsize(fdt) <
  19. (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
  20. }
  21. static int fdt_rw_probe_(void *fdt)
  22. {
  23. if (can_assume(VALID_DTB))
  24. return 0;
  25. FDT_RO_PROBE(fdt);
  26. if (!can_assume(LATEST) && fdt_version(fdt) < 17)
  27. return -FDT_ERR_BADVERSION;
  28. if (fdt_blocks_misordered_(fdt, sizeof(struct fdt_reserve_entry),
  29. fdt_size_dt_struct(fdt)))
  30. return -FDT_ERR_BADLAYOUT;
  31. if (!can_assume(LATEST) && fdt_version(fdt) > 17)
  32. fdt_set_version(fdt, 17);
  33. return 0;
  34. }
  35. #define FDT_RW_PROBE(fdt) \
  36. { \
  37. int err_; \
  38. if ((err_ = fdt_rw_probe_(fdt)) != 0) \
  39. return err_; \
  40. }
  41. static inline unsigned int fdt_data_size_(void *fdt)
  42. {
  43. return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
  44. }
  45. static int fdt_splice_(void *fdt, void *splicepoint, int oldlen, int newlen)
  46. {
  47. char *p = splicepoint;
  48. unsigned int dsize = fdt_data_size_(fdt);
  49. size_t soff = p - (char *)fdt;
  50. if ((oldlen < 0) || (soff + oldlen < soff) || (soff + oldlen > dsize))
  51. return -FDT_ERR_BADOFFSET;
  52. if ((p < (char *)fdt) || (dsize + newlen < (unsigned)oldlen))
  53. return -FDT_ERR_BADOFFSET;
  54. if (dsize - oldlen + newlen > fdt_totalsize(fdt))
  55. return -FDT_ERR_NOSPACE;
  56. memmove(p + newlen, p + oldlen, ((char *)fdt + dsize) - (p + oldlen));
  57. return 0;
  58. }
  59. static int fdt_splice_mem_rsv_(void *fdt, struct fdt_reserve_entry *p,
  60. int oldn, int newn)
  61. {
  62. int delta = (newn - oldn) * sizeof(*p);
  63. int err;
  64. err = fdt_splice_(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
  65. if (err)
  66. return err;
  67. fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
  68. fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
  69. return 0;
  70. }
  71. static int fdt_splice_struct_(void *fdt, void *p,
  72. int oldlen, int newlen)
  73. {
  74. int delta = newlen - oldlen;
  75. int err;
  76. if ((err = fdt_splice_(fdt, p, oldlen, newlen)))
  77. return err;
  78. fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
  79. fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
  80. return 0;
  81. }
  82. /* Must only be used to roll back in case of error */
  83. static void fdt_del_last_string_(void *fdt, const char *s)
  84. {
  85. int newlen = strlen(s) + 1;
  86. fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) - newlen);
  87. }
  88. static int fdt_splice_string_(void *fdt, int newlen)
  89. {
  90. void *p = (char *)fdt
  91. + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
  92. int err;
  93. if ((err = fdt_splice_(fdt, p, 0, newlen)))
  94. return err;
  95. fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
  96. return 0;
  97. }
  98. /**
  99. * fdt_find_add_string_() - Find or allocate a string
  100. *
  101. * @fdt: pointer to the device tree to check/adjust
  102. * @s: string to find/add
  103. * @allocated: Set to 0 if the string was found, 1 if not found and so
  104. * allocated. Ignored if can_assume(NO_ROLLBACK)
  105. * @return offset of string in the string table (whether found or added)
  106. */
  107. static int fdt_find_add_string_(void *fdt, const char *s, int *allocated)
  108. {
  109. char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
  110. const char *p;
  111. char *new;
  112. int len = strlen(s) + 1;
  113. int err;
  114. if (!can_assume(NO_ROLLBACK))
  115. *allocated = 0;
  116. p = fdt_find_string_(strtab, fdt_size_dt_strings(fdt), s);
  117. if (p)
  118. /* found it */
  119. return (p - strtab);
  120. new = strtab + fdt_size_dt_strings(fdt);
  121. err = fdt_splice_string_(fdt, len);
  122. if (err)
  123. return err;
  124. if (!can_assume(NO_ROLLBACK))
  125. *allocated = 1;
  126. memcpy(new, s, len);
  127. return (new - strtab);
  128. }
  129. int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
  130. {
  131. struct fdt_reserve_entry *re;
  132. int err;
  133. FDT_RW_PROBE(fdt);
  134. re = fdt_mem_rsv_w_(fdt, fdt_num_mem_rsv(fdt));
  135. err = fdt_splice_mem_rsv_(fdt, re, 0, 1);
  136. if (err)
  137. return err;
  138. re->address = cpu_to_fdt64(address);
  139. re->size = cpu_to_fdt64(size);
  140. return 0;
  141. }
  142. int fdt_del_mem_rsv(void *fdt, int n)
  143. {
  144. struct fdt_reserve_entry *re = fdt_mem_rsv_w_(fdt, n);
  145. FDT_RW_PROBE(fdt);
  146. if (n >= fdt_num_mem_rsv(fdt))
  147. return -FDT_ERR_NOTFOUND;
  148. return fdt_splice_mem_rsv_(fdt, re, 1, 0);
  149. }
  150. static int fdt_resize_property_(void *fdt, int nodeoffset, const char *name,
  151. int len, struct fdt_property **prop)
  152. {
  153. int oldlen;
  154. int err;
  155. *prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
  156. if (!*prop)
  157. return oldlen;
  158. if ((err = fdt_splice_struct_(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
  159. FDT_TAGALIGN(len))))
  160. return err;
  161. (*prop)->len = cpu_to_fdt32(len);
  162. return 0;
  163. }
  164. static int fdt_add_property_(void *fdt, int nodeoffset, const char *name,
  165. int len, struct fdt_property **prop)
  166. {
  167. int proplen;
  168. int nextoffset;
  169. int namestroff;
  170. int err;
  171. int allocated;
  172. if ((nextoffset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
  173. return nextoffset;
  174. namestroff = fdt_find_add_string_(fdt, name, &allocated);
  175. if (namestroff < 0)
  176. return namestroff;
  177. *prop = fdt_offset_ptr_w_(fdt, nextoffset);
  178. proplen = sizeof(**prop) + FDT_TAGALIGN(len);
  179. err = fdt_splice_struct_(fdt, *prop, 0, proplen);
  180. if (err) {
  181. /* Delete the string if we failed to add it */
  182. if (!can_assume(NO_ROLLBACK) && allocated)
  183. fdt_del_last_string_(fdt, name);
  184. return err;
  185. }
  186. (*prop)->tag = cpu_to_fdt32(FDT_PROP);
  187. (*prop)->nameoff = cpu_to_fdt32(namestroff);
  188. (*prop)->len = cpu_to_fdt32(len);
  189. return 0;
  190. }
  191. int fdt_set_name(void *fdt, int nodeoffset, const char *name)
  192. {
  193. char *namep;
  194. int oldlen, newlen;
  195. int err;
  196. FDT_RW_PROBE(fdt);
  197. namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
  198. if (!namep)
  199. return oldlen;
  200. newlen = strlen(name);
  201. err = fdt_splice_struct_(fdt, namep, FDT_TAGALIGN(oldlen+1),
  202. FDT_TAGALIGN(newlen+1));
  203. if (err)
  204. return err;
  205. memcpy(namep, name, newlen+1);
  206. return 0;
  207. }
  208. int fdt_setprop_placeholder(void *fdt, int nodeoffset, const char *name,
  209. int len, void **prop_data)
  210. {
  211. struct fdt_property *prop;
  212. int err;
  213. FDT_RW_PROBE(fdt);
  214. err = fdt_resize_property_(fdt, nodeoffset, name, len, &prop);
  215. if (err == -FDT_ERR_NOTFOUND)
  216. err = fdt_add_property_(fdt, nodeoffset, name, len, &prop);
  217. if (err)
  218. return err;
  219. *prop_data = prop->data;
  220. return 0;
  221. }
  222. int fdt_setprop(void *fdt, int nodeoffset, const char *name,
  223. const void *val, int len)
  224. {
  225. void *prop_data;
  226. int err;
  227. err = fdt_setprop_placeholder(fdt, nodeoffset, name, len, &prop_data);
  228. if (err)
  229. return err;
  230. if (len)
  231. memcpy(prop_data, val, len);
  232. return 0;
  233. }
  234. int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
  235. const void *val, int len)
  236. {
  237. struct fdt_property *prop;
  238. int err, oldlen, newlen;
  239. FDT_RW_PROBE(fdt);
  240. prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
  241. if (prop) {
  242. newlen = len + oldlen;
  243. err = fdt_splice_struct_(fdt, prop->data,
  244. FDT_TAGALIGN(oldlen),
  245. FDT_TAGALIGN(newlen));
  246. if (err)
  247. return err;
  248. prop->len = cpu_to_fdt32(newlen);
  249. memcpy(prop->data + oldlen, val, len);
  250. } else {
  251. err = fdt_add_property_(fdt, nodeoffset, name, len, &prop);
  252. if (err)
  253. return err;
  254. memcpy(prop->data, val, len);
  255. }
  256. return 0;
  257. }
  258. int fdt_delprop(void *fdt, int nodeoffset, const char *name)
  259. {
  260. struct fdt_property *prop;
  261. int len, proplen;
  262. FDT_RW_PROBE(fdt);
  263. prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
  264. if (!prop)
  265. return len;
  266. proplen = sizeof(*prop) + FDT_TAGALIGN(len);
  267. return fdt_splice_struct_(fdt, prop, proplen, 0);
  268. }
  269. int fdt_add_subnode_namelen(void *fdt, int parentoffset,
  270. const char *name, int namelen)
  271. {
  272. struct fdt_node_header *nh;
  273. int offset, nextoffset;
  274. int nodelen;
  275. int err;
  276. uint32_t tag;
  277. fdt32_t *endtag;
  278. FDT_RW_PROBE(fdt);
  279. offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
  280. if (offset >= 0)
  281. return -FDT_ERR_EXISTS;
  282. else if (offset != -FDT_ERR_NOTFOUND)
  283. return offset;
  284. /* Try to place the new node after the parent's properties */
  285. tag = fdt_next_tag(fdt, parentoffset, &nextoffset);
  286. /* the fdt_subnode_offset_namelen() should ensure this never hits */
  287. if (!can_assume(LIBFDT_FLAWLESS) && (tag != FDT_BEGIN_NODE))
  288. return -FDT_ERR_INTERNAL;
  289. do {
  290. offset = nextoffset;
  291. tag = fdt_next_tag(fdt, offset, &nextoffset);
  292. } while ((tag == FDT_PROP) || (tag == FDT_NOP));
  293. nh = fdt_offset_ptr_w_(fdt, offset);
  294. nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
  295. err = fdt_splice_struct_(fdt, nh, 0, nodelen);
  296. if (err)
  297. return err;
  298. nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
  299. memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
  300. memcpy(nh->name, name, namelen);
  301. endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
  302. *endtag = cpu_to_fdt32(FDT_END_NODE);
  303. return offset;
  304. }
  305. int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
  306. {
  307. return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
  308. }
  309. int fdt_del_node(void *fdt, int nodeoffset)
  310. {
  311. int endoffset;
  312. FDT_RW_PROBE(fdt);
  313. endoffset = fdt_node_end_offset_(fdt, nodeoffset);
  314. if (endoffset < 0)
  315. return endoffset;
  316. return fdt_splice_struct_(fdt, fdt_offset_ptr_w_(fdt, nodeoffset),
  317. endoffset - nodeoffset, 0);
  318. }
  319. static void fdt_packblocks_(const char *old, char *new,
  320. int mem_rsv_size,
  321. int struct_size,
  322. int strings_size)
  323. {
  324. int mem_rsv_off, struct_off, strings_off;
  325. mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
  326. struct_off = mem_rsv_off + mem_rsv_size;
  327. strings_off = struct_off + struct_size;
  328. memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
  329. fdt_set_off_mem_rsvmap(new, mem_rsv_off);
  330. memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
  331. fdt_set_off_dt_struct(new, struct_off);
  332. fdt_set_size_dt_struct(new, struct_size);
  333. memmove(new + strings_off, old + fdt_off_dt_strings(old), strings_size);
  334. fdt_set_off_dt_strings(new, strings_off);
  335. fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
  336. }
  337. int fdt_open_into(const void *fdt, void *buf, int bufsize)
  338. {
  339. int err;
  340. int mem_rsv_size, struct_size;
  341. int newsize;
  342. const char *fdtstart = fdt;
  343. const char *fdtend = fdtstart + fdt_totalsize(fdt);
  344. char *tmp;
  345. FDT_RO_PROBE(fdt);
  346. mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
  347. * sizeof(struct fdt_reserve_entry);
  348. if (can_assume(LATEST) || fdt_version(fdt) >= 17) {
  349. struct_size = fdt_size_dt_struct(fdt);
  350. } else if (fdt_version(fdt) == 16) {
  351. struct_size = 0;
  352. while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
  353. ;
  354. if (struct_size < 0)
  355. return struct_size;
  356. } else {
  357. return -FDT_ERR_BADVERSION;
  358. }
  359. if (can_assume(LIBFDT_ORDER) ||
  360. !fdt_blocks_misordered_(fdt, mem_rsv_size, struct_size)) {
  361. /* no further work necessary */
  362. err = fdt_move(fdt, buf, bufsize);
  363. if (err)
  364. return err;
  365. fdt_set_version(buf, 17);
  366. fdt_set_size_dt_struct(buf, struct_size);
  367. fdt_set_totalsize(buf, bufsize);
  368. return 0;
  369. }
  370. /* Need to reorder */
  371. newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
  372. + struct_size + fdt_size_dt_strings(fdt);
  373. if (bufsize < newsize)
  374. return -FDT_ERR_NOSPACE;
  375. /* First attempt to build converted tree at beginning of buffer */
  376. tmp = buf;
  377. /* But if that overlaps with the old tree... */
  378. if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
  379. /* Try right after the old tree instead */
  380. tmp = (char *)(uintptr_t)fdtend;
  381. if ((tmp + newsize) > ((char *)buf + bufsize))
  382. return -FDT_ERR_NOSPACE;
  383. }
  384. fdt_packblocks_(fdt, tmp, mem_rsv_size, struct_size,
  385. fdt_size_dt_strings(fdt));
  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_PROBE(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_size_dt_strings(fdt));
  402. fdt_set_totalsize(fdt, fdt_data_size_(fdt));
  403. return 0;
  404. }