2
0

device_tree.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <libfdt.h>
  24. void *load_device_tree(const char *filename_path, int *sizep)
  25. {
  26. int dt_size;
  27. int dt_file_load_size;
  28. int ret;
  29. void *fdt = NULL;
  30. *sizep = 0;
  31. dt_size = get_image_size(filename_path);
  32. if (dt_size < 0) {
  33. printf("Unable to get size of device tree file '%s'\n",
  34. filename_path);
  35. goto fail;
  36. }
  37. /* Expand to 2x size to give enough room for manipulation. */
  38. dt_size *= 2;
  39. /* First allocate space in qemu for device tree */
  40. fdt = qemu_mallocz(dt_size);
  41. dt_file_load_size = load_image(filename_path, fdt);
  42. if (dt_file_load_size < 0) {
  43. printf("Unable to open device tree file '%s'\n",
  44. filename_path);
  45. goto fail;
  46. }
  47. ret = fdt_open_into(fdt, fdt, dt_size);
  48. if (ret) {
  49. printf("Unable to copy device tree in memory\n");
  50. goto fail;
  51. }
  52. /* Check sanity of device tree */
  53. if (fdt_check_header(fdt)) {
  54. printf ("Device tree file loaded into memory is invalid: %s\n",
  55. filename_path);
  56. goto fail;
  57. }
  58. *sizep = dt_size;
  59. return fdt;
  60. fail:
  61. qemu_free(fdt);
  62. return NULL;
  63. }
  64. int qemu_devtree_setprop(void *fdt, const char *node_path,
  65. const char *property, void *val_array, int size)
  66. {
  67. int offset;
  68. offset = fdt_path_offset(fdt, node_path);
  69. if (offset < 0)
  70. return offset;
  71. return fdt_setprop(fdt, offset, property, val_array, size);
  72. }
  73. int qemu_devtree_setprop_cell(void *fdt, const char *node_path,
  74. const char *property, uint32_t val)
  75. {
  76. int offset;
  77. offset = fdt_path_offset(fdt, node_path);
  78. if (offset < 0)
  79. return offset;
  80. return fdt_setprop_cell(fdt, offset, property, val);
  81. }
  82. int qemu_devtree_setprop_string(void *fdt, const char *node_path,
  83. const char *property, const char *string)
  84. {
  85. int offset;
  86. offset = fdt_path_offset(fdt, node_path);
  87. if (offset < 0)
  88. return offset;
  89. return fdt_setprop_string(fdt, offset, property, string);
  90. }