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 int 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]; 71 return 0; 72 } 73 74 /* 75 * 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 */ 79 int 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 /* 110 * 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 */ 114 int 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 /* 142 * 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 */ 145 int fdtw_write_inplace_cells(void *dtb, int node, const char *prop, 146 unsigned int cells, void *value) 147 { 148 int err, len; 149 150 assert(dtb != NULL); 151 assert(prop != NULL); 152 assert(value != NULL); 153 assert(node >= 0); 154 155 /* We expect either 1 or 2 cell property */ 156 assert(cells <= 2U); 157 158 if (cells == 2U) 159 *(uint64_t *)value = cpu_to_fdt64(*(uint64_t *)value); 160 else 161 *(uint32_t *)value = cpu_to_fdt32(*(uint32_t *)value); 162 163 len = (int)cells * 4; 164 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 } 174 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 */ 180 int 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 } 216