xref: /rk3399_ARM-atf/common/fdt_wrappers.c (revision 52a616b48c617fe8721106f29f2910ca4681afea)
1 /*
2  * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /* Helper functions to offer easier navigation of Device Tree Blob */
8 
9 #include <assert.h>
10 #include <string.h>
11 
12 #include <libfdt.h>
13 
14 #include <common/debug.h>
15 #include <common/fdt_wrappers.h>
16 
17 /*
18  * Read cells from a given property of the given node. At most 2 cells of the
19  * property are read, and pointer is updated. Returns 0 on success, and -1 upon
20  * error
21  */
22 int fdtw_read_cells(const void *dtb, int node, const char *prop,
23 		unsigned int cells, void *value)
24 {
25 	const uint32_t *value_ptr;
26 	uint32_t hi = 0, lo;
27 	int value_len;
28 
29 	assert(dtb != NULL);
30 	assert(prop != NULL);
31 	assert(value != NULL);
32 	assert(node >= 0);
33 
34 	/* We expect either 1 or 2 cell property */
35 	assert(cells <= 2U);
36 
37 	/* Access property and obtain its length (in bytes) */
38 	value_ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop),
39 			&value_len);
40 	if (value_ptr == NULL) {
41 		WARN("Couldn't find property %s in dtb\n", prop);
42 		return -1;
43 	}
44 
45 	/* Verify that property length accords with cell length */
46 	if (NCELLS((unsigned int)value_len) != cells) {
47 		WARN("Property length mismatch\n");
48 		return -1;
49 	}
50 
51 	if (cells == 2U) {
52 		hi = fdt32_to_cpu(*value_ptr);
53 		value_ptr++;
54 	}
55 
56 	lo = fdt32_to_cpu(*value_ptr);
57 
58 	if (cells == 2U)
59 		*((uint64_t *) value) = ((uint64_t) hi << 32) | lo;
60 	else
61 		*((uint32_t *) value) = lo;
62 
63 	return 0;
64 }
65 
66 /*
67  * Read cells from a given property of the given node. Any number of 32-bit
68  * cells of the property can be read. Returns 0 on success, or a negative
69  * FDT error value otherwise.
70  */
71 int fdt_read_uint32_array(const void *dtb, int node, const char *prop_name,
72 			  unsigned int cells, uint32_t *value)
73 {
74 	const fdt32_t *prop;
75 	int value_len;
76 
77 	assert(dtb != NULL);
78 	assert(prop_name != NULL);
79 	assert(value != NULL);
80 	assert(node >= 0);
81 
82 	/* Access property and obtain its length (in bytes) */
83 	prop = fdt_getprop(dtb, node, prop_name, &value_len);
84 	if (prop == NULL) {
85 		WARN("Couldn't find property %s in dtb\n", prop_name);
86 		return -FDT_ERR_NOTFOUND;
87 	}
88 
89 	/* Verify that property length can fill the entire array. */
90 	if (NCELLS((unsigned int)value_len) < cells) {
91 		WARN("Property length mismatch\n");
92 		return -FDT_ERR_BADVALUE;
93 	}
94 
95 	for (unsigned int i = 0U; i < cells; i++) {
96 		value[i] = fdt32_to_cpu(prop[i]);
97 	}
98 
99 	return 0;
100 }
101 
102 /*
103  * Read bytes from a given property of the given node. Any number of
104  * bytes of the property can be read. The fdt pointer is updated.
105  * Returns 0 on success, and -1 on error.
106  */
107 int fdtw_read_bytes(const void *dtb, int node, const char *prop,
108 		    unsigned int length, void *value)
109 {
110 	const void *ptr;
111 	int value_len;
112 
113 	assert(dtb != NULL);
114 	assert(prop != NULL);
115 	assert(value != NULL);
116 	assert(node >= 0);
117 
118 	/* Access property and obtain its length (in bytes) */
119 	ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop),
120 					&value_len);
121 	if (ptr == NULL) {
122 		WARN("Couldn't find property %s in dtb\n", prop);
123 		return -1;
124 	}
125 
126 	/* Verify that property length is not less than number of bytes */
127 	if ((unsigned int)value_len < length) {
128 		WARN("Property length mismatch\n");
129 		return -1;
130 	}
131 
132 	(void)memcpy(value, ptr, length);
133 
134 	return 0;
135 }
136 
137 /*
138  * Read string from a given property of the given node. Up to 'size - 1'
139  * characters are read, and a NUL terminator is added. Returns 0 on success,
140  * and -1 upon error.
141  */
142 int fdtw_read_string(const void *dtb, int node, const char *prop,
143 		char *str, size_t size)
144 {
145 	const char *ptr;
146 	size_t len;
147 
148 	assert(dtb != NULL);
149 	assert(node >= 0);
150 	assert(prop != NULL);
151 	assert(str != NULL);
152 	assert(size > 0U);
153 
154 	ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop), NULL);
155 	if (ptr == NULL) {
156 		WARN("Couldn't find property %s in dtb\n", prop);
157 		return -1;
158 	}
159 
160 	len = strlcpy(str, ptr, size);
161 	if (len >= size) {
162 		WARN("String of property %s in dtb has been truncated\n", prop);
163 		return -1;
164 	}
165 
166 	return 0;
167 }
168 
169 /*
170  * Write cells in place to a given property of the given node. At most 2 cells
171  * of the property are written. Returns 0 on success, and -1 upon error.
172  */
173 int fdtw_write_inplace_cells(void *dtb, int node, const char *prop,
174 		unsigned int cells, void *value)
175 {
176 	int err, len;
177 
178 	assert(dtb != NULL);
179 	assert(prop != NULL);
180 	assert(value != NULL);
181 	assert(node >= 0);
182 
183 	/* We expect either 1 or 2 cell property */
184 	assert(cells <= 2U);
185 
186 	if (cells == 2U)
187 		*(uint64_t *)value = cpu_to_fdt64(*(uint64_t *)value);
188 	else
189 		*(uint32_t *)value = cpu_to_fdt32(*(uint32_t *)value);
190 
191 	len = (int)cells * 4;
192 
193 	/* Set property value in place */
194 	err = fdt_setprop_inplace(dtb, node, prop, value, len);
195 	if (err != 0) {
196 		WARN("Modify property %s failed with error %d\n", prop, err);
197 		return -1;
198 	}
199 
200 	return 0;
201 }
202 
203 /*
204  * Write bytes in place to a given property of the given node.
205  * Any number of bytes of the property can be written.
206  * Returns 0 on success, and < 0 on error.
207  */
208 int fdtw_write_inplace_bytes(void *dtb, int node, const char *prop,
209 			     unsigned int length, const void *data)
210 {
211 	const void *ptr;
212 	int namelen, value_len, err;
213 
214 	assert(dtb != NULL);
215 	assert(prop != NULL);
216 	assert(data != NULL);
217 	assert(node >= 0);
218 
219 	namelen = (int)strlen(prop);
220 
221 	/* Access property and obtain its length in bytes */
222 	ptr = fdt_getprop_namelen(dtb, node, prop, namelen, &value_len);
223 	if (ptr == NULL) {
224 		WARN("Couldn't find property %s in dtb\n", prop);
225 		return -1;
226 	}
227 
228 	/* Verify that property length is not less than number of bytes */
229 	if ((unsigned int)value_len < length) {
230 		WARN("Property length mismatch\n");
231 		return -1;
232 	}
233 
234 	/* Set property value in place */
235 	err = fdt_setprop_inplace_namelen_partial(dtb, node, prop,
236 						  namelen, 0,
237 						  data, (int)length);
238 	if (err != 0) {
239 		WARN("Set property %s failed with error %d\n", prop, err);
240 	}
241 
242 	return err;
243 }
244