1 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2014 David Gibson <david@gibson.dropbear.id.au>
5 * Copyright (C) 2018 embedded brains GmbH
7 #include "libfdt_env.h"
12 #include "libfdt_internal.h"
14 static int fdt_cells(const void *fdt
, int nodeoffset
, const char *name
)
20 c
= fdt_getprop(fdt
, nodeoffset
, name
, &len
);
24 if (len
!= sizeof(*c
))
25 return -FDT_ERR_BADNCELLS
;
27 val
= fdt32_to_cpu(*c
);
28 if (val
> FDT_MAX_NCELLS
)
29 return -FDT_ERR_BADNCELLS
;
34 int fdt_address_cells(const void *fdt
, int nodeoffset
)
38 val
= fdt_cells(fdt
, nodeoffset
, "#address-cells");
40 return -FDT_ERR_BADNCELLS
;
41 if (val
== -FDT_ERR_NOTFOUND
)
46 int fdt_size_cells(const void *fdt
, int nodeoffset
)
50 val
= fdt_cells(fdt
, nodeoffset
, "#size-cells");
51 if (val
== -FDT_ERR_NOTFOUND
)
56 /* This function assumes that [address|size]_cells is 1 or 2 */
57 int fdt_appendprop_addrrange(void *fdt
, int parent
, int nodeoffset
,
58 const char *name
, uint64_t addr
, uint64_t size
)
60 int addr_cells
, size_cells
, ret
;
61 uint8_t data
[sizeof(fdt64_t
) * 2], *prop
;
63 ret
= fdt_address_cells(fdt
, parent
);
68 ret
= fdt_size_cells(fdt
, parent
);
73 /* check validity of address */
75 if (addr_cells
== 1) {
76 if ((addr
> UINT32_MAX
) || ((UINT32_MAX
+ 1 - addr
) < size
))
77 return -FDT_ERR_BADVALUE
;
79 fdt32_st(prop
, (uint32_t)addr
);
80 } else if (addr_cells
== 2) {
83 return -FDT_ERR_BADNCELLS
;
86 /* check validity of size */
87 prop
+= addr_cells
* sizeof(fdt32_t
);
88 if (size_cells
== 1) {
89 if (size
> UINT32_MAX
)
90 return -FDT_ERR_BADVALUE
;
92 fdt32_st(prop
, (uint32_t)size
);
93 } else if (size_cells
== 2) {
96 return -FDT_ERR_BADNCELLS
;
99 return fdt_appendprop(fdt
, nodeoffset
, name
, data
,
100 (addr_cells
+ size_cells
) * sizeof(fdt32_t
));