blob: 5afa14271a2e4b24a01d55025291fe942cd6f8ec [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/*
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 */
22int 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
Soby Mathewcc364842018-02-21 01:16:39 +000029 assert(dtb != NULL);
30 assert(prop != NULL);
31 assert(value != NULL);
Soby Mathewb9fccca2017-11-06 13:56:40 +000032 assert(node >= 0);
33
34 /* We expect either 1 or 2 cell property */
Soby Mathewcc364842018-02-21 01:16:39 +000035 assert(cells <= 2U);
Soby Mathewb9fccca2017-11-06 13:56:40 +000036
37 /* Access property and obtain its length (in bytes) */
Soby Mathewcc364842018-02-21 01:16:39 +000038 value_ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop),
Soby Mathewb9fccca2017-11-06 13:56:40 +000039 &value_len);
40 if (value_ptr == NULL) {
41 WARN("Couldn't find property %s in dtb\n", prop);
42 return -1;
43 }
44
Soby Mathewb9fccca2017-11-06 13:56:40 +000045 /* Verify that property length accords with cell length */
Soby Mathewcc364842018-02-21 01:16:39 +000046 if (NCELLS((unsigned int)value_len) != cells) {
Soby Mathewb9fccca2017-11-06 13:56:40 +000047 WARN("Property length mismatch\n");
48 return -1;
49 }
50
Soby Mathewcc364842018-02-21 01:16:39 +000051 if (cells == 2U) {
Soby Mathewb9fccca2017-11-06 13:56:40 +000052 hi = fdt32_to_cpu(*value_ptr);
53 value_ptr++;
54 }
55
56 lo = fdt32_to_cpu(*value_ptr);
57
Soby Mathewcc364842018-02-21 01:16:39 +000058 if (cells == 2U)
Soby Mathewb9fccca2017-11-06 13:56:40 +000059 *((uint64_t *) value) = ((uint64_t) hi << 32) | lo;
60 else
61 *((uint32_t *) value) = lo;
62
63 return 0;
64}
65
66/*
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010067 * Read cells from a given property of the given node. Any number of 32-bit
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010068 * cells of the property can be read. Returns 0 on success, or a negative
69 * FDT error value otherwise.
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010070 */
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010071int fdt_read_uint32_array(const void *dtb, int node, const char *prop_name,
72 unsigned int cells, uint32_t *value)
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010073{
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010074 const fdt32_t *prop;
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010075 int value_len;
76
77 assert(dtb != NULL);
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010078 assert(prop_name != NULL);
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010079 assert(value != NULL);
80 assert(node >= 0);
81
82 /* Access property and obtain its length (in bytes) */
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010083 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;
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010087 }
88
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010089 /* Verify that property length can fill the entire array. */
90 if (NCELLS((unsigned int)value_len) < cells) {
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010091 WARN("Property length mismatch\n");
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010092 return -FDT_ERR_BADVALUE;
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010093 }
94
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010095 for (unsigned int i = 0U; i < cells; i++) {
Andre Przywara6cf6a1b2020-03-30 23:21:13 +010096 value[i] = fdt32_to_cpu(prop[i]);
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010097 }
98
99 return 0;
100}
101
102/*
Alexei Fedorov2de82fc2020-01-29 16:21:28 +0000103 * 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 */
107int 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/*
Antonio Nino Diazd85c8782018-06-26 10:34:07 +0100138 * 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 */
142int 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/*
Soby Mathewb9fccca2017-11-06 13:56:40 +0000170 * 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 */
173int fdtw_write_inplace_cells(void *dtb, int node, const char *prop,
174 unsigned int cells, void *value)
175{
176 int err, len;
177
Soby Mathewcc364842018-02-21 01:16:39 +0000178 assert(dtb != NULL);
179 assert(prop != NULL);
180 assert(value != NULL);
Soby Mathewb9fccca2017-11-06 13:56:40 +0000181 assert(node >= 0);
182
183 /* We expect either 1 or 2 cell property */
Soby Mathewcc364842018-02-21 01:16:39 +0000184 assert(cells <= 2U);
Soby Mathewb9fccca2017-11-06 13:56:40 +0000185
Soby Mathewcc364842018-02-21 01:16:39 +0000186 if (cells == 2U)
Soby Mathewb9fccca2017-11-06 13:56:40 +0000187 *(uint64_t *)value = cpu_to_fdt64(*(uint64_t *)value);
188 else
189 *(uint32_t *)value = cpu_to_fdt32(*(uint32_t *)value);
190
Soby Mathewcc364842018-02-21 01:16:39 +0000191 len = (int)cells * 4;
Soby Mathewb9fccca2017-11-06 13:56:40 +0000192
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}
Alexei Fedorov2de82fc2020-01-29 16:21:28 +0000202
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 */
208int 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}