1*3e6106c4SLoGin // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
2*3e6106c4SLoGin /*
3*3e6106c4SLoGin * libfdt - Flat Device Tree manipulation
4*3e6106c4SLoGin * Copyright (C) 2014 David Gibson <david@gibson.dropbear.id.au>
5*3e6106c4SLoGin * Copyright (C) 2018 embedded brains GmbH
6*3e6106c4SLoGin */
7*3e6106c4SLoGin #include "libfdt_env.h"
8*3e6106c4SLoGin
9*3e6106c4SLoGin #include <fdt.h>
10*3e6106c4SLoGin #include <libfdt.h>
11*3e6106c4SLoGin
12*3e6106c4SLoGin #include "libfdt_internal.h"
13*3e6106c4SLoGin
fdt_cells(const void * fdt,int nodeoffset,const char * name)14*3e6106c4SLoGin static int fdt_cells(const void *fdt, int nodeoffset, const char *name)
15*3e6106c4SLoGin {
16*3e6106c4SLoGin const fdt32_t *c;
17*3e6106c4SLoGin uint32_t val;
18*3e6106c4SLoGin int len;
19*3e6106c4SLoGin
20*3e6106c4SLoGin c = fdt_getprop(fdt, nodeoffset, name, &len);
21*3e6106c4SLoGin if (!c)
22*3e6106c4SLoGin return len;
23*3e6106c4SLoGin
24*3e6106c4SLoGin if (len != sizeof(*c))
25*3e6106c4SLoGin return -FDT_ERR_BADNCELLS;
26*3e6106c4SLoGin
27*3e6106c4SLoGin val = fdt32_to_cpu(*c);
28*3e6106c4SLoGin if (val > FDT_MAX_NCELLS)
29*3e6106c4SLoGin return -FDT_ERR_BADNCELLS;
30*3e6106c4SLoGin
31*3e6106c4SLoGin return (int)val;
32*3e6106c4SLoGin }
33*3e6106c4SLoGin
fdt_address_cells(const void * fdt,int nodeoffset)34*3e6106c4SLoGin int fdt_address_cells(const void *fdt, int nodeoffset)
35*3e6106c4SLoGin {
36*3e6106c4SLoGin int val;
37*3e6106c4SLoGin
38*3e6106c4SLoGin val = fdt_cells(fdt, nodeoffset, "#address-cells");
39*3e6106c4SLoGin if (val == 0)
40*3e6106c4SLoGin return -FDT_ERR_BADNCELLS;
41*3e6106c4SLoGin if (val == -FDT_ERR_NOTFOUND)
42*3e6106c4SLoGin return 2;
43*3e6106c4SLoGin return val;
44*3e6106c4SLoGin }
45*3e6106c4SLoGin
fdt_size_cells(const void * fdt,int nodeoffset)46*3e6106c4SLoGin int fdt_size_cells(const void *fdt, int nodeoffset)
47*3e6106c4SLoGin {
48*3e6106c4SLoGin int val;
49*3e6106c4SLoGin
50*3e6106c4SLoGin val = fdt_cells(fdt, nodeoffset, "#size-cells");
51*3e6106c4SLoGin if (val == -FDT_ERR_NOTFOUND)
52*3e6106c4SLoGin return 1;
53*3e6106c4SLoGin return val;
54*3e6106c4SLoGin }
55*3e6106c4SLoGin
56*3e6106c4SLoGin /* This function assumes that [address|size]_cells is 1 or 2 */
fdt_appendprop_addrrange(void * fdt,int parent,int nodeoffset,const char * name,uint64_t addr,uint64_t size)57*3e6106c4SLoGin int fdt_appendprop_addrrange(void *fdt, int parent, int nodeoffset,
58*3e6106c4SLoGin const char *name, uint64_t addr, uint64_t size)
59*3e6106c4SLoGin {
60*3e6106c4SLoGin int addr_cells, size_cells, ret;
61*3e6106c4SLoGin uint8_t data[sizeof(fdt64_t) * 2], *prop;
62*3e6106c4SLoGin
63*3e6106c4SLoGin ret = fdt_address_cells(fdt, parent);
64*3e6106c4SLoGin if (ret < 0)
65*3e6106c4SLoGin return ret;
66*3e6106c4SLoGin addr_cells = ret;
67*3e6106c4SLoGin
68*3e6106c4SLoGin ret = fdt_size_cells(fdt, parent);
69*3e6106c4SLoGin if (ret < 0)
70*3e6106c4SLoGin return ret;
71*3e6106c4SLoGin size_cells = ret;
72*3e6106c4SLoGin
73*3e6106c4SLoGin /* check validity of address */
74*3e6106c4SLoGin prop = data;
75*3e6106c4SLoGin if (addr_cells == 1) {
76*3e6106c4SLoGin if ((addr > UINT32_MAX) || (((uint64_t) UINT32_MAX + 1 - addr) < size))
77*3e6106c4SLoGin return -FDT_ERR_BADVALUE;
78*3e6106c4SLoGin
79*3e6106c4SLoGin fdt32_st(prop, (uint32_t)addr);
80*3e6106c4SLoGin } else if (addr_cells == 2) {
81*3e6106c4SLoGin fdt64_st(prop, addr);
82*3e6106c4SLoGin } else {
83*3e6106c4SLoGin return -FDT_ERR_BADNCELLS;
84*3e6106c4SLoGin }
85*3e6106c4SLoGin
86*3e6106c4SLoGin /* check validity of size */
87*3e6106c4SLoGin prop += addr_cells * sizeof(fdt32_t);
88*3e6106c4SLoGin if (size_cells == 1) {
89*3e6106c4SLoGin if (size > UINT32_MAX)
90*3e6106c4SLoGin return -FDT_ERR_BADVALUE;
91*3e6106c4SLoGin
92*3e6106c4SLoGin fdt32_st(prop, (uint32_t)size);
93*3e6106c4SLoGin } else if (size_cells == 2) {
94*3e6106c4SLoGin fdt64_st(prop, size);
95*3e6106c4SLoGin } else {
96*3e6106c4SLoGin return -FDT_ERR_BADNCELLS;
97*3e6106c4SLoGin }
98*3e6106c4SLoGin
99*3e6106c4SLoGin return fdt_appendprop(fdt, nodeoffset, name, data,
100*3e6106c4SLoGin (addr_cells + size_cells) * sizeof(fdt32_t));
101*3e6106c4SLoGin }
102