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 */
fdtw_read_cells(const void * dtb,int node,const char * prop,unsigned int cells,void * value)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. The fdt pointer is updated. Returns 0 on
69 * success, and -1 on error.
70 */
fdtw_read_array(const void * dtb,int node,const char * prop,unsigned int cells,void * value)71 int 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 /*
106 * 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 */
fdtw_read_bytes(const void * dtb,int node,const char * prop,unsigned int length,void * value)110 int 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 /*
141 * 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 */
fdtw_read_string(const void * dtb,int node,const char * prop,char * str,size_t size)145 int 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 /*
173 * 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 */
fdtw_write_inplace_cells(void * dtb,int node,const char * prop,unsigned int cells,void * value)176 int fdtw_write_inplace_cells(void *dtb, int node, const char *prop,
177 unsigned int cells, void *value)
178 {
179 int err, len;
180
181 assert(dtb != NULL);
182 assert(prop != NULL);
183 assert(value != NULL);
184 assert(node >= 0);
185
186 /* We expect either 1 or 2 cell property */
187 assert(cells <= 2U);
188
189 if (cells == 2U)
190 *(uint64_t *)value = cpu_to_fdt64(*(uint64_t *)value);
191 else
192 *(uint32_t *)value = cpu_to_fdt32(*(uint32_t *)value);
193
194 len = (int)cells * 4;
195
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 }
205
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 */
fdtw_write_inplace_bytes(void * dtb,int node,const char * prop,unsigned int length,const void * data)211 int 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 }
247