root / device_tree.c @ 6f06f178
History | View | Annotate | Download (4.1 kB)
1 | f652e6af | aurel32 | /*
|
---|---|---|---|
2 | f652e6af | aurel32 | * Functions to help device tree manipulation using libfdt.
|
3 | f652e6af | aurel32 | * It also provides functions to read entries from device tree proc
|
4 | f652e6af | aurel32 | * interface.
|
5 | f652e6af | aurel32 | *
|
6 | f652e6af | aurel32 | * Copyright 2008 IBM Corporation.
|
7 | f652e6af | aurel32 | * Authors: Jerone Young <jyoung5@us.ibm.com>
|
8 | f652e6af | aurel32 | * Hollis Blanchard <hollisb@us.ibm.com>
|
9 | f652e6af | aurel32 | *
|
10 | f652e6af | aurel32 | * This work is licensed under the GNU GPL license version 2 or later.
|
11 | f652e6af | aurel32 | *
|
12 | f652e6af | aurel32 | */
|
13 | f652e6af | aurel32 | |
14 | f652e6af | aurel32 | #include <stdio.h> |
15 | f652e6af | aurel32 | #include <sys/types.h> |
16 | f652e6af | aurel32 | #include <sys/stat.h> |
17 | f652e6af | aurel32 | #include <fcntl.h> |
18 | f652e6af | aurel32 | #include <unistd.h> |
19 | f652e6af | aurel32 | #include <stdlib.h> |
20 | f652e6af | aurel32 | |
21 | f652e6af | aurel32 | #include "config.h" |
22 | f652e6af | aurel32 | #include "qemu-common.h" |
23 | f652e6af | aurel32 | #include "device_tree.h" |
24 | 39b7f20e | Blue Swirl | #include "hw/loader.h" |
25 | f652e6af | aurel32 | |
26 | f652e6af | aurel32 | #include <libfdt.h> |
27 | f652e6af | aurel32 | |
28 | 7ec632b4 | pbrook | void *load_device_tree(const char *filename_path, int *sizep) |
29 | f652e6af | aurel32 | { |
30 | 7ec632b4 | pbrook | int dt_size;
|
31 | f652e6af | aurel32 | int dt_file_load_size;
|
32 | f652e6af | aurel32 | int ret;
|
33 | 7ec632b4 | pbrook | void *fdt = NULL; |
34 | f652e6af | aurel32 | |
35 | 7ec632b4 | pbrook | *sizep = 0;
|
36 | 7ec632b4 | pbrook | dt_size = get_image_size(filename_path); |
37 | 7ec632b4 | pbrook | if (dt_size < 0) { |
38 | f652e6af | aurel32 | printf("Unable to get size of device tree file '%s'\n",
|
39 | f652e6af | aurel32 | filename_path); |
40 | f652e6af | aurel32 | goto fail;
|
41 | f652e6af | aurel32 | } |
42 | f652e6af | aurel32 | |
43 | 7ec632b4 | pbrook | /* Expand to 2x size to give enough room for manipulation. */
|
44 | ded57c5f | Alexander Graf | dt_size += 10000;
|
45 | 7ec632b4 | pbrook | dt_size *= 2;
|
46 | f652e6af | aurel32 | /* First allocate space in qemu for device tree */
|
47 | 7267c094 | Anthony Liguori | fdt = g_malloc0(dt_size); |
48 | f652e6af | aurel32 | |
49 | 7ec632b4 | pbrook | dt_file_load_size = load_image(filename_path, fdt); |
50 | 7ec632b4 | pbrook | if (dt_file_load_size < 0) { |
51 | 7ec632b4 | pbrook | printf("Unable to open device tree file '%s'\n",
|
52 | 7ec632b4 | pbrook | filename_path); |
53 | 7ec632b4 | pbrook | goto fail;
|
54 | 7ec632b4 | pbrook | } |
55 | f652e6af | aurel32 | |
56 | 7ec632b4 | pbrook | ret = fdt_open_into(fdt, fdt, dt_size); |
57 | f652e6af | aurel32 | if (ret) {
|
58 | f652e6af | aurel32 | printf("Unable to copy device tree in memory\n");
|
59 | f652e6af | aurel32 | goto fail;
|
60 | f652e6af | aurel32 | } |
61 | f652e6af | aurel32 | |
62 | f652e6af | aurel32 | /* Check sanity of device tree */
|
63 | f652e6af | aurel32 | if (fdt_check_header(fdt)) {
|
64 | f652e6af | aurel32 | printf ("Device tree file loaded into memory is invalid: %s\n",
|
65 | f652e6af | aurel32 | filename_path); |
66 | f652e6af | aurel32 | goto fail;
|
67 | f652e6af | aurel32 | } |
68 | 7ec632b4 | pbrook | *sizep = dt_size; |
69 | f652e6af | aurel32 | return fdt;
|
70 | f652e6af | aurel32 | |
71 | f652e6af | aurel32 | fail:
|
72 | 7267c094 | Anthony Liguori | g_free(fdt); |
73 | f652e6af | aurel32 | return NULL; |
74 | f652e6af | aurel32 | } |
75 | f652e6af | aurel32 | |
76 | ccbcfedd | Alexander Graf | static int findnode_nofail(void *fdt, const char *node_path) |
77 | f652e6af | aurel32 | { |
78 | f652e6af | aurel32 | int offset;
|
79 | f652e6af | aurel32 | |
80 | f652e6af | aurel32 | offset = fdt_path_offset(fdt, node_path); |
81 | ccbcfedd | Alexander Graf | if (offset < 0) { |
82 | ccbcfedd | Alexander Graf | fprintf(stderr, "%s Couldn't find node %s: %s\n", __func__, node_path,
|
83 | ccbcfedd | Alexander Graf | fdt_strerror(offset)); |
84 | ccbcfedd | Alexander Graf | exit(1);
|
85 | ccbcfedd | Alexander Graf | } |
86 | ccbcfedd | Alexander Graf | |
87 | ccbcfedd | Alexander Graf | return offset;
|
88 | ccbcfedd | Alexander Graf | } |
89 | ccbcfedd | Alexander Graf | |
90 | ccbcfedd | Alexander Graf | int qemu_devtree_setprop(void *fdt, const char *node_path, |
91 | ccbcfedd | Alexander Graf | const char *property, void *val_array, int size) |
92 | ccbcfedd | Alexander Graf | { |
93 | ccbcfedd | Alexander Graf | int r;
|
94 | ccbcfedd | Alexander Graf | |
95 | ccbcfedd | Alexander Graf | r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val_array, size); |
96 | ccbcfedd | Alexander Graf | if (r < 0) { |
97 | ccbcfedd | Alexander Graf | fprintf(stderr, "%s: Couldn't set %s/%s: %s\n", __func__, node_path,
|
98 | ccbcfedd | Alexander Graf | property, fdt_strerror(r)); |
99 | ccbcfedd | Alexander Graf | exit(1);
|
100 | ccbcfedd | Alexander Graf | } |
101 | f652e6af | aurel32 | |
102 | ccbcfedd | Alexander Graf | return r;
|
103 | f652e6af | aurel32 | } |
104 | f652e6af | aurel32 | |
105 | f652e6af | aurel32 | int qemu_devtree_setprop_cell(void *fdt, const char *node_path, |
106 | f652e6af | aurel32 | const char *property, uint32_t val) |
107 | f652e6af | aurel32 | { |
108 | ccbcfedd | Alexander Graf | int r;
|
109 | f652e6af | aurel32 | |
110 | ccbcfedd | Alexander Graf | r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val); |
111 | ccbcfedd | Alexander Graf | if (r < 0) { |
112 | ccbcfedd | Alexander Graf | fprintf(stderr, "%s: Couldn't set %s/%s = %#08x: %s\n", __func__,
|
113 | ccbcfedd | Alexander Graf | node_path, property, val, fdt_strerror(r)); |
114 | ccbcfedd | Alexander Graf | exit(1);
|
115 | ccbcfedd | Alexander Graf | } |
116 | f652e6af | aurel32 | |
117 | ccbcfedd | Alexander Graf | return r;
|
118 | f652e6af | aurel32 | } |
119 | f652e6af | aurel32 | |
120 | f652e6af | aurel32 | int qemu_devtree_setprop_string(void *fdt, const char *node_path, |
121 | f652e6af | aurel32 | const char *property, const char *string) |
122 | f652e6af | aurel32 | { |
123 | ccbcfedd | Alexander Graf | int r;
|
124 | f652e6af | aurel32 | |
125 | ccbcfedd | Alexander Graf | r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string); |
126 | ccbcfedd | Alexander Graf | if (r < 0) { |
127 | ccbcfedd | Alexander Graf | fprintf(stderr, "%s: Couldn't set %s/%s = %s: %s\n", __func__,
|
128 | ccbcfedd | Alexander Graf | node_path, property, string, fdt_strerror(r)); |
129 | ccbcfedd | Alexander Graf | exit(1);
|
130 | ccbcfedd | Alexander Graf | } |
131 | f652e6af | aurel32 | |
132 | ccbcfedd | Alexander Graf | return r;
|
133 | f652e6af | aurel32 | } |
134 | d69a8e63 | Alexander Graf | |
135 | d69a8e63 | Alexander Graf | int qemu_devtree_nop_node(void *fdt, const char *node_path) |
136 | d69a8e63 | Alexander Graf | { |
137 | ccbcfedd | Alexander Graf | int r;
|
138 | d69a8e63 | Alexander Graf | |
139 | ccbcfedd | Alexander Graf | r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path)); |
140 | ccbcfedd | Alexander Graf | if (r < 0) { |
141 | ccbcfedd | Alexander Graf | fprintf(stderr, "%s: Couldn't nop node %s: %s\n", __func__, node_path,
|
142 | ccbcfedd | Alexander Graf | fdt_strerror(r)); |
143 | ccbcfedd | Alexander Graf | exit(1);
|
144 | ccbcfedd | Alexander Graf | } |
145 | d69a8e63 | Alexander Graf | |
146 | ccbcfedd | Alexander Graf | return r;
|
147 | d69a8e63 | Alexander Graf | } |
148 | 80ad7816 | Alexander Graf | |
149 | 80ad7816 | Alexander Graf | int qemu_devtree_add_subnode(void *fdt, const char *name) |
150 | 80ad7816 | Alexander Graf | { |
151 | 80ad7816 | Alexander Graf | char *dupname = g_strdup(name);
|
152 | 80ad7816 | Alexander Graf | char *basename = strrchr(dupname, '/'); |
153 | 80ad7816 | Alexander Graf | int retval;
|
154 | 80ad7816 | Alexander Graf | |
155 | 80ad7816 | Alexander Graf | if (!basename) {
|
156 | bff39b63 | Stefan Weil | g_free(dupname); |
157 | 80ad7816 | Alexander Graf | return -1; |
158 | 80ad7816 | Alexander Graf | } |
159 | 80ad7816 | Alexander Graf | |
160 | 80ad7816 | Alexander Graf | basename[0] = '\0'; |
161 | 80ad7816 | Alexander Graf | basename++; |
162 | 80ad7816 | Alexander Graf | |
163 | ccbcfedd | Alexander Graf | retval = fdt_add_subnode(fdt, findnode_nofail(fdt, dupname), basename); |
164 | ccbcfedd | Alexander Graf | if (retval < 0) { |
165 | ccbcfedd | Alexander Graf | fprintf(stderr, "FDT: Failed to create subnode %s: %s\n", name,
|
166 | ccbcfedd | Alexander Graf | fdt_strerror(retval)); |
167 | ccbcfedd | Alexander Graf | exit(1);
|
168 | 80ad7816 | Alexander Graf | } |
169 | 80ad7816 | Alexander Graf | |
170 | 80ad7816 | Alexander Graf | g_free(dupname); |
171 | 80ad7816 | Alexander Graf | return retval;
|
172 | 80ad7816 | Alexander Graf | } |