2
0

device_tree.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "sysemu.h"
  22. #include "device_tree.h"
  23. #include <libfdt.h>
  24. void *load_device_tree(const char *filename_path, void *load_addr)
  25. {
  26. int dt_file_size;
  27. int dt_file_load_size;
  28. int new_dt_size;
  29. int ret;
  30. void *dt_file = NULL;
  31. void *fdt;
  32. dt_file_size = get_image_size(filename_path);
  33. if (dt_file_size < 0) {
  34. printf("Unable to get size of device tree file '%s'\n",
  35. filename_path);
  36. goto fail;
  37. }
  38. /* First allocate space in qemu for device tree */
  39. dt_file = qemu_mallocz(dt_file_size);
  40. dt_file_load_size = load_image(filename_path, dt_file);
  41. /* Second we place new copy of 2x size in guest memory
  42. * This give us enough room for manipulation.
  43. */
  44. new_dt_size = dt_file_size * 2;
  45. fdt = load_addr;
  46. ret = fdt_open_into(dt_file, fdt, new_dt_size);
  47. if (ret) {
  48. printf("Unable to copy device tree in memory\n");
  49. goto fail;
  50. }
  51. /* Check sanity of device tree */
  52. if (fdt_check_header(fdt)) {
  53. printf ("Device tree file loaded into memory is invalid: %s\n",
  54. filename_path);
  55. goto fail;
  56. }
  57. /* free qemu memory with old device tree */
  58. qemu_free(dt_file);
  59. return fdt;
  60. fail:
  61. qemu_free(dt_file);
  62. return NULL;
  63. }
  64. int qemu_devtree_setprop(void *fdt, const char *node_path,
  65. const char *property, uint32_t *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. }