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. Any number of 32-bit 19 * cells of the property can be read. Returns 0 on success, or a negative 20 * FDT error value otherwise. 21 */ 22 int fdt_read_uint32_array(const void *dtb, int node, const char *prop_name, 23 unsigned int cells, uint32_t *value) 24 { 25 const fdt32_t *prop; 26 int value_len; 27 28 assert(dtb != NULL); 29 assert(prop_name != NULL); 30 assert(value != NULL); 31 assert(node >= 0); 32 33 /* Access property and obtain its length (in bytes) */ 34 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; 38 } 39 40 /* Verify that property length can fill the entire array. */ 41 if (NCELLS((unsigned int)value_len) < cells) { 42 WARN("Property length mismatch\n"); 43 return -FDT_ERR_BADVALUE; 44 } 45 46 for (unsigned int i = 0U; i < cells; i++) { 47 value[i] = fdt32_to_cpu(prop[i]); 48 } 49 50 return 0; 51 } 52 53 int 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 59 uint32_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 72 int 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]; 84 return 0; 85 } 86 87 /* 88 * 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 */ 92 int 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 /* 123 * 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 */ 127 int 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 /* 155 * 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 */ 158 int fdtw_write_inplace_cells(void *dtb, int node, const char *prop, 159 unsigned int cells, void *value) 160 { 161 int err, len; 162 163 assert(dtb != NULL); 164 assert(prop != NULL); 165 assert(value != NULL); 166 assert(node >= 0); 167 168 /* We expect either 1 or 2 cell property */ 169 assert(cells <= 2U); 170 171 if (cells == 2U) 172 *(uint64_t *)value = cpu_to_fdt64(*(uint64_t *)value); 173 else 174 *(uint32_t *)value = cpu_to_fdt32(*(uint32_t *)value); 175 176 len = (int)cells * 4; 177 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 } 187 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 */ 193 int 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 } 229 230 static uint64_t fdt_read_prop_cells(const fdt32_t *prop, int nr_cells) 231 { 232 uint64_t reg = fdt32_to_cpu(prop[0]); 233 234 if (nr_cells > 1) { 235 reg = (reg << 32) | fdt32_to_cpu(prop[1]); 236 } 237 238 return reg; 239 } 240 241 int fdt_get_reg_props_by_index(const void *dtb, int node, int index, 242 uintptr_t *base, size_t *size) 243 { 244 const fdt32_t *prop; 245 int parent, len; 246 int ac, sc; 247 int cell; 248 249 parent = fdt_parent_offset(dtb, node); 250 if (parent < 0) { 251 return -FDT_ERR_BADOFFSET; 252 } 253 254 ac = fdt_address_cells(dtb, parent); 255 sc = fdt_size_cells(dtb, parent); 256 257 cell = index * (ac + sc); 258 259 prop = fdt_getprop(dtb, node, "reg", &len); 260 if (prop == NULL) { 261 WARN("Couldn't find \"reg\" property in dtb\n"); 262 return -FDT_ERR_NOTFOUND; 263 } 264 265 if (((cell + ac + sc) * (int)sizeof(uint32_t)) > len) { 266 return -FDT_ERR_BADVALUE; 267 } 268 269 if (base != NULL) { 270 *base = (uintptr_t)fdt_read_prop_cells(&prop[cell], ac); 271 } 272 273 if (size != NULL) { 274 *size = (size_t)fdt_read_prop_cells(&prop[cell + ac], sc); 275 } 276 277 return 0; 278 } 279 280 /******************************************************************************* 281 * This function fills reg node info (base & size) with an index found by 282 * checking the reg-names node. 283 * Returns 0 on success and a negative FDT error code on failure. 284 ******************************************************************************/ 285 int fdt_get_reg_props_by_name(const void *dtb, int node, const char *name, 286 uintptr_t *base, size_t *size) 287 { 288 int index; 289 290 index = fdt_stringlist_search(dtb, node, "reg-names", name); 291 if (index < 0) { 292 return index; 293 } 294 295 return fdt_get_reg_props_by_index(dtb, node, index, base, size); 296 } 297 298 /******************************************************************************* 299 * This function gets the stdout path node. 300 * It reads the value indicated inside the device tree. 301 * Returns node offset on success and a negative FDT error code on failure. 302 ******************************************************************************/ 303 int fdt_get_stdout_node_offset(const void *dtb) 304 { 305 int node; 306 const char *prop, *path; 307 int len; 308 309 /* The /secure-chosen node takes precedence over the standard one. */ 310 node = fdt_path_offset(dtb, "/secure-chosen"); 311 if (node < 0) { 312 node = fdt_path_offset(dtb, "/chosen"); 313 if (node < 0) { 314 return -FDT_ERR_NOTFOUND; 315 } 316 } 317 318 prop = fdt_getprop(dtb, node, "stdout-path", NULL); 319 if (prop == NULL) { 320 return -FDT_ERR_NOTFOUND; 321 } 322 323 /* Determine the actual path length, as a colon terminates the path. */ 324 path = strchr(prop, ':'); 325 if (path == NULL) { 326 len = strlen(prop); 327 } else { 328 len = path - prop; 329 } 330 331 /* Aliases cannot start with a '/', so it must be the actual path. */ 332 if (prop[0] == '/') { 333 return fdt_path_offset_namelen(dtb, prop, len); 334 } 335 336 /* Lookup the alias, as this contains the actual path. */ 337 path = fdt_get_alias_namelen(dtb, prop, len); 338 if (path == NULL) { 339 return -FDT_ERR_NOTFOUND; 340 } 341 342 return fdt_path_offset(dtb, path); 343 } 344