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