1328459Skevans/*
2328459Skevans * libfdt - Flat Device Tree manipulation
3328459Skevans * Copyright (C) 2014 David Gibson <david@gibson.dropbear.id.au>
4328459Skevans *
5328459Skevans * libfdt is dual licensed: you can use it either under the terms of
6328459Skevans * the GPL, or the BSD license, at your option.
7328459Skevans *
8328459Skevans *  a) This library is free software; you can redistribute it and/or
9328459Skevans *     modify it under the terms of the GNU General Public License as
10328459Skevans *     published by the Free Software Foundation; either version 2 of the
11328459Skevans *     License, or (at your option) any later version.
12328459Skevans *
13328459Skevans *     This library is distributed in the hope that it will be useful,
14328459Skevans *     but WITHOUT ANY WARRANTY; without even the implied warranty of
15328459Skevans *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16328459Skevans *     GNU General Public License for more details.
17328459Skevans *
18328459Skevans *     You should have received a copy of the GNU General Public
19328459Skevans *     License along with this library; if not, write to the Free
20328459Skevans *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21328459Skevans *     MA 02110-1301 USA
22328459Skevans *
23328459Skevans * Alternatively,
24328459Skevans *
25328459Skevans *  b) Redistribution and use in source and binary forms, with or
26328459Skevans *     without modification, are permitted provided that the following
27328459Skevans *     conditions are met:
28328459Skevans *
29328459Skevans *     1. Redistributions of source code must retain the above
30328459Skevans *        copyright notice, this list of conditions and the following
31328459Skevans *        disclaimer.
32328459Skevans *     2. Redistributions in binary form must reproduce the above
33328459Skevans *        copyright notice, this list of conditions and the following
34328459Skevans *        disclaimer in the documentation and/or other materials
35328459Skevans *        provided with the distribution.
36328459Skevans *
37328459Skevans *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38328459Skevans *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39328459Skevans *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40328459Skevans *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41328459Skevans *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
42328459Skevans *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43328459Skevans *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44328459Skevans *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45328459Skevans *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46328459Skevans *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47328459Skevans *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48328459Skevans *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49328459Skevans *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50328459Skevans */
51328459Skevans#include "libfdt_env.h"
52328459Skevans
53328459Skevans#include <fdt.h>
54328459Skevans#include <libfdt.h>
55328459Skevans
56328459Skevans#include "libfdt_internal.h"
57328459Skevans
58328459Skevansint fdt_address_cells(const void *fdt, int nodeoffset)
59328459Skevans{
60328459Skevans	const fdt32_t *ac;
61328459Skevans	int val;
62328459Skevans	int len;
63328459Skevans
64328459Skevans	ac = fdt_getprop(fdt, nodeoffset, "#address-cells", &len);
65328459Skevans	if (!ac)
66328459Skevans		return 2;
67328459Skevans
68328459Skevans	if (len != sizeof(*ac))
69328459Skevans		return -FDT_ERR_BADNCELLS;
70328459Skevans
71328459Skevans	val = fdt32_to_cpu(*ac);
72328459Skevans	if ((val <= 0) || (val > FDT_MAX_NCELLS))
73328459Skevans		return -FDT_ERR_BADNCELLS;
74328459Skevans
75328459Skevans	return val;
76328459Skevans}
77328459Skevans
78328459Skevansint fdt_size_cells(const void *fdt, int nodeoffset)
79328459Skevans{
80328459Skevans	const fdt32_t *sc;
81328459Skevans	int val;
82328459Skevans	int len;
83328459Skevans
84328459Skevans	sc = fdt_getprop(fdt, nodeoffset, "#size-cells", &len);
85328459Skevans	if (!sc)
86328459Skevans		return 2;
87328459Skevans
88328459Skevans	if (len != sizeof(*sc))
89328459Skevans		return -FDT_ERR_BADNCELLS;
90328459Skevans
91328459Skevans	val = fdt32_to_cpu(*sc);
92328459Skevans	if ((val < 0) || (val > FDT_MAX_NCELLS))
93328459Skevans		return -FDT_ERR_BADNCELLS;
94328459Skevans
95328459Skevans	return val;
96328459Skevans}
97