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" |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 23 | #include "device_tree.h" |
Blue Swirl | 39b7f20 | 2009-09-22 17:51:36 +0000 | [diff] [blame] | 24 | #include "hw/loader.h" |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 25 | |
| 26 | #include <libfdt.h> |
| 27 | |
Alexander Graf | ce36252 | 2012-05-17 15:33:54 +0200 | [diff] [blame^] | 28 | #define FDT_MAX_SIZE 0x10000 |
| 29 | |
| 30 | void *create_device_tree(int *sizep) |
| 31 | { |
| 32 | void *fdt; |
| 33 | int ret; |
| 34 | |
| 35 | *sizep = FDT_MAX_SIZE; |
| 36 | fdt = g_malloc0(FDT_MAX_SIZE); |
| 37 | ret = fdt_create(fdt, FDT_MAX_SIZE); |
| 38 | if (ret < 0) { |
| 39 | goto fail; |
| 40 | } |
| 41 | ret = fdt_begin_node(fdt, ""); |
| 42 | if (ret < 0) { |
| 43 | goto fail; |
| 44 | } |
| 45 | ret = fdt_end_node(fdt); |
| 46 | if (ret < 0) { |
| 47 | goto fail; |
| 48 | } |
| 49 | ret = fdt_finish(fdt); |
| 50 | if (ret < 0) { |
| 51 | goto fail; |
| 52 | } |
| 53 | ret = fdt_open_into(fdt, fdt, *sizep); |
| 54 | if (ret) { |
| 55 | fprintf(stderr, "Unable to copy device tree in memory\n"); |
| 56 | exit(1); |
| 57 | } |
| 58 | |
| 59 | return fdt; |
| 60 | fail: |
| 61 | fprintf(stderr, "%s Couldn't create dt: %s\n", __func__, fdt_strerror(ret)); |
| 62 | exit(1); |
| 63 | } |
| 64 | |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 65 | void *load_device_tree(const char *filename_path, int *sizep) |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 66 | { |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 67 | int dt_size; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 68 | int dt_file_load_size; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 69 | int ret; |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 70 | void *fdt = NULL; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 71 | |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 72 | *sizep = 0; |
| 73 | dt_size = get_image_size(filename_path); |
| 74 | if (dt_size < 0) { |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 75 | printf("Unable to get size of device tree file '%s'\n", |
| 76 | filename_path); |
| 77 | goto fail; |
| 78 | } |
| 79 | |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 80 | /* Expand to 2x size to give enough room for manipulation. */ |
Alexander Graf | ded57c5 | 2011-07-23 10:54:11 +0200 | [diff] [blame] | 81 | dt_size += 10000; |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 82 | dt_size *= 2; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 83 | /* First allocate space in qemu for device tree */ |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 84 | fdt = g_malloc0(dt_size); |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 85 | |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 86 | dt_file_load_size = load_image(filename_path, fdt); |
| 87 | if (dt_file_load_size < 0) { |
| 88 | printf("Unable to open device tree file '%s'\n", |
| 89 | filename_path); |
| 90 | goto fail; |
| 91 | } |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 92 | |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 93 | ret = fdt_open_into(fdt, fdt, dt_size); |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 94 | if (ret) { |
| 95 | printf("Unable to copy device tree in memory\n"); |
| 96 | goto fail; |
| 97 | } |
| 98 | |
| 99 | /* Check sanity of device tree */ |
| 100 | if (fdt_check_header(fdt)) { |
| 101 | printf ("Device tree file loaded into memory is invalid: %s\n", |
| 102 | filename_path); |
| 103 | goto fail; |
| 104 | } |
pbrook | 7ec632b | 2009-04-10 16:23:59 +0000 | [diff] [blame] | 105 | *sizep = dt_size; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 106 | return fdt; |
| 107 | |
| 108 | fail: |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 109 | g_free(fdt); |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 110 | return NULL; |
| 111 | } |
| 112 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 113 | static int findnode_nofail(void *fdt, const char *node_path) |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 114 | { |
| 115 | int offset; |
| 116 | |
| 117 | offset = fdt_path_offset(fdt, node_path); |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 118 | if (offset < 0) { |
| 119 | fprintf(stderr, "%s Couldn't find node %s: %s\n", __func__, node_path, |
| 120 | fdt_strerror(offset)); |
| 121 | exit(1); |
| 122 | } |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 123 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 124 | return offset; |
| 125 | } |
| 126 | |
| 127 | int qemu_devtree_setprop(void *fdt, const char *node_path, |
| 128 | const char *property, void *val_array, int size) |
| 129 | { |
| 130 | int r; |
| 131 | |
| 132 | r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val_array, size); |
| 133 | if (r < 0) { |
| 134 | fprintf(stderr, "%s: Couldn't set %s/%s: %s\n", __func__, node_path, |
| 135 | property, fdt_strerror(r)); |
| 136 | exit(1); |
| 137 | } |
| 138 | |
| 139 | return r; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | int qemu_devtree_setprop_cell(void *fdt, const char *node_path, |
| 143 | const char *property, uint32_t val) |
| 144 | { |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 145 | int r; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 146 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 147 | r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val); |
| 148 | if (r < 0) { |
| 149 | fprintf(stderr, "%s: Couldn't set %s/%s = %#08x: %s\n", __func__, |
| 150 | node_path, property, val, fdt_strerror(r)); |
| 151 | exit(1); |
| 152 | } |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 153 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 154 | return r; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | int qemu_devtree_setprop_string(void *fdt, const char *node_path, |
| 158 | const char *property, const char *string) |
| 159 | { |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 160 | int r; |
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 | r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string); |
| 163 | if (r < 0) { |
| 164 | fprintf(stderr, "%s: Couldn't set %s/%s = %s: %s\n", __func__, |
| 165 | node_path, property, string, fdt_strerror(r)); |
| 166 | exit(1); |
| 167 | } |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 168 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 169 | return r; |
aurel32 | f652e6a | 2008-12-16 10:43:48 +0000 | [diff] [blame] | 170 | } |
Alexander Graf | d69a8e6 | 2011-07-21 01:52:57 +0200 | [diff] [blame] | 171 | |
Alexander Graf | 7d5fd10 | 2012-05-17 15:23:39 +0200 | [diff] [blame] | 172 | uint32_t qemu_devtree_get_phandle(void *fdt, const char *path) |
| 173 | { |
| 174 | uint32_t r; |
| 175 | |
| 176 | r = fdt_get_phandle(fdt, findnode_nofail(fdt, path)); |
| 177 | if (r <= 0) { |
| 178 | fprintf(stderr, "%s: Couldn't get phandle for %s: %s\n", __func__, |
| 179 | path, fdt_strerror(r)); |
| 180 | exit(1); |
| 181 | } |
| 182 | |
| 183 | return r; |
| 184 | } |
| 185 | |
Alexander Graf | 8535ab1 | 2012-05-17 14:11:52 +0200 | [diff] [blame] | 186 | int qemu_devtree_setprop_phandle(void *fdt, const char *node_path, |
| 187 | const char *property, |
| 188 | const char *target_node_path) |
| 189 | { |
Alexander Graf | 7d5fd10 | 2012-05-17 15:23:39 +0200 | [diff] [blame] | 190 | uint32_t phandle = qemu_devtree_get_phandle(fdt, target_node_path); |
Alexander Graf | 8535ab1 | 2012-05-17 14:11:52 +0200 | [diff] [blame] | 191 | return qemu_devtree_setprop_cell(fdt, node_path, property, phandle); |
| 192 | } |
| 193 | |
Alexander Graf | d69a8e6 | 2011-07-21 01:52:57 +0200 | [diff] [blame] | 194 | int qemu_devtree_nop_node(void *fdt, const char *node_path) |
| 195 | { |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 196 | int r; |
Alexander Graf | d69a8e6 | 2011-07-21 01:52:57 +0200 | [diff] [blame] | 197 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 198 | r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path)); |
| 199 | if (r < 0) { |
| 200 | fprintf(stderr, "%s: Couldn't nop node %s: %s\n", __func__, node_path, |
| 201 | fdt_strerror(r)); |
| 202 | exit(1); |
| 203 | } |
Alexander Graf | d69a8e6 | 2011-07-21 01:52:57 +0200 | [diff] [blame] | 204 | |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 205 | return r; |
Alexander Graf | d69a8e6 | 2011-07-21 01:52:57 +0200 | [diff] [blame] | 206 | } |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 207 | |
| 208 | int qemu_devtree_add_subnode(void *fdt, const char *name) |
| 209 | { |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 210 | char *dupname = g_strdup(name); |
| 211 | char *basename = strrchr(dupname, '/'); |
| 212 | int retval; |
Alexander Graf | c640d08 | 2012-05-17 11:40:42 +0200 | [diff] [blame] | 213 | int parent = 0; |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 214 | |
| 215 | if (!basename) { |
Stefan Weil | bff39b6 | 2011-10-17 22:12:09 +0200 | [diff] [blame] | 216 | g_free(dupname); |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 217 | return -1; |
| 218 | } |
| 219 | |
| 220 | basename[0] = '\0'; |
| 221 | basename++; |
| 222 | |
Alexander Graf | c640d08 | 2012-05-17 11:40:42 +0200 | [diff] [blame] | 223 | if (dupname[0]) { |
| 224 | parent = findnode_nofail(fdt, dupname); |
| 225 | } |
| 226 | |
| 227 | retval = fdt_add_subnode(fdt, parent, basename); |
Alexander Graf | ef5d833 | 2012-05-17 14:12:57 +0200 | [diff] [blame] | 228 | #if 0 |
Alexander Graf | ccbcfed | 2011-07-23 10:52:00 +0200 | [diff] [blame] | 229 | if (retval < 0) { |
| 230 | fprintf(stderr, "FDT: Failed to create subnode %s: %s\n", name, |
| 231 | fdt_strerror(retval)); |
| 232 | exit(1); |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 233 | } |
Alexander Graf | ef5d833 | 2012-05-17 14:12:57 +0200 | [diff] [blame] | 234 | #endif |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 235 | |
Alexander Graf | 80ad781 | 2011-07-22 13:55:37 +0200 | [diff] [blame] | 236 | g_free(dupname); |
| 237 | return retval; |
| 238 | } |