xref: /rk3399_rockchip-uboot/scripts/dtc/libfdt/fdt_addresses.c (revision 14df9b242f1aeec4707933666e9581796e2e1597)
1*14df9b24SMasahiro Yamada /*
2*14df9b24SMasahiro Yamada  * libfdt - Flat Device Tree manipulation
3*14df9b24SMasahiro Yamada  * Copyright (C) 2014 David Gibson <david@gibson.dropbear.id.au>
4*14df9b24SMasahiro Yamada  *
5*14df9b24SMasahiro Yamada  * libfdt is dual licensed: you can use it either under the terms of
6*14df9b24SMasahiro Yamada  * the GPL, or the BSD license, at your option.
7*14df9b24SMasahiro Yamada  *
8*14df9b24SMasahiro Yamada  *  a) This library is free software; you can redistribute it and/or
9*14df9b24SMasahiro Yamada  *     modify it under the terms of the GNU General Public License as
10*14df9b24SMasahiro Yamada  *     published by the Free Software Foundation; either version 2 of the
11*14df9b24SMasahiro Yamada  *     License, or (at your option) any later version.
12*14df9b24SMasahiro Yamada  *
13*14df9b24SMasahiro Yamada  *     This library is distributed in the hope that it will be useful,
14*14df9b24SMasahiro Yamada  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
15*14df9b24SMasahiro Yamada  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*14df9b24SMasahiro Yamada  *     GNU General Public License for more details.
17*14df9b24SMasahiro Yamada  *
18*14df9b24SMasahiro Yamada  *     You should have received a copy of the GNU General Public
19*14df9b24SMasahiro Yamada  *     License along with this library; if not, write to the Free
20*14df9b24SMasahiro Yamada  *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21*14df9b24SMasahiro Yamada  *     MA 02110-1301 USA
22*14df9b24SMasahiro Yamada  *
23*14df9b24SMasahiro Yamada  * Alternatively,
24*14df9b24SMasahiro Yamada  *
25*14df9b24SMasahiro Yamada  *  b) Redistribution and use in source and binary forms, with or
26*14df9b24SMasahiro Yamada  *     without modification, are permitted provided that the following
27*14df9b24SMasahiro Yamada  *     conditions are met:
28*14df9b24SMasahiro Yamada  *
29*14df9b24SMasahiro Yamada  *     1. Redistributions of source code must retain the above
30*14df9b24SMasahiro Yamada  *        copyright notice, this list of conditions and the following
31*14df9b24SMasahiro Yamada  *        disclaimer.
32*14df9b24SMasahiro Yamada  *     2. Redistributions in binary form must reproduce the above
33*14df9b24SMasahiro Yamada  *        copyright notice, this list of conditions and the following
34*14df9b24SMasahiro Yamada  *        disclaimer in the documentation and/or other materials
35*14df9b24SMasahiro Yamada  *        provided with the distribution.
36*14df9b24SMasahiro Yamada  *
37*14df9b24SMasahiro Yamada  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38*14df9b24SMasahiro Yamada  *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39*14df9b24SMasahiro Yamada  *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40*14df9b24SMasahiro Yamada  *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41*14df9b24SMasahiro Yamada  *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
42*14df9b24SMasahiro Yamada  *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43*14df9b24SMasahiro Yamada  *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44*14df9b24SMasahiro Yamada  *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45*14df9b24SMasahiro Yamada  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46*14df9b24SMasahiro Yamada  *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47*14df9b24SMasahiro Yamada  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48*14df9b24SMasahiro Yamada  *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49*14df9b24SMasahiro Yamada  *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50*14df9b24SMasahiro Yamada  */
51*14df9b24SMasahiro Yamada #include "libfdt_env.h"
52*14df9b24SMasahiro Yamada 
53*14df9b24SMasahiro Yamada #include <fdt.h>
54*14df9b24SMasahiro Yamada #include <libfdt.h>
55*14df9b24SMasahiro Yamada 
56*14df9b24SMasahiro Yamada #include "libfdt_internal.h"
57*14df9b24SMasahiro Yamada 
fdt_address_cells(const void * fdt,int nodeoffset)58*14df9b24SMasahiro Yamada int fdt_address_cells(const void *fdt, int nodeoffset)
59*14df9b24SMasahiro Yamada {
60*14df9b24SMasahiro Yamada 	const fdt32_t *ac;
61*14df9b24SMasahiro Yamada 	int val;
62*14df9b24SMasahiro Yamada 	int len;
63*14df9b24SMasahiro Yamada 
64*14df9b24SMasahiro Yamada 	ac = fdt_getprop(fdt, nodeoffset, "#address-cells", &len);
65*14df9b24SMasahiro Yamada 	if (!ac)
66*14df9b24SMasahiro Yamada 		return 2;
67*14df9b24SMasahiro Yamada 
68*14df9b24SMasahiro Yamada 	if (len != sizeof(*ac))
69*14df9b24SMasahiro Yamada 		return -FDT_ERR_BADNCELLS;
70*14df9b24SMasahiro Yamada 
71*14df9b24SMasahiro Yamada 	val = fdt32_to_cpu(*ac);
72*14df9b24SMasahiro Yamada 	if ((val <= 0) || (val > FDT_MAX_NCELLS))
73*14df9b24SMasahiro Yamada 		return -FDT_ERR_BADNCELLS;
74*14df9b24SMasahiro Yamada 
75*14df9b24SMasahiro Yamada 	return val;
76*14df9b24SMasahiro Yamada }
77*14df9b24SMasahiro Yamada 
fdt_size_cells(const void * fdt,int nodeoffset)78*14df9b24SMasahiro Yamada int fdt_size_cells(const void *fdt, int nodeoffset)
79*14df9b24SMasahiro Yamada {
80*14df9b24SMasahiro Yamada 	const fdt32_t *sc;
81*14df9b24SMasahiro Yamada 	int val;
82*14df9b24SMasahiro Yamada 	int len;
83*14df9b24SMasahiro Yamada 
84*14df9b24SMasahiro Yamada 	sc = fdt_getprop(fdt, nodeoffset, "#size-cells", &len);
85*14df9b24SMasahiro Yamada 	if (!sc)
86*14df9b24SMasahiro Yamada 		return 2;
87*14df9b24SMasahiro Yamada 
88*14df9b24SMasahiro Yamada 	if (len != sizeof(*sc))
89*14df9b24SMasahiro Yamada 		return -FDT_ERR_BADNCELLS;
90*14df9b24SMasahiro Yamada 
91*14df9b24SMasahiro Yamada 	val = fdt32_to_cpu(*sc);
92*14df9b24SMasahiro Yamada 	if ((val < 0) || (val > FDT_MAX_NCELLS))
93*14df9b24SMasahiro Yamada 		return -FDT_ERR_BADNCELLS;
94*14df9b24SMasahiro Yamada 
95*14df9b24SMasahiro Yamada 	return val;
96*14df9b24SMasahiro Yamada }
97