1314985Sgonzo/*
2314985Sgonzo * libfdt - Flat Device Tree manipulation
3314985Sgonzo * Copyright (C) 2014 David Gibson <david@gibson.dropbear.id.au>
4314985Sgonzo *
5314985Sgonzo * libfdt is dual licensed: you can use it either under the terms of
6314985Sgonzo * the GPL, or the BSD license, at your option.
7314985Sgonzo *
8314985Sgonzo *  a) This library is free software; you can redistribute it and/or
9314985Sgonzo *     modify it under the terms of the GNU General Public License as
10314985Sgonzo *     published by the Free Software Foundation; either version 2 of the
11314985Sgonzo *     License, or (at your option) any later version.
12314985Sgonzo *
13314985Sgonzo *     This library is distributed in the hope that it will be useful,
14314985Sgonzo *     but WITHOUT ANY WARRANTY; without even the implied warranty of
15314985Sgonzo *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16314985Sgonzo *     GNU General Public License for more details.
17314985Sgonzo *
18314985Sgonzo *     You should have received a copy of the GNU General Public
19314985Sgonzo *     License along with this library; if not, write to the Free
20314985Sgonzo *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21314985Sgonzo *     MA 02110-1301 USA
22314985Sgonzo *
23314985Sgonzo * Alternatively,
24314985Sgonzo *
25314985Sgonzo *  b) Redistribution and use in source and binary forms, with or
26314985Sgonzo *     without modification, are permitted provided that the following
27314985Sgonzo *     conditions are met:
28314985Sgonzo *
29314985Sgonzo *     1. Redistributions of source code must retain the above
30314985Sgonzo *        copyright notice, this list of conditions and the following
31314985Sgonzo *        disclaimer.
32314985Sgonzo *     2. Redistributions in binary form must reproduce the above
33314985Sgonzo *        copyright notice, this list of conditions and the following
34314985Sgonzo *        disclaimer in the documentation and/or other materials
35314985Sgonzo *        provided with the distribution.
36314985Sgonzo *
37314985Sgonzo *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38314985Sgonzo *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39314985Sgonzo *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40314985Sgonzo *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41314985Sgonzo *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
42314985Sgonzo *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43314985Sgonzo *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44314985Sgonzo *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45314985Sgonzo *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46314985Sgonzo *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47314985Sgonzo *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48314985Sgonzo *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49314985Sgonzo *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50314985Sgonzo */
51314985Sgonzo#include "libfdt_env.h"
52314985Sgonzo
53314985Sgonzo#include <fdt.h>
54314985Sgonzo#include <libfdt.h>
55314985Sgonzo
56314985Sgonzo#include "libfdt_internal.h"
57314985Sgonzo
58314985Sgonzoint fdt_address_cells(const void *fdt, int nodeoffset)
59314985Sgonzo{
60314985Sgonzo	const fdt32_t *ac;
61314985Sgonzo	int val;
62314985Sgonzo	int len;
63314985Sgonzo
64314985Sgonzo	ac = fdt_getprop(fdt, nodeoffset, "#address-cells", &len);
65314985Sgonzo	if (!ac)
66314985Sgonzo		return 2;
67314985Sgonzo
68314985Sgonzo	if (len != sizeof(*ac))
69314985Sgonzo		return -FDT_ERR_BADNCELLS;
70314985Sgonzo
71314985Sgonzo	val = fdt32_to_cpu(*ac);
72314985Sgonzo	if ((val <= 0) || (val > FDT_MAX_NCELLS))
73314985Sgonzo		return -FDT_ERR_BADNCELLS;
74314985Sgonzo
75314985Sgonzo	return val;
76314985Sgonzo}
77314985Sgonzo
78314985Sgonzoint fdt_size_cells(const void *fdt, int nodeoffset)
79314985Sgonzo{
80314985Sgonzo	const fdt32_t *sc;
81314985Sgonzo	int val;
82314985Sgonzo	int len;
83314985Sgonzo
84314985Sgonzo	sc = fdt_getprop(fdt, nodeoffset, "#size-cells", &len);
85314985Sgonzo	if (!sc)
86314985Sgonzo		return 2;
87314985Sgonzo
88314985Sgonzo	if (len != sizeof(*sc))
89314985Sgonzo		return -FDT_ERR_BADNCELLS;
90314985Sgonzo
91314985Sgonzo	val = fdt32_to_cpu(*sc);
92314985Sgonzo	if ((val < 0) || (val > FDT_MAX_NCELLS))
93314985Sgonzo		return -FDT_ERR_BADNCELLS;
94314985Sgonzo
95314985Sgonzo	return val;
96314985Sgonzo}
97