]>
Commit | Line | Data |
---|---|---|
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 | 28 | void *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 PB |
43 | /* Expand to 2x size to give enough room for manipulation. */ |
44 | dt_size *= 2; | |
f652e6af | 45 | /* First allocate space in qemu for device tree */ |
7267c094 | 46 | fdt = g_malloc0(dt_size); |
f652e6af | 47 | |
7ec632b4 PB |
48 | dt_file_load_size = load_image(filename_path, fdt); |
49 | if (dt_file_load_size < 0) { | |
50 | printf("Unable to open device tree file '%s'\n", | |
51 | filename_path); | |
52 | goto fail; | |
53 | } | |
f652e6af | 54 | |
7ec632b4 | 55 | ret = fdt_open_into(fdt, fdt, dt_size); |
f652e6af AJ |
56 | if (ret) { |
57 | printf("Unable to copy device tree in memory\n"); | |
58 | goto fail; | |
59 | } | |
60 | ||
61 | /* Check sanity of device tree */ | |
62 | if (fdt_check_header(fdt)) { | |
63 | printf ("Device tree file loaded into memory is invalid: %s\n", | |
64 | filename_path); | |
65 | goto fail; | |
66 | } | |
7ec632b4 | 67 | *sizep = dt_size; |
f652e6af AJ |
68 | return fdt; |
69 | ||
70 | fail: | |
7267c094 | 71 | g_free(fdt); |
f652e6af AJ |
72 | return NULL; |
73 | } | |
74 | ||
ccbcfedd | 75 | static int findnode_nofail(void *fdt, const char *node_path) |
f652e6af AJ |
76 | { |
77 | int offset; | |
78 | ||
79 | offset = fdt_path_offset(fdt, node_path); | |
ccbcfedd AG |
80 | if (offset < 0) { |
81 | fprintf(stderr, "%s Couldn't find node %s: %s\n", __func__, node_path, | |
82 | fdt_strerror(offset)); | |
83 | exit(1); | |
84 | } | |
85 | ||
86 | return offset; | |
87 | } | |
88 | ||
89 | int qemu_devtree_setprop(void *fdt, const char *node_path, | |
90 | const char *property, void *val_array, int size) | |
91 | { | |
92 | int r; | |
93 | ||
94 | r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val_array, size); | |
95 | if (r < 0) { | |
96 | fprintf(stderr, "%s: Couldn't set %s/%s: %s\n", __func__, node_path, | |
97 | property, fdt_strerror(r)); | |
98 | exit(1); | |
99 | } | |
f652e6af | 100 | |
ccbcfedd | 101 | return r; |
f652e6af AJ |
102 | } |
103 | ||
104 | int qemu_devtree_setprop_cell(void *fdt, const char *node_path, | |
105 | const char *property, uint32_t val) | |
106 | { | |
ccbcfedd | 107 | int r; |
f652e6af | 108 | |
ccbcfedd AG |
109 | r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val); |
110 | if (r < 0) { | |
111 | fprintf(stderr, "%s: Couldn't set %s/%s = %#08x: %s\n", __func__, | |
112 | node_path, property, val, fdt_strerror(r)); | |
113 | exit(1); | |
114 | } | |
f652e6af | 115 | |
ccbcfedd | 116 | return r; |
f652e6af AJ |
117 | } |
118 | ||
119 | int qemu_devtree_setprop_string(void *fdt, const char *node_path, | |
120 | const char *property, const char *string) | |
121 | { | |
ccbcfedd | 122 | int r; |
f652e6af | 123 | |
ccbcfedd AG |
124 | r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string); |
125 | if (r < 0) { | |
126 | fprintf(stderr, "%s: Couldn't set %s/%s = %s: %s\n", __func__, | |
127 | node_path, property, string, fdt_strerror(r)); | |
128 | exit(1); | |
129 | } | |
f652e6af | 130 | |
ccbcfedd | 131 | return r; |
f652e6af | 132 | } |
d69a8e63 AG |
133 | |
134 | int qemu_devtree_nop_node(void *fdt, const char *node_path) | |
135 | { | |
ccbcfedd | 136 | int r; |
d69a8e63 | 137 | |
ccbcfedd AG |
138 | r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path)); |
139 | if (r < 0) { | |
140 | fprintf(stderr, "%s: Couldn't nop node %s: %s\n", __func__, node_path, | |
141 | fdt_strerror(r)); | |
142 | exit(1); | |
143 | } | |
d69a8e63 | 144 | |
ccbcfedd | 145 | return r; |
d69a8e63 | 146 | } |
80ad7816 AG |
147 | |
148 | int qemu_devtree_add_subnode(void *fdt, const char *name) | |
149 | { | |
80ad7816 AG |
150 | char *dupname = g_strdup(name); |
151 | char *basename = strrchr(dupname, '/'); | |
152 | int retval; | |
153 | ||
154 | if (!basename) { | |
155 | return -1; | |
156 | } | |
157 | ||
158 | basename[0] = '\0'; | |
159 | basename++; | |
160 | ||
ccbcfedd AG |
161 | retval = fdt_add_subnode(fdt, findnode_nofail(fdt, dupname), basename); |
162 | if (retval < 0) { | |
163 | fprintf(stderr, "FDT: Failed to create subnode %s: %s\n", name, | |
164 | fdt_strerror(retval)); | |
165 | exit(1); | |
80ad7816 AG |
166 | } |
167 | ||
80ad7816 AG |
168 | g_free(dupname); |
169 | return retval; | |
170 | } |