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