blob: ca5b4556d3f8b9ca5c658da2972a83f8bcd55daf [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
68 * cells of the property can be read. The fdt pointer is updated. Returns 0 on
69 * success, and -1 on error.
70 */
71int fdtw_read_array(const void *dtb, int node, const char *prop,
72 unsigned int cells, void *value)
73{
74 const uint32_t *value_ptr;
75 int value_len;
76
77 assert(dtb != NULL);
78 assert(prop != NULL);
79 assert(value != NULL);
80 assert(node >= 0);
81
82 /* Access property and obtain its length (in bytes) */
83 value_ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop),
84 &value_len);
85 if (value_ptr == NULL) {
86 WARN("Couldn't find property %s in dtb\n", prop);
87 return -1;
88 }
89
90 /* Verify that property length accords with cell length */
91 if (NCELLS((unsigned int)value_len) != cells) {
92 WARN("Property length mismatch\n");
93 return -1;
94 }
95
96 uint32_t *dst = value;
97
98 for (unsigned int i = 0U; i < cells; i++) {
99 dst[i] = fdt32_to_cpu(value_ptr[i]);
100 }
101
102 return 0;
103}
104
105/*
Alexei Fedorov2de82fc2020-01-29 16:21:28 +0000106 * Read bytes from a given property of the given node. Any number of
107 * bytes of the property can be read. The fdt pointer is updated.
108 * Returns 0 on success, and -1 on error.
109 */
110int fdtw_read_bytes(const void *dtb, int node, const char *prop,
111 unsigned int length, void *value)
112{
113 const void *ptr;
114 int value_len;
115
116 assert(dtb != NULL);
117 assert(prop != NULL);
118 assert(value != NULL);
119 assert(node >= 0);
120
121 /* Access property and obtain its length (in bytes) */
122 ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop),
123 &value_len);
124 if (ptr == NULL) {
125 WARN("Couldn't find property %s in dtb\n", prop);
126 return -1;
127 }
128
129 /* Verify that property length is not less than number of bytes */
130 if ((unsigned int)value_len < length) {
131 WARN("Property length mismatch\n");
132 return -1;
133 }
134
135 (void)memcpy(value, ptr, length);
136
137 return 0;
138}
139
140/*
Antonio Nino Diazd85c8782018-06-26 10:34:07 +0100141 * Read string from a given property of the given node. Up to 'size - 1'
142 * characters are read, and a NUL terminator is added. Returns 0 on success,
143 * and -1 upon error.
144 */
145int fdtw_read_string(const void *dtb, int node, const char *prop,
146 char *str, size_t size)
147{
148 const char *ptr;
149 size_t len;
150
151 assert(dtb != NULL);
152 assert(node >= 0);
153 assert(prop != NULL);
154 assert(str != NULL);
155 assert(size > 0U);
156
157 ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop), NULL);
158 if (ptr == NULL) {
159 WARN("Couldn't find property %s in dtb\n", prop);
160 return -1;
161 }
162
163 len = strlcpy(str, ptr, size);
164 if (len >= size) {
165 WARN("String of property %s in dtb has been truncated\n", prop);
166 return -1;
167 }
168
169 return 0;
170}
171
172/*
Soby Mathewb9fccca2017-11-06 13:56:40 +0000173 * Write cells in place to a given property of the given node. At most 2 cells
174 * of the property are written. Returns 0 on success, and -1 upon error.
175 */
176int fdtw_write_inplace_cells(void *dtb, int node, const char *prop,
177 unsigned int cells, void *value)
178{
179 int err, len;
180
Soby Mathewcc364842018-02-21 01:16:39 +0000181 assert(dtb != NULL);
182 assert(prop != NULL);
183 assert(value != NULL);
Soby Mathewb9fccca2017-11-06 13:56:40 +0000184 assert(node >= 0);
185
186 /* We expect either 1 or 2 cell property */
Soby Mathewcc364842018-02-21 01:16:39 +0000187 assert(cells <= 2U);
Soby Mathewb9fccca2017-11-06 13:56:40 +0000188
Soby Mathewcc364842018-02-21 01:16:39 +0000189 if (cells == 2U)
Soby Mathewb9fccca2017-11-06 13:56:40 +0000190 *(uint64_t *)value = cpu_to_fdt64(*(uint64_t *)value);
191 else
192 *(uint32_t *)value = cpu_to_fdt32(*(uint32_t *)value);
193
Soby Mathewcc364842018-02-21 01:16:39 +0000194 len = (int)cells * 4;
Soby Mathewb9fccca2017-11-06 13:56:40 +0000195
196 /* Set property value in place */
197 err = fdt_setprop_inplace(dtb, node, prop, value, len);
198 if (err != 0) {
199 WARN("Modify property %s failed with error %d\n", prop, err);
200 return -1;
201 }
202
203 return 0;
204}
Alexei Fedorov2de82fc2020-01-29 16:21:28 +0000205
206/*
207 * Write bytes in place to a given property of the given node.
208 * Any number of bytes of the property can be written.
209 * Returns 0 on success, and < 0 on error.
210 */
211int fdtw_write_inplace_bytes(void *dtb, int node, const char *prop,
212 unsigned int length, const void *data)
213{
214 const void *ptr;
215 int namelen, value_len, err;
216
217 assert(dtb != NULL);
218 assert(prop != NULL);
219 assert(data != NULL);
220 assert(node >= 0);
221
222 namelen = (int)strlen(prop);
223
224 /* Access property and obtain its length in bytes */
225 ptr = fdt_getprop_namelen(dtb, node, prop, namelen, &value_len);
226 if (ptr == NULL) {
227 WARN("Couldn't find property %s in dtb\n", prop);
228 return -1;
229 }
230
231 /* Verify that property length is not less than number of bytes */
232 if ((unsigned int)value_len < length) {
233 WARN("Property length mismatch\n");
234 return -1;
235 }
236
237 /* Set property value in place */
238 err = fdt_setprop_inplace_namelen_partial(dtb, node, prop,
239 namelen, 0,
240 data, (int)length);
241 if (err != 0) {
242 WARN("Set property %s failed with error %d\n", prop, err);
243 }
244
245 return err;
246}