blob: 1901a204073540a607ed4a6db418efd974c5f963 [file] [log] [blame]
Soby Mathewb9fccca2017-11-06 13:56:40 +00001/*
Alexei Fedorov2de82fc2020-01-29 16:21:28 +00002 * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
Soby Mathewb9fccca2017-11-06 13:56:40 +00003 *
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>
Antonio Nino Diazd85c8782018-06-26 10:34:07 +010010#include <string.h>
Soby Mathewb9fccca2017-11-06 13:56:40 +000011
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <libfdt.h>
13
14#include <common/debug.h>
15#include <common/fdt_wrappers.h>
16
Soby Mathewb9fccca2017-11-06 13:56:40 +000017/*
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010018 * Read cells from a given property of the given node. Any number of 32-bit
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010019 * cells of the property can be read. Returns 0 on success, or a negative
20 * FDT error value otherwise.
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010021 */
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010022int fdt_read_uint32_array(const void *dtb, int node, const char *prop_name,
23 unsigned int cells, uint32_t *value)
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010024{
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010025 const fdt32_t *prop;
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010026 int value_len;
27
28 assert(dtb != NULL);
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010029 assert(prop_name != NULL);
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010030 assert(value != NULL);
31 assert(node >= 0);
32
33 /* Access property and obtain its length (in bytes) */
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010034 prop = fdt_getprop(dtb, node, prop_name, &value_len);
35 if (prop == NULL) {
36 WARN("Couldn't find property %s in dtb\n", prop_name);
37 return -FDT_ERR_NOTFOUND;
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010038 }
39
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010040 /* Verify that property length can fill the entire array. */
41 if (NCELLS((unsigned int)value_len) < cells) {
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010042 WARN("Property length mismatch\n");
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010043 return -FDT_ERR_BADVALUE;
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010044 }
45
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010046 for (unsigned int i = 0U; i < cells; i++) {
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010047 value[i] = fdt32_to_cpu(prop[i]);
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010048 }
49
Andre Przywarafe5bdf52020-03-26 11:22:37 +000050 return 0;
51}
52
53int fdt_read_uint32(const void *dtb, int node, const char *prop_name,
54 uint32_t *value)
55{
56 return fdt_read_uint32_array(dtb, node, prop_name, 1, value);
57}
58
Andre Przywara2d5690c2020-03-26 11:50:33 +000059uint32_t fdt_read_uint32_default(const void *dtb, int node,
60 const char *prop_name, uint32_t dflt_value)
61{
62 uint32_t ret = dflt_value;
63 int err = fdt_read_uint32(dtb, node, prop_name, &ret);
64
65 if (err < 0) {
66 return dflt_value;
67 }
68
69 return ret;
70}
71
Andre Przywarafe5bdf52020-03-26 11:22:37 +000072int fdt_read_uint64(const void *dtb, int node, const char *prop_name,
73 uint64_t *value)
74{
75 uint32_t array[2] = {0, 0};
76 int ret;
77
78 ret = fdt_read_uint32_array(dtb, node, prop_name, 2, array);
79 if (ret < 0) {
80 return ret;
81 }
82
83 *value = ((uint64_t)array[0] << 32) | array[1];
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010084 return 0;
85}
86
87/*
Alexei Fedorov2de82fc2020-01-29 16:21:28 +000088 * Read bytes from a given property of the given node. Any number of
89 * bytes of the property can be read. The fdt pointer is updated.
90 * Returns 0 on success, and -1 on error.
91 */
92int fdtw_read_bytes(const void *dtb, int node, const char *prop,
93 unsigned int length, void *value)
94{
95 const void *ptr;
96 int value_len;
97
98 assert(dtb != NULL);
99 assert(prop != NULL);
100 assert(value != NULL);
101 assert(node >= 0);
102
103 /* Access property and obtain its length (in bytes) */
104 ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop),
105 &value_len);
106 if (ptr == NULL) {
107 WARN("Couldn't find property %s in dtb\n", prop);
108 return -1;
109 }
110
111 /* Verify that property length is not less than number of bytes */
112 if ((unsigned int)value_len < length) {
113 WARN("Property length mismatch\n");
114 return -1;
115 }
116
117 (void)memcpy(value, ptr, length);
118
119 return 0;
120}
121
122/*
Antonio Nino Diazd85c8782018-06-26 10:34:07 +0100123 * Read string from a given property of the given node. Up to 'size - 1'
124 * characters are read, and a NUL terminator is added. Returns 0 on success,
125 * and -1 upon error.
126 */
127int fdtw_read_string(const void *dtb, int node, const char *prop,
128 char *str, size_t size)
129{
130 const char *ptr;
131 size_t len;
132
133 assert(dtb != NULL);
134 assert(node >= 0);
135 assert(prop != NULL);
136 assert(str != NULL);
137 assert(size > 0U);
138
139 ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop), NULL);
140 if (ptr == NULL) {
141 WARN("Couldn't find property %s in dtb\n", prop);
142 return -1;
143 }
144
145 len = strlcpy(str, ptr, size);
146 if (len >= size) {
147 WARN("String of property %s in dtb has been truncated\n", prop);
148 return -1;
149 }
150
151 return 0;
152}
153
154/*
Soby Mathewb9fccca2017-11-06 13:56:40 +0000155 * Write cells in place to a given property of the given node. At most 2 cells
156 * of the property are written. Returns 0 on success, and -1 upon error.
157 */
158int fdtw_write_inplace_cells(void *dtb, int node, const char *prop,
159 unsigned int cells, void *value)
160{
161 int err, len;
162
Soby Mathewcc364842018-02-21 01:16:39 +0000163 assert(dtb != NULL);
164 assert(prop != NULL);
165 assert(value != NULL);
Soby Mathewb9fccca2017-11-06 13:56:40 +0000166 assert(node >= 0);
167
168 /* We expect either 1 or 2 cell property */
Soby Mathewcc364842018-02-21 01:16:39 +0000169 assert(cells <= 2U);
Soby Mathewb9fccca2017-11-06 13:56:40 +0000170
Soby Mathewcc364842018-02-21 01:16:39 +0000171 if (cells == 2U)
Soby Mathewb9fccca2017-11-06 13:56:40 +0000172 *(uint64_t *)value = cpu_to_fdt64(*(uint64_t *)value);
173 else
174 *(uint32_t *)value = cpu_to_fdt32(*(uint32_t *)value);
175
Soby Mathewcc364842018-02-21 01:16:39 +0000176 len = (int)cells * 4;
Soby Mathewb9fccca2017-11-06 13:56:40 +0000177
178 /* Set property value in place */
179 err = fdt_setprop_inplace(dtb, node, prop, value, len);
180 if (err != 0) {
181 WARN("Modify property %s failed with error %d\n", prop, err);
182 return -1;
183 }
184
185 return 0;
186}
Alexei Fedorov2de82fc2020-01-29 16:21:28 +0000187
188/*
189 * Write bytes in place to a given property of the given node.
190 * Any number of bytes of the property can be written.
191 * Returns 0 on success, and < 0 on error.
192 */
193int fdtw_write_inplace_bytes(void *dtb, int node, const char *prop,
194 unsigned int length, const void *data)
195{
196 const void *ptr;
197 int namelen, value_len, err;
198
199 assert(dtb != NULL);
200 assert(prop != NULL);
201 assert(data != NULL);
202 assert(node >= 0);
203
204 namelen = (int)strlen(prop);
205
206 /* Access property and obtain its length in bytes */
207 ptr = fdt_getprop_namelen(dtb, node, prop, namelen, &value_len);
208 if (ptr == NULL) {
209 WARN("Couldn't find property %s in dtb\n", prop);
210 return -1;
211 }
212
213 /* Verify that property length is not less than number of bytes */
214 if ((unsigned int)value_len < length) {
215 WARN("Property length mismatch\n");
216 return -1;
217 }
218
219 /* Set property value in place */
220 err = fdt_setprop_inplace_namelen_partial(dtb, node, prop,
221 namelen, 0,
222 data, (int)length);
223 if (err != 0) {
224 WARN("Set property %s failed with error %d\n", prop, err);
225 }
226
227 return err;
228}
Andre Przywarae183e152020-03-26 11:57:43 +0000229
230static uint64_t fdt_read_prop_cells(const fdt32_t *prop, int nr_cells)
231{
232 uint64_t reg = fdt32_to_cpu(prop[0]);
233
234 if (nr_cells > 1) {
235 reg = (reg << 32) | fdt32_to_cpu(prop[1]);
236 }
237
238 return reg;
239}
240
241int fdt_get_reg_props_by_index(const void *dtb, int node, int index,
242 uintptr_t *base, size_t *size)
243{
244 const fdt32_t *prop;
245 int parent, len;
246 int ac, sc;
247 int cell;
248
249 parent = fdt_parent_offset(dtb, node);
250 if (parent < 0) {
251 return -FDT_ERR_BADOFFSET;
252 }
253
254 ac = fdt_address_cells(dtb, parent);
255 sc = fdt_size_cells(dtb, parent);
256
257 cell = index * (ac + sc);
258
259 prop = fdt_getprop(dtb, node, "reg", &len);
260 if (prop == NULL) {
261 WARN("Couldn't find \"reg\" property in dtb\n");
262 return -FDT_ERR_NOTFOUND;
263 }
264
265 if (((cell + ac + sc) * (int)sizeof(uint32_t)) > len) {
266 return -FDT_ERR_BADVALUE;
267 }
268
269 if (base != NULL) {
270 *base = (uintptr_t)fdt_read_prop_cells(&prop[cell], ac);
271 }
272
273 if (size != NULL) {
274 *size = (size_t)fdt_read_prop_cells(&prop[cell + ac], sc);
275 }
276
277 return 0;
278}
Andre Przywara4a1c8742020-03-26 12:11:34 +0000279
280/*******************************************************************************
281 * This function fills reg node info (base & size) with an index found by
282 * checking the reg-names node.
283 * Returns 0 on success and a negative FDT error code on failure.
284 ******************************************************************************/
285int fdt_get_reg_props_by_name(const void *dtb, int node, const char *name,
286 uintptr_t *base, size_t *size)
287{
288 int index;
289
290 index = fdt_stringlist_search(dtb, node, "reg-names", name);
291 if (index < 0) {
292 return index;
293 }
294
295 return fdt_get_reg_props_by_index(dtb, node, index, base, size);
296}
Andre Przywaraa0eaca52020-03-26 12:52:06 +0000297
298/*******************************************************************************
299 * This function gets the stdout path node.
300 * It reads the value indicated inside the device tree.
301 * Returns node offset on success and a negative FDT error code on failure.
302 ******************************************************************************/
303int fdt_get_stdout_node_offset(const void *dtb)
304{
305 int node;
306 const char *prop, *path;
307 int len;
308
309 /* The /secure-chosen node takes precedence over the standard one. */
310 node = fdt_path_offset(dtb, "/secure-chosen");
311 if (node < 0) {
312 node = fdt_path_offset(dtb, "/chosen");
313 if (node < 0) {
314 return -FDT_ERR_NOTFOUND;
315 }
316 }
317
318 prop = fdt_getprop(dtb, node, "stdout-path", NULL);
319 if (prop == NULL) {
320 return -FDT_ERR_NOTFOUND;
321 }
322
323 /* Determine the actual path length, as a colon terminates the path. */
324 path = strchr(prop, ':');
325 if (path == NULL) {
326 len = strlen(prop);
327 } else {
328 len = path - prop;
329 }
330
331 /* Aliases cannot start with a '/', so it must be the actual path. */
332 if (prop[0] == '/') {
333 return fdt_path_offset_namelen(dtb, prop, len);
334 }
335
336 /* Lookup the alias, as this contains the actual path. */
337 path = fdt_get_alias_namelen(dtb, prop, len);
338 if (path == NULL) {
339 return -FDT_ERR_NOTFOUND;
340 }
341
342 return fdt_path_offset(dtb, path);
343}