2
0

device_tree.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include "config.h"
  20. #include "qemu-common.h"
  21. #include "device_tree.h"
  22. #include "hw/loader.h"
  23. #include "qemu-option.h"
  24. #include "qemu-config.h"
  25. #include <libfdt.h>
  26. #define FDT_MAX_SIZE 0x10000
  27. void *create_device_tree(int *sizep)
  28. {
  29. void *fdt;
  30. int ret;
  31. *sizep = FDT_MAX_SIZE;
  32. fdt = g_malloc0(FDT_MAX_SIZE);
  33. ret = fdt_create(fdt, FDT_MAX_SIZE);
  34. if (ret < 0) {
  35. goto fail;
  36. }
  37. ret = fdt_begin_node(fdt, "");
  38. if (ret < 0) {
  39. goto fail;
  40. }
  41. ret = fdt_end_node(fdt);
  42. if (ret < 0) {
  43. goto fail;
  44. }
  45. ret = fdt_finish(fdt);
  46. if (ret < 0) {
  47. goto fail;
  48. }
  49. ret = fdt_open_into(fdt, fdt, *sizep);
  50. if (ret) {
  51. fprintf(stderr, "Unable to copy device tree in memory\n");
  52. exit(1);
  53. }
  54. return fdt;
  55. fail:
  56. fprintf(stderr, "%s Couldn't create dt: %s\n", __func__, fdt_strerror(ret));
  57. exit(1);
  58. }
  59. void *load_device_tree(const char *filename_path, int *sizep)
  60. {
  61. int dt_size;
  62. int dt_file_load_size;
  63. int ret;
  64. void *fdt = NULL;
  65. *sizep = 0;
  66. dt_size = get_image_size(filename_path);
  67. if (dt_size < 0) {
  68. printf("Unable to get size of device tree file '%s'\n",
  69. filename_path);
  70. goto fail;
  71. }
  72. /* Expand to 2x size to give enough room for manipulation. */
  73. dt_size += 10000;
  74. dt_size *= 2;
  75. /* First allocate space in qemu for device tree */
  76. fdt = g_malloc0(dt_size);
  77. dt_file_load_size = load_image(filename_path, fdt);
  78. if (dt_file_load_size < 0) {
  79. printf("Unable to open device tree file '%s'\n",
  80. filename_path);
  81. goto fail;
  82. }
  83. ret = fdt_open_into(fdt, fdt, dt_size);
  84. if (ret) {
  85. printf("Unable to copy device tree in memory\n");
  86. goto fail;
  87. }
  88. /* Check sanity of device tree */
  89. if (fdt_check_header(fdt)) {
  90. printf ("Device tree file loaded into memory is invalid: %s\n",
  91. filename_path);
  92. goto fail;
  93. }
  94. *sizep = dt_size;
  95. return fdt;
  96. fail:
  97. g_free(fdt);
  98. return NULL;
  99. }
  100. static int findnode_nofail(void *fdt, const char *node_path)
  101. {
  102. int offset;
  103. offset = fdt_path_offset(fdt, node_path);
  104. if (offset < 0) {
  105. fprintf(stderr, "%s Couldn't find node %s: %s\n", __func__, node_path,
  106. fdt_strerror(offset));
  107. exit(1);
  108. }
  109. return offset;
  110. }
  111. int qemu_devtree_setprop(void *fdt, const char *node_path,
  112. const char *property, const void *val_array, int size)
  113. {
  114. int r;
  115. r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val_array, size);
  116. if (r < 0) {
  117. fprintf(stderr, "%s: Couldn't set %s/%s: %s\n", __func__, node_path,
  118. property, fdt_strerror(r));
  119. exit(1);
  120. }
  121. return r;
  122. }
  123. int qemu_devtree_setprop_cell(void *fdt, const char *node_path,
  124. const char *property, uint32_t val)
  125. {
  126. int r;
  127. r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
  128. if (r < 0) {
  129. fprintf(stderr, "%s: Couldn't set %s/%s = %#08x: %s\n", __func__,
  130. node_path, property, val, fdt_strerror(r));
  131. exit(1);
  132. }
  133. return r;
  134. }
  135. int qemu_devtree_setprop_u64(void *fdt, const char *node_path,
  136. const char *property, uint64_t val)
  137. {
  138. val = cpu_to_be64(val);
  139. return qemu_devtree_setprop(fdt, node_path, property, &val, sizeof(val));
  140. }
  141. int qemu_devtree_setprop_string(void *fdt, const char *node_path,
  142. const char *property, const char *string)
  143. {
  144. int r;
  145. r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
  146. if (r < 0) {
  147. fprintf(stderr, "%s: Couldn't set %s/%s = %s: %s\n", __func__,
  148. node_path, property, string, fdt_strerror(r));
  149. exit(1);
  150. }
  151. return r;
  152. }
  153. const void *qemu_devtree_getprop(void *fdt, const char *node_path,
  154. const char *property, int *lenp)
  155. {
  156. int len;
  157. const void *r;
  158. if (!lenp) {
  159. lenp = &len;
  160. }
  161. r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
  162. if (!r) {
  163. fprintf(stderr, "%s: Couldn't get %s/%s: %s\n", __func__,
  164. node_path, property, fdt_strerror(*lenp));
  165. exit(1);
  166. }
  167. return r;
  168. }
  169. uint32_t qemu_devtree_getprop_cell(void *fdt, const char *node_path,
  170. const char *property)
  171. {
  172. int len;
  173. const uint32_t *p = qemu_devtree_getprop(fdt, node_path, property, &len);
  174. if (len != 4) {
  175. fprintf(stderr, "%s: %s/%s not 4 bytes long (not a cell?)\n",
  176. __func__, node_path, property);
  177. exit(1);
  178. }
  179. return be32_to_cpu(*p);
  180. }
  181. uint32_t qemu_devtree_get_phandle(void *fdt, const char *path)
  182. {
  183. uint32_t r;
  184. r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
  185. if (r <= 0) {
  186. fprintf(stderr, "%s: Couldn't get phandle for %s: %s\n", __func__,
  187. path, fdt_strerror(r));
  188. exit(1);
  189. }
  190. return r;
  191. }
  192. int qemu_devtree_setprop_phandle(void *fdt, const char *node_path,
  193. const char *property,
  194. const char *target_node_path)
  195. {
  196. uint32_t phandle = qemu_devtree_get_phandle(fdt, target_node_path);
  197. return qemu_devtree_setprop_cell(fdt, node_path, property, phandle);
  198. }
  199. uint32_t qemu_devtree_alloc_phandle(void *fdt)
  200. {
  201. static int phandle = 0x0;
  202. /*
  203. * We need to find out if the user gave us special instruction at
  204. * which phandle id to start allocting phandles.
  205. */
  206. if (!phandle) {
  207. QemuOpts *machine_opts;
  208. machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
  209. if (machine_opts) {
  210. const char *phandle_start;
  211. phandle_start = qemu_opt_get(machine_opts, "phandle_start");
  212. if (phandle_start) {
  213. phandle = strtoul(phandle_start, NULL, 0);
  214. }
  215. }
  216. }
  217. if (!phandle) {
  218. /*
  219. * None or invalid phandle given on the command line, so fall back to
  220. * default starting point.
  221. */
  222. phandle = 0x8000;
  223. }
  224. return phandle++;
  225. }
  226. int qemu_devtree_nop_node(void *fdt, const char *node_path)
  227. {
  228. int r;
  229. r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
  230. if (r < 0) {
  231. fprintf(stderr, "%s: Couldn't nop node %s: %s\n", __func__, node_path,
  232. fdt_strerror(r));
  233. exit(1);
  234. }
  235. return r;
  236. }
  237. int qemu_devtree_add_subnode(void *fdt, const char *name)
  238. {
  239. char *dupname = g_strdup(name);
  240. char *basename = strrchr(dupname, '/');
  241. int retval;
  242. int parent = 0;
  243. if (!basename) {
  244. g_free(dupname);
  245. return -1;
  246. }
  247. basename[0] = '\0';
  248. basename++;
  249. if (dupname[0]) {
  250. parent = findnode_nofail(fdt, dupname);
  251. }
  252. retval = fdt_add_subnode(fdt, parent, basename);
  253. if (retval < 0) {
  254. fprintf(stderr, "FDT: Failed to create subnode %s: %s\n", name,
  255. fdt_strerror(retval));
  256. exit(1);
  257. }
  258. g_free(dupname);
  259. return retval;
  260. }