2
0

device_tree.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "qemu/error-report.h"
  22. #include "sysemu/device_tree.h"
  23. #include "sysemu/sysemu.h"
  24. #include "hw/loader.h"
  25. #include "qemu/option.h"
  26. #include "qemu/config-file.h"
  27. #include <libfdt.h>
  28. #define FDT_MAX_SIZE 0x10000
  29. void *create_device_tree(int *sizep)
  30. {
  31. void *fdt;
  32. int ret;
  33. *sizep = FDT_MAX_SIZE;
  34. fdt = g_malloc0(FDT_MAX_SIZE);
  35. ret = fdt_create(fdt, FDT_MAX_SIZE);
  36. if (ret < 0) {
  37. goto fail;
  38. }
  39. ret = fdt_finish_reservemap(fdt);
  40. if (ret < 0) {
  41. goto fail;
  42. }
  43. ret = fdt_begin_node(fdt, "");
  44. if (ret < 0) {
  45. goto fail;
  46. }
  47. ret = fdt_end_node(fdt);
  48. if (ret < 0) {
  49. goto fail;
  50. }
  51. ret = fdt_finish(fdt);
  52. if (ret < 0) {
  53. goto fail;
  54. }
  55. ret = fdt_open_into(fdt, fdt, *sizep);
  56. if (ret) {
  57. error_report("Unable to copy device tree in memory");
  58. exit(1);
  59. }
  60. return fdt;
  61. fail:
  62. error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
  63. exit(1);
  64. }
  65. void *load_device_tree(const char *filename_path, int *sizep)
  66. {
  67. int dt_size;
  68. int dt_file_load_size;
  69. int ret;
  70. void *fdt = NULL;
  71. *sizep = 0;
  72. dt_size = get_image_size(filename_path);
  73. if (dt_size < 0) {
  74. error_report("Unable to get size of device tree file '%s'",
  75. filename_path);
  76. goto fail;
  77. }
  78. /* Expand to 2x size to give enough room for manipulation. */
  79. dt_size += 10000;
  80. dt_size *= 2;
  81. /* First allocate space in qemu for device tree */
  82. fdt = g_malloc0(dt_size);
  83. dt_file_load_size = load_image(filename_path, fdt);
  84. if (dt_file_load_size < 0) {
  85. error_report("Unable to open device tree file '%s'",
  86. filename_path);
  87. goto fail;
  88. }
  89. ret = fdt_open_into(fdt, fdt, dt_size);
  90. if (ret) {
  91. error_report("Unable to copy device tree in memory");
  92. goto fail;
  93. }
  94. /* Check sanity of device tree */
  95. if (fdt_check_header(fdt)) {
  96. error_report("Device tree file loaded into memory is invalid: %s",
  97. filename_path);
  98. goto fail;
  99. }
  100. *sizep = dt_size;
  101. return fdt;
  102. fail:
  103. g_free(fdt);
  104. return NULL;
  105. }
  106. static int findnode_nofail(void *fdt, const char *node_path)
  107. {
  108. int offset;
  109. offset = fdt_path_offset(fdt, node_path);
  110. if (offset < 0) {
  111. error_report("%s Couldn't find node %s: %s", __func__, node_path,
  112. fdt_strerror(offset));
  113. exit(1);
  114. }
  115. return offset;
  116. }
  117. int qemu_fdt_setprop(void *fdt, const char *node_path,
  118. const char *property, const void *val, int size)
  119. {
  120. int r;
  121. r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
  122. if (r < 0) {
  123. error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
  124. property, fdt_strerror(r));
  125. exit(1);
  126. }
  127. return r;
  128. }
  129. int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
  130. const char *property, uint32_t val)
  131. {
  132. int r;
  133. r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
  134. if (r < 0) {
  135. error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
  136. node_path, property, val, fdt_strerror(r));
  137. exit(1);
  138. }
  139. return r;
  140. }
  141. int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
  142. const char *property, uint64_t val)
  143. {
  144. val = cpu_to_be64(val);
  145. return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
  146. }
  147. int qemu_fdt_setprop_string(void *fdt, const char *node_path,
  148. const char *property, const char *string)
  149. {
  150. int r;
  151. r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
  152. if (r < 0) {
  153. error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
  154. node_path, property, string, fdt_strerror(r));
  155. exit(1);
  156. }
  157. return r;
  158. }
  159. const void *qemu_fdt_getprop(void *fdt, const char *node_path,
  160. const char *property, int *lenp)
  161. {
  162. int len;
  163. const void *r;
  164. if (!lenp) {
  165. lenp = &len;
  166. }
  167. r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
  168. if (!r) {
  169. error_report("%s: Couldn't get %s/%s: %s", __func__,
  170. node_path, property, fdt_strerror(*lenp));
  171. exit(1);
  172. }
  173. return r;
  174. }
  175. uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
  176. const char *property)
  177. {
  178. int len;
  179. const uint32_t *p = qemu_fdt_getprop(fdt, node_path, property, &len);
  180. if (len != 4) {
  181. error_report("%s: %s/%s not 4 bytes long (not a cell?)",
  182. __func__, node_path, property);
  183. exit(1);
  184. }
  185. return be32_to_cpu(*p);
  186. }
  187. uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
  188. {
  189. uint32_t r;
  190. r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
  191. if (r == 0) {
  192. error_report("%s: Couldn't get phandle for %s: %s", __func__,
  193. path, fdt_strerror(r));
  194. exit(1);
  195. }
  196. return r;
  197. }
  198. int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
  199. const char *property,
  200. const char *target_node_path)
  201. {
  202. uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
  203. return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
  204. }
  205. uint32_t qemu_fdt_alloc_phandle(void *fdt)
  206. {
  207. static int phandle = 0x0;
  208. /*
  209. * We need to find out if the user gave us special instruction at
  210. * which phandle id to start allocting phandles.
  211. */
  212. if (!phandle) {
  213. phandle = qemu_opt_get_number(qemu_get_machine_opts(),
  214. "phandle_start", 0);
  215. }
  216. if (!phandle) {
  217. /*
  218. * None or invalid phandle given on the command line, so fall back to
  219. * default starting point.
  220. */
  221. phandle = 0x8000;
  222. }
  223. return phandle++;
  224. }
  225. int qemu_fdt_nop_node(void *fdt, const char *node_path)
  226. {
  227. int r;
  228. r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
  229. if (r < 0) {
  230. error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
  231. fdt_strerror(r));
  232. exit(1);
  233. }
  234. return r;
  235. }
  236. int qemu_fdt_add_subnode(void *fdt, const char *name)
  237. {
  238. char *dupname = g_strdup(name);
  239. char *basename = strrchr(dupname, '/');
  240. int retval;
  241. int parent = 0;
  242. if (!basename) {
  243. g_free(dupname);
  244. return -1;
  245. }
  246. basename[0] = '\0';
  247. basename++;
  248. if (dupname[0]) {
  249. parent = findnode_nofail(fdt, dupname);
  250. }
  251. retval = fdt_add_subnode(fdt, parent, basename);
  252. if (retval < 0) {
  253. error_report("FDT: Failed to create subnode %s: %s", name,
  254. fdt_strerror(retval));
  255. exit(1);
  256. }
  257. g_free(dupname);
  258. return retval;
  259. }
  260. void qemu_fdt_dumpdtb(void *fdt, int size)
  261. {
  262. const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
  263. if (dumpdtb) {
  264. /* Dump the dtb to a file and quit */
  265. exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
  266. }
  267. }
  268. int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
  269. const char *node_path,
  270. const char *property,
  271. int numvalues,
  272. uint64_t *values)
  273. {
  274. uint32_t *propcells;
  275. uint64_t value;
  276. int cellnum, vnum, ncells;
  277. uint32_t hival;
  278. propcells = g_new0(uint32_t, numvalues * 2);
  279. cellnum = 0;
  280. for (vnum = 0; vnum < numvalues; vnum++) {
  281. ncells = values[vnum * 2];
  282. if (ncells != 1 && ncells != 2) {
  283. return -1;
  284. }
  285. value = values[vnum * 2 + 1];
  286. hival = cpu_to_be32(value >> 32);
  287. if (ncells > 1) {
  288. propcells[cellnum++] = hival;
  289. } else if (hival != 0) {
  290. return -1;
  291. }
  292. propcells[cellnum++] = cpu_to_be32(value);
  293. }
  294. return qemu_fdt_setprop(fdt, node_path, property, propcells,
  295. cellnum * sizeof(uint32_t));
  296. }