blob: 394f3b0ca09ae1daa3d1dc5a120b603f3d23d02b [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
59int fdt_read_uint64(const void *dtb, int node, const char *prop_name,
60 uint64_t *value)
61{
62 uint32_t array[2] = {0, 0};
63 int ret;
64
65 ret = fdt_read_uint32_array(dtb, node, prop_name, 2, array);
66 if (ret < 0) {
67 return ret;
68 }
69
70 *value = ((uint64_t)array[0] << 32) | array[1];
Antonio Nino Diaz3fddfbb2018-06-26 10:34:10 +010071 return 0;
72}
73
74/*
Alexei Fedorov2de82fc2020-01-29 16:21:28 +000075 * Read bytes from a given property of the given node. Any number of
76 * bytes of the property can be read. The fdt pointer is updated.
77 * Returns 0 on success, and -1 on error.
78 */
79int fdtw_read_bytes(const void *dtb, int node, const char *prop,
80 unsigned int length, void *value)
81{
82 const void *ptr;
83 int value_len;
84
85 assert(dtb != NULL);
86 assert(prop != NULL);
87 assert(value != NULL);
88 assert(node >= 0);
89
90 /* Access property and obtain its length (in bytes) */
91 ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop),
92 &value_len);
93 if (ptr == NULL) {
94 WARN("Couldn't find property %s in dtb\n", prop);
95 return -1;
96 }
97
98 /* Verify that property length is not less than number of bytes */
99 if ((unsigned int)value_len < length) {
100 WARN("Property length mismatch\n");
101 return -1;
102 }
103
104 (void)memcpy(value, ptr, length);
105
106 return 0;
107}
108
109/*
Antonio Nino Diazd85c8782018-06-26 10:34:07 +0100110 * Read string from a given property of the given node. Up to 'size - 1'
111 * characters are read, and a NUL terminator is added. Returns 0 on success,
112 * and -1 upon error.
113 */
114int fdtw_read_string(const void *dtb, int node, const char *prop,
115 char *str, size_t size)
116{
117 const char *ptr;
118 size_t len;
119
120 assert(dtb != NULL);
121 assert(node >= 0);
122 assert(prop != NULL);
123 assert(str != NULL);
124 assert(size > 0U);
125
126 ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop), NULL);
127 if (ptr == NULL) {
128 WARN("Couldn't find property %s in dtb\n", prop);
129 return -1;
130 }
131
132 len = strlcpy(str, ptr, size);
133 if (len >= size) {
134 WARN("String of property %s in dtb has been truncated\n", prop);
135 return -1;
136 }
137
138 return 0;
139}
140
141/*
Soby Mathewb9fccca2017-11-06 13:56:40 +0000142 * Write cells in place to a given property of the given node. At most 2 cells
143 * of the property are written. Returns 0 on success, and -1 upon error.
144 */
145int fdtw_write_inplace_cells(void *dtb, int node, const char *prop,
146 unsigned int cells, void *value)
147{
148 int err, len;
149
Soby Mathewcc364842018-02-21 01:16:39 +0000150 assert(dtb != NULL);
151 assert(prop != NULL);
152 assert(value != NULL);
Soby Mathewb9fccca2017-11-06 13:56:40 +0000153 assert(node >= 0);
154
155 /* We expect either 1 or 2 cell property */
Soby Mathewcc364842018-02-21 01:16:39 +0000156 assert(cells <= 2U);
Soby Mathewb9fccca2017-11-06 13:56:40 +0000157
Soby Mathewcc364842018-02-21 01:16:39 +0000158 if (cells == 2U)
Soby Mathewb9fccca2017-11-06 13:56:40 +0000159 *(uint64_t *)value = cpu_to_fdt64(*(uint64_t *)value);
160 else
161 *(uint32_t *)value = cpu_to_fdt32(*(uint32_t *)value);
162
Soby Mathewcc364842018-02-21 01:16:39 +0000163 len = (int)cells * 4;
Soby Mathewb9fccca2017-11-06 13:56:40 +0000164
165 /* Set property value in place */
166 err = fdt_setprop_inplace(dtb, node, prop, value, len);
167 if (err != 0) {
168 WARN("Modify property %s failed with error %d\n", prop, err);
169 return -1;
170 }
171
172 return 0;
173}
Alexei Fedorov2de82fc2020-01-29 16:21:28 +0000174
175/*
176 * Write bytes in place to a given property of the given node.
177 * Any number of bytes of the property can be written.
178 * Returns 0 on success, and < 0 on error.
179 */
180int fdtw_write_inplace_bytes(void *dtb, int node, const char *prop,
181 unsigned int length, const void *data)
182{
183 const void *ptr;
184 int namelen, value_len, err;
185
186 assert(dtb != NULL);
187 assert(prop != NULL);
188 assert(data != NULL);
189 assert(node >= 0);
190
191 namelen = (int)strlen(prop);
192
193 /* Access property and obtain its length in bytes */
194 ptr = fdt_getprop_namelen(dtb, node, prop, namelen, &value_len);
195 if (ptr == NULL) {
196 WARN("Couldn't find property %s in dtb\n", prop);
197 return -1;
198 }
199
200 /* Verify that property length is not less than number of bytes */
201 if ((unsigned int)value_len < length) {
202 WARN("Property length mismatch\n");
203 return -1;
204 }
205
206 /* Set property value in place */
207 err = fdt_setprop_inplace_namelen_partial(dtb, node, prop,
208 namelen, 0,
209 data, (int)length);
210 if (err != 0) {
211 WARN("Set property %s failed with error %d\n", prop, err);
212 }
213
214 return err;
215}