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