]> git.proxmox.com Git - qemu.git/blame - device_tree.c
dt: temporarily disable subtree creation failure check
[qemu.git] / device_tree.c
CommitLineData
f652e6af
AJ
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"
f652e6af 23#include "device_tree.h"
39b7f20e 24#include "hw/loader.h"
f652e6af
AJ
25
26#include <libfdt.h>
27
7ec632b4 28void *load_device_tree(const char *filename_path, int *sizep)
f652e6af 29{
7ec632b4 30 int dt_size;
f652e6af 31 int dt_file_load_size;
f652e6af 32 int ret;
7ec632b4 33 void *fdt = NULL;
f652e6af 34
7ec632b4
PB
35 *sizep = 0;
36 dt_size = get_image_size(filename_path);
37 if (dt_size < 0) {
f652e6af
AJ
38 printf("Unable to get size of device tree file '%s'\n",
39 filename_path);
40 goto fail;
41 }
42
7ec632b4 43 /* Expand to 2x size to give enough room for manipulation. */
ded57c5f 44 dt_size += 10000;
7ec632b4 45 dt_size *= 2;
f652e6af 46 /* First allocate space in qemu for device tree */
7267c094 47 fdt = g_malloc0(dt_size);
f652e6af 48
7ec632b4
PB
49 dt_file_load_size = load_image(filename_path, fdt);
50 if (dt_file_load_size < 0) {
51 printf("Unable to open device tree file '%s'\n",
52 filename_path);
53 goto fail;
54 }
f652e6af 55
7ec632b4 56 ret = fdt_open_into(fdt, fdt, dt_size);
f652e6af
AJ
57 if (ret) {
58 printf("Unable to copy device tree in memory\n");
59 goto fail;
60 }
61
62 /* Check sanity of device tree */
63 if (fdt_check_header(fdt)) {
64 printf ("Device tree file loaded into memory is invalid: %s\n",
65 filename_path);
66 goto fail;
67 }
7ec632b4 68 *sizep = dt_size;
f652e6af
AJ
69 return fdt;
70
71fail:
7267c094 72 g_free(fdt);
f652e6af
AJ
73 return NULL;
74}
75
ccbcfedd 76static int findnode_nofail(void *fdt, const char *node_path)
f652e6af
AJ
77{
78 int offset;
79
80 offset = fdt_path_offset(fdt, node_path);
ccbcfedd
AG
81 if (offset < 0) {
82 fprintf(stderr, "%s Couldn't find node %s: %s\n", __func__, node_path,
83 fdt_strerror(offset));
84 exit(1);
85 }
86
87 return offset;
88}
89
90int qemu_devtree_setprop(void *fdt, const char *node_path,
91 const char *property, void *val_array, int size)
92{
93 int r;
94
95 r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val_array, size);
96 if (r < 0) {
97 fprintf(stderr, "%s: Couldn't set %s/%s: %s\n", __func__, node_path,
98 property, fdt_strerror(r));
99 exit(1);
100 }
f652e6af 101
ccbcfedd 102 return r;
f652e6af
AJ
103}
104
105int qemu_devtree_setprop_cell(void *fdt, const char *node_path,
106 const char *property, uint32_t val)
107{
ccbcfedd 108 int r;
f652e6af 109
ccbcfedd
AG
110 r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
111 if (r < 0) {
112 fprintf(stderr, "%s: Couldn't set %s/%s = %#08x: %s\n", __func__,
113 node_path, property, val, fdt_strerror(r));
114 exit(1);
115 }
f652e6af 116
ccbcfedd 117 return r;
f652e6af
AJ
118}
119
120int qemu_devtree_setprop_string(void *fdt, const char *node_path,
121 const char *property, const char *string)
122{
ccbcfedd 123 int r;
f652e6af 124
ccbcfedd
AG
125 r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
126 if (r < 0) {
127 fprintf(stderr, "%s: Couldn't set %s/%s = %s: %s\n", __func__,
128 node_path, property, string, fdt_strerror(r));
129 exit(1);
130 }
f652e6af 131
ccbcfedd 132 return r;
f652e6af 133}
d69a8e63 134
8535ab12
AG
135int qemu_devtree_setprop_phandle(void *fdt, const char *node_path,
136 const char *property,
137 const char *target_node_path)
138{
139 uint32_t phandle = fdt_get_phandle(fdt, findnode_nofail(fdt, target_node_path));
140 return qemu_devtree_setprop_cell(fdt, node_path, property, phandle);
141}
142
d69a8e63
AG
143int qemu_devtree_nop_node(void *fdt, const char *node_path)
144{
ccbcfedd 145 int r;
d69a8e63 146
ccbcfedd
AG
147 r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
148 if (r < 0) {
149 fprintf(stderr, "%s: Couldn't nop node %s: %s\n", __func__, node_path,
150 fdt_strerror(r));
151 exit(1);
152 }
d69a8e63 153
ccbcfedd 154 return r;
d69a8e63 155}
80ad7816
AG
156
157int qemu_devtree_add_subnode(void *fdt, const char *name)
158{
80ad7816
AG
159 char *dupname = g_strdup(name);
160 char *basename = strrchr(dupname, '/');
161 int retval;
c640d088 162 int parent = 0;
80ad7816
AG
163
164 if (!basename) {
bff39b63 165 g_free(dupname);
80ad7816
AG
166 return -1;
167 }
168
169 basename[0] = '\0';
170 basename++;
171
c640d088
AG
172 if (dupname[0]) {
173 parent = findnode_nofail(fdt, dupname);
174 }
175
176 retval = fdt_add_subnode(fdt, parent, basename);
ef5d833f 177#if 0
ccbcfedd
AG
178 if (retval < 0) {
179 fprintf(stderr, "FDT: Failed to create subnode %s: %s\n", name,
180 fdt_strerror(retval));
181 exit(1);
80ad7816 182 }
ef5d833f 183#endif
80ad7816 184
80ad7816
AG
185 g_free(dupname);
186 return retval;
187}