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