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 | |
| 14 | #include <stdio.h> |
| 15 | #include <sys/types.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <unistd.h> |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | #include "config.h" |
| 22 | #include "qemu-common.h" |
Li Liu | db013f8 | 2014-08-26 14:38:02 +0800 | [diff] [blame] | 23 | #include "qemu/error-report.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 | |
Alexander Graf | ce36252 | 2012-05-17 15:33:54 +0200 | [diff] [blame] | 32 | #define FDT_MAX_SIZE 0x10000 |
| 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 | } |
| 87 | |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 88 | /* Expand to 2x size to give enough room for manipulation. */ |
Alexander Graf | ded57c5 | 2011-07-23 10:54:11 +0200 | [diff] [blame] | 89 | dt_size += 10000; |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 90 | dt_size *= 2; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 91 | /* First allocate space in qemu for device tree */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 92 | fdt = g_malloc0(dt_size); |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 93 | |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 94 | dt_file_load_size = load_image(filename_path, fdt); |
| 95 | if (dt_file_load_size < 0) { |
Li Liu | db013f8 | 2014-08-26 14:38:02 +0800 | [diff] [blame] | 96 | error_report("Unable to open device tree file '%s'", |
| 97 | filename_path); |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 98 | goto fail; |
| 99 | } |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 100 | |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 101 | ret = fdt_open_into(fdt, fdt, dt_size); |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 102 | if (ret) { |
Li Liu | db013f8 | 2014-08-26 14:38:02 +0800 | [diff] [blame] | 103 | error_report("Unable to copy device tree in memory"); |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 104 | goto fail; |
| 105 | } |
| 106 | |
| 107 | /* Check sanity of device tree */ |
| 108 | if (fdt_check_header(fdt)) { |
Li Liu | db013f8 | 2014-08-26 14:38:02 +0800 | [diff] [blame] | 109 | error_report("Device tree file loaded into memory is invalid: %s", |
| 110 | filename_path); |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 111 | goto fail; |
| 112 | } |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 113 | *sizep = dt_size; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 114 | return fdt; |
| 115 | |
| 116 | fail: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 117 | g_free(fdt); |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 118 | return NULL; |
| 119 | } |
| 120 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 121 | static int findnode_nofail(void *fdt, const char *node_path) |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 122 | { |
| 123 | int offset; |
| 124 | |
| 125 | offset = fdt_path_offset(fdt, node_path); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 126 | if (offset < 0) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 127 | error_report("%s Couldn't find node %s: %s", __func__, node_path, |
| 128 | fdt_strerror(offset)); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 129 | exit(1); |
| 130 | } |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 131 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 132 | return offset; |
| 133 | } |
| 134 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 135 | int qemu_fdt_setprop(void *fdt, const char *node_path, |
Peter Crosthwaite | be5907f | 2013-11-11 18:15:21 +1000 | [diff] [blame] | 136 | const char *property, const void *val, int size) |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 137 | { |
| 138 | int r; |
| 139 | |
Peter Crosthwaite | be5907f | 2013-11-11 18:15:21 +1000 | [diff] [blame] | 140 | r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 141 | if (r < 0) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 142 | error_report("%s: Couldn't set %s/%s: %s", __func__, node_path, |
| 143 | property, fdt_strerror(r)); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 144 | exit(1); |
| 145 | } |
| 146 | |
| 147 | return r; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 150 | int qemu_fdt_setprop_cell(void *fdt, const char *node_path, |
| 151 | const char *property, uint32_t val) |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 152 | { |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 153 | int r; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 154 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 155 | r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val); |
| 156 | if (r < 0) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 157 | error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__, |
| 158 | node_path, property, val, fdt_strerror(r)); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 159 | exit(1); |
| 160 | } |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 161 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 162 | return r; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 165 | int qemu_fdt_setprop_u64(void *fdt, const char *node_path, |
| 166 | const char *property, uint64_t val) |
Alexander Graf | bb28eb3 | 2012-05-18 01:53:01 +0200 | [diff] [blame] | 167 | { |
| 168 | val = cpu_to_be64(val); |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 169 | return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val)); |
Alexander Graf | bb28eb3 | 2012-05-18 01:53:01 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 172 | int qemu_fdt_setprop_string(void *fdt, const char *node_path, |
| 173 | const char *property, const char *string) |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 174 | { |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 175 | int r; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 176 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 177 | r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string); |
| 178 | if (r < 0) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 179 | error_report("%s: Couldn't set %s/%s = %s: %s", __func__, |
| 180 | node_path, property, string, fdt_strerror(r)); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 181 | exit(1); |
| 182 | } |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 183 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 184 | return r; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 185 | } |
Alexander Graf | d69a8e6 | 2011-07-21 01:52:57 +0200 | [diff] [blame] | 186 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 187 | const void *qemu_fdt_getprop(void *fdt, const char *node_path, |
| 188 | const char *property, int *lenp) |
Peter Maydell | f0aa713 | 2012-07-20 13:34:50 +0100 | [diff] [blame] | 189 | { |
| 190 | int len; |
| 191 | const void *r; |
| 192 | if (!lenp) { |
| 193 | lenp = &len; |
| 194 | } |
| 195 | r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp); |
| 196 | if (!r) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 197 | error_report("%s: Couldn't get %s/%s: %s", __func__, |
| 198 | node_path, property, fdt_strerror(*lenp)); |
Peter Maydell | f0aa713 | 2012-07-20 13:34:50 +0100 | [diff] [blame] | 199 | exit(1); |
| 200 | } |
| 201 | return r; |
| 202 | } |
| 203 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 204 | uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path, |
| 205 | const char *property) |
Peter Maydell | f0aa713 | 2012-07-20 13:34:50 +0100 | [diff] [blame] | 206 | { |
| 207 | int len; |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 208 | const uint32_t *p = qemu_fdt_getprop(fdt, node_path, property, &len); |
Peter Maydell | f0aa713 | 2012-07-20 13:34:50 +0100 | [diff] [blame] | 209 | if (len != 4) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 210 | error_report("%s: %s/%s not 4 bytes long (not a cell?)", |
| 211 | __func__, node_path, property); |
Peter Maydell | f0aa713 | 2012-07-20 13:34:50 +0100 | [diff] [blame] | 212 | exit(1); |
| 213 | } |
| 214 | return be32_to_cpu(*p); |
| 215 | } |
| 216 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 217 | uint32_t qemu_fdt_get_phandle(void *fdt, const char *path) |
Alexander Graf | 7d5fd10 | 2012-05-17 15:23:39 +0200 | [diff] [blame] | 218 | { |
| 219 | uint32_t r; |
| 220 | |
| 221 | r = fdt_get_phandle(fdt, findnode_nofail(fdt, path)); |
Stefan Weil | 909a196 | 2013-06-10 22:12:25 +0200 | [diff] [blame] | 222 | if (r == 0) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 223 | error_report("%s: Couldn't get phandle for %s: %s", __func__, |
| 224 | path, fdt_strerror(r)); |
Alexander Graf | 7d5fd10 | 2012-05-17 15:23:39 +0200 | [diff] [blame] | 225 | exit(1); |
| 226 | } |
| 227 | |
| 228 | return r; |
| 229 | } |
| 230 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 231 | int qemu_fdt_setprop_phandle(void *fdt, const char *node_path, |
| 232 | const char *property, |
| 233 | const char *target_node_path) |
Alexander Graf | 8535ab1 | 2012-05-17 14:11:52 +0200 | [diff] [blame] | 234 | { |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 235 | uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path); |
| 236 | return qemu_fdt_setprop_cell(fdt, node_path, property, phandle); |
Alexander Graf | 8535ab1 | 2012-05-17 14:11:52 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 239 | uint32_t qemu_fdt_alloc_phandle(void *fdt) |
Alexander Graf | 3601b57 | 2012-05-17 16:58:55 +0200 | [diff] [blame] | 240 | { |
Alexander Graf | 4b1b1c8 | 2012-06-06 01:01:23 +0200 | [diff] [blame] | 241 | static int phandle = 0x0; |
| 242 | |
| 243 | /* |
| 244 | * We need to find out if the user gave us special instruction at |
| 245 | * which phandle id to start allocting phandles. |
| 246 | */ |
| 247 | if (!phandle) { |
Marcel Apfelbaum | 6cabe7f | 2015-02-04 17:43:53 +0200 | [diff] [blame] | 248 | phandle = machine_phandle_start(current_machine); |
Alexander Graf | 4b1b1c8 | 2012-06-06 01:01:23 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | if (!phandle) { |
| 252 | /* |
| 253 | * None or invalid phandle given on the command line, so fall back to |
| 254 | * default starting point. |
| 255 | */ |
| 256 | phandle = 0x8000; |
| 257 | } |
Alexander Graf | 3601b57 | 2012-05-17 16:58:55 +0200 | [diff] [blame] | 258 | |
| 259 | return phandle++; |
| 260 | } |
| 261 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 262 | int qemu_fdt_nop_node(void *fdt, const char *node_path) |
Alexander Graf | d69a8e6 | 2011-07-21 01:52:57 +0200 | [diff] [blame] | 263 | { |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 264 | int r; |
Alexander Graf | d69a8e6 | 2011-07-21 01:52:57 +0200 | [diff] [blame] | 265 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 266 | r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path)); |
| 267 | if (r < 0) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 268 | error_report("%s: Couldn't nop node %s: %s", __func__, node_path, |
| 269 | fdt_strerror(r)); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 270 | exit(1); |
| 271 | } |
Alexander Graf | d69a8e6 | 2011-07-21 01:52:57 +0200 | [diff] [blame] | 272 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 273 | return r; |
Alexander Graf | d69a8e6 | 2011-07-21 01:52:57 +0200 | [diff] [blame] | 274 | } |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 275 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 276 | int qemu_fdt_add_subnode(void *fdt, const char *name) |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 277 | { |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 278 | char *dupname = g_strdup(name); |
| 279 | char *basename = strrchr(dupname, '/'); |
| 280 | int retval; |
Alexander Graf | c640d08 | 2012-05-17 11:40:42 +0200 | [diff] [blame] | 281 | int parent = 0; |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 282 | |
| 283 | if (!basename) { |
Stefan Weil | bff39b6 | 2011-10-17 22:12:09 +0200 | [diff] [blame] | 284 | g_free(dupname); |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 285 | return -1; |
| 286 | } |
| 287 | |
| 288 | basename[0] = '\0'; |
| 289 | basename++; |
| 290 | |
Alexander Graf | c640d08 | 2012-05-17 11:40:42 +0200 | [diff] [blame] | 291 | if (dupname[0]) { |
| 292 | parent = findnode_nofail(fdt, dupname); |
| 293 | } |
| 294 | |
| 295 | retval = fdt_add_subnode(fdt, parent, basename); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 296 | if (retval < 0) { |
Li Liu | 508e221 | 2014-08-26 14:38:03 +0800 | [diff] [blame] | 297 | error_report("FDT: Failed to create subnode %s: %s", name, |
| 298 | fdt_strerror(retval)); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 299 | exit(1); |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 302 | g_free(dupname); |
| 303 | return retval; |
| 304 | } |
Alexander Graf | 7119343 | 2012-09-23 08:37:59 +0200 | [diff] [blame] | 305 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 306 | void qemu_fdt_dumpdtb(void *fdt, int size) |
Alexander Graf | 7119343 | 2012-09-23 08:37:59 +0200 | [diff] [blame] | 307 | { |
Markus Armbruster | 2ff3de6 | 2013-07-04 15:09:22 +0200 | [diff] [blame] | 308 | const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb"); |
Alexander Graf | 7119343 | 2012-09-23 08:37:59 +0200 | [diff] [blame] | 309 | |
Markus Armbruster | 2ff3de6 | 2013-07-04 15:09:22 +0200 | [diff] [blame] | 310 | if (dumpdtb) { |
| 311 | /* Dump the dtb to a file and quit */ |
| 312 | exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1); |
Alexander Graf | 7119343 | 2012-09-23 08:37:59 +0200 | [diff] [blame] | 313 | } |
Alexander Graf | 7119343 | 2012-09-23 08:37:59 +0200 | [diff] [blame] | 314 | } |
Peter Maydell | 97c38f8 | 2013-07-16 13:25:05 +0100 | [diff] [blame] | 315 | |
Peter Crosthwaite | 5a4348d | 2013-11-11 18:14:41 +1000 | [diff] [blame] | 316 | int qemu_fdt_setprop_sized_cells_from_array(void *fdt, |
| 317 | const char *node_path, |
| 318 | const char *property, |
| 319 | int numvalues, |
| 320 | uint64_t *values) |
Peter Maydell | 97c38f8 | 2013-07-16 13:25:05 +0100 | [diff] [blame] | 321 | { |
| 322 | uint32_t *propcells; |
| 323 | uint64_t value; |
| 324 | int cellnum, vnum, ncells; |
| 325 | uint32_t hival; |
Sergey Fedorov | 2bf9feb | 2014-12-11 18:45:05 +0300 | [diff] [blame] | 326 | int ret; |
Peter Maydell | 97c38f8 | 2013-07-16 13:25:05 +0100 | [diff] [blame] | 327 | |
| 328 | propcells = g_new0(uint32_t, numvalues * 2); |
| 329 | |
| 330 | cellnum = 0; |
| 331 | for (vnum = 0; vnum < numvalues; vnum++) { |
| 332 | ncells = values[vnum * 2]; |
| 333 | if (ncells != 1 && ncells != 2) { |
Sergey Fedorov | 2bf9feb | 2014-12-11 18:45:05 +0300 | [diff] [blame] | 334 | ret = -1; |
| 335 | goto out; |
Peter Maydell | 97c38f8 | 2013-07-16 13:25:05 +0100 | [diff] [blame] | 336 | } |
| 337 | value = values[vnum * 2 + 1]; |
| 338 | hival = cpu_to_be32(value >> 32); |
| 339 | if (ncells > 1) { |
| 340 | propcells[cellnum++] = hival; |
| 341 | } else if (hival != 0) { |
Sergey Fedorov | 2bf9feb | 2014-12-11 18:45:05 +0300 | [diff] [blame] | 342 | ret = -1; |
| 343 | goto out; |
Peter Maydell | 97c38f8 | 2013-07-16 13:25:05 +0100 | [diff] [blame] | 344 | } |
| 345 | propcells[cellnum++] = cpu_to_be32(value); |
| 346 | } |
| 347 | |
Sergey Fedorov | 2bf9feb | 2014-12-11 18:45:05 +0300 | [diff] [blame] | 348 | ret = qemu_fdt_setprop(fdt, node_path, property, propcells, |
| 349 | cellnum * sizeof(uint32_t)); |
| 350 | out: |
| 351 | g_free(propcells); |
| 352 | return ret; |
Peter Maydell | 97c38f8 | 2013-07-16 13:25:05 +0100 | [diff] [blame] | 353 | } |