xref: /linux/scripts/dtc/libfdt/fdt_rw.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
112869ecdSRob Herring // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
29fffb55fSDavid Gibson /*
39fffb55fSDavid Gibson  * libfdt - Flat Device Tree manipulation
49fffb55fSDavid Gibson  * Copyright (C) 2006 David Gibson, IBM Corporation.
59fffb55fSDavid Gibson  */
69fffb55fSDavid Gibson #include "libfdt_env.h"
79fffb55fSDavid Gibson 
89fffb55fSDavid Gibson #include <fdt.h>
99fffb55fSDavid Gibson #include <libfdt.h>
109fffb55fSDavid Gibson 
119fffb55fSDavid Gibson #include "libfdt_internal.h"
129fffb55fSDavid Gibson 
fdt_blocks_misordered_(const void * fdt,int mem_rsv_size,int struct_size)139130ba88SRob Herring static int fdt_blocks_misordered_(const void *fdt,
149fffb55fSDavid Gibson 				  int mem_rsv_size, int struct_size)
159fffb55fSDavid Gibson {
169fffb55fSDavid Gibson 	return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8))
179fffb55fSDavid Gibson 		|| (fdt_off_dt_struct(fdt) <
189fffb55fSDavid Gibson 		    (fdt_off_mem_rsvmap(fdt) + mem_rsv_size))
199fffb55fSDavid Gibson 		|| (fdt_off_dt_strings(fdt) <
209fffb55fSDavid Gibson 		    (fdt_off_dt_struct(fdt) + struct_size))
219fffb55fSDavid Gibson 		|| (fdt_totalsize(fdt) <
229fffb55fSDavid Gibson 		    (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
239fffb55fSDavid Gibson }
249fffb55fSDavid Gibson 
fdt_rw_probe_(void * fdt)25f858927fSRob Herring static int fdt_rw_probe_(void *fdt)
269fffb55fSDavid Gibson {
27d047cd8aSRob Herring 	if (can_assume(VALID_DTB))
28d047cd8aSRob Herring 		return 0;
29f858927fSRob Herring 	FDT_RO_PROBE(fdt);
309fffb55fSDavid Gibson 
31d047cd8aSRob Herring 	if (!can_assume(LATEST) && fdt_version(fdt) < 17)
329fffb55fSDavid Gibson 		return -FDT_ERR_BADVERSION;
339130ba88SRob Herring 	if (fdt_blocks_misordered_(fdt, sizeof(struct fdt_reserve_entry),
349fffb55fSDavid Gibson 				   fdt_size_dt_struct(fdt)))
359fffb55fSDavid Gibson 		return -FDT_ERR_BADLAYOUT;
36d047cd8aSRob Herring 	if (!can_assume(LATEST) && fdt_version(fdt) > 17)
379fffb55fSDavid Gibson 		fdt_set_version(fdt, 17);
389fffb55fSDavid Gibson 
399fffb55fSDavid Gibson 	return 0;
409fffb55fSDavid Gibson }
419fffb55fSDavid Gibson 
42f858927fSRob Herring #define FDT_RW_PROBE(fdt) \
439fffb55fSDavid Gibson 	{ \
449130ba88SRob Herring 		int err_; \
45f858927fSRob Herring 		if ((err_ = fdt_rw_probe_(fdt)) != 0) \
469130ba88SRob Herring 			return err_; \
479fffb55fSDavid Gibson 	}
489fffb55fSDavid Gibson 
fdt_data_size_(void * fdt)49d047cd8aSRob Herring static inline unsigned int fdt_data_size_(void *fdt)
509fffb55fSDavid Gibson {
519fffb55fSDavid Gibson 	return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
529fffb55fSDavid Gibson }
539fffb55fSDavid Gibson 
fdt_splice_(void * fdt,void * splicepoint,int oldlen,int newlen)549130ba88SRob Herring static int fdt_splice_(void *fdt, void *splicepoint, int oldlen, int newlen)
559fffb55fSDavid Gibson {
569fffb55fSDavid Gibson 	char *p = splicepoint;
57d047cd8aSRob Herring 	unsigned int dsize = fdt_data_size_(fdt);
58d047cd8aSRob Herring 	size_t soff = p - (char *)fdt;
599fffb55fSDavid Gibson 
60d047cd8aSRob Herring 	if ((oldlen < 0) || (soff + oldlen < soff) || (soff + oldlen > dsize))
619fffb55fSDavid Gibson 		return -FDT_ERR_BADOFFSET;
626e9c9686SRob Herring 	if ((p < (char *)fdt) || (dsize + newlen < (unsigned)oldlen))
6391feabc2SRob Herring 		return -FDT_ERR_BADOFFSET;
64d047cd8aSRob Herring 	if (dsize - oldlen + newlen > fdt_totalsize(fdt))
659fffb55fSDavid Gibson 		return -FDT_ERR_NOSPACE;
66d047cd8aSRob Herring 	memmove(p + newlen, p + oldlen, ((char *)fdt + dsize) - (p + oldlen));
679fffb55fSDavid Gibson 	return 0;
689fffb55fSDavid Gibson }
699fffb55fSDavid Gibson 
fdt_splice_mem_rsv_(void * fdt,struct fdt_reserve_entry * p,int oldn,int newn)709130ba88SRob Herring static int fdt_splice_mem_rsv_(void *fdt, struct fdt_reserve_entry *p,
719fffb55fSDavid Gibson 			       int oldn, int newn)
729fffb55fSDavid Gibson {
739fffb55fSDavid Gibson 	int delta = (newn - oldn) * sizeof(*p);
749fffb55fSDavid Gibson 	int err;
759130ba88SRob Herring 	err = fdt_splice_(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
769fffb55fSDavid Gibson 	if (err)
779fffb55fSDavid Gibson 		return err;
789fffb55fSDavid Gibson 	fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
799fffb55fSDavid Gibson 	fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
809fffb55fSDavid Gibson 	return 0;
819fffb55fSDavid Gibson }
829fffb55fSDavid Gibson 
fdt_splice_struct_(void * fdt,void * p,int oldlen,int newlen)839130ba88SRob Herring static int fdt_splice_struct_(void *fdt, void *p,
849fffb55fSDavid Gibson 			      int oldlen, int newlen)
859fffb55fSDavid Gibson {
869fffb55fSDavid Gibson 	int delta = newlen - oldlen;
879fffb55fSDavid Gibson 	int err;
889fffb55fSDavid Gibson 
899130ba88SRob Herring 	if ((err = fdt_splice_(fdt, p, oldlen, newlen)))
909fffb55fSDavid Gibson 		return err;
919fffb55fSDavid Gibson 
929fffb55fSDavid Gibson 	fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
939fffb55fSDavid Gibson 	fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
949fffb55fSDavid Gibson 	return 0;
959fffb55fSDavid Gibson }
969fffb55fSDavid Gibson 
979bb9c6a1SRob Herring /* Must only be used to roll back in case of error */
fdt_del_last_string_(void * fdt,const char * s)989bb9c6a1SRob Herring static void fdt_del_last_string_(void *fdt, const char *s)
999bb9c6a1SRob Herring {
1009bb9c6a1SRob Herring 	int newlen = strlen(s) + 1;
1019bb9c6a1SRob Herring 
1029bb9c6a1SRob Herring 	fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) - newlen);
1039bb9c6a1SRob Herring }
1049bb9c6a1SRob Herring 
fdt_splice_string_(void * fdt,int newlen)1059130ba88SRob Herring static int fdt_splice_string_(void *fdt, int newlen)
1069fffb55fSDavid Gibson {
1079fffb55fSDavid Gibson 	void *p = (char *)fdt
1089fffb55fSDavid Gibson 		+ fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
1099fffb55fSDavid Gibson 	int err;
1109fffb55fSDavid Gibson 
1119130ba88SRob Herring 	if ((err = fdt_splice_(fdt, p, 0, newlen)))
1129fffb55fSDavid Gibson 		return err;
1139fffb55fSDavid Gibson 
1149fffb55fSDavid Gibson 	fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
1159fffb55fSDavid Gibson 	return 0;
1169fffb55fSDavid Gibson }
1179fffb55fSDavid Gibson 
118d047cd8aSRob Herring /**
119d047cd8aSRob Herring  * fdt_find_add_string_() - Find or allocate a string
120d047cd8aSRob Herring  *
121d047cd8aSRob Herring  * @fdt: pointer to the device tree to check/adjust
122d047cd8aSRob Herring  * @s: string to find/add
123d047cd8aSRob Herring  * @allocated: Set to 0 if the string was found, 1 if not found and so
124d047cd8aSRob Herring  *	allocated. Ignored if can_assume(NO_ROLLBACK)
125d047cd8aSRob Herring  * @return offset of string in the string table (whether found or added)
126d047cd8aSRob Herring  */
fdt_find_add_string_(void * fdt,const char * s,int * allocated)1279bb9c6a1SRob Herring static int fdt_find_add_string_(void *fdt, const char *s, int *allocated)
1289fffb55fSDavid Gibson {
1299fffb55fSDavid Gibson 	char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
1309fffb55fSDavid Gibson 	const char *p;
1319fffb55fSDavid Gibson 	char *new;
1329fffb55fSDavid Gibson 	int len = strlen(s) + 1;
1339fffb55fSDavid Gibson 	int err;
1349fffb55fSDavid Gibson 
135d047cd8aSRob Herring 	if (!can_assume(NO_ROLLBACK))
1369bb9c6a1SRob Herring 		*allocated = 0;
1379bb9c6a1SRob Herring 
1389130ba88SRob Herring 	p = fdt_find_string_(strtab, fdt_size_dt_strings(fdt), s);
1399fffb55fSDavid Gibson 	if (p)
1409fffb55fSDavid Gibson 		/* found it */
1419fffb55fSDavid Gibson 		return (p - strtab);
1429fffb55fSDavid Gibson 
1439fffb55fSDavid Gibson 	new = strtab + fdt_size_dt_strings(fdt);
1449130ba88SRob Herring 	err = fdt_splice_string_(fdt, len);
1459fffb55fSDavid Gibson 	if (err)
1469fffb55fSDavid Gibson 		return err;
1479fffb55fSDavid Gibson 
148d047cd8aSRob Herring 	if (!can_assume(NO_ROLLBACK))
1499bb9c6a1SRob Herring 		*allocated = 1;
1509bb9c6a1SRob Herring 
1519fffb55fSDavid Gibson 	memcpy(new, s, len);
1529fffb55fSDavid Gibson 	return (new - strtab);
1539fffb55fSDavid Gibson }
1549fffb55fSDavid Gibson 
fdt_add_mem_rsv(void * fdt,uint64_t address,uint64_t size)1559fffb55fSDavid Gibson int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
1569fffb55fSDavid Gibson {
1579fffb55fSDavid Gibson 	struct fdt_reserve_entry *re;
1589fffb55fSDavid Gibson 	int err;
1599fffb55fSDavid Gibson 
160f858927fSRob Herring 	FDT_RW_PROBE(fdt);
1619fffb55fSDavid Gibson 
1629130ba88SRob Herring 	re = fdt_mem_rsv_w_(fdt, fdt_num_mem_rsv(fdt));
1639130ba88SRob Herring 	err = fdt_splice_mem_rsv_(fdt, re, 0, 1);
1649fffb55fSDavid Gibson 	if (err)
1659fffb55fSDavid Gibson 		return err;
1669fffb55fSDavid Gibson 
1679fffb55fSDavid Gibson 	re->address = cpu_to_fdt64(address);
1689fffb55fSDavid Gibson 	re->size = cpu_to_fdt64(size);
1699fffb55fSDavid Gibson 	return 0;
1709fffb55fSDavid Gibson }
1719fffb55fSDavid Gibson 
fdt_del_mem_rsv(void * fdt,int n)1729fffb55fSDavid Gibson int fdt_del_mem_rsv(void *fdt, int n)
1739fffb55fSDavid Gibson {
1749130ba88SRob Herring 	struct fdt_reserve_entry *re = fdt_mem_rsv_w_(fdt, n);
1759fffb55fSDavid Gibson 
176f858927fSRob Herring 	FDT_RW_PROBE(fdt);
1779fffb55fSDavid Gibson 
1789fffb55fSDavid Gibson 	if (n >= fdt_num_mem_rsv(fdt))
1799fffb55fSDavid Gibson 		return -FDT_ERR_NOTFOUND;
1809fffb55fSDavid Gibson 
1819130ba88SRob Herring 	return fdt_splice_mem_rsv_(fdt, re, 1, 0);
1829fffb55fSDavid Gibson }
1839fffb55fSDavid Gibson 
fdt_resize_property_(void * fdt,int nodeoffset,const char * name,int len,struct fdt_property ** prop)1849130ba88SRob Herring static int fdt_resize_property_(void *fdt, int nodeoffset, const char *name,
1859fffb55fSDavid Gibson 				int len, struct fdt_property **prop)
1869fffb55fSDavid Gibson {
1879fffb55fSDavid Gibson 	int oldlen;
1889fffb55fSDavid Gibson 	int err;
1899fffb55fSDavid Gibson 
1909fffb55fSDavid Gibson 	*prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
1914201d057SRob Herring 	if (!*prop)
1929fffb55fSDavid Gibson 		return oldlen;
1939fffb55fSDavid Gibson 
1949130ba88SRob Herring 	if ((err = fdt_splice_struct_(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
1959fffb55fSDavid Gibson 				      FDT_TAGALIGN(len))))
1969fffb55fSDavid Gibson 		return err;
1979fffb55fSDavid Gibson 
1989fffb55fSDavid Gibson 	(*prop)->len = cpu_to_fdt32(len);
1999fffb55fSDavid Gibson 	return 0;
2009fffb55fSDavid Gibson }
2019fffb55fSDavid Gibson 
fdt_add_property_(void * fdt,int nodeoffset,const char * name,int len,struct fdt_property ** prop)2029130ba88SRob Herring static int fdt_add_property_(void *fdt, int nodeoffset, const char *name,
2039fffb55fSDavid Gibson 			     int len, struct fdt_property **prop)
2049fffb55fSDavid Gibson {
2059fffb55fSDavid Gibson 	int proplen;
2069fffb55fSDavid Gibson 	int nextoffset;
2079fffb55fSDavid Gibson 	int namestroff;
2089fffb55fSDavid Gibson 	int err;
2099bb9c6a1SRob Herring 	int allocated;
2109fffb55fSDavid Gibson 
2119130ba88SRob Herring 	if ((nextoffset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
2129fffb55fSDavid Gibson 		return nextoffset;
2139fffb55fSDavid Gibson 
2149bb9c6a1SRob Herring 	namestroff = fdt_find_add_string_(fdt, name, &allocated);
2159fffb55fSDavid Gibson 	if (namestroff < 0)
2169fffb55fSDavid Gibson 		return namestroff;
2179fffb55fSDavid Gibson 
2189130ba88SRob Herring 	*prop = fdt_offset_ptr_w_(fdt, nextoffset);
2199fffb55fSDavid Gibson 	proplen = sizeof(**prop) + FDT_TAGALIGN(len);
2209fffb55fSDavid Gibson 
2219130ba88SRob Herring 	err = fdt_splice_struct_(fdt, *prop, 0, proplen);
2229bb9c6a1SRob Herring 	if (err) {
223d047cd8aSRob Herring 		/* Delete the string if we failed to add it */
224d047cd8aSRob Herring 		if (!can_assume(NO_ROLLBACK) && allocated)
2259bb9c6a1SRob Herring 			fdt_del_last_string_(fdt, name);
2269fffb55fSDavid Gibson 		return err;
2279bb9c6a1SRob Herring 	}
2289fffb55fSDavid Gibson 
2299fffb55fSDavid Gibson 	(*prop)->tag = cpu_to_fdt32(FDT_PROP);
2309fffb55fSDavid Gibson 	(*prop)->nameoff = cpu_to_fdt32(namestroff);
2319fffb55fSDavid Gibson 	(*prop)->len = cpu_to_fdt32(len);
2329fffb55fSDavid Gibson 	return 0;
2339fffb55fSDavid Gibson }
2349fffb55fSDavid Gibson 
fdt_set_name(void * fdt,int nodeoffset,const char * name)2359fffb55fSDavid Gibson int fdt_set_name(void *fdt, int nodeoffset, const char *name)
2369fffb55fSDavid Gibson {
2379fffb55fSDavid Gibson 	char *namep;
2389fffb55fSDavid Gibson 	int oldlen, newlen;
2399fffb55fSDavid Gibson 	int err;
2409fffb55fSDavid Gibson 
241f858927fSRob Herring 	FDT_RW_PROBE(fdt);
2429fffb55fSDavid Gibson 
2439fffb55fSDavid Gibson 	namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
2449fffb55fSDavid Gibson 	if (!namep)
2459fffb55fSDavid Gibson 		return oldlen;
2469fffb55fSDavid Gibson 
2479fffb55fSDavid Gibson 	newlen = strlen(name);
2489fffb55fSDavid Gibson 
2499130ba88SRob Herring 	err = fdt_splice_struct_(fdt, namep, FDT_TAGALIGN(oldlen+1),
2509fffb55fSDavid Gibson 				 FDT_TAGALIGN(newlen+1));
2519fffb55fSDavid Gibson 	if (err)
2529fffb55fSDavid Gibson 		return err;
2539fffb55fSDavid Gibson 
2549fffb55fSDavid Gibson 	memcpy(namep, name, newlen+1);
2559fffb55fSDavid Gibson 	return 0;
2569fffb55fSDavid Gibson }
2579fffb55fSDavid Gibson 
fdt_setprop_placeholder(void * fdt,int nodeoffset,const char * name,int len,void ** prop_data)2584201d057SRob Herring int fdt_setprop_placeholder(void *fdt, int nodeoffset, const char *name,
2594201d057SRob Herring 			    int len, void **prop_data)
2609fffb55fSDavid Gibson {
2619fffb55fSDavid Gibson 	struct fdt_property *prop;
2629fffb55fSDavid Gibson 	int err;
2639fffb55fSDavid Gibson 
264f858927fSRob Herring 	FDT_RW_PROBE(fdt);
2659fffb55fSDavid Gibson 
2669130ba88SRob Herring 	err = fdt_resize_property_(fdt, nodeoffset, name, len, &prop);
2679fffb55fSDavid Gibson 	if (err == -FDT_ERR_NOTFOUND)
2689130ba88SRob Herring 		err = fdt_add_property_(fdt, nodeoffset, name, len, &prop);
2699fffb55fSDavid Gibson 	if (err)
2709fffb55fSDavid Gibson 		return err;
2719fffb55fSDavid Gibson 
2724201d057SRob Herring 	*prop_data = prop->data;
2734201d057SRob Herring 	return 0;
2744201d057SRob Herring }
2754201d057SRob Herring 
fdt_setprop(void * fdt,int nodeoffset,const char * name,const void * val,int len)2764201d057SRob Herring int fdt_setprop(void *fdt, int nodeoffset, const char *name,
2774201d057SRob Herring 		const void *val, int len)
2784201d057SRob Herring {
2794201d057SRob Herring 	void *prop_data;
2804201d057SRob Herring 	int err;
2814201d057SRob Herring 
2824201d057SRob Herring 	err = fdt_setprop_placeholder(fdt, nodeoffset, name, len, &prop_data);
2834201d057SRob Herring 	if (err)
2844201d057SRob Herring 		return err;
2854201d057SRob Herring 
28689d12310SRob Herring 	if (len)
2874201d057SRob Herring 		memcpy(prop_data, val, len);
2889fffb55fSDavid Gibson 	return 0;
2899fffb55fSDavid Gibson }
2909fffb55fSDavid Gibson 
fdt_appendprop(void * fdt,int nodeoffset,const char * name,const void * val,int len)291cd296721SStephen Warren int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
292cd296721SStephen Warren 		   const void *val, int len)
293cd296721SStephen Warren {
294cd296721SStephen Warren 	struct fdt_property *prop;
295cd296721SStephen Warren 	int err, oldlen, newlen;
296cd296721SStephen Warren 
297f858927fSRob Herring 	FDT_RW_PROBE(fdt);
298cd296721SStephen Warren 
299cd296721SStephen Warren 	prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
300cd296721SStephen Warren 	if (prop) {
301cd296721SStephen Warren 		newlen = len + oldlen;
3029130ba88SRob Herring 		err = fdt_splice_struct_(fdt, prop->data,
303cd296721SStephen Warren 					 FDT_TAGALIGN(oldlen),
304cd296721SStephen Warren 					 FDT_TAGALIGN(newlen));
305cd296721SStephen Warren 		if (err)
306cd296721SStephen Warren 			return err;
307cd296721SStephen Warren 		prop->len = cpu_to_fdt32(newlen);
308cd296721SStephen Warren 		memcpy(prop->data + oldlen, val, len);
309cd296721SStephen Warren 	} else {
3109130ba88SRob Herring 		err = fdt_add_property_(fdt, nodeoffset, name, len, &prop);
311cd296721SStephen Warren 		if (err)
312cd296721SStephen Warren 			return err;
313cd296721SStephen Warren 		memcpy(prop->data, val, len);
314cd296721SStephen Warren 	}
315cd296721SStephen Warren 	return 0;
316cd296721SStephen Warren }
317cd296721SStephen Warren 
fdt_delprop(void * fdt,int nodeoffset,const char * name)3189fffb55fSDavid Gibson int fdt_delprop(void *fdt, int nodeoffset, const char *name)
3199fffb55fSDavid Gibson {
3209fffb55fSDavid Gibson 	struct fdt_property *prop;
3219fffb55fSDavid Gibson 	int len, proplen;
3229fffb55fSDavid Gibson 
323f858927fSRob Herring 	FDT_RW_PROBE(fdt);
3249fffb55fSDavid Gibson 
3259fffb55fSDavid Gibson 	prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
3269fffb55fSDavid Gibson 	if (!prop)
3279fffb55fSDavid Gibson 		return len;
3289fffb55fSDavid Gibson 
3299fffb55fSDavid Gibson 	proplen = sizeof(*prop) + FDT_TAGALIGN(len);
3309130ba88SRob Herring 	return fdt_splice_struct_(fdt, prop, proplen, 0);
3319fffb55fSDavid Gibson }
3329fffb55fSDavid Gibson 
fdt_add_subnode_namelen(void * fdt,int parentoffset,const char * name,int namelen)3339fffb55fSDavid Gibson int fdt_add_subnode_namelen(void *fdt, int parentoffset,
3349fffb55fSDavid Gibson 			    const char *name, int namelen)
3359fffb55fSDavid Gibson {
3369fffb55fSDavid Gibson 	struct fdt_node_header *nh;
3379fffb55fSDavid Gibson 	int offset, nextoffset;
3389fffb55fSDavid Gibson 	int nodelen;
3399fffb55fSDavid Gibson 	int err;
3409fffb55fSDavid Gibson 	uint32_t tag;
34147605971SRob Herring 	fdt32_t *endtag;
3429fffb55fSDavid Gibson 
343f858927fSRob Herring 	FDT_RW_PROBE(fdt);
3449fffb55fSDavid Gibson 
3459fffb55fSDavid Gibson 	offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
3469fffb55fSDavid Gibson 	if (offset >= 0)
3479fffb55fSDavid Gibson 		return -FDT_ERR_EXISTS;
3489fffb55fSDavid Gibson 	else if (offset != -FDT_ERR_NOTFOUND)
3499fffb55fSDavid Gibson 		return offset;
3509fffb55fSDavid Gibson 
3519fffb55fSDavid Gibson 	/* Try to place the new node after the parent's properties */
352*a77725a9SRob Herring 	tag = fdt_next_tag(fdt, parentoffset, &nextoffset);
353*a77725a9SRob Herring 	/* the fdt_subnode_offset_namelen() should ensure this never hits */
354*a77725a9SRob Herring 	if (!can_assume(LIBFDT_FLAWLESS) && (tag != FDT_BEGIN_NODE))
355*a77725a9SRob Herring 		return -FDT_ERR_INTERNAL;
3569fffb55fSDavid Gibson 	do {
3579fffb55fSDavid Gibson 		offset = nextoffset;
3589fffb55fSDavid Gibson 		tag = fdt_next_tag(fdt, offset, &nextoffset);
3599fffb55fSDavid Gibson 	} while ((tag == FDT_PROP) || (tag == FDT_NOP));
3609fffb55fSDavid Gibson 
3619130ba88SRob Herring 	nh = fdt_offset_ptr_w_(fdt, offset);
3629fffb55fSDavid Gibson 	nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
3639fffb55fSDavid Gibson 
3649130ba88SRob Herring 	err = fdt_splice_struct_(fdt, nh, 0, nodelen);
3659fffb55fSDavid Gibson 	if (err)
3669fffb55fSDavid Gibson 		return err;
3679fffb55fSDavid Gibson 
3689fffb55fSDavid Gibson 	nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
3699fffb55fSDavid Gibson 	memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
3709fffb55fSDavid Gibson 	memcpy(nh->name, name, namelen);
37147605971SRob Herring 	endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
3729fffb55fSDavid Gibson 	*endtag = cpu_to_fdt32(FDT_END_NODE);
3739fffb55fSDavid Gibson 
3749fffb55fSDavid Gibson 	return offset;
3759fffb55fSDavid Gibson }
3769fffb55fSDavid Gibson 
fdt_add_subnode(void * fdt,int parentoffset,const char * name)3779fffb55fSDavid Gibson int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
3789fffb55fSDavid Gibson {
3799fffb55fSDavid Gibson 	return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
3809fffb55fSDavid Gibson }
3819fffb55fSDavid Gibson 
fdt_del_node(void * fdt,int nodeoffset)3829fffb55fSDavid Gibson int fdt_del_node(void *fdt, int nodeoffset)
3839fffb55fSDavid Gibson {
3849fffb55fSDavid Gibson 	int endoffset;
3859fffb55fSDavid Gibson 
386f858927fSRob Herring 	FDT_RW_PROBE(fdt);
3879fffb55fSDavid Gibson 
3889130ba88SRob Herring 	endoffset = fdt_node_end_offset_(fdt, nodeoffset);
3899fffb55fSDavid Gibson 	if (endoffset < 0)
3909fffb55fSDavid Gibson 		return endoffset;
3919fffb55fSDavid Gibson 
3929130ba88SRob Herring 	return fdt_splice_struct_(fdt, fdt_offset_ptr_w_(fdt, nodeoffset),
3939fffb55fSDavid Gibson 				  endoffset - nodeoffset, 0);
3949fffb55fSDavid Gibson }
3959fffb55fSDavid Gibson 
fdt_packblocks_(const char * old,char * new,int mem_rsv_size,int struct_size,int strings_size)3969130ba88SRob Herring static void fdt_packblocks_(const char *old, char *new,
397*a77725a9SRob Herring 			    int mem_rsv_size,
398*a77725a9SRob Herring 			    int struct_size,
399*a77725a9SRob Herring 			    int strings_size)
4009fffb55fSDavid Gibson {
4019fffb55fSDavid Gibson 	int mem_rsv_off, struct_off, strings_off;
4029fffb55fSDavid Gibson 
4039fffb55fSDavid Gibson 	mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
4049fffb55fSDavid Gibson 	struct_off = mem_rsv_off + mem_rsv_size;
4059fffb55fSDavid Gibson 	strings_off = struct_off + struct_size;
4069fffb55fSDavid Gibson 
4079fffb55fSDavid Gibson 	memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
4089fffb55fSDavid Gibson 	fdt_set_off_mem_rsvmap(new, mem_rsv_off);
4099fffb55fSDavid Gibson 
4109fffb55fSDavid Gibson 	memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
4119fffb55fSDavid Gibson 	fdt_set_off_dt_struct(new, struct_off);
4129fffb55fSDavid Gibson 	fdt_set_size_dt_struct(new, struct_size);
4139fffb55fSDavid Gibson 
414*a77725a9SRob Herring 	memmove(new + strings_off, old + fdt_off_dt_strings(old), strings_size);
4159fffb55fSDavid Gibson 	fdt_set_off_dt_strings(new, strings_off);
4169fffb55fSDavid Gibson 	fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
4179fffb55fSDavid Gibson }
4189fffb55fSDavid Gibson 
fdt_open_into(const void * fdt,void * buf,int bufsize)4199fffb55fSDavid Gibson int fdt_open_into(const void *fdt, void *buf, int bufsize)
4209fffb55fSDavid Gibson {
4219fffb55fSDavid Gibson 	int err;
4229fffb55fSDavid Gibson 	int mem_rsv_size, struct_size;
4239fffb55fSDavid Gibson 	int newsize;
4249fffb55fSDavid Gibson 	const char *fdtstart = fdt;
4259fffb55fSDavid Gibson 	const char *fdtend = fdtstart + fdt_totalsize(fdt);
4269fffb55fSDavid Gibson 	char *tmp;
4279fffb55fSDavid Gibson 
428f858927fSRob Herring 	FDT_RO_PROBE(fdt);
4299fffb55fSDavid Gibson 
4309fffb55fSDavid Gibson 	mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
4319fffb55fSDavid Gibson 		* sizeof(struct fdt_reserve_entry);
4329fffb55fSDavid Gibson 
433d047cd8aSRob Herring 	if (can_assume(LATEST) || fdt_version(fdt) >= 17) {
4349fffb55fSDavid Gibson 		struct_size = fdt_size_dt_struct(fdt);
43579edff12SRob Herring 	} else if (fdt_version(fdt) == 16) {
4369fffb55fSDavid Gibson 		struct_size = 0;
4379fffb55fSDavid Gibson 		while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
4389fffb55fSDavid Gibson 			;
439cd296721SStephen Warren 		if (struct_size < 0)
440cd296721SStephen Warren 			return struct_size;
44179edff12SRob Herring 	} else {
44279edff12SRob Herring 		return -FDT_ERR_BADVERSION;
4439fffb55fSDavid Gibson 	}
4449fffb55fSDavid Gibson 
4453eb619b2SRob Herring 	if (can_assume(LIBFDT_ORDER) ||
446d047cd8aSRob Herring 	    !fdt_blocks_misordered_(fdt, mem_rsv_size, struct_size)) {
4479fffb55fSDavid Gibson 		/* no further work necessary */
4489fffb55fSDavid Gibson 		err = fdt_move(fdt, buf, bufsize);
4499fffb55fSDavid Gibson 		if (err)
4509fffb55fSDavid Gibson 			return err;
4519fffb55fSDavid Gibson 		fdt_set_version(buf, 17);
4529fffb55fSDavid Gibson 		fdt_set_size_dt_struct(buf, struct_size);
4539fffb55fSDavid Gibson 		fdt_set_totalsize(buf, bufsize);
4549fffb55fSDavid Gibson 		return 0;
4559fffb55fSDavid Gibson 	}
4569fffb55fSDavid Gibson 
4579fffb55fSDavid Gibson 	/* Need to reorder */
4589fffb55fSDavid Gibson 	newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
4599fffb55fSDavid Gibson 		+ struct_size + fdt_size_dt_strings(fdt);
4609fffb55fSDavid Gibson 
4619fffb55fSDavid Gibson 	if (bufsize < newsize)
4629fffb55fSDavid Gibson 		return -FDT_ERR_NOSPACE;
4639fffb55fSDavid Gibson 
4649fffb55fSDavid Gibson 	/* First attempt to build converted tree at beginning of buffer */
4659fffb55fSDavid Gibson 	tmp = buf;
4669fffb55fSDavid Gibson 	/* But if that overlaps with the old tree... */
4679fffb55fSDavid Gibson 	if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
4689fffb55fSDavid Gibson 		/* Try right after the old tree instead */
4699fffb55fSDavid Gibson 		tmp = (char *)(uintptr_t)fdtend;
4709fffb55fSDavid Gibson 		if ((tmp + newsize) > ((char *)buf + bufsize))
4719fffb55fSDavid Gibson 			return -FDT_ERR_NOSPACE;
4729fffb55fSDavid Gibson 	}
4739fffb55fSDavid Gibson 
474*a77725a9SRob Herring 	fdt_packblocks_(fdt, tmp, mem_rsv_size, struct_size,
475*a77725a9SRob Herring 			fdt_size_dt_strings(fdt));
4769fffb55fSDavid Gibson 	memmove(buf, tmp, newsize);
4779fffb55fSDavid Gibson 
4789fffb55fSDavid Gibson 	fdt_set_magic(buf, FDT_MAGIC);
4799fffb55fSDavid Gibson 	fdt_set_totalsize(buf, bufsize);
4809fffb55fSDavid Gibson 	fdt_set_version(buf, 17);
4819fffb55fSDavid Gibson 	fdt_set_last_comp_version(buf, 16);
4829fffb55fSDavid Gibson 	fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
4839fffb55fSDavid Gibson 
4849fffb55fSDavid Gibson 	return 0;
4859fffb55fSDavid Gibson }
4869fffb55fSDavid Gibson 
fdt_pack(void * fdt)4879fffb55fSDavid Gibson int fdt_pack(void *fdt)
4889fffb55fSDavid Gibson {
4899fffb55fSDavid Gibson 	int mem_rsv_size;
4909fffb55fSDavid Gibson 
491f858927fSRob Herring 	FDT_RW_PROBE(fdt);
4929fffb55fSDavid Gibson 
4939fffb55fSDavid Gibson 	mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
4949fffb55fSDavid Gibson 		* sizeof(struct fdt_reserve_entry);
495*a77725a9SRob Herring 	fdt_packblocks_(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt),
496*a77725a9SRob Herring 			fdt_size_dt_strings(fdt));
4979130ba88SRob Herring 	fdt_set_totalsize(fdt, fdt_data_size_(fdt));
4989fffb55fSDavid Gibson 
4999fffb55fSDavid Gibson 	return 0;
5009fffb55fSDavid Gibson }
501