xref: /openbmc/u-boot/lib/libfdt/fdt_addresses.c (revision ee7bb5be)
1 /*
2  * libfdt - Flat Device Tree manipulation
3  * Copyright (C) 2014 David Gibson <david@gibson.dropbear.id.au>
4  * SPDX-License-Identifier:	GPL-2.0+ BSD-2-Clause
5  */
6 #include "libfdt_env.h"
7 
8 #ifndef USE_HOSTCC
9 #include <fdt.h>
10 #include <libfdt.h>
11 #else
12 #include "fdt_host.h"
13 #endif
14 
15 #include "libfdt_internal.h"
16 
17 int fdt_address_cells(const void *fdt, int nodeoffset)
18 {
19 	const fdt32_t *ac;
20 	int val;
21 	int len;
22 
23 	ac = fdt_getprop(fdt, nodeoffset, "#address-cells", &len);
24 	if (!ac)
25 		return 2;
26 
27 	if (len != sizeof(*ac))
28 		return -FDT_ERR_BADNCELLS;
29 
30 	val = fdt32_to_cpu(*ac);
31 	if ((val <= 0) || (val > FDT_MAX_NCELLS))
32 		return -FDT_ERR_BADNCELLS;
33 
34 	return val;
35 }
36 
37 int fdt_size_cells(const void *fdt, int nodeoffset)
38 {
39 	const fdt32_t *sc;
40 	int val;
41 	int len;
42 
43 	sc = fdt_getprop(fdt, nodeoffset, "#size-cells", &len);
44 	if (!sc)
45 		return 2;
46 
47 	if (len != sizeof(*sc))
48 		return -FDT_ERR_BADNCELLS;
49 
50 	val = fdt32_to_cpu(*sc);
51 	if ((val < 0) || (val > FDT_MAX_NCELLS))
52 		return -FDT_ERR_BADNCELLS;
53 
54 	return val;
55 }
56