xref: /openbmc/u-boot/scripts/dtc/libfdt/fdt_wip.c (revision db405d1980e0e3bfdd629399d1dfe457f1f32646)
1c0e032e0STom Rini /*
2c0e032e0STom Rini  * libfdt - Flat Device Tree manipulation
3c0e032e0STom Rini  * Copyright (C) 2006 David Gibson, IBM Corporation.
4c0e032e0STom Rini  *
5c0e032e0STom Rini  * libfdt is dual licensed: you can use it either under the terms of
6c0e032e0STom Rini  * the GPL, or the BSD license, at your option.
7c0e032e0STom Rini  *
8c0e032e0STom Rini  *  a) This library is free software; you can redistribute it and/or
9c0e032e0STom Rini  *     modify it under the terms of the GNU General Public License as
10c0e032e0STom Rini  *     published by the Free Software Foundation; either version 2 of the
11c0e032e0STom Rini  *     License, or (at your option) any later version.
12c0e032e0STom Rini  *
13c0e032e0STom Rini  *     This library is distributed in the hope that it will be useful,
14c0e032e0STom Rini  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
15c0e032e0STom Rini  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16c0e032e0STom Rini  *     GNU General Public License for more details.
17c0e032e0STom Rini  *
18c0e032e0STom Rini  *     You should have received a copy of the GNU General Public
19c0e032e0STom Rini  *     License along with this library; if not, write to the Free
20c0e032e0STom Rini  *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21c0e032e0STom Rini  *     MA 02110-1301 USA
22c0e032e0STom Rini  *
23c0e032e0STom Rini  * Alternatively,
24c0e032e0STom Rini  *
25c0e032e0STom Rini  *  b) Redistribution and use in source and binary forms, with or
26c0e032e0STom Rini  *     without modification, are permitted provided that the following
27c0e032e0STom Rini  *     conditions are met:
28c0e032e0STom Rini  *
29c0e032e0STom Rini  *     1. Redistributions of source code must retain the above
30c0e032e0STom Rini  *        copyright notice, this list of conditions and the following
31c0e032e0STom Rini  *        disclaimer.
32c0e032e0STom Rini  *     2. Redistributions in binary form must reproduce the above
33c0e032e0STom Rini  *        copyright notice, this list of conditions and the following
34c0e032e0STom Rini  *        disclaimer in the documentation and/or other materials
35c0e032e0STom Rini  *        provided with the distribution.
36c0e032e0STom Rini  *
37c0e032e0STom Rini  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38c0e032e0STom Rini  *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39c0e032e0STom Rini  *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40c0e032e0STom Rini  *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41c0e032e0STom Rini  *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
42c0e032e0STom Rini  *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43c0e032e0STom Rini  *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44c0e032e0STom Rini  *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45c0e032e0STom Rini  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46c0e032e0STom Rini  *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47c0e032e0STom Rini  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48c0e032e0STom Rini  *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49c0e032e0STom Rini  *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50c0e032e0STom Rini  */
51c0e032e0STom Rini #include "libfdt_env.h"
52c0e032e0STom Rini 
53c0e032e0STom Rini #include <fdt.h>
54c0e032e0STom Rini #include <libfdt.h>
55c0e032e0STom Rini 
56c0e032e0STom Rini #include "libfdt_internal.h"
57c0e032e0STom Rini 
fdt_setprop_inplace_namelen_partial(void * fdt,int nodeoffset,const char * name,int namelen,uint32_t idx,const void * val,int len)58c0e032e0STom Rini int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
59c0e032e0STom Rini 					const char *name, int namelen,
60c0e032e0STom Rini 					uint32_t idx, const void *val,
61c0e032e0STom Rini 					int len)
62c0e032e0STom Rini {
63c0e032e0STom Rini 	void *propval;
64c0e032e0STom Rini 	int proplen;
65c0e032e0STom Rini 
66c0e032e0STom Rini 	propval = fdt_getprop_namelen_w(fdt, nodeoffset, name, namelen,
67c0e032e0STom Rini 					&proplen);
68c0e032e0STom Rini 	if (!propval)
69c0e032e0STom Rini 		return proplen;
70c0e032e0STom Rini 
71c0e032e0STom Rini 	if (proplen < (len + idx))
72c0e032e0STom Rini 		return -FDT_ERR_NOSPACE;
73c0e032e0STom Rini 
74c0e032e0STom Rini 	memcpy((char *)propval + idx, val, len);
75c0e032e0STom Rini 	return 0;
76c0e032e0STom Rini }
77c0e032e0STom Rini 
fdt_setprop_inplace(void * fdt,int nodeoffset,const char * name,const void * val,int len)78c0e032e0STom Rini int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
79c0e032e0STom Rini 			const void *val, int len)
80c0e032e0STom Rini {
81c0e032e0STom Rini 	const void *propval;
82c0e032e0STom Rini 	int proplen;
83c0e032e0STom Rini 
84c0e032e0STom Rini 	propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
85c0e032e0STom Rini 	if (!propval)
86c0e032e0STom Rini 		return proplen;
87c0e032e0STom Rini 
88c0e032e0STom Rini 	if (proplen != len)
89c0e032e0STom Rini 		return -FDT_ERR_NOSPACE;
90c0e032e0STom Rini 
91c0e032e0STom Rini 	return fdt_setprop_inplace_namelen_partial(fdt, nodeoffset, name,
92c0e032e0STom Rini 						   strlen(name), 0,
93c0e032e0STom Rini 						   val, len);
94c0e032e0STom Rini }
95c0e032e0STom Rini 
fdt_nop_region_(void * start,int len)96*db405d19SRob Herring static void fdt_nop_region_(void *start, int len)
97c0e032e0STom Rini {
98c0e032e0STom Rini 	fdt32_t *p;
99c0e032e0STom Rini 
100c0e032e0STom Rini 	for (p = start; (char *)p < ((char *)start + len); p++)
101c0e032e0STom Rini 		*p = cpu_to_fdt32(FDT_NOP);
102c0e032e0STom Rini }
103c0e032e0STom Rini 
fdt_nop_property(void * fdt,int nodeoffset,const char * name)104c0e032e0STom Rini int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
105c0e032e0STom Rini {
106c0e032e0STom Rini 	struct fdt_property *prop;
107c0e032e0STom Rini 	int len;
108c0e032e0STom Rini 
109c0e032e0STom Rini 	prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
110c0e032e0STom Rini 	if (!prop)
111c0e032e0STom Rini 		return len;
112c0e032e0STom Rini 
113*db405d19SRob Herring 	fdt_nop_region_(prop, len + sizeof(*prop));
114c0e032e0STom Rini 
115c0e032e0STom Rini 	return 0;
116c0e032e0STom Rini }
117c0e032e0STom Rini 
fdt_node_end_offset_(void * fdt,int offset)118*db405d19SRob Herring int fdt_node_end_offset_(void *fdt, int offset)
119c0e032e0STom Rini {
120c0e032e0STom Rini 	int depth = 0;
121c0e032e0STom Rini 
122c0e032e0STom Rini 	while ((offset >= 0) && (depth >= 0))
123c0e032e0STom Rini 		offset = fdt_next_node(fdt, offset, &depth);
124c0e032e0STom Rini 
125c0e032e0STom Rini 	return offset;
126c0e032e0STom Rini }
127c0e032e0STom Rini 
fdt_nop_node(void * fdt,int nodeoffset)128c0e032e0STom Rini int fdt_nop_node(void *fdt, int nodeoffset)
129c0e032e0STom Rini {
130c0e032e0STom Rini 	int endoffset;
131c0e032e0STom Rini 
132*db405d19SRob Herring 	endoffset = fdt_node_end_offset_(fdt, nodeoffset);
133c0e032e0STom Rini 	if (endoffset < 0)
134c0e032e0STom Rini 		return endoffset;
135c0e032e0STom Rini 
136*db405d19SRob Herring 	fdt_nop_region_(fdt_offset_ptr_w(fdt, nodeoffset, 0),
137c0e032e0STom Rini 			endoffset - nodeoffset);
138c0e032e0STom Rini 	return 0;
139c0e032e0STom Rini }
140