You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

device_tree.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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 "qemu/osdep.h"
  14. #ifdef CONFIG_LINUX
  15. #include <dirent.h>
  16. #endif
  17. #include "qapi/error.h"
  18. #include "qemu/error-report.h"
  19. #include "qemu/option.h"
  20. #include "qemu/bswap.h"
  21. #include "sysemu/device_tree.h"
  22. #include "sysemu/sysemu.h"
  23. #include "hw/loader.h"
  24. #include "hw/boards.h"
  25. #include "qemu/config-file.h"
  26. #include <libfdt.h>
  27. #define FDT_MAX_SIZE 0x100000
  28. void *create_device_tree(int *sizep)
  29. {
  30. void *fdt;
  31. int ret;
  32. *sizep = FDT_MAX_SIZE;
  33. fdt = g_malloc0(FDT_MAX_SIZE);
  34. ret = fdt_create(fdt, FDT_MAX_SIZE);
  35. if (ret < 0) {
  36. goto fail;
  37. }
  38. ret = fdt_finish_reservemap(fdt);
  39. if (ret < 0) {
  40. goto fail;
  41. }
  42. ret = fdt_begin_node(fdt, "");
  43. if (ret < 0) {
  44. goto fail;
  45. }
  46. ret = fdt_end_node(fdt);
  47. if (ret < 0) {
  48. goto fail;
  49. }
  50. ret = fdt_finish(fdt);
  51. if (ret < 0) {
  52. goto fail;
  53. }
  54. ret = fdt_open_into(fdt, fdt, *sizep);
  55. if (ret) {
  56. error_report("Unable to copy device tree in memory");
  57. exit(1);
  58. }
  59. return fdt;
  60. fail:
  61. error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
  62. exit(1);
  63. }
  64. void *load_device_tree(const char *filename_path, int *sizep)
  65. {
  66. int dt_size;
  67. int dt_file_load_size;
  68. int ret;
  69. void *fdt = NULL;
  70. *sizep = 0;
  71. dt_size = get_image_size(filename_path);
  72. if (dt_size < 0) {
  73. error_report("Unable to get size of device tree file '%s'",
  74. filename_path);
  75. goto fail;
  76. }
  77. if (dt_size > INT_MAX / 2 - 10000) {
  78. error_report("Device tree file '%s' is too large", filename_path);
  79. goto fail;
  80. }
  81. /* Expand to 2x size to give enough room for manipulation. */
  82. dt_size += 10000;
  83. dt_size *= 2;
  84. /* First allocate space in qemu for device tree */
  85. fdt = g_malloc0(dt_size);
  86. dt_file_load_size = load_image_size(filename_path, fdt, dt_size);
  87. if (dt_file_load_size < 0) {
  88. error_report("Unable to open device tree file '%s'",
  89. filename_path);
  90. goto fail;
  91. }
  92. ret = fdt_open_into(fdt, fdt, dt_size);
  93. if (ret) {
  94. error_report("Unable to copy device tree in memory");
  95. goto fail;
  96. }
  97. /* Check sanity of device tree */
  98. if (fdt_check_header(fdt)) {
  99. error_report("Device tree file loaded into memory is invalid: %s",
  100. filename_path);
  101. goto fail;
  102. }
  103. *sizep = dt_size;
  104. return fdt;
  105. fail:
  106. g_free(fdt);
  107. return NULL;
  108. }
  109. #ifdef CONFIG_LINUX
  110. #define SYSFS_DT_BASEDIR "/proc/device-tree"
  111. /**
  112. * read_fstree: this function is inspired from dtc read_fstree
  113. * @fdt: preallocated fdt blob buffer, to be populated
  114. * @dirname: directory to scan under SYSFS_DT_BASEDIR
  115. * the search is recursive and the tree is searched down to the
  116. * leaves (property files).
  117. *
  118. * the function asserts in case of error
  119. */
  120. static void read_fstree(void *fdt, const char *dirname)
  121. {
  122. DIR *d;
  123. struct dirent *de;
  124. struct stat st;
  125. const char *root_dir = SYSFS_DT_BASEDIR;
  126. const char *parent_node;
  127. if (strstr(dirname, root_dir) != dirname) {
  128. error_report("%s: %s must be searched within %s",
  129. __func__, dirname, root_dir);
  130. exit(1);
  131. }
  132. parent_node = &dirname[strlen(SYSFS_DT_BASEDIR)];
  133. d = opendir(dirname);
  134. if (!d) {
  135. error_report("%s cannot open %s", __func__, dirname);
  136. exit(1);
  137. }
  138. while ((de = readdir(d)) != NULL) {
  139. char *tmpnam;
  140. if (!g_strcmp0(de->d_name, ".")
  141. || !g_strcmp0(de->d_name, "..")) {
  142. continue;
  143. }
  144. tmpnam = g_strdup_printf("%s/%s", dirname, de->d_name);
  145. if (lstat(tmpnam, &st) < 0) {
  146. error_report("%s cannot lstat %s", __func__, tmpnam);
  147. exit(1);
  148. }
  149. if (S_ISREG(st.st_mode)) {
  150. gchar *val;
  151. gsize len;
  152. if (!g_file_get_contents(tmpnam, &val, &len, NULL)) {
  153. error_report("%s not able to extract info from %s",
  154. __func__, tmpnam);
  155. exit(1);
  156. }
  157. if (strlen(parent_node) > 0) {
  158. qemu_fdt_setprop(fdt, parent_node,
  159. de->d_name, val, len);
  160. } else {
  161. qemu_fdt_setprop(fdt, "/", de->d_name, val, len);
  162. }
  163. g_free(val);
  164. } else if (S_ISDIR(st.st_mode)) {
  165. char *node_name;
  166. node_name = g_strdup_printf("%s/%s",
  167. parent_node, de->d_name);
  168. qemu_fdt_add_subnode(fdt, node_name);
  169. g_free(node_name);
  170. read_fstree(fdt, tmpnam);
  171. }
  172. g_free(tmpnam);
  173. }
  174. closedir(d);
  175. }
  176. /* load_device_tree_from_sysfs: extract the dt blob from host sysfs */
  177. void *load_device_tree_from_sysfs(void)
  178. {
  179. void *host_fdt;
  180. int host_fdt_size;
  181. host_fdt = create_device_tree(&host_fdt_size);
  182. read_fstree(host_fdt, SYSFS_DT_BASEDIR);
  183. if (fdt_check_header(host_fdt)) {
  184. error_report("%s host device tree extracted into memory is invalid",
  185. __func__);
  186. exit(1);
  187. }
  188. return host_fdt;
  189. }
  190. #endif /* CONFIG_LINUX */
  191. static int findnode_nofail(void *fdt, const char *node_path)
  192. {
  193. int offset;
  194. offset = fdt_path_offset(fdt, node_path);
  195. if (offset < 0) {
  196. error_report("%s Couldn't find node %s: %s", __func__, node_path,
  197. fdt_strerror(offset));
  198. exit(1);
  199. }
  200. return offset;
  201. }
  202. char **qemu_fdt_node_unit_path(void *fdt, const char *name, Error **errp)
  203. {
  204. char *prefix = g_strdup_printf("%s@", name);
  205. unsigned int path_len = 16, n = 0;
  206. GSList *path_list = NULL, *iter;
  207. const char *iter_name;
  208. int offset, len, ret;
  209. char **path_array;
  210. offset = fdt_next_node(fdt, -1, NULL);
  211. while (offset >= 0) {
  212. iter_name = fdt_get_name(fdt, offset, &len);
  213. if (!iter_name) {
  214. offset = len;
  215. break;
  216. }
  217. if (!strcmp(iter_name, name) || g_str_has_prefix(iter_name, prefix)) {
  218. char *path;
  219. path = g_malloc(path_len);
  220. while ((ret = fdt_get_path(fdt, offset, path, path_len))
  221. == -FDT_ERR_NOSPACE) {
  222. path_len += 16;
  223. path = g_realloc(path, path_len);
  224. }
  225. path_list = g_slist_prepend(path_list, path);
  226. n++;
  227. }
  228. offset = fdt_next_node(fdt, offset, NULL);
  229. }
  230. g_free(prefix);
  231. if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
  232. error_setg(errp, "%s: abort parsing dt for %s node units: %s",
  233. __func__, name, fdt_strerror(offset));
  234. for (iter = path_list; iter; iter = iter->next) {
  235. g_free(iter->data);
  236. }
  237. g_slist_free(path_list);
  238. return NULL;
  239. }
  240. path_array = g_new(char *, n + 1);
  241. path_array[n--] = NULL;
  242. for (iter = path_list; iter; iter = iter->next) {
  243. path_array[n--] = iter->data;
  244. }
  245. g_slist_free(path_list);
  246. return path_array;
  247. }
  248. char **qemu_fdt_node_path(void *fdt, const char *name, char *compat,
  249. Error **errp)
  250. {
  251. int offset, len, ret;
  252. const char *iter_name;
  253. unsigned int path_len = 16, n = 0;
  254. GSList *path_list = NULL, *iter;
  255. char **path_array;
  256. offset = fdt_node_offset_by_compatible(fdt, -1, compat);
  257. while (offset >= 0) {
  258. iter_name = fdt_get_name(fdt, offset, &len);
  259. if (!iter_name) {
  260. offset = len;
  261. break;
  262. }
  263. if (!strcmp(iter_name, name)) {
  264. char *path;
  265. path = g_malloc(path_len);
  266. while ((ret = fdt_get_path(fdt, offset, path, path_len))
  267. == -FDT_ERR_NOSPACE) {
  268. path_len += 16;
  269. path = g_realloc(path, path_len);
  270. }
  271. path_list = g_slist_prepend(path_list, path);
  272. n++;
  273. }
  274. offset = fdt_node_offset_by_compatible(fdt, offset, compat);
  275. }
  276. if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
  277. error_setg(errp, "%s: abort parsing dt for %s/%s: %s",
  278. __func__, name, compat, fdt_strerror(offset));
  279. for (iter = path_list; iter; iter = iter->next) {
  280. g_free(iter->data);
  281. }
  282. g_slist_free(path_list);
  283. return NULL;
  284. }
  285. path_array = g_new(char *, n + 1);
  286. path_array[n--] = NULL;
  287. for (iter = path_list; iter; iter = iter->next) {
  288. path_array[n--] = iter->data;
  289. }
  290. g_slist_free(path_list);
  291. return path_array;
  292. }
  293. int qemu_fdt_setprop(void *fdt, const char *node_path,
  294. const char *property, const void *val, int size)
  295. {
  296. int r;
  297. r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
  298. if (r < 0) {
  299. error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
  300. property, fdt_strerror(r));
  301. exit(1);
  302. }
  303. return r;
  304. }
  305. int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
  306. const char *property, uint32_t val)
  307. {
  308. int r;
  309. r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
  310. if (r < 0) {
  311. error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
  312. node_path, property, val, fdt_strerror(r));
  313. exit(1);
  314. }
  315. return r;
  316. }
  317. int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
  318. const char *property, uint64_t val)
  319. {
  320. val = cpu_to_be64(val);
  321. return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
  322. }
  323. int qemu_fdt_setprop_string(void *fdt, const char *node_path,
  324. const char *property, const char *string)
  325. {
  326. int r;
  327. r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
  328. if (r < 0) {
  329. error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
  330. node_path, property, string, fdt_strerror(r));
  331. exit(1);
  332. }
  333. return r;
  334. }
  335. const void *qemu_fdt_getprop(void *fdt, const char *node_path,
  336. const char *property, int *lenp, Error **errp)
  337. {
  338. int len;
  339. const void *r;
  340. if (!lenp) {
  341. lenp = &len;
  342. }
  343. r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
  344. if (!r) {
  345. error_setg(errp, "%s: Couldn't get %s/%s: %s", __func__,
  346. node_path, property, fdt_strerror(*lenp));
  347. }
  348. return r;
  349. }
  350. uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
  351. const char *property, int *lenp, Error **errp)
  352. {
  353. int len;
  354. const uint32_t *p;
  355. if (!lenp) {
  356. lenp = &len;
  357. }
  358. p = qemu_fdt_getprop(fdt, node_path, property, lenp, errp);
  359. if (!p) {
  360. return 0;
  361. } else if (*lenp != 4) {
  362. error_setg(errp, "%s: %s/%s not 4 bytes long (not a cell?)",
  363. __func__, node_path, property);
  364. *lenp = -EINVAL;
  365. return 0;
  366. }
  367. return be32_to_cpu(*p);
  368. }
  369. uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
  370. {
  371. uint32_t r;
  372. r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
  373. if (r == 0) {
  374. error_report("%s: Couldn't get phandle for %s: %s", __func__,
  375. path, fdt_strerror(r));
  376. exit(1);
  377. }
  378. return r;
  379. }
  380. int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
  381. const char *property,
  382. const char *target_node_path)
  383. {
  384. uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
  385. return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
  386. }
  387. uint32_t qemu_fdt_alloc_phandle(void *fdt)
  388. {
  389. static int phandle = 0x0;
  390. /*
  391. * We need to find out if the user gave us special instruction at
  392. * which phandle id to start allocating phandles.
  393. */
  394. if (!phandle) {
  395. phandle = machine_phandle_start(current_machine);
  396. }
  397. if (!phandle) {
  398. /*
  399. * None or invalid phandle given on the command line, so fall back to
  400. * default starting point.
  401. */
  402. phandle = 0x8000;
  403. }
  404. return phandle++;
  405. }
  406. int qemu_fdt_nop_node(void *fdt, const char *node_path)
  407. {
  408. int r;
  409. r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
  410. if (r < 0) {
  411. error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
  412. fdt_strerror(r));
  413. exit(1);
  414. }
  415. return r;
  416. }
  417. int qemu_fdt_add_subnode(void *fdt, const char *name)
  418. {
  419. char *dupname = g_strdup(name);
  420. char *basename = strrchr(dupname, '/');
  421. int retval;
  422. int parent = 0;
  423. if (!basename) {
  424. g_free(dupname);
  425. return -1;
  426. }
  427. basename[0] = '\0';
  428. basename++;
  429. if (dupname[0]) {
  430. parent = findnode_nofail(fdt, dupname);
  431. }
  432. retval = fdt_add_subnode(fdt, parent, basename);
  433. if (retval < 0) {
  434. error_report("FDT: Failed to create subnode %s: %s", name,
  435. fdt_strerror(retval));
  436. exit(1);
  437. }
  438. g_free(dupname);
  439. return retval;
  440. }
  441. void qemu_fdt_dumpdtb(void *fdt, int size)
  442. {
  443. const char *dumpdtb = qemu_opt_get(qemu_get_machine_opts(), "dumpdtb");
  444. if (dumpdtb) {
  445. /* Dump the dtb to a file and quit */
  446. exit(g_file_set_contents(dumpdtb, fdt, size, NULL) ? 0 : 1);
  447. }
  448. }
  449. int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
  450. const char *node_path,
  451. const char *property,
  452. int numvalues,
  453. uint64_t *values)
  454. {
  455. uint32_t *propcells;
  456. uint64_t value;
  457. int cellnum, vnum, ncells;
  458. uint32_t hival;
  459. int ret;
  460. propcells = g_new0(uint32_t, numvalues * 2);
  461. cellnum = 0;
  462. for (vnum = 0; vnum < numvalues; vnum++) {
  463. ncells = values[vnum * 2];
  464. if (ncells != 1 && ncells != 2) {
  465. ret = -1;
  466. goto out;
  467. }
  468. value = values[vnum * 2 + 1];
  469. hival = cpu_to_be32(value >> 32);
  470. if (ncells > 1) {
  471. propcells[cellnum++] = hival;
  472. } else if (hival != 0) {
  473. ret = -1;
  474. goto out;
  475. }
  476. propcells[cellnum++] = cpu_to_be32(value);
  477. }
  478. ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
  479. cellnum * sizeof(uint32_t));
  480. out:
  481. g_free(propcells);
  482. return ret;
  483. }