aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Functions to help device tree manipulation using libfdt. |
| 3 | * It also provides functions to read entries from device tree proc |
| 4 | * interface. |
| 5 | * |
| 6 | * Copyright 2008 IBM Corporation. |
| 7 | * Authors: Jerone Young <jyoung5@us.ibm.com> |
| 8 | * Hollis Blanchard <hollisb@us.ibm.com> |
| 9 | * |
| 10 | * This work is licensed under the GNU GPL license version 2 or later. |
| 11 | * |
| 12 | */ |
| 13 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 15 | |
Eric Auger | 60e43e9 | 2016-02-19 09:42:29 -0700 | [diff] [blame] | 16 | #ifdef CONFIG_LINUX |
| 17 | #include <dirent.h> |
| 18 | #endif |
| 19 | |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 20 | #include "qapi/error.h" |
Li Liu | db013f8 | 2014-08-26 14:38:02 +0800 | [diff] [blame] | 21 | #include "qemu/error-report.h" |
Markus Armbruster | 922a01a | 2018-02-01 12:18:46 +0100 | [diff] [blame] | 22 | #include "qemu/option.h" |
Paolo Bonzini | 58369e2 | 2016-03-15 17:22:36 +0100 | [diff] [blame] | 23 | #include "qemu/bswap.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 24 | #include "sysemu/device_tree.h" |
Markus Armbruster | 2ff3de6 | 2013-07-04 15:09:22 +0200 | [diff] [blame] | 25 | #include "sysemu/sysemu.h" |
Blue Swirl | 39b7f20 | 2009-09-22 17:51:36 +0000 | [diff] [blame] | 26 | #include "hw/loader.h" |
Marcel Apfelbaum | 6cabe7f | 2015-02-04 17:43:53 +0200 | [diff] [blame] | 27 | #include "hw/boards.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 28 | #include "qemu/config-file.h" |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 29 | |
| 30 | #include <libfdt.h> |
| 31 | |
Geert Uytterhoeven | 14ec3cb | 2018-04-26 11:04:38 +0100 | [diff] [blame] | 32 | #define FDT_MAX_SIZE 0x100000 |
Alexander Graf | ce36252 | 2012-05-17 15:33:54 +0200 | [diff] [blame] | 33 | |
| 34 | void *create_device_tree(int *sizep) |
| 35 | { |
| 36 | void *fdt; |
| 37 | int ret; |
| 38 | |
| 39 | *sizep = FDT_MAX_SIZE; |
| 40 | fdt = g_malloc0(FDT_MAX_SIZE); |
| 41 | ret = fdt_create(fdt, FDT_MAX_SIZE); |
| 42 | if (ret < 0) { |
| 43 | goto fail; |
| 44 | } |
Peter Maydell | ef6de70 | 2013-11-22 17:17:09 +0000 | [diff] [blame] | 45 | ret = fdt_finish_reservemap(fdt); |
| 46 | if (ret < 0) { |
| 47 | goto fail; |
| 48 | } |
Alexander Graf | ce36252 | 2012-05-17 15:33:54 +0200 | [diff] [blame] | 49 | ret = fdt_begin_node(fdt, ""); |
| 50 | if (ret < 0) { |
| 51 | goto fail; |
| 52 | } |
| 53 | ret = fdt_end_node(fdt); |
| 54 | if (ret < 0) { |
| 55 | goto fail; |
| 56 | } |
| 57 | ret = fdt_finish(fdt); |
| 58 | if (ret < 0) { |
| 59 | goto fail; |
| 60 | } |
| 61 | ret = fdt_open_into(fdt, fdt, *sizep); |
| 62 | if (ret) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 63 | error_report("Unable to copy device tree in memory"); |
Alexander Graf | ce36252 | 2012-05-17 15:33:54 +0200 | [diff] [blame] | 64 | exit(1); |
| 65 | } |
| 66 | |
| 67 | return fdt; |
| 68 | fail: |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 69 | error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret)); |
Alexander Graf | ce36252 | 2012-05-17 15:33:54 +0200 | [diff] [blame] | 70 | exit(1); |
| 71 | } |
| 72 | |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 73 | void *load_device_tree(const char *filename_path, int *sizep) |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 74 | { |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 75 | int dt_size; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 76 | int dt_file_load_size; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 77 | int ret; |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 78 | void *fdt = NULL; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 79 | |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 80 | *sizep = 0; |
| 81 | dt_size = get_image_size(filename_path); |
| 82 | if (dt_size < 0) { |
Li Liu | db013f8 | 2014-08-26 14:38:02 +0800 | [diff] [blame] | 83 | error_report("Unable to get size of device tree file '%s'", |
| 84 | filename_path); |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 85 | goto fail; |
| 86 | } |
Markus Armbruster | 065e629 | 2019-04-09 19:40:18 +0200 | [diff] [blame] | 87 | if (dt_size > INT_MAX / 2 - 10000) { |
| 88 | error_report("Device tree file '%s' is too large", filename_path); |
| 89 | goto fail; |
| 90 | } |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 91 | |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 92 | /* Expand to 2x size to give enough room for manipulation. */ |
Alexander Graf | ded57c5 | 2011-07-23 10:54:11 +0200 | [diff] [blame] | 93 | dt_size += 10000; |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 94 | dt_size *= 2; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 95 | /* First allocate space in qemu for device tree */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 96 | fdt = g_malloc0(dt_size); |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 97 | |
Peter Maydell | da885fe | 2018-12-14 13:30:52 +0000 | [diff] [blame] | 98 | dt_file_load_size = load_image_size(filename_path, fdt, dt_size); |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 99 | if (dt_file_load_size < 0) { |
Li Liu | db013f8 | 2014-08-26 14:38:02 +0800 | [diff] [blame] | 100 | error_report("Unable to open device tree file '%s'", |
| 101 | filename_path); |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 102 | goto fail; |
| 103 | } |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 104 | |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 105 | ret = fdt_open_into(fdt, fdt, dt_size); |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 106 | if (ret) { |
Li Liu | db013f8 | 2014-08-26 14:38:02 +0800 | [diff] [blame] | 107 | error_report("Unable to copy device tree in memory"); |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 108 | goto fail; |
| 109 | } |
| 110 | |
| 111 | /* Check sanity of device tree */ |
| 112 | if (fdt_check_header(fdt)) { |
Li Liu | db013f8 | 2014-08-26 14:38:02 +0800 | [diff] [blame] | 113 | error_report("Device tree file loaded into memory is invalid: %s", |
| 114 | filename_path); |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 115 | goto fail; |
| 116 | } |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 117 | *sizep = dt_size; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 118 | return fdt; |
| 119 | |
| 120 | fail: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 121 | g_free(fdt); |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 122 | return NULL; |
| 123 | } |
| 124 | |
Eric Auger | 60e43e9 | 2016-02-19 09:42:29 -0700 | [diff] [blame] | 125 | #ifdef CONFIG_LINUX |
| 126 | |
| 127 | #define SYSFS_DT_BASEDIR "/proc/device-tree" |
| 128 | |
| 129 | /** |
| 130 | * read_fstree: this function is inspired from dtc read_fstree |
| 131 | * @fdt: preallocated fdt blob buffer, to be populated |
| 132 | * @dirname: directory to scan under SYSFS_DT_BASEDIR |
| 133 | * the search is recursive and the tree is searched down to the |
| 134 | * leaves (property files). |
| 135 | * |
| 136 | * the function asserts in case of error |
| 137 | */ |
| 138 | static void read_fstree(void *fdt, const char *dirname) |
| 139 | { |
| 140 | DIR *d; |
| 141 | struct dirent *de; |
| 142 | struct stat st; |
| 143 | const char *root_dir = SYSFS_DT_BASEDIR; |
| 144 | const char *parent_node; |
| 145 | |
| 146 | if (strstr(dirname, root_dir) != dirname) { |
Philippe Mathieu-Daudé | 38754e4 | 2018-06-29 15:11:00 +0100 | [diff] [blame] | 147 | error_report("%s: %s must be searched within %s", |
| 148 | __func__, dirname, root_dir); |
| 149 | exit(1); |
Eric Auger | 60e43e9 | 2016-02-19 09:42:29 -0700 | [diff] [blame] | 150 | } |
| 151 | parent_node = &dirname[strlen(SYSFS_DT_BASEDIR)]; |
| 152 | |
| 153 | d = opendir(dirname); |
| 154 | if (!d) { |
Philippe Mathieu-Daudé | 38754e4 | 2018-06-29 15:11:00 +0100 | [diff] [blame] | 155 | error_report("%s cannot open %s", __func__, dirname); |
| 156 | exit(1); |
Eric Auger | 60e43e9 | 2016-02-19 09:42:29 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | while ((de = readdir(d)) != NULL) { |
| 160 | char *tmpnam; |
| 161 | |
| 162 | if (!g_strcmp0(de->d_name, ".") |
| 163 | || !g_strcmp0(de->d_name, "..")) { |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | tmpnam = g_strdup_printf("%s/%s", dirname, de->d_name); |
| 168 | |
| 169 | if (lstat(tmpnam, &st) < 0) { |
Philippe Mathieu-Daudé | 38754e4 | 2018-06-29 15:11:00 +0100 | [diff] [blame] | 170 | error_report("%s cannot lstat %s", __func__, tmpnam); |
| 171 | exit(1); |
Eric Auger | 60e43e9 | 2016-02-19 09:42:29 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | if (S_ISREG(st.st_mode)) { |
| 175 | gchar *val; |
| 176 | gsize len; |
| 177 | |
| 178 | if (!g_file_get_contents(tmpnam, &val, &len, NULL)) { |
Philippe Mathieu-Daudé | 38754e4 | 2018-06-29 15:11:00 +0100 | [diff] [blame] | 179 | error_report("%s not able to extract info from %s", |
| 180 | __func__, tmpnam); |
| 181 | exit(1); |
Eric Auger | 60e43e9 | 2016-02-19 09:42:29 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | if (strlen(parent_node) > 0) { |
| 185 | qemu_fdt_setprop(fdt, parent_node, |
| 186 | de->d_name, val, len); |
| 187 | } else { |
| 188 | qemu_fdt_setprop(fdt, "/", de->d_name, val, len); |
| 189 | } |
| 190 | g_free(val); |
| 191 | } else if (S_ISDIR(st.st_mode)) { |
| 192 | char *node_name; |
| 193 | |
| 194 | node_name = g_strdup_printf("%s/%s", |
| 195 | parent_node, de->d_name); |
| 196 | qemu_fdt_add_subnode(fdt, node_name); |
| 197 | g_free(node_name); |
| 198 | read_fstree(fdt, tmpnam); |
| 199 | } |
| 200 | |
| 201 | g_free(tmpnam); |
| 202 | } |
| 203 | |
| 204 | closedir(d); |
| 205 | } |
| 206 | |
| 207 | /* load_device_tree_from_sysfs: extract the dt blob from host sysfs */ |
| 208 | void *load_device_tree_from_sysfs(void) |
| 209 | { |
| 210 | void *host_fdt; |
| 211 | int host_fdt_size; |
| 212 | |
| 213 | host_fdt = create_device_tree(&host_fdt_size); |
| 214 | read_fstree(host_fdt, SYSFS_DT_BASEDIR); |
| 215 | if (fdt_check_header(host_fdt)) { |
Philippe Mathieu-Daudé | 38754e4 | 2018-06-29 15:11:00 +0100 | [diff] [blame] | 216 | error_report("%s host device tree extracted into memory is invalid", |
| 217 | __func__); |
| 218 | exit(1); |
Eric Auger | 60e43e9 | 2016-02-19 09:42:29 -0700 | [diff] [blame] | 219 | } |
| 220 | return host_fdt; |
| 221 | } |
| 222 | |
| 223 | #endif /* CONFIG_LINUX */ |
| 224 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 225 | static int findnode_nofail(void *fdt, const char *node_path) |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 226 | { |
| 227 | int offset; |
| 228 | |
| 229 | offset = fdt_path_offset(fdt, node_path); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 230 | if (offset < 0) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 231 | error_report("%s Couldn't find node %s: %s", __func__, node_path, |
| 232 | fdt_strerror(offset)); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 233 | exit(1); |
| 234 | } |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 235 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 236 | return offset; |
| 237 | } |
| 238 | |
Eric Auger | f963cc2 | 2018-06-29 15:11:01 +0100 | [diff] [blame] | 239 | char **qemu_fdt_node_unit_path(void *fdt, const char *name, Error **errp) |
| 240 | { |
| 241 | char *prefix = g_strdup_printf("%s@", name); |
| 242 | unsigned int path_len = 16, n = 0; |
| 243 | GSList *path_list = NULL, *iter; |
| 244 | const char *iter_name; |
| 245 | int offset, len, ret; |
| 246 | char **path_array; |
| 247 | |
| 248 | offset = fdt_next_node(fdt, -1, NULL); |
| 249 | |
| 250 | while (offset >= 0) { |
| 251 | iter_name = fdt_get_name(fdt, offset, &len); |
| 252 | if (!iter_name) { |
| 253 | offset = len; |
| 254 | break; |
| 255 | } |
| 256 | if (!strcmp(iter_name, name) || g_str_has_prefix(iter_name, prefix)) { |
| 257 | char *path; |
| 258 | |
| 259 | path = g_malloc(path_len); |
| 260 | while ((ret = fdt_get_path(fdt, offset, path, path_len)) |
| 261 | == -FDT_ERR_NOSPACE) { |
| 262 | path_len += 16; |
| 263 | path = g_realloc(path, path_len); |
| 264 | } |
| 265 | path_list = g_slist_prepend(path_list, path); |
| 266 | n++; |
| 267 | } |
| 268 | offset = fdt_next_node(fdt, offset, NULL); |
| 269 | } |
| 270 | g_free(prefix); |
| 271 | |
| 272 | if (offset < 0 && offset != -FDT_ERR_NOTFOUND) { |
| 273 | error_setg(errp, "%s: abort parsing dt for %s node units: %s", |
| 274 | __func__, name, fdt_strerror(offset)); |
| 275 | for (iter = path_list; iter; iter = iter->next) { |
| 276 | g_free(iter->data); |
| 277 | } |
| 278 | g_slist_free(path_list); |
| 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | path_array = g_new(char *, n + 1); |
| 283 | path_array[n--] = NULL; |
| 284 | |
| 285 | for (iter = path_list; iter; iter = iter->next) { |
| 286 | path_array[n--] = iter->data; |
| 287 | } |
| 288 | |
| 289 | g_slist_free(path_list); |
| 290 | |
| 291 | return path_array; |
| 292 | } |
| 293 | |
Eric Auger | 6d79566 | 2016-02-19 09:42:30 -0700 | [diff] [blame] | 294 | char **qemu_fdt_node_path(void *fdt, const char *name, char *compat, |
| 295 | Error **errp) |
| 296 | { |
| 297 | int offset, len, ret; |
| 298 | const char *iter_name; |
| 299 | unsigned int path_len = 16, n = 0; |
| 300 | GSList *path_list = NULL, *iter; |
| 301 | char **path_array; |
| 302 | |
| 303 | offset = fdt_node_offset_by_compatible(fdt, -1, compat); |
| 304 | |
| 305 | while (offset >= 0) { |
| 306 | iter_name = fdt_get_name(fdt, offset, &len); |
| 307 | if (!iter_name) { |
| 308 | offset = len; |
| 309 | break; |
| 310 | } |
| 311 | if (!strcmp(iter_name, name)) { |
| 312 | char *path; |
| 313 | |
| 314 | path = g_malloc(path_len); |
| 315 | while ((ret = fdt_get_path(fdt, offset, path, path_len)) |
| 316 | == -FDT_ERR_NOSPACE) { |
| 317 | path_len += 16; |
| 318 | path = g_realloc(path, path_len); |
| 319 | } |
| 320 | path_list = g_slist_prepend(path_list, path); |
| 321 | n++; |
| 322 | } |
| 323 | offset = fdt_node_offset_by_compatible(fdt, offset, compat); |
| 324 | } |
| 325 | |
| 326 | if (offset < 0 && offset != -FDT_ERR_NOTFOUND) { |
| 327 | error_setg(errp, "%s: abort parsing dt for %s/%s: %s", |
| 328 | __func__, name, compat, fdt_strerror(offset)); |
| 329 | for (iter = path_list; iter; iter = iter->next) { |
| 330 | g_free(iter->data); |
| 331 | } |
| 332 | g_slist_free(path_list); |
| 333 | return NULL; |
| 334 | } |
| 335 | |
| 336 | path_array = g_new(char *, n + 1); |
| 337 | path_array[n--] = NULL; |
| 338 | |
| 339 | for (iter = path_list; iter; iter = iter->next) { |
| 340 | path_array[n--] = iter->data; |
| 341 | } |
| 342 | |
| 343 | g_slist_free(path_list); |
| 344 | |
| 345 | return path_array; |
| 346 | } |
| 347 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 348 | int qemu_fdt_setprop(void *fdt, const char *node_path, |
Peter Crosthwaite | be5907f | 2013-11-11 18:15:21 +1000 | [diff] [blame] | 349 | const char *property, const void *val, int size) |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 350 | { |
| 351 | int r; |
| 352 | |
Peter Crosthwaite | be5907f | 2013-11-11 18:15:21 +1000 | [diff] [blame] | 353 | r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 354 | if (r < 0) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 355 | error_report("%s: Couldn't set %s/%s: %s", __func__, node_path, |
| 356 | property, fdt_strerror(r)); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 357 | exit(1); |
| 358 | } |
| 359 | |
| 360 | return r; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 363 | int qemu_fdt_setprop_cell(void *fdt, const char *node_path, |
| 364 | const char *property, uint32_t val) |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 365 | { |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 366 | int r; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 367 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 368 | r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val); |
| 369 | if (r < 0) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 370 | error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__, |
| 371 | node_path, property, val, fdt_strerror(r)); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 372 | exit(1); |
| 373 | } |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 374 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 375 | return r; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 378 | int qemu_fdt_setprop_u64(void *fdt, const char *node_path, |
| 379 | const char *property, uint64_t val) |
Alexander Graf | bb28eb3 | 2012-05-18 01:53:01 +0200 | [diff] [blame] | 380 | { |
| 381 | val = cpu_to_be64(val); |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 382 | return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val)); |
Alexander Graf | bb28eb3 | 2012-05-18 01:53:01 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 385 | int qemu_fdt_setprop_string(void *fdt, const char *node_path, |
| 386 | const char *property, const char *string) |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 387 | { |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 388 | int r; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 389 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 390 | r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string); |
| 391 | if (r < 0) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 392 | error_report("%s: Couldn't set %s/%s = %s: %s", __func__, |
| 393 | node_path, property, string, fdt_strerror(r)); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 394 | exit(1); |
| 395 | } |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 396 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 397 | return r; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 398 | } |
Alexander Graf | d69a8e6 | 2011-07-21 01:52:57 +0200 | [diff] [blame] | 399 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 400 | const void *qemu_fdt_getprop(void *fdt, const char *node_path, |
Eric Auger | 78e24f2 | 2016-02-19 09:42:30 -0700 | [diff] [blame] | 401 | const char *property, int *lenp, Error **errp) |
Peter Maydell | f0aa713 | 2012-07-20 13:34:50 +0100 | [diff] [blame] | 402 | { |
| 403 | int len; |
| 404 | const void *r; |
Eric Auger | 78e24f2 | 2016-02-19 09:42:30 -0700 | [diff] [blame] | 405 | |
Peter Maydell | f0aa713 | 2012-07-20 13:34:50 +0100 | [diff] [blame] | 406 | if (!lenp) { |
| 407 | lenp = &len; |
| 408 | } |
| 409 | r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp); |
| 410 | if (!r) { |
Eric Auger | 78e24f2 | 2016-02-19 09:42:30 -0700 | [diff] [blame] | 411 | error_setg(errp, "%s: Couldn't get %s/%s: %s", __func__, |
| 412 | node_path, property, fdt_strerror(*lenp)); |
Peter Maydell | f0aa713 | 2012-07-20 13:34:50 +0100 | [diff] [blame] | 413 | } |
| 414 | return r; |
| 415 | } |
| 416 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 417 | uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path, |
Eric Auger | 58e7109 | 2016-02-19 09:42:30 -0700 | [diff] [blame] | 418 | const char *property, int *lenp, Error **errp) |
Peter Maydell | f0aa713 | 2012-07-20 13:34:50 +0100 | [diff] [blame] | 419 | { |
| 420 | int len; |
Eric Auger | 58e7109 | 2016-02-19 09:42:30 -0700 | [diff] [blame] | 421 | const uint32_t *p; |
| 422 | |
| 423 | if (!lenp) { |
| 424 | lenp = &len; |
| 425 | } |
| 426 | p = qemu_fdt_getprop(fdt, node_path, property, lenp, errp); |
| 427 | if (!p) { |
| 428 | return 0; |
| 429 | } else if (*lenp != 4) { |
| 430 | error_setg(errp, "%s: %s/%s not 4 bytes long (not a cell?)", |
| 431 | __func__, node_path, property); |
| 432 | *lenp = -EINVAL; |
| 433 | return 0; |
Peter Maydell | f0aa713 | 2012-07-20 13:34:50 +0100 | [diff] [blame] | 434 | } |
| 435 | return be32_to_cpu(*p); |
| 436 | } |
| 437 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 438 | uint32_t qemu_fdt_get_phandle(void *fdt, const char *path) |
Alexander Graf | 7d5fd10 | 2012-05-17 15:23:39 +0200 | [diff] [blame] | 439 | { |
| 440 | uint32_t r; |
| 441 | |
| 442 | r = fdt_get_phandle(fdt, findnode_nofail(fdt, path)); |
Stefan Weil | 909a196 | 2013-06-10 22:12:25 +0200 | [diff] [blame] | 443 | if (r == 0) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 444 | error_report("%s: Couldn't get phandle for %s: %s", __func__, |
| 445 | path, fdt_strerror(r)); |
Alexander Graf | 7d5fd10 | 2012-05-17 15:23:39 +0200 | [diff] [blame] | 446 | exit(1); |
| 447 | } |
| 448 | |
| 449 | return r; |
| 450 | } |
| 451 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 452 | int qemu_fdt_setprop_phandle(void *fdt, const char *node_path, |
| 453 | const char *property, |
| 454 | const char *target_node_path) |
Alexander Graf | 8535ab1 | 2012-05-17 14:11:52 +0200 | [diff] [blame] | 455 | { |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 456 | uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path); |
| 457 | return qemu_fdt_setprop_cell(fdt, node_path, property, phandle); |
Alexander Graf | 8535ab1 | 2012-05-17 14:11:52 +0200 | [diff] [blame] | 458 | } |
| 459 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 460 | uint32_t qemu_fdt_alloc_phandle(void *fdt) |
Alexander Graf | 3601b57 | 2012-05-17 16:58:55 +0200 | [diff] [blame] | 461 | { |
Alexander Graf | 4b1b1c8 | 2012-06-06 01:01:23 +0200 | [diff] [blame] | 462 | static int phandle = 0x0; |
| 463 | |
| 464 | /* |
| 465 | * We need to find out if the user gave us special instruction at |
Kamalesh Babulal | cc47a16 | 2015-07-24 13:48:13 +0530 | [diff] [blame] | 466 | * which phandle id to start allocating phandles. |
Alexander Graf | 4b1b1c8 | 2012-06-06 01:01:23 +0200 | [diff] [blame] | 467 | */ |
| 468 | if (!phandle) { |
Marcel Apfelbaum | 6cabe7f | 2015-02-04 17:43:53 +0200 | [diff] [blame] | 469 | phandle = machine_phandle_start(current_machine); |
Alexander Graf | 4b1b1c8 | 2012-06-06 01:01:23 +0200 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | if (!phandle) { |
| 473 | /* |
| 474 | * None or invalid phandle given on the command line, so fall back to |
| 475 | * default starting point. |
| 476 | */ |
| 477 | phandle = 0x8000; |
| 478 | } |
Alexander Graf | 3601b57 | 2012-05-17 16:58:55 +0200 | [diff] [blame] | 479 | |
| 480 | return phandle++; |
| 481 | } |
| 482 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 483 | int qemu_fdt_nop_node(void *fdt, const char *node_path) |
Alexander Graf | d69a8e6 | 2011-07-21 01:52:57 +0200 | [diff] [blame] | 484 | { |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 485 | int r; |
Alexander Graf | d69a8e6 | 2011-07-21 01:52:57 +0200 | [diff] [blame] | 486 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 487 | r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path)); |
| 488 | if (r < 0) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 489 | error_report("%s: Couldn't nop node %s: %s", __func__, node_path, |
| 490 | fdt_strerror(r)); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 491 | exit(1); |
| 492 | } |
Alexander Graf | d69a8e6 | 2011-07-21 01:52:57 +0200 | [diff] [blame] | 493 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 494 | return r; |
Alexander Graf | d69a8e6 | 2011-07-21 01:52:57 +0200 | [diff] [blame] | 495 | } |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 496 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 497 | int qemu_fdt_add_subnode(void *fdt, const char *name) |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 498 | { |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 499 | char *dupname = g_strdup(name); |
| 500 | char *basename = strrchr(dupname, '/'); |
| 501 | int retval; |
Alexander Graf | c640d08 | 2012-05-17 11:40:42 +0200 | [diff] [blame] | 502 | int parent = 0; |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 503 | |
| 504 | if (!basename) { |
Stefan Weil | bff39b6 | 2011-10-17 22:12:09 +0200 | [diff] [blame] | 505 | g_free(dupname); |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 506 | return -1; |
| 507 | } |
| 508 | |
| 509 | basename[0] = '\0'; |
| 510 | basename++; |
| 511 | |
Alexander Graf | c640d08 | 2012-05-17 11:40:42 +0200 | [diff] [blame] | 512 | if (dupname[0]) { |
| 513 | parent = findnode_nofail(fdt, dupname); |
| 514 | } |
| 515 | |
| 516 | retval = fdt_add_subnode(fdt, parent, basename); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 517 | if (retval < 0) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 518 | error_report("FDT: Failed to create subnode %s: %s", name, |
| 519 | fdt_strerror(retval)); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 520 | exit(1); |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 521 | } |
| 522 | |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 523 | g_free(dupname); |
| 524 | return retval; |
| 525 | } |
Alexander Graf | 7119343 | 2012-09-23 08:37:59 +0200 | [diff] [blame] | 526 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 527 | void qemu_fdt_dumpdtb(void *fdt, int size) |
Alexander Graf | 7119343 | 2012-09-23 08:37:59 +0200 | [diff] [blame] | 528 | { |
Markus Armbruster | 2ff3de6 | 2013-07-04 15:09:22 +0200 | [diff] [blame] | 529 | const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb"); |
Alexander Graf | 7119343 | 2012-09-23 08:37:59 +0200 | [diff] [blame] | 530 | |
Markus Armbruster | 2ff3de6 | 2013-07-04 15:09:22 +0200 | [diff] [blame] | 531 | if (dumpdtb) { |
| 532 | /* Dump the dtb to a file and quit */ |
| 533 | exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1); |
Alexander Graf | 7119343 | 2012-09-23 08:37:59 +0200 | [diff] [blame] | 534 | } |
Alexander Graf | 7119343 | 2012-09-23 08:37:59 +0200 | [diff] [blame] | 535 | } |
Peter Maydell | 97c38f8 | 2013-07-16 13:25:05 +0100 | [diff] [blame] | 536 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 537 | int qemu_fdt_setprop_sized_cells_from_array(void *fdt, |
| 538 | const char *node_path, |
| 539 | const char *property, |
| 540 | int numvalues, |
| 541 | uint64_t *values) |
Peter Maydell | 97c38f8 | 2013-07-16 13:25:05 +0100 | [diff] [blame] | 542 | { |
| 543 | uint32_t *propcells; |
| 544 | uint64_t value; |
| 545 | int cellnum, vnum, ncells; |
| 546 | uint32_t hival; |
Sergey Fedorov | 2bf9feb | 2014-12-11 18:45:05 +0300 | [diff] [blame] | 547 | int ret; |
Peter Maydell | 97c38f8 | 2013-07-16 13:25:05 +0100 | [diff] [blame] | 548 | |
| 549 | propcells = g_new0(uint32_t, numvalues * 2); |
| 550 | |
| 551 | cellnum = 0; |
| 552 | for (vnum = 0; vnum < numvalues; vnum++) { |
| 553 | ncells = values[vnum * 2]; |
| 554 | if (ncells != 1 && ncells != 2) { |
Sergey Fedorov | 2bf9feb | 2014-12-11 18:45:05 +0300 | [diff] [blame] | 555 | ret = -1; |
| 556 | goto out; |
Peter Maydell | 97c38f8 | 2013-07-16 13:25:05 +0100 | [diff] [blame] | 557 | } |
| 558 | value = values[vnum * 2 + 1]; |
| 559 | hival = cpu_to_be32(value >> 32); |
| 560 | if (ncells > 1) { |
| 561 | propcells[cellnum++] = hival; |
| 562 | } else if (hival != 0) { |
Sergey Fedorov | 2bf9feb | 2014-12-11 18:45:05 +0300 | [diff] [blame] | 563 | ret = -1; |
| 564 | goto out; |
Peter Maydell | 97c38f8 | 2013-07-16 13:25:05 +0100 | [diff] [blame] | 565 | } |
| 566 | propcells[cellnum++] = cpu_to_be32(value); |
| 567 | } |
| 568 | |
Sergey Fedorov | 2bf9feb | 2014-12-11 18:45:05 +0300 | [diff] [blame] | 569 | ret = qemu_fdt_setprop(fdt, node_path, property, propcells, |
| 570 | cellnum * sizeof(uint32_t)); |
| 571 | out: |
| 572 | g_free(propcells); |
| 573 | return ret; |
Peter Maydell | 97c38f8 | 2013-07-16 13:25:05 +0100 | [diff] [blame] | 574 | } |