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