blob: 57e3bcd406a27eaae535c2e600fc31278d7e79f9 [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}