1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * libfdt - Flat Device Tree manipulation
3*4882a593Smuzhiyun * Copyright (C) 2014 David Gibson <david@gibson.dropbear.id.au>
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_address_cells(const void * fdt,int nodeoffset)58*4882a593Smuzhiyun int fdt_address_cells(const void *fdt, int nodeoffset)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun const fdt32_t *ac;
61*4882a593Smuzhiyun int val;
62*4882a593Smuzhiyun int len;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun ac = fdt_getprop(fdt, nodeoffset, "#address-cells", &len);
65*4882a593Smuzhiyun if (!ac)
66*4882a593Smuzhiyun return 2;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun if (len != sizeof(*ac))
69*4882a593Smuzhiyun return -FDT_ERR_BADNCELLS;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun val = fdt32_to_cpu(*ac);
72*4882a593Smuzhiyun if ((val <= 0) || (val > FDT_MAX_NCELLS))
73*4882a593Smuzhiyun return -FDT_ERR_BADNCELLS;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun return val;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
fdt_size_cells(const void * fdt,int nodeoffset)78*4882a593Smuzhiyun int fdt_size_cells(const void *fdt, int nodeoffset)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun const fdt32_t *sc;
81*4882a593Smuzhiyun int val;
82*4882a593Smuzhiyun int len;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun sc = fdt_getprop(fdt, nodeoffset, "#size-cells", &len);
85*4882a593Smuzhiyun if (!sc)
86*4882a593Smuzhiyun return 2;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun if (len != sizeof(*sc))
89*4882a593Smuzhiyun return -FDT_ERR_BADNCELLS;
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun val = fdt32_to_cpu(*sc);
92*4882a593Smuzhiyun if ((val < 0) || (val > FDT_MAX_NCELLS))
93*4882a593Smuzhiyun return -FDT_ERR_BADNCELLS;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun return val;
96*4882a593Smuzhiyun }
97